pn553-19a.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  1. /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
  2. * Copyright (C) 2021 XiaoMi, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/fs.h>
  16. #include <linux/reboot.h>
  17. #include <linux/slab.h>
  18. #include <linux/irq.h>
  19. #include <linux/delay.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/gpio.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/of_device.h>
  25. #include <linux/uaccess.h>
  26. #include "pn553-19a.h"
  27. #include <linux/clk.h>
  28. #ifdef CONFIG_COMPAT
  29. #include <linux/compat.h>
  30. #endif
  31. /** [NFC_MODI] @{ */
  32. #define __MTK_GPIO_DEVM_API (1)
  33. /** [NFC_MODI] } */
  34. struct nqx_platform_data {
  35. unsigned int irq_gpio;
  36. unsigned int en_gpio;
  37. //unsigned int clkreq_gpio;
  38. unsigned int firm_gpio;
  39. //unsigned int ese_gpio;
  40. const char *clk_src_name;
  41. /* NFC_CLK pin voting state */
  42. bool clk_pin_voting;
  43. };
  44. #define DEV_COUNT 1
  45. #define DEVICE_NAME "nq-nci"
  46. #define CLASS_NAME "nq-nci"
  47. #define MAX_BUFFER_SIZE (320)
  48. #define WAKEUP_SRC_TIMEOUT (2000)
  49. #define MAX_RETRY_COUNT 3
  50. #define NCI_RESET_CMD_LEN 4
  51. #define NCI_INIT_CMD_LEN 3
  52. #define NCI_RESET_RSP_LEN 6
  53. #define NCI_INIT_RSP_LEN 28
  54. #define NCI_GET_VERSION_CMD_LEN 8
  55. #define NCI_GET_VERSION_RSP_LEN 12
  56. struct nqx_dev {
  57. wait_queue_head_t read_wq;
  58. struct mutex read_mutex;
  59. struct i2c_client *client;
  60. dev_t devno;
  61. struct class *nqx_class;
  62. struct device *nqx_device;
  63. struct cdev c_dev;
  64. union nqx_uinfo nqx_info;
  65. /* NFC GPIO variables */
  66. unsigned int irq_gpio;
  67. unsigned int en_gpio;
  68. unsigned int firm_gpio;
  69. //unsigned int clkreq_gpio;
  70. //unsigned int ese_gpio;
  71. /* NFC VEN pin state powered by Nfc */
  72. bool nfc_ven_enabled;
  73. /* NFC_IRQ state */
  74. bool irq_enabled;
  75. /* NFC_IRQ wake-up state */
  76. bool irq_wake_up;
  77. spinlock_t irq_enabled_lock;
  78. unsigned int count_irq;
  79. /* Initial CORE RESET notification */
  80. unsigned int core_reset_ntf;
  81. /* CLK control */
  82. bool clk_run;
  83. struct clk *s_clk;
  84. /* read buffer*/
  85. size_t kbuflen;
  86. u8 *kbuf;
  87. struct nqx_platform_data *pdata;
  88. };
  89. static int nfcc_reboot(struct notifier_block *notifier, unsigned long val,
  90. void *v);
  91. /*clock enable function*/
  92. static int nqx_clock_select(struct nqx_dev *nqx_dev);
  93. /*clock disable function*/
  94. static int nqx_clock_deselect(struct nqx_dev *nqx_dev);
  95. static struct notifier_block nfcc_notifier = {
  96. .notifier_call = nfcc_reboot,
  97. .next = NULL,
  98. .priority = 0
  99. };
  100. static unsigned int disable_ctrl;
  101. #define MAX_I2C_DUMP_SIZE 512
  102. /**
  103. * [NFC_MODI] @{
  104. * debug()
  105. *
  106. * Print debug log by using printk
  107. *
  108. */
  109. #define __LOGTAG__ "NFCC_SN100T"
  110. #define ROY_DEBUG (1)
  111. #ifdef ROY_DEBUG
  112. #ifdef __LOGTAG__
  113. #define debug(fmt, arg...) do {\
  114. printk("ROY %s::%s "fmt, __LOGTAG__, __func__, ##arg);\
  115. } while (0);
  116. #define logerr(fmt, arg...) do {\
  117. printk(KERN_ERR"ROY %s::%s "fmt, __LOGTAG__, __func__, ##arg);\
  118. } while (0);
  119. #else
  120. #define debug(fmt, arg...) printk("ROY [%s ]"fmt, __func__, ##arg)
  121. #define logerr(fmt, arg...) printk(KERN_ERR"ROY %s "fmt, __func__, ##arg);
  122. #endif
  123. #else
  124. #define debug(fmt, arg...)
  125. #define logerr(fmt, arg...) printk(KERN_ERR"ROY %s "fmt, __func__, ##arg);
  126. #endif
  127. /** [NFC_MODI] } */
  128. static void print_send_buffer(struct nqx_dev *nqx_dev, unsigned char *buf, int len)
  129. {
  130. unsigned char output[MAX_I2C_DUMP_SIZE * 2 + 1];
  131. int i;
  132. debug(" Entry\n");
  133. if (len > MAX_I2C_DUMP_SIZE)
  134. len = MAX_I2C_DUMP_SIZE - 1;
  135. for (i = 0; i < len; i++) {
  136. snprintf(output + i * 2, 3, "%02x ", buf[i]);
  137. }
  138. dev_warn(&nqx_dev->client->dev, "%3d > %s\n", len, output);
  139. }
  140. static void print_recv_buffer(struct nqx_dev *nqx_dev, unsigned char *buf, int len)
  141. {
  142. unsigned char output[MAX_I2C_DUMP_SIZE * 2 + 1];
  143. int i;
  144. debug(" Entry\n");
  145. if (len > MAX_I2C_DUMP_SIZE)
  146. len = MAX_I2C_DUMP_SIZE - 1;
  147. for (i = 0; i < len; i++) {
  148. snprintf(output + i * 2, 3, "%02x ", buf[i]);
  149. }
  150. dev_warn(&nqx_dev->client->dev, "%3d < %s\n", len, output);
  151. }
  152. static void nqx_init_stat(struct nqx_dev *nqx_dev)
  153. {
  154. debug(" Entry\n");
  155. nqx_dev->count_irq = 0;
  156. }
  157. /**
  158. * nqx_enable_irq()
  159. *
  160. * Check if interrupt is enabled or not
  161. * and enable interrupt
  162. *
  163. * Return: void
  164. */
  165. static void nqx_enable_irq(struct nqx_dev *nqx_dev)
  166. {
  167. unsigned long flags;
  168. debug(" Entry\n");
  169. spin_lock_irqsave(&nqx_dev->irq_enabled_lock, flags);
  170. //if (!nqx_dev->irq_enabled) {
  171. nqx_dev->irq_enabled = true;
  172. enable_irq(nqx_dev->client->irq);
  173. //}
  174. spin_unlock_irqrestore(&nqx_dev->irq_enabled_lock, flags);
  175. }
  176. /**
  177. * nqx_disable_irq()
  178. *
  179. * Check if interrupt is enabled or not
  180. * and disable interrupt
  181. *
  182. * Return: void
  183. */
  184. static void nqx_disable_irq(struct nqx_dev *nqx_dev)
  185. {
  186. unsigned long flags;
  187. debug(" Entry\n");
  188. spin_lock_irqsave(&nqx_dev->irq_enabled_lock, flags);
  189. if (nqx_dev->irq_enabled) {
  190. disable_irq_nosync(nqx_dev->client->irq);
  191. nqx_dev->irq_enabled = false;
  192. }
  193. spin_unlock_irqrestore(&nqx_dev->irq_enabled_lock, flags);
  194. }
  195. static irqreturn_t nqx_dev_irq_handler(int irq, void *dev_id)
  196. {
  197. struct nqx_dev *nqx_dev = dev_id;
  198. unsigned long flags;
  199. debug(" Entry\n");
  200. if (device_may_wakeup(&nqx_dev->client->dev))
  201. pm_wakeup_event(&nqx_dev->client->dev, WAKEUP_SRC_TIMEOUT);
  202. nqx_disable_irq(nqx_dev);
  203. spin_lock_irqsave(&nqx_dev->irq_enabled_lock, flags);
  204. nqx_dev->count_irq++;
  205. spin_unlock_irqrestore(&nqx_dev->irq_enabled_lock, flags);
  206. /* Wake up waiting readers */
  207. wake_up(&nqx_dev->read_wq);
  208. return IRQ_HANDLED;
  209. }
  210. static ssize_t nfc_read(struct file *filp, char __user *buf,
  211. size_t count, loff_t *offset)
  212. {
  213. struct nqx_dev *nqx_dev = filp->private_data;
  214. unsigned char *tmp = NULL;
  215. int ret;
  216. int irq_gpio_val = 0;
  217. if (!nqx_dev) {
  218. ret = -ENODEV;
  219. goto out;
  220. }
  221. if (count > nqx_dev->kbuflen)
  222. count = nqx_dev->kbuflen;
  223. debug(" Entry reading %zu bytes.\n", count);
  224. mutex_lock(&nqx_dev->read_mutex);
  225. irq_gpio_val = gpio_get_value(nqx_dev->irq_gpio);
  226. debug(" irq_gpio_val %d.\n", irq_gpio_val);
  227. if (irq_gpio_val == 0) {
  228. if (filp->f_flags & O_NONBLOCK) {
  229. dev_err(&nqx_dev->client->dev,
  230. ":f_falg has O_NONBLOCK. EAGAIN\n");
  231. ret = -EAGAIN;
  232. goto err;
  233. }
  234. while (1) {
  235. ret = 0;
  236. if (!nqx_dev->irq_enabled) {
  237. nqx_dev->irq_enabled = true;
  238. enable_irq(nqx_dev->client->irq);
  239. }
  240. if (!gpio_get_value(nqx_dev->irq_gpio)) {
  241. ret = wait_event_interruptible(nqx_dev->read_wq,
  242. !nqx_dev->irq_enabled);
  243. }
  244. if (ret)
  245. goto err;
  246. nqx_disable_irq(nqx_dev);
  247. if (gpio_get_value(nqx_dev->irq_gpio))
  248. break;
  249. dev_err_ratelimited(&nqx_dev->client->dev,
  250. "gpio is low, no need to read data\n");
  251. }
  252. }
  253. tmp = nqx_dev->kbuf;
  254. if (!tmp) {
  255. dev_err(&nqx_dev->client->dev,
  256. "%s: device doesn't exist anymore\n", __func__);
  257. ret = -ENODEV;
  258. goto err;
  259. }
  260. memset(tmp, 0x00, count);
  261. /* Read data */
  262. pr_err("%s : client address: %d \n", __func__, &nqx_dev->client->addr);
  263. ret = i2c_master_recv(nqx_dev->client, tmp, count);
  264. if (ret < 0) {
  265. dev_err(&nqx_dev->client->dev,
  266. "%s: i2c_master_recv returned %d\n", __func__, ret);
  267. goto err;
  268. }
  269. if (ret > count) {
  270. dev_err(&nqx_dev->client->dev,
  271. "%s: received too many bytes from i2c (%d)\n",
  272. __func__, ret);
  273. ret = -EIO;
  274. goto err;
  275. }
  276. #ifdef NFC_KERNEL_BU
  277. printk("htqhtqhtq %s : NfcNciRx %x %x %x\n",
  278. __func__, tmp[0], tmp[1], tmp[2]);
  279. #endif
  280. print_recv_buffer(nqx_dev, tmp, ret);
  281. if (copy_to_user(buf, tmp, ret)) {
  282. dev_warn(&nqx_dev->client->dev,
  283. "%s : failed to copy to user space\n", __func__);
  284. ret = -EFAULT;
  285. goto err;
  286. }
  287. mutex_unlock(&nqx_dev->read_mutex);
  288. return ret;
  289. err:
  290. mutex_unlock(&nqx_dev->read_mutex);
  291. out:
  292. return ret;
  293. }
  294. static ssize_t nfc_write(struct file *filp, const char __user *buf,
  295. size_t count, loff_t *offset)
  296. {
  297. struct nqx_dev *nqx_dev = filp->private_data;
  298. char *tmp = NULL;
  299. int ret = 0;
  300. debug(" Entry\n");
  301. if (!nqx_dev) {
  302. ret = -ENODEV;
  303. goto out;
  304. }
  305. if (count > nqx_dev->kbuflen) {
  306. dev_err(&nqx_dev->client->dev, "%s: out of memory\n",
  307. __func__);
  308. ret = -ENOMEM;
  309. goto out;
  310. }
  311. tmp = memdup_user(buf, count);
  312. if (IS_ERR(tmp)) {
  313. dev_err(&nqx_dev->client->dev, "%s: memdup_user failed\n",
  314. __func__);
  315. ret = PTR_ERR(tmp);
  316. goto out;
  317. }
  318. ret = i2c_master_send(nqx_dev->client, tmp, count);
  319. if (ret != count) {
  320. dev_err(&nqx_dev->client->dev,
  321. "%s: failed to write %d\n", __func__, ret);
  322. ret = -EIO;
  323. goto out_free;
  324. }
  325. #ifdef NFC_KERNEL_BU
  326. printk("htqhtqhtq %s : i2c-%d: NfcNciTx %x %x %x\n",
  327. __func__, iminor(file_inode(filp)),
  328. tmp[0], tmp[1], tmp[2]);
  329. #endif
  330. print_send_buffer(nqx_dev, tmp, count);
  331. usleep_range(1000, 1100);
  332. out_free:
  333. kfree(tmp);
  334. out:
  335. return ret;
  336. }
  337. /**
  338. * nqx_standby_write()
  339. * @buf: pointer to data buffer
  340. * @len: # of bytes need to transfer
  341. *
  342. * write data buffer over I2C and retry
  343. * if NFCC is in stand by mode
  344. *
  345. * Return: # of bytes written or -ve value in case of error
  346. */
  347. static int nqx_standby_write(struct nqx_dev *nqx_dev,
  348. const unsigned char *buf, size_t len)
  349. {
  350. int ret = -EINVAL;
  351. int retry_cnt;
  352. debug(" Entry\n");
  353. for (retry_cnt = 1; retry_cnt <= MAX_RETRY_COUNT; retry_cnt++) {
  354. pr_err("%s : client address: %d \n", __func__, &nqx_dev->client->addr);
  355. ret = i2c_master_send(nqx_dev->client, buf, len);
  356. if (ret < 0) {
  357. dev_err(&nqx_dev->client->dev,
  358. "%s: write failed, Maybe in Standby Mode - Retry(%d)\n",
  359. __func__, retry_cnt);
  360. usleep_range(1000, 1100);
  361. } else if (ret == len)
  362. break;
  363. }
  364. return ret;
  365. }
  366. /*
  367. * Power management of the eSE
  368. * NFC & eSE ON : NFC_EN high and eSE_pwr_req high.
  369. * NFC OFF & eSE ON : NFC_EN high and eSE_pwr_req high.
  370. * NFC OFF & eSE OFF : NFC_EN low and eSE_pwr_req low.
  371. */
  372. static int nqx_ese_pwr(struct nqx_dev *nqx_dev, unsigned long int arg)
  373. {
  374. int r = -1;
  375. const unsigned char svdd_off_cmd_warn[] = {0x2F, 0x31, 0x01, 0x01};
  376. const unsigned char svdd_off_cmd_done[] = {0x2F, 0x31, 0x01, 0x00};
  377. if (arg == 0) {
  378. /*
  379. * We want to power on the eSE and to do so we need the
  380. * eSE_pwr_req pin and the NFC_EN pin to be high
  381. */
  382. /**
  383. * Let's store the NFC_EN pin state
  384. * only if the eSE is not yet on
  385. */
  386. nqx_dev->nfc_ven_enabled = gpio_get_value(nqx_dev->en_gpio);
  387. if (!nqx_dev->nfc_ven_enabled) {
  388. gpio_set_value(nqx_dev->en_gpio, 1);
  389. /* hardware dependent delay */
  390. usleep_range(10000, 11000);
  391. printk("we going to set ven value 1");
  392. }
  393. gpio_set_value(nqx_dev->en_gpio, 1);
  394. usleep_range(10000, 11000);
  395. printk("p61 nxp the ven value is %d",
  396. gpio_get_value(nqx_dev->en_gpio));
  397. r = 0;
  398. } else if (arg == 1) {
  399. if (nqx_dev->nfc_ven_enabled &&
  400. ((nqx_dev->nqx_info.info.chip_type == NFCC_NQ_220) ||
  401. (nqx_dev->nqx_info.info.chip_type == NFCC_PN66T))) {
  402. /**
  403. * Let's inform the CLF we're
  404. * powering off the eSE
  405. */
  406. r = nqx_standby_write(nqx_dev, svdd_off_cmd_warn,
  407. sizeof(svdd_off_cmd_warn));
  408. if (r < 0) {
  409. dev_err(&nqx_dev->client->dev,
  410. "%s: write failed after max retry\n",
  411. __func__);
  412. return -ENXIO;
  413. }
  414. dev_err(&nqx_dev->client->dev,
  415. "htqhtqhtq %s: svdd_off_cmd_warn sent\n", __func__);
  416. /**
  417. * Time needed for the SVDD capacitor
  418. * to get discharged
  419. */
  420. usleep_range(8000, 8100);
  421. /* Let's inform the CLF the eSE is now off */
  422. r = nqx_standby_write(nqx_dev, svdd_off_cmd_done,
  423. sizeof(svdd_off_cmd_done));
  424. if (r < 0) {
  425. dev_err(&nqx_dev->client->dev,
  426. "%s: write failed after max retry\n",
  427. __func__);
  428. return -ENXIO;
  429. }
  430. dev_err(&nqx_dev->client->dev,
  431. "%s: svdd_off_cmd_done sent\n", __func__);
  432. } else {
  433. /**
  434. * In case the NFC is off,
  435. * there's no need to send the i2c commands
  436. */
  437. usleep_range(1000, 1100);
  438. }
  439. if (!nqx_dev->nfc_ven_enabled) {
  440. /* hardware dependent delay */
  441. usleep_range(1000, 1100);
  442. dev_err(&nqx_dev->client->dev, "disabling en_gpio\n");
  443. gpio_set_value(nqx_dev->en_gpio, 0);
  444. }
  445. }
  446. return r;
  447. }
  448. static int nfc_open(struct inode *inode, struct file *filp)
  449. {
  450. int ret = 0;
  451. struct nqx_dev *nqx_dev = container_of(inode->i_cdev,
  452. struct nqx_dev, c_dev);
  453. filp->private_data = nqx_dev;
  454. nqx_init_stat(nqx_dev);
  455. dev_err(&nqx_dev->client->dev,
  456. "%s: %d,%d\n", __func__, imajor(inode), iminor(inode));
  457. return ret;
  458. }
  459. /*
  460. * nfc_ioctl_power_states() - power control
  461. * @filp: pointer to the file descriptor
  462. * @arg: mode that we want to move to
  463. *
  464. * Device power control. Depending on the arg value, device moves to
  465. * different states
  466. * (arg = 0): NFC_ENABLE GPIO = 0, FW_DL GPIO = 0
  467. * (arg = 1): NFC_ENABLE GPIO = 1, FW_DL GPIO = 0
  468. * (arg = 2): FW_DL GPIO = 1
  469. *
  470. * Return: -ENOIOCTLCMD if arg is not supported, 0 in any other case
  471. */
  472. int nfc_ioctl_power_states(struct file *filp, unsigned long arg)
  473. {
  474. int r = 0;
  475. struct nqx_dev *nqx_dev = filp->private_data;
  476. debug(" Entry arg = %d.\n", arg);
  477. if (arg == 0) {
  478. /*
  479. * We are attempting a hardware reset so let us disable
  480. * interrupts to avoid spurious notifications to upper
  481. * layers.
  482. */
  483. printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  484. /**
  485. * NFCC_DNLD output LOW
  486. * NFCC_VEN output LOW
  487. */
  488. // nqx_disable_irq(nqx_dev);
  489. dev_err(&nqx_dev->client->dev,
  490. "gpio_set_value disable: %s: info: %p\n",
  491. __func__, nqx_dev);
  492. //if (gpio_is_valid(nqx_dev->firm_gpio)) {
  493. gpio_set_value(nqx_dev->firm_gpio, 0);
  494. usleep_range(10000, 10100);
  495. //}
  496. {
  497. dev_err(&nqx_dev->client->dev, "ese_gpio invalid, set en_gpio to low\n");
  498. gpio_set_value(nqx_dev->en_gpio, 0);
  499. usleep_range(10000, 10100);
  500. }
  501. if (nqx_dev->pdata->clk_pin_voting) {
  502. r = nqx_clock_deselect(nqx_dev);
  503. if (r < 0)
  504. dev_err(&nqx_dev->client->dev, "unable to disable clock\n");
  505. }
  506. nqx_dev->nfc_ven_enabled = false;
  507. } else if (arg == 1) {
  508. printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  509. /**
  510. * NFCC_DNLD output LOW
  511. * NFCC_VEN output HIGH
  512. */
  513. // nqx_enable_irq(nqx_dev);
  514. dev_err(&nqx_dev->client->dev,
  515. "gpio_set_value enable: %s: info: %p\n",
  516. __func__, nqx_dev);
  517. //if (gpio_is_valid(nqx_dev->firm_gpio)) {
  518. gpio_set_value(nqx_dev->firm_gpio, 0);
  519. usleep_range(10000, 10100);
  520. //}
  521. gpio_set_value(nqx_dev->en_gpio, 1);
  522. usleep_range(10000, 10100);
  523. if (nqx_dev->pdata->clk_pin_voting) {
  524. r = nqx_clock_select(nqx_dev);
  525. if (r < 0)
  526. dev_err(&nqx_dev->client->dev, "unable to enable clock\n");
  527. }
  528. nqx_dev->nfc_ven_enabled = true;
  529. printk("p61 nxp the ven value is %d",
  530. gpio_get_value(nqx_dev->en_gpio));
  531. } else if (arg == 2) {
  532. /*
  533. * We are switching to Dowload Mode, toggle the enable pin
  534. * in order to set the NFCC in the new mode
  535. */
  536. gpio_set_value(nqx_dev->en_gpio, 1);
  537. usleep_range(10000, 10100);
  538. if (gpio_is_valid(nqx_dev->firm_gpio)) {
  539. gpio_set_value(nqx_dev->firm_gpio, 1);
  540. usleep_range(10000, 10100);
  541. }
  542. gpio_set_value(nqx_dev->en_gpio, 0);
  543. usleep_range(10000, 10100);
  544. gpio_set_value(nqx_dev->en_gpio, 1);
  545. usleep_range(10000, 10100);
  546. } else if (arg == 4) {
  547. /*
  548. * Setting firmware download gpio to HIGH for SN100U
  549. * before FW download start
  550. */
  551. dev_err(&nqx_dev->client->dev, "SN100 fw gpio HIGH\n");
  552. if (gpio_is_valid(nqx_dev->firm_gpio)) {
  553. gpio_set_value(nqx_dev->firm_gpio, 1);
  554. usleep_range(10000, 10100);
  555. } else
  556. dev_err(&nqx_dev->client->dev,
  557. "firm_gpio is invalid\n");
  558. } else if (arg == 6) {
  559. /*
  560. * Setting firmware download gpio to LOW for SN100U
  561. * FW download finished
  562. */
  563. dev_err(&nqx_dev->client->dev, "SN100 fw gpio LOW\n");
  564. if (gpio_is_valid(nqx_dev->firm_gpio)) {
  565. gpio_set_value(nqx_dev->firm_gpio, 0);
  566. usleep_range(10000, 10100);
  567. } else {
  568. dev_err(&nqx_dev->client->dev,
  569. "firm_gpio is invalid\n");
  570. }
  571. } else {
  572. r = -ENOIOCTLCMD;
  573. }
  574. return r;
  575. }
  576. #ifdef CONFIG_COMPAT
  577. static long nfc_compat_ioctl(struct file *pfile, unsigned int cmd,
  578. unsigned long arg)
  579. {
  580. long r = 0;
  581. printk("------------------------------------------------htqhtqhtq------------------------------------------- %s:", __func__);
  582. arg = (compat_u64)arg;
  583. switch (cmd) {
  584. case NFC_SET_PWR:
  585. nfc_ioctl_power_states(pfile, arg);
  586. break;
  587. case ESE_SET_PWR:
  588. nqx_ese_pwr(pfile->private_data, arg);
  589. break;
  590. case ESE_GET_PWR:
  591. nqx_ese_pwr(pfile->private_data, 3);
  592. break;
  593. case SET_RX_BLOCK:
  594. break;
  595. case SET_EMULATOR_TEST_POINT:
  596. break;
  597. default:
  598. r = -ENOTTY;
  599. }
  600. return r;
  601. }
  602. #endif
  603. /*
  604. * nfc_ioctl_core_reset_ntf()
  605. * @filp: pointer to the file descriptor
  606. *
  607. * Allows callers to determine if a CORE_RESET_NTF has arrived
  608. *
  609. * Return: the value of variable core_reset_ntf
  610. */
  611. int nfc_ioctl_core_reset_ntf(struct file *filp)
  612. {
  613. struct nqx_dev *nqx_dev = filp->private_data;
  614. dev_err(&nqx_dev->client->dev, "%s: returning = %d\n", __func__,
  615. nqx_dev->core_reset_ntf);
  616. return nqx_dev->core_reset_ntf;
  617. }
  618. /*
  619. * Inside nfc_ioctl_nfcc_info
  620. *
  621. * @brief nfc_ioctl_nfcc_info
  622. *
  623. * Check the NQ Chipset and firmware version details
  624. */
  625. unsigned int nfc_ioctl_nfcc_info(struct file *filp, unsigned long arg)
  626. {
  627. unsigned int r = 0;
  628. struct nqx_dev *nqx_dev = filp->private_data;
  629. r = nqx_dev->nqx_info.i;
  630. dev_err(&nqx_dev->client->dev,
  631. "nqx nfc : %s r = %d\n", __func__, r);
  632. return r;
  633. }
  634. static long nfc_ioctl(struct file *pfile, unsigned int cmd,
  635. unsigned long arg)
  636. {
  637. int r = 0;
  638. debug(" Entry cmd[%d], arg[%d]:\n", cmd, arg);
  639. switch (cmd) {
  640. case NFC_SET_PWR:
  641. r = nfc_ioctl_power_states(pfile, arg);
  642. break;
  643. case ESE_SET_PWR:
  644. r = nqx_ese_pwr(pfile->private_data, arg);
  645. break;
  646. case ESE_GET_PWR:
  647. r = nqx_ese_pwr(pfile->private_data, 3);
  648. break;
  649. case SET_RX_BLOCK:
  650. break;
  651. case SET_EMULATOR_TEST_POINT:
  652. break;
  653. case NFCC_INITIAL_CORE_RESET_NTF:
  654. r = nfc_ioctl_core_reset_ntf(pfile);
  655. break;
  656. case NFCC_GET_INFO:
  657. r = nfc_ioctl_nfcc_info(pfile, arg);
  658. break;
  659. default:
  660. r = -ENOIOCTLCMD;
  661. }
  662. return r;
  663. }
  664. static const struct file_operations nfc_dev_fops = {
  665. .owner = THIS_MODULE,
  666. .llseek = no_llseek,
  667. .read = nfc_read,
  668. .write = nfc_write,
  669. .open = nfc_open,
  670. .unlocked_ioctl = nfc_ioctl,
  671. #ifdef CONFIG_COMPAT
  672. .compat_ioctl = nfc_compat_ioctl
  673. #endif
  674. };
  675. /**
  676. * Do not need check availability of NFCC.
  677. * This function will block NFCC to enter FW download mode.
  678. */
  679. #if 0
  680. /* Check for availability of NQ_ NFC controller hardware */
  681. static int nfcc_hw_check(struct i2c_client *client, struct nqx_dev *nqx_dev)
  682. {
  683. int ret = 0;
  684. int gpio_retry_count = 0;
  685. unsigned char init_rsp_len = 0;
  686. unsigned int enable_gpio = nqx_dev->en_gpio;
  687. char *nci_reset_cmd = NULL;
  688. char *nci_init_cmd = NULL;
  689. char *nci_init_rsp = NULL;
  690. char *nci_reset_rsp = NULL;
  691. char *nci_get_version_cmd = NULL;
  692. char *nci_get_version_rsp = NULL;
  693. debug(" Entry\n");
  694. nci_reset_cmd = kzalloc(NCI_RESET_CMD_LEN + 1, GFP_DMA | GFP_KERNEL);
  695. if (!nci_reset_cmd) {
  696. ret = -ENOMEM;
  697. goto done;
  698. }
  699. nci_reset_rsp = kzalloc(NCI_RESET_RSP_LEN + 1, GFP_DMA | GFP_KERNEL);
  700. if (!nci_reset_rsp) {
  701. ret = -ENOMEM;
  702. goto done;
  703. }
  704. nci_init_cmd = kzalloc(NCI_INIT_CMD_LEN + 1, GFP_DMA | GFP_KERNEL);
  705. if (!nci_init_cmd) {
  706. ret = -ENOMEM;
  707. goto done;
  708. }
  709. nci_init_rsp = kzalloc(NCI_INIT_RSP_LEN + 1, GFP_DMA | GFP_KERNEL);
  710. if (!nci_init_rsp) {
  711. ret = -ENOMEM;
  712. goto done;
  713. }
  714. nci_get_version_cmd = kzalloc(NCI_GET_VERSION_CMD_LEN + 1,
  715. GFP_DMA | GFP_KERNEL);
  716. if (!nci_get_version_cmd) {
  717. ret = -ENOMEM;
  718. goto done;
  719. }
  720. nci_get_version_rsp = kzalloc(NCI_GET_VERSION_RSP_LEN + 1,
  721. GFP_DMA | GFP_KERNEL);
  722. if (!nci_get_version_rsp) {
  723. ret = -ENOMEM;
  724. goto done;
  725. }
  726. reset_enable_gpio:
  727. /* making sure that the NFCC starts in a clean state. */
  728. gpio_set_value(enable_gpio, 0);/* ULPM: Disable */
  729. /* hardware dependent delay */
  730. usleep_range(10000, 10100);
  731. gpio_set_value(enable_gpio, 1);/* HPD : Enable*/
  732. /* hardware dependent delay */
  733. usleep_range(10000, 10100);
  734. nci_reset_cmd[0] = 0x20;
  735. nci_reset_cmd[1] = 0x00;
  736. nci_reset_cmd[2] = 0x01;
  737. nci_reset_cmd[3] = 0x00;
  738. debug(" 1. Send NCI CORE RESET CMD.\n");
  739. /* send NCI CORE RESET CMD with Keep Config parameters */
  740. ret = i2c_master_send(client, nci_reset_cmd, NCI_RESET_CMD_LEN);
  741. if (ret < 0) {
  742. dev_err(&client->dev,
  743. "%s: - i2c_master_send core reset Error\n", __func__);
  744. //if (gpio_is_valid(nqx_dev->firm_gpio)) {
  745. gpio_set_value(nqx_dev->firm_gpio, 1);
  746. usleep_range(10000, 10100);
  747. //}
  748. gpio_set_value(nqx_dev->en_gpio, 0);
  749. usleep_range(10000, 10100);
  750. gpio_set_value(nqx_dev->en_gpio, 1);
  751. usleep_range(10000, 10100);
  752. nci_get_version_cmd[0] = 0x00;
  753. nci_get_version_cmd[1] = 0x04;
  754. nci_get_version_cmd[2] = 0xF1;
  755. nci_get_version_cmd[3] = 0x00;
  756. nci_get_version_cmd[4] = 0x00;
  757. nci_get_version_cmd[5] = 0x00;
  758. nci_get_version_cmd[6] = 0x6E;
  759. nci_get_version_cmd[7] = 0xEF;
  760. debug(" 1-1. If Reset Failed, Send GET_VERSION CMD.\n");
  761. ret = i2c_master_send(client, nci_get_version_cmd,
  762. NCI_GET_VERSION_CMD_LEN);
  763. if (ret < 0) {
  764. dev_err(&client->dev,
  765. "%s: - i2c_master_send get version cmd Error\n",
  766. __func__);
  767. goto err_nfcc_hw_check;
  768. }
  769. /* hardware dependent delay */
  770. usleep_range(10000, 10100);
  771. ret = i2c_master_recv(client, nci_get_version_rsp,
  772. NCI_GET_VERSION_RSP_LEN);
  773. if (ret < 0) {
  774. dev_err(&client->dev,
  775. "%s: - i2c_master_recv get version rsp Error\n",
  776. __func__);
  777. goto err_nfcc_hw_check;
  778. } else {
  779. nqx_dev->nqx_info.info.chip_type =
  780. nci_get_version_rsp[3];
  781. nqx_dev->nqx_info.info.rom_version =
  782. nci_get_version_rsp[4];
  783. nqx_dev->nqx_info.info.fw_minor =
  784. nci_get_version_rsp[6];
  785. nqx_dev->nqx_info.info.fw_major =
  786. nci_get_version_rsp[7];
  787. dev_err(&nqx_dev->client->dev, "NQ fw version = %x.%x.%x.%x\n",
  788. nqx_dev->nqx_info.info.chip_type,
  789. nqx_dev->nqx_info.info.rom_version,
  790. nqx_dev->nqx_info.info.fw_major,
  791. nqx_dev->nqx_info.info.fw_minor);
  792. }
  793. goto err_nfcc_reset_failed;
  794. }
  795. /* hardware dependent delay */
  796. msleep(30);
  797. debug(" 1<-. Get NCI CORE RESET CMD RSP.\n");
  798. /* Read Response of RESET command */
  799. ret = i2c_master_recv(client, nci_reset_rsp, NCI_RESET_RSP_LEN);
  800. if (ret < 0) {
  801. dev_err(&client->dev,
  802. "%s: - i2c_master_recv Error\n", __func__);
  803. gpio_retry_count = gpio_retry_count + 1;
  804. if (gpio_retry_count < MAX_RETRY_COUNT)
  805. goto reset_enable_gpio;
  806. goto err_nfcc_hw_check;
  807. }
  808. nci_init_cmd[0] = 0x20;
  809. nci_init_cmd[1] = 0x01;
  810. nci_init_cmd[2] = 0x00;
  811. debug(" 2->. Send NCI CORE INIT CMD RSP.\n");
  812. ret = nqx_standby_write(nqx_dev, nci_init_cmd, NCI_INIT_CMD_LEN);
  813. if (ret < 0) {
  814. dev_err(&client->dev,
  815. "%s: - i2c_master_send failed for Core INIT\n", __func__);
  816. goto err_nfcc_core_init_fail;
  817. }
  818. /* hardware dependent delay */
  819. msleep(30);
  820. /* Read Response of INIT command */
  821. debug(" 2<-. Get NCI CORE INIT CMD RSP.\n");
  822. ret = i2c_master_recv(client, nci_init_rsp, NCI_INIT_RSP_LEN);
  823. if (ret < 0) {
  824. printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  825. dev_err(&client->dev,
  826. "%s: - i2c_master_recv Error\n", __func__);
  827. goto err_nfcc_core_init_fail;
  828. }
  829. printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  830. init_rsp_len = 2 + nci_init_rsp[2]; /*payload + len*/
  831. if (init_rsp_len > PAYLOAD_HEADER_LENGTH) {
  832. printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
  833. nqx_dev->nqx_info.info.chip_type =
  834. nci_init_rsp[init_rsp_len - 3];
  835. nqx_dev->nqx_info.info.rom_version =
  836. nci_init_rsp[init_rsp_len - 2];
  837. nqx_dev->nqx_info.info.fw_major =
  838. nci_init_rsp[init_rsp_len - 1];
  839. nqx_dev->nqx_info.info.fw_minor =
  840. nci_init_rsp[init_rsp_len];
  841. }
  842. dev_err(&client->dev,
  843. "%s: - nq - reset cmd answer : NfcNciRx %x %x %x\n",
  844. __func__, nci_reset_rsp[0],
  845. nci_reset_rsp[1], nci_reset_rsp[2]);
  846. err_nfcc_reset_failed:
  847. dev_err(&nqx_dev->client->dev, "NQ NFCC chip_type = %x\n",
  848. nqx_dev->nqx_info.info.chip_type);
  849. dev_err(&nqx_dev->client->dev, "NQ fw version = %x.%x.%x\n",
  850. nqx_dev->nqx_info.info.rom_version,
  851. nqx_dev->nqx_info.info.fw_major,
  852. nqx_dev->nqx_info.info.fw_minor);
  853. switch (nqx_dev->nqx_info.info.chip_type) {
  854. case NFCC_NQ_210:
  855. dev_err(&client->dev,
  856. "%s: ## NFCC == NQ210 ##\n", __func__);
  857. break;
  858. case NFCC_NQ_220:
  859. dev_err(&client->dev,
  860. "%s: ## NFCC == NQ220 ##\n", __func__);
  861. break;
  862. case NFCC_NQ_310:
  863. dev_err(&client->dev,
  864. "%s: ## NFCC == NQ310 ##\n", __func__);
  865. break;
  866. case NFCC_NQ_330:
  867. dev_err(&client->dev,
  868. "%s: ## NFCC == NQ330 ##\n", __func__);
  869. break;
  870. case NFCC_PN66T:
  871. dev_err(&client->dev,
  872. "%s: ## NFCC == PN66T ##\n", __func__);
  873. break;
  874. case NFCC_SN100T:
  875. debug(" ## NFCC == SN100T ##\n");
  876. break;
  877. default:
  878. dev_err(&client->dev,
  879. "%s: - NFCC HW not Supported\n", __func__);
  880. break;
  881. }
  882. /*Disable NFC by default to save power on boot*/
  883. gpio_set_value(enable_gpio, 0);/* ULPM: Disable */
  884. ret = 0;
  885. goto done;
  886. err_nfcc_core_init_fail:
  887. dev_err(&client->dev,
  888. "%s: - nq - reset cmd answer : NfcNciRx %x %x %x\n",
  889. __func__, nci_reset_rsp[0],
  890. nci_reset_rsp[1], nci_reset_rsp[2]);
  891. err_nfcc_hw_check:
  892. ret = -ENXIO;
  893. dev_err(&client->dev,
  894. "%s: - NFCC HW not available\n", __func__);
  895. done:
  896. kfree(nci_reset_rsp);
  897. kfree(nci_init_rsp);
  898. kfree(nci_init_cmd);
  899. kfree(nci_reset_cmd);
  900. kfree(nci_get_version_cmd);
  901. kfree(nci_get_version_rsp);
  902. debug(" ret=[%]\n", ret);
  903. return ret;
  904. }
  905. #endif
  906. /*
  907. * Routine to enable clock.
  908. * this routine can be extended to select from multiple
  909. * sources based on clk_src_name.
  910. */
  911. static int nqx_clock_select(struct nqx_dev *nqx_dev)
  912. {
  913. int r = 0;
  914. printk("------------------------------------------------htqhtqhtq------------------------------------------- %s:", __func__);
  915. nqx_dev->s_clk = clk_get(&nqx_dev->client->dev, "ref_clk");
  916. if (nqx_dev->s_clk == NULL)
  917. goto err_clk;
  918. if (nqx_dev->clk_run == false)
  919. r = clk_prepare_enable(nqx_dev->s_clk);
  920. if (r)
  921. goto err_clk;
  922. nqx_dev->clk_run = true;
  923. return r;
  924. err_clk:
  925. r = -1;
  926. return r;
  927. }
  928. /*
  929. * Routine to disable clocks
  930. */
  931. static int nqx_clock_deselect(struct nqx_dev *nqx_dev)
  932. {
  933. int r = -1;
  934. printk("------------------------------------------------htqhtqhtq------------------------------------------- %s:", __func__);
  935. if (nqx_dev->s_clk != NULL) {
  936. if (nqx_dev->clk_run == true) {
  937. clk_disable_unprepare(nqx_dev->s_clk);
  938. nqx_dev->clk_run = false;
  939. }
  940. return 0;
  941. }
  942. return r;
  943. }
  944. static int nfc_parse_dt(struct device *dev, struct nqx_platform_data *pdata)
  945. {
  946. int r = 0;
  947. struct device_node *np = dev->of_node;
  948. pdata->en_gpio = of_get_named_gpio(np, "nq-ven", 0);
  949. if ((!gpio_is_valid(pdata->en_gpio)))
  950. return -EINVAL;
  951. disable_ctrl = pdata->en_gpio;
  952. pdata->irq_gpio = of_get_named_gpio(np, "nq-irq", 0);
  953. if ((!gpio_is_valid(pdata->irq_gpio)))
  954. return -EINVAL;
  955. pdata->firm_gpio = of_get_named_gpio(np, "nq-firm", 0);
  956. if (!gpio_is_valid(pdata->firm_gpio)) {
  957. dev_warn(dev,
  958. "FIRM GPIO <OPTIONAL> error getting from OF node\n");
  959. pdata->firm_gpio = -EINVAL;
  960. }
  961. if (of_property_read_string(np, "clk-src", &pdata->clk_src_name))
  962. pdata->clk_pin_voting = false;
  963. else
  964. pdata->clk_pin_voting = true;
  965. //pdata->clkreq_gpio = of_get_named_gpio(np, "nq-clkreq", 0);
  966. return r;
  967. }
  968. static inline int gpio_input_init(const struct device * const dev,
  969. const int gpio, const char * const gpio_name)
  970. {
  971. int r = gpio_request(gpio, gpio_name);
  972. if (r) {
  973. dev_err(dev, "unable to request gpio [%d]\n", gpio);
  974. return r;
  975. }
  976. r = gpio_direction_input(gpio);
  977. if (r)
  978. dev_err(dev, "unable to set direction for gpio [%d]\n", gpio);
  979. return r;
  980. }
  981. static int nqx_probe(struct i2c_client *client,
  982. const struct i2c_device_id *id)
  983. {
  984. int r = 0;
  985. int irqn = 0;
  986. struct nqx_platform_data *platform_data;
  987. struct nqx_dev *nqx_dev;
  988. dev_err(&client->dev, "%s: enter\n", __func__);
  989. debug("Enter--");
  990. /**
  991. * 1. Get pinmux configuration from dts
  992. */
  993. if (client->dev.of_node) {
  994. platform_data = devm_kzalloc(&client->dev,
  995. sizeof(struct nqx_platform_data), GFP_KERNEL);
  996. if (!platform_data) {
  997. r = -ENOMEM;
  998. goto err_platform_data;
  999. }
  1000. r = nfc_parse_dt(&client->dev, platform_data);
  1001. if (r)
  1002. goto err_free_data;
  1003. } else
  1004. platform_data = client->dev.platform_data;
  1005. dev_err(&client->dev,
  1006. "%s, inside nfc-nci flags = %x\n",
  1007. __func__, client->flags);
  1008. if (platform_data == NULL) {
  1009. dev_err(&client->dev, "%s: failed\n", __func__);
  1010. r = -ENODEV;
  1011. goto err_platform_data;
  1012. }
  1013. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1014. dev_err(&client->dev, "%s: need I2C_FUNC_I2C\n", __func__);
  1015. r = -ENODEV;
  1016. goto err_free_data;
  1017. }
  1018. nqx_dev = kzalloc(sizeof(*nqx_dev), GFP_KERNEL);
  1019. if (nqx_dev == NULL) {
  1020. r = -ENOMEM;
  1021. goto err_free_data;
  1022. }
  1023. nqx_dev->client = client;
  1024. nqx_dev->kbuflen = MAX_BUFFER_SIZE;
  1025. nqx_dev->kbuf = kzalloc(MAX_BUFFER_SIZE, GFP_KERNEL);
  1026. if (!nqx_dev->kbuf) {
  1027. dev_err(&client->dev,
  1028. "failed to allocate memory for nqx_dev->kbuf\n");
  1029. r = -ENOMEM;
  1030. goto err_free_dev;
  1031. }
  1032. /**
  1033. * 2. NFCC_VEN output 0
  1034. */
  1035. if (gpio_is_valid(platform_data->en_gpio)) {
  1036. #ifndef __MTK_GPIO_DEVM_API
  1037. r = gpio_request(platform_data->en_gpio, "nfc_reset_gpio");
  1038. if (r) {
  1039. dev_err(&client->dev,
  1040. "%s: unable to request nfc reset gpio [%d]\n",
  1041. __func__,
  1042. platform_data->en_gpio);
  1043. goto err_mem;
  1044. }
  1045. r = gpio_direction_output(platform_data->en_gpio, 0);
  1046. #else
  1047. r = devm_gpio_request_one(&client->dev, platform_data->en_gpio, GPIOF_OUT_INIT_LOW, "nfc_reset_gpio");
  1048. #endif
  1049. if (r) {
  1050. dev_err(&client->dev,
  1051. "%s: unable to set direction for nfc reset gpio [%d]\n",
  1052. __func__,
  1053. platform_data->en_gpio);
  1054. goto err_en_gpio;
  1055. }
  1056. } else {
  1057. dev_err(&client->dev,
  1058. "%s: nfc reset gpio not provided\n", __func__);
  1059. goto err_mem;
  1060. }
  1061. /**
  1062. * 3. NFCC_IRQ input
  1063. */
  1064. if (gpio_is_valid(platform_data->irq_gpio)) {
  1065. #ifndef __MTK_GPIO_DEVM_API
  1066. r = gpio_request(platform_data->irq_gpio, "nfc_irq_gpio");
  1067. if (r) {
  1068. dev_err(&client->dev, "%s: unable to request nfc irq gpio [%d]\n",
  1069. __func__, platform_data->irq_gpio);
  1070. goto err_en_gpio;
  1071. }
  1072. r = gpio_direction_input(platform_data->irq_gpio);
  1073. #else
  1074. r = devm_gpio_request_one(&client->dev, platform_data->irq_gpio, GPIOF_IN, "nfc_irq_gpio");
  1075. #endif
  1076. if (r) {
  1077. dev_err(&client->dev,
  1078. "%s: unable to set direction for nfc irq gpio [%d]\n",
  1079. __func__,
  1080. platform_data->irq_gpio);
  1081. goto err_irq_gpio;
  1082. }
  1083. irqn = gpio_to_irq(platform_data->irq_gpio);
  1084. if (irqn < 0) {
  1085. r = irqn;
  1086. goto err_irq_gpio;
  1087. }
  1088. client->irq = irqn;
  1089. } else {
  1090. dev_err(&client->dev, "%s: irq gpio not provided\n", __func__);
  1091. goto err_en_gpio;
  1092. }
  1093. /**
  1094. * 4. NFCC_DNLD output 0
  1095. */
  1096. if (gpio_is_valid(platform_data->firm_gpio)) {
  1097. #ifndef __MTK_GPIO_DEVM_API
  1098. r = gpio_request(platform_data->firm_gpio,
  1099. "nfc_firm_gpio");
  1100. if (r) {
  1101. dev_err(&client->dev,
  1102. "%s: unable to request nfc firmware gpio [%d]\n",
  1103. __func__, platform_data->firm_gpio);
  1104. goto err_irq_gpio;
  1105. }
  1106. r = gpio_direction_output(platform_data->firm_gpio, 0);
  1107. #else
  1108. r = devm_gpio_request_one(&client->dev, platform_data->firm_gpio, GPIOF_OUT_INIT_LOW, "nfc_firm_gpio");
  1109. #endif
  1110. if (r) {
  1111. dev_err(&client->dev,
  1112. "%s: cannot set direction for nfc firmware gpio [%d]\n",
  1113. __func__, platform_data->firm_gpio);
  1114. goto err_firm_gpio;
  1115. }
  1116. } else {
  1117. dev_err(&client->dev,
  1118. "%s: firm gpio not provided\n", __func__);
  1119. goto err_irq_gpio;
  1120. }
  1121. /**
  1122. * 6. NFCC_CLKREQ input
  1123. */
  1124. /*
  1125. if (gpio_is_valid(platform_data->clkreq_gpio)) {
  1126. #ifndef __MTK_GPIO_DEVM_API
  1127. r = gpio_request(platform_data->clkreq_gpio,
  1128. "nfc_clkreq_gpio");
  1129. if (r) {
  1130. dev_err(&client->dev,
  1131. "%s: unable to request nfc clkreq gpio [%d]\n",
  1132. __func__, platform_data->clkreq_gpio);
  1133. goto err_ese_gpio;
  1134. }
  1135. r = gpio_direction_input(platform_data->clkreq_gpio);
  1136. #else
  1137. r = devm_gpio_request_one(&client->dev, platform_data->clkreq_gpio, GPIOF_IN, "nfc_clkreq_gpio");
  1138. #endif
  1139. if (r) {
  1140. dev_err(&client->dev,
  1141. "%s: cannot set direction for nfc clkreq gpio [%d]\n",
  1142. __func__, platform_data->clkreq_gpio);
  1143. goto err_clkreq_gpio;
  1144. }
  1145. } else {
  1146. dev_err(&client->dev,
  1147. "%s: clkreq gpio not provided\n", __func__);
  1148. goto err_ese_gpio;
  1149. }
  1150. */
  1151. nqx_dev->en_gpio = platform_data->en_gpio;
  1152. nqx_dev->irq_gpio = platform_data->irq_gpio;
  1153. nqx_dev->firm_gpio = platform_data->firm_gpio;
  1154. //nqx_dev->clkreq_gpio = platform_data->clkreq_gpio;
  1155. nqx_dev->pdata = platform_data;
  1156. /* init mutex and queues */
  1157. init_waitqueue_head(&nqx_dev->read_wq);
  1158. mutex_init(&nqx_dev->read_mutex);
  1159. spin_lock_init(&nqx_dev->irq_enabled_lock);
  1160. r = alloc_chrdev_region(&nqx_dev->devno, 0, DEV_COUNT, DEVICE_NAME);
  1161. if (r < 0) {
  1162. dev_err(&client->dev,
  1163. "%s: failed to alloc chrdev region\n", __func__);
  1164. goto err_char_dev_register;
  1165. }
  1166. nqx_dev->nqx_class = class_create(THIS_MODULE, CLASS_NAME);
  1167. if (IS_ERR(nqx_dev->nqx_class)) {
  1168. dev_err(&client->dev,
  1169. "%s: failed to register device class\n", __func__);
  1170. goto err_class_create;
  1171. }
  1172. cdev_init(&nqx_dev->c_dev, &nfc_dev_fops);
  1173. r = cdev_add(&nqx_dev->c_dev, nqx_dev->devno, DEV_COUNT);
  1174. if (r < 0) {
  1175. dev_err(&client->dev, "%s: failed to add cdev\n", __func__);
  1176. goto err_cdev_add;
  1177. }
  1178. nqx_dev->nqx_device = device_create(nqx_dev->nqx_class, NULL,
  1179. nqx_dev->devno, nqx_dev, DEVICE_NAME);
  1180. if (IS_ERR(nqx_dev->nqx_device)) {
  1181. dev_err(&client->dev,
  1182. "%s: failed to create the device\n", __func__);
  1183. goto err_device_create;
  1184. }
  1185. /* NFC_INT IRQ */
  1186. nqx_dev->irq_enabled = true;
  1187. r = request_irq(client->irq, nqx_dev_irq_handler,
  1188. IRQF_TRIGGER_HIGH, client->name, nqx_dev);
  1189. if (r) {
  1190. dev_err(&client->dev, "%s: request_irq failed\n", __func__);
  1191. goto err_request_irq_failed;
  1192. }
  1193. nqx_disable_irq(nqx_dev);
  1194. /* Do not perform nfcc_hw_check, make sure that nfcc is present */
  1195. #if 0
  1196. /*
  1197. * To be efficient we need to test whether nfcc hardware is physically
  1198. * present before attempting further hardware initialisation.
  1199. *
  1200. */
  1201. r = nfcc_hw_check(client, nqx_dev);
  1202. if (r) {
  1203. /* make sure NFCC is not enabled */
  1204. gpio_set_value(platform_data->firm_gpio, 0);
  1205. /* hardware dependent delay */
  1206. usleep_range(10000, 10100);
  1207. gpio_set_value(platform_data->en_gpio, 0);
  1208. /* hardware dependent delay */
  1209. usleep_range(10000, 10100);
  1210. /* We don't think there is hardware switch NFC OFF */
  1211. goto err_request_hw_check_failed;
  1212. }
  1213. #endif
  1214. /* Register reboot notifier here */
  1215. r = register_reboot_notifier(&nfcc_notifier);
  1216. if (r) {
  1217. dev_err(&client->dev,
  1218. "%s: cannot register reboot notifier(err = %d)\n",
  1219. __func__, r);
  1220. /*
  1221. * nfcc_hw_check function not doing memory
  1222. * allocation so using same goto target here
  1223. */
  1224. goto err_request_hw_check_failed;
  1225. }
  1226. #ifdef NFC_KERNEL_BU
  1227. r = nqx_clock_select(nqx_dev);
  1228. if (r < 0) {
  1229. dev_err(&client->dev,
  1230. "%s: nqx_clock_select failed\n", __func__);
  1231. goto err_clock_en_failed;
  1232. }
  1233. gpio_set_value(platform_data->en_gpio, 1);
  1234. #endif
  1235. device_init_wakeup(&client->dev, true);
  1236. device_set_wakeup_capable(&client->dev, true);
  1237. i2c_set_clientdata(client, nqx_dev);
  1238. nqx_dev->irq_wake_up = false;
  1239. dev_err(&client->dev,
  1240. "%s: probing NFCC NQxxx exited successfully\n",
  1241. __func__);
  1242. return 0;
  1243. #ifdef NFC_KERNEL_BU
  1244. err_clock_en_failed:
  1245. unregister_reboot_notifier(&nfcc_notifier);
  1246. #endif
  1247. err_request_hw_check_failed:
  1248. free_irq(client->irq, nqx_dev);
  1249. err_request_irq_failed:
  1250. device_destroy(nqx_dev->nqx_class, nqx_dev->devno);
  1251. err_device_create:
  1252. cdev_del(&nqx_dev->c_dev);
  1253. err_cdev_add:
  1254. class_destroy(nqx_dev->nqx_class);
  1255. err_class_create:
  1256. unregister_chrdev_region(nqx_dev->devno, DEV_COUNT);
  1257. err_char_dev_register:
  1258. mutex_destroy(&nqx_dev->read_mutex);
  1259. //err_clkreq_gpio:
  1260. // gpio_free(platform_data->clkreq_gpio);
  1261. //err_ese_gpio:
  1262. /* optional gpio, not sure was configured in probe */
  1263. err_firm_gpio:
  1264. gpio_free(platform_data->firm_gpio);
  1265. err_irq_gpio:
  1266. gpio_free(platform_data->irq_gpio);
  1267. err_en_gpio:
  1268. gpio_free(platform_data->en_gpio);
  1269. err_mem:
  1270. kfree(nqx_dev->kbuf);
  1271. err_free_dev:
  1272. kfree(nqx_dev);
  1273. err_free_data:
  1274. if (client->dev.of_node)
  1275. devm_kfree(&client->dev, platform_data);
  1276. err_platform_data:
  1277. dev_err(&client->dev,
  1278. "%s: probing nqxx failed, check hardware\n",
  1279. __func__);
  1280. return r;
  1281. }
  1282. static int nqx_remove(struct i2c_client *client)
  1283. {
  1284. int ret = 0;
  1285. struct nqx_dev *nqx_dev;
  1286. nqx_dev = i2c_get_clientdata(client);
  1287. printk("------------------------------------------------htqhtqhtq------------------------------------------- %s:", __func__);
  1288. if (!nqx_dev) {
  1289. dev_err(&client->dev,
  1290. "%s: device doesn't exist anymore\n", __func__);
  1291. ret = -ENODEV;
  1292. goto err;
  1293. }
  1294. unregister_reboot_notifier(&nfcc_notifier);
  1295. free_irq(client->irq, nqx_dev);
  1296. cdev_del(&nqx_dev->c_dev);
  1297. device_destroy(nqx_dev->nqx_class, nqx_dev->devno);
  1298. class_destroy(nqx_dev->nqx_class);
  1299. unregister_chrdev_region(nqx_dev->devno, DEV_COUNT);
  1300. mutex_destroy(&nqx_dev->read_mutex);
  1301. // gpio_free(nqx_dev->clkreq_gpio);
  1302. /* optional gpio, not sure was configured in probe */
  1303. gpio_free(nqx_dev->firm_gpio);
  1304. gpio_free(nqx_dev->irq_gpio);
  1305. gpio_free(nqx_dev->en_gpio);
  1306. kfree(nqx_dev->kbuf);
  1307. if (client->dev.of_node)
  1308. devm_kfree(&client->dev, nqx_dev->pdata);
  1309. kfree(nqx_dev);
  1310. err:
  1311. return ret;
  1312. }
  1313. static int nqx_suspend(struct device *device)
  1314. {
  1315. struct i2c_client *client = to_i2c_client(device);
  1316. struct nqx_dev *nqx_dev = i2c_get_clientdata(client);
  1317. debug(" Entry");
  1318. if (device_may_wakeup(&client->dev) && nqx_dev->irq_enabled) {
  1319. if (!enable_irq_wake(client->irq))
  1320. nqx_dev->irq_wake_up = true;
  1321. }
  1322. return 0;
  1323. }
  1324. static int nqx_resume(struct device *device)
  1325. {
  1326. struct i2c_client *client = to_i2c_client(device);
  1327. struct nqx_dev *nqx_dev = i2c_get_clientdata(client);
  1328. debug(" Entry");
  1329. if (device_may_wakeup(&client->dev) && nqx_dev->irq_wake_up) {
  1330. if (!disable_irq_wake(client->irq))
  1331. nqx_dev->irq_wake_up = false;
  1332. }
  1333. return 0;
  1334. }
  1335. static const struct i2c_device_id nqx_id[] = {
  1336. {"nqx-i2c", 0},
  1337. {}
  1338. };
  1339. static const struct dev_pm_ops nfc_pm_ops = {
  1340. SET_SYSTEM_SLEEP_PM_OPS(nqx_suspend, nqx_resume)
  1341. };
  1342. static const struct of_device_id nqx_match_table[] = {
  1343. {.compatible = "nq-nci"},
  1344. {}
  1345. };
  1346. MODULE_DEVICE_TABLE(i2c, nqx_match_table);
  1347. static struct i2c_driver nqx = {
  1348. .id_table = nqx_id,
  1349. .probe = nqx_probe,
  1350. .remove = nqx_remove,
  1351. .driver = {
  1352. .owner = THIS_MODULE,
  1353. .name = "nq-nci",
  1354. .of_match_table = nqx_match_table,
  1355. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1356. .pm = &nfc_pm_ops,
  1357. },
  1358. };
  1359. static int nfcc_reboot(struct notifier_block *notifier, unsigned long val,
  1360. void *v)
  1361. {
  1362. gpio_set_value(disable_ctrl, 1);
  1363. printk("nfcc_reboot\n");
  1364. return NOTIFY_OK;
  1365. }
  1366. /*
  1367. * module load/unload record keeping
  1368. */
  1369. extern char *saved_command_line;
  1370. static int __init nqx_dev_init(void)
  1371. {
  1372. printk(KERN_ERR "saved_command_line:%s", saved_command_line);
  1373. if (!strstr(saved_command_line, "androidboot.product.hardware.sku=galahad")) {
  1374. printk(KERN_ERR "not nfc phone!");
  1375. return -1;
  1376. }
  1377. printk(KERN_ERR "nqx nfc init");
  1378. return i2c_add_driver(&nqx);
  1379. }
  1380. module_init(nqx_dev_init);
  1381. static void __exit nqx_dev_exit(void)
  1382. {
  1383. unregister_reboot_notifier(&nfcc_notifier);
  1384. i2c_del_driver(&nqx);
  1385. }
  1386. module_exit(nqx_dev_exit);
  1387. MODULE_DESCRIPTION("NFC nqx");
  1388. MODULE_LICENSE("GPL v2");