common.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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. /* Read clkreq GPIO number from device tree*/
  453. ret = of_property_read_u32_index(nfc_dev->i2c_dev.client->dev.of_node,
  454. DTS_CLKREQ_GPIO_STR, 1, &clkreq_gpio);
  455. if (ret < 0) {
  456. pr_err("NxpDrv: %s Failed to read clkreq gipo number, ret: %d\n",
  457. __func__, ret);
  458. return ret;
  459. }
  460. /* configure clkreq GPIO as wakeup capable */
  461. ret = msm_gpio_mpm_wake_set(clkreq_gpio, true);
  462. if (ret < 0) {
  463. pr_err("NxpDrv: %s clkreq gpio %d as wakeup capable failed, ret: %d\n",
  464. __func__, clkreq_gpio, ret);
  465. return ret;
  466. }
  467. ret = nfcc_hw_check(nfc_dev);
  468. if (ret || nfc_dev->nfc_state == NFC_STATE_UNKNOWN) {
  469. pr_err("NxpDrv: nfc hw check failed ret %d\n", ret);
  470. gpio_free(nfc_gpio->dwl_req);
  471. gpio_free(nfc_gpio->ven);
  472. return ret;
  473. }
  474. #ifdef NFC_SECURE_PERIPHERAL_ENABLED
  475. /*Initialising sempahore to disbale NFC Ven GPIO only after eSE is power off flag is set */
  476. if (nfc_dev->configs.CNSS_NFC_HW_SECURE_ENABLE == true) {
  477. sema_init(&sem_eSE_pwr_off,0);
  478. }
  479. #endif
  480. post_init_success = 1;
  481. pr_info("NxpDrv: %s success\n", __func__);
  482. return 0;
  483. }
  484. #ifdef NFC_SECURE_PERIPHERAL_ENABLED
  485. /**
  486. * nfc_hw_secure_check() - Checks the NFC secure zone status
  487. *
  488. * Queries the TZ secure libraries if NFC is in secure zone statue or not.
  489. *
  490. * Return: 0 if FEATURE_NOT_SUPPORTED or PERIPHERAL_NOT_FOUND or nfc_sec_state = 2(non-secure zone) and
  491. * return 1 if nfc_sec_state = 1(secure zone) or error otherwise
  492. */
  493. bool nfc_hw_secure_check(void)
  494. {
  495. struct Object client_env;
  496. struct Object app_object;
  497. u32 nfc_uid = HW_NFC_UID;
  498. union ObjectArg obj_arg[2] = {{{0, 0}}};
  499. int ret;
  500. bool retstat = 1;
  501. u8 nfc_sec_state = 0;
  502. /* get rootObj */
  503. ret = get_client_env_object(&client_env);
  504. if (ret) {
  505. pr_err("NxpDrv: Failed to get client_env_object, ret: %d\n", ret);
  506. return retstat;
  507. }
  508. ret = IClientEnv_open(client_env, HW_STATE_UID, &app_object);
  509. if (ret) {
  510. pr_debug("NxpDrv: Failed to get app_object, ret: %d\n", ret);
  511. if (ret == FEATURE_NOT_SUPPORTED) {
  512. retstat = 0; /* Do not Assert */
  513. pr_debug("NxpDrv: Secure HW feature not supported\n");
  514. }
  515. goto exit_release_clientenv;
  516. }
  517. obj_arg[0].b = (struct ObjectBuf) {&nfc_uid, sizeof(u32)};
  518. obj_arg[1].b = (struct ObjectBuf) {&nfc_sec_state, sizeof(u8)};
  519. ret = Object_invoke(app_object, HW_OP_GET_STATE, obj_arg,
  520. ObjectCounts_pack(1, 1, 0, 0));
  521. pr_info("NxpDrv: TZ ret: %d nfc_sec_state: %d\n", ret, nfc_sec_state);
  522. if (ret) {
  523. if (ret == PERIPHERAL_NOT_FOUND) {
  524. retstat = 0; /* Do not Assert */
  525. pr_debug("NxpDrv: Secure HW mode is not updated. Peripheral not found\n");
  526. }
  527. goto exit_release_app_obj;
  528. }
  529. secure_peripheral_not_found = false;
  530. /* Refer peripheral state utilities for different states of NFC peripherals */
  531. if (nfc_sec_state == 1) {
  532. /*Secure Zone*/
  533. retstat = 1;
  534. } else {
  535. /*Non-Secure Zone*/
  536. retstat = 0;
  537. }
  538. exit_release_app_obj:
  539. Object_release(app_object);
  540. exit_release_clientenv:
  541. Object_release(client_env);
  542. return retstat;
  543. }
  544. /**
  545. * nfc_dynamic_protection_ioctl() - dynamic protection control
  546. * @nfc_dev: nfc device data structure
  547. * @sec_zone_trans: mode that we want to move to
  548. * If sec_zone_trans = 1; transition from non-secure zone to secure zone
  549. * If sec_zone_trans = 0; transition from secure zone to non - secure zone
  550. *
  551. * nfc periheral dynamic protection control. Depending on the sec_zone_trans value, device moves to
  552. * secure zone and non-secure zone
  553. *
  554. * Return: -ENOIOCTLCMD if sec_zone_trans val is not supported, 0 if Success(or no issue)
  555. * and error ret code otherwise
  556. */
  557. int nfc_dynamic_protection_ioctl(struct nfc_dev *nfc_dev, unsigned long sec_zone_trans)
  558. {
  559. int ret = 0;
  560. static int init_flag=1;
  561. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  562. if(sec_zone_trans == 1) {
  563. /*check NFC is disabled, only then set Ven GPIO low*/
  564. if(nfc_dev->cold_reset.is_nfc_enabled == false) {
  565. pr_debug("NxpDrv: %s: value %d\n", __func__, gpio_get_value(nfc_gpio->ven));
  566. chk_eSE_pwr_off = 1;
  567. /*check if eSE is active, if yes, wait max of 1sec, until it's inactive */
  568. if(nfc_dev->is_ese_session_active == true) {
  569. if(down_timeout(&sem_eSE_pwr_off, msecs_to_jiffies(1000))) {
  570. /*waited for 1sec yet eSE not turned off, so, ignoring eSE power off*/
  571. pr_info("NxpDrv: Forcefull shutdown of eSE\n");
  572. }
  573. }
  574. ret = nfc_ioctl_power_states(nfc_dev, 0);
  575. /*set driver as secure zone, such that no ioctl calls are allowed*/
  576. nfc_dev->secure_zone = true;
  577. pr_info("NxpDrv: Driver Secure flag set successful\n");
  578. } else {
  579. ret = -1;
  580. }
  581. }
  582. else if(sec_zone_trans == 0) {
  583. chk_eSE_pwr_off = 0;
  584. nfc_dev->secure_zone = false;
  585. if(init_flag) {
  586. /*Initialize once,only during the first non-secure entry*/
  587. ret = nfc_post_init(nfc_dev);
  588. if(ret == 0)
  589. init_flag=0;
  590. }
  591. else {
  592. if(!gpio_get_value(nfc_gpio->ven))
  593. ret = nfc_ioctl_power_states(nfc_dev, 1);
  594. }
  595. pr_info("NxpDrv: Func Driver Secure flag clear successful\n");
  596. } else {
  597. pr_info("NxpDrv: INVALID ARG\n");
  598. ret = -ENOIOCTLCMD;
  599. }
  600. return ret;
  601. }
  602. #endif
  603. /**
  604. * nfc_dev_ioctl - used to set or get data from upper layer.
  605. * @pfile file node for opened device.
  606. * @cmd ioctl type from upper layer.
  607. * @arg ioctl arg from upper layer.
  608. *
  609. * NFC and ESE Device power control, based on the argument value
  610. *
  611. * Return: -ENOIOCTLCMD if arg is not supported
  612. * 0 if Success(or no issue)
  613. * 0 or 1 in case of arg is ESE_GET_PWR/ESE_POWER_STATE
  614. * and error ret code otherwise
  615. */
  616. long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
  617. {
  618. int ret = 0;
  619. struct nfc_dev *nfc_dev = pfile->private_data;
  620. if (!nfc_dev)
  621. return -ENODEV;
  622. #ifdef NFC_SECURE_PERIPHERAL_ENABLED
  623. if( nfc_dev->configs.CNSS_NFC_HW_SECURE_ENABLE == true) {
  624. /*Avoiding ioctl call in secure zone*/
  625. if(nfc_dev->secure_zone) {
  626. if(cmd!=NFC_SECURE_ZONE) {
  627. pr_debug("NxpDrv: nfc_dev_ioctl failed\n");
  628. return -1;
  629. }
  630. }
  631. }
  632. #endif
  633. pr_debug("NxpDrv: %s: cmd = %x arg = %zx\n", __func__, cmd, arg);
  634. switch (cmd) {
  635. case NFC_SET_PWR:
  636. ret = nfc_ioctl_power_states(nfc_dev, arg);
  637. break;
  638. case NFC_SET_RESET_READ_PENDING:
  639. if (arg == NFC_SET_READ_PENDING) {
  640. nfc_dev->cold_reset.is_nfc_read_pending = true;
  641. /* Set default NFC state as NCI for Nfc read pending request */
  642. nfc_dev->nfc_state = NFC_STATE_NCI;
  643. } else if (arg == NFC_RESET_READ_PENDING) {
  644. nfc_dev->cold_reset.is_nfc_read_pending = false;
  645. } else {
  646. ret = -EINVAL;
  647. }
  648. break;
  649. case ESE_SET_PWR:
  650. ret = nfc_ese_pwr(nfc_dev, arg);
  651. break;
  652. case ESE_GET_PWR:
  653. ret = nfc_ese_pwr(nfc_dev, ESE_POWER_STATE);
  654. break;
  655. case NFC_GET_GPIO_STATUS:
  656. ret = nfc_gpio_info(nfc_dev, arg);
  657. break;
  658. case NFCC_GET_INFO:
  659. ret = nfc_ioctl_nfcc_info(pfile, arg);
  660. break;
  661. case ESE_COLD_RESET:
  662. pr_debug("NxpDrv: nfc ese cold reset ioctl\n");
  663. ret = ese_cold_reset_ioctl(nfc_dev, arg);
  664. break;
  665. #ifdef NFC_SECURE_PERIPHERAL_ENABLED
  666. case NFC_SECURE_ZONE:
  667. if( nfc_dev->configs.CNSS_NFC_HW_SECURE_ENABLE == true) {
  668. ret = nfc_dynamic_protection_ioctl(nfc_dev, arg);
  669. }
  670. break;
  671. #endif
  672. default:
  673. pr_err("NxpDrv: %s: bad cmd %lu\n", __func__, arg);
  674. ret = -ENOIOCTLCMD;
  675. }
  676. return ret;
  677. }
  678. int nfc_dev_open(struct inode *inode, struct file *filp)
  679. {
  680. struct nfc_dev *nfc_dev = NULL;
  681. nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
  682. if (!nfc_dev)
  683. return -ENODEV;
  684. pr_debug("NxpDrv: %s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  685. /* Set flag to block freezer fake signal if not set already.
  686. * Without this Signal being set, Driver is trying to do a read
  687. * which is causing the delay in moving to Hibernate Mode.
  688. */
  689. if (!(current->flags & PF_NOFREEZE)) {
  690. current->flags |= PF_NOFREEZE;
  691. pr_debug("NxpDrv: %s: current->flags 0x%x. \n", __func__, current->flags);
  692. }
  693. mutex_lock(&nfc_dev->dev_ref_mutex);
  694. filp->private_data = nfc_dev;
  695. if (nfc_dev->dev_ref_count == 0) {
  696. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  697. nfc_dev->nfc_enable_intr(nfc_dev);
  698. }
  699. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count + 1;
  700. mutex_unlock(&nfc_dev->dev_ref_mutex);
  701. return 0;
  702. }
  703. int nfc_dev_flush(struct file *pfile, fl_owner_t id)
  704. {
  705. struct nfc_dev *nfc_dev = pfile->private_data;
  706. if (!nfc_dev)
  707. return -ENODEV;
  708. /*
  709. * release blocked user thread waiting for pending read during close
  710. */
  711. if (!mutex_trylock(&nfc_dev->read_mutex)) {
  712. nfc_dev->release_read = true;
  713. nfc_dev->nfc_disable_intr(nfc_dev);
  714. wake_up(&nfc_dev->read_wq);
  715. pr_debug("NxpDrv: %s: waiting for release of blocked read\n", __func__);
  716. mutex_lock(&nfc_dev->read_mutex);
  717. nfc_dev->release_read = false;
  718. } else {
  719. pr_debug("NxpDrv: %s: read thread already released\n", __func__);
  720. }
  721. mutex_unlock(&nfc_dev->read_mutex);
  722. return 0;
  723. }
  724. int nfc_dev_close(struct inode *inode, struct file *filp)
  725. {
  726. struct nfc_dev *nfc_dev = NULL;
  727. nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
  728. if (!nfc_dev)
  729. return -ENODEV;
  730. pr_debug("NxpDrv: %s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  731. /* unset the flag to restore to previous state */
  732. if (current->flags & PF_NOFREEZE) {
  733. current->flags &= ~PF_NOFREEZE;
  734. pr_debug("NxpDrv: %s: current->flags 0x%x. \n", __func__, current->flags);
  735. }
  736. mutex_lock(&nfc_dev->dev_ref_mutex);
  737. if (nfc_dev->dev_ref_count == 1) {
  738. nfc_dev->nfc_disable_intr(nfc_dev);
  739. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  740. }
  741. if (nfc_dev->dev_ref_count > 0)
  742. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count - 1;
  743. filp->private_data = NULL;
  744. mutex_unlock(&nfc_dev->dev_ref_mutex);
  745. return 0;
  746. }
  747. int validate_nfc_state_nci(struct nfc_dev *nfc_dev)
  748. {
  749. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  750. if(!nfc_dev->secure_zone) {
  751. if (!gpio_get_value(nfc_gpio->ven)) {
  752. pr_err("NxpDrv: %s: ven low - nfcc powered off\n", __func__);
  753. return -ENODEV;
  754. }
  755. }
  756. if (get_valid_gpio(nfc_gpio->dwl_req) == 1) {
  757. pr_err("NxpDrv: %s: fw download in-progress\n", __func__);
  758. return -EBUSY;
  759. }
  760. if (nfc_dev->nfc_state != NFC_STATE_NCI) {
  761. pr_err("NxpDrv: %s: fw download state\n", __func__);
  762. return -EBUSY;
  763. }
  764. return 0;
  765. }