aqt1000-irq.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* Copyright (c) 2018, 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/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/sched.h>
  15. #include <linux/irq.h>
  16. #include <linux/delay.h>
  17. #include <linux/of.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/slab.h>
  20. #include <linux/ratelimit.h>
  21. #include <linux/irqdomain.h>
  22. #include <linux/regmap.h>
  23. #include "aqt1000-registers.h"
  24. #include "aqt1000-irq.h"
  25. static int aqt_irq_init(struct aqt1000_irq *aqt_irq)
  26. {
  27. int i, ret;
  28. if (aqt_irq == NULL) {
  29. pr_err("%s: aqt_irq is NULL\n", __func__);
  30. return -EINVAL;
  31. }
  32. mutex_init(&aqt_irq->irq_lock);
  33. mutex_init(&aqt_irq->nested_irq_lock);
  34. aqt_irq->irq = aqt_irq_get_upstream_irq(aqt_irq);
  35. if (!aqt_irq->irq) {
  36. pr_warn("%s: irq driver is not yet initialized\n", __func__);
  37. mutex_destroy(&aqt_irq->irq_lock);
  38. mutex_destroy(&aqt_irq->nested_irq_lock);
  39. return -EPROBE_DEFER;
  40. }
  41. pr_debug("%s: probed irq %d\n", __func__, aqt_irq->irq);
  42. /* Setup downstream IRQs */
  43. ret = aqt_irq_setup_downstream_irq(aqt_irq);
  44. if (ret) {
  45. pr_err("%s: Failed to setup downstream IRQ\n", __func__);
  46. goto fail_irq_init;
  47. }
  48. /* mask all the interrupts */
  49. for (i = 0; i < aqt_irq->num_irqs; i++) {
  50. aqt_irq->irq_masks_cur |= BYTE_BIT_MASK(i);
  51. aqt_irq->irq_masks_cache |= BYTE_BIT_MASK(i);
  52. }
  53. ret = request_threaded_irq(aqt_irq->irq, NULL, aqt_irq_thread,
  54. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  55. "aqt", aqt_irq);
  56. if (ret != 0) {
  57. dev_err(aqt_irq->dev, "Failed to request IRQ %d: %d\n",
  58. aqt_irq->irq, ret);
  59. } else {
  60. ret = enable_irq_wake(aqt_irq->irq);
  61. if (ret) {
  62. dev_err(aqt_irq->dev,
  63. "Failed to set wake interrupt on IRQ %d: %d\n",
  64. aqt_irq->irq, ret);
  65. free_irq(aqt_irq->irq, aqt_irq);
  66. }
  67. }
  68. if (ret)
  69. goto fail_irq_init;
  70. return ret;
  71. fail_irq_init:
  72. dev_err(aqt_irq->dev,
  73. "%s: Failed to init aqt irq\n", __func__);
  74. aqt_irq_put_upstream_irq(aqt_irq);
  75. mutex_destroy(&aqt_irq->irq_lock);
  76. mutex_destroy(&aqt_irq->nested_irq_lock);
  77. return ret;
  78. }
  79. static int aqt_irq_probe(struct platform_device *pdev)
  80. {
  81. int irq;
  82. struct aqt1000_irq *aqt_irq = NULL;
  83. int ret = -EINVAL;
  84. irq = platform_get_irq_byname(pdev, "aqt-int");
  85. if (irq < 0) {
  86. dev_err(&pdev->dev, "%s: Couldn't find aqt-int node(%d)\n",
  87. __func__, irq);
  88. return -EINVAL;
  89. }
  90. aqt_irq = kzalloc(sizeof(*aqt_irq), GFP_KERNEL);
  91. if (!aqt_irq)
  92. return -ENOMEM;
  93. /*
  94. * AQT interrupt controller supports N to N irq mapping with
  95. * single cell binding with irq numbers(offsets) only.
  96. * Use irq_domain_simple_ops that has irq_domain_simple_map and
  97. * irq_domain_xlate_onetwocell.
  98. */
  99. aqt_irq->dev = &pdev->dev;
  100. aqt_irq->domain = irq_domain_add_linear(aqt_irq->dev->of_node,
  101. WSA_NUM_IRQS, &irq_domain_simple_ops,
  102. aqt_irq);
  103. if (!aqt_irq->domain) {
  104. dev_err(&pdev->dev, "%s: domain is NULL\n", __func__);
  105. ret = -ENOMEM;
  106. goto err;
  107. }
  108. aqt_irq->dev = &pdev->dev;
  109. dev_dbg(&pdev->dev, "%s: virq = %d\n", __func__, irq);
  110. aqt_irq->irq = irq;
  111. aqt_irq->num_irq_regs = 2;
  112. aqt_irq->num_irqs = WSA_NUM_IRQS;
  113. ret = aqt_irq_init(aqt_irq);
  114. if (ret < 0) {
  115. dev_err(&pdev->dev, "%s: failed to do irq init %d\n",
  116. __func__, ret);
  117. goto err;
  118. }
  119. return ret;
  120. err:
  121. kfree(aqt_irq);
  122. return ret;
  123. }
  124. static int aqt_irq_remove(struct platform_device *pdev)
  125. {
  126. struct irq_domain *domain;
  127. struct aqt1000_irq *data;
  128. domain = irq_find_host(pdev->dev.of_node);
  129. if (unlikely(!domain)) {
  130. pr_err("%s: domain is NULL\n", __func__);
  131. return -EINVAL;
  132. }
  133. data = (struct aqt_irq *)domain->host_data;
  134. data->irq = 0;
  135. return 0;
  136. }
  137. static const struct of_device_id of_match[] = {
  138. { .compatible = "qcom,aqt-irq" },
  139. { }
  140. };
  141. static struct platform_driver aqt_irq_driver = {
  142. .probe = aqt_irq_probe,
  143. .remove = aqt_irq_remove,
  144. .driver = {
  145. .name = "aqt_intc",
  146. .owner = THIS_MODULE,
  147. .of_match_table = of_match_ptr(of_match),
  148. },
  149. };
  150. static int aqt_irq_drv_init(void)
  151. {
  152. return platform_driver_register(&aqt_irq_driver);
  153. }
  154. subsys_initcall(aqt_irq_drv_init);
  155. static void aqt_irq_drv_exit(void)
  156. {
  157. platform_driver_unregister(&aqt_irq_driver);
  158. }
  159. module_exit(aqt_irq_drv_exit);
  160. MODULE_DESCRIPTION("AQT1000 IRQ driver");
  161. MODULE_LICENSE("GPL v2");