1 /*
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
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>
29
30 #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
43 static struct i2c_device_id st21nfcb_nci_i2c_id_table[] = {
44 {ST21NFCB_NCI_DRIVER_NAME, 0},
45 {}
46 };
47 MODULE_DEVICE_TABLE(i2c, st21nfcb_nci_i2c_id_table);
48
49 struct st21nfcb_i2c_phy {
50 struct i2c_client *i2c_dev;
51 struct llt_ndlc *ndlc;
52
53 unsigned int gpio_irq;
54 unsigned int gpio_reset;
55 unsigned int irq_polarity;
56
57 int powered;
58 };
59
60 #define I2C_DUMP_SKB(info, skb) \
61 do { \
62 pr_debug("%s:\n", info); \
63 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
64 16, 1, (skb)->data, (skb)->len, 0); \
65 } while (0)
66
st21nfcb_nci_i2c_enable(void * phy_id)67 static int st21nfcb_nci_i2c_enable(void *phy_id)
68 {
69 struct st21nfcb_i2c_phy *phy = phy_id;
70
71 gpio_set_value(phy->gpio_reset, 0);
72 usleep_range(10000, 15000);
73 gpio_set_value(phy->gpio_reset, 1);
74 phy->powered = 1;
75 usleep_range(80000, 85000);
76
77 return 0;
78 }
79
st21nfcb_nci_i2c_disable(void * phy_id)80 static void st21nfcb_nci_i2c_disable(void *phy_id)
81 {
82 struct st21nfcb_i2c_phy *phy = phy_id;
83
84 pr_info("\n");
85
86 phy->powered = 0;
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);
91 }
92
93 /*
94 * Writing a frame must not return the number of written bytes.
95 * It must return either zero for success, or <0 for error.
96 * In addition, it must not alter the skb
97 */
st21nfcb_nci_i2c_write(void * phy_id,struct sk_buff * skb)98 static int st21nfcb_nci_i2c_write(void *phy_id, struct sk_buff *skb)
99 {
100 int r = -1;
101 struct st21nfcb_i2c_phy *phy = phy_id;
102 struct i2c_client *client = phy->i2c_dev;
103
104 I2C_DUMP_SKB("st21nfcb_nci_i2c_write", skb);
105
106 if (phy->ndlc->hard_fault != 0)
107 return phy->ndlc->hard_fault;
108
109 r = i2c_master_send(client, skb->data, skb->len);
110 if (r < 0) { /* Retry, chip was in standby */
111 usleep_range(1000, 4000);
112 r = i2c_master_send(client, skb->data, skb->len);
113 }
114
115 if (r >= 0) {
116 if (r != skb->len)
117 r = -EREMOTEIO;
118 else
119 r = 0;
120 }
121
122 return r;
123 }
124
125 /*
126 * Reads an ndlc frame and returns it in a newly allocated sk_buff.
127 * returns:
128 * frame size : if received frame is complete (find ST21NFCB_SOF_EOF at
129 * end of read)
130 * -EAGAIN : if received frame is incomplete (not find ST21NFCB_SOF_EOF
131 * at end of read)
132 * -EREMOTEIO : i2c read error (fatal)
133 * -EBADMSG : frame was incorrect and discarded
134 * (value returned from st21nfcb_nci_i2c_repack)
135 * -EIO : if no ST21NFCB_SOF_EOF is found after reaching
136 * the read length end sequence
137 */
st21nfcb_nci_i2c_read(struct st21nfcb_i2c_phy * phy,struct sk_buff ** skb)138 static int st21nfcb_nci_i2c_read(struct st21nfcb_i2c_phy *phy,
139 struct sk_buff **skb)
140 {
141 int r;
142 u8 len;
143 u8 buf[ST21NFCB_NCI_I2C_MAX_SIZE];
144 struct i2c_client *client = phy->i2c_dev;
145
146 r = i2c_master_recv(client, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
147 if (r < 0) { /* Retry, chip was in standby */
148 usleep_range(1000, 4000);
149 r = i2c_master_recv(client, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
150 }
151
152 if (r != ST21NFCB_NCI_I2C_MIN_SIZE)
153 return -EREMOTEIO;
154
155 len = be16_to_cpu(*(__be16 *) (buf + 2));
156 if (len > ST21NFCB_NCI_I2C_MAX_SIZE) {
157 nfc_err(&client->dev, "invalid frame len\n");
158 return -EBADMSG;
159 }
160
161 *skb = alloc_skb(ST21NFCB_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
162 if (*skb == NULL)
163 return -ENOMEM;
164
165 skb_reserve(*skb, ST21NFCB_NCI_I2C_MIN_SIZE);
166 skb_put(*skb, ST21NFCB_NCI_I2C_MIN_SIZE);
167 memcpy((*skb)->data, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
168
169 if (!len)
170 return 0;
171
172 r = i2c_master_recv(client, buf, len);
173 if (r != len) {
174 kfree_skb(*skb);
175 return -EREMOTEIO;
176 }
177
178 skb_put(*skb, len);
179 memcpy((*skb)->data + ST21NFCB_NCI_I2C_MIN_SIZE, buf, len);
180
181 I2C_DUMP_SKB("i2c frame read", *skb);
182
183 return 0;
184 }
185
186 /*
187 * Reads an ndlc frame from the chip.
188 *
189 * On ST21NFCB, IRQ goes in idle state when read starts.
190 */
st21nfcb_nci_irq_thread_fn(int irq,void * phy_id)191 static irqreturn_t st21nfcb_nci_irq_thread_fn(int irq, void *phy_id)
192 {
193 struct st21nfcb_i2c_phy *phy = phy_id;
194 struct i2c_client *client;
195 struct sk_buff *skb = NULL;
196 int r;
197
198 if (!phy || irq != phy->i2c_dev->irq) {
199 WARN_ON_ONCE(1);
200 return IRQ_NONE;
201 }
202
203 client = phy->i2c_dev;
204 dev_dbg(&client->dev, "IRQ\n");
205
206 if (phy->ndlc->hard_fault)
207 return IRQ_HANDLED;
208
209 if (!phy->powered) {
210 st21nfcb_nci_i2c_disable(phy);
211 return IRQ_HANDLED;
212 }
213
214 r = st21nfcb_nci_i2c_read(phy, &skb);
215 if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
216 return IRQ_HANDLED;
217
218 ndlc_recv(phy->ndlc, skb);
219
220 return IRQ_HANDLED;
221 }
222
223 static struct nfc_phy_ops i2c_phy_ops = {
224 .write = st21nfcb_nci_i2c_write,
225 .enable = st21nfcb_nci_i2c_enable,
226 .disable = st21nfcb_nci_i2c_disable,
227 };
228
229 #ifdef CONFIG_OF
st21nfcb_nci_i2c_of_request_resources(struct i2c_client * client)230 static int st21nfcb_nci_i2c_of_request_resources(struct i2c_client *client)
231 {
232 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
233 struct device_node *pp;
234 int gpio;
235 int r;
236
237 pp = client->dev.of_node;
238 if (!pp)
239 return -ENODEV;
240
241 /* Get GPIO from device tree */
242 gpio = of_get_named_gpio(pp, "reset-gpios", 0);
243 if (gpio < 0) {
244 nfc_err(&client->dev,
245 "Failed to retrieve reset-gpios from device tree\n");
246 return gpio;
247 }
248
249 /* GPIO request and configuration */
250 r = devm_gpio_request_one(&client->dev, gpio,
251 GPIOF_OUT_INIT_HIGH, "clf_reset");
252 if (r) {
253 nfc_err(&client->dev, "Failed to request reset pin\n");
254 return -ENODEV;
255 }
256 phy->gpio_reset = gpio;
257
258 /* IRQ */
259 r = irq_of_parse_and_map(pp, 0);
260 if (r < 0) {
261 nfc_err(&client->dev, "Unable to get irq, error: %d\n", r);
262 return r;
263 }
264
265 phy->irq_polarity = irq_get_trigger_type(r);
266 client->irq = r;
267
268 return 0;
269 }
270 #else
st21nfcb_nci_i2c_of_request_resources(struct i2c_client * client)271 static int st21nfcb_nci_i2c_of_request_resources(struct i2c_client *client)
272 {
273 return -ENODEV;
274 }
275 #endif
276
st21nfcb_nci_i2c_request_resources(struct i2c_client * client)277 static int st21nfcb_nci_i2c_request_resources(struct i2c_client *client)
278 {
279 struct st21nfcb_nfc_platform_data *pdata;
280 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
281 int r;
282 int irq;
283
284 pdata = client->dev.platform_data;
285 if (pdata == NULL) {
286 nfc_err(&client->dev, "No platform data\n");
287 return -EINVAL;
288 }
289
290 /* store for later use */
291 phy->gpio_irq = pdata->gpio_irq;
292 phy->gpio_reset = pdata->gpio_reset;
293 phy->irq_polarity = pdata->irq_polarity;
294
295 r = devm_gpio_request_one(&client->dev, phy->gpio_irq,
296 GPIOF_IN, "clf_irq");
297 if (r) {
298 pr_err("%s : gpio_request failed\n", __FILE__);
299 return -ENODEV;
300 }
301
302 r = devm_gpio_request_one(&client->dev,
303 phy->gpio_reset, GPIOF_OUT_INIT_HIGH, "clf_reset");
304 if (r) {
305 pr_err("%s : reset gpio_request failed\n", __FILE__);
306 return -ENODEV;
307 }
308
309 /* IRQ */
310 irq = gpio_to_irq(phy->gpio_irq);
311 if (irq < 0) {
312 nfc_err(&client->dev,
313 "Unable to get irq number for GPIO %d error %d\n",
314 phy->gpio_irq, r);
315 return -ENODEV;
316 }
317 client->irq = irq;
318
319 return 0;
320 }
321
st21nfcb_nci_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)322 static int st21nfcb_nci_i2c_probe(struct i2c_client *client,
323 const struct i2c_device_id *id)
324 {
325 struct st21nfcb_i2c_phy *phy;
326 struct st21nfcb_nfc_platform_data *pdata;
327 int r;
328
329 dev_dbg(&client->dev, "%s\n", __func__);
330 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
331
332 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
333 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
334 return -ENODEV;
335 }
336
337 phy = devm_kzalloc(&client->dev, sizeof(struct st21nfcb_i2c_phy),
338 GFP_KERNEL);
339 if (!phy) {
340 nfc_err(&client->dev,
341 "Cannot allocate memory for st21nfcb i2c phy.\n");
342 return -ENOMEM;
343 }
344
345 phy->i2c_dev = client;
346
347 i2c_set_clientdata(client, phy);
348
349 pdata = client->dev.platform_data;
350 if (!pdata && client->dev.of_node) {
351 r = st21nfcb_nci_i2c_of_request_resources(client);
352 if (r) {
353 nfc_err(&client->dev, "No platform data\n");
354 return r;
355 }
356 } else if (pdata) {
357 r = st21nfcb_nci_i2c_request_resources(client);
358 if (r) {
359 nfc_err(&client->dev,
360 "Cannot get platform resources\n");
361 return r;
362 }
363 } else {
364 nfc_err(&client->dev,
365 "st21nfcb platform resources not available\n");
366 return -ENODEV;
367 }
368
369 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
370 st21nfcb_nci_irq_thread_fn,
371 phy->irq_polarity | IRQF_ONESHOT,
372 ST21NFCB_NCI_DRIVER_NAME, phy);
373 if (r < 0) {
374 nfc_err(&client->dev, "Unable to register IRQ handler\n");
375 return r;
376 }
377
378 return ndlc_probe(phy, &i2c_phy_ops, &client->dev,
379 ST21NFCB_FRAME_HEADROOM, ST21NFCB_FRAME_TAILROOM,
380 &phy->ndlc);
381 }
382
st21nfcb_nci_i2c_remove(struct i2c_client * client)383 static int st21nfcb_nci_i2c_remove(struct i2c_client *client)
384 {
385 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
386
387 dev_dbg(&client->dev, "%s\n", __func__);
388
389 ndlc_remove(phy->ndlc);
390
391 return 0;
392 }
393
394 static const struct of_device_id of_st21nfcb_i2c_match[] = {
395 { .compatible = "st,st21nfcb_i2c", },
396 {}
397 };
398
399 static struct i2c_driver st21nfcb_nci_i2c_driver = {
400 .driver = {
401 .owner = THIS_MODULE,
402 .name = ST21NFCB_NCI_I2C_DRIVER_NAME,
403 .of_match_table = of_match_ptr(of_st21nfcb_i2c_match),
404 },
405 .probe = st21nfcb_nci_i2c_probe,
406 .id_table = st21nfcb_nci_i2c_id_table,
407 .remove = st21nfcb_nci_i2c_remove,
408 };
409
410 module_i2c_driver(st21nfcb_nci_i2c_driver);
411
412 MODULE_LICENSE("GPL");
413 MODULE_DESCRIPTION(DRIVER_DESC);
414