arizona-core.c 27.1 KB
Newer Older
M
Mark Brown 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Arizona core driver
 *
 * 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>
14
#include <linux/err.h>
M
Mark Brown 已提交
15 16 17 18
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/mfd/core.h>
#include <linux/module.h>
19 20 21
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
M
Mark Brown 已提交
22 23 24
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
25
#include <linux/regulator/machine.h>
M
Mark Brown 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <linux/slab.h>

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

#include "arizona.h"

static const char *wm5102_core_supplies[] = {
	"AVDD",
	"DBVDD1",
};

int arizona_clk32k_enable(struct arizona *arizona)
{
	int ret = 0;

	mutex_lock(&arizona->clk_lock);

	arizona->clk32k_ref++;

46 47 48 49 50 51 52 53 54
	if (arizona->clk32k_ref == 1) {
		switch (arizona->pdata.clk32k_src) {
		case ARIZONA_32KZ_MCLK1:
			ret = pm_runtime_get_sync(arizona->dev);
			if (ret != 0)
				goto out;
			break;
		}

M
Mark Brown 已提交
55 56 57
		ret = regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
					 ARIZONA_CLK_32K_ENA,
					 ARIZONA_CLK_32K_ENA);
58
	}
M
Mark Brown 已提交
59

60
out:
M
Mark Brown 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	if (ret != 0)
		arizona->clk32k_ref--;

	mutex_unlock(&arizona->clk_lock);

	return ret;
}
EXPORT_SYMBOL_GPL(arizona_clk32k_enable);

int arizona_clk32k_disable(struct arizona *arizona)
{
	int ret = 0;

	mutex_lock(&arizona->clk_lock);

	BUG_ON(arizona->clk32k_ref <= 0);

	arizona->clk32k_ref--;

80
	if (arizona->clk32k_ref == 0) {
M
Mark Brown 已提交
81 82 83
		regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
				   ARIZONA_CLK_32K_ENA, 0);

84 85 86 87 88 89 90
		switch (arizona->pdata.clk32k_src) {
		case ARIZONA_32KZ_MCLK1:
			pm_runtime_put_sync(arizona->dev);
			break;
		}
	}

M
Mark Brown 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	mutex_unlock(&arizona->clk_lock);

	return ret;
}
EXPORT_SYMBOL_GPL(arizona_clk32k_disable);

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

	dev_err(arizona->dev, "CLKGEN error\n");

	return IRQ_HANDLED;
}

static irqreturn_t arizona_underclocked(int irq, void *data)
{
	struct arizona *arizona = data;
	unsigned int val;
	int ret;

	ret = regmap_read(arizona->regmap, ARIZONA_INTERRUPT_RAW_STATUS_8,
			  &val);
	if (ret != 0) {
		dev_err(arizona->dev, "Failed to read underclock status: %d\n",
			ret);
		return IRQ_NONE;
	}

	if (val & ARIZONA_AIF3_UNDERCLOCKED_STS)
		dev_err(arizona->dev, "AIF3 underclocked\n");
	if (val & ARIZONA_AIF2_UNDERCLOCKED_STS)
123 124
		dev_err(arizona->dev, "AIF2 underclocked\n");
	if (val & ARIZONA_AIF1_UNDERCLOCKED_STS)
M
Mark Brown 已提交
125
		dev_err(arizona->dev, "AIF1 underclocked\n");
126 127
	if (val & ARIZONA_ISRC3_UNDERCLOCKED_STS)
		dev_err(arizona->dev, "ISRC3 underclocked\n");
M
Mark Brown 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140
	if (val & ARIZONA_ISRC2_UNDERCLOCKED_STS)
		dev_err(arizona->dev, "ISRC2 underclocked\n");
	if (val & ARIZONA_ISRC1_UNDERCLOCKED_STS)
		dev_err(arizona->dev, "ISRC1 underclocked\n");
	if (val & ARIZONA_FX_UNDERCLOCKED_STS)
		dev_err(arizona->dev, "FX underclocked\n");
	if (val & ARIZONA_ASRC_UNDERCLOCKED_STS)
		dev_err(arizona->dev, "ASRC underclocked\n");
	if (val & ARIZONA_DAC_UNDERCLOCKED_STS)
		dev_err(arizona->dev, "DAC underclocked\n");
	if (val & ARIZONA_ADC_UNDERCLOCKED_STS)
		dev_err(arizona->dev, "ADC underclocked\n");
	if (val & ARIZONA_MIXER_UNDERCLOCKED_STS)
141
		dev_err(arizona->dev, "Mixer dropped sample\n");
M
Mark Brown 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

	return IRQ_HANDLED;
}

static irqreturn_t arizona_overclocked(int irq, void *data)
{
	struct arizona *arizona = data;
	unsigned int val[2];
	int ret;
	
	ret = regmap_bulk_read(arizona->regmap, ARIZONA_INTERRUPT_RAW_STATUS_6,
			       &val[0], 2);
	if (ret != 0) {
		dev_err(arizona->dev, "Failed to read overclock status: %d\n",
			ret);
		return IRQ_NONE;
	}

	if (val[0] & ARIZONA_PWM_OVERCLOCKED_STS)
		dev_err(arizona->dev, "PWM overclocked\n");
	if (val[0] & ARIZONA_FX_CORE_OVERCLOCKED_STS)
		dev_err(arizona->dev, "FX core overclocked\n");
	if (val[0] & ARIZONA_DAC_SYS_OVERCLOCKED_STS)
		dev_err(arizona->dev, "DAC SYS overclocked\n");
	if (val[0] & ARIZONA_DAC_WARP_OVERCLOCKED_STS)
		dev_err(arizona->dev, "DAC WARP overclocked\n");
	if (val[0] & ARIZONA_ADC_OVERCLOCKED_STS)
		dev_err(arizona->dev, "ADC overclocked\n");
	if (val[0] & ARIZONA_MIXER_OVERCLOCKED_STS)
		dev_err(arizona->dev, "Mixer overclocked\n");
	if (val[0] & ARIZONA_AIF3_SYNC_OVERCLOCKED_STS)
		dev_err(arizona->dev, "AIF3 overclocked\n");
	if (val[0] & ARIZONA_AIF2_SYNC_OVERCLOCKED_STS)
		dev_err(arizona->dev, "AIF2 overclocked\n");
	if (val[0] & ARIZONA_AIF1_SYNC_OVERCLOCKED_STS)
		dev_err(arizona->dev, "AIF1 overclocked\n");
	if (val[0] & ARIZONA_PAD_CTRL_OVERCLOCKED_STS)
		dev_err(arizona->dev, "Pad control overclocked\n");

	if (val[1] & ARIZONA_SLIMBUS_SUBSYS_OVERCLOCKED_STS)
		dev_err(arizona->dev, "Slimbus subsystem overclocked\n");
	if (val[1] & ARIZONA_SLIMBUS_ASYNC_OVERCLOCKED_STS)
		dev_err(arizona->dev, "Slimbus async overclocked\n");
	if (val[1] & ARIZONA_SLIMBUS_SYNC_OVERCLOCKED_STS)
		dev_err(arizona->dev, "Slimbus sync overclocked\n");
	if (val[1] & ARIZONA_ASRC_ASYNC_SYS_OVERCLOCKED_STS)
		dev_err(arizona->dev, "ASRC async system overclocked\n");
	if (val[1] & ARIZONA_ASRC_ASYNC_WARP_OVERCLOCKED_STS)
		dev_err(arizona->dev, "ASRC async WARP overclocked\n");
	if (val[1] & ARIZONA_ASRC_SYNC_SYS_OVERCLOCKED_STS)
		dev_err(arizona->dev, "ASRC sync system overclocked\n");
	if (val[1] & ARIZONA_ASRC_SYNC_WARP_OVERCLOCKED_STS)
		dev_err(arizona->dev, "ASRC sync WARP overclocked\n");
	if (val[1] & ARIZONA_ADSP2_1_OVERCLOCKED_STS)
		dev_err(arizona->dev, "DSP1 overclocked\n");
197 198
	if (val[1] & ARIZONA_ISRC3_OVERCLOCKED_STS)
		dev_err(arizona->dev, "ISRC3 overclocked\n");
M
Mark Brown 已提交
199 200 201 202 203 204 205 206
	if (val[1] & ARIZONA_ISRC2_OVERCLOCKED_STS)
		dev_err(arizona->dev, "ISRC2 overclocked\n");
	if (val[1] & ARIZONA_ISRC1_OVERCLOCKED_STS)
		dev_err(arizona->dev, "ISRC1 overclocked\n");

	return IRQ_HANDLED;
}

207 208 209
static int arizona_poll_reg(struct arizona *arizona,
			    int timeout, unsigned int reg,
			    unsigned int mask, unsigned int target)
M
Mark Brown 已提交
210
{
211
	unsigned int val = 0;
M
Mark Brown 已提交
212 213
	int ret, i;

214 215
	for (i = 0; i < timeout; i++) {
		ret = regmap_read(arizona->regmap, reg, &val);
M
Mark Brown 已提交
216
		if (ret != 0) {
217 218
			dev_err(arizona->dev, "Failed to read reg %u: %d\n",
				reg, ret);
219
			continue;
M
Mark Brown 已提交
220 221
		}

222 223 224 225
		if ((val & mask) == target)
			return 0;

		msleep(1);
M
Mark Brown 已提交
226 227
	}

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
	dev_err(arizona->dev, "Polling reg %u timed out: %x\n", reg, val);
	return -ETIMEDOUT;
}

static int arizona_wait_for_boot(struct arizona *arizona)
{
	int ret;

	/*
	 * We can't use an interrupt as we need to runtime resume to do so,
	 * we won't race with the interrupt handler as it'll be blocked on
	 * runtime resume.
	 */
	ret = arizona_poll_reg(arizona, 5, ARIZONA_INTERRUPT_RAW_STATUS_5,
			       ARIZONA_BOOT_DONE_STS, ARIZONA_BOOT_DONE_STS);

	if (!ret)
M
Mark Brown 已提交
245 246 247 248 249
		regmap_write(arizona->regmap, ARIZONA_INTERRUPT_STATUS_5,
			     ARIZONA_BOOT_DONE_STS);

	pm_runtime_mark_last_busy(arizona->dev);

250
	return ret;
M
Mark Brown 已提交
251 252
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266
static inline void arizona_enable_reset(struct arizona *arizona)
{
	if (arizona->pdata.reset)
		gpio_set_value_cansleep(arizona->pdata.reset, 0);
}

static void arizona_disable_reset(struct arizona *arizona)
{
	if (arizona->pdata.reset) {
		gpio_set_value_cansleep(arizona->pdata.reset, 1);
		msleep(1);
	}
}

267
static int wm5102_apply_hardware_patch(struct arizona *arizona)
268 269 270 271 272 273
{
	unsigned int fll, sysclk;
	int ret, err;

	/* Cache existing FLL and SYSCLK settings */
	ret = regmap_read(arizona->regmap, ARIZONA_FLL1_CONTROL_1, &fll);
274
	if (ret) {
275 276 277 278 279
		dev_err(arizona->dev, "Failed to cache FLL settings: %d\n",
			ret);
		return ret;
	}
	ret = regmap_read(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1, &sysclk);
280
	if (ret) {
281 282 283 284 285 286 287 288
		dev_err(arizona->dev, "Failed to cache SYSCLK settings: %d\n",
			ret);
		return ret;
	}

	/* Start up SYSCLK using the FLL in free running mode */
	ret = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1,
			ARIZONA_FLL1_ENA | ARIZONA_FLL1_FREERUN);
289
	if (ret) {
290 291 292 293 294 295 296 297
		dev_err(arizona->dev,
			"Failed to start FLL in freerunning mode: %d\n",
			ret);
		return ret;
	}
	ret = arizona_poll_reg(arizona, 25, ARIZONA_INTERRUPT_RAW_STATUS_5,
			       ARIZONA_FLL1_CLOCK_OK_STS,
			       ARIZONA_FLL1_CLOCK_OK_STS);
298
	if (ret) {
299 300 301 302 303
		ret = -ETIMEDOUT;
		goto err_fll;
	}

	ret = regmap_write(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1, 0x0144);
304
	if (ret) {
305 306 307 308 309 310
		dev_err(arizona->dev, "Failed to start SYSCLK: %d\n", ret);
		goto err_fll;
	}

	/* Start the write sequencer and wait for it to finish */
	ret = regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_0,
311 312
			   ARIZONA_WSEQ_ENA | ARIZONA_WSEQ_START | 160);
	if (ret) {
313 314 315 316 317 318
		dev_err(arizona->dev, "Failed to start write sequencer: %d\n",
			ret);
		goto err_sysclk;
	}
	ret = arizona_poll_reg(arizona, 5, ARIZONA_WRITE_SEQUENCER_CTRL_1,
			       ARIZONA_WSEQ_BUSY, 0);
319
	if (ret) {
320
		regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_0,
321
			     ARIZONA_WSEQ_ABORT);
322 323 324 325 326
		ret = -ETIMEDOUT;
	}

err_sysclk:
	err = regmap_write(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1, sysclk);
327
	if (err) {
328 329 330 331 332 333 334
		dev_err(arizona->dev,
			"Failed to re-apply old SYSCLK settings: %d\n",
			err);
	}

err_fll:
	err = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1, fll);
335
	if (err) {
336 337 338 339 340
		dev_err(arizona->dev,
			"Failed to re-apply old FLL settings: %d\n",
			err);
	}

341
	return ret ?: err;
342 343
}

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
static int wm5102_clear_write_sequencer(struct arizona *arizona)
{
	int ret;

	ret = regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_3,
			   0x0);
	if (ret) {
		dev_err(arizona->dev,
			"Failed to clear write sequencer state: %d\n", ret);
		return ret;
	}

	arizona_enable_reset(arizona);
	regulator_disable(arizona->dcvdd);

	msleep(20);

	ret = regulator_enable(arizona->dcvdd);
	if (ret) {
		dev_err(arizona->dev, "Failed to re-enable DCVDD: %d\n", ret);
		return ret;
	}
	arizona_disable_reset(arizona);

	return 0;
}

371
#ifdef CONFIG_PM
M
Mark Brown 已提交
372 373 374 375 376
static int arizona_runtime_resume(struct device *dev)
{
	struct arizona *arizona = dev_get_drvdata(dev);
	int ret;

377 378
	dev_dbg(arizona->dev, "Leaving AoD mode\n");

379 380 381 382 383
	ret = regulator_enable(arizona->dcvdd);
	if (ret != 0) {
		dev_err(arizona->dev, "Failed to enable DCVDD: %d\n", ret);
		return ret;
	}
M
Mark Brown 已提交
384 385 386

	regcache_cache_only(arizona->regmap, false);

387 388
	switch (arizona->type) {
	case WM5102:
389 390 391 392 393 394 395 396 397 398 399
		if (arizona->external_dcvdd) {
			ret = regmap_update_bits(arizona->regmap,
						 ARIZONA_ISOLATION_CONTROL,
						 ARIZONA_ISOLATE_DCVDD1, 0);
			if (ret != 0) {
				dev_err(arizona->dev,
					"Failed to connect DCVDD: %d\n", ret);
				goto err;
			}
		}

400 401 402 403 404 405
		ret = wm5102_patch(arizona);
		if (ret != 0) {
			dev_err(arizona->dev, "Failed to apply patch: %d\n",
				ret);
			goto err;
		}
406

407 408
		ret = wm5102_apply_hardware_patch(arizona);
		if (ret) {
409 410 411 412 413 414 415
			dev_err(arizona->dev,
				"Failed to apply hardware patch: %d\n",
				ret);
			goto err;
		}
		break;
	default:
416 417 418 419 420
		ret = arizona_wait_for_boot(arizona);
		if (ret != 0) {
			goto err;
		}

421 422 423 424 425 426 427 428 429 430
		if (arizona->external_dcvdd) {
			ret = regmap_update_bits(arizona->regmap,
						 ARIZONA_ISOLATION_CONTROL,
						 ARIZONA_ISOLATE_DCVDD1, 0);
			if (ret != 0) {
				dev_err(arizona->dev,
					"Failed to connect DCVDD: %d\n", ret);
				goto err;
			}
		}
431
		break;
432 433
	}

434 435 436
	ret = regcache_sync(arizona->regmap);
	if (ret != 0) {
		dev_err(arizona->dev, "Failed to restore register cache\n");
437
		goto err;
438
	}
M
Mark Brown 已提交
439 440

	return 0;
441 442 443 444 445

err:
	regcache_cache_only(arizona->regmap, true);
	regulator_disable(arizona->dcvdd);
	return ret;
M
Mark Brown 已提交
446 447 448 449 450
}

static int arizona_runtime_suspend(struct device *dev)
{
	struct arizona *arizona = dev_get_drvdata(dev);
451
	int ret;
M
Mark Brown 已提交
452

453 454
	dev_dbg(arizona->dev, "Entering AoD mode\n");

455 456 457 458 459 460 461 462 463 464 465 466
	if (arizona->external_dcvdd) {
		ret = regmap_update_bits(arizona->regmap,
					 ARIZONA_ISOLATION_CONTROL,
					 ARIZONA_ISOLATE_DCVDD1,
					 ARIZONA_ISOLATE_DCVDD1);
		if (ret != 0) {
			dev_err(arizona->dev, "Failed to isolate DCVDD: %d\n",
				ret);
			return ret;
		}
	}

467 468
	regcache_cache_only(arizona->regmap, true);
	regcache_mark_dirty(arizona->regmap);
469
	regulator_disable(arizona->dcvdd);
M
Mark Brown 已提交
470 471 472 473 474

	return 0;
}
#endif

475
#ifdef CONFIG_PM_SLEEP
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
static int arizona_suspend(struct device *dev)
{
	struct arizona *arizona = dev_get_drvdata(dev);

	dev_dbg(arizona->dev, "Suspend, disabling IRQ\n");
	disable_irq(arizona->irq);

	return 0;
}

static int arizona_suspend_late(struct device *dev)
{
	struct arizona *arizona = dev_get_drvdata(dev);

	dev_dbg(arizona->dev, "Late suspend, reenabling IRQ\n");
	enable_irq(arizona->irq);

	return 0;
}

496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
static int arizona_resume_noirq(struct device *dev)
{
	struct arizona *arizona = dev_get_drvdata(dev);

	dev_dbg(arizona->dev, "Early resume, disabling IRQ\n");
	disable_irq(arizona->irq);

	return 0;
}

static int arizona_resume(struct device *dev)
{
	struct arizona *arizona = dev_get_drvdata(dev);

	dev_dbg(arizona->dev, "Late resume, reenabling IRQ\n");
	enable_irq(arizona->irq);

	return 0;
}
#endif

M
Mark Brown 已提交
517 518 519 520
const struct dev_pm_ops arizona_pm_ops = {
	SET_RUNTIME_PM_OPS(arizona_runtime_suspend,
			   arizona_runtime_resume,
			   NULL)
521
	SET_SYSTEM_SLEEP_PM_OPS(arizona_suspend, arizona_resume)
522
#ifdef CONFIG_PM_SLEEP
523
	.suspend_late = arizona_suspend_late,
524 525
	.resume_noirq = arizona_resume_noirq,
#endif
M
Mark Brown 已提交
526 527 528
};
EXPORT_SYMBOL_GPL(arizona_pm_ops);

529
#ifdef CONFIG_OF
530
unsigned long arizona_of_get_type(struct device *dev)
531 532 533 534
{
	const struct of_device_id *id = of_match_device(arizona_of_match, dev);

	if (id)
535
		return (unsigned long)id->data;
536 537 538 539 540
	else
		return 0;
}
EXPORT_SYMBOL_GPL(arizona_of_get_type);

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
int arizona_of_get_named_gpio(struct arizona *arizona, const char *prop,
			      bool mandatory)
{
	int gpio;

	gpio = of_get_named_gpio(arizona->dev->of_node, prop, 0);
	if (gpio < 0) {
		if (mandatory)
			dev_err(arizona->dev,
				"Mandatory DT gpio %s missing/malformed: %d\n",
				prop, gpio);

		gpio = 0;
	}

	return gpio;
}
EXPORT_SYMBOL_GPL(arizona_of_get_named_gpio);

560 561
static int arizona_of_get_core_pdata(struct arizona *arizona)
{
562
	struct arizona_pdata *pdata = &arizona->pdata;
563 564 565
	struct property *prop;
	const __be32 *cur;
	u32 val;
566
	int ret, i;
567
	int count = 0;
568

569
	pdata->reset = arizona_of_get_named_gpio(arizona, "wlf,reset", true);
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584

	ret = of_property_read_u32_array(arizona->dev->of_node,
					 "wlf,gpio-defaults",
					 arizona->pdata.gpio_defaults,
					 ARRAY_SIZE(arizona->pdata.gpio_defaults));
	if (ret >= 0) {
		/*
		 * All values are literal except out of range values
		 * which are chip default, translate into platform
		 * data which uses 0 as chip default and out of range
		 * as zero.
		 */
		for (i = 0; i < ARRAY_SIZE(arizona->pdata.gpio_defaults); i++) {
			if (arizona->pdata.gpio_defaults[i] > 0xffff)
				arizona->pdata.gpio_defaults[i] = 0;
585
			else if (arizona->pdata.gpio_defaults[i] == 0)
586 587 588 589 590 591 592
				arizona->pdata.gpio_defaults[i] = 0x10000;
		}
	} else {
		dev_err(arizona->dev, "Failed to parse GPIO defaults: %d\n",
			ret);
	}

593 594 595 596 597 598 599 600 601
	of_property_for_each_u32(arizona->dev->of_node, "wlf,inmode", prop,
				 cur, val) {
		if (count == ARRAY_SIZE(arizona->pdata.inmode))
			break;

		arizona->pdata.inmode[count] = val;
		count++;
	}

602 603 604 605 606 607 608 609 610 611
	count = 0;
	of_property_for_each_u32(arizona->dev->of_node, "wlf,dmic-ref", prop,
				 cur, val) {
		if (count == ARRAY_SIZE(arizona->pdata.dmic_ref))
			break;

		arizona->pdata.dmic_ref[count] = val;
		count++;
	}

612 613 614 615 616 617
	return 0;
}

const struct of_device_id arizona_of_match[] = {
	{ .compatible = "wlf,wm5102", .data = (void *)WM5102 },
	{ .compatible = "wlf,wm5110", .data = (void *)WM5110 },
618
	{ .compatible = "wlf,wm8280", .data = (void *)WM8280 },
619
	{ .compatible = "wlf,wm8997", .data = (void *)WM8997 },
620 621 622 623 624 625 626 627 628 629
	{},
};
EXPORT_SYMBOL_GPL(arizona_of_match);
#else
static inline int arizona_of_get_core_pdata(struct arizona *arizona)
{
	return 0;
}
#endif

630
static const struct mfd_cell early_devs[] = {
M
Mark Brown 已提交
631 632 633
	{ .name = "arizona-ldo1" },
};

634
static const char *wm5102_supplies[] = {
635
	"MICVDD",
636 637 638 639 640 641 642
	"DBVDD2",
	"DBVDD3",
	"CPVDD",
	"SPKVDDL",
	"SPKVDDR",
};

643
static const struct mfd_cell wm5102_devs[] = {
644
	{ .name = "arizona-micsupp" },
645 646 647 648 649
	{
		.name = "arizona-extcon",
		.parent_supplies = wm5102_supplies,
		.num_parent_supplies = 1, /* We only need MICVDD */
	},
M
Mark Brown 已提交
650
	{ .name = "arizona-gpio" },
651
	{ .name = "arizona-haptics" },
M
Mark Brown 已提交
652
	{ .name = "arizona-pwm" },
653 654 655 656 657
	{
		.name = "wm5102-codec",
		.parent_supplies = wm5102_supplies,
		.num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
	},
M
Mark Brown 已提交
658 659
};

660
static const struct mfd_cell wm5110_devs[] = {
661
	{ .name = "arizona-micsupp" },
662 663 664 665 666
	{
		.name = "arizona-extcon",
		.parent_supplies = wm5102_supplies,
		.num_parent_supplies = 1, /* We only need MICVDD */
	},
M
Mark Brown 已提交
667
	{ .name = "arizona-gpio" },
668
	{ .name = "arizona-haptics" },
M
Mark Brown 已提交
669
	{ .name = "arizona-pwm" },
670 671 672 673 674 675 676 677
	{
		.name = "wm5110-codec",
		.parent_supplies = wm5102_supplies,
		.num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
	},
};

static const char *wm8997_supplies[] = {
678
	"MICVDD",
679 680 681
	"DBVDD2",
	"CPVDD",
	"SPKVDD",
M
Mark Brown 已提交
682 683
};

684
static const struct mfd_cell wm8997_devs[] = {
685
	{ .name = "arizona-micsupp" },
686 687 688 689 690
	{
		.name = "arizona-extcon",
		.parent_supplies = wm8997_supplies,
		.num_parent_supplies = 1, /* We only need MICVDD */
	},
691 692 693
	{ .name = "arizona-gpio" },
	{ .name = "arizona-haptics" },
	{ .name = "arizona-pwm" },
694 695 696 697 698
	{
		.name = "wm8997-codec",
		.parent_supplies = wm8997_supplies,
		.num_parent_supplies = ARRAY_SIZE(wm8997_supplies),
	},
699 700
};

B
Bill Pemberton 已提交
701
int arizona_dev_init(struct arizona *arizona)
M
Mark Brown 已提交
702 703 704 705
{
	struct device *dev = arizona->dev;
	const char *type_name;
	unsigned int reg, val;
706
	int (*apply_patch)(struct arizona *) = NULL;
M
Mark Brown 已提交
707 708 709 710 711 712 713 714
	int ret, i;

	dev_set_drvdata(arizona->dev, arizona);
	mutex_init(&arizona->clk_lock);

	if (dev_get_platdata(arizona->dev))
		memcpy(&arizona->pdata, dev_get_platdata(arizona->dev),
		       sizeof(arizona->pdata));
715 716
	else
		arizona_of_get_core_pdata(arizona);
M
Mark Brown 已提交
717 718 719 720 721

	regcache_cache_only(arizona->regmap, true);

	switch (arizona->type) {
	case WM5102:
M
Mark Brown 已提交
722
	case WM5110:
723
	case WM8280:
724
	case WM8997:
M
Mark Brown 已提交
725 726 727 728 729 730 731 732 733 734 735
		for (i = 0; i < ARRAY_SIZE(wm5102_core_supplies); i++)
			arizona->core_supplies[i].supply
				= wm5102_core_supplies[i];
		arizona->num_core_supplies = ARRAY_SIZE(wm5102_core_supplies);
		break;
	default:
		dev_err(arizona->dev, "Unknown device type %d\n",
			arizona->type);
		return -EINVAL;
	}

736 737 738
	/* Mark DCVDD as external, LDO1 driver will clear if internal */
	arizona->external_dcvdd = true;

M
Mark Brown 已提交
739
	ret = mfd_add_devices(arizona->dev, -1, early_devs,
740
			      ARRAY_SIZE(early_devs), NULL, 0, NULL);
M
Mark Brown 已提交
741 742 743 744 745 746 747 748 749 750 751 752 753
	if (ret != 0) {
		dev_err(dev, "Failed to add early children: %d\n", ret);
		return ret;
	}

	ret = devm_regulator_bulk_get(dev, arizona->num_core_supplies,
				      arizona->core_supplies);
	if (ret != 0) {
		dev_err(dev, "Failed to request core supplies: %d\n",
			ret);
		goto err_early;
	}

754 755 756 757 758 759
	/**
	 * Don't use devres here because the only device we have to get
	 * against is the MFD device and DCVDD will likely be supplied by
	 * one of its children. Meaning that the regulator will be
	 * destroyed by the time devres calls regulator put.
	 */
760
	arizona->dcvdd = regulator_get(arizona->dev, "DCVDD");
761 762 763 764 765 766
	if (IS_ERR(arizona->dcvdd)) {
		ret = PTR_ERR(arizona->dcvdd);
		dev_err(dev, "Failed to request DCVDD: %d\n", ret);
		goto err_early;
	}

767 768
	if (arizona->pdata.reset) {
		/* Start out with /RESET low to put the chip into reset */
769 770 771
		ret = devm_gpio_request_one(arizona->dev, arizona->pdata.reset,
					    GPIOF_DIR_OUT | GPIOF_INIT_LOW,
					    "arizona /RESET");
772 773
		if (ret != 0) {
			dev_err(dev, "Failed to request /RESET: %d\n", ret);
774
			goto err_dcvdd;
775 776 777
		}
	}

M
Mark Brown 已提交
778 779 780 781 782
	ret = regulator_bulk_enable(arizona->num_core_supplies,
				    arizona->core_supplies);
	if (ret != 0) {
		dev_err(dev, "Failed to enable core supplies: %d\n",
			ret);
783
		goto err_dcvdd;
M
Mark Brown 已提交
784 785
	}

786 787 788 789 790 791
	ret = regulator_enable(arizona->dcvdd);
	if (ret != 0) {
		dev_err(dev, "Failed to enable DCVDD: %d\n", ret);
		goto err_enable;
	}

792
	arizona_disable_reset(arizona);
M
Mark Brown 已提交
793 794 795

	regcache_cache_only(arizona->regmap, false);

796
	/* Verify that this is a chip we know about */
M
Mark Brown 已提交
797 798 799
	ret = regmap_read(arizona->regmap, ARIZONA_SOFTWARE_RESET, &reg);
	if (ret != 0) {
		dev_err(dev, "Failed to read ID register: %d\n", ret);
800
		goto err_reset;
M
Mark Brown 已提交
801 802 803 804
	}

	switch (reg) {
	case 0x5102:
M
Mark Brown 已提交
805
	case 0x5110:
806
	case 0x8997:
M
Mark Brown 已提交
807
		break;
M
Mark Brown 已提交
808
	default:
809
		dev_err(arizona->dev, "Unknown device ID: %x\n", reg);
810
		goto err_reset;
M
Mark Brown 已提交
811 812 813 814 815 816 817
	}

	/* If we have a /RESET GPIO we'll already be reset */
	if (!arizona->pdata.reset) {
		ret = regmap_write(arizona->regmap, ARIZONA_SOFTWARE_RESET, 0);
		if (ret != 0) {
			dev_err(dev, "Failed to reset device: %d\n", ret);
818
			goto err_reset;
M
Mark Brown 已提交
819
		}
820

821
		msleep(1);
M
Mark Brown 已提交
822 823
	}

824
	/* Ensure device startup is complete */
825 826
	switch (arizona->type) {
	case WM5102:
827 828
		ret = regmap_read(arizona->regmap,
				  ARIZONA_WRITE_SEQUENCER_CTRL_3, &val);
829
		if (ret) {
830 831 832
			dev_err(dev,
				"Failed to check write sequencer state: %d\n",
				ret);
833 834 835 836
		} else if (val & 0x01) {
			ret = wm5102_clear_write_sequencer(arizona);
			if (ret)
				return ret;
837 838
		}
		break;
839 840 841 842 843 844 845 846
	default:
		break;
	}

	ret = arizona_wait_for_boot(arizona);
	if (ret) {
		dev_err(arizona->dev, "Device failed initial boot: %d\n", ret);
		goto err_reset;
847
	}
M
Mark Brown 已提交
848

849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
	/* Read the device ID information & do device specific stuff */
	ret = regmap_read(arizona->regmap, ARIZONA_SOFTWARE_RESET, &reg);
	if (ret != 0) {
		dev_err(dev, "Failed to read ID register: %d\n", ret);
		goto err_reset;
	}

	ret = regmap_read(arizona->regmap, ARIZONA_DEVICE_REVISION,
			  &arizona->rev);
	if (ret != 0) {
		dev_err(dev, "Failed to read revision register: %d\n", ret);
		goto err_reset;
	}
	arizona->rev &= ARIZONA_DEVICE_REVISION_MASK;

	switch (reg) {
#ifdef CONFIG_MFD_WM5102
	case 0x5102:
		type_name = "WM5102";
		if (arizona->type != WM5102) {
			dev_err(arizona->dev, "WM5102 registered as %d\n",
				arizona->type);
			arizona->type = WM5102;
		}
		apply_patch = wm5102_patch;
		arizona->rev &= 0x7;
		break;
#endif
#ifdef CONFIG_MFD_WM5110
	case 0x5110:
879 880 881 882 883 884 885 886 887
		switch (arizona->type) {
		case WM5110:
			type_name = "WM5110";
			break;
		case WM8280:
			type_name = "WM8280";
			break;
		default:
			type_name = "WM5110";
888 889 890
			dev_err(arizona->dev, "WM5110 registered as %d\n",
				arizona->type);
			arizona->type = WM5110;
891
			break;
892 893 894
		}
		apply_patch = wm5110_patch;
		break;
895 896 897 898 899 900 901 902 903 904 905
#endif
#ifdef CONFIG_MFD_WM8997
	case 0x8997:
		type_name = "WM8997";
		if (arizona->type != WM8997) {
			dev_err(arizona->dev, "WM8997 registered as %d\n",
				arizona->type);
			arizona->type = WM8997;
		}
		apply_patch = wm8997_patch;
		break;
906 907 908 909 910 911 912 913
#endif
	default:
		dev_err(arizona->dev, "Unknown device ID %x\n", reg);
		goto err_reset;
	}

	dev_info(dev, "%s revision %c\n", type_name, arizona->rev + 'A');

914 915 916 917 918 919 920
	if (apply_patch) {
		ret = apply_patch(arizona);
		if (ret != 0) {
			dev_err(arizona->dev, "Failed to apply patch: %d\n",
				ret);
			goto err_reset;
		}
921 922 923

		switch (arizona->type) {
		case WM5102:
924 925
			ret = wm5102_apply_hardware_patch(arizona);
			if (ret) {
926 927 928 929 930 931 932 933 934
				dev_err(arizona->dev,
					"Failed to apply hardware patch: %d\n",
					ret);
				goto err_reset;
			}
			break;
		default:
			break;
		}
935 936
	}

M
Mark Brown 已提交
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
	for (i = 0; i < ARRAY_SIZE(arizona->pdata.gpio_defaults); i++) {
		if (!arizona->pdata.gpio_defaults[i])
			continue;

		regmap_write(arizona->regmap, ARIZONA_GPIO1_CTRL + i,
			     arizona->pdata.gpio_defaults[i]);
	}

	pm_runtime_set_autosuspend_delay(arizona->dev, 100);
	pm_runtime_use_autosuspend(arizona->dev);
	pm_runtime_enable(arizona->dev);

	/* Chip default */
	if (!arizona->pdata.clk32k_src)
		arizona->pdata.clk32k_src = ARIZONA_32KZ_MCLK2;

	switch (arizona->pdata.clk32k_src) {
	case ARIZONA_32KZ_MCLK1:
	case ARIZONA_32KZ_MCLK2:
		regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
				   ARIZONA_CLK_32K_SRC_MASK,
				   arizona->pdata.clk32k_src - 1);
959
		arizona_clk32k_enable(arizona);
M
Mark Brown 已提交
960 961 962 963 964 965 966 967 968
		break;
	case ARIZONA_32KZ_NONE:
		regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
				   ARIZONA_CLK_32K_SRC_MASK, 2);
		break;
	default:
		dev_err(arizona->dev, "Invalid 32kHz clock source: %d\n",
			arizona->pdata.clk32k_src);
		ret = -EINVAL;
969
		goto err_reset;
M
Mark Brown 已提交
970 971
	}

972
	for (i = 0; i < ARIZONA_MAX_MICBIAS; i++) {
973 974
		if (!arizona->pdata.micbias[i].mV &&
		    !arizona->pdata.micbias[i].bypass)
975 976
			continue;

977 978 979 980
		/* Apply default for bypass mode */
		if (!arizona->pdata.micbias[i].mV)
			arizona->pdata.micbias[i].mV = 2800;

981
		val = (arizona->pdata.micbias[i].mV - 1500) / 100;
982

983 984 985 986 987 988 989 990
		val <<= ARIZONA_MICB1_LVL_SHIFT;

		if (arizona->pdata.micbias[i].ext_cap)
			val |= ARIZONA_MICB1_EXT_CAP;

		if (arizona->pdata.micbias[i].discharge)
			val |= ARIZONA_MICB1_DISCH;

991
		if (arizona->pdata.micbias[i].soft_start)
992 993
			val |= ARIZONA_MICB1_RATE;

994 995 996
		if (arizona->pdata.micbias[i].bypass)
			val |= ARIZONA_MICB1_BYPASS;

997 998 999
		regmap_update_bits(arizona->regmap,
				   ARIZONA_MIC_BIAS_CTRL_1 + i,
				   ARIZONA_MICB1_LVL_MASK |
1000
				   ARIZONA_MICB1_EXT_CAP |
1001
				   ARIZONA_MICB1_DISCH |
1002
				   ARIZONA_MICB1_BYPASS |
1003 1004 1005
				   ARIZONA_MICB1_RATE, val);
	}

M
Mark Brown 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
	for (i = 0; i < ARIZONA_MAX_INPUT; i++) {
		/* Default for both is 0 so noop with defaults */
		val = arizona->pdata.dmic_ref[i]
			<< ARIZONA_IN1_DMIC_SUP_SHIFT;
		val |= arizona->pdata.inmode[i] << ARIZONA_IN1_MODE_SHIFT;

		regmap_update_bits(arizona->regmap,
				   ARIZONA_IN1L_CONTROL + (i * 8),
				   ARIZONA_IN1_DMIC_SUP_MASK |
				   ARIZONA_IN1_MODE_MASK, val);
	}

	for (i = 0; i < ARIZONA_MAX_OUTPUT; i++) {
		/* Default is 0 so noop with defaults */
		if (arizona->pdata.out_mono[i])
			val = ARIZONA_OUT1_MONO;
		else
			val = 0;

		regmap_update_bits(arizona->regmap,
				   ARIZONA_OUTPUT_PATH_CONFIG_1L + (i * 8),
				   ARIZONA_OUT1_MONO, val);
	}

	for (i = 0; i < ARIZONA_MAX_PDM_SPK; i++) {
		if (arizona->pdata.spk_mute[i])
			regmap_update_bits(arizona->regmap,
1033
					   ARIZONA_PDM_SPK1_CTRL_1 + (i * 2),
M
Mark Brown 已提交
1034 1035 1036 1037 1038 1039
					   ARIZONA_SPK1_MUTE_ENDIAN_MASK |
					   ARIZONA_SPK1_MUTE_SEQ1_MASK,
					   arizona->pdata.spk_mute[i]);

		if (arizona->pdata.spk_fmt[i])
			regmap_update_bits(arizona->regmap,
1040
					   ARIZONA_PDM_SPK1_CTRL_2 + (i * 2),
M
Mark Brown 已提交
1041 1042 1043 1044 1045 1046 1047
					   ARIZONA_SPK1_FMT_MASK,
					   arizona->pdata.spk_fmt[i]);
	}

	/* Set up for interrupts */
	ret = arizona_irq_init(arizona);
	if (ret != 0)
1048
		goto err_reset;
M
Mark Brown 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059

	arizona_request_irq(arizona, ARIZONA_IRQ_CLKGEN_ERR, "CLKGEN error",
			    arizona_clkgen_err, arizona);
	arizona_request_irq(arizona, ARIZONA_IRQ_OVERCLOCKED, "Overclocked",
			    arizona_overclocked, arizona);
	arizona_request_irq(arizona, ARIZONA_IRQ_UNDERCLOCKED, "Underclocked",
			    arizona_underclocked, arizona);

	switch (arizona->type) {
	case WM5102:
		ret = mfd_add_devices(arizona->dev, -1, wm5102_devs,
1060
				      ARRAY_SIZE(wm5102_devs), NULL, 0, NULL);
M
Mark Brown 已提交
1061 1062
		break;
	case WM5110:
1063
	case WM8280:
M
Mark Brown 已提交
1064
		ret = mfd_add_devices(arizona->dev, -1, wm5110_devs,
1065
				      ARRAY_SIZE(wm5110_devs), NULL, 0, NULL);
M
Mark Brown 已提交
1066
		break;
1067 1068 1069 1070
	case WM8997:
		ret = mfd_add_devices(arizona->dev, -1, wm8997_devs,
				      ARRAY_SIZE(wm8997_devs), NULL, 0, NULL);
		break;
M
Mark Brown 已提交
1071 1072 1073 1074 1075 1076 1077
	}

	if (ret != 0) {
		dev_err(arizona->dev, "Failed to add subdevices: %d\n", ret);
		goto err_irq;
	}

1078
#ifdef CONFIG_PM
1079 1080 1081
	regulator_disable(arizona->dcvdd);
#endif

M
Mark Brown 已提交
1082 1083 1084 1085 1086
	return 0;

err_irq:
	arizona_irq_exit(arizona);
err_reset:
1087
	arizona_enable_reset(arizona);
1088
	regulator_disable(arizona->dcvdd);
M
Mark Brown 已提交
1089
err_enable:
1090
	regulator_bulk_disable(arizona->num_core_supplies,
M
Mark Brown 已提交
1091
			       arizona->core_supplies);
1092 1093
err_dcvdd:
	regulator_put(arizona->dcvdd);
M
Mark Brown 已提交
1094 1095 1096 1097 1098 1099
err_early:
	mfd_remove_devices(dev);
	return ret;
}
EXPORT_SYMBOL_GPL(arizona_dev_init);

B
Bill Pemberton 已提交
1100
int arizona_dev_exit(struct arizona *arizona)
M
Mark Brown 已提交
1101
{
1102 1103
	pm_runtime_disable(arizona->dev);

1104
	regulator_disable(arizona->dcvdd);
1105
	regulator_put(arizona->dcvdd);
1106

M
Mark Brown 已提交
1107 1108 1109 1110 1111
	mfd_remove_devices(arizona->dev);
	arizona_free_irq(arizona, ARIZONA_IRQ_UNDERCLOCKED, arizona);
	arizona_free_irq(arizona, ARIZONA_IRQ_OVERCLOCKED, arizona);
	arizona_free_irq(arizona, ARIZONA_IRQ_CLKGEN_ERR, arizona);
	arizona_irq_exit(arizona);
1112
	arizona_enable_reset(arizona);
1113

1114
	regulator_bulk_disable(arizona->num_core_supplies,
1115
			       arizona->core_supplies);
M
Mark Brown 已提交
1116 1117 1118
	return 0;
}
EXPORT_SYMBOL_GPL(arizona_dev_exit);