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>
21 #include <linux/irqdomain.h>
23 #include <linux/mfd/wm831x/core.h>
24 #include <linux/mfd/wm831x/pdata.h>
25 #include <linux/mfd/wm831x/gpio.h>
26 #include <linux/mfd/wm831x/irq.h>
28 #include <linux/delay.h>
30 struct wm831x_irq_data
{
36 static struct wm831x_irq_data wm831x_irqs
[] = {
37 [WM831X_IRQ_TEMP_THW
] = {
38 .primary
= WM831X_TEMP_INT
,
40 .mask
= WM831X_TEMP_THW_EINT
,
42 [WM831X_IRQ_GPIO_1
] = {
43 .primary
= WM831X_GP_INT
,
45 .mask
= WM831X_GP1_EINT
,
47 [WM831X_IRQ_GPIO_2
] = {
48 .primary
= WM831X_GP_INT
,
50 .mask
= WM831X_GP2_EINT
,
52 [WM831X_IRQ_GPIO_3
] = {
53 .primary
= WM831X_GP_INT
,
55 .mask
= WM831X_GP3_EINT
,
57 [WM831X_IRQ_GPIO_4
] = {
58 .primary
= WM831X_GP_INT
,
60 .mask
= WM831X_GP4_EINT
,
62 [WM831X_IRQ_GPIO_5
] = {
63 .primary
= WM831X_GP_INT
,
65 .mask
= WM831X_GP5_EINT
,
67 [WM831X_IRQ_GPIO_6
] = {
68 .primary
= WM831X_GP_INT
,
70 .mask
= WM831X_GP6_EINT
,
72 [WM831X_IRQ_GPIO_7
] = {
73 .primary
= WM831X_GP_INT
,
75 .mask
= WM831X_GP7_EINT
,
77 [WM831X_IRQ_GPIO_8
] = {
78 .primary
= WM831X_GP_INT
,
80 .mask
= WM831X_GP8_EINT
,
82 [WM831X_IRQ_GPIO_9
] = {
83 .primary
= WM831X_GP_INT
,
85 .mask
= WM831X_GP9_EINT
,
87 [WM831X_IRQ_GPIO_10
] = {
88 .primary
= WM831X_GP_INT
,
90 .mask
= WM831X_GP10_EINT
,
92 [WM831X_IRQ_GPIO_11
] = {
93 .primary
= WM831X_GP_INT
,
95 .mask
= WM831X_GP11_EINT
,
97 [WM831X_IRQ_GPIO_12
] = {
98 .primary
= WM831X_GP_INT
,
100 .mask
= WM831X_GP12_EINT
,
102 [WM831X_IRQ_GPIO_13
] = {
103 .primary
= WM831X_GP_INT
,
105 .mask
= WM831X_GP13_EINT
,
107 [WM831X_IRQ_GPIO_14
] = {
108 .primary
= WM831X_GP_INT
,
110 .mask
= WM831X_GP14_EINT
,
112 [WM831X_IRQ_GPIO_15
] = {
113 .primary
= WM831X_GP_INT
,
115 .mask
= WM831X_GP15_EINT
,
117 [WM831X_IRQ_GPIO_16
] = {
118 .primary
= WM831X_GP_INT
,
120 .mask
= WM831X_GP16_EINT
,
123 .primary
= WM831X_ON_PIN_INT
,
125 .mask
= WM831X_ON_PIN_EINT
,
127 [WM831X_IRQ_PPM_SYSLO
] = {
128 .primary
= WM831X_PPM_INT
,
130 .mask
= WM831X_PPM_SYSLO_EINT
,
132 [WM831X_IRQ_PPM_PWR_SRC
] = {
133 .primary
= WM831X_PPM_INT
,
135 .mask
= WM831X_PPM_PWR_SRC_EINT
,
137 [WM831X_IRQ_PPM_USB_CURR
] = {
138 .primary
= WM831X_PPM_INT
,
140 .mask
= WM831X_PPM_USB_CURR_EINT
,
142 [WM831X_IRQ_WDOG_TO
] = {
143 .primary
= WM831X_WDOG_INT
,
145 .mask
= WM831X_WDOG_TO_EINT
,
147 [WM831X_IRQ_RTC_PER
] = {
148 .primary
= WM831X_RTC_INT
,
150 .mask
= WM831X_RTC_PER_EINT
,
152 [WM831X_IRQ_RTC_ALM
] = {
153 .primary
= WM831X_RTC_INT
,
155 .mask
= WM831X_RTC_ALM_EINT
,
157 [WM831X_IRQ_CHG_BATT_HOT
] = {
158 .primary
= WM831X_CHG_INT
,
160 .mask
= WM831X_CHG_BATT_HOT_EINT
,
162 [WM831X_IRQ_CHG_BATT_COLD
] = {
163 .primary
= WM831X_CHG_INT
,
165 .mask
= WM831X_CHG_BATT_COLD_EINT
,
167 [WM831X_IRQ_CHG_BATT_FAIL
] = {
168 .primary
= WM831X_CHG_INT
,
170 .mask
= WM831X_CHG_BATT_FAIL_EINT
,
172 [WM831X_IRQ_CHG_OV
] = {
173 .primary
= WM831X_CHG_INT
,
175 .mask
= WM831X_CHG_OV_EINT
,
177 [WM831X_IRQ_CHG_END
] = {
178 .primary
= WM831X_CHG_INT
,
180 .mask
= WM831X_CHG_END_EINT
,
182 [WM831X_IRQ_CHG_TO
] = {
183 .primary
= WM831X_CHG_INT
,
185 .mask
= WM831X_CHG_TO_EINT
,
187 [WM831X_IRQ_CHG_MODE
] = {
188 .primary
= WM831X_CHG_INT
,
190 .mask
= WM831X_CHG_MODE_EINT
,
192 [WM831X_IRQ_CHG_START
] = {
193 .primary
= WM831X_CHG_INT
,
195 .mask
= WM831X_CHG_START_EINT
,
197 [WM831X_IRQ_TCHDATA
] = {
198 .primary
= WM831X_TCHDATA_INT
,
200 .mask
= WM831X_TCHDATA_EINT
,
202 [WM831X_IRQ_TCHPD
] = {
203 .primary
= WM831X_TCHPD_INT
,
205 .mask
= WM831X_TCHPD_EINT
,
207 [WM831X_IRQ_AUXADC_DATA
] = {
208 .primary
= WM831X_AUXADC_INT
,
210 .mask
= WM831X_AUXADC_DATA_EINT
,
212 [WM831X_IRQ_AUXADC_DCOMP1
] = {
213 .primary
= WM831X_AUXADC_INT
,
215 .mask
= WM831X_AUXADC_DCOMP1_EINT
,
217 [WM831X_IRQ_AUXADC_DCOMP2
] = {
218 .primary
= WM831X_AUXADC_INT
,
220 .mask
= WM831X_AUXADC_DCOMP2_EINT
,
222 [WM831X_IRQ_AUXADC_DCOMP3
] = {
223 .primary
= WM831X_AUXADC_INT
,
225 .mask
= WM831X_AUXADC_DCOMP3_EINT
,
227 [WM831X_IRQ_AUXADC_DCOMP4
] = {
228 .primary
= WM831X_AUXADC_INT
,
230 .mask
= WM831X_AUXADC_DCOMP4_EINT
,
233 .primary
= WM831X_CS_INT
,
235 .mask
= WM831X_CS1_EINT
,
238 .primary
= WM831X_CS_INT
,
240 .mask
= WM831X_CS2_EINT
,
242 [WM831X_IRQ_HC_DC1
] = {
243 .primary
= WM831X_HC_INT
,
245 .mask
= WM831X_HC_DC1_EINT
,
247 [WM831X_IRQ_HC_DC2
] = {
248 .primary
= WM831X_HC_INT
,
250 .mask
= WM831X_HC_DC2_EINT
,
252 [WM831X_IRQ_UV_LDO1
] = {
253 .primary
= WM831X_UV_INT
,
255 .mask
= WM831X_UV_LDO1_EINT
,
257 [WM831X_IRQ_UV_LDO2
] = {
258 .primary
= WM831X_UV_INT
,
260 .mask
= WM831X_UV_LDO2_EINT
,
262 [WM831X_IRQ_UV_LDO3
] = {
263 .primary
= WM831X_UV_INT
,
265 .mask
= WM831X_UV_LDO3_EINT
,
267 [WM831X_IRQ_UV_LDO4
] = {
268 .primary
= WM831X_UV_INT
,
270 .mask
= WM831X_UV_LDO4_EINT
,
272 [WM831X_IRQ_UV_LDO5
] = {
273 .primary
= WM831X_UV_INT
,
275 .mask
= WM831X_UV_LDO5_EINT
,
277 [WM831X_IRQ_UV_LDO6
] = {
278 .primary
= WM831X_UV_INT
,
280 .mask
= WM831X_UV_LDO6_EINT
,
282 [WM831X_IRQ_UV_LDO7
] = {
283 .primary
= WM831X_UV_INT
,
285 .mask
= WM831X_UV_LDO7_EINT
,
287 [WM831X_IRQ_UV_LDO8
] = {
288 .primary
= WM831X_UV_INT
,
290 .mask
= WM831X_UV_LDO8_EINT
,
292 [WM831X_IRQ_UV_LDO9
] = {
293 .primary
= WM831X_UV_INT
,
295 .mask
= WM831X_UV_LDO9_EINT
,
297 [WM831X_IRQ_UV_LDO10
] = {
298 .primary
= WM831X_UV_INT
,
300 .mask
= WM831X_UV_LDO10_EINT
,
302 [WM831X_IRQ_UV_DC1
] = {
303 .primary
= WM831X_UV_INT
,
305 .mask
= WM831X_UV_DC1_EINT
,
307 [WM831X_IRQ_UV_DC2
] = {
308 .primary
= WM831X_UV_INT
,
310 .mask
= WM831X_UV_DC2_EINT
,
312 [WM831X_IRQ_UV_DC3
] = {
313 .primary
= WM831X_UV_INT
,
315 .mask
= WM831X_UV_DC3_EINT
,
317 [WM831X_IRQ_UV_DC4
] = {
318 .primary
= WM831X_UV_INT
,
320 .mask
= WM831X_UV_DC4_EINT
,
324 static inline int irq_data_to_status_reg(struct wm831x_irq_data
*irq_data
)
326 return WM831X_INTERRUPT_STATUS_1
- 1 + irq_data
->reg
;
329 static inline struct wm831x_irq_data
*irq_to_wm831x_irq(struct wm831x
*wm831x
,
332 return &wm831x_irqs
[irq
];
335 static void wm831x_irq_lock(struct irq_data
*data
)
337 struct wm831x
*wm831x
= irq_data_get_irq_chip_data(data
);
339 mutex_lock(&wm831x
->irq_lock
);
342 static void wm831x_irq_sync_unlock(struct irq_data
*data
)
344 struct wm831x
*wm831x
= irq_data_get_irq_chip_data(data
);
347 for (i
= 0; i
< ARRAY_SIZE(wm831x
->gpio_update
); i
++) {
348 if (wm831x
->gpio_update
[i
]) {
349 wm831x_set_bits(wm831x
, WM831X_GPIO1_CONTROL
+ i
,
350 WM831X_GPN_INT_MODE
| WM831X_GPN_POL
,
351 wm831x
->gpio_update
[i
]);
352 wm831x
->gpio_update
[i
] = 0;
356 for (i
= 0; i
< ARRAY_SIZE(wm831x
->irq_masks_cur
); i
++) {
357 /* If there's been a change in the mask write it back
358 * to the hardware. */
359 if (wm831x
->irq_masks_cur
[i
] != wm831x
->irq_masks_cache
[i
]) {
360 dev_dbg(wm831x
->dev
, "IRQ mask sync: %x = %x\n",
361 WM831X_INTERRUPT_STATUS_1_MASK
+ i
,
362 wm831x
->irq_masks_cur
[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_enable(struct irq_data
*data
)
376 struct wm831x
*wm831x
= irq_data_get_irq_chip_data(data
);
377 struct wm831x_irq_data
*irq_data
= irq_to_wm831x_irq(wm831x
,
380 wm831x
->irq_masks_cur
[irq_data
->reg
- 1] &= ~irq_data
->mask
;
383 static void wm831x_irq_disable(struct irq_data
*data
)
385 struct wm831x
*wm831x
= irq_data_get_irq_chip_data(data
);
386 struct wm831x_irq_data
*irq_data
= irq_to_wm831x_irq(wm831x
,
389 wm831x
->irq_masks_cur
[irq_data
->reg
- 1] |= irq_data
->mask
;
392 static int wm831x_irq_set_type(struct irq_data
*data
, unsigned int type
)
394 struct wm831x
*wm831x
= irq_data_get_irq_chip_data(data
);
399 if (irq
< WM831X_IRQ_GPIO_1
|| irq
> WM831X_IRQ_GPIO_11
) {
400 /* Ignore internal-only IRQs */
401 if (irq
>= 0 && irq
< WM831X_NUM_IRQS
)
407 /* Rebase the IRQ into the GPIO range so we've got a sensible array
410 irq
-= WM831X_IRQ_GPIO_1
;
412 /* We set the high bit to flag that we need an update; don't
413 * do the update here as we can be called with the bus lock
416 wm831x
->gpio_level_low
[irq
] = false;
417 wm831x
->gpio_level_high
[irq
] = false;
419 case IRQ_TYPE_EDGE_BOTH
:
420 wm831x
->gpio_update
[irq
] = 0x10000 | WM831X_GPN_INT_MODE
;
422 case IRQ_TYPE_EDGE_RISING
:
423 wm831x
->gpio_update
[irq
] = 0x10000 | WM831X_GPN_POL
;
425 case IRQ_TYPE_EDGE_FALLING
:
426 wm831x
->gpio_update
[irq
] = 0x10000;
428 case IRQ_TYPE_LEVEL_HIGH
:
429 wm831x
->gpio_update
[irq
] = 0x10000 | WM831X_GPN_POL
;
430 wm831x
->gpio_level_high
[irq
] = true;
432 case IRQ_TYPE_LEVEL_LOW
:
433 wm831x
->gpio_update
[irq
] = 0x10000;
434 wm831x
->gpio_level_low
[irq
] = true;
443 static struct irq_chip wm831x_irq_chip
= {
445 .irq_bus_lock
= wm831x_irq_lock
,
446 .irq_bus_sync_unlock
= wm831x_irq_sync_unlock
,
447 .irq_disable
= wm831x_irq_disable
,
448 .irq_enable
= wm831x_irq_enable
,
449 .irq_set_type
= wm831x_irq_set_type
,
452 /* The processing of the primary interrupt occurs in a thread so that
453 * we can interact with the device over I2C or SPI. */
454 static irqreturn_t
wm831x_irq_thread(int irq
, void *data
)
456 struct wm831x
*wm831x
= data
;
458 int primary
, status_addr
, ret
;
459 int status_regs
[WM831X_NUM_IRQ_REGS
] = { 0 };
460 int read
[WM831X_NUM_IRQ_REGS
] = { 0 };
463 primary
= wm831x_reg_read(wm831x
, WM831X_SYSTEM_INTERRUPTS
);
465 dev_err(wm831x
->dev
, "Failed to read system interrupt: %d\n",
470 /* The touch interrupts are visible in the primary register as
471 * an optimisation; open code this to avoid complicating the
472 * main handling loop and so we can also skip iterating the
475 if (primary
& WM831X_TCHPD_INT
)
476 handle_nested_irq(irq_find_mapping(wm831x
->irq_domain
,
478 if (primary
& WM831X_TCHDATA_INT
)
479 handle_nested_irq(irq_find_mapping(wm831x
->irq_domain
,
480 WM831X_IRQ_TCHDATA
));
481 primary
&= ~(WM831X_TCHDATA_EINT
| WM831X_TCHPD_EINT
);
483 for (i
= 0; i
< ARRAY_SIZE(wm831x_irqs
); i
++) {
484 int offset
= wm831x_irqs
[i
].reg
- 1;
486 if (!(primary
& wm831x_irqs
[i
].primary
))
489 status
= &status_regs
[offset
];
491 /* Hopefully there should only be one register to read
492 * each time otherwise we ought to do a block read. */
494 status_addr
= irq_data_to_status_reg(&wm831x_irqs
[i
]);
496 *status
= wm831x_reg_read(wm831x
, status_addr
);
499 "Failed to read IRQ status: %d\n",
506 /* Ignore any bits that we don't think are masked */
507 *status
&= ~wm831x
->irq_masks_cur
[offset
];
509 /* Acknowledge now so we don't miss
510 * notifications while we handle.
512 wm831x_reg_write(wm831x
, status_addr
, *status
);
515 if (*status
& wm831x_irqs
[i
].mask
)
516 handle_nested_irq(irq_find_mapping(wm831x
->irq_domain
,
519 /* Simulate an edge triggered IRQ by polling the input
520 * status. This is sucky but improves interoperability.
522 if (primary
== WM831X_GP_INT
&&
523 wm831x
->gpio_level_high
[i
- WM831X_IRQ_GPIO_1
]) {
524 ret
= wm831x_reg_read(wm831x
, WM831X_GPIO_LEVEL
);
525 while (ret
& 1 << (i
- WM831X_IRQ_GPIO_1
)) {
526 handle_nested_irq(irq_find_mapping(wm831x
->irq_domain
,
528 ret
= wm831x_reg_read(wm831x
,
533 if (primary
== WM831X_GP_INT
&&
534 wm831x
->gpio_level_low
[i
- WM831X_IRQ_GPIO_1
]) {
535 ret
= wm831x_reg_read(wm831x
, WM831X_GPIO_LEVEL
);
536 while (!(ret
& 1 << (i
- WM831X_IRQ_GPIO_1
))) {
537 handle_nested_irq(irq_find_mapping(wm831x
->irq_domain
,
539 ret
= wm831x_reg_read(wm831x
,
549 static int wm831x_irq_map(struct irq_domain
*h
, unsigned int virq
,
552 irq_set_chip_data(virq
, h
->host_data
);
553 irq_set_chip_and_handler(virq
, &wm831x_irq_chip
, handle_edge_irq
);
554 irq_set_nested_thread(virq
, 1);
556 /* ARM needs us to explicitly flag the IRQ as valid
557 * and will set them noprobe when we do so. */
559 set_irq_flags(virq
, IRQF_VALID
);
561 irq_set_noprobe(virq
);
567 static struct irq_domain_ops wm831x_irq_domain_ops
= {
568 .map
= wm831x_irq_map
,
569 .xlate
= irq_domain_xlate_twocell
,
572 int wm831x_irq_init(struct wm831x
*wm831x
, int irq
)
574 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
575 struct irq_domain
*domain
;
576 int i
, ret
, irq_base
;
578 mutex_init(&wm831x
->irq_lock
);
580 /* Mask the individual interrupt sources */
581 for (i
= 0; i
< ARRAY_SIZE(wm831x
->irq_masks_cur
); i
++) {
582 wm831x
->irq_masks_cur
[i
] = 0xffff;
583 wm831x
->irq_masks_cache
[i
] = 0xffff;
584 wm831x_reg_write(wm831x
, WM831X_INTERRUPT_STATUS_1_MASK
+ i
,
588 /* Try to dynamically allocate IRQs if no base is specified */
589 if (pdata
&& pdata
->irq_base
) {
590 irq_base
= irq_alloc_descs(pdata
->irq_base
, 0,
593 dev_warn(wm831x
->dev
, "Failed to allocate IRQs: %d\n",
602 domain
= irq_domain_add_legacy(wm831x
->dev
->of_node
,
603 ARRAY_SIZE(wm831x_irqs
),
605 &wm831x_irq_domain_ops
,
608 domain
= irq_domain_add_linear(wm831x
->dev
->of_node
,
609 ARRAY_SIZE(wm831x_irqs
),
610 &wm831x_irq_domain_ops
,
614 dev_warn(wm831x
->dev
, "Failed to allocate IRQ domain\n");
618 if (pdata
&& pdata
->irq_cmos
)
623 wm831x_set_bits(wm831x
, WM831X_IRQ_CONFIG
,
627 wm831x
->irq_domain
= domain
;
630 /* Try to flag /IRQ as a wake source; there are a number of
631 * unconditional wake sources in the PMIC so this isn't
632 * conditional but we don't actually care *too* much if it
635 ret
= enable_irq_wake(irq
);
637 dev_warn(wm831x
->dev
,
638 "Can't enable IRQ as wake source: %d\n",
642 ret
= request_threaded_irq(irq
, NULL
, wm831x_irq_thread
,
643 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
646 dev_err(wm831x
->dev
, "Failed to request IRQ %d: %d\n",
651 dev_warn(wm831x
->dev
,
652 "No interrupt specified - functionality limited\n");
655 /* Enable top level interrupts, we mask at secondary level */
656 wm831x_reg_write(wm831x
, WM831X_SYSTEM_INTERRUPTS_MASK
, 0);
661 void wm831x_irq_exit(struct wm831x
*wm831x
)
664 free_irq(wm831x
->irq
, wm831x
);