Linux-libre 3.17.4-gnu
[librecmc/linux-libre.git] / drivers / nfc / st21nfca / i2c.c
1 /*
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/of_irq.h>
25 #include <linux/of_gpio.h>
26 #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
53 /* SOF + 00 */
54 #define ST21NFCA_FRAME_HEADROOM                 2
55
56 /* 2 bytes crc + EOF */
57 #define ST21NFCA_FRAME_TAILROOM 3
58 #define IS_START_OF_FRAME(buf) (buf[0] == ST21NFCA_SOF_EOF && \
59                                 buf[1] == 0)
60
61 #define ST21NFCA_HCI_I2C_DRIVER_NAME "st21nfca_hci_i2c"
62
63 static struct i2c_device_id st21nfca_hci_i2c_id_table[] = {
64         {ST21NFCA_HCI_DRIVER_NAME, 0},
65         {}
66 };
67
68 MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table);
69
70 struct 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;
94         struct mutex phy_lock;
95 };
96 static u8 len_seq[] = { 16, 24, 12, 29 };
97 static u16 wait_tab[] = { 2, 3, 5, 15, 20, 40};
98
99 #define I2C_DUMP_SKB(info, skb)                                 \
100 do {                                                            \
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
106 /*
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  */
112 static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy)
113 {
114         u16 wait_reboot[] = { 50, 300, 1000 };
115         char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E };
116         u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE];
117         int i, r = -1;
118
119         for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
120                 r = i2c_master_send(phy->i2c_dev, reboot_cmd,
121                                     sizeof(reboot_cmd));
122                 if (r < 0)
123                         msleep(wait_reboot[i]);
124         }
125         if (r < 0)
126                 return r;
127
128         /* 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;
149 }
150
151 static 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
164 static 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
174 static void st21nfca_hci_add_len_crc(struct sk_buff *skb)
175 {
176         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;
189 }
190
191 static void st21nfca_hci_remove_len_crc(struct sk_buff *skb)
192 {
193         skb_pull(skb, ST21NFCA_FRAME_HEADROOM);
194         skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM);
195 }
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  */
202 static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb)
203 {
204         int r = -1, i, j;
205         struct st21nfca_i2c_phy *phy = phy_id;
206         struct i2c_client *client = phy->i2c_dev;
207         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          */
220         st21nfca_hci_add_len_crc(skb);
221
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          */
251         mutex_lock(&phy->phy_lock);
252         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         }
257         mutex_unlock(&phy->phy_lock);
258
259         if (r >= 0) {
260                 if (r != j)
261                         r = -EREMOTEIO;
262                 else
263                         r = 0;
264         }
265
266         st21nfca_hci_remove_len_crc(skb);
267
268         return r;
269 }
270
271 static int get_frame_size(u8 *buf, int buflen)
272 {
273         int len = 0;
274         if (buf[len + 1] == ST21NFCA_SOF_EOF)
275                 return 0;
276
277         for (len = 1; len < buflen && buf[len] != ST21NFCA_SOF_EOF; len++)
278                 ;
279
280         return len;
281 }
282
283 static int check_crc(u8 *buf, int buflen)
284 {
285         u16 crc;
286
287         crc = crc_ccitt(0xffff, buf, buflen - 2);
288         crc = ~crc;
289
290         if (buf[buflen - 2] != (crc & 0xff) || buf[buflen - 1] != (crc >> 8)) {
291                 pr_err(ST21NFCA_HCI_DRIVER_NAME
292                        ": CRC error 0x%x != 0x%x 0x%x\n", crc, buf[buflen - 1],
293                        buf[buflen - 2]);
294
295                 pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
296                 print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
297                                16, 2, buf, buflen, false);
298                 return -EPERM;
299         }
300         return 0;
301 }
302
303 /*
304  * Prepare received data for upper layer.
305  * Received data include byte stuffing, crc and sof/eof
306  * which is not usable by hci part.
307  * returns:
308  * frame size without sof/eof, header and byte stuffing
309  * -EBADMSG : frame was incorrect and discarded
310  */
311 static int st21nfca_hci_i2c_repack(struct sk_buff *skb)
312 {
313         int i, j, r, size;
314         if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0))
315                 return -EBADMSG;
316
317         size = get_frame_size(skb->data, skb->len);
318         if (size > 0) {
319                 skb_trim(skb, size);
320                 /* remove ST21NFCA byte stuffing for upper layer */
321                 for (i = 1, j = 0; i < skb->len; i++) {
322                         if (skb->data[i + j] ==
323                                         (u8) ST21NFCA_ESCAPE_BYTE_STUFFING) {
324                                 skb->data[i] = skb->data[i + j + 1]
325                                                 | ST21NFCA_BYTE_STUFFING_MASK;
326                                 i++;
327                                 j++;
328                         }
329                         skb->data[i] = skb->data[i + j];
330                 }
331                 /* remove byte stuffing useless byte */
332                 skb_trim(skb, i - j);
333                 /* remove ST21NFCA_SOF_EOF from head */
334                 skb_pull(skb, 1);
335
336                 r = check_crc(skb->data, skb->len);
337                 if (r != 0) {
338                         i = 0;
339                         return -EBADMSG;
340                 }
341
342                 /* remove headbyte */
343                 skb_pull(skb, 1);
344                 /* remove crc. Byte Stuffing is already removed here */
345                 skb_trim(skb, skb->len - 2);
346                 return skb->len;
347         }
348         return 0;
349 }
350
351 /*
352  * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
353  * that i2c bus will be flushed and that next read will start on a new frame.
354  * returned skb contains only LLC header and payload.
355  * returns:
356  * frame size : if received frame is complete (find ST21NFCA_SOF_EOF at
357  * end of read)
358  * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF
359  * at end of read)
360  * -EREMOTEIO : i2c read error (fatal)
361  * -EBADMSG : frame was incorrect and discarded
362  * (value returned from st21nfca_hci_i2c_repack)
363  * -EIO : if no ST21NFCA_SOF_EOF is found after reaching
364  * the read length end sequence
365  */
366 static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy,
367                                  struct sk_buff *skb)
368 {
369         int r, i;
370         u8 len;
371         u8 buf[ST21NFCA_HCI_LLC_MAX_PAYLOAD];
372         struct i2c_client *client = phy->i2c_dev;
373
374         if (phy->current_read_len < ARRAY_SIZE(len_seq)) {
375                 len = len_seq[phy->current_read_len];
376
377                 /*
378                  * Add retry mecanism
379                  * Operation on I2C interface may fail in case of operation on
380                  * RF or SWP interface
381                  */
382                 r = 0;
383                 mutex_lock(&phy->phy_lock);
384                 for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) {
385                         r = i2c_master_recv(client, buf, len);
386                         if (r < 0)
387                                 msleep(wait_tab[i]);
388                 }
389                 mutex_unlock(&phy->phy_lock);
390
391                 if (r != len) {
392                         phy->current_read_len = 0;
393                         return -EREMOTEIO;
394                 }
395
396                 /*
397                  * The first read sequence does not start with SOF.
398                  * Data is corrupeted so we drop it.
399                  */
400                 if (!phy->current_read_len && !IS_START_OF_FRAME(buf)) {
401                         skb_trim(skb, 0);
402                         phy->current_read_len = 0;
403                         return -EIO;
404                 } else if (phy->current_read_len && IS_START_OF_FRAME(buf)) {
405                         /*
406                          * Previous frame transmission was interrupted and
407                          * the frame got repeated.
408                          * Received frame start with ST21NFCA_SOF_EOF + 00.
409                          */
410                         skb_trim(skb, 0);
411                         phy->current_read_len = 0;
412                 }
413
414                 memcpy(skb_put(skb, len), buf, len);
415
416                 if (skb->data[skb->len - 1] == ST21NFCA_SOF_EOF) {
417                         phy->current_read_len = 0;
418                         return st21nfca_hci_i2c_repack(skb);
419                 }
420                 phy->current_read_len++;
421                 return -EAGAIN;
422         }
423         return -EIO;
424 }
425
426 /*
427  * Reads an shdlc frame from the chip. This is not as straightforward as it
428  * seems. The frame format is data-crc, and corruption can occur anywhere
429  * while transiting on i2c bus, such that we could read an invalid data.
430  * The tricky case is when we read a corrupted data or crc. We must detect
431  * this here in order to determine that data can be transmitted to the hci
432  * core. This is the reason why we check the crc here.
433  * The CLF will repeat a frame until we send a RR on that frame.
434  *
435  * On ST21NFCA, IRQ goes in idle when read starts. As no size information are
436  * available in the incoming data, other IRQ might come. Every IRQ will trigger
437  * a read sequence with different length and will fill the current frame.
438  * The reception is complete once we reach a ST21NFCA_SOF_EOF.
439  */
440 static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id)
441 {
442         struct st21nfca_i2c_phy *phy = phy_id;
443         struct i2c_client *client;
444
445         int r;
446
447         if (!phy || irq != phy->i2c_dev->irq) {
448                 WARN_ON_ONCE(1);
449                 return IRQ_NONE;
450         }
451
452         client = phy->i2c_dev;
453         dev_dbg(&client->dev, "IRQ\n");
454
455         if (phy->hard_fault != 0)
456                 return IRQ_HANDLED;
457
458         r = st21nfca_hci_i2c_read(phy, phy->pending_skb);
459         if (r == -EREMOTEIO) {
460                 phy->hard_fault = r;
461
462                 nfc_hci_recv_frame(phy->hdev, NULL);
463
464                 return IRQ_HANDLED;
465         } else if (r == -EAGAIN || r == -EIO) {
466                 return IRQ_HANDLED;
467         } else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) {
468                 /*
469                  * With ST21NFCA, only one interface (I2C, RF or SWP)
470                  * may be active at a time.
471                  * Having incorrect crc is usually due to i2c macrocell
472                  * deactivation in the middle of a transmission.
473                  * It may generate corrupted data on i2c.
474                  * We give sometime to get i2c back.
475                  * The complete frame will be repeated.
476                  */
477                 msleep(wait_tab[phy->crc_trials]);
478                 phy->crc_trials++;
479                 phy->current_read_len = 0;
480                 kfree_skb(phy->pending_skb);
481         } else if (r > 0) {
482                 /*
483                  * We succeeded to read data from the CLF and
484                  * data is valid.
485                  * Reset counter.
486                  */
487                 nfc_hci_recv_frame(phy->hdev, phy->pending_skb);
488                 phy->crc_trials = 0;
489         } else {
490                 kfree_skb(phy->pending_skb);
491         }
492
493         phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
494         if (phy->pending_skb == NULL) {
495                 phy->hard_fault = -ENOMEM;
496                 nfc_hci_recv_frame(phy->hdev, NULL);
497         }
498
499         return IRQ_HANDLED;
500 }
501
502 static struct nfc_phy_ops i2c_phy_ops = {
503         .write = st21nfca_hci_i2c_write,
504         .enable = st21nfca_hci_i2c_enable,
505         .disable = st21nfca_hci_i2c_disable,
506 };
507
508 #ifdef CONFIG_OF
509 static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client)
510 {
511         struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
512         struct device_node *pp;
513         int gpio;
514         int r;
515
516         pp = client->dev.of_node;
517         if (!pp)
518                 return -ENODEV;
519
520         /* Get GPIO from device tree */
521         gpio = of_get_named_gpio(pp, "enable-gpios", 0);
522         if (gpio < 0) {
523                 nfc_err(&client->dev, "Failed to retrieve enable-gpios from device tree\n");
524                 return gpio;
525         }
526
527         /* GPIO request and configuration */
528         r = devm_gpio_request(&client->dev, gpio, "clf_enable");
529         if (r) {
530                 nfc_err(&client->dev, "Failed to request enable pin\n");
531                 return -ENODEV;
532         }
533
534         r = gpio_direction_output(gpio, 1);
535         if (r) {
536                 nfc_err(&client->dev, "Failed to set enable pin direction as output\n");
537                 return -ENODEV;
538         }
539         phy->gpio_ena = gpio;
540
541         /* IRQ */
542         r = irq_of_parse_and_map(pp, 0);
543         if (r < 0) {
544                 nfc_err(&client->dev,
545                                 "Unable to get irq, error: %d\n", r);
546                 return r;
547         }
548
549         phy->irq_polarity = irq_get_trigger_type(r);
550         client->irq = r;
551
552         return 0;
553 }
554 #else
555 static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client)
556 {
557         return -ENODEV;
558 }
559 #endif
560
561 static int st21nfca_hci_i2c_request_resources(struct i2c_client *client)
562 {
563         struct st21nfca_nfc_platform_data *pdata;
564         struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
565         int r;
566         int irq;
567
568         pdata = client->dev.platform_data;
569         if (pdata == NULL) {
570                 nfc_err(&client->dev, "No platform data\n");
571                 return -EINVAL;
572         }
573
574         /* store for later use */
575         phy->gpio_irq = pdata->gpio_irq;
576         phy->gpio_ena = pdata->gpio_ena;
577         phy->irq_polarity = pdata->irq_polarity;
578
579         r = devm_gpio_request(&client->dev, phy->gpio_irq, "wake_up");
580         if (r) {
581                 pr_err("%s : gpio_request failed\n", __FILE__);
582                 return -ENODEV;
583         }
584
585         r = gpio_direction_input(phy->gpio_irq);
586         if (r) {
587                 pr_err("%s : gpio_direction_input failed\n", __FILE__);
588                 return -ENODEV;
589         }
590
591         if (phy->gpio_ena > 0) {
592                 r = devm_gpio_request(&client->dev,
593                                         phy->gpio_ena, "clf_enable");
594                 if (r) {
595                         pr_err("%s : ena gpio_request failed\n", __FILE__);
596                         return -ENODEV;
597                 }
598                 r = gpio_direction_output(phy->gpio_ena, 1);
599
600                 if (r) {
601                         pr_err("%s : ena gpio_direction_output failed\n",
602                                __FILE__);
603                         return -ENODEV;
604                 }
605         }
606
607         /* IRQ */
608         irq = gpio_to_irq(phy->gpio_irq);
609         if (irq < 0) {
610                 nfc_err(&client->dev,
611                                 "Unable to get irq number for GPIO %d error %d\n",
612                                 phy->gpio_irq, r);
613                 return -ENODEV;
614         }
615         client->irq = irq;
616
617         return 0;
618 }
619
620 static int st21nfca_hci_i2c_probe(struct i2c_client *client,
621                                   const struct i2c_device_id *id)
622 {
623         struct st21nfca_i2c_phy *phy;
624         struct st21nfca_nfc_platform_data *pdata;
625         int r;
626
627         dev_dbg(&client->dev, "%s\n", __func__);
628         dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
629
630         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
631                 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
632                 return -ENODEV;
633         }
634
635         phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy),
636                            GFP_KERNEL);
637         if (!phy) {
638                 nfc_err(&client->dev,
639                         "Cannot allocate memory for st21nfca i2c phy.\n");
640                 return -ENOMEM;
641         }
642
643         phy->i2c_dev = client;
644         phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
645         if (phy->pending_skb == NULL)
646                 return -ENOMEM;
647
648         phy->current_read_len = 0;
649         phy->crc_trials = 0;
650         mutex_init(&phy->phy_lock);
651         i2c_set_clientdata(client, phy);
652
653         pdata = client->dev.platform_data;
654         if (!pdata && client->dev.of_node) {
655                 r = st21nfca_hci_i2c_of_request_resources(client);
656                 if (r) {
657                         nfc_err(&client->dev, "No platform data\n");
658                         return r;
659                 }
660         } else if (pdata) {
661                 r = st21nfca_hci_i2c_request_resources(client);
662                 if (r) {
663                         nfc_err(&client->dev, "Cannot get platform resources\n");
664                         return r;
665                 }
666         } else {
667                 nfc_err(&client->dev, "st21nfca platform resources not available\n");
668                 return -ENODEV;
669         }
670
671         r = st21nfca_hci_platform_init(phy);
672         if (r < 0) {
673                 nfc_err(&client->dev, "Unable to reboot st21nfca\n");
674                 return -ENODEV;
675         }
676
677         r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
678                                 st21nfca_hci_irq_thread_fn,
679                                 phy->irq_polarity | IRQF_ONESHOT,
680                                 ST21NFCA_HCI_DRIVER_NAME, phy);
681         if (r < 0) {
682                 nfc_err(&client->dev, "Unable to register IRQ handler\n");
683                 return r;
684         }
685
686         return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
687                                ST21NFCA_FRAME_HEADROOM, ST21NFCA_FRAME_TAILROOM,
688                                ST21NFCA_HCI_LLC_MAX_PAYLOAD, &phy->hdev);
689 }
690
691 static int st21nfca_hci_i2c_remove(struct i2c_client *client)
692 {
693         struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
694
695         dev_dbg(&client->dev, "%s\n", __func__);
696
697         st21nfca_hci_remove(phy->hdev);
698
699         if (phy->powered)
700                 st21nfca_hci_i2c_disable(phy);
701
702         return 0;
703 }
704
705 static const struct of_device_id of_st21nfca_i2c_match[] = {
706         { .compatible = "st,st21nfca_i2c", },
707         {}
708 };
709
710 static struct i2c_driver st21nfca_hci_i2c_driver = {
711         .driver = {
712                 .owner = THIS_MODULE,
713                 .name = ST21NFCA_HCI_I2C_DRIVER_NAME,
714                 .owner = THIS_MODULE,
715                 .of_match_table = of_match_ptr(of_st21nfca_i2c_match),
716         },
717         .probe = st21nfca_hci_i2c_probe,
718         .id_table = st21nfca_hci_i2c_id_table,
719         .remove = st21nfca_hci_i2c_remove,
720 };
721
722 module_i2c_driver(st21nfca_hci_i2c_driver);
723
724 MODULE_LICENSE("GPL");
725 MODULE_DESCRIPTION(DRIVER_DESC);