nfc_common.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2019-2021 NXP
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. ******************************************************************************/
  21. #include <linux/of_gpio.h>
  22. #include <linux/of_device.h>
  23. #include <linux/delay.h>
  24. #include "nfc_common.h"
  25. int nfc_parse_dt(struct device *dev, struct platform_configs *nfc_configs,
  26. uint8_t interface)
  27. {
  28. int ret;
  29. struct device_node *np = dev->of_node;
  30. struct platform_gpio *nfc_gpio = &nfc_configs->gpio;
  31. struct platform_ldo *ldo = &nfc_configs->ldo;
  32. if (!np) {
  33. pr_err("nfc of_node NULL\n");
  34. return -EINVAL;
  35. }
  36. nfc_gpio->irq = -EINVAL;
  37. nfc_gpio->dwl_req = -EINVAL;
  38. nfc_gpio->ven = -EINVAL;
  39. nfc_gpio->clkreq = -EINVAL;
  40. /* required for i2c based chips only */
  41. if (interface == PLATFORM_IF_I2C) {
  42. nfc_gpio->irq = of_get_named_gpio(np, DTS_IRQ_GPIO_STR, 0);
  43. if ((!gpio_is_valid(nfc_gpio->irq))) {
  44. pr_err("nfc irq gpio invalid %d\n", nfc_gpio->irq);
  45. return -EINVAL;
  46. }
  47. pr_info("%s: irq %d\n", __func__, nfc_gpio->irq);
  48. }
  49. nfc_gpio->ven = of_get_named_gpio(np, DTS_VEN_GPIO_STR, 0);
  50. if ((!gpio_is_valid(nfc_gpio->ven))) {
  51. pr_err("nfc ven gpio invalid %d\n", nfc_gpio->ven);
  52. return -EINVAL;
  53. }
  54. nfc_gpio->dwl_req = of_get_named_gpio(np, DTS_FWDN_GPIO_STR, 0);
  55. /* not returning failure for dwl gpio as it is optional for sn220 */
  56. if ((!gpio_is_valid(nfc_gpio->dwl_req)))
  57. pr_warn("nfc dwl_req gpio invalid %d\n", nfc_gpio->dwl_req);
  58. nfc_gpio->clkreq = of_get_named_gpio(np, DTS_CLKREQ_GPIO_STR, 0);
  59. if (!gpio_is_valid(nfc_gpio->clkreq)) {
  60. dev_err(dev, "clkreq gpio invalid %d\n", nfc_gpio->dwl_req);
  61. return -EINVAL;
  62. }
  63. pr_info("%s: ven %d, dwl req %d, clkreq %d\n", __func__,
  64. nfc_gpio->ven, nfc_gpio->dwl_req, nfc_gpio->clkreq);
  65. // optional property
  66. ret = of_property_read_u32_array(np, NFC_LDO_VOL_DT_NAME,
  67. (u32 *) ldo->vdd_levels,
  68. ARRAY_SIZE(ldo->vdd_levels));
  69. if (ret) {
  70. dev_err(dev, "error reading NFC VDDIO min and max value\n");
  71. // set default as per datasheet
  72. ldo->vdd_levels[0] = NFC_VDDIO_MIN;
  73. ldo->vdd_levels[1] = NFC_VDDIO_MAX;
  74. }
  75. // optional property
  76. ret = of_property_read_u32(np, NFC_LDO_CUR_DT_NAME, &ldo->max_current);
  77. if (ret) {
  78. dev_err(dev, "error reading NFC current value\n");
  79. // set default as per datasheet
  80. ldo->max_current = NFC_CURRENT_MAX;
  81. }
  82. return 0;
  83. }
  84. /**
  85. * nfc_ldo_vote()
  86. * @nfc_dev: NFC device containing regulator handle
  87. *
  88. * LDO voting based on voltage and current entries in DT
  89. *
  90. * Return: 0 on success and -ve on failure
  91. */
  92. int nfc_ldo_vote(struct nfc_dev *nfc_dev)
  93. {
  94. int ret;
  95. ret = regulator_set_voltage(nfc_dev->reg,
  96. nfc_dev->configs.ldo.vdd_levels[0],
  97. nfc_dev->configs.ldo.vdd_levels[1]);
  98. if (ret < 0) {
  99. pr_err("%s: set voltage failed\n", __func__);
  100. return ret;
  101. }
  102. /* pass expected current from NFC in uA */
  103. ret = regulator_set_load(nfc_dev->reg, nfc_dev->configs.ldo.max_current);
  104. if (ret < 0) {
  105. pr_err("%s: set load failed\n", __func__);
  106. return ret;
  107. }
  108. ret = regulator_enable(nfc_dev->reg);
  109. if (ret < 0)
  110. pr_err("%s: regulator_enable failed\n", __func__);
  111. else
  112. nfc_dev->is_vreg_enabled = true;
  113. return ret;
  114. }
  115. /**
  116. * nfc_ldo_config()
  117. * @dev: device instance to read DT entry
  118. * @nfc_dev: NFC device containing regulator handle
  119. *
  120. * Configure LDO if entry is present in DT file otherwise
  121. * return with success as it's optional
  122. *
  123. * Return: 0 on success and -ve on failure
  124. */
  125. int nfc_ldo_config(struct device *dev, struct nfc_dev *nfc_dev)
  126. {
  127. int ret;
  128. if (of_get_property(dev->of_node, NFC_LDO_SUPPLY_NAME, NULL)) {
  129. // Get the regulator handle
  130. nfc_dev->reg = regulator_get(dev, NFC_LDO_SUPPLY_DT_NAME);
  131. if (IS_ERR(nfc_dev->reg)) {
  132. ret = PTR_ERR(nfc_dev->reg);
  133. nfc_dev->reg = NULL;
  134. pr_err("%s: regulator_get failed, ret = %d\n",
  135. __func__, ret);
  136. return ret;
  137. }
  138. } else {
  139. nfc_dev->reg = NULL;
  140. pr_err("%s: regulator entry not present\n", __func__);
  141. // return success as it's optional to configure LDO
  142. return 0;
  143. }
  144. // LDO config supported by platform DT
  145. ret = nfc_ldo_vote(nfc_dev);
  146. if (ret < 0) {
  147. pr_err("%s: LDO voting failed, ret = %d\n", __func__, ret);
  148. regulator_put(nfc_dev->reg);
  149. }
  150. return ret;
  151. }
  152. /**
  153. * nfc_ldo_unvote()
  154. * @nfc_dev: NFC device containing regulator handle
  155. *
  156. * set voltage and load to zero and disable regulator
  157. *
  158. * Return: 0 on success and -ve on failure
  159. */
  160. int nfc_ldo_unvote(struct nfc_dev *nfc_dev)
  161. {
  162. int ret;
  163. if (!nfc_dev->is_vreg_enabled) {
  164. pr_err("%s: regulator already disabled\n", __func__);
  165. return -EINVAL;
  166. }
  167. ret = regulator_disable(nfc_dev->reg);
  168. if (ret < 0) {
  169. pr_err("%s: regulator_disable failed\n", __func__);
  170. return ret;
  171. }
  172. nfc_dev->is_vreg_enabled = false;
  173. ret = regulator_set_voltage(nfc_dev->reg, 0, NFC_VDDIO_MAX);
  174. if (ret < 0) {
  175. pr_err("%s: set voltage failed\n", __func__);
  176. return ret;
  177. }
  178. ret = regulator_set_load(nfc_dev->reg, 0);
  179. if (ret < 0)
  180. pr_err("%s: set load failed\n", __func__);
  181. return ret;
  182. }
  183. void set_valid_gpio(int gpio, int value)
  184. {
  185. if (gpio_is_valid(gpio)) {
  186. pr_debug("%s gpio %d value %d\n", __func__, gpio, value);
  187. gpio_set_value(gpio, value);
  188. /* hardware dependent delay */
  189. usleep_range(NFC_GPIO_SET_WAIT_TIME_USEC,
  190. NFC_GPIO_SET_WAIT_TIME_USEC + 100);
  191. }
  192. }
  193. int get_valid_gpio(int gpio)
  194. {
  195. int value = -EINVAL;
  196. if (gpio_is_valid(gpio)) {
  197. value = gpio_get_value(gpio);
  198. pr_debug("%s gpio %d value %d\n", __func__, gpio, value);
  199. }
  200. return value;
  201. }
  202. void gpio_set_ven(struct nfc_dev *nfc_dev, int value)
  203. {
  204. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  205. if (gpio_get_value(nfc_gpio->ven) != value) {
  206. pr_debug("%s: value %d\n", __func__, value);
  207. gpio_set_value(nfc_gpio->ven, value);
  208. /* hardware dependent delay */
  209. usleep_range(NFC_GPIO_SET_WAIT_TIME_USEC,
  210. NFC_GPIO_SET_WAIT_TIME_USEC + 100);
  211. }
  212. }
  213. int configure_gpio(unsigned int gpio, int flag)
  214. {
  215. int ret;
  216. pr_debug("%s: nfc gpio [%d] flag [%01x]\n", __func__, gpio, flag);
  217. if (gpio_is_valid(gpio)) {
  218. ret = gpio_request(gpio, "nfc_gpio");
  219. if (ret) {
  220. pr_err("%s: unable to request nfc gpio [%d]\n",
  221. __func__, gpio);
  222. return ret;
  223. }
  224. /* set direction and value for output pin */
  225. if (flag & GPIO_OUTPUT) {
  226. ret = gpio_direction_output(gpio, (GPIO_HIGH & flag));
  227. pr_debug("nfc o/p gpio %d level %d\n", gpio, gpio_get_value(gpio));
  228. } else {
  229. ret = gpio_direction_input(gpio);
  230. pr_debug("nfc i/p gpio %d\n", gpio);
  231. }
  232. if (ret) {
  233. pr_err
  234. ("%s: unable to set direction for nfc gpio [%d]\n",
  235. __func__, gpio);
  236. gpio_free(gpio);
  237. return ret;
  238. }
  239. // Consider value as control for input IRQ pin
  240. if (flag & GPIO_IRQ) {
  241. ret = gpio_to_irq(gpio);
  242. if (ret < 0) {
  243. pr_err("%s: unable to set irq for nfc gpio [%d]\n",
  244. __func__, gpio);
  245. gpio_free(gpio);
  246. return ret;
  247. }
  248. pr_debug
  249. ("%s: gpio_to_irq successful [%d]\n",
  250. __func__, gpio);
  251. return ret;
  252. }
  253. } else {
  254. pr_err("%s: invalid gpio\n", __func__);
  255. ret = -EINVAL;
  256. }
  257. return ret;
  258. }
  259. void nfc_misc_unregister(struct nfc_dev *nfc_dev, int count)
  260. {
  261. pr_debug("%s: entry\n", __func__);
  262. kfree(nfc_dev->kbuf);
  263. device_destroy(nfc_dev->nfc_class, nfc_dev->devno);
  264. cdev_del(&nfc_dev->c_dev);
  265. class_destroy(nfc_dev->nfc_class);
  266. unregister_chrdev_region(nfc_dev->devno, count);
  267. if (nfc_dev->ipcl)
  268. ipc_log_context_destroy(nfc_dev->ipcl);
  269. }
  270. int nfc_misc_register(struct nfc_dev *nfc_dev,
  271. const struct file_operations *nfc_fops, int count,
  272. char *devname, char *classname)
  273. {
  274. int ret = 0;
  275. ret = alloc_chrdev_region(&nfc_dev->devno, 0, count, devname);
  276. if (ret < 0) {
  277. pr_err("%s: failed to alloc chrdev region ret %d\n",
  278. __func__, ret);
  279. return ret;
  280. }
  281. nfc_dev->nfc_class = class_create(THIS_MODULE, classname);
  282. if (IS_ERR(nfc_dev->nfc_class)) {
  283. ret = PTR_ERR(nfc_dev->nfc_class);
  284. pr_err("%s: failed to register device class ret %d\n",
  285. __func__, ret);
  286. unregister_chrdev_region(nfc_dev->devno, count);
  287. return ret;
  288. }
  289. cdev_init(&nfc_dev->c_dev, nfc_fops);
  290. ret = cdev_add(&nfc_dev->c_dev, nfc_dev->devno, count);
  291. if (ret < 0) {
  292. pr_err("%s: failed to add cdev ret %d\n", __func__, ret);
  293. class_destroy(nfc_dev->nfc_class);
  294. unregister_chrdev_region(nfc_dev->devno, count);
  295. return ret;
  296. }
  297. nfc_dev->nfc_device = device_create(nfc_dev->nfc_class, NULL,
  298. nfc_dev->devno, nfc_dev, devname);
  299. if (IS_ERR(nfc_dev->nfc_device)) {
  300. ret = PTR_ERR(nfc_dev->nfc_device);
  301. pr_err("%s: failed to create the device ret %d\n",
  302. __func__, ret);
  303. cdev_del(&nfc_dev->c_dev);
  304. class_destroy(nfc_dev->nfc_class);
  305. unregister_chrdev_region(nfc_dev->devno, count);
  306. return ret;
  307. }
  308. nfc_dev->ipcl = ipc_log_context_create(NUM_OF_IPC_LOG_PAGES,
  309. dev_name(nfc_dev->nfc_device), 0);
  310. nfc_dev->kbuflen = MAX_BUFFER_SIZE;
  311. nfc_dev->kbuf = kzalloc(MAX_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
  312. if (!nfc_dev->kbuf) {
  313. nfc_misc_unregister(nfc_dev, count);
  314. return -ENOMEM;
  315. }
  316. nfc_dev->cold_reset.rsp_pending = false;
  317. nfc_dev->cold_reset.is_nfc_enabled = false;
  318. nfc_dev->cold_reset.is_crp_en = false;
  319. nfc_dev->cold_reset.last_src_ese_prot = ESE_COLD_RESET_ORIGIN_NONE;
  320. init_waitqueue_head(&nfc_dev->cold_reset.read_wq);
  321. return 0;
  322. }
  323. /*
  324. * Power management of the eSE
  325. * eSE and NFCC both are powered using VEN gpio,
  326. * VEN HIGH - eSE and NFCC both are powered on
  327. * VEN LOW - eSE and NFCC both are power down
  328. */
  329. int nfc_ese_pwr(struct nfc_dev *nfc_dev, unsigned long arg)
  330. {
  331. int ret = 0;
  332. if (arg == ESE_POWER_ON) {
  333. /*
  334. * Let's store the NFC VEN pin state
  335. * will check stored value in case of eSE power off request,
  336. * to find out if NFC MW also sent request to set VEN HIGH
  337. * VEN state will remain HIGH if NFC is enabled otherwise
  338. * it will be set as LOW
  339. */
  340. nfc_dev->nfc_ven_enabled = gpio_get_value(nfc_dev->configs.gpio.ven);
  341. if (!nfc_dev->nfc_ven_enabled) {
  342. pr_debug("eSE HAL service setting ven HIGH\n");
  343. gpio_set_ven(nfc_dev, 1);
  344. } else {
  345. pr_debug("ven already HIGH\n");
  346. }
  347. nfc_dev->is_ese_session_active = true;
  348. } else if (arg == ESE_POWER_OFF) {
  349. if (!nfc_dev->nfc_ven_enabled) {
  350. pr_debug("NFC not enabled, disabling ven\n");
  351. gpio_set_ven(nfc_dev, 0);
  352. } else {
  353. pr_debug("keep ven high as NFC is enabled\n");
  354. }
  355. nfc_dev->is_ese_session_active = false;
  356. } else if (arg == ESE_POWER_STATE) {
  357. /* get VEN gpio state for eSE, as eSE also enabled through same GPIO */
  358. ret = gpio_get_value(nfc_dev->configs.gpio.ven);
  359. } else {
  360. pr_err("%s bad arg %lu\n", __func__, arg);
  361. ret = -ENOIOCTLCMD;
  362. }
  363. return ret;
  364. }
  365. /*
  366. * nfc_ioctl_power_states() - power control
  367. * @nfc_dev: nfc device data structure
  368. * @arg: mode that we want to move to
  369. *
  370. * Device power control. Depending on the arg value, device moves to
  371. * different states, refer nfcc_ioctl_request in nfc_common.h for args
  372. *
  373. * Return: -ENOIOCTLCMD if arg is not supported, 0 in any other case
  374. */
  375. static int nfc_ioctl_power_states(struct nfc_dev *nfc_dev, unsigned long arg)
  376. {
  377. int ret = 0;
  378. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  379. if (arg == NFC_POWER_OFF) {
  380. /*
  381. * We are attempting a hardware reset so let us disable
  382. * interrupts to avoid spurious notifications to upper
  383. * layers.
  384. */
  385. nfc_dev->nfc_disable_intr(nfc_dev);
  386. set_valid_gpio(nfc_gpio->dwl_req, 0);
  387. gpio_set_ven(nfc_dev, 0);
  388. nfc_dev->nfc_ven_enabled = false;
  389. } else if (arg == NFC_POWER_ON) {
  390. nfc_dev->nfc_enable_intr(nfc_dev);
  391. set_valid_gpio(nfc_gpio->dwl_req, 0);
  392. gpio_set_ven(nfc_dev, 1);
  393. nfc_dev->nfc_ven_enabled = true;
  394. } else if (arg == NFC_FW_DWL_VEN_TOGGLE) {
  395. /*
  396. * We are switching to download Mode, toggle the enable pin
  397. * in order to set the NFCC in the new mode
  398. */
  399. nfc_dev->nfc_disable_intr(nfc_dev);
  400. set_valid_gpio(nfc_gpio->dwl_req, 1);
  401. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  402. gpio_set_ven(nfc_dev, 0);
  403. gpio_set_ven(nfc_dev, 1);
  404. nfc_dev->nfc_enable_intr(nfc_dev);
  405. } else if (arg == NFC_FW_DWL_HIGH) {
  406. /*
  407. * Setting firmware download gpio to HIGH
  408. * before FW download start
  409. */
  410. pr_debug("set fw gpio high\n");
  411. set_valid_gpio(nfc_gpio->dwl_req, 1);
  412. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  413. } else if (arg == NFC_VEN_FORCED_HARD_RESET) {
  414. nfc_dev->nfc_disable_intr(nfc_dev);
  415. gpio_set_ven(nfc_dev, 0);
  416. gpio_set_ven(nfc_dev, 1);
  417. nfc_dev->nfc_enable_intr(nfc_dev);
  418. pr_info("%s VEN forced reset done\n", __func__);
  419. } else if (arg == NFC_FW_DWL_LOW) {
  420. /*
  421. * Setting firmware download gpio to LOW
  422. * FW download finished
  423. */
  424. pr_debug("set fw gpio LOW\n");
  425. set_valid_gpio(nfc_gpio->dwl_req, 0);
  426. nfc_dev->nfc_state = NFC_STATE_NCI;
  427. } else if (arg == NFC_ENABLE) {
  428. /*
  429. * Setting flag true when NFC is enabled
  430. */
  431. nfc_dev->cold_reset.is_nfc_enabled = true;
  432. } else if (arg == NFC_DISABLE) {
  433. /*
  434. * Setting flag true when NFC is disabled
  435. */
  436. nfc_dev->cold_reset.is_nfc_enabled = false;
  437. } else {
  438. pr_err("%s bad arg %lu\n", __func__, arg);
  439. ret = -ENOIOCTLCMD;
  440. }
  441. return ret;
  442. }
  443. /*
  444. * Inside nfc_ioctl_nfcc_info
  445. *
  446. * @brief nfc_ioctl_nfcc_info
  447. *
  448. * Check the NFC Chipset and firmware version details
  449. */
  450. unsigned int nfc_ioctl_nfcc_info(struct file *filp, unsigned long arg)
  451. {
  452. unsigned int r = 0;
  453. struct nfc_dev *nfc_dev = filp->private_data;
  454. r = nfc_dev->nqx_info.i;
  455. pr_debug("nfc : %s r = 0x%x\n", __func__, r);
  456. return r;
  457. }
  458. /** @brief IOCTL function to be used to set or get data from upper layer.
  459. *
  460. * @param pfile fil node for opened device.
  461. * @cmd IOCTL type from upper layer.
  462. * @arg IOCTL arg from upper layer.
  463. *
  464. * @return 0 on success, error code for failures.
  465. */
  466. long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
  467. {
  468. int ret = 0;
  469. struct nfc_dev *nfc_dev = pfile->private_data;
  470. if (!nfc_dev)
  471. return -ENODEV;
  472. pr_debug("%s cmd = %x arg = %zx\n", __func__, cmd, arg);
  473. switch (cmd) {
  474. case NFC_SET_PWR:
  475. ret = nfc_ioctl_power_states(nfc_dev, arg);
  476. break;
  477. case ESE_SET_PWR:
  478. ret = nfc_ese_pwr(nfc_dev, arg);
  479. break;
  480. case ESE_GET_PWR:
  481. ret = nfc_ese_pwr(nfc_dev, ESE_POWER_STATE);
  482. break;
  483. case NFCC_GET_INFO:
  484. ret = nfc_ioctl_nfcc_info(pfile, arg);
  485. break;
  486. case NFC_GET_PLATFORM_TYPE:
  487. ret = nfc_dev->interface;
  488. break;
  489. case ESE_COLD_RESET:
  490. pr_debug("nfc ese cold reset ioctl\n");
  491. ret = ese_cold_reset_ioctl(nfc_dev, arg);
  492. break;
  493. case NFC_GET_IRQ_STATE:
  494. ret = gpio_get_value(nfc_dev->configs.gpio.irq);
  495. break;
  496. default:
  497. pr_err("%s Unsupported ioctl cmd 0x%x, arg %lu\n",
  498. __func__, cmd, arg);
  499. ret = -ENOIOCTLCMD;
  500. }
  501. return ret;
  502. }
  503. int nfc_dev_open(struct inode *inode, struct file *filp)
  504. {
  505. struct nfc_dev *nfc_dev = container_of(inode->i_cdev,
  506. struct nfc_dev, c_dev);
  507. if (!nfc_dev)
  508. return -ENODEV;
  509. pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  510. mutex_lock(&nfc_dev->dev_ref_mutex);
  511. filp->private_data = nfc_dev;
  512. if (nfc_dev->dev_ref_count == 0) {
  513. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  514. nfc_dev->nfc_enable_intr(nfc_dev);
  515. }
  516. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count + 1;
  517. mutex_unlock(&nfc_dev->dev_ref_mutex);
  518. return 0;
  519. }
  520. int nfc_dev_close(struct inode *inode, struct file *filp)
  521. {
  522. struct nfc_dev *nfc_dev = container_of(inode->i_cdev,
  523. struct nfc_dev, c_dev);
  524. if (!nfc_dev)
  525. return -ENODEV;
  526. pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  527. mutex_lock(&nfc_dev->dev_ref_mutex);
  528. if (nfc_dev->dev_ref_count == 1) {
  529. nfc_dev->nfc_disable_intr(nfc_dev);
  530. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  531. }
  532. if (nfc_dev->dev_ref_count > 0)
  533. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count - 1;
  534. filp->private_data = NULL;
  535. mutex_unlock(&nfc_dev->dev_ref_mutex);
  536. return 0;
  537. }
  538. int is_nfc_data_available_for_read(struct nfc_dev *nfc_dev)
  539. {
  540. int ret;
  541. nfc_dev->nfc_enable_intr(nfc_dev);
  542. ret = wait_event_interruptible_timeout(nfc_dev->read_wq,
  543. !nfc_dev->i2c_dev.irq_enabled,
  544. msecs_to_jiffies(MAX_IRQ_WAIT_TIME));
  545. return ret;
  546. }
  547. /**
  548. * get_nfcc_chip_type_dl() - get chip type in fw download command;
  549. * @nfc_dev: nfc device data structure
  550. *
  551. * Perform get version command and determine chip
  552. * type from response.
  553. *
  554. * @Return: enum chip_types value
  555. *
  556. */
  557. static enum chip_types get_nfcc_chip_type_dl(struct nfc_dev *nfc_dev)
  558. {
  559. int ret = 0;
  560. uint8_t *cmd = nfc_dev->write_kbuf;
  561. uint8_t *rsp = nfc_dev->read_kbuf;
  562. enum chip_types chip_type = CHIP_UNKNOWN;
  563. *cmd++ = DL_CMD;
  564. *cmd++ = DL_GET_VERSION_CMD_PAYLOAD_LEN;
  565. *cmd++ = DL_GET_VERSION_CMD_ID;
  566. *cmd++ = DL_PAYLOAD_BYTE_ZERO;
  567. *cmd++ = DL_PAYLOAD_BYTE_ZERO;
  568. *cmd++ = DL_PAYLOAD_BYTE_ZERO;
  569. *cmd++ = DL_GET_VERSION_CMD_CRC_1;
  570. *cmd++ = DL_GET_VERSION_CMD_CRC_2;
  571. pr_debug("%s:Sending GET_VERSION cmd of size = %d\n", __func__, DL_GET_VERSION_CMD_LEN);
  572. ret = nfc_dev->nfc_write(nfc_dev, nfc_dev->write_kbuf, DL_GET_VERSION_CMD_LEN,
  573. MAX_RETRY_COUNT);
  574. if (ret <= 0) {
  575. pr_err("%s: - nfc get version cmd error ret %d\n", __func__, ret);
  576. goto err;
  577. }
  578. memset(rsp, 0x00, DL_GET_VERSION_RSP_LEN_2);
  579. pr_debug("%s:Reading response of GET_VERSION cmd\n", __func__);
  580. ret = nfc_dev->nfc_read(nfc_dev, rsp, DL_GET_VERSION_RSP_LEN_2, NCI_CMD_RSP_TIMEOUT);
  581. if (ret <= 0) {
  582. pr_err("%s: - nfc get version rsp error ret %d\n", __func__, ret);
  583. goto err;
  584. }
  585. if (rsp[0] == FW_MSG_CMD_RSP && ret >= DL_GET_VERSION_RSP_LEN_2) {
  586. nfc_dev->fw_major_version = rsp[FW_MAJOR_VER_OFFSET];
  587. if (rsp[FW_ROM_CODE_VER_OFFSET] == SN1XX_ROM_VER &&
  588. rsp[FW_MAJOR_VER_OFFSET] == SN1xx_MAJOR_VER)
  589. chip_type = CHIP_SN1XX;
  590. else if (rsp[FW_ROM_CODE_VER_OFFSET] == SN220_ROM_VER &&
  591. rsp[FW_MAJOR_VER_OFFSET] == SN220_MAJOR_VER)
  592. chip_type = CHIP_SN220;
  593. pr_debug("%s:NFC Chip Type 0x%02x Rom Version 0x%02x FW Minor 0x%02x Major 0x%02x\n",
  594. __func__, rsp[GET_VERSION_RSP_CHIP_TYPE_OFFSET],
  595. rsp[FW_ROM_CODE_VER_OFFSET],
  596. rsp[GET_VERSION_RSP_MINOR_VERSION_OFFSET],
  597. rsp[FW_MAJOR_VER_OFFSET]);
  598. nfc_dev->nqx_info.info.chip_type = rsp[GET_VERSION_RSP_CHIP_TYPE_OFFSET];
  599. nfc_dev->nqx_info.info.rom_version = rsp[FW_ROM_CODE_VER_OFFSET];
  600. nfc_dev->nqx_info.info.fw_minor = rsp[GET_VERSION_RSP_MINOR_VERSION_OFFSET];
  601. nfc_dev->nqx_info.info.fw_major = rsp[FW_MAJOR_VER_OFFSET];
  602. }
  603. err:
  604. return chip_type;
  605. }
  606. /**
  607. * get_nfcc_session_state_dl() - gets the session state
  608. * @nfc_dev: nfc device data structure
  609. *
  610. * Performs get session command and determine
  611. * the nfcc state based on session status.
  612. *
  613. * @Return nfcc state based on session status.
  614. * NFC_STATE_FW_TEARED if sessionis not closed
  615. * NFC_STATE_FW_DWL if session closed
  616. * NFC_STATE_UNKNOWN in error cases.
  617. */
  618. enum nfc_state_flags get_nfcc_session_state_dl(struct nfc_dev *nfc_dev)
  619. {
  620. int ret = 0;
  621. uint8_t *cmd = nfc_dev->write_kbuf;
  622. uint8_t *rsp = nfc_dev->read_kbuf;
  623. enum nfc_state_flags nfc_state = NFC_STATE_UNKNOWN;
  624. *cmd++ = DL_CMD;
  625. *cmd++ = DL_GET_SESSION_STATE_CMD_PAYLOAD_LEN;
  626. *cmd++ = DL_GET_SESSION_CMD_ID;
  627. *cmd++ = DL_PAYLOAD_BYTE_ZERO;
  628. *cmd++ = DL_PAYLOAD_BYTE_ZERO;
  629. *cmd++ = DL_PAYLOAD_BYTE_ZERO;
  630. *cmd++ = DL_GET_SESSION_CMD_CRC_1;
  631. *cmd++ = DL_GET_SESSION_CMD_CRC_2;
  632. pr_debug("%s:Sending GET_SESSION_STATE cmd of size = %d\n", __func__,
  633. DL_GET_SESSION_STATE_CMD_LEN);
  634. ret = nfc_dev->nfc_write(nfc_dev, nfc_dev->write_kbuf, DL_GET_SESSION_STATE_CMD_LEN,
  635. MAX_RETRY_COUNT);
  636. if (ret <= 0) {
  637. pr_err("%s: - nfc get session cmd error ret %d\n", __func__, ret);
  638. goto err;
  639. }
  640. memset(rsp, 0x00, DL_GET_SESSION_STATE_RSP_LEN);
  641. pr_debug("%s:Reading response of GET_SESSION_STATE cmd\n", __func__);
  642. ret = nfc_dev->nfc_read(nfc_dev, rsp, DL_GET_SESSION_STATE_RSP_LEN, NCI_CMD_RSP_TIMEOUT);
  643. if (ret <= 0) {
  644. pr_err("%s: - nfc get session rsp error ret %d\n", __func__, ret);
  645. goto err;
  646. }
  647. if (rsp[0] != FW_MSG_CMD_RSP) {
  648. pr_err("%s: - nfc invalid get session state rsp\n", __func__);
  649. goto err;
  650. }
  651. pr_debug("Response bytes are %02x%02x%02x%02x%02x%02x%02x%02x\n",
  652. rsp[0], rsp[1], rsp[2], rsp[3], rsp[4], rsp[5], rsp[6], rsp[7]);
  653. /*verify fw in non-teared state */
  654. if (rsp[GET_SESSION_STS_OFF] != NFCC_SESSION_STS_CLOSED) {
  655. pr_err("%s NFCC booted in FW teared state\n", __func__);
  656. nfc_state = NFC_STATE_FW_TEARED;
  657. } else {
  658. pr_info("%s NFCC booted in FW DN mode\n", __func__);
  659. nfc_state = NFC_STATE_FW_DWL;
  660. }
  661. err:
  662. return nfc_state;
  663. }
  664. /**
  665. * get_nfcc_chip_type() - get nfcc chip type in nci mode.
  666. * @nfc_dev: nfc device data structure.
  667. *
  668. * Function to perform nci core reset and extract
  669. * chip type from the response.
  670. *
  671. * @Return: enum chip_types value
  672. *
  673. */
  674. static enum chip_types get_nfcc_chip_type(struct nfc_dev *nfc_dev)
  675. {
  676. int ret = 0;
  677. uint8_t major_version = 0;
  678. uint8_t rom_version = 0;
  679. uint8_t *cmd = nfc_dev->write_kbuf;
  680. uint8_t *rsp = nfc_dev->read_kbuf;
  681. enum chip_types chip_type = CHIP_UNKNOWN;
  682. *cmd++ = NCI_MSG_CMD;
  683. *cmd++ = NCI_CORE_RESET_CMD_OID;
  684. *cmd++ = NCI_CORE_RESET_CMD_PAYLOAD_LEN;
  685. *cmd++ = NCI_CORE_RESET_KEEP_CONFIG;
  686. pr_debug("%s:Sending NCI Core Reset cmd of size = %d\n", __func__, NCI_RESET_CMD_LEN);
  687. ret = nfc_dev->nfc_write(nfc_dev, nfc_dev->write_kbuf, NCI_RESET_CMD_LEN, NO_RETRY);
  688. if (ret <= 0) {
  689. pr_err("%s: - nfc nci core reset cmd error ret %d\n", __func__, ret);
  690. goto err;
  691. }
  692. /* to flush out debug NTF this delay is required */
  693. usleep_range(NCI_RESET_RESP_READ_DELAY, NCI_RESET_RESP_READ_DELAY + 100);
  694. nfc_dev->nfc_enable_intr(nfc_dev);
  695. memset(rsp, 0x00, NCI_RESET_RSP_LEN);
  696. pr_debug("%s:Reading NCI Core Reset rsp\n", __func__);
  697. ret = nfc_dev->nfc_read(nfc_dev, rsp, NCI_RESET_RSP_LEN, NCI_CMD_RSP_TIMEOUT);
  698. if (ret <= 0) {
  699. pr_err("%s: - nfc nci core reset rsp error ret %d\n", __func__, ret);
  700. goto err_disable_intr;
  701. }
  702. pr_debug(" %s: nci core reset response 0x%02x%02x%02x%02x\n",
  703. __func__, rsp[0], rsp[1], rsp[2], rsp[3]);
  704. if (rsp[0] != NCI_MSG_RSP) {
  705. /* reset response failed response*/
  706. pr_err("%s invalid nci core reset response\n", __func__);
  707. goto err_disable_intr;
  708. }
  709. memset(rsp, 0x00, NCI_RESET_NTF_LEN);
  710. /* read nci rest response ntf */
  711. ret = nfc_dev->nfc_read(nfc_dev, rsp, NCI_RESET_NTF_LEN, NCI_CMD_RSP_TIMEOUT);
  712. if (ret <= 0) {
  713. pr_err("%s - nfc nci rest rsp ntf error status %d\n", __func__, ret);
  714. goto err_disable_intr;
  715. }
  716. if (rsp[0] == NCI_MSG_NTF) {
  717. /* read version info from NCI Reset Notification */
  718. rom_version = rsp[NCI_HDR_LEN + rsp[NCI_PAYLOAD_LEN_IDX] - 3];
  719. major_version = rsp[NCI_HDR_LEN + rsp[NCI_PAYLOAD_LEN_IDX] - 2];
  720. /* determine chip type based on version info */
  721. if (rom_version == SN1XX_ROM_VER && major_version == SN1xx_MAJOR_VER)
  722. chip_type = CHIP_SN1XX;
  723. else if (rom_version == SN220_ROM_VER && major_version == SN220_MAJOR_VER)
  724. chip_type = CHIP_SN220;
  725. pr_debug(" %s:NCI Core Reset ntf 0x%02x%02x%02x%02x\n",
  726. __func__, rsp[0], rsp[1], rsp[2], rsp[3]);
  727. nfc_dev->nqx_info.info.chip_type = rsp[NCI_HDR_LEN + rsp[NCI_PAYLOAD_LEN_IDX] -
  728. NFC_CHIP_TYPE_OFF];
  729. nfc_dev->nqx_info.info.rom_version = rom_version;
  730. nfc_dev->nqx_info.info.fw_major = major_version;
  731. nfc_dev->nqx_info.info.fw_minor = rsp[NCI_HDR_LEN + rsp[NCI_PAYLOAD_LEN_IDX] -
  732. NFC_FW_MINOR_OFF];
  733. }
  734. err_disable_intr:
  735. nfc_dev->nfc_disable_intr(nfc_dev);
  736. err:
  737. return chip_type;
  738. }
  739. /**
  740. * validate_download_gpio() - validate download gpio.
  741. * @nfc_dev: nfc_dev device data structure.
  742. * @chip_type: chip type of the platform.
  743. *
  744. * Validates dwnld gpio should configured for supported and
  745. * should not be configured for unsupported platform.
  746. *
  747. * @Return: true if gpio validation successful ortherwise
  748. * false if validation fails.
  749. */
  750. static bool validate_download_gpio(struct nfc_dev *nfc_dev, enum chip_types chip_type)
  751. {
  752. bool status = false;
  753. struct platform_gpio *nfc_gpio;
  754. if (nfc_dev == NULL) {
  755. pr_err("%s nfc devices structure is null\n");
  756. return status;
  757. }
  758. nfc_gpio = &nfc_dev->configs.gpio;
  759. if (chip_type == CHIP_SN1XX) {
  760. /* gpio should be configured for SN1xx */
  761. status = gpio_is_valid(nfc_gpio->dwl_req);
  762. } else if (chip_type == CHIP_SN220) {
  763. /* gpio should not be configured for SN220 */
  764. set_valid_gpio(nfc_gpio->dwl_req, 0);
  765. gpio_free(nfc_gpio->dwl_req);
  766. nfc_gpio->dwl_req = -EINVAL;
  767. status = true;
  768. }
  769. return status;
  770. }
  771. /* Check for availability of NFC controller hardware */
  772. int nfcc_hw_check(struct nfc_dev *nfc_dev)
  773. {
  774. int ret = 0;
  775. enum nfc_state_flags nfc_state = NFC_STATE_UNKNOWN;
  776. enum chip_types chip_type = CHIP_UNKNOWN;
  777. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  778. /*get fw version in nci mode*/
  779. gpio_set_ven(nfc_dev, 1);
  780. gpio_set_ven(nfc_dev, 0);
  781. gpio_set_ven(nfc_dev, 1);
  782. chip_type = get_nfcc_chip_type(nfc_dev);
  783. /*get fw version in fw dwl mode*/
  784. if (chip_type == CHIP_UNKNOWN) {
  785. nfc_dev->nfc_enable_intr(nfc_dev);
  786. /*Chip is unknown, initially assume with fw dwl pin enabled*/
  787. set_valid_gpio(nfc_gpio->dwl_req, 1);
  788. gpio_set_ven(nfc_dev, 0);
  789. gpio_set_ven(nfc_dev, 1);
  790. chip_type = get_nfcc_chip_type_dl(nfc_dev);
  791. /*get the state of nfcc normal/teared in fw dwl mode*/
  792. } else {
  793. nfc_state = NFC_STATE_NCI;
  794. }
  795. /*validate gpio config required as per the chip*/
  796. if (!validate_download_gpio(nfc_dev, chip_type)) {
  797. pr_info("%s gpio validation fail\n", __func__);
  798. ret = -ENXIO;
  799. goto err;
  800. }
  801. /*check whether the NFCC is in FW DN or Teared state*/
  802. if (nfc_state != NFC_STATE_NCI)
  803. nfc_state = get_nfcc_session_state_dl(nfc_dev);
  804. /*nfcc state specific operations */
  805. switch (nfc_state) {
  806. case NFC_STATE_FW_TEARED:
  807. pr_warn("%s: - NFCC FW Teared State\n", __func__);
  808. case NFC_STATE_FW_DWL:
  809. case NFC_STATE_NCI:
  810. break;
  811. case NFC_STATE_UNKNOWN:
  812. default:
  813. ret = -ENXIO;
  814. pr_err("%s: - NFCC HW not available\n", __func__);
  815. goto err;
  816. }
  817. nfc_dev->nfc_state = nfc_state;
  818. err:
  819. nfc_dev->nfc_disable_intr(nfc_dev);
  820. set_valid_gpio(nfc_gpio->dwl_req, 0);
  821. gpio_set_ven(nfc_dev, 0);
  822. gpio_set_ven(nfc_dev, 1);
  823. nfc_dev->nfc_ven_enabled = true;
  824. return ret;
  825. }
  826. int validate_nfc_state_nci(struct nfc_dev *nfc_dev)
  827. {
  828. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  829. if (!gpio_get_value(nfc_gpio->ven)) {
  830. pr_err("VEN LOW - NFCC powered off\n");
  831. return -ENODEV;
  832. }
  833. if (get_valid_gpio(nfc_gpio->dwl_req) == 1) {
  834. pr_err("FW download in-progress\n");
  835. return -EBUSY;
  836. }
  837. if (nfc_dev->nfc_state != NFC_STATE_NCI) {
  838. pr_err("FW download state\n");
  839. return -EBUSY;
  840. }
  841. return 0;
  842. }