Quellcode durchsuchen

Updated corresponding to - NFC_AR_00_6000_11.06.00

nxf24591 vor 4 Jahren
Ursprung
Commit
bedce83629
12 geänderte Dateien mit 1600 neuen und 2261 gelöschten Zeilen
  1. 6 0
      nfc/Makefile
  2. 447 0
      nfc/common.c
  3. 211 0
      nfc/common.h
  4. 347 0
      nfc/common_ese.c
  5. 97 0
      nfc/common_ese.h
  6. 467 0
      nfc/i2c_drv.c
  7. 25 19
      nfc/i2c_drv.h
  8. 0 13
      pn553-i2c/Kconfig
  9. 0 9
      pn553-i2c/Makefile
  10. 0 343
      pn553-i2c/cold_reset.c
  11. 0 1611
      pn553-i2c/pn553.c
  12. 0 266
      pn553-i2c/pn553.h

+ 6 - 0
nfc/Makefile

@@ -0,0 +1,6 @@
+#
+# Makefile for nfc devices
+#
+obj-$(CONFIG_NXP_NFC_I2C)	+= pn553_i2c.o
+pn553_i2c-objs			:= common.o common_ese.o i2c_drv.o
+#ccflags-y 			:= -DDEBUG

+ 447 - 0
nfc/common.c

@@ -0,0 +1,447 @@
+/******************************************************************************
+ *  Copyright (C) 2019-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.
+ *
+ * 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/of_gpio.h>
+#include <linux/of_device.h>
+#include <linux/delay.h>
+#include <linux/version.h>
+#include "common.h"
+#include "common_ese.h"
+
+int nfc_parse_dt(struct device *dev, platform_gpio_t * nfc_gpio,
+		 uint8_t interface)
+{
+	struct device_node *np = dev->of_node;
+
+	if (!np) {
+		pr_err("nfc of_node NULL\n");
+		return -EINVAL;
+	}
+
+	nfc_gpio->irq = -EINVAL;
+	nfc_gpio->dwl_req = -EINVAL;
+	nfc_gpio->ven = -EINVAL;
+
+	//required for i2c based chips only
+	if (interface == PLATFORM_IF_I2C) {
+		nfc_gpio->irq = of_get_named_gpio(np, DTS_IRQ_GPIO_STR, 0);
+		if ((!gpio_is_valid(nfc_gpio->irq))) {
+			pr_err("nfc irq gpio invalid %d\n", nfc_gpio->irq);
+			return -EINVAL;
+		}
+		pr_info("%s: irq %d\n", __func__, nfc_gpio->irq);
+
+		nfc_gpio->dwl_req = of_get_named_gpio(np, DTS_FWDN_GPIO_STR, 0);
+		if ((!gpio_is_valid(nfc_gpio->dwl_req))) {
+			pr_err("nfc dwl_req gpio invalid %d\n", nfc_gpio->dwl_req);
+		}
+	}
+	nfc_gpio->ven = of_get_named_gpio(np, DTS_VEN_GPIO_STR, 0);
+	if ((!gpio_is_valid(nfc_gpio->ven))) {
+		pr_err("nfc ven gpio invalid %d\n", nfc_gpio->ven);
+		return -EINVAL;
+	}
+
+	pr_info("%s: %d, %d, %d, %d\n", __func__, nfc_gpio->irq, nfc_gpio->ven,
+		nfc_gpio->dwl_req);
+	return 0;
+}
+
+void set_valid_gpio(int gpio, int value)
+{
+	if (gpio_is_valid(gpio)) {
+		pr_debug("%s gpio %d value %d\n", __func__, gpio, value);
+		gpio_set_value(gpio, value);
+		// hardware dependent delay
+		usleep_range(10000, 10100);
+	}
+}
+
+int get_valid_gpio(int gpio)
+{
+	int value = -1;
+	if (gpio_is_valid(gpio)) {
+		value = gpio_get_value(gpio);
+		pr_debug("%s gpio %d value %d\n", __func__, gpio, value);
+	}
+	return value;
+}
+
+void gpio_set_ven(struct nfc_dev *nfc_dev, int value)
+{
+	if (gpio_get_value(nfc_dev->gpio.ven) != value) {
+		pr_debug("%s: gpio_set_ven %d\n", __func__, value);
+		/*reset on change in level from high to low */
+		if (value) {
+			common_ese_on_hard_reset(nfc_dev);
+		}
+		gpio_set_value(nfc_dev->gpio.ven, value);
+		// hardware dependent delay
+		usleep_range(10000, 10100);
+	}
+}
+
+int configure_gpio(unsigned int gpio, int flag)
+{
+	int ret;
+	pr_debug("%s: nfc gpio [%d] flag [%01x]\n", __func__, gpio, flag);
+	if (gpio_is_valid(gpio)) {
+		ret = gpio_request(gpio, "nfc_gpio");
+		if (ret) {
+			pr_err("%s: unable to request nfc gpio [%d]\n", __func__, gpio);
+			return ret;
+		}
+		/*set direction and value for output pin */
+		if (flag & GPIO_OUTPUT) {
+			ret = gpio_direction_output(gpio, (GPIO_HIGH & flag));
+			pr_debug("nfc o/p gpio %d level %d\n", gpio, gpio_get_value(gpio));
+		} else {
+			ret = gpio_direction_input(gpio);
+			pr_debug("nfc i/p gpio %d\n", gpio);
+		}
+
+		if (ret) {
+			pr_err("%s: unable to set direction for nfc gpio [%d]\n", __func__, gpio);
+			gpio_free(gpio);
+			return ret;
+		}
+		/*Consider value as control for input IRQ pin */
+		if (flag & GPIO_IRQ) {
+			ret = gpio_to_irq(gpio);
+			if (ret < 0) {
+				pr_err("%s: unable to set irq for nfc gpio [%d]\n", __func__, gpio);
+				gpio_free(gpio);
+				return ret;
+			}
+			pr_debug("%s: gpio_to_irq successful [%d]\n", __func__, gpio);
+			return ret;
+		}
+	} else {
+		pr_err("%s: invalid gpio\n", __func__);
+		ret = -EINVAL;
+	}
+	return ret;
+}
+
+void gpio_free_all(nfc_dev_t *nfc_dev)
+{
+	if (gpio_is_valid(nfc_dev->gpio.dwl_req)) {
+		gpio_free(nfc_dev->gpio.dwl_req);
+	}
+	if (gpio_is_valid(nfc_dev->gpio.irq)) {
+		gpio_free(nfc_dev->gpio.irq);
+	}
+	if (gpio_is_valid(nfc_dev->gpio.ven)) {
+		gpio_free(nfc_dev->gpio.ven);
+	}
+}
+
+void nfc_misc_unregister(nfc_dev_t *nfc_dev, int count)
+{
+	pr_debug("%s: entry\n", __func__);
+	device_destroy(nfc_dev->nfc_class, nfc_dev->devno);
+	cdev_del(&nfc_dev->c_dev);
+	class_destroy(nfc_dev->nfc_class);
+	unregister_chrdev_region(nfc_dev->devno, count);
+}
+
+int nfc_misc_register(nfc_dev_t *nfc_dev,
+		      const struct file_operations *nfc_fops,
+		      int count, char *devname, char *classname)
+{
+	int ret = 0;
+	ret = alloc_chrdev_region(&nfc_dev->devno, 0, count, devname);
+	if (ret < 0) {
+		pr_err("%s: failed to alloc chrdev region ret %d\n", __func__, ret);
+		return ret;
+	}
+	nfc_dev->nfc_class = class_create(THIS_MODULE, classname);
+	if (IS_ERR(nfc_dev->nfc_class)) {
+		ret = PTR_ERR(nfc_dev->nfc_class);
+		pr_err("%s: failed to register device class ret %d\n", __func__, ret);
+		unregister_chrdev_region(nfc_dev->devno, count);
+		return ret;
+	}
+	cdev_init(&nfc_dev->c_dev, nfc_fops);
+	ret = cdev_add(&nfc_dev->c_dev, nfc_dev->devno, count);
+	if (ret < 0) {
+		pr_err("%s: failed to add cdev ret %d\n", __func__, ret);
+		class_destroy(nfc_dev->nfc_class);
+		unregister_chrdev_region(nfc_dev->devno, count);
+		return ret;
+	}
+	nfc_dev->nfc_device = device_create(nfc_dev->nfc_class, NULL,
+					    nfc_dev->devno, nfc_dev, devname);
+	if (IS_ERR(nfc_dev->nfc_device)) {
+		ret = PTR_ERR(nfc_dev->nfc_device);
+		pr_err("%s: failed to create the device ret %d\n", __func__, ret);
+		cdev_del(&nfc_dev->c_dev);
+		class_destroy(nfc_dev->nfc_class);
+		unregister_chrdev_region(nfc_dev->devno, count);
+		return ret;
+	}
+	return 0;
+}
+
+/*
+ * nfc_ioctl_power_states() - power control
+ * @nfc_dev:    nfc device data structure
+ * @arg:    mode that we want to move to
+ *
+ * Device power control. Depending on the arg value, device moves to
+ * different states, refer common.h for args
+ *
+ * Return: -ENOIOCTLCMD if arg is not supported, 0 in any other case
+ */
+static int nfc_ioctl_power_states(nfc_dev_t *nfc_dev, unsigned long arg)
+{
+	int ret = 0;
+	if (arg == NFC_POWER_OFF) {
+		/*
+		 * We are attempting a hardware reset so let us disable
+		 * interrupts to avoid spurious notifications to upper
+		 * layers.
+		 */
+		nfc_dev->nfc_disable_intr(nfc_dev);
+		set_valid_gpio(nfc_dev->gpio.dwl_req, 0);
+		gpio_set_ven(nfc_dev, 0);
+		nfc_dev->nfc_ven_enabled = false;
+	} else if (arg == NFC_POWER_ON) {
+		nfc_dev->nfc_enable_intr(nfc_dev);
+		set_valid_gpio(nfc_dev->gpio.dwl_req, 0);
+
+		gpio_set_ven(nfc_dev, 1);
+		nfc_dev->nfc_ven_enabled = true;
+	} else if (arg == NFC_FW_DWL_VEN_TOGGLE) {
+		/*
+		 * We are switching to download Mode, toggle the enable pin
+		 * in order to set the NFCC in the new mode
+		 */
+		nfc_dev->nfc_disable_intr(nfc_dev);
+		set_valid_gpio(nfc_dev->gpio.dwl_req, 1);
+		if (nfc_dev->interface == PLATFORM_IF_I2C) {
+			nfc_dev->nfc_state = NFC_STATE_FW_DWL;
+		}
+		gpio_set_ven(nfc_dev, 0);
+		gpio_set_ven(nfc_dev, 1);
+		nfc_dev->nfc_enable_intr(nfc_dev);
+	} else if (arg == NFC_FW_DWL_HIGH) {
+		/*
+		 * Setting firmware download gpio to HIGH
+		 * before FW download start
+		 */
+		set_valid_gpio(nfc_dev->gpio.dwl_req, 1);
+		if (nfc_dev->interface == PLATFORM_IF_I2C) {
+			nfc_dev->nfc_state = NFC_STATE_FW_DWL;
+		}
+
+	} else if (arg == NFC_VEN_FORCED_HARD_RESET) {
+		nfc_dev->nfc_disable_intr(nfc_dev);
+		gpio_set_ven(nfc_dev, 0);
+		gpio_set_ven(nfc_dev, 1);
+		nfc_dev->nfc_enable_intr(nfc_dev);
+	} else if (arg == NFC_FW_DWL_LOW) {
+		/*
+		 * Setting firmware download gpio to LOW
+		 * FW download finished
+		 */
+		set_valid_gpio(nfc_dev->gpio.dwl_req, 0);
+		if (nfc_dev->interface == PLATFORM_IF_I2C) {
+			nfc_dev->nfc_state = NFC_STATE_NCI;
+		}
+	} else {
+		pr_err("%s bad arg %lu\n", __func__, arg);
+		ret = -ENOIOCTLCMD;
+	}
+	return ret;
+}
+
+/** @brief   IOCTL function  to be used to set or get data from upper layer.
+ *
+ *  @param   pfile  fil node for opened device.
+ *  @cmd     IOCTL type from upper layer.
+ *  @arg     IOCTL arg from upper layer.
+ *
+ *  @return 0 on success, error code for failures.
+ */
+long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
+{
+	int ret = 0;
+	struct nfc_dev *nfc_dev = pfile->private_data;
+	if (!nfc_dev)
+		return -ENODEV;
+
+	pr_debug("%s cmd = %x arg = %zx\n", __func__, cmd, arg);
+	switch (cmd) {
+	case NFC_SET_PWR:
+		ret = nfc_ioctl_power_states(nfc_dev, arg);
+		break;
+	case ESE_SET_PWR:
+		ret = nfc_ese_pwr(nfc_dev, arg);
+		break;
+	case ESE_GET_PWR:
+		ret = nfc_ese_pwr(nfc_dev, ESE_POWER_STATE);
+		break;
+	case NFC_GET_PLATFORM_TYPE:
+		ret = nfc_dev->interface;
+		break;
+	case NFC_GET_NFC_STATE:
+		ret = nfc_dev->nfc_state;
+		pr_debug("nfc get state %d\n", ret);
+		break;
+	case NFC_GET_IRQ_STATE:
+		ret = 0;
+		ret = gpio_get_value(nfc_dev->gpio.irq);
+		break;
+	default:
+		pr_err("%s bad cmd %lu\n", __func__, arg);
+		ret = -ENOIOCTLCMD;
+	};
+	return ret;
+}
+
+int nfc_dev_open(struct inode *inode, struct file *filp)
+{
+	nfc_dev_t *nfc_dev = container_of(inode->i_cdev, nfc_dev_t, c_dev);
+	pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
+
+	mutex_lock(&nfc_dev->dev_ref_mutex);
+
+	filp->private_data = nfc_dev;
+
+	if (nfc_dev->dev_ref_count == 0) {
+		set_valid_gpio(nfc_dev->gpio.dwl_req, 0);
+
+		nfc_dev->nfc_enable_intr(nfc_dev);
+	}
+	nfc_dev->dev_ref_count = nfc_dev->dev_ref_count + 1;
+	mutex_unlock(&nfc_dev->dev_ref_mutex);
+	return 0;
+}
+
+int nfc_dev_close(struct inode *inode, struct file *filp)
+{
+	nfc_dev_t *nfc_dev = container_of(inode->i_cdev, nfc_dev_t, c_dev);
+	pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
+	mutex_lock(&nfc_dev->dev_ref_mutex);
+	if (nfc_dev->dev_ref_count == 1) {
+		nfc_dev->nfc_disable_intr(nfc_dev);
+		set_valid_gpio(nfc_dev->gpio.dwl_req, 0);
+	}
+	if (nfc_dev->dev_ref_count > 0)
+		nfc_dev->dev_ref_count = nfc_dev->dev_ref_count - 1;
+	else {
+		nfc_ese_pwr(nfc_dev, ESE_RST_PROT_DIS_NFC);
+		/* Uncomment below line incase of eSE calls flow is via NFC driver
+		 * i.e. direct calls from SPI HAL to NFC driver*/
+		//nfc_ese_pwr(nfc_dev, ESE_RST_PROT_DIS);
+	}
+	filp->private_data = NULL;
+
+	mutex_unlock(&nfc_dev->dev_ref_mutex);
+	return 0;
+}
+
+static int get_nfcc_boot_state(struct nfc_dev *nfc_dev)
+{
+	int ret = 0;
+	char get_version_cmd[] = { 0x00, 0x04, 0xF1, 0x00, 0x00, 0x00, 0x6E, 0xEF };
+	char get_session_state_cmd[] = { 0x00, 0x04, 0xF2, 0x00, 0x00, 0x00, 0xF5, 0x33 };
+	char rsp_buf[MAX_BUFFER_SIZE];
+	/*clearing any data in the kbuf store */
+	do {
+		ret = nfc_dev->nfc_read(nfc_dev, rsp_buf, MAX_BUFFER_SIZE);
+		if (ret < 0) {
+			pr_err("%s: - nfc read ret %d\n", __func__, ret);
+		}
+		pr_info("%s: - nfc read ret %d\n", __func__, ret);
+	} while (ret > 0);
+
+	pr_debug("%s:Sending GET_VERSION cmd\n", __func__);
+	ret = nfc_dev->nfc_write(nfc_dev, get_version_cmd,
+				 sizeof(get_version_cmd), MAX_RETRY_COUNT);
+	if (ret <= 0) {
+		pr_err("%s: - nfc get version cmd error ret %d\n", __func__, ret);
+		goto err;
+	}
+	memset(rsp_buf, 0x00, MAX_BUFFER_SIZE);
+	pr_debug("%s:Reading response of GET_VERSION cmd\n", __func__);
+	ret = nfc_dev->nfc_read(nfc_dev, rsp_buf, MAX_BUFFER_SIZE);
+	if (ret <= 0) {
+		pr_err("%s: - nfc get version rsp error ret %d\n", __func__, ret);
+		goto err;
+	} else if (rsp_buf[0] == 0x0
+		   && ret == (FW_HDR_LEN + rsp_buf[FW_PAYLOAD_LEN_IDX] + FW_CRC_LEN)
+		   && (ret == DL_GET_VERSION_RSP_LEN_1 || ret == DL_GET_VERSION_RSP_LEN_2)) {
+
+		pr_info("%s:NFC chip_type 0x%02x rom_version 0x%02x fw_minor 0x%02x fw_major 0x%02x\n",
+			__func__, rsp_buf[3], rsp_buf[4], rsp_buf[6], rsp_buf[7]);
+	} else if (rsp_buf[0] != 0x0
+		   && ret == (NCI_HDR_LEN + rsp_buf[NCI_PAYLOAD_LEN_IDX])) {
+		pr_info("%s:NFC response bytes 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__,
+			rsp_buf[0], rsp_buf[1], rsp_buf[2], rsp_buf[3], rsp_buf[3]);
+		pr_debug("%s NFCC booted in NCI mode %d\n", __func__, __LINE__);
+		return NFC_STATE_NCI;
+	}
+
+	pr_debug("%s:Sending GET_SESSION_STATE cmd \n", __func__);
+	ret = nfc_dev->nfc_write(nfc_dev, get_session_state_cmd,
+				 sizeof(get_session_state_cmd),
+				 MAX_RETRY_COUNT);
+	if (ret <= 0) {
+		pr_err("%s: - nfc get session state cmd err ret %d\n", __func__, ret);
+		goto err;
+	}
+	memset(rsp_buf, 0x00, DL_GET_SESSION_STATE_RSP_LEN);
+	pr_debug("%s:Reading response of GET_SESSION_STATE cmd\n", __func__);
+	ret = nfc_dev->nfc_read(nfc_dev, rsp_buf, DL_GET_SESSION_STATE_RSP_LEN);
+	if (ret <= 0) {
+		pr_err("%s: - nfc get session state rsp err %d\n", __func__, ret);
+		goto err;
+	}
+	pr_debug("Response bytes are %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x",
+		 rsp_buf[0], rsp_buf[1], rsp_buf[2], rsp_buf[3], rsp_buf[4], rsp_buf[5],
+		 rsp_buf[6], rsp_buf[7]);
+	/*verify fw in non-teared state */
+	if (rsp_buf[GET_SESSION_STS_OFF] != NFCC_SESSION_STS_CLOSED) {
+		pr_debug("%s NFCC  booted in teared fw state %d\n", __func__, __LINE__);
+		return NFC_STATE_FW_TEARED;
+	}
+	pr_debug("%s NFCC booted in FW DN mode %d\n", __func__, __LINE__);
+	return NFC_STATE_FW_DWL;
+err:
+	pr_err("%s Unlikely NFCC not booted in FW DN mode %d\n", __func__, __LINE__);
+	return NFC_STATE_UNKNOWN;
+}
+
+int validate_nfc_state_nci(nfc_dev_t *nfc_dev)
+{
+	if (!gpio_get_value(nfc_dev->gpio.ven)) {
+		pr_err("VEN LOW - NFCC powered off\n");
+		return -ENODEV;
+	} else {
+		if (get_valid_gpio(nfc_dev->gpio.dwl_req) == 1) {
+			pr_err("FW download in-progress\n");
+			return -EBUSY;
+		} else if (nfc_dev->nfc_state == NFC_STATE_FW_DWL) {
+			pr_err("FW download state \n");
+			return -EBUSY;
+		}
+	}
+	return 0;
+}

+ 211 - 0
nfc/common.h

@@ -0,0 +1,211 @@
+/******************************************************************************
+ *  Copyright (C) 2019-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.
+ *
+ * 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 _COMMON_H_
+#define _COMMON_H_
+#include <linux/types.h>
+#include <linux/version.h>
+#include <linux/semaphore.h>
+#include <linux/completion.h>
+#include <linux/ioctl.h>
+#include <linux/cdev.h>
+#include <linux/spinlock.h>
+#include <linux/gpio.h>
+
+#include "i2c_drv.h"
+
+#define DEV_COUNT               1	/* Max device count for this driver */
+#define CLASS_NAME              "nfc"	/* i2c device class */
+
+//  NFC character device name, this will be in /dev/
+#define NFC_CHAR_DEV_NAME       "pn553"
+
+// NCI packet details
+#define NCI_MSG_CMD                 0x20
+#define NCI_MSG_RSP                 0x40
+#define NCI_HDR_LEN                 3
+#define NCI_PAYLOAD_IDX             3
+#define NCI_PAYLOAD_LEN_IDX         2
+
+// FW DNLD packet details
+#define FW_HDR_LEN                  2
+#define FW_PAYLOAD_LEN_IDX          1
+#define FW_CRC_LEN                  2
+#define MIN_NFC_DL_FRAME_SIZE       3
+
+#define NCI_RESET_CMD_LEN           (4)
+#define NCI_RESET_RSP_LEN           (4)
+#define NCI_RESET_NTF_LEN           (13)
+
+#define DL_GET_VERSION_CMD_LEN      (8)
+#define DL_GET_VERSION_RSP_LEN_1    (12)
+#define DL_GET_VERSION_RSP_LEN_2    (20)
+
+#define DL_RESET_CMD_LEN                (8)
+#define DL_GET_SESSION_STATE_CMD_LEN    (8)
+#define DL_GET_SESSION_STATE_RSP_LEN    (8)
+#define GET_SESSION_STS_OFF             (3)
+#define NFCC_SESSION_STS_CLOSED         (0x0)
+#define MAX_NCI_PAYLOAD_LEN             (255)
+#define MAX_BUFFER_SIZE                 (NCI_HDR_LEN + MAX_NCI_PAYLOAD_LEN)
+#define MAX_DL_PAYLOAD_LEN              (550)
+#define MAX_DL_BUFFER_SIZE              (FW_HDR_LEN + FW_CRC_LEN + MAX_DL_PAYLOAD_LEN)
+// Maximum retry count for standby writes
+#define MAX_RETRY_COUNT                 (3)
+// Retry count for normal write
+#define NO_RETRY                        (1)
+#define MAX_IRQ_WAIT_TIME               (90)
+#define WAKEUP_SRC_TIMEOUT              (2000)
+
+/*command response timeout*/
+#define NCI_CMD_RSP_TIMEOUT             (2000)	//2s
+
+#define NFC_MAGIC 0xE9
+
+/*Ioctls*/
+// The type should be aligned with MW HAL definitions
+#define NFC_SET_PWR            _IOW(NFC_MAGIC, 0x01, long)
+#define ESE_SET_PWR            _IOW(NFC_MAGIC, 0x02, long)
+#define ESE_GET_PWR            _IOR(NFC_MAGIC, 0x03, long)
+#define NFC_GET_PLATFORM_TYPE  _IO(NFC_MAGIC, 0x04)
+#define NFC_GET_NFC_STATE      _IO(NFC_MAGIC, 0x05)
+/* NFC HAL can call this ioctl to get the current IRQ state */
+#define NFC_GET_IRQ_STATE      _IOW(NFC_MAGIC, 0x06, long)
+
+#define DTS_IRQ_GPIO_STR    "nxp,pn544-irq"
+#define DTS_VEN_GPIO_STR    "nxp,pn544-ven"
+#define DTS_FWDN_GPIO_STR   "nxp,pn544-fw-dwnld"
+
+enum nfcc_ioctl_request {
+	/* NFC disable request with VEN LOW */
+	NFC_POWER_OFF = 0,
+	/* NFC enable request with VEN Toggle */
+	NFC_POWER_ON,
+	/* firmware download request with VEN Toggle */
+	NFC_FW_DWL_VEN_TOGGLE,
+	/* ISO reset request */
+	NFC_ISO_RESET,
+	/* request for firmware download gpio HIGH */
+	NFC_FW_DWL_HIGH,
+	/* VEN hard reset request */
+	NFC_VEN_FORCED_HARD_RESET,
+	/* request for firmware download gpio LOW */
+	NFC_FW_DWL_LOW,
+};
+
+/*nfc platform interface type*/
+enum interface_flags {
+	/*I2C physical IF for NFCC */
+	PLATFORM_IF_I2C = 0,
+};
+
+/*nfc state flags*/
+enum nfc_state_flags {
+	/*nfc in unknown state */
+	NFC_STATE_UNKNOWN = 0,
+	/*nfc in download mode */
+	NFC_STATE_FW_DWL = 0x1,
+	/*nfc booted in NCI mode */
+	NFC_STATE_NCI = 0x2,
+	/*nfc booted in Fw teared mode */
+	NFC_STATE_FW_TEARED = 0x4,
+};
+/*
+ * Power state for IBI handing, mainly needed to defer the IBI handling
+ *  for the IBI received in suspend state to do it later in resume call
+ */
+enum pm_state_flags {
+	PM_STATE_NORMAL = 0,
+	PM_STATE_SUSPEND,
+	PM_STATE_IBI_BEFORE_RESUME,
+};
+
+/* Enum for GPIO values*/
+enum gpio_values {
+	GPIO_INPUT = 0x0,
+	GPIO_OUTPUT = 0x1,
+	GPIO_HIGH = 0x2,
+	GPIO_OUTPUT_HIGH = 0x3,
+	GPIO_IRQ = 0x4,
+};
+
+// NFC GPIO variables
+typedef struct platform_gpio {
+	unsigned int irq;
+	unsigned int ven;
+	unsigned int dwl_req;
+} platform_gpio_t;
+
+//cold reset Features specific Parameters
+typedef struct cold_reset {
+	bool rsp_pending;	/*cmd rsp pending status */
+	bool in_progress;	/*for cold reset when gurad timer in progress */
+	bool reset_protection;	/*reset protection enabled/disabled */
+	uint8_t status;		/*status from response buffer */
+	uint8_t rst_prot_src;	/*reset protection source (SPI, NFC) */
+	struct mutex sync_mutex;
+	struct timer_list timer;
+	wait_queue_head_t read_wq;
+} cold_reset_t;
+
+/* Device specific structure */
+typedef struct nfc_dev {
+	wait_queue_head_t read_wq;
+	struct mutex read_mutex;
+	struct mutex ese_access_mutex;
+	struct mutex dev_ref_mutex;
+	unsigned int dev_ref_count;
+	struct class *nfc_class;
+	struct device *nfc_device;
+	struct cdev c_dev;
+	dev_t devno;
+	/* Interface flag */
+	uint8_t interface;
+	/* nfc state flags */
+	uint8_t nfc_state;
+	/* NFC VEN pin state */
+	bool nfc_ven_enabled;
+	union {
+		i2c_dev_t i2c_dev;
+	};
+	platform_gpio_t gpio;
+	cold_reset_t cold_reset;
+
+	/*funtion pointers for the common i2c functionality */
+	int (*nfc_read) (struct nfc_dev *dev, char *buf, size_t count);
+	int (*nfc_write) (struct nfc_dev *dev, const char *buf, const size_t count,
+			  int max_retry_cnt);
+	int (*nfc_enable_intr) (struct nfc_dev *dev);
+	int (*nfc_disable_intr) (struct nfc_dev *dev);
+	int (*nfc_flush_readq) (struct nfc_dev *dev);
+} nfc_dev_t;
+
+int nfc_dev_open(struct inode *inode, struct file *filp);
+int nfc_dev_close(struct inode *inode, struct file *filp);
+long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg);
+int nfc_parse_dt(struct device *dev, platform_gpio_t *nfc_gpio,
+		 uint8_t interface);
+int nfc_misc_register(nfc_dev_t *nfc_dev,
+		      const struct file_operations *nfc_fops, int count, char *devname,
+		      char *classname);
+void nfc_misc_unregister(nfc_dev_t *nfc_dev, int count);
+int configure_gpio(unsigned int gpio, int flag);
+void gpio_set_ven(nfc_dev_t *nfc_dev, int value);
+void gpio_free_all(nfc_dev_t *nfc_dev);
+int validate_nfc_state_nci(nfc_dev_t *nfc_dev);
+#endif //_COMMON_H_

+ 347 - 0
nfc/common_ese.c

@@ -0,0 +1,347 @@
+/******************************************************************************
+ *  Copyright (C) 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.
+ *
+ * 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/i2c.h>
+#include <linux/irq.h>
+#include <linux/jiffies.h>
+#include <linux/delay.h>
+#include <linux/spinlock.h>
+#include <linux/version.h>
+#include <linux/fs.h>
+#include "common.h"
+#include "common_ese.h"
+
+static void cold_reset_gaurd_timer_callback(struct timer_list *t)
+{
+	cold_reset_t *cold_reset = from_timer(cold_reset, t, timer);
+	pr_debug("%s: Enter\n", __func__);
+	cold_reset->in_progress = false;
+	return;
+}
+
+static long start_cold_reset_guard_timer(cold_reset_t *cold_reset)
+{
+	long ret = -EINVAL;
+	if (timer_pending(&cold_reset->timer) == 1) {
+		pr_debug("ese_cold_reset_guard_timer: delete pending timer \n");
+		/* delete timer if already pending */
+		del_timer(&cold_reset->timer);
+	}
+	cold_reset->in_progress = true;
+	timer_setup(&cold_reset->timer, cold_reset_gaurd_timer_callback, 0);
+	ret = mod_timer(&cold_reset->timer,
+			jiffies + msecs_to_jiffies(ESE_CLD_RST_GUARD_TIME));
+	return ret;
+}
+
+static int send_cold_reset_protection_cmd(nfc_dev_t *nfc_dev, bool requestType)
+{
+	int ret = 0;
+	int length = 0;
+	uint8_t *cmd = NULL;
+	uint8_t cld_rst_cmd[] = { NCI_PROP_MSG_CMD, CLD_RST_OID,
+				  CLD_RST_PAYLOAD_SIZE
+				};
+	uint8_t rst_prot_cmd[] = { NCI_PROP_MSG_CMD, RST_PROT_OID,
+				   RST_PROT_PAYLOAD_SIZE, 0x00
+				 };
+
+	cold_reset_t *cold_reset = &nfc_dev->cold_reset;
+	if (requestType) {
+		length = sizeof(rst_prot_cmd);
+		rst_prot_cmd[NCI_PAYLOAD_IDX] = (!cold_reset->reset_protection) ? 1 : 0;
+		cmd = rst_prot_cmd;
+	} else {
+		length = sizeof(cld_rst_cmd);
+		cmd = cld_rst_cmd;
+	}
+
+	ret = nfc_dev->nfc_write(nfc_dev, cmd, length, MAX_RETRY_COUNT);
+	if (ret != length) {
+		pr_err("%s : nfc_write returned %d\n", __func__, ret);
+		return -EIO;
+	}
+
+	if (requestType) {
+		pr_debug("%s: NxpNciX: %d > %02X%02X%02X%02X\n", __func__, ret, cmd[0], cmd[1],
+			 cmd[2], cmd[3]);
+	} else {
+		pr_debug("%s: NxpNciX: %d > %02X%02X%02X\n", __func__, ret, cmd[0], cmd[1],
+			 cmd[2]);
+	}
+	return ret;
+}
+
+void wakeup_on_prop_rsp(nfc_dev_t *nfc_dev, uint8_t *buf)
+{
+	cold_reset_t *cold_reset = &nfc_dev->cold_reset;
+	cold_reset->status = -EIO;
+
+	if ((NCI_HDR_LEN + buf[NCI_PAYLOAD_LEN_IDX]) != NCI_PROP_MSG_RSP_LEN) {
+		pr_err("%s: - invalid response for cold_reset/protection \n", __func__);
+	} else {
+		cold_reset->status = buf[NCI_PAYLOAD_IDX];
+	}
+	pr_debug("%s NxpNciR : len = 4 > %02X%02X%02X%02X\n", __func__, buf[0], buf[1],
+		buf[2], buf[3]);
+
+	cold_reset->rsp_pending = false;
+	wake_up_interruptible(&cold_reset->read_wq);
+}
+
+static int validate_cold_reset_protection_request(cold_reset_t *cold_reset,
+		unsigned long arg)
+{
+	if (!cold_reset->reset_protection) {
+		if (IS_RST_PROT_EN_REQ(arg) && IS_SRC_VALID_PROT(arg)) {
+			pr_debug("%s:req - reset protection enable\n", __func__);
+		} else if (IS_CLD_RST_REQ(arg) && IS_SRC_VALID(arg)) {
+			pr_debug("%s:req - cold reset\n", __func__);
+		} else if (IS_RST_PROT_DIS_REQ(arg) && IS_SRC_VALID_PROT(arg)) {
+			pr_debug("%s:req - reset protection already disable\n", __func__);
+			return -EINVAL;
+		} else {
+			pr_err("%s:Operation not permitted \n", __func__);
+			return -EPERM;
+		}
+	} else {
+		if (IS_RST_PROT_DIS_REQ(arg)
+		    && IS_SRC(arg, cold_reset->rst_prot_src)) {
+			pr_debug("%s:req - disable reset protection from same src\n", __func__);
+		} else if (IS_CLD_RST_REQ(arg)
+			   && IS_SRC(arg, cold_reset->rst_prot_src)) {
+			pr_debug("%s:req - cold reset from same source\n", __func__);
+		} else if (IS_RST_PROT_EN_REQ(arg)
+			   && IS_SRC(arg, cold_reset->rst_prot_src)) {
+			pr_debug("%s:request - enable reset protection from same source\n", __func__);
+		} else {
+			pr_err("%s: Operation not permitted \n", __func__);
+			return -EPERM;
+		}
+	}
+	return 0;
+}
+
+static int perform_cold_reset_protection(nfc_dev_t *nfc_dev, unsigned long arg)
+{
+	int ret = 0;
+	struct file filp;
+	cold_reset_t *cold_reset = &nfc_dev->cold_reset;
+	bool nfc_dev_opened = false;
+
+	/*check if NFCC not in the FW download or hard reset state */
+	ret = validate_nfc_state_nci(nfc_dev);
+	if (ret < 0) {
+		pr_err("%s: invalid cmd", __func__);
+		return ret;
+	}
+
+	/* check if NFC is enabled */
+	mutex_lock(&nfc_dev->dev_ref_mutex);
+	nfc_dev_opened = (nfc_dev->dev_ref_count > 0) ? true : false;
+	mutex_unlock(&nfc_dev->dev_ref_mutex);
+
+	mutex_lock(&cold_reset->sync_mutex);
+	/*check if NFCC not in the FW download or hard reset state */
+	ret = validate_cold_reset_protection_request(cold_reset, arg);
+	if (ret < 0) {
+		pr_err("%s: invalid cmd", __func__);
+		goto err;
+	}
+
+	/*check if cold reset already in progress */
+	if (IS_CLD_RST_REQ(arg) && cold_reset->in_progress) {
+		pr_err("%s: cold reset already in progress", __func__);
+		ret = -EBUSY;
+		goto err;
+	}
+	/* set default value for status as failure */
+	cold_reset->status = -EIO;
+	cold_reset->rsp_pending = true;
+
+	/*enable interrupt before sending cmd, when devnode not opened by HAL */
+	if (!nfc_dev_opened)
+		nfc_dev->nfc_enable_intr(nfc_dev);
+
+	ret = send_cold_reset_protection_cmd(nfc_dev, IS_RST_PROT_REQ(arg));
+	if (ret < 0) {
+		pr_err("failed to send cold reset/protection command\n");
+		cold_reset->rsp_pending = false;
+		goto err;
+	}
+	ret = 0;
+	/*start the cold reset guard timer */
+	if (IS_CLD_RST_REQ(arg)) {
+		/*Guard timer not needed when OSU over NFC*/
+		if(!(cold_reset->reset_protection && IS_SRC_NFC(arg))) {
+			ret = start_cold_reset_guard_timer(cold_reset);
+			if (ret) {
+				pr_err("%s: Error in mod_timer\n", __func__);
+				goto err;
+			}
+		}
+	}
+
+	do {
+		/* Read is pending from the HAL service which will complete the response */
+		if (nfc_dev_opened) {
+			if (!wait_event_interruptible_timeout
+			    (cold_reset->read_wq,
+			     cold_reset->rsp_pending == false,
+			     msecs_to_jiffies(NCI_CMD_RSP_TIMEOUT))) {
+				pr_err("%s:cold reset/protection response timeout\n", __func__);
+				ret = -EAGAIN;
+			}
+		} else {
+			/* Read data as NFC thread is not active */
+			filp.private_data = nfc_dev;
+#if IS_ENABLED(CONFIG_NXP_NFC_I2C)
+			if (nfc_dev->interface == PLATFORM_IF_I2C) {
+				filp.f_flags &= ~O_NONBLOCK;
+				ret = nfc_i2c_dev_read(&filp, NULL, 3, 0);
+				usleep_range(3500, 4000);
+			}
+#endif //IS_ENABLED(CONFIG_NXP_NFC_I2C)
+		}
+	} while (ret == -ERESTARTSYS || ret == -EFAULT);
+
+	if (ret == 0) {		/* success case */
+		ret = cold_reset->status;
+		if (IS_RST_PROT_REQ(arg)) {
+			cold_reset->reset_protection = IS_RST_PROT_EN_REQ(arg);
+			cold_reset->rst_prot_src =
+				IS_RST_PROT_EN_REQ(arg) ? GET_SRC(arg) : SRC_NONE;
+			/* wait for reboot guard timer */
+		} else if (wait_event_interruptible_timeout
+			   (cold_reset->read_wq, true,
+			    msecs_to_jiffies(ESE_CLD_RST_REBOOT_GUARD_TIME)) ==
+			   0) {
+			pr_info("%s: reboot guard timer timeout", __func__);
+		}
+	}
+err:
+	mutex_unlock(&cold_reset->sync_mutex);
+	return ret;
+}
+
+/*
+ * Power management of the eSE
+ * eSE and NFCC both are powered using VEN gpio,
+ * VEN HIGH - eSE and NFCC both are powered on
+ * VEN LOW - eSE and NFCC both are power down
+ */
+int nfc_ese_pwr(nfc_dev_t *nfc_dev, unsigned long arg)
+{
+	int ret = 0;
+	if (arg == ESE_POWER_ON) {
+		/**
+		 * Let's store the NFC VEN pin state
+		 * will check stored value in case of eSE power off request,
+		 * to find out if NFC MW also sent request to set VEN HIGH
+		 * VEN state will remain HIGH if NFC is enabled otherwise
+		 * it will be set as LOW
+		 */
+		nfc_dev->nfc_ven_enabled = gpio_get_value(nfc_dev->gpio.ven);
+		if (!nfc_dev->nfc_ven_enabled) {
+			pr_debug("eSE HAL service setting ven HIGH\n");
+			gpio_set_ven(nfc_dev, 1);
+		} else {
+			pr_debug("ven already HIGH\n");
+		}
+	} else if (arg == ESE_POWER_OFF) {
+		if (!nfc_dev->nfc_ven_enabled) {
+			pr_debug("NFC not enabled, disabling ven\n");
+			gpio_set_ven(nfc_dev, 0);
+		} else {
+			pr_debug("keep ven high as NFC is enabled\n");
+		}
+	} else if (arg == ESE_POWER_STATE) {
+		// eSE get power state
+		ret = gpio_get_value(nfc_dev->gpio.ven);
+	} else if (IS_CLD_RST_REQ(arg) || IS_RST_PROT_REQ(arg)) {
+		ret = perform_cold_reset_protection(nfc_dev, arg);
+	} else {
+		pr_err("%s bad arg %lu\n", __func__, arg);
+		ret = -ENOIOCTLCMD;
+	}
+	return ret;
+}
+
+EXPORT_SYMBOL(nfc_ese_pwr);
+
+#define ESE_LEGACY_INTERFACE
+#ifdef ESE_LEGACY_INTERFACE
+static nfc_dev_t *nfc_dev_legacy = NULL;
+
+/******************************************************************************
+ * perform_ese_cold_reset() - It shall be called by others driver(not nfc/ese)
+ * to perform cold reset only
+ * @arg: request of cold reset from other drivers should be ESE_CLD_RST_OTHER
+ *
+ * Returns:- 0 in case of sucess and negative values in case of failure
+ *****************************************************************************/
+int perform_ese_cold_reset(unsigned long arg)
+{
+	int ret = 0;
+	if (nfc_dev_legacy) {
+		if (IS_CLD_RST_REQ(arg) && IS_SRC_OTHER(arg)) {
+			ret = nfc_ese_pwr(nfc_dev_legacy, arg);
+		} else {
+			pr_err("%s :  Operation not permitted \n", __func__);
+			return -EPERM;
+		}
+	}
+	pr_debug("%s:%d exit, status:%lu", __func__, arg, ret);
+	return ret;
+}
+
+EXPORT_SYMBOL(perform_ese_cold_reset);
+#endif //ESE_LEGACY_INTERFACE
+
+void common_ese_on_hard_reset(nfc_dev_t *nfc_dev)
+{
+	cold_reset_t *cold_reset = &nfc_dev->cold_reset;
+	cold_reset->rsp_pending = false;
+	cold_reset->in_progress = false;
+	if (timer_pending(&cold_reset->timer) == 1) {
+		del_timer(&cold_reset->timer);
+	}
+}
+
+void common_ese_init(nfc_dev_t *nfc_dev)
+{
+	cold_reset_t *cold_reset = &nfc_dev->cold_reset;
+	cold_reset->reset_protection = false;
+	cold_reset->rst_prot_src = SRC_NONE;
+	init_waitqueue_head(&cold_reset->read_wq);
+	mutex_init(&cold_reset->sync_mutex);
+	common_ese_on_hard_reset(nfc_dev);
+#ifdef ESE_LEGACY_INTERFACE
+	nfc_dev_legacy = nfc_dev;
+#endif //ESE_LEGACY_INTERFACE
+}
+
+void common_ese_exit(nfc_dev_t *nfc_dev)
+{
+	mutex_destroy(&nfc_dev->cold_reset.sync_mutex);
+#ifdef ESE_LEGACY_INTERFACE
+	nfc_dev_legacy = NULL;
+#endif //ESE_LEGACY_INTERFACE
+}

+ 97 - 0
nfc/common_ese.h

@@ -0,0 +1,97 @@
+/******************************************************************************
+ *  Copyright (C) 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.
+ *
+ * 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 _COMMON_ESE_H_
+#define _COMMON_ESE_H_
+
+#include "common.h"
+
+/*nci prop msg 1st byte*/
+#define NCI_PROP_MSG_GID                    0x0F
+#define NCI_PROP_MSG_CMD                    (NCI_MSG_CMD | NCI_PROP_MSG_GID)
+#define NCI_PROP_MSG_RSP                    (NCI_MSG_RSP | NCI_PROP_MSG_GID)
+
+/*nci prop msg 2nd byte*/
+#define CLD_RST_OID                         0x1E
+#define RST_PROT_OID                        0x1F
+
+/*nci prop msg 3rd byte*/
+#define CLD_RST_PAYLOAD_SIZE                0x00
+#define RST_PROT_PAYLOAD_SIZE               0x01
+
+/*nci prop msg response length*/
+#define NCI_PROP_MSG_RSP_LEN                0x04
+
+/*cold reset guard time to allow back to back cold reset after some time*/
+#define ESE_CLD_RST_GUARD_TIME              (3000)	//3s
+/*guard time to reboot after reset*/
+#define ESE_CLD_RST_REBOOT_GUARD_TIME       (50)	//50ms
+/*sources of reset protection and cold reset*/
+typedef enum reset_source {
+	SRC_SPI = 0,
+	SRC_NFC = 0x10,
+	SRC_OTHER = 0x20,
+	SRC_NONE = 0x80,
+} reset_source_t;
+
+enum ese_ioctl_request {
+	ESE_POWER_ON = 0,	/* eSE POWER ON */
+	ESE_POWER_OFF,		/* eSE POWER OFF */
+	ESE_POWER_STATE,	/* eSE GET POWER STATE */
+
+	/*ese reset requests from eSE service/hal/driver */
+	ESE_CLD_RST,		/* eSE COLD RESET */
+	ESE_RST_PROT_EN,	/* eSE RESET PROTECTION ENABLE */
+	ESE_RST_PROT_DIS,	/* eSE RESET PROTECTION DISABLE */
+
+	/*similar ese reset requests from nfc service/hal/driver */
+	ESE_CLD_RST_NFC = ESE_CLD_RST | SRC_NFC,
+	ESE_RST_PROT_EN_NFC = ESE_RST_PROT_EN | SRC_NFC,
+	ESE_RST_PROT_DIS_NFC = ESE_RST_PROT_DIS | SRC_NFC,
+
+	/*similar ese reset requests from other service/hal/driver */
+	ESE_CLD_RST_OTHER = ESE_CLD_RST | SRC_OTHER,
+};
+
+#define GET_SRC(arg) (arg & 0xF0)
+#define IS_SRC(arg, src) (GET_SRC(arg) == src)
+#define IS_SRC_SPI(arg) IS_SRC(arg, SRC_SPI)
+#define IS_SRC_NFC(arg) IS_SRC(arg, SRC_NFC)
+#define IS_SRC_OTHER(arg) IS_SRC(arg, SRC_OTHER)
+#define IS_SRC_VALID(arg) (IS_SRC_SPI(arg) || IS_SRC_NFC(arg) ||  \
+                           IS_SRC_OTHER(arg))
+#define IS_SRC_VALID_PROT(arg) (IS_SRC_SPI(arg) || IS_SRC_NFC(arg))
+
+#define IS_RST(arg, type) ((arg & 0xF) == type)
+#define IS_CLD_RST_REQ(arg) IS_RST(arg, ESE_CLD_RST)
+#define IS_RST_PROT_EN_REQ(arg) IS_RST(arg, ESE_RST_PROT_EN)
+#define IS_RST_PROT_DIS_REQ(arg) IS_RST(arg, ESE_RST_PROT_DIS)
+#define IS_RST_PROT_REQ(arg) (IS_RST_PROT_EN_REQ(arg) || IS_RST_PROT_DIS_REQ(arg))
+/* This macro evaluates to 1 if prop cmd response is received */
+#define IS_PROP_CMD_RSP(buf)                                                                          \
+                ((NCI_PROP_MSG_RSP == buf[0]) && ((CLD_RST_OID == buf[1]) ||     \
+                 (RST_PROT_OID == buf[1])))
+
+void wakeup_on_prop_rsp(nfc_dev_t *nfc_dev, uint8_t *buf);
+int nfc_ese_pwr(nfc_dev_t *nfc_dev, unsigned long arg);
+void common_ese_on_hard_reset(nfc_dev_t *nfc_dev);
+void common_ese_init(nfc_dev_t *nfc_dev);
+void common_ese_exit(nfc_dev_t *nfc_dev);
+
+#endif /* _COMMON_ESE_H_ */

+ 467 - 0
nfc/i2c_drv.c

@@ -0,0 +1,467 @@
+/******************************************************************************
+ *  Copyright (C) 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.
+ *
+ * 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
+ *
+ ******************************************************************************/
+/*
+ * 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/fs.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/uaccess.h>
+#include <linux/slab.h>
+#include "common.h"
+#include "common_ese.h"
+/**
+ * i2c_disable_irq()
+ *
+ * Check if interrupt is disabled or not
+ * and disable interrupt
+ *
+ * Return: int
+ */
+int i2c_disable_irq(struct nfc_dev *dev)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&dev->i2c_dev.irq_enabled_lock, flags);
+	if (dev->i2c_dev.irq_enabled) {
+		disable_irq_nosync(dev->i2c_dev.client->irq);
+		dev->i2c_dev.irq_enabled = false;
+	}
+	spin_unlock_irqrestore(&dev->i2c_dev.irq_enabled_lock, flags);
+
+	return 0;
+}
+
+/**
+ * i2c_enable_irq()
+ *
+ * Check if interrupt is enabled or not
+ * and enable interrupt
+ *
+ * Return: int
+ */
+int i2c_enable_irq(struct nfc_dev *dev)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&dev->i2c_dev.irq_enabled_lock, flags);
+	if (!dev->i2c_dev.irq_enabled) {
+		dev->i2c_dev.irq_enabled = true;
+		enable_irq(dev->i2c_dev.client->irq);
+	}
+	spin_unlock_irqrestore(&dev->i2c_dev.irq_enabled_lock, flags);
+
+	return 0;
+}
+
+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;
+	if (device_may_wakeup(&i2c_dev->client->dev))
+		pm_wakeup_event(&i2c_dev->client->dev, WAKEUP_SRC_TIMEOUT);
+
+	i2c_disable_irq(nfc_dev);
+	wake_up(&nfc_dev->read_wq);
+
+	return IRQ_HANDLED;
+}
+
+int i2c_read(struct nfc_dev *dev, char *buf, size_t count)
+{
+	int ret;
+	pr_debug("%s : reading %zu bytes.\n", __func__, count);
+	/* Read data */
+	ret = i2c_master_recv(dev->i2c_dev.client, buf, count);
+	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;
+	}
+err:
+	return ret;
+}
+
+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(1000, 1100);
+		} 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;
+	char tmp[MAX_BUFFER_SIZE];
+	nfc_dev_t *nfc_dev = filp->private_data;
+	i2c_dev_t *i2c_dev = &nfc_dev->i2c_dev;
+
+	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_dev->gpio.irq)) {
+		if (filp->f_flags & O_NONBLOCK) {
+			pr_err(":f_falg has O_NONBLOCK. EAGAIN\n");
+			ret = -EAGAIN;
+			goto err;
+		}
+		while (1) {
+			ret = 0;
+			if (!i2c_dev->irq_enabled) {
+				i2c_dev->irq_enabled = true;
+				enable_irq(i2c_dev->client->irq);
+			}
+			if (!gpio_get_value(nfc_dev->gpio.irq)) {
+				ret = wait_event_interruptible(nfc_dev->read_wq,
+							       !i2c_dev->
+							       irq_enabled);
+
+				if (ret) {
+					pr_err("error wakeup of read wq\n");
+					goto err;
+				}
+			}
+			i2c_disable_irq(nfc_dev);
+
+			if (gpio_get_value(nfc_dev->gpio.irq))
+				break;
+			if (!gpio_get_value(nfc_dev->gpio.ven)) {
+				pr_info("%s: releasing read\n", __func__);
+				ret = -EIO;
+				goto err;
+			}
+			pr_warn("%s: spurious interrupt detected\n", __func__);
+		}
+	}
+
+	memset(tmp, 0x00, count);
+	/* Read data */
+	ret = i2c_read(nfc_dev, tmp, count);
+	if (ret <= 0) {
+		pr_err("%s: i2c_read returned %d\n", __func__, ret);
+		goto err;
+	}
+	/* check if it's response of cold reset command
+	 * NFC HAL process shouldn't receive this data as
+	 * command was sent by driver
+	 */
+	if (nfc_dev->cold_reset.rsp_pending) {
+		if (IS_PROP_CMD_RSP(tmp)) {
+			/* Read data */
+			ret =
+				i2c_read(nfc_dev, &tmp[NCI_PAYLOAD_IDX],
+					 tmp[NCI_PAYLOAD_LEN_IDX]);
+			if (ret <= 0) {
+				pr_err("%s: failure to read prop cold reset/protection rsp header\n", __func__);
+				goto err;
+			}
+			wakeup_on_prop_rsp(nfc_dev, tmp);
+			mutex_unlock(&nfc_dev->read_mutex);
+			/*
+			 * NFC process doesn't know about cold reset command
+			 * being sent as it was initiated by eSE process
+			 * we shouldn't return any data to NFC process
+			 */
+			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_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;
+
+	if (count > MAX_DL_BUFFER_SIZE)
+		count = MAX_DL_BUFFER_SIZE;
+
+	if (copy_from_user(tmp, buf, count)) {
+		pr_err("%s : failed to copy from user space\n", __func__);
+		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;
+	}
+
+	return ret;
+}
+
+static const struct file_operations nfc_i2c_dev_fops = {
+	.owner = THIS_MODULE,
+	.llseek = no_llseek,
+	.read = nfc_i2c_dev_read,
+	.write = nfc_i2c_dev_write,
+	.open = nfc_dev_open,
+	.release = nfc_dev_close,
+	.unlocked_ioctl = nfc_dev_ioctl,
+};
+
+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_gpio_t nfc_gpio;
+	pr_debug("%s: enter\n", __func__);
+	/*retrive details of gpios from dt */
+	ret = nfc_parse_dt(&client->dev, &nfc_gpio, PLATFORM_IF_I2C);
+	if (ret) {
+		pr_err("%s : failed to parse dt\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_t), GFP_KERNEL);
+	if (nfc_dev == NULL) {
+		ret = -ENOMEM;
+		goto err;
+	}
+	nfc_dev->interface = PLATFORM_IF_I2C;
+	nfc_dev->nfc_state = NFC_STATE_NCI;
+	nfc_dev->i2c_dev.client = client;
+	i2c_dev = &nfc_dev->i2c_dev;
+	nfc_dev->nfc_read = i2c_read;
+	nfc_dev->nfc_write = i2c_write;
+	nfc_dev->nfc_enable_intr = i2c_enable_irq;
+	nfc_dev->nfc_disable_intr = i2c_disable_irq;
+
+	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);
+		goto err;
+	}
+	ret = configure_gpio(nfc_gpio.irq, GPIO_IRQ);
+	if (ret <= 0) {
+		pr_err("%s: unable to request nfc irq gpio [%d]\n", __func__, nfc_gpio.irq);
+		goto err;
+	}
+	client->irq = ret;
+	ret = configure_gpio(nfc_gpio.dwl_req, GPIO_OUTPUT);
+	if (ret) {
+		pr_err("%s: unable to request nfc firm downl gpio [%d]\n", __func__,
+		       nfc_gpio.dwl_req);
+	}
+
+	/*copy the retrived gpio details from DT */
+	memcpy(&nfc_dev->gpio, &nfc_gpio, sizeof(struct platform_gpio));
+
+	/* init mutex and queues */
+	init_waitqueue_head(&nfc_dev->read_wq);
+	mutex_init(&nfc_dev->read_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,
+				NFC_CHAR_DEV_NAME, CLASS_NAME);
+	if (ret) {
+		pr_err("%s: nfc_misc_register failed\n", __func__);
+		goto err_mutex_destroy;
+	}
+	/* interrupt initializations */
+	pr_info("%s : requesting IRQ %d\n", __func__, client->irq);
+	i2c_dev->irq_enabled = true;
+	ret = request_irq(client->irq, i2c_irq_handler,
+			  IRQF_TRIGGER_HIGH, client->name, nfc_dev);
+	if (ret) {
+		pr_err("%s: request_irq failed\n", __func__);
+		goto err_nfc_misc_unregister;
+	}
+	i2c_disable_irq(nfc_dev);
+	device_init_wakeup(&client->dev, true);
+	device_set_wakeup_capable(&client->dev, true);
+	i2c_set_clientdata(client, nfc_dev);
+	i2c_dev->irq_wake_up = false;
+
+	//reset nfc
+	usleep_range(10000, 10100);
+	gpio_set_value(nfc_dev->gpio.ven, 1);
+	usleep_range(10000, 10100);
+
+	pr_info("%s probing nfc i2c successfully", __func__);
+	return 0;
+err_nfc_misc_unregister:
+	nfc_misc_unregister(nfc_dev, DEV_COUNT);
+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);
+err:
+	gpio_free_all(nfc_dev);
+	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;
+	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;
+		return ret;
+	}
+	if (nfc_dev->dev_ref_count > 0) {
+		pr_err("%s: device already in use\n", __func__);
+		return -EBUSY;
+	}
+	device_init_wakeup(&client->dev, false);
+	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);
+	gpio_free_all(nfc_dev);
+	kfree(nfc_dev);
+	return ret;
+}
+
+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;
+
+	if (device_may_wakeup(&client->dev) && i2c_dev->irq_enabled) {
+		if (!enable_irq_wake(client->irq))
+			i2c_dev->irq_wake_up = true;
+	}
+	return 0;
+}
+
+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;
+
+	if (device_may_wakeup(&client->dev) && i2c_dev->irq_wake_up) {
+		if (!disable_irq_wake(client->irq))
+			i2c_dev->irq_wake_up = false;
+	}
+	return 0;
+}
+
+static const struct i2c_device_id nfc_i2c_dev_id[] = {
+	{NFC_I2C_DEV_ID, 0},
+	{}
+};
+
+static const struct of_device_id nfc_i2c_dev_match_table[] = {
+	{.compatible = NFC_I2C_DRV_STR,},
+	{}
+};
+
+static const struct dev_pm_ops nfc_i2c_dev_pm_ops = {
+	SET_SYSTEM_SLEEP_PM_OPS(nfc_i2c_dev_suspend, nfc_i2c_dev_resume)
+};
+
+static struct i2c_driver nfc_i2c_dev_driver = {
+	.id_table = nfc_i2c_dev_id,
+	.probe = nfc_i2c_dev_probe,
+	.remove = nfc_i2c_dev_remove,
+	.driver = {
+		.name = NFC_I2C_DRV_STR,
+		.pm = &nfc_i2c_dev_pm_ops,
+		.of_match_table = nfc_i2c_dev_match_table,
+		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
+	},
+};
+
+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)
+		pr_err("NFC I2C add driver error ret %d\n", ret);
+	return ret;
+}
+
+module_init(nfc_i2c_dev_init);
+
+static void __exit nfc_i2c_dev_exit(void)
+{
+	pr_info("Unloading NXP NFC I2C driver\n");
+	i2c_del_driver(&nfc_i2c_dev_driver);
+}
+
+module_exit(nfc_i2c_dev_exit);
+
+MODULE_DESCRIPTION("NXP NFC I2C driver");
+MODULE_LICENSE("GPL");

+ 25 - 19
pn553-i2c/cold_reset.h → nfc/i2c_drv.h

@@ -1,5 +1,5 @@
 /******************************************************************************
- *  Copyright (C) 2020 NXP
+ *  Copyright (C) 2019-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
@@ -16,24 +16,30 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  ******************************************************************************/
-#ifndef _NFC_COMMON_H_
-#define _NFC_COMMON_H_
+#ifndef _I2C_DRV_H_
+#define _I2C_DRV_H_
+#include <linux/i2c.h>
 
-#define MSG_NFCC_RSP              0x40
-#define MSG_PROP_GID              0x0F
-#define ESE_CLD_RST_OID           0x1E
-#define RST_PROTECTION_CMD_INDEX  0x03
+/*kept same as dts */
+#define NFC_I2C_DRV_STR     "nxp,pn544"
+#define NFC_I2C_DEV_ID      "pn553"
 
-#define RST_PROTECTION_OID        0x1F
-#define RST_PROTECTION_ENABLED    0x08
+//Interface specific parameters
+typedef struct i2c_dev {
+	struct i2c_client *client;
+	/*IRQ parameters */
+	bool irq_enabled;
+	spinlock_t irq_enabled_lock;
+	/* NFC_IRQ wake-up state */
+	bool irq_wake_up;
+} i2c_dev_t;
 
-typedef enum ese_cold_reset_origin {
-    ESE_COLD_RESET_NOT_REQUESTED = 0x00,
-    ESE_COLD_RESET_SOURCE_NFC = 0x01,
-    ESE_COLD_RESET_SOURCE_SPI = 0x02,
-    ESE_COLD_RESET_SOURCE_UWB = 0x04,
-}ese_cold_reset_origin_t;
-
-void ese_reset_resource_init(void);
-void ese_reset_resource_destroy(void);
-#endif /* _NFC_COMMON_H_ */
+long nfc_i2c_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg);
+int nfc_i2c_dev_probe(struct i2c_client *client,
+		      const struct i2c_device_id *id);
+int nfc_i2c_dev_remove(struct i2c_client *client);
+int nfc_i2c_dev_suspend(struct device *device);
+int nfc_i2c_dev_resume(struct device *device);
+ssize_t nfc_i2c_dev_read(struct file *filp, char __user *buf, size_t count,
+			 loff_t * offset);
+#endif //_I2C_DRV_H_

+ 0 - 13
pn553-i2c/Kconfig

@@ -1,13 +0,0 @@
-#
-# Nxp Nci protocol (I2C) devices
-#
-
-config NFC_PN553_DEVICES
-	bool "Nxp pn553 NCI protocol driver (I2C) devices"
-	default y
-	---help---
-	  You'll have to say Y if your computer contains an I2C device that
-	  you want to use under Linux.
-
-	  You can say N here if you don't have any SPI connected to your computer.
-

+ 0 - 9
pn553-i2c/Makefile

@@ -1,9 +0,0 @@
-#
-# Makefile for nfc devices
-#
-
-obj-$(CONFIG_NFC_PN553_DEVICES)     += pn553.o
-obj-$(CONFIG_NFC_PN553_DEVICES)     += cold_reset.o
-
-ccflags-$(CONFIG_NFC_PN553_DEVICES) := -DDEBUG
-

+ 0 - 343
pn553-i2c/cold_reset.c

@@ -1,343 +0,0 @@
-/******************************************************************************
- *  Copyright (C) 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.
- *
- * 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/i2c.h>
-#include <linux/irq.h>
-#include <linux/jiffies.h>
-#include <linux/delay.h>
-#include <linux/spinlock.h>
-#include <linux/version.h>
-#include <linux/fs.h>
-#include "cold_reset.h"
-#include "pn553.h"
-
-/*ESE_COLD_RESET MACROS */
-#define MAX_BUFFER_SIZE 512 /* copied from pn553.c */
-#define MSG_NFCC_CMD         0x20
-#define NCI_PROP_RST_RSP_SIZE 0x04
-
-
-/* Evaluates to 1 If cold reset is in progress or the guard timer is still running */
-#define IS_COLD_RESET_REQ_IN_PROGRESS(flags)                                         \
-               (flags & (MASK_ESE_COLD_RESET | MASK_ESE_COLD_RESET_GUARD_TIMER))
-
-#define IS_RESET_PROTECTION_ENABLED(flags) (flags & RST_PROTECTION_ENABLED)
-
-#define IS_COLD_RESET_ALLOWED(flags, src)  (!IS_COLD_RESET_REQ_IN_PROGRESS(flags)    \
-                && (!IS_RESET_PROTECTION_ENABLED(flags) || src == ESE_COLD_RESET_SOURCE_SPI))
-
-static struct pn544_dev *pn544_dev;
-struct mutex        ese_cold_reset_sync_mutex;
-struct mutex        nci_send_cmd_mutex;
-
-extern ssize_t pn544_dev_read(struct file *filp, char __user *buf,
-        size_t count, loff_t *offset);
-
-static int8_t prop_nci_rsp[NCI_PROP_RST_RSP_SIZE];
-static struct timer_list ese_cold_reset_timer;
-static struct completion prop_cmd_resp_sema;
-static struct completion ese_cold_reset_sema;
-
-#if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
-static void ese_cold_reset_gaurd_timer_callback(unsigned long data);
-#else
-static void ese_cold_reset_gaurd_timer_callback(struct timer_list *unused);
-#endif
-static long start_ese_cold_reset_guard_timer(void);
-
-extern struct pn544_dev * get_nfcc_dev_data(void);
-
-#if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
-static void ese_cold_reset_gaurd_timer_callback(unsigned long data)
-{
-    (void)data;
-#else
-static void ese_cold_reset_gaurd_timer_callback(struct timer_list *unused)
-{
-#endif
-    pr_info("%s: Enter\n",__func__);
-    pn544_dev->state_flags &= ~MASK_ESE_COLD_RESET_GUARD_TIMER;
-    return;
-}
-
-static long start_ese_cold_reset_guard_timer(void)
-{
-    long ret = -EINVAL;
-    printk( KERN_INFO "starting ese_cold_reset_timer \n");
-    if(timer_pending(&ese_cold_reset_timer) == 1)
-    {
-        pr_info("ese_cold_reset_guard_timer: delete pending timer \n");
-        /* delete timer if already pending */
-        del_timer(&ese_cold_reset_timer);
-    }
-    pn544_dev->state_flags |= MASK_ESE_COLD_RESET_GUARD_TIMER;
-#if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
-    init_timer(&ese_cold_reset_timer);
-    setup_timer( &ese_cold_reset_timer, ese_cold_reset_gaurd_timer_callback, 0 );
-#else
-    timer_setup(&ese_cold_reset_timer, ese_cold_reset_gaurd_timer_callback, 0);
-#endif
-    ret = mod_timer(&ese_cold_reset_timer, jiffies + msecs_to_jiffies(ESE_COLD_RESET_GUARD_TIME));
-    if (ret)
-      printk( KERN_INFO "%s: Error in mod_timer\n",__func__);
-    return ret;
-}
-void ese_reset_resource_init(void) {
-    mutex_init(&ese_cold_reset_sync_mutex);
-    mutex_init(&nci_send_cmd_mutex);
-}
-void ese_reset_resource_destroy(void) {
-    mutex_destroy(&ese_cold_reset_sync_mutex);
-    mutex_destroy(&nci_send_cmd_mutex);
-}
-void rcv_prop_resp_status(const char * const buf)
-{
-    int ret = -1;
-    char tmp[MAX_BUFFER_SIZE];
-    size_t rcount = 0;
-    memset(&prop_nci_rsp, 0, sizeof(prop_nci_rsp));
-    memcpy(prop_nci_rsp, buf, 3);
-    rcount = (size_t)prop_nci_rsp[2];
-
-    /* Read data: No need to wait for the interrupt */
-    ret = i2c_master_recv(pn544_dev->client, tmp, rcount);
-    if(ret == rcount){
-        prop_nci_rsp[3] = tmp[0];
-        pr_info("%s NxpNciR : len = 4 > %02X%02X%02X%02X\n", __func__,prop_nci_rsp[0],
-                prop_nci_rsp[1],prop_nci_rsp[2],prop_nci_rsp[3]);
-    }else{
-        pr_err("%s : Failed to receive payload of the cold_rst_cmd\n",__func__);
-        prop_nci_rsp[3] = -EIO;
-    }
-    if(pn544_dev->state_flags &(P544_FLAG_NFC_ON)){
-        complete(&prop_cmd_resp_sema);
-    }
-}
-
-/******************************************************************************
- * Function    : send_nci_transceive
- *
- * Description : Common NCI command send utility function.
- *
- * Parameters  : prop_cmd      : Data to be sent to NFCC
- *               prop_cmd_size : Length of the data to be sent
- *
- * Returns     : 0 (SUCCESS)/ (-1)otherwise
-
- *****************************************************************************/
-static int send_nci_transceive(uint8_t *prop_cmd, size_t prop_cmd_size) {
-    int ret = 0;
-    unsigned int loop=0x03;
-    struct file filp = {NULL};
-    int retry = 1;
-
-    pr_info("%s: Enter", __func__);
-
-    filp.private_data = pn544_dev;
-    if(pn544_dev->state_flags & P544_FLAG_FW_DNLD) {
-      /* If FW DNLD, Operation is not permitted */
-      pr_err("%s : Operation is not permitted during fwdnld\n", __func__);
-      return -ECANCELED;
-    }
-    mutex_lock(&nci_send_cmd_mutex);
-    init_completion(&prop_cmd_resp_sema);
-    /* write command to I2C line*/
-    do {
-      ret = i2c_master_send(pn544_dev->client, prop_cmd, prop_cmd_size);
-      if (ret == prop_cmd_size) {
-        break;
-      }
-      usleep_range(5000, 6000);
-    } while(loop--);
-    if(!loop && (ret != prop_cmd_size)) {
-      pr_err("%s : i2c_master_send returned %d\n", __func__, ret);
-      mutex_unlock(&nci_send_cmd_mutex);
-      return -EREMOTEIO;
-    }
-    ret = 0x00;
-
-    do {
-      if(pn544_dev->state_flags & P544_FLAG_NFC_ON)/* NFC_ON */ {
-        /* Read is pending from the NFC service which will complete the prop_cmd_resp_sema */
-        if(wait_for_completion_timeout(&prop_cmd_resp_sema,
-            msecs_to_jiffies(NCI_CMD_RSP_TIMEOUT)) == 0){
-          pr_err("%s: Timeout", __func__);
-          ret = prop_nci_rsp[3] = -EAGAIN; // Failure case
-        }
-      } else { /* NFC_OFF */
-        /* call the pn544_dev_read() */
-        filp.f_flags &= ~O_NONBLOCK;
-        ret = pn544_dev_read(&filp, NULL,3, 0);
-        if(!ret)
-          break;
-        usleep_range(3500, 4000);
-      }
-    } while((retry-- >= 0) && ret == -ERESTARTSYS);
-
-    mutex_unlock(&nci_send_cmd_mutex);
-    if(0x00 == ret && prop_nci_rsp[3])
-        ret = -1 * prop_nci_rsp[3];
-    /* Return the status to the SPI/UWB Driver */
-    pr_info("%s: exit, Status:%d", __func__, ret);
-    return ret;
-}
-
-/******************************************************************************
- * Function    : do_reset_protection
- *
- * Description : It shall be called by SPI driver to enable/disable reset
- *               protection
- *
- * Parameters  : Enable(TRUE)/Disable(FALSE)
- *
- * Returns     :
- *     0           :    OK             < Success case >
- *    -EPERM(-1)   :    REJECTED       < NFCC rejects the cold reset cmd>
- *    -3           :    FAILED         < NFCC responds to cold reset cmd>
- *    -EIO(-5)     :    SYNTAX_ERROR   < NFCC cmd framing is wrong >
- *    -6           :    SEMANTIC_ERROR < NFCC rsp to cold reset cmd >
- *    -9           :    INAVLID_PARAM  < NFCC rsp to cold reset cmd >
- *    -EAGAIN(-11) :    < 1. mod_timer(): temp error during kernel alloc >
- *                      < 2. No rsp received from NFCC for cold reset cmd >
- *    -ENOMEM(-12) :    < mod_timer(): failed to allocate memory >
- *    -EINVAL(-22) :    < 1. cold rst req is received from unknown source >
- *                      < 2. mod_timer(): invalid arg is passed>
- *    -EREMOTEIO(-121): < Reset cmd write failure over I2C >
- *    -ECANCELED(-125): < FW DWNLD is going on so driver canceled operation >
- *    -ERESTARTSYS(-512): < Userspace process is restarted during read operation >
- *****************************************************************************/
-int do_reset_protection(bool type) {
-    int ret = 0;
-    uint8_t prop_cmd[] = {0x2F, 0x1F, 0x01, 0x00};
-    pn544_dev = get_nfcc_dev_data();
-
-    pr_info("%s: Enter cmd type: %d", __func__, type);
-
-    prop_cmd[RST_PROTECTION_CMD_INDEX] = (type) ? 1 : 0;
-
-    if(type ) {
-      pn544_dev->state_flags |= RST_PROTECTION_ENABLED;
-    } else {
-      if(!(pn544_dev->state_flags & RST_PROTECTION_ENABLED)) {
-        return ret;
-      }
-    }
-    pr_info("%s: NxpNciX: %d > %02X%02X%02X%02X \n", __func__, ret,prop_cmd[0],
-             prop_cmd[1],prop_cmd[2],prop_cmd[3]);
-
-    ret = send_nci_transceive(prop_cmd, sizeof(prop_cmd));
-    if(ret) {
-      pr_err("%s : send_nci_command returned %d\n", __func__, ret);
-    }
-    if(!type) {
-      pn544_dev->state_flags &= ~RST_PROTECTION_ENABLED;
-    }
-    pr_info("%s: exit, Status:%d state_flag : %x ", __func__, ret,
-            pn544_dev->state_flags);
-    return ret;
-}
-EXPORT_SYMBOL(do_reset_protection);
-
-/******************************************************************************
- * Function    : ese_cold_reset
- *
- * Description : It shall be called by NFC/SPI/UWB driver to perform driver to
- *               to driver eSE cold reset.
- *
- * Parameters  : src Source of the cold reset request
- *
- * Returns     :
- *     0           :    OK             < Success case >
- *    -EPERM(-1)   :    REJECTED       < Guard timer running>
- *    -3           :    FAILED         < NFCC responds to cold reset cmd>
- *    -EIO(-5)     :    SYNTAX_ERROR   < NFCC cmd framing is wrong >
- *    -6           :    SEMANTIC_ERROR < NFCC rsp to cold reset cmd >
- *    -9           :    INAVLID_PARAM  < NFCC rsp to cold reset cmd >
- *    -EAGAIN(-11) :    < 1. mod_timer(): temp error during kernel alloc >
- *                      < 2. No rsp received from NFCC for cold reset cmd >
- *    -ENOMEM(-12) :    < mod_timer(): failed to allocate memory >
- *    -EBUSY(-16)  :    < eSE busy, in updater mode>
- *    -EINVAL(-22) :    < 1. cold rst req is received from unknown source >
- *                      < 2. mod_timer(): invalid arg is passed>
- *    -EREMOTEIO(-121): < Reset cmd write failure over I2C >
- *    -ECANCELED(-125): < FW DWNLD is going on so driver canceled operation >
- *    -ERESTARTSYS(-512): < Userspace process is restarted during read operation >
- *****************************************************************************/
-int ese_cold_reset(ese_cold_reset_origin_t src)
-{
-    int ret = 0;
-    uint8_t ese_cld_reset[] = {0x2F, 0x1E, 0x00};
-
-    pr_info("%s: Enter origin:%d", __func__, src);
-
-    switch(src) {
-    case ESE_COLD_RESET_SOURCE_NFC:
-    case ESE_COLD_RESET_SOURCE_SPI:
-    case ESE_COLD_RESET_SOURCE_UWB:
-      break;
-    default:
-      pr_info("%s: Invalid argument", __func__);
-      return -EINVAL;
-    }
-    pn544_dev = get_nfcc_dev_data();
-    mutex_lock(&ese_cold_reset_sync_mutex);
-    if(IS_COLD_RESET_ALLOWED(pn544_dev->state_flags, src)) {
-      ret = start_ese_cold_reset_guard_timer();
-      if(ret) {
-        mutex_unlock(&ese_cold_reset_sync_mutex);
-        return ret; /* EAGAIN/EINVAL/ENOMEM*/
-      }
-      pn544_dev->state_flags |= src << ESE_COLD_RESET_ORIGIN_FLAGS_POS;
-      init_completion(&ese_cold_reset_sema);
-      pr_info("%s: NxpNciX: %d > %02X%02X%02X \n", __func__, ret,ese_cld_reset[0],
-              ese_cld_reset[1],ese_cld_reset[2]);
-      ret = send_nci_transceive(ese_cld_reset, sizeof(ese_cld_reset));
-      if(ret) {
-        pn544_dev->state_flags &= ~(MASK_ESE_COLD_RESET | MASK_ESE_COLD_RESET_GUARD_TIMER);
-        mutex_unlock(&ese_cold_reset_sync_mutex);
-        return ret;
-      }
-      /* wait for reboot guard timer*/
-      if(!ret && wait_for_completion_timeout(&ese_cold_reset_sema,
-          msecs_to_jiffies(ESE_COLD_RESET_REBOOT_GUARD_TIME)) == 0){
-        pr_info("%s: guard Timeout", __func__);
-      }
-    } else {
-        if(IS_RESET_PROTECTION_ENABLED(pn544_dev->state_flags)) {
-          pr_err("%s :  Not allowed resource busy \n", __func__);
-          ret = -EBUSY;
-        }
-        else if(IS_COLD_RESET_REQ_IN_PROGRESS(pn544_dev->state_flags)) {
-          pr_err("%s :  Operation not permitted \n", __func__);
-          ret = -EPERM;
-        }
-        else {
-          /*No Action required*/
-        }
-    }
-    pn544_dev->state_flags &= ~(src << ESE_COLD_RESET_ORIGIN_FLAGS_POS);
-    mutex_unlock(&ese_cold_reset_sync_mutex);
-
-    /* Return the status to the SPI/UWB Driver */
-    pr_info("%s:%d exit, Status:%d", __func__, src, ret);
-    return ret;
-}
-
-EXPORT_SYMBOL(ese_cold_reset);

+ 0 - 1611
pn553-i2c/pn553.c

@@ -1,1611 +0,0 @@
-/*
- * 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
- *
- */
-/******************************************************************************
- *
- *  The original Work has been changed by NXP Semiconductors.
- *
- *  Copyright (C) 2013-2020 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.
- *
- * 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/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/of_gpio.h>
-#include <linux/miscdevice.h>
-#include <linux/spinlock.h>
-#include <asm/siginfo.h>
-#include <linux/rcupdate.h>
-#include <linux/sched.h>
-#include <linux/signal.h>
-#include <linux/workqueue.h>
-/* HiKey Compilation fix */
-#define HiKey_620_COMPILATION_FIX 1
-#ifndef HiKey_620_COMPILATION_FIX
-#include <linux/wakelock.h>
-#endif
-
-#include <linux/version.h>
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
-#include <linux/sched/signal.h>
-#include <linux/fs.h>
-#endif
-
-#include <linux/timer.h>
-#include "pn553.h"
-#include "cold_reset.h"
-
-#define DRAGON_NFC 1
-#define SIG_NFC 44
-#define MAX_BUFFER_SIZE 554
-#define MAX_SECURE_SESSIONS 1
-
-/* This macro evaluates to 1 if the cold reset is requested by driver(SPI/UWB). */
-#define IS_PROP_CMD_REQUESTED(flags) (flags & (MASK_ESE_COLD_RESET | RST_PROTECTION_ENABLED))
-/* This macro evaluates to 1 if eSE cold reset response is received */
-#define IS_PROP_RSP(buf)                                                                          \
-                (((MSG_NFCC_RSP | MSG_PROP_GID) == buf[0]) && ((ESE_CLD_RST_OID == buf[1]) ||     \
-                 (RST_PROTECTION_OID == buf[1]) ))
-
-/* VEN is kept ON all the time if you define the macro VEN_ALWAYS_ON.
-Used for SN100 usecases */
-#define VEN_ALWAYS_ON
-/* Macro added to disable SVDD power toggling */
-/* #define JCOP_4X_VALIDATION */
-
-
-/* HiKey Compilation fix */
-#ifndef HiKey_620_COMPILATION_FIX
-struct wake_lock nfc_wake_lock;
-#if HWINFO
-struct hw_type_info hw_info;
-#endif
-static bool  sIsWakeLocked = false;
-#endif
-static struct pn544_dev *pn544_dev;
-static struct semaphore ese_access_sema;
-static struct semaphore svdd_sync_onoff_sema;
-static struct completion dwp_onoff_sema;
-static struct timer_list secure_timer;
-static void release_ese_lock(p61_access_state_t  p61_current_state);
-int get_ese_lock(p61_access_state_t  p61_current_state, int timeout);
-static long set_jcop_download_state(unsigned long arg);
-static long start_seccure_timer(unsigned long timer_value);
-static long secure_timer_operation(struct pn544_dev *pn544_dev, unsigned long arg);
-extern void rcv_prop_resp_status(const char * const buf);
-extern long ese_cold_reset(ese_cold_reset_origin_t src);
-extern void ese_reset_resource_init(void);
-extern void ese_reset_resource_destroy(void);
-#if HWINFO
-static void check_hw_info(void);
-#endif
-#define SECURE_TIMER_WORK_QUEUE "SecTimerCbWq"
-
-struct pn544_dev * get_nfcc_dev_data(void) {
-  return pn544_dev;
-}
-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);
-        disable_irq_wake(pn544_dev->client->irq);
-        pn544_dev->irq_enabled = false;
-    }
-    spin_unlock_irqrestore(&pn544_dev->irq_enabled_lock, flags);
-}
-
-static int pn544_dev_release(struct inode *inode, struct file *filp) {
-    pn544_dev->state_flags &= ~(P544_FLAG_NFC_VEN_RESET|P544_FLAG_NFC_ON|P544_FLAG_FW_DNLD);
-    if (pn544_dev->firm_gpio)
-        gpio_set_value(pn544_dev->firm_gpio, 0);
-    pr_info(KERN_ALERT "Exit %s: NFC driver release  nfc hal  \n", __func__);
-    return 0;
-}
-static irqreturn_t pn544_dev_irq_handler(int irq, void *dev_id)
-{
-    struct pn544_dev *pn544_dev = dev_id;
-
-    pn544_disable_irq(pn544_dev);
-    /* HiKey Compilation fix */
-    #ifndef HiKey_620_COMPILATION_FIX
-    if (sIsWakeLocked == false)
-    {
-        wake_lock(&nfc_wake_lock);
-        sIsWakeLocked = true;
-    } else {
-            pr_debug("%s already wake locked!\n", __func__);
-    }
-    #endif
-    /* Wake up waiting readers */
-    wake_up(&pn544_dev->read_wq);
-
-
-    return IRQ_HANDLED;
-}
-
-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;
-
-    if (count > MAX_BUFFER_SIZE)
-        count = MAX_BUFFER_SIZE;
-
-    //pr_debug("%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;
-        }
-
-        while (1) {
-            pn544_dev->irq_enabled = true;
-            enable_irq(pn544_dev->client->irq);
-            enable_irq_wake(pn544_dev->client->irq);
-            ret = wait_event_interruptible(
-                    pn544_dev->read_wq,
-                    !pn544_dev->irq_enabled);
-
-            pn544_disable_irq(pn544_dev);
-
-            if (ret)
-                goto fail;
-            if(pn544_dev->state_flags & P544_FLAG_NFC_VEN_RESET) {
-                pr_warning("%s: releasing read  \n", __func__);
-                pn544_dev->state_flags &= ~P544_FLAG_NFC_VEN_RESET;
-                ret =  -EL3RST;
-                goto fail;
-            }
-            if (gpio_get_value(pn544_dev->irq_gpio))
-                break;
-
-            pr_warning("%s: spurious interrupt detected\n", __func__);
-        }
-    }
-
-    /* Read data */
-    ret = i2c_master_recv(pn544_dev->client, tmp, count);
-    #ifndef HiKey_620_COMPILATION_FIX
-    /* HiKey Compilation fix */
-    if (sIsWakeLocked == true) {
-        wake_unlock(&nfc_wake_lock);
-        sIsWakeLocked = false;
-    }
-    #endif
-
-
-    /* If ese cold reset has been requested then read the response */
-    if(IS_PROP_CMD_REQUESTED(pn544_dev->state_flags) && IS_PROP_RSP(tmp)) {
-      rcv_prop_resp_status(tmp);
-      /* Request is from driver, consume the response */
-      mutex_unlock(&pn544_dev->read_mutex);
-      return 0;
-    }
-    mutex_unlock(&pn544_dev->read_mutex);
-
-    /* pn544 seems to be slow in handling I2C read requests
-     * so add 1ms delay after recv operation */
-#if !NEXUS5x
-    udelay(1000);
-#endif
-
-    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;
-    }
-    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;
-
-    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;
-    }
-
-    //pr_debug("%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;
-    }
-    /* pn544 seems to be slow in handling I2C write requests
-     * so add 1ms delay after I2C send oparation */
-    udelay(1000);
-
-    return ret;
-}
-
-static void p61_update_access_state(struct pn544_dev *pn544_dev, p61_access_state_t current_state, bool set)
-{
-    pr_info("%s: Enter current_state = %x\n", __func__, pn544_dev->p61_current_state);
-    if (current_state)
-    {
-        if(set){
-            if(pn544_dev->p61_current_state == P61_STATE_IDLE)
-                pn544_dev->p61_current_state = P61_STATE_INVALID;
-            pn544_dev->p61_current_state |= current_state;
-        }
-        else{
-            pn544_dev->p61_current_state ^= current_state;
-            if(!pn544_dev->p61_current_state)
-                pn544_dev->p61_current_state = P61_STATE_IDLE;
-        }
-    }
-    pr_info("%s: Exit current_state = %x\n", __func__, pn544_dev->p61_current_state);
-}
-
-static void p61_get_access_state(struct pn544_dev *pn544_dev, p61_access_state_t *current_state)
-{
-
-    if (current_state == NULL) {
-        //*current_state = P61_STATE_INVALID;
-        pr_err("%s : invalid state of p61_access_state_t current state  \n", __func__);
-    } else {
-        *current_state = pn544_dev->p61_current_state;
-    }
-}
-static void p61_access_lock(struct pn544_dev *pn544_dev)
-{
-    mutex_lock(&pn544_dev->p61_state_mutex);
-}
-static void p61_access_unlock(struct pn544_dev *pn544_dev)
-{
-    mutex_unlock(&pn544_dev->p61_state_mutex);
-}
-
-static int signal_handler(p61_access_state_t state, long nfc_pid)
-{
-    struct siginfo sinfo;
-    pid_t pid;
-    struct task_struct *task;
-    int sigret = 0, ret = 0;
-    pr_info("%s: Enter\n", __func__);
-
-    if(nfc_pid == 0)
-    {
-        pr_info("nfc_pid is clear don't call signal_handler.\n");
-    }
-    else
-    {
-        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.\n", task->comm);
-            sigret = force_sig_info(SIG_NFC, &sinfo, task);
-            if(sigret < 0){
-                pr_info("send_sig_info failed..... sigret %d.\n", sigret);
-                ret = -1;
-                //msleep(60);
-            }
-        }
-        else{
-             pr_info("finding task from PID failed\r\n");
-             ret = -1;
-        }
-    }
-    pr_info("%s: Exit ret = %d\n", __func__, ret);
-    return ret;
-}
-static void svdd_sync_onoff(long nfc_service_pid, p61_access_state_t origin)
-{
-    int timeout = 100; //100 ms timeout
-    unsigned long tempJ = msecs_to_jiffies(timeout);
-    pr_info("%s: Enter nfc_service_pid: %ld\n", __func__, nfc_service_pid);
-    if(nfc_service_pid)
-    {
-        if (0 == signal_handler(origin, nfc_service_pid))
-        {
-            sema_init(&svdd_sync_onoff_sema, 0);
-            pr_info("Waiting for svdd protection response");
-            if(down_timeout(&svdd_sync_onoff_sema, tempJ) != 0)
-            {
-                pr_info("svdd wait protection: Timeout");
-            }
-            pr_info("svdd wait protection : released");
-        }
-    }
-}
-static int release_svdd_wait(void)
-{
-    pr_info("%s: Enter \n", __func__);
-    up(&svdd_sync_onoff_sema);
-    return 0;
-}
-
-static void dwp_OnOff(long nfc_service_pid, p61_access_state_t origin)
-{
-    int timeout = 100; //100 ms timeout
-    unsigned long tempJ = msecs_to_jiffies(timeout);
-    if(nfc_service_pid)
-    {
-        if (0 == signal_handler(origin, nfc_service_pid))
-        {
-            init_completion(&dwp_onoff_sema);
-            if(wait_for_completion_timeout(&dwp_onoff_sema, tempJ) != 0)
-            {
-                pr_info("Dwp On/off wait protection: Timeout");
-            }
-            pr_info("Dwp On/Off wait protection : released");
-        }
-    }
-}
-static int release_dwpOnOff_wait(void)
-{
-    pr_info("%s: Enter \n", __func__);
-    complete(&dwp_onoff_sema);
-    return 0;
-}
-
-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;
-    pn544_dev->state_flags |= (P544_FLAG_NFC_ON);
-    pr_debug("%s : %d,%d\n", __func__, imajor(inode), iminor(inode));
-
-    return 0;
-}
-
-static int set_nfc_pid(unsigned long arg)
-{
-    pr_info("%s : The NFC Service PID is %ld\n", __func__, arg);
-    pn544_dev->nfc_service_pid = arg;
-    return 0;
-}
-
-long  pn544_dev_ioctl(struct file *filp, unsigned int cmd,
-        unsigned long arg)
-{
-    /* Free pass autobahn area, not protected. Use it carefullly. START */
-    switch(cmd)
-    {
-        case P544_GET_ESE_ACCESS:
-            return get_ese_lock(P61_STATE_WIRED, arg);
-        break;
-        case P544_REL_SVDD_WAIT:
-            return release_svdd_wait();
-        break;
-        case P544_SET_NFC_SERVICE_PID:
-            return set_nfc_pid(arg);
-        break;
-        case P544_REL_DWPONOFF_WAIT:
-            return release_dwpOnOff_wait();
-        break;
-        default:
-        break;
-    }
-    /* Free pass autobahn area, not protected. Use it carefullly. END */
-
-    p61_access_lock(pn544_dev);
-    switch (cmd) {
-    case PN544_SET_PWR:
-    {
-        p61_access_state_t current_state = P61_STATE_INVALID;
-        p61_get_access_state(pn544_dev, &current_state);
-        if (arg == 2) {
-            if (current_state & (P61_STATE_SPI|P61_STATE_SPI_PRIO) && (pn544_dev->chip_pwr_scheme != PN80T_EXT_PMU_SCHEME))
-            {
-                /* NFCC fw/download should not be allowed if p61 is used
-                 * by SPI
-                 */
-                pr_info("%s NFCC should not be allowed to reset/FW download \n", __func__);
-                p61_access_unlock(pn544_dev);
-                return -EBUSY; /* Device or resource busy */
-            }
-            pn544_dev->nfc_ven_enabled = true;
-            if ((pn544_dev->spi_ven_enabled == false && !(pn544_dev->secure_timer_cnt))
-            || (pn544_dev->chip_pwr_scheme == PN80T_EXT_PMU_SCHEME))
-            {
-                /* power on with firmware download (requires hw reset)
-                 */
-                pr_info("%s power on with firmware\n", __func__);
-                gpio_set_value(pn544_dev->ven_gpio, 1);
-                msleep(10);
-                if (pn544_dev->firm_gpio) {
-                    p61_update_access_state(pn544_dev, P61_STATE_DWNLD, true);
-                    gpio_set_value(pn544_dev->firm_gpio, 1);
-                    pn544_dev->state_flags |= (P544_FLAG_FW_DNLD);
-                }
-
-                msleep(10);
-                gpio_set_value(pn544_dev->ven_gpio, 0);
-                msleep(10);
-                gpio_set_value(pn544_dev->ven_gpio, 1);
-                msleep(10);
-            }
-        } else if (arg == 1) {
-            /* power on */
-            if (pn544_dev->firm_gpio) {
-                if ((current_state & (P61_STATE_WIRED|P61_STATE_SPI|P61_STATE_SPI_PRIO))== 0){
-                    p61_update_access_state(pn544_dev, P61_STATE_IDLE, true);
-                }
-                if(current_state & P61_STATE_DWNLD){
-                    p61_update_access_state(pn544_dev, P61_STATE_DWNLD, false);
-                }
-                gpio_set_value(pn544_dev->firm_gpio, 0);
-                pn544_dev->state_flags &= ~(P544_FLAG_FW_DNLD);
-            }
-
-            pn544_dev->nfc_ven_enabled = true;
-            #ifndef VEN_ALWAYS_ON
-            if (pn544_dev->spi_ven_enabled == false || (pn544_dev->chip_pwr_scheme == PN80T_EXT_PMU_SCHEME)) {
-                gpio_set_value(pn544_dev->ven_gpio, 1);
-            }
-            #endif
-        } else if (arg == 0) {
-            /* power off */
-            if (pn544_dev->firm_gpio) {
-                if ((current_state & (P61_STATE_WIRED|P61_STATE_SPI|P61_STATE_SPI_PRIO))== 0){
-                    p61_update_access_state(pn544_dev, P61_STATE_IDLE, true);
-                }
-                gpio_set_value(pn544_dev->firm_gpio, 0);
-            }
-
-            pn544_dev->nfc_ven_enabled = false;
-            /* Don't change Ven state if spi made it high */
-            #ifndef VEN_ALWAYS_ON
-            if ((pn544_dev->spi_ven_enabled == false && !(pn544_dev->secure_timer_cnt))
-            || (pn544_dev->chip_pwr_scheme == PN80T_EXT_PMU_SCHEME)) {
-                gpio_set_value(pn544_dev->ven_gpio, 0);
-            }
-            #endif
-            /* HiKey Compilation fix */
-            #ifndef HiKey_620_COMPILATION_FIX
-            if (sIsWakeLocked == true) {
-                wake_unlock(&nfc_wake_lock);
-                sIsWakeLocked = false;
-            }
-            #endif
-        } else if (arg == 3) {
-            /*NFC Service called ISO-RST*/
-            p61_access_state_t current_state = P61_STATE_INVALID;
-            p61_get_access_state(pn544_dev, &current_state);
-            if(current_state & (P61_STATE_SPI|P61_STATE_SPI_PRIO)) {
-                p61_access_unlock(pn544_dev);
-                return -EPERM; /* Operation not permitted */
-            }
-            if(current_state & P61_STATE_WIRED) {
-                p61_update_access_state(pn544_dev, P61_STATE_WIRED, false);
-            }
-#ifdef ISO_RST
-            gpio_set_value(pn544_dev->iso_rst_gpio, 0);
-            msleep(50);
-            gpio_set_value(pn544_dev->iso_rst_gpio, 1);
-            msleep(50);
-            pr_info("%s ISO RESET from DWP DONE\n", __func__);
-#endif
-        } else if (arg == 4) {
-            pr_info("%s FW dwldioctl called from NFC \n", __func__);
-            /*NFC Service called FW dwnld*/
-            if (pn544_dev->firm_gpio) {
-                p61_update_access_state(pn544_dev, P61_STATE_DWNLD, true);
-                gpio_set_value(pn544_dev->firm_gpio, 1);
-                pn544_dev->state_flags |= (P544_FLAG_FW_DNLD);
-                msleep(10);
-            }
-        } else if (arg == 5) {
-            pn544_dev->state_flags |= P544_FLAG_NFC_VEN_RESET;
-            pn544_disable_irq(pn544_dev);
-            wake_up(&pn544_dev->read_wq);
-            msleep(10);
-            gpio_set_value(pn544_dev->ven_gpio, 0);
-            msleep(10);
-            gpio_set_value(pn544_dev->ven_gpio, 1);
-            msleep(10);
-            pr_info("%s VEN reset DONE >>>>>>>\n", __func__);
-        }  else if (arg == 6) {
-            if (pn544_dev->firm_gpio) {
-                gpio_set_value(pn544_dev->firm_gpio, 0);
-                pn544_dev->state_flags &= ~(P544_FLAG_FW_DNLD);
-            }
-            pr_info("%s FW GPIO set to 0x00 >>>>>>>\n", __func__);
-        }else {
-            pr_err("%s bad arg %lu\n", __func__, arg);
-            /* changed the p61 state to idle*/
-            p61_access_unlock(pn544_dev);
-            return -EINVAL;
-        }
-    }
-    break;
-    case P61_SET_SPI_PWR:
-    {
-        p61_access_state_t current_state = P61_STATE_INVALID;
-        p61_get_access_state(pn544_dev, &current_state);
-        if (arg == 1) {
-            pr_info("%s : PN61_SET_SPI_PWR - power on ese\n", __func__);
-            if ((current_state & (P61_STATE_SPI|P61_STATE_SPI_PRIO)) == 0)
-            {
-                p61_update_access_state(pn544_dev, P61_STATE_SPI, true);
-                /*To handle triple mode protection signal
-                NFC service when SPI session started*/
-                if (!(current_state & P61_STATE_JCP_DWNLD)){
-                    if(pn544_dev->nfc_service_pid){
-                        pr_info("nfc service pid %s   ---- %ld", __func__, pn544_dev->nfc_service_pid);
-                        /*signal_handler(P61_STATE_SPI, pn544_dev->nfc_service_pid);*/
-                        dwp_OnOff(pn544_dev->nfc_service_pid, P61_STATE_SPI);
-                    }
-                    else{
-                        pr_info(" invalid nfc service pid....signalling failed%s   ---- %ld", __func__, pn544_dev->nfc_service_pid);
-                    }
-                }
-                pn544_dev->spi_ven_enabled = true;
-
-                if(pn544_dev->chip_pwr_scheme == PN80T_EXT_PMU_SCHEME)
-                    break;
-                #ifndef VEN_ALWAYS_ON
-                if (pn544_dev->nfc_ven_enabled == false)
-                {
-                    /* provide power to NFCC if, NFC service not provided */
-                    gpio_set_value(pn544_dev->ven_gpio, 1);
-                    msleep(10);
-                }
-                #endif
-                /* pull the gpio to high once NFCC is power on*/
-                gpio_set_value(pn544_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_info("%s : PN61_SET_SPI_PWR -  power on ese failed \n", __func__);
-                p61_access_unlock(pn544_dev);
-                return -EBUSY; /* Device or resource busy */
-            }
-        } else if (arg == 0) {
-            pr_info("%s : PN61_SET_SPI_PWR - power off ese\n", __func__);
-            if(current_state & P61_STATE_SPI_PRIO){
-                p61_update_access_state(pn544_dev, P61_STATE_SPI_PRIO, false);
-                if (!(current_state & P61_STATE_JCP_DWNLD))
-                {
-                    if(pn544_dev->nfc_service_pid){
-                        pr_info("nfc service pid %s   ---- %ld", __func__, pn544_dev->nfc_service_pid);
-                        if(!(current_state & P61_STATE_WIRED))
-                        {
-                            svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_START |
-                                                     P61_STATE_SPI_PRIO_END);
-                        }else {
-                            signal_handler(P61_STATE_SPI_PRIO_END, pn544_dev->nfc_service_pid);
-                        }
-                    }
-                    else{
-                        pr_info(" invalid nfc service pid....signalling failed%s   ---- %ld", __func__, pn544_dev->nfc_service_pid);
-                    }
-                } else if (!(current_state & P61_STATE_WIRED)) {
-                    svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_START);
-                }
-                pn544_dev->spi_ven_enabled = false;
-
-                if(pn544_dev->chip_pwr_scheme == PN80T_EXT_PMU_SCHEME)
-                    break;
-
-                /* 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(pn544_dev->secure_timer_cnt)
-                    usleep_range(25000, 30000);
-
-                if (!(current_state & P61_STATE_WIRED) && !(pn544_dev->secure_timer_cnt))
-                {
-#ifndef JCOP_4X_VALIDATION
-                    gpio_set_value(pn544_dev->ese_pwr_gpio, 0);
-                    /* Delay (2.5ms) after SVDD_PWR_OFF for the shutdown settlement time */
-                    usleep_range(2500, 3000);
-#endif
-                    svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_END);
-                }
-#ifndef JCOP_4X_VALIDATION
-                #ifndef VEN_ALWAYS_ON
-                if ((pn544_dev->nfc_ven_enabled == false) && !(pn544_dev->secure_timer_cnt)) {
-                     gpio_set_value(pn544_dev->ven_gpio, 0);
-                     msleep(10);
-                }
-                #endif
-#endif
-              }else if(current_state & P61_STATE_SPI){
-                  p61_update_access_state(pn544_dev, P61_STATE_SPI, false);
-                  if (!(current_state & P61_STATE_WIRED) &&
-                      (pn544_dev->chip_pwr_scheme != PN80T_EXT_PMU_SCHEME) &&
-                      !(current_state & P61_STATE_JCP_DWNLD))
-                  {
-                      if(pn544_dev->nfc_service_pid){
-                          pr_info("nfc service pid %s   ---- %ld", __func__, pn544_dev->nfc_service_pid);
-                          svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_START | P61_STATE_SPI_END);
-                       }
-                       else{
-                           pr_info(" invalid nfc service pid....signalling failed%s   ---- %ld", __func__, pn544_dev->nfc_service_pid);
-                       }
-                       /* 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(pn544_dev->secure_timer_cnt)
-                            usleep_range(25000, 30000);
-
-                      if (!(pn544_dev->secure_timer_cnt)) {
-#ifndef JCOP_4X_VALIDATION
-                          gpio_set_value(pn544_dev->ese_pwr_gpio, 0);
-                          /* Delay (2.5ms) after SVDD_PWR_OFF for the shutdown settlement time */
-                          usleep_range(2500, 3000);
-#endif
-                          svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_END);
-                       }
-                  }
-                  /*If JCOP3.2 or 3.3 for handling triple mode
-                  protection signal NFC service */
-                  else
-                  {
-                      if (!(current_state & P61_STATE_JCP_DWNLD))
-                      {
-                          if(pn544_dev->nfc_service_pid){
-                              pr_info("nfc service pid %s   ---- %ld", __func__, pn544_dev->nfc_service_pid);
-                              if(pn544_dev->chip_pwr_scheme == PN80T_LEGACY_PWR_SCHEME)
-                              {
-                                  svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_START | P61_STATE_SPI_END);
-                              } else {
-                                  signal_handler(P61_STATE_SPI_END, pn544_dev->nfc_service_pid);
-                              }
-                           }
-                           else{
-                               pr_info(" invalid nfc service pid....signalling failed%s   ---- %ld", __func__, pn544_dev->nfc_service_pid);
-                           }
-                      } else if (pn544_dev->chip_pwr_scheme == PN80T_LEGACY_PWR_SCHEME) {
-                          svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_START);
-                      }
-                      if(pn544_dev->chip_pwr_scheme == PN80T_LEGACY_PWR_SCHEME)
-                      {
-#ifndef JCOP_4X_VALIDATION
-                          gpio_set_value(pn544_dev->ese_pwr_gpio, 0);
-#endif
-                          svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_END);
-                          pr_info("PN80T legacy ese_pwr_gpio off %s", __func__);
-                      }
-                  }
-                  pn544_dev->spi_ven_enabled = false;
-#ifndef VEN_ALWAYS_ON
-                  if (pn544_dev->nfc_ven_enabled == false && (pn544_dev->chip_pwr_scheme != PN80T_EXT_PMU_SCHEME)
-                       && !(pn544_dev->secure_timer_cnt)) {
-                      gpio_set_value(pn544_dev->ven_gpio, 0);
-                      msleep(10);
-                  }
-#endif
-            } else {
-                pr_err("%s : PN61_SET_SPI_PWR - failed, current_state = %x \n",
-                        __func__, pn544_dev->p61_current_state);
-                p61_access_unlock(pn544_dev);
-                return -EPERM; /* Operation not permitted */
-            }
-        }else if (arg == 2) {
-            pr_info("%s : PN61_SET_SPI_PWR - reset\n", __func__);
-            if (current_state & (P61_STATE_IDLE|P61_STATE_SPI|P61_STATE_SPI_PRIO)) {
-                if (pn544_dev->spi_ven_enabled == false)
-                {
-                    pn544_dev->spi_ven_enabled = true;
-                    #ifndef VEN_ALWAYS_ON
-                    if ((pn544_dev->nfc_ven_enabled == false) && (pn544_dev->chip_pwr_scheme != PN80T_EXT_PMU_SCHEME)) {
-                        /* provide power to NFCC if, NFC service not provided */
-                        gpio_set_value(pn544_dev->ven_gpio, 1);
-                        msleep(10);
-                    }
-                    #endif
-                }
-                if(pn544_dev->chip_pwr_scheme != PN80T_EXT_PMU_SCHEME  && !(pn544_dev->secure_timer_cnt))
-                {
-                    svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_START);
-#ifndef JCOP_4X_VALIDATION
-                    gpio_set_value(pn544_dev->ese_pwr_gpio, 0);
-#endif
-                    svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_END);
-                    msleep(10);
-                    if(!gpio_get_value(pn544_dev->ese_pwr_gpio))
-                        gpio_set_value(pn544_dev->ese_pwr_gpio, 1);
-                    msleep(10);
-                }
-            } else {
-                pr_info("%s : PN61_SET_SPI_PWR - reset  failed \n", __func__);
-                p61_access_unlock(pn544_dev);
-                return -EBUSY; /* Device or resource busy */
-            }
-        }else if (arg == 3) {
-            int ret = ese_cold_reset(ESE_COLD_RESET_SOURCE_NFC);
-            p61_access_unlock(pn544_dev);
-            return ret;
-        }else if (arg == 4) {
-            if (current_state & P61_STATE_SPI_PRIO)
-            {
-                pr_info("%s : PN61_SET_SPI_PWR - Prio Session Ending...\n", __func__);
-                p61_update_access_state(pn544_dev, P61_STATE_SPI_PRIO, false);
-                /*after SPI prio timeout, the state is changing from SPI prio to SPI */
-                p61_update_access_state(pn544_dev, P61_STATE_SPI, true);
-                if (current_state & P61_STATE_WIRED)
-                {
-                    if(pn544_dev->nfc_service_pid){
-                        pr_info("nfc service pid %s   ---- %ld", __func__, pn544_dev->nfc_service_pid);
-                        signal_handler(P61_STATE_SPI_PRIO_END, pn544_dev->nfc_service_pid);
-                    }
-                    else{
-                        pr_info(" invalid nfc service pid....signalling failed%s   ---- %ld", __func__, pn544_dev->nfc_service_pid);
-                    }
-               }
-            }
-            else
-            {
-                pr_info("%s : PN61_SET_SPI_PWR -  Prio Session End failed \n", __func__);
-                p61_access_unlock(pn544_dev);
-                return -EBADRQC; /* Device or resource busy */
-            }
-        } else if(arg == 5){
-            release_ese_lock(P61_STATE_SPI);
-        } else if (arg == 6) {
-            /*SPI Service called ISO-RST*/
-            p61_access_state_t current_state = P61_STATE_INVALID;
-            p61_get_access_state(pn544_dev, &current_state);
-            if(current_state & P61_STATE_WIRED) {
-                p61_access_unlock(pn544_dev);
-                return -EPERM; /* Operation not permitted */
-            }
-            if(current_state & P61_STATE_SPI) {
-                p61_update_access_state(pn544_dev, P61_STATE_SPI, false);
-            }else if(current_state & P61_STATE_SPI_PRIO) {
-                p61_update_access_state(pn544_dev, P61_STATE_SPI_PRIO, false);
-            }
-#ifdef ISO_RST
-            gpio_set_value(pn544_dev->iso_rst_gpio, 0);
-            msleep(50);
-            gpio_set_value(pn544_dev->iso_rst_gpio, 1);
-            msleep(50);
-            pr_info("%s ISO RESET from SPI DONE\n", __func__);
-#endif
-        }
-        else {
-            pr_info("%s bad ese pwr arg %lu\n", __func__, arg);
-            p61_access_unlock(pn544_dev);
-            return -EBADRQC; /* Invalid request code */
-        }
-    }
-    break;
-
-    case P61_GET_PWR_STATUS:
-    {
-        p61_access_state_t current_state = P61_STATE_INVALID;
-        p61_get_access_state(pn544_dev, &current_state);
-        pr_info("%s: P61_GET_PWR_STATUS  = %x",__func__, current_state);
-        put_user(current_state, (int __user *)arg);
-    }
-    break;
-
-    case PN544_SET_DWNLD_STATUS:
-    {
-        long ret;
-        ret = set_jcop_download_state(arg);
-        if(ret < 0)
-        {
-            p61_access_unlock(pn544_dev);
-            return ret;
-        }
-    }
-    break;
-
-    case P61_SET_WIRED_ACCESS:
-    {
-        p61_access_state_t current_state = P61_STATE_INVALID;
-        p61_get_access_state(pn544_dev, &current_state);
-        if (arg == 1)
-        {
-            if (current_state)
-            {
-                pr_info("%s : P61_SET_WIRED_ACCESS - enabling\n", __func__);
-                p61_update_access_state(pn544_dev, P61_STATE_WIRED, true);
-                if (current_state & P61_STATE_SPI_PRIO)
-                {
-                    if(pn544_dev->nfc_service_pid){
-                        pr_info("nfc service pid %s   ---- %ld", __func__, pn544_dev->nfc_service_pid);
-                        signal_handler(P61_STATE_SPI_PRIO, pn544_dev->nfc_service_pid);
-                    }
-                    else{
-                        pr_info(" invalid nfc service pid....signalling failed%s   ---- %ld", __func__, pn544_dev->nfc_service_pid);
-                    }
-                }
-                if((current_state & (P61_STATE_SPI|P61_STATE_SPI_PRIO)) == 0 && (pn544_dev->chip_pwr_scheme == PN67T_PWR_SCHEME))
-                    gpio_set_value(pn544_dev->ese_pwr_gpio, 1);
-            } else {
-                pr_info("%s : P61_SET_WIRED_ACCESS -  enabling failed \n", __func__);
-                p61_access_unlock(pn544_dev);
-                return -EBUSY; /* Device or resource busy */
-            }
-        } else if (arg == 0) {
-            pr_info("%s : P61_SET_WIRED_ACCESS - disabling \n", __func__);
-            if (current_state & P61_STATE_WIRED){
-                p61_update_access_state(pn544_dev, P61_STATE_WIRED, false);
-                if((current_state & (P61_STATE_SPI|P61_STATE_SPI_PRIO)) == 0 && (pn544_dev->chip_pwr_scheme == PN67T_PWR_SCHEME))
-                {
-                    svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_START);
-                    gpio_set_value(pn544_dev->ese_pwr_gpio, 0);
-                    svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_END);
-                }
-            } else {
-                pr_err("%s : P61_SET_WIRED_ACCESS - failed, current_state = %x \n",
-                        __func__, pn544_dev->p61_current_state);
-                p61_access_unlock(pn544_dev);
-                return -EPERM; /* Operation not permitted */
-            }
-        }
-        else if(arg == 2)
-        {
-             pr_info("%s : P61_ESE_GPIO_LOW  \n", __func__);
-             if(pn544_dev->chip_pwr_scheme == PN67T_PWR_SCHEME)
-             {
-                 svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_START);
-                 gpio_set_value(pn544_dev->ese_pwr_gpio, 0);
-                 svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_END);
-             }
-        }
-        else if(arg == 3)
-        {
-            pr_info("%s : P61_ESE_GPIO_HIGH  \n", __func__);
-            if(pn544_dev->chip_pwr_scheme == PN67T_PWR_SCHEME)
-            gpio_set_value(pn544_dev->ese_pwr_gpio, 1);
-        }
-        else if(arg == 4)
-        {
-            release_ese_lock(P61_STATE_WIRED);
-        }
-        else {
-             pr_info("%s P61_SET_WIRED_ACCESS - bad arg %lu\n", __func__, arg);
-             p61_access_unlock(pn544_dev);
-             return -EBADRQC; /* Invalid request code */
-        }
-    }
-    break;
-    case P544_SET_POWER_SCHEME:
-    {
-        if(arg == PN67T_PWR_SCHEME)
-        {
-            pn544_dev->chip_pwr_scheme = PN67T_PWR_SCHEME;
-            pr_info("%s : The power scheme is set to PN67T legacy \n", __func__);
-        }
-        else if(arg == PN80T_LEGACY_PWR_SCHEME)
-        {
-            pn544_dev->chip_pwr_scheme = PN80T_LEGACY_PWR_SCHEME;
-            pr_info("%s : The power scheme is set to PN80T_LEGACY_PWR_SCHEME,\n", __func__);
-        }
-        else if(arg == PN80T_EXT_PMU_SCHEME)
-        {
-            pn544_dev->chip_pwr_scheme = PN80T_EXT_PMU_SCHEME;
-            pr_info("%s : The power scheme is set to PN80T_EXT_PMU_SCHEME,\n", __func__);
-        }
-        else
-        {
-            pr_info("%s : The power scheme is invalid,\n", __func__);
-        }
-    }
-    break;
-    case P544_SECURE_TIMER_SESSION:
-    {
-       secure_timer_operation(pn544_dev, arg);
-    }
-    break;
-    default:
-        pr_err("%s bad ioctl %u\n", __func__, cmd);
-        p61_access_unlock(pn544_dev);
-        return -EINVAL;
-    }
-    p61_access_unlock(pn544_dev);
-    return 0;
-}
-EXPORT_SYMBOL(pn544_dev_ioctl);
-
-static void secure_timer_workqueue(struct work_struct *Wq)
-{
-  p61_access_state_t current_state = P61_STATE_INVALID;
-  printk( KERN_INFO "secure_timer_callback: called (%lu).\n", jiffies);
-  /* Locking the critical section: ESE_PWR_OFF to allow eSE to shutdown peacefully :: START */
-  get_ese_lock(P61_STATE_WIRED, MAX_ESE_ACCESS_TIME_OUT_MS);
-  p61_update_access_state(pn544_dev, P61_STATE_SECURE_MODE, false);
-  p61_get_access_state(pn544_dev, &current_state);
-
-  if((current_state & (P61_STATE_SPI|P61_STATE_SPI_PRIO)) == 0)
-  {
-      printk( KERN_INFO "secure_timer_callback: make se_pwer_gpio low, state = %d", current_state);
-      gpio_set_value(pn544_dev->ese_pwr_gpio, 0);
-      /* Delay (2.5ms) after SVDD_PWR_OFF for the shutdown settlement time */
-      usleep_range(2500, 3000);
-      #ifndef VEN_ALWAYS_ON
-      if(pn544_dev->nfc_service_pid == 0x00)
-      {
-          gpio_set_value(pn544_dev->ven_gpio, 0);
-          printk( KERN_INFO "secure_timer_callback :make ven_gpio low, state = %d", current_state);
-      }
-      #endif
-  }
-  pn544_dev->secure_timer_cnt = 0;
-  /* Locking the critical section: ESE_PWR_OFF to allow eSE to shutdown peacefully :: END */
-  release_ese_lock(P61_STATE_WIRED);
-  return;
-}
-
-#if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
-static void secure_timer_callback( unsigned long data )
-{
-(void)data;
-#else
-static void secure_timer_callback(struct timer_list *unused)
-{
-#endif
-    /* Flush and push the timer callback event to the bottom half(work queue)
-    to be executed later, at a safer time */
-    flush_workqueue(pn544_dev->pSecureTimerCbWq);
-    queue_work(pn544_dev->pSecureTimerCbWq, &pn544_dev->wq_task);
-    return;
-}
-
-static long start_seccure_timer(unsigned long timer_value)
-{
-    long ret = -EINVAL;
-    pr_info("start_seccure_timer: enter\n");
-    /* Delete the timer if timer pending */
-    if(timer_pending(&secure_timer) == 1)
-    {
-        pr_info("start_seccure_timer: delete pending timer \n");
-        /* delete timer if already pending */
-        del_timer(&secure_timer);
-    }
-    /* Start the timer if timer value is non-zero */
-    if(timer_value)
-    {
-#if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
-        init_timer(&secure_timer);
-        setup_timer( &secure_timer, secure_timer_callback, 0 );
-#else
-        timer_setup(&secure_timer, secure_timer_callback, 0);
-#endif
-        pr_info("start_seccure_timer: timeout %lums (%lu)\n",timer_value, jiffies );
-        ret = mod_timer( &secure_timer, jiffies + msecs_to_jiffies(timer_value));
-        if (ret)
-            pr_info("start_seccure_timer: Error in mod_timer\n");
-    }
-    return ret;
-}
-
-static long secure_timer_operation(struct pn544_dev *pn544_dev, unsigned long arg)
-{
-    long ret = -EINVAL;
-    unsigned long timer_value =  arg;
-
-    printk( KERN_INFO "secure_timer_operation, %d\n",pn544_dev->chip_pwr_scheme);
-    if(pn544_dev->chip_pwr_scheme == PN80T_LEGACY_PWR_SCHEME)
-    {
-        ret = start_seccure_timer(timer_value);
-        if(!ret)
-        {
-            pn544_dev->secure_timer_cnt  = 1;
-            p61_update_access_state(pn544_dev, P61_STATE_SECURE_MODE, true);
-        }
-        else
-        {
-            pn544_dev->secure_timer_cnt  = 0;
-            p61_update_access_state(pn544_dev, P61_STATE_SECURE_MODE, false);
-            pr_info("%s :Secure timer reset \n", __func__);
-        }
-    }
-    else
-    {
-        pr_info("%s :Secure timer session not applicable  \n", __func__);
-    }
-    return ret;
-}
-
-static long set_jcop_download_state(unsigned long arg)
-{
-        p61_access_state_t current_state = P61_STATE_INVALID;
-        long ret = 0;
-        p61_get_access_state(pn544_dev, &current_state);
-        pr_info("%s:Enter PN544_SET_DWNLD_STATUS:JCOP Dwnld state arg = %ld",__func__, arg);
-        if(arg == JCP_DWNLD_INIT)
-        {
-            if(pn544_dev->nfc_service_pid)
-            {
-                pr_info("nfc service pid %s   ---- %ld", __func__, pn544_dev->nfc_service_pid);
-                signal_handler(JCP_DWNLD_INIT, pn544_dev->nfc_service_pid);
-            }
-            else
-            {
-                if (current_state & P61_STATE_JCP_DWNLD)
-                {
-                    ret = -EINVAL;
-                }
-                else
-                {
-                    p61_update_access_state(pn544_dev, P61_STATE_JCP_DWNLD, true);
-                }
-            }
-        }
-        else if (arg == JCP_DWNLD_START)
-        {
-            if (current_state & P61_STATE_JCP_DWNLD)
-            {
-                ret = -EINVAL;
-            }
-            else
-            {
-                p61_update_access_state(pn544_dev, P61_STATE_JCP_DWNLD, true);
-            }
-        }
-        else if (arg == JCP_SPI_DWNLD_COMPLETE)
-        {
-            if(pn544_dev->nfc_service_pid)
-            {
-                signal_handler(JCP_DWP_DWNLD_COMPLETE, pn544_dev->nfc_service_pid);
-            }
-            p61_update_access_state(pn544_dev, P61_STATE_JCP_DWNLD, false);
-        }
-        else if (arg == JCP_DWP_DWNLD_COMPLETE)
-        {
-            p61_update_access_state(pn544_dev, P61_STATE_JCP_DWNLD, false);
-        }
-        else
-        {
-            pr_info("%s bad ese pwr arg %lu\n", __func__, arg);
-            p61_access_unlock(pn544_dev);
-            return -EBADRQC; /* Invalid request code */
-        }
-        pr_info("%s: PN544_SET_DWNLD_STATUS  = %x",__func__, current_state);
-
-    return ret;
-}
-
-int get_ese_lock(p61_access_state_t  p61_current_state, int timeout)
-{
-    unsigned long tempJ = msecs_to_jiffies(timeout);
-    if(down_timeout(&ese_access_sema, tempJ) != 0)
-    {
-        printk("get_ese_lock: timeout p61_current_state = %d\n", p61_current_state);
-        return -EBUSY;
-    }
-    return 0;
-}
-EXPORT_SYMBOL(get_ese_lock);
-
-static void release_ese_lock(p61_access_state_t  p61_current_state)
-{
-    up(&ese_access_sema);
-}
-
-
-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,
-        .release = pn544_dev_release,
-        .unlocked_ioctl  = pn544_dev_ioctl,
-};
-#if DRAGON_NFC
-static int pn544_parse_dt(struct device *dev,
-    struct pn544_i2c_platform_data *data)
-{
-    struct device_node *np = dev->of_node;
-    int errorno = 0;
-
-#if !NEXUS5x
-        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;
-
-        data->ese_pwr_gpio = of_get_named_gpio(np, "nxp,pn544-ese-pwr", 0);
-        if ((!gpio_is_valid(data->ese_pwr_gpio)))
-                return -EINVAL;
-        data->iso_rst_gpio = of_get_named_gpio(np, "nxp,pn544-iso-pwr-rst", 0);
-        if ((!gpio_is_valid(data->iso_rst_gpio)))
-                return -EINVAL;
-#else
-        data->ven_gpio = of_get_named_gpio_flags(np,
-                                        "nxp,gpio_ven", 0, NULL);
-        data->firm_gpio = of_get_named_gpio_flags(np,
-                                        "nxp,gpio_mode", 0, NULL);
-        data->irq_gpio = of_get_named_gpio_flags(np,
-                                        "nxp,gpio_irq", 0, NULL);
-#endif
-    pr_info("%s: %d, %d, %d, %d, %d error:%d\n", __func__,
-        data->irq_gpio, data->ven_gpio, data->firm_gpio, data->iso_rst_gpio,
-        data->ese_pwr_gpio, errorno);
-
-    return errorno;
-}
-#endif
-
-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;
-
-#if !DRAGON_NFC
-    platform_data = client->dev.platform_data;
-#else
-    struct device_node *node = client->dev.of_node;
-
-    if (node) {
-        platform_data = devm_kzalloc(&client->dev,
-            sizeof(struct pn544_i2c_platform_data), GFP_KERNEL);
-        if (!platform_data) {
-            dev_err(&client->dev,
-                "nfc-nci probe: Failed to allocate memory\n");
-            return -ENOMEM;
-        }
-        ret = pn544_parse_dt(&client->dev, platform_data);
-        if (ret)
-        {
-            pr_info("%s pn544_parse_dt failed", __func__);
-        }
-        client->irq = gpio_to_irq(platform_data->irq_gpio);
-        if (client->irq < 0)
-        {
-            pr_info("%s gpio to irq failed", __func__);
-        }
-    } else {
-        platform_data = client->dev.platform_data;
-    }
-#endif
-    if (platform_data == NULL) {
-        pr_err("%s : nfc probe fail\n", __func__);
-        return  -ENODEV;
-    }
-
-    if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
-        pr_err("%s : need I2C_FUNC_I2C\n", __func__);
-        return  -ENODEV;
-    }
-#if !DRAGON_NFC
-    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->ese_pwr_gpio, "nfc_ese_pwr");
-    if (ret)
-        goto err_ese_pwr;
-    if (platform_data->firm_gpio) {
-        ret = gpio_request(platform_data->firm_gpio, "nfc_firm");
-        if (ret)
-            goto err_firm;
-    }
-#ifdef ISO_RST
-    if(platform_data->iso_rst_gpio) {
-        ret = gpio_request(platform_data->iso_rst_gpio, "nfc_iso_rst");
-        if (ret)
-            goto err_iso_rst;
-    }
-#endif
-#endif
-    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;
-    }
-
-    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->ese_pwr_gpio  = platform_data->ese_pwr_gpio;
-#ifdef ISO_RST
-    pn544_dev->iso_rst_gpio = platform_data->iso_rst_gpio;
-#endif
-    pn544_dev->p61_current_state = P61_STATE_IDLE;
-    pn544_dev->nfc_ven_enabled = false;
-    pn544_dev->spi_ven_enabled = false;
-    pn544_dev->chip_pwr_scheme = PN67T_PWR_SCHEME;
-    pn544_dev->client   = client;
-    pn544_dev->secure_timer_cnt = 0;
-
-    pn544_dev->state_flags = 0x00;
-    ret = gpio_direction_input(pn544_dev->irq_gpio);
-    if (ret < 0) {
-        pr_err("%s :not able to set irq_gpio as input\n", __func__);
-        goto err_ven;
-    }
-    #ifndef VEN_ALWAYS_ON
-    ret = gpio_direction_output(pn544_dev->ven_gpio, 0);
-    if (ret < 0) {
-        pr_err("%s : not able to set ven_gpio as output\n", __func__);
-        goto err_firm;
-    }
-    #else
-    ret = gpio_direction_output(pn544_dev->ven_gpio, 1);
-    if (ret < 0) {
-        pr_err("%s : not able to set ven_gpio as output\n", __func__);
-        goto err_firm;
-    }
-    #endif
-    ret = gpio_direction_output(pn544_dev->ese_pwr_gpio, 0);
-    if (ret < 0) {
-        pr_err("%s : not able to set ese_pwr gpio as output\n", __func__);
-        goto err_ese_pwr;
-    }
-    if (platform_data->firm_gpio) {
-        ret = gpio_direction_output(pn544_dev->firm_gpio, 0);
-        if (ret < 0) {
-            pr_err("%s : not able to set firm_gpio as output\n",
-                    __func__);
-            goto err_exit;
-        }
-    }
-#ifdef ISO_RST
-    ret = gpio_direction_output(pn544_dev->iso_rst_gpio, 0);
-    if (ret < 0) {
-        pr_err("%s : not able to set iso rst gpio as output\n", __func__);
-        goto err_iso_rst;
-    }
-#endif
-    /* init mutex and queues */
-    ese_reset_resource_init();
-    init_waitqueue_head(&pn544_dev->read_wq);
-    mutex_init(&pn544_dev->read_mutex);
-    sema_init(&ese_access_sema, 1);
-    mutex_init(&pn544_dev->p61_state_mutex);
-    spin_lock_init(&pn544_dev->irq_enabled_lock);
-    pn544_dev->pSecureTimerCbWq = create_workqueue(SECURE_TIMER_WORK_QUEUE);
-    INIT_WORK(&pn544_dev->wq_task, secure_timer_workqueue);
-    pn544_dev->pn544_device.minor = MISC_DYNAMIC_MINOR;
-    pn544_dev->pn544_device.name = "pn553";
-    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;
-    }
-    /* HiKey Compilation fix */
-    #ifndef HiKey_620_COMPILATION_FIX
-    wake_lock_init(&nfc_wake_lock, WAKE_LOCK_SUSPEND, "NFCWAKE");
-    #endif
-#ifdef ISO_RST
-    /* Setting ISO RESET pin high to power ESE during init */
-    gpio_set_value(pn544_dev->iso_rst_gpio, 1);
-#endif
-    /* 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("%s : requesting IRQ %d\n", __func__, client->irq);
-    pn544_dev->irq_enabled = true;
-    ret = request_irq(client->irq, pn544_dev_irq_handler,
-            IRQF_TRIGGER_HIGH, client->name, pn544_dev);
-    if (ret) {
-        dev_err(&client->dev, "request_irq failed\n");
-        goto err_request_irq_failed;
-    }
-    enable_irq_wake(pn544_dev->client->irq);
-    pn544_disable_irq(pn544_dev);
-    i2c_set_clientdata(client, pn544_dev);
-#ifdef VEN_ALWAYS_ON
-    msleep(5); /* VBAT--> VDDIO(HIGH) + Guardtime of min 5ms --> VEN(HIGH) */
-    /* VEN toggle(reset) to proceed */
-    gpio_set_value(pn544_dev->ven_gpio, 0);
-    msleep(5);
-    gpio_set_value(pn544_dev->ven_gpio, 1);
-#endif
-
-#if HWINFO
-    /*
-     * This function is used only if
-     * hardware info is required during probe*/
-    check_hw_info();
-#endif
-
-    return 0;
-
-    err_request_irq_failed:
-    misc_deregister(&pn544_dev->pn544_device);
-    err_misc_register:
-    ese_reset_resource_destroy();
-    mutex_destroy(&pn544_dev->read_mutex);
-    mutex_destroy(&pn544_dev->p61_state_mutex);
-    kfree(pn544_dev);
-    err_exit:
-    if (pn544_dev->firm_gpio)
-        gpio_free(platform_data->firm_gpio);
-    err_firm:
-    gpio_free(platform_data->ese_pwr_gpio);
-    err_ese_pwr:
-    gpio_free(platform_data->ven_gpio);
-    err_ven:
-    gpio_free(platform_data->irq_gpio);
-#ifdef ISO_RST
-    err_iso_rst:
-    gpio_free(platform_data->iso_rst_gpio);
-#endif
-    return ret;
-}
-
-static int pn544_remove(struct i2c_client *client)
-{
-    struct pn544_dev *pn544_dev;
-
-    pn544_dev = i2c_get_clientdata(client);
-    free_irq(client->irq, pn544_dev);
-    misc_deregister(&pn544_dev->pn544_device);
-    mutex_destroy(&pn544_dev->read_mutex);
-    mutex_destroy(&pn544_dev->p61_state_mutex);
-    gpio_free(pn544_dev->irq_gpio);
-    gpio_free(pn544_dev->ven_gpio);
-    gpio_free(pn544_dev->ese_pwr_gpio);
-    destroy_workqueue(pn544_dev->pSecureTimerCbWq);
-#ifdef ISO_RST
-    gpio_free(pn544_dev->iso_rst_gpio);
-#endif
-    pn544_dev->p61_current_state = P61_STATE_INVALID;
-    pn544_dev->nfc_ven_enabled = false;
-    pn544_dev->spi_ven_enabled = false;
-    ese_reset_resource_destroy();
-
-    if (pn544_dev->firm_gpio)
-        gpio_free(pn544_dev->firm_gpio);
-    kfree(pn544_dev);
-
-    return 0;
-}
-
-static const struct i2c_device_id pn544_id[] = {
-#if NEXUS5x
-        { "pn548", 0 },
-#else
-        { "pn544", 0 },
-#endif
-        { }
-};
-#if DRAGON_NFC
-static struct of_device_id pn544_i2c_dt_match[] = {
-    {
-#if NEXUS5x
-        .compatible = "nxp,pn548",
-#else
-        .compatible = "nxp,pn544",
-#endif
-    },
-    {}
-};
-#endif
-static struct i2c_driver pn544_driver = {
-        .id_table   = pn544_id,
-        .probe      = pn544_probe,
-        .remove     = pn544_remove,
-        .driver     = {
-                .owner = THIS_MODULE,
-#if NEXUS5x
-                .name  = "pn548",
-#else
-                .name  = "pn544",
-#endif
-#if DRAGON_NFC
-                .of_match_table = pn544_i2c_dt_match,
-#endif
-        },
-};
-#if HWINFO
-/******************************************************************************
- * Function         check_hw_info
- *
- * Description      This function is called during pn544_probe to retrieve
- *                  HW info.
- *                  Useful get HW information in case of previous FW download is
- *                  interrupted and core reset is not allowed.
- *                  This function checks if core reset  is allowed, if not
- *                  sets DWNLD_REQ(firm_gpio) , ven reset and sends firmware
- *                  get version command.
- *                  In response HW information will be received.
- *
- * Returns          None
- *
- ******************************************************************************/
-static void check_hw_info() {
-    char read_data[20];
-    int ret, get_version_len = 8, retry_count = 0;
-    static uint8_t cmd_reset_nci[] = {0x20, 0x00, 0x01, 0x00};
-    char get_version_cmd[] =
-    {0x00, 0x04, 0xF1, 0x00, 0x00, 0x00, 0x6E, 0xEF};
-
-    pr_info("%s :Enter\n", __func__);
-
-    /*
-     * Ven Reset  before sending core Reset
-     * This is to check core reset is allowed or not.
-     * If not allowed then previous FW download is interrupted in between
-     * */
-    pr_info("%s :Ven Reset \n", __func__);
-    gpio_set_value(pn544_dev->ven_gpio, 1);
-    msleep(10);
-    gpio_set_value(pn544_dev->ven_gpio, 0);
-    msleep(10);
-    gpio_set_value(pn544_dev->ven_gpio, 1);
-    msleep(10);
-    ret = i2c_master_send(pn544_dev->client, cmd_reset_nci, 4);
-
-    if (ret == 4) {
-        pr_info("%s : core reset write success\n", __func__);
-    } else {
-
-        /*
-         * Core reset  failed.
-         * set the DWNLD_REQ , do ven reset
-         * send firmware download info command
-         * */
-        pr_err("%s : write failed\n", __func__);
-        pr_info("%s power on with firmware\n", __func__);
-        gpio_set_value(pn544_dev->ven_gpio, 1);
-        msleep(10);
-        if (pn544_dev->firm_gpio) {
-            p61_update_access_state(pn544_dev, P61_STATE_DWNLD, true);
-            gpio_set_value(pn544_dev->firm_gpio, 1);
-        }
-        msleep(10);
-        gpio_set_value(pn544_dev->ven_gpio, 0);
-        msleep(10);
-        gpio_set_value(pn544_dev->ven_gpio, 1);
-        msleep(10);
-        ret = i2c_master_send(pn544_dev->client, get_version_cmd, get_version_len);
-        if (ret != get_version_len) {
-            ret = -EIO;
-            pr_err("%s : write_failed \n", __func__);
-        }
-        else {
-            pr_info("%s :data sent\n", __func__);
-        }
-
-        ret = 0;
-
-        while (retry_count < 10) {
-
-            /*
-             * Wait for read interrupt
-             * If spurious interrupt is received retry again
-             * */
-            pn544_dev->irq_enabled = true;
-            enable_irq(pn544_dev->client->irq);
-            enable_irq_wake(pn544_dev->client->irq);
-            ret = wait_event_interruptible(
-                    pn544_dev->read_wq,
-                    !pn544_dev->irq_enabled);
-
-            pn544_disable_irq(pn544_dev);
-
-            if (gpio_get_value(pn544_dev->irq_gpio))
-                break;
-
-            pr_warning("%s: spurious interrupt detected\n", __func__);
-            retry_count ++;
-        }
-
-        if(ret) {
-            return;
-        }
-
-        /*
-         * Read response data and copy into hw_type_info
-         * */
-        ret = i2c_master_recv(pn544_dev->client, read_data, 14);
-
-        if(ret) {
-            memcpy(hw_info.data, read_data, ret);
-            hw_info.len = ret;
-            pr_info("%s :data received len  : %d\n", __func__,hw_info.len);
-        }
-        else {
-            pr_err("%s :Read Failed\n", __func__);
-        }
-    }
-}
-#endif
-/*
- * module load/unload record keeping
- */
-
-static int __init pn544_dev_init(void)
-{
-    pr_info("Loading pn544 driver\n");
-    return i2c_add_driver(&pn544_driver);
-}
-module_init(pn544_dev_init);
-
-static void __exit pn544_dev_exit(void)
-{
-    pr_info("Unloading pn544 driver\n");
-    i2c_del_driver(&pn544_driver);
-}
-module_exit(pn544_dev_exit);
-
-MODULE_AUTHOR("Sylvain Fonteneau");
-MODULE_DESCRIPTION("NFC PN544 driver");
-MODULE_LICENSE("GPL");

+ 0 - 266
pn553-i2c/pn553.h

@@ -1,266 +0,0 @@
-/*
- * 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
- */
-/******************************************************************************
- *
- *  The original Work has been changed by NXP Semiconductors.
- *
- *  Copyright (C) 2013-2020 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.
- *
- * 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 _PN553_H_
-#define _PN553_H_
-
-#include <linux/miscdevice.h>
-
-#define PN544_MAGIC 0xE9
-
-/*
- * PN544 power control via ioctl
- * PN544_SET_PWR(0): power off
- * PN544_SET_PWR(1): power on
- * PN544_SET_PWR(2): reset and power on with firmware download enabled
- */
-#define PN544_SET_PWR    _IOW(PN544_MAGIC, 0x01, long)
-
-/*
- * SPI Request NFCC to enable p61 power, only in param
- * Only for SPI
- * level 1 = Enable power
- * level 0 = Disable power
- * This also be used to perform eSE cold reset when
- * argument value is 0x03
- */
-#define P61_SET_SPI_PWR    _IOW(PN544_MAGIC, 0x02, long)
-
-/* SPI or DWP can call this ioctl to get the current
- * power state of P61
- *
-*/
-#define P61_GET_PWR_STATUS    _IOR(PN544_MAGIC, 0x03, long)
-
-/* DWP side this ioctl will be called
- * level 1 = Wired access is enabled/ongoing
- * level 0 = Wired access is disalbed/stopped
-*/
-#define P61_SET_WIRED_ACCESS _IOW(PN544_MAGIC, 0x04, long)
-
-/*
-  NFC Init will call the ioctl to register the PID with the i2c driver
-*/
-#define P544_SET_NFC_SERVICE_PID _IOW(PN544_MAGIC, 0x05, long)
-
-/*
-  NFC and SPI will call the ioctl to get the i2c/spi bus access
-*/
-#define P544_GET_ESE_ACCESS _IOW(PN544_MAGIC, 0x06, long)
-/*
-  NFC and SPI will call the ioctl to update the power scheme
-*/
-#define P544_SET_POWER_SCHEME _IOW(PN544_MAGIC, 0x07, long)
-
-/*
-  NFC will call the ioctl to release the svdd protection
-*/
-#define P544_REL_SVDD_WAIT _IOW(PN544_MAGIC, 0x08, long)
-
-/* SPI or DWP can call this ioctl to get the current
- * power state of P61
- *
-*/
-#define PN544_SET_DWNLD_STATUS    _IOW(PN544_MAGIC, 0x09, long)
-/*
-  NFC will call the ioctl to release the dwp on/off protection
-*/
-#define P544_REL_DWPONOFF_WAIT _IOW(PN544_MAGIC, 0x0A, long)
-
-/*
-  NFC will call the ioctl to start Secure Timer
-*/
-
-#define P544_SECURE_TIMER_SESSION _IOW(PN544_MAGIC, 0x0B, long)
-
-#define MAX_ESE_ACCESS_TIME_OUT_MS 200 /*100 milliseconds*/
-
-/*
-  NFC_ON: Driver is being used by the NFC service (bit b0)
-*/
-#define P544_FLAG_NFC_ON         0x01
-/*
-  FW_DNLD: NFC_ON and FW download is going on  (bit b1)
-*/
-#define P544_FLAG_FW_DNLD        0x02
-/*
- * VEN_RESET: NFC_ON and FW download with VEN reset (bit b2)
-*/
-#define P544_FLAG_NFC_VEN_RESET  0x04
-/*
- * ESE_RESET: Starting of flag positions for eSE cold reset origin
-*/
-#define ESE_COLD_RESET_ORIGIN_FLAGS_POS     (4) //(bit b4)
-#define ESE_COLD_RESET_ORIGIN_NFC_FLAG_POS  (4) //(bit b4)
-/*
- * ESE_RESET: Mask for the flags used for Driver to driver cold reset
- * b6, b5, b4 :
- * 0   0   0 -> no request for ese_cold_reset
- * 0   0   1 -> ese_cold_reset requested from NFC driver
- * 0   1   0 -> ese_cold_reset requested from eSE driver
- * 1   0   0 -> ese_cold_reset requested from UWB driver
-*/
-#define MASK_ESE_COLD_RESET             (0x70)
-/*
- * ESE_RESET: Bit mask to check if ese_reset_guard timer is started (bit b7)
-*/
-#define MASK_ESE_COLD_RESET_GUARD_TIMER  (0x80)
-/*
- * ESE_RESET: Guard time to allow eSE cold reset from the driver
-*/
-#define ESE_COLD_RESET_GUARD_TIME        (3000) //3s
-/*
- * ESE_RESET: NCI command response timeout
-*/
-#define NCI_CMD_RSP_TIMEOUT              (2000) //2s
-/*
- * ESE_RESET: Guard time to reboot the JCOP
-*/
-#define ESE_COLD_RESET_REBOOT_GUARD_TIME   (50) //50ms
-
-typedef enum p61_access_state{
-    P61_STATE_INVALID = 0x0000,
-    P61_STATE_IDLE = 0x0100, /* p61 is free to use */
-    P61_STATE_WIRED = 0x0200,  /* p61 is being accessed by DWP (NFCC)*/
-    P61_STATE_SPI = 0x0400, /* P61 is being accessed by SPI */
-    P61_STATE_DWNLD = 0x0800, /* NFCC fw download is in progress */
-    P61_STATE_SPI_PRIO = 0x1000, /*Start of p61 access by SPI on priority*/
-    P61_STATE_SPI_PRIO_END = 0x2000, /*End of p61 access by SPI on priority*/
-    P61_STATE_SPI_END = 0x4000,
-    P61_STATE_JCP_DWNLD = 0x8000,/* JCOP downlad in progress */
-    P61_STATE_SECURE_MODE = 0x100000, /* secure mode state*/
-    P61_STATE_SPI_SVDD_SYNC_START = 0x0001, /*ESE_VDD Low req by SPI*/
-    P61_STATE_SPI_SVDD_SYNC_END = 0x0002, /*ESE_VDD is Low by SPI*/
-    P61_STATE_DWP_SVDD_SYNC_START = 0x0004, /*ESE_VDD  Low req by Nfc*/
-    P61_STATE_DWP_SVDD_SYNC_END = 0x0008 /*ESE_VDD is Low by Nfc*/
-}p61_access_state_t;
-
-typedef enum chip_type_pwr_scheme{
-    PN67T_PWR_SCHEME = 0x01,
-    PN80T_LEGACY_PWR_SCHEME,
-    PN80T_EXT_PMU_SCHEME,
-}chip_pwr_scheme_t;
-
-typedef enum jcop_dwnld_state{
-    JCP_DWNLD_IDLE = P61_STATE_JCP_DWNLD,   /* jcop dwnld is ongoing*/
-    JCP_DWNLD_INIT=0x8010,                         /* jcop dwonload init state*/
-    JCP_DWNLD_START=0x8020,                        /* download started */
-    JCP_SPI_DWNLD_COMPLETE=0x8040,                 /* jcop download complete in spi interface*/
-    JCP_DWP_DWNLD_COMPLETE=0x8080,                 /* jcop download complete */
-} jcop_dwnld_state_t;
-
-struct pn544_i2c_platform_data {
-    unsigned int irq_gpio;
-    unsigned int ven_gpio;
-    unsigned int firm_gpio;
-    unsigned int ese_pwr_gpio; /* gpio to give power to p61, only TEE should use this */
-    unsigned int iso_rst_gpio; /* gpio used for ISO hard reset P73*/
-};
-
-struct hw_type_info {
-    /*
-     * Response of get_version_cmd will be stored in data
-     * byte structure :
-     * byte 0-1     : Header
-     * byte 2       : Status
-     * byte 3       : Hardware Version
-     * byte 4       : ROM code
-     * byte 5       : 0x00 constant
-     * byte 6-7     : Protected data version
-     * byte 8-9     : Trim data version
-     * byte 10-11   : FW version
-     * byte 12-13   : CRC
-     * */
-    char data[20];
-    int len;
-};
-
-#define NEXUS5x    0
-#define HWINFO     0
-#if NEXUS5x
-#undef ISO_RST
-#else
-#define ISO_RST
-#endif
-
-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;
-    unsigned int        ese_pwr_gpio; /* gpio used by SPI to provide power to p61 via NFCC */
-#ifdef ISO_RST
-    unsigned int        iso_rst_gpio; /* ISO-RST pin gpio*/
-#endif
-    struct mutex        p61_state_mutex; /* used to make p61_current_state flag secure */
-    p61_access_state_t  p61_current_state; /* stores the current P61 state */
-    bool                nfc_ven_enabled; /* stores the VEN pin state powered by Nfc */
-    bool                spi_ven_enabled; /* stores the VEN pin state powered by Spi */
-    bool                irq_enabled;
-    spinlock_t          irq_enabled_lock;
-    long                nfc_service_pid; /*used to signal the nfc the nfc service */
-    chip_pwr_scheme_t   chip_pwr_scheme;
-    unsigned int        secure_timer_cnt;
-    struct workqueue_struct *pSecureTimerCbWq;
-    struct work_struct wq_task;
-    /* This byte represents different flags used during eSE cold reset request from
-     * Driver to driver
-     * Bit value  Status           Remark
-     * b0 : 1  -> NFC_ON           Driver Open should set the flag
-     *      0     NFC_OFF          Driver release should reset this flag
-     * b1 : 1  -> FWDNLD           If FWDNLD is going on.
-     *      0     Normal operation
-     * b2 : 1 --> Ven reset has been requested
-     * b3 : reserved bit
-     * b6, b5, b4 :
-     * 0   0   0 -> no request for ese_cold_reset
-     * 0   0   1 -> ese_cold_reset requested from NFC driver
-     * 0   1   0 -> ese_cold_reset requested from eSE driver
-     * 0   1   1 -> ese_cold_reset requested from UWB driver
-     *              Remaining combinations: Reserved for future use.
-     *              These bits will be cleared once cold reset rsp is received.
-     * b7 : 1 -->   The ese_cold reset guard time has is running
-     *              It will be cleared by the Guard Timer Callback
-     * */
-    volatile uint8_t    state_flags;
-};
-
-#endif