spi.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * Marvell NFC-over-SPI driver: SPI interface related functions
  3. *
  4. * Copyright (C) 2015, Marvell International Ltd.
  5. *
  6. * This software file (the "File") is distributed by Marvell International
  7. * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  8. * (the "License"). You may use, redistribute and/or modify this File in
  9. * accordance with the terms and conditions of the License, a copy of which
  10. * is available on the worldwide web at
  11. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  12. *
  13. * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  14. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  15. * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
  16. * this warranty disclaimer.
  17. **/
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/nfc.h>
  22. #include <linux/gpio.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/of_gpio.h>
  25. #include <net/nfc/nci.h>
  26. #include <net/nfc/nci_core.h>
  27. #include <linux/spi/spi.h>
  28. #include "nfcmrvl.h"
  29. #define SPI_WAIT_HANDSHAKE 1
  30. struct nfcmrvl_spi_drv_data {
  31. unsigned long flags;
  32. struct spi_device *spi;
  33. struct nci_spi *nci_spi;
  34. struct completion handshake_completion;
  35. struct nfcmrvl_private *priv;
  36. };
  37. static irqreturn_t nfcmrvl_spi_int_irq_thread_fn(int irq, void *drv_data_ptr)
  38. {
  39. struct nfcmrvl_spi_drv_data *drv_data = drv_data_ptr;
  40. struct sk_buff *skb;
  41. /*
  42. * Special case where we are waiting for SPI_INT deassertion to start a
  43. * transfer.
  44. */
  45. if (test_and_clear_bit(SPI_WAIT_HANDSHAKE, &drv_data->flags)) {
  46. complete(&drv_data->handshake_completion);
  47. return IRQ_HANDLED;
  48. }
  49. /* Normal case, SPI_INT deasserted by slave to trigger a master read */
  50. skb = nci_spi_read(drv_data->nci_spi);
  51. if (!skb) {
  52. nfc_err(&drv_data->spi->dev, "failed to read spi packet");
  53. return IRQ_HANDLED;
  54. }
  55. if (nfcmrvl_nci_recv_frame(drv_data->priv, skb) < 0)
  56. nfc_err(&drv_data->spi->dev, "corrupted RX packet");
  57. return IRQ_HANDLED;
  58. }
  59. static int nfcmrvl_spi_nci_open(struct nfcmrvl_private *priv)
  60. {
  61. return 0;
  62. }
  63. static int nfcmrvl_spi_nci_close(struct nfcmrvl_private *priv)
  64. {
  65. return 0;
  66. }
  67. static int nfcmrvl_spi_nci_send(struct nfcmrvl_private *priv,
  68. struct sk_buff *skb)
  69. {
  70. struct nfcmrvl_spi_drv_data *drv_data = priv->drv_data;
  71. int err;
  72. /* Reinit completion for slave handshake */
  73. reinit_completion(&drv_data->handshake_completion);
  74. set_bit(SPI_WAIT_HANDSHAKE, &drv_data->flags);
  75. /*
  76. * Append a dummy byte at the end of SPI frame. This is due to a
  77. * specific DMA implementation in the controller
  78. */
  79. skb_put(skb, 1);
  80. /* Send the SPI packet */
  81. err = nci_spi_send(drv_data->nci_spi, &drv_data->handshake_completion,
  82. skb);
  83. if (err)
  84. nfc_err(priv->dev, "spi_send failed %d", err);
  85. return err;
  86. }
  87. static void nfcmrvl_spi_nci_update_config(struct nfcmrvl_private *priv,
  88. const void *param)
  89. {
  90. struct nfcmrvl_spi_drv_data *drv_data = priv->drv_data;
  91. const struct nfcmrvl_fw_spi_config *config = param;
  92. drv_data->nci_spi->xfer_speed_hz = config->clk;
  93. }
  94. static struct nfcmrvl_if_ops spi_ops = {
  95. .nci_open = nfcmrvl_spi_nci_open,
  96. .nci_close = nfcmrvl_spi_nci_close,
  97. .nci_send = nfcmrvl_spi_nci_send,
  98. .nci_update_config = nfcmrvl_spi_nci_update_config,
  99. };
  100. static int nfcmrvl_spi_parse_dt(struct device_node *node,
  101. struct nfcmrvl_platform_data *pdata)
  102. {
  103. int ret;
  104. ret = nfcmrvl_parse_dt(node, pdata);
  105. if (ret < 0) {
  106. pr_err("Failed to get generic entries\n");
  107. return ret;
  108. }
  109. ret = irq_of_parse_and_map(node, 0);
  110. if (ret < 0) {
  111. pr_err("Unable to get irq, error: %d\n", ret);
  112. return ret;
  113. }
  114. pdata->irq = ret;
  115. return 0;
  116. }
  117. static int nfcmrvl_spi_probe(struct spi_device *spi)
  118. {
  119. struct nfcmrvl_platform_data *pdata;
  120. struct nfcmrvl_platform_data config;
  121. struct nfcmrvl_spi_drv_data *drv_data;
  122. int ret = 0;
  123. drv_data = devm_kzalloc(&spi->dev, sizeof(*drv_data), GFP_KERNEL);
  124. if (!drv_data)
  125. return -ENOMEM;
  126. drv_data->spi = spi;
  127. drv_data->priv = NULL;
  128. spi_set_drvdata(spi, drv_data);
  129. pdata = spi->dev.platform_data;
  130. if (!pdata && spi->dev.of_node)
  131. if (nfcmrvl_spi_parse_dt(spi->dev.of_node, &config) == 0)
  132. pdata = &config;
  133. if (!pdata)
  134. return -EINVAL;
  135. ret = devm_request_threaded_irq(&drv_data->spi->dev, pdata->irq,
  136. NULL, nfcmrvl_spi_int_irq_thread_fn,
  137. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  138. "nfcmrvl_spi_int", drv_data);
  139. if (ret < 0) {
  140. nfc_err(&drv_data->spi->dev, "Unable to register IRQ handler");
  141. return -ENODEV;
  142. }
  143. drv_data->priv = nfcmrvl_nci_register_dev(NFCMRVL_PHY_SPI,
  144. drv_data, &spi_ops,
  145. &drv_data->spi->dev,
  146. pdata);
  147. if (IS_ERR(drv_data->priv))
  148. return PTR_ERR(drv_data->priv);
  149. drv_data->priv->support_fw_dnld = true;
  150. drv_data->nci_spi = nci_spi_allocate_spi(drv_data->spi, 0, 10,
  151. drv_data->priv->ndev);
  152. /* Init completion for slave handshake */
  153. init_completion(&drv_data->handshake_completion);
  154. return 0;
  155. }
  156. static int nfcmrvl_spi_remove(struct spi_device *spi)
  157. {
  158. struct nfcmrvl_spi_drv_data *drv_data = spi_get_drvdata(spi);
  159. nfcmrvl_nci_unregister_dev(drv_data->priv);
  160. return 0;
  161. }
  162. static const struct of_device_id of_nfcmrvl_spi_match[] = {
  163. { .compatible = "marvell,nfc-spi", },
  164. {},
  165. };
  166. MODULE_DEVICE_TABLE(of, of_nfcmrvl_spi_match);
  167. static const struct spi_device_id nfcmrvl_spi_id_table[] = {
  168. { "nfcmrvl_spi", 0 },
  169. { }
  170. };
  171. MODULE_DEVICE_TABLE(spi, nfcmrvl_spi_id_table);
  172. static struct spi_driver nfcmrvl_spi_driver = {
  173. .probe = nfcmrvl_spi_probe,
  174. .remove = nfcmrvl_spi_remove,
  175. .id_table = nfcmrvl_spi_id_table,
  176. .driver = {
  177. .name = "nfcmrvl_spi",
  178. .owner = THIS_MODULE,
  179. .of_match_table = of_match_ptr(of_nfcmrvl_spi_match),
  180. },
  181. };
  182. module_spi_driver(nfcmrvl_spi_driver);
  183. MODULE_AUTHOR("Marvell International Ltd.");
  184. MODULE_DESCRIPTION("Marvell NFC-over-SPI driver");
  185. MODULE_LICENSE("GPL v2");