2 * SPI Link Layer for ST NCI based Driver
3 * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/module.h>
21 #include <linux/spi/spi.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/acpi.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/nfc.h>
28 #include <net/nfc/nci.h>
32 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
35 #define ST_NCI_FRAME_HEADROOM 1
36 #define ST_NCI_FRAME_TAILROOM 0
38 #define ST_NCI_SPI_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
39 #define ST_NCI_SPI_MAX_SIZE 250 /* req 4.2.1 */
41 #define ST_NCI_DRIVER_NAME "st_nci"
42 #define ST_NCI_SPI_DRIVER_NAME "st_nci_spi"
44 struct st_nci_spi_phy
{
45 struct spi_device
*spi_dev
;
46 struct llt_ndlc
*ndlc
;
50 struct gpio_desc
*gpiod_reset
;
52 struct st_nci_se_status se_status
;
55 static int st_nci_spi_enable(void *phy_id
)
57 struct st_nci_spi_phy
*phy
= phy_id
;
59 gpiod_set_value(phy
->gpiod_reset
, 0);
60 usleep_range(10000, 15000);
61 gpiod_set_value(phy
->gpiod_reset
, 1);
62 usleep_range(80000, 85000);
64 if (phy
->ndlc
->powered
== 0 && phy
->irq_active
== 0) {
65 enable_irq(phy
->spi_dev
->irq
);
66 phy
->irq_active
= true;
72 static void st_nci_spi_disable(void *phy_id
)
74 struct st_nci_spi_phy
*phy
= phy_id
;
76 disable_irq_nosync(phy
->spi_dev
->irq
);
77 phy
->irq_active
= false;
81 * Writing a frame must not return the number of written bytes.
82 * It must return either zero for success, or <0 for error.
83 * In addition, it must not alter the skb
85 static int st_nci_spi_write(void *phy_id
, struct sk_buff
*skb
)
88 struct st_nci_spi_phy
*phy
= phy_id
;
89 struct spi_device
*dev
= phy
->spi_dev
;
90 struct sk_buff
*skb_rx
;
91 u8 buf
[ST_NCI_SPI_MAX_SIZE
+ NCI_DATA_HDR_SIZE
+
92 ST_NCI_FRAME_HEADROOM
+ ST_NCI_FRAME_TAILROOM
];
93 struct spi_transfer spi_xfer
= {
99 if (phy
->ndlc
->hard_fault
!= 0)
100 return phy
->ndlc
->hard_fault
;
102 r
= spi_sync_transfer(dev
, &spi_xfer
, 1);
104 * We may have received some valuable data on miso line.
105 * Send them back in the ndlc state machine.
108 skb_rx
= alloc_skb(skb
->len
, GFP_KERNEL
);
114 skb_put(skb_rx
, skb
->len
);
115 memcpy(skb_rx
->data
, buf
, skb
->len
);
116 ndlc_recv(phy
->ndlc
, skb_rx
);
124 * Reads an ndlc frame and returns it in a newly allocated sk_buff.
126 * 0 : if received frame is complete
127 * -EREMOTEIO : i2c read error (fatal)
128 * -EBADMSG : frame was incorrect and discarded
129 * -ENOMEM : cannot allocate skb, frame dropped
131 static int st_nci_spi_read(struct st_nci_spi_phy
*phy
,
132 struct sk_buff
**skb
)
136 u8 buf
[ST_NCI_SPI_MAX_SIZE
];
137 struct spi_device
*dev
= phy
->spi_dev
;
138 struct spi_transfer spi_xfer
= {
140 .len
= ST_NCI_SPI_MIN_SIZE
,
143 r
= spi_sync_transfer(dev
, &spi_xfer
, 1);
147 len
= be16_to_cpu(*(__be16
*) (buf
+ 2));
148 if (len
> ST_NCI_SPI_MAX_SIZE
) {
149 nfc_err(&dev
->dev
, "invalid frame len\n");
150 phy
->ndlc
->hard_fault
= 1;
154 *skb
= alloc_skb(ST_NCI_SPI_MIN_SIZE
+ len
, GFP_KERNEL
);
158 skb_reserve(*skb
, ST_NCI_SPI_MIN_SIZE
);
159 skb_put(*skb
, ST_NCI_SPI_MIN_SIZE
);
160 memcpy((*skb
)->data
, buf
, ST_NCI_SPI_MIN_SIZE
);
166 r
= spi_sync_transfer(dev
, &spi_xfer
, 1);
173 memcpy((*skb
)->data
+ ST_NCI_SPI_MIN_SIZE
, buf
, len
);
179 * Reads an ndlc frame from the chip.
181 * On ST21NFCB, IRQ goes in idle state when read starts.
183 static irqreturn_t
st_nci_irq_thread_fn(int irq
, void *phy_id
)
185 struct st_nci_spi_phy
*phy
= phy_id
;
186 struct spi_device
*dev
;
187 struct sk_buff
*skb
= NULL
;
190 if (!phy
|| !phy
->ndlc
|| irq
!= phy
->spi_dev
->irq
) {
196 dev_dbg(&dev
->dev
, "IRQ\n");
198 if (phy
->ndlc
->hard_fault
)
201 if (!phy
->ndlc
->powered
) {
202 st_nci_spi_disable(phy
);
206 r
= st_nci_spi_read(phy
, &skb
);
207 if (r
== -EREMOTEIO
|| r
== -ENOMEM
|| r
== -EBADMSG
)
210 ndlc_recv(phy
->ndlc
, skb
);
215 static struct nfc_phy_ops spi_phy_ops
= {
216 .write
= st_nci_spi_write
,
217 .enable
= st_nci_spi_enable
,
218 .disable
= st_nci_spi_disable
,
221 static const struct acpi_gpio_params reset_gpios
= { 1, 0, false };
223 static const struct acpi_gpio_mapping acpi_st_nci_gpios
[] = {
224 { "reset-gpios", &reset_gpios
, 1 },
228 static int st_nci_spi_probe(struct spi_device
*dev
)
230 struct st_nci_spi_phy
*phy
;
233 dev_dbg(&dev
->dev
, "%s\n", __func__
);
234 dev_dbg(&dev
->dev
, "IRQ: %d\n", dev
->irq
);
236 /* Check SPI platform functionnalities */
238 pr_debug("%s: dev is NULL. Device is not accessible.\n",
243 phy
= devm_kzalloc(&dev
->dev
, sizeof(struct st_nci_spi_phy
),
250 spi_set_drvdata(dev
, phy
);
252 r
= devm_acpi_dev_add_driver_gpios(&dev
->dev
, acpi_st_nci_gpios
);
254 dev_dbg(&dev
->dev
, "Unable to add GPIO mapping table\n");
257 phy
->gpiod_reset
= devm_gpiod_get(&dev
->dev
, "reset", GPIOD_OUT_HIGH
);
258 if (IS_ERR(phy
->gpiod_reset
)) {
259 nfc_err(&dev
->dev
, "Unable to get RESET GPIO\n");
260 return PTR_ERR(phy
->gpiod_reset
);
263 phy
->se_status
.is_ese_present
=
264 device_property_read_bool(&dev
->dev
, "ese-present");
265 phy
->se_status
.is_uicc_present
=
266 device_property_read_bool(&dev
->dev
, "uicc-present");
268 r
= ndlc_probe(phy
, &spi_phy_ops
, &dev
->dev
,
269 ST_NCI_FRAME_HEADROOM
, ST_NCI_FRAME_TAILROOM
,
270 &phy
->ndlc
, &phy
->se_status
);
272 nfc_err(&dev
->dev
, "Unable to register ndlc layer\n");
276 phy
->irq_active
= true;
277 r
= devm_request_threaded_irq(&dev
->dev
, dev
->irq
, NULL
,
278 st_nci_irq_thread_fn
,
280 ST_NCI_SPI_DRIVER_NAME
, phy
);
282 nfc_err(&dev
->dev
, "Unable to register IRQ handler\n");
287 static int st_nci_spi_remove(struct spi_device
*dev
)
289 struct st_nci_spi_phy
*phy
= spi_get_drvdata(dev
);
291 dev_dbg(&dev
->dev
, "%s\n", __func__
);
293 ndlc_remove(phy
->ndlc
);
298 static struct spi_device_id st_nci_spi_id_table
[] = {
299 {ST_NCI_SPI_DRIVER_NAME
, 0},
302 MODULE_DEVICE_TABLE(spi
, st_nci_spi_id_table
);
304 static const struct acpi_device_id st_nci_spi_acpi_match
[] = {
308 MODULE_DEVICE_TABLE(acpi
, st_nci_spi_acpi_match
);
310 static const struct of_device_id of_st_nci_spi_match
[] = {
311 { .compatible
= "st,st21nfcb-spi", },
314 MODULE_DEVICE_TABLE(of
, of_st_nci_spi_match
);
316 static struct spi_driver st_nci_spi_driver
= {
318 .name
= ST_NCI_SPI_DRIVER_NAME
,
319 .of_match_table
= of_match_ptr(of_st_nci_spi_match
),
320 .acpi_match_table
= ACPI_PTR(st_nci_spi_acpi_match
),
322 .probe
= st_nci_spi_probe
,
323 .id_table
= st_nci_spi_id_table
,
324 .remove
= st_nci_spi_remove
,
326 module_spi_driver(st_nci_spi_driver
);
328 MODULE_LICENSE("GPL");
329 MODULE_DESCRIPTION(DRIVER_DESC
);