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