blob: 09cf3699e354415046af18c21eae3d98a445a53e [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
Charles Keepax1a86dcb2016-11-22 16:10:27 +000029#define ARIZONA_AOD_IRQ_INDEX 0
30#define ARIZONA_MAIN_IRQ_INDEX 1
31
Mark Brown966cdc92012-06-19 16:34:23 +010032static int arizona_map_irq(struct arizona *arizona, int irq)
33{
34 int ret;
35
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +000036 if (arizona->aod_irq_chip) {
37 ret = regmap_irq_get_virq(arizona->aod_irq_chip, irq);
38 if (ret >= 0)
39 return ret;
40 }
Mark Brown966cdc92012-06-19 16:34:23 +010041
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +000042 return regmap_irq_get_virq(arizona->irq_chip, irq);
Mark Brown966cdc92012-06-19 16:34:23 +010043}
44
45int arizona_request_irq(struct arizona *arizona, int irq, char *name,
46 irq_handler_t handler, void *data)
47{
48 irq = arizona_map_irq(arizona, irq);
49 if (irq < 0)
50 return irq;
51
52 return request_threaded_irq(irq, NULL, handler, IRQF_ONESHOT,
53 name, data);
54}
55EXPORT_SYMBOL_GPL(arizona_request_irq);
56
57void arizona_free_irq(struct arizona *arizona, int irq, void *data)
58{
59 irq = arizona_map_irq(arizona, irq);
60 if (irq < 0)
61 return;
62
63 free_irq(irq, data);
64}
65EXPORT_SYMBOL_GPL(arizona_free_irq);
66
67int arizona_set_irq_wake(struct arizona *arizona, int irq, int on)
68{
69 irq = arizona_map_irq(arizona, irq);
70 if (irq < 0)
71 return irq;
72
73 return irq_set_irq_wake(irq, on);
74}
75EXPORT_SYMBOL_GPL(arizona_set_irq_wake);
76
77static irqreturn_t arizona_boot_done(int irq, void *data)
78{
79 struct arizona *arizona = data;
80
81 dev_dbg(arizona->dev, "Boot done\n");
82
83 return IRQ_HANDLED;
84}
85
86static irqreturn_t arizona_ctrlif_err(int irq, void *data)
87{
88 struct arizona *arizona = data;
89
90 /*
91 * For pretty much all potential sources a register cache sync
92 * won't help, we've just got a software bug somewhere.
93 */
94 dev_err(arizona->dev, "Control interface error\n");
95
96 return IRQ_HANDLED;
97}
98
99static irqreturn_t arizona_irq_thread(int irq, void *data)
100{
101 struct arizona *arizona = data;
Mark Brown3092f802013-03-24 23:05:58 +0000102 bool poll;
Mark Brown3080de42012-08-21 20:02:03 +0100103 unsigned int val;
Mark Browncdabc1c2012-09-08 09:00:59 +0800104 int ret;
Mark Brown966cdc92012-06-19 16:34:23 +0100105
106 ret = pm_runtime_get_sync(arizona->dev);
107 if (ret < 0) {
108 dev_err(arizona->dev, "Failed to resume device: %d\n", ret);
109 return IRQ_NONE;
110 }
111
Mark Brown3092f802013-03-24 23:05:58 +0000112 do {
113 poll = false;
Mark Brown3080de42012-08-21 20:02:03 +0100114
Richard Fitzgerald1f2c3972016-06-15 10:28:44 +0100115 if (arizona->aod_irq_chip) {
116 /*
117 * Check the AOD status register to determine whether
118 * the nested IRQ handler should be called.
119 */
120 ret = regmap_read(arizona->regmap,
121 ARIZONA_AOD_IRQ1, &val);
122 if (ret)
123 dev_warn(arizona->dev,
124 "Failed to read AOD IRQ1 %d\n", ret);
125 else if (val)
126 handle_nested_irq(
127 irq_find_mapping(arizona->virq, 0));
128 }
Mark Brown3092f802013-03-24 23:05:58 +0000129
130 /*
131 * Check if one of the main interrupts is asserted and only
132 * check that domain if it is.
133 */
134 ret = regmap_read(arizona->regmap, ARIZONA_IRQ_PIN_STATUS,
135 &val);
136 if (ret == 0 && val & ARIZONA_IRQ1_STS) {
137 handle_nested_irq(irq_find_mapping(arizona->virq, 1));
138 } else if (ret != 0) {
139 dev_err(arizona->dev,
140 "Failed to read main IRQ status: %d\n", ret);
141 }
142
143 /*
144 * Poll the IRQ pin status to see if we're really done
145 * if the interrupt controller can't do it for us.
146 */
147 if (!arizona->pdata.irq_gpio) {
148 break;
149 } else if (arizona->pdata.irq_flags & IRQF_TRIGGER_RISING &&
150 gpio_get_value_cansleep(arizona->pdata.irq_gpio)) {
151 poll = true;
152 } else if (arizona->pdata.irq_flags & IRQF_TRIGGER_FALLING &&
153 !gpio_get_value_cansleep(arizona->pdata.irq_gpio)) {
154 poll = true;
155 }
156 } while (poll);
Mark Brown966cdc92012-06-19 16:34:23 +0100157
158 pm_runtime_mark_last_busy(arizona->dev);
159 pm_runtime_put_autosuspend(arizona->dev);
160
161 return IRQ_HANDLED;
162}
163
164static void arizona_irq_enable(struct irq_data *data)
165{
166}
167
168static void arizona_irq_disable(struct irq_data *data)
169{
170}
171
Charles Keepaxc38715f2014-09-01 15:29:11 +0100172static int arizona_irq_set_wake(struct irq_data *data, unsigned int on)
173{
174 struct arizona *arizona = irq_data_get_irq_chip_data(data);
175
176 return irq_set_irq_wake(arizona->irq, on);
177}
178
Mark Brown966cdc92012-06-19 16:34:23 +0100179static struct irq_chip arizona_irq_chip = {
180 .name = "arizona",
181 .irq_disable = arizona_irq_disable,
182 .irq_enable = arizona_irq_enable,
Charles Keepaxc38715f2014-09-01 15:29:11 +0100183 .irq_set_wake = arizona_irq_set_wake,
Mark Brown966cdc92012-06-19 16:34:23 +0100184};
185
Charles Keepaxdedf24a2016-03-25 14:27:09 +0000186static struct lock_class_key arizona_irq_lock_class;
187
Mark Brown966cdc92012-06-19 16:34:23 +0100188static int arizona_irq_map(struct irq_domain *h, unsigned int virq,
189 irq_hw_number_t hw)
190{
Charles Keepax0a464df2015-09-11 16:07:56 +0100191 struct arizona *data = h->host_data;
Mark Brown966cdc92012-06-19 16:34:23 +0100192
193 irq_set_chip_data(virq, data);
Charles Keepaxdedf24a2016-03-25 14:27:09 +0000194 irq_set_lockdep_class(virq, &arizona_irq_lock_class);
Charles Keepaxcfeb35d2014-09-09 17:00:09 +0100195 irq_set_chip_and_handler(virq, &arizona_irq_chip, handle_simple_irq);
Mark Brown966cdc92012-06-19 16:34:23 +0100196 irq_set_nested_thread(virq, 1);
Mark Brown966cdc92012-06-19 16:34:23 +0100197 irq_set_noprobe(virq);
Mark Brown966cdc92012-06-19 16:34:23 +0100198
199 return 0;
200}
201
Krzysztof Kozlowski7ce7b262015-04-27 21:54:13 +0900202static const struct irq_domain_ops arizona_domain_ops = {
Mark Brown966cdc92012-06-19 16:34:23 +0100203 .map = arizona_irq_map,
204 .xlate = irq_domain_xlate_twocell,
205};
206
207int arizona_irq_init(struct arizona *arizona)
208{
209 int flags = IRQF_ONESHOT;
Charles Keepax003db342016-11-22 16:10:26 +0000210 int ret;
Mark Brown966cdc92012-06-19 16:34:23 +0100211 const struct regmap_irq_chip *aod, *irq;
Mark Brown22c75fe2013-03-24 23:16:56 +0000212 struct irq_data *irq_data;
Charles Keepax3dfaff22016-11-22 16:10:28 +0000213 unsigned int virq;
Mark Brown966cdc92012-06-19 16:34:23 +0100214
Charles Keepax30a2af32014-07-15 11:21:50 +0100215 arizona->ctrlif_error = true;
216
Mark Brown966cdc92012-06-19 16:34:23 +0100217 switch (arizona->type) {
Mark Brown863df8d2012-07-05 20:35:31 +0100218#ifdef CONFIG_MFD_WM5102
Mark Brown966cdc92012-06-19 16:34:23 +0100219 case WM5102:
220 aod = &wm5102_aod;
221 irq = &wm5102_irq;
Mark Brown92d80132012-08-07 19:57:53 +0100222
Charles Keepax30a2af32014-07-15 11:21:50 +0100223 arizona->ctrlif_error = false;
Mark Brown966cdc92012-06-19 16:34:23 +0100224 break;
Mark Brown863df8d2012-07-05 20:35:31 +0100225#endif
Mark Browne102bef2012-07-10 12:37:58 +0100226#ifdef CONFIG_MFD_WM5110
227 case WM5110:
Richard Fitzgeralde5d4ef02015-01-17 15:21:22 +0000228 case WM8280:
Mark Browne102bef2012-07-10 12:37:58 +0100229 aod = &wm5110_aod;
Charles Keepax32155012014-07-15 11:21:48 +0100230
231 switch (arizona->rev) {
232 case 0 ... 2:
233 irq = &wm5110_irq;
234 break;
235 default:
236 irq = &wm5110_revd_irq;
237 break;
238 }
Mark Brown92d80132012-08-07 19:57:53 +0100239
Charles Keepax30a2af32014-07-15 11:21:50 +0100240 arizona->ctrlif_error = false;
Mark Browne102bef2012-07-10 12:37:58 +0100241 break;
242#endif
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000243#ifdef CONFIG_MFD_CS47L24
244 case WM1831:
245 case CS47L24:
246 aod = NULL;
247 irq = &cs47l24_irq;
248
249 arizona->ctrlif_error = false;
250 break;
251#endif
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100252#ifdef CONFIG_MFD_WM8997
253 case WM8997:
254 aod = &wm8997_aod;
255 irq = &wm8997_irq;
256
Charles Keepax30a2af32014-07-15 11:21:50 +0100257 arizona->ctrlif_error = false;
Charles Keepaxdc7d4862013-06-13 09:43:29 +0100258 break;
259#endif
Richard Fitzgerald6887b042015-07-03 16:16:35 +0100260#ifdef CONFIG_MFD_WM8998
261 case WM8998:
262 case WM1814:
263 aod = &wm8998_aod;
264 irq = &wm8998_irq;
265
266 arizona->ctrlif_error = false;
267 break;
268#endif
Mark Brown966cdc92012-06-19 16:34:23 +0100269 default:
270 BUG_ON("Unknown Arizona class device" == NULL);
271 return -EINVAL;
272 }
273
Mark Brown1816cb32013-01-17 16:54:18 +0900274 /* Disable all wake sources by default */
275 regmap_write(arizona->regmap, ARIZONA_WAKE_CONTROL, 0);
276
Mark Brown22c75fe2013-03-24 23:16:56 +0000277 /* Read the flags from the interrupt controller if not specified */
278 if (!arizona->pdata.irq_flags) {
279 irq_data = irq_get_irq_data(arizona->irq);
280 if (!irq_data) {
281 dev_err(arizona->dev, "Invalid IRQ: %d\n",
282 arizona->irq);
283 return -EINVAL;
284 }
285
286 arizona->pdata.irq_flags = irqd_get_trigger_type(irq_data);
287 switch (arizona->pdata.irq_flags) {
288 case IRQF_TRIGGER_LOW:
289 case IRQF_TRIGGER_HIGH:
290 case IRQF_TRIGGER_RISING:
291 case IRQF_TRIGGER_FALLING:
292 break;
293
294 case IRQ_TYPE_NONE:
295 default:
296 /* Device default */
297 arizona->pdata.irq_flags = IRQF_TRIGGER_LOW;
298 break;
299 }
300 }
Mark Brownf8a09412013-03-22 12:59:33 +0100301
302 if (arizona->pdata.irq_flags & (IRQF_TRIGGER_HIGH |
303 IRQF_TRIGGER_RISING)) {
Mark Brown966cdc92012-06-19 16:34:23 +0100304 ret = regmap_update_bits(arizona->regmap, ARIZONA_IRQ_CTRL_1,
305 ARIZONA_IRQ_POL, 0);
306 if (ret != 0) {
307 dev_err(arizona->dev, "Couldn't set IRQ polarity: %d\n",
308 ret);
309 goto err;
310 }
Mark Brown966cdc92012-06-19 16:34:23 +0100311 }
312
Mark Brownf8a09412013-03-22 12:59:33 +0100313 flags |= arizona->pdata.irq_flags;
314
Mark Brown966cdc92012-06-19 16:34:23 +0100315 /* Allocate a virtual IRQ domain to distribute to the regmap domains */
316 arizona->virq = irq_domain_add_linear(NULL, 2, &arizona_domain_ops,
317 arizona);
318 if (!arizona->virq) {
Mark Brownb7dea5d2012-12-05 11:46:26 +0900319 dev_err(arizona->dev, "Failed to add core IRQ domain\n");
Mark Brown966cdc92012-06-19 16:34:23 +0100320 ret = -EINVAL;
321 goto err;
322 }
323
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000324 if (aod) {
Charles Keepax3dfaff22016-11-22 16:10:28 +0000325 virq = irq_create_mapping(arizona->virq, ARIZONA_AOD_IRQ_INDEX);
326 if (!virq) {
327 dev_err(arizona->dev, "Failed to map AOD IRQs\n");
328 ret = -EINVAL;
329 goto err_domain;
330 }
331
332 ret = regmap_add_irq_chip(arizona->regmap, virq, IRQF_ONESHOT,
333 0, aod, &arizona->aod_irq_chip);
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000334 if (ret != 0) {
335 dev_err(arizona->dev,
336 "Failed to add AOD IRQs: %d\n", ret);
Charles Keepax3dfaff22016-11-22 16:10:28 +0000337 goto err_map_aod;
Richard Fitzgeraldea1f3332015-11-03 15:08:32 +0000338 }
Mark Brown966cdc92012-06-19 16:34:23 +0100339 }
340
Charles Keepax3dfaff22016-11-22 16:10:28 +0000341 virq = irq_create_mapping(arizona->virq, ARIZONA_MAIN_IRQ_INDEX);
342 if (!virq) {
343 dev_err(arizona->dev, "Failed to map main IRQs\n");
344 ret = -EINVAL;
345 goto err_aod;
346 }
347
348 ret = regmap_add_irq_chip(arizona->regmap, virq, IRQF_ONESHOT,
349 0, irq, &arizona->irq_chip);
Mark Brown966cdc92012-06-19 16:34:23 +0100350 if (ret != 0) {
Charles Keepaxd1cb4cc2014-05-19 17:35:29 +0100351 dev_err(arizona->dev, "Failed to add main IRQs: %d\n", ret);
Charles Keepax3dfaff22016-11-22 16:10:28 +0000352 goto err_map_main_irq;
Mark Brown966cdc92012-06-19 16:34:23 +0100353 }
354
Mark Brown3092f802013-03-24 23:05:58 +0000355 /* Used to emulate edge trigger and to work around broken pinmux */
356 if (arizona->pdata.irq_gpio) {
357 if (gpio_to_irq(arizona->pdata.irq_gpio) != arizona->irq) {
358 dev_warn(arizona->dev, "IRQ %d is not GPIO %d (%d)\n",
359 arizona->irq, arizona->pdata.irq_gpio,
360 gpio_to_irq(arizona->pdata.irq_gpio));
361 arizona->irq = gpio_to_irq(arizona->pdata.irq_gpio);
362 }
363
364 ret = devm_gpio_request_one(arizona->dev,
365 arizona->pdata.irq_gpio,
366 GPIOF_IN, "arizona IRQ");
367 if (ret != 0) {
368 dev_err(arizona->dev,
369 "Failed to request IRQ GPIO %d:: %d\n",
370 arizona->pdata.irq_gpio, ret);
371 arizona->pdata.irq_gpio = 0;
372 }
373 }
374
Mark Brown966cdc92012-06-19 16:34:23 +0100375 ret = request_threaded_irq(arizona->irq, NULL, arizona_irq_thread,
376 flags, "arizona", arizona);
377
378 if (ret != 0) {
Mark Brown7994c662013-03-19 09:51:14 +0000379 dev_err(arizona->dev, "Failed to request primary IRQ %d: %d\n",
Mark Brown966cdc92012-06-19 16:34:23 +0100380 arizona->irq, ret);
381 goto err_main_irq;
382 }
383
Charles Keepax6c006b12015-12-16 13:53:59 +0000384 /* Make sure the boot done IRQ is unmasked for resumes */
Charles Keepax003db342016-11-22 16:10:26 +0000385 ret = arizona_request_irq(arizona, ARIZONA_IRQ_BOOT_DONE, "Boot done",
386 arizona_boot_done, arizona);
Charles Keepax6c006b12015-12-16 13:53:59 +0000387 if (ret != 0) {
388 dev_err(arizona->dev, "Failed to request boot done %d: %d\n",
389 arizona->irq, ret);
390 goto err_boot_done;
391 }
392
393 /* Handle control interface errors in the core */
394 if (arizona->ctrlif_error) {
Charles Keepax003db342016-11-22 16:10:26 +0000395 ret = arizona_request_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR,
396 "Control interface error",
397 arizona_ctrlif_err, arizona);
Charles Keepax6c006b12015-12-16 13:53:59 +0000398 if (ret != 0) {
399 dev_err(arizona->dev,
400 "Failed to request CTRLIF_ERR %d: %d\n",
401 arizona->irq, ret);
402 goto err_ctrlif;
403 }
404 }
405
Mark Brown966cdc92012-06-19 16:34:23 +0100406 return 0;
407
Mark Brown966cdc92012-06-19 16:34:23 +0100408err_ctrlif:
Charles Keepax003db342016-11-22 16:10:26 +0000409 arizona_free_irq(arizona, ARIZONA_IRQ_BOOT_DONE, arizona);
Mark Brown966cdc92012-06-19 16:34:23 +0100410err_boot_done:
Charles Keepax6c006b12015-12-16 13:53:59 +0000411 free_irq(arizona->irq, arizona);
412err_main_irq:
Charles Keepax1a86dcb2016-11-22 16:10:27 +0000413 regmap_del_irq_chip(irq_find_mapping(arizona->virq,
414 ARIZONA_MAIN_IRQ_INDEX),
Mark Brown966cdc92012-06-19 16:34:23 +0100415 arizona->irq_chip);
Charles Keepax3dfaff22016-11-22 16:10:28 +0000416err_map_main_irq:
417 irq_dispose_mapping(irq_find_mapping(arizona->virq,
418 ARIZONA_MAIN_IRQ_INDEX));
Mark Brown966cdc92012-06-19 16:34:23 +0100419err_aod:
Charles Keepax1a86dcb2016-11-22 16:10:27 +0000420 regmap_del_irq_chip(irq_find_mapping(arizona->virq,
421 ARIZONA_AOD_IRQ_INDEX),
Mark Brown966cdc92012-06-19 16:34:23 +0100422 arizona->aod_irq_chip);
Charles Keepax3dfaff22016-11-22 16:10:28 +0000423err_map_aod:
424 irq_dispose_mapping(irq_find_mapping(arizona->virq,
425 ARIZONA_AOD_IRQ_INDEX));
426err_domain:
427 irq_domain_remove(arizona->virq);
Mark Brown966cdc92012-06-19 16:34:23 +0100428err:
429 return ret;
430}
431
432int arizona_irq_exit(struct arizona *arizona)
433{
Charles Keepax3dfaff22016-11-22 16:10:28 +0000434 unsigned int virq;
435
Charles Keepax30a2af32014-07-15 11:21:50 +0100436 if (arizona->ctrlif_error)
Charles Keepax003db342016-11-22 16:10:26 +0000437 arizona_free_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR, arizona);
438 arizona_free_irq(arizona, ARIZONA_IRQ_BOOT_DONE, arizona);
439
Charles Keepax3dfaff22016-11-22 16:10:28 +0000440 virq = irq_find_mapping(arizona->virq, ARIZONA_MAIN_IRQ_INDEX);
441 regmap_del_irq_chip(virq, arizona->irq_chip);
442 irq_dispose_mapping(virq);
443
444 virq = irq_find_mapping(arizona->virq, ARIZONA_AOD_IRQ_INDEX);
445 regmap_del_irq_chip(virq, arizona->aod_irq_chip);
446 irq_dispose_mapping(virq);
447
448 irq_domain_remove(arizona->virq);
449
Mark Brown966cdc92012-06-19 16:34:23 +0100450 free_irq(arizona->irq, arizona);
451
452 return 0;
453}