pn81a.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* drivers/nfc/ese/pn81a.c
  2. *
  3. * Copyright 2017 Google Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/fs.h>
  12. #include <linux/delay.h>
  13. #include <linux/gpio.h>
  14. #include <linux/of_gpio.h>
  15. #include <linux/of_device.h>
  16. #include <linux/spi/spi.h>
  17. #include <linux/uaccess.h>
  18. #ifdef CONFIG_COMPAT
  19. #include <linux/compat.h>
  20. #endif
  21. /* Relies on struct nfcc_data and nqx_ese_pwr().
  22. * This can easily be made generic is a function is exported
  23. * that happily takes a void *.
  24. */
  25. #include "../nq-nci.h"
  26. #define PN81A_MAX_BUF 258U
  27. struct ese_dev {
  28. struct spi_device *spi;
  29. struct device *nfcc_device;
  30. struct nqx_dev *nfcc_data;
  31. struct mutex mutex;
  32. struct miscdevice device;
  33. int gpio_clear_n;
  34. const char *nfcc_name;
  35. char *kbuf;
  36. };
  37. static long ese_clear_gpio(struct ese_dev *ese_dev, unsigned long arg)
  38. {
  39. long r = 0;
  40. int val = arg ? 1 : 0;
  41. if (ese_dev->gpio_clear_n == -EINVAL) {
  42. r = -ENODEV;
  43. } else {
  44. gpio_set_value(ese_dev->gpio_clear_n, val);
  45. usleep_range(1000, 1100);
  46. if (gpio_get_value(ese_dev->gpio_clear_n) != val) {
  47. dev_err(&ese_dev->spi->dev,
  48. "gpio_clear_n won't change to %d\n", val);
  49. r = -EBUSY;
  50. } else {
  51. dev_info(&ese_dev->spi->dev,
  52. "%s: gpio_clear_n changed: %d\n",
  53. __func__, val);
  54. }
  55. }
  56. return r;
  57. }
  58. #ifdef CONFIG_COMPAT
  59. static long ese_compat_ioctl(struct file *filp, unsigned int cmd,
  60. unsigned long arg)
  61. {
  62. long r = 0;
  63. struct ese_dev *ese_dev = filp->private_data;
  64. mutex_lock(&ese_dev->mutex);
  65. arg = (compat_u64)arg;
  66. switch (cmd) {
  67. case ESE_SET_PWR:
  68. nqx_ese_pwr(ese_dev->nfcc_data, arg);
  69. break;
  70. case ESE_GET_PWR:
  71. nqx_ese_pwr(ese_dev->nfcc_data, 3);
  72. break;
  73. case ESE_CLEAR_GPIO:
  74. r = ese_clear_gpio(ese_dev, arg);
  75. break;
  76. default:
  77. r = -ENOTTY;
  78. }
  79. mutex_unlock(&ese_dev->mutex);
  80. return r;
  81. }
  82. #endif
  83. static long ese_ioctl(struct file *filp, unsigned int cmd,
  84. unsigned long arg)
  85. {
  86. int r = 0;
  87. struct ese_dev *ese_dev = filp->private_data;
  88. mutex_lock(&ese_dev->mutex);
  89. switch (cmd) {
  90. case ESE_SET_PWR:
  91. r = nqx_ese_pwr(ese_dev->nfcc_data, arg);
  92. break;
  93. case ESE_GET_PWR:
  94. r = nqx_ese_pwr(ese_dev->nfcc_data, 3);
  95. break;
  96. case ESE_CLEAR_GPIO:
  97. r = ese_clear_gpio(ese_dev, arg);
  98. break;
  99. default:
  100. r = -ENOIOCTLCMD;
  101. }
  102. mutex_unlock(&ese_dev->mutex);
  103. return r;
  104. }
  105. static int ese_open(struct inode *inode, struct file *filp)
  106. {
  107. int pwr = 0;
  108. struct ese_dev *ese_dev = container_of(filp->private_data,
  109. struct ese_dev, device);
  110. mutex_lock(&ese_dev->mutex);
  111. /* Find the NFC parent device if it exists. */
  112. if (ese_dev->nfcc_data == NULL) {
  113. struct device *nfc_dev = bus_find_device_by_name(
  114. &i2c_bus_type,
  115. NULL,
  116. ese_dev->nfcc_name);
  117. if (!nfc_dev) {
  118. dev_err(&ese_dev->spi->dev,
  119. "%s: cannot find NFC controller '%s'\n",
  120. __func__, ese_dev->nfcc_name);
  121. goto err;
  122. }
  123. ese_dev->nfcc_data = dev_get_drvdata(nfc_dev);
  124. if (!ese_dev->nfcc_data) {
  125. dev_err(&ese_dev->spi->dev,
  126. "%s: cannot find NFC controller device data\n",
  127. __func__);
  128. put_device(nfc_dev);
  129. goto err;
  130. }
  131. dev_info(&ese_dev->spi->dev,
  132. "%s: NFC controller found\n", __func__);
  133. ese_dev->nfcc_device = nfc_dev;
  134. }
  135. mutex_unlock(&ese_dev->mutex);
  136. if (nqx_claim_ese(ese_dev->nfcc_data, true) != 0)
  137. return -EBUSY;
  138. filp->private_data = ese_dev;
  139. dev_dbg(&ese_dev->spi->dev,
  140. "%s: major,minor: %d,%d\n",
  141. __func__, imajor(inode), iminor(inode));
  142. /* Note, opening and closing is treated wholly independently
  143. * from power management. This ensures that the eSE can go to
  144. * a deep power down state as dictated by the software stack.
  145. */
  146. pwr = ese_ioctl(filp, ESE_GET_PWR, 0);
  147. dev_dbg(&ese_dev->spi->dev,
  148. "%s: eSE opened (power: %d)\n", __func__, pwr);
  149. return 0;
  150. err:
  151. mutex_unlock(&ese_dev->mutex);
  152. return -ENODEV;
  153. }
  154. static int ese_release(struct inode *ino, struct file *filp)
  155. {
  156. struct ese_dev *ese_dev = filp->private_data;
  157. int pwr = 0;
  158. nqx_claim_ese(ese_dev->nfcc_data, false);
  159. pwr = ese_ioctl(filp, ESE_GET_PWR, 0);
  160. mutex_lock(&ese_dev->mutex);
  161. dev_dbg(&ese_dev->spi->dev,
  162. "%s: power: %d\n", __func__, pwr);
  163. mutex_unlock(&ese_dev->mutex);
  164. return 0;
  165. }
  166. static ssize_t ese_write(struct file *filp, const char __user *ubuf,
  167. size_t len, loff_t *offset)
  168. {
  169. struct ese_dev *ese_dev = filp->private_data;
  170. ssize_t ret = -EFAULT;
  171. size_t bytes = len;
  172. char *tx_buf = NULL;
  173. if (len > INT_MAX)
  174. return -EINVAL;
  175. mutex_lock(&ese_dev->mutex);
  176. while (bytes > 0) {
  177. size_t block = bytes < PN81A_MAX_BUF ? bytes : PN81A_MAX_BUF;
  178. tx_buf = memdup_user(ubuf, block);
  179. if (IS_ERR(tx_buf)) {
  180. dev_dbg(&ese_dev->spi->dev, "memdup_user failed\n");
  181. ret = PTR_ERR(tx_buf);
  182. goto err;
  183. }
  184. ret = spi_write(ese_dev->spi, tx_buf, block);
  185. if (ret < 0) {
  186. dev_dbg(&ese_dev->spi->dev, "failed to write to SPI\n");
  187. kfree(tx_buf);
  188. goto err;
  189. }
  190. kfree(tx_buf);
  191. ubuf += block;
  192. bytes -= block;
  193. }
  194. ret = len;
  195. err:
  196. mutex_unlock(&ese_dev->mutex);
  197. return ret;
  198. }
  199. static ssize_t ese_read(struct file *filp, char __user *ubuf,
  200. size_t len, loff_t *offset)
  201. {
  202. struct ese_dev *ese_dev = filp->private_data;
  203. ssize_t ret = -EFAULT;
  204. size_t bytes = len;
  205. char *rx_buf = NULL;
  206. if (len > INT_MAX)
  207. return -EINVAL;
  208. mutex_lock(&ese_dev->mutex);
  209. while (bytes > 0) {
  210. size_t block = bytes < PN81A_MAX_BUF ? bytes : PN81A_MAX_BUF;
  211. rx_buf = ese_dev->kbuf;
  212. if (!rx_buf) {
  213. dev_dbg(&ese_dev->spi->dev, "rx_buf does not exist anymore\n");
  214. ret = -ENOMEM;
  215. goto err;
  216. }
  217. memset(rx_buf, 0, PN81A_MAX_BUF);
  218. ret = spi_read(ese_dev->spi, rx_buf, block);
  219. if (ret < 0) {
  220. dev_dbg(&ese_dev->spi->dev, "failed to read from SPI\n");
  221. goto err;
  222. }
  223. if (copy_to_user(ubuf, rx_buf, block)) {
  224. dev_dbg(&ese_dev->spi->dev, "failed to copy from user\n");
  225. goto err;
  226. }
  227. ubuf += block;
  228. bytes -= block;
  229. }
  230. ret = len;
  231. err:
  232. mutex_unlock(&ese_dev->mutex);
  233. return ret;
  234. }
  235. static const struct file_operations ese_dev_fops = {
  236. .owner = THIS_MODULE,
  237. .llseek = no_llseek,
  238. .read = ese_read,
  239. .write = ese_write,
  240. .open = ese_open,
  241. .release = ese_release,
  242. .unlocked_ioctl = ese_ioctl,
  243. #ifdef CONFIG_COMPAT
  244. .compat_ioctl = ese_compat_ioctl
  245. #endif
  246. };
  247. static int pn81a_probe(struct spi_device *spi)
  248. {
  249. struct ese_dev *ese_dev;
  250. struct device_node *np = dev_of_node(&spi->dev);
  251. int ret;
  252. int gpio;
  253. dev_dbg(&spi->dev, "%s: called\n", __func__);
  254. if (!np) {
  255. dev_err(&spi->dev, "%s: device tree data missing\n", __func__);
  256. return -EINVAL;
  257. }
  258. ese_dev = kzalloc(sizeof(*ese_dev), GFP_KERNEL);
  259. if (ese_dev == NULL)
  260. return -ENOMEM;
  261. ese_dev->kbuf = kzalloc(PN81A_MAX_BUF, GFP_KERNEL);
  262. if (ese_dev->kbuf == NULL)
  263. return -ENOMEM;
  264. ese_dev->spi = spi;
  265. ese_dev->device.minor = MISC_DYNAMIC_MINOR;
  266. ese_dev->device.name = "pn81a";
  267. ese_dev->device.fops = &ese_dev_fops;
  268. spi->bits_per_word = 8;
  269. gpio = of_get_named_gpio(np, "nxp,clear-n", 0);
  270. if (!gpio_is_valid(gpio)) {
  271. dev_warn(&spi->dev,
  272. "%s: nxp,clear-n invalid or missing in device tree\n",
  273. __func__);
  274. gpio = -EINVAL;
  275. }
  276. ese_dev->gpio_clear_n = gpio;
  277. if (ese_dev->gpio_clear_n != -EINVAL) {
  278. ret = gpio_request(ese_dev->gpio_clear_n, "ese_clear_n");
  279. if (ret) {
  280. dev_warn(&spi->dev,
  281. "%s: unable to request ese clear n gpio [%d]\n",
  282. __func__,
  283. gpio);
  284. ese_dev->gpio_clear_n = -EINVAL;
  285. goto skip_gpio;
  286. }
  287. ret = gpio_direction_output(ese_dev->gpio_clear_n, 1);
  288. if (ret) {
  289. dev_warn(&spi->dev,
  290. "%s: failed to set direction for ese clear n[%d]\n",
  291. __func__,
  292. ese_dev->gpio_clear_n);
  293. }
  294. }
  295. skip_gpio:
  296. mutex_init(&ese_dev->mutex);
  297. ret = of_property_read_string(np, "nxp,nfcc", &ese_dev->nfcc_name);
  298. if (ret < 0) {
  299. dev_err(&spi->dev,
  300. "%s: nxp,nfcc invalid or missing in device tree (%d)\n",
  301. __func__, ret);
  302. goto err;
  303. }
  304. dev_info(&spi->dev, "%s: device tree set '%s' as eSE power controller\n",
  305. __func__, ese_dev->nfcc_name);
  306. ret = misc_register(&ese_dev->device);
  307. if (ret) {
  308. dev_err(&spi->dev, "%s: misc_register failed\n", __func__);
  309. goto err;
  310. }
  311. dev_info(&spi->dev, "%s: eSE is configured\n", __func__);
  312. spi_set_drvdata(spi, ese_dev);
  313. return 0;
  314. err:
  315. mutex_destroy(&ese_dev->mutex);
  316. kfree(ese_dev->kbuf);
  317. kfree(ese_dev);
  318. return ret;
  319. }
  320. static int pn81a_remove(struct spi_device *spi)
  321. {
  322. struct ese_dev *ese_dev = spi_get_drvdata(spi);
  323. int ret = 0;
  324. if (!ese_dev) {
  325. dev_err(&spi->dev,
  326. "%s: device doesn't exist anymore\n", __func__);
  327. ret = -ENODEV;
  328. goto err;
  329. }
  330. /* If we have a NFC device, release it. */
  331. if (ese_dev->nfcc_device) {
  332. put_device(ese_dev->nfcc_device);
  333. ese_dev->nfcc_data = NULL;
  334. ese_dev->nfcc_device = NULL;
  335. }
  336. misc_deregister(&ese_dev->device);
  337. mutex_destroy(&ese_dev->mutex);
  338. if (ese_dev->gpio_clear_n != -EINVAL)
  339. gpio_free(ese_dev->gpio_clear_n);
  340. kfree(ese_dev->kbuf);
  341. kfree(ese_dev);
  342. err:
  343. return ret;
  344. }
  345. static const struct of_device_id pn81a_match_table[] = {
  346. { .compatible = "nxp,pn81a" },
  347. { }
  348. };
  349. MODULE_DEVICE_TABLE(of, pn81a_match_table);
  350. static struct spi_driver pn81a_driver = {
  351. .driver = {
  352. .name = "pn81a",
  353. .of_match_table = pn81a_match_table,
  354. },
  355. .probe = pn81a_probe,
  356. .remove = pn81a_remove,
  357. };
  358. module_spi_driver(pn81a_driver);
  359. MODULE_DESCRIPTION("PN81A eSE driver");
  360. MODULE_ALIAS("spi:pn81a");
  361. MODULE_AUTHOR("Google Inc");
  362. MODULE_LICENSE("GPL");