i2c.c 10 KB

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