ASoC: wm8994: Use audio mode for jack detection when system is active
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / soc / codecs / wm8994.c
blobbc12d097ef0dbbc5b383d04970eaba80792e1bae
1 /*
2 * wm8994.c -- WM8994 ALSA SoC Audio driver
4 * Copyright 2009 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/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/pm.h>
19 #include <linux/i2c.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/slab.h>
24 #include <sound/core.h>
25 #include <sound/jack.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/initval.h>
30 #include <sound/tlv.h>
31 #include <trace/events/asoc.h>
33 #include <linux/mfd/wm8994/core.h>
34 #include <linux/mfd/wm8994/registers.h>
35 #include <linux/mfd/wm8994/pdata.h>
36 #include <linux/mfd/wm8994/gpio.h>
38 #include "wm8994.h"
39 #include "wm_hubs.h"
41 #define WM1811_JACKDET_MODE_NONE 0x0000
42 #define WM1811_JACKDET_MODE_JACK 0x0100
43 #define WM1811_JACKDET_MODE_MIC 0x0080
44 #define WM1811_JACKDET_MODE_AUDIO 0x0180
46 #define WM8994_NUM_DRC 3
47 #define WM8994_NUM_EQ 3
49 static int wm8994_drc_base[] = {
50 WM8994_AIF1_DRC1_1,
51 WM8994_AIF1_DRC2_1,
52 WM8994_AIF2_DRC_1,
55 static int wm8994_retune_mobile_base[] = {
56 WM8994_AIF1_DAC1_EQ_GAINS_1,
57 WM8994_AIF1_DAC2_EQ_GAINS_1,
58 WM8994_AIF2_EQ_GAINS_1,
61 static void wm8958_default_micdet(u16 status, void *data);
63 static const struct wm8958_micd_rate micdet_rates[] = {
64 { 32768, true, 1, 4 },
65 { 32768, false, 1, 1 },
66 { 44100 * 256, true, 7, 10 },
67 { 44100 * 256, false, 7, 10 },
70 static const struct wm8958_micd_rate jackdet_rates[] = {
71 { 32768, true, 0, 1 },
72 { 32768, false, 0, 1 },
73 { 44100 * 256, true, 7, 10 },
74 { 44100 * 256, false, 7, 10 },
77 static void wm8958_micd_set_rate(struct snd_soc_codec *codec)
79 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
80 int best, i, sysclk, val;
81 bool idle;
82 const struct wm8958_micd_rate *rates;
83 int num_rates;
85 if (wm8994->jack_cb != wm8958_default_micdet)
86 return;
88 idle = !wm8994->jack_mic;
90 sysclk = snd_soc_read(codec, WM8994_CLOCKING_1);
91 if (sysclk & WM8994_SYSCLK_SRC)
92 sysclk = wm8994->aifclk[1];
93 else
94 sysclk = wm8994->aifclk[0];
96 if (wm8994->pdata && wm8994->pdata->micd_rates) {
97 rates = wm8994->pdata->micd_rates;
98 num_rates = wm8994->pdata->num_micd_rates;
99 } else if (wm8994->jackdet) {
100 rates = jackdet_rates;
101 num_rates = ARRAY_SIZE(jackdet_rates);
102 } else {
103 rates = micdet_rates;
104 num_rates = ARRAY_SIZE(micdet_rates);
107 best = 0;
108 for (i = 0; i < num_rates; i++) {
109 if (rates[i].idle != idle)
110 continue;
111 if (abs(rates[i].sysclk - sysclk) <
112 abs(rates[best].sysclk - sysclk))
113 best = i;
114 else if (rates[best].idle != idle)
115 best = i;
118 val = rates[best].start << WM8958_MICD_BIAS_STARTTIME_SHIFT
119 | rates[best].rate << WM8958_MICD_RATE_SHIFT;
121 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
122 WM8958_MICD_BIAS_STARTTIME_MASK |
123 WM8958_MICD_RATE_MASK, val);
126 static int configure_aif_clock(struct snd_soc_codec *codec, int aif)
128 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
129 int rate;
130 int reg1 = 0;
131 int offset;
133 if (aif)
134 offset = 4;
135 else
136 offset = 0;
138 switch (wm8994->sysclk[aif]) {
139 case WM8994_SYSCLK_MCLK1:
140 rate = wm8994->mclk[0];
141 break;
143 case WM8994_SYSCLK_MCLK2:
144 reg1 |= 0x8;
145 rate = wm8994->mclk[1];
146 break;
148 case WM8994_SYSCLK_FLL1:
149 reg1 |= 0x10;
150 rate = wm8994->fll[0].out;
151 break;
153 case WM8994_SYSCLK_FLL2:
154 reg1 |= 0x18;
155 rate = wm8994->fll[1].out;
156 break;
158 default:
159 return -EINVAL;
162 if (rate >= 13500000) {
163 rate /= 2;
164 reg1 |= WM8994_AIF1CLK_DIV;
166 dev_dbg(codec->dev, "Dividing AIF%d clock to %dHz\n",
167 aif + 1, rate);
170 wm8994->aifclk[aif] = rate;
172 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1 + offset,
173 WM8994_AIF1CLK_SRC_MASK | WM8994_AIF1CLK_DIV,
174 reg1);
176 return 0;
179 static int configure_clock(struct snd_soc_codec *codec)
181 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
182 int change, new;
184 /* Bring up the AIF clocks first */
185 configure_aif_clock(codec, 0);
186 configure_aif_clock(codec, 1);
188 /* Then switch CLK_SYS over to the higher of them; a change
189 * can only happen as a result of a clocking change which can
190 * only be made outside of DAPM so we can safely redo the
191 * clocking.
194 /* If they're equal it doesn't matter which is used */
195 if (wm8994->aifclk[0] == wm8994->aifclk[1]) {
196 wm8958_micd_set_rate(codec);
197 return 0;
200 if (wm8994->aifclk[0] < wm8994->aifclk[1])
201 new = WM8994_SYSCLK_SRC;
202 else
203 new = 0;
205 change = snd_soc_update_bits(codec, WM8994_CLOCKING_1,
206 WM8994_SYSCLK_SRC, new);
207 if (change)
208 snd_soc_dapm_sync(&codec->dapm);
210 wm8958_micd_set_rate(codec);
212 return 0;
215 static int check_clk_sys(struct snd_soc_dapm_widget *source,
216 struct snd_soc_dapm_widget *sink)
218 int reg = snd_soc_read(source->codec, WM8994_CLOCKING_1);
219 const char *clk;
221 /* Check what we're currently using for CLK_SYS */
222 if (reg & WM8994_SYSCLK_SRC)
223 clk = "AIF2CLK";
224 else
225 clk = "AIF1CLK";
227 return strcmp(source->name, clk) == 0;
230 static const char *sidetone_hpf_text[] = {
231 "2.7kHz", "1.35kHz", "675Hz", "370Hz", "180Hz", "90Hz", "45Hz"
234 static const struct soc_enum sidetone_hpf =
235 SOC_ENUM_SINGLE(WM8994_SIDETONE, 7, 7, sidetone_hpf_text);
237 static const char *adc_hpf_text[] = {
238 "HiFi", "Voice 1", "Voice 2", "Voice 3"
241 static const struct soc_enum aif1adc1_hpf =
242 SOC_ENUM_SINGLE(WM8994_AIF1_ADC1_FILTERS, 13, 4, adc_hpf_text);
244 static const struct soc_enum aif1adc2_hpf =
245 SOC_ENUM_SINGLE(WM8994_AIF1_ADC2_FILTERS, 13, 4, adc_hpf_text);
247 static const struct soc_enum aif2adc_hpf =
248 SOC_ENUM_SINGLE(WM8994_AIF2_ADC_FILTERS, 13, 4, adc_hpf_text);
250 static const DECLARE_TLV_DB_SCALE(aif_tlv, 0, 600, 0);
251 static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
252 static const DECLARE_TLV_DB_SCALE(st_tlv, -3600, 300, 0);
253 static const DECLARE_TLV_DB_SCALE(wm8994_3d_tlv, -1600, 183, 0);
254 static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
255 static const DECLARE_TLV_DB_SCALE(ng_tlv, -10200, 600, 0);
256 static const DECLARE_TLV_DB_SCALE(mixin_boost_tlv, 0, 900, 0);
258 #define WM8994_DRC_SWITCH(xname, reg, shift) \
259 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
260 .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,\
261 .put = wm8994_put_drc_sw, \
262 .private_value = SOC_SINGLE_VALUE(reg, shift, 1, 0) }
264 static int wm8994_put_drc_sw(struct snd_kcontrol *kcontrol,
265 struct snd_ctl_elem_value *ucontrol)
267 struct soc_mixer_control *mc =
268 (struct soc_mixer_control *)kcontrol->private_value;
269 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
270 int mask, ret;
272 /* Can't enable both ADC and DAC paths simultaneously */
273 if (mc->shift == WM8994_AIF1DAC1_DRC_ENA_SHIFT)
274 mask = WM8994_AIF1ADC1L_DRC_ENA_MASK |
275 WM8994_AIF1ADC1R_DRC_ENA_MASK;
276 else
277 mask = WM8994_AIF1DAC1_DRC_ENA_MASK;
279 ret = snd_soc_read(codec, mc->reg);
280 if (ret < 0)
281 return ret;
282 if (ret & mask)
283 return -EINVAL;
285 return snd_soc_put_volsw(kcontrol, ucontrol);
288 static void wm8994_set_drc(struct snd_soc_codec *codec, int drc)
290 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
291 struct wm8994_pdata *pdata = wm8994->pdata;
292 int base = wm8994_drc_base[drc];
293 int cfg = wm8994->drc_cfg[drc];
294 int save, i;
296 /* Save any enables; the configuration should clear them. */
297 save = snd_soc_read(codec, base);
298 save &= WM8994_AIF1DAC1_DRC_ENA | WM8994_AIF1ADC1L_DRC_ENA |
299 WM8994_AIF1ADC1R_DRC_ENA;
301 for (i = 0; i < WM8994_DRC_REGS; i++)
302 snd_soc_update_bits(codec, base + i, 0xffff,
303 pdata->drc_cfgs[cfg].regs[i]);
305 snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_DRC_ENA |
306 WM8994_AIF1ADC1L_DRC_ENA |
307 WM8994_AIF1ADC1R_DRC_ENA, save);
310 /* Icky as hell but saves code duplication */
311 static int wm8994_get_drc(const char *name)
313 if (strcmp(name, "AIF1DRC1 Mode") == 0)
314 return 0;
315 if (strcmp(name, "AIF1DRC2 Mode") == 0)
316 return 1;
317 if (strcmp(name, "AIF2DRC Mode") == 0)
318 return 2;
319 return -EINVAL;
322 static int wm8994_put_drc_enum(struct snd_kcontrol *kcontrol,
323 struct snd_ctl_elem_value *ucontrol)
325 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
326 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
327 struct wm8994_pdata *pdata = wm8994->pdata;
328 int drc = wm8994_get_drc(kcontrol->id.name);
329 int value = ucontrol->value.integer.value[0];
331 if (drc < 0)
332 return drc;
334 if (value >= pdata->num_drc_cfgs)
335 return -EINVAL;
337 wm8994->drc_cfg[drc] = value;
339 wm8994_set_drc(codec, drc);
341 return 0;
344 static int wm8994_get_drc_enum(struct snd_kcontrol *kcontrol,
345 struct snd_ctl_elem_value *ucontrol)
347 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
348 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
349 int drc = wm8994_get_drc(kcontrol->id.name);
351 ucontrol->value.enumerated.item[0] = wm8994->drc_cfg[drc];
353 return 0;
356 static void wm8994_set_retune_mobile(struct snd_soc_codec *codec, int block)
358 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
359 struct wm8994_pdata *pdata = wm8994->pdata;
360 int base = wm8994_retune_mobile_base[block];
361 int iface, best, best_val, save, i, cfg;
363 if (!pdata || !wm8994->num_retune_mobile_texts)
364 return;
366 switch (block) {
367 case 0:
368 case 1:
369 iface = 0;
370 break;
371 case 2:
372 iface = 1;
373 break;
374 default:
375 return;
378 /* Find the version of the currently selected configuration
379 * with the nearest sample rate. */
380 cfg = wm8994->retune_mobile_cfg[block];
381 best = 0;
382 best_val = INT_MAX;
383 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
384 if (strcmp(pdata->retune_mobile_cfgs[i].name,
385 wm8994->retune_mobile_texts[cfg]) == 0 &&
386 abs(pdata->retune_mobile_cfgs[i].rate
387 - wm8994->dac_rates[iface]) < best_val) {
388 best = i;
389 best_val = abs(pdata->retune_mobile_cfgs[i].rate
390 - wm8994->dac_rates[iface]);
394 dev_dbg(codec->dev, "ReTune Mobile %d %s/%dHz for %dHz sample rate\n",
395 block,
396 pdata->retune_mobile_cfgs[best].name,
397 pdata->retune_mobile_cfgs[best].rate,
398 wm8994->dac_rates[iface]);
400 /* The EQ will be disabled while reconfiguring it, remember the
401 * current configuration.
403 save = snd_soc_read(codec, base);
404 save &= WM8994_AIF1DAC1_EQ_ENA;
406 for (i = 0; i < WM8994_EQ_REGS; i++)
407 snd_soc_update_bits(codec, base + i, 0xffff,
408 pdata->retune_mobile_cfgs[best].regs[i]);
410 snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_EQ_ENA, save);
413 /* Icky as hell but saves code duplication */
414 static int wm8994_get_retune_mobile_block(const char *name)
416 if (strcmp(name, "AIF1.1 EQ Mode") == 0)
417 return 0;
418 if (strcmp(name, "AIF1.2 EQ Mode") == 0)
419 return 1;
420 if (strcmp(name, "AIF2 EQ Mode") == 0)
421 return 2;
422 return -EINVAL;
425 static int wm8994_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
426 struct snd_ctl_elem_value *ucontrol)
428 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
429 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
430 struct wm8994_pdata *pdata = wm8994->pdata;
431 int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
432 int value = ucontrol->value.integer.value[0];
434 if (block < 0)
435 return block;
437 if (value >= pdata->num_retune_mobile_cfgs)
438 return -EINVAL;
440 wm8994->retune_mobile_cfg[block] = value;
442 wm8994_set_retune_mobile(codec, block);
444 return 0;
447 static int wm8994_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
448 struct snd_ctl_elem_value *ucontrol)
450 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
451 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
452 int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
454 ucontrol->value.enumerated.item[0] = wm8994->retune_mobile_cfg[block];
456 return 0;
459 static const char *aif_chan_src_text[] = {
460 "Left", "Right"
463 static const struct soc_enum aif1adcl_src =
464 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_1, 15, 2, aif_chan_src_text);
466 static const struct soc_enum aif1adcr_src =
467 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_1, 14, 2, aif_chan_src_text);
469 static const struct soc_enum aif2adcl_src =
470 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_1, 15, 2, aif_chan_src_text);
472 static const struct soc_enum aif2adcr_src =
473 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_1, 14, 2, aif_chan_src_text);
475 static const struct soc_enum aif1dacl_src =
476 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_2, 15, 2, aif_chan_src_text);
478 static const struct soc_enum aif1dacr_src =
479 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_2, 14, 2, aif_chan_src_text);
481 static const struct soc_enum aif2dacl_src =
482 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_2, 15, 2, aif_chan_src_text);
484 static const struct soc_enum aif2dacr_src =
485 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_2, 14, 2, aif_chan_src_text);
487 static const char *osr_text[] = {
488 "Low Power", "High Performance",
491 static const struct soc_enum dac_osr =
492 SOC_ENUM_SINGLE(WM8994_OVERSAMPLING, 0, 2, osr_text);
494 static const struct soc_enum adc_osr =
495 SOC_ENUM_SINGLE(WM8994_OVERSAMPLING, 1, 2, osr_text);
497 static const struct snd_kcontrol_new wm8994_snd_controls[] = {
498 SOC_DOUBLE_R_TLV("AIF1ADC1 Volume", WM8994_AIF1_ADC1_LEFT_VOLUME,
499 WM8994_AIF1_ADC1_RIGHT_VOLUME,
500 1, 119, 0, digital_tlv),
501 SOC_DOUBLE_R_TLV("AIF1ADC2 Volume", WM8994_AIF1_ADC2_LEFT_VOLUME,
502 WM8994_AIF1_ADC2_RIGHT_VOLUME,
503 1, 119, 0, digital_tlv),
504 SOC_DOUBLE_R_TLV("AIF2ADC Volume", WM8994_AIF2_ADC_LEFT_VOLUME,
505 WM8994_AIF2_ADC_RIGHT_VOLUME,
506 1, 119, 0, digital_tlv),
508 SOC_ENUM("AIF1ADCL Source", aif1adcl_src),
509 SOC_ENUM("AIF1ADCR Source", aif1adcr_src),
510 SOC_ENUM("AIF2ADCL Source", aif2adcl_src),
511 SOC_ENUM("AIF2ADCR Source", aif2adcr_src),
513 SOC_ENUM("AIF1DACL Source", aif1dacl_src),
514 SOC_ENUM("AIF1DACR Source", aif1dacr_src),
515 SOC_ENUM("AIF2DACL Source", aif2dacl_src),
516 SOC_ENUM("AIF2DACR Source", aif2dacr_src),
518 SOC_DOUBLE_R_TLV("AIF1DAC1 Volume", WM8994_AIF1_DAC1_LEFT_VOLUME,
519 WM8994_AIF1_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
520 SOC_DOUBLE_R_TLV("AIF1DAC2 Volume", WM8994_AIF1_DAC2_LEFT_VOLUME,
521 WM8994_AIF1_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
522 SOC_DOUBLE_R_TLV("AIF2DAC Volume", WM8994_AIF2_DAC_LEFT_VOLUME,
523 WM8994_AIF2_DAC_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
525 SOC_SINGLE_TLV("AIF1 Boost Volume", WM8994_AIF1_CONTROL_2, 10, 3, 0, aif_tlv),
526 SOC_SINGLE_TLV("AIF2 Boost Volume", WM8994_AIF2_CONTROL_2, 10, 3, 0, aif_tlv),
528 SOC_SINGLE("AIF1DAC1 EQ Switch", WM8994_AIF1_DAC1_EQ_GAINS_1, 0, 1, 0),
529 SOC_SINGLE("AIF1DAC2 EQ Switch", WM8994_AIF1_DAC2_EQ_GAINS_1, 0, 1, 0),
530 SOC_SINGLE("AIF2 EQ Switch", WM8994_AIF2_EQ_GAINS_1, 0, 1, 0),
532 WM8994_DRC_SWITCH("AIF1DAC1 DRC Switch", WM8994_AIF1_DRC1_1, 2),
533 WM8994_DRC_SWITCH("AIF1ADC1L DRC Switch", WM8994_AIF1_DRC1_1, 1),
534 WM8994_DRC_SWITCH("AIF1ADC1R DRC Switch", WM8994_AIF1_DRC1_1, 0),
536 WM8994_DRC_SWITCH("AIF1DAC2 DRC Switch", WM8994_AIF1_DRC2_1, 2),
537 WM8994_DRC_SWITCH("AIF1ADC2L DRC Switch", WM8994_AIF1_DRC2_1, 1),
538 WM8994_DRC_SWITCH("AIF1ADC2R DRC Switch", WM8994_AIF1_DRC2_1, 0),
540 WM8994_DRC_SWITCH("AIF2DAC DRC Switch", WM8994_AIF2_DRC_1, 2),
541 WM8994_DRC_SWITCH("AIF2ADCL DRC Switch", WM8994_AIF2_DRC_1, 1),
542 WM8994_DRC_SWITCH("AIF2ADCR DRC Switch", WM8994_AIF2_DRC_1, 0),
544 SOC_SINGLE_TLV("DAC1 Right Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
545 5, 12, 0, st_tlv),
546 SOC_SINGLE_TLV("DAC1 Left Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
547 0, 12, 0, st_tlv),
548 SOC_SINGLE_TLV("DAC2 Right Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
549 5, 12, 0, st_tlv),
550 SOC_SINGLE_TLV("DAC2 Left Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
551 0, 12, 0, st_tlv),
552 SOC_ENUM("Sidetone HPF Mux", sidetone_hpf),
553 SOC_SINGLE("Sidetone HPF Switch", WM8994_SIDETONE, 6, 1, 0),
555 SOC_ENUM("AIF1ADC1 HPF Mode", aif1adc1_hpf),
556 SOC_DOUBLE("AIF1ADC1 HPF Switch", WM8994_AIF1_ADC1_FILTERS, 12, 11, 1, 0),
558 SOC_ENUM("AIF1ADC2 HPF Mode", aif1adc2_hpf),
559 SOC_DOUBLE("AIF1ADC2 HPF Switch", WM8994_AIF1_ADC2_FILTERS, 12, 11, 1, 0),
561 SOC_ENUM("AIF2ADC HPF Mode", aif2adc_hpf),
562 SOC_DOUBLE("AIF2ADC HPF Switch", WM8994_AIF2_ADC_FILTERS, 12, 11, 1, 0),
564 SOC_ENUM("ADC OSR", adc_osr),
565 SOC_ENUM("DAC OSR", dac_osr),
567 SOC_DOUBLE_R_TLV("DAC1 Volume", WM8994_DAC1_LEFT_VOLUME,
568 WM8994_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
569 SOC_DOUBLE_R("DAC1 Switch", WM8994_DAC1_LEFT_VOLUME,
570 WM8994_DAC1_RIGHT_VOLUME, 9, 1, 1),
572 SOC_DOUBLE_R_TLV("DAC2 Volume", WM8994_DAC2_LEFT_VOLUME,
573 WM8994_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
574 SOC_DOUBLE_R("DAC2 Switch", WM8994_DAC2_LEFT_VOLUME,
575 WM8994_DAC2_RIGHT_VOLUME, 9, 1, 1),
577 SOC_SINGLE_TLV("SPKL DAC2 Volume", WM8994_SPKMIXL_ATTENUATION,
578 6, 1, 1, wm_hubs_spkmix_tlv),
579 SOC_SINGLE_TLV("SPKL DAC1 Volume", WM8994_SPKMIXL_ATTENUATION,
580 2, 1, 1, wm_hubs_spkmix_tlv),
582 SOC_SINGLE_TLV("SPKR DAC2 Volume", WM8994_SPKMIXR_ATTENUATION,
583 6, 1, 1, wm_hubs_spkmix_tlv),
584 SOC_SINGLE_TLV("SPKR DAC1 Volume", WM8994_SPKMIXR_ATTENUATION,
585 2, 1, 1, wm_hubs_spkmix_tlv),
587 SOC_SINGLE_TLV("AIF1DAC1 3D Stereo Volume", WM8994_AIF1_DAC1_FILTERS_2,
588 10, 15, 0, wm8994_3d_tlv),
589 SOC_SINGLE("AIF1DAC1 3D Stereo Switch", WM8994_AIF1_DAC1_FILTERS_2,
590 8, 1, 0),
591 SOC_SINGLE_TLV("AIF1DAC2 3D Stereo Volume", WM8994_AIF1_DAC2_FILTERS_2,
592 10, 15, 0, wm8994_3d_tlv),
593 SOC_SINGLE("AIF1DAC2 3D Stereo Switch", WM8994_AIF1_DAC2_FILTERS_2,
594 8, 1, 0),
595 SOC_SINGLE_TLV("AIF2DAC 3D Stereo Volume", WM8994_AIF2_DAC_FILTERS_2,
596 10, 15, 0, wm8994_3d_tlv),
597 SOC_SINGLE("AIF2DAC 3D Stereo Switch", WM8994_AIF2_DAC_FILTERS_2,
598 8, 1, 0),
601 static const struct snd_kcontrol_new wm8994_eq_controls[] = {
602 SOC_SINGLE_TLV("AIF1DAC1 EQ1 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 11, 31, 0,
603 eq_tlv),
604 SOC_SINGLE_TLV("AIF1DAC1 EQ2 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 6, 31, 0,
605 eq_tlv),
606 SOC_SINGLE_TLV("AIF1DAC1 EQ3 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 1, 31, 0,
607 eq_tlv),
608 SOC_SINGLE_TLV("AIF1DAC1 EQ4 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 11, 31, 0,
609 eq_tlv),
610 SOC_SINGLE_TLV("AIF1DAC1 EQ5 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 6, 31, 0,
611 eq_tlv),
613 SOC_SINGLE_TLV("AIF1DAC2 EQ1 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 11, 31, 0,
614 eq_tlv),
615 SOC_SINGLE_TLV("AIF1DAC2 EQ2 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 6, 31, 0,
616 eq_tlv),
617 SOC_SINGLE_TLV("AIF1DAC2 EQ3 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 1, 31, 0,
618 eq_tlv),
619 SOC_SINGLE_TLV("AIF1DAC2 EQ4 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 11, 31, 0,
620 eq_tlv),
621 SOC_SINGLE_TLV("AIF1DAC2 EQ5 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 6, 31, 0,
622 eq_tlv),
624 SOC_SINGLE_TLV("AIF2 EQ1 Volume", WM8994_AIF2_EQ_GAINS_1, 11, 31, 0,
625 eq_tlv),
626 SOC_SINGLE_TLV("AIF2 EQ2 Volume", WM8994_AIF2_EQ_GAINS_1, 6, 31, 0,
627 eq_tlv),
628 SOC_SINGLE_TLV("AIF2 EQ3 Volume", WM8994_AIF2_EQ_GAINS_1, 1, 31, 0,
629 eq_tlv),
630 SOC_SINGLE_TLV("AIF2 EQ4 Volume", WM8994_AIF2_EQ_GAINS_2, 11, 31, 0,
631 eq_tlv),
632 SOC_SINGLE_TLV("AIF2 EQ5 Volume", WM8994_AIF2_EQ_GAINS_2, 6, 31, 0,
633 eq_tlv),
636 static const char *wm8958_ng_text[] = {
637 "30ms", "125ms", "250ms", "500ms",
640 static const struct soc_enum wm8958_aif1dac1_ng_hold =
641 SOC_ENUM_SINGLE(WM8958_AIF1_DAC1_NOISE_GATE,
642 WM8958_AIF1DAC1_NG_THR_SHIFT, 4, wm8958_ng_text);
644 static const struct soc_enum wm8958_aif1dac2_ng_hold =
645 SOC_ENUM_SINGLE(WM8958_AIF1_DAC2_NOISE_GATE,
646 WM8958_AIF1DAC2_NG_THR_SHIFT, 4, wm8958_ng_text);
648 static const struct soc_enum wm8958_aif2dac_ng_hold =
649 SOC_ENUM_SINGLE(WM8958_AIF2_DAC_NOISE_GATE,
650 WM8958_AIF2DAC_NG_THR_SHIFT, 4, wm8958_ng_text);
652 static const struct snd_kcontrol_new wm8958_snd_controls[] = {
653 SOC_SINGLE_TLV("AIF3 Boost Volume", WM8958_AIF3_CONTROL_2, 10, 3, 0, aif_tlv),
655 SOC_SINGLE("AIF1DAC1 Noise Gate Switch", WM8958_AIF1_DAC1_NOISE_GATE,
656 WM8958_AIF1DAC1_NG_ENA_SHIFT, 1, 0),
657 SOC_ENUM("AIF1DAC1 Noise Gate Hold Time", wm8958_aif1dac1_ng_hold),
658 SOC_SINGLE_TLV("AIF1DAC1 Noise Gate Threshold Volume",
659 WM8958_AIF1_DAC1_NOISE_GATE, WM8958_AIF1DAC1_NG_THR_SHIFT,
660 7, 1, ng_tlv),
662 SOC_SINGLE("AIF1DAC2 Noise Gate Switch", WM8958_AIF1_DAC2_NOISE_GATE,
663 WM8958_AIF1DAC2_NG_ENA_SHIFT, 1, 0),
664 SOC_ENUM("AIF1DAC2 Noise Gate Hold Time", wm8958_aif1dac2_ng_hold),
665 SOC_SINGLE_TLV("AIF1DAC2 Noise Gate Threshold Volume",
666 WM8958_AIF1_DAC2_NOISE_GATE, WM8958_AIF1DAC2_NG_THR_SHIFT,
667 7, 1, ng_tlv),
669 SOC_SINGLE("AIF2DAC Noise Gate Switch", WM8958_AIF2_DAC_NOISE_GATE,
670 WM8958_AIF2DAC_NG_ENA_SHIFT, 1, 0),
671 SOC_ENUM("AIF2DAC Noise Gate Hold Time", wm8958_aif2dac_ng_hold),
672 SOC_SINGLE_TLV("AIF2DAC Noise Gate Threshold Volume",
673 WM8958_AIF2_DAC_NOISE_GATE, WM8958_AIF2DAC_NG_THR_SHIFT,
674 7, 1, ng_tlv),
677 static const struct snd_kcontrol_new wm1811_snd_controls[] = {
678 SOC_SINGLE_TLV("MIXINL IN1LP Boost Volume", WM8994_INPUT_MIXER_1, 7, 1, 0,
679 mixin_boost_tlv),
680 SOC_SINGLE_TLV("MIXINL IN1RP Boost Volume", WM8994_INPUT_MIXER_1, 8, 1, 0,
681 mixin_boost_tlv),
684 /* We run all mode setting through a function to enforce audio mode */
685 static void wm1811_jackdet_set_mode(struct snd_soc_codec *codec, u16 mode)
687 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
689 if (!wm8994->jackdet || !wm8994->jack_cb)
690 return;
692 if (wm8994->active_refcount)
693 mode = WM1811_JACKDET_MODE_AUDIO;
695 if (mode == wm8994->jackdet_mode)
696 return;
698 wm8994->jackdet_mode = mode;
700 /* Always use audio mode to detect while the system is active */
701 if (mode != WM1811_JACKDET_MODE_NONE)
702 mode = WM1811_JACKDET_MODE_AUDIO;
704 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
705 WM1811_JACKDET_MODE_MASK, mode);
708 static void active_reference(struct snd_soc_codec *codec)
710 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
712 mutex_lock(&wm8994->accdet_lock);
714 wm8994->active_refcount++;
716 dev_dbg(codec->dev, "Active refcount incremented, now %d\n",
717 wm8994->active_refcount);
719 /* If we're using jack detection go into audio mode */
720 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_AUDIO);
722 mutex_unlock(&wm8994->accdet_lock);
725 static void active_dereference(struct snd_soc_codec *codec)
727 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
728 u16 mode;
730 mutex_lock(&wm8994->accdet_lock);
732 wm8994->active_refcount--;
734 dev_dbg(codec->dev, "Active refcount decremented, now %d\n",
735 wm8994->active_refcount);
737 if (wm8994->active_refcount == 0) {
738 /* Go into appropriate detection only mode */
739 if (wm8994->jack_mic || wm8994->mic_detecting)
740 mode = WM1811_JACKDET_MODE_MIC;
741 else
742 mode = WM1811_JACKDET_MODE_JACK;
744 wm1811_jackdet_set_mode(codec, mode);
747 mutex_unlock(&wm8994->accdet_lock);
750 static int clk_sys_event(struct snd_soc_dapm_widget *w,
751 struct snd_kcontrol *kcontrol, int event)
753 struct snd_soc_codec *codec = w->codec;
755 switch (event) {
756 case SND_SOC_DAPM_PRE_PMU:
757 return configure_clock(codec);
759 case SND_SOC_DAPM_POST_PMD:
760 configure_clock(codec);
761 break;
764 return 0;
767 static void vmid_reference(struct snd_soc_codec *codec)
769 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
771 pm_runtime_get_sync(codec->dev);
773 wm8994->vmid_refcount++;
775 dev_dbg(codec->dev, "Referencing VMID, refcount is now %d\n",
776 wm8994->vmid_refcount);
778 if (wm8994->vmid_refcount == 1) {
779 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
780 WM8994_LINEOUT_VMID_BUF_ENA |
781 WM8994_LINEOUT1_DISCH |
782 WM8994_LINEOUT2_DISCH,
783 WM8994_LINEOUT_VMID_BUF_ENA);
785 wm_hubs_vmid_ena(codec);
787 /* Startup bias, VMID ramp & buffer */
788 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
789 WM8994_BIAS_SRC |
790 WM8994_VMID_DISCH |
791 WM8994_STARTUP_BIAS_ENA |
792 WM8994_VMID_BUF_ENA |
793 WM8994_VMID_RAMP_MASK,
794 WM8994_BIAS_SRC |
795 WM8994_STARTUP_BIAS_ENA |
796 WM8994_VMID_BUF_ENA |
797 (0x2 << WM8994_VMID_RAMP_SHIFT));
799 /* Main bias enable, VMID=2x40k */
800 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
801 WM8994_BIAS_ENA |
802 WM8994_VMID_SEL_MASK,
803 WM8994_BIAS_ENA | 0x2);
805 msleep(50);
807 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
808 WM8994_VMID_RAMP_MASK | WM8994_BIAS_SRC,
813 static void vmid_dereference(struct snd_soc_codec *codec)
815 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
817 wm8994->vmid_refcount--;
819 dev_dbg(codec->dev, "Dereferencing VMID, refcount is now %d\n",
820 wm8994->vmid_refcount);
822 if (wm8994->vmid_refcount == 0) {
823 /* Switch over to startup biases */
824 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
825 WM8994_BIAS_SRC |
826 WM8994_STARTUP_BIAS_ENA |
827 WM8994_VMID_BUF_ENA |
828 WM8994_VMID_RAMP_MASK,
829 WM8994_BIAS_SRC |
830 WM8994_STARTUP_BIAS_ENA |
831 WM8994_VMID_BUF_ENA |
832 (1 << WM8994_VMID_RAMP_SHIFT));
834 /* Disable main biases */
835 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
836 WM8994_BIAS_ENA |
837 WM8994_VMID_SEL_MASK, 0);
839 /* Discharge VMID */
840 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
841 WM8994_VMID_DISCH, WM8994_VMID_DISCH);
843 /* Discharge line */
844 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
845 WM8994_LINEOUT1_DISCH |
846 WM8994_LINEOUT2_DISCH,
847 WM8994_LINEOUT1_DISCH |
848 WM8994_LINEOUT2_DISCH);
850 msleep(5);
852 /* Switch off startup biases */
853 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
854 WM8994_BIAS_SRC |
855 WM8994_STARTUP_BIAS_ENA |
856 WM8994_VMID_BUF_ENA |
857 WM8994_VMID_RAMP_MASK, 0);
860 pm_runtime_put(codec->dev);
863 static int vmid_event(struct snd_soc_dapm_widget *w,
864 struct snd_kcontrol *kcontrol, int event)
866 struct snd_soc_codec *codec = w->codec;
868 switch (event) {
869 case SND_SOC_DAPM_PRE_PMU:
870 vmid_reference(codec);
871 break;
873 case SND_SOC_DAPM_POST_PMD:
874 vmid_dereference(codec);
875 break;
878 return 0;
881 static void wm8994_update_class_w(struct snd_soc_codec *codec)
883 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
884 int enable = 1;
885 int source = 0; /* GCC flow analysis can't track enable */
886 int reg, reg_r;
888 /* Only support direct DAC->headphone paths */
889 reg = snd_soc_read(codec, WM8994_OUTPUT_MIXER_1);
890 if (!(reg & WM8994_DAC1L_TO_HPOUT1L)) {
891 dev_vdbg(codec->dev, "HPL connected to output mixer\n");
892 enable = 0;
895 reg = snd_soc_read(codec, WM8994_OUTPUT_MIXER_2);
896 if (!(reg & WM8994_DAC1R_TO_HPOUT1R)) {
897 dev_vdbg(codec->dev, "HPR connected to output mixer\n");
898 enable = 0;
901 /* We also need the same setting for L/R and only one path */
902 reg = snd_soc_read(codec, WM8994_DAC1_LEFT_MIXER_ROUTING);
903 switch (reg) {
904 case WM8994_AIF2DACL_TO_DAC1L:
905 dev_vdbg(codec->dev, "Class W source AIF2DAC\n");
906 source = 2 << WM8994_CP_DYN_SRC_SEL_SHIFT;
907 break;
908 case WM8994_AIF1DAC2L_TO_DAC1L:
909 dev_vdbg(codec->dev, "Class W source AIF1DAC2\n");
910 source = 1 << WM8994_CP_DYN_SRC_SEL_SHIFT;
911 break;
912 case WM8994_AIF1DAC1L_TO_DAC1L:
913 dev_vdbg(codec->dev, "Class W source AIF1DAC1\n");
914 source = 0 << WM8994_CP_DYN_SRC_SEL_SHIFT;
915 break;
916 default:
917 dev_vdbg(codec->dev, "DAC mixer setting: %x\n", reg);
918 enable = 0;
919 break;
922 reg_r = snd_soc_read(codec, WM8994_DAC1_RIGHT_MIXER_ROUTING);
923 if (reg_r != reg) {
924 dev_vdbg(codec->dev, "Left and right DAC mixers different\n");
925 enable = 0;
928 if (enable) {
929 dev_dbg(codec->dev, "Class W enabled\n");
930 snd_soc_update_bits(codec, WM8994_CLASS_W_1,
931 WM8994_CP_DYN_PWR |
932 WM8994_CP_DYN_SRC_SEL_MASK,
933 source | WM8994_CP_DYN_PWR);
934 wm8994->hubs.class_w = true;
936 } else {
937 dev_dbg(codec->dev, "Class W disabled\n");
938 snd_soc_update_bits(codec, WM8994_CLASS_W_1,
939 WM8994_CP_DYN_PWR, 0);
940 wm8994->hubs.class_w = false;
944 static int late_enable_ev(struct snd_soc_dapm_widget *w,
945 struct snd_kcontrol *kcontrol, int event)
947 struct snd_soc_codec *codec = w->codec;
948 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
950 switch (event) {
951 case SND_SOC_DAPM_PRE_PMU:
952 if (wm8994->aif1clk_enable) {
953 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
954 WM8994_AIF1CLK_ENA_MASK,
955 WM8994_AIF1CLK_ENA);
956 wm8994->aif1clk_enable = 0;
958 if (wm8994->aif2clk_enable) {
959 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
960 WM8994_AIF2CLK_ENA_MASK,
961 WM8994_AIF2CLK_ENA);
962 wm8994->aif2clk_enable = 0;
964 break;
967 /* We may also have postponed startup of DSP, handle that. */
968 wm8958_aif_ev(w, kcontrol, event);
970 return 0;
973 static int late_disable_ev(struct snd_soc_dapm_widget *w,
974 struct snd_kcontrol *kcontrol, int event)
976 struct snd_soc_codec *codec = w->codec;
977 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
979 switch (event) {
980 case SND_SOC_DAPM_POST_PMD:
981 if (wm8994->aif1clk_disable) {
982 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
983 WM8994_AIF1CLK_ENA_MASK, 0);
984 wm8994->aif1clk_disable = 0;
986 if (wm8994->aif2clk_disable) {
987 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
988 WM8994_AIF2CLK_ENA_MASK, 0);
989 wm8994->aif2clk_disable = 0;
991 break;
994 return 0;
997 static int aif1clk_ev(struct snd_soc_dapm_widget *w,
998 struct snd_kcontrol *kcontrol, int event)
1000 struct snd_soc_codec *codec = w->codec;
1001 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1003 switch (event) {
1004 case SND_SOC_DAPM_PRE_PMU:
1005 wm8994->aif1clk_enable = 1;
1006 break;
1007 case SND_SOC_DAPM_POST_PMD:
1008 wm8994->aif1clk_disable = 1;
1009 break;
1012 return 0;
1015 static int aif2clk_ev(struct snd_soc_dapm_widget *w,
1016 struct snd_kcontrol *kcontrol, int event)
1018 struct snd_soc_codec *codec = w->codec;
1019 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1021 switch (event) {
1022 case SND_SOC_DAPM_PRE_PMU:
1023 wm8994->aif2clk_enable = 1;
1024 break;
1025 case SND_SOC_DAPM_POST_PMD:
1026 wm8994->aif2clk_disable = 1;
1027 break;
1030 return 0;
1033 static int adc_mux_ev(struct snd_soc_dapm_widget *w,
1034 struct snd_kcontrol *kcontrol, int event)
1036 late_enable_ev(w, kcontrol, event);
1037 return 0;
1040 static int micbias_ev(struct snd_soc_dapm_widget *w,
1041 struct snd_kcontrol *kcontrol, int event)
1043 late_enable_ev(w, kcontrol, event);
1044 return 0;
1047 static int dac_ev(struct snd_soc_dapm_widget *w,
1048 struct snd_kcontrol *kcontrol, int event)
1050 struct snd_soc_codec *codec = w->codec;
1051 unsigned int mask = 1 << w->shift;
1053 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1054 mask, mask);
1055 return 0;
1058 static const char *hp_mux_text[] = {
1059 "Mixer",
1060 "DAC",
1063 #define WM8994_HP_ENUM(xname, xenum) \
1064 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1065 .info = snd_soc_info_enum_double, \
1066 .get = snd_soc_dapm_get_enum_double, \
1067 .put = wm8994_put_hp_enum, \
1068 .private_value = (unsigned long)&xenum }
1070 static int wm8994_put_hp_enum(struct snd_kcontrol *kcontrol,
1071 struct snd_ctl_elem_value *ucontrol)
1073 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1074 struct snd_soc_dapm_widget *w = wlist->widgets[0];
1075 struct snd_soc_codec *codec = w->codec;
1076 int ret;
1078 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
1080 wm8994_update_class_w(codec);
1082 return ret;
1085 static const struct soc_enum hpl_enum =
1086 SOC_ENUM_SINGLE(WM8994_OUTPUT_MIXER_1, 8, 2, hp_mux_text);
1088 static const struct snd_kcontrol_new hpl_mux =
1089 WM8994_HP_ENUM("Left Headphone Mux", hpl_enum);
1091 static const struct soc_enum hpr_enum =
1092 SOC_ENUM_SINGLE(WM8994_OUTPUT_MIXER_2, 8, 2, hp_mux_text);
1094 static const struct snd_kcontrol_new hpr_mux =
1095 WM8994_HP_ENUM("Right Headphone Mux", hpr_enum);
1097 static const char *adc_mux_text[] = {
1098 "ADC",
1099 "DMIC",
1102 static const struct soc_enum adc_enum =
1103 SOC_ENUM_SINGLE(0, 0, 2, adc_mux_text);
1105 static const struct snd_kcontrol_new adcl_mux =
1106 SOC_DAPM_ENUM_VIRT("ADCL Mux", adc_enum);
1108 static const struct snd_kcontrol_new adcr_mux =
1109 SOC_DAPM_ENUM_VIRT("ADCR Mux", adc_enum);
1111 static const struct snd_kcontrol_new left_speaker_mixer[] = {
1112 SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 9, 1, 0),
1113 SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 7, 1, 0),
1114 SOC_DAPM_SINGLE("IN1LP Switch", WM8994_SPEAKER_MIXER, 5, 1, 0),
1115 SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 3, 1, 0),
1116 SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 1, 1, 0),
1119 static const struct snd_kcontrol_new right_speaker_mixer[] = {
1120 SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 8, 1, 0),
1121 SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 6, 1, 0),
1122 SOC_DAPM_SINGLE("IN1RP Switch", WM8994_SPEAKER_MIXER, 4, 1, 0),
1123 SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 2, 1, 0),
1124 SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 0, 1, 0),
1127 /* Debugging; dump chip status after DAPM transitions */
1128 static int post_ev(struct snd_soc_dapm_widget *w,
1129 struct snd_kcontrol *kcontrol, int event)
1131 struct snd_soc_codec *codec = w->codec;
1132 dev_dbg(codec->dev, "SRC status: %x\n",
1133 snd_soc_read(codec,
1134 WM8994_RATE_STATUS));
1135 return 0;
1138 static const struct snd_kcontrol_new aif1adc1l_mix[] = {
1139 SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1140 1, 1, 0),
1141 SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1142 0, 1, 0),
1145 static const struct snd_kcontrol_new aif1adc1r_mix[] = {
1146 SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1147 1, 1, 0),
1148 SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1149 0, 1, 0),
1152 static const struct snd_kcontrol_new aif1adc2l_mix[] = {
1153 SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1154 1, 1, 0),
1155 SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1156 0, 1, 0),
1159 static const struct snd_kcontrol_new aif1adc2r_mix[] = {
1160 SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1161 1, 1, 0),
1162 SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1163 0, 1, 0),
1166 static const struct snd_kcontrol_new aif2dac2l_mix[] = {
1167 SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1168 5, 1, 0),
1169 SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1170 4, 1, 0),
1171 SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1172 2, 1, 0),
1173 SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1174 1, 1, 0),
1175 SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1176 0, 1, 0),
1179 static const struct snd_kcontrol_new aif2dac2r_mix[] = {
1180 SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1181 5, 1, 0),
1182 SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1183 4, 1, 0),
1184 SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1185 2, 1, 0),
1186 SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1187 1, 1, 0),
1188 SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1189 0, 1, 0),
1192 #define WM8994_CLASS_W_SWITCH(xname, reg, shift, max, invert) \
1193 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1194 .info = snd_soc_info_volsw, \
1195 .get = snd_soc_dapm_get_volsw, .put = wm8994_put_class_w, \
1196 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
1198 static int wm8994_put_class_w(struct snd_kcontrol *kcontrol,
1199 struct snd_ctl_elem_value *ucontrol)
1201 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1202 struct snd_soc_dapm_widget *w = wlist->widgets[0];
1203 struct snd_soc_codec *codec = w->codec;
1204 int ret;
1206 ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
1208 wm8994_update_class_w(codec);
1210 return ret;
1213 static const struct snd_kcontrol_new dac1l_mix[] = {
1214 WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1215 5, 1, 0),
1216 WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1217 4, 1, 0),
1218 WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1219 2, 1, 0),
1220 WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1221 1, 1, 0),
1222 WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1223 0, 1, 0),
1226 static const struct snd_kcontrol_new dac1r_mix[] = {
1227 WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1228 5, 1, 0),
1229 WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1230 4, 1, 0),
1231 WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1232 2, 1, 0),
1233 WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1234 1, 1, 0),
1235 WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1236 0, 1, 0),
1239 static const char *sidetone_text[] = {
1240 "ADC/DMIC1", "DMIC2",
1243 static const struct soc_enum sidetone1_enum =
1244 SOC_ENUM_SINGLE(WM8994_SIDETONE, 0, 2, sidetone_text);
1246 static const struct snd_kcontrol_new sidetone1_mux =
1247 SOC_DAPM_ENUM("Left Sidetone Mux", sidetone1_enum);
1249 static const struct soc_enum sidetone2_enum =
1250 SOC_ENUM_SINGLE(WM8994_SIDETONE, 1, 2, sidetone_text);
1252 static const struct snd_kcontrol_new sidetone2_mux =
1253 SOC_DAPM_ENUM("Right Sidetone Mux", sidetone2_enum);
1255 static const char *aif1dac_text[] = {
1256 "AIF1DACDAT", "AIF3DACDAT",
1259 static const struct soc_enum aif1dac_enum =
1260 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 0, 2, aif1dac_text);
1262 static const struct snd_kcontrol_new aif1dac_mux =
1263 SOC_DAPM_ENUM("AIF1DAC Mux", aif1dac_enum);
1265 static const char *aif2dac_text[] = {
1266 "AIF2DACDAT", "AIF3DACDAT",
1269 static const struct soc_enum aif2dac_enum =
1270 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 1, 2, aif2dac_text);
1272 static const struct snd_kcontrol_new aif2dac_mux =
1273 SOC_DAPM_ENUM("AIF2DAC Mux", aif2dac_enum);
1275 static const char *aif2adc_text[] = {
1276 "AIF2ADCDAT", "AIF3DACDAT",
1279 static const struct soc_enum aif2adc_enum =
1280 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 2, 2, aif2adc_text);
1282 static const struct snd_kcontrol_new aif2adc_mux =
1283 SOC_DAPM_ENUM("AIF2ADC Mux", aif2adc_enum);
1285 static const char *aif3adc_text[] = {
1286 "AIF1ADCDAT", "AIF2ADCDAT", "AIF2DACDAT", "Mono PCM",
1289 static const struct soc_enum wm8994_aif3adc_enum =
1290 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 3, 3, aif3adc_text);
1292 static const struct snd_kcontrol_new wm8994_aif3adc_mux =
1293 SOC_DAPM_ENUM("AIF3ADC Mux", wm8994_aif3adc_enum);
1295 static const struct soc_enum wm8958_aif3adc_enum =
1296 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 3, 4, aif3adc_text);
1298 static const struct snd_kcontrol_new wm8958_aif3adc_mux =
1299 SOC_DAPM_ENUM("AIF3ADC Mux", wm8958_aif3adc_enum);
1301 static const char *mono_pcm_out_text[] = {
1302 "None", "AIF2ADCL", "AIF2ADCR",
1305 static const struct soc_enum mono_pcm_out_enum =
1306 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 9, 3, mono_pcm_out_text);
1308 static const struct snd_kcontrol_new mono_pcm_out_mux =
1309 SOC_DAPM_ENUM("Mono PCM Out Mux", mono_pcm_out_enum);
1311 static const char *aif2dac_src_text[] = {
1312 "AIF2", "AIF3",
1315 /* Note that these two control shouldn't be simultaneously switched to AIF3 */
1316 static const struct soc_enum aif2dacl_src_enum =
1317 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 7, 2, aif2dac_src_text);
1319 static const struct snd_kcontrol_new aif2dacl_src_mux =
1320 SOC_DAPM_ENUM("AIF2DACL Mux", aif2dacl_src_enum);
1322 static const struct soc_enum aif2dacr_src_enum =
1323 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 8, 2, aif2dac_src_text);
1325 static const struct snd_kcontrol_new aif2dacr_src_mux =
1326 SOC_DAPM_ENUM("AIF2DACR Mux", aif2dacr_src_enum);
1328 static const struct snd_soc_dapm_widget wm8994_lateclk_revd_widgets[] = {
1329 SND_SOC_DAPM_SUPPLY("AIF1CLK", SND_SOC_NOPM, 0, 0, aif1clk_ev,
1330 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1331 SND_SOC_DAPM_SUPPLY("AIF2CLK", SND_SOC_NOPM, 0, 0, aif2clk_ev,
1332 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1334 SND_SOC_DAPM_PGA_E("Late DAC1L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1335 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1336 SND_SOC_DAPM_PGA_E("Late DAC1R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1337 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1338 SND_SOC_DAPM_PGA_E("Late DAC2L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1339 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1340 SND_SOC_DAPM_PGA_E("Late DAC2R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1341 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1342 SND_SOC_DAPM_PGA_E("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0,
1343 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1345 SND_SOC_DAPM_MIXER_E("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
1346 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer),
1347 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1348 SND_SOC_DAPM_MIXER_E("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
1349 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer),
1350 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1351 SND_SOC_DAPM_MUX_E("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &hpl_mux,
1352 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1353 SND_SOC_DAPM_MUX_E("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &hpr_mux,
1354 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1356 SND_SOC_DAPM_POST("Late Disable PGA", late_disable_ev)
1359 static const struct snd_soc_dapm_widget wm8994_lateclk_widgets[] = {
1360 SND_SOC_DAPM_SUPPLY("AIF1CLK", WM8994_AIF1_CLOCKING_1, 0, 0, NULL, 0),
1361 SND_SOC_DAPM_SUPPLY("AIF2CLK", WM8994_AIF2_CLOCKING_1, 0, 0, NULL, 0),
1362 SND_SOC_DAPM_PGA("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0),
1363 SND_SOC_DAPM_MIXER("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
1364 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
1365 SND_SOC_DAPM_MIXER("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
1366 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
1367 SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
1368 SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
1371 static const struct snd_soc_dapm_widget wm8994_dac_revd_widgets[] = {
1372 SND_SOC_DAPM_DAC_E("DAC2L", NULL, SND_SOC_NOPM, 3, 0,
1373 dac_ev, SND_SOC_DAPM_PRE_PMU),
1374 SND_SOC_DAPM_DAC_E("DAC2R", NULL, SND_SOC_NOPM, 2, 0,
1375 dac_ev, SND_SOC_DAPM_PRE_PMU),
1376 SND_SOC_DAPM_DAC_E("DAC1L", NULL, SND_SOC_NOPM, 1, 0,
1377 dac_ev, SND_SOC_DAPM_PRE_PMU),
1378 SND_SOC_DAPM_DAC_E("DAC1R", NULL, SND_SOC_NOPM, 0, 0,
1379 dac_ev, SND_SOC_DAPM_PRE_PMU),
1382 static const struct snd_soc_dapm_widget wm8994_dac_widgets[] = {
1383 SND_SOC_DAPM_DAC("DAC2L", NULL, WM8994_POWER_MANAGEMENT_5, 3, 0),
1384 SND_SOC_DAPM_DAC("DAC2R", NULL, WM8994_POWER_MANAGEMENT_5, 2, 0),
1385 SND_SOC_DAPM_DAC("DAC1L", NULL, WM8994_POWER_MANAGEMENT_5, 1, 0),
1386 SND_SOC_DAPM_DAC("DAC1R", NULL, WM8994_POWER_MANAGEMENT_5, 0, 0),
1389 static const struct snd_soc_dapm_widget wm8994_adc_revd_widgets[] = {
1390 SND_SOC_DAPM_VIRT_MUX_E("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux,
1391 adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
1392 SND_SOC_DAPM_VIRT_MUX_E("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux,
1393 adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
1396 static const struct snd_soc_dapm_widget wm8994_adc_widgets[] = {
1397 SND_SOC_DAPM_VIRT_MUX("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux),
1398 SND_SOC_DAPM_VIRT_MUX("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux),
1401 static const struct snd_soc_dapm_widget wm8994_dapm_widgets[] = {
1402 SND_SOC_DAPM_INPUT("DMIC1DAT"),
1403 SND_SOC_DAPM_INPUT("DMIC2DAT"),
1404 SND_SOC_DAPM_INPUT("Clock"),
1406 SND_SOC_DAPM_SUPPLY_S("MICBIAS Supply", 1, SND_SOC_NOPM, 0, 0, micbias_ev,
1407 SND_SOC_DAPM_PRE_PMU),
1408 SND_SOC_DAPM_SUPPLY("VMID", SND_SOC_NOPM, 0, 0, vmid_event,
1409 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1411 SND_SOC_DAPM_SUPPLY("CLK_SYS", SND_SOC_NOPM, 0, 0, clk_sys_event,
1412 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1414 SND_SOC_DAPM_SUPPLY("DSP1CLK", WM8994_CLOCKING_1, 3, 0, NULL, 0),
1415 SND_SOC_DAPM_SUPPLY("DSP2CLK", WM8994_CLOCKING_1, 2, 0, NULL, 0),
1416 SND_SOC_DAPM_SUPPLY("DSPINTCLK", WM8994_CLOCKING_1, 1, 0, NULL, 0),
1418 SND_SOC_DAPM_AIF_OUT("AIF1ADC1L", NULL,
1419 0, WM8994_POWER_MANAGEMENT_4, 9, 0),
1420 SND_SOC_DAPM_AIF_OUT("AIF1ADC1R", NULL,
1421 0, WM8994_POWER_MANAGEMENT_4, 8, 0),
1422 SND_SOC_DAPM_AIF_IN_E("AIF1DAC1L", NULL, 0,
1423 WM8994_POWER_MANAGEMENT_5, 9, 0, wm8958_aif_ev,
1424 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1425 SND_SOC_DAPM_AIF_IN_E("AIF1DAC1R", NULL, 0,
1426 WM8994_POWER_MANAGEMENT_5, 8, 0, wm8958_aif_ev,
1427 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1429 SND_SOC_DAPM_AIF_OUT("AIF1ADC2L", NULL,
1430 0, WM8994_POWER_MANAGEMENT_4, 11, 0),
1431 SND_SOC_DAPM_AIF_OUT("AIF1ADC2R", NULL,
1432 0, WM8994_POWER_MANAGEMENT_4, 10, 0),
1433 SND_SOC_DAPM_AIF_IN_E("AIF1DAC2L", NULL, 0,
1434 WM8994_POWER_MANAGEMENT_5, 11, 0, wm8958_aif_ev,
1435 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1436 SND_SOC_DAPM_AIF_IN_E("AIF1DAC2R", NULL, 0,
1437 WM8994_POWER_MANAGEMENT_5, 10, 0, wm8958_aif_ev,
1438 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1440 SND_SOC_DAPM_MIXER("AIF1ADC1L Mixer", SND_SOC_NOPM, 0, 0,
1441 aif1adc1l_mix, ARRAY_SIZE(aif1adc1l_mix)),
1442 SND_SOC_DAPM_MIXER("AIF1ADC1R Mixer", SND_SOC_NOPM, 0, 0,
1443 aif1adc1r_mix, ARRAY_SIZE(aif1adc1r_mix)),
1445 SND_SOC_DAPM_MIXER("AIF1ADC2L Mixer", SND_SOC_NOPM, 0, 0,
1446 aif1adc2l_mix, ARRAY_SIZE(aif1adc2l_mix)),
1447 SND_SOC_DAPM_MIXER("AIF1ADC2R Mixer", SND_SOC_NOPM, 0, 0,
1448 aif1adc2r_mix, ARRAY_SIZE(aif1adc2r_mix)),
1450 SND_SOC_DAPM_MIXER("AIF2DAC2L Mixer", SND_SOC_NOPM, 0, 0,
1451 aif2dac2l_mix, ARRAY_SIZE(aif2dac2l_mix)),
1452 SND_SOC_DAPM_MIXER("AIF2DAC2R Mixer", SND_SOC_NOPM, 0, 0,
1453 aif2dac2r_mix, ARRAY_SIZE(aif2dac2r_mix)),
1455 SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &sidetone1_mux),
1456 SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &sidetone2_mux),
1458 SND_SOC_DAPM_MIXER("DAC1L Mixer", SND_SOC_NOPM, 0, 0,
1459 dac1l_mix, ARRAY_SIZE(dac1l_mix)),
1460 SND_SOC_DAPM_MIXER("DAC1R Mixer", SND_SOC_NOPM, 0, 0,
1461 dac1r_mix, ARRAY_SIZE(dac1r_mix)),
1463 SND_SOC_DAPM_AIF_OUT("AIF2ADCL", NULL, 0,
1464 WM8994_POWER_MANAGEMENT_4, 13, 0),
1465 SND_SOC_DAPM_AIF_OUT("AIF2ADCR", NULL, 0,
1466 WM8994_POWER_MANAGEMENT_4, 12, 0),
1467 SND_SOC_DAPM_AIF_IN_E("AIF2DACL", NULL, 0,
1468 WM8994_POWER_MANAGEMENT_5, 13, 0, wm8958_aif_ev,
1469 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1470 SND_SOC_DAPM_AIF_IN_E("AIF2DACR", NULL, 0,
1471 WM8994_POWER_MANAGEMENT_5, 12, 0, wm8958_aif_ev,
1472 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1474 SND_SOC_DAPM_AIF_IN("AIF1DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1475 SND_SOC_DAPM_AIF_IN("AIF2DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1476 SND_SOC_DAPM_AIF_OUT("AIF1ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1477 SND_SOC_DAPM_AIF_OUT("AIF2ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1479 SND_SOC_DAPM_MUX("AIF1DAC Mux", SND_SOC_NOPM, 0, 0, &aif1dac_mux),
1480 SND_SOC_DAPM_MUX("AIF2DAC Mux", SND_SOC_NOPM, 0, 0, &aif2dac_mux),
1481 SND_SOC_DAPM_MUX("AIF2ADC Mux", SND_SOC_NOPM, 0, 0, &aif2adc_mux),
1483 SND_SOC_DAPM_AIF_IN("AIF3DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1484 SND_SOC_DAPM_AIF_OUT("AIF3ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1486 SND_SOC_DAPM_SUPPLY("TOCLK", WM8994_CLOCKING_1, 4, 0, NULL, 0),
1488 SND_SOC_DAPM_ADC("DMIC2L", NULL, WM8994_POWER_MANAGEMENT_4, 5, 0),
1489 SND_SOC_DAPM_ADC("DMIC2R", NULL, WM8994_POWER_MANAGEMENT_4, 4, 0),
1490 SND_SOC_DAPM_ADC("DMIC1L", NULL, WM8994_POWER_MANAGEMENT_4, 3, 0),
1491 SND_SOC_DAPM_ADC("DMIC1R", NULL, WM8994_POWER_MANAGEMENT_4, 2, 0),
1493 /* Power is done with the muxes since the ADC power also controls the
1494 * downsampling chain, the chip will automatically manage the analogue
1495 * specific portions.
1497 SND_SOC_DAPM_ADC("ADCL", NULL, SND_SOC_NOPM, 1, 0),
1498 SND_SOC_DAPM_ADC("ADCR", NULL, SND_SOC_NOPM, 0, 0),
1500 SND_SOC_DAPM_POST("Debug log", post_ev),
1503 static const struct snd_soc_dapm_widget wm8994_specific_dapm_widgets[] = {
1504 SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8994_aif3adc_mux),
1507 static const struct snd_soc_dapm_widget wm8958_dapm_widgets[] = {
1508 SND_SOC_DAPM_MUX("Mono PCM Out Mux", SND_SOC_NOPM, 0, 0, &mono_pcm_out_mux),
1509 SND_SOC_DAPM_MUX("AIF2DACL Mux", SND_SOC_NOPM, 0, 0, &aif2dacl_src_mux),
1510 SND_SOC_DAPM_MUX("AIF2DACR Mux", SND_SOC_NOPM, 0, 0, &aif2dacr_src_mux),
1511 SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8958_aif3adc_mux),
1514 static const struct snd_soc_dapm_route intercon[] = {
1515 { "CLK_SYS", NULL, "AIF1CLK", check_clk_sys },
1516 { "CLK_SYS", NULL, "AIF2CLK", check_clk_sys },
1518 { "DSP1CLK", NULL, "CLK_SYS" },
1519 { "DSP2CLK", NULL, "CLK_SYS" },
1520 { "DSPINTCLK", NULL, "CLK_SYS" },
1522 { "AIF1ADC1L", NULL, "AIF1CLK" },
1523 { "AIF1ADC1L", NULL, "DSP1CLK" },
1524 { "AIF1ADC1R", NULL, "AIF1CLK" },
1525 { "AIF1ADC1R", NULL, "DSP1CLK" },
1526 { "AIF1ADC1R", NULL, "DSPINTCLK" },
1528 { "AIF1DAC1L", NULL, "AIF1CLK" },
1529 { "AIF1DAC1L", NULL, "DSP1CLK" },
1530 { "AIF1DAC1R", NULL, "AIF1CLK" },
1531 { "AIF1DAC1R", NULL, "DSP1CLK" },
1532 { "AIF1DAC1R", NULL, "DSPINTCLK" },
1534 { "AIF1ADC2L", NULL, "AIF1CLK" },
1535 { "AIF1ADC2L", NULL, "DSP1CLK" },
1536 { "AIF1ADC2R", NULL, "AIF1CLK" },
1537 { "AIF1ADC2R", NULL, "DSP1CLK" },
1538 { "AIF1ADC2R", NULL, "DSPINTCLK" },
1540 { "AIF1DAC2L", NULL, "AIF1CLK" },
1541 { "AIF1DAC2L", NULL, "DSP1CLK" },
1542 { "AIF1DAC2R", NULL, "AIF1CLK" },
1543 { "AIF1DAC2R", NULL, "DSP1CLK" },
1544 { "AIF1DAC2R", NULL, "DSPINTCLK" },
1546 { "AIF2ADCL", NULL, "AIF2CLK" },
1547 { "AIF2ADCL", NULL, "DSP2CLK" },
1548 { "AIF2ADCR", NULL, "AIF2CLK" },
1549 { "AIF2ADCR", NULL, "DSP2CLK" },
1550 { "AIF2ADCR", NULL, "DSPINTCLK" },
1552 { "AIF2DACL", NULL, "AIF2CLK" },
1553 { "AIF2DACL", NULL, "DSP2CLK" },
1554 { "AIF2DACR", NULL, "AIF2CLK" },
1555 { "AIF2DACR", NULL, "DSP2CLK" },
1556 { "AIF2DACR", NULL, "DSPINTCLK" },
1558 { "DMIC1L", NULL, "DMIC1DAT" },
1559 { "DMIC1L", NULL, "CLK_SYS" },
1560 { "DMIC1R", NULL, "DMIC1DAT" },
1561 { "DMIC1R", NULL, "CLK_SYS" },
1562 { "DMIC2L", NULL, "DMIC2DAT" },
1563 { "DMIC2L", NULL, "CLK_SYS" },
1564 { "DMIC2R", NULL, "DMIC2DAT" },
1565 { "DMIC2R", NULL, "CLK_SYS" },
1567 { "ADCL", NULL, "AIF1CLK" },
1568 { "ADCL", NULL, "DSP1CLK" },
1569 { "ADCL", NULL, "DSPINTCLK" },
1571 { "ADCR", NULL, "AIF1CLK" },
1572 { "ADCR", NULL, "DSP1CLK" },
1573 { "ADCR", NULL, "DSPINTCLK" },
1575 { "ADCL Mux", "ADC", "ADCL" },
1576 { "ADCL Mux", "DMIC", "DMIC1L" },
1577 { "ADCR Mux", "ADC", "ADCR" },
1578 { "ADCR Mux", "DMIC", "DMIC1R" },
1580 { "DAC1L", NULL, "AIF1CLK" },
1581 { "DAC1L", NULL, "DSP1CLK" },
1582 { "DAC1L", NULL, "DSPINTCLK" },
1584 { "DAC1R", NULL, "AIF1CLK" },
1585 { "DAC1R", NULL, "DSP1CLK" },
1586 { "DAC1R", NULL, "DSPINTCLK" },
1588 { "DAC2L", NULL, "AIF2CLK" },
1589 { "DAC2L", NULL, "DSP2CLK" },
1590 { "DAC2L", NULL, "DSPINTCLK" },
1592 { "DAC2R", NULL, "AIF2DACR" },
1593 { "DAC2R", NULL, "AIF2CLK" },
1594 { "DAC2R", NULL, "DSP2CLK" },
1595 { "DAC2R", NULL, "DSPINTCLK" },
1597 { "TOCLK", NULL, "CLK_SYS" },
1599 { "AIF1DACDAT", NULL, "AIF1 Playback" },
1600 { "AIF2DACDAT", NULL, "AIF2 Playback" },
1601 { "AIF3DACDAT", NULL, "AIF3 Playback" },
1603 { "AIF1 Capture", NULL, "AIF1ADCDAT" },
1604 { "AIF2 Capture", NULL, "AIF2ADCDAT" },
1605 { "AIF3 Capture", NULL, "AIF3ADCDAT" },
1607 /* AIF1 outputs */
1608 { "AIF1ADC1L", NULL, "AIF1ADC1L Mixer" },
1609 { "AIF1ADC1L Mixer", "ADC/DMIC Switch", "ADCL Mux" },
1610 { "AIF1ADC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1612 { "AIF1ADC1R", NULL, "AIF1ADC1R Mixer" },
1613 { "AIF1ADC1R Mixer", "ADC/DMIC Switch", "ADCR Mux" },
1614 { "AIF1ADC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1616 { "AIF1ADC2L", NULL, "AIF1ADC2L Mixer" },
1617 { "AIF1ADC2L Mixer", "DMIC Switch", "DMIC2L" },
1618 { "AIF1ADC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1620 { "AIF1ADC2R", NULL, "AIF1ADC2R Mixer" },
1621 { "AIF1ADC2R Mixer", "DMIC Switch", "DMIC2R" },
1622 { "AIF1ADC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1624 /* Pin level routing for AIF3 */
1625 { "AIF1DAC1L", NULL, "AIF1DAC Mux" },
1626 { "AIF1DAC1R", NULL, "AIF1DAC Mux" },
1627 { "AIF1DAC2L", NULL, "AIF1DAC Mux" },
1628 { "AIF1DAC2R", NULL, "AIF1DAC Mux" },
1630 { "AIF1DAC Mux", "AIF1DACDAT", "AIF1DACDAT" },
1631 { "AIF1DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1632 { "AIF2DAC Mux", "AIF2DACDAT", "AIF2DACDAT" },
1633 { "AIF2DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1634 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCL" },
1635 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCR" },
1636 { "AIF2ADC Mux", "AIF3DACDAT", "AIF3ADCDAT" },
1638 /* DAC1 inputs */
1639 { "DAC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1640 { "DAC1L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1641 { "DAC1L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1642 { "DAC1L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1643 { "DAC1L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1645 { "DAC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1646 { "DAC1R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1647 { "DAC1R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1648 { "DAC1R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1649 { "DAC1R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1651 /* DAC2/AIF2 outputs */
1652 { "AIF2ADCL", NULL, "AIF2DAC2L Mixer" },
1653 { "AIF2DAC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1654 { "AIF2DAC2L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1655 { "AIF2DAC2L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1656 { "AIF2DAC2L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1657 { "AIF2DAC2L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1659 { "AIF2ADCR", NULL, "AIF2DAC2R Mixer" },
1660 { "AIF2DAC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1661 { "AIF2DAC2R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1662 { "AIF2DAC2R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1663 { "AIF2DAC2R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1664 { "AIF2DAC2R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1666 { "AIF1ADCDAT", NULL, "AIF1ADC1L" },
1667 { "AIF1ADCDAT", NULL, "AIF1ADC1R" },
1668 { "AIF1ADCDAT", NULL, "AIF1ADC2L" },
1669 { "AIF1ADCDAT", NULL, "AIF1ADC2R" },
1671 { "AIF2ADCDAT", NULL, "AIF2ADC Mux" },
1673 /* AIF3 output */
1674 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1L" },
1675 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1R" },
1676 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2L" },
1677 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2R" },
1678 { "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCL" },
1679 { "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCR" },
1680 { "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACL" },
1681 { "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACR" },
1683 /* Sidetone */
1684 { "Left Sidetone", "ADC/DMIC1", "ADCL Mux" },
1685 { "Left Sidetone", "DMIC2", "DMIC2L" },
1686 { "Right Sidetone", "ADC/DMIC1", "ADCR Mux" },
1687 { "Right Sidetone", "DMIC2", "DMIC2R" },
1689 /* Output stages */
1690 { "Left Output Mixer", "DAC Switch", "DAC1L" },
1691 { "Right Output Mixer", "DAC Switch", "DAC1R" },
1693 { "SPKL", "DAC1 Switch", "DAC1L" },
1694 { "SPKL", "DAC2 Switch", "DAC2L" },
1696 { "SPKR", "DAC1 Switch", "DAC1R" },
1697 { "SPKR", "DAC2 Switch", "DAC2R" },
1699 { "Left Headphone Mux", "DAC", "DAC1L" },
1700 { "Right Headphone Mux", "DAC", "DAC1R" },
1703 static const struct snd_soc_dapm_route wm8994_lateclk_revd_intercon[] = {
1704 { "DAC1L", NULL, "Late DAC1L Enable PGA" },
1705 { "Late DAC1L Enable PGA", NULL, "DAC1L Mixer" },
1706 { "DAC1R", NULL, "Late DAC1R Enable PGA" },
1707 { "Late DAC1R Enable PGA", NULL, "DAC1R Mixer" },
1708 { "DAC2L", NULL, "Late DAC2L Enable PGA" },
1709 { "Late DAC2L Enable PGA", NULL, "AIF2DAC2L Mixer" },
1710 { "DAC2R", NULL, "Late DAC2R Enable PGA" },
1711 { "Late DAC2R Enable PGA", NULL, "AIF2DAC2R Mixer" }
1714 static const struct snd_soc_dapm_route wm8994_lateclk_intercon[] = {
1715 { "DAC1L", NULL, "DAC1L Mixer" },
1716 { "DAC1R", NULL, "DAC1R Mixer" },
1717 { "DAC2L", NULL, "AIF2DAC2L Mixer" },
1718 { "DAC2R", NULL, "AIF2DAC2R Mixer" },
1721 static const struct snd_soc_dapm_route wm8994_revd_intercon[] = {
1722 { "AIF1DACDAT", NULL, "AIF2DACDAT" },
1723 { "AIF2DACDAT", NULL, "AIF1DACDAT" },
1724 { "AIF1ADCDAT", NULL, "AIF2ADCDAT" },
1725 { "AIF2ADCDAT", NULL, "AIF1ADCDAT" },
1726 { "MICBIAS1", NULL, "CLK_SYS" },
1727 { "MICBIAS1", NULL, "MICBIAS Supply" },
1728 { "MICBIAS2", NULL, "CLK_SYS" },
1729 { "MICBIAS2", NULL, "MICBIAS Supply" },
1732 static const struct snd_soc_dapm_route wm8994_intercon[] = {
1733 { "AIF2DACL", NULL, "AIF2DAC Mux" },
1734 { "AIF2DACR", NULL, "AIF2DAC Mux" },
1735 { "MICBIAS1", NULL, "VMID" },
1736 { "MICBIAS2", NULL, "VMID" },
1739 static const struct snd_soc_dapm_route wm8958_intercon[] = {
1740 { "AIF2DACL", NULL, "AIF2DACL Mux" },
1741 { "AIF2DACR", NULL, "AIF2DACR Mux" },
1743 { "AIF2DACL Mux", "AIF2", "AIF2DAC Mux" },
1744 { "AIF2DACL Mux", "AIF3", "AIF3DACDAT" },
1745 { "AIF2DACR Mux", "AIF2", "AIF2DAC Mux" },
1746 { "AIF2DACR Mux", "AIF3", "AIF3DACDAT" },
1748 { "Mono PCM Out Mux", "AIF2ADCL", "AIF2ADCL" },
1749 { "Mono PCM Out Mux", "AIF2ADCR", "AIF2ADCR" },
1751 { "AIF3ADC Mux", "Mono PCM", "Mono PCM Out Mux" },
1754 /* The size in bits of the FLL divide multiplied by 10
1755 * to allow rounding later */
1756 #define FIXED_FLL_SIZE ((1 << 16) * 10)
1758 struct fll_div {
1759 u16 outdiv;
1760 u16 n;
1761 u16 k;
1762 u16 clk_ref_div;
1763 u16 fll_fratio;
1766 static int wm8994_get_fll_config(struct fll_div *fll,
1767 int freq_in, int freq_out)
1769 u64 Kpart;
1770 unsigned int K, Ndiv, Nmod;
1772 pr_debug("FLL input=%dHz, output=%dHz\n", freq_in, freq_out);
1774 /* Scale the input frequency down to <= 13.5MHz */
1775 fll->clk_ref_div = 0;
1776 while (freq_in > 13500000) {
1777 fll->clk_ref_div++;
1778 freq_in /= 2;
1780 if (fll->clk_ref_div > 3)
1781 return -EINVAL;
1783 pr_debug("CLK_REF_DIV=%d, Fref=%dHz\n", fll->clk_ref_div, freq_in);
1785 /* Scale the output to give 90MHz<=Fvco<=100MHz */
1786 fll->outdiv = 3;
1787 while (freq_out * (fll->outdiv + 1) < 90000000) {
1788 fll->outdiv++;
1789 if (fll->outdiv > 63)
1790 return -EINVAL;
1792 freq_out *= fll->outdiv + 1;
1793 pr_debug("OUTDIV=%d, Fvco=%dHz\n", fll->outdiv, freq_out);
1795 if (freq_in > 1000000) {
1796 fll->fll_fratio = 0;
1797 } else if (freq_in > 256000) {
1798 fll->fll_fratio = 1;
1799 freq_in *= 2;
1800 } else if (freq_in > 128000) {
1801 fll->fll_fratio = 2;
1802 freq_in *= 4;
1803 } else if (freq_in > 64000) {
1804 fll->fll_fratio = 3;
1805 freq_in *= 8;
1806 } else {
1807 fll->fll_fratio = 4;
1808 freq_in *= 16;
1810 pr_debug("FLL_FRATIO=%d, Fref=%dHz\n", fll->fll_fratio, freq_in);
1812 /* Now, calculate N.K */
1813 Ndiv = freq_out / freq_in;
1815 fll->n = Ndiv;
1816 Nmod = freq_out % freq_in;
1817 pr_debug("Nmod=%d\n", Nmod);
1819 /* Calculate fractional part - scale up so we can round. */
1820 Kpart = FIXED_FLL_SIZE * (long long)Nmod;
1822 do_div(Kpart, freq_in);
1824 K = Kpart & 0xFFFFFFFF;
1826 if ((K % 10) >= 5)
1827 K += 5;
1829 /* Move down to proper range now rounding is done */
1830 fll->k = K / 10;
1832 pr_debug("N=%x K=%x\n", fll->n, fll->k);
1834 return 0;
1837 static int _wm8994_set_fll(struct snd_soc_codec *codec, int id, int src,
1838 unsigned int freq_in, unsigned int freq_out)
1840 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1841 struct wm8994 *control = wm8994->wm8994;
1842 int reg_offset, ret;
1843 struct fll_div fll;
1844 u16 reg, aif1, aif2;
1845 unsigned long timeout;
1846 bool was_enabled;
1848 aif1 = snd_soc_read(codec, WM8994_AIF1_CLOCKING_1)
1849 & WM8994_AIF1CLK_ENA;
1851 aif2 = snd_soc_read(codec, WM8994_AIF2_CLOCKING_1)
1852 & WM8994_AIF2CLK_ENA;
1854 switch (id) {
1855 case WM8994_FLL1:
1856 reg_offset = 0;
1857 id = 0;
1858 break;
1859 case WM8994_FLL2:
1860 reg_offset = 0x20;
1861 id = 1;
1862 break;
1863 default:
1864 return -EINVAL;
1867 reg = snd_soc_read(codec, WM8994_FLL1_CONTROL_1 + reg_offset);
1868 was_enabled = reg & WM8994_FLL1_ENA;
1870 switch (src) {
1871 case 0:
1872 /* Allow no source specification when stopping */
1873 if (freq_out)
1874 return -EINVAL;
1875 src = wm8994->fll[id].src;
1876 break;
1877 case WM8994_FLL_SRC_MCLK1:
1878 case WM8994_FLL_SRC_MCLK2:
1879 case WM8994_FLL_SRC_LRCLK:
1880 case WM8994_FLL_SRC_BCLK:
1881 break;
1882 default:
1883 return -EINVAL;
1886 /* Are we changing anything? */
1887 if (wm8994->fll[id].src == src &&
1888 wm8994->fll[id].in == freq_in && wm8994->fll[id].out == freq_out)
1889 return 0;
1891 /* If we're stopping the FLL redo the old config - no
1892 * registers will actually be written but we avoid GCC flow
1893 * analysis bugs spewing warnings.
1895 if (freq_out)
1896 ret = wm8994_get_fll_config(&fll, freq_in, freq_out);
1897 else
1898 ret = wm8994_get_fll_config(&fll, wm8994->fll[id].in,
1899 wm8994->fll[id].out);
1900 if (ret < 0)
1901 return ret;
1903 /* Gate the AIF clocks while we reclock */
1904 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1905 WM8994_AIF1CLK_ENA, 0);
1906 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1907 WM8994_AIF2CLK_ENA, 0);
1909 /* We always need to disable the FLL while reconfiguring */
1910 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
1911 WM8994_FLL1_ENA, 0);
1913 reg = (fll.outdiv << WM8994_FLL1_OUTDIV_SHIFT) |
1914 (fll.fll_fratio << WM8994_FLL1_FRATIO_SHIFT);
1915 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_2 + reg_offset,
1916 WM8994_FLL1_OUTDIV_MASK |
1917 WM8994_FLL1_FRATIO_MASK, reg);
1919 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_3 + reg_offset,
1920 WM8994_FLL1_K_MASK, fll.k);
1922 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_4 + reg_offset,
1923 WM8994_FLL1_N_MASK,
1924 fll.n << WM8994_FLL1_N_SHIFT);
1926 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_5 + reg_offset,
1927 WM8994_FLL1_REFCLK_DIV_MASK |
1928 WM8994_FLL1_REFCLK_SRC_MASK,
1929 (fll.clk_ref_div << WM8994_FLL1_REFCLK_DIV_SHIFT) |
1930 (src - 1));
1932 /* Clear any pending completion from a previous failure */
1933 try_wait_for_completion(&wm8994->fll_locked[id]);
1935 /* Enable (with fractional mode if required) */
1936 if (freq_out) {
1937 /* Enable VMID if we need it */
1938 if (!was_enabled) {
1939 active_reference(codec);
1941 switch (control->type) {
1942 case WM8994:
1943 vmid_reference(codec);
1944 break;
1945 case WM8958:
1946 if (wm8994->revision < 1)
1947 vmid_reference(codec);
1948 break;
1949 default:
1950 break;
1954 if (fll.k)
1955 reg = WM8994_FLL1_ENA | WM8994_FLL1_FRAC;
1956 else
1957 reg = WM8994_FLL1_ENA;
1958 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
1959 WM8994_FLL1_ENA | WM8994_FLL1_FRAC,
1960 reg);
1962 if (wm8994->fll_locked_irq) {
1963 timeout = wait_for_completion_timeout(&wm8994->fll_locked[id],
1964 msecs_to_jiffies(10));
1965 if (timeout == 0)
1966 dev_warn(codec->dev,
1967 "Timed out waiting for FLL lock\n");
1968 } else {
1969 msleep(5);
1971 } else {
1972 if (was_enabled) {
1973 switch (control->type) {
1974 case WM8994:
1975 vmid_dereference(codec);
1976 break;
1977 case WM8958:
1978 if (wm8994->revision < 1)
1979 vmid_dereference(codec);
1980 break;
1981 default:
1982 break;
1985 active_dereference(codec);
1989 wm8994->fll[id].in = freq_in;
1990 wm8994->fll[id].out = freq_out;
1991 wm8994->fll[id].src = src;
1993 /* Enable any gated AIF clocks */
1994 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1995 WM8994_AIF1CLK_ENA, aif1);
1996 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1997 WM8994_AIF2CLK_ENA, aif2);
1999 configure_clock(codec);
2001 return 0;
2004 static irqreturn_t wm8994_fll_locked_irq(int irq, void *data)
2006 struct completion *completion = data;
2008 complete(completion);
2010 return IRQ_HANDLED;
2013 static int opclk_divs[] = { 10, 20, 30, 40, 55, 60, 80, 120, 160 };
2015 static int wm8994_set_fll(struct snd_soc_dai *dai, int id, int src,
2016 unsigned int freq_in, unsigned int freq_out)
2018 return _wm8994_set_fll(dai->codec, id, src, freq_in, freq_out);
2021 static int wm8994_set_dai_sysclk(struct snd_soc_dai *dai,
2022 int clk_id, unsigned int freq, int dir)
2024 struct snd_soc_codec *codec = dai->codec;
2025 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2026 int i;
2028 switch (dai->id) {
2029 case 1:
2030 case 2:
2031 break;
2033 default:
2034 /* AIF3 shares clocking with AIF1/2 */
2035 return -EINVAL;
2038 switch (clk_id) {
2039 case WM8994_SYSCLK_MCLK1:
2040 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK1;
2041 wm8994->mclk[0] = freq;
2042 dev_dbg(dai->dev, "AIF%d using MCLK1 at %uHz\n",
2043 dai->id, freq);
2044 break;
2046 case WM8994_SYSCLK_MCLK2:
2047 /* TODO: Set GPIO AF */
2048 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK2;
2049 wm8994->mclk[1] = freq;
2050 dev_dbg(dai->dev, "AIF%d using MCLK2 at %uHz\n",
2051 dai->id, freq);
2052 break;
2054 case WM8994_SYSCLK_FLL1:
2055 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL1;
2056 dev_dbg(dai->dev, "AIF%d using FLL1\n", dai->id);
2057 break;
2059 case WM8994_SYSCLK_FLL2:
2060 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL2;
2061 dev_dbg(dai->dev, "AIF%d using FLL2\n", dai->id);
2062 break;
2064 case WM8994_SYSCLK_OPCLK:
2065 /* Special case - a division (times 10) is given and
2066 * no effect on main clocking.
2068 if (freq) {
2069 for (i = 0; i < ARRAY_SIZE(opclk_divs); i++)
2070 if (opclk_divs[i] == freq)
2071 break;
2072 if (i == ARRAY_SIZE(opclk_divs))
2073 return -EINVAL;
2074 snd_soc_update_bits(codec, WM8994_CLOCKING_2,
2075 WM8994_OPCLK_DIV_MASK, i);
2076 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
2077 WM8994_OPCLK_ENA, WM8994_OPCLK_ENA);
2078 } else {
2079 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
2080 WM8994_OPCLK_ENA, 0);
2083 default:
2084 return -EINVAL;
2087 configure_clock(codec);
2089 return 0;
2092 static int wm8994_set_bias_level(struct snd_soc_codec *codec,
2093 enum snd_soc_bias_level level)
2095 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2096 struct wm8994 *control = wm8994->wm8994;
2098 wm_hubs_set_bias_level(codec, level);
2100 switch (level) {
2101 case SND_SOC_BIAS_ON:
2102 break;
2104 case SND_SOC_BIAS_PREPARE:
2105 /* MICBIAS into regulating mode */
2106 switch (control->type) {
2107 case WM8958:
2108 case WM1811:
2109 snd_soc_update_bits(codec, WM8958_MICBIAS1,
2110 WM8958_MICB1_MODE, 0);
2111 snd_soc_update_bits(codec, WM8958_MICBIAS2,
2112 WM8958_MICB2_MODE, 0);
2113 break;
2114 default:
2115 break;
2118 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
2119 active_reference(codec);
2120 break;
2122 case SND_SOC_BIAS_STANDBY:
2123 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
2124 switch (control->type) {
2125 case WM8994:
2126 if (wm8994->revision < 4) {
2127 /* Tweak DC servo and DSP
2128 * configuration for improved
2129 * performance. */
2130 snd_soc_write(codec, 0x102, 0x3);
2131 snd_soc_write(codec, 0x56, 0x3);
2132 snd_soc_write(codec, 0x817, 0);
2133 snd_soc_write(codec, 0x102, 0);
2135 break;
2137 case WM8958:
2138 if (wm8994->revision == 0) {
2139 /* Optimise performance for rev A */
2140 snd_soc_write(codec, 0x102, 0x3);
2141 snd_soc_write(codec, 0xcb, 0x81);
2142 snd_soc_write(codec, 0x817, 0);
2143 snd_soc_write(codec, 0x102, 0);
2145 snd_soc_update_bits(codec,
2146 WM8958_CHARGE_PUMP_2,
2147 WM8958_CP_DISCH,
2148 WM8958_CP_DISCH);
2150 break;
2152 case WM1811:
2153 if (wm8994->revision < 2) {
2154 snd_soc_write(codec, 0x102, 0x3);
2155 snd_soc_write(codec, 0x5d, 0x7e);
2156 snd_soc_write(codec, 0x5e, 0x0);
2157 snd_soc_write(codec, 0x102, 0x0);
2159 break;
2162 /* Discharge LINEOUT1 & 2 */
2163 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
2164 WM8994_LINEOUT1_DISCH |
2165 WM8994_LINEOUT2_DISCH,
2166 WM8994_LINEOUT1_DISCH |
2167 WM8994_LINEOUT2_DISCH);
2170 if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE)
2171 active_dereference(codec);
2173 /* MICBIAS into bypass mode on newer devices */
2174 switch (control->type) {
2175 case WM8958:
2176 case WM1811:
2177 snd_soc_update_bits(codec, WM8958_MICBIAS1,
2178 WM8958_MICB1_MODE,
2179 WM8958_MICB1_MODE);
2180 snd_soc_update_bits(codec, WM8958_MICBIAS2,
2181 WM8958_MICB2_MODE,
2182 WM8958_MICB2_MODE);
2183 break;
2184 default:
2185 break;
2187 break;
2189 case SND_SOC_BIAS_OFF:
2190 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
2191 wm8994->cur_fw = NULL;
2192 break;
2195 codec->dapm.bias_level = level;
2197 return 0;
2200 static int wm8994_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2202 struct snd_soc_codec *codec = dai->codec;
2203 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2204 struct wm8994 *control = wm8994->wm8994;
2205 int ms_reg;
2206 int aif1_reg;
2207 int ms = 0;
2208 int aif1 = 0;
2210 switch (dai->id) {
2211 case 1:
2212 ms_reg = WM8994_AIF1_MASTER_SLAVE;
2213 aif1_reg = WM8994_AIF1_CONTROL_1;
2214 break;
2215 case 2:
2216 ms_reg = WM8994_AIF2_MASTER_SLAVE;
2217 aif1_reg = WM8994_AIF2_CONTROL_1;
2218 break;
2219 default:
2220 return -EINVAL;
2223 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
2224 case SND_SOC_DAIFMT_CBS_CFS:
2225 break;
2226 case SND_SOC_DAIFMT_CBM_CFM:
2227 ms = WM8994_AIF1_MSTR;
2228 break;
2229 default:
2230 return -EINVAL;
2233 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2234 case SND_SOC_DAIFMT_DSP_B:
2235 aif1 |= WM8994_AIF1_LRCLK_INV;
2236 case SND_SOC_DAIFMT_DSP_A:
2237 aif1 |= 0x18;
2238 break;
2239 case SND_SOC_DAIFMT_I2S:
2240 aif1 |= 0x10;
2241 break;
2242 case SND_SOC_DAIFMT_RIGHT_J:
2243 break;
2244 case SND_SOC_DAIFMT_LEFT_J:
2245 aif1 |= 0x8;
2246 break;
2247 default:
2248 return -EINVAL;
2251 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2252 case SND_SOC_DAIFMT_DSP_A:
2253 case SND_SOC_DAIFMT_DSP_B:
2254 /* frame inversion not valid for DSP modes */
2255 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2256 case SND_SOC_DAIFMT_NB_NF:
2257 break;
2258 case SND_SOC_DAIFMT_IB_NF:
2259 aif1 |= WM8994_AIF1_BCLK_INV;
2260 break;
2261 default:
2262 return -EINVAL;
2264 break;
2266 case SND_SOC_DAIFMT_I2S:
2267 case SND_SOC_DAIFMT_RIGHT_J:
2268 case SND_SOC_DAIFMT_LEFT_J:
2269 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2270 case SND_SOC_DAIFMT_NB_NF:
2271 break;
2272 case SND_SOC_DAIFMT_IB_IF:
2273 aif1 |= WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV;
2274 break;
2275 case SND_SOC_DAIFMT_IB_NF:
2276 aif1 |= WM8994_AIF1_BCLK_INV;
2277 break;
2278 case SND_SOC_DAIFMT_NB_IF:
2279 aif1 |= WM8994_AIF1_LRCLK_INV;
2280 break;
2281 default:
2282 return -EINVAL;
2284 break;
2285 default:
2286 return -EINVAL;
2289 /* The AIF2 format configuration needs to be mirrored to AIF3
2290 * on WM8958 if it's in use so just do it all the time. */
2291 switch (control->type) {
2292 case WM1811:
2293 case WM8958:
2294 if (dai->id == 2)
2295 snd_soc_update_bits(codec, WM8958_AIF3_CONTROL_1,
2296 WM8994_AIF1_LRCLK_INV |
2297 WM8958_AIF3_FMT_MASK, aif1);
2298 break;
2300 default:
2301 break;
2304 snd_soc_update_bits(codec, aif1_reg,
2305 WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV |
2306 WM8994_AIF1_FMT_MASK,
2307 aif1);
2308 snd_soc_update_bits(codec, ms_reg, WM8994_AIF1_MSTR,
2309 ms);
2311 return 0;
2314 static struct {
2315 int val, rate;
2316 } srs[] = {
2317 { 0, 8000 },
2318 { 1, 11025 },
2319 { 2, 12000 },
2320 { 3, 16000 },
2321 { 4, 22050 },
2322 { 5, 24000 },
2323 { 6, 32000 },
2324 { 7, 44100 },
2325 { 8, 48000 },
2326 { 9, 88200 },
2327 { 10, 96000 },
2330 static int fs_ratios[] = {
2331 64, 128, 192, 256, 348, 512, 768, 1024, 1408, 1536
2334 static int bclk_divs[] = {
2335 10, 15, 20, 30, 40, 50, 60, 80, 110, 120, 160, 220, 240, 320, 440, 480,
2336 640, 880, 960, 1280, 1760, 1920
2339 static int wm8994_hw_params(struct snd_pcm_substream *substream,
2340 struct snd_pcm_hw_params *params,
2341 struct snd_soc_dai *dai)
2343 struct snd_soc_codec *codec = dai->codec;
2344 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2345 int aif1_reg;
2346 int aif2_reg;
2347 int bclk_reg;
2348 int lrclk_reg;
2349 int rate_reg;
2350 int aif1 = 0;
2351 int aif2 = 0;
2352 int bclk = 0;
2353 int lrclk = 0;
2354 int rate_val = 0;
2355 int id = dai->id - 1;
2357 int i, cur_val, best_val, bclk_rate, best;
2359 switch (dai->id) {
2360 case 1:
2361 aif1_reg = WM8994_AIF1_CONTROL_1;
2362 aif2_reg = WM8994_AIF1_CONTROL_2;
2363 bclk_reg = WM8994_AIF1_BCLK;
2364 rate_reg = WM8994_AIF1_RATE;
2365 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
2366 wm8994->lrclk_shared[0]) {
2367 lrclk_reg = WM8994_AIF1DAC_LRCLK;
2368 } else {
2369 lrclk_reg = WM8994_AIF1ADC_LRCLK;
2370 dev_dbg(codec->dev, "AIF1 using split LRCLK\n");
2372 break;
2373 case 2:
2374 aif1_reg = WM8994_AIF2_CONTROL_1;
2375 aif2_reg = WM8994_AIF2_CONTROL_2;
2376 bclk_reg = WM8994_AIF2_BCLK;
2377 rate_reg = WM8994_AIF2_RATE;
2378 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
2379 wm8994->lrclk_shared[1]) {
2380 lrclk_reg = WM8994_AIF2DAC_LRCLK;
2381 } else {
2382 lrclk_reg = WM8994_AIF2ADC_LRCLK;
2383 dev_dbg(codec->dev, "AIF2 using split LRCLK\n");
2385 break;
2386 default:
2387 return -EINVAL;
2390 bclk_rate = params_rate(params) * 2;
2391 switch (params_format(params)) {
2392 case SNDRV_PCM_FORMAT_S16_LE:
2393 bclk_rate *= 16;
2394 break;
2395 case SNDRV_PCM_FORMAT_S20_3LE:
2396 bclk_rate *= 20;
2397 aif1 |= 0x20;
2398 break;
2399 case SNDRV_PCM_FORMAT_S24_LE:
2400 bclk_rate *= 24;
2401 aif1 |= 0x40;
2402 break;
2403 case SNDRV_PCM_FORMAT_S32_LE:
2404 bclk_rate *= 32;
2405 aif1 |= 0x60;
2406 break;
2407 default:
2408 return -EINVAL;
2411 /* Try to find an appropriate sample rate; look for an exact match. */
2412 for (i = 0; i < ARRAY_SIZE(srs); i++)
2413 if (srs[i].rate == params_rate(params))
2414 break;
2415 if (i == ARRAY_SIZE(srs))
2416 return -EINVAL;
2417 rate_val |= srs[i].val << WM8994_AIF1_SR_SHIFT;
2419 dev_dbg(dai->dev, "Sample rate is %dHz\n", srs[i].rate);
2420 dev_dbg(dai->dev, "AIF%dCLK is %dHz, target BCLK %dHz\n",
2421 dai->id, wm8994->aifclk[id], bclk_rate);
2423 if (params_channels(params) == 1 &&
2424 (snd_soc_read(codec, aif1_reg) & 0x18) == 0x18)
2425 aif2 |= WM8994_AIF1_MONO;
2427 if (wm8994->aifclk[id] == 0) {
2428 dev_err(dai->dev, "AIF%dCLK not configured\n", dai->id);
2429 return -EINVAL;
2432 /* AIFCLK/fs ratio; look for a close match in either direction */
2433 best = 0;
2434 best_val = abs((fs_ratios[0] * params_rate(params))
2435 - wm8994->aifclk[id]);
2436 for (i = 1; i < ARRAY_SIZE(fs_ratios); i++) {
2437 cur_val = abs((fs_ratios[i] * params_rate(params))
2438 - wm8994->aifclk[id]);
2439 if (cur_val >= best_val)
2440 continue;
2441 best = i;
2442 best_val = cur_val;
2444 dev_dbg(dai->dev, "Selected AIF%dCLK/fs = %d\n",
2445 dai->id, fs_ratios[best]);
2446 rate_val |= best;
2448 /* We may not get quite the right frequency if using
2449 * approximate clocks so look for the closest match that is
2450 * higher than the target (we need to ensure that there enough
2451 * BCLKs to clock out the samples).
2453 best = 0;
2454 for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
2455 cur_val = (wm8994->aifclk[id] * 10 / bclk_divs[i]) - bclk_rate;
2456 if (cur_val < 0) /* BCLK table is sorted */
2457 break;
2458 best = i;
2460 bclk_rate = wm8994->aifclk[id] * 10 / bclk_divs[best];
2461 dev_dbg(dai->dev, "Using BCLK_DIV %d for actual BCLK %dHz\n",
2462 bclk_divs[best], bclk_rate);
2463 bclk |= best << WM8994_AIF1_BCLK_DIV_SHIFT;
2465 lrclk = bclk_rate / params_rate(params);
2466 if (!lrclk) {
2467 dev_err(dai->dev, "Unable to generate LRCLK from %dHz BCLK\n",
2468 bclk_rate);
2469 return -EINVAL;
2471 dev_dbg(dai->dev, "Using LRCLK rate %d for actual LRCLK %dHz\n",
2472 lrclk, bclk_rate / lrclk);
2474 snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
2475 snd_soc_update_bits(codec, aif2_reg, WM8994_AIF1_MONO, aif2);
2476 snd_soc_update_bits(codec, bclk_reg, WM8994_AIF1_BCLK_DIV_MASK, bclk);
2477 snd_soc_update_bits(codec, lrclk_reg, WM8994_AIF1DAC_RATE_MASK,
2478 lrclk);
2479 snd_soc_update_bits(codec, rate_reg, WM8994_AIF1_SR_MASK |
2480 WM8994_AIF1CLK_RATE_MASK, rate_val);
2482 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
2483 switch (dai->id) {
2484 case 1:
2485 wm8994->dac_rates[0] = params_rate(params);
2486 wm8994_set_retune_mobile(codec, 0);
2487 wm8994_set_retune_mobile(codec, 1);
2488 break;
2489 case 2:
2490 wm8994->dac_rates[1] = params_rate(params);
2491 wm8994_set_retune_mobile(codec, 2);
2492 break;
2496 return 0;
2499 static int wm8994_aif3_hw_params(struct snd_pcm_substream *substream,
2500 struct snd_pcm_hw_params *params,
2501 struct snd_soc_dai *dai)
2503 struct snd_soc_codec *codec = dai->codec;
2504 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2505 struct wm8994 *control = wm8994->wm8994;
2506 int aif1_reg;
2507 int aif1 = 0;
2509 switch (dai->id) {
2510 case 3:
2511 switch (control->type) {
2512 case WM1811:
2513 case WM8958:
2514 aif1_reg = WM8958_AIF3_CONTROL_1;
2515 break;
2516 default:
2517 return 0;
2519 default:
2520 return 0;
2523 switch (params_format(params)) {
2524 case SNDRV_PCM_FORMAT_S16_LE:
2525 break;
2526 case SNDRV_PCM_FORMAT_S20_3LE:
2527 aif1 |= 0x20;
2528 break;
2529 case SNDRV_PCM_FORMAT_S24_LE:
2530 aif1 |= 0x40;
2531 break;
2532 case SNDRV_PCM_FORMAT_S32_LE:
2533 aif1 |= 0x60;
2534 break;
2535 default:
2536 return -EINVAL;
2539 return snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
2542 static void wm8994_aif_shutdown(struct snd_pcm_substream *substream,
2543 struct snd_soc_dai *dai)
2545 struct snd_soc_codec *codec = dai->codec;
2546 int rate_reg = 0;
2548 switch (dai->id) {
2549 case 1:
2550 rate_reg = WM8994_AIF1_RATE;
2551 break;
2552 case 2:
2553 rate_reg = WM8994_AIF2_RATE;
2554 break;
2555 default:
2556 break;
2559 /* If the DAI is idle then configure the divider tree for the
2560 * lowest output rate to save a little power if the clock is
2561 * still active (eg, because it is system clock).
2563 if (rate_reg && !dai->playback_active && !dai->capture_active)
2564 snd_soc_update_bits(codec, rate_reg,
2565 WM8994_AIF1_SR_MASK |
2566 WM8994_AIF1CLK_RATE_MASK, 0x9);
2569 static int wm8994_aif_mute(struct snd_soc_dai *codec_dai, int mute)
2571 struct snd_soc_codec *codec = codec_dai->codec;
2572 int mute_reg;
2573 int reg;
2575 switch (codec_dai->id) {
2576 case 1:
2577 mute_reg = WM8994_AIF1_DAC1_FILTERS_1;
2578 break;
2579 case 2:
2580 mute_reg = WM8994_AIF2_DAC_FILTERS_1;
2581 break;
2582 default:
2583 return -EINVAL;
2586 if (mute)
2587 reg = WM8994_AIF1DAC1_MUTE;
2588 else
2589 reg = 0;
2591 snd_soc_update_bits(codec, mute_reg, WM8994_AIF1DAC1_MUTE, reg);
2593 return 0;
2596 static int wm8994_set_tristate(struct snd_soc_dai *codec_dai, int tristate)
2598 struct snd_soc_codec *codec = codec_dai->codec;
2599 int reg, val, mask;
2601 switch (codec_dai->id) {
2602 case 1:
2603 reg = WM8994_AIF1_MASTER_SLAVE;
2604 mask = WM8994_AIF1_TRI;
2605 break;
2606 case 2:
2607 reg = WM8994_AIF2_MASTER_SLAVE;
2608 mask = WM8994_AIF2_TRI;
2609 break;
2610 case 3:
2611 reg = WM8994_POWER_MANAGEMENT_6;
2612 mask = WM8994_AIF3_TRI;
2613 break;
2614 default:
2615 return -EINVAL;
2618 if (tristate)
2619 val = mask;
2620 else
2621 val = 0;
2623 return snd_soc_update_bits(codec, reg, mask, val);
2626 static int wm8994_aif2_probe(struct snd_soc_dai *dai)
2628 struct snd_soc_codec *codec = dai->codec;
2630 /* Disable the pulls on the AIF if we're using it to save power. */
2631 snd_soc_update_bits(codec, WM8994_GPIO_3,
2632 WM8994_GPN_PU | WM8994_GPN_PD, 0);
2633 snd_soc_update_bits(codec, WM8994_GPIO_4,
2634 WM8994_GPN_PU | WM8994_GPN_PD, 0);
2635 snd_soc_update_bits(codec, WM8994_GPIO_5,
2636 WM8994_GPN_PU | WM8994_GPN_PD, 0);
2638 return 0;
2641 #define WM8994_RATES SNDRV_PCM_RATE_8000_96000
2643 #define WM8994_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
2644 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
2646 static const struct snd_soc_dai_ops wm8994_aif1_dai_ops = {
2647 .set_sysclk = wm8994_set_dai_sysclk,
2648 .set_fmt = wm8994_set_dai_fmt,
2649 .hw_params = wm8994_hw_params,
2650 .shutdown = wm8994_aif_shutdown,
2651 .digital_mute = wm8994_aif_mute,
2652 .set_pll = wm8994_set_fll,
2653 .set_tristate = wm8994_set_tristate,
2656 static const struct snd_soc_dai_ops wm8994_aif2_dai_ops = {
2657 .set_sysclk = wm8994_set_dai_sysclk,
2658 .set_fmt = wm8994_set_dai_fmt,
2659 .hw_params = wm8994_hw_params,
2660 .shutdown = wm8994_aif_shutdown,
2661 .digital_mute = wm8994_aif_mute,
2662 .set_pll = wm8994_set_fll,
2663 .set_tristate = wm8994_set_tristate,
2666 static const struct snd_soc_dai_ops wm8994_aif3_dai_ops = {
2667 .hw_params = wm8994_aif3_hw_params,
2668 .set_tristate = wm8994_set_tristate,
2671 static struct snd_soc_dai_driver wm8994_dai[] = {
2673 .name = "wm8994-aif1",
2674 .id = 1,
2675 .playback = {
2676 .stream_name = "AIF1 Playback",
2677 .channels_min = 1,
2678 .channels_max = 2,
2679 .rates = WM8994_RATES,
2680 .formats = WM8994_FORMATS,
2681 .sig_bits = 24,
2683 .capture = {
2684 .stream_name = "AIF1 Capture",
2685 .channels_min = 1,
2686 .channels_max = 2,
2687 .rates = WM8994_RATES,
2688 .formats = WM8994_FORMATS,
2689 .sig_bits = 24,
2691 .ops = &wm8994_aif1_dai_ops,
2694 .name = "wm8994-aif2",
2695 .id = 2,
2696 .playback = {
2697 .stream_name = "AIF2 Playback",
2698 .channels_min = 1,
2699 .channels_max = 2,
2700 .rates = WM8994_RATES,
2701 .formats = WM8994_FORMATS,
2702 .sig_bits = 24,
2704 .capture = {
2705 .stream_name = "AIF2 Capture",
2706 .channels_min = 1,
2707 .channels_max = 2,
2708 .rates = WM8994_RATES,
2709 .formats = WM8994_FORMATS,
2710 .sig_bits = 24,
2712 .probe = wm8994_aif2_probe,
2713 .ops = &wm8994_aif2_dai_ops,
2716 .name = "wm8994-aif3",
2717 .id = 3,
2718 .playback = {
2719 .stream_name = "AIF3 Playback",
2720 .channels_min = 1,
2721 .channels_max = 2,
2722 .rates = WM8994_RATES,
2723 .formats = WM8994_FORMATS,
2724 .sig_bits = 24,
2726 .capture = {
2727 .stream_name = "AIF3 Capture",
2728 .channels_min = 1,
2729 .channels_max = 2,
2730 .rates = WM8994_RATES,
2731 .formats = WM8994_FORMATS,
2732 .sig_bits = 24,
2734 .ops = &wm8994_aif3_dai_ops,
2738 #ifdef CONFIG_PM
2739 static int wm8994_codec_suspend(struct snd_soc_codec *codec)
2741 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2742 struct wm8994 *control = wm8994->wm8994;
2743 int i, ret;
2745 switch (control->type) {
2746 case WM8994:
2747 snd_soc_update_bits(codec, WM8994_MICBIAS, WM8994_MICD_ENA, 0);
2748 break;
2749 case WM1811:
2750 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
2751 WM1811_JACKDET_MODE_MASK, 0);
2752 /* Fall through */
2753 case WM8958:
2754 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
2755 WM8958_MICD_ENA, 0);
2756 break;
2759 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
2760 memcpy(&wm8994->fll_suspend[i], &wm8994->fll[i],
2761 sizeof(struct wm8994_fll_config));
2762 ret = _wm8994_set_fll(codec, i + 1, 0, 0, 0);
2763 if (ret < 0)
2764 dev_warn(codec->dev, "Failed to stop FLL%d: %d\n",
2765 i + 1, ret);
2768 wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);
2770 return 0;
2773 static int wm8994_codec_resume(struct snd_soc_codec *codec)
2775 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2776 struct wm8994 *control = wm8994->wm8994;
2777 int i, ret;
2778 unsigned int val, mask;
2780 if (wm8994->revision < 4) {
2781 /* force a HW read */
2782 ret = regmap_read(control->regmap,
2783 WM8994_POWER_MANAGEMENT_5, &val);
2785 /* modify the cache only */
2786 codec->cache_only = 1;
2787 mask = WM8994_DAC1R_ENA | WM8994_DAC1L_ENA |
2788 WM8994_DAC2R_ENA | WM8994_DAC2L_ENA;
2789 val &= mask;
2790 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
2791 mask, val);
2792 codec->cache_only = 0;
2795 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
2796 if (!wm8994->fll_suspend[i].out)
2797 continue;
2799 ret = _wm8994_set_fll(codec, i + 1,
2800 wm8994->fll_suspend[i].src,
2801 wm8994->fll_suspend[i].in,
2802 wm8994->fll_suspend[i].out);
2803 if (ret < 0)
2804 dev_warn(codec->dev, "Failed to restore FLL%d: %d\n",
2805 i + 1, ret);
2808 switch (control->type) {
2809 case WM8994:
2810 if (wm8994->micdet[0].jack || wm8994->micdet[1].jack)
2811 snd_soc_update_bits(codec, WM8994_MICBIAS,
2812 WM8994_MICD_ENA, WM8994_MICD_ENA);
2813 break;
2814 case WM1811:
2815 if (wm8994->jackdet && wm8994->jack_cb) {
2816 /* Restart from idle */
2817 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
2818 WM1811_JACKDET_MODE_MASK,
2819 WM1811_JACKDET_MODE_JACK);
2820 break;
2822 case WM8958:
2823 if (wm8994->jack_cb)
2824 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
2825 WM8958_MICD_ENA, WM8958_MICD_ENA);
2826 break;
2829 return 0;
2831 #else
2832 #define wm8994_codec_suspend NULL
2833 #define wm8994_codec_resume NULL
2834 #endif
2836 static void wm8994_handle_retune_mobile_pdata(struct wm8994_priv *wm8994)
2838 struct snd_soc_codec *codec = wm8994->codec;
2839 struct wm8994_pdata *pdata = wm8994->pdata;
2840 struct snd_kcontrol_new controls[] = {
2841 SOC_ENUM_EXT("AIF1.1 EQ Mode",
2842 wm8994->retune_mobile_enum,
2843 wm8994_get_retune_mobile_enum,
2844 wm8994_put_retune_mobile_enum),
2845 SOC_ENUM_EXT("AIF1.2 EQ Mode",
2846 wm8994->retune_mobile_enum,
2847 wm8994_get_retune_mobile_enum,
2848 wm8994_put_retune_mobile_enum),
2849 SOC_ENUM_EXT("AIF2 EQ Mode",
2850 wm8994->retune_mobile_enum,
2851 wm8994_get_retune_mobile_enum,
2852 wm8994_put_retune_mobile_enum),
2854 int ret, i, j;
2855 const char **t;
2857 /* We need an array of texts for the enum API but the number
2858 * of texts is likely to be less than the number of
2859 * configurations due to the sample rate dependency of the
2860 * configurations. */
2861 wm8994->num_retune_mobile_texts = 0;
2862 wm8994->retune_mobile_texts = NULL;
2863 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
2864 for (j = 0; j < wm8994->num_retune_mobile_texts; j++) {
2865 if (strcmp(pdata->retune_mobile_cfgs[i].name,
2866 wm8994->retune_mobile_texts[j]) == 0)
2867 break;
2870 if (j != wm8994->num_retune_mobile_texts)
2871 continue;
2873 /* Expand the array... */
2874 t = krealloc(wm8994->retune_mobile_texts,
2875 sizeof(char *) *
2876 (wm8994->num_retune_mobile_texts + 1),
2877 GFP_KERNEL);
2878 if (t == NULL)
2879 continue;
2881 /* ...store the new entry... */
2882 t[wm8994->num_retune_mobile_texts] =
2883 pdata->retune_mobile_cfgs[i].name;
2885 /* ...and remember the new version. */
2886 wm8994->num_retune_mobile_texts++;
2887 wm8994->retune_mobile_texts = t;
2890 dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
2891 wm8994->num_retune_mobile_texts);
2893 wm8994->retune_mobile_enum.max = wm8994->num_retune_mobile_texts;
2894 wm8994->retune_mobile_enum.texts = wm8994->retune_mobile_texts;
2896 ret = snd_soc_add_codec_controls(wm8994->codec, controls,
2897 ARRAY_SIZE(controls));
2898 if (ret != 0)
2899 dev_err(wm8994->codec->dev,
2900 "Failed to add ReTune Mobile controls: %d\n", ret);
2903 static void wm8994_handle_pdata(struct wm8994_priv *wm8994)
2905 struct snd_soc_codec *codec = wm8994->codec;
2906 struct wm8994_pdata *pdata = wm8994->pdata;
2907 int ret, i;
2909 if (!pdata)
2910 return;
2912 wm_hubs_handle_analogue_pdata(codec, pdata->lineout1_diff,
2913 pdata->lineout2_diff,
2914 pdata->lineout1fb,
2915 pdata->lineout2fb,
2916 pdata->jd_scthr,
2917 pdata->jd_thr,
2918 pdata->micbias1_lvl,
2919 pdata->micbias2_lvl);
2921 dev_dbg(codec->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
2923 if (pdata->num_drc_cfgs) {
2924 struct snd_kcontrol_new controls[] = {
2925 SOC_ENUM_EXT("AIF1DRC1 Mode", wm8994->drc_enum,
2926 wm8994_get_drc_enum, wm8994_put_drc_enum),
2927 SOC_ENUM_EXT("AIF1DRC2 Mode", wm8994->drc_enum,
2928 wm8994_get_drc_enum, wm8994_put_drc_enum),
2929 SOC_ENUM_EXT("AIF2DRC Mode", wm8994->drc_enum,
2930 wm8994_get_drc_enum, wm8994_put_drc_enum),
2933 /* We need an array of texts for the enum API */
2934 wm8994->drc_texts = devm_kzalloc(wm8994->codec->dev,
2935 sizeof(char *) * pdata->num_drc_cfgs, GFP_KERNEL);
2936 if (!wm8994->drc_texts) {
2937 dev_err(wm8994->codec->dev,
2938 "Failed to allocate %d DRC config texts\n",
2939 pdata->num_drc_cfgs);
2940 return;
2943 for (i = 0; i < pdata->num_drc_cfgs; i++)
2944 wm8994->drc_texts[i] = pdata->drc_cfgs[i].name;
2946 wm8994->drc_enum.max = pdata->num_drc_cfgs;
2947 wm8994->drc_enum.texts = wm8994->drc_texts;
2949 ret = snd_soc_add_codec_controls(wm8994->codec, controls,
2950 ARRAY_SIZE(controls));
2951 if (ret != 0)
2952 dev_err(wm8994->codec->dev,
2953 "Failed to add DRC mode controls: %d\n", ret);
2955 for (i = 0; i < WM8994_NUM_DRC; i++)
2956 wm8994_set_drc(codec, i);
2959 dev_dbg(codec->dev, "%d ReTune Mobile configurations\n",
2960 pdata->num_retune_mobile_cfgs);
2962 if (pdata->num_retune_mobile_cfgs)
2963 wm8994_handle_retune_mobile_pdata(wm8994);
2964 else
2965 snd_soc_add_codec_controls(wm8994->codec, wm8994_eq_controls,
2966 ARRAY_SIZE(wm8994_eq_controls));
2968 for (i = 0; i < ARRAY_SIZE(pdata->micbias); i++) {
2969 if (pdata->micbias[i]) {
2970 snd_soc_write(codec, WM8958_MICBIAS1 + i,
2971 pdata->micbias[i] & 0xffff);
2977 * wm8994_mic_detect - Enable microphone detection via the WM8994 IRQ
2979 * @codec: WM8994 codec
2980 * @jack: jack to report detection events on
2981 * @micbias: microphone bias to detect on
2983 * Enable microphone detection via IRQ on the WM8994. If GPIOs are
2984 * being used to bring out signals to the processor then only platform
2985 * data configuration is needed for WM8994 and processor GPIOs should
2986 * be configured using snd_soc_jack_add_gpios() instead.
2988 * Configuration of detection levels is available via the micbias1_lvl
2989 * and micbias2_lvl platform data members.
2991 int wm8994_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
2992 int micbias)
2994 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2995 struct wm8994_micdet *micdet;
2996 struct wm8994 *control = wm8994->wm8994;
2997 int reg, ret;
2999 if (control->type != WM8994) {
3000 dev_warn(codec->dev, "Not a WM8994\n");
3001 return -EINVAL;
3004 switch (micbias) {
3005 case 1:
3006 micdet = &wm8994->micdet[0];
3007 if (jack)
3008 ret = snd_soc_dapm_force_enable_pin(&codec->dapm,
3009 "MICBIAS1");
3010 else
3011 ret = snd_soc_dapm_disable_pin(&codec->dapm,
3012 "MICBIAS1");
3013 break;
3014 case 2:
3015 micdet = &wm8994->micdet[1];
3016 if (jack)
3017 ret = snd_soc_dapm_force_enable_pin(&codec->dapm,
3018 "MICBIAS1");
3019 else
3020 ret = snd_soc_dapm_disable_pin(&codec->dapm,
3021 "MICBIAS1");
3022 break;
3023 default:
3024 dev_warn(codec->dev, "Invalid MICBIAS %d\n", micbias);
3025 return -EINVAL;
3028 if (ret != 0)
3029 dev_warn(codec->dev, "Failed to configure MICBIAS%d: %d\n",
3030 micbias, ret);
3032 dev_dbg(codec->dev, "Configuring microphone detection on %d %p\n",
3033 micbias, jack);
3035 /* Store the configuration */
3036 micdet->jack = jack;
3037 micdet->detecting = true;
3039 /* If either of the jacks is set up then enable detection */
3040 if (wm8994->micdet[0].jack || wm8994->micdet[1].jack)
3041 reg = WM8994_MICD_ENA;
3042 else
3043 reg = 0;
3045 snd_soc_update_bits(codec, WM8994_MICBIAS, WM8994_MICD_ENA, reg);
3047 snd_soc_dapm_sync(&codec->dapm);
3049 return 0;
3051 EXPORT_SYMBOL_GPL(wm8994_mic_detect);
3053 static irqreturn_t wm8994_mic_irq(int irq, void *data)
3055 struct wm8994_priv *priv = data;
3056 struct snd_soc_codec *codec = priv->codec;
3057 int reg;
3058 int report;
3060 #ifndef CONFIG_SND_SOC_WM8994_MODULE
3061 trace_snd_soc_jack_irq(dev_name(codec->dev));
3062 #endif
3064 reg = snd_soc_read(codec, WM8994_INTERRUPT_RAW_STATUS_2);
3065 if (reg < 0) {
3066 dev_err(codec->dev, "Failed to read microphone status: %d\n",
3067 reg);
3068 return IRQ_HANDLED;
3071 dev_dbg(codec->dev, "Microphone status: %x\n", reg);
3073 report = 0;
3074 if (reg & WM8994_MIC1_DET_STS) {
3075 if (priv->micdet[0].detecting)
3076 report = SND_JACK_HEADSET;
3078 if (reg & WM8994_MIC1_SHRT_STS) {
3079 if (priv->micdet[0].detecting)
3080 report = SND_JACK_HEADPHONE;
3081 else
3082 report |= SND_JACK_BTN_0;
3084 if (report)
3085 priv->micdet[0].detecting = false;
3086 else
3087 priv->micdet[0].detecting = true;
3089 snd_soc_jack_report(priv->micdet[0].jack, report,
3090 SND_JACK_HEADSET | SND_JACK_BTN_0);
3092 report = 0;
3093 if (reg & WM8994_MIC2_DET_STS) {
3094 if (priv->micdet[1].detecting)
3095 report = SND_JACK_HEADSET;
3097 if (reg & WM8994_MIC2_SHRT_STS) {
3098 if (priv->micdet[1].detecting)
3099 report = SND_JACK_HEADPHONE;
3100 else
3101 report |= SND_JACK_BTN_0;
3103 if (report)
3104 priv->micdet[1].detecting = false;
3105 else
3106 priv->micdet[1].detecting = true;
3108 snd_soc_jack_report(priv->micdet[1].jack, report,
3109 SND_JACK_HEADSET | SND_JACK_BTN_0);
3111 return IRQ_HANDLED;
3114 /* Default microphone detection handler for WM8958 - the user can
3115 * override this if they wish.
3117 static void wm8958_default_micdet(u16 status, void *data)
3119 struct snd_soc_codec *codec = data;
3120 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
3121 int report;
3123 dev_dbg(codec->dev, "MICDET %x\n", status);
3125 /* Either nothing present or just starting detection */
3126 if (!(status & WM8958_MICD_STS)) {
3127 if (!wm8994->jackdet) {
3128 /* If nothing present then clear our statuses */
3129 dev_dbg(codec->dev, "Detected open circuit\n");
3130 wm8994->jack_mic = false;
3131 wm8994->mic_detecting = true;
3133 wm8958_micd_set_rate(codec);
3135 snd_soc_jack_report(wm8994->micdet[0].jack, 0,
3136 wm8994->btn_mask |
3137 SND_JACK_HEADSET);
3139 return;
3142 /* If the measurement is showing a high impedence we've got a
3143 * microphone.
3145 if (wm8994->mic_detecting && (status & 0x600)) {
3146 dev_dbg(codec->dev, "Detected microphone\n");
3148 wm8994->mic_detecting = false;
3149 wm8994->jack_mic = true;
3151 wm8958_micd_set_rate(codec);
3153 snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADSET,
3154 SND_JACK_HEADSET);
3158 if (wm8994->mic_detecting && status & 0xfc) {
3159 dev_dbg(codec->dev, "Detected headphone\n");
3160 wm8994->mic_detecting = false;
3162 wm8958_micd_set_rate(codec);
3164 snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADPHONE,
3165 SND_JACK_HEADSET);
3167 /* If we have jackdet that will detect removal */
3168 if (wm8994->jackdet) {
3169 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3170 WM8958_MICD_ENA, 0);
3172 if (wm8994->pdata->jd_ext_cap) {
3173 mutex_lock(&codec->mutex);
3174 snd_soc_dapm_disable_pin(&codec->dapm,
3175 "MICBIAS2");
3176 snd_soc_dapm_sync(&codec->dapm);
3177 mutex_unlock(&codec->mutex);
3180 wm1811_jackdet_set_mode(codec,
3181 WM1811_JACKDET_MODE_JACK);
3185 /* Report short circuit as a button */
3186 if (wm8994->jack_mic) {
3187 report = 0;
3188 if (status & 0x4)
3189 report |= SND_JACK_BTN_0;
3191 if (status & 0x8)
3192 report |= SND_JACK_BTN_1;
3194 if (status & 0x10)
3195 report |= SND_JACK_BTN_2;
3197 if (status & 0x20)
3198 report |= SND_JACK_BTN_3;
3200 if (status & 0x40)
3201 report |= SND_JACK_BTN_4;
3203 if (status & 0x80)
3204 report |= SND_JACK_BTN_5;
3206 snd_soc_jack_report(wm8994->micdet[0].jack, report,
3207 wm8994->btn_mask);
3211 static irqreturn_t wm1811_jackdet_irq(int irq, void *data)
3213 struct wm8994_priv *wm8994 = data;
3214 struct snd_soc_codec *codec = wm8994->codec;
3215 int reg;
3217 mutex_lock(&wm8994->accdet_lock);
3219 reg = snd_soc_read(codec, WM1811_JACKDET_CTRL);
3220 if (reg < 0) {
3221 dev_err(codec->dev, "Failed to read jack status: %d\n", reg);
3222 mutex_unlock(&wm8994->accdet_lock);
3223 return IRQ_NONE;
3226 dev_dbg(codec->dev, "JACKDET %x\n", reg);
3228 if (reg & WM1811_JACKDET_LVL) {
3229 dev_dbg(codec->dev, "Jack detected\n");
3231 snd_soc_jack_report(wm8994->micdet[0].jack,
3232 SND_JACK_MECHANICAL, SND_JACK_MECHANICAL);
3234 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3235 WM8958_MICB2_DISCH, 0);
3237 /* Disable debounce while inserted */
3238 snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
3239 WM1811_JACKDET_DB, 0);
3242 * Start off measument of microphone impedence to find
3243 * out what's actually there.
3245 wm8994->mic_detecting = true;
3246 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_MIC);
3248 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3249 WM8958_MICD_ENA, WM8958_MICD_ENA);
3251 /* If required for an external cap force MICBIAS on */
3252 if (wm8994->pdata->jd_ext_cap) {
3253 mutex_lock(&codec->mutex);
3254 snd_soc_dapm_force_enable_pin(&codec->dapm,
3255 "MICBIAS2");
3256 snd_soc_dapm_sync(&codec->dapm);
3257 mutex_unlock(&codec->mutex);
3259 } else {
3260 dev_dbg(codec->dev, "Jack not detected\n");
3262 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3263 WM8958_MICB2_DISCH, WM8958_MICB2_DISCH);
3265 if (wm8994->pdata->jd_ext_cap) {
3266 mutex_lock(&codec->mutex);
3267 snd_soc_dapm_disable_pin(&codec->dapm, "MICBIAS2");
3268 snd_soc_dapm_sync(&codec->dapm);
3269 mutex_unlock(&codec->mutex);
3272 snd_soc_jack_report(wm8994->micdet[0].jack, 0,
3273 SND_JACK_MECHANICAL | SND_JACK_HEADSET |
3274 wm8994->btn_mask);
3276 /* Enable debounce while removed */
3277 snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
3278 WM1811_JACKDET_DB, WM1811_JACKDET_DB);
3280 wm8994->mic_detecting = false;
3281 wm8994->jack_mic = false;
3282 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3283 WM8958_MICD_ENA, 0);
3284 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_JACK);
3287 mutex_unlock(&wm8994->accdet_lock);
3289 return IRQ_HANDLED;
3293 * wm8958_mic_detect - Enable microphone detection via the WM8958 IRQ
3295 * @codec: WM8958 codec
3296 * @jack: jack to report detection events on
3298 * Enable microphone detection functionality for the WM8958. By
3299 * default simple detection which supports the detection of up to 6
3300 * buttons plus video and microphone functionality is supported.
3302 * The WM8958 has an advanced jack detection facility which is able to
3303 * support complex accessory detection, especially when used in
3304 * conjunction with external circuitry. In order to provide maximum
3305 * flexiblity a callback is provided which allows a completely custom
3306 * detection algorithm.
3308 int wm8958_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
3309 wm8958_micdet_cb cb, void *cb_data)
3311 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
3312 struct wm8994 *control = wm8994->wm8994;
3313 u16 micd_lvl_sel;
3315 switch (control->type) {
3316 case WM1811:
3317 case WM8958:
3318 break;
3319 default:
3320 return -EINVAL;
3323 if (jack) {
3324 if (!cb) {
3325 dev_dbg(codec->dev, "Using default micdet callback\n");
3326 cb = wm8958_default_micdet;
3327 cb_data = codec;
3330 snd_soc_dapm_force_enable_pin(&codec->dapm, "CLK_SYS");
3331 snd_soc_dapm_sync(&codec->dapm);
3333 wm8994->micdet[0].jack = jack;
3334 wm8994->jack_cb = cb;
3335 wm8994->jack_cb_data = cb_data;
3337 wm8994->mic_detecting = true;
3338 wm8994->jack_mic = false;
3340 wm8958_micd_set_rate(codec);
3342 /* Detect microphones and short circuits by default */
3343 if (wm8994->pdata->micd_lvl_sel)
3344 micd_lvl_sel = wm8994->pdata->micd_lvl_sel;
3345 else
3346 micd_lvl_sel = 0x41;
3348 wm8994->btn_mask = SND_JACK_BTN_0 | SND_JACK_BTN_1 |
3349 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
3350 SND_JACK_BTN_4 | SND_JACK_BTN_5;
3352 snd_soc_update_bits(codec, WM8958_MIC_DETECT_2,
3353 WM8958_MICD_LVL_SEL_MASK, micd_lvl_sel);
3355 WARN_ON(codec->dapm.bias_level > SND_SOC_BIAS_STANDBY);
3358 * If we can use jack detection start off with that,
3359 * otherwise jump straight to microphone detection.
3361 if (wm8994->jackdet) {
3362 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3363 WM8958_MICB2_DISCH,
3364 WM8958_MICB2_DISCH);
3365 snd_soc_update_bits(codec, WM8994_LDO_1,
3366 WM8994_LDO1_DISCH, 0);
3367 wm1811_jackdet_set_mode(codec,
3368 WM1811_JACKDET_MODE_JACK);
3369 } else {
3370 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3371 WM8958_MICD_ENA, WM8958_MICD_ENA);
3374 } else {
3375 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3376 WM8958_MICD_ENA, 0);
3377 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_NONE);
3378 snd_soc_dapm_disable_pin(&codec->dapm, "CLK_SYS");
3379 snd_soc_dapm_sync(&codec->dapm);
3382 return 0;
3384 EXPORT_SYMBOL_GPL(wm8958_mic_detect);
3386 static irqreturn_t wm8958_mic_irq(int irq, void *data)
3388 struct wm8994_priv *wm8994 = data;
3389 struct snd_soc_codec *codec = wm8994->codec;
3390 int reg, count;
3392 mutex_lock(&wm8994->accdet_lock);
3395 * Jack detection may have detected a removal simulataneously
3396 * with an update of the MICDET status; if so it will have
3397 * stopped detection and we can ignore this interrupt.
3399 if (!(snd_soc_read(codec, WM8958_MIC_DETECT_1) & WM8958_MICD_ENA)) {
3400 mutex_unlock(&wm8994->accdet_lock);
3401 return IRQ_HANDLED;
3404 /* We may occasionally read a detection without an impedence
3405 * range being provided - if that happens loop again.
3407 count = 10;
3408 do {
3409 reg = snd_soc_read(codec, WM8958_MIC_DETECT_3);
3410 if (reg < 0) {
3411 mutex_unlock(&wm8994->accdet_lock);
3412 dev_err(codec->dev,
3413 "Failed to read mic detect status: %d\n",
3414 reg);
3415 return IRQ_NONE;
3418 if (!(reg & WM8958_MICD_VALID)) {
3419 dev_dbg(codec->dev, "Mic detect data not valid\n");
3420 goto out;
3423 if (!(reg & WM8958_MICD_STS) || (reg & WM8958_MICD_LVL_MASK))
3424 break;
3426 msleep(1);
3427 } while (count--);
3429 if (count == 0)
3430 dev_warn(codec->dev, "No impedence range reported for jack\n");
3432 #ifndef CONFIG_SND_SOC_WM8994_MODULE
3433 trace_snd_soc_jack_irq(dev_name(codec->dev));
3434 #endif
3436 if (wm8994->jack_cb)
3437 wm8994->jack_cb(reg, wm8994->jack_cb_data);
3438 else
3439 dev_warn(codec->dev, "Accessory detection with no callback\n");
3441 out:
3442 mutex_unlock(&wm8994->accdet_lock);
3444 return IRQ_HANDLED;
3447 static irqreturn_t wm8994_fifo_error(int irq, void *data)
3449 struct snd_soc_codec *codec = data;
3451 dev_err(codec->dev, "FIFO error\n");
3453 return IRQ_HANDLED;
3456 static irqreturn_t wm8994_temp_warn(int irq, void *data)
3458 struct snd_soc_codec *codec = data;
3460 dev_err(codec->dev, "Thermal warning\n");
3462 return IRQ_HANDLED;
3465 static irqreturn_t wm8994_temp_shut(int irq, void *data)
3467 struct snd_soc_codec *codec = data;
3469 dev_crit(codec->dev, "Thermal shutdown\n");
3471 return IRQ_HANDLED;
3474 static int wm8994_codec_probe(struct snd_soc_codec *codec)
3476 struct wm8994 *control = dev_get_drvdata(codec->dev->parent);
3477 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
3478 struct snd_soc_dapm_context *dapm = &codec->dapm;
3479 unsigned int reg;
3480 int ret, i;
3482 wm8994->codec = codec;
3483 codec->control_data = control->regmap;
3485 snd_soc_codec_set_cache_io(codec, 16, 16, SND_SOC_REGMAP);
3487 wm8994->codec = codec;
3489 mutex_init(&wm8994->accdet_lock);
3491 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
3492 init_completion(&wm8994->fll_locked[i]);
3494 if (wm8994->pdata && wm8994->pdata->micdet_irq)
3495 wm8994->micdet_irq = wm8994->pdata->micdet_irq;
3496 else if (wm8994->pdata && wm8994->pdata->irq_base)
3497 wm8994->micdet_irq = wm8994->pdata->irq_base +
3498 WM8994_IRQ_MIC1_DET;
3500 pm_runtime_enable(codec->dev);
3501 pm_runtime_idle(codec->dev);
3503 /* By default use idle_bias_off, will override for WM8994 */
3504 codec->dapm.idle_bias_off = 1;
3506 /* Set revision-specific configuration */
3507 wm8994->revision = snd_soc_read(codec, WM8994_CHIP_REVISION);
3508 switch (control->type) {
3509 case WM8994:
3510 /* Single ended line outputs should have VMID on. */
3511 if (!wm8994->pdata->lineout1_diff ||
3512 !wm8994->pdata->lineout2_diff)
3513 codec->dapm.idle_bias_off = 0;
3515 switch (wm8994->revision) {
3516 case 2:
3517 case 3:
3518 wm8994->hubs.dcs_codes_l = -5;
3519 wm8994->hubs.dcs_codes_r = -5;
3520 wm8994->hubs.hp_startup_mode = 1;
3521 wm8994->hubs.dcs_readback_mode = 1;
3522 wm8994->hubs.series_startup = 1;
3523 break;
3524 default:
3525 wm8994->hubs.dcs_readback_mode = 2;
3526 break;
3528 break;
3530 case WM8958:
3531 wm8994->hubs.dcs_readback_mode = 1;
3532 wm8994->hubs.hp_startup_mode = 1;
3533 break;
3535 case WM1811:
3536 wm8994->hubs.dcs_readback_mode = 2;
3537 wm8994->hubs.no_series_update = 1;
3538 wm8994->hubs.hp_startup_mode = 1;
3539 wm8994->hubs.no_cache_class_w = true;
3541 switch (wm8994->revision) {
3542 case 0:
3543 case 1:
3544 case 2:
3545 case 3:
3546 wm8994->hubs.dcs_codes_l = -9;
3547 wm8994->hubs.dcs_codes_r = -5;
3548 break;
3549 default:
3550 break;
3553 snd_soc_update_bits(codec, WM8994_ANALOGUE_HP_1,
3554 WM1811_HPOUT1_ATTN, WM1811_HPOUT1_ATTN);
3555 break;
3557 default:
3558 break;
3561 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR,
3562 wm8994_fifo_error, "FIFO error", codec);
3563 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN,
3564 wm8994_temp_warn, "Thermal warning", codec);
3565 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT,
3566 wm8994_temp_shut, "Thermal shutdown", codec);
3568 ret = wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
3569 wm_hubs_dcs_done, "DC servo done",
3570 &wm8994->hubs);
3571 if (ret == 0)
3572 wm8994->hubs.dcs_done_irq = true;
3574 switch (control->type) {
3575 case WM8994:
3576 if (wm8994->micdet_irq) {
3577 ret = request_threaded_irq(wm8994->micdet_irq, NULL,
3578 wm8994_mic_irq,
3579 IRQF_TRIGGER_RISING,
3580 "Mic1 detect",
3581 wm8994);
3582 if (ret != 0)
3583 dev_warn(codec->dev,
3584 "Failed to request Mic1 detect IRQ: %d\n",
3585 ret);
3588 ret = wm8994_request_irq(wm8994->wm8994,
3589 WM8994_IRQ_MIC1_SHRT,
3590 wm8994_mic_irq, "Mic 1 short",
3591 wm8994);
3592 if (ret != 0)
3593 dev_warn(codec->dev,
3594 "Failed to request Mic1 short IRQ: %d\n",
3595 ret);
3597 ret = wm8994_request_irq(wm8994->wm8994,
3598 WM8994_IRQ_MIC2_DET,
3599 wm8994_mic_irq, "Mic 2 detect",
3600 wm8994);
3601 if (ret != 0)
3602 dev_warn(codec->dev,
3603 "Failed to request Mic2 detect IRQ: %d\n",
3604 ret);
3606 ret = wm8994_request_irq(wm8994->wm8994,
3607 WM8994_IRQ_MIC2_SHRT,
3608 wm8994_mic_irq, "Mic 2 short",
3609 wm8994);
3610 if (ret != 0)
3611 dev_warn(codec->dev,
3612 "Failed to request Mic2 short IRQ: %d\n",
3613 ret);
3614 break;
3616 case WM8958:
3617 case WM1811:
3618 if (wm8994->micdet_irq) {
3619 ret = request_threaded_irq(wm8994->micdet_irq, NULL,
3620 wm8958_mic_irq,
3621 IRQF_TRIGGER_RISING,
3622 "Mic detect",
3623 wm8994);
3624 if (ret != 0)
3625 dev_warn(codec->dev,
3626 "Failed to request Mic detect IRQ: %d\n",
3627 ret);
3631 switch (control->type) {
3632 case WM1811:
3633 if (wm8994->revision > 1) {
3634 ret = wm8994_request_irq(wm8994->wm8994,
3635 WM8994_IRQ_GPIO(6),
3636 wm1811_jackdet_irq, "JACKDET",
3637 wm8994);
3638 if (ret == 0)
3639 wm8994->jackdet = true;
3641 break;
3642 default:
3643 break;
3646 wm8994->fll_locked_irq = true;
3647 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++) {
3648 ret = wm8994_request_irq(wm8994->wm8994,
3649 WM8994_IRQ_FLL1_LOCK + i,
3650 wm8994_fll_locked_irq, "FLL lock",
3651 &wm8994->fll_locked[i]);
3652 if (ret != 0)
3653 wm8994->fll_locked_irq = false;
3656 /* Make sure we can read from the GPIOs if they're inputs */
3657 pm_runtime_get_sync(codec->dev);
3659 /* Remember if AIFnLRCLK is configured as a GPIO. This should be
3660 * configured on init - if a system wants to do this dynamically
3661 * at runtime we can deal with that then.
3663 ret = regmap_read(control->regmap, WM8994_GPIO_1, &reg);
3664 if (ret < 0) {
3665 dev_err(codec->dev, "Failed to read GPIO1 state: %d\n", ret);
3666 goto err_irq;
3668 if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
3669 wm8994->lrclk_shared[0] = 1;
3670 wm8994_dai[0].symmetric_rates = 1;
3671 } else {
3672 wm8994->lrclk_shared[0] = 0;
3675 ret = regmap_read(control->regmap, WM8994_GPIO_6, &reg);
3676 if (ret < 0) {
3677 dev_err(codec->dev, "Failed to read GPIO6 state: %d\n", ret);
3678 goto err_irq;
3680 if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
3681 wm8994->lrclk_shared[1] = 1;
3682 wm8994_dai[1].symmetric_rates = 1;
3683 } else {
3684 wm8994->lrclk_shared[1] = 0;
3687 pm_runtime_put(codec->dev);
3689 /* Latch volume updates (right only; we always do left then right). */
3690 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_LEFT_VOLUME,
3691 WM8994_AIF1DAC1_VU, WM8994_AIF1DAC1_VU);
3692 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_RIGHT_VOLUME,
3693 WM8994_AIF1DAC1_VU, WM8994_AIF1DAC1_VU);
3694 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_LEFT_VOLUME,
3695 WM8994_AIF1DAC2_VU, WM8994_AIF1DAC2_VU);
3696 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_RIGHT_VOLUME,
3697 WM8994_AIF1DAC2_VU, WM8994_AIF1DAC2_VU);
3698 snd_soc_update_bits(codec, WM8994_AIF2_DAC_LEFT_VOLUME,
3699 WM8994_AIF2DAC_VU, WM8994_AIF2DAC_VU);
3700 snd_soc_update_bits(codec, WM8994_AIF2_DAC_RIGHT_VOLUME,
3701 WM8994_AIF2DAC_VU, WM8994_AIF2DAC_VU);
3702 snd_soc_update_bits(codec, WM8994_AIF1_ADC1_LEFT_VOLUME,
3703 WM8994_AIF1ADC1_VU, WM8994_AIF1ADC1_VU);
3704 snd_soc_update_bits(codec, WM8994_AIF1_ADC1_RIGHT_VOLUME,
3705 WM8994_AIF1ADC1_VU, WM8994_AIF1ADC1_VU);
3706 snd_soc_update_bits(codec, WM8994_AIF1_ADC2_LEFT_VOLUME,
3707 WM8994_AIF1ADC2_VU, WM8994_AIF1ADC2_VU);
3708 snd_soc_update_bits(codec, WM8994_AIF1_ADC2_RIGHT_VOLUME,
3709 WM8994_AIF1ADC2_VU, WM8994_AIF1ADC2_VU);
3710 snd_soc_update_bits(codec, WM8994_AIF2_ADC_LEFT_VOLUME,
3711 WM8994_AIF2ADC_VU, WM8994_AIF1ADC2_VU);
3712 snd_soc_update_bits(codec, WM8994_AIF2_ADC_RIGHT_VOLUME,
3713 WM8994_AIF2ADC_VU, WM8994_AIF1ADC2_VU);
3714 snd_soc_update_bits(codec, WM8994_DAC1_LEFT_VOLUME,
3715 WM8994_DAC1_VU, WM8994_DAC1_VU);
3716 snd_soc_update_bits(codec, WM8994_DAC1_RIGHT_VOLUME,
3717 WM8994_DAC1_VU, WM8994_DAC1_VU);
3718 snd_soc_update_bits(codec, WM8994_DAC2_LEFT_VOLUME,
3719 WM8994_DAC2_VU, WM8994_DAC2_VU);
3720 snd_soc_update_bits(codec, WM8994_DAC2_RIGHT_VOLUME,
3721 WM8994_DAC2_VU, WM8994_DAC2_VU);
3723 /* Set the low bit of the 3D stereo depth so TLV matches */
3724 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_FILTERS_2,
3725 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT,
3726 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT);
3727 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_FILTERS_2,
3728 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT,
3729 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT);
3730 snd_soc_update_bits(codec, WM8994_AIF2_DAC_FILTERS_2,
3731 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT,
3732 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT);
3734 /* Unconditionally enable AIF1 ADC TDM mode on chips which can
3735 * use this; it only affects behaviour on idle TDM clock
3736 * cycles. */
3737 switch (control->type) {
3738 case WM8994:
3739 case WM8958:
3740 snd_soc_update_bits(codec, WM8994_AIF1_CONTROL_1,
3741 WM8994_AIF1ADC_TDM, WM8994_AIF1ADC_TDM);
3742 break;
3743 default:
3744 break;
3747 /* Put MICBIAS into bypass mode by default on newer devices */
3748 switch (control->type) {
3749 case WM8958:
3750 case WM1811:
3751 snd_soc_update_bits(codec, WM8958_MICBIAS1,
3752 WM8958_MICB1_MODE, WM8958_MICB1_MODE);
3753 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3754 WM8958_MICB2_MODE, WM8958_MICB2_MODE);
3755 break;
3756 default:
3757 break;
3760 wm8994_update_class_w(codec);
3762 wm8994_handle_pdata(wm8994);
3764 wm_hubs_add_analogue_controls(codec);
3765 snd_soc_add_codec_controls(codec, wm8994_snd_controls,
3766 ARRAY_SIZE(wm8994_snd_controls));
3767 snd_soc_dapm_new_controls(dapm, wm8994_dapm_widgets,
3768 ARRAY_SIZE(wm8994_dapm_widgets));
3770 switch (control->type) {
3771 case WM8994:
3772 snd_soc_dapm_new_controls(dapm, wm8994_specific_dapm_widgets,
3773 ARRAY_SIZE(wm8994_specific_dapm_widgets));
3774 if (wm8994->revision < 4) {
3775 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
3776 ARRAY_SIZE(wm8994_lateclk_revd_widgets));
3777 snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
3778 ARRAY_SIZE(wm8994_adc_revd_widgets));
3779 snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
3780 ARRAY_SIZE(wm8994_dac_revd_widgets));
3781 } else {
3782 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
3783 ARRAY_SIZE(wm8994_lateclk_widgets));
3784 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
3785 ARRAY_SIZE(wm8994_adc_widgets));
3786 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
3787 ARRAY_SIZE(wm8994_dac_widgets));
3789 break;
3790 case WM8958:
3791 snd_soc_add_codec_controls(codec, wm8958_snd_controls,
3792 ARRAY_SIZE(wm8958_snd_controls));
3793 snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
3794 ARRAY_SIZE(wm8958_dapm_widgets));
3795 if (wm8994->revision < 1) {
3796 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
3797 ARRAY_SIZE(wm8994_lateclk_revd_widgets));
3798 snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
3799 ARRAY_SIZE(wm8994_adc_revd_widgets));
3800 snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
3801 ARRAY_SIZE(wm8994_dac_revd_widgets));
3802 } else {
3803 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
3804 ARRAY_SIZE(wm8994_lateclk_widgets));
3805 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
3806 ARRAY_SIZE(wm8994_adc_widgets));
3807 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
3808 ARRAY_SIZE(wm8994_dac_widgets));
3810 break;
3812 case WM1811:
3813 snd_soc_add_codec_controls(codec, wm8958_snd_controls,
3814 ARRAY_SIZE(wm8958_snd_controls));
3815 snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
3816 ARRAY_SIZE(wm8958_dapm_widgets));
3817 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
3818 ARRAY_SIZE(wm8994_lateclk_widgets));
3819 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
3820 ARRAY_SIZE(wm8994_adc_widgets));
3821 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
3822 ARRAY_SIZE(wm8994_dac_widgets));
3823 break;
3827 wm_hubs_add_analogue_routes(codec, 0, 0);
3828 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
3830 switch (control->type) {
3831 case WM8994:
3832 snd_soc_dapm_add_routes(dapm, wm8994_intercon,
3833 ARRAY_SIZE(wm8994_intercon));
3835 if (wm8994->revision < 4) {
3836 snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
3837 ARRAY_SIZE(wm8994_revd_intercon));
3838 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
3839 ARRAY_SIZE(wm8994_lateclk_revd_intercon));
3840 } else {
3841 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
3842 ARRAY_SIZE(wm8994_lateclk_intercon));
3844 break;
3845 case WM8958:
3846 if (wm8994->revision < 1) {
3847 snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
3848 ARRAY_SIZE(wm8994_revd_intercon));
3849 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
3850 ARRAY_SIZE(wm8994_lateclk_revd_intercon));
3851 } else {
3852 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
3853 ARRAY_SIZE(wm8994_lateclk_intercon));
3854 snd_soc_dapm_add_routes(dapm, wm8958_intercon,
3855 ARRAY_SIZE(wm8958_intercon));
3858 wm8958_dsp2_init(codec);
3859 break;
3860 case WM1811:
3861 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
3862 ARRAY_SIZE(wm8994_lateclk_intercon));
3863 snd_soc_dapm_add_routes(dapm, wm8958_intercon,
3864 ARRAY_SIZE(wm8958_intercon));
3865 break;
3868 return 0;
3870 err_irq:
3871 if (wm8994->jackdet)
3872 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);
3873 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_SHRT, wm8994);
3874 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET, wm8994);
3875 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT, wm8994);
3876 if (wm8994->micdet_irq)
3877 free_irq(wm8994->micdet_irq, wm8994);
3878 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
3879 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
3880 &wm8994->fll_locked[i]);
3881 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
3882 &wm8994->hubs);
3883 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, codec);
3884 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, codec);
3885 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, codec);
3887 return ret;
3890 static int wm8994_codec_remove(struct snd_soc_codec *codec)
3892 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
3893 struct wm8994 *control = wm8994->wm8994;
3894 int i;
3896 wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);
3898 pm_runtime_disable(codec->dev);
3900 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
3901 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
3902 &wm8994->fll_locked[i]);
3904 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
3905 &wm8994->hubs);
3906 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, codec);
3907 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, codec);
3908 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, codec);
3910 if (wm8994->jackdet)
3911 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);
3913 switch (control->type) {
3914 case WM8994:
3915 if (wm8994->micdet_irq)
3916 free_irq(wm8994->micdet_irq, wm8994);
3917 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET,
3918 wm8994);
3919 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT,
3920 wm8994);
3921 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_DET,
3922 wm8994);
3923 break;
3925 case WM1811:
3926 case WM8958:
3927 if (wm8994->micdet_irq)
3928 free_irq(wm8994->micdet_irq, wm8994);
3929 break;
3931 if (wm8994->mbc)
3932 release_firmware(wm8994->mbc);
3933 if (wm8994->mbc_vss)
3934 release_firmware(wm8994->mbc_vss);
3935 if (wm8994->enh_eq)
3936 release_firmware(wm8994->enh_eq);
3937 kfree(wm8994->retune_mobile_texts);
3939 return 0;
3942 static struct snd_soc_codec_driver soc_codec_dev_wm8994 = {
3943 .probe = wm8994_codec_probe,
3944 .remove = wm8994_codec_remove,
3945 .suspend = wm8994_codec_suspend,
3946 .resume = wm8994_codec_resume,
3947 .set_bias_level = wm8994_set_bias_level,
3950 static int __devinit wm8994_probe(struct platform_device *pdev)
3952 struct wm8994_priv *wm8994;
3954 wm8994 = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_priv),
3955 GFP_KERNEL);
3956 if (wm8994 == NULL)
3957 return -ENOMEM;
3958 platform_set_drvdata(pdev, wm8994);
3960 wm8994->wm8994 = dev_get_drvdata(pdev->dev.parent);
3961 wm8994->pdata = dev_get_platdata(pdev->dev.parent);
3963 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wm8994,
3964 wm8994_dai, ARRAY_SIZE(wm8994_dai));
3967 static int __devexit wm8994_remove(struct platform_device *pdev)
3969 snd_soc_unregister_codec(&pdev->dev);
3970 return 0;
3973 #ifdef CONFIG_PM_SLEEP
3974 static int wm8994_suspend(struct device *dev)
3976 struct wm8994_priv *wm8994 = dev_get_drvdata(dev);
3978 /* Drop down to power saving mode when system is suspended */
3979 if (wm8994->jackdet && !wm8994->active_refcount)
3980 regmap_update_bits(wm8994->wm8994->regmap, WM8994_ANTIPOP_2,
3981 WM1811_JACKDET_MODE_MASK,
3982 wm8994->jackdet_mode);
3984 return 0;
3987 static int wm8994_resume(struct device *dev)
3989 struct wm8994_priv *wm8994 = dev_get_drvdata(dev);
3991 if (wm8994->jackdet && wm8994->jack_cb)
3992 regmap_update_bits(wm8994->wm8994->regmap, WM8994_ANTIPOP_2,
3993 WM1811_JACKDET_MODE_MASK,
3994 WM1811_JACKDET_MODE_AUDIO);
3996 return 0;
3998 #endif
4000 static const struct dev_pm_ops wm8994_pm_ops = {
4001 SET_SYSTEM_SLEEP_PM_OPS(wm8994_suspend, wm8994_resume)
4004 static struct platform_driver wm8994_codec_driver = {
4005 .driver = {
4006 .name = "wm8994-codec",
4007 .owner = THIS_MODULE,
4008 .pm = &wm8994_pm_ops,
4010 .probe = wm8994_probe,
4011 .remove = __devexit_p(wm8994_remove),
4014 module_platform_driver(wm8994_codec_driver);
4016 MODULE_DESCRIPTION("ASoC WM8994 driver");
4017 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
4018 MODULE_LICENSE("GPL");
4019 MODULE_ALIAS("platform:wm8994-codec");