i2c.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. memcpy(skb_put(*skb, NXP_NCI_FW_HDR_LEN), &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. memcpy(skb_put(*skb, NCI_CTRL_HDR_SIZE), (void *) &header,
  149. NCI_CTRL_HDR_SIZE);
  150. r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
  151. if (r != header.plen) {
  152. nfc_err(&client->dev,
  153. "Invalid frame payload length: %u (expected %u)\n",
  154. r, header.plen);
  155. r = -EBADMSG;
  156. goto nci_read_exit_free_skb;
  157. }
  158. return 0;
  159. nci_read_exit_free_skb:
  160. kfree_skb(*skb);
  161. nci_read_exit:
  162. return r;
  163. }
  164. static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
  165. {
  166. struct nxp_nci_i2c_phy *phy = phy_id;
  167. struct i2c_client *client;
  168. struct nxp_nci_info *info;
  169. struct sk_buff *skb = NULL;
  170. int r = 0;
  171. if (!phy || !phy->ndev)
  172. goto exit_irq_none;
  173. client = phy->i2c_dev;
  174. if (!client || irq != client->irq)
  175. goto exit_irq_none;
  176. info = nci_get_drvdata(phy->ndev);
  177. if (!info)
  178. goto exit_irq_none;
  179. mutex_lock(&info->info_lock);
  180. if (phy->hard_fault != 0)
  181. goto exit_irq_handled;
  182. switch (info->mode) {
  183. case NXP_NCI_MODE_NCI:
  184. r = nxp_nci_i2c_nci_read(phy, &skb);
  185. break;
  186. case NXP_NCI_MODE_FW:
  187. r = nxp_nci_i2c_fw_read(phy, &skb);
  188. break;
  189. case NXP_NCI_MODE_COLD:
  190. r = -EREMOTEIO;
  191. break;
  192. }
  193. if (r == -EREMOTEIO) {
  194. phy->hard_fault = r;
  195. skb = NULL;
  196. } else if (r < 0) {
  197. nfc_err(&client->dev, "Read failed with error %d\n", r);
  198. goto exit_irq_handled;
  199. }
  200. switch (info->mode) {
  201. case NXP_NCI_MODE_NCI:
  202. nci_recv_frame(phy->ndev, skb);
  203. break;
  204. case NXP_NCI_MODE_FW:
  205. nxp_nci_fw_recv_frame(phy->ndev, skb);
  206. break;
  207. case NXP_NCI_MODE_COLD:
  208. break;
  209. }
  210. exit_irq_handled:
  211. mutex_unlock(&info->info_lock);
  212. return IRQ_HANDLED;
  213. exit_irq_none:
  214. WARN_ON_ONCE(1);
  215. return IRQ_NONE;
  216. }
  217. static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
  218. {
  219. struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
  220. struct device_node *pp;
  221. int r;
  222. pp = client->dev.of_node;
  223. if (!pp)
  224. return -ENODEV;
  225. r = of_get_named_gpio(pp, "enable-gpios", 0);
  226. if (r == -EPROBE_DEFER)
  227. r = of_get_named_gpio(pp, "enable-gpios", 0);
  228. if (r < 0) {
  229. nfc_err(&client->dev, "Failed to get EN gpio, error: %d\n", r);
  230. return r;
  231. }
  232. phy->gpio_en = r;
  233. r = of_get_named_gpio(pp, "firmware-gpios", 0);
  234. if (r == -EPROBE_DEFER)
  235. r = of_get_named_gpio(pp, "firmware-gpios", 0);
  236. if (r < 0) {
  237. nfc_err(&client->dev, "Failed to get FW gpio, error: %d\n", r);
  238. return r;
  239. }
  240. phy->gpio_fw = r;
  241. return 0;
  242. }
  243. static int nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy *phy)
  244. {
  245. struct i2c_client *client = phy->i2c_dev;
  246. struct gpio_desc *gpiod_en, *gpiod_fw;
  247. gpiod_en = devm_gpiod_get_index(&client->dev, NULL, 2, GPIOD_OUT_LOW);
  248. gpiod_fw = devm_gpiod_get_index(&client->dev, NULL, 1, GPIOD_OUT_LOW);
  249. if (IS_ERR(gpiod_en) || IS_ERR(gpiod_fw)) {
  250. nfc_err(&client->dev, "No GPIOs\n");
  251. return -EINVAL;
  252. }
  253. phy->gpio_en = desc_to_gpio(gpiod_en);
  254. phy->gpio_fw = desc_to_gpio(gpiod_fw);
  255. return 0;
  256. }
  257. static int nxp_nci_i2c_probe(struct i2c_client *client,
  258. const struct i2c_device_id *id)
  259. {
  260. struct nxp_nci_i2c_phy *phy;
  261. struct nxp_nci_nfc_platform_data *pdata;
  262. int r;
  263. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  264. nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
  265. r = -ENODEV;
  266. goto probe_exit;
  267. }
  268. phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
  269. GFP_KERNEL);
  270. if (!phy) {
  271. r = -ENOMEM;
  272. goto probe_exit;
  273. }
  274. phy->i2c_dev = client;
  275. i2c_set_clientdata(client, phy);
  276. pdata = client->dev.platform_data;
  277. if (!pdata && client->dev.of_node) {
  278. r = nxp_nci_i2c_parse_devtree(client);
  279. if (r < 0) {
  280. nfc_err(&client->dev, "Failed to get DT data\n");
  281. goto probe_exit;
  282. }
  283. } else if (pdata) {
  284. phy->gpio_en = pdata->gpio_en;
  285. phy->gpio_fw = pdata->gpio_fw;
  286. } else if (ACPI_HANDLE(&client->dev)) {
  287. r = nxp_nci_i2c_acpi_config(phy);
  288. if (r < 0)
  289. goto probe_exit;
  290. goto nci_probe;
  291. } else {
  292. nfc_err(&client->dev, "No platform data\n");
  293. r = -EINVAL;
  294. goto probe_exit;
  295. }
  296. r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en,
  297. GPIOF_OUT_INIT_LOW, "nxp_nci_en");
  298. if (r < 0)
  299. goto probe_exit;
  300. r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw,
  301. GPIOF_OUT_INIT_LOW, "nxp_nci_fw");
  302. if (r < 0)
  303. goto probe_exit;
  304. nci_probe:
  305. r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
  306. NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
  307. if (r < 0)
  308. goto probe_exit;
  309. r = request_threaded_irq(client->irq, NULL,
  310. nxp_nci_i2c_irq_thread_fn,
  311. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  312. NXP_NCI_I2C_DRIVER_NAME, phy);
  313. if (r < 0)
  314. nfc_err(&client->dev, "Unable to register IRQ handler\n");
  315. probe_exit:
  316. return r;
  317. }
  318. static int nxp_nci_i2c_remove(struct i2c_client *client)
  319. {
  320. struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
  321. nxp_nci_remove(phy->ndev);
  322. free_irq(client->irq, phy);
  323. return 0;
  324. }
  325. static struct i2c_device_id nxp_nci_i2c_id_table[] = {
  326. {"nxp-nci_i2c", 0},
  327. {}
  328. };
  329. MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
  330. static const struct of_device_id of_nxp_nci_i2c_match[] = {
  331. { .compatible = "nxp,nxp-nci-i2c", },
  332. {},
  333. };
  334. MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
  335. #ifdef CONFIG_ACPI
  336. static struct acpi_device_id acpi_id[] = {
  337. { "NXP7471" },
  338. { },
  339. };
  340. MODULE_DEVICE_TABLE(acpi, acpi_id);
  341. #endif
  342. static struct i2c_driver nxp_nci_i2c_driver = {
  343. .driver = {
  344. .name = NXP_NCI_I2C_DRIVER_NAME,
  345. .acpi_match_table = ACPI_PTR(acpi_id),
  346. .of_match_table = of_match_ptr(of_nxp_nci_i2c_match),
  347. },
  348. .probe = nxp_nci_i2c_probe,
  349. .id_table = nxp_nci_i2c_id_table,
  350. .remove = nxp_nci_i2c_remove,
  351. };
  352. module_i2c_driver(nxp_nci_i2c_driver);
  353. MODULE_LICENSE("GPL");
  354. MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
  355. MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
  356. MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");