common.c 14 KB

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