i2c.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * I2C Link Layer for ST NCI NFC controller familly based Driver
  4. * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/module.h>
  8. #include <linux/i2c.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/acpi.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/delay.h>
  13. #include <linux/nfc.h>
  14. #include <linux/of.h>
  15. #include "st-nci.h"
  16. #define DRIVER_DESC "NCI NFC driver for ST_NCI"
  17. /* ndlc header */
  18. #define ST_NCI_FRAME_HEADROOM 1
  19. #define ST_NCI_FRAME_TAILROOM 0
  20. #define ST_NCI_I2C_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
  21. #define ST_NCI_I2C_MAX_SIZE 250 /* req 4.2.1 */
  22. #define ST_NCI_DRIVER_NAME "st_nci"
  23. #define ST_NCI_I2C_DRIVER_NAME "st_nci_i2c"
  24. struct st_nci_i2c_phy {
  25. struct i2c_client *i2c_dev;
  26. struct llt_ndlc *ndlc;
  27. bool irq_active;
  28. struct gpio_desc *gpiod_reset;
  29. struct st_nci_se_status se_status;
  30. };
  31. static int st_nci_i2c_enable(void *phy_id)
  32. {
  33. struct st_nci_i2c_phy *phy = phy_id;
  34. gpiod_set_value(phy->gpiod_reset, 0);
  35. usleep_range(10000, 15000);
  36. gpiod_set_value(phy->gpiod_reset, 1);
  37. usleep_range(80000, 85000);
  38. if (phy->ndlc->powered == 0 && phy->irq_active == 0) {
  39. enable_irq(phy->i2c_dev->irq);
  40. phy->irq_active = true;
  41. }
  42. return 0;
  43. }
  44. static void st_nci_i2c_disable(void *phy_id)
  45. {
  46. struct st_nci_i2c_phy *phy = phy_id;
  47. disable_irq_nosync(phy->i2c_dev->irq);
  48. phy->irq_active = false;
  49. }
  50. /*
  51. * Writing a frame must not return the number of written bytes.
  52. * It must return either zero for success, or <0 for error.
  53. * In addition, it must not alter the skb
  54. */
  55. static int st_nci_i2c_write(void *phy_id, struct sk_buff *skb)
  56. {
  57. int r;
  58. struct st_nci_i2c_phy *phy = phy_id;
  59. struct i2c_client *client = phy->i2c_dev;
  60. if (phy->ndlc->hard_fault != 0)
  61. return phy->ndlc->hard_fault;
  62. r = i2c_master_send(client, skb->data, skb->len);
  63. if (r < 0) { /* Retry, chip was in standby */
  64. usleep_range(1000, 4000);
  65. r = i2c_master_send(client, skb->data, skb->len);
  66. }
  67. if (r >= 0) {
  68. if (r != skb->len)
  69. r = -EREMOTEIO;
  70. else
  71. r = 0;
  72. }
  73. return r;
  74. }
  75. /*
  76. * Reads an ndlc frame and returns it in a newly allocated sk_buff.
  77. * returns:
  78. * 0 : if received frame is complete
  79. * -EREMOTEIO : i2c read error (fatal)
  80. * -EBADMSG : frame was incorrect and discarded
  81. * -ENOMEM : cannot allocate skb, frame dropped
  82. */
  83. static int st_nci_i2c_read(struct st_nci_i2c_phy *phy,
  84. struct sk_buff **skb)
  85. {
  86. int r;
  87. u8 len;
  88. u8 buf[ST_NCI_I2C_MAX_SIZE];
  89. struct i2c_client *client = phy->i2c_dev;
  90. r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
  91. if (r < 0) { /* Retry, chip was in standby */
  92. usleep_range(1000, 4000);
  93. r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
  94. }
  95. if (r != ST_NCI_I2C_MIN_SIZE)
  96. return -EREMOTEIO;
  97. len = be16_to_cpu(*(__be16 *) (buf + 2));
  98. if (len > ST_NCI_I2C_MAX_SIZE) {
  99. nfc_err(&client->dev, "invalid frame len\n");
  100. return -EBADMSG;
  101. }
  102. *skb = alloc_skb(ST_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
  103. if (*skb == NULL)
  104. return -ENOMEM;
  105. skb_reserve(*skb, ST_NCI_I2C_MIN_SIZE);
  106. skb_put(*skb, ST_NCI_I2C_MIN_SIZE);
  107. memcpy((*skb)->data, buf, ST_NCI_I2C_MIN_SIZE);
  108. if (!len)
  109. return 0;
  110. r = i2c_master_recv(client, buf, len);
  111. if (r != len) {
  112. kfree_skb(*skb);
  113. return -EREMOTEIO;
  114. }
  115. skb_put(*skb, len);
  116. memcpy((*skb)->data + ST_NCI_I2C_MIN_SIZE, buf, len);
  117. return 0;
  118. }
  119. /*
  120. * Reads an ndlc frame from the chip.
  121. *
  122. * On ST_NCI, IRQ goes in idle state when read starts.
  123. */
  124. static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
  125. {
  126. struct st_nci_i2c_phy *phy = phy_id;
  127. struct sk_buff *skb = NULL;
  128. int r;
  129. if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) {
  130. WARN_ON_ONCE(1);
  131. return IRQ_NONE;
  132. }
  133. if (phy->ndlc->hard_fault)
  134. return IRQ_HANDLED;
  135. if (!phy->ndlc->powered) {
  136. st_nci_i2c_disable(phy);
  137. return IRQ_HANDLED;
  138. }
  139. r = st_nci_i2c_read(phy, &skb);
  140. if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
  141. return IRQ_HANDLED;
  142. ndlc_recv(phy->ndlc, skb);
  143. return IRQ_HANDLED;
  144. }
  145. static const struct nfc_phy_ops i2c_phy_ops = {
  146. .write = st_nci_i2c_write,
  147. .enable = st_nci_i2c_enable,
  148. .disable = st_nci_i2c_disable,
  149. };
  150. static const struct acpi_gpio_params reset_gpios = { 1, 0, false };
  151. static const struct acpi_gpio_mapping acpi_st_nci_gpios[] = {
  152. { "reset-gpios", &reset_gpios, 1 },
  153. {},
  154. };
  155. static int st_nci_i2c_probe(struct i2c_client *client,
  156. const struct i2c_device_id *id)
  157. {
  158. struct device *dev = &client->dev;
  159. struct st_nci_i2c_phy *phy;
  160. int r;
  161. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  162. nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
  163. return -ENODEV;
  164. }
  165. phy = devm_kzalloc(dev, sizeof(struct st_nci_i2c_phy), GFP_KERNEL);
  166. if (!phy)
  167. return -ENOMEM;
  168. phy->i2c_dev = client;
  169. i2c_set_clientdata(client, phy);
  170. r = devm_acpi_dev_add_driver_gpios(dev, acpi_st_nci_gpios);
  171. if (r)
  172. dev_dbg(dev, "Unable to add GPIO mapping table\n");
  173. /* Get RESET GPIO */
  174. phy->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  175. if (IS_ERR(phy->gpiod_reset)) {
  176. nfc_err(dev, "Unable to get RESET GPIO\n");
  177. return -ENODEV;
  178. }
  179. phy->se_status.is_ese_present =
  180. device_property_read_bool(dev, "ese-present");
  181. phy->se_status.is_uicc_present =
  182. device_property_read_bool(dev, "uicc-present");
  183. r = ndlc_probe(phy, &i2c_phy_ops, &client->dev,
  184. ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
  185. &phy->ndlc, &phy->se_status);
  186. if (r < 0) {
  187. nfc_err(&client->dev, "Unable to register ndlc layer\n");
  188. return r;
  189. }
  190. phy->irq_active = true;
  191. r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  192. st_nci_irq_thread_fn,
  193. IRQF_ONESHOT,
  194. ST_NCI_DRIVER_NAME, phy);
  195. if (r < 0)
  196. nfc_err(&client->dev, "Unable to register IRQ handler\n");
  197. return r;
  198. }
  199. static void st_nci_i2c_remove(struct i2c_client *client)
  200. {
  201. struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
  202. ndlc_remove(phy->ndlc);
  203. }
  204. static const struct i2c_device_id st_nci_i2c_id_table[] = {
  205. {ST_NCI_DRIVER_NAME, 0},
  206. {}
  207. };
  208. MODULE_DEVICE_TABLE(i2c, st_nci_i2c_id_table);
  209. static const struct acpi_device_id st_nci_i2c_acpi_match[] __maybe_unused = {
  210. {"SMO2101"},
  211. {"SMO2102"},
  212. {}
  213. };
  214. MODULE_DEVICE_TABLE(acpi, st_nci_i2c_acpi_match);
  215. static const struct of_device_id of_st_nci_i2c_match[] __maybe_unused = {
  216. { .compatible = "st,st21nfcb-i2c", },
  217. { .compatible = "st,st21nfcb_i2c", },
  218. { .compatible = "st,st21nfcc-i2c", },
  219. {}
  220. };
  221. MODULE_DEVICE_TABLE(of, of_st_nci_i2c_match);
  222. static struct i2c_driver st_nci_i2c_driver = {
  223. .driver = {
  224. .name = ST_NCI_I2C_DRIVER_NAME,
  225. .of_match_table = of_match_ptr(of_st_nci_i2c_match),
  226. .acpi_match_table = ACPI_PTR(st_nci_i2c_acpi_match),
  227. },
  228. .probe = st_nci_i2c_probe,
  229. .id_table = st_nci_i2c_id_table,
  230. .remove = st_nci_i2c_remove,
  231. };
  232. module_i2c_driver(st_nci_i2c_driver);
  233. MODULE_LICENSE("GPL");
  234. MODULE_DESCRIPTION(DRIVER_DESC);