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/crc-ccitt.h>
21 #include <linux/module.h>
22 #include <linux/i2c.h>
23 #include <linux/gpio.h>
24 #include <linux/of_irq.h>
25 #include <linux/of_gpio.h>
26 #include <linux/miscdevice.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/nfc.h>
30 #include <linux/firmware.h>
31 #include <linux/unaligned/access_ok.h>
32 #include <linux/platform_data/st21nfcb.h>
34 #include <net/nfc/nci.h>
35 #include <net/nfc/llc.h>
36 #include <net/nfc/nfc.h>
40 #define DRIVER_DESC "NCI NFC driver for ST21NFCB"
43 #define ST21NFCB_FRAME_HEADROOM 1
44 #define ST21NFCB_FRAME_TAILROOM 0
46 #define ST21NFCB_NCI_I2C_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
47 #define ST21NFCB_NCI_I2C_MAX_SIZE 250 /* req 4.2.1 */
49 #define ST21NFCB_NCI_I2C_DRIVER_NAME "st21nfcb_nci_i2c"
51 static struct i2c_device_id st21nfcb_nci_i2c_id_table[] = {
52 {ST21NFCB_NCI_DRIVER_NAME, 0},
55 MODULE_DEVICE_TABLE(i2c, st21nfcb_nci_i2c_id_table);
57 struct st21nfcb_i2c_phy {
58 struct i2c_client *i2c_dev;
59 struct llt_ndlc *ndlc;
61 unsigned int gpio_irq;
62 unsigned int gpio_reset;
63 unsigned int irq_polarity;
68 * < 0 if hardware error occured (e.g. i2c err)
69 * and prevents normal operation.
74 #define I2C_DUMP_SKB(info, skb) \
76 pr_debug("%s:\n", info); \
77 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
78 16, 1, (skb)->data, (skb)->len, 0); \
81 static int st21nfcb_nci_i2c_enable(void *phy_id)
83 struct st21nfcb_i2c_phy *phy = phy_id;
85 gpio_set_value(phy->gpio_reset, 0);
86 usleep_range(10000, 15000);
87 gpio_set_value(phy->gpio_reset, 1);
89 usleep_range(80000, 85000);
94 static void st21nfcb_nci_i2c_disable(void *phy_id)
96 struct st21nfcb_i2c_phy *phy = phy_id;
101 /* reset chip in order to flush clf */
102 gpio_set_value(phy->gpio_reset, 0);
103 usleep_range(10000, 15000);
104 gpio_set_value(phy->gpio_reset, 1);
107 static void st21nfcb_nci_remove_header(struct sk_buff *skb)
109 skb_pull(skb, ST21NFCB_FRAME_HEADROOM);
113 * Writing a frame must not return the number of written bytes.
114 * It must return either zero for success, or <0 for error.
115 * In addition, it must not alter the skb
117 static int st21nfcb_nci_i2c_write(void *phy_id, struct sk_buff *skb)
120 struct st21nfcb_i2c_phy *phy = phy_id;
121 struct i2c_client *client = phy->i2c_dev;
123 I2C_DUMP_SKB("st21nfcb_nci_i2c_write", skb);
125 if (phy->hard_fault != 0)
126 return phy->hard_fault;
128 r = i2c_master_send(client, skb->data, skb->len);
129 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
130 usleep_range(1000, 4000);
131 r = i2c_master_send(client, skb->data, skb->len);
141 st21nfcb_nci_remove_header(skb);
147 * Reads an ndlc frame and returns it in a newly allocated sk_buff.
149 * frame size : if received frame is complete (find ST21NFCB_SOF_EOF at
151 * -EAGAIN : if received frame is incomplete (not find ST21NFCB_SOF_EOF
153 * -EREMOTEIO : i2c read error (fatal)
154 * -EBADMSG : frame was incorrect and discarded
155 * (value returned from st21nfcb_nci_i2c_repack)
156 * -EIO : if no ST21NFCB_SOF_EOF is found after reaching
157 * the read length end sequence
159 static int st21nfcb_nci_i2c_read(struct st21nfcb_i2c_phy *phy,
160 struct sk_buff **skb)
164 u8 buf[ST21NFCB_NCI_I2C_MAX_SIZE];
165 struct i2c_client *client = phy->i2c_dev;
167 r = i2c_master_recv(client, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
168 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
169 usleep_range(1000, 4000);
170 r = i2c_master_recv(client, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
171 } else if (r != ST21NFCB_NCI_I2C_MIN_SIZE) {
172 nfc_err(&client->dev, "cannot read ndlc & nci header\n");
176 len = be16_to_cpu(*(__be16 *) (buf + 2));
177 if (len > ST21NFCB_NCI_I2C_MAX_SIZE) {
178 nfc_err(&client->dev, "invalid frame len\n");
182 *skb = alloc_skb(ST21NFCB_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
186 skb_reserve(*skb, ST21NFCB_NCI_I2C_MIN_SIZE);
187 skb_put(*skb, ST21NFCB_NCI_I2C_MIN_SIZE);
188 memcpy((*skb)->data, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
193 r = i2c_master_recv(client, buf, len);
200 memcpy((*skb)->data + ST21NFCB_NCI_I2C_MIN_SIZE, buf, len);
202 I2C_DUMP_SKB("i2c frame read", *skb);
208 * Reads an ndlc frame from the chip.
210 * On ST21NFCB, IRQ goes in idle state when read starts.
212 static irqreturn_t st21nfcb_nci_irq_thread_fn(int irq, void *phy_id)
214 struct st21nfcb_i2c_phy *phy = phy_id;
215 struct i2c_client *client;
216 struct sk_buff *skb = NULL;
219 if (!phy || irq != phy->i2c_dev->irq) {
224 client = phy->i2c_dev;
225 dev_dbg(&client->dev, "IRQ\n");
231 st21nfcb_nci_i2c_disable(phy);
235 r = st21nfcb_nci_i2c_read(phy, &skb);
236 if (r == -EREMOTEIO) {
238 ndlc_recv(phy->ndlc, NULL);
240 } else if (r == -ENOMEM || r == -EBADMSG) {
244 ndlc_recv(phy->ndlc, skb);
249 static struct nfc_phy_ops i2c_phy_ops = {
250 .write = st21nfcb_nci_i2c_write,
251 .enable = st21nfcb_nci_i2c_enable,
252 .disable = st21nfcb_nci_i2c_disable,
256 static int st21nfcb_nci_i2c_of_request_resources(struct i2c_client *client)
258 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
259 struct device_node *pp;
263 pp = client->dev.of_node;
267 /* Get GPIO from device tree */
268 gpio = of_get_named_gpio(pp, "reset-gpios", 0);
270 nfc_err(&client->dev,
271 "Failed to retrieve reset-gpios from device tree\n");
275 /* GPIO request and configuration */
276 r = devm_gpio_request(&client->dev, gpio, "clf_reset");
278 nfc_err(&client->dev, "Failed to request reset pin\n");
282 r = gpio_direction_output(gpio, 1);
284 nfc_err(&client->dev,
285 "Failed to set reset pin direction as output\n");
288 phy->gpio_reset = gpio;
291 r = irq_of_parse_and_map(pp, 0);
293 nfc_err(&client->dev,
294 "Unable to get irq, error: %d\n", r);
298 phy->irq_polarity = irq_get_trigger_type(r);
304 static int st21nfcb_nci_i2c_of_request_resources(struct i2c_client *client)
310 static int st21nfcb_nci_i2c_request_resources(struct i2c_client *client)
312 struct st21nfcb_nfc_platform_data *pdata;
313 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
317 pdata = client->dev.platform_data;
319 nfc_err(&client->dev, "No platform data\n");
323 /* store for later use */
324 phy->gpio_irq = pdata->gpio_irq;
325 phy->gpio_reset = pdata->gpio_reset;
326 phy->irq_polarity = pdata->irq_polarity;
328 r = devm_gpio_request(&client->dev, phy->gpio_irq, "wake_up");
330 pr_err("%s : gpio_request failed\n", __FILE__);
334 r = gpio_direction_input(phy->gpio_irq);
336 pr_err("%s : gpio_direction_input failed\n", __FILE__);
340 r = devm_gpio_request(&client->dev,
341 phy->gpio_reset, "clf_reset");
343 pr_err("%s : reset gpio_request failed\n", __FILE__);
347 r = gpio_direction_output(phy->gpio_reset, 1);
349 pr_err("%s : reset gpio_direction_output failed\n",
355 irq = gpio_to_irq(phy->gpio_irq);
357 nfc_err(&client->dev,
358 "Unable to get irq number for GPIO %d error %d\n",
367 static int st21nfcb_nci_i2c_probe(struct i2c_client *client,
368 const struct i2c_device_id *id)
370 struct st21nfcb_i2c_phy *phy;
371 struct st21nfcb_nfc_platform_data *pdata;
374 dev_dbg(&client->dev, "%s\n", __func__);
375 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
377 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
378 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
382 phy = devm_kzalloc(&client->dev, sizeof(struct st21nfcb_i2c_phy),
385 nfc_err(&client->dev,
386 "Cannot allocate memory for st21nfcb i2c phy.\n");
390 phy->i2c_dev = client;
392 i2c_set_clientdata(client, phy);
394 pdata = client->dev.platform_data;
395 if (!pdata && client->dev.of_node) {
396 r = st21nfcb_nci_i2c_of_request_resources(client);
398 nfc_err(&client->dev, "No platform data\n");
402 r = st21nfcb_nci_i2c_request_resources(client);
404 nfc_err(&client->dev,
405 "Cannot get platform resources\n");
409 nfc_err(&client->dev,
410 "st21nfcb platform resources not available\n");
414 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
415 st21nfcb_nci_irq_thread_fn,
416 phy->irq_polarity | IRQF_ONESHOT,
417 ST21NFCB_NCI_DRIVER_NAME, phy);
419 nfc_err(&client->dev, "Unable to register IRQ handler\n");
423 return ndlc_probe(phy, &i2c_phy_ops, &client->dev,
424 ST21NFCB_FRAME_HEADROOM, ST21NFCB_FRAME_TAILROOM,
428 static int st21nfcb_nci_i2c_remove(struct i2c_client *client)
430 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
432 dev_dbg(&client->dev, "%s\n", __func__);
434 ndlc_remove(phy->ndlc);
437 st21nfcb_nci_i2c_disable(phy);
442 static const struct of_device_id of_st21nfcb_i2c_match[] = {
443 { .compatible = "st,st21nfcb_i2c", },
447 static struct i2c_driver st21nfcb_nci_i2c_driver = {
449 .owner = THIS_MODULE,
450 .name = ST21NFCB_NCI_I2C_DRIVER_NAME,
451 .owner = THIS_MODULE,
452 .of_match_table = of_match_ptr(of_st21nfcb_i2c_match),
454 .probe = st21nfcb_nci_i2c_probe,
455 .id_table = st21nfcb_nci_i2c_id_table,
456 .remove = st21nfcb_nci_i2c_remove,
459 module_i2c_driver(st21nfcb_nci_i2c_driver);
461 MODULE_LICENSE("GPL");
462 MODULE_DESCRIPTION(DRIVER_DESC);