2 * I2C Link Layer for PN544 HCI based Driver
4 * Copyright (C) 2012 Intel Corporation. All rights reserved.
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.
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.
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.
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>
29 #include <linux/platform_data/pn544.h>
31 #include <net/nfc/hci.h>
32 #include <net/nfc/llc.h>
36 #define PN544_I2C_FRAME_HEADROOM 1
37 #define PN544_I2C_FRAME_TAILROOM 2
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)
49 static struct i2c_device_id pn544_hci_i2c_id_table
[] = {
54 MODULE_DEVICE_TABLE(i2c
, pn544_hci_i2c_id_table
);
56 #define PN544_HCI_I2C_DRIVER_NAME "pn544_hci_i2c"
58 struct pn544_i2c_phy
{
59 struct i2c_client
*i2c_dev
;
60 struct nfc_hci_dev
*hdev
;
63 unsigned int gpio_irq
;
65 unsigned int en_polarity
;
70 * < 0 if hardware error occured (e.g. i2c err)
71 * and prevents normal operation.
75 #define I2C_DUMP_SKB(info, skb) \
77 pr_debug("%s:\n", info); \
78 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
79 16, 1, (skb)->data, (skb)->len, 0); \
82 static void pn544_hci_i2c_platform_init(struct pn544_i2c_phy
*phy
)
84 int polarity
, retry
, ret
;
85 char rset_cmd
[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 };
86 int count
= sizeof(rset_cmd
);
88 pr_info(DRIVER_DESC
": %s\n", __func__
);
89 dev_info(&phy
->i2c_dev
->dev
, "Detecting nfc_en polarity\n");
91 /* Disable fw download */
92 gpio_set_value(phy
->gpio_fw
, 0);
94 for (polarity
= 0; polarity
< 2; polarity
++) {
95 phy
->en_polarity
= polarity
;
99 gpio_set_value(phy
->gpio_en
, !phy
->en_polarity
);
100 usleep_range(10000, 15000);
103 gpio_set_value(phy
->gpio_en
, phy
->en_polarity
);
104 usleep_range(10000, 15000);
107 dev_dbg(&phy
->i2c_dev
->dev
, "Sending reset cmd\n");
108 ret
= i2c_master_send(phy
->i2c_dev
, rset_cmd
, count
);
110 dev_info(&phy
->i2c_dev
->dev
,
111 "nfc_en polarity : active %s\n",
112 (polarity
== 0 ? "low" : "high"));
118 dev_err(&phy
->i2c_dev
->dev
,
119 "Could not detect nfc_en polarity, fallback to active high\n");
122 gpio_set_value(phy
->gpio_en
, !phy
->en_polarity
);
125 static int pn544_hci_i2c_enable(void *phy_id
)
127 struct pn544_i2c_phy
*phy
= phy_id
;
129 pr_info(DRIVER_DESC
": %s\n", __func__
);
131 gpio_set_value(phy
->gpio_fw
, 0);
132 gpio_set_value(phy
->gpio_en
, phy
->en_polarity
);
133 usleep_range(10000, 15000);
140 static void pn544_hci_i2c_disable(void *phy_id
)
142 struct pn544_i2c_phy
*phy
= phy_id
;
144 pr_info(DRIVER_DESC
": %s\n", __func__
);
146 gpio_set_value(phy
->gpio_fw
, 0);
147 gpio_set_value(phy
->gpio_en
, !phy
->en_polarity
);
148 usleep_range(10000, 15000);
150 gpio_set_value(phy
->gpio_en
, phy
->en_polarity
);
151 usleep_range(10000, 15000);
153 gpio_set_value(phy
->gpio_en
, !phy
->en_polarity
);
154 usleep_range(10000, 15000);
159 static void pn544_hci_i2c_add_len_crc(struct sk_buff
*skb
)
165 *skb_push(skb
, 1) = len
;
167 crc
= crc_ccitt(0xffff, skb
->data
, skb
->len
);
169 *skb_put(skb
, 1) = crc
& 0xff;
170 *skb_put(skb
, 1) = crc
>> 8;
173 static void pn544_hci_i2c_remove_len_crc(struct sk_buff
*skb
)
175 skb_pull(skb
, PN544_I2C_FRAME_HEADROOM
);
176 skb_trim(skb
, PN544_I2C_FRAME_TAILROOM
);
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
184 static int pn544_hci_i2c_write(void *phy_id
, struct sk_buff
*skb
)
187 struct pn544_i2c_phy
*phy
= phy_id
;
188 struct i2c_client
*client
= phy
->i2c_dev
;
190 if (phy
->hard_fault
!= 0)
191 return phy
->hard_fault
;
193 usleep_range(3000, 6000);
195 pn544_hci_i2c_add_len_crc(skb
);
197 I2C_DUMP_SKB("i2c frame written", skb
);
199 r
= i2c_master_send(client
, skb
->data
, skb
->len
);
201 if (r
== -EREMOTEIO
) { /* Retry, chip was in standby */
202 usleep_range(6000, 10000);
203 r
= i2c_master_send(client
, skb
->data
, skb
->len
);
213 pn544_hci_i2c_remove_len_crc(skb
);
218 static int check_crc(u8
*buf
, int buflen
)
224 crc
= crc_ccitt(0xffff, buf
, len
- 2);
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]);
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);
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.
245 * -EREMOTEIO : i2c read error (fatal)
246 * -EBADMSG : frame was incorrect and discarded
247 * -ENOMEM : cannot allocate skb, frame dropped
249 static int pn544_hci_i2c_read(struct pn544_i2c_phy
*phy
, struct sk_buff
**skb
)
253 u8 tmp
[PN544_HCI_I2C_LLC_MAX_SIZE
- 1];
254 struct i2c_client
*client
= phy
->i2c_dev
;
256 r
= i2c_master_recv(client
, &len
, 1);
258 dev_err(&client
->dev
, "cannot read len byte\n");
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");
269 *skb
= alloc_skb(1 + len
, GFP_KERNEL
);
275 *skb_put(*skb
, 1) = len
;
277 r
= i2c_master_recv(client
, skb_put(*skb
, len
), len
);
283 I2C_DUMP_SKB("i2c frame read", *skb
);
285 r
= check_crc((*skb
)->data
, (*skb
)->len
);
293 skb_trim(*skb
, (*skb
)->len
- 2);
295 usleep_range(3000, 6000);
300 if (i2c_master_recv(client
, tmp
, sizeof(tmp
)) < 0)
303 usleep_range(3000, 6000);
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.
324 static irqreturn_t
pn544_hci_i2c_irq_thread_fn(int irq
, void *phy_id
)
326 struct pn544_i2c_phy
*phy
= phy_id
;
327 struct i2c_client
*client
;
328 struct sk_buff
*skb
= NULL
;
331 if (!phy
|| irq
!= phy
->i2c_dev
->irq
) {
336 client
= phy
->i2c_dev
;
337 dev_dbg(&client
->dev
, "IRQ\n");
339 if (phy
->hard_fault
!= 0)
342 r
= pn544_hci_i2c_read(phy
, &skb
);
343 if (r
== -EREMOTEIO
) {
346 nfc_hci_recv_frame(phy
->hdev
, NULL
);
349 } else if ((r
== -ENOMEM
) || (r
== -EBADMSG
)) {
353 nfc_hci_recv_frame(phy
->hdev
, skb
);
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
,
364 static int __devinit
pn544_hci_i2c_probe(struct i2c_client
*client
,
365 const struct i2c_device_id
*id
)
367 struct pn544_i2c_phy
*phy
;
368 struct pn544_nfc_platform_data
*pdata
;
371 dev_dbg(&client
->dev
, "%s\n", __func__
);
372 dev_dbg(&client
->dev
, "IRQ: %d\n", client
->irq
);
374 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
375 dev_err(&client
->dev
, "Need I2C_FUNC_I2C\n");
379 phy
= kzalloc(sizeof(struct pn544_i2c_phy
), GFP_KERNEL
);
381 dev_err(&client
->dev
,
382 "Cannot allocate memory for pn544 i2c phy.\n");
387 phy
->i2c_dev
= client
;
388 i2c_set_clientdata(client
, phy
);
390 pdata
= client
->dev
.platform_data
;
392 dev_err(&client
->dev
, "No platform data\n");
397 if (pdata
->request_resources
== NULL
) {
398 dev_err(&client
->dev
, "request_resources() missing\n");
403 r
= pdata
->request_resources(client
);
405 dev_err(&client
->dev
, "Cannot get platform resources\n");
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
);
413 pn544_hci_i2c_platform_init(phy
);
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
);
419 dev_err(&client
->dev
, "Unable to register IRQ handler\n");
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
);
432 free_irq(client
->irq
, phy
);
435 if (pdata
->free_resources
!= NULL
)
436 pdata
->free_resources();
445 static __devexit
int pn544_hci_i2c_remove(struct i2c_client
*client
)
447 struct pn544_i2c_phy
*phy
= i2c_get_clientdata(client
);
448 struct pn544_nfc_platform_data
*pdata
= client
->dev
.platform_data
;
450 dev_dbg(&client
->dev
, "%s\n", __func__
);
452 pn544_hci_remove(phy
->hdev
);
455 pn544_hci_i2c_disable(phy
);
457 free_irq(client
->irq
, phy
);
458 if (pdata
->free_resources
)
459 pdata
->free_resources();
466 static struct i2c_driver pn544_hci_i2c_driver
= {
468 .name
= PN544_HCI_I2C_DRIVER_NAME
,
470 .probe
= pn544_hci_i2c_probe
,
471 .id_table
= pn544_hci_i2c_id_table
,
472 .remove
= __devexit_p(pn544_hci_i2c_remove
),
475 static int __init
pn544_hci_i2c_init(void)
479 pr_debug(DRIVER_DESC
": %s\n", __func__
);
481 r
= i2c_add_driver(&pn544_hci_i2c_driver
);
483 pr_err(PN544_HCI_I2C_DRIVER_NAME
484 ": driver registration failed\n");
491 static void __exit
pn544_hci_i2c_exit(void)
493 i2c_del_driver(&pn544_hci_i2c_driver
);
496 module_init(pn544_hci_i2c_init
);
497 module_exit(pn544_hci_i2c_exit
);
499 MODULE_LICENSE("GPL");
500 MODULE_DESCRIPTION(DRIVER_DESC
);