Commit | Line | Data |
---|---|---|
966cdc96 MB |
1 | /* |
2 | * Arizona interrupt support | |
3 | * | |
4 | * Copyright 2012 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 | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | */ | |
12 | ||
13 | #include <linux/delay.h> | |
14 | #include <linux/gpio.h> | |
15 | #include <linux/interrupt.h> | |
16 | #include <linux/irq.h> | |
17 | #include <linux/irqdomain.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/pm_runtime.h> | |
20 | #include <linux/regmap.h> | |
21 | #include <linux/regulator/consumer.h> | |
22 | #include <linux/slab.h> | |
23 | ||
24 | #include <linux/mfd/arizona/core.h> | |
25 | #include <linux/mfd/arizona/registers.h> | |
26 | ||
27 | #include "arizona.h" | |
28 | ||
29 | static int arizona_map_irq(struct arizona *arizona, int irq) | |
30 | { | |
31 | int ret; | |
32 | ||
33 | ret = regmap_irq_get_virq(arizona->aod_irq_chip, irq); | |
34 | if (ret < 0) | |
35 | ret = regmap_irq_get_virq(arizona->irq_chip, irq); | |
36 | ||
37 | return ret; | |
38 | } | |
39 | ||
40 | int arizona_request_irq(struct arizona *arizona, int irq, char *name, | |
41 | irq_handler_t handler, void *data) | |
42 | { | |
43 | irq = arizona_map_irq(arizona, irq); | |
44 | if (irq < 0) | |
45 | return irq; | |
46 | ||
47 | return request_threaded_irq(irq, NULL, handler, IRQF_ONESHOT, | |
48 | name, data); | |
49 | } | |
50 | EXPORT_SYMBOL_GPL(arizona_request_irq); | |
51 | ||
52 | void arizona_free_irq(struct arizona *arizona, int irq, void *data) | |
53 | { | |
54 | irq = arizona_map_irq(arizona, irq); | |
55 | if (irq < 0) | |
56 | return; | |
57 | ||
58 | free_irq(irq, data); | |
59 | } | |
60 | EXPORT_SYMBOL_GPL(arizona_free_irq); | |
61 | ||
62 | int arizona_set_irq_wake(struct arizona *arizona, int irq, int on) | |
63 | { | |
64 | irq = arizona_map_irq(arizona, irq); | |
65 | if (irq < 0) | |
66 | return irq; | |
67 | ||
68 | return irq_set_irq_wake(irq, on); | |
69 | } | |
70 | EXPORT_SYMBOL_GPL(arizona_set_irq_wake); | |
71 | ||
72 | static irqreturn_t arizona_boot_done(int irq, void *data) | |
73 | { | |
74 | struct arizona *arizona = data; | |
75 | ||
76 | dev_dbg(arizona->dev, "Boot done\n"); | |
77 | ||
78 | return IRQ_HANDLED; | |
79 | } | |
80 | ||
81 | static irqreturn_t arizona_ctrlif_err(int irq, void *data) | |
82 | { | |
83 | struct arizona *arizona = data; | |
84 | ||
85 | /* | |
86 | * For pretty much all potential sources a register cache sync | |
87 | * won't help, we've just got a software bug somewhere. | |
88 | */ | |
89 | dev_err(arizona->dev, "Control interface error\n"); | |
90 | ||
91 | return IRQ_HANDLED; | |
92 | } | |
93 | ||
94 | static irqreturn_t arizona_irq_thread(int irq, void *data) | |
95 | { | |
96 | struct arizona *arizona = data; | |
3092f805 | 97 | bool poll; |
3080de4e | 98 | unsigned int val; |
cdabc1c8 | 99 | int ret; |
966cdc96 MB |
100 | |
101 | ret = pm_runtime_get_sync(arizona->dev); | |
102 | if (ret < 0) { | |
103 | dev_err(arizona->dev, "Failed to resume device: %d\n", ret); | |
104 | return IRQ_NONE; | |
105 | } | |
106 | ||
3092f805 MB |
107 | do { |
108 | poll = false; | |
109 | ||
110 | /* Always handle the AoD domain */ | |
111 | handle_nested_irq(irq_find_mapping(arizona->virq, 0)); | |
112 | ||
113 | /* | |
114 | * Check if one of the main interrupts is asserted and only | |
115 | * check that domain if it is. | |
116 | */ | |
117 | ret = regmap_read(arizona->regmap, ARIZONA_IRQ_PIN_STATUS, | |
118 | &val); | |
119 | if (ret == 0 && val & ARIZONA_IRQ1_STS) { | |
120 | handle_nested_irq(irq_find_mapping(arizona->virq, 1)); | |
121 | } else if (ret != 0) { | |
122 | dev_err(arizona->dev, | |
123 | "Failed to read main IRQ status: %d\n", ret); | |
124 | } | |
3080de4e | 125 | |
3092f805 MB |
126 | /* |
127 | * Poll the IRQ pin status to see if we're really done | |
128 | * if the interrupt controller can't do it for us. | |
129 | */ | |
130 | if (!arizona->pdata.irq_gpio) { | |
131 | break; | |
132 | } else if (arizona->pdata.irq_flags & IRQF_TRIGGER_RISING && | |
133 | gpio_get_value_cansleep(arizona->pdata.irq_gpio)) { | |
134 | poll = true; | |
135 | } else if (arizona->pdata.irq_flags & IRQF_TRIGGER_FALLING && | |
136 | !gpio_get_value_cansleep(arizona->pdata.irq_gpio)) { | |
137 | poll = true; | |
138 | } | |
139 | } while (poll); | |
966cdc96 MB |
140 | |
141 | pm_runtime_mark_last_busy(arizona->dev); | |
142 | pm_runtime_put_autosuspend(arizona->dev); | |
143 | ||
144 | return IRQ_HANDLED; | |
145 | } | |
146 | ||
147 | static void arizona_irq_enable(struct irq_data *data) | |
148 | { | |
149 | } | |
150 | ||
151 | static void arizona_irq_disable(struct irq_data *data) | |
152 | { | |
153 | } | |
154 | ||
155 | static struct irq_chip arizona_irq_chip = { | |
156 | .name = "arizona", | |
157 | .irq_disable = arizona_irq_disable, | |
158 | .irq_enable = arizona_irq_enable, | |
159 | }; | |
160 | ||
161 | static int arizona_irq_map(struct irq_domain *h, unsigned int virq, | |
162 | irq_hw_number_t hw) | |
163 | { | |
164 | struct regmap_irq_chip_data *data = h->host_data; | |
165 | ||
166 | irq_set_chip_data(virq, data); | |
167 | irq_set_chip_and_handler(virq, &arizona_irq_chip, handle_edge_irq); | |
168 | irq_set_nested_thread(virq, 1); | |
169 | ||
170 | /* ARM needs us to explicitly flag the IRQ as valid | |
171 | * and will set them noprobe when we do so. */ | |
172 | #ifdef CONFIG_ARM | |
173 | set_irq_flags(virq, IRQF_VALID); | |
174 | #else | |
175 | irq_set_noprobe(virq); | |
176 | #endif | |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
181 | static struct irq_domain_ops arizona_domain_ops = { | |
182 | .map = arizona_irq_map, | |
183 | .xlate = irq_domain_xlate_twocell, | |
184 | }; | |
185 | ||
186 | int arizona_irq_init(struct arizona *arizona) | |
187 | { | |
188 | int flags = IRQF_ONESHOT; | |
189 | int ret, i; | |
190 | const struct regmap_irq_chip *aod, *irq; | |
92d80139 | 191 | bool ctrlif_error = true; |
22c75fe7 | 192 | struct irq_data *irq_data; |
966cdc96 MB |
193 | |
194 | switch (arizona->type) { | |
863df8d5 | 195 | #ifdef CONFIG_MFD_WM5102 |
966cdc96 MB |
196 | case WM5102: |
197 | aod = &wm5102_aod; | |
198 | irq = &wm5102_irq; | |
92d80139 | 199 | |
e1bfe75d | 200 | ctrlif_error = false; |
966cdc96 | 201 | break; |
e102befe MB |
202 | #endif |
203 | #ifdef CONFIG_MFD_WM5110 | |
204 | case WM5110: | |
205 | aod = &wm5110_aod; | |
206 | irq = &wm5110_irq; | |
92d80139 | 207 | |
e1bfe75d | 208 | ctrlif_error = false; |
e102befe | 209 | break; |
863df8d5 | 210 | #endif |
dc7d4863 CK |
211 | #ifdef CONFIG_MFD_WM8997 |
212 | case WM8997: | |
213 | aod = &wm8997_aod; | |
214 | irq = &wm8997_irq; | |
215 | ||
216 | ctrlif_error = false; | |
217 | break; | |
218 | #endif | |
966cdc96 MB |
219 | default: |
220 | BUG_ON("Unknown Arizona class device" == NULL); | |
221 | return -EINVAL; | |
222 | } | |
223 | ||
1816cb34 MB |
224 | /* Disable all wake sources by default */ |
225 | regmap_write(arizona->regmap, ARIZONA_WAKE_CONTROL, 0); | |
226 | ||
22c75fe7 MB |
227 | /* Read the flags from the interrupt controller if not specified */ |
228 | if (!arizona->pdata.irq_flags) { | |
229 | irq_data = irq_get_irq_data(arizona->irq); | |
230 | if (!irq_data) { | |
231 | dev_err(arizona->dev, "Invalid IRQ: %d\n", | |
232 | arizona->irq); | |
233 | return -EINVAL; | |
234 | } | |
235 | ||
236 | arizona->pdata.irq_flags = irqd_get_trigger_type(irq_data); | |
237 | switch (arizona->pdata.irq_flags) { | |
238 | case IRQF_TRIGGER_LOW: | |
239 | case IRQF_TRIGGER_HIGH: | |
240 | case IRQF_TRIGGER_RISING: | |
241 | case IRQF_TRIGGER_FALLING: | |
242 | break; | |
243 | ||
244 | case IRQ_TYPE_NONE: | |
245 | default: | |
246 | /* Device default */ | |
247 | arizona->pdata.irq_flags = IRQF_TRIGGER_LOW; | |
248 | break; | |
249 | } | |
250 | } | |
f8a0941f MB |
251 | |
252 | if (arizona->pdata.irq_flags & (IRQF_TRIGGER_HIGH | | |
253 | IRQF_TRIGGER_RISING)) { | |
966cdc96 MB |
254 | ret = regmap_update_bits(arizona->regmap, ARIZONA_IRQ_CTRL_1, |
255 | ARIZONA_IRQ_POL, 0); | |
256 | if (ret != 0) { | |
257 | dev_err(arizona->dev, "Couldn't set IRQ polarity: %d\n", | |
258 | ret); | |
259 | goto err; | |
260 | } | |
966cdc96 MB |
261 | } |
262 | ||
f8a0941f MB |
263 | flags |= arizona->pdata.irq_flags; |
264 | ||
966cdc96 MB |
265 | /* Allocate a virtual IRQ domain to distribute to the regmap domains */ |
266 | arizona->virq = irq_domain_add_linear(NULL, 2, &arizona_domain_ops, | |
267 | arizona); | |
268 | if (!arizona->virq) { | |
b7dea5dc | 269 | dev_err(arizona->dev, "Failed to add core IRQ domain\n"); |
966cdc96 MB |
270 | ret = -EINVAL; |
271 | goto err; | |
272 | } | |
273 | ||
274 | ret = regmap_add_irq_chip(arizona->regmap, | |
275 | irq_create_mapping(arizona->virq, 0), | |
276 | IRQF_ONESHOT, -1, aod, | |
277 | &arizona->aod_irq_chip); | |
278 | if (ret != 0) { | |
279 | dev_err(arizona->dev, "Failed to add AOD IRQs: %d\n", ret); | |
280 | goto err_domain; | |
281 | } | |
282 | ||
283 | ret = regmap_add_irq_chip(arizona->regmap, | |
284 | irq_create_mapping(arizona->virq, 1), | |
285 | IRQF_ONESHOT, -1, irq, | |
286 | &arizona->irq_chip); | |
287 | if (ret != 0) { | |
288 | dev_err(arizona->dev, "Failed to add AOD IRQs: %d\n", ret); | |
289 | goto err_aod; | |
290 | } | |
291 | ||
292 | /* Make sure the boot done IRQ is unmasked for resumes */ | |
293 | i = arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE); | |
294 | ret = request_threaded_irq(i, NULL, arizona_boot_done, IRQF_ONESHOT, | |
295 | "Boot done", arizona); | |
296 | if (ret != 0) { | |
297 | dev_err(arizona->dev, "Failed to request boot done %d: %d\n", | |
298 | arizona->irq, ret); | |
299 | goto err_boot_done; | |
300 | } | |
301 | ||
302 | /* Handle control interface errors in the core */ | |
92d80139 MB |
303 | if (ctrlif_error) { |
304 | i = arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR); | |
305 | ret = request_threaded_irq(i, NULL, arizona_ctrlif_err, | |
306 | IRQF_ONESHOT, | |
307 | "Control interface error", arizona); | |
308 | if (ret != 0) { | |
309 | dev_err(arizona->dev, | |
310 | "Failed to request CTRLIF_ERR %d: %d\n", | |
311 | arizona->irq, ret); | |
312 | goto err_ctrlif; | |
313 | } | |
966cdc96 MB |
314 | } |
315 | ||
3092f805 MB |
316 | /* Used to emulate edge trigger and to work around broken pinmux */ |
317 | if (arizona->pdata.irq_gpio) { | |
318 | if (gpio_to_irq(arizona->pdata.irq_gpio) != arizona->irq) { | |
319 | dev_warn(arizona->dev, "IRQ %d is not GPIO %d (%d)\n", | |
320 | arizona->irq, arizona->pdata.irq_gpio, | |
321 | gpio_to_irq(arizona->pdata.irq_gpio)); | |
322 | arizona->irq = gpio_to_irq(arizona->pdata.irq_gpio); | |
323 | } | |
324 | ||
325 | ret = devm_gpio_request_one(arizona->dev, | |
326 | arizona->pdata.irq_gpio, | |
327 | GPIOF_IN, "arizona IRQ"); | |
328 | if (ret != 0) { | |
329 | dev_err(arizona->dev, | |
330 | "Failed to request IRQ GPIO %d:: %d\n", | |
331 | arizona->pdata.irq_gpio, ret); | |
332 | arizona->pdata.irq_gpio = 0; | |
333 | } | |
334 | } | |
335 | ||
966cdc96 MB |
336 | ret = request_threaded_irq(arizona->irq, NULL, arizona_irq_thread, |
337 | flags, "arizona", arizona); | |
338 | ||
339 | if (ret != 0) { | |
7994c664 | 340 | dev_err(arizona->dev, "Failed to request primary IRQ %d: %d\n", |
966cdc96 MB |
341 | arizona->irq, ret); |
342 | goto err_main_irq; | |
343 | } | |
344 | ||
345 | return 0; | |
346 | ||
347 | err_main_irq: | |
348 | free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR), arizona); | |
349 | err_ctrlif: | |
350 | free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona); | |
351 | err_boot_done: | |
352 | regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1), | |
353 | arizona->irq_chip); | |
354 | err_aod: | |
355 | regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0), | |
356 | arizona->aod_irq_chip); | |
357 | err_domain: | |
358 | err: | |
359 | return ret; | |
360 | } | |
361 | ||
362 | int arizona_irq_exit(struct arizona *arizona) | |
363 | { | |
364 | free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR), arizona); | |
365 | free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona); | |
366 | regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1), | |
367 | arizona->irq_chip); | |
368 | regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0), | |
369 | arizona->aod_irq_chip); | |
370 | free_irq(arizona->irq, arizona); | |
371 | ||
372 | return 0; | |
373 | } |