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 ret = regmap_irq_get_virq(arizona->aod_irq_chip, irq);
35 ret = regmap_irq_get_virq(arizona->irq_chip, irq);
40 int arizona_request_irq(struct arizona *arizona, int irq, char *name,
41 irq_handler_t handler, void *data)
43 irq = arizona_map_irq(arizona, irq);
47 return request_threaded_irq(irq, NULL, handler, IRQF_ONESHOT,
50 EXPORT_SYMBOL_GPL(arizona_request_irq);
52 void arizona_free_irq(struct arizona *arizona, int irq, void *data)
54 irq = arizona_map_irq(arizona, irq);
60 EXPORT_SYMBOL_GPL(arizona_free_irq);
62 int arizona_set_irq_wake(struct arizona *arizona, int irq, int on)
64 irq = arizona_map_irq(arizona, irq);
68 return irq_set_irq_wake(irq, on);
70 EXPORT_SYMBOL_GPL(arizona_set_irq_wake);
72 static irqreturn_t arizona_boot_done(int irq, void *data)
74 struct arizona *arizona = data;
76 dev_dbg(arizona->dev, "Boot done\n");
81 static irqreturn_t arizona_ctrlif_err(int irq, void *data)
83 struct arizona *arizona = data;
86 * For pretty much all potential sources a register cache sync
87 * won't help, we've just got a software bug somewhere.
89 dev_err(arizona->dev, "Control interface error\n");
94 static irqreturn_t arizona_irq_thread(int irq, void *data)
96 struct arizona *arizona = data;
100 ret = pm_runtime_get_sync(arizona->dev);
102 dev_err(arizona->dev, "Failed to resume device: %d\n", ret);
106 /* Always handle the AoD domain */
107 handle_nested_irq(irq_find_mapping(arizona->virq, 0));
110 * Check if one of the main interrupts is asserted and only
111 * check that domain if it is.
113 ret = regmap_read(arizona->regmap, ARIZONA_IRQ_PIN_STATUS, &val);
114 if (ret == 0 && val & ARIZONA_IRQ1_STS) {
115 handle_nested_irq(irq_find_mapping(arizona->virq, 1));
116 } else if (ret != 0) {
117 dev_err(arizona->dev, "Failed to read main IRQ status: %d\n",
121 pm_runtime_mark_last_busy(arizona->dev);
122 pm_runtime_put_autosuspend(arizona->dev);
127 static void arizona_irq_enable(struct irq_data *data)
131 static void arizona_irq_disable(struct irq_data *data)
135 static struct irq_chip arizona_irq_chip = {
137 .irq_disable = arizona_irq_disable,
138 .irq_enable = arizona_irq_enable,
141 static int arizona_irq_map(struct irq_domain *h, unsigned int virq,
144 struct regmap_irq_chip_data *data = h->host_data;
146 irq_set_chip_data(virq, data);
147 irq_set_chip_and_handler(virq, &arizona_irq_chip, handle_edge_irq);
148 irq_set_nested_thread(virq, 1);
150 /* ARM needs us to explicitly flag the IRQ as valid
151 * and will set them noprobe when we do so. */
153 set_irq_flags(virq, IRQF_VALID);
155 irq_set_noprobe(virq);
161 static struct irq_domain_ops arizona_domain_ops = {
162 .map = arizona_irq_map,
163 .xlate = irq_domain_xlate_twocell,
166 int arizona_irq_init(struct arizona *arizona)
168 int flags = IRQF_ONESHOT;
170 const struct regmap_irq_chip *aod, *irq;
171 bool ctrlif_error = true;
173 switch (arizona->type) {
174 #ifdef CONFIG_MFD_WM5102
179 ctrlif_error = false;
182 #ifdef CONFIG_MFD_WM5110
187 ctrlif_error = false;
191 BUG_ON("Unknown Arizona class device" == NULL);
195 if (arizona->pdata.irq_active_high) {
196 ret = regmap_update_bits(arizona->regmap, ARIZONA_IRQ_CTRL_1,
199 dev_err(arizona->dev, "Couldn't set IRQ polarity: %d\n",
204 flags |= IRQF_TRIGGER_HIGH;
206 flags |= IRQF_TRIGGER_LOW;
209 /* Allocate a virtual IRQ domain to distribute to the regmap domains */
210 arizona->virq = irq_domain_add_linear(NULL, 2, &arizona_domain_ops,
212 if (!arizona->virq) {
213 dev_err(arizona->dev, "Failed to add core IRQ domain\n");
218 ret = regmap_add_irq_chip(arizona->regmap,
219 irq_create_mapping(arizona->virq, 0),
220 IRQF_ONESHOT, -1, aod,
221 &arizona->aod_irq_chip);
223 dev_err(arizona->dev, "Failed to add AOD IRQs: %d\n", ret);
227 ret = regmap_add_irq_chip(arizona->regmap,
228 irq_create_mapping(arizona->virq, 1),
229 IRQF_ONESHOT, -1, irq,
232 dev_err(arizona->dev, "Failed to add AOD IRQs: %d\n", ret);
236 /* Make sure the boot done IRQ is unmasked for resumes */
237 i = arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE);
238 ret = request_threaded_irq(i, NULL, arizona_boot_done, IRQF_ONESHOT,
239 "Boot done", arizona);
241 dev_err(arizona->dev, "Failed to request boot done %d: %d\n",
246 /* Handle control interface errors in the core */
248 i = arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR);
249 ret = request_threaded_irq(i, NULL, arizona_ctrlif_err,
251 "Control interface error", arizona);
253 dev_err(arizona->dev,
254 "Failed to request CTRLIF_ERR %d: %d\n",
260 ret = request_threaded_irq(arizona->irq, NULL, arizona_irq_thread,
261 flags, "arizona", arizona);
264 dev_err(arizona->dev, "Failed to request IRQ %d: %d\n",
272 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR), arizona);
274 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona);
276 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1),
279 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0),
280 arizona->aod_irq_chip);
286 int arizona_irq_exit(struct arizona *arizona)
288 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR), arizona);
289 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona);
290 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1),
292 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0),
293 arizona->aod_irq_chip);
294 free_irq(arizona->irq, arizona);