spi.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SPI Link Layer for ST NCI 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/spi/spi.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 <net/nfc/nci.h>
  16. #include "st-nci.h"
  17. #define DRIVER_DESC "NCI NFC driver for ST_NCI"
  18. /* ndlc header */
  19. #define ST_NCI_FRAME_HEADROOM 1
  20. #define ST_NCI_FRAME_TAILROOM 0
  21. #define ST_NCI_SPI_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
  22. #define ST_NCI_SPI_MAX_SIZE 250 /* req 4.2.1 */
  23. #define ST_NCI_DRIVER_NAME "st_nci"
  24. #define ST_NCI_SPI_DRIVER_NAME "st_nci_spi"
  25. struct st_nci_spi_phy {
  26. struct spi_device *spi_dev;
  27. struct llt_ndlc *ndlc;
  28. bool irq_active;
  29. struct gpio_desc *gpiod_reset;
  30. struct st_nci_se_status se_status;
  31. };
  32. static int st_nci_spi_enable(void *phy_id)
  33. {
  34. struct st_nci_spi_phy *phy = phy_id;
  35. gpiod_set_value(phy->gpiod_reset, 0);
  36. usleep_range(10000, 15000);
  37. gpiod_set_value(phy->gpiod_reset, 1);
  38. usleep_range(80000, 85000);
  39. if (phy->ndlc->powered == 0 && phy->irq_active == 0) {
  40. enable_irq(phy->spi_dev->irq);
  41. phy->irq_active = true;
  42. }
  43. return 0;
  44. }
  45. static void st_nci_spi_disable(void *phy_id)
  46. {
  47. struct st_nci_spi_phy *phy = phy_id;
  48. disable_irq_nosync(phy->spi_dev->irq);
  49. phy->irq_active = false;
  50. }
  51. /*
  52. * Writing a frame must not return the number of written bytes.
  53. * It must return either zero for success, or <0 for error.
  54. * In addition, it must not alter the skb
  55. */
  56. static int st_nci_spi_write(void *phy_id, struct sk_buff *skb)
  57. {
  58. int r;
  59. struct st_nci_spi_phy *phy = phy_id;
  60. struct spi_device *dev = phy->spi_dev;
  61. struct sk_buff *skb_rx;
  62. u8 buf[ST_NCI_SPI_MAX_SIZE + NCI_DATA_HDR_SIZE +
  63. ST_NCI_FRAME_HEADROOM + ST_NCI_FRAME_TAILROOM];
  64. struct spi_transfer spi_xfer = {
  65. .tx_buf = skb->data,
  66. .rx_buf = buf,
  67. .len = skb->len,
  68. };
  69. if (phy->ndlc->hard_fault != 0)
  70. return phy->ndlc->hard_fault;
  71. r = spi_sync_transfer(dev, &spi_xfer, 1);
  72. /*
  73. * We may have received some valuable data on miso line.
  74. * Send them back in the ndlc state machine.
  75. */
  76. if (!r) {
  77. skb_rx = alloc_skb(skb->len, GFP_KERNEL);
  78. if (!skb_rx)
  79. return -ENOMEM;
  80. skb_put(skb_rx, skb->len);
  81. memcpy(skb_rx->data, buf, skb->len);
  82. ndlc_recv(phy->ndlc, skb_rx);
  83. }
  84. return r;
  85. }
  86. /*
  87. * Reads an ndlc frame and returns it in a newly allocated sk_buff.
  88. * returns:
  89. * 0 : if received frame is complete
  90. * -EREMOTEIO : i2c read error (fatal)
  91. * -EBADMSG : frame was incorrect and discarded
  92. * -ENOMEM : cannot allocate skb, frame dropped
  93. */
  94. static int st_nci_spi_read(struct st_nci_spi_phy *phy,
  95. struct sk_buff **skb)
  96. {
  97. int r;
  98. u8 len;
  99. u8 buf[ST_NCI_SPI_MAX_SIZE];
  100. struct spi_device *dev = phy->spi_dev;
  101. struct spi_transfer spi_xfer = {
  102. .rx_buf = buf,
  103. .len = ST_NCI_SPI_MIN_SIZE,
  104. };
  105. r = spi_sync_transfer(dev, &spi_xfer, 1);
  106. if (r < 0)
  107. return -EREMOTEIO;
  108. len = be16_to_cpu(*(__be16 *) (buf + 2));
  109. if (len > ST_NCI_SPI_MAX_SIZE) {
  110. nfc_err(&dev->dev, "invalid frame len\n");
  111. phy->ndlc->hard_fault = 1;
  112. return -EBADMSG;
  113. }
  114. *skb = alloc_skb(ST_NCI_SPI_MIN_SIZE + len, GFP_KERNEL);
  115. if (*skb == NULL)
  116. return -ENOMEM;
  117. skb_reserve(*skb, ST_NCI_SPI_MIN_SIZE);
  118. skb_put(*skb, ST_NCI_SPI_MIN_SIZE);
  119. memcpy((*skb)->data, buf, ST_NCI_SPI_MIN_SIZE);
  120. if (!len)
  121. return 0;
  122. spi_xfer.len = len;
  123. r = spi_sync_transfer(dev, &spi_xfer, 1);
  124. if (r < 0) {
  125. kfree_skb(*skb);
  126. return -EREMOTEIO;
  127. }
  128. skb_put(*skb, len);
  129. memcpy((*skb)->data + ST_NCI_SPI_MIN_SIZE, buf, len);
  130. return 0;
  131. }
  132. /*
  133. * Reads an ndlc frame from the chip.
  134. *
  135. * On ST21NFCB, IRQ goes in idle state when read starts.
  136. */
  137. static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
  138. {
  139. struct st_nci_spi_phy *phy = phy_id;
  140. struct sk_buff *skb = NULL;
  141. int r;
  142. if (!phy || !phy->ndlc || irq != phy->spi_dev->irq) {
  143. WARN_ON_ONCE(1);
  144. return IRQ_NONE;
  145. }
  146. if (phy->ndlc->hard_fault)
  147. return IRQ_HANDLED;
  148. if (!phy->ndlc->powered) {
  149. st_nci_spi_disable(phy);
  150. return IRQ_HANDLED;
  151. }
  152. r = st_nci_spi_read(phy, &skb);
  153. if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
  154. return IRQ_HANDLED;
  155. ndlc_recv(phy->ndlc, skb);
  156. return IRQ_HANDLED;
  157. }
  158. static const struct nfc_phy_ops spi_phy_ops = {
  159. .write = st_nci_spi_write,
  160. .enable = st_nci_spi_enable,
  161. .disable = st_nci_spi_disable,
  162. };
  163. static const struct acpi_gpio_params reset_gpios = { 1, 0, false };
  164. static const struct acpi_gpio_mapping acpi_st_nci_gpios[] = {
  165. { "reset-gpios", &reset_gpios, 1 },
  166. {},
  167. };
  168. static int st_nci_spi_probe(struct spi_device *dev)
  169. {
  170. struct st_nci_spi_phy *phy;
  171. int r;
  172. /* Check SPI platform functionnalities */
  173. if (!dev) {
  174. pr_debug("%s: dev is NULL. Device is not accessible.\n",
  175. __func__);
  176. return -ENODEV;
  177. }
  178. phy = devm_kzalloc(&dev->dev, sizeof(struct st_nci_spi_phy),
  179. GFP_KERNEL);
  180. if (!phy)
  181. return -ENOMEM;
  182. phy->spi_dev = dev;
  183. spi_set_drvdata(dev, phy);
  184. r = devm_acpi_dev_add_driver_gpios(&dev->dev, acpi_st_nci_gpios);
  185. if (r)
  186. dev_dbg(&dev->dev, "Unable to add GPIO mapping table\n");
  187. /* Get RESET GPIO */
  188. phy->gpiod_reset = devm_gpiod_get(&dev->dev, "reset", GPIOD_OUT_HIGH);
  189. if (IS_ERR(phy->gpiod_reset)) {
  190. nfc_err(&dev->dev, "Unable to get RESET GPIO\n");
  191. return PTR_ERR(phy->gpiod_reset);
  192. }
  193. phy->se_status.is_ese_present =
  194. device_property_read_bool(&dev->dev, "ese-present");
  195. phy->se_status.is_uicc_present =
  196. device_property_read_bool(&dev->dev, "uicc-present");
  197. r = ndlc_probe(phy, &spi_phy_ops, &dev->dev,
  198. ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
  199. &phy->ndlc, &phy->se_status);
  200. if (r < 0) {
  201. nfc_err(&dev->dev, "Unable to register ndlc layer\n");
  202. return r;
  203. }
  204. phy->irq_active = true;
  205. r = devm_request_threaded_irq(&dev->dev, dev->irq, NULL,
  206. st_nci_irq_thread_fn,
  207. IRQF_ONESHOT,
  208. ST_NCI_SPI_DRIVER_NAME, phy);
  209. if (r < 0)
  210. nfc_err(&dev->dev, "Unable to register IRQ handler\n");
  211. return r;
  212. }
  213. static void st_nci_spi_remove(struct spi_device *dev)
  214. {
  215. struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
  216. ndlc_remove(phy->ndlc);
  217. }
  218. static struct spi_device_id st_nci_spi_id_table[] = {
  219. {ST_NCI_SPI_DRIVER_NAME, 0},
  220. {"st21nfcb-spi", 0},
  221. {}
  222. };
  223. MODULE_DEVICE_TABLE(spi, st_nci_spi_id_table);
  224. static const struct acpi_device_id st_nci_spi_acpi_match[] __maybe_unused = {
  225. {"SMO2101", 0},
  226. {}
  227. };
  228. MODULE_DEVICE_TABLE(acpi, st_nci_spi_acpi_match);
  229. static const struct of_device_id of_st_nci_spi_match[] __maybe_unused = {
  230. { .compatible = "st,st21nfcb-spi", },
  231. {}
  232. };
  233. MODULE_DEVICE_TABLE(of, of_st_nci_spi_match);
  234. static struct spi_driver st_nci_spi_driver = {
  235. .driver = {
  236. .name = ST_NCI_SPI_DRIVER_NAME,
  237. .of_match_table = of_match_ptr(of_st_nci_spi_match),
  238. .acpi_match_table = ACPI_PTR(st_nci_spi_acpi_match),
  239. },
  240. .probe = st_nci_spi_probe,
  241. .id_table = st_nci_spi_id_table,
  242. .remove = st_nci_spi_remove,
  243. };
  244. module_spi_driver(st_nci_spi_driver);
  245. MODULE_LICENSE("GPL");
  246. MODULE_DESCRIPTION(DRIVER_DESC);