aqt1000-irq.c 7.1 KB

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