spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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/gpio/consumer.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/acpi.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/delay.h>
  27. #include <linux/nfc.h>
  28. #include <net/nfc/nci.h>
  29. #include <linux/platform_data/st-nci.h>
  30. #include "st-nci.h"
  31. #define DRIVER_DESC "NCI NFC driver for ST_NCI"
  32. /* ndlc header */
  33. #define ST_NCI_FRAME_HEADROOM 1
  34. #define ST_NCI_FRAME_TAILROOM 0
  35. #define ST_NCI_SPI_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
  36. #define ST_NCI_SPI_MAX_SIZE 250 /* req 4.2.1 */
  37. #define ST_NCI_SPI_DRIVER_NAME "st_nci_spi"
  38. #define ST_NCI_GPIO_NAME_RESET "reset"
  39. struct st_nci_spi_phy {
  40. struct spi_device *spi_dev;
  41. struct llt_ndlc *ndlc;
  42. bool irq_active;
  43. unsigned int gpio_reset;
  44. unsigned int irq_polarity;
  45. struct st_nci_se_status se_status;
  46. };
  47. static int st_nci_spi_enable(void *phy_id)
  48. {
  49. struct st_nci_spi_phy *phy = phy_id;
  50. gpio_set_value(phy->gpio_reset, 0);
  51. usleep_range(10000, 15000);
  52. gpio_set_value(phy->gpio_reset, 1);
  53. usleep_range(80000, 85000);
  54. if (phy->ndlc->powered == 0 && phy->irq_active == 0) {
  55. enable_irq(phy->spi_dev->irq);
  56. phy->irq_active = true;
  57. }
  58. return 0;
  59. }
  60. static void st_nci_spi_disable(void *phy_id)
  61. {
  62. struct st_nci_spi_phy *phy = phy_id;
  63. disable_irq_nosync(phy->spi_dev->irq);
  64. phy->irq_active = false;
  65. }
  66. /*
  67. * Writing a frame must not return the number of written bytes.
  68. * It must return either zero for success, or <0 for error.
  69. * In addition, it must not alter the skb
  70. */
  71. static int st_nci_spi_write(void *phy_id, struct sk_buff *skb)
  72. {
  73. int r;
  74. struct st_nci_spi_phy *phy = phy_id;
  75. struct spi_device *dev = phy->spi_dev;
  76. struct sk_buff *skb_rx;
  77. u8 buf[ST_NCI_SPI_MAX_SIZE + NCI_DATA_HDR_SIZE +
  78. ST_NCI_FRAME_HEADROOM + ST_NCI_FRAME_TAILROOM];
  79. struct spi_transfer spi_xfer = {
  80. .tx_buf = skb->data,
  81. .rx_buf = buf,
  82. .len = skb->len,
  83. };
  84. if (phy->ndlc->hard_fault != 0)
  85. return phy->ndlc->hard_fault;
  86. r = spi_sync_transfer(dev, &spi_xfer, 1);
  87. /*
  88. * We may have received some valuable data on miso line.
  89. * Send them back in the ndlc state machine.
  90. */
  91. if (!r) {
  92. skb_rx = alloc_skb(skb->len, GFP_KERNEL);
  93. if (!skb_rx) {
  94. r = -ENOMEM;
  95. goto exit;
  96. }
  97. skb_put(skb_rx, skb->len);
  98. memcpy(skb_rx->data, buf, skb->len);
  99. ndlc_recv(phy->ndlc, skb_rx);
  100. }
  101. exit:
  102. return r;
  103. }
  104. /*
  105. * Reads an ndlc frame and returns it in a newly allocated sk_buff.
  106. * returns:
  107. * 0 : if received frame is complete
  108. * -EREMOTEIO : i2c read error (fatal)
  109. * -EBADMSG : frame was incorrect and discarded
  110. * -ENOMEM : cannot allocate skb, frame dropped
  111. */
  112. static int st_nci_spi_read(struct st_nci_spi_phy *phy,
  113. struct sk_buff **skb)
  114. {
  115. int r;
  116. u8 len;
  117. u8 buf[ST_NCI_SPI_MAX_SIZE];
  118. struct spi_device *dev = phy->spi_dev;
  119. struct spi_transfer spi_xfer = {
  120. .rx_buf = buf,
  121. .len = ST_NCI_SPI_MIN_SIZE,
  122. };
  123. r = spi_sync_transfer(dev, &spi_xfer, 1);
  124. if (r < 0)
  125. return -EREMOTEIO;
  126. len = be16_to_cpu(*(__be16 *) (buf + 2));
  127. if (len > ST_NCI_SPI_MAX_SIZE) {
  128. nfc_err(&dev->dev, "invalid frame len\n");
  129. phy->ndlc->hard_fault = 1;
  130. return -EBADMSG;
  131. }
  132. *skb = alloc_skb(ST_NCI_SPI_MIN_SIZE + len, GFP_KERNEL);
  133. if (*skb == NULL)
  134. return -ENOMEM;
  135. skb_reserve(*skb, ST_NCI_SPI_MIN_SIZE);
  136. skb_put(*skb, ST_NCI_SPI_MIN_SIZE);
  137. memcpy((*skb)->data, buf, ST_NCI_SPI_MIN_SIZE);
  138. if (!len)
  139. return 0;
  140. spi_xfer.len = len;
  141. r = spi_sync_transfer(dev, &spi_xfer, 1);
  142. if (r < 0) {
  143. kfree_skb(*skb);
  144. return -EREMOTEIO;
  145. }
  146. skb_put(*skb, len);
  147. memcpy((*skb)->data + ST_NCI_SPI_MIN_SIZE, buf, len);
  148. return 0;
  149. }
  150. /*
  151. * Reads an ndlc frame from the chip.
  152. *
  153. * On ST21NFCB, IRQ goes in idle state when read starts.
  154. */
  155. static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
  156. {
  157. struct st_nci_spi_phy *phy = phy_id;
  158. struct spi_device *dev;
  159. struct sk_buff *skb = NULL;
  160. int r;
  161. if (!phy || !phy->ndlc || irq != phy->spi_dev->irq) {
  162. WARN_ON_ONCE(1);
  163. return IRQ_NONE;
  164. }
  165. dev = phy->spi_dev;
  166. dev_dbg(&dev->dev, "IRQ\n");
  167. if (phy->ndlc->hard_fault)
  168. return IRQ_HANDLED;
  169. if (!phy->ndlc->powered) {
  170. st_nci_spi_disable(phy);
  171. return IRQ_HANDLED;
  172. }
  173. r = st_nci_spi_read(phy, &skb);
  174. if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
  175. return IRQ_HANDLED;
  176. ndlc_recv(phy->ndlc, skb);
  177. return IRQ_HANDLED;
  178. }
  179. static struct nfc_phy_ops spi_phy_ops = {
  180. .write = st_nci_spi_write,
  181. .enable = st_nci_spi_enable,
  182. .disable = st_nci_spi_disable,
  183. };
  184. static int st_nci_spi_acpi_request_resources(struct spi_device *spi_dev)
  185. {
  186. struct st_nci_spi_phy *phy = spi_get_drvdata(spi_dev);
  187. struct gpio_desc *gpiod_reset;
  188. struct device *dev = &spi_dev->dev;
  189. u8 tmp;
  190. /* Get RESET GPIO from ACPI */
  191. gpiod_reset = devm_gpiod_get_index(dev, ST_NCI_GPIO_NAME_RESET, 1,
  192. GPIOD_OUT_HIGH);
  193. if (IS_ERR(gpiod_reset)) {
  194. nfc_err(dev, "Unable to get RESET GPIO\n");
  195. return -ENODEV;
  196. }
  197. phy->gpio_reset = desc_to_gpio(gpiod_reset);
  198. phy->irq_polarity = irq_get_trigger_type(spi_dev->irq);
  199. phy->se_status.is_ese_present = false;
  200. phy->se_status.is_uicc_present = false;
  201. if (device_property_present(dev, "ese-present")) {
  202. device_property_read_u8(dev, "ese-present", &tmp);
  203. tmp = phy->se_status.is_ese_present;
  204. }
  205. if (device_property_present(dev, "uicc-present")) {
  206. device_property_read_u8(dev, "uicc-present", &tmp);
  207. tmp = phy->se_status.is_uicc_present;
  208. }
  209. return 0;
  210. }
  211. static int st_nci_spi_of_request_resources(struct spi_device *dev)
  212. {
  213. struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
  214. struct device_node *pp;
  215. int gpio;
  216. int r;
  217. pp = dev->dev.of_node;
  218. if (!pp)
  219. return -ENODEV;
  220. /* Get GPIO from device tree */
  221. gpio = of_get_named_gpio(pp, "reset-gpios", 0);
  222. if (gpio < 0) {
  223. nfc_err(&dev->dev,
  224. "Failed to retrieve reset-gpios from device tree\n");
  225. return gpio;
  226. }
  227. /* GPIO request and configuration */
  228. r = devm_gpio_request_one(&dev->dev, gpio,
  229. GPIOF_OUT_INIT_HIGH, ST_NCI_GPIO_NAME_RESET);
  230. if (r) {
  231. nfc_err(&dev->dev, "Failed to request reset pin\n");
  232. return r;
  233. }
  234. phy->gpio_reset = gpio;
  235. phy->irq_polarity = irq_get_trigger_type(dev->irq);
  236. phy->se_status.is_ese_present =
  237. of_property_read_bool(pp, "ese-present");
  238. phy->se_status.is_uicc_present =
  239. of_property_read_bool(pp, "uicc-present");
  240. return 0;
  241. }
  242. static int st_nci_spi_request_resources(struct spi_device *dev)
  243. {
  244. struct st_nci_nfc_platform_data *pdata;
  245. struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
  246. int r;
  247. pdata = dev->dev.platform_data;
  248. if (pdata == NULL) {
  249. nfc_err(&dev->dev, "No platform data\n");
  250. return -EINVAL;
  251. }
  252. /* store for later use */
  253. phy->gpio_reset = pdata->gpio_reset;
  254. phy->irq_polarity = pdata->irq_polarity;
  255. r = devm_gpio_request_one(&dev->dev,
  256. phy->gpio_reset, GPIOF_OUT_INIT_HIGH,
  257. ST_NCI_GPIO_NAME_RESET);
  258. if (r) {
  259. pr_err("%s : reset gpio_request failed\n", __FILE__);
  260. return r;
  261. }
  262. phy->se_status.is_ese_present = pdata->is_ese_present;
  263. phy->se_status.is_uicc_present = pdata->is_uicc_present;
  264. return 0;
  265. }
  266. static int st_nci_spi_probe(struct spi_device *dev)
  267. {
  268. struct st_nci_spi_phy *phy;
  269. struct st_nci_nfc_platform_data *pdata;
  270. int r;
  271. dev_dbg(&dev->dev, "%s\n", __func__);
  272. dev_dbg(&dev->dev, "IRQ: %d\n", dev->irq);
  273. /* Check SPI platform functionnalities */
  274. if (!dev) {
  275. pr_debug("%s: dev is NULL. Device is not accessible.\n",
  276. __func__);
  277. return -ENODEV;
  278. }
  279. phy = devm_kzalloc(&dev->dev, sizeof(struct st_nci_spi_phy),
  280. GFP_KERNEL);
  281. if (!phy)
  282. return -ENOMEM;
  283. phy->spi_dev = dev;
  284. spi_set_drvdata(dev, phy);
  285. pdata = dev->dev.platform_data;
  286. if (!pdata && dev->dev.of_node) {
  287. r = st_nci_spi_of_request_resources(dev);
  288. if (r) {
  289. nfc_err(&dev->dev, "No platform data\n");
  290. return r;
  291. }
  292. } else if (pdata) {
  293. r = st_nci_spi_request_resources(dev);
  294. if (r) {
  295. nfc_err(&dev->dev,
  296. "Cannot get platform resources\n");
  297. return r;
  298. }
  299. } else if (ACPI_HANDLE(&dev->dev)) {
  300. r = st_nci_spi_acpi_request_resources(dev);
  301. if (r) {
  302. nfc_err(&dev->dev, "Cannot get ACPI data\n");
  303. return r;
  304. }
  305. } else {
  306. nfc_err(&dev->dev,
  307. "st_nci platform resources not available\n");
  308. return -ENODEV;
  309. }
  310. r = ndlc_probe(phy, &spi_phy_ops, &dev->dev,
  311. ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
  312. &phy->ndlc, &phy->se_status);
  313. if (r < 0) {
  314. nfc_err(&dev->dev, "Unable to register ndlc layer\n");
  315. return r;
  316. }
  317. phy->irq_active = true;
  318. r = devm_request_threaded_irq(&dev->dev, dev->irq, NULL,
  319. st_nci_irq_thread_fn,
  320. phy->irq_polarity | IRQF_ONESHOT,
  321. ST_NCI_SPI_DRIVER_NAME, phy);
  322. if (r < 0)
  323. nfc_err(&dev->dev, "Unable to register IRQ handler\n");
  324. return r;
  325. }
  326. static int st_nci_spi_remove(struct spi_device *dev)
  327. {
  328. struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
  329. dev_dbg(&dev->dev, "%s\n", __func__);
  330. ndlc_remove(phy->ndlc);
  331. return 0;
  332. }
  333. static struct spi_device_id st_nci_spi_id_table[] = {
  334. {ST_NCI_SPI_DRIVER_NAME, 0},
  335. {}
  336. };
  337. MODULE_DEVICE_TABLE(spi, st_nci_spi_id_table);
  338. static const struct acpi_device_id st_nci_spi_acpi_match[] = {
  339. {"SMO2101", 0},
  340. {}
  341. };
  342. MODULE_DEVICE_TABLE(acpi, st_nci_spi_acpi_match);
  343. static const struct of_device_id of_st_nci_spi_match[] = {
  344. { .compatible = "st,st21nfcb-spi", },
  345. {}
  346. };
  347. MODULE_DEVICE_TABLE(of, of_st_nci_spi_match);
  348. static struct spi_driver st_nci_spi_driver = {
  349. .driver = {
  350. .name = ST_NCI_SPI_DRIVER_NAME,
  351. .of_match_table = of_match_ptr(of_st_nci_spi_match),
  352. .acpi_match_table = ACPI_PTR(st_nci_spi_acpi_match),
  353. },
  354. .probe = st_nci_spi_probe,
  355. .id_table = st_nci_spi_id_table,
  356. .remove = st_nci_spi_remove,
  357. };
  358. module_spi_driver(st_nci_spi_driver);
  359. MODULE_LICENSE("GPL");
  360. MODULE_DESCRIPTION(DRIVER_DESC);