blob: 5e18d3c77582b02050da45db97ee87fc1777bab9 [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
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +000033 if (arizona->aod_irq_chip) {
34 ret = regmap_irq_get_virq(arizona->aod_irq_chip, irq);
35 if (ret >= 0)
36 return ret;
37 }
Mark Brown966cdc92012-06-19 16:34:23 +010038
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +000039 return regmap_irq_get_virq(arizona->irq_chip, irq);
Mark Brown966cdc92012-06-19 16:34:23 +010040}
41
42int arizona_request_irq(struct arizona *arizona, int irq, char *name,
43 irq_handler_t handler, void *data)
44{
45 irq = arizona_map_irq(arizona, irq);
46 if (irq < 0)
47 return irq;
48
49 return request_threaded_irq(irq, NULL, handler, IRQF_ONESHOT,
50 name, data);
51}
52EXPORT_SYMBOL_GPL(arizona_request_irq);
53
54void arizona_free_irq(struct arizona *arizona, int irq, void *data)
55{
56 irq = arizona_map_irq(arizona, irq);
57 if (irq < 0)
58 return;
59
60 free_irq(irq, data);
61}
62EXPORT_SYMBOL_GPL(arizona_free_irq);
63
64int arizona_set_irq_wake(struct arizona *arizona, int irq, int on)
65{
66 irq = arizona_map_irq(arizona, irq);
67 if (irq < 0)
68 return irq;
69
70 return irq_set_irq_wake(irq, on);
71}
72EXPORT_SYMBOL_GPL(arizona_set_irq_wake);
73
74static irqreturn_t arizona_boot_done(int irq, void *data)
75{
76 struct arizona *arizona = data;
77
78 dev_dbg(arizona->dev, "Boot done\n");
79
80 return IRQ_HANDLED;
81}
82
83static irqreturn_t arizona_ctrlif_err(int irq, void *data)
84{
85 struct arizona *arizona = data;
86
87 /*
88 * For pretty much all potential sources a register cache sync
89 * won't help, we've just got a software bug somewhere.
90 */
91 dev_err(arizona->dev, "Control interface error\n");
92
93 return IRQ_HANDLED;
94}
95
96static irqreturn_t arizona_irq_thread(int irq, void *data)
97{
98 struct arizona *arizona = data;
Mark Brown3092f802013-03-24 23:05:58 +000099 bool poll;
Mark Brown3080de42012-08-21 20:02:03 +0100100 unsigned int val;
Mark Browncdabc1c2012-09-08 09:00:59 +0800101 int ret;
Mark Brown966cdc92012-06-19 16:34:23 +0100102
103 ret = pm_runtime_get_sync(arizona->dev);
104 if (ret < 0) {
105 dev_err(arizona->dev, "Failed to resume device: %d\n", ret);
106 return IRQ_NONE;
107 }
108
Mark Brown3092f802013-03-24 23:05:58 +0000109 do {
110 poll = false;
Mark Brown3080de42012-08-21 20:02:03 +0100111
Richard Fitzgerald1f2c3972016-06-15 10:28:44 +0100112 if (arizona->aod_irq_chip) {
113 /*
114 * Check the AOD status register to determine whether
115 * the nested IRQ handler should be called.
116 */
117 ret = regmap_read(arizona->regmap,
118 ARIZONA_AOD_IRQ1, &val);
119 if (ret)
120 dev_warn(arizona->dev,
121 "Failed to read AOD IRQ1 %d\n", ret);
122 else if (val)
123 handle_nested_irq(
124 irq_find_mapping(arizona->virq, 0));
125 }
Mark Brown3092f802013-03-24 23:05:58 +0000126
127 /*
128 * Check if one of the main interrupts is asserted and only
129 * check that domain if it is.
130 */
131 ret = regmap_read(arizona->regmap, ARIZONA_IRQ_PIN_STATUS,
132 &val);
133 if (ret == 0 && val & ARIZONA_IRQ1_STS) {
134 handle_nested_irq(irq_find_mapping(arizona->virq, 1));
135 } else if (ret != 0) {
136 dev_err(arizona->dev,
137 "Failed to read main IRQ status: %d\n", ret);
138 }
139
140 /*
141 * Poll the IRQ pin status to see if we're really done
142 * if the interrupt controller can't do it for us.
143 */
144 if (!arizona->pdata.irq_gpio) {
145 break;
146 } else if (arizona->pdata.irq_flags & IRQF_TRIGGER_RISING &&
147 gpio_get_value_cansleep(arizona->pdata.irq_gpio)) {
148 poll = true;
149 } else if (arizona->pdata.irq_flags & IRQF_TRIGGER_FALLING &&
150 !gpio_get_value_cansleep(arizona->pdata.irq_gpio)) {
151 poll = true;
152 }
153 } while (poll);
Mark Brown966cdc92012-06-19 16:34:23 +0100154
155 pm_runtime_mark_last_busy(arizona->dev);
156 pm_runtime_put_autosuspend(arizona->dev);
157
158 return IRQ_HANDLED;
159}
160
161static void arizona_irq_enable(struct irq_data *data)
162{
163}
164
165static void arizona_irq_disable(struct irq_data *data)
166{
167}
168
Charles Keepaxc38715f2014-09-01 15:29:11 +0100169static int arizona_irq_set_wake(struct irq_data *data, unsigned int on)
170{
171 struct arizona *arizona = irq_data_get_irq_chip_data(data);
172
173 return irq_set_irq_wake(arizona->irq, on);
174}
175
Mark Brown966cdc92012-06-19 16:34:23 +0100176static struct irq_chip arizona_irq_chip = {
177 .name = "arizona",
178 .irq_disable = arizona_irq_disable,
179 .irq_enable = arizona_irq_enable,
Charles Keepaxc38715f2014-09-01 15:29:11 +0100180 .irq_set_wake = arizona_irq_set_wake,
Mark Brown966cdc92012-06-19 16:34:23 +0100181};
182
Charles Keepaxdedf24a2016-03-25 14:27:09 +0000183static struct lock_class_key arizona_irq_lock_class;
184
Mark Brown966cdc92012-06-19 16:34:23 +0100185static int arizona_irq_map(struct irq_domain *h, unsigned int virq,
186 irq_hw_number_t hw)
187{
Charles Keepax0a464df2015-09-11 16:07:56 +0100188 struct arizona *data = h->host_data;
Mark Brown966cdc92012-06-19 16:34:23 +0100189
190 irq_set_chip_data(virq, data);
Charles Keepaxdedf24a2016-03-25 14:27:09 +0000191 irq_set_lockdep_class(virq, &arizona_irq_lock_class);
Charles Keepaxcfeb35d2014-09-09 17:00:09 +0100192 irq_set_chip_and_handler(virq, &arizona_irq_chip, handle_simple_irq);
Mark Brown966cdc92012-06-19 16:34:23 +0100193 irq_set_nested_thread(virq, 1);
Mark Brown966cdc92012-06-19 16:34:23 +0100194 irq_set_noprobe(virq);
Mark Brown966cdc92012-06-19 16:34:23 +0100195
196 return 0;
197}
198
Krzysztof Kozlowski7ce7b262015-04-27 21:54:13 +0900199static const struct irq_domain_ops arizona_domain_ops = {
Mark Brown966cdc92012-06-19 16:34:23 +0100200 .map = arizona_irq_map,
201 .xlate = irq_domain_xlate_twocell,
202};
203
204int arizona_irq_init(struct arizona *arizona)
205{
206 int flags = IRQF_ONESHOT;
207 int ret, i;
208 const struct regmap_irq_chip *aod, *irq;
Mark Brown22c75fe2013-03-24 23:16:56 +0000209 struct irq_data *irq_data;
Mark Brown966cdc92012-06-19 16:34:23 +0100210
Charles Keepax30a2af32014-07-15 11:21:50 +0100211 arizona->ctrlif_error = true;
212
Mark Brown966cdc92012-06-19 16:34:23 +0100213 switch (arizona->type) {
Mark Brown863df8d2012-07-05 20:35:31 +0100214#ifdef CONFIG_MFD_WM5102
Mark Brown966cdc92012-06-19 16:34:23 +0100215 case WM5102:
216 aod = &wm5102_aod;
217 irq = &wm5102_irq;
Mark Brown92d80132012-08-07 19:57:53 +0100218
Charles Keepax30a2af32014-07-15 11:21:50 +0100219 arizona->ctrlif_error = false;
Mark Brown966cdc92012-06-19 16:34:23 +0100220 break;
Mark Brown863df8d2012-07-05 20:35:31 +0100221#endif
Mark Browne102bef2012-07-10 12:37:58 +0100222#ifdef CONFIG_MFD_WM5110
223 case WM5110:
Richard Fitzgeralde5d4ef02015-01-17 15:21:22 +0000224 case WM8280:
Mark Browne102bef2012-07-10 12:37:58 +0100225 aod = &wm5110_aod;
Charles Keepax32155012014-07-15 11:21:48 +0100226
227 switch (arizona->rev) {
228 case 0 ... 2:
229 irq = &wm5110_irq;
230 break;
231 default:
232 irq = &wm5110_revd_irq;
233 break;
234 }
Mark Brown92d80132012-08-07 19:57:53 +0100235
Charles Keepax30a2af32014-07-15 11:21:50 +0100236 arizona->ctrlif_error = false;
Mark Browne102bef2012-07-10 12:37:58 +0100237 break;
238#endif
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000239#ifdef CONFIG_MFD_CS47L24
240 case WM1831:
241 case CS47L24:
242 aod = NULL;
243 irq = &cs47l24_irq;
244
245 arizona->ctrlif_error = false;
246 break;
247#endif
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100248#ifdef CONFIG_MFD_WM8997
249 case WM8997:
250 aod = &wm8997_aod;
251 irq = &wm8997_irq;
252
Charles Keepax30a2af32014-07-15 11:21:50 +0100253 arizona->ctrlif_error = false;
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100254 break;
255#endif
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100256#ifdef CONFIG_MFD_WM8998
257 case WM8998:
258 case WM1814:
259 aod = &wm8998_aod;
260 irq = &wm8998_irq;
261
262 arizona->ctrlif_error = false;
263 break;
264#endif
Mark Brown966cdc92012-06-19 16:34:23 +0100265 default:
266 BUG_ON("Unknown Arizona class device" == NULL);
267 return -EINVAL;
268 }
269
Mark Brown1816cb32013-01-17 16:54:18 +0900270 /* Disable all wake sources by default */
271 regmap_write(arizona->regmap, ARIZONA_WAKE_CONTROL, 0);
272
Mark Brown22c75fe2013-03-24 23:16:56 +0000273 /* Read the flags from the interrupt controller if not specified */
274 if (!arizona->pdata.irq_flags) {
275 irq_data = irq_get_irq_data(arizona->irq);
276 if (!irq_data) {
277 dev_err(arizona->dev, "Invalid IRQ: %d\n",
278 arizona->irq);
279 return -EINVAL;
280 }
281
282 arizona->pdata.irq_flags = irqd_get_trigger_type(irq_data);
283 switch (arizona->pdata.irq_flags) {
284 case IRQF_TRIGGER_LOW:
285 case IRQF_TRIGGER_HIGH:
286 case IRQF_TRIGGER_RISING:
287 case IRQF_TRIGGER_FALLING:
288 break;
289
290 case IRQ_TYPE_NONE:
291 default:
292 /* Device default */
293 arizona->pdata.irq_flags = IRQF_TRIGGER_LOW;
294 break;
295 }
296 }
Mark Brownf8a09412013-03-22 12:59:33 +0100297
298 if (arizona->pdata.irq_flags & (IRQF_TRIGGER_HIGH |
299 IRQF_TRIGGER_RISING)) {
Mark Brown966cdc92012-06-19 16:34:23 +0100300 ret = regmap_update_bits(arizona->regmap, ARIZONA_IRQ_CTRL_1,
301 ARIZONA_IRQ_POL, 0);
302 if (ret != 0) {
303 dev_err(arizona->dev, "Couldn't set IRQ polarity: %d\n",
304 ret);
305 goto err;
306 }
Mark Brown966cdc92012-06-19 16:34:23 +0100307 }
308
Mark Brownf8a09412013-03-22 12:59:33 +0100309 flags |= arizona->pdata.irq_flags;
310
Mark Brown966cdc92012-06-19 16:34:23 +0100311 /* Allocate a virtual IRQ domain to distribute to the regmap domains */
312 arizona->virq = irq_domain_add_linear(NULL, 2, &arizona_domain_ops,
313 arizona);
314 if (!arizona->virq) {
Mark Brownb7dea5d2012-12-05 11:46:26 +0900315 dev_err(arizona->dev, "Failed to add core IRQ domain\n");
Mark Brown966cdc92012-06-19 16:34:23 +0100316 ret = -EINVAL;
317 goto err;
318 }
319
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000320 if (aod) {
321 ret = regmap_add_irq_chip(arizona->regmap,
322 irq_create_mapping(arizona->virq, 0),
323 IRQF_ONESHOT, 0, aod,
324 &arizona->aod_irq_chip);
325 if (ret != 0) {
326 dev_err(arizona->dev,
327 "Failed to add AOD IRQs: %d\n", ret);
Charles Keepax6c006b12015-12-16 13:53:59 +0000328 goto err;
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000329 }
Mark Brown966cdc92012-06-19 16:34:23 +0100330 }
331
332 ret = regmap_add_irq_chip(arizona->regmap,
333 irq_create_mapping(arizona->virq, 1),
Charles Keepax2a3377e2014-08-12 14:51:22 +0100334 IRQF_ONESHOT, 0, irq,
Mark Brown966cdc92012-06-19 16:34:23 +0100335 &arizona->irq_chip);
336 if (ret != 0) {
Charles Keepaxd1cb4cc2014-05-19 17:35:29 +0100337 dev_err(arizona->dev, "Failed to add main IRQs: %d\n", ret);
Mark Brown966cdc92012-06-19 16:34:23 +0100338 goto err_aod;
339 }
340
Mark Brown3092f802013-03-24 23:05:58 +0000341 /* Used to emulate edge trigger and to work around broken pinmux */
342 if (arizona->pdata.irq_gpio) {
343 if (gpio_to_irq(arizona->pdata.irq_gpio) != arizona->irq) {
344 dev_warn(arizona->dev, "IRQ %d is not GPIO %d (%d)\n",
345 arizona->irq, arizona->pdata.irq_gpio,
346 gpio_to_irq(arizona->pdata.irq_gpio));
347 arizona->irq = gpio_to_irq(arizona->pdata.irq_gpio);
348 }
349
350 ret = devm_gpio_request_one(arizona->dev,
351 arizona->pdata.irq_gpio,
352 GPIOF_IN, "arizona IRQ");
353 if (ret != 0) {
354 dev_err(arizona->dev,
355 "Failed to request IRQ GPIO %d:: %d\n",
356 arizona->pdata.irq_gpio, ret);
357 arizona->pdata.irq_gpio = 0;
358 }
359 }
360
Mark Brown966cdc92012-06-19 16:34:23 +0100361 ret = request_threaded_irq(arizona->irq, NULL, arizona_irq_thread,
362 flags, "arizona", arizona);
363
364 if (ret != 0) {
Mark Brown7994c662013-03-19 09:51:14 +0000365 dev_err(arizona->dev, "Failed to request primary IRQ %d: %d\n",
Mark Brown966cdc92012-06-19 16:34:23 +0100366 arizona->irq, ret);
367 goto err_main_irq;
368 }
369
Charles Keepax6c006b12015-12-16 13:53:59 +0000370 /* Make sure the boot done IRQ is unmasked for resumes */
371 i = arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE);
372 ret = request_threaded_irq(i, NULL, arizona_boot_done, IRQF_ONESHOT,
373 "Boot done", arizona);
374 if (ret != 0) {
375 dev_err(arizona->dev, "Failed to request boot done %d: %d\n",
376 arizona->irq, ret);
377 goto err_boot_done;
378 }
379
380 /* Handle control interface errors in the core */
381 if (arizona->ctrlif_error) {
382 i = arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR);
383 ret = request_threaded_irq(i, NULL, arizona_ctrlif_err,
384 IRQF_ONESHOT,
385 "Control interface error", arizona);
386 if (ret != 0) {
387 dev_err(arizona->dev,
388 "Failed to request CTRLIF_ERR %d: %d\n",
389 arizona->irq, ret);
390 goto err_ctrlif;
391 }
392 }
393
Mark Brown966cdc92012-06-19 16:34:23 +0100394 return 0;
395
Mark Brown966cdc92012-06-19 16:34:23 +0100396err_ctrlif:
397 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona);
398err_boot_done:
Charles Keepax6c006b12015-12-16 13:53:59 +0000399 free_irq(arizona->irq, arizona);
400err_main_irq:
Mark Brown966cdc92012-06-19 16:34:23 +0100401 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1),
402 arizona->irq_chip);
403err_aod:
404 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0),
405 arizona->aod_irq_chip);
Mark Brown966cdc92012-06-19 16:34:23 +0100406err:
407 return ret;
408}
409
410int arizona_irq_exit(struct arizona *arizona)
411{
Charles Keepax30a2af32014-07-15 11:21:50 +0100412 if (arizona->ctrlif_error)
413 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR),
414 arizona);
Mark Brown966cdc92012-06-19 16:34:23 +0100415 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona);
416 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1),
417 arizona->irq_chip);
418 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0),
419 arizona->aod_irq_chip);
420 free_irq(arizona->irq, arizona);
421
422 return 0;
423}