max8997-irq.c 11 KB

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