blob: 48f8e23fc3210b1f6166c214b9839d3fc801d2b0 [file] [log] [blame]
Christophe Ricard68957302014-03-25 06:51:47 +01001/*
2 * I2C Link Layer for ST21NFCA HCI 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/crc-ccitt.h>
21#include <linux/module.h>
22#include <linux/i2c.h>
23#include <linux/gpio.h>
24#include <linux/miscdevice.h>
25#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <linux/nfc.h>
28#include <linux/firmware.h>
29#include <linux/unaligned/access_ok.h>
30#include <linux/platform_data/st21nfca.h>
31
32#include <net/nfc/hci.h>
33#include <net/nfc/llc.h>
34#include <net/nfc/nfc.h>
35
36#include "st21nfca.h"
37
38/*
39 * Every frame starts with ST21NFCA_SOF_EOF and ends with ST21NFCA_SOF_EOF.
40 * Because ST21NFCA_SOF_EOF is a possible data value, there is a mecanism
41 * called byte stuffing has been introduced.
42 *
43 * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
44 * - insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
45 * - xor byte with ST21NFCA_BYTE_STUFFING_MASK
46 */
47#define ST21NFCA_SOF_EOF 0x7e
48#define ST21NFCA_BYTE_STUFFING_MASK 0x20
49#define ST21NFCA_ESCAPE_BYTE_STUFFING 0x7d
50
Christophe Ricarde1fb97b2014-04-24 23:19:31 +020051/* SOF + 00 */
Christophe Ricard68957302014-03-25 06:51:47 +010052#define ST21NFCA_FRAME_HEADROOM 2
53
Christophe Ricarde1fb97b2014-04-24 23:19:31 +020054/* 2 bytes crc + EOF */
55#define ST21NFCA_FRAME_TAILROOM 3
Christophe Ricardc97ffdb2014-04-24 23:19:33 +020056#define IS_START_OF_FRAME(buf) (buf[0] == ST21NFCA_SOF_EOF && \
57 buf[1] == 0)
Christophe Ricard68957302014-03-25 06:51:47 +010058
59#define ST21NFCA_HCI_I2C_DRIVER_NAME "st21nfca_hci_i2c"
60
61static struct i2c_device_id st21nfca_hci_i2c_id_table[] = {
62 {ST21NFCA_HCI_DRIVER_NAME, 0},
63 {}
64};
65
66MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table);
67
68struct st21nfca_i2c_phy {
69 struct i2c_client *i2c_dev;
70 struct nfc_hci_dev *hdev;
71
72 unsigned int gpio_ena;
73 unsigned int gpio_irq;
74 unsigned int irq_polarity;
75
76 struct sk_buff *pending_skb;
77 int current_read_len;
78 /*
79 * crc might have fail because i2c macro
80 * is disable due to other interface activity
81 */
82 int crc_trials;
83
84 int powered;
85 int run_mode;
86
87 /*
88 * < 0 if hardware error occured (e.g. i2c err)
89 * and prevents normal operation.
90 */
91 int hard_fault;
92};
93static u8 len_seq[] = { 13, 24, 15, 29 };
94static u16 wait_tab[] = { 2, 3, 5, 15, 20, 40};
95
96#define I2C_DUMP_SKB(info, skb) \
97do { \
98 pr_debug("%s:\n", info); \
99 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
100 16, 1, (skb)->data, (skb)->len, 0); \
101} while (0)
102
Christophe Ricard18d2c622014-04-01 00:34:07 +0200103/*
104 * In order to get the CLF in a known state we generate an internal reboot
105 * using a proprietary command.
106 * Once the reboot is completed, we expect to receive a ST21NFCA_SOF_EOF
107 * fill buffer.
108 */
109static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy)
Christophe Ricard68957302014-03-25 06:51:47 +0100110{
Christophe Ricardc5b0c372014-04-01 00:34:03 +0200111 u16 wait_reboot[] = { 50, 300, 1000 };
Christophe Ricard68957302014-03-25 06:51:47 +0100112 char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E };
113 u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE];
114 int i, r = -1;
115
Christophe Ricard18d2c622014-04-01 00:34:07 +0200116 for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
Christophe Ricard68957302014-03-25 06:51:47 +0100117 r = i2c_master_send(phy->i2c_dev, reboot_cmd,
118 sizeof(reboot_cmd));
Christophe Ricard18d2c622014-04-01 00:34:07 +0200119 if (r < 0)
120 msleep(wait_reboot[i]);
121 }
122 if (r < 0)
123 return r;
Christophe Ricard68957302014-03-25 06:51:47 +0100124
Christophe Ricard18d2c622014-04-01 00:34:07 +0200125 /* CLF is spending about 20ms to do an internal reboot */
126 msleep(20);
127 r = -1;
128 for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
129 r = i2c_master_recv(phy->i2c_dev, tmp,
130 ST21NFCA_HCI_LLC_MAX_SIZE);
131 if (r < 0)
132 msleep(wait_reboot[i]);
133 }
134 if (r < 0)
135 return r;
136
137 for (i = 0; i < ST21NFCA_HCI_LLC_MAX_SIZE &&
138 tmp[i] == ST21NFCA_SOF_EOF; i++)
139 ;
140
141 if (r != ST21NFCA_HCI_LLC_MAX_SIZE)
142 return -ENODEV;
143
144 usleep_range(1000, 1500);
145 return 0;
Christophe Ricard68957302014-03-25 06:51:47 +0100146}
147
148static int st21nfca_hci_i2c_enable(void *phy_id)
149{
150 struct st21nfca_i2c_phy *phy = phy_id;
151
152 gpio_set_value(phy->gpio_ena, 1);
153 phy->powered = 1;
154 phy->run_mode = ST21NFCA_HCI_MODE;
155
156 usleep_range(10000, 15000);
157
158 return 0;
159}
160
161static void st21nfca_hci_i2c_disable(void *phy_id)
162{
163 struct st21nfca_i2c_phy *phy = phy_id;
164
165 pr_info("\n");
166 gpio_set_value(phy->gpio_ena, 0);
167
168 phy->powered = 0;
169}
170
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200171static void st21nfca_hci_add_len_crc(struct sk_buff *skb)
Christophe Ricard68957302014-03-25 06:51:47 +0100172{
Christophe Ricard68957302014-03-25 06:51:47 +0100173 u16 crc;
174 u8 tmp;
175
176 *skb_push(skb, 1) = 0;
177
178 crc = crc_ccitt(0xffff, skb->data, skb->len);
179 crc = ~crc;
180
181 tmp = crc & 0x00ff;
182 *skb_put(skb, 1) = tmp;
183
184 tmp = (crc >> 8) & 0x00ff;
185 *skb_put(skb, 1) = tmp;
Christophe Ricard68957302014-03-25 06:51:47 +0100186}
187
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200188static void st21nfca_hci_remove_len_crc(struct sk_buff *skb)
Christophe Ricard68957302014-03-25 06:51:47 +0100189{
190 skb_pull(skb, ST21NFCA_FRAME_HEADROOM);
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200191 skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM);
Christophe Ricard68957302014-03-25 06:51:47 +0100192}
193
194/*
195 * Writing a frame must not return the number of written bytes.
196 * It must return either zero for success, or <0 for error.
197 * In addition, it must not alter the skb
198 */
199static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb)
200{
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200201 int r = -1, i, j;
Christophe Ricard68957302014-03-25 06:51:47 +0100202 struct st21nfca_i2c_phy *phy = phy_id;
203 struct i2c_client *client = phy->i2c_dev;
Christophe Ricard68957302014-03-25 06:51:47 +0100204 u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2];
205
206 I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb);
207
208
209 if (phy->hard_fault != 0)
210 return phy->hard_fault;
211
212 /*
213 * Compute CRC before byte stuffing computation on frame
214 * Note st21nfca_hci_add_len_crc is doing a byte stuffing
215 * on its own value
216 */
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200217 st21nfca_hci_add_len_crc(skb);
Christophe Ricard68957302014-03-25 06:51:47 +0100218
219 /* add ST21NFCA_SOF_EOF on tail */
220 *skb_put(skb, 1) = ST21NFCA_SOF_EOF;
221 /* add ST21NFCA_SOF_EOF on head */
222 *skb_push(skb, 1) = ST21NFCA_SOF_EOF;
223
224 /*
225 * Compute byte stuffing
226 * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
227 * insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
228 * xor byte with ST21NFCA_BYTE_STUFFING_MASK
229 */
230 tmp[0] = skb->data[0];
231 for (i = 1, j = 1; i < skb->len - 1; i++, j++) {
232 if (skb->data[i] == ST21NFCA_SOF_EOF
233 || skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) {
234 tmp[j] = ST21NFCA_ESCAPE_BYTE_STUFFING;
235 j++;
236 tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK;
237 } else {
238 tmp[j] = skb->data[i];
239 }
240 }
241 tmp[j] = skb->data[i];
242 j++;
243
244 /*
245 * Manage sleep mode
246 * Try 3 times to send data with delay between each
247 */
248 for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++) {
249 r = i2c_master_send(client, tmp, j);
250 if (r < 0)
251 msleep(wait_tab[i]);
252 }
253
254 if (r >= 0) {
255 if (r != j)
256 r = -EREMOTEIO;
257 else
258 r = 0;
259 }
260
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200261 st21nfca_hci_remove_len_crc(skb);
Christophe Ricard68957302014-03-25 06:51:47 +0100262
263 return r;
264}
265
266static int get_frame_size(u8 *buf, int buflen)
267{
268 int len = 0;
269 if (buf[len + 1] == ST21NFCA_SOF_EOF)
270 return 0;
271
272 for (len = 1; len < buflen && buf[len] != ST21NFCA_SOF_EOF; len++)
273 ;
274
275 return len;
276}
277
278static int check_crc(u8 *buf, int buflen)
279{
280 u16 crc;
281
282 crc = crc_ccitt(0xffff, buf, buflen - 2);
283 crc = ~crc;
284
285 if (buf[buflen - 2] != (crc & 0xff) || buf[buflen - 1] != (crc >> 8)) {
286 pr_err(ST21NFCA_HCI_DRIVER_NAME
287 ": CRC error 0x%x != 0x%x 0x%x\n", crc, buf[buflen - 1],
288 buf[buflen - 2]);
289
290 pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
291 print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
292 16, 2, buf, buflen, false);
293 return -EPERM;
294 }
295 return 0;
296}
297
298/*
299 * Prepare received data for upper layer.
300 * Received data include byte stuffing, crc and sof/eof
301 * which is not usable by hci part.
302 * returns:
303 * frame size without sof/eof, header and byte stuffing
304 * -EBADMSG : frame was incorrect and discarded
305 */
306static int st21nfca_hci_i2c_repack(struct sk_buff *skb)
307{
308 int i, j, r, size;
309 if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0))
310 return -EBADMSG;
311
312 size = get_frame_size(skb->data, skb->len);
313 if (size > 0) {
314 skb_trim(skb, size);
315 /* remove ST21NFCA byte stuffing for upper layer */
316 for (i = 1, j = 0; i < skb->len; i++) {
Christophe Ricard3096e252014-04-24 23:19:32 +0200317 if (skb->data[i + j] ==
Christophe Ricard68957302014-03-25 06:51:47 +0100318 (u8) ST21NFCA_ESCAPE_BYTE_STUFFING) {
Christophe Ricard3096e252014-04-24 23:19:32 +0200319 skb->data[i] = skb->data[i + j + 1]
320 | ST21NFCA_BYTE_STUFFING_MASK;
Christophe Ricard68957302014-03-25 06:51:47 +0100321 i++;
322 j++;
323 }
324 skb->data[i] = skb->data[i + j];
325 }
326 /* remove byte stuffing useless byte */
327 skb_trim(skb, i - j);
328 /* remove ST21NFCA_SOF_EOF from head */
329 skb_pull(skb, 1);
330
331 r = check_crc(skb->data, skb->len);
332 if (r != 0) {
333 i = 0;
334 return -EBADMSG;
335 }
336
337 /* remove headbyte */
338 skb_pull(skb, 1);
339 /* remove crc. Byte Stuffing is already removed here */
340 skb_trim(skb, skb->len - 2);
341 return skb->len;
342 }
343 return 0;
344}
345
346/*
347 * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
348 * that i2c bus will be flushed and that next read will start on a new frame.
349 * returned skb contains only LLC header and payload.
350 * returns:
351 * frame size : if received frame is complete (find ST21NFCA_SOF_EOF at
352 * end of read)
353 * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF
354 * at end of read)
355 * -EREMOTEIO : i2c read error (fatal)
356 * -EBADMSG : frame was incorrect and discarded
357 * (value returned from st21nfca_hci_i2c_repack)
358 * -EIO : if no ST21NFCA_SOF_EOF is found after reaching
359 * the read length end sequence
360 */
361static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy,
362 struct sk_buff *skb)
363{
364 int r, i;
365 u8 len;
Christophe Ricardc97ffdb2014-04-24 23:19:33 +0200366 u8 buf[ST21NFCA_HCI_LLC_MAX_PAYLOAD];
Christophe Ricard68957302014-03-25 06:51:47 +0100367 struct i2c_client *client = phy->i2c_dev;
368
369 if (phy->current_read_len < ARRAY_SIZE(len_seq)) {
370 len = len_seq[phy->current_read_len];
371
372 /*
373 * Add retry mecanism
374 * Operation on I2C interface may fail in case of operation on
375 * RF or SWP interface
376 */
377 r = 0;
378 for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) {
Christophe Ricardc97ffdb2014-04-24 23:19:33 +0200379 r = i2c_master_recv(client, buf, len);
Christophe Ricard68957302014-03-25 06:51:47 +0100380 if (r < 0)
381 msleep(wait_tab[i]);
382 }
383
384 if (r != len) {
385 phy->current_read_len = 0;
386 return -EREMOTEIO;
387 }
388
Christophe Ricardc97ffdb2014-04-24 23:19:33 +0200389 /*
390 * The first read sequence does not start with SOF.
391 * Data is corrupeted so we drop it.
392 */
393 if (!phy->current_read_len && buf[0] != ST21NFCA_SOF_EOF) {
394 skb_trim(skb, 0);
395 phy->current_read_len = 0;
396 return -EIO;
397 } else if (phy->current_read_len &&
398 IS_START_OF_FRAME(buf)) {
399 /*
400 * Previous frame transmission was interrupted and
401 * the frame got repeated.
402 * Received frame start with ST21NFCA_SOF_EOF + 00.
403 */
404 skb_trim(skb, 0);
405 phy->current_read_len = 0;
406 }
407
408 memcpy(skb_put(skb, len), buf, len);
409
410 if (skb->data[skb->len - 1] == ST21NFCA_SOF_EOF) {
Christophe Ricard68957302014-03-25 06:51:47 +0100411 phy->current_read_len = 0;
412 return st21nfca_hci_i2c_repack(skb);
413 }
414 phy->current_read_len++;
415 return -EAGAIN;
416 }
417 return -EIO;
418}
419
420/*
421 * Reads an shdlc frame from the chip. This is not as straightforward as it
422 * seems. The frame format is data-crc, and corruption can occur anywhere
423 * while transiting on i2c bus, such that we could read an invalid data.
424 * The tricky case is when we read a corrupted data or crc. We must detect
425 * this here in order to determine that data can be transmitted to the hci
426 * core. This is the reason why we check the crc here.
427 * The CLF will repeat a frame until we send a RR on that frame.
428 *
429 * On ST21NFCA, IRQ goes in idle when read starts. As no size information are
430 * available in the incoming data, other IRQ might come. Every IRQ will trigger
431 * a read sequence with different length and will fill the current frame.
432 * The reception is complete once we reach a ST21NFCA_SOF_EOF.
433 */
434static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id)
435{
436 struct st21nfca_i2c_phy *phy = phy_id;
437 struct i2c_client *client;
438
439 int r;
440
441 if (!phy || irq != phy->i2c_dev->irq) {
442 WARN_ON_ONCE(1);
443 return IRQ_NONE;
444 }
445
446 client = phy->i2c_dev;
447 dev_dbg(&client->dev, "IRQ\n");
448
449 if (phy->hard_fault != 0)
450 return IRQ_HANDLED;
451
452 r = st21nfca_hci_i2c_read(phy, phy->pending_skb);
453 if (r == -EREMOTEIO) {
454 phy->hard_fault = r;
455
456 nfc_hci_recv_frame(phy->hdev, NULL);
457
458 return IRQ_HANDLED;
459 } else if (r == -EAGAIN || r == -EIO) {
460 return IRQ_HANDLED;
461 } else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) {
462 /*
463 * With ST21NFCA, only one interface (I2C, RF or SWP)
464 * may be active at a time.
465 * Having incorrect crc is usually due to i2c macrocell
466 * deactivation in the middle of a transmission.
467 * It may generate corrupted data on i2c.
468 * We give sometime to get i2c back.
469 * The complete frame will be repeated.
470 */
471 msleep(wait_tab[phy->crc_trials]);
472 phy->crc_trials++;
473 phy->current_read_len = 0;
474 } else if (r > 0) {
475 /*
476 * We succeeded to read data from the CLF and
477 * data is valid.
478 * Reset counter.
479 */
480 nfc_hci_recv_frame(phy->hdev, phy->pending_skb);
481 phy->crc_trials = 0;
482 }
483
484 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
485 if (phy->pending_skb == NULL) {
486 phy->hard_fault = -ENOMEM;
487 nfc_hci_recv_frame(phy->hdev, NULL);
488 }
489
490 return IRQ_HANDLED;
491}
492
493static struct nfc_phy_ops i2c_phy_ops = {
494 .write = st21nfca_hci_i2c_write,
495 .enable = st21nfca_hci_i2c_enable,
496 .disable = st21nfca_hci_i2c_disable,
497};
498
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200499static int st21nfca_hci_i2c_request_resources(struct i2c_client *client)
Christophe Ricard68957302014-03-25 06:51:47 +0100500{
501 struct st21nfca_nfc_platform_data *pdata;
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200502 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
Christophe Ricard68957302014-03-25 06:51:47 +0100503 int r;
504
505 pdata = client->dev.platform_data;
506 if (pdata == NULL) {
507 nfc_err(&client->dev, "No platform data\n");
508 return -EINVAL;
509 }
510
511 /* store for later use */
512 phy->gpio_irq = pdata->gpio_irq;
513 phy->gpio_ena = pdata->gpio_ena;
514 phy->irq_polarity = pdata->irq_polarity;
Christophe Ricard68957302014-03-25 06:51:47 +0100515
516 r = devm_gpio_request(&client->dev, phy->gpio_irq, "wake_up");
517 if (r) {
518 pr_err("%s : gpio_request failed\n", __FILE__);
519 return -ENODEV;
520 }
521
522 r = gpio_direction_input(phy->gpio_irq);
523 if (r) {
524 pr_err("%s : gpio_direction_input failed\n", __FILE__);
525 return -ENODEV;
526 }
527
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200528 if (phy->gpio_ena > 0) {
Christophe Ricard68957302014-03-25 06:51:47 +0100529 r = devm_gpio_request(&client->dev,
530 phy->gpio_ena, "clf_enable");
531 if (r) {
532 pr_err("%s : ena gpio_request failed\n", __FILE__);
533 return -ENODEV;
534 }
535 r = gpio_direction_output(phy->gpio_ena, 1);
536
537 if (r) {
538 pr_err("%s : ena gpio_direction_output failed\n",
539 __FILE__);
540 return -ENODEV;
541 }
542 }
543
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200544 return 0;
Christophe Ricard68957302014-03-25 06:51:47 +0100545}
546
547static int st21nfca_hci_i2c_probe(struct i2c_client *client,
548 const struct i2c_device_id *id)
549{
550 struct st21nfca_i2c_phy *phy;
551 struct st21nfca_nfc_platform_data *pdata;
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200552 int r;
553 int irq;
Christophe Ricard68957302014-03-25 06:51:47 +0100554
555 dev_dbg(&client->dev, "%s\n", __func__);
556 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
557
558 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
559 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
560 return -ENODEV;
561 }
562
563 phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy),
564 GFP_KERNEL);
565 if (!phy) {
566 nfc_err(&client->dev,
567 "Cannot allocate memory for st21nfca i2c phy.\n");
568 return -ENOMEM;
569 }
570
571 phy->i2c_dev = client;
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200572 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
573 if (phy->pending_skb == NULL)
574 return -ENOMEM;
Christophe Ricard68957302014-03-25 06:51:47 +0100575
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200576 phy->current_read_len = 0;
577 phy->crc_trials = 0;
Christophe Ricard68957302014-03-25 06:51:47 +0100578 i2c_set_clientdata(client, phy);
579
580 pdata = client->dev.platform_data;
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200581 if (!pdata) {
Christophe Ricard68957302014-03-25 06:51:47 +0100582 nfc_err(&client->dev, "No platform data\n");
583 return -EINVAL;
584 }
585
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200586 r = st21nfca_hci_i2c_request_resources(client);
Christophe Ricard68957302014-03-25 06:51:47 +0100587 if (r) {
588 nfc_err(&client->dev, "Cannot get platform resources\n");
589 return r;
590 }
591
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200592 /* IRQ */
593 irq = gpio_to_irq(phy->gpio_irq);
594 if (irq < 0) {
595 nfc_err(&client->dev,
596 "Unable to get irq number for GPIO %d error %d\n",
597 phy->gpio_irq, r);
598 return -ENODEV;
599 }
600 client->irq = irq;
601
Christophe Ricard18d2c622014-04-01 00:34:07 +0200602 r = st21nfca_hci_platform_init(phy);
603 if (r < 0) {
604 nfc_err(&client->dev, "Unable to reboot st21nfca\n");
605 return -ENODEV;
606 }
607
Christophe Ricard68957302014-03-25 06:51:47 +0100608 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
609 st21nfca_hci_irq_thread_fn,
610 phy->irq_polarity | IRQF_ONESHOT,
611 ST21NFCA_HCI_DRIVER_NAME, phy);
612 if (r < 0) {
613 nfc_err(&client->dev, "Unable to register IRQ handler\n");
614 return r;
615 }
616
Christophe Ricard9bac75d2014-04-01 00:34:05 +0200617 return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
Christophe Ricard68957302014-03-25 06:51:47 +0100618 ST21NFCA_FRAME_HEADROOM, ST21NFCA_FRAME_TAILROOM,
619 ST21NFCA_HCI_LLC_MAX_PAYLOAD, &phy->hdev);
Christophe Ricard68957302014-03-25 06:51:47 +0100620}
621
622static int st21nfca_hci_i2c_remove(struct i2c_client *client)
623{
624 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
625
626 dev_dbg(&client->dev, "%s\n", __func__);
627
628 st21nfca_hci_remove(phy->hdev);
629
630 if (phy->powered)
631 st21nfca_hci_i2c_disable(phy);
632
633 return 0;
634}
635
636static struct i2c_driver st21nfca_hci_i2c_driver = {
637 .driver = {
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200638 .owner = THIS_MODULE,
639 .name = ST21NFCA_HCI_I2C_DRIVER_NAME,
640 },
Christophe Ricard68957302014-03-25 06:51:47 +0100641 .probe = st21nfca_hci_i2c_probe,
642 .id_table = st21nfca_hci_i2c_id_table,
643 .remove = st21nfca_hci_i2c_remove,
644};
645
646module_i2c_driver(st21nfca_hci_i2c_driver);
647
648MODULE_LICENSE("GPL");
649MODULE_DESCRIPTION(DRIVER_DESC);