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