2 * Driver for the PN544 NFC chip.
4 * Copyright (C) Nokia Corporation
6 * Author: Jari Vanhala <ext-jari.vanhala@nokia.com>
7 * Contact: Matti Aaltonen <matti.j.aaltonen@nokia.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/completion.h>
24 #include <linux/crc-ccitt.h>
25 #include <linux/delay.h>
26 #include <linux/interrupt.h>
27 #include <linux/kernel.h>
28 #include <linux/miscdevice.h>
29 #include <linux/module.h>
30 #include <linux/mutex.h>
31 #include <linux/nfc/pn544.h>
32 #include <linux/poll.h>
33 #include <linux/regulator/consumer.h>
34 #include <linux/serial_core.h> /* for TCGETS */
35 #include <linux/slab.h>
37 #define DRIVER_CARD "PN544 NFC"
38 #define DRIVER_DESC "NFC driver for PN544"
40 static struct i2c_device_id pn544_id_table
[] = {
41 { PN544_DRIVER_NAME
, 0 },
44 MODULE_DEVICE_TABLE(i2c
, pn544_id_table
);
61 struct miscdevice miscdev
;
62 struct i2c_client
*i2c_dev
;
63 struct regulator_bulk_data regs
[3];
65 enum pn544_state state
;
66 wait_queue_head_t read_wait
;
68 enum pn544_irq read_irq
;
69 struct mutex read_mutex
; /* Serialize read_irq access */
70 struct mutex mutex
; /* Serialize info struct access */
75 static const char reg_vdd_io
[] = "Vdd_IO";
76 static const char reg_vbat
[] = "VBat";
77 static const char reg_vsim
[] = "VSim";
80 static ssize_t
pn544_test(struct device
*dev
,
81 struct device_attribute
*attr
, char *buf
)
83 struct pn544_info
*info
= dev_get_drvdata(dev
);
84 struct i2c_client
*client
= info
->i2c_dev
;
85 struct pn544_nfc_platform_data
*pdata
= client
->dev
.platform_data
;
87 return snprintf(buf
, PAGE_SIZE
, "%d\n", pdata
->test());
90 static int pn544_enable(struct pn544_info
*info
, int mode
)
92 struct pn544_nfc_platform_data
*pdata
;
93 struct i2c_client
*client
= info
->i2c_dev
;
97 r
= regulator_bulk_enable(ARRAY_SIZE(info
->regs
), info
->regs
);
101 pdata
= client
->dev
.platform_data
;
102 info
->read_irq
= PN544_NONE
;
107 info
->state
= PN544_ST_FW_READY
;
108 dev_dbg(&client
->dev
, "now in FW-mode\n");
110 info
->state
= PN544_ST_READY
;
111 dev_dbg(&client
->dev
, "now in HCI-mode\n");
114 usleep_range(10000, 15000);
119 static void pn544_disable(struct pn544_info
*info
)
121 struct pn544_nfc_platform_data
*pdata
;
122 struct i2c_client
*client
= info
->i2c_dev
;
124 pdata
= client
->dev
.platform_data
;
128 info
->state
= PN544_ST_COLD
;
130 dev_dbg(&client
->dev
, "Now in OFF-mode\n");
132 msleep(PN544_RESETVEN_TIME
);
134 info
->read_irq
= PN544_NONE
;
135 regulator_bulk_disable(ARRAY_SIZE(info
->regs
), info
->regs
);
138 static int check_crc(u8
*buf
, int buflen
)
144 if (len
< 4 || len
!= buflen
|| len
> PN544_MSG_MAX_SIZE
) {
145 pr_err(PN544_DRIVER_NAME
146 ": CRC; corrupt packet len %u (%d)\n", len
, buflen
);
147 print_hex_dump(KERN_DEBUG
, "crc: ", DUMP_PREFIX_NONE
,
148 16, 2, buf
, buflen
, false);
151 crc
= crc_ccitt(0xffff, buf
, len
- 2);
154 if (buf
[len
-2] != (crc
& 0xff) || buf
[len
-1] != (crc
>> 8)) {
155 pr_err(PN544_DRIVER_NAME
": CRC error 0x%x != 0x%x 0x%x\n",
156 crc
, buf
[len
-1], buf
[len
-2]);
158 print_hex_dump(KERN_DEBUG
, "crc: ", DUMP_PREFIX_NONE
,
159 16, 2, buf
, buflen
, false);
165 static int pn544_i2c_write(struct i2c_client
*client
, u8
*buf
, int len
)
169 if (len
< 4 || len
!= (buf
[0] + 1)) {
170 dev_err(&client
->dev
, "%s: Illegal message length: %d\n",
175 if (check_crc(buf
, len
))
178 usleep_range(3000, 6000);
180 r
= i2c_master_send(client
, buf
, len
);
181 dev_dbg(&client
->dev
, "send: %d\n", r
);
183 if (r
== -EREMOTEIO
) { /* Retry, chip was in standby */
184 usleep_range(6000, 10000);
185 r
= i2c_master_send(client
, buf
, len
);
186 dev_dbg(&client
->dev
, "send2: %d\n", r
);
195 static int pn544_i2c_read(struct i2c_client
*client
, u8
*buf
, int buflen
)
201 * You could read a packet in one go, but then you'd need to read
202 * max size and rest would be 0xff fill, so we do split reads.
204 r
= i2c_master_recv(client
, &len
, 1);
205 dev_dbg(&client
->dev
, "recv1: %d\n", r
);
210 if (len
< PN544_LLC_HCI_OVERHEAD
)
211 len
= PN544_LLC_HCI_OVERHEAD
;
212 else if (len
> (PN544_MSG_MAX_SIZE
- 1))
213 len
= PN544_MSG_MAX_SIZE
- 1;
215 if (1 + len
> buflen
) /* len+(data+crc16) */
220 r
= i2c_master_recv(client
, buf
+ 1, len
);
221 dev_dbg(&client
->dev
, "recv2: %d\n", r
);
226 usleep_range(3000, 6000);
231 static int pn544_fw_write(struct i2c_client
*client
, u8
*buf
, int len
)
235 dev_dbg(&client
->dev
, "%s\n", __func__
);
237 if (len
< PN544_FW_HEADER_SIZE
||
238 (PN544_FW_HEADER_SIZE
+ (buf
[1] << 8) + buf
[2]) != len
)
241 r
= i2c_master_send(client
, buf
, len
);
242 dev_dbg(&client
->dev
, "fw send: %d\n", r
);
244 if (r
== -EREMOTEIO
) { /* Retry, chip was in standby */
245 usleep_range(6000, 10000);
246 r
= i2c_master_send(client
, buf
, len
);
247 dev_dbg(&client
->dev
, "fw send2: %d\n", r
);
256 static int pn544_fw_read(struct i2c_client
*client
, u8
*buf
, int buflen
)
260 if (buflen
< PN544_FW_HEADER_SIZE
)
263 r
= i2c_master_recv(client
, buf
, PN544_FW_HEADER_SIZE
);
264 dev_dbg(&client
->dev
, "FW recv1: %d\n", r
);
269 if (r
< PN544_FW_HEADER_SIZE
)
272 len
= (buf
[1] << 8) + buf
[2];
273 if (len
== 0) /* just header, no additional data */
276 if (len
> buflen
- PN544_FW_HEADER_SIZE
)
279 r
= i2c_master_recv(client
, buf
+ PN544_FW_HEADER_SIZE
, len
);
280 dev_dbg(&client
->dev
, "fw recv2: %d\n", r
);
285 return r
+ PN544_FW_HEADER_SIZE
;
288 static irqreturn_t
pn544_irq_thread_fn(int irq
, void *dev_id
)
290 struct pn544_info
*info
= dev_id
;
291 struct i2c_client
*client
= info
->i2c_dev
;
294 BUG_ON(irq
!= info
->i2c_dev
->irq
);
296 dev_dbg(&client
->dev
, "IRQ\n");
298 mutex_lock(&info
->read_mutex
);
299 info
->read_irq
= PN544_INT
;
300 mutex_unlock(&info
->read_mutex
);
302 wake_up_interruptible(&info
->read_wait
);
307 static enum pn544_irq
pn544_irq_state(struct pn544_info
*info
)
311 mutex_lock(&info
->read_mutex
);
312 irq
= info
->read_irq
;
313 mutex_unlock(&info
->read_mutex
);
315 * XXX: should we check GPIO-line status directly?
316 * return pdata->irq_status() ? PN544_INT : PN544_NONE;
322 static ssize_t
pn544_read(struct file
*file
, char __user
*buf
,
323 size_t count
, loff_t
*offset
)
325 struct pn544_info
*info
= container_of(file
->private_data
,
326 struct pn544_info
, miscdev
);
327 struct i2c_client
*client
= info
->i2c_dev
;
332 dev_dbg(&client
->dev
, "%s: info: %p, count: %zu\n", __func__
,
335 mutex_lock(&info
->mutex
);
337 if (info
->state
== PN544_ST_COLD
) {
342 irq
= pn544_irq_state(info
);
343 if (irq
== PN544_NONE
) {
344 if (file
->f_flags
& O_NONBLOCK
) {
349 if (wait_event_interruptible(info
->read_wait
,
350 (info
->read_irq
== PN544_INT
))) {
356 if (info
->state
== PN544_ST_FW_READY
) {
357 len
= min(count
, info
->buflen
);
359 mutex_lock(&info
->read_mutex
);
360 r
= pn544_fw_read(info
->i2c_dev
, info
->buf
, len
);
361 info
->read_irq
= PN544_NONE
;
362 mutex_unlock(&info
->read_mutex
);
365 dev_err(&info
->i2c_dev
->dev
, "FW read failed: %d\n", r
);
369 print_hex_dump(KERN_DEBUG
, "FW read: ", DUMP_PREFIX_NONE
,
370 16, 2, info
->buf
, r
, false);
373 if (copy_to_user(buf
, info
->buf
, r
)) {
378 len
= min(count
, info
->buflen
);
380 mutex_lock(&info
->read_mutex
);
381 r
= pn544_i2c_read(info
->i2c_dev
, info
->buf
, len
);
382 info
->read_irq
= PN544_NONE
;
383 mutex_unlock(&info
->read_mutex
);
386 dev_err(&info
->i2c_dev
->dev
, "read failed (%d)\n", r
);
389 print_hex_dump(KERN_DEBUG
, "read: ", DUMP_PREFIX_NONE
,
390 16, 2, info
->buf
, r
, false);
393 if (copy_to_user(buf
, info
->buf
, r
)) {
400 mutex_unlock(&info
->mutex
);
405 static unsigned int pn544_poll(struct file
*file
, poll_table
*wait
)
407 struct pn544_info
*info
= container_of(file
->private_data
,
408 struct pn544_info
, miscdev
);
409 struct i2c_client
*client
= info
->i2c_dev
;
412 dev_dbg(&client
->dev
, "%s: info: %p\n", __func__
, info
);
414 mutex_lock(&info
->mutex
);
416 if (info
->state
== PN544_ST_COLD
) {
421 poll_wait(file
, &info
->read_wait
, wait
);
423 if (pn544_irq_state(info
) == PN544_INT
) {
424 r
= POLLIN
| POLLRDNORM
;
428 mutex_unlock(&info
->mutex
);
433 static ssize_t
pn544_write(struct file
*file
, const char __user
*buf
,
434 size_t count
, loff_t
*ppos
)
436 struct pn544_info
*info
= container_of(file
->private_data
,
437 struct pn544_info
, miscdev
);
438 struct i2c_client
*client
= info
->i2c_dev
;
442 dev_dbg(&client
->dev
, "%s: info: %p, count %zu\n", __func__
,
445 mutex_lock(&info
->mutex
);
447 if (info
->state
== PN544_ST_COLD
) {
453 * XXX: should we detect rset-writes and clean possible
456 if (info
->state
== PN544_ST_FW_READY
) {
459 if (count
< PN544_FW_HEADER_SIZE
) {
464 len
= min(count
, info
->buflen
);
465 if (copy_from_user(info
->buf
, buf
, len
)) {
470 print_hex_dump(KERN_DEBUG
, "FW write: ", DUMP_PREFIX_NONE
,
471 16, 2, info
->buf
, len
, false);
473 fw_len
= PN544_FW_HEADER_SIZE
+ (info
->buf
[1] << 8) +
476 if (len
> fw_len
) /* 1 msg at a time */
479 r
= pn544_fw_write(info
->i2c_dev
, info
->buf
, len
);
481 if (count
< PN544_LLC_MIN_SIZE
) {
486 len
= min(count
, info
->buflen
);
487 if (copy_from_user(info
->buf
, buf
, len
)) {
492 print_hex_dump(KERN_DEBUG
, "write: ", DUMP_PREFIX_NONE
,
493 16, 2, info
->buf
, len
, false);
495 if (len
> (info
->buf
[0] + 1)) /* 1 msg at a time */
496 len
= info
->buf
[0] + 1;
498 r
= pn544_i2c_write(info
->i2c_dev
, info
->buf
, len
);
501 mutex_unlock(&info
->mutex
);
507 static long pn544_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
509 struct pn544_info
*info
= container_of(file
->private_data
,
510 struct pn544_info
, miscdev
);
511 struct i2c_client
*client
= info
->i2c_dev
;
512 struct pn544_nfc_platform_data
*pdata
;
516 dev_dbg(&client
->dev
, "%s: info: %p, cmd: 0x%x\n", __func__
, info
, cmd
);
518 mutex_lock(&info
->mutex
);
520 if (info
->state
== PN544_ST_COLD
) {
525 pdata
= info
->i2c_dev
->dev
.platform_data
;
527 case PN544_GET_FW_MODE
:
528 dev_dbg(&client
->dev
, "%s: PN544_GET_FW_MODE\n", __func__
);
530 val
= (info
->state
== PN544_ST_FW_READY
);
531 if (copy_to_user((void __user
*)arg
, &val
, sizeof(val
))) {
538 case PN544_SET_FW_MODE
:
539 dev_dbg(&client
->dev
, "%s: PN544_SET_FW_MODE\n", __func__
);
541 if (copy_from_user(&val
, (void __user
*)arg
, sizeof(val
))) {
547 if (info
->state
== PN544_ST_FW_READY
)
551 r
= pn544_enable(info
, FW_MODE
);
555 if (info
->state
== PN544_ST_READY
)
558 r
= pn544_enable(info
, HCI_MODE
);
562 file
->f_pos
= info
->read_offset
;
566 dev_dbg(&client
->dev
, "%s: TCGETS\n", __func__
);
572 dev_err(&client
->dev
, "Unknown ioctl 0x%x\n", cmd
);
578 mutex_unlock(&info
->mutex
);
583 static int pn544_open(struct inode
*inode
, struct file
*file
)
585 struct pn544_info
*info
= container_of(file
->private_data
,
586 struct pn544_info
, miscdev
);
587 struct i2c_client
*client
= info
->i2c_dev
;
590 dev_dbg(&client
->dev
, "%s: info: %p, client %p\n", __func__
,
591 info
, info
->i2c_dev
);
593 mutex_lock(&info
->mutex
);
597 * XXX: maybe user (counter) would work better
599 if (info
->state
!= PN544_ST_COLD
) {
604 file
->f_pos
= info
->read_offset
;
605 r
= pn544_enable(info
, HCI_MODE
);
608 mutex_unlock(&info
->mutex
);
612 static int pn544_close(struct inode
*inode
, struct file
*file
)
614 struct pn544_info
*info
= container_of(file
->private_data
,
615 struct pn544_info
, miscdev
);
616 struct i2c_client
*client
= info
->i2c_dev
;
618 dev_dbg(&client
->dev
, "%s: info: %p, client %p\n",
619 __func__
, info
, info
->i2c_dev
);
621 mutex_lock(&info
->mutex
);
623 mutex_unlock(&info
->mutex
);
628 static const struct file_operations pn544_fops
= {
629 .owner
= THIS_MODULE
,
632 .write
= pn544_write
,
635 .release
= pn544_close
,
636 .unlocked_ioctl
= pn544_ioctl
,
640 static int pn544_suspend(struct device
*dev
)
642 struct i2c_client
*client
= to_i2c_client(dev
);
643 struct pn544_info
*info
;
646 dev_info(&client
->dev
, "***\n%s: client %p\n***\n", __func__
, client
);
648 info
= i2c_get_clientdata(client
);
649 dev_info(&client
->dev
, "%s: info: %p, client %p\n", __func__
,
652 mutex_lock(&info
->mutex
);
654 switch (info
->state
) {
655 case PN544_ST_FW_READY
:
656 /* Do not suspend while upgrading FW, please! */
662 * CHECK: Device should be in standby-mode. No way to check?
663 * Allowing low power mode for the regulator is potentially
664 * dangerous if pn544 does not go to suspension.
672 mutex_unlock(&info
->mutex
);
676 static int pn544_resume(struct device
*dev
)
678 struct i2c_client
*client
= to_i2c_client(dev
);
679 struct pn544_info
*info
= i2c_get_clientdata(client
);
682 dev_dbg(&client
->dev
, "%s: info: %p, client %p\n", __func__
,
685 mutex_lock(&info
->mutex
);
687 switch (info
->state
) {
690 * CHECK: If regulator low power mode is allowed in
691 * pn544_suspend, we should go back to normal mode
699 case PN544_ST_FW_READY
:
703 mutex_unlock(&info
->mutex
);
708 static SIMPLE_DEV_PM_OPS(pn544_pm_ops
, pn544_suspend
, pn544_resume
);
711 static struct device_attribute pn544_attr
=
712 __ATTR(nfc_test
, S_IRUGO
, pn544_test
, NULL
);
714 static int __devinit
pn544_probe(struct i2c_client
*client
,
715 const struct i2c_device_id
*id
)
717 struct pn544_info
*info
;
718 struct pn544_nfc_platform_data
*pdata
;
721 dev_dbg(&client
->dev
, "%s\n", __func__
);
722 dev_dbg(&client
->dev
, "IRQ: %d\n", client
->irq
);
724 /* private data allocation */
725 info
= kzalloc(sizeof(struct pn544_info
), GFP_KERNEL
);
727 dev_err(&client
->dev
,
728 "Cannot allocate memory for pn544_info.\n");
733 info
->buflen
= max(PN544_MSG_MAX_SIZE
, PN544_MAX_I2C_TRANSFER
);
734 info
->buf
= kzalloc(info
->buflen
, GFP_KERNEL
);
736 dev_err(&client
->dev
,
737 "Cannot allocate memory for pn544_info->buf.\n");
742 info
->regs
[0].supply
= reg_vdd_io
;
743 info
->regs
[1].supply
= reg_vbat
;
744 info
->regs
[2].supply
= reg_vsim
;
745 r
= regulator_bulk_get(&client
->dev
, ARRAY_SIZE(info
->regs
),
750 info
->i2c_dev
= client
;
751 info
->state
= PN544_ST_COLD
;
752 info
->read_irq
= PN544_NONE
;
753 mutex_init(&info
->read_mutex
);
754 mutex_init(&info
->mutex
);
755 init_waitqueue_head(&info
->read_wait
);
756 i2c_set_clientdata(client
, info
);
757 pdata
= client
->dev
.platform_data
;
759 dev_err(&client
->dev
, "No platform data\n");
764 if (!pdata
->request_resources
) {
765 dev_err(&client
->dev
, "request_resources() missing\n");
770 r
= pdata
->request_resources(client
);
772 dev_err(&client
->dev
, "Cannot get platform resources\n");
776 r
= request_threaded_irq(client
->irq
, NULL
, pn544_irq_thread_fn
,
777 IRQF_TRIGGER_RISING
, PN544_DRIVER_NAME
,
780 dev_err(&client
->dev
, "Unable to register IRQ handler\n");
784 /* If we don't have the test we don't need the sysfs file */
786 r
= device_create_file(&client
->dev
, &pn544_attr
);
788 dev_err(&client
->dev
,
789 "sysfs registration failed, error %d\n", r
);
794 info
->miscdev
.minor
= MISC_DYNAMIC_MINOR
;
795 info
->miscdev
.name
= PN544_DRIVER_NAME
;
796 info
->miscdev
.fops
= &pn544_fops
;
797 info
->miscdev
.parent
= &client
->dev
;
798 r
= misc_register(&info
->miscdev
);
800 dev_err(&client
->dev
, "Device registration failed\n");
804 dev_dbg(&client
->dev
, "%s: info: %p, pdata %p, client %p\n",
805 __func__
, info
, pdata
, client
);
811 device_remove_file(&client
->dev
, &pn544_attr
);
813 free_irq(client
->irq
, info
);
815 if (pdata
->free_resources
)
816 pdata
->free_resources();
818 regulator_bulk_free(ARRAY_SIZE(info
->regs
), info
->regs
);
827 static __devexit
int pn544_remove(struct i2c_client
*client
)
829 struct pn544_info
*info
= i2c_get_clientdata(client
);
830 struct pn544_nfc_platform_data
*pdata
= client
->dev
.platform_data
;
832 dev_dbg(&client
->dev
, "%s\n", __func__
);
834 misc_deregister(&info
->miscdev
);
836 device_remove_file(&client
->dev
, &pn544_attr
);
838 if (info
->state
!= PN544_ST_COLD
) {
842 info
->read_irq
= PN544_NONE
;
845 free_irq(client
->irq
, info
);
846 if (pdata
->free_resources
)
847 pdata
->free_resources();
849 regulator_bulk_free(ARRAY_SIZE(info
->regs
), info
->regs
);
856 static struct i2c_driver pn544_driver
= {
858 .name
= PN544_DRIVER_NAME
,
863 .probe
= pn544_probe
,
864 .id_table
= pn544_id_table
,
865 .remove
= __devexit_p(pn544_remove
),
868 static int __init
pn544_init(void)
872 pr_debug(DRIVER_DESC
": %s\n", __func__
);
874 r
= i2c_add_driver(&pn544_driver
);
876 pr_err(PN544_DRIVER_NAME
": driver registration failed\n");
883 static void __exit
pn544_exit(void)
885 i2c_del_driver(&pn544_driver
);
886 pr_info(DRIVER_DESC
", Exiting.\n");
889 module_init(pn544_init
);
890 module_exit(pn544_exit
);
892 MODULE_LICENSE("GPL");
893 MODULE_DESCRIPTION(DRIVER_DESC
);