lp8788-irq.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * TI LP8788 MFD - interrupt handler
  3. *
  4. * Copyright 2012 Texas Instruments
  5. *
  6. * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
  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. */
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/irqdomain.h>
  18. #include <linux/device.h>
  19. #include <linux/mfd/lp8788.h>
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. /* register address */
  23. #define LP8788_INT_1 0x00
  24. #define LP8788_INTEN_1 0x03
  25. #define BASE_INTEN_ADDR LP8788_INTEN_1
  26. #define SIZE_REG 8
  27. #define NUM_REGS 3
  28. /*
  29. * struct lp8788_irq_data
  30. * @lp : used for accessing to lp8788 registers
  31. * @irq_lock : mutex for enabling/disabling the interrupt
  32. * @domain : IRQ domain for handling nested interrupt
  33. * @enabled : status of enabled interrupt
  34. */
  35. struct lp8788_irq_data {
  36. struct lp8788 *lp;
  37. struct mutex irq_lock;
  38. struct irq_domain *domain;
  39. int enabled[LP8788_INT_MAX];
  40. };
  41. static inline u8 _irq_to_addr(enum lp8788_int_id id)
  42. {
  43. return id / SIZE_REG;
  44. }
  45. static inline u8 _irq_to_enable_addr(enum lp8788_int_id id)
  46. {
  47. return _irq_to_addr(id) + BASE_INTEN_ADDR;
  48. }
  49. static inline u8 _irq_to_mask(enum lp8788_int_id id)
  50. {
  51. return 1 << (id % SIZE_REG);
  52. }
  53. static inline u8 _irq_to_val(enum lp8788_int_id id, int enable)
  54. {
  55. return enable << (id % SIZE_REG);
  56. }
  57. static void lp8788_irq_enable(struct irq_data *data)
  58. {
  59. struct lp8788_irq_data *irqd = irq_data_get_irq_chip_data(data);
  60. irqd->enabled[data->hwirq] = 1;
  61. }
  62. static void lp8788_irq_disable(struct irq_data *data)
  63. {
  64. struct lp8788_irq_data *irqd = irq_data_get_irq_chip_data(data);
  65. irqd->enabled[data->hwirq] = 0;
  66. }
  67. static void lp8788_irq_bus_lock(struct irq_data *data)
  68. {
  69. struct lp8788_irq_data *irqd = irq_data_get_irq_chip_data(data);
  70. mutex_lock(&irqd->irq_lock);
  71. }
  72. static void lp8788_irq_bus_sync_unlock(struct irq_data *data)
  73. {
  74. struct lp8788_irq_data *irqd = irq_data_get_irq_chip_data(data);
  75. enum lp8788_int_id irq = data->hwirq;
  76. u8 addr, mask, val;
  77. addr = _irq_to_enable_addr(irq);
  78. mask = _irq_to_mask(irq);
  79. val = _irq_to_val(irq, irqd->enabled[irq]);
  80. lp8788_update_bits(irqd->lp, addr, mask, val);
  81. mutex_unlock(&irqd->irq_lock);
  82. }
  83. static struct irq_chip lp8788_irq_chip = {
  84. .name = "lp8788",
  85. .irq_enable = lp8788_irq_enable,
  86. .irq_disable = lp8788_irq_disable,
  87. .irq_bus_lock = lp8788_irq_bus_lock,
  88. .irq_bus_sync_unlock = lp8788_irq_bus_sync_unlock,
  89. };
  90. static irqreturn_t lp8788_irq_handler(int irq, void *ptr)
  91. {
  92. struct lp8788_irq_data *irqd = ptr;
  93. struct lp8788 *lp = irqd->lp;
  94. u8 status[NUM_REGS], addr, mask;
  95. bool handled = false;
  96. int i;
  97. if (lp8788_read_multi_bytes(lp, LP8788_INT_1, status, NUM_REGS))
  98. return IRQ_NONE;
  99. for (i = 0 ; i < LP8788_INT_MAX ; i++) {
  100. addr = _irq_to_addr(i);
  101. mask = _irq_to_mask(i);
  102. /* reporting only if the irq is enabled */
  103. if (status[addr] & mask) {
  104. handle_nested_irq(irq_find_mapping(irqd->domain, i));
  105. handled = true;
  106. }
  107. }
  108. return handled ? IRQ_HANDLED : IRQ_NONE;
  109. }
  110. static int lp8788_irq_map(struct irq_domain *d, unsigned int virq,
  111. irq_hw_number_t hwirq)
  112. {
  113. struct lp8788_irq_data *irqd = d->host_data;
  114. struct irq_chip *chip = &lp8788_irq_chip;
  115. irq_set_chip_data(virq, irqd);
  116. irq_set_chip_and_handler(virq, chip, handle_edge_irq);
  117. irq_set_nested_thread(virq, 1);
  118. irq_set_noprobe(virq);
  119. return 0;
  120. }
  121. static const struct irq_domain_ops lp8788_domain_ops = {
  122. .map = lp8788_irq_map,
  123. };
  124. int lp8788_irq_init(struct lp8788 *lp, int irq)
  125. {
  126. struct lp8788_irq_data *irqd;
  127. int ret;
  128. if (irq <= 0) {
  129. dev_warn(lp->dev, "invalid irq number: %d\n", irq);
  130. return 0;
  131. }
  132. irqd = devm_kzalloc(lp->dev, sizeof(*irqd), GFP_KERNEL);
  133. if (!irqd)
  134. return -ENOMEM;
  135. irqd->lp = lp;
  136. irqd->domain = irq_domain_add_linear(lp->dev->of_node, LP8788_INT_MAX,
  137. &lp8788_domain_ops, irqd);
  138. if (!irqd->domain) {
  139. dev_err(lp->dev, "failed to add irq domain err\n");
  140. return -EINVAL;
  141. }
  142. lp->irqdm = irqd->domain;
  143. mutex_init(&irqd->irq_lock);
  144. ret = request_threaded_irq(irq, NULL, lp8788_irq_handler,
  145. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  146. "lp8788-irq", irqd);
  147. if (ret) {
  148. dev_err(lp->dev, "failed to create a thread for IRQ_N\n");
  149. return ret;
  150. }
  151. lp->irq = irq;
  152. return 0;
  153. }
  154. void lp8788_irq_exit(struct lp8788 *lp)
  155. {
  156. if (lp->irq)
  157. free_irq(lp->irq, lp->irqdm);
  158. }