i2c.c 9.9 KB

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