1 // SPDX-License-Identifier: GPL-2.0-only
3 * Marvell NFC-over-SPI driver: SPI interface related functions
5 * Copyright (C) 2015, Marvell International Ltd.
8 #include <linux/module.h>
9 #include <linux/interrupt.h>
10 #include <linux/nfc.h>
11 #include <linux/of_irq.h>
12 #include <net/nfc/nci.h>
13 #include <net/nfc/nci_core.h>
14 #include <linux/spi/spi.h>
17 #define SPI_WAIT_HANDSHAKE 1
19 struct nfcmrvl_spi_drv_data
{
21 struct spi_device
*spi
;
22 struct nci_spi
*nci_spi
;
23 struct completion handshake_completion
;
24 struct nfcmrvl_private
*priv
;
27 static irqreturn_t
nfcmrvl_spi_int_irq_thread_fn(int irq
, void *drv_data_ptr
)
29 struct nfcmrvl_spi_drv_data
*drv_data
= drv_data_ptr
;
33 * Special case where we are waiting for SPI_INT deassertion to start a
36 if (test_and_clear_bit(SPI_WAIT_HANDSHAKE
, &drv_data
->flags
)) {
37 complete(&drv_data
->handshake_completion
);
41 /* Normal case, SPI_INT deasserted by slave to trigger a master read */
43 skb
= nci_spi_read(drv_data
->nci_spi
);
45 nfc_err(&drv_data
->spi
->dev
, "failed to read spi packet");
49 if (nfcmrvl_nci_recv_frame(drv_data
->priv
, skb
) < 0)
50 nfc_err(&drv_data
->spi
->dev
, "corrupted RX packet");
55 static int nfcmrvl_spi_nci_open(struct nfcmrvl_private
*priv
)
60 static int nfcmrvl_spi_nci_close(struct nfcmrvl_private
*priv
)
65 static int nfcmrvl_spi_nci_send(struct nfcmrvl_private
*priv
,
68 struct nfcmrvl_spi_drv_data
*drv_data
= priv
->drv_data
;
71 /* Reinit completion for slave handshake */
72 reinit_completion(&drv_data
->handshake_completion
);
73 set_bit(SPI_WAIT_HANDSHAKE
, &drv_data
->flags
);
76 * Append a dummy byte at the end of SPI frame. This is due to a
77 * specific DMA implementation in the controller
81 /* Send the SPI packet */
82 err
= nci_spi_send(drv_data
->nci_spi
, &drv_data
->handshake_completion
,
85 nfc_err(priv
->dev
, "spi_send failed %d", err
);
90 static void nfcmrvl_spi_nci_update_config(struct nfcmrvl_private
*priv
,
93 struct nfcmrvl_spi_drv_data
*drv_data
= priv
->drv_data
;
94 const struct nfcmrvl_fw_spi_config
*config
= param
;
96 drv_data
->nci_spi
->xfer_speed_hz
= config
->clk
;
99 static const struct nfcmrvl_if_ops spi_ops
= {
100 .nci_open
= nfcmrvl_spi_nci_open
,
101 .nci_close
= nfcmrvl_spi_nci_close
,
102 .nci_send
= nfcmrvl_spi_nci_send
,
103 .nci_update_config
= nfcmrvl_spi_nci_update_config
,
106 static int nfcmrvl_spi_parse_dt(struct device_node
*node
,
107 struct nfcmrvl_platform_data
*pdata
)
111 ret
= nfcmrvl_parse_dt(node
, pdata
);
113 pr_err("Failed to get generic entries\n");
117 ret
= irq_of_parse_and_map(node
, 0);
119 pr_err("Unable to get irq\n");
127 static int nfcmrvl_spi_probe(struct spi_device
*spi
)
129 const struct nfcmrvl_platform_data
*pdata
;
130 struct nfcmrvl_platform_data config
;
131 struct nfcmrvl_spi_drv_data
*drv_data
;
134 drv_data
= devm_kzalloc(&spi
->dev
, sizeof(*drv_data
), GFP_KERNEL
);
139 drv_data
->priv
= NULL
;
140 spi_set_drvdata(spi
, drv_data
);
142 pdata
= spi
->dev
.platform_data
;
144 if (!pdata
&& spi
->dev
.of_node
)
145 if (nfcmrvl_spi_parse_dt(spi
->dev
.of_node
, &config
) == 0)
151 ret
= devm_request_threaded_irq(&drv_data
->spi
->dev
, pdata
->irq
,
152 NULL
, nfcmrvl_spi_int_irq_thread_fn
,
153 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
154 "nfcmrvl_spi_int", drv_data
);
156 nfc_err(&drv_data
->spi
->dev
, "Unable to register IRQ handler");
160 drv_data
->priv
= nfcmrvl_nci_register_dev(NFCMRVL_PHY_SPI
,
164 if (IS_ERR(drv_data
->priv
))
165 return PTR_ERR(drv_data
->priv
);
167 drv_data
->priv
->support_fw_dnld
= true;
169 drv_data
->nci_spi
= nci_spi_allocate_spi(drv_data
->spi
, 0, 10,
170 drv_data
->priv
->ndev
);
172 /* Init completion for slave handshake */
173 init_completion(&drv_data
->handshake_completion
);
177 static void nfcmrvl_spi_remove(struct spi_device
*spi
)
179 struct nfcmrvl_spi_drv_data
*drv_data
= spi_get_drvdata(spi
);
181 nfcmrvl_nci_unregister_dev(drv_data
->priv
);
184 static const struct of_device_id of_nfcmrvl_spi_match
[] __maybe_unused
= {
185 { .compatible
= "marvell,nfc-spi", },
188 MODULE_DEVICE_TABLE(of
, of_nfcmrvl_spi_match
);
190 static const struct spi_device_id nfcmrvl_spi_id_table
[] = {
191 { "nfcmrvl_spi", 0 },
194 MODULE_DEVICE_TABLE(spi
, nfcmrvl_spi_id_table
);
196 static struct spi_driver nfcmrvl_spi_driver
= {
197 .probe
= nfcmrvl_spi_probe
,
198 .remove
= nfcmrvl_spi_remove
,
199 .id_table
= nfcmrvl_spi_id_table
,
201 .name
= "nfcmrvl_spi",
202 .of_match_table
= of_match_ptr(of_nfcmrvl_spi_match
),
206 module_spi_driver(nfcmrvl_spi_driver
);
208 MODULE_AUTHOR("Marvell International Ltd.");
209 MODULE_DESCRIPTION("Marvell NFC-over-SPI driver");
210 MODULE_LICENSE("GPL v2");