i2c_drv.c 12 KB

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