max77693-irq.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * max77693-irq.c - Interrupt controller support for MAX77693
  3. *
  4. * Copyright (C) 2012 Samsung Electronics Co.Ltd
  5. * SangYoung Son <hello.son@samsung.com>
  6. *
  7. * This program is not provided / owned by Maxim Integrated Products.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * This driver is based on max8997-irq.c
  24. */
  25. #include <linux/err.h>
  26. #include <linux/irq.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/module.h>
  29. #include <linux/irqdomain.h>
  30. #include <linux/mfd/max77693.h>
  31. #include <linux/mfd/max77693-private.h>
  32. static const u8 max77693_mask_reg[] = {
  33. [LED_INT] = MAX77693_LED_REG_FLASH_INT_MASK,
  34. [TOPSYS_INT] = MAX77693_PMIC_REG_TOPSYS_INT_MASK,
  35. [CHG_INT] = MAX77693_CHG_REG_CHG_INT_MASK,
  36. [MUIC_INT1] = MAX77693_MUIC_REG_INTMASK1,
  37. [MUIC_INT2] = MAX77693_MUIC_REG_INTMASK2,
  38. [MUIC_INT3] = MAX77693_MUIC_REG_INTMASK3,
  39. };
  40. static struct regmap *max77693_get_regmap(struct max77693_dev *max77693,
  41. enum max77693_irq_source src)
  42. {
  43. switch (src) {
  44. case LED_INT ... CHG_INT:
  45. return max77693->regmap;
  46. case MUIC_INT1 ... MUIC_INT3:
  47. return max77693->regmap_muic;
  48. default:
  49. return ERR_PTR(-EINVAL);
  50. }
  51. }
  52. struct max77693_irq_data {
  53. int mask;
  54. enum max77693_irq_source group;
  55. };
  56. #define DECLARE_IRQ(idx, _group, _mask) \
  57. [(idx)] = { .group = (_group), .mask = (_mask) }
  58. static const struct max77693_irq_data max77693_irqs[] = {
  59. DECLARE_IRQ(MAX77693_LED_IRQ_FLED2_OPEN, LED_INT, 1 << 0),
  60. DECLARE_IRQ(MAX77693_LED_IRQ_FLED2_SHORT, LED_INT, 1 << 1),
  61. DECLARE_IRQ(MAX77693_LED_IRQ_FLED1_OPEN, LED_INT, 1 << 2),
  62. DECLARE_IRQ(MAX77693_LED_IRQ_FLED1_SHORT, LED_INT, 1 << 3),
  63. DECLARE_IRQ(MAX77693_LED_IRQ_MAX_FLASH, LED_INT, 1 << 4),
  64. DECLARE_IRQ(MAX77693_TOPSYS_IRQ_T120C_INT, TOPSYS_INT, 1 << 0),
  65. DECLARE_IRQ(MAX77693_TOPSYS_IRQ_T140C_INT, TOPSYS_INT, 1 << 1),
  66. DECLARE_IRQ(MAX77693_TOPSYS_IRQ_LOWSYS_INT, TOPSYS_INT, 1 << 3),
  67. DECLARE_IRQ(MAX77693_CHG_IRQ_BYP_I, CHG_INT, 1 << 0),
  68. DECLARE_IRQ(MAX77693_CHG_IRQ_THM_I, CHG_INT, 1 << 2),
  69. DECLARE_IRQ(MAX77693_CHG_IRQ_BAT_I, CHG_INT, 1 << 3),
  70. DECLARE_IRQ(MAX77693_CHG_IRQ_CHG_I, CHG_INT, 1 << 4),
  71. DECLARE_IRQ(MAX77693_CHG_IRQ_CHGIN_I, CHG_INT, 1 << 6),
  72. DECLARE_IRQ(MAX77693_MUIC_IRQ_INT1_ADC, MUIC_INT1, 1 << 0),
  73. DECLARE_IRQ(MAX77693_MUIC_IRQ_INT1_ADC_LOW, MUIC_INT1, 1 << 1),
  74. DECLARE_IRQ(MAX77693_MUIC_IRQ_INT1_ADC_ERR, MUIC_INT1, 1 << 2),
  75. DECLARE_IRQ(MAX77693_MUIC_IRQ_INT1_ADC1K, MUIC_INT1, 1 << 3),
  76. DECLARE_IRQ(MAX77693_MUIC_IRQ_INT2_CHGTYP, MUIC_INT2, 1 << 0),
  77. DECLARE_IRQ(MAX77693_MUIC_IRQ_INT2_CHGDETREUN, MUIC_INT2, 1 << 1),
  78. DECLARE_IRQ(MAX77693_MUIC_IRQ_INT2_DCDTMR, MUIC_INT2, 1 << 2),
  79. DECLARE_IRQ(MAX77693_MUIC_IRQ_INT2_DXOVP, MUIC_INT2, 1 << 3),
  80. DECLARE_IRQ(MAX77693_MUIC_IRQ_INT2_VBVOLT, MUIC_INT2, 1 << 4),
  81. DECLARE_IRQ(MAX77693_MUIC_IRQ_INT2_VIDRM, MUIC_INT2, 1 << 5),
  82. DECLARE_IRQ(MAX77693_MUIC_IRQ_INT3_EOC, MUIC_INT3, 1 << 0),
  83. DECLARE_IRQ(MAX77693_MUIC_IRQ_INT3_CGMBC, MUIC_INT3, 1 << 1),
  84. DECLARE_IRQ(MAX77693_MUIC_IRQ_INT3_OVP, MUIC_INT3, 1 << 2),
  85. DECLARE_IRQ(MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR, MUIC_INT3, 1 << 3),
  86. DECLARE_IRQ(MAX77693_MUIC_IRQ_INT3_CHG_ENABLED, MUIC_INT3, 1 << 4),
  87. DECLARE_IRQ(MAX77693_MUIC_IRQ_INT3_BAT_DET, MUIC_INT3, 1 << 5),
  88. };
  89. static void max77693_irq_lock(struct irq_data *data)
  90. {
  91. struct max77693_dev *max77693 = irq_get_chip_data(data->irq);
  92. mutex_lock(&max77693->irqlock);
  93. }
  94. static void max77693_irq_sync_unlock(struct irq_data *data)
  95. {
  96. struct max77693_dev *max77693 = irq_get_chip_data(data->irq);
  97. int i;
  98. for (i = 0; i < MAX77693_IRQ_GROUP_NR; i++) {
  99. u8 mask_reg = max77693_mask_reg[i];
  100. struct regmap *map = max77693_get_regmap(max77693, i);
  101. if (mask_reg == MAX77693_REG_INVALID ||
  102. IS_ERR_OR_NULL(map))
  103. continue;
  104. max77693->irq_masks_cache[i] = max77693->irq_masks_cur[i];
  105. max77693_write_reg(map, max77693_mask_reg[i],
  106. max77693->irq_masks_cur[i]);
  107. }
  108. mutex_unlock(&max77693->irqlock);
  109. }
  110. static const inline struct max77693_irq_data *
  111. irq_to_max77693_irq(struct max77693_dev *max77693, int irq)
  112. {
  113. return &max77693_irqs[irq];
  114. }
  115. static void max77693_irq_mask(struct irq_data *data)
  116. {
  117. struct max77693_dev *max77693 = irq_get_chip_data(data->irq);
  118. const struct max77693_irq_data *irq_data =
  119. irq_to_max77693_irq(max77693, data->irq);
  120. if (irq_data->group >= MUIC_INT1 && irq_data->group <= MUIC_INT3)
  121. max77693->irq_masks_cur[irq_data->group] &= ~irq_data->mask;
  122. else
  123. max77693->irq_masks_cur[irq_data->group] |= irq_data->mask;
  124. }
  125. static void max77693_irq_unmask(struct irq_data *data)
  126. {
  127. struct max77693_dev *max77693 = irq_get_chip_data(data->irq);
  128. const struct max77693_irq_data *irq_data =
  129. irq_to_max77693_irq(max77693, data->irq);
  130. if (irq_data->group >= MUIC_INT1 && irq_data->group <= MUIC_INT3)
  131. max77693->irq_masks_cur[irq_data->group] |= irq_data->mask;
  132. else
  133. max77693->irq_masks_cur[irq_data->group] &= ~irq_data->mask;
  134. }
  135. static struct irq_chip max77693_irq_chip = {
  136. .name = "max77693",
  137. .irq_bus_lock = max77693_irq_lock,
  138. .irq_bus_sync_unlock = max77693_irq_sync_unlock,
  139. .irq_mask = max77693_irq_mask,
  140. .irq_unmask = max77693_irq_unmask,
  141. };
  142. #define MAX77693_IRQSRC_CHG (1 << 0)
  143. #define MAX77693_IRQSRC_TOP (1 << 1)
  144. #define MAX77693_IRQSRC_FLASH (1 << 2)
  145. #define MAX77693_IRQSRC_MUIC (1 << 3)
  146. static irqreturn_t max77693_irq_thread(int irq, void *data)
  147. {
  148. struct max77693_dev *max77693 = data;
  149. u8 irq_reg[MAX77693_IRQ_GROUP_NR] = {};
  150. u8 irq_src;
  151. int ret;
  152. int i, cur_irq;
  153. ret = max77693_read_reg(max77693->regmap, MAX77693_PMIC_REG_INTSRC,
  154. &irq_src);
  155. if (ret < 0) {
  156. dev_err(max77693->dev, "Failed to read interrupt source: %d\n",
  157. ret);
  158. return IRQ_NONE;
  159. }
  160. if (irq_src & MAX77693_IRQSRC_CHG)
  161. /* CHG_INT */
  162. ret = max77693_read_reg(max77693->regmap, MAX77693_CHG_REG_CHG_INT,
  163. &irq_reg[CHG_INT]);
  164. if (irq_src & MAX77693_IRQSRC_TOP)
  165. /* TOPSYS_INT */
  166. ret = max77693_read_reg(max77693->regmap,
  167. MAX77693_PMIC_REG_TOPSYS_INT, &irq_reg[TOPSYS_INT]);
  168. if (irq_src & MAX77693_IRQSRC_FLASH)
  169. /* LED_INT */
  170. ret = max77693_read_reg(max77693->regmap,
  171. MAX77693_LED_REG_FLASH_INT, &irq_reg[LED_INT]);
  172. if (irq_src & MAX77693_IRQSRC_MUIC)
  173. /* MUIC INT1 ~ INT3 */
  174. max77693_bulk_read(max77693->regmap, MAX77693_MUIC_REG_INT1,
  175. MAX77693_NUM_IRQ_MUIC_REGS, &irq_reg[MUIC_INT1]);
  176. /* Apply masking */
  177. for (i = 0; i < MAX77693_IRQ_GROUP_NR; i++) {
  178. if (i >= MUIC_INT1 && i <= MUIC_INT3)
  179. irq_reg[i] &= max77693->irq_masks_cur[i];
  180. else
  181. irq_reg[i] &= ~max77693->irq_masks_cur[i];
  182. }
  183. /* Report */
  184. for (i = 0; i < MAX77693_IRQ_NR; i++) {
  185. if (irq_reg[max77693_irqs[i].group] & max77693_irqs[i].mask) {
  186. cur_irq = irq_find_mapping(max77693->irq_domain, i);
  187. if (cur_irq)
  188. handle_nested_irq(cur_irq);
  189. }
  190. }
  191. return IRQ_HANDLED;
  192. }
  193. int max77693_irq_resume(struct max77693_dev *max77693)
  194. {
  195. if (max77693->irq)
  196. max77693_irq_thread(0, max77693);
  197. return 0;
  198. }
  199. static int max77693_irq_domain_map(struct irq_domain *d, unsigned int irq,
  200. irq_hw_number_t hw)
  201. {
  202. struct max77693_dev *max77693 = d->host_data;
  203. irq_set_chip_data(irq, max77693);
  204. irq_set_chip_and_handler(irq, &max77693_irq_chip, handle_edge_irq);
  205. irq_set_nested_thread(irq, 1);
  206. #ifdef CONFIG_ARM
  207. set_irq_flags(irq, IRQF_VALID);
  208. #else
  209. irq_set_noprobe(irq);
  210. #endif
  211. return 0;
  212. }
  213. static struct irq_domain_ops max77693_irq_domain_ops = {
  214. .map = max77693_irq_domain_map,
  215. };
  216. int max77693_irq_init(struct max77693_dev *max77693)
  217. {
  218. struct irq_domain *domain;
  219. int i;
  220. int ret;
  221. mutex_init(&max77693->irqlock);
  222. /* Mask individual interrupt sources */
  223. for (i = 0; i < MAX77693_IRQ_GROUP_NR; i++) {
  224. struct regmap *map;
  225. /* MUIC IRQ 0:MASK 1:NOT MASK */
  226. /* Other IRQ 1:MASK 0:NOT MASK */
  227. if (i >= MUIC_INT1 && i <= MUIC_INT3) {
  228. max77693->irq_masks_cur[i] = 0x00;
  229. max77693->irq_masks_cache[i] = 0x00;
  230. } else {
  231. max77693->irq_masks_cur[i] = 0xff;
  232. max77693->irq_masks_cache[i] = 0xff;
  233. }
  234. map = max77693_get_regmap(max77693, i);
  235. if (IS_ERR_OR_NULL(map))
  236. continue;
  237. if (max77693_mask_reg[i] == MAX77693_REG_INVALID)
  238. continue;
  239. if (i >= MUIC_INT1 && i <= MUIC_INT3)
  240. max77693_write_reg(map, max77693_mask_reg[i], 0x00);
  241. else
  242. max77693_write_reg(map, max77693_mask_reg[i], 0xff);
  243. }
  244. domain = irq_domain_add_linear(NULL, MAX77693_IRQ_NR,
  245. &max77693_irq_domain_ops, max77693);
  246. if (!domain) {
  247. dev_err(max77693->dev, "could not create irq domain\n");
  248. return -ENODEV;
  249. }
  250. max77693->irq_domain = domain;
  251. ret = request_threaded_irq(max77693->irq, NULL, max77693_irq_thread,
  252. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  253. "max77693-irq", max77693);
  254. if (ret)
  255. dev_err(max77693->dev, "Failed to request IRQ %d: %d\n",
  256. max77693->irq, ret);
  257. return 0;
  258. }
  259. void max77693_irq_exit(struct max77693_dev *max77693)
  260. {
  261. if (max77693->irq)
  262. free_irq(max77693->irq, max77693);
  263. }