123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616 |
- /*
- * Copyright (C) 2012 Broadcom Corporation.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- *
- */
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/fs.h>
- #include <linux/slab.h>
- #include <linux/init.h>
- #include <linux/list.h>
- #include <linux/i2c.h>
- #include <linux/irq.h>
- #include <linux/jiffies.h>
- #include <linux/uaccess.h>
- #include <linux/delay.h>
- #include <linux/interrupt.h>
- #include <linux/io.h>
- #include <linux/platform_device.h>
- #include <linux/gpio.h>
- #include <linux/miscdevice.h>
- #include <linux/spinlock.h>
- #include <linux/poll.h>
- #include <linux/version.h>
- #include <linux/wakelock.h>
- #include <linux/of_gpio.h>
- #include <linux/regulator/consumer.h>
- #include <linux/nfc/bcm2079x.h>
- #undef DEBUG_BCM2079X_I2C_IRQ
- #define TRUE 1
- #define FALSE 0
- #define STATE_HIGH 1
- #define STATE_LOW 0
- /* end of compile options */
- /* do not change below */
- #define MAX_BUFFER_SIZE 780
- /* Read data */
- #define PACKET_HEADER_SIZE_NCI (4)
- #define PACKET_HEADER_SIZE_HCI (3)
- #define PACKET_TYPE_NCI (16)
- #define PACKET_TYPE_HCIEV (4)
- #define MAX_PACKET_SIZE (PACKET_HEADER_SIZE_NCI + 255)
- #ifdef CONFIG_MACH_VICTORLTE_CTC
- extern unsigned int system_rev;
- #endif
- struct bcm2079x_dev {
- wait_queue_head_t read_wq;
- struct mutex read_mutex;
- struct i2c_client *client;
- struct miscdevice bcm2079x_device;
- struct wake_lock nfc_wake_lock;
- unsigned int wake_gpio;
- unsigned int en_gpio;
- unsigned int irq_gpio;
- bool irq_enabled;
- spinlock_t irq_enabled_lock;
- unsigned int error_write;
- unsigned int error_read;
- unsigned int count_read;
- unsigned int count_irq;
- };
- static void bcm2079x_init_stat(struct bcm2079x_dev *bcm2079x_dev)
- {
- bcm2079x_dev->error_write = 0;
- bcm2079x_dev->error_read = 0;
- bcm2079x_dev->count_read = 0;
- bcm2079x_dev->count_irq = 0;
- }
- #ifndef DEBUG_BCM2079X_I2C_IRQ
- /*static void bcm2079x_disable_irq(struct bcm2079x_dev *bcm2079x_dev)
- {
- unsigned long flags;
- spin_lock_irqsave(&bcm2079x_dev->irq_enabled_lock, flags);
- if (bcm2079x_dev->irq_enabled) {
- disable_irq_wake(bcm2079x_dev->client->irq);
- disable_irq_nosync(bcm2079x_dev->client->irq);
- bcm2079x_dev->irq_enabled = false;
- }
- spin_unlock_irqrestore(&bcm2079x_dev->irq_enabled_lock, flags);
- }*/
- #endif
- static void bcm2079x_enable_irq(struct bcm2079x_dev *bcm2079x_dev)
- {
- unsigned long flags;
- spin_lock_irqsave(&bcm2079x_dev->irq_enabled_lock, flags);
- if (!bcm2079x_dev->irq_enabled) {
- bcm2079x_dev->irq_enabled = true;
- enable_irq(bcm2079x_dev->client->irq);
- enable_irq_wake(bcm2079x_dev->client->irq);
- }
- spin_unlock_irqrestore(&bcm2079x_dev->irq_enabled_lock, flags);
- }
- /*
- The alias address 0x79, when sent as a 7-bit address from the host processor
- will match the first byte (highest 2 bits) of the default client address
- (0x1FA) that is programmed in bcm20791.
- When used together with the first byte (0xFA) of the byte sequence below,
- it can be used to address the bcm20791 in a system that does not support
- 10-bit address and change the default address to 0x38.
- the new address can be changed by changing the CLIENT_ADDRESS below if 0x38
- conflicts with other device on the same i2c bus.
- */
- #define ALIAS_ADDRESS 0x79
- /*
- static void set_client_addr(struct bcm2079x_dev *bcm2079x_dev, int addr)
- {
- struct i2c_client *client = bcm2079x_dev->client;
- client->addr = addr;
- if (addr > 0x7F)
- client->flags |= I2C_CLIENT_TEN;
- dev_info(&client->dev,
- "Set client device changed to (0x%04X) flag = %04x\n",
- client->addr, client->flags);
- }
- */
- static void change_client_addr(struct bcm2079x_dev *bcm2079x_dev, int addr)
- {
- struct i2c_client *client;
- int ret;
- int i;
- int offset = 1;
- char addr_data[] = {
- 0xFA, 0xF2, 0x00, 0x00,
- 0x00, 0x38, 0x00, 0x00, 0x00, 0x2A
- };
- client = bcm2079x_dev->client;
- if ((client->flags & I2C_CLIENT_TEN) == I2C_CLIENT_TEN) {
- client->addr = ALIAS_ADDRESS;
- client->flags &= ~I2C_CLIENT_TEN;
- offset = 0;
- }
- addr_data[5] = addr & 0xFF;
- ret = 0;
- for (i = 1; i < sizeof(addr_data) - 1; ++i)
- ret += addr_data[i];
- addr_data[sizeof(addr_data) - 1] = (ret & 0xFF);
- dev_info(&client->dev,
- "Change client device from (0x%04X) flag = "\
- "%04x, addr_data[%d] = %02x\n",
- client->addr, client->flags, sizeof(addr_data) - 1,
- addr_data[sizeof(addr_data) - 1]);
- ret = i2c_master_send(client, addr_data+offset,
- sizeof(addr_data)-offset);
- if (ret != sizeof(addr_data)-offset) {
- client->addr = ALIAS_ADDRESS;
- client->flags &= ~I2C_CLIENT_TEN;
- dev_info(&client->dev,
- "Change client device from (0x%04X) flag = "\
- "%04x, addr_data[%d] = %02x\n",
- client->addr, client->flags, sizeof(addr_data) - 1,
- addr_data[sizeof(addr_data) - 1]);
- ret = i2c_master_send(client, addr_data, sizeof(addr_data));
- }
- client->addr = addr_data[5];
- dev_info(&client->dev,
- "Change client device changed to (0x%04X) flag = %04x, ret = %d\n",
- client->addr, client->flags, ret);
- }
- static irqreturn_t bcm2079x_dev_irq_handler(int irq, void *dev_id)
- {
- struct bcm2079x_dev *bcm2079x_dev = dev_id;
- unsigned long flags;
- #ifdef DEBUG_BCM2079X_I2C_IRQ
- pr_info("%s, irq is handled!!!!!!!!\n", __func__);
- #endif
- spin_lock_irqsave(&bcm2079x_dev->irq_enabled_lock, flags);
- bcm2079x_dev->count_irq++;
- spin_unlock_irqrestore(&bcm2079x_dev->irq_enabled_lock, flags);
- wake_up(&bcm2079x_dev->read_wq);
- wake_lock_timeout(&bcm2079x_dev->nfc_wake_lock, 2 * HZ);
- return IRQ_HANDLED;
- }
- static unsigned int bcm2079x_dev_poll(struct file *filp, poll_table *wait)
- {
- struct bcm2079x_dev *bcm2079x_dev = filp->private_data;
- unsigned int mask = 0;
- unsigned long flags;
- poll_wait(filp, &bcm2079x_dev->read_wq, wait);
- spin_lock_irqsave(&bcm2079x_dev->irq_enabled_lock, flags);
- if (bcm2079x_dev->count_irq > 0)
- mask |= POLLIN | POLLRDNORM;
- spin_unlock_irqrestore(&bcm2079x_dev->irq_enabled_lock, flags);
- return mask;
- }
- static ssize_t bcm2079x_dev_read(struct file *filp, char __user *buf,
- size_t count, loff_t *offset)
- {
- struct bcm2079x_dev *bcm2079x_dev = filp->private_data;
- unsigned char tmp[MAX_BUFFER_SIZE];
- int total, len, ret;
- total = 0;
- len = 0;
- if (bcm2079x_dev->count_irq > 0)
- bcm2079x_dev->count_irq--;
- bcm2079x_dev->count_read++;
- if (count > MAX_BUFFER_SIZE)
- count = MAX_BUFFER_SIZE;
- mutex_lock(&bcm2079x_dev->read_mutex);
- /** Read the first 4 bytes to include the length of the NCI or HCI.
- **/
- ret = i2c_master_recv(bcm2079x_dev->client, tmp, 4);
- if (ret == 4) {
- total = ret;
- /** First byte is the packet type
- **/
- switch (tmp[0]) {
- case PACKET_TYPE_NCI:
- len = tmp[PACKET_HEADER_SIZE_NCI-1];
- break;
- case PACKET_TYPE_HCIEV:
- len = tmp[PACKET_HEADER_SIZE_HCI-1];
- if (len == 0)
- /* Since payload is 0, decrement total size
- (from 4 to 3) */
- total--;
- else
- /*First byte of payload is in tmp[3] already */
- len--;
- break;
- default:
- /*Unknown packet byte */
- len = 0;
- break;
- } /* switch*/
- /** make sure full packet fits in the buffer **/
- if (len > 0 && (len + total) <= count) {
- /** read the remainder of the packet. **/
- ret = i2c_master_recv(bcm2079x_dev->client, tmp+total, len);
- if (ret == len)
- total += len;
- } /* if */
- } /* if */
- mutex_unlock(&bcm2079x_dev->read_mutex);
- if (total > count || copy_to_user(buf, tmp, total)) {
- dev_err(&bcm2079x_dev->client->dev,
- "failed to copy to user space, total = %d\n", total);
- total = -EFAULT;
- bcm2079x_dev->error_read++;
- }
- return total;
- }
- static ssize_t bcm2079x_dev_write(struct file *filp, const char __user *buf,
- size_t count, loff_t *offset)
- {
- struct bcm2079x_dev *bcm2079x_dev = filp->private_data;
- char tmp[MAX_BUFFER_SIZE];
- int ret;
- if (count > MAX_BUFFER_SIZE) {
- dev_err(&bcm2079x_dev->client->dev, "out of memory\n");
- return -ENOMEM;
- }
- if (copy_from_user(tmp, buf, count)) {
- dev_err(&bcm2079x_dev->client->dev,
- "failed to copy from user space\n");
- return -EFAULT;
- }
- mutex_lock(&bcm2079x_dev->read_mutex);
- /* Write data */
- ret = i2c_master_send(bcm2079x_dev->client, tmp, count);
- if (ret != count) {
- dev_err(&bcm2079x_dev->client->dev,
- "failed to write %d\n", ret);
- ret = -EIO;
- bcm2079x_dev->error_write++;
- }
- mutex_unlock(&bcm2079x_dev->read_mutex);
- return ret;
- }
- static int bcm2079x_dev_open(struct inode *inode, struct file *filp)
- {
- int ret = 0;
- struct bcm2079x_dev *bcm2079x_dev = container_of(filp->private_data,
- struct bcm2079x_dev,
- bcm2079x_device);
- filp->private_data = bcm2079x_dev;
- bcm2079x_init_stat(bcm2079x_dev);
- bcm2079x_enable_irq(bcm2079x_dev);
- dev_info(&bcm2079x_dev->client->dev,
- "dev node major=%d, minor=%d\n", imajor(inode), iminor(inode));
- return ret;
- }
- static long bcm2079x_dev_unlocked_ioctl(struct file *filp,
- unsigned int cmd, unsigned long arg)
- {
- struct bcm2079x_dev *bcm2079x_dev = filp->private_data;
- switch (cmd) {
- case BCMNFC_READ_FULL_PACKET:
- break;
- case BCMNFC_READ_MULTI_PACKETS:
- break;
- case BCMNFC_CHANGE_ADDR:
- dev_info(&bcm2079x_dev->client->dev,
- "%s, BCMNFC_CHANGE_ADDR (%x, %lx):\n", __func__, cmd,
- arg);
- change_client_addr(bcm2079x_dev, arg);
- break;
- case BCMNFC_POWER_CTL:
- dev_info(&bcm2079x_dev->client->dev,
- "%s, BCMNFC_POWER_CTL (%x, %lx):\n", __func__, cmd,
- arg);
- gpio_set_value(bcm2079x_dev->en_gpio, arg);
- break;
- case BCMNFC_WAKE_CTL:
- dev_info(&bcm2079x_dev->client->dev,
- "%s, BCMNFC_WAKE_CTL (%x, %lx):\n", __func__, cmd,
- arg);
- gpio_set_value(bcm2079x_dev->wake_gpio, arg);
- break;
- default:
- dev_err(&bcm2079x_dev->client->dev,
- "%s, unknown cmd (%x, %lx)\n", __func__, cmd, arg);
- return 0;
- }
- return 0;
- }
- static const struct file_operations bcm2079x_dev_fops = {
- .owner = THIS_MODULE,
- .llseek = no_llseek,
- .poll = bcm2079x_dev_poll,
- .read = bcm2079x_dev_read,
- .write = bcm2079x_dev_write,
- .open = bcm2079x_dev_open,
- .unlocked_ioctl = bcm2079x_dev_unlocked_ioctl
- };
- #ifdef CONFIG_OF
- /*device tree parsing*/
- static int bcm2079x_parse_dt(struct device *dev,
- struct bcm2079x_platform_data *pdata)
- {
- struct device_node *np = dev->of_node;
- pdata->irq_gpio = of_get_named_gpio_flags(np, "bcm2079x-i2c,irq-gpio",
- 0, &pdata->irq_gpio_flags);
- pdata->en_gpio = of_get_named_gpio_flags(np, "bcm2079x-i2c,en-gpio",
- 0, &pdata->en_gpio_flags);
- pdata->wake_gpio = of_get_named_gpio_flags(np, "bcm2079x-i2c,wake-gpio",
- 0, &pdata->wake_gpio_flags);
- if (pdata->wake_gpio < 0)
- of_property_read_u32(np, "bcm2079x-i2c,wake-expander-gpio",
- &pdata->wake_gpio);
- return 0;
- }
- #else
- static int bcm2079x_parse_dt(struct device *dev,
- struct bcm2079x_platform_data *pdata)
- {
- return -ENODEV;
- }
- #endif
- static int bcm2079x_probe(struct i2c_client *client,
- const struct i2c_device_id *id)
- {
- int ret;
- int err;
- struct bcm2079x_platform_data *platform_data;
- struct bcm2079x_dev *bcm2079x_dev;
- #ifdef DEBUG_BCM2079X_I2C_IRQ
- char tmp[5] = {0x10, 0x20, 0x00, 0x01, 0x00};
- #endif
- #ifdef CONFIG_MACH_VICTORLTE_CTC
- pr_info("%s : start system_rev : %d\n", __func__,system_rev);
- if (system_rev > 2)
- {
- pr_info("%s : probe fail \n", __func__);
- return -ENODEV;
- }
- #endif
- if (client->dev.of_node) {
- platform_data = devm_kzalloc(&client->dev,
- sizeof(struct bcm2079x_platform_data), GFP_KERNEL);
- if (!platform_data) {
- dev_err(&client->dev, "Failed to allocate memory\n");
- return -ENOMEM;
- }
- err = bcm2079x_parse_dt(&client->dev, platform_data);
- if (err)
- return err;
- } else {
- platform_data = client->dev.platform_data;
- }
- dev_info(&client->dev, "%s, probing bcm2079x driver flags = %x\n",
- __func__, client->flags);
- if (platform_data == NULL) {
- dev_err(&client->dev, "nfc probe fail\n");
- return -ENODEV;
- }
- if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
- dev_err(&client->dev, "need I2C_FUNC_I2C\n");
- return -ENODEV;
- }
- ret = gpio_request(platform_data->irq_gpio, "nfc_int");
- if (ret)
- return -ENODEV;
- gpio_direction_input(platform_data->irq_gpio);
- ret = gpio_request(platform_data->en_gpio, "nfc_en");
- if (ret)
- goto err_en;
- gpio_direction_output(platform_data->en_gpio, 0);
- ret = gpio_request(platform_data->wake_gpio, "nfc_wake");
- if (ret)
- goto err_firm;
- gpio_direction_output(platform_data->wake_gpio, 0);
- #ifdef DEBUG_BCM2079X_I2C_IRQ
- msleep(100);
- gpio_set_value_cansleep(platform_data->en_gpio, 1);
- msleep(100);
- #else
- gpio_set_value_cansleep(platform_data->en_gpio, 0);
- #endif
- gpio_set_value_cansleep(platform_data->wake_gpio, 1);
- bcm2079x_dev = kzalloc(sizeof(*bcm2079x_dev), GFP_KERNEL);
- if (bcm2079x_dev == NULL) {
- dev_err(&client->dev,
- "failed to allocate memory for module data\n");
- ret = -ENOMEM;
- goto err_exit;
- }
- bcm2079x_dev->wake_gpio = platform_data->wake_gpio;
- bcm2079x_dev->irq_gpio = platform_data->irq_gpio;
- bcm2079x_dev->en_gpio = platform_data->en_gpio;
- bcm2079x_dev->client = client;
- /* init mutex and queues */
- init_waitqueue_head(&bcm2079x_dev->read_wq);
- mutex_init(&bcm2079x_dev->read_mutex);
- spin_lock_init(&bcm2079x_dev->irq_enabled_lock);
- bcm2079x_dev->bcm2079x_device.minor = MISC_DYNAMIC_MINOR;
- bcm2079x_dev->bcm2079x_device.name = "bcm2079x";
- bcm2079x_dev->bcm2079x_device.fops = &bcm2079x_dev_fops;
- ret = misc_register(&bcm2079x_dev->bcm2079x_device);
- if (ret) {
- dev_err(&client->dev, "misc_register failed\n");
- goto err_misc_register;
- }
- /* wake lock init */
- wake_lock_init(&bcm2079x_dev->nfc_wake_lock,
- WAKE_LOCK_SUSPEND, "nfc_wake_lock");
- /* request irq. the irq is set whenever the chip has data available
- * for reading. it is cleared when all data has been read.
- */
- pr_info("\n client irq is %d\n",client->irq);
- dev_info(&client->dev, "requesting IRQ %d with IRQF_NO_SUSPEND\n",
- client->irq);
- ret = request_irq(client->irq, bcm2079x_dev_irq_handler,
- IRQF_TRIGGER_RISING|IRQF_NO_SUSPEND, client->name,
- bcm2079x_dev);
- if (ret) {
- dev_err(&client->dev, "request_irq failed\n");
- goto err_request_irq_failed;
- }
- i2c_set_clientdata(client, bcm2079x_dev);
- #ifndef DEBUG_BCM2079X_I2C_IRQ
- bcm2079x_dev->irq_enabled = false;
- disable_irq_nosync(bcm2079x_dev->client->irq);
- #else
- /* NCI reset test */
- ret = i2c_master_send(bcm2079x_dev->client, tmp, sizeof(tmp));
- if (ret != 5)
- pr_err("%s, i2c write error, NCI rest cmd err = %d\n",
- __func__, ret);
- else
- pr_info("%s, i2c write success!!! ret = %d\n",
- __func__, ret);
- #endif
- dev_info(&client->dev,
- "%s, probing bcm2079x driver exited successfully\n",
- __func__);
- return 0;
- err_request_irq_failed:
- wake_lock_destroy(&bcm2079x_dev->nfc_wake_lock);
- misc_deregister(&bcm2079x_dev->bcm2079x_device);
- err_misc_register:
- mutex_destroy(&bcm2079x_dev->read_mutex);
- kfree(bcm2079x_dev);
- err_exit:
- gpio_free(platform_data->wake_gpio);
- err_firm:
- gpio_free(platform_data->en_gpio);
- err_en:
- gpio_free(platform_data->irq_gpio);
- return ret;
- }
- static int bcm2079x_remove(struct i2c_client *client)
- {
- struct bcm2079x_dev *bcm2079x_dev;
- bcm2079x_dev = i2c_get_clientdata(client);
- wake_lock_destroy(&bcm2079x_dev->nfc_wake_lock);
- free_irq(client->irq, bcm2079x_dev);
- misc_deregister(&bcm2079x_dev->bcm2079x_device);
- mutex_destroy(&bcm2079x_dev->read_mutex);
- gpio_free(bcm2079x_dev->irq_gpio);
- gpio_free(bcm2079x_dev->en_gpio);
- gpio_free(bcm2079x_dev->wake_gpio);
- kfree(bcm2079x_dev);
- return 0;
- }
- static const struct i2c_device_id bcm2079x_id[] = {
- {"bcm2079x-i2c", 0},
- {}
- };
- #ifdef CONFIG_OF
- static struct of_device_id nfc_match_table[] = {
- { .compatible = "nfc,bcm2079x-i2c",},
- {},
- };
- #else
- #define nfc_match_table NULL
- #endif
- static struct i2c_driver bcm2079x_driver = {
- .id_table = bcm2079x_id,
- .probe = bcm2079x_probe,
- .remove = bcm2079x_remove,
- .driver = {
- .owner = THIS_MODULE,
- .name = "bcm2079x-i2c",
- .of_match_table = nfc_match_table,
- },
- };
- /*
- * module load/unload record keeping
- */
- static int __init bcm2079x_dev_init(void)
- {
- return i2c_add_driver(&bcm2079x_driver);
- }
- module_init(bcm2079x_dev_init);
- static void __exit bcm2079x_dev_exit(void)
- {
- i2c_del_driver(&bcm2079x_driver);
- }
- module_exit(bcm2079x_dev_exit);
- MODULE_AUTHOR("Broadcom");
- MODULE_DESCRIPTION("NFC bcm2079x driver");
- MODULE_LICENSE("GPL");
|