arizona-irq.c 10.6 KB
Newer Older
M
Mark Brown 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Arizona interrupt support
 *
 * Copyright 2012 Wolfson Microelectronics plc
 *
 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/irqdomain.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>

#include <linux/mfd/arizona/core.h>
#include <linux/mfd/arizona/registers.h>

#include "arizona.h"

29 30 31
#define ARIZONA_AOD_IRQ_INDEX 0
#define ARIZONA_MAIN_IRQ_INDEX 1

M
Mark Brown 已提交
32 33 34 35
static int arizona_map_irq(struct arizona *arizona, int irq)
{
	int ret;

36 37 38 39 40
	if (arizona->aod_irq_chip) {
		ret = regmap_irq_get_virq(arizona->aod_irq_chip, irq);
		if (ret >= 0)
			return ret;
	}
M
Mark Brown 已提交
41

42
	return regmap_irq_get_virq(arizona->irq_chip, irq);
M
Mark Brown 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
}

int arizona_request_irq(struct arizona *arizona, int irq, char *name,
			   irq_handler_t handler, void *data)
{
	irq = arizona_map_irq(arizona, irq);
	if (irq < 0)
		return irq;

	return request_threaded_irq(irq, NULL, handler, IRQF_ONESHOT,
				    name, data);
}
EXPORT_SYMBOL_GPL(arizona_request_irq);

void arizona_free_irq(struct arizona *arizona, int irq, void *data)
{
	irq = arizona_map_irq(arizona, irq);
	if (irq < 0)
		return;

	free_irq(irq, data);
}
EXPORT_SYMBOL_GPL(arizona_free_irq);

int arizona_set_irq_wake(struct arizona *arizona, int irq, int on)
{
	irq = arizona_map_irq(arizona, irq);
	if (irq < 0)
		return irq;

	return irq_set_irq_wake(irq, on);
}
EXPORT_SYMBOL_GPL(arizona_set_irq_wake);

static irqreturn_t arizona_boot_done(int irq, void *data)
{
	struct arizona *arizona = data;

	dev_dbg(arizona->dev, "Boot done\n");

	return IRQ_HANDLED;
}

static irqreturn_t arizona_ctrlif_err(int irq, void *data)
{
	struct arizona *arizona = data;

	/*
	 * For pretty much all potential sources a register cache sync
	 * won't help, we've just got a software bug somewhere.
	 */
	dev_err(arizona->dev, "Control interface error\n");

	return IRQ_HANDLED;
}

static irqreturn_t arizona_irq_thread(int irq, void *data)
{
	struct arizona *arizona = data;
102
	bool poll;
103
	unsigned int val;
104
	int ret;
M
Mark Brown 已提交
105 106 107 108 109 110 111

	ret = pm_runtime_get_sync(arizona->dev);
	if (ret < 0) {
		dev_err(arizona->dev, "Failed to resume device: %d\n", ret);
		return IRQ_NONE;
	}

112 113 114
	do {
		poll = false;

115 116 117 118 119 120 121 122 123 124 125 126 127 128
		if (arizona->aod_irq_chip) {
			/*
			 * Check the AOD status register to determine whether
			 * the nested IRQ handler should be called.
			 */
			ret = regmap_read(arizona->regmap,
					  ARIZONA_AOD_IRQ1, &val);
			if (ret)
				dev_warn(arizona->dev,
					"Failed to read AOD IRQ1 %d\n", ret);
			else if (val)
				handle_nested_irq(
					irq_find_mapping(arizona->virq, 0));
		}
129 130 131 132 133 134 135 136 137 138 139 140 141

		/*
		 * Check if one of the main interrupts is asserted and only
		 * check that domain if it is.
		 */
		ret = regmap_read(arizona->regmap, ARIZONA_IRQ_PIN_STATUS,
				  &val);
		if (ret == 0 && val & ARIZONA_IRQ1_STS) {
			handle_nested_irq(irq_find_mapping(arizona->virq, 1));
		} else if (ret != 0) {
			dev_err(arizona->dev,
				"Failed to read main IRQ status: %d\n", ret);
		}
142

143 144 145 146 147 148 149 150 151 152 153 154 155 156
		/*
		 * Poll the IRQ pin status to see if we're really done
		 * if the interrupt controller can't do it for us.
		 */
		if (!arizona->pdata.irq_gpio) {
			break;
		} else if (arizona->pdata.irq_flags & IRQF_TRIGGER_RISING &&
			   gpio_get_value_cansleep(arizona->pdata.irq_gpio)) {
			poll = true;
		} else if (arizona->pdata.irq_flags & IRQF_TRIGGER_FALLING &&
			   !gpio_get_value_cansleep(arizona->pdata.irq_gpio)) {
			poll = true;
		}
	} while (poll);
M
Mark Brown 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

	pm_runtime_mark_last_busy(arizona->dev);
	pm_runtime_put_autosuspend(arizona->dev);

	return IRQ_HANDLED;
}

static void arizona_irq_enable(struct irq_data *data)
{
}

static void arizona_irq_disable(struct irq_data *data)
{
}

172 173 174 175 176 177 178
static int arizona_irq_set_wake(struct irq_data *data, unsigned int on)
{
	struct arizona *arizona = irq_data_get_irq_chip_data(data);

	return irq_set_irq_wake(arizona->irq, on);
}

M
Mark Brown 已提交
179 180 181 182
static struct irq_chip arizona_irq_chip = {
	.name			= "arizona",
	.irq_disable		= arizona_irq_disable,
	.irq_enable		= arizona_irq_enable,
183
	.irq_set_wake		= arizona_irq_set_wake,
M
Mark Brown 已提交
184 185
};

186 187
static struct lock_class_key arizona_irq_lock_class;

M
Mark Brown 已提交
188 189 190
static int arizona_irq_map(struct irq_domain *h, unsigned int virq,
			      irq_hw_number_t hw)
{
191
	struct arizona *data = h->host_data;
M
Mark Brown 已提交
192 193

	irq_set_chip_data(virq, data);
194
	irq_set_lockdep_class(virq, &arizona_irq_lock_class);
195
	irq_set_chip_and_handler(virq, &arizona_irq_chip, handle_simple_irq);
M
Mark Brown 已提交
196 197 198 199 200 201
	irq_set_nested_thread(virq, 1);
	irq_set_noprobe(virq);

	return 0;
}

202
static const struct irq_domain_ops arizona_domain_ops = {
M
Mark Brown 已提交
203 204 205 206 207 208 209
	.map	= arizona_irq_map,
	.xlate	= irq_domain_xlate_twocell,
};

int arizona_irq_init(struct arizona *arizona)
{
	int flags = IRQF_ONESHOT;
210
	int ret;
M
Mark Brown 已提交
211
	const struct regmap_irq_chip *aod, *irq;
212
	struct irq_data *irq_data;
213
	unsigned int virq;
M
Mark Brown 已提交
214

215 216
	arizona->ctrlif_error = true;

M
Mark Brown 已提交
217
	switch (arizona->type) {
M
Mark Brown 已提交
218
#ifdef CONFIG_MFD_WM5102
M
Mark Brown 已提交
219 220 221
	case WM5102:
		aod = &wm5102_aod;
		irq = &wm5102_irq;
222

223
		arizona->ctrlif_error = false;
M
Mark Brown 已提交
224
		break;
M
Mark Brown 已提交
225 226 227
#endif
#ifdef CONFIG_MFD_WM5110
	case WM5110:
228
	case WM8280:
M
Mark Brown 已提交
229
		aod = &wm5110_aod;
230 231 232 233 234 235 236 237 238

		switch (arizona->rev) {
		case 0 ... 2:
			irq = &wm5110_irq;
			break;
		default:
			irq = &wm5110_revd_irq;
			break;
		}
239

240
		arizona->ctrlif_error = false;
M
Mark Brown 已提交
241
		break;
M
Mark Brown 已提交
242
#endif
243 244 245 246 247 248 249 250 251
#ifdef CONFIG_MFD_CS47L24
	case WM1831:
	case CS47L24:
		aod = NULL;
		irq = &cs47l24_irq;

		arizona->ctrlif_error = false;
		break;
#endif
252 253 254 255 256
#ifdef CONFIG_MFD_WM8997
	case WM8997:
		aod = &wm8997_aod;
		irq = &wm8997_irq;

257
		arizona->ctrlif_error = false;
258 259
		break;
#endif
260 261 262 263 264 265 266 267 268
#ifdef CONFIG_MFD_WM8998
	case WM8998:
	case WM1814:
		aod = &wm8998_aod;
		irq = &wm8998_irq;

		arizona->ctrlif_error = false;
		break;
#endif
M
Mark Brown 已提交
269 270 271 272 273
	default:
		BUG_ON("Unknown Arizona class device" == NULL);
		return -EINVAL;
	}

274 275 276
	/* Disable all wake sources by default */
	regmap_write(arizona->regmap, ARIZONA_WAKE_CONTROL, 0);

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
	/* Read the flags from the interrupt controller if not specified */
	if (!arizona->pdata.irq_flags) {
		irq_data = irq_get_irq_data(arizona->irq);
		if (!irq_data) {
			dev_err(arizona->dev, "Invalid IRQ: %d\n",
				arizona->irq);
			return -EINVAL;
		}

		arizona->pdata.irq_flags = irqd_get_trigger_type(irq_data);
		switch (arizona->pdata.irq_flags) {
		case IRQF_TRIGGER_LOW:
		case IRQF_TRIGGER_HIGH:
		case IRQF_TRIGGER_RISING:
		case IRQF_TRIGGER_FALLING:
			break;

		case IRQ_TYPE_NONE:
		default:
			/* Device default */
			arizona->pdata.irq_flags = IRQF_TRIGGER_LOW;
			break;
		}
	}
301 302 303

	if (arizona->pdata.irq_flags & (IRQF_TRIGGER_HIGH |
					IRQF_TRIGGER_RISING)) {
M
Mark Brown 已提交
304 305 306 307 308 309 310 311 312
		ret = regmap_update_bits(arizona->regmap, ARIZONA_IRQ_CTRL_1,
					 ARIZONA_IRQ_POL, 0);
		if (ret != 0) {
			dev_err(arizona->dev, "Couldn't set IRQ polarity: %d\n",
				ret);
			goto err;
		}
	}

313 314
	flags |= arizona->pdata.irq_flags;

M
Mark Brown 已提交
315 316 317 318
	/* Allocate a virtual IRQ domain to distribute to the regmap domains */
	arizona->virq = irq_domain_add_linear(NULL, 2, &arizona_domain_ops,
					      arizona);
	if (!arizona->virq) {
319
		dev_err(arizona->dev, "Failed to add core IRQ domain\n");
M
Mark Brown 已提交
320 321 322 323
		ret = -EINVAL;
		goto err;
	}

324
	if (aod) {
325 326 327 328 329 330 331 332 333
		virq = irq_create_mapping(arizona->virq, ARIZONA_AOD_IRQ_INDEX);
		if (!virq) {
			dev_err(arizona->dev, "Failed to map AOD IRQs\n");
			ret = -EINVAL;
			goto err_domain;
		}

		ret = regmap_add_irq_chip(arizona->regmap, virq, IRQF_ONESHOT,
					  0, aod, &arizona->aod_irq_chip);
334 335 336
		if (ret != 0) {
			dev_err(arizona->dev,
				"Failed to add AOD IRQs: %d\n", ret);
337
			goto err_map_aod;
338
		}
M
Mark Brown 已提交
339 340
	}

341 342 343 344 345 346 347 348 349
	virq = irq_create_mapping(arizona->virq, ARIZONA_MAIN_IRQ_INDEX);
	if (!virq) {
		dev_err(arizona->dev, "Failed to map main IRQs\n");
		ret = -EINVAL;
		goto err_aod;
	}

	ret = regmap_add_irq_chip(arizona->regmap, virq, IRQF_ONESHOT,
				  0, irq, &arizona->irq_chip);
M
Mark Brown 已提交
350
	if (ret != 0) {
351
		dev_err(arizona->dev, "Failed to add main IRQs: %d\n", ret);
352
		goto err_map_main_irq;
M
Mark Brown 已提交
353 354
	}

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
	/* Used to emulate edge trigger and to work around broken pinmux */
	if (arizona->pdata.irq_gpio) {
		if (gpio_to_irq(arizona->pdata.irq_gpio) != arizona->irq) {
			dev_warn(arizona->dev, "IRQ %d is not GPIO %d (%d)\n",
				 arizona->irq, arizona->pdata.irq_gpio,
				 gpio_to_irq(arizona->pdata.irq_gpio));
			arizona->irq = gpio_to_irq(arizona->pdata.irq_gpio);
		}

		ret = devm_gpio_request_one(arizona->dev,
					    arizona->pdata.irq_gpio,
					    GPIOF_IN, "arizona IRQ");
		if (ret != 0) {
			dev_err(arizona->dev,
				"Failed to request IRQ GPIO %d:: %d\n",
				arizona->pdata.irq_gpio, ret);
			arizona->pdata.irq_gpio = 0;
		}
	}

M
Mark Brown 已提交
375 376 377 378
	ret = request_threaded_irq(arizona->irq, NULL, arizona_irq_thread,
				   flags, "arizona", arizona);

	if (ret != 0) {
379
		dev_err(arizona->dev, "Failed to request primary IRQ %d: %d\n",
M
Mark Brown 已提交
380 381 382 383
			arizona->irq, ret);
		goto err_main_irq;
	}

384
	/* Make sure the boot done IRQ is unmasked for resumes */
385 386
	ret = arizona_request_irq(arizona, ARIZONA_IRQ_BOOT_DONE, "Boot done",
				  arizona_boot_done, arizona);
387 388 389 390 391 392 393 394
	if (ret != 0) {
		dev_err(arizona->dev, "Failed to request boot done %d: %d\n",
			arizona->irq, ret);
		goto err_boot_done;
	}

	/* Handle control interface errors in the core */
	if (arizona->ctrlif_error) {
395 396 397
		ret = arizona_request_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR,
					  "Control interface error",
					  arizona_ctrlif_err, arizona);
398 399 400 401 402 403 404 405
		if (ret != 0) {
			dev_err(arizona->dev,
				"Failed to request CTRLIF_ERR %d: %d\n",
				arizona->irq, ret);
			goto err_ctrlif;
		}
	}

M
Mark Brown 已提交
406 407 408
	return 0;

err_ctrlif:
409
	arizona_free_irq(arizona, ARIZONA_IRQ_BOOT_DONE, arizona);
M
Mark Brown 已提交
410
err_boot_done:
411 412
	free_irq(arizona->irq, arizona);
err_main_irq:
413 414
	regmap_del_irq_chip(irq_find_mapping(arizona->virq,
					     ARIZONA_MAIN_IRQ_INDEX),
M
Mark Brown 已提交
415
			    arizona->irq_chip);
416 417 418
err_map_main_irq:
	irq_dispose_mapping(irq_find_mapping(arizona->virq,
					     ARIZONA_MAIN_IRQ_INDEX));
M
Mark Brown 已提交
419
err_aod:
420 421
	regmap_del_irq_chip(irq_find_mapping(arizona->virq,
					     ARIZONA_AOD_IRQ_INDEX),
M
Mark Brown 已提交
422
			    arizona->aod_irq_chip);
423 424 425 426 427
err_map_aod:
	irq_dispose_mapping(irq_find_mapping(arizona->virq,
					     ARIZONA_AOD_IRQ_INDEX));
err_domain:
	irq_domain_remove(arizona->virq);
M
Mark Brown 已提交
428 429 430 431 432 433
err:
	return ret;
}

int arizona_irq_exit(struct arizona *arizona)
{
434 435
	unsigned int virq;

436
	if (arizona->ctrlif_error)
437 438 439
		arizona_free_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR, arizona);
	arizona_free_irq(arizona, ARIZONA_IRQ_BOOT_DONE, arizona);

440 441 442 443 444 445 446 447 448 449
	virq = irq_find_mapping(arizona->virq, ARIZONA_MAIN_IRQ_INDEX);
	regmap_del_irq_chip(virq, arizona->irq_chip);
	irq_dispose_mapping(virq);

	virq = irq_find_mapping(arizona->virq, ARIZONA_AOD_IRQ_INDEX);
	regmap_del_irq_chip(virq, arizona->aod_irq_chip);
	irq_dispose_mapping(virq);

	irq_domain_remove(arizona->virq);

M
Mark Brown 已提交
450 451 452 453
	free_irq(arizona->irq, arizona);

	return 0;
}