common.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /******************************************************************************
  2. * Copyright (C) 2015, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2019-2021 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. /*
  21. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  22. *
  23. *****************************************************************************/
  24. #include <linux/gpio.h>
  25. #include <linux/of_gpio.h>
  26. #include <linux/delay.h>
  27. #include "common.h"
  28. int nfc_parse_dt(struct device *dev, struct platform_configs *nfc_configs,
  29. uint8_t interface)
  30. {
  31. int ret;
  32. struct device_node *np = dev->of_node;
  33. struct platform_gpio *nfc_gpio = &nfc_configs->gpio;
  34. struct platform_ldo *ldo = &nfc_configs->ldo;
  35. if (!np) {
  36. pr_err("%s: nfc of_node NULL\n", __func__);
  37. return -EINVAL;
  38. }
  39. nfc_gpio->irq = -EINVAL;
  40. nfc_gpio->dwl_req = -EINVAL;
  41. nfc_gpio->ven = -EINVAL;
  42. /* irq required for i2c based chips only */
  43. if (interface == PLATFORM_IF_I2C) {
  44. nfc_gpio->irq = of_get_named_gpio(np, DTS_IRQ_GPIO_STR, 0);
  45. if ((!gpio_is_valid(nfc_gpio->irq))) {
  46. pr_err("%s: nfc irq gpio invalid %d\n", __func__,
  47. nfc_gpio->irq);
  48. return -EINVAL;
  49. }
  50. pr_info("%s: irq %d\n", __func__, nfc_gpio->irq);
  51. }
  52. nfc_gpio->ven = of_get_named_gpio(np, DTS_VEN_GPIO_STR, 0);
  53. if ((!gpio_is_valid(nfc_gpio->ven))) {
  54. pr_err("%s: nfc ven gpio invalid %d\n", __func__, nfc_gpio->ven);
  55. return -EINVAL;
  56. }
  57. /* some products like sn220 does not required fw dwl pin */
  58. nfc_gpio->dwl_req = of_get_named_gpio(np, DTS_FWDN_GPIO_STR, 0);
  59. /* not returning failure for dwl gpio as it is optional for sn220 */
  60. if ((!gpio_is_valid(nfc_gpio->dwl_req)))
  61. pr_warn("%s: nfc dwl_req gpio invalid %d\n", __func__,
  62. nfc_gpio->dwl_req);
  63. pr_info("%s: irq %d, ven %d, dwl %d\n", __func__, nfc_gpio->irq, nfc_gpio->ven,
  64. nfc_gpio->dwl_req);
  65. /* optional property */
  66. ret = of_property_read_u32_array(np, NFC_LDO_VOL_DT_NAME,
  67. (u32 *) ldo->vdd_levels,
  68. ARRAY_SIZE(ldo->vdd_levels));
  69. if (ret) {
  70. dev_err(dev, "error reading NFC VDDIO min and max value\n");
  71. // set default as per datasheet
  72. ldo->vdd_levels[0] = NFC_VDDIO_MIN;
  73. ldo->vdd_levels[1] = NFC_VDDIO_MAX;
  74. }
  75. /* optional property */
  76. ret = of_property_read_u32(np, NFC_LDO_CUR_DT_NAME, &ldo->max_current);
  77. if (ret) {
  78. dev_err(dev, "error reading NFC current value\n");
  79. // set default as per datasheet
  80. ldo->max_current = NFC_CURRENT_MAX;
  81. }
  82. return 0;
  83. }
  84. void set_valid_gpio(int gpio, int value)
  85. {
  86. if (gpio_is_valid(gpio)) {
  87. pr_debug("%s: gpio %d value %d\n", __func__, gpio, value);
  88. gpio_set_value(gpio, value);
  89. /* hardware dependent delay */
  90. usleep_range(NFC_GPIO_SET_WAIT_TIME_US,
  91. NFC_GPIO_SET_WAIT_TIME_US + 100);
  92. }
  93. }
  94. int get_valid_gpio(int gpio)
  95. {
  96. int value = -EINVAL;
  97. if (gpio_is_valid(gpio)) {
  98. value = gpio_get_value(gpio);
  99. pr_debug("%s: gpio %d value %d\n", __func__, gpio, value);
  100. }
  101. return value;
  102. }
  103. void gpio_set_ven(struct nfc_dev *nfc_dev, int value)
  104. {
  105. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  106. if (gpio_get_value(nfc_gpio->ven) != value) {
  107. pr_debug("%s: value %d\n", __func__, value);
  108. gpio_set_value(nfc_gpio->ven, value);
  109. /* hardware dependent delay */
  110. usleep_range(NFC_GPIO_SET_WAIT_TIME_US,
  111. NFC_GPIO_SET_WAIT_TIME_US + 100);
  112. }
  113. }
  114. int configure_gpio(unsigned int gpio, int flag)
  115. {
  116. int ret;
  117. pr_debug("%s: nfc gpio [%d] flag [%01x]\n", __func__, gpio, flag);
  118. if (gpio_is_valid(gpio)) {
  119. ret = gpio_request(gpio, "nfc_gpio");
  120. if (ret) {
  121. pr_err("%s: unable to request nfc gpio [%d]\n",
  122. __func__, gpio);
  123. return ret;
  124. }
  125. /* set direction and value for output pin */
  126. if (flag & GPIO_OUTPUT) {
  127. ret = gpio_direction_output(gpio, (GPIO_HIGH & flag));
  128. pr_debug("%s: nfc o/p gpio %d level %d\n", __func__,
  129. gpio, gpio_get_value(gpio));
  130. } else {
  131. ret = gpio_direction_input(gpio);
  132. pr_debug("%s: nfc i/p gpio %d\n", __func__, gpio);
  133. }
  134. if (ret) {
  135. pr_err("%s: unable to set direction for nfc gpio [%d]\n",
  136. __func__, gpio);
  137. gpio_free(gpio);
  138. return ret;
  139. }
  140. /* Consider value as control for input IRQ pin */
  141. if (flag & GPIO_IRQ) {
  142. ret = gpio_to_irq(gpio);
  143. if (ret < 0) {
  144. pr_err("%s: unable to set irq [%d]\n", __func__,
  145. gpio);
  146. gpio_free(gpio);
  147. return ret;
  148. }
  149. pr_debug("%s: gpio_to_irq successful [%d]\n", __func__,
  150. gpio);
  151. return ret;
  152. }
  153. } else {
  154. pr_err("%s: invalid gpio\n", __func__);
  155. ret = -EINVAL;
  156. }
  157. return ret;
  158. }
  159. void gpio_free_all(struct nfc_dev *nfc_dev)
  160. {
  161. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  162. if (gpio_is_valid(nfc_gpio->dwl_req))
  163. gpio_free(nfc_gpio->dwl_req);
  164. if (gpio_is_valid(nfc_gpio->irq))
  165. gpio_free(nfc_gpio->irq);
  166. if (gpio_is_valid(nfc_gpio->ven))
  167. gpio_free(nfc_gpio->ven);
  168. }
  169. void nfc_misc_unregister(struct nfc_dev *nfc_dev, int count)
  170. {
  171. pr_debug("%s: entry\n", __func__);
  172. kfree(nfc_dev->kbuf);
  173. device_destroy(nfc_dev->nfc_class, nfc_dev->devno);
  174. cdev_del(&nfc_dev->c_dev);
  175. class_destroy(nfc_dev->nfc_class);
  176. unregister_chrdev_region(nfc_dev->devno, count);
  177. if (nfc_dev->ipcl)
  178. ipc_log_context_destroy(nfc_dev->ipcl);
  179. }
  180. int nfc_misc_register(struct nfc_dev *nfc_dev,
  181. const struct file_operations *nfc_fops, int count,
  182. char *devname, char *classname)
  183. {
  184. int ret = 0;
  185. ret = alloc_chrdev_region(&nfc_dev->devno, 0, count, devname);
  186. if (ret < 0) {
  187. pr_err("%s: failed to alloc chrdev region ret %d\n", __func__,
  188. ret);
  189. return ret;
  190. }
  191. nfc_dev->nfc_class = class_create(THIS_MODULE, classname);
  192. if (IS_ERR(nfc_dev->nfc_class)) {
  193. ret = PTR_ERR(nfc_dev->nfc_class);
  194. pr_err("%s: failed to register device class ret %d\n", __func__,
  195. ret);
  196. unregister_chrdev_region(nfc_dev->devno, count);
  197. return ret;
  198. }
  199. cdev_init(&nfc_dev->c_dev, nfc_fops);
  200. ret = cdev_add(&nfc_dev->c_dev, nfc_dev->devno, count);
  201. if (ret < 0) {
  202. pr_err("%s: failed to add cdev ret %d\n", __func__, ret);
  203. class_destroy(nfc_dev->nfc_class);
  204. unregister_chrdev_region(nfc_dev->devno, count);
  205. return ret;
  206. }
  207. nfc_dev->nfc_device = device_create(nfc_dev->nfc_class, NULL,
  208. nfc_dev->devno, nfc_dev, devname);
  209. if (IS_ERR(nfc_dev->nfc_device)) {
  210. ret = PTR_ERR(nfc_dev->nfc_device);
  211. pr_err("%s: failed to create the device ret %d\n", __func__,
  212. ret);
  213. cdev_del(&nfc_dev->c_dev);
  214. class_destroy(nfc_dev->nfc_class);
  215. unregister_chrdev_region(nfc_dev->devno, count);
  216. return ret;
  217. }
  218. nfc_dev->ipcl = ipc_log_context_create(NUM_OF_IPC_LOG_PAGES,
  219. dev_name(nfc_dev->nfc_device), 0);
  220. nfc_dev->kbuflen = MAX_NCI_BUFFER_SIZE;
  221. nfc_dev->kbuf = kzalloc(MAX_NCI_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
  222. if (!nfc_dev->kbuf) {
  223. nfc_misc_unregister(nfc_dev, count);
  224. return -ENOMEM;
  225. }
  226. nfc_dev->cold_reset.rsp_pending = false;
  227. nfc_dev->cold_reset.is_nfc_enabled = false;
  228. nfc_dev->cold_reset.is_crp_en = false;
  229. nfc_dev->cold_reset.last_src_ese_prot = ESE_COLD_RESET_ORIGIN_NONE;
  230. init_waitqueue_head(&nfc_dev->cold_reset.read_wq);
  231. return 0;
  232. }
  233. /**
  234. * nfc_ioctl_power_states() - power control
  235. * @nfc_dev: nfc device data structure
  236. * @arg: mode that we want to move to
  237. *
  238. * Device power control. Depending on the arg value, device moves to
  239. * different states, refer common.h for args
  240. *
  241. * Return: -ENOIOCTLCMD if arg is not supported, 0 if Success(or no issue)
  242. * and error ret code otherwise
  243. */
  244. static int nfc_ioctl_power_states(struct nfc_dev *nfc_dev, unsigned long arg)
  245. {
  246. int ret = 0;
  247. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  248. if (arg == NFC_POWER_OFF) {
  249. /*
  250. * We are attempting a hardware reset so let us disable
  251. * interrupts to avoid spurious notifications to upper
  252. * layers.
  253. */
  254. nfc_dev->nfc_disable_intr(nfc_dev);
  255. set_valid_gpio(nfc_gpio->dwl_req, 0);
  256. gpio_set_ven(nfc_dev, 0);
  257. nfc_dev->nfc_ven_enabled = false;
  258. } else if (arg == NFC_POWER_ON) {
  259. nfc_dev->nfc_enable_intr(nfc_dev);
  260. set_valid_gpio(nfc_gpio->dwl_req, 0);
  261. gpio_set_ven(nfc_dev, 1);
  262. nfc_dev->nfc_ven_enabled = true;
  263. } else if (arg == NFC_FW_DWL_VEN_TOGGLE) {
  264. /*
  265. * We are switching to download Mode, toggle the enable pin
  266. * in order to set the NFCC in the new mode
  267. */
  268. nfc_dev->nfc_disable_intr(nfc_dev);
  269. set_valid_gpio(nfc_gpio->dwl_req, 1);
  270. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  271. gpio_set_ven(nfc_dev, 0);
  272. gpio_set_ven(nfc_dev, 1);
  273. nfc_dev->nfc_enable_intr(nfc_dev);
  274. } else if (arg == NFC_FW_DWL_HIGH) {
  275. /*
  276. * Setting firmware download gpio to HIGH
  277. * before FW download start
  278. */
  279. pr_debug("set fw gpio high\n");
  280. set_valid_gpio(nfc_gpio->dwl_req, 1);
  281. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  282. } else if (arg == NFC_VEN_FORCED_HARD_RESET) {
  283. nfc_dev->nfc_disable_intr(nfc_dev);
  284. gpio_set_ven(nfc_dev, 0);
  285. gpio_set_ven(nfc_dev, 1);
  286. nfc_dev->nfc_enable_intr(nfc_dev);
  287. pr_info("%s VEN forced reset done\n", __func__);
  288. } else if (arg == NFC_FW_DWL_LOW) {
  289. /*
  290. * Setting firmware download gpio to LOW
  291. * FW download finished
  292. */
  293. pr_debug("set fw gpio LOW\n");
  294. set_valid_gpio(nfc_gpio->dwl_req, 0);
  295. nfc_dev->nfc_state = NFC_STATE_NCI;
  296. } else if (arg == NFC_ENABLE) {
  297. /*
  298. * Setting flag true when NFC is enabled
  299. */
  300. nfc_dev->cold_reset.is_nfc_enabled = true;
  301. } else if (arg == NFC_DISABLE) {
  302. /*
  303. * Setting flag true when NFC is disabled
  304. */
  305. nfc_dev->cold_reset.is_nfc_enabled = false;
  306. } else {
  307. pr_err("%s bad arg %lu\n", __func__, arg);
  308. ret = -ENOIOCTLCMD;
  309. }
  310. return ret;
  311. }
  312. #ifdef CONFIG_COMPAT
  313. /**
  314. * nfc_dev_compat_ioctl - used to set or get data from upper layer.
  315. * @pfile file node for opened device.
  316. * @cmd ioctl type from upper layer.
  317. * @arg ioctl arg from upper layer.
  318. *
  319. * NFC and ESE Device power control, based on the argument value
  320. *
  321. * Return: -ENOIOCTLCMD if arg is not supported
  322. * 0 if Success(or no issue)
  323. * 0 or 1 in case of arg is ESE_GET_PWR/ESE_POWER_STATE
  324. * and error ret code otherwise
  325. */
  326. long nfc_dev_compat_ioctl(struct file *pfile, unsigned int cmd,
  327. unsigned long arg)
  328. {
  329. int ret = 0;
  330. arg = (compat_u64)arg;
  331. pr_debug("%s: cmd = %x arg = %zx\n", __func__, cmd, arg);
  332. ret = nfc_dev_ioctl(pfile, cmd, arg);
  333. return ret;
  334. }
  335. #endif
  336. /**
  337. * nfc_dev_ioctl - used to set or get data from upper layer.
  338. * @pfile file node for opened device.
  339. * @cmd ioctl type from upper layer.
  340. * @arg ioctl arg from upper layer.
  341. *
  342. * NFC and ESE Device power control, based on the argument value
  343. *
  344. * Return: -ENOIOCTLCMD if arg is not supported
  345. * 0 if Success(or no issue)
  346. * 0 or 1 in case of arg is ESE_GET_PWR/ESE_POWER_STATE
  347. * and error ret code otherwise
  348. */
  349. long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
  350. {
  351. int ret = 0;
  352. struct nfc_dev *nfc_dev = pfile->private_data;
  353. if (!nfc_dev)
  354. return -ENODEV;
  355. pr_debug("%s: cmd = %x arg = %zx\n", __func__, cmd, arg);
  356. switch (cmd) {
  357. case NFC_SET_PWR:
  358. ret = nfc_ioctl_power_states(nfc_dev, arg);
  359. break;
  360. case ESE_SET_PWR:
  361. ret = nfc_ese_pwr(nfc_dev, arg);
  362. break;
  363. case ESE_GET_PWR:
  364. ret = nfc_ese_pwr(nfc_dev, ESE_POWER_STATE);
  365. break;
  366. case NFCC_GET_INFO:
  367. ret = nfc_ioctl_nfcc_info(pfile, arg);
  368. break;
  369. case ESE_COLD_RESET:
  370. pr_debug("nfc ese cold reset ioctl\n");
  371. ret = ese_cold_reset_ioctl(nfc_dev, arg);
  372. break;
  373. default:
  374. pr_err("%s: bad cmd %lu\n", __func__, arg);
  375. ret = -ENOIOCTLCMD;
  376. }
  377. return ret;
  378. }
  379. int nfc_dev_open(struct inode *inode, struct file *filp)
  380. {
  381. struct nfc_dev *nfc_dev = NULL;
  382. nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
  383. if (!nfc_dev)
  384. return -ENODEV;
  385. pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  386. mutex_lock(&nfc_dev->dev_ref_mutex);
  387. filp->private_data = nfc_dev;
  388. if (nfc_dev->dev_ref_count == 0) {
  389. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  390. nfc_dev->nfc_enable_intr(nfc_dev);
  391. }
  392. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count + 1;
  393. mutex_unlock(&nfc_dev->dev_ref_mutex);
  394. return 0;
  395. }
  396. int nfc_dev_flush(struct file *pfile, fl_owner_t id)
  397. {
  398. struct nfc_dev *nfc_dev = pfile->private_data;
  399. if (!nfc_dev)
  400. return -ENODEV;
  401. /*
  402. * release blocked user thread waiting for pending read during close
  403. */
  404. if (!mutex_trylock(&nfc_dev->read_mutex)) {
  405. nfc_dev->release_read = true;
  406. nfc_dev->nfc_disable_intr(nfc_dev);
  407. wake_up(&nfc_dev->read_wq);
  408. pr_debug("%s: waiting for release of blocked read\n", __func__);
  409. mutex_lock(&nfc_dev->read_mutex);
  410. nfc_dev->release_read = false;
  411. } else {
  412. pr_debug("%s: read thread already released\n", __func__);
  413. }
  414. mutex_unlock(&nfc_dev->read_mutex);
  415. return 0;
  416. }
  417. int nfc_dev_close(struct inode *inode, struct file *filp)
  418. {
  419. struct nfc_dev *nfc_dev = NULL;
  420. nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
  421. if (!nfc_dev)
  422. return -ENODEV;
  423. pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  424. mutex_lock(&nfc_dev->dev_ref_mutex);
  425. if (nfc_dev->dev_ref_count == 1) {
  426. nfc_dev->nfc_disable_intr(nfc_dev);
  427. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  428. }
  429. if (nfc_dev->dev_ref_count > 0)
  430. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count - 1;
  431. filp->private_data = NULL;
  432. mutex_unlock(&nfc_dev->dev_ref_mutex);
  433. return 0;
  434. }
  435. int validate_nfc_state_nci(struct nfc_dev *nfc_dev)
  436. {
  437. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  438. if (!gpio_get_value(nfc_gpio->ven)) {
  439. pr_err("%s: ven low - nfcc powered off\n", __func__);
  440. return -ENODEV;
  441. }
  442. if (get_valid_gpio(nfc_gpio->dwl_req) == 1) {
  443. pr_err("%s: fw download in-progress\n", __func__);
  444. return -EBUSY;
  445. }
  446. if (nfc_dev->nfc_state != NFC_STATE_NCI) {
  447. pr_err("%s: fw download state\n", __func__);
  448. return -EBUSY;
  449. }
  450. return 0;
  451. }