wsa881x-irq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /* Copyright (c) 2015, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/bitops.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/sched.h>
  16. #include <linux/irq.h>
  17. #include <linux/delay.h>
  18. #include <linux/of.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/slab.h>
  21. #include <linux/ratelimit.h>
  22. #include <linux/pm_qos.h>
  23. #include <soc/qcom/pm.h>
  24. #include "wsa881x-irq.h"
  25. #include "wsa881x-registers-analog.h"
  26. #define BYTE_BIT_MASK(nr) (1UL << ((nr) % BITS_PER_BYTE))
  27. #define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE)
  28. #define WSA_MAX_NUM_IRQS 8
  29. #ifndef NO_IRQ
  30. #define NO_IRQ (-1)
  31. #endif
  32. static int virq_to_phyirq(
  33. struct wsa_resource *wsa_res, int virq);
  34. static int phyirq_to_virq(
  35. struct wsa_resource *wsa_res, int irq);
  36. static unsigned int wsa_irq_get_upstream_irq(
  37. struct wsa_resource *wsa_res);
  38. static void wsa_irq_put_upstream_irq(
  39. struct wsa_resource *wsa_res);
  40. static int wsa_map_irq(
  41. struct wsa_resource *wsa_res, int irq);
  42. static struct snd_soc_codec *ptr_codec;
  43. /**
  44. * wsa_set_codec() - to update codec pointer
  45. * @codec: codec pointer.
  46. *
  47. * To update the codec pointer, which is used to read/write
  48. * wsa register.
  49. *
  50. * Return: void.
  51. */
  52. void wsa_set_codec(struct snd_soc_codec *codec)
  53. {
  54. if (codec == NULL) {
  55. pr_err("%s: codec pointer is NULL\n", __func__);
  56. ptr_codec = NULL;
  57. return;
  58. }
  59. ptr_codec = codec;
  60. /* Initialize interrupt mask and level registers */
  61. snd_soc_write(codec, WSA881X_INTR_LEVEL, 0x8F);
  62. snd_soc_write(codec, WSA881X_INTR_MASK, 0x8F);
  63. }
  64. static void wsa_irq_lock(struct irq_data *data)
  65. {
  66. struct wsa_resource *wsa_res =
  67. irq_data_get_irq_chip_data(data);
  68. if (wsa_res == NULL) {
  69. pr_err("%s: wsa_res pointer is NULL\n", __func__);
  70. return;
  71. }
  72. mutex_lock(&wsa_res->irq_lock);
  73. }
  74. static void wsa_irq_sync_unlock(struct irq_data *data)
  75. {
  76. struct wsa_resource *wsa_res =
  77. irq_data_get_irq_chip_data(data);
  78. if (wsa_res == NULL) {
  79. pr_err("%s: wsa_res pointer is NULL\n", __func__);
  80. return;
  81. }
  82. if (wsa_res->codec == NULL) {
  83. pr_err("%s: codec pointer not registered\n", __func__);
  84. if (ptr_codec == NULL) {
  85. pr_err("%s: did not receive valid codec pointer\n",
  86. __func__);
  87. goto unlock;
  88. } else {
  89. wsa_res->codec = ptr_codec;
  90. }
  91. }
  92. /*
  93. * If there's been a change in the mask write it back
  94. * to the hardware.
  95. */
  96. if (wsa_res->irq_masks_cur !=
  97. wsa_res->irq_masks_cache) {
  98. wsa_res->irq_masks_cache =
  99. wsa_res->irq_masks_cur;
  100. snd_soc_write(wsa_res->codec,
  101. WSA881X_INTR_MASK,
  102. wsa_res->irq_masks_cur);
  103. }
  104. unlock:
  105. mutex_unlock(&wsa_res->irq_lock);
  106. }
  107. static void wsa_irq_enable(struct irq_data *data)
  108. {
  109. struct wsa_resource *wsa_res =
  110. irq_data_get_irq_chip_data(data);
  111. int wsa_irq;
  112. if (wsa_res == NULL) {
  113. pr_err("%s: wsa_res pointer is NULL\n", __func__);
  114. return;
  115. }
  116. wsa_irq = virq_to_phyirq(wsa_res, data->irq);
  117. pr_debug("%s: wsa_irq = %d\n", __func__, wsa_irq);
  118. wsa_res->irq_masks_cur &=
  119. ~(BYTE_BIT_MASK(wsa_irq));
  120. }
  121. static void wsa_irq_disable(struct irq_data *data)
  122. {
  123. struct wsa_resource *wsa_res =
  124. irq_data_get_irq_chip_data(data);
  125. int wsa_irq;
  126. if (wsa_res == NULL) {
  127. pr_err("%s: wsa_res pointer is NULL\n", __func__);
  128. return;
  129. }
  130. wsa_irq = virq_to_phyirq(wsa_res, data->irq);
  131. pr_debug("%s: wsa_irq = %d\n", __func__, wsa_irq);
  132. wsa_res->irq_masks_cur
  133. |= BYTE_BIT_MASK(wsa_irq);
  134. }
  135. static void wsa_irq_ack(struct irq_data *data)
  136. {
  137. int wsa_irq = 0;
  138. struct wsa_resource *wsa_res =
  139. irq_data_get_irq_chip_data(data);
  140. if (wsa_res == NULL) {
  141. pr_err("%s: wsa_res is NULL\n", __func__);
  142. return;
  143. }
  144. wsa_irq = virq_to_phyirq(wsa_res, data->irq);
  145. pr_debug("%s: IRQ_ACK called for WCD9XXX IRQ: %d\n",
  146. __func__, wsa_irq);
  147. }
  148. static void wsa_irq_mask(struct irq_data *d)
  149. {
  150. /* do nothing but required as linux calls irq_mask without NULL check */
  151. }
  152. static struct irq_chip wsa_irq_chip = {
  153. .name = "wsa",
  154. .irq_bus_lock = wsa_irq_lock,
  155. .irq_bus_sync_unlock = wsa_irq_sync_unlock,
  156. .irq_disable = wsa_irq_disable,
  157. .irq_enable = wsa_irq_enable,
  158. .irq_mask = wsa_irq_mask,
  159. .irq_ack = wsa_irq_ack,
  160. };
  161. static irqreturn_t wsa_irq_thread(int irq, void *data)
  162. {
  163. struct wsa_resource *wsa_res = data;
  164. int i;
  165. u8 status;
  166. if (wsa_res == NULL) {
  167. pr_err("%s: wsa_res is NULL\n", __func__);
  168. return IRQ_HANDLED;
  169. }
  170. if (wsa_res->codec == NULL) {
  171. pr_err("%s: codec pointer not registered\n", __func__);
  172. if (ptr_codec == NULL) {
  173. pr_err("%s: did not receive valid codec pointer\n",
  174. __func__);
  175. return IRQ_HANDLED;
  176. }
  177. wsa_res->codec = ptr_codec;
  178. }
  179. status = snd_soc_read(wsa_res->codec, WSA881X_INTR_STATUS);
  180. /* Apply masking */
  181. status &= ~wsa_res->irq_masks_cur;
  182. for (i = 0; i < wsa_res->num_irqs; i++) {
  183. if (status & BYTE_BIT_MASK(i)) {
  184. mutex_lock(&wsa_res->nested_irq_lock);
  185. handle_nested_irq(phyirq_to_virq(wsa_res, i));
  186. mutex_unlock(&wsa_res->nested_irq_lock);
  187. }
  188. }
  189. return IRQ_HANDLED;
  190. }
  191. /**
  192. * wsa_free_irq() - to free an interrupt
  193. * @irq: interrupt number.
  194. * @data: pointer to wsa resource.
  195. *
  196. * To free already requested interrupt.
  197. *
  198. * Return: void.
  199. */
  200. void wsa_free_irq(int irq, void *data)
  201. {
  202. struct wsa_resource *wsa_res = data;
  203. if (wsa_res == NULL) {
  204. pr_err("%s: wsa_res is NULL\n", __func__);
  205. return;
  206. }
  207. free_irq(phyirq_to_virq(wsa_res, irq), data);
  208. }
  209. /**
  210. * wsa_enable_irq() - to enable an interrupt
  211. * @wsa_res: pointer to wsa resource.
  212. * @irq: interrupt number.
  213. *
  214. * This function is to enable an interrupt.
  215. *
  216. * Return: void.
  217. */
  218. void wsa_enable_irq(struct wsa_resource *wsa_res, int irq)
  219. {
  220. if (wsa_res == NULL) {
  221. pr_err("%s: wsa_res is NULL\n", __func__);
  222. return;
  223. }
  224. enable_irq(phyirq_to_virq(wsa_res, irq));
  225. }
  226. /**
  227. * wsa_disable_irq() - to disable an interrupt
  228. * @wsa_res: pointer to wsa resource.
  229. * @irq: interrupt number.
  230. *
  231. * To disable an interrupt without waiting for executing
  232. * handler to complete.
  233. *
  234. * Return: void.
  235. */
  236. void wsa_disable_irq(struct wsa_resource *wsa_res, int irq)
  237. {
  238. if (wsa_res == NULL) {
  239. pr_err("%s: wsa_res is NULL\n", __func__);
  240. return;
  241. }
  242. disable_irq_nosync(phyirq_to_virq(wsa_res, irq));
  243. }
  244. /**
  245. * wsa_disable_irq_sync() - to disable an interrupt
  246. * @wsa_res: pointer to wsa resource.
  247. * @irq: interrupt number.
  248. *
  249. * To disable an interrupt, wait for executing IRQ
  250. * handler to complete.
  251. *
  252. * Return: void.
  253. */
  254. void wsa_disable_irq_sync(
  255. struct wsa_resource *wsa_res, int irq)
  256. {
  257. if (wsa_res == NULL) {
  258. pr_err("%s: wsa_res is NULL\n", __func__);
  259. return;
  260. }
  261. disable_irq(phyirq_to_virq(wsa_res, irq));
  262. }
  263. static int wsa_irq_setup_downstream_irq(struct wsa_resource *wsa_res)
  264. {
  265. int irq, virq, ret;
  266. if (wsa_res == NULL) {
  267. pr_err("%s: wsa_res is NULL\n", __func__);
  268. return -EINVAL;
  269. }
  270. pr_debug("%s: enter\n", __func__);
  271. for (irq = 0; irq < wsa_res->num_irqs; irq++) {
  272. /* Map OF irq */
  273. virq = wsa_map_irq(wsa_res, irq);
  274. pr_debug("%s: irq %d -> %d\n", __func__, irq, virq);
  275. if (virq == NO_IRQ) {
  276. pr_err("%s, No interrupt specifier for irq %d\n",
  277. __func__, irq);
  278. return NO_IRQ;
  279. }
  280. ret = irq_set_chip_data(virq, wsa_res);
  281. if (ret) {
  282. pr_err("%s: Failed to configure irq %d (%d)\n",
  283. __func__, irq, ret);
  284. return ret;
  285. }
  286. if (wsa_res->irq_level_high[irq])
  287. irq_set_chip_and_handler(virq, &wsa_irq_chip,
  288. handle_level_irq);
  289. else
  290. irq_set_chip_and_handler(virq, &wsa_irq_chip,
  291. handle_edge_irq);
  292. irq_set_nested_thread(virq, 1);
  293. }
  294. pr_debug("%s: leave\n", __func__);
  295. return 0;
  296. }
  297. static int wsa_irq_init(struct wsa_resource *wsa_res)
  298. {
  299. int i, ret;
  300. if (wsa_res == NULL) {
  301. pr_err("%s: wsa_res is NULL\n", __func__);
  302. return -EINVAL;
  303. }
  304. mutex_init(&wsa_res->irq_lock);
  305. mutex_init(&wsa_res->nested_irq_lock);
  306. wsa_res->irq = wsa_irq_get_upstream_irq(wsa_res);
  307. if (!wsa_res->irq) {
  308. pr_warn("%s: irq driver is not yet initialized\n", __func__);
  309. mutex_destroy(&wsa_res->irq_lock);
  310. mutex_destroy(&wsa_res->nested_irq_lock);
  311. return -EPROBE_DEFER;
  312. }
  313. pr_debug("%s: probed irq %d\n", __func__, wsa_res->irq);
  314. /* Setup downstream IRQs */
  315. ret = wsa_irq_setup_downstream_irq(wsa_res);
  316. if (ret) {
  317. pr_err("%s: Failed to setup downstream IRQ\n", __func__);
  318. goto fail_irq_init;
  319. }
  320. /* mask all the interrupts */
  321. for (i = 0; i < wsa_res->num_irqs; i++) {
  322. wsa_res->irq_masks_cur |= BYTE_BIT_MASK(i);
  323. wsa_res->irq_masks_cache |= BYTE_BIT_MASK(i);
  324. }
  325. ret = request_threaded_irq(wsa_res->irq, NULL, wsa_irq_thread,
  326. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  327. "wsa", wsa_res);
  328. if (ret != 0) {
  329. dev_err(wsa_res->dev, "Failed to request IRQ %d: %d\n",
  330. wsa_res->irq, ret);
  331. } else {
  332. ret = enable_irq_wake(wsa_res->irq);
  333. if (ret) {
  334. dev_err(wsa_res->dev,
  335. "Failed to set wake interrupt on IRQ %d: %d\n",
  336. wsa_res->irq, ret);
  337. free_irq(wsa_res->irq, wsa_res);
  338. }
  339. }
  340. if (ret)
  341. goto fail_irq_init;
  342. return ret;
  343. fail_irq_init:
  344. dev_err(wsa_res->dev,
  345. "%s: Failed to init wsa irq\n", __func__);
  346. wsa_irq_put_upstream_irq(wsa_res);
  347. mutex_destroy(&wsa_res->irq_lock);
  348. mutex_destroy(&wsa_res->nested_irq_lock);
  349. return ret;
  350. }
  351. /**
  352. * wsa_request_irq() - to request/register an interrupt
  353. * @wsa_res: pointer to wsa_resource.
  354. * @irq: interrupt number.
  355. * @handler: interrupt handler function pointer.
  356. * @name: interrupt name.
  357. * @data: device info.
  358. *
  359. * Convert physical irq to virtual irq and then
  360. * reguest for threaded handler.
  361. *
  362. * Return: Retuns success/failure.
  363. */
  364. int wsa_request_irq(struct wsa_resource *wsa_res,
  365. int irq, irq_handler_t handler,
  366. const char *name, void *data)
  367. {
  368. int virq;
  369. if (wsa_res == NULL) {
  370. pr_err("%s: wsa_res is NULL\n", __func__);
  371. return -EINVAL;
  372. }
  373. virq = phyirq_to_virq(wsa_res, irq);
  374. /*
  375. * ARM needs us to explicitly flag the IRQ as valid
  376. * and will set them noprobe when we do so.
  377. */
  378. #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
  379. set_irq_flags(virq, IRQF_VALID);
  380. #else
  381. set_irq_noprobe(virq);
  382. #endif
  383. return request_threaded_irq(virq, NULL, handler, IRQF_TRIGGER_RISING,
  384. name, data);
  385. }
  386. /**
  387. * wsa_irq_exit() - to disable/clear interrupt/resources
  388. * @wsa_res: pointer to wsa_resource
  389. *
  390. * Disable and free the interrupts and then release resources.
  391. *
  392. * Return: void.
  393. */
  394. void wsa_irq_exit(struct wsa_resource *wsa_res)
  395. {
  396. if (wsa_res == NULL) {
  397. pr_err("%s: wsa_res is NULL\n", __func__);
  398. return;
  399. }
  400. dev_dbg(wsa_res->dev, "%s: Cleaning up irq %d\n", __func__,
  401. wsa_res->irq);
  402. if (wsa_res->irq) {
  403. disable_irq_wake(wsa_res->irq);
  404. free_irq(wsa_res->irq, wsa_res);
  405. /* Release parent's of node */
  406. wsa_irq_put_upstream_irq(wsa_res);
  407. }
  408. mutex_destroy(&wsa_res->irq_lock);
  409. mutex_destroy(&wsa_res->nested_irq_lock);
  410. }
  411. static int phyirq_to_virq(struct wsa_resource *wsa_res, int offset)
  412. {
  413. if (wsa_res == NULL) {
  414. pr_err("%s: wsa_res is NULL\n", __func__);
  415. return -EINVAL;
  416. }
  417. return irq_linear_revmap(wsa_res->domain, offset);
  418. }
  419. static int virq_to_phyirq(struct wsa_resource *wsa_res, int virq)
  420. {
  421. struct irq_data *irq_data = irq_get_irq_data(virq);
  422. if (unlikely(!irq_data)) {
  423. pr_err("%s: irq_data is NULL\n", __func__);
  424. return -EINVAL;
  425. }
  426. return irq_data->hwirq;
  427. }
  428. static unsigned int wsa_irq_get_upstream_irq(struct wsa_resource *wsa_res)
  429. {
  430. if (wsa_res == NULL) {
  431. pr_err("%s: wsa_res is NULL\n", __func__);
  432. return -EINVAL;
  433. }
  434. return wsa_res->irq;
  435. }
  436. static void wsa_irq_put_upstream_irq(struct wsa_resource *wsa_res)
  437. {
  438. if (wsa_res == NULL) {
  439. pr_err("%s: wsa_res is NULL\n", __func__);
  440. return;
  441. }
  442. /* Hold parent's of node */
  443. of_node_put(wsa_res->dev->of_node);
  444. }
  445. static int wsa_map_irq(struct wsa_resource *wsa_res, int irq)
  446. {
  447. if (wsa_res == NULL) {
  448. pr_err("%s: wsa_res is NULL\n", __func__);
  449. return -EINVAL;
  450. }
  451. return of_irq_to_resource(wsa_res->dev->of_node, irq, NULL);
  452. }
  453. static int wsa_irq_probe(struct platform_device *pdev)
  454. {
  455. int irq;
  456. struct wsa_resource *wsa_res = NULL;
  457. int ret = -EINVAL;
  458. irq = platform_get_irq_byname(pdev, "wsa-int");
  459. if (irq < 0) {
  460. dev_err(&pdev->dev, "%s: Couldn't find wsa-int node(%d)\n",
  461. __func__, irq);
  462. return -EINVAL;
  463. }
  464. pr_debug("%s: node %s\n", __func__, pdev->name);
  465. wsa_res = kzalloc(sizeof(*wsa_res), GFP_KERNEL);
  466. if (!wsa_res) {
  467. pr_err("%s: could not allocate memory\n", __func__);
  468. return -ENOMEM;
  469. }
  470. /*
  471. * wsa interrupt controller supports N to N irq mapping with
  472. * single cell binding with irq numbers(offsets) only.
  473. * Use irq_domain_simple_ops that has irq_domain_simple_map and
  474. * irq_domain_xlate_onetwocell.
  475. */
  476. wsa_res->dev = &pdev->dev;
  477. wsa_res->domain = irq_domain_add_linear(wsa_res->dev->of_node,
  478. WSA_MAX_NUM_IRQS, &irq_domain_simple_ops,
  479. wsa_res);
  480. if (!wsa_res->domain) {
  481. dev_err(&pdev->dev, "%s: domain is NULL\n", __func__);
  482. ret = -ENOMEM;
  483. goto err;
  484. }
  485. wsa_res->dev = &pdev->dev;
  486. dev_dbg(&pdev->dev, "%s: virq = %d\n", __func__, irq);
  487. wsa_res->irq = irq;
  488. wsa_res->num_irq_regs = 1;
  489. wsa_res->num_irqs = WSA_NUM_IRQS;
  490. ret = wsa_irq_init(wsa_res);
  491. if (ret < 0) {
  492. dev_err(&pdev->dev, "%s: failed to do irq init %d\n",
  493. __func__, ret);
  494. goto err;
  495. }
  496. return ret;
  497. err:
  498. kfree(wsa_res);
  499. return ret;
  500. }
  501. static int wsa_irq_remove(struct platform_device *pdev)
  502. {
  503. struct irq_domain *domain;
  504. struct wsa_resource *data;
  505. domain = irq_find_host(pdev->dev.of_node);
  506. if (unlikely(!domain)) {
  507. pr_err("%s: domain is NULL\n", __func__);
  508. return -EINVAL;
  509. }
  510. data = (struct wsa_resource *)domain->host_data;
  511. data->irq = 0;
  512. return 0;
  513. }
  514. static const struct of_device_id of_match[] = {
  515. { .compatible = "qcom,wsa-irq" },
  516. { }
  517. };
  518. static struct platform_driver wsa_irq_driver = {
  519. .probe = wsa_irq_probe,
  520. .remove = wsa_irq_remove,
  521. .driver = {
  522. .name = "wsa_intc",
  523. .owner = THIS_MODULE,
  524. .of_match_table = of_match_ptr(of_match),
  525. },
  526. };
  527. static int wsa_irq_drv_init(void)
  528. {
  529. return platform_driver_register(&wsa_irq_driver);
  530. }
  531. subsys_initcall(wsa_irq_drv_init);
  532. static void wsa_irq_drv_exit(void)
  533. {
  534. platform_driver_unregister(&wsa_irq_driver);
  535. }
  536. module_exit(wsa_irq_drv_exit);
  537. MODULE_DESCRIPTION("WSA881x IRQ driver");
  538. MODULE_LICENSE("GPL v2");