spi.c 7.6 KB

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