max77804k-irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * max77804k-irq.c - Interrupt controller support for MAX77804K
  3. *
  4. * Copyright (C) 2011 Samsung Electronics Co.Ltd
  5. * SangYoung Son <hello.son@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * This driver is based on max77804k-irq.c
  22. */
  23. #include <linux/err.h>
  24. #include <linux/irq.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/gpio.h>
  27. #include <linux/mfd/max77804k.h>
  28. #include <linux/mfd/max77804k-private.h>
  29. static const u8 max77804k_mask_reg[] = {
  30. [LED_INT] = MAX77804K_LED_REG_FLASH_INT_MASK,
  31. [TOPSYS_INT] = MAX77804K_PMIC_REG_TOPSYS_INT_MASK,
  32. [CHG_INT] = MAX77804K_CHG_REG_CHG_INT_MASK,
  33. [MUIC_INT1] = MAX77804K_MUIC_REG_INTMASK1,
  34. [MUIC_INT2] = MAX77804K_MUIC_REG_INTMASK2,
  35. [MUIC_INT3] = MAX77804K_MUIC_REG_INTMASK3,
  36. };
  37. static struct i2c_client *get_i2c(struct max77804k_dev *max77804k,
  38. enum max77804k_irq_source src)
  39. {
  40. switch (src) {
  41. case LED_INT ... CHG_INT:
  42. return max77804k->i2c;
  43. case MUIC_INT1 ... MUIC_INT3:
  44. return max77804k->muic;
  45. default:
  46. return ERR_PTR(-EINVAL);
  47. }
  48. }
  49. struct max77804k_irq_data {
  50. int mask;
  51. enum max77804k_irq_source group;
  52. };
  53. #define DECLARE_IRQ(idx, _group, _mask) \
  54. [(idx)] = { .group = (_group), .mask = (_mask) }
  55. static const struct max77804k_irq_data max77804k_irqs[] = {
  56. DECLARE_IRQ(MAX77804K_LED_IRQ_FLED2_OPEN, LED_INT, 1 << 0),
  57. DECLARE_IRQ(MAX77804K_LED_IRQ_FLED2_SHORT, LED_INT, 1 << 1),
  58. DECLARE_IRQ(MAX77804K_LED_IRQ_FLED1_OPEN, LED_INT, 1 << 2),
  59. DECLARE_IRQ(MAX77804K_LED_IRQ_FLED1_SHORT, LED_INT, 1 << 3),
  60. DECLARE_IRQ(MAX77804K_LED_IRQ_MAX_FLASH, LED_INT, 1 << 4),
  61. DECLARE_IRQ(MAX77804K_TOPSYS_IRQ_T120C_INT, TOPSYS_INT, 1 << 0),
  62. DECLARE_IRQ(MAX77804K_TOPSYS_IRQ_T140C_INT, TOPSYS_INT, 1 << 1),
  63. DECLARE_IRQ(MAX77804K_TOPSYS_IRQLOWSYS_INT, TOPSYS_INT, 1 << 3),
  64. DECLARE_IRQ(MAX77804K_CHG_IRQ_BYP_I, CHG_INT, 1 << 0),
  65. DECLARE_IRQ(MAX77804K_CHG_IRQ_BATP_I, CHG_INT, 1 << 2),
  66. DECLARE_IRQ(MAX77804K_CHG_IRQ_BAT_I, CHG_INT, 1 << 3),
  67. DECLARE_IRQ(MAX77804K_CHG_IRQ_CHG_I, CHG_INT, 1 << 4),
  68. #if defined(CONFIG_CHARGER_MAX77804K)
  69. DECLARE_IRQ(MAX77804K_CHG_IRQ_WCIN_I, CHG_INT, 1 << 5),
  70. #endif
  71. DECLARE_IRQ(MAX77804K_CHG_IRQ_CHGIN_I, CHG_INT, 1 << 6),
  72. DECLARE_IRQ(MAX77804K_MUIC_IRQ_INT1_ADC, MUIC_INT1, 1 << 0),
  73. DECLARE_IRQ(MAX77804K_MUIC_IRQ_INT1_ADCLOW, MUIC_INT1, 1 << 1),
  74. DECLARE_IRQ(MAX77804K_MUIC_IRQ_INT1_ADCERR, MUIC_INT1, 1 << 2),
  75. DECLARE_IRQ(MAX77804K_MUIC_IRQ_INT1_ADC1K, MUIC_INT1, 1 << 3),
  76. DECLARE_IRQ(MAX77804K_MUIC_IRQ_INT2_CHGTYP, MUIC_INT2, 1 << 0),
  77. DECLARE_IRQ(MAX77804K_MUIC_IRQ_INT2_CHGDETREUN, MUIC_INT2, 1 << 1),
  78. DECLARE_IRQ(MAX77804K_MUIC_IRQ_INT2_DCDTMR, MUIC_INT2, 1 << 2),
  79. DECLARE_IRQ(MAX77804K_MUIC_IRQ_INT2_DXOVP, MUIC_INT2, 1 << 3),
  80. DECLARE_IRQ(MAX77804K_MUIC_IRQ_INT2_VBVOLT, MUIC_INT2, 1 << 4),
  81. DECLARE_IRQ(MAX77804K_MUIC_IRQ_INT2_VIDRM, MUIC_INT2, 1 << 5),
  82. DECLARE_IRQ(MAX77804K_MUIC_IRQ_INT3_EOC, MUIC_INT3, 1 << 0),
  83. DECLARE_IRQ(MAX77804K_MUIC_IRQ_INT3_CGMBC, MUIC_INT3, 1 << 1),
  84. DECLARE_IRQ(MAX77804K_MUIC_IRQ_INT3_OVP, MUIC_INT3, 1 << 2),
  85. DECLARE_IRQ(MAX77804K_MUIC_IRQ_INT3_MBCCHGERR, MUIC_INT3, 1 << 3),
  86. DECLARE_IRQ(MAX77804K_MUIC_IRQ_INT3_CHGENABLED, MUIC_INT3, 1 << 4),
  87. DECLARE_IRQ(MAX77804K_MUIC_IRQ_INT3_BATDET, MUIC_INT3, 1 << 5),
  88. };
  89. static void max77804k_irq_lock(struct irq_data *data)
  90. {
  91. struct max77804k_dev *max77804k = irq_get_chip_data(data->irq);
  92. mutex_lock(&max77804k->irqlock);
  93. }
  94. static void max77804k_irq_sync_unlock(struct irq_data *data)
  95. {
  96. struct max77804k_dev *max77804k = irq_get_chip_data(data->irq);
  97. int i;
  98. for (i = 0; i < MAX77804K_IRQ_GROUP_NR; i++) {
  99. u8 mask_reg = max77804k_mask_reg[i];
  100. struct i2c_client *i2c = get_i2c(max77804k, i);
  101. if (mask_reg == MAX77804K_REG_INVALID ||
  102. IS_ERR_OR_NULL(i2c))
  103. continue;
  104. max77804k->irq_masks_cache[i] = max77804k->irq_masks_cur[i];
  105. max77804k_write_reg(i2c, max77804k_mask_reg[i],
  106. max77804k->irq_masks_cur[i]);
  107. }
  108. mutex_unlock(&max77804k->irqlock);
  109. }
  110. static const inline struct max77804k_irq_data *
  111. irq_to_max77804k_irq(struct max77804k_dev *max77804k, int irq)
  112. {
  113. return &max77804k_irqs[irq - max77804k->irq_base];
  114. }
  115. static void max77804k_irq_mask(struct irq_data *data)
  116. {
  117. struct max77804k_dev *max77804k = irq_get_chip_data(data->irq);
  118. const struct max77804k_irq_data *irq_data =
  119. irq_to_max77804k_irq(max77804k, data->irq);
  120. if (irq_data->group >= MAX77804K_IRQ_GROUP_NR)
  121. return;
  122. if (irq_data->group >= MUIC_INT1 && irq_data->group <= MUIC_INT3)
  123. max77804k->irq_masks_cur[irq_data->group] &= ~irq_data->mask;
  124. else
  125. max77804k->irq_masks_cur[irq_data->group] |= irq_data->mask;
  126. }
  127. static void max77804k_irq_unmask(struct irq_data *data)
  128. {
  129. struct max77804k_dev *max77804k = irq_get_chip_data(data->irq);
  130. const struct max77804k_irq_data *irq_data =
  131. irq_to_max77804k_irq(max77804k, data->irq);
  132. if (irq_data->group >= MAX77804K_IRQ_GROUP_NR)
  133. return;
  134. if (irq_data->group >= MUIC_INT1 && irq_data->group <= MUIC_INT3)
  135. max77804k->irq_masks_cur[irq_data->group] |= irq_data->mask;
  136. else
  137. max77804k->irq_masks_cur[irq_data->group] &= ~irq_data->mask;
  138. }
  139. static struct irq_chip max77804k_irq_chip = {
  140. .name = "max77804k",
  141. .irq_bus_lock = max77804k_irq_lock,
  142. .irq_bus_sync_unlock = max77804k_irq_sync_unlock,
  143. .irq_mask = max77804k_irq_mask,
  144. .irq_unmask = max77804k_irq_unmask,
  145. };
  146. static irqreturn_t max77804k_irq_thread(int irq, void *data)
  147. {
  148. struct max77804k_dev *max77804k = data;
  149. u8 irq_reg[MAX77804K_IRQ_GROUP_NR] = {};
  150. u8 irq_src;
  151. int ret;
  152. int i;
  153. pr_debug("%s: irq gpio pre-state(0x%02x)\n", __func__,
  154. gpio_get_value(max77804k->irq_gpio));
  155. clear_retry:
  156. ret = max77804k_read_reg(max77804k->i2c,
  157. MAX77804K_PMIC_REG_INTSRC, &irq_src);
  158. if (ret < 0) {
  159. dev_err(max77804k->dev, "Failed to read interrupt source: %d\n",
  160. ret);
  161. return IRQ_NONE;
  162. }
  163. pr_info("%s: interrupt source(0x%02x)\n", __func__, irq_src);
  164. if (irq_src & MAX77804K_IRQSRC_CHG) {
  165. /* CHG_INT */
  166. ret = max77804k_read_reg(max77804k->i2c, MAX77804K_CHG_REG_CHG_INT,
  167. &irq_reg[CHG_INT]);
  168. pr_info("%s: charger interrupt(0x%02x)\n",
  169. __func__, irq_reg[CHG_INT]);
  170. /* mask chgin to prevent chgin infinite interrupt
  171. * chgin is unmasked chgin isr
  172. */
  173. if (irq_reg[CHG_INT] & max77804k_irqs[MAX77804K_CHG_IRQ_CHGIN_I].mask) {
  174. max77804k_update_reg(max77804k->i2c,
  175. MAX77804K_CHG_REG_CHG_INT_MASK, MAX77804K_CHGIN_IM, MAX77804K_CHGIN_IM);
  176. }
  177. }
  178. if (irq_src & MAX77804K_IRQSRC_TOP) {
  179. /* TOPSYS_INT */
  180. ret = max77804k_read_reg(max77804k->i2c,
  181. MAX77804K_PMIC_REG_TOPSYS_INT,
  182. &irq_reg[TOPSYS_INT]);
  183. pr_info("%s: topsys interrupt(0x%02x)\n",
  184. __func__, irq_reg[TOPSYS_INT]);
  185. }
  186. if (irq_src & MAX77804K_IRQSRC_FLASH) {
  187. /* LED_INT */
  188. ret = max77804k_read_reg(max77804k->i2c,
  189. MAX77804K_LED_REG_FLASH_INT,
  190. &irq_reg[LED_INT]);
  191. pr_info("%s: led interrupt(0x%02x)\n",
  192. __func__, irq_reg[LED_INT]);
  193. }
  194. if (irq_src & MAX77804K_IRQSRC_MUIC) {
  195. /* MUIC INT1 ~ INT3 */
  196. max77804k_bulk_read(max77804k->muic,
  197. MAX77804K_MUIC_REG_INT1,
  198. MAX77804K_NUM_IRQ_MUIC_REGS,
  199. &irq_reg[MUIC_INT1]);
  200. pr_info("%s: muic interrupt(0x%02x, 0x%02x, 0x%02x)\n",
  201. __func__, irq_reg[MUIC_INT1],
  202. irq_reg[MUIC_INT2], irq_reg[MUIC_INT3]);
  203. }
  204. pr_debug("%s: irq gpio post-state(0x%02x)\n", __func__,
  205. gpio_get_value(max77804k->irq_gpio));
  206. if (gpio_get_value(max77804k->irq_gpio) == 0) {
  207. pr_warn("%s: irq_gpio is not High!\n", __func__);
  208. goto clear_retry;
  209. }
  210. #if 0
  211. /* Apply masking */
  212. for (i = 0; i < MAX77804K_IRQ_GROUP_NR; i++) {
  213. if (i >= MUIC_INT1 && i <= MUIC_INT3)
  214. irq_reg[i] &= max77804k->irq_masks_cur[i];
  215. else
  216. irq_reg[i] &= ~max77804k->irq_masks_cur[i];
  217. }
  218. #endif
  219. /* Report */
  220. for (i = 0; i < MAX77804K_IRQ_NR; i++) {
  221. if (irq_reg[max77804k_irqs[i].group] & max77804k_irqs[i].mask)
  222. handle_nested_irq(max77804k->irq_base + i);
  223. }
  224. return IRQ_HANDLED;
  225. }
  226. int max77804k_irq_resume(struct max77804k_dev *max77804k)
  227. {
  228. int ret = 0;
  229. if (max77804k->irq && max77804k->irq_base)
  230. ret = max77804k_irq_thread(max77804k->irq_base, max77804k);
  231. dev_info(max77804k->dev, "%s: irq_resume ret=%d", __func__, ret);
  232. return ret >= 0 ? 0 : ret;
  233. }
  234. int max77804k_irq_init(struct max77804k_dev *max77804k)
  235. {
  236. int i;
  237. int cur_irq;
  238. int ret;
  239. u8 i2c_data;
  240. pr_info("func: %s, irq_gpio: %d, irq_base: %d\n", __func__,
  241. max77804k->irq_gpio, max77804k->irq_base);
  242. if (!max77804k->irq_gpio) {
  243. dev_warn(max77804k->dev, "No interrupt specified.\n");
  244. max77804k->irq_base = 0;
  245. return 0;
  246. }
  247. if (!max77804k->irq_base) {
  248. dev_err(max77804k->dev, "No interrupt base specified.\n");
  249. return 0;
  250. }
  251. mutex_init(&max77804k->irqlock);
  252. max77804k->irq = gpio_to_irq(max77804k->irq_gpio);
  253. ret = gpio_request(max77804k->irq_gpio, "if_pmic_irq");
  254. if (ret) {
  255. dev_err(max77804k->dev, "%s: failed requesting gpio %d\n",
  256. __func__, max77804k->irq_gpio);
  257. return ret;
  258. }
  259. gpio_direction_input(max77804k->irq_gpio);
  260. gpio_free(max77804k->irq_gpio);
  261. /* Mask individual interrupt sources */
  262. for (i = 0; i < MAX77804K_IRQ_GROUP_NR; i++) {
  263. struct i2c_client *i2c;
  264. /* MUIC IRQ 0:MASK 1:NOT MASK */
  265. /* Other IRQ 1:MASK 0:NOT MASK */
  266. if (i >= MUIC_INT1 && i <= MUIC_INT3) {
  267. max77804k->irq_masks_cur[i] = 0x00;
  268. max77804k->irq_masks_cache[i] = 0x00;
  269. } else {
  270. max77804k->irq_masks_cur[i] = 0xff;
  271. max77804k->irq_masks_cache[i] = 0xff;
  272. }
  273. i2c = get_i2c(max77804k, i);
  274. if (IS_ERR_OR_NULL(i2c))
  275. continue;
  276. if (max77804k_mask_reg[i] == MAX77804K_REG_INVALID)
  277. continue;
  278. if (i >= MUIC_INT1 && i <= MUIC_INT3)
  279. max77804k_write_reg(i2c, max77804k_mask_reg[i], 0x00);
  280. else
  281. max77804k_write_reg(i2c, max77804k_mask_reg[i], 0xff);
  282. }
  283. /* Register with genirq */
  284. for (i = 0; i < MAX77804K_IRQ_NR; i++) {
  285. cur_irq = i + max77804k->irq_base;
  286. irq_set_chip_data(cur_irq, max77804k);
  287. irq_set_chip_and_handler(cur_irq, &max77804k_irq_chip,
  288. handle_edge_irq);
  289. irq_set_nested_thread(cur_irq, 1);
  290. #ifdef CONFIG_ARM
  291. set_irq_flags(cur_irq, IRQF_VALID);
  292. #else
  293. irq_set_noprobe(cur_irq);
  294. #endif
  295. }
  296. /* Unmask max77804k interrupt */
  297. ret = max77804k_read_reg(max77804k->i2c, MAX77804K_PMIC_REG_INTSRC_MASK,
  298. &i2c_data);
  299. if (ret) {
  300. dev_err(max77804k->dev, "%s: fail to read muic reg\n", __func__);
  301. return ret;
  302. }
  303. i2c_data &= ~(MAX77804K_IRQSRC_CHG); /* Unmask charger interrupt */
  304. i2c_data &= ~(MAX77804K_IRQSRC_MUIC); /* Unmask muic interrupt */
  305. max77804k_write_reg(max77804k->i2c, MAX77804K_PMIC_REG_INTSRC_MASK,
  306. i2c_data);
  307. ret = request_threaded_irq(max77804k->irq, NULL, max77804k_irq_thread,
  308. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  309. "max77804k-irq", max77804k);
  310. if (ret) {
  311. dev_err(max77804k->dev, "Failed to request IRQ %d: %d\n",
  312. max77804k->irq, ret);
  313. return ret;
  314. }
  315. return 0;
  316. }
  317. void max77804k_irq_exit(struct max77804k_dev *max77804k)
  318. {
  319. if (max77804k->irq)
  320. free_irq(max77804k->irq, max77804k);
  321. }