i2c.c 9.4 KB

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