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> | |
dfa8070d | 23 | #include <linux/gpio/consumer.h> |
c44cb2ed CR |
24 | #include <linux/of_irq.h> |
25 | #include <linux/of_gpio.h> | |
dfa8070d | 26 | #include <linux/acpi.h> |
68957303 CR |
27 | #include <linux/interrupt.h> |
28 | #include <linux/delay.h> | |
29 | #include <linux/nfc.h> | |
30 | #include <linux/firmware.h> | |
79557b33 | 31 | |
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 | 60 | |
79557b33 | 61 | #define ST21NFCA_HCI_DRIVER_NAME "st21nfca_hci" |
68957303 CR |
62 | #define ST21NFCA_HCI_I2C_DRIVER_NAME "st21nfca_hci_i2c" |
63 | ||
2a196975 | 64 | #define ST21NFCA_GPIO_NAME_EN "enable" |
04e99b71 | 65 | |
68957303 CR |
66 | struct st21nfca_i2c_phy { |
67 | struct i2c_client *i2c_dev; | |
68 | struct nfc_hci_dev *hdev; | |
69 | ||
8d3c50e2 | 70 | struct gpio_desc *gpiod_ena; |
2130fb97 CR |
71 | struct st21nfca_se_status se_status; |
72 | ||
68957303 CR |
73 | 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; | |
a3c5d8fb | 89 | struct mutex phy_lock; |
68957303 | 90 | }; |
57dc828a | 91 | |
0531107e | 92 | static u8 len_seq[] = { 16, 24, 12, 29 }; |
68957303 CR |
93 | static u16 wait_tab[] = { 2, 3, 5, 15, 20, 40}; |
94 | ||
95 | #define I2C_DUMP_SKB(info, skb) \ | |
96 | do { \ | |
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 | ||
18d2c624 CR |
102 | /* |
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 | */ | |
108 | static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy) | |
68957303 | 109 | { |
c5b0c370 | 110 | u16 wait_reboot[] = { 50, 300, 1000 }; |
68957303 CR |
111 | char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E }; |
112 | u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE]; | |
113 | int i, r = -1; | |
114 | ||
18d2c624 CR |
115 | for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) { |
116 | r = i2c_master_send(phy->i2c_dev, reboot_cmd, | |
117 | sizeof(reboot_cmd)); | |
118 | if (r < 0) | |
119 | msleep(wait_reboot[i]); | |
120 | } | |
121 | if (r < 0) | |
122 | return r; | |
123 | ||
124 | /* 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++) { | |
68957303 CR |
128 | r = i2c_master_recv(phy->i2c_dev, tmp, |
129 | ST21NFCA_HCI_LLC_MAX_SIZE); | |
18d2c624 CR |
130 | if (r < 0) |
131 | msleep(wait_reboot[i]); | |
132 | } | |
133 | if (r < 0) | |
134 | return r; | |
68957303 | 135 | |
18d2c624 CR |
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; | |
68957303 | 142 | |
18d2c624 CR |
143 | usleep_range(1000, 1500); |
144 | return 0; | |
68957303 CR |
145 | } |
146 | ||
147 | static int st21nfca_hci_i2c_enable(void *phy_id) | |
148 | { | |
149 | struct st21nfca_i2c_phy *phy = phy_id; | |
150 | ||
8d3c50e2 | 151 | gpiod_set_value(phy->gpiod_ena, 1); |
68957303 CR |
152 | phy->powered = 1; |
153 | phy->run_mode = ST21NFCA_HCI_MODE; | |
154 | ||
155 | usleep_range(10000, 15000); | |
156 | ||
157 | return 0; | |
158 | } | |
159 | ||
160 | static void st21nfca_hci_i2c_disable(void *phy_id) | |
161 | { | |
162 | struct st21nfca_i2c_phy *phy = phy_id; | |
163 | ||
8d3c50e2 | 164 | gpiod_set_value(phy->gpiod_ena, 0); |
68957303 CR |
165 | |
166 | phy->powered = 0; | |
167 | } | |
168 | ||
e1fb97b9 | 169 | static void st21nfca_hci_add_len_crc(struct sk_buff *skb) |
68957303 | 170 | { |
68957303 CR |
171 | u16 crc; |
172 | u8 tmp; | |
173 | ||
174 | *skb_push(skb, 1) = 0; | |
175 | ||
176 | crc = crc_ccitt(0xffff, skb->data, skb->len); | |
177 | crc = ~crc; | |
178 | ||
179 | tmp = crc & 0x00ff; | |
180 | *skb_put(skb, 1) = tmp; | |
181 | ||
182 | tmp = (crc >> 8) & 0x00ff; | |
183 | *skb_put(skb, 1) = tmp; | |
68957303 CR |
184 | } |
185 | ||
e1fb97b9 | 186 | static void st21nfca_hci_remove_len_crc(struct sk_buff *skb) |
68957303 CR |
187 | { |
188 | skb_pull(skb, ST21NFCA_FRAME_HEADROOM); | |
e1fb97b9 | 189 | skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM); |
68957303 CR |
190 | } |
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 | */ | |
197 | static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb) | |
198 | { | |
e1fb97b9 | 199 | int r = -1, i, j; |
68957303 CR |
200 | struct st21nfca_i2c_phy *phy = phy_id; |
201 | struct i2c_client *client = phy->i2c_dev; | |
68957303 CR |
202 | u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2]; |
203 | ||
204 | I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb); | |
205 | ||
68957303 CR |
206 | 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 | */ | |
e1fb97b9 | 214 | st21nfca_hci_add_len_crc(skb); |
68957303 CR |
215 | |
216 | /* add ST21NFCA_SOF_EOF on tail */ | |
217 | *skb_put(skb, 1) = ST21NFCA_SOF_EOF; | |
218 | /* add ST21NFCA_SOF_EOF on head */ | |
219 | *skb_push(skb, 1) = ST21NFCA_SOF_EOF; | |
220 | ||
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 | */ | |
a3c5d8fb | 245 | mutex_lock(&phy->phy_lock); |
68957303 CR |
246 | 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 | } | |
a3c5d8fb | 251 | mutex_unlock(&phy->phy_lock); |
68957303 CR |
252 | |
253 | if (r >= 0) { | |
254 | if (r != j) | |
255 | r = -EREMOTEIO; | |
256 | else | |
257 | r = 0; | |
258 | } | |
259 | ||
e1fb97b9 | 260 | st21nfca_hci_remove_len_crc(skb); |
68957303 CR |
261 | |
262 | return r; | |
263 | } | |
264 | ||
265 | static int get_frame_size(u8 *buf, int buflen) | |
266 | { | |
267 | int len = 0; | |
3e6df919 | 268 | |
68957303 CR |
269 | 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 | ||
278 | static 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 | */ | |
306 | static int st21nfca_hci_i2c_repack(struct sk_buff *skb) | |
307 | { | |
308 | int i, j, r, size; | |
3e6df919 | 309 | |
68957303 CR |
310 | 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++) { | |
3096e25a | 318 | if (skb->data[i + j] == |
68957303 | 319 | (u8) ST21NFCA_ESCAPE_BYTE_STUFFING) { |
3096e25a CR |
320 | skb->data[i] = skb->data[i + j + 1] |
321 | | ST21NFCA_BYTE_STUFFING_MASK; | |
68957303 CR |
322 | 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 | */ | |
362 | static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy, | |
363 | struct sk_buff *skb) | |
364 | { | |
365 | int r, i; | |
366 | u8 len; | |
c97ffdbf | 367 | u8 buf[ST21NFCA_HCI_LLC_MAX_PAYLOAD]; |
68957303 CR |
368 | 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; | |
a3c5d8fb | 379 | mutex_lock(&phy->phy_lock); |
68957303 | 380 | for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) { |
c97ffdbf | 381 | r = i2c_master_recv(client, buf, len); |
68957303 CR |
382 | if (r < 0) |
383 | msleep(wait_tab[i]); | |
384 | } | |
a3c5d8fb | 385 | mutex_unlock(&phy->phy_lock); |
68957303 CR |
386 | |
387 | if (r != len) { | |
388 | phy->current_read_len = 0; | |
389 | return -EREMOTEIO; | |
390 | } | |
391 | ||
c97ffdbf CR |
392 | /* |
393 | * The first read sequence does not start with SOF. | |
394 | * Data is corrupeted so we drop it. | |
395 | */ | |
8e9466cc | 396 | if (!phy->current_read_len && !IS_START_OF_FRAME(buf)) { |
c97ffdbf CR |
397 | skb_trim(skb, 0); |
398 | phy->current_read_len = 0; | |
399 | return -EIO; | |
8e9466cc | 400 | } else if (phy->current_read_len && IS_START_OF_FRAME(buf)) { |
c97ffdbf CR |
401 | /* |
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 | ||
59ae1d12 | 410 | skb_put_data(skb, buf, len); |
c97ffdbf CR |
411 | |
412 | if (skb->data[skb->len - 1] == ST21NFCA_SOF_EOF) { | |
68957303 CR |
413 | 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 | */ | |
436 | static 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; | |
0c942b00 | 476 | kfree_skb(phy->pending_skb); |
68957303 CR |
477 | } 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; | |
cf577344 CR |
485 | } else { |
486 | kfree_skb(phy->pending_skb); | |
68957303 CR |
487 | } |
488 | ||
489 | phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL); | |
490 | if (phy->pending_skb == NULL) { | |
491 | phy->hard_fault = -ENOMEM; | |
492 | nfc_hci_recv_frame(phy->hdev, NULL); | |
493 | } | |
494 | ||
495 | return IRQ_HANDLED; | |
496 | } | |
497 | ||
498 | static 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 | ||
dfa8070d CR |
504 | static int st21nfca_hci_i2c_acpi_request_resources(struct i2c_client *client) |
505 | { | |
506 | struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client); | |
070718a4 | 507 | struct device *dev = &client->dev; |
dfa8070d | 508 | |
dfa8070d | 509 | /* Get EN GPIO from ACPI */ |
8d3c50e2 AS |
510 | phy->gpiod_ena = devm_gpiod_get_index(dev, ST21NFCA_GPIO_NAME_EN, 1, |
511 | GPIOD_OUT_LOW); | |
512 | if (IS_ERR(phy->gpiod_ena)) { | |
b58afe6d | 513 | nfc_err(dev, "Unable to get ENABLE GPIO\n"); |
8d3c50e2 | 514 | return PTR_ERR(phy->gpiod_ena); |
b58afe6d | 515 | } |
dfa8070d | 516 | |
dfa8070d CR |
517 | return 0; |
518 | } | |
519 | ||
c44cb2ed CR |
520 | static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client) |
521 | { | |
522 | struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client); | |
8d3c50e2 | 523 | struct device *dev = &client->dev; |
c44cb2ed CR |
524 | |
525 | /* Get GPIO from device tree */ | |
8d3c50e2 AS |
526 | 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); | |
c44cb2ed CR |
531 | } |
532 | ||
c44cb2ed CR |
533 | return 0; |
534 | } | |
c44cb2ed | 535 | |
68957303 CR |
536 | static int st21nfca_hci_i2c_probe(struct i2c_client *client, |
537 | const struct i2c_device_id *id) | |
538 | { | |
539 | struct st21nfca_i2c_phy *phy; | |
fcb45e6a | 540 | int r; |
68957303 CR |
541 | |
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); | |
2b702832 | 552 | if (!phy) |
68957303 | 553 | return -ENOMEM; |
68957303 CR |
554 | |
555 | phy->i2c_dev = client; | |
fcb45e6a CR |
556 | phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL); |
557 | if (phy->pending_skb == NULL) | |
558 | return -ENOMEM; | |
68957303 | 559 | |
fcb45e6a CR |
560 | phy->current_read_len = 0; |
561 | phy->crc_trials = 0; | |
a3c5d8fb | 562 | mutex_init(&phy->phy_lock); |
68957303 CR |
563 | i2c_set_clientdata(client, phy); |
564 | ||
79557b33 | 565 | if (client->dev.of_node) { |
c44cb2ed CR |
566 | r = st21nfca_hci_i2c_of_request_resources(client); |
567 | if (r) { | |
568 | nfc_err(&client->dev, "No platform data\n"); | |
569 | return r; | |
570 | } | |
dfa8070d CR |
571 | } 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 | } | |
c44cb2ed CR |
577 | } else { |
578 | nfc_err(&client->dev, "st21nfca platform resources not available\n"); | |
fcb45e6a CR |
579 | return -ENODEV; |
580 | } | |
fcb45e6a | 581 | |
682fd618 AS |
582 | 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 | ||
18d2c624 CR |
587 | r = st21nfca_hci_platform_init(phy); |
588 | if (r < 0) { | |
589 | nfc_err(&client->dev, "Unable to reboot st21nfca\n"); | |
67df3f95 | 590 | return r; |
18d2c624 CR |
591 | } |
592 | ||
68957303 CR |
593 | r = devm_request_threaded_irq(&client->dev, client->irq, NULL, |
594 | st21nfca_hci_irq_thread_fn, | |
8e7836d0 | 595 | IRQF_ONESHOT, |
68957303 CR |
596 | 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 | ||
9bac75d0 | 602 | return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME, |
2130fb97 CR |
603 | ST21NFCA_FRAME_HEADROOM, |
604 | ST21NFCA_FRAME_TAILROOM, | |
605 | ST21NFCA_HCI_LLC_MAX_PAYLOAD, | |
606 | &phy->hdev, | |
607 | &phy->se_status); | |
68957303 CR |
608 | } |
609 | ||
610 | static 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 | ||
39db2d21 CR |
624 | static struct i2c_device_id st21nfca_hci_i2c_id_table[] = { |
625 | {ST21NFCA_HCI_DRIVER_NAME, 0}, | |
626 | {} | |
627 | }; | |
628 | MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table); | |
629 | ||
dfa8070d CR |
630 | static const struct acpi_device_id st21nfca_hci_i2c_acpi_match[] = { |
631 | {"SMO2100", 0}, | |
632 | {} | |
633 | }; | |
634 | MODULE_DEVICE_TABLE(acpi, st21nfca_hci_i2c_acpi_match); | |
635 | ||
c44cb2ed | 636 | static const struct of_device_id of_st21nfca_i2c_match[] = { |
6b5fba4e | 637 | { .compatible = "st,st21nfca-i2c", }, |
c44cb2ed CR |
638 | { .compatible = "st,st21nfca_i2c", }, |
639 | {} | |
640 | }; | |
17e40107 | 641 | MODULE_DEVICE_TABLE(of, of_st21nfca_i2c_match); |
c44cb2ed | 642 | |
68957303 CR |
643 | static struct i2c_driver st21nfca_hci_i2c_driver = { |
644 | .driver = { | |
fcb45e6a | 645 | .name = ST21NFCA_HCI_I2C_DRIVER_NAME, |
c44cb2ed | 646 | .of_match_table = of_match_ptr(of_st21nfca_i2c_match), |
dfa8070d | 647 | .acpi_match_table = ACPI_PTR(st21nfca_hci_i2c_acpi_match), |
fcb45e6a | 648 | }, |
68957303 CR |
649 | .probe = st21nfca_hci_i2c_probe, |
650 | .id_table = st21nfca_hci_i2c_id_table, | |
651 | .remove = st21nfca_hci_i2c_remove, | |
652 | }; | |
68957303 CR |
653 | module_i2c_driver(st21nfca_hci_i2c_driver); |
654 | ||
655 | MODULE_LICENSE("GPL"); | |
656 | MODULE_DESCRIPTION(DRIVER_DESC); |