s2mpb02-irq.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * s2mpb02-irq.c - Interrupt controller support for S2MPB02
  3. *
  4. * Copyright (C) 2014 Samsung Electronics Co.Ltd
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * This driver is based on max77804-irq.c
  21. */
  22. #include <linux/err.h>
  23. #include <linux/irq.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/gpio.h>
  26. #include <linux/mfd/samsung/s2mpb02.h>
  27. #include <linux/mfd/samsung/s2mpb02-regulator.h>
  28. static const u8 s2mpb02_mask_reg[] = {
  29. [LED_INT] = S2MPB02_REG_INT1M,
  30. };
  31. struct s2mpb02_irq_data {
  32. int mask;
  33. enum s2mpb02_irq_source group;
  34. };
  35. static const struct s2mpb02_irq_data s2mpb02_irqs[] = {
  36. [S2MPB02_LED_IRQ_IRLED_END] = {
  37. .group = LED_INT,
  38. .mask = 1 << 5
  39. },
  40. };
  41. static void s2mpb02_irq_lock(struct irq_data *data)
  42. {
  43. struct s2mpb02_dev *s2mpb02 = irq_get_chip_data(data->irq);
  44. mutex_lock(&s2mpb02->irqlock);
  45. }
  46. static void s2mpb02_irq_sync_unlock(struct irq_data *data)
  47. {
  48. struct s2mpb02_dev *s2mpb02 = irq_get_chip_data(data->irq);
  49. int i;
  50. for (i = 0; i < S2MPB02_IRQ_GROUP_NR; i++) {
  51. u8 mask_reg = s2mpb02_mask_reg[i];
  52. struct i2c_client *i2c = s2mpb02->i2c;
  53. if (mask_reg == S2MPB02_REG_INVALID || IS_ERR_OR_NULL(i2c))
  54. continue;
  55. s2mpb02->irq_masks_cache[i] = s2mpb02->irq_masks_cur[i];
  56. s2mpb02_write_reg(i2c, s2mpb02_mask_reg[i],
  57. s2mpb02->irq_masks_cur[i]);
  58. }
  59. mutex_unlock(&s2mpb02->irqlock);
  60. }
  61. static const inline struct s2mpb02_irq_data *
  62. irq_to_s2mpb02_irq(struct s2mpb02_dev *s2mpb02, int irq)
  63. {
  64. return &s2mpb02_irqs[irq - s2mpb02->irq_base];
  65. }
  66. static void s2mpb02_irq_mask(struct irq_data *data)
  67. {
  68. struct s2mpb02_dev *s2mpb02 = irq_get_chip_data(data->irq);
  69. const struct s2mpb02_irq_data *irq_data =
  70. irq_to_s2mpb02_irq(s2mpb02, data->irq);
  71. if (irq_data->group >= S2MPB02_IRQ_GROUP_NR)
  72. return;
  73. s2mpb02->irq_masks_cur[irq_data->group] |= irq_data->mask;
  74. }
  75. static void s2mpb02_irq_unmask(struct irq_data *data)
  76. {
  77. struct s2mpb02_dev *s2mpb02 = irq_get_chip_data(data->irq);
  78. const struct s2mpb02_irq_data *irq_data =
  79. irq_to_s2mpb02_irq(s2mpb02, data->irq);
  80. if (irq_data->group >= S2MPB02_IRQ_GROUP_NR)
  81. return;
  82. s2mpb02->irq_masks_cur[irq_data->group] &= ~irq_data->mask;
  83. }
  84. static struct irq_chip s2mpb02_irq_chip = {
  85. .name = MFD_DEV_NAME,
  86. .irq_bus_lock = s2mpb02_irq_lock,
  87. .irq_bus_sync_unlock = s2mpb02_irq_sync_unlock,
  88. .irq_mask = s2mpb02_irq_mask,
  89. .irq_unmask = s2mpb02_irq_unmask,
  90. };
  91. static irqreturn_t s2mpb02_irq_thread(int irq, void *data)
  92. {
  93. struct s2mpb02_dev *s2mpb02 = data;
  94. u8 irq_reg[S2MPB02_IRQ_GROUP_NR] = {0};
  95. int i, ret;
  96. pr_debug("%s: irq gpio pre-state(0x%02x)\n", __func__,
  97. gpio_get_value(s2mpb02->irq_gpio));
  98. /* LED_INT */
  99. ret = s2mpb02_read_reg(s2mpb02->i2c,
  100. S2MPB02_REG_INT1, &irq_reg[LED_INT]);
  101. pr_info("%s: led interrupt(0x%02hhx)\n",
  102. __func__, irq_reg[LED_INT]);
  103. pr_debug("%s: irq gpio post-state(0x%02x)\n", __func__,
  104. gpio_get_value(s2mpb02->irq_gpio));
  105. /* Apply masking */
  106. for (i = 0; i < S2MPB02_IRQ_GROUP_NR; i++)
  107. irq_reg[i] &= ~s2mpb02->irq_masks_cur[i];
  108. /* Report */
  109. for (i = 0; i < S2MPB02_IRQ_NR; i++) {
  110. if (irq_reg[s2mpb02_irqs[i].group] & s2mpb02_irqs[i].mask)
  111. handle_nested_irq(s2mpb02->irq_base + i);
  112. }
  113. return IRQ_HANDLED;
  114. }
  115. int s2mpb02_irq_init(struct s2mpb02_dev *s2mpb02)
  116. {
  117. int i;
  118. int ret;
  119. if (!s2mpb02->irq_gpio) {
  120. pr_warn("%s:%s No interrupt specified.\n",
  121. MFD_DEV_NAME, __func__);
  122. s2mpb02->irq_base = 0;
  123. return 0;
  124. }
  125. if (s2mpb02->irq_base < 0) {
  126. pr_err("%s:%s No interrupt base specified.\n",
  127. MFD_DEV_NAME, __func__);
  128. return 0;
  129. }
  130. mutex_init(&s2mpb02->irqlock);
  131. s2mpb02->irq = gpio_to_irq(s2mpb02->irq_gpio);
  132. pr_info("%s:%s irq=%d, irq->gpio=%d\n", MFD_DEV_NAME, __func__,
  133. s2mpb02->irq, s2mpb02->irq_gpio);
  134. ret = gpio_request(s2mpb02->irq_gpio, "sub_pmic_irq");
  135. if (ret) {
  136. pr_err("%s:%s failed requesting gpio %d\n",
  137. MFD_DEV_NAME, __func__, s2mpb02->irq_gpio);
  138. goto err;
  139. }
  140. gpio_direction_input(s2mpb02->irq_gpio);
  141. gpio_free(s2mpb02->irq_gpio);
  142. /* Mask interrupt sources */
  143. for (i = 0; i < S2MPB02_IRQ_GROUP_NR; i++) {
  144. struct i2c_client *i2c;
  145. s2mpb02->irq_masks_cur[i] = 0xff;
  146. s2mpb02->irq_masks_cache[i] = 0xff;
  147. i2c = s2mpb02->i2c;
  148. if (IS_ERR_OR_NULL(i2c))
  149. continue;
  150. if (s2mpb02_mask_reg[i] == S2MPB02_REG_INVALID)
  151. continue;
  152. s2mpb02_write_reg(i2c, s2mpb02_mask_reg[i], 0xff);
  153. }
  154. /* Register with genirq */
  155. for (i = 0; i < S2MPB02_IRQ_NR; i++) {
  156. int cur_irq = i + s2mpb02->irq_base;
  157. ret = irq_set_chip_data(cur_irq, s2mpb02);
  158. if (ret) {
  159. dev_err(s2mpb02->dev,
  160. "Failed to irq_set_chip_data %d: %d\n",
  161. s2mpb02->irq, ret);
  162. goto err;
  163. }
  164. irq_set_chip_and_handler(cur_irq, &s2mpb02_irq_chip,
  165. handle_edge_irq);
  166. irq_set_nested_thread(cur_irq, 1);
  167. #ifdef CONFIG_ARM
  168. set_irq_flags(cur_irq, IRQF_VALID);
  169. #else
  170. irq_set_noprobe(cur_irq);
  171. #endif
  172. }
  173. ret = request_threaded_irq(s2mpb02->irq, NULL, s2mpb02_irq_thread,
  174. IRQF_TRIGGER_LOW | IRQF_ONESHOT, "s2mpb02-irq", s2mpb02);
  175. if (ret) {
  176. pr_err("%s:%s Failed to request IRQ %d: %d\n", MFD_DEV_NAME,
  177. __func__, s2mpb02->irq, ret);
  178. goto err;
  179. }
  180. return 0;
  181. err:
  182. mutex_destroy(&s2mpb02->i2c_lock);
  183. return ret;
  184. }
  185. void s2mpb02_irq_exit(struct s2mpb02_dev *s2mpb02)
  186. {
  187. if (s2mpb02->irq)
  188. free_irq(s2mpb02->irq, s2mpb02);
  189. }