wm8994.c 124.9 KB
Newer Older
M
Mark Brown 已提交
1 2 3
/*
 * wm8994.c  --  WM8994 ALSA SoC Audio driver
 *
4
 * Copyright 2009-12 Wolfson Microelectronics plc
M
Mark Brown 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/pm.h>
19
#include <linux/gcd.h>
M
Mark Brown 已提交
20 21
#include <linux/i2c.h>
#include <linux/platform_device.h>
22
#include <linux/pm_runtime.h>
M
Mark Brown 已提交
23
#include <linux/regulator/consumer.h>
24
#include <linux/slab.h>
M
Mark Brown 已提交
25
#include <sound/core.h>
26
#include <sound/jack.h>
M
Mark Brown 已提交
27 28 29 30 31
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/initval.h>
#include <sound/tlv.h>
32
#include <trace/events/asoc.h>
M
Mark Brown 已提交
33 34 35 36 37 38 39 40 41

#include <linux/mfd/wm8994/core.h>
#include <linux/mfd/wm8994/registers.h>
#include <linux/mfd/wm8994/pdata.h>
#include <linux/mfd/wm8994/gpio.h>

#include "wm8994.h"
#include "wm_hubs.h"

42 43 44 45 46
#define WM1811_JACKDET_MODE_NONE  0x0000
#define WM1811_JACKDET_MODE_JACK  0x0100
#define WM1811_JACKDET_MODE_MIC   0x0080
#define WM1811_JACKDET_MODE_AUDIO 0x0180

M
Mark Brown 已提交
47 48 49
#define WM8994_NUM_DRC 3
#define WM8994_NUM_EQ  3

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
static struct {
	unsigned int reg;
	unsigned int mask;
} wm8994_vu_bits[] = {
	{ WM8994_LEFT_LINE_INPUT_1_2_VOLUME, WM8994_IN1_VU },
	{ WM8994_RIGHT_LINE_INPUT_1_2_VOLUME, WM8994_IN1_VU },
	{ WM8994_LEFT_LINE_INPUT_3_4_VOLUME, WM8994_IN2_VU },
	{ WM8994_RIGHT_LINE_INPUT_3_4_VOLUME, WM8994_IN2_VU },
	{ WM8994_SPEAKER_VOLUME_LEFT, WM8994_SPKOUT_VU },
	{ WM8994_SPEAKER_VOLUME_RIGHT, WM8994_SPKOUT_VU },
	{ WM8994_LEFT_OUTPUT_VOLUME, WM8994_HPOUT1_VU },
	{ WM8994_RIGHT_OUTPUT_VOLUME, WM8994_HPOUT1_VU },
	{ WM8994_LEFT_OPGA_VOLUME, WM8994_MIXOUT_VU },
	{ WM8994_RIGHT_OPGA_VOLUME, WM8994_MIXOUT_VU },

	{ WM8994_AIF1_DAC1_LEFT_VOLUME, WM8994_AIF1DAC1_VU },
	{ WM8994_AIF1_DAC1_RIGHT_VOLUME, WM8994_AIF1DAC1_VU },
	{ WM8994_AIF1_DAC2_LEFT_VOLUME, WM8994_AIF1DAC2_VU },
	{ WM8994_AIF1_DAC2_RIGHT_VOLUME, WM8994_AIF1DAC2_VU },
	{ WM8994_AIF2_DAC_LEFT_VOLUME, WM8994_AIF2DAC_VU },
	{ WM8994_AIF2_DAC_RIGHT_VOLUME, WM8994_AIF2DAC_VU },
	{ WM8994_AIF1_ADC1_LEFT_VOLUME, WM8994_AIF1ADC1_VU },
	{ WM8994_AIF1_ADC1_RIGHT_VOLUME, WM8994_AIF1ADC1_VU },
	{ WM8994_AIF1_ADC2_LEFT_VOLUME, WM8994_AIF1ADC2_VU },
	{ WM8994_AIF1_ADC2_RIGHT_VOLUME, WM8994_AIF1ADC2_VU },
	{ WM8994_AIF2_ADC_LEFT_VOLUME, WM8994_AIF2ADC_VU },
	{ WM8994_AIF2_ADC_RIGHT_VOLUME, WM8994_AIF1ADC2_VU },
	{ WM8994_DAC1_LEFT_VOLUME, WM8994_DAC1_VU },
	{ WM8994_DAC1_RIGHT_VOLUME, WM8994_DAC1_VU },
	{ WM8994_DAC2_LEFT_VOLUME, WM8994_DAC2_VU },
	{ WM8994_DAC2_RIGHT_VOLUME, WM8994_DAC2_VU },
};

M
Mark Brown 已提交
83 84 85 86 87 88 89 90 91 92 93 94
static int wm8994_drc_base[] = {
	WM8994_AIF1_DRC1_1,
	WM8994_AIF1_DRC2_1,
	WM8994_AIF2_DRC_1,
};

static int wm8994_retune_mobile_base[] = {
	WM8994_AIF1_DAC1_EQ_GAINS_1,
	WM8994_AIF1_DAC2_EQ_GAINS_1,
	WM8994_AIF2_EQ_GAINS_1,
};

95
static const struct wm8958_micd_rate micdet_rates[] = {
96 97
	{ 32768,       true,  1, 4 },
	{ 32768,       false, 1, 1 },
98 99
	{ 44100 * 256, true,  7, 10 },
	{ 44100 * 256, false, 7, 10 },
100 101
};

102 103 104
static const struct wm8958_micd_rate jackdet_rates[] = {
	{ 32768,       true,  0, 1 },
	{ 32768,       false, 0, 1 },
105 106
	{ 44100 * 256, true,  10, 10 },
	{ 44100 * 256, false, 7, 8 },
107 108
};

109 110 111
static void wm8958_micd_set_rate(struct snd_soc_codec *codec)
{
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
112
	struct wm8994 *control = wm8994->wm8994;
113 114
	int best, i, sysclk, val;
	bool idle;
115 116
	const struct wm8958_micd_rate *rates;
	int num_rates;
117 118 119 120 121 122 123 124 125

	idle = !wm8994->jack_mic;

	sysclk = snd_soc_read(codec, WM8994_CLOCKING_1);
	if (sysclk & WM8994_SYSCLK_SRC)
		sysclk = wm8994->aifclk[1];
	else
		sysclk = wm8994->aifclk[0];

126 127 128
	if (control->pdata.micd_rates) {
		rates = control->pdata.micd_rates;
		num_rates = control->pdata.num_micd_rates;
129
	} else if (wm8994->jackdet) {
130 131 132 133 134 135 136
		rates = jackdet_rates;
		num_rates = ARRAY_SIZE(jackdet_rates);
	} else {
		rates = micdet_rates;
		num_rates = ARRAY_SIZE(micdet_rates);
	}

137
	best = 0;
138 139
	for (i = 0; i < num_rates; i++) {
		if (rates[i].idle != idle)
140
			continue;
141 142
		if (abs(rates[i].sysclk - sysclk) <
		    abs(rates[best].sysclk - sysclk))
143
			best = i;
144
		else if (rates[best].idle != idle)
145 146 147
			best = i;
	}

148 149
	val = rates[best].start << WM8958_MICD_BIAS_STARTTIME_SHIFT
		| rates[best].rate << WM8958_MICD_RATE_SHIFT;
150

151 152 153 154
	dev_dbg(codec->dev, "MICD rate %d,%d for %dHz %s\n",
		rates[best].start, rates[best].rate, sysclk,
		idle ? "idle" : "active");

155 156 157 158 159
	snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
			    WM8958_MICD_BIAS_STARTTIME_MASK |
			    WM8958_MICD_RATE_MASK, val);
}

M
Mark Brown 已提交
160 161
static int configure_aif_clock(struct snd_soc_codec *codec, int aif)
{
162
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
M
Mark Brown 已提交
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 197 198 199 200 201 202
	int rate;
	int reg1 = 0;
	int offset;

	if (aif)
		offset = 4;
	else
		offset = 0;

	switch (wm8994->sysclk[aif]) {
	case WM8994_SYSCLK_MCLK1:
		rate = wm8994->mclk[0];
		break;

	case WM8994_SYSCLK_MCLK2:
		reg1 |= 0x8;
		rate = wm8994->mclk[1];
		break;

	case WM8994_SYSCLK_FLL1:
		reg1 |= 0x10;
		rate = wm8994->fll[0].out;
		break;

	case WM8994_SYSCLK_FLL2:
		reg1 |= 0x18;
		rate = wm8994->fll[1].out;
		break;

	default:
		return -EINVAL;
	}

	if (rate >= 13500000) {
		rate /= 2;
		reg1 |= WM8994_AIF1CLK_DIV;

		dev_dbg(codec->dev, "Dividing AIF%d clock to %dHz\n",
			aif + 1, rate);
	}
M
Mark Brown 已提交
203

M
Mark Brown 已提交
204 205 206 207 208 209 210 211 212 213 214
	wm8994->aifclk[aif] = rate;

	snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1 + offset,
			    WM8994_AIF1CLK_SRC_MASK | WM8994_AIF1CLK_DIV,
			    reg1);

	return 0;
}

static int configure_clock(struct snd_soc_codec *codec)
{
215
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
216
	int change, new;
M
Mark Brown 已提交
217 218 219 220 221 222 223 224 225 226 227 228

	/* Bring up the AIF clocks first */
	configure_aif_clock(codec, 0);
	configure_aif_clock(codec, 1);

	/* Then switch CLK_SYS over to the higher of them; a change
	 * can only happen as a result of a clocking change which can
	 * only be made outside of DAPM so we can safely redo the
	 * clocking.
	 */

	/* If they're equal it doesn't matter which is used */
229 230
	if (wm8994->aifclk[0] == wm8994->aifclk[1]) {
		wm8958_micd_set_rate(codec);
M
Mark Brown 已提交
231
		return 0;
232
	}
M
Mark Brown 已提交
233 234 235 236 237 238

	if (wm8994->aifclk[0] < wm8994->aifclk[1])
		new = WM8994_SYSCLK_SRC;
	else
		new = 0;

239 240
	change = snd_soc_update_bits(codec, WM8994_CLOCKING_1,
				     WM8994_SYSCLK_SRC, new);
241 242
	if (change)
		snd_soc_dapm_sync(&codec->dapm);
M
Mark Brown 已提交
243

244 245
	wm8958_micd_set_rate(codec);

M
Mark Brown 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
	return 0;
}

static int check_clk_sys(struct snd_soc_dapm_widget *source,
			 struct snd_soc_dapm_widget *sink)
{
	int reg = snd_soc_read(source->codec, WM8994_CLOCKING_1);
	const char *clk;

	/* Check what we're currently using for CLK_SYS */
	if (reg & WM8994_SYSCLK_SRC)
		clk = "AIF2CLK";
	else
		clk = "AIF1CLK";

	return strcmp(source->name, clk) == 0;
}

static const char *sidetone_hpf_text[] = {
	"2.7kHz", "1.35kHz", "675Hz", "370Hz", "180Hz", "90Hz", "45Hz"
};

268 269
static SOC_ENUM_SINGLE_DECL(sidetone_hpf,
			    WM8994_SIDETONE, 7, sidetone_hpf_text);
M
Mark Brown 已提交
270

271 272 273 274
static const char *adc_hpf_text[] = {
	"HiFi", "Voice 1", "Voice 2", "Voice 3"
};

275 276
static SOC_ENUM_SINGLE_DECL(aif1adc1_hpf,
			    WM8994_AIF1_ADC1_FILTERS, 13, adc_hpf_text);
277

278 279
static SOC_ENUM_SINGLE_DECL(aif1adc2_hpf,
			    WM8994_AIF1_ADC2_FILTERS, 13, adc_hpf_text);
280

281 282
static SOC_ENUM_SINGLE_DECL(aif2adc_hpf,
			    WM8994_AIF2_ADC_FILTERS, 13, adc_hpf_text);
283

M
Mark Brown 已提交
284 285 286 287 288
static const DECLARE_TLV_DB_SCALE(aif_tlv, 0, 600, 0);
static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
static const DECLARE_TLV_DB_SCALE(st_tlv, -3600, 300, 0);
static const DECLARE_TLV_DB_SCALE(wm8994_3d_tlv, -1600, 183, 0);
static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
M
Mark Brown 已提交
289
static const DECLARE_TLV_DB_SCALE(ng_tlv, -10200, 600, 0);
M
Mark Brown 已提交
290
static const DECLARE_TLV_DB_SCALE(mixin_boost_tlv, 0, 900, 0);
M
Mark Brown 已提交
291 292

#define WM8994_DRC_SWITCH(xname, reg, shift) \
293 294
	SOC_SINGLE_EXT(xname, reg, shift, 1, 0, \
		snd_soc_get_volsw, wm8994_put_drc_sw)
M
Mark Brown 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

static int wm8994_put_drc_sw(struct snd_kcontrol *kcontrol,
			     struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
	int mask, ret;

	/* Can't enable both ADC and DAC paths simultaneously */
	if (mc->shift == WM8994_AIF1DAC1_DRC_ENA_SHIFT)
		mask = WM8994_AIF1ADC1L_DRC_ENA_MASK |
			WM8994_AIF1ADC1R_DRC_ENA_MASK;
	else
		mask = WM8994_AIF1DAC1_DRC_ENA_MASK;

	ret = snd_soc_read(codec, mc->reg);
	if (ret < 0)
		return ret;
	if (ret & mask)
		return -EINVAL;

	return snd_soc_put_volsw(kcontrol, ucontrol);
}

static void wm8994_set_drc(struct snd_soc_codec *codec, int drc)
{
322
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
323 324
	struct wm8994 *control = wm8994->wm8994;
	struct wm8994_pdata *pdata = &control->pdata;
M
Mark Brown 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	int base = wm8994_drc_base[drc];
	int cfg = wm8994->drc_cfg[drc];
	int save, i;

	/* Save any enables; the configuration should clear them. */
	save = snd_soc_read(codec, base);
	save &= WM8994_AIF1DAC1_DRC_ENA | WM8994_AIF1ADC1L_DRC_ENA |
		WM8994_AIF1ADC1R_DRC_ENA;

	for (i = 0; i < WM8994_DRC_REGS; i++)
		snd_soc_update_bits(codec, base + i, 0xffff,
				    pdata->drc_cfgs[cfg].regs[i]);

	snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_DRC_ENA |
			     WM8994_AIF1ADC1L_DRC_ENA |
			     WM8994_AIF1ADC1R_DRC_ENA, save);
}

/* Icky as hell but saves code duplication */
static int wm8994_get_drc(const char *name)
{
	if (strcmp(name, "AIF1DRC1 Mode") == 0)
		return 0;
	if (strcmp(name, "AIF1DRC2 Mode") == 0)
		return 1;
	if (strcmp(name, "AIF2DRC Mode") == 0)
		return 2;
	return -EINVAL;
}

static int wm8994_put_drc_enum(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
359
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
360 361
	struct wm8994 *control = wm8994->wm8994;
	struct wm8994_pdata *pdata = &control->pdata;
M
Mark Brown 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
	int drc = wm8994_get_drc(kcontrol->id.name);
	int value = ucontrol->value.integer.value[0];

	if (drc < 0)
		return drc;

	if (value >= pdata->num_drc_cfgs)
		return -EINVAL;

	wm8994->drc_cfg[drc] = value;

	wm8994_set_drc(codec, drc);

	return 0;
}

static int wm8994_get_drc_enum(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
382
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
M
Mark Brown 已提交
383 384
	int drc = wm8994_get_drc(kcontrol->id.name);

385 386
	if (drc < 0)
		return drc;
M
Mark Brown 已提交
387 388 389 390 391 392 393
	ucontrol->value.enumerated.item[0] = wm8994->drc_cfg[drc];

	return 0;
}

static void wm8994_set_retune_mobile(struct snd_soc_codec *codec, int block)
{
394
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
395 396
	struct wm8994 *control = wm8994->wm8994;
	struct wm8994_pdata *pdata = &control->pdata;
M
Mark Brown 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
	int base = wm8994_retune_mobile_base[block];
	int iface, best, best_val, save, i, cfg;

	if (!pdata || !wm8994->num_retune_mobile_texts)
		return;

	switch (block) {
	case 0:
	case 1:
		iface = 0;
		break;
	case 2:
		iface = 1;
		break;
	default:
		return;
	}

	/* Find the version of the currently selected configuration
	 * with the nearest sample rate. */
	cfg = wm8994->retune_mobile_cfg[block];
	best = 0;
	best_val = INT_MAX;
	for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
		if (strcmp(pdata->retune_mobile_cfgs[i].name,
			   wm8994->retune_mobile_texts[cfg]) == 0 &&
		    abs(pdata->retune_mobile_cfgs[i].rate
			- wm8994->dac_rates[iface]) < best_val) {
			best = i;
			best_val = abs(pdata->retune_mobile_cfgs[i].rate
				       - wm8994->dac_rates[iface]);
		}
	}

	dev_dbg(codec->dev, "ReTune Mobile %d %s/%dHz for %dHz sample rate\n",
		block,
		pdata->retune_mobile_cfgs[best].name,
		pdata->retune_mobile_cfgs[best].rate,
		wm8994->dac_rates[iface]);

	/* The EQ will be disabled while reconfiguring it, remember the
438
	 * current configuration.
M
Mark Brown 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	 */
	save = snd_soc_read(codec, base);
	save &= WM8994_AIF1DAC1_EQ_ENA;

	for (i = 0; i < WM8994_EQ_REGS; i++)
		snd_soc_update_bits(codec, base + i, 0xffff,
				pdata->retune_mobile_cfgs[best].regs[i]);

	snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_EQ_ENA, save);
}

/* Icky as hell but saves code duplication */
static int wm8994_get_retune_mobile_block(const char *name)
{
	if (strcmp(name, "AIF1.1 EQ Mode") == 0)
		return 0;
	if (strcmp(name, "AIF1.2 EQ Mode") == 0)
		return 1;
	if (strcmp(name, "AIF2 EQ Mode") == 0)
		return 2;
	return -EINVAL;
}

static int wm8994_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
					 struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
466
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
467 468
	struct wm8994 *control = wm8994->wm8994;
	struct wm8994_pdata *pdata = &control->pdata;
M
Mark Brown 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
	int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
	int value = ucontrol->value.integer.value[0];

	if (block < 0)
		return block;

	if (value >= pdata->num_retune_mobile_cfgs)
		return -EINVAL;

	wm8994->retune_mobile_cfg[block] = value;

	wm8994_set_retune_mobile(codec, block);

	return 0;
}

static int wm8994_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
					 struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
M
Mark Brown 已提交
489
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
M
Mark Brown 已提交
490 491
	int block = wm8994_get_retune_mobile_block(kcontrol->id.name);

492 493 494
	if (block < 0)
		return block;

M
Mark Brown 已提交
495 496 497 498 499
	ucontrol->value.enumerated.item[0] = wm8994->retune_mobile_cfg[block];

	return 0;
}

500
static const char *aif_chan_src_text[] = {
501 502 503
	"Left", "Right"
};

504 505
static SOC_ENUM_SINGLE_DECL(aif1adcl_src,
			    WM8994_AIF1_CONTROL_1, 15, aif_chan_src_text);
506

507 508
static SOC_ENUM_SINGLE_DECL(aif1adcr_src,
			    WM8994_AIF1_CONTROL_1, 14, aif_chan_src_text);
509

510 511
static SOC_ENUM_SINGLE_DECL(aif2adcl_src,
			    WM8994_AIF2_CONTROL_1, 15, aif_chan_src_text);
512

513 514
static SOC_ENUM_SINGLE_DECL(aif2adcr_src,
			    WM8994_AIF2_CONTROL_1, 14, aif_chan_src_text);
515

516 517
static SOC_ENUM_SINGLE_DECL(aif1dacl_src,
			    WM8994_AIF1_CONTROL_2, 15, aif_chan_src_text);
518

519 520
static SOC_ENUM_SINGLE_DECL(aif1dacr_src,
			    WM8994_AIF1_CONTROL_2, 14, aif_chan_src_text);
521

522 523
static SOC_ENUM_SINGLE_DECL(aif2dacl_src,
			    WM8994_AIF2_CONTROL_2, 15, aif_chan_src_text);
524

525 526
static SOC_ENUM_SINGLE_DECL(aif2dacr_src,
			    WM8994_AIF2_CONTROL_2, 14, aif_chan_src_text);
527

528 529 530 531
static const char *osr_text[] = {
	"Low Power", "High Performance",
};

532 533
static SOC_ENUM_SINGLE_DECL(dac_osr,
			    WM8994_OVERSAMPLING, 0, osr_text);
534

535 536
static SOC_ENUM_SINGLE_DECL(adc_osr,
			    WM8994_OVERSAMPLING, 1, osr_text);
537

M
Mark Brown 已提交
538 539 540 541 542 543 544 545 546 547 548
static const struct snd_kcontrol_new wm8994_snd_controls[] = {
SOC_DOUBLE_R_TLV("AIF1ADC1 Volume", WM8994_AIF1_ADC1_LEFT_VOLUME,
		 WM8994_AIF1_ADC1_RIGHT_VOLUME,
		 1, 119, 0, digital_tlv),
SOC_DOUBLE_R_TLV("AIF1ADC2 Volume", WM8994_AIF1_ADC2_LEFT_VOLUME,
		 WM8994_AIF1_ADC2_RIGHT_VOLUME,
		 1, 119, 0, digital_tlv),
SOC_DOUBLE_R_TLV("AIF2ADC Volume", WM8994_AIF2_ADC_LEFT_VOLUME,
		 WM8994_AIF2_ADC_RIGHT_VOLUME,
		 1, 119, 0, digital_tlv),

549 550
SOC_ENUM("AIF1ADCL Source", aif1adcl_src),
SOC_ENUM("AIF1ADCR Source", aif1adcr_src),
551 552
SOC_ENUM("AIF2ADCL Source", aif2adcl_src),
SOC_ENUM("AIF2ADCR Source", aif2adcr_src),
553

554 555
SOC_ENUM("AIF1DACL Source", aif1dacl_src),
SOC_ENUM("AIF1DACR Source", aif1dacr_src),
556 557
SOC_ENUM("AIF2DACL Source", aif2dacl_src),
SOC_ENUM("AIF2DACR Source", aif2dacr_src),
558

M
Mark Brown 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
SOC_DOUBLE_R_TLV("AIF1DAC1 Volume", WM8994_AIF1_DAC1_LEFT_VOLUME,
		 WM8994_AIF1_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
SOC_DOUBLE_R_TLV("AIF1DAC2 Volume", WM8994_AIF1_DAC2_LEFT_VOLUME,
		 WM8994_AIF1_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
SOC_DOUBLE_R_TLV("AIF2DAC Volume", WM8994_AIF2_DAC_LEFT_VOLUME,
		 WM8994_AIF2_DAC_RIGHT_VOLUME, 1, 96, 0, digital_tlv),

SOC_SINGLE_TLV("AIF1 Boost Volume", WM8994_AIF1_CONTROL_2, 10, 3, 0, aif_tlv),
SOC_SINGLE_TLV("AIF2 Boost Volume", WM8994_AIF2_CONTROL_2, 10, 3, 0, aif_tlv),

SOC_SINGLE("AIF1DAC1 EQ Switch", WM8994_AIF1_DAC1_EQ_GAINS_1, 0, 1, 0),
SOC_SINGLE("AIF1DAC2 EQ Switch", WM8994_AIF1_DAC2_EQ_GAINS_1, 0, 1, 0),
SOC_SINGLE("AIF2 EQ Switch", WM8994_AIF2_EQ_GAINS_1, 0, 1, 0),

WM8994_DRC_SWITCH("AIF1DAC1 DRC Switch", WM8994_AIF1_DRC1_1, 2),
WM8994_DRC_SWITCH("AIF1ADC1L DRC Switch", WM8994_AIF1_DRC1_1, 1),
WM8994_DRC_SWITCH("AIF1ADC1R DRC Switch", WM8994_AIF1_DRC1_1, 0),

WM8994_DRC_SWITCH("AIF1DAC2 DRC Switch", WM8994_AIF1_DRC2_1, 2),
WM8994_DRC_SWITCH("AIF1ADC2L DRC Switch", WM8994_AIF1_DRC2_1, 1),
WM8994_DRC_SWITCH("AIF1ADC2R DRC Switch", WM8994_AIF1_DRC2_1, 0),

WM8994_DRC_SWITCH("AIF2DAC DRC Switch", WM8994_AIF2_DRC_1, 2),
WM8994_DRC_SWITCH("AIF2ADCL DRC Switch", WM8994_AIF2_DRC_1, 1),
WM8994_DRC_SWITCH("AIF2ADCR DRC Switch", WM8994_AIF2_DRC_1, 0),

SOC_SINGLE_TLV("DAC1 Right Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
	       5, 12, 0, st_tlv),
SOC_SINGLE_TLV("DAC1 Left Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
	       0, 12, 0, st_tlv),
SOC_SINGLE_TLV("DAC2 Right Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
	       5, 12, 0, st_tlv),
SOC_SINGLE_TLV("DAC2 Left Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
	       0, 12, 0, st_tlv),
SOC_ENUM("Sidetone HPF Mux", sidetone_hpf),
SOC_SINGLE("Sidetone HPF Switch", WM8994_SIDETONE, 6, 1, 0),

596 597 598 599 600 601 602 603 604
SOC_ENUM("AIF1ADC1 HPF Mode", aif1adc1_hpf),
SOC_DOUBLE("AIF1ADC1 HPF Switch", WM8994_AIF1_ADC1_FILTERS, 12, 11, 1, 0),

SOC_ENUM("AIF1ADC2 HPF Mode", aif1adc2_hpf),
SOC_DOUBLE("AIF1ADC2 HPF Switch", WM8994_AIF1_ADC2_FILTERS, 12, 11, 1, 0),

SOC_ENUM("AIF2ADC HPF Mode", aif2adc_hpf),
SOC_DOUBLE("AIF2ADC HPF Switch", WM8994_AIF2_ADC_FILTERS, 12, 11, 1, 0),

605 606 607
SOC_ENUM("ADC OSR", adc_osr),
SOC_ENUM("DAC OSR", dac_osr),

M
Mark Brown 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
SOC_DOUBLE_R_TLV("DAC1 Volume", WM8994_DAC1_LEFT_VOLUME,
		 WM8994_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
SOC_DOUBLE_R("DAC1 Switch", WM8994_DAC1_LEFT_VOLUME,
	     WM8994_DAC1_RIGHT_VOLUME, 9, 1, 1),

SOC_DOUBLE_R_TLV("DAC2 Volume", WM8994_DAC2_LEFT_VOLUME,
		 WM8994_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
SOC_DOUBLE_R("DAC2 Switch", WM8994_DAC2_LEFT_VOLUME,
	     WM8994_DAC2_RIGHT_VOLUME, 9, 1, 1),

SOC_SINGLE_TLV("SPKL DAC2 Volume", WM8994_SPKMIXL_ATTENUATION,
	       6, 1, 1, wm_hubs_spkmix_tlv),
SOC_SINGLE_TLV("SPKL DAC1 Volume", WM8994_SPKMIXL_ATTENUATION,
	       2, 1, 1, wm_hubs_spkmix_tlv),

SOC_SINGLE_TLV("SPKR DAC2 Volume", WM8994_SPKMIXR_ATTENUATION,
	       6, 1, 1, wm_hubs_spkmix_tlv),
SOC_SINGLE_TLV("SPKR DAC1 Volume", WM8994_SPKMIXR_ATTENUATION,
	       2, 1, 1, wm_hubs_spkmix_tlv),

SOC_SINGLE_TLV("AIF1DAC1 3D Stereo Volume", WM8994_AIF1_DAC1_FILTERS_2,
	       10, 15, 0, wm8994_3d_tlv),
630
SOC_SINGLE("AIF1DAC1 3D Stereo Switch", WM8994_AIF1_DAC1_FILTERS_2,
M
Mark Brown 已提交
631 632 633 634 635
	   8, 1, 0),
SOC_SINGLE_TLV("AIF1DAC2 3D Stereo Volume", WM8994_AIF1_DAC2_FILTERS_2,
	       10, 15, 0, wm8994_3d_tlv),
SOC_SINGLE("AIF1DAC2 3D Stereo Switch", WM8994_AIF1_DAC2_FILTERS_2,
	   8, 1, 0),
636
SOC_SINGLE_TLV("AIF2DAC 3D Stereo Volume", WM8994_AIF2_DAC_FILTERS_2,
M
Mark Brown 已提交
637
	       10, 15, 0, wm8994_3d_tlv),
638
SOC_SINGLE("AIF2DAC 3D Stereo Switch", WM8994_AIF2_DAC_FILTERS_2,
M
Mark Brown 已提交
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
	   8, 1, 0),
};

static const struct snd_kcontrol_new wm8994_eq_controls[] = {
SOC_SINGLE_TLV("AIF1DAC1 EQ1 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 11, 31, 0,
	       eq_tlv),
SOC_SINGLE_TLV("AIF1DAC1 EQ2 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 6, 31, 0,
	       eq_tlv),
SOC_SINGLE_TLV("AIF1DAC1 EQ3 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 1, 31, 0,
	       eq_tlv),
SOC_SINGLE_TLV("AIF1DAC1 EQ4 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 11, 31, 0,
	       eq_tlv),
SOC_SINGLE_TLV("AIF1DAC1 EQ5 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 6, 31, 0,
	       eq_tlv),

SOC_SINGLE_TLV("AIF1DAC2 EQ1 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 11, 31, 0,
	       eq_tlv),
SOC_SINGLE_TLV("AIF1DAC2 EQ2 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 6, 31, 0,
	       eq_tlv),
SOC_SINGLE_TLV("AIF1DAC2 EQ3 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 1, 31, 0,
	       eq_tlv),
SOC_SINGLE_TLV("AIF1DAC2 EQ4 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 11, 31, 0,
	       eq_tlv),
SOC_SINGLE_TLV("AIF1DAC2 EQ5 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 6, 31, 0,
	       eq_tlv),

SOC_SINGLE_TLV("AIF2 EQ1 Volume", WM8994_AIF2_EQ_GAINS_1, 11, 31, 0,
	       eq_tlv),
SOC_SINGLE_TLV("AIF2 EQ2 Volume", WM8994_AIF2_EQ_GAINS_1, 6, 31, 0,
	       eq_tlv),
SOC_SINGLE_TLV("AIF2 EQ3 Volume", WM8994_AIF2_EQ_GAINS_1, 1, 31, 0,
	       eq_tlv),
SOC_SINGLE_TLV("AIF2 EQ4 Volume", WM8994_AIF2_EQ_GAINS_2, 11, 31, 0,
	       eq_tlv),
SOC_SINGLE_TLV("AIF2 EQ5 Volume", WM8994_AIF2_EQ_GAINS_2, 6, 31, 0,
	       eq_tlv),
};

677 678 679 680 681 682 683 684 685 686 687 688
static const struct snd_kcontrol_new wm8994_drc_controls[] = {
SND_SOC_BYTES_MASK("AIF1.1 DRC", WM8994_AIF1_DRC1_1, 5,
		   WM8994_AIF1DAC1_DRC_ENA | WM8994_AIF1ADC1L_DRC_ENA |
		   WM8994_AIF1ADC1R_DRC_ENA),
SND_SOC_BYTES_MASK("AIF1.2 DRC", WM8994_AIF1_DRC2_1, 5,
		   WM8994_AIF1DAC2_DRC_ENA | WM8994_AIF1ADC2L_DRC_ENA |
		   WM8994_AIF1ADC2R_DRC_ENA),
SND_SOC_BYTES_MASK("AIF2 DRC", WM8994_AIF2_DRC_1, 5,
		   WM8994_AIF2DAC_DRC_ENA | WM8994_AIF2ADCL_DRC_ENA |
		   WM8994_AIF2ADCR_DRC_ENA),
};

M
Mark Brown 已提交
689 690 691 692
static const char *wm8958_ng_text[] = {
	"30ms", "125ms", "250ms", "500ms",
};

693 694 695 696
static SOC_ENUM_SINGLE_DECL(wm8958_aif1dac1_ng_hold,
			    WM8958_AIF1_DAC1_NOISE_GATE,
			    WM8958_AIF1DAC1_NG_THR_SHIFT,
			    wm8958_ng_text);
M
Mark Brown 已提交
697

698 699 700 701
static SOC_ENUM_SINGLE_DECL(wm8958_aif1dac2_ng_hold,
			    WM8958_AIF1_DAC2_NOISE_GATE,
			    WM8958_AIF1DAC2_NG_THR_SHIFT,
			    wm8958_ng_text);
M
Mark Brown 已提交
702

703 704 705 706
static SOC_ENUM_SINGLE_DECL(wm8958_aif2dac_ng_hold,
			    WM8958_AIF2_DAC_NOISE_GATE,
			    WM8958_AIF2DAC_NG_THR_SHIFT,
			    wm8958_ng_text);
M
Mark Brown 已提交
707

708 709
static const struct snd_kcontrol_new wm8958_snd_controls[] = {
SOC_SINGLE_TLV("AIF3 Boost Volume", WM8958_AIF3_CONTROL_2, 10, 3, 0, aif_tlv),
M
Mark Brown 已提交
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730

SOC_SINGLE("AIF1DAC1 Noise Gate Switch", WM8958_AIF1_DAC1_NOISE_GATE,
	   WM8958_AIF1DAC1_NG_ENA_SHIFT, 1, 0),
SOC_ENUM("AIF1DAC1 Noise Gate Hold Time", wm8958_aif1dac1_ng_hold),
SOC_SINGLE_TLV("AIF1DAC1 Noise Gate Threshold Volume",
	       WM8958_AIF1_DAC1_NOISE_GATE, WM8958_AIF1DAC1_NG_THR_SHIFT,
	       7, 1, ng_tlv),

SOC_SINGLE("AIF1DAC2 Noise Gate Switch", WM8958_AIF1_DAC2_NOISE_GATE,
	   WM8958_AIF1DAC2_NG_ENA_SHIFT, 1, 0),
SOC_ENUM("AIF1DAC2 Noise Gate Hold Time", wm8958_aif1dac2_ng_hold),
SOC_SINGLE_TLV("AIF1DAC2 Noise Gate Threshold Volume",
	       WM8958_AIF1_DAC2_NOISE_GATE, WM8958_AIF1DAC2_NG_THR_SHIFT,
	       7, 1, ng_tlv),

SOC_SINGLE("AIF2DAC Noise Gate Switch", WM8958_AIF2_DAC_NOISE_GATE,
	   WM8958_AIF2DAC_NG_ENA_SHIFT, 1, 0),
SOC_ENUM("AIF2DAC Noise Gate Hold Time", wm8958_aif2dac_ng_hold),
SOC_SINGLE_TLV("AIF2DAC Noise Gate Threshold Volume",
	       WM8958_AIF2_DAC_NOISE_GATE, WM8958_AIF2DAC_NG_THR_SHIFT,
	       7, 1, ng_tlv),
731 732
};

M
Mark Brown 已提交
733 734 735 736 737 738 739
static const struct snd_kcontrol_new wm1811_snd_controls[] = {
SOC_SINGLE_TLV("MIXINL IN1LP Boost Volume", WM8994_INPUT_MIXER_1, 7, 1, 0,
	       mixin_boost_tlv),
SOC_SINGLE_TLV("MIXINL IN1RP Boost Volume", WM8994_INPUT_MIXER_1, 8, 1, 0,
	       mixin_boost_tlv),
};

740 741 742 743 744
/* We run all mode setting through a function to enforce audio mode */
static void wm1811_jackdet_set_mode(struct snd_soc_codec *codec, u16 mode)
{
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);

745
	if (!wm8994->jackdet || !wm8994->micdet[0].jack)
746 747
		return;

748 749 750
	if (wm8994->active_refcount)
		mode = WM1811_JACKDET_MODE_AUDIO;

751
	if (mode == wm8994->jackdet_mode)
752 753
		return;

754
	wm8994->jackdet_mode = mode;
755

756 757 758
	/* Always use audio mode to detect while the system is active */
	if (mode != WM1811_JACKDET_MODE_NONE)
		mode = WM1811_JACKDET_MODE_AUDIO;
759

760 761
	snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
			    WM1811_JACKDET_MODE_MASK, mode);
762 763 764 765 766 767 768 769 770 771 772 773 774
}

static void active_reference(struct snd_soc_codec *codec)
{
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);

	mutex_lock(&wm8994->accdet_lock);

	wm8994->active_refcount++;

	dev_dbg(codec->dev, "Active refcount incremented, now %d\n",
		wm8994->active_refcount);

775 776
	/* If we're using jack detection go into audio mode */
	wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_AUDIO);
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794

	mutex_unlock(&wm8994->accdet_lock);
}

static void active_dereference(struct snd_soc_codec *codec)
{
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
	u16 mode;

	mutex_lock(&wm8994->accdet_lock);

	wm8994->active_refcount--;

	dev_dbg(codec->dev, "Active refcount decremented, now %d\n",
		wm8994->active_refcount);

	if (wm8994->active_refcount == 0) {
		/* Go into appropriate detection only mode */
795 796 797 798 799 800
		if (wm8994->jack_mic || wm8994->mic_detecting)
			mode = WM1811_JACKDET_MODE_MIC;
		else
			mode = WM1811_JACKDET_MODE_JACK;

		wm1811_jackdet_set_mode(codec, mode);
801 802 803 804 805
	}

	mutex_unlock(&wm8994->accdet_lock);
}

M
Mark Brown 已提交
806 807 808 809
static int clk_sys_event(struct snd_soc_dapm_widget *w,
			 struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_codec *codec = w->codec;
810
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
M
Mark Brown 已提交
811 812 813 814 815

	switch (event) {
	case SND_SOC_DAPM_PRE_PMU:
		return configure_clock(codec);

816 817 818 819 820 821 822 823 824
	case SND_SOC_DAPM_POST_PMU:
		/*
		 * JACKDET won't run until we start the clock and it
		 * only reports deltas, make sure we notify the state
		 * up the stack on startup.  Use a *very* generous
		 * timeout for paranoia, there's no urgency and we
		 * don't want false reports.
		 */
		if (wm8994->jackdet && !wm8994->clk_has_run) {
825 826 827
			queue_delayed_work(system_power_efficient_wq,
					   &wm8994->jackdet_bootstrap,
					   msecs_to_jiffies(1000));
828 829 830 831
			wm8994->clk_has_run = true;
		}
		break;

M
Mark Brown 已提交
832 833 834 835 836 837 838 839
	case SND_SOC_DAPM_POST_PMD:
		configure_clock(codec);
		break;
	}

	return 0;
}

840 841 842 843
static void vmid_reference(struct snd_soc_codec *codec)
{
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);

844 845
	pm_runtime_get_sync(codec->dev);

846 847 848 849 850 851
	wm8994->vmid_refcount++;

	dev_dbg(codec->dev, "Referencing VMID, refcount is now %d\n",
		wm8994->vmid_refcount);

	if (wm8994->vmid_refcount == 1) {
852 853
		snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
				    WM8994_LINEOUT1_DISCH |
854
				    WM8994_LINEOUT2_DISCH, 0);
855

856 857
		wm_hubs_vmid_ena(codec);

858 859
		switch (wm8994->vmid_mode) {
		default:
860
			WARN_ON(NULL == "Invalid VMID mode");
861 862 863 864 865 866 867 868 869 870 871
		case WM8994_VMID_NORMAL:
			/* Startup bias, VMID ramp & buffer */
			snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
					    WM8994_BIAS_SRC |
					    WM8994_VMID_DISCH |
					    WM8994_STARTUP_BIAS_ENA |
					    WM8994_VMID_BUF_ENA |
					    WM8994_VMID_RAMP_MASK,
					    WM8994_BIAS_SRC |
					    WM8994_STARTUP_BIAS_ENA |
					    WM8994_VMID_BUF_ENA |
872
					    (0x2 << WM8994_VMID_RAMP_SHIFT));
873 874 875 876 877 878 879

			/* Main bias enable, VMID=2x40k */
			snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
					    WM8994_BIAS_ENA |
					    WM8994_VMID_SEL_MASK,
					    WM8994_BIAS_ENA | 0x2);

880
			msleep(300);
881 882 883 884 885 886

			snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
					    WM8994_VMID_RAMP_MASK |
					    WM8994_BIAS_SRC,
					    0);
			break;
887

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
		case WM8994_VMID_FORCE:
			/* Startup bias, slow VMID ramp & buffer */
			snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
					    WM8994_BIAS_SRC |
					    WM8994_VMID_DISCH |
					    WM8994_STARTUP_BIAS_ENA |
					    WM8994_VMID_BUF_ENA |
					    WM8994_VMID_RAMP_MASK,
					    WM8994_BIAS_SRC |
					    WM8994_STARTUP_BIAS_ENA |
					    WM8994_VMID_BUF_ENA |
					    (0x2 << WM8994_VMID_RAMP_SHIFT));

			/* Main bias enable, VMID=2x40k */
			snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
					    WM8994_BIAS_ENA |
					    WM8994_VMID_SEL_MASK,
					    WM8994_BIAS_ENA | 0x2);

			msleep(400);

			snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
					    WM8994_VMID_RAMP_MASK |
					    WM8994_BIAS_SRC,
					    0);
			break;
		}
915 916 917 918 919 920 921 922 923 924 925 926 927
	}
}

static void vmid_dereference(struct snd_soc_codec *codec)
{
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);

	wm8994->vmid_refcount--;

	dev_dbg(codec->dev, "Dereferencing VMID, refcount is now %d\n",
		wm8994->vmid_refcount);

	if (wm8994->vmid_refcount == 0) {
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
		if (wm8994->hubs.lineout1_se)
			snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_3,
					    WM8994_LINEOUT1N_ENA |
					    WM8994_LINEOUT1P_ENA,
					    WM8994_LINEOUT1N_ENA |
					    WM8994_LINEOUT1P_ENA);

		if (wm8994->hubs.lineout2_se)
			snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_3,
					    WM8994_LINEOUT2N_ENA |
					    WM8994_LINEOUT2P_ENA,
					    WM8994_LINEOUT2N_ENA |
					    WM8994_LINEOUT2P_ENA);

		/* Start discharging VMID */
943 944
		snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
				    WM8994_BIAS_SRC |
945
				    WM8994_VMID_DISCH,
946
				    WM8994_BIAS_SRC |
947
				    WM8994_VMID_DISCH);
948

949 950
		snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
				    WM8994_VMID_SEL_MASK, 0);
951

952
		msleep(400);
953

954
		/* Active discharge */
955 956 957 958 959 960
		snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
				    WM8994_LINEOUT1_DISCH |
				    WM8994_LINEOUT2_DISCH,
				    WM8994_LINEOUT1_DISCH |
				    WM8994_LINEOUT2_DISCH);

961 962 963 964 965 966
		snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_3,
				    WM8994_LINEOUT1N_ENA |
				    WM8994_LINEOUT1P_ENA |
				    WM8994_LINEOUT2N_ENA |
				    WM8994_LINEOUT2P_ENA, 0);

967 968 969 970 971 972
		/* Switch off startup biases */
		snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
				    WM8994_BIAS_SRC |
				    WM8994_STARTUP_BIAS_ENA |
				    WM8994_VMID_BUF_ENA |
				    WM8994_VMID_RAMP_MASK, 0);
973 974

		snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
975
				    WM8994_VMID_SEL_MASK, 0);
976
	}
977 978

	pm_runtime_put(codec->dev);
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
}

static int vmid_event(struct snd_soc_dapm_widget *w,
		      struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_codec *codec = w->codec;

	switch (event) {
	case SND_SOC_DAPM_PRE_PMU:
		vmid_reference(codec);
		break;

	case SND_SOC_DAPM_POST_PMD:
		vmid_dereference(codec);
		break;
	}

	return 0;
}

999
static bool wm8994_check_class_w_digital(struct snd_soc_codec *codec)
M
Mark Brown 已提交
1000 1001 1002 1003
{
	int source = 0;  /* GCC flow analysis can't track enable */
	int reg, reg_r;

1004
	/* We also need the same AIF source for L/R and only one path */
M
Mark Brown 已提交
1005 1006 1007
	reg = snd_soc_read(codec, WM8994_DAC1_LEFT_MIXER_ROUTING);
	switch (reg) {
	case WM8994_AIF2DACL_TO_DAC1L:
1008
		dev_vdbg(codec->dev, "Class W source AIF2DAC\n");
M
Mark Brown 已提交
1009 1010 1011
		source = 2 << WM8994_CP_DYN_SRC_SEL_SHIFT;
		break;
	case WM8994_AIF1DAC2L_TO_DAC1L:
1012
		dev_vdbg(codec->dev, "Class W source AIF1DAC2\n");
M
Mark Brown 已提交
1013 1014 1015
		source = 1 << WM8994_CP_DYN_SRC_SEL_SHIFT;
		break;
	case WM8994_AIF1DAC1L_TO_DAC1L:
1016
		dev_vdbg(codec->dev, "Class W source AIF1DAC1\n");
M
Mark Brown 已提交
1017 1018 1019
		source = 0 << WM8994_CP_DYN_SRC_SEL_SHIFT;
		break;
	default:
1020
		dev_vdbg(codec->dev, "DAC mixer setting: %x\n", reg);
1021
		return false;
M
Mark Brown 已提交
1022 1023 1024 1025
	}

	reg_r = snd_soc_read(codec, WM8994_DAC1_RIGHT_MIXER_ROUTING);
	if (reg_r != reg) {
1026
		dev_vdbg(codec->dev, "Left and right DAC mixers different\n");
1027
		return false;
M
Mark Brown 已提交
1028 1029
	}

1030 1031 1032
	/* Set the source up */
	snd_soc_update_bits(codec, WM8994_CLASS_W_1,
			    WM8994_CP_DYN_SRC_SEL_MASK, source);
1033

1034
	return true;
M
Mark Brown 已提交
1035 1036
}

1037 1038
static int aif1clk_ev(struct snd_soc_dapm_widget *w,
		      struct snd_kcontrol *kcontrol, int event)
1039 1040
{
	struct snd_soc_codec *codec = w->codec;
1041
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1042
	struct wm8994 *control = wm8994->wm8994;
1043
	int mask = WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC1R_ENA;
1044
	int i;
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
	int dac;
	int adc;
	int val;

	switch (control->type) {
	case WM8994:
	case WM8958:
		mask |= WM8994_AIF1DAC2L_ENA | WM8994_AIF1DAC2R_ENA;
		break;
	default:
		break;
	}
1057 1058 1059

	switch (event) {
	case SND_SOC_DAPM_PRE_PMU:
1060 1061 1062 1063
		/* Don't enable timeslot 2 if not in use */
		if (wm8994->channels[0] <= 2)
			mask &= ~(WM8994_AIF1DAC2L_ENA | WM8994_AIF1DAC2R_ENA);

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
		val = snd_soc_read(codec, WM8994_AIF1_CONTROL_1);
		if ((val & WM8994_AIF1ADCL_SRC) &&
		    (val & WM8994_AIF1ADCR_SRC))
			adc = WM8994_AIF1ADC1R_ENA | WM8994_AIF1ADC2R_ENA;
		else if (!(val & WM8994_AIF1ADCL_SRC) &&
			 !(val & WM8994_AIF1ADCR_SRC))
			adc = WM8994_AIF1ADC1L_ENA | WM8994_AIF1ADC2L_ENA;
		else
			adc = WM8994_AIF1ADC1R_ENA | WM8994_AIF1ADC2R_ENA |
				WM8994_AIF1ADC1L_ENA | WM8994_AIF1ADC2L_ENA;

		val = snd_soc_read(codec, WM8994_AIF1_CONTROL_2);
		if ((val & WM8994_AIF1DACL_SRC) &&
		    (val & WM8994_AIF1DACR_SRC))
			dac = WM8994_AIF1DAC1R_ENA | WM8994_AIF1DAC2R_ENA;
		else if (!(val & WM8994_AIF1DACL_SRC) &&
			 !(val & WM8994_AIF1DACR_SRC))
			dac = WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC2L_ENA;
		else
			dac = WM8994_AIF1DAC1R_ENA | WM8994_AIF1DAC2R_ENA |
				WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC2L_ENA;

		snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
				    mask, adc);
		snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
				    mask, dac);
		snd_soc_update_bits(codec, WM8994_CLOCKING_1,
				    WM8994_AIF1DSPCLK_ENA |
				    WM8994_SYSDSPCLK_ENA,
				    WM8994_AIF1DSPCLK_ENA |
				    WM8994_SYSDSPCLK_ENA);
		snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4, mask,
				    WM8994_AIF1ADC1R_ENA |
				    WM8994_AIF1ADC1L_ENA |
				    WM8994_AIF1ADC2R_ENA |
				    WM8994_AIF1ADC2L_ENA);
		snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5, mask,
				    WM8994_AIF1DAC1R_ENA |
				    WM8994_AIF1DAC1L_ENA |
				    WM8994_AIF1DAC2R_ENA |
				    WM8994_AIF1DAC2L_ENA);
1105 1106
		break;

1107 1108 1109 1110 1111 1112 1113
	case SND_SOC_DAPM_POST_PMU:
		for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
			snd_soc_write(codec, wm8994_vu_bits[i].reg,
				      snd_soc_read(codec,
						   wm8994_vu_bits[i].reg));
		break;

1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
	case SND_SOC_DAPM_PRE_PMD:
	case SND_SOC_DAPM_POST_PMD:
		snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
				    mask, 0);
		snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
				    mask, 0);

		val = snd_soc_read(codec, WM8994_CLOCKING_1);
		if (val & WM8994_AIF2DSPCLK_ENA)
			val = WM8994_SYSDSPCLK_ENA;
		else
			val = 0;
		snd_soc_update_bits(codec, WM8994_CLOCKING_1,
				    WM8994_SYSDSPCLK_ENA |
				    WM8994_AIF1DSPCLK_ENA, val);
		break;
	}
1131

1132 1133 1134
	return 0;
}

1135 1136
static int aif2clk_ev(struct snd_soc_dapm_widget *w,
		      struct snd_kcontrol *kcontrol, int event)
1137 1138
{
	struct snd_soc_codec *codec = w->codec;
1139
	int i;
1140 1141 1142
	int dac;
	int adc;
	int val;
1143 1144

	switch (event) {
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
	case SND_SOC_DAPM_PRE_PMU:
		val = snd_soc_read(codec, WM8994_AIF2_CONTROL_1);
		if ((val & WM8994_AIF2ADCL_SRC) &&
		    (val & WM8994_AIF2ADCR_SRC))
			adc = WM8994_AIF2ADCR_ENA;
		else if (!(val & WM8994_AIF2ADCL_SRC) &&
			 !(val & WM8994_AIF2ADCR_SRC))
			adc = WM8994_AIF2ADCL_ENA;
		else
			adc = WM8994_AIF2ADCL_ENA | WM8994_AIF2ADCR_ENA;


		val = snd_soc_read(codec, WM8994_AIF2_CONTROL_2);
		if ((val & WM8994_AIF2DACL_SRC) &&
		    (val & WM8994_AIF2DACR_SRC))
			dac = WM8994_AIF2DACR_ENA;
		else if (!(val & WM8994_AIF2DACL_SRC) &&
			 !(val & WM8994_AIF2DACR_SRC))
			dac = WM8994_AIF2DACL_ENA;
		else
			dac = WM8994_AIF2DACL_ENA | WM8994_AIF2DACR_ENA;

		snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
				    WM8994_AIF2ADCL_ENA |
				    WM8994_AIF2ADCR_ENA, adc);
		snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
				    WM8994_AIF2DACL_ENA |
				    WM8994_AIF2DACR_ENA, dac);
		snd_soc_update_bits(codec, WM8994_CLOCKING_1,
				    WM8994_AIF2DSPCLK_ENA |
				    WM8994_SYSDSPCLK_ENA,
				    WM8994_AIF2DSPCLK_ENA |
				    WM8994_SYSDSPCLK_ENA);
		snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
				    WM8994_AIF2ADCL_ENA |
				    WM8994_AIF2ADCR_ENA,
				    WM8994_AIF2ADCL_ENA |
				    WM8994_AIF2ADCR_ENA);
		snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
				    WM8994_AIF2DACL_ENA |
				    WM8994_AIF2DACR_ENA,
				    WM8994_AIF2DACL_ENA |
				    WM8994_AIF2DACR_ENA);
		break;

1190 1191 1192 1193 1194 1195 1196
	case SND_SOC_DAPM_POST_PMU:
		for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
			snd_soc_write(codec, wm8994_vu_bits[i].reg,
				      snd_soc_read(codec,
						   wm8994_vu_bits[i].reg));
		break;

1197
	case SND_SOC_DAPM_PRE_PMD:
1198
	case SND_SOC_DAPM_POST_PMD:
1199 1200 1201
		snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
				    WM8994_AIF2DACL_ENA |
				    WM8994_AIF2DACR_ENA, 0);
1202
		snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
				    WM8994_AIF2ADCL_ENA |
				    WM8994_AIF2ADCR_ENA, 0);

		val = snd_soc_read(codec, WM8994_CLOCKING_1);
		if (val & WM8994_AIF1DSPCLK_ENA)
			val = WM8994_SYSDSPCLK_ENA;
		else
			val = 0;
		snd_soc_update_bits(codec, WM8994_CLOCKING_1,
				    WM8994_SYSDSPCLK_ENA |
				    WM8994_AIF2DSPCLK_ENA, val);
1214 1215 1216 1217 1218 1219
		break;
	}

	return 0;
}

1220 1221
static int aif1clk_late_ev(struct snd_soc_dapm_widget *w,
			   struct snd_kcontrol *kcontrol, int event)
1222 1223 1224 1225 1226 1227 1228 1229
{
	struct snd_soc_codec *codec = w->codec;
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);

	switch (event) {
	case SND_SOC_DAPM_PRE_PMU:
		wm8994->aif1clk_enable = 1;
		break;
1230 1231 1232
	case SND_SOC_DAPM_POST_PMD:
		wm8994->aif1clk_disable = 1;
		break;
1233 1234 1235 1236 1237
	}

	return 0;
}

1238 1239
static int aif2clk_late_ev(struct snd_soc_dapm_widget *w,
			   struct snd_kcontrol *kcontrol, int event)
1240 1241 1242 1243 1244 1245 1246 1247
{
	struct snd_soc_codec *codec = w->codec;
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);

	switch (event) {
	case SND_SOC_DAPM_PRE_PMU:
		wm8994->aif2clk_enable = 1;
		break;
1248 1249 1250
	case SND_SOC_DAPM_POST_PMD:
		wm8994->aif2clk_disable = 1;
		break;
1251 1252 1253 1254 1255
	}

	return 0;
}

1256 1257 1258 1259 1260 1261 1262 1263 1264
static int late_enable_ev(struct snd_soc_dapm_widget *w,
			  struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_codec *codec = w->codec;
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);

	switch (event) {
	case SND_SOC_DAPM_PRE_PMU:
		if (wm8994->aif1clk_enable) {
1265
			aif1clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMU);
1266 1267 1268
			snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
					    WM8994_AIF1CLK_ENA_MASK,
					    WM8994_AIF1CLK_ENA);
1269
			aif1clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMU);
1270 1271 1272
			wm8994->aif1clk_enable = 0;
		}
		if (wm8994->aif2clk_enable) {
1273
			aif2clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMU);
1274 1275 1276
			snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
					    WM8994_AIF2CLK_ENA_MASK,
					    WM8994_AIF2CLK_ENA);
1277
			aif2clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMU);
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
			wm8994->aif2clk_enable = 0;
		}
		break;
	}

	/* We may also have postponed startup of DSP, handle that. */
	wm8958_aif_ev(w, kcontrol, event);

	return 0;
}

static int late_disable_ev(struct snd_soc_dapm_widget *w,
			   struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_codec *codec = w->codec;
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);

	switch (event) {
	case SND_SOC_DAPM_POST_PMD:
		if (wm8994->aif1clk_disable) {
1298
			aif1clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMD);
1299 1300
			snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
					    WM8994_AIF1CLK_ENA_MASK, 0);
1301
			aif1clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMD);
1302 1303 1304
			wm8994->aif1clk_disable = 0;
		}
		if (wm8994->aif2clk_disable) {
1305
			aif2clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMD);
1306 1307
			snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
					    WM8994_AIF2CLK_ENA_MASK, 0);
1308
			aif2clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMD);
1309 1310 1311 1312 1313 1314 1315 1316
			wm8994->aif2clk_disable = 0;
		}
		break;
	}

	return 0;
}

1317 1318 1319 1320 1321 1322 1323
static int adc_mux_ev(struct snd_soc_dapm_widget *w,
		      struct snd_kcontrol *kcontrol, int event)
{
	late_enable_ev(w, kcontrol, event);
	return 0;
}

1324 1325 1326 1327 1328 1329 1330
static int micbias_ev(struct snd_soc_dapm_widget *w,
		      struct snd_kcontrol *kcontrol, int event)
{
	late_enable_ev(w, kcontrol, event);
	return 0;
}

1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
static int dac_ev(struct snd_soc_dapm_widget *w,
		  struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_codec *codec = w->codec;
	unsigned int mask = 1 << w->shift;

	snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
			    mask, mask);
	return 0;
}

M
Mark Brown 已提交
1342 1343 1344 1345 1346
static const char *adc_mux_text[] = {
	"ADC",
	"DMIC",
};

1347 1348
static SOC_ENUM_SINGLE_DECL(adc_enum,
			    0, 0, adc_mux_text);
M
Mark Brown 已提交
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396

static const struct snd_kcontrol_new adcl_mux =
	SOC_DAPM_ENUM_VIRT("ADCL Mux", adc_enum);

static const struct snd_kcontrol_new adcr_mux =
	SOC_DAPM_ENUM_VIRT("ADCR Mux", adc_enum);

static const struct snd_kcontrol_new left_speaker_mixer[] = {
SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 9, 1, 0),
SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 7, 1, 0),
SOC_DAPM_SINGLE("IN1LP Switch", WM8994_SPEAKER_MIXER, 5, 1, 0),
SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 3, 1, 0),
SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 1, 1, 0),
};

static const struct snd_kcontrol_new right_speaker_mixer[] = {
SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 8, 1, 0),
SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 6, 1, 0),
SOC_DAPM_SINGLE("IN1RP Switch", WM8994_SPEAKER_MIXER, 4, 1, 0),
SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 2, 1, 0),
SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 0, 1, 0),
};

/* Debugging; dump chip status after DAPM transitions */
static int post_ev(struct snd_soc_dapm_widget *w,
	    struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_codec *codec = w->codec;
	dev_dbg(codec->dev, "SRC status: %x\n",
		snd_soc_read(codec,
			     WM8994_RATE_STATUS));
	return 0;
}

static const struct snd_kcontrol_new aif1adc1l_mix[] = {
SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
		1, 1, 0),
SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
		0, 1, 0),
};

static const struct snd_kcontrol_new aif1adc1r_mix[] = {
SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
		1, 1, 0),
SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
		0, 1, 0),
};

1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
static const struct snd_kcontrol_new aif1adc2l_mix[] = {
SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
		1, 1, 0),
SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
		0, 1, 0),
};

static const struct snd_kcontrol_new aif1adc2r_mix[] = {
SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
		1, 1, 0),
SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
		0, 1, 0),
};

M
Mark Brown 已提交
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
static const struct snd_kcontrol_new aif2dac2l_mix[] = {
SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
		5, 1, 0),
SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
		4, 1, 0),
SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
		2, 1, 0),
SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
		1, 1, 0),
SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
		0, 1, 0),
};

static const struct snd_kcontrol_new aif2dac2r_mix[] = {
SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
		5, 1, 0),
SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
		4, 1, 0),
SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
		2, 1, 0),
SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
		1, 1, 0),
SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
		0, 1, 0),
};

#define WM8994_CLASS_W_SWITCH(xname, reg, shift, max, invert) \
1438
	SOC_SINGLE_EXT(xname, reg, shift, max, invert, \
M
Mark Brown 已提交
1439
		snd_soc_dapm_get_volsw, wm8994_put_class_w)
M
Mark Brown 已提交
1440 1441 1442 1443

static int wm8994_put_class_w(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_value *ucontrol)
{
1444
	struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
M
Mark Brown 已提交
1445 1446 1447 1448
	int ret;

	ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);

1449
	wm_hubs_update_class_w(codec);
M
Mark Brown 已提交
1450 1451 1452 1453 1454 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 1483

	return ret;
}

static const struct snd_kcontrol_new dac1l_mix[] = {
WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
		      5, 1, 0),
WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
		      4, 1, 0),
WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
		      2, 1, 0),
WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
		      1, 1, 0),
WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
		      0, 1, 0),
};

static const struct snd_kcontrol_new dac1r_mix[] = {
WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
		      5, 1, 0),
WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
		      4, 1, 0),
WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
		      2, 1, 0),
WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
		      1, 1, 0),
WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
		      0, 1, 0),
};

static const char *sidetone_text[] = {
	"ADC/DMIC1", "DMIC2",
};

1484 1485
static SOC_ENUM_SINGLE_DECL(sidetone1_enum,
			    WM8994_SIDETONE, 0, sidetone_text);
M
Mark Brown 已提交
1486 1487 1488 1489

static const struct snd_kcontrol_new sidetone1_mux =
	SOC_DAPM_ENUM("Left Sidetone Mux", sidetone1_enum);

1490 1491
static SOC_ENUM_SINGLE_DECL(sidetone2_enum,
			    WM8994_SIDETONE, 1, sidetone_text);
M
Mark Brown 已提交
1492 1493 1494 1495 1496 1497 1498 1499

static const struct snd_kcontrol_new sidetone2_mux =
	SOC_DAPM_ENUM("Right Sidetone Mux", sidetone2_enum);

static const char *aif1dac_text[] = {
	"AIF1DACDAT", "AIF3DACDAT",
};

1500 1501 1502 1503
static const char *loopback_text[] = {
	"None", "ADCDAT",
};

1504 1505 1506 1507
static SOC_ENUM_SINGLE_DECL(aif1_loopback_enum,
			    WM8994_AIF1_CONTROL_2,
			    WM8994_AIF1_LOOPBACK_SHIFT,
			    loopback_text);
1508 1509 1510 1511

static const struct snd_kcontrol_new aif1_loopback =
	SOC_DAPM_ENUM("AIF1 Loopback", aif1_loopback_enum);

1512 1513 1514 1515
static SOC_ENUM_SINGLE_DECL(aif2_loopback_enum,
			    WM8994_AIF2_CONTROL_2,
			    WM8994_AIF2_LOOPBACK_SHIFT,
			    loopback_text);
1516 1517 1518 1519

static const struct snd_kcontrol_new aif2_loopback =
	SOC_DAPM_ENUM("AIF2 Loopback", aif2_loopback_enum);

1520 1521
static SOC_ENUM_SINGLE_DECL(aif1dac_enum,
			    WM8994_POWER_MANAGEMENT_6, 0, aif1dac_text);
M
Mark Brown 已提交
1522 1523 1524 1525 1526 1527 1528 1529

static const struct snd_kcontrol_new aif1dac_mux =
	SOC_DAPM_ENUM("AIF1DAC Mux", aif1dac_enum);

static const char *aif2dac_text[] = {
	"AIF2DACDAT", "AIF3DACDAT",
};

1530 1531
static SOC_ENUM_SINGLE_DECL(aif2dac_enum,
			    WM8994_POWER_MANAGEMENT_6, 1, aif2dac_text);
M
Mark Brown 已提交
1532 1533 1534 1535 1536 1537 1538 1539

static const struct snd_kcontrol_new aif2dac_mux =
	SOC_DAPM_ENUM("AIF2DAC Mux", aif2dac_enum);

static const char *aif2adc_text[] = {
	"AIF2ADCDAT", "AIF3DACDAT",
};

1540 1541
static SOC_ENUM_SINGLE_DECL(aif2adc_enum,
			    WM8994_POWER_MANAGEMENT_6, 2, aif2adc_text);
M
Mark Brown 已提交
1542 1543 1544 1545 1546

static const struct snd_kcontrol_new aif2adc_mux =
	SOC_DAPM_ENUM("AIF2ADC Mux", aif2adc_enum);

static const char *aif3adc_text[] = {
1547
	"AIF1ADCDAT", "AIF2ADCDAT", "AIF2DACDAT", "Mono PCM",
M
Mark Brown 已提交
1548 1549
};

1550 1551
static SOC_ENUM_SINGLE_DECL(wm8994_aif3adc_enum,
			    WM8994_POWER_MANAGEMENT_6, 3, aif3adc_text);
M
Mark Brown 已提交
1552

1553 1554 1555
static const struct snd_kcontrol_new wm8994_aif3adc_mux =
	SOC_DAPM_ENUM("AIF3ADC Mux", wm8994_aif3adc_enum);

1556 1557
static SOC_ENUM_SINGLE_DECL(wm8958_aif3adc_enum,
			    WM8994_POWER_MANAGEMENT_6, 3, aif3adc_text);
1558 1559 1560 1561 1562

static const struct snd_kcontrol_new wm8958_aif3adc_mux =
	SOC_DAPM_ENUM("AIF3ADC Mux", wm8958_aif3adc_enum);

static const char *mono_pcm_out_text[] = {
1563
	"None", "AIF2ADCL", "AIF2ADCR",
1564 1565
};

1566 1567
static SOC_ENUM_SINGLE_DECL(mono_pcm_out_enum,
			    WM8994_POWER_MANAGEMENT_6, 9, mono_pcm_out_text);
1568 1569 1570 1571 1572 1573 1574 1575 1576

static const struct snd_kcontrol_new mono_pcm_out_mux =
	SOC_DAPM_ENUM("Mono PCM Out Mux", mono_pcm_out_enum);

static const char *aif2dac_src_text[] = {
	"AIF2", "AIF3",
};

/* Note that these two control shouldn't be simultaneously switched to AIF3 */
1577 1578
static SOC_ENUM_SINGLE_DECL(aif2dacl_src_enum,
			    WM8994_POWER_MANAGEMENT_6, 7, aif2dac_src_text);
1579 1580 1581 1582

static const struct snd_kcontrol_new aif2dacl_src_mux =
	SOC_DAPM_ENUM("AIF2DACL Mux", aif2dacl_src_enum);

1583 1584
static SOC_ENUM_SINGLE_DECL(aif2dacr_src_enum,
			    WM8994_POWER_MANAGEMENT_6, 8, aif2dac_src_text);
1585 1586 1587

static const struct snd_kcontrol_new aif2dacr_src_mux =
	SOC_DAPM_ENUM("AIF2DACR Mux", aif2dacr_src_enum);
M
Mark Brown 已提交
1588

1589
static const struct snd_soc_dapm_widget wm8994_lateclk_revd_widgets[] = {
1590
SND_SOC_DAPM_SUPPLY("AIF1CLK", SND_SOC_NOPM, 0, 0, aif1clk_late_ev,
1591
	SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1592
SND_SOC_DAPM_SUPPLY("AIF2CLK", SND_SOC_NOPM, 0, 0, aif2clk_late_ev,
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
	SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),

SND_SOC_DAPM_PGA_E("Late DAC1L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
	late_enable_ev, SND_SOC_DAPM_PRE_PMU),
SND_SOC_DAPM_PGA_E("Late DAC1R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
	late_enable_ev, SND_SOC_DAPM_PRE_PMU),
SND_SOC_DAPM_PGA_E("Late DAC2L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
	late_enable_ev, SND_SOC_DAPM_PRE_PMU),
SND_SOC_DAPM_PGA_E("Late DAC2R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
	late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1603 1604 1605 1606 1607 1608 1609 1610 1611
SND_SOC_DAPM_PGA_E("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0,
	late_enable_ev, SND_SOC_DAPM_PRE_PMU),

SND_SOC_DAPM_MIXER_E("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
		     left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer),
		     late_enable_ev, SND_SOC_DAPM_PRE_PMU),
SND_SOC_DAPM_MIXER_E("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
		     right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer),
		     late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1612
SND_SOC_DAPM_MUX_E("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpl_mux,
1613
		   late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1614
SND_SOC_DAPM_MUX_E("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpr_mux,
1615
		   late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1616 1617 1618 1619 1620

SND_SOC_DAPM_POST("Late Disable PGA", late_disable_ev)
};

static const struct snd_soc_dapm_widget wm8994_lateclk_widgets[] = {
1621
SND_SOC_DAPM_SUPPLY("AIF1CLK", WM8994_AIF1_CLOCKING_1, 0, 0, aif1clk_ev,
1622 1623
		    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
		    SND_SOC_DAPM_PRE_PMD),
1624
SND_SOC_DAPM_SUPPLY("AIF2CLK", WM8994_AIF2_CLOCKING_1, 0, 0, aif2clk_ev,
1625 1626
		    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
		    SND_SOC_DAPM_PRE_PMD),
1627 1628 1629 1630 1631
SND_SOC_DAPM_PGA("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0),
SND_SOC_DAPM_MIXER("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
		   left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
SND_SOC_DAPM_MIXER("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
		   right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
1632 1633
SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpl_mux),
SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpr_mux),
1634 1635
};

1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
static const struct snd_soc_dapm_widget wm8994_dac_revd_widgets[] = {
SND_SOC_DAPM_DAC_E("DAC2L", NULL, SND_SOC_NOPM, 3, 0,
	dac_ev, SND_SOC_DAPM_PRE_PMU),
SND_SOC_DAPM_DAC_E("DAC2R", NULL, SND_SOC_NOPM, 2, 0,
	dac_ev, SND_SOC_DAPM_PRE_PMU),
SND_SOC_DAPM_DAC_E("DAC1L", NULL, SND_SOC_NOPM, 1, 0,
	dac_ev, SND_SOC_DAPM_PRE_PMU),
SND_SOC_DAPM_DAC_E("DAC1R", NULL, SND_SOC_NOPM, 0, 0,
	dac_ev, SND_SOC_DAPM_PRE_PMU),
};

static const struct snd_soc_dapm_widget wm8994_dac_widgets[] = {
SND_SOC_DAPM_DAC("DAC2L", NULL, WM8994_POWER_MANAGEMENT_5, 3, 0),
1649
SND_SOC_DAPM_DAC("DAC2R", NULL, WM8994_POWER_MANAGEMENT_5, 2, 0),
1650 1651 1652 1653
SND_SOC_DAPM_DAC("DAC1L", NULL, WM8994_POWER_MANAGEMENT_5, 1, 0),
SND_SOC_DAPM_DAC("DAC1R", NULL, WM8994_POWER_MANAGEMENT_5, 0, 0),
};

1654
static const struct snd_soc_dapm_widget wm8994_adc_revd_widgets[] = {
1655 1656 1657 1658
SND_SOC_DAPM_VIRT_MUX_E("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux,
			adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
SND_SOC_DAPM_VIRT_MUX_E("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux,
			adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
1659 1660 1661
};

static const struct snd_soc_dapm_widget wm8994_adc_widgets[] = {
1662 1663
SND_SOC_DAPM_VIRT_MUX("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux),
SND_SOC_DAPM_VIRT_MUX("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux),
1664 1665
};

M
Mark Brown 已提交
1666 1667 1668
static const struct snd_soc_dapm_widget wm8994_dapm_widgets[] = {
SND_SOC_DAPM_INPUT("DMIC1DAT"),
SND_SOC_DAPM_INPUT("DMIC2DAT"),
1669
SND_SOC_DAPM_INPUT("Clock"),
M
Mark Brown 已提交
1670

1671 1672
SND_SOC_DAPM_SUPPLY_S("MICBIAS Supply", 1, SND_SOC_NOPM, 0, 0, micbias_ev,
		      SND_SOC_DAPM_PRE_PMU),
1673 1674
SND_SOC_DAPM_SUPPLY("VMID", SND_SOC_NOPM, 0, 0, vmid_event,
		    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1675

M
Mark Brown 已提交
1676
SND_SOC_DAPM_SUPPLY("CLK_SYS", SND_SOC_NOPM, 0, 0, clk_sys_event,
1677 1678
		    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
		    SND_SOC_DAPM_PRE_PMD),
M
Mark Brown 已提交
1679

1680 1681 1682
SND_SOC_DAPM_SUPPLY("DSP1CLK", SND_SOC_NOPM, 3, 0, NULL, 0),
SND_SOC_DAPM_SUPPLY("DSP2CLK", SND_SOC_NOPM, 2, 0, NULL, 0),
SND_SOC_DAPM_SUPPLY("DSPINTCLK", SND_SOC_NOPM, 1, 0, NULL, 0),
M
Mark Brown 已提交
1683

1684
SND_SOC_DAPM_AIF_OUT("AIF1ADC1L", NULL,
1685
		     0, SND_SOC_NOPM, 9, 0),
1686
SND_SOC_DAPM_AIF_OUT("AIF1ADC1R", NULL,
1687
		     0, SND_SOC_NOPM, 8, 0),
1688
SND_SOC_DAPM_AIF_IN_E("AIF1DAC1L", NULL, 0,
1689
		      SND_SOC_NOPM, 9, 0, wm8958_aif_ev,
1690
		      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1691
SND_SOC_DAPM_AIF_IN_E("AIF1DAC1R", NULL, 0,
1692
		      SND_SOC_NOPM, 8, 0, wm8958_aif_ev,
1693
		      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
M
Mark Brown 已提交
1694

1695
SND_SOC_DAPM_AIF_OUT("AIF1ADC2L", NULL,
1696
		     0, SND_SOC_NOPM, 11, 0),
1697
SND_SOC_DAPM_AIF_OUT("AIF1ADC2R", NULL,
1698
		     0, SND_SOC_NOPM, 10, 0),
1699
SND_SOC_DAPM_AIF_IN_E("AIF1DAC2L", NULL, 0,
1700
		      SND_SOC_NOPM, 11, 0, wm8958_aif_ev,
1701
		      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1702
SND_SOC_DAPM_AIF_IN_E("AIF1DAC2R", NULL, 0,
1703
		      SND_SOC_NOPM, 10, 0, wm8958_aif_ev,
1704
		      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
M
Mark Brown 已提交
1705 1706 1707 1708 1709 1710

SND_SOC_DAPM_MIXER("AIF1ADC1L Mixer", SND_SOC_NOPM, 0, 0,
		   aif1adc1l_mix, ARRAY_SIZE(aif1adc1l_mix)),
SND_SOC_DAPM_MIXER("AIF1ADC1R Mixer", SND_SOC_NOPM, 0, 0,
		   aif1adc1r_mix, ARRAY_SIZE(aif1adc1r_mix)),

1711 1712 1713 1714 1715
SND_SOC_DAPM_MIXER("AIF1ADC2L Mixer", SND_SOC_NOPM, 0, 0,
		   aif1adc2l_mix, ARRAY_SIZE(aif1adc2l_mix)),
SND_SOC_DAPM_MIXER("AIF1ADC2R Mixer", SND_SOC_NOPM, 0, 0,
		   aif1adc2r_mix, ARRAY_SIZE(aif1adc2r_mix)),

M
Mark Brown 已提交
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729
SND_SOC_DAPM_MIXER("AIF2DAC2L Mixer", SND_SOC_NOPM, 0, 0,
		   aif2dac2l_mix, ARRAY_SIZE(aif2dac2l_mix)),
SND_SOC_DAPM_MIXER("AIF2DAC2R Mixer", SND_SOC_NOPM, 0, 0,
		   aif2dac2r_mix, ARRAY_SIZE(aif2dac2r_mix)),

SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &sidetone1_mux),
SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &sidetone2_mux),

SND_SOC_DAPM_MIXER("DAC1L Mixer", SND_SOC_NOPM, 0, 0,
		   dac1l_mix, ARRAY_SIZE(dac1l_mix)),
SND_SOC_DAPM_MIXER("DAC1R Mixer", SND_SOC_NOPM, 0, 0,
		   dac1r_mix, ARRAY_SIZE(dac1r_mix)),

SND_SOC_DAPM_AIF_OUT("AIF2ADCL", NULL, 0,
1730
		     SND_SOC_NOPM, 13, 0),
M
Mark Brown 已提交
1731
SND_SOC_DAPM_AIF_OUT("AIF2ADCR", NULL, 0,
1732
		     SND_SOC_NOPM, 12, 0),
1733
SND_SOC_DAPM_AIF_IN_E("AIF2DACL", NULL, 0,
1734
		      SND_SOC_NOPM, 13, 0, wm8958_aif_ev,
1735 1736
		      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
SND_SOC_DAPM_AIF_IN_E("AIF2DACR", NULL, 0,
1737
		      SND_SOC_NOPM, 12, 0, wm8958_aif_ev,
1738
		      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
M
Mark Brown 已提交
1739

1740 1741 1742 1743
SND_SOC_DAPM_AIF_IN("AIF1DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
SND_SOC_DAPM_AIF_IN("AIF2DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
SND_SOC_DAPM_AIF_OUT("AIF1ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
SND_SOC_DAPM_AIF_OUT("AIF2ADCDAT",  NULL, 0, SND_SOC_NOPM, 0, 0),
M
Mark Brown 已提交
1744 1745 1746 1747 1748

SND_SOC_DAPM_MUX("AIF1DAC Mux", SND_SOC_NOPM, 0, 0, &aif1dac_mux),
SND_SOC_DAPM_MUX("AIF2DAC Mux", SND_SOC_NOPM, 0, 0, &aif2dac_mux),
SND_SOC_DAPM_MUX("AIF2ADC Mux", SND_SOC_NOPM, 0, 0, &aif2adc_mux),

1749 1750
SND_SOC_DAPM_AIF_IN("AIF3DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
SND_SOC_DAPM_AIF_OUT("AIF3ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
M
Mark Brown 已提交
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765

SND_SOC_DAPM_SUPPLY("TOCLK", WM8994_CLOCKING_1, 4, 0, NULL, 0),

SND_SOC_DAPM_ADC("DMIC2L", NULL, WM8994_POWER_MANAGEMENT_4, 5, 0),
SND_SOC_DAPM_ADC("DMIC2R", NULL, WM8994_POWER_MANAGEMENT_4, 4, 0),
SND_SOC_DAPM_ADC("DMIC1L", NULL, WM8994_POWER_MANAGEMENT_4, 3, 0),
SND_SOC_DAPM_ADC("DMIC1R", NULL, WM8994_POWER_MANAGEMENT_4, 2, 0),

/* Power is done with the muxes since the ADC power also controls the
 * downsampling chain, the chip will automatically manage the analogue
 * specific portions.
 */
SND_SOC_DAPM_ADC("ADCL", NULL, SND_SOC_NOPM, 1, 0),
SND_SOC_DAPM_ADC("ADCR", NULL, SND_SOC_NOPM, 0, 0),

1766 1767 1768
SND_SOC_DAPM_MUX("AIF1 Loopback", SND_SOC_NOPM, 0, 0, &aif1_loopback),
SND_SOC_DAPM_MUX("AIF2 Loopback", SND_SOC_NOPM, 0, 0, &aif2_loopback),

M
Mark Brown 已提交
1769 1770 1771
SND_SOC_DAPM_POST("Debug log", post_ev),
};

1772 1773 1774
static const struct snd_soc_dapm_widget wm8994_specific_dapm_widgets[] = {
SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8994_aif3adc_mux),
};
M
Mark Brown 已提交
1775

1776
static const struct snd_soc_dapm_widget wm8958_dapm_widgets[] = {
1777
SND_SOC_DAPM_SUPPLY("AIF3", WM8994_POWER_MANAGEMENT_6, 5, 1, NULL, 0),
1778 1779 1780 1781 1782 1783 1784
SND_SOC_DAPM_MUX("Mono PCM Out Mux", SND_SOC_NOPM, 0, 0, &mono_pcm_out_mux),
SND_SOC_DAPM_MUX("AIF2DACL Mux", SND_SOC_NOPM, 0, 0, &aif2dacl_src_mux),
SND_SOC_DAPM_MUX("AIF2DACR Mux", SND_SOC_NOPM, 0, 0, &aif2dacr_src_mux),
SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8958_aif3adc_mux),
};

static const struct snd_soc_dapm_route intercon[] = {
M
Mark Brown 已提交
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
	{ "CLK_SYS", NULL, "AIF1CLK", check_clk_sys },
	{ "CLK_SYS", NULL, "AIF2CLK", check_clk_sys },

	{ "DSP1CLK", NULL, "CLK_SYS" },
	{ "DSP2CLK", NULL, "CLK_SYS" },
	{ "DSPINTCLK", NULL, "CLK_SYS" },

	{ "AIF1ADC1L", NULL, "AIF1CLK" },
	{ "AIF1ADC1L", NULL, "DSP1CLK" },
	{ "AIF1ADC1R", NULL, "AIF1CLK" },
	{ "AIF1ADC1R", NULL, "DSP1CLK" },
	{ "AIF1ADC1R", NULL, "DSPINTCLK" },

	{ "AIF1DAC1L", NULL, "AIF1CLK" },
	{ "AIF1DAC1L", NULL, "DSP1CLK" },
	{ "AIF1DAC1R", NULL, "AIF1CLK" },
	{ "AIF1DAC1R", NULL, "DSP1CLK" },
	{ "AIF1DAC1R", NULL, "DSPINTCLK" },

	{ "AIF1ADC2L", NULL, "AIF1CLK" },
	{ "AIF1ADC2L", NULL, "DSP1CLK" },
	{ "AIF1ADC2R", NULL, "AIF1CLK" },
	{ "AIF1ADC2R", NULL, "DSP1CLK" },
	{ "AIF1ADC2R", NULL, "DSPINTCLK" },

	{ "AIF1DAC2L", NULL, "AIF1CLK" },
	{ "AIF1DAC2L", NULL, "DSP1CLK" },
	{ "AIF1DAC2R", NULL, "AIF1CLK" },
	{ "AIF1DAC2R", NULL, "DSP1CLK" },
	{ "AIF1DAC2R", NULL, "DSPINTCLK" },

	{ "AIF2ADCL", NULL, "AIF2CLK" },
	{ "AIF2ADCL", NULL, "DSP2CLK" },
	{ "AIF2ADCR", NULL, "AIF2CLK" },
	{ "AIF2ADCR", NULL, "DSP2CLK" },
	{ "AIF2ADCR", NULL, "DSPINTCLK" },

	{ "AIF2DACL", NULL, "AIF2CLK" },
	{ "AIF2DACL", NULL, "DSP2CLK" },
	{ "AIF2DACR", NULL, "AIF2CLK" },
	{ "AIF2DACR", NULL, "DSP2CLK" },
	{ "AIF2DACR", NULL, "DSPINTCLK" },

	{ "DMIC1L", NULL, "DMIC1DAT" },
	{ "DMIC1L", NULL, "CLK_SYS" },
	{ "DMIC1R", NULL, "DMIC1DAT" },
	{ "DMIC1R", NULL, "CLK_SYS" },
	{ "DMIC2L", NULL, "DMIC2DAT" },
	{ "DMIC2L", NULL, "CLK_SYS" },
	{ "DMIC2R", NULL, "DMIC2DAT" },
	{ "DMIC2R", NULL, "CLK_SYS" },

	{ "ADCL", NULL, "AIF1CLK" },
	{ "ADCL", NULL, "DSP1CLK" },
	{ "ADCL", NULL, "DSPINTCLK" },

	{ "ADCR", NULL, "AIF1CLK" },
	{ "ADCR", NULL, "DSP1CLK" },
	{ "ADCR", NULL, "DSPINTCLK" },

	{ "ADCL Mux", "ADC", "ADCL" },
	{ "ADCL Mux", "DMIC", "DMIC1L" },
	{ "ADCR Mux", "ADC", "ADCR" },
	{ "ADCR Mux", "DMIC", "DMIC1R" },

	{ "DAC1L", NULL, "AIF1CLK" },
	{ "DAC1L", NULL, "DSP1CLK" },
	{ "DAC1L", NULL, "DSPINTCLK" },

	{ "DAC1R", NULL, "AIF1CLK" },
	{ "DAC1R", NULL, "DSP1CLK" },
	{ "DAC1R", NULL, "DSPINTCLK" },

	{ "DAC2L", NULL, "AIF2CLK" },
	{ "DAC2L", NULL, "DSP2CLK" },
	{ "DAC2L", NULL, "DSPINTCLK" },

	{ "DAC2R", NULL, "AIF2DACR" },
	{ "DAC2R", NULL, "AIF2CLK" },
	{ "DAC2R", NULL, "DSP2CLK" },
	{ "DAC2R", NULL, "DSPINTCLK" },

	{ "TOCLK", NULL, "CLK_SYS" },

1869 1870 1871 1872 1873 1874 1875 1876
	{ "AIF1DACDAT", NULL, "AIF1 Playback" },
	{ "AIF2DACDAT", NULL, "AIF2 Playback" },
	{ "AIF3DACDAT", NULL, "AIF3 Playback" },

	{ "AIF1 Capture", NULL, "AIF1ADCDAT" },
	{ "AIF2 Capture", NULL, "AIF2ADCDAT" },
	{ "AIF3 Capture", NULL, "AIF3ADCDAT" },

M
Mark Brown 已提交
1877 1878 1879 1880 1881 1882 1883 1884 1885
	/* AIF1 outputs */
	{ "AIF1ADC1L", NULL, "AIF1ADC1L Mixer" },
	{ "AIF1ADC1L Mixer", "ADC/DMIC Switch", "ADCL Mux" },
	{ "AIF1ADC1L Mixer", "AIF2 Switch", "AIF2DACL" },

	{ "AIF1ADC1R", NULL, "AIF1ADC1R Mixer" },
	{ "AIF1ADC1R Mixer", "ADC/DMIC Switch", "ADCR Mux" },
	{ "AIF1ADC1R Mixer", "AIF2 Switch", "AIF2DACR" },

1886 1887 1888 1889 1890 1891 1892 1893
	{ "AIF1ADC2L", NULL, "AIF1ADC2L Mixer" },
	{ "AIF1ADC2L Mixer", "DMIC Switch", "DMIC2L" },
	{ "AIF1ADC2L Mixer", "AIF2 Switch", "AIF2DACL" },

	{ "AIF1ADC2R", NULL, "AIF1ADC2R Mixer" },
	{ "AIF1ADC2R Mixer", "DMIC Switch", "DMIC2R" },
	{ "AIF1ADC2R Mixer", "AIF2 Switch", "AIF2DACR" },

M
Mark Brown 已提交
1894 1895 1896 1897 1898 1899
	/* Pin level routing for AIF3 */
	{ "AIF1DAC1L", NULL, "AIF1DAC Mux" },
	{ "AIF1DAC1R", NULL, "AIF1DAC Mux" },
	{ "AIF1DAC2L", NULL, "AIF1DAC Mux" },
	{ "AIF1DAC2R", NULL, "AIF1DAC Mux" },

1900
	{ "AIF1DAC Mux", "AIF1DACDAT", "AIF1 Loopback" },
M
Mark Brown 已提交
1901
	{ "AIF1DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1902
	{ "AIF2DAC Mux", "AIF2DACDAT", "AIF2 Loopback" },
M
Mark Brown 已提交
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935
	{ "AIF2DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
	{ "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCL" },
	{ "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCR" },
	{ "AIF2ADC Mux", "AIF3DACDAT", "AIF3ADCDAT" },

	/* DAC1 inputs */
	{ "DAC1L Mixer", "AIF2 Switch", "AIF2DACL" },
	{ "DAC1L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
	{ "DAC1L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
	{ "DAC1L Mixer", "Left Sidetone Switch", "Left Sidetone" },
	{ "DAC1L Mixer", "Right Sidetone Switch", "Right Sidetone" },

	{ "DAC1R Mixer", "AIF2 Switch", "AIF2DACR" },
	{ "DAC1R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
	{ "DAC1R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
	{ "DAC1R Mixer", "Left Sidetone Switch", "Left Sidetone" },
	{ "DAC1R Mixer", "Right Sidetone Switch", "Right Sidetone" },

	/* DAC2/AIF2 outputs  */
	{ "AIF2ADCL", NULL, "AIF2DAC2L Mixer" },
	{ "AIF2DAC2L Mixer", "AIF2 Switch", "AIF2DACL" },
	{ "AIF2DAC2L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
	{ "AIF2DAC2L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
	{ "AIF2DAC2L Mixer", "Left Sidetone Switch", "Left Sidetone" },
	{ "AIF2DAC2L Mixer", "Right Sidetone Switch", "Right Sidetone" },

	{ "AIF2ADCR", NULL, "AIF2DAC2R Mixer" },
	{ "AIF2DAC2R Mixer", "AIF2 Switch", "AIF2DACR" },
	{ "AIF2DAC2R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
	{ "AIF2DAC2R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
	{ "AIF2DAC2R Mixer", "Left Sidetone Switch", "Left Sidetone" },
	{ "AIF2DAC2R Mixer", "Right Sidetone Switch", "Right Sidetone" },

1936 1937 1938 1939 1940
	{ "AIF1ADCDAT", NULL, "AIF1ADC1L" },
	{ "AIF1ADCDAT", NULL, "AIF1ADC1R" },
	{ "AIF1ADCDAT", NULL, "AIF1ADC2L" },
	{ "AIF1ADCDAT", NULL, "AIF1ADC2R" },

M
Mark Brown 已提交
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
	{ "AIF2ADCDAT", NULL, "AIF2ADC Mux" },

	/* AIF3 output */
	{ "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1L" },
	{ "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1R" },
	{ "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2L" },
	{ "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2R" },
	{ "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCL" },
	{ "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCR" },
	{ "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACL" },
	{ "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACR" },

1953 1954 1955 1956 1957 1958
	/* Loopback */
	{ "AIF1 Loopback", "ADCDAT", "AIF1ADCDAT" },
	{ "AIF1 Loopback", "None", "AIF1DACDAT" },
	{ "AIF2 Loopback", "ADCDAT", "AIF2ADCDAT" },
	{ "AIF2 Loopback", "None", "AIF2DACDAT" },

M
Mark Brown 已提交
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
	/* Sidetone */
	{ "Left Sidetone", "ADC/DMIC1", "ADCL Mux" },
	{ "Left Sidetone", "DMIC2", "DMIC2L" },
	{ "Right Sidetone", "ADC/DMIC1", "ADCR Mux" },
	{ "Right Sidetone", "DMIC2", "DMIC2R" },

	/* Output stages */
	{ "Left Output Mixer", "DAC Switch", "DAC1L" },
	{ "Right Output Mixer", "DAC Switch", "DAC1R" },

	{ "SPKL", "DAC1 Switch", "DAC1L" },
	{ "SPKL", "DAC2 Switch", "DAC2L" },

	{ "SPKR", "DAC1 Switch", "DAC1R" },
	{ "SPKR", "DAC2 Switch", "DAC2R" },

	{ "Left Headphone Mux", "DAC", "DAC1L" },
	{ "Right Headphone Mux", "DAC", "DAC1R" },
};

1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
static const struct snd_soc_dapm_route wm8994_lateclk_revd_intercon[] = {
	{ "DAC1L", NULL, "Late DAC1L Enable PGA" },
	{ "Late DAC1L Enable PGA", NULL, "DAC1L Mixer" },
	{ "DAC1R", NULL, "Late DAC1R Enable PGA" },
	{ "Late DAC1R Enable PGA", NULL, "DAC1R Mixer" },
	{ "DAC2L", NULL, "Late DAC2L Enable PGA" },
	{ "Late DAC2L Enable PGA", NULL, "AIF2DAC2L Mixer" },
	{ "DAC2R", NULL, "Late DAC2R Enable PGA" },
	{ "Late DAC2R Enable PGA", NULL, "AIF2DAC2R Mixer" }
};

static const struct snd_soc_dapm_route wm8994_lateclk_intercon[] = {
	{ "DAC1L", NULL, "DAC1L Mixer" },
	{ "DAC1R", NULL, "DAC1R Mixer" },
	{ "DAC2L", NULL, "AIF2DAC2L Mixer" },
	{ "DAC2R", NULL, "AIF2DAC2R Mixer" },
};

1997 1998 1999 2000 2001
static const struct snd_soc_dapm_route wm8994_revd_intercon[] = {
	{ "AIF1DACDAT", NULL, "AIF2DACDAT" },
	{ "AIF2DACDAT", NULL, "AIF1DACDAT" },
	{ "AIF1ADCDAT", NULL, "AIF2ADCDAT" },
	{ "AIF2ADCDAT", NULL, "AIF1ADCDAT" },
2002 2003 2004 2005
	{ "MICBIAS1", NULL, "CLK_SYS" },
	{ "MICBIAS1", NULL, "MICBIAS Supply" },
	{ "MICBIAS2", NULL, "CLK_SYS" },
	{ "MICBIAS2", NULL, "MICBIAS Supply" },
2006 2007
};

2008 2009 2010
static const struct snd_soc_dapm_route wm8994_intercon[] = {
	{ "AIF2DACL", NULL, "AIF2DAC Mux" },
	{ "AIF2DACR", NULL, "AIF2DAC Mux" },
2011 2012
	{ "MICBIAS1", NULL, "VMID" },
	{ "MICBIAS2", NULL, "VMID" },
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
};

static const struct snd_soc_dapm_route wm8958_intercon[] = {
	{ "AIF2DACL", NULL, "AIF2DACL Mux" },
	{ "AIF2DACR", NULL, "AIF2DACR Mux" },

	{ "AIF2DACL Mux", "AIF2", "AIF2DAC Mux" },
	{ "AIF2DACL Mux", "AIF3", "AIF3DACDAT" },
	{ "AIF2DACR Mux", "AIF2", "AIF2DAC Mux" },
	{ "AIF2DACR Mux", "AIF3", "AIF3DACDAT" },

2024 2025 2026
	{ "AIF3DACDAT", NULL, "AIF3" },
	{ "AIF3ADCDAT", NULL, "AIF3" },

2027 2028 2029 2030 2031 2032
	{ "Mono PCM Out Mux", "AIF2ADCL", "AIF2ADCL" },
	{ "Mono PCM Out Mux", "AIF2ADCR", "AIF2ADCR" },

	{ "AIF3ADC Mux", "Mono PCM", "Mono PCM Out Mux" },
};

M
Mark Brown 已提交
2033 2034 2035 2036 2037 2038 2039 2040
/* The size in bits of the FLL divide multiplied by 10
 * to allow rounding later */
#define FIXED_FLL_SIZE ((1 << 16) * 10)

struct fll_div {
	u16 outdiv;
	u16 n;
	u16 k;
2041
	u16 lambda;
M
Mark Brown 已提交
2042 2043 2044 2045
	u16 clk_ref_div;
	u16 fll_fratio;
};

2046
static int wm8994_get_fll_config(struct wm8994 *control, struct fll_div *fll,
M
Mark Brown 已提交
2047 2048 2049
				 int freq_in, int freq_out)
{
	u64 Kpart;
2050
	unsigned int K, Ndiv, Nmod, gcd_fll;
M
Mark Brown 已提交
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076

	pr_debug("FLL input=%dHz, output=%dHz\n", freq_in, freq_out);

	/* Scale the input frequency down to <= 13.5MHz */
	fll->clk_ref_div = 0;
	while (freq_in > 13500000) {
		fll->clk_ref_div++;
		freq_in /= 2;

		if (fll->clk_ref_div > 3)
			return -EINVAL;
	}
	pr_debug("CLK_REF_DIV=%d, Fref=%dHz\n", fll->clk_ref_div, freq_in);

	/* Scale the output to give 90MHz<=Fvco<=100MHz */
	fll->outdiv = 3;
	while (freq_out * (fll->outdiv + 1) < 90000000) {
		fll->outdiv++;
		if (fll->outdiv > 63)
			return -EINVAL;
	}
	freq_out *= fll->outdiv + 1;
	pr_debug("OUTDIV=%d, Fvco=%dHz\n", fll->outdiv, freq_out);

	if (freq_in > 1000000) {
		fll->fll_fratio = 0;
2077 2078 2079 2080 2081 2082 2083
	} else if (freq_in > 256000) {
		fll->fll_fratio = 1;
		freq_in *= 2;
	} else if (freq_in > 128000) {
		fll->fll_fratio = 2;
		freq_in *= 4;
	} else if (freq_in > 64000) {
M
Mark Brown 已提交
2084 2085
		fll->fll_fratio = 3;
		freq_in *= 8;
2086 2087 2088
	} else {
		fll->fll_fratio = 4;
		freq_in *= 16;
M
Mark Brown 已提交
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
	}
	pr_debug("FLL_FRATIO=%d, Fref=%dHz\n", fll->fll_fratio, freq_in);

	/* Now, calculate N.K */
	Ndiv = freq_out / freq_in;

	fll->n = Ndiv;
	Nmod = freq_out % freq_in;
	pr_debug("Nmod=%d\n", Nmod);

2099 2100 2101 2102
	switch (control->type) {
	case WM8994:
		/* Calculate fractional part - scale up so we can round. */
		Kpart = FIXED_FLL_SIZE * (long long)Nmod;
M
Mark Brown 已提交
2103

2104
		do_div(Kpart, freq_in);
M
Mark Brown 已提交
2105

2106
		K = Kpart & 0xFFFFFFFF;
M
Mark Brown 已提交
2107

2108 2109
		if ((K % 10) >= 5)
			K += 5;
M
Mark Brown 已提交
2110

2111 2112
		/* Move down to proper range now rounding is done */
		fll->k = K / 10;
2113
		fll->lambda = 0;
M
Mark Brown 已提交
2114

2115
		pr_debug("N=%x K=%x\n", fll->n, fll->k);
2116
		break;
M
Mark Brown 已提交
2117

2118 2119
	default:
		gcd_fll = gcd(freq_out, freq_in);
M
Mark Brown 已提交
2120

2121 2122 2123 2124
		fll->k = (freq_out - (freq_in * fll->n)) / gcd_fll;
		fll->lambda = freq_in / gcd_fll;
		
	}
M
Mark Brown 已提交
2125 2126 2127 2128

	return 0;
}

2129
static int _wm8994_set_fll(struct snd_soc_codec *codec, int id, int src,
M
Mark Brown 已提交
2130 2131
			  unsigned int freq_in, unsigned int freq_out)
{
2132
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2133
	struct wm8994 *control = wm8994->wm8994;
M
Mark Brown 已提交
2134 2135
	int reg_offset, ret;
	struct fll_div fll;
2136
	u16 reg, clk1, aif_reg, aif_src;
M
Mark Brown 已提交
2137
	unsigned long timeout;
2138
	bool was_enabled;
M
Mark Brown 已提交
2139 2140 2141 2142 2143

	switch (id) {
	case WM8994_FLL1:
		reg_offset = 0;
		id = 0;
2144
		aif_src = 0x10;
M
Mark Brown 已提交
2145 2146 2147 2148
		break;
	case WM8994_FLL2:
		reg_offset = 0x20;
		id = 1;
2149
		aif_src = 0x18;
M
Mark Brown 已提交
2150 2151 2152 2153 2154
		break;
	default:
		return -EINVAL;
	}

2155 2156 2157
	reg = snd_soc_read(codec, WM8994_FLL1_CONTROL_1 + reg_offset);
	was_enabled = reg & WM8994_FLL1_ENA;

2158
	switch (src) {
2159 2160 2161 2162
	case 0:
		/* Allow no source specification when stopping */
		if (freq_out)
			return -EINVAL;
2163
		src = wm8994->fll[id].src;
2164
		break;
2165 2166 2167 2168 2169
	case WM8994_FLL_SRC_MCLK1:
	case WM8994_FLL_SRC_MCLK2:
	case WM8994_FLL_SRC_LRCLK:
	case WM8994_FLL_SRC_BCLK:
		break;
2170 2171 2172 2173
	case WM8994_FLL_SRC_INTERNAL:
		freq_in = 12000000;
		freq_out = 12000000;
		break;
2174 2175 2176 2177
	default:
		return -EINVAL;
	}

M
Mark Brown 已提交
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
	/* Are we changing anything? */
	if (wm8994->fll[id].src == src &&
	    wm8994->fll[id].in == freq_in && wm8994->fll[id].out == freq_out)
		return 0;

	/* If we're stopping the FLL redo the old config - no
	 * registers will actually be written but we avoid GCC flow
	 * analysis bugs spewing warnings.
	 */
	if (freq_out)
2188
		ret = wm8994_get_fll_config(control, &fll, freq_in, freq_out);
M
Mark Brown 已提交
2189
	else
2190
		ret = wm8994_get_fll_config(control, &fll, wm8994->fll[id].in,
M
Mark Brown 已提交
2191 2192 2193 2194
					    wm8994->fll[id].out);
	if (ret < 0)
		return ret;

2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
	/* Make sure that we're not providing SYSCLK right now */
	clk1 = snd_soc_read(codec, WM8994_CLOCKING_1);
	if (clk1 & WM8994_SYSCLK_SRC)
		aif_reg = WM8994_AIF2_CLOCKING_1;
	else
		aif_reg = WM8994_AIF1_CLOCKING_1;
	reg = snd_soc_read(codec, aif_reg);

	if ((reg & WM8994_AIF1CLK_ENA) &&
	    (reg & WM8994_AIF1CLK_SRC_MASK) == aif_src) {
		dev_err(codec->dev, "FLL%d is currently providing SYSCLK\n",
			id + 1);
		return -EBUSY;
	}
M
Mark Brown 已提交
2209 2210 2211 2212 2213

	/* We always need to disable the FLL while reconfiguring */
	snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
			    WM8994_FLL1_ENA, 0);

2214
	if (wm8994->fll_byp && src == WM8994_FLL_SRC_BCLK &&
2215
	    freq_in == freq_out && freq_out) {
2216 2217 2218 2219 2220 2221
		dev_dbg(codec->dev, "Bypassing FLL%d\n", id + 1);
		snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_5 + reg_offset,
				    WM8958_FLL1_BYP, WM8958_FLL1_BYP);
		goto out;
	}

M
Mark Brown 已提交
2222 2223 2224 2225 2226 2227
	reg = (fll.outdiv << WM8994_FLL1_OUTDIV_SHIFT) |
		(fll.fll_fratio << WM8994_FLL1_FRATIO_SHIFT);
	snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_2 + reg_offset,
			    WM8994_FLL1_OUTDIV_MASK |
			    WM8994_FLL1_FRATIO_MASK, reg);

2228 2229
	snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_3 + reg_offset,
			    WM8994_FLL1_K_MASK, fll.k);
M
Mark Brown 已提交
2230 2231 2232

	snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_4 + reg_offset,
			    WM8994_FLL1_N_MASK,
2233
			    fll.n << WM8994_FLL1_N_SHIFT);
M
Mark Brown 已提交
2234

2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245
	if (fll.lambda) {
		snd_soc_update_bits(codec, WM8958_FLL1_EFS_1 + reg_offset,
				    WM8958_FLL1_LAMBDA_MASK,
				    fll.lambda);
		snd_soc_update_bits(codec, WM8958_FLL1_EFS_2 + reg_offset,
				    WM8958_FLL1_EFS_ENA, WM8958_FLL1_EFS_ENA);
	} else {
		snd_soc_update_bits(codec, WM8958_FLL1_EFS_2 + reg_offset,
				    WM8958_FLL1_EFS_ENA, 0);
	}

M
Mark Brown 已提交
2246
	snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_5 + reg_offset,
2247
			    WM8994_FLL1_FRC_NCO | WM8958_FLL1_BYP |
2248 2249
			    WM8994_FLL1_REFCLK_DIV_MASK |
			    WM8994_FLL1_REFCLK_SRC_MASK,
2250 2251
			    ((src == WM8994_FLL_SRC_INTERNAL)
			     << WM8994_FLL1_FRC_NCO_SHIFT) |
2252 2253
			    (fll.clk_ref_div << WM8994_FLL1_REFCLK_DIV_SHIFT) |
			    (src - 1));
M
Mark Brown 已提交
2254

2255 2256 2257
	/* Clear any pending completion from a previous failure */
	try_wait_for_completion(&wm8994->fll_locked[id]);

M
Mark Brown 已提交
2258 2259
	/* Enable (with fractional mode if required) */
	if (freq_out) {
2260 2261
		/* Enable VMID if we need it */
		if (!was_enabled) {
2262 2263
			active_reference(codec);

2264 2265 2266 2267 2268
			switch (control->type) {
			case WM8994:
				vmid_reference(codec);
				break;
			case WM8958:
2269
				if (control->revision < 1)
2270 2271 2272 2273 2274 2275 2276
					vmid_reference(codec);
				break;
			default:
				break;
			}
		}

2277 2278
		reg = WM8994_FLL1_ENA;

M
Mark Brown 已提交
2279
		if (fll.k)
2280 2281 2282 2283
			reg |= WM8994_FLL1_FRAC;
		if (src == WM8994_FLL_SRC_INTERNAL)
			reg |= WM8994_FLL1_OSC_ENA;

M
Mark Brown 已提交
2284
		snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
2285 2286
				    WM8994_FLL1_ENA | WM8994_FLL1_OSC_ENA |
				    WM8994_FLL1_FRAC, reg);
2287

M
Mark Brown 已提交
2288 2289 2290 2291 2292 2293 2294 2295 2296
		if (wm8994->fll_locked_irq) {
			timeout = wait_for_completion_timeout(&wm8994->fll_locked[id],
							      msecs_to_jiffies(10));
			if (timeout == 0)
				dev_warn(codec->dev,
					 "Timed out waiting for FLL lock\n");
		} else {
			msleep(5);
		}
2297 2298 2299 2300 2301 2302 2303
	} else {
		if (was_enabled) {
			switch (control->type) {
			case WM8994:
				vmid_dereference(codec);
				break;
			case WM8958:
2304
				if (control->revision < 1)
2305 2306 2307 2308 2309
					vmid_dereference(codec);
				break;
			default:
				break;
			}
2310 2311

			active_dereference(codec);
2312
		}
M
Mark Brown 已提交
2313 2314
	}

2315
out:
M
Mark Brown 已提交
2316 2317
	wm8994->fll[id].in = freq_in;
	wm8994->fll[id].out = freq_out;
2318
	wm8994->fll[id].src = src;
M
Mark Brown 已提交
2319 2320 2321

	configure_clock(codec);

2322 2323 2324 2325 2326 2327
	/*
	 * If SYSCLK will be less than 50kHz adjust AIFnCLK dividers
	 * for detection.
	 */
	if (max(wm8994->aifclk[0], wm8994->aifclk[1]) < 50000) {
		dev_dbg(codec->dev, "Configuring AIFs for 128fs\n");
2328 2329 2330 2331 2332 2333

		wm8994->aifdiv[0] = snd_soc_read(codec, WM8994_AIF1_RATE)
			& WM8994_AIF1CLK_RATE_MASK;
		wm8994->aifdiv[1] = snd_soc_read(codec, WM8994_AIF2_RATE)
			& WM8994_AIF1CLK_RATE_MASK;

2334 2335 2336 2337
		snd_soc_update_bits(codec, WM8994_AIF1_RATE,
				    WM8994_AIF1CLK_RATE_MASK, 0x1);
		snd_soc_update_bits(codec, WM8994_AIF2_RATE,
				    WM8994_AIF2CLK_RATE_MASK, 0x1);
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347
	} else if (wm8994->aifdiv[0]) {
		snd_soc_update_bits(codec, WM8994_AIF1_RATE,
				    WM8994_AIF1CLK_RATE_MASK,
				    wm8994->aifdiv[0]);
		snd_soc_update_bits(codec, WM8994_AIF2_RATE,
				    WM8994_AIF2CLK_RATE_MASK,
				    wm8994->aifdiv[1]);

		wm8994->aifdiv[0] = 0;
		wm8994->aifdiv[1] = 0;
2348 2349
	}

M
Mark Brown 已提交
2350 2351 2352
	return 0;
}

M
Mark Brown 已提交
2353 2354 2355 2356 2357 2358 2359 2360
static irqreturn_t wm8994_fll_locked_irq(int irq, void *data)
{
	struct completion *completion = data;

	complete(completion);

	return IRQ_HANDLED;
}
2361

2362 2363
static int opclk_divs[] = { 10, 20, 30, 40, 55, 60, 80, 120, 160 };

2364 2365 2366 2367 2368 2369
static int wm8994_set_fll(struct snd_soc_dai *dai, int id, int src,
			  unsigned int freq_in, unsigned int freq_out)
{
	return _wm8994_set_fll(dai->codec, id, src, freq_in, freq_out);
}

M
Mark Brown 已提交
2370 2371 2372 2373
static int wm8994_set_dai_sysclk(struct snd_soc_dai *dai,
		int clk_id, unsigned int freq, int dir)
{
	struct snd_soc_codec *codec = dai->codec;
2374
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2375
	int i;
M
Mark Brown 已提交
2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412

	switch (dai->id) {
	case 1:
	case 2:
		break;

	default:
		/* AIF3 shares clocking with AIF1/2 */
		return -EINVAL;
	}

	switch (clk_id) {
	case WM8994_SYSCLK_MCLK1:
		wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK1;
		wm8994->mclk[0] = freq;
		dev_dbg(dai->dev, "AIF%d using MCLK1 at %uHz\n",
			dai->id, freq);
		break;

	case WM8994_SYSCLK_MCLK2:
		/* TODO: Set GPIO AF */
		wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK2;
		wm8994->mclk[1] = freq;
		dev_dbg(dai->dev, "AIF%d using MCLK2 at %uHz\n",
			dai->id, freq);
		break;

	case WM8994_SYSCLK_FLL1:
		wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL1;
		dev_dbg(dai->dev, "AIF%d using FLL1\n", dai->id);
		break;

	case WM8994_SYSCLK_FLL2:
		wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL2;
		dev_dbg(dai->dev, "AIF%d using FLL2\n", dai->id);
		break;

2413 2414
	case WM8994_SYSCLK_OPCLK:
		/* Special case - a division (times 10) is given and
2415
		 * no effect on main clocking.
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431
		 */
		if (freq) {
			for (i = 0; i < ARRAY_SIZE(opclk_divs); i++)
				if (opclk_divs[i] == freq)
					break;
			if (i == ARRAY_SIZE(opclk_divs))
				return -EINVAL;
			snd_soc_update_bits(codec, WM8994_CLOCKING_2,
					    WM8994_OPCLK_DIV_MASK, i);
			snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
					    WM8994_OPCLK_ENA, WM8994_OPCLK_ENA);
		} else {
			snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
					    WM8994_OPCLK_ENA, 0);
		}

M
Mark Brown 已提交
2432 2433 2434 2435 2436 2437
	default:
		return -EINVAL;
	}

	configure_clock(codec);

2438 2439 2440 2441 2442 2443
	/*
	 * If SYSCLK will be less than 50kHz adjust AIFnCLK dividers
	 * for detection.
	 */
	if (max(wm8994->aifclk[0], wm8994->aifclk[1]) < 50000) {
		dev_dbg(codec->dev, "Configuring AIFs for 128fs\n");
2444 2445 2446 2447 2448 2449

		wm8994->aifdiv[0] = snd_soc_read(codec, WM8994_AIF1_RATE)
			& WM8994_AIF1CLK_RATE_MASK;
		wm8994->aifdiv[1] = snd_soc_read(codec, WM8994_AIF2_RATE)
			& WM8994_AIF1CLK_RATE_MASK;

2450 2451 2452 2453
		snd_soc_update_bits(codec, WM8994_AIF1_RATE,
				    WM8994_AIF1CLK_RATE_MASK, 0x1);
		snd_soc_update_bits(codec, WM8994_AIF2_RATE,
				    WM8994_AIF2CLK_RATE_MASK, 0x1);
2454 2455 2456 2457 2458 2459 2460 2461 2462 2463
	} else if (wm8994->aifdiv[0]) {
		snd_soc_update_bits(codec, WM8994_AIF1_RATE,
				    WM8994_AIF1CLK_RATE_MASK,
				    wm8994->aifdiv[0]);
		snd_soc_update_bits(codec, WM8994_AIF2_RATE,
				    WM8994_AIF2CLK_RATE_MASK,
				    wm8994->aifdiv[1]);

		wm8994->aifdiv[0] = 0;
		wm8994->aifdiv[1] = 0;
2464 2465
	}

M
Mark Brown 已提交
2466 2467 2468 2469 2470 2471
	return 0;
}

static int wm8994_set_bias_level(struct snd_soc_codec *codec,
				 enum snd_soc_bias_level level)
{
2472
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2473
	struct wm8994 *control = wm8994->wm8994;
2474

2475 2476
	wm_hubs_set_bias_level(codec, level);

M
Mark Brown 已提交
2477 2478 2479 2480 2481
	switch (level) {
	case SND_SOC_BIAS_ON:
		break;

	case SND_SOC_BIAS_PREPARE:
2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493
		/* MICBIAS into regulating mode */
		switch (control->type) {
		case WM8958:
		case WM1811:
			snd_soc_update_bits(codec, WM8958_MICBIAS1,
					    WM8958_MICB1_MODE, 0);
			snd_soc_update_bits(codec, WM8958_MICBIAS2,
					    WM8958_MICB2_MODE, 0);
			break;
		default:
			break;
		}
2494 2495 2496

		if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
			active_reference(codec);
M
Mark Brown 已提交
2497 2498 2499
		break;

	case SND_SOC_BIAS_STANDBY:
L
Liam Girdwood 已提交
2500
		if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
2501 2502
			switch (control->type) {
			case WM8958:
2503
				if (control->revision == 0) {
2504 2505 2506 2507 2508 2509 2510
					/* Optimise performance for rev A */
					snd_soc_update_bits(codec,
							    WM8958_CHARGE_PUMP_2,
							    WM8958_CP_DISCH,
							    WM8958_CP_DISCH);
				}
				break;
M
Mark Brown 已提交
2511

2512
			default:
M
Mark Brown 已提交
2513
				break;
2514
			}
M
Mark Brown 已提交
2515 2516 2517 2518 2519 2520 2521 2522 2523

			/* Discharge LINEOUT1 & 2 */
			snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
					    WM8994_LINEOUT1_DISCH |
					    WM8994_LINEOUT2_DISCH,
					    WM8994_LINEOUT1_DISCH |
					    WM8994_LINEOUT2_DISCH);
		}

2524 2525 2526
		if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE)
			active_dereference(codec);

2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540
		/* MICBIAS into bypass mode on newer devices */
		switch (control->type) {
		case WM8958:
		case WM1811:
			snd_soc_update_bits(codec, WM8958_MICBIAS1,
					    WM8958_MICB1_MODE,
					    WM8958_MICB1_MODE);
			snd_soc_update_bits(codec, WM8958_MICBIAS2,
					    WM8958_MICB2_MODE,
					    WM8958_MICB2_MODE);
			break;
		default:
			break;
		}
M
Mark Brown 已提交
2541 2542 2543
		break;

	case SND_SOC_BIAS_OFF:
2544
		if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
2545
			wm8994->cur_fw = NULL;
M
Mark Brown 已提交
2546 2547
		break;
	}
2548

L
Liam Girdwood 已提交
2549
	codec->dapm.bias_level = level;
2550

2551 2552 2553 2554 2555 2556
	return 0;
}

int wm8994_vmid_mode(struct snd_soc_codec *codec, enum wm8994_vmid_mode mode)
{
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2557
	struct snd_soc_dapm_context *dapm = &codec->dapm;
2558 2559 2560

	switch (mode) {
	case WM8994_VMID_NORMAL:
2561 2562
		snd_soc_dapm_mutex_lock(dapm);

2563
		if (wm8994->hubs.lineout1_se) {
2564 2565 2566 2567
			snd_soc_dapm_disable_pin_unlocked(dapm,
							  "LINEOUT1N Driver");
			snd_soc_dapm_disable_pin_unlocked(dapm,
							  "LINEOUT1P Driver");
2568 2569
		}
		if (wm8994->hubs.lineout2_se) {
2570 2571 2572 2573
			snd_soc_dapm_disable_pin_unlocked(dapm,
							  "LINEOUT2N Driver");
			snd_soc_dapm_disable_pin_unlocked(dapm,
							  "LINEOUT2P Driver");
2574 2575 2576
		}

		/* Do the sync with the old mode to allow it to clean up */
2577
		snd_soc_dapm_sync_unlocked(dapm);
2578
		wm8994->vmid_mode = mode;
2579 2580

		snd_soc_dapm_mutex_unlock(dapm);
2581 2582 2583
		break;

	case WM8994_VMID_FORCE:
2584 2585
		snd_soc_dapm_mutex_lock(dapm);

2586
		if (wm8994->hubs.lineout1_se) {
2587 2588 2589 2590
			snd_soc_dapm_force_enable_pin_unlocked(dapm,
							       "LINEOUT1N Driver");
			snd_soc_dapm_force_enable_pin_unlocked(dapm,
							       "LINEOUT1P Driver");
2591 2592
		}
		if (wm8994->hubs.lineout2_se) {
2593 2594 2595 2596
			snd_soc_dapm_force_enable_pin_unlocked(dapm,
							       "LINEOUT2N Driver");
			snd_soc_dapm_force_enable_pin_unlocked(dapm,
							       "LINEOUT2P Driver");
2597 2598 2599
		}

		wm8994->vmid_mode = mode;
2600 2601 2602
		snd_soc_dapm_sync_unlocked(dapm);

		snd_soc_dapm_mutex_unlock(dapm);
2603 2604 2605 2606 2607 2608
		break;

	default:
		return -EINVAL;
	}

M
Mark Brown 已提交
2609 2610 2611 2612 2613 2614
	return 0;
}

static int wm8994_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
{
	struct snd_soc_codec *codec = dai->codec;
2615 2616
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
	struct wm8994 *control = wm8994->wm8994;
M
Mark Brown 已提交
2617 2618
	int ms_reg;
	int aif1_reg;
2619 2620
	int dac_reg;
	int adc_reg;
M
Mark Brown 已提交
2621 2622
	int ms = 0;
	int aif1 = 0;
2623
	int lrclk = 0;
M
Mark Brown 已提交
2624 2625 2626 2627 2628

	switch (dai->id) {
	case 1:
		ms_reg = WM8994_AIF1_MASTER_SLAVE;
		aif1_reg = WM8994_AIF1_CONTROL_1;
2629 2630
		dac_reg = WM8994_AIF1DAC_LRCLK;
		adc_reg = WM8994_AIF1ADC_LRCLK;
M
Mark Brown 已提交
2631 2632 2633 2634
		break;
	case 2:
		ms_reg = WM8994_AIF2_MASTER_SLAVE;
		aif1_reg = WM8994_AIF2_CONTROL_1;
2635 2636
		dac_reg = WM8994_AIF1DAC_LRCLK;
		adc_reg = WM8994_AIF1ADC_LRCLK;
M
Mark Brown 已提交
2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654
		break;
	default:
		return -EINVAL;
	}

	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBS_CFS:
		break;
	case SND_SOC_DAIFMT_CBM_CFM:
		ms = WM8994_AIF1_MSTR;
		break;
	default:
		return -EINVAL;
	}

	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_DSP_B:
		aif1 |= WM8994_AIF1_LRCLK_INV;
2655
		lrclk |= WM8958_AIF1_LRCLK_INV;
M
Mark Brown 已提交
2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693
	case SND_SOC_DAIFMT_DSP_A:
		aif1 |= 0x18;
		break;
	case SND_SOC_DAIFMT_I2S:
		aif1 |= 0x10;
		break;
	case SND_SOC_DAIFMT_RIGHT_J:
		break;
	case SND_SOC_DAIFMT_LEFT_J:
		aif1 |= 0x8;
		break;
	default:
		return -EINVAL;
	}

	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_DSP_A:
	case SND_SOC_DAIFMT_DSP_B:
		/* frame inversion not valid for DSP modes */
		switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
		case SND_SOC_DAIFMT_NB_NF:
			break;
		case SND_SOC_DAIFMT_IB_NF:
			aif1 |= WM8994_AIF1_BCLK_INV;
			break;
		default:
			return -EINVAL;
		}
		break;

	case SND_SOC_DAIFMT_I2S:
	case SND_SOC_DAIFMT_RIGHT_J:
	case SND_SOC_DAIFMT_LEFT_J:
		switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
		case SND_SOC_DAIFMT_NB_NF:
			break;
		case SND_SOC_DAIFMT_IB_IF:
			aif1 |= WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV;
2694
			lrclk |= WM8958_AIF1_LRCLK_INV;
M
Mark Brown 已提交
2695 2696 2697 2698 2699 2700
			break;
		case SND_SOC_DAIFMT_IB_NF:
			aif1 |= WM8994_AIF1_BCLK_INV;
			break;
		case SND_SOC_DAIFMT_NB_IF:
			aif1 |= WM8994_AIF1_LRCLK_INV;
2701
			lrclk |= WM8958_AIF1_LRCLK_INV;
M
Mark Brown 已提交
2702 2703 2704 2705 2706 2707 2708 2709 2710
			break;
		default:
			return -EINVAL;
		}
		break;
	default:
		return -EINVAL;
	}

2711 2712
	/* The AIF2 format configuration needs to be mirrored to AIF3
	 * on WM8958 if it's in use so just do it all the time. */
M
Mark Brown 已提交
2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724
	switch (control->type) {
	case WM1811:
	case WM8958:
		if (dai->id == 2)
			snd_soc_update_bits(codec, WM8958_AIF3_CONTROL_1,
					    WM8994_AIF1_LRCLK_INV |
					    WM8958_AIF3_FMT_MASK, aif1);
		break;

	default:
		break;
	}
2725

M
Mark Brown 已提交
2726 2727 2728 2729 2730 2731
	snd_soc_update_bits(codec, aif1_reg,
			    WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV |
			    WM8994_AIF1_FMT_MASK,
			    aif1);
	snd_soc_update_bits(codec, ms_reg, WM8994_AIF1_MSTR,
			    ms);
2732 2733 2734 2735
	snd_soc_update_bits(codec, dac_reg,
			    WM8958_AIF1_LRCLK_INV, lrclk);
	snd_soc_update_bits(codec, adc_reg,
			    WM8958_AIF1_LRCLK_INV, lrclk);
M
Mark Brown 已提交
2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769

	return 0;
}

static struct {
	int val, rate;
} srs[] = {
	{ 0,   8000 },
	{ 1,  11025 },
	{ 2,  12000 },
	{ 3,  16000 },
	{ 4,  22050 },
	{ 5,  24000 },
	{ 6,  32000 },
	{ 7,  44100 },
	{ 8,  48000 },
	{ 9,  88200 },
	{ 10, 96000 },
};

static int fs_ratios[] = {
	64, 128, 192, 256, 348, 512, 768, 1024, 1408, 1536
};

static int bclk_divs[] = {
	10, 15, 20, 30, 40, 50, 60, 80, 110, 120, 160, 220, 240, 320, 440, 480,
	640, 880, 960, 1280, 1760, 1920
};

static int wm8994_hw_params(struct snd_pcm_substream *substream,
			    struct snd_pcm_hw_params *params,
			    struct snd_soc_dai *dai)
{
	struct snd_soc_codec *codec = dai->codec;
2770
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2771 2772
	struct wm8994 *control = wm8994->wm8994;
	struct wm8994_pdata *pdata = &control->pdata;
M
Mark Brown 已提交
2773
	int aif1_reg;
2774
	int aif2_reg;
M
Mark Brown 已提交
2775 2776 2777 2778
	int bclk_reg;
	int lrclk_reg;
	int rate_reg;
	int aif1 = 0;
2779
	int aif2 = 0;
M
Mark Brown 已提交
2780 2781 2782 2783 2784 2785 2786 2787 2788 2789
	int bclk = 0;
	int lrclk = 0;
	int rate_val = 0;
	int id = dai->id - 1;

	int i, cur_val, best_val, bclk_rate, best;

	switch (dai->id) {
	case 1:
		aif1_reg = WM8994_AIF1_CONTROL_1;
2790
		aif2_reg = WM8994_AIF1_CONTROL_2;
M
Mark Brown 已提交
2791 2792 2793
		bclk_reg = WM8994_AIF1_BCLK;
		rate_reg = WM8994_AIF1_RATE;
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
2794
		    wm8994->lrclk_shared[0]) {
M
Mark Brown 已提交
2795
			lrclk_reg = WM8994_AIF1DAC_LRCLK;
2796
		} else {
M
Mark Brown 已提交
2797
			lrclk_reg = WM8994_AIF1ADC_LRCLK;
2798 2799
			dev_dbg(codec->dev, "AIF1 using split LRCLK\n");
		}
M
Mark Brown 已提交
2800 2801 2802
		break;
	case 2:
		aif1_reg = WM8994_AIF2_CONTROL_1;
2803
		aif2_reg = WM8994_AIF2_CONTROL_2;
M
Mark Brown 已提交
2804 2805 2806
		bclk_reg = WM8994_AIF2_BCLK;
		rate_reg = WM8994_AIF2_RATE;
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
2807
		    wm8994->lrclk_shared[1]) {
M
Mark Brown 已提交
2808
			lrclk_reg = WM8994_AIF2DAC_LRCLK;
2809
		} else {
M
Mark Brown 已提交
2810
			lrclk_reg = WM8994_AIF2ADC_LRCLK;
2811 2812
			dev_dbg(codec->dev, "AIF2 using split LRCLK\n");
		}
M
Mark Brown 已提交
2813 2814 2815 2816 2817
		break;
	default:
		return -EINVAL;
	}

2818
	bclk_rate = params_rate(params);
M
Mark Brown 已提交
2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838
	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16_LE:
		bclk_rate *= 16;
		break;
	case SNDRV_PCM_FORMAT_S20_3LE:
		bclk_rate *= 20;
		aif1 |= 0x20;
		break;
	case SNDRV_PCM_FORMAT_S24_LE:
		bclk_rate *= 24;
		aif1 |= 0x40;
		break;
	case SNDRV_PCM_FORMAT_S32_LE:
		bclk_rate *= 32;
		aif1 |= 0x60;
		break;
	default:
		return -EINVAL;
	}

2839
	wm8994->channels[id] = params_channels(params);
2840 2841 2842 2843 2844 2845 2846 2847
	if (pdata->max_channels_clocked[id] &&
	    wm8994->channels[id] > pdata->max_channels_clocked[id]) {
		dev_dbg(dai->dev, "Constraining channels to %d from %d\n",
			pdata->max_channels_clocked[id], wm8994->channels[id]);
		wm8994->channels[id] = pdata->max_channels_clocked[id];
	}

	switch (wm8994->channels[id]) {
2848 2849 2850 2851 2852 2853 2854 2855 2856
	case 1:
	case 2:
		bclk_rate *= 2;
		break;
	default:
		bclk_rate *= 4;
		break;
	}

M
Mark Brown 已提交
2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868
	/* Try to find an appropriate sample rate; look for an exact match. */
	for (i = 0; i < ARRAY_SIZE(srs); i++)
		if (srs[i].rate == params_rate(params))
			break;
	if (i == ARRAY_SIZE(srs))
		return -EINVAL;
	rate_val |= srs[i].val << WM8994_AIF1_SR_SHIFT;

	dev_dbg(dai->dev, "Sample rate is %dHz\n", srs[i].rate);
	dev_dbg(dai->dev, "AIF%dCLK is %dHz, target BCLK %dHz\n",
		dai->id, wm8994->aifclk[id], bclk_rate);

2869
	if (wm8994->channels[id] == 1 &&
2870 2871 2872
	    (snd_soc_read(codec, aif1_reg) & 0x18) == 0x18)
		aif2 |= WM8994_AIF1_MONO;

M
Mark Brown 已提交
2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900
	if (wm8994->aifclk[id] == 0) {
		dev_err(dai->dev, "AIF%dCLK not configured\n", dai->id);
		return -EINVAL;
	}

	/* AIFCLK/fs ratio; look for a close match in either direction */
	best = 0;
	best_val = abs((fs_ratios[0] * params_rate(params))
		       - wm8994->aifclk[id]);
	for (i = 1; i < ARRAY_SIZE(fs_ratios); i++) {
		cur_val = abs((fs_ratios[i] * params_rate(params))
			      - wm8994->aifclk[id]);
		if (cur_val >= best_val)
			continue;
		best = i;
		best_val = cur_val;
	}
	dev_dbg(dai->dev, "Selected AIF%dCLK/fs = %d\n",
		dai->id, fs_ratios[best]);
	rate_val |= best;

	/* We may not get quite the right frequency if using
	 * approximate clocks so look for the closest match that is
	 * higher than the target (we need to ensure that there enough
	 * BCLKs to clock out the samples).
	 */
	best = 0;
	for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
2901
		cur_val = (wm8994->aifclk[id] * 10 / bclk_divs[i]) - bclk_rate;
M
Mark Brown 已提交
2902 2903 2904 2905
		if (cur_val < 0) /* BCLK table is sorted */
			break;
		best = i;
	}
2906
	bclk_rate = wm8994->aifclk[id] * 10 / bclk_divs[best];
M
Mark Brown 已提交
2907 2908 2909 2910 2911
	dev_dbg(dai->dev, "Using BCLK_DIV %d for actual BCLK %dHz\n",
		bclk_divs[best], bclk_rate);
	bclk |= best << WM8994_AIF1_BCLK_DIV_SHIFT;

	lrclk = bclk_rate / params_rate(params);
2912 2913 2914 2915 2916
	if (!lrclk) {
		dev_err(dai->dev, "Unable to generate LRCLK from %dHz BCLK\n",
			bclk_rate);
		return -EINVAL;
	}
M
Mark Brown 已提交
2917 2918 2919 2920
	dev_dbg(dai->dev, "Using LRCLK rate %d for actual LRCLK %dHz\n",
		lrclk, bclk_rate / lrclk);

	snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
2921
	snd_soc_update_bits(codec, aif2_reg, WM8994_AIF1_MONO, aif2);
M
Mark Brown 已提交
2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944
	snd_soc_update_bits(codec, bclk_reg, WM8994_AIF1_BCLK_DIV_MASK, bclk);
	snd_soc_update_bits(codec, lrclk_reg, WM8994_AIF1DAC_RATE_MASK,
			    lrclk);
	snd_soc_update_bits(codec, rate_reg, WM8994_AIF1_SR_MASK |
			    WM8994_AIF1CLK_RATE_MASK, rate_val);

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		switch (dai->id) {
		case 1:
			wm8994->dac_rates[0] = params_rate(params);
			wm8994_set_retune_mobile(codec, 0);
			wm8994_set_retune_mobile(codec, 1);
			break;
		case 2:
			wm8994->dac_rates[1] = params_rate(params);
			wm8994_set_retune_mobile(codec, 2);
			break;
		}
	}

	return 0;
}

2945 2946 2947 2948 2949
static int wm8994_aif3_hw_params(struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *params,
				 struct snd_soc_dai *dai)
{
	struct snd_soc_codec *codec = dai->codec;
2950 2951
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
	struct wm8994 *control = wm8994->wm8994;
2952 2953 2954 2955 2956 2957
	int aif1_reg;
	int aif1 = 0;

	switch (dai->id) {
	case 3:
		switch (control->type) {
M
Mark Brown 已提交
2958
		case WM1811:
2959 2960 2961 2962 2963 2964
		case WM8958:
			aif1_reg = WM8958_AIF3_CONTROL_1;
			break;
		default:
			return 0;
		}
2965
		break;
2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988
	default:
		return 0;
	}

	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16_LE:
		break;
	case SNDRV_PCM_FORMAT_S20_3LE:
		aif1 |= 0x20;
		break;
	case SNDRV_PCM_FORMAT_S24_LE:
		aif1 |= 0x40;
		break;
	case SNDRV_PCM_FORMAT_S32_LE:
		aif1 |= 0x60;
		break;
	default:
		return -EINVAL;
	}

	return snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
}

M
Mark Brown 已提交
2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015
static int wm8994_aif_mute(struct snd_soc_dai *codec_dai, int mute)
{
	struct snd_soc_codec *codec = codec_dai->codec;
	int mute_reg;
	int reg;

	switch (codec_dai->id) {
	case 1:
		mute_reg = WM8994_AIF1_DAC1_FILTERS_1;
		break;
	case 2:
		mute_reg = WM8994_AIF2_DAC_FILTERS_1;
		break;
	default:
		return -EINVAL;
	}

	if (mute)
		reg = WM8994_AIF1DAC1_MUTE;
	else
		reg = 0;

	snd_soc_update_bits(codec, mute_reg, WM8994_AIF1DAC1_MUTE, reg);

	return 0;
}

3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038
static int wm8994_set_tristate(struct snd_soc_dai *codec_dai, int tristate)
{
	struct snd_soc_codec *codec = codec_dai->codec;
	int reg, val, mask;

	switch (codec_dai->id) {
	case 1:
		reg = WM8994_AIF1_MASTER_SLAVE;
		mask = WM8994_AIF1_TRI;
		break;
	case 2:
		reg = WM8994_AIF2_MASTER_SLAVE;
		mask = WM8994_AIF2_TRI;
		break;
	default:
		return -EINVAL;
	}

	if (tristate)
		val = mask;
	else
		val = 0;

3039
	return snd_soc_update_bits(codec, reg, mask, val);
3040 3041
}

3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056
static int wm8994_aif2_probe(struct snd_soc_dai *dai)
{
	struct snd_soc_codec *codec = dai->codec;

	/* Disable the pulls on the AIF if we're using it to save power. */
	snd_soc_update_bits(codec, WM8994_GPIO_3,
			    WM8994_GPN_PU | WM8994_GPN_PD, 0);
	snd_soc_update_bits(codec, WM8994_GPIO_4,
			    WM8994_GPN_PU | WM8994_GPN_PD, 0);
	snd_soc_update_bits(codec, WM8994_GPIO_5,
			    WM8994_GPN_PU | WM8994_GPN_PD, 0);

	return 0;
}

M
Mark Brown 已提交
3057 3058 3059
#define WM8994_RATES SNDRV_PCM_RATE_8000_96000

#define WM8994_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
3060
			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
M
Mark Brown 已提交
3061

3062
static const struct snd_soc_dai_ops wm8994_aif1_dai_ops = {
M
Mark Brown 已提交
3063 3064 3065 3066 3067
	.set_sysclk	= wm8994_set_dai_sysclk,
	.set_fmt	= wm8994_set_dai_fmt,
	.hw_params	= wm8994_hw_params,
	.digital_mute	= wm8994_aif_mute,
	.set_pll	= wm8994_set_fll,
3068
	.set_tristate	= wm8994_set_tristate,
M
Mark Brown 已提交
3069 3070
};

3071
static const struct snd_soc_dai_ops wm8994_aif2_dai_ops = {
M
Mark Brown 已提交
3072 3073 3074 3075 3076
	.set_sysclk	= wm8994_set_dai_sysclk,
	.set_fmt	= wm8994_set_dai_fmt,
	.hw_params	= wm8994_hw_params,
	.digital_mute   = wm8994_aif_mute,
	.set_pll	= wm8994_set_fll,
3077 3078 3079
	.set_tristate	= wm8994_set_tristate,
};

3080
static const struct snd_soc_dai_ops wm8994_aif3_dai_ops = {
3081
	.hw_params	= wm8994_aif3_hw_params,
M
Mark Brown 已提交
3082 3083
};

3084
static struct snd_soc_dai_driver wm8994_dai[] = {
M
Mark Brown 已提交
3085
	{
3086
		.name = "wm8994-aif1",
3087
		.id = 1,
M
Mark Brown 已提交
3088 3089
		.playback = {
			.stream_name = "AIF1 Playback",
3090
			.channels_min = 1,
M
Mark Brown 已提交
3091 3092 3093
			.channels_max = 2,
			.rates = WM8994_RATES,
			.formats = WM8994_FORMATS,
3094
			.sig_bits = 24,
M
Mark Brown 已提交
3095 3096 3097
		},
		.capture = {
			.stream_name = "AIF1 Capture",
3098
			.channels_min = 1,
M
Mark Brown 已提交
3099 3100 3101
			.channels_max = 2,
			.rates = WM8994_RATES,
			.formats = WM8994_FORMATS,
3102
			.sig_bits = 24,
M
Mark Brown 已提交
3103 3104 3105 3106
		 },
		.ops = &wm8994_aif1_dai_ops,
	},
	{
3107
		.name = "wm8994-aif2",
3108
		.id = 2,
M
Mark Brown 已提交
3109 3110
		.playback = {
			.stream_name = "AIF2 Playback",
3111
			.channels_min = 1,
M
Mark Brown 已提交
3112 3113 3114
			.channels_max = 2,
			.rates = WM8994_RATES,
			.formats = WM8994_FORMATS,
3115
			.sig_bits = 24,
M
Mark Brown 已提交
3116 3117 3118
		},
		.capture = {
			.stream_name = "AIF2 Capture",
3119
			.channels_min = 1,
M
Mark Brown 已提交
3120 3121 3122
			.channels_max = 2,
			.rates = WM8994_RATES,
			.formats = WM8994_FORMATS,
3123
			.sig_bits = 24,
M
Mark Brown 已提交
3124
		},
3125
		.probe = wm8994_aif2_probe,
M
Mark Brown 已提交
3126 3127 3128
		.ops = &wm8994_aif2_dai_ops,
	},
	{
3129
		.name = "wm8994-aif3",
3130
		.id = 3,
M
Mark Brown 已提交
3131 3132
		.playback = {
			.stream_name = "AIF3 Playback",
3133
			.channels_min = 1,
M
Mark Brown 已提交
3134 3135 3136
			.channels_max = 2,
			.rates = WM8994_RATES,
			.formats = WM8994_FORMATS,
3137
			.sig_bits = 24,
M
Mark Brown 已提交
3138
		},
3139
		.capture = {
M
Mark Brown 已提交
3140
			.stream_name = "AIF3 Capture",
3141
			.channels_min = 1,
M
Mark Brown 已提交
3142 3143 3144
			.channels_max = 2,
			.rates = WM8994_RATES,
			.formats = WM8994_FORMATS,
3145 3146
			.sig_bits = 24,
		 },
3147
		.ops = &wm8994_aif3_dai_ops,
M
Mark Brown 已提交
3148 3149 3150 3151
	}
};

#ifdef CONFIG_PM
3152
static int wm8994_codec_suspend(struct snd_soc_codec *codec)
M
Mark Brown 已提交
3153
{
3154
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
M
Mark Brown 已提交
3155 3156 3157 3158
	int i, ret;

	for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
		memcpy(&wm8994->fll_suspend[i], &wm8994->fll[i],
3159
		       sizeof(struct wm8994_fll_config));
3160
		ret = _wm8994_set_fll(codec, i + 1, 0, 0, 0);
M
Mark Brown 已提交
3161 3162 3163 3164 3165 3166 3167 3168 3169 3170
		if (ret < 0)
			dev_warn(codec->dev, "Failed to stop FLL%d: %d\n",
				 i + 1, ret);
	}

	wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);

	return 0;
}

3171
static int wm8994_codec_resume(struct snd_soc_codec *codec)
M
Mark Brown 已提交
3172
{
3173
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
M
Mark Brown 已提交
3174 3175 3176
	int i, ret;

	for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
3177 3178 3179
		if (!wm8994->fll_suspend[i].out)
			continue;

3180
		ret = _wm8994_set_fll(codec, i + 1,
M
Mark Brown 已提交
3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191
				     wm8994->fll_suspend[i].src,
				     wm8994->fll_suspend[i].in,
				     wm8994->fll_suspend[i].out);
		if (ret < 0)
			dev_warn(codec->dev, "Failed to restore FLL%d: %d\n",
				 i + 1, ret);
	}

	return 0;
}
#else
3192 3193
#define wm8994_codec_suspend NULL
#define wm8994_codec_resume NULL
M
Mark Brown 已提交
3194 3195 3196 3197
#endif

static void wm8994_handle_retune_mobile_pdata(struct wm8994_priv *wm8994)
{
3198
	struct snd_soc_codec *codec = wm8994->hubs.codec;
3199 3200
	struct wm8994 *control = wm8994->wm8994;
	struct wm8994_pdata *pdata = &control->pdata;
M
Mark Brown 已提交
3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235
	struct snd_kcontrol_new controls[] = {
		SOC_ENUM_EXT("AIF1.1 EQ Mode",
			     wm8994->retune_mobile_enum,
			     wm8994_get_retune_mobile_enum,
			     wm8994_put_retune_mobile_enum),
		SOC_ENUM_EXT("AIF1.2 EQ Mode",
			     wm8994->retune_mobile_enum,
			     wm8994_get_retune_mobile_enum,
			     wm8994_put_retune_mobile_enum),
		SOC_ENUM_EXT("AIF2 EQ Mode",
			     wm8994->retune_mobile_enum,
			     wm8994_get_retune_mobile_enum,
			     wm8994_put_retune_mobile_enum),
	};
	int ret, i, j;
	const char **t;

	/* We need an array of texts for the enum API but the number
	 * of texts is likely to be less than the number of
	 * configurations due to the sample rate dependency of the
	 * configurations. */
	wm8994->num_retune_mobile_texts = 0;
	wm8994->retune_mobile_texts = NULL;
	for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
		for (j = 0; j < wm8994->num_retune_mobile_texts; j++) {
			if (strcmp(pdata->retune_mobile_cfgs[i].name,
				   wm8994->retune_mobile_texts[j]) == 0)
				break;
		}

		if (j != wm8994->num_retune_mobile_texts)
			continue;

		/* Expand the array... */
		t = krealloc(wm8994->retune_mobile_texts,
3236
			     sizeof(char *) *
M
Mark Brown 已提交
3237 3238 3239 3240 3241 3242
			     (wm8994->num_retune_mobile_texts + 1),
			     GFP_KERNEL);
		if (t == NULL)
			continue;

		/* ...store the new entry... */
3243
		t[wm8994->num_retune_mobile_texts] =
M
Mark Brown 已提交
3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256
			pdata->retune_mobile_cfgs[i].name;

		/* ...and remember the new version. */
		wm8994->num_retune_mobile_texts++;
		wm8994->retune_mobile_texts = t;
	}

	dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
		wm8994->num_retune_mobile_texts);

	wm8994->retune_mobile_enum.max = wm8994->num_retune_mobile_texts;
	wm8994->retune_mobile_enum.texts = wm8994->retune_mobile_texts;

3257
	ret = snd_soc_add_codec_controls(wm8994->hubs.codec, controls,
M
Mark Brown 已提交
3258 3259
				   ARRAY_SIZE(controls));
	if (ret != 0)
3260
		dev_err(wm8994->hubs.codec->dev,
M
Mark Brown 已提交
3261 3262 3263 3264 3265
			"Failed to add ReTune Mobile controls: %d\n", ret);
}

static void wm8994_handle_pdata(struct wm8994_priv *wm8994)
{
3266
	struct snd_soc_codec *codec = wm8994->hubs.codec;
3267 3268
	struct wm8994 *control = wm8994->wm8994;
	struct wm8994_pdata *pdata = &control->pdata;
M
Mark Brown 已提交
3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279
	int ret, i;

	if (!pdata)
		return;

	wm_hubs_handle_analogue_pdata(codec, pdata->lineout1_diff,
				      pdata->lineout2_diff,
				      pdata->lineout1fb,
				      pdata->lineout2fb,
				      pdata->jd_scthr,
				      pdata->jd_thr,
3280 3281
				      pdata->micb1_delay,
				      pdata->micb2_delay,
M
Mark Brown 已提交
3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297
				      pdata->micbias1_lvl,
				      pdata->micbias2_lvl);

	dev_dbg(codec->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);

	if (pdata->num_drc_cfgs) {
		struct snd_kcontrol_new controls[] = {
			SOC_ENUM_EXT("AIF1DRC1 Mode", wm8994->drc_enum,
				     wm8994_get_drc_enum, wm8994_put_drc_enum),
			SOC_ENUM_EXT("AIF1DRC2 Mode", wm8994->drc_enum,
				     wm8994_get_drc_enum, wm8994_put_drc_enum),
			SOC_ENUM_EXT("AIF2DRC Mode", wm8994->drc_enum,
				     wm8994_get_drc_enum, wm8994_put_drc_enum),
		};

		/* We need an array of texts for the enum API */
3298
		wm8994->drc_texts = devm_kzalloc(wm8994->hubs.codec->dev,
3299
			    sizeof(char *) * pdata->num_drc_cfgs, GFP_KERNEL);
M
Mark Brown 已提交
3300
		if (!wm8994->drc_texts) {
3301
			dev_err(wm8994->hubs.codec->dev,
M
Mark Brown 已提交
3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312
				"Failed to allocate %d DRC config texts\n",
				pdata->num_drc_cfgs);
			return;
		}

		for (i = 0; i < pdata->num_drc_cfgs; i++)
			wm8994->drc_texts[i] = pdata->drc_cfgs[i].name;

		wm8994->drc_enum.max = pdata->num_drc_cfgs;
		wm8994->drc_enum.texts = wm8994->drc_texts;

3313
		ret = snd_soc_add_codec_controls(wm8994->hubs.codec, controls,
M
Mark Brown 已提交
3314 3315 3316
					   ARRAY_SIZE(controls));
		for (i = 0; i < WM8994_NUM_DRC; i++)
			wm8994_set_drc(codec, i);
3317 3318 3319 3320
	} else {
		ret = snd_soc_add_codec_controls(wm8994->hubs.codec,
						 wm8994_drc_controls,
						 ARRAY_SIZE(wm8994_drc_controls));
M
Mark Brown 已提交
3321 3322
	}

3323 3324 3325 3326 3327
	if (ret != 0)
		dev_err(wm8994->hubs.codec->dev,
			"Failed to add DRC mode controls: %d\n", ret);


M
Mark Brown 已提交
3328 3329 3330 3331 3332 3333
	dev_dbg(codec->dev, "%d ReTune Mobile configurations\n",
		pdata->num_retune_mobile_cfgs);

	if (pdata->num_retune_mobile_cfgs)
		wm8994_handle_retune_mobile_pdata(wm8994);
	else
3334
		snd_soc_add_codec_controls(wm8994->hubs.codec, wm8994_eq_controls,
M
Mark Brown 已提交
3335
				     ARRAY_SIZE(wm8994_eq_controls));
3336 3337 3338 3339 3340 3341 3342

	for (i = 0; i < ARRAY_SIZE(pdata->micbias); i++) {
		if (pdata->micbias[i]) {
			snd_soc_write(codec, WM8958_MICBIAS1 + i,
				pdata->micbias[i] & 0xffff);
		}
	}
M
Mark Brown 已提交
3343 3344
}

3345 3346 3347 3348 3349 3350 3351 3352 3353
/**
 * wm8994_mic_detect - Enable microphone detection via the WM8994 IRQ
 *
 * @codec:   WM8994 codec
 * @jack:    jack to report detection events on
 * @micbias: microphone bias to detect on
 *
 * Enable microphone detection via IRQ on the WM8994.  If GPIOs are
 * being used to bring out signals to the processor then only platform
3354
 * data configuration is needed for WM8994 and processor GPIOs should
3355 3356 3357 3358 3359 3360
 * be configured using snd_soc_jack_add_gpios() instead.
 *
 * Configuration of detection levels is available via the micbias1_lvl
 * and micbias2_lvl platform data members.
 */
int wm8994_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
3361
		      int micbias)
3362
{
3363
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
3364
	struct wm8994_micdet *micdet;
3365
	struct wm8994 *control = wm8994->wm8994;
3366
	int reg, ret;
3367

3368 3369
	if (control->type != WM8994) {
		dev_warn(codec->dev, "Not a WM8994\n");
3370
		return -EINVAL;
3371
	}
3372

3373 3374 3375
	switch (micbias) {
	case 1:
		micdet = &wm8994->micdet[0];
3376 3377 3378 3379 3380 3381
		if (jack)
			ret = snd_soc_dapm_force_enable_pin(&codec->dapm,
							    "MICBIAS1");
		else
			ret = snd_soc_dapm_disable_pin(&codec->dapm,
						       "MICBIAS1");
3382 3383 3384
		break;
	case 2:
		micdet = &wm8994->micdet[1];
3385 3386 3387 3388 3389 3390
		if (jack)
			ret = snd_soc_dapm_force_enable_pin(&codec->dapm,
							    "MICBIAS1");
		else
			ret = snd_soc_dapm_disable_pin(&codec->dapm,
						       "MICBIAS1");
3391 3392
		break;
	default:
3393
		dev_warn(codec->dev, "Invalid MICBIAS %d\n", micbias);
3394
		return -EINVAL;
3395
	}
3396

3397 3398 3399 3400 3401 3402
	if (ret != 0)
		dev_warn(codec->dev, "Failed to configure MICBIAS%d: %d\n",
			 micbias, ret);

	dev_dbg(codec->dev, "Configuring microphone detection on %d %p\n",
		micbias, jack);
3403 3404 3405

	/* Store the configuration */
	micdet->jack = jack;
3406
	micdet->detecting = true;
3407 3408 3409 3410

	/* If either of the jacks is set up then enable detection */
	if (wm8994->micdet[0].jack || wm8994->micdet[1].jack)
		reg = WM8994_MICD_ENA;
3411
	else
3412 3413 3414 3415
		reg = 0;

	snd_soc_update_bits(codec, WM8994_MICBIAS, WM8994_MICD_ENA, reg);

3416 3417 3418 3419 3420 3421
	/* enable MICDET and MICSHRT deboune */
	snd_soc_update_bits(codec, WM8994_IRQ_DEBOUNCE,
			    WM8994_MIC1_DET_DB_MASK | WM8994_MIC1_SHRT_DB_MASK |
			    WM8994_MIC2_DET_DB_MASK | WM8994_MIC2_SHRT_DB_MASK,
			    WM8994_MIC1_DET_DB | WM8994_MIC1_SHRT_DB);

3422 3423
	snd_soc_dapm_sync(&codec->dapm);

3424 3425 3426 3427
	return 0;
}
EXPORT_SYMBOL_GPL(wm8994_mic_detect);

3428
static void wm8994_mic_work(struct work_struct *work)
3429
{
3430 3431 3432
	struct wm8994_priv *priv = container_of(work,
						struct wm8994_priv,
						mic_work.work);
3433 3434 3435 3436
	struct regmap *regmap = priv->wm8994->regmap;
	struct device *dev = priv->wm8994->dev;
	unsigned int reg;
	int ret;
3437 3438
	int report;

3439 3440
	pm_runtime_get_sync(dev);

3441 3442 3443 3444
	ret = regmap_read(regmap, WM8994_INTERRUPT_RAW_STATUS_2, &reg);
	if (ret < 0) {
		dev_err(dev, "Failed to read microphone status: %d\n",
			ret);
3445
		pm_runtime_put(dev);
3446
		return;
3447 3448
	}

3449
	dev_dbg(dev, "Microphone status: %x\n", reg);
3450 3451

	report = 0;
3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466
	if (reg & WM8994_MIC1_DET_STS) {
		if (priv->micdet[0].detecting)
			report = SND_JACK_HEADSET;
	}
	if (reg & WM8994_MIC1_SHRT_STS) {
		if (priv->micdet[0].detecting)
			report = SND_JACK_HEADPHONE;
		else
			report |= SND_JACK_BTN_0;
	}
	if (report)
		priv->micdet[0].detecting = false;
	else
		priv->micdet[0].detecting = true;

3467
	snd_soc_jack_report(priv->micdet[0].jack, report,
3468
			    SND_JACK_HEADSET | SND_JACK_BTN_0);
3469 3470

	report = 0;
3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485
	if (reg & WM8994_MIC2_DET_STS) {
		if (priv->micdet[1].detecting)
			report = SND_JACK_HEADSET;
	}
	if (reg & WM8994_MIC2_SHRT_STS) {
		if (priv->micdet[1].detecting)
			report = SND_JACK_HEADPHONE;
		else
			report |= SND_JACK_BTN_0;
	}
	if (report)
		priv->micdet[1].detecting = false;
	else
		priv->micdet[1].detecting = true;

3486
	snd_soc_jack_report(priv->micdet[1].jack, report,
3487
			    SND_JACK_HEADSET | SND_JACK_BTN_0);
3488 3489

	pm_runtime_put(dev);
3490 3491 3492 3493 3494
}

static irqreturn_t wm8994_mic_irq(int irq, void *data)
{
	struct wm8994_priv *priv = data;
3495
	struct snd_soc_codec *codec = priv->hubs.codec;
3496 3497 3498 3499 3500 3501 3502

#ifndef CONFIG_SND_SOC_WM8994_MODULE
	trace_snd_soc_jack_irq(dev_name(codec->dev));
#endif

	pm_wakeup_event(codec->dev, 300);

3503 3504
	queue_delayed_work(system_power_efficient_wq,
			   &priv->mic_work, msecs_to_jiffies(250));
3505 3506 3507 3508

	return IRQ_HANDLED;
}

3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528
static void wm1811_micd_stop(struct snd_soc_codec *codec)
{
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);

	if (!wm8994->jackdet)
		return;

	mutex_lock(&wm8994->accdet_lock);

	snd_soc_update_bits(codec, WM8958_MIC_DETECT_1, WM8958_MICD_ENA, 0);

	wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_JACK);

	mutex_unlock(&wm8994->accdet_lock);

	if (wm8994->wm8994->pdata.jd_ext_cap)
		snd_soc_dapm_disable_pin(&codec->dapm,
					 "MICBIAS2");
}

3529
static void wm8958_button_det(struct snd_soc_codec *codec, u16 status)
3530 3531
{
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
3532
	int report;
3533

3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556
	report = 0;
	if (status & 0x4)
		report |= SND_JACK_BTN_0;

	if (status & 0x8)
		report |= SND_JACK_BTN_1;

	if (status & 0x10)
		report |= SND_JACK_BTN_2;

	if (status & 0x20)
		report |= SND_JACK_BTN_3;

	if (status & 0x40)
		report |= SND_JACK_BTN_4;

	if (status & 0x80)
		report |= SND_JACK_BTN_5;

	snd_soc_jack_report(wm8994->micdet[0].jack, report,
			    wm8994->btn_mask);
}

3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581
static void wm8958_open_circuit_work(struct work_struct *work)
{
	struct wm8994_priv *wm8994 = container_of(work,
						  struct wm8994_priv,
						  open_circuit_work.work);
	struct device *dev = wm8994->wm8994->dev;

	wm1811_micd_stop(wm8994->hubs.codec);

	mutex_lock(&wm8994->accdet_lock);

	dev_dbg(dev, "Reporting open circuit\n");

	wm8994->jack_mic = false;
	wm8994->mic_detecting = true;

	wm8958_micd_set_rate(wm8994->hubs.codec);

	snd_soc_jack_report(wm8994->micdet[0].jack, 0,
			    wm8994->btn_mask |
			    SND_JACK_HEADSET);

	mutex_unlock(&wm8994->accdet_lock);
}

3582
static void wm8958_mic_id(void *data, u16 status)
3583
{
3584
	struct snd_soc_codec *codec = data;
3585
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
3586

3587
	/* Either nothing present or just starting detection */
3588
	if (!(status & WM8958_MICD_STS)) {
3589 3590 3591
		/* If nothing present then clear our statuses */
		dev_dbg(codec->dev, "Detected open circuit\n");

3592 3593 3594
		queue_delayed_work(system_power_efficient_wq,
				   &wm8994->open_circuit_work,
				   msecs_to_jiffies(2500));
3595 3596
		return;
	}
3597

3598 3599 3600
	/* If the measurement is showing a high impedence we've got a
	 * microphone.
	 */
3601
	if (status & 0x600) {
3602 3603
		dev_dbg(codec->dev, "Detected microphone\n");

3604
		wm8994->mic_detecting = false;
3605 3606 3607 3608 3609 3610 3611
		wm8994->jack_mic = true;

		wm8958_micd_set_rate(codec);

		snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADSET,
				    SND_JACK_HEADSET);
	}
3612

3613

3614
	if (status & 0xfc) {
3615
		dev_dbg(codec->dev, "Detected headphone\n");
3616
		wm8994->mic_detecting = false;
3617 3618 3619

		wm8958_micd_set_rate(codec);

3620
		/* If we have jackdet that will detect removal */
3621
		wm1811_micd_stop(codec);
3622 3623 3624

		snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADPHONE,
				    SND_JACK_HEADSET);
3625
	}
3626
}
3627

3628 3629 3630 3631 3632
/* Deferred mic detection to allow for extra settling time */
static void wm1811_mic_work(struct work_struct *work)
{
	struct wm8994_priv *wm8994 = container_of(work, struct wm8994_priv,
						  mic_work.work);
3633
	struct wm8994 *control = wm8994->wm8994;
3634
	struct snd_soc_codec *codec = wm8994->hubs.codec;
3635

3636
	pm_runtime_get_sync(codec->dev);
3637

3638
	/* If required for an external cap force MICBIAS on */
3639
	if (control->pdata.jd_ext_cap) {
3640 3641 3642 3643
		snd_soc_dapm_force_enable_pin(&codec->dapm,
					      "MICBIAS2");
		snd_soc_dapm_sync(&codec->dapm);
	}
3644

3645
	mutex_lock(&wm8994->accdet_lock);
3646

3647
	dev_dbg(codec->dev, "Starting mic detection\n");
3648

3649 3650 3651 3652 3653 3654 3655 3656 3657 3658
	/* Use a user-supplied callback if we have one */
	if (wm8994->micd_cb) {
		wm8994->micd_cb(wm8994->micd_cb_data);
	} else {
		/*
		 * Start off measument of microphone impedence to find out
		 * what's actually there.
		 */
		wm8994->mic_detecting = true;
		wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_MIC);
3659

3660 3661
		snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
				    WM8958_MICD_ENA, WM8958_MICD_ENA);
3662
	}
3663 3664 3665 3666

	mutex_unlock(&wm8994->accdet_lock);

	pm_runtime_put(codec->dev);
3667 3668
}

3669 3670 3671
static irqreturn_t wm1811_jackdet_irq(int irq, void *data)
{
	struct wm8994_priv *wm8994 = data;
3672
	struct wm8994 *control = wm8994->wm8994;
3673
	struct snd_soc_codec *codec = wm8994->hubs.codec;
3674
	int reg, delay;
3675
	bool present;
3676

3677 3678
	pm_runtime_get_sync(codec->dev);

3679 3680
	cancel_delayed_work_sync(&wm8994->mic_complete_work);

3681 3682 3683 3684 3685 3686
	mutex_lock(&wm8994->accdet_lock);

	reg = snd_soc_read(codec, WM1811_JACKDET_CTRL);
	if (reg < 0) {
		dev_err(codec->dev, "Failed to read jack status: %d\n", reg);
		mutex_unlock(&wm8994->accdet_lock);
3687
		pm_runtime_put(codec->dev);
3688 3689 3690 3691 3692
		return IRQ_NONE;
	}

	dev_dbg(codec->dev, "JACKDET %x\n", reg);

3693
	present = reg & WM1811_JACKDET_LVL;
3694

3695 3696
	if (present) {
		dev_dbg(codec->dev, "Jack detected\n");
3697

3698 3699
		wm8958_micd_set_rate(codec);

3700 3701 3702
		snd_soc_update_bits(codec, WM8958_MICBIAS2,
				    WM8958_MICB2_DISCH, 0);

3703 3704 3705 3706
		/* Disable debounce while inserted */
		snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
				    WM1811_JACKDET_DB, 0);

3707
		delay = control->pdata.micdet_delay;
3708 3709 3710
		queue_delayed_work(system_power_efficient_wq,
				   &wm8994->mic_work,
				   msecs_to_jiffies(delay));
3711 3712 3713
	} else {
		dev_dbg(codec->dev, "Jack not detected\n");

3714 3715
		cancel_delayed_work_sync(&wm8994->mic_work);

3716 3717 3718
		snd_soc_update_bits(codec, WM8958_MICBIAS2,
				    WM8958_MICB2_DISCH, WM8958_MICB2_DISCH);

3719 3720 3721 3722
		/* Enable debounce while removed */
		snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
				    WM1811_JACKDET_DB, WM1811_JACKDET_DB);

3723 3724 3725 3726 3727 3728 3729 3730 3731
		wm8994->mic_detecting = false;
		wm8994->jack_mic = false;
		snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
				    WM8958_MICD_ENA, 0);
		wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_JACK);
	}

	mutex_unlock(&wm8994->accdet_lock);

3732
	/* Turn off MICBIAS if it was on for an external cap */
3733
	if (control->pdata.jd_ext_cap && !present)
3734
		snd_soc_dapm_disable_pin(&codec->dapm, "MICBIAS2");
3735 3736 3737 3738 3739 3740 3741 3742 3743

	if (present)
		snd_soc_jack_report(wm8994->micdet[0].jack,
				    SND_JACK_MECHANICAL, SND_JACK_MECHANICAL);
	else
		snd_soc_jack_report(wm8994->micdet[0].jack, 0,
				    SND_JACK_MECHANICAL | SND_JACK_HEADSET |
				    wm8994->btn_mask);

3744 3745 3746 3747
	/* Since we only report deltas force an update, ensures we
	 * avoid bootstrapping issues with the core. */
	snd_soc_jack_report(wm8994->micdet[0].jack, 0, 0);

3748
	pm_runtime_put(codec->dev);
3749 3750 3751
	return IRQ_HANDLED;
}

3752 3753 3754 3755 3756 3757 3758 3759
static void wm1811_jackdet_bootstrap(struct work_struct *work)
{
	struct wm8994_priv *wm8994 = container_of(work,
						struct wm8994_priv,
						jackdet_bootstrap.work);
	wm1811_jackdet_irq(0, wm8994);
}

3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776
/**
 * wm8958_mic_detect - Enable microphone detection via the WM8958 IRQ
 *
 * @codec:   WM8958 codec
 * @jack:    jack to report detection events on
 *
 * Enable microphone detection functionality for the WM8958.  By
 * default simple detection which supports the detection of up to 6
 * buttons plus video and microphone functionality is supported.
 *
 * The WM8958 has an advanced jack detection facility which is able to
 * support complex accessory detection, especially when used in
 * conjunction with external circuitry.  In order to provide maximum
 * flexiblity a callback is provided which allows a completely custom
 * detection algorithm.
 */
int wm8958_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
3777 3778
		      wm1811_micdet_cb det_cb, void *det_cb_data,
		      wm1811_mic_id_cb id_cb, void *id_cb_data)
3779 3780
{
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
3781
	struct wm8994 *control = wm8994->wm8994;
3782
	u16 micd_lvl_sel;
3783

M
Mark Brown 已提交
3784 3785 3786 3787 3788
	switch (control->type) {
	case WM1811:
	case WM8958:
		break;
	default:
3789
		return -EINVAL;
M
Mark Brown 已提交
3790
	}
3791 3792

	if (jack) {
3793
		snd_soc_dapm_force_enable_pin(&codec->dapm, "CLK_SYS");
3794
		snd_soc_dapm_sync(&codec->dapm);
3795

3796 3797
		wm8994->micdet[0].jack = jack;

3798 3799 3800
		if (det_cb) {
			wm8994->micd_cb = det_cb;
			wm8994->micd_cb_data = det_cb_data;
3801 3802 3803 3804
		} else {
			wm8994->mic_detecting = true;
			wm8994->jack_mic = false;
		}
3805

3806 3807 3808 3809 3810 3811 3812
		if (id_cb) {
			wm8994->mic_id_cb = id_cb;
			wm8994->mic_id_cb_data = id_cb_data;
		} else {
			wm8994->mic_id_cb = wm8958_mic_id;
			wm8994->mic_id_cb_data = codec;
		}
3813 3814 3815

		wm8958_micd_set_rate(codec);

3816
		/* Detect microphones and short circuits by default */
3817 3818
		if (control->pdata.micd_lvl_sel)
			micd_lvl_sel = control->pdata.micd_lvl_sel;
3819 3820 3821 3822 3823 3824 3825
		else
			micd_lvl_sel = 0x41;

		wm8994->btn_mask = SND_JACK_BTN_0 | SND_JACK_BTN_1 |
			SND_JACK_BTN_2 | SND_JACK_BTN_3 |
			SND_JACK_BTN_4 | SND_JACK_BTN_5;

3826
		snd_soc_update_bits(codec, WM8958_MIC_DETECT_2,
3827
				    WM8958_MICD_LVL_SEL_MASK, micd_lvl_sel);
3828

3829 3830 3831 3832 3833 3834 3835
		WARN_ON(codec->dapm.bias_level > SND_SOC_BIAS_STANDBY);

		/*
		 * If we can use jack detection start off with that,
		 * otherwise jump straight to microphone detection.
		 */
		if (wm8994->jackdet) {
3836 3837 3838 3839
			/* Disable debounce for the initial detect */
			snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
					    WM1811_JACKDET_DB, 0);

3840 3841 3842
			snd_soc_update_bits(codec, WM8958_MICBIAS2,
					    WM8958_MICB2_DISCH,
					    WM8958_MICB2_DISCH);
3843 3844 3845 3846 3847 3848 3849 3850 3851
			snd_soc_update_bits(codec, WM8994_LDO_1,
					    WM8994_LDO1_DISCH, 0);
			wm1811_jackdet_set_mode(codec,
						WM1811_JACKDET_MODE_JACK);
		} else {
			snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
					    WM8958_MICD_ENA, WM8958_MICD_ENA);
		}

3852 3853 3854
	} else {
		snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
				    WM8958_MICD_ENA, 0);
3855
		wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_NONE);
3856
		snd_soc_dapm_disable_pin(&codec->dapm, "CLK_SYS");
3857
		snd_soc_dapm_sync(&codec->dapm);
3858 3859 3860 3861 3862 3863
	}

	return 0;
}
EXPORT_SYMBOL_GPL(wm8958_mic_detect);

3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881
static void wm8958_mic_work(struct work_struct *work)
{
	struct wm8994_priv *wm8994 = container_of(work,
						  struct wm8994_priv,
						  mic_complete_work.work);
	struct snd_soc_codec *codec = wm8994->hubs.codec;

	pm_runtime_get_sync(codec->dev);

	mutex_lock(&wm8994->accdet_lock);

	wm8994->mic_id_cb(wm8994->mic_id_cb_data, wm8994->mic_status);

	mutex_unlock(&wm8994->accdet_lock);

	pm_runtime_put(codec->dev);
}

3882 3883 3884
static irqreturn_t wm8958_mic_irq(int irq, void *data)
{
	struct wm8994_priv *wm8994 = data;
3885
	struct snd_soc_codec *codec = wm8994->hubs.codec;
3886
	int reg, count, ret, id_delay;
3887

3888 3889 3890 3891 3892
	/*
	 * Jack detection may have detected a removal simulataneously
	 * with an update of the MICDET status; if so it will have
	 * stopped detection and we can ignore this interrupt.
	 */
3893
	if (!(snd_soc_read(codec, WM8958_MIC_DETECT_1) & WM8958_MICD_ENA))
3894 3895
		return IRQ_HANDLED;

3896
	cancel_delayed_work_sync(&wm8994->mic_complete_work);
3897 3898
	cancel_delayed_work_sync(&wm8994->open_circuit_work);

3899 3900
	pm_runtime_get_sync(codec->dev);

3901 3902 3903 3904 3905 3906 3907 3908 3909 3910
	/* We may occasionally read a detection without an impedence
	 * range being provided - if that happens loop again.
	 */
	count = 10;
	do {
		reg = snd_soc_read(codec, WM8958_MIC_DETECT_3);
		if (reg < 0) {
			dev_err(codec->dev,
				"Failed to read mic detect status: %d\n",
				reg);
3911
			pm_runtime_put(codec->dev);
3912 3913
			return IRQ_NONE;
		}
3914

3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926
		if (!(reg & WM8958_MICD_VALID)) {
			dev_dbg(codec->dev, "Mic detect data not valid\n");
			goto out;
		}

		if (!(reg & WM8958_MICD_STS) || (reg & WM8958_MICD_LVL_MASK))
			break;

		msleep(1);
	} while (count--);

	if (count == 0)
M
Masanari Iida 已提交
3927
		dev_warn(codec->dev, "No impedance range reported for jack\n");
3928

3929
#ifndef CONFIG_SND_SOC_WM8994_MODULE
3930
	trace_snd_soc_jack_irq(dev_name(codec->dev));
3931
#endif
3932

3933 3934
	/* Avoid a transient report when the accessory is being removed */
	if (wm8994->jackdet) {
M
Mark Brown 已提交
3935 3936
		ret = snd_soc_read(codec, WM1811_JACKDET_CTRL);
		if (ret < 0) {
3937
			dev_err(codec->dev, "Failed to read jack status: %d\n",
M
Mark Brown 已提交
3938 3939
				ret);
		} else if (!(ret & WM1811_JACKDET_LVL)) {
3940
			dev_dbg(codec->dev, "Ignoring removed jack\n");
3941
			goto out;
3942
		}
3943 3944 3945 3946
	} else if (!(reg & WM8958_MICD_STS)) {
		snd_soc_jack_report(wm8994->micdet[0].jack, 0,
				    SND_JACK_MECHANICAL | SND_JACK_HEADSET |
				    wm8994->btn_mask);
3947
		wm8994->mic_detecting = true;
3948
		goto out;
3949 3950
	}

3951 3952 3953
	wm8994->mic_status = reg;
	id_delay = wm8994->wm8994->pdata.mic_id_delay;

3954
	if (wm8994->mic_detecting)
3955 3956 3957
		queue_delayed_work(system_power_efficient_wq,
				   &wm8994->mic_complete_work,
				   msecs_to_jiffies(id_delay));
3958
	else
3959
		wm8958_button_det(codec, reg);
3960 3961

out:
3962
	pm_runtime_put(codec->dev);
3963 3964 3965
	return IRQ_HANDLED;
}

3966 3967 3968 3969 3970 3971 3972 3973 3974
static irqreturn_t wm8994_fifo_error(int irq, void *data)
{
	struct snd_soc_codec *codec = data;

	dev_err(codec->dev, "FIFO error\n");

	return IRQ_HANDLED;
}

3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992
static irqreturn_t wm8994_temp_warn(int irq, void *data)
{
	struct snd_soc_codec *codec = data;

	dev_err(codec->dev, "Thermal warning\n");

	return IRQ_HANDLED;
}

static irqreturn_t wm8994_temp_shut(int irq, void *data)
{
	struct snd_soc_codec *codec = data;

	dev_crit(codec->dev, "Thermal shutdown\n");

	return IRQ_HANDLED;
}

3993
static int wm8994_codec_probe(struct snd_soc_codec *codec)
M
Mark Brown 已提交
3994
{
3995
	struct wm8994 *control = dev_get_drvdata(codec->dev->parent);
3996
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
L
Liam Girdwood 已提交
3997
	struct snd_soc_dapm_context *dapm = &codec->dapm;
3998
	unsigned int reg;
M
Mark Brown 已提交
3999
	int ret, i;
M
Mark Brown 已提交
4000

4001
	wm8994->hubs.codec = codec;
4002
	codec->control_data = control->regmap;
M
Mark Brown 已提交
4003

4004
	snd_soc_codec_set_cache_io(codec, 16, 16, SND_SOC_REGMAP);
4005

4006
	mutex_init(&wm8994->accdet_lock);
4007 4008
	INIT_DELAYED_WORK(&wm8994->jackdet_bootstrap,
			  wm1811_jackdet_bootstrap);
4009 4010
	INIT_DELAYED_WORK(&wm8994->open_circuit_work,
			  wm8958_open_circuit_work);
4011

4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022
	switch (control->type) {
	case WM8994:
		INIT_DELAYED_WORK(&wm8994->mic_work, wm8994_mic_work);
		break;
	case WM1811:
		INIT_DELAYED_WORK(&wm8994->mic_work, wm1811_mic_work);
		break;
	default:
		break;
	}

4023 4024
	INIT_DELAYED_WORK(&wm8994->mic_complete_work, wm8958_mic_work);

M
Mark Brown 已提交
4025 4026 4027
	for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
		init_completion(&wm8994->fll_locked[i]);

4028
	wm8994->micdet_irq = control->pdata.micdet_irq;
4029

4030 4031 4032
	/* By default use idle_bias_off, will override for WM8994 */
	codec->dapm.idle_bias_off = 1;

M
Mark Brown 已提交
4033
	/* Set revision-specific configuration */
4034 4035
	switch (control->type) {
	case WM8994:
4036
		/* Single ended line outputs should have VMID on. */
4037 4038
		if (!control->pdata.lineout1_diff ||
		    !control->pdata.lineout2_diff)
4039 4040
			codec->dapm.idle_bias_off = 0;

4041
		switch (control->revision) {
4042 4043
		case 2:
		case 3:
4044 4045
			wm8994->hubs.dcs_codes_l = -5;
			wm8994->hubs.dcs_codes_r = -5;
4046 4047
			wm8994->hubs.hp_startup_mode = 1;
			wm8994->hubs.dcs_readback_mode = 1;
4048
			wm8994->hubs.series_startup = 1;
4049 4050
			break;
		default:
4051
			wm8994->hubs.dcs_readback_mode = 2;
4052 4053
			break;
		}
4054
		break;
4055 4056

	case WM8958:
4057
		wm8994->hubs.dcs_readback_mode = 1;
4058
		wm8994->hubs.hp_startup_mode = 1;
4059

4060
		switch (control->revision) {
4061 4062 4063 4064 4065 4066
		case 0:
			break;
		default:
			wm8994->fll_byp = true;
			break;
		}
M
Mark Brown 已提交
4067
		break;
4068

M
Mark Brown 已提交
4069 4070 4071
	case WM1811:
		wm8994->hubs.dcs_readback_mode = 2;
		wm8994->hubs.no_series_update = 1;
4072
		wm8994->hubs.hp_startup_mode = 1;
4073
		wm8994->hubs.no_cache_dac_hp_direct = true;
4074
		wm8994->fll_byp = true;
M
Mark Brown 已提交
4075

4076 4077
		wm8994->hubs.dcs_codes_l = -9;
		wm8994->hubs.dcs_codes_r = -7;
M
Mark Brown 已提交
4078 4079 4080 4081 4082

		snd_soc_update_bits(codec, WM8994_ANALOGUE_HP_1,
				    WM1811_HPOUT1_ATTN, WM1811_HPOUT1_ATTN);
		break;

M
Mark Brown 已提交
4083 4084 4085 4086
	default:
		break;
	}

4087
	wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR,
4088
			   wm8994_fifo_error, "FIFO error", codec);
4089
	wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN,
4090
			   wm8994_temp_warn, "Thermal warning", codec);
4091
	wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT,
4092
			   wm8994_temp_shut, "Thermal shutdown", codec);
4093

4094 4095
	switch (control->type) {
	case WM8994:
4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106
		if (wm8994->micdet_irq) {
			ret = request_threaded_irq(wm8994->micdet_irq, NULL,
						   wm8994_mic_irq,
						   IRQF_TRIGGER_RISING,
						   "Mic1 detect",
						   wm8994);
			if (ret != 0)
				dev_warn(codec->dev,
					 "Failed to request Mic1 detect IRQ: %d\n",
					 ret);
		}
4107

4108
		ret = wm8994_request_irq(wm8994->wm8994,
4109 4110 4111 4112 4113 4114 4115 4116
					 WM8994_IRQ_MIC1_SHRT,
					 wm8994_mic_irq, "Mic 1 short",
					 wm8994);
		if (ret != 0)
			dev_warn(codec->dev,
				 "Failed to request Mic1 short IRQ: %d\n",
				 ret);

4117
		ret = wm8994_request_irq(wm8994->wm8994,
4118 4119 4120 4121 4122 4123 4124 4125
					 WM8994_IRQ_MIC2_DET,
					 wm8994_mic_irq, "Mic 2 detect",
					 wm8994);
		if (ret != 0)
			dev_warn(codec->dev,
				 "Failed to request Mic2 detect IRQ: %d\n",
				 ret);

4126
		ret = wm8994_request_irq(wm8994->wm8994,
4127 4128 4129 4130 4131 4132 4133 4134
					 WM8994_IRQ_MIC2_SHRT,
					 wm8994_mic_irq, "Mic 2 short",
					 wm8994);
		if (ret != 0)
			dev_warn(codec->dev,
				 "Failed to request Mic2 short IRQ: %d\n",
				 ret);
		break;
4135 4136

	case WM8958:
M
Mark Brown 已提交
4137
	case WM1811:
4138 4139 4140 4141 4142 4143 4144 4145 4146 4147
		if (wm8994->micdet_irq) {
			ret = request_threaded_irq(wm8994->micdet_irq, NULL,
						   wm8958_mic_irq,
						   IRQF_TRIGGER_RISING,
						   "Mic detect",
						   wm8994);
			if (ret != 0)
				dev_warn(codec->dev,
					 "Failed to request Mic detect IRQ: %d\n",
					 ret);
4148 4149 4150 4151
		} else {
			wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_MIC1_DET,
					   wm8958_mic_irq, "Mic detect",
					   wm8994);
4152
		}
4153
	}
4154

4155 4156
	switch (control->type) {
	case WM1811:
4157
		if (control->cust_id > 1 || control->revision > 1) {
4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169
			ret = wm8994_request_irq(wm8994->wm8994,
						 WM8994_IRQ_GPIO(6),
						 wm1811_jackdet_irq, "JACKDET",
						 wm8994);
			if (ret == 0)
				wm8994->jackdet = true;
		}
		break;
	default:
		break;
	}

M
Mark Brown 已提交
4170 4171
	wm8994->fll_locked_irq = true;
	for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++) {
4172
		ret = wm8994_request_irq(wm8994->wm8994,
M
Mark Brown 已提交
4173 4174 4175 4176 4177 4178 4179
					 WM8994_IRQ_FLL1_LOCK + i,
					 wm8994_fll_locked_irq, "FLL lock",
					 &wm8994->fll_locked[i]);
		if (ret != 0)
			wm8994->fll_locked_irq = false;
	}

4180 4181 4182
	/* Make sure we can read from the GPIOs if they're inputs */
	pm_runtime_get_sync(codec->dev);

M
Mark Brown 已提交
4183 4184 4185 4186
	/* Remember if AIFnLRCLK is configured as a GPIO.  This should be
	 * configured on init - if a system wants to do this dynamically
	 * at runtime we can deal with that then.
	 */
4187
	ret = regmap_read(control->regmap, WM8994_GPIO_1, &reg);
M
Mark Brown 已提交
4188 4189
	if (ret < 0) {
		dev_err(codec->dev, "Failed to read GPIO1 state: %d\n", ret);
4190
		goto err_irq;
M
Mark Brown 已提交
4191
	}
4192
	if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
M
Mark Brown 已提交
4193 4194 4195 4196 4197 4198
		wm8994->lrclk_shared[0] = 1;
		wm8994_dai[0].symmetric_rates = 1;
	} else {
		wm8994->lrclk_shared[0] = 0;
	}

4199
	ret = regmap_read(control->regmap, WM8994_GPIO_6, &reg);
M
Mark Brown 已提交
4200 4201
	if (ret < 0) {
		dev_err(codec->dev, "Failed to read GPIO6 state: %d\n", ret);
4202
		goto err_irq;
M
Mark Brown 已提交
4203
	}
4204
	if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
M
Mark Brown 已提交
4205 4206 4207 4208 4209 4210
		wm8994->lrclk_shared[1] = 1;
		wm8994_dai[1].symmetric_rates = 1;
	} else {
		wm8994->lrclk_shared[1] = 0;
	}

4211 4212
	pm_runtime_put(codec->dev);

4213 4214 4215 4216 4217
	/* Latch volume update bits */
	for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
		snd_soc_update_bits(codec, wm8994_vu_bits[i].reg,
				    wm8994_vu_bits[i].mask,
				    wm8994_vu_bits[i].mask);
M
Mark Brown 已提交
4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229

	/* Set the low bit of the 3D stereo depth so TLV matches */
	snd_soc_update_bits(codec, WM8994_AIF1_DAC1_FILTERS_2,
			    1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT,
			    1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT);
	snd_soc_update_bits(codec, WM8994_AIF1_DAC2_FILTERS_2,
			    1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT,
			    1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT);
	snd_soc_update_bits(codec, WM8994_AIF2_DAC_FILTERS_2,
			    1 << WM8994_AIF2DAC_3D_GAIN_SHIFT,
			    1 << WM8994_AIF2DAC_3D_GAIN_SHIFT);

4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241
	/* Unconditionally enable AIF1 ADC TDM mode on chips which can
	 * use this; it only affects behaviour on idle TDM clock
	 * cycles. */
	switch (control->type) {
	case WM8994:
	case WM8958:
		snd_soc_update_bits(codec, WM8994_AIF1_CONTROL_1,
				    WM8994_AIF1ADC_TDM, WM8994_AIF1ADC_TDM);
		break;
	default:
		break;
	}
4242

4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255
	/* Put MICBIAS into bypass mode by default on newer devices */
	switch (control->type) {
	case WM8958:
	case WM1811:
		snd_soc_update_bits(codec, WM8958_MICBIAS1,
				    WM8958_MICB1_MODE, WM8958_MICB1_MODE);
		snd_soc_update_bits(codec, WM8958_MICBIAS2,
				    WM8958_MICB2_MODE, WM8958_MICB2_MODE);
		break;
	default:
		break;
	}

4256 4257
	wm8994->hubs.check_class_w_digital = wm8994_check_class_w_digital;
	wm_hubs_update_class_w(codec);
M
Mark Brown 已提交
4258

4259
	wm8994_handle_pdata(wm8994);
M
Mark Brown 已提交
4260

4261
	wm_hubs_add_analogue_controls(codec);
4262
	snd_soc_add_codec_controls(codec, wm8994_snd_controls,
4263
			     ARRAY_SIZE(wm8994_snd_controls));
L
Liam Girdwood 已提交
4264
	snd_soc_dapm_new_controls(dapm, wm8994_dapm_widgets,
4265
				  ARRAY_SIZE(wm8994_dapm_widgets));
4266 4267 4268 4269 4270

	switch (control->type) {
	case WM8994:
		snd_soc_dapm_new_controls(dapm, wm8994_specific_dapm_widgets,
					  ARRAY_SIZE(wm8994_specific_dapm_widgets));
4271
		if (control->revision < 4) {
4272 4273
			snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
						  ARRAY_SIZE(wm8994_lateclk_revd_widgets));
4274 4275
			snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
						  ARRAY_SIZE(wm8994_adc_revd_widgets));
4276 4277 4278
			snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
						  ARRAY_SIZE(wm8994_dac_revd_widgets));
		} else {
4279 4280
			snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
						  ARRAY_SIZE(wm8994_lateclk_widgets));
4281 4282
			snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
						  ARRAY_SIZE(wm8994_adc_widgets));
4283 4284 4285
			snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
						  ARRAY_SIZE(wm8994_dac_widgets));
		}
4286 4287
		break;
	case WM8958:
4288
		snd_soc_add_codec_controls(codec, wm8958_snd_controls,
4289 4290 4291
				     ARRAY_SIZE(wm8958_snd_controls));
		snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
					  ARRAY_SIZE(wm8958_dapm_widgets));
4292
		if (control->revision < 1) {
4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306
			snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
						  ARRAY_SIZE(wm8994_lateclk_revd_widgets));
			snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
						  ARRAY_SIZE(wm8994_adc_revd_widgets));
			snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
						  ARRAY_SIZE(wm8994_dac_revd_widgets));
		} else {
			snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
						  ARRAY_SIZE(wm8994_lateclk_widgets));
			snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
						  ARRAY_SIZE(wm8994_adc_widgets));
			snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
						  ARRAY_SIZE(wm8994_dac_widgets));
		}
4307
		break;
M
Mark Brown 已提交
4308 4309

	case WM1811:
4310
		snd_soc_add_codec_controls(codec, wm8958_snd_controls,
M
Mark Brown 已提交
4311 4312 4313 4314 4315 4316 4317 4318 4319 4320
				     ARRAY_SIZE(wm8958_snd_controls));
		snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
					  ARRAY_SIZE(wm8958_dapm_widgets));
		snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
					  ARRAY_SIZE(wm8994_lateclk_widgets));
		snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
					  ARRAY_SIZE(wm8994_adc_widgets));
		snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
					  ARRAY_SIZE(wm8994_dac_widgets));
		break;
4321 4322
	}

4323
	wm_hubs_add_analogue_routes(codec, 0, 0);
4324 4325 4326 4327 4328
	ret = wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
				 wm_hubs_dcs_done, "DC servo done",
				 &wm8994->hubs);
	if (ret == 0)
		wm8994->hubs.dcs_done_irq = true;
L
Liam Girdwood 已提交
4329
	snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
M
Mark Brown 已提交
4330

4331 4332 4333 4334
	switch (control->type) {
	case WM8994:
		snd_soc_dapm_add_routes(dapm, wm8994_intercon,
					ARRAY_SIZE(wm8994_intercon));
4335

4336
		if (control->revision < 4) {
4337 4338
			snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
						ARRAY_SIZE(wm8994_revd_intercon));
4339 4340 4341 4342 4343 4344
			snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
						ARRAY_SIZE(wm8994_lateclk_revd_intercon));
		} else {
			snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
						ARRAY_SIZE(wm8994_lateclk_intercon));
		}
4345 4346
		break;
	case WM8958:
4347
		if (control->revision < 1) {
4348 4349
			snd_soc_dapm_add_routes(dapm, wm8994_intercon,
						ARRAY_SIZE(wm8994_intercon));
4350 4351 4352 4353 4354 4355 4356 4357 4358 4359
			snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
						ARRAY_SIZE(wm8994_revd_intercon));
			snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
						ARRAY_SIZE(wm8994_lateclk_revd_intercon));
		} else {
			snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
						ARRAY_SIZE(wm8994_lateclk_intercon));
			snd_soc_dapm_add_routes(dapm, wm8958_intercon,
						ARRAY_SIZE(wm8958_intercon));
		}
4360 4361

		wm8958_dsp2_init(codec);
4362
		break;
M
Mark Brown 已提交
4363 4364 4365 4366 4367 4368
	case WM1811:
		snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
					ARRAY_SIZE(wm8994_lateclk_intercon));
		snd_soc_dapm_add_routes(dapm, wm8958_intercon,
					ARRAY_SIZE(wm8958_intercon));
		break;
4369 4370
	}

M
Mark Brown 已提交
4371 4372
	return 0;

4373
err_irq:
4374 4375
	if (wm8994->jackdet)
		wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);
4376 4377 4378
	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_SHRT, wm8994);
	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET, wm8994);
	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT, wm8994);
4379 4380
	if (wm8994->micdet_irq)
		free_irq(wm8994->micdet_irq, wm8994);
M
Mark Brown 已提交
4381
	for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
4382
		wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
M
Mark Brown 已提交
4383
				&wm8994->fll_locked[i]);
4384
	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
4385
			&wm8994->hubs);
4386 4387 4388
	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, codec);
	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, codec);
	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, codec);
4389

M
Mark Brown 已提交
4390 4391 4392
	return ret;
}

4393
static int wm8994_codec_remove(struct snd_soc_codec *codec)
M
Mark Brown 已提交
4394
{
4395
	struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
4396
	struct wm8994 *control = wm8994->wm8994;
M
Mark Brown 已提交
4397
	int i;
M
Mark Brown 已提交
4398 4399

	wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);
4400

M
Mark Brown 已提交
4401
	for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
4402
		wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
M
Mark Brown 已提交
4403 4404
				&wm8994->fll_locked[i]);

4405
	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
4406
			&wm8994->hubs);
4407 4408 4409
	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, codec);
	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, codec);
	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, codec);
4410

4411 4412 4413
	if (wm8994->jackdet)
		wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);

4414 4415
	switch (control->type) {
	case WM8994:
4416 4417
		if (wm8994->micdet_irq)
			free_irq(wm8994->micdet_irq, wm8994);
4418
		wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET,
4419
				wm8994);
4420
		wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT,
4421
				wm8994);
4422
		wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_DET,
4423 4424
				wm8994);
		break;
4425

M
Mark Brown 已提交
4426
	case WM1811:
4427
	case WM8958:
4428 4429
		if (wm8994->micdet_irq)
			free_irq(wm8994->micdet_irq, wm8994);
4430
		break;
4431
	}
4432 4433 4434
	release_firmware(wm8994->mbc);
	release_firmware(wm8994->mbc_vss);
	release_firmware(wm8994->enh_eq);
A
Axel Lin 已提交
4435
	kfree(wm8994->retune_mobile_texts);
M
Mark Brown 已提交
4436 4437 4438
	return 0;
}

4439 4440 4441
static struct snd_soc_codec_driver soc_codec_dev_wm8994 = {
	.probe =	wm8994_codec_probe,
	.remove =	wm8994_codec_remove,
4442 4443
	.suspend =	wm8994_codec_suspend,
	.resume =	wm8994_codec_resume,
4444 4445 4446
	.set_bias_level = wm8994_set_bias_level,
};

4447
static int wm8994_probe(struct platform_device *pdev)
4448
{
4449 4450 4451 4452 4453 4454 4455 4456 4457 4458
	struct wm8994_priv *wm8994;

	wm8994 = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_priv),
			      GFP_KERNEL);
	if (wm8994 == NULL)
		return -ENOMEM;
	platform_set_drvdata(pdev, wm8994);

	wm8994->wm8994 = dev_get_drvdata(pdev->dev.parent);

4459 4460 4461
	pm_runtime_enable(&pdev->dev);
	pm_runtime_idle(&pdev->dev);

4462 4463 4464 4465
	return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wm8994,
			wm8994_dai, ARRAY_SIZE(wm8994_dai));
}

4466
static int wm8994_remove(struct platform_device *pdev)
4467 4468
{
	snd_soc_unregister_codec(&pdev->dev);
4469 4470
	pm_runtime_disable(&pdev->dev);

4471 4472 4473
	return 0;
}

4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491
#ifdef CONFIG_PM_SLEEP
static int wm8994_suspend(struct device *dev)
{
	struct wm8994_priv *wm8994 = dev_get_drvdata(dev);

	/* Drop down to power saving mode when system is suspended */
	if (wm8994->jackdet && !wm8994->active_refcount)
		regmap_update_bits(wm8994->wm8994->regmap, WM8994_ANTIPOP_2,
				   WM1811_JACKDET_MODE_MASK,
				   wm8994->jackdet_mode);

	return 0;
}

static int wm8994_resume(struct device *dev)
{
	struct wm8994_priv *wm8994 = dev_get_drvdata(dev);

4492
	if (wm8994->jackdet && wm8994->jackdet_mode)
4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504
		regmap_update_bits(wm8994->wm8994->regmap, WM8994_ANTIPOP_2,
				   WM1811_JACKDET_MODE_MASK,
				   WM1811_JACKDET_MODE_AUDIO);

	return 0;
}
#endif

static const struct dev_pm_ops wm8994_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(wm8994_suspend, wm8994_resume)
};

M
Mark Brown 已提交
4505 4506
static struct platform_driver wm8994_codec_driver = {
	.driver = {
4507 4508 4509 4510
		.name = "wm8994-codec",
		.owner = THIS_MODULE,
		.pm = &wm8994_pm_ops,
	},
4511
	.probe = wm8994_probe,
4512
	.remove = wm8994_remove,
M
Mark Brown 已提交
4513 4514
};

4515
module_platform_driver(wm8994_codec_driver);
M
Mark Brown 已提交
4516 4517 4518 4519 4520

MODULE_DESCRIPTION("ASoC WM8994 driver");
MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:wm8994-codec");