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
[2];
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";
79 static ssize_t
pn544_test(struct device
*dev
,
80 struct device_attribute
*attr
, char *buf
)
82 struct pn544_info
*info
= dev_get_drvdata(dev
);
83 struct i2c_client
*client
= info
->i2c_dev
;
84 struct pn544_nfc_platform_data
*pdata
= client
->dev
.platform_data
;
86 return snprintf(buf
, PAGE_SIZE
, "%d\n", pdata
->test());
89 static int pn544_enable(struct pn544_info
*info
, int mode
)
91 struct pn544_nfc_platform_data
*pdata
;
92 struct i2c_client
*client
= info
->i2c_dev
;
96 r
= regulator_bulk_enable(ARRAY_SIZE(info
->regs
), info
->regs
);
100 pdata
= client
->dev
.platform_data
;
101 info
->read_irq
= PN544_NONE
;
106 info
->state
= PN544_ST_FW_READY
;
107 dev_dbg(&client
->dev
, "now in FW-mode\n");
109 info
->state
= PN544_ST_READY
;
110 dev_dbg(&client
->dev
, "now in HCI-mode\n");
113 usleep_range(10000, 15000);
118 static void pn544_disable(struct pn544_info
*info
)
120 struct pn544_nfc_platform_data
*pdata
;
121 struct i2c_client
*client
= info
->i2c_dev
;
123 pdata
= client
->dev
.platform_data
;
127 info
->state
= PN544_ST_COLD
;
129 dev_dbg(&client
->dev
, "Now in OFF-mode\n");
131 msleep(PN544_RESETVEN_TIME
);
133 info
->read_irq
= PN544_NONE
;
134 regulator_bulk_disable(ARRAY_SIZE(info
->regs
), info
->regs
);
137 static int check_crc(u8
*buf
, int buflen
)
143 if (len
< 4 || len
!= buflen
|| len
> PN544_MSG_MAX_SIZE
) {
144 pr_err(PN544_DRIVER_NAME
145 ": CRC; corrupt packet len %u (%d)\n", len
, buflen
);
146 print_hex_dump(KERN_DEBUG
, "crc: ", DUMP_PREFIX_NONE
,
147 16, 2, buf
, buflen
, false);
150 crc
= crc_ccitt(0xffff, buf
, len
- 2);
153 if (buf
[len
-2] != (crc
& 0xff) || buf
[len
-1] != (crc
>> 8)) {
154 pr_err(PN544_DRIVER_NAME
": CRC error 0x%x != 0x%x 0x%x\n",
155 crc
, buf
[len
-1], buf
[len
-2]);
157 print_hex_dump(KERN_DEBUG
, "crc: ", DUMP_PREFIX_NONE
,
158 16, 2, buf
, buflen
, false);
164 static int pn544_i2c_write(struct i2c_client
*client
, u8
*buf
, int len
)
168 if (len
< 4 || len
!= (buf
[0] + 1)) {
169 dev_err(&client
->dev
, "%s: Illegal message length: %d\n",
174 if (check_crc(buf
, len
))
177 usleep_range(3000, 6000);
179 r
= i2c_master_send(client
, buf
, len
);
180 dev_dbg(&client
->dev
, "send: %d\n", r
);
182 if (r
== -EREMOTEIO
) { /* Retry, chip was in standby */
183 usleep_range(6000, 10000);
184 r
= i2c_master_send(client
, buf
, len
);
185 dev_dbg(&client
->dev
, "send2: %d\n", r
);
194 static int pn544_i2c_read(struct i2c_client
*client
, u8
*buf
, int buflen
)
200 * You could read a packet in one go, but then you'd need to read
201 * max size and rest would be 0xff fill, so we do split reads.
203 r
= i2c_master_recv(client
, &len
, 1);
204 dev_dbg(&client
->dev
, "recv1: %d\n", r
);
209 if (len
< PN544_LLC_HCI_OVERHEAD
)
210 len
= PN544_LLC_HCI_OVERHEAD
;
211 else if (len
> (PN544_MSG_MAX_SIZE
- 1))
212 len
= PN544_MSG_MAX_SIZE
- 1;
214 if (1 + len
> buflen
) /* len+(data+crc16) */
219 r
= i2c_master_recv(client
, buf
+ 1, len
);
220 dev_dbg(&client
->dev
, "recv2: %d\n", r
);
225 usleep_range(3000, 6000);
230 static int pn544_fw_write(struct i2c_client
*client
, u8
*buf
, int len
)
234 dev_dbg(&client
->dev
, "%s\n", __func__
);
236 if (len
< PN544_FW_HEADER_SIZE
||
237 (PN544_FW_HEADER_SIZE
+ (buf
[1] << 8) + buf
[2]) != len
)
240 r
= i2c_master_send(client
, buf
, len
);
241 dev_dbg(&client
->dev
, "fw send: %d\n", r
);
243 if (r
== -EREMOTEIO
) { /* Retry, chip was in standby */
244 usleep_range(6000, 10000);
245 r
= i2c_master_send(client
, buf
, len
);
246 dev_dbg(&client
->dev
, "fw send2: %d\n", r
);
255 static int pn544_fw_read(struct i2c_client
*client
, u8
*buf
, int buflen
)
259 if (buflen
< PN544_FW_HEADER_SIZE
)
262 r
= i2c_master_recv(client
, buf
, PN544_FW_HEADER_SIZE
);
263 dev_dbg(&client
->dev
, "FW recv1: %d\n", r
);
268 if (r
< PN544_FW_HEADER_SIZE
)
271 len
= (buf
[1] << 8) + buf
[2];
272 if (len
== 0) /* just header, no additional data */
275 if (len
> buflen
- PN544_FW_HEADER_SIZE
)
278 r
= i2c_master_recv(client
, buf
+ PN544_FW_HEADER_SIZE
, len
);
279 dev_dbg(&client
->dev
, "fw recv2: %d\n", r
);
284 return r
+ PN544_FW_HEADER_SIZE
;
287 static irqreturn_t
pn544_irq_thread_fn(int irq
, void *dev_id
)
289 struct pn544_info
*info
= dev_id
;
290 struct i2c_client
*client
= info
->i2c_dev
;
293 BUG_ON(irq
!= info
->i2c_dev
->irq
);
295 dev_dbg(&client
->dev
, "IRQ\n");
297 mutex_lock(&info
->read_mutex
);
298 info
->read_irq
= PN544_INT
;
299 mutex_unlock(&info
->read_mutex
);
301 wake_up_interruptible(&info
->read_wait
);
306 static enum pn544_irq
pn544_irq_state(struct pn544_info
*info
)
310 mutex_lock(&info
->read_mutex
);
311 irq
= info
->read_irq
;
312 mutex_unlock(&info
->read_mutex
);
314 * XXX: should we check GPIO-line status directly?
315 * return pdata->irq_status() ? PN544_INT : PN544_NONE;
321 static ssize_t
pn544_read(struct file
*file
, char __user
*buf
,
322 size_t count
, loff_t
*offset
)
324 struct pn544_info
*info
= container_of(file
->private_data
,
325 struct pn544_info
, miscdev
);
326 struct i2c_client
*client
= info
->i2c_dev
;
331 dev_dbg(&client
->dev
, "%s: info: %p, count: %zu\n", __func__
,
334 mutex_lock(&info
->mutex
);
336 if (info
->state
== PN544_ST_COLD
) {
341 irq
= pn544_irq_state(info
);
342 if (irq
== PN544_NONE
) {
343 if (file
->f_flags
& O_NONBLOCK
) {
348 if (wait_event_interruptible(info
->read_wait
,
349 (info
->read_irq
== PN544_INT
))) {
355 if (info
->state
== PN544_ST_FW_READY
) {
356 len
= min(count
, info
->buflen
);
358 mutex_lock(&info
->read_mutex
);
359 r
= pn544_fw_read(info
->i2c_dev
, info
->buf
, len
);
360 info
->read_irq
= PN544_NONE
;
361 mutex_unlock(&info
->read_mutex
);
364 dev_err(&info
->i2c_dev
->dev
, "FW read failed: %d\n", r
);
368 print_hex_dump(KERN_DEBUG
, "FW read: ", DUMP_PREFIX_NONE
,
369 16, 2, info
->buf
, r
, false);
372 if (copy_to_user(buf
, info
->buf
, r
)) {
377 len
= min(count
, info
->buflen
);
379 mutex_lock(&info
->read_mutex
);
380 r
= pn544_i2c_read(info
->i2c_dev
, info
->buf
, len
);
381 info
->read_irq
= PN544_NONE
;
382 mutex_unlock(&info
->read_mutex
);
385 dev_err(&info
->i2c_dev
->dev
, "read failed (%d)\n", r
);
388 print_hex_dump(KERN_DEBUG
, "read: ", DUMP_PREFIX_NONE
,
389 16, 2, info
->buf
, r
, false);
392 if (copy_to_user(buf
, info
->buf
, r
)) {
399 mutex_unlock(&info
->mutex
);
404 static unsigned int pn544_poll(struct file
*file
, poll_table
*wait
)
406 struct pn544_info
*info
= container_of(file
->private_data
,
407 struct pn544_info
, miscdev
);
408 struct i2c_client
*client
= info
->i2c_dev
;
411 dev_dbg(&client
->dev
, "%s: info: %p\n", __func__
, info
);
413 mutex_lock(&info
->mutex
);
415 if (info
->state
== PN544_ST_COLD
) {
420 poll_wait(file
, &info
->read_wait
, wait
);
422 if (pn544_irq_state(info
) == PN544_INT
) {
423 r
= POLLIN
| POLLRDNORM
;
427 mutex_unlock(&info
->mutex
);
432 static ssize_t
pn544_write(struct file
*file
, const char __user
*buf
,
433 size_t count
, loff_t
*ppos
)
435 struct pn544_info
*info
= container_of(file
->private_data
,
436 struct pn544_info
, miscdev
);
437 struct i2c_client
*client
= info
->i2c_dev
;
441 dev_dbg(&client
->dev
, "%s: info: %p, count %zu\n", __func__
,
444 mutex_lock(&info
->mutex
);
446 if (info
->state
== PN544_ST_COLD
) {
452 * XXX: should we detect rset-writes and clean possible
455 if (info
->state
== PN544_ST_FW_READY
) {
458 if (count
< PN544_FW_HEADER_SIZE
) {
463 len
= min(count
, info
->buflen
);
464 if (copy_from_user(info
->buf
, buf
, len
)) {
469 print_hex_dump(KERN_DEBUG
, "FW write: ", DUMP_PREFIX_NONE
,
470 16, 2, info
->buf
, len
, false);
472 fw_len
= PN544_FW_HEADER_SIZE
+ (info
->buf
[1] << 8) +
475 if (len
> fw_len
) /* 1 msg at a time */
478 r
= pn544_fw_write(info
->i2c_dev
, info
->buf
, len
);
480 if (count
< PN544_LLC_MIN_SIZE
) {
485 len
= min(count
, info
->buflen
);
486 if (copy_from_user(info
->buf
, buf
, len
)) {
491 print_hex_dump(KERN_DEBUG
, "write: ", DUMP_PREFIX_NONE
,
492 16, 2, info
->buf
, len
, false);
494 if (len
> (info
->buf
[0] + 1)) /* 1 msg at a time */
495 len
= info
->buf
[0] + 1;
497 r
= pn544_i2c_write(info
->i2c_dev
, info
->buf
, len
);
500 mutex_unlock(&info
->mutex
);
506 static long pn544_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
508 struct pn544_info
*info
= container_of(file
->private_data
,
509 struct pn544_info
, miscdev
);
510 struct i2c_client
*client
= info
->i2c_dev
;
511 struct pn544_nfc_platform_data
*pdata
;
515 dev_dbg(&client
->dev
, "%s: info: %p, cmd: 0x%x\n", __func__
, info
, cmd
);
517 mutex_lock(&info
->mutex
);
519 if (info
->state
== PN544_ST_COLD
) {
524 pdata
= info
->i2c_dev
->dev
.platform_data
;
526 case PN544_GET_FW_MODE
:
527 dev_dbg(&client
->dev
, "%s: PN544_GET_FW_MODE\n", __func__
);
529 val
= (info
->state
== PN544_ST_FW_READY
);
530 if (copy_to_user((void __user
*)arg
, &val
, sizeof(val
))) {
537 case PN544_SET_FW_MODE
:
538 dev_dbg(&client
->dev
, "%s: PN544_SET_FW_MODE\n", __func__
);
540 if (copy_from_user(&val
, (void __user
*)arg
, sizeof(val
))) {
546 if (info
->state
== PN544_ST_FW_READY
)
550 r
= pn544_enable(info
, FW_MODE
);
554 if (info
->state
== PN544_ST_READY
)
557 r
= pn544_enable(info
, HCI_MODE
);
561 file
->f_pos
= info
->read_offset
;
565 dev_dbg(&client
->dev
, "%s: TCGETS\n", __func__
);
571 dev_err(&client
->dev
, "Unknown ioctl 0x%x\n", cmd
);
577 mutex_unlock(&info
->mutex
);
582 static int pn544_open(struct inode
*inode
, struct file
*file
)
584 struct pn544_info
*info
= container_of(file
->private_data
,
585 struct pn544_info
, miscdev
);
586 struct i2c_client
*client
= info
->i2c_dev
;
589 dev_dbg(&client
->dev
, "%s: info: %p, client %p\n", __func__
,
590 info
, info
->i2c_dev
);
592 mutex_lock(&info
->mutex
);
596 * XXX: maybe user (counter) would work better
598 if (info
->state
!= PN544_ST_COLD
) {
603 file
->f_pos
= info
->read_offset
;
604 r
= pn544_enable(info
, HCI_MODE
);
607 mutex_unlock(&info
->mutex
);
611 static int pn544_close(struct inode
*inode
, struct file
*file
)
613 struct pn544_info
*info
= container_of(file
->private_data
,
614 struct pn544_info
, miscdev
);
615 struct i2c_client
*client
= info
->i2c_dev
;
617 dev_dbg(&client
->dev
, "%s: info: %p, client %p\n",
618 __func__
, info
, info
->i2c_dev
);
620 mutex_lock(&info
->mutex
);
622 mutex_unlock(&info
->mutex
);
627 static const struct file_operations pn544_fops
= {
628 .owner
= THIS_MODULE
,
631 .write
= pn544_write
,
634 .release
= pn544_close
,
635 .unlocked_ioctl
= pn544_ioctl
,
639 static int pn544_suspend(struct device
*dev
)
641 struct i2c_client
*client
= to_i2c_client(dev
);
642 struct pn544_info
*info
;
645 dev_info(&client
->dev
, "***\n%s: client %p\n***\n", __func__
, client
);
647 info
= i2c_get_clientdata(client
);
648 dev_info(&client
->dev
, "%s: info: %p, client %p\n", __func__
,
651 mutex_lock(&info
->mutex
);
653 switch (info
->state
) {
654 case PN544_ST_FW_READY
:
655 /* Do not suspend while upgrading FW, please! */
661 * CHECK: Device should be in standby-mode. No way to check?
662 * Allowing low power mode for the regulator is potentially
663 * dangerous if pn544 does not go to suspension.
671 mutex_unlock(&info
->mutex
);
675 static int pn544_resume(struct device
*dev
)
677 struct i2c_client
*client
= to_i2c_client(dev
);
678 struct pn544_info
*info
= i2c_get_clientdata(client
);
681 dev_dbg(&client
->dev
, "%s: info: %p, client %p\n", __func__
,
684 mutex_lock(&info
->mutex
);
686 switch (info
->state
) {
689 * CHECK: If regulator low power mode is allowed in
690 * pn544_suspend, we should go back to normal mode
698 case PN544_ST_FW_READY
:
702 mutex_unlock(&info
->mutex
);
707 static SIMPLE_DEV_PM_OPS(pn544_pm_ops
, pn544_suspend
, pn544_resume
);
710 static struct device_attribute pn544_attr
=
711 __ATTR(nfc_test
, S_IRUGO
, pn544_test
, NULL
);
713 static int __devinit
pn544_probe(struct i2c_client
*client
,
714 const struct i2c_device_id
*id
)
716 struct pn544_info
*info
;
717 struct pn544_nfc_platform_data
*pdata
;
720 dev_dbg(&client
->dev
, "%s\n", __func__
);
721 dev_dbg(&client
->dev
, "IRQ: %d\n", client
->irq
);
723 /* private data allocation */
724 info
= kzalloc(sizeof(struct pn544_info
), GFP_KERNEL
);
726 dev_err(&client
->dev
,
727 "Cannot allocate memory for pn544_info.\n");
732 info
->buflen
= max(PN544_MSG_MAX_SIZE
, PN544_MAX_I2C_TRANSFER
);
733 info
->buf
= kzalloc(info
->buflen
, GFP_KERNEL
);
735 dev_err(&client
->dev
,
736 "Cannot allocate memory for pn544_info->buf.\n");
741 info
->regs
[0].supply
= reg_vdd_io
;
742 info
->regs
[1].supply
= reg_vbat
;
743 r
= regulator_bulk_get(&client
->dev
, ARRAY_SIZE(info
->regs
),
748 info
->i2c_dev
= client
;
749 info
->state
= PN544_ST_COLD
;
750 info
->read_irq
= PN544_NONE
;
751 mutex_init(&info
->read_mutex
);
752 mutex_init(&info
->mutex
);
753 init_waitqueue_head(&info
->read_wait
);
754 i2c_set_clientdata(client
, info
);
755 pdata
= client
->dev
.platform_data
;
757 dev_err(&client
->dev
, "No platform data\n");
762 if (!pdata
->request_resources
) {
763 dev_err(&client
->dev
, "request_resources() missing\n");
768 r
= pdata
->request_resources(client
);
770 dev_err(&client
->dev
, "Cannot get platform resources\n");
774 r
= request_threaded_irq(client
->irq
, NULL
, pn544_irq_thread_fn
,
775 IRQF_TRIGGER_RISING
, PN544_DRIVER_NAME
,
778 dev_err(&client
->dev
, "Unable to register IRQ handler\n");
782 /* If we don't have the test we don't need the sysfs file */
784 r
= device_create_file(&client
->dev
, &pn544_attr
);
786 dev_err(&client
->dev
,
787 "sysfs registration failed, error %d\n", r
);
792 info
->miscdev
.minor
= MISC_DYNAMIC_MINOR
;
793 info
->miscdev
.name
= PN544_DRIVER_NAME
;
794 info
->miscdev
.fops
= &pn544_fops
;
795 info
->miscdev
.parent
= &client
->dev
;
796 r
= misc_register(&info
->miscdev
);
798 dev_err(&client
->dev
, "Device registration failed\n");
802 dev_dbg(&client
->dev
, "%s: info: %p, pdata %p, client %p\n",
803 __func__
, info
, pdata
, client
);
809 device_remove_file(&client
->dev
, &pn544_attr
);
811 free_irq(client
->irq
, info
);
813 if (pdata
->free_resources
)
814 pdata
->free_resources();
816 regulator_bulk_free(ARRAY_SIZE(info
->regs
), info
->regs
);
825 static __devexit
int pn544_remove(struct i2c_client
*client
)
827 struct pn544_info
*info
= i2c_get_clientdata(client
);
828 struct pn544_nfc_platform_data
*pdata
= client
->dev
.platform_data
;
830 dev_dbg(&client
->dev
, "%s\n", __func__
);
832 misc_deregister(&info
->miscdev
);
834 device_remove_file(&client
->dev
, &pn544_attr
);
836 if (info
->state
!= PN544_ST_COLD
) {
840 info
->read_irq
= PN544_NONE
;
843 free_irq(client
->irq
, info
);
844 if (pdata
->free_resources
)
845 pdata
->free_resources();
847 regulator_bulk_free(ARRAY_SIZE(info
->regs
), info
->regs
);
854 static struct i2c_driver pn544_driver
= {
856 .name
= PN544_DRIVER_NAME
,
861 .probe
= pn544_probe
,
862 .id_table
= pn544_id_table
,
863 .remove
= __devexit_p(pn544_remove
),
866 static int __init
pn544_init(void)
870 pr_debug(DRIVER_DESC
": %s\n", __func__
);
872 r
= i2c_add_driver(&pn544_driver
);
874 pr_err(PN544_DRIVER_NAME
": driver registration failed\n");
881 static void __exit
pn544_exit(void)
883 i2c_del_driver(&pn544_driver
);
884 pr_info(DRIVER_DESC
", Exiting.\n");
887 module_init(pn544_init
);
888 module_exit(pn544_exit
);
890 MODULE_LICENSE("GPL");
891 MODULE_DESCRIPTION(DRIVER_DESC
);