common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. #include <linux/gpio.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/delay.h>
  23. #include "common_ese.h"
  24. int nfc_parse_dt(struct device *dev, struct platform_configs *nfc_configs,
  25. uint8_t interface)
  26. {
  27. struct device_node *np = dev->of_node;
  28. struct platform_gpio *nfc_gpio = &nfc_configs->gpio;
  29. if (!np) {
  30. pr_err("%s: nfc of_node NULL\n", __func__);
  31. return -EINVAL;
  32. }
  33. nfc_gpio->irq = -EINVAL;
  34. nfc_gpio->dwl_req = -EINVAL;
  35. nfc_gpio->ven = -EINVAL;
  36. /* irq required for i2c based chips only */
  37. if (interface == PLATFORM_IF_I2C) {
  38. nfc_gpio->irq = of_get_named_gpio(np, DTS_IRQ_GPIO_STR, 0);
  39. if ((!gpio_is_valid(nfc_gpio->irq))) {
  40. pr_err("%s: irq gpio invalid %d\n", __func__,
  41. nfc_gpio->irq);
  42. return -EINVAL;
  43. }
  44. pr_info("%s: irq %d\n", __func__, nfc_gpio->irq);
  45. }
  46. nfc_gpio->ven = of_get_named_gpio(np, DTS_VEN_GPIO_STR, 0);
  47. if ((!gpio_is_valid(nfc_gpio->ven))) {
  48. pr_err("%s: ven gpio invalid %d\n", __func__, nfc_gpio->ven);
  49. return -EINVAL;
  50. }
  51. /* some products like sn220 does not required fw dwl pin */
  52. nfc_gpio->dwl_req = of_get_named_gpio(np, DTS_FWDN_GPIO_STR, 0);
  53. if ((!gpio_is_valid(nfc_gpio->dwl_req)))
  54. pr_warn("%s: dwl_req gpio invalid %d\n", __func__,
  55. nfc_gpio->dwl_req);
  56. pr_info("%s: %d, %d, %d\n", __func__, nfc_gpio->irq, nfc_gpio->ven,
  57. nfc_gpio->dwl_req);
  58. return 0;
  59. }
  60. void set_valid_gpio(int gpio, int value)
  61. {
  62. if (gpio_is_valid(gpio)) {
  63. pr_debug("%s: gpio %d value %d\n", __func__, gpio, value);
  64. gpio_set_value(gpio, value);
  65. /* hardware dependent delay */
  66. usleep_range(NFC_GPIO_SET_WAIT_TIME_US,
  67. NFC_GPIO_SET_WAIT_TIME_US + 100);
  68. }
  69. }
  70. int get_valid_gpio(int gpio)
  71. {
  72. int value = -EINVAL;
  73. if (gpio_is_valid(gpio)) {
  74. value = gpio_get_value(gpio);
  75. pr_debug("%s: gpio %d value %d\n", __func__, gpio, value);
  76. }
  77. return value;
  78. }
  79. void gpio_set_ven(struct nfc_dev *nfc_dev, int value)
  80. {
  81. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  82. if (gpio_get_value(nfc_gpio->ven) != value) {
  83. pr_debug("%s: value %d\n", __func__, value);
  84. /* reset on change in level from high to low */
  85. if (value)
  86. ese_cold_reset_release(nfc_dev);
  87. gpio_set_value(nfc_gpio->ven, value);
  88. /* hardware dependent delay */
  89. usleep_range(NFC_GPIO_SET_WAIT_TIME_US,
  90. NFC_GPIO_SET_WAIT_TIME_US + 100);
  91. }
  92. }
  93. int configure_gpio(unsigned int gpio, int flag)
  94. {
  95. int ret;
  96. pr_debug("%s: nfc gpio [%d] flag [%01x]\n", __func__, gpio, flag);
  97. if (gpio_is_valid(gpio)) {
  98. ret = gpio_request(gpio, "nfc_gpio");
  99. if (ret) {
  100. pr_err("%s: unable to request nfc gpio [%d]\n",
  101. __func__, gpio);
  102. return ret;
  103. }
  104. /* set direction and value for output pin */
  105. if (flag & GPIO_OUTPUT) {
  106. ret = gpio_direction_output(gpio, (GPIO_HIGH & flag));
  107. pr_debug("%s: nfc o/p gpio %d level %d\n", __func__,
  108. gpio, gpio_get_value(gpio));
  109. } else {
  110. ret = gpio_direction_input(gpio);
  111. pr_debug("%s: nfc i/p gpio %d\n", __func__, gpio);
  112. }
  113. if (ret) {
  114. pr_err("%s: unable to set direction for nfc gpio [%d]\n",
  115. __func__, gpio);
  116. gpio_free(gpio);
  117. return ret;
  118. }
  119. /* Consider value as control for input IRQ pin */
  120. if (flag & GPIO_IRQ) {
  121. ret = gpio_to_irq(gpio);
  122. if (ret < 0) {
  123. pr_err("%s: unable to set irq [%d]\n", __func__,
  124. gpio);
  125. gpio_free(gpio);
  126. return ret;
  127. }
  128. pr_debug("%s: gpio_to_irq successful [%d]\n", __func__,
  129. gpio);
  130. return ret;
  131. }
  132. } else {
  133. pr_err("%s: invalid gpio\n", __func__);
  134. ret = -EINVAL;
  135. }
  136. return ret;
  137. }
  138. void gpio_free_all(struct nfc_dev *nfc_dev)
  139. {
  140. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  141. if (gpio_is_valid(nfc_gpio->dwl_req))
  142. gpio_free(nfc_gpio->dwl_req);
  143. if (gpio_is_valid(nfc_gpio->irq))
  144. gpio_free(nfc_gpio->irq);
  145. if (gpio_is_valid(nfc_gpio->ven))
  146. gpio_free(nfc_gpio->ven);
  147. }
  148. void nfc_misc_unregister(struct nfc_dev *nfc_dev, int count)
  149. {
  150. pr_debug("%s: entry\n", __func__);
  151. device_destroy(nfc_dev->nfc_class, nfc_dev->devno);
  152. cdev_del(&nfc_dev->c_dev);
  153. class_destroy(nfc_dev->nfc_class);
  154. unregister_chrdev_region(nfc_dev->devno, count);
  155. }
  156. int nfc_misc_register(struct nfc_dev *nfc_dev,
  157. const struct file_operations *nfc_fops, int count,
  158. char *devname, char *classname)
  159. {
  160. int ret = 0;
  161. ret = alloc_chrdev_region(&nfc_dev->devno, 0, count, devname);
  162. if (ret < 0) {
  163. pr_err("%s: failed to alloc chrdev region ret %d\n", __func__,
  164. ret);
  165. return ret;
  166. }
  167. nfc_dev->nfc_class = class_create(THIS_MODULE, classname);
  168. if (IS_ERR(nfc_dev->nfc_class)) {
  169. ret = PTR_ERR(nfc_dev->nfc_class);
  170. pr_err("%s: failed to register device class ret %d\n", __func__,
  171. ret);
  172. unregister_chrdev_region(nfc_dev->devno, count);
  173. return ret;
  174. }
  175. cdev_init(&nfc_dev->c_dev, nfc_fops);
  176. ret = cdev_add(&nfc_dev->c_dev, nfc_dev->devno, count);
  177. if (ret < 0) {
  178. pr_err("%s: failed to add cdev ret %d\n", __func__, ret);
  179. class_destroy(nfc_dev->nfc_class);
  180. unregister_chrdev_region(nfc_dev->devno, count);
  181. return ret;
  182. }
  183. nfc_dev->nfc_device = device_create(nfc_dev->nfc_class, NULL,
  184. nfc_dev->devno, nfc_dev, devname);
  185. if (IS_ERR(nfc_dev->nfc_device)) {
  186. ret = PTR_ERR(nfc_dev->nfc_device);
  187. pr_err("%s: failed to create the device ret %d\n", __func__,
  188. ret);
  189. cdev_del(&nfc_dev->c_dev);
  190. class_destroy(nfc_dev->nfc_class);
  191. unregister_chrdev_region(nfc_dev->devno, count);
  192. return ret;
  193. }
  194. return 0;
  195. }
  196. /**
  197. * nfc_ioctl_power_states() - power control
  198. * @nfc_dev: nfc device data structure
  199. * @arg: mode that we want to move to
  200. *
  201. * Device power control. Depending on the arg value, device moves to
  202. * different states, refer common.h for args
  203. *
  204. * Return: -ENOIOCTLCMD if arg is not supported, 0 if Success(or no issue)
  205. * and error ret code otherwise
  206. */
  207. static int nfc_ioctl_power_states(struct nfc_dev *nfc_dev, unsigned long arg)
  208. {
  209. int ret = 0;
  210. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  211. if (arg == NFC_POWER_OFF) {
  212. /*
  213. * We are attempting a hardware reset so let us disable
  214. * interrupts to avoid spurious notifications to upper
  215. * layers.
  216. */
  217. nfc_dev->nfc_disable_intr(nfc_dev);
  218. set_valid_gpio(nfc_gpio->dwl_req, 0);
  219. gpio_set_ven(nfc_dev, 0);
  220. nfc_dev->nfc_ven_enabled = false;
  221. } else if (arg == NFC_POWER_ON) {
  222. nfc_dev->nfc_enable_intr(nfc_dev);
  223. set_valid_gpio(nfc_gpio->dwl_req, 0);
  224. gpio_set_ven(nfc_dev, 1);
  225. nfc_dev->nfc_ven_enabled = true;
  226. } else if (arg == NFC_FW_DWL_VEN_TOGGLE) {
  227. /*
  228. * We are switching to download Mode, toggle the enable pin
  229. * in order to set the NFCC in the new mode
  230. */
  231. nfc_dev->nfc_disable_intr(nfc_dev);
  232. set_valid_gpio(nfc_gpio->dwl_req, 1);
  233. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  234. gpio_set_ven(nfc_dev, 0);
  235. gpio_set_ven(nfc_dev, 1);
  236. nfc_dev->nfc_enable_intr(nfc_dev);
  237. } else if (arg == NFC_FW_DWL_HIGH) {
  238. /*
  239. * Setting firmware download gpio to HIGH
  240. * before FW download start
  241. */
  242. set_valid_gpio(nfc_gpio->dwl_req, 1);
  243. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  244. } else if (arg == NFC_VEN_FORCED_HARD_RESET) {
  245. nfc_dev->nfc_disable_intr(nfc_dev);
  246. gpio_set_ven(nfc_dev, 0);
  247. gpio_set_ven(nfc_dev, 1);
  248. nfc_dev->nfc_enable_intr(nfc_dev);
  249. } else if (arg == NFC_FW_DWL_LOW) {
  250. /*
  251. * Setting firmware download gpio to LOW
  252. * FW download finished
  253. */
  254. set_valid_gpio(nfc_gpio->dwl_req, 0);
  255. nfc_dev->nfc_state = NFC_STATE_NCI;
  256. } else {
  257. pr_err("%s: bad arg %lu\n", __func__, arg);
  258. ret = -ENOIOCTLCMD;
  259. }
  260. return ret;
  261. }
  262. /**
  263. * nfc_dev_ioctl - used to set or get data from upper layer.
  264. * @pfile file node for opened device.
  265. * @cmd ioctl type from upper layer.
  266. * @arg ioctl arg from upper layer.
  267. *
  268. * NFC and ESE Device power control, based on the argument value
  269. *
  270. * Return: -ENOIOCTLCMD if arg is not supported
  271. * 0 if Success(or no issue)
  272. * 0 or 1 in case of arg is ESE_GET_PWR/ESE_POWER_STATE
  273. * and error ret code otherwise
  274. */
  275. long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
  276. {
  277. int ret = 0;
  278. struct nfc_dev *nfc_dev = pfile->private_data;
  279. if (!nfc_dev)
  280. return -ENODEV;
  281. pr_debug("%s: cmd = %x arg = %zx\n", __func__, cmd, arg);
  282. switch (cmd) {
  283. case NFC_SET_PWR:
  284. ret = nfc_ioctl_power_states(nfc_dev, arg);
  285. break;
  286. case ESE_SET_PWR:
  287. ret = nfc_ese_pwr(nfc_dev, arg);
  288. break;
  289. case ESE_GET_PWR:
  290. ret = nfc_ese_pwr(nfc_dev, ESE_POWER_STATE);
  291. break;
  292. default:
  293. pr_err("%s: bad cmd %lu\n", __func__, arg);
  294. ret = -ENOIOCTLCMD;
  295. };
  296. return ret;
  297. }
  298. int nfc_dev_open(struct inode *inode, struct file *filp)
  299. {
  300. struct nfc_dev *nfc_dev = NULL;
  301. nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
  302. if (!nfc_dev)
  303. return -ENODEV;
  304. pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  305. mutex_lock(&nfc_dev->dev_ref_mutex);
  306. filp->private_data = nfc_dev;
  307. if (nfc_dev->dev_ref_count == 0) {
  308. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  309. nfc_dev->nfc_enable_intr(nfc_dev);
  310. }
  311. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count + 1;
  312. mutex_unlock(&nfc_dev->dev_ref_mutex);
  313. return 0;
  314. }
  315. int nfc_dev_flush(struct file *pfile, fl_owner_t id)
  316. {
  317. struct nfc_dev *nfc_dev = pfile->private_data;
  318. if (!nfc_dev)
  319. return -ENODEV;
  320. /*
  321. * release blocked user thread waiting for pending read during close
  322. */
  323. if (!mutex_trylock(&nfc_dev->read_mutex)) {
  324. nfc_dev->release_read = true;
  325. nfc_dev->nfc_disable_intr(nfc_dev);
  326. wake_up(&nfc_dev->read_wq);
  327. pr_debug("%s: waiting for release of blocked read\n", __func__);
  328. mutex_lock(&nfc_dev->read_mutex);
  329. nfc_dev->release_read = false;
  330. } else {
  331. pr_debug("%s: read thread already released\n", __func__);
  332. }
  333. mutex_unlock(&nfc_dev->read_mutex);
  334. return 0;
  335. }
  336. int nfc_dev_close(struct inode *inode, struct file *filp)
  337. {
  338. struct nfc_dev *nfc_dev = NULL;
  339. nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
  340. if (!nfc_dev)
  341. return -ENODEV;
  342. pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  343. mutex_lock(&nfc_dev->dev_ref_mutex);
  344. if (nfc_dev->dev_ref_count == 1) {
  345. nfc_dev->nfc_disable_intr(nfc_dev);
  346. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  347. }
  348. if (nfc_dev->dev_ref_count > 0)
  349. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count - 1;
  350. else {
  351. /*
  352. * Use "ESE_RST_PROT_DIS" as argument
  353. * if eSE calls flow is via NFC driver
  354. * i.e. direct calls from SPI HAL to NFC driver
  355. */
  356. nfc_ese_pwr(nfc_dev, ESE_RST_PROT_DIS_NFC);
  357. }
  358. filp->private_data = NULL;
  359. mutex_unlock(&nfc_dev->dev_ref_mutex);
  360. return 0;
  361. }
  362. int validate_nfc_state_nci(struct nfc_dev *nfc_dev)
  363. {
  364. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  365. if (!gpio_get_value(nfc_gpio->ven)) {
  366. pr_err("%s: ven low - nfcc powered off\n", __func__);
  367. return -ENODEV;
  368. }
  369. if (get_valid_gpio(nfc_gpio->dwl_req) == 1) {
  370. pr_err("%s: fw download in-progress\n", __func__);
  371. return -EBUSY;
  372. }
  373. if (nfc_dev->nfc_state != NFC_STATE_NCI) {
  374. pr_err("%s: fw download state\n", __func__);
  375. return -EBUSY;
  376. }
  377. return 0;
  378. }