xref: /openbmc/linux/sound/soc/codecs/wm8994.c (revision ebd09753)
1 /*
2  * wm8994.c  --  WM8994 ALSA SoC Audio driver
3  *
4  * Copyright 2009-12 Wolfson Microelectronics plc
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/pm.h>
19 #include <linux/gcd.h>
20 #include <linux/i2c.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/slab.h>
25 #include <sound/core.h>
26 #include <sound/jack.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/initval.h>
31 #include <sound/tlv.h>
32 #include <trace/events/asoc.h>
33 
34 #include <linux/mfd/wm8994/core.h>
35 #include <linux/mfd/wm8994/registers.h>
36 #include <linux/mfd/wm8994/pdata.h>
37 #include <linux/mfd/wm8994/gpio.h>
38 
39 #include "wm8994.h"
40 #include "wm_hubs.h"
41 
42 #define WM1811_JACKDET_MODE_NONE  0x0000
43 #define WM1811_JACKDET_MODE_JACK  0x0100
44 #define WM1811_JACKDET_MODE_MIC   0x0080
45 #define WM1811_JACKDET_MODE_AUDIO 0x0180
46 
47 #define WM8994_NUM_DRC 3
48 #define WM8994_NUM_EQ  3
49 
50 static struct {
51 	unsigned int reg;
52 	unsigned int mask;
53 } wm8994_vu_bits[] = {
54 	{ WM8994_LEFT_LINE_INPUT_1_2_VOLUME, WM8994_IN1_VU },
55 	{ WM8994_RIGHT_LINE_INPUT_1_2_VOLUME, WM8994_IN1_VU },
56 	{ WM8994_LEFT_LINE_INPUT_3_4_VOLUME, WM8994_IN2_VU },
57 	{ WM8994_RIGHT_LINE_INPUT_3_4_VOLUME, WM8994_IN2_VU },
58 	{ WM8994_SPEAKER_VOLUME_LEFT, WM8994_SPKOUT_VU },
59 	{ WM8994_SPEAKER_VOLUME_RIGHT, WM8994_SPKOUT_VU },
60 	{ WM8994_LEFT_OUTPUT_VOLUME, WM8994_HPOUT1_VU },
61 	{ WM8994_RIGHT_OUTPUT_VOLUME, WM8994_HPOUT1_VU },
62 	{ WM8994_LEFT_OPGA_VOLUME, WM8994_MIXOUT_VU },
63 	{ WM8994_RIGHT_OPGA_VOLUME, WM8994_MIXOUT_VU },
64 
65 	{ WM8994_AIF1_DAC1_LEFT_VOLUME, WM8994_AIF1DAC1_VU },
66 	{ WM8994_AIF1_DAC1_RIGHT_VOLUME, WM8994_AIF1DAC1_VU },
67 	{ WM8994_AIF1_DAC2_LEFT_VOLUME, WM8994_AIF1DAC2_VU },
68 	{ WM8994_AIF1_DAC2_RIGHT_VOLUME, WM8994_AIF1DAC2_VU },
69 	{ WM8994_AIF2_DAC_LEFT_VOLUME, WM8994_AIF2DAC_VU },
70 	{ WM8994_AIF2_DAC_RIGHT_VOLUME, WM8994_AIF2DAC_VU },
71 	{ WM8994_AIF1_ADC1_LEFT_VOLUME, WM8994_AIF1ADC1_VU },
72 	{ WM8994_AIF1_ADC1_RIGHT_VOLUME, WM8994_AIF1ADC1_VU },
73 	{ WM8994_AIF1_ADC2_LEFT_VOLUME, WM8994_AIF1ADC2_VU },
74 	{ WM8994_AIF1_ADC2_RIGHT_VOLUME, WM8994_AIF1ADC2_VU },
75 	{ WM8994_AIF2_ADC_LEFT_VOLUME, WM8994_AIF2ADC_VU },
76 	{ WM8994_AIF2_ADC_RIGHT_VOLUME, WM8994_AIF1ADC2_VU },
77 	{ WM8994_DAC1_LEFT_VOLUME, WM8994_DAC1_VU },
78 	{ WM8994_DAC1_RIGHT_VOLUME, WM8994_DAC1_VU },
79 	{ WM8994_DAC2_LEFT_VOLUME, WM8994_DAC2_VU },
80 	{ WM8994_DAC2_RIGHT_VOLUME, WM8994_DAC2_VU },
81 };
82 
83 static int wm8994_drc_base[] = {
84 	WM8994_AIF1_DRC1_1,
85 	WM8994_AIF1_DRC2_1,
86 	WM8994_AIF2_DRC_1,
87 };
88 
89 static int wm8994_retune_mobile_base[] = {
90 	WM8994_AIF1_DAC1_EQ_GAINS_1,
91 	WM8994_AIF1_DAC2_EQ_GAINS_1,
92 	WM8994_AIF2_EQ_GAINS_1,
93 };
94 
95 static const struct wm8958_micd_rate micdet_rates[] = {
96 	{ 32768,       true,  1, 4 },
97 	{ 32768,       false, 1, 1 },
98 	{ 44100 * 256, true,  7, 10 },
99 	{ 44100 * 256, false, 7, 10 },
100 };
101 
102 static const struct wm8958_micd_rate jackdet_rates[] = {
103 	{ 32768,       true,  0, 1 },
104 	{ 32768,       false, 0, 1 },
105 	{ 44100 * 256, true,  10, 10 },
106 	{ 44100 * 256, false, 7, 8 },
107 };
108 
109 static void wm8958_micd_set_rate(struct snd_soc_component *component)
110 {
111 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
112 	struct wm8994 *control = wm8994->wm8994;
113 	int best, i, sysclk, val;
114 	bool idle;
115 	const struct wm8958_micd_rate *rates;
116 	int num_rates;
117 
118 	idle = !wm8994->jack_mic;
119 
120 	sysclk = snd_soc_component_read32(component, WM8994_CLOCKING_1);
121 	if (sysclk & WM8994_SYSCLK_SRC)
122 		sysclk = wm8994->aifclk[1];
123 	else
124 		sysclk = wm8994->aifclk[0];
125 
126 	if (control->pdata.micd_rates) {
127 		rates = control->pdata.micd_rates;
128 		num_rates = control->pdata.num_micd_rates;
129 	} else if (wm8994->jackdet) {
130 		rates = jackdet_rates;
131 		num_rates = ARRAY_SIZE(jackdet_rates);
132 	} else {
133 		rates = micdet_rates;
134 		num_rates = ARRAY_SIZE(micdet_rates);
135 	}
136 
137 	best = 0;
138 	for (i = 0; i < num_rates; i++) {
139 		if (rates[i].idle != idle)
140 			continue;
141 		if (abs(rates[i].sysclk - sysclk) <
142 		    abs(rates[best].sysclk - sysclk))
143 			best = i;
144 		else if (rates[best].idle != idle)
145 			best = i;
146 	}
147 
148 	val = rates[best].start << WM8958_MICD_BIAS_STARTTIME_SHIFT
149 		| rates[best].rate << WM8958_MICD_RATE_SHIFT;
150 
151 	dev_dbg(component->dev, "MICD rate %d,%d for %dHz %s\n",
152 		rates[best].start, rates[best].rate, sysclk,
153 		idle ? "idle" : "active");
154 
155 	snd_soc_component_update_bits(component, WM8958_MIC_DETECT_1,
156 			    WM8958_MICD_BIAS_STARTTIME_MASK |
157 			    WM8958_MICD_RATE_MASK, val);
158 }
159 
160 static int configure_aif_clock(struct snd_soc_component *component, int aif)
161 {
162 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
163 	int rate;
164 	int reg1 = 0;
165 	int offset;
166 
167 	if (aif)
168 		offset = 4;
169 	else
170 		offset = 0;
171 
172 	switch (wm8994->sysclk[aif]) {
173 	case WM8994_SYSCLK_MCLK1:
174 		rate = wm8994->mclk[0];
175 		break;
176 
177 	case WM8994_SYSCLK_MCLK2:
178 		reg1 |= 0x8;
179 		rate = wm8994->mclk[1];
180 		break;
181 
182 	case WM8994_SYSCLK_FLL1:
183 		reg1 |= 0x10;
184 		rate = wm8994->fll[0].out;
185 		break;
186 
187 	case WM8994_SYSCLK_FLL2:
188 		reg1 |= 0x18;
189 		rate = wm8994->fll[1].out;
190 		break;
191 
192 	default:
193 		return -EINVAL;
194 	}
195 
196 	if (rate >= 13500000) {
197 		rate /= 2;
198 		reg1 |= WM8994_AIF1CLK_DIV;
199 
200 		dev_dbg(component->dev, "Dividing AIF%d clock to %dHz\n",
201 			aif + 1, rate);
202 	}
203 
204 	wm8994->aifclk[aif] = rate;
205 
206 	snd_soc_component_update_bits(component, WM8994_AIF1_CLOCKING_1 + offset,
207 			    WM8994_AIF1CLK_SRC_MASK | WM8994_AIF1CLK_DIV,
208 			    reg1);
209 
210 	return 0;
211 }
212 
213 static int configure_clock(struct snd_soc_component *component)
214 {
215 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
216 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
217 	int change, new;
218 
219 	/* Bring up the AIF clocks first */
220 	configure_aif_clock(component, 0);
221 	configure_aif_clock(component, 1);
222 
223 	/* Then switch CLK_SYS over to the higher of them; a change
224 	 * can only happen as a result of a clocking change which can
225 	 * only be made outside of DAPM so we can safely redo the
226 	 * clocking.
227 	 */
228 
229 	/* If they're equal it doesn't matter which is used */
230 	if (wm8994->aifclk[0] == wm8994->aifclk[1]) {
231 		wm8958_micd_set_rate(component);
232 		return 0;
233 	}
234 
235 	if (wm8994->aifclk[0] < wm8994->aifclk[1])
236 		new = WM8994_SYSCLK_SRC;
237 	else
238 		new = 0;
239 
240 	change = snd_soc_component_update_bits(component, WM8994_CLOCKING_1,
241 				     WM8994_SYSCLK_SRC, new);
242 	if (change)
243 		snd_soc_dapm_sync(dapm);
244 
245 	wm8958_micd_set_rate(component);
246 
247 	return 0;
248 }
249 
250 static int check_clk_sys(struct snd_soc_dapm_widget *source,
251 			 struct snd_soc_dapm_widget *sink)
252 {
253 	struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm);
254 	int reg = snd_soc_component_read32(component, WM8994_CLOCKING_1);
255 	const char *clk;
256 
257 	/* Check what we're currently using for CLK_SYS */
258 	if (reg & WM8994_SYSCLK_SRC)
259 		clk = "AIF2CLK";
260 	else
261 		clk = "AIF1CLK";
262 
263 	return strcmp(source->name, clk) == 0;
264 }
265 
266 static const char *sidetone_hpf_text[] = {
267 	"2.7kHz", "1.35kHz", "675Hz", "370Hz", "180Hz", "90Hz", "45Hz"
268 };
269 
270 static SOC_ENUM_SINGLE_DECL(sidetone_hpf,
271 			    WM8994_SIDETONE, 7, sidetone_hpf_text);
272 
273 static const char *adc_hpf_text[] = {
274 	"HiFi", "Voice 1", "Voice 2", "Voice 3"
275 };
276 
277 static SOC_ENUM_SINGLE_DECL(aif1adc1_hpf,
278 			    WM8994_AIF1_ADC1_FILTERS, 13, adc_hpf_text);
279 
280 static SOC_ENUM_SINGLE_DECL(aif1adc2_hpf,
281 			    WM8994_AIF1_ADC2_FILTERS, 13, adc_hpf_text);
282 
283 static SOC_ENUM_SINGLE_DECL(aif2adc_hpf,
284 			    WM8994_AIF2_ADC_FILTERS, 13, adc_hpf_text);
285 
286 static const DECLARE_TLV_DB_SCALE(aif_tlv, 0, 600, 0);
287 static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
288 static const DECLARE_TLV_DB_SCALE(st_tlv, -3600, 300, 0);
289 static const DECLARE_TLV_DB_SCALE(wm8994_3d_tlv, -1600, 183, 0);
290 static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
291 static const DECLARE_TLV_DB_SCALE(ng_tlv, -10200, 600, 0);
292 static const DECLARE_TLV_DB_SCALE(mixin_boost_tlv, 0, 900, 0);
293 
294 #define WM8994_DRC_SWITCH(xname, reg, shift) \
295 	SOC_SINGLE_EXT(xname, reg, shift, 1, 0, \
296 		snd_soc_get_volsw, wm8994_put_drc_sw)
297 
298 static int wm8994_put_drc_sw(struct snd_kcontrol *kcontrol,
299 			     struct snd_ctl_elem_value *ucontrol)
300 {
301 	struct soc_mixer_control *mc =
302 		(struct soc_mixer_control *)kcontrol->private_value;
303 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
304 	int mask, ret;
305 
306 	/* Can't enable both ADC and DAC paths simultaneously */
307 	if (mc->shift == WM8994_AIF1DAC1_DRC_ENA_SHIFT)
308 		mask = WM8994_AIF1ADC1L_DRC_ENA_MASK |
309 			WM8994_AIF1ADC1R_DRC_ENA_MASK;
310 	else
311 		mask = WM8994_AIF1DAC1_DRC_ENA_MASK;
312 
313 	ret = snd_soc_component_read32(component, mc->reg);
314 	if (ret < 0)
315 		return ret;
316 	if (ret & mask)
317 		return -EINVAL;
318 
319 	return snd_soc_put_volsw(kcontrol, ucontrol);
320 }
321 
322 static void wm8994_set_drc(struct snd_soc_component *component, int drc)
323 {
324 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
325 	struct wm8994 *control = wm8994->wm8994;
326 	struct wm8994_pdata *pdata = &control->pdata;
327 	int base = wm8994_drc_base[drc];
328 	int cfg = wm8994->drc_cfg[drc];
329 	int save, i;
330 
331 	/* Save any enables; the configuration should clear them. */
332 	save = snd_soc_component_read32(component, base);
333 	save &= WM8994_AIF1DAC1_DRC_ENA | WM8994_AIF1ADC1L_DRC_ENA |
334 		WM8994_AIF1ADC1R_DRC_ENA;
335 
336 	for (i = 0; i < WM8994_DRC_REGS; i++)
337 		snd_soc_component_update_bits(component, base + i, 0xffff,
338 				    pdata->drc_cfgs[cfg].regs[i]);
339 
340 	snd_soc_component_update_bits(component, base, WM8994_AIF1DAC1_DRC_ENA |
341 			     WM8994_AIF1ADC1L_DRC_ENA |
342 			     WM8994_AIF1ADC1R_DRC_ENA, save);
343 }
344 
345 /* Icky as hell but saves code duplication */
346 static int wm8994_get_drc(const char *name)
347 {
348 	if (strcmp(name, "AIF1DRC1 Mode") == 0)
349 		return 0;
350 	if (strcmp(name, "AIF1DRC2 Mode") == 0)
351 		return 1;
352 	if (strcmp(name, "AIF2DRC Mode") == 0)
353 		return 2;
354 	return -EINVAL;
355 }
356 
357 static int wm8994_put_drc_enum(struct snd_kcontrol *kcontrol,
358 			       struct snd_ctl_elem_value *ucontrol)
359 {
360 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
361 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
362 	struct wm8994 *control = wm8994->wm8994;
363 	struct wm8994_pdata *pdata = &control->pdata;
364 	int drc = wm8994_get_drc(kcontrol->id.name);
365 	int value = ucontrol->value.enumerated.item[0];
366 
367 	if (drc < 0)
368 		return drc;
369 
370 	if (value >= pdata->num_drc_cfgs)
371 		return -EINVAL;
372 
373 	wm8994->drc_cfg[drc] = value;
374 
375 	wm8994_set_drc(component, drc);
376 
377 	return 0;
378 }
379 
380 static int wm8994_get_drc_enum(struct snd_kcontrol *kcontrol,
381 			       struct snd_ctl_elem_value *ucontrol)
382 {
383 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
384 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
385 	int drc = wm8994_get_drc(kcontrol->id.name);
386 
387 	if (drc < 0)
388 		return drc;
389 	ucontrol->value.enumerated.item[0] = wm8994->drc_cfg[drc];
390 
391 	return 0;
392 }
393 
394 static void wm8994_set_retune_mobile(struct snd_soc_component *component, int block)
395 {
396 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
397 	struct wm8994 *control = wm8994->wm8994;
398 	struct wm8994_pdata *pdata = &control->pdata;
399 	int base = wm8994_retune_mobile_base[block];
400 	int iface, best, best_val, save, i, cfg;
401 
402 	if (!pdata || !wm8994->num_retune_mobile_texts)
403 		return;
404 
405 	switch (block) {
406 	case 0:
407 	case 1:
408 		iface = 0;
409 		break;
410 	case 2:
411 		iface = 1;
412 		break;
413 	default:
414 		return;
415 	}
416 
417 	/* Find the version of the currently selected configuration
418 	 * with the nearest sample rate. */
419 	cfg = wm8994->retune_mobile_cfg[block];
420 	best = 0;
421 	best_val = INT_MAX;
422 	for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
423 		if (strcmp(pdata->retune_mobile_cfgs[i].name,
424 			   wm8994->retune_mobile_texts[cfg]) == 0 &&
425 		    abs(pdata->retune_mobile_cfgs[i].rate
426 			- wm8994->dac_rates[iface]) < best_val) {
427 			best = i;
428 			best_val = abs(pdata->retune_mobile_cfgs[i].rate
429 				       - wm8994->dac_rates[iface]);
430 		}
431 	}
432 
433 	dev_dbg(component->dev, "ReTune Mobile %d %s/%dHz for %dHz sample rate\n",
434 		block,
435 		pdata->retune_mobile_cfgs[best].name,
436 		pdata->retune_mobile_cfgs[best].rate,
437 		wm8994->dac_rates[iface]);
438 
439 	/* The EQ will be disabled while reconfiguring it, remember the
440 	 * current configuration.
441 	 */
442 	save = snd_soc_component_read32(component, base);
443 	save &= WM8994_AIF1DAC1_EQ_ENA;
444 
445 	for (i = 0; i < WM8994_EQ_REGS; i++)
446 		snd_soc_component_update_bits(component, base + i, 0xffff,
447 				pdata->retune_mobile_cfgs[best].regs[i]);
448 
449 	snd_soc_component_update_bits(component, base, WM8994_AIF1DAC1_EQ_ENA, save);
450 }
451 
452 /* Icky as hell but saves code duplication */
453 static int wm8994_get_retune_mobile_block(const char *name)
454 {
455 	if (strcmp(name, "AIF1.1 EQ Mode") == 0)
456 		return 0;
457 	if (strcmp(name, "AIF1.2 EQ Mode") == 0)
458 		return 1;
459 	if (strcmp(name, "AIF2 EQ Mode") == 0)
460 		return 2;
461 	return -EINVAL;
462 }
463 
464 static int wm8994_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
465 					 struct snd_ctl_elem_value *ucontrol)
466 {
467 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
468 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
469 	struct wm8994 *control = wm8994->wm8994;
470 	struct wm8994_pdata *pdata = &control->pdata;
471 	int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
472 	int value = ucontrol->value.enumerated.item[0];
473 
474 	if (block < 0)
475 		return block;
476 
477 	if (value >= pdata->num_retune_mobile_cfgs)
478 		return -EINVAL;
479 
480 	wm8994->retune_mobile_cfg[block] = value;
481 
482 	wm8994_set_retune_mobile(component, block);
483 
484 	return 0;
485 }
486 
487 static int wm8994_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
488 					 struct snd_ctl_elem_value *ucontrol)
489 {
490 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
491 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
492 	int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
493 
494 	if (block < 0)
495 		return block;
496 
497 	ucontrol->value.enumerated.item[0] = wm8994->retune_mobile_cfg[block];
498 
499 	return 0;
500 }
501 
502 static const char *aif_chan_src_text[] = {
503 	"Left", "Right"
504 };
505 
506 static SOC_ENUM_SINGLE_DECL(aif1adcl_src,
507 			    WM8994_AIF1_CONTROL_1, 15, aif_chan_src_text);
508 
509 static SOC_ENUM_SINGLE_DECL(aif1adcr_src,
510 			    WM8994_AIF1_CONTROL_1, 14, aif_chan_src_text);
511 
512 static SOC_ENUM_SINGLE_DECL(aif2adcl_src,
513 			    WM8994_AIF2_CONTROL_1, 15, aif_chan_src_text);
514 
515 static SOC_ENUM_SINGLE_DECL(aif2adcr_src,
516 			    WM8994_AIF2_CONTROL_1, 14, aif_chan_src_text);
517 
518 static SOC_ENUM_SINGLE_DECL(aif1dacl_src,
519 			    WM8994_AIF1_CONTROL_2, 15, aif_chan_src_text);
520 
521 static SOC_ENUM_SINGLE_DECL(aif1dacr_src,
522 			    WM8994_AIF1_CONTROL_2, 14, aif_chan_src_text);
523 
524 static SOC_ENUM_SINGLE_DECL(aif2dacl_src,
525 			    WM8994_AIF2_CONTROL_2, 15, aif_chan_src_text);
526 
527 static SOC_ENUM_SINGLE_DECL(aif2dacr_src,
528 			    WM8994_AIF2_CONTROL_2, 14, aif_chan_src_text);
529 
530 static const char *osr_text[] = {
531 	"Low Power", "High Performance",
532 };
533 
534 static SOC_ENUM_SINGLE_DECL(dac_osr,
535 			    WM8994_OVERSAMPLING, 0, osr_text);
536 
537 static SOC_ENUM_SINGLE_DECL(adc_osr,
538 			    WM8994_OVERSAMPLING, 1, osr_text);
539 
540 static const struct snd_kcontrol_new wm8994_snd_controls[] = {
541 SOC_DOUBLE_R_TLV("AIF1ADC1 Volume", WM8994_AIF1_ADC1_LEFT_VOLUME,
542 		 WM8994_AIF1_ADC1_RIGHT_VOLUME,
543 		 1, 119, 0, digital_tlv),
544 SOC_DOUBLE_R_TLV("AIF1ADC2 Volume", WM8994_AIF1_ADC2_LEFT_VOLUME,
545 		 WM8994_AIF1_ADC2_RIGHT_VOLUME,
546 		 1, 119, 0, digital_tlv),
547 SOC_DOUBLE_R_TLV("AIF2ADC Volume", WM8994_AIF2_ADC_LEFT_VOLUME,
548 		 WM8994_AIF2_ADC_RIGHT_VOLUME,
549 		 1, 119, 0, digital_tlv),
550 
551 SOC_ENUM("AIF1ADCL Source", aif1adcl_src),
552 SOC_ENUM("AIF1ADCR Source", aif1adcr_src),
553 SOC_ENUM("AIF2ADCL Source", aif2adcl_src),
554 SOC_ENUM("AIF2ADCR Source", aif2adcr_src),
555 
556 SOC_ENUM("AIF1DACL Source", aif1dacl_src),
557 SOC_ENUM("AIF1DACR Source", aif1dacr_src),
558 SOC_ENUM("AIF2DACL Source", aif2dacl_src),
559 SOC_ENUM("AIF2DACR Source", aif2dacr_src),
560 
561 SOC_DOUBLE_R_TLV("AIF1DAC1 Volume", WM8994_AIF1_DAC1_LEFT_VOLUME,
562 		 WM8994_AIF1_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
563 SOC_DOUBLE_R_TLV("AIF1DAC2 Volume", WM8994_AIF1_DAC2_LEFT_VOLUME,
564 		 WM8994_AIF1_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
565 SOC_DOUBLE_R_TLV("AIF2DAC Volume", WM8994_AIF2_DAC_LEFT_VOLUME,
566 		 WM8994_AIF2_DAC_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
567 
568 SOC_SINGLE_TLV("AIF1 Boost Volume", WM8994_AIF1_CONTROL_2, 10, 3, 0, aif_tlv),
569 SOC_SINGLE_TLV("AIF2 Boost Volume", WM8994_AIF2_CONTROL_2, 10, 3, 0, aif_tlv),
570 
571 SOC_SINGLE("AIF1DAC1 EQ Switch", WM8994_AIF1_DAC1_EQ_GAINS_1, 0, 1, 0),
572 SOC_SINGLE("AIF1DAC2 EQ Switch", WM8994_AIF1_DAC2_EQ_GAINS_1, 0, 1, 0),
573 SOC_SINGLE("AIF2 EQ Switch", WM8994_AIF2_EQ_GAINS_1, 0, 1, 0),
574 
575 WM8994_DRC_SWITCH("AIF1DAC1 DRC Switch", WM8994_AIF1_DRC1_1, 2),
576 WM8994_DRC_SWITCH("AIF1ADC1L DRC Switch", WM8994_AIF1_DRC1_1, 1),
577 WM8994_DRC_SWITCH("AIF1ADC1R DRC Switch", WM8994_AIF1_DRC1_1, 0),
578 
579 WM8994_DRC_SWITCH("AIF1DAC2 DRC Switch", WM8994_AIF1_DRC2_1, 2),
580 WM8994_DRC_SWITCH("AIF1ADC2L DRC Switch", WM8994_AIF1_DRC2_1, 1),
581 WM8994_DRC_SWITCH("AIF1ADC2R DRC Switch", WM8994_AIF1_DRC2_1, 0),
582 
583 WM8994_DRC_SWITCH("AIF2DAC DRC Switch", WM8994_AIF2_DRC_1, 2),
584 WM8994_DRC_SWITCH("AIF2ADCL DRC Switch", WM8994_AIF2_DRC_1, 1),
585 WM8994_DRC_SWITCH("AIF2ADCR DRC Switch", WM8994_AIF2_DRC_1, 0),
586 
587 SOC_SINGLE_TLV("DAC1 Right Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
588 	       5, 12, 0, st_tlv),
589 SOC_SINGLE_TLV("DAC1 Left Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
590 	       0, 12, 0, st_tlv),
591 SOC_SINGLE_TLV("DAC2 Right Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
592 	       5, 12, 0, st_tlv),
593 SOC_SINGLE_TLV("DAC2 Left Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
594 	       0, 12, 0, st_tlv),
595 SOC_ENUM("Sidetone HPF Mux", sidetone_hpf),
596 SOC_SINGLE("Sidetone HPF Switch", WM8994_SIDETONE, 6, 1, 0),
597 
598 SOC_ENUM("AIF1ADC1 HPF Mode", aif1adc1_hpf),
599 SOC_DOUBLE("AIF1ADC1 HPF Switch", WM8994_AIF1_ADC1_FILTERS, 12, 11, 1, 0),
600 
601 SOC_ENUM("AIF1ADC2 HPF Mode", aif1adc2_hpf),
602 SOC_DOUBLE("AIF1ADC2 HPF Switch", WM8994_AIF1_ADC2_FILTERS, 12, 11, 1, 0),
603 
604 SOC_ENUM("AIF2ADC HPF Mode", aif2adc_hpf),
605 SOC_DOUBLE("AIF2ADC HPF Switch", WM8994_AIF2_ADC_FILTERS, 12, 11, 1, 0),
606 
607 SOC_ENUM("ADC OSR", adc_osr),
608 SOC_ENUM("DAC OSR", dac_osr),
609 
610 SOC_DOUBLE_R_TLV("DAC1 Volume", WM8994_DAC1_LEFT_VOLUME,
611 		 WM8994_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
612 SOC_DOUBLE_R("DAC1 Switch", WM8994_DAC1_LEFT_VOLUME,
613 	     WM8994_DAC1_RIGHT_VOLUME, 9, 1, 1),
614 
615 SOC_DOUBLE_R_TLV("DAC2 Volume", WM8994_DAC2_LEFT_VOLUME,
616 		 WM8994_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
617 SOC_DOUBLE_R("DAC2 Switch", WM8994_DAC2_LEFT_VOLUME,
618 	     WM8994_DAC2_RIGHT_VOLUME, 9, 1, 1),
619 
620 SOC_SINGLE_TLV("SPKL DAC2 Volume", WM8994_SPKMIXL_ATTENUATION,
621 	       6, 1, 1, wm_hubs_spkmix_tlv),
622 SOC_SINGLE_TLV("SPKL DAC1 Volume", WM8994_SPKMIXL_ATTENUATION,
623 	       2, 1, 1, wm_hubs_spkmix_tlv),
624 
625 SOC_SINGLE_TLV("SPKR DAC2 Volume", WM8994_SPKMIXR_ATTENUATION,
626 	       6, 1, 1, wm_hubs_spkmix_tlv),
627 SOC_SINGLE_TLV("SPKR DAC1 Volume", WM8994_SPKMIXR_ATTENUATION,
628 	       2, 1, 1, wm_hubs_spkmix_tlv),
629 
630 SOC_SINGLE_TLV("AIF1DAC1 3D Stereo Volume", WM8994_AIF1_DAC1_FILTERS_2,
631 	       10, 15, 0, wm8994_3d_tlv),
632 SOC_SINGLE("AIF1DAC1 3D Stereo Switch", WM8994_AIF1_DAC1_FILTERS_2,
633 	   8, 1, 0),
634 SOC_SINGLE_TLV("AIF1DAC2 3D Stereo Volume", WM8994_AIF1_DAC2_FILTERS_2,
635 	       10, 15, 0, wm8994_3d_tlv),
636 SOC_SINGLE("AIF1DAC2 3D Stereo Switch", WM8994_AIF1_DAC2_FILTERS_2,
637 	   8, 1, 0),
638 SOC_SINGLE_TLV("AIF2DAC 3D Stereo Volume", WM8994_AIF2_DAC_FILTERS_2,
639 	       10, 15, 0, wm8994_3d_tlv),
640 SOC_SINGLE("AIF2DAC 3D Stereo Switch", WM8994_AIF2_DAC_FILTERS_2,
641 	   8, 1, 0),
642 };
643 
644 static const struct snd_kcontrol_new wm8994_eq_controls[] = {
645 SOC_SINGLE_TLV("AIF1DAC1 EQ1 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 11, 31, 0,
646 	       eq_tlv),
647 SOC_SINGLE_TLV("AIF1DAC1 EQ2 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 6, 31, 0,
648 	       eq_tlv),
649 SOC_SINGLE_TLV("AIF1DAC1 EQ3 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 1, 31, 0,
650 	       eq_tlv),
651 SOC_SINGLE_TLV("AIF1DAC1 EQ4 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 11, 31, 0,
652 	       eq_tlv),
653 SOC_SINGLE_TLV("AIF1DAC1 EQ5 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 6, 31, 0,
654 	       eq_tlv),
655 
656 SOC_SINGLE_TLV("AIF1DAC2 EQ1 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 11, 31, 0,
657 	       eq_tlv),
658 SOC_SINGLE_TLV("AIF1DAC2 EQ2 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 6, 31, 0,
659 	       eq_tlv),
660 SOC_SINGLE_TLV("AIF1DAC2 EQ3 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 1, 31, 0,
661 	       eq_tlv),
662 SOC_SINGLE_TLV("AIF1DAC2 EQ4 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 11, 31, 0,
663 	       eq_tlv),
664 SOC_SINGLE_TLV("AIF1DAC2 EQ5 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 6, 31, 0,
665 	       eq_tlv),
666 
667 SOC_SINGLE_TLV("AIF2 EQ1 Volume", WM8994_AIF2_EQ_GAINS_1, 11, 31, 0,
668 	       eq_tlv),
669 SOC_SINGLE_TLV("AIF2 EQ2 Volume", WM8994_AIF2_EQ_GAINS_1, 6, 31, 0,
670 	       eq_tlv),
671 SOC_SINGLE_TLV("AIF2 EQ3 Volume", WM8994_AIF2_EQ_GAINS_1, 1, 31, 0,
672 	       eq_tlv),
673 SOC_SINGLE_TLV("AIF2 EQ4 Volume", WM8994_AIF2_EQ_GAINS_2, 11, 31, 0,
674 	       eq_tlv),
675 SOC_SINGLE_TLV("AIF2 EQ5 Volume", WM8994_AIF2_EQ_GAINS_2, 6, 31, 0,
676 	       eq_tlv),
677 };
678 
679 static const struct snd_kcontrol_new wm8994_drc_controls[] = {
680 SND_SOC_BYTES_MASK("AIF1.1 DRC", WM8994_AIF1_DRC1_1, 5,
681 		   WM8994_AIF1DAC1_DRC_ENA | WM8994_AIF1ADC1L_DRC_ENA |
682 		   WM8994_AIF1ADC1R_DRC_ENA),
683 SND_SOC_BYTES_MASK("AIF1.2 DRC", WM8994_AIF1_DRC2_1, 5,
684 		   WM8994_AIF1DAC2_DRC_ENA | WM8994_AIF1ADC2L_DRC_ENA |
685 		   WM8994_AIF1ADC2R_DRC_ENA),
686 SND_SOC_BYTES_MASK("AIF2 DRC", WM8994_AIF2_DRC_1, 5,
687 		   WM8994_AIF2DAC_DRC_ENA | WM8994_AIF2ADCL_DRC_ENA |
688 		   WM8994_AIF2ADCR_DRC_ENA),
689 };
690 
691 static const char *wm8958_ng_text[] = {
692 	"30ms", "125ms", "250ms", "500ms",
693 };
694 
695 static SOC_ENUM_SINGLE_DECL(wm8958_aif1dac1_ng_hold,
696 			    WM8958_AIF1_DAC1_NOISE_GATE,
697 			    WM8958_AIF1DAC1_NG_THR_SHIFT,
698 			    wm8958_ng_text);
699 
700 static SOC_ENUM_SINGLE_DECL(wm8958_aif1dac2_ng_hold,
701 			    WM8958_AIF1_DAC2_NOISE_GATE,
702 			    WM8958_AIF1DAC2_NG_THR_SHIFT,
703 			    wm8958_ng_text);
704 
705 static SOC_ENUM_SINGLE_DECL(wm8958_aif2dac_ng_hold,
706 			    WM8958_AIF2_DAC_NOISE_GATE,
707 			    WM8958_AIF2DAC_NG_THR_SHIFT,
708 			    wm8958_ng_text);
709 
710 static const struct snd_kcontrol_new wm8958_snd_controls[] = {
711 SOC_SINGLE_TLV("AIF3 Boost Volume", WM8958_AIF3_CONTROL_2, 10, 3, 0, aif_tlv),
712 
713 SOC_SINGLE("AIF1DAC1 Noise Gate Switch", WM8958_AIF1_DAC1_NOISE_GATE,
714 	   WM8958_AIF1DAC1_NG_ENA_SHIFT, 1, 0),
715 SOC_ENUM("AIF1DAC1 Noise Gate Hold Time", wm8958_aif1dac1_ng_hold),
716 SOC_SINGLE_TLV("AIF1DAC1 Noise Gate Threshold Volume",
717 	       WM8958_AIF1_DAC1_NOISE_GATE, WM8958_AIF1DAC1_NG_THR_SHIFT,
718 	       7, 1, ng_tlv),
719 
720 SOC_SINGLE("AIF1DAC2 Noise Gate Switch", WM8958_AIF1_DAC2_NOISE_GATE,
721 	   WM8958_AIF1DAC2_NG_ENA_SHIFT, 1, 0),
722 SOC_ENUM("AIF1DAC2 Noise Gate Hold Time", wm8958_aif1dac2_ng_hold),
723 SOC_SINGLE_TLV("AIF1DAC2 Noise Gate Threshold Volume",
724 	       WM8958_AIF1_DAC2_NOISE_GATE, WM8958_AIF1DAC2_NG_THR_SHIFT,
725 	       7, 1, ng_tlv),
726 
727 SOC_SINGLE("AIF2DAC Noise Gate Switch", WM8958_AIF2_DAC_NOISE_GATE,
728 	   WM8958_AIF2DAC_NG_ENA_SHIFT, 1, 0),
729 SOC_ENUM("AIF2DAC Noise Gate Hold Time", wm8958_aif2dac_ng_hold),
730 SOC_SINGLE_TLV("AIF2DAC Noise Gate Threshold Volume",
731 	       WM8958_AIF2_DAC_NOISE_GATE, WM8958_AIF2DAC_NG_THR_SHIFT,
732 	       7, 1, ng_tlv),
733 };
734 
735 static const struct snd_kcontrol_new wm1811_snd_controls[] = {
736 SOC_SINGLE_TLV("MIXINL IN1LP Boost Volume", WM8994_INPUT_MIXER_1, 7, 1, 0,
737 	       mixin_boost_tlv),
738 SOC_SINGLE_TLV("MIXINL IN1RP Boost Volume", WM8994_INPUT_MIXER_1, 8, 1, 0,
739 	       mixin_boost_tlv),
740 };
741 
742 /* We run all mode setting through a function to enforce audio mode */
743 static void wm1811_jackdet_set_mode(struct snd_soc_component *component, u16 mode)
744 {
745 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
746 
747 	if (!wm8994->jackdet || !wm8994->micdet[0].jack)
748 		return;
749 
750 	if (wm8994->active_refcount)
751 		mode = WM1811_JACKDET_MODE_AUDIO;
752 
753 	if (mode == wm8994->jackdet_mode)
754 		return;
755 
756 	wm8994->jackdet_mode = mode;
757 
758 	/* Always use audio mode to detect while the system is active */
759 	if (mode != WM1811_JACKDET_MODE_NONE)
760 		mode = WM1811_JACKDET_MODE_AUDIO;
761 
762 	snd_soc_component_update_bits(component, WM8994_ANTIPOP_2,
763 			    WM1811_JACKDET_MODE_MASK, mode);
764 }
765 
766 static void active_reference(struct snd_soc_component *component)
767 {
768 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
769 
770 	mutex_lock(&wm8994->accdet_lock);
771 
772 	wm8994->active_refcount++;
773 
774 	dev_dbg(component->dev, "Active refcount incremented, now %d\n",
775 		wm8994->active_refcount);
776 
777 	/* If we're using jack detection go into audio mode */
778 	wm1811_jackdet_set_mode(component, WM1811_JACKDET_MODE_AUDIO);
779 
780 	mutex_unlock(&wm8994->accdet_lock);
781 }
782 
783 static void active_dereference(struct snd_soc_component *component)
784 {
785 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
786 	u16 mode;
787 
788 	mutex_lock(&wm8994->accdet_lock);
789 
790 	wm8994->active_refcount--;
791 
792 	dev_dbg(component->dev, "Active refcount decremented, now %d\n",
793 		wm8994->active_refcount);
794 
795 	if (wm8994->active_refcount == 0) {
796 		/* Go into appropriate detection only mode */
797 		if (wm8994->jack_mic || wm8994->mic_detecting)
798 			mode = WM1811_JACKDET_MODE_MIC;
799 		else
800 			mode = WM1811_JACKDET_MODE_JACK;
801 
802 		wm1811_jackdet_set_mode(component, mode);
803 	}
804 
805 	mutex_unlock(&wm8994->accdet_lock);
806 }
807 
808 static int clk_sys_event(struct snd_soc_dapm_widget *w,
809 			 struct snd_kcontrol *kcontrol, int event)
810 {
811 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
812 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
813 
814 	switch (event) {
815 	case SND_SOC_DAPM_PRE_PMU:
816 		return configure_clock(component);
817 
818 	case SND_SOC_DAPM_POST_PMU:
819 		/*
820 		 * JACKDET won't run until we start the clock and it
821 		 * only reports deltas, make sure we notify the state
822 		 * up the stack on startup.  Use a *very* generous
823 		 * timeout for paranoia, there's no urgency and we
824 		 * don't want false reports.
825 		 */
826 		if (wm8994->jackdet && !wm8994->clk_has_run) {
827 			queue_delayed_work(system_power_efficient_wq,
828 					   &wm8994->jackdet_bootstrap,
829 					   msecs_to_jiffies(1000));
830 			wm8994->clk_has_run = true;
831 		}
832 		break;
833 
834 	case SND_SOC_DAPM_POST_PMD:
835 		configure_clock(component);
836 		break;
837 	}
838 
839 	return 0;
840 }
841 
842 static void vmid_reference(struct snd_soc_component *component)
843 {
844 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
845 
846 	pm_runtime_get_sync(component->dev);
847 
848 	wm8994->vmid_refcount++;
849 
850 	dev_dbg(component->dev, "Referencing VMID, refcount is now %d\n",
851 		wm8994->vmid_refcount);
852 
853 	if (wm8994->vmid_refcount == 1) {
854 		snd_soc_component_update_bits(component, WM8994_ANTIPOP_1,
855 				    WM8994_LINEOUT1_DISCH |
856 				    WM8994_LINEOUT2_DISCH, 0);
857 
858 		wm_hubs_vmid_ena(component);
859 
860 		switch (wm8994->vmid_mode) {
861 		default:
862 			WARN_ON(NULL == "Invalid VMID mode");
863 			/* fall through */
864 		case WM8994_VMID_NORMAL:
865 			/* Startup bias, VMID ramp & buffer */
866 			snd_soc_component_update_bits(component, WM8994_ANTIPOP_2,
867 					    WM8994_BIAS_SRC |
868 					    WM8994_VMID_DISCH |
869 					    WM8994_STARTUP_BIAS_ENA |
870 					    WM8994_VMID_BUF_ENA |
871 					    WM8994_VMID_RAMP_MASK,
872 					    WM8994_BIAS_SRC |
873 					    WM8994_STARTUP_BIAS_ENA |
874 					    WM8994_VMID_BUF_ENA |
875 					    (0x2 << WM8994_VMID_RAMP_SHIFT));
876 
877 			/* Main bias enable, VMID=2x40k */
878 			snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_1,
879 					    WM8994_BIAS_ENA |
880 					    WM8994_VMID_SEL_MASK,
881 					    WM8994_BIAS_ENA | 0x2);
882 
883 			msleep(300);
884 
885 			snd_soc_component_update_bits(component, WM8994_ANTIPOP_2,
886 					    WM8994_VMID_RAMP_MASK |
887 					    WM8994_BIAS_SRC,
888 					    0);
889 			break;
890 
891 		case WM8994_VMID_FORCE:
892 			/* Startup bias, slow VMID ramp & buffer */
893 			snd_soc_component_update_bits(component, WM8994_ANTIPOP_2,
894 					    WM8994_BIAS_SRC |
895 					    WM8994_VMID_DISCH |
896 					    WM8994_STARTUP_BIAS_ENA |
897 					    WM8994_VMID_BUF_ENA |
898 					    WM8994_VMID_RAMP_MASK,
899 					    WM8994_BIAS_SRC |
900 					    WM8994_STARTUP_BIAS_ENA |
901 					    WM8994_VMID_BUF_ENA |
902 					    (0x2 << WM8994_VMID_RAMP_SHIFT));
903 
904 			/* Main bias enable, VMID=2x40k */
905 			snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_1,
906 					    WM8994_BIAS_ENA |
907 					    WM8994_VMID_SEL_MASK,
908 					    WM8994_BIAS_ENA | 0x2);
909 
910 			msleep(400);
911 
912 			snd_soc_component_update_bits(component, WM8994_ANTIPOP_2,
913 					    WM8994_VMID_RAMP_MASK |
914 					    WM8994_BIAS_SRC,
915 					    0);
916 			break;
917 		}
918 	}
919 }
920 
921 static void vmid_dereference(struct snd_soc_component *component)
922 {
923 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
924 
925 	wm8994->vmid_refcount--;
926 
927 	dev_dbg(component->dev, "Dereferencing VMID, refcount is now %d\n",
928 		wm8994->vmid_refcount);
929 
930 	if (wm8994->vmid_refcount == 0) {
931 		if (wm8994->hubs.lineout1_se)
932 			snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_3,
933 					    WM8994_LINEOUT1N_ENA |
934 					    WM8994_LINEOUT1P_ENA,
935 					    WM8994_LINEOUT1N_ENA |
936 					    WM8994_LINEOUT1P_ENA);
937 
938 		if (wm8994->hubs.lineout2_se)
939 			snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_3,
940 					    WM8994_LINEOUT2N_ENA |
941 					    WM8994_LINEOUT2P_ENA,
942 					    WM8994_LINEOUT2N_ENA |
943 					    WM8994_LINEOUT2P_ENA);
944 
945 		/* Start discharging VMID */
946 		snd_soc_component_update_bits(component, WM8994_ANTIPOP_2,
947 				    WM8994_BIAS_SRC |
948 				    WM8994_VMID_DISCH,
949 				    WM8994_BIAS_SRC |
950 				    WM8994_VMID_DISCH);
951 
952 		snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_1,
953 				    WM8994_VMID_SEL_MASK, 0);
954 
955 		msleep(400);
956 
957 		/* Active discharge */
958 		snd_soc_component_update_bits(component, WM8994_ANTIPOP_1,
959 				    WM8994_LINEOUT1_DISCH |
960 				    WM8994_LINEOUT2_DISCH,
961 				    WM8994_LINEOUT1_DISCH |
962 				    WM8994_LINEOUT2_DISCH);
963 
964 		snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_3,
965 				    WM8994_LINEOUT1N_ENA |
966 				    WM8994_LINEOUT1P_ENA |
967 				    WM8994_LINEOUT2N_ENA |
968 				    WM8994_LINEOUT2P_ENA, 0);
969 
970 		/* Switch off startup biases */
971 		snd_soc_component_update_bits(component, WM8994_ANTIPOP_2,
972 				    WM8994_BIAS_SRC |
973 				    WM8994_STARTUP_BIAS_ENA |
974 				    WM8994_VMID_BUF_ENA |
975 				    WM8994_VMID_RAMP_MASK, 0);
976 
977 		snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_1,
978 				    WM8994_VMID_SEL_MASK, 0);
979 	}
980 
981 	pm_runtime_put(component->dev);
982 }
983 
984 static int vmid_event(struct snd_soc_dapm_widget *w,
985 		      struct snd_kcontrol *kcontrol, int event)
986 {
987 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
988 
989 	switch (event) {
990 	case SND_SOC_DAPM_PRE_PMU:
991 		vmid_reference(component);
992 		break;
993 
994 	case SND_SOC_DAPM_POST_PMD:
995 		vmid_dereference(component);
996 		break;
997 	}
998 
999 	return 0;
1000 }
1001 
1002 static bool wm8994_check_class_w_digital(struct snd_soc_component *component)
1003 {
1004 	int source = 0;  /* GCC flow analysis can't track enable */
1005 	int reg, reg_r;
1006 
1007 	/* We also need the same AIF source for L/R and only one path */
1008 	reg = snd_soc_component_read32(component, WM8994_DAC1_LEFT_MIXER_ROUTING);
1009 	switch (reg) {
1010 	case WM8994_AIF2DACL_TO_DAC1L:
1011 		dev_vdbg(component->dev, "Class W source AIF2DAC\n");
1012 		source = 2 << WM8994_CP_DYN_SRC_SEL_SHIFT;
1013 		break;
1014 	case WM8994_AIF1DAC2L_TO_DAC1L:
1015 		dev_vdbg(component->dev, "Class W source AIF1DAC2\n");
1016 		source = 1 << WM8994_CP_DYN_SRC_SEL_SHIFT;
1017 		break;
1018 	case WM8994_AIF1DAC1L_TO_DAC1L:
1019 		dev_vdbg(component->dev, "Class W source AIF1DAC1\n");
1020 		source = 0 << WM8994_CP_DYN_SRC_SEL_SHIFT;
1021 		break;
1022 	default:
1023 		dev_vdbg(component->dev, "DAC mixer setting: %x\n", reg);
1024 		return false;
1025 	}
1026 
1027 	reg_r = snd_soc_component_read32(component, WM8994_DAC1_RIGHT_MIXER_ROUTING);
1028 	if (reg_r != reg) {
1029 		dev_vdbg(component->dev, "Left and right DAC mixers different\n");
1030 		return false;
1031 	}
1032 
1033 	/* Set the source up */
1034 	snd_soc_component_update_bits(component, WM8994_CLASS_W_1,
1035 			    WM8994_CP_DYN_SRC_SEL_MASK, source);
1036 
1037 	return true;
1038 }
1039 
1040 static int aif1clk_ev(struct snd_soc_dapm_widget *w,
1041 		      struct snd_kcontrol *kcontrol, int event)
1042 {
1043 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1044 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
1045 	struct wm8994 *control = wm8994->wm8994;
1046 	int mask = WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC1R_ENA;
1047 	int i;
1048 	int dac;
1049 	int adc;
1050 	int val;
1051 
1052 	switch (control->type) {
1053 	case WM8994:
1054 	case WM8958:
1055 		mask |= WM8994_AIF1DAC2L_ENA | WM8994_AIF1DAC2R_ENA;
1056 		break;
1057 	default:
1058 		break;
1059 	}
1060 
1061 	switch (event) {
1062 	case SND_SOC_DAPM_PRE_PMU:
1063 		/* Don't enable timeslot 2 if not in use */
1064 		if (wm8994->channels[0] <= 2)
1065 			mask &= ~(WM8994_AIF1DAC2L_ENA | WM8994_AIF1DAC2R_ENA);
1066 
1067 		val = snd_soc_component_read32(component, WM8994_AIF1_CONTROL_1);
1068 		if ((val & WM8994_AIF1ADCL_SRC) &&
1069 		    (val & WM8994_AIF1ADCR_SRC))
1070 			adc = WM8994_AIF1ADC1R_ENA | WM8994_AIF1ADC2R_ENA;
1071 		else if (!(val & WM8994_AIF1ADCL_SRC) &&
1072 			 !(val & WM8994_AIF1ADCR_SRC))
1073 			adc = WM8994_AIF1ADC1L_ENA | WM8994_AIF1ADC2L_ENA;
1074 		else
1075 			adc = WM8994_AIF1ADC1R_ENA | WM8994_AIF1ADC2R_ENA |
1076 				WM8994_AIF1ADC1L_ENA | WM8994_AIF1ADC2L_ENA;
1077 
1078 		val = snd_soc_component_read32(component, WM8994_AIF1_CONTROL_2);
1079 		if ((val & WM8994_AIF1DACL_SRC) &&
1080 		    (val & WM8994_AIF1DACR_SRC))
1081 			dac = WM8994_AIF1DAC1R_ENA | WM8994_AIF1DAC2R_ENA;
1082 		else if (!(val & WM8994_AIF1DACL_SRC) &&
1083 			 !(val & WM8994_AIF1DACR_SRC))
1084 			dac = WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC2L_ENA;
1085 		else
1086 			dac = WM8994_AIF1DAC1R_ENA | WM8994_AIF1DAC2R_ENA |
1087 				WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC2L_ENA;
1088 
1089 		snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_4,
1090 				    mask, adc);
1091 		snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_5,
1092 				    mask, dac);
1093 		snd_soc_component_update_bits(component, WM8994_CLOCKING_1,
1094 				    WM8994_AIF1DSPCLK_ENA |
1095 				    WM8994_SYSDSPCLK_ENA,
1096 				    WM8994_AIF1DSPCLK_ENA |
1097 				    WM8994_SYSDSPCLK_ENA);
1098 		snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_4, mask,
1099 				    WM8994_AIF1ADC1R_ENA |
1100 				    WM8994_AIF1ADC1L_ENA |
1101 				    WM8994_AIF1ADC2R_ENA |
1102 				    WM8994_AIF1ADC2L_ENA);
1103 		snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_5, mask,
1104 				    WM8994_AIF1DAC1R_ENA |
1105 				    WM8994_AIF1DAC1L_ENA |
1106 				    WM8994_AIF1DAC2R_ENA |
1107 				    WM8994_AIF1DAC2L_ENA);
1108 		break;
1109 
1110 	case SND_SOC_DAPM_POST_PMU:
1111 		for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
1112 			snd_soc_component_write(component, wm8994_vu_bits[i].reg,
1113 				      snd_soc_component_read32(component,
1114 						   wm8994_vu_bits[i].reg));
1115 		break;
1116 
1117 	case SND_SOC_DAPM_PRE_PMD:
1118 	case SND_SOC_DAPM_POST_PMD:
1119 		snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_5,
1120 				    mask, 0);
1121 		snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_4,
1122 				    mask, 0);
1123 
1124 		val = snd_soc_component_read32(component, WM8994_CLOCKING_1);
1125 		if (val & WM8994_AIF2DSPCLK_ENA)
1126 			val = WM8994_SYSDSPCLK_ENA;
1127 		else
1128 			val = 0;
1129 		snd_soc_component_update_bits(component, WM8994_CLOCKING_1,
1130 				    WM8994_SYSDSPCLK_ENA |
1131 				    WM8994_AIF1DSPCLK_ENA, val);
1132 		break;
1133 	}
1134 
1135 	return 0;
1136 }
1137 
1138 static int aif2clk_ev(struct snd_soc_dapm_widget *w,
1139 		      struct snd_kcontrol *kcontrol, int event)
1140 {
1141 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1142 	int i;
1143 	int dac;
1144 	int adc;
1145 	int val;
1146 
1147 	switch (event) {
1148 	case SND_SOC_DAPM_PRE_PMU:
1149 		val = snd_soc_component_read32(component, WM8994_AIF2_CONTROL_1);
1150 		if ((val & WM8994_AIF2ADCL_SRC) &&
1151 		    (val & WM8994_AIF2ADCR_SRC))
1152 			adc = WM8994_AIF2ADCR_ENA;
1153 		else if (!(val & WM8994_AIF2ADCL_SRC) &&
1154 			 !(val & WM8994_AIF2ADCR_SRC))
1155 			adc = WM8994_AIF2ADCL_ENA;
1156 		else
1157 			adc = WM8994_AIF2ADCL_ENA | WM8994_AIF2ADCR_ENA;
1158 
1159 
1160 		val = snd_soc_component_read32(component, WM8994_AIF2_CONTROL_2);
1161 		if ((val & WM8994_AIF2DACL_SRC) &&
1162 		    (val & WM8994_AIF2DACR_SRC))
1163 			dac = WM8994_AIF2DACR_ENA;
1164 		else if (!(val & WM8994_AIF2DACL_SRC) &&
1165 			 !(val & WM8994_AIF2DACR_SRC))
1166 			dac = WM8994_AIF2DACL_ENA;
1167 		else
1168 			dac = WM8994_AIF2DACL_ENA | WM8994_AIF2DACR_ENA;
1169 
1170 		snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_4,
1171 				    WM8994_AIF2ADCL_ENA |
1172 				    WM8994_AIF2ADCR_ENA, adc);
1173 		snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_5,
1174 				    WM8994_AIF2DACL_ENA |
1175 				    WM8994_AIF2DACR_ENA, dac);
1176 		snd_soc_component_update_bits(component, WM8994_CLOCKING_1,
1177 				    WM8994_AIF2DSPCLK_ENA |
1178 				    WM8994_SYSDSPCLK_ENA,
1179 				    WM8994_AIF2DSPCLK_ENA |
1180 				    WM8994_SYSDSPCLK_ENA);
1181 		snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_4,
1182 				    WM8994_AIF2ADCL_ENA |
1183 				    WM8994_AIF2ADCR_ENA,
1184 				    WM8994_AIF2ADCL_ENA |
1185 				    WM8994_AIF2ADCR_ENA);
1186 		snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_5,
1187 				    WM8994_AIF2DACL_ENA |
1188 				    WM8994_AIF2DACR_ENA,
1189 				    WM8994_AIF2DACL_ENA |
1190 				    WM8994_AIF2DACR_ENA);
1191 		break;
1192 
1193 	case SND_SOC_DAPM_POST_PMU:
1194 		for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
1195 			snd_soc_component_write(component, wm8994_vu_bits[i].reg,
1196 				      snd_soc_component_read32(component,
1197 						   wm8994_vu_bits[i].reg));
1198 		break;
1199 
1200 	case SND_SOC_DAPM_PRE_PMD:
1201 	case SND_SOC_DAPM_POST_PMD:
1202 		snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_5,
1203 				    WM8994_AIF2DACL_ENA |
1204 				    WM8994_AIF2DACR_ENA, 0);
1205 		snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_4,
1206 				    WM8994_AIF2ADCL_ENA |
1207 				    WM8994_AIF2ADCR_ENA, 0);
1208 
1209 		val = snd_soc_component_read32(component, WM8994_CLOCKING_1);
1210 		if (val & WM8994_AIF1DSPCLK_ENA)
1211 			val = WM8994_SYSDSPCLK_ENA;
1212 		else
1213 			val = 0;
1214 		snd_soc_component_update_bits(component, WM8994_CLOCKING_1,
1215 				    WM8994_SYSDSPCLK_ENA |
1216 				    WM8994_AIF2DSPCLK_ENA, val);
1217 		break;
1218 	}
1219 
1220 	return 0;
1221 }
1222 
1223 static int aif1clk_late_ev(struct snd_soc_dapm_widget *w,
1224 			   struct snd_kcontrol *kcontrol, int event)
1225 {
1226 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1227 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
1228 
1229 	switch (event) {
1230 	case SND_SOC_DAPM_PRE_PMU:
1231 		wm8994->aif1clk_enable = 1;
1232 		break;
1233 	case SND_SOC_DAPM_POST_PMD:
1234 		wm8994->aif1clk_disable = 1;
1235 		break;
1236 	}
1237 
1238 	return 0;
1239 }
1240 
1241 static int aif2clk_late_ev(struct snd_soc_dapm_widget *w,
1242 			   struct snd_kcontrol *kcontrol, int event)
1243 {
1244 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1245 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
1246 
1247 	switch (event) {
1248 	case SND_SOC_DAPM_PRE_PMU:
1249 		wm8994->aif2clk_enable = 1;
1250 		break;
1251 	case SND_SOC_DAPM_POST_PMD:
1252 		wm8994->aif2clk_disable = 1;
1253 		break;
1254 	}
1255 
1256 	return 0;
1257 }
1258 
1259 static int late_enable_ev(struct snd_soc_dapm_widget *w,
1260 			  struct snd_kcontrol *kcontrol, int event)
1261 {
1262 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1263 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
1264 
1265 	switch (event) {
1266 	case SND_SOC_DAPM_PRE_PMU:
1267 		if (wm8994->aif1clk_enable) {
1268 			aif1clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMU);
1269 			snd_soc_component_update_bits(component, WM8994_AIF1_CLOCKING_1,
1270 					    WM8994_AIF1CLK_ENA_MASK,
1271 					    WM8994_AIF1CLK_ENA);
1272 			aif1clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMU);
1273 			wm8994->aif1clk_enable = 0;
1274 		}
1275 		if (wm8994->aif2clk_enable) {
1276 			aif2clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMU);
1277 			snd_soc_component_update_bits(component, WM8994_AIF2_CLOCKING_1,
1278 					    WM8994_AIF2CLK_ENA_MASK,
1279 					    WM8994_AIF2CLK_ENA);
1280 			aif2clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMU);
1281 			wm8994->aif2clk_enable = 0;
1282 		}
1283 		break;
1284 	}
1285 
1286 	/* We may also have postponed startup of DSP, handle that. */
1287 	wm8958_aif_ev(w, kcontrol, event);
1288 
1289 	return 0;
1290 }
1291 
1292 static int late_disable_ev(struct snd_soc_dapm_widget *w,
1293 			   struct snd_kcontrol *kcontrol, int event)
1294 {
1295 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1296 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
1297 
1298 	switch (event) {
1299 	case SND_SOC_DAPM_POST_PMD:
1300 		if (wm8994->aif1clk_disable) {
1301 			aif1clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMD);
1302 			snd_soc_component_update_bits(component, WM8994_AIF1_CLOCKING_1,
1303 					    WM8994_AIF1CLK_ENA_MASK, 0);
1304 			aif1clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMD);
1305 			wm8994->aif1clk_disable = 0;
1306 		}
1307 		if (wm8994->aif2clk_disable) {
1308 			aif2clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMD);
1309 			snd_soc_component_update_bits(component, WM8994_AIF2_CLOCKING_1,
1310 					    WM8994_AIF2CLK_ENA_MASK, 0);
1311 			aif2clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMD);
1312 			wm8994->aif2clk_disable = 0;
1313 		}
1314 		break;
1315 	}
1316 
1317 	return 0;
1318 }
1319 
1320 static int adc_mux_ev(struct snd_soc_dapm_widget *w,
1321 		      struct snd_kcontrol *kcontrol, int event)
1322 {
1323 	late_enable_ev(w, kcontrol, event);
1324 	return 0;
1325 }
1326 
1327 static int micbias_ev(struct snd_soc_dapm_widget *w,
1328 		      struct snd_kcontrol *kcontrol, int event)
1329 {
1330 	late_enable_ev(w, kcontrol, event);
1331 	return 0;
1332 }
1333 
1334 static int dac_ev(struct snd_soc_dapm_widget *w,
1335 		  struct snd_kcontrol *kcontrol, int event)
1336 {
1337 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1338 	unsigned int mask = 1 << w->shift;
1339 
1340 	snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_5,
1341 			    mask, mask);
1342 	return 0;
1343 }
1344 
1345 static const char *adc_mux_text[] = {
1346 	"ADC",
1347 	"DMIC",
1348 };
1349 
1350 static SOC_ENUM_SINGLE_VIRT_DECL(adc_enum, adc_mux_text);
1351 
1352 static const struct snd_kcontrol_new adcl_mux =
1353 	SOC_DAPM_ENUM("ADCL Mux", adc_enum);
1354 
1355 static const struct snd_kcontrol_new adcr_mux =
1356 	SOC_DAPM_ENUM("ADCR Mux", adc_enum);
1357 
1358 static const struct snd_kcontrol_new left_speaker_mixer[] = {
1359 SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 9, 1, 0),
1360 SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 7, 1, 0),
1361 SOC_DAPM_SINGLE("IN1LP Switch", WM8994_SPEAKER_MIXER, 5, 1, 0),
1362 SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 3, 1, 0),
1363 SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 1, 1, 0),
1364 };
1365 
1366 static const struct snd_kcontrol_new right_speaker_mixer[] = {
1367 SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 8, 1, 0),
1368 SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 6, 1, 0),
1369 SOC_DAPM_SINGLE("IN1RP Switch", WM8994_SPEAKER_MIXER, 4, 1, 0),
1370 SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 2, 1, 0),
1371 SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 0, 1, 0),
1372 };
1373 
1374 /* Debugging; dump chip status after DAPM transitions */
1375 static int post_ev(struct snd_soc_dapm_widget *w,
1376 	    struct snd_kcontrol *kcontrol, int event)
1377 {
1378 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1379 	dev_dbg(component->dev, "SRC status: %x\n",
1380 		snd_soc_component_read32(component,
1381 			     WM8994_RATE_STATUS));
1382 	return 0;
1383 }
1384 
1385 static const struct snd_kcontrol_new aif1adc1l_mix[] = {
1386 SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1387 		1, 1, 0),
1388 SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1389 		0, 1, 0),
1390 };
1391 
1392 static const struct snd_kcontrol_new aif1adc1r_mix[] = {
1393 SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1394 		1, 1, 0),
1395 SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1396 		0, 1, 0),
1397 };
1398 
1399 static const struct snd_kcontrol_new aif1adc2l_mix[] = {
1400 SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1401 		1, 1, 0),
1402 SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1403 		0, 1, 0),
1404 };
1405 
1406 static const struct snd_kcontrol_new aif1adc2r_mix[] = {
1407 SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1408 		1, 1, 0),
1409 SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1410 		0, 1, 0),
1411 };
1412 
1413 static const struct snd_kcontrol_new aif2dac2l_mix[] = {
1414 SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1415 		5, 1, 0),
1416 SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1417 		4, 1, 0),
1418 SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1419 		2, 1, 0),
1420 SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1421 		1, 1, 0),
1422 SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1423 		0, 1, 0),
1424 };
1425 
1426 static const struct snd_kcontrol_new aif2dac2r_mix[] = {
1427 SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1428 		5, 1, 0),
1429 SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1430 		4, 1, 0),
1431 SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1432 		2, 1, 0),
1433 SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1434 		1, 1, 0),
1435 SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1436 		0, 1, 0),
1437 };
1438 
1439 #define WM8994_CLASS_W_SWITCH(xname, reg, shift, max, invert) \
1440 	SOC_SINGLE_EXT(xname, reg, shift, max, invert, \
1441 		snd_soc_dapm_get_volsw, wm8994_put_class_w)
1442 
1443 static int wm8994_put_class_w(struct snd_kcontrol *kcontrol,
1444 			      struct snd_ctl_elem_value *ucontrol)
1445 {
1446 	struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
1447 	int ret;
1448 
1449 	ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
1450 
1451 	wm_hubs_update_class_w(component);
1452 
1453 	return ret;
1454 }
1455 
1456 static const struct snd_kcontrol_new dac1l_mix[] = {
1457 WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1458 		      5, 1, 0),
1459 WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1460 		      4, 1, 0),
1461 WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1462 		      2, 1, 0),
1463 WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1464 		      1, 1, 0),
1465 WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1466 		      0, 1, 0),
1467 };
1468 
1469 static const struct snd_kcontrol_new dac1r_mix[] = {
1470 WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1471 		      5, 1, 0),
1472 WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1473 		      4, 1, 0),
1474 WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1475 		      2, 1, 0),
1476 WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1477 		      1, 1, 0),
1478 WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1479 		      0, 1, 0),
1480 };
1481 
1482 static const char *sidetone_text[] = {
1483 	"ADC/DMIC1", "DMIC2",
1484 };
1485 
1486 static SOC_ENUM_SINGLE_DECL(sidetone1_enum,
1487 			    WM8994_SIDETONE, 0, sidetone_text);
1488 
1489 static const struct snd_kcontrol_new sidetone1_mux =
1490 	SOC_DAPM_ENUM("Left Sidetone Mux", sidetone1_enum);
1491 
1492 static SOC_ENUM_SINGLE_DECL(sidetone2_enum,
1493 			    WM8994_SIDETONE, 1, sidetone_text);
1494 
1495 static const struct snd_kcontrol_new sidetone2_mux =
1496 	SOC_DAPM_ENUM("Right Sidetone Mux", sidetone2_enum);
1497 
1498 static const char *aif1dac_text[] = {
1499 	"AIF1DACDAT", "AIF3DACDAT",
1500 };
1501 
1502 static const char *loopback_text[] = {
1503 	"None", "ADCDAT",
1504 };
1505 
1506 static SOC_ENUM_SINGLE_DECL(aif1_loopback_enum,
1507 			    WM8994_AIF1_CONTROL_2,
1508 			    WM8994_AIF1_LOOPBACK_SHIFT,
1509 			    loopback_text);
1510 
1511 static const struct snd_kcontrol_new aif1_loopback =
1512 	SOC_DAPM_ENUM("AIF1 Loopback", aif1_loopback_enum);
1513 
1514 static SOC_ENUM_SINGLE_DECL(aif2_loopback_enum,
1515 			    WM8994_AIF2_CONTROL_2,
1516 			    WM8994_AIF2_LOOPBACK_SHIFT,
1517 			    loopback_text);
1518 
1519 static const struct snd_kcontrol_new aif2_loopback =
1520 	SOC_DAPM_ENUM("AIF2 Loopback", aif2_loopback_enum);
1521 
1522 static SOC_ENUM_SINGLE_DECL(aif1dac_enum,
1523 			    WM8994_POWER_MANAGEMENT_6, 0, aif1dac_text);
1524 
1525 static const struct snd_kcontrol_new aif1dac_mux =
1526 	SOC_DAPM_ENUM("AIF1DAC Mux", aif1dac_enum);
1527 
1528 static const char *aif2dac_text[] = {
1529 	"AIF2DACDAT", "AIF3DACDAT",
1530 };
1531 
1532 static SOC_ENUM_SINGLE_DECL(aif2dac_enum,
1533 			    WM8994_POWER_MANAGEMENT_6, 1, aif2dac_text);
1534 
1535 static const struct snd_kcontrol_new aif2dac_mux =
1536 	SOC_DAPM_ENUM("AIF2DAC Mux", aif2dac_enum);
1537 
1538 static const char *aif2adc_text[] = {
1539 	"AIF2ADCDAT", "AIF3DACDAT",
1540 };
1541 
1542 static SOC_ENUM_SINGLE_DECL(aif2adc_enum,
1543 			    WM8994_POWER_MANAGEMENT_6, 2, aif2adc_text);
1544 
1545 static const struct snd_kcontrol_new aif2adc_mux =
1546 	SOC_DAPM_ENUM("AIF2ADC Mux", aif2adc_enum);
1547 
1548 static const char *aif3adc_text[] = {
1549 	"AIF1ADCDAT", "AIF2ADCDAT", "AIF2DACDAT", "Mono PCM",
1550 };
1551 
1552 static SOC_ENUM_SINGLE_DECL(wm8994_aif3adc_enum,
1553 			    WM8994_POWER_MANAGEMENT_6, 3, aif3adc_text);
1554 
1555 static const struct snd_kcontrol_new wm8994_aif3adc_mux =
1556 	SOC_DAPM_ENUM("AIF3ADC Mux", wm8994_aif3adc_enum);
1557 
1558 static SOC_ENUM_SINGLE_DECL(wm8958_aif3adc_enum,
1559 			    WM8994_POWER_MANAGEMENT_6, 3, aif3adc_text);
1560 
1561 static const struct snd_kcontrol_new wm8958_aif3adc_mux =
1562 	SOC_DAPM_ENUM("AIF3ADC Mux", wm8958_aif3adc_enum);
1563 
1564 static const char *mono_pcm_out_text[] = {
1565 	"None", "AIF2ADCL", "AIF2ADCR",
1566 };
1567 
1568 static SOC_ENUM_SINGLE_DECL(mono_pcm_out_enum,
1569 			    WM8994_POWER_MANAGEMENT_6, 9, mono_pcm_out_text);
1570 
1571 static const struct snd_kcontrol_new mono_pcm_out_mux =
1572 	SOC_DAPM_ENUM("Mono PCM Out Mux", mono_pcm_out_enum);
1573 
1574 static const char *aif2dac_src_text[] = {
1575 	"AIF2", "AIF3",
1576 };
1577 
1578 /* Note that these two control shouldn't be simultaneously switched to AIF3 */
1579 static SOC_ENUM_SINGLE_DECL(aif2dacl_src_enum,
1580 			    WM8994_POWER_MANAGEMENT_6, 7, aif2dac_src_text);
1581 
1582 static const struct snd_kcontrol_new aif2dacl_src_mux =
1583 	SOC_DAPM_ENUM("AIF2DACL Mux", aif2dacl_src_enum);
1584 
1585 static SOC_ENUM_SINGLE_DECL(aif2dacr_src_enum,
1586 			    WM8994_POWER_MANAGEMENT_6, 8, aif2dac_src_text);
1587 
1588 static const struct snd_kcontrol_new aif2dacr_src_mux =
1589 	SOC_DAPM_ENUM("AIF2DACR Mux", aif2dacr_src_enum);
1590 
1591 static const struct snd_soc_dapm_widget wm8994_lateclk_revd_widgets[] = {
1592 SND_SOC_DAPM_SUPPLY("AIF1CLK", SND_SOC_NOPM, 0, 0, aif1clk_late_ev,
1593 	SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1594 SND_SOC_DAPM_SUPPLY("AIF2CLK", SND_SOC_NOPM, 0, 0, aif2clk_late_ev,
1595 	SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1596 
1597 SND_SOC_DAPM_PGA_E("Late DAC1L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1598 	late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1599 SND_SOC_DAPM_PGA_E("Late DAC1R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1600 	late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1601 SND_SOC_DAPM_PGA_E("Late DAC2L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1602 	late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1603 SND_SOC_DAPM_PGA_E("Late DAC2R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1604 	late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1605 SND_SOC_DAPM_PGA_E("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0,
1606 	late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1607 
1608 SND_SOC_DAPM_MIXER_E("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
1609 		     left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer),
1610 		     late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1611 SND_SOC_DAPM_MIXER_E("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
1612 		     right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer),
1613 		     late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1614 SND_SOC_DAPM_MUX_E("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpl_mux,
1615 		   late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1616 SND_SOC_DAPM_MUX_E("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpr_mux,
1617 		   late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1618 
1619 SND_SOC_DAPM_POST("Late Disable PGA", late_disable_ev)
1620 };
1621 
1622 static const struct snd_soc_dapm_widget wm8994_lateclk_widgets[] = {
1623 SND_SOC_DAPM_SUPPLY("AIF1CLK", WM8994_AIF1_CLOCKING_1, 0, 0, aif1clk_ev,
1624 		    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
1625 		    SND_SOC_DAPM_PRE_PMD),
1626 SND_SOC_DAPM_SUPPLY("AIF2CLK", WM8994_AIF2_CLOCKING_1, 0, 0, aif2clk_ev,
1627 		    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
1628 		    SND_SOC_DAPM_PRE_PMD),
1629 SND_SOC_DAPM_PGA("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0),
1630 SND_SOC_DAPM_MIXER("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
1631 		   left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
1632 SND_SOC_DAPM_MIXER("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
1633 		   right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
1634 SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpl_mux),
1635 SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpr_mux),
1636 };
1637 
1638 static const struct snd_soc_dapm_widget wm8994_dac_revd_widgets[] = {
1639 SND_SOC_DAPM_DAC_E("DAC2L", NULL, SND_SOC_NOPM, 3, 0,
1640 	dac_ev, SND_SOC_DAPM_PRE_PMU),
1641 SND_SOC_DAPM_DAC_E("DAC2R", NULL, SND_SOC_NOPM, 2, 0,
1642 	dac_ev, SND_SOC_DAPM_PRE_PMU),
1643 SND_SOC_DAPM_DAC_E("DAC1L", NULL, SND_SOC_NOPM, 1, 0,
1644 	dac_ev, SND_SOC_DAPM_PRE_PMU),
1645 SND_SOC_DAPM_DAC_E("DAC1R", NULL, SND_SOC_NOPM, 0, 0,
1646 	dac_ev, SND_SOC_DAPM_PRE_PMU),
1647 };
1648 
1649 static const struct snd_soc_dapm_widget wm8994_dac_widgets[] = {
1650 SND_SOC_DAPM_DAC("DAC2L", NULL, WM8994_POWER_MANAGEMENT_5, 3, 0),
1651 SND_SOC_DAPM_DAC("DAC2R", NULL, WM8994_POWER_MANAGEMENT_5, 2, 0),
1652 SND_SOC_DAPM_DAC("DAC1L", NULL, WM8994_POWER_MANAGEMENT_5, 1, 0),
1653 SND_SOC_DAPM_DAC("DAC1R", NULL, WM8994_POWER_MANAGEMENT_5, 0, 0),
1654 };
1655 
1656 static const struct snd_soc_dapm_widget wm8994_adc_revd_widgets[] = {
1657 SND_SOC_DAPM_MUX_E("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux,
1658 			adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
1659 SND_SOC_DAPM_MUX_E("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux,
1660 			adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
1661 };
1662 
1663 static const struct snd_soc_dapm_widget wm8994_adc_widgets[] = {
1664 SND_SOC_DAPM_MUX("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux),
1665 SND_SOC_DAPM_MUX("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux),
1666 };
1667 
1668 static const struct snd_soc_dapm_widget wm8994_dapm_widgets[] = {
1669 SND_SOC_DAPM_INPUT("DMIC1DAT"),
1670 SND_SOC_DAPM_INPUT("DMIC2DAT"),
1671 SND_SOC_DAPM_INPUT("Clock"),
1672 
1673 SND_SOC_DAPM_SUPPLY_S("MICBIAS Supply", 1, SND_SOC_NOPM, 0, 0, micbias_ev,
1674 		      SND_SOC_DAPM_PRE_PMU),
1675 SND_SOC_DAPM_SUPPLY("VMID", SND_SOC_NOPM, 0, 0, vmid_event,
1676 		    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1677 
1678 SND_SOC_DAPM_SUPPLY("CLK_SYS", SND_SOC_NOPM, 0, 0, clk_sys_event,
1679 		    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
1680 		    SND_SOC_DAPM_PRE_PMD),
1681 
1682 SND_SOC_DAPM_SUPPLY("DSP1CLK", SND_SOC_NOPM, 3, 0, NULL, 0),
1683 SND_SOC_DAPM_SUPPLY("DSP2CLK", SND_SOC_NOPM, 2, 0, NULL, 0),
1684 SND_SOC_DAPM_SUPPLY("DSPINTCLK", SND_SOC_NOPM, 1, 0, NULL, 0),
1685 
1686 SND_SOC_DAPM_AIF_OUT("AIF1ADC1L", NULL,
1687 		     0, SND_SOC_NOPM, 9, 0),
1688 SND_SOC_DAPM_AIF_OUT("AIF1ADC1R", NULL,
1689 		     0, SND_SOC_NOPM, 8, 0),
1690 SND_SOC_DAPM_AIF_IN_E("AIF1DAC1L", NULL, 0,
1691 		      SND_SOC_NOPM, 9, 0, wm8958_aif_ev,
1692 		      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1693 SND_SOC_DAPM_AIF_IN_E("AIF1DAC1R", NULL, 0,
1694 		      SND_SOC_NOPM, 8, 0, wm8958_aif_ev,
1695 		      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1696 
1697 SND_SOC_DAPM_AIF_OUT("AIF1ADC2L", NULL,
1698 		     0, SND_SOC_NOPM, 11, 0),
1699 SND_SOC_DAPM_AIF_OUT("AIF1ADC2R", NULL,
1700 		     0, SND_SOC_NOPM, 10, 0),
1701 SND_SOC_DAPM_AIF_IN_E("AIF1DAC2L", NULL, 0,
1702 		      SND_SOC_NOPM, 11, 0, wm8958_aif_ev,
1703 		      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1704 SND_SOC_DAPM_AIF_IN_E("AIF1DAC2R", NULL, 0,
1705 		      SND_SOC_NOPM, 10, 0, wm8958_aif_ev,
1706 		      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1707 
1708 SND_SOC_DAPM_MIXER("AIF1ADC1L Mixer", SND_SOC_NOPM, 0, 0,
1709 		   aif1adc1l_mix, ARRAY_SIZE(aif1adc1l_mix)),
1710 SND_SOC_DAPM_MIXER("AIF1ADC1R Mixer", SND_SOC_NOPM, 0, 0,
1711 		   aif1adc1r_mix, ARRAY_SIZE(aif1adc1r_mix)),
1712 
1713 SND_SOC_DAPM_MIXER("AIF1ADC2L Mixer", SND_SOC_NOPM, 0, 0,
1714 		   aif1adc2l_mix, ARRAY_SIZE(aif1adc2l_mix)),
1715 SND_SOC_DAPM_MIXER("AIF1ADC2R Mixer", SND_SOC_NOPM, 0, 0,
1716 		   aif1adc2r_mix, ARRAY_SIZE(aif1adc2r_mix)),
1717 
1718 SND_SOC_DAPM_MIXER("AIF2DAC2L Mixer", SND_SOC_NOPM, 0, 0,
1719 		   aif2dac2l_mix, ARRAY_SIZE(aif2dac2l_mix)),
1720 SND_SOC_DAPM_MIXER("AIF2DAC2R Mixer", SND_SOC_NOPM, 0, 0,
1721 		   aif2dac2r_mix, ARRAY_SIZE(aif2dac2r_mix)),
1722 
1723 SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &sidetone1_mux),
1724 SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &sidetone2_mux),
1725 
1726 SND_SOC_DAPM_MIXER("DAC1L Mixer", SND_SOC_NOPM, 0, 0,
1727 		   dac1l_mix, ARRAY_SIZE(dac1l_mix)),
1728 SND_SOC_DAPM_MIXER("DAC1R Mixer", SND_SOC_NOPM, 0, 0,
1729 		   dac1r_mix, ARRAY_SIZE(dac1r_mix)),
1730 
1731 SND_SOC_DAPM_AIF_OUT("AIF2ADCL", NULL, 0,
1732 		     SND_SOC_NOPM, 13, 0),
1733 SND_SOC_DAPM_AIF_OUT("AIF2ADCR", NULL, 0,
1734 		     SND_SOC_NOPM, 12, 0),
1735 SND_SOC_DAPM_AIF_IN_E("AIF2DACL", NULL, 0,
1736 		      SND_SOC_NOPM, 13, 0, wm8958_aif_ev,
1737 		      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1738 SND_SOC_DAPM_AIF_IN_E("AIF2DACR", NULL, 0,
1739 		      SND_SOC_NOPM, 12, 0, wm8958_aif_ev,
1740 		      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1741 
1742 SND_SOC_DAPM_AIF_IN("AIF1DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1743 SND_SOC_DAPM_AIF_IN("AIF2DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1744 SND_SOC_DAPM_AIF_OUT("AIF1ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1745 SND_SOC_DAPM_AIF_OUT("AIF2ADCDAT",  NULL, 0, SND_SOC_NOPM, 0, 0),
1746 
1747 SND_SOC_DAPM_MUX("AIF1DAC Mux", SND_SOC_NOPM, 0, 0, &aif1dac_mux),
1748 SND_SOC_DAPM_MUX("AIF2DAC Mux", SND_SOC_NOPM, 0, 0, &aif2dac_mux),
1749 SND_SOC_DAPM_MUX("AIF2ADC Mux", SND_SOC_NOPM, 0, 0, &aif2adc_mux),
1750 
1751 SND_SOC_DAPM_AIF_IN("AIF3DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1752 SND_SOC_DAPM_AIF_OUT("AIF3ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1753 
1754 SND_SOC_DAPM_SUPPLY("TOCLK", WM8994_CLOCKING_1, 4, 0, NULL, 0),
1755 
1756 SND_SOC_DAPM_ADC("DMIC2L", NULL, WM8994_POWER_MANAGEMENT_4, 5, 0),
1757 SND_SOC_DAPM_ADC("DMIC2R", NULL, WM8994_POWER_MANAGEMENT_4, 4, 0),
1758 SND_SOC_DAPM_ADC("DMIC1L", NULL, WM8994_POWER_MANAGEMENT_4, 3, 0),
1759 SND_SOC_DAPM_ADC("DMIC1R", NULL, WM8994_POWER_MANAGEMENT_4, 2, 0),
1760 
1761 /* Power is done with the muxes since the ADC power also controls the
1762  * downsampling chain, the chip will automatically manage the analogue
1763  * specific portions.
1764  */
1765 SND_SOC_DAPM_ADC("ADCL", NULL, SND_SOC_NOPM, 1, 0),
1766 SND_SOC_DAPM_ADC("ADCR", NULL, SND_SOC_NOPM, 0, 0),
1767 
1768 SND_SOC_DAPM_MUX("AIF1 Loopback", SND_SOC_NOPM, 0, 0, &aif1_loopback),
1769 SND_SOC_DAPM_MUX("AIF2 Loopback", SND_SOC_NOPM, 0, 0, &aif2_loopback),
1770 
1771 SND_SOC_DAPM_POST("Debug log", post_ev),
1772 };
1773 
1774 static const struct snd_soc_dapm_widget wm8994_specific_dapm_widgets[] = {
1775 SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8994_aif3adc_mux),
1776 };
1777 
1778 static const struct snd_soc_dapm_widget wm8958_dapm_widgets[] = {
1779 SND_SOC_DAPM_SUPPLY("AIF3", WM8994_POWER_MANAGEMENT_6, 5, 1, NULL, 0),
1780 SND_SOC_DAPM_MUX("Mono PCM Out Mux", SND_SOC_NOPM, 0, 0, &mono_pcm_out_mux),
1781 SND_SOC_DAPM_MUX("AIF2DACL Mux", SND_SOC_NOPM, 0, 0, &aif2dacl_src_mux),
1782 SND_SOC_DAPM_MUX("AIF2DACR Mux", SND_SOC_NOPM, 0, 0, &aif2dacr_src_mux),
1783 SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8958_aif3adc_mux),
1784 };
1785 
1786 static const struct snd_soc_dapm_route intercon[] = {
1787 	{ "CLK_SYS", NULL, "AIF1CLK", check_clk_sys },
1788 	{ "CLK_SYS", NULL, "AIF2CLK", check_clk_sys },
1789 
1790 	{ "DSP1CLK", NULL, "CLK_SYS" },
1791 	{ "DSP2CLK", NULL, "CLK_SYS" },
1792 	{ "DSPINTCLK", NULL, "CLK_SYS" },
1793 
1794 	{ "AIF1ADC1L", NULL, "AIF1CLK" },
1795 	{ "AIF1ADC1L", NULL, "DSP1CLK" },
1796 	{ "AIF1ADC1R", NULL, "AIF1CLK" },
1797 	{ "AIF1ADC1R", NULL, "DSP1CLK" },
1798 	{ "AIF1ADC1R", NULL, "DSPINTCLK" },
1799 
1800 	{ "AIF1DAC1L", NULL, "AIF1CLK" },
1801 	{ "AIF1DAC1L", NULL, "DSP1CLK" },
1802 	{ "AIF1DAC1R", NULL, "AIF1CLK" },
1803 	{ "AIF1DAC1R", NULL, "DSP1CLK" },
1804 	{ "AIF1DAC1R", NULL, "DSPINTCLK" },
1805 
1806 	{ "AIF1ADC2L", NULL, "AIF1CLK" },
1807 	{ "AIF1ADC2L", NULL, "DSP1CLK" },
1808 	{ "AIF1ADC2R", NULL, "AIF1CLK" },
1809 	{ "AIF1ADC2R", NULL, "DSP1CLK" },
1810 	{ "AIF1ADC2R", NULL, "DSPINTCLK" },
1811 
1812 	{ "AIF1DAC2L", NULL, "AIF1CLK" },
1813 	{ "AIF1DAC2L", NULL, "DSP1CLK" },
1814 	{ "AIF1DAC2R", NULL, "AIF1CLK" },
1815 	{ "AIF1DAC2R", NULL, "DSP1CLK" },
1816 	{ "AIF1DAC2R", NULL, "DSPINTCLK" },
1817 
1818 	{ "AIF2ADCL", NULL, "AIF2CLK" },
1819 	{ "AIF2ADCL", NULL, "DSP2CLK" },
1820 	{ "AIF2ADCR", NULL, "AIF2CLK" },
1821 	{ "AIF2ADCR", NULL, "DSP2CLK" },
1822 	{ "AIF2ADCR", NULL, "DSPINTCLK" },
1823 
1824 	{ "AIF2DACL", NULL, "AIF2CLK" },
1825 	{ "AIF2DACL", NULL, "DSP2CLK" },
1826 	{ "AIF2DACR", NULL, "AIF2CLK" },
1827 	{ "AIF2DACR", NULL, "DSP2CLK" },
1828 	{ "AIF2DACR", NULL, "DSPINTCLK" },
1829 
1830 	{ "DMIC1L", NULL, "DMIC1DAT" },
1831 	{ "DMIC1L", NULL, "CLK_SYS" },
1832 	{ "DMIC1R", NULL, "DMIC1DAT" },
1833 	{ "DMIC1R", NULL, "CLK_SYS" },
1834 	{ "DMIC2L", NULL, "DMIC2DAT" },
1835 	{ "DMIC2L", NULL, "CLK_SYS" },
1836 	{ "DMIC2R", NULL, "DMIC2DAT" },
1837 	{ "DMIC2R", NULL, "CLK_SYS" },
1838 
1839 	{ "ADCL", NULL, "AIF1CLK" },
1840 	{ "ADCL", NULL, "DSP1CLK" },
1841 	{ "ADCL", NULL, "DSPINTCLK" },
1842 
1843 	{ "ADCR", NULL, "AIF1CLK" },
1844 	{ "ADCR", NULL, "DSP1CLK" },
1845 	{ "ADCR", NULL, "DSPINTCLK" },
1846 
1847 	{ "ADCL Mux", "ADC", "ADCL" },
1848 	{ "ADCL Mux", "DMIC", "DMIC1L" },
1849 	{ "ADCR Mux", "ADC", "ADCR" },
1850 	{ "ADCR Mux", "DMIC", "DMIC1R" },
1851 
1852 	{ "DAC1L", NULL, "AIF1CLK" },
1853 	{ "DAC1L", NULL, "DSP1CLK" },
1854 	{ "DAC1L", NULL, "DSPINTCLK" },
1855 
1856 	{ "DAC1R", NULL, "AIF1CLK" },
1857 	{ "DAC1R", NULL, "DSP1CLK" },
1858 	{ "DAC1R", NULL, "DSPINTCLK" },
1859 
1860 	{ "DAC2L", NULL, "AIF2CLK" },
1861 	{ "DAC2L", NULL, "DSP2CLK" },
1862 	{ "DAC2L", NULL, "DSPINTCLK" },
1863 
1864 	{ "DAC2R", NULL, "AIF2DACR" },
1865 	{ "DAC2R", NULL, "AIF2CLK" },
1866 	{ "DAC2R", NULL, "DSP2CLK" },
1867 	{ "DAC2R", NULL, "DSPINTCLK" },
1868 
1869 	{ "TOCLK", NULL, "CLK_SYS" },
1870 
1871 	{ "AIF1DACDAT", NULL, "AIF1 Playback" },
1872 	{ "AIF2DACDAT", NULL, "AIF2 Playback" },
1873 	{ "AIF3DACDAT", NULL, "AIF3 Playback" },
1874 
1875 	{ "AIF1 Capture", NULL, "AIF1ADCDAT" },
1876 	{ "AIF2 Capture", NULL, "AIF2ADCDAT" },
1877 	{ "AIF3 Capture", NULL, "AIF3ADCDAT" },
1878 
1879 	/* AIF1 outputs */
1880 	{ "AIF1ADC1L", NULL, "AIF1ADC1L Mixer" },
1881 	{ "AIF1ADC1L Mixer", "ADC/DMIC Switch", "ADCL Mux" },
1882 	{ "AIF1ADC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1883 
1884 	{ "AIF1ADC1R", NULL, "AIF1ADC1R Mixer" },
1885 	{ "AIF1ADC1R Mixer", "ADC/DMIC Switch", "ADCR Mux" },
1886 	{ "AIF1ADC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1887 
1888 	{ "AIF1ADC2L", NULL, "AIF1ADC2L Mixer" },
1889 	{ "AIF1ADC2L Mixer", "DMIC Switch", "DMIC2L" },
1890 	{ "AIF1ADC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1891 
1892 	{ "AIF1ADC2R", NULL, "AIF1ADC2R Mixer" },
1893 	{ "AIF1ADC2R Mixer", "DMIC Switch", "DMIC2R" },
1894 	{ "AIF1ADC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1895 
1896 	/* Pin level routing for AIF3 */
1897 	{ "AIF1DAC1L", NULL, "AIF1DAC Mux" },
1898 	{ "AIF1DAC1R", NULL, "AIF1DAC Mux" },
1899 	{ "AIF1DAC2L", NULL, "AIF1DAC Mux" },
1900 	{ "AIF1DAC2R", NULL, "AIF1DAC Mux" },
1901 
1902 	{ "AIF1DAC Mux", "AIF1DACDAT", "AIF1 Loopback" },
1903 	{ "AIF1DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1904 	{ "AIF2DAC Mux", "AIF2DACDAT", "AIF2 Loopback" },
1905 	{ "AIF2DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1906 	{ "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCL" },
1907 	{ "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCR" },
1908 	{ "AIF2ADC Mux", "AIF3DACDAT", "AIF3ADCDAT" },
1909 
1910 	/* DAC1 inputs */
1911 	{ "DAC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1912 	{ "DAC1L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1913 	{ "DAC1L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1914 	{ "DAC1L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1915 	{ "DAC1L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1916 
1917 	{ "DAC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1918 	{ "DAC1R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1919 	{ "DAC1R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1920 	{ "DAC1R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1921 	{ "DAC1R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1922 
1923 	/* DAC2/AIF2 outputs  */
1924 	{ "AIF2ADCL", NULL, "AIF2DAC2L Mixer" },
1925 	{ "AIF2DAC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1926 	{ "AIF2DAC2L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1927 	{ "AIF2DAC2L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1928 	{ "AIF2DAC2L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1929 	{ "AIF2DAC2L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1930 
1931 	{ "AIF2ADCR", NULL, "AIF2DAC2R Mixer" },
1932 	{ "AIF2DAC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1933 	{ "AIF2DAC2R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1934 	{ "AIF2DAC2R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1935 	{ "AIF2DAC2R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1936 	{ "AIF2DAC2R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1937 
1938 	{ "AIF1ADCDAT", NULL, "AIF1ADC1L" },
1939 	{ "AIF1ADCDAT", NULL, "AIF1ADC1R" },
1940 	{ "AIF1ADCDAT", NULL, "AIF1ADC2L" },
1941 	{ "AIF1ADCDAT", NULL, "AIF1ADC2R" },
1942 
1943 	{ "AIF2ADCDAT", NULL, "AIF2ADC Mux" },
1944 
1945 	/* AIF3 output */
1946 	{ "AIF3ADC Mux", "AIF1ADCDAT", "AIF1ADC1L" },
1947 	{ "AIF3ADC Mux", "AIF1ADCDAT", "AIF1ADC1R" },
1948 	{ "AIF3ADC Mux", "AIF1ADCDAT", "AIF1ADC2L" },
1949 	{ "AIF3ADC Mux", "AIF1ADCDAT", "AIF1ADC2R" },
1950 	{ "AIF3ADC Mux", "AIF2ADCDAT", "AIF2ADCL" },
1951 	{ "AIF3ADC Mux", "AIF2ADCDAT", "AIF2ADCR" },
1952 	{ "AIF3ADC Mux", "AIF2DACDAT", "AIF2DACL" },
1953 	{ "AIF3ADC Mux", "AIF2DACDAT", "AIF2DACR" },
1954 
1955 	{ "AIF3ADCDAT", NULL, "AIF3ADC Mux" },
1956 
1957 	/* Loopback */
1958 	{ "AIF1 Loopback", "ADCDAT", "AIF1ADCDAT" },
1959 	{ "AIF1 Loopback", "None", "AIF1DACDAT" },
1960 	{ "AIF2 Loopback", "ADCDAT", "AIF2ADCDAT" },
1961 	{ "AIF2 Loopback", "None", "AIF2DACDAT" },
1962 
1963 	/* Sidetone */
1964 	{ "Left Sidetone", "ADC/DMIC1", "ADCL Mux" },
1965 	{ "Left Sidetone", "DMIC2", "DMIC2L" },
1966 	{ "Right Sidetone", "ADC/DMIC1", "ADCR Mux" },
1967 	{ "Right Sidetone", "DMIC2", "DMIC2R" },
1968 
1969 	/* Output stages */
1970 	{ "Left Output Mixer", "DAC Switch", "DAC1L" },
1971 	{ "Right Output Mixer", "DAC Switch", "DAC1R" },
1972 
1973 	{ "SPKL", "DAC1 Switch", "DAC1L" },
1974 	{ "SPKL", "DAC2 Switch", "DAC2L" },
1975 
1976 	{ "SPKR", "DAC1 Switch", "DAC1R" },
1977 	{ "SPKR", "DAC2 Switch", "DAC2R" },
1978 
1979 	{ "Left Headphone Mux", "DAC", "DAC1L" },
1980 	{ "Right Headphone Mux", "DAC", "DAC1R" },
1981 };
1982 
1983 static const struct snd_soc_dapm_route wm8994_lateclk_revd_intercon[] = {
1984 	{ "DAC1L", NULL, "Late DAC1L Enable PGA" },
1985 	{ "Late DAC1L Enable PGA", NULL, "DAC1L Mixer" },
1986 	{ "DAC1R", NULL, "Late DAC1R Enable PGA" },
1987 	{ "Late DAC1R Enable PGA", NULL, "DAC1R Mixer" },
1988 	{ "DAC2L", NULL, "Late DAC2L Enable PGA" },
1989 	{ "Late DAC2L Enable PGA", NULL, "AIF2DAC2L Mixer" },
1990 	{ "DAC2R", NULL, "Late DAC2R Enable PGA" },
1991 	{ "Late DAC2R Enable PGA", NULL, "AIF2DAC2R Mixer" }
1992 };
1993 
1994 static const struct snd_soc_dapm_route wm8994_lateclk_intercon[] = {
1995 	{ "DAC1L", NULL, "DAC1L Mixer" },
1996 	{ "DAC1R", NULL, "DAC1R Mixer" },
1997 	{ "DAC2L", NULL, "AIF2DAC2L Mixer" },
1998 	{ "DAC2R", NULL, "AIF2DAC2R Mixer" },
1999 };
2000 
2001 static const struct snd_soc_dapm_route wm8994_revd_intercon[] = {
2002 	{ "AIF1DACDAT", NULL, "AIF2DACDAT" },
2003 	{ "AIF2DACDAT", NULL, "AIF1DACDAT" },
2004 	{ "AIF1ADCDAT", NULL, "AIF2ADCDAT" },
2005 	{ "AIF2ADCDAT", NULL, "AIF1ADCDAT" },
2006 	{ "MICBIAS1", NULL, "CLK_SYS" },
2007 	{ "MICBIAS1", NULL, "MICBIAS Supply" },
2008 	{ "MICBIAS2", NULL, "CLK_SYS" },
2009 	{ "MICBIAS2", NULL, "MICBIAS Supply" },
2010 };
2011 
2012 static const struct snd_soc_dapm_route wm8994_intercon[] = {
2013 	{ "AIF2DACL", NULL, "AIF2DAC Mux" },
2014 	{ "AIF2DACR", NULL, "AIF2DAC Mux" },
2015 	{ "MICBIAS1", NULL, "VMID" },
2016 	{ "MICBIAS2", NULL, "VMID" },
2017 };
2018 
2019 static const struct snd_soc_dapm_route wm8958_intercon[] = {
2020 	{ "AIF2DACL", NULL, "AIF2DACL Mux" },
2021 	{ "AIF2DACR", NULL, "AIF2DACR Mux" },
2022 
2023 	{ "AIF2DACL Mux", "AIF2", "AIF2DAC Mux" },
2024 	{ "AIF2DACL Mux", "AIF3", "AIF3DACDAT" },
2025 	{ "AIF2DACR Mux", "AIF2", "AIF2DAC Mux" },
2026 	{ "AIF2DACR Mux", "AIF3", "AIF3DACDAT" },
2027 
2028 	{ "AIF3DACDAT", NULL, "AIF3" },
2029 	{ "AIF3ADCDAT", NULL, "AIF3" },
2030 
2031 	{ "Mono PCM Out Mux", "AIF2ADCL", "AIF2ADCL" },
2032 	{ "Mono PCM Out Mux", "AIF2ADCR", "AIF2ADCR" },
2033 
2034 	{ "AIF3ADC Mux", "Mono PCM", "Mono PCM Out Mux" },
2035 };
2036 
2037 /* The size in bits of the FLL divide multiplied by 10
2038  * to allow rounding later */
2039 #define FIXED_FLL_SIZE ((1 << 16) * 10)
2040 
2041 struct fll_div {
2042 	u16 outdiv;
2043 	u16 n;
2044 	u16 k;
2045 	u16 lambda;
2046 	u16 clk_ref_div;
2047 	u16 fll_fratio;
2048 };
2049 
2050 static int wm8994_get_fll_config(struct wm8994 *control, struct fll_div *fll,
2051 				 int freq_in, int freq_out)
2052 {
2053 	u64 Kpart;
2054 	unsigned int K, Ndiv, Nmod, gcd_fll;
2055 
2056 	pr_debug("FLL input=%dHz, output=%dHz\n", freq_in, freq_out);
2057 
2058 	/* Scale the input frequency down to <= 13.5MHz */
2059 	fll->clk_ref_div = 0;
2060 	while (freq_in > 13500000) {
2061 		fll->clk_ref_div++;
2062 		freq_in /= 2;
2063 
2064 		if (fll->clk_ref_div > 3)
2065 			return -EINVAL;
2066 	}
2067 	pr_debug("CLK_REF_DIV=%d, Fref=%dHz\n", fll->clk_ref_div, freq_in);
2068 
2069 	/* Scale the output to give 90MHz<=Fvco<=100MHz */
2070 	fll->outdiv = 3;
2071 	while (freq_out * (fll->outdiv + 1) < 90000000) {
2072 		fll->outdiv++;
2073 		if (fll->outdiv > 63)
2074 			return -EINVAL;
2075 	}
2076 	freq_out *= fll->outdiv + 1;
2077 	pr_debug("OUTDIV=%d, Fvco=%dHz\n", fll->outdiv, freq_out);
2078 
2079 	if (freq_in > 1000000) {
2080 		fll->fll_fratio = 0;
2081 	} else if (freq_in > 256000) {
2082 		fll->fll_fratio = 1;
2083 		freq_in *= 2;
2084 	} else if (freq_in > 128000) {
2085 		fll->fll_fratio = 2;
2086 		freq_in *= 4;
2087 	} else if (freq_in > 64000) {
2088 		fll->fll_fratio = 3;
2089 		freq_in *= 8;
2090 	} else {
2091 		fll->fll_fratio = 4;
2092 		freq_in *= 16;
2093 	}
2094 	pr_debug("FLL_FRATIO=%d, Fref=%dHz\n", fll->fll_fratio, freq_in);
2095 
2096 	/* Now, calculate N.K */
2097 	Ndiv = freq_out / freq_in;
2098 
2099 	fll->n = Ndiv;
2100 	Nmod = freq_out % freq_in;
2101 	pr_debug("Nmod=%d\n", Nmod);
2102 
2103 	switch (control->type) {
2104 	case WM8994:
2105 		/* Calculate fractional part - scale up so we can round. */
2106 		Kpart = FIXED_FLL_SIZE * (long long)Nmod;
2107 
2108 		do_div(Kpart, freq_in);
2109 
2110 		K = Kpart & 0xFFFFFFFF;
2111 
2112 		if ((K % 10) >= 5)
2113 			K += 5;
2114 
2115 		/* Move down to proper range now rounding is done */
2116 		fll->k = K / 10;
2117 		fll->lambda = 0;
2118 
2119 		pr_debug("N=%x K=%x\n", fll->n, fll->k);
2120 		break;
2121 
2122 	default:
2123 		gcd_fll = gcd(freq_out, freq_in);
2124 
2125 		fll->k = (freq_out - (freq_in * fll->n)) / gcd_fll;
2126 		fll->lambda = freq_in / gcd_fll;
2127 
2128 	}
2129 
2130 	return 0;
2131 }
2132 
2133 static int _wm8994_set_fll(struct snd_soc_component *component, int id, int src,
2134 			  unsigned int freq_in, unsigned int freq_out)
2135 {
2136 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
2137 	struct wm8994 *control = wm8994->wm8994;
2138 	int reg_offset, ret;
2139 	struct fll_div fll;
2140 	u16 reg, clk1, aif_reg, aif_src;
2141 	unsigned long timeout;
2142 	bool was_enabled;
2143 
2144 	switch (id) {
2145 	case WM8994_FLL1:
2146 		reg_offset = 0;
2147 		id = 0;
2148 		aif_src = 0x10;
2149 		break;
2150 	case WM8994_FLL2:
2151 		reg_offset = 0x20;
2152 		id = 1;
2153 		aif_src = 0x18;
2154 		break;
2155 	default:
2156 		return -EINVAL;
2157 	}
2158 
2159 	reg = snd_soc_component_read32(component, WM8994_FLL1_CONTROL_1 + reg_offset);
2160 	was_enabled = reg & WM8994_FLL1_ENA;
2161 
2162 	switch (src) {
2163 	case 0:
2164 		/* Allow no source specification when stopping */
2165 		if (freq_out)
2166 			return -EINVAL;
2167 		src = wm8994->fll[id].src;
2168 		break;
2169 	case WM8994_FLL_SRC_MCLK1:
2170 	case WM8994_FLL_SRC_MCLK2:
2171 	case WM8994_FLL_SRC_LRCLK:
2172 	case WM8994_FLL_SRC_BCLK:
2173 		break;
2174 	case WM8994_FLL_SRC_INTERNAL:
2175 		freq_in = 12000000;
2176 		freq_out = 12000000;
2177 		break;
2178 	default:
2179 		return -EINVAL;
2180 	}
2181 
2182 	/* Are we changing anything? */
2183 	if (wm8994->fll[id].src == src &&
2184 	    wm8994->fll[id].in == freq_in && wm8994->fll[id].out == freq_out)
2185 		return 0;
2186 
2187 	/* If we're stopping the FLL redo the old config - no
2188 	 * registers will actually be written but we avoid GCC flow
2189 	 * analysis bugs spewing warnings.
2190 	 */
2191 	if (freq_out)
2192 		ret = wm8994_get_fll_config(control, &fll, freq_in, freq_out);
2193 	else
2194 		ret = wm8994_get_fll_config(control, &fll, wm8994->fll[id].in,
2195 					    wm8994->fll[id].out);
2196 	if (ret < 0)
2197 		return ret;
2198 
2199 	/* Make sure that we're not providing SYSCLK right now */
2200 	clk1 = snd_soc_component_read32(component, WM8994_CLOCKING_1);
2201 	if (clk1 & WM8994_SYSCLK_SRC)
2202 		aif_reg = WM8994_AIF2_CLOCKING_1;
2203 	else
2204 		aif_reg = WM8994_AIF1_CLOCKING_1;
2205 	reg = snd_soc_component_read32(component, aif_reg);
2206 
2207 	if ((reg & WM8994_AIF1CLK_ENA) &&
2208 	    (reg & WM8994_AIF1CLK_SRC_MASK) == aif_src) {
2209 		dev_err(component->dev, "FLL%d is currently providing SYSCLK\n",
2210 			id + 1);
2211 		return -EBUSY;
2212 	}
2213 
2214 	/* We always need to disable the FLL while reconfiguring */
2215 	snd_soc_component_update_bits(component, WM8994_FLL1_CONTROL_1 + reg_offset,
2216 			    WM8994_FLL1_ENA, 0);
2217 
2218 	if (wm8994->fll_byp && src == WM8994_FLL_SRC_BCLK &&
2219 	    freq_in == freq_out && freq_out) {
2220 		dev_dbg(component->dev, "Bypassing FLL%d\n", id + 1);
2221 		snd_soc_component_update_bits(component, WM8994_FLL1_CONTROL_5 + reg_offset,
2222 				    WM8958_FLL1_BYP, WM8958_FLL1_BYP);
2223 		goto out;
2224 	}
2225 
2226 	reg = (fll.outdiv << WM8994_FLL1_OUTDIV_SHIFT) |
2227 		(fll.fll_fratio << WM8994_FLL1_FRATIO_SHIFT);
2228 	snd_soc_component_update_bits(component, WM8994_FLL1_CONTROL_2 + reg_offset,
2229 			    WM8994_FLL1_OUTDIV_MASK |
2230 			    WM8994_FLL1_FRATIO_MASK, reg);
2231 
2232 	snd_soc_component_update_bits(component, WM8994_FLL1_CONTROL_3 + reg_offset,
2233 			    WM8994_FLL1_K_MASK, fll.k);
2234 
2235 	snd_soc_component_update_bits(component, WM8994_FLL1_CONTROL_4 + reg_offset,
2236 			    WM8994_FLL1_N_MASK,
2237 			    fll.n << WM8994_FLL1_N_SHIFT);
2238 
2239 	if (fll.lambda) {
2240 		snd_soc_component_update_bits(component, WM8958_FLL1_EFS_1 + reg_offset,
2241 				    WM8958_FLL1_LAMBDA_MASK,
2242 				    fll.lambda);
2243 		snd_soc_component_update_bits(component, WM8958_FLL1_EFS_2 + reg_offset,
2244 				    WM8958_FLL1_EFS_ENA, WM8958_FLL1_EFS_ENA);
2245 	} else {
2246 		snd_soc_component_update_bits(component, WM8958_FLL1_EFS_2 + reg_offset,
2247 				    WM8958_FLL1_EFS_ENA, 0);
2248 	}
2249 
2250 	snd_soc_component_update_bits(component, WM8994_FLL1_CONTROL_5 + reg_offset,
2251 			    WM8994_FLL1_FRC_NCO | WM8958_FLL1_BYP |
2252 			    WM8994_FLL1_REFCLK_DIV_MASK |
2253 			    WM8994_FLL1_REFCLK_SRC_MASK,
2254 			    ((src == WM8994_FLL_SRC_INTERNAL)
2255 			     << WM8994_FLL1_FRC_NCO_SHIFT) |
2256 			    (fll.clk_ref_div << WM8994_FLL1_REFCLK_DIV_SHIFT) |
2257 			    (src - 1));
2258 
2259 	/* Clear any pending completion from a previous failure */
2260 	try_wait_for_completion(&wm8994->fll_locked[id]);
2261 
2262 	/* Enable (with fractional mode if required) */
2263 	if (freq_out) {
2264 		/* Enable VMID if we need it */
2265 		if (!was_enabled) {
2266 			active_reference(component);
2267 
2268 			switch (control->type) {
2269 			case WM8994:
2270 				vmid_reference(component);
2271 				break;
2272 			case WM8958:
2273 				if (control->revision < 1)
2274 					vmid_reference(component);
2275 				break;
2276 			default:
2277 				break;
2278 			}
2279 		}
2280 
2281 		reg = WM8994_FLL1_ENA;
2282 
2283 		if (fll.k)
2284 			reg |= WM8994_FLL1_FRAC;
2285 		if (src == WM8994_FLL_SRC_INTERNAL)
2286 			reg |= WM8994_FLL1_OSC_ENA;
2287 
2288 		snd_soc_component_update_bits(component, WM8994_FLL1_CONTROL_1 + reg_offset,
2289 				    WM8994_FLL1_ENA | WM8994_FLL1_OSC_ENA |
2290 				    WM8994_FLL1_FRAC, reg);
2291 
2292 		if (wm8994->fll_locked_irq) {
2293 			timeout = wait_for_completion_timeout(&wm8994->fll_locked[id],
2294 							      msecs_to_jiffies(10));
2295 			if (timeout == 0)
2296 				dev_warn(component->dev,
2297 					 "Timed out waiting for FLL lock\n");
2298 		} else {
2299 			msleep(5);
2300 		}
2301 	} else {
2302 		if (was_enabled) {
2303 			switch (control->type) {
2304 			case WM8994:
2305 				vmid_dereference(component);
2306 				break;
2307 			case WM8958:
2308 				if (control->revision < 1)
2309 					vmid_dereference(component);
2310 				break;
2311 			default:
2312 				break;
2313 			}
2314 
2315 			active_dereference(component);
2316 		}
2317 	}
2318 
2319 out:
2320 	wm8994->fll[id].in = freq_in;
2321 	wm8994->fll[id].out = freq_out;
2322 	wm8994->fll[id].src = src;
2323 
2324 	configure_clock(component);
2325 
2326 	/*
2327 	 * If SYSCLK will be less than 50kHz adjust AIFnCLK dividers
2328 	 * for detection.
2329 	 */
2330 	if (max(wm8994->aifclk[0], wm8994->aifclk[1]) < 50000) {
2331 		dev_dbg(component->dev, "Configuring AIFs for 128fs\n");
2332 
2333 		wm8994->aifdiv[0] = snd_soc_component_read32(component, WM8994_AIF1_RATE)
2334 			& WM8994_AIF1CLK_RATE_MASK;
2335 		wm8994->aifdiv[1] = snd_soc_component_read32(component, WM8994_AIF2_RATE)
2336 			& WM8994_AIF1CLK_RATE_MASK;
2337 
2338 		snd_soc_component_update_bits(component, WM8994_AIF1_RATE,
2339 				    WM8994_AIF1CLK_RATE_MASK, 0x1);
2340 		snd_soc_component_update_bits(component, WM8994_AIF2_RATE,
2341 				    WM8994_AIF2CLK_RATE_MASK, 0x1);
2342 	} else if (wm8994->aifdiv[0]) {
2343 		snd_soc_component_update_bits(component, WM8994_AIF1_RATE,
2344 				    WM8994_AIF1CLK_RATE_MASK,
2345 				    wm8994->aifdiv[0]);
2346 		snd_soc_component_update_bits(component, WM8994_AIF2_RATE,
2347 				    WM8994_AIF2CLK_RATE_MASK,
2348 				    wm8994->aifdiv[1]);
2349 
2350 		wm8994->aifdiv[0] = 0;
2351 		wm8994->aifdiv[1] = 0;
2352 	}
2353 
2354 	return 0;
2355 }
2356 
2357 static irqreturn_t wm8994_fll_locked_irq(int irq, void *data)
2358 {
2359 	struct completion *completion = data;
2360 
2361 	complete(completion);
2362 
2363 	return IRQ_HANDLED;
2364 }
2365 
2366 static int opclk_divs[] = { 10, 20, 30, 40, 55, 60, 80, 120, 160 };
2367 
2368 static int wm8994_set_fll(struct snd_soc_dai *dai, int id, int src,
2369 			  unsigned int freq_in, unsigned int freq_out)
2370 {
2371 	return _wm8994_set_fll(dai->component, id, src, freq_in, freq_out);
2372 }
2373 
2374 static int wm8994_set_dai_sysclk(struct snd_soc_dai *dai,
2375 		int clk_id, unsigned int freq, int dir)
2376 {
2377 	struct snd_soc_component *component = dai->component;
2378 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
2379 	int i;
2380 
2381 	switch (dai->id) {
2382 	case 1:
2383 	case 2:
2384 		break;
2385 
2386 	default:
2387 		/* AIF3 shares clocking with AIF1/2 */
2388 		return -EINVAL;
2389 	}
2390 
2391 	switch (clk_id) {
2392 	case WM8994_SYSCLK_MCLK1:
2393 		wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK1;
2394 		wm8994->mclk[0] = freq;
2395 		dev_dbg(dai->dev, "AIF%d using MCLK1 at %uHz\n",
2396 			dai->id, freq);
2397 		break;
2398 
2399 	case WM8994_SYSCLK_MCLK2:
2400 		/* TODO: Set GPIO AF */
2401 		wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK2;
2402 		wm8994->mclk[1] = freq;
2403 		dev_dbg(dai->dev, "AIF%d using MCLK2 at %uHz\n",
2404 			dai->id, freq);
2405 		break;
2406 
2407 	case WM8994_SYSCLK_FLL1:
2408 		wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL1;
2409 		dev_dbg(dai->dev, "AIF%d using FLL1\n", dai->id);
2410 		break;
2411 
2412 	case WM8994_SYSCLK_FLL2:
2413 		wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL2;
2414 		dev_dbg(dai->dev, "AIF%d using FLL2\n", dai->id);
2415 		break;
2416 
2417 	case WM8994_SYSCLK_OPCLK:
2418 		/* Special case - a division (times 10) is given and
2419 		 * no effect on main clocking.
2420 		 */
2421 		if (freq) {
2422 			for (i = 0; i < ARRAY_SIZE(opclk_divs); i++)
2423 				if (opclk_divs[i] == freq)
2424 					break;
2425 			if (i == ARRAY_SIZE(opclk_divs))
2426 				return -EINVAL;
2427 			snd_soc_component_update_bits(component, WM8994_CLOCKING_2,
2428 					    WM8994_OPCLK_DIV_MASK, i);
2429 			snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_2,
2430 					    WM8994_OPCLK_ENA, WM8994_OPCLK_ENA);
2431 		} else {
2432 			snd_soc_component_update_bits(component, WM8994_POWER_MANAGEMENT_2,
2433 					    WM8994_OPCLK_ENA, 0);
2434 		}
2435 		break;
2436 
2437 	default:
2438 		return -EINVAL;
2439 	}
2440 
2441 	configure_clock(component);
2442 
2443 	/*
2444 	 * If SYSCLK will be less than 50kHz adjust AIFnCLK dividers
2445 	 * for detection.
2446 	 */
2447 	if (max(wm8994->aifclk[0], wm8994->aifclk[1]) < 50000) {
2448 		dev_dbg(component->dev, "Configuring AIFs for 128fs\n");
2449 
2450 		wm8994->aifdiv[0] = snd_soc_component_read32(component, WM8994_AIF1_RATE)
2451 			& WM8994_AIF1CLK_RATE_MASK;
2452 		wm8994->aifdiv[1] = snd_soc_component_read32(component, WM8994_AIF2_RATE)
2453 			& WM8994_AIF1CLK_RATE_MASK;
2454 
2455 		snd_soc_component_update_bits(component, WM8994_AIF1_RATE,
2456 				    WM8994_AIF1CLK_RATE_MASK, 0x1);
2457 		snd_soc_component_update_bits(component, WM8994_AIF2_RATE,
2458 				    WM8994_AIF2CLK_RATE_MASK, 0x1);
2459 	} else if (wm8994->aifdiv[0]) {
2460 		snd_soc_component_update_bits(component, WM8994_AIF1_RATE,
2461 				    WM8994_AIF1CLK_RATE_MASK,
2462 				    wm8994->aifdiv[0]);
2463 		snd_soc_component_update_bits(component, WM8994_AIF2_RATE,
2464 				    WM8994_AIF2CLK_RATE_MASK,
2465 				    wm8994->aifdiv[1]);
2466 
2467 		wm8994->aifdiv[0] = 0;
2468 		wm8994->aifdiv[1] = 0;
2469 	}
2470 
2471 	return 0;
2472 }
2473 
2474 static int wm8994_set_bias_level(struct snd_soc_component *component,
2475 				 enum snd_soc_bias_level level)
2476 {
2477 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
2478 	struct wm8994 *control = wm8994->wm8994;
2479 
2480 	wm_hubs_set_bias_level(component, level);
2481 
2482 	switch (level) {
2483 	case SND_SOC_BIAS_ON:
2484 		break;
2485 
2486 	case SND_SOC_BIAS_PREPARE:
2487 		/* MICBIAS into regulating mode */
2488 		switch (control->type) {
2489 		case WM8958:
2490 		case WM1811:
2491 			snd_soc_component_update_bits(component, WM8958_MICBIAS1,
2492 					    WM8958_MICB1_MODE, 0);
2493 			snd_soc_component_update_bits(component, WM8958_MICBIAS2,
2494 					    WM8958_MICB2_MODE, 0);
2495 			break;
2496 		default:
2497 			break;
2498 		}
2499 
2500 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_STANDBY)
2501 			active_reference(component);
2502 		break;
2503 
2504 	case SND_SOC_BIAS_STANDBY:
2505 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
2506 			switch (control->type) {
2507 			case WM8958:
2508 				if (control->revision == 0) {
2509 					/* Optimise performance for rev A */
2510 					snd_soc_component_update_bits(component,
2511 							    WM8958_CHARGE_PUMP_2,
2512 							    WM8958_CP_DISCH,
2513 							    WM8958_CP_DISCH);
2514 				}
2515 				break;
2516 
2517 			default:
2518 				break;
2519 			}
2520 
2521 			/* Discharge LINEOUT1 & 2 */
2522 			snd_soc_component_update_bits(component, WM8994_ANTIPOP_1,
2523 					    WM8994_LINEOUT1_DISCH |
2524 					    WM8994_LINEOUT2_DISCH,
2525 					    WM8994_LINEOUT1_DISCH |
2526 					    WM8994_LINEOUT2_DISCH);
2527 		}
2528 
2529 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_PREPARE)
2530 			active_dereference(component);
2531 
2532 		/* MICBIAS into bypass mode on newer devices */
2533 		switch (control->type) {
2534 		case WM8958:
2535 		case WM1811:
2536 			snd_soc_component_update_bits(component, WM8958_MICBIAS1,
2537 					    WM8958_MICB1_MODE,
2538 					    WM8958_MICB1_MODE);
2539 			snd_soc_component_update_bits(component, WM8958_MICBIAS2,
2540 					    WM8958_MICB2_MODE,
2541 					    WM8958_MICB2_MODE);
2542 			break;
2543 		default:
2544 			break;
2545 		}
2546 		break;
2547 
2548 	case SND_SOC_BIAS_OFF:
2549 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_STANDBY)
2550 			wm8994->cur_fw = NULL;
2551 		break;
2552 	}
2553 
2554 	return 0;
2555 }
2556 
2557 int wm8994_vmid_mode(struct snd_soc_component *component, enum wm8994_vmid_mode mode)
2558 {
2559 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
2560 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
2561 
2562 	switch (mode) {
2563 	case WM8994_VMID_NORMAL:
2564 		snd_soc_dapm_mutex_lock(dapm);
2565 
2566 		if (wm8994->hubs.lineout1_se) {
2567 			snd_soc_dapm_disable_pin_unlocked(dapm,
2568 							  "LINEOUT1N Driver");
2569 			snd_soc_dapm_disable_pin_unlocked(dapm,
2570 							  "LINEOUT1P Driver");
2571 		}
2572 		if (wm8994->hubs.lineout2_se) {
2573 			snd_soc_dapm_disable_pin_unlocked(dapm,
2574 							  "LINEOUT2N Driver");
2575 			snd_soc_dapm_disable_pin_unlocked(dapm,
2576 							  "LINEOUT2P Driver");
2577 		}
2578 
2579 		/* Do the sync with the old mode to allow it to clean up */
2580 		snd_soc_dapm_sync_unlocked(dapm);
2581 		wm8994->vmid_mode = mode;
2582 
2583 		snd_soc_dapm_mutex_unlock(dapm);
2584 		break;
2585 
2586 	case WM8994_VMID_FORCE:
2587 		snd_soc_dapm_mutex_lock(dapm);
2588 
2589 		if (wm8994->hubs.lineout1_se) {
2590 			snd_soc_dapm_force_enable_pin_unlocked(dapm,
2591 							       "LINEOUT1N Driver");
2592 			snd_soc_dapm_force_enable_pin_unlocked(dapm,
2593 							       "LINEOUT1P Driver");
2594 		}
2595 		if (wm8994->hubs.lineout2_se) {
2596 			snd_soc_dapm_force_enable_pin_unlocked(dapm,
2597 							       "LINEOUT2N Driver");
2598 			snd_soc_dapm_force_enable_pin_unlocked(dapm,
2599 							       "LINEOUT2P Driver");
2600 		}
2601 
2602 		wm8994->vmid_mode = mode;
2603 		snd_soc_dapm_sync_unlocked(dapm);
2604 
2605 		snd_soc_dapm_mutex_unlock(dapm);
2606 		break;
2607 
2608 	default:
2609 		return -EINVAL;
2610 	}
2611 
2612 	return 0;
2613 }
2614 
2615 static int wm8994_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2616 {
2617 	struct snd_soc_component *component = dai->component;
2618 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
2619 	struct wm8994 *control = wm8994->wm8994;
2620 	int ms_reg;
2621 	int aif1_reg;
2622 	int dac_reg;
2623 	int adc_reg;
2624 	int ms = 0;
2625 	int aif1 = 0;
2626 	int lrclk = 0;
2627 
2628 	switch (dai->id) {
2629 	case 1:
2630 		ms_reg = WM8994_AIF1_MASTER_SLAVE;
2631 		aif1_reg = WM8994_AIF1_CONTROL_1;
2632 		dac_reg = WM8994_AIF1DAC_LRCLK;
2633 		adc_reg = WM8994_AIF1ADC_LRCLK;
2634 		break;
2635 	case 2:
2636 		ms_reg = WM8994_AIF2_MASTER_SLAVE;
2637 		aif1_reg = WM8994_AIF2_CONTROL_1;
2638 		dac_reg = WM8994_AIF1DAC_LRCLK;
2639 		adc_reg = WM8994_AIF1ADC_LRCLK;
2640 		break;
2641 	default:
2642 		return -EINVAL;
2643 	}
2644 
2645 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
2646 	case SND_SOC_DAIFMT_CBS_CFS:
2647 		break;
2648 	case SND_SOC_DAIFMT_CBM_CFM:
2649 		ms = WM8994_AIF1_MSTR;
2650 		break;
2651 	default:
2652 		return -EINVAL;
2653 	}
2654 
2655 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2656 	case SND_SOC_DAIFMT_DSP_B:
2657 		aif1 |= WM8994_AIF1_LRCLK_INV;
2658 		lrclk |= WM8958_AIF1_LRCLK_INV;
2659 		/* fall through */
2660 	case SND_SOC_DAIFMT_DSP_A:
2661 		aif1 |= 0x18;
2662 		break;
2663 	case SND_SOC_DAIFMT_I2S:
2664 		aif1 |= 0x10;
2665 		break;
2666 	case SND_SOC_DAIFMT_RIGHT_J:
2667 		break;
2668 	case SND_SOC_DAIFMT_LEFT_J:
2669 		aif1 |= 0x8;
2670 		break;
2671 	default:
2672 		return -EINVAL;
2673 	}
2674 
2675 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2676 	case SND_SOC_DAIFMT_DSP_A:
2677 	case SND_SOC_DAIFMT_DSP_B:
2678 		/* frame inversion not valid for DSP modes */
2679 		switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2680 		case SND_SOC_DAIFMT_NB_NF:
2681 			break;
2682 		case SND_SOC_DAIFMT_IB_NF:
2683 			aif1 |= WM8994_AIF1_BCLK_INV;
2684 			break;
2685 		default:
2686 			return -EINVAL;
2687 		}
2688 		break;
2689 
2690 	case SND_SOC_DAIFMT_I2S:
2691 	case SND_SOC_DAIFMT_RIGHT_J:
2692 	case SND_SOC_DAIFMT_LEFT_J:
2693 		switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2694 		case SND_SOC_DAIFMT_NB_NF:
2695 			break;
2696 		case SND_SOC_DAIFMT_IB_IF:
2697 			aif1 |= WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV;
2698 			lrclk |= WM8958_AIF1_LRCLK_INV;
2699 			break;
2700 		case SND_SOC_DAIFMT_IB_NF:
2701 			aif1 |= WM8994_AIF1_BCLK_INV;
2702 			break;
2703 		case SND_SOC_DAIFMT_NB_IF:
2704 			aif1 |= WM8994_AIF1_LRCLK_INV;
2705 			lrclk |= WM8958_AIF1_LRCLK_INV;
2706 			break;
2707 		default:
2708 			return -EINVAL;
2709 		}
2710 		break;
2711 	default:
2712 		return -EINVAL;
2713 	}
2714 
2715 	/* The AIF2 format configuration needs to be mirrored to AIF3
2716 	 * on WM8958 if it's in use so just do it all the time. */
2717 	switch (control->type) {
2718 	case WM1811:
2719 	case WM8958:
2720 		if (dai->id == 2)
2721 			snd_soc_component_update_bits(component, WM8958_AIF3_CONTROL_1,
2722 					    WM8994_AIF1_LRCLK_INV |
2723 					    WM8958_AIF3_FMT_MASK, aif1);
2724 		break;
2725 
2726 	default:
2727 		break;
2728 	}
2729 
2730 	snd_soc_component_update_bits(component, aif1_reg,
2731 			    WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV |
2732 			    WM8994_AIF1_FMT_MASK,
2733 			    aif1);
2734 	snd_soc_component_update_bits(component, ms_reg, WM8994_AIF1_MSTR,
2735 			    ms);
2736 	snd_soc_component_update_bits(component, dac_reg,
2737 			    WM8958_AIF1_LRCLK_INV, lrclk);
2738 	snd_soc_component_update_bits(component, adc_reg,
2739 			    WM8958_AIF1_LRCLK_INV, lrclk);
2740 
2741 	return 0;
2742 }
2743 
2744 static struct {
2745 	int val, rate;
2746 } srs[] = {
2747 	{ 0,   8000 },
2748 	{ 1,  11025 },
2749 	{ 2,  12000 },
2750 	{ 3,  16000 },
2751 	{ 4,  22050 },
2752 	{ 5,  24000 },
2753 	{ 6,  32000 },
2754 	{ 7,  44100 },
2755 	{ 8,  48000 },
2756 	{ 9,  88200 },
2757 	{ 10, 96000 },
2758 };
2759 
2760 static int fs_ratios[] = {
2761 	64, 128, 192, 256, 384, 512, 768, 1024, 1408, 1536
2762 };
2763 
2764 static int bclk_divs[] = {
2765 	10, 15, 20, 30, 40, 50, 60, 80, 110, 120, 160, 220, 240, 320, 440, 480,
2766 	640, 880, 960, 1280, 1760, 1920
2767 };
2768 
2769 static int wm8994_hw_params(struct snd_pcm_substream *substream,
2770 			    struct snd_pcm_hw_params *params,
2771 			    struct snd_soc_dai *dai)
2772 {
2773 	struct snd_soc_component *component = dai->component;
2774 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
2775 	struct wm8994 *control = wm8994->wm8994;
2776 	struct wm8994_pdata *pdata = &control->pdata;
2777 	int aif1_reg;
2778 	int aif2_reg;
2779 	int bclk_reg;
2780 	int lrclk_reg;
2781 	int rate_reg;
2782 	int aif1 = 0;
2783 	int aif2 = 0;
2784 	int bclk = 0;
2785 	int lrclk = 0;
2786 	int rate_val = 0;
2787 	int id = dai->id - 1;
2788 
2789 	int i, cur_val, best_val, bclk_rate, best;
2790 
2791 	switch (dai->id) {
2792 	case 1:
2793 		aif1_reg = WM8994_AIF1_CONTROL_1;
2794 		aif2_reg = WM8994_AIF1_CONTROL_2;
2795 		bclk_reg = WM8994_AIF1_BCLK;
2796 		rate_reg = WM8994_AIF1_RATE;
2797 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
2798 		    wm8994->lrclk_shared[0]) {
2799 			lrclk_reg = WM8994_AIF1DAC_LRCLK;
2800 		} else {
2801 			lrclk_reg = WM8994_AIF1ADC_LRCLK;
2802 			dev_dbg(component->dev, "AIF1 using split LRCLK\n");
2803 		}
2804 		break;
2805 	case 2:
2806 		aif1_reg = WM8994_AIF2_CONTROL_1;
2807 		aif2_reg = WM8994_AIF2_CONTROL_2;
2808 		bclk_reg = WM8994_AIF2_BCLK;
2809 		rate_reg = WM8994_AIF2_RATE;
2810 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
2811 		    wm8994->lrclk_shared[1]) {
2812 			lrclk_reg = WM8994_AIF2DAC_LRCLK;
2813 		} else {
2814 			lrclk_reg = WM8994_AIF2ADC_LRCLK;
2815 			dev_dbg(component->dev, "AIF2 using split LRCLK\n");
2816 		}
2817 		break;
2818 	default:
2819 		return -EINVAL;
2820 	}
2821 
2822 	bclk_rate = params_rate(params);
2823 	switch (params_width(params)) {
2824 	case 16:
2825 		bclk_rate *= 16;
2826 		break;
2827 	case 20:
2828 		bclk_rate *= 20;
2829 		aif1 |= 0x20;
2830 		break;
2831 	case 24:
2832 		bclk_rate *= 24;
2833 		aif1 |= 0x40;
2834 		break;
2835 	case 32:
2836 		bclk_rate *= 32;
2837 		aif1 |= 0x60;
2838 		break;
2839 	default:
2840 		return -EINVAL;
2841 	}
2842 
2843 	wm8994->channels[id] = params_channels(params);
2844 	if (pdata->max_channels_clocked[id] &&
2845 	    wm8994->channels[id] > pdata->max_channels_clocked[id]) {
2846 		dev_dbg(dai->dev, "Constraining channels to %d from %d\n",
2847 			pdata->max_channels_clocked[id], wm8994->channels[id]);
2848 		wm8994->channels[id] = pdata->max_channels_clocked[id];
2849 	}
2850 
2851 	switch (wm8994->channels[id]) {
2852 	case 1:
2853 	case 2:
2854 		bclk_rate *= 2;
2855 		break;
2856 	default:
2857 		bclk_rate *= 4;
2858 		break;
2859 	}
2860 
2861 	/* Try to find an appropriate sample rate; look for an exact match. */
2862 	for (i = 0; i < ARRAY_SIZE(srs); i++)
2863 		if (srs[i].rate == params_rate(params))
2864 			break;
2865 	if (i == ARRAY_SIZE(srs))
2866 		return -EINVAL;
2867 	rate_val |= srs[i].val << WM8994_AIF1_SR_SHIFT;
2868 
2869 	dev_dbg(dai->dev, "Sample rate is %dHz\n", srs[i].rate);
2870 	dev_dbg(dai->dev, "AIF%dCLK is %dHz, target BCLK %dHz\n",
2871 		dai->id, wm8994->aifclk[id], bclk_rate);
2872 
2873 	if (wm8994->channels[id] == 1 &&
2874 	    (snd_soc_component_read32(component, aif1_reg) & 0x18) == 0x18)
2875 		aif2 |= WM8994_AIF1_MONO;
2876 
2877 	if (wm8994->aifclk[id] == 0) {
2878 		dev_err(dai->dev, "AIF%dCLK not configured\n", dai->id);
2879 		return -EINVAL;
2880 	}
2881 
2882 	/* AIFCLK/fs ratio; look for a close match in either direction */
2883 	best = 0;
2884 	best_val = abs((fs_ratios[0] * params_rate(params))
2885 		       - wm8994->aifclk[id]);
2886 	for (i = 1; i < ARRAY_SIZE(fs_ratios); i++) {
2887 		cur_val = abs((fs_ratios[i] * params_rate(params))
2888 			      - wm8994->aifclk[id]);
2889 		if (cur_val >= best_val)
2890 			continue;
2891 		best = i;
2892 		best_val = cur_val;
2893 	}
2894 	dev_dbg(dai->dev, "Selected AIF%dCLK/fs = %d\n",
2895 		dai->id, fs_ratios[best]);
2896 	rate_val |= best;
2897 
2898 	/* We may not get quite the right frequency if using
2899 	 * approximate clocks so look for the closest match that is
2900 	 * higher than the target (we need to ensure that there enough
2901 	 * BCLKs to clock out the samples).
2902 	 */
2903 	best = 0;
2904 	for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
2905 		cur_val = (wm8994->aifclk[id] * 10 / bclk_divs[i]) - bclk_rate;
2906 		if (cur_val < 0) /* BCLK table is sorted */
2907 			break;
2908 		best = i;
2909 	}
2910 	bclk_rate = wm8994->aifclk[id] * 10 / bclk_divs[best];
2911 	dev_dbg(dai->dev, "Using BCLK_DIV %d for actual BCLK %dHz\n",
2912 		bclk_divs[best], bclk_rate);
2913 	bclk |= best << WM8994_AIF1_BCLK_DIV_SHIFT;
2914 
2915 	lrclk = bclk_rate / params_rate(params);
2916 	if (!lrclk) {
2917 		dev_err(dai->dev, "Unable to generate LRCLK from %dHz BCLK\n",
2918 			bclk_rate);
2919 		return -EINVAL;
2920 	}
2921 	dev_dbg(dai->dev, "Using LRCLK rate %d for actual LRCLK %dHz\n",
2922 		lrclk, bclk_rate / lrclk);
2923 
2924 	snd_soc_component_update_bits(component, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
2925 	snd_soc_component_update_bits(component, aif2_reg, WM8994_AIF1_MONO, aif2);
2926 	snd_soc_component_update_bits(component, bclk_reg, WM8994_AIF1_BCLK_DIV_MASK, bclk);
2927 	snd_soc_component_update_bits(component, lrclk_reg, WM8994_AIF1DAC_RATE_MASK,
2928 			    lrclk);
2929 	snd_soc_component_update_bits(component, rate_reg, WM8994_AIF1_SR_MASK |
2930 			    WM8994_AIF1CLK_RATE_MASK, rate_val);
2931 
2932 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
2933 		switch (dai->id) {
2934 		case 1:
2935 			wm8994->dac_rates[0] = params_rate(params);
2936 			wm8994_set_retune_mobile(component, 0);
2937 			wm8994_set_retune_mobile(component, 1);
2938 			break;
2939 		case 2:
2940 			wm8994->dac_rates[1] = params_rate(params);
2941 			wm8994_set_retune_mobile(component, 2);
2942 			break;
2943 		}
2944 	}
2945 
2946 	return 0;
2947 }
2948 
2949 static int wm8994_aif3_hw_params(struct snd_pcm_substream *substream,
2950 				 struct snd_pcm_hw_params *params,
2951 				 struct snd_soc_dai *dai)
2952 {
2953 	struct snd_soc_component *component = dai->component;
2954 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
2955 	struct wm8994 *control = wm8994->wm8994;
2956 	int aif1_reg;
2957 	int aif1 = 0;
2958 
2959 	switch (dai->id) {
2960 	case 3:
2961 		switch (control->type) {
2962 		case WM1811:
2963 		case WM8958:
2964 			aif1_reg = WM8958_AIF3_CONTROL_1;
2965 			break;
2966 		default:
2967 			return 0;
2968 		}
2969 		break;
2970 	default:
2971 		return 0;
2972 	}
2973 
2974 	switch (params_width(params)) {
2975 	case 16:
2976 		break;
2977 	case 20:
2978 		aif1 |= 0x20;
2979 		break;
2980 	case 24:
2981 		aif1 |= 0x40;
2982 		break;
2983 	case 32:
2984 		aif1 |= 0x60;
2985 		break;
2986 	default:
2987 		return -EINVAL;
2988 	}
2989 
2990 	return snd_soc_component_update_bits(component, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
2991 }
2992 
2993 static int wm8994_aif_mute(struct snd_soc_dai *codec_dai, int mute)
2994 {
2995 	struct snd_soc_component *component = codec_dai->component;
2996 	int mute_reg;
2997 	int reg;
2998 
2999 	switch (codec_dai->id) {
3000 	case 1:
3001 		mute_reg = WM8994_AIF1_DAC1_FILTERS_1;
3002 		break;
3003 	case 2:
3004 		mute_reg = WM8994_AIF2_DAC_FILTERS_1;
3005 		break;
3006 	default:
3007 		return -EINVAL;
3008 	}
3009 
3010 	if (mute)
3011 		reg = WM8994_AIF1DAC1_MUTE;
3012 	else
3013 		reg = 0;
3014 
3015 	snd_soc_component_update_bits(component, mute_reg, WM8994_AIF1DAC1_MUTE, reg);
3016 
3017 	return 0;
3018 }
3019 
3020 static int wm8994_set_tristate(struct snd_soc_dai *codec_dai, int tristate)
3021 {
3022 	struct snd_soc_component *component = codec_dai->component;
3023 	int reg, val, mask;
3024 
3025 	switch (codec_dai->id) {
3026 	case 1:
3027 		reg = WM8994_AIF1_MASTER_SLAVE;
3028 		mask = WM8994_AIF1_TRI;
3029 		break;
3030 	case 2:
3031 		reg = WM8994_AIF2_MASTER_SLAVE;
3032 		mask = WM8994_AIF2_TRI;
3033 		break;
3034 	default:
3035 		return -EINVAL;
3036 	}
3037 
3038 	if (tristate)
3039 		val = mask;
3040 	else
3041 		val = 0;
3042 
3043 	return snd_soc_component_update_bits(component, reg, mask, val);
3044 }
3045 
3046 static int wm8994_aif2_probe(struct snd_soc_dai *dai)
3047 {
3048 	struct snd_soc_component *component = dai->component;
3049 
3050 	/* Disable the pulls on the AIF if we're using it to save power. */
3051 	snd_soc_component_update_bits(component, WM8994_GPIO_3,
3052 			    WM8994_GPN_PU | WM8994_GPN_PD, 0);
3053 	snd_soc_component_update_bits(component, WM8994_GPIO_4,
3054 			    WM8994_GPN_PU | WM8994_GPN_PD, 0);
3055 	snd_soc_component_update_bits(component, WM8994_GPIO_5,
3056 			    WM8994_GPN_PU | WM8994_GPN_PD, 0);
3057 
3058 	return 0;
3059 }
3060 
3061 #define WM8994_RATES SNDRV_PCM_RATE_8000_96000
3062 
3063 #define WM8994_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
3064 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
3065 
3066 static const struct snd_soc_dai_ops wm8994_aif1_dai_ops = {
3067 	.set_sysclk	= wm8994_set_dai_sysclk,
3068 	.set_fmt	= wm8994_set_dai_fmt,
3069 	.hw_params	= wm8994_hw_params,
3070 	.digital_mute	= wm8994_aif_mute,
3071 	.set_pll	= wm8994_set_fll,
3072 	.set_tristate	= wm8994_set_tristate,
3073 };
3074 
3075 static const struct snd_soc_dai_ops wm8994_aif2_dai_ops = {
3076 	.set_sysclk	= wm8994_set_dai_sysclk,
3077 	.set_fmt	= wm8994_set_dai_fmt,
3078 	.hw_params	= wm8994_hw_params,
3079 	.digital_mute   = wm8994_aif_mute,
3080 	.set_pll	= wm8994_set_fll,
3081 	.set_tristate	= wm8994_set_tristate,
3082 };
3083 
3084 static const struct snd_soc_dai_ops wm8994_aif3_dai_ops = {
3085 	.hw_params	= wm8994_aif3_hw_params,
3086 };
3087 
3088 static struct snd_soc_dai_driver wm8994_dai[] = {
3089 	{
3090 		.name = "wm8994-aif1",
3091 		.id = 1,
3092 		.playback = {
3093 			.stream_name = "AIF1 Playback",
3094 			.channels_min = 1,
3095 			.channels_max = 2,
3096 			.rates = WM8994_RATES,
3097 			.formats = WM8994_FORMATS,
3098 			.sig_bits = 24,
3099 		},
3100 		.capture = {
3101 			.stream_name = "AIF1 Capture",
3102 			.channels_min = 1,
3103 			.channels_max = 2,
3104 			.rates = WM8994_RATES,
3105 			.formats = WM8994_FORMATS,
3106 			.sig_bits = 24,
3107 		 },
3108 		.ops = &wm8994_aif1_dai_ops,
3109 	},
3110 	{
3111 		.name = "wm8994-aif2",
3112 		.id = 2,
3113 		.playback = {
3114 			.stream_name = "AIF2 Playback",
3115 			.channels_min = 1,
3116 			.channels_max = 2,
3117 			.rates = WM8994_RATES,
3118 			.formats = WM8994_FORMATS,
3119 			.sig_bits = 24,
3120 		},
3121 		.capture = {
3122 			.stream_name = "AIF2 Capture",
3123 			.channels_min = 1,
3124 			.channels_max = 2,
3125 			.rates = WM8994_RATES,
3126 			.formats = WM8994_FORMATS,
3127 			.sig_bits = 24,
3128 		},
3129 		.probe = wm8994_aif2_probe,
3130 		.ops = &wm8994_aif2_dai_ops,
3131 	},
3132 	{
3133 		.name = "wm8994-aif3",
3134 		.id = 3,
3135 		.playback = {
3136 			.stream_name = "AIF3 Playback",
3137 			.channels_min = 1,
3138 			.channels_max = 2,
3139 			.rates = WM8994_RATES,
3140 			.formats = WM8994_FORMATS,
3141 			.sig_bits = 24,
3142 		},
3143 		.capture = {
3144 			.stream_name = "AIF3 Capture",
3145 			.channels_min = 1,
3146 			.channels_max = 2,
3147 			.rates = WM8994_RATES,
3148 			.formats = WM8994_FORMATS,
3149 			.sig_bits = 24,
3150 		 },
3151 		.ops = &wm8994_aif3_dai_ops,
3152 	}
3153 };
3154 
3155 #ifdef CONFIG_PM
3156 static int wm8994_component_suspend(struct snd_soc_component *component)
3157 {
3158 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
3159 	int i, ret;
3160 
3161 	for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
3162 		memcpy(&wm8994->fll_suspend[i], &wm8994->fll[i],
3163 		       sizeof(struct wm8994_fll_config));
3164 		ret = _wm8994_set_fll(component, i + 1, 0, 0, 0);
3165 		if (ret < 0)
3166 			dev_warn(component->dev, "Failed to stop FLL%d: %d\n",
3167 				 i + 1, ret);
3168 	}
3169 
3170 	snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF);
3171 
3172 	return 0;
3173 }
3174 
3175 static int wm8994_component_resume(struct snd_soc_component *component)
3176 {
3177 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
3178 	int i, ret;
3179 
3180 	for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
3181 		if (!wm8994->fll_suspend[i].out)
3182 			continue;
3183 
3184 		ret = _wm8994_set_fll(component, i + 1,
3185 				     wm8994->fll_suspend[i].src,
3186 				     wm8994->fll_suspend[i].in,
3187 				     wm8994->fll_suspend[i].out);
3188 		if (ret < 0)
3189 			dev_warn(component->dev, "Failed to restore FLL%d: %d\n",
3190 				 i + 1, ret);
3191 	}
3192 
3193 	return 0;
3194 }
3195 #else
3196 #define wm8994_component_suspend NULL
3197 #define wm8994_component_resume NULL
3198 #endif
3199 
3200 static void wm8994_handle_retune_mobile_pdata(struct wm8994_priv *wm8994)
3201 {
3202 	struct snd_soc_component *component = wm8994->hubs.component;
3203 	struct wm8994 *control = wm8994->wm8994;
3204 	struct wm8994_pdata *pdata = &control->pdata;
3205 	struct snd_kcontrol_new controls[] = {
3206 		SOC_ENUM_EXT("AIF1.1 EQ Mode",
3207 			     wm8994->retune_mobile_enum,
3208 			     wm8994_get_retune_mobile_enum,
3209 			     wm8994_put_retune_mobile_enum),
3210 		SOC_ENUM_EXT("AIF1.2 EQ Mode",
3211 			     wm8994->retune_mobile_enum,
3212 			     wm8994_get_retune_mobile_enum,
3213 			     wm8994_put_retune_mobile_enum),
3214 		SOC_ENUM_EXT("AIF2 EQ Mode",
3215 			     wm8994->retune_mobile_enum,
3216 			     wm8994_get_retune_mobile_enum,
3217 			     wm8994_put_retune_mobile_enum),
3218 	};
3219 	int ret, i, j;
3220 	const char **t;
3221 
3222 	/* We need an array of texts for the enum API but the number
3223 	 * of texts is likely to be less than the number of
3224 	 * configurations due to the sample rate dependency of the
3225 	 * configurations. */
3226 	wm8994->num_retune_mobile_texts = 0;
3227 	wm8994->retune_mobile_texts = NULL;
3228 	for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
3229 		for (j = 0; j < wm8994->num_retune_mobile_texts; j++) {
3230 			if (strcmp(pdata->retune_mobile_cfgs[i].name,
3231 				   wm8994->retune_mobile_texts[j]) == 0)
3232 				break;
3233 		}
3234 
3235 		if (j != wm8994->num_retune_mobile_texts)
3236 			continue;
3237 
3238 		/* Expand the array... */
3239 		t = krealloc(wm8994->retune_mobile_texts,
3240 			     sizeof(char *) *
3241 			     (wm8994->num_retune_mobile_texts + 1),
3242 			     GFP_KERNEL);
3243 		if (t == NULL)
3244 			continue;
3245 
3246 		/* ...store the new entry... */
3247 		t[wm8994->num_retune_mobile_texts] =
3248 			pdata->retune_mobile_cfgs[i].name;
3249 
3250 		/* ...and remember the new version. */
3251 		wm8994->num_retune_mobile_texts++;
3252 		wm8994->retune_mobile_texts = t;
3253 	}
3254 
3255 	dev_dbg(component->dev, "Allocated %d unique ReTune Mobile names\n",
3256 		wm8994->num_retune_mobile_texts);
3257 
3258 	wm8994->retune_mobile_enum.items = wm8994->num_retune_mobile_texts;
3259 	wm8994->retune_mobile_enum.texts = wm8994->retune_mobile_texts;
3260 
3261 	ret = snd_soc_add_component_controls(wm8994->hubs.component, controls,
3262 				   ARRAY_SIZE(controls));
3263 	if (ret != 0)
3264 		dev_err(wm8994->hubs.component->dev,
3265 			"Failed to add ReTune Mobile controls: %d\n", ret);
3266 }
3267 
3268 static void wm8994_handle_pdata(struct wm8994_priv *wm8994)
3269 {
3270 	struct snd_soc_component *component = wm8994->hubs.component;
3271 	struct wm8994 *control = wm8994->wm8994;
3272 	struct wm8994_pdata *pdata = &control->pdata;
3273 	int ret, i;
3274 
3275 	if (!pdata)
3276 		return;
3277 
3278 	wm_hubs_handle_analogue_pdata(component, pdata->lineout1_diff,
3279 				      pdata->lineout2_diff,
3280 				      pdata->lineout1fb,
3281 				      pdata->lineout2fb,
3282 				      pdata->jd_scthr,
3283 				      pdata->jd_thr,
3284 				      pdata->micb1_delay,
3285 				      pdata->micb2_delay,
3286 				      pdata->micbias1_lvl,
3287 				      pdata->micbias2_lvl);
3288 
3289 	dev_dbg(component->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
3290 
3291 	if (pdata->num_drc_cfgs) {
3292 		struct snd_kcontrol_new controls[] = {
3293 			SOC_ENUM_EXT("AIF1DRC1 Mode", wm8994->drc_enum,
3294 				     wm8994_get_drc_enum, wm8994_put_drc_enum),
3295 			SOC_ENUM_EXT("AIF1DRC2 Mode", wm8994->drc_enum,
3296 				     wm8994_get_drc_enum, wm8994_put_drc_enum),
3297 			SOC_ENUM_EXT("AIF2DRC Mode", wm8994->drc_enum,
3298 				     wm8994_get_drc_enum, wm8994_put_drc_enum),
3299 		};
3300 
3301 		/* We need an array of texts for the enum API */
3302 		wm8994->drc_texts = devm_kcalloc(wm8994->hubs.component->dev,
3303 			    pdata->num_drc_cfgs, sizeof(char *), GFP_KERNEL);
3304 		if (!wm8994->drc_texts)
3305 			return;
3306 
3307 		for (i = 0; i < pdata->num_drc_cfgs; i++)
3308 			wm8994->drc_texts[i] = pdata->drc_cfgs[i].name;
3309 
3310 		wm8994->drc_enum.items = pdata->num_drc_cfgs;
3311 		wm8994->drc_enum.texts = wm8994->drc_texts;
3312 
3313 		ret = snd_soc_add_component_controls(wm8994->hubs.component, controls,
3314 					   ARRAY_SIZE(controls));
3315 		for (i = 0; i < WM8994_NUM_DRC; i++)
3316 			wm8994_set_drc(component, i);
3317 	} else {
3318 		ret = snd_soc_add_component_controls(wm8994->hubs.component,
3319 						 wm8994_drc_controls,
3320 						 ARRAY_SIZE(wm8994_drc_controls));
3321 	}
3322 
3323 	if (ret != 0)
3324 		dev_err(wm8994->hubs.component->dev,
3325 			"Failed to add DRC mode controls: %d\n", ret);
3326 
3327 
3328 	dev_dbg(component->dev, "%d ReTune Mobile configurations\n",
3329 		pdata->num_retune_mobile_cfgs);
3330 
3331 	if (pdata->num_retune_mobile_cfgs)
3332 		wm8994_handle_retune_mobile_pdata(wm8994);
3333 	else
3334 		snd_soc_add_component_controls(wm8994->hubs.component, wm8994_eq_controls,
3335 				     ARRAY_SIZE(wm8994_eq_controls));
3336 
3337 	for (i = 0; i < ARRAY_SIZE(pdata->micbias); i++) {
3338 		if (pdata->micbias[i]) {
3339 			snd_soc_component_write(component, WM8958_MICBIAS1 + i,
3340 				pdata->micbias[i] & 0xffff);
3341 		}
3342 	}
3343 }
3344 
3345 /**
3346  * wm8994_mic_detect - Enable microphone detection via the WM8994 IRQ
3347  *
3348  * @component:   WM8994 component
3349  * @jack:    jack to report detection events on
3350  * @micbias: microphone bias to detect on
3351  *
3352  * Enable microphone detection via IRQ on the WM8994.  If GPIOs are
3353  * being used to bring out signals to the processor then only platform
3354  * data configuration is needed for WM8994 and processor GPIOs should
3355  * be configured using snd_soc_jack_add_gpios() instead.
3356  *
3357  * Configuration of detection levels is available via the micbias1_lvl
3358  * and micbias2_lvl platform data members.
3359  */
3360 int wm8994_mic_detect(struct snd_soc_component *component, struct snd_soc_jack *jack,
3361 		      int micbias)
3362 {
3363 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
3364 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
3365 	struct wm8994_micdet *micdet;
3366 	struct wm8994 *control = wm8994->wm8994;
3367 	int reg, ret;
3368 
3369 	if (control->type != WM8994) {
3370 		dev_warn(component->dev, "Not a WM8994\n");
3371 		return -EINVAL;
3372 	}
3373 
3374 	switch (micbias) {
3375 	case 1:
3376 		micdet = &wm8994->micdet[0];
3377 		if (jack)
3378 			ret = snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
3379 		else
3380 			ret = snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
3381 		break;
3382 	case 2:
3383 		micdet = &wm8994->micdet[1];
3384 		if (jack)
3385 			ret = snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
3386 		else
3387 			ret = snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
3388 		break;
3389 	default:
3390 		dev_warn(component->dev, "Invalid MICBIAS %d\n", micbias);
3391 		return -EINVAL;
3392 	}
3393 
3394 	if (ret != 0)
3395 		dev_warn(component->dev, "Failed to configure MICBIAS%d: %d\n",
3396 			 micbias, ret);
3397 
3398 	dev_dbg(component->dev, "Configuring microphone detection on %d %p\n",
3399 		micbias, jack);
3400 
3401 	/* Store the configuration */
3402 	micdet->jack = jack;
3403 	micdet->detecting = true;
3404 
3405 	/* If either of the jacks is set up then enable detection */
3406 	if (wm8994->micdet[0].jack || wm8994->micdet[1].jack)
3407 		reg = WM8994_MICD_ENA;
3408 	else
3409 		reg = 0;
3410 
3411 	snd_soc_component_update_bits(component, WM8994_MICBIAS, WM8994_MICD_ENA, reg);
3412 
3413 	/* enable MICDET and MICSHRT deboune */
3414 	snd_soc_component_update_bits(component, WM8994_IRQ_DEBOUNCE,
3415 			    WM8994_MIC1_DET_DB_MASK | WM8994_MIC1_SHRT_DB_MASK |
3416 			    WM8994_MIC2_DET_DB_MASK | WM8994_MIC2_SHRT_DB_MASK,
3417 			    WM8994_MIC1_DET_DB | WM8994_MIC1_SHRT_DB);
3418 
3419 	snd_soc_dapm_sync(dapm);
3420 
3421 	return 0;
3422 }
3423 EXPORT_SYMBOL_GPL(wm8994_mic_detect);
3424 
3425 static void wm8994_mic_work(struct work_struct *work)
3426 {
3427 	struct wm8994_priv *priv = container_of(work,
3428 						struct wm8994_priv,
3429 						mic_work.work);
3430 	struct regmap *regmap = priv->wm8994->regmap;
3431 	struct device *dev = priv->wm8994->dev;
3432 	unsigned int reg;
3433 	int ret;
3434 	int report;
3435 
3436 	pm_runtime_get_sync(dev);
3437 
3438 	ret = regmap_read(regmap, WM8994_INTERRUPT_RAW_STATUS_2, &reg);
3439 	if (ret < 0) {
3440 		dev_err(dev, "Failed to read microphone status: %d\n",
3441 			ret);
3442 		pm_runtime_put(dev);
3443 		return;
3444 	}
3445 
3446 	dev_dbg(dev, "Microphone status: %x\n", reg);
3447 
3448 	report = 0;
3449 	if (reg & WM8994_MIC1_DET_STS) {
3450 		if (priv->micdet[0].detecting)
3451 			report = SND_JACK_HEADSET;
3452 	}
3453 	if (reg & WM8994_MIC1_SHRT_STS) {
3454 		if (priv->micdet[0].detecting)
3455 			report = SND_JACK_HEADPHONE;
3456 		else
3457 			report |= SND_JACK_BTN_0;
3458 	}
3459 	if (report)
3460 		priv->micdet[0].detecting = false;
3461 	else
3462 		priv->micdet[0].detecting = true;
3463 
3464 	snd_soc_jack_report(priv->micdet[0].jack, report,
3465 			    SND_JACK_HEADSET | SND_JACK_BTN_0);
3466 
3467 	report = 0;
3468 	if (reg & WM8994_MIC2_DET_STS) {
3469 		if (priv->micdet[1].detecting)
3470 			report = SND_JACK_HEADSET;
3471 	}
3472 	if (reg & WM8994_MIC2_SHRT_STS) {
3473 		if (priv->micdet[1].detecting)
3474 			report = SND_JACK_HEADPHONE;
3475 		else
3476 			report |= SND_JACK_BTN_0;
3477 	}
3478 	if (report)
3479 		priv->micdet[1].detecting = false;
3480 	else
3481 		priv->micdet[1].detecting = true;
3482 
3483 	snd_soc_jack_report(priv->micdet[1].jack, report,
3484 			    SND_JACK_HEADSET | SND_JACK_BTN_0);
3485 
3486 	pm_runtime_put(dev);
3487 }
3488 
3489 static irqreturn_t wm8994_mic_irq(int irq, void *data)
3490 {
3491 	struct wm8994_priv *priv = data;
3492 	struct snd_soc_component *component = priv->hubs.component;
3493 
3494 #ifndef CONFIG_SND_SOC_WM8994_MODULE
3495 	trace_snd_soc_jack_irq(dev_name(component->dev));
3496 #endif
3497 
3498 	pm_wakeup_event(component->dev, 300);
3499 
3500 	queue_delayed_work(system_power_efficient_wq,
3501 			   &priv->mic_work, msecs_to_jiffies(250));
3502 
3503 	return IRQ_HANDLED;
3504 }
3505 
3506 /* Should be called with accdet_lock held */
3507 static void wm1811_micd_stop(struct snd_soc_component *component)
3508 {
3509 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
3510 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
3511 
3512 	if (!wm8994->jackdet)
3513 		return;
3514 
3515 	snd_soc_component_update_bits(component, WM8958_MIC_DETECT_1, WM8958_MICD_ENA, 0);
3516 
3517 	wm1811_jackdet_set_mode(component, WM1811_JACKDET_MODE_JACK);
3518 
3519 	if (wm8994->wm8994->pdata.jd_ext_cap)
3520 		snd_soc_dapm_disable_pin(dapm, "MICBIAS2");
3521 }
3522 
3523 static void wm8958_button_det(struct snd_soc_component *component, u16 status)
3524 {
3525 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
3526 	int report;
3527 
3528 	report = 0;
3529 	if (status & 0x4)
3530 		report |= SND_JACK_BTN_0;
3531 
3532 	if (status & 0x8)
3533 		report |= SND_JACK_BTN_1;
3534 
3535 	if (status & 0x10)
3536 		report |= SND_JACK_BTN_2;
3537 
3538 	if (status & 0x20)
3539 		report |= SND_JACK_BTN_3;
3540 
3541 	if (status & 0x40)
3542 		report |= SND_JACK_BTN_4;
3543 
3544 	if (status & 0x80)
3545 		report |= SND_JACK_BTN_5;
3546 
3547 	snd_soc_jack_report(wm8994->micdet[0].jack, report,
3548 			    wm8994->btn_mask);
3549 }
3550 
3551 static void wm8958_open_circuit_work(struct work_struct *work)
3552 {
3553 	struct wm8994_priv *wm8994 = container_of(work,
3554 						  struct wm8994_priv,
3555 						  open_circuit_work.work);
3556 	struct device *dev = wm8994->wm8994->dev;
3557 
3558 	mutex_lock(&wm8994->accdet_lock);
3559 
3560 	wm1811_micd_stop(wm8994->hubs.component);
3561 
3562 	dev_dbg(dev, "Reporting open circuit\n");
3563 
3564 	wm8994->jack_mic = false;
3565 	wm8994->mic_detecting = true;
3566 
3567 	wm8958_micd_set_rate(wm8994->hubs.component);
3568 
3569 	snd_soc_jack_report(wm8994->micdet[0].jack, 0,
3570 			    wm8994->btn_mask |
3571 			    SND_JACK_HEADSET);
3572 
3573 	mutex_unlock(&wm8994->accdet_lock);
3574 }
3575 
3576 static void wm8958_mic_id(void *data, u16 status)
3577 {
3578 	struct snd_soc_component *component = data;
3579 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
3580 
3581 	/* Either nothing present or just starting detection */
3582 	if (!(status & WM8958_MICD_STS)) {
3583 		/* If nothing present then clear our statuses */
3584 		dev_dbg(component->dev, "Detected open circuit\n");
3585 
3586 		queue_delayed_work(system_power_efficient_wq,
3587 				   &wm8994->open_circuit_work,
3588 				   msecs_to_jiffies(2500));
3589 		return;
3590 	}
3591 
3592 	/* If the measurement is showing a high impedence we've got a
3593 	 * microphone.
3594 	 */
3595 	if (status & 0x600) {
3596 		dev_dbg(component->dev, "Detected microphone\n");
3597 
3598 		wm8994->mic_detecting = false;
3599 		wm8994->jack_mic = true;
3600 
3601 		wm8958_micd_set_rate(component);
3602 
3603 		snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADSET,
3604 				    SND_JACK_HEADSET);
3605 	}
3606 
3607 
3608 	if (status & 0xfc) {
3609 		dev_dbg(component->dev, "Detected headphone\n");
3610 		wm8994->mic_detecting = false;
3611 
3612 		wm8958_micd_set_rate(component);
3613 
3614 		/* If we have jackdet that will detect removal */
3615 		wm1811_micd_stop(component);
3616 
3617 		snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADPHONE,
3618 				    SND_JACK_HEADSET);
3619 	}
3620 }
3621 
3622 /* Deferred mic detection to allow for extra settling time */
3623 static void wm1811_mic_work(struct work_struct *work)
3624 {
3625 	struct wm8994_priv *wm8994 = container_of(work, struct wm8994_priv,
3626 						  mic_work.work);
3627 	struct wm8994 *control = wm8994->wm8994;
3628 	struct snd_soc_component *component = wm8994->hubs.component;
3629 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
3630 
3631 	pm_runtime_get_sync(component->dev);
3632 
3633 	/* If required for an external cap force MICBIAS on */
3634 	if (control->pdata.jd_ext_cap) {
3635 		snd_soc_dapm_force_enable_pin(dapm, "MICBIAS2");
3636 		snd_soc_dapm_sync(dapm);
3637 	}
3638 
3639 	mutex_lock(&wm8994->accdet_lock);
3640 
3641 	dev_dbg(component->dev, "Starting mic detection\n");
3642 
3643 	/* Use a user-supplied callback if we have one */
3644 	if (wm8994->micd_cb) {
3645 		wm8994->micd_cb(wm8994->micd_cb_data);
3646 	} else {
3647 		/*
3648 		 * Start off measument of microphone impedence to find out
3649 		 * what's actually there.
3650 		 */
3651 		wm8994->mic_detecting = true;
3652 		wm1811_jackdet_set_mode(component, WM1811_JACKDET_MODE_MIC);
3653 
3654 		snd_soc_component_update_bits(component, WM8958_MIC_DETECT_1,
3655 				    WM8958_MICD_ENA, WM8958_MICD_ENA);
3656 	}
3657 
3658 	mutex_unlock(&wm8994->accdet_lock);
3659 
3660 	pm_runtime_put(component->dev);
3661 }
3662 
3663 static irqreturn_t wm1811_jackdet_irq(int irq, void *data)
3664 {
3665 	struct wm8994_priv *wm8994 = data;
3666 	struct wm8994 *control = wm8994->wm8994;
3667 	struct snd_soc_component *component = wm8994->hubs.component;
3668 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
3669 	int reg, delay;
3670 	bool present;
3671 
3672 	pm_runtime_get_sync(component->dev);
3673 
3674 	cancel_delayed_work_sync(&wm8994->mic_complete_work);
3675 
3676 	mutex_lock(&wm8994->accdet_lock);
3677 
3678 	reg = snd_soc_component_read32(component, WM1811_JACKDET_CTRL);
3679 	if (reg < 0) {
3680 		dev_err(component->dev, "Failed to read jack status: %d\n", reg);
3681 		mutex_unlock(&wm8994->accdet_lock);
3682 		pm_runtime_put(component->dev);
3683 		return IRQ_NONE;
3684 	}
3685 
3686 	dev_dbg(component->dev, "JACKDET %x\n", reg);
3687 
3688 	present = reg & WM1811_JACKDET_LVL;
3689 
3690 	if (present) {
3691 		dev_dbg(component->dev, "Jack detected\n");
3692 
3693 		wm8958_micd_set_rate(component);
3694 
3695 		snd_soc_component_update_bits(component, WM8958_MICBIAS2,
3696 				    WM8958_MICB2_DISCH, 0);
3697 
3698 		/* Disable debounce while inserted */
3699 		snd_soc_component_update_bits(component, WM1811_JACKDET_CTRL,
3700 				    WM1811_JACKDET_DB, 0);
3701 
3702 		delay = control->pdata.micdet_delay;
3703 		queue_delayed_work(system_power_efficient_wq,
3704 				   &wm8994->mic_work,
3705 				   msecs_to_jiffies(delay));
3706 	} else {
3707 		dev_dbg(component->dev, "Jack not detected\n");
3708 
3709 		cancel_delayed_work_sync(&wm8994->mic_work);
3710 
3711 		snd_soc_component_update_bits(component, WM8958_MICBIAS2,
3712 				    WM8958_MICB2_DISCH, WM8958_MICB2_DISCH);
3713 
3714 		/* Enable debounce while removed */
3715 		snd_soc_component_update_bits(component, WM1811_JACKDET_CTRL,
3716 				    WM1811_JACKDET_DB, WM1811_JACKDET_DB);
3717 
3718 		wm8994->mic_detecting = false;
3719 		wm8994->jack_mic = false;
3720 		snd_soc_component_update_bits(component, WM8958_MIC_DETECT_1,
3721 				    WM8958_MICD_ENA, 0);
3722 		wm1811_jackdet_set_mode(component, WM1811_JACKDET_MODE_JACK);
3723 	}
3724 
3725 	mutex_unlock(&wm8994->accdet_lock);
3726 
3727 	/* Turn off MICBIAS if it was on for an external cap */
3728 	if (control->pdata.jd_ext_cap && !present)
3729 		snd_soc_dapm_disable_pin(dapm, "MICBIAS2");
3730 
3731 	if (present)
3732 		snd_soc_jack_report(wm8994->micdet[0].jack,
3733 				    SND_JACK_MECHANICAL, SND_JACK_MECHANICAL);
3734 	else
3735 		snd_soc_jack_report(wm8994->micdet[0].jack, 0,
3736 				    SND_JACK_MECHANICAL | SND_JACK_HEADSET |
3737 				    wm8994->btn_mask);
3738 
3739 	/* Since we only report deltas force an update, ensures we
3740 	 * avoid bootstrapping issues with the core. */
3741 	snd_soc_jack_report(wm8994->micdet[0].jack, 0, 0);
3742 
3743 	pm_runtime_put(component->dev);
3744 	return IRQ_HANDLED;
3745 }
3746 
3747 static void wm1811_jackdet_bootstrap(struct work_struct *work)
3748 {
3749 	struct wm8994_priv *wm8994 = container_of(work,
3750 						struct wm8994_priv,
3751 						jackdet_bootstrap.work);
3752 	wm1811_jackdet_irq(0, wm8994);
3753 }
3754 
3755 /**
3756  * wm8958_mic_detect - Enable microphone detection via the WM8958 IRQ
3757  *
3758  * @component:   WM8958 component
3759  * @jack:    jack to report detection events on
3760  *
3761  * Enable microphone detection functionality for the WM8958.  By
3762  * default simple detection which supports the detection of up to 6
3763  * buttons plus video and microphone functionality is supported.
3764  *
3765  * The WM8958 has an advanced jack detection facility which is able to
3766  * support complex accessory detection, especially when used in
3767  * conjunction with external circuitry.  In order to provide maximum
3768  * flexiblity a callback is provided which allows a completely custom
3769  * detection algorithm.
3770  */
3771 int wm8958_mic_detect(struct snd_soc_component *component, struct snd_soc_jack *jack,
3772 		      wm1811_micdet_cb det_cb, void *det_cb_data,
3773 		      wm1811_mic_id_cb id_cb, void *id_cb_data)
3774 {
3775 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
3776 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
3777 	struct wm8994 *control = wm8994->wm8994;
3778 	u16 micd_lvl_sel;
3779 
3780 	switch (control->type) {
3781 	case WM1811:
3782 	case WM8958:
3783 		break;
3784 	default:
3785 		return -EINVAL;
3786 	}
3787 
3788 	if (jack) {
3789 		snd_soc_dapm_force_enable_pin(dapm, "CLK_SYS");
3790 		snd_soc_dapm_sync(dapm);
3791 
3792 		wm8994->micdet[0].jack = jack;
3793 
3794 		if (det_cb) {
3795 			wm8994->micd_cb = det_cb;
3796 			wm8994->micd_cb_data = det_cb_data;
3797 		} else {
3798 			wm8994->mic_detecting = true;
3799 			wm8994->jack_mic = false;
3800 		}
3801 
3802 		if (id_cb) {
3803 			wm8994->mic_id_cb = id_cb;
3804 			wm8994->mic_id_cb_data = id_cb_data;
3805 		} else {
3806 			wm8994->mic_id_cb = wm8958_mic_id;
3807 			wm8994->mic_id_cb_data = component;
3808 		}
3809 
3810 		wm8958_micd_set_rate(component);
3811 
3812 		/* Detect microphones and short circuits by default */
3813 		if (control->pdata.micd_lvl_sel)
3814 			micd_lvl_sel = control->pdata.micd_lvl_sel;
3815 		else
3816 			micd_lvl_sel = 0x41;
3817 
3818 		wm8994->btn_mask = SND_JACK_BTN_0 | SND_JACK_BTN_1 |
3819 			SND_JACK_BTN_2 | SND_JACK_BTN_3 |
3820 			SND_JACK_BTN_4 | SND_JACK_BTN_5;
3821 
3822 		snd_soc_component_update_bits(component, WM8958_MIC_DETECT_2,
3823 				    WM8958_MICD_LVL_SEL_MASK, micd_lvl_sel);
3824 
3825 		WARN_ON(snd_soc_component_get_bias_level(component) > SND_SOC_BIAS_STANDBY);
3826 
3827 		/*
3828 		 * If we can use jack detection start off with that,
3829 		 * otherwise jump straight to microphone detection.
3830 		 */
3831 		if (wm8994->jackdet) {
3832 			/* Disable debounce for the initial detect */
3833 			snd_soc_component_update_bits(component, WM1811_JACKDET_CTRL,
3834 					    WM1811_JACKDET_DB, 0);
3835 
3836 			snd_soc_component_update_bits(component, WM8958_MICBIAS2,
3837 					    WM8958_MICB2_DISCH,
3838 					    WM8958_MICB2_DISCH);
3839 			snd_soc_component_update_bits(component, WM8994_LDO_1,
3840 					    WM8994_LDO1_DISCH, 0);
3841 			wm1811_jackdet_set_mode(component,
3842 						WM1811_JACKDET_MODE_JACK);
3843 		} else {
3844 			snd_soc_component_update_bits(component, WM8958_MIC_DETECT_1,
3845 					    WM8958_MICD_ENA, WM8958_MICD_ENA);
3846 		}
3847 
3848 	} else {
3849 		snd_soc_component_update_bits(component, WM8958_MIC_DETECT_1,
3850 				    WM8958_MICD_ENA, 0);
3851 		wm1811_jackdet_set_mode(component, WM1811_JACKDET_MODE_NONE);
3852 		snd_soc_dapm_disable_pin(dapm, "CLK_SYS");
3853 		snd_soc_dapm_sync(dapm);
3854 	}
3855 
3856 	return 0;
3857 }
3858 EXPORT_SYMBOL_GPL(wm8958_mic_detect);
3859 
3860 static void wm8958_mic_work(struct work_struct *work)
3861 {
3862 	struct wm8994_priv *wm8994 = container_of(work,
3863 						  struct wm8994_priv,
3864 						  mic_complete_work.work);
3865 	struct snd_soc_component *component = wm8994->hubs.component;
3866 
3867 	pm_runtime_get_sync(component->dev);
3868 
3869 	mutex_lock(&wm8994->accdet_lock);
3870 
3871 	wm8994->mic_id_cb(wm8994->mic_id_cb_data, wm8994->mic_status);
3872 
3873 	mutex_unlock(&wm8994->accdet_lock);
3874 
3875 	pm_runtime_put(component->dev);
3876 }
3877 
3878 static irqreturn_t wm8958_mic_irq(int irq, void *data)
3879 {
3880 	struct wm8994_priv *wm8994 = data;
3881 	struct snd_soc_component *component = wm8994->hubs.component;
3882 	int reg, count, ret, id_delay;
3883 
3884 	/*
3885 	 * Jack detection may have detected a removal simulataneously
3886 	 * with an update of the MICDET status; if so it will have
3887 	 * stopped detection and we can ignore this interrupt.
3888 	 */
3889 	if (!(snd_soc_component_read32(component, WM8958_MIC_DETECT_1) & WM8958_MICD_ENA))
3890 		return IRQ_HANDLED;
3891 
3892 	cancel_delayed_work_sync(&wm8994->mic_complete_work);
3893 	cancel_delayed_work_sync(&wm8994->open_circuit_work);
3894 
3895 	pm_runtime_get_sync(component->dev);
3896 
3897 	/* We may occasionally read a detection without an impedence
3898 	 * range being provided - if that happens loop again.
3899 	 */
3900 	count = 10;
3901 	do {
3902 		reg = snd_soc_component_read32(component, WM8958_MIC_DETECT_3);
3903 		if (reg < 0) {
3904 			dev_err(component->dev,
3905 				"Failed to read mic detect status: %d\n",
3906 				reg);
3907 			pm_runtime_put(component->dev);
3908 			return IRQ_NONE;
3909 		}
3910 
3911 		if (!(reg & WM8958_MICD_VALID)) {
3912 			dev_dbg(component->dev, "Mic detect data not valid\n");
3913 			goto out;
3914 		}
3915 
3916 		if (!(reg & WM8958_MICD_STS) || (reg & WM8958_MICD_LVL_MASK))
3917 			break;
3918 
3919 		msleep(1);
3920 	} while (count--);
3921 
3922 	if (count == 0)
3923 		dev_warn(component->dev, "No impedance range reported for jack\n");
3924 
3925 #ifndef CONFIG_SND_SOC_WM8994_MODULE
3926 	trace_snd_soc_jack_irq(dev_name(component->dev));
3927 #endif
3928 
3929 	/* Avoid a transient report when the accessory is being removed */
3930 	if (wm8994->jackdet) {
3931 		ret = snd_soc_component_read32(component, WM1811_JACKDET_CTRL);
3932 		if (ret < 0) {
3933 			dev_err(component->dev, "Failed to read jack status: %d\n",
3934 				ret);
3935 		} else if (!(ret & WM1811_JACKDET_LVL)) {
3936 			dev_dbg(component->dev, "Ignoring removed jack\n");
3937 			goto out;
3938 		}
3939 	} else if (!(reg & WM8958_MICD_STS)) {
3940 		snd_soc_jack_report(wm8994->micdet[0].jack, 0,
3941 				    SND_JACK_MECHANICAL | SND_JACK_HEADSET |
3942 				    wm8994->btn_mask);
3943 		wm8994->mic_detecting = true;
3944 		goto out;
3945 	}
3946 
3947 	wm8994->mic_status = reg;
3948 	id_delay = wm8994->wm8994->pdata.mic_id_delay;
3949 
3950 	if (wm8994->mic_detecting)
3951 		queue_delayed_work(system_power_efficient_wq,
3952 				   &wm8994->mic_complete_work,
3953 				   msecs_to_jiffies(id_delay));
3954 	else
3955 		wm8958_button_det(component, reg);
3956 
3957 out:
3958 	pm_runtime_put(component->dev);
3959 	return IRQ_HANDLED;
3960 }
3961 
3962 static irqreturn_t wm8994_fifo_error(int irq, void *data)
3963 {
3964 	struct snd_soc_component *component = data;
3965 
3966 	dev_err(component->dev, "FIFO error\n");
3967 
3968 	return IRQ_HANDLED;
3969 }
3970 
3971 static irqreturn_t wm8994_temp_warn(int irq, void *data)
3972 {
3973 	struct snd_soc_component *component = data;
3974 
3975 	dev_err(component->dev, "Thermal warning\n");
3976 
3977 	return IRQ_HANDLED;
3978 }
3979 
3980 static irqreturn_t wm8994_temp_shut(int irq, void *data)
3981 {
3982 	struct snd_soc_component *component = data;
3983 
3984 	dev_crit(component->dev, "Thermal shutdown\n");
3985 
3986 	return IRQ_HANDLED;
3987 }
3988 
3989 static int wm8994_component_probe(struct snd_soc_component *component)
3990 {
3991 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
3992 	struct wm8994 *control = dev_get_drvdata(component->dev->parent);
3993 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
3994 	unsigned int reg;
3995 	int ret, i;
3996 
3997 	snd_soc_component_init_regmap(component, control->regmap);
3998 
3999 	wm8994->hubs.component = component;
4000 
4001 	mutex_init(&wm8994->accdet_lock);
4002 	INIT_DELAYED_WORK(&wm8994->jackdet_bootstrap,
4003 			  wm1811_jackdet_bootstrap);
4004 	INIT_DELAYED_WORK(&wm8994->open_circuit_work,
4005 			  wm8958_open_circuit_work);
4006 
4007 	switch (control->type) {
4008 	case WM8994:
4009 		INIT_DELAYED_WORK(&wm8994->mic_work, wm8994_mic_work);
4010 		break;
4011 	case WM1811:
4012 		INIT_DELAYED_WORK(&wm8994->mic_work, wm1811_mic_work);
4013 		break;
4014 	default:
4015 		break;
4016 	}
4017 
4018 	INIT_DELAYED_WORK(&wm8994->mic_complete_work, wm8958_mic_work);
4019 
4020 	for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
4021 		init_completion(&wm8994->fll_locked[i]);
4022 
4023 	wm8994->micdet_irq = control->pdata.micdet_irq;
4024 
4025 	/* By default use idle_bias_off, will override for WM8994 */
4026 	dapm->idle_bias_off = 1;
4027 
4028 	/* Set revision-specific configuration */
4029 	switch (control->type) {
4030 	case WM8994:
4031 		/* Single ended line outputs should have VMID on. */
4032 		if (!control->pdata.lineout1_diff ||
4033 		    !control->pdata.lineout2_diff)
4034 			dapm->idle_bias_off = 0;
4035 
4036 		switch (control->revision) {
4037 		case 2:
4038 		case 3:
4039 			wm8994->hubs.dcs_codes_l = -5;
4040 			wm8994->hubs.dcs_codes_r = -5;
4041 			wm8994->hubs.hp_startup_mode = 1;
4042 			wm8994->hubs.dcs_readback_mode = 1;
4043 			wm8994->hubs.series_startup = 1;
4044 			break;
4045 		default:
4046 			wm8994->hubs.dcs_readback_mode = 2;
4047 			break;
4048 		}
4049 		break;
4050 
4051 	case WM8958:
4052 		wm8994->hubs.dcs_readback_mode = 1;
4053 		wm8994->hubs.hp_startup_mode = 1;
4054 
4055 		switch (control->revision) {
4056 		case 0:
4057 			break;
4058 		default:
4059 			wm8994->fll_byp = true;
4060 			break;
4061 		}
4062 		break;
4063 
4064 	case WM1811:
4065 		wm8994->hubs.dcs_readback_mode = 2;
4066 		wm8994->hubs.no_series_update = 1;
4067 		wm8994->hubs.hp_startup_mode = 1;
4068 		wm8994->hubs.no_cache_dac_hp_direct = true;
4069 		wm8994->fll_byp = true;
4070 
4071 		wm8994->hubs.dcs_codes_l = -9;
4072 		wm8994->hubs.dcs_codes_r = -7;
4073 
4074 		snd_soc_component_update_bits(component, WM8994_ANALOGUE_HP_1,
4075 				    WM1811_HPOUT1_ATTN, WM1811_HPOUT1_ATTN);
4076 		break;
4077 
4078 	default:
4079 		break;
4080 	}
4081 
4082 	wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR,
4083 			   wm8994_fifo_error, "FIFO error", component);
4084 	wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN,
4085 			   wm8994_temp_warn, "Thermal warning", component);
4086 	wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT,
4087 			   wm8994_temp_shut, "Thermal shutdown", component);
4088 
4089 	switch (control->type) {
4090 	case WM8994:
4091 		if (wm8994->micdet_irq)
4092 			ret = request_threaded_irq(wm8994->micdet_irq, NULL,
4093 						   wm8994_mic_irq,
4094 						   IRQF_TRIGGER_RISING |
4095 						   IRQF_ONESHOT,
4096 						   "Mic1 detect",
4097 						   wm8994);
4098 		 else
4099 			ret = wm8994_request_irq(wm8994->wm8994,
4100 					WM8994_IRQ_MIC1_DET,
4101 					wm8994_mic_irq, "Mic 1 detect",
4102 					wm8994);
4103 
4104 		if (ret != 0)
4105 			dev_warn(component->dev,
4106 				 "Failed to request Mic1 detect IRQ: %d\n",
4107 				 ret);
4108 
4109 
4110 		ret = wm8994_request_irq(wm8994->wm8994,
4111 					 WM8994_IRQ_MIC1_SHRT,
4112 					 wm8994_mic_irq, "Mic 1 short",
4113 					 wm8994);
4114 		if (ret != 0)
4115 			dev_warn(component->dev,
4116 				 "Failed to request Mic1 short IRQ: %d\n",
4117 				 ret);
4118 
4119 		ret = wm8994_request_irq(wm8994->wm8994,
4120 					 WM8994_IRQ_MIC2_DET,
4121 					 wm8994_mic_irq, "Mic 2 detect",
4122 					 wm8994);
4123 		if (ret != 0)
4124 			dev_warn(component->dev,
4125 				 "Failed to request Mic2 detect IRQ: %d\n",
4126 				 ret);
4127 
4128 		ret = wm8994_request_irq(wm8994->wm8994,
4129 					 WM8994_IRQ_MIC2_SHRT,
4130 					 wm8994_mic_irq, "Mic 2 short",
4131 					 wm8994);
4132 		if (ret != 0)
4133 			dev_warn(component->dev,
4134 				 "Failed to request Mic2 short IRQ: %d\n",
4135 				 ret);
4136 		break;
4137 
4138 	case WM8958:
4139 	case WM1811:
4140 		if (wm8994->micdet_irq) {
4141 			ret = request_threaded_irq(wm8994->micdet_irq, NULL,
4142 						   wm8958_mic_irq,
4143 						   IRQF_TRIGGER_RISING |
4144 						   IRQF_ONESHOT,
4145 						   "Mic detect",
4146 						   wm8994);
4147 			if (ret != 0)
4148 				dev_warn(component->dev,
4149 					 "Failed to request Mic detect IRQ: %d\n",
4150 					 ret);
4151 		} else {
4152 			wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_MIC1_DET,
4153 					   wm8958_mic_irq, "Mic detect",
4154 					   wm8994);
4155 		}
4156 	}
4157 
4158 	switch (control->type) {
4159 	case WM1811:
4160 		if (control->cust_id > 1 || control->revision > 1) {
4161 			ret = wm8994_request_irq(wm8994->wm8994,
4162 						 WM8994_IRQ_GPIO(6),
4163 						 wm1811_jackdet_irq, "JACKDET",
4164 						 wm8994);
4165 			if (ret == 0)
4166 				wm8994->jackdet = true;
4167 		}
4168 		break;
4169 	default:
4170 		break;
4171 	}
4172 
4173 	wm8994->fll_locked_irq = true;
4174 	for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++) {
4175 		ret = wm8994_request_irq(wm8994->wm8994,
4176 					 WM8994_IRQ_FLL1_LOCK + i,
4177 					 wm8994_fll_locked_irq, "FLL lock",
4178 					 &wm8994->fll_locked[i]);
4179 		if (ret != 0)
4180 			wm8994->fll_locked_irq = false;
4181 	}
4182 
4183 	/* Make sure we can read from the GPIOs if they're inputs */
4184 	pm_runtime_get_sync(component->dev);
4185 
4186 	/* Remember if AIFnLRCLK is configured as a GPIO.  This should be
4187 	 * configured on init - if a system wants to do this dynamically
4188 	 * at runtime we can deal with that then.
4189 	 */
4190 	ret = regmap_read(control->regmap, WM8994_GPIO_1, &reg);
4191 	if (ret < 0) {
4192 		dev_err(component->dev, "Failed to read GPIO1 state: %d\n", ret);
4193 		goto err_irq;
4194 	}
4195 	if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
4196 		wm8994->lrclk_shared[0] = 1;
4197 		wm8994_dai[0].symmetric_rates = 1;
4198 	} else {
4199 		wm8994->lrclk_shared[0] = 0;
4200 	}
4201 
4202 	ret = regmap_read(control->regmap, WM8994_GPIO_6, &reg);
4203 	if (ret < 0) {
4204 		dev_err(component->dev, "Failed to read GPIO6 state: %d\n", ret);
4205 		goto err_irq;
4206 	}
4207 	if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
4208 		wm8994->lrclk_shared[1] = 1;
4209 		wm8994_dai[1].symmetric_rates = 1;
4210 	} else {
4211 		wm8994->lrclk_shared[1] = 0;
4212 	}
4213 
4214 	pm_runtime_put(component->dev);
4215 
4216 	/* Latch volume update bits */
4217 	for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
4218 		snd_soc_component_update_bits(component, wm8994_vu_bits[i].reg,
4219 				    wm8994_vu_bits[i].mask,
4220 				    wm8994_vu_bits[i].mask);
4221 
4222 	/* Set the low bit of the 3D stereo depth so TLV matches */
4223 	snd_soc_component_update_bits(component, WM8994_AIF1_DAC1_FILTERS_2,
4224 			    1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT,
4225 			    1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT);
4226 	snd_soc_component_update_bits(component, WM8994_AIF1_DAC2_FILTERS_2,
4227 			    1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT,
4228 			    1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT);
4229 	snd_soc_component_update_bits(component, WM8994_AIF2_DAC_FILTERS_2,
4230 			    1 << WM8994_AIF2DAC_3D_GAIN_SHIFT,
4231 			    1 << WM8994_AIF2DAC_3D_GAIN_SHIFT);
4232 
4233 	/* Unconditionally enable AIF1 ADC TDM mode on chips which can
4234 	 * use this; it only affects behaviour on idle TDM clock
4235 	 * cycles. */
4236 	switch (control->type) {
4237 	case WM8994:
4238 	case WM8958:
4239 		snd_soc_component_update_bits(component, WM8994_AIF1_CONTROL_1,
4240 				    WM8994_AIF1ADC_TDM, WM8994_AIF1ADC_TDM);
4241 		break;
4242 	default:
4243 		break;
4244 	}
4245 
4246 	/* Put MICBIAS into bypass mode by default on newer devices */
4247 	switch (control->type) {
4248 	case WM8958:
4249 	case WM1811:
4250 		snd_soc_component_update_bits(component, WM8958_MICBIAS1,
4251 				    WM8958_MICB1_MODE, WM8958_MICB1_MODE);
4252 		snd_soc_component_update_bits(component, WM8958_MICBIAS2,
4253 				    WM8958_MICB2_MODE, WM8958_MICB2_MODE);
4254 		break;
4255 	default:
4256 		break;
4257 	}
4258 
4259 	wm8994->hubs.check_class_w_digital = wm8994_check_class_w_digital;
4260 	wm_hubs_update_class_w(component);
4261 
4262 	wm8994_handle_pdata(wm8994);
4263 
4264 	wm_hubs_add_analogue_controls(component);
4265 	snd_soc_add_component_controls(component, wm8994_snd_controls,
4266 			     ARRAY_SIZE(wm8994_snd_controls));
4267 	snd_soc_dapm_new_controls(dapm, wm8994_dapm_widgets,
4268 				  ARRAY_SIZE(wm8994_dapm_widgets));
4269 
4270 	switch (control->type) {
4271 	case WM8994:
4272 		snd_soc_dapm_new_controls(dapm, wm8994_specific_dapm_widgets,
4273 					  ARRAY_SIZE(wm8994_specific_dapm_widgets));
4274 		if (control->revision < 4) {
4275 			snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
4276 						  ARRAY_SIZE(wm8994_lateclk_revd_widgets));
4277 			snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
4278 						  ARRAY_SIZE(wm8994_adc_revd_widgets));
4279 			snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
4280 						  ARRAY_SIZE(wm8994_dac_revd_widgets));
4281 		} else {
4282 			snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
4283 						  ARRAY_SIZE(wm8994_lateclk_widgets));
4284 			snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
4285 						  ARRAY_SIZE(wm8994_adc_widgets));
4286 			snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
4287 						  ARRAY_SIZE(wm8994_dac_widgets));
4288 		}
4289 		break;
4290 	case WM8958:
4291 		snd_soc_add_component_controls(component, wm8958_snd_controls,
4292 				     ARRAY_SIZE(wm8958_snd_controls));
4293 		snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
4294 					  ARRAY_SIZE(wm8958_dapm_widgets));
4295 		if (control->revision < 1) {
4296 			snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
4297 						  ARRAY_SIZE(wm8994_lateclk_revd_widgets));
4298 			snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
4299 						  ARRAY_SIZE(wm8994_adc_revd_widgets));
4300 			snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
4301 						  ARRAY_SIZE(wm8994_dac_revd_widgets));
4302 		} else {
4303 			snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
4304 						  ARRAY_SIZE(wm8994_lateclk_widgets));
4305 			snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
4306 						  ARRAY_SIZE(wm8994_adc_widgets));
4307 			snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
4308 						  ARRAY_SIZE(wm8994_dac_widgets));
4309 		}
4310 		break;
4311 
4312 	case WM1811:
4313 		snd_soc_add_component_controls(component, wm8958_snd_controls,
4314 				     ARRAY_SIZE(wm8958_snd_controls));
4315 		snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
4316 					  ARRAY_SIZE(wm8958_dapm_widgets));
4317 		snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
4318 					  ARRAY_SIZE(wm8994_lateclk_widgets));
4319 		snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
4320 					  ARRAY_SIZE(wm8994_adc_widgets));
4321 		snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
4322 					  ARRAY_SIZE(wm8994_dac_widgets));
4323 		break;
4324 	}
4325 
4326 	wm_hubs_add_analogue_routes(component, 0, 0);
4327 	ret = wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
4328 				 wm_hubs_dcs_done, "DC servo done",
4329 				 &wm8994->hubs);
4330 	if (ret == 0)
4331 		wm8994->hubs.dcs_done_irq = true;
4332 	snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
4333 
4334 	switch (control->type) {
4335 	case WM8994:
4336 		snd_soc_dapm_add_routes(dapm, wm8994_intercon,
4337 					ARRAY_SIZE(wm8994_intercon));
4338 
4339 		if (control->revision < 4) {
4340 			snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
4341 						ARRAY_SIZE(wm8994_revd_intercon));
4342 			snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
4343 						ARRAY_SIZE(wm8994_lateclk_revd_intercon));
4344 		} else {
4345 			snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
4346 						ARRAY_SIZE(wm8994_lateclk_intercon));
4347 		}
4348 		break;
4349 	case WM8958:
4350 		if (control->revision < 1) {
4351 			snd_soc_dapm_add_routes(dapm, wm8994_intercon,
4352 						ARRAY_SIZE(wm8994_intercon));
4353 			snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
4354 						ARRAY_SIZE(wm8994_revd_intercon));
4355 			snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
4356 						ARRAY_SIZE(wm8994_lateclk_revd_intercon));
4357 		} else {
4358 			snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
4359 						ARRAY_SIZE(wm8994_lateclk_intercon));
4360 			snd_soc_dapm_add_routes(dapm, wm8958_intercon,
4361 						ARRAY_SIZE(wm8958_intercon));
4362 		}
4363 
4364 		wm8958_dsp2_init(component);
4365 		break;
4366 	case WM1811:
4367 		snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
4368 					ARRAY_SIZE(wm8994_lateclk_intercon));
4369 		snd_soc_dapm_add_routes(dapm, wm8958_intercon,
4370 					ARRAY_SIZE(wm8958_intercon));
4371 		break;
4372 	}
4373 
4374 	return 0;
4375 
4376 err_irq:
4377 	if (wm8994->jackdet)
4378 		wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);
4379 	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_SHRT, wm8994);
4380 	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET, wm8994);
4381 	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT, wm8994);
4382 	if (wm8994->micdet_irq)
4383 		free_irq(wm8994->micdet_irq, wm8994);
4384 	for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
4385 		wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
4386 				&wm8994->fll_locked[i]);
4387 	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
4388 			&wm8994->hubs);
4389 	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, component);
4390 	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, component);
4391 	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, component);
4392 
4393 	return ret;
4394 }
4395 
4396 static void wm8994_component_remove(struct snd_soc_component *component)
4397 {
4398 	struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
4399 	struct wm8994 *control = wm8994->wm8994;
4400 	int i;
4401 
4402 	for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
4403 		wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
4404 				&wm8994->fll_locked[i]);
4405 
4406 	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
4407 			&wm8994->hubs);
4408 	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, component);
4409 	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, component);
4410 	wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, component);
4411 
4412 	if (wm8994->jackdet)
4413 		wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);
4414 
4415 	switch (control->type) {
4416 	case WM8994:
4417 		if (wm8994->micdet_irq)
4418 			free_irq(wm8994->micdet_irq, wm8994);
4419 		wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET,
4420 				wm8994);
4421 		wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT,
4422 				wm8994);
4423 		wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_DET,
4424 				wm8994);
4425 		break;
4426 
4427 	case WM1811:
4428 	case WM8958:
4429 		if (wm8994->micdet_irq)
4430 			free_irq(wm8994->micdet_irq, wm8994);
4431 		break;
4432 	}
4433 	release_firmware(wm8994->mbc);
4434 	release_firmware(wm8994->mbc_vss);
4435 	release_firmware(wm8994->enh_eq);
4436 	kfree(wm8994->retune_mobile_texts);
4437 }
4438 
4439 static const struct snd_soc_component_driver soc_component_dev_wm8994 = {
4440 	.probe			= wm8994_component_probe,
4441 	.remove			= wm8994_component_remove,
4442 	.suspend		= wm8994_component_suspend,
4443 	.resume			= wm8994_component_resume,
4444 	.set_bias_level		= wm8994_set_bias_level,
4445 	.idle_bias_on		= 1,
4446 	.use_pmdown_time	= 1,
4447 	.endianness		= 1,
4448 	.non_legacy_dai_naming	= 1,
4449 };
4450 
4451 static int wm8994_probe(struct platform_device *pdev)
4452 {
4453 	struct wm8994_priv *wm8994;
4454 
4455 	wm8994 = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_priv),
4456 			      GFP_KERNEL);
4457 	if (wm8994 == NULL)
4458 		return -ENOMEM;
4459 	platform_set_drvdata(pdev, wm8994);
4460 
4461 	mutex_init(&wm8994->fw_lock);
4462 
4463 	wm8994->wm8994 = dev_get_drvdata(pdev->dev.parent);
4464 
4465 	pm_runtime_enable(&pdev->dev);
4466 	pm_runtime_idle(&pdev->dev);
4467 
4468 	return devm_snd_soc_register_component(&pdev->dev, &soc_component_dev_wm8994,
4469 			wm8994_dai, ARRAY_SIZE(wm8994_dai));
4470 }
4471 
4472 static int wm8994_remove(struct platform_device *pdev)
4473 {
4474 	pm_runtime_disable(&pdev->dev);
4475 
4476 	return 0;
4477 }
4478 
4479 #ifdef CONFIG_PM_SLEEP
4480 static int wm8994_suspend(struct device *dev)
4481 {
4482 	struct wm8994_priv *wm8994 = dev_get_drvdata(dev);
4483 
4484 	/* Drop down to power saving mode when system is suspended */
4485 	if (wm8994->jackdet && !wm8994->active_refcount)
4486 		regmap_update_bits(wm8994->wm8994->regmap, WM8994_ANTIPOP_2,
4487 				   WM1811_JACKDET_MODE_MASK,
4488 				   wm8994->jackdet_mode);
4489 
4490 	return 0;
4491 }
4492 
4493 static int wm8994_resume(struct device *dev)
4494 {
4495 	struct wm8994_priv *wm8994 = dev_get_drvdata(dev);
4496 
4497 	if (wm8994->jackdet && wm8994->jackdet_mode)
4498 		regmap_update_bits(wm8994->wm8994->regmap, WM8994_ANTIPOP_2,
4499 				   WM1811_JACKDET_MODE_MASK,
4500 				   WM1811_JACKDET_MODE_AUDIO);
4501 
4502 	return 0;
4503 }
4504 #endif
4505 
4506 static const struct dev_pm_ops wm8994_pm_ops = {
4507 	SET_SYSTEM_SLEEP_PM_OPS(wm8994_suspend, wm8994_resume)
4508 };
4509 
4510 static struct platform_driver wm8994_codec_driver = {
4511 	.driver = {
4512 		.name = "wm8994-codec",
4513 		.pm = &wm8994_pm_ops,
4514 	},
4515 	.probe = wm8994_probe,
4516 	.remove = wm8994_remove,
4517 };
4518 
4519 module_platform_driver(wm8994_codec_driver);
4520 
4521 MODULE_DESCRIPTION("ASoC WM8994 driver");
4522 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
4523 MODULE_LICENSE("GPL");
4524 MODULE_ALIAS("platform:wm8994-codec");
4525