2 * Arizona interrupt support
4 * Copyright 2012 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
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>
24 #include <linux/mfd/arizona/core.h>
25 #include <linux/mfd/arizona/registers.h>
29 static int arizona_map_irq(struct arizona
*arizona
, int irq
)
33 if (arizona
->aod_irq_chip
) {
34 ret
= regmap_irq_get_virq(arizona
->aod_irq_chip
, irq
);
39 return regmap_irq_get_virq(arizona
->irq_chip
, irq
);
42 int arizona_request_irq(struct arizona
*arizona
, int irq
, char *name
,
43 irq_handler_t handler
, void *data
)
45 irq
= arizona_map_irq(arizona
, irq
);
49 return request_threaded_irq(irq
, NULL
, handler
, IRQF_ONESHOT
,
52 EXPORT_SYMBOL_GPL(arizona_request_irq
);
54 void arizona_free_irq(struct arizona
*arizona
, int irq
, void *data
)
56 irq
= arizona_map_irq(arizona
, irq
);
62 EXPORT_SYMBOL_GPL(arizona_free_irq
);
64 int arizona_set_irq_wake(struct arizona
*arizona
, int irq
, int on
)
66 irq
= arizona_map_irq(arizona
, irq
);
70 return irq_set_irq_wake(irq
, on
);
72 EXPORT_SYMBOL_GPL(arizona_set_irq_wake
);
74 static irqreturn_t
arizona_boot_done(int irq
, void *data
)
76 struct arizona
*arizona
= data
;
78 dev_dbg(arizona
->dev
, "Boot done\n");
83 static irqreturn_t
arizona_ctrlif_err(int irq
, void *data
)
85 struct arizona
*arizona
= data
;
88 * For pretty much all potential sources a register cache sync
89 * won't help, we've just got a software bug somewhere.
91 dev_err(arizona
->dev
, "Control interface error\n");
96 static irqreturn_t
arizona_irq_thread(int irq
, void *data
)
98 struct arizona
*arizona
= data
;
103 ret
= pm_runtime_get_sync(arizona
->dev
);
105 dev_err(arizona
->dev
, "Failed to resume device: %d\n", ret
);
112 if (arizona
->aod_irq_chip
)
113 handle_nested_irq(irq_find_mapping(arizona
->virq
, 0));
116 * Check if one of the main interrupts is asserted and only
117 * check that domain if it is.
119 ret
= regmap_read(arizona
->regmap
, ARIZONA_IRQ_PIN_STATUS
,
121 if (ret
== 0 && val
& ARIZONA_IRQ1_STS
) {
122 handle_nested_irq(irq_find_mapping(arizona
->virq
, 1));
123 } else if (ret
!= 0) {
124 dev_err(arizona
->dev
,
125 "Failed to read main IRQ status: %d\n", ret
);
129 * Poll the IRQ pin status to see if we're really done
130 * if the interrupt controller can't do it for us.
132 if (!arizona
->pdata
.irq_gpio
) {
134 } else if (arizona
->pdata
.irq_flags
& IRQF_TRIGGER_RISING
&&
135 gpio_get_value_cansleep(arizona
->pdata
.irq_gpio
)) {
137 } else if (arizona
->pdata
.irq_flags
& IRQF_TRIGGER_FALLING
&&
138 !gpio_get_value_cansleep(arizona
->pdata
.irq_gpio
)) {
143 pm_runtime_mark_last_busy(arizona
->dev
);
144 pm_runtime_put_autosuspend(arizona
->dev
);
149 static void arizona_irq_enable(struct irq_data
*data
)
153 static void arizona_irq_disable(struct irq_data
*data
)
157 static int arizona_irq_set_wake(struct irq_data
*data
, unsigned int on
)
159 struct arizona
*arizona
= irq_data_get_irq_chip_data(data
);
161 return irq_set_irq_wake(arizona
->irq
, on
);
164 static struct irq_chip arizona_irq_chip
= {
166 .irq_disable
= arizona_irq_disable
,
167 .irq_enable
= arizona_irq_enable
,
168 .irq_set_wake
= arizona_irq_set_wake
,
171 static struct lock_class_key arizona_irq_lock_class
;
173 static int arizona_irq_map(struct irq_domain
*h
, unsigned int virq
,
176 struct arizona
*data
= h
->host_data
;
178 irq_set_chip_data(virq
, data
);
179 irq_set_lockdep_class(virq
, &arizona_irq_lock_class
);
180 irq_set_chip_and_handler(virq
, &arizona_irq_chip
, handle_simple_irq
);
181 irq_set_nested_thread(virq
, 1);
182 irq_set_noprobe(virq
);
187 static const struct irq_domain_ops arizona_domain_ops
= {
188 .map
= arizona_irq_map
,
189 .xlate
= irq_domain_xlate_twocell
,
192 int arizona_irq_init(struct arizona
*arizona
)
194 int flags
= IRQF_ONESHOT
;
196 const struct regmap_irq_chip
*aod
, *irq
;
197 struct irq_data
*irq_data
;
199 arizona
->ctrlif_error
= true;
201 switch (arizona
->type
) {
202 #ifdef CONFIG_MFD_WM5102
207 arizona
->ctrlif_error
= false;
210 #ifdef CONFIG_MFD_WM5110
215 switch (arizona
->rev
) {
220 irq
= &wm5110_revd_irq
;
224 arizona
->ctrlif_error
= false;
227 #ifdef CONFIG_MFD_CS47L24
233 arizona
->ctrlif_error
= false;
236 #ifdef CONFIG_MFD_WM8997
241 arizona
->ctrlif_error
= false;
244 #ifdef CONFIG_MFD_WM8998
250 arizona
->ctrlif_error
= false;
254 BUG_ON("Unknown Arizona class device" == NULL
);
258 /* Disable all wake sources by default */
259 regmap_write(arizona
->regmap
, ARIZONA_WAKE_CONTROL
, 0);
261 /* Read the flags from the interrupt controller if not specified */
262 if (!arizona
->pdata
.irq_flags
) {
263 irq_data
= irq_get_irq_data(arizona
->irq
);
265 dev_err(arizona
->dev
, "Invalid IRQ: %d\n",
270 arizona
->pdata
.irq_flags
= irqd_get_trigger_type(irq_data
);
271 switch (arizona
->pdata
.irq_flags
) {
272 case IRQF_TRIGGER_LOW
:
273 case IRQF_TRIGGER_HIGH
:
274 case IRQF_TRIGGER_RISING
:
275 case IRQF_TRIGGER_FALLING
:
281 arizona
->pdata
.irq_flags
= IRQF_TRIGGER_LOW
;
286 if (arizona
->pdata
.irq_flags
& (IRQF_TRIGGER_HIGH
|
287 IRQF_TRIGGER_RISING
)) {
288 ret
= regmap_update_bits(arizona
->regmap
, ARIZONA_IRQ_CTRL_1
,
291 dev_err(arizona
->dev
, "Couldn't set IRQ polarity: %d\n",
297 flags
|= arizona
->pdata
.irq_flags
;
299 /* Allocate a virtual IRQ domain to distribute to the regmap domains */
300 arizona
->virq
= irq_domain_add_linear(NULL
, 2, &arizona_domain_ops
,
302 if (!arizona
->virq
) {
303 dev_err(arizona
->dev
, "Failed to add core IRQ domain\n");
309 ret
= regmap_add_irq_chip(arizona
->regmap
,
310 irq_create_mapping(arizona
->virq
, 0),
311 IRQF_ONESHOT
, 0, aod
,
312 &arizona
->aod_irq_chip
);
314 dev_err(arizona
->dev
,
315 "Failed to add AOD IRQs: %d\n", ret
);
320 ret
= regmap_add_irq_chip(arizona
->regmap
,
321 irq_create_mapping(arizona
->virq
, 1),
322 IRQF_ONESHOT
, 0, irq
,
325 dev_err(arizona
->dev
, "Failed to add main IRQs: %d\n", ret
);
329 /* Used to emulate edge trigger and to work around broken pinmux */
330 if (arizona
->pdata
.irq_gpio
) {
331 if (gpio_to_irq(arizona
->pdata
.irq_gpio
) != arizona
->irq
) {
332 dev_warn(arizona
->dev
, "IRQ %d is not GPIO %d (%d)\n",
333 arizona
->irq
, arizona
->pdata
.irq_gpio
,
334 gpio_to_irq(arizona
->pdata
.irq_gpio
));
335 arizona
->irq
= gpio_to_irq(arizona
->pdata
.irq_gpio
);
338 ret
= devm_gpio_request_one(arizona
->dev
,
339 arizona
->pdata
.irq_gpio
,
340 GPIOF_IN
, "arizona IRQ");
342 dev_err(arizona
->dev
,
343 "Failed to request IRQ GPIO %d:: %d\n",
344 arizona
->pdata
.irq_gpio
, ret
);
345 arizona
->pdata
.irq_gpio
= 0;
349 ret
= request_threaded_irq(arizona
->irq
, NULL
, arizona_irq_thread
,
350 flags
, "arizona", arizona
);
353 dev_err(arizona
->dev
, "Failed to request primary IRQ %d: %d\n",
358 /* Make sure the boot done IRQ is unmasked for resumes */
359 i
= arizona_map_irq(arizona
, ARIZONA_IRQ_BOOT_DONE
);
360 ret
= request_threaded_irq(i
, NULL
, arizona_boot_done
, IRQF_ONESHOT
,
361 "Boot done", arizona
);
363 dev_err(arizona
->dev
, "Failed to request boot done %d: %d\n",
368 /* Handle control interface errors in the core */
369 if (arizona
->ctrlif_error
) {
370 i
= arizona_map_irq(arizona
, ARIZONA_IRQ_CTRLIF_ERR
);
371 ret
= request_threaded_irq(i
, NULL
, arizona_ctrlif_err
,
373 "Control interface error", arizona
);
375 dev_err(arizona
->dev
,
376 "Failed to request CTRLIF_ERR %d: %d\n",
385 free_irq(arizona_map_irq(arizona
, ARIZONA_IRQ_BOOT_DONE
), arizona
);
387 free_irq(arizona
->irq
, arizona
);
389 regmap_del_irq_chip(irq_create_mapping(arizona
->virq
, 1),
392 regmap_del_irq_chip(irq_create_mapping(arizona
->virq
, 0),
393 arizona
->aod_irq_chip
);
398 int arizona_irq_exit(struct arizona
*arizona
)
400 if (arizona
->ctrlif_error
)
401 free_irq(arizona_map_irq(arizona
, ARIZONA_IRQ_CTRLIF_ERR
),
403 free_irq(arizona_map_irq(arizona
, ARIZONA_IRQ_BOOT_DONE
), arizona
);
404 regmap_del_irq_chip(irq_create_mapping(arizona
->virq
, 1),
406 regmap_del_irq_chip(irq_create_mapping(arizona
->virq
, 0),
407 arizona
->aod_irq_chip
);
408 free_irq(arizona
->irq
, arizona
);