Commit | Line | Data |
---|---|---|
68957303 CR |
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> | |
c44cb2ed CR |
24 | #include <linux/of_irq.h> |
25 | #include <linux/of_gpio.h> | |
68957303 CR |
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> | |
68957303 | 31 | #include <linux/platform_data/st21nfca.h> |
db083bcb | 32 | #include <asm/unaligned.h> |
68957303 CR |
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 | ||
e1fb97b9 | 53 | /* SOF + 00 */ |
68957303 CR |
54 | #define ST21NFCA_FRAME_HEADROOM 2 |
55 | ||
e1fb97b9 CR |
56 | /* 2 bytes crc + EOF */ |
57 | #define ST21NFCA_FRAME_TAILROOM 3 | |
c97ffdbf CR |
58 | #define IS_START_OF_FRAME(buf) (buf[0] == ST21NFCA_SOF_EOF && \ |
59 | buf[1] == 0) | |
68957303 CR |
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; | |
68957303 CR |
75 | unsigned int irq_polarity; |
76 | ||
2130fb97 CR |
77 | struct st21nfca_se_status se_status; |
78 | ||
68957303 CR |
79 | struct sk_buff *pending_skb; |
80 | int current_read_len; | |
81 | /* | |
82 | * crc might have fail because i2c macro | |
83 | * is disable due to other interface activity | |
84 | */ | |
85 | int crc_trials; | |
86 | ||
87 | int powered; | |
88 | int run_mode; | |
89 | ||
90 | /* | |
91 | * < 0 if hardware error occured (e.g. i2c err) | |
92 | * and prevents normal operation. | |
93 | */ | |
94 | int hard_fault; | |
a3c5d8fb | 95 | struct mutex phy_lock; |
68957303 | 96 | }; |
0531107e | 97 | static u8 len_seq[] = { 16, 24, 12, 29 }; |
68957303 CR |
98 | static u16 wait_tab[] = { 2, 3, 5, 15, 20, 40}; |
99 | ||
100 | #define I2C_DUMP_SKB(info, skb) \ | |
101 | do { \ | |
102 | pr_debug("%s:\n", info); \ | |
103 | print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \ | |
104 | 16, 1, (skb)->data, (skb)->len, 0); \ | |
105 | } while (0) | |
106 | ||
18d2c624 CR |
107 | /* |
108 | * In order to get the CLF in a known state we generate an internal reboot | |
109 | * using a proprietary command. | |
110 | * Once the reboot is completed, we expect to receive a ST21NFCA_SOF_EOF | |
111 | * fill buffer. | |
112 | */ | |
113 | static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy) | |
68957303 | 114 | { |
c5b0c370 | 115 | u16 wait_reboot[] = { 50, 300, 1000 }; |
68957303 CR |
116 | char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E }; |
117 | u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE]; | |
118 | int i, r = -1; | |
119 | ||
18d2c624 CR |
120 | for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) { |
121 | r = i2c_master_send(phy->i2c_dev, reboot_cmd, | |
122 | sizeof(reboot_cmd)); | |
123 | if (r < 0) | |
124 | msleep(wait_reboot[i]); | |
125 | } | |
126 | if (r < 0) | |
127 | return r; | |
128 | ||
129 | /* CLF is spending about 20ms to do an internal reboot */ | |
130 | msleep(20); | |
131 | r = -1; | |
132 | for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) { | |
68957303 CR |
133 | r = i2c_master_recv(phy->i2c_dev, tmp, |
134 | ST21NFCA_HCI_LLC_MAX_SIZE); | |
18d2c624 CR |
135 | if (r < 0) |
136 | msleep(wait_reboot[i]); | |
137 | } | |
138 | if (r < 0) | |
139 | return r; | |
68957303 | 140 | |
18d2c624 CR |
141 | for (i = 0; i < ST21NFCA_HCI_LLC_MAX_SIZE && |
142 | tmp[i] == ST21NFCA_SOF_EOF; i++) | |
143 | ; | |
144 | ||
145 | if (r != ST21NFCA_HCI_LLC_MAX_SIZE) | |
146 | return -ENODEV; | |
68957303 | 147 | |
18d2c624 CR |
148 | usleep_range(1000, 1500); |
149 | return 0; | |
68957303 CR |
150 | } |
151 | ||
152 | static int st21nfca_hci_i2c_enable(void *phy_id) | |
153 | { | |
154 | struct st21nfca_i2c_phy *phy = phy_id; | |
155 | ||
156 | gpio_set_value(phy->gpio_ena, 1); | |
157 | phy->powered = 1; | |
158 | phy->run_mode = ST21NFCA_HCI_MODE; | |
159 | ||
160 | usleep_range(10000, 15000); | |
161 | ||
162 | return 0; | |
163 | } | |
164 | ||
165 | static void st21nfca_hci_i2c_disable(void *phy_id) | |
166 | { | |
167 | struct st21nfca_i2c_phy *phy = phy_id; | |
168 | ||
169 | pr_info("\n"); | |
170 | gpio_set_value(phy->gpio_ena, 0); | |
171 | ||
172 | phy->powered = 0; | |
173 | } | |
174 | ||
e1fb97b9 | 175 | static void st21nfca_hci_add_len_crc(struct sk_buff *skb) |
68957303 | 176 | { |
68957303 CR |
177 | u16 crc; |
178 | u8 tmp; | |
179 | ||
180 | *skb_push(skb, 1) = 0; | |
181 | ||
182 | crc = crc_ccitt(0xffff, skb->data, skb->len); | |
183 | crc = ~crc; | |
184 | ||
185 | tmp = crc & 0x00ff; | |
186 | *skb_put(skb, 1) = tmp; | |
187 | ||
188 | tmp = (crc >> 8) & 0x00ff; | |
189 | *skb_put(skb, 1) = tmp; | |
68957303 CR |
190 | } |
191 | ||
e1fb97b9 | 192 | static void st21nfca_hci_remove_len_crc(struct sk_buff *skb) |
68957303 CR |
193 | { |
194 | skb_pull(skb, ST21NFCA_FRAME_HEADROOM); | |
e1fb97b9 | 195 | skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM); |
68957303 CR |
196 | } |
197 | ||
198 | /* | |
199 | * Writing a frame must not return the number of written bytes. | |
200 | * It must return either zero for success, or <0 for error. | |
201 | * In addition, it must not alter the skb | |
202 | */ | |
203 | static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb) | |
204 | { | |
e1fb97b9 | 205 | int r = -1, i, j; |
68957303 CR |
206 | struct st21nfca_i2c_phy *phy = phy_id; |
207 | struct i2c_client *client = phy->i2c_dev; | |
68957303 CR |
208 | u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2]; |
209 | ||
210 | I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb); | |
211 | ||
212 | ||
213 | if (phy->hard_fault != 0) | |
214 | return phy->hard_fault; | |
215 | ||
216 | /* | |
217 | * Compute CRC before byte stuffing computation on frame | |
218 | * Note st21nfca_hci_add_len_crc is doing a byte stuffing | |
219 | * on its own value | |
220 | */ | |
e1fb97b9 | 221 | st21nfca_hci_add_len_crc(skb); |
68957303 CR |
222 | |
223 | /* add ST21NFCA_SOF_EOF on tail */ | |
224 | *skb_put(skb, 1) = ST21NFCA_SOF_EOF; | |
225 | /* add ST21NFCA_SOF_EOF on head */ | |
226 | *skb_push(skb, 1) = ST21NFCA_SOF_EOF; | |
227 | ||
228 | /* | |
229 | * Compute byte stuffing | |
230 | * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING | |
231 | * insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte) | |
232 | * xor byte with ST21NFCA_BYTE_STUFFING_MASK | |
233 | */ | |
234 | tmp[0] = skb->data[0]; | |
235 | for (i = 1, j = 1; i < skb->len - 1; i++, j++) { | |
236 | if (skb->data[i] == ST21NFCA_SOF_EOF | |
237 | || skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) { | |
238 | tmp[j] = ST21NFCA_ESCAPE_BYTE_STUFFING; | |
239 | j++; | |
240 | tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK; | |
241 | } else { | |
242 | tmp[j] = skb->data[i]; | |
243 | } | |
244 | } | |
245 | tmp[j] = skb->data[i]; | |
246 | j++; | |
247 | ||
248 | /* | |
249 | * Manage sleep mode | |
250 | * Try 3 times to send data with delay between each | |
251 | */ | |
a3c5d8fb | 252 | mutex_lock(&phy->phy_lock); |
68957303 CR |
253 | for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++) { |
254 | r = i2c_master_send(client, tmp, j); | |
255 | if (r < 0) | |
256 | msleep(wait_tab[i]); | |
257 | } | |
a3c5d8fb | 258 | mutex_unlock(&phy->phy_lock); |
68957303 CR |
259 | |
260 | if (r >= 0) { | |
261 | if (r != j) | |
262 | r = -EREMOTEIO; | |
263 | else | |
264 | r = 0; | |
265 | } | |
266 | ||
e1fb97b9 | 267 | st21nfca_hci_remove_len_crc(skb); |
68957303 CR |
268 | |
269 | return r; | |
270 | } | |
271 | ||
272 | static int get_frame_size(u8 *buf, int buflen) | |
273 | { | |
274 | int len = 0; | |
3e6df919 | 275 | |
68957303 CR |
276 | if (buf[len + 1] == ST21NFCA_SOF_EOF) |
277 | return 0; | |
278 | ||
279 | for (len = 1; len < buflen && buf[len] != ST21NFCA_SOF_EOF; len++) | |
280 | ; | |
281 | ||
282 | return len; | |
283 | } | |
284 | ||
285 | static int check_crc(u8 *buf, int buflen) | |
286 | { | |
287 | u16 crc; | |
288 | ||
289 | crc = crc_ccitt(0xffff, buf, buflen - 2); | |
290 | crc = ~crc; | |
291 | ||
292 | if (buf[buflen - 2] != (crc & 0xff) || buf[buflen - 1] != (crc >> 8)) { | |
293 | pr_err(ST21NFCA_HCI_DRIVER_NAME | |
294 | ": CRC error 0x%x != 0x%x 0x%x\n", crc, buf[buflen - 1], | |
295 | buf[buflen - 2]); | |
296 | ||
297 | pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__); | |
298 | print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE, | |
299 | 16, 2, buf, buflen, false); | |
300 | return -EPERM; | |
301 | } | |
302 | return 0; | |
303 | } | |
304 | ||
305 | /* | |
306 | * Prepare received data for upper layer. | |
307 | * Received data include byte stuffing, crc and sof/eof | |
308 | * which is not usable by hci part. | |
309 | * returns: | |
310 | * frame size without sof/eof, header and byte stuffing | |
311 | * -EBADMSG : frame was incorrect and discarded | |
312 | */ | |
313 | static int st21nfca_hci_i2c_repack(struct sk_buff *skb) | |
314 | { | |
315 | int i, j, r, size; | |
3e6df919 | 316 | |
68957303 CR |
317 | if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0)) |
318 | return -EBADMSG; | |
319 | ||
320 | size = get_frame_size(skb->data, skb->len); | |
321 | if (size > 0) { | |
322 | skb_trim(skb, size); | |
323 | /* remove ST21NFCA byte stuffing for upper layer */ | |
324 | for (i = 1, j = 0; i < skb->len; i++) { | |
3096e25a | 325 | if (skb->data[i + j] == |
68957303 | 326 | (u8) ST21NFCA_ESCAPE_BYTE_STUFFING) { |
3096e25a CR |
327 | skb->data[i] = skb->data[i + j + 1] |
328 | | ST21NFCA_BYTE_STUFFING_MASK; | |
68957303 CR |
329 | i++; |
330 | j++; | |
331 | } | |
332 | skb->data[i] = skb->data[i + j]; | |
333 | } | |
334 | /* remove byte stuffing useless byte */ | |
335 | skb_trim(skb, i - j); | |
336 | /* remove ST21NFCA_SOF_EOF from head */ | |
337 | skb_pull(skb, 1); | |
338 | ||
339 | r = check_crc(skb->data, skb->len); | |
340 | if (r != 0) { | |
341 | i = 0; | |
342 | return -EBADMSG; | |
343 | } | |
344 | ||
345 | /* remove headbyte */ | |
346 | skb_pull(skb, 1); | |
347 | /* remove crc. Byte Stuffing is already removed here */ | |
348 | skb_trim(skb, skb->len - 2); | |
349 | return skb->len; | |
350 | } | |
351 | return 0; | |
352 | } | |
353 | ||
354 | /* | |
355 | * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees | |
356 | * that i2c bus will be flushed and that next read will start on a new frame. | |
357 | * returned skb contains only LLC header and payload. | |
358 | * returns: | |
359 | * frame size : if received frame is complete (find ST21NFCA_SOF_EOF at | |
360 | * end of read) | |
361 | * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF | |
362 | * at end of read) | |
363 | * -EREMOTEIO : i2c read error (fatal) | |
364 | * -EBADMSG : frame was incorrect and discarded | |
365 | * (value returned from st21nfca_hci_i2c_repack) | |
366 | * -EIO : if no ST21NFCA_SOF_EOF is found after reaching | |
367 | * the read length end sequence | |
368 | */ | |
369 | static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy, | |
370 | struct sk_buff *skb) | |
371 | { | |
372 | int r, i; | |
373 | u8 len; | |
c97ffdbf | 374 | u8 buf[ST21NFCA_HCI_LLC_MAX_PAYLOAD]; |
68957303 CR |
375 | struct i2c_client *client = phy->i2c_dev; |
376 | ||
377 | if (phy->current_read_len < ARRAY_SIZE(len_seq)) { | |
378 | len = len_seq[phy->current_read_len]; | |
379 | ||
380 | /* | |
381 | * Add retry mecanism | |
382 | * Operation on I2C interface may fail in case of operation on | |
383 | * RF or SWP interface | |
384 | */ | |
385 | r = 0; | |
a3c5d8fb | 386 | mutex_lock(&phy->phy_lock); |
68957303 | 387 | for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) { |
c97ffdbf | 388 | r = i2c_master_recv(client, buf, len); |
68957303 CR |
389 | if (r < 0) |
390 | msleep(wait_tab[i]); | |
391 | } | |
a3c5d8fb | 392 | mutex_unlock(&phy->phy_lock); |
68957303 CR |
393 | |
394 | if (r != len) { | |
395 | phy->current_read_len = 0; | |
396 | return -EREMOTEIO; | |
397 | } | |
398 | ||
c97ffdbf CR |
399 | /* |
400 | * The first read sequence does not start with SOF. | |
401 | * Data is corrupeted so we drop it. | |
402 | */ | |
8e9466cc | 403 | if (!phy->current_read_len && !IS_START_OF_FRAME(buf)) { |
c97ffdbf CR |
404 | skb_trim(skb, 0); |
405 | phy->current_read_len = 0; | |
406 | return -EIO; | |
8e9466cc | 407 | } else if (phy->current_read_len && IS_START_OF_FRAME(buf)) { |
c97ffdbf CR |
408 | /* |
409 | * Previous frame transmission was interrupted and | |
410 | * the frame got repeated. | |
411 | * Received frame start with ST21NFCA_SOF_EOF + 00. | |
412 | */ | |
413 | skb_trim(skb, 0); | |
414 | phy->current_read_len = 0; | |
415 | } | |
416 | ||
417 | memcpy(skb_put(skb, len), buf, len); | |
418 | ||
419 | if (skb->data[skb->len - 1] == ST21NFCA_SOF_EOF) { | |
68957303 CR |
420 | phy->current_read_len = 0; |
421 | return st21nfca_hci_i2c_repack(skb); | |
422 | } | |
423 | phy->current_read_len++; | |
424 | return -EAGAIN; | |
425 | } | |
426 | return -EIO; | |
427 | } | |
428 | ||
429 | /* | |
430 | * Reads an shdlc frame from the chip. This is not as straightforward as it | |
431 | * seems. The frame format is data-crc, and corruption can occur anywhere | |
432 | * while transiting on i2c bus, such that we could read an invalid data. | |
433 | * The tricky case is when we read a corrupted data or crc. We must detect | |
434 | * this here in order to determine that data can be transmitted to the hci | |
435 | * core. This is the reason why we check the crc here. | |
436 | * The CLF will repeat a frame until we send a RR on that frame. | |
437 | * | |
438 | * On ST21NFCA, IRQ goes in idle when read starts. As no size information are | |
439 | * available in the incoming data, other IRQ might come. Every IRQ will trigger | |
440 | * a read sequence with different length and will fill the current frame. | |
441 | * The reception is complete once we reach a ST21NFCA_SOF_EOF. | |
442 | */ | |
443 | static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id) | |
444 | { | |
445 | struct st21nfca_i2c_phy *phy = phy_id; | |
446 | struct i2c_client *client; | |
447 | ||
448 | int r; | |
449 | ||
450 | if (!phy || irq != phy->i2c_dev->irq) { | |
451 | WARN_ON_ONCE(1); | |
452 | return IRQ_NONE; | |
453 | } | |
454 | ||
455 | client = phy->i2c_dev; | |
456 | dev_dbg(&client->dev, "IRQ\n"); | |
457 | ||
458 | if (phy->hard_fault != 0) | |
459 | return IRQ_HANDLED; | |
460 | ||
461 | r = st21nfca_hci_i2c_read(phy, phy->pending_skb); | |
462 | if (r == -EREMOTEIO) { | |
463 | phy->hard_fault = r; | |
464 | ||
465 | nfc_hci_recv_frame(phy->hdev, NULL); | |
466 | ||
467 | return IRQ_HANDLED; | |
468 | } else if (r == -EAGAIN || r == -EIO) { | |
469 | return IRQ_HANDLED; | |
470 | } else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) { | |
471 | /* | |
472 | * With ST21NFCA, only one interface (I2C, RF or SWP) | |
473 | * may be active at a time. | |
474 | * Having incorrect crc is usually due to i2c macrocell | |
475 | * deactivation in the middle of a transmission. | |
476 | * It may generate corrupted data on i2c. | |
477 | * We give sometime to get i2c back. | |
478 | * The complete frame will be repeated. | |
479 | */ | |
480 | msleep(wait_tab[phy->crc_trials]); | |
481 | phy->crc_trials++; | |
482 | phy->current_read_len = 0; | |
0c942b00 | 483 | kfree_skb(phy->pending_skb); |
68957303 CR |
484 | } else if (r > 0) { |
485 | /* | |
486 | * We succeeded to read data from the CLF and | |
487 | * data is valid. | |
488 | * Reset counter. | |
489 | */ | |
490 | nfc_hci_recv_frame(phy->hdev, phy->pending_skb); | |
491 | phy->crc_trials = 0; | |
cf577344 CR |
492 | } else { |
493 | kfree_skb(phy->pending_skb); | |
68957303 CR |
494 | } |
495 | ||
496 | phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL); | |
497 | if (phy->pending_skb == NULL) { | |
498 | phy->hard_fault = -ENOMEM; | |
499 | nfc_hci_recv_frame(phy->hdev, NULL); | |
500 | } | |
501 | ||
502 | return IRQ_HANDLED; | |
503 | } | |
504 | ||
505 | static struct nfc_phy_ops i2c_phy_ops = { | |
506 | .write = st21nfca_hci_i2c_write, | |
507 | .enable = st21nfca_hci_i2c_enable, | |
508 | .disable = st21nfca_hci_i2c_disable, | |
509 | }; | |
510 | ||
c44cb2ed CR |
511 | #ifdef CONFIG_OF |
512 | static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client) | |
513 | { | |
514 | struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client); | |
515 | struct device_node *pp; | |
516 | int gpio; | |
517 | int r; | |
518 | ||
519 | pp = client->dev.of_node; | |
520 | if (!pp) | |
521 | return -ENODEV; | |
522 | ||
523 | /* Get GPIO from device tree */ | |
524 | gpio = of_get_named_gpio(pp, "enable-gpios", 0); | |
525 | if (gpio < 0) { | |
526 | nfc_err(&client->dev, "Failed to retrieve enable-gpios from device tree\n"); | |
527 | return gpio; | |
528 | } | |
529 | ||
530 | /* GPIO request and configuration */ | |
0be8ce73 AL |
531 | r = devm_gpio_request_one(&client->dev, gpio, GPIOF_OUT_INIT_HIGH, |
532 | "clf_enable"); | |
c44cb2ed CR |
533 | if (r) { |
534 | nfc_err(&client->dev, "Failed to request enable pin\n"); | |
67df3f95 | 535 | return r; |
c44cb2ed CR |
536 | } |
537 | ||
c44cb2ed CR |
538 | phy->gpio_ena = gpio; |
539 | ||
40af86a4 | 540 | phy->irq_polarity = irq_get_trigger_type(client->irq); |
c44cb2ed | 541 | |
2130fb97 CR |
542 | phy->se_status.is_ese_present = |
543 | of_property_read_bool(pp, "ese-present"); | |
544 | phy->se_status.is_uicc_present = | |
545 | of_property_read_bool(pp, "uicc-present"); | |
546 | ||
c44cb2ed CR |
547 | return 0; |
548 | } | |
549 | #else | |
550 | static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client) | |
551 | { | |
552 | return -ENODEV; | |
553 | } | |
554 | #endif | |
555 | ||
fcb45e6a | 556 | static int st21nfca_hci_i2c_request_resources(struct i2c_client *client) |
68957303 CR |
557 | { |
558 | struct st21nfca_nfc_platform_data *pdata; | |
fcb45e6a | 559 | struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client); |
68957303 CR |
560 | int r; |
561 | ||
562 | pdata = client->dev.platform_data; | |
563 | if (pdata == NULL) { | |
564 | nfc_err(&client->dev, "No platform data\n"); | |
565 | return -EINVAL; | |
566 | } | |
567 | ||
568 | /* store for later use */ | |
68957303 CR |
569 | phy->gpio_ena = pdata->gpio_ena; |
570 | phy->irq_polarity = pdata->irq_polarity; | |
68957303 | 571 | |
fcb45e6a | 572 | if (phy->gpio_ena > 0) { |
0be8ce73 AL |
573 | r = devm_gpio_request_one(&client->dev, phy->gpio_ena, |
574 | GPIOF_OUT_INIT_HIGH, "clf_enable"); | |
68957303 CR |
575 | if (r) { |
576 | pr_err("%s : ena gpio_request failed\n", __FILE__); | |
67df3f95 | 577 | return r; |
68957303 | 578 | } |
68957303 CR |
579 | } |
580 | ||
2130fb97 CR |
581 | phy->se_status.is_ese_present = pdata->is_ese_present; |
582 | phy->se_status.is_uicc_present = pdata->is_uicc_present; | |
583 | ||
fcb45e6a | 584 | return 0; |
68957303 CR |
585 | } |
586 | ||
587 | static int st21nfca_hci_i2c_probe(struct i2c_client *client, | |
588 | const struct i2c_device_id *id) | |
589 | { | |
590 | struct st21nfca_i2c_phy *phy; | |
591 | struct st21nfca_nfc_platform_data *pdata; | |
fcb45e6a | 592 | int r; |
68957303 CR |
593 | |
594 | dev_dbg(&client->dev, "%s\n", __func__); | |
595 | dev_dbg(&client->dev, "IRQ: %d\n", client->irq); | |
596 | ||
597 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | |
598 | nfc_err(&client->dev, "Need I2C_FUNC_I2C\n"); | |
599 | return -ENODEV; | |
600 | } | |
601 | ||
602 | phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy), | |
603 | GFP_KERNEL); | |
2b702832 | 604 | if (!phy) |
68957303 | 605 | return -ENOMEM; |
68957303 CR |
606 | |
607 | phy->i2c_dev = client; | |
fcb45e6a CR |
608 | phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL); |
609 | if (phy->pending_skb == NULL) | |
610 | return -ENOMEM; | |
68957303 | 611 | |
fcb45e6a CR |
612 | phy->current_read_len = 0; |
613 | phy->crc_trials = 0; | |
a3c5d8fb | 614 | mutex_init(&phy->phy_lock); |
68957303 CR |
615 | i2c_set_clientdata(client, phy); |
616 | ||
617 | pdata = client->dev.platform_data; | |
c44cb2ed CR |
618 | if (!pdata && client->dev.of_node) { |
619 | r = st21nfca_hci_i2c_of_request_resources(client); | |
620 | if (r) { | |
621 | nfc_err(&client->dev, "No platform data\n"); | |
622 | return r; | |
623 | } | |
624 | } else if (pdata) { | |
625 | r = st21nfca_hci_i2c_request_resources(client); | |
626 | if (r) { | |
627 | nfc_err(&client->dev, "Cannot get platform resources\n"); | |
628 | return r; | |
629 | } | |
630 | } else { | |
631 | nfc_err(&client->dev, "st21nfca platform resources not available\n"); | |
fcb45e6a CR |
632 | return -ENODEV; |
633 | } | |
fcb45e6a | 634 | |
18d2c624 CR |
635 | r = st21nfca_hci_platform_init(phy); |
636 | if (r < 0) { | |
637 | nfc_err(&client->dev, "Unable to reboot st21nfca\n"); | |
67df3f95 | 638 | return r; |
18d2c624 CR |
639 | } |
640 | ||
68957303 CR |
641 | r = devm_request_threaded_irq(&client->dev, client->irq, NULL, |
642 | st21nfca_hci_irq_thread_fn, | |
643 | phy->irq_polarity | IRQF_ONESHOT, | |
644 | ST21NFCA_HCI_DRIVER_NAME, phy); | |
645 | if (r < 0) { | |
646 | nfc_err(&client->dev, "Unable to register IRQ handler\n"); | |
647 | return r; | |
648 | } | |
649 | ||
9bac75d0 | 650 | return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME, |
2130fb97 CR |
651 | ST21NFCA_FRAME_HEADROOM, |
652 | ST21NFCA_FRAME_TAILROOM, | |
653 | ST21NFCA_HCI_LLC_MAX_PAYLOAD, | |
654 | &phy->hdev, | |
655 | &phy->se_status); | |
68957303 CR |
656 | } |
657 | ||
658 | static int st21nfca_hci_i2c_remove(struct i2c_client *client) | |
659 | { | |
660 | struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client); | |
661 | ||
662 | dev_dbg(&client->dev, "%s\n", __func__); | |
663 | ||
664 | st21nfca_hci_remove(phy->hdev); | |
665 | ||
666 | if (phy->powered) | |
667 | st21nfca_hci_i2c_disable(phy); | |
668 | ||
669 | return 0; | |
670 | } | |
671 | ||
17e40107 | 672 | #ifdef CONFIG_OF |
c44cb2ed | 673 | static const struct of_device_id of_st21nfca_i2c_match[] = { |
6b5fba4e | 674 | { .compatible = "st,st21nfca-i2c", }, |
c44cb2ed CR |
675 | { .compatible = "st,st21nfca_i2c", }, |
676 | {} | |
677 | }; | |
17e40107 CR |
678 | MODULE_DEVICE_TABLE(of, of_st21nfca_i2c_match); |
679 | #endif | |
c44cb2ed | 680 | |
68957303 CR |
681 | static struct i2c_driver st21nfca_hci_i2c_driver = { |
682 | .driver = { | |
fcb45e6a CR |
683 | .owner = THIS_MODULE, |
684 | .name = ST21NFCA_HCI_I2C_DRIVER_NAME, | |
c44cb2ed | 685 | .of_match_table = of_match_ptr(of_st21nfca_i2c_match), |
fcb45e6a | 686 | }, |
68957303 CR |
687 | .probe = st21nfca_hci_i2c_probe, |
688 | .id_table = st21nfca_hci_i2c_id_table, | |
689 | .remove = st21nfca_hci_i2c_remove, | |
690 | }; | |
691 | ||
692 | module_i2c_driver(st21nfca_hci_i2c_driver); | |
693 | ||
694 | MODULE_LICENSE("GPL"); | |
695 | MODULE_DESCRIPTION(DRIVER_DESC); |