1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * wm831x-irq.c -- Interrupt controller support for Wolfson WM831x PMICs
5 * Copyright 2009 Wolfson Microelectronics PLC.
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/irq.h>
14 #include <linux/mfd/core.h>
15 #include <linux/interrupt.h>
16 #include <linux/irqdomain.h>
18 #include <linux/mfd/wm831x/core.h>
19 #include <linux/mfd/wm831x/pdata.h>
20 #include <linux/mfd/wm831x/gpio.h>
21 #include <linux/mfd/wm831x/irq.h>
23 #include <linux/delay.h>
25 struct wm831x_irq_data
{
31 static struct wm831x_irq_data wm831x_irqs
[] = {
32 [WM831X_IRQ_TEMP_THW
] = {
33 .primary
= WM831X_TEMP_INT
,
35 .mask
= WM831X_TEMP_THW_EINT
,
37 [WM831X_IRQ_GPIO_1
] = {
38 .primary
= WM831X_GP_INT
,
40 .mask
= WM831X_GP1_EINT
,
42 [WM831X_IRQ_GPIO_2
] = {
43 .primary
= WM831X_GP_INT
,
45 .mask
= WM831X_GP2_EINT
,
47 [WM831X_IRQ_GPIO_3
] = {
48 .primary
= WM831X_GP_INT
,
50 .mask
= WM831X_GP3_EINT
,
52 [WM831X_IRQ_GPIO_4
] = {
53 .primary
= WM831X_GP_INT
,
55 .mask
= WM831X_GP4_EINT
,
57 [WM831X_IRQ_GPIO_5
] = {
58 .primary
= WM831X_GP_INT
,
60 .mask
= WM831X_GP5_EINT
,
62 [WM831X_IRQ_GPIO_6
] = {
63 .primary
= WM831X_GP_INT
,
65 .mask
= WM831X_GP6_EINT
,
67 [WM831X_IRQ_GPIO_7
] = {
68 .primary
= WM831X_GP_INT
,
70 .mask
= WM831X_GP7_EINT
,
72 [WM831X_IRQ_GPIO_8
] = {
73 .primary
= WM831X_GP_INT
,
75 .mask
= WM831X_GP8_EINT
,
77 [WM831X_IRQ_GPIO_9
] = {
78 .primary
= WM831X_GP_INT
,
80 .mask
= WM831X_GP9_EINT
,
82 [WM831X_IRQ_GPIO_10
] = {
83 .primary
= WM831X_GP_INT
,
85 .mask
= WM831X_GP10_EINT
,
87 [WM831X_IRQ_GPIO_11
] = {
88 .primary
= WM831X_GP_INT
,
90 .mask
= WM831X_GP11_EINT
,
92 [WM831X_IRQ_GPIO_12
] = {
93 .primary
= WM831X_GP_INT
,
95 .mask
= WM831X_GP12_EINT
,
97 [WM831X_IRQ_GPIO_13
] = {
98 .primary
= WM831X_GP_INT
,
100 .mask
= WM831X_GP13_EINT
,
102 [WM831X_IRQ_GPIO_14
] = {
103 .primary
= WM831X_GP_INT
,
105 .mask
= WM831X_GP14_EINT
,
107 [WM831X_IRQ_GPIO_15
] = {
108 .primary
= WM831X_GP_INT
,
110 .mask
= WM831X_GP15_EINT
,
112 [WM831X_IRQ_GPIO_16
] = {
113 .primary
= WM831X_GP_INT
,
115 .mask
= WM831X_GP16_EINT
,
118 .primary
= WM831X_ON_PIN_INT
,
120 .mask
= WM831X_ON_PIN_EINT
,
122 [WM831X_IRQ_PPM_SYSLO
] = {
123 .primary
= WM831X_PPM_INT
,
125 .mask
= WM831X_PPM_SYSLO_EINT
,
127 [WM831X_IRQ_PPM_PWR_SRC
] = {
128 .primary
= WM831X_PPM_INT
,
130 .mask
= WM831X_PPM_PWR_SRC_EINT
,
132 [WM831X_IRQ_PPM_USB_CURR
] = {
133 .primary
= WM831X_PPM_INT
,
135 .mask
= WM831X_PPM_USB_CURR_EINT
,
137 [WM831X_IRQ_WDOG_TO
] = {
138 .primary
= WM831X_WDOG_INT
,
140 .mask
= WM831X_WDOG_TO_EINT
,
142 [WM831X_IRQ_RTC_PER
] = {
143 .primary
= WM831X_RTC_INT
,
145 .mask
= WM831X_RTC_PER_EINT
,
147 [WM831X_IRQ_RTC_ALM
] = {
148 .primary
= WM831X_RTC_INT
,
150 .mask
= WM831X_RTC_ALM_EINT
,
152 [WM831X_IRQ_CHG_BATT_HOT
] = {
153 .primary
= WM831X_CHG_INT
,
155 .mask
= WM831X_CHG_BATT_HOT_EINT
,
157 [WM831X_IRQ_CHG_BATT_COLD
] = {
158 .primary
= WM831X_CHG_INT
,
160 .mask
= WM831X_CHG_BATT_COLD_EINT
,
162 [WM831X_IRQ_CHG_BATT_FAIL
] = {
163 .primary
= WM831X_CHG_INT
,
165 .mask
= WM831X_CHG_BATT_FAIL_EINT
,
167 [WM831X_IRQ_CHG_OV
] = {
168 .primary
= WM831X_CHG_INT
,
170 .mask
= WM831X_CHG_OV_EINT
,
172 [WM831X_IRQ_CHG_END
] = {
173 .primary
= WM831X_CHG_INT
,
175 .mask
= WM831X_CHG_END_EINT
,
177 [WM831X_IRQ_CHG_TO
] = {
178 .primary
= WM831X_CHG_INT
,
180 .mask
= WM831X_CHG_TO_EINT
,
182 [WM831X_IRQ_CHG_MODE
] = {
183 .primary
= WM831X_CHG_INT
,
185 .mask
= WM831X_CHG_MODE_EINT
,
187 [WM831X_IRQ_CHG_START
] = {
188 .primary
= WM831X_CHG_INT
,
190 .mask
= WM831X_CHG_START_EINT
,
192 [WM831X_IRQ_TCHDATA
] = {
193 .primary
= WM831X_TCHDATA_INT
,
195 .mask
= WM831X_TCHDATA_EINT
,
197 [WM831X_IRQ_TCHPD
] = {
198 .primary
= WM831X_TCHPD_INT
,
200 .mask
= WM831X_TCHPD_EINT
,
202 [WM831X_IRQ_AUXADC_DATA
] = {
203 .primary
= WM831X_AUXADC_INT
,
205 .mask
= WM831X_AUXADC_DATA_EINT
,
207 [WM831X_IRQ_AUXADC_DCOMP1
] = {
208 .primary
= WM831X_AUXADC_INT
,
210 .mask
= WM831X_AUXADC_DCOMP1_EINT
,
212 [WM831X_IRQ_AUXADC_DCOMP2
] = {
213 .primary
= WM831X_AUXADC_INT
,
215 .mask
= WM831X_AUXADC_DCOMP2_EINT
,
217 [WM831X_IRQ_AUXADC_DCOMP3
] = {
218 .primary
= WM831X_AUXADC_INT
,
220 .mask
= WM831X_AUXADC_DCOMP3_EINT
,
222 [WM831X_IRQ_AUXADC_DCOMP4
] = {
223 .primary
= WM831X_AUXADC_INT
,
225 .mask
= WM831X_AUXADC_DCOMP4_EINT
,
228 .primary
= WM831X_CS_INT
,
230 .mask
= WM831X_CS1_EINT
,
233 .primary
= WM831X_CS_INT
,
235 .mask
= WM831X_CS2_EINT
,
237 [WM831X_IRQ_HC_DC1
] = {
238 .primary
= WM831X_HC_INT
,
240 .mask
= WM831X_HC_DC1_EINT
,
242 [WM831X_IRQ_HC_DC2
] = {
243 .primary
= WM831X_HC_INT
,
245 .mask
= WM831X_HC_DC2_EINT
,
247 [WM831X_IRQ_UV_LDO1
] = {
248 .primary
= WM831X_UV_INT
,
250 .mask
= WM831X_UV_LDO1_EINT
,
252 [WM831X_IRQ_UV_LDO2
] = {
253 .primary
= WM831X_UV_INT
,
255 .mask
= WM831X_UV_LDO2_EINT
,
257 [WM831X_IRQ_UV_LDO3
] = {
258 .primary
= WM831X_UV_INT
,
260 .mask
= WM831X_UV_LDO3_EINT
,
262 [WM831X_IRQ_UV_LDO4
] = {
263 .primary
= WM831X_UV_INT
,
265 .mask
= WM831X_UV_LDO4_EINT
,
267 [WM831X_IRQ_UV_LDO5
] = {
268 .primary
= WM831X_UV_INT
,
270 .mask
= WM831X_UV_LDO5_EINT
,
272 [WM831X_IRQ_UV_LDO6
] = {
273 .primary
= WM831X_UV_INT
,
275 .mask
= WM831X_UV_LDO6_EINT
,
277 [WM831X_IRQ_UV_LDO7
] = {
278 .primary
= WM831X_UV_INT
,
280 .mask
= WM831X_UV_LDO7_EINT
,
282 [WM831X_IRQ_UV_LDO8
] = {
283 .primary
= WM831X_UV_INT
,
285 .mask
= WM831X_UV_LDO8_EINT
,
287 [WM831X_IRQ_UV_LDO9
] = {
288 .primary
= WM831X_UV_INT
,
290 .mask
= WM831X_UV_LDO9_EINT
,
292 [WM831X_IRQ_UV_LDO10
] = {
293 .primary
= WM831X_UV_INT
,
295 .mask
= WM831X_UV_LDO10_EINT
,
297 [WM831X_IRQ_UV_DC1
] = {
298 .primary
= WM831X_UV_INT
,
300 .mask
= WM831X_UV_DC1_EINT
,
302 [WM831X_IRQ_UV_DC2
] = {
303 .primary
= WM831X_UV_INT
,
305 .mask
= WM831X_UV_DC2_EINT
,
307 [WM831X_IRQ_UV_DC3
] = {
308 .primary
= WM831X_UV_INT
,
310 .mask
= WM831X_UV_DC3_EINT
,
312 [WM831X_IRQ_UV_DC4
] = {
313 .primary
= WM831X_UV_INT
,
315 .mask
= WM831X_UV_DC4_EINT
,
319 static inline int irq_data_to_status_reg(struct wm831x_irq_data
*irq_data
)
321 return WM831X_INTERRUPT_STATUS_1
- 1 + irq_data
->reg
;
324 static inline struct wm831x_irq_data
*irq_to_wm831x_irq(struct wm831x
*wm831x
,
327 return &wm831x_irqs
[irq
];
330 static void wm831x_irq_lock(struct irq_data
*data
)
332 struct wm831x
*wm831x
= irq_data_get_irq_chip_data(data
);
334 mutex_lock(&wm831x
->irq_lock
);
337 static void wm831x_irq_sync_unlock(struct irq_data
*data
)
339 struct wm831x
*wm831x
= irq_data_get_irq_chip_data(data
);
342 for (i
= 0; i
< ARRAY_SIZE(wm831x
->gpio_update
); i
++) {
343 if (wm831x
->gpio_update
[i
]) {
344 wm831x_set_bits(wm831x
, WM831X_GPIO1_CONTROL
+ i
,
345 WM831X_GPN_INT_MODE
| WM831X_GPN_POL
,
346 wm831x
->gpio_update
[i
]);
347 wm831x
->gpio_update
[i
] = 0;
351 for (i
= 0; i
< ARRAY_SIZE(wm831x
->irq_masks_cur
); i
++) {
352 /* If there's been a change in the mask write it back
353 * to the hardware. */
354 if (wm831x
->irq_masks_cur
[i
] != wm831x
->irq_masks_cache
[i
]) {
355 dev_dbg(wm831x
->dev
, "IRQ mask sync: %x = %x\n",
356 WM831X_INTERRUPT_STATUS_1_MASK
+ i
,
357 wm831x
->irq_masks_cur
[i
]);
359 wm831x
->irq_masks_cache
[i
] = wm831x
->irq_masks_cur
[i
];
360 wm831x_reg_write(wm831x
,
361 WM831X_INTERRUPT_STATUS_1_MASK
+ i
,
362 wm831x
->irq_masks_cur
[i
]);
366 mutex_unlock(&wm831x
->irq_lock
);
369 static void wm831x_irq_enable(struct irq_data
*data
)
371 struct wm831x
*wm831x
= irq_data_get_irq_chip_data(data
);
372 struct wm831x_irq_data
*irq_data
= irq_to_wm831x_irq(wm831x
,
375 wm831x
->irq_masks_cur
[irq_data
->reg
- 1] &= ~irq_data
->mask
;
378 static void wm831x_irq_disable(struct irq_data
*data
)
380 struct wm831x
*wm831x
= irq_data_get_irq_chip_data(data
);
381 struct wm831x_irq_data
*irq_data
= irq_to_wm831x_irq(wm831x
,
384 wm831x
->irq_masks_cur
[irq_data
->reg
- 1] |= irq_data
->mask
;
387 static int wm831x_irq_set_type(struct irq_data
*data
, unsigned int type
)
389 struct wm831x
*wm831x
= irq_data_get_irq_chip_data(data
);
394 if (irq
< WM831X_IRQ_GPIO_1
|| irq
> WM831X_IRQ_GPIO_11
) {
395 /* Ignore internal-only IRQs */
396 if (irq
>= 0 && irq
< WM831X_NUM_IRQS
)
402 /* Rebase the IRQ into the GPIO range so we've got a sensible array
405 irq
-= WM831X_IRQ_GPIO_1
;
407 /* We set the high bit to flag that we need an update; don't
408 * do the update here as we can be called with the bus lock
411 wm831x
->gpio_level_low
[irq
] = false;
412 wm831x
->gpio_level_high
[irq
] = false;
414 case IRQ_TYPE_EDGE_BOTH
:
415 wm831x
->gpio_update
[irq
] = 0x10000 | WM831X_GPN_INT_MODE
;
417 case IRQ_TYPE_EDGE_RISING
:
418 wm831x
->gpio_update
[irq
] = 0x10000 | WM831X_GPN_POL
;
420 case IRQ_TYPE_EDGE_FALLING
:
421 wm831x
->gpio_update
[irq
] = 0x10000;
423 case IRQ_TYPE_LEVEL_HIGH
:
424 wm831x
->gpio_update
[irq
] = 0x10000 | WM831X_GPN_POL
;
425 wm831x
->gpio_level_high
[irq
] = true;
427 case IRQ_TYPE_LEVEL_LOW
:
428 wm831x
->gpio_update
[irq
] = 0x10000;
429 wm831x
->gpio_level_low
[irq
] = true;
438 static struct irq_chip wm831x_irq_chip
= {
440 .irq_bus_lock
= wm831x_irq_lock
,
441 .irq_bus_sync_unlock
= wm831x_irq_sync_unlock
,
442 .irq_disable
= wm831x_irq_disable
,
443 .irq_enable
= wm831x_irq_enable
,
444 .irq_set_type
= wm831x_irq_set_type
,
447 /* The processing of the primary interrupt occurs in a thread so that
448 * we can interact with the device over I2C or SPI. */
449 static irqreturn_t
wm831x_irq_thread(int irq
, void *data
)
451 struct wm831x
*wm831x
= data
;
453 int primary
, status_addr
, ret
;
454 int status_regs
[WM831X_NUM_IRQ_REGS
] = { 0 };
455 int read
[WM831X_NUM_IRQ_REGS
] = { 0 };
458 primary
= wm831x_reg_read(wm831x
, WM831X_SYSTEM_INTERRUPTS
);
460 dev_err(wm831x
->dev
, "Failed to read system interrupt: %d\n",
465 /* The touch interrupts are visible in the primary register as
466 * an optimisation; open code this to avoid complicating the
467 * main handling loop and so we can also skip iterating the
470 if (primary
& WM831X_TCHPD_INT
)
471 handle_nested_irq(irq_find_mapping(wm831x
->irq_domain
,
473 if (primary
& WM831X_TCHDATA_INT
)
474 handle_nested_irq(irq_find_mapping(wm831x
->irq_domain
,
475 WM831X_IRQ_TCHDATA
));
476 primary
&= ~(WM831X_TCHDATA_EINT
| WM831X_TCHPD_EINT
);
478 for (i
= 0; i
< ARRAY_SIZE(wm831x_irqs
); i
++) {
479 int offset
= wm831x_irqs
[i
].reg
- 1;
481 if (!(primary
& wm831x_irqs
[i
].primary
))
484 status
= &status_regs
[offset
];
486 /* Hopefully there should only be one register to read
487 * each time otherwise we ought to do a block read. */
489 status_addr
= irq_data_to_status_reg(&wm831x_irqs
[i
]);
491 *status
= wm831x_reg_read(wm831x
, status_addr
);
494 "Failed to read IRQ status: %d\n",
501 /* Ignore any bits that we don't think are masked */
502 *status
&= ~wm831x
->irq_masks_cur
[offset
];
504 /* Acknowledge now so we don't miss
505 * notifications while we handle.
507 wm831x_reg_write(wm831x
, status_addr
, *status
);
510 if (*status
& wm831x_irqs
[i
].mask
)
511 handle_nested_irq(irq_find_mapping(wm831x
->irq_domain
,
514 /* Simulate an edge triggered IRQ by polling the input
515 * status. This is sucky but improves interoperability.
517 if (primary
== WM831X_GP_INT
&&
518 wm831x
->gpio_level_high
[i
- WM831X_IRQ_GPIO_1
]) {
519 ret
= wm831x_reg_read(wm831x
, WM831X_GPIO_LEVEL
);
520 while (ret
& 1 << (i
- WM831X_IRQ_GPIO_1
)) {
521 handle_nested_irq(irq_find_mapping(wm831x
->irq_domain
,
523 ret
= wm831x_reg_read(wm831x
,
528 if (primary
== WM831X_GP_INT
&&
529 wm831x
->gpio_level_low
[i
- WM831X_IRQ_GPIO_1
]) {
530 ret
= wm831x_reg_read(wm831x
, WM831X_GPIO_LEVEL
);
531 while (!(ret
& 1 << (i
- WM831X_IRQ_GPIO_1
))) {
532 handle_nested_irq(irq_find_mapping(wm831x
->irq_domain
,
534 ret
= wm831x_reg_read(wm831x
,
544 static int wm831x_irq_map(struct irq_domain
*h
, unsigned int virq
,
547 irq_set_chip_data(virq
, h
->host_data
);
548 irq_set_chip_and_handler(virq
, &wm831x_irq_chip
, handle_edge_irq
);
549 irq_set_nested_thread(virq
, 1);
550 irq_set_noprobe(virq
);
555 static const struct irq_domain_ops wm831x_irq_domain_ops
= {
556 .map
= wm831x_irq_map
,
557 .xlate
= irq_domain_xlate_twocell
,
560 int wm831x_irq_init(struct wm831x
*wm831x
, int irq
)
562 struct wm831x_pdata
*pdata
= &wm831x
->pdata
;
563 struct irq_domain
*domain
;
564 int i
, ret
, irq_base
;
566 mutex_init(&wm831x
->irq_lock
);
568 /* Mask the individual interrupt sources */
569 for (i
= 0; i
< ARRAY_SIZE(wm831x
->irq_masks_cur
); i
++) {
570 wm831x
->irq_masks_cur
[i
] = 0xffff;
571 wm831x
->irq_masks_cache
[i
] = 0xffff;
572 wm831x_reg_write(wm831x
, WM831X_INTERRUPT_STATUS_1_MASK
+ i
,
576 /* Try to dynamically allocate IRQs if no base is specified */
577 if (pdata
->irq_base
) {
578 irq_base
= irq_alloc_descs(pdata
->irq_base
, 0,
581 dev_warn(wm831x
->dev
, "Failed to allocate IRQs: %d\n",
590 domain
= irq_domain_add_legacy(wm831x
->dev
->of_node
,
591 ARRAY_SIZE(wm831x_irqs
),
593 &wm831x_irq_domain_ops
,
596 domain
= irq_domain_add_linear(wm831x
->dev
->of_node
,
597 ARRAY_SIZE(wm831x_irqs
),
598 &wm831x_irq_domain_ops
,
602 dev_warn(wm831x
->dev
, "Failed to allocate IRQ domain\n");
611 wm831x_set_bits(wm831x
, WM831X_IRQ_CONFIG
,
615 wm831x
->irq_domain
= domain
;
618 /* Try to flag /IRQ as a wake source; there are a number of
619 * unconditional wake sources in the PMIC so this isn't
620 * conditional but we don't actually care *too* much if it
623 ret
= enable_irq_wake(irq
);
625 dev_warn(wm831x
->dev
,
626 "Can't enable IRQ as wake source: %d\n",
630 ret
= request_threaded_irq(irq
, NULL
, wm831x_irq_thread
,
631 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
634 dev_err(wm831x
->dev
, "Failed to request IRQ %d: %d\n",
639 dev_warn(wm831x
->dev
,
640 "No interrupt specified - functionality limited\n");
643 /* Enable top level interrupts, we mask at secondary level */
644 wm831x_reg_write(wm831x
, WM831X_SYSTEM_INTERRUPTS_MASK
, 0);
649 void wm831x_irq_exit(struct wm831x
*wm831x
)
652 free_irq(wm831x
->irq
, wm831x
);