2 * wm8904.c -- WM8904 ALSA SoC Audio driver
4 * Copyright 2009-12 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
14 #include <linux/clk.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
19 #include <linux/i2c.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/initval.h>
28 #include <sound/tlv.h>
29 #include <sound/wm8904.h>
38 #define WM8904_NUM_DCS_CHANNELS 4
40 #define WM8904_NUM_SUPPLIES 5
41 static const char *wm8904_supply_names[WM8904_NUM_SUPPLIES] = {
49 /* codec private data */
51 struct regmap *regmap;
54 enum wm8904_type devtype;
56 struct regulator_bulk_data supplies[WM8904_NUM_SUPPLIES];
58 struct wm8904_pdata *pdata;
62 /* Platform provided DRC configuration */
63 const char **drc_texts;
65 struct soc_enum drc_enum;
67 /* Platform provided ReTune mobile configuration */
68 int num_retune_mobile_texts;
69 const char **retune_mobile_texts;
70 int retune_mobile_cfg;
71 struct soc_enum retune_mobile_enum;
78 /* Clocking configuration */
79 unsigned int mclk_rate;
81 unsigned int sysclk_rate;
88 /* DC servo configuration - cached offset values */
89 int dcs_state[WM8904_NUM_DCS_CHANNELS];
92 static const struct reg_default wm8904_reg_defaults[] = {
93 { 4, 0x0018 }, /* R4 - Bias Control 0 */
94 { 5, 0x0000 }, /* R5 - VMID Control 0 */
95 { 6, 0x0000 }, /* R6 - Mic Bias Control 0 */
96 { 7, 0x0000 }, /* R7 - Mic Bias Control 1 */
97 { 8, 0x0001 }, /* R8 - Analogue DAC 0 */
98 { 9, 0x9696 }, /* R9 - mic Filter Control */
99 { 10, 0x0001 }, /* R10 - Analogue ADC 0 */
100 { 12, 0x0000 }, /* R12 - Power Management 0 */
101 { 14, 0x0000 }, /* R14 - Power Management 2 */
102 { 15, 0x0000 }, /* R15 - Power Management 3 */
103 { 18, 0x0000 }, /* R18 - Power Management 6 */
104 { 20, 0x945E }, /* R20 - Clock Rates 0 */
105 { 21, 0x0C05 }, /* R21 - Clock Rates 1 */
106 { 22, 0x0006 }, /* R22 - Clock Rates 2 */
107 { 24, 0x0050 }, /* R24 - Audio Interface 0 */
108 { 25, 0x000A }, /* R25 - Audio Interface 1 */
109 { 26, 0x00E4 }, /* R26 - Audio Interface 2 */
110 { 27, 0x0040 }, /* R27 - Audio Interface 3 */
111 { 30, 0x00C0 }, /* R30 - DAC Digital Volume Left */
112 { 31, 0x00C0 }, /* R31 - DAC Digital Volume Right */
113 { 32, 0x0000 }, /* R32 - DAC Digital 0 */
114 { 33, 0x0008 }, /* R33 - DAC Digital 1 */
115 { 36, 0x00C0 }, /* R36 - ADC Digital Volume Left */
116 { 37, 0x00C0 }, /* R37 - ADC Digital Volume Right */
117 { 38, 0x0010 }, /* R38 - ADC Digital 0 */
118 { 39, 0x0000 }, /* R39 - Digital Microphone 0 */
119 { 40, 0x01AF }, /* R40 - DRC 0 */
120 { 41, 0x3248 }, /* R41 - DRC 1 */
121 { 42, 0x0000 }, /* R42 - DRC 2 */
122 { 43, 0x0000 }, /* R43 - DRC 3 */
123 { 44, 0x0085 }, /* R44 - Analogue Left Input 0 */
124 { 45, 0x0085 }, /* R45 - Analogue Right Input 0 */
125 { 46, 0x0044 }, /* R46 - Analogue Left Input 1 */
126 { 47, 0x0044 }, /* R47 - Analogue Right Input 1 */
127 { 57, 0x002D }, /* R57 - Analogue OUT1 Left */
128 { 58, 0x002D }, /* R58 - Analogue OUT1 Right */
129 { 59, 0x0039 }, /* R59 - Analogue OUT2 Left */
130 { 60, 0x0039 }, /* R60 - Analogue OUT2 Right */
131 { 61, 0x0000 }, /* R61 - Analogue OUT12 ZC */
132 { 67, 0x0000 }, /* R67 - DC Servo 0 */
133 { 69, 0xAAAA }, /* R69 - DC Servo 2 */
134 { 71, 0xAAAA }, /* R71 - DC Servo 4 */
135 { 72, 0xAAAA }, /* R72 - DC Servo 5 */
136 { 90, 0x0000 }, /* R90 - Analogue HP 0 */
137 { 94, 0x0000 }, /* R94 - Analogue Lineout 0 */
138 { 98, 0x0000 }, /* R98 - Charge Pump 0 */
139 { 104, 0x0004 }, /* R104 - Class W 0 */
140 { 108, 0x0000 }, /* R108 - Write Sequencer 0 */
141 { 109, 0x0000 }, /* R109 - Write Sequencer 1 */
142 { 110, 0x0000 }, /* R110 - Write Sequencer 2 */
143 { 111, 0x0000 }, /* R111 - Write Sequencer 3 */
144 { 112, 0x0000 }, /* R112 - Write Sequencer 4 */
145 { 116, 0x0000 }, /* R116 - FLL Control 1 */
146 { 117, 0x0007 }, /* R117 - FLL Control 2 */
147 { 118, 0x0000 }, /* R118 - FLL Control 3 */
148 { 119, 0x2EE0 }, /* R119 - FLL Control 4 */
149 { 120, 0x0004 }, /* R120 - FLL Control 5 */
150 { 121, 0x0014 }, /* R121 - GPIO Control 1 */
151 { 122, 0x0010 }, /* R122 - GPIO Control 2 */
152 { 123, 0x0010 }, /* R123 - GPIO Control 3 */
153 { 124, 0x0000 }, /* R124 - GPIO Control 4 */
154 { 126, 0x0000 }, /* R126 - Digital Pulls */
155 { 128, 0xFFFF }, /* R128 - Interrupt Status Mask */
156 { 129, 0x0000 }, /* R129 - Interrupt Polarity */
157 { 130, 0x0000 }, /* R130 - Interrupt Debounce */
158 { 134, 0x0000 }, /* R134 - EQ1 */
159 { 135, 0x000C }, /* R135 - EQ2 */
160 { 136, 0x000C }, /* R136 - EQ3 */
161 { 137, 0x000C }, /* R137 - EQ4 */
162 { 138, 0x000C }, /* R138 - EQ5 */
163 { 139, 0x000C }, /* R139 - EQ6 */
164 { 140, 0x0FCA }, /* R140 - EQ7 */
165 { 141, 0x0400 }, /* R141 - EQ8 */
166 { 142, 0x00D8 }, /* R142 - EQ9 */
167 { 143, 0x1EB5 }, /* R143 - EQ10 */
168 { 144, 0xF145 }, /* R144 - EQ11 */
169 { 145, 0x0B75 }, /* R145 - EQ12 */
170 { 146, 0x01C5 }, /* R146 - EQ13 */
171 { 147, 0x1C58 }, /* R147 - EQ14 */
172 { 148, 0xF373 }, /* R148 - EQ15 */
173 { 149, 0x0A54 }, /* R149 - EQ16 */
174 { 150, 0x0558 }, /* R150 - EQ17 */
175 { 151, 0x168E }, /* R151 - EQ18 */
176 { 152, 0xF829 }, /* R152 - EQ19 */
177 { 153, 0x07AD }, /* R153 - EQ20 */
178 { 154, 0x1103 }, /* R154 - EQ21 */
179 { 155, 0x0564 }, /* R155 - EQ22 */
180 { 156, 0x0559 }, /* R156 - EQ23 */
181 { 157, 0x4000 }, /* R157 - EQ24 */
182 { 161, 0x0000 }, /* R161 - Control Interface Test 1 */
183 { 204, 0x0000 }, /* R204 - Analogue Output Bias 0 */
184 { 247, 0x0000 }, /* R247 - FLL NCO Test 0 */
185 { 248, 0x0019 }, /* R248 - FLL NCO Test 1 */
188 static bool wm8904_volatile_register(struct device *dev, unsigned int reg)
191 case WM8904_SW_RESET_AND_ID:
192 case WM8904_REVISION:
193 case WM8904_DC_SERVO_1:
194 case WM8904_DC_SERVO_6:
195 case WM8904_DC_SERVO_7:
196 case WM8904_DC_SERVO_8:
197 case WM8904_DC_SERVO_9:
198 case WM8904_DC_SERVO_READBACK_0:
199 case WM8904_INTERRUPT_STATUS:
206 static bool wm8904_readable_register(struct device *dev, unsigned int reg)
209 case WM8904_SW_RESET_AND_ID:
210 case WM8904_REVISION:
211 case WM8904_BIAS_CONTROL_0:
212 case WM8904_VMID_CONTROL_0:
213 case WM8904_MIC_BIAS_CONTROL_0:
214 case WM8904_MIC_BIAS_CONTROL_1:
215 case WM8904_ANALOGUE_DAC_0:
216 case WM8904_MIC_FILTER_CONTROL:
217 case WM8904_ANALOGUE_ADC_0:
218 case WM8904_POWER_MANAGEMENT_0:
219 case WM8904_POWER_MANAGEMENT_2:
220 case WM8904_POWER_MANAGEMENT_3:
221 case WM8904_POWER_MANAGEMENT_6:
222 case WM8904_CLOCK_RATES_0:
223 case WM8904_CLOCK_RATES_1:
224 case WM8904_CLOCK_RATES_2:
225 case WM8904_AUDIO_INTERFACE_0:
226 case WM8904_AUDIO_INTERFACE_1:
227 case WM8904_AUDIO_INTERFACE_2:
228 case WM8904_AUDIO_INTERFACE_3:
229 case WM8904_DAC_DIGITAL_VOLUME_LEFT:
230 case WM8904_DAC_DIGITAL_VOLUME_RIGHT:
231 case WM8904_DAC_DIGITAL_0:
232 case WM8904_DAC_DIGITAL_1:
233 case WM8904_ADC_DIGITAL_VOLUME_LEFT:
234 case WM8904_ADC_DIGITAL_VOLUME_RIGHT:
235 case WM8904_ADC_DIGITAL_0:
236 case WM8904_DIGITAL_MICROPHONE_0:
241 case WM8904_ANALOGUE_LEFT_INPUT_0:
242 case WM8904_ANALOGUE_RIGHT_INPUT_0:
243 case WM8904_ANALOGUE_LEFT_INPUT_1:
244 case WM8904_ANALOGUE_RIGHT_INPUT_1:
245 case WM8904_ANALOGUE_OUT1_LEFT:
246 case WM8904_ANALOGUE_OUT1_RIGHT:
247 case WM8904_ANALOGUE_OUT2_LEFT:
248 case WM8904_ANALOGUE_OUT2_RIGHT:
249 case WM8904_ANALOGUE_OUT12_ZC:
250 case WM8904_DC_SERVO_0:
251 case WM8904_DC_SERVO_1:
252 case WM8904_DC_SERVO_2:
253 case WM8904_DC_SERVO_4:
254 case WM8904_DC_SERVO_5:
255 case WM8904_DC_SERVO_6:
256 case WM8904_DC_SERVO_7:
257 case WM8904_DC_SERVO_8:
258 case WM8904_DC_SERVO_9:
259 case WM8904_DC_SERVO_READBACK_0:
260 case WM8904_ANALOGUE_HP_0:
261 case WM8904_ANALOGUE_LINEOUT_0:
262 case WM8904_CHARGE_PUMP_0:
263 case WM8904_CLASS_W_0:
264 case WM8904_WRITE_SEQUENCER_0:
265 case WM8904_WRITE_SEQUENCER_1:
266 case WM8904_WRITE_SEQUENCER_2:
267 case WM8904_WRITE_SEQUENCER_3:
268 case WM8904_WRITE_SEQUENCER_4:
269 case WM8904_FLL_CONTROL_1:
270 case WM8904_FLL_CONTROL_2:
271 case WM8904_FLL_CONTROL_3:
272 case WM8904_FLL_CONTROL_4:
273 case WM8904_FLL_CONTROL_5:
274 case WM8904_GPIO_CONTROL_1:
275 case WM8904_GPIO_CONTROL_2:
276 case WM8904_GPIO_CONTROL_3:
277 case WM8904_GPIO_CONTROL_4:
278 case WM8904_DIGITAL_PULLS:
279 case WM8904_INTERRUPT_STATUS:
280 case WM8904_INTERRUPT_STATUS_MASK:
281 case WM8904_INTERRUPT_POLARITY:
282 case WM8904_INTERRUPT_DEBOUNCE:
307 case WM8904_CONTROL_INTERFACE_TEST_1:
308 case WM8904_ADC_TEST_0:
309 case WM8904_ANALOGUE_OUTPUT_BIAS_0:
310 case WM8904_FLL_NCO_TEST_0:
311 case WM8904_FLL_NCO_TEST_1:
318 static int wm8904_configure_clocking(struct snd_soc_component *component)
320 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
321 unsigned int clock0, clock2, rate;
323 /* Gate the clock while we're updating to avoid misclocking */
324 clock2 = snd_soc_component_read32(component, WM8904_CLOCK_RATES_2);
325 snd_soc_component_update_bits(component, WM8904_CLOCK_RATES_2,
326 WM8904_SYSCLK_SRC, 0);
328 /* This should be done on init() for bypass paths */
329 switch (wm8904->sysclk_src) {
330 case WM8904_CLK_MCLK:
331 dev_dbg(component->dev, "Using %dHz MCLK\n", wm8904->mclk_rate);
333 clock2 &= ~WM8904_SYSCLK_SRC;
334 rate = wm8904->mclk_rate;
336 /* Ensure the FLL is stopped */
337 snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
338 WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
342 dev_dbg(component->dev, "Using %dHz FLL clock\n",
345 clock2 |= WM8904_SYSCLK_SRC;
346 rate = wm8904->fll_fout;
350 dev_err(component->dev, "System clock not configured\n");
354 /* SYSCLK shouldn't be over 13.5MHz */
355 if (rate > 13500000) {
356 clock0 = WM8904_MCLK_DIV;
357 wm8904->sysclk_rate = rate / 2;
360 wm8904->sysclk_rate = rate;
363 snd_soc_component_update_bits(component, WM8904_CLOCK_RATES_0, WM8904_MCLK_DIV,
366 snd_soc_component_update_bits(component, WM8904_CLOCK_RATES_2,
367 WM8904_CLK_SYS_ENA | WM8904_SYSCLK_SRC, clock2);
369 dev_dbg(component->dev, "CLK_SYS is %dHz\n", wm8904->sysclk_rate);
374 static void wm8904_set_drc(struct snd_soc_component *component)
376 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
377 struct wm8904_pdata *pdata = wm8904->pdata;
380 /* Save any enables; the configuration should clear them. */
381 save = snd_soc_component_read32(component, WM8904_DRC_0);
383 for (i = 0; i < WM8904_DRC_REGS; i++)
384 snd_soc_component_update_bits(component, WM8904_DRC_0 + i, 0xffff,
385 pdata->drc_cfgs[wm8904->drc_cfg].regs[i]);
387 /* Reenable the DRC */
388 snd_soc_component_update_bits(component, WM8904_DRC_0,
389 WM8904_DRC_ENA | WM8904_DRC_DAC_PATH, save);
392 static int wm8904_put_drc_enum(struct snd_kcontrol *kcontrol,
393 struct snd_ctl_elem_value *ucontrol)
395 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
396 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
397 struct wm8904_pdata *pdata = wm8904->pdata;
398 int value = ucontrol->value.enumerated.item[0];
400 if (value >= pdata->num_drc_cfgs)
403 wm8904->drc_cfg = value;
405 wm8904_set_drc(component);
410 static int wm8904_get_drc_enum(struct snd_kcontrol *kcontrol,
411 struct snd_ctl_elem_value *ucontrol)
413 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
414 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
416 ucontrol->value.enumerated.item[0] = wm8904->drc_cfg;
421 static void wm8904_set_retune_mobile(struct snd_soc_component *component)
423 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
424 struct wm8904_pdata *pdata = wm8904->pdata;
425 int best, best_val, save, i, cfg;
427 if (!pdata || !wm8904->num_retune_mobile_texts)
430 /* Find the version of the currently selected configuration
431 * with the nearest sample rate. */
432 cfg = wm8904->retune_mobile_cfg;
435 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
436 if (strcmp(pdata->retune_mobile_cfgs[i].name,
437 wm8904->retune_mobile_texts[cfg]) == 0 &&
438 abs(pdata->retune_mobile_cfgs[i].rate
439 - wm8904->fs) < best_val) {
441 best_val = abs(pdata->retune_mobile_cfgs[i].rate
446 dev_dbg(component->dev, "ReTune Mobile %s/%dHz for %dHz sample rate\n",
447 pdata->retune_mobile_cfgs[best].name,
448 pdata->retune_mobile_cfgs[best].rate,
451 /* The EQ will be disabled while reconfiguring it, remember the
452 * current configuration.
454 save = snd_soc_component_read32(component, WM8904_EQ1);
456 for (i = 0; i < WM8904_EQ_REGS; i++)
457 snd_soc_component_update_bits(component, WM8904_EQ1 + i, 0xffff,
458 pdata->retune_mobile_cfgs[best].regs[i]);
460 snd_soc_component_update_bits(component, WM8904_EQ1, WM8904_EQ_ENA, save);
463 static int wm8904_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
464 struct snd_ctl_elem_value *ucontrol)
466 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
467 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
468 struct wm8904_pdata *pdata = wm8904->pdata;
469 int value = ucontrol->value.enumerated.item[0];
471 if (value >= pdata->num_retune_mobile_cfgs)
474 wm8904->retune_mobile_cfg = value;
476 wm8904_set_retune_mobile(component);
481 static int wm8904_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
482 struct snd_ctl_elem_value *ucontrol)
484 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
485 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
487 ucontrol->value.enumerated.item[0] = wm8904->retune_mobile_cfg;
492 static int deemph_settings[] = { 0, 32000, 44100, 48000 };
494 static int wm8904_set_deemph(struct snd_soc_component *component)
496 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
499 /* If we're using deemphasis select the nearest available sample
502 if (wm8904->deemph) {
504 for (i = 2; i < ARRAY_SIZE(deemph_settings); i++) {
505 if (abs(deemph_settings[i] - wm8904->fs) <
506 abs(deemph_settings[best] - wm8904->fs))
510 val = best << WM8904_DEEMPH_SHIFT;
515 dev_dbg(component->dev, "Set deemphasis %d\n", val);
517 return snd_soc_component_update_bits(component, WM8904_DAC_DIGITAL_1,
518 WM8904_DEEMPH_MASK, val);
521 static int wm8904_get_deemph(struct snd_kcontrol *kcontrol,
522 struct snd_ctl_elem_value *ucontrol)
524 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
525 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
527 ucontrol->value.integer.value[0] = wm8904->deemph;
531 static int wm8904_put_deemph(struct snd_kcontrol *kcontrol,
532 struct snd_ctl_elem_value *ucontrol)
534 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
535 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
536 unsigned int deemph = ucontrol->value.integer.value[0];
541 wm8904->deemph = deemph;
543 return wm8904_set_deemph(component);
546 static const DECLARE_TLV_DB_SCALE(dac_boost_tlv, 0, 600, 0);
547 static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
548 static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
549 static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 300, 0);
550 static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
552 static const char *input_mode_text[] = {
553 "Single-Ended", "Differential Line", "Differential Mic"
556 static SOC_ENUM_SINGLE_DECL(lin_mode,
557 WM8904_ANALOGUE_LEFT_INPUT_1, 0,
560 static SOC_ENUM_SINGLE_DECL(rin_mode,
561 WM8904_ANALOGUE_RIGHT_INPUT_1, 0,
564 static const char *hpf_mode_text[] = {
565 "Hi-fi", "Voice 1", "Voice 2", "Voice 3"
568 static SOC_ENUM_SINGLE_DECL(hpf_mode, WM8904_ADC_DIGITAL_0, 5,
571 static int wm8904_adc_osr_put(struct snd_kcontrol *kcontrol,
572 struct snd_ctl_elem_value *ucontrol)
574 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
578 ret = snd_soc_put_volsw(kcontrol, ucontrol);
582 if (ucontrol->value.integer.value[0])
585 val = WM8904_ADC_128_OSR_TST_MODE | WM8904_ADC_BIASX1P5;
587 snd_soc_component_update_bits(component, WM8904_ADC_TEST_0,
588 WM8904_ADC_128_OSR_TST_MODE | WM8904_ADC_BIASX1P5,
594 static const struct snd_kcontrol_new wm8904_adc_snd_controls[] = {
595 SOC_DOUBLE_R_TLV("Digital Capture Volume", WM8904_ADC_DIGITAL_VOLUME_LEFT,
596 WM8904_ADC_DIGITAL_VOLUME_RIGHT, 1, 119, 0, digital_tlv),
598 SOC_ENUM("Left Capture Mode", lin_mode),
599 SOC_ENUM("Right Capture Mode", rin_mode),
601 /* No TLV since it depends on mode */
602 SOC_DOUBLE_R("Capture Volume", WM8904_ANALOGUE_LEFT_INPUT_0,
603 WM8904_ANALOGUE_RIGHT_INPUT_0, 0, 31, 0),
604 SOC_DOUBLE_R("Capture Switch", WM8904_ANALOGUE_LEFT_INPUT_0,
605 WM8904_ANALOGUE_RIGHT_INPUT_0, 7, 1, 1),
607 SOC_SINGLE("High Pass Filter Switch", WM8904_ADC_DIGITAL_0, 4, 1, 0),
608 SOC_ENUM("High Pass Filter Mode", hpf_mode),
609 SOC_SINGLE_EXT("ADC 128x OSR Switch", WM8904_ANALOGUE_ADC_0, 0, 1, 0,
610 snd_soc_get_volsw, wm8904_adc_osr_put),
613 static const char *drc_path_text[] = {
617 static SOC_ENUM_SINGLE_DECL(drc_path, WM8904_DRC_0, 14, drc_path_text);
619 static const struct snd_kcontrol_new wm8904_dac_snd_controls[] = {
620 SOC_SINGLE_TLV("Digital Playback Boost Volume",
621 WM8904_AUDIO_INTERFACE_0, 9, 3, 0, dac_boost_tlv),
622 SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8904_DAC_DIGITAL_VOLUME_LEFT,
623 WM8904_DAC_DIGITAL_VOLUME_RIGHT, 1, 96, 0, digital_tlv),
625 SOC_DOUBLE_R_TLV("Headphone Volume", WM8904_ANALOGUE_OUT1_LEFT,
626 WM8904_ANALOGUE_OUT1_RIGHT, 0, 63, 0, out_tlv),
627 SOC_DOUBLE_R("Headphone Switch", WM8904_ANALOGUE_OUT1_LEFT,
628 WM8904_ANALOGUE_OUT1_RIGHT, 8, 1, 1),
629 SOC_DOUBLE_R("Headphone ZC Switch", WM8904_ANALOGUE_OUT1_LEFT,
630 WM8904_ANALOGUE_OUT1_RIGHT, 6, 1, 0),
632 SOC_DOUBLE_R_TLV("Line Output Volume", WM8904_ANALOGUE_OUT2_LEFT,
633 WM8904_ANALOGUE_OUT2_RIGHT, 0, 63, 0, out_tlv),
634 SOC_DOUBLE_R("Line Output Switch", WM8904_ANALOGUE_OUT2_LEFT,
635 WM8904_ANALOGUE_OUT2_RIGHT, 8, 1, 1),
636 SOC_DOUBLE_R("Line Output ZC Switch", WM8904_ANALOGUE_OUT2_LEFT,
637 WM8904_ANALOGUE_OUT2_RIGHT, 6, 1, 0),
639 SOC_SINGLE("EQ Switch", WM8904_EQ1, 0, 1, 0),
640 SOC_SINGLE("DRC Switch", WM8904_DRC_0, 15, 1, 0),
641 SOC_ENUM("DRC Path", drc_path),
642 SOC_SINGLE("DAC OSRx2 Switch", WM8904_DAC_DIGITAL_1, 6, 1, 0),
643 SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
644 wm8904_get_deemph, wm8904_put_deemph),
647 static const struct snd_kcontrol_new wm8904_snd_controls[] = {
648 SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8904_DAC_DIGITAL_0, 4, 8, 15, 0,
652 static const struct snd_kcontrol_new wm8904_eq_controls[] = {
653 SOC_SINGLE_TLV("EQ1 Volume", WM8904_EQ2, 0, 24, 0, eq_tlv),
654 SOC_SINGLE_TLV("EQ2 Volume", WM8904_EQ3, 0, 24, 0, eq_tlv),
655 SOC_SINGLE_TLV("EQ3 Volume", WM8904_EQ4, 0, 24, 0, eq_tlv),
656 SOC_SINGLE_TLV("EQ4 Volume", WM8904_EQ5, 0, 24, 0, eq_tlv),
657 SOC_SINGLE_TLV("EQ5 Volume", WM8904_EQ6, 0, 24, 0, eq_tlv),
660 static int cp_event(struct snd_soc_dapm_widget *w,
661 struct snd_kcontrol *kcontrol, int event)
663 if (WARN_ON(event != SND_SOC_DAPM_POST_PMU))
666 /* Maximum startup time */
672 static int sysclk_event(struct snd_soc_dapm_widget *w,
673 struct snd_kcontrol *kcontrol, int event)
675 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
676 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
679 case SND_SOC_DAPM_PRE_PMU:
680 /* If we're using the FLL then we only start it when
681 * required; we assume that the configuration has been
682 * done previously and all we need to do is kick it
685 switch (wm8904->sysclk_src) {
687 snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
691 snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
701 case SND_SOC_DAPM_POST_PMD:
702 snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
703 WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
710 static int out_pga_event(struct snd_soc_dapm_widget *w,
711 struct snd_kcontrol *kcontrol, int event)
713 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
714 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
718 int dcs_l_reg, dcs_r_reg;
722 /* This code is shared between HP and LINEOUT; we do all our
723 * power management in stereo pairs to avoid latency issues so
724 * we reuse shift to identify which rather than strcmp() the
729 case WM8904_ANALOGUE_HP_0:
730 pwr_reg = WM8904_POWER_MANAGEMENT_2;
731 dcs_mask = WM8904_DCS_ENA_CHAN_0 | WM8904_DCS_ENA_CHAN_1;
732 dcs_r_reg = WM8904_DC_SERVO_8;
733 dcs_l_reg = WM8904_DC_SERVO_9;
737 case WM8904_ANALOGUE_LINEOUT_0:
738 pwr_reg = WM8904_POWER_MANAGEMENT_3;
739 dcs_mask = WM8904_DCS_ENA_CHAN_2 | WM8904_DCS_ENA_CHAN_3;
740 dcs_r_reg = WM8904_DC_SERVO_6;
741 dcs_l_reg = WM8904_DC_SERVO_7;
746 WARN(1, "Invalid reg %d\n", reg);
751 case SND_SOC_DAPM_PRE_PMU:
752 /* Power on the PGAs */
753 snd_soc_component_update_bits(component, pwr_reg,
754 WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA,
755 WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA);
757 /* Power on the amplifier */
758 snd_soc_component_update_bits(component, reg,
759 WM8904_HPL_ENA | WM8904_HPR_ENA,
760 WM8904_HPL_ENA | WM8904_HPR_ENA);
763 /* Enable the first stage */
764 snd_soc_component_update_bits(component, reg,
765 WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY,
766 WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY);
768 /* Power up the DC servo */
769 snd_soc_component_update_bits(component, WM8904_DC_SERVO_0,
772 /* Either calibrate the DC servo or restore cached state
775 if (wm8904->dcs_state[dcs_l] || wm8904->dcs_state[dcs_r]) {
776 dev_dbg(component->dev, "Restoring DC servo state\n");
778 snd_soc_component_write(component, dcs_l_reg,
779 wm8904->dcs_state[dcs_l]);
780 snd_soc_component_write(component, dcs_r_reg,
781 wm8904->dcs_state[dcs_r]);
783 snd_soc_component_write(component, WM8904_DC_SERVO_1, dcs_mask);
787 dev_dbg(component->dev, "Calibrating DC servo\n");
789 snd_soc_component_write(component, WM8904_DC_SERVO_1,
790 dcs_mask << WM8904_DCS_TRIG_STARTUP_0_SHIFT);
795 /* Wait for DC servo to complete */
796 dcs_mask <<= WM8904_DCS_CAL_COMPLETE_SHIFT;
798 val = snd_soc_component_read32(component, WM8904_DC_SERVO_READBACK_0);
799 if ((val & dcs_mask) == dcs_mask)
805 if ((val & dcs_mask) != dcs_mask)
806 dev_warn(component->dev, "DC servo timed out\n");
808 dev_dbg(component->dev, "DC servo ready\n");
810 /* Enable the output stage */
811 snd_soc_component_update_bits(component, reg,
812 WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP,
813 WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP);
816 case SND_SOC_DAPM_POST_PMU:
817 /* Unshort the output itself */
818 snd_soc_component_update_bits(component, reg,
819 WM8904_HPL_RMV_SHORT |
820 WM8904_HPR_RMV_SHORT,
821 WM8904_HPL_RMV_SHORT |
822 WM8904_HPR_RMV_SHORT);
826 case SND_SOC_DAPM_PRE_PMD:
827 /* Short the output */
828 snd_soc_component_update_bits(component, reg,
829 WM8904_HPL_RMV_SHORT |
830 WM8904_HPR_RMV_SHORT, 0);
833 case SND_SOC_DAPM_POST_PMD:
834 /* Cache the DC servo configuration; this will be
835 * invalidated if we change the configuration. */
836 wm8904->dcs_state[dcs_l] = snd_soc_component_read32(component, dcs_l_reg);
837 wm8904->dcs_state[dcs_r] = snd_soc_component_read32(component, dcs_r_reg);
839 snd_soc_component_update_bits(component, WM8904_DC_SERVO_0,
842 /* Disable the amplifier input and output stages */
843 snd_soc_component_update_bits(component, reg,
844 WM8904_HPL_ENA | WM8904_HPR_ENA |
845 WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY |
846 WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP,
850 snd_soc_component_update_bits(component, pwr_reg,
851 WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA,
859 static const char *lin_text[] = {
860 "IN1L", "IN2L", "IN3L"
863 static SOC_ENUM_SINGLE_DECL(lin_enum, WM8904_ANALOGUE_LEFT_INPUT_1, 2,
866 static const struct snd_kcontrol_new lin_mux =
867 SOC_DAPM_ENUM("Left Capture Mux", lin_enum);
869 static SOC_ENUM_SINGLE_DECL(lin_inv_enum, WM8904_ANALOGUE_LEFT_INPUT_1, 4,
872 static const struct snd_kcontrol_new lin_inv_mux =
873 SOC_DAPM_ENUM("Left Capture Inveting Mux", lin_inv_enum);
875 static const char *rin_text[] = {
876 "IN1R", "IN2R", "IN3R"
879 static SOC_ENUM_SINGLE_DECL(rin_enum, WM8904_ANALOGUE_RIGHT_INPUT_1, 2,
882 static const struct snd_kcontrol_new rin_mux =
883 SOC_DAPM_ENUM("Right Capture Mux", rin_enum);
885 static SOC_ENUM_SINGLE_DECL(rin_inv_enum, WM8904_ANALOGUE_RIGHT_INPUT_1, 4,
888 static const struct snd_kcontrol_new rin_inv_mux =
889 SOC_DAPM_ENUM("Right Capture Inveting Mux", rin_inv_enum);
891 static const char *aif_text[] = {
895 static SOC_ENUM_SINGLE_DECL(aifoutl_enum, WM8904_AUDIO_INTERFACE_0, 7,
898 static const struct snd_kcontrol_new aifoutl_mux =
899 SOC_DAPM_ENUM("AIFOUTL Mux", aifoutl_enum);
901 static SOC_ENUM_SINGLE_DECL(aifoutr_enum, WM8904_AUDIO_INTERFACE_0, 6,
904 static const struct snd_kcontrol_new aifoutr_mux =
905 SOC_DAPM_ENUM("AIFOUTR Mux", aifoutr_enum);
907 static SOC_ENUM_SINGLE_DECL(aifinl_enum, WM8904_AUDIO_INTERFACE_0, 5,
910 static const struct snd_kcontrol_new aifinl_mux =
911 SOC_DAPM_ENUM("AIFINL Mux", aifinl_enum);
913 static SOC_ENUM_SINGLE_DECL(aifinr_enum, WM8904_AUDIO_INTERFACE_0, 4,
916 static const struct snd_kcontrol_new aifinr_mux =
917 SOC_DAPM_ENUM("AIFINR Mux", aifinr_enum);
919 static const struct snd_soc_dapm_widget wm8904_core_dapm_widgets[] = {
920 SND_SOC_DAPM_SUPPLY("SYSCLK", WM8904_CLOCK_RATES_2, 2, 0, sysclk_event,
921 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
922 SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8904_CLOCK_RATES_2, 1, 0, NULL, 0),
923 SND_SOC_DAPM_SUPPLY("TOCLK", WM8904_CLOCK_RATES_2, 0, 0, NULL, 0),
926 static const struct snd_soc_dapm_widget wm8904_adc_dapm_widgets[] = {
927 SND_SOC_DAPM_INPUT("IN1L"),
928 SND_SOC_DAPM_INPUT("IN1R"),
929 SND_SOC_DAPM_INPUT("IN2L"),
930 SND_SOC_DAPM_INPUT("IN2R"),
931 SND_SOC_DAPM_INPUT("IN3L"),
932 SND_SOC_DAPM_INPUT("IN3R"),
934 SND_SOC_DAPM_SUPPLY("MICBIAS", WM8904_MIC_BIAS_CONTROL_0, 0, 0, NULL, 0),
936 SND_SOC_DAPM_MUX("Left Capture Mux", SND_SOC_NOPM, 0, 0, &lin_mux),
937 SND_SOC_DAPM_MUX("Left Capture Inverting Mux", SND_SOC_NOPM, 0, 0,
939 SND_SOC_DAPM_MUX("Right Capture Mux", SND_SOC_NOPM, 0, 0, &rin_mux),
940 SND_SOC_DAPM_MUX("Right Capture Inverting Mux", SND_SOC_NOPM, 0, 0,
943 SND_SOC_DAPM_PGA("Left Capture PGA", WM8904_POWER_MANAGEMENT_0, 1, 0,
945 SND_SOC_DAPM_PGA("Right Capture PGA", WM8904_POWER_MANAGEMENT_0, 0, 0,
948 SND_SOC_DAPM_ADC("ADCL", NULL, WM8904_POWER_MANAGEMENT_6, 1, 0),
949 SND_SOC_DAPM_ADC("ADCR", NULL, WM8904_POWER_MANAGEMENT_6, 0, 0),
951 SND_SOC_DAPM_MUX("AIFOUTL Mux", SND_SOC_NOPM, 0, 0, &aifoutl_mux),
952 SND_SOC_DAPM_MUX("AIFOUTR Mux", SND_SOC_NOPM, 0, 0, &aifoutr_mux),
954 SND_SOC_DAPM_AIF_OUT("AIFOUTL", "Capture", 0, SND_SOC_NOPM, 0, 0),
955 SND_SOC_DAPM_AIF_OUT("AIFOUTR", "Capture", 1, SND_SOC_NOPM, 0, 0),
958 static const struct snd_soc_dapm_widget wm8904_dac_dapm_widgets[] = {
959 SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
960 SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
962 SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &aifinl_mux),
963 SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &aifinr_mux),
965 SND_SOC_DAPM_DAC("DACL", NULL, WM8904_POWER_MANAGEMENT_6, 3, 0),
966 SND_SOC_DAPM_DAC("DACR", NULL, WM8904_POWER_MANAGEMENT_6, 2, 0),
968 SND_SOC_DAPM_SUPPLY("Charge pump", WM8904_CHARGE_PUMP_0, 0, 0, cp_event,
969 SND_SOC_DAPM_POST_PMU),
971 SND_SOC_DAPM_PGA("HPL PGA", SND_SOC_NOPM, 1, 0, NULL, 0),
972 SND_SOC_DAPM_PGA("HPR PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
974 SND_SOC_DAPM_PGA("LINEL PGA", SND_SOC_NOPM, 1, 0, NULL, 0),
975 SND_SOC_DAPM_PGA("LINER PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
977 SND_SOC_DAPM_PGA_E("Headphone Output", SND_SOC_NOPM, WM8904_ANALOGUE_HP_0,
978 0, NULL, 0, out_pga_event,
979 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
980 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
981 SND_SOC_DAPM_PGA_E("Line Output", SND_SOC_NOPM, WM8904_ANALOGUE_LINEOUT_0,
982 0, NULL, 0, out_pga_event,
983 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
984 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
986 SND_SOC_DAPM_OUTPUT("HPOUTL"),
987 SND_SOC_DAPM_OUTPUT("HPOUTR"),
988 SND_SOC_DAPM_OUTPUT("LINEOUTL"),
989 SND_SOC_DAPM_OUTPUT("LINEOUTR"),
992 static const char *out_mux_text[] = {
996 static SOC_ENUM_SINGLE_DECL(hpl_enum, WM8904_ANALOGUE_OUT12_ZC, 3,
999 static const struct snd_kcontrol_new hpl_mux =
1000 SOC_DAPM_ENUM("HPL Mux", hpl_enum);
1002 static SOC_ENUM_SINGLE_DECL(hpr_enum, WM8904_ANALOGUE_OUT12_ZC, 2,
1005 static const struct snd_kcontrol_new hpr_mux =
1006 SOC_DAPM_ENUM("HPR Mux", hpr_enum);
1008 static SOC_ENUM_SINGLE_DECL(linel_enum, WM8904_ANALOGUE_OUT12_ZC, 1,
1011 static const struct snd_kcontrol_new linel_mux =
1012 SOC_DAPM_ENUM("LINEL Mux", linel_enum);
1014 static SOC_ENUM_SINGLE_DECL(liner_enum, WM8904_ANALOGUE_OUT12_ZC, 0,
1017 static const struct snd_kcontrol_new liner_mux =
1018 SOC_DAPM_ENUM("LINER Mux", liner_enum);
1020 static const char *sidetone_text[] = {
1021 "None", "Left", "Right"
1024 static SOC_ENUM_SINGLE_DECL(dacl_sidetone_enum, WM8904_DAC_DIGITAL_0, 2,
1027 static const struct snd_kcontrol_new dacl_sidetone_mux =
1028 SOC_DAPM_ENUM("Left Sidetone Mux", dacl_sidetone_enum);
1030 static SOC_ENUM_SINGLE_DECL(dacr_sidetone_enum, WM8904_DAC_DIGITAL_0, 0,
1033 static const struct snd_kcontrol_new dacr_sidetone_mux =
1034 SOC_DAPM_ENUM("Right Sidetone Mux", dacr_sidetone_enum);
1036 static const struct snd_soc_dapm_widget wm8904_dapm_widgets[] = {
1037 SND_SOC_DAPM_SUPPLY("Class G", WM8904_CLASS_W_0, 0, 1, NULL, 0),
1038 SND_SOC_DAPM_PGA("Left Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
1039 SND_SOC_DAPM_PGA("Right Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
1041 SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &dacl_sidetone_mux),
1042 SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &dacr_sidetone_mux),
1044 SND_SOC_DAPM_MUX("HPL Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
1045 SND_SOC_DAPM_MUX("HPR Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
1046 SND_SOC_DAPM_MUX("LINEL Mux", SND_SOC_NOPM, 0, 0, &linel_mux),
1047 SND_SOC_DAPM_MUX("LINER Mux", SND_SOC_NOPM, 0, 0, &liner_mux),
1050 static const struct snd_soc_dapm_route core_intercon[] = {
1051 { "CLK_DSP", NULL, "SYSCLK" },
1052 { "TOCLK", NULL, "SYSCLK" },
1055 static const struct snd_soc_dapm_route adc_intercon[] = {
1056 { "Left Capture Mux", "IN1L", "IN1L" },
1057 { "Left Capture Mux", "IN2L", "IN2L" },
1058 { "Left Capture Mux", "IN3L", "IN3L" },
1060 { "Left Capture Inverting Mux", "IN1L", "IN1L" },
1061 { "Left Capture Inverting Mux", "IN2L", "IN2L" },
1062 { "Left Capture Inverting Mux", "IN3L", "IN3L" },
1064 { "Right Capture Mux", "IN1R", "IN1R" },
1065 { "Right Capture Mux", "IN2R", "IN2R" },
1066 { "Right Capture Mux", "IN3R", "IN3R" },
1068 { "Right Capture Inverting Mux", "IN1R", "IN1R" },
1069 { "Right Capture Inverting Mux", "IN2R", "IN2R" },
1070 { "Right Capture Inverting Mux", "IN3R", "IN3R" },
1072 { "Left Capture PGA", NULL, "Left Capture Mux" },
1073 { "Left Capture PGA", NULL, "Left Capture Inverting Mux" },
1075 { "Right Capture PGA", NULL, "Right Capture Mux" },
1076 { "Right Capture PGA", NULL, "Right Capture Inverting Mux" },
1078 { "AIFOUTL Mux", "Left", "ADCL" },
1079 { "AIFOUTL Mux", "Right", "ADCR" },
1080 { "AIFOUTR Mux", "Left", "ADCL" },
1081 { "AIFOUTR Mux", "Right", "ADCR" },
1083 { "AIFOUTL", NULL, "AIFOUTL Mux" },
1084 { "AIFOUTR", NULL, "AIFOUTR Mux" },
1086 { "ADCL", NULL, "CLK_DSP" },
1087 { "ADCL", NULL, "Left Capture PGA" },
1089 { "ADCR", NULL, "CLK_DSP" },
1090 { "ADCR", NULL, "Right Capture PGA" },
1093 static const struct snd_soc_dapm_route dac_intercon[] = {
1094 { "DACL Mux", "Left", "AIFINL" },
1095 { "DACL Mux", "Right", "AIFINR" },
1097 { "DACR Mux", "Left", "AIFINL" },
1098 { "DACR Mux", "Right", "AIFINR" },
1100 { "DACL", NULL, "DACL Mux" },
1101 { "DACL", NULL, "CLK_DSP" },
1103 { "DACR", NULL, "DACR Mux" },
1104 { "DACR", NULL, "CLK_DSP" },
1106 { "Charge pump", NULL, "SYSCLK" },
1108 { "Headphone Output", NULL, "HPL PGA" },
1109 { "Headphone Output", NULL, "HPR PGA" },
1110 { "Headphone Output", NULL, "Charge pump" },
1111 { "Headphone Output", NULL, "TOCLK" },
1113 { "Line Output", NULL, "LINEL PGA" },
1114 { "Line Output", NULL, "LINER PGA" },
1115 { "Line Output", NULL, "Charge pump" },
1116 { "Line Output", NULL, "TOCLK" },
1118 { "HPOUTL", NULL, "Headphone Output" },
1119 { "HPOUTR", NULL, "Headphone Output" },
1121 { "LINEOUTL", NULL, "Line Output" },
1122 { "LINEOUTR", NULL, "Line Output" },
1125 static const struct snd_soc_dapm_route wm8904_intercon[] = {
1126 { "Left Sidetone", "Left", "ADCL" },
1127 { "Left Sidetone", "Right", "ADCR" },
1128 { "DACL", NULL, "Left Sidetone" },
1130 { "Right Sidetone", "Left", "ADCL" },
1131 { "Right Sidetone", "Right", "ADCR" },
1132 { "DACR", NULL, "Right Sidetone" },
1134 { "Left Bypass", NULL, "Class G" },
1135 { "Left Bypass", NULL, "Left Capture PGA" },
1137 { "Right Bypass", NULL, "Class G" },
1138 { "Right Bypass", NULL, "Right Capture PGA" },
1140 { "HPL Mux", "DAC", "DACL" },
1141 { "HPL Mux", "Bypass", "Left Bypass" },
1143 { "HPR Mux", "DAC", "DACR" },
1144 { "HPR Mux", "Bypass", "Right Bypass" },
1146 { "LINEL Mux", "DAC", "DACL" },
1147 { "LINEL Mux", "Bypass", "Left Bypass" },
1149 { "LINER Mux", "DAC", "DACR" },
1150 { "LINER Mux", "Bypass", "Right Bypass" },
1152 { "HPL PGA", NULL, "HPL Mux" },
1153 { "HPR PGA", NULL, "HPR Mux" },
1155 { "LINEL PGA", NULL, "LINEL Mux" },
1156 { "LINER PGA", NULL, "LINER Mux" },
1159 static const struct snd_soc_dapm_route wm8912_intercon[] = {
1160 { "HPL PGA", NULL, "DACL" },
1161 { "HPR PGA", NULL, "DACR" },
1163 { "LINEL PGA", NULL, "DACL" },
1164 { "LINER PGA", NULL, "DACR" },
1167 static int wm8904_add_widgets(struct snd_soc_component *component)
1169 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
1170 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1172 snd_soc_dapm_new_controls(dapm, wm8904_core_dapm_widgets,
1173 ARRAY_SIZE(wm8904_core_dapm_widgets));
1174 snd_soc_dapm_add_routes(dapm, core_intercon,
1175 ARRAY_SIZE(core_intercon));
1177 switch (wm8904->devtype) {
1179 snd_soc_add_component_controls(component, wm8904_adc_snd_controls,
1180 ARRAY_SIZE(wm8904_adc_snd_controls));
1181 snd_soc_add_component_controls(component, wm8904_dac_snd_controls,
1182 ARRAY_SIZE(wm8904_dac_snd_controls));
1183 snd_soc_add_component_controls(component, wm8904_snd_controls,
1184 ARRAY_SIZE(wm8904_snd_controls));
1186 snd_soc_dapm_new_controls(dapm, wm8904_adc_dapm_widgets,
1187 ARRAY_SIZE(wm8904_adc_dapm_widgets));
1188 snd_soc_dapm_new_controls(dapm, wm8904_dac_dapm_widgets,
1189 ARRAY_SIZE(wm8904_dac_dapm_widgets));
1190 snd_soc_dapm_new_controls(dapm, wm8904_dapm_widgets,
1191 ARRAY_SIZE(wm8904_dapm_widgets));
1193 snd_soc_dapm_add_routes(dapm, adc_intercon,
1194 ARRAY_SIZE(adc_intercon));
1195 snd_soc_dapm_add_routes(dapm, dac_intercon,
1196 ARRAY_SIZE(dac_intercon));
1197 snd_soc_dapm_add_routes(dapm, wm8904_intercon,
1198 ARRAY_SIZE(wm8904_intercon));
1202 snd_soc_add_component_controls(component, wm8904_dac_snd_controls,
1203 ARRAY_SIZE(wm8904_dac_snd_controls));
1205 snd_soc_dapm_new_controls(dapm, wm8904_dac_dapm_widgets,
1206 ARRAY_SIZE(wm8904_dac_dapm_widgets));
1208 snd_soc_dapm_add_routes(dapm, dac_intercon,
1209 ARRAY_SIZE(dac_intercon));
1210 snd_soc_dapm_add_routes(dapm, wm8912_intercon,
1211 ARRAY_SIZE(wm8912_intercon));
1220 unsigned int clk_sys_rate;
1221 } clk_sys_rates[] = {
1237 } sample_rates[] = {
1250 int div; /* *10 due to .5s */
1276 static int wm8904_hw_params(struct snd_pcm_substream *substream,
1277 struct snd_pcm_hw_params *params,
1278 struct snd_soc_dai *dai)
1280 struct snd_soc_component *component = dai->component;
1281 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
1282 int ret, i, best, best_val, cur_val;
1283 unsigned int aif1 = 0;
1284 unsigned int aif2 = 0;
1285 unsigned int aif3 = 0;
1286 unsigned int clock1 = 0;
1287 unsigned int dac_digital1 = 0;
1289 /* What BCLK do we need? */
1290 wm8904->fs = params_rate(params);
1291 if (wm8904->tdm_slots) {
1292 dev_dbg(component->dev, "Configuring for %d %d bit TDM slots\n",
1293 wm8904->tdm_slots, wm8904->tdm_width);
1294 wm8904->bclk = snd_soc_calc_bclk(wm8904->fs,
1295 wm8904->tdm_width, 2,
1298 wm8904->bclk = snd_soc_params_to_bclk(params);
1301 switch (params_width(params)) {
1318 dev_dbg(component->dev, "Target BCLK is %dHz\n", wm8904->bclk);
1320 ret = wm8904_configure_clocking(component);
1324 /* Select nearest CLK_SYS_RATE */
1326 best_val = abs((wm8904->sysclk_rate / clk_sys_rates[0].ratio)
1328 for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
1329 cur_val = abs((wm8904->sysclk_rate /
1330 clk_sys_rates[i].ratio) - wm8904->fs);
1331 if (cur_val < best_val) {
1336 dev_dbg(component->dev, "Selected CLK_SYS_RATIO of %d\n",
1337 clk_sys_rates[best].ratio);
1338 clock1 |= (clk_sys_rates[best].clk_sys_rate
1339 << WM8904_CLK_SYS_RATE_SHIFT);
1343 best_val = abs(wm8904->fs - sample_rates[0].rate);
1344 for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
1346 cur_val = abs(wm8904->fs - sample_rates[i].rate);
1347 if (cur_val < best_val) {
1352 dev_dbg(component->dev, "Selected SAMPLE_RATE of %dHz\n",
1353 sample_rates[best].rate);
1354 clock1 |= (sample_rates[best].sample_rate
1355 << WM8904_SAMPLE_RATE_SHIFT);
1357 /* Enable sloping stopband filter for low sample rates */
1358 if (wm8904->fs <= 24000)
1359 dac_digital1 |= WM8904_DAC_SB_FILT;
1364 for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
1365 cur_val = ((wm8904->sysclk_rate * 10) / bclk_divs[i].div)
1367 if (cur_val < 0) /* Table is sorted */
1369 if (cur_val < best_val) {
1374 wm8904->bclk = (wm8904->sysclk_rate * 10) / bclk_divs[best].div;
1375 dev_dbg(component->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
1376 bclk_divs[best].div, wm8904->bclk);
1377 aif2 |= bclk_divs[best].bclk_div;
1379 /* LRCLK is a simple fraction of BCLK */
1380 dev_dbg(component->dev, "LRCLK_RATE is %d\n", wm8904->bclk / wm8904->fs);
1381 aif3 |= wm8904->bclk / wm8904->fs;
1383 /* Apply the settings */
1384 snd_soc_component_update_bits(component, WM8904_DAC_DIGITAL_1,
1385 WM8904_DAC_SB_FILT, dac_digital1);
1386 snd_soc_component_update_bits(component, WM8904_AUDIO_INTERFACE_1,
1387 WM8904_AIF_WL_MASK, aif1);
1388 snd_soc_component_update_bits(component, WM8904_AUDIO_INTERFACE_2,
1389 WM8904_BCLK_DIV_MASK, aif2);
1390 snd_soc_component_update_bits(component, WM8904_AUDIO_INTERFACE_3,
1391 WM8904_LRCLK_RATE_MASK, aif3);
1392 snd_soc_component_update_bits(component, WM8904_CLOCK_RATES_1,
1393 WM8904_SAMPLE_RATE_MASK |
1394 WM8904_CLK_SYS_RATE_MASK, clock1);
1396 /* Update filters for the new settings */
1397 wm8904_set_retune_mobile(component);
1398 wm8904_set_deemph(component);
1404 static int wm8904_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1405 unsigned int freq, int dir)
1407 struct snd_soc_component *component = dai->component;
1408 struct wm8904_priv *priv = snd_soc_component_get_drvdata(component);
1411 case WM8904_CLK_MCLK:
1412 priv->sysclk_src = clk_id;
1413 priv->mclk_rate = freq;
1416 case WM8904_CLK_FLL:
1417 priv->sysclk_src = clk_id;
1424 dev_dbg(dai->dev, "Clock source is %d at %uHz\n", clk_id, freq);
1426 wm8904_configure_clocking(component);
1431 static int wm8904_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1433 struct snd_soc_component *component = dai->component;
1434 unsigned int aif1 = 0;
1435 unsigned int aif3 = 0;
1437 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1438 case SND_SOC_DAIFMT_CBS_CFS:
1440 case SND_SOC_DAIFMT_CBS_CFM:
1441 aif3 |= WM8904_LRCLK_DIR;
1443 case SND_SOC_DAIFMT_CBM_CFS:
1444 aif1 |= WM8904_BCLK_DIR;
1446 case SND_SOC_DAIFMT_CBM_CFM:
1447 aif1 |= WM8904_BCLK_DIR;
1448 aif3 |= WM8904_LRCLK_DIR;
1454 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1455 case SND_SOC_DAIFMT_DSP_B:
1456 aif1 |= 0x3 | WM8904_AIF_LRCLK_INV;
1458 case SND_SOC_DAIFMT_DSP_A:
1461 case SND_SOC_DAIFMT_I2S:
1464 case SND_SOC_DAIFMT_RIGHT_J:
1466 case SND_SOC_DAIFMT_LEFT_J:
1473 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1474 case SND_SOC_DAIFMT_DSP_A:
1475 case SND_SOC_DAIFMT_DSP_B:
1476 /* frame inversion not valid for DSP modes */
1477 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1478 case SND_SOC_DAIFMT_NB_NF:
1480 case SND_SOC_DAIFMT_IB_NF:
1481 aif1 |= WM8904_AIF_BCLK_INV;
1488 case SND_SOC_DAIFMT_I2S:
1489 case SND_SOC_DAIFMT_RIGHT_J:
1490 case SND_SOC_DAIFMT_LEFT_J:
1491 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1492 case SND_SOC_DAIFMT_NB_NF:
1494 case SND_SOC_DAIFMT_IB_IF:
1495 aif1 |= WM8904_AIF_BCLK_INV | WM8904_AIF_LRCLK_INV;
1497 case SND_SOC_DAIFMT_IB_NF:
1498 aif1 |= WM8904_AIF_BCLK_INV;
1500 case SND_SOC_DAIFMT_NB_IF:
1501 aif1 |= WM8904_AIF_LRCLK_INV;
1511 snd_soc_component_update_bits(component, WM8904_AUDIO_INTERFACE_1,
1512 WM8904_AIF_BCLK_INV | WM8904_AIF_LRCLK_INV |
1513 WM8904_AIF_FMT_MASK | WM8904_BCLK_DIR, aif1);
1514 snd_soc_component_update_bits(component, WM8904_AUDIO_INTERFACE_3,
1515 WM8904_LRCLK_DIR, aif3);
1521 static int wm8904_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
1522 unsigned int rx_mask, int slots, int slot_width)
1524 struct snd_soc_component *component = dai->component;
1525 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
1528 /* Don't need to validate anything if we're turning off TDM */
1532 /* Note that we allow configurations we can't handle ourselves -
1533 * for example, we can generate clocks for slots 2 and up even if
1534 * we can't use those slots ourselves.
1536 aif1 |= WM8904_AIFADC_TDM | WM8904_AIFDAC_TDM;
1542 aif1 |= WM8904_AIFADC_TDM_CHAN;
1553 aif1 |= WM8904_AIFDAC_TDM_CHAN;
1560 wm8904->tdm_width = slot_width;
1561 wm8904->tdm_slots = slots / 2;
1563 snd_soc_component_update_bits(component, WM8904_AUDIO_INTERFACE_1,
1564 WM8904_AIFADC_TDM | WM8904_AIFADC_TDM_CHAN |
1565 WM8904_AIFDAC_TDM | WM8904_AIFDAC_TDM_CHAN, aif1);
1573 u16 fll_clk_ref_div;
1578 /* The size in bits of the FLL divide multiplied by 10
1579 * to allow rounding later */
1580 #define FIXED_FLL_SIZE ((1 << 16) * 10)
1588 { 0, 64000, 4, 16 },
1589 { 64000, 128000, 3, 8 },
1590 { 128000, 256000, 2, 4 },
1591 { 256000, 1000000, 1, 2 },
1592 { 1000000, 13500000, 0, 1 },
1595 static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
1599 unsigned int K, Ndiv, Nmod, target;
1603 /* Fref must be <=13.5MHz */
1605 fll_div->fll_clk_ref_div = 0;
1606 while ((Fref / div) > 13500000) {
1608 fll_div->fll_clk_ref_div++;
1611 pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
1617 pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
1619 /* Apply the division for our remaining calculations */
1622 /* Fvco should be 90-100MHz; don't check the upper bound */
1624 while (Fout * div < 90000000) {
1627 pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
1632 target = Fout * div;
1633 fll_div->fll_outdiv = div - 1;
1635 pr_debug("Fvco=%dHz\n", target);
1637 /* Find an appropriate FLL_FRATIO and factor it out of the target */
1638 for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
1639 if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
1640 fll_div->fll_fratio = fll_fratios[i].fll_fratio;
1641 target /= fll_fratios[i].ratio;
1645 if (i == ARRAY_SIZE(fll_fratios)) {
1646 pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
1650 /* Now, calculate N.K */
1651 Ndiv = target / Fref;
1654 Nmod = target % Fref;
1655 pr_debug("Nmod=%d\n", Nmod);
1657 /* Calculate fractional part - scale up so we can round. */
1658 Kpart = FIXED_FLL_SIZE * (long long)Nmod;
1660 do_div(Kpart, Fref);
1662 K = Kpart & 0xFFFFFFFF;
1667 /* Move down to proper range now rounding is done */
1668 fll_div->k = K / 10;
1670 pr_debug("N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
1671 fll_div->n, fll_div->k,
1672 fll_div->fll_fratio, fll_div->fll_outdiv,
1673 fll_div->fll_clk_ref_div);
1678 static int wm8904_set_fll(struct snd_soc_dai *dai, int fll_id, int source,
1679 unsigned int Fref, unsigned int Fout)
1681 struct snd_soc_component *component = dai->component;
1682 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
1683 struct _fll_div fll_div;
1688 if (source == wm8904->fll_src && Fref == wm8904->fll_fref &&
1689 Fout == wm8904->fll_fout)
1692 clock2 = snd_soc_component_read32(component, WM8904_CLOCK_RATES_2);
1695 dev_dbg(component->dev, "FLL disabled\n");
1697 wm8904->fll_fref = 0;
1698 wm8904->fll_fout = 0;
1700 /* Gate SYSCLK to avoid glitches */
1701 snd_soc_component_update_bits(component, WM8904_CLOCK_RATES_2,
1702 WM8904_CLK_SYS_ENA, 0);
1704 snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
1705 WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
1710 /* Validate the FLL ID */
1712 case WM8904_FLL_MCLK:
1713 case WM8904_FLL_LRCLK:
1714 case WM8904_FLL_BCLK:
1715 ret = fll_factors(&fll_div, Fref, Fout);
1720 case WM8904_FLL_FREE_RUNNING:
1721 dev_dbg(component->dev, "Using free running FLL\n");
1722 /* Force 12MHz and output/4 for now */
1726 memset(&fll_div, 0, sizeof(fll_div));
1727 fll_div.fll_outdiv = 3;
1731 dev_err(component->dev, "Unknown FLL ID %d\n", fll_id);
1735 /* Save current state then disable the FLL and SYSCLK to avoid
1737 fll1 = snd_soc_component_read32(component, WM8904_FLL_CONTROL_1);
1738 snd_soc_component_update_bits(component, WM8904_CLOCK_RATES_2,
1739 WM8904_CLK_SYS_ENA, 0);
1740 snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
1741 WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
1743 /* Unlock forced oscilator control to switch it on/off */
1744 snd_soc_component_update_bits(component, WM8904_CONTROL_INTERFACE_TEST_1,
1745 WM8904_USER_KEY, WM8904_USER_KEY);
1747 if (fll_id == WM8904_FLL_FREE_RUNNING) {
1748 val = WM8904_FLL_FRC_NCO;
1753 snd_soc_component_update_bits(component, WM8904_FLL_NCO_TEST_1, WM8904_FLL_FRC_NCO,
1755 snd_soc_component_update_bits(component, WM8904_CONTROL_INTERFACE_TEST_1,
1756 WM8904_USER_KEY, 0);
1759 case WM8904_FLL_MCLK:
1760 snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_5,
1761 WM8904_FLL_CLK_REF_SRC_MASK, 0);
1764 case WM8904_FLL_LRCLK:
1765 snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_5,
1766 WM8904_FLL_CLK_REF_SRC_MASK, 1);
1769 case WM8904_FLL_BCLK:
1770 snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_5,
1771 WM8904_FLL_CLK_REF_SRC_MASK, 2);
1776 val = WM8904_FLL_FRACN_ENA;
1779 snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
1780 WM8904_FLL_FRACN_ENA, val);
1782 snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_2,
1783 WM8904_FLL_OUTDIV_MASK | WM8904_FLL_FRATIO_MASK,
1784 (fll_div.fll_outdiv << WM8904_FLL_OUTDIV_SHIFT) |
1785 (fll_div.fll_fratio << WM8904_FLL_FRATIO_SHIFT));
1787 snd_soc_component_write(component, WM8904_FLL_CONTROL_3, fll_div.k);
1789 snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_4, WM8904_FLL_N_MASK,
1790 fll_div.n << WM8904_FLL_N_SHIFT);
1792 snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_5,
1793 WM8904_FLL_CLK_REF_DIV_MASK,
1794 fll_div.fll_clk_ref_div
1795 << WM8904_FLL_CLK_REF_DIV_SHIFT);
1797 dev_dbg(component->dev, "FLL configured for %dHz->%dHz\n", Fref, Fout);
1799 wm8904->fll_fref = Fref;
1800 wm8904->fll_fout = Fout;
1801 wm8904->fll_src = source;
1803 /* Enable the FLL if it was previously active */
1804 snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
1805 WM8904_FLL_OSC_ENA, fll1);
1806 snd_soc_component_update_bits(component, WM8904_FLL_CONTROL_1,
1807 WM8904_FLL_ENA, fll1);
1810 /* Reenable SYSCLK if it was previously active */
1811 snd_soc_component_update_bits(component, WM8904_CLOCK_RATES_2,
1812 WM8904_CLK_SYS_ENA, clock2);
1817 static int wm8904_digital_mute(struct snd_soc_dai *codec_dai, int mute)
1819 struct snd_soc_component *component = codec_dai->component;
1823 val = WM8904_DAC_MUTE;
1827 snd_soc_component_update_bits(component, WM8904_DAC_DIGITAL_1, WM8904_DAC_MUTE, val);
1832 static int wm8904_set_bias_level(struct snd_soc_component *component,
1833 enum snd_soc_bias_level level)
1835 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
1839 case SND_SOC_BIAS_ON:
1842 case SND_SOC_BIAS_PREPARE:
1843 /* VMID resistance 2*50k */
1844 snd_soc_component_update_bits(component, WM8904_VMID_CONTROL_0,
1845 WM8904_VMID_RES_MASK,
1846 0x1 << WM8904_VMID_RES_SHIFT);
1848 /* Normal bias current */
1849 snd_soc_component_update_bits(component, WM8904_BIAS_CONTROL_0,
1850 WM8904_ISEL_MASK, 2 << WM8904_ISEL_SHIFT);
1853 case SND_SOC_BIAS_STANDBY:
1854 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
1855 ret = regulator_bulk_enable(ARRAY_SIZE(wm8904->supplies),
1858 dev_err(component->dev,
1859 "Failed to enable supplies: %d\n",
1864 ret = clk_prepare_enable(wm8904->mclk);
1866 dev_err(component->dev,
1867 "Failed to enable MCLK: %d\n", ret);
1868 regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies),
1873 regcache_cache_only(wm8904->regmap, false);
1874 regcache_sync(wm8904->regmap);
1877 snd_soc_component_update_bits(component, WM8904_BIAS_CONTROL_0,
1878 WM8904_BIAS_ENA, WM8904_BIAS_ENA);
1880 /* Enable VMID, VMID buffering, 2*5k resistance */
1881 snd_soc_component_update_bits(component, WM8904_VMID_CONTROL_0,
1883 WM8904_VMID_RES_MASK,
1885 0x3 << WM8904_VMID_RES_SHIFT);
1891 /* Maintain VMID with 2*250k */
1892 snd_soc_component_update_bits(component, WM8904_VMID_CONTROL_0,
1893 WM8904_VMID_RES_MASK,
1894 0x2 << WM8904_VMID_RES_SHIFT);
1896 /* Bias current *0.5 */
1897 snd_soc_component_update_bits(component, WM8904_BIAS_CONTROL_0,
1898 WM8904_ISEL_MASK, 0);
1901 case SND_SOC_BIAS_OFF:
1903 snd_soc_component_update_bits(component, WM8904_VMID_CONTROL_0,
1904 WM8904_VMID_RES_MASK | WM8904_VMID_ENA, 0);
1906 /* Stop bias generation */
1907 snd_soc_component_update_bits(component, WM8904_BIAS_CONTROL_0,
1908 WM8904_BIAS_ENA, 0);
1910 regcache_cache_only(wm8904->regmap, true);
1911 regcache_mark_dirty(wm8904->regmap);
1913 regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies),
1915 clk_disable_unprepare(wm8904->mclk);
1921 #define WM8904_RATES SNDRV_PCM_RATE_8000_96000
1923 #define WM8904_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1924 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
1926 static const struct snd_soc_dai_ops wm8904_dai_ops = {
1927 .set_sysclk = wm8904_set_sysclk,
1928 .set_fmt = wm8904_set_fmt,
1929 .set_tdm_slot = wm8904_set_tdm_slot,
1930 .set_pll = wm8904_set_fll,
1931 .hw_params = wm8904_hw_params,
1932 .digital_mute = wm8904_digital_mute,
1935 static struct snd_soc_dai_driver wm8904_dai = {
1936 .name = "wm8904-hifi",
1938 .stream_name = "Playback",
1941 .rates = WM8904_RATES,
1942 .formats = WM8904_FORMATS,
1945 .stream_name = "Capture",
1948 .rates = WM8904_RATES,
1949 .formats = WM8904_FORMATS,
1951 .ops = &wm8904_dai_ops,
1952 .symmetric_rates = 1,
1955 static void wm8904_handle_retune_mobile_pdata(struct snd_soc_component *component)
1957 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
1958 struct wm8904_pdata *pdata = wm8904->pdata;
1959 struct snd_kcontrol_new control =
1960 SOC_ENUM_EXT("EQ Mode",
1961 wm8904->retune_mobile_enum,
1962 wm8904_get_retune_mobile_enum,
1963 wm8904_put_retune_mobile_enum);
1967 /* We need an array of texts for the enum API but the number
1968 * of texts is likely to be less than the number of
1969 * configurations due to the sample rate dependency of the
1970 * configurations. */
1971 wm8904->num_retune_mobile_texts = 0;
1972 wm8904->retune_mobile_texts = NULL;
1973 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
1974 for (j = 0; j < wm8904->num_retune_mobile_texts; j++) {
1975 if (strcmp(pdata->retune_mobile_cfgs[i].name,
1976 wm8904->retune_mobile_texts[j]) == 0)
1980 if (j != wm8904->num_retune_mobile_texts)
1983 /* Expand the array... */
1984 t = krealloc(wm8904->retune_mobile_texts,
1986 (wm8904->num_retune_mobile_texts + 1),
1991 /* ...store the new entry... */
1992 t[wm8904->num_retune_mobile_texts] =
1993 pdata->retune_mobile_cfgs[i].name;
1995 /* ...and remember the new version. */
1996 wm8904->num_retune_mobile_texts++;
1997 wm8904->retune_mobile_texts = t;
2000 dev_dbg(component->dev, "Allocated %d unique ReTune Mobile names\n",
2001 wm8904->num_retune_mobile_texts);
2003 wm8904->retune_mobile_enum.items = wm8904->num_retune_mobile_texts;
2004 wm8904->retune_mobile_enum.texts = wm8904->retune_mobile_texts;
2006 ret = snd_soc_add_component_controls(component, &control, 1);
2008 dev_err(component->dev,
2009 "Failed to add ReTune Mobile control: %d\n", ret);
2012 static void wm8904_handle_pdata(struct snd_soc_component *component)
2014 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
2015 struct wm8904_pdata *pdata = wm8904->pdata;
2019 snd_soc_add_component_controls(component, wm8904_eq_controls,
2020 ARRAY_SIZE(wm8904_eq_controls));
2024 dev_dbg(component->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
2026 if (pdata->num_drc_cfgs) {
2027 struct snd_kcontrol_new control =
2028 SOC_ENUM_EXT("DRC Mode", wm8904->drc_enum,
2029 wm8904_get_drc_enum, wm8904_put_drc_enum);
2031 /* We need an array of texts for the enum API */
2032 wm8904->drc_texts = kmalloc_array(pdata->num_drc_cfgs,
2035 if (!wm8904->drc_texts)
2038 for (i = 0; i < pdata->num_drc_cfgs; i++)
2039 wm8904->drc_texts[i] = pdata->drc_cfgs[i].name;
2041 wm8904->drc_enum.items = pdata->num_drc_cfgs;
2042 wm8904->drc_enum.texts = wm8904->drc_texts;
2044 ret = snd_soc_add_component_controls(component, &control, 1);
2046 dev_err(component->dev,
2047 "Failed to add DRC mode control: %d\n", ret);
2049 wm8904_set_drc(component);
2052 dev_dbg(component->dev, "%d ReTune Mobile configurations\n",
2053 pdata->num_retune_mobile_cfgs);
2055 if (pdata->num_retune_mobile_cfgs)
2056 wm8904_handle_retune_mobile_pdata(component);
2058 snd_soc_add_component_controls(component, wm8904_eq_controls,
2059 ARRAY_SIZE(wm8904_eq_controls));
2063 static int wm8904_probe(struct snd_soc_component *component)
2065 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
2067 switch (wm8904->devtype) {
2071 memset(&wm8904_dai.capture, 0, sizeof(wm8904_dai.capture));
2074 dev_err(component->dev, "Unknown device type %d\n",
2079 wm8904_handle_pdata(component);
2081 wm8904_add_widgets(component);
2086 static void wm8904_remove(struct snd_soc_component *component)
2088 struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
2090 kfree(wm8904->retune_mobile_texts);
2091 kfree(wm8904->drc_texts);
2094 static const struct snd_soc_component_driver soc_component_dev_wm8904 = {
2095 .probe = wm8904_probe,
2096 .remove = wm8904_remove,
2097 .set_bias_level = wm8904_set_bias_level,
2098 .use_pmdown_time = 1,
2100 .non_legacy_dai_naming = 1,
2103 static const struct regmap_config wm8904_regmap = {
2107 .max_register = WM8904_MAX_REGISTER,
2108 .volatile_reg = wm8904_volatile_register,
2109 .readable_reg = wm8904_readable_register,
2111 .cache_type = REGCACHE_RBTREE,
2112 .reg_defaults = wm8904_reg_defaults,
2113 .num_reg_defaults = ARRAY_SIZE(wm8904_reg_defaults),
2117 static const struct of_device_id wm8904_of_match[] = {
2119 .compatible = "wlf,wm8904",
2120 .data = (void *)WM8904,
2122 .compatible = "wlf,wm8912",
2123 .data = (void *)WM8912,
2128 MODULE_DEVICE_TABLE(of, wm8904_of_match);
2131 static int wm8904_i2c_probe(struct i2c_client *i2c,
2132 const struct i2c_device_id *id)
2134 struct wm8904_priv *wm8904;
2138 wm8904 = devm_kzalloc(&i2c->dev, sizeof(struct wm8904_priv),
2143 wm8904->mclk = devm_clk_get(&i2c->dev, "mclk");
2144 if (IS_ERR(wm8904->mclk)) {
2145 ret = PTR_ERR(wm8904->mclk);
2146 dev_err(&i2c->dev, "Failed to get MCLK\n");
2150 wm8904->regmap = devm_regmap_init_i2c(i2c, &wm8904_regmap);
2151 if (IS_ERR(wm8904->regmap)) {
2152 ret = PTR_ERR(wm8904->regmap);
2153 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
2158 if (i2c->dev.of_node) {
2159 const struct of_device_id *match;
2161 match = of_match_node(wm8904_of_match, i2c->dev.of_node);
2164 wm8904->devtype = (enum wm8904_type)match->data;
2166 wm8904->devtype = id->driver_data;
2169 i2c_set_clientdata(i2c, wm8904);
2170 wm8904->pdata = i2c->dev.platform_data;
2172 for (i = 0; i < ARRAY_SIZE(wm8904->supplies); i++)
2173 wm8904->supplies[i].supply = wm8904_supply_names[i];
2175 ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8904->supplies),
2178 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
2182 ret = regulator_bulk_enable(ARRAY_SIZE(wm8904->supplies),
2185 dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
2189 ret = regmap_read(wm8904->regmap, WM8904_SW_RESET_AND_ID, &val);
2191 dev_err(&i2c->dev, "Failed to read ID register: %d\n", ret);
2194 if (val != 0x8904) {
2195 dev_err(&i2c->dev, "Device is not a WM8904, ID is %x\n", val);
2200 ret = regmap_read(wm8904->regmap, WM8904_REVISION, &val);
2202 dev_err(&i2c->dev, "Failed to read device revision: %d\n",
2206 dev_info(&i2c->dev, "revision %c\n", val + 'A');
2208 ret = regmap_write(wm8904->regmap, WM8904_SW_RESET_AND_ID, 0);
2210 dev_err(&i2c->dev, "Failed to issue reset: %d\n", ret);
2214 /* Change some default settings - latch VU and enable ZC */
2215 regmap_update_bits(wm8904->regmap, WM8904_ADC_DIGITAL_VOLUME_LEFT,
2216 WM8904_ADC_VU, WM8904_ADC_VU);
2217 regmap_update_bits(wm8904->regmap, WM8904_ADC_DIGITAL_VOLUME_RIGHT,
2218 WM8904_ADC_VU, WM8904_ADC_VU);
2219 regmap_update_bits(wm8904->regmap, WM8904_DAC_DIGITAL_VOLUME_LEFT,
2220 WM8904_DAC_VU, WM8904_DAC_VU);
2221 regmap_update_bits(wm8904->regmap, WM8904_DAC_DIGITAL_VOLUME_RIGHT,
2222 WM8904_DAC_VU, WM8904_DAC_VU);
2223 regmap_update_bits(wm8904->regmap, WM8904_ANALOGUE_OUT1_LEFT,
2224 WM8904_HPOUT_VU | WM8904_HPOUTLZC,
2225 WM8904_HPOUT_VU | WM8904_HPOUTLZC);
2226 regmap_update_bits(wm8904->regmap, WM8904_ANALOGUE_OUT1_RIGHT,
2227 WM8904_HPOUT_VU | WM8904_HPOUTRZC,
2228 WM8904_HPOUT_VU | WM8904_HPOUTRZC);
2229 regmap_update_bits(wm8904->regmap, WM8904_ANALOGUE_OUT2_LEFT,
2230 WM8904_LINEOUT_VU | WM8904_LINEOUTLZC,
2231 WM8904_LINEOUT_VU | WM8904_LINEOUTLZC);
2232 regmap_update_bits(wm8904->regmap, WM8904_ANALOGUE_OUT2_RIGHT,
2233 WM8904_LINEOUT_VU | WM8904_LINEOUTRZC,
2234 WM8904_LINEOUT_VU | WM8904_LINEOUTRZC);
2235 regmap_update_bits(wm8904->regmap, WM8904_CLOCK_RATES_0,
2238 /* Apply configuration from the platform data. */
2239 if (wm8904->pdata) {
2240 for (i = 0; i < WM8904_GPIO_REGS; i++) {
2241 if (!wm8904->pdata->gpio_cfg[i])
2244 regmap_update_bits(wm8904->regmap,
2245 WM8904_GPIO_CONTROL_1 + i,
2247 wm8904->pdata->gpio_cfg[i]);
2250 /* Zero is the default value for these anyway */
2251 for (i = 0; i < WM8904_MIC_REGS; i++)
2252 regmap_update_bits(wm8904->regmap,
2253 WM8904_MIC_BIAS_CONTROL_0 + i,
2255 wm8904->pdata->mic_cfg[i]);
2258 /* Set Class W by default - this will be managed by the Class
2259 * G widget at runtime where bypass paths are available.
2261 regmap_update_bits(wm8904->regmap, WM8904_CLASS_W_0,
2262 WM8904_CP_DYN_PWR, WM8904_CP_DYN_PWR);
2264 /* Use normal bias source */
2265 regmap_update_bits(wm8904->regmap, WM8904_BIAS_CONTROL_0,
2268 /* Can leave the device powered off until we need it */
2269 regcache_cache_only(wm8904->regmap, true);
2270 regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
2272 ret = devm_snd_soc_register_component(&i2c->dev,
2273 &soc_component_dev_wm8904, &wm8904_dai, 1);
2280 regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
2284 static const struct i2c_device_id wm8904_i2c_id[] = {
2285 { "wm8904", WM8904 },
2286 { "wm8912", WM8912 },
2287 { "wm8918", WM8904 }, /* Actually a subset, updates to follow */
2290 MODULE_DEVICE_TABLE(i2c, wm8904_i2c_id);
2292 static struct i2c_driver wm8904_i2c_driver = {
2295 .of_match_table = of_match_ptr(wm8904_of_match),
2297 .probe = wm8904_i2c_probe,
2298 .id_table = wm8904_i2c_id,
2301 module_i2c_driver(wm8904_i2c_driver);
2303 MODULE_DESCRIPTION("ASoC WM8904 driver");
2304 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
2305 MODULE_LICENSE("GPL");