nq-nci.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/fs.h>
  15. #include <linux/reboot.h>
  16. #include <linux/slab.h>
  17. #include <linux/irq.h>
  18. #include <linux/delay.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/gpio.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/of_device.h>
  24. #include <linux/uaccess.h>
  25. #include "nq-nci.h"
  26. #include <linux/clk.h>
  27. #ifdef CONFIG_COMPAT
  28. #include <linux/compat.h>
  29. #endif
  30. struct nqx_platform_data {
  31. unsigned int irq_gpio;
  32. unsigned int en_gpio;
  33. unsigned int clkreq_gpio;
  34. unsigned int firm_gpio;
  35. unsigned int ese_gpio;
  36. int i2c_postinit_cmd_len;
  37. u8 *i2c_postinit_cmd;
  38. const char *clk_src_name;
  39. /* NFC_CLK pin voting state */
  40. bool clk_pin_voting;
  41. };
  42. static const struct of_device_id msm_match_table[] = {
  43. {.compatible = "qcom,nq-nci"},
  44. {}
  45. };
  46. MODULE_DEVICE_TABLE(of, msm_match_table);
  47. #define MAX_BUFFER_SIZE (320)
  48. #define WAKEUP_SRC_TIMEOUT (2000)
  49. #define MAX_RETRY_COUNT 3
  50. struct nqx_dev {
  51. wait_queue_head_t read_wq;
  52. struct mutex read_mutex;
  53. struct mutex ese_status_mutex;
  54. struct i2c_client *client;
  55. struct miscdevice nqx_device;
  56. union nqx_uinfo nqx_info;
  57. /* NFC GPIO variables */
  58. unsigned int irq_gpio;
  59. unsigned int en_gpio;
  60. unsigned int firm_gpio;
  61. unsigned int clkreq_gpio;
  62. unsigned int ese_gpio;
  63. /* ese usage and gpio information */
  64. unsigned int ese_gpio_stored;
  65. bool ese_in_use; /* protected by ese_status_mutex */
  66. /* NFC VEN pin state powered by Nfc */
  67. bool nfc_ven_enabled;
  68. /* NFC_IRQ state */
  69. bool irq_enabled;
  70. /* NFC_IRQ wake-up state */
  71. bool irq_wake_up;
  72. spinlock_t irq_enabled_lock;
  73. unsigned int count_irq;
  74. /* Initial CORE RESET notification */
  75. unsigned int core_reset_ntf;
  76. /* CLK control */
  77. bool clk_run;
  78. struct clk *s_clk;
  79. /* read buffer*/
  80. size_t kbuflen;
  81. u8 *kbuf;
  82. struct nqx_platform_data *pdata;
  83. };
  84. static int nfcc_reboot(struct notifier_block *notifier, unsigned long val,
  85. void *v);
  86. /*clock enable function*/
  87. static int nqx_clock_select(struct nqx_dev *nqx_dev);
  88. /*clock disable function*/
  89. static int nqx_clock_deselect(struct nqx_dev *nqx_dev);
  90. static struct notifier_block nfcc_notifier = {
  91. .notifier_call = nfcc_reboot,
  92. .next = NULL,
  93. .priority = 0
  94. };
  95. unsigned int disable_ctrl;
  96. static void nqx_init_stat(struct nqx_dev *nqx_dev)
  97. {
  98. nqx_dev->count_irq = 0;
  99. nqx_dev->ese_gpio_stored = 0;
  100. }
  101. static void nqx_disable_irq(struct nqx_dev *nqx_dev)
  102. {
  103. unsigned long flags;
  104. spin_lock_irqsave(&nqx_dev->irq_enabled_lock, flags);
  105. if (nqx_dev->irq_enabled) {
  106. disable_irq_nosync(nqx_dev->client->irq);
  107. nqx_dev->irq_enabled = false;
  108. }
  109. spin_unlock_irqrestore(&nqx_dev->irq_enabled_lock, flags);
  110. }
  111. /**
  112. * nqx_enable_irq()
  113. *
  114. * Check if interrupt is enabled or not
  115. * and enable interrupt
  116. *
  117. * Return: void
  118. */
  119. static void nqx_enable_irq(struct nqx_dev *nqx_dev)
  120. {
  121. unsigned long flags;
  122. spin_lock_irqsave(&nqx_dev->irq_enabled_lock, flags);
  123. if (!nqx_dev->irq_enabled) {
  124. nqx_dev->irq_enabled = true;
  125. enable_irq(nqx_dev->client->irq);
  126. }
  127. spin_unlock_irqrestore(&nqx_dev->irq_enabled_lock, flags);
  128. }
  129. static irqreturn_t nqx_dev_irq_handler(int irq, void *dev_id)
  130. {
  131. struct nqx_dev *nqx_dev = dev_id;
  132. unsigned long flags;
  133. if (device_may_wakeup(&nqx_dev->client->dev))
  134. pm_wakeup_event(&nqx_dev->client->dev, WAKEUP_SRC_TIMEOUT);
  135. nqx_disable_irq(nqx_dev);
  136. spin_lock_irqsave(&nqx_dev->irq_enabled_lock, flags);
  137. nqx_dev->count_irq++;
  138. spin_unlock_irqrestore(&nqx_dev->irq_enabled_lock, flags);
  139. wake_up(&nqx_dev->read_wq);
  140. return IRQ_HANDLED;
  141. }
  142. static ssize_t nfc_read(struct file *filp, char __user *buf,
  143. size_t count, loff_t *offset)
  144. {
  145. struct nqx_dev *nqx_dev = filp->private_data;
  146. unsigned char *tmp = NULL;
  147. int ret;
  148. int irq_gpio_val = 0;
  149. if (!nqx_dev) {
  150. ret = -ENODEV;
  151. goto out;
  152. }
  153. if (count > nqx_dev->kbuflen)
  154. count = nqx_dev->kbuflen;
  155. dev_dbg(&nqx_dev->client->dev, "%s : reading %zu bytes.\n",
  156. __func__, count);
  157. mutex_lock(&nqx_dev->read_mutex);
  158. irq_gpio_val = gpio_get_value(nqx_dev->irq_gpio);
  159. if (irq_gpio_val == 0) {
  160. if (filp->f_flags & O_NONBLOCK) {
  161. dev_err(&nqx_dev->client->dev,
  162. ":f_falg has O_NONBLOCK. EAGAIN\n");
  163. ret = -EAGAIN;
  164. goto err;
  165. }
  166. while (1) {
  167. ret = 0;
  168. if (!nqx_dev->irq_enabled) {
  169. nqx_dev->irq_enabled = true;
  170. enable_irq(nqx_dev->client->irq);
  171. }
  172. if (!gpio_get_value(nqx_dev->irq_gpio)) {
  173. ret = wait_event_interruptible(nqx_dev->read_wq,
  174. !nqx_dev->irq_enabled);
  175. }
  176. if (ret)
  177. goto err;
  178. nqx_disable_irq(nqx_dev);
  179. if (gpio_get_value(nqx_dev->irq_gpio))
  180. break;
  181. dev_err_ratelimited(&nqx_dev->client->dev,
  182. "gpio is low, no need to read data\n");
  183. }
  184. }
  185. tmp = nqx_dev->kbuf;
  186. if (!tmp) {
  187. dev_err(&nqx_dev->client->dev,
  188. "%s: device doesn't exist anymore\n", __func__);
  189. ret = -ENODEV;
  190. goto err;
  191. }
  192. memset(tmp, 0x00, count);
  193. /* Read data */
  194. ret = i2c_master_recv(nqx_dev->client, tmp, count);
  195. if (ret < 0) {
  196. dev_err(&nqx_dev->client->dev,
  197. "%s: i2c_master_recv returned %d\n", __func__, ret);
  198. goto err;
  199. }
  200. if (ret > count) {
  201. dev_err(&nqx_dev->client->dev,
  202. "%s: received too many bytes from i2c (%d)\n",
  203. __func__, ret);
  204. ret = -EIO;
  205. goto err;
  206. }
  207. #ifdef NFC_KERNEL_BU
  208. dev_dbg(&nqx_dev->client->dev, "%s : NfcNciRx %x %x %x\n",
  209. __func__, tmp[0], tmp[1], tmp[2]);
  210. #endif
  211. if (copy_to_user(buf, tmp, ret)) {
  212. dev_warn(&nqx_dev->client->dev,
  213. "%s : failed to copy to user space\n", __func__);
  214. ret = -EFAULT;
  215. goto err;
  216. }
  217. mutex_unlock(&nqx_dev->read_mutex);
  218. return ret;
  219. err:
  220. mutex_unlock(&nqx_dev->read_mutex);
  221. out:
  222. return ret;
  223. }
  224. static ssize_t nfc_write(struct file *filp, const char __user *buf,
  225. size_t count, loff_t *offset)
  226. {
  227. struct nqx_dev *nqx_dev = filp->private_data;
  228. char *tmp = NULL;
  229. int ret = 0;
  230. if (!nqx_dev) {
  231. ret = -ENODEV;
  232. goto out;
  233. }
  234. if (count > nqx_dev->kbuflen) {
  235. dev_err(&nqx_dev->client->dev, "%s: out of memory\n",
  236. __func__);
  237. ret = -ENOMEM;
  238. goto out;
  239. }
  240. tmp = memdup_user(buf, count);
  241. if (IS_ERR(tmp)) {
  242. dev_err(&nqx_dev->client->dev, "%s: memdup_user failed\n",
  243. __func__);
  244. ret = PTR_ERR(tmp);
  245. goto out;
  246. }
  247. ret = i2c_master_send(nqx_dev->client, tmp, count);
  248. if (ret != count) {
  249. dev_dbg(&nqx_dev->client->dev,
  250. "%s: failed to write %d\n", __func__, ret);
  251. ret = -EIO;
  252. goto out_free;
  253. }
  254. #ifdef NFC_KERNEL_BU
  255. dev_dbg(&nqx_dev->client->dev,
  256. "%s : i2c-%d: NfcNciTx %x %x %x\n",
  257. __func__, iminor(file_inode(filp)),
  258. tmp[0], tmp[1], tmp[2]);
  259. #endif
  260. usleep_range(1000, 1100);
  261. out_free:
  262. kfree(tmp);
  263. out:
  264. return ret;
  265. }
  266. /* Callback to indicate if secure element is in use.
  267. * @claim: true/false indicating claim/release
  268. *
  269. * returns 0 in case request was successful, otherwise -1 if ese was forced low
  270. * due to firmware download mode.
  271. */
  272. int nqx_claim_ese(struct nqx_dev *nqx_dev, bool claim)
  273. {
  274. mutex_lock(&nqx_dev->ese_status_mutex);
  275. if (claim) {
  276. /* currently in download mode,
  277. * we cannot start an ese transactionx
  278. */
  279. if (gpio_is_valid(nqx_dev->firm_gpio) &&
  280. gpio_get_value(nqx_dev->firm_gpio)) {
  281. dev_err(&nqx_dev->client->dev, "%s: claim ese failed\n",
  282. __func__);
  283. mutex_unlock(&nqx_dev->ese_status_mutex);
  284. return -EBUSY;
  285. }
  286. dev_dbg(&nqx_dev->client->dev, "%s: ese claimed\n", __func__);
  287. nqx_dev->ese_in_use = true;
  288. } else {
  289. dev_dbg(&nqx_dev->client->dev, "%s: ese released\n", __func__);
  290. nqx_dev->ese_in_use = false;
  291. }
  292. mutex_unlock(&nqx_dev->ese_status_mutex);
  293. return 0;
  294. }
  295. /*
  296. * Power management of the eSE
  297. * NFC & eSE ON : NFC_EN high and eSE_pwr_req high.
  298. * NFC OFF & eSE ON : NFC_EN high and eSE_pwr_req high.
  299. * NFC OFF & eSE OFF : NFC_EN low and eSE_pwr_req low.
  300. */
  301. int nqx_ese_pwr(struct nqx_dev *nqx_dev, unsigned long int arg)
  302. {
  303. int r = -1;
  304. dev_dbg(&nqx_dev->client->dev, "%s: %lu\n", __func__, arg);
  305. if (!gpio_is_valid(nqx_dev->ese_gpio)) {
  306. dev_err(&nqx_dev->client->dev,
  307. "%s: ese_gpio is not valid\n", __func__);
  308. return -EINVAL;
  309. }
  310. if (arg == 0) {
  311. /*
  312. * We want to power on the eSE and to do so we need the
  313. * eSE_pwr_req pin and the NFC_EN pin to be high
  314. */
  315. if (gpio_get_value(nqx_dev->ese_gpio)) {
  316. dev_dbg(&nqx_dev->client->dev, "ese_gpio is already high\n");
  317. r = 0;
  318. } else {
  319. /**
  320. * Let's store the NFC_EN pin state
  321. * only if the eSE is not yet on
  322. */
  323. nqx_dev->nfc_ven_enabled =
  324. gpio_get_value(nqx_dev->en_gpio);
  325. if (!nqx_dev->nfc_ven_enabled) {
  326. dev_dbg(&nqx_dev->client->dev, "setting NFC VEN high\n");
  327. gpio_set_value(nqx_dev->en_gpio, 1);
  328. /* hardware dependent delay */
  329. usleep_range(1000, 1100);
  330. if (gpio_get_value(nqx_dev->en_gpio) == 0)
  331. dev_dbg(&nqx_dev->client->dev, "NFC VEN still low\n");
  332. }
  333. dev_err(&nqx_dev->client->dev, "setting ese_gpio high\n");
  334. gpio_set_value(nqx_dev->ese_gpio, 1);
  335. /* hardware dependent delay */
  336. usleep_range(5000, 5100);
  337. if (gpio_get_value(nqx_dev->ese_gpio)) {
  338. dev_dbg(&nqx_dev->client->dev, "ese_gpio is enabled\n");
  339. r = 0;
  340. }
  341. }
  342. } else if (arg == 1) {
  343. r = 0;
  344. /**
  345. * In case the NFC is off,
  346. * there's no need to send the i2c commands
  347. */
  348. gpio_set_value(nqx_dev->ese_gpio, 0);
  349. dev_dbg(&nqx_dev->client->dev, "ese_gpio is disabled\n");
  350. if (!gpio_get_value(nqx_dev->ese_gpio)) {
  351. dev_dbg(&nqx_dev->client->dev, "ese_gpio is disabled\n");
  352. r = 0;
  353. }
  354. if (!nqx_dev->nfc_ven_enabled) {
  355. /* hardware dependent delay */
  356. usleep_range(1000, 1100);
  357. dev_dbg(&nqx_dev->client->dev, "disabling en_gpio\n");
  358. gpio_set_value(nqx_dev->en_gpio, 0);
  359. }
  360. } else if (arg == 3) {
  361. r = gpio_get_value(nqx_dev->ese_gpio);
  362. }
  363. return r;
  364. }
  365. static int nfc_open(struct inode *inode, struct file *filp)
  366. {
  367. int ret = 0;
  368. struct nqx_dev *nqx_dev = container_of(filp->private_data,
  369. struct nqx_dev, nqx_device);
  370. filp->private_data = nqx_dev;
  371. nqx_init_stat(nqx_dev);
  372. dev_dbg(&nqx_dev->client->dev,
  373. "%s: %d,%d\n", __func__, imajor(inode), iminor(inode));
  374. return ret;
  375. }
  376. /*
  377. * nfc_ioctl_power_states() - power control
  378. * @filp: pointer to the file descriptor
  379. * @arg: mode that we want to move to
  380. *
  381. * Device power control. Depending on the arg value, device moves to
  382. * different states
  383. * (arg = 0): NFC_ENABLE GPIO = 0, FW_DL GPIO = 0
  384. * (arg = 1): NFC_ENABLE GPIO = 1, FW_DL GPIO = 0
  385. * (arg = 2): FW_DL GPIO = 1
  386. *
  387. * Return: -ENOIOCTLCMD if arg is not supported, 0 in any other case
  388. */
  389. int nfc_ioctl_power_states(struct file *filp, unsigned long arg)
  390. {
  391. int r = 0;
  392. struct nqx_dev *nqx_dev = filp->private_data;
  393. dev_dbg(&nqx_dev->client->dev, "%s: %lu\n", __func__, arg);
  394. if (arg == 0) {
  395. /*
  396. * We are attempting a hardware reset so let us disable
  397. * interrupts to avoid spurious notifications to upper
  398. * layers.
  399. */
  400. nqx_disable_irq(nqx_dev);
  401. dev_dbg(&nqx_dev->client->dev,
  402. "gpio_set_value disable: %s: info: %p\n",
  403. __func__, nqx_dev);
  404. mutex_lock(&nqx_dev->ese_status_mutex);
  405. if (gpio_is_valid(nqx_dev->firm_gpio) &&
  406. gpio_get_value(nqx_dev->firm_gpio)) {
  407. if (gpio_is_valid(nqx_dev->ese_gpio) &&
  408. gpio_get_value(nqx_dev->ese_gpio) !=
  409. nqx_dev->ese_gpio_stored) {
  410. dev_dbg(&nqx_dev->client->dev,
  411. "%s: Restoring ese state %d\n",
  412. __func__,
  413. nqx_dev->ese_gpio_stored);
  414. gpio_set_value(nqx_dev->ese_gpio,
  415. nqx_dev->ese_gpio_stored);
  416. }
  417. gpio_set_value(nqx_dev->firm_gpio, 0);
  418. usleep_range(10000, 10100);
  419. }
  420. mutex_unlock(&nqx_dev->ese_status_mutex);
  421. if (gpio_is_valid(nqx_dev->ese_gpio)) {
  422. if (!gpio_get_value(nqx_dev->ese_gpio)) {
  423. dev_dbg(&nqx_dev->client->dev, "disabling en_gpio\n");
  424. gpio_set_value(nqx_dev->en_gpio, 0);
  425. usleep_range(10000, 10100);
  426. } else {
  427. dev_dbg(&nqx_dev->client->dev, "keeping en_gpio high\n");
  428. }
  429. } else {
  430. dev_dbg(&nqx_dev->client->dev, "ese_gpio invalid, set en_gpio to low\n");
  431. gpio_set_value(nqx_dev->en_gpio, 0);
  432. usleep_range(10000, 10100);
  433. }
  434. if (nqx_dev->pdata->clk_pin_voting) {
  435. r = nqx_clock_deselect(nqx_dev);
  436. if (r < 0)
  437. dev_err(&nqx_dev->client->dev, "unable to disable clock\n");
  438. }
  439. if (gpio_is_valid(nqx_dev->clkreq_gpio)) {
  440. r = nqx_clock_deselect(nqx_dev);
  441. if (r < 0)
  442. dev_err(&nqx_dev->client->dev, "unable to disable clock\n");
  443. }
  444. nqx_dev->nfc_ven_enabled = false;
  445. } else if (arg == 1) {
  446. nqx_enable_irq(nqx_dev);
  447. dev_dbg(&nqx_dev->client->dev,
  448. "gpio_set_value enable: %s: info: %p\n",
  449. __func__, nqx_dev);
  450. if (gpio_is_valid(nqx_dev->firm_gpio)) {
  451. gpio_set_value(nqx_dev->firm_gpio, 0);
  452. usleep_range(10000, 10100);
  453. }
  454. gpio_set_value(nqx_dev->en_gpio, 1);
  455. usleep_range(10000, 10100);
  456. if (nqx_dev->pdata->clk_pin_voting) {
  457. r = nqx_clock_select(nqx_dev);
  458. if (r < 0)
  459. dev_err(&nqx_dev->client->dev, "unable to enable clock\n");
  460. }
  461. nqx_dev->nfc_ven_enabled = true;
  462. } else if (arg == 2) {
  463. /*
  464. * We are switching to Dowload Mode, toggle the enable pin
  465. * in order to set the NFCC in the new mode
  466. */
  467. mutex_lock(&nqx_dev->ese_status_mutex);
  468. if (gpio_is_valid(nqx_dev->ese_gpio)) {
  469. if (nqx_dev->ese_in_use) {
  470. dev_err(&nqx_dev->client->dev,
  471. "FW download forbidden while ese is in use\n");
  472. mutex_unlock(&nqx_dev->ese_status_mutex);
  473. return -EBUSY; /* Device or resource busy */
  474. }
  475. nqx_dev->ese_gpio_stored =
  476. gpio_get_value(nqx_dev->ese_gpio);
  477. dev_err(&nqx_dev->client->dev,
  478. "FW download backing up se state: %d\n",
  479. nqx_dev->ese_gpio_stored);
  480. if (nqx_dev->ese_gpio_stored == 1)
  481. gpio_set_value(nqx_dev->ese_gpio, 0);
  482. }
  483. mutex_unlock(&nqx_dev->ese_status_mutex);
  484. gpio_set_value(nqx_dev->en_gpio, 1);
  485. usleep_range(10000, 10100);
  486. if (gpio_is_valid(nqx_dev->firm_gpio)) {
  487. gpio_set_value(nqx_dev->firm_gpio, 1);
  488. usleep_range(10000, 10100);
  489. }
  490. gpio_set_value(nqx_dev->en_gpio, 0);
  491. usleep_range(10000, 10100);
  492. gpio_set_value(nqx_dev->en_gpio, 1);
  493. usleep_range(10000, 10100);
  494. } else {
  495. r = -ENOIOCTLCMD;
  496. }
  497. return r;
  498. }
  499. #ifdef CONFIG_COMPAT
  500. static long nfc_compat_ioctl(struct file *pfile, unsigned int cmd,
  501. unsigned long arg)
  502. {
  503. long r = 0;
  504. arg = (compat_u64)arg;
  505. switch (cmd) {
  506. case NFC_SET_PWR:
  507. nfc_ioctl_power_states(pfile, arg);
  508. break;
  509. case ESE_SET_PWR:
  510. nqx_ese_pwr(pfile->private_data, arg);
  511. break;
  512. case ESE_GET_PWR:
  513. nqx_ese_pwr(pfile->private_data, 3);
  514. break;
  515. case SET_RX_BLOCK:
  516. break;
  517. case SET_EMULATOR_TEST_POINT:
  518. break;
  519. default:
  520. r = -ENOTTY;
  521. }
  522. return r;
  523. }
  524. #endif
  525. /*
  526. * nfc_ioctl_core_reset_ntf()
  527. * @filp: pointer to the file descriptor
  528. *
  529. * Allows callers to determine if a CORE_RESET_NTF has arrived
  530. *
  531. * Return: the value of variable core_reset_ntf
  532. */
  533. int nfc_ioctl_core_reset_ntf(struct file *filp)
  534. {
  535. struct nqx_dev *nqx_dev = filp->private_data;
  536. dev_dbg(&nqx_dev->client->dev, "%s: returning = %d\n", __func__,
  537. nqx_dev->core_reset_ntf);
  538. return nqx_dev->core_reset_ntf;
  539. }
  540. /*
  541. * Inside nfc_ioctl_nfcc_info
  542. *
  543. * @brief nfc_ioctl_nfcc_info
  544. *
  545. * Check the NQ Chipset and firmware version details
  546. */
  547. unsigned int nfc_ioctl_nfcc_info(struct file *filp, unsigned long arg)
  548. {
  549. unsigned int r = 0;
  550. struct nqx_dev *nqx_dev = filp->private_data;
  551. r = nqx_dev->nqx_info.i;
  552. dev_dbg(&nqx_dev->client->dev,
  553. "nqx nfc : nfc_ioctl_nfcc_info r = %d\n", r);
  554. return r;
  555. }
  556. static long nfc_ioctl(struct file *pfile, unsigned int cmd,
  557. unsigned long arg)
  558. {
  559. int r = 0;
  560. switch (cmd) {
  561. case NFC_SET_PWR:
  562. r = nfc_ioctl_power_states(pfile, arg);
  563. break;
  564. case ESE_SET_PWR:
  565. r = nqx_ese_pwr(pfile->private_data, arg);
  566. break;
  567. case ESE_GET_PWR:
  568. r = nqx_ese_pwr(pfile->private_data, 3);
  569. break;
  570. case SET_RX_BLOCK:
  571. break;
  572. case SET_EMULATOR_TEST_POINT:
  573. break;
  574. case NFCC_INITIAL_CORE_RESET_NTF:
  575. r = nfc_ioctl_core_reset_ntf(pfile);
  576. break;
  577. case NFCC_GET_INFO:
  578. r = nfc_ioctl_nfcc_info(pfile, arg);
  579. break;
  580. default:
  581. r = -ENOIOCTLCMD;
  582. }
  583. return r;
  584. }
  585. static const struct file_operations nfc_dev_fops = {
  586. .owner = THIS_MODULE,
  587. .llseek = no_llseek,
  588. .read = nfc_read,
  589. .write = nfc_write,
  590. .open = nfc_open,
  591. .unlocked_ioctl = nfc_ioctl,
  592. #ifdef CONFIG_COMPAT
  593. .compat_ioctl = nfc_compat_ioctl
  594. #endif
  595. };
  596. /*
  597. * Routine to enable clock.
  598. * this routine can be extended to select from multiple
  599. * sources based on clk_src_name.
  600. */
  601. static int nqx_clock_select(struct nqx_dev *nqx_dev)
  602. {
  603. int r = 0;
  604. nqx_dev->s_clk = clk_get(&nqx_dev->client->dev, "ref_clk");
  605. if (nqx_dev->s_clk == NULL)
  606. goto err_clk;
  607. if (nqx_dev->clk_run == false)
  608. r = clk_prepare_enable(nqx_dev->s_clk);
  609. if (r)
  610. goto err_clk;
  611. nqx_dev->clk_run = true;
  612. return r;
  613. err_clk:
  614. r = -1;
  615. return r;
  616. }
  617. /*
  618. * Routine to disable clocks
  619. */
  620. static int nqx_clock_deselect(struct nqx_dev *nqx_dev)
  621. {
  622. int r = -1;
  623. if (nqx_dev->s_clk != NULL) {
  624. if (nqx_dev->clk_run == true) {
  625. clk_disable_unprepare(nqx_dev->s_clk);
  626. nqx_dev->clk_run = false;
  627. }
  628. return 0;
  629. }
  630. return r;
  631. }
  632. static int nfc_parse_dt(struct device *dev, struct nqx_platform_data *pdata)
  633. {
  634. int r = 0;
  635. struct device_node *np = dev->of_node;
  636. pdata->en_gpio = of_get_named_gpio(np, "qcom,nq-ven", 0);
  637. if ((!gpio_is_valid(pdata->en_gpio)))
  638. return -EINVAL;
  639. disable_ctrl = pdata->en_gpio;
  640. pdata->irq_gpio = of_get_named_gpio(np, "qcom,nq-irq", 0);
  641. if ((!gpio_is_valid(pdata->irq_gpio)))
  642. return -EINVAL;
  643. pdata->firm_gpio = of_get_named_gpio(np, "qcom,nq-firm", 0);
  644. if (!gpio_is_valid(pdata->firm_gpio)) {
  645. dev_warn(dev,
  646. "FIRM GPIO <OPTIONAL> error getting from OF node\n");
  647. pdata->firm_gpio = -EINVAL;
  648. }
  649. pdata->ese_gpio = of_get_named_gpio(np, "qcom,nq-esepwr", 0);
  650. if (!gpio_is_valid(pdata->ese_gpio)) {
  651. dev_warn(dev,
  652. "ese GPIO <OPTIONAL> error getting from OF node\n");
  653. pdata->ese_gpio = -EINVAL;
  654. }
  655. /* First byte is the expected response length. */
  656. /* E.g., to send a swp_svdd_cfg command to a pn5xx:
  657. * qcom,nq-postinit-cmd = /bits/ 8
  658. * <0x5 0x20 0x02 0x05 0x01 0xA0 0xF2 0x01 0x01>;
  659. */
  660. pdata->i2c_postinit_cmd_len = of_property_count_elems_of_size(np,
  661. "qcom,nq-postinit-cmd",
  662. sizeof(u8));
  663. if (pdata->i2c_postinit_cmd_len > 1) {
  664. pdata->i2c_postinit_cmd = devm_kzalloc(dev,
  665. pdata->i2c_postinit_cmd_len, GFP_KERNEL);
  666. if (!pdata->i2c_postinit_cmd)
  667. pdata->i2c_postinit_cmd_len = 0;
  668. }
  669. if (pdata->i2c_postinit_cmd) {
  670. r = of_property_read_u8_array(np, "qcom,nq-postinit-cmd",
  671. pdata->i2c_postinit_cmd, pdata->i2c_postinit_cmd_len);
  672. if (r) {
  673. dev_warn(dev, "nq-postinit-cmd <OPTIONAL> error ");
  674. dev_warn(dev, " getting from OF node\n");
  675. devm_kfree(dev, pdata->i2c_postinit_cmd);
  676. }
  677. }
  678. if (of_property_read_string(np, "qcom,clk-src", &pdata->clk_src_name))
  679. pdata->clk_pin_voting = false;
  680. else
  681. pdata->clk_pin_voting = true;
  682. pdata->clkreq_gpio = of_get_named_gpio(np, "qcom,nq-clkreq", 0);
  683. return r;
  684. }
  685. static inline int gpio_input_init(const struct device * const dev,
  686. const int gpio, const char * const gpio_name)
  687. {
  688. int r = gpio_request(gpio, gpio_name);
  689. if (r) {
  690. dev_err(dev, "unable to request gpio [%d]\n", gpio);
  691. return r;
  692. }
  693. r = gpio_direction_input(gpio);
  694. if (r)
  695. dev_err(dev, "unable to set direction for gpio [%d]\n", gpio);
  696. return r;
  697. }
  698. static int nqx_probe(struct i2c_client *client,
  699. const struct i2c_device_id *id)
  700. {
  701. int r = 0;
  702. int irqn = 0;
  703. struct nqx_platform_data *platform_data;
  704. struct nqx_dev *nqx_dev;
  705. dev_dbg(&client->dev, "%s: enter\n", __func__);
  706. if (client->dev.of_node) {
  707. platform_data = devm_kzalloc(&client->dev,
  708. sizeof(struct nqx_platform_data), GFP_KERNEL);
  709. if (!platform_data) {
  710. r = -ENOMEM;
  711. goto err_platform_data;
  712. }
  713. r = nfc_parse_dt(&client->dev, platform_data);
  714. if (r)
  715. goto err_free_data;
  716. } else
  717. platform_data = client->dev.platform_data;
  718. dev_dbg(&client->dev,
  719. "%s, inside nfc-nci flags = %x\n",
  720. __func__, client->flags);
  721. if (platform_data == NULL) {
  722. dev_err(&client->dev, "%s: failed\n", __func__);
  723. r = -ENODEV;
  724. goto err_platform_data;
  725. }
  726. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  727. dev_err(&client->dev, "%s: need I2C_FUNC_I2C\n", __func__);
  728. r = -ENODEV;
  729. goto err_free_data;
  730. }
  731. nqx_dev = kzalloc(sizeof(*nqx_dev), GFP_KERNEL);
  732. if (nqx_dev == NULL) {
  733. r = -ENOMEM;
  734. goto err_free_data;
  735. }
  736. nqx_dev->client = client;
  737. nqx_dev->kbuflen = MAX_BUFFER_SIZE;
  738. nqx_dev->kbuf = kzalloc(MAX_BUFFER_SIZE, GFP_KERNEL);
  739. if (!nqx_dev->kbuf) {
  740. dev_err(&client->dev,
  741. "failed to allocate memory for nqx_dev->kbuf\n");
  742. r = -ENOMEM;
  743. goto err_free_dev;
  744. }
  745. if (gpio_is_valid(platform_data->en_gpio)) {
  746. r = gpio_request(platform_data->en_gpio, "nfc_reset_gpio");
  747. if (r) {
  748. dev_err(&client->dev,
  749. "%s: unable to request nfc reset gpio [%d]\n",
  750. __func__,
  751. platform_data->en_gpio);
  752. goto err_mem;
  753. }
  754. r = gpio_direction_output(platform_data->en_gpio, 0);
  755. if (r) {
  756. dev_err(&client->dev,
  757. "%s: unable to set direction for nfc reset gpio [%d]\n",
  758. __func__,
  759. platform_data->en_gpio);
  760. goto err_en_gpio;
  761. }
  762. } else {
  763. dev_err(&client->dev,
  764. "%s: nfc reset gpio not provided\n", __func__);
  765. goto err_mem;
  766. }
  767. if (gpio_is_valid(platform_data->irq_gpio)) {
  768. r = gpio_request(platform_data->irq_gpio, "nfc_irq_gpio");
  769. if (r) {
  770. dev_err(&client->dev, "%s: unable to request nfc irq gpio [%d]\n",
  771. __func__, platform_data->irq_gpio);
  772. goto err_en_gpio;
  773. }
  774. r = gpio_direction_input(platform_data->irq_gpio);
  775. if (r) {
  776. dev_err(&client->dev,
  777. "%s: unable to set direction for nfc irq gpio [%d]\n",
  778. __func__,
  779. platform_data->irq_gpio);
  780. goto err_irq_gpio;
  781. }
  782. irqn = gpio_to_irq(platform_data->irq_gpio);
  783. if (irqn < 0) {
  784. r = irqn;
  785. goto err_irq_gpio;
  786. }
  787. client->irq = irqn;
  788. } else {
  789. dev_err(&client->dev, "%s: irq gpio not provided\n", __func__);
  790. goto err_en_gpio;
  791. }
  792. if (gpio_is_valid(platform_data->firm_gpio)) {
  793. r = gpio_request(platform_data->firm_gpio,
  794. "nfc_firm_gpio");
  795. if (r) {
  796. dev_err(&client->dev,
  797. "%s: unable to request nfc firmware gpio [%d]\n",
  798. __func__, platform_data->firm_gpio);
  799. goto err_irq_gpio;
  800. }
  801. r = gpio_direction_output(platform_data->firm_gpio, 0);
  802. if (r) {
  803. dev_err(&client->dev,
  804. "%s: cannot set direction for nfc firmware gpio [%d]\n",
  805. __func__, platform_data->firm_gpio);
  806. goto err_firm_gpio;
  807. }
  808. } else {
  809. dev_err(&client->dev,
  810. "%s: firm gpio not provided\n", __func__);
  811. goto err_irq_gpio;
  812. }
  813. if (gpio_is_valid(platform_data->ese_gpio)) {
  814. r = gpio_request(platform_data->ese_gpio,
  815. "nfc-ese_pwr");
  816. if (r) {
  817. nqx_dev->ese_gpio = -EINVAL;
  818. dev_err(&client->dev,
  819. "%s: unable to request nfc ese gpio [%d]\n",
  820. __func__, platform_data->ese_gpio);
  821. /* ese gpio optional so we should continue */
  822. } else {
  823. nqx_dev->ese_gpio = platform_data->ese_gpio;
  824. r = gpio_direction_output(platform_data->ese_gpio, 0);
  825. if (r) {
  826. /*
  827. * free ese gpio and set invalid
  828. * to avoid further use
  829. */
  830. gpio_free(platform_data->ese_gpio);
  831. nqx_dev->ese_gpio = -EINVAL;
  832. dev_err(&client->dev,
  833. "%s: cannot set direction for nfc ese gpio [%d]\n",
  834. __func__, platform_data->ese_gpio);
  835. /* ese gpio optional so we should continue */
  836. }
  837. }
  838. } else {
  839. nqx_dev->ese_gpio = -EINVAL;
  840. dev_err(&client->dev,
  841. "%s: ese gpio not provided\n", __func__);
  842. /* ese gpio optional so we should continue */
  843. }
  844. if (gpio_is_valid(platform_data->clkreq_gpio)) {
  845. r = gpio_request(platform_data->clkreq_gpio,
  846. "nfc_clkreq_gpio");
  847. if (r) {
  848. dev_err(&client->dev,
  849. "%s: unable to request nfc clkreq gpio [%d]\n",
  850. __func__, platform_data->clkreq_gpio);
  851. goto err_ese_gpio;
  852. }
  853. r = gpio_direction_input(platform_data->clkreq_gpio);
  854. if (r) {
  855. dev_err(&client->dev,
  856. "%s: cannot set direction for nfc clkreq gpio [%d]\n",
  857. __func__, platform_data->clkreq_gpio);
  858. goto err_clkreq_gpio;
  859. }
  860. } else {
  861. dev_err(&client->dev,
  862. "%s: clkreq gpio not provided\n", __func__);
  863. }
  864. nqx_dev->en_gpio = platform_data->en_gpio;
  865. nqx_dev->irq_gpio = platform_data->irq_gpio;
  866. nqx_dev->firm_gpio = platform_data->firm_gpio;
  867. nqx_dev->clkreq_gpio = platform_data->clkreq_gpio;
  868. nqx_dev->pdata = platform_data;
  869. /* init mutex and queues */
  870. init_waitqueue_head(&nqx_dev->read_wq);
  871. mutex_init(&nqx_dev->read_mutex);
  872. mutex_init(&nqx_dev->ese_status_mutex);
  873. spin_lock_init(&nqx_dev->irq_enabled_lock);
  874. nqx_dev->nqx_device.minor = MISC_DYNAMIC_MINOR;
  875. nqx_dev->nqx_device.name = "nq-nci";
  876. nqx_dev->nqx_device.fops = &nfc_dev_fops;
  877. r = misc_register(&nqx_dev->nqx_device);
  878. if (r) {
  879. dev_err(&client->dev, "%s: misc_register failed\n", __func__);
  880. goto err_misc_register;
  881. }
  882. /* NFC_INT IRQ */
  883. nqx_dev->irq_enabled = true;
  884. r = request_irq(client->irq, nqx_dev_irq_handler,
  885. IRQF_TRIGGER_HIGH, client->name, nqx_dev);
  886. if (r) {
  887. dev_err(&client->dev, "%s: request_irq failed\n", __func__);
  888. goto err_request_irq_failed;
  889. }
  890. nqx_disable_irq(nqx_dev);
  891. /* Register reboot notifier here */
  892. r = register_reboot_notifier(&nfcc_notifier);
  893. if (r) {
  894. dev_err(&client->dev,
  895. "%s: cannot register reboot notifier(err = %d)\n",
  896. __func__, r);
  897. /*
  898. * nfcc_hw_check function not doing memory
  899. * allocation so using same goto target here
  900. */
  901. goto err_request_hw_check_failed;
  902. }
  903. #ifdef NFC_KERNEL_BU
  904. r = nqx_clock_select(nqx_dev);
  905. if (r < 0) {
  906. dev_err(&client->dev,
  907. "%s: nqx_clock_select failed\n", __func__);
  908. goto err_clock_en_failed;
  909. }
  910. gpio_set_value(platform_data->en_gpio, 1);
  911. #endif
  912. device_init_wakeup(&client->dev, true);
  913. device_set_wakeup_capable(&client->dev, true);
  914. i2c_set_clientdata(client, nqx_dev);
  915. nqx_dev->irq_wake_up = false;
  916. dev_err(&client->dev,
  917. "%s: probing NFCC NQxxx exited successfully\n",
  918. __func__);
  919. return 0;
  920. #ifdef NFC_KERNEL_BU
  921. err_clock_en_failed:
  922. unregister_reboot_notifier(&nfcc_notifier);
  923. #endif
  924. err_request_hw_check_failed:
  925. free_irq(client->irq, nqx_dev);
  926. err_request_irq_failed:
  927. misc_deregister(&nqx_dev->nqx_device);
  928. err_misc_register:
  929. mutex_destroy(&nqx_dev->read_mutex);
  930. mutex_destroy(&nqx_dev->ese_status_mutex);
  931. err_clkreq_gpio:
  932. gpio_free(platform_data->clkreq_gpio);
  933. err_ese_gpio:
  934. /* optional gpio, not sure was configured in probe */
  935. if (nqx_dev->ese_gpio > 0)
  936. gpio_free(platform_data->ese_gpio);
  937. err_firm_gpio:
  938. gpio_free(platform_data->firm_gpio);
  939. err_irq_gpio:
  940. gpio_free(platform_data->irq_gpio);
  941. err_en_gpio:
  942. gpio_free(platform_data->en_gpio);
  943. err_mem:
  944. kfree(nqx_dev->kbuf);
  945. err_free_dev:
  946. kfree(nqx_dev);
  947. err_free_data:
  948. if (client->dev.of_node) {
  949. if (platform_data->i2c_postinit_cmd)
  950. devm_kfree(&client->dev,
  951. platform_data->i2c_postinit_cmd);
  952. devm_kfree(&client->dev, platform_data);
  953. }
  954. err_platform_data:
  955. dev_err(&client->dev,
  956. "%s: probing nqxx failed, check hardware\n",
  957. __func__);
  958. return r;
  959. }
  960. static int nqx_remove(struct i2c_client *client)
  961. {
  962. int ret = 0;
  963. struct nqx_dev *nqx_dev;
  964. nqx_dev = i2c_get_clientdata(client);
  965. if (!nqx_dev) {
  966. dev_err(&client->dev,
  967. "%s: device doesn't exist anymore\n", __func__);
  968. ret = -ENODEV;
  969. goto err;
  970. }
  971. unregister_reboot_notifier(&nfcc_notifier);
  972. free_irq(client->irq, nqx_dev);
  973. misc_deregister(&nqx_dev->nqx_device);
  974. mutex_destroy(&nqx_dev->read_mutex);
  975. mutex_destroy(&nqx_dev->ese_status_mutex);
  976. gpio_free(nqx_dev->clkreq_gpio);
  977. /* optional gpio, not sure was configured in probe */
  978. if (nqx_dev->ese_gpio > 0)
  979. gpio_free(nqx_dev->ese_gpio);
  980. gpio_free(nqx_dev->firm_gpio);
  981. gpio_free(nqx_dev->irq_gpio);
  982. gpio_free(nqx_dev->en_gpio);
  983. kfree(nqx_dev->kbuf);
  984. if (nqx_dev->pdata->i2c_postinit_cmd)
  985. devm_kfree(&client->dev,
  986. nqx_dev->pdata->i2c_postinit_cmd);
  987. if (client->dev.of_node)
  988. devm_kfree(&client->dev, nqx_dev->pdata);
  989. kfree(nqx_dev);
  990. err:
  991. return ret;
  992. }
  993. static int nqx_suspend(struct device *device)
  994. {
  995. struct i2c_client *client = to_i2c_client(device);
  996. struct nqx_dev *nqx_dev = i2c_get_clientdata(client);
  997. if (device_may_wakeup(&client->dev) && nqx_dev->irq_enabled) {
  998. if (!enable_irq_wake(client->irq))
  999. nqx_dev->irq_wake_up = true;
  1000. }
  1001. return 0;
  1002. }
  1003. static int nqx_resume(struct device *device)
  1004. {
  1005. struct i2c_client *client = to_i2c_client(device);
  1006. struct nqx_dev *nqx_dev = i2c_get_clientdata(client);
  1007. if (device_may_wakeup(&client->dev) && nqx_dev->irq_wake_up) {
  1008. if (!disable_irq_wake(client->irq))
  1009. nqx_dev->irq_wake_up = false;
  1010. }
  1011. return 0;
  1012. }
  1013. static const struct i2c_device_id nqx_id[] = {
  1014. {"nqx-i2c", 0},
  1015. {}
  1016. };
  1017. static const struct dev_pm_ops nfc_pm_ops = {
  1018. SET_SYSTEM_SLEEP_PM_OPS(nqx_suspend, nqx_resume)
  1019. };
  1020. static struct i2c_driver nqx = {
  1021. .id_table = nqx_id,
  1022. .probe = nqx_probe,
  1023. .remove = nqx_remove,
  1024. .driver = {
  1025. .owner = THIS_MODULE,
  1026. .name = "nq-nci",
  1027. .of_match_table = msm_match_table,
  1028. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1029. .pm = &nfc_pm_ops,
  1030. },
  1031. };
  1032. static int nfcc_reboot(struct notifier_block *notifier, unsigned long val,
  1033. void *v)
  1034. {
  1035. gpio_set_value(disable_ctrl, 1);
  1036. return NOTIFY_OK;
  1037. }
  1038. /*
  1039. * module load/unload record keeping
  1040. */
  1041. static int __init nqx_dev_init(void)
  1042. {
  1043. return i2c_add_driver(&nqx);
  1044. }
  1045. module_init(nqx_dev_init);
  1046. static void __exit nqx_dev_exit(void)
  1047. {
  1048. unregister_reboot_notifier(&nfcc_notifier);
  1049. i2c_del_driver(&nqx);
  1050. }
  1051. module_exit(nqx_dev_exit);
  1052. MODULE_DESCRIPTION("NFC nqx");
  1053. MODULE_LICENSE("GPL v2");