i2c.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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/module.h>
  31. #include <linux/nfc.h>
  32. #include <linux/gpio/consumer.h>
  33. #include <linux/of_gpio.h>
  34. #include <linux/of_irq.h>
  35. #include <linux/platform_data/nxp-nci.h>
  36. #include <asm/unaligned.h>
  37. #include <net/nfc/nfc.h>
  38. #include "nxp-nci.h"
  39. #define NXP_NCI_I2C_DRIVER_NAME "nxp-nci_i2c"
  40. #define NXP_NCI_I2C_MAX_PAYLOAD 32
  41. struct nxp_nci_i2c_phy {
  42. struct i2c_client *i2c_dev;
  43. struct nci_dev *ndev;
  44. unsigned int gpio_en;
  45. unsigned int gpio_fw;
  46. int hard_fault; /*
  47. * < 0 if hardware error occurred (e.g. i2c err)
  48. * and prevents normal operation.
  49. */
  50. };
  51. static int nxp_nci_i2c_set_mode(void *phy_id,
  52. enum nxp_nci_mode mode)
  53. {
  54. struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id;
  55. gpio_set_value(phy->gpio_fw, (mode == NXP_NCI_MODE_FW) ? 1 : 0);
  56. gpio_set_value(phy->gpio_en, (mode != NXP_NCI_MODE_COLD) ? 1 : 0);
  57. usleep_range(10000, 15000);
  58. if (mode == NXP_NCI_MODE_COLD)
  59. phy->hard_fault = 0;
  60. return 0;
  61. }
  62. static int nxp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
  63. {
  64. int r;
  65. struct nxp_nci_i2c_phy *phy = phy_id;
  66. struct i2c_client *client = phy->i2c_dev;
  67. if (phy->hard_fault != 0)
  68. return phy->hard_fault;
  69. r = i2c_master_send(client, skb->data, skb->len);
  70. if (r < 0) {
  71. /* Retry, chip was in standby */
  72. msleep(110);
  73. r = i2c_master_send(client, skb->data, skb->len);
  74. }
  75. if (r < 0) {
  76. nfc_err(&client->dev, "Error %d on I2C send\n", r);
  77. } else if (r != skb->len) {
  78. nfc_err(&client->dev,
  79. "Invalid length sent: %u (expected %u)\n",
  80. r, skb->len);
  81. r = -EREMOTEIO;
  82. } else {
  83. /* Success but return 0 and not number of bytes */
  84. r = 0;
  85. }
  86. return r;
  87. }
  88. static const struct nxp_nci_phy_ops i2c_phy_ops = {
  89. .set_mode = nxp_nci_i2c_set_mode,
  90. .write = nxp_nci_i2c_write,
  91. };
  92. static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy *phy,
  93. struct sk_buff **skb)
  94. {
  95. struct i2c_client *client = phy->i2c_dev;
  96. u16 header;
  97. size_t frame_len;
  98. int r;
  99. r = i2c_master_recv(client, (u8 *) &header, NXP_NCI_FW_HDR_LEN);
  100. if (r < 0) {
  101. goto fw_read_exit;
  102. } else if (r != NXP_NCI_FW_HDR_LEN) {
  103. nfc_err(&client->dev, "Incorrect header length: %u\n", r);
  104. r = -EBADMSG;
  105. goto fw_read_exit;
  106. }
  107. frame_len = (be16_to_cpu(header) & NXP_NCI_FW_FRAME_LEN_MASK) +
  108. NXP_NCI_FW_CRC_LEN;
  109. *skb = alloc_skb(NXP_NCI_FW_HDR_LEN + frame_len, GFP_KERNEL);
  110. if (*skb == NULL) {
  111. r = -ENOMEM;
  112. goto fw_read_exit;
  113. }
  114. skb_put_data(*skb, &header, NXP_NCI_FW_HDR_LEN);
  115. r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len);
  116. if (r != frame_len) {
  117. nfc_err(&client->dev,
  118. "Invalid frame length: %u (expected %zu)\n",
  119. r, frame_len);
  120. r = -EBADMSG;
  121. goto fw_read_exit_free_skb;
  122. }
  123. return 0;
  124. fw_read_exit_free_skb:
  125. kfree_skb(*skb);
  126. fw_read_exit:
  127. return r;
  128. }
  129. static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy *phy,
  130. struct sk_buff **skb)
  131. {
  132. struct nci_ctrl_hdr header; /* May actually be a data header */
  133. struct i2c_client *client = phy->i2c_dev;
  134. int r;
  135. r = i2c_master_recv(client, (u8 *) &header, NCI_CTRL_HDR_SIZE);
  136. if (r < 0) {
  137. goto nci_read_exit;
  138. } else if (r != NCI_CTRL_HDR_SIZE) {
  139. nfc_err(&client->dev, "Incorrect header length: %u\n", r);
  140. r = -EBADMSG;
  141. goto nci_read_exit;
  142. }
  143. *skb = alloc_skb(NCI_CTRL_HDR_SIZE + header.plen, GFP_KERNEL);
  144. if (*skb == NULL) {
  145. r = -ENOMEM;
  146. goto nci_read_exit;
  147. }
  148. skb_put_data(*skb, (void *)&header, NCI_CTRL_HDR_SIZE);
  149. r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
  150. if (r != header.plen) {
  151. nfc_err(&client->dev,
  152. "Invalid frame payload length: %u (expected %u)\n",
  153. r, header.plen);
  154. r = -EBADMSG;
  155. goto nci_read_exit_free_skb;
  156. }
  157. return 0;
  158. nci_read_exit_free_skb:
  159. kfree_skb(*skb);
  160. nci_read_exit:
  161. return r;
  162. }
  163. static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
  164. {
  165. struct nxp_nci_i2c_phy *phy = phy_id;
  166. struct i2c_client *client;
  167. struct nxp_nci_info *info;
  168. struct sk_buff *skb = NULL;
  169. int r = 0;
  170. if (!phy || !phy->ndev)
  171. goto exit_irq_none;
  172. client = phy->i2c_dev;
  173. if (!client || irq != client->irq)
  174. goto exit_irq_none;
  175. info = nci_get_drvdata(phy->ndev);
  176. if (!info)
  177. goto exit_irq_none;
  178. mutex_lock(&info->info_lock);
  179. if (phy->hard_fault != 0)
  180. goto exit_irq_handled;
  181. switch (info->mode) {
  182. case NXP_NCI_MODE_NCI:
  183. r = nxp_nci_i2c_nci_read(phy, &skb);
  184. break;
  185. case NXP_NCI_MODE_FW:
  186. r = nxp_nci_i2c_fw_read(phy, &skb);
  187. break;
  188. case NXP_NCI_MODE_COLD:
  189. r = -EREMOTEIO;
  190. break;
  191. }
  192. if (r == -EREMOTEIO) {
  193. phy->hard_fault = r;
  194. skb = NULL;
  195. } else if (r < 0) {
  196. nfc_err(&client->dev, "Read failed with error %d\n", r);
  197. goto exit_irq_handled;
  198. }
  199. switch (info->mode) {
  200. case NXP_NCI_MODE_NCI:
  201. nci_recv_frame(phy->ndev, skb);
  202. break;
  203. case NXP_NCI_MODE_FW:
  204. nxp_nci_fw_recv_frame(phy->ndev, skb);
  205. break;
  206. case NXP_NCI_MODE_COLD:
  207. break;
  208. }
  209. exit_irq_handled:
  210. mutex_unlock(&info->info_lock);
  211. return IRQ_HANDLED;
  212. exit_irq_none:
  213. WARN_ON_ONCE(1);
  214. return IRQ_NONE;
  215. }
  216. static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
  217. {
  218. struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
  219. struct device_node *pp;
  220. int r;
  221. pp = client->dev.of_node;
  222. if (!pp)
  223. return -ENODEV;
  224. r = of_get_named_gpio(pp, "enable-gpios", 0);
  225. if (r == -EPROBE_DEFER)
  226. r = of_get_named_gpio(pp, "enable-gpios", 0);
  227. if (r < 0) {
  228. nfc_err(&client->dev, "Failed to get EN gpio, error: %d\n", r);
  229. return r;
  230. }
  231. phy->gpio_en = r;
  232. r = of_get_named_gpio(pp, "firmware-gpios", 0);
  233. if (r == -EPROBE_DEFER)
  234. r = of_get_named_gpio(pp, "firmware-gpios", 0);
  235. if (r < 0) {
  236. nfc_err(&client->dev, "Failed to get FW gpio, error: %d\n", r);
  237. return r;
  238. }
  239. phy->gpio_fw = r;
  240. return 0;
  241. }
  242. static int nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy *phy)
  243. {
  244. struct i2c_client *client = phy->i2c_dev;
  245. struct gpio_desc *gpiod_en, *gpiod_fw;
  246. gpiod_en = devm_gpiod_get_index(&client->dev, NULL, 2, GPIOD_OUT_LOW);
  247. gpiod_fw = devm_gpiod_get_index(&client->dev, NULL, 1, GPIOD_OUT_LOW);
  248. if (IS_ERR(gpiod_en) || IS_ERR(gpiod_fw)) {
  249. nfc_err(&client->dev, "No GPIOs\n");
  250. return -EINVAL;
  251. }
  252. phy->gpio_en = desc_to_gpio(gpiod_en);
  253. phy->gpio_fw = desc_to_gpio(gpiod_fw);
  254. return 0;
  255. }
  256. static int nxp_nci_i2c_probe(struct i2c_client *client,
  257. const struct i2c_device_id *id)
  258. {
  259. struct nxp_nci_i2c_phy *phy;
  260. struct nxp_nci_nfc_platform_data *pdata;
  261. int r;
  262. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  263. nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
  264. r = -ENODEV;
  265. goto probe_exit;
  266. }
  267. phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
  268. GFP_KERNEL);
  269. if (!phy) {
  270. r = -ENOMEM;
  271. goto probe_exit;
  272. }
  273. phy->i2c_dev = client;
  274. i2c_set_clientdata(client, phy);
  275. pdata = client->dev.platform_data;
  276. if (!pdata && client->dev.of_node) {
  277. r = nxp_nci_i2c_parse_devtree(client);
  278. if (r < 0) {
  279. nfc_err(&client->dev, "Failed to get DT data\n");
  280. goto probe_exit;
  281. }
  282. } else if (pdata) {
  283. phy->gpio_en = pdata->gpio_en;
  284. phy->gpio_fw = pdata->gpio_fw;
  285. } else if (ACPI_HANDLE(&client->dev)) {
  286. r = nxp_nci_i2c_acpi_config(phy);
  287. if (r < 0)
  288. goto probe_exit;
  289. goto nci_probe;
  290. } else {
  291. nfc_err(&client->dev, "No platform data\n");
  292. r = -EINVAL;
  293. goto probe_exit;
  294. }
  295. r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en,
  296. GPIOF_OUT_INIT_LOW, "nxp_nci_en");
  297. if (r < 0)
  298. goto probe_exit;
  299. r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw,
  300. GPIOF_OUT_INIT_LOW, "nxp_nci_fw");
  301. if (r < 0)
  302. goto probe_exit;
  303. nci_probe:
  304. r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
  305. NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
  306. if (r < 0)
  307. goto probe_exit;
  308. r = request_threaded_irq(client->irq, NULL,
  309. nxp_nci_i2c_irq_thread_fn,
  310. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  311. NXP_NCI_I2C_DRIVER_NAME, phy);
  312. if (r < 0)
  313. nfc_err(&client->dev, "Unable to register IRQ handler\n");
  314. probe_exit:
  315. return r;
  316. }
  317. static int nxp_nci_i2c_remove(struct i2c_client *client)
  318. {
  319. struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
  320. nxp_nci_remove(phy->ndev);
  321. free_irq(client->irq, phy);
  322. return 0;
  323. }
  324. static const struct i2c_device_id nxp_nci_i2c_id_table[] = {
  325. {"nxp-nci_i2c", 0},
  326. {}
  327. };
  328. MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
  329. static const struct of_device_id of_nxp_nci_i2c_match[] = {
  330. { .compatible = "nxp,nxp-nci-i2c", },
  331. {},
  332. };
  333. MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
  334. #ifdef CONFIG_ACPI
  335. static struct acpi_device_id acpi_id[] = {
  336. { "NXP7471" },
  337. { },
  338. };
  339. MODULE_DEVICE_TABLE(acpi, acpi_id);
  340. #endif
  341. static struct i2c_driver nxp_nci_i2c_driver = {
  342. .driver = {
  343. .name = NXP_NCI_I2C_DRIVER_NAME,
  344. .acpi_match_table = ACPI_PTR(acpi_id),
  345. .of_match_table = of_match_ptr(of_nxp_nci_i2c_match),
  346. },
  347. .probe = nxp_nci_i2c_probe,
  348. .id_table = nxp_nci_i2c_id_table,
  349. .remove = nxp_nci_i2c_remove,
  350. };
  351. module_i2c_driver(nxp_nci_i2c_driver);
  352. MODULE_LICENSE("GPL");
  353. MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
  354. MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
  355. MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");