wm831x-irq.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. * wm831x-irq.c -- Interrupt controller support for Wolfson WM831x PMICs
  3. *
  4. * Copyright 2009 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/i2c.h>
  17. #include <linux/irq.h>
  18. #include <linux/mfd/core.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irqdomain.h>
  21. #include <linux/mfd/wm831x/core.h>
  22. #include <linux/mfd/wm831x/pdata.h>
  23. #include <linux/mfd/wm831x/gpio.h>
  24. #include <linux/mfd/wm831x/irq.h>
  25. #include <linux/delay.h>
  26. struct wm831x_irq_data {
  27. int primary;
  28. int reg;
  29. int mask;
  30. };
  31. static struct wm831x_irq_data wm831x_irqs[] = {
  32. [WM831X_IRQ_TEMP_THW] = {
  33. .primary = WM831X_TEMP_INT,
  34. .reg = 1,
  35. .mask = WM831X_TEMP_THW_EINT,
  36. },
  37. [WM831X_IRQ_GPIO_1] = {
  38. .primary = WM831X_GP_INT,
  39. .reg = 5,
  40. .mask = WM831X_GP1_EINT,
  41. },
  42. [WM831X_IRQ_GPIO_2] = {
  43. .primary = WM831X_GP_INT,
  44. .reg = 5,
  45. .mask = WM831X_GP2_EINT,
  46. },
  47. [WM831X_IRQ_GPIO_3] = {
  48. .primary = WM831X_GP_INT,
  49. .reg = 5,
  50. .mask = WM831X_GP3_EINT,
  51. },
  52. [WM831X_IRQ_GPIO_4] = {
  53. .primary = WM831X_GP_INT,
  54. .reg = 5,
  55. .mask = WM831X_GP4_EINT,
  56. },
  57. [WM831X_IRQ_GPIO_5] = {
  58. .primary = WM831X_GP_INT,
  59. .reg = 5,
  60. .mask = WM831X_GP5_EINT,
  61. },
  62. [WM831X_IRQ_GPIO_6] = {
  63. .primary = WM831X_GP_INT,
  64. .reg = 5,
  65. .mask = WM831X_GP6_EINT,
  66. },
  67. [WM831X_IRQ_GPIO_7] = {
  68. .primary = WM831X_GP_INT,
  69. .reg = 5,
  70. .mask = WM831X_GP7_EINT,
  71. },
  72. [WM831X_IRQ_GPIO_8] = {
  73. .primary = WM831X_GP_INT,
  74. .reg = 5,
  75. .mask = WM831X_GP8_EINT,
  76. },
  77. [WM831X_IRQ_GPIO_9] = {
  78. .primary = WM831X_GP_INT,
  79. .reg = 5,
  80. .mask = WM831X_GP9_EINT,
  81. },
  82. [WM831X_IRQ_GPIO_10] = {
  83. .primary = WM831X_GP_INT,
  84. .reg = 5,
  85. .mask = WM831X_GP10_EINT,
  86. },
  87. [WM831X_IRQ_GPIO_11] = {
  88. .primary = WM831X_GP_INT,
  89. .reg = 5,
  90. .mask = WM831X_GP11_EINT,
  91. },
  92. [WM831X_IRQ_GPIO_12] = {
  93. .primary = WM831X_GP_INT,
  94. .reg = 5,
  95. .mask = WM831X_GP12_EINT,
  96. },
  97. [WM831X_IRQ_GPIO_13] = {
  98. .primary = WM831X_GP_INT,
  99. .reg = 5,
  100. .mask = WM831X_GP13_EINT,
  101. },
  102. [WM831X_IRQ_GPIO_14] = {
  103. .primary = WM831X_GP_INT,
  104. .reg = 5,
  105. .mask = WM831X_GP14_EINT,
  106. },
  107. [WM831X_IRQ_GPIO_15] = {
  108. .primary = WM831X_GP_INT,
  109. .reg = 5,
  110. .mask = WM831X_GP15_EINT,
  111. },
  112. [WM831X_IRQ_GPIO_16] = {
  113. .primary = WM831X_GP_INT,
  114. .reg = 5,
  115. .mask = WM831X_GP16_EINT,
  116. },
  117. [WM831X_IRQ_ON] = {
  118. .primary = WM831X_ON_PIN_INT,
  119. .reg = 1,
  120. .mask = WM831X_ON_PIN_EINT,
  121. },
  122. [WM831X_IRQ_PPM_SYSLO] = {
  123. .primary = WM831X_PPM_INT,
  124. .reg = 1,
  125. .mask = WM831X_PPM_SYSLO_EINT,
  126. },
  127. [WM831X_IRQ_PPM_PWR_SRC] = {
  128. .primary = WM831X_PPM_INT,
  129. .reg = 1,
  130. .mask = WM831X_PPM_PWR_SRC_EINT,
  131. },
  132. [WM831X_IRQ_PPM_USB_CURR] = {
  133. .primary = WM831X_PPM_INT,
  134. .reg = 1,
  135. .mask = WM831X_PPM_USB_CURR_EINT,
  136. },
  137. [WM831X_IRQ_WDOG_TO] = {
  138. .primary = WM831X_WDOG_INT,
  139. .reg = 1,
  140. .mask = WM831X_WDOG_TO_EINT,
  141. },
  142. [WM831X_IRQ_RTC_PER] = {
  143. .primary = WM831X_RTC_INT,
  144. .reg = 1,
  145. .mask = WM831X_RTC_PER_EINT,
  146. },
  147. [WM831X_IRQ_RTC_ALM] = {
  148. .primary = WM831X_RTC_INT,
  149. .reg = 1,
  150. .mask = WM831X_RTC_ALM_EINT,
  151. },
  152. [WM831X_IRQ_CHG_BATT_HOT] = {
  153. .primary = WM831X_CHG_INT,
  154. .reg = 2,
  155. .mask = WM831X_CHG_BATT_HOT_EINT,
  156. },
  157. [WM831X_IRQ_CHG_BATT_COLD] = {
  158. .primary = WM831X_CHG_INT,
  159. .reg = 2,
  160. .mask = WM831X_CHG_BATT_COLD_EINT,
  161. },
  162. [WM831X_IRQ_CHG_BATT_FAIL] = {
  163. .primary = WM831X_CHG_INT,
  164. .reg = 2,
  165. .mask = WM831X_CHG_BATT_FAIL_EINT,
  166. },
  167. [WM831X_IRQ_CHG_OV] = {
  168. .primary = WM831X_CHG_INT,
  169. .reg = 2,
  170. .mask = WM831X_CHG_OV_EINT,
  171. },
  172. [WM831X_IRQ_CHG_END] = {
  173. .primary = WM831X_CHG_INT,
  174. .reg = 2,
  175. .mask = WM831X_CHG_END_EINT,
  176. },
  177. [WM831X_IRQ_CHG_TO] = {
  178. .primary = WM831X_CHG_INT,
  179. .reg = 2,
  180. .mask = WM831X_CHG_TO_EINT,
  181. },
  182. [WM831X_IRQ_CHG_MODE] = {
  183. .primary = WM831X_CHG_INT,
  184. .reg = 2,
  185. .mask = WM831X_CHG_MODE_EINT,
  186. },
  187. [WM831X_IRQ_CHG_START] = {
  188. .primary = WM831X_CHG_INT,
  189. .reg = 2,
  190. .mask = WM831X_CHG_START_EINT,
  191. },
  192. [WM831X_IRQ_TCHDATA] = {
  193. .primary = WM831X_TCHDATA_INT,
  194. .reg = 1,
  195. .mask = WM831X_TCHDATA_EINT,
  196. },
  197. [WM831X_IRQ_TCHPD] = {
  198. .primary = WM831X_TCHPD_INT,
  199. .reg = 1,
  200. .mask = WM831X_TCHPD_EINT,
  201. },
  202. [WM831X_IRQ_AUXADC_DATA] = {
  203. .primary = WM831X_AUXADC_INT,
  204. .reg = 1,
  205. .mask = WM831X_AUXADC_DATA_EINT,
  206. },
  207. [WM831X_IRQ_AUXADC_DCOMP1] = {
  208. .primary = WM831X_AUXADC_INT,
  209. .reg = 1,
  210. .mask = WM831X_AUXADC_DCOMP1_EINT,
  211. },
  212. [WM831X_IRQ_AUXADC_DCOMP2] = {
  213. .primary = WM831X_AUXADC_INT,
  214. .reg = 1,
  215. .mask = WM831X_AUXADC_DCOMP2_EINT,
  216. },
  217. [WM831X_IRQ_AUXADC_DCOMP3] = {
  218. .primary = WM831X_AUXADC_INT,
  219. .reg = 1,
  220. .mask = WM831X_AUXADC_DCOMP3_EINT,
  221. },
  222. [WM831X_IRQ_AUXADC_DCOMP4] = {
  223. .primary = WM831X_AUXADC_INT,
  224. .reg = 1,
  225. .mask = WM831X_AUXADC_DCOMP4_EINT,
  226. },
  227. [WM831X_IRQ_CS1] = {
  228. .primary = WM831X_CS_INT,
  229. .reg = 2,
  230. .mask = WM831X_CS1_EINT,
  231. },
  232. [WM831X_IRQ_CS2] = {
  233. .primary = WM831X_CS_INT,
  234. .reg = 2,
  235. .mask = WM831X_CS2_EINT,
  236. },
  237. [WM831X_IRQ_HC_DC1] = {
  238. .primary = WM831X_HC_INT,
  239. .reg = 4,
  240. .mask = WM831X_HC_DC1_EINT,
  241. },
  242. [WM831X_IRQ_HC_DC2] = {
  243. .primary = WM831X_HC_INT,
  244. .reg = 4,
  245. .mask = WM831X_HC_DC2_EINT,
  246. },
  247. [WM831X_IRQ_UV_LDO1] = {
  248. .primary = WM831X_UV_INT,
  249. .reg = 3,
  250. .mask = WM831X_UV_LDO1_EINT,
  251. },
  252. [WM831X_IRQ_UV_LDO2] = {
  253. .primary = WM831X_UV_INT,
  254. .reg = 3,
  255. .mask = WM831X_UV_LDO2_EINT,
  256. },
  257. [WM831X_IRQ_UV_LDO3] = {
  258. .primary = WM831X_UV_INT,
  259. .reg = 3,
  260. .mask = WM831X_UV_LDO3_EINT,
  261. },
  262. [WM831X_IRQ_UV_LDO4] = {
  263. .primary = WM831X_UV_INT,
  264. .reg = 3,
  265. .mask = WM831X_UV_LDO4_EINT,
  266. },
  267. [WM831X_IRQ_UV_LDO5] = {
  268. .primary = WM831X_UV_INT,
  269. .reg = 3,
  270. .mask = WM831X_UV_LDO5_EINT,
  271. },
  272. [WM831X_IRQ_UV_LDO6] = {
  273. .primary = WM831X_UV_INT,
  274. .reg = 3,
  275. .mask = WM831X_UV_LDO6_EINT,
  276. },
  277. [WM831X_IRQ_UV_LDO7] = {
  278. .primary = WM831X_UV_INT,
  279. .reg = 3,
  280. .mask = WM831X_UV_LDO7_EINT,
  281. },
  282. [WM831X_IRQ_UV_LDO8] = {
  283. .primary = WM831X_UV_INT,
  284. .reg = 3,
  285. .mask = WM831X_UV_LDO8_EINT,
  286. },
  287. [WM831X_IRQ_UV_LDO9] = {
  288. .primary = WM831X_UV_INT,
  289. .reg = 3,
  290. .mask = WM831X_UV_LDO9_EINT,
  291. },
  292. [WM831X_IRQ_UV_LDO10] = {
  293. .primary = WM831X_UV_INT,
  294. .reg = 3,
  295. .mask = WM831X_UV_LDO10_EINT,
  296. },
  297. [WM831X_IRQ_UV_DC1] = {
  298. .primary = WM831X_UV_INT,
  299. .reg = 4,
  300. .mask = WM831X_UV_DC1_EINT,
  301. },
  302. [WM831X_IRQ_UV_DC2] = {
  303. .primary = WM831X_UV_INT,
  304. .reg = 4,
  305. .mask = WM831X_UV_DC2_EINT,
  306. },
  307. [WM831X_IRQ_UV_DC3] = {
  308. .primary = WM831X_UV_INT,
  309. .reg = 4,
  310. .mask = WM831X_UV_DC3_EINT,
  311. },
  312. [WM831X_IRQ_UV_DC4] = {
  313. .primary = WM831X_UV_INT,
  314. .reg = 4,
  315. .mask = WM831X_UV_DC4_EINT,
  316. },
  317. };
  318. static inline int irq_data_to_status_reg(struct wm831x_irq_data *irq_data)
  319. {
  320. return WM831X_INTERRUPT_STATUS_1 - 1 + irq_data->reg;
  321. }
  322. static inline struct wm831x_irq_data *irq_to_wm831x_irq(struct wm831x *wm831x,
  323. int irq)
  324. {
  325. return &wm831x_irqs[irq];
  326. }
  327. static void wm831x_irq_lock(struct irq_data *data)
  328. {
  329. struct wm831x *wm831x = irq_data_get_irq_chip_data(data);
  330. mutex_lock(&wm831x->irq_lock);
  331. }
  332. static void wm831x_irq_sync_unlock(struct irq_data *data)
  333. {
  334. struct wm831x *wm831x = irq_data_get_irq_chip_data(data);
  335. int i;
  336. for (i = 0; i < ARRAY_SIZE(wm831x->gpio_update); i++) {
  337. if (wm831x->gpio_update[i]) {
  338. wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + i,
  339. WM831X_GPN_INT_MODE | WM831X_GPN_POL,
  340. wm831x->gpio_update[i]);
  341. wm831x->gpio_update[i] = 0;
  342. }
  343. }
  344. for (i = 0; i < ARRAY_SIZE(wm831x->irq_masks_cur); i++) {
  345. /* If there's been a change in the mask write it back
  346. * to the hardware. */
  347. if (wm831x->irq_masks_cur[i] != wm831x->irq_masks_cache[i]) {
  348. dev_dbg(wm831x->dev, "IRQ mask sync: %x = %x\n",
  349. WM831X_INTERRUPT_STATUS_1_MASK + i,
  350. wm831x->irq_masks_cur[i]);
  351. wm831x->irq_masks_cache[i] = wm831x->irq_masks_cur[i];
  352. wm831x_reg_write(wm831x,
  353. WM831X_INTERRUPT_STATUS_1_MASK + i,
  354. wm831x->irq_masks_cur[i]);
  355. }
  356. }
  357. mutex_unlock(&wm831x->irq_lock);
  358. }
  359. static void wm831x_irq_enable(struct irq_data *data)
  360. {
  361. struct wm831x *wm831x = irq_data_get_irq_chip_data(data);
  362. struct wm831x_irq_data *irq_data = irq_to_wm831x_irq(wm831x,
  363. data->hwirq);
  364. wm831x->irq_masks_cur[irq_data->reg - 1] &= ~irq_data->mask;
  365. }
  366. static void wm831x_irq_disable(struct irq_data *data)
  367. {
  368. struct wm831x *wm831x = irq_data_get_irq_chip_data(data);
  369. struct wm831x_irq_data *irq_data = irq_to_wm831x_irq(wm831x,
  370. data->hwirq);
  371. wm831x->irq_masks_cur[irq_data->reg - 1] |= irq_data->mask;
  372. }
  373. static int wm831x_irq_set_type(struct irq_data *data, unsigned int type)
  374. {
  375. struct wm831x *wm831x = irq_data_get_irq_chip_data(data);
  376. int irq;
  377. irq = data->hwirq;
  378. if (irq < WM831X_IRQ_GPIO_1 || irq > WM831X_IRQ_GPIO_11) {
  379. /* Ignore internal-only IRQs */
  380. if (irq >= 0 && irq < WM831X_NUM_IRQS)
  381. return 0;
  382. else
  383. return -EINVAL;
  384. }
  385. /* Rebase the IRQ into the GPIO range so we've got a sensible array
  386. * index.
  387. */
  388. irq -= WM831X_IRQ_GPIO_1;
  389. /* We set the high bit to flag that we need an update; don't
  390. * do the update here as we can be called with the bus lock
  391. * held.
  392. */
  393. wm831x->gpio_level_low[irq] = false;
  394. wm831x->gpio_level_high[irq] = false;
  395. switch (type) {
  396. case IRQ_TYPE_EDGE_BOTH:
  397. wm831x->gpio_update[irq] = 0x10000 | WM831X_GPN_INT_MODE;
  398. break;
  399. case IRQ_TYPE_EDGE_RISING:
  400. wm831x->gpio_update[irq] = 0x10000 | WM831X_GPN_POL;
  401. break;
  402. case IRQ_TYPE_EDGE_FALLING:
  403. wm831x->gpio_update[irq] = 0x10000;
  404. break;
  405. case IRQ_TYPE_LEVEL_HIGH:
  406. wm831x->gpio_update[irq] = 0x10000 | WM831X_GPN_POL;
  407. wm831x->gpio_level_high[irq] = true;
  408. break;
  409. case IRQ_TYPE_LEVEL_LOW:
  410. wm831x->gpio_update[irq] = 0x10000;
  411. wm831x->gpio_level_low[irq] = true;
  412. break;
  413. default:
  414. return -EINVAL;
  415. }
  416. return 0;
  417. }
  418. static struct irq_chip wm831x_irq_chip = {
  419. .name = "wm831x",
  420. .irq_bus_lock = wm831x_irq_lock,
  421. .irq_bus_sync_unlock = wm831x_irq_sync_unlock,
  422. .irq_disable = wm831x_irq_disable,
  423. .irq_enable = wm831x_irq_enable,
  424. .irq_set_type = wm831x_irq_set_type,
  425. };
  426. /* The processing of the primary interrupt occurs in a thread so that
  427. * we can interact with the device over I2C or SPI. */
  428. static irqreturn_t wm831x_irq_thread(int irq, void *data)
  429. {
  430. struct wm831x *wm831x = data;
  431. unsigned int i;
  432. int primary, status_addr, ret;
  433. int status_regs[WM831X_NUM_IRQ_REGS] = { 0 };
  434. int read[WM831X_NUM_IRQ_REGS] = { 0 };
  435. int *status;
  436. primary = wm831x_reg_read(wm831x, WM831X_SYSTEM_INTERRUPTS);
  437. if (primary < 0) {
  438. dev_err(wm831x->dev, "Failed to read system interrupt: %d\n",
  439. primary);
  440. goto out;
  441. }
  442. /* The touch interrupts are visible in the primary register as
  443. * an optimisation; open code this to avoid complicating the
  444. * main handling loop and so we can also skip iterating the
  445. * descriptors.
  446. */
  447. if (primary & WM831X_TCHPD_INT)
  448. handle_nested_irq(irq_find_mapping(wm831x->irq_domain,
  449. WM831X_IRQ_TCHPD));
  450. if (primary & WM831X_TCHDATA_INT)
  451. handle_nested_irq(irq_find_mapping(wm831x->irq_domain,
  452. WM831X_IRQ_TCHDATA));
  453. primary &= ~(WM831X_TCHDATA_EINT | WM831X_TCHPD_EINT);
  454. for (i = 0; i < ARRAY_SIZE(wm831x_irqs); i++) {
  455. int offset = wm831x_irqs[i].reg - 1;
  456. if (!(primary & wm831x_irqs[i].primary))
  457. continue;
  458. status = &status_regs[offset];
  459. /* Hopefully there should only be one register to read
  460. * each time otherwise we ought to do a block read. */
  461. if (!read[offset]) {
  462. status_addr = irq_data_to_status_reg(&wm831x_irqs[i]);
  463. *status = wm831x_reg_read(wm831x, status_addr);
  464. if (*status < 0) {
  465. dev_err(wm831x->dev,
  466. "Failed to read IRQ status: %d\n",
  467. *status);
  468. goto out;
  469. }
  470. read[offset] = 1;
  471. /* Ignore any bits that we don't think are masked */
  472. *status &= ~wm831x->irq_masks_cur[offset];
  473. /* Acknowledge now so we don't miss
  474. * notifications while we handle.
  475. */
  476. wm831x_reg_write(wm831x, status_addr, *status);
  477. }
  478. if (*status & wm831x_irqs[i].mask)
  479. handle_nested_irq(irq_find_mapping(wm831x->irq_domain,
  480. i));
  481. /* Simulate an edge triggered IRQ by polling the input
  482. * status. This is sucky but improves interoperability.
  483. */
  484. if (primary == WM831X_GP_INT &&
  485. wm831x->gpio_level_high[i - WM831X_IRQ_GPIO_1]) {
  486. ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL);
  487. while (ret & 1 << (i - WM831X_IRQ_GPIO_1)) {
  488. handle_nested_irq(irq_find_mapping(wm831x->irq_domain,
  489. i));
  490. ret = wm831x_reg_read(wm831x,
  491. WM831X_GPIO_LEVEL);
  492. }
  493. }
  494. if (primary == WM831X_GP_INT &&
  495. wm831x->gpio_level_low[i - WM831X_IRQ_GPIO_1]) {
  496. ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL);
  497. while (!(ret & 1 << (i - WM831X_IRQ_GPIO_1))) {
  498. handle_nested_irq(irq_find_mapping(wm831x->irq_domain,
  499. i));
  500. ret = wm831x_reg_read(wm831x,
  501. WM831X_GPIO_LEVEL);
  502. }
  503. }
  504. }
  505. out:
  506. return IRQ_HANDLED;
  507. }
  508. static int wm831x_irq_map(struct irq_domain *h, unsigned int virq,
  509. irq_hw_number_t hw)
  510. {
  511. irq_set_chip_data(virq, h->host_data);
  512. irq_set_chip_and_handler(virq, &wm831x_irq_chip, handle_edge_irq);
  513. irq_set_nested_thread(virq, 1);
  514. irq_set_noprobe(virq);
  515. return 0;
  516. }
  517. static const struct irq_domain_ops wm831x_irq_domain_ops = {
  518. .map = wm831x_irq_map,
  519. .xlate = irq_domain_xlate_twocell,
  520. };
  521. int wm831x_irq_init(struct wm831x *wm831x, int irq)
  522. {
  523. struct wm831x_pdata *pdata = &wm831x->pdata;
  524. struct irq_domain *domain;
  525. int i, ret, irq_base;
  526. mutex_init(&wm831x->irq_lock);
  527. /* Mask the individual interrupt sources */
  528. for (i = 0; i < ARRAY_SIZE(wm831x->irq_masks_cur); i++) {
  529. wm831x->irq_masks_cur[i] = 0xffff;
  530. wm831x->irq_masks_cache[i] = 0xffff;
  531. wm831x_reg_write(wm831x, WM831X_INTERRUPT_STATUS_1_MASK + i,
  532. 0xffff);
  533. }
  534. /* Try to dynamically allocate IRQs if no base is specified */
  535. if (pdata->irq_base) {
  536. irq_base = irq_alloc_descs(pdata->irq_base, 0,
  537. WM831X_NUM_IRQS, 0);
  538. if (irq_base < 0) {
  539. dev_warn(wm831x->dev, "Failed to allocate IRQs: %d\n",
  540. irq_base);
  541. irq_base = 0;
  542. }
  543. } else {
  544. irq_base = 0;
  545. }
  546. if (irq_base)
  547. domain = irq_domain_add_legacy(wm831x->dev->of_node,
  548. ARRAY_SIZE(wm831x_irqs),
  549. irq_base, 0,
  550. &wm831x_irq_domain_ops,
  551. wm831x);
  552. else
  553. domain = irq_domain_add_linear(wm831x->dev->of_node,
  554. ARRAY_SIZE(wm831x_irqs),
  555. &wm831x_irq_domain_ops,
  556. wm831x);
  557. if (!domain) {
  558. dev_warn(wm831x->dev, "Failed to allocate IRQ domain\n");
  559. return -EINVAL;
  560. }
  561. if (pdata->irq_cmos)
  562. i = 0;
  563. else
  564. i = WM831X_IRQ_OD;
  565. wm831x_set_bits(wm831x, WM831X_IRQ_CONFIG,
  566. WM831X_IRQ_OD, i);
  567. wm831x->irq = irq;
  568. wm831x->irq_domain = domain;
  569. if (irq) {
  570. /* Try to flag /IRQ as a wake source; there are a number of
  571. * unconditional wake sources in the PMIC so this isn't
  572. * conditional but we don't actually care *too* much if it
  573. * fails.
  574. */
  575. ret = enable_irq_wake(irq);
  576. if (ret != 0) {
  577. dev_warn(wm831x->dev,
  578. "Can't enable IRQ as wake source: %d\n",
  579. ret);
  580. }
  581. ret = request_threaded_irq(irq, NULL, wm831x_irq_thread,
  582. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  583. "wm831x", wm831x);
  584. if (ret != 0) {
  585. dev_err(wm831x->dev, "Failed to request IRQ %d: %d\n",
  586. irq, ret);
  587. return ret;
  588. }
  589. } else {
  590. dev_warn(wm831x->dev,
  591. "No interrupt specified - functionality limited\n");
  592. }
  593. /* Enable top level interrupts, we mask at secondary level */
  594. wm831x_reg_write(wm831x, WM831X_SYSTEM_INTERRUPTS_MASK, 0);
  595. return 0;
  596. }
  597. void wm831x_irq_exit(struct wm831x *wm831x)
  598. {
  599. if (wm831x->irq)
  600. free_irq(wm831x->irq, wm831x);
  601. }