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.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/of_irq.h>
25 #include <linux/of_gpio.h>
26 #include <linux/acpi.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/nfc.h>
30 #include <net/nfc/nci.h>
31 #include <linux/platform_data/st-nci.h>
35 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
38 #define ST_NCI_FRAME_HEADROOM 1
39 #define ST_NCI_FRAME_TAILROOM 0
41 #define ST_NCI_SPI_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
42 #define ST_NCI_SPI_MAX_SIZE 250 /* req 4.2.1 */
44 #define ST_NCI_SPI_DRIVER_NAME "st_nci_spi"
46 #define ST_NCI_GPIO_NAME_RESET "reset"
48 struct st_nci_spi_phy
{
49 struct spi_device
*spi_dev
;
50 struct llt_ndlc
*ndlc
;
54 unsigned int gpio_reset
;
55 unsigned int irq_polarity
;
57 struct st_nci_se_status se_status
;
60 static int st_nci_spi_enable(void *phy_id
)
62 struct st_nci_spi_phy
*phy
= phy_id
;
64 gpio_set_value(phy
->gpio_reset
, 0);
65 usleep_range(10000, 15000);
66 gpio_set_value(phy
->gpio_reset
, 1);
67 usleep_range(80000, 85000);
69 if (phy
->ndlc
->powered
== 0 && phy
->irq_active
== 0) {
70 enable_irq(phy
->spi_dev
->irq
);
71 phy
->irq_active
= true;
77 static void st_nci_spi_disable(void *phy_id
)
79 struct st_nci_spi_phy
*phy
= phy_id
;
81 disable_irq_nosync(phy
->spi_dev
->irq
);
82 phy
->irq_active
= false;
86 * Writing a frame must not return the number of written bytes.
87 * It must return either zero for success, or <0 for error.
88 * In addition, it must not alter the skb
90 static int st_nci_spi_write(void *phy_id
, struct sk_buff
*skb
)
93 struct st_nci_spi_phy
*phy
= phy_id
;
94 struct spi_device
*dev
= phy
->spi_dev
;
95 struct sk_buff
*skb_rx
;
96 u8 buf
[ST_NCI_SPI_MAX_SIZE
+ NCI_DATA_HDR_SIZE
+
97 ST_NCI_FRAME_HEADROOM
+ ST_NCI_FRAME_TAILROOM
];
98 struct spi_transfer spi_xfer
= {
104 if (phy
->ndlc
->hard_fault
!= 0)
105 return phy
->ndlc
->hard_fault
;
107 r
= spi_sync_transfer(dev
, &spi_xfer
, 1);
109 * We may have received some valuable data on miso line.
110 * Send them back in the ndlc state machine.
113 skb_rx
= alloc_skb(skb
->len
, GFP_KERNEL
);
119 skb_put(skb_rx
, skb
->len
);
120 memcpy(skb_rx
->data
, buf
, skb
->len
);
121 ndlc_recv(phy
->ndlc
, skb_rx
);
129 * Reads an ndlc frame and returns it in a newly allocated sk_buff.
131 * 0 : if received frame is complete
132 * -EREMOTEIO : i2c read error (fatal)
133 * -EBADMSG : frame was incorrect and discarded
134 * -ENOMEM : cannot allocate skb, frame dropped
136 static int st_nci_spi_read(struct st_nci_spi_phy
*phy
,
137 struct sk_buff
**skb
)
141 u8 buf
[ST_NCI_SPI_MAX_SIZE
];
142 struct spi_device
*dev
= phy
->spi_dev
;
143 struct spi_transfer spi_xfer
= {
145 .len
= ST_NCI_SPI_MIN_SIZE
,
148 r
= spi_sync_transfer(dev
, &spi_xfer
, 1);
152 len
= be16_to_cpu(*(__be16
*) (buf
+ 2));
153 if (len
> ST_NCI_SPI_MAX_SIZE
) {
154 nfc_err(&dev
->dev
, "invalid frame len\n");
155 phy
->ndlc
->hard_fault
= 1;
159 *skb
= alloc_skb(ST_NCI_SPI_MIN_SIZE
+ len
, GFP_KERNEL
);
163 skb_reserve(*skb
, ST_NCI_SPI_MIN_SIZE
);
164 skb_put(*skb
, ST_NCI_SPI_MIN_SIZE
);
165 memcpy((*skb
)->data
, buf
, ST_NCI_SPI_MIN_SIZE
);
171 r
= spi_sync_transfer(dev
, &spi_xfer
, 1);
178 memcpy((*skb
)->data
+ ST_NCI_SPI_MIN_SIZE
, buf
, len
);
184 * Reads an ndlc frame from the chip.
186 * On ST21NFCB, IRQ goes in idle state when read starts.
188 static irqreturn_t
st_nci_irq_thread_fn(int irq
, void *phy_id
)
190 struct st_nci_spi_phy
*phy
= phy_id
;
191 struct spi_device
*dev
;
192 struct sk_buff
*skb
= NULL
;
195 if (!phy
|| !phy
->ndlc
|| irq
!= phy
->spi_dev
->irq
) {
201 dev_dbg(&dev
->dev
, "IRQ\n");
203 if (phy
->ndlc
->hard_fault
)
206 if (!phy
->ndlc
->powered
) {
207 st_nci_spi_disable(phy
);
211 r
= st_nci_spi_read(phy
, &skb
);
212 if (r
== -EREMOTEIO
|| r
== -ENOMEM
|| r
== -EBADMSG
)
215 ndlc_recv(phy
->ndlc
, skb
);
220 static struct nfc_phy_ops spi_phy_ops
= {
221 .write
= st_nci_spi_write
,
222 .enable
= st_nci_spi_enable
,
223 .disable
= st_nci_spi_disable
,
226 static int st_nci_spi_acpi_request_resources(struct spi_device
*spi_dev
)
228 struct st_nci_spi_phy
*phy
= spi_get_drvdata(spi_dev
);
229 struct gpio_desc
*gpiod_reset
;
230 struct device
*dev
= &spi_dev
->dev
;
233 /* Get RESET GPIO from ACPI */
234 gpiod_reset
= devm_gpiod_get_index(dev
, ST_NCI_GPIO_NAME_RESET
, 1,
236 if (IS_ERR(gpiod_reset
)) {
237 nfc_err(dev
, "Unable to get RESET GPIO\n");
241 phy
->gpio_reset
= desc_to_gpio(gpiod_reset
);
243 phy
->irq_polarity
= irq_get_trigger_type(spi_dev
->irq
);
245 phy
->se_status
.is_ese_present
= false;
246 phy
->se_status
.is_uicc_present
= false;
248 if (device_property_present(dev
, "ese-present")) {
249 device_property_read_u8(dev
, "ese-present", &tmp
);
250 tmp
= phy
->se_status
.is_ese_present
;
253 if (device_property_present(dev
, "uicc-present")) {
254 device_property_read_u8(dev
, "uicc-present", &tmp
);
255 tmp
= phy
->se_status
.is_uicc_present
;
261 static int st_nci_spi_of_request_resources(struct spi_device
*dev
)
263 struct st_nci_spi_phy
*phy
= spi_get_drvdata(dev
);
264 struct device_node
*pp
;
268 pp
= dev
->dev
.of_node
;
272 /* Get GPIO from device tree */
273 gpio
= of_get_named_gpio(pp
, "reset-gpios", 0);
276 "Failed to retrieve reset-gpios from device tree\n");
280 /* GPIO request and configuration */
281 r
= devm_gpio_request_one(&dev
->dev
, gpio
,
282 GPIOF_OUT_INIT_HIGH
, ST_NCI_GPIO_NAME_RESET
);
284 nfc_err(&dev
->dev
, "Failed to request reset pin\n");
287 phy
->gpio_reset
= gpio
;
289 phy
->irq_polarity
= irq_get_trigger_type(dev
->irq
);
291 phy
->se_status
.is_ese_present
=
292 of_property_read_bool(pp
, "ese-present");
293 phy
->se_status
.is_uicc_present
=
294 of_property_read_bool(pp
, "uicc-present");
299 static int st_nci_spi_request_resources(struct spi_device
*dev
)
301 struct st_nci_nfc_platform_data
*pdata
;
302 struct st_nci_spi_phy
*phy
= spi_get_drvdata(dev
);
305 pdata
= dev
->dev
.platform_data
;
307 nfc_err(&dev
->dev
, "No platform data\n");
311 /* store for later use */
312 phy
->gpio_reset
= pdata
->gpio_reset
;
313 phy
->irq_polarity
= pdata
->irq_polarity
;
315 r
= devm_gpio_request_one(&dev
->dev
,
316 phy
->gpio_reset
, GPIOF_OUT_INIT_HIGH
,
317 ST_NCI_GPIO_NAME_RESET
);
319 pr_err("%s : reset gpio_request failed\n", __FILE__
);
323 phy
->se_status
.is_ese_present
= pdata
->is_ese_present
;
324 phy
->se_status
.is_uicc_present
= pdata
->is_uicc_present
;
329 static int st_nci_spi_probe(struct spi_device
*dev
)
331 struct st_nci_spi_phy
*phy
;
332 struct st_nci_nfc_platform_data
*pdata
;
335 dev_dbg(&dev
->dev
, "%s\n", __func__
);
336 dev_dbg(&dev
->dev
, "IRQ: %d\n", dev
->irq
);
338 /* Check SPI platform functionnalities */
340 pr_debug("%s: dev is NULL. Device is not accessible.\n",
345 phy
= devm_kzalloc(&dev
->dev
, sizeof(struct st_nci_spi_phy
),
352 spi_set_drvdata(dev
, phy
);
354 pdata
= dev
->dev
.platform_data
;
355 if (!pdata
&& dev
->dev
.of_node
) {
356 r
= st_nci_spi_of_request_resources(dev
);
358 nfc_err(&dev
->dev
, "No platform data\n");
362 r
= st_nci_spi_request_resources(dev
);
365 "Cannot get platform resources\n");
368 } else if (ACPI_HANDLE(&dev
->dev
)) {
369 r
= st_nci_spi_acpi_request_resources(dev
);
371 nfc_err(&dev
->dev
, "Cannot get ACPI data\n");
376 "st_nci platform resources not available\n");
380 r
= ndlc_probe(phy
, &spi_phy_ops
, &dev
->dev
,
381 ST_NCI_FRAME_HEADROOM
, ST_NCI_FRAME_TAILROOM
,
382 &phy
->ndlc
, &phy
->se_status
);
384 nfc_err(&dev
->dev
, "Unable to register ndlc layer\n");
388 phy
->irq_active
= true;
389 r
= devm_request_threaded_irq(&dev
->dev
, dev
->irq
, NULL
,
390 st_nci_irq_thread_fn
,
391 phy
->irq_polarity
| IRQF_ONESHOT
,
392 ST_NCI_SPI_DRIVER_NAME
, phy
);
394 nfc_err(&dev
->dev
, "Unable to register IRQ handler\n");
399 static int st_nci_spi_remove(struct spi_device
*dev
)
401 struct st_nci_spi_phy
*phy
= spi_get_drvdata(dev
);
403 dev_dbg(&dev
->dev
, "%s\n", __func__
);
405 ndlc_remove(phy
->ndlc
);
410 static struct spi_device_id st_nci_spi_id_table
[] = {
411 {ST_NCI_SPI_DRIVER_NAME
, 0},
414 MODULE_DEVICE_TABLE(spi
, st_nci_spi_id_table
);
416 static const struct acpi_device_id st_nci_spi_acpi_match
[] = {
420 MODULE_DEVICE_TABLE(acpi
, st_nci_spi_acpi_match
);
422 static const struct of_device_id of_st_nci_spi_match
[] = {
423 { .compatible
= "st,st21nfcb-spi", },
426 MODULE_DEVICE_TABLE(of
, of_st_nci_spi_match
);
428 static struct spi_driver st_nci_spi_driver
= {
430 .name
= ST_NCI_SPI_DRIVER_NAME
,
431 .of_match_table
= of_match_ptr(of_st_nci_spi_match
),
432 .acpi_match_table
= ACPI_PTR(st_nci_spi_acpi_match
),
434 .probe
= st_nci_spi_probe
,
435 .id_table
= st_nci_spi_id_table
,
436 .remove
= st_nci_spi_remove
,
438 module_spi_driver(st_nci_spi_driver
);
440 MODULE_LICENSE("GPL");
441 MODULE_DESCRIPTION(DRIVER_DESC
);