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/of_irq.h>
24 #include <linux/of_gpio.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <linux/nfc.h>
28 #include <net/nfc/nci.h>
29 #include <linux/platform_data/st-nci.h>
33 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
36 #define ST_NCI_FRAME_HEADROOM 1
37 #define ST_NCI_FRAME_TAILROOM 0
39 #define ST_NCI_SPI_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
40 #define ST_NCI_SPI_MAX_SIZE 250 /* req 4.2.1 */
42 #define ST_NCI_SPI_DRIVER_NAME "st_nci_spi"
44 static struct spi_device_id st_nci_spi_id_table
[] = {
45 {ST_NCI_SPI_DRIVER_NAME
, 0},
48 MODULE_DEVICE_TABLE(spi
, st_nci_spi_id_table
);
50 struct st_nci_spi_phy
{
51 struct spi_device
*spi_dev
;
52 struct llt_ndlc
*ndlc
;
56 unsigned int gpio_reset
;
57 unsigned int irq_polarity
;
59 struct st_nci_se_status se_status
;
62 static int st_nci_spi_enable(void *phy_id
)
64 struct st_nci_spi_phy
*phy
= phy_id
;
66 gpio_set_value(phy
->gpio_reset
, 0);
67 usleep_range(10000, 15000);
68 gpio_set_value(phy
->gpio_reset
, 1);
69 usleep_range(80000, 85000);
71 if (phy
->ndlc
->powered
== 0 && phy
->irq_active
== 0) {
72 enable_irq(phy
->spi_dev
->irq
);
73 phy
->irq_active
= true;
79 static void st_nci_spi_disable(void *phy_id
)
81 struct st_nci_spi_phy
*phy
= phy_id
;
83 disable_irq_nosync(phy
->spi_dev
->irq
);
84 phy
->irq_active
= false;
88 * Writing a frame must not return the number of written bytes.
89 * It must return either zero for success, or <0 for error.
90 * In addition, it must not alter the skb
92 static int st_nci_spi_write(void *phy_id
, struct sk_buff
*skb
)
95 struct st_nci_spi_phy
*phy
= phy_id
;
96 struct spi_device
*dev
= phy
->spi_dev
;
97 struct sk_buff
*skb_rx
;
98 u8 buf
[ST_NCI_SPI_MAX_SIZE
+ NCI_DATA_HDR_SIZE
+
99 ST_NCI_FRAME_HEADROOM
+ ST_NCI_FRAME_TAILROOM
];
100 struct spi_transfer spi_xfer
= {
106 if (phy
->ndlc
->hard_fault
!= 0)
107 return phy
->ndlc
->hard_fault
;
109 r
= spi_sync_transfer(dev
, &spi_xfer
, 1);
111 * We may have received some valuable data on miso line.
112 * Send them back in the ndlc state machine.
115 skb_rx
= alloc_skb(skb
->len
, GFP_KERNEL
);
121 skb_put(skb_rx
, skb
->len
);
122 memcpy(skb_rx
->data
, buf
, skb
->len
);
123 ndlc_recv(phy
->ndlc
, skb_rx
);
131 * Reads an ndlc frame and returns it in a newly allocated sk_buff.
133 * 0 : if received frame is complete
134 * -EREMOTEIO : i2c read error (fatal)
135 * -EBADMSG : frame was incorrect and discarded
136 * -ENOMEM : cannot allocate skb, frame dropped
138 static int st_nci_spi_read(struct st_nci_spi_phy
*phy
,
139 struct sk_buff
**skb
)
143 u8 buf
[ST_NCI_SPI_MAX_SIZE
];
144 struct spi_device
*dev
= phy
->spi_dev
;
145 struct spi_transfer spi_xfer
= {
147 .len
= ST_NCI_SPI_MIN_SIZE
,
150 r
= spi_sync_transfer(dev
, &spi_xfer
, 1);
154 len
= be16_to_cpu(*(__be16
*) (buf
+ 2));
155 if (len
> ST_NCI_SPI_MAX_SIZE
) {
156 nfc_err(&dev
->dev
, "invalid frame len\n");
157 phy
->ndlc
->hard_fault
= 1;
161 *skb
= alloc_skb(ST_NCI_SPI_MIN_SIZE
+ len
, GFP_KERNEL
);
165 skb_reserve(*skb
, ST_NCI_SPI_MIN_SIZE
);
166 skb_put(*skb
, ST_NCI_SPI_MIN_SIZE
);
167 memcpy((*skb
)->data
, buf
, ST_NCI_SPI_MIN_SIZE
);
173 r
= spi_sync_transfer(dev
, &spi_xfer
, 1);
180 memcpy((*skb
)->data
+ ST_NCI_SPI_MIN_SIZE
, buf
, len
);
186 * Reads an ndlc frame from the chip.
188 * On ST21NFCB, IRQ goes in idle state when read starts.
190 static irqreturn_t
st_nci_irq_thread_fn(int irq
, void *phy_id
)
192 struct st_nci_spi_phy
*phy
= phy_id
;
193 struct spi_device
*dev
;
194 struct sk_buff
*skb
= NULL
;
197 if (!phy
|| !phy
->ndlc
|| irq
!= phy
->spi_dev
->irq
) {
203 dev_dbg(&dev
->dev
, "IRQ\n");
205 if (phy
->ndlc
->hard_fault
)
208 if (!phy
->ndlc
->powered
) {
209 st_nci_spi_disable(phy
);
213 r
= st_nci_spi_read(phy
, &skb
);
214 if (r
== -EREMOTEIO
|| r
== -ENOMEM
|| r
== -EBADMSG
)
217 ndlc_recv(phy
->ndlc
, skb
);
222 static struct nfc_phy_ops spi_phy_ops
= {
223 .write
= st_nci_spi_write
,
224 .enable
= st_nci_spi_enable
,
225 .disable
= st_nci_spi_disable
,
229 static int st_nci_spi_of_request_resources(struct spi_device
*dev
)
231 struct st_nci_spi_phy
*phy
= spi_get_drvdata(dev
);
232 struct device_node
*pp
;
236 pp
= dev
->dev
.of_node
;
240 /* Get GPIO from device tree */
241 gpio
= of_get_named_gpio(pp
, "reset-gpios", 0);
244 "Failed to retrieve reset-gpios from device tree\n");
248 /* GPIO request and configuration */
249 r
= devm_gpio_request_one(&dev
->dev
, gpio
,
250 GPIOF_OUT_INIT_HIGH
, "clf_reset");
252 nfc_err(&dev
->dev
, "Failed to request reset pin\n");
255 phy
->gpio_reset
= gpio
;
257 phy
->irq_polarity
= irq_get_trigger_type(dev
->irq
);
259 phy
->se_status
.is_ese_present
=
260 of_property_read_bool(pp
, "ese-present");
261 phy
->se_status
.is_uicc_present
=
262 of_property_read_bool(pp
, "uicc-present");
267 static int st_nci_spi_of_request_resources(struct spi_device
*dev
)
273 static int st_nci_spi_request_resources(struct spi_device
*dev
)
275 struct st_nci_nfc_platform_data
*pdata
;
276 struct st_nci_spi_phy
*phy
= spi_get_drvdata(dev
);
279 pdata
= dev
->dev
.platform_data
;
281 nfc_err(&dev
->dev
, "No platform data\n");
285 /* store for later use */
286 phy
->gpio_reset
= pdata
->gpio_reset
;
287 phy
->irq_polarity
= pdata
->irq_polarity
;
289 r
= devm_gpio_request_one(&dev
->dev
,
290 phy
->gpio_reset
, GPIOF_OUT_INIT_HIGH
, "clf_reset");
292 pr_err("%s : reset gpio_request failed\n", __FILE__
);
296 phy
->se_status
.is_ese_present
= pdata
->is_ese_present
;
297 phy
->se_status
.is_uicc_present
= pdata
->is_uicc_present
;
302 static int st_nci_spi_probe(struct spi_device
*dev
)
304 struct st_nci_spi_phy
*phy
;
305 struct st_nci_nfc_platform_data
*pdata
;
308 dev_dbg(&dev
->dev
, "%s\n", __func__
);
309 dev_dbg(&dev
->dev
, "IRQ: %d\n", dev
->irq
);
311 /* Check SPI platform functionnalities */
313 pr_debug("%s: dev is NULL. Device is not accessible.\n",
318 phy
= devm_kzalloc(&dev
->dev
, sizeof(struct st_nci_spi_phy
),
325 spi_set_drvdata(dev
, phy
);
327 pdata
= dev
->dev
.platform_data
;
328 if (!pdata
&& dev
->dev
.of_node
) {
329 r
= st_nci_spi_of_request_resources(dev
);
331 nfc_err(&dev
->dev
, "No platform data\n");
335 r
= st_nci_spi_request_resources(dev
);
338 "Cannot get platform resources\n");
343 "st_nci platform resources not available\n");
347 r
= ndlc_probe(phy
, &spi_phy_ops
, &dev
->dev
,
348 ST_NCI_FRAME_HEADROOM
, ST_NCI_FRAME_TAILROOM
,
349 &phy
->ndlc
, &phy
->se_status
);
351 nfc_err(&dev
->dev
, "Unable to register ndlc layer\n");
355 phy
->irq_active
= true;
356 r
= devm_request_threaded_irq(&dev
->dev
, dev
->irq
, NULL
,
357 st_nci_irq_thread_fn
,
358 phy
->irq_polarity
| IRQF_ONESHOT
,
359 ST_NCI_SPI_DRIVER_NAME
, phy
);
361 nfc_err(&dev
->dev
, "Unable to register IRQ handler\n");
366 static int st_nci_spi_remove(struct spi_device
*dev
)
368 struct st_nci_spi_phy
*phy
= spi_get_drvdata(dev
);
370 dev_dbg(&dev
->dev
, "%s\n", __func__
);
372 ndlc_remove(phy
->ndlc
);
378 static const struct of_device_id of_st_nci_spi_match
[] = {
379 { .compatible
= "st,st21nfcb-spi", },
382 MODULE_DEVICE_TABLE(of
, of_st_nci_spi_match
);
385 static struct spi_driver st_nci_spi_driver
= {
387 .name
= ST_NCI_SPI_DRIVER_NAME
,
388 .of_match_table
= of_match_ptr(of_st_nci_spi_match
),
390 .probe
= st_nci_spi_probe
,
391 .id_table
= st_nci_spi_id_table
,
392 .remove
= st_nci_spi_remove
,
395 module_spi_driver(st_nci_spi_driver
);
397 MODULE_LICENSE("GPL");
398 MODULE_DESCRIPTION(DRIVER_DESC
);