Add 3rd party device driver and associated platform support for NFC (NXP PN544 controller chip)
Please enabled in the default config for mid as well, ie: CONFIG_PN544_NFC=y Change-Id: Idf6a2b4a0283d6ae1dd7fdf0455b9d6529cd1670 Signed-off-by: Ken Lierman <[email protected]> --- arch/x86/kernel/mrst.c | 103 ++++++ drivers/misc/Kconfig | 11 + drivers/misc/Makefile | 1 + drivers/misc/pn544.c | 843 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/pn544.h | 97 ++++++ 5 files changed, 1055 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/pn544.c create mode 100644 include/linux/pn544.h diff --git a/arch/x86/kernel/mrst.c b/arch/x86/kernel/mrst.c index a316c11..a057a07 100644 --- a/arch/x86/kernel/mrst.c +++ b/arch/x86/kernel/mrst.c @@ -37,6 +37,7 @@ #include <linux/bh1770glc.h> #include <linux/leds-lp5523.h> #include <linux/atmel_maxtouch.h> +#include <linux/pn544.h> #include <asm/setup.h> #include <asm/mpspec_def.h> @@ -1141,6 +1142,107 @@ void *lp5523_platform_data_init(void *info) return &pdata; } +#define NFC_HOST_INT_GPIO "NFC-intr" +#define NFC_ENABLE_GPIO "NFC-enable" +#define NFC_FW_RESET_GPIO "NFC-reset" + +static int nfc_host_int_gpio, nfc_enable_gpio, nfc_fw_reset_gpio; + +static int pn544_nfc_request_resources(struct i2c_client *client) +{ + int ret; + ret = gpio_request(nfc_host_int_gpio, NFC_HOST_INT_GPIO); + if (ret) { + dev_err(&client->dev, "Request NFC INT GPIO fails %d\n", ret); + return -1; + } + ret = gpio_direction_input(nfc_host_int_gpio); + if (ret) { + dev_err(&client->dev, "Set GPIO Direction fails %d\n", ret); + goto err_int; + } + + ret = gpio_request(nfc_enable_gpio, NFC_ENABLE_GPIO); + if (ret) { + dev_err(&client->dev, + "Request for NFC Enable GPIO fails %d\n", ret); + goto err_int; + } + ret = gpio_direction_output(nfc_enable_gpio, 0); + if (ret) { + dev_err(&client->dev, "Set GPIO Direction fails %d\n", ret); + goto err_enable; + } + + ret = gpio_request(nfc_fw_reset_gpio, NFC_FW_RESET_GPIO); + if (ret) { + dev_err(&client->dev, + "Request for NFC FW Reset GPIO fails %d\n", ret); + goto err_enable; + } + ret = gpio_direction_output(nfc_fw_reset_gpio, 0); + if (ret) { + dev_err(&client->dev, "Set GPIO Direction fails %d\n", ret); + goto err_fw; + } + + return 0; +err_fw: + gpio_free(nfc_fw_reset_gpio); +err_enable: + gpio_free(nfc_enable_gpio); +err_int: + gpio_free(nfc_host_int_gpio); + return -1; +} + +static void pn544_nfc_free_resources(void) +{ + gpio_free(nfc_host_int_gpio); + gpio_free(nfc_enable_gpio); + gpio_free(nfc_fw_reset_gpio); +} + +static void pn544_nfc_enable(int fw) +{ + gpio_set_value(nfc_fw_reset_gpio, fw ? 1 : 0); + msleep(PN544_GPIO4VEN_TIME); + gpio_set_value(nfc_enable_gpio, 1); +} + +static void pn544_nfc_disable(void) +{ + gpio_set_value(nfc_enable_gpio, 0); +} + +void *pn544_platform_data(void *info) +{ + struct i2c_board_info *i2c_info = (struct i2c_board_info *) info; + static struct pn544_nfc_platform_data mfld_pn544_nfc_platform_data; + + memset(&mfld_pn544_nfc_platform_data, 0x00, + sizeof(struct pn544_nfc_platform_data)); + + nfc_host_int_gpio = get_gpio_by_name(NFC_HOST_INT_GPIO); + if (nfc_host_int_gpio == -1) + return NULL; + nfc_enable_gpio = get_gpio_by_name(NFC_ENABLE_GPIO); + if (nfc_enable_gpio == -1) + return NULL; + nfc_fw_reset_gpio = get_gpio_by_name(NFC_FW_RESET_GPIO); + if (nfc_fw_reset_gpio == -1) + return NULL; + + i2c_info->irq = nfc_host_int_gpio + MRST_IRQ_OFFSET; + mfld_pn544_nfc_platform_data.request_resources = pn544_nfc_request_resources; + mfld_pn544_nfc_platform_data.free_resources = pn544_nfc_free_resources; + mfld_pn544_nfc_platform_data.enable = pn544_nfc_enable; + mfld_pn544_nfc_platform_data.disable = pn544_nfc_disable; + + return &mfld_pn544_nfc_platform_data; +} + + static void tc35894xbg_reset_ctrl(struct i2c_client *client, int value) { struct tc35894xbg_platform_data *pdata = client->dev.platform_data; @@ -1356,6 +1458,7 @@ static const struct devs_id device_ids[] = { #ifdef CONFIG_BCM4751_GPS {"bcm4751-gps", SFI_DEV_TYPE_I2C, 0, &bcm4751_platform_data }, #endif + {"pn544", SFI_DEV_TYPE_I2C, 0, &pn544_platform_data}, {}, }; diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig index fbd6f0b..677560d 100644 --- a/drivers/misc/Kconfig +++ b/drivers/misc/Kconfig @@ -408,6 +408,17 @@ config INTEL_MID_PTI an Intel Atom (non-netbook) mobile device containing a MIPI P1149.7 standard implementation. +config PN544_NFC + tristate "PN544 NFC device" + depends on I2C && X86_MRST && CRC_CCITT + help + Say yes here you get support for the PN544 NFC device. + Note this is only a prototype driver and full support of the device + is still working-in-progress. + + This driver can also be built as a module. If so, the module + will be called mfld_nfc. + config BH1770GLC tristate "Rohm BH1770GLC ambient light and proximity sensor" depends on I2C diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile index 5f5be29..a40069b 100644 --- a/drivers/misc/Makefile +++ b/drivers/misc/Makefile @@ -42,6 +42,7 @@ obj-$(CONFIG_INTEL_MID_I2S) += intel-mid-i2s/ obj-$(CONFIG_CMT_SPEECH) += cmt-speech/ obj-$(CONFIG_BT_PCM) += bt-pcm/ obj-$(CONFIG_HMC6352) += hmc6352.o +obj-$(CONFIG_PN544_NFC) += pn544.o obj-y += eeprom/ obj-y += cb710/ obj-$(CONFIG_X86_MRST) += koski_hwid.o diff --git a/drivers/misc/pn544.c b/drivers/misc/pn544.c new file mode 100644 index 0000000..2b402b2 --- /dev/null +++ b/drivers/misc/pn544.c @@ -0,0 +1,843 @@ +/* + * Driver for the PN544 NFC chip. + * + * Copyright (C) 2010 Nokia Corporation + * + * Author: Jari Vanhala <[email protected]> + * Contact: Matti Aaltonen <[email protected]> + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#define DEBUG + +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/delay.h> +#include <linux/interrupt.h> +#include <linux/miscdevice.h> +#include <linux/poll.h> +#include <linux/completion.h> +#include <linux/regulator/consumer.h> +#include <linux/mutex.h> +#include <linux/serial_core.h> /* for TCGETS */ +#include <linux/crc-ccitt.h> +#include <linux/pn544.h> + +/* #define DO_RSET_TEST */ + +#define DRIVER_CARD "PN544 NFC" +#define DRIVER_DESC "NFC driver for PN544" + +static int debug; + +static struct i2c_device_id pn544_id_table[] = { + { PN544_DRIVER_NAME, 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, pn544_id_table); + +#define HCI_MODE 0 +#define FW_MODE 1 + +enum pn544_state { + PN544_ST_COLD, + PN544_ST_FW_READY, + PN544_ST_READY, +}; + +enum pn544_irq { + PN544_NONE, + PN544_INT, +}; + +struct pn544_info { + struct i2c_client *i2c_dev; + struct regulator_bulk_data regs[2]; + + enum pn544_state state; + wait_queue_head_t read_wait; + loff_t read_offset; + enum pn544_irq read_irq; + struct mutex read_mutex; /* read_irq */ +}; +static struct pn544_info *pn544; /* for open */ + +#ifdef PN544_VREG_CONTROL +static const char reg_vdd_io[] = "Vdd_IO"; +static const char reg_vbat[] = "VBat"; +#endif + +static void pn544_print_buf(char *msg, u8 *buf, int len) +{ +#ifdef DEBUG + int i; + + pr_debug(PN544_DRIVER_NAME ": Got %d at %s:", len, msg); + for (i = 0; i < len; i++) + pr_info(" %x", buf[i]); + + pr_info("\n"); +#endif +} + +static int pn544_enable(struct pn544_info *info, int mode) +{ + struct pn544_nfc_platform_data *pdata; + struct i2c_client *client = info->i2c_dev; + +#ifdef PN544_VREG_CONTROL + int r; + + r = regulator_bulk_enable(ARRAY_SIZE(info->regs), info->regs); + if (r < 0) + return r; +#endif + pdata = client->dev.platform_data; + info->read_irq = PN544_NONE; + pdata->enable(mode); + + if (mode) { + info->state = PN544_ST_FW_READY; + dev_dbg(&client->dev, "now in FW-mode\n"); + } else { + info->state = PN544_ST_READY; + dev_dbg(&client->dev, "now in HCI-mode\n"); + } + + msleep(PN544_BOOT_TIME); + + return 0; +} + +static void pn544_disable(struct pn544_info *info) +{ + struct pn544_nfc_platform_data *pdata; + struct i2c_client *client = info->i2c_dev; + + pdata = client->dev.platform_data; + pdata->disable(); + info->state = PN544_ST_COLD; + + dev_dbg(&client->dev, "Now in OFF-mode\n"); + + msleep(PN544_RESETVEN_TIME); + + info->read_irq = PN544_NONE; + +#ifdef PN544_VREG_CONTROL + regulator_bulk_disable(ARRAY_SIZE(info->regs), info->regs); +#endif +} + +static int check_crc(u8 *buf, int buflen) +{ + u8 len; + u16 crc; + + len = buf[0] + 1; + if (len < 4 || len != buflen || len > PN544_MSG_MAX_SIZE) { + pr_err(PN544_DRIVER_NAME + ": CRC; corrupted packet len %u (%d)\n", len, buflen); + pn544_print_buf("CRC", buf, buflen); + return -EPERM; + } + crc = crc_ccitt(0xffff, buf, len - 2); + crc = ~crc; + + if (buf[len-2] != (crc & 0xff) || buf[len-1] != (crc >> 8)) { + pr_err(PN544_DRIVER_NAME ": CRC error 0x%x != 0x%x 0x%x\n", + crc, buf[len-1], buf[len-2]); + + pn544_print_buf("CRC", buf, buflen); + return -EPERM; + } + return 0; +} + +static int pn544_i2c_write(struct i2c_client *client, u8 *buf, int len) +{ + int r; + + if (len < 4 || len != (buf[0] + 1) || check_crc(buf, len)) + return -EBADMSG; + + r = i2c_master_send(client, buf, len); + dev_dbg(&client->dev, "send: %d\n", r); + + if (r == -EREMOTEIO) { /* Retry, chip was in standby */ + msleep(PN544_WAKEUP_GUARD); + r = i2c_master_send(client, buf, len); + dev_dbg(&client->dev, "send2: %d\n", r); + } + + if (r != len) + return -EREMOTEIO; + + return r; +} + +static int pn544_i2c_read(struct i2c_client *client, u8 *buf, int buflen) +{ + int r; + u8 len; + + /* + * You could read a packet in one go, but then you'd need to read + * max size and rest would be 0xff fill, so we do split reads. + */ + r = i2c_master_recv(client, &len, 1); + dev_dbg(&client->dev, "recv1: %d\n", r); + + if (r != 1) + return -EREMOTEIO; + + if (len < PN544_LLC_HCI_OVERHEAD || len > (PN544_MSG_MAX_SIZE - 1)) { + dev_err(&client->dev, "%s: %d, corrupted packet; len %d\n", + __func__, r, len); + return -EBADMSG; + } + + if (1 + len > buflen) /* len+(data+crc16) */ + return -EOVERFLOW; + + buf[0] = len; + + r = i2c_master_recv(client, buf + 1, len); + dev_dbg(&client->dev, "recv2: %d\n", r); + + if (r != len) + return -EREMOTEIO; + + if (check_crc(buf, r + 1)) + return -EBADMSG; + + return r + 1; +} + +static int pn544_fw_write(struct i2c_client *client, u8 *buf, int len) +{ + int r; + + if (len < PN544_FW_HEADER_SIZE || + (PN544_FW_HEADER_SIZE + (buf[1] << 8) + buf[2]) != len) + return -EBADMSG; + + r = i2c_master_send(client, buf, len); + dev_dbg(&client->dev, "fw send: %d\n", r); + + return r; +} + +static int pn544_fw_read(struct i2c_client *client, u8 *buf, int buflen) +{ + int r, len; + + if (buflen < PN544_FW_HEADER_SIZE) + return -ENOSPC; + + r = i2c_master_recv(client, buf, PN544_FW_HEADER_SIZE); + dev_dbg(&client->dev, "FW recv1: %d\n", r); + + if (r < 0) + return r; + + if (r < PN544_FW_HEADER_SIZE) + return -EBADMSG; + + len = (buf[1] << 8) + buf[2]; + if (len == 0) /* just header, no additional data */ + return r; + + if (len > buflen - PN544_FW_HEADER_SIZE) + return -ENOSPC; + + r = i2c_master_recv(client, buf + PN544_FW_HEADER_SIZE, len); + dev_dbg(&client->dev, "fw recv2: %d\n", r); + + if (r != len) + return -EBADMSG; + + return r + PN544_FW_HEADER_SIZE; +} + +static irqreturn_t pn544_irq_handler(int irq, void *dev_id) +{ + struct pn544_info *info = dev_id; + struct i2c_client *client = info->i2c_dev; + + BUG_ON(!info); + BUG_ON(irq != info->i2c_dev->irq); + + dev_dbg(&client->dev, "IRQ\n"); + + mutex_lock(&info->read_mutex); + info->read_irq = PN544_INT; + mutex_unlock(&info->read_mutex); + + wake_up_interruptible(&info->read_wait); + + return IRQ_HANDLED; +} + +static enum pn544_irq pn544_irq_state(struct pn544_info *info) +{ + enum pn544_irq irq; + + mutex_lock(&info->read_mutex); + irq = info->read_irq; + mutex_unlock(&info->read_mutex); + /* + * XXX: should we check GPIO-line status directly? + * return pdata->irq_status() ? PN544_INT : PN544_NONE; + */ + + return irq; +} + +static ssize_t pn544_read(struct file *file, char __user *buf, + size_t count, loff_t *offset) +{ + struct pn544_info *info = file->private_data; + struct i2c_client *client = info->i2c_dev; + enum pn544_irq irq; + size_t len; + int r; + u8 read_buf[PN544_MSG_MAX_SIZE]; + + dev_dbg(&client->dev, "%s: info: %p, count: %d\n", __func__, + info, count); + + if (info->state == PN544_ST_COLD) + return -ENODEV; + + irq = pn544_irq_state(info); + if (irq == PN544_NONE) { + if (file->f_flags & O_NONBLOCK) + return -EAGAIN; + if (wait_event_interruptible(info->read_wait, + (info->read_irq == PN544_INT))) + return -ERESTARTSYS; + } + + if (info->state == PN544_ST_FW_READY) { + u8 fw_read_buf[PN544_MAX_I2C_TRANSFER]; + + len = min(count, sizeof(fw_read_buf)); + + mutex_lock(&info->read_mutex); + r = pn544_fw_read(info->i2c_dev, fw_read_buf, len); + info->read_irq = PN544_NONE; + mutex_unlock(&info->read_mutex); + + if (r < 0) { + dev_err(&info->i2c_dev->dev, "FW read failed: %d\n", r); + return r; + } + pn544_print_buf("fw_read", fw_read_buf, r); + + *offset += r; + if (copy_to_user(buf, fw_read_buf, r)) + return -EFAULT; + + return r; + } + + len = min(count, sizeof(read_buf)); + + mutex_lock(&info->read_mutex); + r = pn544_i2c_read(info->i2c_dev, read_buf, len); + info->read_irq = PN544_NONE; + mutex_unlock(&info->read_mutex); + + if (r < 0) { + dev_err(&info->i2c_dev->dev, "read failed (%d)\n", r); + return r; + } + pn544_print_buf("read", read_buf, r); + + *offset += r; + if (copy_to_user(buf, read_buf, r)) + return -EFAULT; + + return r; +} + +static unsigned int pn544_poll(struct file *file, poll_table *wait) +{ + struct pn544_info *info = file->private_data; + struct i2c_client *client = info->i2c_dev; + + dev_dbg(&client->dev, "%s: info: %p\n", __func__, info); + + if (info->state == PN544_ST_COLD) + return -ENODEV; + + poll_wait(file, &info->read_wait, wait); + + if (pn544_irq_state(info) == PN544_INT) + return POLLIN | POLLRDNORM; + + return 0; +} + +static ssize_t pn544_write(struct file *file, const char __user *buf, + size_t count, loff_t *ppos) +{ + struct pn544_info *info = file->private_data; + struct i2c_client *client = info->i2c_dev; + u8 data[PN544_MSG_MAX_SIZE]; + ssize_t len; + + dev_dbg(&client->dev, "%s: info: %p, count %d\n", __func__, + info, count); + + if (info->state == PN544_ST_COLD) + return -ENODEV; + + /* + * XXX: should we detect rset-writes and clean possible + * read_irq state + */ + if (info->state == PN544_ST_FW_READY) { + u8 fw_data[PN544_MAX_I2C_TRANSFER]; + size_t fw_len; + + if (count < PN544_FW_HEADER_SIZE) + return -EINVAL; + + len = min(count, sizeof(fw_data)); + if (copy_from_user(fw_data, buf, len)) + return -EFAULT; + + pn544_print_buf("fw_write", fw_data, len); + fw_len = PN544_FW_HEADER_SIZE + (fw_data[1] << 8) + fw_data[2]; + if (len > fw_len) /* 1 msg at a time */ + len = fw_len; + + return pn544_fw_write(info->i2c_dev, fw_data, len); + } + + if (count < PN544_LLC_MIN_SIZE) + return -EINVAL; + + len = min(count, sizeof(data)); + if (copy_from_user(data, buf, len)) + return -EFAULT; + + pn544_print_buf("write", data, len); + if (len > (data[0] + 1)) /* 1 msg at a time */ + len = data[0] + 1; + + return pn544_i2c_write(info->i2c_dev, data, len); +} + +static int pn544_ioctl(struct inode *inode, struct file *file, + unsigned int cmd, unsigned long arg) +{ + struct pn544_info *info = file->private_data; + struct i2c_client *client = info->i2c_dev; + struct pn544_nfc_platform_data *pdata; + unsigned int val; + int r; + + dev_dbg(&client->dev, "%s: info: %p, cmd: 0x%x\n", __func__, info, cmd); + + if (info->state == PN544_ST_COLD) + return -ENODEV; + + pdata = info->i2c_dev->dev.platform_data; + switch (cmd) { + case PN544_GET_FW_MODE: + val = (info->state == PN544_ST_FW_READY); + if (copy_to_user((void __user *)arg, &val, sizeof(val))) + return -EFAULT; + break; + + case PN544_SET_FW_MODE: + if (copy_from_user(&val, (void __user *)arg, sizeof(val))) + return -EFAULT; + if (val) { + if (info->state == PN544_ST_FW_READY) + break; + /* we should block open while in here */ + pn544_disable(info); + r = pn544_enable(info, FW_MODE); + if (r < 0) + return r; + } else { + if (info->state == PN544_ST_READY) + break; + pn544_disable(info); + r = pn544_enable(info, HCI_MODE); + if (r < 0) + return r; + } + file->f_pos = info->read_offset; + break; + + case PN544_GET_DEBUG: + val = debug; + if (copy_to_user((void __user *)arg, &val, sizeof(val))) + return -EFAULT; + break; + + case PN544_SET_DEBUG: + if (copy_from_user(&val, (void __user *)arg, sizeof(val))) + return -EFAULT; + debug = val; + break; + + case TCGETS: + return -ENOIOCTLCMD; + + default: + dev_err(&info->i2c_dev->dev, "Unknown ioctl 0x%x\n", cmd); + return -ENOIOCTLCMD; + } + + return 0; +} + +static int pn544_open(struct inode *inode, struct file *file) +{ + struct pn544_info *info; + struct i2c_client *client; + int r = 0; + + info = pn544; /* ugly, somehow we should get this via inode dev */ + if (!info) + return -ENXIO; + + client = info->i2c_dev; + + dev_dbg(&client->dev, "%s: info: %p, client %p\n", __func__, + info, info->i2c_dev); + /* + * Only 1 at a time. + * XXX: maybe user (counter) would work better + */ + if (info->state != PN544_ST_COLD) + return -EBUSY; + + file->private_data = info; + file->f_pos = info->read_offset; + r = pn544_enable(info, HCI_MODE); + + return r; +} + +static int pn544_close(struct inode *inode, struct file *file) +{ + struct pn544_info *info = file->private_data; + struct i2c_client *client = info->i2c_dev; + + dev_dbg(&client->dev, "%s: info: %p, client %p\n", + __func__, info, info->i2c_dev); + + pn544_disable(info); + + return 0; +} + +static const struct file_operations pn544_fops = { + .owner = THIS_MODULE, + .llseek = no_llseek, + .read = pn544_read, + .write = pn544_write, + .poll = pn544_poll, + .open = pn544_open, + .release = pn544_close, + .ioctl = pn544_ioctl, +}; + +static struct miscdevice pn544_miscdevice = { + .minor = MISC_DYNAMIC_MINOR, + .name = PN544_DRIVER_NAME, + .fops = &pn544_fops, +}; + +/* Optional test to see if the chip actually answers */ +#ifdef DO_RSET_TEST +static int pn544_rset_test(struct pn544_info *info) +{ + struct i2c_client *client = info->i2c_dev; + struct pn544_llc_packet rset = { /* U RSET frame without baudrate */ + .length = 0x05, + .header = 0xF9, + .data = { 0x04, 0x00, 0xC3, 0xE5 } + }; + struct pn544_llc_packet answer = { + .length = 0x03, + .header = 0xe6, + .data = { 0x17, 0xa7 } + }; + u8 read_buf[PN544_MSG_MAX_SIZE]; + int r; + + r = pn544_enable(info, HCI_MODE); + if (r < 0) + return r; + + pn544_print_buf("rset write", (u8 *)&rset, rset.length + 1); + r = pn544_i2c_write(client, (u8 *)&rset, rset.length + 1); + if (r < 0) { + dev_err(&client->dev, "Write error to rset: %d\n", r); + goto out; + } + + wait_event_timeout(info->read_wait, (info->read_irq == PN544_INT), + msecs_to_jiffies(PN544_WAKEUP_GUARD)); + if (info->read_irq == PN544_NONE) { + dev_err(&client->dev, "No answer to rset\n"); + r = -ENODEV; + goto out; + } + + mutex_lock(&info->read_mutex); + r = pn544_i2c_read(client, read_buf, sizeof(read_buf)); + info->read_irq = PN544_NONE; + mutex_unlock(&info->read_mutex); + + if (r < 0) { + dev_err(&client->dev, "Read error to rset: %d\n", r); + goto out; + } + + pn544_print_buf("rset read", read_buf, r); + + if (r != (answer.length + 1) || + memcmp(read_buf, (u8 *)&answer, r)) { + dev_err(&client->dev, "Wrong answer to rset\n"); + r = -ENODEV; + goto out; + } + + dev_dbg(&client->dev, "RSET test ok.\n"); + +out: + pn544_disable(info); + return r; +} +#endif + +#ifdef CONFIG_PM +static int pn544_suspend(struct device *dev) +{ + struct i2c_client *client = to_i2c_client(dev); + struct pn544_info *info; + + dev_info(&client->dev, "***\n%s: client %p\n***\n", __func__, client); + + info = i2c_get_clientdata(client); + dev_info(&client->dev, "%s: info: %p, client %p\n", __func__, + info, client); + + switch (info->state) { + case PN544_ST_FW_READY: + /* Do not suspend while upgrading FW, please! */ + return -EPERM; + + case PN544_ST_READY: + /* + * CHECK: Device should be in standby-mode. No way to check? + * Allowing low power mode for the regulator is potentially + * dangerous if pn544 does not go to suspension. + */ + break; + + case PN544_ST_COLD: + break; + }; + return 0; +} + +static int pn544_resume(struct device *dev) +{ + struct i2c_client *client = to_i2c_client(dev); + struct pn544_info *info = i2c_get_clientdata(client); + int r = 0; + + dev_dbg(&client->dev, "%s: info: %p, client %p\n", __func__, + info, client); + + switch (info->state) { + case PN544_ST_READY: + /* + * CHECK: If regulator low power mode is allowed in + * pn544_suspend, we should go back to normal mode + * here. + */ + break; + + case PN544_ST_COLD: + break; + + case PN544_ST_FW_READY: + break; + }; + + return r; +} + +static SIMPLE_DEV_PM_OPS(pn544_pm_ops, pn544_suspend, pn544_resume); +#endif + +static int __devinit pn544_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct pn544_info *info; + struct pn544_nfc_platform_data *pdata; + int r = 0; + + dev_dbg(&client->dev, "%s\n", __func__); + dev_dbg(&client->dev, "IRQ: %d\n", client->irq); + + /* private data allocation */ + info = kzalloc(sizeof(struct pn544_info), GFP_KERNEL); + if (!info) { + dev_err(&client->dev, + "Cannot allocate memory for pn544_info.\n"); + r = -ENOMEM; + goto err_kzalloc; + } + +#ifdef PN544_VREG_CONTROL + info->regs[0].supply = reg_vdd_io; + info->regs[1].supply = reg_vbat; + r = regulator_bulk_get(&client->dev, ARRAY_SIZE(info->regs), + info->regs); + if (r < 0) + goto err_kmalloc; +#endif + info->i2c_dev = client; + info->state = PN544_ST_COLD; + info->read_irq = PN544_NONE; + mutex_init(&info->read_mutex); + init_waitqueue_head(&info->read_wait); + i2c_set_clientdata(client, info); + pdata = client->dev.platform_data; + if (!pdata) { + dev_err(&client->dev, "IRQ not configured\n"); + r = -EINVAL; + goto err_reg; + } + + r = pdata->request_resources(client); + if (r) { + dev_err(&client->dev, "Cannot get platform data\n"); + goto err_reg; + } + + r = request_threaded_irq(client->irq, NULL, pn544_irq_handler, + IRQF_TRIGGER_RISING, PN544_DRIVER_NAME, + info); + if (r < 0) { + dev_err(&client->dev, "Unable to register IRQ handler\n"); + goto err_res; + } +#ifdef DO_RSET_TEST + r = pn544_rset_test(info); + if (r < 0) + goto err_irq; +#endif + pn544_miscdevice.parent = &client->dev; + r = misc_register(&pn544_miscdevice); + if (r < 0) { + dev_err(&client->dev, "Device registration failed\n"); + goto err_irq; + } + + dev_dbg(&client->dev, "%s: info: %p, pdata %p, client %p, misc %p\n", + __func__, info, pdata, client, &pn544_miscdevice); + + pn544 = info; /* we need this in open */ + + return 0; +err_irq: + free_irq(client->irq, info); +err_res: + pdata->free_resources(); +err_reg: +#ifdef PN544_VREG_CONTROL + regulator_bulk_free(ARRAY_SIZE(info->regs), info->regs); +err_kmalloc: +#endif + kfree(info); +err_kzalloc: + return r; +} + +static __devexit int pn544_remove(struct i2c_client *client) +{ + struct pn544_info *info = i2c_get_clientdata(client); + struct pn544_nfc_platform_data *pdata = client->dev.platform_data; + + dev_dbg(&client->dev, "%s\n", __func__); + + misc_deregister(&pn544_miscdevice); + if (info->state != PN544_ST_COLD) { + pdata->disable(); + info->read_irq = PN544_NONE; + } + + free_irq(client->irq, info); + pdata->free_resources(); +#ifdef PN544_VREG_CONTROL + regulator_bulk_free(ARRAY_SIZE(info->regs), info->regs); +#endif + kfree(info); + pn544 = NULL; + + return 0; +} + +static struct i2c_driver pn544_driver = { + .driver = { + .name = PN544_DRIVER_NAME, +#ifdef CONFIG_PM + .pm = &pn544_pm_ops, +#endif + }, + .probe = pn544_probe, + .id_table = pn544_id_table, + .remove = __devexit_p(pn544_remove), +}; + +static int __init pn544_init(void) +{ + int r; + + pr_debug(DRIVER_DESC ": %s\n", __func__); + + r = i2c_add_driver(&pn544_driver); + if (r) { + pr_err(PN544_DRIVER_NAME ": driver registration failed\n"); + return r; + } + return 0; +} + +static void __exit pn544_exit(void) +{ + flush_scheduled_work(); + i2c_del_driver(&pn544_driver); + pr_info(DRIVER_DESC ", Exiting.\n"); +} + +module_init(pn544_init); +module_exit(pn544_exit); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION(DRIVER_DESC); diff --git a/include/linux/pn544.h b/include/linux/pn544.h new file mode 100644 index 0000000..06641ce --- /dev/null +++ b/include/linux/pn544.h @@ -0,0 +1,97 @@ +/* + * Driver include for the PN544 NFC chip. + * + * Copyright (C) 2010 Nokia Corporation + * + * Author: Jari Vanhala <[email protected]> + * Contact: Matti Aaltoenn <[email protected]> + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _PN544_H_ +#define _PN544_H_ + +#include <linux/i2c.h> + +#define PN544_DRIVER_NAME "pn544" +#define PN544_MAXWINDOW_SIZE 7 +#define PN544_WINDOW_SIZE 4 +#define PN544_RETRIES 10 +#define PN544_MAX_I2C_TRANSFER 0xC0 +#define PN544_MSG_MAX_SIZE 0x21 /* at normal HCI mode */ + +/* ioctl */ +#define PN544_CHAR_BASE 'P' +#define PN544_IOR(num, dtype) _IOR(PN544_CHAR_BASE, num, dtype) +#define PN544_IOW(num, dtype) _IOW(PN544_CHAR_BASE, num, dtype) +#define PN544_GET_FW_MODE PN544_IOW(1, unsigned int) +#define PN544_SET_FW_MODE PN544_IOW(2, unsigned int) +#define PN544_GET_DEBUG PN544_IOW(3, unsigned int) +#define PN544_SET_DEBUG PN544_IOW(4, unsigned int) + +/* Timing restrictions (ms) */ +#define PN544_RESETVEN_TIME 30 /* 7 */ +#define PN544_PVDDVEN_TIME 0 +#define PN544_VBATVEN_TIME 0 +#define PN544_GPIO4VEN_TIME 0 +#define PN544_BOOT_TIME 10 /* 3 */ +#define PN544_WAKEUP_ACK 5 +#define PN544_WAKEUP_GUARD (PN544_WAKEUP_ACK + 1) +#define PN544_INACTIVITY_TIME 1000 +#define PN544_INTERFRAME_DELAY 200 /* us */ +#define PN544_BAUDRATE_CHANGE 150 /* us */ + +/* Debug bits */ +#define PN544_DEBUG_BUF 0x01 +#define PN544_DEBUG_READ 0x02 +#define PN544_DEBUG_WRITE 0x04 +#define PN544_DEBUG_IRQ 0x08 +#define PN544_DEBUG_CALLS 0x10 +#define PN544_DEBUG_MODE 0x20 + +/* Normal (HCI) mode */ +#define PN544_LLC_HCI_OVERHEAD 3 /* header + crc (to length) */ +#define PN544_LLC_MIN_SIZE (1 + PN544_LLC_HCI_OVERHEAD) /* length + */ +#define PN544_LLC_MAX_DATA (PN544_MSG_MAX_SIZE - 2) +#define PN544_LLC_MAX_HCI_SIZE (PN544_LLC_MAX_DATA - 2) + +struct pn544_llc_packet { + unsigned char length; /* of rest of packet */ + unsigned char header; + unsigned char data[PN544_LLC_MAX_DATA]; /* includes crc-ccitt */ +}; + +/* Firmware upgrade mode */ +#define PN544_FW_HEADER_SIZE 3 +/* max fw transfer is 1024bytes, but I2C limits it to 0xC0 */ +#define PN544_MAX_FW_DATA (PN544_MAX_I2C_TRANSFER - PN544_FW_HEADER_SIZE) + +struct pn544_fw_packet { + unsigned char command; /* status in answer */ + unsigned char length[2]; /* big-endian order (msf) */ + unsigned char data[PN544_MAX_FW_DATA]; +}; + +#ifdef __KERNEL__ +/* board config */ +struct pn544_nfc_platform_data { + int (*request_resources) (struct i2c_client *client); + void (*free_resources) (void); + void (*enable) (int fw); + void (*disable) (void); +}; +#endif /* __KERNEL__ */ + +#endif /* _PN544_H_ */ -- 1.7.1 _______________________________________________ Meego-kernel mailing list [email protected] http://lists.meego.com/listinfo/meego-kernel
