max77888-irq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * max77888-irq.c - Interrupt controller support for MAX77888
  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 max77888-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/max77888.h>
  28. #include <linux/mfd/max77888-private.h>
  29. #ifdef CONFIG_MUIC_RESET_PIN_ENABLE
  30. struct delayed_work muic_restore_work;
  31. extern void max77888_muic_regdump(void);
  32. struct max77888_dev *max77888_backup;
  33. extern int muic_reset_pin;
  34. #endif
  35. static const u8 max77888_mask_reg[] = {
  36. [LED_INT] = MAX77888_LED_REG_FLASH_INT_MASK,
  37. [TOPSYS_INT] = MAX77888_PMIC_REG_TOPSYS_INT_MASK,
  38. [CHG_INT] = MAX77888_CHG_REG_CHG_INT_MASK,
  39. [MUIC_INT1] = MAX77888_MUIC_REG_INTMASK1,
  40. [MUIC_INT2] = MAX77888_MUIC_REG_INTMASK2,
  41. [MUIC_INT3] = MAX77888_MUIC_REG_INTMASK3,
  42. };
  43. static struct i2c_client *get_i2c(struct max77888_dev *max77888,
  44. enum max77888_irq_source src)
  45. {
  46. switch (src) {
  47. case LED_INT ... CHG_INT:
  48. return max77888->i2c;
  49. case MUIC_INT1 ... MUIC_INT3:
  50. return max77888->muic;
  51. default:
  52. return ERR_PTR(-EINVAL);
  53. }
  54. }
  55. struct max77888_irq_data {
  56. int mask;
  57. enum max77888_irq_source group;
  58. };
  59. #define DECLARE_IRQ(idx, _group, _mask) \
  60. [(idx)] = { .group = (_group), .mask = (_mask) }
  61. static const struct max77888_irq_data max77888_irqs[] = {
  62. DECLARE_IRQ(MAX77888_LED_IRQ_FLED2_OPEN, LED_INT, 1 << 0),
  63. DECLARE_IRQ(MAX77888_LED_IRQ_FLED2_SHORT, LED_INT, 1 << 1),
  64. DECLARE_IRQ(MAX77888_LED_IRQ_FLED1_OPEN, LED_INT, 1 << 2),
  65. DECLARE_IRQ(MAX77888_LED_IRQ_FLED1_SHORT, LED_INT, 1 << 3),
  66. DECLARE_IRQ(MAX77888_LED_IRQ_MAX_FLASH, LED_INT, 1 << 4),
  67. DECLARE_IRQ(MAX77888_TOPSYS_IRQ_T120C_INT, TOPSYS_INT, 1 << 0),
  68. DECLARE_IRQ(MAX77888_TOPSYS_IRQ_T140C_INT, TOPSYS_INT, 1 << 1),
  69. DECLARE_IRQ(MAX77888_TOPSYS_IRQLOWSYS_INT, TOPSYS_INT, 1 << 3),
  70. DECLARE_IRQ(MAX77888_CHG_IRQ_BYP_I, CHG_INT, 1 << 0),
  71. DECLARE_IRQ(MAX77888_CHG_IRQ_BATP_I, CHG_INT, 1 << 2),
  72. DECLARE_IRQ(MAX77888_CHG_IRQ_BAT_I, CHG_INT, 1 << 3),
  73. DECLARE_IRQ(MAX77888_CHG_IRQ_CHG_I, CHG_INT, 1 << 4),
  74. DECLARE_IRQ(MAX77888_CHG_IRQ_WCIN_I, CHG_INT, 1 << 5),
  75. DECLARE_IRQ(MAX77888_CHG_IRQ_CHGIN_I, CHG_INT, 1 << 6),
  76. DECLARE_IRQ(MAX77888_MUIC_IRQ_INT1_ADC, MUIC_INT1, 1 << 0),
  77. DECLARE_IRQ(MAX77888_MUIC_IRQ_INT1_ADCLOW, MUIC_INT1, 1 << 1),
  78. DECLARE_IRQ(MAX77888_MUIC_IRQ_INT1_ADCERR, MUIC_INT1, 1 << 2),
  79. DECLARE_IRQ(MAX77888_MUIC_IRQ_INT1_ADC1K, MUIC_INT1, 1 << 3),
  80. DECLARE_IRQ(MAX77888_MUIC_IRQ_INT2_CHGTYP, MUIC_INT2, 1 << 0),
  81. DECLARE_IRQ(MAX77888_MUIC_IRQ_INT2_CHGDETREUN, MUIC_INT2, 1 << 1),
  82. DECLARE_IRQ(MAX77888_MUIC_IRQ_INT2_DCDTMR, MUIC_INT2, 1 << 2),
  83. DECLARE_IRQ(MAX77888_MUIC_IRQ_INT2_DXOVP, MUIC_INT2, 1 << 3),
  84. DECLARE_IRQ(MAX77888_MUIC_IRQ_INT2_VBVOLT, MUIC_INT2, 1 << 4),
  85. DECLARE_IRQ(MAX77888_MUIC_IRQ_INT2_VIDRM, MUIC_INT2, 1 << 5),
  86. DECLARE_IRQ(MAX77888_MUIC_IRQ_INT3_EOC, MUIC_INT3, 1 << 0),
  87. DECLARE_IRQ(MAX77888_MUIC_IRQ_INT3_CGMBC, MUIC_INT3, 1 << 1),
  88. DECLARE_IRQ(MAX77888_MUIC_IRQ_INT3_OVP, MUIC_INT3, 1 << 2),
  89. DECLARE_IRQ(MAX77888_MUIC_IRQ_INT3_MBCCHGERR, MUIC_INT3, 1 << 3),
  90. DECLARE_IRQ(MAX77888_MUIC_IRQ_INT3_CHGENABLED, MUIC_INT3, 1 << 4),
  91. DECLARE_IRQ(MAX77888_MUIC_IRQ_INT3_BATDET, MUIC_INT3, 1 << 5),
  92. };
  93. static void max77888_irq_lock(struct irq_data *data)
  94. {
  95. struct max77888_dev *max77888 = irq_get_chip_data(data->irq);
  96. mutex_lock(&max77888->irqlock);
  97. }
  98. static void max77888_irq_sync_unlock(struct irq_data *data)
  99. {
  100. struct max77888_dev *max77888 = irq_get_chip_data(data->irq);
  101. int i;
  102. for (i = 0; i < MAX77888_IRQ_GROUP_NR; i++) {
  103. u8 mask_reg = max77888_mask_reg[i];
  104. struct i2c_client *i2c = get_i2c(max77888, i);
  105. if (mask_reg == MAX77888_REG_INVALID ||
  106. IS_ERR_OR_NULL(i2c))
  107. continue;
  108. max77888->irq_masks_cache[i] = max77888->irq_masks_cur[i];
  109. max77888_write_reg(i2c, max77888_mask_reg[i],
  110. max77888->irq_masks_cur[i]);
  111. }
  112. mutex_unlock(&max77888->irqlock);
  113. }
  114. static const inline struct max77888_irq_data *
  115. irq_to_max77888_irq(struct max77888_dev *max77888, int irq)
  116. {
  117. return &max77888_irqs[irq - max77888->irq_base];
  118. }
  119. static void max77888_irq_mask(struct irq_data *data)
  120. {
  121. struct max77888_dev *max77888 = irq_get_chip_data(data->irq);
  122. const struct max77888_irq_data *irq_data =
  123. irq_to_max77888_irq(max77888, data->irq);
  124. if (irq_data->group >= MAX77888_IRQ_GROUP_NR)
  125. return;
  126. if (irq_data->group >= MUIC_INT1 && irq_data->group <= MUIC_INT3)
  127. max77888->irq_masks_cur[irq_data->group] &= ~irq_data->mask;
  128. else
  129. max77888->irq_masks_cur[irq_data->group] |= irq_data->mask;
  130. }
  131. static void max77888_irq_unmask(struct irq_data *data)
  132. {
  133. struct max77888_dev *max77888 = irq_get_chip_data(data->irq);
  134. const struct max77888_irq_data *irq_data =
  135. irq_to_max77888_irq(max77888, data->irq);
  136. if (irq_data->group >= MAX77888_IRQ_GROUP_NR)
  137. return;
  138. if (irq_data->group >= MUIC_INT1 && irq_data->group <= MUIC_INT3)
  139. max77888->irq_masks_cur[irq_data->group] |= irq_data->mask;
  140. else
  141. max77888->irq_masks_cur[irq_data->group] &= ~irq_data->mask;
  142. }
  143. #ifdef CONFIG_MUIC_RESET_PIN_ENABLE
  144. static void _max77888_restore_muic_reg(struct max77888_dev *max77888);
  145. static void max77888_restore_muic_reg(struct work_struct *work) {
  146. _max77888_restore_muic_reg(max77888_backup);
  147. }
  148. static void _max77888_restore_muic_reg(struct max77888_dev *max77888)
  149. {
  150. pr_info("%s:Restore muic irq\n", __func__);
  151. max77888_write_reg(max77888->muic, MAX77888_MUIC_REG_INTMASK1, 0x09);
  152. max77888_write_reg(max77888->muic, MAX77888_MUIC_REG_INTMASK2, 0x11);
  153. max77888_update_reg(max77888->muic, MAX77888_MUIC_REG_CDETCTRL1,
  154. (0x01 << CHGTYPM_SHIFT), CHGTYPM_MASK);
  155. max77888_write_reg(max77888->muic, MAX77888_MUIC_REG_CTRL4, 0x02);
  156. max77888_muic_regdump();
  157. }
  158. #endif
  159. static struct irq_chip max77888_irq_chip = {
  160. .name = "max77888",
  161. .irq_bus_lock = max77888_irq_lock,
  162. .irq_bus_sync_unlock = max77888_irq_sync_unlock,
  163. .irq_mask = max77888_irq_mask,
  164. .irq_unmask = max77888_irq_unmask,
  165. };
  166. #ifdef CONFIG_MUIC_RESET_PIN_ENABLE
  167. static irqreturn_t max77888_reset_irq_thread(int irq, void *data)
  168. {
  169. max77888_backup = data;
  170. pr_info("%s: MUIC block was reset, restore reg now\n", __func__);
  171. cancel_delayed_work_sync(&muic_restore_work);
  172. schedule_delayed_work(&muic_restore_work, msecs_to_jiffies(100));
  173. return IRQ_HANDLED;
  174. }
  175. #endif
  176. static irqreturn_t max77888_irq_thread(int irq, void *data)
  177. {
  178. struct max77888_dev *max77888 = data;
  179. u8 irq_reg[MAX77888_IRQ_GROUP_NR] = {0};
  180. u8 tmp_irq_reg[MAX77888_IRQ_GROUP_NR] = {};
  181. u8 irq_src;
  182. int ret;
  183. int i;
  184. /* INTMASK1 3:ADC1K 0:ADC */
  185. /* INTMASK2 4:VBVolt 0:Chgtype */
  186. max77888_write_reg(max77888->muic, MAX77888_MUIC_REG_INTMASK1, 0x09);
  187. max77888_write_reg(max77888->muic, MAX77888_MUIC_REG_INTMASK2, 0x11);
  188. clear_retry:
  189. ret = max77888_read_reg(max77888->i2c,
  190. MAX77888_PMIC_REG_INTSRC, &irq_src);
  191. if (ret < 0) {
  192. dev_err(max77888->dev,
  193. "Failed to read interrupt source: %d\n", ret);
  194. return IRQ_NONE;
  195. }
  196. pr_info("%s: interrupt source(0x%02x)\n", __func__, irq_src);
  197. if (irq_src & MAX77888_IRQSRC_CHG) {
  198. /* CHG_INT */
  199. ret = max77888_read_reg(max77888->i2c, MAX77888_CHG_REG_CHG_INT,
  200. &irq_reg[CHG_INT]);
  201. pr_info("%s: charger interrupt(0x%02x)\n",
  202. __func__, irq_reg[CHG_INT]);
  203. /* mask chgin to prevent chgin infinite interrupt
  204. * chgin is unmasked chgin isr
  205. */
  206. if (irq_reg[CHG_INT] &
  207. max77888_irqs[MAX77888_CHG_IRQ_CHGIN_I].mask) {
  208. u8 reg_data;
  209. max77888_read_reg(max77888->i2c,
  210. MAX77888_CHG_REG_CHG_INT_MASK, &reg_data);
  211. reg_data |= (1 << 6);
  212. max77888_write_reg(max77888->i2c,
  213. MAX77888_CHG_REG_CHG_INT_MASK, reg_data);
  214. }
  215. }
  216. if (irq_src & MAX77888_IRQSRC_TOP) {
  217. /* TOPSYS_INT */
  218. ret = max77888_read_reg(max77888->i2c,
  219. MAX77888_PMIC_REG_TOPSYS_INT,
  220. &irq_reg[TOPSYS_INT]);
  221. pr_info("%s: topsys interrupt(0x%02x)\n",
  222. __func__, irq_reg[TOPSYS_INT]);
  223. }
  224. if (irq_src & MAX77888_IRQSRC_FLASH) {
  225. /* LED_INT */
  226. ret = max77888_read_reg(max77888->i2c,
  227. MAX77888_LED_REG_FLASH_INT, &irq_reg[LED_INT]);
  228. pr_info("%s: led interrupt(0x%02x)\n",
  229. __func__, irq_reg[LED_INT]);
  230. }
  231. if (irq_src & MAX77888_IRQSRC_MUIC) {
  232. /* MUIC INT1 ~ INT3 */
  233. max77888_bulk_read(max77888->muic, MAX77888_MUIC_REG_INT1,
  234. MAX77888_NUM_IRQ_MUIC_REGS,
  235. &tmp_irq_reg[MUIC_INT1]);
  236. /* Or temp irq register to irq register for if it retries */
  237. for (i = MUIC_INT1; i < MAX77888_IRQ_GROUP_NR; i++)
  238. irq_reg[i] |= tmp_irq_reg[i];
  239. pr_info("%s: muic interrupt(0x%02x, 0x%02x, 0x%02x)\n",
  240. __func__, irq_reg[MUIC_INT1],
  241. irq_reg[MUIC_INT2], irq_reg[MUIC_INT3]);
  242. }
  243. pr_debug("%s: irq gpio post-state(0x%02x)\n", __func__,
  244. gpio_get_value(max77888->irq_gpio));
  245. if (gpio_get_value(max77888->irq_gpio) == 0) {
  246. pr_warn("%s: irq_gpio is not High!\n", __func__);
  247. goto clear_retry;
  248. }
  249. #if 0
  250. /* Apply masking */
  251. for (i = 0; i < MAX77888_IRQ_GROUP_NR; i++) {
  252. if (i >= MUIC_INT1 && i <= MUIC_INT3)
  253. irq_reg[i] &= max77888->irq_masks_cur[i];
  254. else
  255. irq_reg[i] &= ~max77888->irq_masks_cur[i];
  256. }
  257. #endif
  258. /* Report */
  259. for (i = 0; i < MAX77888_IRQ_NR; i++) {
  260. if (irq_reg[max77888_irqs[i].group] & max77888_irqs[i].mask)
  261. handle_nested_irq(max77888->irq_base + i);
  262. }
  263. return IRQ_HANDLED;
  264. }
  265. int max77888_irq_resume(struct max77888_dev *max77888)
  266. {
  267. int ret = 0;
  268. if (max77888->irq && max77888->irq_base)
  269. ret = max77888_irq_thread(max77888->irq_base, max77888);
  270. dev_info(max77888->dev, "%s: irq_resume ret=%d", __func__, ret);
  271. return ret >= 0 ? 0 : ret;
  272. }
  273. int max77888_irq_init(struct max77888_dev *max77888)
  274. {
  275. int i;
  276. int cur_irq;
  277. int ret;
  278. u8 i2c_data;
  279. pr_info("func: %s, irq_gpio: %d, irq_base: %d\n", __func__,
  280. max77888->irq_gpio, max77888->irq_base);
  281. #ifdef CONFIG_MUIC_RESET_PIN_ENABLE
  282. INIT_DELAYED_WORK(&muic_restore_work, max77888_restore_muic_reg);
  283. #endif
  284. if (!max77888->irq_gpio) {
  285. dev_warn(max77888->dev, "No interrupt specified.\n");
  286. max77888->irq_base = 0;
  287. return 0;
  288. }
  289. if (!max77888->irq_base) {
  290. dev_err(max77888->dev, "No interrupt base specified.\n");
  291. return 0;
  292. }
  293. mutex_init(&max77888->irqlock);
  294. max77888->irq = gpio_to_irq(max77888->irq_gpio);
  295. ret = gpio_request(max77888->irq_gpio, "if_pmic_irq");
  296. if (ret) {
  297. dev_err(max77888->dev, "%s: failed requesting gpio %d\n",
  298. __func__, max77888->irq_gpio);
  299. return ret;
  300. }
  301. gpio_direction_input(max77888->irq_gpio);
  302. gpio_free(max77888->irq_gpio);
  303. #ifdef CONFIG_MUIC_RESET_PIN_ENABLE
  304. if (muic_reset_pin)
  305. {
  306. max77888->irq_reset = gpio_to_irq(max77888->irq_reset_gpio);
  307. ret = gpio_request(max77888->irq_reset_gpio, "muic_reset_irq");
  308. if (ret) {
  309. dev_err(max77888->dev, "%s: failed requesting gpio %d\n",
  310. __func__, max77888->irq_reset_gpio);
  311. return ret;
  312. }
  313. gpio_direction_input(max77888->irq_reset_gpio);
  314. gpio_free(max77888->irq_reset_gpio);
  315. }
  316. #endif
  317. /* Mask individual interrupt sources */
  318. for (i = 0; i < MAX77888_IRQ_GROUP_NR; i++) {
  319. struct i2c_client *i2c;
  320. /* MUIC IRQ 0:MASK 1:NOT MASK */
  321. /* Other IRQ 1:MASK 0:NOT MASK */
  322. if (i >= MUIC_INT1 && i <= MUIC_INT3) {
  323. max77888->irq_masks_cur[i] = 0x00;
  324. max77888->irq_masks_cache[i] = 0x00;
  325. } else {
  326. max77888->irq_masks_cur[i] = 0xff;
  327. max77888->irq_masks_cache[i] = 0xff;
  328. }
  329. i2c = get_i2c(max77888, i);
  330. if (IS_ERR_OR_NULL(i2c))
  331. continue;
  332. if (max77888_mask_reg[i] == MAX77888_REG_INVALID)
  333. continue;
  334. if (i >= MUIC_INT1 && i <= MUIC_INT3)
  335. max77888_write_reg(i2c, max77888_mask_reg[i], 0x00);
  336. else
  337. max77888_write_reg(i2c, max77888_mask_reg[i], 0xff);
  338. }
  339. /* Register with genirq */
  340. for (i = 0; i < MAX77888_IRQ_NR; i++) {
  341. cur_irq = i + max77888->irq_base;
  342. irq_set_chip_data(cur_irq, max77888);
  343. irq_set_chip_and_handler(cur_irq, &max77888_irq_chip,
  344. handle_edge_irq);
  345. irq_set_nested_thread(cur_irq, 1);
  346. #ifdef CONFIG_ARM
  347. set_irq_flags(cur_irq, IRQF_VALID);
  348. #else
  349. irq_set_noprobe(cur_irq);
  350. #endif
  351. }
  352. /* Unmask max77888 interrupt */
  353. ret = max77888_read_reg(max77888->i2c, MAX77888_PMIC_REG_INTSRC_MASK,
  354. &i2c_data);
  355. if (ret) {
  356. dev_err(max77888->dev, "%s: fail to read muic reg\n", __func__);
  357. return ret;
  358. }
  359. i2c_data &= ~(MAX77888_IRQSRC_CHG); /* Unmask charger interrupt */
  360. i2c_data &= ~(MAX77888_IRQSRC_MUIC); /* Unmask muic interrupt */
  361. max77888_write_reg(max77888->i2c, MAX77888_PMIC_REG_INTSRC_MASK,
  362. i2c_data);
  363. ret = request_threaded_irq(max77888->irq, NULL, max77888_irq_thread,
  364. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  365. "max77888-irq", max77888);
  366. if (ret) {
  367. dev_err(max77888->dev, "Failed to request IRQ %d: %d\n",
  368. max77888->irq, ret);
  369. return ret;
  370. }
  371. #ifdef CONFIG_MUIC_RESET_PIN_ENABLE
  372. if (muic_reset_pin)
  373. {
  374. ret = request_threaded_irq(max77888->irq_reset, NULL, max77888_reset_irq_thread,
  375. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  376. "max77888-reset_irq", max77888);
  377. if (ret) {
  378. dev_err(max77888->dev, "Failed to request IRQ %d: %d\n",
  379. max77888->irq_reset, ret);
  380. return ret;
  381. }
  382. }
  383. #endif
  384. return 0;
  385. }
  386. void max77888_irq_exit(struct max77888_dev *max77888)
  387. {
  388. if (max77888->irq)
  389. free_irq(max77888->irq, max77888);
  390. #ifdef CONFIG_MUIC_RESET_PIN_ENABLE
  391. if (muic_reset_pin)
  392. {
  393. if (max77888->irq_reset)
  394. free_irq(max77888->irq_reset, max77888);
  395. }
  396. #endif
  397. }