blob: d420dbc0e2b0065dbb1f03d29d6e4c9c20c7abc2 [file] [log] [blame]
Mark Brown966cdc92012-06-19 16:34:23 +01001/*
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
29static 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
40int 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}
50EXPORT_SYMBOL_GPL(arizona_request_irq);
51
52void 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}
60EXPORT_SYMBOL_GPL(arizona_free_irq);
61
62int 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}
70EXPORT_SYMBOL_GPL(arizona_set_irq_wake);
71
72static 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
81static 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
94static irqreturn_t arizona_irq_thread(int irq, void *data)
95{
96 struct arizona *arizona = data;
Mark Brown3092f802013-03-24 23:05:58 +000097 bool poll;
Mark Brown3080de42012-08-21 20:02:03 +010098 unsigned int val;
Mark Browncdabc1c2012-09-08 09:00:59 +080099 int ret;
Mark Brown966cdc92012-06-19 16:34:23 +0100100
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
Mark Brown3092f802013-03-24 23:05:58 +0000107 do {
108 poll = false;
Mark Brown3080de42012-08-21 20:02:03 +0100109
Mark Brown3092f802013-03-24 23:05:58 +0000110 /* 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 }
125
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);
Mark Brown966cdc92012-06-19 16:34:23 +0100140
141 pm_runtime_mark_last_busy(arizona->dev);
142 pm_runtime_put_autosuspend(arizona->dev);
143
144 return IRQ_HANDLED;
145}
146
147static void arizona_irq_enable(struct irq_data *data)
148{
149}
150
151static void arizona_irq_disable(struct irq_data *data)
152{
153}
154
155static struct irq_chip arizona_irq_chip = {
156 .name = "arizona",
157 .irq_disable = arizona_irq_disable,
158 .irq_enable = arizona_irq_enable,
159};
160
161static 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
181static struct irq_domain_ops arizona_domain_ops = {
182 .map = arizona_irq_map,
183 .xlate = irq_domain_xlate_twocell,
184};
185
186int arizona_irq_init(struct arizona *arizona)
187{
188 int flags = IRQF_ONESHOT;
189 int ret, i;
190 const struct regmap_irq_chip *aod, *irq;
Mark Brown22c75fe2013-03-24 23:16:56 +0000191 struct irq_data *irq_data;
Mark Brown966cdc92012-06-19 16:34:23 +0100192
Charles Keepax30a2af32014-07-15 11:21:50 +0100193 arizona->ctrlif_error = true;
194
Mark Brown966cdc92012-06-19 16:34:23 +0100195 switch (arizona->type) {
Mark Brown863df8d2012-07-05 20:35:31 +0100196#ifdef CONFIG_MFD_WM5102
Mark Brown966cdc92012-06-19 16:34:23 +0100197 case WM5102:
198 aod = &wm5102_aod;
199 irq = &wm5102_irq;
Mark Brown92d80132012-08-07 19:57:53 +0100200
Charles Keepax30a2af32014-07-15 11:21:50 +0100201 arizona->ctrlif_error = false;
Mark Brown966cdc92012-06-19 16:34:23 +0100202 break;
Mark Brown863df8d2012-07-05 20:35:31 +0100203#endif
Mark Browne102bef2012-07-10 12:37:58 +0100204#ifdef CONFIG_MFD_WM5110
205 case WM5110:
206 aod = &wm5110_aod;
Charles Keepax32155012014-07-15 11:21:48 +0100207
208 switch (arizona->rev) {
209 case 0 ... 2:
210 irq = &wm5110_irq;
211 break;
212 default:
213 irq = &wm5110_revd_irq;
214 break;
215 }
Mark Brown92d80132012-08-07 19:57:53 +0100216
Charles Keepax30a2af32014-07-15 11:21:50 +0100217 arizona->ctrlif_error = false;
Mark Browne102bef2012-07-10 12:37:58 +0100218 break;
219#endif
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100220#ifdef CONFIG_MFD_WM8997
221 case WM8997:
222 aod = &wm8997_aod;
223 irq = &wm8997_irq;
224
Charles Keepax30a2af32014-07-15 11:21:50 +0100225 arizona->ctrlif_error = false;
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100226 break;
227#endif
Mark Brown966cdc92012-06-19 16:34:23 +0100228 default:
229 BUG_ON("Unknown Arizona class device" == NULL);
230 return -EINVAL;
231 }
232
Mark Brown1816cb32013-01-17 16:54:18 +0900233 /* Disable all wake sources by default */
234 regmap_write(arizona->regmap, ARIZONA_WAKE_CONTROL, 0);
235
Mark Brown22c75fe2013-03-24 23:16:56 +0000236 /* Read the flags from the interrupt controller if not specified */
237 if (!arizona->pdata.irq_flags) {
238 irq_data = irq_get_irq_data(arizona->irq);
239 if (!irq_data) {
240 dev_err(arizona->dev, "Invalid IRQ: %d\n",
241 arizona->irq);
242 return -EINVAL;
243 }
244
245 arizona->pdata.irq_flags = irqd_get_trigger_type(irq_data);
246 switch (arizona->pdata.irq_flags) {
247 case IRQF_TRIGGER_LOW:
248 case IRQF_TRIGGER_HIGH:
249 case IRQF_TRIGGER_RISING:
250 case IRQF_TRIGGER_FALLING:
251 break;
252
253 case IRQ_TYPE_NONE:
254 default:
255 /* Device default */
256 arizona->pdata.irq_flags = IRQF_TRIGGER_LOW;
257 break;
258 }
259 }
Mark Brownf8a09412013-03-22 12:59:33 +0100260
261 if (arizona->pdata.irq_flags & (IRQF_TRIGGER_HIGH |
262 IRQF_TRIGGER_RISING)) {
Mark Brown966cdc92012-06-19 16:34:23 +0100263 ret = regmap_update_bits(arizona->regmap, ARIZONA_IRQ_CTRL_1,
264 ARIZONA_IRQ_POL, 0);
265 if (ret != 0) {
266 dev_err(arizona->dev, "Couldn't set IRQ polarity: %d\n",
267 ret);
268 goto err;
269 }
Mark Brown966cdc92012-06-19 16:34:23 +0100270 }
271
Mark Brownf8a09412013-03-22 12:59:33 +0100272 flags |= arizona->pdata.irq_flags;
273
Mark Brown966cdc92012-06-19 16:34:23 +0100274 /* Allocate a virtual IRQ domain to distribute to the regmap domains */
275 arizona->virq = irq_domain_add_linear(NULL, 2, &arizona_domain_ops,
276 arizona);
277 if (!arizona->virq) {
Mark Brownb7dea5d2012-12-05 11:46:26 +0900278 dev_err(arizona->dev, "Failed to add core IRQ domain\n");
Mark Brown966cdc92012-06-19 16:34:23 +0100279 ret = -EINVAL;
280 goto err;
281 }
282
283 ret = regmap_add_irq_chip(arizona->regmap,
284 irq_create_mapping(arizona->virq, 0),
285 IRQF_ONESHOT, -1, aod,
286 &arizona->aod_irq_chip);
287 if (ret != 0) {
288 dev_err(arizona->dev, "Failed to add AOD IRQs: %d\n", ret);
289 goto err_domain;
290 }
291
292 ret = regmap_add_irq_chip(arizona->regmap,
293 irq_create_mapping(arizona->virq, 1),
294 IRQF_ONESHOT, -1, irq,
295 &arizona->irq_chip);
296 if (ret != 0) {
Charles Keepaxd1cb4cc2014-05-19 17:35:29 +0100297 dev_err(arizona->dev, "Failed to add main IRQs: %d\n", ret);
Mark Brown966cdc92012-06-19 16:34:23 +0100298 goto err_aod;
299 }
300
301 /* Make sure the boot done IRQ is unmasked for resumes */
302 i = arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE);
303 ret = request_threaded_irq(i, NULL, arizona_boot_done, IRQF_ONESHOT,
304 "Boot done", arizona);
305 if (ret != 0) {
306 dev_err(arizona->dev, "Failed to request boot done %d: %d\n",
307 arizona->irq, ret);
308 goto err_boot_done;
309 }
310
311 /* Handle control interface errors in the core */
Charles Keepax30a2af32014-07-15 11:21:50 +0100312 if (arizona->ctrlif_error) {
Mark Brown92d80132012-08-07 19:57:53 +0100313 i = arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR);
314 ret = request_threaded_irq(i, NULL, arizona_ctrlif_err,
315 IRQF_ONESHOT,
316 "Control interface error", arizona);
317 if (ret != 0) {
318 dev_err(arizona->dev,
319 "Failed to request CTRLIF_ERR %d: %d\n",
320 arizona->irq, ret);
321 goto err_ctrlif;
322 }
Mark Brown966cdc92012-06-19 16:34:23 +0100323 }
324
Mark Brown3092f802013-03-24 23:05:58 +0000325 /* Used to emulate edge trigger and to work around broken pinmux */
326 if (arizona->pdata.irq_gpio) {
327 if (gpio_to_irq(arizona->pdata.irq_gpio) != arizona->irq) {
328 dev_warn(arizona->dev, "IRQ %d is not GPIO %d (%d)\n",
329 arizona->irq, arizona->pdata.irq_gpio,
330 gpio_to_irq(arizona->pdata.irq_gpio));
331 arizona->irq = gpio_to_irq(arizona->pdata.irq_gpio);
332 }
333
334 ret = devm_gpio_request_one(arizona->dev,
335 arizona->pdata.irq_gpio,
336 GPIOF_IN, "arizona IRQ");
337 if (ret != 0) {
338 dev_err(arizona->dev,
339 "Failed to request IRQ GPIO %d:: %d\n",
340 arizona->pdata.irq_gpio, ret);
341 arizona->pdata.irq_gpio = 0;
342 }
343 }
344
Mark Brown966cdc92012-06-19 16:34:23 +0100345 ret = request_threaded_irq(arizona->irq, NULL, arizona_irq_thread,
346 flags, "arizona", arizona);
347
348 if (ret != 0) {
Mark Brown7994c662013-03-19 09:51:14 +0000349 dev_err(arizona->dev, "Failed to request primary IRQ %d: %d\n",
Mark Brown966cdc92012-06-19 16:34:23 +0100350 arizona->irq, ret);
351 goto err_main_irq;
352 }
353
354 return 0;
355
356err_main_irq:
Charles Keepax30a2af32014-07-15 11:21:50 +0100357 if (arizona->ctrlif_error)
358 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR),
359 arizona);
Mark Brown966cdc92012-06-19 16:34:23 +0100360err_ctrlif:
361 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona);
362err_boot_done:
363 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1),
364 arizona->irq_chip);
365err_aod:
366 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0),
367 arizona->aod_irq_chip);
368err_domain:
369err:
370 return ret;
371}
372
373int arizona_irq_exit(struct arizona *arizona)
374{
Charles Keepax30a2af32014-07-15 11:21:50 +0100375 if (arizona->ctrlif_error)
376 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR),
377 arizona);
Mark Brown966cdc92012-06-19 16:34:23 +0100378 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona);
379 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1),
380 arizona->irq_chip);
381 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0),
382 arizona->aod_irq_chip);
383 free_irq(arizona->irq, arizona);
384
385 return 0;
386}