]>
Commit | Line | Data |
---|---|---|
97f18414 EL |
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 | ||
61cdb018 | 29 | #include <linux/platform_data/pn544.h> |
97f18414 EL |
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 | ||
0fe763c5 GKH |
364 | static int pn544_hci_i2c_probe(struct i2c_client *client, |
365 | const struct i2c_device_id *id) | |
97f18414 EL |
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 | ||
a0f36536 SO |
379 | phy = devm_kzalloc(&client->dev, sizeof(struct pn544_i2c_phy), |
380 | GFP_KERNEL); | |
97f18414 EL |
381 | if (!phy) { |
382 | dev_err(&client->dev, | |
383 | "Cannot allocate memory for pn544 i2c phy.\n"); | |
a0f36536 | 384 | return -ENOMEM; |
97f18414 EL |
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"); | |
a0f36536 | 393 | return -EINVAL; |
97f18414 EL |
394 | } |
395 | ||
396 | if (pdata->request_resources == NULL) { | |
397 | dev_err(&client->dev, "request_resources() missing\n"); | |
a0f36536 | 398 | return -EINVAL; |
97f18414 EL |
399 | } |
400 | ||
401 | r = pdata->request_resources(client); | |
402 | if (r) { | |
403 | dev_err(&client->dev, "Cannot get platform resources\n"); | |
a0f36536 | 404 | return r; |
97f18414 EL |
405 | } |
406 | ||
407 | phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE); | |
408 | phy->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET); | |
409 | phy->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ); | |
410 | ||
411 | pn544_hci_i2c_platform_init(phy); | |
412 | ||
413 | r = request_threaded_irq(client->irq, NULL, pn544_hci_i2c_irq_thread_fn, | |
414 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, | |
415 | PN544_HCI_I2C_DRIVER_NAME, phy); | |
416 | if (r < 0) { | |
417 | dev_err(&client->dev, "Unable to register IRQ handler\n"); | |
418 | goto err_rti; | |
419 | } | |
420 | ||
421 | r = pn544_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME, | |
422 | PN544_I2C_FRAME_HEADROOM, PN544_I2C_FRAME_TAILROOM, | |
423 | PN544_HCI_I2C_LLC_MAX_PAYLOAD, &phy->hdev); | |
424 | if (r < 0) | |
425 | goto err_hci; | |
426 | ||
427 | return 0; | |
428 | ||
429 | err_hci: | |
430 | free_irq(client->irq, phy); | |
431 | ||
432 | err_rti: | |
433 | if (pdata->free_resources != NULL) | |
434 | pdata->free_resources(); | |
435 | ||
97f18414 EL |
436 | return r; |
437 | } | |
438 | ||
0fe763c5 | 439 | static int pn544_hci_i2c_remove(struct i2c_client *client) |
97f18414 EL |
440 | { |
441 | struct pn544_i2c_phy *phy = i2c_get_clientdata(client); | |
442 | struct pn544_nfc_platform_data *pdata = client->dev.platform_data; | |
443 | ||
444 | dev_dbg(&client->dev, "%s\n", __func__); | |
445 | ||
446 | pn544_hci_remove(phy->hdev); | |
447 | ||
448 | if (phy->powered) | |
449 | pn544_hci_i2c_disable(phy); | |
450 | ||
451 | free_irq(client->irq, phy); | |
452 | if (pdata->free_resources) | |
453 | pdata->free_resources(); | |
454 | ||
97f18414 EL |
455 | return 0; |
456 | } | |
457 | ||
458 | static struct i2c_driver pn544_hci_i2c_driver = { | |
459 | .driver = { | |
460 | .name = PN544_HCI_I2C_DRIVER_NAME, | |
461 | }, | |
462 | .probe = pn544_hci_i2c_probe, | |
463 | .id_table = pn544_hci_i2c_id_table, | |
0fe763c5 | 464 | .remove = pn544_hci_i2c_remove, |
97f18414 EL |
465 | }; |
466 | ||
234d4d6b | 467 | module_i2c_driver(pn544_hci_i2c_driver); |
97f18414 EL |
468 | |
469 | MODULE_LICENSE("GPL"); | |
470 | MODULE_DESCRIPTION(DRIVER_DESC); |