blob: e09c8982596c3354fcf6e7527b0fae4962d30fe2 [file] [log] [blame]
Eric Lapuyade97f18412012-10-02 18:44:06 +02001/*
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
Marcel Holtmann61cdb012012-10-24 11:45:26 -070029#include <linux/platform_data/pn544.h>
Eric Lapuyade97f18412012-10-02 18:44:06 +020030
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
49static struct i2c_device_id pn544_hci_i2c_id_table[] = {
50 {"pn544", 0},
51 {}
52};
53
54MODULE_DEVICE_TABLE(i2c, pn544_hci_i2c_id_table);
55
56#define PN544_HCI_I2C_DRIVER_NAME "pn544_hci_i2c"
57
58struct 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;
Eric Lapuyadeeab10b72013-07-19 14:57:13 +020068 int run_mode;
Eric Lapuyade97f18412012-10-02 18:44:06 +020069
70 int hard_fault; /*
71 * < 0 if hardware error occured (e.g. i2c err)
72 * and prevents normal operation.
73 */
74};
75
76#define I2C_DUMP_SKB(info, skb) \
77do { \
78 pr_debug("%s:\n", info); \
79 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
80 16, 1, (skb)->data, (skb)->len, 0); \
81} while (0)
82
83static void pn544_hci_i2c_platform_init(struct pn544_i2c_phy *phy)
84{
85 int polarity, retry, ret;
86 char rset_cmd[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 };
87 int count = sizeof(rset_cmd);
88
89 pr_info(DRIVER_DESC ": %s\n", __func__);
90 dev_info(&phy->i2c_dev->dev, "Detecting nfc_en polarity\n");
91
92 /* Disable fw download */
93 gpio_set_value(phy->gpio_fw, 0);
94
95 for (polarity = 0; polarity < 2; polarity++) {
96 phy->en_polarity = polarity;
97 retry = 3;
98 while (retry--) {
99 /* power off */
100 gpio_set_value(phy->gpio_en, !phy->en_polarity);
101 usleep_range(10000, 15000);
102
103 /* power on */
104 gpio_set_value(phy->gpio_en, phy->en_polarity);
105 usleep_range(10000, 15000);
106
107 /* send reset */
108 dev_dbg(&phy->i2c_dev->dev, "Sending reset cmd\n");
109 ret = i2c_master_send(phy->i2c_dev, rset_cmd, count);
110 if (ret == count) {
111 dev_info(&phy->i2c_dev->dev,
112 "nfc_en polarity : active %s\n",
113 (polarity == 0 ? "low" : "high"));
114 goto out;
115 }
116 }
117 }
118
119 dev_err(&phy->i2c_dev->dev,
120 "Could not detect nfc_en polarity, fallback to active high\n");
121
122out:
123 gpio_set_value(phy->gpio_en, !phy->en_polarity);
124}
125
Eric Lapuyadeeab10b72013-07-19 14:57:13 +0200126static void pn544_hci_i2c_enable_mode(struct pn544_i2c_phy *phy, int run_mode)
127{
128 gpio_set_value(phy->gpio_fw, run_mode == PN544_FW_MODE ? 1 : 0);
129 gpio_set_value(phy->gpio_en, phy->en_polarity);
130 usleep_range(10000, 15000);
131
132 phy->run_mode = run_mode;
133}
134
Eric Lapuyade97f18412012-10-02 18:44:06 +0200135static int pn544_hci_i2c_enable(void *phy_id)
136{
137 struct pn544_i2c_phy *phy = phy_id;
138
139 pr_info(DRIVER_DESC ": %s\n", __func__);
140
Eric Lapuyadeeab10b72013-07-19 14:57:13 +0200141 pn544_hci_i2c_enable_mode(phy, PN544_HCI_MODE);
Eric Lapuyade97f18412012-10-02 18:44:06 +0200142
143 phy->powered = 1;
144
145 return 0;
146}
147
148static void pn544_hci_i2c_disable(void *phy_id)
149{
150 struct pn544_i2c_phy *phy = phy_id;
151
152 pr_info(DRIVER_DESC ": %s\n", __func__);
153
154 gpio_set_value(phy->gpio_fw, 0);
155 gpio_set_value(phy->gpio_en, !phy->en_polarity);
156 usleep_range(10000, 15000);
157
158 gpio_set_value(phy->gpio_en, phy->en_polarity);
159 usleep_range(10000, 15000);
160
161 gpio_set_value(phy->gpio_en, !phy->en_polarity);
162 usleep_range(10000, 15000);
163
164 phy->powered = 0;
165}
166
167static void pn544_hci_i2c_add_len_crc(struct sk_buff *skb)
168{
169 u16 crc;
170 int len;
171
172 len = skb->len + 2;
173 *skb_push(skb, 1) = len;
174
175 crc = crc_ccitt(0xffff, skb->data, skb->len);
176 crc = ~crc;
177 *skb_put(skb, 1) = crc & 0xff;
178 *skb_put(skb, 1) = crc >> 8;
179}
180
181static void pn544_hci_i2c_remove_len_crc(struct sk_buff *skb)
182{
183 skb_pull(skb, PN544_I2C_FRAME_HEADROOM);
184 skb_trim(skb, PN544_I2C_FRAME_TAILROOM);
185}
186
187/*
188 * Writing a frame must not return the number of written bytes.
189 * It must return either zero for success, or <0 for error.
190 * In addition, it must not alter the skb
191 */
192static int pn544_hci_i2c_write(void *phy_id, struct sk_buff *skb)
193{
194 int r;
195 struct pn544_i2c_phy *phy = phy_id;
196 struct i2c_client *client = phy->i2c_dev;
197
198 if (phy->hard_fault != 0)
199 return phy->hard_fault;
200
201 usleep_range(3000, 6000);
202
203 pn544_hci_i2c_add_len_crc(skb);
204
205 I2C_DUMP_SKB("i2c frame written", skb);
206
207 r = i2c_master_send(client, skb->data, skb->len);
208
209 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
210 usleep_range(6000, 10000);
211 r = i2c_master_send(client, skb->data, skb->len);
212 }
213
214 if (r >= 0) {
215 if (r != skb->len)
216 r = -EREMOTEIO;
217 else
218 r = 0;
219 }
220
221 pn544_hci_i2c_remove_len_crc(skb);
222
223 return r;
224}
225
226static int check_crc(u8 *buf, int buflen)
227{
228 int len;
229 u16 crc;
230
231 len = buf[0] + 1;
232 crc = crc_ccitt(0xffff, buf, len - 2);
233 crc = ~crc;
234
235 if (buf[len - 2] != (crc & 0xff) || buf[len - 1] != (crc >> 8)) {
236 pr_err(PN544_HCI_I2C_DRIVER_NAME
237 ": CRC error 0x%x != 0x%x 0x%x\n",
238 crc, buf[len - 1], buf[len - 2]);
239
240 pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
241 print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
242 16, 2, buf, buflen, false);
243 return -EPERM;
244 }
245 return 0;
246}
247
248/*
249 * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
250 * that i2c bus will be flushed and that next read will start on a new frame.
251 * returned skb contains only LLC header and payload.
252 * returns:
253 * -EREMOTEIO : i2c read error (fatal)
254 * -EBADMSG : frame was incorrect and discarded
255 * -ENOMEM : cannot allocate skb, frame dropped
256 */
257static int pn544_hci_i2c_read(struct pn544_i2c_phy *phy, struct sk_buff **skb)
258{
259 int r;
260 u8 len;
261 u8 tmp[PN544_HCI_I2C_LLC_MAX_SIZE - 1];
262 struct i2c_client *client = phy->i2c_dev;
263
264 r = i2c_master_recv(client, &len, 1);
265 if (r != 1) {
266 dev_err(&client->dev, "cannot read len byte\n");
267 return -EREMOTEIO;
268 }
269
270 if ((len < (PN544_HCI_I2C_LLC_MIN_SIZE - 1)) ||
271 (len > (PN544_HCI_I2C_LLC_MAX_SIZE - 1))) {
272 dev_err(&client->dev, "invalid len byte\n");
273 r = -EBADMSG;
274 goto flush;
275 }
276
277 *skb = alloc_skb(1 + len, GFP_KERNEL);
278 if (*skb == NULL) {
279 r = -ENOMEM;
280 goto flush;
281 }
282
283 *skb_put(*skb, 1) = len;
284
285 r = i2c_master_recv(client, skb_put(*skb, len), len);
286 if (r != len) {
287 kfree_skb(*skb);
288 return -EREMOTEIO;
289 }
290
291 I2C_DUMP_SKB("i2c frame read", *skb);
292
293 r = check_crc((*skb)->data, (*skb)->len);
294 if (r != 0) {
295 kfree_skb(*skb);
296 r = -EBADMSG;
297 goto flush;
298 }
299
300 skb_pull(*skb, 1);
301 skb_trim(*skb, (*skb)->len - 2);
302
303 usleep_range(3000, 6000);
304
305 return 0;
306
307flush:
308 if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
309 r = -EREMOTEIO;
310
311 usleep_range(3000, 6000);
312
313 return r;
314}
315
316/*
317 * Reads an shdlc frame from the chip. This is not as straightforward as it
318 * seems. There are cases where we could loose the frame start synchronization.
319 * The frame format is len-data-crc, and corruption can occur anywhere while
320 * transiting on i2c bus, such that we could read an invalid len.
321 * In order to recover synchronization with the next frame, we must be sure
322 * to read the real amount of data without using the len byte. We do this by
323 * assuming the following:
324 * - the chip will always present only one single complete frame on the bus
325 * before triggering the interrupt
326 * - the chip will not present a new frame until we have completely read
327 * the previous one (or until we have handled the interrupt).
328 * The tricky case is when we read a corrupted len that is less than the real
329 * len. We must detect this here in order to determine that we need to flush
330 * the bus. This is the reason why we check the crc here.
331 */
332static irqreturn_t pn544_hci_i2c_irq_thread_fn(int irq, void *phy_id)
333{
334 struct pn544_i2c_phy *phy = phy_id;
335 struct i2c_client *client;
336 struct sk_buff *skb = NULL;
337 int r;
338
339 if (!phy || irq != phy->i2c_dev->irq) {
340 WARN_ON_ONCE(1);
341 return IRQ_NONE;
342 }
343
344 client = phy->i2c_dev;
345 dev_dbg(&client->dev, "IRQ\n");
346
347 if (phy->hard_fault != 0)
348 return IRQ_HANDLED;
349
350 r = pn544_hci_i2c_read(phy, &skb);
351 if (r == -EREMOTEIO) {
352 phy->hard_fault = r;
353
354 nfc_hci_recv_frame(phy->hdev, NULL);
355
356 return IRQ_HANDLED;
357 } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
358 return IRQ_HANDLED;
359 }
360
361 nfc_hci_recv_frame(phy->hdev, skb);
362
363 return IRQ_HANDLED;
364}
365
366static struct nfc_phy_ops i2c_phy_ops = {
367 .write = pn544_hci_i2c_write,
368 .enable = pn544_hci_i2c_enable,
369 .disable = pn544_hci_i2c_disable,
370};
371
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800372static int pn544_hci_i2c_probe(struct i2c_client *client,
373 const struct i2c_device_id *id)
Eric Lapuyade97f18412012-10-02 18:44:06 +0200374{
375 struct pn544_i2c_phy *phy;
376 struct pn544_nfc_platform_data *pdata;
377 int r = 0;
378
379 dev_dbg(&client->dev, "%s\n", __func__);
380 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
381
382 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
383 dev_err(&client->dev, "Need I2C_FUNC_I2C\n");
384 return -ENODEV;
385 }
386
Samuel Ortiza0f36532012-12-18 18:07:37 +0100387 phy = devm_kzalloc(&client->dev, sizeof(struct pn544_i2c_phy),
388 GFP_KERNEL);
Eric Lapuyade97f18412012-10-02 18:44:06 +0200389 if (!phy) {
390 dev_err(&client->dev,
391 "Cannot allocate memory for pn544 i2c phy.\n");
Samuel Ortiza0f36532012-12-18 18:07:37 +0100392 return -ENOMEM;
Eric Lapuyade97f18412012-10-02 18:44:06 +0200393 }
394
395 phy->i2c_dev = client;
396 i2c_set_clientdata(client, phy);
397
398 pdata = client->dev.platform_data;
399 if (pdata == NULL) {
400 dev_err(&client->dev, "No platform data\n");
Samuel Ortiza0f36532012-12-18 18:07:37 +0100401 return -EINVAL;
Eric Lapuyade97f18412012-10-02 18:44:06 +0200402 }
403
404 if (pdata->request_resources == NULL) {
405 dev_err(&client->dev, "request_resources() missing\n");
Samuel Ortiza0f36532012-12-18 18:07:37 +0100406 return -EINVAL;
Eric Lapuyade97f18412012-10-02 18:44:06 +0200407 }
408
409 r = pdata->request_resources(client);
410 if (r) {
411 dev_err(&client->dev, "Cannot get platform resources\n");
Samuel Ortiza0f36532012-12-18 18:07:37 +0100412 return r;
Eric Lapuyade97f18412012-10-02 18:44:06 +0200413 }
414
415 phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
416 phy->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET);
417 phy->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ);
418
419 pn544_hci_i2c_platform_init(phy);
420
421 r = request_threaded_irq(client->irq, NULL, pn544_hci_i2c_irq_thread_fn,
422 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
423 PN544_HCI_I2C_DRIVER_NAME, phy);
424 if (r < 0) {
425 dev_err(&client->dev, "Unable to register IRQ handler\n");
426 goto err_rti;
427 }
428
429 r = pn544_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
430 PN544_I2C_FRAME_HEADROOM, PN544_I2C_FRAME_TAILROOM,
431 PN544_HCI_I2C_LLC_MAX_PAYLOAD, &phy->hdev);
432 if (r < 0)
433 goto err_hci;
434
435 return 0;
436
437err_hci:
438 free_irq(client->irq, phy);
439
440err_rti:
441 if (pdata->free_resources != NULL)
442 pdata->free_resources();
443
Eric Lapuyade97f18412012-10-02 18:44:06 +0200444 return r;
445}
446
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800447static int pn544_hci_i2c_remove(struct i2c_client *client)
Eric Lapuyade97f18412012-10-02 18:44:06 +0200448{
449 struct pn544_i2c_phy *phy = i2c_get_clientdata(client);
450 struct pn544_nfc_platform_data *pdata = client->dev.platform_data;
451
452 dev_dbg(&client->dev, "%s\n", __func__);
453
454 pn544_hci_remove(phy->hdev);
455
456 if (phy->powered)
457 pn544_hci_i2c_disable(phy);
458
459 free_irq(client->irq, phy);
460 if (pdata->free_resources)
461 pdata->free_resources();
462
Eric Lapuyade97f18412012-10-02 18:44:06 +0200463 return 0;
464}
465
466static 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,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800472 .remove = pn544_hci_i2c_remove,
Eric Lapuyade97f18412012-10-02 18:44:06 +0200473};
474
Samuel Ortiz234d4d62012-12-18 16:40:16 +0100475module_i2c_driver(pn544_hci_i2c_driver);
Eric Lapuyade97f18412012-10-02 18:44:06 +0200476
477MODULE_LICENSE("GPL");
478MODULE_DESCRIPTION(DRIVER_DESC);