common.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /******************************************************************************
  2. * Copyright (C) 2015, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2019-2022 NXP
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. ******************************************************************************/
  20. /*
  21. * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  22. *
  23. *****************************************************************************/
  24. #include <linux/gpio.h>
  25. #include <linux/of_gpio.h>
  26. #include <linux/delay.h>
  27. #include <linux/pinctrl/qcom-pinctrl.h>
  28. #include "common.h"
  29. bool secure_peripheral_not_found = true;
  30. int nfc_parse_dt(struct device *dev, struct platform_configs *nfc_configs,
  31. uint8_t interface)
  32. {
  33. int ret;
  34. struct device_node *np = dev->of_node;
  35. struct platform_gpio *nfc_gpio = &nfc_configs->gpio;
  36. struct platform_ldo *ldo = &nfc_configs->ldo;
  37. if (!np) {
  38. pr_err("NxpDrv: %s: nfc of_node NULL\n", __func__);
  39. return -EINVAL;
  40. }
  41. nfc_gpio->irq = -EINVAL;
  42. nfc_gpio->dwl_req = -EINVAL;
  43. nfc_gpio->ven = -EINVAL;
  44. nfc_gpio->clkreq = -EINVAL;
  45. /* irq required for i2c based chips only */
  46. if (interface == PLATFORM_IF_I2C) {
  47. nfc_gpio->irq = of_get_named_gpio(np, DTS_IRQ_GPIO_STR, 0);
  48. if ((!gpio_is_valid(nfc_gpio->irq))) {
  49. pr_err("NxpDrv: %s: irq gpio invalid %d\n", __func__,
  50. nfc_gpio->irq);
  51. return nfc_gpio->irq;
  52. }
  53. pr_info("NxpDrv: %s: irq %d\n", __func__, nfc_gpio->irq);
  54. }
  55. nfc_gpio->ven = of_get_named_gpio(np, DTS_VEN_GPIO_STR, 0);
  56. if ((!gpio_is_valid(nfc_gpio->ven))) {
  57. pr_err("NxpDrv: %s: ven gpio invalid %d\n", __func__, nfc_gpio->ven);
  58. return nfc_gpio->ven;
  59. }
  60. /* some products like sn220 does not required fw dwl pin */
  61. nfc_gpio->dwl_req = of_get_named_gpio(np, DTS_FWDN_GPIO_STR, 0);
  62. /* not returning failure for dwl gpio as it is optional for sn220 */
  63. if ((!gpio_is_valid(nfc_gpio->dwl_req))) {
  64. pr_warn("NxpDrv: %s: dwl_req gpio invalid %d\n", __func__,
  65. nfc_gpio->dwl_req);
  66. }
  67. /* Read clock request gpio configuration if MGPIO configurations are not preasent */
  68. if (of_property_read_string(np, DTS_CLKSRC_GPIO_STR, &nfc_configs->clk_src_name)) {
  69. nfc_configs->clk_pin_voting = false;
  70. } else
  71. nfc_configs->clk_pin_voting = true;
  72. /* Read clkreq GPIO pin number from DTSI */
  73. nfc_gpio->clkreq = of_get_named_gpio(np, DTS_CLKREQ_GPIO_STR, 0);
  74. if (!gpio_is_valid(nfc_gpio->clkreq)) {
  75. dev_err(dev, "NxpDrv: clkreq gpio invalid %d\n", nfc_gpio->clkreq);
  76. return -EINVAL;
  77. }
  78. #ifdef NFC_SECURE_PERIPHERAL_ENABLED
  79. /* Read DTS_SZONE_STR to check secure zone support */
  80. if (of_property_read_string(np, DTS_SZONE_STR, &nfc_configs->szone)) {
  81. nfc_configs->CNSS_NFC_HW_SECURE_ENABLE = false;
  82. }else
  83. nfc_configs->CNSS_NFC_HW_SECURE_ENABLE = true;
  84. #endif
  85. pr_info("NxpDrv: %s: irq %d, ven %d, dwl %d, clkreq %d \n", __func__, nfc_gpio->irq, nfc_gpio->ven,
  86. nfc_gpio->dwl_req, nfc_gpio->clkreq);
  87. /* optional property */
  88. ret = of_property_read_u32_array(np, NFC_LDO_VOL_DT_NAME,
  89. (u32 *) ldo->vdd_levels,
  90. ARRAY_SIZE(ldo->vdd_levels));
  91. if (ret) {
  92. dev_err(dev, "NxpDrv: error reading NFC VDDIO min and max value\n");
  93. // set default as per datasheet
  94. ldo->vdd_levels[0] = NFC_VDDIO_MIN;
  95. ldo->vdd_levels[1] = NFC_VDDIO_MAX;
  96. }
  97. /* optional property */
  98. ret = of_property_read_u32(np, NFC_LDO_CUR_DT_NAME, &ldo->max_current);
  99. if (ret) {
  100. dev_err(dev, "NxpDrv: error reading NFC current value\n");
  101. // set default as per datasheet
  102. ldo->max_current = NFC_CURRENT_MAX;
  103. }
  104. return 0;
  105. }
  106. void set_valid_gpio(int gpio, int value)
  107. {
  108. if (gpio_is_valid(gpio)) {
  109. pr_debug("NxpDrv: %s: gpio %d value %d\n", __func__, gpio, value);
  110. gpio_set_value(gpio, value);
  111. /* hardware dependent delay */
  112. usleep_range(NFC_GPIO_SET_WAIT_TIME_US,
  113. NFC_GPIO_SET_WAIT_TIME_US + 100);
  114. }
  115. }
  116. int get_valid_gpio(int gpio)
  117. {
  118. int value = -EINVAL;
  119. if (gpio_is_valid(gpio)) {
  120. value = gpio_get_value(gpio);
  121. pr_debug("NxpDrv: %s: gpio %d value %d\n", __func__, gpio, value);
  122. }
  123. return value;
  124. }
  125. void gpio_set_ven(struct nfc_dev *nfc_dev, int value)
  126. {
  127. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  128. if (gpio_get_value(nfc_gpio->ven) != value) {
  129. pr_debug("NxpDrv: %s: value %d\n", __func__, value);
  130. #ifdef NFC_SECURE_PERIPHERAL_ENABLED
  131. if(secure_peripheral_not_found)
  132. {
  133. /*secure peripheral feature is not enabled*/
  134. gpio_set_value(nfc_gpio->ven, value);
  135. }
  136. else
  137. {
  138. /*secure peripheral feature is enabled*/
  139. if(!nfc_hw_secure_check())
  140. gpio_set_value(nfc_gpio->ven, value);
  141. }
  142. #else
  143. gpio_set_value(nfc_gpio->ven, value);
  144. #endif
  145. /* hardware dependent delay */
  146. usleep_range(NFC_GPIO_SET_WAIT_TIME_US,
  147. NFC_GPIO_SET_WAIT_TIME_US + 100);
  148. }
  149. }
  150. int configure_gpio(unsigned int gpio, int flag)
  151. {
  152. int ret;
  153. pr_debug("NxpDrv: %s: nfc gpio [%d] flag [%01x]\n", __func__, gpio, flag);
  154. if (gpio_is_valid(gpio)) {
  155. ret = gpio_request(gpio, "nfc_gpio");
  156. if (ret) {
  157. pr_err("NxpDrv: %s: unable to request nfc gpio [%d]\n",
  158. __func__, gpio);
  159. return ret;
  160. }
  161. /* set direction and value for output pin */
  162. if (flag & GPIO_OUTPUT) {
  163. ret = gpio_direction_output(gpio, (GPIO_HIGH & flag));
  164. pr_debug("NxpDrv: %s: nfc o/p gpio %d level %d\n", __func__,
  165. gpio, gpio_get_value(gpio));
  166. } else {
  167. ret = gpio_direction_input(gpio);
  168. pr_debug("NxpDrv: %s: nfc i/p gpio %d\n", __func__, gpio);
  169. }
  170. if (ret) {
  171. pr_err("NxpDrv: %s: unable to set direction for nfc gpio [%d]\n", __func__, gpio);
  172. gpio_free(gpio);
  173. return ret;
  174. }
  175. /* Consider value as control for input IRQ pin */
  176. if (flag & GPIO_IRQ) {
  177. ret = gpio_to_irq(gpio);
  178. if (ret < 0) {
  179. pr_err("NxpDrv: %s: unable to set irq [%d]\n", __func__,
  180. gpio);
  181. gpio_free(gpio);
  182. return ret;
  183. }
  184. pr_debug("NxpDrv: %s: gpio_to_irq successful [%d]\n", __func__,
  185. gpio);
  186. return ret;
  187. }
  188. } else {
  189. pr_err("NxpDrv: %s: invalid gpio\n", __func__);
  190. ret = -EINVAL;
  191. }
  192. return ret;
  193. }
  194. void gpio_free_all(struct nfc_dev *nfc_dev)
  195. {
  196. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  197. if (gpio_is_valid(nfc_gpio->clkreq))
  198. gpio_free(nfc_gpio->clkreq);
  199. if (gpio_is_valid(nfc_gpio->dwl_req))
  200. gpio_free(nfc_gpio->dwl_req);
  201. if (gpio_is_valid(nfc_gpio->irq))
  202. gpio_free(nfc_gpio->irq);
  203. if (gpio_is_valid(nfc_gpio->ven))
  204. gpio_free(nfc_gpio->ven);
  205. }
  206. void nfc_misc_unregister(struct nfc_dev *nfc_dev, int count)
  207. {
  208. pr_debug("NxpDrv: %s: entry\n", __func__);
  209. kfree(nfc_dev->kbuf);
  210. device_destroy(nfc_dev->nfc_class, nfc_dev->devno);
  211. cdev_del(&nfc_dev->c_dev);
  212. class_destroy(nfc_dev->nfc_class);
  213. unregister_chrdev_region(nfc_dev->devno, count);
  214. if (nfc_dev->ipcl)
  215. ipc_log_context_destroy(nfc_dev->ipcl);
  216. }
  217. int nfc_misc_register(struct nfc_dev *nfc_dev,
  218. const struct file_operations *nfc_fops, int count,
  219. char *devname, char *classname)
  220. {
  221. int ret = 0;
  222. ret = alloc_chrdev_region(&nfc_dev->devno, 0, count, devname);
  223. if (ret < 0) {
  224. pr_err("NxpDrv: %s: failed to alloc chrdev region ret %d\n", __func__,
  225. ret);
  226. return ret;
  227. }
  228. nfc_dev->nfc_class = class_create(THIS_MODULE, classname);
  229. if (IS_ERR(nfc_dev->nfc_class)) {
  230. ret = PTR_ERR(nfc_dev->nfc_class);
  231. pr_err("NxpDrv: %s: failed to register device class ret %d\n", __func__,
  232. ret);
  233. unregister_chrdev_region(nfc_dev->devno, count);
  234. return ret;
  235. }
  236. cdev_init(&nfc_dev->c_dev, nfc_fops);
  237. ret = cdev_add(&nfc_dev->c_dev, nfc_dev->devno, count);
  238. if (ret < 0) {
  239. pr_err("NxpDrv: %s: failed to add cdev ret %d\n", __func__, ret);
  240. class_destroy(nfc_dev->nfc_class);
  241. unregister_chrdev_region(nfc_dev->devno, count);
  242. return ret;
  243. }
  244. nfc_dev->nfc_device = device_create(nfc_dev->nfc_class, NULL,
  245. nfc_dev->devno, nfc_dev, devname);
  246. if (IS_ERR(nfc_dev->nfc_device)) {
  247. ret = PTR_ERR(nfc_dev->nfc_device);
  248. pr_err("NxpDrv: %s: failed to create the device ret %d\n", __func__,
  249. ret);
  250. cdev_del(&nfc_dev->c_dev);
  251. class_destroy(nfc_dev->nfc_class);
  252. unregister_chrdev_region(nfc_dev->devno, count);
  253. return ret;
  254. }
  255. nfc_dev->ipcl = ipc_log_context_create(NUM_OF_IPC_LOG_PAGES,
  256. dev_name(nfc_dev->nfc_device), 0);
  257. nfc_dev->kbuflen = MAX_NCI_BUFFER_SIZE;
  258. nfc_dev->kbuf = kzalloc(MAX_NCI_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
  259. if (!nfc_dev->kbuf) {
  260. nfc_misc_unregister(nfc_dev, count);
  261. return -ENOMEM;
  262. }
  263. nfc_dev->cold_reset.rsp_pending = false;
  264. nfc_dev->cold_reset.is_nfc_enabled = false;
  265. nfc_dev->cold_reset.is_crp_en = false;
  266. nfc_dev->cold_reset.last_src_ese_prot = ESE_COLD_RESET_ORIGIN_NONE;
  267. init_waitqueue_head(&nfc_dev->cold_reset.read_wq);
  268. return 0;
  269. }
  270. /**
  271. * nfc_gpio_info() - gets the status of nfc gpio pins and encodes into a byte.
  272. * @nfc_dev: nfc device data structure
  273. * @arg: userspace buffer
  274. *
  275. * Encoding can be done in following manner
  276. * 1) map the gpio value into INVALID(-2), SET(1), RESET(0).
  277. * 2) mask the first 2 bits of gpio.
  278. * 3) left shift the 2 bits as multiple of 2.
  279. * 4) multiply factor can be defined as position of gpio pin in struct platform_gpio
  280. *
  281. * Return: -EFAULT, if unable to copy the data from kernel space to userspace, 0
  282. * if Success(or no issue)
  283. */
  284. static int nfc_gpio_info(struct nfc_dev *nfc_dev, unsigned long arg)
  285. {
  286. unsigned int gpios_status = 0;
  287. int value = 0;
  288. int gpio_no = 0;
  289. int i;
  290. int ret = 0;
  291. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  292. for (i = 0; i < sizeof(struct platform_gpio) / sizeof(unsigned int);
  293. i++) {
  294. gpio_no = *((unsigned int *)nfc_gpio + i);
  295. value = get_valid_gpio(gpio_no);
  296. if (value < 0)
  297. value = -2;
  298. gpios_status |= (value & GPIO_STATUS_MASK_BITS)<<(GPIO_POS_SHIFT_VAL*i);
  299. }
  300. ret = copy_to_user((uint32_t *) arg, &gpios_status, sizeof(value));
  301. if (ret < 0) {
  302. pr_err("NxpDrv: %s : Unable to copy data from kernel space to user space", __func__);
  303. return -EFAULT;
  304. }
  305. return 0;
  306. }
  307. /**
  308. * nfc_ioctl_power_states() - power control
  309. * @nfc_dev: nfc device data structure
  310. * @arg: mode that we want to move to
  311. *
  312. * Device power control. Depending on the arg value, device moves to
  313. * different states, refer common.h for args
  314. *
  315. * Return: -ENOIOCTLCMD if arg is not supported, 0 if Success(or no issue)
  316. * and error ret code otherwise
  317. */
  318. static int nfc_ioctl_power_states(struct nfc_dev *nfc_dev, unsigned long arg)
  319. {
  320. int ret = 0;
  321. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  322. if (arg == NFC_POWER_OFF) {
  323. /*
  324. * We are attempting a hardware reset so let us disable
  325. * interrupts to avoid spurious notifications to upper
  326. * layers.
  327. */
  328. nfc_dev->nfc_disable_intr(nfc_dev);
  329. set_valid_gpio(nfc_gpio->dwl_req, 0);
  330. gpio_set_ven(nfc_dev, 0);
  331. nfc_dev->nfc_ven_enabled = false;
  332. nfc_dev->nfc_state = NFC_STATE_NCI;
  333. } else if (arg == NFC_POWER_ON) {
  334. nfc_dev->nfc_enable_intr(nfc_dev);
  335. set_valid_gpio(nfc_gpio->dwl_req, 0);
  336. gpio_set_ven(nfc_dev, 1);
  337. nfc_dev->nfc_ven_enabled = true;
  338. nfc_dev->nfc_state = NFC_STATE_NCI;
  339. } else if (arg == NFC_FW_DWL_VEN_TOGGLE) {
  340. /*
  341. * We are switching to download Mode, toggle the enable pin
  342. * in order to set the NFCC in the new mode
  343. */
  344. nfc_dev->nfc_disable_intr(nfc_dev);
  345. set_valid_gpio(nfc_gpio->dwl_req, 1);
  346. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  347. gpio_set_ven(nfc_dev, 0);
  348. gpio_set_ven(nfc_dev, 1);
  349. nfc_dev->nfc_enable_intr(nfc_dev);
  350. } else if (arg == NFC_FW_DWL_HIGH) {
  351. /*
  352. * Setting firmware download gpio to HIGH
  353. * before FW download start
  354. */
  355. pr_debug("NxpDrv: set fw gpio high\n");
  356. set_valid_gpio(nfc_gpio->dwl_req, 1);
  357. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  358. } else if (arg == NFC_VEN_FORCED_HARD_RESET) {
  359. nfc_dev->nfc_disable_intr(nfc_dev);
  360. gpio_set_ven(nfc_dev, 0);
  361. gpio_set_ven(nfc_dev, 1);
  362. nfc_dev->nfc_enable_intr(nfc_dev);
  363. pr_info("NxpDrv: %s VEN forced reset done\n", __func__);
  364. } else if (arg == NFC_FW_DWL_LOW) {
  365. /*
  366. * Setting firmware download gpio to LOW
  367. * FW download finished
  368. */
  369. pr_debug("NxpDrv: set fw gpio LOW\n");
  370. set_valid_gpio(nfc_gpio->dwl_req, 0);
  371. nfc_dev->nfc_state = NFC_STATE_NCI;
  372. } else if (arg == NFC_ENABLE) {
  373. if (nfc_dev->configs.clk_pin_voting) {
  374. /* Enabling nfc clock */
  375. ret = nfc_clock_select(nfc_dev);
  376. if (ret)
  377. pr_err("%s unable to select clock\n", __func__);
  378. }
  379. /* Setting flag true when NFC is enabled */
  380. nfc_dev->cold_reset.is_nfc_enabled = true;
  381. } else if (arg == NFC_DISABLE) {
  382. if (nfc_dev->configs.clk_pin_voting) {
  383. /* Disabling nfc clock */
  384. ret = nfc_clock_deselect(nfc_dev);
  385. if (ret)
  386. pr_err("%s unable to disable clock\n", __func__);
  387. }
  388. /* Setting flag true when NFC is disabled */
  389. nfc_dev->cold_reset.is_nfc_enabled = false;
  390. } else {
  391. pr_err("NxpDrv: %s: bad arg %lu\n", __func__, arg);
  392. ret = -ENOIOCTLCMD;
  393. }
  394. return ret;
  395. }
  396. #ifdef CONFIG_COMPAT
  397. /**
  398. * nfc_dev_compat_ioctl - used to set or get data from upper layer.
  399. * @pfile file node for opened device.
  400. * @cmd ioctl type from upper layer.
  401. * @arg ioctl arg from upper layer.
  402. *
  403. * NFC and ESE Device power control, based on the argument value
  404. *
  405. * Return: -ENOIOCTLCMD if arg is not supported
  406. * 0 if Success(or no issue)
  407. * 0 or 1 in case of arg is ESE_GET_PWR/ESE_POWER_STATE
  408. * and error ret code otherwise
  409. */
  410. long nfc_dev_compat_ioctl(struct file *pfile, unsigned int cmd,
  411. unsigned long arg)
  412. {
  413. int ret = 0;
  414. arg = (compat_u64) arg;
  415. pr_debug("NxpDrv: %s: cmd = %x arg = %zx\n", __func__, cmd, arg);
  416. ret = nfc_dev_ioctl(pfile, cmd, arg);
  417. return ret;
  418. }
  419. #endif
  420. /**
  421. * nfc_post_init() - Configuraing Ven GPIO and hardware check
  422. * @nfc_dev: nfc device data structure
  423. *
  424. * Configure GPIOs post notification from TZ, ensuring it's a non-secure zone.
  425. *
  426. * Return: 0 if Success(or no issue) and error ret code otherwise
  427. */
  428. int nfc_post_init(struct nfc_dev *nfc_dev)
  429. {
  430. int ret=0;
  431. unsigned int clkreq_gpio = 0;
  432. static int post_init_success;
  433. struct platform_configs nfc_configs;
  434. struct platform_gpio *nfc_gpio;
  435. if(post_init_success)
  436. return 0;
  437. if (!nfc_dev)
  438. return -ENODEV;
  439. memcpy(&nfc_configs, &nfc_dev->configs, sizeof(struct platform_configs));
  440. nfc_gpio = &nfc_configs.gpio;
  441. ret = configure_gpio(nfc_gpio->ven, GPIO_OUTPUT);
  442. if (ret) {
  443. pr_err("NxpDrv: %s: unable to request nfc reset gpio [%d]\n",
  444. __func__, nfc_gpio->ven);
  445. return ret;
  446. }
  447. ret = configure_gpio(nfc_gpio->dwl_req, GPIO_OUTPUT);
  448. if (ret) {
  449. pr_err("NxpDrv: %s: unable to request nfc firm downl gpio [%d]\n",
  450. __func__, nfc_gpio->dwl_req);
  451. }
  452. ret = configure_gpio(nfc_gpio->clkreq, GPIO_INPUT);
  453. if (ret) {
  454. pr_err("NxpDrv: %s: unable to request nfc clkreq gpio [%d]\n",
  455. __func__, nfc_gpio->clkreq);
  456. }
  457. /* Read clkreq GPIO number from device tree*/
  458. ret = of_property_read_u32_index(nfc_dev->i2c_dev.client->dev.of_node,
  459. DTS_CLKREQ_GPIO_STR, 1, &clkreq_gpio);
  460. if (ret < 0) {
  461. pr_err("NxpDrv: %s Failed to read clkreq gipo number, ret: %d\n",
  462. __func__, ret);
  463. return ret;
  464. }
  465. /* configure clkreq GPIO as wakeup capable */
  466. ret = msm_gpio_mpm_wake_set(clkreq_gpio, true);
  467. if (ret < 0) {
  468. pr_err("NxpDrv: %s clkreq gpio %d as wakeup capable failed, ret: %d\n",
  469. __func__, clkreq_gpio, ret);
  470. return ret;
  471. }
  472. ret = nfcc_hw_check(nfc_dev);
  473. if (ret || nfc_dev->nfc_state == NFC_STATE_UNKNOWN) {
  474. pr_err("NxpDrv: nfc hw check failed ret %d\n", ret);
  475. gpio_free(nfc_gpio->dwl_req);
  476. gpio_free(nfc_gpio->ven);
  477. return ret;
  478. }
  479. #ifdef NFC_SECURE_PERIPHERAL_ENABLED
  480. /*Initialising sempahore to disbale NFC Ven GPIO only after eSE is power off flag is set */
  481. if (nfc_dev->configs.CNSS_NFC_HW_SECURE_ENABLE == true) {
  482. sema_init(&sem_eSE_pwr_off,0);
  483. }
  484. #endif
  485. post_init_success = 1;
  486. pr_info("NxpDrv: %s success\n", __func__);
  487. return 0;
  488. }
  489. #ifdef NFC_SECURE_PERIPHERAL_ENABLED
  490. /**
  491. * nfc_hw_secure_check() - Checks the NFC secure zone status
  492. *
  493. * Queries the TZ secure libraries if NFC is in secure zone statue or not.
  494. *
  495. * Return: 0 if FEATURE_NOT_SUPPORTED or PERIPHERAL_NOT_FOUND or nfc_sec_state = 2(non-secure zone) and
  496. * return 1 if nfc_sec_state = 1(secure zone) or error otherwise
  497. */
  498. bool nfc_hw_secure_check(void)
  499. {
  500. struct Object client_env;
  501. struct Object app_object;
  502. u32 nfc_uid = HW_NFC_UID;
  503. union ObjectArg obj_arg[2] = {{{0, 0}}};
  504. int ret;
  505. bool retstat = 1;
  506. u8 nfc_sec_state = 0;
  507. /* get rootObj */
  508. ret = get_client_env_object(&client_env);
  509. if (ret) {
  510. pr_err("NxpDrv: Failed to get client_env_object, ret: %d\n", ret);
  511. return retstat;
  512. }
  513. ret = IClientEnv_open(client_env, HW_STATE_UID, &app_object);
  514. if (ret) {
  515. pr_debug("NxpDrv: Failed to get app_object, ret: %d\n", ret);
  516. if (ret == FEATURE_NOT_SUPPORTED) {
  517. retstat = 0; /* Do not Assert */
  518. pr_debug("NxpDrv: Secure HW feature not supported\n");
  519. }
  520. goto exit_release_clientenv;
  521. }
  522. obj_arg[0].b = (struct ObjectBuf) {&nfc_uid, sizeof(u32)};
  523. obj_arg[1].b = (struct ObjectBuf) {&nfc_sec_state, sizeof(u8)};
  524. ret = Object_invoke(app_object, HW_OP_GET_STATE, obj_arg,
  525. ObjectCounts_pack(1, 1, 0, 0));
  526. pr_info("NxpDrv: TZ ret: %d nfc_sec_state: %d\n", ret, nfc_sec_state);
  527. if (ret) {
  528. if (ret == PERIPHERAL_NOT_FOUND) {
  529. retstat = 0; /* Do not Assert */
  530. pr_debug("NxpDrv: Secure HW mode is not updated. Peripheral not found\n");
  531. }
  532. goto exit_release_app_obj;
  533. }
  534. secure_peripheral_not_found = false;
  535. /* Refer peripheral state utilities for different states of NFC peripherals */
  536. if (nfc_sec_state == 1) {
  537. /*Secure Zone*/
  538. retstat = 1;
  539. } else {
  540. /*Non-Secure Zone*/
  541. retstat = 0;
  542. }
  543. exit_release_app_obj:
  544. Object_release(app_object);
  545. exit_release_clientenv:
  546. Object_release(client_env);
  547. return retstat;
  548. }
  549. /**
  550. * nfc_dynamic_protection_ioctl() - dynamic protection control
  551. * @nfc_dev: nfc device data structure
  552. * @sec_zone_trans: mode that we want to move to
  553. * If sec_zone_trans = 1; transition from non-secure zone to secure zone
  554. * If sec_zone_trans = 0; transition from secure zone to non - secure zone
  555. *
  556. * nfc periheral dynamic protection control. Depending on the sec_zone_trans value, device moves to
  557. * secure zone and non-secure zone
  558. *
  559. * Return: -ENOIOCTLCMD if sec_zone_trans val is not supported, 0 if Success(or no issue)
  560. * and error ret code otherwise
  561. */
  562. int nfc_dynamic_protection_ioctl(struct nfc_dev *nfc_dev, unsigned long sec_zone_trans)
  563. {
  564. int ret = 0;
  565. static int init_flag=1;
  566. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  567. if(sec_zone_trans == 1) {
  568. /*check NFC is disabled, only then set Ven GPIO low*/
  569. if(nfc_dev->cold_reset.is_nfc_enabled == false) {
  570. pr_debug("NxpDrv: %s: value %d\n", __func__, gpio_get_value(nfc_gpio->ven));
  571. chk_eSE_pwr_off = 1;
  572. /*check if eSE is active, if yes, wait max of 1sec, until it's inactive */
  573. if(nfc_dev->is_ese_session_active == true) {
  574. if(down_timeout(&sem_eSE_pwr_off, msecs_to_jiffies(1000))) {
  575. /*waited for 1sec yet eSE not turned off, so, ignoring eSE power off*/
  576. pr_info("NxpDrv: Forcefull shutdown of eSE\n");
  577. }
  578. }
  579. ret = nfc_ioctl_power_states(nfc_dev, 0);
  580. /*set driver as secure zone, such that no ioctl calls are allowed*/
  581. nfc_dev->secure_zone = true;
  582. pr_info("NxpDrv: Driver Secure flag set successful\n");
  583. } else {
  584. ret = -1;
  585. }
  586. }
  587. else if(sec_zone_trans == 0) {
  588. chk_eSE_pwr_off = 0;
  589. nfc_dev->secure_zone = false;
  590. if(init_flag) {
  591. /*Initialize once,only during the first non-secure entry*/
  592. ret = nfc_post_init(nfc_dev);
  593. if(ret == 0)
  594. init_flag=0;
  595. }
  596. else {
  597. if(!gpio_get_value(nfc_gpio->ven))
  598. ret = nfc_ioctl_power_states(nfc_dev, 1);
  599. }
  600. pr_info("NxpDrv: Func Driver Secure flag clear successful\n");
  601. } else {
  602. pr_info("NxpDrv: INVALID ARG\n");
  603. ret = -ENOIOCTLCMD;
  604. }
  605. return ret;
  606. }
  607. #endif
  608. /**
  609. * nfc_dev_ioctl - used to set or get data from upper layer.
  610. * @pfile file node for opened device.
  611. * @cmd ioctl type from upper layer.
  612. * @arg ioctl arg from upper layer.
  613. *
  614. * NFC and ESE Device power control, based on the argument value
  615. *
  616. * Return: -ENOIOCTLCMD if arg is not supported
  617. * 0 if Success(or no issue)
  618. * 0 or 1 in case of arg is ESE_GET_PWR/ESE_POWER_STATE
  619. * and error ret code otherwise
  620. */
  621. long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
  622. {
  623. int ret = 0;
  624. struct nfc_dev *nfc_dev = pfile->private_data;
  625. if (!nfc_dev)
  626. return -ENODEV;
  627. #ifdef NFC_SECURE_PERIPHERAL_ENABLED
  628. if( nfc_dev->configs.CNSS_NFC_HW_SECURE_ENABLE == true) {
  629. /*Avoiding ioctl call in secure zone*/
  630. if(nfc_dev->secure_zone) {
  631. if(cmd!=NFC_SECURE_ZONE) {
  632. pr_debug("NxpDrv: nfc_dev_ioctl failed\n");
  633. return -1;
  634. }
  635. }
  636. }
  637. #endif
  638. pr_debug("NxpDrv: %s: cmd = %x arg = %zx\n", __func__, cmd, arg);
  639. switch (cmd) {
  640. case NFC_SET_PWR:
  641. ret = nfc_ioctl_power_states(nfc_dev, arg);
  642. break;
  643. case NFC_SET_RESET_READ_PENDING:
  644. if (arg == NFC_SET_READ_PENDING) {
  645. nfc_dev->cold_reset.is_nfc_read_pending = true;
  646. /* Set default NFC state as NCI for Nfc read pending request */
  647. nfc_dev->nfc_state = NFC_STATE_NCI;
  648. } else if (arg == NFC_RESET_READ_PENDING) {
  649. nfc_dev->cold_reset.is_nfc_read_pending = false;
  650. } else {
  651. ret = -EINVAL;
  652. }
  653. break;
  654. case ESE_SET_PWR:
  655. ret = nfc_ese_pwr(nfc_dev, arg);
  656. break;
  657. case ESE_GET_PWR:
  658. ret = nfc_ese_pwr(nfc_dev, ESE_POWER_STATE);
  659. break;
  660. case NFC_GET_GPIO_STATUS:
  661. ret = nfc_gpio_info(nfc_dev, arg);
  662. break;
  663. case NFCC_GET_INFO:
  664. ret = nfc_ioctl_nfcc_info(pfile, arg);
  665. break;
  666. case ESE_COLD_RESET:
  667. pr_debug("NxpDrv: nfc ese cold reset ioctl\n");
  668. ret = ese_cold_reset_ioctl(nfc_dev, arg);
  669. break;
  670. #ifdef NFC_SECURE_PERIPHERAL_ENABLED
  671. case NFC_SECURE_ZONE:
  672. if( nfc_dev->configs.CNSS_NFC_HW_SECURE_ENABLE == true) {
  673. ret = nfc_dynamic_protection_ioctl(nfc_dev, arg);
  674. }
  675. break;
  676. #endif
  677. default:
  678. pr_err("NxpDrv: %s: bad cmd %lu\n", __func__, arg);
  679. ret = -ENOIOCTLCMD;
  680. }
  681. return ret;
  682. }
  683. int nfc_dev_open(struct inode *inode, struct file *filp)
  684. {
  685. struct nfc_dev *nfc_dev = NULL;
  686. nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
  687. if (!nfc_dev)
  688. return -ENODEV;
  689. pr_debug("NxpDrv: %s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  690. /* Set flag to block freezer fake signal if not set already.
  691. * Without this Signal being set, Driver is trying to do a read
  692. * which is causing the delay in moving to Hibernate Mode.
  693. */
  694. if (!(current->flags & PF_NOFREEZE)) {
  695. current->flags |= PF_NOFREEZE;
  696. pr_debug("NxpDrv: %s: current->flags 0x%x. \n", __func__, current->flags);
  697. }
  698. mutex_lock(&nfc_dev->dev_ref_mutex);
  699. filp->private_data = nfc_dev;
  700. if (nfc_dev->dev_ref_count == 0) {
  701. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  702. nfc_dev->nfc_enable_intr(nfc_dev);
  703. }
  704. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count + 1;
  705. mutex_unlock(&nfc_dev->dev_ref_mutex);
  706. return 0;
  707. }
  708. int nfc_dev_flush(struct file *pfile, fl_owner_t id)
  709. {
  710. struct nfc_dev *nfc_dev = pfile->private_data;
  711. if (!nfc_dev)
  712. return -ENODEV;
  713. /*
  714. * release blocked user thread waiting for pending read during close
  715. */
  716. if (!mutex_trylock(&nfc_dev->read_mutex)) {
  717. nfc_dev->release_read = true;
  718. nfc_dev->nfc_disable_intr(nfc_dev);
  719. wake_up(&nfc_dev->read_wq);
  720. pr_debug("NxpDrv: %s: waiting for release of blocked read\n", __func__);
  721. mutex_lock(&nfc_dev->read_mutex);
  722. nfc_dev->release_read = false;
  723. } else {
  724. pr_debug("NxpDrv: %s: read thread already released\n", __func__);
  725. }
  726. mutex_unlock(&nfc_dev->read_mutex);
  727. return 0;
  728. }
  729. int nfc_dev_close(struct inode *inode, struct file *filp)
  730. {
  731. struct nfc_dev *nfc_dev = NULL;
  732. nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
  733. if (!nfc_dev)
  734. return -ENODEV;
  735. pr_debug("NxpDrv: %s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  736. /* unset the flag to restore to previous state */
  737. if (current->flags & PF_NOFREEZE) {
  738. current->flags &= ~PF_NOFREEZE;
  739. pr_debug("NxpDrv: %s: current->flags 0x%x. \n", __func__, current->flags);
  740. }
  741. mutex_lock(&nfc_dev->dev_ref_mutex);
  742. if (nfc_dev->dev_ref_count == 1) {
  743. nfc_dev->nfc_disable_intr(nfc_dev);
  744. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  745. }
  746. if (nfc_dev->dev_ref_count > 0)
  747. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count - 1;
  748. filp->private_data = NULL;
  749. mutex_unlock(&nfc_dev->dev_ref_mutex);
  750. return 0;
  751. }
  752. int validate_nfc_state_nci(struct nfc_dev *nfc_dev)
  753. {
  754. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  755. if(!nfc_dev->secure_zone) {
  756. if (!gpio_get_value(nfc_gpio->ven)) {
  757. pr_err("NxpDrv: %s: ven low - nfcc powered off\n", __func__);
  758. return -ENODEV;
  759. }
  760. }
  761. if (get_valid_gpio(nfc_gpio->dwl_req) == 1) {
  762. pr_err("NxpDrv: %s: fw download in-progress\n", __func__);
  763. return -EBUSY;
  764. }
  765. if (nfc_dev->nfc_state != NFC_STATE_NCI) {
  766. pr_err("NxpDrv: %s: fw download state\n", __func__);
  767. return -EBUSY;
  768. }
  769. return 0;
  770. }