blob: 1d1b43fe1722205f6939b38b05fc1f537263d9c0 [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 |
Mark Browna3a1d9d2012-08-22 17:23:56 +0100865 (0x2 << 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 Browna3a1d9d2012-08-22 17:23:56 +0100873 msleep(300);
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 Brownf95be9d2012-08-22 17:25:37 +0100942 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
943 WM8994_VMID_SEL_MASK, 0);
Mark Brown4b7ed832011-08-10 17:47:33 +0900944
Mark Brownf95be9d2012-08-22 17:25:37 +0100945 msleep(400);
Mark Browne85b26c2012-02-11 23:10:30 +0000946
Mark Brown22f8d052012-03-19 17:32:06 +0000947 /* Active discharge */
Mark Brown4b7ed832011-08-10 17:47:33 +0900948 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
949 WM8994_LINEOUT1_DISCH |
950 WM8994_LINEOUT2_DISCH,
951 WM8994_LINEOUT1_DISCH |
952 WM8994_LINEOUT2_DISCH);
953
Mark Brown22f8d052012-03-19 17:32:06 +0000954 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_3,
955 WM8994_LINEOUT1N_ENA |
956 WM8994_LINEOUT1P_ENA |
957 WM8994_LINEOUT2N_ENA |
958 WM8994_LINEOUT2P_ENA, 0);
959
Mark Brown4b7ed832011-08-10 17:47:33 +0900960 /* Switch off startup biases */
961 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
962 WM8994_BIAS_SRC |
963 WM8994_STARTUP_BIAS_ENA |
964 WM8994_VMID_BUF_ENA |
965 WM8994_VMID_RAMP_MASK, 0);
Mark Brown22f8d052012-03-19 17:32:06 +0000966
967 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
Mark Brownf95be9d2012-08-22 17:25:37 +0100968 WM8994_VMID_SEL_MASK, 0);
Mark Brown4b7ed832011-08-10 17:47:33 +0900969 }
Mark Browndb966f82012-02-06 12:07:08 +0000970
971 pm_runtime_put(codec->dev);
Mark Brown4b7ed832011-08-10 17:47:33 +0900972}
973
974static int vmid_event(struct snd_soc_dapm_widget *w,
975 struct snd_kcontrol *kcontrol, int event)
976{
977 struct snd_soc_codec *codec = w->codec;
978
979 switch (event) {
980 case SND_SOC_DAPM_PRE_PMU:
981 vmid_reference(codec);
982 break;
983
984 case SND_SOC_DAPM_POST_PMD:
985 vmid_dereference(codec);
986 break;
987 }
988
989 return 0;
990}
991
Mark Brownc3403042012-04-26 21:29:29 +0100992static bool wm8994_check_class_w_digital(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +0000993{
Mark Brown9e6e96a2010-01-29 17:47:12 +0000994 int source = 0; /* GCC flow analysis can't track enable */
995 int reg, reg_r;
996
Mark Brownc3403042012-04-26 21:29:29 +0100997 /* We also need the same AIF source for L/R and only one path */
Mark Brown9e6e96a2010-01-29 17:47:12 +0000998 reg = snd_soc_read(codec, WM8994_DAC1_LEFT_MIXER_ROUTING);
999 switch (reg) {
1000 case WM8994_AIF2DACL_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +09001001 dev_vdbg(codec->dev, "Class W source AIF2DAC\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +00001002 source = 2 << WM8994_CP_DYN_SRC_SEL_SHIFT;
1003 break;
1004 case WM8994_AIF1DAC2L_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +09001005 dev_vdbg(codec->dev, "Class W source AIF1DAC2\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +00001006 source = 1 << WM8994_CP_DYN_SRC_SEL_SHIFT;
1007 break;
1008 case WM8994_AIF1DAC1L_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +09001009 dev_vdbg(codec->dev, "Class W source AIF1DAC1\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +00001010 source = 0 << WM8994_CP_DYN_SRC_SEL_SHIFT;
1011 break;
1012 default:
Mark Brownee839a22010-04-20 13:57:08 +09001013 dev_vdbg(codec->dev, "DAC mixer setting: %x\n", reg);
Mark Brownc3403042012-04-26 21:29:29 +01001014 return false;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001015 }
1016
1017 reg_r = snd_soc_read(codec, WM8994_DAC1_RIGHT_MIXER_ROUTING);
1018 if (reg_r != reg) {
Mark Brownee839a22010-04-20 13:57:08 +09001019 dev_vdbg(codec->dev, "Left and right DAC mixers different\n");
Mark Brownc3403042012-04-26 21:29:29 +01001020 return false;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001021 }
1022
Mark Brownc3403042012-04-26 21:29:29 +01001023 /* Set the source up */
1024 snd_soc_update_bits(codec, WM8994_CLASS_W_1,
1025 WM8994_CP_DYN_SRC_SEL_MASK, source);
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02001026
Mark Brownc3403042012-04-26 21:29:29 +01001027 return true;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001028}
1029
Mark Brown1a383362012-04-12 19:47:11 +01001030static int aif1clk_ev(struct snd_soc_dapm_widget *w,
1031 struct snd_kcontrol *kcontrol, int event)
1032{
1033 struct snd_soc_codec *codec = w->codec;
1034 struct wm8994 *control = codec->control_data;
1035 int mask = WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC1R_ENA;
Mark Brownbfd37bb2012-06-05 12:31:32 +01001036 int i;
Mark Brown1a383362012-04-12 19:47:11 +01001037 int dac;
1038 int adc;
1039 int val;
1040
1041 switch (control->type) {
1042 case WM8994:
1043 case WM8958:
1044 mask |= WM8994_AIF1DAC2L_ENA | WM8994_AIF1DAC2R_ENA;
1045 break;
1046 default:
1047 break;
1048 }
1049
1050 switch (event) {
1051 case SND_SOC_DAPM_PRE_PMU:
1052 val = snd_soc_read(codec, WM8994_AIF1_CONTROL_1);
1053 if ((val & WM8994_AIF1ADCL_SRC) &&
1054 (val & WM8994_AIF1ADCR_SRC))
1055 adc = WM8994_AIF1ADC1R_ENA | WM8994_AIF1ADC2R_ENA;
1056 else if (!(val & WM8994_AIF1ADCL_SRC) &&
1057 !(val & WM8994_AIF1ADCR_SRC))
1058 adc = WM8994_AIF1ADC1L_ENA | WM8994_AIF1ADC2L_ENA;
1059 else
1060 adc = WM8994_AIF1ADC1R_ENA | WM8994_AIF1ADC2R_ENA |
1061 WM8994_AIF1ADC1L_ENA | WM8994_AIF1ADC2L_ENA;
1062
1063 val = snd_soc_read(codec, WM8994_AIF1_CONTROL_2);
1064 if ((val & WM8994_AIF1DACL_SRC) &&
1065 (val & WM8994_AIF1DACR_SRC))
1066 dac = WM8994_AIF1DAC1R_ENA | WM8994_AIF1DAC2R_ENA;
1067 else if (!(val & WM8994_AIF1DACL_SRC) &&
1068 !(val & WM8994_AIF1DACR_SRC))
1069 dac = WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC2L_ENA;
1070 else
1071 dac = WM8994_AIF1DAC1R_ENA | WM8994_AIF1DAC2R_ENA |
1072 WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC2L_ENA;
1073
1074 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
1075 mask, adc);
1076 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1077 mask, dac);
1078 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
1079 WM8994_AIF1DSPCLK_ENA |
1080 WM8994_SYSDSPCLK_ENA,
1081 WM8994_AIF1DSPCLK_ENA |
1082 WM8994_SYSDSPCLK_ENA);
1083 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4, mask,
1084 WM8994_AIF1ADC1R_ENA |
1085 WM8994_AIF1ADC1L_ENA |
1086 WM8994_AIF1ADC2R_ENA |
1087 WM8994_AIF1ADC2L_ENA);
1088 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5, mask,
1089 WM8994_AIF1DAC1R_ENA |
1090 WM8994_AIF1DAC1L_ENA |
1091 WM8994_AIF1DAC2R_ENA |
1092 WM8994_AIF1DAC2L_ENA);
1093 break;
1094
Mark Brownbfd37bb2012-06-05 12:31:32 +01001095 case SND_SOC_DAPM_POST_PMU:
1096 for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
1097 snd_soc_write(codec, wm8994_vu_bits[i].reg,
1098 snd_soc_read(codec,
1099 wm8994_vu_bits[i].reg));
1100 break;
1101
Mark Brown1a383362012-04-12 19:47:11 +01001102 case SND_SOC_DAPM_PRE_PMD:
1103 case SND_SOC_DAPM_POST_PMD:
1104 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1105 mask, 0);
1106 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
1107 mask, 0);
1108
1109 val = snd_soc_read(codec, WM8994_CLOCKING_1);
1110 if (val & WM8994_AIF2DSPCLK_ENA)
1111 val = WM8994_SYSDSPCLK_ENA;
1112 else
1113 val = 0;
1114 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
1115 WM8994_SYSDSPCLK_ENA |
1116 WM8994_AIF1DSPCLK_ENA, val);
1117 break;
1118 }
1119
1120 return 0;
1121}
1122
1123static int aif2clk_ev(struct snd_soc_dapm_widget *w,
1124 struct snd_kcontrol *kcontrol, int event)
1125{
1126 struct snd_soc_codec *codec = w->codec;
Mark Brownbfd37bb2012-06-05 12:31:32 +01001127 int i;
Mark Brown1a383362012-04-12 19:47:11 +01001128 int dac;
1129 int adc;
1130 int val;
1131
1132 switch (event) {
1133 case SND_SOC_DAPM_PRE_PMU:
1134 val = snd_soc_read(codec, WM8994_AIF2_CONTROL_1);
1135 if ((val & WM8994_AIF2ADCL_SRC) &&
1136 (val & WM8994_AIF2ADCR_SRC))
1137 adc = WM8994_AIF2ADCR_ENA;
1138 else if (!(val & WM8994_AIF2ADCL_SRC) &&
1139 !(val & WM8994_AIF2ADCR_SRC))
1140 adc = WM8994_AIF2ADCL_ENA;
1141 else
1142 adc = WM8994_AIF2ADCL_ENA | WM8994_AIF2ADCR_ENA;
1143
1144
1145 val = snd_soc_read(codec, WM8994_AIF2_CONTROL_2);
1146 if ((val & WM8994_AIF2DACL_SRC) &&
1147 (val & WM8994_AIF2DACR_SRC))
1148 dac = WM8994_AIF2DACR_ENA;
1149 else if (!(val & WM8994_AIF2DACL_SRC) &&
1150 !(val & WM8994_AIF2DACR_SRC))
1151 dac = WM8994_AIF2DACL_ENA;
1152 else
1153 dac = WM8994_AIF2DACL_ENA | WM8994_AIF2DACR_ENA;
1154
1155 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
1156 WM8994_AIF2ADCL_ENA |
1157 WM8994_AIF2ADCR_ENA, adc);
1158 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1159 WM8994_AIF2DACL_ENA |
1160 WM8994_AIF2DACR_ENA, dac);
1161 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
1162 WM8994_AIF2DSPCLK_ENA |
1163 WM8994_SYSDSPCLK_ENA,
1164 WM8994_AIF2DSPCLK_ENA |
1165 WM8994_SYSDSPCLK_ENA);
1166 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
1167 WM8994_AIF2ADCL_ENA |
1168 WM8994_AIF2ADCR_ENA,
1169 WM8994_AIF2ADCL_ENA |
1170 WM8994_AIF2ADCR_ENA);
1171 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1172 WM8994_AIF2DACL_ENA |
1173 WM8994_AIF2DACR_ENA,
1174 WM8994_AIF2DACL_ENA |
1175 WM8994_AIF2DACR_ENA);
1176 break;
1177
Mark Brownbfd37bb2012-06-05 12:31:32 +01001178 case SND_SOC_DAPM_POST_PMU:
1179 for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
1180 snd_soc_write(codec, wm8994_vu_bits[i].reg,
1181 snd_soc_read(codec,
1182 wm8994_vu_bits[i].reg));
1183 break;
1184
Mark Brown1a383362012-04-12 19:47:11 +01001185 case SND_SOC_DAPM_PRE_PMD:
1186 case SND_SOC_DAPM_POST_PMD:
1187 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1188 WM8994_AIF2DACL_ENA |
1189 WM8994_AIF2DACR_ENA, 0);
Mark Brownc7f5f232012-05-15 18:13:00 +01001190 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
Mark Brown1a383362012-04-12 19:47:11 +01001191 WM8994_AIF2ADCL_ENA |
1192 WM8994_AIF2ADCR_ENA, 0);
1193
1194 val = snd_soc_read(codec, WM8994_CLOCKING_1);
1195 if (val & WM8994_AIF1DSPCLK_ENA)
1196 val = WM8994_SYSDSPCLK_ENA;
1197 else
1198 val = 0;
1199 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
1200 WM8994_SYSDSPCLK_ENA |
1201 WM8994_AIF2DSPCLK_ENA, val);
1202 break;
1203 }
1204
1205 return 0;
1206}
1207
1208static int aif1clk_late_ev(struct snd_soc_dapm_widget *w,
1209 struct snd_kcontrol *kcontrol, int event)
1210{
1211 struct snd_soc_codec *codec = w->codec;
1212 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1213
1214 switch (event) {
1215 case SND_SOC_DAPM_PRE_PMU:
1216 wm8994->aif1clk_enable = 1;
1217 break;
1218 case SND_SOC_DAPM_POST_PMD:
1219 wm8994->aif1clk_disable = 1;
1220 break;
1221 }
1222
1223 return 0;
1224}
1225
1226static int aif2clk_late_ev(struct snd_soc_dapm_widget *w,
1227 struct snd_kcontrol *kcontrol, int event)
1228{
1229 struct snd_soc_codec *codec = w->codec;
1230 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1231
1232 switch (event) {
1233 case SND_SOC_DAPM_PRE_PMU:
1234 wm8994->aif2clk_enable = 1;
1235 break;
1236 case SND_SOC_DAPM_POST_PMD:
1237 wm8994->aif2clk_disable = 1;
1238 break;
1239 }
1240
1241 return 0;
1242}
1243
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001244static int late_enable_ev(struct snd_soc_dapm_widget *w,
1245 struct snd_kcontrol *kcontrol, int event)
1246{
1247 struct snd_soc_codec *codec = w->codec;
1248 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1249
1250 switch (event) {
1251 case SND_SOC_DAPM_PRE_PMU:
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001252 if (wm8994->aif1clk_enable) {
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001253 aif1clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMU);
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001254 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1255 WM8994_AIF1CLK_ENA_MASK,
1256 WM8994_AIF1CLK_ENA);
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001257 aif1clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMU);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001258 wm8994->aif1clk_enable = 0;
1259 }
1260 if (wm8994->aif2clk_enable) {
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001261 aif2clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMU);
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001262 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1263 WM8994_AIF2CLK_ENA_MASK,
1264 WM8994_AIF2CLK_ENA);
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001265 aif2clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMU);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001266 wm8994->aif2clk_enable = 0;
1267 }
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001268 break;
1269 }
1270
Mark Brownc6b7b572011-03-11 18:13:12 +00001271 /* We may also have postponed startup of DSP, handle that. */
1272 wm8958_aif_ev(w, kcontrol, event);
1273
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001274 return 0;
1275}
1276
1277static int late_disable_ev(struct snd_soc_dapm_widget *w,
1278 struct snd_kcontrol *kcontrol, int event)
1279{
1280 struct snd_soc_codec *codec = w->codec;
1281 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1282
1283 switch (event) {
1284 case SND_SOC_DAPM_POST_PMD:
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001285 if (wm8994->aif1clk_disable) {
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001286 aif1clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMD);
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001287 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1288 WM8994_AIF1CLK_ENA_MASK, 0);
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001289 aif1clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMD);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001290 wm8994->aif1clk_disable = 0;
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001291 }
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001292 if (wm8994->aif2clk_disable) {
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001293 aif2clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMD);
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001294 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1295 WM8994_AIF2CLK_ENA_MASK, 0);
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001296 aif2clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMD);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001297 wm8994->aif2clk_disable = 0;
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001298 }
1299 break;
1300 }
1301
1302 return 0;
1303}
1304
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001305static int adc_mux_ev(struct snd_soc_dapm_widget *w,
1306 struct snd_kcontrol *kcontrol, int event)
1307{
1308 late_enable_ev(w, kcontrol, event);
1309 return 0;
1310}
1311
Dimitris Papastamosb462c6e2011-03-01 12:54:39 +00001312static int micbias_ev(struct snd_soc_dapm_widget *w,
1313 struct snd_kcontrol *kcontrol, int event)
1314{
1315 late_enable_ev(w, kcontrol, event);
1316 return 0;
1317}
1318
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001319static int dac_ev(struct snd_soc_dapm_widget *w,
1320 struct snd_kcontrol *kcontrol, int event)
1321{
1322 struct snd_soc_codec *codec = w->codec;
1323 unsigned int mask = 1 << w->shift;
1324
1325 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1326 mask, mask);
1327 return 0;
1328}
1329
Mark Brown9e6e96a2010-01-29 17:47:12 +00001330static const char *adc_mux_text[] = {
1331 "ADC",
1332 "DMIC",
1333};
1334
1335static const struct soc_enum adc_enum =
1336 SOC_ENUM_SINGLE(0, 0, 2, adc_mux_text);
1337
1338static const struct snd_kcontrol_new adcl_mux =
1339 SOC_DAPM_ENUM_VIRT("ADCL Mux", adc_enum);
1340
1341static const struct snd_kcontrol_new adcr_mux =
1342 SOC_DAPM_ENUM_VIRT("ADCR Mux", adc_enum);
1343
1344static const struct snd_kcontrol_new left_speaker_mixer[] = {
1345SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 9, 1, 0),
1346SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 7, 1, 0),
1347SOC_DAPM_SINGLE("IN1LP Switch", WM8994_SPEAKER_MIXER, 5, 1, 0),
1348SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 3, 1, 0),
1349SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 1, 1, 0),
1350};
1351
1352static const struct snd_kcontrol_new right_speaker_mixer[] = {
1353SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 8, 1, 0),
1354SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 6, 1, 0),
1355SOC_DAPM_SINGLE("IN1RP Switch", WM8994_SPEAKER_MIXER, 4, 1, 0),
1356SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 2, 1, 0),
1357SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 0, 1, 0),
1358};
1359
1360/* Debugging; dump chip status after DAPM transitions */
1361static int post_ev(struct snd_soc_dapm_widget *w,
1362 struct snd_kcontrol *kcontrol, int event)
1363{
1364 struct snd_soc_codec *codec = w->codec;
1365 dev_dbg(codec->dev, "SRC status: %x\n",
1366 snd_soc_read(codec,
1367 WM8994_RATE_STATUS));
1368 return 0;
1369}
1370
1371static const struct snd_kcontrol_new aif1adc1l_mix[] = {
1372SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1373 1, 1, 0),
1374SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1375 0, 1, 0),
1376};
1377
1378static const struct snd_kcontrol_new aif1adc1r_mix[] = {
1379SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1380 1, 1, 0),
1381SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1382 0, 1, 0),
1383};
1384
Mark Browna3257ba2010-07-19 14:02:34 +01001385static const struct snd_kcontrol_new aif1adc2l_mix[] = {
1386SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1387 1, 1, 0),
1388SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1389 0, 1, 0),
1390};
1391
1392static const struct snd_kcontrol_new aif1adc2r_mix[] = {
1393SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1394 1, 1, 0),
1395SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1396 0, 1, 0),
1397};
1398
Mark Brown9e6e96a2010-01-29 17:47:12 +00001399static const struct snd_kcontrol_new aif2dac2l_mix[] = {
1400SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1401 5, 1, 0),
1402SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1403 4, 1, 0),
1404SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1405 2, 1, 0),
1406SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1407 1, 1, 0),
1408SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1409 0, 1, 0),
1410};
1411
1412static const struct snd_kcontrol_new aif2dac2r_mix[] = {
1413SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1414 5, 1, 0),
1415SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1416 4, 1, 0),
1417SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1418 2, 1, 0),
1419SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1420 1, 1, 0),
1421SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1422 0, 1, 0),
1423};
1424
1425#define WM8994_CLASS_W_SWITCH(xname, reg, shift, max, invert) \
1426{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1427 .info = snd_soc_info_volsw, \
1428 .get = snd_soc_dapm_get_volsw, .put = wm8994_put_class_w, \
1429 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
1430
1431static int wm8994_put_class_w(struct snd_kcontrol *kcontrol,
1432 struct snd_ctl_elem_value *ucontrol)
1433{
Jarkko Nikula9d035452011-05-13 19:16:52 +03001434 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1435 struct snd_soc_dapm_widget *w = wlist->widgets[0];
Mark Brown9e6e96a2010-01-29 17:47:12 +00001436 struct snd_soc_codec *codec = w->codec;
1437 int ret;
1438
1439 ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
1440
Mark Brownc3403042012-04-26 21:29:29 +01001441 wm_hubs_update_class_w(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001442
1443 return ret;
1444}
1445
1446static const struct snd_kcontrol_new dac1l_mix[] = {
1447WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1448 5, 1, 0),
1449WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1450 4, 1, 0),
1451WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1452 2, 1, 0),
1453WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1454 1, 1, 0),
1455WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1456 0, 1, 0),
1457};
1458
1459static const struct snd_kcontrol_new dac1r_mix[] = {
1460WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1461 5, 1, 0),
1462WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1463 4, 1, 0),
1464WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1465 2, 1, 0),
1466WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1467 1, 1, 0),
1468WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1469 0, 1, 0),
1470};
1471
1472static const char *sidetone_text[] = {
1473 "ADC/DMIC1", "DMIC2",
1474};
1475
1476static const struct soc_enum sidetone1_enum =
1477 SOC_ENUM_SINGLE(WM8994_SIDETONE, 0, 2, sidetone_text);
1478
1479static const struct snd_kcontrol_new sidetone1_mux =
1480 SOC_DAPM_ENUM("Left Sidetone Mux", sidetone1_enum);
1481
1482static const struct soc_enum sidetone2_enum =
1483 SOC_ENUM_SINGLE(WM8994_SIDETONE, 1, 2, sidetone_text);
1484
1485static const struct snd_kcontrol_new sidetone2_mux =
1486 SOC_DAPM_ENUM("Right Sidetone Mux", sidetone2_enum);
1487
1488static const char *aif1dac_text[] = {
1489 "AIF1DACDAT", "AIF3DACDAT",
1490};
1491
1492static const struct soc_enum aif1dac_enum =
1493 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 0, 2, aif1dac_text);
1494
1495static const struct snd_kcontrol_new aif1dac_mux =
1496 SOC_DAPM_ENUM("AIF1DAC Mux", aif1dac_enum);
1497
1498static const char *aif2dac_text[] = {
1499 "AIF2DACDAT", "AIF3DACDAT",
1500};
1501
1502static const struct soc_enum aif2dac_enum =
1503 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 1, 2, aif2dac_text);
1504
1505static const struct snd_kcontrol_new aif2dac_mux =
1506 SOC_DAPM_ENUM("AIF2DAC Mux", aif2dac_enum);
1507
1508static const char *aif2adc_text[] = {
1509 "AIF2ADCDAT", "AIF3DACDAT",
1510};
1511
1512static const struct soc_enum aif2adc_enum =
1513 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 2, 2, aif2adc_text);
1514
1515static const struct snd_kcontrol_new aif2adc_mux =
1516 SOC_DAPM_ENUM("AIF2ADC Mux", aif2adc_enum);
1517
1518static const char *aif3adc_text[] = {
Mark Brownc4431df2010-11-26 15:21:07 +00001519 "AIF1ADCDAT", "AIF2ADCDAT", "AIF2DACDAT", "Mono PCM",
Mark Brown9e6e96a2010-01-29 17:47:12 +00001520};
1521
Mark Brownc4431df2010-11-26 15:21:07 +00001522static const struct soc_enum wm8994_aif3adc_enum =
Mark Brown9e6e96a2010-01-29 17:47:12 +00001523 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 3, 3, aif3adc_text);
1524
Mark Brownc4431df2010-11-26 15:21:07 +00001525static const struct snd_kcontrol_new wm8994_aif3adc_mux =
1526 SOC_DAPM_ENUM("AIF3ADC Mux", wm8994_aif3adc_enum);
1527
1528static const struct soc_enum wm8958_aif3adc_enum =
1529 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 3, 4, aif3adc_text);
1530
1531static const struct snd_kcontrol_new wm8958_aif3adc_mux =
1532 SOC_DAPM_ENUM("AIF3ADC Mux", wm8958_aif3adc_enum);
1533
1534static const char *mono_pcm_out_text[] = {
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02001535 "None", "AIF2ADCL", "AIF2ADCR",
Mark Brownc4431df2010-11-26 15:21:07 +00001536};
1537
1538static const struct soc_enum mono_pcm_out_enum =
1539 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 9, 3, mono_pcm_out_text);
1540
1541static const struct snd_kcontrol_new mono_pcm_out_mux =
1542 SOC_DAPM_ENUM("Mono PCM Out Mux", mono_pcm_out_enum);
1543
1544static const char *aif2dac_src_text[] = {
1545 "AIF2", "AIF3",
1546};
1547
1548/* Note that these two control shouldn't be simultaneously switched to AIF3 */
1549static const struct soc_enum aif2dacl_src_enum =
1550 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 7, 2, aif2dac_src_text);
1551
1552static const struct snd_kcontrol_new aif2dacl_src_mux =
1553 SOC_DAPM_ENUM("AIF2DACL Mux", aif2dacl_src_enum);
1554
1555static const struct soc_enum aif2dacr_src_enum =
1556 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 8, 2, aif2dac_src_text);
1557
1558static const struct snd_kcontrol_new aif2dacr_src_mux =
1559 SOC_DAPM_ENUM("AIF2DACR Mux", aif2dacr_src_enum);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001560
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001561static const struct snd_soc_dapm_widget wm8994_lateclk_revd_widgets[] = {
Mark Brown1a383362012-04-12 19:47:11 +01001562SND_SOC_DAPM_SUPPLY("AIF1CLK", SND_SOC_NOPM, 0, 0, aif1clk_late_ev,
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001563 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown1a383362012-04-12 19:47:11 +01001564SND_SOC_DAPM_SUPPLY("AIF2CLK", SND_SOC_NOPM, 0, 0, aif2clk_late_ev,
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001565 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1566
1567SND_SOC_DAPM_PGA_E("Late DAC1L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1568 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1569SND_SOC_DAPM_PGA_E("Late DAC1R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1570 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1571SND_SOC_DAPM_PGA_E("Late DAC2L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1572 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1573SND_SOC_DAPM_PGA_E("Late DAC2R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1574 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Mark Brownb70a51b2011-06-29 00:21:09 -07001575SND_SOC_DAPM_PGA_E("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0,
1576 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1577
1578SND_SOC_DAPM_MIXER_E("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
1579 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer),
1580 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1581SND_SOC_DAPM_MIXER_E("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
1582 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer),
1583 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Mark Brownc3403042012-04-26 21:29:29 +01001584SND_SOC_DAPM_MUX_E("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpl_mux,
Mark Brownb70a51b2011-06-29 00:21:09 -07001585 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Mark Brownc3403042012-04-26 21:29:29 +01001586SND_SOC_DAPM_MUX_E("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpr_mux,
Mark Brownb70a51b2011-06-29 00:21:09 -07001587 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001588
1589SND_SOC_DAPM_POST("Late Disable PGA", late_disable_ev)
1590};
1591
1592static const struct snd_soc_dapm_widget wm8994_lateclk_widgets[] = {
Mark Brown1a383362012-04-12 19:47:11 +01001593SND_SOC_DAPM_SUPPLY("AIF1CLK", WM8994_AIF1_CLOCKING_1, 0, 0, aif1clk_ev,
Mark Brownbfd37bb2012-06-05 12:31:32 +01001594 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
1595 SND_SOC_DAPM_PRE_PMD),
Mark Brown1a383362012-04-12 19:47:11 +01001596SND_SOC_DAPM_SUPPLY("AIF2CLK", WM8994_AIF2_CLOCKING_1, 0, 0, aif2clk_ev,
Mark Brownbfd37bb2012-06-05 12:31:32 +01001597 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
1598 SND_SOC_DAPM_PRE_PMD),
Mark Brownb70a51b2011-06-29 00:21:09 -07001599SND_SOC_DAPM_PGA("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0),
1600SND_SOC_DAPM_MIXER("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
1601 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
1602SND_SOC_DAPM_MIXER("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
1603 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
Mark Brownc3403042012-04-26 21:29:29 +01001604SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpl_mux),
1605SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpr_mux),
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001606};
1607
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001608static const struct snd_soc_dapm_widget wm8994_dac_revd_widgets[] = {
1609SND_SOC_DAPM_DAC_E("DAC2L", NULL, SND_SOC_NOPM, 3, 0,
1610 dac_ev, SND_SOC_DAPM_PRE_PMU),
1611SND_SOC_DAPM_DAC_E("DAC2R", NULL, SND_SOC_NOPM, 2, 0,
1612 dac_ev, SND_SOC_DAPM_PRE_PMU),
1613SND_SOC_DAPM_DAC_E("DAC1L", NULL, SND_SOC_NOPM, 1, 0,
1614 dac_ev, SND_SOC_DAPM_PRE_PMU),
1615SND_SOC_DAPM_DAC_E("DAC1R", NULL, SND_SOC_NOPM, 0, 0,
1616 dac_ev, SND_SOC_DAPM_PRE_PMU),
1617};
1618
1619static const struct snd_soc_dapm_widget wm8994_dac_widgets[] = {
1620SND_SOC_DAPM_DAC("DAC2L", NULL, WM8994_POWER_MANAGEMENT_5, 3, 0),
Mark Brown0627bd22011-03-09 19:09:17 +00001621SND_SOC_DAPM_DAC("DAC2R", NULL, WM8994_POWER_MANAGEMENT_5, 2, 0),
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001622SND_SOC_DAPM_DAC("DAC1L", NULL, WM8994_POWER_MANAGEMENT_5, 1, 0),
1623SND_SOC_DAPM_DAC("DAC1R", NULL, WM8994_POWER_MANAGEMENT_5, 0, 0),
1624};
1625
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001626static const struct snd_soc_dapm_widget wm8994_adc_revd_widgets[] = {
Mark Brown87b86ad2011-08-14 13:39:20 +09001627SND_SOC_DAPM_VIRT_MUX_E("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux,
1628 adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
1629SND_SOC_DAPM_VIRT_MUX_E("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux,
1630 adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001631};
1632
1633static const struct snd_soc_dapm_widget wm8994_adc_widgets[] = {
Mark Brown87b86ad2011-08-14 13:39:20 +09001634SND_SOC_DAPM_VIRT_MUX("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux),
1635SND_SOC_DAPM_VIRT_MUX("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux),
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001636};
1637
Mark Brown9e6e96a2010-01-29 17:47:12 +00001638static const struct snd_soc_dapm_widget wm8994_dapm_widgets[] = {
1639SND_SOC_DAPM_INPUT("DMIC1DAT"),
1640SND_SOC_DAPM_INPUT("DMIC2DAT"),
Mark Brown66b47fd2010-07-08 11:25:43 +09001641SND_SOC_DAPM_INPUT("Clock"),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001642
Dimitris Papastamosb462c6e2011-03-01 12:54:39 +00001643SND_SOC_DAPM_SUPPLY_S("MICBIAS Supply", 1, SND_SOC_NOPM, 0, 0, micbias_ev,
1644 SND_SOC_DAPM_PRE_PMU),
Mark Brown4b7ed832011-08-10 17:47:33 +09001645SND_SOC_DAPM_SUPPLY("VMID", SND_SOC_NOPM, 0, 0, vmid_event,
1646 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
Dimitris Papastamosb462c6e2011-03-01 12:54:39 +00001647
Mark Brown9e6e96a2010-01-29 17:47:12 +00001648SND_SOC_DAPM_SUPPLY("CLK_SYS", SND_SOC_NOPM, 0, 0, clk_sys_event,
Mark Brown99af79d2012-07-25 23:03:36 +01001649 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
1650 SND_SOC_DAPM_PRE_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001651
Mark Brown1a383362012-04-12 19:47:11 +01001652SND_SOC_DAPM_SUPPLY("DSP1CLK", SND_SOC_NOPM, 3, 0, NULL, 0),
1653SND_SOC_DAPM_SUPPLY("DSP2CLK", SND_SOC_NOPM, 2, 0, NULL, 0),
1654SND_SOC_DAPM_SUPPLY("DSPINTCLK", SND_SOC_NOPM, 1, 0, NULL, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001655
Mark Brown7f94de42011-02-03 16:27:34 +00001656SND_SOC_DAPM_AIF_OUT("AIF1ADC1L", NULL,
Mark Brown1a383362012-04-12 19:47:11 +01001657 0, SND_SOC_NOPM, 9, 0),
Mark Brown7f94de42011-02-03 16:27:34 +00001658SND_SOC_DAPM_AIF_OUT("AIF1ADC1R", NULL,
Mark Brown1a383362012-04-12 19:47:11 +01001659 0, SND_SOC_NOPM, 8, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001660SND_SOC_DAPM_AIF_IN_E("AIF1DAC1L", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001661 SND_SOC_NOPM, 9, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001662 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brownd6addcc2010-11-26 15:21:08 +00001663SND_SOC_DAPM_AIF_IN_E("AIF1DAC1R", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001664 SND_SOC_NOPM, 8, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001665 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001666
Mark Brown7f94de42011-02-03 16:27:34 +00001667SND_SOC_DAPM_AIF_OUT("AIF1ADC2L", NULL,
Mark Brown1a383362012-04-12 19:47:11 +01001668 0, SND_SOC_NOPM, 11, 0),
Mark Brown7f94de42011-02-03 16:27:34 +00001669SND_SOC_DAPM_AIF_OUT("AIF1ADC2R", NULL,
Mark Brown1a383362012-04-12 19:47:11 +01001670 0, SND_SOC_NOPM, 10, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001671SND_SOC_DAPM_AIF_IN_E("AIF1DAC2L", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001672 SND_SOC_NOPM, 11, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001673 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brownd6addcc2010-11-26 15:21:08 +00001674SND_SOC_DAPM_AIF_IN_E("AIF1DAC2R", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001675 SND_SOC_NOPM, 10, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001676 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001677
1678SND_SOC_DAPM_MIXER("AIF1ADC1L Mixer", SND_SOC_NOPM, 0, 0,
1679 aif1adc1l_mix, ARRAY_SIZE(aif1adc1l_mix)),
1680SND_SOC_DAPM_MIXER("AIF1ADC1R Mixer", SND_SOC_NOPM, 0, 0,
1681 aif1adc1r_mix, ARRAY_SIZE(aif1adc1r_mix)),
1682
Mark Browna3257ba2010-07-19 14:02:34 +01001683SND_SOC_DAPM_MIXER("AIF1ADC2L Mixer", SND_SOC_NOPM, 0, 0,
1684 aif1adc2l_mix, ARRAY_SIZE(aif1adc2l_mix)),
1685SND_SOC_DAPM_MIXER("AIF1ADC2R Mixer", SND_SOC_NOPM, 0, 0,
1686 aif1adc2r_mix, ARRAY_SIZE(aif1adc2r_mix)),
1687
Mark Brown9e6e96a2010-01-29 17:47:12 +00001688SND_SOC_DAPM_MIXER("AIF2DAC2L Mixer", SND_SOC_NOPM, 0, 0,
1689 aif2dac2l_mix, ARRAY_SIZE(aif2dac2l_mix)),
1690SND_SOC_DAPM_MIXER("AIF2DAC2R Mixer", SND_SOC_NOPM, 0, 0,
1691 aif2dac2r_mix, ARRAY_SIZE(aif2dac2r_mix)),
1692
1693SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &sidetone1_mux),
1694SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &sidetone2_mux),
1695
1696SND_SOC_DAPM_MIXER("DAC1L Mixer", SND_SOC_NOPM, 0, 0,
1697 dac1l_mix, ARRAY_SIZE(dac1l_mix)),
1698SND_SOC_DAPM_MIXER("DAC1R Mixer", SND_SOC_NOPM, 0, 0,
1699 dac1r_mix, ARRAY_SIZE(dac1r_mix)),
1700
1701SND_SOC_DAPM_AIF_OUT("AIF2ADCL", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001702 SND_SOC_NOPM, 13, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001703SND_SOC_DAPM_AIF_OUT("AIF2ADCR", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001704 SND_SOC_NOPM, 12, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001705SND_SOC_DAPM_AIF_IN_E("AIF2DACL", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001706 SND_SOC_NOPM, 13, 0, wm8958_aif_ev,
Mark Brownd6addcc2010-11-26 15:21:08 +00001707 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1708SND_SOC_DAPM_AIF_IN_E("AIF2DACR", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001709 SND_SOC_NOPM, 12, 0, wm8958_aif_ev,
Mark Brownd6addcc2010-11-26 15:21:08 +00001710 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001711
Mark Brown5567d8c2012-02-16 21:43:29 -08001712SND_SOC_DAPM_AIF_IN("AIF1DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1713SND_SOC_DAPM_AIF_IN("AIF2DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1714SND_SOC_DAPM_AIF_OUT("AIF1ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1715SND_SOC_DAPM_AIF_OUT("AIF2ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001716
1717SND_SOC_DAPM_MUX("AIF1DAC Mux", SND_SOC_NOPM, 0, 0, &aif1dac_mux),
1718SND_SOC_DAPM_MUX("AIF2DAC Mux", SND_SOC_NOPM, 0, 0, &aif2dac_mux),
1719SND_SOC_DAPM_MUX("AIF2ADC Mux", SND_SOC_NOPM, 0, 0, &aif2adc_mux),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001720
Mark Brown5567d8c2012-02-16 21:43:29 -08001721SND_SOC_DAPM_AIF_IN("AIF3DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1722SND_SOC_DAPM_AIF_OUT("AIF3ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001723
1724SND_SOC_DAPM_SUPPLY("TOCLK", WM8994_CLOCKING_1, 4, 0, NULL, 0),
1725
1726SND_SOC_DAPM_ADC("DMIC2L", NULL, WM8994_POWER_MANAGEMENT_4, 5, 0),
1727SND_SOC_DAPM_ADC("DMIC2R", NULL, WM8994_POWER_MANAGEMENT_4, 4, 0),
1728SND_SOC_DAPM_ADC("DMIC1L", NULL, WM8994_POWER_MANAGEMENT_4, 3, 0),
1729SND_SOC_DAPM_ADC("DMIC1R", NULL, WM8994_POWER_MANAGEMENT_4, 2, 0),
1730
1731/* Power is done with the muxes since the ADC power also controls the
1732 * downsampling chain, the chip will automatically manage the analogue
1733 * specific portions.
1734 */
1735SND_SOC_DAPM_ADC("ADCL", NULL, SND_SOC_NOPM, 1, 0),
1736SND_SOC_DAPM_ADC("ADCR", NULL, SND_SOC_NOPM, 0, 0),
1737
Mark Brown9e6e96a2010-01-29 17:47:12 +00001738SND_SOC_DAPM_POST("Debug log", post_ev),
1739};
1740
Mark Brownc4431df2010-11-26 15:21:07 +00001741static const struct snd_soc_dapm_widget wm8994_specific_dapm_widgets[] = {
1742SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8994_aif3adc_mux),
1743};
Mark Brown9e6e96a2010-01-29 17:47:12 +00001744
Mark Brownc4431df2010-11-26 15:21:07 +00001745static const struct snd_soc_dapm_widget wm8958_dapm_widgets[] = {
Mark Brown8c5b8422012-04-17 20:49:05 +01001746SND_SOC_DAPM_SUPPLY("AIF3", WM8994_POWER_MANAGEMENT_6, 5, 1, NULL, 0),
Mark Brownc4431df2010-11-26 15:21:07 +00001747SND_SOC_DAPM_MUX("Mono PCM Out Mux", SND_SOC_NOPM, 0, 0, &mono_pcm_out_mux),
1748SND_SOC_DAPM_MUX("AIF2DACL Mux", SND_SOC_NOPM, 0, 0, &aif2dacl_src_mux),
1749SND_SOC_DAPM_MUX("AIF2DACR Mux", SND_SOC_NOPM, 0, 0, &aif2dacr_src_mux),
1750SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8958_aif3adc_mux),
1751};
1752
1753static const struct snd_soc_dapm_route intercon[] = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00001754 { "CLK_SYS", NULL, "AIF1CLK", check_clk_sys },
1755 { "CLK_SYS", NULL, "AIF2CLK", check_clk_sys },
1756
1757 { "DSP1CLK", NULL, "CLK_SYS" },
1758 { "DSP2CLK", NULL, "CLK_SYS" },
1759 { "DSPINTCLK", NULL, "CLK_SYS" },
1760
1761 { "AIF1ADC1L", NULL, "AIF1CLK" },
1762 { "AIF1ADC1L", NULL, "DSP1CLK" },
1763 { "AIF1ADC1R", NULL, "AIF1CLK" },
1764 { "AIF1ADC1R", NULL, "DSP1CLK" },
1765 { "AIF1ADC1R", NULL, "DSPINTCLK" },
1766
1767 { "AIF1DAC1L", NULL, "AIF1CLK" },
1768 { "AIF1DAC1L", NULL, "DSP1CLK" },
1769 { "AIF1DAC1R", NULL, "AIF1CLK" },
1770 { "AIF1DAC1R", NULL, "DSP1CLK" },
1771 { "AIF1DAC1R", NULL, "DSPINTCLK" },
1772
1773 { "AIF1ADC2L", NULL, "AIF1CLK" },
1774 { "AIF1ADC2L", NULL, "DSP1CLK" },
1775 { "AIF1ADC2R", NULL, "AIF1CLK" },
1776 { "AIF1ADC2R", NULL, "DSP1CLK" },
1777 { "AIF1ADC2R", NULL, "DSPINTCLK" },
1778
1779 { "AIF1DAC2L", NULL, "AIF1CLK" },
1780 { "AIF1DAC2L", NULL, "DSP1CLK" },
1781 { "AIF1DAC2R", NULL, "AIF1CLK" },
1782 { "AIF1DAC2R", NULL, "DSP1CLK" },
1783 { "AIF1DAC2R", NULL, "DSPINTCLK" },
1784
1785 { "AIF2ADCL", NULL, "AIF2CLK" },
1786 { "AIF2ADCL", NULL, "DSP2CLK" },
1787 { "AIF2ADCR", NULL, "AIF2CLK" },
1788 { "AIF2ADCR", NULL, "DSP2CLK" },
1789 { "AIF2ADCR", NULL, "DSPINTCLK" },
1790
1791 { "AIF2DACL", NULL, "AIF2CLK" },
1792 { "AIF2DACL", NULL, "DSP2CLK" },
1793 { "AIF2DACR", NULL, "AIF2CLK" },
1794 { "AIF2DACR", NULL, "DSP2CLK" },
1795 { "AIF2DACR", NULL, "DSPINTCLK" },
1796
1797 { "DMIC1L", NULL, "DMIC1DAT" },
1798 { "DMIC1L", NULL, "CLK_SYS" },
1799 { "DMIC1R", NULL, "DMIC1DAT" },
1800 { "DMIC1R", NULL, "CLK_SYS" },
1801 { "DMIC2L", NULL, "DMIC2DAT" },
1802 { "DMIC2L", NULL, "CLK_SYS" },
1803 { "DMIC2R", NULL, "DMIC2DAT" },
1804 { "DMIC2R", NULL, "CLK_SYS" },
1805
1806 { "ADCL", NULL, "AIF1CLK" },
1807 { "ADCL", NULL, "DSP1CLK" },
1808 { "ADCL", NULL, "DSPINTCLK" },
1809
1810 { "ADCR", NULL, "AIF1CLK" },
1811 { "ADCR", NULL, "DSP1CLK" },
1812 { "ADCR", NULL, "DSPINTCLK" },
1813
1814 { "ADCL Mux", "ADC", "ADCL" },
1815 { "ADCL Mux", "DMIC", "DMIC1L" },
1816 { "ADCR Mux", "ADC", "ADCR" },
1817 { "ADCR Mux", "DMIC", "DMIC1R" },
1818
1819 { "DAC1L", NULL, "AIF1CLK" },
1820 { "DAC1L", NULL, "DSP1CLK" },
1821 { "DAC1L", NULL, "DSPINTCLK" },
1822
1823 { "DAC1R", NULL, "AIF1CLK" },
1824 { "DAC1R", NULL, "DSP1CLK" },
1825 { "DAC1R", NULL, "DSPINTCLK" },
1826
1827 { "DAC2L", NULL, "AIF2CLK" },
1828 { "DAC2L", NULL, "DSP2CLK" },
1829 { "DAC2L", NULL, "DSPINTCLK" },
1830
1831 { "DAC2R", NULL, "AIF2DACR" },
1832 { "DAC2R", NULL, "AIF2CLK" },
1833 { "DAC2R", NULL, "DSP2CLK" },
1834 { "DAC2R", NULL, "DSPINTCLK" },
1835
1836 { "TOCLK", NULL, "CLK_SYS" },
1837
Mark Brown5567d8c2012-02-16 21:43:29 -08001838 { "AIF1DACDAT", NULL, "AIF1 Playback" },
1839 { "AIF2DACDAT", NULL, "AIF2 Playback" },
1840 { "AIF3DACDAT", NULL, "AIF3 Playback" },
1841
1842 { "AIF1 Capture", NULL, "AIF1ADCDAT" },
1843 { "AIF2 Capture", NULL, "AIF2ADCDAT" },
1844 { "AIF3 Capture", NULL, "AIF3ADCDAT" },
1845
Mark Brown9e6e96a2010-01-29 17:47:12 +00001846 /* AIF1 outputs */
1847 { "AIF1ADC1L", NULL, "AIF1ADC1L Mixer" },
1848 { "AIF1ADC1L Mixer", "ADC/DMIC Switch", "ADCL Mux" },
1849 { "AIF1ADC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1850
1851 { "AIF1ADC1R", NULL, "AIF1ADC1R Mixer" },
1852 { "AIF1ADC1R Mixer", "ADC/DMIC Switch", "ADCR Mux" },
1853 { "AIF1ADC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1854
Mark Browna3257ba2010-07-19 14:02:34 +01001855 { "AIF1ADC2L", NULL, "AIF1ADC2L Mixer" },
1856 { "AIF1ADC2L Mixer", "DMIC Switch", "DMIC2L" },
1857 { "AIF1ADC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1858
1859 { "AIF1ADC2R", NULL, "AIF1ADC2R Mixer" },
1860 { "AIF1ADC2R Mixer", "DMIC Switch", "DMIC2R" },
1861 { "AIF1ADC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1862
Mark Brown9e6e96a2010-01-29 17:47:12 +00001863 /* Pin level routing for AIF3 */
1864 { "AIF1DAC1L", NULL, "AIF1DAC Mux" },
1865 { "AIF1DAC1R", NULL, "AIF1DAC Mux" },
1866 { "AIF1DAC2L", NULL, "AIF1DAC Mux" },
1867 { "AIF1DAC2R", NULL, "AIF1DAC Mux" },
1868
Mark Brown9e6e96a2010-01-29 17:47:12 +00001869 { "AIF1DAC Mux", "AIF1DACDAT", "AIF1DACDAT" },
1870 { "AIF1DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1871 { "AIF2DAC Mux", "AIF2DACDAT", "AIF2DACDAT" },
1872 { "AIF2DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1873 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCL" },
1874 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCR" },
1875 { "AIF2ADC Mux", "AIF3DACDAT", "AIF3ADCDAT" },
1876
1877 /* DAC1 inputs */
Mark Brown9e6e96a2010-01-29 17:47:12 +00001878 { "DAC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1879 { "DAC1L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1880 { "DAC1L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1881 { "DAC1L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1882 { "DAC1L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1883
Mark Brown9e6e96a2010-01-29 17:47:12 +00001884 { "DAC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1885 { "DAC1R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1886 { "DAC1R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1887 { "DAC1R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1888 { "DAC1R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1889
1890 /* DAC2/AIF2 outputs */
1891 { "AIF2ADCL", NULL, "AIF2DAC2L Mixer" },
Mark Brown9e6e96a2010-01-29 17:47:12 +00001892 { "AIF2DAC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1893 { "AIF2DAC2L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1894 { "AIF2DAC2L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1895 { "AIF2DAC2L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1896 { "AIF2DAC2L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1897
1898 { "AIF2ADCR", NULL, "AIF2DAC2R Mixer" },
Mark Brown9e6e96a2010-01-29 17:47:12 +00001899 { "AIF2DAC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1900 { "AIF2DAC2R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1901 { "AIF2DAC2R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1902 { "AIF2DAC2R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1903 { "AIF2DAC2R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1904
Mark Brown7f94de42011-02-03 16:27:34 +00001905 { "AIF1ADCDAT", NULL, "AIF1ADC1L" },
1906 { "AIF1ADCDAT", NULL, "AIF1ADC1R" },
1907 { "AIF1ADCDAT", NULL, "AIF1ADC2L" },
1908 { "AIF1ADCDAT", NULL, "AIF1ADC2R" },
1909
Mark Brown9e6e96a2010-01-29 17:47:12 +00001910 { "AIF2ADCDAT", NULL, "AIF2ADC Mux" },
1911
1912 /* AIF3 output */
1913 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1L" },
1914 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1R" },
1915 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2L" },
1916 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2R" },
1917 { "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCL" },
1918 { "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCR" },
1919 { "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACL" },
1920 { "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACR" },
1921
1922 /* Sidetone */
1923 { "Left Sidetone", "ADC/DMIC1", "ADCL Mux" },
1924 { "Left Sidetone", "DMIC2", "DMIC2L" },
1925 { "Right Sidetone", "ADC/DMIC1", "ADCR Mux" },
1926 { "Right Sidetone", "DMIC2", "DMIC2R" },
1927
1928 /* Output stages */
1929 { "Left Output Mixer", "DAC Switch", "DAC1L" },
1930 { "Right Output Mixer", "DAC Switch", "DAC1R" },
1931
1932 { "SPKL", "DAC1 Switch", "DAC1L" },
1933 { "SPKL", "DAC2 Switch", "DAC2L" },
1934
1935 { "SPKR", "DAC1 Switch", "DAC1R" },
1936 { "SPKR", "DAC2 Switch", "DAC2R" },
1937
1938 { "Left Headphone Mux", "DAC", "DAC1L" },
1939 { "Right Headphone Mux", "DAC", "DAC1R" },
1940};
1941
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001942static const struct snd_soc_dapm_route wm8994_lateclk_revd_intercon[] = {
1943 { "DAC1L", NULL, "Late DAC1L Enable PGA" },
1944 { "Late DAC1L Enable PGA", NULL, "DAC1L Mixer" },
1945 { "DAC1R", NULL, "Late DAC1R Enable PGA" },
1946 { "Late DAC1R Enable PGA", NULL, "DAC1R Mixer" },
1947 { "DAC2L", NULL, "Late DAC2L Enable PGA" },
1948 { "Late DAC2L Enable PGA", NULL, "AIF2DAC2L Mixer" },
1949 { "DAC2R", NULL, "Late DAC2R Enable PGA" },
1950 { "Late DAC2R Enable PGA", NULL, "AIF2DAC2R Mixer" }
1951};
1952
1953static const struct snd_soc_dapm_route wm8994_lateclk_intercon[] = {
1954 { "DAC1L", NULL, "DAC1L Mixer" },
1955 { "DAC1R", NULL, "DAC1R Mixer" },
1956 { "DAC2L", NULL, "AIF2DAC2L Mixer" },
1957 { "DAC2R", NULL, "AIF2DAC2R Mixer" },
1958};
1959
Mark Brown6ed8f142011-02-03 16:27:35 +00001960static const struct snd_soc_dapm_route wm8994_revd_intercon[] = {
1961 { "AIF1DACDAT", NULL, "AIF2DACDAT" },
1962 { "AIF2DACDAT", NULL, "AIF1DACDAT" },
1963 { "AIF1ADCDAT", NULL, "AIF2ADCDAT" },
1964 { "AIF2ADCDAT", NULL, "AIF1ADCDAT" },
Mark Brownb793eb62011-07-14 18:21:37 +09001965 { "MICBIAS1", NULL, "CLK_SYS" },
1966 { "MICBIAS1", NULL, "MICBIAS Supply" },
1967 { "MICBIAS2", NULL, "CLK_SYS" },
1968 { "MICBIAS2", NULL, "MICBIAS Supply" },
Mark Brown6ed8f142011-02-03 16:27:35 +00001969};
1970
Mark Brownc4431df2010-11-26 15:21:07 +00001971static const struct snd_soc_dapm_route wm8994_intercon[] = {
1972 { "AIF2DACL", NULL, "AIF2DAC Mux" },
1973 { "AIF2DACR", NULL, "AIF2DAC Mux" },
Mark Brown4e04ada2011-07-15 15:12:31 +09001974 { "MICBIAS1", NULL, "VMID" },
1975 { "MICBIAS2", NULL, "VMID" },
Mark Brownc4431df2010-11-26 15:21:07 +00001976};
1977
1978static const struct snd_soc_dapm_route wm8958_intercon[] = {
1979 { "AIF2DACL", NULL, "AIF2DACL Mux" },
1980 { "AIF2DACR", NULL, "AIF2DACR Mux" },
1981
1982 { "AIF2DACL Mux", "AIF2", "AIF2DAC Mux" },
1983 { "AIF2DACL Mux", "AIF3", "AIF3DACDAT" },
1984 { "AIF2DACR Mux", "AIF2", "AIF2DAC Mux" },
1985 { "AIF2DACR Mux", "AIF3", "AIF3DACDAT" },
1986
Mark Brown8c5b8422012-04-17 20:49:05 +01001987 { "AIF3DACDAT", NULL, "AIF3" },
1988 { "AIF3ADCDAT", NULL, "AIF3" },
1989
Mark Brownc4431df2010-11-26 15:21:07 +00001990 { "Mono PCM Out Mux", "AIF2ADCL", "AIF2ADCL" },
1991 { "Mono PCM Out Mux", "AIF2ADCR", "AIF2ADCR" },
1992
1993 { "AIF3ADC Mux", "Mono PCM", "Mono PCM Out Mux" },
1994};
1995
Mark Brown9e6e96a2010-01-29 17:47:12 +00001996/* The size in bits of the FLL divide multiplied by 10
1997 * to allow rounding later */
1998#define FIXED_FLL_SIZE ((1 << 16) * 10)
1999
2000struct fll_div {
2001 u16 outdiv;
2002 u16 n;
2003 u16 k;
2004 u16 clk_ref_div;
2005 u16 fll_fratio;
2006};
2007
2008static int wm8994_get_fll_config(struct fll_div *fll,
2009 int freq_in, int freq_out)
2010{
2011 u64 Kpart;
2012 unsigned int K, Ndiv, Nmod;
2013
2014 pr_debug("FLL input=%dHz, output=%dHz\n", freq_in, freq_out);
2015
2016 /* Scale the input frequency down to <= 13.5MHz */
2017 fll->clk_ref_div = 0;
2018 while (freq_in > 13500000) {
2019 fll->clk_ref_div++;
2020 freq_in /= 2;
2021
2022 if (fll->clk_ref_div > 3)
2023 return -EINVAL;
2024 }
2025 pr_debug("CLK_REF_DIV=%d, Fref=%dHz\n", fll->clk_ref_div, freq_in);
2026
2027 /* Scale the output to give 90MHz<=Fvco<=100MHz */
2028 fll->outdiv = 3;
2029 while (freq_out * (fll->outdiv + 1) < 90000000) {
2030 fll->outdiv++;
2031 if (fll->outdiv > 63)
2032 return -EINVAL;
2033 }
2034 freq_out *= fll->outdiv + 1;
2035 pr_debug("OUTDIV=%d, Fvco=%dHz\n", fll->outdiv, freq_out);
2036
2037 if (freq_in > 1000000) {
2038 fll->fll_fratio = 0;
Mark Brown7d48a6a2010-04-20 13:36:11 +09002039 } else if (freq_in > 256000) {
2040 fll->fll_fratio = 1;
2041 freq_in *= 2;
2042 } else if (freq_in > 128000) {
2043 fll->fll_fratio = 2;
2044 freq_in *= 4;
2045 } else if (freq_in > 64000) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002046 fll->fll_fratio = 3;
2047 freq_in *= 8;
Mark Brown7d48a6a2010-04-20 13:36:11 +09002048 } else {
2049 fll->fll_fratio = 4;
2050 freq_in *= 16;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002051 }
2052 pr_debug("FLL_FRATIO=%d, Fref=%dHz\n", fll->fll_fratio, freq_in);
2053
2054 /* Now, calculate N.K */
2055 Ndiv = freq_out / freq_in;
2056
2057 fll->n = Ndiv;
2058 Nmod = freq_out % freq_in;
2059 pr_debug("Nmod=%d\n", Nmod);
2060
2061 /* Calculate fractional part - scale up so we can round. */
2062 Kpart = FIXED_FLL_SIZE * (long long)Nmod;
2063
2064 do_div(Kpart, freq_in);
2065
2066 K = Kpart & 0xFFFFFFFF;
2067
2068 if ((K % 10) >= 5)
2069 K += 5;
2070
2071 /* Move down to proper range now rounding is done */
2072 fll->k = K / 10;
2073
2074 pr_debug("N=%x K=%x\n", fll->n, fll->k);
2075
2076 return 0;
2077}
2078
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002079static int _wm8994_set_fll(struct snd_soc_codec *codec, int id, int src,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002080 unsigned int freq_in, unsigned int freq_out)
2081{
Mark Brownb2c812e2010-04-14 15:35:19 +09002082 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01002083 struct wm8994 *control = wm8994->wm8994;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002084 int reg_offset, ret;
2085 struct fll_div fll;
Mark Browne413ba82012-03-29 14:49:27 +01002086 u16 reg, clk1, aif_reg, aif_src;
Mark Brownc7ebf932011-07-12 19:47:59 +09002087 unsigned long timeout;
Mark Brown4b7ed832011-08-10 17:47:33 +09002088 bool was_enabled;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002089
Mark Brown9e6e96a2010-01-29 17:47:12 +00002090 switch (id) {
2091 case WM8994_FLL1:
2092 reg_offset = 0;
2093 id = 0;
Mark Browne413ba82012-03-29 14:49:27 +01002094 aif_src = 0x10;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002095 break;
2096 case WM8994_FLL2:
2097 reg_offset = 0x20;
2098 id = 1;
Mark Browne413ba82012-03-29 14:49:27 +01002099 aif_src = 0x18;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002100 break;
2101 default:
2102 return -EINVAL;
2103 }
2104
Mark Brown4b7ed832011-08-10 17:47:33 +09002105 reg = snd_soc_read(codec, WM8994_FLL1_CONTROL_1 + reg_offset);
2106 was_enabled = reg & WM8994_FLL1_ENA;
2107
Mark Brown136ff2a2010-04-20 12:56:18 +09002108 switch (src) {
Mark Brown7add84a2010-04-22 02:29:01 +09002109 case 0:
2110 /* Allow no source specification when stopping */
2111 if (freq_out)
2112 return -EINVAL;
Mark Brown4514e892010-12-03 16:02:10 +00002113 src = wm8994->fll[id].src;
Mark Brown7add84a2010-04-22 02:29:01 +09002114 break;
Mark Brown136ff2a2010-04-20 12:56:18 +09002115 case WM8994_FLL_SRC_MCLK1:
2116 case WM8994_FLL_SRC_MCLK2:
2117 case WM8994_FLL_SRC_LRCLK:
2118 case WM8994_FLL_SRC_BCLK:
2119 break;
Mark Brownfbfe6982012-07-23 20:14:43 +01002120 case WM8994_FLL_SRC_INTERNAL:
2121 freq_in = 12000000;
2122 freq_out = 12000000;
2123 break;
Mark Brown136ff2a2010-04-20 12:56:18 +09002124 default:
2125 return -EINVAL;
2126 }
2127
Mark Brown9e6e96a2010-01-29 17:47:12 +00002128 /* Are we changing anything? */
2129 if (wm8994->fll[id].src == src &&
2130 wm8994->fll[id].in == freq_in && wm8994->fll[id].out == freq_out)
2131 return 0;
2132
2133 /* If we're stopping the FLL redo the old config - no
2134 * registers will actually be written but we avoid GCC flow
2135 * analysis bugs spewing warnings.
2136 */
2137 if (freq_out)
2138 ret = wm8994_get_fll_config(&fll, freq_in, freq_out);
2139 else
2140 ret = wm8994_get_fll_config(&fll, wm8994->fll[id].in,
2141 wm8994->fll[id].out);
2142 if (ret < 0)
2143 return ret;
2144
Mark Browne413ba82012-03-29 14:49:27 +01002145 /* Make sure that we're not providing SYSCLK right now */
2146 clk1 = snd_soc_read(codec, WM8994_CLOCKING_1);
2147 if (clk1 & WM8994_SYSCLK_SRC)
2148 aif_reg = WM8994_AIF2_CLOCKING_1;
2149 else
2150 aif_reg = WM8994_AIF1_CLOCKING_1;
2151 reg = snd_soc_read(codec, aif_reg);
2152
2153 if ((reg & WM8994_AIF1CLK_ENA) &&
2154 (reg & WM8994_AIF1CLK_SRC_MASK) == aif_src) {
2155 dev_err(codec->dev, "FLL%d is currently providing SYSCLK\n",
2156 id + 1);
2157 return -EBUSY;
2158 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002159
2160 /* We always need to disable the FLL while reconfiguring */
2161 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
2162 WM8994_FLL1_ENA, 0);
2163
Mark Brown20dc24a2012-04-05 12:55:20 +01002164 if (wm8994->fll_byp && src == WM8994_FLL_SRC_BCLK &&
Kyung-Kwee Ryue05854d2012-04-24 18:01:48 +01002165 freq_in == freq_out && freq_out) {
Mark Brown20dc24a2012-04-05 12:55:20 +01002166 dev_dbg(codec->dev, "Bypassing FLL%d\n", id + 1);
2167 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_5 + reg_offset,
2168 WM8958_FLL1_BYP, WM8958_FLL1_BYP);
2169 goto out;
2170 }
2171
Mark Brown9e6e96a2010-01-29 17:47:12 +00002172 reg = (fll.outdiv << WM8994_FLL1_OUTDIV_SHIFT) |
2173 (fll.fll_fratio << WM8994_FLL1_FRATIO_SHIFT);
2174 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_2 + reg_offset,
2175 WM8994_FLL1_OUTDIV_MASK |
2176 WM8994_FLL1_FRATIO_MASK, reg);
2177
Mark Brownb16db742012-03-03 15:33:23 +00002178 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_3 + reg_offset,
2179 WM8994_FLL1_K_MASK, fll.k);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002180
2181 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_4 + reg_offset,
2182 WM8994_FLL1_N_MASK,
Mark Brown7435d4e2012-07-26 14:49:11 +01002183 fll.n << WM8994_FLL1_N_SHIFT);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002184
2185 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_5 + reg_offset,
Mark Brownfbfe6982012-07-23 20:14:43 +01002186 WM8994_FLL1_FRC_NCO | WM8958_FLL1_BYP |
Mark Brown136ff2a2010-04-20 12:56:18 +09002187 WM8994_FLL1_REFCLK_DIV_MASK |
2188 WM8994_FLL1_REFCLK_SRC_MASK,
Mark Brownfbfe6982012-07-23 20:14:43 +01002189 ((src == WM8994_FLL_SRC_INTERNAL)
2190 << WM8994_FLL1_FRC_NCO_SHIFT) |
Mark Brown136ff2a2010-04-20 12:56:18 +09002191 (fll.clk_ref_div << WM8994_FLL1_REFCLK_DIV_SHIFT) |
2192 (src - 1));
Mark Brown9e6e96a2010-01-29 17:47:12 +00002193
Mark Brownf0f50392011-07-16 03:12:18 +09002194 /* Clear any pending completion from a previous failure */
2195 try_wait_for_completion(&wm8994->fll_locked[id]);
2196
Mark Brown9e6e96a2010-01-29 17:47:12 +00002197 /* Enable (with fractional mode if required) */
2198 if (freq_out) {
Mark Brown4b7ed832011-08-10 17:47:33 +09002199 /* Enable VMID if we need it */
2200 if (!was_enabled) {
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002201 active_reference(codec);
2202
Mark Brown4b7ed832011-08-10 17:47:33 +09002203 switch (control->type) {
2204 case WM8994:
2205 vmid_reference(codec);
2206 break;
2207 case WM8958:
2208 if (wm8994->revision < 1)
2209 vmid_reference(codec);
2210 break;
2211 default:
2212 break;
2213 }
2214 }
2215
Mark Brownfbfe6982012-07-23 20:14:43 +01002216 reg = WM8994_FLL1_ENA;
2217
Mark Brown9e6e96a2010-01-29 17:47:12 +00002218 if (fll.k)
Mark Brownfbfe6982012-07-23 20:14:43 +01002219 reg |= WM8994_FLL1_FRAC;
2220 if (src == WM8994_FLL_SRC_INTERNAL)
2221 reg |= WM8994_FLL1_OSC_ENA;
2222
Mark Brown9e6e96a2010-01-29 17:47:12 +00002223 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
Mark Brownfbfe6982012-07-23 20:14:43 +01002224 WM8994_FLL1_ENA | WM8994_FLL1_OSC_ENA |
2225 WM8994_FLL1_FRAC, reg);
Mark Brown8e9ddf82011-07-01 17:24:46 -07002226
Mark Brownc7ebf932011-07-12 19:47:59 +09002227 if (wm8994->fll_locked_irq) {
2228 timeout = wait_for_completion_timeout(&wm8994->fll_locked[id],
2229 msecs_to_jiffies(10));
2230 if (timeout == 0)
2231 dev_warn(codec->dev,
2232 "Timed out waiting for FLL lock\n");
2233 } else {
2234 msleep(5);
2235 }
Mark Brown4b7ed832011-08-10 17:47:33 +09002236 } else {
2237 if (was_enabled) {
2238 switch (control->type) {
2239 case WM8994:
2240 vmid_dereference(codec);
2241 break;
2242 case WM8958:
2243 if (wm8994->revision < 1)
2244 vmid_dereference(codec);
2245 break;
2246 default:
2247 break;
2248 }
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002249
2250 active_dereference(codec);
Mark Brown4b7ed832011-08-10 17:47:33 +09002251 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002252 }
2253
Mark Brown20dc24a2012-04-05 12:55:20 +01002254out:
Mark Brown9e6e96a2010-01-29 17:47:12 +00002255 wm8994->fll[id].in = freq_in;
2256 wm8994->fll[id].out = freq_out;
Mark Brown136ff2a2010-04-20 12:56:18 +09002257 wm8994->fll[id].src = src;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002258
Mark Brown9e6e96a2010-01-29 17:47:12 +00002259 configure_clock(codec);
2260
2261 return 0;
2262}
2263
Mark Brownc7ebf932011-07-12 19:47:59 +09002264static irqreturn_t wm8994_fll_locked_irq(int irq, void *data)
2265{
2266 struct completion *completion = data;
2267
2268 complete(completion);
2269
2270 return IRQ_HANDLED;
2271}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002272
Mark Brown66b47fd2010-07-08 11:25:43 +09002273static int opclk_divs[] = { 10, 20, 30, 40, 55, 60, 80, 120, 160 };
2274
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002275static int wm8994_set_fll(struct snd_soc_dai *dai, int id, int src,
2276 unsigned int freq_in, unsigned int freq_out)
2277{
2278 return _wm8994_set_fll(dai->codec, id, src, freq_in, freq_out);
2279}
2280
Mark Brown9e6e96a2010-01-29 17:47:12 +00002281static int wm8994_set_dai_sysclk(struct snd_soc_dai *dai,
2282 int clk_id, unsigned int freq, int dir)
2283{
2284 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09002285 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown66b47fd2010-07-08 11:25:43 +09002286 int i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002287
2288 switch (dai->id) {
2289 case 1:
2290 case 2:
2291 break;
2292
2293 default:
2294 /* AIF3 shares clocking with AIF1/2 */
2295 return -EINVAL;
2296 }
2297
2298 switch (clk_id) {
2299 case WM8994_SYSCLK_MCLK1:
2300 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK1;
2301 wm8994->mclk[0] = freq;
2302 dev_dbg(dai->dev, "AIF%d using MCLK1 at %uHz\n",
2303 dai->id, freq);
2304 break;
2305
2306 case WM8994_SYSCLK_MCLK2:
2307 /* TODO: Set GPIO AF */
2308 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK2;
2309 wm8994->mclk[1] = freq;
2310 dev_dbg(dai->dev, "AIF%d using MCLK2 at %uHz\n",
2311 dai->id, freq);
2312 break;
2313
2314 case WM8994_SYSCLK_FLL1:
2315 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL1;
2316 dev_dbg(dai->dev, "AIF%d using FLL1\n", dai->id);
2317 break;
2318
2319 case WM8994_SYSCLK_FLL2:
2320 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL2;
2321 dev_dbg(dai->dev, "AIF%d using FLL2\n", dai->id);
2322 break;
2323
Mark Brown66b47fd2010-07-08 11:25:43 +09002324 case WM8994_SYSCLK_OPCLK:
2325 /* Special case - a division (times 10) is given and
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02002326 * no effect on main clocking.
Mark Brown66b47fd2010-07-08 11:25:43 +09002327 */
2328 if (freq) {
2329 for (i = 0; i < ARRAY_SIZE(opclk_divs); i++)
2330 if (opclk_divs[i] == freq)
2331 break;
2332 if (i == ARRAY_SIZE(opclk_divs))
2333 return -EINVAL;
2334 snd_soc_update_bits(codec, WM8994_CLOCKING_2,
2335 WM8994_OPCLK_DIV_MASK, i);
2336 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
2337 WM8994_OPCLK_ENA, WM8994_OPCLK_ENA);
2338 } else {
2339 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
2340 WM8994_OPCLK_ENA, 0);
2341 }
2342
Mark Brown9e6e96a2010-01-29 17:47:12 +00002343 default:
2344 return -EINVAL;
2345 }
2346
2347 configure_clock(codec);
2348
Mark Brown67300492012-10-24 10:56:30 +01002349 /*
2350 * If SYSCLK will be less than 50kHz adjust AIFnCLK dividers
2351 * for detection.
2352 */
2353 if (max(wm8994->aifclk[0], wm8994->aifclk[1]) < 50000) {
2354 dev_dbg(codec->dev, "Configuring AIFs for 128fs\n");
2355 snd_soc_update_bits(codec, WM8994_AIF1_RATE,
2356 WM8994_AIF1CLK_RATE_MASK, 0x1);
2357 snd_soc_update_bits(codec, WM8994_AIF2_RATE,
2358 WM8994_AIF2CLK_RATE_MASK, 0x1);
2359 }
2360
Mark Brown9e6e96a2010-01-29 17:47:12 +00002361 return 0;
2362}
2363
2364static int wm8994_set_bias_level(struct snd_soc_codec *codec,
2365 enum snd_soc_bias_level level)
2366{
Mark Brownb6b05692010-08-13 12:58:20 +01002367 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01002368 struct wm8994 *control = wm8994->wm8994;
Mark Brownb6b05692010-08-13 12:58:20 +01002369
Mark Brown5f2f3892012-02-08 18:51:42 +00002370 wm_hubs_set_bias_level(codec, level);
2371
Mark Brown9e6e96a2010-01-29 17:47:12 +00002372 switch (level) {
2373 case SND_SOC_BIAS_ON:
2374 break;
2375
2376 case SND_SOC_BIAS_PREPARE:
Mark Brown500fa302011-11-29 19:58:19 +00002377 /* MICBIAS into regulating mode */
2378 switch (control->type) {
2379 case WM8958:
2380 case WM1811:
2381 snd_soc_update_bits(codec, WM8958_MICBIAS1,
2382 WM8958_MICB1_MODE, 0);
2383 snd_soc_update_bits(codec, WM8958_MICBIAS2,
2384 WM8958_MICB2_MODE, 0);
2385 break;
2386 default:
2387 break;
2388 }
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002389
2390 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
2391 active_reference(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002392 break;
2393
2394 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002395 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
Mark Brown8bc3c2c2010-11-30 14:56:18 +00002396 switch (control->type) {
Mark Brown8bc3c2c2010-11-30 14:56:18 +00002397 case WM8958:
2398 if (wm8994->revision == 0) {
2399 /* Optimise performance for rev A */
Mark Brown8bc3c2c2010-11-30 14:56:18 +00002400 snd_soc_update_bits(codec,
2401 WM8958_CHARGE_PUMP_2,
2402 WM8958_CP_DISCH,
2403 WM8958_CP_DISCH);
2404 }
2405 break;
Mark Brown81204c82011-05-24 17:35:53 +08002406
Mark Brown462835e2012-01-21 12:11:53 +00002407 default:
Mark Brown81204c82011-05-24 17:35:53 +08002408 break;
Mark Brownb6b05692010-08-13 12:58:20 +01002409 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002410
2411 /* Discharge LINEOUT1 & 2 */
2412 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
2413 WM8994_LINEOUT1_DISCH |
2414 WM8994_LINEOUT2_DISCH,
2415 WM8994_LINEOUT1_DISCH |
2416 WM8994_LINEOUT2_DISCH);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002417 }
2418
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002419 if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE)
2420 active_dereference(codec);
2421
Mark Brown500fa302011-11-29 19:58:19 +00002422 /* MICBIAS into bypass mode on newer devices */
2423 switch (control->type) {
2424 case WM8958:
2425 case WM1811:
2426 snd_soc_update_bits(codec, WM8958_MICBIAS1,
2427 WM8958_MICB1_MODE,
2428 WM8958_MICB1_MODE);
2429 snd_soc_update_bits(codec, WM8958_MICBIAS2,
2430 WM8958_MICB2_MODE,
2431 WM8958_MICB2_MODE);
2432 break;
2433 default:
2434 break;
2435 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002436 break;
2437
2438 case SND_SOC_BIAS_OFF:
Mark Brown4105ab82011-12-05 15:17:36 +00002439 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
Mark Brownfbbf5922011-03-11 18:09:04 +00002440 wm8994->cur_fw = NULL;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002441 break;
2442 }
Mark Brown5f2f3892012-02-08 18:51:42 +00002443
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002444 codec->dapm.bias_level = level;
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002445
Mark Brown9e6e96a2010-01-29 17:47:12 +00002446 return 0;
2447}
2448
Mark Brown22f8d052012-03-19 17:32:06 +00002449int wm8994_vmid_mode(struct snd_soc_codec *codec, enum wm8994_vmid_mode mode)
2450{
2451 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2452
2453 switch (mode) {
2454 case WM8994_VMID_NORMAL:
2455 if (wm8994->hubs.lineout1_se) {
2456 snd_soc_dapm_disable_pin(&codec->dapm,
2457 "LINEOUT1N Driver");
2458 snd_soc_dapm_disable_pin(&codec->dapm,
2459 "LINEOUT1P Driver");
2460 }
2461 if (wm8994->hubs.lineout2_se) {
2462 snd_soc_dapm_disable_pin(&codec->dapm,
2463 "LINEOUT2N Driver");
2464 snd_soc_dapm_disable_pin(&codec->dapm,
2465 "LINEOUT2P Driver");
2466 }
2467
2468 /* Do the sync with the old mode to allow it to clean up */
2469 snd_soc_dapm_sync(&codec->dapm);
2470 wm8994->vmid_mode = mode;
2471 break;
2472
2473 case WM8994_VMID_FORCE:
2474 if (wm8994->hubs.lineout1_se) {
2475 snd_soc_dapm_force_enable_pin(&codec->dapm,
2476 "LINEOUT1N Driver");
2477 snd_soc_dapm_force_enable_pin(&codec->dapm,
2478 "LINEOUT1P Driver");
2479 }
2480 if (wm8994->hubs.lineout2_se) {
2481 snd_soc_dapm_force_enable_pin(&codec->dapm,
2482 "LINEOUT2N Driver");
2483 snd_soc_dapm_force_enable_pin(&codec->dapm,
2484 "LINEOUT2P Driver");
2485 }
2486
2487 wm8994->vmid_mode = mode;
2488 snd_soc_dapm_sync(&codec->dapm);
2489 break;
2490
2491 default:
2492 return -EINVAL;
2493 }
2494
2495 return 0;
2496}
2497
Mark Brown9e6e96a2010-01-29 17:47:12 +00002498static int wm8994_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2499{
2500 struct snd_soc_codec *codec = dai->codec;
Mark Brown2a8a8562011-07-24 12:20:41 +01002501 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2502 struct wm8994 *control = wm8994->wm8994;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002503 int ms_reg;
2504 int aif1_reg;
2505 int ms = 0;
2506 int aif1 = 0;
2507
2508 switch (dai->id) {
2509 case 1:
2510 ms_reg = WM8994_AIF1_MASTER_SLAVE;
2511 aif1_reg = WM8994_AIF1_CONTROL_1;
2512 break;
2513 case 2:
2514 ms_reg = WM8994_AIF2_MASTER_SLAVE;
2515 aif1_reg = WM8994_AIF2_CONTROL_1;
2516 break;
2517 default:
2518 return -EINVAL;
2519 }
2520
2521 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
2522 case SND_SOC_DAIFMT_CBS_CFS:
2523 break;
2524 case SND_SOC_DAIFMT_CBM_CFM:
2525 ms = WM8994_AIF1_MSTR;
2526 break;
2527 default:
2528 return -EINVAL;
2529 }
2530
2531 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2532 case SND_SOC_DAIFMT_DSP_B:
2533 aif1 |= WM8994_AIF1_LRCLK_INV;
2534 case SND_SOC_DAIFMT_DSP_A:
2535 aif1 |= 0x18;
2536 break;
2537 case SND_SOC_DAIFMT_I2S:
2538 aif1 |= 0x10;
2539 break;
2540 case SND_SOC_DAIFMT_RIGHT_J:
2541 break;
2542 case SND_SOC_DAIFMT_LEFT_J:
2543 aif1 |= 0x8;
2544 break;
2545 default:
2546 return -EINVAL;
2547 }
2548
2549 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2550 case SND_SOC_DAIFMT_DSP_A:
2551 case SND_SOC_DAIFMT_DSP_B:
2552 /* frame inversion not valid for DSP modes */
2553 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2554 case SND_SOC_DAIFMT_NB_NF:
2555 break;
2556 case SND_SOC_DAIFMT_IB_NF:
2557 aif1 |= WM8994_AIF1_BCLK_INV;
2558 break;
2559 default:
2560 return -EINVAL;
2561 }
2562 break;
2563
2564 case SND_SOC_DAIFMT_I2S:
2565 case SND_SOC_DAIFMT_RIGHT_J:
2566 case SND_SOC_DAIFMT_LEFT_J:
2567 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2568 case SND_SOC_DAIFMT_NB_NF:
2569 break;
2570 case SND_SOC_DAIFMT_IB_IF:
2571 aif1 |= WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV;
2572 break;
2573 case SND_SOC_DAIFMT_IB_NF:
2574 aif1 |= WM8994_AIF1_BCLK_INV;
2575 break;
2576 case SND_SOC_DAIFMT_NB_IF:
2577 aif1 |= WM8994_AIF1_LRCLK_INV;
2578 break;
2579 default:
2580 return -EINVAL;
2581 }
2582 break;
2583 default:
2584 return -EINVAL;
2585 }
2586
Mark Brownc4431df2010-11-26 15:21:07 +00002587 /* The AIF2 format configuration needs to be mirrored to AIF3
2588 * on WM8958 if it's in use so just do it all the time. */
Mark Brown81204c82011-05-24 17:35:53 +08002589 switch (control->type) {
2590 case WM1811:
2591 case WM8958:
2592 if (dai->id == 2)
2593 snd_soc_update_bits(codec, WM8958_AIF3_CONTROL_1,
2594 WM8994_AIF1_LRCLK_INV |
2595 WM8958_AIF3_FMT_MASK, aif1);
2596 break;
2597
2598 default:
2599 break;
2600 }
Mark Brownc4431df2010-11-26 15:21:07 +00002601
Mark Brown9e6e96a2010-01-29 17:47:12 +00002602 snd_soc_update_bits(codec, aif1_reg,
2603 WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV |
2604 WM8994_AIF1_FMT_MASK,
2605 aif1);
2606 snd_soc_update_bits(codec, ms_reg, WM8994_AIF1_MSTR,
2607 ms);
2608
2609 return 0;
2610}
2611
2612static struct {
2613 int val, rate;
2614} srs[] = {
2615 { 0, 8000 },
2616 { 1, 11025 },
2617 { 2, 12000 },
2618 { 3, 16000 },
2619 { 4, 22050 },
2620 { 5, 24000 },
2621 { 6, 32000 },
2622 { 7, 44100 },
2623 { 8, 48000 },
2624 { 9, 88200 },
2625 { 10, 96000 },
2626};
2627
2628static int fs_ratios[] = {
2629 64, 128, 192, 256, 348, 512, 768, 1024, 1408, 1536
2630};
2631
2632static int bclk_divs[] = {
2633 10, 15, 20, 30, 40, 50, 60, 80, 110, 120, 160, 220, 240, 320, 440, 480,
2634 640, 880, 960, 1280, 1760, 1920
2635};
2636
2637static int wm8994_hw_params(struct snd_pcm_substream *substream,
2638 struct snd_pcm_hw_params *params,
2639 struct snd_soc_dai *dai)
2640{
2641 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09002642 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002643 int aif1_reg;
Mark Brownb1e43d92010-12-07 17:14:56 +00002644 int aif2_reg;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002645 int bclk_reg;
2646 int lrclk_reg;
2647 int rate_reg;
2648 int aif1 = 0;
Mark Brownb1e43d92010-12-07 17:14:56 +00002649 int aif2 = 0;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002650 int bclk = 0;
2651 int lrclk = 0;
2652 int rate_val = 0;
2653 int id = dai->id - 1;
2654
2655 int i, cur_val, best_val, bclk_rate, best;
2656
2657 switch (dai->id) {
2658 case 1:
2659 aif1_reg = WM8994_AIF1_CONTROL_1;
Mark Brownb1e43d92010-12-07 17:14:56 +00002660 aif2_reg = WM8994_AIF1_CONTROL_2;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002661 bclk_reg = WM8994_AIF1_BCLK;
2662 rate_reg = WM8994_AIF1_RATE;
2663 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
Mark Brown7d83d212010-08-23 10:54:43 +01002664 wm8994->lrclk_shared[0]) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002665 lrclk_reg = WM8994_AIF1DAC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002666 } else {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002667 lrclk_reg = WM8994_AIF1ADC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002668 dev_dbg(codec->dev, "AIF1 using split LRCLK\n");
2669 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002670 break;
2671 case 2:
2672 aif1_reg = WM8994_AIF2_CONTROL_1;
Mark Brownb1e43d92010-12-07 17:14:56 +00002673 aif2_reg = WM8994_AIF2_CONTROL_2;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002674 bclk_reg = WM8994_AIF2_BCLK;
2675 rate_reg = WM8994_AIF2_RATE;
2676 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
Mark Brown7d83d212010-08-23 10:54:43 +01002677 wm8994->lrclk_shared[1]) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002678 lrclk_reg = WM8994_AIF2DAC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002679 } else {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002680 lrclk_reg = WM8994_AIF2ADC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002681 dev_dbg(codec->dev, "AIF2 using split LRCLK\n");
2682 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002683 break;
2684 default:
2685 return -EINVAL;
2686 }
2687
Mark Brownb8edf3e2012-06-22 17:21:17 +01002688 bclk_rate = params_rate(params) * 4;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002689 switch (params_format(params)) {
2690 case SNDRV_PCM_FORMAT_S16_LE:
2691 bclk_rate *= 16;
2692 break;
2693 case SNDRV_PCM_FORMAT_S20_3LE:
2694 bclk_rate *= 20;
2695 aif1 |= 0x20;
2696 break;
2697 case SNDRV_PCM_FORMAT_S24_LE:
2698 bclk_rate *= 24;
2699 aif1 |= 0x40;
2700 break;
2701 case SNDRV_PCM_FORMAT_S32_LE:
2702 bclk_rate *= 32;
2703 aif1 |= 0x60;
2704 break;
2705 default:
2706 return -EINVAL;
2707 }
2708
2709 /* Try to find an appropriate sample rate; look for an exact match. */
2710 for (i = 0; i < ARRAY_SIZE(srs); i++)
2711 if (srs[i].rate == params_rate(params))
2712 break;
2713 if (i == ARRAY_SIZE(srs))
2714 return -EINVAL;
2715 rate_val |= srs[i].val << WM8994_AIF1_SR_SHIFT;
2716
2717 dev_dbg(dai->dev, "Sample rate is %dHz\n", srs[i].rate);
2718 dev_dbg(dai->dev, "AIF%dCLK is %dHz, target BCLK %dHz\n",
2719 dai->id, wm8994->aifclk[id], bclk_rate);
2720
Mark Brownb1e43d92010-12-07 17:14:56 +00002721 if (params_channels(params) == 1 &&
2722 (snd_soc_read(codec, aif1_reg) & 0x18) == 0x18)
2723 aif2 |= WM8994_AIF1_MONO;
2724
Mark Brown9e6e96a2010-01-29 17:47:12 +00002725 if (wm8994->aifclk[id] == 0) {
2726 dev_err(dai->dev, "AIF%dCLK not configured\n", dai->id);
2727 return -EINVAL;
2728 }
2729
2730 /* AIFCLK/fs ratio; look for a close match in either direction */
2731 best = 0;
2732 best_val = abs((fs_ratios[0] * params_rate(params))
2733 - wm8994->aifclk[id]);
2734 for (i = 1; i < ARRAY_SIZE(fs_ratios); i++) {
2735 cur_val = abs((fs_ratios[i] * params_rate(params))
2736 - wm8994->aifclk[id]);
2737 if (cur_val >= best_val)
2738 continue;
2739 best = i;
2740 best_val = cur_val;
2741 }
2742 dev_dbg(dai->dev, "Selected AIF%dCLK/fs = %d\n",
2743 dai->id, fs_ratios[best]);
2744 rate_val |= best;
2745
2746 /* We may not get quite the right frequency if using
2747 * approximate clocks so look for the closest match that is
2748 * higher than the target (we need to ensure that there enough
2749 * BCLKs to clock out the samples).
2750 */
2751 best = 0;
2752 for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
Joonyoung Shim07cd8ad2010-02-02 18:53:19 +09002753 cur_val = (wm8994->aifclk[id] * 10 / bclk_divs[i]) - bclk_rate;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002754 if (cur_val < 0) /* BCLK table is sorted */
2755 break;
2756 best = i;
2757 }
Joonyoung Shim07cd8ad2010-02-02 18:53:19 +09002758 bclk_rate = wm8994->aifclk[id] * 10 / bclk_divs[best];
Mark Brown9e6e96a2010-01-29 17:47:12 +00002759 dev_dbg(dai->dev, "Using BCLK_DIV %d for actual BCLK %dHz\n",
2760 bclk_divs[best], bclk_rate);
2761 bclk |= best << WM8994_AIF1_BCLK_DIV_SHIFT;
2762
2763 lrclk = bclk_rate / params_rate(params);
Mark Brownfc07ecd2011-11-28 21:16:56 +00002764 if (!lrclk) {
2765 dev_err(dai->dev, "Unable to generate LRCLK from %dHz BCLK\n",
2766 bclk_rate);
2767 return -EINVAL;
2768 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002769 dev_dbg(dai->dev, "Using LRCLK rate %d for actual LRCLK %dHz\n",
2770 lrclk, bclk_rate / lrclk);
2771
2772 snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
Mark Brownb1e43d92010-12-07 17:14:56 +00002773 snd_soc_update_bits(codec, aif2_reg, WM8994_AIF1_MONO, aif2);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002774 snd_soc_update_bits(codec, bclk_reg, WM8994_AIF1_BCLK_DIV_MASK, bclk);
2775 snd_soc_update_bits(codec, lrclk_reg, WM8994_AIF1DAC_RATE_MASK,
2776 lrclk);
2777 snd_soc_update_bits(codec, rate_reg, WM8994_AIF1_SR_MASK |
2778 WM8994_AIF1CLK_RATE_MASK, rate_val);
2779
2780 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
2781 switch (dai->id) {
2782 case 1:
2783 wm8994->dac_rates[0] = params_rate(params);
2784 wm8994_set_retune_mobile(codec, 0);
2785 wm8994_set_retune_mobile(codec, 1);
2786 break;
2787 case 2:
2788 wm8994->dac_rates[1] = params_rate(params);
2789 wm8994_set_retune_mobile(codec, 2);
2790 break;
2791 }
2792 }
2793
2794 return 0;
2795}
2796
Mark Brownc4431df2010-11-26 15:21:07 +00002797static int wm8994_aif3_hw_params(struct snd_pcm_substream *substream,
2798 struct snd_pcm_hw_params *params,
2799 struct snd_soc_dai *dai)
2800{
2801 struct snd_soc_codec *codec = dai->codec;
Mark Brown2a8a8562011-07-24 12:20:41 +01002802 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2803 struct wm8994 *control = wm8994->wm8994;
Mark Brownc4431df2010-11-26 15:21:07 +00002804 int aif1_reg;
2805 int aif1 = 0;
2806
2807 switch (dai->id) {
2808 case 3:
2809 switch (control->type) {
Mark Brown81204c82011-05-24 17:35:53 +08002810 case WM1811:
Mark Brownc4431df2010-11-26 15:21:07 +00002811 case WM8958:
2812 aif1_reg = WM8958_AIF3_CONTROL_1;
2813 break;
2814 default:
2815 return 0;
2816 }
2817 default:
2818 return 0;
2819 }
2820
2821 switch (params_format(params)) {
2822 case SNDRV_PCM_FORMAT_S16_LE:
2823 break;
2824 case SNDRV_PCM_FORMAT_S20_3LE:
2825 aif1 |= 0x20;
2826 break;
2827 case SNDRV_PCM_FORMAT_S24_LE:
2828 aif1 |= 0x40;
2829 break;
2830 case SNDRV_PCM_FORMAT_S32_LE:
2831 aif1 |= 0x60;
2832 break;
2833 default:
2834 return -EINVAL;
2835 }
2836
2837 return snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
2838}
2839
Mark Brown9e6e96a2010-01-29 17:47:12 +00002840static int wm8994_aif_mute(struct snd_soc_dai *codec_dai, int mute)
2841{
2842 struct snd_soc_codec *codec = codec_dai->codec;
2843 int mute_reg;
2844 int reg;
2845
2846 switch (codec_dai->id) {
2847 case 1:
2848 mute_reg = WM8994_AIF1_DAC1_FILTERS_1;
2849 break;
2850 case 2:
2851 mute_reg = WM8994_AIF2_DAC_FILTERS_1;
2852 break;
2853 default:
2854 return -EINVAL;
2855 }
2856
2857 if (mute)
2858 reg = WM8994_AIF1DAC1_MUTE;
2859 else
2860 reg = 0;
2861
2862 snd_soc_update_bits(codec, mute_reg, WM8994_AIF1DAC1_MUTE, reg);
2863
2864 return 0;
2865}
2866
Mark Brown778a76e2010-03-22 22:05:10 +00002867static int wm8994_set_tristate(struct snd_soc_dai *codec_dai, int tristate)
2868{
2869 struct snd_soc_codec *codec = codec_dai->codec;
2870 int reg, val, mask;
2871
2872 switch (codec_dai->id) {
2873 case 1:
2874 reg = WM8994_AIF1_MASTER_SLAVE;
2875 mask = WM8994_AIF1_TRI;
2876 break;
2877 case 2:
2878 reg = WM8994_AIF2_MASTER_SLAVE;
2879 mask = WM8994_AIF2_TRI;
2880 break;
Mark Brown778a76e2010-03-22 22:05:10 +00002881 default:
2882 return -EINVAL;
2883 }
2884
2885 if (tristate)
2886 val = mask;
2887 else
2888 val = 0;
2889
Qiao Zhou78b3fb42011-01-19 19:10:47 +08002890 return snd_soc_update_bits(codec, reg, mask, val);
Mark Brown778a76e2010-03-22 22:05:10 +00002891}
2892
Mark Brownd09f3ec2011-08-15 11:01:02 +09002893static int wm8994_aif2_probe(struct snd_soc_dai *dai)
2894{
2895 struct snd_soc_codec *codec = dai->codec;
2896
2897 /* Disable the pulls on the AIF if we're using it to save power. */
2898 snd_soc_update_bits(codec, WM8994_GPIO_3,
2899 WM8994_GPN_PU | WM8994_GPN_PD, 0);
2900 snd_soc_update_bits(codec, WM8994_GPIO_4,
2901 WM8994_GPN_PU | WM8994_GPN_PD, 0);
2902 snd_soc_update_bits(codec, WM8994_GPIO_5,
2903 WM8994_GPN_PU | WM8994_GPN_PD, 0);
2904
2905 return 0;
2906}
2907
Mark Brown9e6e96a2010-01-29 17:47:12 +00002908#define WM8994_RATES SNDRV_PCM_RATE_8000_96000
2909
2910#define WM8994_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
Ian Lartey3079aed2010-08-31 23:56:34 +01002911 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002912
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01002913static const struct snd_soc_dai_ops wm8994_aif1_dai_ops = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002914 .set_sysclk = wm8994_set_dai_sysclk,
2915 .set_fmt = wm8994_set_dai_fmt,
2916 .hw_params = wm8994_hw_params,
2917 .digital_mute = wm8994_aif_mute,
2918 .set_pll = wm8994_set_fll,
Mark Brown778a76e2010-03-22 22:05:10 +00002919 .set_tristate = wm8994_set_tristate,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002920};
2921
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01002922static const struct snd_soc_dai_ops wm8994_aif2_dai_ops = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002923 .set_sysclk = wm8994_set_dai_sysclk,
2924 .set_fmt = wm8994_set_dai_fmt,
2925 .hw_params = wm8994_hw_params,
2926 .digital_mute = wm8994_aif_mute,
2927 .set_pll = wm8994_set_fll,
Mark Brown778a76e2010-03-22 22:05:10 +00002928 .set_tristate = wm8994_set_tristate,
2929};
2930
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01002931static const struct snd_soc_dai_ops wm8994_aif3_dai_ops = {
Mark Brownc4431df2010-11-26 15:21:07 +00002932 .hw_params = wm8994_aif3_hw_params,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002933};
2934
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002935static struct snd_soc_dai_driver wm8994_dai[] = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002936 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002937 .name = "wm8994-aif1",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002938 .id = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002939 .playback = {
2940 .stream_name = "AIF1 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002941 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002942 .channels_max = 2,
2943 .rates = WM8994_RATES,
2944 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002945 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002946 },
2947 .capture = {
2948 .stream_name = "AIF1 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002949 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002950 .channels_max = 2,
2951 .rates = WM8994_RATES,
2952 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002953 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002954 },
2955 .ops = &wm8994_aif1_dai_ops,
2956 },
2957 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002958 .name = "wm8994-aif2",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002959 .id = 2,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002960 .playback = {
2961 .stream_name = "AIF2 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002962 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002963 .channels_max = 2,
2964 .rates = WM8994_RATES,
2965 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002966 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002967 },
2968 .capture = {
2969 .stream_name = "AIF2 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002970 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002971 .channels_max = 2,
2972 .rates = WM8994_RATES,
2973 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002974 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002975 },
Mark Brownd09f3ec2011-08-15 11:01:02 +09002976 .probe = wm8994_aif2_probe,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002977 .ops = &wm8994_aif2_dai_ops,
2978 },
2979 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002980 .name = "wm8994-aif3",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002981 .id = 3,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002982 .playback = {
2983 .stream_name = "AIF3 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002984 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002985 .channels_max = 2,
2986 .rates = WM8994_RATES,
2987 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002988 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002989 },
Dan Carpentera8462bd2010-03-24 14:58:34 +03002990 .capture = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002991 .stream_name = "AIF3 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002992 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002993 .channels_max = 2,
2994 .rates = WM8994_RATES,
2995 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002996 .sig_bits = 24,
2997 },
Mark Brown778a76e2010-03-22 22:05:10 +00002998 .ops = &wm8994_aif3_dai_ops,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002999 }
3000};
Mark Brown9e6e96a2010-01-29 17:47:12 +00003001
3002#ifdef CONFIG_PM
Mark Brown4752a882012-03-04 02:16:01 +00003003static int wm8994_codec_suspend(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003004{
Mark Brownb2c812e2010-04-14 15:35:19 +09003005 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003006 int i, ret;
3007
3008 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
3009 memcpy(&wm8994->fll_suspend[i], &wm8994->fll[i],
Mark Brownf701a2e2011-03-09 19:31:01 +00003010 sizeof(struct wm8994_fll_config));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003011 ret = _wm8994_set_fll(codec, i + 1, 0, 0, 0);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003012 if (ret < 0)
3013 dev_warn(codec->dev, "Failed to stop FLL%d: %d\n",
3014 i + 1, ret);
3015 }
3016
3017 wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);
3018
3019 return 0;
3020}
3021
Mark Brown4752a882012-03-04 02:16:01 +00003022static int wm8994_codec_resume(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003023{
Mark Brownb2c812e2010-04-14 15:35:19 +09003024 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01003025 struct wm8994 *control = wm8994->wm8994;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003026 int i, ret;
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00003027 unsigned int val, mask;
3028
3029 if (wm8994->revision < 4) {
3030 /* force a HW read */
Mark Brownd9a76662011-07-24 12:49:52 +01003031 ret = regmap_read(control->regmap,
3032 WM8994_POWER_MANAGEMENT_5, &val);
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00003033
3034 /* modify the cache only */
3035 codec->cache_only = 1;
3036 mask = WM8994_DAC1R_ENA | WM8994_DAC1L_ENA |
3037 WM8994_DAC2R_ENA | WM8994_DAC2L_ENA;
3038 val &= mask;
3039 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
3040 mask, val);
3041 codec->cache_only = 0;
3042 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00003043
Mark Brown9e6e96a2010-01-29 17:47:12 +00003044 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
Mark Brown6a2f1ee2010-05-10 18:36:37 +01003045 if (!wm8994->fll_suspend[i].out)
3046 continue;
3047
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003048 ret = _wm8994_set_fll(codec, i + 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003049 wm8994->fll_suspend[i].src,
3050 wm8994->fll_suspend[i].in,
3051 wm8994->fll_suspend[i].out);
3052 if (ret < 0)
3053 dev_warn(codec->dev, "Failed to restore FLL%d: %d\n",
3054 i + 1, ret);
3055 }
3056
3057 return 0;
3058}
3059#else
Mark Brown4752a882012-03-04 02:16:01 +00003060#define wm8994_codec_suspend NULL
3061#define wm8994_codec_resume NULL
Mark Brown9e6e96a2010-01-29 17:47:12 +00003062#endif
3063
3064static void wm8994_handle_retune_mobile_pdata(struct wm8994_priv *wm8994)
3065{
Mark Brown8cb8e832012-07-25 18:10:03 +01003066 struct snd_soc_codec *codec = wm8994->hubs.codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003067 struct wm8994_pdata *pdata = wm8994->pdata;
3068 struct snd_kcontrol_new controls[] = {
3069 SOC_ENUM_EXT("AIF1.1 EQ Mode",
3070 wm8994->retune_mobile_enum,
3071 wm8994_get_retune_mobile_enum,
3072 wm8994_put_retune_mobile_enum),
3073 SOC_ENUM_EXT("AIF1.2 EQ Mode",
3074 wm8994->retune_mobile_enum,
3075 wm8994_get_retune_mobile_enum,
3076 wm8994_put_retune_mobile_enum),
3077 SOC_ENUM_EXT("AIF2 EQ Mode",
3078 wm8994->retune_mobile_enum,
3079 wm8994_get_retune_mobile_enum,
3080 wm8994_put_retune_mobile_enum),
3081 };
3082 int ret, i, j;
3083 const char **t;
3084
3085 /* We need an array of texts for the enum API but the number
3086 * of texts is likely to be less than the number of
3087 * configurations due to the sample rate dependency of the
3088 * configurations. */
3089 wm8994->num_retune_mobile_texts = 0;
3090 wm8994->retune_mobile_texts = NULL;
3091 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
3092 for (j = 0; j < wm8994->num_retune_mobile_texts; j++) {
3093 if (strcmp(pdata->retune_mobile_cfgs[i].name,
3094 wm8994->retune_mobile_texts[j]) == 0)
3095 break;
3096 }
3097
3098 if (j != wm8994->num_retune_mobile_texts)
3099 continue;
3100
3101 /* Expand the array... */
3102 t = krealloc(wm8994->retune_mobile_texts,
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02003103 sizeof(char *) *
Mark Brown9e6e96a2010-01-29 17:47:12 +00003104 (wm8994->num_retune_mobile_texts + 1),
3105 GFP_KERNEL);
3106 if (t == NULL)
3107 continue;
3108
3109 /* ...store the new entry... */
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02003110 t[wm8994->num_retune_mobile_texts] =
Mark Brown9e6e96a2010-01-29 17:47:12 +00003111 pdata->retune_mobile_cfgs[i].name;
3112
3113 /* ...and remember the new version. */
3114 wm8994->num_retune_mobile_texts++;
3115 wm8994->retune_mobile_texts = t;
3116 }
3117
3118 dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
3119 wm8994->num_retune_mobile_texts);
3120
3121 wm8994->retune_mobile_enum.max = wm8994->num_retune_mobile_texts;
3122 wm8994->retune_mobile_enum.texts = wm8994->retune_mobile_texts;
3123
Mark Brown8cb8e832012-07-25 18:10:03 +01003124 ret = snd_soc_add_codec_controls(wm8994->hubs.codec, controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003125 ARRAY_SIZE(controls));
3126 if (ret != 0)
Mark Brown8cb8e832012-07-25 18:10:03 +01003127 dev_err(wm8994->hubs.codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003128 "Failed to add ReTune Mobile controls: %d\n", ret);
3129}
3130
3131static void wm8994_handle_pdata(struct wm8994_priv *wm8994)
3132{
Mark Brown8cb8e832012-07-25 18:10:03 +01003133 struct snd_soc_codec *codec = wm8994->hubs.codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003134 struct wm8994_pdata *pdata = wm8994->pdata;
3135 int ret, i;
3136
3137 if (!pdata)
3138 return;
3139
3140 wm_hubs_handle_analogue_pdata(codec, pdata->lineout1_diff,
3141 pdata->lineout2_diff,
3142 pdata->lineout1fb,
3143 pdata->lineout2fb,
3144 pdata->jd_scthr,
3145 pdata->jd_thr,
Mark Brown02e79472012-08-21 17:54:52 +01003146 pdata->micb1_delay,
3147 pdata->micb2_delay,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003148 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 Brownc0cc3f12012-09-28 16:50:15 +01003471/* Deferred mic detection to allow for extra settling time */
3472static void wm1811_mic_work(struct work_struct *work)
3473{
3474 struct wm8994_priv *wm8994 = container_of(work, struct wm8994_priv,
3475 mic_work.work);
3476 struct snd_soc_codec *codec = wm8994->hubs.codec;
3477
3478 pm_runtime_get_sync(codec->dev);
3479
3480 /* If required for an external cap force MICBIAS on */
3481 if (wm8994->pdata->jd_ext_cap) {
3482 snd_soc_dapm_force_enable_pin(&codec->dapm,
3483 "MICBIAS2");
3484 snd_soc_dapm_sync(&codec->dapm);
3485 }
3486
3487 mutex_lock(&wm8994->accdet_lock);
3488
3489 dev_dbg(codec->dev, "Starting mic detection\n");
3490
3491 /*
3492 * Start off measument of microphone impedence to find out
3493 * what's actually there.
3494 */
3495 wm8994->mic_detecting = true;
3496 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_MIC);
3497
3498 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3499 WM8958_MICD_ENA, WM8958_MICD_ENA);
3500
3501 mutex_unlock(&wm8994->accdet_lock);
3502
3503 pm_runtime_put(codec->dev);
3504}
3505
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003506static irqreturn_t wm1811_jackdet_irq(int irq, void *data)
3507{
3508 struct wm8994_priv *wm8994 = data;
Mark Brown8cb8e832012-07-25 18:10:03 +01003509 struct snd_soc_codec *codec = wm8994->hubs.codec;
Mark Brownc0cc3f12012-09-28 16:50:15 +01003510 int reg, delay;
Mark Brownc9865642012-03-12 16:31:50 +00003511 bool present;
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003512
Mark Brownb8176622012-07-24 15:48:57 +01003513 pm_runtime_get_sync(codec->dev);
3514
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003515 mutex_lock(&wm8994->accdet_lock);
3516
3517 reg = snd_soc_read(codec, WM1811_JACKDET_CTRL);
3518 if (reg < 0) {
3519 dev_err(codec->dev, "Failed to read jack status: %d\n", reg);
3520 mutex_unlock(&wm8994->accdet_lock);
Mark Brownb8176622012-07-24 15:48:57 +01003521 pm_runtime_put(codec->dev);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003522 return IRQ_NONE;
3523 }
3524
3525 dev_dbg(codec->dev, "JACKDET %x\n", reg);
3526
Mark Brownc9865642012-03-12 16:31:50 +00003527 present = reg & WM1811_JACKDET_LVL;
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003528
Mark Brownc9865642012-03-12 16:31:50 +00003529 if (present) {
3530 dev_dbg(codec->dev, "Jack detected\n");
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003531
Mark Browne9d9a962012-04-26 16:07:32 +01003532 wm8958_micd_set_rate(codec);
3533
Mark Brown55a27782012-02-21 13:45:53 +00003534 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3535 WM8958_MICB2_DISCH, 0);
3536
Mark Brown378ec0c2012-03-01 19:01:43 +00003537 /* Disable debounce while inserted */
3538 snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
3539 WM1811_JACKDET_DB, 0);
3540
Mark Brownc0cc3f12012-09-28 16:50:15 +01003541 delay = wm8994->pdata->micdet_delay;
3542 schedule_delayed_work(&wm8994->mic_work,
3543 msecs_to_jiffies(delay));
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003544 } else {
3545 dev_dbg(codec->dev, "Jack not detected\n");
3546
Mark Brownc0cc3f12012-09-28 16:50:15 +01003547 cancel_delayed_work_sync(&wm8994->mic_work);
3548
Mark Brown55a27782012-02-21 13:45:53 +00003549 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3550 WM8958_MICB2_DISCH, WM8958_MICB2_DISCH);
3551
Mark Brown378ec0c2012-03-01 19:01:43 +00003552 /* Enable debounce while removed */
3553 snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
3554 WM1811_JACKDET_DB, WM1811_JACKDET_DB);
3555
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003556 wm8994->mic_detecting = false;
3557 wm8994->jack_mic = false;
3558 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3559 WM8958_MICD_ENA, 0);
3560 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_JACK);
3561 }
3562
3563 mutex_unlock(&wm8994->accdet_lock);
3564
Mark Brownc0cc3f12012-09-28 16:50:15 +01003565 /* Turn off MICBIAS if it was on for an external cap */
3566 if (wm8994->pdata->jd_ext_cap && !present)
3567 snd_soc_dapm_disable_pin(&codec->dapm, "MICBIAS2");
Mark Brownc9865642012-03-12 16:31:50 +00003568
3569 if (present)
3570 snd_soc_jack_report(wm8994->micdet[0].jack,
3571 SND_JACK_MECHANICAL, SND_JACK_MECHANICAL);
3572 else
3573 snd_soc_jack_report(wm8994->micdet[0].jack, 0,
3574 SND_JACK_MECHANICAL | SND_JACK_HEADSET |
3575 wm8994->btn_mask);
3576
Mark Brown99af79d2012-07-25 23:03:36 +01003577 /* Since we only report deltas force an update, ensures we
3578 * avoid bootstrapping issues with the core. */
3579 snd_soc_jack_report(wm8994->micdet[0].jack, 0, 0);
3580
Mark Brownb8176622012-07-24 15:48:57 +01003581 pm_runtime_put(codec->dev);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003582 return IRQ_HANDLED;
3583}
3584
Mark Brown99af79d2012-07-25 23:03:36 +01003585static void wm1811_jackdet_bootstrap(struct work_struct *work)
3586{
3587 struct wm8994_priv *wm8994 = container_of(work,
3588 struct wm8994_priv,
3589 jackdet_bootstrap.work);
3590 wm1811_jackdet_irq(0, wm8994);
3591}
3592
Mark Brown821edd22010-11-26 15:21:09 +00003593/**
3594 * wm8958_mic_detect - Enable microphone detection via the WM8958 IRQ
3595 *
3596 * @codec: WM8958 codec
3597 * @jack: jack to report detection events on
3598 *
3599 * Enable microphone detection functionality for the WM8958. By
3600 * default simple detection which supports the detection of up to 6
3601 * buttons plus video and microphone functionality is supported.
3602 *
3603 * The WM8958 has an advanced jack detection facility which is able to
3604 * support complex accessory detection, especially when used in
3605 * conjunction with external circuitry. In order to provide maximum
3606 * flexiblity a callback is provided which allows a completely custom
3607 * detection algorithm.
3608 */
3609int wm8958_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
3610 wm8958_micdet_cb cb, void *cb_data)
3611{
3612 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01003613 struct wm8994 *control = wm8994->wm8994;
Mark Brown4585790d2011-11-30 10:55:14 +00003614 u16 micd_lvl_sel;
Mark Brown821edd22010-11-26 15:21:09 +00003615
Mark Brown81204c82011-05-24 17:35:53 +08003616 switch (control->type) {
3617 case WM1811:
3618 case WM8958:
3619 break;
3620 default:
Mark Brown821edd22010-11-26 15:21:09 +00003621 return -EINVAL;
Mark Brown81204c82011-05-24 17:35:53 +08003622 }
Mark Brown821edd22010-11-26 15:21:09 +00003623
3624 if (jack) {
3625 if (!cb) {
3626 dev_dbg(codec->dev, "Using default micdet callback\n");
3627 cb = wm8958_default_micdet;
3628 cb_data = codec;
3629 }
3630
Mark Brown4cdf5e42011-11-29 14:36:17 +00003631 snd_soc_dapm_force_enable_pin(&codec->dapm, "CLK_SYS");
Mark Brown7d464b22012-03-03 18:46:06 +00003632 snd_soc_dapm_sync(&codec->dapm);
Mark Brown4cdf5e42011-11-29 14:36:17 +00003633
Mark Brown821edd22010-11-26 15:21:09 +00003634 wm8994->micdet[0].jack = jack;
3635 wm8994->jack_cb = cb;
3636 wm8994->jack_cb_data = cb_data;
3637
Mark Brown157a75e2011-11-30 13:43:51 +00003638 wm8994->mic_detecting = true;
Mark Brownb00adf72011-08-13 11:57:18 +09003639 wm8994->jack_mic = false;
3640
3641 wm8958_micd_set_rate(codec);
3642
Mark Brown4585790d2011-11-30 10:55:14 +00003643 /* Detect microphones and short circuits by default */
3644 if (wm8994->pdata->micd_lvl_sel)
3645 micd_lvl_sel = wm8994->pdata->micd_lvl_sel;
3646 else
3647 micd_lvl_sel = 0x41;
3648
3649 wm8994->btn_mask = SND_JACK_BTN_0 | SND_JACK_BTN_1 |
3650 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
3651 SND_JACK_BTN_4 | SND_JACK_BTN_5;
3652
Mark Brownb00adf72011-08-13 11:57:18 +09003653 snd_soc_update_bits(codec, WM8958_MIC_DETECT_2,
Mark Brown4585790d2011-11-30 10:55:14 +00003654 WM8958_MICD_LVL_SEL_MASK, micd_lvl_sel);
Mark Brownb00adf72011-08-13 11:57:18 +09003655
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003656 WARN_ON(codec->dapm.bias_level > SND_SOC_BIAS_STANDBY);
3657
3658 /*
3659 * If we can use jack detection start off with that,
3660 * otherwise jump straight to microphone detection.
3661 */
3662 if (wm8994->jackdet) {
Mark Brown99af79d2012-07-25 23:03:36 +01003663 /* Disable debounce for the initial detect */
3664 snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
3665 WM1811_JACKDET_DB, 0);
3666
Mark Brown55a27782012-02-21 13:45:53 +00003667 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3668 WM8958_MICB2_DISCH,
3669 WM8958_MICB2_DISCH);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003670 snd_soc_update_bits(codec, WM8994_LDO_1,
3671 WM8994_LDO1_DISCH, 0);
3672 wm1811_jackdet_set_mode(codec,
3673 WM1811_JACKDET_MODE_JACK);
3674 } else {
3675 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3676 WM8958_MICD_ENA, WM8958_MICD_ENA);
3677 }
3678
Mark Brown821edd22010-11-26 15:21:09 +00003679 } else {
3680 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3681 WM8958_MICD_ENA, 0);
Mark Brownafaf1592012-03-03 18:46:36 +00003682 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_NONE);
Mark Brown4cdf5e42011-11-29 14:36:17 +00003683 snd_soc_dapm_disable_pin(&codec->dapm, "CLK_SYS");
Mark Brown7d464b22012-03-03 18:46:06 +00003684 snd_soc_dapm_sync(&codec->dapm);
Mark Brown821edd22010-11-26 15:21:09 +00003685 }
3686
3687 return 0;
3688}
3689EXPORT_SYMBOL_GPL(wm8958_mic_detect);
3690
3691static irqreturn_t wm8958_mic_irq(int irq, void *data)
3692{
3693 struct wm8994_priv *wm8994 = data;
Mark Brown8cb8e832012-07-25 18:10:03 +01003694 struct snd_soc_codec *codec = wm8994->hubs.codec;
Mark Brown19940b32011-08-19 18:05:05 +09003695 int reg, count;
Mark Brown821edd22010-11-26 15:21:09 +00003696
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003697 /*
3698 * Jack detection may have detected a removal simulataneously
3699 * with an update of the MICDET status; if so it will have
3700 * stopped detection and we can ignore this interrupt.
3701 */
Mark Brownc9865642012-03-12 16:31:50 +00003702 if (!(snd_soc_read(codec, WM8958_MIC_DETECT_1) & WM8958_MICD_ENA))
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003703 return IRQ_HANDLED;
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003704
Mark Brownb8176622012-07-24 15:48:57 +01003705 pm_runtime_get_sync(codec->dev);
3706
Mark Brown19940b32011-08-19 18:05:05 +09003707 /* We may occasionally read a detection without an impedence
3708 * range being provided - if that happens loop again.
3709 */
3710 count = 10;
3711 do {
3712 reg = snd_soc_read(codec, WM8958_MIC_DETECT_3);
3713 if (reg < 0) {
3714 dev_err(codec->dev,
3715 "Failed to read mic detect status: %d\n",
3716 reg);
Mark Brownb8176622012-07-24 15:48:57 +01003717 pm_runtime_put(codec->dev);
Mark Brown19940b32011-08-19 18:05:05 +09003718 return IRQ_NONE;
3719 }
Mark Brown821edd22010-11-26 15:21:09 +00003720
Mark Brown19940b32011-08-19 18:05:05 +09003721 if (!(reg & WM8958_MICD_VALID)) {
3722 dev_dbg(codec->dev, "Mic detect data not valid\n");
3723 goto out;
3724 }
3725
3726 if (!(reg & WM8958_MICD_STS) || (reg & WM8958_MICD_LVL_MASK))
3727 break;
3728
3729 msleep(1);
3730 } while (count--);
3731
3732 if (count == 0)
3733 dev_warn(codec->dev, "No impedence range reported for jack\n");
Mark Brown821edd22010-11-26 15:21:09 +00003734
Mark Brown7116f452010-12-29 13:05:21 +00003735#ifndef CONFIG_SND_SOC_WM8994_MODULE
Mark Brown2bbb5d62010-12-05 12:50:12 +00003736 trace_snd_soc_jack_irq(dev_name(codec->dev));
Mark Brown7116f452010-12-29 13:05:21 +00003737#endif
Mark Brown2bbb5d62010-12-05 12:50:12 +00003738
Mark Brown821edd22010-11-26 15:21:09 +00003739 if (wm8994->jack_cb)
3740 wm8994->jack_cb(reg, wm8994->jack_cb_data);
3741 else
3742 dev_warn(codec->dev, "Accessory detection with no callback\n");
3743
3744out:
Mark Brownb8176622012-07-24 15:48:57 +01003745 pm_runtime_put(codec->dev);
Mark Brown821edd22010-11-26 15:21:09 +00003746 return IRQ_HANDLED;
3747}
3748
Mark Brown3b1af3f2011-07-14 12:38:18 +09003749static irqreturn_t wm8994_fifo_error(int irq, void *data)
3750{
3751 struct snd_soc_codec *codec = data;
3752
3753 dev_err(codec->dev, "FIFO error\n");
3754
3755 return IRQ_HANDLED;
3756}
3757
Mark Brownf0b182b2011-08-16 12:01:27 +09003758static irqreturn_t wm8994_temp_warn(int irq, void *data)
3759{
3760 struct snd_soc_codec *codec = data;
3761
3762 dev_err(codec->dev, "Thermal warning\n");
3763
3764 return IRQ_HANDLED;
3765}
3766
3767static irqreturn_t wm8994_temp_shut(int irq, void *data)
3768{
3769 struct snd_soc_codec *codec = data;
3770
3771 dev_crit(codec->dev, "Thermal shutdown\n");
3772
3773 return IRQ_HANDLED;
3774}
3775
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003776static int wm8994_codec_probe(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003777{
Mark Brownd9a76662011-07-24 12:49:52 +01003778 struct wm8994 *control = dev_get_drvdata(codec->dev->parent);
Mark Brown2bc16ed2012-03-03 23:24:39 +00003779 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003780 struct snd_soc_dapm_context *dapm = &codec->dapm;
Mark Brownd9a76662011-07-24 12:49:52 +01003781 unsigned int reg;
Mark Brownec62dbd2010-08-15 14:56:40 +01003782 int ret, i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003783
Mark Brown8cb8e832012-07-25 18:10:03 +01003784 wm8994->hubs.codec = codec;
Mark Brownd9a76662011-07-24 12:49:52 +01003785 codec->control_data = control->regmap;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003786
Mark Brownd9a76662011-07-24 12:49:52 +01003787 snd_soc_codec_set_cache_io(codec, 16, 16, SND_SOC_REGMAP);
Mark Brown2a8a8562011-07-24 12:20:41 +01003788
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003789 mutex_init(&wm8994->accdet_lock);
Mark Brown99af79d2012-07-25 23:03:36 +01003790 INIT_DELAYED_WORK(&wm8994->jackdet_bootstrap,
3791 wm1811_jackdet_bootstrap);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003792
Mark Brownc0cc3f12012-09-28 16:50:15 +01003793 switch (control->type) {
3794 case WM8994:
3795 INIT_DELAYED_WORK(&wm8994->mic_work, wm8994_mic_work);
3796 break;
3797 case WM1811:
3798 INIT_DELAYED_WORK(&wm8994->mic_work, wm1811_mic_work);
3799 break;
3800 default:
3801 break;
3802 }
3803
Mark Brownc7ebf932011-07-12 19:47:59 +09003804 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
3805 init_completion(&wm8994->fll_locked[i]);
3806
Mark Brown9b7c5252011-02-17 20:05:44 -08003807 if (wm8994->pdata && wm8994->pdata->micdet_irq)
3808 wm8994->micdet_irq = wm8994->pdata->micdet_irq;
Mark Brown9b7c5252011-02-17 20:05:44 -08003809
Mark Brown39fb51a2010-11-26 17:23:43 +00003810 pm_runtime_enable(codec->dev);
Mark Brown5fab51742012-02-06 18:37:08 +00003811 pm_runtime_idle(codec->dev);
Mark Brown39fb51a2010-11-26 17:23:43 +00003812
Mark Brownf959dee2012-01-31 16:16:47 +00003813 /* By default use idle_bias_off, will override for WM8994 */
3814 codec->dapm.idle_bias_off = 1;
3815
Mark Brown9e6e96a2010-01-29 17:47:12 +00003816 /* Set revision-specific configuration */
Mark Brownb6b05692010-08-13 12:58:20 +01003817 wm8994->revision = snd_soc_read(codec, WM8994_CHIP_REVISION);
Mark Brown3a423152010-11-26 15:21:06 +00003818 switch (control->type) {
3819 case WM8994:
Mark Brownf959dee2012-01-31 16:16:47 +00003820 /* Single ended line outputs should have VMID on. */
3821 if (!wm8994->pdata->lineout1_diff ||
3822 !wm8994->pdata->lineout2_diff)
3823 codec->dapm.idle_bias_off = 0;
3824
Mark Brown3a423152010-11-26 15:21:06 +00003825 switch (wm8994->revision) {
3826 case 2:
3827 case 3:
Mark Brown4537c4e2011-08-01 13:10:16 +09003828 wm8994->hubs.dcs_codes_l = -5;
3829 wm8994->hubs.dcs_codes_r = -5;
Mark Brown3a423152010-11-26 15:21:06 +00003830 wm8994->hubs.hp_startup_mode = 1;
3831 wm8994->hubs.dcs_readback_mode = 1;
Mark Brownf9acf9f2011-06-07 23:23:52 +01003832 wm8994->hubs.series_startup = 1;
Mark Brown3a423152010-11-26 15:21:06 +00003833 break;
3834 default:
Mark Brown79ef0ab2011-08-01 13:02:17 +09003835 wm8994->hubs.dcs_readback_mode = 2;
Mark Brown3a423152010-11-26 15:21:06 +00003836 break;
3837 }
Mark Brown280ec8b2011-08-10 22:19:19 +09003838 break;
Mark Brown3a423152010-11-26 15:21:06 +00003839
3840 case WM8958:
Mark Brown8437f702010-03-29 17:09:45 +01003841 wm8994->hubs.dcs_readback_mode = 1;
Mark Brown29fdc362012-02-21 10:50:50 +00003842 wm8994->hubs.hp_startup_mode = 1;
Mark Brown20dc24a2012-04-05 12:55:20 +01003843
3844 switch (wm8994->revision) {
3845 case 0:
3846 break;
3847 default:
3848 wm8994->fll_byp = true;
3849 break;
3850 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00003851 break;
Mark Brown3a423152010-11-26 15:21:06 +00003852
Mark Brown81204c82011-05-24 17:35:53 +08003853 case WM1811:
3854 wm8994->hubs.dcs_readback_mode = 2;
3855 wm8994->hubs.no_series_update = 1;
Mark Brown29fdc362012-02-21 10:50:50 +00003856 wm8994->hubs.hp_startup_mode = 1;
Mark Brownaf31a222012-04-26 20:06:56 +01003857 wm8994->hubs.no_cache_dac_hp_direct = true;
Mark Brown20dc24a2012-04-05 12:55:20 +01003858 wm8994->fll_byp = true;
Mark Brown81204c82011-05-24 17:35:53 +08003859
Mark Brown52ca1132012-08-23 15:50:45 +01003860 switch (control->cust_id) {
Mark Brown81204c82011-05-24 17:35:53 +08003861 case 0:
Mark Brownfc8e6e82011-11-28 18:48:46 +00003862 case 2:
Mark Brown6473a142011-10-17 19:38:52 +01003863 wm8994->hubs.dcs_codes_l = -9;
Mark Browne1660582012-03-21 13:22:40 +00003864 wm8994->hubs.dcs_codes_r = -7;
Mark Brown81204c82011-05-24 17:35:53 +08003865 break;
Mark Brown52ca1132012-08-23 15:50:45 +01003866 case 1:
3867 case 3:
3868 wm8994->hubs.dcs_codes_l = -8;
3869 wm8994->hubs.dcs_codes_r = -7;
3870 break;
Mark Brown81204c82011-05-24 17:35:53 +08003871 default:
3872 break;
3873 }
3874
3875 snd_soc_update_bits(codec, WM8994_ANALOGUE_HP_1,
3876 WM1811_HPOUT1_ATTN, WM1811_HPOUT1_ATTN);
3877 break;
3878
Mark Brown9e6e96a2010-01-29 17:47:12 +00003879 default:
3880 break;
3881 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00003882
Mark Brown2a8a8562011-07-24 12:20:41 +01003883 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR,
Mark Brown3b1af3f2011-07-14 12:38:18 +09003884 wm8994_fifo_error, "FIFO error", codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01003885 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN,
Mark Brownf0b182b2011-08-16 12:01:27 +09003886 wm8994_temp_warn, "Thermal warning", codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01003887 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT,
Mark Brownf0b182b2011-08-16 12:01:27 +09003888 wm8994_temp_shut, "Thermal shutdown", codec);
Mark Brown3b1af3f2011-07-14 12:38:18 +09003889
Mark Brown2a8a8562011-07-24 12:20:41 +01003890 ret = wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
Mark Brownb30ead52011-07-12 15:47:17 +09003891 wm_hubs_dcs_done, "DC servo done",
3892 &wm8994->hubs);
3893 if (ret == 0)
3894 wm8994->hubs.dcs_done_irq = true;
3895
Mark Brown3a423152010-11-26 15:21:06 +00003896 switch (control->type) {
3897 case WM8994:
Mark Brown9b7c5252011-02-17 20:05:44 -08003898 if (wm8994->micdet_irq) {
3899 ret = request_threaded_irq(wm8994->micdet_irq, NULL,
3900 wm8994_mic_irq,
3901 IRQF_TRIGGER_RISING,
3902 "Mic1 detect",
3903 wm8994);
3904 if (ret != 0)
3905 dev_warn(codec->dev,
3906 "Failed to request Mic1 detect IRQ: %d\n",
3907 ret);
3908 }
Mark Brown88766982010-03-29 20:57:12 +01003909
Mark Brown2a8a8562011-07-24 12:20:41 +01003910 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brown3a423152010-11-26 15:21:06 +00003911 WM8994_IRQ_MIC1_SHRT,
3912 wm8994_mic_irq, "Mic 1 short",
3913 wm8994);
3914 if (ret != 0)
3915 dev_warn(codec->dev,
3916 "Failed to request Mic1 short IRQ: %d\n",
3917 ret);
Mark Brown88766982010-03-29 20:57:12 +01003918
Mark Brown2a8a8562011-07-24 12:20:41 +01003919 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brown3a423152010-11-26 15:21:06 +00003920 WM8994_IRQ_MIC2_DET,
3921 wm8994_mic_irq, "Mic 2 detect",
3922 wm8994);
3923 if (ret != 0)
3924 dev_warn(codec->dev,
3925 "Failed to request Mic2 detect IRQ: %d\n",
3926 ret);
Mark Brown88766982010-03-29 20:57:12 +01003927
Mark Brown2a8a8562011-07-24 12:20:41 +01003928 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brown3a423152010-11-26 15:21:06 +00003929 WM8994_IRQ_MIC2_SHRT,
3930 wm8994_mic_irq, "Mic 2 short",
3931 wm8994);
3932 if (ret != 0)
3933 dev_warn(codec->dev,
3934 "Failed to request Mic2 short IRQ: %d\n",
3935 ret);
3936 break;
Mark Brown821edd22010-11-26 15:21:09 +00003937
3938 case WM8958:
Mark Brown81204c82011-05-24 17:35:53 +08003939 case WM1811:
Mark Brown9b7c5252011-02-17 20:05:44 -08003940 if (wm8994->micdet_irq) {
3941 ret = request_threaded_irq(wm8994->micdet_irq, NULL,
3942 wm8958_mic_irq,
3943 IRQF_TRIGGER_RISING,
3944 "Mic detect",
3945 wm8994);
3946 if (ret != 0)
3947 dev_warn(codec->dev,
3948 "Failed to request Mic detect IRQ: %d\n",
3949 ret);
Mark Brownb4046d02012-07-18 19:11:30 +01003950 } else {
3951 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_MIC1_DET,
3952 wm8958_mic_irq, "Mic detect",
3953 wm8994);
Mark Brown9b7c5252011-02-17 20:05:44 -08003954 }
Mark Brown3a423152010-11-26 15:21:06 +00003955 }
Mark Brown88766982010-03-29 20:57:12 +01003956
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003957 switch (control->type) {
3958 case WM1811:
Mark Brown52ca1132012-08-23 15:50:45 +01003959 if (control->cust_id > 1 || wm8994->revision > 1) {
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003960 ret = wm8994_request_irq(wm8994->wm8994,
3961 WM8994_IRQ_GPIO(6),
3962 wm1811_jackdet_irq, "JACKDET",
3963 wm8994);
3964 if (ret == 0)
3965 wm8994->jackdet = true;
3966 }
3967 break;
3968 default:
3969 break;
3970 }
3971
Mark Brownc7ebf932011-07-12 19:47:59 +09003972 wm8994->fll_locked_irq = true;
3973 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++) {
Mark Brown2a8a8562011-07-24 12:20:41 +01003974 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brownc7ebf932011-07-12 19:47:59 +09003975 WM8994_IRQ_FLL1_LOCK + i,
3976 wm8994_fll_locked_irq, "FLL lock",
3977 &wm8994->fll_locked[i]);
3978 if (ret != 0)
3979 wm8994->fll_locked_irq = false;
3980 }
3981
Mark Brown27060b3c2012-02-06 18:42:14 +00003982 /* Make sure we can read from the GPIOs if they're inputs */
3983 pm_runtime_get_sync(codec->dev);
3984
Mark Brown9e6e96a2010-01-29 17:47:12 +00003985 /* Remember if AIFnLRCLK is configured as a GPIO. This should be
3986 * configured on init - if a system wants to do this dynamically
3987 * at runtime we can deal with that then.
3988 */
Mark Brownd9a76662011-07-24 12:49:52 +01003989 ret = regmap_read(control->regmap, WM8994_GPIO_1, &reg);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003990 if (ret < 0) {
3991 dev_err(codec->dev, "Failed to read GPIO1 state: %d\n", ret);
Mark Brown88766982010-03-29 20:57:12 +01003992 goto err_irq;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003993 }
Mark Brownd9a76662011-07-24 12:49:52 +01003994 if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00003995 wm8994->lrclk_shared[0] = 1;
3996 wm8994_dai[0].symmetric_rates = 1;
3997 } else {
3998 wm8994->lrclk_shared[0] = 0;
3999 }
4000
Mark Brownd9a76662011-07-24 12:49:52 +01004001 ret = regmap_read(control->regmap, WM8994_GPIO_6, &reg);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004002 if (ret < 0) {
4003 dev_err(codec->dev, "Failed to read GPIO6 state: %d\n", ret);
Mark Brown88766982010-03-29 20:57:12 +01004004 goto err_irq;
Mark Brown9e6e96a2010-01-29 17:47:12 +00004005 }
Mark Brownd9a76662011-07-24 12:49:52 +01004006 if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00004007 wm8994->lrclk_shared[1] = 1;
4008 wm8994_dai[1].symmetric_rates = 1;
4009 } else {
4010 wm8994->lrclk_shared[1] = 0;
4011 }
4012
Mark Brown27060b3c2012-02-06 18:42:14 +00004013 pm_runtime_put(codec->dev);
4014
Mark Brownbfd37bb2012-06-05 12:31:32 +01004015 /* Latch volume update bits */
4016 for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
4017 snd_soc_update_bits(codec, wm8994_vu_bits[i].reg,
4018 wm8994_vu_bits[i].mask,
4019 wm8994_vu_bits[i].mask);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004020
4021 /* Set the low bit of the 3D stereo depth so TLV matches */
4022 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_FILTERS_2,
4023 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT,
4024 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT);
4025 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_FILTERS_2,
4026 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT,
4027 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT);
4028 snd_soc_update_bits(codec, WM8994_AIF2_DAC_FILTERS_2,
4029 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT,
4030 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT);
4031
Mark Brown5b739672011-07-06 00:08:43 -07004032 /* Unconditionally enable AIF1 ADC TDM mode on chips which can
4033 * use this; it only affects behaviour on idle TDM clock
4034 * cycles. */
4035 switch (control->type) {
4036 case WM8994:
4037 case WM8958:
4038 snd_soc_update_bits(codec, WM8994_AIF1_CONTROL_1,
4039 WM8994_AIF1ADC_TDM, WM8994_AIF1ADC_TDM);
4040 break;
4041 default:
4042 break;
4043 }
Mark Brownd1ce6b22010-07-20 10:13:14 +01004044
Mark Brown500fa302011-11-29 19:58:19 +00004045 /* Put MICBIAS into bypass mode by default on newer devices */
4046 switch (control->type) {
4047 case WM8958:
4048 case WM1811:
4049 snd_soc_update_bits(codec, WM8958_MICBIAS1,
4050 WM8958_MICB1_MODE, WM8958_MICB1_MODE);
4051 snd_soc_update_bits(codec, WM8958_MICBIAS2,
4052 WM8958_MICB2_MODE, WM8958_MICB2_MODE);
4053 break;
4054 default:
4055 break;
4056 }
4057
Mark Brownc3403042012-04-26 21:29:29 +01004058 wm8994->hubs.check_class_w_digital = wm8994_check_class_w_digital;
4059 wm_hubs_update_class_w(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004060
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004061 wm8994_handle_pdata(wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004062
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004063 wm_hubs_add_analogue_controls(codec);
Liam Girdwood022658b2012-02-03 17:43:09 +00004064 snd_soc_add_codec_controls(codec, wm8994_snd_controls,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004065 ARRAY_SIZE(wm8994_snd_controls));
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004066 snd_soc_dapm_new_controls(dapm, wm8994_dapm_widgets,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004067 ARRAY_SIZE(wm8994_dapm_widgets));
Mark Brownc4431df2010-11-26 15:21:07 +00004068
4069 switch (control->type) {
4070 case WM8994:
4071 snd_soc_dapm_new_controls(dapm, wm8994_specific_dapm_widgets,
4072 ARRAY_SIZE(wm8994_specific_dapm_widgets));
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00004073 if (wm8994->revision < 4) {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00004074 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
4075 ARRAY_SIZE(wm8994_lateclk_revd_widgets));
Dimitris Papastamos04d28682011-03-01 11:47:10 +00004076 snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
4077 ARRAY_SIZE(wm8994_adc_revd_widgets));
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00004078 snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
4079 ARRAY_SIZE(wm8994_dac_revd_widgets));
4080 } else {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00004081 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
4082 ARRAY_SIZE(wm8994_lateclk_widgets));
Dimitris Papastamos04d28682011-03-01 11:47:10 +00004083 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
4084 ARRAY_SIZE(wm8994_adc_widgets));
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00004085 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
4086 ARRAY_SIZE(wm8994_dac_widgets));
4087 }
Mark Brownc4431df2010-11-26 15:21:07 +00004088 break;
4089 case WM8958:
Liam Girdwood022658b2012-02-03 17:43:09 +00004090 snd_soc_add_codec_controls(codec, wm8958_snd_controls,
Mark Brownc4431df2010-11-26 15:21:07 +00004091 ARRAY_SIZE(wm8958_snd_controls));
4092 snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
4093 ARRAY_SIZE(wm8958_dapm_widgets));
Mark Brown780e2802011-03-11 18:00:19 +00004094 if (wm8994->revision < 1) {
4095 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
4096 ARRAY_SIZE(wm8994_lateclk_revd_widgets));
4097 snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
4098 ARRAY_SIZE(wm8994_adc_revd_widgets));
4099 snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
4100 ARRAY_SIZE(wm8994_dac_revd_widgets));
4101 } else {
4102 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
4103 ARRAY_SIZE(wm8994_lateclk_widgets));
4104 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
4105 ARRAY_SIZE(wm8994_adc_widgets));
4106 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
4107 ARRAY_SIZE(wm8994_dac_widgets));
4108 }
Mark Brownc4431df2010-11-26 15:21:07 +00004109 break;
Mark Brown81204c82011-05-24 17:35:53 +08004110
4111 case WM1811:
Liam Girdwood022658b2012-02-03 17:43:09 +00004112 snd_soc_add_codec_controls(codec, wm8958_snd_controls,
Mark Brown81204c82011-05-24 17:35:53 +08004113 ARRAY_SIZE(wm8958_snd_controls));
4114 snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
4115 ARRAY_SIZE(wm8958_dapm_widgets));
4116 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
4117 ARRAY_SIZE(wm8994_lateclk_widgets));
4118 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
4119 ARRAY_SIZE(wm8994_adc_widgets));
4120 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
4121 ARRAY_SIZE(wm8994_dac_widgets));
4122 break;
Mark Brownc4431df2010-11-26 15:21:07 +00004123 }
Mark Brownc4431df2010-11-26 15:21:07 +00004124
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004125 wm_hubs_add_analogue_routes(codec, 0, 0);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004126 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
Mark Brown9e6e96a2010-01-29 17:47:12 +00004127
Mark Brownc4431df2010-11-26 15:21:07 +00004128 switch (control->type) {
4129 case WM8994:
4130 snd_soc_dapm_add_routes(dapm, wm8994_intercon,
4131 ARRAY_SIZE(wm8994_intercon));
Mark Brown6ed8f142011-02-03 16:27:35 +00004132
Dimitris Papastamos173efa02011-02-11 16:32:11 +00004133 if (wm8994->revision < 4) {
Mark Brown6ed8f142011-02-03 16:27:35 +00004134 snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
4135 ARRAY_SIZE(wm8994_revd_intercon));
Dimitris Papastamos173efa02011-02-11 16:32:11 +00004136 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
4137 ARRAY_SIZE(wm8994_lateclk_revd_intercon));
4138 } else {
4139 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
4140 ARRAY_SIZE(wm8994_lateclk_intercon));
4141 }
Mark Brownc4431df2010-11-26 15:21:07 +00004142 break;
4143 case WM8958:
Mark Brown780e2802011-03-11 18:00:19 +00004144 if (wm8994->revision < 1) {
Chris Rattray15676932012-08-09 10:10:54 +01004145 snd_soc_dapm_add_routes(dapm, wm8994_intercon,
4146 ARRAY_SIZE(wm8994_intercon));
Mark Brown780e2802011-03-11 18:00:19 +00004147 snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
4148 ARRAY_SIZE(wm8994_revd_intercon));
4149 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
4150 ARRAY_SIZE(wm8994_lateclk_revd_intercon));
4151 } else {
4152 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
4153 ARRAY_SIZE(wm8994_lateclk_intercon));
4154 snd_soc_dapm_add_routes(dapm, wm8958_intercon,
4155 ARRAY_SIZE(wm8958_intercon));
4156 }
Mark Brownf701a2e2011-03-09 19:31:01 +00004157
4158 wm8958_dsp2_init(codec);
Mark Brownc4431df2010-11-26 15:21:07 +00004159 break;
Mark Brown81204c82011-05-24 17:35:53 +08004160 case WM1811:
4161 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
4162 ARRAY_SIZE(wm8994_lateclk_intercon));
4163 snd_soc_dapm_add_routes(dapm, wm8958_intercon,
4164 ARRAY_SIZE(wm8958_intercon));
4165 break;
Mark Brownc4431df2010-11-26 15:21:07 +00004166 }
4167
Mark Brown9e6e96a2010-01-29 17:47:12 +00004168 return 0;
4169
Mark Brown88766982010-03-29 20:57:12 +01004170err_irq:
Mark Brownaf6b6fe2011-11-30 20:32:05 +00004171 if (wm8994->jackdet)
4172 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01004173 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_SHRT, wm8994);
4174 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET, wm8994);
4175 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT, wm8994);
Mark Brown9b7c5252011-02-17 20:05:44 -08004176 if (wm8994->micdet_irq)
4177 free_irq(wm8994->micdet_irq, wm8994);
Mark Brownc7ebf932011-07-12 19:47:59 +09004178 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
Mark Brown2a8a8562011-07-24 12:20:41 +01004179 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
Mark Brownc7ebf932011-07-12 19:47:59 +09004180 &wm8994->fll_locked[i]);
Mark Brown2a8a8562011-07-24 12:20:41 +01004181 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
Mark Brownb30ead52011-07-12 15:47:17 +09004182 &wm8994->hubs);
Mark Brown2a8a8562011-07-24 12:20:41 +01004183 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, codec);
4184 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, codec);
4185 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, codec);
Mark Browna421a0e2011-12-29 11:08:34 +00004186
Mark Brown9e6e96a2010-01-29 17:47:12 +00004187 return ret;
4188}
4189
Jesper Juhl34ff0f92012-04-09 22:52:19 +02004190static int wm8994_codec_remove(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00004191{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004192 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01004193 struct wm8994 *control = wm8994->wm8994;
Mark Brownc7ebf932011-07-12 19:47:59 +09004194 int i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00004195
4196 wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004197
Mark Brown39fb51a2010-11-26 17:23:43 +00004198 pm_runtime_disable(codec->dev);
4199
Mark Brownc7ebf932011-07-12 19:47:59 +09004200 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
Mark Brown2a8a8562011-07-24 12:20:41 +01004201 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
Mark Brownc7ebf932011-07-12 19:47:59 +09004202 &wm8994->fll_locked[i]);
4203
Mark Brown2a8a8562011-07-24 12:20:41 +01004204 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
Mark Brownb30ead52011-07-12 15:47:17 +09004205 &wm8994->hubs);
Mark Brown2a8a8562011-07-24 12:20:41 +01004206 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, codec);
4207 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, codec);
4208 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, codec);
Mark Brownb30ead52011-07-12 15:47:17 +09004209
Mark Brownaf6b6fe2011-11-30 20:32:05 +00004210 if (wm8994->jackdet)
4211 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);
4212
Mark Brown3a423152010-11-26 15:21:06 +00004213 switch (control->type) {
4214 case WM8994:
Mark Brown9b7c5252011-02-17 20:05:44 -08004215 if (wm8994->micdet_irq)
4216 free_irq(wm8994->micdet_irq, wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01004217 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET,
Mark Brown3a423152010-11-26 15:21:06 +00004218 wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01004219 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT,
Mark Brown3a423152010-11-26 15:21:06 +00004220 wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01004221 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_DET,
Mark Brown3a423152010-11-26 15:21:06 +00004222 wm8994);
4223 break;
Mark Brown821edd22010-11-26 15:21:09 +00004224
Mark Brown81204c82011-05-24 17:35:53 +08004225 case WM1811:
Mark Brown821edd22010-11-26 15:21:09 +00004226 case WM8958:
Mark Brown9b7c5252011-02-17 20:05:44 -08004227 if (wm8994->micdet_irq)
4228 free_irq(wm8994->micdet_irq, wm8994);
Mark Brown821edd22010-11-26 15:21:09 +00004229 break;
Mark Brown3a423152010-11-26 15:21:06 +00004230 }
Jesper Juhl34ff0f92012-04-09 22:52:19 +02004231 release_firmware(wm8994->mbc);
4232 release_firmware(wm8994->mbc_vss);
4233 release_firmware(wm8994->enh_eq);
Axel Lin24fb2b12010-11-23 15:58:39 +08004234 kfree(wm8994->retune_mobile_texts);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004235 return 0;
4236}
4237
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004238static struct snd_soc_codec_driver soc_codec_dev_wm8994 = {
4239 .probe = wm8994_codec_probe,
4240 .remove = wm8994_codec_remove,
Mark Brown4752a882012-03-04 02:16:01 +00004241 .suspend = wm8994_codec_suspend,
4242 .resume = wm8994_codec_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004243 .set_bias_level = wm8994_set_bias_level,
4244};
4245
4246static int __devinit wm8994_probe(struct platform_device *pdev)
4247{
Mark Brown2bc16ed2012-03-03 23:24:39 +00004248 struct wm8994_priv *wm8994;
4249
4250 wm8994 = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_priv),
4251 GFP_KERNEL);
4252 if (wm8994 == NULL)
4253 return -ENOMEM;
4254 platform_set_drvdata(pdev, wm8994);
4255
4256 wm8994->wm8994 = dev_get_drvdata(pdev->dev.parent);
4257 wm8994->pdata = dev_get_platdata(pdev->dev.parent);
4258
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004259 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wm8994,
4260 wm8994_dai, ARRAY_SIZE(wm8994_dai));
4261}
4262
4263static int __devexit wm8994_remove(struct platform_device *pdev)
4264{
4265 snd_soc_unregister_codec(&pdev->dev);
4266 return 0;
4267}
4268
Mark Brown4752a882012-03-04 02:16:01 +00004269#ifdef CONFIG_PM_SLEEP
4270static int wm8994_suspend(struct device *dev)
4271{
4272 struct wm8994_priv *wm8994 = dev_get_drvdata(dev);
4273
4274 /* Drop down to power saving mode when system is suspended */
4275 if (wm8994->jackdet && !wm8994->active_refcount)
4276 regmap_update_bits(wm8994->wm8994->regmap, WM8994_ANTIPOP_2,
4277 WM1811_JACKDET_MODE_MASK,
4278 wm8994->jackdet_mode);
4279
4280 return 0;
4281}
4282
4283static int wm8994_resume(struct device *dev)
4284{
4285 struct wm8994_priv *wm8994 = dev_get_drvdata(dev);
4286
4287 if (wm8994->jackdet && wm8994->jack_cb)
4288 regmap_update_bits(wm8994->wm8994->regmap, WM8994_ANTIPOP_2,
4289 WM1811_JACKDET_MODE_MASK,
4290 WM1811_JACKDET_MODE_AUDIO);
4291
4292 return 0;
4293}
4294#endif
4295
4296static const struct dev_pm_ops wm8994_pm_ops = {
4297 SET_SYSTEM_SLEEP_PM_OPS(wm8994_suspend, wm8994_resume)
4298};
4299
Mark Brown9e6e96a2010-01-29 17:47:12 +00004300static struct platform_driver wm8994_codec_driver = {
4301 .driver = {
Mark Brown4752a882012-03-04 02:16:01 +00004302 .name = "wm8994-codec",
4303 .owner = THIS_MODULE,
4304 .pm = &wm8994_pm_ops,
4305 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004306 .probe = wm8994_probe,
4307 .remove = __devexit_p(wm8994_remove),
Mark Brown9e6e96a2010-01-29 17:47:12 +00004308};
4309
Mark Brown5bbcc3c2011-11-23 22:52:08 +00004310module_platform_driver(wm8994_codec_driver);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004311
4312MODULE_DESCRIPTION("ASoC WM8994 driver");
4313MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
4314MODULE_LICENSE("GPL");
4315MODULE_ALIAS("platform:wm8994-codec");