aqt1000-irq.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2018, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/irq.h>
  7. #include <linux/delay.h>
  8. #include <linux/gpio.h>
  9. #include <linux/of.h>
  10. #include <linux/of_irq.h>
  11. #include <linux/slab.h>
  12. #include <linux/ratelimit.h>
  13. #include <linux/irqdomain.h>
  14. #include <linux/regmap.h>
  15. #include <linux/pm_runtime.h>
  16. #include "pdata.h"
  17. #include "aqt1000.h"
  18. #include "aqt1000-registers.h"
  19. #include "aqt1000-irq.h"
  20. static const struct regmap_irq aqt1000_irqs[AQT1000_NUM_IRQS] = {
  21. REGMAP_IRQ_REG(AQT1000_IRQ_MBHC_BUTTON_RELEASE_DET, 0, 0x01),
  22. REGMAP_IRQ_REG(AQT1000_IRQ_MBHC_BUTTON_PRESS_DET, 0, 0x02),
  23. REGMAP_IRQ_REG(AQT1000_IRQ_MBHC_ELECT_INS_REM_DET, 0, 0x04),
  24. REGMAP_IRQ_REG(AQT1000_IRQ_MBHC_ELECT_INS_REM_LEG_DET, 0, 0x08),
  25. REGMAP_IRQ_REG(AQT1000_IRQ_MBHC_SW_DET, 0, 0x10),
  26. REGMAP_IRQ_REG(AQT1000_IRQ_HPH_PA_OCPL_FAULT, 0, 0x20),
  27. REGMAP_IRQ_REG(AQT1000_IRQ_HPH_PA_OCPR_FAULT, 0, 0x40),
  28. REGMAP_IRQ_REG(AQT1000_IRQ_HPH_PA_CNPL_COMPLETE, 0, 0x80),
  29. REGMAP_IRQ_REG(AQT1000_IRQ_HPH_PA_CNPR_COMPLETE, 1, 0x01),
  30. REGMAP_IRQ_REG(AQT1000_CDC_HPHL_SURGE, 1, 0x02),
  31. REGMAP_IRQ_REG(AQT1000_CDC_HPHR_SURGE, 1, 0x04),
  32. };
  33. static const struct regmap_irq_chip aqt_regmap_irq_chip = {
  34. .name = "AQT1000",
  35. .irqs = aqt1000_irqs,
  36. .num_irqs = ARRAY_SIZE(aqt1000_irqs),
  37. .num_regs = 2,
  38. .status_base = AQT1000_INTR_CTRL_INT_STATUS_2,
  39. .mask_base = AQT1000_INTR_CTRL_INT_MASK_2,
  40. .unmask_base = AQT1000_INTR_CTRL_INT_CLEAR_2,
  41. .ack_base = AQT1000_INTR_CTRL_INT_STATUS_2,
  42. .runtime_pm = true,
  43. };
  44. static int aqt_map_irq(struct aqt1000 *aqt, int irq)
  45. {
  46. return regmap_irq_get_virq(aqt->irq_chip, irq);
  47. }
  48. /**
  49. * aqt_request_irq: Request a thread handler for the given IRQ
  50. * @aqt: pointer to aqt1000 structure
  51. * @irq: irq number
  52. * @name: name for the IRQ thread
  53. * @handler: irq handler
  54. * @data: data pointer
  55. *
  56. * Returns 0 on success or error on failure
  57. */
  58. int aqt_request_irq(struct aqt1000 *aqt, int irq, const char *name,
  59. irq_handler_t handler, void *data)
  60. {
  61. irq = aqt_map_irq(aqt, irq);
  62. if (irq < 0)
  63. return irq;
  64. return request_threaded_irq(irq, NULL, handler,
  65. IRQF_ONESHOT | IRQF_TRIGGER_RISING,
  66. name, data);
  67. }
  68. EXPORT_SYMBOL(aqt_request_irq);
  69. /**
  70. * aqt_free_irq: Free the IRQ resources allocated during request_irq
  71. * @aqt: pointer to aqt1000 structure
  72. * @irq: irq number
  73. * @data: data pointer
  74. */
  75. void aqt_free_irq(struct aqt1000 *aqt, int irq, void *data)
  76. {
  77. irq = aqt_map_irq(aqt, irq);
  78. if (irq < 0)
  79. return;
  80. free_irq(irq, data);
  81. }
  82. EXPORT_SYMBOL(aqt_free_irq);
  83. /**
  84. * aqt_enable_irq: Enable the given IRQ
  85. * @aqt: pointer to aqt1000 structure
  86. * @irq: irq number
  87. */
  88. void aqt_enable_irq(struct aqt1000 *aqt, int irq)
  89. {
  90. if (aqt)
  91. enable_irq(aqt_map_irq(aqt, irq));
  92. }
  93. EXPORT_SYMBOL(aqt_enable_irq);
  94. /**
  95. * aqt_disable_irq: Disable the given IRQ
  96. * @aqt: pointer to aqt1000 structure
  97. * @irq: irq number
  98. */
  99. void aqt_disable_irq(struct aqt1000 *aqt, int irq)
  100. {
  101. if (aqt)
  102. disable_irq(aqt_map_irq(aqt, irq));
  103. }
  104. EXPORT_SYMBOL(aqt_disable_irq);
  105. static irqreturn_t aqt_irq_thread(int irq, void *data)
  106. {
  107. int ret = 0;
  108. u8 sts[2];
  109. struct aqt1000 *aqt = data;
  110. int num_irq_regs = aqt->num_irq_regs;
  111. struct aqt1000_pdata *pdata;
  112. pdata = dev_get_platdata(aqt->dev);
  113. memset(sts, 0, sizeof(sts));
  114. ret = regmap_bulk_read(aqt->regmap, AQT1000_INTR_CTRL_INT_STATUS_2,
  115. sts, num_irq_regs);
  116. if (ret < 0) {
  117. dev_err(aqt->dev, "%s: Failed to read intr status: %d\n",
  118. __func__, ret);
  119. } else if (ret == 0) {
  120. while (gpio_get_value_cansleep(pdata->irq_gpio))
  121. handle_nested_irq(irq_find_mapping(aqt->virq, 0));
  122. }
  123. return IRQ_HANDLED;
  124. }
  125. static void aqt_irq_disable(struct irq_data *data)
  126. {
  127. }
  128. static void aqt_irq_enable(struct irq_data *data)
  129. {
  130. }
  131. static struct irq_chip aqt_irq_chip = {
  132. .name = "AQT",
  133. .irq_disable = aqt_irq_disable,
  134. .irq_enable = aqt_irq_enable,
  135. };
  136. static struct lock_class_key aqt_irq_lock_class;
  137. static struct lock_class_key aqt_irq_lock_requested_class;
  138. static int aqt_irq_map(struct irq_domain *irqd, unsigned int virq,
  139. irq_hw_number_t hw)
  140. {
  141. struct aqt1000 *data = irqd->host_data;
  142. irq_set_chip_data(virq, data);
  143. irq_set_chip_and_handler(virq, &aqt_irq_chip, handle_simple_irq);
  144. irq_set_lockdep_class(virq, &aqt_irq_lock_class,
  145. &aqt_irq_lock_requested_class);
  146. irq_set_nested_thread(virq, 1);
  147. irq_set_noprobe(virq);
  148. return 0;
  149. }
  150. static const struct irq_domain_ops aqt_domain_ops = {
  151. .map = aqt_irq_map,
  152. .xlate = irq_domain_xlate_twocell,
  153. };
  154. /**
  155. * aqt_irq_init: Initializes IRQ module
  156. * @aqt: pointer to aqt1000 structure
  157. *
  158. * Returns 0 on success or error on failure
  159. */
  160. int aqt_irq_init(struct aqt1000 *aqt)
  161. {
  162. int i, ret;
  163. unsigned int flags = IRQF_ONESHOT;
  164. struct irq_data *irq_data;
  165. struct aqt1000_pdata *pdata;
  166. if (!aqt) {
  167. pr_err("%s: Null pointer handle\n", __func__);
  168. return -EINVAL;
  169. }
  170. pdata = dev_get_platdata(aqt->dev);
  171. if (!pdata) {
  172. dev_err(aqt->dev, "%s: Invalid platform data\n", __func__);
  173. return -EINVAL;
  174. }
  175. /* Select default if not defined in DT */
  176. flags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
  177. if (pdata->irq_flags)
  178. flags = pdata->irq_flags;
  179. if (pdata->irq_gpio) {
  180. aqt->irq = gpio_to_irq(pdata->irq_gpio);
  181. ret = devm_gpio_request_one(aqt->dev, pdata->irq_gpio,
  182. GPIOF_IN, "AQT IRQ");
  183. if (ret) {
  184. dev_err(aqt->dev, "%s: Failed to request gpio %d\n",
  185. __func__, ret);
  186. pdata->irq_gpio = 0;
  187. return ret;
  188. }
  189. }
  190. irq_data = irq_get_irq_data(aqt->irq);
  191. if (!irq_data) {
  192. dev_err(aqt->dev, "%s: Invalid IRQ: %d\n",
  193. __func__, aqt->irq);
  194. return -EINVAL;
  195. }
  196. aqt->num_irq_regs = aqt_regmap_irq_chip.num_regs;
  197. for (i = 0; i < aqt->num_irq_regs; i++) {
  198. regmap_write(aqt->regmap,
  199. (AQT1000_INTR_CTRL_INT_TYPE_2 + i), 0);
  200. }
  201. aqt->virq = irq_domain_add_linear(NULL, 1, &aqt_domain_ops, aqt);
  202. if (!aqt->virq) {
  203. dev_err(aqt->dev, "%s: Failed to add IRQ domain\n", __func__);
  204. ret = -EINVAL;
  205. goto err;
  206. }
  207. ret = regmap_add_irq_chip(aqt->regmap,
  208. irq_create_mapping(aqt->virq, 0),
  209. IRQF_ONESHOT, 0, &aqt_regmap_irq_chip,
  210. &aqt->irq_chip);
  211. if (ret) {
  212. dev_err(aqt->dev, "%s: Failed to add IRQs: %d\n",
  213. __func__, ret);
  214. goto err;
  215. }
  216. ret = request_threaded_irq(aqt->irq, NULL, aqt_irq_thread, flags,
  217. "aqt", aqt);
  218. if (ret) {
  219. dev_err(aqt->dev, "%s: failed to register irq: %d\n",
  220. __func__, ret);
  221. goto err_irq;
  222. }
  223. return 0;
  224. err_irq:
  225. regmap_del_irq_chip(irq_create_mapping(aqt->virq, 1), aqt->irq_chip);
  226. err:
  227. return ret;
  228. }
  229. EXPORT_SYMBOL(aqt_irq_init);
  230. /**
  231. * aqt_irq_exit: Uninitialize regmap IRQ and free IRQ resources
  232. * @aqt: pointer to aqt1000 structure
  233. *
  234. * Returns 0 on success or error on failure
  235. */
  236. int aqt_irq_exit(struct aqt1000 *aqt)
  237. {
  238. if (!aqt) {
  239. pr_err("%s: Null pointer handle\n", __func__);
  240. return -EINVAL;
  241. }
  242. regmap_del_irq_chip(irq_create_mapping(aqt->virq, 1), aqt->irq_chip);
  243. free_irq(aqt->irq, aqt);
  244. return 0;
  245. }
  246. EXPORT_SYMBOL(aqt_irq_exit);