twl6040-irq.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/irq.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/mfd/core.h>
  28. #include <linux/mfd/twl6040.h>
  29. struct twl6040_irq_data {
  30. int mask;
  31. int status;
  32. };
  33. static struct twl6040_irq_data twl6040_irqs[] = {
  34. {
  35. .mask = TWL6040_THMSK,
  36. .status = TWL6040_THINT,
  37. },
  38. {
  39. .mask = TWL6040_PLUGMSK,
  40. .status = TWL6040_PLUGINT | TWL6040_UNPLUGINT,
  41. },
  42. {
  43. .mask = TWL6040_HOOKMSK,
  44. .status = TWL6040_HOOKINT,
  45. },
  46. {
  47. .mask = TWL6040_HFMSK,
  48. .status = TWL6040_HFINT,
  49. },
  50. {
  51. .mask = TWL6040_VIBMSK,
  52. .status = TWL6040_VIBINT,
  53. },
  54. {
  55. .mask = TWL6040_READYMSK,
  56. .status = TWL6040_READYINT,
  57. },
  58. };
  59. static inline
  60. struct twl6040_irq_data *irq_to_twl6040_irq(struct twl6040 *twl6040,
  61. int irq)
  62. {
  63. return &twl6040_irqs[irq - twl6040->irq_base];
  64. }
  65. static void twl6040_irq_lock(struct irq_data *data)
  66. {
  67. struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
  68. mutex_lock(&twl6040->irq_mutex);
  69. }
  70. static void twl6040_irq_sync_unlock(struct irq_data *data)
  71. {
  72. struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
  73. /* write back to hardware any change in irq mask */
  74. if (twl6040->irq_masks_cur != twl6040->irq_masks_cache) {
  75. twl6040->irq_masks_cache = twl6040->irq_masks_cur;
  76. twl6040_reg_write(twl6040, TWL6040_REG_INTMR,
  77. twl6040->irq_masks_cur);
  78. }
  79. mutex_unlock(&twl6040->irq_mutex);
  80. }
  81. static void twl6040_irq_enable(struct irq_data *data)
  82. {
  83. struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
  84. struct twl6040_irq_data *irq_data = irq_to_twl6040_irq(twl6040,
  85. data->irq);
  86. twl6040->irq_masks_cur &= ~irq_data->mask;
  87. }
  88. static void twl6040_irq_disable(struct irq_data *data)
  89. {
  90. struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data);
  91. struct twl6040_irq_data *irq_data = irq_to_twl6040_irq(twl6040,
  92. data->irq);
  93. twl6040->irq_masks_cur |= irq_data->mask;
  94. }
  95. static struct irq_chip twl6040_irq_chip = {
  96. .name = "twl6040",
  97. .irq_bus_lock = twl6040_irq_lock,
  98. .irq_bus_sync_unlock = twl6040_irq_sync_unlock,
  99. .irq_enable = twl6040_irq_enable,
  100. .irq_disable = twl6040_irq_disable,
  101. };
  102. static irqreturn_t twl6040_irq_thread(int irq, void *data)
  103. {
  104. struct twl6040 *twl6040 = data;
  105. u8 intid;
  106. int i;
  107. intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
  108. /* apply masking and report (backwards to handle READYINT first) */
  109. for (i = ARRAY_SIZE(twl6040_irqs) - 1; i >= 0; i--) {
  110. if (twl6040->irq_masks_cur & twl6040_irqs[i].mask)
  111. intid &= ~twl6040_irqs[i].status;
  112. if (intid & twl6040_irqs[i].status)
  113. handle_nested_irq(twl6040->irq_base + i);
  114. }
  115. /* ack unmasked irqs */
  116. twl6040_reg_write(twl6040, TWL6040_REG_INTID, intid);
  117. return IRQ_HANDLED;
  118. }
  119. int twl6040_irq_init(struct twl6040 *twl6040)
  120. {
  121. int cur_irq, ret;
  122. u8 val;
  123. mutex_init(&twl6040->irq_mutex);
  124. /* mask the individual interrupt sources */
  125. twl6040->irq_masks_cur = TWL6040_ALLINT_MSK;
  126. twl6040->irq_masks_cache = TWL6040_ALLINT_MSK;
  127. twl6040_reg_write(twl6040, TWL6040_REG_INTMR, TWL6040_ALLINT_MSK);
  128. /* Register them with genirq */
  129. for (cur_irq = twl6040->irq_base;
  130. cur_irq < twl6040->irq_base + ARRAY_SIZE(twl6040_irqs);
  131. cur_irq++) {
  132. irq_set_chip_data(cur_irq, twl6040);
  133. irq_set_chip_and_handler(cur_irq, &twl6040_irq_chip,
  134. handle_level_irq);
  135. irq_set_nested_thread(cur_irq, 1);
  136. /* ARM needs us to explicitly flag the IRQ as valid
  137. * and will set them noprobe when we do so. */
  138. #ifdef CONFIG_ARM
  139. set_irq_flags(cur_irq, IRQF_VALID);
  140. #else
  141. irq_set_noprobe(cur_irq);
  142. #endif
  143. }
  144. ret = request_threaded_irq(twl6040->irq, NULL, twl6040_irq_thread,
  145. IRQF_ONESHOT, "twl6040", twl6040);
  146. if (ret) {
  147. dev_err(twl6040->dev, "failed to request IRQ %d: %d\n",
  148. twl6040->irq, ret);
  149. return ret;
  150. }
  151. /* reset interrupts */
  152. val = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
  153. /* interrupts cleared on write */
  154. twl6040_clear_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_INTCLRMODE);
  155. return 0;
  156. }
  157. EXPORT_SYMBOL(twl6040_irq_init);
  158. void twl6040_irq_exit(struct twl6040 *twl6040)
  159. {
  160. free_irq(twl6040->irq, twl6040);
  161. }
  162. EXPORT_SYMBOL(twl6040_irq_exit);