spi.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 <linux/gpio.h>
  29. #include "nfcmrvl.h"
  30. #define SPI_WAIT_HANDSHAKE 1
  31. struct nfcmrvl_spi_drv_data {
  32. unsigned long flags;
  33. struct spi_device *spi;
  34. struct nci_spi *nci_spi;
  35. struct completion handshake_completion;
  36. struct nfcmrvl_private *priv;
  37. };
  38. static irqreturn_t nfcmrvl_spi_int_irq_thread_fn(int irq, void *drv_data_ptr)
  39. {
  40. struct nfcmrvl_spi_drv_data *drv_data = drv_data_ptr;
  41. struct sk_buff *skb;
  42. /*
  43. * Special case where we are waiting for SPI_INT deassertion to start a
  44. * transfer.
  45. */
  46. if (test_and_clear_bit(SPI_WAIT_HANDSHAKE, &drv_data->flags)) {
  47. complete(&drv_data->handshake_completion);
  48. return IRQ_HANDLED;
  49. }
  50. /* Normal case, SPI_INT deasserted by slave to trigger a master read */
  51. skb = nci_spi_read(drv_data->nci_spi);
  52. if (!skb) {
  53. nfc_err(&drv_data->spi->dev, "failed to read spi packet");
  54. return IRQ_HANDLED;
  55. }
  56. if (nfcmrvl_nci_recv_frame(drv_data->priv, skb) < 0)
  57. nfc_err(&drv_data->spi->dev, "corrupted RX packet");
  58. return IRQ_HANDLED;
  59. }
  60. static int nfcmrvl_spi_nci_open(struct nfcmrvl_private *priv)
  61. {
  62. return 0;
  63. }
  64. static int nfcmrvl_spi_nci_close(struct nfcmrvl_private *priv)
  65. {
  66. return 0;
  67. }
  68. static int nfcmrvl_spi_nci_send(struct nfcmrvl_private *priv,
  69. struct sk_buff *skb)
  70. {
  71. struct nfcmrvl_spi_drv_data *drv_data = priv->drv_data;
  72. int err;
  73. /* Reinit completion for slave handshake */
  74. reinit_completion(&drv_data->handshake_completion);
  75. set_bit(SPI_WAIT_HANDSHAKE, &drv_data->flags);
  76. /*
  77. * Append a dummy byte at the end of SPI frame. This is due to a
  78. * specific DMA implementation in the controller
  79. */
  80. skb_put(skb, 1);
  81. /* Send the SPI packet */
  82. err = nci_spi_send(drv_data->nci_spi, &drv_data->handshake_completion,
  83. skb);
  84. if (err)
  85. nfc_err(priv->dev, "spi_send failed %d", err);
  86. return err;
  87. }
  88. static void nfcmrvl_spi_nci_update_config(struct nfcmrvl_private *priv,
  89. const void *param)
  90. {
  91. struct nfcmrvl_spi_drv_data *drv_data = priv->drv_data;
  92. const struct nfcmrvl_fw_spi_config *config = param;
  93. drv_data->nci_spi->xfer_speed_hz = config->clk;
  94. }
  95. static struct nfcmrvl_if_ops spi_ops = {
  96. .nci_open = nfcmrvl_spi_nci_open,
  97. .nci_close = nfcmrvl_spi_nci_close,
  98. .nci_send = nfcmrvl_spi_nci_send,
  99. .nci_update_config = nfcmrvl_spi_nci_update_config,
  100. };
  101. static int nfcmrvl_spi_parse_dt(struct device_node *node,
  102. struct nfcmrvl_platform_data *pdata)
  103. {
  104. int ret;
  105. ret = nfcmrvl_parse_dt(node, pdata);
  106. if (ret < 0) {
  107. pr_err("Failed to get generic entries\n");
  108. return ret;
  109. }
  110. ret = irq_of_parse_and_map(node, 0);
  111. if (ret < 0) {
  112. pr_err("Unable to get irq, error: %d\n", ret);
  113. return ret;
  114. }
  115. pdata->irq = ret;
  116. return 0;
  117. }
  118. static int nfcmrvl_spi_probe(struct spi_device *spi)
  119. {
  120. struct nfcmrvl_platform_data *pdata;
  121. struct nfcmrvl_platform_data config;
  122. struct nfcmrvl_spi_drv_data *drv_data;
  123. int ret = 0;
  124. drv_data = devm_kzalloc(&spi->dev, sizeof(*drv_data), GFP_KERNEL);
  125. if (!drv_data)
  126. return -ENOMEM;
  127. drv_data->spi = spi;
  128. drv_data->priv = NULL;
  129. spi_set_drvdata(spi, drv_data);
  130. pdata = spi->dev.platform_data;
  131. if (!pdata && spi->dev.of_node)
  132. if (nfcmrvl_spi_parse_dt(spi->dev.of_node, &config) == 0)
  133. pdata = &config;
  134. if (!pdata)
  135. return -EINVAL;
  136. ret = devm_request_threaded_irq(&drv_data->spi->dev, pdata->irq,
  137. NULL, nfcmrvl_spi_int_irq_thread_fn,
  138. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  139. "nfcmrvl_spi_int", drv_data);
  140. if (ret < 0) {
  141. nfc_err(&drv_data->spi->dev, "Unable to register IRQ handler");
  142. return -ENODEV;
  143. }
  144. drv_data->priv = nfcmrvl_nci_register_dev(NFCMRVL_PHY_SPI,
  145. drv_data, &spi_ops,
  146. &drv_data->spi->dev,
  147. pdata);
  148. if (IS_ERR(drv_data->priv))
  149. return PTR_ERR(drv_data->priv);
  150. drv_data->priv->support_fw_dnld = true;
  151. drv_data->nci_spi = nci_spi_allocate_spi(drv_data->spi, 0, 10,
  152. drv_data->priv->ndev);
  153. /* Init completion for slave handshake */
  154. init_completion(&drv_data->handshake_completion);
  155. return 0;
  156. }
  157. static int nfcmrvl_spi_remove(struct spi_device *spi)
  158. {
  159. struct nfcmrvl_spi_drv_data *drv_data = spi_get_drvdata(spi);
  160. nfcmrvl_nci_unregister_dev(drv_data->priv);
  161. return 0;
  162. }
  163. static const struct of_device_id of_nfcmrvl_spi_match[] = {
  164. { .compatible = "marvell,nfc-spi", },
  165. {},
  166. };
  167. MODULE_DEVICE_TABLE(of, of_nfcmrvl_spi_match);
  168. static const struct spi_device_id nfcmrvl_spi_id_table[] = {
  169. { "nfcmrvl_spi", 0 },
  170. { }
  171. };
  172. MODULE_DEVICE_TABLE(spi, nfcmrvl_spi_id_table);
  173. static struct spi_driver nfcmrvl_spi_driver = {
  174. .probe = nfcmrvl_spi_probe,
  175. .remove = nfcmrvl_spi_remove,
  176. .id_table = nfcmrvl_spi_id_table,
  177. .driver = {
  178. .name = "nfcmrvl_spi",
  179. .owner = THIS_MODULE,
  180. .of_match_table = of_match_ptr(of_nfcmrvl_spi_match),
  181. },
  182. };
  183. module_spi_driver(nfcmrvl_spi_driver);
  184. MODULE_AUTHOR("Marvell International Ltd.");
  185. MODULE_DESCRIPTION("Marvell NFC-over-SPI driver");
  186. MODULE_LICENSE("GPL v2");