blob: 76a4cad41cec9b3affa4f903683a313d4620b389 [file] [log] [blame]
Christophe Ricard35630df2014-05-25 22:35:38 +02001/*
2 * I2C Link Layer for ST21NFCB NCI based Driver
3 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
4 *
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.
8 *
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.
13 *
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/>.
16 */
17
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Christophe Ricard35630df2014-05-25 22:35:38 +020020#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>
Christophe Ricard35630df2014-05-25 22:35:38 +020025#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <linux/nfc.h>
Christophe Ricard35630df2014-05-25 22:35:38 +020028#include <linux/platform_data/st21nfcb.h>
29
Christophe Ricard35630df2014-05-25 22:35:38 +020030#include "ndlc.h"
31
32#define DRIVER_DESC "NCI NFC driver for ST21NFCB"
33
34/* ndlc header */
35#define ST21NFCB_FRAME_HEADROOM 1
36#define ST21NFCB_FRAME_TAILROOM 0
37
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 */
40
41#define ST21NFCB_NCI_I2C_DRIVER_NAME "st21nfcb_nci_i2c"
42
43static struct i2c_device_id st21nfcb_nci_i2c_id_table[] = {
44 {ST21NFCB_NCI_DRIVER_NAME, 0},
45 {}
46};
47MODULE_DEVICE_TABLE(i2c, st21nfcb_nci_i2c_id_table);
48
49struct st21nfcb_i2c_phy {
50 struct i2c_client *i2c_dev;
51 struct llt_ndlc *ndlc;
52
Christophe Ricard35630df2014-05-25 22:35:38 +020053 unsigned int gpio_reset;
54 unsigned int irq_polarity;
55
56 int powered;
Christophe Ricard35630df2014-05-25 22:35:38 +020057};
58
59#define I2C_DUMP_SKB(info, skb) \
60do { \
61 pr_debug("%s:\n", info); \
62 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
63 16, 1, (skb)->data, (skb)->len, 0); \
64} while (0)
65
66static int st21nfcb_nci_i2c_enable(void *phy_id)
67{
68 struct st21nfcb_i2c_phy *phy = phy_id;
69
70 gpio_set_value(phy->gpio_reset, 0);
71 usleep_range(10000, 15000);
72 gpio_set_value(phy->gpio_reset, 1);
73 phy->powered = 1;
74 usleep_range(80000, 85000);
75
76 return 0;
77}
78
79static void st21nfcb_nci_i2c_disable(void *phy_id)
80{
81 struct st21nfcb_i2c_phy *phy = phy_id;
82
Christophe Ricard35630df2014-05-25 22:35:38 +020083 phy->powered = 0;
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);
88}
89
90static void st21nfcb_nci_remove_header(struct sk_buff *skb)
91{
92 skb_pull(skb, ST21NFCB_FRAME_HEADROOM);
93}
94
95/*
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
99 */
100static int st21nfcb_nci_i2c_write(void *phy_id, struct sk_buff *skb)
101{
102 int r = -1;
103 struct st21nfcb_i2c_phy *phy = phy_id;
104 struct i2c_client *client = phy->i2c_dev;
105
106 I2C_DUMP_SKB("st21nfcb_nci_i2c_write", skb);
107
Christophe Ricard4294e322014-09-13 10:28:47 +0200108 if (phy->ndlc->hard_fault != 0)
109 return phy->ndlc->hard_fault;
Christophe Ricard35630df2014-05-25 22:35:38 +0200110
111 r = i2c_master_send(client, skb->data, skb->len);
Christophe Ricardd4a41d12015-03-31 08:02:15 +0200112 if (r < 0) { /* Retry, chip was in standby */
Christophe Ricard35630df2014-05-25 22:35:38 +0200113 usleep_range(1000, 4000);
114 r = i2c_master_send(client, skb->data, skb->len);
115 }
116
117 if (r >= 0) {
118 if (r != skb->len)
119 r = -EREMOTEIO;
120 else
121 r = 0;
122 }
123
124 st21nfcb_nci_remove_header(skb);
125
126 return r;
127}
128
129/*
130 * Reads an ndlc frame and returns it in a newly allocated sk_buff.
131 * returns:
132 * frame size : if received frame is complete (find ST21NFCB_SOF_EOF at
133 * end of read)
134 * -EAGAIN : if received frame is incomplete (not find ST21NFCB_SOF_EOF
135 * at end of read)
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
141 */
142static int st21nfcb_nci_i2c_read(struct st21nfcb_i2c_phy *phy,
143 struct sk_buff **skb)
144{
145 int r;
146 u8 len;
147 u8 buf[ST21NFCB_NCI_I2C_MAX_SIZE];
148 struct i2c_client *client = phy->i2c_dev;
149
Christophe Ricardfb92ff72014-05-25 22:46:58 +0200150 r = i2c_master_recv(client, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
Christophe Ricardd4a41d12015-03-31 08:02:15 +0200151 if (r < 0) { /* Retry, chip was in standby */
Christophe Ricard35630df2014-05-25 22:35:38 +0200152 usleep_range(1000, 4000);
Christophe Ricardfb92ff72014-05-25 22:46:58 +0200153 r = i2c_master_recv(client, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
Christophe Ricardac633ba2014-09-03 23:30:26 +0200154 }
155
Christophe Ricardbc6b8272014-09-13 10:28:51 +0200156 if (r != ST21NFCB_NCI_I2C_MIN_SIZE)
Christophe Ricard35630df2014-05-25 22:35:38 +0200157 return -EREMOTEIO;
Christophe Ricard35630df2014-05-25 22:35:38 +0200158
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");
162 return -EBADMSG;
163 }
164
Christophe Ricardfb92ff72014-05-25 22:46:58 +0200165 *skb = alloc_skb(ST21NFCB_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
Christophe Ricard35630df2014-05-25 22:35:38 +0200166 if (*skb == NULL)
167 return -ENOMEM;
168
Christophe Ricardfb92ff72014-05-25 22:46:58 +0200169 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);
Christophe Ricard35630df2014-05-25 22:35:38 +0200172
173 if (!len)
174 return 0;
175
176 r = i2c_master_recv(client, buf, len);
177 if (r != len) {
178 kfree_skb(*skb);
179 return -EREMOTEIO;
180 }
181
182 skb_put(*skb, len);
Christophe Ricardfb92ff72014-05-25 22:46:58 +0200183 memcpy((*skb)->data + ST21NFCB_NCI_I2C_MIN_SIZE, buf, len);
Christophe Ricard35630df2014-05-25 22:35:38 +0200184
185 I2C_DUMP_SKB("i2c frame read", *skb);
186
187 return 0;
188}
189
190/*
191 * Reads an ndlc frame from the chip.
192 *
193 * On ST21NFCB, IRQ goes in idle state when read starts.
194 */
195static irqreturn_t st21nfcb_nci_irq_thread_fn(int irq, void *phy_id)
196{
197 struct st21nfcb_i2c_phy *phy = phy_id;
198 struct i2c_client *client;
199 struct sk_buff *skb = NULL;
200 int r;
201
Christophe Ricard72744962015-01-25 23:33:29 +0100202 if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) {
Christophe Ricard35630df2014-05-25 22:35:38 +0200203 WARN_ON_ONCE(1);
204 return IRQ_NONE;
205 }
206
207 client = phy->i2c_dev;
208 dev_dbg(&client->dev, "IRQ\n");
209
Christophe Ricard4294e322014-09-13 10:28:47 +0200210 if (phy->ndlc->hard_fault)
Christophe Ricard35630df2014-05-25 22:35:38 +0200211 return IRQ_HANDLED;
212
213 if (!phy->powered) {
214 st21nfcb_nci_i2c_disable(phy);
215 return IRQ_HANDLED;
216 }
217
218 r = st21nfcb_nci_i2c_read(phy, &skb);
Christophe Ricard4294e322014-09-13 10:28:47 +0200219 if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
Christophe Ricard35630df2014-05-25 22:35:38 +0200220 return IRQ_HANDLED;
Christophe Ricard35630df2014-05-25 22:35:38 +0200221
222 ndlc_recv(phy->ndlc, skb);
223
224 return IRQ_HANDLED;
225}
226
227static 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,
231};
232
233#ifdef CONFIG_OF
234static int st21nfcb_nci_i2c_of_request_resources(struct i2c_client *client)
235{
236 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
237 struct device_node *pp;
238 int gpio;
239 int r;
240
241 pp = client->dev.of_node;
242 if (!pp)
243 return -ENODEV;
244
245 /* Get GPIO from device tree */
246 gpio = of_get_named_gpio(pp, "reset-gpios", 0);
247 if (gpio < 0) {
248 nfc_err(&client->dev,
249 "Failed to retrieve reset-gpios from device tree\n");
250 return gpio;
251 }
252
253 /* GPIO request and configuration */
Christophe Ricard56ee6452014-07-28 18:11:33 +0200254 r = devm_gpio_request_one(&client->dev, gpio,
255 GPIOF_OUT_INIT_HIGH, "clf_reset");
Christophe Ricard35630df2014-05-25 22:35:38 +0200256 if (r) {
257 nfc_err(&client->dev, "Failed to request reset pin\n");
Christophe Ricard18159622014-12-02 21:27:56 +0100258 return r;
Christophe Ricard35630df2014-05-25 22:35:38 +0200259 }
Christophe Ricard35630df2014-05-25 22:35:38 +0200260 phy->gpio_reset = gpio;
261
Christophe Ricarda80d0cb2014-11-13 00:30:26 +0100262 phy->irq_polarity = irq_get_trigger_type(client->irq);
Christophe Ricard35630df2014-05-25 22:35:38 +0200263
264 return 0;
265}
266#else
267static int st21nfcb_nci_i2c_of_request_resources(struct i2c_client *client)
268{
269 return -ENODEV;
270}
271#endif
272
273static int st21nfcb_nci_i2c_request_resources(struct i2c_client *client)
274{
275 struct st21nfcb_nfc_platform_data *pdata;
276 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
277 int r;
Christophe Ricard35630df2014-05-25 22:35:38 +0200278
279 pdata = client->dev.platform_data;
280 if (pdata == NULL) {
281 nfc_err(&client->dev, "No platform data\n");
282 return -EINVAL;
283 }
284
285 /* store for later use */
Christophe Ricard35630df2014-05-25 22:35:38 +0200286 phy->gpio_reset = pdata->gpio_reset;
287 phy->irq_polarity = pdata->irq_polarity;
288
Christophe Ricard56ee6452014-07-28 18:11:33 +0200289 r = devm_gpio_request_one(&client->dev,
290 phy->gpio_reset, GPIOF_OUT_INIT_HIGH, "clf_reset");
Christophe Ricard35630df2014-05-25 22:35:38 +0200291 if (r) {
292 pr_err("%s : reset gpio_request failed\n", __FILE__);
Christophe Ricard18159622014-12-02 21:27:56 +0100293 return r;
Christophe Ricard35630df2014-05-25 22:35:38 +0200294 }
295
Christophe Ricard35630df2014-05-25 22:35:38 +0200296 return 0;
297}
298
299static int st21nfcb_nci_i2c_probe(struct i2c_client *client,
300 const struct i2c_device_id *id)
301{
302 struct st21nfcb_i2c_phy *phy;
303 struct st21nfcb_nfc_platform_data *pdata;
304 int r;
305
306 dev_dbg(&client->dev, "%s\n", __func__);
307 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
308
309 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
310 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
311 return -ENODEV;
312 }
313
314 phy = devm_kzalloc(&client->dev, sizeof(struct st21nfcb_i2c_phy),
315 GFP_KERNEL);
Joe Perches3590ebc2015-04-07 00:17:00 -0700316 if (!phy)
Christophe Ricard35630df2014-05-25 22:35:38 +0200317 return -ENOMEM;
Christophe Ricard35630df2014-05-25 22:35:38 +0200318
319 phy->i2c_dev = client;
320
321 i2c_set_clientdata(client, phy);
322
323 pdata = client->dev.platform_data;
324 if (!pdata && client->dev.of_node) {
325 r = st21nfcb_nci_i2c_of_request_resources(client);
326 if (r) {
327 nfc_err(&client->dev, "No platform data\n");
328 return r;
329 }
330 } else if (pdata) {
331 r = st21nfcb_nci_i2c_request_resources(client);
332 if (r) {
333 nfc_err(&client->dev,
334 "Cannot get platform resources\n");
335 return r;
336 }
337 } else {
338 nfc_err(&client->dev,
339 "st21nfcb platform resources not available\n");
340 return -ENODEV;
341 }
342
Christophe Ricard72744962015-01-25 23:33:29 +0100343 r = ndlc_probe(phy, &i2c_phy_ops, &client->dev,
344 ST21NFCB_FRAME_HEADROOM, ST21NFCB_FRAME_TAILROOM,
345 &phy->ndlc);
346 if (r < 0) {
347 nfc_err(&client->dev, "Unable to register ndlc layer\n");
348 return r;
349 }
350
Christophe Ricard35630df2014-05-25 22:35:38 +0200351 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
352 st21nfcb_nci_irq_thread_fn,
353 phy->irq_polarity | IRQF_ONESHOT,
354 ST21NFCB_NCI_DRIVER_NAME, phy);
Christophe Ricard72744962015-01-25 23:33:29 +0100355 if (r < 0)
Christophe Ricard35630df2014-05-25 22:35:38 +0200356 nfc_err(&client->dev, "Unable to register IRQ handler\n");
Christophe Ricard35630df2014-05-25 22:35:38 +0200357
Christophe Ricard72744962015-01-25 23:33:29 +0100358 return r;
Christophe Ricard35630df2014-05-25 22:35:38 +0200359}
360
361static int st21nfcb_nci_i2c_remove(struct i2c_client *client)
362{
363 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
364
365 dev_dbg(&client->dev, "%s\n", __func__);
366
367 ndlc_remove(phy->ndlc);
368
369 if (phy->powered)
370 st21nfcb_nci_i2c_disable(phy);
371
372 return 0;
373}
374
Christophe Ricardd9b66912014-11-13 00:30:25 +0100375#ifdef CONFIG_OF
Christophe Ricard35630df2014-05-25 22:35:38 +0200376static const struct of_device_id of_st21nfcb_i2c_match[] = {
Christophe Ricard1a94cb62014-12-08 22:08:07 +0100377 { .compatible = "st,st21nfcb-i2c", },
Christophe Ricard35630df2014-05-25 22:35:38 +0200378 { .compatible = "st,st21nfcb_i2c", },
379 {}
380};
Christophe Ricardd9b66912014-11-13 00:30:25 +0100381MODULE_DEVICE_TABLE(of, of_st21nfcb_i2c_match);
382#endif
Christophe Ricard35630df2014-05-25 22:35:38 +0200383
384static struct i2c_driver st21nfcb_nci_i2c_driver = {
385 .driver = {
386 .owner = THIS_MODULE,
387 .name = ST21NFCB_NCI_I2C_DRIVER_NAME,
Christophe Ricard35630df2014-05-25 22:35:38 +0200388 .of_match_table = of_match_ptr(of_st21nfcb_i2c_match),
389 },
390 .probe = st21nfcb_nci_i2c_probe,
391 .id_table = st21nfcb_nci_i2c_id_table,
392 .remove = st21nfcb_nci_i2c_remove,
393};
394
395module_i2c_driver(st21nfcb_nci_i2c_driver);
396
397MODULE_LICENSE("GPL");
398MODULE_DESCRIPTION(DRIVER_DESC);