i2c_drv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /******************************************************************************
  2. * Copyright (C) 2013-2021 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. static ssize_t i2c_read_internal(struct nfc_dev *dev, char *buf, size_t count, int timeout);
  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. nfc_dev_t *nfc_dev = dev_id;
  88. i2c_dev_t *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 *dev, char *buf, size_t count, int timeout){
  96. pr_debug("%s : reading %zu bytes.\n", __func__, count);
  97. if(timeout > NCI_MAX_CMD_RSP_TIMEOUT)
  98. timeout = NCI_MAX_CMD_RSP_TIMEOUT;
  99. return i2c_read_internal(dev, buf, count, timeout);
  100. }
  101. int i2c_write(struct nfc_dev *dev, const char *buf, size_t count,
  102. int max_retry_cnt)
  103. {
  104. int ret = -EINVAL;
  105. int retry_cnt;
  106. pr_debug("%s : writing %zu bytes.\n", __func__, count);
  107. for (retry_cnt = 1; retry_cnt <= max_retry_cnt; retry_cnt++) {
  108. ret = i2c_master_send(dev->i2c_dev.client, buf, count);
  109. if (ret <= 0) {
  110. pr_warn("%s: write failed, Maybe in Standby Mode - Retry(%d)\n", __func__,
  111. retry_cnt);
  112. usleep_range(WRITE_RETRY_WAIT_TIME_USEC,
  113. WRITE_RETRY_WAIT_TIME_USEC + 100);
  114. } else if (ret == count)
  115. break;
  116. }
  117. return ret;
  118. }
  119. static ssize_t i2c_read_internal(struct nfc_dev *dev, char *buf, size_t count, int timeout)
  120. {
  121. int ret;
  122. char tmp[MAX_BUFFER_SIZE];
  123. nfc_dev_t *nfc_dev = dev;
  124. i2c_dev_t *i2c_dev = &nfc_dev->i2c_dev;
  125. platform_gpio_t *nfc_gpio = &nfc_dev->configs.gpio;
  126. if (count > MAX_BUFFER_SIZE)
  127. count = MAX_BUFFER_SIZE;
  128. pr_debug("%s : reading %zu bytes.\n", __func__, count);
  129. mutex_lock(&nfc_dev->read_mutex);
  130. if (!gpio_get_value(nfc_gpio->irq)) {
  131. while (1) {
  132. ret = 0;
  133. if (!i2c_dev->irq_enabled) {
  134. i2c_dev->irq_enabled = true;
  135. enable_irq(i2c_dev->client->irq);
  136. }
  137. if (!gpio_get_value(nfc_gpio->irq)) {
  138. if(timeout) {
  139. ret = wait_event_interruptible_timeout(nfc_dev->read_wq,
  140. !i2c_dev-> irq_enabled, msecs_to_jiffies(timeout));
  141. if (ret <= 0) {
  142. pr_err("%s timeout/error in read\n", __func__);
  143. goto err;
  144. }
  145. } else {
  146. ret = wait_event_interruptible(nfc_dev->read_wq,
  147. !i2c_dev->irq_enabled);
  148. if (ret) {
  149. pr_err("%s error wakeup of read wq\n", __func__);
  150. goto err;
  151. }
  152. }
  153. }
  154. i2c_disable_irq(nfc_dev);
  155. if (gpio_get_value(nfc_gpio->irq))
  156. break;
  157. if (!gpio_get_value(nfc_gpio->ven)) {
  158. pr_info("%s: releasing read\n", __func__);
  159. ret = -EIO;
  160. goto err;
  161. }
  162. pr_warn("%s: spurious interrupt detected\n", __func__);
  163. }
  164. }
  165. memset(tmp, 0x00, count);
  166. /* Read data */
  167. ret = i2c_master_recv(nfc_dev->i2c_dev.client, tmp, count);
  168. if (ret <= 0) {
  169. pr_err("%s: i2c_read returned %d\n", __func__, ret);
  170. goto err;
  171. }
  172. /* check if it's response of cold reset command
  173. * NFC HAL process shouldn't receive this data as
  174. * command was sent by driver
  175. */
  176. if (nfc_dev->cold_reset.rsp_pending) {
  177. if (IS_PROP_CMD_RSP(tmp)) {
  178. /* Read data */
  179. ret = i2c_master_recv(nfc_dev->i2c_dev.client, &tmp[NCI_PAYLOAD_IDX],
  180. tmp[NCI_PAYLOAD_LEN_IDX]);
  181. if (ret <= 0) {
  182. pr_err("%s: failure to read prop cold reset/protection rsp header\n", __func__);
  183. goto err;
  184. }
  185. wakeup_on_prop_rsp(nfc_dev, tmp);
  186. mutex_unlock(&nfc_dev->read_mutex);
  187. /*
  188. * NFC process doesn't know about cold reset command
  189. * being sent as it was initiated by eSE process
  190. * we shouldn't return any data to NFC process
  191. */
  192. return 0;
  193. }
  194. }
  195. if (copy_to_user(buf, tmp, ret)) {
  196. pr_warn("%s : failed to copy to user space\n", __func__);
  197. ret = -EFAULT;
  198. }
  199. err:
  200. mutex_unlock(&nfc_dev->read_mutex);
  201. return ret;
  202. }
  203. ssize_t nfc_i2c_dev_read(struct file * filp, char __user *buf,
  204. size_t count, loff_t * offset)
  205. {
  206. pr_debug("%s : reading %zu bytes.\n", __func__, count);
  207. if (filp->f_flags & O_NONBLOCK) {
  208. pr_err(":f_falg has O_NONBLOCK. EAGAIN\n");
  209. return -EAGAIN;
  210. }
  211. return i2c_read_internal((nfc_dev_t *)filp->private_data, buf, count, 0);
  212. }
  213. ssize_t nfc_i2c_dev_write(struct file * filp, const char __user *buf,
  214. size_t count, loff_t * offset)
  215. {
  216. int ret;
  217. char tmp[MAX_DL_BUFFER_SIZE];
  218. nfc_dev_t *nfc_dev = filp->private_data;
  219. if (count > MAX_DL_BUFFER_SIZE)
  220. count = MAX_DL_BUFFER_SIZE;
  221. if (copy_from_user(tmp, buf, count)) {
  222. pr_err("%s : failed to copy from user space\n", __func__);
  223. return -EFAULT;
  224. }
  225. ret = i2c_write(nfc_dev, tmp, count, NO_RETRY);
  226. if (ret != count) {
  227. pr_err("%s: failed to write %d\n", __func__, ret);
  228. ret = -EIO;
  229. }
  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. nfc_dev_t *nfc_dev = NULL;
  245. i2c_dev_t *i2c_dev = NULL;
  246. platform_configs_t nfc_configs;
  247. platform_gpio_t *nfc_gpio = &nfc_configs.gpio;
  248. pr_debug("%s: enter\n", __func__);
  249. /*retrive 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(nfc_dev_t), GFP_KERNEL);
  261. if (nfc_dev == NULL) {
  262. ret = -ENOMEM;
  263. goto err;
  264. }
  265. nfc_dev->interface = PLATFORM_IF_I2C;
  266. nfc_dev->nfc_state = NFC_STATE_NCI;
  267. nfc_dev->i2c_dev.client = client;
  268. i2c_dev = &nfc_dev->i2c_dev;
  269. nfc_dev->nfc_read = i2c_read;
  270. nfc_dev->nfc_write = i2c_write;
  271. nfc_dev->nfc_enable_intr = i2c_enable_irq;
  272. nfc_dev->nfc_disable_intr = i2c_disable_irq;
  273. #if defined(RECOVERY_ENABLE)
  274. nfc_dev->recovery_required = false;
  275. nfc_dev->fw_major_version = 0;
  276. #endif
  277. ret = configure_gpio(nfc_gpio->ven, GPIO_OUTPUT);
  278. if (ret) {
  279. pr_err("%s: unable to request nfc reset gpio [%d]\n", __func__, nfc_gpio->ven);
  280. goto err;
  281. }
  282. ret = configure_gpio(nfc_gpio->irq, GPIO_IRQ);
  283. if (ret <= 0) {
  284. pr_err("%s: unable to request nfc irq gpio [%d]\n", __func__, nfc_gpio->irq);
  285. goto err;
  286. }
  287. client->irq = ret;
  288. ret = configure_gpio(nfc_gpio->dwl_req, GPIO_OUTPUT);
  289. if (ret) {
  290. pr_err("%s: unable to request nfc firm downl gpio [%d]\n", __func__,
  291. nfc_gpio->dwl_req);
  292. }
  293. /*copy the retrived gpio details from DT */
  294. memcpy(&nfc_dev->configs, &nfc_configs, sizeof(struct platform_configs));
  295. /* init mutex and queues */
  296. init_waitqueue_head(&nfc_dev->read_wq);
  297. mutex_init(&nfc_dev->read_mutex);
  298. mutex_init(&nfc_dev->dev_ref_mutex);
  299. mutex_init(&nfc_dev->ese_access_mutex);
  300. spin_lock_init(&i2c_dev->irq_enabled_lock);
  301. common_ese_init(nfc_dev);
  302. ret = nfc_misc_register(nfc_dev, &nfc_i2c_dev_fops, DEV_COUNT,
  303. NFC_CHAR_DEV_NAME, CLASS_NAME);
  304. if (ret) {
  305. pr_err("%s: nfc_misc_register failed\n", __func__);
  306. goto err_mutex_destroy;
  307. }
  308. /* interrupt initializations */
  309. pr_info("%s : requesting IRQ %d\n", __func__, client->irq);
  310. i2c_dev->irq_enabled = true;
  311. ret = request_irq(client->irq, i2c_irq_handler,
  312. IRQF_TRIGGER_HIGH, client->name, nfc_dev);
  313. if (ret) {
  314. pr_err("%s: request_irq failed\n", __func__);
  315. goto err_nfc_misc_unregister;
  316. }
  317. i2c_disable_irq(nfc_dev);
  318. ret = nfcc_hw_check(nfc_dev);
  319. if (ret || nfc_dev->nfc_state == NFC_STATE_UNKNOWN) {
  320. pr_err("nfc hw check failed ret %d\n", ret);
  321. }
  322. device_init_wakeup(&client->dev, true);
  323. device_set_wakeup_capable(&client->dev, true);
  324. i2c_set_clientdata(client, nfc_dev);
  325. i2c_dev->irq_wake_up = false;
  326. pr_info("%s probing nfc i2c successfully", __func__);
  327. return 0;
  328. err_nfc_misc_unregister:
  329. nfc_misc_unregister(nfc_dev, DEV_COUNT);
  330. err_mutex_destroy:
  331. mutex_destroy(&nfc_dev->dev_ref_mutex);
  332. mutex_destroy(&nfc_dev->read_mutex);
  333. mutex_destroy(&nfc_dev->ese_access_mutex);
  334. mutex_destroy(&nfc_dev->cold_reset.sync_mutex);
  335. err:
  336. if (nfc_dev) {
  337. gpio_free_all(nfc_dev);
  338. kfree(nfc_dev);
  339. }
  340. pr_err("%s: probing not successful, check hardware\n", __func__);
  341. return ret;
  342. }
  343. int nfc_i2c_dev_remove(struct i2c_client *client)
  344. {
  345. int ret = 0;
  346. nfc_dev_t *nfc_dev = NULL;
  347. pr_info("%s: remove device\n", __func__);
  348. nfc_dev = i2c_get_clientdata(client);
  349. if (!nfc_dev) {
  350. pr_err("%s: device doesn't exist anymore\n", __func__);
  351. ret = -ENODEV;
  352. return ret;
  353. }
  354. if (nfc_dev->dev_ref_count > 0) {
  355. pr_err("%s: device already in use\n", __func__);
  356. return -EBUSY;
  357. }
  358. device_init_wakeup(&client->dev, false);
  359. free_irq(client->irq, nfc_dev);
  360. nfc_misc_unregister(nfc_dev, DEV_COUNT);
  361. mutex_destroy(&nfc_dev->read_mutex);
  362. mutex_destroy(&nfc_dev->ese_access_mutex);
  363. mutex_destroy(&nfc_dev->cold_reset.sync_mutex);
  364. gpio_free_all(nfc_dev);
  365. kfree(nfc_dev);
  366. return ret;
  367. }
  368. int nfc_i2c_dev_suspend(struct device *device)
  369. {
  370. struct i2c_client *client = to_i2c_client(device);
  371. nfc_dev_t *nfc_dev = i2c_get_clientdata(client);
  372. i2c_dev_t *i2c_dev = &nfc_dev->i2c_dev;
  373. if (device_may_wakeup(&client->dev) && i2c_dev->irq_enabled) {
  374. if (!enable_irq_wake(client->irq))
  375. i2c_dev->irq_wake_up = true;
  376. }
  377. return 0;
  378. }
  379. int nfc_i2c_dev_resume(struct device *device)
  380. {
  381. struct i2c_client *client = to_i2c_client(device);
  382. nfc_dev_t *nfc_dev = i2c_get_clientdata(client);
  383. i2c_dev_t *i2c_dev = &nfc_dev->i2c_dev;
  384. if (device_may_wakeup(&client->dev) && i2c_dev->irq_wake_up) {
  385. if (!disable_irq_wake(client->irq))
  386. i2c_dev->irq_wake_up = false;
  387. }
  388. return 0;
  389. }
  390. static const struct i2c_device_id nfc_i2c_dev_id[] = {
  391. {NFC_I2C_DEV_ID, 0},
  392. {}
  393. };
  394. static const struct of_device_id nfc_i2c_dev_match_table[] = {
  395. {.compatible = NFC_I2C_DRV_STR,},
  396. {}
  397. };
  398. static const struct dev_pm_ops nfc_i2c_dev_pm_ops = {
  399. SET_SYSTEM_SLEEP_PM_OPS(nfc_i2c_dev_suspend, nfc_i2c_dev_resume)
  400. };
  401. static struct i2c_driver nfc_i2c_dev_driver = {
  402. .id_table = nfc_i2c_dev_id,
  403. .probe = nfc_i2c_dev_probe,
  404. .remove = nfc_i2c_dev_remove,
  405. .driver = {
  406. .name = NFC_I2C_DRV_STR,
  407. .pm = &nfc_i2c_dev_pm_ops,
  408. .of_match_table = nfc_i2c_dev_match_table,
  409. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  410. },
  411. };
  412. MODULE_DEVICE_TABLE(of, nfc_i2c_dev_match_table);
  413. static int __init nfc_i2c_dev_init(void)
  414. {
  415. int ret = 0;
  416. pr_info("Loading NXP NFC I2C driver\n");
  417. ret = i2c_add_driver(&nfc_i2c_dev_driver);
  418. if (ret != 0)
  419. pr_err("NFC I2C add driver error ret %d\n", ret);
  420. return ret;
  421. }
  422. module_init(nfc_i2c_dev_init);
  423. static void __exit nfc_i2c_dev_exit(void)
  424. {
  425. pr_info("Unloading NXP NFC I2C driver\n");
  426. i2c_del_driver(&nfc_i2c_dev_driver);
  427. }
  428. module_exit(nfc_i2c_dev_exit);
  429. MODULE_DESCRIPTION("NXP NFC I2C driver");
  430. MODULE_LICENSE("GPL");