i2c.c 9.7 KB

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