2 * I2C Link Layer for ST21NFCB NCI based Driver
3 * Copyright (C) 2014 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/i2c.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 <linux/platform_data/st21nfcb.h>
32 #define DRIVER_DESC "NCI NFC driver for ST21NFCB"
35 #define ST21NFCB_FRAME_HEADROOM 1
36 #define ST21NFCB_FRAME_TAILROOM 0
38 #define ST21NFCB_NCI_I2C_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
39 #define ST21NFCB_NCI_I2C_MAX_SIZE 250 /* req 4.2.1 */
41 #define ST21NFCB_NCI_I2C_DRIVER_NAME "st21nfcb_nci_i2c"
43 static struct i2c_device_id st21nfcb_nci_i2c_id_table
[] = {
44 {ST21NFCB_NCI_DRIVER_NAME
, 0},
47 MODULE_DEVICE_TABLE(i2c
, st21nfcb_nci_i2c_id_table
);
49 struct st21nfcb_i2c_phy
{
50 struct i2c_client
*i2c_dev
;
51 struct llt_ndlc
*ndlc
;
53 unsigned int gpio_reset
;
54 unsigned int irq_polarity
;
59 #define I2C_DUMP_SKB(info, skb) \
61 pr_debug("%s:\n", info); \
62 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
63 16, 1, (skb)->data, (skb)->len, 0); \
66 static int st21nfcb_nci_i2c_enable(void *phy_id
)
68 struct st21nfcb_i2c_phy
*phy
= phy_id
;
70 gpio_set_value(phy
->gpio_reset
, 0);
71 usleep_range(10000, 15000);
72 gpio_set_value(phy
->gpio_reset
, 1);
74 usleep_range(80000, 85000);
79 static void st21nfcb_nci_i2c_disable(void *phy_id
)
81 struct st21nfcb_i2c_phy
*phy
= phy_id
;
84 /* reset chip in order to flush clf */
85 gpio_set_value(phy
->gpio_reset
, 0);
86 usleep_range(10000, 15000);
87 gpio_set_value(phy
->gpio_reset
, 1);
90 static void st21nfcb_nci_remove_header(struct sk_buff
*skb
)
92 skb_pull(skb
, ST21NFCB_FRAME_HEADROOM
);
96 * Writing a frame must not return the number of written bytes.
97 * It must return either zero for success, or <0 for error.
98 * In addition, it must not alter the skb
100 static int st21nfcb_nci_i2c_write(void *phy_id
, struct sk_buff
*skb
)
103 struct st21nfcb_i2c_phy
*phy
= phy_id
;
104 struct i2c_client
*client
= phy
->i2c_dev
;
106 I2C_DUMP_SKB("st21nfcb_nci_i2c_write", skb
);
108 if (phy
->ndlc
->hard_fault
!= 0)
109 return phy
->ndlc
->hard_fault
;
111 r
= i2c_master_send(client
, skb
->data
, skb
->len
);
112 if (r
== -EREMOTEIO
) { /* Retry, chip was in standby */
113 usleep_range(1000, 4000);
114 r
= i2c_master_send(client
, skb
->data
, skb
->len
);
124 st21nfcb_nci_remove_header(skb
);
130 * Reads an ndlc frame and returns it in a newly allocated sk_buff.
132 * frame size : if received frame is complete (find ST21NFCB_SOF_EOF at
134 * -EAGAIN : if received frame is incomplete (not find ST21NFCB_SOF_EOF
136 * -EREMOTEIO : i2c read error (fatal)
137 * -EBADMSG : frame was incorrect and discarded
138 * (value returned from st21nfcb_nci_i2c_repack)
139 * -EIO : if no ST21NFCB_SOF_EOF is found after reaching
140 * the read length end sequence
142 static int st21nfcb_nci_i2c_read(struct st21nfcb_i2c_phy
*phy
,
143 struct sk_buff
**skb
)
147 u8 buf
[ST21NFCB_NCI_I2C_MAX_SIZE
];
148 struct i2c_client
*client
= phy
->i2c_dev
;
150 r
= i2c_master_recv(client
, buf
, ST21NFCB_NCI_I2C_MIN_SIZE
);
151 if (r
== -EREMOTEIO
) { /* Retry, chip was in standby */
152 usleep_range(1000, 4000);
153 r
= i2c_master_recv(client
, buf
, ST21NFCB_NCI_I2C_MIN_SIZE
);
156 if (r
!= ST21NFCB_NCI_I2C_MIN_SIZE
)
159 len
= be16_to_cpu(*(__be16
*) (buf
+ 2));
160 if (len
> ST21NFCB_NCI_I2C_MAX_SIZE
) {
161 nfc_err(&client
->dev
, "invalid frame len\n");
165 *skb
= alloc_skb(ST21NFCB_NCI_I2C_MIN_SIZE
+ len
, GFP_KERNEL
);
169 skb_reserve(*skb
, ST21NFCB_NCI_I2C_MIN_SIZE
);
170 skb_put(*skb
, ST21NFCB_NCI_I2C_MIN_SIZE
);
171 memcpy((*skb
)->data
, buf
, ST21NFCB_NCI_I2C_MIN_SIZE
);
176 r
= i2c_master_recv(client
, buf
, len
);
183 memcpy((*skb
)->data
+ ST21NFCB_NCI_I2C_MIN_SIZE
, buf
, len
);
185 I2C_DUMP_SKB("i2c frame read", *skb
);
191 * Reads an ndlc frame from the chip.
193 * On ST21NFCB, IRQ goes in idle state when read starts.
195 static irqreturn_t
st21nfcb_nci_irq_thread_fn(int irq
, void *phy_id
)
197 struct st21nfcb_i2c_phy
*phy
= phy_id
;
198 struct i2c_client
*client
;
199 struct sk_buff
*skb
= NULL
;
202 if (!phy
|| !phy
->ndlc
|| irq
!= phy
->i2c_dev
->irq
) {
207 client
= phy
->i2c_dev
;
208 dev_dbg(&client
->dev
, "IRQ\n");
210 if (phy
->ndlc
->hard_fault
)
214 st21nfcb_nci_i2c_disable(phy
);
218 r
= st21nfcb_nci_i2c_read(phy
, &skb
);
219 if (r
== -EREMOTEIO
|| r
== -ENOMEM
|| r
== -EBADMSG
)
222 ndlc_recv(phy
->ndlc
, skb
);
227 static struct nfc_phy_ops i2c_phy_ops
= {
228 .write
= st21nfcb_nci_i2c_write
,
229 .enable
= st21nfcb_nci_i2c_enable
,
230 .disable
= st21nfcb_nci_i2c_disable
,
234 static int st21nfcb_nci_i2c_of_request_resources(struct i2c_client
*client
)
236 struct st21nfcb_i2c_phy
*phy
= i2c_get_clientdata(client
);
237 struct device_node
*pp
;
241 pp
= client
->dev
.of_node
;
245 /* Get GPIO from device tree */
246 gpio
= of_get_named_gpio(pp
, "reset-gpios", 0);
248 nfc_err(&client
->dev
,
249 "Failed to retrieve reset-gpios from device tree\n");
253 /* GPIO request and configuration */
254 r
= devm_gpio_request_one(&client
->dev
, gpio
,
255 GPIOF_OUT_INIT_HIGH
, "clf_reset");
257 nfc_err(&client
->dev
, "Failed to request reset pin\n");
260 phy
->gpio_reset
= gpio
;
262 phy
->irq_polarity
= irq_get_trigger_type(client
->irq
);
267 static int st21nfcb_nci_i2c_of_request_resources(struct i2c_client
*client
)
273 static int st21nfcb_nci_i2c_request_resources(struct i2c_client
*client
)
275 struct st21nfcb_nfc_platform_data
*pdata
;
276 struct st21nfcb_i2c_phy
*phy
= i2c_get_clientdata(client
);
279 pdata
= client
->dev
.platform_data
;
281 nfc_err(&client
->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(&client
->dev
,
290 phy
->gpio_reset
, GPIOF_OUT_INIT_HIGH
, "clf_reset");
292 pr_err("%s : reset gpio_request failed\n", __FILE__
);
299 static int st21nfcb_nci_i2c_probe(struct i2c_client
*client
,
300 const struct i2c_device_id
*id
)
302 struct st21nfcb_i2c_phy
*phy
;
303 struct st21nfcb_nfc_platform_data
*pdata
;
306 dev_dbg(&client
->dev
, "%s\n", __func__
);
307 dev_dbg(&client
->dev
, "IRQ: %d\n", client
->irq
);
309 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
310 nfc_err(&client
->dev
, "Need I2C_FUNC_I2C\n");
314 phy
= devm_kzalloc(&client
->dev
, sizeof(struct st21nfcb_i2c_phy
),
317 nfc_err(&client
->dev
,
318 "Cannot allocate memory for st21nfcb i2c phy.\n");
322 phy
->i2c_dev
= client
;
324 i2c_set_clientdata(client
, phy
);
326 pdata
= client
->dev
.platform_data
;
327 if (!pdata
&& client
->dev
.of_node
) {
328 r
= st21nfcb_nci_i2c_of_request_resources(client
);
330 nfc_err(&client
->dev
, "No platform data\n");
334 r
= st21nfcb_nci_i2c_request_resources(client
);
336 nfc_err(&client
->dev
,
337 "Cannot get platform resources\n");
341 nfc_err(&client
->dev
,
342 "st21nfcb platform resources not available\n");
346 r
= ndlc_probe(phy
, &i2c_phy_ops
, &client
->dev
,
347 ST21NFCB_FRAME_HEADROOM
, ST21NFCB_FRAME_TAILROOM
,
350 nfc_err(&client
->dev
, "Unable to register ndlc layer\n");
354 r
= devm_request_threaded_irq(&client
->dev
, client
->irq
, NULL
,
355 st21nfcb_nci_irq_thread_fn
,
356 phy
->irq_polarity
| IRQF_ONESHOT
,
357 ST21NFCB_NCI_DRIVER_NAME
, phy
);
359 nfc_err(&client
->dev
, "Unable to register IRQ handler\n");
364 static int st21nfcb_nci_i2c_remove(struct i2c_client
*client
)
366 struct st21nfcb_i2c_phy
*phy
= i2c_get_clientdata(client
);
368 dev_dbg(&client
->dev
, "%s\n", __func__
);
370 ndlc_remove(phy
->ndlc
);
373 st21nfcb_nci_i2c_disable(phy
);
379 static const struct of_device_id of_st21nfcb_i2c_match
[] = {
380 { .compatible
= "st,st21nfcb-i2c", },
381 { .compatible
= "st,st21nfcb_i2c", },
384 MODULE_DEVICE_TABLE(of
, of_st21nfcb_i2c_match
);
387 static struct i2c_driver st21nfcb_nci_i2c_driver
= {
389 .owner
= THIS_MODULE
,
390 .name
= ST21NFCB_NCI_I2C_DRIVER_NAME
,
391 .of_match_table
= of_match_ptr(of_st21nfcb_i2c_match
),
393 .probe
= st21nfcb_nci_i2c_probe
,
394 .id_table
= st21nfcb_nci_i2c_id_table
,
395 .remove
= st21nfcb_nci_i2c_remove
,
398 module_i2c_driver(st21nfcb_nci_i2c_driver
);
400 MODULE_LICENSE("GPL");
401 MODULE_DESCRIPTION(DRIVER_DESC
);