/*
* Copyright (C) 2010 Trusted Logic S.A.
*
* 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/pn544.h>
#include <plat/gpio-core.h>
#include <plat/gpio-cfg.h>
#include <plat/gpio-cfg-helpers.h>
//#define pr_err printk
//#define pr_debug printk
//#define pr_warning printk
#define MAX_BUFFER_SIZE 512
bool iswakeup ;
struct pn544_dev {
wait_queue_head_t read_wq;
struct mutex read_mutex;
struct i2c_client *client;
struct miscdevice pn544_device;
unsigned int ven_gpio;
unsigned int firm_gpio;
unsigned int irq_gpio;
bool irq_enabled;
spinlock_t irq_enabled_lock;
};
static void pn544_disable_irq(struct pn544_dev *pn544_dev)
{
unsigned long flags;
spin_lock_irqsave(&pn544_dev->irq_enabled_lock, flags);
if (pn544_dev->irq_enabled) {
disable_irq_nosync(pn544_dev->client->irq);
pn544_dev->irq_enabled = false;
}
spin_unlock_irqrestore(&pn544_dev->irq_enabled_lock, flags);
}
static irqreturn_t pn544_dev_irq_handler(int irq, void *dev_id)
{
struct pn544_dev *pn544_dev = dev_id;
//if (!gpio_get_value(pn544_dev->irq_gpio)) {
// return IRQ_HANDLED;
//}
iswakeup = true; // Modify
pn544_disable_irq(pn544_dev);
/* Wake up waiting readers */
wake_up(&pn544_dev->read_wq);
return IRQ_HANDLED;
}
static ssize_t pn544_dev_read(struct file *filp, char __user *buf,
size_t count, loff_t *offset)
{
struct pn544_dev *pn544_dev = filp->private_data;
char tmp[MAX_BUFFER_SIZE];
int ret,i;
if (count > MAX_BUFFER_SIZE)
count = MAX_BUFFER_SIZE;
printk("%s : reading %zu bytes.\n", __func__, count);
mutex_lock(&pn544_dev->read_mutex);
if (!gpio_get_value(pn544_dev->irq_gpio)) {
if (filp->f_flags & O_NONBLOCK) {
ret = -EAGAIN;
goto fail;
}
pn544_dev->irq_enabled = true;
enable_irq(pn544_dev->client->irq);
//ret = wait_event_interruptible(pn544_dev->read_wq,
//gpio_get_value(pn544_dev->irq_gpio));
ret = wait_event_interruptible(pn544_dev->read_wq,
iswakeup == true); // Modify
iswakeup = false; // Modify
pn544_disable_irq(pn544_dev);
if (ret)
goto fail;
}
/* Read data */
ret = i2c_master_recv(pn544_dev->client, tmp, count);
mutex_unlock(&pn544_dev->read_mutex);
if (ret < 0) {
pr_err("%s: i2c_master_recv returned %d\n", __func__, ret);
return ret;
}
if (ret > count) {
pr_err("%s: received too many bytes from i2c (%d)\n",
__func__, ret);
return -EIO;
}
if (copy_to_user(buf, tmp, ret)) {
pr_warning("%s : failed to copy to user space\n", __func__);
return -EFAULT;
}
printk("IFD->PC:");
for(i = 0; i < ret; i++){
printk(" %02X", tmp[i]);
}
printk("\n");
return ret;
fail:
mutex_unlock(&pn544_dev->read_mutex);
return ret;
}
static ssize_t pn544_dev_write(struct file *filp, const char __user *buf,
size_t count, loff_t *offset)
{
struct pn544_dev *pn544_dev;
char tmp[MAX_BUFFER_SIZE];
int ret,i;
pn544_dev = filp->private_data;
if (count > MAX_BUFFER_SIZE)
count = MAX_BUFFER_SIZE;
if (copy_from_user(tmp, buf, count)) {
pr_err("%s : failed to copy from user space\n", __func__);
return -EFAULT;
}
printk("%s : writing %zu bytes.\n", __func__, count);
/* Write data */
ret = i2c_master_send(pn544_dev->client, tmp, count);
if (ret != count) {
pr_err("%s : i2c_master_send returned %d\n", __func__, ret);
ret = -EIO;
}
printk("PC->IFD:");
for(i = 0; i < count; i++){
printk(" %02X", tmp[i]);
}
printk("\n");
return ret;
}
static int pn544_dev_open(struct inode *inode, struct file *filp)
{
struct pn544_dev *pn544_dev = container_of(filp->private_data,
struct pn544_dev,
pn544_device);
filp->private_data = pn544_dev;
pr_debug("%s : %d,%d\n", __func__, imajor(inode), iminor(inode));
return 0;
}
static int pn544_dev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
struct pn544_dev *pn544_dev = filp->private_data;
switch (cmd) {
case PN544_SET_PWR:
if (arg == 2) {
/* power on with firmware download (requires hw reset)
*/
printk("%s power on with firmware\n", __func__);
gpio_set_value(pn544_dev->ven_gpio, 1);
gpio_set_value(pn544_dev->firm_gpio, 1);
msleep(10);
gpio_set_value(pn544_dev->ven_gpio, 0);
msleep(50);
gpio_set_value(pn544_dev->ven_gpio, 1);
msleep(10);
} else if (arg == 1) {
/* power on */
printk("%s power on\n", __func__);
gpio_set_value(pn544_dev->firm_gpio, 0);
gpio_set_value(pn544_dev->ven_gpio, 1);
msleep(10);
} else if (arg == 0) {
/* power off */
printk("%s power off\n", __func__);
gpio_set_value(pn544_dev->firm_gpio, 0);
gpio_set_value(pn544_dev->ven_gpio, 0);
msleep(10);
} else {
printk("%s bad arg %u\n", __func__, arg);
return -EINVAL;
}
break;
default:
printk("%s bad ioctl %u\n", __func__, cmd);
return -EINVAL;
}
return 0;
}
static const struct file_operations pn544_dev_fops = {
.owner = THIS_MODULE,
.llseek = no_llseek,
.read = pn544_dev_read,
.write = pn544_dev_write,
.open = pn544_dev_open,
.unlocked_ioctl = pn544_dev_ioctl,
};
static int pn544_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
int ret;
struct pn544_i2c_platform_data *platform_data;
struct pn544_dev *pn544_dev;
platform_data = client->dev.platform_data;
if (platform_data == NULL) {
pr_err("%s : nfc probe fail\n", __func__);
return -ENODEV;
}
printk("nfc probe step01 is ok\n");
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
pr_err("%s : need I2C_FUNC_I2C\n", __func__);
return -ENODEV;
}
printk("nfc probe step02 is ok\n");
ret = gpio_request(platform_data->irq_gpio, "nfc_int");
if (ret)
return -ENODEV;
ret = gpio_request(platform_data->ven_gpio, "nfc_ven");
if (ret)
goto err_ven;
ret = gpio_request(platform_data->firm_gpio, "nfc_firm");
if (ret)
goto err_firm;
printk("nfc probe step03 is ok\n");
pn544_dev = kzalloc(sizeof(*pn544_dev), GFP_KERNEL);
if (pn544_dev == NULL) {
dev_err(&client->dev,
"failed to allocate memory for module data\n");
ret = -ENOMEM;
goto err_exit;
}
printk("nfc probe step04 is ok\n");
pn544_dev->irq_gpio = platform_data->irq_gpio;
pn544_dev->ven_gpio = platform_data->ven_gpio;
pn544_dev->firm_gpio = platform_data->firm_gpio;
pn544_dev->client = client;
/* init mutex and queues */
init_waitqueue_head(&pn544_dev->read_wq);
mutex_init(&pn544_dev->read_mutex);
spin_lock_init(&pn544_dev->irq_enabled_lock);
pn544_dev->pn544_device.minor = MISC_DYNAMIC_MINOR;
pn544_dev->pn544_device.name = "pn544";
pn544_dev->pn544_device.fops = &pn544_dev_fops;
ret = misc_register(&pn544_dev->pn544_device);
if (ret) {
pr_err("%s : misc_register failed\n", __FILE__);
goto err_misc_register;
}
printk("nfc probe step05 is ok\n");
/* request irq. the irq is set whenever the chip has data available
* for reading. it is cleared when all data has
评论0