drivers: nfc: Import PN8XT drivers from 'br_android_ncihalx_row_12'

* Relevant HALs, drivers can be found on NXP's GitHub:
   https://github.com/NXPNFCProject

Change-Id: I6fba39977083d59ceb8157e274fd14d66f663e13
This commit is contained in:
Alexander Koskovich 2022-03-20 12:07:03 -07:00 committed by Davide Garberi
parent 9051f67ca9
commit 26c9909bb1
20 changed files with 2340 additions and 0 deletions

View File

@ -61,6 +61,8 @@ source "drivers/nfc/s3fwrn5/Kconfig"
source "drivers/nfc/st95hf/Kconfig" source "drivers/nfc/st95hf/Kconfig"
endmenu endmenu
source "drivers/nfc/pn8xt/Kconfig"
config NFC_QTI_I2C config NFC_QTI_I2C
tristate "QTI NCI based NFC I2C Slave Driver for SNxxx" tristate "QTI NCI based NFC I2C Slave Driver for SNxxx"
depends on I2C depends on I2C

View File

@ -18,3 +18,4 @@ obj-$(CONFIG_NFC_NXP_NCI) += nxp-nci/
obj-$(CONFIG_NFC_S3FWRN5) += s3fwrn5/ obj-$(CONFIG_NFC_S3FWRN5) += s3fwrn5/
obj-$(CONFIG_NFC_ST95HF) += st95hf/ obj-$(CONFIG_NFC_ST95HF) += st95hf/
obj-y += qti/ obj-y += qti/
obj-y += pn8xt/

11
drivers/nfc/pn8xt/Kconfig Normal file
View File

@ -0,0 +1,11 @@
#
# Copyright (C) 2019 NXP
#
# SPDX-License-Identifier: GPL-2.0
#
config NXP_NFC_ESE_DEVICE
tristate "NXP NFC ESE Driver Controller"
source "drivers/nfc/pn8xt/ese/Kconfig"
source "drivers/nfc/pn8xt/nfc/Kconfig"

View File

@ -0,0 +1,8 @@
#
# Copyright (C) 2019 NXP
#
# SPDX-License-Identifier: GPL-2.0
#
obj-y += nfc/
obj-y += ese/

View File

@ -0,0 +1,21 @@
#
# Copyright (C) 2019 NXP
#
# SPDX-License-Identifier: GPL-2.0
#
config NXP_ESE_SN1XX
bool "Nxp ESE sn1xx Controller"
---help---
You'll have to say Y if your computer contains an sn1xx SPI device that
you want to use under Linux.
You can say N here if you don't have any sn1xx SPI connected to your computer.
config NXP_ESE_PN8XT
bool "Nxp ESE pn8xt Controller"
---help---
You'll have to say Y if your computer contains an pn8xt SPI device that
you want to use under Linux.
You can say N here if you don't have any pn8xt SPI connected to your computer.

View File

@ -0,0 +1,10 @@
#
# Copyright (C) 2019 NXP
#
# SPDX-License-Identifier: GPL-2.0
#
p73-objs = ese.o sn1xx.o pn8xt.o
obj-$(CONFIG_NXP_NFC_ESE_DEVICE) += p73.o
ccflags-$(CONFIG_NXP_ESE_SN1XX) := -DNFC_PLATFORM=sn1xx
ccflags-$(CONFIG_NXP_ESE_PN8XT) := -DNFC_PLATFORM=pn8xt

311
drivers/nfc/pn8xt/ese/ese.c Normal file
View File

@ -0,0 +1,311 @@
/*
* The original Work has been changed by NXP Semiconductors.
* Copyright 2013-2019 NXP
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only 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.
*/
/*
* Copyright 2017 Google Inc.
*
* 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.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/of_device.h>
#include <linux/spi/spi.h>
#include <linux/uaccess.h>
#include <linux/poll.h>
#include <linux/ioctl.h>
#include <linux/miscdevice.h>
#include <linux/i2c.h>
#include "../nfc/nfc.h"
#include "ese.h"
#include "sn1xx.h"
#include "pn8xt.h"
/*Compile time function calls based on the platform selection*/
#define platform_func(prefix, postfix) prefix##postfix
#define func(prefix, postfix) platform_func(prefix, postfix)
#define MAX_BUFFER_SIZE func(NFC_PLATFORM, _max_buffer_size)
static ssize_t ese_dev_read(struct file *filp, char __user *ubuf,
size_t len, loff_t *offset)
{
ssize_t ret = -EFAULT;
struct ese_dev *ese_dev = filp->private_data;
char rx_buf[MAX_BUFFER_SIZE];
mutex_lock(&ese_dev->mutex);
if (len > MAX_BUFFER_SIZE) {
len = MAX_BUFFER_SIZE;
}
pr_debug("%s: start reading of %zu bytes\n", __func__, len);
memset(rx_buf, 0, sizeof(rx_buf));
ret = spi_read(ese_dev->spi, rx_buf, len);
if (0 > ret) {
pr_err("%s failed to read from SPI\n", __func__);
mutex_unlock(&ese_dev->mutex);
return -EIO;
}
if (copy_to_user(ubuf, rx_buf, len)) {
pr_err("%s failed to copy from user\n", __func__);
mutex_unlock(&ese_dev->mutex);
return -EFAULT;
}
mutex_unlock(&ese_dev->mutex);
pr_debug("%s: Success in reading %zu bytes\n", __func__, len);
return ret;
}
static ssize_t ese_dev_write(struct file *filp, const char __user *ubuf,
size_t len, loff_t *offset)
{
ssize_t ret = -EFAULT;
struct ese_dev *ese_dev = filp->private_data;
char tx_buf[MAX_BUFFER_SIZE];
mutex_lock(&ese_dev->write_mutex);
if (len > MAX_BUFFER_SIZE)
len = MAX_BUFFER_SIZE;
pr_debug("%s: start writing of %zu bytes\n", __func__, len);
memset(tx_buf, 0, sizeof(tx_buf));
if (copy_from_user(tx_buf, ubuf, len)) {
pr_err("%s: failed to copy from user\n", __func__);
mutex_unlock(&ese_dev->write_mutex);
return -EFAULT;
}
ret = spi_write(ese_dev->spi, tx_buf, len);
if (ret < 0) {
pr_err("%s: failed to write to SPI\n", __func__);
mutex_unlock(&ese_dev->write_mutex);
return -EIO;
}
pr_debug("%s: Success in writing %zu bytes\n", __func__, len);
mutex_unlock(&ese_dev->write_mutex);
return len;
}
static long ese_dev_ioctl(struct file *filep, unsigned int cmd,
unsigned long arg)
{
long ret = 0;
struct ese_dev *ese_dev = filep->private_data;
ret = func(NFC_PLATFORM, _ese_dev_ioctl)(ese_dev, cmd, arg);
if (ret != 0)
pr_err("%s: ioctl: cmd = %u, arg = %lu\n", __func__, cmd, arg);
return ret;
}
static int ese_dev_open(struct inode *inode, struct file *filp)
{
struct ese_dev *ese_dev = container_of(filp->private_data,
struct ese_dev, device);
mutex_lock(&ese_dev->mutex);
/* Find the NFC parent device if it exists. */
if (ese_dev->nfcc_data == NULL) {
struct device *nfc_dev = bus_find_device_by_name(
&i2c_bus_type,
NULL,
ese_dev->nfcc_name);
if (!nfc_dev) {
pr_err("%s: cannot find NFC controller '%s'\n",
__func__, ese_dev->nfcc_name);
mutex_unlock(&ese_dev->mutex);
return -ENODEV;
}
ese_dev->nfcc_data = dev_get_drvdata(nfc_dev);
if (!ese_dev->nfcc_data) {
pr_err("%s: cannot find NFC controller device data\n",
__func__);
put_device(nfc_dev);
mutex_unlock(&ese_dev->mutex);
return -ENODEV;
}
pr_info("%s: NFC controller found\n", __func__);
ese_dev->nfcc_device = nfc_dev;
}
mutex_unlock(&ese_dev->mutex);
func(NFC_PLATFORM, _ese_dev_open)(ese_dev);
filp->private_data = ese_dev;
pr_info("%s: major,minor: %d,%d\n",
__func__, imajor(inode), iminor(inode));
return 0;
}
static int ese_dev_release(struct inode *inode, struct file *filp)
{
struct ese_dev *ese_dev = filp->private_data;
func(NFC_PLATFORM, _ese_dev_release)(ese_dev);
pr_info("%s: released\n", __func__);
return 0;
}
/* possible fops on the ese device */
static const struct file_operations ese_dev_fops = {
.owner = THIS_MODULE,
.read = ese_dev_read,
.write = ese_dev_write,
.open = ese_dev_open,
.release = ese_dev_release,
.unlocked_ioctl = ese_dev_ioctl,
};
static int ese_probe(struct spi_device *spi)
{
int ret;
unsigned int rst_gpio;
unsigned int max_speed_hz;
struct ese_dev *ese_dev;
struct device_node *np = dev_of_node(&spi->dev);
pr_debug("%s: called\n", __func__);
if (!np) {
pr_err("%s: device tree data missing\n", __func__);
return -EINVAL;
}
ese_dev = kzalloc(sizeof(*ese_dev), GFP_KERNEL);
if (ese_dev == NULL) {
pr_err("%s: No memory\n", __func__);
return -ENOMEM;
}
ese_dev->spi = spi;
ese_dev->device.minor = MISC_DYNAMIC_MINOR;
ese_dev->device.name = "p73";
ese_dev->device.fops = &ese_dev_fops;
ese_dev->device.parent = &spi->dev;
spi->bits_per_word = 8;
spi->mode = SPI_MODE_0;
ret = of_property_read_u32(np, "spi-max-frequency",
&max_speed_hz);
if (ret < 0) {
pr_err("%s: There's no spi-max-frequency property\n",
__func__);
goto err;
}
spi->max_speed_hz = max_speed_hz;
pr_info("%s: device tree set SPI clock Frequency %u\n",
__func__, spi->max_speed_hz);
ret = spi_setup(spi);
if (ret < 0) {
pr_err("%s: failed to do spi_setup()\n", __func__);
goto err;
}
rst_gpio = of_get_named_gpio(np, "nxp,p61-rst", 0);
if (gpio_is_valid(rst_gpio)) {
ret = gpio_request(rst_gpio, "p61 reset");
if (ret) {
pr_err("%s: unable to request rst gpio [%d]\n",
__func__, rst_gpio);
goto err;
}
/*soft reset gpio is set to default high*/
ret = gpio_direction_output(rst_gpio, 1);
if (ret) {
pr_err("%s: cannot set direction for rst gpio [%d]\n",
__func__, rst_gpio);
goto err;
}
} else {
/* rst gpio not required for latest platform*/
pr_info("%s: rst gpio not provided\n", __func__);
}
ret = of_property_read_string(np, "nxp,nfcc", &ese_dev->nfcc_name);
if (ret < 0) {
pr_err("%s: nxp,nfcc invalid or missing in device tree (%d)\n",
__func__, ret);
goto err;
}
pr_info("%s: device tree set '%s' as eSE power controller\n",
__func__, ese_dev->nfcc_name);
ret = misc_register(&ese_dev->device);
if (ret) {
pr_err("%s: misc_register failed\n", __func__);
goto err;
}
pr_info("%s: eSE is configured\n", __func__);
spi_set_drvdata(spi, ese_dev);
mutex_init(&ese_dev->mutex);
mutex_init(&ese_dev->write_mutex);
return 0;
err:
kfree(ese_dev);
return ret;
}
static int ese_remove(struct spi_device *spi)
{
struct ese_dev *ese_dev = spi_get_drvdata(spi);
int ret = 0;
if (!ese_dev) {
pr_err("%s: device doesn't exist anymore\n", __func__);
return -ENODEV;
}
/* If we have a NFC device, release it. */
if (ese_dev->nfcc_device) {
put_device(ese_dev->nfcc_device);
ese_dev->nfcc_data = NULL;
ese_dev->nfcc_device = NULL;
}
misc_deregister(&ese_dev->device);
mutex_destroy(&ese_dev->mutex);
mutex_destroy(&ese_dev->write_mutex);
kfree(ese_dev);
return ret;
}
static struct of_device_id ese_match_table[] = {
{ .compatible = "nxp,p61", },
{ }
};
MODULE_DEVICE_TABLE(of, ese_match_table);
static struct spi_driver ese_driver = {
.driver = {
.name = "p61",
.bus = &spi_bus_type,
.owner = THIS_MODULE,
.of_match_table = ese_match_table,
},
.probe = ese_probe,
.remove = ese_remove,
};
/*
* module load/unload record keeping
*/
static int __init ese_dev_init(void)
{
pr_info("Loading NXP ESE driver\n");
return spi_register_driver(&ese_driver);
}
module_init(ese_dev_init);
static void __exit ese_dev_exit(void)
{
pr_info("Unloading NXP ESE driver\n");
spi_unregister_driver(&ese_driver);
}
module_exit(ese_dev_exit);
MODULE_DESCRIPTION("NXP ESE SPI driver");
MODULE_AUTHOR("Google Inc");
MODULE_LICENSE("GPL");

View File

@ -0,0 +1,36 @@
/*
* The original Work has been changed by NXP Semiconductors.
* Copyright 2013-2019 NXP
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only 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.
*/
/*
* Copyright 2017 Google Inc.
*
* 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.
*/
#ifndef _NXP_ESE_H_
#define _NXP_ESE_H_
#define NXP_ESE_MAGIC 0xEA
/* Device specific structure */
struct ese_dev {
struct spi_device *spi;
struct device *nfcc_device;
struct nfc_dev *nfcc_data;
struct mutex mutex;
struct mutex write_mutex; /* write mutex */
struct miscdevice device;
const char *nfcc_name;
};
#endif //_NXP_ESE_H_

View File

@ -0,0 +1,76 @@
/*
* Copyright 2013-2019 NXP
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only 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.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/of_device.h>
#include <linux/spi/spi.h>
#include <linux/uaccess.h>
#include <linux/poll.h>
#include <linux/ioctl.h>
#include <linux/miscdevice.h>
#include <linux/i2c.h>
#include "../nfc/nfc.h"
#include "../nfc/pn8xt.h"
#include "ese.h"
#include "pn8xt.h"
extern long pn8xt_nfc_ese_ioctl(struct nfc_dev *nfc_dev, unsigned int cmd, unsigned long arg);
long pn8xt_ese_dev_ioctl(struct ese_dev *ese_dev, unsigned int cmd, unsigned long arg)
{
long ret = 0;
pr_debug("%s :enter cmd = %u, arg = %ld\n", __func__, cmd, arg);
switch (cmd) {
case PN8XT_ESE_SET_SPI_PWR:
ret = pn8xt_nfc_ese_ioctl(ese_dev->nfcc_data, PN8XT_SET_SPI_PWR, arg);
break;
case PN8XT_ESE_GET_SPI_STATUS:
ret = pn8xt_nfc_ese_ioctl(ese_dev->nfcc_data, PN8XT_GET_PWR_STATUS, arg);
break;
case PN8XT_ESE_GET_ACCESS:
ret = pn8xt_nfc_ese_ioctl(ese_dev->nfcc_data, PN8XT_GET_ESE_ACCESS, arg);
break;
case PN8XT_ESE_SET_PWR_SCM:
ret = pn8xt_nfc_ese_ioctl(ese_dev->nfcc_data, PN8XT_SET_POWER_SCM, arg);
break;
case PN8XT_ESE_SET_DN_STATUS:
ret = pn8xt_nfc_ese_ioctl(ese_dev->nfcc_data, PN8XT_SET_DN_STATUS, arg);
break;
case PN8XT_ESE_INHIBIT_PWR_CNTRL:
ret = pn8xt_nfc_ese_ioctl(ese_dev->nfcc_data, PN8XT_SECURE_TIMER_SESSION, arg);
break;
default:
ret = -ENOIOCTLCMD;
pr_err("%s: bad ioctl: cmd = %u, arg = %lu\n", __func__, cmd, arg);
}
pr_debug("%s :exit cmd = %u, arg = %ld\n", __func__, cmd, arg);
return ret;
}
void pn8xt_ese_dev_open(struct ese_dev *ese_dev)
{
pr_info("%s: called\n", __func__);
}
void pn8xt_ese_dev_release(struct ese_dev *ese_dev)
{
pr_info("%s: called\n", __func__);
}

View File

@ -0,0 +1,36 @@
/*
* Copyright 2013-2019 NXP
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only 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.
*/
#ifndef _NXP_ESE_PN8XT_H_
#define _NXP_ESE_PN8XT_H_
#define pn8xt_max_buffer_size 258U
/* SPI Request NFCC to enable pn8xt ese power,
* Only for SPI
* level 1 = Enable power
* level 0 = Disable power
*/
#define PN8XT_ESE_SET_SPI_PWR _IOW(NXP_ESE_MAGIC, 0x04, long)
/* SPI or DWP can call this ioctl to get the current
* power state of P61
*/
#define PN8XT_ESE_GET_SPI_STATUS _IOR(NXP_ESE_MAGIC, 0x05, long)
#define PN8XT_ESE_GET_ACCESS _IOW(NXP_ESE_MAGIC, 0x07, long)
#define PN8XT_ESE_SET_PWR_SCM _IOW(NXP_ESE_MAGIC, 0x08, long)
#define PN8XT_ESE_SET_DN_STATUS _IOW(NXP_ESE_MAGIC, 0x09, long)
#define PN8XT_ESE_INHIBIT_PWR_CNTRL _IOW(NXP_ESE_MAGIC, 0x0A, long)
long pn8xt_ese_dev_ioctl(struct ese_dev *ese_dev, unsigned int cmd, unsigned long arg);
void pn8xt_ese_dev_open(struct ese_dev *ese_dev);
void pn8xt_ese_dev_release(struct ese_dev *ese_dev);
#endif //_NXP_ESE_PN8XT_H_

View File

@ -0,0 +1,62 @@
/*
* Copyright 2013-2019 NXP
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only 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.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/of_device.h>
#include <linux/spi/spi.h>
#include <linux/uaccess.h>
#include <linux/poll.h>
#include <linux/ioctl.h>
#include <linux/miscdevice.h>
#include <linux/i2c.h>
#include "../nfc/nfc.h"
#include "../nfc/sn1xx.h"
#include "ese.h"
#include "sn1xx.h"
extern long sn1xx_nfc_ese_ioctl(struct nfc_dev *nfc_dev, unsigned int cmd, unsigned long arg);
long sn1xx_ese_dev_ioctl(struct ese_dev *ese_dev, unsigned int cmd, unsigned long arg)
{
long ret = 0;
mutex_lock(&ese_dev->mutex);
switch (cmd) {
case SN1XX_ESE_PERFORM_COLD_RESET:
ret = sn1xx_nfc_ese_ioctl(ese_dev->nfcc_data, cmd, arg);
break;
default:
ret = -ENOIOCTLCMD;
pr_err("%s: bad ioctl: cmd = %u, arg = %lu\n", __func__, cmd, arg);
}
mutex_unlock(&ese_dev->mutex);
return ret;
}
void sn1xx_ese_dev_open(struct ese_dev *ese_dev)
{
nfc_ese_acquire(ese_dev->nfcc_data);
pr_info("%s: acquired\n", __func__);
}
void sn1xx_ese_dev_release(struct ese_dev *ese_dev)
{
nfc_ese_release(ese_dev->nfcc_data);
pr_info("%s: released\n", __func__);
}

View File

@ -0,0 +1,23 @@
/*
* Copyright 2013-2019 NXP
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only 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.
*/
#ifndef _NXP_ESE_SN1XX_H_
#define _NXP_ESE_SN1XX_H_
#define sn1xx_max_buffer_size 780U
#define SN1XX_ESE_PERFORM_COLD_RESET _IOW(NXP_ESE_MAGIC, 0x0C, long)
long sn1xx_ese_dev_ioctl(struct ese_dev *ese_dev, unsigned int cmd, unsigned long arg);
void sn1xx_ese_dev_open(struct ese_dev *ese_dev);
void sn1xx_ese_dev_release(struct ese_dev *ese_dev);
#endif //_NXP_ESE_SN1XX_H_

View File

@ -0,0 +1,21 @@
#
# Copyright (C) 2019 NXP
#
# SPDX-License-Identifier: GPL-2.0
#
config NXP_NFC_SN1XX
bool "Nxp NFC sn1xx Controller"
---help---
You'll have to say Y if your computer contains an sn1xx I2C device that
you want to use under Linux.
You can say N here if you don't have any sn1xx I2C connected to your computer.
config NXP_NFC_PN8XT
bool "Nxp NFC pn8xt Controller"
---help---
You'll have to say Y if your computer contains an pn8xt I2C device that
you want to use under Linux.
You can say N here if you don't have any pn8xt I2C connected to your computer.

View File

@ -0,0 +1,10 @@
#
# Copyright (C) 2019 NXP
#
# SPDX-License-Identifier: GPL-2.0
#
pn553-objs = nfc.o sn1xx.o pn8xt.o
obj-$(CONFIG_NXP_NFC_ESE_DEVICE) += pn553.o
ccflags-$(CONFIG_NXP_NFC_SN1XX) := -DNFC_PLATFORM=sn1xx
ccflags-$(CONFIG_NXP_NFC_PN8XT) := -DNFC_PLATFORM=pn8xt

509
drivers/nfc/pn8xt/nfc/nfc.c Normal file
View File

@ -0,0 +1,509 @@
/*
* The original Work has been changed by NXP Semiconductors.
* Copyright 2013-2019 NXP
*
* Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only 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.
*/
/*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/spinlock.h>
#include <linux/of_gpio.h>
#include <linux/of_device.h>
#include <linux/uaccess.h>
#include <linux/ioctl.h>
#include <linux/miscdevice.h>
#include <linux/i2c.h>
#include "nfc.h"
#include "sn1xx.h"
#include "pn8xt.h"
#define MAX_BUFFER_SIZE (512)
#define WAKEUP_SRC_TIMEOUT (2000)
#define MAX_RETRY_COUNT 3
#define MAX_SECURE_SESSIONS 1
/*Compile time function calls based on the platform selection*/
#define platform_func(prefix, postfix) prefix##postfix
#define func(prefix, postfix) platform_func(prefix, postfix)
void nfc_disable_irq(struct nfc_dev *nfc_dev)
{
unsigned long flags;
spin_lock_irqsave(&nfc_dev->irq_enabled_lock, flags);
if (nfc_dev->irq_enabled) {
disable_irq_nosync(nfc_dev->client->irq);
nfc_dev->irq_enabled = false;
}
spin_unlock_irqrestore(&nfc_dev->irq_enabled_lock, flags);
}
void nfc_enable_irq(struct nfc_dev *nfc_dev)
{
unsigned long flags;
spin_lock_irqsave(&nfc_dev->irq_enabled_lock, flags);
if (!nfc_dev->irq_enabled) {
nfc_dev->irq_enabled = true;
enable_irq(nfc_dev->client->irq);
}
spin_unlock_irqrestore(&nfc_dev->irq_enabled_lock, flags);
}
static irqreturn_t nfc_dev_irq_handler(int irq, void *dev_id)
{
struct nfc_dev *nfc_dev = dev_id;
unsigned long flags;
nfc_disable_irq(nfc_dev);
spin_lock_irqsave(&nfc_dev->irq_enabled_lock, flags);
nfc_dev->count_irq++;
spin_unlock_irqrestore(&nfc_dev->irq_enabled_lock, flags);
wake_up(&nfc_dev->read_wq);
return IRQ_HANDLED;
}
static ssize_t nfc_dev_read(struct file *filp, char __user *buf,
size_t count, loff_t *offset)
{
struct nfc_dev *nfc_dev = filp->private_data;
char tmp[MAX_BUFFER_SIZE];
int ret;
int irq_gpio_val = 0;
if (!nfc_dev) {
return -ENODEV;
}
if (count > MAX_BUFFER_SIZE)
count = MAX_BUFFER_SIZE;
pr_debug("%s: start reading of %zu bytes\n", __func__, count);
mutex_lock(&nfc_dev->read_mutex);
irq_gpio_val = gpio_get_value(nfc_dev->irq_gpio);
if (irq_gpio_val == 0) {
if (filp->f_flags & O_NONBLOCK) {
dev_err(&nfc_dev->client->dev,
":f_flags has O_NONBLOCK. EAGAIN\n");
ret = -EAGAIN;
goto err;
}
while (1) {
ret = 0;
if (!nfc_dev->irq_enabled) {
nfc_dev->irq_enabled = true;
enable_irq(nfc_dev->client->irq);
}
if (!gpio_get_value(nfc_dev->irq_gpio)) {
ret = wait_event_interruptible(nfc_dev->read_wq,
!nfc_dev->irq_enabled);
}
if (ret)
goto err;
nfc_disable_irq(nfc_dev);
if (gpio_get_value(nfc_dev->irq_gpio))
break;
pr_warning("%s: spurious interrupt detected\n", __func__);
}
}
memset(tmp, 0x00, count);
/* Read data */
ret = i2c_master_recv(nfc_dev->client, tmp, count);
mutex_unlock(&nfc_dev->read_mutex);
/* delay of 1ms for slow devices*/
udelay(1000);
if (ret < 0) {
pr_err("%s: i2c_master_recv returned %d\n", __func__, ret);
goto err;
}
if (ret > count) {
pr_err("%s: received too many bytes from i2c (%d)\n",
__func__, ret);
ret = -EIO;
goto err;
}
if (copy_to_user(buf, tmp, ret)) {
pr_warning("%s : failed to copy to user space\n", __func__);
ret = -EFAULT;
goto err;
}
pr_debug("%s: Success in reading %zu bytes\n", __func__, count);
return ret;
err:
mutex_unlock(&nfc_dev->read_mutex);
return ret;
}
static ssize_t nfc_dev_write(struct file *filp, const char __user *buf,
size_t count, loff_t *offset)
{
struct nfc_dev *nfc_dev = filp->private_data;
char tmp[MAX_BUFFER_SIZE];
int ret = 0;
if (!nfc_dev) {
return -ENODEV;
}
if (count > MAX_BUFFER_SIZE) {
count = MAX_BUFFER_SIZE;
}
pr_debug("%s: start writing of %zu bytes\n", __func__, count);
if (copy_from_user(tmp, buf, count)) {
pr_err("%s : failed to copy from user space\n", __func__);
return -EFAULT;
}
ret = i2c_master_send(nfc_dev->client, tmp, count);
if (ret != count) {
pr_err("%s: i2c_master_send returned %d\n", __func__, ret);
ret = -EIO;
}
pr_debug("%s: Success in writing %zu bytes\n", __func__, count);
/* delay of 1ms for slow devices*/
udelay(1000);
return ret;
}
/* Callback to claim the embedded secure element
* It is a blocking call, in order to protect the ese
* from being reset from outside when it is in use.
*/
void nfc_ese_acquire(struct nfc_dev *nfc_dev)
{
mutex_lock(&nfc_dev->ese_status_mutex);
pr_debug("%s: ese acquired\n", __func__);
}
/* Callback to release the embedded secure element
* it should be released, after completion of any
* operation (usage or reset) of ese.
*/
void nfc_ese_release(struct nfc_dev *nfc_dev)
{
mutex_unlock(&nfc_dev->ese_status_mutex);
pr_debug("%s: ese released\n", __func__);
}
static int nfc_dev_open(struct inode *inode, struct file *filp)
{
int ret = 0;
struct nfc_dev *nfc_dev = container_of(filp->private_data,
struct nfc_dev, nfc_device);
filp->private_data = nfc_dev;
pr_info("%s: %d,%d\n", __func__, imajor(inode), iminor(inode));
return ret;
}
long nfc_dev_ioctl(struct file *filep, unsigned int cmd,
unsigned long arg)
{
long ret = 0;
struct nfc_dev *nfc_dev = filep->private_data;
ret = func(NFC_PLATFORM, _nfc_ioctl)(nfc_dev, cmd, arg);
if (ret != 0)
pr_err("%s: ioctl: cmd = %u, arg = %lu\n", __func__, cmd, arg);
return ret;
}
static const struct file_operations nfc_dev_fops = {
.owner = THIS_MODULE,
.llseek = no_llseek,
.read = nfc_dev_read,
.write = nfc_dev_write,
.open = nfc_dev_open,
.unlocked_ioctl = nfc_dev_ioctl,
};
struct nfc_platform_data {
unsigned int irq_gpio;
unsigned int ven_gpio;
unsigned int firm_gpio;
unsigned int ese_pwr_gpio;
};
static int nfc_parse_dt(struct device *dev,
struct nfc_platform_data *data)
{
int ret = 0;
struct device_node *np = dev->of_node;
data->irq_gpio = of_get_named_gpio(np, "nxp,pn544-irq", 0);
if ((!gpio_is_valid(data->irq_gpio)))
return -EINVAL;
data->ven_gpio = of_get_named_gpio(np, "nxp,pn544-ven", 0);
if ((!gpio_is_valid(data->ven_gpio)))
return -EINVAL;
data->firm_gpio = of_get_named_gpio(np, "nxp,pn544-fw-dwnld", 0);
if ((!gpio_is_valid(data->firm_gpio)))
return -EINVAL;
//required for old platform only
data->ese_pwr_gpio = of_get_named_gpio(np, "nxp,pn544-ese-pwr", 0);
if ((!gpio_is_valid(data->ese_pwr_gpio)))
data->ese_pwr_gpio = -EINVAL;
pr_info("%s: %d, %d, %d, %d, error:%d\n", __func__,
data->irq_gpio, data->ven_gpio, data->firm_gpio,
data->ese_pwr_gpio, ret);
return ret;
}
static int nfc_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
int ret = 0;
int irqn = 0;
struct nfc_platform_data platform_data;
struct nfc_dev *nfc_dev;
pr_debug("%s: enter\n", __func__);
ret = nfc_parse_dt(&client->dev, &platform_data);
if (ret) {
pr_err("%s : failed to parse\n", __func__);
goto err;
}
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
pr_err("%s : need I2C_FUNC_I2C\n", __func__);
ret = -ENODEV;
goto err;
}
nfc_dev = kzalloc(sizeof(*nfc_dev), GFP_KERNEL);
if (nfc_dev == NULL) {
ret = -ENOMEM;
goto err;
}
nfc_dev->client = client;
if (gpio_is_valid(platform_data.ven_gpio)) {
ret = gpio_request(platform_data.ven_gpio, "nfc_reset_gpio");
if (ret) {
pr_err("%s: unable to request nfc reset gpio [%d]\n",
__func__, platform_data.ven_gpio);
goto err_mem;
}
ret = gpio_direction_output(platform_data.ven_gpio, 0);
if (ret) {
pr_err("%s: unable to set direction for nfc reset gpio [%d]\n",
__func__, platform_data.ven_gpio);
goto err_en_gpio;
}
} else {
pr_err("%s: nfc reset gpio not provided\n", __func__);
goto err_mem;
}
if (gpio_is_valid(platform_data.irq_gpio)) {
ret = gpio_request(platform_data.irq_gpio, "nfc_irq_gpio");
if (ret) {
pr_err("%s: unable to request nfc irq gpio [%d]\n",
__func__, platform_data.irq_gpio);
goto err_en_gpio;
}
ret = gpio_direction_input(platform_data.irq_gpio);
if (ret) {
pr_err("%s: unable to set direction for nfc irq gpio [%d]\n",
__func__, platform_data.irq_gpio);
goto err_irq_gpio;
}
irqn = gpio_to_irq(platform_data.irq_gpio);
if (irqn < 0) {
ret = irqn;
goto err_irq_gpio;
}
client->irq = irqn;
} else {
pr_err("%s: irq gpio not provided\n", __func__);
goto err_en_gpio;
}
if (gpio_is_valid(platform_data.firm_gpio)) {
ret = gpio_request(platform_data.firm_gpio, "nfc_firm_gpio");
if (ret) {
pr_err("%s: unable to request nfc firmware gpio [%d]\n",
__func__, platform_data.firm_gpio);
goto err_irq_gpio;
}
ret = gpio_direction_output(platform_data.firm_gpio, 0);
if (ret) {
pr_err("%s: cannot set direction for nfc firmware gpio [%d]\n",
__func__, platform_data.firm_gpio);
goto err_firm_gpio;
}
} else {
pr_err("%s: firm gpio not provided\n", __func__);
goto err_irq_gpio;
}
if (gpio_is_valid(platform_data.ese_pwr_gpio)) {
ret = gpio_request(platform_data.ese_pwr_gpio, "nfc-ese_pwr");
if (ret) {
pr_err("%s: unable to request nfc ese gpio [%d]\n",
__func__, platform_data.ese_pwr_gpio);
goto err_firm_gpio;
}
ret = gpio_direction_output(platform_data.ese_pwr_gpio, 0);
if (ret) {
pr_err("%s: cannot set direction for nfc ese gpio [%d]\n",
__func__, platform_data.ese_pwr_gpio);
goto err_ese_pwr_gpio;
}
} else {
/* ese gpio not required for latest platform*/
pr_info("%s: ese pwr gpio not provided\n", __func__);
}
nfc_dev->ven_gpio = platform_data.ven_gpio;
nfc_dev->irq_gpio = platform_data.irq_gpio;
nfc_dev->firm_gpio = platform_data.firm_gpio;
nfc_dev->ese_pwr_gpio = platform_data.ese_pwr_gpio;
/* init mutex and queues */
init_waitqueue_head(&nfc_dev->read_wq);
mutex_init(&nfc_dev->read_mutex);
mutex_init(&nfc_dev->ese_status_mutex);
spin_lock_init(&nfc_dev->irq_enabled_lock);
nfc_dev->nfc_device.minor = MISC_DYNAMIC_MINOR;
nfc_dev->nfc_device.name = "pn553";
nfc_dev->nfc_device.fops = &nfc_dev_fops;
ret = misc_register(&nfc_dev->nfc_device);
if (ret) {
pr_err("%s: misc_register failed\n", __func__);
goto err_misc_register;
}
/* NFC_INT IRQ */
nfc_dev->irq_enabled = true;
ret = request_irq(client->irq, nfc_dev_irq_handler,
IRQF_TRIGGER_HIGH, client->name, nfc_dev);
if (ret) {
pr_err("request_irq failed\n");
goto err_request_irq_failed;
}
device_init_wakeup(&client->dev, true);
device_set_wakeup_capable(&client->dev, true);
i2c_set_clientdata(client, nfc_dev);
/*Enable IRQ and VEN*/
nfc_enable_irq(nfc_dev);
/*call to platform specific probe*/
ret = func(NFC_PLATFORM, _nfc_probe)(nfc_dev);
if (ret != 0) {
pr_err("%s: probing platform failed\n", __func__);
goto err_request_irq_failed;
};
pr_info("%s: probing NXP NFC exited successfully\n", __func__);
return 0;
err_request_irq_failed:
misc_deregister(&nfc_dev->nfc_device);
err_misc_register:
mutex_destroy(&nfc_dev->read_mutex);
mutex_destroy(&nfc_dev->ese_status_mutex);
err_ese_pwr_gpio:
gpio_free(platform_data.ese_pwr_gpio);
err_firm_gpio:
gpio_free(platform_data.firm_gpio);
err_irq_gpio:
gpio_free(platform_data.irq_gpio);
err_en_gpio:
gpio_free(platform_data.ven_gpio);
err_mem:
kfree(nfc_dev);
err:
pr_err("%s: probing NXP NFC driver failed, check hardware\n", __func__);
return ret;
}
static int nfc_remove(struct i2c_client *client)
{
int ret = 0;
struct nfc_dev *nfc_dev;
pr_info("%s: remove device\n", __func__);
nfc_dev = i2c_get_clientdata(client);
if (!nfc_dev) {
pr_err("%s: device doesn't exist anymore\n", __func__);
ret = -ENODEV;
goto err;
}
/*call to platform specific remove*/
ret = func(NFC_PLATFORM, _nfc_remove)(nfc_dev);
if (ret != 0) {
pr_err("%s: platform failed\n", __func__);
goto err;
}
free_irq(client->irq, nfc_dev);
misc_deregister(&nfc_dev->nfc_device);
mutex_destroy(&nfc_dev->read_mutex);
mutex_destroy(&nfc_dev->ese_status_mutex);
gpio_free(nfc_dev->ese_pwr_gpio);
gpio_free(nfc_dev->firm_gpio);
gpio_free(nfc_dev->irq_gpio);
gpio_free(nfc_dev->ven_gpio);
kfree(nfc_dev);
err:
return ret;
}
static const struct i2c_device_id nfc_id[] = {
{ "pn544", 0 },
{ }
};
static struct of_device_id nfc_match_table[] = {
{.compatible = "nxp,pn544",},
{}
};
MODULE_DEVICE_TABLE(of, nfc_match_table);
static struct i2c_driver nfc_driver = {
.id_table = nfc_id,
.probe = nfc_probe,
.remove = nfc_remove,
.driver = {
.owner = THIS_MODULE,
.name = "pn544",
.of_match_table = nfc_match_table,
},
};
static int __init nfc_dev_init(void)
{
pr_info("Loading NXP NFC driver\n");
return i2c_add_driver(&nfc_driver);
}
module_init(nfc_dev_init);
static void __exit nfc_dev_exit(void)
{
pr_info("Unloading NXP NFC driver\n");
i2c_del_driver(&nfc_driver);
}
module_exit(nfc_dev_exit);
MODULE_DESCRIPTION("NXP NFC driver");
MODULE_LICENSE("GPL");

View File

@ -0,0 +1,61 @@
/*
* The original Work has been changed by NXP Semiconductors.
* Copyright 2013-2019 NXP
*
* Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only 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.
*/
/*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _NXP_NFC_H_
#define _NXP_NFC_H_
#define NXP_NFC_MAGIC 0xE9
/* Device specific structure */
struct nfc_dev {
wait_queue_head_t read_wq;
struct mutex read_mutex;
struct mutex ese_status_mutex;
struct i2c_client *client;
struct miscdevice nfc_device;
/* NFC GPIO variables */
unsigned int irq_gpio;
unsigned int ven_gpio;
unsigned int firm_gpio;
unsigned int ese_pwr_gpio;
/* NFC_IRQ state */
bool irq_enabled;
spinlock_t irq_enabled_lock;
unsigned int count_irq;
/* NFC additional parameters for old platforms */
void *pdata_op;
};
void nfc_disable_irq(struct nfc_dev *nfc_dev);
void nfc_enable_irq(struct nfc_dev *nfc_dev);
void nfc_ese_acquire(struct nfc_dev *nfc_dev);
void nfc_ese_release(struct nfc_dev *nfc_dev);
#endif //_NXP_NFC_H_

View File

@ -0,0 +1,801 @@
/*
* 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.
*
*/
/******************************************************************************
*
* The original Work has been changed by NXP.
*
* Copyright 2013-2020 NXP
*
* 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.
*
******************************************************************************/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/spinlock.h>
#include <linux/of_gpio.h>
#include <linux/of_device.h>
#include <linux/uaccess.h>
#include <linux/ioctl.h>
#include <linux/miscdevice.h>
#include <linux/i2c.h>
#include <linux/sched.h>
#include <linux/sched/signal.h>
#include <linux/workqueue.h>
#include <linux/timer.h>
#include "nfc.h"
#include "pn8xt.h"
#define SIG_NFC 44
struct pn8xt_dev {
pn8xt_access_st_t cur_state;
pn8xt_pwr_scm_t pwr_scheme;
bool nfc_ven_enabled;
bool spi_ven_enabled;
long service_pid;
struct semaphore ese_access_sema;
struct semaphore svdd_onoff_sema;
struct semaphore dwp_onoff_sema;
struct semaphore dwp_complete_sema;
unsigned long dwpLinkUpdateStat;
unsigned int secure_timer_cnt;
struct timer_list secure_timer;
struct workqueue_struct *pSecureTimerCbWq;
struct nfc_dev *nfc_dev;
struct work_struct wq_task;
};
static pn8xt_access_st_t *pn8xt_get_state(struct pn8xt_dev *pn8xt_dev)
{
return &pn8xt_dev->cur_state;
}
static void pn8xt_update_state(struct pn8xt_dev *pn8xt_dev, pn8xt_access_st_t state, bool set)
{
if (state) {
if(set) {
if(pn8xt_dev->cur_state == ST_IDLE)
pn8xt_dev->cur_state = ST_INVALID;
pn8xt_dev->cur_state |= state;
} else {
pn8xt_dev->cur_state ^= state;
if(!pn8xt_dev->cur_state)
pn8xt_dev->cur_state = ST_IDLE;
}
}
}
int get_ese_lock(struct pn8xt_dev *pn8xt_dev, int timeout)
{
unsigned long tempJ = msecs_to_jiffies(timeout);
if(down_timeout(&pn8xt_dev->ese_access_sema, tempJ) != 0) {
printk("get_ese_lock: timeout cur_state = %d\n", pn8xt_dev->cur_state);
return -EBUSY;
}
return 0;
}
static int signal_handler(pn8xt_access_st_t state, long nfc_pid)
{
int sigret = 0;
pid_t pid;
struct siginfo sinfo;
struct task_struct *task;
if(nfc_pid <= 0) {
pr_err("%s: invalid nfc service pid %ld\n", __func__, nfc_pid);
return 0;
}
memset(&sinfo, 0, sizeof(struct siginfo));
sinfo.si_signo = SIG_NFC;
sinfo.si_code = SI_QUEUE;
sinfo.si_int = state;
pid = nfc_pid;
task = pid_task(find_vpid(pid), PIDTYPE_PID);
if(task) {
pr_info("%s: %s\n", __func__, task->comm);
sigret = force_sig_info(SIG_NFC, &sinfo, task);
if(sigret < 0) {
pr_err("%s: send_sig_info failed, sigret %d\n", __func__, sigret);
return -1;
}
} else {
pr_err("%s: finding task from PID failed\n", __func__);
return -1;
}
pr_debug("%s: return successfully\n", __func__);
return 0;
}
static int trigger_onoff(struct pn8xt_dev *pn8xt_dev, pn8xt_access_st_t state)
{
int timeout = 4500; //timeout in ms
unsigned long timeoutInJiffies = msecs_to_jiffies(timeout);
struct semaphore *sema;
pr_debug("%s: nfc service_pid: %ld\n", __func__, pn8xt_dev->service_pid);
if(pn8xt_dev->service_pid <= 0) {
pr_err("%s:Invalid nfc service_pid %ld\n", __func__, pn8xt_dev->service_pid);
return 0;
}
if((state & ST_SPI_SVDD_SY_START) || (state & ST_SPI_SVDD_SY_END)) {
sema = &pn8xt_dev->svdd_onoff_sema;
} else {
sema = &pn8xt_dev->dwp_onoff_sema;
}
sema_init(sema, 0);
if (0 == signal_handler(state, pn8xt_dev->service_pid)) {
pr_debug("%s: Waiting for protection response", __func__);
if(down_timeout(sema, timeoutInJiffies) != 0) {
pr_err("%s: protection: Timeout", __func__);
return -1;
}
msleep(10);
pr_debug("%s: wait protection released", __func__);
}
return (pn8xt_dev->dwpLinkUpdateStat == 0x00) ? 0 : -1;
}
/*
* pn8xt_nfc_pwr() - power control/firmware download
* @filp: pointer to the file descriptor
* @arg: mode that we want to move to
*
* Device power control. Depending on the arg value, device moves to
* different states
* (arg = 0):FW_DL GPIO = 0, VEN GPIO = 0
* (arg = 1):FW_DL GPIO = 0, VEN GPIO = 1
* (arg = 2):FW_DL GPIO = 1, KEEP VEN down/up - firmware download mode
* Return: 0 on success and error on failure
*/
static long pn8xt_nfc_pwr(struct nfc_dev *nfc_dev, unsigned long arg)
{
struct pn8xt_dev *pn8xt_dev = (struct pn8xt_dev *)nfc_dev->pdata_op;
pn8xt_access_st_t *cur_state = pn8xt_get_state(pn8xt_dev);
if (!pn8xt_dev) {
pr_err("%s: pn8xt_dev doesn't exist anymore\n", __func__);
return -ENODEV;
}
switch(arg) {
case 0:
/* power off */
pr_debug("%s power off\n", __func__);
if (nfc_dev->firm_gpio) {
if ((*cur_state & (ST_WIRED | ST_SPI | ST_SPI_PRIO))== 0){
pn8xt_update_state(pn8xt_dev, ST_IDLE, true);
}
gpio_set_value(nfc_dev->firm_gpio, 0);
}
pn8xt_dev->nfc_ven_enabled = false;
/* Don't change Ven state if spi made it high */
if ((pn8xt_dev->spi_ven_enabled == false && !(pn8xt_dev->secure_timer_cnt))
|| (pn8xt_dev->pwr_scheme == PN80T_EXT_PMU_SCM)) {
gpio_set_value(nfc_dev->ven_gpio, 0);
}
break;
case 1:
/* power on */
pr_debug("%s power on\n", __func__);
if (nfc_dev->firm_gpio) {
if ((*cur_state & (ST_WIRED|ST_SPI|ST_SPI_PRIO))== 0){
pn8xt_update_state(pn8xt_dev, ST_IDLE, true);
}
if(*cur_state & ST_DN){
pn8xt_update_state(pn8xt_dev, ST_DN, false);
}
gpio_set_value(nfc_dev->firm_gpio, 0);
}
pn8xt_dev->nfc_ven_enabled = true;
if (pn8xt_dev->spi_ven_enabled == false || (pn8xt_dev->pwr_scheme == PN80T_EXT_PMU_SCM)) {
gpio_set_value(nfc_dev->ven_gpio, 1);
}
break;
case 2:
if(*cur_state & (ST_SPI|ST_SPI_PRIO) && (pn8xt_dev->pwr_scheme != PN80T_EXT_PMU_SCM)) {
/* NFCC fw/download should not be allowed when SPI is being used*/
pr_err("%s NFCC should not be allowed to reset/FW download \n", __func__);
return -EBUSY; /* Device or resource busy */
}
pn8xt_dev->nfc_ven_enabled = true;
if ((pn8xt_dev->spi_ven_enabled == false && !(pn8xt_dev->secure_timer_cnt))
|| (pn8xt_dev->pwr_scheme == PN80T_EXT_PMU_SCM))
{
/* power on with firmware download (requires hw reset)
*/
pr_debug("%s power on with firmware\n", __func__);
gpio_set_value(nfc_dev->ven_gpio, 1);
msleep(10);
if (nfc_dev->firm_gpio) {
pn8xt_update_state(pn8xt_dev, ST_DN, true);
gpio_set_value(nfc_dev->firm_gpio, 1);
}
msleep(10);
gpio_set_value(nfc_dev->ven_gpio, 0);
msleep(10);
gpio_set_value(nfc_dev->ven_gpio, 1);
msleep(10);
}
break;
case 3:
if(*cur_state & (ST_SPI|ST_SPI_PRIO)) {
return -EPERM; /* Operation not permitted */
}
if(*cur_state & ST_WIRED) {
pn8xt_update_state(pn8xt_dev, ST_WIRED, false);
}
break;
case 4:
pr_debug("%s FW dwld ioctl called from NFC \n", __func__);
/*NFC Service called FW dwnld*/
if (nfc_dev->firm_gpio) {
pn8xt_update_state(pn8xt_dev, ST_DN, true);
gpio_set_value(nfc_dev->firm_gpio, 1);
msleep(10);
}
break;
default:
pr_err("%s bad arg %lu\n", __func__, arg);
return -EINVAL;
};
return 0;
}
static long pn8xt_ese_pwr(struct nfc_dev *nfc_dev, unsigned int cmd, unsigned long arg)
{
bool isSignalTriggerReqd = !(arg & 0x10);/*5th bit to/not trigger signal*/
unsigned long pwrLevel = arg & 0x0F;
struct pn8xt_dev *pn8xt_dev = (struct pn8xt_dev *)nfc_dev->pdata_op;
pn8xt_access_st_t *cur_state = pn8xt_get_state(pn8xt_dev);
if (!pn8xt_dev) {
pr_err("%s: pn8xt_dev doesn't exist anymore\n", __func__);
return -ENODEV;
}
switch(pwrLevel) {
case 0:
pr_debug("%s: power off ese\n", __func__);
if(*cur_state & ST_SPI_PRIO){
pn8xt_update_state(pn8xt_dev, ST_SPI_PRIO, false);
if (!(*cur_state & ST_JCP_DN)) {
if(!(*cur_state & ST_WIRED)) {
trigger_onoff(pn8xt_dev, ST_SPI_SVDD_SY_START | ST_SPI_PRIO_END);
} else {
signal_handler(ST_SPI_PRIO_END, pn8xt_dev->service_pid);
}
} else if (!(*cur_state & ST_WIRED)) {
trigger_onoff(pn8xt_dev, ST_SPI_SVDD_SY_START);
}
pn8xt_dev->spi_ven_enabled = false;
if(pn8xt_dev->pwr_scheme == PN80T_EXT_PMU_SCM)
break;
/*if secure timer is running, Delay the SPI by 25ms after sending End of Apdu
to enable eSE go into DPD gracefully(20ms after EOS+5ms DPD settlement time)*/
if(pn8xt_dev->secure_timer_cnt)
usleep_range(25000, 30000);
if (!(*cur_state & ST_WIRED) && !(pn8xt_dev->secure_timer_cnt)) {
gpio_set_value(nfc_dev->ese_pwr_gpio, 0);
/* Delay (2.5ms) after SVDD_PWR_OFF for the shutdown settlement time */
usleep_range(2500, 3000);
trigger_onoff(pn8xt_dev, ST_SPI_SVDD_SY_END);
}
if ((pn8xt_dev->nfc_ven_enabled == false) && !(pn8xt_dev->secure_timer_cnt)) {
gpio_set_value(nfc_dev->ven_gpio, 0);
msleep(10);
}
} else if((*cur_state & ST_SPI) || (*cur_state & ST_SPI_FAILED)) {
if (!(*cur_state & ST_WIRED) &&
(pn8xt_dev->pwr_scheme != PN80T_EXT_PMU_SCM) &&
!(*cur_state & ST_JCP_DN)) {
if (isSignalTriggerReqd && !(*cur_state & ST_JCP_DN)) {
if(trigger_onoff(pn8xt_dev, ST_SPI_SVDD_SY_START | ST_SPI_END)) {
pr_debug(" %s DWP link activation failed. Returning..", __func__);
pn8xt_update_state(pn8xt_dev, ST_SPI_FAILED, true);
return -1;
}
}
/*if secure timer is running,Delay the SPI close by 25ms after sending End of Apdu
to enable eSE go into DPD gracefully(20ms after EOS + 5ms DPD settlement time)*/
if(pn8xt_dev->secure_timer_cnt)
usleep_range(25000, 30000);
if (!(pn8xt_dev->secure_timer_cnt)) {
gpio_set_value(nfc_dev->ese_pwr_gpio, 0);
/* Delay (2.5ms) after SVDD_PWR_OFF for the shutdown settlement time */
usleep_range(2500, 3000);
if(*cur_state & ST_SPI_FAILED) {
pn8xt_update_state(pn8xt_dev, ST_SPI_FAILED, false);
}
if(*cur_state & ST_SPI) {
pn8xt_update_state(pn8xt_dev, ST_SPI, false);
}
if(isSignalTriggerReqd)
trigger_onoff(pn8xt_dev, ST_SPI_SVDD_SY_END);
}
}
/*If JCOP3.2 or 3.3 for handling triple mode
protection signal NFC service */
else {
if (isSignalTriggerReqd) {
if (!(*cur_state & ST_JCP_DN)) {
if(pn8xt_dev->pwr_scheme == PN80T_LEGACY_PWR_SCM) {
if(trigger_onoff(pn8xt_dev, ST_SPI_SVDD_SY_START | ST_SPI_END)) {
pr_debug(" %s DWP link activation failed. Returning..", __func__);
pn8xt_update_state(pn8xt_dev, ST_SPI_FAILED, true);
return -1;
}
} else {
signal_handler(ST_SPI_END, pn8xt_dev->service_pid);
}
} else if (pn8xt_dev->pwr_scheme == PN80T_LEGACY_PWR_SCM) {
trigger_onoff(pn8xt_dev, ST_SPI_SVDD_SY_START);
}
}
if(pn8xt_dev->pwr_scheme == PN80T_LEGACY_PWR_SCM) {
gpio_set_value(nfc_dev->ese_pwr_gpio, 0);
if(*cur_state & ST_SPI_FAILED) {
pn8xt_update_state(pn8xt_dev, ST_SPI_FAILED, false);
}
if(*cur_state & ST_SPI) {
pn8xt_update_state(pn8xt_dev, ST_SPI, false);
}
if(isSignalTriggerReqd)
trigger_onoff(pn8xt_dev, ST_SPI_SVDD_SY_END);
pr_debug("%s:PN80T legacy ese_pwr_gpio off", __func__);
}
}
pn8xt_dev->spi_ven_enabled = false;
if (pn8xt_dev->nfc_ven_enabled == false && (pn8xt_dev->pwr_scheme != PN80T_EXT_PMU_SCM)
&& !(pn8xt_dev->secure_timer_cnt)) {
gpio_set_value(nfc_dev->ven_gpio, 0);
msleep(10);
}
} else {
pr_err("%s:failed, cur_state = %x\n", __func__, *cur_state);
return -EPERM; /* Operation not permitted */
}
break;
case 1:
pr_err("%s: power on ese\n", __func__);
if (((*cur_state & (ST_SPI|ST_SPI_PRIO)) == 0) || (*cur_state & ST_SPI_FAILED)) {
/*To handle triple mode protection signal
NFC service when SPI session started*/
if (isSignalTriggerReqd && !(*cur_state & ST_JCP_DN)) {
if(trigger_onoff(pn8xt_dev, ST_SPI)) {
pr_debug(" %s DWP link activation failed. Returning..", __func__);
pn8xt_update_state(pn8xt_dev, ST_SPI_FAILED, true);
return -1;
}
}
pn8xt_dev->spi_ven_enabled = true;
if(pn8xt_dev->pwr_scheme == PN80T_EXT_PMU_SCM)
break;
if (pn8xt_dev->nfc_ven_enabled == false) {
/* provide power to NFCC if, NFC service not provided */
gpio_set_value(nfc_dev->ven_gpio, 1);
msleep(10);
}
/* pull the gpio to high once NFCC is power on*/
gpio_set_value(nfc_dev->ese_pwr_gpio, 1);
/* Delay (10ms) after SVDD_PWR_ON to allow JCOP to bootup (5ms jcop boot time + 5ms guard time) */
usleep_range(10000, 12000);
if(*cur_state & ST_SPI_FAILED) {
pn8xt_update_state(pn8xt_dev, ST_SPI_FAILED, false);
}
pn8xt_update_state(pn8xt_dev, ST_SPI, true);
if (pn8xt_dev->service_pid) {
up(&pn8xt_dev->dwp_complete_sema);
}
} else if ((*cur_state & (ST_SPI|ST_SPI_PRIO))
&& (gpio_get_value(nfc_dev->ese_pwr_gpio)) && (gpio_get_value(nfc_dev->ven_gpio))) {
/* Returning success if SET_SPI_PWR called while already SPI is open */
return 0;
} else {
pr_info("%s : PN61_SET_SPI_PWR - power on ese failed \n", __func__);
return -EBUSY; /* Device or resource busy */
}
break;
case 2:
pr_debug("%s: reset\n", __func__);
if (*cur_state & (ST_IDLE|ST_SPI|ST_SPI_PRIO)) {
if (pn8xt_dev->spi_ven_enabled == false) {
pn8xt_dev->spi_ven_enabled = true;
if ((pn8xt_dev->nfc_ven_enabled == false) && (pn8xt_dev->pwr_scheme != PN80T_EXT_PMU_SCM)) {
/* provide power to NFCC if, NFC service not provided */
gpio_set_value(nfc_dev->ven_gpio, 1);
msleep(10);
}
}
if(pn8xt_dev->pwr_scheme != PN80T_EXT_PMU_SCM && !(pn8xt_dev->secure_timer_cnt)) {
trigger_onoff(pn8xt_dev, ST_SPI_SVDD_SY_START);
gpio_set_value(nfc_dev->ese_pwr_gpio, 0);
trigger_onoff(pn8xt_dev, ST_SPI_SVDD_SY_END);
msleep(10);
if(!gpio_get_value(nfc_dev->ese_pwr_gpio))
gpio_set_value(nfc_dev->ese_pwr_gpio, 1);
msleep(10);
}
} else {
pr_err("%s : PN61_SET_SPI_PWR - reset failed \n", __func__);
return -EBUSY; /* Device or resource busy */
}
break;
case 3:
pr_debug("%s: Prio Session Start power on ese\n", __func__);
if ((*cur_state & (ST_SPI | ST_SPI_PRIO)) == 0) {
pn8xt_update_state(pn8xt_dev, ST_SPI_PRIO, true);
if (*cur_state & ST_WIRED) {
trigger_onoff(pn8xt_dev, ST_SPI_PRIO);
}
pn8xt_dev->spi_ven_enabled = true;
if(pn8xt_dev->pwr_scheme != PN80T_EXT_PMU_SCM) {
if (pn8xt_dev->nfc_ven_enabled == false) {
/* provide power to NFCC if, NFC service not provided */
gpio_set_value(nfc_dev->ven_gpio, 1);
msleep(10);
}
/* pull the gpio to high once NFCC is power on*/
gpio_set_value(nfc_dev->ese_pwr_gpio, 1);
/* Delay (10ms) after SVDD_PWR_ON to allow JCOP to bootup (5ms jcop boot time + 5ms guard time) */
usleep_range(10000, 12000);
}
} else {
pr_err("%s : Prio Session Start power on ese failed \n", __func__);
return -EBUSY; /* Device or resource busy */
}
break;
case 4:
pr_debug("%s: Prio Session End called\n", __func__);
if (*cur_state & ST_SPI_PRIO) {
pr_info("%s : PN61_SET_SPI_PWR - Prio Session Ending...\n", __func__);
pn8xt_update_state(pn8xt_dev, ST_SPI_PRIO, false);
/*after SPI prio timeout, the state is changing from SPI prio to SPI */
pn8xt_update_state(pn8xt_dev, ST_SPI, true);
if (*cur_state & ST_WIRED) {
signal_handler(ST_SPI_PRIO_END, pn8xt_dev->service_pid);
}
} else {
pr_err("%s : PN61_SET_SPI_PWR - Prio Session End failed \n", __func__);
return -EBADRQC; /* Device or resource busy */
}
break;
case 5:
pr_debug("%s: Up ese_access_sema\n", __func__);
up(&pn8xt_dev->ese_access_sema);
break;
default:
pr_err("%s bad ese pwr arg %lu\n", __func__, arg);
return -EBADRQC; /* Invalid request code */
};
return 0;
}
static long set_jcop_download_state(struct pn8xt_dev *pn8xt_dev, unsigned long arg)
{
long ret = 0;
pn8xt_access_st_t *cur_state = pn8xt_get_state(pn8xt_dev);
pr_debug("%s::JCOP Dwnld arg = %ld",__func__, arg);
switch(arg) {
case JCP_DN_INIT:
if(pn8xt_dev->service_pid) {
pr_err("%s:nfc service pid %ld", __func__, pn8xt_dev->service_pid);
signal_handler(JCP_DN_INIT, pn8xt_dev->service_pid);
} else {
if (*cur_state & ST_JCP_DN) {
ret = -EINVAL;
} else {
pn8xt_update_state(pn8xt_dev, ST_JCP_DN, true);
}
}
break;
case JCP_DN_START:
if (*cur_state & ST_JCP_DN) {
ret = -EINVAL;
} else {
pn8xt_update_state(pn8xt_dev, ST_JCP_DN, true);
}
break;
case JCP_SPI_DN_COMP:
signal_handler(JCP_DWP_DN_COMP, pn8xt_dev->service_pid);
pn8xt_update_state(pn8xt_dev, ST_JCP_DN, false);
break;
case JCP_DWP_DN_COMP:
pn8xt_update_state(pn8xt_dev, ST_JCP_DN, false);
break;
default:
pr_err("%s: bad ese pwr arg %lu\n", __func__, arg);
return -EBADRQC; /* Invalid request code */
};
return ret;
}
static int set_wired_access(struct nfc_dev *nfc_dev, unsigned long arg)
{
struct pn8xt_dev *pn8xt_dev = (struct pn8xt_dev *)nfc_dev->pdata_op;
pn8xt_access_st_t *cur_state = pn8xt_get_state(pn8xt_dev);
if (!pn8xt_dev) {
pr_err("%s: pn8xt_dev doesn't exist anymore\n", __func__);
return -ENODEV;
}
switch(arg) {
case 0:
pr_debug("%s: disabling \n", __func__);
if (*cur_state & ST_WIRED) {
pn8xt_update_state(pn8xt_dev, ST_WIRED, false);
} else {
pr_err("%s: failed, cur_state = %x\n", __func__, *cur_state);
return -EPERM; /* Operation not permitted */
}
break;
case 1:
if (*cur_state)
{
pr_debug("%s: enabling\n", __func__);
pn8xt_update_state(pn8xt_dev, ST_WIRED, true);
if (*cur_state & ST_SPI_PRIO) {
signal_handler(ST_SPI_PRIO, pn8xt_dev->service_pid);
}
} else {
pr_err("%s: enabling failed \n", __func__);
return -EBUSY; /* Device or resource busy */
}
break;
case 2:
case 3:
pr_debug("%s: obsolete arguments for P67\n", __func__);
break;
case 4:
up(&pn8xt_dev->ese_access_sema);
break;
case 5:
gpio_set_value(nfc_dev->ese_pwr_gpio, 1);
if (gpio_get_value(nfc_dev->ese_pwr_gpio)) {
pr_info("%s: ese_pwr gpio is enabled\n", __func__);
}
break;
case 6:
gpio_set_value(nfc_dev->ese_pwr_gpio, 0);
pr_info("%s: ese_pwr gpio set to low\n", __func__);
break;
default:
pr_err("%s bad arg %lu\n", __func__, arg);
return -EBADRQC; /* Invalid request code */
};
return 0;
}
static void secure_timer_callback(struct timer_list *t)
{
struct pn8xt_dev *pn8xt_dev = from_timer(pn8xt_dev, t, secure_timer);;
/* Flush and push the timer callback event to the bottom half(work queue)
to be executed later, at a safer time */
flush_workqueue(pn8xt_dev->pSecureTimerCbWq);
queue_work(pn8xt_dev->pSecureTimerCbWq, &pn8xt_dev->wq_task);
return;
}
static long start_seccure_timer(struct pn8xt_dev *pn8xt_dev, unsigned long timer_value)
{
long ret = -EINVAL;
pr_debug("%s: called\n", __func__);
/* Delete the timer if timer pending */
if(timer_pending(&pn8xt_dev->secure_timer) == 1) {
pr_debug("%s: delete pending timer \n", __func__);
/* delete timer if already pending */
del_timer(&pn8xt_dev->secure_timer);
}
/* Start the timer if timer value is non-zero */
if(timer_value) {
timer_setup(&pn8xt_dev->secure_timer, secure_timer_callback, 0);
pr_debug("%s:timeout %lums (%lu)\n", __func__, timer_value, jiffies);
ret = mod_timer(&pn8xt_dev->secure_timer, jiffies + msecs_to_jiffies(timer_value));
if (ret)
pr_err("%s:Error in mod_timer\n", __func__);
}
return ret;
}
static void secure_timer_workqueue(struct work_struct *wq)
{
struct pn8xt_dev *pn8xt_dev = container_of(wq, struct pn8xt_dev, wq_task);
struct nfc_dev *nfc_dev = pn8xt_dev->nfc_dev;
pn8xt_access_st_t *cur_state = pn8xt_get_state(pn8xt_dev);
pr_debug("%s:called (%lu).\n", __func__, jiffies);
/* Locking the critical section: ESE_PWR_OFF to allow eSE to shutdown peacefully :: START */
get_ese_lock(pn8xt_dev, MAX_ESE_ACCESS_TIME_OUT_MS);
pn8xt_update_state(pn8xt_dev, ST_SECURE_MODE, false);
if((*cur_state & (ST_SPI|ST_SPI_PRIO)) == 0) {
pr_debug("%s: make se_pwer_gpio low, state = %d", __func__, *cur_state);
gpio_set_value(nfc_dev->ese_pwr_gpio, 0);
/* Delay (2.5ms) after SVDD_PWR_OFF for the shutdown settlement time */
usleep_range(2500, 3000);
if(pn8xt_dev->service_pid == 0x00) {
gpio_set_value(nfc_dev->ven_gpio, 0);
pr_debug("%s: make ven_gpio low, state = %d", __func__, *cur_state);
}
}
pn8xt_dev->secure_timer_cnt = 0;
/* Locking the critical section: ESE_PWR_OFF to allow eSE to shutdown peacefully :: END */
up(&pn8xt_dev->ese_access_sema);
return;
}
static long secure_timer_operation(struct pn8xt_dev *pn8xt_dev, unsigned long arg)
{
long ret = -EINVAL;
unsigned long timer_value = arg;
pr_debug("%s: pwr scheme = %d\n", __func__, pn8xt_dev->pwr_scheme);
if(pn8xt_dev->pwr_scheme == PN80T_LEGACY_PWR_SCM) {
ret = start_seccure_timer(pn8xt_dev, timer_value);
if(!ret) {
pn8xt_dev->secure_timer_cnt = 1;
pn8xt_update_state(pn8xt_dev, ST_SECURE_MODE, true);
} else {
pn8xt_dev->secure_timer_cnt = 0;
pn8xt_update_state(pn8xt_dev, ST_SECURE_MODE, false);
pr_debug("%s :timer reset \n", __func__);
}
} else {
pr_info("%s: timer session not applicable\n", __func__);
}
return ret;
}
long pn8xt_nfc_ese_ioctl(struct nfc_dev *nfc_dev, unsigned int cmd, unsigned long arg)
{
long ret = 0;
struct pn8xt_dev *pn8xt_dev = (struct pn8xt_dev *)nfc_dev->pdata_op;
if (!pn8xt_dev) {
pr_err("%s: pn8xt_dev doesn't exist anymore\n", __func__);
return -ENODEV;
}
if (cmd == PN8XT_GET_ESE_ACCESS) {
ret = get_ese_lock(pn8xt_dev, arg);
return ret;
}
switch (cmd) {
case PN8XT_SET_SPI_PWR:
ret = pn8xt_ese_pwr(nfc_dev, cmd, arg);
break;
case PN8XT_GET_PWR_STATUS:
put_user(pn8xt_dev->cur_state, (int __user *)arg);
break;
case PN8XT_SET_DN_STATUS:
ret = set_jcop_download_state(pn8xt_dev, arg);
break;
case PN8XT_SET_POWER_SCM:
if(arg == PN80T_LEGACY_PWR_SCM || arg == PN80T_EXT_PMU_SCM)
pn8xt_dev->pwr_scheme = arg;
else
pr_err("%s : The power scheme is invalid,\n", __func__);
break;
case PN8XT_SECURE_TIMER_SESSION:
ret = secure_timer_operation(pn8xt_dev, arg);
break;
default:
pr_err("%s bad ioctl %u\n", __func__, cmd);
ret = -EINVAL;
};
return ret;
}
long pn8xt_nfc_ioctl(struct nfc_dev *nfc_dev, unsigned int cmd, unsigned long arg)
{
long ret = 0;
struct pn8xt_dev *pn8xt_dev = (struct pn8xt_dev *)nfc_dev->pdata_op;
if (!pn8xt_dev) {
pr_err("%s: pn8xt_dev doesn't exist anymore\n", __func__);
return -ENODEV;
}
pr_debug("%s :enter cmd = %u, arg = %ld\n", __func__, cmd, arg);
switch(cmd) {
case PN8XT_SET_PWR:
ret = pn8xt_nfc_pwr(nfc_dev, arg);
break;
case PN8XT_SET_WIRED_ACCESS:
ret = set_wired_access(nfc_dev, arg);
break;
case PN8XT_REL_SVDD_WAIT:
pn8xt_dev->dwpLinkUpdateStat = arg;
up(&pn8xt_dev->svdd_onoff_sema);
break;
case PN8XT_SET_NFC_SERVICE_PID:
pn8xt_dev->service_pid = arg;
break;
case PN8XT_REL_DWP_WAIT:
pn8xt_dev->dwpLinkUpdateStat = arg;
up(&pn8xt_dev->dwp_onoff_sema);
sema_init(&pn8xt_dev->dwp_complete_sema, 0);
/*release JNI only after all the SPI On related actions are completed*/
if (down_timeout(&pn8xt_dev->dwp_complete_sema, msecs_to_jiffies(500)) != 0) {
pr_debug("Dwp On/off release wait protection: Timeout");
}
pr_debug("Dwp On/Off release wait protection : released");
break;
default:
ret = pn8xt_nfc_ese_ioctl(nfc_dev, cmd, arg);
if (ret)
pr_err("%s bad ioctl %u\n", __func__, cmd);
break;
};
pr_debug("%s :exit cmd = %u, arg = %ld\n", __func__, cmd, arg);
return ret;
}
#define SECURE_TIMER_WORK_QUEUE "SecTimerCbWq"
int pn8xt_nfc_probe(struct nfc_dev *nfc_dev)
{
struct pn8xt_dev *pn8xt_dev;
pn8xt_dev = kzalloc(sizeof(struct pn8xt_dev), GFP_KERNEL);
if (!pn8xt_dev) {
pr_err("failed to allocate memory for pn8xt_dev\n");
return -ENOMEM;
}
nfc_dev->pdata_op = pn8xt_dev;
pn8xt_dev->cur_state = ST_IDLE;
pn8xt_dev->pwr_scheme = PN80T_LEGACY_PWR_SCM;
pn8xt_dev->secure_timer_cnt = 0;
pn8xt_dev->nfc_ven_enabled = false;
pn8xt_dev->spi_ven_enabled = false;
/* init mutex and queues */
sema_init(&pn8xt_dev->ese_access_sema, 1);
sema_init(&pn8xt_dev->dwp_complete_sema, 0);
pn8xt_dev->pSecureTimerCbWq = create_workqueue(SECURE_TIMER_WORK_QUEUE);
INIT_WORK(&pn8xt_dev->wq_task, secure_timer_workqueue);
pn8xt_dev->nfc_dev = nfc_dev;
return 0;
}
int pn8xt_nfc_remove(struct nfc_dev *nfc_dev)
{
struct pn8xt_dev *pn8xt_dev;
pr_debug("%s: called\n", __func__);
pn8xt_dev = (struct pn8xt_dev *)nfc_dev->pdata_op;
if (!pn8xt_dev) {
pr_err("%s: pn8xt_dev doesn't exist anymore\n", __func__);
return -ENODEV;
}
destroy_workqueue(pn8xt_dev->pSecureTimerCbWq);
pn8xt_dev->cur_state = ST_INVALID;
pn8xt_dev->nfc_ven_enabled = false;
pn8xt_dev->spi_ven_enabled = false;
pn8xt_dev->nfc_dev = NULL;
kfree(pn8xt_dev);
return 0;
}

View File

@ -0,0 +1,135 @@
/*
* 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.
*
*/
/******************************************************************************
*
* The original Work has been changed by NXP Semiconductors.
*
* Copyright (C) 2013-2019 NXP Semiconductors
* *
* 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.
*
******************************************************************************/
#ifndef _NXP_NFC_PN8XT_H_
#define _NXP_NFC_PN8XT_H_
/*
* NFC power control via ioctl
* PN8XT_SET_PWR(0): power off
* PN8XT_SET_PWR(1): power on
* PN8XT_SET_PWR(2): reset and power on with firmware download enabled
*/
#define PN8XT_SET_PWR _IOW(NXP_NFC_MAGIC, 0x01, long)
/*
* SPI Request NFCC to enable ESE power, only in param
* Only for SPI
* level 1 = Enable power
* level 0 = Disable power
*/
#define PN8XT_SET_SPI_PWR _IOW(NXP_NFC_MAGIC, 0x02, long)
/* SPI or DWP can call this ioctl to get the current
* power state of ESE
*
*/
#define PN8XT_GET_PWR_STATUS _IOR(NXP_NFC_MAGIC, 0x03, long)
/* DWP side this ioctl will be called
* level 1 = Wired access is enabled/ongoing
* level 0 = Wired access is disabled/stopped
*/
#define PN8XT_SET_WIRED_ACCESS _IOW(NXP_NFC_MAGIC, 0x04, long)
/*
NFC Init will call the ioctl to register the PID with the i2c driver
*/
#define PN8XT_SET_NFC_SERVICE_PID _IOW(NXP_NFC_MAGIC, 0x05, long)
/*
NFC and SPI will call the ioctl to get the i2c/spi bus access
*/
#define PN8XT_GET_ESE_ACCESS _IOW(NXP_NFC_MAGIC, 0x06, long)
/*
NFC and SPI will call the ioctl to update the power scheme
*/
#define PN8XT_SET_POWER_SCM _IOW(NXP_NFC_MAGIC, 0x07, long)
/*
NFC will call the ioctl to release the svdd protection
*/
#define PN8XT_REL_SVDD_WAIT _IOW(NXP_NFC_MAGIC, 0x08, long)
/* SPI or DWP can call this ioctl to get the current
* power state of ESE
*
*/
#define PN8XT_SET_DN_STATUS _IOW(NXP_NFC_MAGIC, 0x09, long)
/*
NFC will call the ioctl to release the dwp on/off protection
*/
#define PN8XT_REL_DWP_WAIT _IOW(NXP_NFC_MAGIC, 0x0A, long)
/*
NFC will call the ioctl to start Secure Timer
*/
#define PN8XT_SECURE_TIMER_SESSION _IOW(NXP_NFC_MAGIC, 0x0B, long)
#define MAX_ESE_ACCESS_TIME_OUT_MS 200
typedef enum pn8xt_access_state {
ST_INVALID = 0x0000,
ST_IDLE = 0x0100, /*ESE is free to use */
ST_WIRED = 0x0200, /*ESE is being accessed by DWP (NFCC)*/
ST_SPI = 0x0400, /*ESE is being accessed by SPI */
ST_DN = 0x0800, /*NFCC fw download is in progress */
ST_SPI_PRIO = 0x1000, /*Start of ESE access by SPI on priority*/
ST_SPI_PRIO_END = 0x2000, /*End of ESE access by SPI on priority*/
ST_SPI_END = 0x4000,
ST_JCP_DN = 0x8000, /*JCOP downlad in progress */
ST_SECURE_MODE = 0x100000, /*secure mode state*/
ST_SPI_SVDD_SY_START = 0x0001, /*ESE_VDD Low req by SPI*/
ST_SPI_SVDD_SY_END = 0x0002, /*ESE_VDD is Low by SPI*/
ST_DWP_SVDD_SY_START = 0x0004, /*ESE_VDD Low req by Nfc*/
ST_DWP_SVDD_SY_END = 0x0008, /*ESE_VDD is Low by Nfc*/
ST_SPI_FAILED = 0x0010 /*SPI open/close failed*/
}pn8xt_access_st_t;
typedef enum pn8xt_pwr_scheme {
PN80T_LEGACY_PWR_SCM = 0x02,
PN80T_EXT_PMU_SCM,
}pn8xt_pwr_scm_t;
typedef enum pn8xt_jcop_dwnld_state {
JCP_DN_IDLE = ST_JCP_DN, /* jcop dwnld is ongoing*/
JCP_DN_INIT=0x8010, /* jcop dwonload init state*/
JCP_DN_START=0x8020, /* download started */
JCP_SPI_DN_COMP=0x8040, /* jcop download complete in spi interface*/
JCP_DWP_DN_COMP=0x8080, /* jcop download complete */
} pn8xt_jcop_dwnld_state_t;
long pn8xt_nfc_ese_ioctl(struct nfc_dev *nfc_dev, unsigned int cmd, unsigned long arg);
long pn8xt_nfc_ioctl(struct nfc_dev *nfc_dev, unsigned int cmd, unsigned long arg);
int pn8xt_nfc_probe(struct nfc_dev *nfc_dev);
int pn8xt_nfc_remove(struct nfc_dev *nfc_dev);
#endif //_NXP_NFC_PN8XT_H_

View File

@ -0,0 +1,159 @@
/*
* 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.
*
*/
/******************************************************************************
*
* The original Work has been changed by NXP Semiconductors.
*
* Copyright (C) 2013-2019 NXP Semiconductors
* *
* 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.
*
******************************************************************************/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/spinlock.h>
#include <linux/of_gpio.h>
#include <linux/of_device.h>
#include <linux/uaccess.h>
#include <linux/ioctl.h>
#include <linux/miscdevice.h>
#include <linux/i2c.h>
#include "nfc.h"
#include "sn1xx.h"
/*
* Power management of the eSE
*/
long sn1xx_nfc_ese_ioctl(struct nfc_dev *nfc_dev, unsigned int cmd, unsigned long arg)
{
int ret = 0;
pr_debug("%s: cmd = %u, arg = %lu\n", __func__, cmd, arg);
switch(cmd) {
//TODO - add cases for the ESE mechanism
default:
pr_err("%s: bad ioctl: cmd = %u, arg = %lu\n", __func__, cmd, arg);
ret = -ENOIOCTLCMD;
};
return ret;
}
/*
* sn100_nfc_pwr() - power control/firmware download
* @filp: pointer to the file descriptor
* @arg: mode that we want to move to
*
* Device power control. Depending on the arg value, device moves to
* different states
* (arg = 1):FW_DL GPIO = 0
* (arg = 2):FW_DL GPIO = 1 - Power up in firmware download mode
* (additional ioctl for sn100 because of keeping VEN always high)
* (arg = 4):enable firmware download via NCI commands
* (arg = 5):power on/reset NFC and ESE
* (arg = 6):disable firmware download via NCI commands
*
* Return: -ENOIOCTLCMD if arg is not supported, 0 in any other case
*/
static long sn1xx_nfc_pwr(struct nfc_dev *nfc_dev, unsigned long arg)
{
pr_debug("%s: %lu\n", __func__, arg);
switch(arg) {
case 1:
case 6:
if (gpio_is_valid(nfc_dev->firm_gpio)) {
gpio_set_value(nfc_dev->firm_gpio, 0);
usleep_range(10000, 10100);
}
break;
case 2:
case 5:
nfc_ese_acquire(nfc_dev);
nfc_disable_irq(nfc_dev);
gpio_set_value(nfc_dev->ven_gpio, 0);
usleep_range(10000, 10100);
if (2 == arg) {
if (gpio_is_valid(nfc_dev->firm_gpio)) {
gpio_set_value(nfc_dev->firm_gpio, 1);
usleep_range(10000, 10100);
}
}
nfc_enable_irq(nfc_dev);
gpio_set_value(nfc_dev->ven_gpio, 1);
usleep_range(10000, 10100);
nfc_ese_release(nfc_dev);
break;
case 4:
if (gpio_is_valid(nfc_dev->firm_gpio)) {
gpio_set_value(nfc_dev->firm_gpio, 1);
usleep_range(10000, 10100);
}
break;
default:
pr_err("%s bad ioctl %lu\n", __func__, arg);
return -ENOIOCTLCMD;
}
return 0;
}
long sn1xx_nfc_ioctl(struct nfc_dev *nfc_dev, unsigned int cmd,
unsigned long arg)
{
int ret = 0;
pr_debug("%s: cmd = %u, arg = %lu\n", __func__, cmd, arg);
switch (cmd) {
case SN1XX_SET_PWR:
ret = sn1xx_nfc_pwr(nfc_dev, arg);
break;
default:
pr_err("%s: bad ioctl: cmd = %u, arg = %lu\n", __func__, cmd, arg);
ret = -ENOIOCTLCMD;
}
return ret;
}
int sn1xx_nfc_probe(struct nfc_dev *nfc_dev)
{
pr_debug("%s: enter\n", __func__);
/* VBAT--> VDDIO(HIGH) + Guardtime of min 5ms --> VEN(HIGH) */
nfc_ese_acquire(nfc_dev);
msleep(5);
/* VEN toggle(reset) to proceed */
gpio_set_value(nfc_dev->ven_gpio, 0);
msleep(5);
gpio_set_value(nfc_dev->ven_gpio, 1);
nfc_ese_release(nfc_dev);
return 0;
}
int sn1xx_nfc_remove(struct nfc_dev *nfc_dev)
{
/*nothing needed here in addition to generic driver*/
return 0;
}

View File

@ -0,0 +1,47 @@
/*
* 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.
*
*/
/******************************************************************************
*
* The original Work has been changed by NXP Semiconductors.
*
* Copyright (C) 2013-2019 NXP Semiconductors
* *
* 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.
*
******************************************************************************/
#ifndef _NXP_NFC_SN1XX_H_
#define _NXP_NFC_SN1XX_H_
/*
* NFC power control via ioctl
* SN1XX_SET_PWR(4): enable firmware download via NCI commands
* SN1XX_SET_PWR(5): power on/reset NFC and ESE
* SN1XX_SET_PWR(6): disable firmware download via NCI commands
*/
#define SN1XX_SET_PWR _IOW(NXP_NFC_MAGIC, 0x01, long)
long sn1xx_nfc_ese_ioctl(struct nfc_dev *nfc_dev, unsigned int cmd, unsigned long arg);
long sn1xx_nfc_ioctl(struct nfc_dev *nfc_dev, unsigned int cmd, unsigned long arg);
int sn1xx_nfc_probe(struct nfc_dev *nfc_dev);
int sn1xx_nfc_remove(struct nfc_dev *nfc_dev);
#endif //_NXP_NFC_SN1XX_H_