From e77bc813ae8d5a22c36dd96c60bcf350c97cc8bd Mon Sep 17 00:00:00 2001 From: Ningkai Zhuang Date: Thu, 29 Oct 2020 10:44:58 +0800 Subject: [PATCH] NFC driver mmi and DLKM support 1 NFC driver will mmi.nfc to decide if need to probe 2 Add NXP SN1xx driver to DLKM 3 Fix ST driver building error on v(CR) Change-Id: If7f6b1439c7214c4eaaef4ed1368a89988802c67 Signed-off-by: Ningkai Zhuang Reviewed-on: https://gerrit.mot.com/1788053 SME-Granted: SME Approvals Granted SLTApproved: Slta Waiver Tested-by: Jira Key Reviewed-by: Zishang Zhou Reviewed-by: Tao Sun Submit-Approved: Jira Key --- drivers/nfc/sn1xx/Android.mk | 9 + drivers/nfc/sn1xx/Kbuild | 8 + drivers/nfc/sn1xx/Makefile | 11 + drivers/nfc/sn1xx/nfc_common.c | 962 ++++++++++++++++++++++++++++++++ drivers/nfc/sn1xx/nfc_i2c_drv.c | 516 +++++++++++++++++ drivers/nfc/st21nfc/Android.mk | 1 + drivers/nfc/st21nfc/Kbuild | 4 +- drivers/nfc/st21nfc/st21nfc.c | 8 + include/linux/nfc_common.h | 253 +++++++++ include/linux/nfc_i2c_drv.h | 38 ++ include/linux/nfc_i3c_drv.h | 143 +++++ include/linux/st21nfc.h | 2 + 12 files changed, 1954 insertions(+), 1 deletion(-) create mode 100644 drivers/nfc/sn1xx/Android.mk create mode 100644 drivers/nfc/sn1xx/Kbuild create mode 100644 drivers/nfc/sn1xx/Makefile create mode 100644 drivers/nfc/sn1xx/nfc_common.c create mode 100644 drivers/nfc/sn1xx/nfc_i2c_drv.c create mode 100644 include/linux/nfc_common.h create mode 100644 include/linux/nfc_i2c_drv.h create mode 100644 include/linux/nfc_i3c_drv.h diff --git a/drivers/nfc/sn1xx/Android.mk b/drivers/nfc/sn1xx/Android.mk new file mode 100644 index 000000000000..d7675ec04b13 --- /dev/null +++ b/drivers/nfc/sn1xx/Android.mk @@ -0,0 +1,9 @@ +DLKM_DIR := motorola/kernel/modules +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_MODULE := nfc_i2c.ko +LOCAL_MODULE_TAGS := optional +LOCAL_MODULE_PATH := $(KERNEL_MODULES_OUT) +LOCAL_ADDITIONAL_DEPENDENCIES := $(KERNEL_MODULES_OUT)/mmi_info.ko +include $(DLKM_DIR)/AndroidKernelModule.mk diff --git a/drivers/nfc/sn1xx/Kbuild b/drivers/nfc/sn1xx/Kbuild new file mode 100644 index 000000000000..5426c69d2b16 --- /dev/null +++ b/drivers/nfc/sn1xx/Kbuild @@ -0,0 +1,8 @@ +# add -Wall to try to catch everything we can. +EXTRA_CFLAGS += -Wall +EXTRA_CFLAGS += -I$(ANDROID_BUILD_TOP)/motorola/kernel/modules/include + +obj-m += nfc_i2c.o +nfc_i2c-objs:= nfc_common.o nfc_i2c_drv.o + +KBUILD_EXTRA_SYMBOLS += $(CURDIR)/$(KBUILD_EXTMOD)/../../mmi_info/Module.symvers diff --git a/drivers/nfc/sn1xx/Makefile b/drivers/nfc/sn1xx/Makefile new file mode 100644 index 000000000000..62a13fd28500 --- /dev/null +++ b/drivers/nfc/sn1xx/Makefile @@ -0,0 +1,11 @@ +KERNEL_SRC ?= /lib/modules/$(shell uname -r)/build + +all: + $(MAKE) -C $(KERNEL_SRC) M=$(shell pwd) modules $(KBUILD_OPTIONS) + +modules_install: + $(MAKE) INSTALL_MOD_STRIP=1 -C $(KERNEL_SRC) M=$(shell pwd) modules_install + +clean: + $(MAKE) -C $(KERNEL_SRC) M=$(PWD) clean + diff --git a/drivers/nfc/sn1xx/nfc_common.c b/drivers/nfc/sn1xx/nfc_common.c new file mode 100644 index 000000000000..5f3822b7e3d9 --- /dev/null +++ b/drivers/nfc/sn1xx/nfc_common.c @@ -0,0 +1,962 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2015-2020, The Linux Foundation. All rights reserved. + */ + +#include +#include +#include +#include + + +int nfc_parse_dt(struct device *dev, struct platform_gpio *nfc_gpio, + struct platform_ldo *ldo, uint8_t interface) +{ + struct device_node *np = dev->of_node; + int ret; + + if (!np) { + pr_err("nfc of_node NULL\n"); + return -EINVAL; + } + + 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->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; + } + + 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); + return -EINVAL; + } + + nfc_gpio->clkreq = of_get_named_gpio(np, DTS_CLKREQ_GPIO_STR, 0); + if (!gpio_is_valid(nfc_gpio->clkreq)) { + dev_err(dev, "clkreq gpio invalid %d\n", nfc_gpio->dwl_req); + return -EINVAL; + } + + pr_info("%s: ven %d, dwl req %d, clkreq %d\n", __func__, + nfc_gpio->ven, nfc_gpio->dwl_req, nfc_gpio->clkreq); + + // optional property + ret = of_property_read_u32_array(np, NFC_LDO_VOL_DT_NAME, + (u32 *) ldo->vdd_levels, + ARRAY_SIZE(ldo->vdd_levels)); + if (ret) { + dev_err(dev, "error reading NFC VDDIO min and max value\n"); + // set default as per datasheet + ldo->vdd_levels[0] = NFC_VDDIO_MIN; + ldo->vdd_levels[1] = NFC_VDDIO_MAX; + } + + // optional property + ret = of_property_read_u32(np, NFC_LDO_CUR_DT_NAME, &ldo->max_current); + if (ret) { + dev_err(dev, "error reading NFC current value\n"); + // set default as per datasheet + ldo->max_current = NFC_CURRENT_MAX; + } + + return 0; +} + +/** + * nfc_ldo_vote() + * @nfc_dev: NFC device containing regulator handle + * + * LDO voting based on voltage and current entries in DT + * + * Return: 0 on success and -ve on failure + */ +int nfc_ldo_vote(struct nfc_dev *nfc_dev) +{ + int ret; + + ret = regulator_set_voltage(nfc_dev->reg, + nfc_dev->ldo.vdd_levels[0], + nfc_dev->ldo.vdd_levels[1]); + if (ret < 0) { + pr_err("%s: set voltage failed\n", __func__); + return ret; + } + + /* pass expected current from NFC in uA */ + ret = regulator_set_load(nfc_dev->reg, nfc_dev->ldo.max_current); + if (ret < 0) { + pr_err("%s: set load failed\n", __func__); + return ret; + } + + ret = regulator_enable(nfc_dev->reg); + if (ret < 0) + pr_err("%s: regulator_enable failed\n", __func__); + else + nfc_dev->is_vreg_enabled = true; + return ret; +} + +/** + * nfc_ldo_config() + * @dev: device instance to read DT entry + * @nfc_dev: NFC device containing regulator handle + * + * Configure LDO if entry is present in DT file otherwise + * return with success as it's optional + * + * Return: 0 on success and -ve on failure + */ +int nfc_ldo_config(struct device *dev, struct nfc_dev *nfc_dev) +{ + int ret; + + if (of_get_property(dev->of_node, NFC_LDO_SUPPLY_NAME, NULL)) { + // Get the regulator handle + nfc_dev->reg = regulator_get(dev, NFC_LDO_SUPPLY_DT_NAME); + if (IS_ERR(nfc_dev->reg)) { + ret = PTR_ERR(nfc_dev->reg); + nfc_dev->reg = NULL; + pr_err("%s: regulator_get failed, ret = %d\n", + __func__, ret); + return ret; + } + } else { + nfc_dev->reg = NULL; + pr_err("%s: regulator entry not present\n", __func__); + // return success as it's optional to configure LDO + return 0; + } + + // LDO config supported by platform DT + ret = nfc_ldo_vote(nfc_dev); + if (ret < 0) { + pr_err("%s: LDO voting failed, ret = %d\n", __func__, ret); + regulator_put(nfc_dev->reg); + } + return ret; +} + +/** + * nfc_ldo_unvote() + * @nfc_dev: NFC device containing regulator handle + * + * set voltage and load to zero and disable regulator + * + * Return: 0 on success and -ve on failure + */ +int nfc_ldo_unvote(struct nfc_dev *nfc_dev) +{ + int ret; + + if (!nfc_dev->is_vreg_enabled) { + pr_err("%s: regulator already disabled\n", __func__); + return -EINVAL; + } + + ret = regulator_disable(nfc_dev->reg); + if (ret < 0) { + pr_err("%s: regulator_disable failed\n", __func__); + return ret; + } + nfc_dev->is_vreg_enabled = false; + + ret = regulator_set_voltage(nfc_dev->reg, 0, NFC_VDDIO_MAX); + if (ret < 0) { + pr_err("%s: set voltage failed\n", __func__); + return ret; + } + + ret = regulator_set_load(nfc_dev->reg, 0); + if (ret < 0) + pr_err("%s: set load failed\n", __func__); + return ret; +} + +void gpio_set_ven(struct nfc_dev *nfc_dev, int value) +{ + if (gpio_get_value(nfc_dev->gpio.ven) != value) { + 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)); + else + ret = gpio_direction_input(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 nfc_misc_remove(struct nfc_dev *nfc_dev, int count) +{ + pr_debug("%s: entry\n", __func__); + + kfree(nfc_dev->kbuf); + 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_probe(struct nfc_dev *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, "nq-nci"); + 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; + } + + nfc_dev->kbuflen = MAX_BUFFER_SIZE; + nfc_dev->kbuf = kzalloc(MAX_BUFFER_SIZE, GFP_KERNEL | GFP_DMA); + if (!nfc_dev->kbuf) + return -ENOMEM; + + nfc_dev->cold_reset.rsp_pending = false; + nfc_dev->cold_reset.is_nfc_enabled = false; + init_waitqueue_head(&nfc_dev->cold_reset.read_wq); + + return 0; +} + + +static int send_cold_reset_cmd(struct nfc_dev *nfc_dev) +{ + int ret = 0; + char *cold_reset_cmd = NULL; + + cold_reset_cmd = kzalloc(COLD_RESET_CMD_LEN, GFP_DMA | GFP_KERNEL); + if (!cold_reset_cmd) + return -ENOMEM; + + if (gpio_get_value(nfc_dev->gpio.dwl_req)) { + pr_err("FW download in-progress\n"); + ret = -EBUSY; + goto error; + } + if (!gpio_get_value(nfc_dev->gpio.ven)) { + pr_err("VEN LOW - NFCC powered off\n"); + ret = -ENODEV; + goto error; + } + + cold_reset_cmd[0] = COLD_RESET_CMD_GID; + cold_reset_cmd[1] = COLD_RESET_OID; + cold_reset_cmd[2] = COLD_RESET_CMD_PAYLOAD_LEN; + + ret = nfc_dev->nfc_write(nfc_dev, cold_reset_cmd, + COLD_RESET_CMD_LEN, MAX_RETRY_COUNT); + if (ret <= 0) + pr_err("%s: write failed after max retry, ret %d\n", + __func__, ret); + +error: + kfree(cold_reset_cmd); + return ret; +} + +void read_cold_reset_rsp(struct nfc_dev *nfc_dev, char *header) +{ + int ret = -1; + char *cold_reset_rsp = NULL; + struct cold_reset *cold_reset = &nfc_dev->cold_reset; + + cold_reset_rsp = kzalloc(COLD_RESET_RSP_LEN, GFP_DMA | GFP_KERNEL); + if (!cold_reset_rsp) + return; + + /* + * read header also if NFC is disabled + * for enable case, will be taken care by nfc read thread + */ + if ((!cold_reset->is_nfc_enabled) && + (nfc_dev->interface == PLATFORM_IF_I2C)) { + + ret = nfc_dev->nfc_read(nfc_dev, cold_reset_rsp, + NCI_HDR_LEN); + if (ret <= 0) { + pr_err("%s: failure to read cold reset rsp header\n", + __func__); + goto error; + } + } else { + + /* For I3C driver, header is read by the worker thread */ + memcpy(cold_reset_rsp, header, NCI_HDR_LEN); + } + + if ((cold_reset_rsp[0] != COLD_RESET_RSP_GID) + || (cold_reset_rsp[1] != COLD_RESET_OID)) { + pr_err("%s: - invalid response GID or OID for cold_reset\n", + __func__); + ret = -EINVAL; + goto error; + } + if ((NCI_HDR_LEN + cold_reset_rsp[2]) > COLD_RESET_RSP_LEN) { + pr_err("%s: - invalid response for cold_reset\n", __func__); + ret = -EINVAL; + goto error; + } + + if (nfc_dev->interface == PLATFORM_IF_I2C) + ret = nfc_dev->nfc_read(nfc_dev, + &cold_reset_rsp[NCI_PAYLOAD_IDX], + cold_reset_rsp[2]); + else + ret = nfc_dev->i3c_dev.nfc_read_direct(nfc_dev, + &cold_reset_rsp[NCI_PAYLOAD_IDX], + cold_reset_rsp[2]); + + if (ret <= 0) { + pr_err("%s: failure to read cold reset rsp payload\n", + __func__); + goto error; + } + cold_reset->status = cold_reset_rsp[NCI_PAYLOAD_IDX]; + +error: + kfree(cold_reset_rsp); +} + +/* + * 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(struct nfc_dev *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"); + } + nfc_dev->is_ese_session_active = true; + } 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"); + } + nfc_dev->is_ese_session_active = false; + } else if (arg == ESE_COLD_RESET) { + + // set default value for status as failure + nfc_dev->cold_reset.status = -EIO; + + ret = send_cold_reset_cmd(nfc_dev); + if (ret <= 0) { + pr_err("failed to send cold reset command\n"); + return nfc_dev->cold_reset.status; + } + + nfc_dev->cold_reset.rsp_pending = true; + + // check if NFC is enabled + if (nfc_dev->cold_reset.is_nfc_enabled) { + + /* + * nfc_read thread will initiate cold reset response + * and it will signal for data available + */ + wait_event_interruptible(nfc_dev->cold_reset.read_wq, + !nfc_dev->cold_reset.rsp_pending); + + } else { + + // Read data as NFC thread is not active + + nfc_dev->nfc_enable_intr(nfc_dev); + + if (nfc_dev->interface == PLATFORM_IF_I2C) { + ret = wait_event_interruptible_timeout( + nfc_dev->read_wq, + !nfc_dev->i2c_dev.irq_enabled, + msecs_to_jiffies(MAX_IRQ_WAIT_TIME)); + if (ret <= 0) { + nfc_dev->nfc_disable_intr(nfc_dev); + nfc_dev->cold_reset.rsp_pending = false; + return nfc_dev->cold_reset.status; + } + read_cold_reset_rsp(nfc_dev, NULL); + nfc_dev->cold_reset.rsp_pending = false; + } else { + wait_event_interruptible( + nfc_dev->cold_reset.read_wq, + !nfc_dev->cold_reset.rsp_pending); + nfc_dev->nfc_disable_intr(nfc_dev); + } + } + + ret = nfc_dev->cold_reset.status; + + } else if (arg == ESE_POWER_STATE) { + // eSE power state + ret = gpio_get_value(nfc_dev->gpio.ven); + } else { + pr_err("%s bad arg %lu\n", __func__, arg); + ret = -ENOIOCTLCMD; + } + return ret; +} + +/* + * 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 nfcc_ioctl_request in nfc_common.h for args + * + * Return: -ENOIOCTLCMD if arg is not supported, 0 in any other case + */ +static int nfc_ioctl_power_states(struct nfc_dev *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); + pr_debug("gpio firm disable\n"); + if (gpio_is_valid(nfc_dev->gpio.dwl_req)) { + gpio_set_value(nfc_dev->gpio.dwl_req, 0); + usleep_range(10000, 10100); + } + + pr_debug("Set ven to low\n"); + 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); + pr_debug("gpio_set_value enable: %s:\n", __func__); + if (gpio_is_valid(nfc_dev->gpio.dwl_req)) { + gpio_set_value(nfc_dev->gpio.dwl_req, 0); + usleep_range(10000, 10100); + } + gpio_set_ven(nfc_dev, 1); + nfc_dev->nfc_ven_enabled = true; + + if (nfc_dev->interface == PLATFORM_IF_I3C) + nfc_dev->i3c_dev.read_hdr = NCI_HDR_LEN; + + } 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 + */ + + gpio_set_ven(nfc_dev, 1); + + if (gpio_is_valid(nfc_dev->gpio.dwl_req)) { + gpio_set_value(nfc_dev->gpio.dwl_req, 1); + usleep_range(10000, 10100); + } + if (nfc_dev->interface == PLATFORM_IF_I2C) { + gpio_set_ven(nfc_dev, 0); + gpio_set_ven(nfc_dev, 1); + } + + } else if (arg == NFC_FW_DWL_HIGH) { + /* + * Setting firmware download gpio to HIGH + * before FW download start + */ + pr_debug("set fw gpio high\n"); + if (gpio_is_valid(nfc_dev->gpio.dwl_req)) { + gpio_set_value(nfc_dev->gpio.dwl_req, 1); + usleep_range(10000, 10100); + } else + pr_debug("gpio.dwl_req is invalid\n"); + + } else if (arg == NFC_VEN_FORCED_HARD_RESET + && nfc_dev->interface == PLATFORM_IF_I2C) { + /* + * TODO: Enable Ven reset for I3C, after hot join integration + */ + + gpio_set_value(nfc_dev->gpio.ven, 0); + usleep_range(10000, 10100); + gpio_set_value(nfc_dev->gpio.ven, 1); + usleep_range(10000, 10100); + pr_info("%s VEN forced reset done\n", __func__); + + } else if (arg == NFC_FW_DWL_LOW) { + /* + * Setting firmware download gpio to LOW + * FW download finished + */ + pr_debug("set fw gpio LOW\n"); + gpio_set_value(nfc_dev->gpio.dwl_req, 0); + usleep_range(10000, 10100); + + if (nfc_dev->interface == PLATFORM_IF_I3C) + nfc_dev->i3c_dev.read_hdr = NCI_HDR_LEN; + + } else if (arg == NFC_FW_HDR_LEN) { + if (nfc_dev->interface == PLATFORM_IF_I3C) + nfc_dev->i3c_dev.read_hdr = FW_HDR_LEN; + } else if (arg == NFC_ENABLE) { + /* + * Setting flag true when NFC is enabled + */ + nfc_dev->cold_reset.is_nfc_enabled = true; + } else if (arg == NFC_DISABLE) { + /* + * Setting flag true when NFC is disabled + */ + nfc_dev->cold_reset.is_nfc_enabled = false; + } else { + pr_err("%s bad arg %lu\n", __func__, arg); + ret = -ENOIOCTLCMD; + } + return ret; +} + +/* + * Inside nfc_ioctl_nfcc_info + * + * @brief nfc_ioctl_nfcc_info + * + * Check the NFC Chipset and firmware version details + */ +unsigned int nfc_ioctl_nfcc_info(struct file *filp, unsigned long arg) +{ + unsigned int r = 0; + struct nfc_dev *nfc_dev = filp->private_data; + + r = nfc_dev->nqx_info.i; + pr_debug("nfc : %s r = %d\n", __func__, r); + + return r; +} + +/** @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 NFCC_GET_INFO: + ret = nfc_ioctl_nfcc_info(pfile, arg); + break; + case NFC_GET_PLATFORM_TYPE: + ret = nfc_dev->interface; + 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) +{ + struct nfc_dev *nfc_dev = container_of(inode->i_cdev, + struct nfc_dev, c_dev); + + if (!nfc_dev) + return -ENODEV; + + 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) { + if (gpio_is_valid(nfc_dev->gpio.dwl_req)) { + gpio_set_value(nfc_dev->gpio.dwl_req, 0); + usleep_range(10000, 10100); + } + 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) +{ + struct nfc_dev *nfc_dev = container_of(inode->i_cdev, + struct nfc_dev, c_dev); + + if (!nfc_dev) + return -ENODEV; + + 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); + + if (gpio_is_valid(nfc_dev->gpio.dwl_req)) { + gpio_set_value(nfc_dev->gpio.dwl_req, 0); + usleep_range(10000, 10100); + } + } + + if (nfc_dev->dev_ref_count > 0) + nfc_dev->dev_ref_count = nfc_dev->dev_ref_count - 1; + + filp->private_data = NULL; + + mutex_unlock(&nfc_dev->dev_ref_mutex); + + return 0; +} + +int is_data_available_for_read(struct nfc_dev *nfc_dev) +{ + int ret; + + nfc_dev->nfc_enable_intr(nfc_dev); + + ret = wait_event_interruptible_timeout(nfc_dev->read_wq, + !nfc_dev->i2c_dev.irq_enabled, + msecs_to_jiffies(MAX_IRQ_WAIT_TIME)); + return ret; +} + +/* Check for availability of NFC controller hardware */ +int nfcc_hw_check(struct nfc_dev *nfc_dev) +{ + int ret = 0; + unsigned char reset_ntf_len = 0; + char *nci_reset_cmd = NULL; + char *nci_reset_rsp = NULL; + char *nci_reset_ntf = NULL; + char *nci_get_version_cmd = NULL; + char *nci_get_version_rsp = NULL; + + nci_reset_cmd = kzalloc(NCI_RESET_CMD_LEN + 1, GFP_DMA | GFP_KERNEL); + if (!nci_reset_cmd) + return -ENOMEM; + + nci_reset_rsp = kzalloc(NCI_RESET_RSP_LEN + 1, GFP_DMA | GFP_KERNEL); + if (!nci_reset_rsp) { + ret = -ENOMEM; + goto done; + } + + nci_reset_ntf = kzalloc(NCI_RESET_NTF_LEN + 1, GFP_DMA | GFP_KERNEL); + if (!nci_reset_ntf) { + ret = -ENOMEM; + goto done; + } + + nci_get_version_cmd = kzalloc(NCI_GET_VERSION_CMD_LEN + 1, + GFP_DMA | GFP_KERNEL); + if (!nci_get_version_cmd) { + ret = -ENOMEM; + goto done; + } + + nci_get_version_rsp = kzalloc(NCI_GET_VERSION_RSP_LEN + 1, + GFP_DMA | GFP_KERNEL); + if (!nci_get_version_rsp) { + ret = -ENOMEM; + goto done; + } + + if (nfc_dev->interface == PLATFORM_IF_I3C) + nfc_dev->nfc_enable_intr(nfc_dev); + else { + /* making sure that the NFCC starts in a clean state. */ + gpio_set_ven(nfc_dev, 1);/* HPD : Enable*/ + gpio_set_ven(nfc_dev, 0);/* ULPM: Disable */ + gpio_set_ven(nfc_dev, 1);/* HPD : Enable*/ + } + + nci_reset_cmd[0] = 0x20; + nci_reset_cmd[1] = 0x00; + nci_reset_cmd[2] = 0x01; + nci_reset_cmd[3] = 0x00; + + /* send NCI CORE RESET CMD with Keep Config parameters */ + ret = nfc_dev->nfc_write(nfc_dev, nci_reset_cmd, NCI_RESET_CMD_LEN, + MAX_RETRY_COUNT); + if (ret <= 0) { + pr_err("%s: - nfc core reset error\n", __func__); + + if (gpio_is_valid(nfc_dev->gpio.dwl_req)) { + gpio_set_value(nfc_dev->gpio.dwl_req, 1); + usleep_range(10000, 10100); + } + + if (nfc_dev->interface == PLATFORM_IF_I2C) { + gpio_set_ven(nfc_dev, 0); + gpio_set_ven(nfc_dev, 1); + } + + nci_get_version_cmd[0] = 0x00; + nci_get_version_cmd[1] = 0x04; + nci_get_version_cmd[2] = 0xF1; + nci_get_version_cmd[3] = 0x00; + nci_get_version_cmd[4] = 0x00; + nci_get_version_cmd[5] = 0x00; + nci_get_version_cmd[6] = 0x6E; + nci_get_version_cmd[7] = 0xEF; + + ret = nfc_dev->nfc_write(nfc_dev, nci_get_version_cmd, + NCI_GET_VERSION_CMD_LEN, MAX_RETRY_COUNT); + if (ret <= 0) { + pr_err("%s: - nfc get version cmd error ret %d\n", + __func__, ret); + goto err_nfcc_hw_check; + } + + if (nfc_dev->interface == PLATFORM_IF_I2C) { + ret = is_data_available_for_read(nfc_dev); + if (ret <= 0) { + nfc_dev->nfc_disable_intr(nfc_dev); + pr_err("%s: - error waiting for get version rsp ret %d\n", + __func__, ret); + goto err_nfcc_hw_check; + } + } + + ret = nfc_dev->nfc_read(nfc_dev, nci_get_version_rsp, + NCI_GET_VERSION_RSP_LEN); + if (ret <= 0) { + pr_err("%s: - nfc get version rsp error ret %d\n", + __func__, ret); + goto err_nfcc_hw_check; + } else { + nfc_dev->nqx_info.info.chip_type = + nci_get_version_rsp[3]; + nfc_dev->nqx_info.info.rom_version = + nci_get_version_rsp[4]; + nfc_dev->nqx_info.info.fw_minor = + nci_get_version_rsp[6]; + nfc_dev->nqx_info.info.fw_major = + nci_get_version_rsp[7]; + } + + gpio_set_value(nfc_dev->gpio.dwl_req, 0); + + goto err_nfcc_reset_failed; + } + + if (nfc_dev->interface == PLATFORM_IF_I2C) { + ret = is_data_available_for_read(nfc_dev); + if (ret <= 0) { + nfc_dev->nfc_disable_intr(nfc_dev); + pr_err("%s: - error waiting for core reset rsp ret %d\n", + __func__, ret); + + goto err_nfcc_hw_check; + } + } + + /* Read Response of RESET command */ + ret = nfc_dev->nfc_read(nfc_dev, nci_reset_rsp, NCI_RESET_RSP_LEN); + if (ret <= 0) { + pr_err("%s: - nfc rst rsp read err %d\n", __func__, + ret); + goto err_nfcc_hw_check; + } + + if (nfc_dev->interface == PLATFORM_IF_I2C) { + ret = is_data_available_for_read(nfc_dev); + if (ret <= 0) { + pr_err("%s: - error waiting for core reset ntf ret %d\n", + __func__, ret); + nfc_dev->nfc_disable_intr(nfc_dev); + goto err_nfcc_hw_check; + } + } + + /* Read Notification of RESET command */ + ret = nfc_dev->nfc_read(nfc_dev, nci_reset_ntf, NCI_RESET_NTF_LEN); + if (ret <= 0) { + pr_err("%s: nfc nfc read error %d\n", __func__, ret); + goto err_nfcc_hw_check; + } + + reset_ntf_len = NCI_HDR_LEN + nci_reset_ntf[NCI_PAYLOAD_LEN_IDX] - 1; + if (reset_ntf_len > NCI_HDR_LEN) { + nfc_dev->nqx_info.info.chip_type = + nci_reset_ntf[reset_ntf_len - NFC_CHIP_TYPE_OFF]; + nfc_dev->nqx_info.info.rom_version = + nci_reset_ntf[reset_ntf_len - NFC_ROM_VERSION_OFF]; + nfc_dev->nqx_info.info.fw_major = + nci_reset_ntf[reset_ntf_len - NFC_FW_MAJOR_OFF]; + nfc_dev->nqx_info.info.fw_minor = + nci_reset_ntf[reset_ntf_len]; + } + pr_debug("%s: - NFC reset rsp : NfcNciRx %x %x %x\n", + __func__, nci_reset_rsp[0], + nci_reset_rsp[1], nci_reset_rsp[2]); + +err_nfcc_reset_failed: + pr_info("NFC chip_type = %x\n", + nfc_dev->nqx_info.info.chip_type); + pr_info("NFC fw version = %x.%x.%x\n", + nfc_dev->nqx_info.info.rom_version, + nfc_dev->nqx_info.info.fw_major, + nfc_dev->nqx_info.info.fw_minor); + + switch (nfc_dev->nqx_info.info.chip_type) { + case NFCC_SN100_A: + case NFCC_SN100_B: + pr_debug("%s: ## NFCC == SN100x ##\n", __func__); + break; + default: + pr_err("%s: - NFCC HW not Supported\n", __func__); + break; + } + + ret = 0; + nfc_dev->nfc_ven_enabled = true; + + goto disable_i3c_intr; + +err_nfcc_hw_check: + if (nfc_dev->interface == PLATFORM_IF_I2C) + gpio_set_ven(nfc_dev, 0); + + gpio_set_value(nfc_dev->gpio.dwl_req, 0); + + ret = -ENXIO; + pr_debug("%s: - NFCC HW not available\n", __func__); + +disable_i3c_intr: + if (nfc_dev->interface == PLATFORM_IF_I3C) + nfc_dev->nfc_disable_intr(nfc_dev); +done: + kfree(nci_reset_rsp); + kfree(nci_reset_ntf); + kfree(nci_get_version_cmd); + kfree(nci_get_version_rsp); + kfree(nci_reset_cmd); + + return ret; +} diff --git a/drivers/nfc/sn1xx/nfc_i2c_drv.c b/drivers/nfc/sn1xx/nfc_i2c_drv.c new file mode 100644 index 000000000000..1781b60e691c --- /dev/null +++ b/drivers/nfc/sn1xx/nfc_i2c_drv.c @@ -0,0 +1,516 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2015-2020, The Linux Foundation. All rights reserved. + */ + +#include + + +/** + * 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) +{ + struct nfc_dev *nfc_dev = dev_id; + struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev; + + if (device_may_wakeup(&i2c_dev->client->dev)) + pm_wakeup_event(&i2c_dev->client->dev, WAKEUP_SRC_TIMEOUT); + + 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 i2c_read_err; + } + if (ret > count) { + pr_err("%s: received too many bytes from i2c (%d)\n", + __func__, ret); + ret = -EIO; + } + /* delay for the slow nfc devices between susequent read operation */ + usleep_range(1000, 1100); +i2c_read_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 = NULL; + struct nfc_dev *nfc_dev = filp->private_data; + struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev; + + if (!nfc_dev) + return -ENODEV; + + if (count > nfc_dev->kbuflen) + count = nfc_dev->kbuflen; + + 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: ven low in read !\n", __func__); + ret = -ENODEV; + goto err; + } + + pr_warn("%s: spurious interrupt detected\n", __func__); + } + } + + tmp = nfc_dev->kbuf; + if (!tmp) { + pr_err("%s: device doesn't exist anymore\n", __func__); + ret = -ENODEV; + goto err; + } + memset(tmp, 0x00, count); + + /* Read data */ + ret = i2c_read(nfc_dev, tmp, count); + if (ret <= 0) { + pr_err("%s: i2c_master_recv 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 SPI driver + */ + if (nfc_dev->cold_reset.rsp_pending + && (tmp[0] == COLD_RESET_RSP_GID) + && (tmp[1] == COLD_RESET_OID)) { + read_cold_reset_rsp(nfc_dev, tmp); + nfc_dev->cold_reset.rsp_pending = false; + wake_up_interruptible(&nfc_dev->cold_reset.read_wq); + 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 = NULL; + struct nfc_dev *nfc_dev = filp->private_data; + + if (!nfc_dev) { + ret = -ENODEV; + goto out; + } + + if (count > nfc_dev->kbuflen) { + pr_err("%s: out of memory\n", __func__); + ret = -ENOMEM; + goto out; + } + + tmp = memdup_user(buf, count); + if (IS_ERR(tmp)) { + pr_err("%s: memdup_user failed\n", __func__); + ret = PTR_ERR(tmp); + goto out; + } + + ret = i2c_write(nfc_dev, tmp, count, NO_RETRY); + if (ret != count) { + pr_err("%s: failed to write %d\n", __func__, ret); + ret = -EIO; + goto out_free; + } + +out_free: + kfree(tmp); +out: + 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; + struct nfc_dev *nfc_dev = NULL; + struct i2c_dev *i2c_dev = NULL; + struct platform_gpio nfc_gpio; + struct platform_ldo nfc_ldo; + + pr_debug("%s: enter\n", __func__); + + if (client->dev.of_node && !mmi_device_is_available(client->dev.of_node)) { + pr_err("%s : mmi: device not supported\n", __func__); + return -ENODEV; + } else { + pr_err("%s : supported device found\n", __func__); + } + + //retrieve details of gpios from dt + + ret = nfc_parse_dt(&client->dev, &nfc_gpio, &nfc_ldo, 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(struct nfc_dev), GFP_KERNEL); + if (nfc_dev == NULL) { + ret = -ENOMEM; + goto err; + } + nfc_dev->interface = PLATFORM_IF_I2C; + 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_free_nfc_dev; + } + 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_free_ven; + } + 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); + goto err_free_irq; + } + + ret = configure_gpio(nfc_gpio.clkreq, GPIO_INPUT); + if (ret) { + pr_err("%s: unable to request nfc clkreq gpio [%d]\n", + __func__, nfc_gpio.clkreq); + goto err_free_dwl_req; + } + + nfc_dev->gpio.ven = nfc_gpio.ven; + nfc_dev->gpio.irq = nfc_gpio.irq; + nfc_dev->gpio.dwl_req = nfc_gpio.dwl_req; + nfc_dev->gpio.clkreq = nfc_gpio.clkreq; + + /* init mutex and queues */ + init_waitqueue_head(&nfc_dev->read_wq); + mutex_init(&nfc_dev->read_mutex); + mutex_init(&nfc_dev->dev_ref_mutex); + spin_lock_init(&i2c_dev->irq_enabled_lock); + + ret = nfc_misc_probe(nfc_dev, &nfc_i2c_dev_fops, DEV_COUNT, + NFC_CHAR_DEV_NAME, CLASS_NAME); + if (ret) { + pr_err("%s: nfc_misc_probe 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_remove; + } + i2c_disable_irq(nfc_dev); + i2c_set_clientdata(client, nfc_dev); + + ret = nfc_ldo_config(&client->dev, nfc_dev); + if (ret) { + pr_err("LDO config failed\n"); + goto err_ldo_config_failed; + } + + ret = nfcc_hw_check(nfc_dev); + if (ret) { + pr_err("nfc hw check failed ret %d\n", ret); + goto err_nfcc_hw_check; + } + + device_init_wakeup(&client->dev, true); + i2c_dev->irq_wake_up = false; + nfc_dev->is_ese_session_active = false; + + pr_info("%s success\n", __func__); + return 0; + +err_nfcc_hw_check: + if (nfc_dev->reg) { + nfc_ldo_unvote(nfc_dev); + regulator_put(nfc_dev->reg); + } +err_ldo_config_failed: + free_irq(client->irq, nfc_dev); +err_nfc_misc_remove: + nfc_misc_remove(nfc_dev, DEV_COUNT); +err_mutex_destroy: + mutex_destroy(&nfc_dev->dev_ref_mutex); + mutex_destroy(&nfc_dev->read_mutex); + gpio_free(nfc_dev->gpio.clkreq); +err_free_dwl_req: + gpio_free(nfc_dev->gpio.dwl_req); +err_free_irq: + gpio_free(nfc_dev->gpio.irq); +err_free_ven: + gpio_free(nfc_dev->gpio.ven); +err_free_nfc_dev: + kfree(nfc_dev); +err: + pr_err("%s: failed\n", __func__); + return ret; +} + +int nfc_i2c_dev_remove(struct i2c_client *client) +{ + int ret = 0; + struct nfc_dev *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; + } + + gpio_set_value(nfc_dev->gpio.ven, 0); + // HW dependent delay before LDO goes into LPM mode + usleep_range(10000, 10100); + if (nfc_dev->reg) { + nfc_ldo_unvote(nfc_dev); + regulator_put(nfc_dev->reg); + } + + device_init_wakeup(&client->dev, false); + free_irq(client->irq, nfc_dev); + nfc_misc_remove(nfc_dev, DEV_COUNT); + mutex_destroy(&nfc_dev->dev_ref_mutex); + mutex_destroy(&nfc_dev->read_mutex); + + if (gpio_is_valid(nfc_dev->gpio.clkreq)) + gpio_free(nfc_dev->gpio.clkreq); + + 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); + + kfree(nfc_dev); + return ret; +} + +int nfc_i2c_dev_suspend(struct device *device) +{ + struct i2c_client *client = to_i2c_client(device); + struct nfc_dev *nfc_dev = i2c_get_clientdata(client); + struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev; + + if (device_may_wakeup(&client->dev) && i2c_dev->irq_enabled) { + if (!enable_irq_wake(client->irq)) + i2c_dev->irq_wake_up = true; + } + return 0; +} + +int nfc_i2c_dev_resume(struct device *device) +{ + struct i2c_client *client = to_i2c_client(device); + struct nfc_dev *nfc_dev = i2c_get_clientdata(client); + struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev; + + if (device_may_wakeup(&client->dev) && i2c_dev->irq_wake_up) { + if (!disable_irq_wake(client->irq)) + 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; + + 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_debug("Unloading NFC I2C driver\n"); + i2c_del_driver(&nfc_i2c_dev_driver); +} + +module_exit(nfc_i2c_dev_exit); + +MODULE_DESCRIPTION("QTI NFC I2C driver"); +MODULE_LICENSE("GPL v2"); diff --git a/drivers/nfc/st21nfc/Android.mk b/drivers/nfc/st21nfc/Android.mk index 8c3da5b7f6c8..7baa9bff33d5 100644 --- a/drivers/nfc/st21nfc/Android.mk +++ b/drivers/nfc/st21nfc/Android.mk @@ -5,4 +5,5 @@ include $(CLEAR_VARS) LOCAL_MODULE := st21nfc.ko LOCAL_MODULE_TAGS := optional LOCAL_MODULE_PATH := $(KERNEL_MODULES_OUT) +LOCAL_ADDITIONAL_DEPENDENCIES := $(KERNEL_MODULES_OUT)/mmi_info.ko include $(DLKM_DIR)/AndroidKernelModule.mk diff --git a/drivers/nfc/st21nfc/Kbuild b/drivers/nfc/st21nfc/Kbuild index 8e69b981bb3a..76794fa10ece 100644 --- a/drivers/nfc/st21nfc/Kbuild +++ b/drivers/nfc/st21nfc/Kbuild @@ -1,5 +1,7 @@ # add -Wall to try to catch everything we can. EXTRA_CFLAGS += -Wall -EXTRA_CFLAGS += -I$(TOP)/motorola/kernel/modules/include +EXTRA_CFLAGS += -I$(ANDROID_BUILD_TOP)/motorola/kernel/modules/include obj-m += st21nfc.o + +KBUILD_EXTRA_SYMBOLS += $(CURDIR)/$(KBUILD_EXTMOD)/../../mmi_info/Module.symvers diff --git a/drivers/nfc/st21nfc/st21nfc.c b/drivers/nfc/st21nfc/st21nfc.c index 090a755a208b..80c24ca66927 100644 --- a/drivers/nfc/st21nfc/st21nfc.c +++ b/drivers/nfc/st21nfc/st21nfc.c @@ -667,6 +667,14 @@ static int st21nfc_probe(struct i2c_client *client, struct st21nfc_dev *st21nfc_dev; pr_info("st21nfc_probe\n"); + + if (client->dev.of_node && !mmi_device_is_available(client->dev.of_node)) { + pr_err("%s : mmi: device not supported\n", __func__); + return -ENODEV; + } else { + pr_err("%s : supported device found\n", __func__); + } + if (client->dev.of_node) { platform_data = devm_kzalloc(&client->dev, sizeof(struct st21nfc_platform_data), GFP_KERNEL); diff --git a/include/linux/nfc_common.h b/include/linux/nfc_common.h new file mode 100644 index 000000000000..fc4915058dc7 --- /dev/null +++ b/include/linux/nfc_common.h @@ -0,0 +1,253 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2015-2020, The Linux Foundation. All rights reserved. + */ + +#ifndef _NFC_COMMON_H_ +#define _NFC_COMMON_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +// Max device count for this driver +#define DEV_COUNT 1 + +// NFC device class +#define CLASS_NAME "nfc" + +// NFC character device name, this will be in /dev/ +#define NFC_CHAR_DEV_NAME "nq-nci" + +// HDR length of NCI packet +#define NCI_HDR_LEN 3 +#define NCI_PAYLOAD_IDX 3 +#define NCI_PAYLOAD_LEN_IDX 2 + +#define NCI_RESET_CMD_LEN (4) +#define NCI_RESET_RSP_LEN (4) +#define NCI_RESET_NTF_LEN (13) +#define NCI_GET_VERSION_CMD_LEN (8) +#define NCI_GET_VERSION_RSP_LEN (12) + +// Below offsets should be subtracted from core reset ntf len + +#define NFC_CHIP_TYPE_OFF (3) +#define NFC_ROM_VERSION_OFF (2) +#define NFC_FW_MAJOR_OFF (1) + +#define COLD_RESET_CMD_LEN 3 +#define COLD_RESET_RSP_LEN 4 +#define COLD_RESET_CMD_GID 0x2F +#define COLD_RESET_CMD_PAYLOAD_LEN 0x00 +#define COLD_RESET_RSP_GID 0x4F +#define COLD_RESET_OID 0x1E + +#define MAX_NCI_PAYLOAD_LEN (255) +/* + * From MW 11.04 buffer size increased to support + * frame size of 554 in FW download mode + * Frame len(2) + Frame Header(6) + DATA(512) + HASH(32) + CRC(2) + RFU(4) + */ +#define MAX_BUFFER_SIZE (558) + +// 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) + +#define NFC_MAGIC 0xE9 + +// Ioctls +// The type should be aligned with MW HAL definitions + +#define NFC_SET_PWR _IOW(NFC_MAGIC, 0x01, unsigned int) +#define ESE_SET_PWR _IOW(NFC_MAGIC, 0x02, unsigned int) +#define ESE_GET_PWR _IOR(NFC_MAGIC, 0x03, unsigned int) +#define NFC_GET_PLATFORM_TYPE _IO(NFC_MAGIC, 0x04) + +#define DTS_IRQ_GPIO_STR "qcom,sn-irq" +#define DTS_VEN_GPIO_STR "qcom,sn-ven" +#define DTS_FWDN_GPIO_STR "qcom,sn-firm" +#define DTS_CLKREQ_GPIO_STR "qcom,sn-clkreq" +#define DTS_CLKSRC_GPIO_STR "qcom,clk-src" +#define NFC_LDO_SUPPLY_DT_NAME "qcom,sn-vdd-1p8" +#define NFC_LDO_SUPPLY_NAME "qcom,sn-vdd-1p8-supply" +#define NFC_LDO_VOL_DT_NAME "qcom,sn-vdd-1p8-voltage" +#define NFC_LDO_CUR_DT_NAME "qcom,sn-vdd-1p8-current" + +//as per SN1x0 datasheet +#define NFC_VDDIO_MIN 1650000 //in uV +#define NFC_VDDIO_MAX 1950000 //in uV +#define NFC_CURRENT_MAX 157000 //in uA + +enum ese_ioctl_request { + /* eSE POWER ON */ + ESE_POWER_ON = 0, + /* eSE POWER OFF */ + ESE_POWER_OFF, + /* eSE COLD RESET */ + ESE_COLD_RESET, + /* eSE POWER STATE */ + ESE_POWER_STATE +}; + +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 enable without VEN gpio modification */ + NFC_ENABLE, + /* NFC disable without VEN gpio modification */ + NFC_DISABLE, + /*for HDR size change in FW mode */ + NFC_FW_HDR_LEN, +}; + +/*nfc platform interface type*/ +enum interface_flags { + /*I2C physical IF for NFCC */ + PLATFORM_IF_I2C = 0, + /*I3C physical IF for NFCC */ + PLATFORM_IF_I3C, +}; + +/* + * 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, +}; + +enum nfcc_chip_variant { + NFCC_SN100_A = 0xa3, /**< NFCC SN100_A */ + NFCC_SN100_B = 0xa4, /**< NFCC SN100_B */ + NFCC_NOT_SUPPORTED = 0xFF /**< NFCC is not supported */ +}; + +// NFC GPIO variables +struct platform_gpio { + unsigned int irq; + unsigned int ven; + unsigned int clkreq; + unsigned int dwl_req; +}; + +// NFC LDO entries from DT +struct platform_ldo { + int vdd_levels[2]; + int max_current; +}; + +//Features specific Parameters +struct cold_reset { + wait_queue_head_t read_wq; + bool rsp_pending; + uint8_t status; + /* Is NFC enabled from UI */ + bool is_nfc_enabled; +}; + +/* Device specific structure */ +struct nfc_dev { + wait_queue_head_t read_wq; + struct mutex read_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 VEN pin state */ + bool nfc_ven_enabled; + bool is_vreg_enabled; + bool is_ese_session_active; + union { + struct i2c_dev i2c_dev; + struct i3c_dev i3c_dev; + }; + struct platform_gpio gpio; + struct platform_ldo ldo; + struct cold_reset cold_reset; + struct regulator *reg; + + /* read buffer*/ + size_t kbuflen; + u8 *kbuf; + + union nqx_uinfo nqx_info; + + 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_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, struct platform_gpio *nfc_gpio, + struct platform_ldo *ldo, uint8_t interface); +int nfc_misc_probe(struct nfc_dev *nfc_dev, + const struct file_operations *nfc_fops, int count, + char *devname, char *classname); +void nfc_misc_remove(struct nfc_dev *nfc_dev, int count); +int configure_gpio(unsigned int gpio, int flag); +void read_cold_reset_rsp(struct nfc_dev *nfc_dev, char *header); +void gpio_set_ven(struct nfc_dev *nfc_dev, int value); +int nfcc_hw_check(struct nfc_dev *nfc_dev); +int nfc_ldo_config(struct device *dev, struct nfc_dev *nfc_dev); +int nfc_ldo_vote(struct nfc_dev *nfc_dev); +int nfc_ldo_unvote(struct nfc_dev *nfc_dev); + +#endif //_NFC_COMMON_H_ diff --git a/include/linux/nfc_i2c_drv.h b/include/linux/nfc_i2c_drv.h new file mode 100644 index 000000000000..2416051090c4 --- /dev/null +++ b/include/linux/nfc_i2c_drv.h @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2015-2020, The Linux Foundation. All rights reserved. + */ + +#ifndef _NFC_I2C_DRV_H_ +#define _NFC_I2C_DRV_H_ +#include + +#define NFC_I2C_DRV_STR "qcom,sn-nci" /*kept same as dts */ +#define NFC_I2C_DEV_ID "sn-i2c" + +struct nfc_dev; + +//Interface specific parameters +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; +}; +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); + + +int i2c_enable_irq(struct nfc_dev *dev); +int i2c_disable_irq(struct nfc_dev *dev); +int i2c_write(struct nfc_dev *dev, const char *buf, size_t count, + int max_retry_cnt); +int i2c_read(struct nfc_dev *dev, char *buf, size_t count); + +#endif //_NFC_I2C_DRV_H_ diff --git a/include/linux/nfc_i3c_drv.h b/include/linux/nfc_i3c_drv.h new file mode 100644 index 000000000000..7ef3dfda2990 --- /dev/null +++ b/include/linux/nfc_i3c_drv.h @@ -0,0 +1,143 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved. + */ + +#ifndef _NFC_I3C_DRV_H_ +#define _NFC_I3C_DRV_H_ + +#include +#include + +//to to kept same as dt +#define NFC_I3C_DRV_STR "qcom,sn-nci-i3c" +#define NFC_I3C_MANU_ID (0x011B) +#define NFC_I3C_PART_ID (0) + +//Byte indicating I3C Read +#define NFC_I3C_READ 1 + +//Byte indicating I3C Write +#define NFC_I3C_WRITE 0 + +// Maximum no of IBI slot +#define NUM_NFC_IBI_SLOT 1 + +// Maximum IBI payload length +#define MAX_IBI_PAYLOAD_LEN 0 + +// CRC len to be read +#define FW_CRC_LEN 2 + +// FW DNLD HDR length +#define FW_HDR_LEN 2 + +// Time to wait before retrying I3C writes, in micro seconds +#define RETRY_WAIT_TIME_USEC (2000) + +// Retry count for enable/disable IBI CCC +#define RETRY_COUNT_IBI (3) + +// I3C WorkQueue name +#define NFC_I3C_WORKQUEUE "nfc_i3c_workq" + +/* Time(in ms) to wait for NCI packet respones */ +#define MAX_IBI_WAIT_TIME (2000) +struct nfc_dev; + +/** + * struct nci_buf - NCI buffer used to store and retrieve read data from device. + * @read_offset: The offset pointing to data available to read in nci buf. + * @write_offset: The offset pointing to free buf available to write. + * @total_size: Size of nci buf. + * @kbuf: allocated nci buf. + */ +struct nci_buf { + unsigned int read_offset; + unsigned int write_offset; + size_t total_size; + char *kbuf; +}; + +/** + * struct i3c_dev Structure representing device and driver data. + * @i3c_device Structure to represent I3C device. + * @wq: NCI workqueue for handling IBI request. + * @work: Work added to workqueue to read data from IBI handler. + * @buf Driver buf store read data from device.Read call will + * fetch from this buffer. + * @nci_buf_mutex: mutex to protect NCI buf retrieve/store . + * @read_cplt: Completion to wait for read data to be available. + * @read_kbuf_len: Temp buf len to hold I3C data. + * @read_kbuf: Temp buf to hold I3C data. + * @read_hdr Header size for reads. + * @ibi_enabled: IBI enabled or not. + * @pm_state: PM state of NFC I3C device. + * @nfc_read_direct Do NFC read bypassing the read buffer. + * @is_probe_done Is NFC I3C probe completed. + */ +struct i3c_dev { + struct i3c_device *device; + struct workqueue_struct *wq; + struct work_struct work; + struct nci_buf buf; + struct mutex nci_buf_mutex; + struct completion read_cplt; + size_t read_kbuf_len; + char *read_kbuf; + unsigned char read_hdr; + bool ibi_enabled; + atomic_t pm_state; + int (*nfc_read_direct)(struct nfc_dev *dev, + char *buf, size_t count); + bool is_probe_done; +}; + +int nfc_i3c_dev_probe(struct i3c_device *device); +int nfc_i3c_dev_remove(struct i3c_device *device); +int nfc_i3c_dev_suspend(struct device *device); +int nfc_i3c_dev_resume(struct device *device); + +#if IS_ENABLED(CONFIG_NFC_QTI_I3C) + +int i3c_enable_ibi(struct nfc_dev *dev); +int i3c_disable_ibi(struct nfc_dev *dev); +int i3c_write(struct nfc_dev *dev, const char *buf, const size_t count, + int max_retry_cnt); +int i3c_read(struct nfc_dev *dev, char *buf, size_t count); +int i3c_nci_kbuf_retrieve(struct nfc_dev *dev, char *buf, + size_t count); + +#else + +static inline int i3c_enable_ibi(struct nfc_dev *dev) +{ + return -ENXIO; +} + +static inline int i3c_disable_ibi(struct nfc_dev *dev) +{ + return -ENXIO; +} + +static inline int i3c_write(struct nfc_dev *dev, + const char *buf, const size_t count, int max_retry_cnt) +{ + return -ENXIO; +} + +static inline int i3c_read(struct nfc_dev *dev, + char *buf, size_t count) +{ + return -ENXIO; +} + +static inline int i3c_nci_kbuf_retrieve(struct nfc_dev *dev, + char *buf, size_t count) +{ + return -ENXIO; +} + +#endif + +#endif //_NFC_I3C_DRV_H_ diff --git a/include/linux/st21nfc.h b/include/linux/st21nfc.h index 9cd89bf84a74..4be75fa53cd0 100644 --- a/include/linux/st21nfc.h +++ b/include/linux/st21nfc.h @@ -18,6 +18,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include + #define ST21NFC_MAGIC 0xEA #define ST21NFC_NAME "st21nfc"