diff options
Diffstat (limited to 'drivers/nfc/pn544/i2c.c')
-rw-r--r-- | drivers/nfc/pn544/i2c.c | 500 |
1 files changed, 500 insertions, 0 deletions
diff --git a/drivers/nfc/pn544/i2c.c b/drivers/nfc/pn544/i2c.c new file mode 100644 index 000000000000..fb430d882352 --- /dev/null +++ b/drivers/nfc/pn544/i2c.c | |||
@@ -0,0 +1,500 @@ | |||
1 | /* | ||
2 | * I2C Link Layer for PN544 HCI based Driver | ||
3 | * | ||
4 | * Copyright (C) 2012 Intel Corporation. All rights reserved. | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify it | ||
7 | * under the terms and conditions of the GNU General Public License, | ||
8 | * version 2, as published by the Free Software Foundation. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, write to the | ||
17 | * Free Software Foundation, Inc., | ||
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | #include <linux/crc-ccitt.h> | ||
22 | #include <linux/module.h> | ||
23 | #include <linux/i2c.h> | ||
24 | #include <linux/gpio.h> | ||
25 | #include <linux/miscdevice.h> | ||
26 | #include <linux/interrupt.h> | ||
27 | #include <linux/delay.h> | ||
28 | |||
29 | #include <linux/nfc/pn544.h> | ||
30 | |||
31 | #include <net/nfc/hci.h> | ||
32 | #include <net/nfc/llc.h> | ||
33 | |||
34 | #include "pn544.h" | ||
35 | |||
36 | #define PN544_I2C_FRAME_HEADROOM 1 | ||
37 | #define PN544_I2C_FRAME_TAILROOM 2 | ||
38 | |||
39 | /* framing in HCI mode */ | ||
40 | #define PN544_HCI_I2C_LLC_LEN 1 | ||
41 | #define PN544_HCI_I2C_LLC_CRC 2 | ||
42 | #define PN544_HCI_I2C_LLC_LEN_CRC (PN544_HCI_I2C_LLC_LEN + \ | ||
43 | PN544_HCI_I2C_LLC_CRC) | ||
44 | #define PN544_HCI_I2C_LLC_MIN_SIZE (1 + PN544_HCI_I2C_LLC_LEN_CRC) | ||
45 | #define PN544_HCI_I2C_LLC_MAX_PAYLOAD 29 | ||
46 | #define PN544_HCI_I2C_LLC_MAX_SIZE (PN544_HCI_I2C_LLC_LEN_CRC + 1 + \ | ||
47 | PN544_HCI_I2C_LLC_MAX_PAYLOAD) | ||
48 | |||
49 | static struct i2c_device_id pn544_hci_i2c_id_table[] = { | ||
50 | {"pn544", 0}, | ||
51 | {} | ||
52 | }; | ||
53 | |||
54 | MODULE_DEVICE_TABLE(i2c, pn544_hci_i2c_id_table); | ||
55 | |||
56 | #define PN544_HCI_I2C_DRIVER_NAME "pn544_hci_i2c" | ||
57 | |||
58 | struct pn544_i2c_phy { | ||
59 | struct i2c_client *i2c_dev; | ||
60 | struct nfc_hci_dev *hdev; | ||
61 | |||
62 | unsigned int gpio_en; | ||
63 | unsigned int gpio_irq; | ||
64 | unsigned int gpio_fw; | ||
65 | unsigned int en_polarity; | ||
66 | |||
67 | int powered; | ||
68 | |||
69 | int hard_fault; /* | ||
70 | * < 0 if hardware error occured (e.g. i2c err) | ||
71 | * and prevents normal operation. | ||
72 | */ | ||
73 | }; | ||
74 | |||
75 | #define I2C_DUMP_SKB(info, skb) \ | ||
76 | do { \ | ||
77 | pr_debug("%s:\n", info); \ | ||
78 | print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \ | ||
79 | 16, 1, (skb)->data, (skb)->len, 0); \ | ||
80 | } while (0) | ||
81 | |||
82 | static void pn544_hci_i2c_platform_init(struct pn544_i2c_phy *phy) | ||
83 | { | ||
84 | int polarity, retry, ret; | ||
85 | char rset_cmd[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 }; | ||
86 | int count = sizeof(rset_cmd); | ||
87 | |||
88 | pr_info(DRIVER_DESC ": %s\n", __func__); | ||
89 | dev_info(&phy->i2c_dev->dev, "Detecting nfc_en polarity\n"); | ||
90 | |||
91 | /* Disable fw download */ | ||
92 | gpio_set_value(phy->gpio_fw, 0); | ||
93 | |||
94 | for (polarity = 0; polarity < 2; polarity++) { | ||
95 | phy->en_polarity = polarity; | ||
96 | retry = 3; | ||
97 | while (retry--) { | ||
98 | /* power off */ | ||
99 | gpio_set_value(phy->gpio_en, !phy->en_polarity); | ||
100 | usleep_range(10000, 15000); | ||
101 | |||
102 | /* power on */ | ||
103 | gpio_set_value(phy->gpio_en, phy->en_polarity); | ||
104 | usleep_range(10000, 15000); | ||
105 | |||
106 | /* send reset */ | ||
107 | dev_dbg(&phy->i2c_dev->dev, "Sending reset cmd\n"); | ||
108 | ret = i2c_master_send(phy->i2c_dev, rset_cmd, count); | ||
109 | if (ret == count) { | ||
110 | dev_info(&phy->i2c_dev->dev, | ||
111 | "nfc_en polarity : active %s\n", | ||
112 | (polarity == 0 ? "low" : "high")); | ||
113 | goto out; | ||
114 | } | ||
115 | } | ||
116 | } | ||
117 | |||
118 | dev_err(&phy->i2c_dev->dev, | ||
119 | "Could not detect nfc_en polarity, fallback to active high\n"); | ||
120 | |||
121 | out: | ||
122 | gpio_set_value(phy->gpio_en, !phy->en_polarity); | ||
123 | } | ||
124 | |||
125 | static int pn544_hci_i2c_enable(void *phy_id) | ||
126 | { | ||
127 | struct pn544_i2c_phy *phy = phy_id; | ||
128 | |||
129 | pr_info(DRIVER_DESC ": %s\n", __func__); | ||
130 | |||
131 | gpio_set_value(phy->gpio_fw, 0); | ||
132 | gpio_set_value(phy->gpio_en, phy->en_polarity); | ||
133 | usleep_range(10000, 15000); | ||
134 | |||
135 | phy->powered = 1; | ||
136 | |||
137 | return 0; | ||
138 | } | ||
139 | |||
140 | static void pn544_hci_i2c_disable(void *phy_id) | ||
141 | { | ||
142 | struct pn544_i2c_phy *phy = phy_id; | ||
143 | |||
144 | pr_info(DRIVER_DESC ": %s\n", __func__); | ||
145 | |||
146 | gpio_set_value(phy->gpio_fw, 0); | ||
147 | gpio_set_value(phy->gpio_en, !phy->en_polarity); | ||
148 | usleep_range(10000, 15000); | ||
149 | |||
150 | gpio_set_value(phy->gpio_en, phy->en_polarity); | ||
151 | usleep_range(10000, 15000); | ||
152 | |||
153 | gpio_set_value(phy->gpio_en, !phy->en_polarity); | ||
154 | usleep_range(10000, 15000); | ||
155 | |||
156 | phy->powered = 0; | ||
157 | } | ||
158 | |||
159 | static void pn544_hci_i2c_add_len_crc(struct sk_buff *skb) | ||
160 | { | ||
161 | u16 crc; | ||
162 | int len; | ||
163 | |||
164 | len = skb->len + 2; | ||
165 | *skb_push(skb, 1) = len; | ||
166 | |||
167 | crc = crc_ccitt(0xffff, skb->data, skb->len); | ||
168 | crc = ~crc; | ||
169 | *skb_put(skb, 1) = crc & 0xff; | ||
170 | *skb_put(skb, 1) = crc >> 8; | ||
171 | } | ||
172 | |||
173 | static void pn544_hci_i2c_remove_len_crc(struct sk_buff *skb) | ||
174 | { | ||
175 | skb_pull(skb, PN544_I2C_FRAME_HEADROOM); | ||
176 | skb_trim(skb, PN544_I2C_FRAME_TAILROOM); | ||
177 | } | ||
178 | |||
179 | /* | ||
180 | * Writing a frame must not return the number of written bytes. | ||
181 | * It must return either zero for success, or <0 for error. | ||
182 | * In addition, it must not alter the skb | ||
183 | */ | ||
184 | static int pn544_hci_i2c_write(void *phy_id, struct sk_buff *skb) | ||
185 | { | ||
186 | int r; | ||
187 | struct pn544_i2c_phy *phy = phy_id; | ||
188 | struct i2c_client *client = phy->i2c_dev; | ||
189 | |||
190 | if (phy->hard_fault != 0) | ||
191 | return phy->hard_fault; | ||
192 | |||
193 | usleep_range(3000, 6000); | ||
194 | |||
195 | pn544_hci_i2c_add_len_crc(skb); | ||
196 | |||
197 | I2C_DUMP_SKB("i2c frame written", skb); | ||
198 | |||
199 | r = i2c_master_send(client, skb->data, skb->len); | ||
200 | |||
201 | if (r == -EREMOTEIO) { /* Retry, chip was in standby */ | ||
202 | usleep_range(6000, 10000); | ||
203 | r = i2c_master_send(client, skb->data, skb->len); | ||
204 | } | ||
205 | |||
206 | if (r >= 0) { | ||
207 | if (r != skb->len) | ||
208 | r = -EREMOTEIO; | ||
209 | else | ||
210 | r = 0; | ||
211 | } | ||
212 | |||
213 | pn544_hci_i2c_remove_len_crc(skb); | ||
214 | |||
215 | return r; | ||
216 | } | ||
217 | |||
218 | static int check_crc(u8 *buf, int buflen) | ||
219 | { | ||
220 | int len; | ||
221 | u16 crc; | ||
222 | |||
223 | len = buf[0] + 1; | ||
224 | crc = crc_ccitt(0xffff, buf, len - 2); | ||
225 | crc = ~crc; | ||
226 | |||
227 | if (buf[len - 2] != (crc & 0xff) || buf[len - 1] != (crc >> 8)) { | ||
228 | pr_err(PN544_HCI_I2C_DRIVER_NAME | ||
229 | ": CRC error 0x%x != 0x%x 0x%x\n", | ||
230 | crc, buf[len - 1], buf[len - 2]); | ||
231 | |||
232 | pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__); | ||
233 | print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE, | ||
234 | 16, 2, buf, buflen, false); | ||
235 | return -EPERM; | ||
236 | } | ||
237 | return 0; | ||
238 | } | ||
239 | |||
240 | /* | ||
241 | * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees | ||
242 | * that i2c bus will be flushed and that next read will start on a new frame. | ||
243 | * returned skb contains only LLC header and payload. | ||
244 | * returns: | ||
245 | * -EREMOTEIO : i2c read error (fatal) | ||
246 | * -EBADMSG : frame was incorrect and discarded | ||
247 | * -ENOMEM : cannot allocate skb, frame dropped | ||
248 | */ | ||
249 | static int pn544_hci_i2c_read(struct pn544_i2c_phy *phy, struct sk_buff **skb) | ||
250 | { | ||
251 | int r; | ||
252 | u8 len; | ||
253 | u8 tmp[PN544_HCI_I2C_LLC_MAX_SIZE - 1]; | ||
254 | struct i2c_client *client = phy->i2c_dev; | ||
255 | |||
256 | r = i2c_master_recv(client, &len, 1); | ||
257 | if (r != 1) { | ||
258 | dev_err(&client->dev, "cannot read len byte\n"); | ||
259 | return -EREMOTEIO; | ||
260 | } | ||
261 | |||
262 | if ((len < (PN544_HCI_I2C_LLC_MIN_SIZE - 1)) || | ||
263 | (len > (PN544_HCI_I2C_LLC_MAX_SIZE - 1))) { | ||
264 | dev_err(&client->dev, "invalid len byte\n"); | ||
265 | r = -EBADMSG; | ||
266 | goto flush; | ||
267 | } | ||
268 | |||
269 | *skb = alloc_skb(1 + len, GFP_KERNEL); | ||
270 | if (*skb == NULL) { | ||
271 | r = -ENOMEM; | ||
272 | goto flush; | ||
273 | } | ||
274 | |||
275 | *skb_put(*skb, 1) = len; | ||
276 | |||
277 | r = i2c_master_recv(client, skb_put(*skb, len), len); | ||
278 | if (r != len) { | ||
279 | kfree_skb(*skb); | ||
280 | return -EREMOTEIO; | ||
281 | } | ||
282 | |||
283 | I2C_DUMP_SKB("i2c frame read", *skb); | ||
284 | |||
285 | r = check_crc((*skb)->data, (*skb)->len); | ||
286 | if (r != 0) { | ||
287 | kfree_skb(*skb); | ||
288 | r = -EBADMSG; | ||
289 | goto flush; | ||
290 | } | ||
291 | |||
292 | skb_pull(*skb, 1); | ||
293 | skb_trim(*skb, (*skb)->len - 2); | ||
294 | |||
295 | usleep_range(3000, 6000); | ||
296 | |||
297 | return 0; | ||
298 | |||
299 | flush: | ||
300 | if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0) | ||
301 | r = -EREMOTEIO; | ||
302 | |||
303 | usleep_range(3000, 6000); | ||
304 | |||
305 | return r; | ||
306 | } | ||
307 | |||
308 | /* | ||
309 | * Reads an shdlc frame from the chip. This is not as straightforward as it | ||
310 | * seems. There are cases where we could loose the frame start synchronization. | ||
311 | * The frame format is len-data-crc, and corruption can occur anywhere while | ||
312 | * transiting on i2c bus, such that we could read an invalid len. | ||
313 | * In order to recover synchronization with the next frame, we must be sure | ||
314 | * to read the real amount of data without using the len byte. We do this by | ||
315 | * assuming the following: | ||
316 | * - the chip will always present only one single complete frame on the bus | ||
317 | * before triggering the interrupt | ||
318 | * - the chip will not present a new frame until we have completely read | ||
319 | * the previous one (or until we have handled the interrupt). | ||
320 | * The tricky case is when we read a corrupted len that is less than the real | ||
321 | * len. We must detect this here in order to determine that we need to flush | ||
322 | * the bus. This is the reason why we check the crc here. | ||
323 | */ | ||
324 | static irqreturn_t pn544_hci_i2c_irq_thread_fn(int irq, void *phy_id) | ||
325 | { | ||
326 | struct pn544_i2c_phy *phy = phy_id; | ||
327 | struct i2c_client *client; | ||
328 | struct sk_buff *skb = NULL; | ||
329 | int r; | ||
330 | |||
331 | if (!phy || irq != phy->i2c_dev->irq) { | ||
332 | WARN_ON_ONCE(1); | ||
333 | return IRQ_NONE; | ||
334 | } | ||
335 | |||
336 | client = phy->i2c_dev; | ||
337 | dev_dbg(&client->dev, "IRQ\n"); | ||
338 | |||
339 | if (phy->hard_fault != 0) | ||
340 | return IRQ_HANDLED; | ||
341 | |||
342 | r = pn544_hci_i2c_read(phy, &skb); | ||
343 | if (r == -EREMOTEIO) { | ||
344 | phy->hard_fault = r; | ||
345 | |||
346 | nfc_hci_recv_frame(phy->hdev, NULL); | ||
347 | |||
348 | return IRQ_HANDLED; | ||
349 | } else if ((r == -ENOMEM) || (r == -EBADMSG)) { | ||
350 | return IRQ_HANDLED; | ||
351 | } | ||
352 | |||
353 | nfc_hci_recv_frame(phy->hdev, skb); | ||
354 | |||
355 | return IRQ_HANDLED; | ||
356 | } | ||
357 | |||
358 | static struct nfc_phy_ops i2c_phy_ops = { | ||
359 | .write = pn544_hci_i2c_write, | ||
360 | .enable = pn544_hci_i2c_enable, | ||
361 | .disable = pn544_hci_i2c_disable, | ||
362 | }; | ||
363 | |||
364 | static int __devinit pn544_hci_i2c_probe(struct i2c_client *client, | ||
365 | const struct i2c_device_id *id) | ||
366 | { | ||
367 | struct pn544_i2c_phy *phy; | ||
368 | struct pn544_nfc_platform_data *pdata; | ||
369 | int r = 0; | ||
370 | |||
371 | dev_dbg(&client->dev, "%s\n", __func__); | ||
372 | dev_dbg(&client->dev, "IRQ: %d\n", client->irq); | ||
373 | |||
374 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | ||
375 | dev_err(&client->dev, "Need I2C_FUNC_I2C\n"); | ||
376 | return -ENODEV; | ||
377 | } | ||
378 | |||
379 | phy = kzalloc(sizeof(struct pn544_i2c_phy), GFP_KERNEL); | ||
380 | if (!phy) { | ||
381 | dev_err(&client->dev, | ||
382 | "Cannot allocate memory for pn544 i2c phy.\n"); | ||
383 | r = -ENOMEM; | ||
384 | goto err_phy_alloc; | ||
385 | } | ||
386 | |||
387 | phy->i2c_dev = client; | ||
388 | i2c_set_clientdata(client, phy); | ||
389 | |||
390 | pdata = client->dev.platform_data; | ||
391 | if (pdata == NULL) { | ||
392 | dev_err(&client->dev, "No platform data\n"); | ||
393 | r = -EINVAL; | ||
394 | goto err_pdata; | ||
395 | } | ||
396 | |||
397 | if (pdata->request_resources == NULL) { | ||
398 | dev_err(&client->dev, "request_resources() missing\n"); | ||
399 | r = -EINVAL; | ||
400 | goto err_pdata; | ||
401 | } | ||
402 | |||
403 | r = pdata->request_resources(client); | ||
404 | if (r) { | ||
405 | dev_err(&client->dev, "Cannot get platform resources\n"); | ||
406 | goto err_pdata; | ||
407 | } | ||
408 | |||
409 | phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE); | ||
410 | phy->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET); | ||
411 | phy->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ); | ||
412 | |||
413 | pn544_hci_i2c_platform_init(phy); | ||
414 | |||
415 | r = request_threaded_irq(client->irq, NULL, pn544_hci_i2c_irq_thread_fn, | ||
416 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, | ||
417 | PN544_HCI_I2C_DRIVER_NAME, phy); | ||
418 | if (r < 0) { | ||
419 | dev_err(&client->dev, "Unable to register IRQ handler\n"); | ||
420 | goto err_rti; | ||
421 | } | ||
422 | |||
423 | r = pn544_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME, | ||
424 | PN544_I2C_FRAME_HEADROOM, PN544_I2C_FRAME_TAILROOM, | ||
425 | PN544_HCI_I2C_LLC_MAX_PAYLOAD, &phy->hdev); | ||
426 | if (r < 0) | ||
427 | goto err_hci; | ||
428 | |||
429 | return 0; | ||
430 | |||
431 | err_hci: | ||
432 | free_irq(client->irq, phy); | ||
433 | |||
434 | err_rti: | ||
435 | if (pdata->free_resources != NULL) | ||
436 | pdata->free_resources(); | ||
437 | |||
438 | err_pdata: | ||
439 | kfree(phy); | ||
440 | |||
441 | err_phy_alloc: | ||
442 | return r; | ||
443 | } | ||
444 | |||
445 | static __devexit int pn544_hci_i2c_remove(struct i2c_client *client) | ||
446 | { | ||
447 | struct pn544_i2c_phy *phy = i2c_get_clientdata(client); | ||
448 | struct pn544_nfc_platform_data *pdata = client->dev.platform_data; | ||
449 | |||
450 | dev_dbg(&client->dev, "%s\n", __func__); | ||
451 | |||
452 | pn544_hci_remove(phy->hdev); | ||
453 | |||
454 | if (phy->powered) | ||
455 | pn544_hci_i2c_disable(phy); | ||
456 | |||
457 | free_irq(client->irq, phy); | ||
458 | if (pdata->free_resources) | ||
459 | pdata->free_resources(); | ||
460 | |||
461 | kfree(phy); | ||
462 | |||
463 | return 0; | ||
464 | } | ||
465 | |||
466 | static struct i2c_driver pn544_hci_i2c_driver = { | ||
467 | .driver = { | ||
468 | .name = PN544_HCI_I2C_DRIVER_NAME, | ||
469 | }, | ||
470 | .probe = pn544_hci_i2c_probe, | ||
471 | .id_table = pn544_hci_i2c_id_table, | ||
472 | .remove = __devexit_p(pn544_hci_i2c_remove), | ||
473 | }; | ||
474 | |||
475 | static int __init pn544_hci_i2c_init(void) | ||
476 | { | ||
477 | int r; | ||
478 | |||
479 | pr_debug(DRIVER_DESC ": %s\n", __func__); | ||
480 | |||
481 | r = i2c_add_driver(&pn544_hci_i2c_driver); | ||
482 | if (r) { | ||
483 | pr_err(PN544_HCI_I2C_DRIVER_NAME | ||
484 | ": driver registration failed\n"); | ||
485 | return r; | ||
486 | } | ||
487 | |||
488 | return 0; | ||
489 | } | ||
490 | |||
491 | static void __exit pn544_hci_i2c_exit(void) | ||
492 | { | ||
493 | i2c_del_driver(&pn544_hci_i2c_driver); | ||
494 | } | ||
495 | |||
496 | module_init(pn544_hci_i2c_init); | ||
497 | module_exit(pn544_hci_i2c_exit); | ||
498 | |||
499 | MODULE_LICENSE("GPL"); | ||
500 | MODULE_DESCRIPTION(DRIVER_DESC); | ||