common.c 23 KB

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