i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * I2C link layer for the NXP NCI driver
  3. *
  4. * Copyright (C) 2014 NXP Semiconductors All rights reserved.
  5. * Copyright (C) 2012-2015 Intel Corporation. All rights reserved.
  6. *
  7. * Authors: Clément Perrochaud <clement.perrochaud@nxp.com>
  8. * Authors: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
  9. *
  10. * Derived from PN544 device driver:
  11. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms and conditions of the GNU General Public License,
  15. * version 2, as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/acpi.h>
  27. #include <linux/delay.h>
  28. #include <linux/i2c.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/miscdevice.h>
  31. #include <linux/module.h>
  32. #include <linux/nfc.h>
  33. #include <linux/gpio/consumer.h>
  34. #include <linux/of_gpio.h>
  35. #include <linux/of_irq.h>
  36. #include <linux/platform_data/nxp-nci.h>
  37. #include <linux/unaligned/access_ok.h>
  38. #include <net/nfc/nfc.h>
  39. #include "nxp-nci.h"
  40. #define NXP_NCI_I2C_DRIVER_NAME "nxp-nci_i2c"
  41. #define NXP_NCI_I2C_MAX_PAYLOAD 32
  42. struct nxp_nci_i2c_phy {
  43. struct i2c_client *i2c_dev;
  44. struct nci_dev *ndev;
  45. unsigned int gpio_en;
  46. unsigned int gpio_fw;
  47. unsigned int gpio_irq;
  48. int hard_fault; /*
  49. * < 0 if hardware error occurred (e.g. i2c err)
  50. * and prevents normal operation.
  51. */
  52. };
  53. static int nxp_nci_i2c_set_mode(void *phy_id,
  54. enum nxp_nci_mode mode)
  55. {
  56. struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id;
  57. gpio_set_value(phy->gpio_fw, (mode == NXP_NCI_MODE_FW) ? 1 : 0);
  58. gpio_set_value(phy->gpio_en, (mode != NXP_NCI_MODE_COLD) ? 1 : 0);
  59. usleep_range(10000, 15000);
  60. if (mode == NXP_NCI_MODE_COLD)
  61. phy->hard_fault = 0;
  62. return 0;
  63. }
  64. static int nxp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
  65. {
  66. int r;
  67. struct nxp_nci_i2c_phy *phy = phy_id;
  68. struct i2c_client *client = phy->i2c_dev;
  69. if (phy->hard_fault != 0)
  70. return phy->hard_fault;
  71. r = i2c_master_send(client, skb->data, skb->len);
  72. if (r == -EREMOTEIO) {
  73. /* Retry, chip was in standby */
  74. usleep_range(110000, 120000);
  75. r = i2c_master_send(client, skb->data, skb->len);
  76. }
  77. if (r < 0) {
  78. nfc_err(&client->dev, "Error %d on I2C send\n", r);
  79. } else if (r != skb->len) {
  80. nfc_err(&client->dev,
  81. "Invalid length sent: %u (expected %u)\n",
  82. r, skb->len);
  83. r = -EREMOTEIO;
  84. } else {
  85. /* Success but return 0 and not number of bytes */
  86. r = 0;
  87. }
  88. return r;
  89. }
  90. static struct nxp_nci_phy_ops i2c_phy_ops = {
  91. .set_mode = nxp_nci_i2c_set_mode,
  92. .write = nxp_nci_i2c_write,
  93. };
  94. static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy *phy,
  95. struct sk_buff **skb)
  96. {
  97. struct i2c_client *client = phy->i2c_dev;
  98. u16 header;
  99. size_t frame_len;
  100. int r;
  101. r = i2c_master_recv(client, (u8 *) &header, NXP_NCI_FW_HDR_LEN);
  102. if (r < 0) {
  103. goto fw_read_exit;
  104. } else if (r != NXP_NCI_FW_HDR_LEN) {
  105. nfc_err(&client->dev, "Incorrect header length: %u\n", r);
  106. r = -EBADMSG;
  107. goto fw_read_exit;
  108. }
  109. frame_len = (get_unaligned_be16(&header) & NXP_NCI_FW_FRAME_LEN_MASK) +
  110. NXP_NCI_FW_CRC_LEN;
  111. *skb = alloc_skb(NXP_NCI_FW_HDR_LEN + frame_len, GFP_KERNEL);
  112. if (*skb == NULL) {
  113. r = -ENOMEM;
  114. goto fw_read_exit;
  115. }
  116. memcpy(skb_put(*skb, NXP_NCI_FW_HDR_LEN), &header, NXP_NCI_FW_HDR_LEN);
  117. r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len);
  118. if (r != frame_len) {
  119. nfc_err(&client->dev,
  120. "Invalid frame length: %u (expected %zu)\n",
  121. r, frame_len);
  122. r = -EBADMSG;
  123. goto fw_read_exit_free_skb;
  124. }
  125. return 0;
  126. fw_read_exit_free_skb:
  127. kfree_skb(*skb);
  128. fw_read_exit:
  129. return r;
  130. }
  131. static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy *phy,
  132. struct sk_buff **skb)
  133. {
  134. struct nci_ctrl_hdr header; /* May actually be a data header */
  135. struct i2c_client *client = phy->i2c_dev;
  136. int r;
  137. r = i2c_master_recv(client, (u8 *) &header, NCI_CTRL_HDR_SIZE);
  138. if (r < 0) {
  139. goto nci_read_exit;
  140. } else if (r != NCI_CTRL_HDR_SIZE) {
  141. nfc_err(&client->dev, "Incorrect header length: %u\n", r);
  142. r = -EBADMSG;
  143. goto nci_read_exit;
  144. }
  145. *skb = alloc_skb(NCI_CTRL_HDR_SIZE + header.plen, GFP_KERNEL);
  146. if (*skb == NULL) {
  147. r = -ENOMEM;
  148. goto nci_read_exit;
  149. }
  150. memcpy(skb_put(*skb, NCI_CTRL_HDR_SIZE), (void *) &header,
  151. NCI_CTRL_HDR_SIZE);
  152. r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
  153. if (r != header.plen) {
  154. nfc_err(&client->dev,
  155. "Invalid frame payload length: %u (expected %u)\n",
  156. r, header.plen);
  157. r = -EBADMSG;
  158. goto nci_read_exit_free_skb;
  159. }
  160. return 0;
  161. nci_read_exit_free_skb:
  162. kfree_skb(*skb);
  163. nci_read_exit:
  164. return r;
  165. }
  166. static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
  167. {
  168. struct nxp_nci_i2c_phy *phy = phy_id;
  169. struct i2c_client *client;
  170. struct nxp_nci_info *info;
  171. struct sk_buff *skb = NULL;
  172. int r = 0;
  173. if (!phy || !phy->ndev)
  174. goto exit_irq_none;
  175. client = phy->i2c_dev;
  176. if (!client || irq != client->irq)
  177. goto exit_irq_none;
  178. info = nci_get_drvdata(phy->ndev);
  179. if (!info)
  180. goto exit_irq_none;
  181. mutex_lock(&info->info_lock);
  182. if (phy->hard_fault != 0)
  183. goto exit_irq_handled;
  184. switch (info->mode) {
  185. case NXP_NCI_MODE_NCI:
  186. r = nxp_nci_i2c_nci_read(phy, &skb);
  187. break;
  188. case NXP_NCI_MODE_FW:
  189. r = nxp_nci_i2c_fw_read(phy, &skb);
  190. break;
  191. case NXP_NCI_MODE_COLD:
  192. r = -EREMOTEIO;
  193. break;
  194. }
  195. if (r == -EREMOTEIO) {
  196. phy->hard_fault = r;
  197. skb = NULL;
  198. } else if (r < 0) {
  199. nfc_err(&client->dev, "Read failed with error %d\n", r);
  200. goto exit_irq_handled;
  201. }
  202. switch (info->mode) {
  203. case NXP_NCI_MODE_NCI:
  204. nci_recv_frame(phy->ndev, skb);
  205. break;
  206. case NXP_NCI_MODE_FW:
  207. nxp_nci_fw_recv_frame(phy->ndev, skb);
  208. break;
  209. case NXP_NCI_MODE_COLD:
  210. break;
  211. }
  212. exit_irq_handled:
  213. mutex_unlock(&info->info_lock);
  214. return IRQ_HANDLED;
  215. exit_irq_none:
  216. WARN_ON_ONCE(1);
  217. return IRQ_NONE;
  218. }
  219. #ifdef CONFIG_OF
  220. static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
  221. {
  222. struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
  223. struct device_node *pp;
  224. int r;
  225. pp = client->dev.of_node;
  226. if (!pp)
  227. return -ENODEV;
  228. r = of_get_named_gpio(pp, "enable-gpios", 0);
  229. if (r == -EPROBE_DEFER)
  230. r = of_get_named_gpio(pp, "enable-gpios", 0);
  231. if (r < 0) {
  232. nfc_err(&client->dev, "Failed to get EN gpio, error: %d\n", r);
  233. return r;
  234. }
  235. phy->gpio_en = r;
  236. r = of_get_named_gpio(pp, "firmware-gpios", 0);
  237. if (r == -EPROBE_DEFER)
  238. r = of_get_named_gpio(pp, "firmware-gpios", 0);
  239. if (r < 0) {
  240. nfc_err(&client->dev, "Failed to get FW gpio, error: %d\n", r);
  241. return r;
  242. }
  243. phy->gpio_fw = r;
  244. r = irq_of_parse_and_map(pp, 0);
  245. if (r < 0) {
  246. nfc_err(&client->dev, "Unable to get irq, error: %d\n", r);
  247. return r;
  248. }
  249. client->irq = r;
  250. return 0;
  251. }
  252. #else
  253. static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
  254. {
  255. return -ENODEV;
  256. }
  257. #endif
  258. static int nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy *phy)
  259. {
  260. struct i2c_client *client = phy->i2c_dev;
  261. struct gpio_desc *gpiod_en, *gpiod_fw, *gpiod_irq;
  262. gpiod_en = devm_gpiod_get_index(&client->dev, NULL, 2, GPIOD_OUT_LOW);
  263. gpiod_fw = devm_gpiod_get_index(&client->dev, NULL, 1, GPIOD_OUT_LOW);
  264. gpiod_irq = devm_gpiod_get_index(&client->dev, NULL, 0, GPIOD_IN);
  265. if (IS_ERR(gpiod_en) || IS_ERR(gpiod_fw) || IS_ERR(gpiod_irq)) {
  266. nfc_err(&client->dev, "No GPIOs\n");
  267. return -EINVAL;
  268. }
  269. client->irq = gpiod_to_irq(gpiod_irq);
  270. if (client->irq < 0) {
  271. nfc_err(&client->dev, "No IRQ\n");
  272. return -EINVAL;
  273. }
  274. phy->gpio_en = desc_to_gpio(gpiod_en);
  275. phy->gpio_fw = desc_to_gpio(gpiod_fw);
  276. phy->gpio_irq = desc_to_gpio(gpiod_irq);
  277. return 0;
  278. }
  279. static int nxp_nci_i2c_probe(struct i2c_client *client,
  280. const struct i2c_device_id *id)
  281. {
  282. struct nxp_nci_i2c_phy *phy;
  283. struct nxp_nci_nfc_platform_data *pdata;
  284. int r;
  285. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  286. nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
  287. r = -ENODEV;
  288. goto probe_exit;
  289. }
  290. phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
  291. GFP_KERNEL);
  292. if (!phy) {
  293. r = -ENOMEM;
  294. goto probe_exit;
  295. }
  296. phy->i2c_dev = client;
  297. i2c_set_clientdata(client, phy);
  298. pdata = client->dev.platform_data;
  299. if (!pdata && client->dev.of_node) {
  300. r = nxp_nci_i2c_parse_devtree(client);
  301. if (r < 0) {
  302. nfc_err(&client->dev, "Failed to get DT data\n");
  303. goto probe_exit;
  304. }
  305. } else if (pdata) {
  306. phy->gpio_en = pdata->gpio_en;
  307. phy->gpio_fw = pdata->gpio_fw;
  308. client->irq = pdata->irq;
  309. } else if (ACPI_HANDLE(&client->dev)) {
  310. r = nxp_nci_i2c_acpi_config(phy);
  311. if (r < 0)
  312. goto probe_exit;
  313. goto nci_probe;
  314. } else {
  315. nfc_err(&client->dev, "No platform data\n");
  316. r = -EINVAL;
  317. goto probe_exit;
  318. }
  319. r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en,
  320. GPIOF_OUT_INIT_LOW, "nxp_nci_en");
  321. if (r < 0)
  322. goto probe_exit;
  323. r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw,
  324. GPIOF_OUT_INIT_LOW, "nxp_nci_fw");
  325. if (r < 0)
  326. goto probe_exit;
  327. nci_probe:
  328. r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
  329. NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
  330. if (r < 0)
  331. goto probe_exit;
  332. r = request_threaded_irq(client->irq, NULL,
  333. nxp_nci_i2c_irq_thread_fn,
  334. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  335. NXP_NCI_I2C_DRIVER_NAME, phy);
  336. if (r < 0)
  337. nfc_err(&client->dev, "Unable to register IRQ handler\n");
  338. probe_exit:
  339. return r;
  340. }
  341. static int nxp_nci_i2c_remove(struct i2c_client *client)
  342. {
  343. struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
  344. nxp_nci_remove(phy->ndev);
  345. free_irq(client->irq, phy);
  346. return 0;
  347. }
  348. static struct i2c_device_id nxp_nci_i2c_id_table[] = {
  349. {"nxp-nci_i2c", 0},
  350. {}
  351. };
  352. MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
  353. static const struct of_device_id of_nxp_nci_i2c_match[] = {
  354. { .compatible = "nxp,nxp-nci-i2c", },
  355. {},
  356. };
  357. MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
  358. #ifdef CONFIG_ACPI
  359. static struct acpi_device_id acpi_id[] = {
  360. { "NXP7471" },
  361. { },
  362. };
  363. MODULE_DEVICE_TABLE(acpi, acpi_id);
  364. #endif
  365. static struct i2c_driver nxp_nci_i2c_driver = {
  366. .driver = {
  367. .name = NXP_NCI_I2C_DRIVER_NAME,
  368. .owner = THIS_MODULE,
  369. .acpi_match_table = ACPI_PTR(acpi_id),
  370. .of_match_table = of_match_ptr(of_nxp_nci_i2c_match),
  371. },
  372. .probe = nxp_nci_i2c_probe,
  373. .id_table = nxp_nci_i2c_id_table,
  374. .remove = nxp_nci_i2c_remove,
  375. };
  376. module_i2c_driver(nxp_nci_i2c_driver);
  377. MODULE_LICENSE("GPL");
  378. MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
  379. MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
  380. MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");