blob: 396cdafb3e366ae5b75f3800153c8a1ef3e788e6 [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>
Christophe Ricarddfa80702015-12-23 23:45:11 +010023#include <linux/gpio/consumer.h>
Christophe Ricardc44cb2e2014-05-13 22:03:39 +020024#include <linux/of_irq.h>
25#include <linux/of_gpio.h>
Christophe Ricarddfa80702015-12-23 23:45:11 +010026#include <linux/acpi.h>
Christophe Ricard68957302014-03-25 06:51:47 +010027#include <linux/interrupt.h>
28#include <linux/delay.h>
29#include <linux/nfc.h>
30#include <linux/firmware.h>
Andy Shevchenko79557b32017-03-07 12:25:43 +020031
Johannes Bergdb083bc2014-11-19 21:17:22 +010032#include <asm/unaligned.h>
Christophe Ricard68957302014-03-25 06:51:47 +010033
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
Andy Shevchenko79557b32017-03-07 12:25:43 +020061#define ST21NFCA_HCI_DRIVER_NAME "st21nfca_hci"
Christophe Ricard68957302014-03-25 06:51:47 +010062#define ST21NFCA_HCI_I2C_DRIVER_NAME "st21nfca_hci_i2c"
63
Christophe Ricard2a196972016-04-30 09:12:37 +020064#define ST21NFCA_GPIO_NAME_EN "enable"
Christophe Ricard04e99b72015-12-23 23:45:08 +010065
Christophe Ricard68957302014-03-25 06:51:47 +010066struct st21nfca_i2c_phy {
67 struct i2c_client *i2c_dev;
68 struct nfc_hci_dev *hdev;
69
Andy Shevchenko8d3c50e2017-03-07 12:25:45 +020070 struct gpio_desc *gpiod_ena;
Christophe Ricard2130fb92015-01-27 01:18:19 +010071 struct st21nfca_se_status se_status;
72
Christophe Ricard68957302014-03-25 06:51:47 +010073 struct sk_buff *pending_skb;
74 int current_read_len;
75 /*
76 * crc might have fail because i2c macro
77 * is disable due to other interface activity
78 */
79 int crc_trials;
80
81 int powered;
82 int run_mode;
83
84 /*
85 * < 0 if hardware error occured (e.g. i2c err)
86 * and prevents normal operation.
87 */
88 int hard_fault;
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +020089 struct mutex phy_lock;
Christophe Ricard68957302014-03-25 06:51:47 +010090};
Christophe Ricard57dc8282015-10-25 22:54:45 +010091
Christophe Ricard05311072014-05-20 22:21:56 +020092static u8 len_seq[] = { 16, 24, 12, 29 };
Christophe Ricard68957302014-03-25 06:51:47 +010093static u16 wait_tab[] = { 2, 3, 5, 15, 20, 40};
94
95#define I2C_DUMP_SKB(info, skb) \
96do { \
97 pr_debug("%s:\n", info); \
98 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
99 16, 1, (skb)->data, (skb)->len, 0); \
100} while (0)
101
Christophe Ricard18d2c622014-04-01 00:34:07 +0200102/*
103 * In order to get the CLF in a known state we generate an internal reboot
104 * using a proprietary command.
105 * Once the reboot is completed, we expect to receive a ST21NFCA_SOF_EOF
106 * fill buffer.
107 */
108static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy)
Christophe Ricard68957302014-03-25 06:51:47 +0100109{
Christophe Ricardc5b0c372014-04-01 00:34:03 +0200110 u16 wait_reboot[] = { 50, 300, 1000 };
Christophe Ricard68957302014-03-25 06:51:47 +0100111 char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E };
112 u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE];
113 int i, r = -1;
114
Christophe Ricard18d2c622014-04-01 00:34:07 +0200115 for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
Christophe Ricard68957302014-03-25 06:51:47 +0100116 r = i2c_master_send(phy->i2c_dev, reboot_cmd,
117 sizeof(reboot_cmd));
Christophe Ricard18d2c622014-04-01 00:34:07 +0200118 if (r < 0)
119 msleep(wait_reboot[i]);
120 }
121 if (r < 0)
122 return r;
Christophe Ricard68957302014-03-25 06:51:47 +0100123
Christophe Ricard18d2c622014-04-01 00:34:07 +0200124 /* CLF is spending about 20ms to do an internal reboot */
125 msleep(20);
126 r = -1;
127 for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
128 r = i2c_master_recv(phy->i2c_dev, tmp,
129 ST21NFCA_HCI_LLC_MAX_SIZE);
130 if (r < 0)
131 msleep(wait_reboot[i]);
132 }
133 if (r < 0)
134 return r;
135
136 for (i = 0; i < ST21NFCA_HCI_LLC_MAX_SIZE &&
137 tmp[i] == ST21NFCA_SOF_EOF; i++)
138 ;
139
140 if (r != ST21NFCA_HCI_LLC_MAX_SIZE)
141 return -ENODEV;
142
143 usleep_range(1000, 1500);
144 return 0;
Christophe Ricard68957302014-03-25 06:51:47 +0100145}
146
147static int st21nfca_hci_i2c_enable(void *phy_id)
148{
149 struct st21nfca_i2c_phy *phy = phy_id;
150
Andy Shevchenko8d3c50e2017-03-07 12:25:45 +0200151 gpiod_set_value(phy->gpiod_ena, 1);
Christophe Ricard68957302014-03-25 06:51:47 +0100152 phy->powered = 1;
153 phy->run_mode = ST21NFCA_HCI_MODE;
154
155 usleep_range(10000, 15000);
156
157 return 0;
158}
159
160static void st21nfca_hci_i2c_disable(void *phy_id)
161{
162 struct st21nfca_i2c_phy *phy = phy_id;
163
Andy Shevchenko8d3c50e2017-03-07 12:25:45 +0200164 gpiod_set_value(phy->gpiod_ena, 0);
Christophe Ricard68957302014-03-25 06:51:47 +0100165
166 phy->powered = 0;
167}
168
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200169static void st21nfca_hci_add_len_crc(struct sk_buff *skb)
Christophe Ricard68957302014-03-25 06:51:47 +0100170{
Christophe Ricard68957302014-03-25 06:51:47 +0100171 u16 crc;
172 u8 tmp;
173
Johannes Bergd58ff352017-06-16 14:29:23 +0200174 *(u8 *)skb_push(skb, 1) = 0;
Christophe Ricard68957302014-03-25 06:51:47 +0100175
176 crc = crc_ccitt(0xffff, skb->data, skb->len);
177 crc = ~crc;
178
179 tmp = crc & 0x00ff;
Johannes Berg4df864c2017-06-16 14:29:21 +0200180 *(u8 *)skb_put(skb, 1) = tmp;
Christophe Ricard68957302014-03-25 06:51:47 +0100181
182 tmp = (crc >> 8) & 0x00ff;
Johannes Berg4df864c2017-06-16 14:29:21 +0200183 *(u8 *)skb_put(skb, 1) = tmp;
Christophe Ricard68957302014-03-25 06:51:47 +0100184}
185
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200186static void st21nfca_hci_remove_len_crc(struct sk_buff *skb)
Christophe Ricard68957302014-03-25 06:51:47 +0100187{
188 skb_pull(skb, ST21NFCA_FRAME_HEADROOM);
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200189 skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM);
Christophe Ricard68957302014-03-25 06:51:47 +0100190}
191
192/*
193 * Writing a frame must not return the number of written bytes.
194 * It must return either zero for success, or <0 for error.
195 * In addition, it must not alter the skb
196 */
197static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb)
198{
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200199 int r = -1, i, j;
Christophe Ricard68957302014-03-25 06:51:47 +0100200 struct st21nfca_i2c_phy *phy = phy_id;
201 struct i2c_client *client = phy->i2c_dev;
Christophe Ricard68957302014-03-25 06:51:47 +0100202 u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2];
203
204 I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb);
205
Christophe Ricard68957302014-03-25 06:51:47 +0100206 if (phy->hard_fault != 0)
207 return phy->hard_fault;
208
209 /*
210 * Compute CRC before byte stuffing computation on frame
211 * Note st21nfca_hci_add_len_crc is doing a byte stuffing
212 * on its own value
213 */
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200214 st21nfca_hci_add_len_crc(skb);
Christophe Ricard68957302014-03-25 06:51:47 +0100215
216 /* add ST21NFCA_SOF_EOF on tail */
Johannes Berg4df864c2017-06-16 14:29:21 +0200217 *(u8 *)skb_put(skb, 1) = ST21NFCA_SOF_EOF;
Christophe Ricard68957302014-03-25 06:51:47 +0100218 /* add ST21NFCA_SOF_EOF on head */
Johannes Bergd58ff352017-06-16 14:29:23 +0200219 *(u8 *)skb_push(skb, 1) = ST21NFCA_SOF_EOF;
Christophe Ricard68957302014-03-25 06:51:47 +0100220
221 /*
222 * Compute byte stuffing
223 * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
224 * insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
225 * xor byte with ST21NFCA_BYTE_STUFFING_MASK
226 */
227 tmp[0] = skb->data[0];
228 for (i = 1, j = 1; i < skb->len - 1; i++, j++) {
229 if (skb->data[i] == ST21NFCA_SOF_EOF
230 || skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) {
231 tmp[j] = ST21NFCA_ESCAPE_BYTE_STUFFING;
232 j++;
233 tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK;
234 } else {
235 tmp[j] = skb->data[i];
236 }
237 }
238 tmp[j] = skb->data[i];
239 j++;
240
241 /*
242 * Manage sleep mode
243 * Try 3 times to send data with delay between each
244 */
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +0200245 mutex_lock(&phy->phy_lock);
Christophe Ricard68957302014-03-25 06:51:47 +0100246 for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++) {
247 r = i2c_master_send(client, tmp, j);
248 if (r < 0)
249 msleep(wait_tab[i]);
250 }
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +0200251 mutex_unlock(&phy->phy_lock);
Christophe Ricard68957302014-03-25 06:51:47 +0100252
253 if (r >= 0) {
254 if (r != j)
255 r = -EREMOTEIO;
256 else
257 r = 0;
258 }
259
Christophe Ricarde1fb97b2014-04-24 23:19:31 +0200260 st21nfca_hci_remove_len_crc(skb);
Christophe Ricard68957302014-03-25 06:51:47 +0100261
262 return r;
263}
264
265static int get_frame_size(u8 *buf, int buflen)
266{
267 int len = 0;
Christophe Ricard3e6df912014-07-28 18:11:31 +0200268
Christophe Ricard68957302014-03-25 06:51:47 +0100269 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;
Christophe Ricard3e6df912014-07-28 18:11:31 +0200309
Christophe Ricard68957302014-03-25 06:51:47 +0100310 if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0))
311 return -EBADMSG;
312
313 size = get_frame_size(skb->data, skb->len);
314 if (size > 0) {
315 skb_trim(skb, size);
316 /* remove ST21NFCA byte stuffing for upper layer */
317 for (i = 1, j = 0; i < skb->len; i++) {
Christophe Ricard3096e252014-04-24 23:19:32 +0200318 if (skb->data[i + j] ==
Christophe Ricard68957302014-03-25 06:51:47 +0100319 (u8) ST21NFCA_ESCAPE_BYTE_STUFFING) {
Christophe Ricard3096e252014-04-24 23:19:32 +0200320 skb->data[i] = skb->data[i + j + 1]
321 | ST21NFCA_BYTE_STUFFING_MASK;
Christophe Ricard68957302014-03-25 06:51:47 +0100322 i++;
323 j++;
324 }
325 skb->data[i] = skb->data[i + j];
326 }
327 /* remove byte stuffing useless byte */
328 skb_trim(skb, i - j);
329 /* remove ST21NFCA_SOF_EOF from head */
330 skb_pull(skb, 1);
331
332 r = check_crc(skb->data, skb->len);
333 if (r != 0) {
334 i = 0;
335 return -EBADMSG;
336 }
337
338 /* remove headbyte */
339 skb_pull(skb, 1);
340 /* remove crc. Byte Stuffing is already removed here */
341 skb_trim(skb, skb->len - 2);
342 return skb->len;
343 }
344 return 0;
345}
346
347/*
348 * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
349 * that i2c bus will be flushed and that next read will start on a new frame.
350 * returned skb contains only LLC header and payload.
351 * returns:
352 * frame size : if received frame is complete (find ST21NFCA_SOF_EOF at
353 * end of read)
354 * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF
355 * at end of read)
356 * -EREMOTEIO : i2c read error (fatal)
357 * -EBADMSG : frame was incorrect and discarded
358 * (value returned from st21nfca_hci_i2c_repack)
359 * -EIO : if no ST21NFCA_SOF_EOF is found after reaching
360 * the read length end sequence
361 */
362static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy,
363 struct sk_buff *skb)
364{
365 int r, i;
366 u8 len;
Christophe Ricardc97ffdb2014-04-24 23:19:33 +0200367 u8 buf[ST21NFCA_HCI_LLC_MAX_PAYLOAD];
Christophe Ricard68957302014-03-25 06:51:47 +0100368 struct i2c_client *client = phy->i2c_dev;
369
370 if (phy->current_read_len < ARRAY_SIZE(len_seq)) {
371 len = len_seq[phy->current_read_len];
372
373 /*
374 * Add retry mecanism
375 * Operation on I2C interface may fail in case of operation on
376 * RF or SWP interface
377 */
378 r = 0;
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +0200379 mutex_lock(&phy->phy_lock);
Christophe Ricard68957302014-03-25 06:51:47 +0100380 for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) {
Christophe Ricardc97ffdb2014-04-24 23:19:33 +0200381 r = i2c_master_recv(client, buf, len);
Christophe Ricard68957302014-03-25 06:51:47 +0100382 if (r < 0)
383 msleep(wait_tab[i]);
384 }
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +0200385 mutex_unlock(&phy->phy_lock);
Christophe Ricard68957302014-03-25 06:51:47 +0100386
387 if (r != len) {
388 phy->current_read_len = 0;
389 return -EREMOTEIO;
390 }
391
Christophe Ricardc97ffdb2014-04-24 23:19:33 +0200392 /*
393 * The first read sequence does not start with SOF.
394 * Data is corrupeted so we drop it.
395 */
Christophe Ricard8e9466c2014-05-20 22:21:55 +0200396 if (!phy->current_read_len && !IS_START_OF_FRAME(buf)) {
Christophe Ricardc97ffdb2014-04-24 23:19:33 +0200397 skb_trim(skb, 0);
398 phy->current_read_len = 0;
399 return -EIO;
Christophe Ricard8e9466c2014-05-20 22:21:55 +0200400 } else if (phy->current_read_len && IS_START_OF_FRAME(buf)) {
Christophe Ricardc97ffdb2014-04-24 23:19:33 +0200401 /*
402 * Previous frame transmission was interrupted and
403 * the frame got repeated.
404 * Received frame start with ST21NFCA_SOF_EOF + 00.
405 */
406 skb_trim(skb, 0);
407 phy->current_read_len = 0;
408 }
409
Johannes Berg59ae1d12017-06-16 14:29:20 +0200410 skb_put_data(skb, buf, len);
Christophe Ricardc97ffdb2014-04-24 23:19:33 +0200411
412 if (skb->data[skb->len - 1] == ST21NFCA_SOF_EOF) {
Christophe Ricard68957302014-03-25 06:51:47 +0100413 phy->current_read_len = 0;
414 return st21nfca_hci_i2c_repack(skb);
415 }
416 phy->current_read_len++;
417 return -EAGAIN;
418 }
419 return -EIO;
420}
421
422/*
423 * Reads an shdlc frame from the chip. This is not as straightforward as it
424 * seems. The frame format is data-crc, and corruption can occur anywhere
425 * while transiting on i2c bus, such that we could read an invalid data.
426 * The tricky case is when we read a corrupted data or crc. We must detect
427 * this here in order to determine that data can be transmitted to the hci
428 * core. This is the reason why we check the crc here.
429 * The CLF will repeat a frame until we send a RR on that frame.
430 *
431 * On ST21NFCA, IRQ goes in idle when read starts. As no size information are
432 * available in the incoming data, other IRQ might come. Every IRQ will trigger
433 * a read sequence with different length and will fill the current frame.
434 * The reception is complete once we reach a ST21NFCA_SOF_EOF.
435 */
436static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id)
437{
438 struct st21nfca_i2c_phy *phy = phy_id;
439 struct i2c_client *client;
440
441 int r;
442
443 if (!phy || irq != phy->i2c_dev->irq) {
444 WARN_ON_ONCE(1);
445 return IRQ_NONE;
446 }
447
448 client = phy->i2c_dev;
449 dev_dbg(&client->dev, "IRQ\n");
450
451 if (phy->hard_fault != 0)
452 return IRQ_HANDLED;
453
454 r = st21nfca_hci_i2c_read(phy, phy->pending_skb);
455 if (r == -EREMOTEIO) {
456 phy->hard_fault = r;
457
458 nfc_hci_recv_frame(phy->hdev, NULL);
459
460 return IRQ_HANDLED;
461 } else if (r == -EAGAIN || r == -EIO) {
462 return IRQ_HANDLED;
463 } else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) {
464 /*
465 * With ST21NFCA, only one interface (I2C, RF or SWP)
466 * may be active at a time.
467 * Having incorrect crc is usually due to i2c macrocell
468 * deactivation in the middle of a transmission.
469 * It may generate corrupted data on i2c.
470 * We give sometime to get i2c back.
471 * The complete frame will be repeated.
472 */
473 msleep(wait_tab[phy->crc_trials]);
474 phy->crc_trials++;
475 phy->current_read_len = 0;
Christophe Ricard0c942b02014-04-24 23:19:35 +0200476 kfree_skb(phy->pending_skb);
Christophe Ricard68957302014-03-25 06:51:47 +0100477 } else if (r > 0) {
478 /*
479 * We succeeded to read data from the CLF and
480 * data is valid.
481 * Reset counter.
482 */
483 nfc_hci_recv_frame(phy->hdev, phy->pending_skb);
484 phy->crc_trials = 0;
Christophe Ricardcf577342014-05-20 22:21:54 +0200485 } else {
486 kfree_skb(phy->pending_skb);
Christophe Ricard68957302014-03-25 06:51:47 +0100487 }
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 Ricarddfa80702015-12-23 23:45:11 +0100504static int st21nfca_hci_i2c_acpi_request_resources(struct i2c_client *client)
505{
506 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
Christophe Ricard070718a2016-04-30 09:12:44 +0200507 struct device *dev = &client->dev;
Christophe Ricarddfa80702015-12-23 23:45:11 +0100508
Christophe Ricarddfa80702015-12-23 23:45:11 +0100509 /* Get EN GPIO from ACPI */
Andy Shevchenko8d3c50e2017-03-07 12:25:45 +0200510 phy->gpiod_ena = devm_gpiod_get_index(dev, ST21NFCA_GPIO_NAME_EN, 1,
511 GPIOD_OUT_LOW);
512 if (IS_ERR(phy->gpiod_ena)) {
Christophe Ricardb58afe62016-04-30 09:12:34 +0200513 nfc_err(dev, "Unable to get ENABLE GPIO\n");
Andy Shevchenko8d3c50e2017-03-07 12:25:45 +0200514 return PTR_ERR(phy->gpiod_ena);
Christophe Ricardb58afe62016-04-30 09:12:34 +0200515 }
Christophe Ricarddfa80702015-12-23 23:45:11 +0100516
Christophe Ricarddfa80702015-12-23 23:45:11 +0100517 return 0;
518}
519
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200520static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client)
521{
522 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
Andy Shevchenko8d3c50e2017-03-07 12:25:45 +0200523 struct device *dev = &client->dev;
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200524
525 /* Get GPIO from device tree */
Andy Shevchenko8d3c50e2017-03-07 12:25:45 +0200526 phy->gpiod_ena = devm_gpiod_get_index(dev, ST21NFCA_GPIO_NAME_EN, 0,
527 GPIOD_OUT_HIGH);
528 if (IS_ERR(phy->gpiod_ena)) {
529 nfc_err(dev, "Failed to request enable pin\n");
530 return PTR_ERR(phy->gpiod_ena);
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200531 }
532
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200533 return 0;
534}
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200535
Christophe Ricard68957302014-03-25 06:51:47 +0100536static int st21nfca_hci_i2c_probe(struct i2c_client *client,
537 const struct i2c_device_id *id)
538{
539 struct st21nfca_i2c_phy *phy;
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200540 int r;
Christophe Ricard68957302014-03-25 06:51:47 +0100541
542 dev_dbg(&client->dev, "%s\n", __func__);
543 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
544
545 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
546 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
547 return -ENODEV;
548 }
549
550 phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy),
551 GFP_KERNEL);
Christophe Ricard2b702832015-01-25 23:33:27 +0100552 if (!phy)
Christophe Ricard68957302014-03-25 06:51:47 +0100553 return -ENOMEM;
Christophe Ricard68957302014-03-25 06:51:47 +0100554
555 phy->i2c_dev = client;
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200556 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
557 if (phy->pending_skb == NULL)
558 return -ENOMEM;
Christophe Ricard68957302014-03-25 06:51:47 +0100559
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200560 phy->current_read_len = 0;
561 phy->crc_trials = 0;
Christophe Ricarda3c5d8f2014-04-24 23:19:34 +0200562 mutex_init(&phy->phy_lock);
Christophe Ricard68957302014-03-25 06:51:47 +0100563 i2c_set_clientdata(client, phy);
564
Andy Shevchenko79557b32017-03-07 12:25:43 +0200565 if (client->dev.of_node) {
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200566 r = st21nfca_hci_i2c_of_request_resources(client);
567 if (r) {
568 nfc_err(&client->dev, "No platform data\n");
569 return r;
570 }
Christophe Ricarddfa80702015-12-23 23:45:11 +0100571 } else if (ACPI_HANDLE(&client->dev)) {
572 r = st21nfca_hci_i2c_acpi_request_resources(client);
573 if (r) {
574 nfc_err(&client->dev, "Cannot get ACPI data\n");
575 return r;
576 }
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200577 } else {
578 nfc_err(&client->dev, "st21nfca platform resources not available\n");
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200579 return -ENODEV;
580 }
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200581
Andy Shevchenko682fd612017-03-07 12:25:46 +0200582 phy->se_status.is_ese_present =
583 device_property_read_bool(&client->dev, "ese-present");
584 phy->se_status.is_uicc_present =
585 device_property_read_bool(&client->dev, "uicc-present");
586
Christophe Ricard18d2c622014-04-01 00:34:07 +0200587 r = st21nfca_hci_platform_init(phy);
588 if (r < 0) {
589 nfc_err(&client->dev, "Unable to reboot st21nfca\n");
Christophe Ricard67df3f92014-12-02 21:27:55 +0100590 return r;
Christophe Ricard18d2c622014-04-01 00:34:07 +0200591 }
592
Christophe Ricard68957302014-03-25 06:51:47 +0100593 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
594 st21nfca_hci_irq_thread_fn,
Andy Shevchenko8e7836d2017-03-07 12:25:44 +0200595 IRQF_ONESHOT,
Christophe Ricard68957302014-03-25 06:51:47 +0100596 ST21NFCA_HCI_DRIVER_NAME, phy);
597 if (r < 0) {
598 nfc_err(&client->dev, "Unable to register IRQ handler\n");
599 return r;
600 }
601
Christophe Ricard9bac75d2014-04-01 00:34:05 +0200602 return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
Christophe Ricard2130fb92015-01-27 01:18:19 +0100603 ST21NFCA_FRAME_HEADROOM,
604 ST21NFCA_FRAME_TAILROOM,
605 ST21NFCA_HCI_LLC_MAX_PAYLOAD,
606 &phy->hdev,
607 &phy->se_status);
Christophe Ricard68957302014-03-25 06:51:47 +0100608}
609
610static int st21nfca_hci_i2c_remove(struct i2c_client *client)
611{
612 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
613
614 dev_dbg(&client->dev, "%s\n", __func__);
615
616 st21nfca_hci_remove(phy->hdev);
617
618 if (phy->powered)
619 st21nfca_hci_i2c_disable(phy);
620
621 return 0;
622}
623
Christophe Ricard39db2d22015-12-23 23:45:06 +0100624static struct i2c_device_id st21nfca_hci_i2c_id_table[] = {
625 {ST21NFCA_HCI_DRIVER_NAME, 0},
626 {}
627};
628MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table);
629
Christophe Ricarddfa80702015-12-23 23:45:11 +0100630static const struct acpi_device_id st21nfca_hci_i2c_acpi_match[] = {
631 {"SMO2100", 0},
632 {}
633};
634MODULE_DEVICE_TABLE(acpi, st21nfca_hci_i2c_acpi_match);
635
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200636static const struct of_device_id of_st21nfca_i2c_match[] = {
Christophe Ricard6b5fba42014-12-08 22:08:06 +0100637 { .compatible = "st,st21nfca-i2c", },
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200638 { .compatible = "st,st21nfca_i2c", },
639 {}
640};
Christophe Ricard17e401072014-11-13 00:30:22 +0100641MODULE_DEVICE_TABLE(of, of_st21nfca_i2c_match);
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200642
Christophe Ricard68957302014-03-25 06:51:47 +0100643static struct i2c_driver st21nfca_hci_i2c_driver = {
644 .driver = {
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200645 .name = ST21NFCA_HCI_I2C_DRIVER_NAME,
Christophe Ricardc44cb2e2014-05-13 22:03:39 +0200646 .of_match_table = of_match_ptr(of_st21nfca_i2c_match),
Christophe Ricarddfa80702015-12-23 23:45:11 +0100647 .acpi_match_table = ACPI_PTR(st21nfca_hci_i2c_acpi_match),
Christophe Ricardfcb45e62014-04-01 00:34:06 +0200648 },
Christophe Ricard68957302014-03-25 06:51:47 +0100649 .probe = st21nfca_hci_i2c_probe,
650 .id_table = st21nfca_hci_i2c_id_table,
651 .remove = st21nfca_hci_i2c_remove,
652};
Christophe Ricard68957302014-03-25 06:51:47 +0100653module_i2c_driver(st21nfca_hci_i2c_driver);
654
655MODULE_LICENSE("GPL");
656MODULE_DESCRIPTION(DRIVER_DESC);