twl6040-irq.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Interrupt controller support for TWL6040
  3. *
  4. * Author: Misael Lopez Cruz <misael.lopez@ti.com>
  5. *
  6. * Copyright: (C) 2011 Texas Instruments, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/err.h>
  26. #include <linux/irq.h>
  27. #include <linux/of.h>
  28. #include <linux/irqdomain.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/mfd/core.h>
  31. #include <linux/mfd/twl6040.h>
  32. struct twl6040_irq_data {
  33. int mask;
  34. int status;
  35. };
  36. static struct twl6040_irq_data twl6040_irqs[] = {
  37. {
  38. .mask = TWL6040_THMSK,
  39. .status = TWL6040_THINT,
  40. },
  41. {
  42. .mask = TWL6040_PLUGMSK,
  43. .status = TWL6040_PLUGINT | TWL6040_UNPLUGINT,
  44. },
  45. {
  46. .mask = TWL6040_HOOKMSK,
  47. .status = TWL6040_HOOKINT,
  48. },
  49. {
  50. .mask = TWL6040_HFMSK,
  51. .status = TWL6040_HFINT,
  52. },
  53. {
  54. .mask = TWL6040_VIBMSK,
  55. .status = TWL6040_VIBINT,
  56. },
  57. {
  58. .mask = TWL6040_READYMSK,
  59. .status = TWL6040_READYINT,
  60. },
  61. };
  62. static inline
  63. struct twl6040_irq_data *irq_to_twl6040_irq(struct twl6040 *twl6040,
  64. int irq)
  65. {
  66. return &twl6040_irqs[irq - twl6040->irq_base];
  67. }
  68. static void twl6040_irq_lock(struct irq_data *data)
  69. {
  70. struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
  71. mutex_lock(&twl6040->irq_mutex);
  72. }
  73. static void twl6040_irq_sync_unlock(struct irq_data *data)
  74. {
  75. struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
  76. /* write back to hardware any change in irq mask */
  77. if (twl6040->irq_masks_cur != twl6040->irq_masks_cache) {
  78. twl6040->irq_masks_cache = twl6040->irq_masks_cur;
  79. twl6040_reg_write(twl6040, TWL6040_REG_INTMR,
  80. twl6040->irq_masks_cur);
  81. }
  82. mutex_unlock(&twl6040->irq_mutex);
  83. }
  84. static void twl6040_irq_enable(struct irq_data *data)
  85. {
  86. struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
  87. struct twl6040_irq_data *irq_data = irq_to_twl6040_irq(twl6040,
  88. data->irq);
  89. twl6040->irq_masks_cur &= ~irq_data->mask;
  90. }
  91. static void twl6040_irq_disable(struct irq_data *data)
  92. {
  93. struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
  94. struct twl6040_irq_data *irq_data = irq_to_twl6040_irq(twl6040,
  95. data->irq);
  96. twl6040->irq_masks_cur |= irq_data->mask;
  97. }
  98. static struct irq_chip twl6040_irq_chip = {
  99. .name = "twl6040",
  100. .irq_bus_lock = twl6040_irq_lock,
  101. .irq_bus_sync_unlock = twl6040_irq_sync_unlock,
  102. .irq_enable = twl6040_irq_enable,
  103. .irq_disable = twl6040_irq_disable,
  104. };
  105. static irqreturn_t twl6040_irq_thread(int irq, void *data)
  106. {
  107. struct twl6040 *twl6040 = data;
  108. u8 intid;
  109. int i;
  110. intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
  111. /* apply masking and report (backwards to handle READYINT first) */
  112. for (i = ARRAY_SIZE(twl6040_irqs) - 1; i >= 0; i--) {
  113. if (twl6040->irq_masks_cur & twl6040_irqs[i].mask)
  114. intid &= ~twl6040_irqs[i].status;
  115. if (intid & twl6040_irqs[i].status)
  116. handle_nested_irq(twl6040->irq_base + i);
  117. }
  118. /* ack unmasked irqs */
  119. twl6040_reg_write(twl6040, TWL6040_REG_INTID, intid);
  120. return IRQ_HANDLED;
  121. }
  122. int twl6040_irq_init(struct twl6040 *twl6040)
  123. {
  124. struct device_node *node = twl6040->dev->of_node;
  125. int i, nr_irqs, irq_base, ret;
  126. u8 val;
  127. mutex_init(&twl6040->irq_mutex);
  128. /* mask the individual interrupt sources */
  129. twl6040->irq_masks_cur = TWL6040_ALLINT_MSK;
  130. twl6040->irq_masks_cache = TWL6040_ALLINT_MSK;
  131. twl6040_reg_write(twl6040, TWL6040_REG_INTMR, TWL6040_ALLINT_MSK);
  132. nr_irqs = ARRAY_SIZE(twl6040_irqs);
  133. irq_base = irq_alloc_descs(-1, 0, nr_irqs, 0);
  134. if (IS_ERR_VALUE(irq_base)) {
  135. dev_err(twl6040->dev, "Fail to allocate IRQ descs\n");
  136. return irq_base;
  137. }
  138. twl6040->irq_base = irq_base;
  139. irq_domain_add_legacy(node, ARRAY_SIZE(twl6040_irqs), irq_base, 0,
  140. &irq_domain_simple_ops, NULL);
  141. /* Register them with genirq */
  142. for (i = irq_base; i < irq_base + nr_irqs; i++) {
  143. irq_set_chip_data(i, twl6040);
  144. irq_set_chip_and_handler(i, &twl6040_irq_chip,
  145. handle_level_irq);
  146. irq_set_nested_thread(i, 1);
  147. /* ARM needs us to explicitly flag the IRQ as valid
  148. * and will set them noprobe when we do so. */
  149. #ifdef CONFIG_ARM
  150. set_irq_flags(i, IRQF_VALID);
  151. #else
  152. irq_set_noprobe(i);
  153. #endif
  154. }
  155. ret = request_threaded_irq(twl6040->irq, NULL, twl6040_irq_thread,
  156. IRQF_ONESHOT, "twl6040", twl6040);
  157. if (ret) {
  158. dev_err(twl6040->dev, "failed to request IRQ %d: %d\n",
  159. twl6040->irq, ret);
  160. return ret;
  161. }
  162. /* reset interrupts */
  163. val = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
  164. /* interrupts cleared on write */
  165. twl6040_clear_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_INTCLRMODE);
  166. return 0;
  167. }
  168. EXPORT_SYMBOL(twl6040_irq_init);
  169. void twl6040_irq_exit(struct twl6040 *twl6040)
  170. {
  171. free_irq(twl6040->irq, twl6040);
  172. }
  173. EXPORT_SYMBOL(twl6040_irq_exit);