blob: 64940c6da93c031b7fb5e8dad88366b79c1eaa57 [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;
97 int i, ret;
98
99 ret = pm_runtime_get_sync(arizona->dev);
100 if (ret < 0) {
101 dev_err(arizona->dev, "Failed to resume device: %d\n", ret);
102 return IRQ_NONE;
103 }
104
105 /* Check both domains */
106 for (i = 0; i < 2; i++)
107 handle_nested_irq(irq_find_mapping(arizona->virq, i));
108
109 pm_runtime_mark_last_busy(arizona->dev);
110 pm_runtime_put_autosuspend(arizona->dev);
111
112 return IRQ_HANDLED;
113}
114
115static void arizona_irq_enable(struct irq_data *data)
116{
117}
118
119static void arizona_irq_disable(struct irq_data *data)
120{
121}
122
123static struct irq_chip arizona_irq_chip = {
124 .name = "arizona",
125 .irq_disable = arizona_irq_disable,
126 .irq_enable = arizona_irq_enable,
127};
128
129static int arizona_irq_map(struct irq_domain *h, unsigned int virq,
130 irq_hw_number_t hw)
131{
132 struct regmap_irq_chip_data *data = h->host_data;
133
134 irq_set_chip_data(virq, data);
135 irq_set_chip_and_handler(virq, &arizona_irq_chip, handle_edge_irq);
136 irq_set_nested_thread(virq, 1);
137
138 /* ARM needs us to explicitly flag the IRQ as valid
139 * and will set them noprobe when we do so. */
140#ifdef CONFIG_ARM
141 set_irq_flags(virq, IRQF_VALID);
142#else
143 irq_set_noprobe(virq);
144#endif
145
146 return 0;
147}
148
149static struct irq_domain_ops arizona_domain_ops = {
150 .map = arizona_irq_map,
151 .xlate = irq_domain_xlate_twocell,
152};
153
154int arizona_irq_init(struct arizona *arizona)
155{
156 int flags = IRQF_ONESHOT;
157 int ret, i;
158 const struct regmap_irq_chip *aod, *irq;
Mark Brown92d80132012-08-07 19:57:53 +0100159 bool ctrlif_error = true;
Mark Brown966cdc92012-06-19 16:34:23 +0100160
161 switch (arizona->type) {
Mark Brown863df8d2012-07-05 20:35:31 +0100162#ifdef CONFIG_MFD_WM5102
Mark Brown966cdc92012-06-19 16:34:23 +0100163 case WM5102:
164 aod = &wm5102_aod;
165 irq = &wm5102_irq;
Mark Brown92d80132012-08-07 19:57:53 +0100166
167 switch (arizona->rev) {
168 case 0:
169 ctrlif_error = false;
170 break;
171 default:
172 break;
173 }
Mark Brown966cdc92012-06-19 16:34:23 +0100174 break;
Mark Brown863df8d2012-07-05 20:35:31 +0100175#endif
Mark Browne102bef2012-07-10 12:37:58 +0100176#ifdef CONFIG_MFD_WM5110
177 case WM5110:
178 aod = &wm5110_aod;
179 irq = &wm5110_irq;
Mark Brown92d80132012-08-07 19:57:53 +0100180
181 switch (arizona->rev) {
182 case 0:
183 ctrlif_error = false;
184 break;
185 default:
186 break;
187 }
Mark Browne102bef2012-07-10 12:37:58 +0100188 break;
189#endif
Mark Brown966cdc92012-06-19 16:34:23 +0100190 default:
191 BUG_ON("Unknown Arizona class device" == NULL);
192 return -EINVAL;
193 }
194
195 if (arizona->pdata.irq_active_high) {
196 ret = regmap_update_bits(arizona->regmap, ARIZONA_IRQ_CTRL_1,
197 ARIZONA_IRQ_POL, 0);
198 if (ret != 0) {
199 dev_err(arizona->dev, "Couldn't set IRQ polarity: %d\n",
200 ret);
201 goto err;
202 }
203
204 flags |= IRQF_TRIGGER_HIGH;
205 } else {
206 flags |= IRQF_TRIGGER_LOW;
207 }
208
209 /* Allocate a virtual IRQ domain to distribute to the regmap domains */
210 arizona->virq = irq_domain_add_linear(NULL, 2, &arizona_domain_ops,
211 arizona);
212 if (!arizona->virq) {
213 ret = -EINVAL;
214 goto err;
215 }
216
217 ret = regmap_add_irq_chip(arizona->regmap,
218 irq_create_mapping(arizona->virq, 0),
219 IRQF_ONESHOT, -1, aod,
220 &arizona->aod_irq_chip);
221 if (ret != 0) {
222 dev_err(arizona->dev, "Failed to add AOD IRQs: %d\n", ret);
223 goto err_domain;
224 }
225
226 ret = regmap_add_irq_chip(arizona->regmap,
227 irq_create_mapping(arizona->virq, 1),
228 IRQF_ONESHOT, -1, irq,
229 &arizona->irq_chip);
230 if (ret != 0) {
231 dev_err(arizona->dev, "Failed to add AOD IRQs: %d\n", ret);
232 goto err_aod;
233 }
234
235 /* Make sure the boot done IRQ is unmasked for resumes */
236 i = arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE);
237 ret = request_threaded_irq(i, NULL, arizona_boot_done, IRQF_ONESHOT,
238 "Boot done", arizona);
239 if (ret != 0) {
240 dev_err(arizona->dev, "Failed to request boot done %d: %d\n",
241 arizona->irq, ret);
242 goto err_boot_done;
243 }
244
245 /* Handle control interface errors in the core */
Mark Brown92d80132012-08-07 19:57:53 +0100246 if (ctrlif_error) {
247 i = arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR);
248 ret = request_threaded_irq(i, NULL, arizona_ctrlif_err,
249 IRQF_ONESHOT,
250 "Control interface error", arizona);
251 if (ret != 0) {
252 dev_err(arizona->dev,
253 "Failed to request CTRLIF_ERR %d: %d\n",
254 arizona->irq, ret);
255 goto err_ctrlif;
256 }
Mark Brown966cdc92012-06-19 16:34:23 +0100257 }
258
259 ret = request_threaded_irq(arizona->irq, NULL, arizona_irq_thread,
260 flags, "arizona", arizona);
261
262 if (ret != 0) {
263 dev_err(arizona->dev, "Failed to request IRQ %d: %d\n",
264 arizona->irq, ret);
265 goto err_main_irq;
266 }
267
268 return 0;
269
270err_main_irq:
271 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR), arizona);
272err_ctrlif:
273 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona);
274err_boot_done:
275 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1),
276 arizona->irq_chip);
277err_aod:
278 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0),
279 arizona->aod_irq_chip);
280err_domain:
281err:
282 return ret;
283}
284
285int arizona_irq_exit(struct arizona *arizona)
286{
287 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR), arizona);
288 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona);
289 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1),
290 arizona->irq_chip);
291 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0),
292 arizona->aod_irq_chip);
293 free_irq(arizona->irq, arizona);
294
295 return 0;
296}