]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/mfd/arizona-irq.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[mirror_ubuntu-artful-kernel.git] / drivers / mfd / arizona-irq.c
1 /*
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
29 static 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
40 int 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 }
50 EXPORT_SYMBOL_GPL(arizona_request_irq);
51
52 void 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 }
60 EXPORT_SYMBOL_GPL(arizona_free_irq);
61
62 int 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 }
70 EXPORT_SYMBOL_GPL(arizona_set_irq_wake);
71
72 static 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
81 static 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
94 static irqreturn_t arizona_irq_thread(int irq, void *data)
95 {
96 struct arizona *arizona = data;
97 bool poll;
98 unsigned int val;
99 int ret;
100
101 ret = pm_runtime_get_sync(arizona->dev);
102 if (ret < 0) {
103 dev_err(arizona->dev, "Failed to resume device: %d\n", ret);
104 return IRQ_NONE;
105 }
106
107 do {
108 poll = false;
109
110 /* Always handle the AoD domain */
111 handle_nested_irq(irq_find_mapping(arizona->virq, 0));
112
113 /*
114 * Check if one of the main interrupts is asserted and only
115 * check that domain if it is.
116 */
117 ret = regmap_read(arizona->regmap, ARIZONA_IRQ_PIN_STATUS,
118 &val);
119 if (ret == 0 && val & ARIZONA_IRQ1_STS) {
120 handle_nested_irq(irq_find_mapping(arizona->virq, 1));
121 } else if (ret != 0) {
122 dev_err(arizona->dev,
123 "Failed to read main IRQ status: %d\n", ret);
124 }
125
126 /*
127 * Poll the IRQ pin status to see if we're really done
128 * if the interrupt controller can't do it for us.
129 */
130 if (!arizona->pdata.irq_gpio) {
131 break;
132 } else if (arizona->pdata.irq_flags & IRQF_TRIGGER_RISING &&
133 gpio_get_value_cansleep(arizona->pdata.irq_gpio)) {
134 poll = true;
135 } else if (arizona->pdata.irq_flags & IRQF_TRIGGER_FALLING &&
136 !gpio_get_value_cansleep(arizona->pdata.irq_gpio)) {
137 poll = true;
138 }
139 } while (poll);
140
141 pm_runtime_mark_last_busy(arizona->dev);
142 pm_runtime_put_autosuspend(arizona->dev);
143
144 return IRQ_HANDLED;
145 }
146
147 static void arizona_irq_enable(struct irq_data *data)
148 {
149 }
150
151 static void arizona_irq_disable(struct irq_data *data)
152 {
153 }
154
155 static int arizona_irq_set_wake(struct irq_data *data, unsigned int on)
156 {
157 struct arizona *arizona = irq_data_get_irq_chip_data(data);
158
159 return irq_set_irq_wake(arizona->irq, on);
160 }
161
162 static struct irq_chip arizona_irq_chip = {
163 .name = "arizona",
164 .irq_disable = arizona_irq_disable,
165 .irq_enable = arizona_irq_enable,
166 .irq_set_wake = arizona_irq_set_wake,
167 };
168
169 static int arizona_irq_map(struct irq_domain *h, unsigned int virq,
170 irq_hw_number_t hw)
171 {
172 struct regmap_irq_chip_data *data = h->host_data;
173
174 irq_set_chip_data(virq, data);
175 irq_set_chip_and_handler(virq, &arizona_irq_chip, handle_simple_irq);
176 irq_set_nested_thread(virq, 1);
177
178 /* ARM needs us to explicitly flag the IRQ as valid
179 * and will set them noprobe when we do so. */
180 #ifdef CONFIG_ARM
181 set_irq_flags(virq, IRQF_VALID);
182 #else
183 irq_set_noprobe(virq);
184 #endif
185
186 return 0;
187 }
188
189 static struct irq_domain_ops arizona_domain_ops = {
190 .map = arizona_irq_map,
191 .xlate = irq_domain_xlate_twocell,
192 };
193
194 int arizona_irq_init(struct arizona *arizona)
195 {
196 int flags = IRQF_ONESHOT;
197 int ret, i;
198 const struct regmap_irq_chip *aod, *irq;
199 struct irq_data *irq_data;
200
201 arizona->ctrlif_error = true;
202
203 switch (arizona->type) {
204 #ifdef CONFIG_MFD_WM5102
205 case WM5102:
206 aod = &wm5102_aod;
207 irq = &wm5102_irq;
208
209 arizona->ctrlif_error = false;
210 break;
211 #endif
212 #ifdef CONFIG_MFD_WM5110
213 case WM5110:
214 aod = &wm5110_aod;
215
216 switch (arizona->rev) {
217 case 0 ... 2:
218 irq = &wm5110_irq;
219 break;
220 default:
221 irq = &wm5110_revd_irq;
222 break;
223 }
224
225 arizona->ctrlif_error = false;
226 break;
227 #endif
228 #ifdef CONFIG_MFD_WM8997
229 case WM8997:
230 aod = &wm8997_aod;
231 irq = &wm8997_irq;
232
233 arizona->ctrlif_error = false;
234 break;
235 #endif
236 default:
237 BUG_ON("Unknown Arizona class device" == NULL);
238 return -EINVAL;
239 }
240
241 /* Disable all wake sources by default */
242 regmap_write(arizona->regmap, ARIZONA_WAKE_CONTROL, 0);
243
244 /* Read the flags from the interrupt controller if not specified */
245 if (!arizona->pdata.irq_flags) {
246 irq_data = irq_get_irq_data(arizona->irq);
247 if (!irq_data) {
248 dev_err(arizona->dev, "Invalid IRQ: %d\n",
249 arizona->irq);
250 return -EINVAL;
251 }
252
253 arizona->pdata.irq_flags = irqd_get_trigger_type(irq_data);
254 switch (arizona->pdata.irq_flags) {
255 case IRQF_TRIGGER_LOW:
256 case IRQF_TRIGGER_HIGH:
257 case IRQF_TRIGGER_RISING:
258 case IRQF_TRIGGER_FALLING:
259 break;
260
261 case IRQ_TYPE_NONE:
262 default:
263 /* Device default */
264 arizona->pdata.irq_flags = IRQF_TRIGGER_LOW;
265 break;
266 }
267 }
268
269 if (arizona->pdata.irq_flags & (IRQF_TRIGGER_HIGH |
270 IRQF_TRIGGER_RISING)) {
271 ret = regmap_update_bits(arizona->regmap, ARIZONA_IRQ_CTRL_1,
272 ARIZONA_IRQ_POL, 0);
273 if (ret != 0) {
274 dev_err(arizona->dev, "Couldn't set IRQ polarity: %d\n",
275 ret);
276 goto err;
277 }
278 }
279
280 flags |= arizona->pdata.irq_flags;
281
282 /* Allocate a virtual IRQ domain to distribute to the regmap domains */
283 arizona->virq = irq_domain_add_linear(NULL, 2, &arizona_domain_ops,
284 arizona);
285 if (!arizona->virq) {
286 dev_err(arizona->dev, "Failed to add core IRQ domain\n");
287 ret = -EINVAL;
288 goto err;
289 }
290
291 ret = regmap_add_irq_chip(arizona->regmap,
292 irq_create_mapping(arizona->virq, 0),
293 IRQF_ONESHOT, 0, aod,
294 &arizona->aod_irq_chip);
295 if (ret != 0) {
296 dev_err(arizona->dev, "Failed to add AOD IRQs: %d\n", ret);
297 goto err_domain;
298 }
299
300 ret = regmap_add_irq_chip(arizona->regmap,
301 irq_create_mapping(arizona->virq, 1),
302 IRQF_ONESHOT, 0, irq,
303 &arizona->irq_chip);
304 if (ret != 0) {
305 dev_err(arizona->dev, "Failed to add main IRQs: %d\n", ret);
306 goto err_aod;
307 }
308
309 /* Make sure the boot done IRQ is unmasked for resumes */
310 i = arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE);
311 ret = request_threaded_irq(i, NULL, arizona_boot_done, IRQF_ONESHOT,
312 "Boot done", arizona);
313 if (ret != 0) {
314 dev_err(arizona->dev, "Failed to request boot done %d: %d\n",
315 arizona->irq, ret);
316 goto err_boot_done;
317 }
318
319 /* Handle control interface errors in the core */
320 if (arizona->ctrlif_error) {
321 i = arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR);
322 ret = request_threaded_irq(i, NULL, arizona_ctrlif_err,
323 IRQF_ONESHOT,
324 "Control interface error", arizona);
325 if (ret != 0) {
326 dev_err(arizona->dev,
327 "Failed to request CTRLIF_ERR %d: %d\n",
328 arizona->irq, ret);
329 goto err_ctrlif;
330 }
331 }
332
333 /* Used to emulate edge trigger and to work around broken pinmux */
334 if (arizona->pdata.irq_gpio) {
335 if (gpio_to_irq(arizona->pdata.irq_gpio) != arizona->irq) {
336 dev_warn(arizona->dev, "IRQ %d is not GPIO %d (%d)\n",
337 arizona->irq, arizona->pdata.irq_gpio,
338 gpio_to_irq(arizona->pdata.irq_gpio));
339 arizona->irq = gpio_to_irq(arizona->pdata.irq_gpio);
340 }
341
342 ret = devm_gpio_request_one(arizona->dev,
343 arizona->pdata.irq_gpio,
344 GPIOF_IN, "arizona IRQ");
345 if (ret != 0) {
346 dev_err(arizona->dev,
347 "Failed to request IRQ GPIO %d:: %d\n",
348 arizona->pdata.irq_gpio, ret);
349 arizona->pdata.irq_gpio = 0;
350 }
351 }
352
353 ret = request_threaded_irq(arizona->irq, NULL, arizona_irq_thread,
354 flags, "arizona", arizona);
355
356 if (ret != 0) {
357 dev_err(arizona->dev, "Failed to request primary IRQ %d: %d\n",
358 arizona->irq, ret);
359 goto err_main_irq;
360 }
361
362 return 0;
363
364 err_main_irq:
365 if (arizona->ctrlif_error)
366 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR),
367 arizona);
368 err_ctrlif:
369 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona);
370 err_boot_done:
371 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1),
372 arizona->irq_chip);
373 err_aod:
374 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0),
375 arizona->aod_irq_chip);
376 err_domain:
377 err:
378 return ret;
379 }
380
381 int arizona_irq_exit(struct arizona *arizona)
382 {
383 if (arizona->ctrlif_error)
384 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR),
385 arizona);
386 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona);
387 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1),
388 arizona->irq_chip);
389 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0),
390 arizona->aod_irq_chip);
391 free_irq(arizona->irq, arizona);
392
393 return 0;
394 }