2 * wm831x-irq.c -- Interrupt controller support for Wolfson WM831x PMICs
4 * Copyright 2009 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/irq.h>
19 #include <linux/mfd/core.h>
20 #include <linux/interrupt.h>
22 #include <linux/mfd/wm831x/core.h>
23 #include <linux/mfd/wm831x/pdata.h>
24 #include <linux/mfd/wm831x/gpio.h>
25 #include <linux/mfd/wm831x/irq.h>
27 #include <linux/delay.h>
30 * Since generic IRQs don't currently support interrupt controllers on
31 * interrupt driven buses we don't use genirq but instead provide an
32 * interface that looks very much like the standard ones. This leads
33 * to some bodges, including storing interrupt handler information in
34 * the static irq_data table we use to look up the data for individual
35 * interrupts, but hopefully won't last too long.
38 struct wm831x_irq_data
{
44 static struct wm831x_irq_data wm831x_irqs
[] = {
45 [WM831X_IRQ_TEMP_THW
] = {
46 .primary
= WM831X_TEMP_INT
,
48 .mask
= WM831X_TEMP_THW_EINT
,
50 [WM831X_IRQ_GPIO_1
] = {
51 .primary
= WM831X_GP_INT
,
53 .mask
= WM831X_GP1_EINT
,
55 [WM831X_IRQ_GPIO_2
] = {
56 .primary
= WM831X_GP_INT
,
58 .mask
= WM831X_GP2_EINT
,
60 [WM831X_IRQ_GPIO_3
] = {
61 .primary
= WM831X_GP_INT
,
63 .mask
= WM831X_GP3_EINT
,
65 [WM831X_IRQ_GPIO_4
] = {
66 .primary
= WM831X_GP_INT
,
68 .mask
= WM831X_GP4_EINT
,
70 [WM831X_IRQ_GPIO_5
] = {
71 .primary
= WM831X_GP_INT
,
73 .mask
= WM831X_GP5_EINT
,
75 [WM831X_IRQ_GPIO_6
] = {
76 .primary
= WM831X_GP_INT
,
78 .mask
= WM831X_GP6_EINT
,
80 [WM831X_IRQ_GPIO_7
] = {
81 .primary
= WM831X_GP_INT
,
83 .mask
= WM831X_GP7_EINT
,
85 [WM831X_IRQ_GPIO_8
] = {
86 .primary
= WM831X_GP_INT
,
88 .mask
= WM831X_GP8_EINT
,
90 [WM831X_IRQ_GPIO_9
] = {
91 .primary
= WM831X_GP_INT
,
93 .mask
= WM831X_GP9_EINT
,
95 [WM831X_IRQ_GPIO_10
] = {
96 .primary
= WM831X_GP_INT
,
98 .mask
= WM831X_GP10_EINT
,
100 [WM831X_IRQ_GPIO_11
] = {
101 .primary
= WM831X_GP_INT
,
103 .mask
= WM831X_GP11_EINT
,
105 [WM831X_IRQ_GPIO_12
] = {
106 .primary
= WM831X_GP_INT
,
108 .mask
= WM831X_GP12_EINT
,
110 [WM831X_IRQ_GPIO_13
] = {
111 .primary
= WM831X_GP_INT
,
113 .mask
= WM831X_GP13_EINT
,
115 [WM831X_IRQ_GPIO_14
] = {
116 .primary
= WM831X_GP_INT
,
118 .mask
= WM831X_GP14_EINT
,
120 [WM831X_IRQ_GPIO_15
] = {
121 .primary
= WM831X_GP_INT
,
123 .mask
= WM831X_GP15_EINT
,
125 [WM831X_IRQ_GPIO_16
] = {
126 .primary
= WM831X_GP_INT
,
128 .mask
= WM831X_GP16_EINT
,
131 .primary
= WM831X_ON_PIN_INT
,
133 .mask
= WM831X_ON_PIN_EINT
,
135 [WM831X_IRQ_PPM_SYSLO
] = {
136 .primary
= WM831X_PPM_INT
,
138 .mask
= WM831X_PPM_SYSLO_EINT
,
140 [WM831X_IRQ_PPM_PWR_SRC
] = {
141 .primary
= WM831X_PPM_INT
,
143 .mask
= WM831X_PPM_PWR_SRC_EINT
,
145 [WM831X_IRQ_PPM_USB_CURR
] = {
146 .primary
= WM831X_PPM_INT
,
148 .mask
= WM831X_PPM_USB_CURR_EINT
,
150 [WM831X_IRQ_WDOG_TO
] = {
151 .primary
= WM831X_WDOG_INT
,
153 .mask
= WM831X_WDOG_TO_EINT
,
155 [WM831X_IRQ_RTC_PER
] = {
156 .primary
= WM831X_RTC_INT
,
158 .mask
= WM831X_RTC_PER_EINT
,
160 [WM831X_IRQ_RTC_ALM
] = {
161 .primary
= WM831X_RTC_INT
,
163 .mask
= WM831X_RTC_ALM_EINT
,
165 [WM831X_IRQ_CHG_BATT_HOT
] = {
166 .primary
= WM831X_CHG_INT
,
168 .mask
= WM831X_CHG_BATT_HOT_EINT
,
170 [WM831X_IRQ_CHG_BATT_COLD
] = {
171 .primary
= WM831X_CHG_INT
,
173 .mask
= WM831X_CHG_BATT_COLD_EINT
,
175 [WM831X_IRQ_CHG_BATT_FAIL
] = {
176 .primary
= WM831X_CHG_INT
,
178 .mask
= WM831X_CHG_BATT_FAIL_EINT
,
180 [WM831X_IRQ_CHG_OV
] = {
181 .primary
= WM831X_CHG_INT
,
183 .mask
= WM831X_CHG_OV_EINT
,
185 [WM831X_IRQ_CHG_END
] = {
186 .primary
= WM831X_CHG_INT
,
188 .mask
= WM831X_CHG_END_EINT
,
190 [WM831X_IRQ_CHG_TO
] = {
191 .primary
= WM831X_CHG_INT
,
193 .mask
= WM831X_CHG_TO_EINT
,
195 [WM831X_IRQ_CHG_MODE
] = {
196 .primary
= WM831X_CHG_INT
,
198 .mask
= WM831X_CHG_MODE_EINT
,
200 [WM831X_IRQ_CHG_START
] = {
201 .primary
= WM831X_CHG_INT
,
203 .mask
= WM831X_CHG_START_EINT
,
205 [WM831X_IRQ_TCHDATA
] = {
206 .primary
= WM831X_TCHDATA_INT
,
208 .mask
= WM831X_TCHDATA_EINT
,
210 [WM831X_IRQ_TCHPD
] = {
211 .primary
= WM831X_TCHPD_INT
,
213 .mask
= WM831X_TCHPD_EINT
,
215 [WM831X_IRQ_AUXADC_DATA
] = {
216 .primary
= WM831X_AUXADC_INT
,
218 .mask
= WM831X_AUXADC_DATA_EINT
,
220 [WM831X_IRQ_AUXADC_DCOMP1
] = {
221 .primary
= WM831X_AUXADC_INT
,
223 .mask
= WM831X_AUXADC_DCOMP1_EINT
,
225 [WM831X_IRQ_AUXADC_DCOMP2
] = {
226 .primary
= WM831X_AUXADC_INT
,
228 .mask
= WM831X_AUXADC_DCOMP2_EINT
,
230 [WM831X_IRQ_AUXADC_DCOMP3
] = {
231 .primary
= WM831X_AUXADC_INT
,
233 .mask
= WM831X_AUXADC_DCOMP3_EINT
,
235 [WM831X_IRQ_AUXADC_DCOMP4
] = {
236 .primary
= WM831X_AUXADC_INT
,
238 .mask
= WM831X_AUXADC_DCOMP4_EINT
,
241 .primary
= WM831X_CS_INT
,
243 .mask
= WM831X_CS1_EINT
,
246 .primary
= WM831X_CS_INT
,
248 .mask
= WM831X_CS2_EINT
,
250 [WM831X_IRQ_HC_DC1
] = {
251 .primary
= WM831X_HC_INT
,
253 .mask
= WM831X_HC_DC1_EINT
,
255 [WM831X_IRQ_HC_DC2
] = {
256 .primary
= WM831X_HC_INT
,
258 .mask
= WM831X_HC_DC2_EINT
,
260 [WM831X_IRQ_UV_LDO1
] = {
261 .primary
= WM831X_UV_INT
,
263 .mask
= WM831X_UV_LDO1_EINT
,
265 [WM831X_IRQ_UV_LDO2
] = {
266 .primary
= WM831X_UV_INT
,
268 .mask
= WM831X_UV_LDO2_EINT
,
270 [WM831X_IRQ_UV_LDO3
] = {
271 .primary
= WM831X_UV_INT
,
273 .mask
= WM831X_UV_LDO3_EINT
,
275 [WM831X_IRQ_UV_LDO4
] = {
276 .primary
= WM831X_UV_INT
,
278 .mask
= WM831X_UV_LDO4_EINT
,
280 [WM831X_IRQ_UV_LDO5
] = {
281 .primary
= WM831X_UV_INT
,
283 .mask
= WM831X_UV_LDO5_EINT
,
285 [WM831X_IRQ_UV_LDO6
] = {
286 .primary
= WM831X_UV_INT
,
288 .mask
= WM831X_UV_LDO6_EINT
,
290 [WM831X_IRQ_UV_LDO7
] = {
291 .primary
= WM831X_UV_INT
,
293 .mask
= WM831X_UV_LDO7_EINT
,
295 [WM831X_IRQ_UV_LDO8
] = {
296 .primary
= WM831X_UV_INT
,
298 .mask
= WM831X_UV_LDO8_EINT
,
300 [WM831X_IRQ_UV_LDO9
] = {
301 .primary
= WM831X_UV_INT
,
303 .mask
= WM831X_UV_LDO9_EINT
,
305 [WM831X_IRQ_UV_LDO10
] = {
306 .primary
= WM831X_UV_INT
,
308 .mask
= WM831X_UV_LDO10_EINT
,
310 [WM831X_IRQ_UV_DC1
] = {
311 .primary
= WM831X_UV_INT
,
313 .mask
= WM831X_UV_DC1_EINT
,
315 [WM831X_IRQ_UV_DC2
] = {
316 .primary
= WM831X_UV_INT
,
318 .mask
= WM831X_UV_DC2_EINT
,
320 [WM831X_IRQ_UV_DC3
] = {
321 .primary
= WM831X_UV_INT
,
323 .mask
= WM831X_UV_DC3_EINT
,
325 [WM831X_IRQ_UV_DC4
] = {
326 .primary
= WM831X_UV_INT
,
328 .mask
= WM831X_UV_DC4_EINT
,
332 static inline int irq_data_to_status_reg(struct wm831x_irq_data
*irq_data
)
334 return WM831X_INTERRUPT_STATUS_1
- 1 + irq_data
->reg
;
337 static inline int irq_data_to_mask_reg(struct wm831x_irq_data
*irq_data
)
339 return WM831X_INTERRUPT_STATUS_1_MASK
- 1 + irq_data
->reg
;
342 static inline struct wm831x_irq_data
*irq_to_wm831x_irq(struct wm831x
*wm831x
,
345 return &wm831x_irqs
[irq
- wm831x
->irq_base
];
348 static void wm831x_irq_lock(unsigned int irq
)
350 struct wm831x
*wm831x
= get_irq_chip_data(irq
);
352 mutex_lock(&wm831x
->irq_lock
);
355 static void wm831x_irq_sync_unlock(unsigned int irq
)
357 struct wm831x
*wm831x
= get_irq_chip_data(irq
);
360 for (i
= 0; i
< ARRAY_SIZE(wm831x
->irq_masks_cur
); i
++) {
361 /* If there's been a change in the mask write it back
362 * to the hardware. */
363 if (wm831x
->irq_masks_cur
[i
] != wm831x
->irq_masks_cache
[i
]) {
364 wm831x
->irq_masks_cache
[i
] = wm831x
->irq_masks_cur
[i
];
365 wm831x_reg_write(wm831x
,
366 WM831X_INTERRUPT_STATUS_1_MASK
+ i
,
367 wm831x
->irq_masks_cur
[i
]);
371 mutex_unlock(&wm831x
->irq_lock
);
374 static void wm831x_irq_unmask(unsigned int irq
)
376 struct wm831x
*wm831x
= get_irq_chip_data(irq
);
377 struct wm831x_irq_data
*irq_data
= irq_to_wm831x_irq(wm831x
, irq
);
379 wm831x
->irq_masks_cur
[irq_data
->reg
- 1] &= ~irq_data
->mask
;
382 static void wm831x_irq_mask(unsigned int irq
)
384 struct wm831x
*wm831x
= get_irq_chip_data(irq
);
385 struct wm831x_irq_data
*irq_data
= irq_to_wm831x_irq(wm831x
, irq
);
387 wm831x
->irq_masks_cur
[irq_data
->reg
- 1] |= irq_data
->mask
;
390 static int wm831x_irq_set_type(unsigned int irq
, unsigned int type
)
392 struct wm831x
*wm831x
= get_irq_chip_data(irq
);
395 irq
= irq
- wm831x
->irq_base
;
397 if (irq
< WM831X_IRQ_GPIO_1
|| irq
> WM831X_IRQ_GPIO_11
) {
398 /* Ignore internal-only IRQs */
399 if (irq
>= 0 && irq
< WM831X_NUM_IRQS
)
406 case IRQ_TYPE_EDGE_BOTH
:
407 val
= WM831X_GPN_INT_MODE
;
409 case IRQ_TYPE_EDGE_RISING
:
410 val
= WM831X_GPN_POL
;
412 case IRQ_TYPE_EDGE_FALLING
:
419 return wm831x_set_bits(wm831x
, WM831X_GPIO1_CONTROL
+ irq
,
420 WM831X_GPN_INT_MODE
| WM831X_GPN_POL
, val
);
423 static struct irq_chip wm831x_irq_chip
= {
425 .bus_lock
= wm831x_irq_lock
,
426 .bus_sync_unlock
= wm831x_irq_sync_unlock
,
427 .mask
= wm831x_irq_mask
,
428 .unmask
= wm831x_irq_unmask
,
429 .set_type
= wm831x_irq_set_type
,
432 /* The processing of the primary interrupt occurs in a thread so that
433 * we can interact with the device over I2C or SPI. */
434 static irqreturn_t
wm831x_irq_thread(int irq
, void *data
)
436 struct wm831x
*wm831x
= data
;
439 int status_regs
[WM831X_NUM_IRQ_REGS
] = { 0 };
440 int read
[WM831X_NUM_IRQ_REGS
] = { 0 };
443 primary
= wm831x_reg_read(wm831x
, WM831X_SYSTEM_INTERRUPTS
);
445 dev_err(wm831x
->dev
, "Failed to read system interrupt: %d\n",
450 for (i
= 0; i
< ARRAY_SIZE(wm831x_irqs
); i
++) {
451 int offset
= wm831x_irqs
[i
].reg
- 1;
453 if (!(primary
& wm831x_irqs
[i
].primary
))
456 status
= &status_regs
[offset
];
458 /* Hopefully there should only be one register to read
459 * each time otherwise we ought to do a block read. */
461 *status
= wm831x_reg_read(wm831x
,
462 irq_data_to_status_reg(&wm831x_irqs
[i
]));
465 "Failed to read IRQ status: %d\n",
473 /* Report it if it isn't masked, or forget the status. */
474 if ((*status
& ~wm831x
->irq_masks_cur
[offset
])
475 & wm831x_irqs
[i
].mask
)
476 handle_nested_irq(wm831x
->irq_base
+ i
);
478 *status
&= ~wm831x_irqs
[i
].mask
;
482 for (i
= 0; i
< ARRAY_SIZE(status_regs
); i
++) {
484 wm831x_reg_write(wm831x
, WM831X_INTERRUPT_STATUS_1
+ i
,
491 int wm831x_irq_init(struct wm831x
*wm831x
, int irq
)
493 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
496 mutex_init(&wm831x
->irq_lock
);
498 /* Mask the individual interrupt sources */
499 for (i
= 0; i
< ARRAY_SIZE(wm831x
->irq_masks_cur
); i
++) {
500 wm831x
->irq_masks_cur
[i
] = 0xffff;
501 wm831x
->irq_masks_cache
[i
] = 0xffff;
502 wm831x_reg_write(wm831x
, WM831X_INTERRUPT_STATUS_1_MASK
+ i
,
507 dev_warn(wm831x
->dev
,
508 "No interrupt specified - functionality limited\n");
512 if (!pdata
|| !pdata
->irq_base
) {
514 "No interrupt base specified, no interrupts\n");
519 wm831x
->irq_base
= pdata
->irq_base
;
521 /* Register them with genirq */
522 for (cur_irq
= wm831x
->irq_base
;
523 cur_irq
< ARRAY_SIZE(wm831x_irqs
) + wm831x
->irq_base
;
525 set_irq_chip_data(cur_irq
, wm831x
);
526 set_irq_chip_and_handler(cur_irq
, &wm831x_irq_chip
,
528 set_irq_nested_thread(cur_irq
, 1);
530 /* ARM needs us to explicitly flag the IRQ as valid
531 * and will set them noprobe when we do so. */
533 set_irq_flags(cur_irq
, IRQF_VALID
);
535 set_irq_noprobe(cur_irq
);
539 ret
= request_threaded_irq(irq
, NULL
, wm831x_irq_thread
,
540 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
543 dev_err(wm831x
->dev
, "Failed to request IRQ %d: %d\n",
548 /* Enable top level interrupts, we mask at secondary level */
549 wm831x_reg_write(wm831x
, WM831X_SYSTEM_INTERRUPTS_MASK
, 0);
554 void wm831x_irq_exit(struct wm831x
*wm831x
)
557 free_irq(wm831x
->irq
, wm831x
);