1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528 |
- /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
- * Copyright (C) 2021 XiaoMi, Inc.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 and
- * only version 2 as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- */
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/fs.h>
- #include <linux/reboot.h>
- #include <linux/slab.h>
- #include <linux/irq.h>
- #include <linux/delay.h>
- #include <linux/interrupt.h>
- #include <linux/gpio.h>
- #include <linux/spinlock.h>
- #include <linux/of_gpio.h>
- #include <linux/of_device.h>
- #include <linux/uaccess.h>
- #include "pn553-19a.h"
- #include <linux/clk.h>
- #ifdef CONFIG_COMPAT
- #include <linux/compat.h>
- #endif
- /** [NFC_MODI] @{ */
- #define __MTK_GPIO_DEVM_API (1)
- /** [NFC_MODI] } */
- struct nqx_platform_data {
- unsigned int irq_gpio;
- unsigned int en_gpio;
- //unsigned int clkreq_gpio;
- unsigned int firm_gpio;
- //unsigned int ese_gpio;
- const char *clk_src_name;
- /* NFC_CLK pin voting state */
- bool clk_pin_voting;
- };
- #define DEV_COUNT 1
- #define DEVICE_NAME "nq-nci"
- #define CLASS_NAME "nq-nci"
- #define MAX_BUFFER_SIZE (320)
- #define WAKEUP_SRC_TIMEOUT (2000)
- #define MAX_RETRY_COUNT 3
- #define NCI_RESET_CMD_LEN 4
- #define NCI_INIT_CMD_LEN 3
- #define NCI_RESET_RSP_LEN 6
- #define NCI_INIT_RSP_LEN 28
- #define NCI_GET_VERSION_CMD_LEN 8
- #define NCI_GET_VERSION_RSP_LEN 12
- struct nqx_dev {
- wait_queue_head_t read_wq;
- struct mutex read_mutex;
- struct i2c_client *client;
- dev_t devno;
- struct class *nqx_class;
- struct device *nqx_device;
- struct cdev c_dev;
- union nqx_uinfo nqx_info;
- /* NFC GPIO variables */
- unsigned int irq_gpio;
- unsigned int en_gpio;
- unsigned int firm_gpio;
- //unsigned int clkreq_gpio;
- //unsigned int ese_gpio;
- /* NFC VEN pin state powered by Nfc */
- bool nfc_ven_enabled;
- /* NFC_IRQ state */
- bool irq_enabled;
- /* NFC_IRQ wake-up state */
- bool irq_wake_up;
- spinlock_t irq_enabled_lock;
- unsigned int count_irq;
- /* Initial CORE RESET notification */
- unsigned int core_reset_ntf;
- /* CLK control */
- bool clk_run;
- struct clk *s_clk;
- /* read buffer*/
- size_t kbuflen;
- u8 *kbuf;
- struct nqx_platform_data *pdata;
- };
- static int nfcc_reboot(struct notifier_block *notifier, unsigned long val,
- void *v);
- /*clock enable function*/
- static int nqx_clock_select(struct nqx_dev *nqx_dev);
- /*clock disable function*/
- static int nqx_clock_deselect(struct nqx_dev *nqx_dev);
- static struct notifier_block nfcc_notifier = {
- .notifier_call = nfcc_reboot,
- .next = NULL,
- .priority = 0
- };
- static unsigned int disable_ctrl;
- #define MAX_I2C_DUMP_SIZE 512
- /**
- * [NFC_MODI] @{
- * debug()
- *
- * Print debug log by using printk
- *
- */
- #define __LOGTAG__ "NFCC_SN100T"
- #define ROY_DEBUG (1)
- #ifdef ROY_DEBUG
- #ifdef __LOGTAG__
- #define debug(fmt, arg...) do {\
- printk("ROY %s::%s "fmt, __LOGTAG__, __func__, ##arg);\
- } while (0);
- #define logerr(fmt, arg...) do {\
- printk(KERN_ERR"ROY %s::%s "fmt, __LOGTAG__, __func__, ##arg);\
- } while (0);
- #else
- #define debug(fmt, arg...) printk("ROY [%s ]"fmt, __func__, ##arg)
- #define logerr(fmt, arg...) printk(KERN_ERR"ROY %s "fmt, __func__, ##arg);
- #endif
- #else
- #define debug(fmt, arg...)
- #define logerr(fmt, arg...) printk(KERN_ERR"ROY %s "fmt, __func__, ##arg);
- #endif
- /** [NFC_MODI] } */
- static void print_send_buffer(struct nqx_dev *nqx_dev, unsigned char *buf, int len)
- {
- unsigned char output[MAX_I2C_DUMP_SIZE * 2 + 1];
- int i;
- debug(" Entry\n");
- if (len > MAX_I2C_DUMP_SIZE)
- len = MAX_I2C_DUMP_SIZE - 1;
- for (i = 0; i < len; i++) {
- snprintf(output + i * 2, 3, "%02x ", buf[i]);
- }
- dev_warn(&nqx_dev->client->dev, "%3d > %s\n", len, output);
- }
- static void print_recv_buffer(struct nqx_dev *nqx_dev, unsigned char *buf, int len)
- {
- unsigned char output[MAX_I2C_DUMP_SIZE * 2 + 1];
- int i;
- debug(" Entry\n");
- if (len > MAX_I2C_DUMP_SIZE)
- len = MAX_I2C_DUMP_SIZE - 1;
- for (i = 0; i < len; i++) {
- snprintf(output + i * 2, 3, "%02x ", buf[i]);
- }
- dev_warn(&nqx_dev->client->dev, "%3d < %s\n", len, output);
- }
- static void nqx_init_stat(struct nqx_dev *nqx_dev)
- {
- debug(" Entry\n");
- nqx_dev->count_irq = 0;
- }
- /**
- * nqx_enable_irq()
- *
- * Check if interrupt is enabled or not
- * and enable interrupt
- *
- * Return: void
- */
- static void nqx_enable_irq(struct nqx_dev *nqx_dev)
- {
- unsigned long flags;
- debug(" Entry\n");
- spin_lock_irqsave(&nqx_dev->irq_enabled_lock, flags);
- //if (!nqx_dev->irq_enabled) {
- nqx_dev->irq_enabled = true;
- enable_irq(nqx_dev->client->irq);
- //}
- spin_unlock_irqrestore(&nqx_dev->irq_enabled_lock, flags);
- }
- /**
- * nqx_disable_irq()
- *
- * Check if interrupt is enabled or not
- * and disable interrupt
- *
- * Return: void
- */
- static void nqx_disable_irq(struct nqx_dev *nqx_dev)
- {
- unsigned long flags;
- debug(" Entry\n");
- spin_lock_irqsave(&nqx_dev->irq_enabled_lock, flags);
- if (nqx_dev->irq_enabled) {
- disable_irq_nosync(nqx_dev->client->irq);
- nqx_dev->irq_enabled = false;
- }
- spin_unlock_irqrestore(&nqx_dev->irq_enabled_lock, flags);
- }
- static irqreturn_t nqx_dev_irq_handler(int irq, void *dev_id)
- {
- struct nqx_dev *nqx_dev = dev_id;
- unsigned long flags;
- debug(" Entry\n");
- if (device_may_wakeup(&nqx_dev->client->dev))
- pm_wakeup_event(&nqx_dev->client->dev, WAKEUP_SRC_TIMEOUT);
- nqx_disable_irq(nqx_dev);
- spin_lock_irqsave(&nqx_dev->irq_enabled_lock, flags);
- nqx_dev->count_irq++;
- spin_unlock_irqrestore(&nqx_dev->irq_enabled_lock, flags);
- /* Wake up waiting readers */
- wake_up(&nqx_dev->read_wq);
- return IRQ_HANDLED;
- }
- static ssize_t nfc_read(struct file *filp, char __user *buf,
- size_t count, loff_t *offset)
- {
- struct nqx_dev *nqx_dev = filp->private_data;
- unsigned char *tmp = NULL;
- int ret;
- int irq_gpio_val = 0;
- if (!nqx_dev) {
- ret = -ENODEV;
- goto out;
- }
- if (count > nqx_dev->kbuflen)
- count = nqx_dev->kbuflen;
- debug(" Entry reading %zu bytes.\n", count);
- mutex_lock(&nqx_dev->read_mutex);
- irq_gpio_val = gpio_get_value(nqx_dev->irq_gpio);
- debug(" irq_gpio_val %d.\n", irq_gpio_val);
- if (irq_gpio_val == 0) {
- if (filp->f_flags & O_NONBLOCK) {
- dev_err(&nqx_dev->client->dev,
- ":f_falg has O_NONBLOCK. EAGAIN\n");
- ret = -EAGAIN;
- goto err;
- }
- while (1) {
- ret = 0;
- if (!nqx_dev->irq_enabled) {
- nqx_dev->irq_enabled = true;
- enable_irq(nqx_dev->client->irq);
- }
- if (!gpio_get_value(nqx_dev->irq_gpio)) {
- ret = wait_event_interruptible(nqx_dev->read_wq,
- !nqx_dev->irq_enabled);
- }
- if (ret)
- goto err;
- nqx_disable_irq(nqx_dev);
- if (gpio_get_value(nqx_dev->irq_gpio))
- break;
- dev_err_ratelimited(&nqx_dev->client->dev,
- "gpio is low, no need to read data\n");
- }
- }
- tmp = nqx_dev->kbuf;
- if (!tmp) {
- dev_err(&nqx_dev->client->dev,
- "%s: device doesn't exist anymore\n", __func__);
- ret = -ENODEV;
- goto err;
- }
- memset(tmp, 0x00, count);
- /* Read data */
- pr_err("%s : client address: %d \n", __func__, &nqx_dev->client->addr);
- ret = i2c_master_recv(nqx_dev->client, tmp, count);
- if (ret < 0) {
- dev_err(&nqx_dev->client->dev,
- "%s: i2c_master_recv returned %d\n", __func__, ret);
- goto err;
- }
- if (ret > count) {
- dev_err(&nqx_dev->client->dev,
- "%s: received too many bytes from i2c (%d)\n",
- __func__, ret);
- ret = -EIO;
- goto err;
- }
- #ifdef NFC_KERNEL_BU
- printk("htqhtqhtq %s : NfcNciRx %x %x %x\n",
- __func__, tmp[0], tmp[1], tmp[2]);
- #endif
- print_recv_buffer(nqx_dev, tmp, ret);
- if (copy_to_user(buf, tmp, ret)) {
- dev_warn(&nqx_dev->client->dev,
- "%s : failed to copy to user space\n", __func__);
- ret = -EFAULT;
- goto err;
- }
- mutex_unlock(&nqx_dev->read_mutex);
- return ret;
- err:
- mutex_unlock(&nqx_dev->read_mutex);
- out:
- return ret;
- }
- static ssize_t nfc_write(struct file *filp, const char __user *buf,
- size_t count, loff_t *offset)
- {
- struct nqx_dev *nqx_dev = filp->private_data;
- char *tmp = NULL;
- int ret = 0;
- debug(" Entry\n");
- if (!nqx_dev) {
- ret = -ENODEV;
- goto out;
- }
- if (count > nqx_dev->kbuflen) {
- dev_err(&nqx_dev->client->dev, "%s: out of memory\n",
- __func__);
- ret = -ENOMEM;
- goto out;
- }
- tmp = memdup_user(buf, count);
- if (IS_ERR(tmp)) {
- dev_err(&nqx_dev->client->dev, "%s: memdup_user failed\n",
- __func__);
- ret = PTR_ERR(tmp);
- goto out;
- }
- ret = i2c_master_send(nqx_dev->client, tmp, count);
- if (ret != count) {
- dev_err(&nqx_dev->client->dev,
- "%s: failed to write %d\n", __func__, ret);
- ret = -EIO;
- goto out_free;
- }
- #ifdef NFC_KERNEL_BU
- printk("htqhtqhtq %s : i2c-%d: NfcNciTx %x %x %x\n",
- __func__, iminor(file_inode(filp)),
- tmp[0], tmp[1], tmp[2]);
- #endif
- print_send_buffer(nqx_dev, tmp, count);
- usleep_range(1000, 1100);
- out_free:
- kfree(tmp);
- out:
- return ret;
- }
- /**
- * nqx_standby_write()
- * @buf: pointer to data buffer
- * @len: # of bytes need to transfer
- *
- * write data buffer over I2C and retry
- * if NFCC is in stand by mode
- *
- * Return: # of bytes written or -ve value in case of error
- */
- static int nqx_standby_write(struct nqx_dev *nqx_dev,
- const unsigned char *buf, size_t len)
- {
- int ret = -EINVAL;
- int retry_cnt;
- debug(" Entry\n");
- for (retry_cnt = 1; retry_cnt <= MAX_RETRY_COUNT; retry_cnt++) {
- pr_err("%s : client address: %d \n", __func__, &nqx_dev->client->addr);
- ret = i2c_master_send(nqx_dev->client, buf, len);
- if (ret < 0) {
- dev_err(&nqx_dev->client->dev,
- "%s: write failed, Maybe in Standby Mode - Retry(%d)\n",
- __func__, retry_cnt);
- usleep_range(1000, 1100);
- } else if (ret == len)
- break;
- }
- return ret;
- }
- /*
- * Power management of the eSE
- * NFC & eSE ON : NFC_EN high and eSE_pwr_req high.
- * NFC OFF & eSE ON : NFC_EN high and eSE_pwr_req high.
- * NFC OFF & eSE OFF : NFC_EN low and eSE_pwr_req low.
- */
- static int nqx_ese_pwr(struct nqx_dev *nqx_dev, unsigned long int arg)
- {
- int r = -1;
- const unsigned char svdd_off_cmd_warn[] = {0x2F, 0x31, 0x01, 0x01};
- const unsigned char svdd_off_cmd_done[] = {0x2F, 0x31, 0x01, 0x00};
- if (arg == 0) {
- /*
- * We want to power on the eSE and to do so we need the
- * eSE_pwr_req pin and the NFC_EN pin to be high
- */
- /**
- * Let's store the NFC_EN pin state
- * only if the eSE is not yet on
- */
- nqx_dev->nfc_ven_enabled = gpio_get_value(nqx_dev->en_gpio);
- if (!nqx_dev->nfc_ven_enabled) {
- gpio_set_value(nqx_dev->en_gpio, 1);
- /* hardware dependent delay */
- usleep_range(10000, 11000);
- printk("we going to set ven value 1");
- }
- gpio_set_value(nqx_dev->en_gpio, 1);
- usleep_range(10000, 11000);
- printk("p61 nxp the ven value is %d",
- gpio_get_value(nqx_dev->en_gpio));
- r = 0;
- } else if (arg == 1) {
- if (nqx_dev->nfc_ven_enabled &&
- ((nqx_dev->nqx_info.info.chip_type == NFCC_NQ_220) ||
- (nqx_dev->nqx_info.info.chip_type == NFCC_PN66T))) {
- /**
- * Let's inform the CLF we're
- * powering off the eSE
- */
- r = nqx_standby_write(nqx_dev, svdd_off_cmd_warn,
- sizeof(svdd_off_cmd_warn));
- if (r < 0) {
- dev_err(&nqx_dev->client->dev,
- "%s: write failed after max retry\n",
- __func__);
- return -ENXIO;
- }
- dev_err(&nqx_dev->client->dev,
- "htqhtqhtq %s: svdd_off_cmd_warn sent\n", __func__);
- /**
- * Time needed for the SVDD capacitor
- * to get discharged
- */
- usleep_range(8000, 8100);
- /* Let's inform the CLF the eSE is now off */
- r = nqx_standby_write(nqx_dev, svdd_off_cmd_done,
- sizeof(svdd_off_cmd_done));
- if (r < 0) {
- dev_err(&nqx_dev->client->dev,
- "%s: write failed after max retry\n",
- __func__);
- return -ENXIO;
- }
- dev_err(&nqx_dev->client->dev,
- "%s: svdd_off_cmd_done sent\n", __func__);
- } else {
- /**
- * In case the NFC is off,
- * there's no need to send the i2c commands
- */
- usleep_range(1000, 1100);
- }
- if (!nqx_dev->nfc_ven_enabled) {
- /* hardware dependent delay */
- usleep_range(1000, 1100);
- dev_err(&nqx_dev->client->dev, "disabling en_gpio\n");
- gpio_set_value(nqx_dev->en_gpio, 0);
- }
- }
- return r;
- }
- static int nfc_open(struct inode *inode, struct file *filp)
- {
- int ret = 0;
- struct nqx_dev *nqx_dev = container_of(inode->i_cdev,
- struct nqx_dev, c_dev);
- filp->private_data = nqx_dev;
- nqx_init_stat(nqx_dev);
- dev_err(&nqx_dev->client->dev,
- "%s: %d,%d\n", __func__, imajor(inode), iminor(inode));
- return ret;
- }
- /*
- * nfc_ioctl_power_states() - power control
- * @filp: pointer to the file descriptor
- * @arg: mode that we want to move to
- *
- * Device power control. Depending on the arg value, device moves to
- * different states
- * (arg = 0): NFC_ENABLE GPIO = 0, FW_DL GPIO = 0
- * (arg = 1): NFC_ENABLE GPIO = 1, FW_DL GPIO = 0
- * (arg = 2): FW_DL GPIO = 1
- *
- * Return: -ENOIOCTLCMD if arg is not supported, 0 in any other case
- */
- int nfc_ioctl_power_states(struct file *filp, unsigned long arg)
- {
- int r = 0;
- struct nqx_dev *nqx_dev = filp->private_data;
- debug(" Entry arg = %d.\n", arg);
- if (arg == 0) {
- /*
- * We are attempting a hardware reset so let us disable
- * interrupts to avoid spurious notifications to upper
- * layers.
- */
- printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
- /**
- * NFCC_DNLD output LOW
- * NFCC_VEN output LOW
- */
- // nqx_disable_irq(nqx_dev);
- dev_err(&nqx_dev->client->dev,
- "gpio_set_value disable: %s: info: %p\n",
- __func__, nqx_dev);
- //if (gpio_is_valid(nqx_dev->firm_gpio)) {
- gpio_set_value(nqx_dev->firm_gpio, 0);
- usleep_range(10000, 10100);
- //}
- {
- dev_err(&nqx_dev->client->dev, "ese_gpio invalid, set en_gpio to low\n");
- gpio_set_value(nqx_dev->en_gpio, 0);
- usleep_range(10000, 10100);
- }
- if (nqx_dev->pdata->clk_pin_voting) {
- r = nqx_clock_deselect(nqx_dev);
- if (r < 0)
- dev_err(&nqx_dev->client->dev, "unable to disable clock\n");
- }
- nqx_dev->nfc_ven_enabled = false;
- } else if (arg == 1) {
- printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
- /**
- * NFCC_DNLD output LOW
- * NFCC_VEN output HIGH
- */
- // nqx_enable_irq(nqx_dev);
- dev_err(&nqx_dev->client->dev,
- "gpio_set_value enable: %s: info: %p\n",
- __func__, nqx_dev);
- //if (gpio_is_valid(nqx_dev->firm_gpio)) {
- gpio_set_value(nqx_dev->firm_gpio, 0);
- usleep_range(10000, 10100);
- //}
- gpio_set_value(nqx_dev->en_gpio, 1);
- usleep_range(10000, 10100);
- if (nqx_dev->pdata->clk_pin_voting) {
- r = nqx_clock_select(nqx_dev);
- if (r < 0)
- dev_err(&nqx_dev->client->dev, "unable to enable clock\n");
- }
- nqx_dev->nfc_ven_enabled = true;
- printk("p61 nxp the ven value is %d",
- gpio_get_value(nqx_dev->en_gpio));
- } else if (arg == 2) {
- /*
- * We are switching to Dowload Mode, toggle the enable pin
- * in order to set the NFCC in the new mode
- */
- gpio_set_value(nqx_dev->en_gpio, 1);
- usleep_range(10000, 10100);
- if (gpio_is_valid(nqx_dev->firm_gpio)) {
- gpio_set_value(nqx_dev->firm_gpio, 1);
- usleep_range(10000, 10100);
- }
- gpio_set_value(nqx_dev->en_gpio, 0);
- usleep_range(10000, 10100);
- gpio_set_value(nqx_dev->en_gpio, 1);
- usleep_range(10000, 10100);
- } else if (arg == 4) {
- /*
- * Setting firmware download gpio to HIGH for SN100U
- * before FW download start
- */
- dev_err(&nqx_dev->client->dev, "SN100 fw gpio HIGH\n");
- if (gpio_is_valid(nqx_dev->firm_gpio)) {
- gpio_set_value(nqx_dev->firm_gpio, 1);
- usleep_range(10000, 10100);
- } else
- dev_err(&nqx_dev->client->dev,
- "firm_gpio is invalid\n");
- } else if (arg == 6) {
- /*
- * Setting firmware download gpio to LOW for SN100U
- * FW download finished
- */
- dev_err(&nqx_dev->client->dev, "SN100 fw gpio LOW\n");
- if (gpio_is_valid(nqx_dev->firm_gpio)) {
- gpio_set_value(nqx_dev->firm_gpio, 0);
- usleep_range(10000, 10100);
- } else {
- dev_err(&nqx_dev->client->dev,
- "firm_gpio is invalid\n");
- }
- } else {
- r = -ENOIOCTLCMD;
- }
- return r;
- }
- #ifdef CONFIG_COMPAT
- static long nfc_compat_ioctl(struct file *pfile, unsigned int cmd,
- unsigned long arg)
- {
- long r = 0;
- printk("------------------------------------------------htqhtqhtq------------------------------------------- %s:", __func__);
- arg = (compat_u64)arg;
- switch (cmd) {
- case NFC_SET_PWR:
- nfc_ioctl_power_states(pfile, arg);
- break;
- case ESE_SET_PWR:
- nqx_ese_pwr(pfile->private_data, arg);
- break;
- case ESE_GET_PWR:
- nqx_ese_pwr(pfile->private_data, 3);
- break;
- case SET_RX_BLOCK:
- break;
- case SET_EMULATOR_TEST_POINT:
- break;
- default:
- r = -ENOTTY;
- }
- return r;
- }
- #endif
- /*
- * nfc_ioctl_core_reset_ntf()
- * @filp: pointer to the file descriptor
- *
- * Allows callers to determine if a CORE_RESET_NTF has arrived
- *
- * Return: the value of variable core_reset_ntf
- */
- int nfc_ioctl_core_reset_ntf(struct file *filp)
- {
- struct nqx_dev *nqx_dev = filp->private_data;
- dev_err(&nqx_dev->client->dev, "%s: returning = %d\n", __func__,
- nqx_dev->core_reset_ntf);
- return nqx_dev->core_reset_ntf;
- }
- /*
- * Inside nfc_ioctl_nfcc_info
- *
- * @brief nfc_ioctl_nfcc_info
- *
- * Check the NQ Chipset and firmware version details
- */
- unsigned int nfc_ioctl_nfcc_info(struct file *filp, unsigned long arg)
- {
- unsigned int r = 0;
- struct nqx_dev *nqx_dev = filp->private_data;
- r = nqx_dev->nqx_info.i;
- dev_err(&nqx_dev->client->dev,
- "nqx nfc : %s r = %d\n", __func__, r);
- return r;
- }
- static long nfc_ioctl(struct file *pfile, unsigned int cmd,
- unsigned long arg)
- {
- int r = 0;
- debug(" Entry cmd[%d], arg[%d]:\n", cmd, arg);
- switch (cmd) {
- case NFC_SET_PWR:
- r = nfc_ioctl_power_states(pfile, arg);
- break;
- case ESE_SET_PWR:
- r = nqx_ese_pwr(pfile->private_data, arg);
- break;
- case ESE_GET_PWR:
- r = nqx_ese_pwr(pfile->private_data, 3);
- break;
- case SET_RX_BLOCK:
- break;
- case SET_EMULATOR_TEST_POINT:
- break;
- case NFCC_INITIAL_CORE_RESET_NTF:
- r = nfc_ioctl_core_reset_ntf(pfile);
- break;
- case NFCC_GET_INFO:
- r = nfc_ioctl_nfcc_info(pfile, arg);
- break;
- default:
- r = -ENOIOCTLCMD;
- }
- return r;
- }
- static const struct file_operations nfc_dev_fops = {
- .owner = THIS_MODULE,
- .llseek = no_llseek,
- .read = nfc_read,
- .write = nfc_write,
- .open = nfc_open,
- .unlocked_ioctl = nfc_ioctl,
- #ifdef CONFIG_COMPAT
- .compat_ioctl = nfc_compat_ioctl
- #endif
- };
- /**
- * Do not need check availability of NFCC.
- * This function will block NFCC to enter FW download mode.
- */
- #if 0
- /* Check for availability of NQ_ NFC controller hardware */
- static int nfcc_hw_check(struct i2c_client *client, struct nqx_dev *nqx_dev)
- {
- int ret = 0;
- int gpio_retry_count = 0;
- unsigned char init_rsp_len = 0;
- unsigned int enable_gpio = nqx_dev->en_gpio;
- char *nci_reset_cmd = NULL;
- char *nci_init_cmd = NULL;
- char *nci_init_rsp = NULL;
- char *nci_reset_rsp = NULL;
- char *nci_get_version_cmd = NULL;
- char *nci_get_version_rsp = NULL;
- debug(" Entry\n");
- nci_reset_cmd = kzalloc(NCI_RESET_CMD_LEN + 1, GFP_DMA | GFP_KERNEL);
- if (!nci_reset_cmd) {
- ret = -ENOMEM;
- goto done;
- }
- nci_reset_rsp = kzalloc(NCI_RESET_RSP_LEN + 1, GFP_DMA | GFP_KERNEL);
- if (!nci_reset_rsp) {
- ret = -ENOMEM;
- goto done;
- }
- nci_init_cmd = kzalloc(NCI_INIT_CMD_LEN + 1, GFP_DMA | GFP_KERNEL);
- if (!nci_init_cmd) {
- ret = -ENOMEM;
- goto done;
- }
- nci_init_rsp = kzalloc(NCI_INIT_RSP_LEN + 1, GFP_DMA | GFP_KERNEL);
- if (!nci_init_rsp) {
- 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;
- }
- reset_enable_gpio:
- /* making sure that the NFCC starts in a clean state. */
- gpio_set_value(enable_gpio, 0);/* ULPM: Disable */
- /* hardware dependent delay */
- usleep_range(10000, 10100);
- gpio_set_value(enable_gpio, 1);/* HPD : Enable*/
- /* hardware dependent delay */
- usleep_range(10000, 10100);
- nci_reset_cmd[0] = 0x20;
- nci_reset_cmd[1] = 0x00;
- nci_reset_cmd[2] = 0x01;
- nci_reset_cmd[3] = 0x00;
- debug(" 1. Send NCI CORE RESET CMD.\n");
- /* send NCI CORE RESET CMD with Keep Config parameters */
- ret = i2c_master_send(client, nci_reset_cmd, NCI_RESET_CMD_LEN);
- if (ret < 0) {
- dev_err(&client->dev,
- "%s: - i2c_master_send core reset Error\n", __func__);
- //if (gpio_is_valid(nqx_dev->firm_gpio)) {
- gpio_set_value(nqx_dev->firm_gpio, 1);
- usleep_range(10000, 10100);
- //}
- gpio_set_value(nqx_dev->en_gpio, 0);
- usleep_range(10000, 10100);
- gpio_set_value(nqx_dev->en_gpio, 1);
- usleep_range(10000, 10100);
- 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;
- debug(" 1-1. If Reset Failed, Send GET_VERSION CMD.\n");
- ret = i2c_master_send(client, nci_get_version_cmd,
- NCI_GET_VERSION_CMD_LEN);
- if (ret < 0) {
- dev_err(&client->dev,
- "%s: - i2c_master_send get version cmd Error\n",
- __func__);
- goto err_nfcc_hw_check;
- }
- /* hardware dependent delay */
- usleep_range(10000, 10100);
- ret = i2c_master_recv(client, nci_get_version_rsp,
- NCI_GET_VERSION_RSP_LEN);
- if (ret < 0) {
- dev_err(&client->dev,
- "%s: - i2c_master_recv get version rsp Error\n",
- __func__);
- goto err_nfcc_hw_check;
- } else {
- nqx_dev->nqx_info.info.chip_type =
- nci_get_version_rsp[3];
- nqx_dev->nqx_info.info.rom_version =
- nci_get_version_rsp[4];
- nqx_dev->nqx_info.info.fw_minor =
- nci_get_version_rsp[6];
- nqx_dev->nqx_info.info.fw_major =
- nci_get_version_rsp[7];
- dev_err(&nqx_dev->client->dev, "NQ fw version = %x.%x.%x.%x\n",
- nqx_dev->nqx_info.info.chip_type,
- nqx_dev->nqx_info.info.rom_version,
- nqx_dev->nqx_info.info.fw_major,
- nqx_dev->nqx_info.info.fw_minor);
- }
- goto err_nfcc_reset_failed;
- }
- /* hardware dependent delay */
- msleep(30);
- debug(" 1<-. Get NCI CORE RESET CMD RSP.\n");
- /* Read Response of RESET command */
- ret = i2c_master_recv(client, nci_reset_rsp, NCI_RESET_RSP_LEN);
- if (ret < 0) {
- dev_err(&client->dev,
- "%s: - i2c_master_recv Error\n", __func__);
- gpio_retry_count = gpio_retry_count + 1;
- if (gpio_retry_count < MAX_RETRY_COUNT)
- goto reset_enable_gpio;
- goto err_nfcc_hw_check;
- }
- nci_init_cmd[0] = 0x20;
- nci_init_cmd[1] = 0x01;
- nci_init_cmd[2] = 0x00;
- debug(" 2->. Send NCI CORE INIT CMD RSP.\n");
- ret = nqx_standby_write(nqx_dev, nci_init_cmd, NCI_INIT_CMD_LEN);
- if (ret < 0) {
- dev_err(&client->dev,
- "%s: - i2c_master_send failed for Core INIT\n", __func__);
- goto err_nfcc_core_init_fail;
- }
- /* hardware dependent delay */
- msleep(30);
- /* Read Response of INIT command */
- debug(" 2<-. Get NCI CORE INIT CMD RSP.\n");
- ret = i2c_master_recv(client, nci_init_rsp, NCI_INIT_RSP_LEN);
- if (ret < 0) {
- printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
- dev_err(&client->dev,
- "%s: - i2c_master_recv Error\n", __func__);
- goto err_nfcc_core_init_fail;
- }
- printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
- init_rsp_len = 2 + nci_init_rsp[2]; /*payload + len*/
- if (init_rsp_len > PAYLOAD_HEADER_LENGTH) {
- printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
- nqx_dev->nqx_info.info.chip_type =
- nci_init_rsp[init_rsp_len - 3];
- nqx_dev->nqx_info.info.rom_version =
- nci_init_rsp[init_rsp_len - 2];
- nqx_dev->nqx_info.info.fw_major =
- nci_init_rsp[init_rsp_len - 1];
- nqx_dev->nqx_info.info.fw_minor =
- nci_init_rsp[init_rsp_len];
- }
- dev_err(&client->dev,
- "%s: - nq - reset cmd answer : NfcNciRx %x %x %x\n",
- __func__, nci_reset_rsp[0],
- nci_reset_rsp[1], nci_reset_rsp[2]);
- err_nfcc_reset_failed:
- dev_err(&nqx_dev->client->dev, "NQ NFCC chip_type = %x\n",
- nqx_dev->nqx_info.info.chip_type);
- dev_err(&nqx_dev->client->dev, "NQ fw version = %x.%x.%x\n",
- nqx_dev->nqx_info.info.rom_version,
- nqx_dev->nqx_info.info.fw_major,
- nqx_dev->nqx_info.info.fw_minor);
- switch (nqx_dev->nqx_info.info.chip_type) {
- case NFCC_NQ_210:
- dev_err(&client->dev,
- "%s: ## NFCC == NQ210 ##\n", __func__);
- break;
- case NFCC_NQ_220:
- dev_err(&client->dev,
- "%s: ## NFCC == NQ220 ##\n", __func__);
- break;
- case NFCC_NQ_310:
- dev_err(&client->dev,
- "%s: ## NFCC == NQ310 ##\n", __func__);
- break;
- case NFCC_NQ_330:
- dev_err(&client->dev,
- "%s: ## NFCC == NQ330 ##\n", __func__);
- break;
- case NFCC_PN66T:
- dev_err(&client->dev,
- "%s: ## NFCC == PN66T ##\n", __func__);
- break;
- case NFCC_SN100T:
- debug(" ## NFCC == SN100T ##\n");
- break;
- default:
- dev_err(&client->dev,
- "%s: - NFCC HW not Supported\n", __func__);
- break;
- }
- /*Disable NFC by default to save power on boot*/
- gpio_set_value(enable_gpio, 0);/* ULPM: Disable */
- ret = 0;
- goto done;
- err_nfcc_core_init_fail:
- dev_err(&client->dev,
- "%s: - nq - reset cmd answer : NfcNciRx %x %x %x\n",
- __func__, nci_reset_rsp[0],
- nci_reset_rsp[1], nci_reset_rsp[2]);
- err_nfcc_hw_check:
- ret = -ENXIO;
- dev_err(&client->dev,
- "%s: - NFCC HW not available\n", __func__);
- done:
- kfree(nci_reset_rsp);
- kfree(nci_init_rsp);
- kfree(nci_init_cmd);
- kfree(nci_reset_cmd);
- kfree(nci_get_version_cmd);
- kfree(nci_get_version_rsp);
- debug(" ret=[%]\n", ret);
- return ret;
- }
- #endif
- /*
- * Routine to enable clock.
- * this routine can be extended to select from multiple
- * sources based on clk_src_name.
- */
- static int nqx_clock_select(struct nqx_dev *nqx_dev)
- {
- int r = 0;
- printk("------------------------------------------------htqhtqhtq------------------------------------------- %s:", __func__);
- nqx_dev->s_clk = clk_get(&nqx_dev->client->dev, "ref_clk");
- if (nqx_dev->s_clk == NULL)
- goto err_clk;
- if (nqx_dev->clk_run == false)
- r = clk_prepare_enable(nqx_dev->s_clk);
- if (r)
- goto err_clk;
- nqx_dev->clk_run = true;
- return r;
- err_clk:
- r = -1;
- return r;
- }
- /*
- * Routine to disable clocks
- */
- static int nqx_clock_deselect(struct nqx_dev *nqx_dev)
- {
- int r = -1;
- printk("------------------------------------------------htqhtqhtq------------------------------------------- %s:", __func__);
- if (nqx_dev->s_clk != NULL) {
- if (nqx_dev->clk_run == true) {
- clk_disable_unprepare(nqx_dev->s_clk);
- nqx_dev->clk_run = false;
- }
- return 0;
- }
- return r;
- }
- static int nfc_parse_dt(struct device *dev, struct nqx_platform_data *pdata)
- {
- int r = 0;
- struct device_node *np = dev->of_node;
- pdata->en_gpio = of_get_named_gpio(np, "nq-ven", 0);
- if ((!gpio_is_valid(pdata->en_gpio)))
- return -EINVAL;
- disable_ctrl = pdata->en_gpio;
- pdata->irq_gpio = of_get_named_gpio(np, "nq-irq", 0);
- if ((!gpio_is_valid(pdata->irq_gpio)))
- return -EINVAL;
- pdata->firm_gpio = of_get_named_gpio(np, "nq-firm", 0);
- if (!gpio_is_valid(pdata->firm_gpio)) {
- dev_warn(dev,
- "FIRM GPIO <OPTIONAL> error getting from OF node\n");
- pdata->firm_gpio = -EINVAL;
- }
- if (of_property_read_string(np, "clk-src", &pdata->clk_src_name))
- pdata->clk_pin_voting = false;
- else
- pdata->clk_pin_voting = true;
- //pdata->clkreq_gpio = of_get_named_gpio(np, "nq-clkreq", 0);
- return r;
- }
- static inline int gpio_input_init(const struct device * const dev,
- const int gpio, const char * const gpio_name)
- {
- int r = gpio_request(gpio, gpio_name);
- if (r) {
- dev_err(dev, "unable to request gpio [%d]\n", gpio);
- return r;
- }
- r = gpio_direction_input(gpio);
- if (r)
- dev_err(dev, "unable to set direction for gpio [%d]\n", gpio);
- return r;
- }
- static int nqx_probe(struct i2c_client *client,
- const struct i2c_device_id *id)
- {
- int r = 0;
- int irqn = 0;
- struct nqx_platform_data *platform_data;
- struct nqx_dev *nqx_dev;
- dev_err(&client->dev, "%s: enter\n", __func__);
- debug("Enter--");
- /**
- * 1. Get pinmux configuration from dts
- */
- if (client->dev.of_node) {
- platform_data = devm_kzalloc(&client->dev,
- sizeof(struct nqx_platform_data), GFP_KERNEL);
- if (!platform_data) {
- r = -ENOMEM;
- goto err_platform_data;
- }
- r = nfc_parse_dt(&client->dev, platform_data);
- if (r)
- goto err_free_data;
- } else
- platform_data = client->dev.platform_data;
- dev_err(&client->dev,
- "%s, inside nfc-nci flags = %x\n",
- __func__, client->flags);
- if (platform_data == NULL) {
- dev_err(&client->dev, "%s: failed\n", __func__);
- r = -ENODEV;
- goto err_platform_data;
- }
- if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
- dev_err(&client->dev, "%s: need I2C_FUNC_I2C\n", __func__);
- r = -ENODEV;
- goto err_free_data;
- }
- nqx_dev = kzalloc(sizeof(*nqx_dev), GFP_KERNEL);
- if (nqx_dev == NULL) {
- r = -ENOMEM;
- goto err_free_data;
- }
- nqx_dev->client = client;
- nqx_dev->kbuflen = MAX_BUFFER_SIZE;
- nqx_dev->kbuf = kzalloc(MAX_BUFFER_SIZE, GFP_KERNEL);
- if (!nqx_dev->kbuf) {
- dev_err(&client->dev,
- "failed to allocate memory for nqx_dev->kbuf\n");
- r = -ENOMEM;
- goto err_free_dev;
- }
- /**
- * 2. NFCC_VEN output 0
- */
- if (gpio_is_valid(platform_data->en_gpio)) {
- #ifndef __MTK_GPIO_DEVM_API
- r = gpio_request(platform_data->en_gpio, "nfc_reset_gpio");
- if (r) {
- dev_err(&client->dev,
- "%s: unable to request nfc reset gpio [%d]\n",
- __func__,
- platform_data->en_gpio);
- goto err_mem;
- }
- r = gpio_direction_output(platform_data->en_gpio, 0);
- #else
- r = devm_gpio_request_one(&client->dev, platform_data->en_gpio, GPIOF_OUT_INIT_LOW, "nfc_reset_gpio");
- #endif
- if (r) {
- dev_err(&client->dev,
- "%s: unable to set direction for nfc reset gpio [%d]\n",
- __func__,
- platform_data->en_gpio);
- goto err_en_gpio;
- }
- } else {
- dev_err(&client->dev,
- "%s: nfc reset gpio not provided\n", __func__);
- goto err_mem;
- }
- /**
- * 3. NFCC_IRQ input
- */
- if (gpio_is_valid(platform_data->irq_gpio)) {
- #ifndef __MTK_GPIO_DEVM_API
- r = gpio_request(platform_data->irq_gpio, "nfc_irq_gpio");
- if (r) {
- dev_err(&client->dev, "%s: unable to request nfc irq gpio [%d]\n",
- __func__, platform_data->irq_gpio);
- goto err_en_gpio;
- }
- r = gpio_direction_input(platform_data->irq_gpio);
- #else
- r = devm_gpio_request_one(&client->dev, platform_data->irq_gpio, GPIOF_IN, "nfc_irq_gpio");
- #endif
- if (r) {
- dev_err(&client->dev,
- "%s: unable to set direction for nfc irq gpio [%d]\n",
- __func__,
- platform_data->irq_gpio);
- goto err_irq_gpio;
- }
- irqn = gpio_to_irq(platform_data->irq_gpio);
- if (irqn < 0) {
- r = irqn;
- goto err_irq_gpio;
- }
- client->irq = irqn;
- } else {
- dev_err(&client->dev, "%s: irq gpio not provided\n", __func__);
- goto err_en_gpio;
- }
- /**
- * 4. NFCC_DNLD output 0
- */
- if (gpio_is_valid(platform_data->firm_gpio)) {
- #ifndef __MTK_GPIO_DEVM_API
- r = gpio_request(platform_data->firm_gpio,
- "nfc_firm_gpio");
- if (r) {
- dev_err(&client->dev,
- "%s: unable to request nfc firmware gpio [%d]\n",
- __func__, platform_data->firm_gpio);
- goto err_irq_gpio;
- }
- r = gpio_direction_output(platform_data->firm_gpio, 0);
- #else
- r = devm_gpio_request_one(&client->dev, platform_data->firm_gpio, GPIOF_OUT_INIT_LOW, "nfc_firm_gpio");
- #endif
- if (r) {
- dev_err(&client->dev,
- "%s: cannot set direction for nfc firmware gpio [%d]\n",
- __func__, platform_data->firm_gpio);
- goto err_firm_gpio;
- }
- } else {
- dev_err(&client->dev,
- "%s: firm gpio not provided\n", __func__);
- goto err_irq_gpio;
- }
- /**
- * 6. NFCC_CLKREQ input
- */
- /*
- if (gpio_is_valid(platform_data->clkreq_gpio)) {
- #ifndef __MTK_GPIO_DEVM_API
- r = gpio_request(platform_data->clkreq_gpio,
- "nfc_clkreq_gpio");
- if (r) {
- dev_err(&client->dev,
- "%s: unable to request nfc clkreq gpio [%d]\n",
- __func__, platform_data->clkreq_gpio);
- goto err_ese_gpio;
- }
- r = gpio_direction_input(platform_data->clkreq_gpio);
- #else
- r = devm_gpio_request_one(&client->dev, platform_data->clkreq_gpio, GPIOF_IN, "nfc_clkreq_gpio");
- #endif
- if (r) {
- dev_err(&client->dev,
- "%s: cannot set direction for nfc clkreq gpio [%d]\n",
- __func__, platform_data->clkreq_gpio);
- goto err_clkreq_gpio;
- }
- } else {
- dev_err(&client->dev,
- "%s: clkreq gpio not provided\n", __func__);
- goto err_ese_gpio;
- }
- */
- nqx_dev->en_gpio = platform_data->en_gpio;
- nqx_dev->irq_gpio = platform_data->irq_gpio;
- nqx_dev->firm_gpio = platform_data->firm_gpio;
- //nqx_dev->clkreq_gpio = platform_data->clkreq_gpio;
- nqx_dev->pdata = platform_data;
- /* init mutex and queues */
- init_waitqueue_head(&nqx_dev->read_wq);
- mutex_init(&nqx_dev->read_mutex);
- spin_lock_init(&nqx_dev->irq_enabled_lock);
- r = alloc_chrdev_region(&nqx_dev->devno, 0, DEV_COUNT, DEVICE_NAME);
- if (r < 0) {
- dev_err(&client->dev,
- "%s: failed to alloc chrdev region\n", __func__);
- goto err_char_dev_register;
- }
- nqx_dev->nqx_class = class_create(THIS_MODULE, CLASS_NAME);
- if (IS_ERR(nqx_dev->nqx_class)) {
- dev_err(&client->dev,
- "%s: failed to register device class\n", __func__);
- goto err_class_create;
- }
- cdev_init(&nqx_dev->c_dev, &nfc_dev_fops);
- r = cdev_add(&nqx_dev->c_dev, nqx_dev->devno, DEV_COUNT);
- if (r < 0) {
- dev_err(&client->dev, "%s: failed to add cdev\n", __func__);
- goto err_cdev_add;
- }
- nqx_dev->nqx_device = device_create(nqx_dev->nqx_class, NULL,
- nqx_dev->devno, nqx_dev, DEVICE_NAME);
- if (IS_ERR(nqx_dev->nqx_device)) {
- dev_err(&client->dev,
- "%s: failed to create the device\n", __func__);
- goto err_device_create;
- }
- /* NFC_INT IRQ */
- nqx_dev->irq_enabled = true;
- r = request_irq(client->irq, nqx_dev_irq_handler,
- IRQF_TRIGGER_HIGH, client->name, nqx_dev);
- if (r) {
- dev_err(&client->dev, "%s: request_irq failed\n", __func__);
- goto err_request_irq_failed;
- }
- nqx_disable_irq(nqx_dev);
- /* Do not perform nfcc_hw_check, make sure that nfcc is present */
- #if 0
- /*
- * To be efficient we need to test whether nfcc hardware is physically
- * present before attempting further hardware initialisation.
- *
- */
- r = nfcc_hw_check(client, nqx_dev);
- if (r) {
- /* make sure NFCC is not enabled */
- gpio_set_value(platform_data->firm_gpio, 0);
- /* hardware dependent delay */
- usleep_range(10000, 10100);
- gpio_set_value(platform_data->en_gpio, 0);
- /* hardware dependent delay */
- usleep_range(10000, 10100);
- /* We don't think there is hardware switch NFC OFF */
- goto err_request_hw_check_failed;
- }
- #endif
- /* Register reboot notifier here */
- r = register_reboot_notifier(&nfcc_notifier);
- if (r) {
- dev_err(&client->dev,
- "%s: cannot register reboot notifier(err = %d)\n",
- __func__, r);
- /*
- * nfcc_hw_check function not doing memory
- * allocation so using same goto target here
- */
- goto err_request_hw_check_failed;
- }
- #ifdef NFC_KERNEL_BU
- r = nqx_clock_select(nqx_dev);
- if (r < 0) {
- dev_err(&client->dev,
- "%s: nqx_clock_select failed\n", __func__);
- goto err_clock_en_failed;
- }
- gpio_set_value(platform_data->en_gpio, 1);
- #endif
- device_init_wakeup(&client->dev, true);
- device_set_wakeup_capable(&client->dev, true);
- i2c_set_clientdata(client, nqx_dev);
- nqx_dev->irq_wake_up = false;
- dev_err(&client->dev,
- "%s: probing NFCC NQxxx exited successfully\n",
- __func__);
- return 0;
- #ifdef NFC_KERNEL_BU
- err_clock_en_failed:
- unregister_reboot_notifier(&nfcc_notifier);
- #endif
- err_request_hw_check_failed:
- free_irq(client->irq, nqx_dev);
- err_request_irq_failed:
- device_destroy(nqx_dev->nqx_class, nqx_dev->devno);
- err_device_create:
- cdev_del(&nqx_dev->c_dev);
- err_cdev_add:
- class_destroy(nqx_dev->nqx_class);
- err_class_create:
- unregister_chrdev_region(nqx_dev->devno, DEV_COUNT);
- err_char_dev_register:
- mutex_destroy(&nqx_dev->read_mutex);
- //err_clkreq_gpio:
- // gpio_free(platform_data->clkreq_gpio);
- //err_ese_gpio:
- /* optional gpio, not sure was configured in probe */
- err_firm_gpio:
- gpio_free(platform_data->firm_gpio);
- err_irq_gpio:
- gpio_free(platform_data->irq_gpio);
- err_en_gpio:
- gpio_free(platform_data->en_gpio);
- err_mem:
- kfree(nqx_dev->kbuf);
- err_free_dev:
- kfree(nqx_dev);
- err_free_data:
- if (client->dev.of_node)
- devm_kfree(&client->dev, platform_data);
- err_platform_data:
- dev_err(&client->dev,
- "%s: probing nqxx failed, check hardware\n",
- __func__);
- return r;
- }
- static int nqx_remove(struct i2c_client *client)
- {
- int ret = 0;
- struct nqx_dev *nqx_dev;
- nqx_dev = i2c_get_clientdata(client);
- printk("------------------------------------------------htqhtqhtq------------------------------------------- %s:", __func__);
- if (!nqx_dev) {
- dev_err(&client->dev,
- "%s: device doesn't exist anymore\n", __func__);
- ret = -ENODEV;
- goto err;
- }
- unregister_reboot_notifier(&nfcc_notifier);
- free_irq(client->irq, nqx_dev);
- cdev_del(&nqx_dev->c_dev);
- device_destroy(nqx_dev->nqx_class, nqx_dev->devno);
- class_destroy(nqx_dev->nqx_class);
- unregister_chrdev_region(nqx_dev->devno, DEV_COUNT);
- mutex_destroy(&nqx_dev->read_mutex);
- // gpio_free(nqx_dev->clkreq_gpio);
- /* optional gpio, not sure was configured in probe */
- gpio_free(nqx_dev->firm_gpio);
- gpio_free(nqx_dev->irq_gpio);
- gpio_free(nqx_dev->en_gpio);
- kfree(nqx_dev->kbuf);
- if (client->dev.of_node)
- devm_kfree(&client->dev, nqx_dev->pdata);
- kfree(nqx_dev);
- err:
- return ret;
- }
- static int nqx_suspend(struct device *device)
- {
- struct i2c_client *client = to_i2c_client(device);
- struct nqx_dev *nqx_dev = i2c_get_clientdata(client);
- debug(" Entry");
- if (device_may_wakeup(&client->dev) && nqx_dev->irq_enabled) {
- if (!enable_irq_wake(client->irq))
- nqx_dev->irq_wake_up = true;
- }
- return 0;
- }
- static int nqx_resume(struct device *device)
- {
- struct i2c_client *client = to_i2c_client(device);
- struct nqx_dev *nqx_dev = i2c_get_clientdata(client);
- debug(" Entry");
- if (device_may_wakeup(&client->dev) && nqx_dev->irq_wake_up) {
- if (!disable_irq_wake(client->irq))
- nqx_dev->irq_wake_up = false;
- }
- return 0;
- }
- static const struct i2c_device_id nqx_id[] = {
- {"nqx-i2c", 0},
- {}
- };
- static const struct dev_pm_ops nfc_pm_ops = {
- SET_SYSTEM_SLEEP_PM_OPS(nqx_suspend, nqx_resume)
- };
- static const struct of_device_id nqx_match_table[] = {
- {.compatible = "nq-nci"},
- {}
- };
- MODULE_DEVICE_TABLE(i2c, nqx_match_table);
- static struct i2c_driver nqx = {
- .id_table = nqx_id,
- .probe = nqx_probe,
- .remove = nqx_remove,
- .driver = {
- .owner = THIS_MODULE,
- .name = "nq-nci",
- .of_match_table = nqx_match_table,
- .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- .pm = &nfc_pm_ops,
- },
- };
- static int nfcc_reboot(struct notifier_block *notifier, unsigned long val,
- void *v)
- {
- gpio_set_value(disable_ctrl, 1);
- printk("nfcc_reboot\n");
- return NOTIFY_OK;
- }
- /*
- * module load/unload record keeping
- */
- extern char *saved_command_line;
- static int __init nqx_dev_init(void)
- {
- printk(KERN_ERR "saved_command_line:%s", saved_command_line);
- if (!strstr(saved_command_line, "androidboot.product.hardware.sku=galahad")) {
- printk(KERN_ERR "not nfc phone!");
- return -1;
- }
- printk(KERN_ERR "nqx nfc init");
- return i2c_add_driver(&nqx);
- }
- module_init(nqx_dev_init);
- static void __exit nqx_dev_exit(void)
- {
- unregister_reboot_notifier(&nfcc_notifier);
- i2c_del_driver(&nqx);
- }
- module_exit(nqx_dev_exit);
- MODULE_DESCRIPTION("NFC nqx");
- MODULE_LICENSE("GPL v2");
|