1 // SPDX-License-Identifier: GPL-2.0-only
3 * SPI Link Layer for ST NCI based Driver
4 * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/module.h>
10 #include <linux/spi/spi.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/acpi.h>
13 #include <linux/interrupt.h>
14 #include <linux/delay.h>
15 #include <linux/nfc.h>
17 #include <net/nfc/nci.h>
21 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
24 #define ST_NCI_FRAME_HEADROOM 1
25 #define ST_NCI_FRAME_TAILROOM 0
27 #define ST_NCI_SPI_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
28 #define ST_NCI_SPI_MAX_SIZE 250 /* req 4.2.1 */
30 #define ST_NCI_DRIVER_NAME "st_nci"
31 #define ST_NCI_SPI_DRIVER_NAME "st_nci_spi"
33 struct st_nci_spi_phy
{
34 struct spi_device
*spi_dev
;
35 struct llt_ndlc
*ndlc
;
39 struct gpio_desc
*gpiod_reset
;
41 struct st_nci_se_status se_status
;
44 static int st_nci_spi_enable(void *phy_id
)
46 struct st_nci_spi_phy
*phy
= phy_id
;
48 gpiod_set_value(phy
->gpiod_reset
, 0);
49 usleep_range(10000, 15000);
50 gpiod_set_value(phy
->gpiod_reset
, 1);
51 usleep_range(80000, 85000);
53 if (phy
->ndlc
->powered
== 0 && phy
->irq_active
== 0) {
54 enable_irq(phy
->spi_dev
->irq
);
55 phy
->irq_active
= true;
61 static void st_nci_spi_disable(void *phy_id
)
63 struct st_nci_spi_phy
*phy
= phy_id
;
65 disable_irq_nosync(phy
->spi_dev
->irq
);
66 phy
->irq_active
= false;
70 * Writing a frame must not return the number of written bytes.
71 * It must return either zero for success, or <0 for error.
72 * In addition, it must not alter the skb
74 static int st_nci_spi_write(void *phy_id
, struct sk_buff
*skb
)
77 struct st_nci_spi_phy
*phy
= phy_id
;
78 struct spi_device
*dev
= phy
->spi_dev
;
79 struct sk_buff
*skb_rx
;
80 u8 buf
[ST_NCI_SPI_MAX_SIZE
+ NCI_DATA_HDR_SIZE
+
81 ST_NCI_FRAME_HEADROOM
+ ST_NCI_FRAME_TAILROOM
];
82 struct spi_transfer spi_xfer
= {
88 if (phy
->ndlc
->hard_fault
!= 0)
89 return phy
->ndlc
->hard_fault
;
91 r
= spi_sync_transfer(dev
, &spi_xfer
, 1);
93 * We may have received some valuable data on miso line.
94 * Send them back in the ndlc state machine.
97 skb_rx
= alloc_skb(skb
->len
, GFP_KERNEL
);
103 skb_put(skb_rx
, skb
->len
);
104 memcpy(skb_rx
->data
, buf
, skb
->len
);
105 ndlc_recv(phy
->ndlc
, skb_rx
);
113 * Reads an ndlc frame and returns it in a newly allocated sk_buff.
115 * 0 : if received frame is complete
116 * -EREMOTEIO : i2c read error (fatal)
117 * -EBADMSG : frame was incorrect and discarded
118 * -ENOMEM : cannot allocate skb, frame dropped
120 static int st_nci_spi_read(struct st_nci_spi_phy
*phy
,
121 struct sk_buff
**skb
)
125 u8 buf
[ST_NCI_SPI_MAX_SIZE
];
126 struct spi_device
*dev
= phy
->spi_dev
;
127 struct spi_transfer spi_xfer
= {
129 .len
= ST_NCI_SPI_MIN_SIZE
,
132 r
= spi_sync_transfer(dev
, &spi_xfer
, 1);
136 len
= be16_to_cpu(*(__be16
*) (buf
+ 2));
137 if (len
> ST_NCI_SPI_MAX_SIZE
) {
138 nfc_err(&dev
->dev
, "invalid frame len\n");
139 phy
->ndlc
->hard_fault
= 1;
143 *skb
= alloc_skb(ST_NCI_SPI_MIN_SIZE
+ len
, GFP_KERNEL
);
147 skb_reserve(*skb
, ST_NCI_SPI_MIN_SIZE
);
148 skb_put(*skb
, ST_NCI_SPI_MIN_SIZE
);
149 memcpy((*skb
)->data
, buf
, ST_NCI_SPI_MIN_SIZE
);
155 r
= spi_sync_transfer(dev
, &spi_xfer
, 1);
162 memcpy((*skb
)->data
+ ST_NCI_SPI_MIN_SIZE
, buf
, len
);
168 * Reads an ndlc frame from the chip.
170 * On ST21NFCB, IRQ goes in idle state when read starts.
172 static irqreturn_t
st_nci_irq_thread_fn(int irq
, void *phy_id
)
174 struct st_nci_spi_phy
*phy
= phy_id
;
175 struct spi_device
*dev
;
176 struct sk_buff
*skb
= NULL
;
179 if (!phy
|| !phy
->ndlc
|| irq
!= phy
->spi_dev
->irq
) {
185 dev_dbg(&dev
->dev
, "IRQ\n");
187 if (phy
->ndlc
->hard_fault
)
190 if (!phy
->ndlc
->powered
) {
191 st_nci_spi_disable(phy
);
195 r
= st_nci_spi_read(phy
, &skb
);
196 if (r
== -EREMOTEIO
|| r
== -ENOMEM
|| r
== -EBADMSG
)
199 ndlc_recv(phy
->ndlc
, skb
);
204 static struct nfc_phy_ops spi_phy_ops
= {
205 .write
= st_nci_spi_write
,
206 .enable
= st_nci_spi_enable
,
207 .disable
= st_nci_spi_disable
,
210 static const struct acpi_gpio_params reset_gpios
= { 1, 0, false };
212 static const struct acpi_gpio_mapping acpi_st_nci_gpios
[] = {
213 { "reset-gpios", &reset_gpios
, 1 },
217 static int st_nci_spi_probe(struct spi_device
*dev
)
219 struct st_nci_spi_phy
*phy
;
222 dev_dbg(&dev
->dev
, "%s\n", __func__
);
223 dev_dbg(&dev
->dev
, "IRQ: %d\n", dev
->irq
);
225 /* Check SPI platform functionnalities */
227 pr_debug("%s: dev is NULL. Device is not accessible.\n",
232 phy
= devm_kzalloc(&dev
->dev
, sizeof(struct st_nci_spi_phy
),
239 spi_set_drvdata(dev
, phy
);
241 r
= devm_acpi_dev_add_driver_gpios(&dev
->dev
, acpi_st_nci_gpios
);
243 dev_dbg(&dev
->dev
, "Unable to add GPIO mapping table\n");
246 phy
->gpiod_reset
= devm_gpiod_get(&dev
->dev
, "reset", GPIOD_OUT_HIGH
);
247 if (IS_ERR(phy
->gpiod_reset
)) {
248 nfc_err(&dev
->dev
, "Unable to get RESET GPIO\n");
249 return PTR_ERR(phy
->gpiod_reset
);
252 phy
->se_status
.is_ese_present
=
253 device_property_read_bool(&dev
->dev
, "ese-present");
254 phy
->se_status
.is_uicc_present
=
255 device_property_read_bool(&dev
->dev
, "uicc-present");
257 r
= ndlc_probe(phy
, &spi_phy_ops
, &dev
->dev
,
258 ST_NCI_FRAME_HEADROOM
, ST_NCI_FRAME_TAILROOM
,
259 &phy
->ndlc
, &phy
->se_status
);
261 nfc_err(&dev
->dev
, "Unable to register ndlc layer\n");
265 phy
->irq_active
= true;
266 r
= devm_request_threaded_irq(&dev
->dev
, dev
->irq
, NULL
,
267 st_nci_irq_thread_fn
,
269 ST_NCI_SPI_DRIVER_NAME
, phy
);
271 nfc_err(&dev
->dev
, "Unable to register IRQ handler\n");
276 static int st_nci_spi_remove(struct spi_device
*dev
)
278 struct st_nci_spi_phy
*phy
= spi_get_drvdata(dev
);
280 dev_dbg(&dev
->dev
, "%s\n", __func__
);
282 ndlc_remove(phy
->ndlc
);
287 static struct spi_device_id st_nci_spi_id_table
[] = {
288 {ST_NCI_SPI_DRIVER_NAME
, 0},
291 MODULE_DEVICE_TABLE(spi
, st_nci_spi_id_table
);
293 static const struct acpi_device_id st_nci_spi_acpi_match
[] = {
297 MODULE_DEVICE_TABLE(acpi
, st_nci_spi_acpi_match
);
299 static const struct of_device_id of_st_nci_spi_match
[] = {
300 { .compatible
= "st,st21nfcb-spi", },
303 MODULE_DEVICE_TABLE(of
, of_st_nci_spi_match
);
305 static struct spi_driver st_nci_spi_driver
= {
307 .name
= ST_NCI_SPI_DRIVER_NAME
,
308 .of_match_table
= of_match_ptr(of_st_nci_spi_match
),
309 .acpi_match_table
= ACPI_PTR(st_nci_spi_acpi_match
),
311 .probe
= st_nci_spi_probe
,
312 .id_table
= st_nci_spi_id_table
,
313 .remove
= st_nci_spi_remove
,
315 module_spi_driver(st_nci_spi_driver
);
317 MODULE_LICENSE("GPL");
318 MODULE_DESCRIPTION(DRIVER_DESC
);