Merge branch 'topic/irq' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[deliverable/linux.git] / drivers / mfd / wm8994-irq.c
CommitLineData
c9fbf7e0
MB
1/*
2 * wm8994-irq.c -- Interrupt controller support for Wolfson WM8994
3 *
4 * Copyright 2010 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
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
22#include <linux/mfd/wm8994/core.h>
23#include <linux/mfd/wm8994/registers.h>
24
25#include <linux/delay.h>
26
27struct wm8994_irq_data {
28 int reg;
29 int mask;
30};
31
32static struct wm8994_irq_data wm8994_irqs[] = {
33 [WM8994_IRQ_TEMP_SHUT] = {
34 .reg = 2,
35 .mask = WM8994_TEMP_SHUT_EINT,
36 },
37 [WM8994_IRQ_MIC1_DET] = {
38 .reg = 2,
39 .mask = WM8994_MIC1_DET_EINT,
40 },
41 [WM8994_IRQ_MIC1_SHRT] = {
42 .reg = 2,
43 .mask = WM8994_MIC1_SHRT_EINT,
44 },
45 [WM8994_IRQ_MIC2_DET] = {
46 .reg = 2,
47 .mask = WM8994_MIC2_DET_EINT,
48 },
49 [WM8994_IRQ_MIC2_SHRT] = {
50 .reg = 2,
51 .mask = WM8994_MIC2_SHRT_EINT,
52 },
53 [WM8994_IRQ_FLL1_LOCK] = {
54 .reg = 2,
55 .mask = WM8994_FLL1_LOCK_EINT,
56 },
57 [WM8994_IRQ_FLL2_LOCK] = {
58 .reg = 2,
59 .mask = WM8994_FLL2_LOCK_EINT,
60 },
61 [WM8994_IRQ_SRC1_LOCK] = {
62 .reg = 2,
63 .mask = WM8994_SRC1_LOCK_EINT,
64 },
65 [WM8994_IRQ_SRC2_LOCK] = {
66 .reg = 2,
67 .mask = WM8994_SRC2_LOCK_EINT,
68 },
69 [WM8994_IRQ_AIF1DRC1_SIG_DET] = {
70 .reg = 2,
71 .mask = WM8994_AIF1DRC1_SIG_DET,
72 },
73 [WM8994_IRQ_AIF1DRC2_SIG_DET] = {
74 .reg = 2,
75 .mask = WM8994_AIF1DRC2_SIG_DET_EINT,
76 },
77 [WM8994_IRQ_AIF2DRC_SIG_DET] = {
78 .reg = 2,
79 .mask = WM8994_AIF2DRC_SIG_DET_EINT,
80 },
81 [WM8994_IRQ_FIFOS_ERR] = {
82 .reg = 2,
83 .mask = WM8994_FIFOS_ERR_EINT,
84 },
85 [WM8994_IRQ_WSEQ_DONE] = {
86 .reg = 2,
87 .mask = WM8994_WSEQ_DONE_EINT,
88 },
89 [WM8994_IRQ_DCS_DONE] = {
90 .reg = 2,
91 .mask = WM8994_DCS_DONE_EINT,
92 },
93 [WM8994_IRQ_TEMP_WARN] = {
94 .reg = 2,
95 .mask = WM8994_TEMP_WARN_EINT,
96 },
97 [WM8994_IRQ_GPIO(1)] = {
98 .reg = 1,
99 .mask = WM8994_GP1_EINT,
100 },
101 [WM8994_IRQ_GPIO(2)] = {
102 .reg = 1,
103 .mask = WM8994_GP2_EINT,
104 },
105 [WM8994_IRQ_GPIO(3)] = {
106 .reg = 1,
107 .mask = WM8994_GP3_EINT,
108 },
109 [WM8994_IRQ_GPIO(4)] = {
110 .reg = 1,
111 .mask = WM8994_GP4_EINT,
112 },
113 [WM8994_IRQ_GPIO(5)] = {
114 .reg = 1,
115 .mask = WM8994_GP5_EINT,
116 },
117 [WM8994_IRQ_GPIO(6)] = {
118 .reg = 1,
119 .mask = WM8994_GP6_EINT,
120 },
121 [WM8994_IRQ_GPIO(7)] = {
122 .reg = 1,
123 .mask = WM8994_GP7_EINT,
124 },
125 [WM8994_IRQ_GPIO(8)] = {
126 .reg = 1,
127 .mask = WM8994_GP8_EINT,
128 },
129 [WM8994_IRQ_GPIO(9)] = {
130 .reg = 1,
131 .mask = WM8994_GP8_EINT,
132 },
133 [WM8994_IRQ_GPIO(10)] = {
134 .reg = 1,
135 .mask = WM8994_GP10_EINT,
136 },
137 [WM8994_IRQ_GPIO(11)] = {
138 .reg = 1,
139 .mask = WM8994_GP11_EINT,
140 },
141};
142
c9fbf7e0
MB
143static inline struct wm8994_irq_data *irq_to_wm8994_irq(struct wm8994 *wm8994,
144 int irq)
145{
146 return &wm8994_irqs[irq - wm8994->irq_base];
147}
148
baa3f63b 149static void wm8994_irq_lock(struct irq_data *data)
c9fbf7e0 150{
25a947f8 151 struct wm8994 *wm8994 = irq_data_get_irq_chip_data(data);
c9fbf7e0
MB
152
153 mutex_lock(&wm8994->irq_lock);
154}
155
baa3f63b 156static void wm8994_irq_sync_unlock(struct irq_data *data)
c9fbf7e0 157{
25a947f8 158 struct wm8994 *wm8994 = irq_data_get_irq_chip_data(data);
c9fbf7e0
MB
159 int i;
160
161 for (i = 0; i < ARRAY_SIZE(wm8994->irq_masks_cur); i++) {
162 /* If there's been a change in the mask write it back
163 * to the hardware. */
164 if (wm8994->irq_masks_cur[i] != wm8994->irq_masks_cache[i]) {
165 wm8994->irq_masks_cache[i] = wm8994->irq_masks_cur[i];
166 wm8994_reg_write(wm8994,
167 WM8994_INTERRUPT_STATUS_1_MASK + i,
168 wm8994->irq_masks_cur[i]);
169 }
170 }
171
172 mutex_unlock(&wm8994->irq_lock);
173}
174
d664f200 175static void wm8994_irq_enable(struct irq_data *data)
c9fbf7e0 176{
25a947f8 177 struct wm8994 *wm8994 = irq_data_get_irq_chip_data(data);
baa3f63b
MB
178 struct wm8994_irq_data *irq_data = irq_to_wm8994_irq(wm8994,
179 data->irq);
c9fbf7e0
MB
180
181 wm8994->irq_masks_cur[irq_data->reg - 1] &= ~irq_data->mask;
182}
183
d664f200 184static void wm8994_irq_disable(struct irq_data *data)
c9fbf7e0 185{
25a947f8 186 struct wm8994 *wm8994 = irq_data_get_irq_chip_data(data);
baa3f63b
MB
187 struct wm8994_irq_data *irq_data = irq_to_wm8994_irq(wm8994,
188 data->irq);
c9fbf7e0
MB
189
190 wm8994->irq_masks_cur[irq_data->reg - 1] |= irq_data->mask;
191}
192
193static struct irq_chip wm8994_irq_chip = {
baa3f63b
MB
194 .name = "wm8994",
195 .irq_bus_lock = wm8994_irq_lock,
196 .irq_bus_sync_unlock = wm8994_irq_sync_unlock,
d664f200
MB
197 .irq_disable = wm8994_irq_disable,
198 .irq_enable = wm8994_irq_enable,
c9fbf7e0
MB
199};
200
201/* The processing of the primary interrupt occurs in a thread so that
202 * we can interact with the device over I2C or SPI. */
203static irqreturn_t wm8994_irq_thread(int irq, void *data)
204{
205 struct wm8994 *wm8994 = data;
206 unsigned int i;
207 u16 status[WM8994_NUM_IRQ_REGS];
208 int ret;
209
210 ret = wm8994_bulk_read(wm8994, WM8994_INTERRUPT_STATUS_1,
211 WM8994_NUM_IRQ_REGS, status);
212 if (ret < 0) {
213 dev_err(wm8994->dev, "Failed to read interrupt status: %d\n",
214 ret);
215 return IRQ_NONE;
216 }
217
316b6cc0
MB
218 /* Bit swap and apply masking */
219 for (i = 0; i < WM8994_NUM_IRQ_REGS; i++) {
220 status[i] = be16_to_cpu(status[i]);
c9fbf7e0 221 status[i] &= ~wm8994->irq_masks_cur[i];
316b6cc0 222 }
c9fbf7e0 223
c9fbf7e0
MB
224 /* Ack any unmasked IRQs */
225 for (i = 0; i < ARRAY_SIZE(status); i++) {
226 if (status[i])
227 wm8994_reg_write(wm8994, WM8994_INTERRUPT_STATUS_1 + i,
228 status[i]);
229 }
230
ccd80c71
MB
231 /* Report */
232 for (i = 0; i < ARRAY_SIZE(wm8994_irqs); i++) {
233 if (status[wm8994_irqs[i].reg - 1] & wm8994_irqs[i].mask)
234 handle_nested_irq(wm8994->irq_base + i);
235 }
236
c9fbf7e0
MB
237 return IRQ_HANDLED;
238}
239
240int wm8994_irq_init(struct wm8994 *wm8994)
241{
242 int i, cur_irq, ret;
243
244 mutex_init(&wm8994->irq_lock);
245
246 /* Mask the individual interrupt sources */
247 for (i = 0; i < ARRAY_SIZE(wm8994->irq_masks_cur); i++) {
248 wm8994->irq_masks_cur[i] = 0xffff;
249 wm8994->irq_masks_cache[i] = 0xffff;
250 wm8994_reg_write(wm8994, WM8994_INTERRUPT_STATUS_1_MASK + i,
251 0xffff);
252 }
253
254 if (!wm8994->irq) {
255 dev_warn(wm8994->dev,
256 "No interrupt specified, no interrupts\n");
257 wm8994->irq_base = 0;
258 return 0;
259 }
260
261 if (!wm8994->irq_base) {
262 dev_err(wm8994->dev,
263 "No interrupt base specified, no interrupts\n");
264 return 0;
265 }
266
267 /* Register them with genirq */
268 for (cur_irq = wm8994->irq_base;
269 cur_irq < ARRAY_SIZE(wm8994_irqs) + wm8994->irq_base;
270 cur_irq++) {
d5bb1221
TG
271 irq_set_chip_data(cur_irq, wm8994);
272 irq_set_chip_and_handler(cur_irq, &wm8994_irq_chip,
c9fbf7e0 273 handle_edge_irq);
d5bb1221 274 irq_set_nested_thread(cur_irq, 1);
c9fbf7e0
MB
275
276 /* ARM needs us to explicitly flag the IRQ as valid
277 * and will set them noprobe when we do so. */
278#ifdef CONFIG_ARM
279 set_irq_flags(cur_irq, IRQF_VALID);
280#else
d5bb1221 281 irq_set_noprobe(cur_irq);
c9fbf7e0
MB
282#endif
283 }
284
285 ret = request_threaded_irq(wm8994->irq, NULL, wm8994_irq_thread,
286 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
287 "wm8994", wm8994);
288 if (ret != 0) {
289 dev_err(wm8994->dev, "Failed to request IRQ %d: %d\n",
290 wm8994->irq, ret);
291 return ret;
292 }
293
294 /* Enable top level interrupt if it was masked */
295 wm8994_reg_write(wm8994, WM8994_INTERRUPT_CONTROL, 0);
296
297 return 0;
298}
299
300void wm8994_irq_exit(struct wm8994 *wm8994)
301{
302 if (wm8994->irq)
303 free_irq(wm8994->irq, wm8994);
304}
This page took 0.180069 seconds and 5 git commands to generate.