mirror of
https://github.com/BobTheBlinker/android_kernel_motorola_sm6375.git
synced 2026-10-05 19:31:57 -04:00
dlkm: add new nfc_nci driver
Add new nfc driver nfc_nci that copy from Qcom bs-8450 kernel branch. Change-Id: Icccb63e9b3136c4d5d6296584892b256da9c7181 Signed-off-by: wuwl10 <wuwl10@lenovo.com> Reviewed-on: https://gerrit.mot.com/2676486 SME-Granted: SME Approvals Granted SLTApproved: Slta Waiver Tested-by: Jira Key Reviewed-by: Ningkai Zhuang <zhuangnk1@lenovo.com> Reviewed-by: Hujun Liao <liaohj@motorola.com> Submit-Approved: Jira Key
This commit is contained in:
parent
417f8e9526
commit
3fc4e9567a
9 changed files with 2459 additions and 0 deletions
9
drivers/nfc/sn2xx/Android.mk
Normal file
9
drivers/nfc/sn2xx/Android.mk
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
DLKM_DIR := motorola/kernel/modules
|
||||
LOCAL_PATH := $(call my-dir)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_MODULE := nfc_nci.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
|
||||
8
drivers/nfc/sn2xx/Kbuild
Normal file
8
drivers/nfc/sn2xx/Kbuild
Normal file
|
|
@ -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_nci.o
|
||||
nfc_nci-objs:= nfc_common.o nfc_i2c_drv.o ese_cold_reset.o
|
||||
|
||||
KBUILD_EXTRA_SYMBOLS += $(CURDIR)/$(KBUILD_EXTMOD)/../../mmi_info/Module.symvers
|
||||
11
drivers/nfc/sn2xx/Makefile
Normal file
11
drivers/nfc/sn2xx/Makefile
Normal file
|
|
@ -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
|
||||
|
||||
346
drivers/nfc/sn2xx/ese_cold_reset.c
Normal file
346
drivers/nfc/sn2xx/ese_cold_reset.c
Normal file
|
|
@ -0,0 +1,346 @@
|
|||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
/*
|
||||
* Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
|
||||
*/
|
||||
|
||||
#include <linux/slab.h>
|
||||
#include <linux/gpio.h>
|
||||
#include <linux/uaccess.h>
|
||||
#include "nfc_common.h"
|
||||
|
||||
/**
|
||||
* send_ese_cmd() - Send eSE command to NFC controller.
|
||||
* @nfc_dev: NFC device handle.
|
||||
*
|
||||
* Return: 0 on pass and negative value on failure.
|
||||
*/
|
||||
static int send_ese_cmd(struct nfc_dev *nfc_dev)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (nfc_dev->nfc_state == NFC_STATE_FW_DWL) {
|
||||
dev_err(nfc_dev->nfc_device,
|
||||
"cannot send ese cmd as FW download is in-progress\n");
|
||||
return -EBUSY;
|
||||
}
|
||||
if (!gpio_get_value(nfc_dev->configs.gpio.ven)) {
|
||||
dev_err(nfc_dev->nfc_device,
|
||||
"cannot send ese cmd as NFCC powered off\n");
|
||||
return -ENODEV;
|
||||
}
|
||||
if (nfc_dev->cold_reset.cmd_buf == NULL)
|
||||
return -EFAULT;
|
||||
ret = nfc_dev->nfc_write(nfc_dev, nfc_dev->cold_reset.cmd_buf,
|
||||
nfc_dev->cold_reset.cmd_len,
|
||||
MAX_RETRY_COUNT);
|
||||
if (ret <= 0)
|
||||
dev_err(nfc_dev->nfc_device,
|
||||
"%s: write failed after max retry, ret %d\n",
|
||||
__func__, ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* read_cold_reset_rsp() - Read response of the cold reset command.
|
||||
* @nfc_dev: NFC device handle.
|
||||
* @header: Pointer to NCI header if it is already read.
|
||||
*
|
||||
* Return: 0 on pass and negative value on failure.
|
||||
*/
|
||||
int read_cold_reset_rsp(struct nfc_dev *nfc_dev, char *header)
|
||||
{
|
||||
int ret = -EPERM;
|
||||
struct cold_reset *cold_rst = &nfc_dev->cold_reset;
|
||||
char *rsp_buf = NULL;
|
||||
|
||||
if (cold_rst->rsp_len < COLD_RESET_RSP_LEN) {
|
||||
dev_err(nfc_dev->nfc_device,
|
||||
"%s: received cold reset rsp buffer length is invalid\n",
|
||||
__func__);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
rsp_buf = kzalloc(cold_rst->rsp_len, GFP_DMA | GFP_KERNEL);
|
||||
if (!rsp_buf)
|
||||
return -ENOMEM;
|
||||
|
||||
/*
|
||||
* read header if NFC is disabled
|
||||
* for enable case, header is read by nfc read thread(for i2c)
|
||||
*/
|
||||
if ((!cold_rst->is_nfc_enabled) &&
|
||||
(nfc_dev->interface == PLATFORM_IF_I2C)) {
|
||||
ret = i2c_master_recv(nfc_dev->i2c_dev.client, rsp_buf, NCI_HDR_LEN);
|
||||
if (ret <= 0) {
|
||||
dev_err(nfc_dev->nfc_device,
|
||||
"%s: failure to read cold reset rsp header\n",
|
||||
__func__);
|
||||
ret = -EIO;
|
||||
goto error;
|
||||
}
|
||||
/*
|
||||
* return failure, if packet is not a response packet or
|
||||
* if response's OID doesn't match with the CMD's OID
|
||||
*/
|
||||
if (!(rsp_buf[0] & NCI_RSP_PKT_TYPE) ||
|
||||
(!cold_rst->cmd_buf) ||
|
||||
(rsp_buf[1] != cold_rst->cmd_buf[1])) {
|
||||
|
||||
dev_err(nfc_dev->nfc_device,
|
||||
"%s: - invalid cold reset response 0x%x 0x%x\n",
|
||||
__func__, rsp_buf[0], rsp_buf[1]);
|
||||
ret = -EINVAL;
|
||||
goto error;
|
||||
}
|
||||
} else if (header) {
|
||||
memcpy(rsp_buf, header, NCI_HDR_LEN);
|
||||
} else {
|
||||
dev_err(nfc_dev->nfc_device,
|
||||
"%s: - invalid or NULL header\n", __func__);
|
||||
ret = -EINVAL;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((NCI_HDR_LEN + rsp_buf[NCI_PAYLOAD_LEN_IDX]) >
|
||||
cold_rst->rsp_len) {
|
||||
dev_err(nfc_dev->nfc_device,
|
||||
"%s: - no space for cold_reset resp\n", __func__);
|
||||
ret = -ENOMEM;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (nfc_dev->interface == PLATFORM_IF_I2C) {
|
||||
ret = nfc_dev->nfc_read(nfc_dev,
|
||||
&rsp_buf[NCI_PAYLOAD_IDX],
|
||||
rsp_buf[NCI_PAYLOAD_LEN_IDX],
|
||||
NCI_CMD_RSP_TIMEOUT);
|
||||
|
||||
if (ret <= 0) {
|
||||
dev_err(nfc_dev->nfc_device,
|
||||
"%s: failure to read cold reset rsp payload\n",
|
||||
__func__);
|
||||
ret = -EIO;
|
||||
goto error;
|
||||
}
|
||||
ret = cold_rst->status = rsp_buf[NCI_PAYLOAD_IDX];
|
||||
|
||||
pr_debug("nfc ese rsp hdr 0x%x 0x%x 0x%x, payload byte0 0x%x\n",
|
||||
rsp_buf[0], rsp_buf[1], rsp_buf[2], rsp_buf[3]);
|
||||
}
|
||||
|
||||
error:
|
||||
kfree(rsp_buf);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ese_cold_reset_ioctl() - This function handles the eSE cold reset ioctls.
|
||||
* @nfc_dev: NFC device handle.
|
||||
* @arg: ioctl argument.
|
||||
*
|
||||
* Return: 0 on pass and negative value on failure.
|
||||
*/
|
||||
|
||||
int ese_cold_reset_ioctl(struct nfc_dev *nfc_dev, unsigned long arg)
|
||||
{
|
||||
int ret;
|
||||
struct ese_ioctl_arg ioctl_arg;
|
||||
struct ese_cold_reset_arg *cold_reset_arg = NULL;
|
||||
|
||||
if (!arg) {
|
||||
dev_err(nfc_dev->nfc_device, "arg is invalid\n");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
ret = copy_from_user((void *)&ioctl_arg, (const void *)arg,
|
||||
sizeof(ioctl_arg));
|
||||
if (ret) {
|
||||
dev_err(nfc_dev->nfc_device,
|
||||
"ese ioctl arg copy from user failed\n");
|
||||
return -EFAULT;
|
||||
}
|
||||
|
||||
cold_reset_arg = kzalloc(sizeof(struct ese_cold_reset_arg), GFP_KERNEL);
|
||||
if (!cold_reset_arg)
|
||||
return -ENOMEM;
|
||||
|
||||
mutex_lock(&nfc_dev->write_mutex);
|
||||
ret = copy_struct_from_user(cold_reset_arg,
|
||||
sizeof(struct ese_cold_reset_arg),
|
||||
u64_to_user_ptr(ioctl_arg.buf),
|
||||
sizeof(struct ese_cold_reset_arg));
|
||||
if (ret) {
|
||||
dev_err(nfc_dev->nfc_device,
|
||||
"ese ioctl arg buffer copy from user failed\n");
|
||||
|
||||
ret = -EFAULT;
|
||||
goto err;
|
||||
}
|
||||
|
||||
switch (cold_reset_arg->sub_cmd) {
|
||||
|
||||
case ESE_COLD_RESET_DO:
|
||||
|
||||
/*
|
||||
* cold reset allowed during protection enable, only if the
|
||||
* source is same as the one which enabled protection.
|
||||
*/
|
||||
if (nfc_dev->cold_reset.is_crp_en &&
|
||||
(cold_reset_arg->src !=
|
||||
nfc_dev->cold_reset.last_src_ese_prot)) {
|
||||
dev_err(nfc_dev->nfc_device,
|
||||
"cold reset from %d denied, protection is on\n",
|
||||
cold_reset_arg->src);
|
||||
ret = -EACCES;
|
||||
goto err;
|
||||
}
|
||||
|
||||
nfc_dev->cold_reset.cmd_buf = kzalloc(COLD_RESET_CMD_LEN,
|
||||
GFP_DMA | GFP_KERNEL);
|
||||
if (!nfc_dev->cold_reset.cmd_buf) {
|
||||
ret = -ENOMEM;
|
||||
goto err;
|
||||
}
|
||||
|
||||
nfc_dev->cold_reset.cmd_buf[0] = PROP_NCI_CMD_GID;
|
||||
nfc_dev->cold_reset.cmd_buf[1] = COLD_RESET_OID;
|
||||
nfc_dev->cold_reset.cmd_buf[2] = COLD_RESET_CMD_PL_LEN;
|
||||
nfc_dev->cold_reset.cmd_len = NCI_HDR_LEN +
|
||||
COLD_RESET_CMD_PL_LEN;
|
||||
nfc_dev->cold_reset.rsp_len = COLD_RESET_RSP_LEN;
|
||||
break;
|
||||
|
||||
case ESE_COLD_RESET_PROTECT_EN:
|
||||
|
||||
if (nfc_dev->cold_reset.is_crp_en) {
|
||||
if (cold_reset_arg->src !=
|
||||
nfc_dev->cold_reset.last_src_ese_prot) {
|
||||
dev_err(nfc_dev->nfc_device,
|
||||
"ese protection enable denied\n");
|
||||
ret = -EACCES;
|
||||
goto err;
|
||||
}
|
||||
pr_warn("ese protection already enabled\n");
|
||||
|
||||
ret = 0;
|
||||
/* free buffers and exit with pass */
|
||||
goto err;
|
||||
}
|
||||
|
||||
case ESE_COLD_RESET_PROTECT_DIS:
|
||||
|
||||
if (nfc_dev->cold_reset.is_crp_en &&
|
||||
cold_reset_arg->src !=
|
||||
nfc_dev->cold_reset.last_src_ese_prot) {
|
||||
pr_err("ese cold reset protection disable denied\n");
|
||||
ret = -EACCES;
|
||||
goto err;
|
||||
}
|
||||
nfc_dev->cold_reset.cmd_buf = kzalloc(COLD_RESET_PROT_CMD_LEN,
|
||||
GFP_DMA | GFP_KERNEL);
|
||||
if (!nfc_dev->cold_reset.cmd_buf) {
|
||||
ret = -ENOMEM;
|
||||
goto err;
|
||||
}
|
||||
|
||||
nfc_dev->cold_reset.cmd_buf[0] = PROP_NCI_CMD_GID;
|
||||
nfc_dev->cold_reset.cmd_buf[1] = COLD_RESET_PROT_OID;
|
||||
nfc_dev->cold_reset.cmd_buf[2] = COLD_RESET_PROT_CMD_PL_LEN;
|
||||
nfc_dev->cold_reset.cmd_len = NCI_HDR_LEN +
|
||||
COLD_RESET_PROT_CMD_PL_LEN;
|
||||
nfc_dev->cold_reset.rsp_len = COLD_RESET_PROT_RSP_LEN;
|
||||
if (cold_reset_arg->sub_cmd == ESE_COLD_RESET_PROTECT_EN)
|
||||
nfc_dev->cold_reset.cmd_buf[3] = 0x1;
|
||||
else
|
||||
nfc_dev->cold_reset.cmd_buf[3] = 0x0;
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
pr_err("%s invalid ese ioctl sub cmd %d\n", __func__,
|
||||
cold_reset_arg->sub_cmd);
|
||||
ret = -ENOIOCTLCMD;
|
||||
goto err;
|
||||
}
|
||||
|
||||
pr_debug("nfc ese cmd hdr 0x%x 0x%x 0x%x\n",
|
||||
nfc_dev->cold_reset.cmd_buf[0],
|
||||
nfc_dev->cold_reset.cmd_buf[1],
|
||||
nfc_dev->cold_reset.cmd_buf[2]);
|
||||
|
||||
ret = send_ese_cmd(nfc_dev);
|
||||
if (ret <= 0) {
|
||||
pr_err("failed to send ese command\n");
|
||||
goto err;
|
||||
}
|
||||
|
||||
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 read thread is not active
|
||||
*/
|
||||
|
||||
if (nfc_dev->interface == PLATFORM_IF_I2C) {
|
||||
ret = is_nfc_data_available_for_read(nfc_dev);
|
||||
if (ret <= 0) {
|
||||
nfc_dev->nfc_disable_intr(nfc_dev);
|
||||
nfc_dev->cold_reset.rsp_pending = false;
|
||||
goto err;
|
||||
}
|
||||
|
||||
ret = read_cold_reset_rsp(nfc_dev, NULL);
|
||||
nfc_dev->cold_reset.rsp_pending = false;
|
||||
if (ret < 0) {
|
||||
pr_err("%s rsp read err\n", __func__);
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* Enable intr as it is disabled when NFC is in disable
|
||||
* state
|
||||
*/
|
||||
nfc_dev->nfc_enable_intr(nfc_dev);
|
||||
|
||||
wait_event_interruptible(
|
||||
nfc_dev->cold_reset.read_wq,
|
||||
!nfc_dev->cold_reset.rsp_pending);
|
||||
}
|
||||
|
||||
nfc_dev->nfc_disable_intr(nfc_dev);
|
||||
}
|
||||
|
||||
if (cold_reset_arg->sub_cmd == ESE_COLD_RESET_PROTECT_EN) {
|
||||
nfc_dev->cold_reset.is_crp_en = true;
|
||||
nfc_dev->cold_reset.last_src_ese_prot = cold_reset_arg->src;
|
||||
} else if (cold_reset_arg->sub_cmd == ESE_COLD_RESET_PROTECT_DIS) {
|
||||
nfc_dev->cold_reset.is_crp_en = false;
|
||||
nfc_dev->cold_reset.last_src_ese_prot =
|
||||
ESE_COLD_RESET_ORIGIN_NONE;
|
||||
} else
|
||||
pr_debug("ese cmd is %d\n", cold_reset_arg->sub_cmd);
|
||||
|
||||
ret = nfc_dev->cold_reset.status;
|
||||
|
||||
err:
|
||||
kfree(nfc_dev->cold_reset.cmd_buf);
|
||||
nfc_dev->cold_reset.cmd_buf = NULL;
|
||||
kfree(cold_reset_arg);
|
||||
cold_reset_arg = NULL;
|
||||
|
||||
mutex_unlock(&nfc_dev->write_mutex);
|
||||
|
||||
return ret;
|
||||
}
|
||||
80
drivers/nfc/sn2xx/ese_cold_reset.h
Normal file
80
drivers/nfc/sn2xx/ese_cold_reset.h
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
/*
|
||||
* Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
|
||||
*/
|
||||
|
||||
#ifndef __ESE_COLD_RESET_H
|
||||
#define __ESE_COLD_RESET_H
|
||||
|
||||
#include <linux/nfcinfo.h>
|
||||
|
||||
#define MAX_BUFF_SIZE 264
|
||||
|
||||
/* ESE_COLD_RESET MACROS */
|
||||
#define COLD_RESET_CMD_LEN 3
|
||||
#define COLD_RESET_RSP_LEN 4
|
||||
#define COLD_RESET_PROT_CMD_LEN 4
|
||||
#define COLD_RESET_PROT_RSP_LEN 4
|
||||
#define PROP_NCI_CMD_GID 0x2F
|
||||
#define COLD_RESET_CMD_PL_LEN 0x00
|
||||
#define COLD_RESET_PROT_CMD_PL_LEN 0x01
|
||||
#define PROP_NCI_RSP_GID 0x4F
|
||||
#define COLD_RESET_OID 0x1E
|
||||
#define COLD_RESET_PROT_OID 0x1F
|
||||
|
||||
#define ESE_COLD_RESET _IOWR(NFCC_MAGIC, 0x08, struct ese_ioctl_arg)
|
||||
|
||||
enum ese_ioctl_arg_type {
|
||||
ESE_ARG_TYPE_COLD_RESET = 0,
|
||||
};
|
||||
|
||||
/* ESE_COLD_RESET ioctl origin, max 4 are supported */
|
||||
enum ese_cold_reset_origin {
|
||||
ESE_COLD_RESET_ORIGIN_ESE = 0,
|
||||
ESE_COLD_RESET_ORIGIN_NFC,
|
||||
ESE_COLD_RESET_ORIGIN_OTHER = 0x20,
|
||||
ESE_COLD_RESET_ORIGIN_NONE = 0xFF,
|
||||
};
|
||||
|
||||
/* ESE_COLD_RESET ioctl sub commands, max 8 are supported */
|
||||
enum ese_cold_reset_sub_cmd {
|
||||
ESE_COLD_RESET_DO = 0,
|
||||
ESE_COLD_RESET_PROTECT_EN,
|
||||
ESE_COLD_RESET_PROTECT_DIS,
|
||||
};
|
||||
|
||||
/* Data passed in buf of ese cold reset ioctl */
|
||||
struct ese_cold_reset_arg {
|
||||
__u8 src;
|
||||
__u8 sub_cmd;
|
||||
__u16 rfu;
|
||||
};
|
||||
|
||||
/* Argument buffer passed to ese ioctl */
|
||||
struct ese_ioctl_arg {
|
||||
__u64 buf;
|
||||
__u32 buf_size;
|
||||
__u8 type;
|
||||
};
|
||||
|
||||
/* Features specific Parameters */
|
||||
struct cold_reset {
|
||||
wait_queue_head_t read_wq;
|
||||
char *cmd_buf;
|
||||
uint16_t cmd_len;
|
||||
uint16_t rsp_len;
|
||||
/* Source of last ese protection command */
|
||||
uint8_t last_src_ese_prot;
|
||||
uint8_t status;
|
||||
/* Is cold reset protection enabled */
|
||||
bool is_crp_en;
|
||||
bool rsp_pending;
|
||||
/* Is NFC enabled from UI */
|
||||
bool is_nfc_enabled;
|
||||
};
|
||||
|
||||
struct nfc_dev;
|
||||
int ese_cold_reset_ioctl(struct nfc_dev *nfc_dev, unsigned long arg);
|
||||
int read_cold_reset_rsp(struct nfc_dev *nfc_dev, char *header);
|
||||
|
||||
#endif
|
||||
1017
drivers/nfc/sn2xx/nfc_common.c
Normal file
1017
drivers/nfc/sn2xx/nfc_common.c
Normal file
File diff suppressed because it is too large
Load diff
352
drivers/nfc/sn2xx/nfc_common.h
Normal file
352
drivers/nfc/sn2xx/nfc_common.h
Normal file
|
|
@ -0,0 +1,352 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
/*
|
||||
* Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
|
||||
* Copyright (c) 2019-2021 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 _NFC_COMMON_H_
|
||||
#define _NFC_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 <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 <linux/nfcinfo.h>
|
||||
#include <linux/regulator/consumer.h>
|
||||
#include <linux/ipc_logging.h>
|
||||
#include "nfc_i2c_drv.h"
|
||||
#include "ese_cold_reset.h"
|
||||
|
||||
// Max device count for this driver
|
||||
#define DEV_COUNT 1
|
||||
|
||||
// NFC device class
|
||||
#define CLASS_NAME "qti-nfc"
|
||||
|
||||
// NFC character device name, this will be in /dev/
|
||||
#define NFC_CHAR_DEV_NAME "nq-nci"
|
||||
|
||||
// NCI packet details
|
||||
#define NCI_MSG_CMD 0x20
|
||||
#define NCI_MSG_RSP 0x40
|
||||
#define NCI_MSG_NTF 0x60
|
||||
#define DL_CMD 0x00
|
||||
#define DL_PAYLOAD_BYTE_ZERO 0x00
|
||||
#define NCI_HDR_LEN 3
|
||||
#define NCI_PAYLOAD_IDX 3
|
||||
#define NCI_PAYLOAD_LEN_IDX 2
|
||||
|
||||
/*Time to wait for first NCI rest response*/
|
||||
#define NCI_RESET_RESP_READ_DELAY (10000) // 10ms
|
||||
#define NCI_RESET_RESP_TIMEOUT (500) // 500ms
|
||||
|
||||
// FW DNLD packet details
|
||||
#define FW_MSG_CMD_RSP 0x00
|
||||
#define FW_HDR_LEN 2
|
||||
#define FW_PAYLOAD_LEN_IDX 1
|
||||
#define FW_CRC_LEN 2
|
||||
|
||||
#define NCI_RSP_PKT_TYPE (0x40)
|
||||
#define FW_MIN_PAYLOAD_LEN 4
|
||||
#define MIN_NFC_DL_FRAME_SIZE 3
|
||||
|
||||
#define NCI_RESET_CMD_LEN (4)
|
||||
#define NCI_RESET_RSP_LEN (4)
|
||||
#define NCI_CORE_RESET_CMD_OID (0x0)
|
||||
#define NCI_CORE_RESET_CMD_PAYLOAD_LEN (0x1)
|
||||
#define NCI_CORE_RESET_KEEP_CONFIG (0x0)
|
||||
#define NCI_RESET_NTF_LEN (13)
|
||||
#define SN1XX_ROM_VER 0x01
|
||||
#define SN1xx_MAJOR_VER 0x10
|
||||
#define SN220_ROM_VER 0x01
|
||||
#define SN220_MAJOR_VER 0x01
|
||||
#define NXP557_ROM_VER 0x12
|
||||
#define NXP557_MAJOR_VER 0x00
|
||||
#define FW_ROM_CODE_VER_OFFSET 4
|
||||
#define FW_MAJOR_VER_OFFSET 7
|
||||
#define GET_VERSION_RSP_CHIP_TYPE_OFFSET 3
|
||||
#define GET_VERSION_RSP_MINOR_VERSION_OFFSET 6
|
||||
#define DL_GET_VERSION_CMD_LEN (8)
|
||||
#define DL_GET_VERSION_RSP_LEN_1 (12) /* SN110 */
|
||||
#define DL_GET_VERSION_RSP_LEN_2 (20) /* SN220 */
|
||||
#define DL_GET_VERSION_CMD_PAYLOAD_LEN (4)
|
||||
#define DL_GET_VERSION_CMD_ID (0xF1)
|
||||
#define DL_GET_VERSION_CMD_CRC_1 (0x6E)
|
||||
#define DL_GET_VERSION_CMD_CRC_2 (0xEF)
|
||||
|
||||
#define DL_RESET_CMD_LEN (8)
|
||||
#define DL_GET_SESSION_STATE_CMD_LEN (8)
|
||||
#define DL_GET_SESSION_STATE_RSP_LEN (8)
|
||||
#define DL_GET_SESSION_STATE_CMD_PAYLOAD_LEN (4)
|
||||
#define DL_GET_SESSION_CMD_ID (0xF2)
|
||||
#define DL_GET_SESSION_CMD_CRC_1 (0xF5)
|
||||
#define DL_GET_SESSION_CMD_CRC_2 (0x33)
|
||||
#define GET_SESSION_STS_OFF (3)
|
||||
#define NFCC_SESSION_STS_CLOSED (0x0)
|
||||
|
||||
/* Below offsets should be subtracted from NCI header length + payload length */
|
||||
|
||||
#define NFC_CHIP_TYPE_OFF (4)
|
||||
#define NFC_FW_MINOR_OFF (1)
|
||||
|
||||
#define GET_VERSION_CMD_LEN 8
|
||||
#define GET_SESSION_STATE_CMD_LEN 8
|
||||
#define MAX_NCI_PAYLOAD_LEN (255)
|
||||
#define MAX_BUFFER_SIZE (NCI_HDR_LEN + MAX_NCI_PAYLOAD_LEN)
|
||||
/*
|
||||
* 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_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
|
||||
/*Time to wait for NFCC to be ready again after any change in the GPIO*/
|
||||
#define NFC_GPIO_SET_WAIT_TIME_USEC (10000)
|
||||
/*Time to wait after soft reset via any NCI/DL cmd*/
|
||||
#define NFC_SOFT_RESET_WAIT_TIME_USEC (5000)
|
||||
/*Time to wait before retrying i2c writes*/
|
||||
#define WRITE_RETRY_WAIT_TIME_USEC (1000)
|
||||
/*Time to wait before retrying read for some specific usecases*/
|
||||
#define READ_RETRY_WAIT_TIME_USEC (3500)
|
||||
#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)
|
||||
|
||||
/* NFC HAL can call this ioctl to get the current IRQ state */
|
||||
#define NFC_GET_IRQ_STATE _IO(NFC_MAGIC, 0x06)
|
||||
|
||||
#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
|
||||
|
||||
|
||||
#define NUM_OF_IPC_LOG_PAGES (2)
|
||||
#define PKT_MAX_LEN (4) // no of max bytes to print for cmd/resp
|
||||
|
||||
#define GET_IPCLOG_MAX_PKT_LEN(c) ((c > PKT_MAX_LEN) ? PKT_MAX_LEN : c)
|
||||
|
||||
#define NFCLOG_IPC(nfc_dev, log_to_dmesg, x...) \
|
||||
do { \
|
||||
ipc_log_string(nfc_dev->ipcl, x); \
|
||||
if (log_to_dmesg) { \
|
||||
if (nfc_dev->nfc_device) \
|
||||
dev_err((nfc_dev->nfc_device), x); \
|
||||
else \
|
||||
pr_err(x); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
enum ese_ioctl_request {
|
||||
/* eSE POWER ON */
|
||||
ESE_POWER_ON = 0,
|
||||
/* eSE POWER OFF */
|
||||
ESE_POWER_OFF,
|
||||
/* 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,
|
||||
};
|
||||
|
||||
/*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
|
||||
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;
|
||||
};
|
||||
|
||||
// NFC Struct to get all the required configs from DTS
|
||||
struct platform_configs {
|
||||
struct platform_gpio gpio;
|
||||
struct platform_ldo ldo;
|
||||
};
|
||||
|
||||
enum chip_types {
|
||||
CHIP_SN1XX = 0x01,
|
||||
CHIP_SN220 = 0x02,
|
||||
CHIP_NXP557 = 0x03,
|
||||
CHIP_UNKNOWN = 0xFF,
|
||||
};
|
||||
|
||||
/* Device specific structure */
|
||||
struct nfc_dev {
|
||||
wait_queue_head_t read_wq;
|
||||
struct mutex read_mutex;
|
||||
struct mutex write_mutex;
|
||||
uint8_t *read_kbuf;
|
||||
uint8_t *write_kbuf;
|
||||
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;
|
||||
/* current firmware major version */
|
||||
uint8_t fw_major_version;
|
||||
bool is_vreg_enabled;
|
||||
bool is_ese_session_active;
|
||||
struct i2c_dev i2c_dev;
|
||||
struct platform_configs configs;
|
||||
struct cold_reset cold_reset;
|
||||
struct regulator *reg;
|
||||
|
||||
/* read buffer*/
|
||||
size_t kbuflen;
|
||||
u8 *kbuf;
|
||||
|
||||
union nqx_uinfo nqx_info;
|
||||
|
||||
void *ipcl;
|
||||
|
||||
int (*nfc_read)(struct nfc_dev *dev, char *buf, size_t count, int timeout);
|
||||
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);
|
||||
#ifdef CONFIG_COMPAT
|
||||
long nfc_compat_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg);
|
||||
#endif
|
||||
int nfc_parse_dt(struct device *dev, struct platform_configs *nfc_configs,
|
||||
uint8_t interface);
|
||||
int nfc_misc_register(struct nfc_dev *nfc_dev,
|
||||
const struct file_operations *nfc_fops, int count, char *devname,
|
||||
char *classname);
|
||||
void nfc_misc_unregister(struct nfc_dev *nfc_dev, int count);
|
||||
int configure_gpio(unsigned int gpio, int flag);
|
||||
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);
|
||||
int is_nfc_data_available_for_read(struct nfc_dev *nfc_dev);
|
||||
int validate_nfc_state_nci(struct nfc_dev *nfc_dev);
|
||||
void set_nfcc_state_from_rsp(struct nfc_dev *dev, const char *buf,
|
||||
const int count);
|
||||
void enable_dwnld_mode(struct nfc_dev *nfc_dev, bool value);
|
||||
#endif //_NFC_COMMON_H_
|
||||
582
drivers/nfc/sn2xx/nfc_i2c_drv.c
Normal file
582
drivers/nfc/sn2xx/nfc_i2c_drv.c
Normal file
|
|
@ -0,0 +1,582 @@
|
|||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
/*
|
||||
* Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
|
||||
* Copyright (c) 2013-2021 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 "nfc_common.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)
|
||||
{
|
||||
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 *nfc_dev, char *buf, size_t count, int timeout)
|
||||
{
|
||||
int ret;
|
||||
struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev;
|
||||
struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
|
||||
uint16_t i = 0;
|
||||
uint16_t disp_len = GET_IPCLOG_MAX_PKT_LEN(count);
|
||||
|
||||
pr_debug("%s : reading %zu bytes.\n", __func__, count);
|
||||
|
||||
if (timeout > NCI_CMD_RSP_TIMEOUT)
|
||||
timeout = NCI_CMD_RSP_TIMEOUT;
|
||||
|
||||
if (count > MAX_BUFFER_SIZE)
|
||||
count = MAX_BUFFER_SIZE;
|
||||
|
||||
if (!gpio_get_value(nfc_gpio->irq)) {
|
||||
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_gpio->irq)) {
|
||||
if (timeout) {
|
||||
ret = wait_event_interruptible_timeout(nfc_dev->read_wq,
|
||||
!i2c_dev->irq_enabled, msecs_to_jiffies(timeout));
|
||||
|
||||
if (ret <= 0) {
|
||||
pr_err("%s timeout/error in read\n", __func__);
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
ret = wait_event_interruptible(nfc_dev->read_wq,
|
||||
!i2c_dev->irq_enabled);
|
||||
if (ret) {
|
||||
pr_err("%s error wakeup of read wq\n", __func__);
|
||||
ret = -EINTR;
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
}
|
||||
i2c_disable_irq(nfc_dev);
|
||||
|
||||
if (gpio_get_value(nfc_gpio->irq))
|
||||
break;
|
||||
if (!gpio_get_value(nfc_gpio->ven)) {
|
||||
pr_info("%s: releasing read\n", __func__);
|
||||
ret = -EIO;
|
||||
goto err;
|
||||
}
|
||||
pr_warn("%s: spurious interrupt detected\n", __func__);
|
||||
}
|
||||
}
|
||||
|
||||
memset(buf, 0x00, count);
|
||||
/* Read data */
|
||||
ret = i2c_master_recv(nfc_dev->i2c_dev.client, buf, count);
|
||||
NFCLOG_IPC(nfc_dev, false, "%s of %zu bytes, ret %d", __func__, count,
|
||||
ret);
|
||||
if (ret <= 0) {
|
||||
pr_err("%s: returned %d\n", __func__, ret);
|
||||
goto err;
|
||||
}
|
||||
|
||||
for (i = 0; i < disp_len; i++)
|
||||
NFCLOG_IPC(nfc_dev, false, " %02x", buf[i]);
|
||||
|
||||
/* check if it's response of cold reset command
|
||||
* NFC HAL process shouldn't receive this data as
|
||||
* command was esepowermanager
|
||||
*/
|
||||
if (nfc_dev->cold_reset.rsp_pending && nfc_dev->cold_reset.cmd_buf
|
||||
&& (buf[0] == PROP_NCI_RSP_GID)
|
||||
&& (buf[1] == nfc_dev->cold_reset.cmd_buf[1])) {
|
||||
read_cold_reset_rsp(nfc_dev, buf);
|
||||
nfc_dev->cold_reset.rsp_pending = false;
|
||||
wake_up_interruptible(&nfc_dev->cold_reset.read_wq);
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
err:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int i2c_write(struct nfc_dev *nfc_dev, const char *buf, size_t count,
|
||||
int max_retry_cnt)
|
||||
{
|
||||
int ret = -EINVAL;
|
||||
int retry_cnt;
|
||||
uint16_t i = 0;
|
||||
uint16_t disp_len = GET_IPCLOG_MAX_PKT_LEN(count);
|
||||
|
||||
if (count > MAX_DL_BUFFER_SIZE)
|
||||
count = MAX_DL_BUFFER_SIZE;
|
||||
|
||||
pr_debug("%s : writing %zu bytes.\n", __func__, count);
|
||||
|
||||
NFCLOG_IPC(nfc_dev, false, "%s sending %zu B", __func__, count);
|
||||
|
||||
for (i = 0; i < disp_len; i++)
|
||||
NFCLOG_IPC(nfc_dev, false, " %02x", buf[i]);
|
||||
|
||||
for (retry_cnt = 1; retry_cnt <= max_retry_cnt; retry_cnt++) {
|
||||
|
||||
ret = i2c_master_send(nfc_dev->i2c_dev.client, buf, count);
|
||||
NFCLOG_IPC(nfc_dev, false, "%s ret %d", __func__, ret);
|
||||
if (ret <= 0) {
|
||||
pr_warn("%s: write failed ret %d, Maybe in Standby Mode - Retry(%d)\n",
|
||||
__func__, ret, retry_cnt);
|
||||
usleep_range(WRITE_RETRY_WAIT_TIME_USEC,
|
||||
WRITE_RETRY_WAIT_TIME_USEC + 100);
|
||||
} else if (ret != count) {
|
||||
pr_err("%s: failed to write %d\n", __func__, ret);
|
||||
ret = -EIO;
|
||||
} else if (ret == count)
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t nfc_i2c_dev_read(struct file *filp, char __user *buf,
|
||||
size_t count, loff_t *offset)
|
||||
{
|
||||
int ret = 0;
|
||||
struct nfc_dev *nfc_dev = (struct nfc_dev *)filp->private_data;
|
||||
|
||||
mutex_lock(&nfc_dev->read_mutex);
|
||||
if (filp->f_flags & O_NONBLOCK) {
|
||||
ret = i2c_master_recv(nfc_dev->i2c_dev.client, nfc_dev->read_kbuf, count);
|
||||
pr_debug("%s: NONBLOCK read ret = %d\n", __func__, ret);
|
||||
} else {
|
||||
ret = i2c_read(nfc_dev, nfc_dev->read_kbuf, count, 0);
|
||||
}
|
||||
if (ret > 0) {
|
||||
if (copy_to_user(buf, nfc_dev->read_kbuf, ret)) {
|
||||
pr_warn("%s : failed to copy to user space\n", __func__);
|
||||
ret = -EFAULT;
|
||||
}
|
||||
}
|
||||
mutex_unlock(&nfc_dev->read_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t nfc_i2c_dev_write(struct file *filp, const char __user *buf,
|
||||
size_t count, loff_t *offset)
|
||||
{
|
||||
int ret;
|
||||
struct nfc_dev *nfc_dev = (struct nfc_dev *)filp->private_data;
|
||||
|
||||
if (count > MAX_DL_BUFFER_SIZE)
|
||||
count = MAX_DL_BUFFER_SIZE;
|
||||
|
||||
if (!nfc_dev)
|
||||
return -ENODEV;
|
||||
|
||||
mutex_lock(&nfc_dev->write_mutex);
|
||||
if (copy_from_user(nfc_dev->write_kbuf, buf, count)) {
|
||||
pr_err("%s : failed to copy from user space\n", __func__);
|
||||
mutex_unlock(&nfc_dev->write_mutex);
|
||||
return -EFAULT;
|
||||
}
|
||||
ret = i2c_write(nfc_dev, nfc_dev->write_kbuf, count, NO_RETRY);
|
||||
mutex_unlock(&nfc_dev->write_mutex);
|
||||
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,
|
||||
#ifdef CONFIG_COMPAT
|
||||
.compat_ioctl = nfc_compat_dev_ioctl,
|
||||
#endif
|
||||
};
|
||||
|
||||
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_configs nfc_configs;
|
||||
struct platform_gpio *nfc_gpio = &nfc_configs.gpio;
|
||||
|
||||
pr_debug("%s: enter\n", __func__);
|
||||
|
||||
//retrieve details of gpios from dt
|
||||
|
||||
ret = nfc_parse_dt(&client->dev, &nfc_configs, PLATFORM_IF_I2C);
|
||||
if (ret) {
|
||||
pr_err("%s : failed to parse dt\n", __func__);
|
||||
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->read_kbuf = kzalloc(MAX_BUFFER_SIZE, GFP_DMA | GFP_KERNEL);
|
||||
if (!nfc_dev->read_kbuf) {
|
||||
ret = -ENOMEM;
|
||||
goto err_free_nfc_dev;
|
||||
}
|
||||
nfc_dev->write_kbuf = kzalloc(MAX_DL_BUFFER_SIZE, GFP_DMA | GFP_KERNEL);
|
||||
if (!nfc_dev->write_kbuf) {
|
||||
ret = -ENOMEM;
|
||||
goto err_free_read_kbuf;
|
||||
}
|
||||
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;
|
||||
nfc_dev->fw_major_version = 0;
|
||||
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_write_kbuf;
|
||||
}
|
||||
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);
|
||||
//not returning failure here as dwl gpio is a optional gpio for sn220
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/*copy the retrieved gpio details from DT */
|
||||
memcpy(&nfc_dev->configs, &nfc_configs, sizeof(struct platform_configs));
|
||||
|
||||
/* init mutex and queues */
|
||||
init_waitqueue_head(&nfc_dev->read_wq);
|
||||
mutex_init(&nfc_dev->read_mutex);
|
||||
mutex_init(&nfc_dev->write_mutex);
|
||||
mutex_init(&nfc_dev->dev_ref_mutex);
|
||||
spin_lock_init(&i2c_dev->irq_enabled_lock);
|
||||
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);
|
||||
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 || nfc_dev->nfc_state == NFC_STATE_UNKNOWN) {
|
||||
pr_err("nfc hw check failed ret %d\n", ret);
|
||||
}
|
||||
|
||||
device_init_wakeup(&client->dev, true);
|
||||
enable_irq_wake(client->irq);
|
||||
i2c_dev->irq_wake_up = false;
|
||||
nfc_dev->is_ese_session_active = false;
|
||||
|
||||
pr_info("%s success\n", __func__);
|
||||
return 0;
|
||||
|
||||
err_ldo_config_failed:
|
||||
if (nfc_dev->reg) {
|
||||
nfc_ldo_unvote(nfc_dev);
|
||||
regulator_put(nfc_dev->reg);
|
||||
}
|
||||
free_irq(client->irq, nfc_dev);
|
||||
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->write_mutex);
|
||||
gpio_free(nfc_gpio->clkreq);
|
||||
err_free_dwl_req:
|
||||
if (gpio_is_valid(nfc_gpio->dwl_req))
|
||||
gpio_free(nfc_gpio->dwl_req);
|
||||
gpio_free(nfc_gpio->irq);
|
||||
err_free_ven:
|
||||
gpio_free(nfc_gpio->ven);
|
||||
err_free_write_kbuf:
|
||||
kfree(nfc_dev->write_kbuf);
|
||||
err_free_read_kbuf:
|
||||
kfree(nfc_dev->read_kbuf);
|
||||
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;
|
||||
}
|
||||
|
||||
if (nfc_dev->dev_ref_count > 0) {
|
||||
pr_err("%s: device already in use\n", __func__);
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
gpio_set_value(nfc_dev->configs.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_unregister(nfc_dev, DEV_COUNT);
|
||||
mutex_destroy(&nfc_dev->dev_ref_mutex);
|
||||
mutex_destroy(&nfc_dev->read_mutex);
|
||||
mutex_destroy(&nfc_dev->write_mutex);
|
||||
|
||||
if (gpio_is_valid(nfc_dev->configs.gpio.clkreq))
|
||||
gpio_free(nfc_dev->configs.gpio.clkreq);
|
||||
|
||||
if (gpio_is_valid(nfc_dev->configs.gpio.dwl_req))
|
||||
gpio_free(nfc_dev->configs.gpio.dwl_req);
|
||||
|
||||
if (gpio_is_valid(nfc_dev->configs.gpio.irq))
|
||||
gpio_free(nfc_dev->configs.gpio.irq);
|
||||
|
||||
if (gpio_is_valid(nfc_dev->configs.gpio.ven))
|
||||
gpio_free(nfc_dev->configs.gpio.ven);
|
||||
|
||||
kfree(nfc_dev->read_kbuf);
|
||||
kfree(nfc_dev->write_kbuf);
|
||||
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 = NULL;
|
||||
|
||||
if (!nfc_dev) {
|
||||
pr_err("%s: device doesn't exist anymore\n", __func__);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
i2c_dev = &nfc_dev->i2c_dev;
|
||||
|
||||
NFCLOG_IPC(nfc_dev, false, "%s: irq_enabled = %d", __func__,
|
||||
i2c_dev->irq_enabled);
|
||||
|
||||
if (device_may_wakeup(&client->dev) && i2c_dev->irq_enabled) {
|
||||
//if (!enable_irq_wake(client->irq))
|
||||
// i2c_dev->irq_wake_up = true;
|
||||
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 = NULL;
|
||||
|
||||
if (!nfc_dev) {
|
||||
pr_err("%s: device doesn't exist anymore\n", __func__);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
i2c_dev = &nfc_dev->i2c_dev;
|
||||
|
||||
NFCLOG_IPC(nfc_dev, false, "%s: irq_wake_up = %d", __func__,
|
||||
i2c_dev->irq_wake_up);
|
||||
|
||||
if (device_may_wakeup(&client->dev) && i2c_dev->irq_wake_up) {
|
||||
//if (!disable_irq_wake(client->irq))
|
||||
// i2c_dev->irq_wake_up = false;
|
||||
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");
|
||||
54
drivers/nfc/sn2xx/nfc_i2c_drv.h
Normal file
54
drivers/nfc/sn2xx/nfc_i2c_drv.h
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
/*
|
||||
* Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
|
||||
* Copyright (c) 2019-2021 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 _NFC_I2C_DRV_H_
|
||||
#define _NFC_I2C_DRV_H_
|
||||
#include <linux/i2c.h>
|
||||
|
||||
#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, int timeout);
|
||||
|
||||
#endif //_NFC_I2C_DRV_H_
|
||||
Loading…
Reference in a new issue