i2c_drv.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /******************************************************************************
  2. * Copyright (C) 2015, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2013-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) 2010 Trusted Logic S.A.
  22. *
  23. * This program is free software; you can redistribute it and/or modify
  24. * it under the terms of the GNU General Public License as published by
  25. * the Free Software Foundation; either version 2 of the License, or
  26. * (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software
  35. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  36. */
  37. #include <linux/module.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/delay.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/gpio.h>
  42. #include "common_ese.h"
  43. /**
  44. * i2c_disable_irq()
  45. *
  46. * Check if interrupt is disabled or not
  47. * and disable interrupt
  48. *
  49. * Return: int
  50. */
  51. int i2c_disable_irq(struct nfc_dev *dev)
  52. {
  53. unsigned long flags;
  54. spin_lock_irqsave(&dev->i2c_dev.irq_enabled_lock, flags);
  55. if (dev->i2c_dev.irq_enabled) {
  56. disable_irq_nosync(dev->i2c_dev.client->irq);
  57. dev->i2c_dev.irq_enabled = false;
  58. }
  59. spin_unlock_irqrestore(&dev->i2c_dev.irq_enabled_lock, flags);
  60. return 0;
  61. }
  62. /**
  63. * i2c_enable_irq()
  64. *
  65. * Check if interrupt is enabled or not
  66. * and enable interrupt
  67. *
  68. * Return: int
  69. */
  70. int i2c_enable_irq(struct nfc_dev *dev)
  71. {
  72. unsigned long flags;
  73. spin_lock_irqsave(&dev->i2c_dev.irq_enabled_lock, flags);
  74. if (!dev->i2c_dev.irq_enabled) {
  75. dev->i2c_dev.irq_enabled = true;
  76. enable_irq(dev->i2c_dev.client->irq);
  77. }
  78. spin_unlock_irqrestore(&dev->i2c_dev.irq_enabled_lock, flags);
  79. return 0;
  80. }
  81. static irqreturn_t i2c_irq_handler(int irq, void *dev_id)
  82. {
  83. struct nfc_dev *nfc_dev = dev_id;
  84. struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev;
  85. if (device_may_wakeup(&i2c_dev->client->dev))
  86. pm_wakeup_event(&i2c_dev->client->dev, WAKEUP_SRC_TIMEOUT);
  87. i2c_disable_irq(nfc_dev);
  88. wake_up(&nfc_dev->read_wq);
  89. return IRQ_HANDLED;
  90. }
  91. int i2c_read(struct nfc_dev *nfc_dev, char *buf, size_t count, int timeout)
  92. {
  93. int ret;
  94. struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev;
  95. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  96. pr_debug("%s: reading %zu bytes.\n", __func__, count);
  97. if (timeout > NCI_CMD_RSP_TIMEOUT_MS)
  98. timeout = NCI_CMD_RSP_TIMEOUT_MS;
  99. if (count > MAX_NCI_BUFFER_SIZE)
  100. count = MAX_NCI_BUFFER_SIZE;
  101. if (!gpio_get_value(nfc_gpio->irq)) {
  102. while (1) {
  103. ret = 0;
  104. if (!i2c_dev->irq_enabled) {
  105. i2c_dev->irq_enabled = true;
  106. enable_irq(i2c_dev->client->irq);
  107. }
  108. if (!gpio_get_value(nfc_gpio->irq)) {
  109. if (timeout) {
  110. ret = wait_event_interruptible_timeout(
  111. nfc_dev->read_wq,
  112. !i2c_dev->irq_enabled,
  113. msecs_to_jiffies(timeout));
  114. if (ret <= 0) {
  115. pr_err("%s: timeout error\n",
  116. __func__);
  117. goto err;
  118. }
  119. } else {
  120. ret = wait_event_interruptible(
  121. nfc_dev->read_wq,
  122. !i2c_dev->irq_enabled);
  123. if (ret) {
  124. pr_err("%s: err wakeup of wq\n",
  125. __func__);
  126. goto err;
  127. }
  128. }
  129. }
  130. i2c_disable_irq(nfc_dev);
  131. if (gpio_get_value(nfc_gpio->irq))
  132. break;
  133. if (!gpio_get_value(nfc_gpio->ven)) {
  134. pr_info("%s: releasing read\n", __func__);
  135. ret = -EIO;
  136. goto err;
  137. }
  138. /*
  139. * NFC service wanted to close the driver so,
  140. * release the calling reader thread asap.
  141. *
  142. * This can happen in case of nfc node close call from
  143. * eSE HAL in that case the NFC HAL reader thread
  144. * will again call read system call
  145. */
  146. if (nfc_dev->release_read) {
  147. pr_debug("%s: releasing read\n", __func__);
  148. return 0;
  149. }
  150. pr_warn("%s: spurious interrupt detected\n", __func__);
  151. }
  152. }
  153. memset(buf, 0x00, count);
  154. /* Read data */
  155. ret = i2c_master_recv(nfc_dev->i2c_dev.client, buf, count);
  156. if (ret <= 0) {
  157. pr_err("%s: returned %d\n", __func__, ret);
  158. goto err;
  159. }
  160. /* check if it's response of cold reset command
  161. * NFC HAL process shouldn't receive this data as
  162. * command was sent by driver
  163. */
  164. if (nfc_dev->cold_reset.rsp_pending) {
  165. if (IS_PROP_CMD_RSP(buf)) {
  166. /* Read data */
  167. ret = i2c_master_recv(nfc_dev->i2c_dev.client,
  168. &buf[NCI_PAYLOAD_IDX],
  169. buf[NCI_PAYLOAD_LEN_IDX]);
  170. if (ret <= 0) {
  171. pr_err("%s: error reading cold rst/prot rsp\n",
  172. __func__);
  173. goto err;
  174. }
  175. wakeup_on_prop_rsp(nfc_dev, buf);
  176. /*
  177. * NFC process doesn't know about cold reset command
  178. * being sent as it was initiated by eSE process
  179. * we shouldn't return any data to NFC process
  180. */
  181. return 0;
  182. }
  183. }
  184. err:
  185. return ret;
  186. }
  187. int i2c_write(struct nfc_dev *nfc_dev, const char *buf, size_t count,
  188. int max_retry_cnt)
  189. {
  190. int ret = -EINVAL;
  191. int retry_cnt;
  192. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  193. if (count > MAX_DL_BUFFER_SIZE)
  194. count = MAX_DL_BUFFER_SIZE;
  195. pr_debug("%s: writing %zu bytes.\n", __func__, count);
  196. /*
  197. * Wait for any pending read for max 15ms before write
  198. * This is to avoid any packet corruption during read, when
  199. * the host cmds resets NFCC during any parallel read operation
  200. */
  201. for (retry_cnt = 1; retry_cnt <= MAX_WRITE_IRQ_COUNT; retry_cnt++) {
  202. if (gpio_get_value(nfc_gpio->irq)) {
  203. pr_warn("%s: irq high during write, wait\n", __func__);
  204. usleep_range(NFC_WRITE_IRQ_WAIT_TIME_US,
  205. NFC_WRITE_IRQ_WAIT_TIME_US + 100);
  206. } else {
  207. break;
  208. }
  209. if (retry_cnt == MAX_WRITE_IRQ_COUNT &&
  210. gpio_get_value(nfc_gpio->irq)) {
  211. pr_warn("%s: allow after maximum wait\n", __func__);
  212. }
  213. }
  214. for (retry_cnt = 1; retry_cnt <= max_retry_cnt; retry_cnt++) {
  215. ret = i2c_master_send(nfc_dev->i2c_dev.client, buf, count);
  216. if (ret <= 0) {
  217. pr_warn("%s: write failed ret(%d), maybe in standby\n",
  218. __func__, ret);
  219. usleep_range(WRITE_RETRY_WAIT_TIME_US,
  220. WRITE_RETRY_WAIT_TIME_US + 100);
  221. } else if (ret != count) {
  222. pr_err("%s: failed to write %d\n", __func__, ret);
  223. ret = -EIO;
  224. } else if (ret == count)
  225. break;
  226. }
  227. return ret;
  228. }
  229. ssize_t nfc_i2c_dev_read(struct file *filp, char __user *buf, size_t count,
  230. loff_t *offset)
  231. {
  232. int ret;
  233. struct nfc_dev *nfc_dev = (struct nfc_dev *)filp->private_data;
  234. if (filp->f_flags & O_NONBLOCK) {
  235. pr_err("%s: f_flags has nonblock. try again\n", __func__);
  236. return -EAGAIN;
  237. }
  238. mutex_lock(&nfc_dev->read_mutex);
  239. ret = i2c_read(nfc_dev, nfc_dev->read_kbuf, count, 0);
  240. if (ret > 0) {
  241. if (copy_to_user(buf, nfc_dev->read_kbuf, ret)) {
  242. pr_warn("%s: failed to copy to user space\n", __func__);
  243. ret = -EFAULT;
  244. }
  245. }
  246. mutex_unlock(&nfc_dev->read_mutex);
  247. return ret;
  248. }
  249. ssize_t nfc_i2c_dev_write(struct file *filp, const char __user *buf,
  250. size_t count, loff_t *offset)
  251. {
  252. int ret;
  253. struct nfc_dev *nfc_dev = (struct nfc_dev *)filp->private_data;
  254. if (count > MAX_DL_BUFFER_SIZE)
  255. count = MAX_DL_BUFFER_SIZE;
  256. mutex_lock(&nfc_dev->write_mutex);
  257. if (copy_from_user(nfc_dev->write_kbuf, buf, count)) {
  258. pr_err("%s: failed to copy from user space\n", __func__);
  259. mutex_unlock(&nfc_dev->write_mutex);
  260. return -EFAULT;
  261. }
  262. ret = i2c_write(nfc_dev, nfc_dev->write_kbuf, count, NO_RETRY);
  263. mutex_unlock(&nfc_dev->write_mutex);
  264. return ret;
  265. }
  266. static const struct file_operations nfc_i2c_dev_fops = {
  267. .owner = THIS_MODULE,
  268. .llseek = no_llseek,
  269. .read = nfc_i2c_dev_read,
  270. .write = nfc_i2c_dev_write,
  271. .open = nfc_dev_open,
  272. .flush = nfc_dev_flush,
  273. .release = nfc_dev_close,
  274. .unlocked_ioctl = nfc_dev_ioctl,
  275. };
  276. int nfc_i2c_dev_probe(struct i2c_client *client, const struct i2c_device_id *id)
  277. {
  278. int ret = 0;
  279. struct nfc_dev *nfc_dev = NULL;
  280. struct i2c_dev *i2c_dev = NULL;
  281. struct platform_configs nfc_configs;
  282. struct platform_gpio *nfc_gpio = &nfc_configs.gpio;
  283. pr_debug("%s: enter\n", __func__);
  284. /* retrieve details of gpios from dt */
  285. ret = nfc_parse_dt(&client->dev, &nfc_configs, PLATFORM_IF_I2C);
  286. if (ret) {
  287. pr_err("%s: failed to parse dt\n", __func__);
  288. goto err;
  289. }
  290. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  291. pr_err("%s: need I2C_FUNC_I2C\n", __func__);
  292. ret = -ENODEV;
  293. goto err;
  294. }
  295. nfc_dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
  296. if (nfc_dev == NULL) {
  297. ret = -ENOMEM;
  298. goto err;
  299. }
  300. nfc_dev->read_kbuf = kzalloc(MAX_NCI_BUFFER_SIZE, GFP_DMA | GFP_KERNEL);
  301. if (!nfc_dev->read_kbuf) {
  302. ret = -ENOMEM;
  303. goto err_free_nfc_dev;
  304. }
  305. nfc_dev->write_kbuf = kzalloc(MAX_DL_BUFFER_SIZE, GFP_DMA | GFP_KERNEL);
  306. if (!nfc_dev->write_kbuf) {
  307. ret = -ENOMEM;
  308. goto err_free_read_kbuf;
  309. }
  310. nfc_dev->interface = PLATFORM_IF_I2C;
  311. nfc_dev->nfc_state = NFC_STATE_NCI;
  312. nfc_dev->i2c_dev.client = client;
  313. i2c_dev = &nfc_dev->i2c_dev;
  314. nfc_dev->nfc_read = i2c_read;
  315. nfc_dev->nfc_write = i2c_write;
  316. nfc_dev->nfc_enable_intr = i2c_enable_irq;
  317. nfc_dev->nfc_disable_intr = i2c_disable_irq;
  318. ret = configure_gpio(nfc_gpio->ven, GPIO_OUTPUT);
  319. if (ret) {
  320. pr_err("%s: unable to request nfc reset gpio [%d]\n", __func__,
  321. nfc_gpio->ven);
  322. goto err_free_write_kbuf;
  323. }
  324. ret = configure_gpio(nfc_gpio->irq, GPIO_IRQ);
  325. if (ret <= 0) {
  326. pr_err("%s: unable to request nfc irq gpio [%d]\n", __func__,
  327. nfc_gpio->irq);
  328. goto err_free_gpio;
  329. }
  330. client->irq = ret;
  331. ret = configure_gpio(nfc_gpio->dwl_req, GPIO_OUTPUT);
  332. if (ret) {
  333. pr_err("%s: unable to request nfc firm downl gpio [%d]\n",
  334. __func__, nfc_gpio->dwl_req);
  335. }
  336. /* copy the retrieved gpio details from DT */
  337. memcpy(&nfc_dev->configs, &nfc_configs,
  338. sizeof(struct platform_configs));
  339. /* init mutex and queues */
  340. init_waitqueue_head(&nfc_dev->read_wq);
  341. mutex_init(&nfc_dev->read_mutex);
  342. mutex_init(&nfc_dev->write_mutex);
  343. mutex_init(&nfc_dev->dev_ref_mutex);
  344. spin_lock_init(&i2c_dev->irq_enabled_lock);
  345. common_ese_init(nfc_dev);
  346. ret = nfc_misc_register(nfc_dev, &nfc_i2c_dev_fops, DEV_COUNT,
  347. NFC_CHAR_DEV_NAME, CLASS_NAME);
  348. if (ret) {
  349. pr_err("%s: nfc_misc_register failed\n", __func__);
  350. goto err_mutex_destroy;
  351. }
  352. /* interrupt initializations */
  353. pr_info("%s: requesting IRQ %d\n", __func__, client->irq);
  354. i2c_dev->irq_enabled = true;
  355. ret = request_irq(client->irq, i2c_irq_handler, IRQF_TRIGGER_HIGH,
  356. client->name, nfc_dev);
  357. if (ret) {
  358. pr_err("%s: request_irq failed\n", __func__);
  359. goto err_nfc_misc_unregister;
  360. }
  361. i2c_disable_irq(nfc_dev);
  362. gpio_set_ven(nfc_dev, 1);
  363. gpio_set_ven(nfc_dev, 0);
  364. gpio_set_ven(nfc_dev, 1);
  365. device_init_wakeup(&client->dev, true);
  366. i2c_set_clientdata(client, nfc_dev);
  367. i2c_dev->irq_wake_up = false;
  368. pr_info("%s: probing nfc i2c successfully\n", __func__);
  369. return 0;
  370. err_nfc_misc_unregister:
  371. nfc_misc_unregister(nfc_dev, DEV_COUNT);
  372. err_mutex_destroy:
  373. mutex_destroy(&nfc_dev->dev_ref_mutex);
  374. mutex_destroy(&nfc_dev->read_mutex);
  375. mutex_destroy(&nfc_dev->write_mutex);
  376. err_free_gpio:
  377. gpio_free_all(nfc_dev);
  378. err_free_write_kbuf:
  379. kfree(nfc_dev->write_kbuf);
  380. err_free_read_kbuf:
  381. kfree(nfc_dev->read_kbuf);
  382. err_free_nfc_dev:
  383. kfree(nfc_dev);
  384. err:
  385. pr_err("%s: probing not successful, check hardware\n", __func__);
  386. return ret;
  387. }
  388. int nfc_i2c_dev_remove(struct i2c_client *client)
  389. {
  390. int ret = 0;
  391. struct nfc_dev *nfc_dev = NULL;
  392. pr_info("%s: remove device\n", __func__);
  393. nfc_dev = i2c_get_clientdata(client);
  394. if (!nfc_dev) {
  395. pr_err("%s: device doesn't exist anymore\n", __func__);
  396. ret = -ENODEV;
  397. return ret;
  398. }
  399. if (nfc_dev->dev_ref_count > 0) {
  400. pr_err("%s: device already in use\n", __func__);
  401. return -EBUSY;
  402. }
  403. device_init_wakeup(&client->dev, false);
  404. free_irq(client->irq, nfc_dev);
  405. nfc_misc_unregister(nfc_dev, DEV_COUNT);
  406. mutex_destroy(&nfc_dev->read_mutex);
  407. mutex_destroy(&nfc_dev->write_mutex);
  408. gpio_free_all(nfc_dev);
  409. kfree(nfc_dev->read_kbuf);
  410. kfree(nfc_dev->write_kbuf);
  411. kfree(nfc_dev);
  412. return ret;
  413. }
  414. int nfc_i2c_dev_suspend(struct device *device)
  415. {
  416. struct i2c_client *client = to_i2c_client(device);
  417. struct nfc_dev *nfc_dev = i2c_get_clientdata(client);
  418. struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev;
  419. if (device_may_wakeup(&client->dev) && i2c_dev->irq_enabled) {
  420. if (!enable_irq_wake(client->irq))
  421. i2c_dev->irq_wake_up = true;
  422. }
  423. return 0;
  424. }
  425. int nfc_i2c_dev_resume(struct device *device)
  426. {
  427. struct i2c_client *client = to_i2c_client(device);
  428. struct nfc_dev *nfc_dev = i2c_get_clientdata(client);
  429. struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev;
  430. if (device_may_wakeup(&client->dev) && i2c_dev->irq_wake_up) {
  431. if (!disable_irq_wake(client->irq))
  432. i2c_dev->irq_wake_up = false;
  433. }
  434. return 0;
  435. }
  436. static const struct i2c_device_id nfc_i2c_dev_id[] = { { NFC_I2C_DEV_ID, 0 },
  437. {} };
  438. static const struct of_device_id nfc_i2c_dev_match_table[] = {
  439. {
  440. .compatible = NFC_I2C_DRV_STR,
  441. },
  442. {}
  443. };
  444. static const struct dev_pm_ops nfc_i2c_dev_pm_ops = { SET_SYSTEM_SLEEP_PM_OPS(
  445. nfc_i2c_dev_suspend, nfc_i2c_dev_resume) };
  446. static struct i2c_driver nfc_i2c_dev_driver = {
  447. .id_table = nfc_i2c_dev_id,
  448. .probe = nfc_i2c_dev_probe,
  449. .remove = nfc_i2c_dev_remove,
  450. .driver = {
  451. .name = NFC_I2C_DRV_STR,
  452. .pm = &nfc_i2c_dev_pm_ops,
  453. .of_match_table = nfc_i2c_dev_match_table,
  454. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  455. },
  456. };
  457. MODULE_DEVICE_TABLE(of, nfc_i2c_dev_match_table);
  458. static int __init nfc_i2c_dev_init(void)
  459. {
  460. int ret = 0;
  461. pr_info("%s: Loading NXP NFC I2C driver\n", __func__);
  462. ret = i2c_add_driver(&nfc_i2c_dev_driver);
  463. if (ret != 0)
  464. pr_err("%s: NFC I2C add driver error ret %d\n", __func__, ret);
  465. return ret;
  466. }
  467. module_init(nfc_i2c_dev_init);
  468. static void __exit nfc_i2c_dev_exit(void)
  469. {
  470. pr_info("%s: Unloading NXP NFC I2C driver\n", __func__);
  471. i2c_del_driver(&nfc_i2c_dev_driver);
  472. }
  473. module_exit(nfc_i2c_dev_exit);
  474. MODULE_DESCRIPTION("NXP NFC I2C driver");
  475. MODULE_LICENSE("GPL");