i2c.c 8.7 KB

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