blob: d2db63022607fa634fb851e4aa01c4c7d1a848c1 [file] [log] [blame]
Mark Brown9e6e96a2010-01-29 17:47:12 +00001/*
2 * wm8994.c -- WM8994 ALSA SoC Audio driver
3 *
4 * Copyright 2009 Wolfson Microelectronics plc
5 *
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
49static int wm8994_drc_base[] = {
50 WM8994_AIF1_DRC1_1,
51 WM8994_AIF1_DRC2_1,
52 WM8994_AIF2_DRC_1,
53};
54
55static int wm8994_retune_mobile_base[] = {
56 WM8994_AIF1_DAC1_EQ_GAINS_1,
57 WM8994_AIF1_DAC2_EQ_GAINS_1,
58 WM8994_AIF2_EQ_GAINS_1,
59};
60
Mark Brownb00adf72011-08-13 11:57:18 +090061static void wm8958_default_micdet(u16 status, void *data);
62
Mark Brownaf6b6fe2011-11-30 20:32:05 +000063static const struct wm8958_micd_rate micdet_rates[] = {
Mark Brownb00adf72011-08-13 11:57:18 +090064 { 32768, true, 1, 4 },
65 { 32768, false, 1, 1 },
Mark Brown604533d2011-12-01 12:51:25 +000066 { 44100 * 256, true, 7, 10 },
67 { 44100 * 256, false, 7, 10 },
Mark Brownb00adf72011-08-13 11:57:18 +090068};
69
Mark Brownaf6b6fe2011-11-30 20:32:05 +000070static const struct wm8958_micd_rate jackdet_rates[] = {
71 { 32768, true, 0, 1 },
72 { 32768, false, 0, 1 },
Mark Browne9d9a962012-04-26 16:07:32 +010073 { 44100 * 256, true, 10, 10 },
74 { 44100 * 256, false, 7, 8 },
Mark Brownaf6b6fe2011-11-30 20:32:05 +000075};
76
Mark Brownb00adf72011-08-13 11:57:18 +090077static void wm8958_micd_set_rate(struct snd_soc_codec *codec)
78{
79 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
80 int best, i, sysclk, val;
81 bool idle;
Mark Brownaf6b6fe2011-11-30 20:32:05 +000082 const struct wm8958_micd_rate *rates;
83 int num_rates;
Mark Brownb00adf72011-08-13 11:57:18 +090084
Mark Brownfcdc4de2012-04-26 16:35:46 +010085 if (!(wm8994->pdata && wm8994->pdata->micd_rates) &&
86 wm8994->jack_cb != wm8958_default_micdet)
Mark Brownb00adf72011-08-13 11:57:18 +090087 return;
88
89 idle = !wm8994->jack_mic;
90
91 sysclk = snd_soc_read(codec, WM8994_CLOCKING_1);
92 if (sysclk & WM8994_SYSCLK_SRC)
93 sysclk = wm8994->aifclk[1];
94 else
95 sysclk = wm8994->aifclk[0];
96
Mark Browncd1707a2011-12-01 13:44:25 +000097 if (wm8994->pdata && wm8994->pdata->micd_rates) {
98 rates = wm8994->pdata->micd_rates;
99 num_rates = wm8994->pdata->num_micd_rates;
100 } else if (wm8994->jackdet) {
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000101 rates = jackdet_rates;
102 num_rates = ARRAY_SIZE(jackdet_rates);
103 } else {
104 rates = micdet_rates;
105 num_rates = ARRAY_SIZE(micdet_rates);
106 }
107
Mark Brownb00adf72011-08-13 11:57:18 +0900108 best = 0;
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000109 for (i = 0; i < num_rates; i++) {
110 if (rates[i].idle != idle)
Mark Brownb00adf72011-08-13 11:57:18 +0900111 continue;
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000112 if (abs(rates[i].sysclk - sysclk) <
113 abs(rates[best].sysclk - sysclk))
Mark Brownb00adf72011-08-13 11:57:18 +0900114 best = i;
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000115 else if (rates[best].idle != idle)
Mark Brownb00adf72011-08-13 11:57:18 +0900116 best = i;
117 }
118
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000119 val = rates[best].start << WM8958_MICD_BIAS_STARTTIME_SHIFT
120 | rates[best].rate << WM8958_MICD_RATE_SHIFT;
Mark Brownb00adf72011-08-13 11:57:18 +0900121
Mark Brown3a334ad2012-04-26 17:02:16 +0100122 dev_dbg(codec->dev, "MICD rate %d,%d for %dHz %s\n",
123 rates[best].start, rates[best].rate, sysclk,
124 idle ? "idle" : "active");
125
Mark Brownb00adf72011-08-13 11:57:18 +0900126 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
127 WM8958_MICD_BIAS_STARTTIME_MASK |
128 WM8958_MICD_RATE_MASK, val);
129}
130
Mark Brown9e6e96a2010-01-29 17:47:12 +0000131static int configure_aif_clock(struct snd_soc_codec *codec, int aif)
132{
Mark Brownb2c812e2010-04-14 15:35:19 +0900133 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000134 int rate;
135 int reg1 = 0;
136 int offset;
137
138 if (aif)
139 offset = 4;
140 else
141 offset = 0;
142
143 switch (wm8994->sysclk[aif]) {
144 case WM8994_SYSCLK_MCLK1:
145 rate = wm8994->mclk[0];
146 break;
147
148 case WM8994_SYSCLK_MCLK2:
149 reg1 |= 0x8;
150 rate = wm8994->mclk[1];
151 break;
152
153 case WM8994_SYSCLK_FLL1:
154 reg1 |= 0x10;
155 rate = wm8994->fll[0].out;
156 break;
157
158 case WM8994_SYSCLK_FLL2:
159 reg1 |= 0x18;
160 rate = wm8994->fll[1].out;
161 break;
162
163 default:
164 return -EINVAL;
165 }
166
167 if (rate >= 13500000) {
168 rate /= 2;
169 reg1 |= WM8994_AIF1CLK_DIV;
170
171 dev_dbg(codec->dev, "Dividing AIF%d clock to %dHz\n",
172 aif + 1, rate);
173 }
Mark Brown5e5e2be2010-04-25 12:20:30 +0100174
Mark Brown9e6e96a2010-01-29 17:47:12 +0000175 wm8994->aifclk[aif] = rate;
176
177 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1 + offset,
178 WM8994_AIF1CLK_SRC_MASK | WM8994_AIF1CLK_DIV,
179 reg1);
180
181 return 0;
182}
183
184static int configure_clock(struct snd_soc_codec *codec)
185{
Mark Brownb2c812e2010-04-14 15:35:19 +0900186 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Axel Lin04f45c42011-10-04 20:07:03 +0800187 int change, new;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000188
189 /* Bring up the AIF clocks first */
190 configure_aif_clock(codec, 0);
191 configure_aif_clock(codec, 1);
192
193 /* Then switch CLK_SYS over to the higher of them; a change
194 * can only happen as a result of a clocking change which can
195 * only be made outside of DAPM so we can safely redo the
196 * clocking.
197 */
198
199 /* If they're equal it doesn't matter which is used */
Mark Brownb00adf72011-08-13 11:57:18 +0900200 if (wm8994->aifclk[0] == wm8994->aifclk[1]) {
201 wm8958_micd_set_rate(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000202 return 0;
Mark Brownb00adf72011-08-13 11:57:18 +0900203 }
Mark Brown9e6e96a2010-01-29 17:47:12 +0000204
205 if (wm8994->aifclk[0] < wm8994->aifclk[1])
206 new = WM8994_SYSCLK_SRC;
207 else
208 new = 0;
209
Axel Lin04f45c42011-10-04 20:07:03 +0800210 change = snd_soc_update_bits(codec, WM8994_CLOCKING_1,
211 WM8994_SYSCLK_SRC, new);
Mark Brown52ac7ab2011-12-01 12:43:26 +0000212 if (change)
213 snd_soc_dapm_sync(&codec->dapm);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000214
Mark Brownb00adf72011-08-13 11:57:18 +0900215 wm8958_micd_set_rate(codec);
216
Mark Brown9e6e96a2010-01-29 17:47:12 +0000217 return 0;
218}
219
220static int check_clk_sys(struct snd_soc_dapm_widget *source,
221 struct snd_soc_dapm_widget *sink)
222{
223 int reg = snd_soc_read(source->codec, WM8994_CLOCKING_1);
224 const char *clk;
225
226 /* Check what we're currently using for CLK_SYS */
227 if (reg & WM8994_SYSCLK_SRC)
228 clk = "AIF2CLK";
229 else
230 clk = "AIF1CLK";
231
232 return strcmp(source->name, clk) == 0;
233}
234
235static const char *sidetone_hpf_text[] = {
236 "2.7kHz", "1.35kHz", "675Hz", "370Hz", "180Hz", "90Hz", "45Hz"
237};
238
239static const struct soc_enum sidetone_hpf =
240 SOC_ENUM_SINGLE(WM8994_SIDETONE, 7, 7, sidetone_hpf_text);
241
Uk Kim146fd572010-12-07 13:58:40 +0000242static const char *adc_hpf_text[] = {
243 "HiFi", "Voice 1", "Voice 2", "Voice 3"
244};
245
246static const struct soc_enum aif1adc1_hpf =
247 SOC_ENUM_SINGLE(WM8994_AIF1_ADC1_FILTERS, 13, 4, adc_hpf_text);
248
249static const struct soc_enum aif1adc2_hpf =
250 SOC_ENUM_SINGLE(WM8994_AIF1_ADC2_FILTERS, 13, 4, adc_hpf_text);
251
252static const struct soc_enum aif2adc_hpf =
253 SOC_ENUM_SINGLE(WM8994_AIF2_ADC_FILTERS, 13, 4, adc_hpf_text);
254
Mark Brown9e6e96a2010-01-29 17:47:12 +0000255static const DECLARE_TLV_DB_SCALE(aif_tlv, 0, 600, 0);
256static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
257static const DECLARE_TLV_DB_SCALE(st_tlv, -3600, 300, 0);
258static const DECLARE_TLV_DB_SCALE(wm8994_3d_tlv, -1600, 183, 0);
259static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
Mark Brown1ddc07d2011-08-16 10:08:48 +0900260static const DECLARE_TLV_DB_SCALE(ng_tlv, -10200, 600, 0);
Mark Brown81204c82011-05-24 17:35:53 +0800261static const DECLARE_TLV_DB_SCALE(mixin_boost_tlv, 0, 900, 0);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000262
263#define WM8994_DRC_SWITCH(xname, reg, shift) \
264{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
265 .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,\
266 .put = wm8994_put_drc_sw, \
267 .private_value = SOC_SINGLE_VALUE(reg, shift, 1, 0) }
268
269static int wm8994_put_drc_sw(struct snd_kcontrol *kcontrol,
270 struct snd_ctl_elem_value *ucontrol)
271{
272 struct soc_mixer_control *mc =
273 (struct soc_mixer_control *)kcontrol->private_value;
274 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
275 int mask, ret;
276
277 /* Can't enable both ADC and DAC paths simultaneously */
278 if (mc->shift == WM8994_AIF1DAC1_DRC_ENA_SHIFT)
279 mask = WM8994_AIF1ADC1L_DRC_ENA_MASK |
280 WM8994_AIF1ADC1R_DRC_ENA_MASK;
281 else
282 mask = WM8994_AIF1DAC1_DRC_ENA_MASK;
283
284 ret = snd_soc_read(codec, mc->reg);
285 if (ret < 0)
286 return ret;
287 if (ret & mask)
288 return -EINVAL;
289
290 return snd_soc_put_volsw(kcontrol, ucontrol);
291}
292
Mark Brown9e6e96a2010-01-29 17:47:12 +0000293static void wm8994_set_drc(struct snd_soc_codec *codec, int drc)
294{
Mark Brownb2c812e2010-04-14 15:35:19 +0900295 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000296 struct wm8994_pdata *pdata = wm8994->pdata;
297 int base = wm8994_drc_base[drc];
298 int cfg = wm8994->drc_cfg[drc];
299 int save, i;
300
301 /* Save any enables; the configuration should clear them. */
302 save = snd_soc_read(codec, base);
303 save &= WM8994_AIF1DAC1_DRC_ENA | WM8994_AIF1ADC1L_DRC_ENA |
304 WM8994_AIF1ADC1R_DRC_ENA;
305
306 for (i = 0; i < WM8994_DRC_REGS; i++)
307 snd_soc_update_bits(codec, base + i, 0xffff,
308 pdata->drc_cfgs[cfg].regs[i]);
309
310 snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_DRC_ENA |
311 WM8994_AIF1ADC1L_DRC_ENA |
312 WM8994_AIF1ADC1R_DRC_ENA, save);
313}
314
315/* Icky as hell but saves code duplication */
316static int wm8994_get_drc(const char *name)
317{
318 if (strcmp(name, "AIF1DRC1 Mode") == 0)
319 return 0;
320 if (strcmp(name, "AIF1DRC2 Mode") == 0)
321 return 1;
322 if (strcmp(name, "AIF2DRC Mode") == 0)
323 return 2;
324 return -EINVAL;
325}
326
327static int wm8994_put_drc_enum(struct snd_kcontrol *kcontrol,
328 struct snd_ctl_elem_value *ucontrol)
329{
330 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000331 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000332 struct wm8994_pdata *pdata = wm8994->pdata;
333 int drc = wm8994_get_drc(kcontrol->id.name);
334 int value = ucontrol->value.integer.value[0];
335
336 if (drc < 0)
337 return drc;
338
339 if (value >= pdata->num_drc_cfgs)
340 return -EINVAL;
341
342 wm8994->drc_cfg[drc] = value;
343
344 wm8994_set_drc(codec, drc);
345
346 return 0;
347}
348
349static int wm8994_get_drc_enum(struct snd_kcontrol *kcontrol,
350 struct snd_ctl_elem_value *ucontrol)
351{
352 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +0900353 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000354 int drc = wm8994_get_drc(kcontrol->id.name);
355
356 ucontrol->value.enumerated.item[0] = wm8994->drc_cfg[drc];
357
358 return 0;
359}
360
361static void wm8994_set_retune_mobile(struct snd_soc_codec *codec, int block)
362{
Mark Brownb2c812e2010-04-14 15:35:19 +0900363 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000364 struct wm8994_pdata *pdata = wm8994->pdata;
365 int base = wm8994_retune_mobile_base[block];
366 int iface, best, best_val, save, i, cfg;
367
368 if (!pdata || !wm8994->num_retune_mobile_texts)
369 return;
370
371 switch (block) {
372 case 0:
373 case 1:
374 iface = 0;
375 break;
376 case 2:
377 iface = 1;
378 break;
379 default:
380 return;
381 }
382
383 /* Find the version of the currently selected configuration
384 * with the nearest sample rate. */
385 cfg = wm8994->retune_mobile_cfg[block];
386 best = 0;
387 best_val = INT_MAX;
388 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
389 if (strcmp(pdata->retune_mobile_cfgs[i].name,
390 wm8994->retune_mobile_texts[cfg]) == 0 &&
391 abs(pdata->retune_mobile_cfgs[i].rate
392 - wm8994->dac_rates[iface]) < best_val) {
393 best = i;
394 best_val = abs(pdata->retune_mobile_cfgs[i].rate
395 - wm8994->dac_rates[iface]);
396 }
397 }
398
399 dev_dbg(codec->dev, "ReTune Mobile %d %s/%dHz for %dHz sample rate\n",
400 block,
401 pdata->retune_mobile_cfgs[best].name,
402 pdata->retune_mobile_cfgs[best].rate,
403 wm8994->dac_rates[iface]);
404
405 /* The EQ will be disabled while reconfiguring it, remember the
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +0200406 * current configuration.
Mark Brown9e6e96a2010-01-29 17:47:12 +0000407 */
408 save = snd_soc_read(codec, base);
409 save &= WM8994_AIF1DAC1_EQ_ENA;
410
411 for (i = 0; i < WM8994_EQ_REGS; i++)
412 snd_soc_update_bits(codec, base + i, 0xffff,
413 pdata->retune_mobile_cfgs[best].regs[i]);
414
415 snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_EQ_ENA, save);
416}
417
418/* Icky as hell but saves code duplication */
419static int wm8994_get_retune_mobile_block(const char *name)
420{
421 if (strcmp(name, "AIF1.1 EQ Mode") == 0)
422 return 0;
423 if (strcmp(name, "AIF1.2 EQ Mode") == 0)
424 return 1;
425 if (strcmp(name, "AIF2 EQ Mode") == 0)
426 return 2;
427 return -EINVAL;
428}
429
430static int wm8994_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
431 struct snd_ctl_elem_value *ucontrol)
432{
433 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000434 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000435 struct wm8994_pdata *pdata = wm8994->pdata;
436 int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
437 int value = ucontrol->value.integer.value[0];
438
439 if (block < 0)
440 return block;
441
442 if (value >= pdata->num_retune_mobile_cfgs)
443 return -EINVAL;
444
445 wm8994->retune_mobile_cfg[block] = value;
446
447 wm8994_set_retune_mobile(codec, block);
448
449 return 0;
450}
451
452static int wm8994_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
453 struct snd_ctl_elem_value *ucontrol)
454{
455 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brown4a8d9292011-02-16 14:57:17 -0800456 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000457 int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
458
459 ucontrol->value.enumerated.item[0] = wm8994->retune_mobile_cfg[block];
460
461 return 0;
462}
463
Mark Brown96b101e2010-11-18 15:49:38 +0000464static const char *aif_chan_src_text[] = {
Mark Brownf5548852010-08-31 19:39:48 +0100465 "Left", "Right"
466};
467
Mark Brown96b101e2010-11-18 15:49:38 +0000468static const struct soc_enum aif1adcl_src =
469 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_1, 15, 2, aif_chan_src_text);
470
471static const struct soc_enum aif1adcr_src =
472 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_1, 14, 2, aif_chan_src_text);
473
474static const struct soc_enum aif2adcl_src =
475 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_1, 15, 2, aif_chan_src_text);
476
477static const struct soc_enum aif2adcr_src =
478 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_1, 14, 2, aif_chan_src_text);
479
Mark Brownf5548852010-08-31 19:39:48 +0100480static const struct soc_enum aif1dacl_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000481 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_2, 15, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100482
483static const struct soc_enum aif1dacr_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000484 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_2, 14, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100485
486static const struct soc_enum aif2dacl_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000487 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_2, 15, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100488
489static const struct soc_enum aif2dacr_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000490 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_2, 14, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100491
Mark Brown154b26a2010-12-09 12:07:44 +0000492static const char *osr_text[] = {
493 "Low Power", "High Performance",
494};
495
496static const struct soc_enum dac_osr =
497 SOC_ENUM_SINGLE(WM8994_OVERSAMPLING, 0, 2, osr_text);
498
499static const struct soc_enum adc_osr =
500 SOC_ENUM_SINGLE(WM8994_OVERSAMPLING, 1, 2, osr_text);
501
Mark Brown9e6e96a2010-01-29 17:47:12 +0000502static const struct snd_kcontrol_new wm8994_snd_controls[] = {
503SOC_DOUBLE_R_TLV("AIF1ADC1 Volume", WM8994_AIF1_ADC1_LEFT_VOLUME,
504 WM8994_AIF1_ADC1_RIGHT_VOLUME,
505 1, 119, 0, digital_tlv),
506SOC_DOUBLE_R_TLV("AIF1ADC2 Volume", WM8994_AIF1_ADC2_LEFT_VOLUME,
507 WM8994_AIF1_ADC2_RIGHT_VOLUME,
508 1, 119, 0, digital_tlv),
509SOC_DOUBLE_R_TLV("AIF2ADC Volume", WM8994_AIF2_ADC_LEFT_VOLUME,
510 WM8994_AIF2_ADC_RIGHT_VOLUME,
511 1, 119, 0, digital_tlv),
512
Mark Brown96b101e2010-11-18 15:49:38 +0000513SOC_ENUM("AIF1ADCL Source", aif1adcl_src),
514SOC_ENUM("AIF1ADCR Source", aif1adcr_src),
Mark Brown49db7e72010-12-08 13:49:43 +0000515SOC_ENUM("AIF2ADCL Source", aif2adcl_src),
516SOC_ENUM("AIF2ADCR Source", aif2adcr_src),
Mark Brown96b101e2010-11-18 15:49:38 +0000517
Mark Brownf5548852010-08-31 19:39:48 +0100518SOC_ENUM("AIF1DACL Source", aif1dacl_src),
519SOC_ENUM("AIF1DACR Source", aif1dacr_src),
Mark Brown49db7e72010-12-08 13:49:43 +0000520SOC_ENUM("AIF2DACL Source", aif2dacl_src),
521SOC_ENUM("AIF2DACR Source", aif2dacr_src),
Mark Brownf5548852010-08-31 19:39:48 +0100522
Mark Brown9e6e96a2010-01-29 17:47:12 +0000523SOC_DOUBLE_R_TLV("AIF1DAC1 Volume", WM8994_AIF1_DAC1_LEFT_VOLUME,
524 WM8994_AIF1_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
525SOC_DOUBLE_R_TLV("AIF1DAC2 Volume", WM8994_AIF1_DAC2_LEFT_VOLUME,
526 WM8994_AIF1_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
527SOC_DOUBLE_R_TLV("AIF2DAC Volume", WM8994_AIF2_DAC_LEFT_VOLUME,
528 WM8994_AIF2_DAC_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
529
530SOC_SINGLE_TLV("AIF1 Boost Volume", WM8994_AIF1_CONTROL_2, 10, 3, 0, aif_tlv),
531SOC_SINGLE_TLV("AIF2 Boost Volume", WM8994_AIF2_CONTROL_2, 10, 3, 0, aif_tlv),
532
533SOC_SINGLE("AIF1DAC1 EQ Switch", WM8994_AIF1_DAC1_EQ_GAINS_1, 0, 1, 0),
534SOC_SINGLE("AIF1DAC2 EQ Switch", WM8994_AIF1_DAC2_EQ_GAINS_1, 0, 1, 0),
535SOC_SINGLE("AIF2 EQ Switch", WM8994_AIF2_EQ_GAINS_1, 0, 1, 0),
536
537WM8994_DRC_SWITCH("AIF1DAC1 DRC Switch", WM8994_AIF1_DRC1_1, 2),
538WM8994_DRC_SWITCH("AIF1ADC1L DRC Switch", WM8994_AIF1_DRC1_1, 1),
539WM8994_DRC_SWITCH("AIF1ADC1R DRC Switch", WM8994_AIF1_DRC1_1, 0),
540
541WM8994_DRC_SWITCH("AIF1DAC2 DRC Switch", WM8994_AIF1_DRC2_1, 2),
542WM8994_DRC_SWITCH("AIF1ADC2L DRC Switch", WM8994_AIF1_DRC2_1, 1),
543WM8994_DRC_SWITCH("AIF1ADC2R DRC Switch", WM8994_AIF1_DRC2_1, 0),
544
545WM8994_DRC_SWITCH("AIF2DAC DRC Switch", WM8994_AIF2_DRC_1, 2),
546WM8994_DRC_SWITCH("AIF2ADCL DRC Switch", WM8994_AIF2_DRC_1, 1),
547WM8994_DRC_SWITCH("AIF2ADCR DRC Switch", WM8994_AIF2_DRC_1, 0),
548
549SOC_SINGLE_TLV("DAC1 Right Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
550 5, 12, 0, st_tlv),
551SOC_SINGLE_TLV("DAC1 Left Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
552 0, 12, 0, st_tlv),
553SOC_SINGLE_TLV("DAC2 Right Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
554 5, 12, 0, st_tlv),
555SOC_SINGLE_TLV("DAC2 Left Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
556 0, 12, 0, st_tlv),
557SOC_ENUM("Sidetone HPF Mux", sidetone_hpf),
558SOC_SINGLE("Sidetone HPF Switch", WM8994_SIDETONE, 6, 1, 0),
559
Uk Kim146fd572010-12-07 13:58:40 +0000560SOC_ENUM("AIF1ADC1 HPF Mode", aif1adc1_hpf),
561SOC_DOUBLE("AIF1ADC1 HPF Switch", WM8994_AIF1_ADC1_FILTERS, 12, 11, 1, 0),
562
563SOC_ENUM("AIF1ADC2 HPF Mode", aif1adc2_hpf),
564SOC_DOUBLE("AIF1ADC2 HPF Switch", WM8994_AIF1_ADC2_FILTERS, 12, 11, 1, 0),
565
566SOC_ENUM("AIF2ADC HPF Mode", aif2adc_hpf),
567SOC_DOUBLE("AIF2ADC HPF Switch", WM8994_AIF2_ADC_FILTERS, 12, 11, 1, 0),
568
Mark Brown154b26a2010-12-09 12:07:44 +0000569SOC_ENUM("ADC OSR", adc_osr),
570SOC_ENUM("DAC OSR", dac_osr),
571
Mark Brown9e6e96a2010-01-29 17:47:12 +0000572SOC_DOUBLE_R_TLV("DAC1 Volume", WM8994_DAC1_LEFT_VOLUME,
573 WM8994_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
574SOC_DOUBLE_R("DAC1 Switch", WM8994_DAC1_LEFT_VOLUME,
575 WM8994_DAC1_RIGHT_VOLUME, 9, 1, 1),
576
577SOC_DOUBLE_R_TLV("DAC2 Volume", WM8994_DAC2_LEFT_VOLUME,
578 WM8994_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
579SOC_DOUBLE_R("DAC2 Switch", WM8994_DAC2_LEFT_VOLUME,
580 WM8994_DAC2_RIGHT_VOLUME, 9, 1, 1),
581
582SOC_SINGLE_TLV("SPKL DAC2 Volume", WM8994_SPKMIXL_ATTENUATION,
583 6, 1, 1, wm_hubs_spkmix_tlv),
584SOC_SINGLE_TLV("SPKL DAC1 Volume", WM8994_SPKMIXL_ATTENUATION,
585 2, 1, 1, wm_hubs_spkmix_tlv),
586
587SOC_SINGLE_TLV("SPKR DAC2 Volume", WM8994_SPKMIXR_ATTENUATION,
588 6, 1, 1, wm_hubs_spkmix_tlv),
589SOC_SINGLE_TLV("SPKR DAC1 Volume", WM8994_SPKMIXR_ATTENUATION,
590 2, 1, 1, wm_hubs_spkmix_tlv),
591
592SOC_SINGLE_TLV("AIF1DAC1 3D Stereo Volume", WM8994_AIF1_DAC1_FILTERS_2,
593 10, 15, 0, wm8994_3d_tlv),
Mark Brown458350b2010-12-20 14:35:09 +0000594SOC_SINGLE("AIF1DAC1 3D Stereo Switch", WM8994_AIF1_DAC1_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000595 8, 1, 0),
596SOC_SINGLE_TLV("AIF1DAC2 3D Stereo Volume", WM8994_AIF1_DAC2_FILTERS_2,
597 10, 15, 0, wm8994_3d_tlv),
598SOC_SINGLE("AIF1DAC2 3D Stereo Switch", WM8994_AIF1_DAC2_FILTERS_2,
599 8, 1, 0),
Mark Brown458350b2010-12-20 14:35:09 +0000600SOC_SINGLE_TLV("AIF2DAC 3D Stereo Volume", WM8994_AIF2_DAC_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000601 10, 15, 0, wm8994_3d_tlv),
Mark Brown458350b2010-12-20 14:35:09 +0000602SOC_SINGLE("AIF2DAC 3D Stereo Switch", WM8994_AIF2_DAC_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000603 8, 1, 0),
604};
605
606static const struct snd_kcontrol_new wm8994_eq_controls[] = {
607SOC_SINGLE_TLV("AIF1DAC1 EQ1 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 11, 31, 0,
608 eq_tlv),
609SOC_SINGLE_TLV("AIF1DAC1 EQ2 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 6, 31, 0,
610 eq_tlv),
611SOC_SINGLE_TLV("AIF1DAC1 EQ3 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 1, 31, 0,
612 eq_tlv),
613SOC_SINGLE_TLV("AIF1DAC1 EQ4 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 11, 31, 0,
614 eq_tlv),
615SOC_SINGLE_TLV("AIF1DAC1 EQ5 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 6, 31, 0,
616 eq_tlv),
617
618SOC_SINGLE_TLV("AIF1DAC2 EQ1 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 11, 31, 0,
619 eq_tlv),
620SOC_SINGLE_TLV("AIF1DAC2 EQ2 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 6, 31, 0,
621 eq_tlv),
622SOC_SINGLE_TLV("AIF1DAC2 EQ3 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 1, 31, 0,
623 eq_tlv),
624SOC_SINGLE_TLV("AIF1DAC2 EQ4 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 11, 31, 0,
625 eq_tlv),
626SOC_SINGLE_TLV("AIF1DAC2 EQ5 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 6, 31, 0,
627 eq_tlv),
628
629SOC_SINGLE_TLV("AIF2 EQ1 Volume", WM8994_AIF2_EQ_GAINS_1, 11, 31, 0,
630 eq_tlv),
631SOC_SINGLE_TLV("AIF2 EQ2 Volume", WM8994_AIF2_EQ_GAINS_1, 6, 31, 0,
632 eq_tlv),
633SOC_SINGLE_TLV("AIF2 EQ3 Volume", WM8994_AIF2_EQ_GAINS_1, 1, 31, 0,
634 eq_tlv),
635SOC_SINGLE_TLV("AIF2 EQ4 Volume", WM8994_AIF2_EQ_GAINS_2, 11, 31, 0,
636 eq_tlv),
637SOC_SINGLE_TLV("AIF2 EQ5 Volume", WM8994_AIF2_EQ_GAINS_2, 6, 31, 0,
638 eq_tlv),
639};
640
Mark Brown1ddc07d2011-08-16 10:08:48 +0900641static const char *wm8958_ng_text[] = {
642 "30ms", "125ms", "250ms", "500ms",
643};
644
645static const struct soc_enum wm8958_aif1dac1_ng_hold =
646 SOC_ENUM_SINGLE(WM8958_AIF1_DAC1_NOISE_GATE,
647 WM8958_AIF1DAC1_NG_THR_SHIFT, 4, wm8958_ng_text);
648
649static const struct soc_enum wm8958_aif1dac2_ng_hold =
650 SOC_ENUM_SINGLE(WM8958_AIF1_DAC2_NOISE_GATE,
651 WM8958_AIF1DAC2_NG_THR_SHIFT, 4, wm8958_ng_text);
652
653static const struct soc_enum wm8958_aif2dac_ng_hold =
654 SOC_ENUM_SINGLE(WM8958_AIF2_DAC_NOISE_GATE,
655 WM8958_AIF2DAC_NG_THR_SHIFT, 4, wm8958_ng_text);
656
Mark Brownc4431df2010-11-26 15:21:07 +0000657static const struct snd_kcontrol_new wm8958_snd_controls[] = {
658SOC_SINGLE_TLV("AIF3 Boost Volume", WM8958_AIF3_CONTROL_2, 10, 3, 0, aif_tlv),
Mark Brown1ddc07d2011-08-16 10:08:48 +0900659
660SOC_SINGLE("AIF1DAC1 Noise Gate Switch", WM8958_AIF1_DAC1_NOISE_GATE,
661 WM8958_AIF1DAC1_NG_ENA_SHIFT, 1, 0),
662SOC_ENUM("AIF1DAC1 Noise Gate Hold Time", wm8958_aif1dac1_ng_hold),
663SOC_SINGLE_TLV("AIF1DAC1 Noise Gate Threshold Volume",
664 WM8958_AIF1_DAC1_NOISE_GATE, WM8958_AIF1DAC1_NG_THR_SHIFT,
665 7, 1, ng_tlv),
666
667SOC_SINGLE("AIF1DAC2 Noise Gate Switch", WM8958_AIF1_DAC2_NOISE_GATE,
668 WM8958_AIF1DAC2_NG_ENA_SHIFT, 1, 0),
669SOC_ENUM("AIF1DAC2 Noise Gate Hold Time", wm8958_aif1dac2_ng_hold),
670SOC_SINGLE_TLV("AIF1DAC2 Noise Gate Threshold Volume",
671 WM8958_AIF1_DAC2_NOISE_GATE, WM8958_AIF1DAC2_NG_THR_SHIFT,
672 7, 1, ng_tlv),
673
674SOC_SINGLE("AIF2DAC Noise Gate Switch", WM8958_AIF2_DAC_NOISE_GATE,
675 WM8958_AIF2DAC_NG_ENA_SHIFT, 1, 0),
676SOC_ENUM("AIF2DAC Noise Gate Hold Time", wm8958_aif2dac_ng_hold),
677SOC_SINGLE_TLV("AIF2DAC Noise Gate Threshold Volume",
678 WM8958_AIF2_DAC_NOISE_GATE, WM8958_AIF2DAC_NG_THR_SHIFT,
679 7, 1, ng_tlv),
Mark Brownc4431df2010-11-26 15:21:07 +0000680};
681
Mark Brown81204c82011-05-24 17:35:53 +0800682static const struct snd_kcontrol_new wm1811_snd_controls[] = {
683SOC_SINGLE_TLV("MIXINL IN1LP Boost Volume", WM8994_INPUT_MIXER_1, 7, 1, 0,
684 mixin_boost_tlv),
685SOC_SINGLE_TLV("MIXINL IN1RP Boost Volume", WM8994_INPUT_MIXER_1, 8, 1, 0,
686 mixin_boost_tlv),
687};
688
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000689/* We run all mode setting through a function to enforce audio mode */
690static void wm1811_jackdet_set_mode(struct snd_soc_codec *codec, u16 mode)
691{
692 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
693
Mark Brown28e33262012-03-03 00:10:02 +0000694 if (!wm8994->jackdet || !wm8994->jack_cb)
695 return;
696
Mark Brown149c53b2012-03-03 00:10:02 +0000697 if (!wm8994->jackdet || !wm8994->jack_cb)
698 return;
699
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000700 if (wm8994->active_refcount)
701 mode = WM1811_JACKDET_MODE_AUDIO;
702
Mark Brown4752a882012-03-04 02:16:01 +0000703 if (mode == wm8994->jackdet_mode)
Mark Brown1defde22012-03-03 20:02:49 +0000704 return;
705
Mark Brown4752a882012-03-04 02:16:01 +0000706 wm8994->jackdet_mode = mode;
707
708 /* Always use audio mode to detect while the system is active */
709 if (mode != WM1811_JACKDET_MODE_NONE)
710 mode = WM1811_JACKDET_MODE_AUDIO;
711
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000712 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
713 WM1811_JACKDET_MODE_MASK, mode);
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000714}
715
716static void active_reference(struct snd_soc_codec *codec)
717{
718 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
719
720 mutex_lock(&wm8994->accdet_lock);
721
722 wm8994->active_refcount++;
723
724 dev_dbg(codec->dev, "Active refcount incremented, now %d\n",
725 wm8994->active_refcount);
726
Mark Brown1defde22012-03-03 20:02:49 +0000727 /* If we're using jack detection go into audio mode */
728 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_AUDIO);
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000729
730 mutex_unlock(&wm8994->accdet_lock);
731}
732
733static void active_dereference(struct snd_soc_codec *codec)
734{
735 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
736 u16 mode;
737
738 mutex_lock(&wm8994->accdet_lock);
739
740 wm8994->active_refcount--;
741
742 dev_dbg(codec->dev, "Active refcount decremented, now %d\n",
743 wm8994->active_refcount);
744
745 if (wm8994->active_refcount == 0) {
746 /* Go into appropriate detection only mode */
Mark Brown1defde22012-03-03 20:02:49 +0000747 if (wm8994->jack_mic || wm8994->mic_detecting)
748 mode = WM1811_JACKDET_MODE_MIC;
749 else
750 mode = WM1811_JACKDET_MODE_JACK;
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000751
Mark Brown1defde22012-03-03 20:02:49 +0000752 wm1811_jackdet_set_mode(codec, mode);
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000753 }
754
755 mutex_unlock(&wm8994->accdet_lock);
756}
757
Mark Brown9e6e96a2010-01-29 17:47:12 +0000758static int clk_sys_event(struct snd_soc_dapm_widget *w,
759 struct snd_kcontrol *kcontrol, int event)
760{
761 struct snd_soc_codec *codec = w->codec;
762
763 switch (event) {
764 case SND_SOC_DAPM_PRE_PMU:
765 return configure_clock(codec);
766
767 case SND_SOC_DAPM_POST_PMD:
768 configure_clock(codec);
769 break;
770 }
771
772 return 0;
773}
774
Mark Brown4b7ed832011-08-10 17:47:33 +0900775static void vmid_reference(struct snd_soc_codec *codec)
776{
777 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
778
Mark Browndb966f82012-02-06 12:07:08 +0000779 pm_runtime_get_sync(codec->dev);
780
Mark Brown4b7ed832011-08-10 17:47:33 +0900781 wm8994->vmid_refcount++;
782
783 dev_dbg(codec->dev, "Referencing VMID, refcount is now %d\n",
784 wm8994->vmid_refcount);
785
786 if (wm8994->vmid_refcount == 1) {
Mark Browncc6d5a82012-02-11 23:09:53 +0000787 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
Mark Browncc6d5a82012-02-11 23:09:53 +0000788 WM8994_LINEOUT1_DISCH |
Mark Brown22f8d052012-03-19 17:32:06 +0000789 WM8994_LINEOUT2_DISCH, 0);
Mark Browncc6d5a82012-02-11 23:09:53 +0000790
Mark Brownf7085642012-02-21 16:24:00 +0000791 wm_hubs_vmid_ena(codec);
792
Mark Brown22f8d052012-03-19 17:32:06 +0000793 switch (wm8994->vmid_mode) {
794 default:
795 WARN_ON(0 == "Invalid VMID mode");
796 case WM8994_VMID_NORMAL:
797 /* Startup bias, VMID ramp & buffer */
798 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
799 WM8994_BIAS_SRC |
800 WM8994_VMID_DISCH |
801 WM8994_STARTUP_BIAS_ENA |
802 WM8994_VMID_BUF_ENA |
803 WM8994_VMID_RAMP_MASK,
804 WM8994_BIAS_SRC |
805 WM8994_STARTUP_BIAS_ENA |
806 WM8994_VMID_BUF_ENA |
807 (0x3 << WM8994_VMID_RAMP_SHIFT));
Mark Brown4b7ed832011-08-10 17:47:33 +0900808
Mark Brown22f8d052012-03-19 17:32:06 +0000809 /* Main bias enable, VMID=2x40k */
810 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
811 WM8994_BIAS_ENA |
812 WM8994_VMID_SEL_MASK,
813 WM8994_BIAS_ENA | 0x2);
Mark Brown4b7ed832011-08-10 17:47:33 +0900814
Mark Brown22f8d052012-03-19 17:32:06 +0000815 msleep(50);
Mark Browncc6d5a82012-02-11 23:09:53 +0000816
Mark Brown22f8d052012-03-19 17:32:06 +0000817 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
818 WM8994_VMID_RAMP_MASK |
819 WM8994_BIAS_SRC,
820 0);
821 break;
822
823 case WM8994_VMID_FORCE:
824 /* Startup bias, slow VMID ramp & buffer */
825 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
826 WM8994_BIAS_SRC |
827 WM8994_VMID_DISCH |
828 WM8994_STARTUP_BIAS_ENA |
829 WM8994_VMID_BUF_ENA |
830 WM8994_VMID_RAMP_MASK,
831 WM8994_BIAS_SRC |
832 WM8994_STARTUP_BIAS_ENA |
833 WM8994_VMID_BUF_ENA |
834 (0x2 << WM8994_VMID_RAMP_SHIFT));
835
836 /* Main bias enable, VMID=2x40k */
837 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
838 WM8994_BIAS_ENA |
839 WM8994_VMID_SEL_MASK,
840 WM8994_BIAS_ENA | 0x2);
841
842 msleep(400);
843
844 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
845 WM8994_VMID_RAMP_MASK |
846 WM8994_BIAS_SRC,
847 0);
848 break;
849 }
Mark Brown4b7ed832011-08-10 17:47:33 +0900850 }
851}
852
853static void vmid_dereference(struct snd_soc_codec *codec)
854{
855 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
856
857 wm8994->vmid_refcount--;
858
859 dev_dbg(codec->dev, "Dereferencing VMID, refcount is now %d\n",
860 wm8994->vmid_refcount);
861
862 if (wm8994->vmid_refcount == 0) {
Mark Brown22f8d052012-03-19 17:32:06 +0000863 if (wm8994->hubs.lineout1_se)
864 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_3,
865 WM8994_LINEOUT1N_ENA |
866 WM8994_LINEOUT1P_ENA,
867 WM8994_LINEOUT1N_ENA |
868 WM8994_LINEOUT1P_ENA);
869
870 if (wm8994->hubs.lineout2_se)
871 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_3,
872 WM8994_LINEOUT2N_ENA |
873 WM8994_LINEOUT2P_ENA,
874 WM8994_LINEOUT2N_ENA |
875 WM8994_LINEOUT2P_ENA);
876
877 /* Start discharging VMID */
Mark Brown4b7ed832011-08-10 17:47:33 +0900878 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
879 WM8994_BIAS_SRC |
Mark Brown22f8d052012-03-19 17:32:06 +0000880 WM8994_VMID_DISCH,
Mark Brown4b7ed832011-08-10 17:47:33 +0900881 WM8994_BIAS_SRC |
Mark Brown22f8d052012-03-19 17:32:06 +0000882 WM8994_VMID_DISCH);
Mark Brown4b7ed832011-08-10 17:47:33 +0900883
Mark Brown22f8d052012-03-19 17:32:06 +0000884 switch (wm8994->vmid_mode) {
885 case WM8994_VMID_FORCE:
886 msleep(350);
887 break;
888 default:
889 break;
890 }
Mark Brown4b7ed832011-08-10 17:47:33 +0900891
Mark Brown22f8d052012-03-19 17:32:06 +0000892 snd_soc_update_bits(codec, WM8994_ADDITIONAL_CONTROL,
893 WM8994_VROI, WM8994_VROI);
Mark Browne85b26c2012-02-11 23:10:30 +0000894
Mark Brown22f8d052012-03-19 17:32:06 +0000895 /* Active discharge */
Mark Brown4b7ed832011-08-10 17:47:33 +0900896 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
897 WM8994_LINEOUT1_DISCH |
898 WM8994_LINEOUT2_DISCH,
899 WM8994_LINEOUT1_DISCH |
900 WM8994_LINEOUT2_DISCH);
901
Mark Brown22f8d052012-03-19 17:32:06 +0000902 msleep(150);
903
904 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_3,
905 WM8994_LINEOUT1N_ENA |
906 WM8994_LINEOUT1P_ENA |
907 WM8994_LINEOUT2N_ENA |
908 WM8994_LINEOUT2P_ENA, 0);
909
910 snd_soc_update_bits(codec, WM8994_ADDITIONAL_CONTROL,
911 WM8994_VROI, 0);
Mark Brown4b7ed832011-08-10 17:47:33 +0900912
913 /* Switch off startup biases */
914 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
915 WM8994_BIAS_SRC |
916 WM8994_STARTUP_BIAS_ENA |
917 WM8994_VMID_BUF_ENA |
918 WM8994_VMID_RAMP_MASK, 0);
Mark Brown22f8d052012-03-19 17:32:06 +0000919
920 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
921 WM8994_BIAS_ENA | WM8994_VMID_SEL_MASK, 0);
922
923 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
924 WM8994_VMID_RAMP_MASK, 0);
Mark Brown4b7ed832011-08-10 17:47:33 +0900925 }
Mark Browndb966f82012-02-06 12:07:08 +0000926
927 pm_runtime_put(codec->dev);
Mark Brown4b7ed832011-08-10 17:47:33 +0900928}
929
930static int vmid_event(struct snd_soc_dapm_widget *w,
931 struct snd_kcontrol *kcontrol, int event)
932{
933 struct snd_soc_codec *codec = w->codec;
934
935 switch (event) {
936 case SND_SOC_DAPM_PRE_PMU:
937 vmid_reference(codec);
938 break;
939
940 case SND_SOC_DAPM_POST_PMD:
941 vmid_dereference(codec);
942 break;
943 }
944
945 return 0;
946}
947
Mark Brownc3403042012-04-26 21:29:29 +0100948static bool wm8994_check_class_w_digital(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +0000949{
Mark Brown9e6e96a2010-01-29 17:47:12 +0000950 int source = 0; /* GCC flow analysis can't track enable */
951 int reg, reg_r;
952
Mark Brownc3403042012-04-26 21:29:29 +0100953 /* We also need the same AIF source for L/R and only one path */
Mark Brown9e6e96a2010-01-29 17:47:12 +0000954 reg = snd_soc_read(codec, WM8994_DAC1_LEFT_MIXER_ROUTING);
955 switch (reg) {
956 case WM8994_AIF2DACL_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +0900957 dev_vdbg(codec->dev, "Class W source AIF2DAC\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000958 source = 2 << WM8994_CP_DYN_SRC_SEL_SHIFT;
959 break;
960 case WM8994_AIF1DAC2L_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +0900961 dev_vdbg(codec->dev, "Class W source AIF1DAC2\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000962 source = 1 << WM8994_CP_DYN_SRC_SEL_SHIFT;
963 break;
964 case WM8994_AIF1DAC1L_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +0900965 dev_vdbg(codec->dev, "Class W source AIF1DAC1\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000966 source = 0 << WM8994_CP_DYN_SRC_SEL_SHIFT;
967 break;
968 default:
Mark Brownee839a22010-04-20 13:57:08 +0900969 dev_vdbg(codec->dev, "DAC mixer setting: %x\n", reg);
Mark Brownc3403042012-04-26 21:29:29 +0100970 return false;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000971 }
972
973 reg_r = snd_soc_read(codec, WM8994_DAC1_RIGHT_MIXER_ROUTING);
974 if (reg_r != reg) {
Mark Brownee839a22010-04-20 13:57:08 +0900975 dev_vdbg(codec->dev, "Left and right DAC mixers different\n");
Mark Brownc3403042012-04-26 21:29:29 +0100976 return false;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000977 }
978
Mark Brownc3403042012-04-26 21:29:29 +0100979 /* Set the source up */
980 snd_soc_update_bits(codec, WM8994_CLASS_W_1,
981 WM8994_CP_DYN_SRC_SEL_MASK, source);
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +0200982
Mark Brownc3403042012-04-26 21:29:29 +0100983 return true;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000984}
985
Dimitris Papastamos173efa02011-02-11 16:32:11 +0000986static int late_enable_ev(struct snd_soc_dapm_widget *w,
987 struct snd_kcontrol *kcontrol, int event)
988{
989 struct snd_soc_codec *codec = w->codec;
990 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
991
992 switch (event) {
993 case SND_SOC_DAPM_PRE_PMU:
Dimitris Papastamosa3cff812011-02-28 17:24:11 +0000994 if (wm8994->aif1clk_enable) {
Dimitris Papastamos173efa02011-02-11 16:32:11 +0000995 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
996 WM8994_AIF1CLK_ENA_MASK,
997 WM8994_AIF1CLK_ENA);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +0000998 wm8994->aif1clk_enable = 0;
999 }
1000 if (wm8994->aif2clk_enable) {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001001 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1002 WM8994_AIF2CLK_ENA_MASK,
1003 WM8994_AIF2CLK_ENA);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001004 wm8994->aif2clk_enable = 0;
1005 }
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001006 break;
1007 }
1008
Mark Brownc6b7b572011-03-11 18:13:12 +00001009 /* We may also have postponed startup of DSP, handle that. */
1010 wm8958_aif_ev(w, kcontrol, event);
1011
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001012 return 0;
1013}
1014
1015static int late_disable_ev(struct snd_soc_dapm_widget *w,
1016 struct snd_kcontrol *kcontrol, int event)
1017{
1018 struct snd_soc_codec *codec = w->codec;
1019 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1020
1021 switch (event) {
1022 case SND_SOC_DAPM_POST_PMD:
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001023 if (wm8994->aif1clk_disable) {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001024 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1025 WM8994_AIF1CLK_ENA_MASK, 0);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001026 wm8994->aif1clk_disable = 0;
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001027 }
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001028 if (wm8994->aif2clk_disable) {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001029 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1030 WM8994_AIF2CLK_ENA_MASK, 0);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001031 wm8994->aif2clk_disable = 0;
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001032 }
1033 break;
1034 }
1035
1036 return 0;
1037}
1038
1039static int aif1clk_ev(struct snd_soc_dapm_widget *w,
1040 struct snd_kcontrol *kcontrol, int event)
1041{
1042 struct snd_soc_codec *codec = w->codec;
1043 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1044
1045 switch (event) {
1046 case SND_SOC_DAPM_PRE_PMU:
1047 wm8994->aif1clk_enable = 1;
1048 break;
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001049 case SND_SOC_DAPM_POST_PMD:
1050 wm8994->aif1clk_disable = 1;
1051 break;
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001052 }
1053
1054 return 0;
1055}
1056
1057static int aif2clk_ev(struct snd_soc_dapm_widget *w,
1058 struct snd_kcontrol *kcontrol, int event)
1059{
1060 struct snd_soc_codec *codec = w->codec;
1061 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1062
1063 switch (event) {
1064 case SND_SOC_DAPM_PRE_PMU:
1065 wm8994->aif2clk_enable = 1;
1066 break;
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001067 case SND_SOC_DAPM_POST_PMD:
1068 wm8994->aif2clk_disable = 1;
1069 break;
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001070 }
1071
1072 return 0;
1073}
1074
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001075static int adc_mux_ev(struct snd_soc_dapm_widget *w,
1076 struct snd_kcontrol *kcontrol, int event)
1077{
1078 late_enable_ev(w, kcontrol, event);
1079 return 0;
1080}
1081
Dimitris Papastamosb462c6e2011-03-01 12:54:39 +00001082static int micbias_ev(struct snd_soc_dapm_widget *w,
1083 struct snd_kcontrol *kcontrol, int event)
1084{
1085 late_enable_ev(w, kcontrol, event);
1086 return 0;
1087}
1088
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001089static int dac_ev(struct snd_soc_dapm_widget *w,
1090 struct snd_kcontrol *kcontrol, int event)
1091{
1092 struct snd_soc_codec *codec = w->codec;
1093 unsigned int mask = 1 << w->shift;
1094
1095 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1096 mask, mask);
1097 return 0;
1098}
1099
Mark Brown9e6e96a2010-01-29 17:47:12 +00001100static const char *adc_mux_text[] = {
1101 "ADC",
1102 "DMIC",
1103};
1104
1105static const struct soc_enum adc_enum =
1106 SOC_ENUM_SINGLE(0, 0, 2, adc_mux_text);
1107
1108static const struct snd_kcontrol_new adcl_mux =
1109 SOC_DAPM_ENUM_VIRT("ADCL Mux", adc_enum);
1110
1111static const struct snd_kcontrol_new adcr_mux =
1112 SOC_DAPM_ENUM_VIRT("ADCR Mux", adc_enum);
1113
1114static const struct snd_kcontrol_new left_speaker_mixer[] = {
1115SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 9, 1, 0),
1116SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 7, 1, 0),
1117SOC_DAPM_SINGLE("IN1LP Switch", WM8994_SPEAKER_MIXER, 5, 1, 0),
1118SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 3, 1, 0),
1119SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 1, 1, 0),
1120};
1121
1122static const struct snd_kcontrol_new right_speaker_mixer[] = {
1123SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 8, 1, 0),
1124SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 6, 1, 0),
1125SOC_DAPM_SINGLE("IN1RP Switch", WM8994_SPEAKER_MIXER, 4, 1, 0),
1126SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 2, 1, 0),
1127SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 0, 1, 0),
1128};
1129
1130/* Debugging; dump chip status after DAPM transitions */
1131static int post_ev(struct snd_soc_dapm_widget *w,
1132 struct snd_kcontrol *kcontrol, int event)
1133{
1134 struct snd_soc_codec *codec = w->codec;
1135 dev_dbg(codec->dev, "SRC status: %x\n",
1136 snd_soc_read(codec,
1137 WM8994_RATE_STATUS));
1138 return 0;
1139}
1140
1141static const struct snd_kcontrol_new aif1adc1l_mix[] = {
1142SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1143 1, 1, 0),
1144SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1145 0, 1, 0),
1146};
1147
1148static const struct snd_kcontrol_new aif1adc1r_mix[] = {
1149SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1150 1, 1, 0),
1151SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1152 0, 1, 0),
1153};
1154
Mark Browna3257ba2010-07-19 14:02:34 +01001155static const struct snd_kcontrol_new aif1adc2l_mix[] = {
1156SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1157 1, 1, 0),
1158SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1159 0, 1, 0),
1160};
1161
1162static const struct snd_kcontrol_new aif1adc2r_mix[] = {
1163SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1164 1, 1, 0),
1165SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1166 0, 1, 0),
1167};
1168
Mark Brown9e6e96a2010-01-29 17:47:12 +00001169static const struct snd_kcontrol_new aif2dac2l_mix[] = {
1170SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1171 5, 1, 0),
1172SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1173 4, 1, 0),
1174SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1175 2, 1, 0),
1176SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1177 1, 1, 0),
1178SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1179 0, 1, 0),
1180};
1181
1182static const struct snd_kcontrol_new aif2dac2r_mix[] = {
1183SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1184 5, 1, 0),
1185SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1186 4, 1, 0),
1187SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1188 2, 1, 0),
1189SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1190 1, 1, 0),
1191SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1192 0, 1, 0),
1193};
1194
1195#define WM8994_CLASS_W_SWITCH(xname, reg, shift, max, invert) \
1196{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1197 .info = snd_soc_info_volsw, \
1198 .get = snd_soc_dapm_get_volsw, .put = wm8994_put_class_w, \
1199 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
1200
1201static int wm8994_put_class_w(struct snd_kcontrol *kcontrol,
1202 struct snd_ctl_elem_value *ucontrol)
1203{
Jarkko Nikula9d035452011-05-13 19:16:52 +03001204 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1205 struct snd_soc_dapm_widget *w = wlist->widgets[0];
Mark Brown9e6e96a2010-01-29 17:47:12 +00001206 struct snd_soc_codec *codec = w->codec;
1207 int ret;
1208
1209 ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
1210
Mark Brownc3403042012-04-26 21:29:29 +01001211 wm_hubs_update_class_w(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001212
1213 return ret;
1214}
1215
1216static const struct snd_kcontrol_new dac1l_mix[] = {
1217WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1218 5, 1, 0),
1219WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1220 4, 1, 0),
1221WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1222 2, 1, 0),
1223WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1224 1, 1, 0),
1225WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1226 0, 1, 0),
1227};
1228
1229static const struct snd_kcontrol_new dac1r_mix[] = {
1230WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1231 5, 1, 0),
1232WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1233 4, 1, 0),
1234WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1235 2, 1, 0),
1236WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1237 1, 1, 0),
1238WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1239 0, 1, 0),
1240};
1241
1242static const char *sidetone_text[] = {
1243 "ADC/DMIC1", "DMIC2",
1244};
1245
1246static const struct soc_enum sidetone1_enum =
1247 SOC_ENUM_SINGLE(WM8994_SIDETONE, 0, 2, sidetone_text);
1248
1249static const struct snd_kcontrol_new sidetone1_mux =
1250 SOC_DAPM_ENUM("Left Sidetone Mux", sidetone1_enum);
1251
1252static const struct soc_enum sidetone2_enum =
1253 SOC_ENUM_SINGLE(WM8994_SIDETONE, 1, 2, sidetone_text);
1254
1255static const struct snd_kcontrol_new sidetone2_mux =
1256 SOC_DAPM_ENUM("Right Sidetone Mux", sidetone2_enum);
1257
1258static const char *aif1dac_text[] = {
1259 "AIF1DACDAT", "AIF3DACDAT",
1260};
1261
1262static const struct soc_enum aif1dac_enum =
1263 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 0, 2, aif1dac_text);
1264
1265static const struct snd_kcontrol_new aif1dac_mux =
1266 SOC_DAPM_ENUM("AIF1DAC Mux", aif1dac_enum);
1267
1268static const char *aif2dac_text[] = {
1269 "AIF2DACDAT", "AIF3DACDAT",
1270};
1271
1272static const struct soc_enum aif2dac_enum =
1273 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 1, 2, aif2dac_text);
1274
1275static const struct snd_kcontrol_new aif2dac_mux =
1276 SOC_DAPM_ENUM("AIF2DAC Mux", aif2dac_enum);
1277
1278static const char *aif2adc_text[] = {
1279 "AIF2ADCDAT", "AIF3DACDAT",
1280};
1281
1282static const struct soc_enum aif2adc_enum =
1283 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 2, 2, aif2adc_text);
1284
1285static const struct snd_kcontrol_new aif2adc_mux =
1286 SOC_DAPM_ENUM("AIF2ADC Mux", aif2adc_enum);
1287
1288static const char *aif3adc_text[] = {
Mark Brownc4431df2010-11-26 15:21:07 +00001289 "AIF1ADCDAT", "AIF2ADCDAT", "AIF2DACDAT", "Mono PCM",
Mark Brown9e6e96a2010-01-29 17:47:12 +00001290};
1291
Mark Brownc4431df2010-11-26 15:21:07 +00001292static const struct soc_enum wm8994_aif3adc_enum =
Mark Brown9e6e96a2010-01-29 17:47:12 +00001293 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 3, 3, aif3adc_text);
1294
Mark Brownc4431df2010-11-26 15:21:07 +00001295static const struct snd_kcontrol_new wm8994_aif3adc_mux =
1296 SOC_DAPM_ENUM("AIF3ADC Mux", wm8994_aif3adc_enum);
1297
1298static const struct soc_enum wm8958_aif3adc_enum =
1299 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 3, 4, aif3adc_text);
1300
1301static const struct snd_kcontrol_new wm8958_aif3adc_mux =
1302 SOC_DAPM_ENUM("AIF3ADC Mux", wm8958_aif3adc_enum);
1303
1304static const char *mono_pcm_out_text[] = {
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02001305 "None", "AIF2ADCL", "AIF2ADCR",
Mark Brownc4431df2010-11-26 15:21:07 +00001306};
1307
1308static const struct soc_enum mono_pcm_out_enum =
1309 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 9, 3, mono_pcm_out_text);
1310
1311static const struct snd_kcontrol_new mono_pcm_out_mux =
1312 SOC_DAPM_ENUM("Mono PCM Out Mux", mono_pcm_out_enum);
1313
1314static const char *aif2dac_src_text[] = {
1315 "AIF2", "AIF3",
1316};
1317
1318/* Note that these two control shouldn't be simultaneously switched to AIF3 */
1319static const struct soc_enum aif2dacl_src_enum =
1320 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 7, 2, aif2dac_src_text);
1321
1322static const struct snd_kcontrol_new aif2dacl_src_mux =
1323 SOC_DAPM_ENUM("AIF2DACL Mux", aif2dacl_src_enum);
1324
1325static const struct soc_enum aif2dacr_src_enum =
1326 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 8, 2, aif2dac_src_text);
1327
1328static const struct snd_kcontrol_new aif2dacr_src_mux =
1329 SOC_DAPM_ENUM("AIF2DACR Mux", aif2dacr_src_enum);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001330
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001331static const struct snd_soc_dapm_widget wm8994_lateclk_revd_widgets[] = {
1332SND_SOC_DAPM_SUPPLY("AIF1CLK", SND_SOC_NOPM, 0, 0, aif1clk_ev,
1333 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1334SND_SOC_DAPM_SUPPLY("AIF2CLK", SND_SOC_NOPM, 0, 0, aif2clk_ev,
1335 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1336
1337SND_SOC_DAPM_PGA_E("Late DAC1L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1338 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1339SND_SOC_DAPM_PGA_E("Late DAC1R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1340 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1341SND_SOC_DAPM_PGA_E("Late DAC2L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1342 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1343SND_SOC_DAPM_PGA_E("Late DAC2R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1344 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Mark Brownb70a51b2011-06-29 00:21:09 -07001345SND_SOC_DAPM_PGA_E("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0,
1346 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1347
1348SND_SOC_DAPM_MIXER_E("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
1349 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer),
1350 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1351SND_SOC_DAPM_MIXER_E("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
1352 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer),
1353 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Mark Brownc3403042012-04-26 21:29:29 +01001354SND_SOC_DAPM_MUX_E("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpl_mux,
Mark Brownb70a51b2011-06-29 00:21:09 -07001355 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Mark Brownc3403042012-04-26 21:29:29 +01001356SND_SOC_DAPM_MUX_E("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpr_mux,
Mark Brownb70a51b2011-06-29 00:21:09 -07001357 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001358
1359SND_SOC_DAPM_POST("Late Disable PGA", late_disable_ev)
1360};
1361
1362static const struct snd_soc_dapm_widget wm8994_lateclk_widgets[] = {
1363SND_SOC_DAPM_SUPPLY("AIF1CLK", WM8994_AIF1_CLOCKING_1, 0, 0, NULL, 0),
Mark Brownb70a51b2011-06-29 00:21:09 -07001364SND_SOC_DAPM_SUPPLY("AIF2CLK", WM8994_AIF2_CLOCKING_1, 0, 0, NULL, 0),
1365SND_SOC_DAPM_PGA("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0),
1366SND_SOC_DAPM_MIXER("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
1367 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
1368SND_SOC_DAPM_MIXER("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
1369 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
Mark Brownc3403042012-04-26 21:29:29 +01001370SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpl_mux),
1371SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpr_mux),
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001372};
1373
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001374static const struct snd_soc_dapm_widget wm8994_dac_revd_widgets[] = {
1375SND_SOC_DAPM_DAC_E("DAC2L", NULL, SND_SOC_NOPM, 3, 0,
1376 dac_ev, SND_SOC_DAPM_PRE_PMU),
1377SND_SOC_DAPM_DAC_E("DAC2R", NULL, SND_SOC_NOPM, 2, 0,
1378 dac_ev, SND_SOC_DAPM_PRE_PMU),
1379SND_SOC_DAPM_DAC_E("DAC1L", NULL, SND_SOC_NOPM, 1, 0,
1380 dac_ev, SND_SOC_DAPM_PRE_PMU),
1381SND_SOC_DAPM_DAC_E("DAC1R", NULL, SND_SOC_NOPM, 0, 0,
1382 dac_ev, SND_SOC_DAPM_PRE_PMU),
1383};
1384
1385static const struct snd_soc_dapm_widget wm8994_dac_widgets[] = {
1386SND_SOC_DAPM_DAC("DAC2L", NULL, WM8994_POWER_MANAGEMENT_5, 3, 0),
Mark Brown0627bd22011-03-09 19:09:17 +00001387SND_SOC_DAPM_DAC("DAC2R", NULL, WM8994_POWER_MANAGEMENT_5, 2, 0),
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001388SND_SOC_DAPM_DAC("DAC1L", NULL, WM8994_POWER_MANAGEMENT_5, 1, 0),
1389SND_SOC_DAPM_DAC("DAC1R", NULL, WM8994_POWER_MANAGEMENT_5, 0, 0),
1390};
1391
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001392static const struct snd_soc_dapm_widget wm8994_adc_revd_widgets[] = {
Mark Brown87b86ad2011-08-14 13:39:20 +09001393SND_SOC_DAPM_VIRT_MUX_E("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux,
1394 adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
1395SND_SOC_DAPM_VIRT_MUX_E("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux,
1396 adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001397};
1398
1399static const struct snd_soc_dapm_widget wm8994_adc_widgets[] = {
Mark Brown87b86ad2011-08-14 13:39:20 +09001400SND_SOC_DAPM_VIRT_MUX("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux),
1401SND_SOC_DAPM_VIRT_MUX("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux),
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001402};
1403
Mark Brown9e6e96a2010-01-29 17:47:12 +00001404static const struct snd_soc_dapm_widget wm8994_dapm_widgets[] = {
1405SND_SOC_DAPM_INPUT("DMIC1DAT"),
1406SND_SOC_DAPM_INPUT("DMIC2DAT"),
Mark Brown66b47fd2010-07-08 11:25:43 +09001407SND_SOC_DAPM_INPUT("Clock"),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001408
Dimitris Papastamosb462c6e2011-03-01 12:54:39 +00001409SND_SOC_DAPM_SUPPLY_S("MICBIAS Supply", 1, SND_SOC_NOPM, 0, 0, micbias_ev,
1410 SND_SOC_DAPM_PRE_PMU),
Mark Brown4b7ed832011-08-10 17:47:33 +09001411SND_SOC_DAPM_SUPPLY("VMID", SND_SOC_NOPM, 0, 0, vmid_event,
1412 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
Dimitris Papastamosb462c6e2011-03-01 12:54:39 +00001413
Mark Brown9e6e96a2010-01-29 17:47:12 +00001414SND_SOC_DAPM_SUPPLY("CLK_SYS", SND_SOC_NOPM, 0, 0, clk_sys_event,
1415 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1416
1417SND_SOC_DAPM_SUPPLY("DSP1CLK", WM8994_CLOCKING_1, 3, 0, NULL, 0),
1418SND_SOC_DAPM_SUPPLY("DSP2CLK", WM8994_CLOCKING_1, 2, 0, NULL, 0),
1419SND_SOC_DAPM_SUPPLY("DSPINTCLK", WM8994_CLOCKING_1, 1, 0, NULL, 0),
1420
Mark Brown7f94de42011-02-03 16:27:34 +00001421SND_SOC_DAPM_AIF_OUT("AIF1ADC1L", NULL,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001422 0, WM8994_POWER_MANAGEMENT_4, 9, 0),
Mark Brown7f94de42011-02-03 16:27:34 +00001423SND_SOC_DAPM_AIF_OUT("AIF1ADC1R", NULL,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001424 0, WM8994_POWER_MANAGEMENT_4, 8, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001425SND_SOC_DAPM_AIF_IN_E("AIF1DAC1L", NULL, 0,
1426 WM8994_POWER_MANAGEMENT_5, 9, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001427 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brownd6addcc2010-11-26 15:21:08 +00001428SND_SOC_DAPM_AIF_IN_E("AIF1DAC1R", NULL, 0,
1429 WM8994_POWER_MANAGEMENT_5, 8, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001430 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001431
Mark Brown7f94de42011-02-03 16:27:34 +00001432SND_SOC_DAPM_AIF_OUT("AIF1ADC2L", NULL,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001433 0, WM8994_POWER_MANAGEMENT_4, 11, 0),
Mark Brown7f94de42011-02-03 16:27:34 +00001434SND_SOC_DAPM_AIF_OUT("AIF1ADC2R", NULL,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001435 0, WM8994_POWER_MANAGEMENT_4, 10, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001436SND_SOC_DAPM_AIF_IN_E("AIF1DAC2L", NULL, 0,
1437 WM8994_POWER_MANAGEMENT_5, 11, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001438 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brownd6addcc2010-11-26 15:21:08 +00001439SND_SOC_DAPM_AIF_IN_E("AIF1DAC2R", NULL, 0,
1440 WM8994_POWER_MANAGEMENT_5, 10, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001441 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001442
1443SND_SOC_DAPM_MIXER("AIF1ADC1L Mixer", SND_SOC_NOPM, 0, 0,
1444 aif1adc1l_mix, ARRAY_SIZE(aif1adc1l_mix)),
1445SND_SOC_DAPM_MIXER("AIF1ADC1R Mixer", SND_SOC_NOPM, 0, 0,
1446 aif1adc1r_mix, ARRAY_SIZE(aif1adc1r_mix)),
1447
Mark Browna3257ba2010-07-19 14:02:34 +01001448SND_SOC_DAPM_MIXER("AIF1ADC2L Mixer", SND_SOC_NOPM, 0, 0,
1449 aif1adc2l_mix, ARRAY_SIZE(aif1adc2l_mix)),
1450SND_SOC_DAPM_MIXER("AIF1ADC2R Mixer", SND_SOC_NOPM, 0, 0,
1451 aif1adc2r_mix, ARRAY_SIZE(aif1adc2r_mix)),
1452
Mark Brown9e6e96a2010-01-29 17:47:12 +00001453SND_SOC_DAPM_MIXER("AIF2DAC2L Mixer", SND_SOC_NOPM, 0, 0,
1454 aif2dac2l_mix, ARRAY_SIZE(aif2dac2l_mix)),
1455SND_SOC_DAPM_MIXER("AIF2DAC2R Mixer", SND_SOC_NOPM, 0, 0,
1456 aif2dac2r_mix, ARRAY_SIZE(aif2dac2r_mix)),
1457
1458SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &sidetone1_mux),
1459SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &sidetone2_mux),
1460
1461SND_SOC_DAPM_MIXER("DAC1L Mixer", SND_SOC_NOPM, 0, 0,
1462 dac1l_mix, ARRAY_SIZE(dac1l_mix)),
1463SND_SOC_DAPM_MIXER("DAC1R Mixer", SND_SOC_NOPM, 0, 0,
1464 dac1r_mix, ARRAY_SIZE(dac1r_mix)),
1465
1466SND_SOC_DAPM_AIF_OUT("AIF2ADCL", NULL, 0,
1467 WM8994_POWER_MANAGEMENT_4, 13, 0),
1468SND_SOC_DAPM_AIF_OUT("AIF2ADCR", NULL, 0,
1469 WM8994_POWER_MANAGEMENT_4, 12, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001470SND_SOC_DAPM_AIF_IN_E("AIF2DACL", NULL, 0,
1471 WM8994_POWER_MANAGEMENT_5, 13, 0, wm8958_aif_ev,
1472 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1473SND_SOC_DAPM_AIF_IN_E("AIF2DACR", NULL, 0,
1474 WM8994_POWER_MANAGEMENT_5, 12, 0, wm8958_aif_ev,
1475 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001476
Mark Brown5567d8c2012-02-16 21:43:29 -08001477SND_SOC_DAPM_AIF_IN("AIF1DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1478SND_SOC_DAPM_AIF_IN("AIF2DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1479SND_SOC_DAPM_AIF_OUT("AIF1ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1480SND_SOC_DAPM_AIF_OUT("AIF2ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001481
1482SND_SOC_DAPM_MUX("AIF1DAC Mux", SND_SOC_NOPM, 0, 0, &aif1dac_mux),
1483SND_SOC_DAPM_MUX("AIF2DAC Mux", SND_SOC_NOPM, 0, 0, &aif2dac_mux),
1484SND_SOC_DAPM_MUX("AIF2ADC Mux", SND_SOC_NOPM, 0, 0, &aif2adc_mux),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001485
Mark Brown5567d8c2012-02-16 21:43:29 -08001486SND_SOC_DAPM_AIF_IN("AIF3DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1487SND_SOC_DAPM_AIF_OUT("AIF3ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001488
1489SND_SOC_DAPM_SUPPLY("TOCLK", WM8994_CLOCKING_1, 4, 0, NULL, 0),
1490
1491SND_SOC_DAPM_ADC("DMIC2L", NULL, WM8994_POWER_MANAGEMENT_4, 5, 0),
1492SND_SOC_DAPM_ADC("DMIC2R", NULL, WM8994_POWER_MANAGEMENT_4, 4, 0),
1493SND_SOC_DAPM_ADC("DMIC1L", NULL, WM8994_POWER_MANAGEMENT_4, 3, 0),
1494SND_SOC_DAPM_ADC("DMIC1R", NULL, WM8994_POWER_MANAGEMENT_4, 2, 0),
1495
1496/* Power is done with the muxes since the ADC power also controls the
1497 * downsampling chain, the chip will automatically manage the analogue
1498 * specific portions.
1499 */
1500SND_SOC_DAPM_ADC("ADCL", NULL, SND_SOC_NOPM, 1, 0),
1501SND_SOC_DAPM_ADC("ADCR", NULL, SND_SOC_NOPM, 0, 0),
1502
Mark Brown9e6e96a2010-01-29 17:47:12 +00001503SND_SOC_DAPM_POST("Debug log", post_ev),
1504};
1505
Mark Brownc4431df2010-11-26 15:21:07 +00001506static const struct snd_soc_dapm_widget wm8994_specific_dapm_widgets[] = {
1507SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8994_aif3adc_mux),
1508};
Mark Brown9e6e96a2010-01-29 17:47:12 +00001509
Mark Brownc4431df2010-11-26 15:21:07 +00001510static const struct snd_soc_dapm_widget wm8958_dapm_widgets[] = {
Mark Brown8c5b8422012-04-17 20:49:05 +01001511SND_SOC_DAPM_SUPPLY("AIF3", WM8994_POWER_MANAGEMENT_6, 5, 1, NULL, 0),
Mark Brownc4431df2010-11-26 15:21:07 +00001512SND_SOC_DAPM_MUX("Mono PCM Out Mux", SND_SOC_NOPM, 0, 0, &mono_pcm_out_mux),
1513SND_SOC_DAPM_MUX("AIF2DACL Mux", SND_SOC_NOPM, 0, 0, &aif2dacl_src_mux),
1514SND_SOC_DAPM_MUX("AIF2DACR Mux", SND_SOC_NOPM, 0, 0, &aif2dacr_src_mux),
1515SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8958_aif3adc_mux),
1516};
1517
1518static const struct snd_soc_dapm_route intercon[] = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00001519 { "CLK_SYS", NULL, "AIF1CLK", check_clk_sys },
1520 { "CLK_SYS", NULL, "AIF2CLK", check_clk_sys },
1521
1522 { "DSP1CLK", NULL, "CLK_SYS" },
1523 { "DSP2CLK", NULL, "CLK_SYS" },
1524 { "DSPINTCLK", NULL, "CLK_SYS" },
1525
1526 { "AIF1ADC1L", NULL, "AIF1CLK" },
1527 { "AIF1ADC1L", NULL, "DSP1CLK" },
1528 { "AIF1ADC1R", NULL, "AIF1CLK" },
1529 { "AIF1ADC1R", NULL, "DSP1CLK" },
1530 { "AIF1ADC1R", NULL, "DSPINTCLK" },
1531
1532 { "AIF1DAC1L", NULL, "AIF1CLK" },
1533 { "AIF1DAC1L", NULL, "DSP1CLK" },
1534 { "AIF1DAC1R", NULL, "AIF1CLK" },
1535 { "AIF1DAC1R", NULL, "DSP1CLK" },
1536 { "AIF1DAC1R", NULL, "DSPINTCLK" },
1537
1538 { "AIF1ADC2L", NULL, "AIF1CLK" },
1539 { "AIF1ADC2L", NULL, "DSP1CLK" },
1540 { "AIF1ADC2R", NULL, "AIF1CLK" },
1541 { "AIF1ADC2R", NULL, "DSP1CLK" },
1542 { "AIF1ADC2R", NULL, "DSPINTCLK" },
1543
1544 { "AIF1DAC2L", NULL, "AIF1CLK" },
1545 { "AIF1DAC2L", NULL, "DSP1CLK" },
1546 { "AIF1DAC2R", NULL, "AIF1CLK" },
1547 { "AIF1DAC2R", NULL, "DSP1CLK" },
1548 { "AIF1DAC2R", NULL, "DSPINTCLK" },
1549
1550 { "AIF2ADCL", NULL, "AIF2CLK" },
1551 { "AIF2ADCL", NULL, "DSP2CLK" },
1552 { "AIF2ADCR", NULL, "AIF2CLK" },
1553 { "AIF2ADCR", NULL, "DSP2CLK" },
1554 { "AIF2ADCR", NULL, "DSPINTCLK" },
1555
1556 { "AIF2DACL", NULL, "AIF2CLK" },
1557 { "AIF2DACL", NULL, "DSP2CLK" },
1558 { "AIF2DACR", NULL, "AIF2CLK" },
1559 { "AIF2DACR", NULL, "DSP2CLK" },
1560 { "AIF2DACR", NULL, "DSPINTCLK" },
1561
1562 { "DMIC1L", NULL, "DMIC1DAT" },
1563 { "DMIC1L", NULL, "CLK_SYS" },
1564 { "DMIC1R", NULL, "DMIC1DAT" },
1565 { "DMIC1R", NULL, "CLK_SYS" },
1566 { "DMIC2L", NULL, "DMIC2DAT" },
1567 { "DMIC2L", NULL, "CLK_SYS" },
1568 { "DMIC2R", NULL, "DMIC2DAT" },
1569 { "DMIC2R", NULL, "CLK_SYS" },
1570
1571 { "ADCL", NULL, "AIF1CLK" },
1572 { "ADCL", NULL, "DSP1CLK" },
1573 { "ADCL", NULL, "DSPINTCLK" },
1574
1575 { "ADCR", NULL, "AIF1CLK" },
1576 { "ADCR", NULL, "DSP1CLK" },
1577 { "ADCR", NULL, "DSPINTCLK" },
1578
1579 { "ADCL Mux", "ADC", "ADCL" },
1580 { "ADCL Mux", "DMIC", "DMIC1L" },
1581 { "ADCR Mux", "ADC", "ADCR" },
1582 { "ADCR Mux", "DMIC", "DMIC1R" },
1583
1584 { "DAC1L", NULL, "AIF1CLK" },
1585 { "DAC1L", NULL, "DSP1CLK" },
1586 { "DAC1L", NULL, "DSPINTCLK" },
1587
1588 { "DAC1R", NULL, "AIF1CLK" },
1589 { "DAC1R", NULL, "DSP1CLK" },
1590 { "DAC1R", NULL, "DSPINTCLK" },
1591
1592 { "DAC2L", NULL, "AIF2CLK" },
1593 { "DAC2L", NULL, "DSP2CLK" },
1594 { "DAC2L", NULL, "DSPINTCLK" },
1595
1596 { "DAC2R", NULL, "AIF2DACR" },
1597 { "DAC2R", NULL, "AIF2CLK" },
1598 { "DAC2R", NULL, "DSP2CLK" },
1599 { "DAC2R", NULL, "DSPINTCLK" },
1600
1601 { "TOCLK", NULL, "CLK_SYS" },
1602
Mark Brown5567d8c2012-02-16 21:43:29 -08001603 { "AIF1DACDAT", NULL, "AIF1 Playback" },
1604 { "AIF2DACDAT", NULL, "AIF2 Playback" },
1605 { "AIF3DACDAT", NULL, "AIF3 Playback" },
1606
1607 { "AIF1 Capture", NULL, "AIF1ADCDAT" },
1608 { "AIF2 Capture", NULL, "AIF2ADCDAT" },
1609 { "AIF3 Capture", NULL, "AIF3ADCDAT" },
1610
Mark Brown9e6e96a2010-01-29 17:47:12 +00001611 /* AIF1 outputs */
1612 { "AIF1ADC1L", NULL, "AIF1ADC1L Mixer" },
1613 { "AIF1ADC1L Mixer", "ADC/DMIC Switch", "ADCL Mux" },
1614 { "AIF1ADC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1615
1616 { "AIF1ADC1R", NULL, "AIF1ADC1R Mixer" },
1617 { "AIF1ADC1R Mixer", "ADC/DMIC Switch", "ADCR Mux" },
1618 { "AIF1ADC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1619
Mark Browna3257ba2010-07-19 14:02:34 +01001620 { "AIF1ADC2L", NULL, "AIF1ADC2L Mixer" },
1621 { "AIF1ADC2L Mixer", "DMIC Switch", "DMIC2L" },
1622 { "AIF1ADC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1623
1624 { "AIF1ADC2R", NULL, "AIF1ADC2R Mixer" },
1625 { "AIF1ADC2R Mixer", "DMIC Switch", "DMIC2R" },
1626 { "AIF1ADC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1627
Mark Brown9e6e96a2010-01-29 17:47:12 +00001628 /* Pin level routing for AIF3 */
1629 { "AIF1DAC1L", NULL, "AIF1DAC Mux" },
1630 { "AIF1DAC1R", NULL, "AIF1DAC Mux" },
1631 { "AIF1DAC2L", NULL, "AIF1DAC Mux" },
1632 { "AIF1DAC2R", NULL, "AIF1DAC Mux" },
1633
Mark Brown9e6e96a2010-01-29 17:47:12 +00001634 { "AIF1DAC Mux", "AIF1DACDAT", "AIF1DACDAT" },
1635 { "AIF1DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1636 { "AIF2DAC Mux", "AIF2DACDAT", "AIF2DACDAT" },
1637 { "AIF2DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1638 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCL" },
1639 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCR" },
1640 { "AIF2ADC Mux", "AIF3DACDAT", "AIF3ADCDAT" },
1641
1642 /* DAC1 inputs */
Mark Brown9e6e96a2010-01-29 17:47:12 +00001643 { "DAC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1644 { "DAC1L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1645 { "DAC1L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1646 { "DAC1L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1647 { "DAC1L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1648
Mark Brown9e6e96a2010-01-29 17:47:12 +00001649 { "DAC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1650 { "DAC1R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1651 { "DAC1R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1652 { "DAC1R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1653 { "DAC1R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1654
1655 /* DAC2/AIF2 outputs */
1656 { "AIF2ADCL", NULL, "AIF2DAC2L Mixer" },
Mark Brown9e6e96a2010-01-29 17:47:12 +00001657 { "AIF2DAC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1658 { "AIF2DAC2L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1659 { "AIF2DAC2L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1660 { "AIF2DAC2L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1661 { "AIF2DAC2L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1662
1663 { "AIF2ADCR", NULL, "AIF2DAC2R Mixer" },
Mark Brown9e6e96a2010-01-29 17:47:12 +00001664 { "AIF2DAC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1665 { "AIF2DAC2R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1666 { "AIF2DAC2R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1667 { "AIF2DAC2R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1668 { "AIF2DAC2R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1669
Mark Brown7f94de42011-02-03 16:27:34 +00001670 { "AIF1ADCDAT", NULL, "AIF1ADC1L" },
1671 { "AIF1ADCDAT", NULL, "AIF1ADC1R" },
1672 { "AIF1ADCDAT", NULL, "AIF1ADC2L" },
1673 { "AIF1ADCDAT", NULL, "AIF1ADC2R" },
1674
Mark Brown9e6e96a2010-01-29 17:47:12 +00001675 { "AIF2ADCDAT", NULL, "AIF2ADC Mux" },
1676
1677 /* AIF3 output */
1678 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1L" },
1679 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1R" },
1680 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2L" },
1681 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2R" },
1682 { "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCL" },
1683 { "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCR" },
1684 { "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACL" },
1685 { "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACR" },
1686
1687 /* Sidetone */
1688 { "Left Sidetone", "ADC/DMIC1", "ADCL Mux" },
1689 { "Left Sidetone", "DMIC2", "DMIC2L" },
1690 { "Right Sidetone", "ADC/DMIC1", "ADCR Mux" },
1691 { "Right Sidetone", "DMIC2", "DMIC2R" },
1692
1693 /* Output stages */
1694 { "Left Output Mixer", "DAC Switch", "DAC1L" },
1695 { "Right Output Mixer", "DAC Switch", "DAC1R" },
1696
1697 { "SPKL", "DAC1 Switch", "DAC1L" },
1698 { "SPKL", "DAC2 Switch", "DAC2L" },
1699
1700 { "SPKR", "DAC1 Switch", "DAC1R" },
1701 { "SPKR", "DAC2 Switch", "DAC2R" },
1702
1703 { "Left Headphone Mux", "DAC", "DAC1L" },
1704 { "Right Headphone Mux", "DAC", "DAC1R" },
1705};
1706
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001707static const struct snd_soc_dapm_route wm8994_lateclk_revd_intercon[] = {
1708 { "DAC1L", NULL, "Late DAC1L Enable PGA" },
1709 { "Late DAC1L Enable PGA", NULL, "DAC1L Mixer" },
1710 { "DAC1R", NULL, "Late DAC1R Enable PGA" },
1711 { "Late DAC1R Enable PGA", NULL, "DAC1R Mixer" },
1712 { "DAC2L", NULL, "Late DAC2L Enable PGA" },
1713 { "Late DAC2L Enable PGA", NULL, "AIF2DAC2L Mixer" },
1714 { "DAC2R", NULL, "Late DAC2R Enable PGA" },
1715 { "Late DAC2R Enable PGA", NULL, "AIF2DAC2R Mixer" }
1716};
1717
1718static const struct snd_soc_dapm_route wm8994_lateclk_intercon[] = {
1719 { "DAC1L", NULL, "DAC1L Mixer" },
1720 { "DAC1R", NULL, "DAC1R Mixer" },
1721 { "DAC2L", NULL, "AIF2DAC2L Mixer" },
1722 { "DAC2R", NULL, "AIF2DAC2R Mixer" },
1723};
1724
Mark Brown6ed8f142011-02-03 16:27:35 +00001725static const struct snd_soc_dapm_route wm8994_revd_intercon[] = {
1726 { "AIF1DACDAT", NULL, "AIF2DACDAT" },
1727 { "AIF2DACDAT", NULL, "AIF1DACDAT" },
1728 { "AIF1ADCDAT", NULL, "AIF2ADCDAT" },
1729 { "AIF2ADCDAT", NULL, "AIF1ADCDAT" },
Mark Brownb793eb62011-07-14 18:21:37 +09001730 { "MICBIAS1", NULL, "CLK_SYS" },
1731 { "MICBIAS1", NULL, "MICBIAS Supply" },
1732 { "MICBIAS2", NULL, "CLK_SYS" },
1733 { "MICBIAS2", NULL, "MICBIAS Supply" },
Mark Brown6ed8f142011-02-03 16:27:35 +00001734};
1735
Mark Brownc4431df2010-11-26 15:21:07 +00001736static const struct snd_soc_dapm_route wm8994_intercon[] = {
1737 { "AIF2DACL", NULL, "AIF2DAC Mux" },
1738 { "AIF2DACR", NULL, "AIF2DAC Mux" },
Mark Brown4e04ada2011-07-15 15:12:31 +09001739 { "MICBIAS1", NULL, "VMID" },
1740 { "MICBIAS2", NULL, "VMID" },
Mark Brownc4431df2010-11-26 15:21:07 +00001741};
1742
1743static const struct snd_soc_dapm_route wm8958_intercon[] = {
1744 { "AIF2DACL", NULL, "AIF2DACL Mux" },
1745 { "AIF2DACR", NULL, "AIF2DACR Mux" },
1746
1747 { "AIF2DACL Mux", "AIF2", "AIF2DAC Mux" },
1748 { "AIF2DACL Mux", "AIF3", "AIF3DACDAT" },
1749 { "AIF2DACR Mux", "AIF2", "AIF2DAC Mux" },
1750 { "AIF2DACR Mux", "AIF3", "AIF3DACDAT" },
1751
Mark Brown8c5b8422012-04-17 20:49:05 +01001752 { "AIF3DACDAT", NULL, "AIF3" },
1753 { "AIF3ADCDAT", NULL, "AIF3" },
1754
Mark Brownc4431df2010-11-26 15:21:07 +00001755 { "Mono PCM Out Mux", "AIF2ADCL", "AIF2ADCL" },
1756 { "Mono PCM Out Mux", "AIF2ADCR", "AIF2ADCR" },
1757
1758 { "AIF3ADC Mux", "Mono PCM", "Mono PCM Out Mux" },
1759};
1760
Mark Brown9e6e96a2010-01-29 17:47:12 +00001761/* The size in bits of the FLL divide multiplied by 10
1762 * to allow rounding later */
1763#define FIXED_FLL_SIZE ((1 << 16) * 10)
1764
1765struct fll_div {
1766 u16 outdiv;
1767 u16 n;
1768 u16 k;
1769 u16 clk_ref_div;
1770 u16 fll_fratio;
1771};
1772
1773static int wm8994_get_fll_config(struct fll_div *fll,
1774 int freq_in, int freq_out)
1775{
1776 u64 Kpart;
1777 unsigned int K, Ndiv, Nmod;
1778
1779 pr_debug("FLL input=%dHz, output=%dHz\n", freq_in, freq_out);
1780
1781 /* Scale the input frequency down to <= 13.5MHz */
1782 fll->clk_ref_div = 0;
1783 while (freq_in > 13500000) {
1784 fll->clk_ref_div++;
1785 freq_in /= 2;
1786
1787 if (fll->clk_ref_div > 3)
1788 return -EINVAL;
1789 }
1790 pr_debug("CLK_REF_DIV=%d, Fref=%dHz\n", fll->clk_ref_div, freq_in);
1791
1792 /* Scale the output to give 90MHz<=Fvco<=100MHz */
1793 fll->outdiv = 3;
1794 while (freq_out * (fll->outdiv + 1) < 90000000) {
1795 fll->outdiv++;
1796 if (fll->outdiv > 63)
1797 return -EINVAL;
1798 }
1799 freq_out *= fll->outdiv + 1;
1800 pr_debug("OUTDIV=%d, Fvco=%dHz\n", fll->outdiv, freq_out);
1801
1802 if (freq_in > 1000000) {
1803 fll->fll_fratio = 0;
Mark Brown7d48a6a2010-04-20 13:36:11 +09001804 } else if (freq_in > 256000) {
1805 fll->fll_fratio = 1;
1806 freq_in *= 2;
1807 } else if (freq_in > 128000) {
1808 fll->fll_fratio = 2;
1809 freq_in *= 4;
1810 } else if (freq_in > 64000) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00001811 fll->fll_fratio = 3;
1812 freq_in *= 8;
Mark Brown7d48a6a2010-04-20 13:36:11 +09001813 } else {
1814 fll->fll_fratio = 4;
1815 freq_in *= 16;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001816 }
1817 pr_debug("FLL_FRATIO=%d, Fref=%dHz\n", fll->fll_fratio, freq_in);
1818
1819 /* Now, calculate N.K */
1820 Ndiv = freq_out / freq_in;
1821
1822 fll->n = Ndiv;
1823 Nmod = freq_out % freq_in;
1824 pr_debug("Nmod=%d\n", Nmod);
1825
1826 /* Calculate fractional part - scale up so we can round. */
1827 Kpart = FIXED_FLL_SIZE * (long long)Nmod;
1828
1829 do_div(Kpart, freq_in);
1830
1831 K = Kpart & 0xFFFFFFFF;
1832
1833 if ((K % 10) >= 5)
1834 K += 5;
1835
1836 /* Move down to proper range now rounding is done */
1837 fll->k = K / 10;
1838
1839 pr_debug("N=%x K=%x\n", fll->n, fll->k);
1840
1841 return 0;
1842}
1843
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001844static int _wm8994_set_fll(struct snd_soc_codec *codec, int id, int src,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001845 unsigned int freq_in, unsigned int freq_out)
1846{
Mark Brownb2c812e2010-04-14 15:35:19 +09001847 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01001848 struct wm8994 *control = wm8994->wm8994;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001849 int reg_offset, ret;
1850 struct fll_div fll;
Mark Browne413ba82012-03-29 14:49:27 +01001851 u16 reg, clk1, aif_reg, aif_src;
Mark Brownc7ebf932011-07-12 19:47:59 +09001852 unsigned long timeout;
Mark Brown4b7ed832011-08-10 17:47:33 +09001853 bool was_enabled;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001854
Mark Brown9e6e96a2010-01-29 17:47:12 +00001855 switch (id) {
1856 case WM8994_FLL1:
1857 reg_offset = 0;
1858 id = 0;
Mark Browne413ba82012-03-29 14:49:27 +01001859 aif_src = 0x10;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001860 break;
1861 case WM8994_FLL2:
1862 reg_offset = 0x20;
1863 id = 1;
Mark Browne413ba82012-03-29 14:49:27 +01001864 aif_src = 0x18;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001865 break;
1866 default:
1867 return -EINVAL;
1868 }
1869
Mark Brown4b7ed832011-08-10 17:47:33 +09001870 reg = snd_soc_read(codec, WM8994_FLL1_CONTROL_1 + reg_offset);
1871 was_enabled = reg & WM8994_FLL1_ENA;
1872
Mark Brown136ff2a2010-04-20 12:56:18 +09001873 switch (src) {
Mark Brown7add84a2010-04-22 02:29:01 +09001874 case 0:
1875 /* Allow no source specification when stopping */
1876 if (freq_out)
1877 return -EINVAL;
Mark Brown4514e892010-12-03 16:02:10 +00001878 src = wm8994->fll[id].src;
Mark Brown7add84a2010-04-22 02:29:01 +09001879 break;
Mark Brown136ff2a2010-04-20 12:56:18 +09001880 case WM8994_FLL_SRC_MCLK1:
1881 case WM8994_FLL_SRC_MCLK2:
1882 case WM8994_FLL_SRC_LRCLK:
1883 case WM8994_FLL_SRC_BCLK:
1884 break;
1885 default:
1886 return -EINVAL;
1887 }
1888
Mark Brown9e6e96a2010-01-29 17:47:12 +00001889 /* Are we changing anything? */
1890 if (wm8994->fll[id].src == src &&
1891 wm8994->fll[id].in == freq_in && wm8994->fll[id].out == freq_out)
1892 return 0;
1893
1894 /* If we're stopping the FLL redo the old config - no
1895 * registers will actually be written but we avoid GCC flow
1896 * analysis bugs spewing warnings.
1897 */
1898 if (freq_out)
1899 ret = wm8994_get_fll_config(&fll, freq_in, freq_out);
1900 else
1901 ret = wm8994_get_fll_config(&fll, wm8994->fll[id].in,
1902 wm8994->fll[id].out);
1903 if (ret < 0)
1904 return ret;
1905
Mark Browne413ba82012-03-29 14:49:27 +01001906 /* Make sure that we're not providing SYSCLK right now */
1907 clk1 = snd_soc_read(codec, WM8994_CLOCKING_1);
1908 if (clk1 & WM8994_SYSCLK_SRC)
1909 aif_reg = WM8994_AIF2_CLOCKING_1;
1910 else
1911 aif_reg = WM8994_AIF1_CLOCKING_1;
1912 reg = snd_soc_read(codec, aif_reg);
1913
1914 if ((reg & WM8994_AIF1CLK_ENA) &&
1915 (reg & WM8994_AIF1CLK_SRC_MASK) == aif_src) {
1916 dev_err(codec->dev, "FLL%d is currently providing SYSCLK\n",
1917 id + 1);
1918 return -EBUSY;
1919 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00001920
1921 /* We always need to disable the FLL while reconfiguring */
1922 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
1923 WM8994_FLL1_ENA, 0);
1924
Mark Brown20dc24a2012-04-05 12:55:20 +01001925 if (wm8994->fll_byp && src == WM8994_FLL_SRC_BCLK &&
Kyung-Kwee Ryue05854d2012-04-24 18:01:48 +01001926 freq_in == freq_out && freq_out) {
Mark Brown20dc24a2012-04-05 12:55:20 +01001927 dev_dbg(codec->dev, "Bypassing FLL%d\n", id + 1);
1928 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_5 + reg_offset,
1929 WM8958_FLL1_BYP, WM8958_FLL1_BYP);
1930 goto out;
1931 }
1932
Mark Brown9e6e96a2010-01-29 17:47:12 +00001933 reg = (fll.outdiv << WM8994_FLL1_OUTDIV_SHIFT) |
1934 (fll.fll_fratio << WM8994_FLL1_FRATIO_SHIFT);
1935 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_2 + reg_offset,
1936 WM8994_FLL1_OUTDIV_MASK |
1937 WM8994_FLL1_FRATIO_MASK, reg);
1938
Mark Brownb16db742012-03-03 15:33:23 +00001939 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_3 + reg_offset,
1940 WM8994_FLL1_K_MASK, fll.k);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001941
1942 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_4 + reg_offset,
1943 WM8994_FLL1_N_MASK,
1944 fll.n << WM8994_FLL1_N_SHIFT);
1945
1946 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_5 + reg_offset,
Mark Brown20dc24a2012-04-05 12:55:20 +01001947 WM8958_FLL1_BYP |
Mark Brown136ff2a2010-04-20 12:56:18 +09001948 WM8994_FLL1_REFCLK_DIV_MASK |
1949 WM8994_FLL1_REFCLK_SRC_MASK,
1950 (fll.clk_ref_div << WM8994_FLL1_REFCLK_DIV_SHIFT) |
1951 (src - 1));
Mark Brown9e6e96a2010-01-29 17:47:12 +00001952
Mark Brownf0f50392011-07-16 03:12:18 +09001953 /* Clear any pending completion from a previous failure */
1954 try_wait_for_completion(&wm8994->fll_locked[id]);
1955
Mark Brown9e6e96a2010-01-29 17:47:12 +00001956 /* Enable (with fractional mode if required) */
1957 if (freq_out) {
Mark Brown4b7ed832011-08-10 17:47:33 +09001958 /* Enable VMID if we need it */
1959 if (!was_enabled) {
Mark Brownaf6b6fe2011-11-30 20:32:05 +00001960 active_reference(codec);
1961
Mark Brown4b7ed832011-08-10 17:47:33 +09001962 switch (control->type) {
1963 case WM8994:
1964 vmid_reference(codec);
1965 break;
1966 case WM8958:
1967 if (wm8994->revision < 1)
1968 vmid_reference(codec);
1969 break;
1970 default:
1971 break;
1972 }
1973 }
1974
Mark Brown9e6e96a2010-01-29 17:47:12 +00001975 if (fll.k)
1976 reg = WM8994_FLL1_ENA | WM8994_FLL1_FRAC;
1977 else
1978 reg = WM8994_FLL1_ENA;
1979 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
1980 WM8994_FLL1_ENA | WM8994_FLL1_FRAC,
1981 reg);
Mark Brown8e9ddf82011-07-01 17:24:46 -07001982
Mark Brownc7ebf932011-07-12 19:47:59 +09001983 if (wm8994->fll_locked_irq) {
1984 timeout = wait_for_completion_timeout(&wm8994->fll_locked[id],
1985 msecs_to_jiffies(10));
1986 if (timeout == 0)
1987 dev_warn(codec->dev,
1988 "Timed out waiting for FLL lock\n");
1989 } else {
1990 msleep(5);
1991 }
Mark Brown4b7ed832011-08-10 17:47:33 +09001992 } else {
1993 if (was_enabled) {
1994 switch (control->type) {
1995 case WM8994:
1996 vmid_dereference(codec);
1997 break;
1998 case WM8958:
1999 if (wm8994->revision < 1)
2000 vmid_dereference(codec);
2001 break;
2002 default:
2003 break;
2004 }
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002005
2006 active_dereference(codec);
Mark Brown4b7ed832011-08-10 17:47:33 +09002007 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002008 }
2009
Mark Brown20dc24a2012-04-05 12:55:20 +01002010out:
Mark Brown9e6e96a2010-01-29 17:47:12 +00002011 wm8994->fll[id].in = freq_in;
2012 wm8994->fll[id].out = freq_out;
Mark Brown136ff2a2010-04-20 12:56:18 +09002013 wm8994->fll[id].src = src;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002014
Mark Brown9e6e96a2010-01-29 17:47:12 +00002015 configure_clock(codec);
2016
2017 return 0;
2018}
2019
Mark Brownc7ebf932011-07-12 19:47:59 +09002020static irqreturn_t wm8994_fll_locked_irq(int irq, void *data)
2021{
2022 struct completion *completion = data;
2023
2024 complete(completion);
2025
2026 return IRQ_HANDLED;
2027}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002028
Mark Brown66b47fd2010-07-08 11:25:43 +09002029static int opclk_divs[] = { 10, 20, 30, 40, 55, 60, 80, 120, 160 };
2030
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002031static int wm8994_set_fll(struct snd_soc_dai *dai, int id, int src,
2032 unsigned int freq_in, unsigned int freq_out)
2033{
2034 return _wm8994_set_fll(dai->codec, id, src, freq_in, freq_out);
2035}
2036
Mark Brown9e6e96a2010-01-29 17:47:12 +00002037static int wm8994_set_dai_sysclk(struct snd_soc_dai *dai,
2038 int clk_id, unsigned int freq, int dir)
2039{
2040 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09002041 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown66b47fd2010-07-08 11:25:43 +09002042 int i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002043
2044 switch (dai->id) {
2045 case 1:
2046 case 2:
2047 break;
2048
2049 default:
2050 /* AIF3 shares clocking with AIF1/2 */
2051 return -EINVAL;
2052 }
2053
2054 switch (clk_id) {
2055 case WM8994_SYSCLK_MCLK1:
2056 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK1;
2057 wm8994->mclk[0] = freq;
2058 dev_dbg(dai->dev, "AIF%d using MCLK1 at %uHz\n",
2059 dai->id, freq);
2060 break;
2061
2062 case WM8994_SYSCLK_MCLK2:
2063 /* TODO: Set GPIO AF */
2064 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK2;
2065 wm8994->mclk[1] = freq;
2066 dev_dbg(dai->dev, "AIF%d using MCLK2 at %uHz\n",
2067 dai->id, freq);
2068 break;
2069
2070 case WM8994_SYSCLK_FLL1:
2071 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL1;
2072 dev_dbg(dai->dev, "AIF%d using FLL1\n", dai->id);
2073 break;
2074
2075 case WM8994_SYSCLK_FLL2:
2076 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL2;
2077 dev_dbg(dai->dev, "AIF%d using FLL2\n", dai->id);
2078 break;
2079
Mark Brown66b47fd2010-07-08 11:25:43 +09002080 case WM8994_SYSCLK_OPCLK:
2081 /* Special case - a division (times 10) is given and
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02002082 * no effect on main clocking.
Mark Brown66b47fd2010-07-08 11:25:43 +09002083 */
2084 if (freq) {
2085 for (i = 0; i < ARRAY_SIZE(opclk_divs); i++)
2086 if (opclk_divs[i] == freq)
2087 break;
2088 if (i == ARRAY_SIZE(opclk_divs))
2089 return -EINVAL;
2090 snd_soc_update_bits(codec, WM8994_CLOCKING_2,
2091 WM8994_OPCLK_DIV_MASK, i);
2092 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
2093 WM8994_OPCLK_ENA, WM8994_OPCLK_ENA);
2094 } else {
2095 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
2096 WM8994_OPCLK_ENA, 0);
2097 }
2098
Mark Brown9e6e96a2010-01-29 17:47:12 +00002099 default:
2100 return -EINVAL;
2101 }
2102
2103 configure_clock(codec);
2104
2105 return 0;
2106}
2107
2108static int wm8994_set_bias_level(struct snd_soc_codec *codec,
2109 enum snd_soc_bias_level level)
2110{
Mark Brownb6b05692010-08-13 12:58:20 +01002111 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01002112 struct wm8994 *control = wm8994->wm8994;
Mark Brownb6b05692010-08-13 12:58:20 +01002113
Mark Brown5f2f3892012-02-08 18:51:42 +00002114 wm_hubs_set_bias_level(codec, level);
2115
Mark Brown9e6e96a2010-01-29 17:47:12 +00002116 switch (level) {
2117 case SND_SOC_BIAS_ON:
2118 break;
2119
2120 case SND_SOC_BIAS_PREPARE:
Mark Brown500fa302011-11-29 19:58:19 +00002121 /* MICBIAS into regulating mode */
2122 switch (control->type) {
2123 case WM8958:
2124 case WM1811:
2125 snd_soc_update_bits(codec, WM8958_MICBIAS1,
2126 WM8958_MICB1_MODE, 0);
2127 snd_soc_update_bits(codec, WM8958_MICBIAS2,
2128 WM8958_MICB2_MODE, 0);
2129 break;
2130 default:
2131 break;
2132 }
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002133
2134 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
2135 active_reference(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002136 break;
2137
2138 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002139 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
Mark Brown8bc3c2c2010-11-30 14:56:18 +00002140 switch (control->type) {
Mark Brown8bc3c2c2010-11-30 14:56:18 +00002141 case WM8958:
2142 if (wm8994->revision == 0) {
2143 /* Optimise performance for rev A */
Mark Brown8bc3c2c2010-11-30 14:56:18 +00002144 snd_soc_update_bits(codec,
2145 WM8958_CHARGE_PUMP_2,
2146 WM8958_CP_DISCH,
2147 WM8958_CP_DISCH);
2148 }
2149 break;
Mark Brown81204c82011-05-24 17:35:53 +08002150
Mark Brown462835e2012-01-21 12:11:53 +00002151 default:
Mark Brown81204c82011-05-24 17:35:53 +08002152 break;
Mark Brownb6b05692010-08-13 12:58:20 +01002153 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002154
2155 /* Discharge LINEOUT1 & 2 */
2156 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
2157 WM8994_LINEOUT1_DISCH |
2158 WM8994_LINEOUT2_DISCH,
2159 WM8994_LINEOUT1_DISCH |
2160 WM8994_LINEOUT2_DISCH);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002161 }
2162
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002163 if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE)
2164 active_dereference(codec);
2165
Mark Brown500fa302011-11-29 19:58:19 +00002166 /* MICBIAS into bypass mode on newer devices */
2167 switch (control->type) {
2168 case WM8958:
2169 case WM1811:
2170 snd_soc_update_bits(codec, WM8958_MICBIAS1,
2171 WM8958_MICB1_MODE,
2172 WM8958_MICB1_MODE);
2173 snd_soc_update_bits(codec, WM8958_MICBIAS2,
2174 WM8958_MICB2_MODE,
2175 WM8958_MICB2_MODE);
2176 break;
2177 default:
2178 break;
2179 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002180 break;
2181
2182 case SND_SOC_BIAS_OFF:
Mark Brown4105ab82011-12-05 15:17:36 +00002183 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
Mark Brownfbbf5922011-03-11 18:09:04 +00002184 wm8994->cur_fw = NULL;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002185 break;
2186 }
Mark Brown5f2f3892012-02-08 18:51:42 +00002187
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002188 codec->dapm.bias_level = level;
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002189
Mark Brown9e6e96a2010-01-29 17:47:12 +00002190 return 0;
2191}
2192
Mark Brown22f8d052012-03-19 17:32:06 +00002193int wm8994_vmid_mode(struct snd_soc_codec *codec, enum wm8994_vmid_mode mode)
2194{
2195 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2196
2197 switch (mode) {
2198 case WM8994_VMID_NORMAL:
2199 if (wm8994->hubs.lineout1_se) {
2200 snd_soc_dapm_disable_pin(&codec->dapm,
2201 "LINEOUT1N Driver");
2202 snd_soc_dapm_disable_pin(&codec->dapm,
2203 "LINEOUT1P Driver");
2204 }
2205 if (wm8994->hubs.lineout2_se) {
2206 snd_soc_dapm_disable_pin(&codec->dapm,
2207 "LINEOUT2N Driver");
2208 snd_soc_dapm_disable_pin(&codec->dapm,
2209 "LINEOUT2P Driver");
2210 }
2211
2212 /* Do the sync with the old mode to allow it to clean up */
2213 snd_soc_dapm_sync(&codec->dapm);
2214 wm8994->vmid_mode = mode;
2215 break;
2216
2217 case WM8994_VMID_FORCE:
2218 if (wm8994->hubs.lineout1_se) {
2219 snd_soc_dapm_force_enable_pin(&codec->dapm,
2220 "LINEOUT1N Driver");
2221 snd_soc_dapm_force_enable_pin(&codec->dapm,
2222 "LINEOUT1P Driver");
2223 }
2224 if (wm8994->hubs.lineout2_se) {
2225 snd_soc_dapm_force_enable_pin(&codec->dapm,
2226 "LINEOUT2N Driver");
2227 snd_soc_dapm_force_enable_pin(&codec->dapm,
2228 "LINEOUT2P Driver");
2229 }
2230
2231 wm8994->vmid_mode = mode;
2232 snd_soc_dapm_sync(&codec->dapm);
2233 break;
2234
2235 default:
2236 return -EINVAL;
2237 }
2238
2239 return 0;
2240}
2241
Mark Brown9e6e96a2010-01-29 17:47:12 +00002242static int wm8994_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2243{
2244 struct snd_soc_codec *codec = dai->codec;
Mark Brown2a8a8562011-07-24 12:20:41 +01002245 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2246 struct wm8994 *control = wm8994->wm8994;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002247 int ms_reg;
2248 int aif1_reg;
2249 int ms = 0;
2250 int aif1 = 0;
2251
2252 switch (dai->id) {
2253 case 1:
2254 ms_reg = WM8994_AIF1_MASTER_SLAVE;
2255 aif1_reg = WM8994_AIF1_CONTROL_1;
2256 break;
2257 case 2:
2258 ms_reg = WM8994_AIF2_MASTER_SLAVE;
2259 aif1_reg = WM8994_AIF2_CONTROL_1;
2260 break;
2261 default:
2262 return -EINVAL;
2263 }
2264
2265 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
2266 case SND_SOC_DAIFMT_CBS_CFS:
2267 break;
2268 case SND_SOC_DAIFMT_CBM_CFM:
2269 ms = WM8994_AIF1_MSTR;
2270 break;
2271 default:
2272 return -EINVAL;
2273 }
2274
2275 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2276 case SND_SOC_DAIFMT_DSP_B:
2277 aif1 |= WM8994_AIF1_LRCLK_INV;
2278 case SND_SOC_DAIFMT_DSP_A:
2279 aif1 |= 0x18;
2280 break;
2281 case SND_SOC_DAIFMT_I2S:
2282 aif1 |= 0x10;
2283 break;
2284 case SND_SOC_DAIFMT_RIGHT_J:
2285 break;
2286 case SND_SOC_DAIFMT_LEFT_J:
2287 aif1 |= 0x8;
2288 break;
2289 default:
2290 return -EINVAL;
2291 }
2292
2293 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2294 case SND_SOC_DAIFMT_DSP_A:
2295 case SND_SOC_DAIFMT_DSP_B:
2296 /* frame inversion not valid for DSP modes */
2297 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2298 case SND_SOC_DAIFMT_NB_NF:
2299 break;
2300 case SND_SOC_DAIFMT_IB_NF:
2301 aif1 |= WM8994_AIF1_BCLK_INV;
2302 break;
2303 default:
2304 return -EINVAL;
2305 }
2306 break;
2307
2308 case SND_SOC_DAIFMT_I2S:
2309 case SND_SOC_DAIFMT_RIGHT_J:
2310 case SND_SOC_DAIFMT_LEFT_J:
2311 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2312 case SND_SOC_DAIFMT_NB_NF:
2313 break;
2314 case SND_SOC_DAIFMT_IB_IF:
2315 aif1 |= WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV;
2316 break;
2317 case SND_SOC_DAIFMT_IB_NF:
2318 aif1 |= WM8994_AIF1_BCLK_INV;
2319 break;
2320 case SND_SOC_DAIFMT_NB_IF:
2321 aif1 |= WM8994_AIF1_LRCLK_INV;
2322 break;
2323 default:
2324 return -EINVAL;
2325 }
2326 break;
2327 default:
2328 return -EINVAL;
2329 }
2330
Mark Brownc4431df2010-11-26 15:21:07 +00002331 /* The AIF2 format configuration needs to be mirrored to AIF3
2332 * on WM8958 if it's in use so just do it all the time. */
Mark Brown81204c82011-05-24 17:35:53 +08002333 switch (control->type) {
2334 case WM1811:
2335 case WM8958:
2336 if (dai->id == 2)
2337 snd_soc_update_bits(codec, WM8958_AIF3_CONTROL_1,
2338 WM8994_AIF1_LRCLK_INV |
2339 WM8958_AIF3_FMT_MASK, aif1);
2340 break;
2341
2342 default:
2343 break;
2344 }
Mark Brownc4431df2010-11-26 15:21:07 +00002345
Mark Brown9e6e96a2010-01-29 17:47:12 +00002346 snd_soc_update_bits(codec, aif1_reg,
2347 WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV |
2348 WM8994_AIF1_FMT_MASK,
2349 aif1);
2350 snd_soc_update_bits(codec, ms_reg, WM8994_AIF1_MSTR,
2351 ms);
2352
2353 return 0;
2354}
2355
2356static struct {
2357 int val, rate;
2358} srs[] = {
2359 { 0, 8000 },
2360 { 1, 11025 },
2361 { 2, 12000 },
2362 { 3, 16000 },
2363 { 4, 22050 },
2364 { 5, 24000 },
2365 { 6, 32000 },
2366 { 7, 44100 },
2367 { 8, 48000 },
2368 { 9, 88200 },
2369 { 10, 96000 },
2370};
2371
2372static int fs_ratios[] = {
2373 64, 128, 192, 256, 348, 512, 768, 1024, 1408, 1536
2374};
2375
2376static int bclk_divs[] = {
2377 10, 15, 20, 30, 40, 50, 60, 80, 110, 120, 160, 220, 240, 320, 440, 480,
2378 640, 880, 960, 1280, 1760, 1920
2379};
2380
2381static int wm8994_hw_params(struct snd_pcm_substream *substream,
2382 struct snd_pcm_hw_params *params,
2383 struct snd_soc_dai *dai)
2384{
2385 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09002386 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002387 int aif1_reg;
Mark Brownb1e43d92010-12-07 17:14:56 +00002388 int aif2_reg;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002389 int bclk_reg;
2390 int lrclk_reg;
2391 int rate_reg;
2392 int aif1 = 0;
Mark Brownb1e43d92010-12-07 17:14:56 +00002393 int aif2 = 0;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002394 int bclk = 0;
2395 int lrclk = 0;
2396 int rate_val = 0;
2397 int id = dai->id - 1;
2398
2399 int i, cur_val, best_val, bclk_rate, best;
2400
2401 switch (dai->id) {
2402 case 1:
2403 aif1_reg = WM8994_AIF1_CONTROL_1;
Mark Brownb1e43d92010-12-07 17:14:56 +00002404 aif2_reg = WM8994_AIF1_CONTROL_2;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002405 bclk_reg = WM8994_AIF1_BCLK;
2406 rate_reg = WM8994_AIF1_RATE;
2407 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
Mark Brown7d83d212010-08-23 10:54:43 +01002408 wm8994->lrclk_shared[0]) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002409 lrclk_reg = WM8994_AIF1DAC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002410 } else {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002411 lrclk_reg = WM8994_AIF1ADC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002412 dev_dbg(codec->dev, "AIF1 using split LRCLK\n");
2413 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002414 break;
2415 case 2:
2416 aif1_reg = WM8994_AIF2_CONTROL_1;
Mark Brownb1e43d92010-12-07 17:14:56 +00002417 aif2_reg = WM8994_AIF2_CONTROL_2;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002418 bclk_reg = WM8994_AIF2_BCLK;
2419 rate_reg = WM8994_AIF2_RATE;
2420 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
Mark Brown7d83d212010-08-23 10:54:43 +01002421 wm8994->lrclk_shared[1]) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002422 lrclk_reg = WM8994_AIF2DAC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002423 } else {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002424 lrclk_reg = WM8994_AIF2ADC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002425 dev_dbg(codec->dev, "AIF2 using split LRCLK\n");
2426 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002427 break;
2428 default:
2429 return -EINVAL;
2430 }
2431
2432 bclk_rate = params_rate(params) * 2;
2433 switch (params_format(params)) {
2434 case SNDRV_PCM_FORMAT_S16_LE:
2435 bclk_rate *= 16;
2436 break;
2437 case SNDRV_PCM_FORMAT_S20_3LE:
2438 bclk_rate *= 20;
2439 aif1 |= 0x20;
2440 break;
2441 case SNDRV_PCM_FORMAT_S24_LE:
2442 bclk_rate *= 24;
2443 aif1 |= 0x40;
2444 break;
2445 case SNDRV_PCM_FORMAT_S32_LE:
2446 bclk_rate *= 32;
2447 aif1 |= 0x60;
2448 break;
2449 default:
2450 return -EINVAL;
2451 }
2452
2453 /* Try to find an appropriate sample rate; look for an exact match. */
2454 for (i = 0; i < ARRAY_SIZE(srs); i++)
2455 if (srs[i].rate == params_rate(params))
2456 break;
2457 if (i == ARRAY_SIZE(srs))
2458 return -EINVAL;
2459 rate_val |= srs[i].val << WM8994_AIF1_SR_SHIFT;
2460
2461 dev_dbg(dai->dev, "Sample rate is %dHz\n", srs[i].rate);
2462 dev_dbg(dai->dev, "AIF%dCLK is %dHz, target BCLK %dHz\n",
2463 dai->id, wm8994->aifclk[id], bclk_rate);
2464
Mark Brownb1e43d92010-12-07 17:14:56 +00002465 if (params_channels(params) == 1 &&
2466 (snd_soc_read(codec, aif1_reg) & 0x18) == 0x18)
2467 aif2 |= WM8994_AIF1_MONO;
2468
Mark Brown9e6e96a2010-01-29 17:47:12 +00002469 if (wm8994->aifclk[id] == 0) {
2470 dev_err(dai->dev, "AIF%dCLK not configured\n", dai->id);
2471 return -EINVAL;
2472 }
2473
2474 /* AIFCLK/fs ratio; look for a close match in either direction */
2475 best = 0;
2476 best_val = abs((fs_ratios[0] * params_rate(params))
2477 - wm8994->aifclk[id]);
2478 for (i = 1; i < ARRAY_SIZE(fs_ratios); i++) {
2479 cur_val = abs((fs_ratios[i] * params_rate(params))
2480 - wm8994->aifclk[id]);
2481 if (cur_val >= best_val)
2482 continue;
2483 best = i;
2484 best_val = cur_val;
2485 }
2486 dev_dbg(dai->dev, "Selected AIF%dCLK/fs = %d\n",
2487 dai->id, fs_ratios[best]);
2488 rate_val |= best;
2489
2490 /* We may not get quite the right frequency if using
2491 * approximate clocks so look for the closest match that is
2492 * higher than the target (we need to ensure that there enough
2493 * BCLKs to clock out the samples).
2494 */
2495 best = 0;
2496 for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
Joonyoung Shim07cd8ad2010-02-02 18:53:19 +09002497 cur_val = (wm8994->aifclk[id] * 10 / bclk_divs[i]) - bclk_rate;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002498 if (cur_val < 0) /* BCLK table is sorted */
2499 break;
2500 best = i;
2501 }
Joonyoung Shim07cd8ad2010-02-02 18:53:19 +09002502 bclk_rate = wm8994->aifclk[id] * 10 / bclk_divs[best];
Mark Brown9e6e96a2010-01-29 17:47:12 +00002503 dev_dbg(dai->dev, "Using BCLK_DIV %d for actual BCLK %dHz\n",
2504 bclk_divs[best], bclk_rate);
2505 bclk |= best << WM8994_AIF1_BCLK_DIV_SHIFT;
2506
2507 lrclk = bclk_rate / params_rate(params);
Mark Brownfc07ecd2011-11-28 21:16:56 +00002508 if (!lrclk) {
2509 dev_err(dai->dev, "Unable to generate LRCLK from %dHz BCLK\n",
2510 bclk_rate);
2511 return -EINVAL;
2512 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002513 dev_dbg(dai->dev, "Using LRCLK rate %d for actual LRCLK %dHz\n",
2514 lrclk, bclk_rate / lrclk);
2515
2516 snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
Mark Brownb1e43d92010-12-07 17:14:56 +00002517 snd_soc_update_bits(codec, aif2_reg, WM8994_AIF1_MONO, aif2);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002518 snd_soc_update_bits(codec, bclk_reg, WM8994_AIF1_BCLK_DIV_MASK, bclk);
2519 snd_soc_update_bits(codec, lrclk_reg, WM8994_AIF1DAC_RATE_MASK,
2520 lrclk);
2521 snd_soc_update_bits(codec, rate_reg, WM8994_AIF1_SR_MASK |
2522 WM8994_AIF1CLK_RATE_MASK, rate_val);
2523
2524 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
2525 switch (dai->id) {
2526 case 1:
2527 wm8994->dac_rates[0] = params_rate(params);
2528 wm8994_set_retune_mobile(codec, 0);
2529 wm8994_set_retune_mobile(codec, 1);
2530 break;
2531 case 2:
2532 wm8994->dac_rates[1] = params_rate(params);
2533 wm8994_set_retune_mobile(codec, 2);
2534 break;
2535 }
2536 }
2537
2538 return 0;
2539}
2540
Mark Brownc4431df2010-11-26 15:21:07 +00002541static int wm8994_aif3_hw_params(struct snd_pcm_substream *substream,
2542 struct snd_pcm_hw_params *params,
2543 struct snd_soc_dai *dai)
2544{
2545 struct snd_soc_codec *codec = dai->codec;
Mark Brown2a8a8562011-07-24 12:20:41 +01002546 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2547 struct wm8994 *control = wm8994->wm8994;
Mark Brownc4431df2010-11-26 15:21:07 +00002548 int aif1_reg;
2549 int aif1 = 0;
2550
2551 switch (dai->id) {
2552 case 3:
2553 switch (control->type) {
Mark Brown81204c82011-05-24 17:35:53 +08002554 case WM1811:
Mark Brownc4431df2010-11-26 15:21:07 +00002555 case WM8958:
2556 aif1_reg = WM8958_AIF3_CONTROL_1;
2557 break;
2558 default:
2559 return 0;
2560 }
2561 default:
2562 return 0;
2563 }
2564
2565 switch (params_format(params)) {
2566 case SNDRV_PCM_FORMAT_S16_LE:
2567 break;
2568 case SNDRV_PCM_FORMAT_S20_3LE:
2569 aif1 |= 0x20;
2570 break;
2571 case SNDRV_PCM_FORMAT_S24_LE:
2572 aif1 |= 0x40;
2573 break;
2574 case SNDRV_PCM_FORMAT_S32_LE:
2575 aif1 |= 0x60;
2576 break;
2577 default:
2578 return -EINVAL;
2579 }
2580
2581 return snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
2582}
2583
Mark Brown9e6e96a2010-01-29 17:47:12 +00002584static int wm8994_aif_mute(struct snd_soc_dai *codec_dai, int mute)
2585{
2586 struct snd_soc_codec *codec = codec_dai->codec;
2587 int mute_reg;
2588 int reg;
2589
2590 switch (codec_dai->id) {
2591 case 1:
2592 mute_reg = WM8994_AIF1_DAC1_FILTERS_1;
2593 break;
2594 case 2:
2595 mute_reg = WM8994_AIF2_DAC_FILTERS_1;
2596 break;
2597 default:
2598 return -EINVAL;
2599 }
2600
2601 if (mute)
2602 reg = WM8994_AIF1DAC1_MUTE;
2603 else
2604 reg = 0;
2605
2606 snd_soc_update_bits(codec, mute_reg, WM8994_AIF1DAC1_MUTE, reg);
2607
2608 return 0;
2609}
2610
Mark Brown778a76e2010-03-22 22:05:10 +00002611static int wm8994_set_tristate(struct snd_soc_dai *codec_dai, int tristate)
2612{
2613 struct snd_soc_codec *codec = codec_dai->codec;
2614 int reg, val, mask;
2615
2616 switch (codec_dai->id) {
2617 case 1:
2618 reg = WM8994_AIF1_MASTER_SLAVE;
2619 mask = WM8994_AIF1_TRI;
2620 break;
2621 case 2:
2622 reg = WM8994_AIF2_MASTER_SLAVE;
2623 mask = WM8994_AIF2_TRI;
2624 break;
Mark Brown778a76e2010-03-22 22:05:10 +00002625 default:
2626 return -EINVAL;
2627 }
2628
2629 if (tristate)
2630 val = mask;
2631 else
2632 val = 0;
2633
Qiao Zhou78b3fb42011-01-19 19:10:47 +08002634 return snd_soc_update_bits(codec, reg, mask, val);
Mark Brown778a76e2010-03-22 22:05:10 +00002635}
2636
Mark Brownd09f3ec2011-08-15 11:01:02 +09002637static int wm8994_aif2_probe(struct snd_soc_dai *dai)
2638{
2639 struct snd_soc_codec *codec = dai->codec;
2640
2641 /* Disable the pulls on the AIF if we're using it to save power. */
2642 snd_soc_update_bits(codec, WM8994_GPIO_3,
2643 WM8994_GPN_PU | WM8994_GPN_PD, 0);
2644 snd_soc_update_bits(codec, WM8994_GPIO_4,
2645 WM8994_GPN_PU | WM8994_GPN_PD, 0);
2646 snd_soc_update_bits(codec, WM8994_GPIO_5,
2647 WM8994_GPN_PU | WM8994_GPN_PD, 0);
2648
2649 return 0;
2650}
2651
Mark Brown9e6e96a2010-01-29 17:47:12 +00002652#define WM8994_RATES SNDRV_PCM_RATE_8000_96000
2653
2654#define WM8994_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
Ian Lartey3079aed2010-08-31 23:56:34 +01002655 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002656
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01002657static const struct snd_soc_dai_ops wm8994_aif1_dai_ops = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002658 .set_sysclk = wm8994_set_dai_sysclk,
2659 .set_fmt = wm8994_set_dai_fmt,
2660 .hw_params = wm8994_hw_params,
2661 .digital_mute = wm8994_aif_mute,
2662 .set_pll = wm8994_set_fll,
Mark Brown778a76e2010-03-22 22:05:10 +00002663 .set_tristate = wm8994_set_tristate,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002664};
2665
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01002666static const struct snd_soc_dai_ops wm8994_aif2_dai_ops = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002667 .set_sysclk = wm8994_set_dai_sysclk,
2668 .set_fmt = wm8994_set_dai_fmt,
2669 .hw_params = wm8994_hw_params,
2670 .digital_mute = wm8994_aif_mute,
2671 .set_pll = wm8994_set_fll,
Mark Brown778a76e2010-03-22 22:05:10 +00002672 .set_tristate = wm8994_set_tristate,
2673};
2674
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01002675static const struct snd_soc_dai_ops wm8994_aif3_dai_ops = {
Mark Brownc4431df2010-11-26 15:21:07 +00002676 .hw_params = wm8994_aif3_hw_params,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002677};
2678
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002679static struct snd_soc_dai_driver wm8994_dai[] = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002680 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002681 .name = "wm8994-aif1",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002682 .id = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002683 .playback = {
2684 .stream_name = "AIF1 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002685 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002686 .channels_max = 2,
2687 .rates = WM8994_RATES,
2688 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002689 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002690 },
2691 .capture = {
2692 .stream_name = "AIF1 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002693 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002694 .channels_max = 2,
2695 .rates = WM8994_RATES,
2696 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002697 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002698 },
2699 .ops = &wm8994_aif1_dai_ops,
2700 },
2701 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002702 .name = "wm8994-aif2",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002703 .id = 2,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002704 .playback = {
2705 .stream_name = "AIF2 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002706 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002707 .channels_max = 2,
2708 .rates = WM8994_RATES,
2709 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002710 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002711 },
2712 .capture = {
2713 .stream_name = "AIF2 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002714 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002715 .channels_max = 2,
2716 .rates = WM8994_RATES,
2717 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002718 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002719 },
Mark Brownd09f3ec2011-08-15 11:01:02 +09002720 .probe = wm8994_aif2_probe,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002721 .ops = &wm8994_aif2_dai_ops,
2722 },
2723 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002724 .name = "wm8994-aif3",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002725 .id = 3,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002726 .playback = {
2727 .stream_name = "AIF3 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002728 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002729 .channels_max = 2,
2730 .rates = WM8994_RATES,
2731 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002732 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002733 },
Dan Carpentera8462bd2010-03-24 14:58:34 +03002734 .capture = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002735 .stream_name = "AIF3 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002736 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002737 .channels_max = 2,
2738 .rates = WM8994_RATES,
2739 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00002740 .sig_bits = 24,
2741 },
Mark Brown778a76e2010-03-22 22:05:10 +00002742 .ops = &wm8994_aif3_dai_ops,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002743 }
2744};
Mark Brown9e6e96a2010-01-29 17:47:12 +00002745
2746#ifdef CONFIG_PM
Mark Brown4752a882012-03-04 02:16:01 +00002747static int wm8994_codec_suspend(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002748{
Mark Brownb2c812e2010-04-14 15:35:19 +09002749 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01002750 struct wm8994 *control = wm8994->wm8994;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002751 int i, ret;
2752
Mark Brownca629922011-05-11 14:34:53 +02002753 switch (control->type) {
2754 case WM8994:
2755 snd_soc_update_bits(codec, WM8994_MICBIAS, WM8994_MICD_ENA, 0);
2756 break;
Mark Brown81204c82011-05-24 17:35:53 +08002757 case WM1811:
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002758 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
2759 WM1811_JACKDET_MODE_MASK, 0);
2760 /* Fall through */
Mark Brownca629922011-05-11 14:34:53 +02002761 case WM8958:
2762 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
2763 WM8958_MICD_ENA, 0);
2764 break;
2765 }
2766
Mark Brown9e6e96a2010-01-29 17:47:12 +00002767 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
2768 memcpy(&wm8994->fll_suspend[i], &wm8994->fll[i],
Mark Brownf701a2e2011-03-09 19:31:01 +00002769 sizeof(struct wm8994_fll_config));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002770 ret = _wm8994_set_fll(codec, i + 1, 0, 0, 0);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002771 if (ret < 0)
2772 dev_warn(codec->dev, "Failed to stop FLL%d: %d\n",
2773 i + 1, ret);
2774 }
2775
2776 wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);
2777
2778 return 0;
2779}
2780
Mark Brown4752a882012-03-04 02:16:01 +00002781static int wm8994_codec_resume(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002782{
Mark Brownb2c812e2010-04-14 15:35:19 +09002783 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01002784 struct wm8994 *control = wm8994->wm8994;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002785 int i, ret;
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00002786 unsigned int val, mask;
2787
2788 if (wm8994->revision < 4) {
2789 /* force a HW read */
Mark Brownd9a76662011-07-24 12:49:52 +01002790 ret = regmap_read(control->regmap,
2791 WM8994_POWER_MANAGEMENT_5, &val);
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00002792
2793 /* modify the cache only */
2794 codec->cache_only = 1;
2795 mask = WM8994_DAC1R_ENA | WM8994_DAC1L_ENA |
2796 WM8994_DAC2R_ENA | WM8994_DAC2L_ENA;
2797 val &= mask;
2798 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
2799 mask, val);
2800 codec->cache_only = 0;
2801 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002802
Mark Brown9e6e96a2010-01-29 17:47:12 +00002803 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
Mark Brown6a2f1ee2010-05-10 18:36:37 +01002804 if (!wm8994->fll_suspend[i].out)
2805 continue;
2806
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002807 ret = _wm8994_set_fll(codec, i + 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002808 wm8994->fll_suspend[i].src,
2809 wm8994->fll_suspend[i].in,
2810 wm8994->fll_suspend[i].out);
2811 if (ret < 0)
2812 dev_warn(codec->dev, "Failed to restore FLL%d: %d\n",
2813 i + 1, ret);
2814 }
2815
Mark Brownca629922011-05-11 14:34:53 +02002816 switch (control->type) {
2817 case WM8994:
2818 if (wm8994->micdet[0].jack || wm8994->micdet[1].jack)
2819 snd_soc_update_bits(codec, WM8994_MICBIAS,
2820 WM8994_MICD_ENA, WM8994_MICD_ENA);
2821 break;
Mark Brown81204c82011-05-24 17:35:53 +08002822 case WM1811:
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002823 if (wm8994->jackdet && wm8994->jack_cb) {
2824 /* Restart from idle */
2825 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
2826 WM1811_JACKDET_MODE_MASK,
2827 WM1811_JACKDET_MODE_JACK);
2828 break;
2829 }
Mark Brown6f8270c2012-03-03 13:06:25 +00002830 break;
Mark Brownca629922011-05-11 14:34:53 +02002831 case WM8958:
2832 if (wm8994->jack_cb)
2833 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
2834 WM8958_MICD_ENA, WM8958_MICD_ENA);
2835 break;
2836 }
2837
Mark Brown9e6e96a2010-01-29 17:47:12 +00002838 return 0;
2839}
2840#else
Mark Brown4752a882012-03-04 02:16:01 +00002841#define wm8994_codec_suspend NULL
2842#define wm8994_codec_resume NULL
Mark Brown9e6e96a2010-01-29 17:47:12 +00002843#endif
2844
2845static void wm8994_handle_retune_mobile_pdata(struct wm8994_priv *wm8994)
2846{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002847 struct snd_soc_codec *codec = wm8994->codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002848 struct wm8994_pdata *pdata = wm8994->pdata;
2849 struct snd_kcontrol_new controls[] = {
2850 SOC_ENUM_EXT("AIF1.1 EQ Mode",
2851 wm8994->retune_mobile_enum,
2852 wm8994_get_retune_mobile_enum,
2853 wm8994_put_retune_mobile_enum),
2854 SOC_ENUM_EXT("AIF1.2 EQ Mode",
2855 wm8994->retune_mobile_enum,
2856 wm8994_get_retune_mobile_enum,
2857 wm8994_put_retune_mobile_enum),
2858 SOC_ENUM_EXT("AIF2 EQ Mode",
2859 wm8994->retune_mobile_enum,
2860 wm8994_get_retune_mobile_enum,
2861 wm8994_put_retune_mobile_enum),
2862 };
2863 int ret, i, j;
2864 const char **t;
2865
2866 /* We need an array of texts for the enum API but the number
2867 * of texts is likely to be less than the number of
2868 * configurations due to the sample rate dependency of the
2869 * configurations. */
2870 wm8994->num_retune_mobile_texts = 0;
2871 wm8994->retune_mobile_texts = NULL;
2872 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
2873 for (j = 0; j < wm8994->num_retune_mobile_texts; j++) {
2874 if (strcmp(pdata->retune_mobile_cfgs[i].name,
2875 wm8994->retune_mobile_texts[j]) == 0)
2876 break;
2877 }
2878
2879 if (j != wm8994->num_retune_mobile_texts)
2880 continue;
2881
2882 /* Expand the array... */
2883 t = krealloc(wm8994->retune_mobile_texts,
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02002884 sizeof(char *) *
Mark Brown9e6e96a2010-01-29 17:47:12 +00002885 (wm8994->num_retune_mobile_texts + 1),
2886 GFP_KERNEL);
2887 if (t == NULL)
2888 continue;
2889
2890 /* ...store the new entry... */
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02002891 t[wm8994->num_retune_mobile_texts] =
Mark Brown9e6e96a2010-01-29 17:47:12 +00002892 pdata->retune_mobile_cfgs[i].name;
2893
2894 /* ...and remember the new version. */
2895 wm8994->num_retune_mobile_texts++;
2896 wm8994->retune_mobile_texts = t;
2897 }
2898
2899 dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
2900 wm8994->num_retune_mobile_texts);
2901
2902 wm8994->retune_mobile_enum.max = wm8994->num_retune_mobile_texts;
2903 wm8994->retune_mobile_enum.texts = wm8994->retune_mobile_texts;
2904
Liam Girdwood022658b2012-02-03 17:43:09 +00002905 ret = snd_soc_add_codec_controls(wm8994->codec, controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002906 ARRAY_SIZE(controls));
2907 if (ret != 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002908 dev_err(wm8994->codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002909 "Failed to add ReTune Mobile controls: %d\n", ret);
2910}
2911
2912static void wm8994_handle_pdata(struct wm8994_priv *wm8994)
2913{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002914 struct snd_soc_codec *codec = wm8994->codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002915 struct wm8994_pdata *pdata = wm8994->pdata;
2916 int ret, i;
2917
2918 if (!pdata)
2919 return;
2920
2921 wm_hubs_handle_analogue_pdata(codec, pdata->lineout1_diff,
2922 pdata->lineout2_diff,
2923 pdata->lineout1fb,
2924 pdata->lineout2fb,
2925 pdata->jd_scthr,
2926 pdata->jd_thr,
2927 pdata->micbias1_lvl,
2928 pdata->micbias2_lvl);
2929
2930 dev_dbg(codec->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
2931
2932 if (pdata->num_drc_cfgs) {
2933 struct snd_kcontrol_new controls[] = {
2934 SOC_ENUM_EXT("AIF1DRC1 Mode", wm8994->drc_enum,
2935 wm8994_get_drc_enum, wm8994_put_drc_enum),
2936 SOC_ENUM_EXT("AIF1DRC2 Mode", wm8994->drc_enum,
2937 wm8994_get_drc_enum, wm8994_put_drc_enum),
2938 SOC_ENUM_EXT("AIF2DRC Mode", wm8994->drc_enum,
2939 wm8994_get_drc_enum, wm8994_put_drc_enum),
2940 };
2941
2942 /* We need an array of texts for the enum API */
Mark Brown7270ceb2011-12-01 14:00:19 +00002943 wm8994->drc_texts = devm_kzalloc(wm8994->codec->dev,
2944 sizeof(char *) * pdata->num_drc_cfgs, GFP_KERNEL);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002945 if (!wm8994->drc_texts) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002946 dev_err(wm8994->codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002947 "Failed to allocate %d DRC config texts\n",
2948 pdata->num_drc_cfgs);
2949 return;
2950 }
2951
2952 for (i = 0; i < pdata->num_drc_cfgs; i++)
2953 wm8994->drc_texts[i] = pdata->drc_cfgs[i].name;
2954
2955 wm8994->drc_enum.max = pdata->num_drc_cfgs;
2956 wm8994->drc_enum.texts = wm8994->drc_texts;
2957
Liam Girdwood022658b2012-02-03 17:43:09 +00002958 ret = snd_soc_add_codec_controls(wm8994->codec, controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002959 ARRAY_SIZE(controls));
2960 if (ret != 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002961 dev_err(wm8994->codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002962 "Failed to add DRC mode controls: %d\n", ret);
2963
2964 for (i = 0; i < WM8994_NUM_DRC; i++)
2965 wm8994_set_drc(codec, i);
2966 }
2967
2968 dev_dbg(codec->dev, "%d ReTune Mobile configurations\n",
2969 pdata->num_retune_mobile_cfgs);
2970
2971 if (pdata->num_retune_mobile_cfgs)
2972 wm8994_handle_retune_mobile_pdata(wm8994);
2973 else
Liam Girdwood022658b2012-02-03 17:43:09 +00002974 snd_soc_add_codec_controls(wm8994->codec, wm8994_eq_controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002975 ARRAY_SIZE(wm8994_eq_controls));
Mark Brown48e028e2011-02-21 17:11:59 -08002976
2977 for (i = 0; i < ARRAY_SIZE(pdata->micbias); i++) {
2978 if (pdata->micbias[i]) {
2979 snd_soc_write(codec, WM8958_MICBIAS1 + i,
2980 pdata->micbias[i] & 0xffff);
2981 }
2982 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002983}
2984
Mark Brown88766982010-03-29 20:57:12 +01002985/**
2986 * wm8994_mic_detect - Enable microphone detection via the WM8994 IRQ
2987 *
2988 * @codec: WM8994 codec
2989 * @jack: jack to report detection events on
2990 * @micbias: microphone bias to detect on
Mark Brown88766982010-03-29 20:57:12 +01002991 *
2992 * Enable microphone detection via IRQ on the WM8994. If GPIOs are
2993 * being used to bring out signals to the processor then only platform
Mark Brown5ab230a2010-09-06 14:59:34 +01002994 * data configuration is needed for WM8994 and processor GPIOs should
Mark Brown88766982010-03-29 20:57:12 +01002995 * be configured using snd_soc_jack_add_gpios() instead.
2996 *
2997 * Configuration of detection levels is available via the micbias1_lvl
2998 * and micbias2_lvl platform data members.
2999 */
3000int wm8994_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
Mark Brown87092e32012-02-06 18:50:39 +00003001 int micbias)
Mark Brown88766982010-03-29 20:57:12 +01003002{
Mark Brownb2c812e2010-04-14 15:35:19 +09003003 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown88766982010-03-29 20:57:12 +01003004 struct wm8994_micdet *micdet;
Mark Brown2a8a8562011-07-24 12:20:41 +01003005 struct wm8994 *control = wm8994->wm8994;
Mark Brown87092e32012-02-06 18:50:39 +00003006 int reg, ret;
Mark Brown88766982010-03-29 20:57:12 +01003007
Mark Brown87092e32012-02-06 18:50:39 +00003008 if (control->type != WM8994) {
3009 dev_warn(codec->dev, "Not a WM8994\n");
Mark Brown3a423152010-11-26 15:21:06 +00003010 return -EINVAL;
Mark Brown87092e32012-02-06 18:50:39 +00003011 }
Mark Brown3a423152010-11-26 15:21:06 +00003012
Mark Brown88766982010-03-29 20:57:12 +01003013 switch (micbias) {
3014 case 1:
3015 micdet = &wm8994->micdet[0];
Mark Brown87092e32012-02-06 18:50:39 +00003016 if (jack)
3017 ret = snd_soc_dapm_force_enable_pin(&codec->dapm,
3018 "MICBIAS1");
3019 else
3020 ret = snd_soc_dapm_disable_pin(&codec->dapm,
3021 "MICBIAS1");
Mark Brown88766982010-03-29 20:57:12 +01003022 break;
3023 case 2:
3024 micdet = &wm8994->micdet[1];
Mark Brown87092e32012-02-06 18:50:39 +00003025 if (jack)
3026 ret = snd_soc_dapm_force_enable_pin(&codec->dapm,
3027 "MICBIAS1");
3028 else
3029 ret = snd_soc_dapm_disable_pin(&codec->dapm,
3030 "MICBIAS1");
Mark Brown88766982010-03-29 20:57:12 +01003031 break;
3032 default:
Mark Brown87092e32012-02-06 18:50:39 +00003033 dev_warn(codec->dev, "Invalid MICBIAS %d\n", micbias);
Mark Brown88766982010-03-29 20:57:12 +01003034 return -EINVAL;
Mark Brown87092e32012-02-06 18:50:39 +00003035 }
Mark Brown88766982010-03-29 20:57:12 +01003036
Mark Brown87092e32012-02-06 18:50:39 +00003037 if (ret != 0)
3038 dev_warn(codec->dev, "Failed to configure MICBIAS%d: %d\n",
3039 micbias, ret);
3040
3041 dev_dbg(codec->dev, "Configuring microphone detection on %d %p\n",
3042 micbias, jack);
Mark Brown88766982010-03-29 20:57:12 +01003043
3044 /* Store the configuration */
3045 micdet->jack = jack;
Mark Brown87092e32012-02-06 18:50:39 +00003046 micdet->detecting = true;
Mark Brown88766982010-03-29 20:57:12 +01003047
3048 /* If either of the jacks is set up then enable detection */
3049 if (wm8994->micdet[0].jack || wm8994->micdet[1].jack)
3050 reg = WM8994_MICD_ENA;
Mark Brown87092e32012-02-06 18:50:39 +00003051 else
Mark Brown88766982010-03-29 20:57:12 +01003052 reg = 0;
3053
3054 snd_soc_update_bits(codec, WM8994_MICBIAS, WM8994_MICD_ENA, reg);
3055
Mark Brown87092e32012-02-06 18:50:39 +00003056 snd_soc_dapm_sync(&codec->dapm);
3057
Mark Brown88766982010-03-29 20:57:12 +01003058 return 0;
3059}
3060EXPORT_SYMBOL_GPL(wm8994_mic_detect);
3061
3062static irqreturn_t wm8994_mic_irq(int irq, void *data)
3063{
3064 struct wm8994_priv *priv = data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003065 struct snd_soc_codec *codec = priv->codec;
Mark Brown88766982010-03-29 20:57:12 +01003066 int reg;
3067 int report;
3068
Mark Brown7116f452010-12-29 13:05:21 +00003069#ifndef CONFIG_SND_SOC_WM8994_MODULE
Mark Brown2bbb5d62010-12-05 12:50:12 +00003070 trace_snd_soc_jack_irq(dev_name(codec->dev));
Mark Brown7116f452010-12-29 13:05:21 +00003071#endif
Mark Brown2bbb5d62010-12-05 12:50:12 +00003072
Mark Brown88766982010-03-29 20:57:12 +01003073 reg = snd_soc_read(codec, WM8994_INTERRUPT_RAW_STATUS_2);
3074 if (reg < 0) {
3075 dev_err(codec->dev, "Failed to read microphone status: %d\n",
3076 reg);
3077 return IRQ_HANDLED;
3078 }
3079
3080 dev_dbg(codec->dev, "Microphone status: %x\n", reg);
3081
3082 report = 0;
Mark Brown87092e32012-02-06 18:50:39 +00003083 if (reg & WM8994_MIC1_DET_STS) {
3084 if (priv->micdet[0].detecting)
3085 report = SND_JACK_HEADSET;
3086 }
3087 if (reg & WM8994_MIC1_SHRT_STS) {
3088 if (priv->micdet[0].detecting)
3089 report = SND_JACK_HEADPHONE;
3090 else
3091 report |= SND_JACK_BTN_0;
3092 }
3093 if (report)
3094 priv->micdet[0].detecting = false;
3095 else
3096 priv->micdet[0].detecting = true;
3097
Mark Brown88766982010-03-29 20:57:12 +01003098 snd_soc_jack_report(priv->micdet[0].jack, report,
Mark Brown87092e32012-02-06 18:50:39 +00003099 SND_JACK_HEADSET | SND_JACK_BTN_0);
Mark Brown88766982010-03-29 20:57:12 +01003100
3101 report = 0;
Mark Brown87092e32012-02-06 18:50:39 +00003102 if (reg & WM8994_MIC2_DET_STS) {
3103 if (priv->micdet[1].detecting)
3104 report = SND_JACK_HEADSET;
3105 }
3106 if (reg & WM8994_MIC2_SHRT_STS) {
3107 if (priv->micdet[1].detecting)
3108 report = SND_JACK_HEADPHONE;
3109 else
3110 report |= SND_JACK_BTN_0;
3111 }
3112 if (report)
3113 priv->micdet[1].detecting = false;
3114 else
3115 priv->micdet[1].detecting = true;
3116
Mark Brown88766982010-03-29 20:57:12 +01003117 snd_soc_jack_report(priv->micdet[1].jack, report,
Mark Brown87092e32012-02-06 18:50:39 +00003118 SND_JACK_HEADSET | SND_JACK_BTN_0);
Mark Brown88766982010-03-29 20:57:12 +01003119
3120 return IRQ_HANDLED;
3121}
3122
Mark Brown821edd22010-11-26 15:21:09 +00003123/* Default microphone detection handler for WM8958 - the user can
3124 * override this if they wish.
3125 */
3126static void wm8958_default_micdet(u16 status, void *data)
3127{
3128 struct snd_soc_codec *codec = data;
3129 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown4585790d2011-11-30 10:55:14 +00003130 int report;
Mark Brown821edd22010-11-26 15:21:09 +00003131
Mark Browna1691342011-11-30 14:56:40 +00003132 dev_dbg(codec->dev, "MICDET %x\n", status);
3133
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003134 /* Either nothing present or just starting detection */
Mark Brownb00adf72011-08-13 11:57:18 +09003135 if (!(status & WM8958_MICD_STS)) {
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003136 if (!wm8994->jackdet) {
3137 /* If nothing present then clear our statuses */
3138 dev_dbg(codec->dev, "Detected open circuit\n");
3139 wm8994->jack_mic = false;
3140 wm8994->mic_detecting = true;
Mark Brown821edd22010-11-26 15:21:09 +00003141
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003142 wm8958_micd_set_rate(codec);
Mark Brown821edd22010-11-26 15:21:09 +00003143
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003144 snd_soc_jack_report(wm8994->micdet[0].jack, 0,
3145 wm8994->btn_mask |
3146 SND_JACK_HEADSET);
3147 }
Mark Brownb00adf72011-08-13 11:57:18 +09003148 return;
3149 }
3150
3151 /* If the measurement is showing a high impedence we've got a
3152 * microphone.
3153 */
Mark Brown157a75e2011-11-30 13:43:51 +00003154 if (wm8994->mic_detecting && (status & 0x600)) {
Mark Brownb00adf72011-08-13 11:57:18 +09003155 dev_dbg(codec->dev, "Detected microphone\n");
3156
Mark Brown157a75e2011-11-30 13:43:51 +00003157 wm8994->mic_detecting = false;
Mark Brownb00adf72011-08-13 11:57:18 +09003158 wm8994->jack_mic = true;
3159
3160 wm8958_micd_set_rate(codec);
3161
3162 snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADSET,
3163 SND_JACK_HEADSET);
3164 }
3165
3166
Mark Brown7c08b512012-01-26 18:33:24 +00003167 if (wm8994->mic_detecting && status & 0xfc) {
Mark Brownb00adf72011-08-13 11:57:18 +09003168 dev_dbg(codec->dev, "Detected headphone\n");
Mark Brown157a75e2011-11-30 13:43:51 +00003169 wm8994->mic_detecting = false;
Mark Brownb00adf72011-08-13 11:57:18 +09003170
3171 wm8958_micd_set_rate(codec);
3172
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003173 /* If we have jackdet that will detect removal */
3174 if (wm8994->jackdet) {
Mark Brownc9865642012-03-12 16:31:50 +00003175 mutex_lock(&wm8994->accdet_lock);
3176
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003177 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3178 WM8958_MICD_ENA, 0);
3179
Mark Brownc9865642012-03-12 16:31:50 +00003180 wm1811_jackdet_set_mode(codec,
3181 WM1811_JACKDET_MODE_JACK);
3182
3183 mutex_unlock(&wm8994->accdet_lock);
3184
Mark Brownecd17322012-03-12 16:34:35 +00003185 if (wm8994->pdata->jd_ext_cap)
Mark Brown07fb9d92012-02-21 16:23:35 +00003186 snd_soc_dapm_disable_pin(&codec->dapm,
3187 "MICBIAS2");
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003188 }
Mark Brownecd17322012-03-12 16:34:35 +00003189
3190 snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADPHONE,
3191 SND_JACK_HEADSET);
Mark Brownb00adf72011-08-13 11:57:18 +09003192 }
3193
3194 /* Report short circuit as a button */
3195 if (wm8994->jack_mic) {
Mark Brown4585790d2011-11-30 10:55:14 +00003196 report = 0;
Mark Brownb00adf72011-08-13 11:57:18 +09003197 if (status & 0x4)
Mark Brown4585790d2011-11-30 10:55:14 +00003198 report |= SND_JACK_BTN_0;
3199
3200 if (status & 0x8)
3201 report |= SND_JACK_BTN_1;
3202
3203 if (status & 0x10)
3204 report |= SND_JACK_BTN_2;
3205
3206 if (status & 0x20)
3207 report |= SND_JACK_BTN_3;
3208
3209 if (status & 0x40)
3210 report |= SND_JACK_BTN_4;
3211
3212 if (status & 0x80)
3213 report |= SND_JACK_BTN_5;
3214
3215 snd_soc_jack_report(wm8994->micdet[0].jack, report,
3216 wm8994->btn_mask);
Mark Brownb00adf72011-08-13 11:57:18 +09003217 }
Mark Brown821edd22010-11-26 15:21:09 +00003218}
3219
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003220static irqreturn_t wm1811_jackdet_irq(int irq, void *data)
3221{
3222 struct wm8994_priv *wm8994 = data;
3223 struct snd_soc_codec *codec = wm8994->codec;
3224 int reg;
Mark Brownc9865642012-03-12 16:31:50 +00003225 bool present;
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003226
3227 mutex_lock(&wm8994->accdet_lock);
3228
3229 reg = snd_soc_read(codec, WM1811_JACKDET_CTRL);
3230 if (reg < 0) {
3231 dev_err(codec->dev, "Failed to read jack status: %d\n", reg);
3232 mutex_unlock(&wm8994->accdet_lock);
3233 return IRQ_NONE;
3234 }
3235
3236 dev_dbg(codec->dev, "JACKDET %x\n", reg);
3237
Mark Brownc9865642012-03-12 16:31:50 +00003238 present = reg & WM1811_JACKDET_LVL;
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003239
Mark Brownc9865642012-03-12 16:31:50 +00003240 if (present) {
3241 dev_dbg(codec->dev, "Jack detected\n");
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003242
Mark Browne9d9a962012-04-26 16:07:32 +01003243 wm8958_micd_set_rate(codec);
3244
Mark Brown55a27782012-02-21 13:45:53 +00003245 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3246 WM8958_MICB2_DISCH, 0);
3247
Mark Brown378ec0c2012-03-01 19:01:43 +00003248 /* Disable debounce while inserted */
3249 snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
3250 WM1811_JACKDET_DB, 0);
3251
Mark Brownb9e67e52012-02-28 19:03:37 +00003252 /*
3253 * Start off measument of microphone impedence to find
3254 * out what's actually there.
3255 */
3256 wm8994->mic_detecting = true;
3257 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_MIC);
3258
3259 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3260 WM8958_MICD_ENA, WM8958_MICD_ENA);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003261 } else {
3262 dev_dbg(codec->dev, "Jack not detected\n");
3263
Mark Brown55a27782012-02-21 13:45:53 +00003264 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3265 WM8958_MICB2_DISCH, WM8958_MICB2_DISCH);
3266
Mark Brown378ec0c2012-03-01 19:01:43 +00003267 /* Enable debounce while removed */
3268 snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
3269 WM1811_JACKDET_DB, WM1811_JACKDET_DB);
3270
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003271 wm8994->mic_detecting = false;
3272 wm8994->jack_mic = false;
3273 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3274 WM8958_MICD_ENA, 0);
3275 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_JACK);
3276 }
3277
3278 mutex_unlock(&wm8994->accdet_lock);
3279
Mark Brownc9865642012-03-12 16:31:50 +00003280 /* If required for an external cap force MICBIAS on */
3281 if (wm8994->pdata->jd_ext_cap) {
Mark Brownc9865642012-03-12 16:31:50 +00003282 if (present)
3283 snd_soc_dapm_force_enable_pin(&codec->dapm,
3284 "MICBIAS2");
3285 else
3286 snd_soc_dapm_disable_pin(&codec->dapm, "MICBIAS2");
Mark Brownc9865642012-03-12 16:31:50 +00003287 }
3288
3289 if (present)
3290 snd_soc_jack_report(wm8994->micdet[0].jack,
3291 SND_JACK_MECHANICAL, SND_JACK_MECHANICAL);
3292 else
3293 snd_soc_jack_report(wm8994->micdet[0].jack, 0,
3294 SND_JACK_MECHANICAL | SND_JACK_HEADSET |
3295 wm8994->btn_mask);
3296
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003297 return IRQ_HANDLED;
3298}
3299
Mark Brown821edd22010-11-26 15:21:09 +00003300/**
3301 * wm8958_mic_detect - Enable microphone detection via the WM8958 IRQ
3302 *
3303 * @codec: WM8958 codec
3304 * @jack: jack to report detection events on
3305 *
3306 * Enable microphone detection functionality for the WM8958. By
3307 * default simple detection which supports the detection of up to 6
3308 * buttons plus video and microphone functionality is supported.
3309 *
3310 * The WM8958 has an advanced jack detection facility which is able to
3311 * support complex accessory detection, especially when used in
3312 * conjunction with external circuitry. In order to provide maximum
3313 * flexiblity a callback is provided which allows a completely custom
3314 * detection algorithm.
3315 */
3316int wm8958_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
3317 wm8958_micdet_cb cb, void *cb_data)
3318{
3319 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01003320 struct wm8994 *control = wm8994->wm8994;
Mark Brown4585790d2011-11-30 10:55:14 +00003321 u16 micd_lvl_sel;
Mark Brown821edd22010-11-26 15:21:09 +00003322
Mark Brown81204c82011-05-24 17:35:53 +08003323 switch (control->type) {
3324 case WM1811:
3325 case WM8958:
3326 break;
3327 default:
Mark Brown821edd22010-11-26 15:21:09 +00003328 return -EINVAL;
Mark Brown81204c82011-05-24 17:35:53 +08003329 }
Mark Brown821edd22010-11-26 15:21:09 +00003330
3331 if (jack) {
3332 if (!cb) {
3333 dev_dbg(codec->dev, "Using default micdet callback\n");
3334 cb = wm8958_default_micdet;
3335 cb_data = codec;
3336 }
3337
Mark Brown4cdf5e42011-11-29 14:36:17 +00003338 snd_soc_dapm_force_enable_pin(&codec->dapm, "CLK_SYS");
Mark Brown7d464b22012-03-03 18:46:06 +00003339 snd_soc_dapm_sync(&codec->dapm);
Mark Brown4cdf5e42011-11-29 14:36:17 +00003340
Mark Brown821edd22010-11-26 15:21:09 +00003341 wm8994->micdet[0].jack = jack;
3342 wm8994->jack_cb = cb;
3343 wm8994->jack_cb_data = cb_data;
3344
Mark Brown157a75e2011-11-30 13:43:51 +00003345 wm8994->mic_detecting = true;
Mark Brownb00adf72011-08-13 11:57:18 +09003346 wm8994->jack_mic = false;
3347
3348 wm8958_micd_set_rate(codec);
3349
Mark Brown4585790d2011-11-30 10:55:14 +00003350 /* Detect microphones and short circuits by default */
3351 if (wm8994->pdata->micd_lvl_sel)
3352 micd_lvl_sel = wm8994->pdata->micd_lvl_sel;
3353 else
3354 micd_lvl_sel = 0x41;
3355
3356 wm8994->btn_mask = SND_JACK_BTN_0 | SND_JACK_BTN_1 |
3357 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
3358 SND_JACK_BTN_4 | SND_JACK_BTN_5;
3359
Mark Brownb00adf72011-08-13 11:57:18 +09003360 snd_soc_update_bits(codec, WM8958_MIC_DETECT_2,
Mark Brown4585790d2011-11-30 10:55:14 +00003361 WM8958_MICD_LVL_SEL_MASK, micd_lvl_sel);
Mark Brownb00adf72011-08-13 11:57:18 +09003362
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003363 WARN_ON(codec->dapm.bias_level > SND_SOC_BIAS_STANDBY);
3364
3365 /*
3366 * If we can use jack detection start off with that,
3367 * otherwise jump straight to microphone detection.
3368 */
3369 if (wm8994->jackdet) {
Mark Brown55a27782012-02-21 13:45:53 +00003370 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3371 WM8958_MICB2_DISCH,
3372 WM8958_MICB2_DISCH);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003373 snd_soc_update_bits(codec, WM8994_LDO_1,
3374 WM8994_LDO1_DISCH, 0);
3375 wm1811_jackdet_set_mode(codec,
3376 WM1811_JACKDET_MODE_JACK);
3377 } else {
3378 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3379 WM8958_MICD_ENA, WM8958_MICD_ENA);
3380 }
3381
Mark Brown821edd22010-11-26 15:21:09 +00003382 } else {
3383 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3384 WM8958_MICD_ENA, 0);
Mark Brownafaf1592012-03-03 18:46:36 +00003385 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_NONE);
Mark Brown4cdf5e42011-11-29 14:36:17 +00003386 snd_soc_dapm_disable_pin(&codec->dapm, "CLK_SYS");
Mark Brown7d464b22012-03-03 18:46:06 +00003387 snd_soc_dapm_sync(&codec->dapm);
Mark Brown821edd22010-11-26 15:21:09 +00003388 }
3389
3390 return 0;
3391}
3392EXPORT_SYMBOL_GPL(wm8958_mic_detect);
3393
3394static irqreturn_t wm8958_mic_irq(int irq, void *data)
3395{
3396 struct wm8994_priv *wm8994 = data;
3397 struct snd_soc_codec *codec = wm8994->codec;
Mark Brown19940b32011-08-19 18:05:05 +09003398 int reg, count;
Mark Brown821edd22010-11-26 15:21:09 +00003399
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003400 /*
3401 * Jack detection may have detected a removal simulataneously
3402 * with an update of the MICDET status; if so it will have
3403 * stopped detection and we can ignore this interrupt.
3404 */
Mark Brownc9865642012-03-12 16:31:50 +00003405 if (!(snd_soc_read(codec, WM8958_MIC_DETECT_1) & WM8958_MICD_ENA))
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003406 return IRQ_HANDLED;
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003407
Mark Brown19940b32011-08-19 18:05:05 +09003408 /* We may occasionally read a detection without an impedence
3409 * range being provided - if that happens loop again.
3410 */
3411 count = 10;
3412 do {
3413 reg = snd_soc_read(codec, WM8958_MIC_DETECT_3);
3414 if (reg < 0) {
3415 dev_err(codec->dev,
3416 "Failed to read mic detect status: %d\n",
3417 reg);
3418 return IRQ_NONE;
3419 }
Mark Brown821edd22010-11-26 15:21:09 +00003420
Mark Brown19940b32011-08-19 18:05:05 +09003421 if (!(reg & WM8958_MICD_VALID)) {
3422 dev_dbg(codec->dev, "Mic detect data not valid\n");
3423 goto out;
3424 }
3425
3426 if (!(reg & WM8958_MICD_STS) || (reg & WM8958_MICD_LVL_MASK))
3427 break;
3428
3429 msleep(1);
3430 } while (count--);
3431
3432 if (count == 0)
3433 dev_warn(codec->dev, "No impedence range reported for jack\n");
Mark Brown821edd22010-11-26 15:21:09 +00003434
Mark Brown7116f452010-12-29 13:05:21 +00003435#ifndef CONFIG_SND_SOC_WM8994_MODULE
Mark Brown2bbb5d62010-12-05 12:50:12 +00003436 trace_snd_soc_jack_irq(dev_name(codec->dev));
Mark Brown7116f452010-12-29 13:05:21 +00003437#endif
Mark Brown2bbb5d62010-12-05 12:50:12 +00003438
Mark Brown821edd22010-11-26 15:21:09 +00003439 if (wm8994->jack_cb)
3440 wm8994->jack_cb(reg, wm8994->jack_cb_data);
3441 else
3442 dev_warn(codec->dev, "Accessory detection with no callback\n");
3443
3444out:
3445 return IRQ_HANDLED;
3446}
3447
Mark Brown3b1af3f2011-07-14 12:38:18 +09003448static irqreturn_t wm8994_fifo_error(int irq, void *data)
3449{
3450 struct snd_soc_codec *codec = data;
3451
3452 dev_err(codec->dev, "FIFO error\n");
3453
3454 return IRQ_HANDLED;
3455}
3456
Mark Brownf0b182b2011-08-16 12:01:27 +09003457static irqreturn_t wm8994_temp_warn(int irq, void *data)
3458{
3459 struct snd_soc_codec *codec = data;
3460
3461 dev_err(codec->dev, "Thermal warning\n");
3462
3463 return IRQ_HANDLED;
3464}
3465
3466static irqreturn_t wm8994_temp_shut(int irq, void *data)
3467{
3468 struct snd_soc_codec *codec = data;
3469
3470 dev_crit(codec->dev, "Thermal shutdown\n");
3471
3472 return IRQ_HANDLED;
3473}
3474
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003475static int wm8994_codec_probe(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003476{
Mark Brownd9a76662011-07-24 12:49:52 +01003477 struct wm8994 *control = dev_get_drvdata(codec->dev->parent);
Mark Brown2bc16ed2012-03-03 23:24:39 +00003478 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003479 struct snd_soc_dapm_context *dapm = &codec->dapm;
Mark Brownd9a76662011-07-24 12:49:52 +01003480 unsigned int reg;
Mark Brownec62dbd2010-08-15 14:56:40 +01003481 int ret, i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003482
Mark Brown2bc16ed2012-03-03 23:24:39 +00003483 wm8994->codec = codec;
Mark Brownd9a76662011-07-24 12:49:52 +01003484 codec->control_data = control->regmap;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003485
Mark Brownd9a76662011-07-24 12:49:52 +01003486 snd_soc_codec_set_cache_io(codec, 16, 16, SND_SOC_REGMAP);
Mark Brown2a8a8562011-07-24 12:20:41 +01003487
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003488 wm8994->codec = codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003489
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003490 mutex_init(&wm8994->accdet_lock);
3491
Mark Brownc7ebf932011-07-12 19:47:59 +09003492 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
3493 init_completion(&wm8994->fll_locked[i]);
3494
Mark Brown9b7c5252011-02-17 20:05:44 -08003495 if (wm8994->pdata && wm8994->pdata->micdet_irq)
3496 wm8994->micdet_irq = wm8994->pdata->micdet_irq;
3497 else if (wm8994->pdata && wm8994->pdata->irq_base)
3498 wm8994->micdet_irq = wm8994->pdata->irq_base +
3499 WM8994_IRQ_MIC1_DET;
3500
Mark Brown39fb51a2010-11-26 17:23:43 +00003501 pm_runtime_enable(codec->dev);
Mark Brown5fab51742012-02-06 18:37:08 +00003502 pm_runtime_idle(codec->dev);
Mark Brown39fb51a2010-11-26 17:23:43 +00003503
Mark Brownf959dee2012-01-31 16:16:47 +00003504 /* By default use idle_bias_off, will override for WM8994 */
3505 codec->dapm.idle_bias_off = 1;
3506
Mark Brown9e6e96a2010-01-29 17:47:12 +00003507 /* Set revision-specific configuration */
Mark Brownb6b05692010-08-13 12:58:20 +01003508 wm8994->revision = snd_soc_read(codec, WM8994_CHIP_REVISION);
Mark Brown3a423152010-11-26 15:21:06 +00003509 switch (control->type) {
3510 case WM8994:
Mark Brownf959dee2012-01-31 16:16:47 +00003511 /* Single ended line outputs should have VMID on. */
3512 if (!wm8994->pdata->lineout1_diff ||
3513 !wm8994->pdata->lineout2_diff)
3514 codec->dapm.idle_bias_off = 0;
3515
Mark Brown3a423152010-11-26 15:21:06 +00003516 switch (wm8994->revision) {
3517 case 2:
3518 case 3:
Mark Brown4537c4e2011-08-01 13:10:16 +09003519 wm8994->hubs.dcs_codes_l = -5;
3520 wm8994->hubs.dcs_codes_r = -5;
Mark Brown3a423152010-11-26 15:21:06 +00003521 wm8994->hubs.hp_startup_mode = 1;
3522 wm8994->hubs.dcs_readback_mode = 1;
Mark Brownf9acf9f2011-06-07 23:23:52 +01003523 wm8994->hubs.series_startup = 1;
Mark Brown3a423152010-11-26 15:21:06 +00003524 break;
3525 default:
Mark Brown79ef0ab2011-08-01 13:02:17 +09003526 wm8994->hubs.dcs_readback_mode = 2;
Mark Brown3a423152010-11-26 15:21:06 +00003527 break;
3528 }
Mark Brown280ec8b2011-08-10 22:19:19 +09003529 break;
Mark Brown3a423152010-11-26 15:21:06 +00003530
3531 case WM8958:
Mark Brown8437f702010-03-29 17:09:45 +01003532 wm8994->hubs.dcs_readback_mode = 1;
Mark Brown29fdc362012-02-21 10:50:50 +00003533 wm8994->hubs.hp_startup_mode = 1;
Mark Brown20dc24a2012-04-05 12:55:20 +01003534
3535 switch (wm8994->revision) {
3536 case 0:
3537 break;
3538 default:
3539 wm8994->fll_byp = true;
3540 break;
3541 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00003542 break;
Mark Brown3a423152010-11-26 15:21:06 +00003543
Mark Brown81204c82011-05-24 17:35:53 +08003544 case WM1811:
3545 wm8994->hubs.dcs_readback_mode = 2;
3546 wm8994->hubs.no_series_update = 1;
Mark Brown29fdc362012-02-21 10:50:50 +00003547 wm8994->hubs.hp_startup_mode = 1;
Mark Brownaf31a222012-04-26 20:06:56 +01003548 wm8994->hubs.no_cache_dac_hp_direct = true;
Mark Brown20dc24a2012-04-05 12:55:20 +01003549 wm8994->fll_byp = true;
Mark Brown81204c82011-05-24 17:35:53 +08003550
3551 switch (wm8994->revision) {
3552 case 0:
3553 case 1:
Mark Brownfc8e6e82011-11-28 18:48:46 +00003554 case 2:
3555 case 3:
Mark Brown6473a142011-10-17 19:38:52 +01003556 wm8994->hubs.dcs_codes_l = -9;
Mark Browne1660582012-03-21 13:22:40 +00003557 wm8994->hubs.dcs_codes_r = -7;
Mark Brown81204c82011-05-24 17:35:53 +08003558 break;
3559 default:
3560 break;
3561 }
3562
3563 snd_soc_update_bits(codec, WM8994_ANALOGUE_HP_1,
3564 WM1811_HPOUT1_ATTN, WM1811_HPOUT1_ATTN);
3565 break;
3566
Mark Brown9e6e96a2010-01-29 17:47:12 +00003567 default:
3568 break;
3569 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00003570
Mark Brown2a8a8562011-07-24 12:20:41 +01003571 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR,
Mark Brown3b1af3f2011-07-14 12:38:18 +09003572 wm8994_fifo_error, "FIFO error", codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01003573 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN,
Mark Brownf0b182b2011-08-16 12:01:27 +09003574 wm8994_temp_warn, "Thermal warning", codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01003575 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT,
Mark Brownf0b182b2011-08-16 12:01:27 +09003576 wm8994_temp_shut, "Thermal shutdown", codec);
Mark Brown3b1af3f2011-07-14 12:38:18 +09003577
Mark Brown2a8a8562011-07-24 12:20:41 +01003578 ret = wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
Mark Brownb30ead52011-07-12 15:47:17 +09003579 wm_hubs_dcs_done, "DC servo done",
3580 &wm8994->hubs);
3581 if (ret == 0)
3582 wm8994->hubs.dcs_done_irq = true;
3583
Mark Brown3a423152010-11-26 15:21:06 +00003584 switch (control->type) {
3585 case WM8994:
Mark Brown9b7c5252011-02-17 20:05:44 -08003586 if (wm8994->micdet_irq) {
3587 ret = request_threaded_irq(wm8994->micdet_irq, NULL,
3588 wm8994_mic_irq,
3589 IRQF_TRIGGER_RISING,
3590 "Mic1 detect",
3591 wm8994);
3592 if (ret != 0)
3593 dev_warn(codec->dev,
3594 "Failed to request Mic1 detect IRQ: %d\n",
3595 ret);
3596 }
Mark Brown88766982010-03-29 20:57:12 +01003597
Mark Brown2a8a8562011-07-24 12:20:41 +01003598 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brown3a423152010-11-26 15:21:06 +00003599 WM8994_IRQ_MIC1_SHRT,
3600 wm8994_mic_irq, "Mic 1 short",
3601 wm8994);
3602 if (ret != 0)
3603 dev_warn(codec->dev,
3604 "Failed to request Mic1 short IRQ: %d\n",
3605 ret);
Mark Brown88766982010-03-29 20:57:12 +01003606
Mark Brown2a8a8562011-07-24 12:20:41 +01003607 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brown3a423152010-11-26 15:21:06 +00003608 WM8994_IRQ_MIC2_DET,
3609 wm8994_mic_irq, "Mic 2 detect",
3610 wm8994);
3611 if (ret != 0)
3612 dev_warn(codec->dev,
3613 "Failed to request Mic2 detect IRQ: %d\n",
3614 ret);
Mark Brown88766982010-03-29 20:57:12 +01003615
Mark Brown2a8a8562011-07-24 12:20:41 +01003616 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brown3a423152010-11-26 15:21:06 +00003617 WM8994_IRQ_MIC2_SHRT,
3618 wm8994_mic_irq, "Mic 2 short",
3619 wm8994);
3620 if (ret != 0)
3621 dev_warn(codec->dev,
3622 "Failed to request Mic2 short IRQ: %d\n",
3623 ret);
3624 break;
Mark Brown821edd22010-11-26 15:21:09 +00003625
3626 case WM8958:
Mark Brown81204c82011-05-24 17:35:53 +08003627 case WM1811:
Mark Brown9b7c5252011-02-17 20:05:44 -08003628 if (wm8994->micdet_irq) {
3629 ret = request_threaded_irq(wm8994->micdet_irq, NULL,
3630 wm8958_mic_irq,
3631 IRQF_TRIGGER_RISING,
3632 "Mic detect",
3633 wm8994);
3634 if (ret != 0)
3635 dev_warn(codec->dev,
3636 "Failed to request Mic detect IRQ: %d\n",
3637 ret);
3638 }
Mark Brown3a423152010-11-26 15:21:06 +00003639 }
Mark Brown88766982010-03-29 20:57:12 +01003640
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003641 switch (control->type) {
3642 case WM1811:
3643 if (wm8994->revision > 1) {
3644 ret = wm8994_request_irq(wm8994->wm8994,
3645 WM8994_IRQ_GPIO(6),
3646 wm1811_jackdet_irq, "JACKDET",
3647 wm8994);
3648 if (ret == 0)
3649 wm8994->jackdet = true;
3650 }
3651 break;
3652 default:
3653 break;
3654 }
3655
Mark Brownc7ebf932011-07-12 19:47:59 +09003656 wm8994->fll_locked_irq = true;
3657 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++) {
Mark Brown2a8a8562011-07-24 12:20:41 +01003658 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brownc7ebf932011-07-12 19:47:59 +09003659 WM8994_IRQ_FLL1_LOCK + i,
3660 wm8994_fll_locked_irq, "FLL lock",
3661 &wm8994->fll_locked[i]);
3662 if (ret != 0)
3663 wm8994->fll_locked_irq = false;
3664 }
3665
Mark Brown27060b3c2012-02-06 18:42:14 +00003666 /* Make sure we can read from the GPIOs if they're inputs */
3667 pm_runtime_get_sync(codec->dev);
3668
Mark Brown9e6e96a2010-01-29 17:47:12 +00003669 /* Remember if AIFnLRCLK is configured as a GPIO. This should be
3670 * configured on init - if a system wants to do this dynamically
3671 * at runtime we can deal with that then.
3672 */
Mark Brownd9a76662011-07-24 12:49:52 +01003673 ret = regmap_read(control->regmap, WM8994_GPIO_1, &reg);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003674 if (ret < 0) {
3675 dev_err(codec->dev, "Failed to read GPIO1 state: %d\n", ret);
Mark Brown88766982010-03-29 20:57:12 +01003676 goto err_irq;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003677 }
Mark Brownd9a76662011-07-24 12:49:52 +01003678 if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00003679 wm8994->lrclk_shared[0] = 1;
3680 wm8994_dai[0].symmetric_rates = 1;
3681 } else {
3682 wm8994->lrclk_shared[0] = 0;
3683 }
3684
Mark Brownd9a76662011-07-24 12:49:52 +01003685 ret = regmap_read(control->regmap, WM8994_GPIO_6, &reg);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003686 if (ret < 0) {
3687 dev_err(codec->dev, "Failed to read GPIO6 state: %d\n", ret);
Mark Brown88766982010-03-29 20:57:12 +01003688 goto err_irq;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003689 }
Mark Brownd9a76662011-07-24 12:49:52 +01003690 if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00003691 wm8994->lrclk_shared[1] = 1;
3692 wm8994_dai[1].symmetric_rates = 1;
3693 } else {
3694 wm8994->lrclk_shared[1] = 0;
3695 }
3696
Mark Brown27060b3c2012-02-06 18:42:14 +00003697 pm_runtime_put(codec->dev);
3698
Mark Brown9e6e96a2010-01-29 17:47:12 +00003699 /* Latch volume updates (right only; we always do left then right). */
Mark Brownbaa81602011-04-06 10:52:42 +09003700 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_LEFT_VOLUME,
3701 WM8994_AIF1DAC1_VU, WM8994_AIF1DAC1_VU);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003702 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_RIGHT_VOLUME,
3703 WM8994_AIF1DAC1_VU, WM8994_AIF1DAC1_VU);
Mark Brownbaa81602011-04-06 10:52:42 +09003704 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_LEFT_VOLUME,
3705 WM8994_AIF1DAC2_VU, WM8994_AIF1DAC2_VU);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003706 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_RIGHT_VOLUME,
3707 WM8994_AIF1DAC2_VU, WM8994_AIF1DAC2_VU);
Mark Brownbaa81602011-04-06 10:52:42 +09003708 snd_soc_update_bits(codec, WM8994_AIF2_DAC_LEFT_VOLUME,
3709 WM8994_AIF2DAC_VU, WM8994_AIF2DAC_VU);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003710 snd_soc_update_bits(codec, WM8994_AIF2_DAC_RIGHT_VOLUME,
3711 WM8994_AIF2DAC_VU, WM8994_AIF2DAC_VU);
Mark Brownbaa81602011-04-06 10:52:42 +09003712 snd_soc_update_bits(codec, WM8994_AIF1_ADC1_LEFT_VOLUME,
3713 WM8994_AIF1ADC1_VU, WM8994_AIF1ADC1_VU);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003714 snd_soc_update_bits(codec, WM8994_AIF1_ADC1_RIGHT_VOLUME,
3715 WM8994_AIF1ADC1_VU, WM8994_AIF1ADC1_VU);
Mark Brownbaa81602011-04-06 10:52:42 +09003716 snd_soc_update_bits(codec, WM8994_AIF1_ADC2_LEFT_VOLUME,
3717 WM8994_AIF1ADC2_VU, WM8994_AIF1ADC2_VU);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003718 snd_soc_update_bits(codec, WM8994_AIF1_ADC2_RIGHT_VOLUME,
3719 WM8994_AIF1ADC2_VU, WM8994_AIF1ADC2_VU);
Mark Brownbaa81602011-04-06 10:52:42 +09003720 snd_soc_update_bits(codec, WM8994_AIF2_ADC_LEFT_VOLUME,
3721 WM8994_AIF2ADC_VU, WM8994_AIF1ADC2_VU);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003722 snd_soc_update_bits(codec, WM8994_AIF2_ADC_RIGHT_VOLUME,
3723 WM8994_AIF2ADC_VU, WM8994_AIF1ADC2_VU);
Mark Brownbaa81602011-04-06 10:52:42 +09003724 snd_soc_update_bits(codec, WM8994_DAC1_LEFT_VOLUME,
3725 WM8994_DAC1_VU, WM8994_DAC1_VU);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003726 snd_soc_update_bits(codec, WM8994_DAC1_RIGHT_VOLUME,
3727 WM8994_DAC1_VU, WM8994_DAC1_VU);
Mark Brownbaa81602011-04-06 10:52:42 +09003728 snd_soc_update_bits(codec, WM8994_DAC2_LEFT_VOLUME,
3729 WM8994_DAC2_VU, WM8994_DAC2_VU);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003730 snd_soc_update_bits(codec, WM8994_DAC2_RIGHT_VOLUME,
3731 WM8994_DAC2_VU, WM8994_DAC2_VU);
3732
3733 /* Set the low bit of the 3D stereo depth so TLV matches */
3734 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_FILTERS_2,
3735 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT,
3736 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT);
3737 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_FILTERS_2,
3738 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT,
3739 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT);
3740 snd_soc_update_bits(codec, WM8994_AIF2_DAC_FILTERS_2,
3741 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT,
3742 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT);
3743
Mark Brown5b739672011-07-06 00:08:43 -07003744 /* Unconditionally enable AIF1 ADC TDM mode on chips which can
3745 * use this; it only affects behaviour on idle TDM clock
3746 * cycles. */
3747 switch (control->type) {
3748 case WM8994:
3749 case WM8958:
3750 snd_soc_update_bits(codec, WM8994_AIF1_CONTROL_1,
3751 WM8994_AIF1ADC_TDM, WM8994_AIF1ADC_TDM);
3752 break;
3753 default:
3754 break;
3755 }
Mark Brownd1ce6b22010-07-20 10:13:14 +01003756
Mark Brown500fa302011-11-29 19:58:19 +00003757 /* Put MICBIAS into bypass mode by default on newer devices */
3758 switch (control->type) {
3759 case WM8958:
3760 case WM1811:
3761 snd_soc_update_bits(codec, WM8958_MICBIAS1,
3762 WM8958_MICB1_MODE, WM8958_MICB1_MODE);
3763 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3764 WM8958_MICB2_MODE, WM8958_MICB2_MODE);
3765 break;
3766 default:
3767 break;
3768 }
3769
Mark Brownc3403042012-04-26 21:29:29 +01003770 wm8994->hubs.check_class_w_digital = wm8994_check_class_w_digital;
3771 wm_hubs_update_class_w(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003772
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003773 wm8994_handle_pdata(wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003774
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003775 wm_hubs_add_analogue_controls(codec);
Liam Girdwood022658b2012-02-03 17:43:09 +00003776 snd_soc_add_codec_controls(codec, wm8994_snd_controls,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003777 ARRAY_SIZE(wm8994_snd_controls));
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003778 snd_soc_dapm_new_controls(dapm, wm8994_dapm_widgets,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003779 ARRAY_SIZE(wm8994_dapm_widgets));
Mark Brownc4431df2010-11-26 15:21:07 +00003780
3781 switch (control->type) {
3782 case WM8994:
3783 snd_soc_dapm_new_controls(dapm, wm8994_specific_dapm_widgets,
3784 ARRAY_SIZE(wm8994_specific_dapm_widgets));
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00003785 if (wm8994->revision < 4) {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00003786 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
3787 ARRAY_SIZE(wm8994_lateclk_revd_widgets));
Dimitris Papastamos04d28682011-03-01 11:47:10 +00003788 snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
3789 ARRAY_SIZE(wm8994_adc_revd_widgets));
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00003790 snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
3791 ARRAY_SIZE(wm8994_dac_revd_widgets));
3792 } else {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00003793 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
3794 ARRAY_SIZE(wm8994_lateclk_widgets));
Dimitris Papastamos04d28682011-03-01 11:47:10 +00003795 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
3796 ARRAY_SIZE(wm8994_adc_widgets));
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00003797 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
3798 ARRAY_SIZE(wm8994_dac_widgets));
3799 }
Mark Brownc4431df2010-11-26 15:21:07 +00003800 break;
3801 case WM8958:
Liam Girdwood022658b2012-02-03 17:43:09 +00003802 snd_soc_add_codec_controls(codec, wm8958_snd_controls,
Mark Brownc4431df2010-11-26 15:21:07 +00003803 ARRAY_SIZE(wm8958_snd_controls));
3804 snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
3805 ARRAY_SIZE(wm8958_dapm_widgets));
Mark Brown780e2802011-03-11 18:00:19 +00003806 if (wm8994->revision < 1) {
3807 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
3808 ARRAY_SIZE(wm8994_lateclk_revd_widgets));
3809 snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
3810 ARRAY_SIZE(wm8994_adc_revd_widgets));
3811 snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
3812 ARRAY_SIZE(wm8994_dac_revd_widgets));
3813 } else {
3814 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
3815 ARRAY_SIZE(wm8994_lateclk_widgets));
3816 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
3817 ARRAY_SIZE(wm8994_adc_widgets));
3818 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
3819 ARRAY_SIZE(wm8994_dac_widgets));
3820 }
Mark Brownc4431df2010-11-26 15:21:07 +00003821 break;
Mark Brown81204c82011-05-24 17:35:53 +08003822
3823 case WM1811:
Liam Girdwood022658b2012-02-03 17:43:09 +00003824 snd_soc_add_codec_controls(codec, wm8958_snd_controls,
Mark Brown81204c82011-05-24 17:35:53 +08003825 ARRAY_SIZE(wm8958_snd_controls));
3826 snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
3827 ARRAY_SIZE(wm8958_dapm_widgets));
3828 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
3829 ARRAY_SIZE(wm8994_lateclk_widgets));
3830 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
3831 ARRAY_SIZE(wm8994_adc_widgets));
3832 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
3833 ARRAY_SIZE(wm8994_dac_widgets));
3834 break;
Mark Brownc4431df2010-11-26 15:21:07 +00003835 }
Mark Brownc4431df2010-11-26 15:21:07 +00003836
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003837 wm_hubs_add_analogue_routes(codec, 0, 0);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003838 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
Mark Brown9e6e96a2010-01-29 17:47:12 +00003839
Mark Brownc4431df2010-11-26 15:21:07 +00003840 switch (control->type) {
3841 case WM8994:
3842 snd_soc_dapm_add_routes(dapm, wm8994_intercon,
3843 ARRAY_SIZE(wm8994_intercon));
Mark Brown6ed8f142011-02-03 16:27:35 +00003844
Dimitris Papastamos173efa02011-02-11 16:32:11 +00003845 if (wm8994->revision < 4) {
Mark Brown6ed8f142011-02-03 16:27:35 +00003846 snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
3847 ARRAY_SIZE(wm8994_revd_intercon));
Dimitris Papastamos173efa02011-02-11 16:32:11 +00003848 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
3849 ARRAY_SIZE(wm8994_lateclk_revd_intercon));
3850 } else {
3851 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
3852 ARRAY_SIZE(wm8994_lateclk_intercon));
3853 }
Mark Brownc4431df2010-11-26 15:21:07 +00003854 break;
3855 case WM8958:
Mark Brown780e2802011-03-11 18:00:19 +00003856 if (wm8994->revision < 1) {
3857 snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
3858 ARRAY_SIZE(wm8994_revd_intercon));
3859 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
3860 ARRAY_SIZE(wm8994_lateclk_revd_intercon));
3861 } else {
3862 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
3863 ARRAY_SIZE(wm8994_lateclk_intercon));
3864 snd_soc_dapm_add_routes(dapm, wm8958_intercon,
3865 ARRAY_SIZE(wm8958_intercon));
3866 }
Mark Brownf701a2e2011-03-09 19:31:01 +00003867
3868 wm8958_dsp2_init(codec);
Mark Brownc4431df2010-11-26 15:21:07 +00003869 break;
Mark Brown81204c82011-05-24 17:35:53 +08003870 case WM1811:
3871 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
3872 ARRAY_SIZE(wm8994_lateclk_intercon));
3873 snd_soc_dapm_add_routes(dapm, wm8958_intercon,
3874 ARRAY_SIZE(wm8958_intercon));
3875 break;
Mark Brownc4431df2010-11-26 15:21:07 +00003876 }
3877
Mark Brown9e6e96a2010-01-29 17:47:12 +00003878 return 0;
3879
Mark Brown88766982010-03-29 20:57:12 +01003880err_irq:
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003881 if (wm8994->jackdet)
3882 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01003883 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_SHRT, wm8994);
3884 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET, wm8994);
3885 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT, wm8994);
Mark Brown9b7c5252011-02-17 20:05:44 -08003886 if (wm8994->micdet_irq)
3887 free_irq(wm8994->micdet_irq, wm8994);
Mark Brownc7ebf932011-07-12 19:47:59 +09003888 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
Mark Brown2a8a8562011-07-24 12:20:41 +01003889 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
Mark Brownc7ebf932011-07-12 19:47:59 +09003890 &wm8994->fll_locked[i]);
Mark Brown2a8a8562011-07-24 12:20:41 +01003891 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
Mark Brownb30ead52011-07-12 15:47:17 +09003892 &wm8994->hubs);
Mark Brown2a8a8562011-07-24 12:20:41 +01003893 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, codec);
3894 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, codec);
3895 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, codec);
Mark Browna421a0e2011-12-29 11:08:34 +00003896
Mark Brown9e6e96a2010-01-29 17:47:12 +00003897 return ret;
3898}
3899
Jesper Juhl34ff0f92012-04-09 22:52:19 +02003900static int wm8994_codec_remove(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003901{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003902 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01003903 struct wm8994 *control = wm8994->wm8994;
Mark Brownc7ebf932011-07-12 19:47:59 +09003904 int i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003905
3906 wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003907
Mark Brown39fb51a2010-11-26 17:23:43 +00003908 pm_runtime_disable(codec->dev);
3909
Mark Brownc7ebf932011-07-12 19:47:59 +09003910 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
Mark Brown2a8a8562011-07-24 12:20:41 +01003911 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
Mark Brownc7ebf932011-07-12 19:47:59 +09003912 &wm8994->fll_locked[i]);
3913
Mark Brown2a8a8562011-07-24 12:20:41 +01003914 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
Mark Brownb30ead52011-07-12 15:47:17 +09003915 &wm8994->hubs);
Mark Brown2a8a8562011-07-24 12:20:41 +01003916 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, codec);
3917 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, codec);
3918 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, codec);
Mark Brownb30ead52011-07-12 15:47:17 +09003919
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003920 if (wm8994->jackdet)
3921 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);
3922
Mark Brown3a423152010-11-26 15:21:06 +00003923 switch (control->type) {
3924 case WM8994:
Mark Brown9b7c5252011-02-17 20:05:44 -08003925 if (wm8994->micdet_irq)
3926 free_irq(wm8994->micdet_irq, wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01003927 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET,
Mark Brown3a423152010-11-26 15:21:06 +00003928 wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01003929 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT,
Mark Brown3a423152010-11-26 15:21:06 +00003930 wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01003931 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_DET,
Mark Brown3a423152010-11-26 15:21:06 +00003932 wm8994);
3933 break;
Mark Brown821edd22010-11-26 15:21:09 +00003934
Mark Brown81204c82011-05-24 17:35:53 +08003935 case WM1811:
Mark Brown821edd22010-11-26 15:21:09 +00003936 case WM8958:
Mark Brown9b7c5252011-02-17 20:05:44 -08003937 if (wm8994->micdet_irq)
3938 free_irq(wm8994->micdet_irq, wm8994);
Mark Brown821edd22010-11-26 15:21:09 +00003939 break;
Mark Brown3a423152010-11-26 15:21:06 +00003940 }
Jesper Juhl34ff0f92012-04-09 22:52:19 +02003941 release_firmware(wm8994->mbc);
3942 release_firmware(wm8994->mbc_vss);
3943 release_firmware(wm8994->enh_eq);
Axel Lin24fb2b12010-11-23 15:58:39 +08003944 kfree(wm8994->retune_mobile_texts);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003945 return 0;
3946}
3947
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003948static struct snd_soc_codec_driver soc_codec_dev_wm8994 = {
3949 .probe = wm8994_codec_probe,
3950 .remove = wm8994_codec_remove,
Mark Brown4752a882012-03-04 02:16:01 +00003951 .suspend = wm8994_codec_suspend,
3952 .resume = wm8994_codec_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003953 .set_bias_level = wm8994_set_bias_level,
3954};
3955
3956static int __devinit wm8994_probe(struct platform_device *pdev)
3957{
Mark Brown2bc16ed2012-03-03 23:24:39 +00003958 struct wm8994_priv *wm8994;
3959
3960 wm8994 = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_priv),
3961 GFP_KERNEL);
3962 if (wm8994 == NULL)
3963 return -ENOMEM;
3964 platform_set_drvdata(pdev, wm8994);
3965
3966 wm8994->wm8994 = dev_get_drvdata(pdev->dev.parent);
3967 wm8994->pdata = dev_get_platdata(pdev->dev.parent);
3968
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003969 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wm8994,
3970 wm8994_dai, ARRAY_SIZE(wm8994_dai));
3971}
3972
3973static int __devexit wm8994_remove(struct platform_device *pdev)
3974{
3975 snd_soc_unregister_codec(&pdev->dev);
3976 return 0;
3977}
3978
Mark Brown4752a882012-03-04 02:16:01 +00003979#ifdef CONFIG_PM_SLEEP
3980static int wm8994_suspend(struct device *dev)
3981{
3982 struct wm8994_priv *wm8994 = dev_get_drvdata(dev);
3983
3984 /* Drop down to power saving mode when system is suspended */
3985 if (wm8994->jackdet && !wm8994->active_refcount)
3986 regmap_update_bits(wm8994->wm8994->regmap, WM8994_ANTIPOP_2,
3987 WM1811_JACKDET_MODE_MASK,
3988 wm8994->jackdet_mode);
3989
3990 return 0;
3991}
3992
3993static int wm8994_resume(struct device *dev)
3994{
3995 struct wm8994_priv *wm8994 = dev_get_drvdata(dev);
3996
3997 if (wm8994->jackdet && wm8994->jack_cb)
3998 regmap_update_bits(wm8994->wm8994->regmap, WM8994_ANTIPOP_2,
3999 WM1811_JACKDET_MODE_MASK,
4000 WM1811_JACKDET_MODE_AUDIO);
4001
4002 return 0;
4003}
4004#endif
4005
4006static const struct dev_pm_ops wm8994_pm_ops = {
4007 SET_SYSTEM_SLEEP_PM_OPS(wm8994_suspend, wm8994_resume)
4008};
4009
Mark Brown9e6e96a2010-01-29 17:47:12 +00004010static struct platform_driver wm8994_codec_driver = {
4011 .driver = {
Mark Brown4752a882012-03-04 02:16:01 +00004012 .name = "wm8994-codec",
4013 .owner = THIS_MODULE,
4014 .pm = &wm8994_pm_ops,
4015 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004016 .probe = wm8994_probe,
4017 .remove = __devexit_p(wm8994_remove),
Mark Brown9e6e96a2010-01-29 17:47:12 +00004018};
4019
Mark Brown5bbcc3c2011-11-23 22:52:08 +00004020module_platform_driver(wm8994_codec_driver);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004021
4022MODULE_DESCRIPTION("ASoC WM8994 driver");
4023MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
4024MODULE_LICENSE("GPL");
4025MODULE_ALIAS("platform:wm8994-codec");