extcon-max77803-irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * max77803-irq.c - Interrupt controller support for MAX77803
  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 max77803-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/max77803.h>
  28. #include <linux/mfd/max77803-private.h>
  29. static const u8 max77803_mask_reg[] = {
  30. [LED_INT] = MAX77803_LED_REG_FLASH_INT_MASK,
  31. [TOPSYS_INT] = MAX77803_PMIC_REG_TOPSYS_INT_MASK,
  32. [CHG_INT] = MAX77803_CHG_REG_CHG_INT_MASK,
  33. [MUIC_INT1] = MAX77803_MUIC_REG_INTMASK1,
  34. [MUIC_INT2] = MAX77803_MUIC_REG_INTMASK2,
  35. [MUIC_INT3] = MAX77803_MUIC_REG_INTMASK3,
  36. };
  37. static struct i2c_client *get_i2c(struct max77803_dev *max77803,
  38. enum max77803_irq_source src)
  39. {
  40. switch (src) {
  41. case LED_INT ... CHG_INT:
  42. return max77803->i2c;
  43. case MUIC_INT1 ... MUIC_INT3:
  44. return max77803->muic;
  45. default:
  46. return ERR_PTR(-EINVAL);
  47. }
  48. }
  49. struct max77803_irq_data {
  50. int mask;
  51. enum max77803_irq_source group;
  52. };
  53. #define DECLARE_IRQ(idx, _group, _mask) \
  54. [(idx)] = { .group = (_group), .mask = (_mask) }
  55. static const struct max77803_irq_data max77803_irqs[] = {
  56. DECLARE_IRQ(MAX77803_LED_IRQ_FLED2_OPEN, LED_INT, 1 << 0),
  57. DECLARE_IRQ(MAX77803_LED_IRQ_FLED2_SHORT, LED_INT, 1 << 1),
  58. DECLARE_IRQ(MAX77803_LED_IRQ_FLED1_OPEN, LED_INT, 1 << 2),
  59. DECLARE_IRQ(MAX77803_LED_IRQ_FLED1_SHORT, LED_INT, 1 << 3),
  60. DECLARE_IRQ(MAX77803_LED_IRQ_MAX_FLASH, LED_INT, 1 << 4),
  61. DECLARE_IRQ(MAX77803_TOPSYS_IRQ_T120C_INT, TOPSYS_INT, 1 << 0),
  62. DECLARE_IRQ(MAX77803_TOPSYS_IRQ_T140C_INT, TOPSYS_INT, 1 << 1),
  63. DECLARE_IRQ(MAX77803_TOPSYS_IRQLOWSYS_INT, TOPSYS_INT, 1 << 3),
  64. DECLARE_IRQ(MAX77803_CHG_IRQ_BYP_I, CHG_INT, 1 << 0),
  65. #if defined(CONFIG_CHARGER_MAX77803)
  66. DECLARE_IRQ(MAX77803_CHG_IRQ_BATP_I, CHG_INT, 1 << 2),
  67. #else
  68. DECLARE_IRQ(MAX77803_CHG_IRQ_THM_I, CHG_INT, 1 << 2),
  69. #endif
  70. DECLARE_IRQ(MAX77803_CHG_IRQ_BAT_I, CHG_INT, 1 << 3),
  71. DECLARE_IRQ(MAX77803_CHG_IRQ_CHG_I, CHG_INT, 1 << 4),
  72. #if defined(CONFIG_CHARGER_MAX77803)
  73. DECLARE_IRQ(MAX77803_CHG_IRQ_WCIN_I, CHG_INT, 1 << 5),
  74. #endif
  75. DECLARE_IRQ(MAX77803_CHG_IRQ_CHGIN_I, CHG_INT, 1 << 6),
  76. DECLARE_IRQ(MAX77803_MUIC_IRQ_INT1_ADC, MUIC_INT1, 1 << 0),
  77. DECLARE_IRQ(MAX77803_MUIC_IRQ_INT1_ADCLOW, MUIC_INT1, 1 << 1),
  78. DECLARE_IRQ(MAX77803_MUIC_IRQ_INT1_ADCERR, MUIC_INT1, 1 << 2),
  79. DECLARE_IRQ(MAX77803_MUIC_IRQ_INT1_ADC1K, MUIC_INT1, 1 << 3),
  80. DECLARE_IRQ(MAX77803_MUIC_IRQ_INT2_CHGTYP, MUIC_INT2, 1 << 0),
  81. DECLARE_IRQ(MAX77803_MUIC_IRQ_INT2_CHGDETREUN, MUIC_INT2, 1 << 1),
  82. DECLARE_IRQ(MAX77803_MUIC_IRQ_INT2_DCDTMR, MUIC_INT2, 1 << 2),
  83. DECLARE_IRQ(MAX77803_MUIC_IRQ_INT2_DXOVP, MUIC_INT2, 1 << 3),
  84. DECLARE_IRQ(MAX77803_MUIC_IRQ_INT2_VBVOLT, MUIC_INT2, 1 << 4),
  85. DECLARE_IRQ(MAX77803_MUIC_IRQ_INT2_VIDRM, MUIC_INT2, 1 << 5),
  86. DECLARE_IRQ(MAX77803_MUIC_IRQ_INT3_EOC, MUIC_INT3, 1 << 0),
  87. DECLARE_IRQ(MAX77803_MUIC_IRQ_INT3_CGMBC, MUIC_INT3, 1 << 1),
  88. DECLARE_IRQ(MAX77803_MUIC_IRQ_INT3_OVP, MUIC_INT3, 1 << 2),
  89. DECLARE_IRQ(MAX77803_MUIC_IRQ_INT3_MBCCHGERR, MUIC_INT3, 1 << 3),
  90. DECLARE_IRQ(MAX77803_MUIC_IRQ_INT3_CHGENABLED, MUIC_INT3, 1 << 4),
  91. DECLARE_IRQ(MAX77803_MUIC_IRQ_INT3_BATDET, MUIC_INT3, 1 << 5),
  92. };
  93. static void max77803_irq_lock(struct irq_data *data)
  94. {
  95. struct max77803_dev *max77803 = irq_get_chip_data(data->irq);
  96. mutex_lock(&max77803->irqlock);
  97. }
  98. static void max77803_irq_sync_unlock(struct irq_data *data)
  99. {
  100. struct max77803_dev *max77803 = irq_get_chip_data(data->irq);
  101. int i;
  102. for (i = 0; i < MAX77803_IRQ_GROUP_NR; i++) {
  103. u8 mask_reg = max77803_mask_reg[i];
  104. struct i2c_client *i2c = get_i2c(max77803, i);
  105. if (mask_reg == MAX77803_REG_INVALID ||
  106. IS_ERR_OR_NULL(i2c))
  107. continue;
  108. max77803->irq_masks_cache[i] = max77803->irq_masks_cur[i];
  109. max77803_write_reg(i2c, max77803_mask_reg[i],
  110. max77803->irq_masks_cur[i]);
  111. }
  112. mutex_unlock(&max77803->irqlock);
  113. }
  114. static const inline struct max77803_irq_data *
  115. irq_to_max77803_irq(struct max77803_dev *max77803, int irq)
  116. {
  117. return &max77803_irqs[irq - max77803->irq_base];
  118. }
  119. static void max77803_irq_mask(struct irq_data *data)
  120. {
  121. struct max77803_dev *max77803 = irq_get_chip_data(data->irq);
  122. const struct max77803_irq_data *irq_data =
  123. irq_to_max77803_irq(max77803, data->irq);
  124. if (irq_data->group >= MAX77803_IRQ_GROUP_NR)
  125. return;
  126. if (irq_data->group >= MUIC_INT1 && irq_data->group <= MUIC_INT3)
  127. max77803->irq_masks_cur[irq_data->group] &= ~irq_data->mask;
  128. else
  129. max77803->irq_masks_cur[irq_data->group] |= irq_data->mask;
  130. }
  131. static void max77803_irq_unmask(struct irq_data *data)
  132. {
  133. struct max77803_dev *max77803 = irq_get_chip_data(data->irq);
  134. const struct max77803_irq_data *irq_data =
  135. irq_to_max77803_irq(max77803, data->irq);
  136. if (irq_data->group >= MAX77803_IRQ_GROUP_NR)
  137. return;
  138. if (irq_data->group >= MUIC_INT1 && irq_data->group <= MUIC_INT3)
  139. max77803->irq_masks_cur[irq_data->group] |= irq_data->mask;
  140. else
  141. max77803->irq_masks_cur[irq_data->group] &= ~irq_data->mask;
  142. }
  143. static struct irq_chip max77803_irq_chip = {
  144. .name = "max77803",
  145. .irq_bus_lock = max77803_irq_lock,
  146. .irq_bus_sync_unlock = max77803_irq_sync_unlock,
  147. .irq_mask = max77803_irq_mask,
  148. .irq_unmask = max77803_irq_unmask,
  149. };
  150. static irqreturn_t max77803_irq_thread(int irq, void *data)
  151. {
  152. struct max77803_dev *max77803 = data;
  153. u8 irq_reg[MAX77803_IRQ_GROUP_NR] = {};
  154. u8 irq_src;
  155. int ret;
  156. int i;
  157. pr_debug("%s: irq gpio pre-state(0x%02x)\n", __func__,
  158. gpio_get_value(max77803->irq_gpio));
  159. clear_retry:
  160. ret = max77803_read_reg(max77803->i2c,
  161. MAX77803_PMIC_REG_INTSRC, &irq_src);
  162. if (ret < 0) {
  163. dev_err(max77803->dev, "Failed to read interrupt source: %d\n",
  164. ret);
  165. return IRQ_NONE;
  166. }
  167. pr_info("%s: interrupt source(0x%02x)\n", __func__, irq_src);
  168. if (irq_src & MAX77803_IRQSRC_CHG) {
  169. /* CHG_INT */
  170. ret = max77803_read_reg(max77803->i2c, MAX77803_CHG_REG_CHG_INT,
  171. &irq_reg[CHG_INT]);
  172. pr_info("%s: charger interrupt(0x%02x)\n",
  173. __func__, irq_reg[CHG_INT]);
  174. /* mask chgin to prevent chgin infinite interrupt
  175. * chgin is unmasked chgin isr
  176. */
  177. if (irq_reg[CHG_INT] & max77803_irqs[MAX77803_CHG_IRQ_CHGIN_I].mask) {
  178. u8 reg_data;
  179. max77803_read_reg(max77803->i2c,
  180. MAX77803_CHG_REG_CHG_INT_MASK, &reg_data);
  181. reg_data |= (1 << 6);
  182. max77803_write_reg(max77803->i2c,
  183. MAX77803_CHG_REG_CHG_INT_MASK, reg_data);
  184. }
  185. }
  186. if (irq_src & MAX77803_IRQSRC_TOP) {
  187. /* TOPSYS_INT */
  188. ret = max77803_read_reg(max77803->i2c,
  189. MAX77803_PMIC_REG_TOPSYS_INT,
  190. &irq_reg[TOPSYS_INT]);
  191. pr_info("%s: topsys interrupt(0x%02x)\n",
  192. __func__, irq_reg[TOPSYS_INT]);
  193. }
  194. if (irq_src & MAX77803_IRQSRC_FLASH) {
  195. /* LED_INT */
  196. ret = max77803_read_reg(max77803->i2c,
  197. MAX77803_LED_REG_FLASH_INT,
  198. &irq_reg[LED_INT]);
  199. pr_info("%s: led interrupt(0x%02x)\n",
  200. __func__, irq_reg[LED_INT]);
  201. }
  202. if (irq_src & MAX77803_IRQSRC_MUIC) {
  203. /* MUIC INT1 ~ INT3 */
  204. max77803_bulk_read(max77803->muic,
  205. MAX77803_MUIC_REG_INT1,
  206. MAX77803_NUM_IRQ_MUIC_REGS,
  207. &irq_reg[MUIC_INT1]);
  208. pr_info("%s: muic interrupt(0x%02x, 0x%02x, 0x%02x)\n",
  209. __func__, irq_reg[MUIC_INT1],
  210. irq_reg[MUIC_INT2], irq_reg[MUIC_INT3]);
  211. }
  212. pr_debug("%s: irq gpio post-state(0x%02x)\n", __func__,
  213. gpio_get_value(max77803->irq_gpio));
  214. if (gpio_get_value(max77803->irq_gpio) == 0) {
  215. pr_warn("%s: irq_gpio is not High!\n", __func__);
  216. goto clear_retry;
  217. }
  218. #if 0
  219. /* Apply masking */
  220. for (i = 0; i < MAX77803_IRQ_GROUP_NR; i++) {
  221. if (i >= MUIC_INT1 && i <= MUIC_INT3)
  222. irq_reg[i] &= max77803->irq_masks_cur[i];
  223. else
  224. irq_reg[i] &= ~max77803->irq_masks_cur[i];
  225. }
  226. #endif
  227. /* Report */
  228. for (i = 0; i < MAX77803_IRQ_NR; i++) {
  229. if (irq_reg[max77803_irqs[i].group] & max77803_irqs[i].mask)
  230. handle_nested_irq(max77803->irq_base + i);
  231. }
  232. return IRQ_HANDLED;
  233. }
  234. int max77803_irq_resume(struct max77803_dev *max77803)
  235. {
  236. int ret = 0;
  237. if (max77803->irq && max77803->irq_base)
  238. ret = max77803_irq_thread(max77803->irq_base, max77803);
  239. dev_info(max77803->dev, "%s: irq_resume ret=%d", __func__, ret);
  240. return ret >= 0 ? 0 : ret;
  241. }
  242. int max77803_irq_init(struct max77803_dev *max77803)
  243. {
  244. int i;
  245. int cur_irq;
  246. int ret;
  247. u8 i2c_data;
  248. pr_info("func: %s, irq_gpio: %d, irq_base: %d\n", __func__,
  249. max77803->irq_gpio, max77803->irq_base);
  250. if (!max77803->irq_gpio) {
  251. dev_warn(max77803->dev, "No interrupt specified.\n");
  252. max77803->irq_base = 0;
  253. return 0;
  254. }
  255. if (!max77803->irq_base) {
  256. dev_err(max77803->dev, "No interrupt base specified.\n");
  257. return 0;
  258. }
  259. mutex_init(&max77803->irqlock);
  260. max77803->irq = gpio_to_irq(max77803->irq_gpio);
  261. ret = gpio_request(max77803->irq_gpio, "if_pmic_irq");
  262. if (ret) {
  263. dev_err(max77803->dev, "%s: failed requesting gpio %d\n",
  264. __func__, max77803->irq_gpio);
  265. return ret;
  266. }
  267. gpio_direction_input(max77803->irq_gpio);
  268. gpio_free(max77803->irq_gpio);
  269. /* Mask individual interrupt sources */
  270. for (i = 0; i < MAX77803_IRQ_GROUP_NR; i++) {
  271. struct i2c_client *i2c;
  272. /* MUIC IRQ 0:MASK 1:NOT MASK */
  273. /* Other IRQ 1:MASK 0:NOT MASK */
  274. if (i >= MUIC_INT1 && i <= MUIC_INT3) {
  275. max77803->irq_masks_cur[i] = 0x00;
  276. max77803->irq_masks_cache[i] = 0x00;
  277. } else {
  278. max77803->irq_masks_cur[i] = 0xff;
  279. max77803->irq_masks_cache[i] = 0xff;
  280. }
  281. i2c = get_i2c(max77803, i);
  282. if (IS_ERR_OR_NULL(i2c))
  283. continue;
  284. if (max77803_mask_reg[i] == MAX77803_REG_INVALID)
  285. continue;
  286. if (i >= MUIC_INT1 && i <= MUIC_INT3)
  287. max77803_write_reg(i2c, max77803_mask_reg[i], 0x00);
  288. else
  289. max77803_write_reg(i2c, max77803_mask_reg[i], 0xff);
  290. }
  291. /* Register with genirq */
  292. for (i = 0; i < MAX77803_IRQ_NR; i++) {
  293. cur_irq = i + max77803->irq_base;
  294. irq_set_chip_data(cur_irq, max77803);
  295. irq_set_chip_and_handler(cur_irq, &max77803_irq_chip,
  296. handle_edge_irq);
  297. irq_set_nested_thread(cur_irq, 1);
  298. #ifdef CONFIG_ARM
  299. set_irq_flags(cur_irq, IRQF_VALID);
  300. #else
  301. irq_set_noprobe(cur_irq);
  302. #endif
  303. }
  304. /* Unmask max77803 interrupt */
  305. ret = max77803_read_reg(max77803->i2c, MAX77803_PMIC_REG_INTSRC_MASK,
  306. &i2c_data);
  307. if (ret) {
  308. dev_err(max77803->dev, "%s: fail to read muic reg\n", __func__);
  309. return ret;
  310. }
  311. i2c_data &= ~(MAX77803_IRQSRC_CHG); /* Unmask charger interrupt */
  312. i2c_data &= ~(MAX77803_IRQSRC_MUIC); /* Unmask muic interrupt */
  313. max77803_write_reg(max77803->i2c, MAX77803_PMIC_REG_INTSRC_MASK,
  314. i2c_data);
  315. ret = request_threaded_irq(max77803->irq, NULL, max77803_irq_thread,
  316. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  317. "max77803-irq", max77803);
  318. if (ret) {
  319. dev_err(max77803->dev, "Failed to request IRQ %d: %d\n",
  320. max77803->irq, ret);
  321. return ret;
  322. }
  323. return 0;
  324. }
  325. void max77803_irq_exit(struct max77803_dev *max77803)
  326. {
  327. if (max77803->irq)
  328. free_irq(max77803->irq, max77803);
  329. }