tps65910-irq.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * tps65910-irq.c -- TI TPS6591x
  3. *
  4. * Copyright 2010 Texas Instruments Inc.
  5. *
  6. * Author: Graeme Gregory <gg@slimlogic.co.uk>
  7. * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/bug.h>
  19. #include <linux/device.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/gpio.h>
  23. #include <linux/mfd/tps65910.h>
  24. static inline int irq_to_tps65910_irq(struct tps65910 *tps65910,
  25. int irq)
  26. {
  27. return (irq - tps65910->irq_base);
  28. }
  29. /*
  30. * This is a threaded IRQ handler so can access I2C/SPI. Since all
  31. * interrupts are clear on read the IRQ line will be reasserted and
  32. * the physical IRQ will be handled again if another interrupt is
  33. * asserted while we run - in the normal course of events this is a
  34. * rare occurrence so we save I2C/SPI reads. We're also assuming that
  35. * it's rare to get lots of interrupts firing simultaneously so try to
  36. * minimise I/O.
  37. */
  38. static irqreturn_t tps65910_irq(int irq, void *irq_data)
  39. {
  40. struct tps65910 *tps65910 = irq_data;
  41. u32 irq_sts;
  42. u32 irq_mask;
  43. u8 reg;
  44. int i;
  45. tps65910->read(tps65910, TPS65910_INT_STS, 1, &reg);
  46. irq_sts = reg;
  47. tps65910->read(tps65910, TPS65910_INT_STS2, 1, &reg);
  48. irq_sts |= reg << 8;
  49. switch (tps65910_chip_id(tps65910)) {
  50. case TPS65911:
  51. tps65910->read(tps65910, TPS65910_INT_STS3, 1, &reg);
  52. irq_sts |= reg << 16;
  53. }
  54. tps65910->read(tps65910, TPS65910_INT_MSK, 1, &reg);
  55. irq_mask = reg;
  56. tps65910->read(tps65910, TPS65910_INT_MSK2, 1, &reg);
  57. irq_mask |= reg << 8;
  58. switch (tps65910_chip_id(tps65910)) {
  59. case TPS65911:
  60. tps65910->read(tps65910, TPS65910_INT_MSK3, 1, &reg);
  61. irq_mask |= reg << 16;
  62. }
  63. irq_sts &= ~irq_mask;
  64. if (!irq_sts)
  65. return IRQ_NONE;
  66. for (i = 0; i < tps65910->irq_num; i++) {
  67. if (!(irq_sts & (1 << i)))
  68. continue;
  69. handle_nested_irq(tps65910->irq_base + i);
  70. }
  71. /* Write the STS register back to clear IRQs we handled */
  72. reg = irq_sts & 0xFF;
  73. irq_sts >>= 8;
  74. tps65910->write(tps65910, TPS65910_INT_STS, 1, &reg);
  75. reg = irq_sts & 0xFF;
  76. tps65910->write(tps65910, TPS65910_INT_STS2, 1, &reg);
  77. switch (tps65910_chip_id(tps65910)) {
  78. case TPS65911:
  79. reg = irq_sts >> 8;
  80. tps65910->write(tps65910, TPS65910_INT_STS3, 1, &reg);
  81. }
  82. return IRQ_HANDLED;
  83. }
  84. static void tps65910_irq_lock(struct irq_data *data)
  85. {
  86. struct tps65910 *tps65910 = irq_data_get_irq_chip_data(data);
  87. mutex_lock(&tps65910->irq_lock);
  88. }
  89. static void tps65910_irq_sync_unlock(struct irq_data *data)
  90. {
  91. struct tps65910 *tps65910 = irq_data_get_irq_chip_data(data);
  92. u32 reg_mask;
  93. u8 reg;
  94. tps65910->read(tps65910, TPS65910_INT_MSK, 1, &reg);
  95. reg_mask = reg;
  96. tps65910->read(tps65910, TPS65910_INT_MSK2, 1, &reg);
  97. reg_mask |= reg << 8;
  98. switch (tps65910_chip_id(tps65910)) {
  99. case TPS65911:
  100. tps65910->read(tps65910, TPS65910_INT_MSK3, 1, &reg);
  101. reg_mask |= reg << 16;
  102. }
  103. if (tps65910->irq_mask != reg_mask) {
  104. reg = tps65910->irq_mask & 0xFF;
  105. tps65910->write(tps65910, TPS65910_INT_MSK, 1, &reg);
  106. reg = tps65910->irq_mask >> 8 & 0xFF;
  107. tps65910->write(tps65910, TPS65910_INT_MSK2, 1, &reg);
  108. switch (tps65910_chip_id(tps65910)) {
  109. case TPS65911:
  110. reg = tps65910->irq_mask >> 16;
  111. tps65910->write(tps65910, TPS65910_INT_MSK3, 1, &reg);
  112. }
  113. }
  114. mutex_unlock(&tps65910->irq_lock);
  115. }
  116. static void tps65910_irq_enable(struct irq_data *data)
  117. {
  118. struct tps65910 *tps65910 = irq_data_get_irq_chip_data(data);
  119. tps65910->irq_mask &= ~( 1 << irq_to_tps65910_irq(tps65910, data->irq));
  120. }
  121. static void tps65910_irq_disable(struct irq_data *data)
  122. {
  123. struct tps65910 *tps65910 = irq_data_get_irq_chip_data(data);
  124. tps65910->irq_mask |= ( 1 << irq_to_tps65910_irq(tps65910, data->irq));
  125. }
  126. #ifdef CONFIG_PM_SLEEP
  127. static int tps65910_irq_set_wake(struct irq_data *data, unsigned int enable)
  128. {
  129. struct tps65910 *tps65910 = irq_data_get_irq_chip_data(data);
  130. return irq_set_irq_wake(tps65910->chip_irq, enable);
  131. }
  132. #else
  133. #define tps65910_irq_set_wake NULL
  134. #endif
  135. static struct irq_chip tps65910_irq_chip = {
  136. .name = "tps65910",
  137. .irq_bus_lock = tps65910_irq_lock,
  138. .irq_bus_sync_unlock = tps65910_irq_sync_unlock,
  139. .irq_disable = tps65910_irq_disable,
  140. .irq_enable = tps65910_irq_enable,
  141. .irq_set_wake = tps65910_irq_set_wake,
  142. };
  143. int tps65910_irq_init(struct tps65910 *tps65910, int irq,
  144. struct tps65910_platform_data *pdata)
  145. {
  146. int ret, cur_irq;
  147. int flags = IRQF_ONESHOT;
  148. if (!irq) {
  149. dev_warn(tps65910->dev, "No interrupt support, no core IRQ\n");
  150. return -EINVAL;
  151. }
  152. if (!pdata || !pdata->irq_base) {
  153. dev_warn(tps65910->dev, "No interrupt support, no IRQ base\n");
  154. return -EINVAL;
  155. }
  156. tps65910->irq_mask = 0xFFFFFF;
  157. mutex_init(&tps65910->irq_lock);
  158. tps65910->chip_irq = irq;
  159. tps65910->irq_base = pdata->irq_base;
  160. switch (tps65910_chip_id(tps65910)) {
  161. case TPS65910:
  162. tps65910->irq_num = TPS65910_NUM_IRQ;
  163. break;
  164. case TPS65911:
  165. tps65910->irq_num = TPS65911_NUM_IRQ;
  166. break;
  167. }
  168. /* Register with genirq */
  169. for (cur_irq = tps65910->irq_base;
  170. cur_irq < tps65910->irq_num + tps65910->irq_base;
  171. cur_irq++) {
  172. irq_set_chip_data(cur_irq, tps65910);
  173. irq_set_chip_and_handler(cur_irq, &tps65910_irq_chip,
  174. handle_edge_irq);
  175. irq_set_nested_thread(cur_irq, 1);
  176. /* ARM needs us to explicitly flag the IRQ as valid
  177. * and will set them noprobe when we do so. */
  178. #ifdef CONFIG_ARM
  179. set_irq_flags(cur_irq, IRQF_VALID);
  180. #else
  181. irq_set_noprobe(cur_irq);
  182. #endif
  183. }
  184. ret = request_threaded_irq(irq, NULL, tps65910_irq, flags,
  185. "tps65910", tps65910);
  186. irq_set_irq_type(irq, IRQ_TYPE_LEVEL_LOW);
  187. if (ret != 0)
  188. dev_err(tps65910->dev, "Failed to request IRQ: %d\n", ret);
  189. return ret;
  190. }
  191. int tps65910_irq_exit(struct tps65910 *tps65910)
  192. {
  193. if (tps65910->chip_irq)
  194. free_irq(tps65910->chip_irq, tps65910);
  195. return 0;
  196. }