i2c_drv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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/fs.h>
  38. #include <linux/kernel.h>
  39. #include <linux/module.h>
  40. #include <linux/of_device.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/delay.h>
  43. #include <linux/uaccess.h>
  44. #include <linux/slab.h>
  45. #include "common.h"
  46. #include "common_ese.h"
  47. /**
  48. * i2c_disable_irq()
  49. *
  50. * Check if interrupt is disabled or not
  51. * and disable interrupt
  52. *
  53. * Return: int
  54. */
  55. int i2c_disable_irq(struct nfc_dev *dev)
  56. {
  57. unsigned long flags;
  58. spin_lock_irqsave(&dev->i2c_dev.irq_enabled_lock, flags);
  59. if (dev->i2c_dev.irq_enabled) {
  60. disable_irq_nosync(dev->i2c_dev.client->irq);
  61. dev->i2c_dev.irq_enabled = false;
  62. }
  63. spin_unlock_irqrestore(&dev->i2c_dev.irq_enabled_lock, flags);
  64. return 0;
  65. }
  66. /**
  67. * i2c_enable_irq()
  68. *
  69. * Check if interrupt is enabled or not
  70. * and enable interrupt
  71. *
  72. * Return: int
  73. */
  74. int i2c_enable_irq(struct nfc_dev *dev)
  75. {
  76. unsigned long flags;
  77. spin_lock_irqsave(&dev->i2c_dev.irq_enabled_lock, flags);
  78. if (!dev->i2c_dev.irq_enabled) {
  79. dev->i2c_dev.irq_enabled = true;
  80. enable_irq(dev->i2c_dev.client->irq);
  81. }
  82. spin_unlock_irqrestore(&dev->i2c_dev.irq_enabled_lock, flags);
  83. return 0;
  84. }
  85. static irqreturn_t i2c_irq_handler(int irq, void *dev_id)
  86. {
  87. struct nfc_dev *nfc_dev = dev_id;
  88. struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev;
  89. if (device_may_wakeup(&i2c_dev->client->dev))
  90. pm_wakeup_event(&i2c_dev->client->dev, WAKEUP_SRC_TIMEOUT);
  91. i2c_disable_irq(nfc_dev);
  92. wake_up(&nfc_dev->read_wq);
  93. return IRQ_HANDLED;
  94. }
  95. int i2c_read(struct nfc_dev *nfc_dev, char *buf, size_t count, int timeout)
  96. {
  97. int ret;
  98. struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev;
  99. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  100. pr_debug("%s : reading %zu bytes.\n", __func__, count);
  101. if (timeout > NCI_CMD_RSP_TIMEOUT)
  102. timeout = NCI_CMD_RSP_TIMEOUT;
  103. if (count > MAX_BUFFER_SIZE)
  104. count = MAX_BUFFER_SIZE;
  105. if (!gpio_get_value(nfc_gpio->irq)) {
  106. while (1) {
  107. ret = 0;
  108. if (!i2c_dev->irq_enabled) {
  109. i2c_dev->irq_enabled = true;
  110. enable_irq(i2c_dev->client->irq);
  111. }
  112. if (!gpio_get_value(nfc_gpio->irq)) {
  113. if (timeout) {
  114. ret = wait_event_interruptible_timeout(nfc_dev->read_wq,
  115. !i2c_dev->irq_enabled, msecs_to_jiffies(timeout));
  116. if (ret <= 0) {
  117. pr_err("%s timeout/error in read\n", __func__);
  118. goto err;
  119. }
  120. } else {
  121. ret = wait_event_interruptible(nfc_dev->read_wq,
  122. !i2c_dev->irq_enabled);
  123. if (ret) {
  124. pr_err("%s error wakeup of read wq\n", __func__);
  125. goto err;
  126. }
  127. }
  128. }
  129. i2c_disable_irq(nfc_dev);
  130. if (gpio_get_value(nfc_gpio->irq))
  131. break;
  132. if (!gpio_get_value(nfc_gpio->ven)) {
  133. pr_info("%s: releasing read\n", __func__);
  134. ret = -EIO;
  135. goto err;
  136. }
  137. pr_warn("%s: spurious interrupt detected\n", __func__);
  138. }
  139. }
  140. memset(buf, 0x00, count);
  141. /* Read data */
  142. ret = i2c_master_recv(nfc_dev->i2c_dev.client, buf, count);
  143. if (ret <= 0) {
  144. pr_err("%s: returned %d\n", __func__, ret);
  145. goto err;
  146. }
  147. /* check if it's response of cold reset command
  148. * NFC HAL process shouldn't receive this data as
  149. * command was sent by driver
  150. */
  151. if (nfc_dev->cold_reset.rsp_pending) {
  152. if (IS_PROP_CMD_RSP(buf)) {
  153. /* Read data */
  154. ret = i2c_master_recv(nfc_dev->i2c_dev.client, &buf[NCI_PAYLOAD_IDX],
  155. buf[NCI_PAYLOAD_LEN_IDX]);
  156. if (ret <= 0) {
  157. pr_err("%s: error reading cold reset/prot rsp header\n", __func__);
  158. goto err;
  159. }
  160. wakeup_on_prop_rsp(nfc_dev, buf);
  161. /*
  162. * NFC process doesn't know about cold reset command
  163. * being sent as it was initiated by eSE process
  164. * we shouldn't return any data to NFC process
  165. */
  166. return 0;
  167. }
  168. }
  169. err:
  170. return ret;
  171. }
  172. int i2c_write(struct nfc_dev *nfc_dev, const char *buf, size_t count,
  173. int max_retry_cnt)
  174. {
  175. int ret = -EINVAL;
  176. int retry_cnt;
  177. if (count > MAX_DL_BUFFER_SIZE)
  178. count = MAX_DL_BUFFER_SIZE;
  179. pr_debug("%s : writing %zu bytes.\n", __func__, count);
  180. for (retry_cnt = 1; retry_cnt <= max_retry_cnt; retry_cnt++) {
  181. ret = i2c_master_send(nfc_dev->i2c_dev.client, buf, count);
  182. if (ret <= 0) {
  183. pr_warn("%s: failed to write ret(%d), Maybe in Standby Mode - Retry(%d)\n", __func__,
  184. ret, retry_cnt);
  185. usleep_range(WRITE_RETRY_WAIT_TIME_USEC,
  186. WRITE_RETRY_WAIT_TIME_USEC + 100);
  187. } else if (ret != count) {
  188. pr_err("%s: failed to write %d\n", __func__, ret);
  189. ret = -EIO;
  190. } else if (ret == count)
  191. break;
  192. }
  193. return ret;
  194. }
  195. ssize_t nfc_i2c_dev_read(struct file *filp, char __user *buf,
  196. size_t count, loff_t *offset)
  197. {
  198. int ret = 0;
  199. struct nfc_dev *nfc_dev = (struct nfc_dev *)filp->private_data;
  200. if (filp->f_flags & O_NONBLOCK) {
  201. pr_err(":f_falg has O_NONBLOCK. EAGAIN\n");
  202. return -EAGAIN;
  203. }
  204. mutex_lock(&nfc_dev->read_mutex);
  205. ret = i2c_read(nfc_dev, nfc_dev->read_kbuf, count, 0);
  206. if (ret > 0) {
  207. if (copy_to_user(buf, nfc_dev->read_kbuf, ret)) {
  208. pr_warn("%s : failed to copy to user space\n", __func__);
  209. ret = -EFAULT;
  210. }
  211. }
  212. mutex_unlock(&nfc_dev->read_mutex);
  213. return ret;
  214. }
  215. ssize_t nfc_i2c_dev_write(struct file *filp, const char __user *buf,
  216. size_t count, loff_t *offset)
  217. {
  218. int ret = 0;
  219. struct nfc_dev *nfc_dev = (struct nfc_dev *)filp->private_data;
  220. if (count > MAX_DL_BUFFER_SIZE)
  221. count = MAX_DL_BUFFER_SIZE;
  222. mutex_lock(&nfc_dev->write_mutex);
  223. if (copy_from_user(nfc_dev->write_kbuf, buf, count)) {
  224. pr_err("%s : failed to copy from user space\n", __func__);
  225. mutex_unlock(&nfc_dev->write_mutex);
  226. return -EFAULT;
  227. }
  228. ret = i2c_write(nfc_dev, nfc_dev->write_kbuf, count, NO_RETRY);
  229. mutex_unlock(&nfc_dev->write_mutex);
  230. return ret;
  231. }
  232. static const struct file_operations nfc_i2c_dev_fops = {
  233. .owner = THIS_MODULE,
  234. .llseek = no_llseek,
  235. .read = nfc_i2c_dev_read,
  236. .write = nfc_i2c_dev_write,
  237. .open = nfc_dev_open,
  238. .release = nfc_dev_close,
  239. .unlocked_ioctl = nfc_dev_ioctl,
  240. };
  241. int nfc_i2c_dev_probe(struct i2c_client *client, const struct i2c_device_id *id)
  242. {
  243. int ret = 0;
  244. struct nfc_dev *nfc_dev = NULL;
  245. struct i2c_dev *i2c_dev = NULL;
  246. struct platform_configs nfc_configs;
  247. struct platform_gpio *nfc_gpio = &nfc_configs.gpio;
  248. pr_debug("%s: enter\n", __func__);
  249. /*retrieve details of gpios from dt */
  250. ret = nfc_parse_dt(&client->dev, &nfc_configs, PLATFORM_IF_I2C);
  251. if (ret) {
  252. pr_err("%s : failed to parse dt\n", __func__);
  253. goto err;
  254. }
  255. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  256. pr_err("%s : need I2C_FUNC_I2C\n", __func__);
  257. ret = -ENODEV;
  258. goto err;
  259. }
  260. nfc_dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
  261. if (nfc_dev == NULL) {
  262. ret = -ENOMEM;
  263. goto err;
  264. }
  265. nfc_dev->read_kbuf = kzalloc(MAX_BUFFER_SIZE, GFP_DMA | GFP_KERNEL);
  266. if (!nfc_dev->read_kbuf) {
  267. ret = -ENOMEM;
  268. goto err;
  269. }
  270. nfc_dev->write_kbuf = kzalloc(MAX_DL_BUFFER_SIZE, GFP_DMA | GFP_KERNEL);
  271. if (!nfc_dev->write_kbuf) {
  272. ret = -ENOMEM;
  273. goto err;
  274. }
  275. nfc_dev->interface = PLATFORM_IF_I2C;
  276. nfc_dev->nfc_state = NFC_STATE_NCI;
  277. nfc_dev->i2c_dev.client = client;
  278. i2c_dev = &nfc_dev->i2c_dev;
  279. nfc_dev->nfc_read = i2c_read;
  280. nfc_dev->nfc_write = i2c_write;
  281. nfc_dev->nfc_enable_intr = i2c_enable_irq;
  282. nfc_dev->nfc_disable_intr = i2c_disable_irq;
  283. nfc_dev->fw_major_version = 0;
  284. ret = configure_gpio(nfc_gpio->ven, GPIO_OUTPUT);
  285. if (ret) {
  286. pr_err("%s: unable to request nfc reset gpio [%d]\n", __func__, nfc_gpio->ven);
  287. goto err;
  288. }
  289. ret = configure_gpio(nfc_gpio->irq, GPIO_IRQ);
  290. if (ret <= 0) {
  291. pr_err("%s: unable to request nfc irq gpio [%d]\n", __func__, nfc_gpio->irq);
  292. goto err;
  293. }
  294. client->irq = ret;
  295. ret = configure_gpio(nfc_gpio->dwl_req, GPIO_OUTPUT);
  296. if (ret) {
  297. pr_err("%s: unable to request nfc firm downl gpio [%d]\n", __func__,
  298. nfc_gpio->dwl_req);
  299. }
  300. /*copy the retrieved gpio details from DT */
  301. memcpy(&nfc_dev->configs, &nfc_configs, sizeof(struct platform_configs));
  302. /* init mutex and queues */
  303. init_waitqueue_head(&nfc_dev->read_wq);
  304. mutex_init(&nfc_dev->read_mutex);
  305. mutex_init(&nfc_dev->write_mutex);
  306. mutex_init(&nfc_dev->dev_ref_mutex);
  307. spin_lock_init(&i2c_dev->irq_enabled_lock);
  308. common_ese_init(nfc_dev);
  309. ret = nfc_misc_register(nfc_dev, &nfc_i2c_dev_fops, DEV_COUNT,
  310. NFC_CHAR_DEV_NAME, CLASS_NAME);
  311. if (ret) {
  312. pr_err("%s: nfc_misc_register failed\n", __func__);
  313. goto err_mutex_destroy;
  314. }
  315. /* interrupt initializations */
  316. pr_info("%s : requesting IRQ %d\n", __func__, client->irq);
  317. i2c_dev->irq_enabled = true;
  318. ret = request_irq(client->irq, i2c_irq_handler,
  319. IRQF_TRIGGER_HIGH, client->name, nfc_dev);
  320. if (ret) {
  321. pr_err("%s: request_irq failed\n", __func__);
  322. goto err_nfc_misc_unregister;
  323. }
  324. i2c_disable_irq(nfc_dev);
  325. ret = nfcc_hw_check(nfc_dev);
  326. if (ret != 0) {
  327. pr_err("nfc hw check failed ret %d\n", ret);
  328. goto err_nfc_misc_unregister;
  329. }
  330. device_init_wakeup(&client->dev, true);
  331. device_set_wakeup_capable(&client->dev, true);
  332. i2c_set_clientdata(client, nfc_dev);
  333. i2c_dev->irq_wake_up = false;
  334. pr_info("%s probing nfc i2c successfully", __func__);
  335. return 0;
  336. err_nfc_misc_unregister:
  337. nfc_misc_unregister(nfc_dev, DEV_COUNT);
  338. err_mutex_destroy:
  339. mutex_destroy(&nfc_dev->dev_ref_mutex);
  340. mutex_destroy(&nfc_dev->read_mutex);
  341. mutex_destroy(&nfc_dev->write_mutex);
  342. err:
  343. gpio_free_all(nfc_dev);
  344. kfree(nfc_dev->read_kbuf);
  345. kfree(nfc_dev->write_kbuf);
  346. kfree(nfc_dev);
  347. pr_err("%s: probing not successful, check hardware\n", __func__);
  348. return ret;
  349. }
  350. int nfc_i2c_dev_remove(struct i2c_client *client)
  351. {
  352. int ret = 0;
  353. struct nfc_dev *nfc_dev = NULL;
  354. pr_info("%s: remove device\n", __func__);
  355. nfc_dev = i2c_get_clientdata(client);
  356. if (!nfc_dev) {
  357. pr_err("%s: device doesn't exist anymore\n", __func__);
  358. ret = -ENODEV;
  359. return ret;
  360. }
  361. if (nfc_dev->dev_ref_count > 0) {
  362. pr_err("%s: device already in use\n", __func__);
  363. return -EBUSY;
  364. }
  365. device_init_wakeup(&client->dev, false);
  366. free_irq(client->irq, nfc_dev);
  367. nfc_misc_unregister(nfc_dev, DEV_COUNT);
  368. mutex_destroy(&nfc_dev->read_mutex);
  369. mutex_destroy(&nfc_dev->write_mutex);
  370. gpio_free_all(nfc_dev);
  371. kfree(nfc_dev->read_kbuf);
  372. kfree(nfc_dev->write_kbuf);
  373. kfree(nfc_dev);
  374. return ret;
  375. }
  376. int nfc_i2c_dev_suspend(struct device *device)
  377. {
  378. struct i2c_client *client = to_i2c_client(device);
  379. struct nfc_dev *nfc_dev = i2c_get_clientdata(client);
  380. struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev;
  381. if (device_may_wakeup(&client->dev) && i2c_dev->irq_enabled) {
  382. if (!enable_irq_wake(client->irq))
  383. i2c_dev->irq_wake_up = true;
  384. }
  385. return 0;
  386. }
  387. int nfc_i2c_dev_resume(struct device *device)
  388. {
  389. struct i2c_client *client = to_i2c_client(device);
  390. struct nfc_dev *nfc_dev = i2c_get_clientdata(client);
  391. struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev;
  392. if (device_may_wakeup(&client->dev) && i2c_dev->irq_wake_up) {
  393. if (!disable_irq_wake(client->irq))
  394. i2c_dev->irq_wake_up = false;
  395. }
  396. return 0;
  397. }
  398. static const struct i2c_device_id nfc_i2c_dev_id[] = {
  399. {NFC_I2C_DEV_ID, 0},
  400. {}
  401. };
  402. static const struct of_device_id nfc_i2c_dev_match_table[] = {
  403. {.compatible = NFC_I2C_DRV_STR,},
  404. {}
  405. };
  406. static const struct dev_pm_ops nfc_i2c_dev_pm_ops = {
  407. SET_SYSTEM_SLEEP_PM_OPS(nfc_i2c_dev_suspend, nfc_i2c_dev_resume)
  408. };
  409. static struct i2c_driver nfc_i2c_dev_driver = {
  410. .id_table = nfc_i2c_dev_id,
  411. .probe = nfc_i2c_dev_probe,
  412. .remove = nfc_i2c_dev_remove,
  413. .driver = {
  414. .name = NFC_I2C_DRV_STR,
  415. .pm = &nfc_i2c_dev_pm_ops,
  416. .of_match_table = nfc_i2c_dev_match_table,
  417. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  418. },
  419. };
  420. MODULE_DEVICE_TABLE(of, nfc_i2c_dev_match_table);
  421. static int __init nfc_i2c_dev_init(void)
  422. {
  423. int ret = 0;
  424. pr_info("Loading NXP NFC I2C driver\n");
  425. ret = i2c_add_driver(&nfc_i2c_dev_driver);
  426. if (ret != 0)
  427. pr_err("NFC I2C add driver error ret %d\n", ret);
  428. return ret;
  429. }
  430. module_init(nfc_i2c_dev_init);
  431. static void __exit nfc_i2c_dev_exit(void)
  432. {
  433. pr_info("Unloading NXP NFC I2C driver\n");
  434. i2c_del_driver(&nfc_i2c_dev_driver);
  435. }
  436. module_exit(nfc_i2c_dev_exit);
  437. MODULE_DESCRIPTION("NXP NFC I2C driver");
  438. MODULE_LICENSE("GPL");