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