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 "clf_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 const struct acpi_device_id
*id
;
230 struct gpio_desc
*gpiod_reset
;
238 /* Match the struct device against a given list of ACPI IDs */
239 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
243 /* Get RESET GPIO from ACPI */
244 gpiod_reset
= devm_gpiod_get_index(dev
, ST_NCI_GPIO_NAME_RESET
, 1,
246 if (IS_ERR(gpiod_reset
)) {
247 nfc_err(dev
, "Unable to get RESET GPIO\n");
251 phy
->gpio_reset
= desc_to_gpio(gpiod_reset
);
253 phy
->irq_polarity
= irq_get_trigger_type(spi_dev
->irq
);
255 phy
->se_status
.is_ese_present
=
256 device_property_present(dev
, "ese-present");
257 phy
->se_status
.is_uicc_present
=
258 device_property_present(dev
, "uicc-present");
263 static int st_nci_spi_of_request_resources(struct spi_device
*dev
)
265 struct st_nci_spi_phy
*phy
= spi_get_drvdata(dev
);
266 struct device_node
*pp
;
270 pp
= dev
->dev
.of_node
;
274 /* Get GPIO from device tree */
275 gpio
= of_get_named_gpio(pp
, "reset-gpios", 0);
278 "Failed to retrieve reset-gpios from device tree\n");
282 /* GPIO request and configuration */
283 r
= devm_gpio_request_one(&dev
->dev
, gpio
,
284 GPIOF_OUT_INIT_HIGH
, ST_NCI_GPIO_NAME_RESET
);
286 nfc_err(&dev
->dev
, "Failed to request reset pin\n");
289 phy
->gpio_reset
= gpio
;
291 phy
->irq_polarity
= irq_get_trigger_type(dev
->irq
);
293 phy
->se_status
.is_ese_present
=
294 of_property_read_bool(pp
, "ese-present");
295 phy
->se_status
.is_uicc_present
=
296 of_property_read_bool(pp
, "uicc-present");
301 static int st_nci_spi_request_resources(struct spi_device
*dev
)
303 struct st_nci_nfc_platform_data
*pdata
;
304 struct st_nci_spi_phy
*phy
= spi_get_drvdata(dev
);
307 pdata
= dev
->dev
.platform_data
;
309 nfc_err(&dev
->dev
, "No platform data\n");
313 /* store for later use */
314 phy
->gpio_reset
= pdata
->gpio_reset
;
315 phy
->irq_polarity
= pdata
->irq_polarity
;
317 r
= devm_gpio_request_one(&dev
->dev
,
318 phy
->gpio_reset
, GPIOF_OUT_INIT_HIGH
,
319 ST_NCI_GPIO_NAME_RESET
);
321 pr_err("%s : reset gpio_request failed\n", __FILE__
);
325 phy
->se_status
.is_ese_present
= pdata
->is_ese_present
;
326 phy
->se_status
.is_uicc_present
= pdata
->is_uicc_present
;
331 static int st_nci_spi_probe(struct spi_device
*dev
)
333 struct st_nci_spi_phy
*phy
;
334 struct st_nci_nfc_platform_data
*pdata
;
337 dev_dbg(&dev
->dev
, "%s\n", __func__
);
338 dev_dbg(&dev
->dev
, "IRQ: %d\n", dev
->irq
);
340 /* Check SPI platform functionnalities */
342 pr_debug("%s: dev is NULL. Device is not accessible.\n",
347 phy
= devm_kzalloc(&dev
->dev
, sizeof(struct st_nci_spi_phy
),
354 spi_set_drvdata(dev
, phy
);
356 pdata
= dev
->dev
.platform_data
;
357 if (!pdata
&& dev
->dev
.of_node
) {
358 r
= st_nci_spi_of_request_resources(dev
);
360 nfc_err(&dev
->dev
, "No platform data\n");
364 r
= st_nci_spi_request_resources(dev
);
367 "Cannot get platform resources\n");
370 } else if (ACPI_HANDLE(&dev
->dev
)) {
371 r
= st_nci_spi_acpi_request_resources(dev
);
373 nfc_err(&dev
->dev
, "Cannot get ACPI data\n");
378 "st_nci platform resources not available\n");
382 r
= ndlc_probe(phy
, &spi_phy_ops
, &dev
->dev
,
383 ST_NCI_FRAME_HEADROOM
, ST_NCI_FRAME_TAILROOM
,
384 &phy
->ndlc
, &phy
->se_status
);
386 nfc_err(&dev
->dev
, "Unable to register ndlc layer\n");
390 phy
->irq_active
= true;
391 r
= devm_request_threaded_irq(&dev
->dev
, dev
->irq
, NULL
,
392 st_nci_irq_thread_fn
,
393 phy
->irq_polarity
| IRQF_ONESHOT
,
394 ST_NCI_SPI_DRIVER_NAME
, phy
);
396 nfc_err(&dev
->dev
, "Unable to register IRQ handler\n");
401 static int st_nci_spi_remove(struct spi_device
*dev
)
403 struct st_nci_spi_phy
*phy
= spi_get_drvdata(dev
);
405 dev_dbg(&dev
->dev
, "%s\n", __func__
);
407 ndlc_remove(phy
->ndlc
);
412 static struct spi_device_id st_nci_spi_id_table
[] = {
413 {ST_NCI_SPI_DRIVER_NAME
, 0},
416 MODULE_DEVICE_TABLE(spi
, st_nci_spi_id_table
);
418 static const struct acpi_device_id st_nci_spi_acpi_match
[] = {
422 MODULE_DEVICE_TABLE(acpi
, st_nci_spi_acpi_match
);
424 static const struct of_device_id of_st_nci_spi_match
[] = {
425 { .compatible
= "st,st21nfcb-spi", },
428 MODULE_DEVICE_TABLE(of
, of_st_nci_spi_match
);
430 static struct spi_driver st_nci_spi_driver
= {
432 .name
= ST_NCI_SPI_DRIVER_NAME
,
433 .of_match_table
= of_match_ptr(of_st_nci_spi_match
),
434 .acpi_match_table
= ACPI_PTR(st_nci_spi_acpi_match
),
436 .probe
= st_nci_spi_probe
,
437 .id_table
= st_nci_spi_id_table
,
438 .remove
= st_nci_spi_remove
,
440 module_spi_driver(st_nci_spi_driver
);
442 MODULE_LICENSE("GPL");
443 MODULE_DESCRIPTION(DRIVER_DESC
);