common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /******************************************************************************
  2. * Copyright (C) 2015, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2019-2022 NXP
  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 as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. ******************************************************************************/
  20. #include <linux/gpio.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/delay.h>
  23. #include "common_ese.h"
  24. int nfc_parse_dt(struct device *dev, struct platform_configs *nfc_configs,
  25. uint8_t interface)
  26. {
  27. struct device_node *np = dev->of_node;
  28. struct platform_gpio *nfc_gpio = &nfc_configs->gpio;
  29. if (!np) {
  30. pr_err("%s: nfc of_node NULL\n", __func__);
  31. return -EINVAL;
  32. }
  33. nfc_gpio->irq = -EINVAL;
  34. nfc_gpio->dwl_req = -EINVAL;
  35. nfc_gpio->ven = -EINVAL;
  36. /* irq required for i2c based chips only */
  37. if (interface == PLATFORM_IF_I2C) {
  38. nfc_gpio->irq = of_get_named_gpio(np, DTS_IRQ_GPIO_STR, 0);
  39. if ((!gpio_is_valid(nfc_gpio->irq))) {
  40. pr_err("%s: irq gpio invalid %d\n", __func__,
  41. nfc_gpio->irq);
  42. return nfc_gpio->irq;
  43. }
  44. pr_info("%s: irq %d\n", __func__, nfc_gpio->irq);
  45. }
  46. nfc_gpio->ven = of_get_named_gpio(np, DTS_VEN_GPIO_STR, 0);
  47. if ((!gpio_is_valid(nfc_gpio->ven))) {
  48. pr_err("%s: ven gpio invalid %d\n", __func__, nfc_gpio->ven);
  49. return nfc_gpio->ven;
  50. }
  51. /* some products like sn220 does not required fw dwl pin */
  52. nfc_gpio->dwl_req = of_get_named_gpio(np, DTS_FWDN_GPIO_STR, 0);
  53. if ((!gpio_is_valid(nfc_gpio->dwl_req)))
  54. pr_warn("%s: dwl_req gpio invalid %d\n", __func__,
  55. nfc_gpio->dwl_req);
  56. pr_info("%s: %d, %d, %d\n", __func__, nfc_gpio->irq, nfc_gpio->ven,
  57. nfc_gpio->dwl_req);
  58. return 0;
  59. }
  60. void set_valid_gpio(int gpio, int value)
  61. {
  62. if (gpio_is_valid(gpio)) {
  63. pr_debug("%s: gpio %d value %d\n", __func__, gpio, value);
  64. gpio_set_value(gpio, value);
  65. /* hardware dependent delay */
  66. usleep_range(NFC_GPIO_SET_WAIT_TIME_US,
  67. NFC_GPIO_SET_WAIT_TIME_US + 100);
  68. }
  69. }
  70. int get_valid_gpio(int gpio)
  71. {
  72. int value = -EINVAL;
  73. if (gpio_is_valid(gpio)) {
  74. value = gpio_get_value(gpio);
  75. pr_debug("%s: gpio %d value %d\n", __func__, gpio, value);
  76. }
  77. return value;
  78. }
  79. void gpio_set_ven(struct nfc_dev *nfc_dev, int value)
  80. {
  81. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  82. if (gpio_get_value(nfc_gpio->ven) != value) {
  83. pr_debug("%s: value %d\n", __func__, value);
  84. /* reset on change in level from high to low */
  85. if (value)
  86. ese_cold_reset_release(nfc_dev);
  87. gpio_set_value(nfc_gpio->ven, value);
  88. /* hardware dependent delay */
  89. usleep_range(NFC_GPIO_SET_WAIT_TIME_US,
  90. NFC_GPIO_SET_WAIT_TIME_US + 100);
  91. }
  92. }
  93. int configure_gpio(unsigned int gpio, int flag)
  94. {
  95. int ret;
  96. pr_debug("%s: nfc gpio [%d] flag [%01x]\n", __func__, gpio, flag);
  97. if (gpio_is_valid(gpio)) {
  98. ret = gpio_request(gpio, "nfc_gpio");
  99. if (ret) {
  100. pr_err("%s: unable to request nfc gpio [%d]\n",
  101. __func__, gpio);
  102. return ret;
  103. }
  104. /* set direction and value for output pin */
  105. if (flag & GPIO_OUTPUT) {
  106. ret = gpio_direction_output(gpio, (GPIO_HIGH & flag));
  107. pr_debug("%s: nfc o/p gpio %d level %d\n", __func__,
  108. gpio, gpio_get_value(gpio));
  109. } else {
  110. ret = gpio_direction_input(gpio);
  111. pr_debug("%s: nfc i/p gpio %d\n", __func__, gpio);
  112. }
  113. if (ret) {
  114. pr_err("%s: unable to set direction for nfc gpio [%d]\n",
  115. __func__, gpio);
  116. gpio_free(gpio);
  117. return ret;
  118. }
  119. /* Consider value as control for input IRQ pin */
  120. if (flag & GPIO_IRQ) {
  121. ret = gpio_to_irq(gpio);
  122. if (ret < 0) {
  123. pr_err("%s: unable to set irq [%d]\n", __func__,
  124. gpio);
  125. gpio_free(gpio);
  126. return ret;
  127. }
  128. pr_debug("%s: gpio_to_irq successful [%d]\n", __func__,
  129. gpio);
  130. return ret;
  131. }
  132. } else {
  133. pr_err("%s: invalid gpio\n", __func__);
  134. ret = -EINVAL;
  135. }
  136. return ret;
  137. }
  138. void gpio_free_all(struct nfc_dev *nfc_dev)
  139. {
  140. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  141. if (gpio_is_valid(nfc_gpio->dwl_req))
  142. gpio_free(nfc_gpio->dwl_req);
  143. if (gpio_is_valid(nfc_gpio->irq))
  144. gpio_free(nfc_gpio->irq);
  145. if (gpio_is_valid(nfc_gpio->ven))
  146. gpio_free(nfc_gpio->ven);
  147. }
  148. void nfc_misc_unregister(struct nfc_dev *nfc_dev, int count)
  149. {
  150. pr_debug("%s: entry\n", __func__);
  151. device_destroy(nfc_dev->nfc_class, nfc_dev->devno);
  152. cdev_del(&nfc_dev->c_dev);
  153. class_destroy(nfc_dev->nfc_class);
  154. unregister_chrdev_region(nfc_dev->devno, count);
  155. }
  156. int nfc_misc_register(struct nfc_dev *nfc_dev,
  157. const struct file_operations *nfc_fops, int count,
  158. char *devname, char *classname)
  159. {
  160. int ret = 0;
  161. ret = alloc_chrdev_region(&nfc_dev->devno, 0, count, devname);
  162. if (ret < 0) {
  163. pr_err("%s: failed to alloc chrdev region ret %d\n", __func__,
  164. ret);
  165. return ret;
  166. }
  167. nfc_dev->nfc_class = class_create(THIS_MODULE, classname);
  168. if (IS_ERR(nfc_dev->nfc_class)) {
  169. ret = PTR_ERR(nfc_dev->nfc_class);
  170. pr_err("%s: failed to register device class ret %d\n", __func__,
  171. ret);
  172. unregister_chrdev_region(nfc_dev->devno, count);
  173. return ret;
  174. }
  175. cdev_init(&nfc_dev->c_dev, nfc_fops);
  176. ret = cdev_add(&nfc_dev->c_dev, nfc_dev->devno, count);
  177. if (ret < 0) {
  178. pr_err("%s: failed to add cdev ret %d\n", __func__, ret);
  179. class_destroy(nfc_dev->nfc_class);
  180. unregister_chrdev_region(nfc_dev->devno, count);
  181. return ret;
  182. }
  183. nfc_dev->nfc_device = device_create(nfc_dev->nfc_class, NULL,
  184. nfc_dev->devno, nfc_dev, devname);
  185. if (IS_ERR(nfc_dev->nfc_device)) {
  186. ret = PTR_ERR(nfc_dev->nfc_device);
  187. pr_err("%s: failed to create the device ret %d\n", __func__,
  188. ret);
  189. cdev_del(&nfc_dev->c_dev);
  190. class_destroy(nfc_dev->nfc_class);
  191. unregister_chrdev_region(nfc_dev->devno, count);
  192. return ret;
  193. }
  194. return 0;
  195. }
  196. /**
  197. * nfc_ioctl_power_states() - power control
  198. * @nfc_dev: nfc device data structure
  199. * @arg: mode that we want to move to
  200. *
  201. * Device power control. Depending on the arg value, device moves to
  202. * different states, refer common.h for args
  203. *
  204. * Return: -ENOIOCTLCMD if arg is not supported, 0 if Success(or no issue)
  205. * and error ret code otherwise
  206. */
  207. static int nfc_ioctl_power_states(struct nfc_dev *nfc_dev, unsigned long arg)
  208. {
  209. int ret = 0;
  210. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  211. if (arg == NFC_POWER_OFF) {
  212. /*
  213. * We are attempting a hardware reset so let us disable
  214. * interrupts to avoid spurious notifications to upper
  215. * layers.
  216. */
  217. nfc_dev->nfc_disable_intr(nfc_dev);
  218. set_valid_gpio(nfc_gpio->dwl_req, 0);
  219. gpio_set_ven(nfc_dev, 0);
  220. nfc_dev->nfc_ven_enabled = false;
  221. } else if (arg == NFC_POWER_ON) {
  222. nfc_dev->nfc_enable_intr(nfc_dev);
  223. set_valid_gpio(nfc_gpio->dwl_req, 0);
  224. gpio_set_ven(nfc_dev, 1);
  225. nfc_dev->nfc_ven_enabled = true;
  226. } else if (arg == NFC_FW_DWL_VEN_TOGGLE) {
  227. /*
  228. * We are switching to download Mode, toggle the enable pin
  229. * in order to set the NFCC in the new mode
  230. */
  231. nfc_dev->nfc_disable_intr(nfc_dev);
  232. set_valid_gpio(nfc_gpio->dwl_req, 1);
  233. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  234. gpio_set_ven(nfc_dev, 0);
  235. gpio_set_ven(nfc_dev, 1);
  236. nfc_dev->nfc_enable_intr(nfc_dev);
  237. } else if (arg == NFC_FW_DWL_HIGH) {
  238. /*
  239. * Setting firmware download gpio to HIGH
  240. * before FW download start
  241. */
  242. set_valid_gpio(nfc_gpio->dwl_req, 1);
  243. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  244. } else if (arg == NFC_VEN_FORCED_HARD_RESET) {
  245. nfc_dev->nfc_disable_intr(nfc_dev);
  246. gpio_set_ven(nfc_dev, 0);
  247. gpio_set_ven(nfc_dev, 1);
  248. nfc_dev->nfc_enable_intr(nfc_dev);
  249. } else if (arg == NFC_FW_DWL_LOW) {
  250. /*
  251. * Setting firmware download gpio to LOW
  252. * FW download finished
  253. */
  254. set_valid_gpio(nfc_gpio->dwl_req, 0);
  255. nfc_dev->nfc_state = NFC_STATE_NCI;
  256. } else {
  257. pr_err("%s: bad arg %lu\n", __func__, arg);
  258. ret = -ENOIOCTLCMD;
  259. }
  260. return ret;
  261. }
  262. #ifdef CONFIG_COMPAT
  263. /**
  264. * nfc_dev_compat_ioctl - used to set or get data from upper layer.
  265. * @pfile file node for opened device.
  266. * @cmd ioctl type from upper layer.
  267. * @arg ioctl arg from upper layer.
  268. *
  269. * NFC and ESE Device power control, based on the argument value
  270. *
  271. * Return: -ENOIOCTLCMD if arg is not supported
  272. * 0 if Success(or no issue)
  273. * 0 or 1 in case of arg is ESE_GET_PWR/ESE_POWER_STATE
  274. * and error ret code otherwise
  275. */
  276. long nfc_dev_compat_ioctl(struct file *pfile, unsigned int cmd,
  277. unsigned long arg)
  278. {
  279. int ret = 0;
  280. arg = (compat_u64) arg;
  281. pr_debug("%s: cmd = %x arg = %zx\n", __func__, cmd, arg);
  282. ret = nfc_dev_ioctl(pfile, cmd, arg);
  283. return ret;
  284. }
  285. #endif
  286. /**
  287. * nfc_dev_ioctl - used to set or get data from upper layer.
  288. * @pfile file node for opened device.
  289. * @cmd ioctl type from upper layer.
  290. * @arg ioctl arg from upper layer.
  291. *
  292. * NFC and ESE Device power control, based on the argument value
  293. *
  294. * Return: -ENOIOCTLCMD if arg is not supported
  295. * 0 if Success(or no issue)
  296. * 0 or 1 in case of arg is ESE_GET_PWR/ESE_POWER_STATE
  297. * and error ret code otherwise
  298. */
  299. long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
  300. {
  301. int ret = 0;
  302. struct nfc_dev *nfc_dev = pfile->private_data;
  303. if (!nfc_dev)
  304. return -ENODEV;
  305. pr_debug("%s: cmd = %x arg = %zx\n", __func__, cmd, arg);
  306. switch (cmd) {
  307. case NFC_SET_PWR:
  308. ret = nfc_ioctl_power_states(nfc_dev, arg);
  309. break;
  310. case NFC_SET_RESET_READ_PENDING:
  311. if (arg == NFC_SET_READ_PENDING) {
  312. nfc_dev->cold_reset.is_nfc_read_pending = true;
  313. /* Set default NFC state as NCI for Nfc read pending request */
  314. nfc_dev->nfc_state = NFC_STATE_NCI;
  315. }
  316. else if (arg == NFC_RESET_READ_PENDING){
  317. nfc_dev->cold_reset.is_nfc_read_pending = false;
  318. }
  319. else {
  320. ret = -EINVAL;
  321. }
  322. break;
  323. case ESE_SET_PWR:
  324. ret = nfc_ese_pwr(nfc_dev, arg);
  325. break;
  326. case ESE_GET_PWR:
  327. ret = nfc_ese_pwr(nfc_dev, ESE_POWER_STATE);
  328. break;
  329. default:
  330. pr_err("%s: bad cmd %lu\n", __func__, arg);
  331. ret = -ENOIOCTLCMD;
  332. };
  333. return ret;
  334. }
  335. int nfc_dev_open(struct inode *inode, struct file *filp)
  336. {
  337. struct nfc_dev *nfc_dev = NULL;
  338. nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
  339. if (!nfc_dev)
  340. return -ENODEV;
  341. pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  342. mutex_lock(&nfc_dev->dev_ref_mutex);
  343. filp->private_data = nfc_dev;
  344. if (nfc_dev->dev_ref_count == 0) {
  345. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  346. nfc_dev->nfc_enable_intr(nfc_dev);
  347. }
  348. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count + 1;
  349. mutex_unlock(&nfc_dev->dev_ref_mutex);
  350. return 0;
  351. }
  352. int nfc_dev_flush(struct file *pfile, fl_owner_t id)
  353. {
  354. struct nfc_dev *nfc_dev = pfile->private_data;
  355. if (!nfc_dev)
  356. return -ENODEV;
  357. /*
  358. * release blocked user thread waiting for pending read during close
  359. */
  360. if (!mutex_trylock(&nfc_dev->read_mutex)) {
  361. nfc_dev->release_read = true;
  362. nfc_dev->nfc_disable_intr(nfc_dev);
  363. wake_up(&nfc_dev->read_wq);
  364. pr_debug("%s: waiting for release of blocked read\n", __func__);
  365. mutex_lock(&nfc_dev->read_mutex);
  366. nfc_dev->release_read = false;
  367. } else {
  368. pr_debug("%s: read thread already released\n", __func__);
  369. }
  370. mutex_unlock(&nfc_dev->read_mutex);
  371. return 0;
  372. }
  373. int nfc_dev_close(struct inode *inode, struct file *filp)
  374. {
  375. struct nfc_dev *nfc_dev = NULL;
  376. nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
  377. if (!nfc_dev)
  378. return -ENODEV;
  379. pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  380. mutex_lock(&nfc_dev->dev_ref_mutex);
  381. if (nfc_dev->dev_ref_count == 1) {
  382. nfc_dev->nfc_disable_intr(nfc_dev);
  383. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  384. /*
  385. * Use "ESE_RST_PROT_DIS" as argument
  386. * if eSE calls flow is via NFC driver
  387. * i.e. direct calls from SPI HAL to NFC driver
  388. */
  389. mutex_unlock(&nfc_dev->dev_ref_mutex);
  390. nfc_ese_pwr(nfc_dev, ESE_RST_PROT_DIS_NFC);
  391. mutex_lock(&nfc_dev->dev_ref_mutex);
  392. }
  393. if (nfc_dev->dev_ref_count > 0)
  394. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count - 1;
  395. filp->private_data = NULL;
  396. mutex_unlock(&nfc_dev->dev_ref_mutex);
  397. return 0;
  398. }
  399. int validate_nfc_state_nci(struct nfc_dev *nfc_dev)
  400. {
  401. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  402. if (!gpio_get_value(nfc_gpio->ven)) {
  403. pr_err("%s: ven low - nfcc powered off\n", __func__);
  404. return -ENODEV;
  405. }
  406. if (get_valid_gpio(nfc_gpio->dwl_req) == 1) {
  407. pr_err("%s: fw download in-progress\n", __func__);
  408. return -EBUSY;
  409. }
  410. if (nfc_dev->nfc_state != NFC_STATE_NCI) {
  411. pr_err("%s: fw download state\n", __func__);
  412. return -EBUSY;
  413. }
  414. return 0;
  415. }