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_irq;
54 unsigned int gpio_reset;
55 unsigned int irq_polarity;
60 #define I2C_DUMP_SKB(info, skb) \
62 pr_debug("%s:\n", info); \
63 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
64 16, 1, (skb)->data, (skb)->len, 0); \
67 static int st21nfcb_nci_i2c_enable(void *phy_id)
69 struct st21nfcb_i2c_phy *phy = phy_id;
71 gpio_set_value(phy->gpio_reset, 0);
72 usleep_range(10000, 15000);
73 gpio_set_value(phy->gpio_reset, 1);
75 usleep_range(80000, 85000);
80 static void st21nfcb_nci_i2c_disable(void *phy_id)
82 struct st21nfcb_i2c_phy *phy = phy_id;
87 /* reset chip in order to flush clf */
88 gpio_set_value(phy->gpio_reset, 0);
89 usleep_range(10000, 15000);
90 gpio_set_value(phy->gpio_reset, 1);
93 static void st21nfcb_nci_remove_header(struct sk_buff *skb)
95 skb_pull(skb, ST21NFCB_FRAME_HEADROOM);
99 * Writing a frame must not return the number of written bytes.
100 * It must return either zero for success, or <0 for error.
101 * In addition, it must not alter the skb
103 static int st21nfcb_nci_i2c_write(void *phy_id, struct sk_buff *skb)
106 struct st21nfcb_i2c_phy *phy = phy_id;
107 struct i2c_client *client = phy->i2c_dev;
109 I2C_DUMP_SKB("st21nfcb_nci_i2c_write", skb);
111 if (phy->ndlc->hard_fault != 0)
112 return phy->ndlc->hard_fault;
114 r = i2c_master_send(client, skb->data, skb->len);
115 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
116 usleep_range(1000, 4000);
117 r = i2c_master_send(client, skb->data, skb->len);
127 st21nfcb_nci_remove_header(skb);
133 * Reads an ndlc frame and returns it in a newly allocated sk_buff.
135 * frame size : if received frame is complete (find ST21NFCB_SOF_EOF at
137 * -EAGAIN : if received frame is incomplete (not find ST21NFCB_SOF_EOF
139 * -EREMOTEIO : i2c read error (fatal)
140 * -EBADMSG : frame was incorrect and discarded
141 * (value returned from st21nfcb_nci_i2c_repack)
142 * -EIO : if no ST21NFCB_SOF_EOF is found after reaching
143 * the read length end sequence
145 static int st21nfcb_nci_i2c_read(struct st21nfcb_i2c_phy *phy,
146 struct sk_buff **skb)
150 u8 buf[ST21NFCB_NCI_I2C_MAX_SIZE];
151 struct i2c_client *client = phy->i2c_dev;
153 r = i2c_master_recv(client, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
154 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
155 usleep_range(1000, 4000);
156 r = i2c_master_recv(client, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
159 if (r != ST21NFCB_NCI_I2C_MIN_SIZE)
162 len = be16_to_cpu(*(__be16 *) (buf + 2));
163 if (len > ST21NFCB_NCI_I2C_MAX_SIZE) {
164 nfc_err(&client->dev, "invalid frame len\n");
168 *skb = alloc_skb(ST21NFCB_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
172 skb_reserve(*skb, ST21NFCB_NCI_I2C_MIN_SIZE);
173 skb_put(*skb, ST21NFCB_NCI_I2C_MIN_SIZE);
174 memcpy((*skb)->data, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
179 r = i2c_master_recv(client, buf, len);
186 memcpy((*skb)->data + ST21NFCB_NCI_I2C_MIN_SIZE, buf, len);
188 I2C_DUMP_SKB("i2c frame read", *skb);
194 * Reads an ndlc frame from the chip.
196 * On ST21NFCB, IRQ goes in idle state when read starts.
198 static irqreturn_t st21nfcb_nci_irq_thread_fn(int irq, void *phy_id)
200 struct st21nfcb_i2c_phy *phy = phy_id;
201 struct i2c_client *client;
202 struct sk_buff *skb = NULL;
205 if (!phy || irq != phy->i2c_dev->irq) {
210 client = phy->i2c_dev;
211 dev_dbg(&client->dev, "IRQ\n");
213 if (phy->ndlc->hard_fault)
217 st21nfcb_nci_i2c_disable(phy);
221 r = st21nfcb_nci_i2c_read(phy, &skb);
222 if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
225 ndlc_recv(phy->ndlc, skb);
230 static struct nfc_phy_ops i2c_phy_ops = {
231 .write = st21nfcb_nci_i2c_write,
232 .enable = st21nfcb_nci_i2c_enable,
233 .disable = st21nfcb_nci_i2c_disable,
237 static int st21nfcb_nci_i2c_of_request_resources(struct i2c_client *client)
239 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
240 struct device_node *pp;
244 pp = client->dev.of_node;
248 /* Get GPIO from device tree */
249 gpio = of_get_named_gpio(pp, "reset-gpios", 0);
251 nfc_err(&client->dev,
252 "Failed to retrieve reset-gpios from device tree\n");
256 /* GPIO request and configuration */
257 r = devm_gpio_request_one(&client->dev, gpio,
258 GPIOF_OUT_INIT_HIGH, "clf_reset");
260 nfc_err(&client->dev, "Failed to request reset pin\n");
263 phy->gpio_reset = gpio;
266 r = irq_of_parse_and_map(pp, 0);
268 nfc_err(&client->dev, "Unable to get irq, error: %d\n", r);
272 phy->irq_polarity = irq_get_trigger_type(r);
278 static int st21nfcb_nci_i2c_of_request_resources(struct i2c_client *client)
284 static int st21nfcb_nci_i2c_request_resources(struct i2c_client *client)
286 struct st21nfcb_nfc_platform_data *pdata;
287 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
291 pdata = client->dev.platform_data;
293 nfc_err(&client->dev, "No platform data\n");
297 /* store for later use */
298 phy->gpio_irq = pdata->gpio_irq;
299 phy->gpio_reset = pdata->gpio_reset;
300 phy->irq_polarity = pdata->irq_polarity;
302 r = devm_gpio_request_one(&client->dev, phy->gpio_irq,
303 GPIOF_IN, "clf_irq");
305 pr_err("%s : gpio_request failed\n", __FILE__);
309 r = devm_gpio_request_one(&client->dev,
310 phy->gpio_reset, GPIOF_OUT_INIT_HIGH, "clf_reset");
312 pr_err("%s : reset gpio_request failed\n", __FILE__);
317 irq = gpio_to_irq(phy->gpio_irq);
319 nfc_err(&client->dev,
320 "Unable to get irq number for GPIO %d error %d\n",
329 static int st21nfcb_nci_i2c_probe(struct i2c_client *client,
330 const struct i2c_device_id *id)
332 struct st21nfcb_i2c_phy *phy;
333 struct st21nfcb_nfc_platform_data *pdata;
336 dev_dbg(&client->dev, "%s\n", __func__);
337 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
339 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
340 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
344 phy = devm_kzalloc(&client->dev, sizeof(struct st21nfcb_i2c_phy),
347 nfc_err(&client->dev,
348 "Cannot allocate memory for st21nfcb i2c phy.\n");
352 phy->i2c_dev = client;
354 i2c_set_clientdata(client, phy);
356 pdata = client->dev.platform_data;
357 if (!pdata && client->dev.of_node) {
358 r = st21nfcb_nci_i2c_of_request_resources(client);
360 nfc_err(&client->dev, "No platform data\n");
364 r = st21nfcb_nci_i2c_request_resources(client);
366 nfc_err(&client->dev,
367 "Cannot get platform resources\n");
371 nfc_err(&client->dev,
372 "st21nfcb platform resources not available\n");
376 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
377 st21nfcb_nci_irq_thread_fn,
378 phy->irq_polarity | IRQF_ONESHOT,
379 ST21NFCB_NCI_DRIVER_NAME, phy);
381 nfc_err(&client->dev, "Unable to register IRQ handler\n");
385 return ndlc_probe(phy, &i2c_phy_ops, &client->dev,
386 ST21NFCB_FRAME_HEADROOM, ST21NFCB_FRAME_TAILROOM,
390 static int st21nfcb_nci_i2c_remove(struct i2c_client *client)
392 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
394 dev_dbg(&client->dev, "%s\n", __func__);
396 ndlc_remove(phy->ndlc);
399 st21nfcb_nci_i2c_disable(phy);
404 static const struct of_device_id of_st21nfcb_i2c_match[] = {
405 { .compatible = "st,st21nfcb_i2c", },
409 static struct i2c_driver st21nfcb_nci_i2c_driver = {
411 .owner = THIS_MODULE,
412 .name = ST21NFCB_NCI_I2C_DRIVER_NAME,
413 .of_match_table = of_match_ptr(of_st21nfcb_i2c_match),
415 .probe = st21nfcb_nci_i2c_probe,
416 .id_table = st21nfcb_nci_i2c_id_table,
417 .remove = st21nfcb_nci_i2c_remove,
420 module_i2c_driver(st21nfcb_nci_i2c_driver);
422 MODULE_LICENSE("GPL");
423 MODULE_DESCRIPTION(DRIVER_DESC);