xref: /linux/sound/soc/codecs/wm8904.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * wm8904.c  --  WM8904 ALSA SoC Audio driver
4  *
5  * Copyright 2009-12 Wolfson Microelectronics plc
6  *
7  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8  */
9 
10 #include <linux/clk.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/pm.h>
15 #include <linux/i2c.h>
16 #include <linux/regmap.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/slab.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 #include <sound/initval.h>
24 #include <sound/tlv.h>
25 #include <sound/wm8904.h>
26 
27 #include "wm8904.h"
28 
29 enum wm8904_type {
30 	WM8904,
31 	WM8912,
32 };
33 
34 #define WM8904_NUM_DCS_CHANNELS 4
35 
36 #define WM8904_NUM_SUPPLIES 5
37 static const char *wm8904_supply_names[WM8904_NUM_SUPPLIES] = {
38 	"DCVDD",
39 	"DBVDD",
40 	"AVDD",
41 	"CPVDD",
42 	"MICVDD",
43 };
44 
45 /* codec private data */
46 struct wm8904_priv {
47 	struct regmap *regmap;
48 	struct clk *mclk;
49 
50 	enum wm8904_type devtype;
51 
52 	struct regulator_bulk_data supplies[WM8904_NUM_SUPPLIES];
53 
54 	struct wm8904_pdata *pdata;
55 
56 	int deemph;
57 
58 	/* Platform provided DRC configuration */
59 	const char **drc_texts;
60 	int drc_cfg;
61 	struct soc_enum drc_enum;
62 
63 	/* Platform provided ReTune mobile configuration */
64 	int num_retune_mobile_texts;
65 	const char **retune_mobile_texts;
66 	int retune_mobile_cfg;
67 	struct soc_enum retune_mobile_enum;
68 
69 	/* FLL setup */
70 	int fll_src;
71 	int fll_fref;
72 	int fll_fout;
73 
74 	/* Clocking configuration */
75 	unsigned int mclk_rate;
76 	int sysclk_src;
77 	unsigned int sysclk_rate;
78 
79 	int tdm_width;
80 	int tdm_slots;
81 	int bclk;
82 	int fs;
83 
84 	/* DC servo configuration - cached offset values */
85 	int dcs_state[WM8904_NUM_DCS_CHANNELS];
86 };
87 
88 static const struct reg_default wm8904_reg_defaults[] = {
89 	{ 4,   0x0018 },     /* R4   - Bias Control 0 */
90 	{ 5,   0x0000 },     /* R5   - VMID Control 0 */
91 	{ 6,   0x0000 },     /* R6   - Mic Bias Control 0 */
92 	{ 7,   0x0000 },     /* R7   - Mic Bias Control 1 */
93 	{ 8,   0x0001 },     /* R8   - Analogue DAC 0 */
94 	{ 9,   0x9696 },     /* R9   - mic Filter Control */
95 	{ 10,  0x0001 },     /* R10  - Analogue ADC 0 */
96 	{ 12,  0x0000 },     /* R12  - Power Management 0 */
97 	{ 14,  0x0000 },     /* R14  - Power Management 2 */
98 	{ 15,  0x0000 },     /* R15  - Power Management 3 */
99 	{ 18,  0x0000 },     /* R18  - Power Management 6 */
100 	{ 20,  0x945E },     /* R20  - Clock Rates 0 */
101 	{ 21,  0x0C05 },     /* R21  - Clock Rates 1 */
102 	{ 22,  0x0006 },     /* R22  - Clock Rates 2 */
103 	{ 24,  0x0050 },     /* R24  - Audio Interface 0 */
104 	{ 25,  0x000A },     /* R25  - Audio Interface 1 */
105 	{ 26,  0x00E4 },     /* R26  - Audio Interface 2 */
106 	{ 27,  0x0040 },     /* R27  - Audio Interface 3 */
107 	{ 30,  0x00C0 },     /* R30  - DAC Digital Volume Left */
108 	{ 31,  0x00C0 },     /* R31  - DAC Digital Volume Right */
109 	{ 32,  0x0000 },     /* R32  - DAC Digital 0 */
110 	{ 33,  0x0008 },     /* R33  - DAC Digital 1 */
111 	{ 36,  0x00C0 },     /* R36  - ADC Digital Volume Left */
112 	{ 37,  0x00C0 },     /* R37  - ADC Digital Volume Right */
113 	{ 38,  0x0010 },     /* R38  - ADC Digital 0 */
114 	{ 39,  0x0000 },     /* R39  - Digital Microphone 0 */
115 	{ 40,  0x01AF },     /* R40  - DRC 0 */
116 	{ 41,  0x3248 },     /* R41  - DRC 1 */
117 	{ 42,  0x0000 },     /* R42  - DRC 2 */
118 	{ 43,  0x0000 },     /* R43  - DRC 3 */
119 	{ 44,  0x0085 },     /* R44  - Analogue Left Input 0 */
120 	{ 45,  0x0085 },     /* R45  - Analogue Right Input 0 */
121 	{ 46,  0x0044 },     /* R46  - Analogue Left Input 1 */
122 	{ 47,  0x0044 },     /* R47  - Analogue Right Input 1 */
123 	{ 57,  0x002D },     /* R57  - Analogue OUT1 Left */
124 	{ 58,  0x002D },     /* R58  - Analogue OUT1 Right */
125 	{ 59,  0x0039 },     /* R59  - Analogue OUT2 Left */
126 	{ 60,  0x0039 },     /* R60  - Analogue OUT2 Right */
127 	{ 61,  0x0000 },     /* R61  - Analogue OUT12 ZC */
128 	{ 67,  0x0000 },     /* R67  - DC Servo 0 */
129 	{ 69,  0xAAAA },     /* R69  - DC Servo 2 */
130 	{ 71,  0xAAAA },     /* R71  - DC Servo 4 */
131 	{ 72,  0xAAAA },     /* R72  - DC Servo 5 */
132 	{ 90,  0x0000 },     /* R90  - Analogue HP 0 */
133 	{ 94,  0x0000 },     /* R94  - Analogue Lineout 0 */
134 	{ 98,  0x0000 },     /* R98  - Charge Pump 0 */
135 	{ 104, 0x0004 },     /* R104 - Class W 0 */
136 	{ 108, 0x0000 },     /* R108 - Write Sequencer 0 */
137 	{ 109, 0x0000 },     /* R109 - Write Sequencer 1 */
138 	{ 110, 0x0000 },     /* R110 - Write Sequencer 2 */
139 	{ 111, 0x0000 },     /* R111 - Write Sequencer 3 */
140 	{ 112, 0x0000 },     /* R112 - Write Sequencer 4 */
141 	{ 116, 0x0000 },     /* R116 - FLL Control 1 */
142 	{ 117, 0x0007 },     /* R117 - FLL Control 2 */
143 	{ 118, 0x0000 },     /* R118 - FLL Control 3 */
144 	{ 119, 0x2EE0 },     /* R119 - FLL Control 4 */
145 	{ 120, 0x0004 },     /* R120 - FLL Control 5 */
146 	{ 121, 0x0014 },     /* R121 - GPIO Control 1 */
147 	{ 122, 0x0010 },     /* R122 - GPIO Control 2 */
148 	{ 123, 0x0010 },     /* R123 - GPIO Control 3 */
149 	{ 124, 0x0000 },     /* R124 - GPIO Control 4 */
150 	{ 126, 0x0000 },     /* R126 - Digital Pulls */
151 	{ 128, 0xFFFF },     /* R128 - Interrupt Status Mask */
152 	{ 129, 0x0000 },     /* R129 - Interrupt Polarity */
153 	{ 130, 0x0000 },     /* R130 - Interrupt Debounce */
154 	{ 134, 0x0000 },     /* R134 - EQ1 */
155 	{ 135, 0x000C },     /* R135 - EQ2 */
156 	{ 136, 0x000C },     /* R136 - EQ3 */
157 	{ 137, 0x000C },     /* R137 - EQ4 */
158 	{ 138, 0x000C },     /* R138 - EQ5 */
159 	{ 139, 0x000C },     /* R139 - EQ6 */
160 	{ 140, 0x0FCA },     /* R140 - EQ7 */
161 	{ 141, 0x0400 },     /* R141 - EQ8 */
162 	{ 142, 0x00D8 },     /* R142 - EQ9 */
163 	{ 143, 0x1EB5 },     /* R143 - EQ10 */
164 	{ 144, 0xF145 },     /* R144 - EQ11 */
165 	{ 145, 0x0B75 },     /* R145 - EQ12 */
166 	{ 146, 0x01C5 },     /* R146 - EQ13 */
167 	{ 147, 0x1C58 },     /* R147 - EQ14 */
168 	{ 148, 0xF373 },     /* R148 - EQ15 */
169 	{ 149, 0x0A54 },     /* R149 - EQ16 */
170 	{ 150, 0x0558 },     /* R150 - EQ17 */
171 	{ 151, 0x168E },     /* R151 - EQ18 */
172 	{ 152, 0xF829 },     /* R152 - EQ19 */
173 	{ 153, 0x07AD },     /* R153 - EQ20 */
174 	{ 154, 0x1103 },     /* R154 - EQ21 */
175 	{ 155, 0x0564 },     /* R155 - EQ22 */
176 	{ 156, 0x0559 },     /* R156 - EQ23 */
177 	{ 157, 0x4000 },     /* R157 - EQ24 */
178 	{ 161, 0x0000 },     /* R161 - Control Interface Test 1 */
179 	{ 204, 0x0000 },     /* R204 - Analogue Output Bias 0 */
180 	{ 247, 0x0000 },     /* R247 - FLL NCO Test 0 */
181 	{ 248, 0x0019 },     /* R248 - FLL NCO Test 1 */
182 };
183 
184 static bool wm8904_volatile_register(struct device *dev, unsigned int reg)
185 {
186 	switch (reg) {
187 	case WM8904_SW_RESET_AND_ID:
188 	case WM8904_REVISION:
189 	case WM8904_DC_SERVO_1:
190 	case WM8904_DC_SERVO_6:
191 	case WM8904_DC_SERVO_7:
192 	case WM8904_DC_SERVO_8:
193 	case WM8904_DC_SERVO_9:
194 	case WM8904_DC_SERVO_READBACK_0:
195 	case WM8904_INTERRUPT_STATUS:
196 		return true;
197 	default:
198 		return false;
199 	}
200 }
201 
202 static bool wm8904_readable_register(struct device *dev, unsigned int reg)
203 {
204 	switch (reg) {
205 	case WM8904_SW_RESET_AND_ID:
206 	case WM8904_REVISION:
207 	case WM8904_BIAS_CONTROL_0:
208 	case WM8904_VMID_CONTROL_0:
209 	case WM8904_MIC_BIAS_CONTROL_0:
210 	case WM8904_MIC_BIAS_CONTROL_1:
211 	case WM8904_ANALOGUE_DAC_0:
212 	case WM8904_MIC_FILTER_CONTROL:
213 	case WM8904_ANALOGUE_ADC_0:
214 	case WM8904_POWER_MANAGEMENT_0:
215 	case WM8904_POWER_MANAGEMENT_2:
216 	case WM8904_POWER_MANAGEMENT_3:
217 	case WM8904_POWER_MANAGEMENT_6:
218 	case WM8904_CLOCK_RATES_0:
219 	case WM8904_CLOCK_RATES_1:
220 	case WM8904_CLOCK_RATES_2:
221 	case WM8904_AUDIO_INTERFACE_0:
222 	case WM8904_AUDIO_INTERFACE_1:
223 	case WM8904_AUDIO_INTERFACE_2:
224 	case WM8904_AUDIO_INTERFACE_3:
225 	case WM8904_DAC_DIGITAL_VOLUME_LEFT:
226 	case WM8904_DAC_DIGITAL_VOLUME_RIGHT:
227 	case WM8904_DAC_DIGITAL_0:
228 	case WM8904_DAC_DIGITAL_1:
229 	case WM8904_ADC_DIGITAL_VOLUME_LEFT:
230 	case WM8904_ADC_DIGITAL_VOLUME_RIGHT:
231 	case WM8904_ADC_DIGITAL_0:
232 	case WM8904_DIGITAL_MICROPHONE_0:
233 	case WM8904_DRC_0:
234 	case WM8904_DRC_1:
235 	case WM8904_DRC_2:
236 	case WM8904_DRC_3:
237 	case WM8904_ANALOGUE_LEFT_INPUT_0:
238 	case WM8904_ANALOGUE_RIGHT_INPUT_0:
239 	case WM8904_ANALOGUE_LEFT_INPUT_1:
240 	case WM8904_ANALOGUE_RIGHT_INPUT_1:
241 	case WM8904_ANALOGUE_OUT1_LEFT:
242 	case WM8904_ANALOGUE_OUT1_RIGHT:
243 	case WM8904_ANALOGUE_OUT2_LEFT:
244 	case WM8904_ANALOGUE_OUT2_RIGHT:
245 	case WM8904_ANALOGUE_OUT12_ZC:
246 	case WM8904_DC_SERVO_0:
247 	case WM8904_DC_SERVO_1:
248 	case WM8904_DC_SERVO_2:
249 	case WM8904_DC_SERVO_4:
250 	case WM8904_DC_SERVO_5:
251 	case WM8904_DC_SERVO_6:
252 	case WM8904_DC_SERVO_7:
253 	case WM8904_DC_SERVO_8:
254 	case WM8904_DC_SERVO_9:
255 	case WM8904_DC_SERVO_READBACK_0:
256 	case WM8904_ANALOGUE_HP_0:
257 	case WM8904_ANALOGUE_LINEOUT_0:
258 	case WM8904_CHARGE_PUMP_0:
259 	case WM8904_CLASS_W_0:
260 	case WM8904_WRITE_SEQUENCER_0:
261 	case WM8904_WRITE_SEQUENCER_1:
262 	case WM8904_WRITE_SEQUENCER_2:
263 	case WM8904_WRITE_SEQUENCER_3:
264 	case WM8904_WRITE_SEQUENCER_4:
265 	case WM8904_FLL_CONTROL_1:
266 	case WM8904_FLL_CONTROL_2:
267 	case WM8904_FLL_CONTROL_3:
268 	case WM8904_FLL_CONTROL_4:
269 	case WM8904_FLL_CONTROL_5:
270 	case WM8904_GPIO_CONTROL_1:
271 	case WM8904_GPIO_CONTROL_2:
272 	case WM8904_GPIO_CONTROL_3:
273 	case WM8904_GPIO_CONTROL_4:
274 	case WM8904_DIGITAL_PULLS:
275 	case WM8904_INTERRUPT_STATUS:
276 	case WM8904_INTERRUPT_STATUS_MASK:
277 	case WM8904_INTERRUPT_POLARITY:
278 	case WM8904_INTERRUPT_DEBOUNCE:
279 	case WM8904_EQ1:
280 	case WM8904_EQ2:
281 	case WM8904_EQ3:
282 	case WM8904_EQ4:
283 	case WM8904_EQ5:
284 	case WM8904_EQ6:
285 	case WM8904_EQ7:
286 	case WM8904_EQ8:
287 	case WM8904_EQ9:
288 	case WM8904_EQ10:
289 	case WM8904_EQ11:
290 	case WM8904_EQ12:
291 	case WM8904_EQ13:
292 	case WM8904_EQ14:
293 	case WM8904_EQ15:
294 	case WM8904_EQ16:
295 	case WM8904_EQ17:
296 	case WM8904_EQ18:
297 	case WM8904_EQ19:
298 	case WM8904_EQ20:
299 	case WM8904_EQ21:
300 	case WM8904_EQ22:
301 	case WM8904_EQ23:
302 	case WM8904_EQ24:
303 	case WM8904_CONTROL_INTERFACE_TEST_1:
304 	case WM8904_ADC_TEST_0:
305 	case WM8904_ANALOGUE_OUTPUT_BIAS_0:
306 	case WM8904_FLL_NCO_TEST_0:
307 	case WM8904_FLL_NCO_TEST_1:
308 		return true;
309 	default:
310 		return false;
311 	}
312 }
313 
314 static int wm8904_configure_clocking(struct snd_soc_component *component)
315 {
316 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
317 	unsigned int clock0, clock2, rate;
318 
319 	/* Gate the clock while we're updating to avoid misclocking */
320 	clock2 = snd_soc_component_read(component, WM8904_CLOCK_RATES_2);
321 	snd_soc_component_update_bits(component, WM8904_CLOCK_RATES_2,
322 			    WM8904_SYSCLK_SRC, 0);
323 
324 	/* This should be done on init() for bypass paths */
325 	switch (wm8904->sysclk_src) {
326 	case WM8904_CLK_MCLK:
327 		dev_dbg(component->dev, "Using %dHz MCLK\n", wm8904->mclk_rate);
328 
329 		clock2 &= ~WM8904_SYSCLK_SRC;
330 		rate = wm8904->mclk_rate;
331 
332 		/* Ensure the FLL is stopped */
333 		snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
334 				    WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
335 		break;
336 
337 	case WM8904_CLK_FLL:
338 		dev_dbg(component->dev, "Using %dHz FLL clock\n",
339 			wm8904->fll_fout);
340 
341 		/* The SYSCLK may already be on; if source changes to FLL, ensure
342 		 * FLL is enabled before selecting it as SYSCLK source.
343 		 */
344 		if (clock2 & WM8904_CLK_SYS_ENA) {
345 			snd_soc_component_update_bits(component,
346 						      WM8904_FLL_CONTROL_1,
347 						      WM8904_FLL_OSC_ENA,
348 						      WM8904_FLL_OSC_ENA);
349 			snd_soc_component_update_bits(component,
350 						      WM8904_FLL_CONTROL_1,
351 						      WM8904_FLL_ENA,
352 						      WM8904_FLL_ENA);
353 		}
354 
355 		clock2 |= WM8904_SYSCLK_SRC;
356 		rate = wm8904->fll_fout;
357 		break;
358 
359 	default:
360 		dev_err(component->dev, "System clock not configured\n");
361 		return -EINVAL;
362 	}
363 
364 	/* SYSCLK shouldn't be over 13.5MHz */
365 	if (rate > 13500000) {
366 		clock0 = WM8904_MCLK_DIV;
367 		wm8904->sysclk_rate = rate / 2;
368 	} else {
369 		clock0 = 0;
370 		wm8904->sysclk_rate = rate;
371 	}
372 
373 	snd_soc_component_update_bits(component, WM8904_CLOCK_RATES_0, WM8904_MCLK_DIV,
374 			    clock0);
375 
376 	snd_soc_component_update_bits(component, WM8904_CLOCK_RATES_2,
377 			    WM8904_CLK_SYS_ENA | WM8904_SYSCLK_SRC, clock2);
378 
379 	dev_dbg(component->dev, "CLK_SYS is %dHz\n", wm8904->sysclk_rate);
380 
381 	return 0;
382 }
383 
384 static void wm8904_set_drc(struct snd_soc_component *component)
385 {
386 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
387 	struct wm8904_pdata *pdata = wm8904->pdata;
388 	int save, i;
389 
390 	/* Save any enables; the configuration should clear them. */
391 	save = snd_soc_component_read(component, WM8904_DRC_0);
392 
393 	for (i = 0; i < WM8904_DRC_REGS; i++)
394 		snd_soc_component_update_bits(component, WM8904_DRC_0 + i, 0xffff,
395 				    pdata->drc_cfgs[wm8904->drc_cfg].regs[i]);
396 
397 	/* Reenable the DRC */
398 	snd_soc_component_update_bits(component, WM8904_DRC_0,
399 			    WM8904_DRC_ENA | WM8904_DRC_DAC_PATH, save);
400 }
401 
402 static int wm8904_put_drc_enum(struct snd_kcontrol *kcontrol,
403 			       struct snd_ctl_elem_value *ucontrol)
404 {
405 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
406 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
407 	struct wm8904_pdata *pdata = wm8904->pdata;
408 	int value = ucontrol->value.enumerated.item[0];
409 
410 	if (value >= pdata->num_drc_cfgs)
411 		return -EINVAL;
412 
413 	wm8904->drc_cfg = value;
414 
415 	wm8904_set_drc(component);
416 
417 	return 0;
418 }
419 
420 static int wm8904_get_drc_enum(struct snd_kcontrol *kcontrol,
421 			       struct snd_ctl_elem_value *ucontrol)
422 {
423 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
424 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
425 
426 	ucontrol->value.enumerated.item[0] = wm8904->drc_cfg;
427 
428 	return 0;
429 }
430 
431 static void wm8904_set_retune_mobile(struct snd_soc_component *component)
432 {
433 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
434 	struct wm8904_pdata *pdata = wm8904->pdata;
435 	int best, best_val, save, i, cfg;
436 
437 	if (!pdata || !wm8904->num_retune_mobile_texts)
438 		return;
439 
440 	/* Find the version of the currently selected configuration
441 	 * with the nearest sample rate. */
442 	cfg = wm8904->retune_mobile_cfg;
443 	best = 0;
444 	best_val = INT_MAX;
445 	for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
446 		if (strcmp(pdata->retune_mobile_cfgs[i].name,
447 			   wm8904->retune_mobile_texts[cfg]) == 0 &&
448 		    abs(pdata->retune_mobile_cfgs[i].rate
449 			- wm8904->fs) < best_val) {
450 			best = i;
451 			best_val = abs(pdata->retune_mobile_cfgs[i].rate
452 				       - wm8904->fs);
453 		}
454 	}
455 
456 	dev_dbg(component->dev, "ReTune Mobile %s/%dHz for %dHz sample rate\n",
457 		pdata->retune_mobile_cfgs[best].name,
458 		pdata->retune_mobile_cfgs[best].rate,
459 		wm8904->fs);
460 
461 	/* The EQ will be disabled while reconfiguring it, remember the
462 	 * current configuration.
463 	 */
464 	save = snd_soc_component_read(component, WM8904_EQ1);
465 
466 	for (i = 0; i < WM8904_EQ_REGS; i++)
467 		snd_soc_component_update_bits(component, WM8904_EQ1 + i, 0xffff,
468 				pdata->retune_mobile_cfgs[best].regs[i]);
469 
470 	snd_soc_component_update_bits(component, WM8904_EQ1, WM8904_EQ_ENA, save);
471 }
472 
473 static int wm8904_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
474 					 struct snd_ctl_elem_value *ucontrol)
475 {
476 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
477 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
478 	struct wm8904_pdata *pdata = wm8904->pdata;
479 	int value = ucontrol->value.enumerated.item[0];
480 
481 	if (value >= pdata->num_retune_mobile_cfgs)
482 		return -EINVAL;
483 
484 	wm8904->retune_mobile_cfg = value;
485 
486 	wm8904_set_retune_mobile(component);
487 
488 	return 0;
489 }
490 
491 static int wm8904_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
492 					 struct snd_ctl_elem_value *ucontrol)
493 {
494 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
495 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
496 
497 	ucontrol->value.enumerated.item[0] = wm8904->retune_mobile_cfg;
498 
499 	return 0;
500 }
501 
502 static int deemph_settings[] = { 0, 32000, 44100, 48000 };
503 
504 static int wm8904_set_deemph(struct snd_soc_component *component)
505 {
506 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
507 	int val, i, best;
508 
509 	/* If we're using deemphasis select the nearest available sample
510 	 * rate.
511 	 */
512 	if (wm8904->deemph) {
513 		best = 1;
514 		for (i = 2; i < ARRAY_SIZE(deemph_settings); i++) {
515 			if (abs(deemph_settings[i] - wm8904->fs) <
516 			    abs(deemph_settings[best] - wm8904->fs))
517 				best = i;
518 		}
519 
520 		val = best << WM8904_DEEMPH_SHIFT;
521 	} else {
522 		val = 0;
523 	}
524 
525 	dev_dbg(component->dev, "Set deemphasis %d\n", val);
526 
527 	return snd_soc_component_update_bits(component, WM8904_DAC_DIGITAL_1,
528 				   WM8904_DEEMPH_MASK, val);
529 }
530 
531 static int wm8904_get_deemph(struct snd_kcontrol *kcontrol,
532 			     struct snd_ctl_elem_value *ucontrol)
533 {
534 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
535 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
536 
537 	ucontrol->value.integer.value[0] = wm8904->deemph;
538 	return 0;
539 }
540 
541 static int wm8904_put_deemph(struct snd_kcontrol *kcontrol,
542 			      struct snd_ctl_elem_value *ucontrol)
543 {
544 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
545 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
546 	unsigned int deemph = ucontrol->value.integer.value[0];
547 
548 	if (deemph > 1)
549 		return -EINVAL;
550 
551 	wm8904->deemph = deemph;
552 
553 	return wm8904_set_deemph(component);
554 }
555 
556 static const DECLARE_TLV_DB_SCALE(dac_boost_tlv, 0, 600, 0);
557 static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
558 static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
559 static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 300, 0);
560 static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
561 
562 static const char *hpf_mode_text[] = {
563 	"Hi-fi", "Voice 1", "Voice 2", "Voice 3"
564 };
565 
566 static SOC_ENUM_SINGLE_DECL(hpf_mode, WM8904_ADC_DIGITAL_0, 5,
567 			    hpf_mode_text);
568 
569 static int wm8904_adc_osr_put(struct snd_kcontrol *kcontrol,
570 			      struct snd_ctl_elem_value *ucontrol)
571 {
572 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
573 	unsigned int val;
574 	int ret;
575 
576 	ret = snd_soc_put_volsw(kcontrol, ucontrol);
577 	if (ret < 0)
578 		return ret;
579 
580 	if (ucontrol->value.integer.value[0])
581 		val = 0;
582 	else
583 		val = WM8904_ADC_128_OSR_TST_MODE | WM8904_ADC_BIASX1P5;
584 
585 	snd_soc_component_update_bits(component, WM8904_ADC_TEST_0,
586 			    WM8904_ADC_128_OSR_TST_MODE | WM8904_ADC_BIASX1P5,
587 			    val);
588 
589 	return ret;
590 }
591 
592 static const struct snd_kcontrol_new wm8904_adc_snd_controls[] = {
593 SOC_DOUBLE_R_TLV("Digital Capture Volume", WM8904_ADC_DIGITAL_VOLUME_LEFT,
594 		 WM8904_ADC_DIGITAL_VOLUME_RIGHT, 1, 119, 0, digital_tlv),
595 
596 /* No TLV since it depends on mode */
597 SOC_DOUBLE_R("Capture Volume", WM8904_ANALOGUE_LEFT_INPUT_0,
598 	     WM8904_ANALOGUE_RIGHT_INPUT_0, 0, 31, 0),
599 SOC_DOUBLE_R("Capture Switch", WM8904_ANALOGUE_LEFT_INPUT_0,
600 	     WM8904_ANALOGUE_RIGHT_INPUT_0, 7, 1, 1),
601 
602 SOC_SINGLE("High Pass Filter Switch", WM8904_ADC_DIGITAL_0, 4, 1, 0),
603 SOC_ENUM("High Pass Filter Mode", hpf_mode),
604 SOC_SINGLE_EXT("ADC 128x OSR Switch", WM8904_ANALOGUE_ADC_0, 0, 1, 0,
605 	snd_soc_get_volsw, wm8904_adc_osr_put),
606 };
607 
608 static const char *drc_path_text[] = {
609 	"ADC", "DAC"
610 };
611 
612 static SOC_ENUM_SINGLE_DECL(drc_path, WM8904_DRC_0, 14, drc_path_text);
613 
614 static const struct snd_kcontrol_new wm8904_dac_snd_controls[] = {
615 SOC_SINGLE_TLV("Digital Playback Boost Volume",
616 	       WM8904_AUDIO_INTERFACE_0, 9, 3, 0, dac_boost_tlv),
617 SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8904_DAC_DIGITAL_VOLUME_LEFT,
618 		 WM8904_DAC_DIGITAL_VOLUME_RIGHT, 1, 96, 0, digital_tlv),
619 
620 SOC_DOUBLE_R_TLV("Headphone Volume", WM8904_ANALOGUE_OUT1_LEFT,
621 		 WM8904_ANALOGUE_OUT1_RIGHT, 0, 63, 0, out_tlv),
622 SOC_DOUBLE_R("Headphone Switch", WM8904_ANALOGUE_OUT1_LEFT,
623 	     WM8904_ANALOGUE_OUT1_RIGHT, 8, 1, 1),
624 SOC_DOUBLE_R("Headphone ZC Switch", WM8904_ANALOGUE_OUT1_LEFT,
625 	     WM8904_ANALOGUE_OUT1_RIGHT, 6, 1, 0),
626 
627 SOC_DOUBLE_R_TLV("Line Output Volume", WM8904_ANALOGUE_OUT2_LEFT,
628 		 WM8904_ANALOGUE_OUT2_RIGHT, 0, 63, 0, out_tlv),
629 SOC_DOUBLE_R("Line Output Switch", WM8904_ANALOGUE_OUT2_LEFT,
630 	     WM8904_ANALOGUE_OUT2_RIGHT, 8, 1, 1),
631 SOC_DOUBLE_R("Line Output ZC Switch", WM8904_ANALOGUE_OUT2_LEFT,
632 	     WM8904_ANALOGUE_OUT2_RIGHT, 6, 1, 0),
633 
634 SOC_SINGLE("EQ Switch", WM8904_EQ1, 0, 1, 0),
635 SOC_SINGLE("DRC Switch", WM8904_DRC_0, 15, 1, 0),
636 SOC_ENUM("DRC Path", drc_path),
637 SOC_SINGLE("DAC OSRx2 Switch", WM8904_DAC_DIGITAL_1, 6, 1, 0),
638 SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
639 		    wm8904_get_deemph, wm8904_put_deemph),
640 };
641 
642 static const struct snd_kcontrol_new wm8904_snd_controls[] = {
643 SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8904_DAC_DIGITAL_0, 4, 8, 15, 0,
644 	       sidetone_tlv),
645 };
646 
647 static const struct snd_kcontrol_new wm8904_eq_controls[] = {
648 SOC_SINGLE_TLV("EQ1 Volume", WM8904_EQ2, 0, 24, 0, eq_tlv),
649 SOC_SINGLE_TLV("EQ2 Volume", WM8904_EQ3, 0, 24, 0, eq_tlv),
650 SOC_SINGLE_TLV("EQ3 Volume", WM8904_EQ4, 0, 24, 0, eq_tlv),
651 SOC_SINGLE_TLV("EQ4 Volume", WM8904_EQ5, 0, 24, 0, eq_tlv),
652 SOC_SINGLE_TLV("EQ5 Volume", WM8904_EQ6, 0, 24, 0, eq_tlv),
653 };
654 
655 static int cp_event(struct snd_soc_dapm_widget *w,
656 		    struct snd_kcontrol *kcontrol, int event)
657 {
658 	if (WARN_ON(event != SND_SOC_DAPM_POST_PMU))
659 		return -EINVAL;
660 
661 	/* Maximum startup time */
662 	udelay(500);
663 
664 	return 0;
665 }
666 
667 static int sysclk_event(struct snd_soc_dapm_widget *w,
668 			 struct snd_kcontrol *kcontrol, int event)
669 {
670 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
671 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
672 
673 	switch (event) {
674 	case SND_SOC_DAPM_PRE_PMU:
675 		/* If we're using the FLL then we only start it when
676 		 * required; we assume that the configuration has been
677 		 * done previously and all we need to do is kick it
678 		 * off.
679 		 */
680 		switch (wm8904->sysclk_src) {
681 		case WM8904_CLK_FLL:
682 			snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
683 					    WM8904_FLL_OSC_ENA,
684 					    WM8904_FLL_OSC_ENA);
685 
686 			snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
687 					    WM8904_FLL_ENA,
688 					    WM8904_FLL_ENA);
689 			break;
690 
691 		default:
692 			break;
693 		}
694 		break;
695 
696 	case SND_SOC_DAPM_POST_PMD:
697 		snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
698 				    WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
699 		break;
700 	}
701 
702 	return 0;
703 }
704 
705 static int out_pga_event(struct snd_soc_dapm_widget *w,
706 			 struct snd_kcontrol *kcontrol, int event)
707 {
708 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
709 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
710 	int reg, val;
711 	int dcs_mask;
712 	int dcs_l, dcs_r;
713 	int dcs_l_reg, dcs_r_reg;
714 	int an_out_reg;
715 	int timeout;
716 	int pwr_reg;
717 
718 	/* This code is shared between HP and LINEOUT; we do all our
719 	 * power management in stereo pairs to avoid latency issues so
720 	 * we reuse shift to identify which rather than strcmp() the
721 	 * name. */
722 	reg = w->shift;
723 
724 	switch (reg) {
725 	case WM8904_ANALOGUE_HP_0:
726 		pwr_reg = WM8904_POWER_MANAGEMENT_2;
727 		dcs_mask = WM8904_DCS_ENA_CHAN_0 | WM8904_DCS_ENA_CHAN_1;
728 		dcs_r_reg = WM8904_DC_SERVO_8;
729 		dcs_l_reg = WM8904_DC_SERVO_9;
730 		an_out_reg = WM8904_ANALOGUE_OUT1_LEFT;
731 		dcs_l = 0;
732 		dcs_r = 1;
733 		break;
734 	case WM8904_ANALOGUE_LINEOUT_0:
735 		pwr_reg = WM8904_POWER_MANAGEMENT_3;
736 		dcs_mask = WM8904_DCS_ENA_CHAN_2 | WM8904_DCS_ENA_CHAN_3;
737 		dcs_r_reg = WM8904_DC_SERVO_6;
738 		dcs_l_reg = WM8904_DC_SERVO_7;
739 		an_out_reg = WM8904_ANALOGUE_OUT2_LEFT;
740 		dcs_l = 2;
741 		dcs_r = 3;
742 		break;
743 	default:
744 		WARN(1, "Invalid reg %d\n", reg);
745 		return -EINVAL;
746 	}
747 
748 	switch (event) {
749 	case SND_SOC_DAPM_PRE_PMU:
750 		/* Power on the PGAs */
751 		snd_soc_component_update_bits(component, pwr_reg,
752 				    WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA,
753 				    WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA);
754 
755 		/* Power on the amplifier */
756 		snd_soc_component_update_bits(component, reg,
757 				    WM8904_HPL_ENA | WM8904_HPR_ENA,
758 				    WM8904_HPL_ENA | WM8904_HPR_ENA);
759 
760 
761 		/* Enable the first stage */
762 		snd_soc_component_update_bits(component, reg,
763 				    WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY,
764 				    WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY);
765 
766 		/* Power up the DC servo */
767 		snd_soc_component_update_bits(component, WM8904_DC_SERVO_0,
768 				    dcs_mask, dcs_mask);
769 
770 		/* Either calibrate the DC servo or restore cached state
771 		 * if we have that.
772 		 */
773 		if (wm8904->dcs_state[dcs_l] || wm8904->dcs_state[dcs_r]) {
774 			dev_dbg(component->dev, "Restoring DC servo state\n");
775 
776 			snd_soc_component_write(component, dcs_l_reg,
777 				      wm8904->dcs_state[dcs_l]);
778 			snd_soc_component_write(component, dcs_r_reg,
779 				      wm8904->dcs_state[dcs_r]);
780 
781 			snd_soc_component_write(component, WM8904_DC_SERVO_1, dcs_mask);
782 
783 			timeout = 20;
784 		} else {
785 			dev_dbg(component->dev, "Calibrating DC servo\n");
786 
787 			snd_soc_component_write(component, WM8904_DC_SERVO_1,
788 				dcs_mask << WM8904_DCS_TRIG_STARTUP_0_SHIFT);
789 
790 			timeout = 500;
791 		}
792 
793 		/* Wait for DC servo to complete */
794 		dcs_mask <<= WM8904_DCS_CAL_COMPLETE_SHIFT;
795 		do {
796 			val = snd_soc_component_read(component, WM8904_DC_SERVO_READBACK_0);
797 			if ((val & dcs_mask) == dcs_mask)
798 				break;
799 
800 			msleep(1);
801 		} while (--timeout);
802 
803 		if ((val & dcs_mask) != dcs_mask)
804 			dev_warn(component->dev, "DC servo timed out\n");
805 		else
806 			dev_dbg(component->dev, "DC servo ready\n");
807 
808 		/* Enable the output stage */
809 		snd_soc_component_update_bits(component, reg,
810 				    WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP,
811 				    WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP);
812 
813 		/* Update volume, requires PGA to be powered */
814 		val = snd_soc_component_read(component, an_out_reg);
815 		snd_soc_component_write(component, an_out_reg, val);
816 		break;
817 
818 	case SND_SOC_DAPM_POST_PMU:
819 		/* Unshort the output itself */
820 		snd_soc_component_update_bits(component, reg,
821 				    WM8904_HPL_RMV_SHORT |
822 				    WM8904_HPR_RMV_SHORT,
823 				    WM8904_HPL_RMV_SHORT |
824 				    WM8904_HPR_RMV_SHORT);
825 
826 		break;
827 
828 	case SND_SOC_DAPM_PRE_PMD:
829 		/* Short the output */
830 		snd_soc_component_update_bits(component, reg,
831 				    WM8904_HPL_RMV_SHORT |
832 				    WM8904_HPR_RMV_SHORT, 0);
833 		break;
834 
835 	case SND_SOC_DAPM_POST_PMD:
836 		/* Cache the DC servo configuration; this will be
837 		 * invalidated if we change the configuration. */
838 		wm8904->dcs_state[dcs_l] = snd_soc_component_read(component, dcs_l_reg);
839 		wm8904->dcs_state[dcs_r] = snd_soc_component_read(component, dcs_r_reg);
840 
841 		snd_soc_component_update_bits(component, WM8904_DC_SERVO_0,
842 				    dcs_mask, 0);
843 
844 		/* Disable the amplifier input and output stages */
845 		snd_soc_component_update_bits(component, reg,
846 				    WM8904_HPL_ENA | WM8904_HPR_ENA |
847 				    WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY |
848 				    WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP,
849 				    0);
850 
851 		/* PGAs too */
852 		snd_soc_component_update_bits(component, pwr_reg,
853 				    WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA,
854 				    0);
855 		break;
856 	}
857 
858 	return 0;
859 }
860 
861 static const char * const dmic_text[] = {
862 	"DMIC1", "DMIC2"
863 };
864 
865 static SOC_ENUM_SINGLE_DECL(dmic_enum, WM8904_DIGITAL_MICROPHONE_0,
866 			    WM8904_DMIC_SRC_SHIFT, dmic_text);
867 
868 static const struct snd_kcontrol_new dmic_mux =
869 	SOC_DAPM_ENUM("DMIC Mux", dmic_enum);
870 
871 static const char * const cin_text[] = {
872 	"ADC", "DMIC"
873 };
874 
875 static SOC_ENUM_SINGLE_DECL(cin_enum, WM8904_DIGITAL_MICROPHONE_0,
876 			    WM8904_DMIC_ENA_SHIFT, cin_text);
877 
878 static const struct snd_kcontrol_new cin_mux =
879 	SOC_DAPM_ENUM("Capture Input", cin_enum);
880 
881 static const char *input_mode_text[] = {
882 	"Single-Ended", "Differential Line", "Differential Mic"
883 };
884 
885 static const char *lin_text[] = {
886 	"IN1L", "IN2L", "IN3L"
887 };
888 
889 static SOC_ENUM_SINGLE_DECL(lin_enum, WM8904_ANALOGUE_LEFT_INPUT_1, 2,
890 			    lin_text);
891 
892 static const struct snd_kcontrol_new lin_mux =
893 	SOC_DAPM_ENUM("Left Capture Mux", lin_enum);
894 
895 static SOC_ENUM_SINGLE_DECL(lin_inv_enum, WM8904_ANALOGUE_LEFT_INPUT_1, 4,
896 			    lin_text);
897 
898 static const struct snd_kcontrol_new lin_inv_mux =
899 	SOC_DAPM_ENUM("Left Capture Inverting Mux", lin_inv_enum);
900 
901 static SOC_ENUM_SINGLE_DECL(lin_mode_enum,
902 			    WM8904_ANALOGUE_LEFT_INPUT_1, 0,
903 			    input_mode_text);
904 
905 static const struct snd_kcontrol_new lin_mode =
906 	SOC_DAPM_ENUM("Left Capture Mode", lin_mode_enum);
907 
908 static const char *rin_text[] = {
909 	"IN1R", "IN2R", "IN3R"
910 };
911 
912 static SOC_ENUM_SINGLE_DECL(rin_enum, WM8904_ANALOGUE_RIGHT_INPUT_1, 2,
913 			    rin_text);
914 
915 static const struct snd_kcontrol_new rin_mux =
916 	SOC_DAPM_ENUM("Right Capture Mux", rin_enum);
917 
918 static SOC_ENUM_SINGLE_DECL(rin_inv_enum, WM8904_ANALOGUE_RIGHT_INPUT_1, 4,
919 			    rin_text);
920 
921 static const struct snd_kcontrol_new rin_inv_mux =
922 	SOC_DAPM_ENUM("Right Capture Inverting Mux", rin_inv_enum);
923 
924 static SOC_ENUM_SINGLE_DECL(rin_mode_enum,
925 			    WM8904_ANALOGUE_RIGHT_INPUT_1, 0,
926 			    input_mode_text);
927 
928 static const struct snd_kcontrol_new rin_mode =
929 	SOC_DAPM_ENUM("Right Capture Mode", rin_mode_enum);
930 
931 static const char *aif_text[] = {
932 	"Left", "Right"
933 };
934 
935 static SOC_ENUM_SINGLE_DECL(aifoutl_enum, WM8904_AUDIO_INTERFACE_0, 7,
936 			    aif_text);
937 
938 static const struct snd_kcontrol_new aifoutl_mux =
939 	SOC_DAPM_ENUM("AIFOUTL Mux", aifoutl_enum);
940 
941 static SOC_ENUM_SINGLE_DECL(aifoutr_enum, WM8904_AUDIO_INTERFACE_0, 6,
942 			    aif_text);
943 
944 static const struct snd_kcontrol_new aifoutr_mux =
945 	SOC_DAPM_ENUM("AIFOUTR Mux", aifoutr_enum);
946 
947 static SOC_ENUM_SINGLE_DECL(aifinl_enum, WM8904_AUDIO_INTERFACE_0, 5,
948 			    aif_text);
949 
950 static const struct snd_kcontrol_new aifinl_mux =
951 	SOC_DAPM_ENUM("AIFINL Mux", aifinl_enum);
952 
953 static SOC_ENUM_SINGLE_DECL(aifinr_enum, WM8904_AUDIO_INTERFACE_0, 4,
954 			    aif_text);
955 
956 static const struct snd_kcontrol_new aifinr_mux =
957 	SOC_DAPM_ENUM("AIFINR Mux", aifinr_enum);
958 
959 static const struct snd_soc_dapm_widget wm8904_core_dapm_widgets[] = {
960 SND_SOC_DAPM_SUPPLY("SYSCLK", WM8904_CLOCK_RATES_2, 2, 0, sysclk_event,
961 		    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
962 SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8904_CLOCK_RATES_2, 1, 0, NULL, 0),
963 SND_SOC_DAPM_SUPPLY("TOCLK", WM8904_CLOCK_RATES_2, 0, 0, NULL, 0),
964 };
965 
966 static const struct snd_soc_dapm_widget wm8904_adc_dapm_widgets[] = {
967 SND_SOC_DAPM_INPUT("IN1L"),
968 SND_SOC_DAPM_INPUT("IN1R"),
969 SND_SOC_DAPM_INPUT("IN2L"),
970 SND_SOC_DAPM_INPUT("IN2R"),
971 SND_SOC_DAPM_INPUT("IN3L"),
972 SND_SOC_DAPM_INPUT("IN3R"),
973 
974 SND_SOC_DAPM_SUPPLY("MICBIAS", WM8904_MIC_BIAS_CONTROL_0, 0, 0, NULL, 0),
975 
976 SND_SOC_DAPM_MUX("Left Capture Mux", SND_SOC_NOPM, 0, 0, &lin_mux),
977 SND_SOC_DAPM_MUX("Left Capture Inverting Mux", SND_SOC_NOPM, 0, 0,
978 		 &lin_inv_mux),
979 SND_SOC_DAPM_MUX("Left Capture Mode", SND_SOC_NOPM, 0, 0, &lin_mode),
980 SND_SOC_DAPM_MUX("Right Capture Mux", SND_SOC_NOPM, 0, 0, &rin_mux),
981 SND_SOC_DAPM_MUX("Right Capture Inverting Mux", SND_SOC_NOPM, 0, 0,
982 		 &rin_inv_mux),
983 SND_SOC_DAPM_MUX("Right Capture Mode", SND_SOC_NOPM, 0, 0, &rin_mode),
984 
985 SND_SOC_DAPM_PGA("Left Capture PGA", WM8904_POWER_MANAGEMENT_0, 1, 0,
986 		 NULL, 0),
987 SND_SOC_DAPM_PGA("Right Capture PGA", WM8904_POWER_MANAGEMENT_0, 0, 0,
988 		 NULL, 0),
989 
990 SND_SOC_DAPM_ADC("ADCL", NULL, WM8904_POWER_MANAGEMENT_6, 1, 0),
991 SND_SOC_DAPM_ADC("ADCR", NULL, WM8904_POWER_MANAGEMENT_6, 0, 0),
992 
993 SND_SOC_DAPM_MUX("AIFOUTL Mux", SND_SOC_NOPM, 0, 0, &aifoutl_mux),
994 SND_SOC_DAPM_MUX("AIFOUTR Mux", SND_SOC_NOPM, 0, 0, &aifoutr_mux),
995 
996 SND_SOC_DAPM_AIF_OUT("AIFOUTL", "Capture", 0, SND_SOC_NOPM, 0, 0),
997 SND_SOC_DAPM_AIF_OUT("AIFOUTR", "Capture", 1, SND_SOC_NOPM, 0, 0),
998 };
999 
1000 static const struct snd_soc_dapm_widget wm8904_dmic_dapm_widgets[] = {
1001 SND_SOC_DAPM_MUX("DMIC Mux", SND_SOC_NOPM, 0, 0, &dmic_mux),
1002 };
1003 
1004 static const struct snd_soc_dapm_widget wm8904_cin_dapm_widgets[] = {
1005 SND_SOC_DAPM_MUX("Left Capture Input", SND_SOC_NOPM, 0, 0, &cin_mux),
1006 SND_SOC_DAPM_MUX("Right Capture Input", SND_SOC_NOPM, 0, 0, &cin_mux),
1007 };
1008 
1009 static const struct snd_soc_dapm_widget wm8904_dac_dapm_widgets[] = {
1010 SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
1011 SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
1012 
1013 SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &aifinl_mux),
1014 SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &aifinr_mux),
1015 
1016 SND_SOC_DAPM_DAC("DACL", NULL, WM8904_POWER_MANAGEMENT_6, 3, 0),
1017 SND_SOC_DAPM_DAC("DACR", NULL, WM8904_POWER_MANAGEMENT_6, 2, 0),
1018 
1019 SND_SOC_DAPM_SUPPLY("Charge pump", WM8904_CHARGE_PUMP_0, 0, 0, cp_event,
1020 		    SND_SOC_DAPM_POST_PMU),
1021 
1022 SND_SOC_DAPM_PGA("HPL PGA", SND_SOC_NOPM, 1, 0, NULL, 0),
1023 SND_SOC_DAPM_PGA("HPR PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
1024 
1025 SND_SOC_DAPM_PGA("LINEL PGA", SND_SOC_NOPM, 1, 0, NULL, 0),
1026 SND_SOC_DAPM_PGA("LINER PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
1027 
1028 SND_SOC_DAPM_PGA_E("Headphone Output", SND_SOC_NOPM, WM8904_ANALOGUE_HP_0,
1029 		   0, NULL, 0, out_pga_event,
1030 		   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
1031 		   SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
1032 SND_SOC_DAPM_PGA_E("Line Output", SND_SOC_NOPM, WM8904_ANALOGUE_LINEOUT_0,
1033 		   0, NULL, 0, out_pga_event,
1034 		   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
1035 		   SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
1036 
1037 SND_SOC_DAPM_OUTPUT("HPOUTL"),
1038 SND_SOC_DAPM_OUTPUT("HPOUTR"),
1039 SND_SOC_DAPM_OUTPUT("LINEOUTL"),
1040 SND_SOC_DAPM_OUTPUT("LINEOUTR"),
1041 };
1042 
1043 static const char *out_mux_text[] = {
1044 	"DAC", "Bypass"
1045 };
1046 
1047 static SOC_ENUM_SINGLE_DECL(hpl_enum, WM8904_ANALOGUE_OUT12_ZC, 3,
1048 			    out_mux_text);
1049 
1050 static const struct snd_kcontrol_new hpl_mux =
1051 	SOC_DAPM_ENUM("HPL Mux", hpl_enum);
1052 
1053 static SOC_ENUM_SINGLE_DECL(hpr_enum, WM8904_ANALOGUE_OUT12_ZC, 2,
1054 			    out_mux_text);
1055 
1056 static const struct snd_kcontrol_new hpr_mux =
1057 	SOC_DAPM_ENUM("HPR Mux", hpr_enum);
1058 
1059 static SOC_ENUM_SINGLE_DECL(linel_enum, WM8904_ANALOGUE_OUT12_ZC, 1,
1060 			    out_mux_text);
1061 
1062 static const struct snd_kcontrol_new linel_mux =
1063 	SOC_DAPM_ENUM("LINEL Mux", linel_enum);
1064 
1065 static SOC_ENUM_SINGLE_DECL(liner_enum, WM8904_ANALOGUE_OUT12_ZC, 0,
1066 			    out_mux_text);
1067 
1068 static const struct snd_kcontrol_new liner_mux =
1069 	SOC_DAPM_ENUM("LINER Mux", liner_enum);
1070 
1071 static const char *sidetone_text[] = {
1072 	"None", "Left", "Right"
1073 };
1074 
1075 static SOC_ENUM_SINGLE_DECL(dacl_sidetone_enum, WM8904_DAC_DIGITAL_0, 2,
1076 			    sidetone_text);
1077 
1078 static const struct snd_kcontrol_new dacl_sidetone_mux =
1079 	SOC_DAPM_ENUM("Left Sidetone Mux", dacl_sidetone_enum);
1080 
1081 static SOC_ENUM_SINGLE_DECL(dacr_sidetone_enum, WM8904_DAC_DIGITAL_0, 0,
1082 			    sidetone_text);
1083 
1084 static const struct snd_kcontrol_new dacr_sidetone_mux =
1085 	SOC_DAPM_ENUM("Right Sidetone Mux", dacr_sidetone_enum);
1086 
1087 static const struct snd_soc_dapm_widget wm8904_dapm_widgets[] = {
1088 SND_SOC_DAPM_SUPPLY("Class G", WM8904_CLASS_W_0, 0, 1, NULL, 0),
1089 SND_SOC_DAPM_PGA("Left Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
1090 SND_SOC_DAPM_PGA("Right Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
1091 
1092 SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &dacl_sidetone_mux),
1093 SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &dacr_sidetone_mux),
1094 
1095 SND_SOC_DAPM_MUX("HPL Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
1096 SND_SOC_DAPM_MUX("HPR Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
1097 SND_SOC_DAPM_MUX("LINEL Mux", SND_SOC_NOPM, 0, 0, &linel_mux),
1098 SND_SOC_DAPM_MUX("LINER Mux", SND_SOC_NOPM, 0, 0, &liner_mux),
1099 };
1100 
1101 static const struct snd_soc_dapm_route core_intercon[] = {
1102 	{ "CLK_DSP", NULL, "SYSCLK" },
1103 	{ "TOCLK", NULL, "SYSCLK" },
1104 };
1105 
1106 static const struct snd_soc_dapm_route adc_intercon[] = {
1107 	{ "Left Capture Mux", "IN1L", "IN1L" },
1108 	{ "Left Capture Mux", "IN2L", "IN2L" },
1109 	{ "Left Capture Mux", "IN3L", "IN3L" },
1110 
1111 	{ "Left Capture Inverting Mux", "IN1L", "IN1L" },
1112 	{ "Left Capture Inverting Mux", "IN2L", "IN2L" },
1113 	{ "Left Capture Inverting Mux", "IN3L", "IN3L" },
1114 
1115 	{ "Left Capture Mode", "Single-Ended", "Left Capture Inverting Mux" },
1116 	{ "Left Capture Mode", "Differential Line", "Left Capture Mux" },
1117 	{ "Left Capture Mode", "Differential Line", "Left Capture Inverting Mux" },
1118 	{ "Left Capture Mode", "Differential Mic", "Left Capture Mux" },
1119 	{ "Left Capture Mode", "Differential Mic", "Left Capture Inverting Mux" },
1120 
1121 	{ "Right Capture Mux", "IN1R", "IN1R" },
1122 	{ "Right Capture Mux", "IN2R", "IN2R" },
1123 	{ "Right Capture Mux", "IN3R", "IN3R" },
1124 
1125 	{ "Right Capture Inverting Mux", "IN1R", "IN1R" },
1126 	{ "Right Capture Inverting Mux", "IN2R", "IN2R" },
1127 	{ "Right Capture Inverting Mux", "IN3R", "IN3R" },
1128 
1129 	{ "Right Capture Mode", "Single-Ended", "Right Capture Inverting Mux" },
1130 	{ "Right Capture Mode", "Differential Line", "Right Capture Mux" },
1131 	{ "Right Capture Mode", "Differential Line", "Right Capture Inverting Mux" },
1132 	{ "Right Capture Mode", "Differential Mic", "Right Capture Mux" },
1133 	{ "Right Capture Mode", "Differential Mic", "Right Capture Inverting Mux" },
1134 
1135 	{ "Left Capture PGA", NULL, "Left Capture Mode" },
1136 	{ "Right Capture PGA", NULL, "Right Capture Mode" },
1137 
1138 	{ "AIFOUTL Mux", "Left", "ADCL" },
1139 	{ "AIFOUTL Mux", "Right", "ADCR" },
1140 	{ "AIFOUTR Mux", "Left", "ADCL" },
1141 	{ "AIFOUTR Mux", "Right", "ADCR" },
1142 
1143 	{ "AIFOUTL", NULL, "AIFOUTL Mux" },
1144 	{ "AIFOUTR", NULL, "AIFOUTR Mux" },
1145 
1146 	{ "ADCL", NULL, "CLK_DSP" },
1147 	{ "ADCR", NULL, "CLK_DSP" },
1148 };
1149 
1150 /* No DMICs, always connect PGAs */
1151 static const struct snd_soc_dapm_route cin_nodmic_con[] = {
1152 	{ "ADCL", NULL, "Left Capture PGA" },
1153 	{ "ADCR", NULL, "Right Capture PGA" },
1154 };
1155 
1156 /* DMIC system in use: mux between ADC and DMICDAT1, 2 or both */
1157 static const struct snd_soc_dapm_route cin_adc_dmic_con[] = {
1158 	{ "Left Capture Input", "ADC", "Left Capture PGA" },
1159 	{ "Right Capture Input", "ADC", "Right Capture PGA" },
1160 
1161 	{ "ADCL", NULL, "Left Capture Input" },
1162 	{ "ADCR", NULL, "Right Capture Input" },
1163 };
1164 
1165 /*  IN1L as DMICDAT1 */
1166 static const struct snd_soc_dapm_route cin_dmic1_con[] = {
1167 	{ "Left Capture Input", "DMIC", "IN1L" },
1168 	{ "Right Capture Input", "DMIC", "IN1L" },
1169 };
1170 
1171 /* IN1R as DMICDAT2 */
1172 static const struct snd_soc_dapm_route cin_dmic2_con[] = {
1173 	{ "Left Capture Input", "DMIC", "IN1R" },
1174 	{ "Right Capture Input", "DMIC", "IN1R" },
1175 };
1176 
1177 /* DMICDAT1 and DMICDAT2: mux between them, ADC still used for IN2 and IN3 */
1178 static const struct snd_soc_dapm_route cin_2dmics_con[] = {
1179 	{ "DMIC Mux", "DMIC1", "IN1L" },
1180 	{ "DMIC Mux", "DMIC2", "IN1R" },
1181 
1182 	{ "Left Capture Input", "DMIC", "DMIC Mux" },
1183 	{ "Right Capture Input", "DMIC", "DMIC Mux" },
1184 };
1185 
1186 static const struct snd_soc_dapm_route dac_intercon[] = {
1187 	{ "DACL Mux", "Left", "AIFINL" },
1188 	{ "DACL Mux", "Right", "AIFINR" },
1189 
1190 	{ "DACR Mux", "Left", "AIFINL" },
1191 	{ "DACR Mux", "Right", "AIFINR" },
1192 
1193 	{ "DACL", NULL, "DACL Mux" },
1194 	{ "DACL", NULL, "CLK_DSP" },
1195 
1196 	{ "DACR", NULL, "DACR Mux" },
1197 	{ "DACR", NULL, "CLK_DSP" },
1198 
1199 	{ "Charge pump", NULL, "SYSCLK" },
1200 
1201 	{ "Headphone Output", NULL, "HPL PGA" },
1202 	{ "Headphone Output", NULL, "HPR PGA" },
1203 	{ "Headphone Output", NULL, "Charge pump" },
1204 	{ "Headphone Output", NULL, "TOCLK" },
1205 
1206 	{ "Line Output", NULL, "LINEL PGA" },
1207 	{ "Line Output", NULL, "LINER PGA" },
1208 	{ "Line Output", NULL, "Charge pump" },
1209 	{ "Line Output", NULL, "TOCLK" },
1210 
1211 	{ "HPOUTL", NULL, "Headphone Output" },
1212 	{ "HPOUTR", NULL, "Headphone Output" },
1213 
1214 	{ "LINEOUTL", NULL, "Line Output" },
1215 	{ "LINEOUTR", NULL, "Line Output" },
1216 };
1217 
1218 static const struct snd_soc_dapm_route wm8904_intercon[] = {
1219 	{ "Left Sidetone", "Left", "ADCL" },
1220 	{ "Left Sidetone", "Right", "ADCR" },
1221 	{ "DACL", NULL, "Left Sidetone" },
1222 
1223 	{ "Right Sidetone", "Left", "ADCL" },
1224 	{ "Right Sidetone", "Right", "ADCR" },
1225 	{ "DACR", NULL, "Right Sidetone" },
1226 
1227 	{ "Left Bypass", NULL, "Class G" },
1228 	{ "Left Bypass", NULL, "Left Capture PGA" },
1229 
1230 	{ "Right Bypass", NULL, "Class G" },
1231 	{ "Right Bypass", NULL, "Right Capture PGA" },
1232 
1233 	{ "HPL Mux", "DAC", "DACL" },
1234 	{ "HPL Mux", "Bypass", "Left Bypass" },
1235 
1236 	{ "HPR Mux", "DAC", "DACR" },
1237 	{ "HPR Mux", "Bypass", "Right Bypass" },
1238 
1239 	{ "LINEL Mux", "DAC", "DACL" },
1240 	{ "LINEL Mux", "Bypass", "Left Bypass" },
1241 
1242 	{ "LINER Mux", "DAC", "DACR" },
1243 	{ "LINER Mux", "Bypass", "Right Bypass" },
1244 
1245 	{ "HPL PGA", NULL, "HPL Mux" },
1246 	{ "HPR PGA", NULL, "HPR Mux" },
1247 
1248 	{ "LINEL PGA", NULL, "LINEL Mux" },
1249 	{ "LINER PGA", NULL, "LINER Mux" },
1250 };
1251 
1252 static const struct snd_soc_dapm_route wm8912_intercon[] = {
1253 	{ "HPL PGA", NULL, "DACL" },
1254 	{ "HPR PGA", NULL, "DACR" },
1255 
1256 	{ "LINEL PGA", NULL, "DACL" },
1257 	{ "LINER PGA", NULL, "DACR" },
1258 };
1259 
1260 static int wm8904_add_widgets(struct snd_soc_component *component)
1261 {
1262 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
1263 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
1264 
1265 	snd_soc_dapm_new_controls(dapm, wm8904_core_dapm_widgets,
1266 				  ARRAY_SIZE(wm8904_core_dapm_widgets));
1267 	snd_soc_dapm_add_routes(dapm, core_intercon,
1268 				ARRAY_SIZE(core_intercon));
1269 
1270 	switch (wm8904->devtype) {
1271 	case WM8904:
1272 		snd_soc_add_component_controls(component, wm8904_adc_snd_controls,
1273 				     ARRAY_SIZE(wm8904_adc_snd_controls));
1274 		snd_soc_add_component_controls(component, wm8904_dac_snd_controls,
1275 				     ARRAY_SIZE(wm8904_dac_snd_controls));
1276 		snd_soc_add_component_controls(component, wm8904_snd_controls,
1277 				     ARRAY_SIZE(wm8904_snd_controls));
1278 
1279 		snd_soc_dapm_new_controls(dapm, wm8904_adc_dapm_widgets,
1280 					  ARRAY_SIZE(wm8904_adc_dapm_widgets));
1281 		snd_soc_dapm_new_controls(dapm, wm8904_dac_dapm_widgets,
1282 					  ARRAY_SIZE(wm8904_dac_dapm_widgets));
1283 		snd_soc_dapm_new_controls(dapm, wm8904_dapm_widgets,
1284 					  ARRAY_SIZE(wm8904_dapm_widgets));
1285 
1286 		snd_soc_dapm_add_routes(dapm, adc_intercon,
1287 					ARRAY_SIZE(adc_intercon));
1288 		snd_soc_dapm_add_routes(dapm, dac_intercon,
1289 					ARRAY_SIZE(dac_intercon));
1290 		snd_soc_dapm_add_routes(dapm, wm8904_intercon,
1291 					ARRAY_SIZE(wm8904_intercon));
1292 		break;
1293 
1294 	case WM8912:
1295 		snd_soc_add_component_controls(component, wm8904_dac_snd_controls,
1296 				     ARRAY_SIZE(wm8904_dac_snd_controls));
1297 
1298 		snd_soc_dapm_new_controls(dapm, wm8904_dac_dapm_widgets,
1299 					  ARRAY_SIZE(wm8904_dac_dapm_widgets));
1300 
1301 		snd_soc_dapm_add_routes(dapm, dac_intercon,
1302 					ARRAY_SIZE(dac_intercon));
1303 		snd_soc_dapm_add_routes(dapm, wm8912_intercon,
1304 					ARRAY_SIZE(wm8912_intercon));
1305 		break;
1306 	}
1307 
1308 	return 0;
1309 }
1310 
1311 static struct {
1312 	int ratio;
1313 	unsigned int clk_sys_rate;
1314 } clk_sys_rates[] = {
1315 	{   64,  0 },
1316 	{  128,  1 },
1317 	{  192,  2 },
1318 	{  256,  3 },
1319 	{  384,  4 },
1320 	{  512,  5 },
1321 	{  786,  6 },
1322 	{ 1024,  7 },
1323 	{ 1408,  8 },
1324 	{ 1536,  9 },
1325 };
1326 
1327 static struct {
1328 	int rate;
1329 	int sample_rate;
1330 } sample_rates[] = {
1331 	{ 8000,  0  },
1332 	{ 11025, 1  },
1333 	{ 12000, 1  },
1334 	{ 16000, 2  },
1335 	{ 22050, 3  },
1336 	{ 24000, 3  },
1337 	{ 32000, 4  },
1338 	{ 44100, 5  },
1339 	{ 48000, 5  },
1340 };
1341 
1342 static struct {
1343 	int div; /* *10 due to .5s */
1344 	int bclk_div;
1345 } bclk_divs[] = {
1346 	{ 10,  0  },
1347 	{ 15,  1  },
1348 	{ 20,  2  },
1349 	{ 30,  3  },
1350 	{ 40,  4  },
1351 	{ 50,  5  },
1352 	{ 55,  6  },
1353 	{ 60,  7  },
1354 	{ 80,  8  },
1355 	{ 100, 9  },
1356 	{ 110, 10 },
1357 	{ 120, 11 },
1358 	{ 160, 12 },
1359 	{ 200, 13 },
1360 	{ 220, 14 },
1361 	{ 240, 16 },
1362 	{ 200, 17 },
1363 	{ 320, 18 },
1364 	{ 440, 19 },
1365 	{ 480, 20 },
1366 };
1367 
1368 
1369 static int wm8904_hw_params(struct snd_pcm_substream *substream,
1370 			    struct snd_pcm_hw_params *params,
1371 			    struct snd_soc_dai *dai)
1372 {
1373 	struct snd_soc_component *component = dai->component;
1374 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
1375 	int ret, i, best, best_val, cur_val;
1376 	unsigned int aif1 = 0;
1377 	unsigned int aif2 = 0;
1378 	unsigned int aif3 = 0;
1379 	unsigned int clock1 = 0;
1380 	unsigned int dac_digital1 = 0;
1381 
1382 	/* What BCLK do we need? */
1383 	wm8904->fs = params_rate(params);
1384 	if (wm8904->tdm_slots) {
1385 		dev_dbg(component->dev, "Configuring for %d %d bit TDM slots\n",
1386 			wm8904->tdm_slots, wm8904->tdm_width);
1387 		wm8904->bclk = snd_soc_calc_bclk(wm8904->fs,
1388 						 wm8904->tdm_width, 2,
1389 						 wm8904->tdm_slots);
1390 	} else {
1391 		wm8904->bclk = snd_soc_params_to_bclk(params);
1392 	}
1393 
1394 	switch (params_width(params)) {
1395 	case 16:
1396 		break;
1397 	case 20:
1398 		aif1 |= 0x40;
1399 		break;
1400 	case 24:
1401 		aif1 |= 0x80;
1402 		break;
1403 	case 32:
1404 		aif1 |= 0xc0;
1405 		break;
1406 	default:
1407 		return -EINVAL;
1408 	}
1409 
1410 
1411 	dev_dbg(component->dev, "Target BCLK is %dHz\n", wm8904->bclk);
1412 
1413 	ret = wm8904_configure_clocking(component);
1414 	if (ret != 0)
1415 		return ret;
1416 
1417 	/* Select nearest CLK_SYS_RATE */
1418 	best = 0;
1419 	best_val = abs((wm8904->sysclk_rate / clk_sys_rates[0].ratio)
1420 		       - wm8904->fs);
1421 	for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
1422 		cur_val = abs((wm8904->sysclk_rate /
1423 			       clk_sys_rates[i].ratio) - wm8904->fs);
1424 		if (cur_val < best_val) {
1425 			best = i;
1426 			best_val = cur_val;
1427 		}
1428 	}
1429 	dev_dbg(component->dev, "Selected CLK_SYS_RATIO of %d\n",
1430 		clk_sys_rates[best].ratio);
1431 	clock1 |= (clk_sys_rates[best].clk_sys_rate
1432 		   << WM8904_CLK_SYS_RATE_SHIFT);
1433 
1434 	/* SAMPLE_RATE */
1435 	best = 0;
1436 	best_val = abs(wm8904->fs - sample_rates[0].rate);
1437 	for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
1438 		/* Closest match */
1439 		cur_val = abs(wm8904->fs - sample_rates[i].rate);
1440 		if (cur_val < best_val) {
1441 			best = i;
1442 			best_val = cur_val;
1443 		}
1444 	}
1445 	dev_dbg(component->dev, "Selected SAMPLE_RATE of %dHz\n",
1446 		sample_rates[best].rate);
1447 	clock1 |= (sample_rates[best].sample_rate
1448 		   << WM8904_SAMPLE_RATE_SHIFT);
1449 
1450 	/* Enable sloping stopband filter for low sample rates */
1451 	if (wm8904->fs <= 24000)
1452 		dac_digital1 |= WM8904_DAC_SB_FILT;
1453 
1454 	/* BCLK_DIV */
1455 	best = 0;
1456 	best_val = INT_MAX;
1457 	for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
1458 		cur_val = ((wm8904->sysclk_rate * 10) / bclk_divs[i].div)
1459 			- wm8904->bclk;
1460 		if (cur_val < 0) /* Table is sorted */
1461 			break;
1462 		if (cur_val < best_val) {
1463 			best = i;
1464 			best_val = cur_val;
1465 		}
1466 	}
1467 	wm8904->bclk = (wm8904->sysclk_rate * 10) / bclk_divs[best].div;
1468 	dev_dbg(component->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
1469 		bclk_divs[best].div, wm8904->bclk);
1470 	aif2 |= bclk_divs[best].bclk_div;
1471 
1472 	/* LRCLK is a simple fraction of BCLK */
1473 	dev_dbg(component->dev, "LRCLK_RATE is %d\n", wm8904->bclk / wm8904->fs);
1474 	aif3 |= wm8904->bclk / wm8904->fs;
1475 
1476 	/* Apply the settings */
1477 	snd_soc_component_update_bits(component, WM8904_DAC_DIGITAL_1,
1478 			    WM8904_DAC_SB_FILT, dac_digital1);
1479 	snd_soc_component_update_bits(component, WM8904_AUDIO_INTERFACE_1,
1480 			    WM8904_AIF_WL_MASK, aif1);
1481 	snd_soc_component_update_bits(component, WM8904_AUDIO_INTERFACE_2,
1482 			    WM8904_BCLK_DIV_MASK, aif2);
1483 	snd_soc_component_update_bits(component, WM8904_AUDIO_INTERFACE_3,
1484 			    WM8904_LRCLK_RATE_MASK, aif3);
1485 	snd_soc_component_update_bits(component, WM8904_CLOCK_RATES_1,
1486 			    WM8904_SAMPLE_RATE_MASK |
1487 			    WM8904_CLK_SYS_RATE_MASK, clock1);
1488 
1489 	/* Update filters for the new settings */
1490 	wm8904_set_retune_mobile(component);
1491 	wm8904_set_deemph(component);
1492 
1493 	return 0;
1494 }
1495 
1496 static int wm8904_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1497 {
1498 	struct snd_soc_component *component = dai->component;
1499 	unsigned int aif1 = 0;
1500 	unsigned int aif3 = 0;
1501 
1502 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1503 	case SND_SOC_DAIFMT_CBC_CFC:
1504 		break;
1505 	case SND_SOC_DAIFMT_CBC_CFP:
1506 		aif3 |= WM8904_LRCLK_DIR;
1507 		break;
1508 	case SND_SOC_DAIFMT_CBP_CFC:
1509 		aif1 |= WM8904_BCLK_DIR;
1510 		break;
1511 	case SND_SOC_DAIFMT_CBP_CFP:
1512 		aif1 |= WM8904_BCLK_DIR;
1513 		aif3 |= WM8904_LRCLK_DIR;
1514 		break;
1515 	default:
1516 		return -EINVAL;
1517 	}
1518 
1519 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1520 	case SND_SOC_DAIFMT_DSP_B:
1521 		aif1 |= 0x3 | WM8904_AIF_LRCLK_INV;
1522 		fallthrough;
1523 	case SND_SOC_DAIFMT_DSP_A:
1524 		aif1 |= 0x3;
1525 		break;
1526 	case SND_SOC_DAIFMT_I2S:
1527 		aif1 |= 0x2;
1528 		break;
1529 	case SND_SOC_DAIFMT_RIGHT_J:
1530 		break;
1531 	case SND_SOC_DAIFMT_LEFT_J:
1532 		aif1 |= 0x1;
1533 		break;
1534 	default:
1535 		return -EINVAL;
1536 	}
1537 
1538 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1539 	case SND_SOC_DAIFMT_DSP_A:
1540 	case SND_SOC_DAIFMT_DSP_B:
1541 		/* frame inversion not valid for DSP modes */
1542 		switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1543 		case SND_SOC_DAIFMT_NB_NF:
1544 			break;
1545 		case SND_SOC_DAIFMT_IB_NF:
1546 			aif1 |= WM8904_AIF_BCLK_INV;
1547 			break;
1548 		default:
1549 			return -EINVAL;
1550 		}
1551 		break;
1552 
1553 	case SND_SOC_DAIFMT_I2S:
1554 	case SND_SOC_DAIFMT_RIGHT_J:
1555 	case SND_SOC_DAIFMT_LEFT_J:
1556 		switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1557 		case SND_SOC_DAIFMT_NB_NF:
1558 			break;
1559 		case SND_SOC_DAIFMT_IB_IF:
1560 			aif1 |= WM8904_AIF_BCLK_INV | WM8904_AIF_LRCLK_INV;
1561 			break;
1562 		case SND_SOC_DAIFMT_IB_NF:
1563 			aif1 |= WM8904_AIF_BCLK_INV;
1564 			break;
1565 		case SND_SOC_DAIFMT_NB_IF:
1566 			aif1 |= WM8904_AIF_LRCLK_INV;
1567 			break;
1568 		default:
1569 			return -EINVAL;
1570 		}
1571 		break;
1572 	default:
1573 		return -EINVAL;
1574 	}
1575 
1576 	snd_soc_component_update_bits(component, WM8904_AUDIO_INTERFACE_1,
1577 			    WM8904_AIF_BCLK_INV | WM8904_AIF_LRCLK_INV |
1578 			    WM8904_AIF_FMT_MASK | WM8904_BCLK_DIR, aif1);
1579 	snd_soc_component_update_bits(component, WM8904_AUDIO_INTERFACE_3,
1580 			    WM8904_LRCLK_DIR, aif3);
1581 
1582 	return 0;
1583 }
1584 
1585 
1586 static int wm8904_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
1587 			       unsigned int rx_mask, int slots, int slot_width)
1588 {
1589 	struct snd_soc_component *component = dai->component;
1590 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
1591 	int aif1 = 0;
1592 
1593 	/* Don't need to validate anything if we're turning off TDM */
1594 	if (slots == 0)
1595 		goto out;
1596 
1597 	/* Note that we allow configurations we can't handle ourselves -
1598 	 * for example, we can generate clocks for slots 2 and up even if
1599 	 * we can't use those slots ourselves.
1600 	 */
1601 	aif1 |= WM8904_AIFADC_TDM | WM8904_AIFDAC_TDM;
1602 
1603 	switch (rx_mask) {
1604 	case 3:
1605 		break;
1606 	case 0xc:
1607 		aif1 |= WM8904_AIFADC_TDM_CHAN;
1608 		break;
1609 	default:
1610 		return -EINVAL;
1611 	}
1612 
1613 
1614 	switch (tx_mask) {
1615 	case 3:
1616 		break;
1617 	case 0xc:
1618 		aif1 |= WM8904_AIFDAC_TDM_CHAN;
1619 		break;
1620 	default:
1621 		return -EINVAL;
1622 	}
1623 
1624 out:
1625 	wm8904->tdm_width = slot_width;
1626 	wm8904->tdm_slots = slots / 2;
1627 
1628 	snd_soc_component_update_bits(component, WM8904_AUDIO_INTERFACE_1,
1629 			    WM8904_AIFADC_TDM | WM8904_AIFADC_TDM_CHAN |
1630 			    WM8904_AIFDAC_TDM | WM8904_AIFDAC_TDM_CHAN, aif1);
1631 
1632 	return 0;
1633 }
1634 
1635 struct _fll_div {
1636 	u16 fll_fratio;
1637 	u16 fll_outdiv;
1638 	u16 fll_clk_ref_div;
1639 	u16 n;
1640 	u16 k;
1641 };
1642 
1643 /* The size in bits of the FLL divide multiplied by 10
1644  * to allow rounding later */
1645 #define FIXED_FLL_SIZE ((1 << 16) * 10)
1646 
1647 static struct {
1648 	unsigned int min;
1649 	unsigned int max;
1650 	u16 fll_fratio;
1651 	int ratio;
1652 } fll_fratios[] = {
1653 	{       0,    64000, 4, 16 },
1654 	{   64000,   128000, 3,  8 },
1655 	{  128000,   256000, 2,  4 },
1656 	{  256000,  1000000, 1,  2 },
1657 	{ 1000000, 13500000, 0,  1 },
1658 };
1659 
1660 static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
1661 		       unsigned int Fout)
1662 {
1663 	u64 Kpart;
1664 	unsigned int K, Ndiv, Nmod, target;
1665 	unsigned int div;
1666 	int i;
1667 
1668 	/* Fref must be <=13.5MHz */
1669 	div = 1;
1670 	fll_div->fll_clk_ref_div = 0;
1671 	while ((Fref / div) > 13500000) {
1672 		div *= 2;
1673 		fll_div->fll_clk_ref_div++;
1674 
1675 		if (div > 8) {
1676 			pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
1677 			       Fref);
1678 			return -EINVAL;
1679 		}
1680 	}
1681 
1682 	pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
1683 
1684 	/* Apply the division for our remaining calculations */
1685 	Fref /= div;
1686 
1687 	/* Fvco should be 90-100MHz; don't check the upper bound */
1688 	div = 4;
1689 	while (Fout * div < 90000000) {
1690 		div++;
1691 		if (div > 64) {
1692 			pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
1693 			       Fout);
1694 			return -EINVAL;
1695 		}
1696 	}
1697 	target = Fout * div;
1698 	fll_div->fll_outdiv = div - 1;
1699 
1700 	pr_debug("Fvco=%dHz\n", target);
1701 
1702 	/* Find an appropriate FLL_FRATIO and factor it out of the target */
1703 	for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
1704 		if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
1705 			fll_div->fll_fratio = fll_fratios[i].fll_fratio;
1706 			target /= fll_fratios[i].ratio;
1707 			break;
1708 		}
1709 	}
1710 	if (i == ARRAY_SIZE(fll_fratios)) {
1711 		pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
1712 		return -EINVAL;
1713 	}
1714 
1715 	/* Now, calculate N.K */
1716 	Ndiv = target / Fref;
1717 
1718 	fll_div->n = Ndiv;
1719 	Nmod = target % Fref;
1720 	pr_debug("Nmod=%d\n", Nmod);
1721 
1722 	/* Calculate fractional part - scale up so we can round. */
1723 	Kpart = FIXED_FLL_SIZE * (long long)Nmod;
1724 
1725 	do_div(Kpart, Fref);
1726 
1727 	K = Kpart & 0xFFFFFFFF;
1728 
1729 	if ((K % 10) >= 5)
1730 		K += 5;
1731 
1732 	/* Move down to proper range now rounding is done */
1733 	fll_div->k = K / 10;
1734 
1735 	pr_debug("N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
1736 		 fll_div->n, fll_div->k,
1737 		 fll_div->fll_fratio, fll_div->fll_outdiv,
1738 		 fll_div->fll_clk_ref_div);
1739 
1740 	return 0;
1741 }
1742 
1743 static int wm8904_set_fll(struct snd_soc_dai *dai, int fll_id, int source,
1744 			  unsigned int Fref, unsigned int Fout)
1745 {
1746 	struct snd_soc_component *component = dai->component;
1747 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
1748 	struct _fll_div fll_div;
1749 	int ret, val;
1750 	int clock2, fll1;
1751 
1752 	/* Any change? */
1753 	if (source == wm8904->fll_src && Fref == wm8904->fll_fref &&
1754 	    Fout == wm8904->fll_fout)
1755 		return 0;
1756 
1757 	clock2 = snd_soc_component_read(component, WM8904_CLOCK_RATES_2);
1758 
1759 	if (Fout == 0) {
1760 		dev_dbg(component->dev, "FLL disabled\n");
1761 
1762 		wm8904->fll_fref = 0;
1763 		wm8904->fll_fout = 0;
1764 
1765 		/* Gate SYSCLK to avoid glitches */
1766 		snd_soc_component_update_bits(component, WM8904_CLOCK_RATES_2,
1767 				    WM8904_CLK_SYS_ENA, 0);
1768 
1769 		snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
1770 				    WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
1771 
1772 		goto out;
1773 	}
1774 
1775 	/* Validate the FLL ID */
1776 	switch (source) {
1777 	case WM8904_FLL_MCLK:
1778 	case WM8904_FLL_LRCLK:
1779 	case WM8904_FLL_BCLK:
1780 		ret = fll_factors(&fll_div, Fref, Fout);
1781 		if (ret != 0)
1782 			return ret;
1783 		break;
1784 
1785 	case WM8904_FLL_FREE_RUNNING:
1786 		dev_dbg(component->dev, "Using free running FLL\n");
1787 		/* Force 12MHz and output/4 for now */
1788 		Fout = 12000000;
1789 		Fref = 12000000;
1790 
1791 		memset(&fll_div, 0, sizeof(fll_div));
1792 		fll_div.fll_outdiv = 3;
1793 		break;
1794 
1795 	default:
1796 		dev_err(component->dev, "Unknown FLL ID %d\n", fll_id);
1797 		return -EINVAL;
1798 	}
1799 
1800 	/* Save current state then disable the FLL and SYSCLK to avoid
1801 	 * misclocking */
1802 	fll1 = snd_soc_component_read(component, WM8904_FLL_CONTROL_1);
1803 	snd_soc_component_update_bits(component, WM8904_CLOCK_RATES_2,
1804 			    WM8904_CLK_SYS_ENA, 0);
1805 	snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
1806 			    WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
1807 
1808 	/* Unlock forced oscilator control to switch it on/off */
1809 	snd_soc_component_update_bits(component, WM8904_CONTROL_INTERFACE_TEST_1,
1810 			    WM8904_USER_KEY, WM8904_USER_KEY);
1811 
1812 	if (fll_id == WM8904_FLL_FREE_RUNNING) {
1813 		val = WM8904_FLL_FRC_NCO;
1814 	} else {
1815 		val = 0;
1816 	}
1817 
1818 	snd_soc_component_update_bits(component, WM8904_FLL_NCO_TEST_1, WM8904_FLL_FRC_NCO,
1819 			    val);
1820 	snd_soc_component_update_bits(component, WM8904_CONTROL_INTERFACE_TEST_1,
1821 			    WM8904_USER_KEY, 0);
1822 
1823 	switch (fll_id) {
1824 	case WM8904_FLL_MCLK:
1825 		snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_5,
1826 				    WM8904_FLL_CLK_REF_SRC_MASK, 0);
1827 		break;
1828 
1829 	case WM8904_FLL_LRCLK:
1830 		snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_5,
1831 				    WM8904_FLL_CLK_REF_SRC_MASK, 1);
1832 		break;
1833 
1834 	case WM8904_FLL_BCLK:
1835 		snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_5,
1836 				    WM8904_FLL_CLK_REF_SRC_MASK, 2);
1837 		break;
1838 	}
1839 
1840 	if (fll_div.k)
1841 		val = WM8904_FLL_FRACN_ENA;
1842 	else
1843 		val = 0;
1844 	snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
1845 			    WM8904_FLL_FRACN_ENA, val);
1846 
1847 	snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_2,
1848 			    WM8904_FLL_OUTDIV_MASK | WM8904_FLL_FRATIO_MASK,
1849 			    (fll_div.fll_outdiv << WM8904_FLL_OUTDIV_SHIFT) |
1850 			    (fll_div.fll_fratio << WM8904_FLL_FRATIO_SHIFT));
1851 
1852 	snd_soc_component_write(component, WM8904_FLL_CONTROL_3, fll_div.k);
1853 
1854 	snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_4, WM8904_FLL_N_MASK,
1855 			    fll_div.n << WM8904_FLL_N_SHIFT);
1856 
1857 	snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_5,
1858 			    WM8904_FLL_CLK_REF_DIV_MASK,
1859 			    fll_div.fll_clk_ref_div
1860 			    << WM8904_FLL_CLK_REF_DIV_SHIFT);
1861 
1862 	dev_dbg(component->dev, "FLL configured for %dHz->%dHz\n", Fref, Fout);
1863 
1864 	wm8904->fll_fref = Fref;
1865 	wm8904->fll_fout = Fout;
1866 	wm8904->fll_src = source;
1867 
1868 	/* Enable the FLL if it was previously active */
1869 	snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
1870 			    WM8904_FLL_OSC_ENA, fll1);
1871 	snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
1872 			    WM8904_FLL_ENA, fll1);
1873 
1874 out:
1875 	/* Reenable SYSCLK if it was previously active */
1876 	snd_soc_component_update_bits(component, WM8904_CLOCK_RATES_2,
1877 			    WM8904_CLK_SYS_ENA, clock2);
1878 
1879 	return 0;
1880 }
1881 
1882 static int wm8904_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1883 			     unsigned int freq, int dir)
1884 {
1885 	struct snd_soc_component *component = dai->component;
1886 	struct wm8904_priv *priv = snd_soc_component_get_drvdata(component);
1887 	unsigned long mclk_freq;
1888 	int ret;
1889 
1890 	switch (clk_id) {
1891 	case WM8904_CLK_AUTO:
1892 		/* We don't have any rate constraints, so just ignore the
1893 		 * request to disable constraining.
1894 		 */
1895 		if (!freq)
1896 			return 0;
1897 
1898 		mclk_freq = clk_get_rate(priv->mclk);
1899 		/* enable FLL if a different sysclk is desired */
1900 		if (mclk_freq != freq) {
1901 			priv->sysclk_src = WM8904_CLK_FLL;
1902 			ret = wm8904_set_fll(dai, WM8904_FLL_MCLK,
1903 					     WM8904_FLL_MCLK,
1904 					     mclk_freq, freq);
1905 			if (ret)
1906 				return ret;
1907 			break;
1908 		}
1909 		clk_id = WM8904_CLK_MCLK;
1910 		fallthrough;
1911 
1912 	case WM8904_CLK_MCLK:
1913 		priv->sysclk_src = clk_id;
1914 		priv->mclk_rate = freq;
1915 		break;
1916 
1917 	case WM8904_CLK_FLL:
1918 		priv->sysclk_src = clk_id;
1919 		break;
1920 
1921 	default:
1922 		return -EINVAL;
1923 	}
1924 
1925 	dev_dbg(dai->dev, "Clock source is %d at %uHz\n", clk_id, freq);
1926 
1927 	wm8904_configure_clocking(component);
1928 
1929 	return 0;
1930 }
1931 
1932 static int wm8904_mute(struct snd_soc_dai *codec_dai, int mute, int direction)
1933 {
1934 	struct snd_soc_component *component = codec_dai->component;
1935 	int val;
1936 
1937 	if (mute)
1938 		val = WM8904_DAC_MUTE;
1939 	else
1940 		val = 0;
1941 
1942 	snd_soc_component_update_bits(component, WM8904_DAC_DIGITAL_1, WM8904_DAC_MUTE, val);
1943 
1944 	return 0;
1945 }
1946 
1947 static int wm8904_set_bias_level(struct snd_soc_component *component,
1948 				 enum snd_soc_bias_level level)
1949 {
1950 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
1951 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
1952 	int ret;
1953 
1954 	switch (level) {
1955 	case SND_SOC_BIAS_ON:
1956 		break;
1957 
1958 	case SND_SOC_BIAS_PREPARE:
1959 		/* VMID resistance 2*50k */
1960 		snd_soc_component_update_bits(component, WM8904_VMID_CONTROL_0,
1961 				    WM8904_VMID_RES_MASK,
1962 				    0x1 << WM8904_VMID_RES_SHIFT);
1963 
1964 		/* Normal bias current */
1965 		snd_soc_component_update_bits(component, WM8904_BIAS_CONTROL_0,
1966 				    WM8904_ISEL_MASK, 2 << WM8904_ISEL_SHIFT);
1967 		break;
1968 
1969 	case SND_SOC_BIAS_STANDBY:
1970 		if (snd_soc_dapm_get_bias_level(dapm) == SND_SOC_BIAS_OFF) {
1971 			ret = regulator_bulk_enable(ARRAY_SIZE(wm8904->supplies),
1972 						    wm8904->supplies);
1973 			if (ret != 0) {
1974 				dev_err(component->dev,
1975 					"Failed to enable supplies: %d\n",
1976 					ret);
1977 				return ret;
1978 			}
1979 
1980 			ret = clk_prepare_enable(wm8904->mclk);
1981 			if (ret) {
1982 				dev_err(component->dev,
1983 					"Failed to enable MCLK: %d\n", ret);
1984 				regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies),
1985 						       wm8904->supplies);
1986 				return ret;
1987 			}
1988 
1989 			regcache_cache_only(wm8904->regmap, false);
1990 			regcache_sync(wm8904->regmap);
1991 
1992 			/* Enable bias */
1993 			snd_soc_component_update_bits(component, WM8904_BIAS_CONTROL_0,
1994 					    WM8904_BIAS_ENA, WM8904_BIAS_ENA);
1995 
1996 			/* Enable VMID, VMID buffering, 2*5k resistance */
1997 			snd_soc_component_update_bits(component, WM8904_VMID_CONTROL_0,
1998 					    WM8904_VMID_ENA |
1999 					    WM8904_VMID_RES_MASK,
2000 					    WM8904_VMID_ENA |
2001 					    0x3 << WM8904_VMID_RES_SHIFT);
2002 
2003 			/* Let VMID ramp */
2004 			msleep(1);
2005 		}
2006 
2007 		/* Maintain VMID with 2*250k */
2008 		snd_soc_component_update_bits(component, WM8904_VMID_CONTROL_0,
2009 				    WM8904_VMID_RES_MASK,
2010 				    0x2 << WM8904_VMID_RES_SHIFT);
2011 
2012 		/* Bias current *0.5 */
2013 		snd_soc_component_update_bits(component, WM8904_BIAS_CONTROL_0,
2014 				    WM8904_ISEL_MASK, 0);
2015 		break;
2016 
2017 	case SND_SOC_BIAS_OFF:
2018 		/* Turn off VMID */
2019 		snd_soc_component_update_bits(component, WM8904_VMID_CONTROL_0,
2020 				    WM8904_VMID_RES_MASK | WM8904_VMID_ENA, 0);
2021 
2022 		/* Stop bias generation */
2023 		snd_soc_component_update_bits(component, WM8904_BIAS_CONTROL_0,
2024 				    WM8904_BIAS_ENA, 0);
2025 
2026 		snd_soc_component_write(component, WM8904_SW_RESET_AND_ID, 0);
2027 		regcache_cache_only(wm8904->regmap, true);
2028 		regcache_mark_dirty(wm8904->regmap);
2029 
2030 		regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies),
2031 				       wm8904->supplies);
2032 		clk_disable_unprepare(wm8904->mclk);
2033 		break;
2034 	}
2035 	return 0;
2036 }
2037 
2038 #define WM8904_RATES SNDRV_PCM_RATE_8000_96000
2039 
2040 #define WM8904_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
2041 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
2042 
2043 static const u64 wm8904_selectable_formats[] = {
2044 	/* 1st priority */
2045 	SND_SOC_POSSIBLE_DAIFMT_I2S	|
2046 	SND_SOC_POSSIBLE_DAIFMT_RIGHT_J	|
2047 	SND_SOC_POSSIBLE_DAIFMT_LEFT_J	|
2048 	SND_SOC_POSSIBLE_DAIFMT_NB_NF	|
2049 	SND_SOC_POSSIBLE_DAIFMT_NB_IF	|
2050 	SND_SOC_POSSIBLE_DAIFMT_IB_NF	|
2051 	SND_SOC_POSSIBLE_DAIFMT_IB_IF,
2052 	/* 2nd priority */
2053 	SND_SOC_POSSIBLE_DAIFMT_DSP_A	|
2054 	SND_SOC_POSSIBLE_DAIFMT_DSP_B	|
2055 	SND_SOC_POSSIBLE_DAIFMT_NB_NF	|
2056 	SND_SOC_POSSIBLE_DAIFMT_IB_NF,
2057 };
2058 
2059 static const struct snd_soc_dai_ops wm8904_dai_ops = {
2060 	.set_sysclk = wm8904_set_sysclk,
2061 	.set_fmt = wm8904_set_fmt,
2062 	.set_tdm_slot = wm8904_set_tdm_slot,
2063 	.set_pll = wm8904_set_fll,
2064 	.hw_params = wm8904_hw_params,
2065 	.mute_stream = wm8904_mute,
2066 	.no_capture_mute = 1,
2067 	.auto_selectable_formats = wm8904_selectable_formats,
2068 	.num_auto_selectable_formats = ARRAY_SIZE(wm8904_selectable_formats),
2069 };
2070 
2071 static struct snd_soc_dai_driver wm8904_dai = {
2072 	.name = "wm8904-hifi",
2073 	.playback = {
2074 		.stream_name = "Playback",
2075 		.channels_min = 2,
2076 		.channels_max = 2,
2077 		.rates = WM8904_RATES,
2078 		.formats = WM8904_FORMATS,
2079 	},
2080 	.capture = {
2081 		.stream_name = "Capture",
2082 		.channels_min = 2,
2083 		.channels_max = 2,
2084 		.rates = WM8904_RATES,
2085 		.formats = WM8904_FORMATS,
2086 	},
2087 	.ops = &wm8904_dai_ops,
2088 	.symmetric_rate = 1,
2089 };
2090 
2091 static void wm8904_handle_retune_mobile_pdata(struct snd_soc_component *component)
2092 {
2093 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
2094 	struct wm8904_pdata *pdata = wm8904->pdata;
2095 	struct snd_kcontrol_new control =
2096 		SOC_ENUM_EXT("EQ Mode",
2097 			     wm8904->retune_mobile_enum,
2098 			     wm8904_get_retune_mobile_enum,
2099 			     wm8904_put_retune_mobile_enum);
2100 	int ret, i, j;
2101 	const char **t;
2102 
2103 	/* We need an array of texts for the enum API but the number
2104 	 * of texts is likely to be less than the number of
2105 	 * configurations due to the sample rate dependency of the
2106 	 * configurations. */
2107 	wm8904->num_retune_mobile_texts = 0;
2108 	wm8904->retune_mobile_texts = NULL;
2109 	for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
2110 		for (j = 0; j < wm8904->num_retune_mobile_texts; j++) {
2111 			if (strcmp(pdata->retune_mobile_cfgs[i].name,
2112 				   wm8904->retune_mobile_texts[j]) == 0)
2113 				break;
2114 		}
2115 
2116 		if (j != wm8904->num_retune_mobile_texts)
2117 			continue;
2118 
2119 		/* Expand the array... */
2120 		t = krealloc(wm8904->retune_mobile_texts,
2121 			     sizeof(char *) *
2122 			     (wm8904->num_retune_mobile_texts + 1),
2123 			     GFP_KERNEL);
2124 		if (t == NULL)
2125 			continue;
2126 
2127 		/* ...store the new entry... */
2128 		t[wm8904->num_retune_mobile_texts] =
2129 			pdata->retune_mobile_cfgs[i].name;
2130 
2131 		/* ...and remember the new version. */
2132 		wm8904->num_retune_mobile_texts++;
2133 		wm8904->retune_mobile_texts = t;
2134 	}
2135 
2136 	dev_dbg(component->dev, "Allocated %d unique ReTune Mobile names\n",
2137 		wm8904->num_retune_mobile_texts);
2138 
2139 	wm8904->retune_mobile_enum.items = wm8904->num_retune_mobile_texts;
2140 	wm8904->retune_mobile_enum.texts = wm8904->retune_mobile_texts;
2141 
2142 	ret = snd_soc_add_component_controls(component, &control, 1);
2143 	if (ret != 0)
2144 		dev_err(component->dev,
2145 			"Failed to add ReTune Mobile control: %d\n", ret);
2146 }
2147 
2148 static void wm8904_handle_dmic_pdata(struct snd_soc_component *component)
2149 {
2150 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
2151 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
2152 	struct wm8904_pdata *pdata = wm8904->pdata;
2153 	unsigned int dmic_src;
2154 
2155 	if (!pdata->in1l_as_dmicdat1 && !pdata->in1r_as_dmicdat2) {
2156 		snd_soc_dapm_add_routes(dapm, cin_nodmic_con,
2157 					ARRAY_SIZE(cin_nodmic_con));
2158 		snd_soc_component_update_bits(component, WM8904_DIGITAL_MICROPHONE_0,
2159 					      WM8904_DMIC_ENA_MASK, 0);
2160 		return;
2161 	}
2162 
2163 	/* Need a control and routing to switch between DMIC and ADC */
2164 	snd_soc_dapm_new_controls(dapm, wm8904_cin_dapm_widgets,
2165 				  ARRAY_SIZE(wm8904_cin_dapm_widgets));
2166 	snd_soc_dapm_add_routes(dapm, cin_adc_dmic_con,
2167 				ARRAY_SIZE(cin_adc_dmic_con));
2168 
2169 	if (pdata->in1l_as_dmicdat1 && pdata->in1r_as_dmicdat2) {
2170 		/* Need a control and routing to mux between DMICDAT1 and 2 */
2171 		dev_dbg(component->dev, "DMICDAT1 and DMICDAT2 in use\n");
2172 		snd_soc_dapm_new_controls(dapm, wm8904_dmic_dapm_widgets,
2173 					  ARRAY_SIZE(wm8904_dmic_dapm_widgets));
2174 		snd_soc_dapm_add_routes(dapm, cin_2dmics_con,
2175 					ARRAY_SIZE(cin_2dmics_con));
2176 		return;
2177 	}
2178 
2179 	/* Either DMICDAT1 or DMICDAT2 is in use, not both */
2180 	if (pdata->in1l_as_dmicdat1) {
2181 		dmic_src = 0;
2182 		snd_soc_dapm_add_routes(dapm, cin_dmic1_con,
2183 					ARRAY_SIZE(cin_dmic1_con));
2184 	} else {
2185 		dmic_src = 1;
2186 		snd_soc_dapm_add_routes(dapm, cin_dmic2_con,
2187 					ARRAY_SIZE(cin_dmic2_con));
2188 	}
2189 	dev_dbg(component->dev, "DMIC_SRC (0 or 1): %d\n", dmic_src);
2190 	snd_soc_component_update_bits(component, WM8904_DIGITAL_MICROPHONE_0,
2191 				      WM8904_DMIC_SRC_MASK,
2192 				      dmic_src << WM8904_DMIC_SRC_SHIFT);
2193 }
2194 
2195 static void wm8904_handle_pdata(struct snd_soc_component *component)
2196 {
2197 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
2198 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
2199 	struct wm8904_pdata *pdata = wm8904->pdata;
2200 	int ret, i;
2201 
2202 	if (!pdata) {
2203 		snd_soc_dapm_add_routes(dapm, cin_nodmic_con,
2204 					ARRAY_SIZE(cin_nodmic_con));
2205 		snd_soc_add_component_controls(component, wm8904_eq_controls,
2206 					       ARRAY_SIZE(wm8904_eq_controls));
2207 		return;
2208 	}
2209 
2210 	wm8904_handle_dmic_pdata(component);
2211 
2212 	dev_dbg(component->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
2213 
2214 	if (pdata->num_drc_cfgs) {
2215 		struct snd_kcontrol_new control =
2216 			SOC_ENUM_EXT("DRC Mode", wm8904->drc_enum,
2217 				     wm8904_get_drc_enum, wm8904_put_drc_enum);
2218 
2219 		/* We need an array of texts for the enum API */
2220 		wm8904->drc_texts = kmalloc_objs(*wm8904->drc_texts,
2221 						 pdata->num_drc_cfgs);
2222 		if (!wm8904->drc_texts)
2223 			return;
2224 
2225 		for (i = 0; i < pdata->num_drc_cfgs; i++)
2226 			wm8904->drc_texts[i] = pdata->drc_cfgs[i].name;
2227 
2228 		wm8904->drc_enum.items = pdata->num_drc_cfgs;
2229 		wm8904->drc_enum.texts = wm8904->drc_texts;
2230 
2231 		ret = snd_soc_add_component_controls(component, &control, 1);
2232 		if (ret != 0)
2233 			dev_err(component->dev,
2234 				"Failed to add DRC mode control: %d\n", ret);
2235 
2236 		wm8904_set_drc(component);
2237 	}
2238 
2239 	dev_dbg(component->dev, "%d ReTune Mobile configurations\n",
2240 		pdata->num_retune_mobile_cfgs);
2241 
2242 	if (pdata->num_retune_mobile_cfgs)
2243 		wm8904_handle_retune_mobile_pdata(component);
2244 	else
2245 		snd_soc_add_component_controls(component, wm8904_eq_controls,
2246 				     ARRAY_SIZE(wm8904_eq_controls));
2247 }
2248 
2249 
2250 static int wm8904_probe(struct snd_soc_component *component)
2251 {
2252 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
2253 
2254 	switch (wm8904->devtype) {
2255 	case WM8904:
2256 		break;
2257 	case WM8912:
2258 		memset(&wm8904_dai.capture, 0, sizeof(wm8904_dai.capture));
2259 		break;
2260 	default:
2261 		dev_err(component->dev, "Unknown device type %d\n",
2262 			wm8904->devtype);
2263 		return -EINVAL;
2264 	}
2265 
2266 	wm8904_add_widgets(component);
2267 
2268 	/* This can add dependent widgets, so it is done after add_widgets */
2269 	wm8904_handle_pdata(component);
2270 
2271 	return 0;
2272 }
2273 
2274 static void wm8904_remove(struct snd_soc_component *component)
2275 {
2276 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
2277 
2278 	kfree(wm8904->retune_mobile_texts);
2279 	kfree(wm8904->drc_texts);
2280 }
2281 
2282 static const struct snd_soc_component_driver soc_component_dev_wm8904 = {
2283 	.probe			= wm8904_probe,
2284 	.remove			= wm8904_remove,
2285 	.set_bias_level		= wm8904_set_bias_level,
2286 	.use_pmdown_time	= 1,
2287 	.endianness		= 1,
2288 };
2289 
2290 static const struct regmap_config wm8904_regmap = {
2291 	.reg_bits = 8,
2292 	.val_bits = 16,
2293 
2294 	.max_register = WM8904_MAX_REGISTER,
2295 	.volatile_reg = wm8904_volatile_register,
2296 	.readable_reg = wm8904_readable_register,
2297 
2298 	.cache_type = REGCACHE_MAPLE,
2299 	.reg_defaults = wm8904_reg_defaults,
2300 	.num_reg_defaults = ARRAY_SIZE(wm8904_reg_defaults),
2301 };
2302 
2303 #ifdef CONFIG_OF
2304 static const struct of_device_id wm8904_of_match[] = {
2305 	{
2306 		.compatible = "wlf,wm8904",
2307 		.data = (void *)WM8904,
2308 	}, {
2309 		.compatible = "wlf,wm8912",
2310 		.data = (void *)WM8912,
2311 	}, {
2312 		/* sentinel */
2313 	}
2314 };
2315 MODULE_DEVICE_TABLE(of, wm8904_of_match);
2316 #endif
2317 
2318 /**
2319  * wm8904_read_cfg_reg_arr() - Reads a subarray from a DT u16 array
2320  *
2321  * @np: pointer to the device_node struct
2322  * @regs_property: DT property of interest
2323  * @size: size of subarrays within the array
2324  * @idx: index of the subarray of interest
2325  * @out: output
2326  *
2327  * Helper to read a subarray from a DT uint16-array,
2328  *  divided into equally sized arrays of size `size`
2329  *
2330  * Subset starts at `idx * size` and is of size `size`
2331  *
2332  * Return: 0 on success, negative error code otherwise
2333  */
2334 static int wm8904_read_cfg_reg_arr(const struct device_node *np,
2335 				   const char * const regs_property,
2336 				   int size, int idx,
2337 				   u16 * const out)
2338 {
2339 	int i, offset, ret;
2340 
2341 	offset = idx * size;
2342 
2343 	for (i = 0; i < size; i++) {
2344 		ret = of_property_read_u16_index(np, regs_property, i + offset, &out[i]);
2345 		if (ret)
2346 			return ret;
2347 	}
2348 	return 0;
2349 }
2350 
2351 static int wm8904_parse_retune_cfg_regs(const struct device_node *np,
2352 					struct wm8904_pdata *pdata, int cfg_idx)
2353 {
2354 	return wm8904_read_cfg_reg_arr(np, "wlf,retune-mobile-cfg-regs",
2355 				       WM8904_EQ_REGS, cfg_idx,
2356 				       &pdata->retune_mobile_cfgs[cfg_idx].regs[0]);
2357 }
2358 
2359 static int wm8904_parse_drc_cfg_regs(const struct device_node *np,
2360 				     struct wm8904_pdata *pdata, int cfg_idx)
2361 {
2362 	return wm8904_read_cfg_reg_arr(np, "wlf,drc-cfg-regs",
2363 				       WM8904_DRC_REGS, cfg_idx,
2364 				       &pdata->drc_cfgs[cfg_idx].regs[0]);
2365 }
2366 
2367 static int wm8904_parse_drc_cfg_from_of(struct i2c_client *i2c,
2368 					struct wm8904_pdata *pdata)
2369 {
2370 	const struct device_node *np = i2c->dev.of_node;
2371 	int i, n_cfgs;
2372 
2373 	n_cfgs = of_property_count_strings(np, "wlf,drc-cfg-names");
2374 	if (n_cfgs == -EINVAL)
2375 		return 0;
2376 
2377 	if (n_cfgs <= 0) {
2378 		dev_err(&i2c->dev, "Could not get wlf,drc-cfg-names length: %d",
2379 			n_cfgs);
2380 		return n_cfgs;
2381 	}
2382 
2383 	pdata->drc_cfgs = devm_kzalloc(&i2c->dev,
2384 				       n_cfgs * sizeof(struct wm8904_drc_cfg),
2385 				       GFP_KERNEL);
2386 	if (!pdata->drc_cfgs)
2387 		return -ENOMEM;
2388 
2389 	for (i = 0; i < n_cfgs; i++) {
2390 		if (wm8904_parse_drc_cfg_regs(np, pdata, i)) {
2391 			dev_err(&i2c->dev,
2392 				"Invalid 'wlf,drc-cfg-regs[%i,:]'\n", i);
2393 			return -EINVAL;
2394 		}
2395 
2396 		if (of_property_read_string_index(np, "wlf,drc-cfg-names", i,
2397 						  &pdata->drc_cfgs[i].name)) {
2398 			dev_err(&i2c->dev,
2399 				"Invalid 'wlf,drc-cfg-names[%i]'\n", i);
2400 			return -EINVAL;
2401 		}
2402 	}
2403 
2404 	pdata->num_drc_cfgs = n_cfgs;
2405 	return 0;
2406 }
2407 
2408 static int wm8904_parse_retune_cfg_from_of(struct i2c_client *i2c,
2409 					   struct wm8904_pdata *pdata)
2410 {
2411 	const struct device_node *np = i2c->dev.of_node;
2412 	int i, n_cfgs;
2413 
2414 	n_cfgs = of_property_count_strings(np, "wlf,retune-mobile-cfg-names");
2415 	if (n_cfgs == -EINVAL)
2416 		return 0;
2417 
2418 	if (n_cfgs <= 0) {
2419 		dev_err(&i2c->dev,
2420 			"Could not get wlf,retune-mobile-cfg-names length: %d",
2421 			n_cfgs);
2422 		return n_cfgs;
2423 	}
2424 
2425 	pdata->retune_mobile_cfgs = devm_kzalloc(&i2c->dev,
2426 						 n_cfgs * sizeof(struct wm8904_retune_mobile_cfg),
2427 						 GFP_KERNEL);
2428 	if (!pdata->retune_mobile_cfgs)
2429 		return -ENOMEM;
2430 
2431 	for (i = 0; i < n_cfgs; i++) {
2432 		if (wm8904_parse_retune_cfg_regs(np, pdata, i)) {
2433 			dev_err(&i2c->dev,
2434 				"Invalid 'wlf,retune-mobile-cfg-regs[%i,:]'\n", i);
2435 			return -EINVAL;
2436 		}
2437 
2438 		if (of_property_read_u32_index(np, "wlf,retune-mobile-cfg-hz", i,
2439 					       &pdata->retune_mobile_cfgs[i].rate)) {
2440 			dev_err(&i2c->dev,
2441 				"Invalid 'wlf,retune-mobile-cfg-hz[%i]'\n", i);
2442 			return -EINVAL;
2443 		}
2444 
2445 		if (of_property_read_string_index(np, "wlf,retune-mobile-cfg-names", i,
2446 						  &pdata->retune_mobile_cfgs[i].name)) {
2447 			dev_err(&i2c->dev,
2448 				"Invalid 'wlf,retune-mobile-cfg-names[%i]'\n", i);
2449 			return -EINVAL;
2450 		}
2451 	}
2452 
2453 	pdata->num_retune_mobile_cfgs = n_cfgs;
2454 	return 0;
2455 }
2456 
2457 static int wm8904_set_pdata_from_of(struct i2c_client *i2c,
2458 				    struct wm8904_priv *wm8904)
2459 {
2460 	const struct device_node *np = i2c->dev.of_node;
2461 	struct wm8904_pdata *pdata;
2462 	int ret, i;
2463 
2464 	pdata = devm_kzalloc_obj(&i2c->dev, *pdata);
2465 	if (!pdata)
2466 		return -ENOMEM;
2467 
2468 	pdata->in1l_as_dmicdat1 =
2469 		of_property_read_bool(np, "wlf,in1l-as-dmicdat1");
2470 
2471 	pdata->in1r_as_dmicdat2 =
2472 		of_property_read_bool(np, "wlf,in1r-as-dmicdat2");
2473 
2474 	/* If absent, default to 0xFFFF for GPIO config (i.e.: don't set) */
2475 	for (i = 0; i < WM8904_GPIO_REGS; i++)
2476 		pdata->gpio_cfg[i] = 0xFFFF;
2477 
2478 	of_property_read_u32_array(np, "wlf,gpio-cfg", pdata->gpio_cfg,
2479 				   ARRAY_SIZE(pdata->gpio_cfg));
2480 
2481 	of_property_read_u32_array(np, "wlf,micbias-cfg", pdata->mic_cfg,
2482 				   ARRAY_SIZE(pdata->mic_cfg));
2483 
2484 	ret = wm8904_parse_drc_cfg_from_of(i2c, pdata);
2485 	if (ret)
2486 		return ret;
2487 
2488 	ret = wm8904_parse_retune_cfg_from_of(i2c, pdata);
2489 	if (ret)
2490 		return ret;
2491 
2492 	wm8904->pdata = pdata;
2493 	return 0;
2494 }
2495 
2496 static const struct i2c_device_id wm8904_i2c_id[];
2497 
2498 static int wm8904_i2c_probe(struct i2c_client *i2c)
2499 {
2500 	struct wm8904_priv *wm8904;
2501 	unsigned int val;
2502 	int ret, i;
2503 
2504 	wm8904 = devm_kzalloc_obj(&i2c->dev, struct wm8904_priv);
2505 	if (wm8904 == NULL)
2506 		return -ENOMEM;
2507 
2508 	wm8904->mclk = devm_clk_get(&i2c->dev, "mclk");
2509 	if (IS_ERR(wm8904->mclk)) {
2510 		ret = PTR_ERR(wm8904->mclk);
2511 		dev_err(&i2c->dev, "Failed to get MCLK\n");
2512 		return ret;
2513 	}
2514 
2515 	wm8904->regmap = devm_regmap_init_i2c(i2c, &wm8904_regmap);
2516 	if (IS_ERR(wm8904->regmap)) {
2517 		ret = PTR_ERR(wm8904->regmap);
2518 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
2519 			ret);
2520 		return ret;
2521 	}
2522 
2523 	wm8904->devtype = (uintptr_t)i2c_get_match_data(i2c);
2524 
2525 	i2c_set_clientdata(i2c, wm8904);
2526 
2527 	if (i2c->dev.of_node) {
2528 		ret = wm8904_set_pdata_from_of(i2c, wm8904);
2529 		if (ret) {
2530 			dev_err(&i2c->dev, "Failed to set platform data from of: %d\n", ret);
2531 			return ret;
2532 		}
2533 	} else {
2534 		wm8904->pdata = i2c->dev.platform_data;
2535 	}
2536 
2537 	for (i = 0; i < ARRAY_SIZE(wm8904->supplies); i++)
2538 		wm8904->supplies[i].supply = wm8904_supply_names[i];
2539 
2540 	ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8904->supplies),
2541 				      wm8904->supplies);
2542 	if (ret != 0) {
2543 		dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
2544 		return ret;
2545 	}
2546 
2547 	ret = regulator_bulk_enable(ARRAY_SIZE(wm8904->supplies),
2548 				    wm8904->supplies);
2549 	if (ret != 0) {
2550 		dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
2551 		return ret;
2552 	}
2553 
2554 	ret = regmap_read(wm8904->regmap, WM8904_SW_RESET_AND_ID, &val);
2555 	if (ret < 0) {
2556 		dev_err(&i2c->dev, "Failed to read ID register: %d\n", ret);
2557 		goto err_enable;
2558 	}
2559 	if (val != 0x8904) {
2560 		dev_err(&i2c->dev, "Device is not a WM8904, ID is %x\n", val);
2561 		ret = -EINVAL;
2562 		goto err_enable;
2563 	}
2564 
2565 	ret = regmap_read(wm8904->regmap, WM8904_REVISION, &val);
2566 	if (ret < 0) {
2567 		dev_err(&i2c->dev, "Failed to read device revision: %d\n",
2568 			ret);
2569 		goto err_enable;
2570 	}
2571 	dev_info(&i2c->dev, "revision %c\n", val + 'A');
2572 
2573 	ret = regmap_write(wm8904->regmap, WM8904_SW_RESET_AND_ID, 0);
2574 	if (ret < 0) {
2575 		dev_err(&i2c->dev, "Failed to issue reset: %d\n", ret);
2576 		goto err_enable;
2577 	}
2578 
2579 	/* Change some default settings - latch VU and enable ZC */
2580 	regmap_update_bits(wm8904->regmap, WM8904_ADC_DIGITAL_VOLUME_LEFT,
2581 			   WM8904_ADC_VU, WM8904_ADC_VU);
2582 	regmap_update_bits(wm8904->regmap, WM8904_ADC_DIGITAL_VOLUME_RIGHT,
2583 			   WM8904_ADC_VU, WM8904_ADC_VU);
2584 	regmap_update_bits(wm8904->regmap, WM8904_DAC_DIGITAL_VOLUME_LEFT,
2585 			   WM8904_DAC_VU, WM8904_DAC_VU);
2586 	regmap_update_bits(wm8904->regmap, WM8904_DAC_DIGITAL_VOLUME_RIGHT,
2587 			   WM8904_DAC_VU, WM8904_DAC_VU);
2588 	regmap_update_bits(wm8904->regmap, WM8904_ANALOGUE_OUT1_LEFT,
2589 			   WM8904_HPOUT_VU | WM8904_HPOUTLZC,
2590 			   WM8904_HPOUT_VU | WM8904_HPOUTLZC);
2591 	regmap_update_bits(wm8904->regmap, WM8904_ANALOGUE_OUT1_RIGHT,
2592 			   WM8904_HPOUT_VU | WM8904_HPOUTRZC,
2593 			   WM8904_HPOUT_VU | WM8904_HPOUTRZC);
2594 	regmap_update_bits(wm8904->regmap, WM8904_ANALOGUE_OUT2_LEFT,
2595 			   WM8904_LINEOUT_VU | WM8904_LINEOUTLZC,
2596 			   WM8904_LINEOUT_VU | WM8904_LINEOUTLZC);
2597 	regmap_update_bits(wm8904->regmap, WM8904_ANALOGUE_OUT2_RIGHT,
2598 			   WM8904_LINEOUT_VU | WM8904_LINEOUTRZC,
2599 			   WM8904_LINEOUT_VU | WM8904_LINEOUTRZC);
2600 	regmap_update_bits(wm8904->regmap, WM8904_CLOCK_RATES_0,
2601 			   WM8904_SR_MODE, 0);
2602 
2603 	/* Apply configuration from the platform data. */
2604 	if (wm8904->pdata) {
2605 		for (i = 0; i < WM8904_GPIO_REGS; i++) {
2606 			/* 0xFFFF in this config means "don't touch" */
2607 			if (wm8904->pdata->gpio_cfg[i] == 0xffff)
2608 				continue;
2609 
2610 			regmap_update_bits(wm8904->regmap,
2611 					   WM8904_GPIO_CONTROL_1 + i,
2612 					   0xffff,
2613 					   wm8904->pdata->gpio_cfg[i]);
2614 		}
2615 
2616 		/* Zero is the default value for these anyway */
2617 		for (i = 0; i < WM8904_MIC_REGS; i++)
2618 			regmap_update_bits(wm8904->regmap,
2619 					   WM8904_MIC_BIAS_CONTROL_0 + i,
2620 					   0xffff,
2621 					   wm8904->pdata->mic_cfg[i]);
2622 	}
2623 
2624 	/* Set Class W by default - this will be managed by the Class
2625 	 * G widget at runtime where bypass paths are available.
2626 	 */
2627 	regmap_update_bits(wm8904->regmap, WM8904_CLASS_W_0,
2628 			    WM8904_CP_DYN_PWR, WM8904_CP_DYN_PWR);
2629 
2630 	/* Use normal bias source */
2631 	regmap_update_bits(wm8904->regmap, WM8904_BIAS_CONTROL_0,
2632 			    WM8904_POBCTRL, 0);
2633 
2634 	/* Fill the cache for the ADC test register */
2635 	regmap_read(wm8904->regmap, WM8904_ADC_TEST_0, &val);
2636 
2637 	/* Can leave the device powered off until we need it */
2638 	regcache_cache_only(wm8904->regmap, true);
2639 	regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
2640 
2641 	ret = devm_snd_soc_register_component(&i2c->dev,
2642 			&soc_component_dev_wm8904, &wm8904_dai, 1);
2643 	if (ret != 0)
2644 		return ret;
2645 
2646 	return 0;
2647 
2648 err_enable:
2649 	regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
2650 	return ret;
2651 }
2652 
2653 static const struct i2c_device_id wm8904_i2c_id[] = {
2654 	{ .name = "wm8904", .driver_data = WM8904 },
2655 	{ .name = "wm8912", .driver_data = WM8912 },
2656 	{ .name = "wm8918", .driver_data = WM8904 },   /* Actually a subset, updates to follow */
2657 	{ }
2658 };
2659 MODULE_DEVICE_TABLE(i2c, wm8904_i2c_id);
2660 
2661 static struct i2c_driver wm8904_i2c_driver = {
2662 	.driver = {
2663 		.name = "wm8904",
2664 		.of_match_table = of_match_ptr(wm8904_of_match),
2665 	},
2666 	.probe = wm8904_i2c_probe,
2667 	.id_table = wm8904_i2c_id,
2668 };
2669 
2670 module_i2c_driver(wm8904_i2c_driver);
2671 
2672 MODULE_DESCRIPTION("ASoC WM8904 driver");
2673 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
2674 MODULE_LICENSE("GPL");
2675