common.c 24 KB

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