i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * I2C Link Layer for ST21NFCB NCI based Driver
  3. * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  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, see <http://www.gnu.org/licenses/>.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/crc-ccitt.h>
  19. #include <linux/module.h>
  20. #include <linux/i2c.h>
  21. #include <linux/gpio.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/delay.h>
  27. #include <linux/nfc.h>
  28. #include <linux/firmware.h>
  29. #include <linux/unaligned/access_ok.h>
  30. #include <linux/platform_data/st21nfcb.h>
  31. #include <net/nfc/nci.h>
  32. #include <net/nfc/llc.h>
  33. #include <net/nfc/nfc.h>
  34. #include "ndlc.h"
  35. #define DRIVER_DESC "NCI NFC driver for ST21NFCB"
  36. /* ndlc header */
  37. #define ST21NFCB_FRAME_HEADROOM 1
  38. #define ST21NFCB_FRAME_TAILROOM 0
  39. #define ST21NFCB_NCI_I2C_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
  40. #define ST21NFCB_NCI_I2C_MAX_SIZE 250 /* req 4.2.1 */
  41. #define ST21NFCB_NCI_I2C_DRIVER_NAME "st21nfcb_nci_i2c"
  42. static struct i2c_device_id st21nfcb_nci_i2c_id_table[] = {
  43. {ST21NFCB_NCI_DRIVER_NAME, 0},
  44. {}
  45. };
  46. MODULE_DEVICE_TABLE(i2c, st21nfcb_nci_i2c_id_table);
  47. struct st21nfcb_i2c_phy {
  48. struct i2c_client *i2c_dev;
  49. struct llt_ndlc *ndlc;
  50. unsigned int gpio_irq;
  51. unsigned int gpio_reset;
  52. unsigned int irq_polarity;
  53. int powered;
  54. /*
  55. * < 0 if hardware error occured (e.g. i2c err)
  56. * and prevents normal operation.
  57. */
  58. int hard_fault;
  59. };
  60. #define I2C_DUMP_SKB(info, skb) \
  61. do { \
  62. pr_debug("%s:\n", info); \
  63. print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
  64. 16, 1, (skb)->data, (skb)->len, 0); \
  65. } while (0)
  66. static int st21nfcb_nci_i2c_enable(void *phy_id)
  67. {
  68. struct st21nfcb_i2c_phy *phy = phy_id;
  69. gpio_set_value(phy->gpio_reset, 0);
  70. usleep_range(10000, 15000);
  71. gpio_set_value(phy->gpio_reset, 1);
  72. phy->powered = 1;
  73. usleep_range(80000, 85000);
  74. return 0;
  75. }
  76. static void st21nfcb_nci_i2c_disable(void *phy_id)
  77. {
  78. struct st21nfcb_i2c_phy *phy = phy_id;
  79. pr_info("\n");
  80. phy->powered = 0;
  81. /* reset chip in order to flush clf */
  82. gpio_set_value(phy->gpio_reset, 0);
  83. usleep_range(10000, 15000);
  84. gpio_set_value(phy->gpio_reset, 1);
  85. }
  86. static void st21nfcb_nci_remove_header(struct sk_buff *skb)
  87. {
  88. skb_pull(skb, ST21NFCB_FRAME_HEADROOM);
  89. }
  90. /*
  91. * Writing a frame must not return the number of written bytes.
  92. * It must return either zero for success, or <0 for error.
  93. * In addition, it must not alter the skb
  94. */
  95. static int st21nfcb_nci_i2c_write(void *phy_id, struct sk_buff *skb)
  96. {
  97. int r = -1;
  98. struct st21nfcb_i2c_phy *phy = phy_id;
  99. struct i2c_client *client = phy->i2c_dev;
  100. I2C_DUMP_SKB("st21nfcb_nci_i2c_write", skb);
  101. if (phy->hard_fault != 0)
  102. return phy->hard_fault;
  103. r = i2c_master_send(client, skb->data, skb->len);
  104. if (r == -EREMOTEIO) { /* Retry, chip was in standby */
  105. usleep_range(1000, 4000);
  106. r = i2c_master_send(client, skb->data, skb->len);
  107. }
  108. if (r >= 0) {
  109. if (r != skb->len)
  110. r = -EREMOTEIO;
  111. else
  112. r = 0;
  113. }
  114. st21nfcb_nci_remove_header(skb);
  115. return r;
  116. }
  117. /*
  118. * Reads an ndlc frame and returns it in a newly allocated sk_buff.
  119. * returns:
  120. * frame size : if received frame is complete (find ST21NFCB_SOF_EOF at
  121. * end of read)
  122. * -EAGAIN : if received frame is incomplete (not find ST21NFCB_SOF_EOF
  123. * at end of read)
  124. * -EREMOTEIO : i2c read error (fatal)
  125. * -EBADMSG : frame was incorrect and discarded
  126. * (value returned from st21nfcb_nci_i2c_repack)
  127. * -EIO : if no ST21NFCB_SOF_EOF is found after reaching
  128. * the read length end sequence
  129. */
  130. static int st21nfcb_nci_i2c_read(struct st21nfcb_i2c_phy *phy,
  131. struct sk_buff **skb)
  132. {
  133. int r;
  134. u8 len;
  135. u8 buf[ST21NFCB_NCI_I2C_MAX_SIZE];
  136. struct i2c_client *client = phy->i2c_dev;
  137. r = i2c_master_recv(client, buf, 4);
  138. if (r == -EREMOTEIO) { /* Retry, chip was in standby */
  139. usleep_range(1000, 4000);
  140. r = i2c_master_recv(client, buf, 4);
  141. } else if (r != 4) {
  142. nfc_err(&client->dev, "cannot read ndlc & nci header\n");
  143. return -EREMOTEIO;
  144. }
  145. len = be16_to_cpu(*(__be16 *) (buf + 2));
  146. if (len > ST21NFCB_NCI_I2C_MAX_SIZE) {
  147. nfc_err(&client->dev, "invalid frame len\n");
  148. return -EBADMSG;
  149. }
  150. *skb = alloc_skb(4 + len, GFP_KERNEL);
  151. if (*skb == NULL)
  152. return -ENOMEM;
  153. skb_reserve(*skb, 4);
  154. skb_put(*skb, 4);
  155. memcpy((*skb)->data, buf, 4);
  156. if (!len)
  157. return 0;
  158. r = i2c_master_recv(client, buf, len);
  159. if (r != len) {
  160. kfree_skb(*skb);
  161. return -EREMOTEIO;
  162. }
  163. skb_put(*skb, len);
  164. memcpy((*skb)->data + 4, buf, len);
  165. I2C_DUMP_SKB("i2c frame read", *skb);
  166. return 0;
  167. }
  168. /*
  169. * Reads an ndlc frame from the chip.
  170. *
  171. * On ST21NFCB, IRQ goes in idle state when read starts.
  172. */
  173. static irqreturn_t st21nfcb_nci_irq_thread_fn(int irq, void *phy_id)
  174. {
  175. struct st21nfcb_i2c_phy *phy = phy_id;
  176. struct i2c_client *client;
  177. struct sk_buff *skb = NULL;
  178. int r;
  179. if (!phy || irq != phy->i2c_dev->irq) {
  180. WARN_ON_ONCE(1);
  181. return IRQ_NONE;
  182. }
  183. client = phy->i2c_dev;
  184. dev_dbg(&client->dev, "IRQ\n");
  185. if (phy->hard_fault)
  186. return IRQ_HANDLED;
  187. if (!phy->powered) {
  188. st21nfcb_nci_i2c_disable(phy);
  189. return IRQ_HANDLED;
  190. }
  191. r = st21nfcb_nci_i2c_read(phy, &skb);
  192. if (r == -EREMOTEIO) {
  193. phy->hard_fault = r;
  194. ndlc_recv(phy->ndlc, NULL);
  195. return IRQ_HANDLED;
  196. } else if (r == -ENOMEM || r == -EBADMSG) {
  197. return IRQ_HANDLED;
  198. }
  199. ndlc_recv(phy->ndlc, skb);
  200. return IRQ_HANDLED;
  201. }
  202. static struct nfc_phy_ops i2c_phy_ops = {
  203. .write = st21nfcb_nci_i2c_write,
  204. .enable = st21nfcb_nci_i2c_enable,
  205. .disable = st21nfcb_nci_i2c_disable,
  206. };
  207. #ifdef CONFIG_OF
  208. static int st21nfcb_nci_i2c_of_request_resources(struct i2c_client *client)
  209. {
  210. struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
  211. struct device_node *pp;
  212. int gpio;
  213. int r;
  214. pp = client->dev.of_node;
  215. if (!pp)
  216. return -ENODEV;
  217. /* Get GPIO from device tree */
  218. gpio = of_get_named_gpio(pp, "reset-gpios", 0);
  219. if (gpio < 0) {
  220. nfc_err(&client->dev,
  221. "Failed to retrieve reset-gpios from device tree\n");
  222. return gpio;
  223. }
  224. /* GPIO request and configuration */
  225. r = devm_gpio_request(&client->dev, gpio, "clf_reset");
  226. if (r) {
  227. nfc_err(&client->dev, "Failed to request reset pin\n");
  228. return -ENODEV;
  229. }
  230. r = gpio_direction_output(gpio, 1);
  231. if (r) {
  232. nfc_err(&client->dev,
  233. "Failed to set reset pin direction as output\n");
  234. return -ENODEV;
  235. }
  236. phy->gpio_reset = gpio;
  237. /* IRQ */
  238. r = irq_of_parse_and_map(pp, 0);
  239. if (r < 0) {
  240. nfc_err(&client->dev,
  241. "Unable to get irq, error: %d\n", r);
  242. return r;
  243. }
  244. phy->irq_polarity = irq_get_trigger_type(r);
  245. client->irq = r;
  246. return 0;
  247. }
  248. #else
  249. static int st21nfcb_nci_i2c_of_request_resources(struct i2c_client *client)
  250. {
  251. return -ENODEV;
  252. }
  253. #endif
  254. static int st21nfcb_nci_i2c_request_resources(struct i2c_client *client)
  255. {
  256. struct st21nfcb_nfc_platform_data *pdata;
  257. struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
  258. int r;
  259. int irq;
  260. pdata = client->dev.platform_data;
  261. if (pdata == NULL) {
  262. nfc_err(&client->dev, "No platform data\n");
  263. return -EINVAL;
  264. }
  265. /* store for later use */
  266. phy->gpio_irq = pdata->gpio_irq;
  267. phy->gpio_reset = pdata->gpio_reset;
  268. phy->irq_polarity = pdata->irq_polarity;
  269. r = devm_gpio_request(&client->dev, phy->gpio_irq, "wake_up");
  270. if (r) {
  271. pr_err("%s : gpio_request failed\n", __FILE__);
  272. return -ENODEV;
  273. }
  274. r = gpio_direction_input(phy->gpio_irq);
  275. if (r) {
  276. pr_err("%s : gpio_direction_input failed\n", __FILE__);
  277. return -ENODEV;
  278. }
  279. r = devm_gpio_request(&client->dev,
  280. phy->gpio_reset, "clf_reset");
  281. if (r) {
  282. pr_err("%s : reset gpio_request failed\n", __FILE__);
  283. return -ENODEV;
  284. }
  285. r = gpio_direction_output(phy->gpio_reset, 1);
  286. if (r) {
  287. pr_err("%s : reset gpio_direction_output failed\n",
  288. __FILE__);
  289. return -ENODEV;
  290. }
  291. /* IRQ */
  292. irq = gpio_to_irq(phy->gpio_irq);
  293. if (irq < 0) {
  294. nfc_err(&client->dev,
  295. "Unable to get irq number for GPIO %d error %d\n",
  296. phy->gpio_irq, r);
  297. return -ENODEV;
  298. }
  299. client->irq = irq;
  300. return 0;
  301. }
  302. static int st21nfcb_nci_i2c_probe(struct i2c_client *client,
  303. const struct i2c_device_id *id)
  304. {
  305. struct st21nfcb_i2c_phy *phy;
  306. struct st21nfcb_nfc_platform_data *pdata;
  307. int r;
  308. dev_dbg(&client->dev, "%s\n", __func__);
  309. dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
  310. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  311. nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
  312. return -ENODEV;
  313. }
  314. phy = devm_kzalloc(&client->dev, sizeof(struct st21nfcb_i2c_phy),
  315. GFP_KERNEL);
  316. if (!phy) {
  317. nfc_err(&client->dev,
  318. "Cannot allocate memory for st21nfcb i2c phy.\n");
  319. return -ENOMEM;
  320. }
  321. phy->i2c_dev = client;
  322. i2c_set_clientdata(client, phy);
  323. pdata = client->dev.platform_data;
  324. if (!pdata && client->dev.of_node) {
  325. r = st21nfcb_nci_i2c_of_request_resources(client);
  326. if (r) {
  327. nfc_err(&client->dev, "No platform data\n");
  328. return r;
  329. }
  330. } else if (pdata) {
  331. r = st21nfcb_nci_i2c_request_resources(client);
  332. if (r) {
  333. nfc_err(&client->dev,
  334. "Cannot get platform resources\n");
  335. return r;
  336. }
  337. } else {
  338. nfc_err(&client->dev,
  339. "st21nfcb platform resources not available\n");
  340. return -ENODEV;
  341. }
  342. r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  343. st21nfcb_nci_irq_thread_fn,
  344. phy->irq_polarity | IRQF_ONESHOT,
  345. ST21NFCB_NCI_DRIVER_NAME, phy);
  346. if (r < 0) {
  347. nfc_err(&client->dev, "Unable to register IRQ handler\n");
  348. return r;
  349. }
  350. return ndlc_probe(phy, &i2c_phy_ops, &client->dev,
  351. ST21NFCB_FRAME_HEADROOM, ST21NFCB_FRAME_TAILROOM,
  352. &phy->ndlc);
  353. }
  354. static int st21nfcb_nci_i2c_remove(struct i2c_client *client)
  355. {
  356. struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
  357. dev_dbg(&client->dev, "%s\n", __func__);
  358. ndlc_remove(phy->ndlc);
  359. if (phy->powered)
  360. st21nfcb_nci_i2c_disable(phy);
  361. return 0;
  362. }
  363. static const struct of_device_id of_st21nfcb_i2c_match[] = {
  364. { .compatible = "st,st21nfcb_i2c", },
  365. {}
  366. };
  367. static struct i2c_driver st21nfcb_nci_i2c_driver = {
  368. .driver = {
  369. .owner = THIS_MODULE,
  370. .name = ST21NFCB_NCI_I2C_DRIVER_NAME,
  371. .owner = THIS_MODULE,
  372. .of_match_table = of_match_ptr(of_st21nfcb_i2c_match),
  373. },
  374. .probe = st21nfcb_nci_i2c_probe,
  375. .id_table = st21nfcb_nci_i2c_id_table,
  376. .remove = st21nfcb_nci_i2c_remove,
  377. };
  378. module_i2c_driver(st21nfcb_nci_i2c_driver);
  379. MODULE_LICENSE("GPL");
  380. MODULE_DESCRIPTION(DRIVER_DESC);