blob: b64d8e2e429a0ad3dd11840239d91c6dce69cb74 [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;
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +020092 struct mutex phy_lock;
Christophe Ricard68957302014-03-25 06:51:47 +010093};
94static u8 len_seq[] = { 13, 24, 15, 29 };
95static u16 wait_tab[] = { 2, 3, 5, 15, 20, 40};
96
97#define I2C_DUMP_SKB(info, skb) \
98do { \
99 pr_debug("%s:\n", info); \
100 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
101 16, 1, (skb)->data, (skb)->len, 0); \
102} while (0)
103
Christophe Ricard18d2c622014-04-01 00:34:07 +0200104/*
105 * In order to get the CLF in a known state we generate an internal reboot
106 * using a proprietary command.
107 * Once the reboot is completed, we expect to receive a ST21NFCA_SOF_EOF
108 * fill buffer.
109 */
110static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy)
Christophe Ricard68957302014-03-25 06:51:47 +0100111{
Christophe Ricardc5b0c372014-04-01 00:34:03 +0200112 u16 wait_reboot[] = { 50, 300, 1000 };
Christophe Ricard68957302014-03-25 06:51:47 +0100113 char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E };
114 u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE];
115 int i, r = -1;
116
Christophe Ricard18d2c622014-04-01 00:34:07 +0200117 for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
Christophe Ricard68957302014-03-25 06:51:47 +0100118 r = i2c_master_send(phy->i2c_dev, reboot_cmd,
119 sizeof(reboot_cmd));
Christophe Ricard18d2c622014-04-01 00:34:07 +0200120 if (r < 0)
121 msleep(wait_reboot[i]);
122 }
123 if (r < 0)
124 return r;
Christophe Ricard68957302014-03-25 06:51:47 +0100125
Christophe Ricard18d2c622014-04-01 00:34:07 +0200126 /* CLF is spending about 20ms to do an internal reboot */
127 msleep(20);
128 r = -1;
129 for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
130 r = i2c_master_recv(phy->i2c_dev, tmp,
131 ST21NFCA_HCI_LLC_MAX_SIZE);
132 if (r < 0)
133 msleep(wait_reboot[i]);
134 }
135 if (r < 0)
136 return r;
137
138 for (i = 0; i < ST21NFCA_HCI_LLC_MAX_SIZE &&
139 tmp[i] == ST21NFCA_SOF_EOF; i++)
140 ;
141
142 if (r != ST21NFCA_HCI_LLC_MAX_SIZE)
143 return -ENODEV;
144
145 usleep_range(1000, 1500);
146 return 0;
Christophe Ricard68957302014-03-25 06:51:47 +0100147}
148
149static int st21nfca_hci_i2c_enable(void *phy_id)
150{
151 struct st21nfca_i2c_phy *phy = phy_id;
152
153 gpio_set_value(phy->gpio_ena, 1);
154 phy->powered = 1;
155 phy->run_mode = ST21NFCA_HCI_MODE;
156
157 usleep_range(10000, 15000);
158
159 return 0;
160}
161
162static void st21nfca_hci_i2c_disable(void *phy_id)
163{
164 struct st21nfca_i2c_phy *phy = phy_id;
165
166 pr_info("\n");
167 gpio_set_value(phy->gpio_ena, 0);
168
169 phy->powered = 0;
170}
171
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200172static void st21nfca_hci_add_len_crc(struct sk_buff *skb)
Christophe Ricard68957302014-03-25 06:51:47 +0100173{
Christophe Ricard68957302014-03-25 06:51:47 +0100174 u16 crc;
175 u8 tmp;
176
177 *skb_push(skb, 1) = 0;
178
179 crc = crc_ccitt(0xffff, skb->data, skb->len);
180 crc = ~crc;
181
182 tmp = crc & 0x00ff;
183 *skb_put(skb, 1) = tmp;
184
185 tmp = (crc >> 8) & 0x00ff;
186 *skb_put(skb, 1) = tmp;
Christophe Ricard68957302014-03-25 06:51:47 +0100187}
188
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200189static void st21nfca_hci_remove_len_crc(struct sk_buff *skb)
Christophe Ricard68957302014-03-25 06:51:47 +0100190{
191 skb_pull(skb, ST21NFCA_FRAME_HEADROOM);
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200192 skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM);
Christophe Ricard68957302014-03-25 06:51:47 +0100193}
194
195/*
196 * Writing a frame must not return the number of written bytes.
197 * It must return either zero for success, or <0 for error.
198 * In addition, it must not alter the skb
199 */
200static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb)
201{
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200202 int r = -1, i, j;
Christophe Ricard68957302014-03-25 06:51:47 +0100203 struct st21nfca_i2c_phy *phy = phy_id;
204 struct i2c_client *client = phy->i2c_dev;
Christophe Ricard68957302014-03-25 06:51:47 +0100205 u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2];
206
207 I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb);
208
209
210 if (phy->hard_fault != 0)
211 return phy->hard_fault;
212
213 /*
214 * Compute CRC before byte stuffing computation on frame
215 * Note st21nfca_hci_add_len_crc is doing a byte stuffing
216 * on its own value
217 */
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200218 st21nfca_hci_add_len_crc(skb);
Christophe Ricard68957302014-03-25 06:51:47 +0100219
220 /* add ST21NFCA_SOF_EOF on tail */
221 *skb_put(skb, 1) = ST21NFCA_SOF_EOF;
222 /* add ST21NFCA_SOF_EOF on head */
223 *skb_push(skb, 1) = ST21NFCA_SOF_EOF;
224
225 /*
226 * Compute byte stuffing
227 * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
228 * insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
229 * xor byte with ST21NFCA_BYTE_STUFFING_MASK
230 */
231 tmp[0] = skb->data[0];
232 for (i = 1, j = 1; i < skb->len - 1; i++, j++) {
233 if (skb->data[i] == ST21NFCA_SOF_EOF
234 || skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) {
235 tmp[j] = ST21NFCA_ESCAPE_BYTE_STUFFING;
236 j++;
237 tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK;
238 } else {
239 tmp[j] = skb->data[i];
240 }
241 }
242 tmp[j] = skb->data[i];
243 j++;
244
245 /*
246 * Manage sleep mode
247 * Try 3 times to send data with delay between each
248 */
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +0200249 mutex_lock(&phy->phy_lock);
Christophe Ricard68957302014-03-25 06:51:47 +0100250 for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++) {
251 r = i2c_master_send(client, tmp, j);
252 if (r < 0)
253 msleep(wait_tab[i]);
254 }
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +0200255 mutex_unlock(&phy->phy_lock);
Christophe Ricard68957302014-03-25 06:51:47 +0100256
257 if (r >= 0) {
258 if (r != j)
259 r = -EREMOTEIO;
260 else
261 r = 0;
262 }
263
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200264 st21nfca_hci_remove_len_crc(skb);
Christophe Ricard68957302014-03-25 06:51:47 +0100265
266 return r;
267}
268
269static int get_frame_size(u8 *buf, int buflen)
270{
271 int len = 0;
272 if (buf[len + 1] == ST21NFCA_SOF_EOF)
273 return 0;
274
275 for (len = 1; len < buflen && buf[len] != ST21NFCA_SOF_EOF; len++)
276 ;
277
278 return len;
279}
280
281static int check_crc(u8 *buf, int buflen)
282{
283 u16 crc;
284
285 crc = crc_ccitt(0xffff, buf, buflen - 2);
286 crc = ~crc;
287
288 if (buf[buflen - 2] != (crc & 0xff) || buf[buflen - 1] != (crc >> 8)) {
289 pr_err(ST21NFCA_HCI_DRIVER_NAME
290 ": CRC error 0x%x != 0x%x 0x%x\n", crc, buf[buflen - 1],
291 buf[buflen - 2]);
292
293 pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
294 print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
295 16, 2, buf, buflen, false);
296 return -EPERM;
297 }
298 return 0;
299}
300
301/*
302 * Prepare received data for upper layer.
303 * Received data include byte stuffing, crc and sof/eof
304 * which is not usable by hci part.
305 * returns:
306 * frame size without sof/eof, header and byte stuffing
307 * -EBADMSG : frame was incorrect and discarded
308 */
309static int st21nfca_hci_i2c_repack(struct sk_buff *skb)
310{
311 int i, j, r, size;
312 if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0))
313 return -EBADMSG;
314
315 size = get_frame_size(skb->data, skb->len);
316 if (size > 0) {
317 skb_trim(skb, size);
318 /* remove ST21NFCA byte stuffing for upper layer */
319 for (i = 1, j = 0; i < skb->len; i++) {
Christophe Ricard3096e252014-04-24 23:19:32 +0200320 if (skb->data[i + j] ==
Christophe Ricard68957302014-03-25 06:51:47 +0100321 (u8) ST21NFCA_ESCAPE_BYTE_STUFFING) {
Christophe Ricard3096e252014-04-24 23:19:32 +0200322 skb->data[i] = skb->data[i + j + 1]
323 | ST21NFCA_BYTE_STUFFING_MASK;
Christophe Ricard68957302014-03-25 06:51:47 +0100324 i++;
325 j++;
326 }
327 skb->data[i] = skb->data[i + j];
328 }
329 /* remove byte stuffing useless byte */
330 skb_trim(skb, i - j);
331 /* remove ST21NFCA_SOF_EOF from head */
332 skb_pull(skb, 1);
333
334 r = check_crc(skb->data, skb->len);
335 if (r != 0) {
336 i = 0;
337 return -EBADMSG;
338 }
339
340 /* remove headbyte */
341 skb_pull(skb, 1);
342 /* remove crc. Byte Stuffing is already removed here */
343 skb_trim(skb, skb->len - 2);
344 return skb->len;
345 }
346 return 0;
347}
348
349/*
350 * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
351 * that i2c bus will be flushed and that next read will start on a new frame.
352 * returned skb contains only LLC header and payload.
353 * returns:
354 * frame size : if received frame is complete (find ST21NFCA_SOF_EOF at
355 * end of read)
356 * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF
357 * at end of read)
358 * -EREMOTEIO : i2c read error (fatal)
359 * -EBADMSG : frame was incorrect and discarded
360 * (value returned from st21nfca_hci_i2c_repack)
361 * -EIO : if no ST21NFCA_SOF_EOF is found after reaching
362 * the read length end sequence
363 */
364static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy,
365 struct sk_buff *skb)
366{
367 int r, i;
368 u8 len;
Christophe Ricardc97ffdb2014-04-24 23:19:33 +0200369 u8 buf[ST21NFCA_HCI_LLC_MAX_PAYLOAD];
Christophe Ricard68957302014-03-25 06:51:47 +0100370 struct i2c_client *client = phy->i2c_dev;
371
372 if (phy->current_read_len < ARRAY_SIZE(len_seq)) {
373 len = len_seq[phy->current_read_len];
374
375 /*
376 * Add retry mecanism
377 * Operation on I2C interface may fail in case of operation on
378 * RF or SWP interface
379 */
380 r = 0;
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +0200381 mutex_lock(&phy->phy_lock);
Christophe Ricard68957302014-03-25 06:51:47 +0100382 for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) {
Christophe Ricardc97ffdb2014-04-24 23:19:33 +0200383 r = i2c_master_recv(client, buf, len);
Christophe Ricard68957302014-03-25 06:51:47 +0100384 if (r < 0)
385 msleep(wait_tab[i]);
386 }
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +0200387 mutex_unlock(&phy->phy_lock);
Christophe Ricard68957302014-03-25 06:51:47 +0100388
389 if (r != len) {
390 phy->current_read_len = 0;
391 return -EREMOTEIO;
392 }
393
Christophe Ricardc97ffdb2014-04-24 23:19:33 +0200394 /*
395 * The first read sequence does not start with SOF.
396 * Data is corrupeted so we drop it.
397 */
398 if (!phy->current_read_len && buf[0] != ST21NFCA_SOF_EOF) {
399 skb_trim(skb, 0);
400 phy->current_read_len = 0;
401 return -EIO;
402 } else if (phy->current_read_len &&
403 IS_START_OF_FRAME(buf)) {
404 /*
405 * Previous frame transmission was interrupted and
406 * the frame got repeated.
407 * Received frame start with ST21NFCA_SOF_EOF + 00.
408 */
409 skb_trim(skb, 0);
410 phy->current_read_len = 0;
411 }
412
413 memcpy(skb_put(skb, len), buf, len);
414
415 if (skb->data[skb->len - 1] == ST21NFCA_SOF_EOF) {
Christophe Ricard68957302014-03-25 06:51:47 +0100416 phy->current_read_len = 0;
417 return st21nfca_hci_i2c_repack(skb);
418 }
419 phy->current_read_len++;
420 return -EAGAIN;
421 }
422 return -EIO;
423}
424
425/*
426 * Reads an shdlc frame from the chip. This is not as straightforward as it
427 * seems. The frame format is data-crc, and corruption can occur anywhere
428 * while transiting on i2c bus, such that we could read an invalid data.
429 * The tricky case is when we read a corrupted data or crc. We must detect
430 * this here in order to determine that data can be transmitted to the hci
431 * core. This is the reason why we check the crc here.
432 * The CLF will repeat a frame until we send a RR on that frame.
433 *
434 * On ST21NFCA, IRQ goes in idle when read starts. As no size information are
435 * available in the incoming data, other IRQ might come. Every IRQ will trigger
436 * a read sequence with different length and will fill the current frame.
437 * The reception is complete once we reach a ST21NFCA_SOF_EOF.
438 */
439static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id)
440{
441 struct st21nfca_i2c_phy *phy = phy_id;
442 struct i2c_client *client;
443
444 int r;
445
446 if (!phy || irq != phy->i2c_dev->irq) {
447 WARN_ON_ONCE(1);
448 return IRQ_NONE;
449 }
450
451 client = phy->i2c_dev;
452 dev_dbg(&client->dev, "IRQ\n");
453
454 if (phy->hard_fault != 0)
455 return IRQ_HANDLED;
456
457 r = st21nfca_hci_i2c_read(phy, phy->pending_skb);
458 if (r == -EREMOTEIO) {
459 phy->hard_fault = r;
460
461 nfc_hci_recv_frame(phy->hdev, NULL);
462
463 return IRQ_HANDLED;
464 } else if (r == -EAGAIN || r == -EIO) {
465 return IRQ_HANDLED;
466 } else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) {
467 /*
468 * With ST21NFCA, only one interface (I2C, RF or SWP)
469 * may be active at a time.
470 * Having incorrect crc is usually due to i2c macrocell
471 * deactivation in the middle of a transmission.
472 * It may generate corrupted data on i2c.
473 * We give sometime to get i2c back.
474 * The complete frame will be repeated.
475 */
476 msleep(wait_tab[phy->crc_trials]);
477 phy->crc_trials++;
478 phy->current_read_len = 0;
479 } else if (r > 0) {
480 /*
481 * We succeeded to read data from the CLF and
482 * data is valid.
483 * Reset counter.
484 */
485 nfc_hci_recv_frame(phy->hdev, phy->pending_skb);
486 phy->crc_trials = 0;
487 }
488
489 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
490 if (phy->pending_skb == NULL) {
491 phy->hard_fault = -ENOMEM;
492 nfc_hci_recv_frame(phy->hdev, NULL);
493 }
494
495 return IRQ_HANDLED;
496}
497
498static struct nfc_phy_ops i2c_phy_ops = {
499 .write = st21nfca_hci_i2c_write,
500 .enable = st21nfca_hci_i2c_enable,
501 .disable = st21nfca_hci_i2c_disable,
502};
503
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200504static int st21nfca_hci_i2c_request_resources(struct i2c_client *client)
Christophe Ricard68957302014-03-25 06:51:47 +0100505{
506 struct st21nfca_nfc_platform_data *pdata;
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200507 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
Christophe Ricard68957302014-03-25 06:51:47 +0100508 int r;
509
510 pdata = client->dev.platform_data;
511 if (pdata == NULL) {
512 nfc_err(&client->dev, "No platform data\n");
513 return -EINVAL;
514 }
515
516 /* store for later use */
517 phy->gpio_irq = pdata->gpio_irq;
518 phy->gpio_ena = pdata->gpio_ena;
519 phy->irq_polarity = pdata->irq_polarity;
Christophe Ricard68957302014-03-25 06:51:47 +0100520
521 r = devm_gpio_request(&client->dev, phy->gpio_irq, "wake_up");
522 if (r) {
523 pr_err("%s : gpio_request failed\n", __FILE__);
524 return -ENODEV;
525 }
526
527 r = gpio_direction_input(phy->gpio_irq);
528 if (r) {
529 pr_err("%s : gpio_direction_input failed\n", __FILE__);
530 return -ENODEV;
531 }
532
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200533 if (phy->gpio_ena > 0) {
Christophe Ricard68957302014-03-25 06:51:47 +0100534 r = devm_gpio_request(&client->dev,
535 phy->gpio_ena, "clf_enable");
536 if (r) {
537 pr_err("%s : ena gpio_request failed\n", __FILE__);
538 return -ENODEV;
539 }
540 r = gpio_direction_output(phy->gpio_ena, 1);
541
542 if (r) {
543 pr_err("%s : ena gpio_direction_output failed\n",
544 __FILE__);
545 return -ENODEV;
546 }
547 }
548
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200549 return 0;
Christophe Ricard68957302014-03-25 06:51:47 +0100550}
551
552static int st21nfca_hci_i2c_probe(struct i2c_client *client,
553 const struct i2c_device_id *id)
554{
555 struct st21nfca_i2c_phy *phy;
556 struct st21nfca_nfc_platform_data *pdata;
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200557 int r;
558 int irq;
Christophe Ricard68957302014-03-25 06:51:47 +0100559
560 dev_dbg(&client->dev, "%s\n", __func__);
561 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
562
563 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
564 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
565 return -ENODEV;
566 }
567
568 phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy),
569 GFP_KERNEL);
570 if (!phy) {
571 nfc_err(&client->dev,
572 "Cannot allocate memory for st21nfca i2c phy.\n");
573 return -ENOMEM;
574 }
575
576 phy->i2c_dev = client;
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200577 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
578 if (phy->pending_skb == NULL)
579 return -ENOMEM;
Christophe Ricard68957302014-03-25 06:51:47 +0100580
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200581 phy->current_read_len = 0;
582 phy->crc_trials = 0;
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +0200583 mutex_init(&phy->phy_lock);
Christophe Ricard68957302014-03-25 06:51:47 +0100584 i2c_set_clientdata(client, phy);
585
586 pdata = client->dev.platform_data;
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200587 if (!pdata) {
Christophe Ricard68957302014-03-25 06:51:47 +0100588 nfc_err(&client->dev, "No platform data\n");
589 return -EINVAL;
590 }
591
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200592 r = st21nfca_hci_i2c_request_resources(client);
Christophe Ricard68957302014-03-25 06:51:47 +0100593 if (r) {
594 nfc_err(&client->dev, "Cannot get platform resources\n");
595 return r;
596 }
597
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200598 /* IRQ */
599 irq = gpio_to_irq(phy->gpio_irq);
600 if (irq < 0) {
601 nfc_err(&client->dev,
602 "Unable to get irq number for GPIO %d error %d\n",
603 phy->gpio_irq, r);
604 return -ENODEV;
605 }
606 client->irq = irq;
607
Christophe Ricard18d2c622014-04-01 00:34:07 +0200608 r = st21nfca_hci_platform_init(phy);
609 if (r < 0) {
610 nfc_err(&client->dev, "Unable to reboot st21nfca\n");
611 return -ENODEV;
612 }
613
Christophe Ricard68957302014-03-25 06:51:47 +0100614 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
615 st21nfca_hci_irq_thread_fn,
616 phy->irq_polarity | IRQF_ONESHOT,
617 ST21NFCA_HCI_DRIVER_NAME, phy);
618 if (r < 0) {
619 nfc_err(&client->dev, "Unable to register IRQ handler\n");
620 return r;
621 }
622
Christophe Ricard9bac75d2014-04-01 00:34:05 +0200623 return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
Christophe Ricard68957302014-03-25 06:51:47 +0100624 ST21NFCA_FRAME_HEADROOM, ST21NFCA_FRAME_TAILROOM,
625 ST21NFCA_HCI_LLC_MAX_PAYLOAD, &phy->hdev);
Christophe Ricard68957302014-03-25 06:51:47 +0100626}
627
628static int st21nfca_hci_i2c_remove(struct i2c_client *client)
629{
630 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
631
632 dev_dbg(&client->dev, "%s\n", __func__);
633
634 st21nfca_hci_remove(phy->hdev);
635
636 if (phy->powered)
637 st21nfca_hci_i2c_disable(phy);
638
639 return 0;
640}
641
642static struct i2c_driver st21nfca_hci_i2c_driver = {
643 .driver = {
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200644 .owner = THIS_MODULE,
645 .name = ST21NFCA_HCI_I2C_DRIVER_NAME,
646 },
Christophe Ricard68957302014-03-25 06:51:47 +0100647 .probe = st21nfca_hci_i2c_probe,
648 .id_table = st21nfca_hci_i2c_id_table,
649 .remove = st21nfca_hci_i2c_remove,
650};
651
652module_i2c_driver(st21nfca_hci_i2c_driver);
653
654MODULE_LICENSE("GPL");
655MODULE_DESCRIPTION(DRIVER_DESC);