arizona-core.c 37.3 KB
Newer Older
M
Mark Brown 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * 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.
 */

13
#include <linux/clk.h>
M
Mark Brown 已提交
14
#include <linux/delay.h>
15
#include <linux/err.h>
M
Mark Brown 已提交
16 17 18 19
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/mfd/core.h>
#include <linux/module.h>
20 21 22
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
M
Mark Brown 已提交
23 24 25
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
26
#include <linux/regulator/machine.h>
M
Mark Brown 已提交
27
#include <linux/slab.h>
28
#include <linux/platform_device.h>
M
Mark Brown 已提交
29 30 31 32 33 34

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

#include "arizona.h"

35
static const char * const wm5102_core_supplies[] = {
M
Mark Brown 已提交
36 37 38 39 40 41 42 43 44 45 46 47
	"AVDD",
	"DBVDD1",
};

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

	mutex_lock(&arizona->clk_lock);

	arizona->clk32k_ref++;

48 49 50 51 52
	if (arizona->clk32k_ref == 1) {
		switch (arizona->pdata.clk32k_src) {
		case ARIZONA_32KZ_MCLK1:
			ret = pm_runtime_get_sync(arizona->dev);
			if (ret != 0)
53 54 55 56 57 58 59 60 61
				goto err_ref;
			ret = clk_prepare_enable(arizona->mclk[ARIZONA_MCLK1]);
			if (ret != 0)
				goto err_pm;
			break;
		case ARIZONA_32KZ_MCLK2:
			ret = clk_prepare_enable(arizona->mclk[ARIZONA_MCLK2]);
			if (ret != 0)
				goto err_ref;
62 63 64
			break;
		}

M
Mark Brown 已提交
65 66 67
		ret = regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
					 ARIZONA_CLK_32K_ENA,
					 ARIZONA_CLK_32K_ENA);
68
	}
M
Mark Brown 已提交
69

70 71 72
err_pm:
	pm_runtime_put_sync(arizona->dev);
err_ref:
M
Mark Brown 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	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)
{
	mutex_lock(&arizona->clk_lock);

	BUG_ON(arizona->clk32k_ref <= 0);

	arizona->clk32k_ref--;

90
	if (arizona->clk32k_ref == 0) {
M
Mark Brown 已提交
91 92 93
		regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
				   ARIZONA_CLK_32K_ENA, 0);

94 95 96
		switch (arizona->pdata.clk32k_src) {
		case ARIZONA_32KZ_MCLK1:
			pm_runtime_put_sync(arizona->dev);
97 98 99 100
			clk_disable_unprepare(arizona->mclk[ARIZONA_MCLK1]);
			break;
		case ARIZONA_32KZ_MCLK2:
			clk_disable_unprepare(arizona->mclk[ARIZONA_MCLK2]);
101 102 103 104
			break;
		}
	}

M
Mark Brown 已提交
105 106
	mutex_unlock(&arizona->clk_lock);

107
	return 0;
M
Mark Brown 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
}
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)
137 138
		dev_err(arizona->dev, "AIF2 underclocked\n");
	if (val & ARIZONA_AIF1_UNDERCLOCKED_STS)
M
Mark Brown 已提交
139
		dev_err(arizona->dev, "AIF1 underclocked\n");
140 141
	if (val & ARIZONA_ISRC3_UNDERCLOCKED_STS)
		dev_err(arizona->dev, "ISRC3 underclocked\n");
M
Mark Brown 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154
	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)
155
		dev_err(arizona->dev, "Mixer dropped sample\n");
M
Mark Brown 已提交
156 157 158 159 160 161 162

	return IRQ_HANDLED;
}

static irqreturn_t arizona_overclocked(int irq, void *data)
{
	struct arizona *arizona = data;
163
	unsigned int val[3];
M
Mark Brown 已提交
164
	int ret;
165

M
Mark Brown 已提交
166
	ret = regmap_bulk_read(arizona->regmap, ARIZONA_INTERRUPT_RAW_STATUS_6,
167
			       &val[0], 3);
M
Mark Brown 已提交
168 169 170 171 172 173
	if (ret != 0) {
		dev_err(arizona->dev, "Failed to read overclock status: %d\n",
			ret);
		return IRQ_NONE;
	}

174 175 176 177 178 179 180 181 182 183 184 185 186 187
	switch (arizona->type) {
	case WM8998:
	case WM1814:
		/* Some bits are shifted on WM8998,
		 * rearrange to match the standard bit layout
		 */
		val[0] = ((val[0] & 0x60e0) >> 1) |
			 ((val[0] & 0x1e00) >> 2) |
			 (val[0] & 0x000f);
		break;
	default:
		break;
	}

M
Mark Brown 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
	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");
225 226
	if (val[1] & ARIZONA_ISRC3_OVERCLOCKED_STS)
		dev_err(arizona->dev, "ISRC3 overclocked\n");
M
Mark Brown 已提交
227 228 229 230 231
	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");

232 233 234
	if (val[2] & ARIZONA_SPDIF_OVERCLOCKED_STS)
		dev_err(arizona->dev, "SPDIF overclocked\n");

M
Mark Brown 已提交
235 236 237
	return IRQ_HANDLED;
}

238 239 240
static int arizona_poll_reg(struct arizona *arizona,
			    int timeout, unsigned int reg,
			    unsigned int mask, unsigned int target)
M
Mark Brown 已提交
241
{
242
	unsigned int val = 0;
M
Mark Brown 已提交
243 244
	int ret, i;

245 246
	for (i = 0; i < timeout; i++) {
		ret = regmap_read(arizona->regmap, reg, &val);
M
Mark Brown 已提交
247
		if (ret != 0) {
248 249
			dev_err(arizona->dev, "Failed to read reg %u: %d\n",
				reg, ret);
250
			continue;
M
Mark Brown 已提交
251 252
		}

253 254 255
		if ((val & mask) == target)
			return 0;

256
		usleep_range(1000, 5000);
M
Mark Brown 已提交
257 258
	}

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	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 已提交
276 277 278 279 280
		regmap_write(arizona->regmap, ARIZONA_INTERRUPT_STATUS_5,
			     ARIZONA_BOOT_DONE_STS);

	pm_runtime_mark_last_busy(arizona->dev);

281
	return ret;
M
Mark Brown 已提交
282 283
}

284 285 286 287 288 289 290 291 292
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) {
293 294 295 296
		switch (arizona->type) {
		case WM5110:
		case WM8280:
			/* Meet requirements for minimum reset duration */
297
			usleep_range(5000, 10000);
298 299 300 301 302
			break;
		default:
			break;
		}

303
		gpio_set_value_cansleep(arizona->pdata.reset, 1);
304
		usleep_range(1000, 5000);
305 306 307
	}
}

308 309 310 311 312 313 314
struct arizona_sysclk_state {
	unsigned int fll;
	unsigned int sysclk;
};

static int arizona_enable_freerun_sysclk(struct arizona *arizona,
					 struct arizona_sysclk_state *state)
315 316 317 318
{
	int ret, err;

	/* Cache existing FLL and SYSCLK settings */
319
	ret = regmap_read(arizona->regmap, ARIZONA_FLL1_CONTROL_1, &state->fll);
320
	if (ret) {
321 322 323 324
		dev_err(arizona->dev, "Failed to cache FLL settings: %d\n",
			ret);
		return ret;
	}
325 326
	ret = regmap_read(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1,
			  &state->sysclk);
327
	if (ret) {
328 329 330 331 332 333 334 335
		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);
336
	if (ret) {
337 338 339 340 341 342 343 344
		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);
345
	if (ret) {
346 347 348 349 350
		ret = -ETIMEDOUT;
		goto err_fll;
	}

	ret = regmap_write(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1, 0x0144);
351
	if (ret) {
352 353 354 355
		dev_err(arizona->dev, "Failed to start SYSCLK: %d\n", ret);
		goto err_fll;
	}

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
	return 0;

err_fll:
	err = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1, state->fll);
	if (err)
		dev_err(arizona->dev,
			"Failed to re-apply old FLL settings: %d\n", err);

	return ret;
}

static int arizona_disable_freerun_sysclk(struct arizona *arizona,
					  struct arizona_sysclk_state *state)
{
	int ret;

	ret = regmap_write(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1,
			   state->sysclk);
	if (ret) {
		dev_err(arizona->dev,
			"Failed to re-apply old SYSCLK settings: %d\n", ret);
		return ret;
	}

	ret = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1, state->fll);
	if (ret) {
		dev_err(arizona->dev,
			"Failed to re-apply old FLL settings: %d\n", ret);
		return ret;
	}

	return 0;
}

static int wm5102_apply_hardware_patch(struct arizona *arizona)
{
	struct arizona_sysclk_state state;
	int err, ret;

	ret = arizona_enable_freerun_sysclk(arizona, &state);
	if (ret)
		return ret;

399 400
	/* Start the write sequencer and wait for it to finish */
	ret = regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_0,
401 402
			   ARIZONA_WSEQ_ENA | ARIZONA_WSEQ_START | 160);
	if (ret) {
403 404
		dev_err(arizona->dev, "Failed to start write sequencer: %d\n",
			ret);
405
		goto err;
406
	}
407

408 409
	ret = arizona_poll_reg(arizona, 5, ARIZONA_WRITE_SEQUENCER_CTRL_1,
			       ARIZONA_WSEQ_BUSY, 0);
410
	if (ret) {
411
		regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_0,
412
			     ARIZONA_WSEQ_ABORT);
413 414 415
		ret = -ETIMEDOUT;
	}

416 417
err:
	err = arizona_disable_freerun_sysclk(arizona, &state);
418

419
	return ret ?: err;
420 421
}

422 423 424 425
/*
 * Register patch to some of the CODECs internal write sequences
 * to ensure a clean exit from the low power sleep state.
 */
426
static const struct reg_sequence wm5110_sleep_patch[] = {
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
	{ 0x337A, 0xC100 },
	{ 0x337B, 0x0041 },
	{ 0x3300, 0xA210 },
	{ 0x3301, 0x050C },
};

static int wm5110_apply_sleep_patch(struct arizona *arizona)
{
	struct arizona_sysclk_state state;
	int err, ret;

	ret = arizona_enable_freerun_sysclk(arizona, &state);
	if (ret)
		return ret;

	ret = regmap_multi_reg_write_bypassed(arizona->regmap,
					      wm5110_sleep_patch,
					      ARRAY_SIZE(wm5110_sleep_patch));

	err = arizona_disable_freerun_sysclk(arizona, &state);

	return ret ?: err;
}

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
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;
}

478
#ifdef CONFIG_PM
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
static int arizona_isolate_dcvdd(struct arizona *arizona)
{
	int ret;

	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;
}

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

	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);

	return ret;
}

506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
static int arizona_is_jack_det_active(struct arizona *arizona)
{
	unsigned int val;
	int ret;

	ret = regmap_read(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE, &val);
	if (ret) {
		dev_err(arizona->dev,
			"Failed to check jack det status: %d\n", ret);
		return ret;
	} else if (val & ARIZONA_JD1_ENA) {
		return 1;
	} else {
		return 0;
	}
}

M
Mark Brown 已提交
523 524 525 526 527
static int arizona_runtime_resume(struct device *dev)
{
	struct arizona *arizona = dev_get_drvdata(dev);
	int ret;

528 529
	dev_dbg(arizona->dev, "Leaving AoD mode\n");

530 531 532 533 534 535 536 537 538 539 540 541
	if (arizona->has_fully_powered_off) {
		dev_dbg(arizona->dev, "Re-enabling core supplies\n");

		ret = regulator_bulk_enable(arizona->num_core_supplies,
					    arizona->core_supplies);
		if (ret) {
			dev_err(dev, "Failed to enable core supplies: %d\n",
				ret);
			return ret;
		}
	}

542 543 544
	ret = regulator_enable(arizona->dcvdd);
	if (ret != 0) {
		dev_err(arizona->dev, "Failed to enable DCVDD: %d\n", ret);
545 546 547
		if (arizona->has_fully_powered_off)
			regulator_bulk_disable(arizona->num_core_supplies,
					       arizona->core_supplies);
548 549
		return ret;
	}
M
Mark Brown 已提交
550

551 552 553 554 555 556
	if (arizona->has_fully_powered_off) {
		arizona_disable_reset(arizona);
		enable_irq(arizona->irq);
		arizona->has_fully_powered_off = false;
	}

M
Mark Brown 已提交
557 558
	regcache_cache_only(arizona->regmap, false);

559 560
	switch (arizona->type) {
	case WM5102:
561
		if (arizona->external_dcvdd) {
562 563
			ret = arizona_connect_dcvdd(arizona);
			if (ret != 0)
564 565 566
				goto err;
		}

567 568 569 570 571 572
		ret = wm5102_patch(arizona);
		if (ret != 0) {
			dev_err(arizona->dev, "Failed to apply patch: %d\n",
				ret);
			goto err;
		}
573

574 575
		ret = wm5102_apply_hardware_patch(arizona);
		if (ret) {
576 577 578 579 580 581
			dev_err(arizona->dev,
				"Failed to apply hardware patch: %d\n",
				ret);
			goto err;
		}
		break;
582 583 584 585 586 587 588
	case WM5110:
	case WM8280:
		ret = arizona_wait_for_boot(arizona);
		if (ret)
			goto err;

		if (arizona->external_dcvdd) {
589 590
			ret = arizona_connect_dcvdd(arizona);
			if (ret != 0)
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
				goto err;
		} else {
			/*
			 * As this is only called for the internal regulator
			 * (where we know voltage ranges available) it is ok
			 * to request an exact range.
			 */
			ret = regulator_set_voltage(arizona->dcvdd,
						    1200000, 1200000);
			if (ret < 0) {
				dev_err(arizona->dev,
					"Failed to set resume voltage: %d\n",
					ret);
				goto err;
			}
		}
607 608 609 610 611 612 613 614

		ret = wm5110_apply_sleep_patch(arizona);
		if (ret) {
			dev_err(arizona->dev,
				"Failed to re-apply sleep patch: %d\n",
				ret);
			goto err;
		}
615
		break;
616 617 618 619 620 621
	case WM1831:
	case CS47L24:
		ret = arizona_wait_for_boot(arizona);
		if (ret != 0)
			goto err;
		break;
622
	default:
623
		ret = arizona_wait_for_boot(arizona);
624
		if (ret != 0)
625 626
			goto err;

627
		if (arizona->external_dcvdd) {
628 629
			ret = arizona_connect_dcvdd(arizona);
			if (ret != 0)
630 631
				goto err;
		}
632
		break;
633 634
	}

635 636 637
	ret = regcache_sync(arizona->regmap);
	if (ret != 0) {
		dev_err(arizona->dev, "Failed to restore register cache\n");
638
		goto err;
639
	}
M
Mark Brown 已提交
640 641

	return 0;
642 643 644 645 646

err:
	regcache_cache_only(arizona->regmap, true);
	regulator_disable(arizona->dcvdd);
	return ret;
M
Mark Brown 已提交
647 648 649 650 651
}

static int arizona_runtime_suspend(struct device *dev)
{
	struct arizona *arizona = dev_get_drvdata(dev);
652
	int jd_active = 0;
653
	int ret;
M
Mark Brown 已提交
654

655 656
	dev_dbg(arizona->dev, "Entering AoD mode\n");

657 658 659
	switch (arizona->type) {
	case WM5110:
	case WM8280:
660 661 662 663
		jd_active = arizona_is_jack_det_active(arizona);
		if (jd_active < 0)
			return jd_active;

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
		if (arizona->external_dcvdd) {
			ret = arizona_isolate_dcvdd(arizona);
			if (ret != 0)
				return ret;
		} else {
			/*
			 * As this is only called for the internal regulator
			 * (where we know voltage ranges available) it is ok
			 * to request an exact range.
			 */
			ret = regulator_set_voltage(arizona->dcvdd,
						    1175000, 1175000);
			if (ret < 0) {
				dev_err(arizona->dev,
					"Failed to set suspend voltage: %d\n",
					ret);
				return ret;
			}
682 683 684
		}
		break;
	case WM5102:
685 686 687 688
		jd_active = arizona_is_jack_det_active(arizona);
		if (jd_active < 0)
			return jd_active;

689 690 691 692 693 694
		if (arizona->external_dcvdd) {
			ret = arizona_isolate_dcvdd(arizona);
			if (ret != 0)
				return ret;
		}

695
		if (!jd_active) {
696 697 698
			ret = regmap_write(arizona->regmap,
					   ARIZONA_WRITE_SEQUENCER_CTRL_3, 0x0);
			if (ret) {
699
				dev_err(arizona->dev,
700
					"Failed to clear write sequencer: %d\n",
701 702 703 704
					ret);
				return ret;
			}
		}
705
		break;
706 707 708
	case WM1831:
	case CS47L24:
		break;
709
	default:
710 711 712 713
		jd_active = arizona_is_jack_det_active(arizona);
		if (jd_active < 0)
			return jd_active;

714 715 716 717 718
		if (arizona->external_dcvdd) {
			ret = arizona_isolate_dcvdd(arizona);
			if (ret != 0)
				return ret;
		}
719
		break;
720 721
	}

722 723
	regcache_cache_only(arizona->regmap, true);
	regcache_mark_dirty(arizona->regmap);
724
	regulator_disable(arizona->dcvdd);
M
Mark Brown 已提交
725

726
	/* Allow us to completely power down if no jack detection */
727
	if (!jd_active) {
728 729 730 731
		dev_dbg(arizona->dev, "Fully powering off\n");

		arizona->has_fully_powered_off = true;

732
		disable_irq_nosync(arizona->irq);
733 734 735 736 737
		arizona_enable_reset(arizona);
		regulator_bulk_disable(arizona->num_core_supplies,
				       arizona->core_supplies);
	}

M
Mark Brown 已提交
738 739 740 741
	return 0;
}
#endif

742
#ifdef CONFIG_PM_SLEEP
743 744 745 746 747 748 749 750 751 752
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;
}

753
static int arizona_suspend_noirq(struct device *dev)
754 755 756 757 758 759 760 761 762
{
	struct arizona *arizona = dev_get_drvdata(dev);

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

	return 0;
}

763 764 765 766 767 768 769 770 771 772 773 774 775 776
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);

777
	dev_dbg(arizona->dev, "Resume, reenabling IRQ\n");
778 779 780 781 782 783
	enable_irq(arizona->irq);

	return 0;
}
#endif

M
Mark Brown 已提交
784 785 786 787
const struct dev_pm_ops arizona_pm_ops = {
	SET_RUNTIME_PM_OPS(arizona_runtime_suspend,
			   arizona_runtime_resume,
			   NULL)
788
	SET_SYSTEM_SLEEP_PM_OPS(arizona_suspend, arizona_resume)
789 790
	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(arizona_suspend_noirq,
				      arizona_resume_noirq)
M
Mark Brown 已提交
791 792 793
};
EXPORT_SYMBOL_GPL(arizona_pm_ops);

794
#ifdef CONFIG_OF
795
unsigned long arizona_of_get_type(struct device *dev)
796 797 798 799
{
	const struct of_device_id *id = of_match_device(arizona_of_match, dev);

	if (id)
800
		return (unsigned long)id->data;
801 802 803 804 805
	else
		return 0;
}
EXPORT_SYMBOL_GPL(arizona_of_get_type);

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
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);

825 826
static int arizona_of_get_core_pdata(struct arizona *arizona)
{
827
	struct arizona_pdata *pdata = &arizona->pdata;
828 829 830
	struct property *prop;
	const __be32 *cur;
	u32 val;
831
	u32 pdm_val[ARIZONA_MAX_PDM_SPK];
832
	int ret, i;
833
	int count = 0;
834

835
	pdata->reset = arizona_of_get_named_gpio(arizona, "wlf,reset", true);
836 837 838

	ret = of_property_read_u32_array(arizona->dev->of_node,
					 "wlf,gpio-defaults",
839 840
					 pdata->gpio_defaults,
					 ARRAY_SIZE(pdata->gpio_defaults));
841 842 843 844 845 846 847
	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.
		 */
848 849 850 851 852
		for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
			if (pdata->gpio_defaults[i] > 0xffff)
				pdata->gpio_defaults[i] = 0;
			else if (pdata->gpio_defaults[i] == 0)
				pdata->gpio_defaults[i] = 0x10000;
853 854 855 856 857 858
		}
	} else {
		dev_err(arizona->dev, "Failed to parse GPIO defaults: %d\n",
			ret);
	}

859 860
	of_property_for_each_u32(arizona->dev->of_node, "wlf,inmode", prop,
				 cur, val) {
861
		if (count == ARRAY_SIZE(pdata->inmode))
862 863
			break;

864
		pdata->inmode[count] = val;
865 866 867
		count++;
	}

868 869 870
	count = 0;
	of_property_for_each_u32(arizona->dev->of_node, "wlf,dmic-ref", prop,
				 cur, val) {
871
		if (count == ARRAY_SIZE(pdata->dmic_ref))
872 873
			break;

874
		pdata->dmic_ref[count] = val;
875 876 877
		count++;
	}

878 879 880 881 882 883 884 885 886 887
	count = 0;
	of_property_for_each_u32(arizona->dev->of_node, "wlf,out-mono", prop,
				 cur, val) {
		if (count == ARRAY_SIZE(pdata->out_mono))
			break;

		pdata->out_mono[count] = !!val;
		count++;
	}

888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
	count = 0;
	of_property_for_each_u32(arizona->dev->of_node,
				 "wlf,max-channels-clocked",
				 prop, cur, val) {
		if (count == ARRAY_SIZE(pdata->max_channels_clocked))
			break;

		pdata->max_channels_clocked[count] = val;
		count++;
	}

	ret = of_property_read_u32_array(arizona->dev->of_node,
					 "wlf,spk-fmt",
					 pdm_val,
					 ARRAY_SIZE(pdm_val));

	if (ret >= 0)
		for (count = 0; count < ARRAY_SIZE(pdata->spk_fmt); ++count)
			pdata->spk_fmt[count] = pdm_val[count];

	ret = of_property_read_u32_array(arizona->dev->of_node,
					 "wlf,spk-mute",
					 pdm_val,
					 ARRAY_SIZE(pdm_val));

	if (ret >= 0)
		for (count = 0; count < ARRAY_SIZE(pdata->spk_mute); ++count)
			pdata->spk_mute[count] = pdm_val[count];

917 918 919 920 921 922
	return 0;
}

const struct of_device_id arizona_of_match[] = {
	{ .compatible = "wlf,wm5102", .data = (void *)WM5102 },
	{ .compatible = "wlf,wm5110", .data = (void *)WM5110 },
923
	{ .compatible = "wlf,wm8280", .data = (void *)WM8280 },
924
	{ .compatible = "wlf,wm8997", .data = (void *)WM8997 },
925 926
	{ .compatible = "wlf,wm8998", .data = (void *)WM8998 },
	{ .compatible = "wlf,wm1814", .data = (void *)WM1814 },
927 928
	{ .compatible = "wlf,wm1831", .data = (void *)WM1831 },
	{ .compatible = "cirrus,cs47l24", .data = (void *)CS47L24 },
929 930 931 932 933 934 935 936 937 938
	{},
};
EXPORT_SYMBOL_GPL(arizona_of_match);
#else
static inline int arizona_of_get_core_pdata(struct arizona *arizona)
{
	return 0;
}
#endif

939
static const struct mfd_cell early_devs[] = {
M
Mark Brown 已提交
940 941 942
	{ .name = "arizona-ldo1" },
};

943
static const char * const wm5102_supplies[] = {
944
	"MICVDD",
945 946 947 948 949 950 951
	"DBVDD2",
	"DBVDD3",
	"CPVDD",
	"SPKVDDL",
	"SPKVDDR",
};

952
static const struct mfd_cell wm5102_devs[] = {
953
	{ .name = "arizona-micsupp" },
954
	{ .name = "arizona-gpio" },
955 956 957 958 959
	{
		.name = "arizona-extcon",
		.parent_supplies = wm5102_supplies,
		.num_parent_supplies = 1, /* We only need MICVDD */
	},
960
	{ .name = "arizona-haptics" },
M
Mark Brown 已提交
961
	{ .name = "arizona-pwm" },
962 963 964 965 966
	{
		.name = "wm5102-codec",
		.parent_supplies = wm5102_supplies,
		.num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
	},
M
Mark Brown 已提交
967 968
};

969
static const struct mfd_cell wm5110_devs[] = {
970
	{ .name = "arizona-micsupp" },
971
	{ .name = "arizona-gpio" },
972 973 974 975 976
	{
		.name = "arizona-extcon",
		.parent_supplies = wm5102_supplies,
		.num_parent_supplies = 1, /* We only need MICVDD */
	},
977
	{ .name = "arizona-haptics" },
M
Mark Brown 已提交
978
	{ .name = "arizona-pwm" },
979 980 981 982 983 984 985
	{
		.name = "wm5110-codec",
		.parent_supplies = wm5102_supplies,
		.num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
	},
};

986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
static const char * const cs47l24_supplies[] = {
	"MICVDD",
	"CPVDD",
	"SPKVDD",
};

static const struct mfd_cell cs47l24_devs[] = {
	{ .name = "arizona-gpio" },
	{ .name = "arizona-haptics" },
	{ .name = "arizona-pwm" },
	{
		.name = "cs47l24-codec",
		.parent_supplies = cs47l24_supplies,
		.num_parent_supplies = ARRAY_SIZE(cs47l24_supplies),
	},
};

1003
static const char * const wm8997_supplies[] = {
1004
	"MICVDD",
1005 1006 1007
	"DBVDD2",
	"CPVDD",
	"SPKVDD",
M
Mark Brown 已提交
1008 1009
};

1010
static const struct mfd_cell wm8997_devs[] = {
1011
	{ .name = "arizona-micsupp" },
1012
	{ .name = "arizona-gpio" },
1013 1014 1015 1016 1017
	{
		.name = "arizona-extcon",
		.parent_supplies = wm8997_supplies,
		.num_parent_supplies = 1, /* We only need MICVDD */
	},
1018 1019
	{ .name = "arizona-haptics" },
	{ .name = "arizona-pwm" },
1020 1021 1022 1023 1024
	{
		.name = "wm8997-codec",
		.parent_supplies = wm8997_supplies,
		.num_parent_supplies = ARRAY_SIZE(wm8997_supplies),
	},
1025 1026
};

1027
static const struct mfd_cell wm8998_devs[] = {
1028 1029
	{ .name = "arizona-micsupp" },
	{ .name = "arizona-gpio" },
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
	{
		.name = "arizona-extcon",
		.parent_supplies = wm5102_supplies,
		.num_parent_supplies = 1, /* We only need MICVDD */
	},
	{ .name = "arizona-haptics" },
	{ .name = "arizona-pwm" },
	{
		.name = "wm8998-codec",
		.parent_supplies = wm5102_supplies,
		.num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
	},
};

B
Bill Pemberton 已提交
1044
int arizona_dev_init(struct arizona *arizona)
M
Mark Brown 已提交
1045
{
1046
	const char * const mclk_name[] = { "mclk1", "mclk2" };
M
Mark Brown 已提交
1047
	struct device *dev = arizona->dev;
1048
	const char *type_name = NULL;
1049
	unsigned int reg, val, mask;
1050
	int (*apply_patch)(struct arizona *) = NULL;
1051 1052
	const struct mfd_cell *subdevs = NULL;
	int n_subdevs, ret, i;
M
Mark Brown 已提交
1053 1054 1055 1056 1057 1058 1059

	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));
1060 1061
	else
		arizona_of_get_core_pdata(arizona);
M
Mark Brown 已提交
1062

1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
	BUILD_BUG_ON(ARRAY_SIZE(arizona->mclk) != ARRAY_SIZE(mclk_name));
	for (i = 0; i < ARRAY_SIZE(arizona->mclk); i++) {
		arizona->mclk[i] = devm_clk_get(arizona->dev, mclk_name[i]);
		if (IS_ERR(arizona->mclk[i])) {
			dev_info(arizona->dev, "Failed to get %s: %ld\n",
				 mclk_name[i], PTR_ERR(arizona->mclk[i]));
			arizona->mclk[i] = NULL;
		}
	}

M
Mark Brown 已提交
1073 1074 1075 1076
	regcache_cache_only(arizona->regmap, true);

	switch (arizona->type) {
	case WM5102:
M
Mark Brown 已提交
1077
	case WM5110:
1078
	case WM8280:
1079
	case WM8997:
1080 1081
	case WM8998:
	case WM1814:
1082 1083
	case WM1831:
	case CS47L24:
M
Mark Brown 已提交
1084 1085 1086 1087 1088 1089 1090 1091
		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);
1092
		return -ENODEV;
M
Mark Brown 已提交
1093 1094
	}

1095 1096 1097
	/* Mark DCVDD as external, LDO1 driver will clear if internal */
	arizona->external_dcvdd = true;

1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
	switch (arizona->type) {
	case WM1831:
	case CS47L24:
		break; /* No LDO1 regulator */
	default:
		ret = mfd_add_devices(arizona->dev, -1, early_devs,
				      ARRAY_SIZE(early_devs), NULL, 0, NULL);
		if (ret != 0) {
			dev_err(dev, "Failed to add early children: %d\n", ret);
			return ret;
		}
		break;
M
Mark Brown 已提交
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
	}

	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;
	}

1120 1121 1122 1123 1124 1125
	/**
	 * 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.
	 */
1126
	arizona->dcvdd = regulator_get(arizona->dev, "DCVDD");
1127 1128 1129 1130 1131 1132
	if (IS_ERR(arizona->dcvdd)) {
		ret = PTR_ERR(arizona->dcvdd);
		dev_err(dev, "Failed to request DCVDD: %d\n", ret);
		goto err_early;
	}

1133 1134
	if (arizona->pdata.reset) {
		/* Start out with /RESET low to put the chip into reset */
1135 1136 1137
		ret = devm_gpio_request_one(arizona->dev, arizona->pdata.reset,
					    GPIOF_DIR_OUT | GPIOF_INIT_LOW,
					    "arizona /RESET");
1138 1139
		if (ret != 0) {
			dev_err(dev, "Failed to request /RESET: %d\n", ret);
1140
			goto err_dcvdd;
1141 1142 1143
		}
	}

M
Mark Brown 已提交
1144 1145 1146 1147 1148
	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);
1149
		goto err_dcvdd;
M
Mark Brown 已提交
1150 1151
	}

1152 1153 1154 1155 1156 1157
	ret = regulator_enable(arizona->dcvdd);
	if (ret != 0) {
		dev_err(dev, "Failed to enable DCVDD: %d\n", ret);
		goto err_enable;
	}

1158
	arizona_disable_reset(arizona);
M
Mark Brown 已提交
1159 1160 1161

	regcache_cache_only(arizona->regmap, false);

1162
	/* Verify that this is a chip we know about */
M
Mark Brown 已提交
1163 1164 1165
	ret = regmap_read(arizona->regmap, ARIZONA_SOFTWARE_RESET, &reg);
	if (ret != 0) {
		dev_err(dev, "Failed to read ID register: %d\n", ret);
1166
		goto err_reset;
M
Mark Brown 已提交
1167 1168 1169 1170
	}

	switch (reg) {
	case 0x5102:
M
Mark Brown 已提交
1171
	case 0x5110:
1172
	case 0x6349:
1173
	case 0x6363:
1174
	case 0x8997:
M
Mark Brown 已提交
1175
		break;
M
Mark Brown 已提交
1176
	default:
1177
		dev_err(arizona->dev, "Unknown device ID: %x\n", reg);
1178
		ret = -ENODEV;
1179
		goto err_reset;
M
Mark Brown 已提交
1180 1181 1182 1183 1184 1185 1186
	}

	/* 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);
1187
			goto err_reset;
M
Mark Brown 已提交
1188
		}
1189

1190
		usleep_range(1000, 5000);
M
Mark Brown 已提交
1191 1192
	}

1193
	/* Ensure device startup is complete */
1194 1195
	switch (arizona->type) {
	case WM5102:
1196 1197
		ret = regmap_read(arizona->regmap,
				  ARIZONA_WRITE_SEQUENCER_CTRL_3, &val);
1198
		if (ret) {
1199 1200 1201
			dev_err(dev,
				"Failed to check write sequencer state: %d\n",
				ret);
1202 1203 1204 1205
		} else if (val & 0x01) {
			ret = wm5102_clear_write_sequencer(arizona);
			if (ret)
				return ret;
1206 1207
		}
		break;
1208 1209 1210 1211 1212 1213 1214 1215
	default:
		break;
	}

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

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
	/* 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) {
	case 0x5102:
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
		if (IS_ENABLED(CONFIG_MFD_WM5102)) {
			type_name = "WM5102";
			if (arizona->type != WM5102) {
				dev_warn(arizona->dev,
					 "WM5102 registered as %d\n",
					 arizona->type);
				arizona->type = WM5102;
			}

			apply_patch = wm5102_patch;
			arizona->rev &= 0x7;
			subdevs = wm5102_devs;
			n_subdevs = ARRAY_SIZE(wm5102_devs);
1248 1249 1250
		}
		break;
	case 0x5110:
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
		if (IS_ENABLED(CONFIG_MFD_WM5110)) {
			switch (arizona->type) {
			case WM5110:
				type_name = "WM5110";
				break;
			case WM8280:
				type_name = "WM8280";
				break;
			default:
				type_name = "WM5110";
				dev_warn(arizona->dev,
					 "WM5110 registered as %d\n",
					 arizona->type);
				arizona->type = WM5110;
				break;
			}

			apply_patch = wm5110_patch;
			subdevs = wm5110_devs;
			n_subdevs = ARRAY_SIZE(wm5110_devs);
1271 1272
		}
		break;
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
	case 0x6363:
		if (IS_ENABLED(CONFIG_MFD_CS47L24)) {
			switch (arizona->type) {
			case CS47L24:
				type_name = "CS47L24";
				break;

			case WM1831:
				type_name = "WM1831";
				break;

			default:
				dev_warn(arizona->dev,
					 "CS47L24 registered as %d\n",
					 arizona->type);
				arizona->type = CS47L24;
				break;
			}

			apply_patch = cs47l24_patch;
			subdevs = cs47l24_devs;
			n_subdevs = ARRAY_SIZE(cs47l24_devs);
		}
		break;
1297
	case 0x8997:
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
		if (IS_ENABLED(CONFIG_MFD_WM8997)) {
			type_name = "WM8997";
			if (arizona->type != WM8997) {
				dev_warn(arizona->dev,
					 "WM8997 registered as %d\n",
					 arizona->type);
				arizona->type = WM8997;
			}

			apply_patch = wm8997_patch;
			subdevs = wm8997_devs;
			n_subdevs = ARRAY_SIZE(wm8997_devs);
1310 1311
		}
		break;
1312
	case 0x6349:
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
		if (IS_ENABLED(CONFIG_MFD_WM8998)) {
			switch (arizona->type) {
			case WM8998:
				type_name = "WM8998";
				break;

			case WM1814:
				type_name = "WM1814";
				break;

			default:
				type_name = "WM8998";
				dev_warn(arizona->dev,
					 "WM8998 registered as %d\n",
					 arizona->type);
				arizona->type = WM8998;
			}
1330

1331 1332 1333
			apply_patch = wm8998_patch;
			subdevs = wm8998_devs;
			n_subdevs = ARRAY_SIZE(wm8998_devs);
1334 1335
		}
		break;
1336 1337
	default:
		dev_err(arizona->dev, "Unknown device ID %x\n", reg);
1338
		ret = -ENODEV;
1339 1340 1341
		goto err_reset;
	}

1342 1343 1344
	if (!subdevs) {
		dev_err(arizona->dev,
			"No kernel support for device ID %x\n", reg);
1345
		ret = -ENODEV;
1346 1347 1348
		goto err_reset;
	}

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

1351 1352 1353 1354 1355 1356 1357
	if (apply_patch) {
		ret = apply_patch(arizona);
		if (ret != 0) {
			dev_err(arizona->dev, "Failed to apply patch: %d\n",
				ret);
			goto err_reset;
		}
1358 1359 1360

		switch (arizona->type) {
		case WM5102:
1361 1362
			ret = wm5102_apply_hardware_patch(arizona);
			if (ret) {
1363 1364 1365 1366 1367 1368
				dev_err(arizona->dev,
					"Failed to apply hardware patch: %d\n",
					ret);
				goto err_reset;
			}
			break;
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
		case WM5110:
		case WM8280:
			ret = wm5110_apply_sleep_patch(arizona);
			if (ret) {
				dev_err(arizona->dev,
					"Failed to apply sleep patch: %d\n",
					ret);
				goto err_reset;
			}
			break;
1379 1380 1381
		default:
			break;
		}
1382 1383
	}

M
Mark Brown 已提交
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
	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]);
	}

	/* 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);
1402
		arizona_clk32k_enable(arizona);
M
Mark Brown 已提交
1403 1404 1405 1406 1407 1408 1409 1410 1411
		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;
1412
		goto err_reset;
M
Mark Brown 已提交
1413 1414
	}

1415
	for (i = 0; i < ARIZONA_MAX_MICBIAS; i++) {
1416 1417
		if (!arizona->pdata.micbias[i].mV &&
		    !arizona->pdata.micbias[i].bypass)
1418 1419
			continue;

1420 1421 1422 1423
		/* Apply default for bypass mode */
		if (!arizona->pdata.micbias[i].mV)
			arizona->pdata.micbias[i].mV = 2800;

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

1426 1427 1428 1429 1430 1431 1432 1433
		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;

1434
		if (arizona->pdata.micbias[i].soft_start)
1435 1436
			val |= ARIZONA_MICB1_RATE;

1437 1438 1439
		if (arizona->pdata.micbias[i].bypass)
			val |= ARIZONA_MICB1_BYPASS;

1440 1441 1442
		regmap_update_bits(arizona->regmap,
				   ARIZONA_MIC_BIAS_CTRL_1 + i,
				   ARIZONA_MICB1_LVL_MASK |
1443
				   ARIZONA_MICB1_EXT_CAP |
1444
				   ARIZONA_MICB1_DISCH |
1445
				   ARIZONA_MICB1_BYPASS |
1446 1447 1448
				   ARIZONA_MICB1_RATE, val);
	}

M
Mark Brown 已提交
1449 1450 1451 1452
	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;
1453 1454
		if (arizona->pdata.inmode[i] & ARIZONA_INMODE_DMIC)
			val |= 1 << ARIZONA_IN1_MODE_SHIFT;
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482

		switch (arizona->type) {
		case WM8998:
		case WM1814:
			regmap_update_bits(arizona->regmap,
				ARIZONA_ADC_DIGITAL_VOLUME_1L + (i * 8),
				ARIZONA_IN1L_SRC_SE_MASK,
				(arizona->pdata.inmode[i] & ARIZONA_INMODE_SE)
					<< ARIZONA_IN1L_SRC_SE_SHIFT);

			regmap_update_bits(arizona->regmap,
				ARIZONA_ADC_DIGITAL_VOLUME_1R + (i * 8),
				ARIZONA_IN1R_SRC_SE_MASK,
				(arizona->pdata.inmode[i] & ARIZONA_INMODE_SE)
					<< ARIZONA_IN1R_SRC_SE_SHIFT);

			mask = ARIZONA_IN1_DMIC_SUP_MASK |
				ARIZONA_IN1_MODE_MASK;
			break;
		default:
			if (arizona->pdata.inmode[i] & ARIZONA_INMODE_SE)
				val |= 1 << ARIZONA_IN1_SINGLE_ENDED_SHIFT;

			mask = ARIZONA_IN1_DMIC_SUP_MASK |
				ARIZONA_IN1_MODE_MASK |
				ARIZONA_IN1_SINGLE_ENDED_MASK;
			break;
		}
M
Mark Brown 已提交
1483 1484 1485

		regmap_update_bits(arizona->regmap,
				   ARIZONA_IN1L_CONTROL + (i * 8),
1486
				   mask, val);
M
Mark Brown 已提交
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
	}

	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,
1504
					   ARIZONA_PDM_SPK1_CTRL_1 + (i * 2),
M
Mark Brown 已提交
1505 1506 1507 1508 1509 1510
					   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,
1511
					   ARIZONA_PDM_SPK1_CTRL_2 + (i * 2),
M
Mark Brown 已提交
1512 1513 1514 1515
					   ARIZONA_SPK1_FMT_MASK,
					   arizona->pdata.spk_fmt[i]);
	}

1516 1517 1518
	pm_runtime_set_active(arizona->dev);
	pm_runtime_enable(arizona->dev);

M
Mark Brown 已提交
1519 1520 1521
	/* Set up for interrupts */
	ret = arizona_irq_init(arizona);
	if (ret != 0)
1522
		goto err_pm;
M
Mark Brown 已提交
1523

1524 1525 1526
	pm_runtime_set_autosuspend_delay(arizona->dev, 100);
	pm_runtime_use_autosuspend(arizona->dev);

M
Mark Brown 已提交
1527 1528 1529 1530 1531 1532 1533
	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);

1534 1535
	ret = mfd_add_devices(arizona->dev, PLATFORM_DEVID_NONE,
			      subdevs, n_subdevs, NULL, 0, NULL);
M
Mark Brown 已提交
1536

1537
	if (ret) {
M
Mark Brown 已提交
1538 1539 1540 1541 1542 1543 1544 1545
		dev_err(arizona->dev, "Failed to add subdevices: %d\n", ret);
		goto err_irq;
	}

	return 0;

err_irq:
	arizona_irq_exit(arizona);
1546 1547
err_pm:
	pm_runtime_disable(arizona->dev);
M
Mark Brown 已提交
1548
err_reset:
1549
	arizona_enable_reset(arizona);
1550
	regulator_disable(arizona->dcvdd);
M
Mark Brown 已提交
1551
err_enable:
1552
	regulator_bulk_disable(arizona->num_core_supplies,
M
Mark Brown 已提交
1553
			       arizona->core_supplies);
1554 1555
err_dcvdd:
	regulator_put(arizona->dcvdd);
M
Mark Brown 已提交
1556 1557 1558 1559 1560 1561
err_early:
	mfd_remove_devices(dev);
	return ret;
}
EXPORT_SYMBOL_GPL(arizona_dev_init);

B
Bill Pemberton 已提交
1562
int arizona_dev_exit(struct arizona *arizona)
M
Mark Brown 已提交
1563
{
1564 1565
	pm_runtime_disable(arizona->dev);

1566
	regulator_disable(arizona->dcvdd);
1567
	regulator_put(arizona->dcvdd);
1568

M
Mark Brown 已提交
1569 1570 1571 1572 1573
	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);
1574
	arizona_enable_reset(arizona);
1575

1576
	regulator_bulk_disable(arizona->num_core_supplies,
1577
			       arizona->core_supplies);
M
Mark Brown 已提交
1578 1579 1580
	return 0;
}
EXPORT_SYMBOL_GPL(arizona_dev_exit);