spi.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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.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_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_SPI_DRIVER_NAME "st_nci_spi"
  35. static struct spi_device_id st_nci_spi_id_table[] = {
  36. {ST_NCI_SPI_DRIVER_NAME, 0},
  37. {}
  38. };
  39. MODULE_DEVICE_TABLE(spi, st_nci_spi_id_table);
  40. struct st_nci_spi_phy {
  41. struct spi_device *spi_dev;
  42. struct llt_ndlc *ndlc;
  43. unsigned int gpio_reset;
  44. unsigned int irq_polarity;
  45. };
  46. #define SPI_DUMP_SKB(info, skb) \
  47. do { \
  48. pr_debug("%s:\n", info); \
  49. print_hex_dump(KERN_DEBUG, "spi: ", DUMP_PREFIX_OFFSET, \
  50. 16, 1, (skb)->data, (skb)->len, 0); \
  51. } while (0)
  52. static int st_nci_spi_enable(void *phy_id)
  53. {
  54. struct st_nci_spi_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->spi_dev->irq);
  61. return 0;
  62. }
  63. static void st_nci_spi_disable(void *phy_id)
  64. {
  65. struct st_nci_spi_phy *phy = phy_id;
  66. disable_irq_nosync(phy->spi_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_spi_write(void *phy_id, struct sk_buff *skb)
  74. {
  75. int r;
  76. struct st_nci_spi_phy *phy = phy_id;
  77. struct spi_device *dev = phy->spi_dev;
  78. struct sk_buff *skb_rx;
  79. u8 buf[ST_NCI_SPI_MAX_SIZE];
  80. struct spi_transfer spi_xfer = {
  81. .tx_buf = skb->data,
  82. .rx_buf = buf,
  83. .len = skb->len,
  84. };
  85. SPI_DUMP_SKB("st_nci_spi_write", skb);
  86. if (phy->ndlc->hard_fault != 0)
  87. return phy->ndlc->hard_fault;
  88. r = spi_sync_transfer(dev, &spi_xfer, 1);
  89. /*
  90. * We may have received some valuable data on miso line.
  91. * Send them back in the ndlc state machine.
  92. */
  93. if (!r) {
  94. skb_rx = alloc_skb(skb->len, GFP_KERNEL);
  95. if (!skb_rx) {
  96. r = -ENOMEM;
  97. goto exit;
  98. }
  99. skb_put(skb_rx, skb->len);
  100. memcpy(skb_rx->data, buf, skb->len);
  101. ndlc_recv(phy->ndlc, skb_rx);
  102. }
  103. exit:
  104. return r;
  105. }
  106. /*
  107. * Reads an ndlc frame and returns it in a newly allocated sk_buff.
  108. * returns:
  109. * 0 : if received frame is complete
  110. * -EREMOTEIO : i2c read error (fatal)
  111. * -EBADMSG : frame was incorrect and discarded
  112. * -ENOMEM : cannot allocate skb, frame dropped
  113. */
  114. static int st_nci_spi_read(struct st_nci_spi_phy *phy,
  115. struct sk_buff **skb)
  116. {
  117. int r;
  118. u8 len;
  119. u8 buf[ST_NCI_SPI_MAX_SIZE];
  120. struct spi_device *dev = phy->spi_dev;
  121. struct spi_transfer spi_xfer = {
  122. .rx_buf = buf,
  123. .len = ST_NCI_SPI_MIN_SIZE,
  124. };
  125. r = spi_sync_transfer(dev, &spi_xfer, 1);
  126. if (r < 0)
  127. return -EREMOTEIO;
  128. len = be16_to_cpu(*(__be16 *) (buf + 2));
  129. if (len > ST_NCI_SPI_MAX_SIZE) {
  130. nfc_err(&dev->dev, "invalid frame len\n");
  131. phy->ndlc->hard_fault = 1;
  132. return -EBADMSG;
  133. }
  134. *skb = alloc_skb(ST_NCI_SPI_MIN_SIZE + len, GFP_KERNEL);
  135. if (*skb == NULL)
  136. return -ENOMEM;
  137. skb_reserve(*skb, ST_NCI_SPI_MIN_SIZE);
  138. skb_put(*skb, ST_NCI_SPI_MIN_SIZE);
  139. memcpy((*skb)->data, buf, ST_NCI_SPI_MIN_SIZE);
  140. if (!len)
  141. return 0;
  142. spi_xfer.len = len;
  143. r = spi_sync_transfer(dev, &spi_xfer, 1);
  144. if (r < 0) {
  145. kfree_skb(*skb);
  146. return -EREMOTEIO;
  147. }
  148. skb_put(*skb, len);
  149. memcpy((*skb)->data + ST_NCI_SPI_MIN_SIZE, buf, len);
  150. SPI_DUMP_SKB("spi frame read", *skb);
  151. return 0;
  152. }
  153. /*
  154. * Reads an ndlc frame from the chip.
  155. *
  156. * On ST21NFCB, IRQ goes in idle state when read starts.
  157. */
  158. static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
  159. {
  160. struct st_nci_spi_phy *phy = phy_id;
  161. struct spi_device *dev;
  162. struct sk_buff *skb = NULL;
  163. int r;
  164. if (!phy || !phy->ndlc || irq != phy->spi_dev->irq) {
  165. WARN_ON_ONCE(1);
  166. return IRQ_NONE;
  167. }
  168. dev = phy->spi_dev;
  169. dev_dbg(&dev->dev, "IRQ\n");
  170. if (phy->ndlc->hard_fault)
  171. return IRQ_HANDLED;
  172. if (!phy->ndlc->powered) {
  173. st_nci_spi_disable(phy);
  174. return IRQ_HANDLED;
  175. }
  176. r = st_nci_spi_read(phy, &skb);
  177. if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
  178. return IRQ_HANDLED;
  179. ndlc_recv(phy->ndlc, skb);
  180. return IRQ_HANDLED;
  181. }
  182. static struct nfc_phy_ops spi_phy_ops = {
  183. .write = st_nci_spi_write,
  184. .enable = st_nci_spi_enable,
  185. .disable = st_nci_spi_disable,
  186. };
  187. #ifdef CONFIG_OF
  188. static int st_nci_spi_of_request_resources(struct spi_device *dev)
  189. {
  190. struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
  191. struct device_node *pp;
  192. int gpio;
  193. int r;
  194. pp = dev->dev.of_node;
  195. if (!pp)
  196. return -ENODEV;
  197. /* Get GPIO from device tree */
  198. gpio = of_get_named_gpio(pp, "reset-gpios", 0);
  199. if (gpio < 0) {
  200. nfc_err(&dev->dev,
  201. "Failed to retrieve reset-gpios from device tree\n");
  202. return gpio;
  203. }
  204. /* GPIO request and configuration */
  205. r = devm_gpio_request_one(&dev->dev, gpio,
  206. GPIOF_OUT_INIT_HIGH, "clf_reset");
  207. if (r) {
  208. nfc_err(&dev->dev, "Failed to request reset pin\n");
  209. return r;
  210. }
  211. phy->gpio_reset = gpio;
  212. phy->irq_polarity = irq_get_trigger_type(dev->irq);
  213. return 0;
  214. }
  215. #else
  216. static int st_nci_spi_of_request_resources(struct spi_device *dev)
  217. {
  218. return -ENODEV;
  219. }
  220. #endif
  221. static int st_nci_spi_request_resources(struct spi_device *dev)
  222. {
  223. struct st_nci_nfc_platform_data *pdata;
  224. struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
  225. int r;
  226. pdata = dev->dev.platform_data;
  227. if (pdata == NULL) {
  228. nfc_err(&dev->dev, "No platform data\n");
  229. return -EINVAL;
  230. }
  231. /* store for later use */
  232. phy->gpio_reset = pdata->gpio_reset;
  233. phy->irq_polarity = pdata->irq_polarity;
  234. r = devm_gpio_request_one(&dev->dev,
  235. phy->gpio_reset, GPIOF_OUT_INIT_HIGH, "clf_reset");
  236. if (r) {
  237. pr_err("%s : reset gpio_request failed\n", __FILE__);
  238. return r;
  239. }
  240. return 0;
  241. }
  242. static int st_nci_spi_probe(struct spi_device *dev)
  243. {
  244. struct st_nci_spi_phy *phy;
  245. struct st_nci_nfc_platform_data *pdata;
  246. int r;
  247. dev_dbg(&dev->dev, "%s\n", __func__);
  248. dev_dbg(&dev->dev, "IRQ: %d\n", dev->irq);
  249. /* Check SPI platform functionnalities */
  250. if (!dev) {
  251. pr_debug("%s: dev is NULL. Device is not accessible.\n",
  252. __func__);
  253. return -ENODEV;
  254. }
  255. phy = devm_kzalloc(&dev->dev, sizeof(struct st_nci_spi_phy),
  256. GFP_KERNEL);
  257. if (!phy)
  258. return -ENOMEM;
  259. phy->spi_dev = dev;
  260. spi_set_drvdata(dev, phy);
  261. pdata = dev->dev.platform_data;
  262. if (!pdata && dev->dev.of_node) {
  263. r = st_nci_spi_of_request_resources(dev);
  264. if (r) {
  265. nfc_err(&dev->dev, "No platform data\n");
  266. return r;
  267. }
  268. } else if (pdata) {
  269. r = st_nci_spi_request_resources(dev);
  270. if (r) {
  271. nfc_err(&dev->dev,
  272. "Cannot get platform resources\n");
  273. return r;
  274. }
  275. } else {
  276. nfc_err(&dev->dev,
  277. "st_nci platform resources not available\n");
  278. return -ENODEV;
  279. }
  280. r = ndlc_probe(phy, &spi_phy_ops, &dev->dev,
  281. ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
  282. &phy->ndlc);
  283. if (r < 0) {
  284. nfc_err(&dev->dev, "Unable to register ndlc layer\n");
  285. return r;
  286. }
  287. r = devm_request_threaded_irq(&dev->dev, dev->irq, NULL,
  288. st_nci_irq_thread_fn,
  289. phy->irq_polarity | IRQF_ONESHOT,
  290. ST_NCI_SPI_DRIVER_NAME, phy);
  291. if (r < 0)
  292. nfc_err(&dev->dev, "Unable to register IRQ handler\n");
  293. return r;
  294. }
  295. static int st_nci_spi_remove(struct spi_device *dev)
  296. {
  297. struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
  298. dev_dbg(&dev->dev, "%s\n", __func__);
  299. ndlc_remove(phy->ndlc);
  300. return 0;
  301. }
  302. #ifdef CONFIG_OF
  303. static const struct of_device_id of_st_nci_spi_match[] = {
  304. { .compatible = "st,st21nfcb-spi", },
  305. {}
  306. };
  307. MODULE_DEVICE_TABLE(of, of_st_nci_spi_match);
  308. #endif
  309. static struct spi_driver st_nci_spi_driver = {
  310. .driver = {
  311. .owner = THIS_MODULE,
  312. .name = ST_NCI_SPI_DRIVER_NAME,
  313. .of_match_table = of_match_ptr(of_st_nci_spi_match),
  314. },
  315. .probe = st_nci_spi_probe,
  316. .id_table = st_nci_spi_id_table,
  317. .remove = st_nci_spi_remove,
  318. };
  319. module_spi_driver(st_nci_spi_driver);
  320. MODULE_LICENSE("GPL");
  321. MODULE_DESCRIPTION(DRIVER_DESC);