max8997-irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * max8997-irq.c - Interrupt controller support for MAX8997
  3. *
  4. * Copyright (C) 2011 Samsung Electronics Co.Ltd
  5. * MyungJoo Ham <myungjoo.ham@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 max8998-irq.c
  22. */
  23. #include <linux/err.h>
  24. #include <linux/irq.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/mfd/max8997.h>
  27. #include <linux/mfd/max8997-private.h>
  28. static const u8 max8997_mask_reg[] = {
  29. [PMIC_INT1] = MAX8997_REG_INT1MSK,
  30. [PMIC_INT2] = MAX8997_REG_INT2MSK,
  31. [PMIC_INT3] = MAX8997_REG_INT3MSK,
  32. [PMIC_INT4] = MAX8997_REG_INT4MSK,
  33. [FUEL_GAUGE] = MAX8997_REG_INVALID,
  34. [MUIC_INT1] = MAX8997_MUIC_REG_INTMASK1,
  35. [MUIC_INT2] = MAX8997_MUIC_REG_INTMASK2,
  36. [MUIC_INT3] = MAX8997_MUIC_REG_INTMASK3,
  37. [GPIO_LOW] = MAX8997_REG_INVALID,
  38. [GPIO_HI] = MAX8997_REG_INVALID,
  39. [FLASH_STATUS] = MAX8997_REG_INVALID,
  40. };
  41. static struct i2c_client *get_i2c(struct max8997_dev *max8997,
  42. enum max8997_irq_source src)
  43. {
  44. switch (src) {
  45. case PMIC_INT1 ... PMIC_INT4:
  46. return max8997->i2c;
  47. case FUEL_GAUGE:
  48. return NULL;
  49. case MUIC_INT1 ... MUIC_INT3:
  50. return max8997->muic;
  51. case GPIO_LOW ... GPIO_HI:
  52. return max8997->i2c;
  53. case FLASH_STATUS:
  54. return max8997->i2c;
  55. default:
  56. return ERR_PTR(-EINVAL);
  57. }
  58. return ERR_PTR(-EINVAL);
  59. }
  60. struct max8997_irq_data {
  61. int mask;
  62. enum max8997_irq_source group;
  63. };
  64. #define DECLARE_IRQ(idx, _group, _mask) \
  65. [(idx)] = { .group = (_group), .mask = (_mask) }
  66. static const struct max8997_irq_data max8997_irqs[] = {
  67. DECLARE_IRQ(MAX8997_PMICIRQ_PWRONR, PMIC_INT1, 1 << 0),
  68. DECLARE_IRQ(MAX8997_PMICIRQ_PWRONF, PMIC_INT1, 1 << 1),
  69. DECLARE_IRQ(MAX8997_PMICIRQ_PWRON1SEC, PMIC_INT1, 1 << 3),
  70. DECLARE_IRQ(MAX8997_PMICIRQ_JIGONR, PMIC_INT1, 1 << 4),
  71. DECLARE_IRQ(MAX8997_PMICIRQ_JIGONF, PMIC_INT1, 1 << 5),
  72. DECLARE_IRQ(MAX8997_PMICIRQ_LOWBAT2, PMIC_INT1, 1 << 6),
  73. DECLARE_IRQ(MAX8997_PMICIRQ_LOWBAT1, PMIC_INT1, 1 << 7),
  74. DECLARE_IRQ(MAX8997_PMICIRQ_JIGR, PMIC_INT2, 1 << 0),
  75. DECLARE_IRQ(MAX8997_PMICIRQ_JIGF, PMIC_INT2, 1 << 1),
  76. DECLARE_IRQ(MAX8997_PMICIRQ_MR, PMIC_INT2, 1 << 2),
  77. DECLARE_IRQ(MAX8997_PMICIRQ_DVS1OK, PMIC_INT2, 1 << 3),
  78. DECLARE_IRQ(MAX8997_PMICIRQ_DVS2OK, PMIC_INT2, 1 << 4),
  79. DECLARE_IRQ(MAX8997_PMICIRQ_DVS3OK, PMIC_INT2, 1 << 5),
  80. DECLARE_IRQ(MAX8997_PMICIRQ_DVS4OK, PMIC_INT2, 1 << 6),
  81. DECLARE_IRQ(MAX8997_PMICIRQ_CHGINS, PMIC_INT3, 1 << 0),
  82. DECLARE_IRQ(MAX8997_PMICIRQ_CHGRM, PMIC_INT3, 1 << 1),
  83. DECLARE_IRQ(MAX8997_PMICIRQ_DCINOVP, PMIC_INT3, 1 << 2),
  84. DECLARE_IRQ(MAX8997_PMICIRQ_TOPOFFR, PMIC_INT3, 1 << 3),
  85. DECLARE_IRQ(MAX8997_PMICIRQ_CHGRSTF, PMIC_INT3, 1 << 5),
  86. DECLARE_IRQ(MAX8997_PMICIRQ_MBCHGTMEXPD, PMIC_INT3, 1 << 7),
  87. DECLARE_IRQ(MAX8997_PMICIRQ_RTC60S, PMIC_INT4, 1 << 0),
  88. DECLARE_IRQ(MAX8997_PMICIRQ_RTCA1, PMIC_INT4, 1 << 1),
  89. DECLARE_IRQ(MAX8997_PMICIRQ_RTCA2, PMIC_INT4, 1 << 2),
  90. DECLARE_IRQ(MAX8997_PMICIRQ_SMPL_INT, PMIC_INT4, 1 << 3),
  91. DECLARE_IRQ(MAX8997_PMICIRQ_RTC1S, PMIC_INT4, 1 << 4),
  92. DECLARE_IRQ(MAX8997_PMICIRQ_WTSR, PMIC_INT4, 1 << 5),
  93. DECLARE_IRQ(MAX8997_MUICIRQ_ADCError, MUIC_INT1, 1 << 2),
  94. DECLARE_IRQ(MAX8997_MUICIRQ_ADCLow, MUIC_INT1, 1 << 1),
  95. DECLARE_IRQ(MAX8997_MUICIRQ_ADC, MUIC_INT1, 1 << 0),
  96. DECLARE_IRQ(MAX8997_MUICIRQ_VBVolt, MUIC_INT2, 1 << 4),
  97. DECLARE_IRQ(MAX8997_MUICIRQ_DBChg, MUIC_INT2, 1 << 3),
  98. DECLARE_IRQ(MAX8997_MUICIRQ_DCDTmr, MUIC_INT2, 1 << 2),
  99. DECLARE_IRQ(MAX8997_MUICIRQ_ChgDetRun, MUIC_INT2, 1 << 1),
  100. DECLARE_IRQ(MAX8997_MUICIRQ_ChgTyp, MUIC_INT2, 1 << 0),
  101. DECLARE_IRQ(MAX8997_MUICIRQ_OVP, MUIC_INT3, 1 << 2),
  102. };
  103. static void max8997_irq_lock(struct irq_data *data)
  104. {
  105. struct max8997_dev *max8997 = irq_get_chip_data(data->irq);
  106. mutex_lock(&max8997->irqlock);
  107. }
  108. static void max8997_irq_sync_unlock(struct irq_data *data)
  109. {
  110. struct max8997_dev *max8997 = irq_get_chip_data(data->irq);
  111. int i;
  112. for (i = 0; i < MAX8997_IRQ_GROUP_NR; i++) {
  113. u8 mask_reg = max8997_mask_reg[i];
  114. struct i2c_client *i2c = get_i2c(max8997, i);
  115. if (mask_reg == MAX8997_REG_INVALID ||
  116. IS_ERR_OR_NULL(i2c))
  117. continue;
  118. max8997->irq_masks_cache[i] = max8997->irq_masks_cur[i];
  119. max8997_write_reg(i2c, max8997_mask_reg[i],
  120. max8997->irq_masks_cur[i]);
  121. }
  122. mutex_unlock(&max8997->irqlock);
  123. }
  124. static const inline struct max8997_irq_data *
  125. irq_to_max8997_irq(struct max8997_dev *max8997, int irq)
  126. {
  127. return &max8997_irqs[irq - max8997->irq_base];
  128. }
  129. static void max8997_irq_mask(struct irq_data *data)
  130. {
  131. struct max8997_dev *max8997 = irq_get_chip_data(data->irq);
  132. const struct max8997_irq_data *irq_data = irq_to_max8997_irq(max8997,
  133. data->irq);
  134. max8997->irq_masks_cur[irq_data->group] |= irq_data->mask;
  135. }
  136. static void max8997_irq_unmask(struct irq_data *data)
  137. {
  138. struct max8997_dev *max8997 = irq_get_chip_data(data->irq);
  139. const struct max8997_irq_data *irq_data = irq_to_max8997_irq(max8997,
  140. data->irq);
  141. max8997->irq_masks_cur[irq_data->group] &= ~irq_data->mask;
  142. }
  143. static struct irq_chip max8997_irq_chip = {
  144. .name = "max8997",
  145. .irq_bus_lock = max8997_irq_lock,
  146. .irq_bus_sync_unlock = max8997_irq_sync_unlock,
  147. .irq_mask = max8997_irq_mask,
  148. .irq_unmask = max8997_irq_unmask,
  149. };
  150. #define MAX8997_IRQSRC_PMIC (1 << 1)
  151. #define MAX8997_IRQSRC_FUELGAUGE (1 << 2)
  152. #define MAX8997_IRQSRC_MUIC (1 << 3)
  153. #define MAX8997_IRQSRC_GPIO (1 << 4)
  154. #define MAX8997_IRQSRC_FLASH (1 << 5)
  155. static irqreturn_t max8997_irq_thread(int irq, void *data)
  156. {
  157. struct max8997_dev *max8997 = data;
  158. u8 irq_reg[MAX8997_IRQ_GROUP_NR] = {};
  159. u8 irq_src;
  160. int ret;
  161. int i;
  162. ret = max8997_read_reg(max8997->i2c, MAX8997_REG_INTSRC, &irq_src);
  163. if (ret < 0) {
  164. dev_err(max8997->dev, "Failed to read interrupt source: %d\n",
  165. ret);
  166. return IRQ_NONE;
  167. }
  168. if (irq_src & MAX8997_IRQSRC_PMIC) {
  169. /* PMIC INT1 ~ INT4 */
  170. max8997_bulk_read(max8997->i2c, MAX8997_REG_INT1, 4,
  171. &irq_reg[PMIC_INT1]);
  172. }
  173. if (irq_src & MAX8997_IRQSRC_FUELGAUGE) {
  174. /*
  175. * TODO: FUEL GAUGE
  176. *
  177. * This is to be supported by Max17042 driver. When
  178. * an interrupt incurs here, it should be relayed to a
  179. * Max17042 device that is connected (probably by
  180. * platform-data). However, we do not have interrupt
  181. * handling in Max17042 driver currently. The Max17042 IRQ
  182. * driver should be ready to be used as a stand-alone device and
  183. * a Max8997-dependent device. Because it is not ready in
  184. * Max17042-side and it is not too critical in operating
  185. * Max8997, we do not implement this in initial releases.
  186. */
  187. irq_reg[FUEL_GAUGE] = 0;
  188. }
  189. if (irq_src & MAX8997_IRQSRC_MUIC) {
  190. /* MUIC INT1 ~ INT3 */
  191. max8997_bulk_read(max8997->muic, MAX8997_MUIC_REG_INT1, 3,
  192. &irq_reg[MUIC_INT1]);
  193. }
  194. if (irq_src & MAX8997_IRQSRC_GPIO) {
  195. /* GPIO Interrupt */
  196. u8 gpio_info[MAX8997_NUM_GPIO];
  197. irq_reg[GPIO_LOW] = 0;
  198. irq_reg[GPIO_HI] = 0;
  199. max8997_bulk_read(max8997->i2c, MAX8997_REG_GPIOCNTL1,
  200. MAX8997_NUM_GPIO, gpio_info);
  201. for (i = 0; i < MAX8997_NUM_GPIO; i++) {
  202. bool interrupt = false;
  203. switch (gpio_info[i] & MAX8997_GPIO_INT_MASK) {
  204. case MAX8997_GPIO_INT_BOTH:
  205. if (max8997->gpio_status[i] != gpio_info[i])
  206. interrupt = true;
  207. break;
  208. case MAX8997_GPIO_INT_RISE:
  209. if ((max8997->gpio_status[i] != gpio_info[i]) &&
  210. (gpio_info[i] & MAX8997_GPIO_DATA_MASK))
  211. interrupt = true;
  212. break;
  213. case MAX8997_GPIO_INT_FALL:
  214. if ((max8997->gpio_status[i] != gpio_info[i]) &&
  215. !(gpio_info[i] & MAX8997_GPIO_DATA_MASK))
  216. interrupt = true;
  217. break;
  218. default:
  219. break;
  220. }
  221. if (interrupt) {
  222. if (i < 8)
  223. irq_reg[GPIO_LOW] |= (1 << i);
  224. else
  225. irq_reg[GPIO_HI] |= (1 << (i - 8));
  226. }
  227. }
  228. }
  229. if (irq_src & MAX8997_IRQSRC_FLASH) {
  230. /* Flash Status Interrupt */
  231. ret = max8997_read_reg(max8997->i2c, MAX8997_REG_FLASHSTATUS,
  232. &irq_reg[FLASH_STATUS]);
  233. }
  234. /* Apply masking */
  235. for (i = 0; i < MAX8997_IRQ_GROUP_NR; i++)
  236. irq_reg[i] &= ~max8997->irq_masks_cur[i];
  237. /* Report */
  238. for (i = 0; i < MAX8997_IRQ_NR; i++) {
  239. if (irq_reg[max8997_irqs[i].group] & max8997_irqs[i].mask)
  240. handle_nested_irq(max8997->irq_base + i);
  241. }
  242. return IRQ_HANDLED;
  243. }
  244. int max8997_irq_resume(struct max8997_dev *max8997)
  245. {
  246. if (max8997->irq && max8997->irq_base)
  247. max8997_irq_thread(max8997->irq_base, max8997);
  248. return 0;
  249. }
  250. int max8997_irq_init(struct max8997_dev *max8997)
  251. {
  252. int i;
  253. int cur_irq;
  254. int ret;
  255. u8 val;
  256. if (!max8997->irq) {
  257. dev_warn(max8997->dev, "No interrupt specified.\n");
  258. max8997->irq_base = 0;
  259. return 0;
  260. }
  261. if (!max8997->irq_base) {
  262. dev_err(max8997->dev, "No interrupt base specified.\n");
  263. return 0;
  264. }
  265. mutex_init(&max8997->irqlock);
  266. /* Mask individual interrupt sources */
  267. for (i = 0; i < MAX8997_IRQ_GROUP_NR; i++) {
  268. struct i2c_client *i2c;
  269. max8997->irq_masks_cur[i] = 0xff;
  270. max8997->irq_masks_cache[i] = 0xff;
  271. i2c = get_i2c(max8997, i);
  272. if (IS_ERR_OR_NULL(i2c))
  273. continue;
  274. if (max8997_mask_reg[i] == MAX8997_REG_INVALID)
  275. continue;
  276. max8997_write_reg(i2c, max8997_mask_reg[i], 0xff);
  277. }
  278. for (i = 0; i < MAX8997_NUM_GPIO; i++) {
  279. max8997->gpio_status[i] = (max8997_read_reg(max8997->i2c,
  280. MAX8997_REG_GPIOCNTL1 + i,
  281. &val)
  282. & MAX8997_GPIO_DATA_MASK) ?
  283. true : false;
  284. }
  285. /* Register with genirq */
  286. for (i = 0; i < MAX8997_IRQ_NR; i++) {
  287. cur_irq = i + max8997->irq_base;
  288. irq_set_chip_data(cur_irq, max8997);
  289. irq_set_chip_and_handler(cur_irq, &max8997_irq_chip,
  290. handle_edge_irq);
  291. irq_set_nested_thread(cur_irq, 1);
  292. #ifdef CONFIG_ARM
  293. set_irq_flags(cur_irq, IRQF_VALID);
  294. #else
  295. irq_set_noprobe(cur_irq);
  296. #endif
  297. }
  298. ret = request_threaded_irq(max8997->irq, NULL, max8997_irq_thread,
  299. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  300. "max8997-irq", max8997);
  301. if (ret) {
  302. dev_err(max8997->dev, "Failed to request IRQ %d: %d\n",
  303. max8997->irq, ret);
  304. return ret;
  305. }
  306. if (!max8997->ono)
  307. return 0;
  308. ret = request_threaded_irq(max8997->ono, NULL, max8997_irq_thread,
  309. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
  310. IRQF_ONESHOT, "max8997-ono", max8997);
  311. if (ret)
  312. dev_err(max8997->dev, "Failed to request ono-IRQ %d: %d\n",
  313. max8997->ono, ret);
  314. return 0;
  315. }
  316. void max8997_irq_exit(struct max8997_dev *max8997)
  317. {
  318. if (max8997->ono)
  319. free_irq(max8997->ono, max8997);
  320. if (max8997->irq)
  321. free_irq(max8997->irq, max8997);
  322. }