blob: 353612eec8bf18754e9782ae091b1e92f64dab44 [file] [log] [blame]
Mark Brown9e6e96a2010-01-29 17:47:12 +00001/*
2 * wm8994.c -- WM8994 ALSA SoC Audio driver
3 *
Mark Brown656baae2012-05-23 12:39:07 +01004 * Copyright 2009-12 Wolfson Microelectronics plc
Mark Brown9e6e96a2010-01-29 17:47:12 +00005 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/init.h>
17#include <linux/delay.h>
18#include <linux/pm.h>
19#include <linux/i2c.h>
20#include <linux/platform_device.h>
Mark Brown39fb51a2010-11-26 17:23:43 +000021#include <linux/pm_runtime.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000022#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000024#include <sound/core.h>
Mark Brown821edd22010-11-26 15:21:09 +000025#include <sound/jack.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000026#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/soc.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000029#include <sound/initval.h>
30#include <sound/tlv.h>
Mark Brown2bbb5d62010-12-05 12:50:12 +000031#include <trace/events/asoc.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000032
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>
37
38#include "wm8994.h"
39#include "wm_hubs.h"
40
Mark Brownaf6b6fe2011-11-30 20:32:05 +000041#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
45
Mark Brown9e6e96a2010-01-29 17:47:12 +000046#define WM8994_NUM_DRC 3
47#define WM8994_NUM_EQ 3
48
Mark Brownbfd37bb2012-06-05 12:31:32 +010049static struct {
50 unsigned int reg;
51 unsigned int mask;
52} wm8994_vu_bits[] = {
53 { WM8994_LEFT_LINE_INPUT_1_2_VOLUME, WM8994_IN1_VU },
54 { WM8994_RIGHT_LINE_INPUT_1_2_VOLUME, WM8994_IN1_VU },
55 { WM8994_LEFT_LINE_INPUT_3_4_VOLUME, WM8994_IN2_VU },
56 { WM8994_RIGHT_LINE_INPUT_3_4_VOLUME, WM8994_IN2_VU },
57 { WM8994_SPEAKER_VOLUME_LEFT, WM8994_SPKOUT_VU },
58 { WM8994_SPEAKER_VOLUME_RIGHT, WM8994_SPKOUT_VU },
59 { WM8994_LEFT_OUTPUT_VOLUME, WM8994_HPOUT1_VU },
60 { WM8994_RIGHT_OUTPUT_VOLUME, WM8994_HPOUT1_VU },
61 { WM8994_LEFT_OPGA_VOLUME, WM8994_MIXOUT_VU },
62 { WM8994_RIGHT_OPGA_VOLUME, WM8994_MIXOUT_VU },
63
64 { WM8994_AIF1_DAC1_LEFT_VOLUME, WM8994_AIF1DAC1_VU },
65 { WM8994_AIF1_DAC1_RIGHT_VOLUME, WM8994_AIF1DAC1_VU },
66 { WM8994_AIF1_DAC2_LEFT_VOLUME, WM8994_AIF1DAC2_VU },
67 { WM8994_AIF1_DAC2_RIGHT_VOLUME, WM8994_AIF1DAC2_VU },
68 { WM8994_AIF2_DAC_LEFT_VOLUME, WM8994_AIF2DAC_VU },
69 { WM8994_AIF2_DAC_RIGHT_VOLUME, WM8994_AIF2DAC_VU },
70 { WM8994_AIF1_ADC1_LEFT_VOLUME, WM8994_AIF1ADC1_VU },
71 { WM8994_AIF1_ADC1_RIGHT_VOLUME, WM8994_AIF1ADC1_VU },
72 { WM8994_AIF1_ADC2_LEFT_VOLUME, WM8994_AIF1ADC2_VU },
73 { WM8994_AIF1_ADC2_RIGHT_VOLUME, WM8994_AIF1ADC2_VU },
74 { WM8994_AIF2_ADC_LEFT_VOLUME, WM8994_AIF2ADC_VU },
75 { WM8994_AIF2_ADC_RIGHT_VOLUME, WM8994_AIF1ADC2_VU },
76 { WM8994_DAC1_LEFT_VOLUME, WM8994_DAC1_VU },
77 { WM8994_DAC1_RIGHT_VOLUME, WM8994_DAC1_VU },
78 { WM8994_DAC2_LEFT_VOLUME, WM8994_DAC2_VU },
79 { WM8994_DAC2_RIGHT_VOLUME, WM8994_DAC2_VU },
80};
81
Mark Brown9e6e96a2010-01-29 17:47:12 +000082static int wm8994_drc_base[] = {
83 WM8994_AIF1_DRC1_1,
84 WM8994_AIF1_DRC2_1,
85 WM8994_AIF2_DRC_1,
86};
87
88static int wm8994_retune_mobile_base[] = {
89 WM8994_AIF1_DAC1_EQ_GAINS_1,
90 WM8994_AIF1_DAC2_EQ_GAINS_1,
91 WM8994_AIF2_EQ_GAINS_1,
92};
93
Mark Brownb00adf72011-08-13 11:57:18 +090094static void wm8958_default_micdet(u16 status, void *data);
95
Mark Brownaf6b6fe2011-11-30 20:32:05 +000096static const struct wm8958_micd_rate micdet_rates[] = {
Mark Brownb00adf72011-08-13 11:57:18 +090097 { 32768, true, 1, 4 },
98 { 32768, false, 1, 1 },
Mark Brown604533d2011-12-01 12:51:25 +000099 { 44100 * 256, true, 7, 10 },
100 { 44100 * 256, false, 7, 10 },
Mark Brownb00adf72011-08-13 11:57:18 +0900101};
102
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000103static const struct wm8958_micd_rate jackdet_rates[] = {
104 { 32768, true, 0, 1 },
105 { 32768, false, 0, 1 },
Mark Browne9d9a962012-04-26 16:07:32 +0100106 { 44100 * 256, true, 10, 10 },
107 { 44100 * 256, false, 7, 8 },
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000108};
109
Mark Brownb00adf72011-08-13 11:57:18 +0900110static void wm8958_micd_set_rate(struct snd_soc_codec *codec)
111{
112 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
113 int best, i, sysclk, val;
114 bool idle;
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000115 const struct wm8958_micd_rate *rates;
116 int num_rates;
Mark Brownb00adf72011-08-13 11:57:18 +0900117
Mark Brownfcdc4de2012-04-26 16:35:46 +0100118 if (!(wm8994->pdata && wm8994->pdata->micd_rates) &&
119 wm8994->jack_cb != wm8958_default_micdet)
Mark Brownb00adf72011-08-13 11:57:18 +0900120 return;
121
122 idle = !wm8994->jack_mic;
123
124 sysclk = snd_soc_read(codec, WM8994_CLOCKING_1);
125 if (sysclk & WM8994_SYSCLK_SRC)
126 sysclk = wm8994->aifclk[1];
127 else
128 sysclk = wm8994->aifclk[0];
129
Mark Browncd1707a2011-12-01 13:44:25 +0000130 if (wm8994->pdata && wm8994->pdata->micd_rates) {
131 rates = wm8994->pdata->micd_rates;
132 num_rates = wm8994->pdata->num_micd_rates;
133 } else if (wm8994->jackdet) {
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000134 rates = jackdet_rates;
135 num_rates = ARRAY_SIZE(jackdet_rates);
136 } else {
137 rates = micdet_rates;
138 num_rates = ARRAY_SIZE(micdet_rates);
139 }
140
Mark Brownb00adf72011-08-13 11:57:18 +0900141 best = 0;
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000142 for (i = 0; i < num_rates; i++) {
143 if (rates[i].idle != idle)
Mark Brownb00adf72011-08-13 11:57:18 +0900144 continue;
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000145 if (abs(rates[i].sysclk - sysclk) <
146 abs(rates[best].sysclk - sysclk))
Mark Brownb00adf72011-08-13 11:57:18 +0900147 best = i;
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000148 else if (rates[best].idle != idle)
Mark Brownb00adf72011-08-13 11:57:18 +0900149 best = i;
150 }
151
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000152 val = rates[best].start << WM8958_MICD_BIAS_STARTTIME_SHIFT
153 | rates[best].rate << WM8958_MICD_RATE_SHIFT;
Mark Brownb00adf72011-08-13 11:57:18 +0900154
Mark Brown3a334ad2012-04-26 17:02:16 +0100155 dev_dbg(codec->dev, "MICD rate %d,%d for %dHz %s\n",
156 rates[best].start, rates[best].rate, sysclk,
157 idle ? "idle" : "active");
158
Mark Brownb00adf72011-08-13 11:57:18 +0900159 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
160 WM8958_MICD_BIAS_STARTTIME_MASK |
161 WM8958_MICD_RATE_MASK, val);
162}
163
Mark Brown9e6e96a2010-01-29 17:47:12 +0000164static int configure_aif_clock(struct snd_soc_codec *codec, int aif)
165{
Mark Brownb2c812e2010-04-14 15:35:19 +0900166 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000167 int rate;
168 int reg1 = 0;
169 int offset;
170
171 if (aif)
172 offset = 4;
173 else
174 offset = 0;
175
176 switch (wm8994->sysclk[aif]) {
177 case WM8994_SYSCLK_MCLK1:
178 rate = wm8994->mclk[0];
179 break;
180
181 case WM8994_SYSCLK_MCLK2:
182 reg1 |= 0x8;
183 rate = wm8994->mclk[1];
184 break;
185
186 case WM8994_SYSCLK_FLL1:
187 reg1 |= 0x10;
188 rate = wm8994->fll[0].out;
189 break;
190
191 case WM8994_SYSCLK_FLL2:
192 reg1 |= 0x18;
193 rate = wm8994->fll[1].out;
194 break;
195
196 default:
197 return -EINVAL;
198 }
199
200 if (rate >= 13500000) {
201 rate /= 2;
202 reg1 |= WM8994_AIF1CLK_DIV;
203
204 dev_dbg(codec->dev, "Dividing AIF%d clock to %dHz\n",
205 aif + 1, rate);
206 }
Mark Brown5e5e2be2010-04-25 12:20:30 +0100207
Mark Brown9e6e96a2010-01-29 17:47:12 +0000208 wm8994->aifclk[aif] = rate;
209
210 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1 + offset,
211 WM8994_AIF1CLK_SRC_MASK | WM8994_AIF1CLK_DIV,
212 reg1);
213
214 return 0;
215}
216
217static int configure_clock(struct snd_soc_codec *codec)
218{
Mark Brownb2c812e2010-04-14 15:35:19 +0900219 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Axel Lin04f45c42011-10-04 20:07:03 +0800220 int change, new;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000221
222 /* Bring up the AIF clocks first */
223 configure_aif_clock(codec, 0);
224 configure_aif_clock(codec, 1);
225
226 /* Then switch CLK_SYS over to the higher of them; a change
227 * can only happen as a result of a clocking change which can
228 * only be made outside of DAPM so we can safely redo the
229 * clocking.
230 */
231
232 /* If they're equal it doesn't matter which is used */
Mark Brownb00adf72011-08-13 11:57:18 +0900233 if (wm8994->aifclk[0] == wm8994->aifclk[1]) {
234 wm8958_micd_set_rate(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000235 return 0;
Mark Brownb00adf72011-08-13 11:57:18 +0900236 }
Mark Brown9e6e96a2010-01-29 17:47:12 +0000237
238 if (wm8994->aifclk[0] < wm8994->aifclk[1])
239 new = WM8994_SYSCLK_SRC;
240 else
241 new = 0;
242
Axel Lin04f45c42011-10-04 20:07:03 +0800243 change = snd_soc_update_bits(codec, WM8994_CLOCKING_1,
244 WM8994_SYSCLK_SRC, new);
Mark Brown52ac7ab2011-12-01 12:43:26 +0000245 if (change)
246 snd_soc_dapm_sync(&codec->dapm);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000247
Mark Brownb00adf72011-08-13 11:57:18 +0900248 wm8958_micd_set_rate(codec);
249
Mark Brown9e6e96a2010-01-29 17:47:12 +0000250 return 0;
251}
252
253static int check_clk_sys(struct snd_soc_dapm_widget *source,
254 struct snd_soc_dapm_widget *sink)
255{
256 int reg = snd_soc_read(source->codec, WM8994_CLOCKING_1);
257 const char *clk;
258
259 /* Check what we're currently using for CLK_SYS */
260 if (reg & WM8994_SYSCLK_SRC)
261 clk = "AIF2CLK";
262 else
263 clk = "AIF1CLK";
264
265 return strcmp(source->name, clk) == 0;
266}
267
268static const char *sidetone_hpf_text[] = {
269 "2.7kHz", "1.35kHz", "675Hz", "370Hz", "180Hz", "90Hz", "45Hz"
270};
271
272static const struct soc_enum sidetone_hpf =
273 SOC_ENUM_SINGLE(WM8994_SIDETONE, 7, 7, sidetone_hpf_text);
274
Uk Kim146fd572010-12-07 13:58:40 +0000275static const char *adc_hpf_text[] = {
276 "HiFi", "Voice 1", "Voice 2", "Voice 3"
277};
278
279static const struct soc_enum aif1adc1_hpf =
280 SOC_ENUM_SINGLE(WM8994_AIF1_ADC1_FILTERS, 13, 4, adc_hpf_text);
281
282static const struct soc_enum aif1adc2_hpf =
283 SOC_ENUM_SINGLE(WM8994_AIF1_ADC2_FILTERS, 13, 4, adc_hpf_text);
284
285static const struct soc_enum aif2adc_hpf =
286 SOC_ENUM_SINGLE(WM8994_AIF2_ADC_FILTERS, 13, 4, adc_hpf_text);
287
Mark Brown9e6e96a2010-01-29 17:47:12 +0000288static const DECLARE_TLV_DB_SCALE(aif_tlv, 0, 600, 0);
289static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
290static const DECLARE_TLV_DB_SCALE(st_tlv, -3600, 300, 0);
291static const DECLARE_TLV_DB_SCALE(wm8994_3d_tlv, -1600, 183, 0);
292static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
Mark Brown1ddc07d2011-08-16 10:08:48 +0900293static const DECLARE_TLV_DB_SCALE(ng_tlv, -10200, 600, 0);
Mark Brown81204c82011-05-24 17:35:53 +0800294static const DECLARE_TLV_DB_SCALE(mixin_boost_tlv, 0, 900, 0);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000295
296#define WM8994_DRC_SWITCH(xname, reg, shift) \
297{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
298 .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,\
299 .put = wm8994_put_drc_sw, \
300 .private_value = SOC_SINGLE_VALUE(reg, shift, 1, 0) }
301
302static int wm8994_put_drc_sw(struct snd_kcontrol *kcontrol,
303 struct snd_ctl_elem_value *ucontrol)
304{
305 struct soc_mixer_control *mc =
306 (struct soc_mixer_control *)kcontrol->private_value;
307 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
308 int mask, ret;
309
310 /* Can't enable both ADC and DAC paths simultaneously */
311 if (mc->shift == WM8994_AIF1DAC1_DRC_ENA_SHIFT)
312 mask = WM8994_AIF1ADC1L_DRC_ENA_MASK |
313 WM8994_AIF1ADC1R_DRC_ENA_MASK;
314 else
315 mask = WM8994_AIF1DAC1_DRC_ENA_MASK;
316
317 ret = snd_soc_read(codec, mc->reg);
318 if (ret < 0)
319 return ret;
320 if (ret & mask)
321 return -EINVAL;
322
323 return snd_soc_put_volsw(kcontrol, ucontrol);
324}
325
Mark Brown9e6e96a2010-01-29 17:47:12 +0000326static void wm8994_set_drc(struct snd_soc_codec *codec, int drc)
327{
Mark Brownb2c812e2010-04-14 15:35:19 +0900328 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000329 struct wm8994_pdata *pdata = wm8994->pdata;
330 int base = wm8994_drc_base[drc];
331 int cfg = wm8994->drc_cfg[drc];
332 int save, i;
333
334 /* Save any enables; the configuration should clear them. */
335 save = snd_soc_read(codec, base);
336 save &= WM8994_AIF1DAC1_DRC_ENA | WM8994_AIF1ADC1L_DRC_ENA |
337 WM8994_AIF1ADC1R_DRC_ENA;
338
339 for (i = 0; i < WM8994_DRC_REGS; i++)
340 snd_soc_update_bits(codec, base + i, 0xffff,
341 pdata->drc_cfgs[cfg].regs[i]);
342
343 snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_DRC_ENA |
344 WM8994_AIF1ADC1L_DRC_ENA |
345 WM8994_AIF1ADC1R_DRC_ENA, save);
346}
347
348/* Icky as hell but saves code duplication */
349static int wm8994_get_drc(const char *name)
350{
351 if (strcmp(name, "AIF1DRC1 Mode") == 0)
352 return 0;
353 if (strcmp(name, "AIF1DRC2 Mode") == 0)
354 return 1;
355 if (strcmp(name, "AIF2DRC Mode") == 0)
356 return 2;
357 return -EINVAL;
358}
359
360static int wm8994_put_drc_enum(struct snd_kcontrol *kcontrol,
361 struct snd_ctl_elem_value *ucontrol)
362{
363 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000364 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000365 struct wm8994_pdata *pdata = wm8994->pdata;
366 int drc = wm8994_get_drc(kcontrol->id.name);
367 int value = ucontrol->value.integer.value[0];
368
369 if (drc < 0)
370 return drc;
371
372 if (value >= pdata->num_drc_cfgs)
373 return -EINVAL;
374
375 wm8994->drc_cfg[drc] = value;
376
377 wm8994_set_drc(codec, drc);
378
379 return 0;
380}
381
382static int wm8994_get_drc_enum(struct snd_kcontrol *kcontrol,
383 struct snd_ctl_elem_value *ucontrol)
384{
385 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +0900386 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000387 int drc = wm8994_get_drc(kcontrol->id.name);
388
389 ucontrol->value.enumerated.item[0] = wm8994->drc_cfg[drc];
390
391 return 0;
392}
393
394static void wm8994_set_retune_mobile(struct snd_soc_codec *codec, int block)
395{
Mark Brownb2c812e2010-04-14 15:35:19 +0900396 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000397 struct wm8994_pdata *pdata = wm8994->pdata;
398 int base = wm8994_retune_mobile_base[block];
399 int iface, best, best_val, save, i, cfg;
400
401 if (!pdata || !wm8994->num_retune_mobile_texts)
402 return;
403
404 switch (block) {
405 case 0:
406 case 1:
407 iface = 0;
408 break;
409 case 2:
410 iface = 1;
411 break;
412 default:
413 return;
414 }
415
416 /* Find the version of the currently selected configuration
417 * with the nearest sample rate. */
418 cfg = wm8994->retune_mobile_cfg[block];
419 best = 0;
420 best_val = INT_MAX;
421 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
422 if (strcmp(pdata->retune_mobile_cfgs[i].name,
423 wm8994->retune_mobile_texts[cfg]) == 0 &&
424 abs(pdata->retune_mobile_cfgs[i].rate
425 - wm8994->dac_rates[iface]) < best_val) {
426 best = i;
427 best_val = abs(pdata->retune_mobile_cfgs[i].rate
428 - wm8994->dac_rates[iface]);
429 }
430 }
431
432 dev_dbg(codec->dev, "ReTune Mobile %d %s/%dHz for %dHz sample rate\n",
433 block,
434 pdata->retune_mobile_cfgs[best].name,
435 pdata->retune_mobile_cfgs[best].rate,
436 wm8994->dac_rates[iface]);
437
438 /* The EQ will be disabled while reconfiguring it, remember the
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +0200439 * current configuration.
Mark Brown9e6e96a2010-01-29 17:47:12 +0000440 */
441 save = snd_soc_read(codec, base);
442 save &= WM8994_AIF1DAC1_EQ_ENA;
443
444 for (i = 0; i < WM8994_EQ_REGS; i++)
445 snd_soc_update_bits(codec, base + i, 0xffff,
446 pdata->retune_mobile_cfgs[best].regs[i]);
447
448 snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_EQ_ENA, save);
449}
450
451/* Icky as hell but saves code duplication */
452static int wm8994_get_retune_mobile_block(const char *name)
453{
454 if (strcmp(name, "AIF1.1 EQ Mode") == 0)
455 return 0;
456 if (strcmp(name, "AIF1.2 EQ Mode") == 0)
457 return 1;
458 if (strcmp(name, "AIF2 EQ Mode") == 0)
459 return 2;
460 return -EINVAL;
461}
462
463static int wm8994_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
464 struct snd_ctl_elem_value *ucontrol)
465{
466 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000467 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000468 struct wm8994_pdata *pdata = wm8994->pdata;
469 int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
470 int value = ucontrol->value.integer.value[0];
471
472 if (block < 0)
473 return block;
474
475 if (value >= pdata->num_retune_mobile_cfgs)
476 return -EINVAL;
477
478 wm8994->retune_mobile_cfg[block] = value;
479
480 wm8994_set_retune_mobile(codec, block);
481
482 return 0;
483}
484
485static int wm8994_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
486 struct snd_ctl_elem_value *ucontrol)
487{
488 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brown4a8d9292011-02-16 14:57:17 -0800489 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000490 int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
491
492 ucontrol->value.enumerated.item[0] = wm8994->retune_mobile_cfg[block];
493
494 return 0;
495}
496
Mark Brown96b101e2010-11-18 15:49:38 +0000497static const char *aif_chan_src_text[] = {
Mark Brownf5548852010-08-31 19:39:48 +0100498 "Left", "Right"
499};
500
Mark Brown96b101e2010-11-18 15:49:38 +0000501static const struct soc_enum aif1adcl_src =
502 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_1, 15, 2, aif_chan_src_text);
503
504static const struct soc_enum aif1adcr_src =
505 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_1, 14, 2, aif_chan_src_text);
506
507static const struct soc_enum aif2adcl_src =
508 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_1, 15, 2, aif_chan_src_text);
509
510static const struct soc_enum aif2adcr_src =
511 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_1, 14, 2, aif_chan_src_text);
512
Mark Brownf5548852010-08-31 19:39:48 +0100513static const struct soc_enum aif1dacl_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000514 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_2, 15, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100515
516static const struct soc_enum aif1dacr_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000517 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_2, 14, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100518
519static const struct soc_enum aif2dacl_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000520 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_2, 15, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100521
522static const struct soc_enum aif2dacr_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000523 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_2, 14, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100524
Mark Brown154b26a2010-12-09 12:07:44 +0000525static const char *osr_text[] = {
526 "Low Power", "High Performance",
527};
528
529static const struct soc_enum dac_osr =
530 SOC_ENUM_SINGLE(WM8994_OVERSAMPLING, 0, 2, osr_text);
531
532static const struct soc_enum adc_osr =
533 SOC_ENUM_SINGLE(WM8994_OVERSAMPLING, 1, 2, osr_text);
534
Mark Brown9e6e96a2010-01-29 17:47:12 +0000535static const struct snd_kcontrol_new wm8994_snd_controls[] = {
536SOC_DOUBLE_R_TLV("AIF1ADC1 Volume", WM8994_AIF1_ADC1_LEFT_VOLUME,
537 WM8994_AIF1_ADC1_RIGHT_VOLUME,
538 1, 119, 0, digital_tlv),
539SOC_DOUBLE_R_TLV("AIF1ADC2 Volume", WM8994_AIF1_ADC2_LEFT_VOLUME,
540 WM8994_AIF1_ADC2_RIGHT_VOLUME,
541 1, 119, 0, digital_tlv),
542SOC_DOUBLE_R_TLV("AIF2ADC Volume", WM8994_AIF2_ADC_LEFT_VOLUME,
543 WM8994_AIF2_ADC_RIGHT_VOLUME,
544 1, 119, 0, digital_tlv),
545
Mark Brown96b101e2010-11-18 15:49:38 +0000546SOC_ENUM("AIF1ADCL Source", aif1adcl_src),
547SOC_ENUM("AIF1ADCR Source", aif1adcr_src),
Mark Brown49db7e72010-12-08 13:49:43 +0000548SOC_ENUM("AIF2ADCL Source", aif2adcl_src),
549SOC_ENUM("AIF2ADCR Source", aif2adcr_src),
Mark Brown96b101e2010-11-18 15:49:38 +0000550
Mark Brownf5548852010-08-31 19:39:48 +0100551SOC_ENUM("AIF1DACL Source", aif1dacl_src),
552SOC_ENUM("AIF1DACR Source", aif1dacr_src),
Mark Brown49db7e72010-12-08 13:49:43 +0000553SOC_ENUM("AIF2DACL Source", aif2dacl_src),
554SOC_ENUM("AIF2DACR Source", aif2dacr_src),
Mark Brownf5548852010-08-31 19:39:48 +0100555
Mark Brown9e6e96a2010-01-29 17:47:12 +0000556SOC_DOUBLE_R_TLV("AIF1DAC1 Volume", WM8994_AIF1_DAC1_LEFT_VOLUME,
557 WM8994_AIF1_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
558SOC_DOUBLE_R_TLV("AIF1DAC2 Volume", WM8994_AIF1_DAC2_LEFT_VOLUME,
559 WM8994_AIF1_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
560SOC_DOUBLE_R_TLV("AIF2DAC Volume", WM8994_AIF2_DAC_LEFT_VOLUME,
561 WM8994_AIF2_DAC_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
562
563SOC_SINGLE_TLV("AIF1 Boost Volume", WM8994_AIF1_CONTROL_2, 10, 3, 0, aif_tlv),
564SOC_SINGLE_TLV("AIF2 Boost Volume", WM8994_AIF2_CONTROL_2, 10, 3, 0, aif_tlv),
565
566SOC_SINGLE("AIF1DAC1 EQ Switch", WM8994_AIF1_DAC1_EQ_GAINS_1, 0, 1, 0),
567SOC_SINGLE("AIF1DAC2 EQ Switch", WM8994_AIF1_DAC2_EQ_GAINS_1, 0, 1, 0),
568SOC_SINGLE("AIF2 EQ Switch", WM8994_AIF2_EQ_GAINS_1, 0, 1, 0),
569
570WM8994_DRC_SWITCH("AIF1DAC1 DRC Switch", WM8994_AIF1_DRC1_1, 2),
571WM8994_DRC_SWITCH("AIF1ADC1L DRC Switch", WM8994_AIF1_DRC1_1, 1),
572WM8994_DRC_SWITCH("AIF1ADC1R DRC Switch", WM8994_AIF1_DRC1_1, 0),
573
574WM8994_DRC_SWITCH("AIF1DAC2 DRC Switch", WM8994_AIF1_DRC2_1, 2),
575WM8994_DRC_SWITCH("AIF1ADC2L DRC Switch", WM8994_AIF1_DRC2_1, 1),
576WM8994_DRC_SWITCH("AIF1ADC2R DRC Switch", WM8994_AIF1_DRC2_1, 0),
577
578WM8994_DRC_SWITCH("AIF2DAC DRC Switch", WM8994_AIF2_DRC_1, 2),
579WM8994_DRC_SWITCH("AIF2ADCL DRC Switch", WM8994_AIF2_DRC_1, 1),
580WM8994_DRC_SWITCH("AIF2ADCR DRC Switch", WM8994_AIF2_DRC_1, 0),
581
582SOC_SINGLE_TLV("DAC1 Right Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
583 5, 12, 0, st_tlv),
584SOC_SINGLE_TLV("DAC1 Left Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
585 0, 12, 0, st_tlv),
586SOC_SINGLE_TLV("DAC2 Right Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
587 5, 12, 0, st_tlv),
588SOC_SINGLE_TLV("DAC2 Left Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
589 0, 12, 0, st_tlv),
590SOC_ENUM("Sidetone HPF Mux", sidetone_hpf),
591SOC_SINGLE("Sidetone HPF Switch", WM8994_SIDETONE, 6, 1, 0),
592
Uk Kim146fd572010-12-07 13:58:40 +0000593SOC_ENUM("AIF1ADC1 HPF Mode", aif1adc1_hpf),
594SOC_DOUBLE("AIF1ADC1 HPF Switch", WM8994_AIF1_ADC1_FILTERS, 12, 11, 1, 0),
595
596SOC_ENUM("AIF1ADC2 HPF Mode", aif1adc2_hpf),
597SOC_DOUBLE("AIF1ADC2 HPF Switch", WM8994_AIF1_ADC2_FILTERS, 12, 11, 1, 0),
598
599SOC_ENUM("AIF2ADC HPF Mode", aif2adc_hpf),
600SOC_DOUBLE("AIF2ADC HPF Switch", WM8994_AIF2_ADC_FILTERS, 12, 11, 1, 0),
601
Mark Brown154b26a2010-12-09 12:07:44 +0000602SOC_ENUM("ADC OSR", adc_osr),
603SOC_ENUM("DAC OSR", dac_osr),
604
Mark Brown9e6e96a2010-01-29 17:47:12 +0000605SOC_DOUBLE_R_TLV("DAC1 Volume", WM8994_DAC1_LEFT_VOLUME,
606 WM8994_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
607SOC_DOUBLE_R("DAC1 Switch", WM8994_DAC1_LEFT_VOLUME,
608 WM8994_DAC1_RIGHT_VOLUME, 9, 1, 1),
609
610SOC_DOUBLE_R_TLV("DAC2 Volume", WM8994_DAC2_LEFT_VOLUME,
611 WM8994_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
612SOC_DOUBLE_R("DAC2 Switch", WM8994_DAC2_LEFT_VOLUME,
613 WM8994_DAC2_RIGHT_VOLUME, 9, 1, 1),
614
615SOC_SINGLE_TLV("SPKL DAC2 Volume", WM8994_SPKMIXL_ATTENUATION,
616 6, 1, 1, wm_hubs_spkmix_tlv),
617SOC_SINGLE_TLV("SPKL DAC1 Volume", WM8994_SPKMIXL_ATTENUATION,
618 2, 1, 1, wm_hubs_spkmix_tlv),
619
620SOC_SINGLE_TLV("SPKR DAC2 Volume", WM8994_SPKMIXR_ATTENUATION,
621 6, 1, 1, wm_hubs_spkmix_tlv),
622SOC_SINGLE_TLV("SPKR DAC1 Volume", WM8994_SPKMIXR_ATTENUATION,
623 2, 1, 1, wm_hubs_spkmix_tlv),
624
625SOC_SINGLE_TLV("AIF1DAC1 3D Stereo Volume", WM8994_AIF1_DAC1_FILTERS_2,
626 10, 15, 0, wm8994_3d_tlv),
Mark Brown458350b2010-12-20 14:35:09 +0000627SOC_SINGLE("AIF1DAC1 3D Stereo Switch", WM8994_AIF1_DAC1_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000628 8, 1, 0),
629SOC_SINGLE_TLV("AIF1DAC2 3D Stereo Volume", WM8994_AIF1_DAC2_FILTERS_2,
630 10, 15, 0, wm8994_3d_tlv),
631SOC_SINGLE("AIF1DAC2 3D Stereo Switch", WM8994_AIF1_DAC2_FILTERS_2,
632 8, 1, 0),
Mark Brown458350b2010-12-20 14:35:09 +0000633SOC_SINGLE_TLV("AIF2DAC 3D Stereo Volume", WM8994_AIF2_DAC_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000634 10, 15, 0, wm8994_3d_tlv),
Mark Brown458350b2010-12-20 14:35:09 +0000635SOC_SINGLE("AIF2DAC 3D Stereo Switch", WM8994_AIF2_DAC_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000636 8, 1, 0),
637};
638
639static const struct snd_kcontrol_new wm8994_eq_controls[] = {
640SOC_SINGLE_TLV("AIF1DAC1 EQ1 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 11, 31, 0,
641 eq_tlv),
642SOC_SINGLE_TLV("AIF1DAC1 EQ2 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 6, 31, 0,
643 eq_tlv),
644SOC_SINGLE_TLV("AIF1DAC1 EQ3 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 1, 31, 0,
645 eq_tlv),
646SOC_SINGLE_TLV("AIF1DAC1 EQ4 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 11, 31, 0,
647 eq_tlv),
648SOC_SINGLE_TLV("AIF1DAC1 EQ5 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 6, 31, 0,
649 eq_tlv),
650
651SOC_SINGLE_TLV("AIF1DAC2 EQ1 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 11, 31, 0,
652 eq_tlv),
653SOC_SINGLE_TLV("AIF1DAC2 EQ2 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 6, 31, 0,
654 eq_tlv),
655SOC_SINGLE_TLV("AIF1DAC2 EQ3 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 1, 31, 0,
656 eq_tlv),
657SOC_SINGLE_TLV("AIF1DAC2 EQ4 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 11, 31, 0,
658 eq_tlv),
659SOC_SINGLE_TLV("AIF1DAC2 EQ5 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 6, 31, 0,
660 eq_tlv),
661
662SOC_SINGLE_TLV("AIF2 EQ1 Volume", WM8994_AIF2_EQ_GAINS_1, 11, 31, 0,
663 eq_tlv),
664SOC_SINGLE_TLV("AIF2 EQ2 Volume", WM8994_AIF2_EQ_GAINS_1, 6, 31, 0,
665 eq_tlv),
666SOC_SINGLE_TLV("AIF2 EQ3 Volume", WM8994_AIF2_EQ_GAINS_1, 1, 31, 0,
667 eq_tlv),
668SOC_SINGLE_TLV("AIF2 EQ4 Volume", WM8994_AIF2_EQ_GAINS_2, 11, 31, 0,
669 eq_tlv),
670SOC_SINGLE_TLV("AIF2 EQ5 Volume", WM8994_AIF2_EQ_GAINS_2, 6, 31, 0,
671 eq_tlv),
672};
673
Mark Brown45a690f2012-08-15 19:20:54 +0100674static const struct snd_kcontrol_new wm8994_drc_controls[] = {
675SND_SOC_BYTES_MASK("AIF1.1 DRC", WM8994_AIF1_DRC1_1, 5,
676 WM8994_AIF1DAC1_DRC_ENA | WM8994_AIF1ADC1L_DRC_ENA |
677 WM8994_AIF1ADC1R_DRC_ENA),
678SND_SOC_BYTES_MASK("AIF1.2 DRC", WM8994_AIF1_DRC2_1, 5,
679 WM8994_AIF1DAC2_DRC_ENA | WM8994_AIF1ADC2L_DRC_ENA |
680 WM8994_AIF1ADC2R_DRC_ENA),
681SND_SOC_BYTES_MASK("AIF2 DRC", WM8994_AIF2_DRC_1, 5,
682 WM8994_AIF2DAC_DRC_ENA | WM8994_AIF2ADCL_DRC_ENA |
683 WM8994_AIF2ADCR_DRC_ENA),
684};
685
Mark Brown1ddc07d2011-08-16 10:08:48 +0900686static const char *wm8958_ng_text[] = {
687 "30ms", "125ms", "250ms", "500ms",
688};
689
690static const struct soc_enum wm8958_aif1dac1_ng_hold =
691 SOC_ENUM_SINGLE(WM8958_AIF1_DAC1_NOISE_GATE,
692 WM8958_AIF1DAC1_NG_THR_SHIFT, 4, wm8958_ng_text);
693
694static const struct soc_enum wm8958_aif1dac2_ng_hold =
695 SOC_ENUM_SINGLE(WM8958_AIF1_DAC2_NOISE_GATE,
696 WM8958_AIF1DAC2_NG_THR_SHIFT, 4, wm8958_ng_text);
697
698static const struct soc_enum wm8958_aif2dac_ng_hold =
699 SOC_ENUM_SINGLE(WM8958_AIF2_DAC_NOISE_GATE,
700 WM8958_AIF2DAC_NG_THR_SHIFT, 4, wm8958_ng_text);
701
Mark Brownc4431df2010-11-26 15:21:07 +0000702static const struct snd_kcontrol_new wm8958_snd_controls[] = {
703SOC_SINGLE_TLV("AIF3 Boost Volume", WM8958_AIF3_CONTROL_2, 10, 3, 0, aif_tlv),
Mark Brown1ddc07d2011-08-16 10:08:48 +0900704
705SOC_SINGLE("AIF1DAC1 Noise Gate Switch", WM8958_AIF1_DAC1_NOISE_GATE,
706 WM8958_AIF1DAC1_NG_ENA_SHIFT, 1, 0),
707SOC_ENUM("AIF1DAC1 Noise Gate Hold Time", wm8958_aif1dac1_ng_hold),
708SOC_SINGLE_TLV("AIF1DAC1 Noise Gate Threshold Volume",
709 WM8958_AIF1_DAC1_NOISE_GATE, WM8958_AIF1DAC1_NG_THR_SHIFT,
710 7, 1, ng_tlv),
711
712SOC_SINGLE("AIF1DAC2 Noise Gate Switch", WM8958_AIF1_DAC2_NOISE_GATE,
713 WM8958_AIF1DAC2_NG_ENA_SHIFT, 1, 0),
714SOC_ENUM("AIF1DAC2 Noise Gate Hold Time", wm8958_aif1dac2_ng_hold),
715SOC_SINGLE_TLV("AIF1DAC2 Noise Gate Threshold Volume",
716 WM8958_AIF1_DAC2_NOISE_GATE, WM8958_AIF1DAC2_NG_THR_SHIFT,
717 7, 1, ng_tlv),
718
719SOC_SINGLE("AIF2DAC Noise Gate Switch", WM8958_AIF2_DAC_NOISE_GATE,
720 WM8958_AIF2DAC_NG_ENA_SHIFT, 1, 0),
721SOC_ENUM("AIF2DAC Noise Gate Hold Time", wm8958_aif2dac_ng_hold),
722SOC_SINGLE_TLV("AIF2DAC Noise Gate Threshold Volume",
723 WM8958_AIF2_DAC_NOISE_GATE, WM8958_AIF2DAC_NG_THR_SHIFT,
724 7, 1, ng_tlv),
Mark Brownc4431df2010-11-26 15:21:07 +0000725};
726
Mark Brown81204c82011-05-24 17:35:53 +0800727static const struct snd_kcontrol_new wm1811_snd_controls[] = {
728SOC_SINGLE_TLV("MIXINL IN1LP Boost Volume", WM8994_INPUT_MIXER_1, 7, 1, 0,
729 mixin_boost_tlv),
730SOC_SINGLE_TLV("MIXINL IN1RP Boost Volume", WM8994_INPUT_MIXER_1, 8, 1, 0,
731 mixin_boost_tlv),
732};
733
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000734/* We run all mode setting through a function to enforce audio mode */
735static void wm1811_jackdet_set_mode(struct snd_soc_codec *codec, u16 mode)
736{
737 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
738
Mark Brown28e33262012-03-03 00:10:02 +0000739 if (!wm8994->jackdet || !wm8994->jack_cb)
740 return;
741
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000742 if (wm8994->active_refcount)
743 mode = WM1811_JACKDET_MODE_AUDIO;
744
Mark Brown4752a882012-03-04 02:16:01 +0000745 if (mode == wm8994->jackdet_mode)
Mark Brown1defde22012-03-03 20:02:49 +0000746 return;
747
Mark Brown4752a882012-03-04 02:16:01 +0000748 wm8994->jackdet_mode = mode;
749
750 /* Always use audio mode to detect while the system is active */
751 if (mode != WM1811_JACKDET_MODE_NONE)
752 mode = WM1811_JACKDET_MODE_AUDIO;
753
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000754 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
755 WM1811_JACKDET_MODE_MASK, mode);
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000756}
757
758static void active_reference(struct snd_soc_codec *codec)
759{
760 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
761
762 mutex_lock(&wm8994->accdet_lock);
763
764 wm8994->active_refcount++;
765
766 dev_dbg(codec->dev, "Active refcount incremented, now %d\n",
767 wm8994->active_refcount);
768
Mark Brown1defde22012-03-03 20:02:49 +0000769 /* If we're using jack detection go into audio mode */
770 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_AUDIO);
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000771
772 mutex_unlock(&wm8994->accdet_lock);
773}
774
775static void active_dereference(struct snd_soc_codec *codec)
776{
777 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
778 u16 mode;
779
780 mutex_lock(&wm8994->accdet_lock);
781
782 wm8994->active_refcount--;
783
784 dev_dbg(codec->dev, "Active refcount decremented, now %d\n",
785 wm8994->active_refcount);
786
787 if (wm8994->active_refcount == 0) {
788 /* Go into appropriate detection only mode */
Mark Brown1defde22012-03-03 20:02:49 +0000789 if (wm8994->jack_mic || wm8994->mic_detecting)
790 mode = WM1811_JACKDET_MODE_MIC;
791 else
792 mode = WM1811_JACKDET_MODE_JACK;
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000793
Mark Brown1defde22012-03-03 20:02:49 +0000794 wm1811_jackdet_set_mode(codec, mode);
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000795 }
796
797 mutex_unlock(&wm8994->accdet_lock);
798}
799
Mark Brown9e6e96a2010-01-29 17:47:12 +0000800static int clk_sys_event(struct snd_soc_dapm_widget *w,
801 struct snd_kcontrol *kcontrol, int event)
802{
803 struct snd_soc_codec *codec = w->codec;
Mark Brown99af79d2012-07-25 23:03:36 +0100804 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000805
806 switch (event) {
807 case SND_SOC_DAPM_PRE_PMU:
808 return configure_clock(codec);
809
Mark Brown99af79d2012-07-25 23:03:36 +0100810 case SND_SOC_DAPM_POST_PMU:
811 /*
812 * JACKDET won't run until we start the clock and it
813 * only reports deltas, make sure we notify the state
814 * up the stack on startup. Use a *very* generous
815 * timeout for paranoia, there's no urgency and we
816 * don't want false reports.
817 */
818 if (wm8994->jackdet && !wm8994->clk_has_run) {
819 schedule_delayed_work(&wm8994->jackdet_bootstrap,
820 msecs_to_jiffies(1000));
821 wm8994->clk_has_run = true;
822 }
823 break;
824
Mark Brown9e6e96a2010-01-29 17:47:12 +0000825 case SND_SOC_DAPM_POST_PMD:
826 configure_clock(codec);
827 break;
828 }
829
830 return 0;
831}
832
Mark Brown4b7ed832011-08-10 17:47:33 +0900833static void vmid_reference(struct snd_soc_codec *codec)
834{
835 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
836
Mark Browndb966f82012-02-06 12:07:08 +0000837 pm_runtime_get_sync(codec->dev);
838
Mark Brown4b7ed832011-08-10 17:47:33 +0900839 wm8994->vmid_refcount++;
840
841 dev_dbg(codec->dev, "Referencing VMID, refcount is now %d\n",
842 wm8994->vmid_refcount);
843
844 if (wm8994->vmid_refcount == 1) {
Mark Browncc6d5a82012-02-11 23:09:53 +0000845 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
Mark Browncc6d5a82012-02-11 23:09:53 +0000846 WM8994_LINEOUT1_DISCH |
Mark Brown22f8d052012-03-19 17:32:06 +0000847 WM8994_LINEOUT2_DISCH, 0);
Mark Browncc6d5a82012-02-11 23:09:53 +0000848
Mark Brownf7085642012-02-21 16:24:00 +0000849 wm_hubs_vmid_ena(codec);
850
Mark Brown22f8d052012-03-19 17:32:06 +0000851 switch (wm8994->vmid_mode) {
852 default:
Mark Browncbd71f32012-05-09 19:11:03 +0100853 WARN_ON(NULL == "Invalid VMID mode");
Mark Brown22f8d052012-03-19 17:32:06 +0000854 case WM8994_VMID_NORMAL:
855 /* Startup bias, VMID ramp & buffer */
856 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
857 WM8994_BIAS_SRC |
858 WM8994_VMID_DISCH |
859 WM8994_STARTUP_BIAS_ENA |
860 WM8994_VMID_BUF_ENA |
861 WM8994_VMID_RAMP_MASK,
862 WM8994_BIAS_SRC |
863 WM8994_STARTUP_BIAS_ENA |
864 WM8994_VMID_BUF_ENA |
865 (0x3 << WM8994_VMID_RAMP_SHIFT));
Mark Brown4b7ed832011-08-10 17:47:33 +0900866
Mark Brown22f8d052012-03-19 17:32:06 +0000867 /* Main bias enable, VMID=2x40k */
868 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
869 WM8994_BIAS_ENA |
870 WM8994_VMID_SEL_MASK,
871 WM8994_BIAS_ENA | 0x2);
Mark Brown4b7ed832011-08-10 17:47:33 +0900872
Mark Brown22f8d052012-03-19 17:32:06 +0000873 msleep(50);
Mark Browncc6d5a82012-02-11 23:09:53 +0000874
Mark Brown22f8d052012-03-19 17:32:06 +0000875 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
876 WM8994_VMID_RAMP_MASK |
877 WM8994_BIAS_SRC,
878 0);
879 break;
880
881 case WM8994_VMID_FORCE:
882 /* Startup bias, slow VMID ramp & buffer */
883 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
884 WM8994_BIAS_SRC |
885 WM8994_VMID_DISCH |
886 WM8994_STARTUP_BIAS_ENA |
887 WM8994_VMID_BUF_ENA |
888 WM8994_VMID_RAMP_MASK,
889 WM8994_BIAS_SRC |
890 WM8994_STARTUP_BIAS_ENA |
891 WM8994_VMID_BUF_ENA |
892 (0x2 << WM8994_VMID_RAMP_SHIFT));
893
894 /* Main bias enable, VMID=2x40k */
895 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
896 WM8994_BIAS_ENA |
897 WM8994_VMID_SEL_MASK,
898 WM8994_BIAS_ENA | 0x2);
899
900 msleep(400);
901
902 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
903 WM8994_VMID_RAMP_MASK |
904 WM8994_BIAS_SRC,
905 0);
906 break;
907 }
Mark Brown4b7ed832011-08-10 17:47:33 +0900908 }
909}
910
911static void vmid_dereference(struct snd_soc_codec *codec)
912{
913 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
914
915 wm8994->vmid_refcount--;
916
917 dev_dbg(codec->dev, "Dereferencing VMID, refcount is now %d\n",
918 wm8994->vmid_refcount);
919
920 if (wm8994->vmid_refcount == 0) {
Mark Brown22f8d052012-03-19 17:32:06 +0000921 if (wm8994->hubs.lineout1_se)
922 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_3,
923 WM8994_LINEOUT1N_ENA |
924 WM8994_LINEOUT1P_ENA,
925 WM8994_LINEOUT1N_ENA |
926 WM8994_LINEOUT1P_ENA);
927
928 if (wm8994->hubs.lineout2_se)
929 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_3,
930 WM8994_LINEOUT2N_ENA |
931 WM8994_LINEOUT2P_ENA,
932 WM8994_LINEOUT2N_ENA |
933 WM8994_LINEOUT2P_ENA);
934
935 /* Start discharging VMID */
Mark Brown4b7ed832011-08-10 17:47:33 +0900936 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
937 WM8994_BIAS_SRC |
Mark Brown22f8d052012-03-19 17:32:06 +0000938 WM8994_VMID_DISCH,
Mark Brown4b7ed832011-08-10 17:47:33 +0900939 WM8994_BIAS_SRC |
Mark Brown22f8d052012-03-19 17:32:06 +0000940 WM8994_VMID_DISCH);
Mark Brown4b7ed832011-08-10 17:47:33 +0900941
Mark Brown22f8d052012-03-19 17:32:06 +0000942 switch (wm8994->vmid_mode) {
943 case WM8994_VMID_FORCE:
944 msleep(350);
945 break;
946 default:
947 break;
948 }
Mark Brown4b7ed832011-08-10 17:47:33 +0900949
Mark Brown22f8d052012-03-19 17:32:06 +0000950 snd_soc_update_bits(codec, WM8994_ADDITIONAL_CONTROL,
951 WM8994_VROI, WM8994_VROI);
Mark Browne85b26c2012-02-11 23:10:30 +0000952
Mark Brown22f8d052012-03-19 17:32:06 +0000953 /* Active discharge */
Mark Brown4b7ed832011-08-10 17:47:33 +0900954 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
955 WM8994_LINEOUT1_DISCH |
956 WM8994_LINEOUT2_DISCH,
957 WM8994_LINEOUT1_DISCH |
958 WM8994_LINEOUT2_DISCH);
959
Mark Brown22f8d052012-03-19 17:32:06 +0000960 msleep(150);
961
962 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_3,
963 WM8994_LINEOUT1N_ENA |
964 WM8994_LINEOUT1P_ENA |
965 WM8994_LINEOUT2N_ENA |
966 WM8994_LINEOUT2P_ENA, 0);
967
968 snd_soc_update_bits(codec, WM8994_ADDITIONAL_CONTROL,
969 WM8994_VROI, 0);
Mark Brown4b7ed832011-08-10 17:47:33 +0900970
971 /* Switch off startup biases */
972 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
973 WM8994_BIAS_SRC |
974 WM8994_STARTUP_BIAS_ENA |
975 WM8994_VMID_BUF_ENA |
976 WM8994_VMID_RAMP_MASK, 0);
Mark Brown22f8d052012-03-19 17:32:06 +0000977
978 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
979 WM8994_BIAS_ENA | WM8994_VMID_SEL_MASK, 0);
980
981 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
982 WM8994_VMID_RAMP_MASK, 0);
Mark Brown4b7ed832011-08-10 17:47:33 +0900983 }
Mark Browndb966f82012-02-06 12:07:08 +0000984
985 pm_runtime_put(codec->dev);
Mark Brown4b7ed832011-08-10 17:47:33 +0900986}
987
988static int vmid_event(struct snd_soc_dapm_widget *w,
989 struct snd_kcontrol *kcontrol, int event)
990{
991 struct snd_soc_codec *codec = w->codec;
992
993 switch (event) {
994 case SND_SOC_DAPM_PRE_PMU:
995 vmid_reference(codec);
996 break;
997
998 case SND_SOC_DAPM_POST_PMD:
999 vmid_dereference(codec);
1000 break;
1001 }
1002
1003 return 0;
1004}
1005
Mark Brownc3403042012-04-26 21:29:29 +01001006static bool wm8994_check_class_w_digital(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00001007{
Mark Brown9e6e96a2010-01-29 17:47:12 +00001008 int source = 0; /* GCC flow analysis can't track enable */
1009 int reg, reg_r;
1010
Mark Brownc3403042012-04-26 21:29:29 +01001011 /* We also need the same AIF source for L/R and only one path */
Mark Brown9e6e96a2010-01-29 17:47:12 +00001012 reg = snd_soc_read(codec, WM8994_DAC1_LEFT_MIXER_ROUTING);
1013 switch (reg) {
1014 case WM8994_AIF2DACL_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +09001015 dev_vdbg(codec->dev, "Class W source AIF2DAC\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +00001016 source = 2 << WM8994_CP_DYN_SRC_SEL_SHIFT;
1017 break;
1018 case WM8994_AIF1DAC2L_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +09001019 dev_vdbg(codec->dev, "Class W source AIF1DAC2\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +00001020 source = 1 << WM8994_CP_DYN_SRC_SEL_SHIFT;
1021 break;
1022 case WM8994_AIF1DAC1L_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +09001023 dev_vdbg(codec->dev, "Class W source AIF1DAC1\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +00001024 source = 0 << WM8994_CP_DYN_SRC_SEL_SHIFT;
1025 break;
1026 default:
Mark Brownee839a22010-04-20 13:57:08 +09001027 dev_vdbg(codec->dev, "DAC mixer setting: %x\n", reg);
Mark Brownc3403042012-04-26 21:29:29 +01001028 return false;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001029 }
1030
1031 reg_r = snd_soc_read(codec, WM8994_DAC1_RIGHT_MIXER_ROUTING);
1032 if (reg_r != reg) {
Mark Brownee839a22010-04-20 13:57:08 +09001033 dev_vdbg(codec->dev, "Left and right DAC mixers different\n");
Mark Brownc3403042012-04-26 21:29:29 +01001034 return false;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001035 }
1036
Mark Brownc3403042012-04-26 21:29:29 +01001037 /* Set the source up */
1038 snd_soc_update_bits(codec, WM8994_CLASS_W_1,
1039 WM8994_CP_DYN_SRC_SEL_MASK, source);
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02001040
Mark Brownc3403042012-04-26 21:29:29 +01001041 return true;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001042}
1043
Mark Brown1a383362012-04-12 19:47:11 +01001044static int aif1clk_ev(struct snd_soc_dapm_widget *w,
1045 struct snd_kcontrol *kcontrol, int event)
1046{
1047 struct snd_soc_codec *codec = w->codec;
1048 struct wm8994 *control = codec->control_data;
1049 int mask = WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC1R_ENA;
Mark Brownbfd37bb2012-06-05 12:31:32 +01001050 int i;
Mark Brown1a383362012-04-12 19:47:11 +01001051 int dac;
1052 int adc;
1053 int val;
1054
1055 switch (control->type) {
1056 case WM8994:
1057 case WM8958:
1058 mask |= WM8994_AIF1DAC2L_ENA | WM8994_AIF1DAC2R_ENA;
1059 break;
1060 default:
1061 break;
1062 }
1063
1064 switch (event) {
1065 case SND_SOC_DAPM_PRE_PMU:
1066 val = snd_soc_read(codec, WM8994_AIF1_CONTROL_1);
1067 if ((val & WM8994_AIF1ADCL_SRC) &&
1068 (val & WM8994_AIF1ADCR_SRC))
1069 adc = WM8994_AIF1ADC1R_ENA | WM8994_AIF1ADC2R_ENA;
1070 else if (!(val & WM8994_AIF1ADCL_SRC) &&
1071 !(val & WM8994_AIF1ADCR_SRC))
1072 adc = WM8994_AIF1ADC1L_ENA | WM8994_AIF1ADC2L_ENA;
1073 else
1074 adc = WM8994_AIF1ADC1R_ENA | WM8994_AIF1ADC2R_ENA |
1075 WM8994_AIF1ADC1L_ENA | WM8994_AIF1ADC2L_ENA;
1076
1077 val = snd_soc_read(codec, WM8994_AIF1_CONTROL_2);
1078 if ((val & WM8994_AIF1DACL_SRC) &&
1079 (val & WM8994_AIF1DACR_SRC))
1080 dac = WM8994_AIF1DAC1R_ENA | WM8994_AIF1DAC2R_ENA;
1081 else if (!(val & WM8994_AIF1DACL_SRC) &&
1082 !(val & WM8994_AIF1DACR_SRC))
1083 dac = WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC2L_ENA;
1084 else
1085 dac = WM8994_AIF1DAC1R_ENA | WM8994_AIF1DAC2R_ENA |
1086 WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC2L_ENA;
1087
1088 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
1089 mask, adc);
1090 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1091 mask, dac);
1092 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
1093 WM8994_AIF1DSPCLK_ENA |
1094 WM8994_SYSDSPCLK_ENA,
1095 WM8994_AIF1DSPCLK_ENA |
1096 WM8994_SYSDSPCLK_ENA);
1097 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4, mask,
1098 WM8994_AIF1ADC1R_ENA |
1099 WM8994_AIF1ADC1L_ENA |
1100 WM8994_AIF1ADC2R_ENA |
1101 WM8994_AIF1ADC2L_ENA);
1102 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5, mask,
1103 WM8994_AIF1DAC1R_ENA |
1104 WM8994_AIF1DAC1L_ENA |
1105 WM8994_AIF1DAC2R_ENA |
1106 WM8994_AIF1DAC2L_ENA);
1107 break;
1108
Mark Brownbfd37bb2012-06-05 12:31:32 +01001109 case SND_SOC_DAPM_POST_PMU:
1110 for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
1111 snd_soc_write(codec, wm8994_vu_bits[i].reg,
1112 snd_soc_read(codec,
1113 wm8994_vu_bits[i].reg));
1114 break;
1115
Mark Brown1a383362012-04-12 19:47:11 +01001116 case SND_SOC_DAPM_PRE_PMD:
1117 case SND_SOC_DAPM_POST_PMD:
1118 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1119 mask, 0);
1120 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
1121 mask, 0);
1122
1123 val = snd_soc_read(codec, WM8994_CLOCKING_1);
1124 if (val & WM8994_AIF2DSPCLK_ENA)
1125 val = WM8994_SYSDSPCLK_ENA;
1126 else
1127 val = 0;
1128 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
1129 WM8994_SYSDSPCLK_ENA |
1130 WM8994_AIF1DSPCLK_ENA, val);
1131 break;
1132 }
1133
1134 return 0;
1135}
1136
1137static int aif2clk_ev(struct snd_soc_dapm_widget *w,
1138 struct snd_kcontrol *kcontrol, int event)
1139{
1140 struct snd_soc_codec *codec = w->codec;
Mark Brownbfd37bb2012-06-05 12:31:32 +01001141 int i;
Mark Brown1a383362012-04-12 19:47:11 +01001142 int dac;
1143 int adc;
1144 int val;
1145
1146 switch (event) {
1147 case SND_SOC_DAPM_PRE_PMU:
1148 val = snd_soc_read(codec, WM8994_AIF2_CONTROL_1);
1149 if ((val & WM8994_AIF2ADCL_SRC) &&
1150 (val & WM8994_AIF2ADCR_SRC))
1151 adc = WM8994_AIF2ADCR_ENA;
1152 else if (!(val & WM8994_AIF2ADCL_SRC) &&
1153 !(val & WM8994_AIF2ADCR_SRC))
1154 adc = WM8994_AIF2ADCL_ENA;
1155 else
1156 adc = WM8994_AIF2ADCL_ENA | WM8994_AIF2ADCR_ENA;
1157
1158
1159 val = snd_soc_read(codec, WM8994_AIF2_CONTROL_2);
1160 if ((val & WM8994_AIF2DACL_SRC) &&
1161 (val & WM8994_AIF2DACR_SRC))
1162 dac = WM8994_AIF2DACR_ENA;
1163 else if (!(val & WM8994_AIF2DACL_SRC) &&
1164 !(val & WM8994_AIF2DACR_SRC))
1165 dac = WM8994_AIF2DACL_ENA;
1166 else
1167 dac = WM8994_AIF2DACL_ENA | WM8994_AIF2DACR_ENA;
1168
1169 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
1170 WM8994_AIF2ADCL_ENA |
1171 WM8994_AIF2ADCR_ENA, adc);
1172 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1173 WM8994_AIF2DACL_ENA |
1174 WM8994_AIF2DACR_ENA, dac);
1175 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
1176 WM8994_AIF2DSPCLK_ENA |
1177 WM8994_SYSDSPCLK_ENA,
1178 WM8994_AIF2DSPCLK_ENA |
1179 WM8994_SYSDSPCLK_ENA);
1180 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
1181 WM8994_AIF2ADCL_ENA |
1182 WM8994_AIF2ADCR_ENA,
1183 WM8994_AIF2ADCL_ENA |
1184 WM8994_AIF2ADCR_ENA);
1185 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1186 WM8994_AIF2DACL_ENA |
1187 WM8994_AIF2DACR_ENA,
1188 WM8994_AIF2DACL_ENA |
1189 WM8994_AIF2DACR_ENA);
1190 break;
1191
Mark Brownbfd37bb2012-06-05 12:31:32 +01001192 case SND_SOC_DAPM_POST_PMU:
1193 for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
1194 snd_soc_write(codec, wm8994_vu_bits[i].reg,
1195 snd_soc_read(codec,
1196 wm8994_vu_bits[i].reg));
1197 break;
1198
Mark Brown1a383362012-04-12 19:47:11 +01001199 case SND_SOC_DAPM_PRE_PMD:
1200 case SND_SOC_DAPM_POST_PMD:
1201 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1202 WM8994_AIF2DACL_ENA |
1203 WM8994_AIF2DACR_ENA, 0);
Mark Brownc7f5f232012-05-15 18:13:00 +01001204 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
Mark Brown1a383362012-04-12 19:47:11 +01001205 WM8994_AIF2ADCL_ENA |
1206 WM8994_AIF2ADCR_ENA, 0);
1207
1208 val = snd_soc_read(codec, WM8994_CLOCKING_1);
1209 if (val & WM8994_AIF1DSPCLK_ENA)
1210 val = WM8994_SYSDSPCLK_ENA;
1211 else
1212 val = 0;
1213 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
1214 WM8994_SYSDSPCLK_ENA |
1215 WM8994_AIF2DSPCLK_ENA, val);
1216 break;
1217 }
1218
1219 return 0;
1220}
1221
1222static int aif1clk_late_ev(struct snd_soc_dapm_widget *w,
1223 struct snd_kcontrol *kcontrol, int event)
1224{
1225 struct snd_soc_codec *codec = w->codec;
1226 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1227
1228 switch (event) {
1229 case SND_SOC_DAPM_PRE_PMU:
1230 wm8994->aif1clk_enable = 1;
1231 break;
1232 case SND_SOC_DAPM_POST_PMD:
1233 wm8994->aif1clk_disable = 1;
1234 break;
1235 }
1236
1237 return 0;
1238}
1239
1240static int aif2clk_late_ev(struct snd_soc_dapm_widget *w,
1241 struct snd_kcontrol *kcontrol, int event)
1242{
1243 struct snd_soc_codec *codec = w->codec;
1244 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1245
1246 switch (event) {
1247 case SND_SOC_DAPM_PRE_PMU:
1248 wm8994->aif2clk_enable = 1;
1249 break;
1250 case SND_SOC_DAPM_POST_PMD:
1251 wm8994->aif2clk_disable = 1;
1252 break;
1253 }
1254
1255 return 0;
1256}
1257
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001258static int late_enable_ev(struct snd_soc_dapm_widget *w,
1259 struct snd_kcontrol *kcontrol, int event)
1260{
1261 struct snd_soc_codec *codec = w->codec;
1262 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1263
1264 switch (event) {
1265 case SND_SOC_DAPM_PRE_PMU:
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001266 if (wm8994->aif1clk_enable) {
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001267 aif1clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMU);
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001268 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1269 WM8994_AIF1CLK_ENA_MASK,
1270 WM8994_AIF1CLK_ENA);
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001271 aif1clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMU);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001272 wm8994->aif1clk_enable = 0;
1273 }
1274 if (wm8994->aif2clk_enable) {
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001275 aif2clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMU);
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001276 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1277 WM8994_AIF2CLK_ENA_MASK,
1278 WM8994_AIF2CLK_ENA);
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001279 aif2clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMU);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001280 wm8994->aif2clk_enable = 0;
1281 }
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001282 break;
1283 }
1284
Mark Brownc6b7b572011-03-11 18:13:12 +00001285 /* We may also have postponed startup of DSP, handle that. */
1286 wm8958_aif_ev(w, kcontrol, event);
1287
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001288 return 0;
1289}
1290
1291static int late_disable_ev(struct snd_soc_dapm_widget *w,
1292 struct snd_kcontrol *kcontrol, int event)
1293{
1294 struct snd_soc_codec *codec = w->codec;
1295 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1296
1297 switch (event) {
1298 case SND_SOC_DAPM_POST_PMD:
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001299 if (wm8994->aif1clk_disable) {
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001300 aif1clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMD);
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001301 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1302 WM8994_AIF1CLK_ENA_MASK, 0);
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001303 aif1clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMD);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001304 wm8994->aif1clk_disable = 0;
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001305 }
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001306 if (wm8994->aif2clk_disable) {
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001307 aif2clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMD);
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001308 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1309 WM8994_AIF2CLK_ENA_MASK, 0);
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001310 aif2clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMD);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001311 wm8994->aif2clk_disable = 0;
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001312 }
1313 break;
1314 }
1315
1316 return 0;
1317}
1318
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001319static int adc_mux_ev(struct snd_soc_dapm_widget *w,
1320 struct snd_kcontrol *kcontrol, int event)
1321{
1322 late_enable_ev(w, kcontrol, event);
1323 return 0;
1324}
1325
Dimitris Papastamosb462c6e2011-03-01 12:54:39 +00001326static int micbias_ev(struct snd_soc_dapm_widget *w,
1327 struct snd_kcontrol *kcontrol, int event)
1328{
1329 late_enable_ev(w, kcontrol, event);
1330 return 0;
1331}
1332
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001333static int dac_ev(struct snd_soc_dapm_widget *w,
1334 struct snd_kcontrol *kcontrol, int event)
1335{
1336 struct snd_soc_codec *codec = w->codec;
1337 unsigned int mask = 1 << w->shift;
1338
1339 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1340 mask, mask);
1341 return 0;
1342}
1343
Mark Brown9e6e96a2010-01-29 17:47:12 +00001344static const char *adc_mux_text[] = {
1345 "ADC",
1346 "DMIC",
1347};
1348
1349static const struct soc_enum adc_enum =
1350 SOC_ENUM_SINGLE(0, 0, 2, adc_mux_text);
1351
1352static const struct snd_kcontrol_new adcl_mux =
1353 SOC_DAPM_ENUM_VIRT("ADCL Mux", adc_enum);
1354
1355static const struct snd_kcontrol_new adcr_mux =
1356 SOC_DAPM_ENUM_VIRT("ADCR Mux", adc_enum);
1357
1358static const struct snd_kcontrol_new left_speaker_mixer[] = {
1359SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 9, 1, 0),
1360SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 7, 1, 0),
1361SOC_DAPM_SINGLE("IN1LP Switch", WM8994_SPEAKER_MIXER, 5, 1, 0),
1362SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 3, 1, 0),
1363SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 1, 1, 0),
1364};
1365
1366static const struct snd_kcontrol_new right_speaker_mixer[] = {
1367SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 8, 1, 0),
1368SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 6, 1, 0),
1369SOC_DAPM_SINGLE("IN1RP Switch", WM8994_SPEAKER_MIXER, 4, 1, 0),
1370SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 2, 1, 0),
1371SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 0, 1, 0),
1372};
1373
1374/* Debugging; dump chip status after DAPM transitions */
1375static int post_ev(struct snd_soc_dapm_widget *w,
1376 struct snd_kcontrol *kcontrol, int event)
1377{
1378 struct snd_soc_codec *codec = w->codec;
1379 dev_dbg(codec->dev, "SRC status: %x\n",
1380 snd_soc_read(codec,
1381 WM8994_RATE_STATUS));
1382 return 0;
1383}
1384
1385static const struct snd_kcontrol_new aif1adc1l_mix[] = {
1386SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1387 1, 1, 0),
1388SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1389 0, 1, 0),
1390};
1391
1392static const struct snd_kcontrol_new aif1adc1r_mix[] = {
1393SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1394 1, 1, 0),
1395SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1396 0, 1, 0),
1397};
1398
Mark Browna3257ba2010-07-19 14:02:34 +01001399static const struct snd_kcontrol_new aif1adc2l_mix[] = {
1400SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1401 1, 1, 0),
1402SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1403 0, 1, 0),
1404};
1405
1406static const struct snd_kcontrol_new aif1adc2r_mix[] = {
1407SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1408 1, 1, 0),
1409SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1410 0, 1, 0),
1411};
1412
Mark Brown9e6e96a2010-01-29 17:47:12 +00001413static const struct snd_kcontrol_new aif2dac2l_mix[] = {
1414SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1415 5, 1, 0),
1416SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1417 4, 1, 0),
1418SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1419 2, 1, 0),
1420SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1421 1, 1, 0),
1422SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1423 0, 1, 0),
1424};
1425
1426static const struct snd_kcontrol_new aif2dac2r_mix[] = {
1427SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1428 5, 1, 0),
1429SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1430 4, 1, 0),
1431SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1432 2, 1, 0),
1433SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1434 1, 1, 0),
1435SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1436 0, 1, 0),
1437};
1438
1439#define WM8994_CLASS_W_SWITCH(xname, reg, shift, max, invert) \
1440{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1441 .info = snd_soc_info_volsw, \
1442 .get = snd_soc_dapm_get_volsw, .put = wm8994_put_class_w, \
1443 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
1444
1445static int wm8994_put_class_w(struct snd_kcontrol *kcontrol,
1446 struct snd_ctl_elem_value *ucontrol)
1447{
Jarkko Nikula9d035452011-05-13 19:16:52 +03001448 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1449 struct snd_soc_dapm_widget *w = wlist->widgets[0];
Mark Brown9e6e96a2010-01-29 17:47:12 +00001450 struct snd_soc_codec *codec = w->codec;
1451 int ret;
1452
1453 ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
1454
Mark Brownc3403042012-04-26 21:29:29 +01001455 wm_hubs_update_class_w(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001456
1457 return ret;
1458}
1459
1460static const struct snd_kcontrol_new dac1l_mix[] = {
1461WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1462 5, 1, 0),
1463WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1464 4, 1, 0),
1465WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1466 2, 1, 0),
1467WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1468 1, 1, 0),
1469WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1470 0, 1, 0),
1471};
1472
1473static const struct snd_kcontrol_new dac1r_mix[] = {
1474WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1475 5, 1, 0),
1476WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1477 4, 1, 0),
1478WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1479 2, 1, 0),
1480WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1481 1, 1, 0),
1482WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1483 0, 1, 0),
1484};
1485
1486static const char *sidetone_text[] = {
1487 "ADC/DMIC1", "DMIC2",
1488};
1489
1490static const struct soc_enum sidetone1_enum =
1491 SOC_ENUM_SINGLE(WM8994_SIDETONE, 0, 2, sidetone_text);
1492
1493static const struct snd_kcontrol_new sidetone1_mux =
1494 SOC_DAPM_ENUM("Left Sidetone Mux", sidetone1_enum);
1495
1496static const struct soc_enum sidetone2_enum =
1497 SOC_ENUM_SINGLE(WM8994_SIDETONE, 1, 2, sidetone_text);
1498
1499static const struct snd_kcontrol_new sidetone2_mux =
1500 SOC_DAPM_ENUM("Right Sidetone Mux", sidetone2_enum);
1501
1502static const char *aif1dac_text[] = {
1503 "AIF1DACDAT", "AIF3DACDAT",
1504};
1505
1506static const struct soc_enum aif1dac_enum =
1507 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 0, 2, aif1dac_text);
1508
1509static const struct snd_kcontrol_new aif1dac_mux =
1510 SOC_DAPM_ENUM("AIF1DAC Mux", aif1dac_enum);
1511
1512static const char *aif2dac_text[] = {
1513 "AIF2DACDAT", "AIF3DACDAT",
1514};
1515
1516static const struct soc_enum aif2dac_enum =
1517 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 1, 2, aif2dac_text);
1518
1519static const struct snd_kcontrol_new aif2dac_mux =
1520 SOC_DAPM_ENUM("AIF2DAC Mux", aif2dac_enum);
1521
1522static const char *aif2adc_text[] = {
1523 "AIF2ADCDAT", "AIF3DACDAT",
1524};
1525
1526static const struct soc_enum aif2adc_enum =
1527 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 2, 2, aif2adc_text);
1528
1529static const struct snd_kcontrol_new aif2adc_mux =
1530 SOC_DAPM_ENUM("AIF2ADC Mux", aif2adc_enum);
1531
1532static const char *aif3adc_text[] = {
Mark Brownc4431df2010-11-26 15:21:07 +00001533 "AIF1ADCDAT", "AIF2ADCDAT", "AIF2DACDAT", "Mono PCM",
Mark Brown9e6e96a2010-01-29 17:47:12 +00001534};
1535
Mark Brownc4431df2010-11-26 15:21:07 +00001536static const struct soc_enum wm8994_aif3adc_enum =
Mark Brown9e6e96a2010-01-29 17:47:12 +00001537 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 3, 3, aif3adc_text);
1538
Mark Brownc4431df2010-11-26 15:21:07 +00001539static const struct snd_kcontrol_new wm8994_aif3adc_mux =
1540 SOC_DAPM_ENUM("AIF3ADC Mux", wm8994_aif3adc_enum);
1541
1542static const struct soc_enum wm8958_aif3adc_enum =
1543 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 3, 4, aif3adc_text);
1544
1545static const struct snd_kcontrol_new wm8958_aif3adc_mux =
1546 SOC_DAPM_ENUM("AIF3ADC Mux", wm8958_aif3adc_enum);
1547
1548static const char *mono_pcm_out_text[] = {
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02001549 "None", "AIF2ADCL", "AIF2ADCR",
Mark Brownc4431df2010-11-26 15:21:07 +00001550};
1551
1552static const struct soc_enum mono_pcm_out_enum =
1553 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 9, 3, mono_pcm_out_text);
1554
1555static const struct snd_kcontrol_new mono_pcm_out_mux =
1556 SOC_DAPM_ENUM("Mono PCM Out Mux", mono_pcm_out_enum);
1557
1558static const char *aif2dac_src_text[] = {
1559 "AIF2", "AIF3",
1560};
1561
1562/* Note that these two control shouldn't be simultaneously switched to AIF3 */
1563static const struct soc_enum aif2dacl_src_enum =
1564 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 7, 2, aif2dac_src_text);
1565
1566static const struct snd_kcontrol_new aif2dacl_src_mux =
1567 SOC_DAPM_ENUM("AIF2DACL Mux", aif2dacl_src_enum);
1568
1569static const struct soc_enum aif2dacr_src_enum =
1570 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 8, 2, aif2dac_src_text);
1571
1572static const struct snd_kcontrol_new aif2dacr_src_mux =
1573 SOC_DAPM_ENUM("AIF2DACR Mux", aif2dacr_src_enum);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001574
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001575static const struct snd_soc_dapm_widget wm8994_lateclk_revd_widgets[] = {
Mark Brown1a383362012-04-12 19:47:11 +01001576SND_SOC_DAPM_SUPPLY("AIF1CLK", SND_SOC_NOPM, 0, 0, aif1clk_late_ev,
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001577 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown1a383362012-04-12 19:47:11 +01001578SND_SOC_DAPM_SUPPLY("AIF2CLK", SND_SOC_NOPM, 0, 0, aif2clk_late_ev,
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001579 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1580
1581SND_SOC_DAPM_PGA_E("Late DAC1L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1582 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1583SND_SOC_DAPM_PGA_E("Late DAC1R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1584 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1585SND_SOC_DAPM_PGA_E("Late DAC2L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1586 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1587SND_SOC_DAPM_PGA_E("Late DAC2R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1588 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Mark Brownb70a51b2011-06-29 00:21:09 -07001589SND_SOC_DAPM_PGA_E("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0,
1590 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1591
1592SND_SOC_DAPM_MIXER_E("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
1593 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer),
1594 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1595SND_SOC_DAPM_MIXER_E("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
1596 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer),
1597 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Mark Brownc3403042012-04-26 21:29:29 +01001598SND_SOC_DAPM_MUX_E("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpl_mux,
Mark Brownb70a51b2011-06-29 00:21:09 -07001599 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Mark Brownc3403042012-04-26 21:29:29 +01001600SND_SOC_DAPM_MUX_E("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpr_mux,
Mark Brownb70a51b2011-06-29 00:21:09 -07001601 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001602
1603SND_SOC_DAPM_POST("Late Disable PGA", late_disable_ev)
1604};
1605
1606static const struct snd_soc_dapm_widget wm8994_lateclk_widgets[] = {
Mark Brown1a383362012-04-12 19:47:11 +01001607SND_SOC_DAPM_SUPPLY("AIF1CLK", WM8994_AIF1_CLOCKING_1, 0, 0, aif1clk_ev,
Mark Brownbfd37bb2012-06-05 12:31:32 +01001608 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
1609 SND_SOC_DAPM_PRE_PMD),
Mark Brown1a383362012-04-12 19:47:11 +01001610SND_SOC_DAPM_SUPPLY("AIF2CLK", WM8994_AIF2_CLOCKING_1, 0, 0, aif2clk_ev,
Mark Brownbfd37bb2012-06-05 12:31:32 +01001611 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
1612 SND_SOC_DAPM_PRE_PMD),
Mark Brownb70a51b2011-06-29 00:21:09 -07001613SND_SOC_DAPM_PGA("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0),
1614SND_SOC_DAPM_MIXER("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
1615 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
1616SND_SOC_DAPM_MIXER("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
1617 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
Mark Brownc3403042012-04-26 21:29:29 +01001618SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpl_mux),
1619SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpr_mux),
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001620};
1621
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001622static const struct snd_soc_dapm_widget wm8994_dac_revd_widgets[] = {
1623SND_SOC_DAPM_DAC_E("DAC2L", NULL, SND_SOC_NOPM, 3, 0,
1624 dac_ev, SND_SOC_DAPM_PRE_PMU),
1625SND_SOC_DAPM_DAC_E("DAC2R", NULL, SND_SOC_NOPM, 2, 0,
1626 dac_ev, SND_SOC_DAPM_PRE_PMU),
1627SND_SOC_DAPM_DAC_E("DAC1L", NULL, SND_SOC_NOPM, 1, 0,
1628 dac_ev, SND_SOC_DAPM_PRE_PMU),
1629SND_SOC_DAPM_DAC_E("DAC1R", NULL, SND_SOC_NOPM, 0, 0,
1630 dac_ev, SND_SOC_DAPM_PRE_PMU),
1631};
1632
1633static const struct snd_soc_dapm_widget wm8994_dac_widgets[] = {
1634SND_SOC_DAPM_DAC("DAC2L", NULL, WM8994_POWER_MANAGEMENT_5, 3, 0),
Mark Brown0627bd22011-03-09 19:09:17 +00001635SND_SOC_DAPM_DAC("DAC2R", NULL, WM8994_POWER_MANAGEMENT_5, 2, 0),
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001636SND_SOC_DAPM_DAC("DAC1L", NULL, WM8994_POWER_MANAGEMENT_5, 1, 0),
1637SND_SOC_DAPM_DAC("DAC1R", NULL, WM8994_POWER_MANAGEMENT_5, 0, 0),
1638};
1639
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001640static const struct snd_soc_dapm_widget wm8994_adc_revd_widgets[] = {
Mark Brown87b86ad2011-08-14 13:39:20 +09001641SND_SOC_DAPM_VIRT_MUX_E("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux,
1642 adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
1643SND_SOC_DAPM_VIRT_MUX_E("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux,
1644 adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001645};
1646
1647static const struct snd_soc_dapm_widget wm8994_adc_widgets[] = {
Mark Brown87b86ad2011-08-14 13:39:20 +09001648SND_SOC_DAPM_VIRT_MUX("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux),
1649SND_SOC_DAPM_VIRT_MUX("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux),
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001650};
1651
Mark Brown9e6e96a2010-01-29 17:47:12 +00001652static const struct snd_soc_dapm_widget wm8994_dapm_widgets[] = {
1653SND_SOC_DAPM_INPUT("DMIC1DAT"),
1654SND_SOC_DAPM_INPUT("DMIC2DAT"),
Mark Brown66b47fd2010-07-08 11:25:43 +09001655SND_SOC_DAPM_INPUT("Clock"),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001656
Dimitris Papastamosb462c6e2011-03-01 12:54:39 +00001657SND_SOC_DAPM_SUPPLY_S("MICBIAS Supply", 1, SND_SOC_NOPM, 0, 0, micbias_ev,
1658 SND_SOC_DAPM_PRE_PMU),
Mark Brown4b7ed832011-08-10 17:47:33 +09001659SND_SOC_DAPM_SUPPLY("VMID", SND_SOC_NOPM, 0, 0, vmid_event,
1660 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
Dimitris Papastamosb462c6e2011-03-01 12:54:39 +00001661
Mark Brown9e6e96a2010-01-29 17:47:12 +00001662SND_SOC_DAPM_SUPPLY("CLK_SYS", SND_SOC_NOPM, 0, 0, clk_sys_event,
Mark Brown99af79d2012-07-25 23:03:36 +01001663 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
1664 SND_SOC_DAPM_PRE_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001665
Mark Brown1a383362012-04-12 19:47:11 +01001666SND_SOC_DAPM_SUPPLY("DSP1CLK", SND_SOC_NOPM, 3, 0, NULL, 0),
1667SND_SOC_DAPM_SUPPLY("DSP2CLK", SND_SOC_NOPM, 2, 0, NULL, 0),
1668SND_SOC_DAPM_SUPPLY("DSPINTCLK", SND_SOC_NOPM, 1, 0, NULL, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001669
Mark Brown7f94de42011-02-03 16:27:34 +00001670SND_SOC_DAPM_AIF_OUT("AIF1ADC1L", NULL,
Mark Brown1a383362012-04-12 19:47:11 +01001671 0, SND_SOC_NOPM, 9, 0),
Mark Brown7f94de42011-02-03 16:27:34 +00001672SND_SOC_DAPM_AIF_OUT("AIF1ADC1R", NULL,
Mark Brown1a383362012-04-12 19:47:11 +01001673 0, SND_SOC_NOPM, 8, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001674SND_SOC_DAPM_AIF_IN_E("AIF1DAC1L", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001675 SND_SOC_NOPM, 9, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001676 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brownd6addcc2010-11-26 15:21:08 +00001677SND_SOC_DAPM_AIF_IN_E("AIF1DAC1R", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001678 SND_SOC_NOPM, 8, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001679 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001680
Mark Brown7f94de42011-02-03 16:27:34 +00001681SND_SOC_DAPM_AIF_OUT("AIF1ADC2L", NULL,
Mark Brown1a383362012-04-12 19:47:11 +01001682 0, SND_SOC_NOPM, 11, 0),
Mark Brown7f94de42011-02-03 16:27:34 +00001683SND_SOC_DAPM_AIF_OUT("AIF1ADC2R", NULL,
Mark Brown1a383362012-04-12 19:47:11 +01001684 0, SND_SOC_NOPM, 10, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001685SND_SOC_DAPM_AIF_IN_E("AIF1DAC2L", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001686 SND_SOC_NOPM, 11, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001687 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brownd6addcc2010-11-26 15:21:08 +00001688SND_SOC_DAPM_AIF_IN_E("AIF1DAC2R", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001689 SND_SOC_NOPM, 10, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001690 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001691
1692SND_SOC_DAPM_MIXER("AIF1ADC1L Mixer", SND_SOC_NOPM, 0, 0,
1693 aif1adc1l_mix, ARRAY_SIZE(aif1adc1l_mix)),
1694SND_SOC_DAPM_MIXER("AIF1ADC1R Mixer", SND_SOC_NOPM, 0, 0,
1695 aif1adc1r_mix, ARRAY_SIZE(aif1adc1r_mix)),
1696
Mark Browna3257ba2010-07-19 14:02:34 +01001697SND_SOC_DAPM_MIXER("AIF1ADC2L Mixer", SND_SOC_NOPM, 0, 0,
1698 aif1adc2l_mix, ARRAY_SIZE(aif1adc2l_mix)),
1699SND_SOC_DAPM_MIXER("AIF1ADC2R Mixer", SND_SOC_NOPM, 0, 0,
1700 aif1adc2r_mix, ARRAY_SIZE(aif1adc2r_mix)),
1701
Mark Brown9e6e96a2010-01-29 17:47:12 +00001702SND_SOC_DAPM_MIXER("AIF2DAC2L Mixer", SND_SOC_NOPM, 0, 0,
1703 aif2dac2l_mix, ARRAY_SIZE(aif2dac2l_mix)),
1704SND_SOC_DAPM_MIXER("AIF2DAC2R Mixer", SND_SOC_NOPM, 0, 0,
1705 aif2dac2r_mix, ARRAY_SIZE(aif2dac2r_mix)),
1706
1707SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &sidetone1_mux),
1708SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &sidetone2_mux),
1709
1710SND_SOC_DAPM_MIXER("DAC1L Mixer", SND_SOC_NOPM, 0, 0,
1711 dac1l_mix, ARRAY_SIZE(dac1l_mix)),
1712SND_SOC_DAPM_MIXER("DAC1R Mixer", SND_SOC_NOPM, 0, 0,
1713 dac1r_mix, ARRAY_SIZE(dac1r_mix)),
1714
1715SND_SOC_DAPM_AIF_OUT("AIF2ADCL", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001716 SND_SOC_NOPM, 13, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001717SND_SOC_DAPM_AIF_OUT("AIF2ADCR", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001718 SND_SOC_NOPM, 12, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001719SND_SOC_DAPM_AIF_IN_E("AIF2DACL", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001720 SND_SOC_NOPM, 13, 0, wm8958_aif_ev,
Mark Brownd6addcc2010-11-26 15:21:08 +00001721 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1722SND_SOC_DAPM_AIF_IN_E("AIF2DACR", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001723 SND_SOC_NOPM, 12, 0, wm8958_aif_ev,
Mark Brownd6addcc2010-11-26 15:21:08 +00001724 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001725
Mark Brown5567d8c2012-02-16 21:43:29 -08001726SND_SOC_DAPM_AIF_IN("AIF1DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1727SND_SOC_DAPM_AIF_IN("AIF2DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1728SND_SOC_DAPM_AIF_OUT("AIF1ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1729SND_SOC_DAPM_AIF_OUT("AIF2ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001730
1731SND_SOC_DAPM_MUX("AIF1DAC Mux", SND_SOC_NOPM, 0, 0, &aif1dac_mux),
1732SND_SOC_DAPM_MUX("AIF2DAC Mux", SND_SOC_NOPM, 0, 0, &aif2dac_mux),
1733SND_SOC_DAPM_MUX("AIF2ADC Mux", SND_SOC_NOPM, 0, 0, &aif2adc_mux),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001734
Mark Brown5567d8c2012-02-16 21:43:29 -08001735SND_SOC_DAPM_AIF_IN("AIF3DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1736SND_SOC_DAPM_AIF_OUT("AIF3ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001737
1738SND_SOC_DAPM_SUPPLY("TOCLK", WM8994_CLOCKING_1, 4, 0, NULL, 0),
1739
1740SND_SOC_DAPM_ADC("DMIC2L", NULL, WM8994_POWER_MANAGEMENT_4, 5, 0),
1741SND_SOC_DAPM_ADC("DMIC2R", NULL, WM8994_POWER_MANAGEMENT_4, 4, 0),
1742SND_SOC_DAPM_ADC("DMIC1L", NULL, WM8994_POWER_MANAGEMENT_4, 3, 0),
1743SND_SOC_DAPM_ADC("DMIC1R", NULL, WM8994_POWER_MANAGEMENT_4, 2, 0),
1744
1745/* Power is done with the muxes since the ADC power also controls the
1746 * downsampling chain, the chip will automatically manage the analogue
1747 * specific portions.
1748 */
1749SND_SOC_DAPM_ADC("ADCL", NULL, SND_SOC_NOPM, 1, 0),
1750SND_SOC_DAPM_ADC("ADCR", NULL, SND_SOC_NOPM, 0, 0),
1751
Mark Brown9e6e96a2010-01-29 17:47:12 +00001752SND_SOC_DAPM_POST("Debug log", post_ev),
1753};
1754
Mark Brownc4431df2010-11-26 15:21:07 +00001755static const struct snd_soc_dapm_widget wm8994_specific_dapm_widgets[] = {
1756SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8994_aif3adc_mux),
1757};
Mark Brown9e6e96a2010-01-29 17:47:12 +00001758
Mark Brownc4431df2010-11-26 15:21:07 +00001759static const struct snd_soc_dapm_widget wm8958_dapm_widgets[] = {
Mark Brown8c5b8422012-04-17 20:49:05 +01001760SND_SOC_DAPM_SUPPLY("AIF3", WM8994_POWER_MANAGEMENT_6, 5, 1, NULL, 0),
Mark Brownc4431df2010-11-26 15:21:07 +00001761SND_SOC_DAPM_MUX("Mono PCM Out Mux", SND_SOC_NOPM, 0, 0, &mono_pcm_out_mux),
1762SND_SOC_DAPM_MUX("AIF2DACL Mux", SND_SOC_NOPM, 0, 0, &aif2dacl_src_mux),
1763SND_SOC_DAPM_MUX("AIF2DACR Mux", SND_SOC_NOPM, 0, 0, &aif2dacr_src_mux),
1764SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8958_aif3adc_mux),
1765};
1766
1767static const struct snd_soc_dapm_route intercon[] = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00001768 { "CLK_SYS", NULL, "AIF1CLK", check_clk_sys },
1769 { "CLK_SYS", NULL, "AIF2CLK", check_clk_sys },
1770
1771 { "DSP1CLK", NULL, "CLK_SYS" },
1772 { "DSP2CLK", NULL, "CLK_SYS" },
1773 { "DSPINTCLK", NULL, "CLK_SYS" },
1774
1775 { "AIF1ADC1L", NULL, "AIF1CLK" },
1776 { "AIF1ADC1L", NULL, "DSP1CLK" },
1777 { "AIF1ADC1R", NULL, "AIF1CLK" },
1778 { "AIF1ADC1R", NULL, "DSP1CLK" },
1779 { "AIF1ADC1R", NULL, "DSPINTCLK" },
1780
1781 { "AIF1DAC1L", NULL, "AIF1CLK" },
1782 { "AIF1DAC1L", NULL, "DSP1CLK" },
1783 { "AIF1DAC1R", NULL, "AIF1CLK" },
1784 { "AIF1DAC1R", NULL, "DSP1CLK" },
1785 { "AIF1DAC1R", NULL, "DSPINTCLK" },
1786
1787 { "AIF1ADC2L", NULL, "AIF1CLK" },
1788 { "AIF1ADC2L", NULL, "DSP1CLK" },
1789 { "AIF1ADC2R", NULL, "AIF1CLK" },
1790 { "AIF1ADC2R", NULL, "DSP1CLK" },
1791 { "AIF1ADC2R", NULL, "DSPINTCLK" },
1792
1793 { "AIF1DAC2L", NULL, "AIF1CLK" },
1794 { "AIF1DAC2L", NULL, "DSP1CLK" },
1795 { "AIF1DAC2R", NULL, "AIF1CLK" },
1796 { "AIF1DAC2R", NULL, "DSP1CLK" },
1797 { "AIF1DAC2R", NULL, "DSPINTCLK" },
1798
1799 { "AIF2ADCL", NULL, "AIF2CLK" },
1800 { "AIF2ADCL", NULL, "DSP2CLK" },
1801 { "AIF2ADCR", NULL, "AIF2CLK" },
1802 { "AIF2ADCR", NULL, "DSP2CLK" },
1803 { "AIF2ADCR", NULL, "DSPINTCLK" },
1804
1805 { "AIF2DACL", NULL, "AIF2CLK" },
1806 { "AIF2DACL", NULL, "DSP2CLK" },
1807 { "AIF2DACR", NULL, "AIF2CLK" },
1808 { "AIF2DACR", NULL, "DSP2CLK" },
1809 { "AIF2DACR", NULL, "DSPINTCLK" },
1810
1811 { "DMIC1L", NULL, "DMIC1DAT" },
1812 { "DMIC1L", NULL, "CLK_SYS" },
1813 { "DMIC1R", NULL, "DMIC1DAT" },
1814 { "DMIC1R", NULL, "CLK_SYS" },
1815 { "DMIC2L", NULL, "DMIC2DAT" },
1816 { "DMIC2L", NULL, "CLK_SYS" },
1817 { "DMIC2R", NULL, "DMIC2DAT" },
1818 { "DMIC2R", NULL, "CLK_SYS" },
1819
1820 { "ADCL", NULL, "AIF1CLK" },
1821 { "ADCL", NULL, "DSP1CLK" },
1822 { "ADCL", NULL, "DSPINTCLK" },
1823
1824 { "ADCR", NULL, "AIF1CLK" },
1825 { "ADCR", NULL, "DSP1CLK" },
1826 { "ADCR", NULL, "DSPINTCLK" },
1827
1828 { "ADCL Mux", "ADC", "ADCL" },
1829 { "ADCL Mux", "DMIC", "DMIC1L" },
1830 { "ADCR Mux", "ADC", "ADCR" },
1831 { "ADCR Mux", "DMIC", "DMIC1R" },
1832
1833 { "DAC1L", NULL, "AIF1CLK" },
1834 { "DAC1L", NULL, "DSP1CLK" },
1835 { "DAC1L", NULL, "DSPINTCLK" },
1836
1837 { "DAC1R", NULL, "AIF1CLK" },
1838 { "DAC1R", NULL, "DSP1CLK" },
1839 { "DAC1R", NULL, "DSPINTCLK" },
1840
1841 { "DAC2L", NULL, "AIF2CLK" },
1842 { "DAC2L", NULL, "DSP2CLK" },
1843 { "DAC2L", NULL, "DSPINTCLK" },
1844
1845 { "DAC2R", NULL, "AIF2DACR" },
1846 { "DAC2R", NULL, "AIF2CLK" },
1847 { "DAC2R", NULL, "DSP2CLK" },
1848 { "DAC2R", NULL, "DSPINTCLK" },
1849
1850 { "TOCLK", NULL, "CLK_SYS" },
1851
Mark Brown5567d8c2012-02-16 21:43:29 -08001852 { "AIF1DACDAT", NULL, "AIF1 Playback" },
1853 { "AIF2DACDAT", NULL, "AIF2 Playback" },
1854 { "AIF3DACDAT", NULL, "AIF3 Playback" },
1855
1856 { "AIF1 Capture", NULL, "AIF1ADCDAT" },
1857 { "AIF2 Capture", NULL, "AIF2ADCDAT" },
1858 { "AIF3 Capture", NULL, "AIF3ADCDAT" },
1859
Mark Brown9e6e96a2010-01-29 17:47:12 +00001860 /* AIF1 outputs */
1861 { "AIF1ADC1L", NULL, "AIF1ADC1L Mixer" },
1862 { "AIF1ADC1L Mixer", "ADC/DMIC Switch", "ADCL Mux" },
1863 { "AIF1ADC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1864
1865 { "AIF1ADC1R", NULL, "AIF1ADC1R Mixer" },
1866 { "AIF1ADC1R Mixer", "ADC/DMIC Switch", "ADCR Mux" },
1867 { "AIF1ADC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1868
Mark Browna3257ba2010-07-19 14:02:34 +01001869 { "AIF1ADC2L", NULL, "AIF1ADC2L Mixer" },
1870 { "AIF1ADC2L Mixer", "DMIC Switch", "DMIC2L" },
1871 { "AIF1ADC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1872
1873 { "AIF1ADC2R", NULL, "AIF1ADC2R Mixer" },
1874 { "AIF1ADC2R Mixer", "DMIC Switch", "DMIC2R" },
1875 { "AIF1ADC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1876
Mark Brown9e6e96a2010-01-29 17:47:12 +00001877 /* Pin level routing for AIF3 */
1878 { "AIF1DAC1L", NULL, "AIF1DAC Mux" },
1879 { "AIF1DAC1R", NULL, "AIF1DAC Mux" },
1880 { "AIF1DAC2L", NULL, "AIF1DAC Mux" },
1881 { "AIF1DAC2R", NULL, "AIF1DAC Mux" },
1882
Mark Brown9e6e96a2010-01-29 17:47:12 +00001883 { "AIF1DAC Mux", "AIF1DACDAT", "AIF1DACDAT" },
1884 { "AIF1DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1885 { "AIF2DAC Mux", "AIF2DACDAT", "AIF2DACDAT" },
1886 { "AIF2DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1887 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCL" },
1888 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCR" },
1889 { "AIF2ADC Mux", "AIF3DACDAT", "AIF3ADCDAT" },
1890
1891 /* DAC1 inputs */
Mark Brown9e6e96a2010-01-29 17:47:12 +00001892 { "DAC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1893 { "DAC1L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1894 { "DAC1L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1895 { "DAC1L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1896 { "DAC1L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1897
Mark Brown9e6e96a2010-01-29 17:47:12 +00001898 { "DAC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1899 { "DAC1R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1900 { "DAC1R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1901 { "DAC1R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1902 { "DAC1R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1903
1904 /* DAC2/AIF2 outputs */
1905 { "AIF2ADCL", NULL, "AIF2DAC2L Mixer" },
Mark Brown9e6e96a2010-01-29 17:47:12 +00001906 { "AIF2DAC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1907 { "AIF2DAC2L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1908 { "AIF2DAC2L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1909 { "AIF2DAC2L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1910 { "AIF2DAC2L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1911
1912 { "AIF2ADCR", NULL, "AIF2DAC2R Mixer" },
Mark Brown9e6e96a2010-01-29 17:47:12 +00001913 { "AIF2DAC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1914 { "AIF2DAC2R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1915 { "AIF2DAC2R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1916 { "AIF2DAC2R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1917 { "AIF2DAC2R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1918
Mark Brown7f94de42011-02-03 16:27:34 +00001919 { "AIF1ADCDAT", NULL, "AIF1ADC1L" },
1920 { "AIF1ADCDAT", NULL, "AIF1ADC1R" },
1921 { "AIF1ADCDAT", NULL, "AIF1ADC2L" },
1922 { "AIF1ADCDAT", NULL, "AIF1ADC2R" },
1923
Mark Brown9e6e96a2010-01-29 17:47:12 +00001924 { "AIF2ADCDAT", NULL, "AIF2ADC Mux" },
1925
1926 /* AIF3 output */
1927 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1L" },
1928 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1R" },
1929 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2L" },
1930 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2R" },
1931 { "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCL" },
1932 { "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCR" },
1933 { "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACL" },
1934 { "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACR" },
1935
1936 /* Sidetone */
1937 { "Left Sidetone", "ADC/DMIC1", "ADCL Mux" },
1938 { "Left Sidetone", "DMIC2", "DMIC2L" },
1939 { "Right Sidetone", "ADC/DMIC1", "ADCR Mux" },
1940 { "Right Sidetone", "DMIC2", "DMIC2R" },
1941
1942 /* Output stages */
1943 { "Left Output Mixer", "DAC Switch", "DAC1L" },
1944 { "Right Output Mixer", "DAC Switch", "DAC1R" },
1945
1946 { "SPKL", "DAC1 Switch", "DAC1L" },
1947 { "SPKL", "DAC2 Switch", "DAC2L" },
1948
1949 { "SPKR", "DAC1 Switch", "DAC1R" },
1950 { "SPKR", "DAC2 Switch", "DAC2R" },
1951
1952 { "Left Headphone Mux", "DAC", "DAC1L" },
1953 { "Right Headphone Mux", "DAC", "DAC1R" },
1954};
1955
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001956static const struct snd_soc_dapm_route wm8994_lateclk_revd_intercon[] = {
1957 { "DAC1L", NULL, "Late DAC1L Enable PGA" },
1958 { "Late DAC1L Enable PGA", NULL, "DAC1L Mixer" },
1959 { "DAC1R", NULL, "Late DAC1R Enable PGA" },
1960 { "Late DAC1R Enable PGA", NULL, "DAC1R Mixer" },
1961 { "DAC2L", NULL, "Late DAC2L Enable PGA" },
1962 { "Late DAC2L Enable PGA", NULL, "AIF2DAC2L Mixer" },
1963 { "DAC2R", NULL, "Late DAC2R Enable PGA" },
1964 { "Late DAC2R Enable PGA", NULL, "AIF2DAC2R Mixer" }
1965};
1966
1967static const struct snd_soc_dapm_route wm8994_lateclk_intercon[] = {
1968 { "DAC1L", NULL, "DAC1L Mixer" },
1969 { "DAC1R", NULL, "DAC1R Mixer" },
1970 { "DAC2L", NULL, "AIF2DAC2L Mixer" },
1971 { "DAC2R", NULL, "AIF2DAC2R Mixer" },
1972};
1973
Mark Brown6ed8f142011-02-03 16:27:35 +00001974static const struct snd_soc_dapm_route wm8994_revd_intercon[] = {
1975 { "AIF1DACDAT", NULL, "AIF2DACDAT" },
1976 { "AIF2DACDAT", NULL, "AIF1DACDAT" },
1977 { "AIF1ADCDAT", NULL, "AIF2ADCDAT" },
1978 { "AIF2ADCDAT", NULL, "AIF1ADCDAT" },
Mark Brownb793eb62011-07-14 18:21:37 +09001979 { "MICBIAS1", NULL, "CLK_SYS" },
1980 { "MICBIAS1", NULL, "MICBIAS Supply" },
1981 { "MICBIAS2", NULL, "CLK_SYS" },
1982 { "MICBIAS2", NULL, "MICBIAS Supply" },
Mark Brown6ed8f142011-02-03 16:27:35 +00001983};
1984
Mark Brownc4431df2010-11-26 15:21:07 +00001985static const struct snd_soc_dapm_route wm8994_intercon[] = {
1986 { "AIF2DACL", NULL, "AIF2DAC Mux" },
1987 { "AIF2DACR", NULL, "AIF2DAC Mux" },
Mark Brown4e04ada2011-07-15 15:12:31 +09001988 { "MICBIAS1", NULL, "VMID" },
1989 { "MICBIAS2", NULL, "VMID" },
Mark Brownc4431df2010-11-26 15:21:07 +00001990};
1991
1992static const struct snd_soc_dapm_route wm8958_intercon[] = {
1993 { "AIF2DACL", NULL, "AIF2DACL Mux" },
1994 { "AIF2DACR", NULL, "AIF2DACR Mux" },
1995
1996 { "AIF2DACL Mux", "AIF2", "AIF2DAC Mux" },
1997 { "AIF2DACL Mux", "AIF3", "AIF3DACDAT" },
1998 { "AIF2DACR Mux", "AIF2", "AIF2DAC Mux" },
1999 { "AIF2DACR Mux", "AIF3", "AIF3DACDAT" },
2000
Mark Brown8c5b8422012-04-17 20:49:05 +01002001 { "AIF3DACDAT", NULL, "AIF3" },
2002 { "AIF3ADCDAT", NULL, "AIF3" },
2003
Mark Brownc4431df2010-11-26 15:21:07 +00002004 { "Mono PCM Out Mux", "AIF2ADCL", "AIF2ADCL" },
2005 { "Mono PCM Out Mux", "AIF2ADCR", "AIF2ADCR" },
2006
2007 { "AIF3ADC Mux", "Mono PCM", "Mono PCM Out Mux" },
2008};
2009
Mark Brown9e6e96a2010-01-29 17:47:12 +00002010/* The size in bits of the FLL divide multiplied by 10
2011 * to allow rounding later */
2012#define FIXED_FLL_SIZE ((1 << 16) * 10)
2013
2014struct fll_div {
2015 u16 outdiv;
2016 u16 n;
2017 u16 k;
2018 u16 clk_ref_div;
2019 u16 fll_fratio;
2020};
2021
2022static int wm8994_get_fll_config(struct fll_div *fll,
2023 int freq_in, int freq_out)
2024{
2025 u64 Kpart;
2026 unsigned int K, Ndiv, Nmod;
2027
2028 pr_debug("FLL input=%dHz, output=%dHz\n", freq_in, freq_out);
2029
2030 /* Scale the input frequency down to <= 13.5MHz */
2031 fll->clk_ref_div = 0;
2032 while (freq_in > 13500000) {
2033 fll->clk_ref_div++;
2034 freq_in /= 2;
2035
2036 if (fll->clk_ref_div > 3)
2037 return -EINVAL;
2038 }
2039 pr_debug("CLK_REF_DIV=%d, Fref=%dHz\n", fll->clk_ref_div, freq_in);
2040
2041 /* Scale the output to give 90MHz<=Fvco<=100MHz */
2042 fll->outdiv = 3;
2043 while (freq_out * (fll->outdiv + 1) < 90000000) {
2044 fll->outdiv++;
2045 if (fll->outdiv > 63)
2046 return -EINVAL;
2047 }
2048 freq_out *= fll->outdiv + 1;
2049 pr_debug("OUTDIV=%d, Fvco=%dHz\n", fll->outdiv, freq_out);
2050
2051 if (freq_in > 1000000) {
2052 fll->fll_fratio = 0;
Mark Brown7d48a6a2010-04-20 13:36:11 +09002053 } else if (freq_in > 256000) {
2054 fll->fll_fratio = 1;
2055 freq_in *= 2;
2056 } else if (freq_in > 128000) {
2057 fll->fll_fratio = 2;
2058 freq_in *= 4;
2059 } else if (freq_in > 64000) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002060 fll->fll_fratio = 3;
2061 freq_in *= 8;
Mark Brown7d48a6a2010-04-20 13:36:11 +09002062 } else {
2063 fll->fll_fratio = 4;
2064 freq_in *= 16;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002065 }
2066 pr_debug("FLL_FRATIO=%d, Fref=%dHz\n", fll->fll_fratio, freq_in);
2067
2068 /* Now, calculate N.K */
2069 Ndiv = freq_out / freq_in;
2070
2071 fll->n = Ndiv;
2072 Nmod = freq_out % freq_in;
2073 pr_debug("Nmod=%d\n", Nmod);
2074
2075 /* Calculate fractional part - scale up so we can round. */
2076 Kpart = FIXED_FLL_SIZE * (long long)Nmod;
2077
2078 do_div(Kpart, freq_in);
2079
2080 K = Kpart & 0xFFFFFFFF;
2081
2082 if ((K % 10) >= 5)
2083 K += 5;
2084
2085 /* Move down to proper range now rounding is done */
2086 fll->k = K / 10;
2087
2088 pr_debug("N=%x K=%x\n", fll->n, fll->k);
2089
2090 return 0;
2091}
2092
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002093static int _wm8994_set_fll(struct snd_soc_codec *codec, int id, int src,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002094 unsigned int freq_in, unsigned int freq_out)
2095{
Mark Brownb2c812e2010-04-14 15:35:19 +09002096 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01002097 struct wm8994 *control = wm8994->wm8994;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002098 int reg_offset, ret;
2099 struct fll_div fll;
Mark Browne413ba82012-03-29 14:49:27 +01002100 u16 reg, clk1, aif_reg, aif_src;
Mark Brownc7ebf932011-07-12 19:47:59 +09002101 unsigned long timeout;
Mark Brown4b7ed832011-08-10 17:47:33 +09002102 bool was_enabled;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002103
Mark Brown9e6e96a2010-01-29 17:47:12 +00002104 switch (id) {
2105 case WM8994_FLL1:
2106 reg_offset = 0;
2107 id = 0;
Mark Browne413ba82012-03-29 14:49:27 +01002108 aif_src = 0x10;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002109 break;
2110 case WM8994_FLL2:
2111 reg_offset = 0x20;
2112 id = 1;
Mark Browne413ba82012-03-29 14:49:27 +01002113 aif_src = 0x18;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002114 break;
2115 default:
2116 return -EINVAL;
2117 }
2118
Mark Brown4b7ed832011-08-10 17:47:33 +09002119 reg = snd_soc_read(codec, WM8994_FLL1_CONTROL_1 + reg_offset);
2120 was_enabled = reg & WM8994_FLL1_ENA;
2121
Mark Brown136ff2a2010-04-20 12:56:18 +09002122 switch (src) {
Mark Brown7add84a2010-04-22 02:29:01 +09002123 case 0:
2124 /* Allow no source specification when stopping */
2125 if (freq_out)
2126 return -EINVAL;
Mark Brown4514e892010-12-03 16:02:10 +00002127 src = wm8994->fll[id].src;
Mark Brown7add84a2010-04-22 02:29:01 +09002128 break;
Mark Brown136ff2a2010-04-20 12:56:18 +09002129 case WM8994_FLL_SRC_MCLK1:
2130 case WM8994_FLL_SRC_MCLK2:
2131 case WM8994_FLL_SRC_LRCLK:
2132 case WM8994_FLL_SRC_BCLK:
2133 break;
Mark Brownfbfe6982012-07-23 20:14:43 +01002134 case WM8994_FLL_SRC_INTERNAL:
2135 freq_in = 12000000;
2136 freq_out = 12000000;
2137 break;
Mark Brown136ff2a2010-04-20 12:56:18 +09002138 default:
2139 return -EINVAL;
2140 }
2141
Mark Brown9e6e96a2010-01-29 17:47:12 +00002142 /* Are we changing anything? */
2143 if (wm8994->fll[id].src == src &&
2144 wm8994->fll[id].in == freq_in && wm8994->fll[id].out == freq_out)
2145 return 0;
2146
2147 /* If we're stopping the FLL redo the old config - no
2148 * registers will actually be written but we avoid GCC flow
2149 * analysis bugs spewing warnings.
2150 */
2151 if (freq_out)
2152 ret = wm8994_get_fll_config(&fll, freq_in, freq_out);
2153 else
2154 ret = wm8994_get_fll_config(&fll, wm8994->fll[id].in,
2155 wm8994->fll[id].out);
2156 if (ret < 0)
2157 return ret;
2158
Mark Browne413ba82012-03-29 14:49:27 +01002159 /* Make sure that we're not providing SYSCLK right now */
2160 clk1 = snd_soc_read(codec, WM8994_CLOCKING_1);
2161 if (clk1 & WM8994_SYSCLK_SRC)
2162 aif_reg = WM8994_AIF2_CLOCKING_1;
2163 else
2164 aif_reg = WM8994_AIF1_CLOCKING_1;
2165 reg = snd_soc_read(codec, aif_reg);
2166
2167 if ((reg & WM8994_AIF1CLK_ENA) &&
2168 (reg & WM8994_AIF1CLK_SRC_MASK) == aif_src) {
2169 dev_err(codec->dev, "FLL%d is currently providing SYSCLK\n",
2170 id + 1);
2171 return -EBUSY;
2172 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002173
2174 /* We always need to disable the FLL while reconfiguring */
2175 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
2176 WM8994_FLL1_ENA, 0);
2177
Mark Brown20dc24a2012-04-05 12:55:20 +01002178 if (wm8994->fll_byp && src == WM8994_FLL_SRC_BCLK &&
Kyung-Kwee Ryue05854d2012-04-24 18:01:48 +01002179 freq_in == freq_out && freq_out) {
Mark Brown20dc24a2012-04-05 12:55:20 +01002180 dev_dbg(codec->dev, "Bypassing FLL%d\n", id + 1);
2181 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_5 + reg_offset,
2182 WM8958_FLL1_BYP, WM8958_FLL1_BYP);
2183 goto out;
2184 }
2185
Mark Brown9e6e96a2010-01-29 17:47:12 +00002186 reg = (fll.outdiv << WM8994_FLL1_OUTDIV_SHIFT) |
2187 (fll.fll_fratio << WM8994_FLL1_FRATIO_SHIFT);
2188 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_2 + reg_offset,
2189 WM8994_FLL1_OUTDIV_MASK |
2190 WM8994_FLL1_FRATIO_MASK, reg);
2191
Mark Brownb16db742012-03-03 15:33:23 +00002192 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_3 + reg_offset,
2193 WM8994_FLL1_K_MASK, fll.k);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002194
2195 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_4 + reg_offset,
2196 WM8994_FLL1_N_MASK,
Mark Brown7435d4e2012-07-26 14:49:11 +01002197 fll.n << WM8994_FLL1_N_SHIFT);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002198
2199 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_5 + reg_offset,
Mark Brownfbfe6982012-07-23 20:14:43 +01002200 WM8994_FLL1_FRC_NCO | WM8958_FLL1_BYP |
Mark Brown136ff2a2010-04-20 12:56:18 +09002201 WM8994_FLL1_REFCLK_DIV_MASK |
2202 WM8994_FLL1_REFCLK_SRC_MASK,
Mark Brownfbfe6982012-07-23 20:14:43 +01002203 ((src == WM8994_FLL_SRC_INTERNAL)
2204 << WM8994_FLL1_FRC_NCO_SHIFT) |
Mark Brown136ff2a2010-04-20 12:56:18 +09002205 (fll.clk_ref_div << WM8994_FLL1_REFCLK_DIV_SHIFT) |
2206 (src - 1));
Mark Brown9e6e96a2010-01-29 17:47:12 +00002207
Mark Brownf0f50392011-07-16 03:12:18 +09002208 /* Clear any pending completion from a previous failure */
2209 try_wait_for_completion(&wm8994->fll_locked[id]);
2210
Mark Brown9e6e96a2010-01-29 17:47:12 +00002211 /* Enable (with fractional mode if required) */
2212 if (freq_out) {
Mark Brown4b7ed832011-08-10 17:47:33 +09002213 /* Enable VMID if we need it */
2214 if (!was_enabled) {
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002215 active_reference(codec);
2216
Mark Brown4b7ed832011-08-10 17:47:33 +09002217 switch (control->type) {
2218 case WM8994:
2219 vmid_reference(codec);
2220 break;
2221 case WM8958:
2222 if (wm8994->revision < 1)
2223 vmid_reference(codec);
2224 break;
2225 default:
2226 break;
2227 }
2228 }
2229
Mark Brownfbfe6982012-07-23 20:14:43 +01002230 reg = WM8994_FLL1_ENA;
2231
Mark Brown9e6e96a2010-01-29 17:47:12 +00002232 if (fll.k)
Mark Brownfbfe6982012-07-23 20:14:43 +01002233 reg |= WM8994_FLL1_FRAC;
2234 if (src == WM8994_FLL_SRC_INTERNAL)
2235 reg |= WM8994_FLL1_OSC_ENA;
2236
Mark Brown9e6e96a2010-01-29 17:47:12 +00002237 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
Mark Brownfbfe6982012-07-23 20:14:43 +01002238 WM8994_FLL1_ENA | WM8994_FLL1_OSC_ENA |
2239 WM8994_FLL1_FRAC, reg);
Mark Brown8e9ddf82011-07-01 17:24:46 -07002240
Mark Brownc7ebf932011-07-12 19:47:59 +09002241 if (wm8994->fll_locked_irq) {
2242 timeout = wait_for_completion_timeout(&wm8994->fll_locked[id],
2243 msecs_to_jiffies(10));
2244 if (timeout == 0)
2245 dev_warn(codec->dev,
2246 "Timed out waiting for FLL lock\n");
2247 } else {
2248 msleep(5);
2249 }
Mark Brown4b7ed832011-08-10 17:47:33 +09002250 } else {
2251 if (was_enabled) {
2252 switch (control->type) {
2253 case WM8994:
2254 vmid_dereference(codec);
2255 break;
2256 case WM8958:
2257 if (wm8994->revision < 1)
2258 vmid_dereference(codec);
2259 break;
2260 default:
2261 break;
2262 }
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002263
2264 active_dereference(codec);
Mark Brown4b7ed832011-08-10 17:47:33 +09002265 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002266 }
2267
Mark Brown20dc24a2012-04-05 12:55:20 +01002268out:
Mark Brown9e6e96a2010-01-29 17:47:12 +00002269 wm8994->fll[id].in = freq_in;
2270 wm8994->fll[id].out = freq_out;
Mark Brown136ff2a2010-04-20 12:56:18 +09002271 wm8994->fll[id].src = src;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002272
Mark Brown9e6e96a2010-01-29 17:47:12 +00002273 configure_clock(codec);
2274
2275 return 0;
2276}
2277
Mark Brownc7ebf932011-07-12 19:47:59 +09002278static irqreturn_t wm8994_fll_locked_irq(int irq, void *data)
2279{
2280 struct completion *completion = data;
2281
2282 complete(completion);
2283
2284 return IRQ_HANDLED;
2285}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002286
Mark Brown66b47fd2010-07-08 11:25:43 +09002287static int opclk_divs[] = { 10, 20, 30, 40, 55, 60, 80, 120, 160 };
2288
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002289static int wm8994_set_fll(struct snd_soc_dai *dai, int id, int src,
2290 unsigned int freq_in, unsigned int freq_out)
2291{
2292 return _wm8994_set_fll(dai->codec, id, src, freq_in, freq_out);
2293}
2294
Mark Brown9e6e96a2010-01-29 17:47:12 +00002295static int wm8994_set_dai_sysclk(struct snd_soc_dai *dai,
2296 int clk_id, unsigned int freq, int dir)
2297{
2298 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09002299 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown66b47fd2010-07-08 11:25:43 +09002300 int i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002301
2302 switch (dai->id) {
2303 case 1:
2304 case 2:
2305 break;
2306
2307 default:
2308 /* AIF3 shares clocking with AIF1/2 */
2309 return -EINVAL;
2310 }
2311
2312 switch (clk_id) {
2313 case WM8994_SYSCLK_MCLK1:
2314 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK1;
2315 wm8994->mclk[0] = freq;
2316 dev_dbg(dai->dev, "AIF%d using MCLK1 at %uHz\n",
2317 dai->id, freq);
2318 break;
2319
2320 case WM8994_SYSCLK_MCLK2:
2321 /* TODO: Set GPIO AF */
2322 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK2;
2323 wm8994->mclk[1] = freq;
2324 dev_dbg(dai->dev, "AIF%d using MCLK2 at %uHz\n",
2325 dai->id, freq);
2326 break;
2327
2328 case WM8994_SYSCLK_FLL1:
2329 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL1;
2330 dev_dbg(dai->dev, "AIF%d using FLL1\n", dai->id);
2331 break;
2332
2333 case WM8994_SYSCLK_FLL2:
2334 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL2;
2335 dev_dbg(dai->dev, "AIF%d using FLL2\n", dai->id);
2336 break;
2337
Mark Brown66b47fd2010-07-08 11:25:43 +09002338 case WM8994_SYSCLK_OPCLK:
2339 /* Special case - a division (times 10) is given and
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02002340 * no effect on main clocking.
Mark Brown66b47fd2010-07-08 11:25:43 +09002341 */
2342 if (freq) {
2343 for (i = 0; i < ARRAY_SIZE(opclk_divs); i++)
2344 if (opclk_divs[i] == freq)
2345 break;
2346 if (i == ARRAY_SIZE(opclk_divs))
2347 return -EINVAL;
2348 snd_soc_update_bits(codec, WM8994_CLOCKING_2,
2349 WM8994_OPCLK_DIV_MASK, i);
2350 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
2351 WM8994_OPCLK_ENA, WM8994_OPCLK_ENA);
2352 } else {
2353 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
2354 WM8994_OPCLK_ENA, 0);
2355 }
2356
Mark Brown9e6e96a2010-01-29 17:47:12 +00002357 default:
2358 return -EINVAL;
2359 }
2360
2361 configure_clock(codec);
2362
2363 return 0;
2364}
2365
2366static int wm8994_set_bias_level(struct snd_soc_codec *codec,
2367 enum snd_soc_bias_level level)
2368{
Mark Brownb6b05692010-08-13 12:58:20 +01002369 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01002370 struct wm8994 *control = wm8994->wm8994;
Mark Brownb6b05692010-08-13 12:58:20 +01002371
Mark Brown5f2f3892012-02-08 18:51:42 +00002372 wm_hubs_set_bias_level(codec, level);
2373
Mark Brown9e6e96a2010-01-29 17:47:12 +00002374 switch (level) {
2375 case SND_SOC_BIAS_ON:
2376 break;
2377
2378 case SND_SOC_BIAS_PREPARE:
Mark Brown500fa302011-11-29 19:58:19 +00002379 /* MICBIAS into regulating mode */
2380 switch (control->type) {
2381 case WM8958:
2382 case WM1811:
2383 snd_soc_update_bits(codec, WM8958_MICBIAS1,
2384 WM8958_MICB1_MODE, 0);
2385 snd_soc_update_bits(codec, WM8958_MICBIAS2,
2386 WM8958_MICB2_MODE, 0);
2387 break;
2388 default:
2389 break;
2390 }
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002391
2392 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
2393 active_reference(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002394 break;
2395
2396 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002397 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
Mark Brown8bc3c2c2010-11-30 14:56:18 +00002398 switch (control->type) {
Mark Brown8bc3c2c2010-11-30 14:56:18 +00002399 case WM8958:
2400 if (wm8994->revision == 0) {
2401 /* Optimise performance for rev A */
Mark Brown8bc3c2c2010-11-30 14:56:18 +00002402 snd_soc_update_bits(codec,
2403 WM8958_CHARGE_PUMP_2,
2404 WM8958_CP_DISCH,
2405 WM8958_CP_DISCH);
2406 }
2407 break;
Mark Brown81204c82011-05-24 17:35:53 +08002408
Mark Brown462835e2012-01-21 12:11:53 +00002409 default:
Mark Brown81204c82011-05-24 17:35:53 +08002410 break;
Mark Brownb6b05692010-08-13 12:58:20 +01002411 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002412
2413 /* Discharge LINEOUT1 & 2 */
2414 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
2415 WM8994_LINEOUT1_DISCH |
2416 WM8994_LINEOUT2_DISCH,
2417 WM8994_LINEOUT1_DISCH |
2418 WM8994_LINEOUT2_DISCH);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002419 }
2420
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002421 if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE)
2422 active_dereference(codec);
2423
Mark Brown500fa302011-11-29 19:58:19 +00002424 /* MICBIAS into bypass mode on newer devices */
2425 switch (control->type) {
2426 case WM8958:
2427 case WM1811:
2428 snd_soc_update_bits(codec, WM8958_MICBIAS1,
2429 WM8958_MICB1_MODE,
2430 WM8958_MICB1_MODE);
2431 snd_soc_update_bits(codec, WM8958_MICBIAS2,
2432 WM8958_MICB2_MODE,
2433 WM8958_MICB2_MODE);
2434 break;
2435 default:
2436 break;
2437 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002438 break;
2439
2440 case SND_SOC_BIAS_OFF:
Mark Brown4105ab82011-12-05 15:17:36 +00002441 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
Mark Brownfbbf5922011-03-11 18:09:04 +00002442 wm8994->cur_fw = NULL;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002443 break;
2444 }
Mark Brown5f2f3892012-02-08 18:51:42 +00002445
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002446 codec->dapm.bias_level = level;
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002447
Mark Brown9e6e96a2010-01-29 17:47:12 +00002448 return 0;
2449}
2450
Mark Brown22f8d052012-03-19 17:32:06 +00002451int wm8994_vmid_mode(struct snd_soc_codec *codec, enum wm8994_vmid_mode mode)
2452{
2453 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2454
2455 switch (mode) {
2456 case WM8994_VMID_NORMAL:
2457 if (wm8994->hubs.lineout1_se) {
2458 snd_soc_dapm_disable_pin(&codec->dapm,
2459 "LINEOUT1N Driver");
2460 snd_soc_dapm_disable_pin(&codec->dapm,
2461 "LINEOUT1P Driver");
2462 }
2463 if (wm8994->hubs.lineout2_se) {
2464 snd_soc_dapm_disable_pin(&codec->dapm,
2465 "LINEOUT2N Driver");
2466 snd_soc_dapm_disable_pin(&codec->dapm,
2467 "LINEOUT2P Driver");
2468 }
2469
2470 /* Do the sync with the old mode to allow it to clean up */
2471 snd_soc_dapm_sync(&codec->dapm);
2472 wm8994->vmid_mode = mode;
2473 break;
2474
2475 case WM8994_VMID_FORCE:
2476 if (wm8994->hubs.lineout1_se) {
2477 snd_soc_dapm_force_enable_pin(&codec->dapm,
2478 "LINEOUT1N Driver");
2479 snd_soc_dapm_force_enable_pin(&codec->dapm,
2480 "LINEOUT1P Driver");
2481 }
2482 if (wm8994->hubs.lineout2_se) {
2483 snd_soc_dapm_force_enable_pin(&codec->dapm,
2484 "LINEOUT2N Driver");
2485 snd_soc_dapm_force_enable_pin(&codec->dapm,
2486 "LINEOUT2P Driver");
2487 }
2488
2489 wm8994->vmid_mode = mode;
2490 snd_soc_dapm_sync(&codec->dapm);
2491 break;
2492
2493 default:
2494 return -EINVAL;
2495 }
2496
2497 return 0;
2498}
2499
Mark Brown9e6e96a2010-01-29 17:47:12 +00002500static int wm8994_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2501{
2502 struct snd_soc_codec *codec = dai->codec;
Mark Brown2a8a8562011-07-24 12:20:41 +01002503 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2504 struct wm8994 *control = wm8994->wm8994;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002505 int ms_reg;
2506 int aif1_reg;
2507 int ms = 0;
2508 int aif1 = 0;
2509
2510 switch (dai->id) {
2511 case 1:
2512 ms_reg = WM8994_AIF1_MASTER_SLAVE;
2513 aif1_reg = WM8994_AIF1_CONTROL_1;
2514 break;
2515 case 2:
2516 ms_reg = WM8994_AIF2_MASTER_SLAVE;
2517 aif1_reg = WM8994_AIF2_CONTROL_1;
2518 break;
2519 default:
2520 return -EINVAL;
2521 }
2522
2523 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
2524 case SND_SOC_DAIFMT_CBS_CFS:
2525 break;
2526 case SND_SOC_DAIFMT_CBM_CFM:
2527 ms = WM8994_AIF1_MSTR;
2528 break;
2529 default:
2530 return -EINVAL;
2531 }
2532
2533 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2534 case SND_SOC_DAIFMT_DSP_B:
2535 aif1 |= WM8994_AIF1_LRCLK_INV;
2536 case SND_SOC_DAIFMT_DSP_A:
2537 aif1 |= 0x18;
2538 break;
2539 case SND_SOC_DAIFMT_I2S:
2540 aif1 |= 0x10;
2541 break;
2542 case SND_SOC_DAIFMT_RIGHT_J:
2543 break;
2544 case SND_SOC_DAIFMT_LEFT_J:
2545 aif1 |= 0x8;
2546 break;
2547 default:
2548 return -EINVAL;
2549 }
2550
2551 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2552 case SND_SOC_DAIFMT_DSP_A:
2553 case SND_SOC_DAIFMT_DSP_B:
2554 /* frame inversion not valid for DSP modes */
2555 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2556 case SND_SOC_DAIFMT_NB_NF:
2557 break;
2558 case SND_SOC_DAIFMT_IB_NF:
2559 aif1 |= WM8994_AIF1_BCLK_INV;
2560 break;
2561 default:
2562 return -EINVAL;
2563 }
2564 break;
2565
2566 case SND_SOC_DAIFMT_I2S:
2567 case SND_SOC_DAIFMT_RIGHT_J:
2568 case SND_SOC_DAIFMT_LEFT_J:
2569 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2570 case SND_SOC_DAIFMT_NB_NF:
2571 break;
2572 case SND_SOC_DAIFMT_IB_IF:
2573 aif1 |= WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV;
2574 break;
2575 case SND_SOC_DAIFMT_IB_NF:
2576 aif1 |= WM8994_AIF1_BCLK_INV;
2577 break;
2578 case SND_SOC_DAIFMT_NB_IF:
2579 aif1 |= WM8994_AIF1_LRCLK_INV;
2580 break;
2581 default:
2582 return -EINVAL;
2583 }
2584 break;
2585 default:
2586 return -EINVAL;
2587 }
2588
Mark Brownc4431df2010-11-26 15:21:07 +00002589 /* The AIF2 format configuration needs to be mirrored to AIF3
2590 * on WM8958 if it's in use so just do it all the time. */
Mark Brown81204c82011-05-24 17:35:53 +08002591 switch (control->type) {
2592 case WM1811:
2593 case WM8958:
2594 if (dai->id == 2)
2595 snd_soc_update_bits(codec, WM8958_AIF3_CONTROL_1,
2596 WM8994_AIF1_LRCLK_INV |
2597 WM8958_AIF3_FMT_MASK, aif1);
2598 break;
2599
2600 default:
2601 break;
2602 }
Mark Brownc4431df2010-11-26 15:21:07 +00002603
Mark Brown9e6e96a2010-01-29 17:47:12 +00002604 snd_soc_update_bits(codec, aif1_reg,
2605 WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV |
2606 WM8994_AIF1_FMT_MASK,
2607 aif1);
2608 snd_soc_update_bits(codec, ms_reg, WM8994_AIF1_MSTR,
2609 ms);
2610
2611 return 0;
2612}
2613
2614static struct {
2615 int val, rate;
2616} srs[] = {
2617 { 0, 8000 },
2618 { 1, 11025 },
2619 { 2, 12000 },
2620 { 3, 16000 },
2621 { 4, 22050 },
2622 { 5, 24000 },
2623 { 6, 32000 },
2624 { 7, 44100 },
2625 { 8, 48000 },
2626 { 9, 88200 },
2627 { 10, 96000 },
2628};
2629
2630static int fs_ratios[] = {
2631 64, 128, 192, 256, 348, 512, 768, 1024, 1408, 1536
2632};
2633
2634static int bclk_divs[] = {
2635 10, 15, 20, 30, 40, 50, 60, 80, 110, 120, 160, 220, 240, 320, 440, 480,
2636 640, 880, 960, 1280, 1760, 1920
2637};
2638
2639static int wm8994_hw_params(struct snd_pcm_substream *substream,
2640 struct snd_pcm_hw_params *params,
2641 struct snd_soc_dai *dai)
2642{
2643 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09002644 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002645 int aif1_reg;
Mark Brownb1e43d92010-12-07 17:14:56 +00002646 int aif2_reg;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002647 int bclk_reg;
2648 int lrclk_reg;
2649 int rate_reg;
2650 int aif1 = 0;
Mark Brownb1e43d92010-12-07 17:14:56 +00002651 int aif2 = 0;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002652 int bclk = 0;
2653 int lrclk = 0;
2654 int rate_val = 0;
2655 int id = dai->id - 1;
2656
2657 int i, cur_val, best_val, bclk_rate, best;
2658
2659 switch (dai->id) {
2660 case 1:
2661 aif1_reg = WM8994_AIF1_CONTROL_1;
Mark Brownb1e43d92010-12-07 17:14:56 +00002662 aif2_reg = WM8994_AIF1_CONTROL_2;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002663 bclk_reg = WM8994_AIF1_BCLK;
2664 rate_reg = WM8994_AIF1_RATE;
2665 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
Mark Brown7d83d212010-08-23 10:54:43 +01002666 wm8994->lrclk_shared[0]) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002667 lrclk_reg = WM8994_AIF1DAC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002668 } else {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002669 lrclk_reg = WM8994_AIF1ADC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002670 dev_dbg(codec->dev, "AIF1 using split LRCLK\n");
2671 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002672 break;
2673 case 2:
2674 aif1_reg = WM8994_AIF2_CONTROL_1;
Mark Brownb1e43d92010-12-07 17:14:56 +00002675 aif2_reg = WM8994_AIF2_CONTROL_2;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002676 bclk_reg = WM8994_AIF2_BCLK;
2677 rate_reg = WM8994_AIF2_RATE;
2678 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
Mark Brown7d83d212010-08-23 10:54:43 +01002679 wm8994->lrclk_shared[1]) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002680 lrclk_reg = WM8994_AIF2DAC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002681 } else {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002682 lrclk_reg = WM8994_AIF2ADC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002683 dev_dbg(codec->dev, "AIF2 using split LRCLK\n");
2684 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002685 break;
2686 default:
2687 return -EINVAL;
2688 }
2689
Mark Brownb8edf3e2012-06-22 17:21:17 +01002690 bclk_rate = params_rate(params) * 4;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002691 switch (params_format(params)) {
2692 case SNDRV_PCM_FORMAT_S16_LE:
2693 bclk_rate *= 16;
2694 break;
2695 case SNDRV_PCM_FORMAT_S20_3LE:
2696 bclk_rate *= 20;
2697 aif1 |= 0x20;
2698 break;
2699 case SNDRV_PCM_FORMAT_S24_LE:
2700 bclk_rate *= 24;
2701 aif1 |= 0x40;
2702 break;
2703 case SNDRV_PCM_FORMAT_S32_LE:
2704 bclk_rate *= 32;
2705 aif1 |= 0x60;
2706 break;
2707 default:
2708 return -EINVAL;
2709 }
2710
2711 /* Try to find an appropriate sample rate; look for an exact match. */
2712 for (i = 0; i < ARRAY_SIZE(srs); i++)
2713 if (srs[i].rate == params_rate(params))
2714 break;
2715 if (i == ARRAY_SIZE(srs))
2716 return -EINVAL;
2717 rate_val |= srs[i].val << WM8994_AIF1_SR_SHIFT;
2718
2719 dev_dbg(dai->dev, "Sample rate is %dHz\n", srs[i].rate);
2720 dev_dbg(dai->dev, "AIF%dCLK is %dHz, target BCLK %dHz\n",
2721 dai->id, wm8994->aifclk[id], bclk_rate);
2722
Mark Brownb1e43d92010-12-07 17:14:56 +00002723 if (params_channels(params) == 1 &&
2724 (snd_soc_read(codec, aif1_reg) & 0x18) == 0x18)
2725 aif2 |= WM8994_AIF1_MONO;
2726
Mark Brown9e6e96a2010-01-29 17:47:12 +00002727 if (wm8994->aifclk[id] == 0) {
2728 dev_err(dai->dev, "AIF%dCLK not configured\n", dai->id);
2729 return -EINVAL;
2730 }
2731
2732 /* AIFCLK/fs ratio; look for a close match in either direction */
2733 best = 0;
2734 best_val = abs((fs_ratios[0] * params_rate(params))
2735 - wm8994->aifclk[id]);
2736 for (i = 1; i < ARRAY_SIZE(fs_ratios); i++) {
2737 cur_val = abs((fs_ratios[i] * params_rate(params))
2738 - wm8994->aifclk[id]);
2739 if (cur_val >= best_val)
2740 continue;
2741 best = i;
2742 best_val = cur_val;
2743 }
2744 dev_dbg(dai->dev, "Selected AIF%dCLK/fs = %d\n",
2745 dai->id, fs_ratios[best]);
2746 rate_val |= best;
2747
2748 /* We may not get quite the right frequency if using
2749 * approximate clocks so look for the closest match that is
2750 * higher than the target (we need to ensure that there enough
2751 * BCLKs to clock out the samples).
2752 */
2753 best = 0;
2754 for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
Joonyoung Shim07cd8ad2010-02-02 18:53:19 +09002755 cur_val = (wm8994->aifclk[id] * 10 / bclk_divs[i]) - bclk_rate;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002756 if (cur_val < 0) /* BCLK table is sorted */
2757 break;
2758 best = i;
2759 }
Joonyoung Shim07cd8ad2010-02-02 18:53:19 +09002760 bclk_rate = wm8994->aifclk[id] * 10 / bclk_divs[best];
Mark Brown9e6e96a2010-01-29 17:47:12 +00002761 dev_dbg(dai->dev, "Using BCLK_DIV %d for actual BCLK %dHz\n",
2762 bclk_divs[best], bclk_rate);
2763 bclk |= best << WM8994_AIF1_BCLK_DIV_SHIFT;
2764
2765 lrclk = bclk_rate / params_rate(params);
Mark Brownfc07ecd2011-11-28 21:16:56 +00002766 if (!lrclk) {
2767 dev_err(dai->dev, "Unable to generate LRCLK from %dHz BCLK\n",
2768 bclk_rate);
2769 return -EINVAL;
2770 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002771 dev_dbg(dai->dev, "Using LRCLK rate %d for actual LRCLK %dHz\n",
2772 lrclk, bclk_rate / lrclk);
2773
2774 snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
Mark Brownb1e43d92010-12-07 17:14:56 +00002775 snd_soc_update_bits(codec, aif2_reg, WM8994_AIF1_MONO, aif2);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002776 snd_soc_update_bits(codec, bclk_reg, WM8994_AIF1_BCLK_DIV_MASK, bclk);
2777 snd_soc_update_bits(codec, lrclk_reg, WM8994_AIF1DAC_RATE_MASK,
2778 lrclk);
2779 snd_soc_update_bits(codec, rate_reg, WM8994_AIF1_SR_MASK |
2780 WM8994_AIF1CLK_RATE_MASK, rate_val);
2781
2782 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
2783 switch (dai->id) {
2784 case 1:
2785 wm8994->dac_rates[0] = params_rate(params);
2786 wm8994_set_retune_mobile(codec, 0);
2787 wm8994_set_retune_mobile(codec, 1);
2788 break;
2789 case 2:
2790 wm8994->dac_rates[1] = params_rate(params);
2791 wm8994_set_retune_mobile(codec, 2);
2792 break;
2793 }
2794 }
2795
2796 return 0;
2797}
2798
Mark Brownc4431df2010-11-26 15:21:07 +00002799static int wm8994_aif3_hw_params(struct snd_pcm_substream *substream,
2800 struct snd_pcm_hw_params *params,
2801 struct snd_soc_dai *dai)
2802{
2803 struct snd_soc_codec *codec = dai->codec;
Mark Brown2a8a8562011-07-24 12:20:41 +01002804 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2805 struct wm8994 *control = wm8994->wm8994;
Mark Brownc4431df2010-11-26 15:21:07 +00002806 int aif1_reg;
2807 int aif1 = 0;
2808
2809 switch (dai->id) {
2810 case 3:
2811 switch (control->type) {
Mark Brown81204c82011-05-24 17:35:53 +08002812 case WM1811:
Mark Brownc4431df2010-11-26 15:21:07 +00002813 case WM8958:
2814 aif1_reg = WM8958_AIF3_CONTROL_1;
2815 break;
2816 default:
2817 return 0;
2818 }
2819 default:
2820 return 0;
2821 }
2822
2823 switch (params_format(params)) {
2824 case SNDRV_PCM_FORMAT_S16_LE:
2825 break;
2826 case SNDRV_PCM_FORMAT_S20_3LE:
2827 aif1 |= 0x20;
2828 break;
2829 case SNDRV_PCM_FORMAT_S24_LE:
2830 aif1 |= 0x40;
2831 break;
2832 case SNDRV_PCM_FORMAT_S32_LE:
2833 aif1 |= 0x60;
2834 break;
2835 default:
2836 return -EINVAL;
2837 }
2838
2839 return snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
2840}
2841
Mark Brown9e6e96a2010-01-29 17:47:12 +00002842static int wm8994_aif_mute(struct snd_soc_dai *codec_dai, int mute)
2843{
2844 struct snd_soc_codec *codec = codec_dai->codec;
2845 int mute_reg;
2846 int reg;
2847
2848 switch (codec_dai->id) {
2849 case 1:
2850 mute_reg = WM8994_AIF1_DAC1_FILTERS_1;
2851 break;
2852 case 2:
2853 mute_reg = WM8994_AIF2_DAC_FILTERS_1;
2854 break;
2855 default:
2856 return -EINVAL;
2857 }
2858
2859 if (mute)
2860 reg = WM8994_AIF1DAC1_MUTE;
2861 else
2862 reg = 0;
2863
2864 snd_soc_update_bits(codec, mute_reg, WM8994_AIF1DAC1_MUTE, reg);
2865
2866 return 0;
2867}
2868
Mark Brown778a76e2010-03-22 22:05:10 +00002869static int wm8994_set_tristate(struct snd_soc_dai *codec_dai, int tristate)
2870{
2871 struct snd_soc_codec *codec = codec_dai->codec;
2872 int reg, val, mask;
2873
2874 switch (codec_dai->id) {
2875 case 1:
2876 reg = WM8994_AIF1_MASTER_SLAVE;
2877 mask = WM8994_AIF1_TRI;
2878 break;
2879 case 2:
2880 reg = WM8994_AIF2_MASTER_SLAVE;
2881 mask = WM8994_AIF2_TRI;
2882 break;
Mark Brown778a76e2010-03-22 22:05:10 +00002883 default:
2884 return -EINVAL;
2885 }
2886
2887 if (tristate)
2888 val = mask;
2889 else
2890 val = 0;
2891
Qiao Zhou78b3fb42011-01-19 19:10:47 +08002892 return snd_soc_update_bits(codec, reg, mask, val);
Mark Brown778a76e2010-03-22 22:05:10 +00002893}
2894
Mark Brownd09f3ec2011-08-15 11:01:02 +09002895static int wm8994_aif2_probe(struct snd_soc_dai *dai)
2896{
2897 struct snd_soc_codec *codec = dai->codec;
2898
2899 /* Disable the pulls on the AIF if we're using it to save power. */
2900 snd_soc_update_bits(codec, WM8994_GPIO_3,
2901 WM8994_GPN_PU | WM8994_GPN_PD, 0);
2902 snd_soc_update_bits(codec, WM8994_GPIO_4,
2903 WM8994_GPN_PU | WM8994_GPN_PD, 0);
2904 snd_soc_update_bits(codec, WM8994_GPIO_5,
2905 WM8994_GPN_PU | WM8994_GPN_PD, 0);
2906
2907 return 0;
2908}
2909
Mark Brown9e6e96a2010-01-29 17:47:12 +00002910#define WM8994_RATES SNDRV_PCM_RATE_8000_96000
2911
2912#define WM8994_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
Ian Lartey3079aed2010-08-31 23:56:34 +01002913 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002914
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01002915static const struct snd_soc_dai_ops wm8994_aif1_dai_ops = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002916 .set_sysclk = wm8994_set_dai_sysclk,
2917 .set_fmt = wm8994_set_dai_fmt,
2918 .hw_params = wm8994_hw_params,
2919 .digital_mute = wm8994_aif_mute,
2920 .set_pll = wm8994_set_fll,
Mark Brown778a76e2010-03-22 22:05:10 +00002921 .set_tristate = wm8994_set_tristate,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002922};
2923
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01002924static const struct snd_soc_dai_ops wm8994_aif2_dai_ops = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002925 .set_sysclk = wm8994_set_dai_sysclk,
2926 .set_fmt = wm8994_set_dai_fmt,
2927 .hw_params = wm8994_hw_params,
2928 .digital_mute = wm8994_aif_mute,
2929 .set_pll = wm8994_set_fll,
Mark Brown778a76e2010-03-22 22:05:10 +00002930 .set_tristate = wm8994_set_tristate,
2931};
2932
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01002933static const struct snd_soc_dai_ops wm8994_aif3_dai_ops = {
Mark Brownc4431df2010-11-26 15:21:07 +00002934 .hw_params = wm8994_aif3_hw_params,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002935};
2936
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002937static struct snd_soc_dai_driver wm8994_dai[] = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002938 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002939 .name = "wm8994-aif1",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002940 .id = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002941 .playback = {
2942 .stream_name = "AIF1 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002943 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002944 .channels_max = 2,
2945 .rates = WM8994_RATES,
2946 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002947 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002948 },
2949 .capture = {
2950 .stream_name = "AIF1 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002951 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002952 .channels_max = 2,
2953 .rates = WM8994_RATES,
2954 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002955 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002956 },
2957 .ops = &wm8994_aif1_dai_ops,
2958 },
2959 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002960 .name = "wm8994-aif2",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002961 .id = 2,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002962 .playback = {
2963 .stream_name = "AIF2 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002964 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002965 .channels_max = 2,
2966 .rates = WM8994_RATES,
2967 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002968 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002969 },
2970 .capture = {
2971 .stream_name = "AIF2 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002972 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002973 .channels_max = 2,
2974 .rates = WM8994_RATES,
2975 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002976 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002977 },
Mark Brownd09f3ec2011-08-15 11:01:02 +09002978 .probe = wm8994_aif2_probe,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002979 .ops = &wm8994_aif2_dai_ops,
2980 },
2981 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002982 .name = "wm8994-aif3",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002983 .id = 3,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002984 .playback = {
2985 .stream_name = "AIF3 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002986 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002987 .channels_max = 2,
2988 .rates = WM8994_RATES,
2989 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002990 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002991 },
Dan Carpentera8462bd2010-03-24 14:58:34 +03002992 .capture = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002993 .stream_name = "AIF3 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002994 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002995 .channels_max = 2,
2996 .rates = WM8994_RATES,
2997 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002998 .sig_bits = 24,
2999 },
Mark Brown778a76e2010-03-22 22:05:10 +00003000 .ops = &wm8994_aif3_dai_ops,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003001 }
3002};
Mark Brown9e6e96a2010-01-29 17:47:12 +00003003
3004#ifdef CONFIG_PM
Mark Brown4752a882012-03-04 02:16:01 +00003005static int wm8994_codec_suspend(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003006{
Mark Brownb2c812e2010-04-14 15:35:19 +09003007 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003008 int i, ret;
3009
3010 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
3011 memcpy(&wm8994->fll_suspend[i], &wm8994->fll[i],
Mark Brownf701a2e2011-03-09 19:31:01 +00003012 sizeof(struct wm8994_fll_config));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003013 ret = _wm8994_set_fll(codec, i + 1, 0, 0, 0);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003014 if (ret < 0)
3015 dev_warn(codec->dev, "Failed to stop FLL%d: %d\n",
3016 i + 1, ret);
3017 }
3018
3019 wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);
3020
3021 return 0;
3022}
3023
Mark Brown4752a882012-03-04 02:16:01 +00003024static int wm8994_codec_resume(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003025{
Mark Brownb2c812e2010-04-14 15:35:19 +09003026 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01003027 struct wm8994 *control = wm8994->wm8994;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003028 int i, ret;
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00003029 unsigned int val, mask;
3030
3031 if (wm8994->revision < 4) {
3032 /* force a HW read */
Mark Brownd9a76662011-07-24 12:49:52 +01003033 ret = regmap_read(control->regmap,
3034 WM8994_POWER_MANAGEMENT_5, &val);
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00003035
3036 /* modify the cache only */
3037 codec->cache_only = 1;
3038 mask = WM8994_DAC1R_ENA | WM8994_DAC1L_ENA |
3039 WM8994_DAC2R_ENA | WM8994_DAC2L_ENA;
3040 val &= mask;
3041 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
3042 mask, val);
3043 codec->cache_only = 0;
3044 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00003045
Mark Brown9e6e96a2010-01-29 17:47:12 +00003046 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
Mark Brown6a2f1ee2010-05-10 18:36:37 +01003047 if (!wm8994->fll_suspend[i].out)
3048 continue;
3049
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003050 ret = _wm8994_set_fll(codec, i + 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003051 wm8994->fll_suspend[i].src,
3052 wm8994->fll_suspend[i].in,
3053 wm8994->fll_suspend[i].out);
3054 if (ret < 0)
3055 dev_warn(codec->dev, "Failed to restore FLL%d: %d\n",
3056 i + 1, ret);
3057 }
3058
3059 return 0;
3060}
3061#else
Mark Brown4752a882012-03-04 02:16:01 +00003062#define wm8994_codec_suspend NULL
3063#define wm8994_codec_resume NULL
Mark Brown9e6e96a2010-01-29 17:47:12 +00003064#endif
3065
3066static void wm8994_handle_retune_mobile_pdata(struct wm8994_priv *wm8994)
3067{
Mark Brown8cb8e832012-07-25 18:10:03 +01003068 struct snd_soc_codec *codec = wm8994->hubs.codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003069 struct wm8994_pdata *pdata = wm8994->pdata;
3070 struct snd_kcontrol_new controls[] = {
3071 SOC_ENUM_EXT("AIF1.1 EQ Mode",
3072 wm8994->retune_mobile_enum,
3073 wm8994_get_retune_mobile_enum,
3074 wm8994_put_retune_mobile_enum),
3075 SOC_ENUM_EXT("AIF1.2 EQ Mode",
3076 wm8994->retune_mobile_enum,
3077 wm8994_get_retune_mobile_enum,
3078 wm8994_put_retune_mobile_enum),
3079 SOC_ENUM_EXT("AIF2 EQ Mode",
3080 wm8994->retune_mobile_enum,
3081 wm8994_get_retune_mobile_enum,
3082 wm8994_put_retune_mobile_enum),
3083 };
3084 int ret, i, j;
3085 const char **t;
3086
3087 /* We need an array of texts for the enum API but the number
3088 * of texts is likely to be less than the number of
3089 * configurations due to the sample rate dependency of the
3090 * configurations. */
3091 wm8994->num_retune_mobile_texts = 0;
3092 wm8994->retune_mobile_texts = NULL;
3093 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
3094 for (j = 0; j < wm8994->num_retune_mobile_texts; j++) {
3095 if (strcmp(pdata->retune_mobile_cfgs[i].name,
3096 wm8994->retune_mobile_texts[j]) == 0)
3097 break;
3098 }
3099
3100 if (j != wm8994->num_retune_mobile_texts)
3101 continue;
3102
3103 /* Expand the array... */
3104 t = krealloc(wm8994->retune_mobile_texts,
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02003105 sizeof(char *) *
Mark Brown9e6e96a2010-01-29 17:47:12 +00003106 (wm8994->num_retune_mobile_texts + 1),
3107 GFP_KERNEL);
3108 if (t == NULL)
3109 continue;
3110
3111 /* ...store the new entry... */
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02003112 t[wm8994->num_retune_mobile_texts] =
Mark Brown9e6e96a2010-01-29 17:47:12 +00003113 pdata->retune_mobile_cfgs[i].name;
3114
3115 /* ...and remember the new version. */
3116 wm8994->num_retune_mobile_texts++;
3117 wm8994->retune_mobile_texts = t;
3118 }
3119
3120 dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
3121 wm8994->num_retune_mobile_texts);
3122
3123 wm8994->retune_mobile_enum.max = wm8994->num_retune_mobile_texts;
3124 wm8994->retune_mobile_enum.texts = wm8994->retune_mobile_texts;
3125
Mark Brown8cb8e832012-07-25 18:10:03 +01003126 ret = snd_soc_add_codec_controls(wm8994->hubs.codec, controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003127 ARRAY_SIZE(controls));
3128 if (ret != 0)
Mark Brown8cb8e832012-07-25 18:10:03 +01003129 dev_err(wm8994->hubs.codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003130 "Failed to add ReTune Mobile controls: %d\n", ret);
3131}
3132
3133static void wm8994_handle_pdata(struct wm8994_priv *wm8994)
3134{
Mark Brown8cb8e832012-07-25 18:10:03 +01003135 struct snd_soc_codec *codec = wm8994->hubs.codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003136 struct wm8994_pdata *pdata = wm8994->pdata;
3137 int ret, i;
3138
3139 if (!pdata)
3140 return;
3141
3142 wm_hubs_handle_analogue_pdata(codec, pdata->lineout1_diff,
3143 pdata->lineout2_diff,
3144 pdata->lineout1fb,
3145 pdata->lineout2fb,
3146 pdata->jd_scthr,
3147 pdata->jd_thr,
3148 pdata->micbias1_lvl,
3149 pdata->micbias2_lvl);
3150
3151 dev_dbg(codec->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
3152
3153 if (pdata->num_drc_cfgs) {
3154 struct snd_kcontrol_new controls[] = {
3155 SOC_ENUM_EXT("AIF1DRC1 Mode", wm8994->drc_enum,
3156 wm8994_get_drc_enum, wm8994_put_drc_enum),
3157 SOC_ENUM_EXT("AIF1DRC2 Mode", wm8994->drc_enum,
3158 wm8994_get_drc_enum, wm8994_put_drc_enum),
3159 SOC_ENUM_EXT("AIF2DRC Mode", wm8994->drc_enum,
3160 wm8994_get_drc_enum, wm8994_put_drc_enum),
3161 };
3162
3163 /* We need an array of texts for the enum API */
Mark Brown8cb8e832012-07-25 18:10:03 +01003164 wm8994->drc_texts = devm_kzalloc(wm8994->hubs.codec->dev,
Mark Brown7270ceb2011-12-01 14:00:19 +00003165 sizeof(char *) * pdata->num_drc_cfgs, GFP_KERNEL);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003166 if (!wm8994->drc_texts) {
Mark Brown8cb8e832012-07-25 18:10:03 +01003167 dev_err(wm8994->hubs.codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003168 "Failed to allocate %d DRC config texts\n",
3169 pdata->num_drc_cfgs);
3170 return;
3171 }
3172
3173 for (i = 0; i < pdata->num_drc_cfgs; i++)
3174 wm8994->drc_texts[i] = pdata->drc_cfgs[i].name;
3175
3176 wm8994->drc_enum.max = pdata->num_drc_cfgs;
3177 wm8994->drc_enum.texts = wm8994->drc_texts;
3178
Mark Brown8cb8e832012-07-25 18:10:03 +01003179 ret = snd_soc_add_codec_controls(wm8994->hubs.codec, controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003180 ARRAY_SIZE(controls));
Mark Brown9e6e96a2010-01-29 17:47:12 +00003181 for (i = 0; i < WM8994_NUM_DRC; i++)
3182 wm8994_set_drc(codec, i);
Mark Brown45a690f2012-08-15 19:20:54 +01003183 } else {
3184 ret = snd_soc_add_codec_controls(wm8994->hubs.codec,
3185 wm8994_drc_controls,
3186 ARRAY_SIZE(wm8994_drc_controls));
Mark Brown9e6e96a2010-01-29 17:47:12 +00003187 }
3188
Mark Brown45a690f2012-08-15 19:20:54 +01003189 if (ret != 0)
3190 dev_err(wm8994->hubs.codec->dev,
3191 "Failed to add DRC mode controls: %d\n", ret);
3192
3193
Mark Brown9e6e96a2010-01-29 17:47:12 +00003194 dev_dbg(codec->dev, "%d ReTune Mobile configurations\n",
3195 pdata->num_retune_mobile_cfgs);
3196
3197 if (pdata->num_retune_mobile_cfgs)
3198 wm8994_handle_retune_mobile_pdata(wm8994);
3199 else
Mark Brown8cb8e832012-07-25 18:10:03 +01003200 snd_soc_add_codec_controls(wm8994->hubs.codec, wm8994_eq_controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003201 ARRAY_SIZE(wm8994_eq_controls));
Mark Brown48e028e2011-02-21 17:11:59 -08003202
3203 for (i = 0; i < ARRAY_SIZE(pdata->micbias); i++) {
3204 if (pdata->micbias[i]) {
3205 snd_soc_write(codec, WM8958_MICBIAS1 + i,
3206 pdata->micbias[i] & 0xffff);
3207 }
3208 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00003209}
3210
Mark Brown88766982010-03-29 20:57:12 +01003211/**
3212 * wm8994_mic_detect - Enable microphone detection via the WM8994 IRQ
3213 *
3214 * @codec: WM8994 codec
3215 * @jack: jack to report detection events on
3216 * @micbias: microphone bias to detect on
Mark Brown88766982010-03-29 20:57:12 +01003217 *
3218 * Enable microphone detection via IRQ on the WM8994. If GPIOs are
3219 * being used to bring out signals to the processor then only platform
Mark Brown5ab230a2010-09-06 14:59:34 +01003220 * data configuration is needed for WM8994 and processor GPIOs should
Mark Brown88766982010-03-29 20:57:12 +01003221 * be configured using snd_soc_jack_add_gpios() instead.
3222 *
3223 * Configuration of detection levels is available via the micbias1_lvl
3224 * and micbias2_lvl platform data members.
3225 */
3226int wm8994_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
Mark Brown87092e32012-02-06 18:50:39 +00003227 int micbias)
Mark Brown88766982010-03-29 20:57:12 +01003228{
Mark Brownb2c812e2010-04-14 15:35:19 +09003229 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown88766982010-03-29 20:57:12 +01003230 struct wm8994_micdet *micdet;
Mark Brown2a8a8562011-07-24 12:20:41 +01003231 struct wm8994 *control = wm8994->wm8994;
Mark Brown87092e32012-02-06 18:50:39 +00003232 int reg, ret;
Mark Brown88766982010-03-29 20:57:12 +01003233
Mark Brown87092e32012-02-06 18:50:39 +00003234 if (control->type != WM8994) {
3235 dev_warn(codec->dev, "Not a WM8994\n");
Mark Brown3a423152010-11-26 15:21:06 +00003236 return -EINVAL;
Mark Brown87092e32012-02-06 18:50:39 +00003237 }
Mark Brown3a423152010-11-26 15:21:06 +00003238
Mark Brown88766982010-03-29 20:57:12 +01003239 switch (micbias) {
3240 case 1:
3241 micdet = &wm8994->micdet[0];
Mark Brown87092e32012-02-06 18:50:39 +00003242 if (jack)
3243 ret = snd_soc_dapm_force_enable_pin(&codec->dapm,
3244 "MICBIAS1");
3245 else
3246 ret = snd_soc_dapm_disable_pin(&codec->dapm,
3247 "MICBIAS1");
Mark Brown88766982010-03-29 20:57:12 +01003248 break;
3249 case 2:
3250 micdet = &wm8994->micdet[1];
Mark Brown87092e32012-02-06 18:50:39 +00003251 if (jack)
3252 ret = snd_soc_dapm_force_enable_pin(&codec->dapm,
3253 "MICBIAS1");
3254 else
3255 ret = snd_soc_dapm_disable_pin(&codec->dapm,
3256 "MICBIAS1");
Mark Brown88766982010-03-29 20:57:12 +01003257 break;
3258 default:
Mark Brown87092e32012-02-06 18:50:39 +00003259 dev_warn(codec->dev, "Invalid MICBIAS %d\n", micbias);
Mark Brown88766982010-03-29 20:57:12 +01003260 return -EINVAL;
Mark Brown87092e32012-02-06 18:50:39 +00003261 }
Mark Brown88766982010-03-29 20:57:12 +01003262
Mark Brown87092e32012-02-06 18:50:39 +00003263 if (ret != 0)
3264 dev_warn(codec->dev, "Failed to configure MICBIAS%d: %d\n",
3265 micbias, ret);
3266
3267 dev_dbg(codec->dev, "Configuring microphone detection on %d %p\n",
3268 micbias, jack);
Mark Brown88766982010-03-29 20:57:12 +01003269
3270 /* Store the configuration */
3271 micdet->jack = jack;
Mark Brown87092e32012-02-06 18:50:39 +00003272 micdet->detecting = true;
Mark Brown88766982010-03-29 20:57:12 +01003273
3274 /* If either of the jacks is set up then enable detection */
3275 if (wm8994->micdet[0].jack || wm8994->micdet[1].jack)
3276 reg = WM8994_MICD_ENA;
Mark Brown87092e32012-02-06 18:50:39 +00003277 else
Mark Brown88766982010-03-29 20:57:12 +01003278 reg = 0;
3279
3280 snd_soc_update_bits(codec, WM8994_MICBIAS, WM8994_MICD_ENA, reg);
3281
Chris Rattrayd9f34df2012-07-31 14:51:34 +01003282 /* enable MICDET and MICSHRT deboune */
3283 snd_soc_update_bits(codec, WM8994_IRQ_DEBOUNCE,
3284 WM8994_MIC1_DET_DB_MASK | WM8994_MIC1_SHRT_DB_MASK |
3285 WM8994_MIC2_DET_DB_MASK | WM8994_MIC2_SHRT_DB_MASK,
3286 WM8994_MIC1_DET_DB | WM8994_MIC1_SHRT_DB);
3287
Mark Brown87092e32012-02-06 18:50:39 +00003288 snd_soc_dapm_sync(&codec->dapm);
3289
Mark Brown88766982010-03-29 20:57:12 +01003290 return 0;
3291}
3292EXPORT_SYMBOL_GPL(wm8994_mic_detect);
3293
Mark Browne9b54de42012-05-09 19:20:59 +01003294static void wm8994_mic_work(struct work_struct *work)
Mark Brown88766982010-03-29 20:57:12 +01003295{
Mark Browne9b54de42012-05-09 19:20:59 +01003296 struct wm8994_priv *priv = container_of(work,
3297 struct wm8994_priv,
3298 mic_work.work);
Mark Brownfdfc4f32012-05-09 19:24:39 +01003299 struct regmap *regmap = priv->wm8994->regmap;
3300 struct device *dev = priv->wm8994->dev;
3301 unsigned int reg;
3302 int ret;
Mark Brown88766982010-03-29 20:57:12 +01003303 int report;
3304
Mark Brownb8176622012-07-24 15:48:57 +01003305 pm_runtime_get_sync(dev);
3306
Mark Brownfdfc4f32012-05-09 19:24:39 +01003307 ret = regmap_read(regmap, WM8994_INTERRUPT_RAW_STATUS_2, &reg);
3308 if (ret < 0) {
3309 dev_err(dev, "Failed to read microphone status: %d\n",
3310 ret);
Mark Brownb8176622012-07-24 15:48:57 +01003311 pm_runtime_put(dev);
Mark Browne9b54de42012-05-09 19:20:59 +01003312 return;
Mark Brown88766982010-03-29 20:57:12 +01003313 }
3314
Mark Brownfdfc4f32012-05-09 19:24:39 +01003315 dev_dbg(dev, "Microphone status: %x\n", reg);
Mark Brown88766982010-03-29 20:57:12 +01003316
3317 report = 0;
Mark Brown87092e32012-02-06 18:50:39 +00003318 if (reg & WM8994_MIC1_DET_STS) {
3319 if (priv->micdet[0].detecting)
3320 report = SND_JACK_HEADSET;
3321 }
3322 if (reg & WM8994_MIC1_SHRT_STS) {
3323 if (priv->micdet[0].detecting)
3324 report = SND_JACK_HEADPHONE;
3325 else
3326 report |= SND_JACK_BTN_0;
3327 }
3328 if (report)
3329 priv->micdet[0].detecting = false;
3330 else
3331 priv->micdet[0].detecting = true;
3332
Mark Brown88766982010-03-29 20:57:12 +01003333 snd_soc_jack_report(priv->micdet[0].jack, report,
Mark Brown87092e32012-02-06 18:50:39 +00003334 SND_JACK_HEADSET | SND_JACK_BTN_0);
Mark Brown88766982010-03-29 20:57:12 +01003335
3336 report = 0;
Mark Brown87092e32012-02-06 18:50:39 +00003337 if (reg & WM8994_MIC2_DET_STS) {
3338 if (priv->micdet[1].detecting)
3339 report = SND_JACK_HEADSET;
3340 }
3341 if (reg & WM8994_MIC2_SHRT_STS) {
3342 if (priv->micdet[1].detecting)
3343 report = SND_JACK_HEADPHONE;
3344 else
3345 report |= SND_JACK_BTN_0;
3346 }
3347 if (report)
3348 priv->micdet[1].detecting = false;
3349 else
3350 priv->micdet[1].detecting = true;
3351
Mark Brown88766982010-03-29 20:57:12 +01003352 snd_soc_jack_report(priv->micdet[1].jack, report,
Mark Brown87092e32012-02-06 18:50:39 +00003353 SND_JACK_HEADSET | SND_JACK_BTN_0);
Mark Brownb8176622012-07-24 15:48:57 +01003354
3355 pm_runtime_put(dev);
Mark Browne9b54de42012-05-09 19:20:59 +01003356}
3357
3358static irqreturn_t wm8994_mic_irq(int irq, void *data)
3359{
3360 struct wm8994_priv *priv = data;
Mark Brown8cb8e832012-07-25 18:10:03 +01003361 struct snd_soc_codec *codec = priv->hubs.codec;
Mark Browne9b54de42012-05-09 19:20:59 +01003362
3363#ifndef CONFIG_SND_SOC_WM8994_MODULE
3364 trace_snd_soc_jack_irq(dev_name(codec->dev));
3365#endif
3366
3367 pm_wakeup_event(codec->dev, 300);
3368
3369 schedule_delayed_work(&priv->mic_work, msecs_to_jiffies(250));
Mark Brown88766982010-03-29 20:57:12 +01003370
3371 return IRQ_HANDLED;
3372}
3373
Mark Brown821edd22010-11-26 15:21:09 +00003374/* Default microphone detection handler for WM8958 - the user can
3375 * override this if they wish.
3376 */
3377static void wm8958_default_micdet(u16 status, void *data)
3378{
3379 struct snd_soc_codec *codec = data;
3380 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown4585790d2011-11-30 10:55:14 +00003381 int report;
Mark Brown821edd22010-11-26 15:21:09 +00003382
Mark Browna1691342011-11-30 14:56:40 +00003383 dev_dbg(codec->dev, "MICDET %x\n", status);
3384
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003385 /* Either nothing present or just starting detection */
Mark Brownb00adf72011-08-13 11:57:18 +09003386 if (!(status & WM8958_MICD_STS)) {
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003387 if (!wm8994->jackdet) {
3388 /* If nothing present then clear our statuses */
3389 dev_dbg(codec->dev, "Detected open circuit\n");
3390 wm8994->jack_mic = false;
3391 wm8994->mic_detecting = true;
Mark Brown821edd22010-11-26 15:21:09 +00003392
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003393 wm8958_micd_set_rate(codec);
Mark Brown821edd22010-11-26 15:21:09 +00003394
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003395 snd_soc_jack_report(wm8994->micdet[0].jack, 0,
3396 wm8994->btn_mask |
Mark Brown7435d4e2012-07-26 14:49:11 +01003397 SND_JACK_HEADSET);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003398 }
Mark Brownb00adf72011-08-13 11:57:18 +09003399 return;
3400 }
3401
3402 /* If the measurement is showing a high impedence we've got a
3403 * microphone.
3404 */
Mark Brown157a75e2011-11-30 13:43:51 +00003405 if (wm8994->mic_detecting && (status & 0x600)) {
Mark Brownb00adf72011-08-13 11:57:18 +09003406 dev_dbg(codec->dev, "Detected microphone\n");
3407
Mark Brown157a75e2011-11-30 13:43:51 +00003408 wm8994->mic_detecting = false;
Mark Brownb00adf72011-08-13 11:57:18 +09003409 wm8994->jack_mic = true;
3410
3411 wm8958_micd_set_rate(codec);
3412
3413 snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADSET,
3414 SND_JACK_HEADSET);
3415 }
3416
3417
Mark Brown7c08b512012-01-26 18:33:24 +00003418 if (wm8994->mic_detecting && status & 0xfc) {
Mark Brownb00adf72011-08-13 11:57:18 +09003419 dev_dbg(codec->dev, "Detected headphone\n");
Mark Brown157a75e2011-11-30 13:43:51 +00003420 wm8994->mic_detecting = false;
Mark Brownb00adf72011-08-13 11:57:18 +09003421
3422 wm8958_micd_set_rate(codec);
3423
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003424 /* If we have jackdet that will detect removal */
3425 if (wm8994->jackdet) {
Mark Brownc9865642012-03-12 16:31:50 +00003426 mutex_lock(&wm8994->accdet_lock);
3427
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003428 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3429 WM8958_MICD_ENA, 0);
3430
Mark Brownc9865642012-03-12 16:31:50 +00003431 wm1811_jackdet_set_mode(codec,
3432 WM1811_JACKDET_MODE_JACK);
3433
3434 mutex_unlock(&wm8994->accdet_lock);
3435
Mark Brownecd17322012-03-12 16:34:35 +00003436 if (wm8994->pdata->jd_ext_cap)
Mark Brown07fb9d92012-02-21 16:23:35 +00003437 snd_soc_dapm_disable_pin(&codec->dapm,
3438 "MICBIAS2");
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003439 }
Mark Brownecd17322012-03-12 16:34:35 +00003440
3441 snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADPHONE,
3442 SND_JACK_HEADSET);
Mark Brownb00adf72011-08-13 11:57:18 +09003443 }
3444
3445 /* Report short circuit as a button */
3446 if (wm8994->jack_mic) {
Mark Brown4585790d2011-11-30 10:55:14 +00003447 report = 0;
Mark Brownb00adf72011-08-13 11:57:18 +09003448 if (status & 0x4)
Mark Brown4585790d2011-11-30 10:55:14 +00003449 report |= SND_JACK_BTN_0;
3450
3451 if (status & 0x8)
3452 report |= SND_JACK_BTN_1;
3453
3454 if (status & 0x10)
3455 report |= SND_JACK_BTN_2;
3456
3457 if (status & 0x20)
3458 report |= SND_JACK_BTN_3;
3459
3460 if (status & 0x40)
3461 report |= SND_JACK_BTN_4;
3462
3463 if (status & 0x80)
3464 report |= SND_JACK_BTN_5;
3465
3466 snd_soc_jack_report(wm8994->micdet[0].jack, report,
3467 wm8994->btn_mask);
Mark Brownb00adf72011-08-13 11:57:18 +09003468 }
Mark Brown821edd22010-11-26 15:21:09 +00003469}
3470
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003471static irqreturn_t wm1811_jackdet_irq(int irq, void *data)
3472{
3473 struct wm8994_priv *wm8994 = data;
Mark Brown8cb8e832012-07-25 18:10:03 +01003474 struct snd_soc_codec *codec = wm8994->hubs.codec;
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003475 int reg;
Mark Brownc9865642012-03-12 16:31:50 +00003476 bool present;
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003477
Mark Brownb8176622012-07-24 15:48:57 +01003478 pm_runtime_get_sync(codec->dev);
3479
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003480 mutex_lock(&wm8994->accdet_lock);
3481
3482 reg = snd_soc_read(codec, WM1811_JACKDET_CTRL);
3483 if (reg < 0) {
3484 dev_err(codec->dev, "Failed to read jack status: %d\n", reg);
3485 mutex_unlock(&wm8994->accdet_lock);
Mark Brownb8176622012-07-24 15:48:57 +01003486 pm_runtime_put(codec->dev);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003487 return IRQ_NONE;
3488 }
3489
3490 dev_dbg(codec->dev, "JACKDET %x\n", reg);
3491
Mark Brownc9865642012-03-12 16:31:50 +00003492 present = reg & WM1811_JACKDET_LVL;
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003493
Mark Brownc9865642012-03-12 16:31:50 +00003494 if (present) {
3495 dev_dbg(codec->dev, "Jack detected\n");
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003496
Mark Browne9d9a962012-04-26 16:07:32 +01003497 wm8958_micd_set_rate(codec);
3498
Mark Brown55a27782012-02-21 13:45:53 +00003499 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3500 WM8958_MICB2_DISCH, 0);
3501
Mark Brown378ec0c2012-03-01 19:01:43 +00003502 /* Disable debounce while inserted */
3503 snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
3504 WM1811_JACKDET_DB, 0);
3505
Mark Brownb9e67e5e2012-02-28 19:03:37 +00003506 /*
3507 * Start off measument of microphone impedence to find
3508 * out what's actually there.
3509 */
3510 wm8994->mic_detecting = true;
3511 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_MIC);
3512
3513 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3514 WM8958_MICD_ENA, WM8958_MICD_ENA);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003515 } else {
3516 dev_dbg(codec->dev, "Jack not detected\n");
3517
Mark Brown55a27782012-02-21 13:45:53 +00003518 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3519 WM8958_MICB2_DISCH, WM8958_MICB2_DISCH);
3520
Mark Brown378ec0c2012-03-01 19:01:43 +00003521 /* Enable debounce while removed */
3522 snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
3523 WM1811_JACKDET_DB, WM1811_JACKDET_DB);
3524
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003525 wm8994->mic_detecting = false;
3526 wm8994->jack_mic = false;
3527 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3528 WM8958_MICD_ENA, 0);
3529 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_JACK);
3530 }
3531
3532 mutex_unlock(&wm8994->accdet_lock);
3533
Mark Brownc9865642012-03-12 16:31:50 +00003534 /* If required for an external cap force MICBIAS on */
3535 if (wm8994->pdata->jd_ext_cap) {
Mark Brownc9865642012-03-12 16:31:50 +00003536 if (present)
3537 snd_soc_dapm_force_enable_pin(&codec->dapm,
3538 "MICBIAS2");
3539 else
3540 snd_soc_dapm_disable_pin(&codec->dapm, "MICBIAS2");
Mark Brownc9865642012-03-12 16:31:50 +00003541 }
3542
3543 if (present)
3544 snd_soc_jack_report(wm8994->micdet[0].jack,
3545 SND_JACK_MECHANICAL, SND_JACK_MECHANICAL);
3546 else
3547 snd_soc_jack_report(wm8994->micdet[0].jack, 0,
3548 SND_JACK_MECHANICAL | SND_JACK_HEADSET |
3549 wm8994->btn_mask);
3550
Mark Brown99af79d2012-07-25 23:03:36 +01003551 /* Since we only report deltas force an update, ensures we
3552 * avoid bootstrapping issues with the core. */
3553 snd_soc_jack_report(wm8994->micdet[0].jack, 0, 0);
3554
Mark Brownb8176622012-07-24 15:48:57 +01003555 pm_runtime_put(codec->dev);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003556 return IRQ_HANDLED;
3557}
3558
Mark Brown99af79d2012-07-25 23:03:36 +01003559static void wm1811_jackdet_bootstrap(struct work_struct *work)
3560{
3561 struct wm8994_priv *wm8994 = container_of(work,
3562 struct wm8994_priv,
3563 jackdet_bootstrap.work);
3564 wm1811_jackdet_irq(0, wm8994);
3565}
3566
Mark Brown821edd22010-11-26 15:21:09 +00003567/**
3568 * wm8958_mic_detect - Enable microphone detection via the WM8958 IRQ
3569 *
3570 * @codec: WM8958 codec
3571 * @jack: jack to report detection events on
3572 *
3573 * Enable microphone detection functionality for the WM8958. By
3574 * default simple detection which supports the detection of up to 6
3575 * buttons plus video and microphone functionality is supported.
3576 *
3577 * The WM8958 has an advanced jack detection facility which is able to
3578 * support complex accessory detection, especially when used in
3579 * conjunction with external circuitry. In order to provide maximum
3580 * flexiblity a callback is provided which allows a completely custom
3581 * detection algorithm.
3582 */
3583int wm8958_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
3584 wm8958_micdet_cb cb, void *cb_data)
3585{
3586 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01003587 struct wm8994 *control = wm8994->wm8994;
Mark Brown4585790d2011-11-30 10:55:14 +00003588 u16 micd_lvl_sel;
Mark Brown821edd22010-11-26 15:21:09 +00003589
Mark Brown81204c82011-05-24 17:35:53 +08003590 switch (control->type) {
3591 case WM1811:
3592 case WM8958:
3593 break;
3594 default:
Mark Brown821edd22010-11-26 15:21:09 +00003595 return -EINVAL;
Mark Brown81204c82011-05-24 17:35:53 +08003596 }
Mark Brown821edd22010-11-26 15:21:09 +00003597
3598 if (jack) {
3599 if (!cb) {
3600 dev_dbg(codec->dev, "Using default micdet callback\n");
3601 cb = wm8958_default_micdet;
3602 cb_data = codec;
3603 }
3604
Mark Brown4cdf5e42011-11-29 14:36:17 +00003605 snd_soc_dapm_force_enable_pin(&codec->dapm, "CLK_SYS");
Mark Brown7d464b22012-03-03 18:46:06 +00003606 snd_soc_dapm_sync(&codec->dapm);
Mark Brown4cdf5e42011-11-29 14:36:17 +00003607
Mark Brown821edd22010-11-26 15:21:09 +00003608 wm8994->micdet[0].jack = jack;
3609 wm8994->jack_cb = cb;
3610 wm8994->jack_cb_data = cb_data;
3611
Mark Brown157a75e2011-11-30 13:43:51 +00003612 wm8994->mic_detecting = true;
Mark Brownb00adf72011-08-13 11:57:18 +09003613 wm8994->jack_mic = false;
3614
3615 wm8958_micd_set_rate(codec);
3616
Mark Brown4585790d2011-11-30 10:55:14 +00003617 /* Detect microphones and short circuits by default */
3618 if (wm8994->pdata->micd_lvl_sel)
3619 micd_lvl_sel = wm8994->pdata->micd_lvl_sel;
3620 else
3621 micd_lvl_sel = 0x41;
3622
3623 wm8994->btn_mask = SND_JACK_BTN_0 | SND_JACK_BTN_1 |
3624 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
3625 SND_JACK_BTN_4 | SND_JACK_BTN_5;
3626
Mark Brownb00adf72011-08-13 11:57:18 +09003627 snd_soc_update_bits(codec, WM8958_MIC_DETECT_2,
Mark Brown4585790d2011-11-30 10:55:14 +00003628 WM8958_MICD_LVL_SEL_MASK, micd_lvl_sel);
Mark Brownb00adf72011-08-13 11:57:18 +09003629
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003630 WARN_ON(codec->dapm.bias_level > SND_SOC_BIAS_STANDBY);
3631
3632 /*
3633 * If we can use jack detection start off with that,
3634 * otherwise jump straight to microphone detection.
3635 */
3636 if (wm8994->jackdet) {
Mark Brown99af79d2012-07-25 23:03:36 +01003637 /* Disable debounce for the initial detect */
3638 snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
3639 WM1811_JACKDET_DB, 0);
3640
Mark Brown55a27782012-02-21 13:45:53 +00003641 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3642 WM8958_MICB2_DISCH,
3643 WM8958_MICB2_DISCH);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003644 snd_soc_update_bits(codec, WM8994_LDO_1,
3645 WM8994_LDO1_DISCH, 0);
3646 wm1811_jackdet_set_mode(codec,
3647 WM1811_JACKDET_MODE_JACK);
3648 } else {
3649 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3650 WM8958_MICD_ENA, WM8958_MICD_ENA);
3651 }
3652
Mark Brown821edd22010-11-26 15:21:09 +00003653 } else {
3654 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3655 WM8958_MICD_ENA, 0);
Mark Brownafaf1592012-03-03 18:46:36 +00003656 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_NONE);
Mark Brown4cdf5e42011-11-29 14:36:17 +00003657 snd_soc_dapm_disable_pin(&codec->dapm, "CLK_SYS");
Mark Brown7d464b22012-03-03 18:46:06 +00003658 snd_soc_dapm_sync(&codec->dapm);
Mark Brown821edd22010-11-26 15:21:09 +00003659 }
3660
3661 return 0;
3662}
3663EXPORT_SYMBOL_GPL(wm8958_mic_detect);
3664
3665static irqreturn_t wm8958_mic_irq(int irq, void *data)
3666{
3667 struct wm8994_priv *wm8994 = data;
Mark Brown8cb8e832012-07-25 18:10:03 +01003668 struct snd_soc_codec *codec = wm8994->hubs.codec;
Mark Brown19940b32011-08-19 18:05:05 +09003669 int reg, count;
Mark Brown821edd22010-11-26 15:21:09 +00003670
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003671 /*
3672 * Jack detection may have detected a removal simulataneously
3673 * with an update of the MICDET status; if so it will have
3674 * stopped detection and we can ignore this interrupt.
3675 */
Mark Brownc9865642012-03-12 16:31:50 +00003676 if (!(snd_soc_read(codec, WM8958_MIC_DETECT_1) & WM8958_MICD_ENA))
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003677 return IRQ_HANDLED;
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003678
Mark Brownb8176622012-07-24 15:48:57 +01003679 pm_runtime_get_sync(codec->dev);
3680
Mark Brown19940b32011-08-19 18:05:05 +09003681 /* We may occasionally read a detection without an impedence
3682 * range being provided - if that happens loop again.
3683 */
3684 count = 10;
3685 do {
3686 reg = snd_soc_read(codec, WM8958_MIC_DETECT_3);
3687 if (reg < 0) {
3688 dev_err(codec->dev,
3689 "Failed to read mic detect status: %d\n",
3690 reg);
Mark Brownb8176622012-07-24 15:48:57 +01003691 pm_runtime_put(codec->dev);
Mark Brown19940b32011-08-19 18:05:05 +09003692 return IRQ_NONE;
3693 }
Mark Brown821edd22010-11-26 15:21:09 +00003694
Mark Brown19940b32011-08-19 18:05:05 +09003695 if (!(reg & WM8958_MICD_VALID)) {
3696 dev_dbg(codec->dev, "Mic detect data not valid\n");
3697 goto out;
3698 }
3699
3700 if (!(reg & WM8958_MICD_STS) || (reg & WM8958_MICD_LVL_MASK))
3701 break;
3702
3703 msleep(1);
3704 } while (count--);
3705
3706 if (count == 0)
3707 dev_warn(codec->dev, "No impedence range reported for jack\n");
Mark Brown821edd22010-11-26 15:21:09 +00003708
Mark Brown7116f452010-12-29 13:05:21 +00003709#ifndef CONFIG_SND_SOC_WM8994_MODULE
Mark Brown2bbb5d62010-12-05 12:50:12 +00003710 trace_snd_soc_jack_irq(dev_name(codec->dev));
Mark Brown7116f452010-12-29 13:05:21 +00003711#endif
Mark Brown2bbb5d62010-12-05 12:50:12 +00003712
Mark Brown821edd22010-11-26 15:21:09 +00003713 if (wm8994->jack_cb)
3714 wm8994->jack_cb(reg, wm8994->jack_cb_data);
3715 else
3716 dev_warn(codec->dev, "Accessory detection with no callback\n");
3717
3718out:
Mark Brownb8176622012-07-24 15:48:57 +01003719 pm_runtime_put(codec->dev);
Mark Brown821edd22010-11-26 15:21:09 +00003720 return IRQ_HANDLED;
3721}
3722
Mark Brown3b1af3f2011-07-14 12:38:18 +09003723static irqreturn_t wm8994_fifo_error(int irq, void *data)
3724{
3725 struct snd_soc_codec *codec = data;
3726
3727 dev_err(codec->dev, "FIFO error\n");
3728
3729 return IRQ_HANDLED;
3730}
3731
Mark Brownf0b182b2011-08-16 12:01:27 +09003732static irqreturn_t wm8994_temp_warn(int irq, void *data)
3733{
3734 struct snd_soc_codec *codec = data;
3735
3736 dev_err(codec->dev, "Thermal warning\n");
3737
3738 return IRQ_HANDLED;
3739}
3740
3741static irqreturn_t wm8994_temp_shut(int irq, void *data)
3742{
3743 struct snd_soc_codec *codec = data;
3744
3745 dev_crit(codec->dev, "Thermal shutdown\n");
3746
3747 return IRQ_HANDLED;
3748}
3749
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003750static int wm8994_codec_probe(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003751{
Mark Brownd9a76662011-07-24 12:49:52 +01003752 struct wm8994 *control = dev_get_drvdata(codec->dev->parent);
Mark Brown2bc16ed2012-03-03 23:24:39 +00003753 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003754 struct snd_soc_dapm_context *dapm = &codec->dapm;
Mark Brownd9a76662011-07-24 12:49:52 +01003755 unsigned int reg;
Mark Brownec62dbd2010-08-15 14:56:40 +01003756 int ret, i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003757
Mark Brown8cb8e832012-07-25 18:10:03 +01003758 wm8994->hubs.codec = codec;
Mark Brownd9a76662011-07-24 12:49:52 +01003759 codec->control_data = control->regmap;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003760
Mark Brownd9a76662011-07-24 12:49:52 +01003761 snd_soc_codec_set_cache_io(codec, 16, 16, SND_SOC_REGMAP);
Mark Brown2a8a8562011-07-24 12:20:41 +01003762
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003763 mutex_init(&wm8994->accdet_lock);
Mark Browne9b54de42012-05-09 19:20:59 +01003764 INIT_DELAYED_WORK(&wm8994->mic_work, wm8994_mic_work);
Mark Brown99af79d2012-07-25 23:03:36 +01003765 INIT_DELAYED_WORK(&wm8994->jackdet_bootstrap,
3766 wm1811_jackdet_bootstrap);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003767
Mark Brownc7ebf932011-07-12 19:47:59 +09003768 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
3769 init_completion(&wm8994->fll_locked[i]);
3770
Mark Brown9b7c5252011-02-17 20:05:44 -08003771 if (wm8994->pdata && wm8994->pdata->micdet_irq)
3772 wm8994->micdet_irq = wm8994->pdata->micdet_irq;
Mark Brown9b7c5252011-02-17 20:05:44 -08003773
Mark Brown39fb51a2010-11-26 17:23:43 +00003774 pm_runtime_enable(codec->dev);
Mark Brown5fab51742012-02-06 18:37:08 +00003775 pm_runtime_idle(codec->dev);
Mark Brown39fb51a2010-11-26 17:23:43 +00003776
Mark Brownf959dee2012-01-31 16:16:47 +00003777 /* By default use idle_bias_off, will override for WM8994 */
3778 codec->dapm.idle_bias_off = 1;
3779
Mark Brown9e6e96a2010-01-29 17:47:12 +00003780 /* Set revision-specific configuration */
Mark Brownb6b05692010-08-13 12:58:20 +01003781 wm8994->revision = snd_soc_read(codec, WM8994_CHIP_REVISION);
Mark Brown3a423152010-11-26 15:21:06 +00003782 switch (control->type) {
3783 case WM8994:
Mark Brownf959dee2012-01-31 16:16:47 +00003784 /* Single ended line outputs should have VMID on. */
3785 if (!wm8994->pdata->lineout1_diff ||
3786 !wm8994->pdata->lineout2_diff)
3787 codec->dapm.idle_bias_off = 0;
3788
Mark Brown3a423152010-11-26 15:21:06 +00003789 switch (wm8994->revision) {
3790 case 2:
3791 case 3:
Mark Brown4537c4e2011-08-01 13:10:16 +09003792 wm8994->hubs.dcs_codes_l = -5;
3793 wm8994->hubs.dcs_codes_r = -5;
Mark Brown3a423152010-11-26 15:21:06 +00003794 wm8994->hubs.hp_startup_mode = 1;
3795 wm8994->hubs.dcs_readback_mode = 1;
Mark Brownf9acf9f2011-06-07 23:23:52 +01003796 wm8994->hubs.series_startup = 1;
Mark Brown3a423152010-11-26 15:21:06 +00003797 break;
3798 default:
Mark Brown79ef0ab2011-08-01 13:02:17 +09003799 wm8994->hubs.dcs_readback_mode = 2;
Mark Brown3a423152010-11-26 15:21:06 +00003800 break;
3801 }
Mark Brown280ec8b2011-08-10 22:19:19 +09003802 break;
Mark Brown3a423152010-11-26 15:21:06 +00003803
3804 case WM8958:
Mark Brown8437f702010-03-29 17:09:45 +01003805 wm8994->hubs.dcs_readback_mode = 1;
Mark Brown29fdc362012-02-21 10:50:50 +00003806 wm8994->hubs.hp_startup_mode = 1;
Mark Brown20dc24a2012-04-05 12:55:20 +01003807
3808 switch (wm8994->revision) {
3809 case 0:
3810 break;
3811 default:
3812 wm8994->fll_byp = true;
3813 break;
3814 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00003815 break;
Mark Brown3a423152010-11-26 15:21:06 +00003816
Mark Brown81204c82011-05-24 17:35:53 +08003817 case WM1811:
3818 wm8994->hubs.dcs_readback_mode = 2;
3819 wm8994->hubs.no_series_update = 1;
Mark Brown29fdc362012-02-21 10:50:50 +00003820 wm8994->hubs.hp_startup_mode = 1;
Mark Brownaf31a222012-04-26 20:06:56 +01003821 wm8994->hubs.no_cache_dac_hp_direct = true;
Mark Brown20dc24a2012-04-05 12:55:20 +01003822 wm8994->fll_byp = true;
Mark Brown81204c82011-05-24 17:35:53 +08003823
3824 switch (wm8994->revision) {
3825 case 0:
3826 case 1:
Mark Brownfc8e6e82011-11-28 18:48:46 +00003827 case 2:
3828 case 3:
Mark Brown6473a142011-10-17 19:38:52 +01003829 wm8994->hubs.dcs_codes_l = -9;
Mark Browne1660582012-03-21 13:22:40 +00003830 wm8994->hubs.dcs_codes_r = -7;
Mark Brown81204c82011-05-24 17:35:53 +08003831 break;
3832 default:
3833 break;
3834 }
3835
3836 snd_soc_update_bits(codec, WM8994_ANALOGUE_HP_1,
3837 WM1811_HPOUT1_ATTN, WM1811_HPOUT1_ATTN);
3838 break;
3839
Mark Brown9e6e96a2010-01-29 17:47:12 +00003840 default:
3841 break;
3842 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00003843
Mark Brown2a8a8562011-07-24 12:20:41 +01003844 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR,
Mark Brown3b1af3f2011-07-14 12:38:18 +09003845 wm8994_fifo_error, "FIFO error", codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01003846 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN,
Mark Brownf0b182b2011-08-16 12:01:27 +09003847 wm8994_temp_warn, "Thermal warning", codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01003848 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT,
Mark Brownf0b182b2011-08-16 12:01:27 +09003849 wm8994_temp_shut, "Thermal shutdown", codec);
Mark Brown3b1af3f2011-07-14 12:38:18 +09003850
Mark Brown2a8a8562011-07-24 12:20:41 +01003851 ret = wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
Mark Brownb30ead52011-07-12 15:47:17 +09003852 wm_hubs_dcs_done, "DC servo done",
3853 &wm8994->hubs);
3854 if (ret == 0)
3855 wm8994->hubs.dcs_done_irq = true;
3856
Mark Brown3a423152010-11-26 15:21:06 +00003857 switch (control->type) {
3858 case WM8994:
Mark Brown9b7c5252011-02-17 20:05:44 -08003859 if (wm8994->micdet_irq) {
3860 ret = request_threaded_irq(wm8994->micdet_irq, NULL,
3861 wm8994_mic_irq,
3862 IRQF_TRIGGER_RISING,
3863 "Mic1 detect",
3864 wm8994);
3865 if (ret != 0)
3866 dev_warn(codec->dev,
3867 "Failed to request Mic1 detect IRQ: %d\n",
3868 ret);
3869 }
Mark Brown88766982010-03-29 20:57:12 +01003870
Mark Brown2a8a8562011-07-24 12:20:41 +01003871 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brown3a423152010-11-26 15:21:06 +00003872 WM8994_IRQ_MIC1_SHRT,
3873 wm8994_mic_irq, "Mic 1 short",
3874 wm8994);
3875 if (ret != 0)
3876 dev_warn(codec->dev,
3877 "Failed to request Mic1 short IRQ: %d\n",
3878 ret);
Mark Brown88766982010-03-29 20:57:12 +01003879
Mark Brown2a8a8562011-07-24 12:20:41 +01003880 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brown3a423152010-11-26 15:21:06 +00003881 WM8994_IRQ_MIC2_DET,
3882 wm8994_mic_irq, "Mic 2 detect",
3883 wm8994);
3884 if (ret != 0)
3885 dev_warn(codec->dev,
3886 "Failed to request Mic2 detect IRQ: %d\n",
3887 ret);
Mark Brown88766982010-03-29 20:57:12 +01003888
Mark Brown2a8a8562011-07-24 12:20:41 +01003889 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brown3a423152010-11-26 15:21:06 +00003890 WM8994_IRQ_MIC2_SHRT,
3891 wm8994_mic_irq, "Mic 2 short",
3892 wm8994);
3893 if (ret != 0)
3894 dev_warn(codec->dev,
3895 "Failed to request Mic2 short IRQ: %d\n",
3896 ret);
3897 break;
Mark Brown821edd22010-11-26 15:21:09 +00003898
3899 case WM8958:
Mark Brown81204c82011-05-24 17:35:53 +08003900 case WM1811:
Mark Brown9b7c5252011-02-17 20:05:44 -08003901 if (wm8994->micdet_irq) {
3902 ret = request_threaded_irq(wm8994->micdet_irq, NULL,
3903 wm8958_mic_irq,
3904 IRQF_TRIGGER_RISING,
3905 "Mic detect",
3906 wm8994);
3907 if (ret != 0)
3908 dev_warn(codec->dev,
3909 "Failed to request Mic detect IRQ: %d\n",
3910 ret);
Mark Brownb4046d02012-07-18 19:11:30 +01003911 } else {
3912 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_MIC1_DET,
3913 wm8958_mic_irq, "Mic detect",
3914 wm8994);
Mark Brown9b7c5252011-02-17 20:05:44 -08003915 }
Mark Brown3a423152010-11-26 15:21:06 +00003916 }
Mark Brown88766982010-03-29 20:57:12 +01003917
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003918 switch (control->type) {
3919 case WM1811:
3920 if (wm8994->revision > 1) {
3921 ret = wm8994_request_irq(wm8994->wm8994,
3922 WM8994_IRQ_GPIO(6),
3923 wm1811_jackdet_irq, "JACKDET",
3924 wm8994);
3925 if (ret == 0)
3926 wm8994->jackdet = true;
3927 }
3928 break;
3929 default:
3930 break;
3931 }
3932
Mark Brownc7ebf932011-07-12 19:47:59 +09003933 wm8994->fll_locked_irq = true;
3934 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++) {
Mark Brown2a8a8562011-07-24 12:20:41 +01003935 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brownc7ebf932011-07-12 19:47:59 +09003936 WM8994_IRQ_FLL1_LOCK + i,
3937 wm8994_fll_locked_irq, "FLL lock",
3938 &wm8994->fll_locked[i]);
3939 if (ret != 0)
3940 wm8994->fll_locked_irq = false;
3941 }
3942
Mark Brown27060b3c2012-02-06 18:42:14 +00003943 /* Make sure we can read from the GPIOs if they're inputs */
3944 pm_runtime_get_sync(codec->dev);
3945
Mark Brown9e6e96a2010-01-29 17:47:12 +00003946 /* Remember if AIFnLRCLK is configured as a GPIO. This should be
3947 * configured on init - if a system wants to do this dynamically
3948 * at runtime we can deal with that then.
3949 */
Mark Brownd9a76662011-07-24 12:49:52 +01003950 ret = regmap_read(control->regmap, WM8994_GPIO_1, &reg);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003951 if (ret < 0) {
3952 dev_err(codec->dev, "Failed to read GPIO1 state: %d\n", ret);
Mark Brown88766982010-03-29 20:57:12 +01003953 goto err_irq;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003954 }
Mark Brownd9a76662011-07-24 12:49:52 +01003955 if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00003956 wm8994->lrclk_shared[0] = 1;
3957 wm8994_dai[0].symmetric_rates = 1;
3958 } else {
3959 wm8994->lrclk_shared[0] = 0;
3960 }
3961
Mark Brownd9a76662011-07-24 12:49:52 +01003962 ret = regmap_read(control->regmap, WM8994_GPIO_6, &reg);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003963 if (ret < 0) {
3964 dev_err(codec->dev, "Failed to read GPIO6 state: %d\n", ret);
Mark Brown88766982010-03-29 20:57:12 +01003965 goto err_irq;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003966 }
Mark Brownd9a76662011-07-24 12:49:52 +01003967 if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00003968 wm8994->lrclk_shared[1] = 1;
3969 wm8994_dai[1].symmetric_rates = 1;
3970 } else {
3971 wm8994->lrclk_shared[1] = 0;
3972 }
3973
Mark Brown27060b3c2012-02-06 18:42:14 +00003974 pm_runtime_put(codec->dev);
3975
Mark Brownbfd37bb2012-06-05 12:31:32 +01003976 /* Latch volume update bits */
3977 for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
3978 snd_soc_update_bits(codec, wm8994_vu_bits[i].reg,
3979 wm8994_vu_bits[i].mask,
3980 wm8994_vu_bits[i].mask);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003981
3982 /* Set the low bit of the 3D stereo depth so TLV matches */
3983 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_FILTERS_2,
3984 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT,
3985 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT);
3986 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_FILTERS_2,
3987 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT,
3988 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT);
3989 snd_soc_update_bits(codec, WM8994_AIF2_DAC_FILTERS_2,
3990 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT,
3991 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT);
3992
Mark Brown5b739672011-07-06 00:08:43 -07003993 /* Unconditionally enable AIF1 ADC TDM mode on chips which can
3994 * use this; it only affects behaviour on idle TDM clock
3995 * cycles. */
3996 switch (control->type) {
3997 case WM8994:
3998 case WM8958:
3999 snd_soc_update_bits(codec, WM8994_AIF1_CONTROL_1,
4000 WM8994_AIF1ADC_TDM, WM8994_AIF1ADC_TDM);
4001 break;
4002 default:
4003 break;
4004 }
Mark Brownd1ce6b22010-07-20 10:13:14 +01004005
Mark Brown500fa302011-11-29 19:58:19 +00004006 /* Put MICBIAS into bypass mode by default on newer devices */
4007 switch (control->type) {
4008 case WM8958:
4009 case WM1811:
4010 snd_soc_update_bits(codec, WM8958_MICBIAS1,
4011 WM8958_MICB1_MODE, WM8958_MICB1_MODE);
4012 snd_soc_update_bits(codec, WM8958_MICBIAS2,
4013 WM8958_MICB2_MODE, WM8958_MICB2_MODE);
4014 break;
4015 default:
4016 break;
4017 }
4018
Mark Brownc3403042012-04-26 21:29:29 +01004019 wm8994->hubs.check_class_w_digital = wm8994_check_class_w_digital;
4020 wm_hubs_update_class_w(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004021
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004022 wm8994_handle_pdata(wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004023
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004024 wm_hubs_add_analogue_controls(codec);
Liam Girdwood022658b2012-02-03 17:43:09 +00004025 snd_soc_add_codec_controls(codec, wm8994_snd_controls,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004026 ARRAY_SIZE(wm8994_snd_controls));
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004027 snd_soc_dapm_new_controls(dapm, wm8994_dapm_widgets,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004028 ARRAY_SIZE(wm8994_dapm_widgets));
Mark Brownc4431df2010-11-26 15:21:07 +00004029
4030 switch (control->type) {
4031 case WM8994:
4032 snd_soc_dapm_new_controls(dapm, wm8994_specific_dapm_widgets,
4033 ARRAY_SIZE(wm8994_specific_dapm_widgets));
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00004034 if (wm8994->revision < 4) {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00004035 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
4036 ARRAY_SIZE(wm8994_lateclk_revd_widgets));
Dimitris Papastamos04d28682011-03-01 11:47:10 +00004037 snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
4038 ARRAY_SIZE(wm8994_adc_revd_widgets));
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00004039 snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
4040 ARRAY_SIZE(wm8994_dac_revd_widgets));
4041 } else {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00004042 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
4043 ARRAY_SIZE(wm8994_lateclk_widgets));
Dimitris Papastamos04d28682011-03-01 11:47:10 +00004044 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
4045 ARRAY_SIZE(wm8994_adc_widgets));
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00004046 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
4047 ARRAY_SIZE(wm8994_dac_widgets));
4048 }
Mark Brownc4431df2010-11-26 15:21:07 +00004049 break;
4050 case WM8958:
Liam Girdwood022658b2012-02-03 17:43:09 +00004051 snd_soc_add_codec_controls(codec, wm8958_snd_controls,
Mark Brownc4431df2010-11-26 15:21:07 +00004052 ARRAY_SIZE(wm8958_snd_controls));
4053 snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
4054 ARRAY_SIZE(wm8958_dapm_widgets));
Mark Brown780e2802011-03-11 18:00:19 +00004055 if (wm8994->revision < 1) {
4056 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
4057 ARRAY_SIZE(wm8994_lateclk_revd_widgets));
4058 snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
4059 ARRAY_SIZE(wm8994_adc_revd_widgets));
4060 snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
4061 ARRAY_SIZE(wm8994_dac_revd_widgets));
4062 } else {
4063 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
4064 ARRAY_SIZE(wm8994_lateclk_widgets));
4065 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
4066 ARRAY_SIZE(wm8994_adc_widgets));
4067 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
4068 ARRAY_SIZE(wm8994_dac_widgets));
4069 }
Mark Brownc4431df2010-11-26 15:21:07 +00004070 break;
Mark Brown81204c82011-05-24 17:35:53 +08004071
4072 case WM1811:
Liam Girdwood022658b2012-02-03 17:43:09 +00004073 snd_soc_add_codec_controls(codec, wm8958_snd_controls,
Mark Brown81204c82011-05-24 17:35:53 +08004074 ARRAY_SIZE(wm8958_snd_controls));
4075 snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
4076 ARRAY_SIZE(wm8958_dapm_widgets));
4077 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
4078 ARRAY_SIZE(wm8994_lateclk_widgets));
4079 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
4080 ARRAY_SIZE(wm8994_adc_widgets));
4081 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
4082 ARRAY_SIZE(wm8994_dac_widgets));
4083 break;
Mark Brownc4431df2010-11-26 15:21:07 +00004084 }
Mark Brownc4431df2010-11-26 15:21:07 +00004085
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004086 wm_hubs_add_analogue_routes(codec, 0, 0);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004087 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
Mark Brown9e6e96a2010-01-29 17:47:12 +00004088
Mark Brownc4431df2010-11-26 15:21:07 +00004089 switch (control->type) {
4090 case WM8994:
4091 snd_soc_dapm_add_routes(dapm, wm8994_intercon,
4092 ARRAY_SIZE(wm8994_intercon));
Mark Brown6ed8f142011-02-03 16:27:35 +00004093
Dimitris Papastamos173efa02011-02-11 16:32:11 +00004094 if (wm8994->revision < 4) {
Mark Brown6ed8f142011-02-03 16:27:35 +00004095 snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
4096 ARRAY_SIZE(wm8994_revd_intercon));
Dimitris Papastamos173efa02011-02-11 16:32:11 +00004097 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
4098 ARRAY_SIZE(wm8994_lateclk_revd_intercon));
4099 } else {
4100 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
4101 ARRAY_SIZE(wm8994_lateclk_intercon));
4102 }
Mark Brownc4431df2010-11-26 15:21:07 +00004103 break;
4104 case WM8958:
Mark Brown780e2802011-03-11 18:00:19 +00004105 if (wm8994->revision < 1) {
4106 snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
4107 ARRAY_SIZE(wm8994_revd_intercon));
4108 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
4109 ARRAY_SIZE(wm8994_lateclk_revd_intercon));
4110 } else {
4111 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
4112 ARRAY_SIZE(wm8994_lateclk_intercon));
4113 snd_soc_dapm_add_routes(dapm, wm8958_intercon,
4114 ARRAY_SIZE(wm8958_intercon));
4115 }
Mark Brownf701a2e2011-03-09 19:31:01 +00004116
4117 wm8958_dsp2_init(codec);
Mark Brownc4431df2010-11-26 15:21:07 +00004118 break;
Mark Brown81204c82011-05-24 17:35:53 +08004119 case WM1811:
4120 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
4121 ARRAY_SIZE(wm8994_lateclk_intercon));
4122 snd_soc_dapm_add_routes(dapm, wm8958_intercon,
4123 ARRAY_SIZE(wm8958_intercon));
4124 break;
Mark Brownc4431df2010-11-26 15:21:07 +00004125 }
4126
Mark Brown9e6e96a2010-01-29 17:47:12 +00004127 return 0;
4128
Mark Brown88766982010-03-29 20:57:12 +01004129err_irq:
Mark Brownaf6b6fe2011-11-30 20:32:05 +00004130 if (wm8994->jackdet)
4131 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01004132 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_SHRT, wm8994);
4133 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET, wm8994);
4134 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT, wm8994);
Mark Brown9b7c5252011-02-17 20:05:44 -08004135 if (wm8994->micdet_irq)
4136 free_irq(wm8994->micdet_irq, wm8994);
Mark Brownc7ebf932011-07-12 19:47:59 +09004137 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
Mark Brown2a8a8562011-07-24 12:20:41 +01004138 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
Mark Brownc7ebf932011-07-12 19:47:59 +09004139 &wm8994->fll_locked[i]);
Mark Brown2a8a8562011-07-24 12:20:41 +01004140 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
Mark Brownb30ead52011-07-12 15:47:17 +09004141 &wm8994->hubs);
Mark Brown2a8a8562011-07-24 12:20:41 +01004142 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, codec);
4143 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, codec);
4144 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, codec);
Mark Browna421a0e2011-12-29 11:08:34 +00004145
Mark Brown9e6e96a2010-01-29 17:47:12 +00004146 return ret;
4147}
4148
Jesper Juhl34ff0f92012-04-09 22:52:19 +02004149static int wm8994_codec_remove(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00004150{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004151 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01004152 struct wm8994 *control = wm8994->wm8994;
Mark Brownc7ebf932011-07-12 19:47:59 +09004153 int i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00004154
4155 wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004156
Mark Brown39fb51a2010-11-26 17:23:43 +00004157 pm_runtime_disable(codec->dev);
4158
Mark Brownc7ebf932011-07-12 19:47:59 +09004159 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
Mark Brown2a8a8562011-07-24 12:20:41 +01004160 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
Mark Brownc7ebf932011-07-12 19:47:59 +09004161 &wm8994->fll_locked[i]);
4162
Mark Brown2a8a8562011-07-24 12:20:41 +01004163 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
Mark Brownb30ead52011-07-12 15:47:17 +09004164 &wm8994->hubs);
Mark Brown2a8a8562011-07-24 12:20:41 +01004165 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, codec);
4166 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, codec);
4167 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, codec);
Mark Brownb30ead52011-07-12 15:47:17 +09004168
Mark Brownaf6b6fe2011-11-30 20:32:05 +00004169 if (wm8994->jackdet)
4170 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);
4171
Mark Brown3a423152010-11-26 15:21:06 +00004172 switch (control->type) {
4173 case WM8994:
Mark Brown9b7c5252011-02-17 20:05:44 -08004174 if (wm8994->micdet_irq)
4175 free_irq(wm8994->micdet_irq, wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01004176 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET,
Mark Brown3a423152010-11-26 15:21:06 +00004177 wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01004178 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT,
Mark Brown3a423152010-11-26 15:21:06 +00004179 wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01004180 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_DET,
Mark Brown3a423152010-11-26 15:21:06 +00004181 wm8994);
4182 break;
Mark Brown821edd22010-11-26 15:21:09 +00004183
Mark Brown81204c82011-05-24 17:35:53 +08004184 case WM1811:
Mark Brown821edd22010-11-26 15:21:09 +00004185 case WM8958:
Mark Brown9b7c5252011-02-17 20:05:44 -08004186 if (wm8994->micdet_irq)
4187 free_irq(wm8994->micdet_irq, wm8994);
Mark Brown821edd22010-11-26 15:21:09 +00004188 break;
Mark Brown3a423152010-11-26 15:21:06 +00004189 }
Jesper Juhl34ff0f92012-04-09 22:52:19 +02004190 release_firmware(wm8994->mbc);
4191 release_firmware(wm8994->mbc_vss);
4192 release_firmware(wm8994->enh_eq);
Axel Lin24fb2b12010-11-23 15:58:39 +08004193 kfree(wm8994->retune_mobile_texts);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004194 return 0;
4195}
4196
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004197static struct snd_soc_codec_driver soc_codec_dev_wm8994 = {
4198 .probe = wm8994_codec_probe,
4199 .remove = wm8994_codec_remove,
Mark Brown4752a882012-03-04 02:16:01 +00004200 .suspend = wm8994_codec_suspend,
4201 .resume = wm8994_codec_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004202 .set_bias_level = wm8994_set_bias_level,
4203};
4204
4205static int __devinit wm8994_probe(struct platform_device *pdev)
4206{
Mark Brown2bc16ed2012-03-03 23:24:39 +00004207 struct wm8994_priv *wm8994;
4208
4209 wm8994 = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_priv),
4210 GFP_KERNEL);
4211 if (wm8994 == NULL)
4212 return -ENOMEM;
4213 platform_set_drvdata(pdev, wm8994);
4214
4215 wm8994->wm8994 = dev_get_drvdata(pdev->dev.parent);
4216 wm8994->pdata = dev_get_platdata(pdev->dev.parent);
4217
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004218 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wm8994,
4219 wm8994_dai, ARRAY_SIZE(wm8994_dai));
4220}
4221
4222static int __devexit wm8994_remove(struct platform_device *pdev)
4223{
4224 snd_soc_unregister_codec(&pdev->dev);
4225 return 0;
4226}
4227
Mark Brown4752a882012-03-04 02:16:01 +00004228#ifdef CONFIG_PM_SLEEP
4229static int wm8994_suspend(struct device *dev)
4230{
4231 struct wm8994_priv *wm8994 = dev_get_drvdata(dev);
4232
4233 /* Drop down to power saving mode when system is suspended */
4234 if (wm8994->jackdet && !wm8994->active_refcount)
4235 regmap_update_bits(wm8994->wm8994->regmap, WM8994_ANTIPOP_2,
4236 WM1811_JACKDET_MODE_MASK,
4237 wm8994->jackdet_mode);
4238
4239 return 0;
4240}
4241
4242static int wm8994_resume(struct device *dev)
4243{
4244 struct wm8994_priv *wm8994 = dev_get_drvdata(dev);
4245
4246 if (wm8994->jackdet && wm8994->jack_cb)
4247 regmap_update_bits(wm8994->wm8994->regmap, WM8994_ANTIPOP_2,
4248 WM1811_JACKDET_MODE_MASK,
4249 WM1811_JACKDET_MODE_AUDIO);
4250
4251 return 0;
4252}
4253#endif
4254
4255static const struct dev_pm_ops wm8994_pm_ops = {
4256 SET_SYSTEM_SLEEP_PM_OPS(wm8994_suspend, wm8994_resume)
4257};
4258
Mark Brown9e6e96a2010-01-29 17:47:12 +00004259static struct platform_driver wm8994_codec_driver = {
4260 .driver = {
Mark Brown4752a882012-03-04 02:16:01 +00004261 .name = "wm8994-codec",
4262 .owner = THIS_MODULE,
4263 .pm = &wm8994_pm_ops,
4264 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004265 .probe = wm8994_probe,
4266 .remove = __devexit_p(wm8994_remove),
Mark Brown9e6e96a2010-01-29 17:47:12 +00004267};
4268
Mark Brown5bbcc3c2011-11-23 22:52:08 +00004269module_platform_driver(wm8994_codec_driver);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004270
4271MODULE_DESCRIPTION("ASoC WM8994 driver");
4272MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
4273MODULE_LICENSE("GPL");
4274MODULE_ALIAS("platform:wm8994-codec");