2 * wm8994-irq.c -- Interrupt controller support for Wolfson WM8994
4 * Copyright 2010 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/wm8994/core.h>
23 #include <linux/mfd/wm8994/registers.h>
25 #include <linux/delay.h>
27 struct wm8994_irq_data
{
32 static struct wm8994_irq_data wm8994_irqs
[] = {
33 [WM8994_IRQ_TEMP_SHUT
] = {
35 .mask
= WM8994_TEMP_SHUT_EINT
,
37 [WM8994_IRQ_MIC1_DET
] = {
39 .mask
= WM8994_MIC1_DET_EINT
,
41 [WM8994_IRQ_MIC1_SHRT
] = {
43 .mask
= WM8994_MIC1_SHRT_EINT
,
45 [WM8994_IRQ_MIC2_DET
] = {
47 .mask
= WM8994_MIC2_DET_EINT
,
49 [WM8994_IRQ_MIC2_SHRT
] = {
51 .mask
= WM8994_MIC2_SHRT_EINT
,
53 [WM8994_IRQ_FLL1_LOCK
] = {
55 .mask
= WM8994_FLL1_LOCK_EINT
,
57 [WM8994_IRQ_FLL2_LOCK
] = {
59 .mask
= WM8994_FLL2_LOCK_EINT
,
61 [WM8994_IRQ_SRC1_LOCK
] = {
63 .mask
= WM8994_SRC1_LOCK_EINT
,
65 [WM8994_IRQ_SRC2_LOCK
] = {
67 .mask
= WM8994_SRC2_LOCK_EINT
,
69 [WM8994_IRQ_AIF1DRC1_SIG_DET
] = {
71 .mask
= WM8994_AIF1DRC1_SIG_DET
,
73 [WM8994_IRQ_AIF1DRC2_SIG_DET
] = {
75 .mask
= WM8994_AIF1DRC2_SIG_DET_EINT
,
77 [WM8994_IRQ_AIF2DRC_SIG_DET
] = {
79 .mask
= WM8994_AIF2DRC_SIG_DET_EINT
,
81 [WM8994_IRQ_FIFOS_ERR
] = {
83 .mask
= WM8994_FIFOS_ERR_EINT
,
85 [WM8994_IRQ_WSEQ_DONE
] = {
87 .mask
= WM8994_WSEQ_DONE_EINT
,
89 [WM8994_IRQ_DCS_DONE
] = {
91 .mask
= WM8994_DCS_DONE_EINT
,
93 [WM8994_IRQ_TEMP_WARN
] = {
95 .mask
= WM8994_TEMP_WARN_EINT
,
97 [WM8994_IRQ_GPIO(1)] = {
99 .mask
= WM8994_GP1_EINT
,
101 [WM8994_IRQ_GPIO(2)] = {
103 .mask
= WM8994_GP2_EINT
,
105 [WM8994_IRQ_GPIO(3)] = {
107 .mask
= WM8994_GP3_EINT
,
109 [WM8994_IRQ_GPIO(4)] = {
111 .mask
= WM8994_GP4_EINT
,
113 [WM8994_IRQ_GPIO(5)] = {
115 .mask
= WM8994_GP5_EINT
,
117 [WM8994_IRQ_GPIO(6)] = {
119 .mask
= WM8994_GP6_EINT
,
121 [WM8994_IRQ_GPIO(7)] = {
123 .mask
= WM8994_GP7_EINT
,
125 [WM8994_IRQ_GPIO(8)] = {
127 .mask
= WM8994_GP8_EINT
,
129 [WM8994_IRQ_GPIO(9)] = {
131 .mask
= WM8994_GP8_EINT
,
133 [WM8994_IRQ_GPIO(10)] = {
135 .mask
= WM8994_GP10_EINT
,
137 [WM8994_IRQ_GPIO(11)] = {
139 .mask
= WM8994_GP11_EINT
,
143 static inline int irq_data_to_status_reg(struct wm8994_irq_data
*irq_data
)
145 return WM8994_INTERRUPT_STATUS_1
- 1 + irq_data
->reg
;
148 static inline int irq_data_to_mask_reg(struct wm8994_irq_data
*irq_data
)
150 return WM8994_INTERRUPT_STATUS_1_MASK
- 1 + irq_data
->reg
;
153 static inline struct wm8994_irq_data
*irq_to_wm8994_irq(struct wm8994
*wm8994
,
156 return &wm8994_irqs
[irq
- wm8994
->irq_base
];
159 static void wm8994_irq_lock(unsigned int irq
)
161 struct wm8994
*wm8994
= get_irq_chip_data(irq
);
163 mutex_lock(&wm8994
->irq_lock
);
166 static void wm8994_irq_sync_unlock(unsigned int irq
)
168 struct wm8994
*wm8994
= get_irq_chip_data(irq
);
171 for (i
= 0; i
< ARRAY_SIZE(wm8994
->irq_masks_cur
); i
++) {
172 /* If there's been a change in the mask write it back
173 * to the hardware. */
174 if (wm8994
->irq_masks_cur
[i
] != wm8994
->irq_masks_cache
[i
]) {
175 wm8994
->irq_masks_cache
[i
] = wm8994
->irq_masks_cur
[i
];
176 wm8994_reg_write(wm8994
,
177 WM8994_INTERRUPT_STATUS_1_MASK
+ i
,
178 wm8994
->irq_masks_cur
[i
]);
182 mutex_unlock(&wm8994
->irq_lock
);
185 static void wm8994_irq_unmask(unsigned int irq
)
187 struct wm8994
*wm8994
= get_irq_chip_data(irq
);
188 struct wm8994_irq_data
*irq_data
= irq_to_wm8994_irq(wm8994
, irq
);
190 wm8994
->irq_masks_cur
[irq_data
->reg
- 1] &= ~irq_data
->mask
;
193 static void wm8994_irq_mask(unsigned int irq
)
195 struct wm8994
*wm8994
= get_irq_chip_data(irq
);
196 struct wm8994_irq_data
*irq_data
= irq_to_wm8994_irq(wm8994
, irq
);
198 wm8994
->irq_masks_cur
[irq_data
->reg
- 1] |= irq_data
->mask
;
201 static struct irq_chip wm8994_irq_chip
= {
203 .bus_lock
= wm8994_irq_lock
,
204 .bus_sync_unlock
= wm8994_irq_sync_unlock
,
205 .mask
= wm8994_irq_mask
,
206 .unmask
= wm8994_irq_unmask
,
209 /* The processing of the primary interrupt occurs in a thread so that
210 * we can interact with the device over I2C or SPI. */
211 static irqreturn_t
wm8994_irq_thread(int irq
, void *data
)
213 struct wm8994
*wm8994
= data
;
215 u16 status
[WM8994_NUM_IRQ_REGS
];
218 ret
= wm8994_bulk_read(wm8994
, WM8994_INTERRUPT_STATUS_1
,
219 WM8994_NUM_IRQ_REGS
, status
);
221 dev_err(wm8994
->dev
, "Failed to read interrupt status: %d\n",
227 for (i
= 0; i
< WM8994_NUM_IRQ_REGS
; i
++)
228 status
[i
] &= ~wm8994
->irq_masks_cur
[i
];
231 for (i
= 0; i
< ARRAY_SIZE(wm8994_irqs
); i
++) {
232 if (status
[wm8994_irqs
[i
].reg
- 1] & wm8994_irqs
[i
].mask
)
233 handle_nested_irq(wm8994
->irq_base
+ i
);
236 /* Ack any unmasked IRQs */
237 for (i
= 0; i
< ARRAY_SIZE(status
); i
++) {
239 wm8994_reg_write(wm8994
, WM8994_INTERRUPT_STATUS_1
+ i
,
246 int wm8994_irq_init(struct wm8994
*wm8994
)
250 mutex_init(&wm8994
->irq_lock
);
252 /* Mask the individual interrupt sources */
253 for (i
= 0; i
< ARRAY_SIZE(wm8994
->irq_masks_cur
); i
++) {
254 wm8994
->irq_masks_cur
[i
] = 0xffff;
255 wm8994
->irq_masks_cache
[i
] = 0xffff;
256 wm8994_reg_write(wm8994
, WM8994_INTERRUPT_STATUS_1_MASK
+ i
,
261 dev_warn(wm8994
->dev
,
262 "No interrupt specified, no interrupts\n");
263 wm8994
->irq_base
= 0;
267 if (!wm8994
->irq_base
) {
269 "No interrupt base specified, no interrupts\n");
273 /* Register them with genirq */
274 for (cur_irq
= wm8994
->irq_base
;
275 cur_irq
< ARRAY_SIZE(wm8994_irqs
) + wm8994
->irq_base
;
277 set_irq_chip_data(cur_irq
, wm8994
);
278 set_irq_chip_and_handler(cur_irq
, &wm8994_irq_chip
,
280 set_irq_nested_thread(cur_irq
, 1);
282 /* ARM needs us to explicitly flag the IRQ as valid
283 * and will set them noprobe when we do so. */
285 set_irq_flags(cur_irq
, IRQF_VALID
);
287 set_irq_noprobe(cur_irq
);
291 ret
= request_threaded_irq(wm8994
->irq
, NULL
, wm8994_irq_thread
,
292 IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
,
295 dev_err(wm8994
->dev
, "Failed to request IRQ %d: %d\n",
300 /* Enable top level interrupt if it was masked */
301 wm8994_reg_write(wm8994
, WM8994_INTERRUPT_CONTROL
, 0);
306 void wm8994_irq_exit(struct wm8994
*wm8994
)
309 free_irq(wm8994
->irq
, wm8994
);