xref: /openbmc/linux/drivers/mfd/arizona-irq.c (revision b802fb99)
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 	if (arizona->aod_irq_chip) {
34 		ret = regmap_irq_get_virq(arizona->aod_irq_chip, irq);
35 		if (ret >= 0)
36 			return ret;
37 	}
38 
39 	return regmap_irq_get_virq(arizona->irq_chip, irq);
40 }
41 
42 int arizona_request_irq(struct arizona *arizona, int irq, char *name,
43 			   irq_handler_t handler, void *data)
44 {
45 	irq = arizona_map_irq(arizona, irq);
46 	if (irq < 0)
47 		return irq;
48 
49 	return request_threaded_irq(irq, NULL, handler, IRQF_ONESHOT,
50 				    name, data);
51 }
52 EXPORT_SYMBOL_GPL(arizona_request_irq);
53 
54 void arizona_free_irq(struct arizona *arizona, int irq, void *data)
55 {
56 	irq = arizona_map_irq(arizona, irq);
57 	if (irq < 0)
58 		return;
59 
60 	free_irq(irq, data);
61 }
62 EXPORT_SYMBOL_GPL(arizona_free_irq);
63 
64 int arizona_set_irq_wake(struct arizona *arizona, int irq, int on)
65 {
66 	irq = arizona_map_irq(arizona, irq);
67 	if (irq < 0)
68 		return irq;
69 
70 	return irq_set_irq_wake(irq, on);
71 }
72 EXPORT_SYMBOL_GPL(arizona_set_irq_wake);
73 
74 static irqreturn_t arizona_boot_done(int irq, void *data)
75 {
76 	struct arizona *arizona = data;
77 
78 	dev_dbg(arizona->dev, "Boot done\n");
79 
80 	return IRQ_HANDLED;
81 }
82 
83 static irqreturn_t arizona_ctrlif_err(int irq, void *data)
84 {
85 	struct arizona *arizona = data;
86 
87 	/*
88 	 * For pretty much all potential sources a register cache sync
89 	 * won't help, we've just got a software bug somewhere.
90 	 */
91 	dev_err(arizona->dev, "Control interface error\n");
92 
93 	return IRQ_HANDLED;
94 }
95 
96 static irqreturn_t arizona_irq_thread(int irq, void *data)
97 {
98 	struct arizona *arizona = data;
99 	bool poll;
100 	unsigned int val;
101 	int ret;
102 
103 	ret = pm_runtime_get_sync(arizona->dev);
104 	if (ret < 0) {
105 		dev_err(arizona->dev, "Failed to resume device: %d\n", ret);
106 		return IRQ_NONE;
107 	}
108 
109 	do {
110 		poll = false;
111 
112 		if (arizona->aod_irq_chip)
113 			handle_nested_irq(irq_find_mapping(arizona->virq, 0));
114 
115 		/*
116 		 * Check if one of the main interrupts is asserted and only
117 		 * check that domain if it is.
118 		 */
119 		ret = regmap_read(arizona->regmap, ARIZONA_IRQ_PIN_STATUS,
120 				  &val);
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);
126 		}
127 
128 		/*
129 		 * Poll the IRQ pin status to see if we're really done
130 		 * if the interrupt controller can't do it for us.
131 		 */
132 		if (!arizona->pdata.irq_gpio) {
133 			break;
134 		} else if (arizona->pdata.irq_flags & IRQF_TRIGGER_RISING &&
135 			   gpio_get_value_cansleep(arizona->pdata.irq_gpio)) {
136 			poll = true;
137 		} else if (arizona->pdata.irq_flags & IRQF_TRIGGER_FALLING &&
138 			   !gpio_get_value_cansleep(arizona->pdata.irq_gpio)) {
139 			poll = true;
140 		}
141 	} while (poll);
142 
143 	pm_runtime_mark_last_busy(arizona->dev);
144 	pm_runtime_put_autosuspend(arizona->dev);
145 
146 	return IRQ_HANDLED;
147 }
148 
149 static void arizona_irq_enable(struct irq_data *data)
150 {
151 }
152 
153 static void arizona_irq_disable(struct irq_data *data)
154 {
155 }
156 
157 static int arizona_irq_set_wake(struct irq_data *data, unsigned int on)
158 {
159 	struct arizona *arizona = irq_data_get_irq_chip_data(data);
160 
161 	return irq_set_irq_wake(arizona->irq, on);
162 }
163 
164 static struct irq_chip arizona_irq_chip = {
165 	.name			= "arizona",
166 	.irq_disable		= arizona_irq_disable,
167 	.irq_enable		= arizona_irq_enable,
168 	.irq_set_wake		= arizona_irq_set_wake,
169 };
170 
171 static int arizona_irq_map(struct irq_domain *h, unsigned int virq,
172 			      irq_hw_number_t hw)
173 {
174 	struct arizona *data = h->host_data;
175 
176 	irq_set_chip_data(virq, data);
177 	irq_set_chip_and_handler(virq, &arizona_irq_chip, handle_simple_irq);
178 	irq_set_nested_thread(virq, 1);
179 	irq_set_noprobe(virq);
180 
181 	return 0;
182 }
183 
184 static const struct irq_domain_ops arizona_domain_ops = {
185 	.map	= arizona_irq_map,
186 	.xlate	= irq_domain_xlate_twocell,
187 };
188 
189 int arizona_irq_init(struct arizona *arizona)
190 {
191 	int flags = IRQF_ONESHOT;
192 	int ret, i;
193 	const struct regmap_irq_chip *aod, *irq;
194 	struct irq_data *irq_data;
195 
196 	arizona->ctrlif_error = true;
197 
198 	switch (arizona->type) {
199 #ifdef CONFIG_MFD_WM5102
200 	case WM5102:
201 		aod = &wm5102_aod;
202 		irq = &wm5102_irq;
203 
204 		arizona->ctrlif_error = false;
205 		break;
206 #endif
207 #ifdef CONFIG_MFD_WM5110
208 	case WM5110:
209 	case WM8280:
210 		aod = &wm5110_aod;
211 
212 		switch (arizona->rev) {
213 		case 0 ... 2:
214 			irq = &wm5110_irq;
215 			break;
216 		default:
217 			irq = &wm5110_revd_irq;
218 			break;
219 		}
220 
221 		arizona->ctrlif_error = false;
222 		break;
223 #endif
224 #ifdef CONFIG_MFD_CS47L24
225 	case WM1831:
226 	case CS47L24:
227 		aod = NULL;
228 		irq = &cs47l24_irq;
229 
230 		arizona->ctrlif_error = false;
231 		break;
232 #endif
233 #ifdef CONFIG_MFD_WM8997
234 	case WM8997:
235 		aod = &wm8997_aod;
236 		irq = &wm8997_irq;
237 
238 		arizona->ctrlif_error = false;
239 		break;
240 #endif
241 #ifdef CONFIG_MFD_WM8998
242 	case WM8998:
243 	case WM1814:
244 		aod = &wm8998_aod;
245 		irq = &wm8998_irq;
246 
247 		arizona->ctrlif_error = false;
248 		break;
249 #endif
250 	default:
251 		BUG_ON("Unknown Arizona class device" == NULL);
252 		return -EINVAL;
253 	}
254 
255 	/* Disable all wake sources by default */
256 	regmap_write(arizona->regmap, ARIZONA_WAKE_CONTROL, 0);
257 
258 	/* Read the flags from the interrupt controller if not specified */
259 	if (!arizona->pdata.irq_flags) {
260 		irq_data = irq_get_irq_data(arizona->irq);
261 		if (!irq_data) {
262 			dev_err(arizona->dev, "Invalid IRQ: %d\n",
263 				arizona->irq);
264 			return -EINVAL;
265 		}
266 
267 		arizona->pdata.irq_flags = irqd_get_trigger_type(irq_data);
268 		switch (arizona->pdata.irq_flags) {
269 		case IRQF_TRIGGER_LOW:
270 		case IRQF_TRIGGER_HIGH:
271 		case IRQF_TRIGGER_RISING:
272 		case IRQF_TRIGGER_FALLING:
273 			break;
274 
275 		case IRQ_TYPE_NONE:
276 		default:
277 			/* Device default */
278 			arizona->pdata.irq_flags = IRQF_TRIGGER_LOW;
279 			break;
280 		}
281 	}
282 
283 	if (arizona->pdata.irq_flags & (IRQF_TRIGGER_HIGH |
284 					IRQF_TRIGGER_RISING)) {
285 		ret = regmap_update_bits(arizona->regmap, ARIZONA_IRQ_CTRL_1,
286 					 ARIZONA_IRQ_POL, 0);
287 		if (ret != 0) {
288 			dev_err(arizona->dev, "Couldn't set IRQ polarity: %d\n",
289 				ret);
290 			goto err;
291 		}
292 	}
293 
294 	flags |= arizona->pdata.irq_flags;
295 
296 	/* Allocate a virtual IRQ domain to distribute to the regmap domains */
297 	arizona->virq = irq_domain_add_linear(NULL, 2, &arizona_domain_ops,
298 					      arizona);
299 	if (!arizona->virq) {
300 		dev_err(arizona->dev, "Failed to add core IRQ domain\n");
301 		ret = -EINVAL;
302 		goto err;
303 	}
304 
305 	if (aod) {
306 		ret = regmap_add_irq_chip(arizona->regmap,
307 					  irq_create_mapping(arizona->virq, 0),
308 					  IRQF_ONESHOT, 0, aod,
309 					  &arizona->aod_irq_chip);
310 		if (ret != 0) {
311 			dev_err(arizona->dev,
312 				"Failed to add AOD IRQs: %d\n", ret);
313 			goto err;
314 		}
315 	}
316 
317 	ret = regmap_add_irq_chip(arizona->regmap,
318 				  irq_create_mapping(arizona->virq, 1),
319 				  IRQF_ONESHOT, 0, irq,
320 				  &arizona->irq_chip);
321 	if (ret != 0) {
322 		dev_err(arizona->dev, "Failed to add main IRQs: %d\n", ret);
323 		goto err_aod;
324 	}
325 
326 	/* Used to emulate edge trigger and to work around broken pinmux */
327 	if (arizona->pdata.irq_gpio) {
328 		if (gpio_to_irq(arizona->pdata.irq_gpio) != arizona->irq) {
329 			dev_warn(arizona->dev, "IRQ %d is not GPIO %d (%d)\n",
330 				 arizona->irq, arizona->pdata.irq_gpio,
331 				 gpio_to_irq(arizona->pdata.irq_gpio));
332 			arizona->irq = gpio_to_irq(arizona->pdata.irq_gpio);
333 		}
334 
335 		ret = devm_gpio_request_one(arizona->dev,
336 					    arizona->pdata.irq_gpio,
337 					    GPIOF_IN, "arizona IRQ");
338 		if (ret != 0) {
339 			dev_err(arizona->dev,
340 				"Failed to request IRQ GPIO %d:: %d\n",
341 				arizona->pdata.irq_gpio, ret);
342 			arizona->pdata.irq_gpio = 0;
343 		}
344 	}
345 
346 	ret = request_threaded_irq(arizona->irq, NULL, arizona_irq_thread,
347 				   flags, "arizona", arizona);
348 
349 	if (ret != 0) {
350 		dev_err(arizona->dev, "Failed to request primary IRQ %d: %d\n",
351 			arizona->irq, ret);
352 		goto err_main_irq;
353 	}
354 
355 	/* Make sure the boot done IRQ is unmasked for resumes */
356 	i = arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE);
357 	ret = request_threaded_irq(i, NULL, arizona_boot_done, IRQF_ONESHOT,
358 				   "Boot done", arizona);
359 	if (ret != 0) {
360 		dev_err(arizona->dev, "Failed to request boot done %d: %d\n",
361 			arizona->irq, ret);
362 		goto err_boot_done;
363 	}
364 
365 	/* Handle control interface errors in the core */
366 	if (arizona->ctrlif_error) {
367 		i = arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR);
368 		ret = request_threaded_irq(i, NULL, arizona_ctrlif_err,
369 					   IRQF_ONESHOT,
370 					   "Control interface error", arizona);
371 		if (ret != 0) {
372 			dev_err(arizona->dev,
373 				"Failed to request CTRLIF_ERR %d: %d\n",
374 				arizona->irq, ret);
375 			goto err_ctrlif;
376 		}
377 	}
378 
379 	return 0;
380 
381 err_ctrlif:
382 	free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona);
383 err_boot_done:
384 	free_irq(arizona->irq, arizona);
385 err_main_irq:
386 	regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1),
387 			    arizona->irq_chip);
388 err_aod:
389 	regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0),
390 			    arizona->aod_irq_chip);
391 err:
392 	return ret;
393 }
394 
395 int arizona_irq_exit(struct arizona *arizona)
396 {
397 	if (arizona->ctrlif_error)
398 		free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR),
399 			 arizona);
400 	free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona);
401 	regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1),
402 			    arizona->irq_chip);
403 	regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0),
404 			    arizona->aod_irq_chip);
405 	free_irq(arizona->irq, arizona);
406 
407 	return 0;
408 }
409