Updated corresponding to - NFC_AR_00_E000_12.02.01_OpnSrc
这个提交包含在:
227
nfc/i2c_drv.c
227
nfc/i2c_drv.c
@@ -1,4 +1,5 @@
|
||||
/******************************************************************************
|
||||
* Copyright (C) 2015, The Linux Foundation. All rights reserved.
|
||||
* Copyright (C) 2013-2021 NXP
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
@@ -44,7 +45,6 @@
|
||||
#include "common.h"
|
||||
#include "common_ese.h"
|
||||
|
||||
static ssize_t i2c_read_internal(struct nfc_dev *dev, char *buf, size_t count, int timeout);
|
||||
/**
|
||||
* i2c_disable_irq()
|
||||
*
|
||||
@@ -91,8 +91,9 @@ int i2c_enable_irq(struct nfc_dev *dev)
|
||||
|
||||
static irqreturn_t i2c_irq_handler(int irq, void *dev_id)
|
||||
{
|
||||
nfc_dev_t *nfc_dev = dev_id;
|
||||
i2c_dev_t *i2c_dev = &nfc_dev->i2c_dev;
|
||||
struct nfc_dev *nfc_dev = dev_id;
|
||||
struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev;
|
||||
|
||||
if (device_may_wakeup(&i2c_dev->client->dev))
|
||||
pm_wakeup_event(&i2c_dev->client->dev, WAKEUP_SRC_TIMEOUT);
|
||||
|
||||
@@ -102,52 +103,22 @@ static irqreturn_t i2c_irq_handler(int irq, void *dev_id)
|
||||
return IRQ_HANDLED;
|
||||
}
|
||||
|
||||
int i2c_read(struct nfc_dev *dev, char *buf, size_t count, int timeout){
|
||||
|
||||
int i2c_read(struct nfc_dev *nfc_dev, char *buf, size_t count, int timeout)
|
||||
{
|
||||
int ret;
|
||||
struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev;
|
||||
struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
|
||||
|
||||
pr_debug("%s : reading %zu bytes.\n", __func__, count);
|
||||
|
||||
if(timeout > NCI_MAX_CMD_RSP_TIMEOUT)
|
||||
timeout = NCI_MAX_CMD_RSP_TIMEOUT;
|
||||
|
||||
return i2c_read_internal(dev, buf, count, timeout);
|
||||
}
|
||||
|
||||
int i2c_write(struct nfc_dev *dev, const char *buf, size_t count,
|
||||
int max_retry_cnt)
|
||||
{
|
||||
int ret = -EINVAL;
|
||||
int retry_cnt;
|
||||
|
||||
pr_debug("%s : writing %zu bytes.\n", __func__, count);
|
||||
|
||||
for (retry_cnt = 1; retry_cnt <= max_retry_cnt; retry_cnt++) {
|
||||
ret = i2c_master_send(dev->i2c_dev.client, buf, count);
|
||||
if (ret <= 0) {
|
||||
pr_warn("%s: write failed, Maybe in Standby Mode - Retry(%d)\n", __func__,
|
||||
retry_cnt);
|
||||
usleep_range(WRITE_RETRY_WAIT_TIME_USEC,
|
||||
WRITE_RETRY_WAIT_TIME_USEC + 100);
|
||||
} else if (ret == count)
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ssize_t i2c_read_internal(struct nfc_dev *dev, char *buf, size_t count, int timeout)
|
||||
{
|
||||
int ret;
|
||||
char tmp[MAX_BUFFER_SIZE];
|
||||
nfc_dev_t *nfc_dev = dev;
|
||||
i2c_dev_t *i2c_dev = &nfc_dev->i2c_dev;
|
||||
platform_gpio_t *nfc_gpio = &nfc_dev->configs.gpio;
|
||||
if (timeout > NCI_CMD_RSP_TIMEOUT)
|
||||
timeout = NCI_CMD_RSP_TIMEOUT;
|
||||
|
||||
if (count > MAX_BUFFER_SIZE)
|
||||
count = MAX_BUFFER_SIZE;
|
||||
|
||||
pr_debug("%s : reading %zu bytes.\n", __func__, count);
|
||||
mutex_lock(&nfc_dev->read_mutex);
|
||||
if (!gpio_get_value(nfc_gpio->irq)) {
|
||||
|
||||
while (1) {
|
||||
ret = 0;
|
||||
if (!i2c_dev->irq_enabled) {
|
||||
@@ -155,9 +126,9 @@ static ssize_t i2c_read_internal(struct nfc_dev *dev, char *buf, size_t count, i
|
||||
enable_irq(i2c_dev->client->irq);
|
||||
}
|
||||
if (!gpio_get_value(nfc_gpio->irq)) {
|
||||
if(timeout) {
|
||||
if (timeout) {
|
||||
ret = wait_event_interruptible_timeout(nfc_dev->read_wq,
|
||||
!i2c_dev-> irq_enabled, msecs_to_jiffies(timeout));
|
||||
!i2c_dev->irq_enabled, msecs_to_jiffies(timeout));
|
||||
|
||||
if (ret <= 0) {
|
||||
pr_err("%s timeout/error in read\n", __func__);
|
||||
@@ -185,11 +156,11 @@ static ssize_t i2c_read_internal(struct nfc_dev *dev, char *buf, size_t count, i
|
||||
}
|
||||
}
|
||||
|
||||
memset(tmp, 0x00, count);
|
||||
memset(buf, 0x00, count);
|
||||
/* Read data */
|
||||
ret = i2c_master_recv(nfc_dev->i2c_dev.client, tmp, count);
|
||||
ret = i2c_master_recv(nfc_dev->i2c_dev.client, buf, count);
|
||||
if (ret <= 0) {
|
||||
pr_err("%s: i2c_read returned %d\n", __func__, ret);
|
||||
pr_err("%s: returned %d\n", __func__, ret);
|
||||
goto err;
|
||||
}
|
||||
/* check if it's response of cold reset command
|
||||
@@ -197,16 +168,15 @@ static ssize_t i2c_read_internal(struct nfc_dev *dev, char *buf, size_t count, i
|
||||
* command was sent by driver
|
||||
*/
|
||||
if (nfc_dev->cold_reset.rsp_pending) {
|
||||
if (IS_PROP_CMD_RSP(tmp)) {
|
||||
if (IS_PROP_CMD_RSP(buf)) {
|
||||
/* Read data */
|
||||
ret = i2c_master_recv(nfc_dev->i2c_dev.client, &tmp[NCI_PAYLOAD_IDX],
|
||||
tmp[NCI_PAYLOAD_LEN_IDX]);
|
||||
ret = i2c_master_recv(nfc_dev->i2c_dev.client, &buf[NCI_PAYLOAD_IDX],
|
||||
buf[NCI_PAYLOAD_LEN_IDX]);
|
||||
if (ret <= 0) {
|
||||
pr_err("%s: failure to read prop cold reset/protection rsp header\n", __func__);
|
||||
pr_err("%s: error reading cold reset/prot rsp header\n", __func__);
|
||||
goto err;
|
||||
}
|
||||
wakeup_on_prop_rsp(nfc_dev, tmp);
|
||||
mutex_unlock(&nfc_dev->read_mutex);
|
||||
wakeup_on_prop_rsp(nfc_dev, buf);
|
||||
/*
|
||||
* NFC process doesn't know about cold reset command
|
||||
* being sent as it was initiated by eSE process
|
||||
@@ -215,47 +185,76 @@ static ssize_t i2c_read_internal(struct nfc_dev *dev, char *buf, size_t count, i
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (copy_to_user(buf, tmp, ret)) {
|
||||
pr_warn("%s : failed to copy to user space\n", __func__);
|
||||
ret = -EFAULT;
|
||||
}
|
||||
|
||||
err:
|
||||
mutex_unlock(&nfc_dev->read_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t nfc_i2c_dev_read(struct file * filp, char __user *buf,
|
||||
size_t count, loff_t * offset)
|
||||
int i2c_write(struct nfc_dev *nfc_dev, const char *buf, size_t count,
|
||||
int max_retry_cnt)
|
||||
{
|
||||
pr_debug("%s : reading %zu bytes.\n", __func__, count);
|
||||
if (filp->f_flags & O_NONBLOCK) {
|
||||
pr_err(":f_falg has O_NONBLOCK. EAGAIN\n");
|
||||
return -EAGAIN;
|
||||
}
|
||||
return i2c_read_internal((nfc_dev_t *)filp->private_data, buf, count, 0);
|
||||
}
|
||||
|
||||
ssize_t nfc_i2c_dev_write(struct file * filp, const char __user *buf,
|
||||
size_t count, loff_t * offset)
|
||||
{
|
||||
int ret;
|
||||
char tmp[MAX_DL_BUFFER_SIZE];
|
||||
nfc_dev_t *nfc_dev = filp->private_data;
|
||||
int ret = -EINVAL;
|
||||
int retry_cnt;
|
||||
|
||||
if (count > MAX_DL_BUFFER_SIZE)
|
||||
count = MAX_DL_BUFFER_SIZE;
|
||||
|
||||
if (copy_from_user(tmp, buf, count)) {
|
||||
pr_debug("%s : writing %zu bytes.\n", __func__, count);
|
||||
|
||||
for (retry_cnt = 1; retry_cnt <= max_retry_cnt; retry_cnt++) {
|
||||
ret = i2c_master_send(nfc_dev->i2c_dev.client, buf, count);
|
||||
if (ret <= 0) {
|
||||
pr_warn("%s: failed to write ret(%d), Maybe in Standby Mode - Retry(%d)\n", __func__,
|
||||
ret, retry_cnt);
|
||||
usleep_range(WRITE_RETRY_WAIT_TIME_USEC,
|
||||
WRITE_RETRY_WAIT_TIME_USEC + 100);
|
||||
} else if (ret != count) {
|
||||
pr_err("%s: failed to write %d\n", __func__, ret);
|
||||
ret = -EIO;
|
||||
} else if (ret == count)
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t nfc_i2c_dev_read(struct file *filp, char __user *buf,
|
||||
size_t count, loff_t *offset)
|
||||
{
|
||||
int ret = 0;
|
||||
struct nfc_dev *nfc_dev = (struct nfc_dev *)filp->private_data;
|
||||
|
||||
if (filp->f_flags & O_NONBLOCK) {
|
||||
pr_err(":f_falg has O_NONBLOCK. EAGAIN\n");
|
||||
return -EAGAIN;
|
||||
}
|
||||
mutex_lock(&nfc_dev->read_mutex);
|
||||
ret = i2c_read(nfc_dev, nfc_dev->read_kbuf, count, 0);
|
||||
if (ret > 0) {
|
||||
if (copy_to_user(buf, nfc_dev->read_kbuf, ret)) {
|
||||
pr_warn("%s : failed to copy to user space\n", __func__);
|
||||
ret = -EFAULT;
|
||||
}
|
||||
}
|
||||
mutex_unlock(&nfc_dev->read_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t nfc_i2c_dev_write(struct file *filp, const char __user *buf,
|
||||
size_t count, loff_t *offset)
|
||||
{
|
||||
int ret = 0;
|
||||
struct nfc_dev *nfc_dev = (struct nfc_dev *)filp->private_data;
|
||||
|
||||
if (count > MAX_DL_BUFFER_SIZE)
|
||||
count = MAX_DL_BUFFER_SIZE;
|
||||
|
||||
mutex_lock(&nfc_dev->write_mutex);
|
||||
if (copy_from_user(nfc_dev->write_kbuf, buf, count)) {
|
||||
pr_err("%s : failed to copy from user space\n", __func__);
|
||||
mutex_unlock(&nfc_dev->write_mutex);
|
||||
return -EFAULT;
|
||||
}
|
||||
ret = i2c_write(nfc_dev, tmp, count, NO_RETRY);
|
||||
if (ret != count) {
|
||||
pr_err("%s: failed to write %d\n", __func__, ret);
|
||||
ret = -EIO;
|
||||
}
|
||||
|
||||
ret = i2c_write(nfc_dev, nfc_dev->write_kbuf, count, NO_RETRY);
|
||||
mutex_unlock(&nfc_dev->write_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -272,12 +271,13 @@ static const struct file_operations nfc_i2c_dev_fops = {
|
||||
int nfc_i2c_dev_probe(struct i2c_client *client, const struct i2c_device_id *id)
|
||||
{
|
||||
int ret = 0;
|
||||
nfc_dev_t *nfc_dev = NULL;
|
||||
i2c_dev_t *i2c_dev = NULL;
|
||||
platform_configs_t nfc_configs;
|
||||
platform_gpio_t *nfc_gpio = &nfc_configs.gpio;
|
||||
struct nfc_dev *nfc_dev = NULL;
|
||||
struct i2c_dev *i2c_dev = NULL;
|
||||
struct platform_configs nfc_configs;
|
||||
struct platform_gpio *nfc_gpio = &nfc_configs.gpio;
|
||||
|
||||
pr_debug("%s: enter\n", __func__);
|
||||
/*retrive details of gpios from dt */
|
||||
/*retrieve details of gpios from dt */
|
||||
ret = nfc_parse_dt(&client->dev, &nfc_configs, PLATFORM_IF_I2C);
|
||||
if (ret) {
|
||||
pr_err("%s : failed to parse dt\n", __func__);
|
||||
@@ -289,11 +289,21 @@ int nfc_i2c_dev_probe(struct i2c_client *client, const struct i2c_device_id *id)
|
||||
ret = -ENODEV;
|
||||
goto err;
|
||||
}
|
||||
nfc_dev = kzalloc(sizeof(nfc_dev_t), GFP_KERNEL);
|
||||
nfc_dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
|
||||
if (nfc_dev == NULL) {
|
||||
ret = -ENOMEM;
|
||||
goto err;
|
||||
}
|
||||
nfc_dev->read_kbuf = kzalloc(MAX_BUFFER_SIZE, GFP_DMA | GFP_KERNEL);
|
||||
if (!nfc_dev->read_kbuf) {
|
||||
ret = -ENOMEM;
|
||||
goto err;
|
||||
}
|
||||
nfc_dev->write_kbuf = kzalloc(MAX_DL_BUFFER_SIZE, GFP_DMA | GFP_KERNEL);
|
||||
if (!nfc_dev->write_kbuf) {
|
||||
ret = -ENOMEM;
|
||||
goto err;
|
||||
}
|
||||
nfc_dev->interface = PLATFORM_IF_I2C;
|
||||
nfc_dev->nfc_state = NFC_STATE_NCI;
|
||||
nfc_dev->i2c_dev.client = client;
|
||||
@@ -302,11 +312,7 @@ int nfc_i2c_dev_probe(struct i2c_client *client, const struct i2c_device_id *id)
|
||||
nfc_dev->nfc_write = i2c_write;
|
||||
nfc_dev->nfc_enable_intr = i2c_enable_irq;
|
||||
nfc_dev->nfc_disable_intr = i2c_disable_irq;
|
||||
#if defined(RECOVERY_ENABLE)
|
||||
nfc_dev->recovery_required = false;
|
||||
nfc_dev->fw_major_version = 0;
|
||||
#endif
|
||||
|
||||
ret = configure_gpio(nfc_gpio->ven, GPIO_OUTPUT);
|
||||
if (ret) {
|
||||
pr_err("%s: unable to request nfc reset gpio [%d]\n", __func__, nfc_gpio->ven);
|
||||
@@ -324,14 +330,14 @@ int nfc_i2c_dev_probe(struct i2c_client *client, const struct i2c_device_id *id)
|
||||
nfc_gpio->dwl_req);
|
||||
}
|
||||
|
||||
/*copy the retrived gpio details from DT */
|
||||
/*copy the retrieved gpio details from DT */
|
||||
memcpy(&nfc_dev->configs, &nfc_configs, sizeof(struct platform_configs));
|
||||
|
||||
/* init mutex and queues */
|
||||
init_waitqueue_head(&nfc_dev->read_wq);
|
||||
mutex_init(&nfc_dev->read_mutex);
|
||||
mutex_init(&nfc_dev->write_mutex);
|
||||
mutex_init(&nfc_dev->dev_ref_mutex);
|
||||
mutex_init(&nfc_dev->ese_access_mutex);
|
||||
spin_lock_init(&i2c_dev->irq_enabled_lock);
|
||||
common_ese_init(nfc_dev);
|
||||
ret = nfc_misc_register(nfc_dev, &nfc_i2c_dev_fops, DEV_COUNT,
|
||||
@@ -352,8 +358,9 @@ int nfc_i2c_dev_probe(struct i2c_client *client, const struct i2c_device_id *id)
|
||||
i2c_disable_irq(nfc_dev);
|
||||
|
||||
ret = nfcc_hw_check(nfc_dev);
|
||||
if (ret || nfc_dev->nfc_state == NFC_STATE_UNKNOWN) {
|
||||
if (ret != 0) {
|
||||
pr_err("nfc hw check failed ret %d\n", ret);
|
||||
goto err_nfc_misc_unregister;
|
||||
}
|
||||
|
||||
device_init_wakeup(&client->dev, true);
|
||||
@@ -368,21 +375,21 @@ err_nfc_misc_unregister:
|
||||
err_mutex_destroy:
|
||||
mutex_destroy(&nfc_dev->dev_ref_mutex);
|
||||
mutex_destroy(&nfc_dev->read_mutex);
|
||||
mutex_destroy(&nfc_dev->ese_access_mutex);
|
||||
mutex_destroy(&nfc_dev->cold_reset.sync_mutex);
|
||||
mutex_destroy(&nfc_dev->write_mutex);
|
||||
err:
|
||||
if (nfc_dev) {
|
||||
gpio_free_all(nfc_dev);
|
||||
kfree(nfc_dev);
|
||||
}
|
||||
pr_err("%s: probing not successful, check hardware\n", __func__);
|
||||
return ret;
|
||||
gpio_free_all(nfc_dev);
|
||||
kfree(nfc_dev->read_kbuf);
|
||||
kfree(nfc_dev->write_kbuf);
|
||||
kfree(nfc_dev);
|
||||
pr_err("%s: probing not successful, check hardware\n", __func__);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int nfc_i2c_dev_remove(struct i2c_client *client)
|
||||
{
|
||||
int ret = 0;
|
||||
nfc_dev_t *nfc_dev = NULL;
|
||||
struct nfc_dev *nfc_dev = NULL;
|
||||
|
||||
pr_info("%s: remove device\n", __func__);
|
||||
nfc_dev = i2c_get_clientdata(client);
|
||||
if (!nfc_dev) {
|
||||
@@ -398,9 +405,10 @@ int nfc_i2c_dev_remove(struct i2c_client *client)
|
||||
free_irq(client->irq, nfc_dev);
|
||||
nfc_misc_unregister(nfc_dev, DEV_COUNT);
|
||||
mutex_destroy(&nfc_dev->read_mutex);
|
||||
mutex_destroy(&nfc_dev->ese_access_mutex);
|
||||
mutex_destroy(&nfc_dev->cold_reset.sync_mutex);
|
||||
mutex_destroy(&nfc_dev->write_mutex);
|
||||
gpio_free_all(nfc_dev);
|
||||
kfree(nfc_dev->read_kbuf);
|
||||
kfree(nfc_dev->write_kbuf);
|
||||
kfree(nfc_dev);
|
||||
return ret;
|
||||
}
|
||||
@@ -408,8 +416,8 @@ int nfc_i2c_dev_remove(struct i2c_client *client)
|
||||
int nfc_i2c_dev_suspend(struct device *device)
|
||||
{
|
||||
struct i2c_client *client = to_i2c_client(device);
|
||||
nfc_dev_t *nfc_dev = i2c_get_clientdata(client);
|
||||
i2c_dev_t *i2c_dev = &nfc_dev->i2c_dev;
|
||||
struct nfc_dev *nfc_dev = i2c_get_clientdata(client);
|
||||
struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev;
|
||||
|
||||
if (device_may_wakeup(&client->dev) && i2c_dev->irq_enabled) {
|
||||
if (!enable_irq_wake(client->irq))
|
||||
@@ -421,8 +429,8 @@ int nfc_i2c_dev_suspend(struct device *device)
|
||||
int nfc_i2c_dev_resume(struct device *device)
|
||||
{
|
||||
struct i2c_client *client = to_i2c_client(device);
|
||||
nfc_dev_t *nfc_dev = i2c_get_clientdata(client);
|
||||
i2c_dev_t *i2c_dev = &nfc_dev->i2c_dev;
|
||||
struct nfc_dev *nfc_dev = i2c_get_clientdata(client);
|
||||
struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev;
|
||||
|
||||
if (device_may_wakeup(&client->dev) && i2c_dev->irq_wake_up) {
|
||||
if (!disable_irq_wake(client->irq))
|
||||
@@ -462,6 +470,7 @@ MODULE_DEVICE_TABLE(of, nfc_i2c_dev_match_table);
|
||||
static int __init nfc_i2c_dev_init(void)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
pr_info("Loading NXP NFC I2C driver\n");
|
||||
ret = i2c_add_driver(&nfc_i2c_dev_driver);
|
||||
if (ret != 0)
|
||||
|
在新工单中引用
屏蔽一个用户