blob: 72ad36873bdfc24396e46e8757a5ae9fa44137d4 [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>
Christophe Ricardc44cb2e2014-05-13 22:03:39 +020024#include <linux/of_irq.h>
25#include <linux/of_gpio.h>
Christophe Ricard68957302014-03-25 06:51:47 +010026#include <linux/miscdevice.h>
27#include <linux/interrupt.h>
28#include <linux/delay.h>
29#include <linux/nfc.h>
30#include <linux/firmware.h>
31#include <linux/unaligned/access_ok.h>
32#include <linux/platform_data/st21nfca.h>
33
34#include <net/nfc/hci.h>
35#include <net/nfc/llc.h>
36#include <net/nfc/nfc.h>
37
38#include "st21nfca.h"
39
40/*
41 * Every frame starts with ST21NFCA_SOF_EOF and ends with ST21NFCA_SOF_EOF.
42 * Because ST21NFCA_SOF_EOF is a possible data value, there is a mecanism
43 * called byte stuffing has been introduced.
44 *
45 * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
46 * - insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
47 * - xor byte with ST21NFCA_BYTE_STUFFING_MASK
48 */
49#define ST21NFCA_SOF_EOF 0x7e
50#define ST21NFCA_BYTE_STUFFING_MASK 0x20
51#define ST21NFCA_ESCAPE_BYTE_STUFFING 0x7d
52
Christophe Ricarde1fb97b2014-04-24 23:19:31 +020053/* SOF + 00 */
Christophe Ricard68957302014-03-25 06:51:47 +010054#define ST21NFCA_FRAME_HEADROOM 2
55
Christophe Ricarde1fb97b2014-04-24 23:19:31 +020056/* 2 bytes crc + EOF */
57#define ST21NFCA_FRAME_TAILROOM 3
Christophe Ricardc97ffdb2014-04-24 23:19:33 +020058#define IS_START_OF_FRAME(buf) (buf[0] == ST21NFCA_SOF_EOF && \
59 buf[1] == 0)
Christophe Ricard68957302014-03-25 06:51:47 +010060
61#define ST21NFCA_HCI_I2C_DRIVER_NAME "st21nfca_hci_i2c"
62
63static struct i2c_device_id st21nfca_hci_i2c_id_table[] = {
64 {ST21NFCA_HCI_DRIVER_NAME, 0},
65 {}
66};
67
68MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table);
69
70struct st21nfca_i2c_phy {
71 struct i2c_client *i2c_dev;
72 struct nfc_hci_dev *hdev;
73
74 unsigned int gpio_ena;
75 unsigned int gpio_irq;
76 unsigned int irq_polarity;
77
78 struct sk_buff *pending_skb;
79 int current_read_len;
80 /*
81 * crc might have fail because i2c macro
82 * is disable due to other interface activity
83 */
84 int crc_trials;
85
86 int powered;
87 int run_mode;
88
89 /*
90 * < 0 if hardware error occured (e.g. i2c err)
91 * and prevents normal operation.
92 */
93 int hard_fault;
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +020094 struct mutex phy_lock;
Christophe Ricard68957302014-03-25 06:51:47 +010095};
Christophe Ricard05311072014-05-20 22:21:56 +020096static u8 len_seq[] = { 16, 24, 12, 29 };
Christophe Ricard68957302014-03-25 06:51:47 +010097static u16 wait_tab[] = { 2, 3, 5, 15, 20, 40};
98
99#define I2C_DUMP_SKB(info, skb) \
100do { \
101 pr_debug("%s:\n", info); \
102 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
103 16, 1, (skb)->data, (skb)->len, 0); \
104} while (0)
105
Christophe Ricard18d2c622014-04-01 00:34:07 +0200106/*
107 * In order to get the CLF in a known state we generate an internal reboot
108 * using a proprietary command.
109 * Once the reboot is completed, we expect to receive a ST21NFCA_SOF_EOF
110 * fill buffer.
111 */
112static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy)
Christophe Ricard68957302014-03-25 06:51:47 +0100113{
Christophe Ricardc5b0c372014-04-01 00:34:03 +0200114 u16 wait_reboot[] = { 50, 300, 1000 };
Christophe Ricard68957302014-03-25 06:51:47 +0100115 char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E };
116 u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE];
117 int i, r = -1;
118
Christophe Ricard18d2c622014-04-01 00:34:07 +0200119 for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
Christophe Ricard68957302014-03-25 06:51:47 +0100120 r = i2c_master_send(phy->i2c_dev, reboot_cmd,
121 sizeof(reboot_cmd));
Christophe Ricard18d2c622014-04-01 00:34:07 +0200122 if (r < 0)
123 msleep(wait_reboot[i]);
124 }
125 if (r < 0)
126 return r;
Christophe Ricard68957302014-03-25 06:51:47 +0100127
Christophe Ricard18d2c622014-04-01 00:34:07 +0200128 /* CLF is spending about 20ms to do an internal reboot */
129 msleep(20);
130 r = -1;
131 for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
132 r = i2c_master_recv(phy->i2c_dev, tmp,
133 ST21NFCA_HCI_LLC_MAX_SIZE);
134 if (r < 0)
135 msleep(wait_reboot[i]);
136 }
137 if (r < 0)
138 return r;
139
140 for (i = 0; i < ST21NFCA_HCI_LLC_MAX_SIZE &&
141 tmp[i] == ST21NFCA_SOF_EOF; i++)
142 ;
143
144 if (r != ST21NFCA_HCI_LLC_MAX_SIZE)
145 return -ENODEV;
146
147 usleep_range(1000, 1500);
148 return 0;
Christophe Ricard68957302014-03-25 06:51:47 +0100149}
150
151static int st21nfca_hci_i2c_enable(void *phy_id)
152{
153 struct st21nfca_i2c_phy *phy = phy_id;
154
155 gpio_set_value(phy->gpio_ena, 1);
156 phy->powered = 1;
157 phy->run_mode = ST21NFCA_HCI_MODE;
158
159 usleep_range(10000, 15000);
160
161 return 0;
162}
163
164static void st21nfca_hci_i2c_disable(void *phy_id)
165{
166 struct st21nfca_i2c_phy *phy = phy_id;
167
168 pr_info("\n");
169 gpio_set_value(phy->gpio_ena, 0);
170
171 phy->powered = 0;
172}
173
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200174static void st21nfca_hci_add_len_crc(struct sk_buff *skb)
Christophe Ricard68957302014-03-25 06:51:47 +0100175{
Christophe Ricard68957302014-03-25 06:51:47 +0100176 u16 crc;
177 u8 tmp;
178
179 *skb_push(skb, 1) = 0;
180
181 crc = crc_ccitt(0xffff, skb->data, skb->len);
182 crc = ~crc;
183
184 tmp = crc & 0x00ff;
185 *skb_put(skb, 1) = tmp;
186
187 tmp = (crc >> 8) & 0x00ff;
188 *skb_put(skb, 1) = tmp;
Christophe Ricard68957302014-03-25 06:51:47 +0100189}
190
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200191static void st21nfca_hci_remove_len_crc(struct sk_buff *skb)
Christophe Ricard68957302014-03-25 06:51:47 +0100192{
193 skb_pull(skb, ST21NFCA_FRAME_HEADROOM);
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200194 skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM);
Christophe Ricard68957302014-03-25 06:51:47 +0100195}
196
197/*
198 * Writing a frame must not return the number of written bytes.
199 * It must return either zero for success, or <0 for error.
200 * In addition, it must not alter the skb
201 */
202static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb)
203{
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200204 int r = -1, i, j;
Christophe Ricard68957302014-03-25 06:51:47 +0100205 struct st21nfca_i2c_phy *phy = phy_id;
206 struct i2c_client *client = phy->i2c_dev;
Christophe Ricard68957302014-03-25 06:51:47 +0100207 u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2];
208
209 I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb);
210
211
212 if (phy->hard_fault != 0)
213 return phy->hard_fault;
214
215 /*
216 * Compute CRC before byte stuffing computation on frame
217 * Note st21nfca_hci_add_len_crc is doing a byte stuffing
218 * on its own value
219 */
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200220 st21nfca_hci_add_len_crc(skb);
Christophe Ricard68957302014-03-25 06:51:47 +0100221
222 /* add ST21NFCA_SOF_EOF on tail */
223 *skb_put(skb, 1) = ST21NFCA_SOF_EOF;
224 /* add ST21NFCA_SOF_EOF on head */
225 *skb_push(skb, 1) = ST21NFCA_SOF_EOF;
226
227 /*
228 * Compute byte stuffing
229 * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
230 * insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
231 * xor byte with ST21NFCA_BYTE_STUFFING_MASK
232 */
233 tmp[0] = skb->data[0];
234 for (i = 1, j = 1; i < skb->len - 1; i++, j++) {
235 if (skb->data[i] == ST21NFCA_SOF_EOF
236 || skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) {
237 tmp[j] = ST21NFCA_ESCAPE_BYTE_STUFFING;
238 j++;
239 tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK;
240 } else {
241 tmp[j] = skb->data[i];
242 }
243 }
244 tmp[j] = skb->data[i];
245 j++;
246
247 /*
248 * Manage sleep mode
249 * Try 3 times to send data with delay between each
250 */
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +0200251 mutex_lock(&phy->phy_lock);
Christophe Ricard68957302014-03-25 06:51:47 +0100252 for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++) {
253 r = i2c_master_send(client, tmp, j);
254 if (r < 0)
255 msleep(wait_tab[i]);
256 }
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +0200257 mutex_unlock(&phy->phy_lock);
Christophe Ricard68957302014-03-25 06:51:47 +0100258
259 if (r >= 0) {
260 if (r != j)
261 r = -EREMOTEIO;
262 else
263 r = 0;
264 }
265
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200266 st21nfca_hci_remove_len_crc(skb);
Christophe Ricard68957302014-03-25 06:51:47 +0100267
268 return r;
269}
270
271static int get_frame_size(u8 *buf, int buflen)
272{
273 int len = 0;
Christophe Ricard3e6df912014-07-28 18:11:31 +0200274
Christophe Ricard68957302014-03-25 06:51:47 +0100275 if (buf[len + 1] == ST21NFCA_SOF_EOF)
276 return 0;
277
278 for (len = 1; len < buflen && buf[len] != ST21NFCA_SOF_EOF; len++)
279 ;
280
281 return len;
282}
283
284static int check_crc(u8 *buf, int buflen)
285{
286 u16 crc;
287
288 crc = crc_ccitt(0xffff, buf, buflen - 2);
289 crc = ~crc;
290
291 if (buf[buflen - 2] != (crc & 0xff) || buf[buflen - 1] != (crc >> 8)) {
292 pr_err(ST21NFCA_HCI_DRIVER_NAME
293 ": CRC error 0x%x != 0x%x 0x%x\n", crc, buf[buflen - 1],
294 buf[buflen - 2]);
295
296 pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
297 print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
298 16, 2, buf, buflen, false);
299 return -EPERM;
300 }
301 return 0;
302}
303
304/*
305 * Prepare received data for upper layer.
306 * Received data include byte stuffing, crc and sof/eof
307 * which is not usable by hci part.
308 * returns:
309 * frame size without sof/eof, header and byte stuffing
310 * -EBADMSG : frame was incorrect and discarded
311 */
312static int st21nfca_hci_i2c_repack(struct sk_buff *skb)
313{
314 int i, j, r, size;
Christophe Ricard3e6df912014-07-28 18:11:31 +0200315
Christophe Ricard68957302014-03-25 06:51:47 +0100316 if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0))
317 return -EBADMSG;
318
319 size = get_frame_size(skb->data, skb->len);
320 if (size > 0) {
321 skb_trim(skb, size);
322 /* remove ST21NFCA byte stuffing for upper layer */
323 for (i = 1, j = 0; i < skb->len; i++) {
Christophe Ricard3096e252014-04-24 23:19:32 +0200324 if (skb->data[i + j] ==
Christophe Ricard68957302014-03-25 06:51:47 +0100325 (u8) ST21NFCA_ESCAPE_BYTE_STUFFING) {
Christophe Ricard3096e252014-04-24 23:19:32 +0200326 skb->data[i] = skb->data[i + j + 1]
327 | ST21NFCA_BYTE_STUFFING_MASK;
Christophe Ricard68957302014-03-25 06:51:47 +0100328 i++;
329 j++;
330 }
331 skb->data[i] = skb->data[i + j];
332 }
333 /* remove byte stuffing useless byte */
334 skb_trim(skb, i - j);
335 /* remove ST21NFCA_SOF_EOF from head */
336 skb_pull(skb, 1);
337
338 r = check_crc(skb->data, skb->len);
339 if (r != 0) {
340 i = 0;
341 return -EBADMSG;
342 }
343
344 /* remove headbyte */
345 skb_pull(skb, 1);
346 /* remove crc. Byte Stuffing is already removed here */
347 skb_trim(skb, skb->len - 2);
348 return skb->len;
349 }
350 return 0;
351}
352
353/*
354 * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
355 * that i2c bus will be flushed and that next read will start on a new frame.
356 * returned skb contains only LLC header and payload.
357 * returns:
358 * frame size : if received frame is complete (find ST21NFCA_SOF_EOF at
359 * end of read)
360 * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF
361 * at end of read)
362 * -EREMOTEIO : i2c read error (fatal)
363 * -EBADMSG : frame was incorrect and discarded
364 * (value returned from st21nfca_hci_i2c_repack)
365 * -EIO : if no ST21NFCA_SOF_EOF is found after reaching
366 * the read length end sequence
367 */
368static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy,
369 struct sk_buff *skb)
370{
371 int r, i;
372 u8 len;
Christophe Ricardc97ffdb2014-04-24 23:19:33 +0200373 u8 buf[ST21NFCA_HCI_LLC_MAX_PAYLOAD];
Christophe Ricard68957302014-03-25 06:51:47 +0100374 struct i2c_client *client = phy->i2c_dev;
375
376 if (phy->current_read_len < ARRAY_SIZE(len_seq)) {
377 len = len_seq[phy->current_read_len];
378
379 /*
380 * Add retry mecanism
381 * Operation on I2C interface may fail in case of operation on
382 * RF or SWP interface
383 */
384 r = 0;
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +0200385 mutex_lock(&phy->phy_lock);
Christophe Ricard68957302014-03-25 06:51:47 +0100386 for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) {
Christophe Ricardc97ffdb2014-04-24 23:19:33 +0200387 r = i2c_master_recv(client, buf, len);
Christophe Ricard68957302014-03-25 06:51:47 +0100388 if (r < 0)
389 msleep(wait_tab[i]);
390 }
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +0200391 mutex_unlock(&phy->phy_lock);
Christophe Ricard68957302014-03-25 06:51:47 +0100392
393 if (r != len) {
394 phy->current_read_len = 0;
395 return -EREMOTEIO;
396 }
397
Christophe Ricardc97ffdb2014-04-24 23:19:33 +0200398 /*
399 * The first read sequence does not start with SOF.
400 * Data is corrupeted so we drop it.
401 */
Christophe Ricard8e9466c2014-05-20 22:21:55 +0200402 if (!phy->current_read_len && !IS_START_OF_FRAME(buf)) {
Christophe Ricardc97ffdb2014-04-24 23:19:33 +0200403 skb_trim(skb, 0);
404 phy->current_read_len = 0;
405 return -EIO;
Christophe Ricard8e9466c2014-05-20 22:21:55 +0200406 } else if (phy->current_read_len && IS_START_OF_FRAME(buf)) {
Christophe Ricardc97ffdb2014-04-24 23:19:33 +0200407 /*
408 * Previous frame transmission was interrupted and
409 * the frame got repeated.
410 * Received frame start with ST21NFCA_SOF_EOF + 00.
411 */
412 skb_trim(skb, 0);
413 phy->current_read_len = 0;
414 }
415
416 memcpy(skb_put(skb, len), buf, len);
417
418 if (skb->data[skb->len - 1] == ST21NFCA_SOF_EOF) {
Christophe Ricard68957302014-03-25 06:51:47 +0100419 phy->current_read_len = 0;
420 return st21nfca_hci_i2c_repack(skb);
421 }
422 phy->current_read_len++;
423 return -EAGAIN;
424 }
425 return -EIO;
426}
427
428/*
429 * Reads an shdlc frame from the chip. This is not as straightforward as it
430 * seems. The frame format is data-crc, and corruption can occur anywhere
431 * while transiting on i2c bus, such that we could read an invalid data.
432 * The tricky case is when we read a corrupted data or crc. We must detect
433 * this here in order to determine that data can be transmitted to the hci
434 * core. This is the reason why we check the crc here.
435 * The CLF will repeat a frame until we send a RR on that frame.
436 *
437 * On ST21NFCA, IRQ goes in idle when read starts. As no size information are
438 * available in the incoming data, other IRQ might come. Every IRQ will trigger
439 * a read sequence with different length and will fill the current frame.
440 * The reception is complete once we reach a ST21NFCA_SOF_EOF.
441 */
442static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id)
443{
444 struct st21nfca_i2c_phy *phy = phy_id;
445 struct i2c_client *client;
446
447 int r;
448
449 if (!phy || irq != phy->i2c_dev->irq) {
450 WARN_ON_ONCE(1);
451 return IRQ_NONE;
452 }
453
454 client = phy->i2c_dev;
455 dev_dbg(&client->dev, "IRQ\n");
456
457 if (phy->hard_fault != 0)
458 return IRQ_HANDLED;
459
460 r = st21nfca_hci_i2c_read(phy, phy->pending_skb);
461 if (r == -EREMOTEIO) {
462 phy->hard_fault = r;
463
464 nfc_hci_recv_frame(phy->hdev, NULL);
465
466 return IRQ_HANDLED;
467 } else if (r == -EAGAIN || r == -EIO) {
468 return IRQ_HANDLED;
469 } else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) {
470 /*
471 * With ST21NFCA, only one interface (I2C, RF or SWP)
472 * may be active at a time.
473 * Having incorrect crc is usually due to i2c macrocell
474 * deactivation in the middle of a transmission.
475 * It may generate corrupted data on i2c.
476 * We give sometime to get i2c back.
477 * The complete frame will be repeated.
478 */
479 msleep(wait_tab[phy->crc_trials]);
480 phy->crc_trials++;
481 phy->current_read_len = 0;
Christophe Ricard0c942b02014-04-24 23:19:35 +0200482 kfree_skb(phy->pending_skb);
Christophe Ricard68957302014-03-25 06:51:47 +0100483 } else if (r > 0) {
484 /*
485 * We succeeded to read data from the CLF and
486 * data is valid.
487 * Reset counter.
488 */
489 nfc_hci_recv_frame(phy->hdev, phy->pending_skb);
490 phy->crc_trials = 0;
Christophe Ricardcf577342014-05-20 22:21:54 +0200491 } else {
492 kfree_skb(phy->pending_skb);
Christophe Ricard68957302014-03-25 06:51:47 +0100493 }
494
495 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
496 if (phy->pending_skb == NULL) {
497 phy->hard_fault = -ENOMEM;
498 nfc_hci_recv_frame(phy->hdev, NULL);
499 }
500
501 return IRQ_HANDLED;
502}
503
504static struct nfc_phy_ops i2c_phy_ops = {
505 .write = st21nfca_hci_i2c_write,
506 .enable = st21nfca_hci_i2c_enable,
507 .disable = st21nfca_hci_i2c_disable,
508};
509
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200510#ifdef CONFIG_OF
511static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client)
512{
513 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
514 struct device_node *pp;
515 int gpio;
516 int r;
517
518 pp = client->dev.of_node;
519 if (!pp)
520 return -ENODEV;
521
522 /* Get GPIO from device tree */
523 gpio = of_get_named_gpio(pp, "enable-gpios", 0);
524 if (gpio < 0) {
525 nfc_err(&client->dev, "Failed to retrieve enable-gpios from device tree\n");
526 return gpio;
527 }
528
529 /* GPIO request and configuration */
Axel Lin0be8ce72014-07-28 18:11:29 +0200530 r = devm_gpio_request_one(&client->dev, gpio, GPIOF_OUT_INIT_HIGH,
531 "clf_enable");
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200532 if (r) {
533 nfc_err(&client->dev, "Failed to request enable pin\n");
534 return -ENODEV;
535 }
536
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200537 phy->gpio_ena = gpio;
538
539 /* IRQ */
540 r = irq_of_parse_and_map(pp, 0);
541 if (r < 0) {
542 nfc_err(&client->dev,
543 "Unable to get irq, error: %d\n", r);
544 return r;
545 }
546
547 phy->irq_polarity = irq_get_trigger_type(r);
548 client->irq = r;
549
550 return 0;
551}
552#else
553static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client)
554{
555 return -ENODEV;
556}
557#endif
558
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200559static int st21nfca_hci_i2c_request_resources(struct i2c_client *client)
Christophe Ricard68957302014-03-25 06:51:47 +0100560{
561 struct st21nfca_nfc_platform_data *pdata;
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200562 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
Christophe Ricard68957302014-03-25 06:51:47 +0100563 int r;
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200564 int irq;
Christophe Ricard68957302014-03-25 06:51:47 +0100565
566 pdata = client->dev.platform_data;
567 if (pdata == NULL) {
568 nfc_err(&client->dev, "No platform data\n");
569 return -EINVAL;
570 }
571
572 /* store for later use */
573 phy->gpio_irq = pdata->gpio_irq;
574 phy->gpio_ena = pdata->gpio_ena;
575 phy->irq_polarity = pdata->irq_polarity;
Christophe Ricard68957302014-03-25 06:51:47 +0100576
Axel Lin0be8ce72014-07-28 18:11:29 +0200577 r = devm_gpio_request_one(&client->dev, phy->gpio_irq, GPIOF_IN,
578 "wake_up");
Christophe Ricard68957302014-03-25 06:51:47 +0100579 if (r) {
580 pr_err("%s : gpio_request failed\n", __FILE__);
581 return -ENODEV;
582 }
583
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200584 if (phy->gpio_ena > 0) {
Axel Lin0be8ce72014-07-28 18:11:29 +0200585 r = devm_gpio_request_one(&client->dev, phy->gpio_ena,
586 GPIOF_OUT_INIT_HIGH, "clf_enable");
Christophe Ricard68957302014-03-25 06:51:47 +0100587 if (r) {
588 pr_err("%s : ena gpio_request failed\n", __FILE__);
589 return -ENODEV;
590 }
Christophe Ricard68957302014-03-25 06:51:47 +0100591 }
592
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200593 /* IRQ */
594 irq = gpio_to_irq(phy->gpio_irq);
595 if (irq < 0) {
596 nfc_err(&client->dev,
597 "Unable to get irq number for GPIO %d error %d\n",
598 phy->gpio_irq, r);
599 return -ENODEV;
600 }
601 client->irq = irq;
602
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200603 return 0;
Christophe Ricard68957302014-03-25 06:51:47 +0100604}
605
606static int st21nfca_hci_i2c_probe(struct i2c_client *client,
607 const struct i2c_device_id *id)
608{
609 struct st21nfca_i2c_phy *phy;
610 struct st21nfca_nfc_platform_data *pdata;
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200611 int r;
Christophe Ricard68957302014-03-25 06:51:47 +0100612
613 dev_dbg(&client->dev, "%s\n", __func__);
614 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
615
616 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
617 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
618 return -ENODEV;
619 }
620
621 phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy),
622 GFP_KERNEL);
623 if (!phy) {
624 nfc_err(&client->dev,
625 "Cannot allocate memory for st21nfca i2c phy.\n");
626 return -ENOMEM;
627 }
628
629 phy->i2c_dev = client;
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200630 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
631 if (phy->pending_skb == NULL)
632 return -ENOMEM;
Christophe Ricard68957302014-03-25 06:51:47 +0100633
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200634 phy->current_read_len = 0;
635 phy->crc_trials = 0;
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +0200636 mutex_init(&phy->phy_lock);
Christophe Ricard68957302014-03-25 06:51:47 +0100637 i2c_set_clientdata(client, phy);
638
639 pdata = client->dev.platform_data;
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200640 if (!pdata && client->dev.of_node) {
641 r = st21nfca_hci_i2c_of_request_resources(client);
642 if (r) {
643 nfc_err(&client->dev, "No platform data\n");
644 return r;
645 }
646 } else if (pdata) {
647 r = st21nfca_hci_i2c_request_resources(client);
648 if (r) {
649 nfc_err(&client->dev, "Cannot get platform resources\n");
650 return r;
651 }
652 } else {
653 nfc_err(&client->dev, "st21nfca platform resources not available\n");
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200654 return -ENODEV;
655 }
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200656
Christophe Ricard18d2c622014-04-01 00:34:07 +0200657 r = st21nfca_hci_platform_init(phy);
658 if (r < 0) {
659 nfc_err(&client->dev, "Unable to reboot st21nfca\n");
660 return -ENODEV;
661 }
662
Christophe Ricard68957302014-03-25 06:51:47 +0100663 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
664 st21nfca_hci_irq_thread_fn,
665 phy->irq_polarity | IRQF_ONESHOT,
666 ST21NFCA_HCI_DRIVER_NAME, phy);
667 if (r < 0) {
668 nfc_err(&client->dev, "Unable to register IRQ handler\n");
669 return r;
670 }
671
Christophe Ricard9bac75d2014-04-01 00:34:05 +0200672 return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
Christophe Ricard68957302014-03-25 06:51:47 +0100673 ST21NFCA_FRAME_HEADROOM, ST21NFCA_FRAME_TAILROOM,
674 ST21NFCA_HCI_LLC_MAX_PAYLOAD, &phy->hdev);
Christophe Ricard68957302014-03-25 06:51:47 +0100675}
676
677static int st21nfca_hci_i2c_remove(struct i2c_client *client)
678{
679 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
680
681 dev_dbg(&client->dev, "%s\n", __func__);
682
683 st21nfca_hci_remove(phy->hdev);
684
685 if (phy->powered)
686 st21nfca_hci_i2c_disable(phy);
687
688 return 0;
689}
690
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200691static const struct of_device_id of_st21nfca_i2c_match[] = {
692 { .compatible = "st,st21nfca_i2c", },
693 {}
694};
695
Christophe Ricard68957302014-03-25 06:51:47 +0100696static struct i2c_driver st21nfca_hci_i2c_driver = {
697 .driver = {
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200698 .owner = THIS_MODULE,
699 .name = ST21NFCA_HCI_I2C_DRIVER_NAME,
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200700 .of_match_table = of_match_ptr(of_st21nfca_i2c_match),
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200701 },
Christophe Ricard68957302014-03-25 06:51:47 +0100702 .probe = st21nfca_hci_i2c_probe,
703 .id_table = st21nfca_hci_i2c_id_table,
704 .remove = st21nfca_hci_i2c_remove,
705};
706
707module_i2c_driver(st21nfca_hci_i2c_driver);
708
709MODULE_LICENSE("GPL");
710MODULE_DESCRIPTION(DRIVER_DESC);