blob: 91f3638ab33fbc55f6e61f0c4e6a65f0bebdd8c6 [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 +000063struct wm8958_micd_rate {
Mark Brownb00adf72011-08-13 11:57:18 +090064 int sysclk;
65 bool idle;
66 int start;
67 int rate;
Mark Brownaf6b6fe2011-11-30 20:32:05 +000068};
69
70static const struct wm8958_micd_rate micdet_rates[] = {
Mark Brownb00adf72011-08-13 11:57:18 +090071 { 32768, true, 1, 4 },
72 { 32768, false, 1, 1 },
Mark Brown604533d2011-12-01 12:51:25 +000073 { 44100 * 256, true, 7, 10 },
74 { 44100 * 256, false, 7, 10 },
Mark Brownb00adf72011-08-13 11:57:18 +090075};
76
Mark Brownaf6b6fe2011-11-30 20:32:05 +000077static const struct wm8958_micd_rate jackdet_rates[] = {
78 { 32768, true, 0, 1 },
79 { 32768, false, 0, 1 },
80 { 44100 * 256, true, 7, 10 },
81 { 44100 * 256, false, 7, 10 },
82};
83
Mark Brownb00adf72011-08-13 11:57:18 +090084static void wm8958_micd_set_rate(struct snd_soc_codec *codec)
85{
86 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
87 int best, i, sysclk, val;
88 bool idle;
Mark Brownaf6b6fe2011-11-30 20:32:05 +000089 const struct wm8958_micd_rate *rates;
90 int num_rates;
Mark Brownb00adf72011-08-13 11:57:18 +090091
92 if (wm8994->jack_cb != wm8958_default_micdet)
93 return;
94
95 idle = !wm8994->jack_mic;
96
97 sysclk = snd_soc_read(codec, WM8994_CLOCKING_1);
98 if (sysclk & WM8994_SYSCLK_SRC)
99 sysclk = wm8994->aifclk[1];
100 else
101 sysclk = wm8994->aifclk[0];
102
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000103 if (wm8994->jackdet) {
104 rates = jackdet_rates;
105 num_rates = ARRAY_SIZE(jackdet_rates);
106 } else {
107 rates = micdet_rates;
108 num_rates = ARRAY_SIZE(micdet_rates);
109 }
110
Mark Brownb00adf72011-08-13 11:57:18 +0900111 best = 0;
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000112 for (i = 0; i < num_rates; i++) {
113 if (rates[i].idle != idle)
Mark Brownb00adf72011-08-13 11:57:18 +0900114 continue;
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000115 if (abs(rates[i].sysclk - sysclk) <
116 abs(rates[best].sysclk - sysclk))
Mark Brownb00adf72011-08-13 11:57:18 +0900117 best = i;
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000118 else if (rates[best].idle != idle)
Mark Brownb00adf72011-08-13 11:57:18 +0900119 best = i;
120 }
121
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000122 val = rates[best].start << WM8958_MICD_BIAS_STARTTIME_SHIFT
123 | rates[best].rate << WM8958_MICD_RATE_SHIFT;
Mark Brownb00adf72011-08-13 11:57:18 +0900124
125 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
126 WM8958_MICD_BIAS_STARTTIME_MASK |
127 WM8958_MICD_RATE_MASK, val);
128}
129
Dimitris Papastamosd4754ec2011-01-13 12:20:37 +0000130static int wm8994_readable(struct snd_soc_codec *codec, unsigned int reg)
Mark Brown9e6e96a2010-01-29 17:47:12 +0000131{
Mark Brownaf9af862011-03-16 21:05:06 +0000132 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +0100133 struct wm8994 *control = wm8994->wm8994;
Mark Brownaf9af862011-03-16 21:05:06 +0000134
Mark Browne88ff1e2010-07-09 00:12:08 +0900135 switch (reg) {
136 case WM8994_GPIO_1:
137 case WM8994_GPIO_2:
138 case WM8994_GPIO_3:
139 case WM8994_GPIO_4:
140 case WM8994_GPIO_5:
141 case WM8994_GPIO_6:
142 case WM8994_GPIO_7:
143 case WM8994_GPIO_8:
144 case WM8994_GPIO_9:
145 case WM8994_GPIO_10:
146 case WM8994_GPIO_11:
147 case WM8994_INTERRUPT_STATUS_1:
148 case WM8994_INTERRUPT_STATUS_2:
149 case WM8994_INTERRUPT_RAW_STATUS_2:
150 return 1;
Mark Brownaf9af862011-03-16 21:05:06 +0000151
152 case WM8958_DSP2_PROGRAM:
153 case WM8958_DSP2_CONFIG:
154 case WM8958_DSP2_EXECCONTROL:
155 if (control->type == WM8958)
156 return 1;
157 else
158 return 0;
159
Mark Browne88ff1e2010-07-09 00:12:08 +0900160 default:
161 break;
162 }
163
Mark Brown7b306da2010-11-16 20:11:40 +0000164 if (reg >= WM8994_CACHE_SIZE)
Mark Brown9e6e96a2010-01-29 17:47:12 +0000165 return 0;
Mark Brown7b306da2010-11-16 20:11:40 +0000166 return wm8994_access_masks[reg].readable != 0;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000167}
168
Dimitris Papastamosd4754ec2011-01-13 12:20:37 +0000169static int wm8994_volatile(struct snd_soc_codec *codec, unsigned int reg)
Mark Brown9e6e96a2010-01-29 17:47:12 +0000170{
Mark Brownca9aef52010-11-26 17:23:41 +0000171 if (reg >= WM8994_CACHE_SIZE)
Mark Brown9e6e96a2010-01-29 17:47:12 +0000172 return 1;
173
174 switch (reg) {
175 case WM8994_SOFTWARE_RESET:
176 case WM8994_CHIP_REVISION:
177 case WM8994_DC_SERVO_1:
178 case WM8994_DC_SERVO_READBACK:
179 case WM8994_RATE_STATUS:
180 case WM8994_LDO_1:
181 case WM8994_LDO_2:
Mark Brownd6addcc2010-11-26 15:21:08 +0000182 case WM8958_DSP2_EXECCONTROL:
Mark Brown821edd22010-11-26 15:21:09 +0000183 case WM8958_MIC_DETECT_3:
Mark Brown79ef0ab2011-08-01 13:02:17 +0900184 case WM8994_DC_SERVO_4E:
Mark Brown9e6e96a2010-01-29 17:47:12 +0000185 return 1;
186 default:
187 return 0;
188 }
189}
190
191static int wm8994_write(struct snd_soc_codec *codec, unsigned int reg,
192 unsigned int value)
193{
Mark Brownca9aef52010-11-26 17:23:41 +0000194 int ret;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000195
196 BUG_ON(reg > WM8994_MAX_REGISTER);
197
Dimitris Papastamosd4754ec2011-01-13 12:20:37 +0000198 if (!wm8994_volatile(codec, reg)) {
Mark Brownca9aef52010-11-26 17:23:41 +0000199 ret = snd_soc_cache_write(codec, reg, value);
200 if (ret != 0)
201 dev_err(codec->dev, "Cache write to %x failed: %d\n",
202 reg, ret);
203 }
Mark Brown9e6e96a2010-01-29 17:47:12 +0000204
205 return wm8994_reg_write(codec->control_data, reg, value);
206}
207
208static unsigned int wm8994_read(struct snd_soc_codec *codec,
209 unsigned int reg)
210{
Mark Brownca9aef52010-11-26 17:23:41 +0000211 unsigned int val;
212 int ret;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000213
214 BUG_ON(reg > WM8994_MAX_REGISTER);
215
Dimitris Papastamosd4754ec2011-01-13 12:20:37 +0000216 if (!wm8994_volatile(codec, reg) && wm8994_readable(codec, reg) &&
Mark Brownca9aef52010-11-26 17:23:41 +0000217 reg < codec->driver->reg_cache_size) {
218 ret = snd_soc_cache_read(codec, reg, &val);
219 if (ret >= 0)
220 return val;
221 else
222 dev_err(codec->dev, "Cache read from %x failed: %d\n",
223 reg, ret);
224 }
225
226 return wm8994_reg_read(codec->control_data, reg);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000227}
228
229static int configure_aif_clock(struct snd_soc_codec *codec, int aif)
230{
Mark Brownb2c812e2010-04-14 15:35:19 +0900231 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000232 int rate;
233 int reg1 = 0;
234 int offset;
235
236 if (aif)
237 offset = 4;
238 else
239 offset = 0;
240
241 switch (wm8994->sysclk[aif]) {
242 case WM8994_SYSCLK_MCLK1:
243 rate = wm8994->mclk[0];
244 break;
245
246 case WM8994_SYSCLK_MCLK2:
247 reg1 |= 0x8;
248 rate = wm8994->mclk[1];
249 break;
250
251 case WM8994_SYSCLK_FLL1:
252 reg1 |= 0x10;
253 rate = wm8994->fll[0].out;
254 break;
255
256 case WM8994_SYSCLK_FLL2:
257 reg1 |= 0x18;
258 rate = wm8994->fll[1].out;
259 break;
260
261 default:
262 return -EINVAL;
263 }
264
265 if (rate >= 13500000) {
266 rate /= 2;
267 reg1 |= WM8994_AIF1CLK_DIV;
268
269 dev_dbg(codec->dev, "Dividing AIF%d clock to %dHz\n",
270 aif + 1, rate);
271 }
Mark Brown5e5e2be2010-04-25 12:20:30 +0100272
Mark Brown9e6e96a2010-01-29 17:47:12 +0000273 wm8994->aifclk[aif] = rate;
274
275 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1 + offset,
276 WM8994_AIF1CLK_SRC_MASK | WM8994_AIF1CLK_DIV,
277 reg1);
278
279 return 0;
280}
281
282static int configure_clock(struct snd_soc_codec *codec)
283{
Mark Brownb2c812e2010-04-14 15:35:19 +0900284 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Axel Lin04f45c42011-10-04 20:07:03 +0800285 int change, new;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000286
287 /* Bring up the AIF clocks first */
288 configure_aif_clock(codec, 0);
289 configure_aif_clock(codec, 1);
290
291 /* Then switch CLK_SYS over to the higher of them; a change
292 * can only happen as a result of a clocking change which can
293 * only be made outside of DAPM so we can safely redo the
294 * clocking.
295 */
296
297 /* If they're equal it doesn't matter which is used */
Mark Brownb00adf72011-08-13 11:57:18 +0900298 if (wm8994->aifclk[0] == wm8994->aifclk[1]) {
299 wm8958_micd_set_rate(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000300 return 0;
Mark Brownb00adf72011-08-13 11:57:18 +0900301 }
Mark Brown9e6e96a2010-01-29 17:47:12 +0000302
303 if (wm8994->aifclk[0] < wm8994->aifclk[1])
304 new = WM8994_SYSCLK_SRC;
305 else
306 new = 0;
307
Axel Lin04f45c42011-10-04 20:07:03 +0800308 change = snd_soc_update_bits(codec, WM8994_CLOCKING_1,
309 WM8994_SYSCLK_SRC, new);
Mark Brown52ac7ab2011-12-01 12:43:26 +0000310 if (change)
311 snd_soc_dapm_sync(&codec->dapm);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000312
Mark Brownb00adf72011-08-13 11:57:18 +0900313 wm8958_micd_set_rate(codec);
314
Mark Brown9e6e96a2010-01-29 17:47:12 +0000315 return 0;
316}
317
318static int check_clk_sys(struct snd_soc_dapm_widget *source,
319 struct snd_soc_dapm_widget *sink)
320{
321 int reg = snd_soc_read(source->codec, WM8994_CLOCKING_1);
322 const char *clk;
323
324 /* Check what we're currently using for CLK_SYS */
325 if (reg & WM8994_SYSCLK_SRC)
326 clk = "AIF2CLK";
327 else
328 clk = "AIF1CLK";
329
330 return strcmp(source->name, clk) == 0;
331}
332
333static const char *sidetone_hpf_text[] = {
334 "2.7kHz", "1.35kHz", "675Hz", "370Hz", "180Hz", "90Hz", "45Hz"
335};
336
337static const struct soc_enum sidetone_hpf =
338 SOC_ENUM_SINGLE(WM8994_SIDETONE, 7, 7, sidetone_hpf_text);
339
Uk Kim146fd572010-12-07 13:58:40 +0000340static const char *adc_hpf_text[] = {
341 "HiFi", "Voice 1", "Voice 2", "Voice 3"
342};
343
344static const struct soc_enum aif1adc1_hpf =
345 SOC_ENUM_SINGLE(WM8994_AIF1_ADC1_FILTERS, 13, 4, adc_hpf_text);
346
347static const struct soc_enum aif1adc2_hpf =
348 SOC_ENUM_SINGLE(WM8994_AIF1_ADC2_FILTERS, 13, 4, adc_hpf_text);
349
350static const struct soc_enum aif2adc_hpf =
351 SOC_ENUM_SINGLE(WM8994_AIF2_ADC_FILTERS, 13, 4, adc_hpf_text);
352
Mark Brown9e6e96a2010-01-29 17:47:12 +0000353static const DECLARE_TLV_DB_SCALE(aif_tlv, 0, 600, 0);
354static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
355static const DECLARE_TLV_DB_SCALE(st_tlv, -3600, 300, 0);
356static const DECLARE_TLV_DB_SCALE(wm8994_3d_tlv, -1600, 183, 0);
357static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
Mark Brown1ddc07d2011-08-16 10:08:48 +0900358static const DECLARE_TLV_DB_SCALE(ng_tlv, -10200, 600, 0);
Mark Brown81204c82011-05-24 17:35:53 +0800359static const DECLARE_TLV_DB_SCALE(mixin_boost_tlv, 0, 900, 0);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000360
361#define WM8994_DRC_SWITCH(xname, reg, shift) \
362{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
363 .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,\
364 .put = wm8994_put_drc_sw, \
365 .private_value = SOC_SINGLE_VALUE(reg, shift, 1, 0) }
366
367static int wm8994_put_drc_sw(struct snd_kcontrol *kcontrol,
368 struct snd_ctl_elem_value *ucontrol)
369{
370 struct soc_mixer_control *mc =
371 (struct soc_mixer_control *)kcontrol->private_value;
372 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
373 int mask, ret;
374
375 /* Can't enable both ADC and DAC paths simultaneously */
376 if (mc->shift == WM8994_AIF1DAC1_DRC_ENA_SHIFT)
377 mask = WM8994_AIF1ADC1L_DRC_ENA_MASK |
378 WM8994_AIF1ADC1R_DRC_ENA_MASK;
379 else
380 mask = WM8994_AIF1DAC1_DRC_ENA_MASK;
381
382 ret = snd_soc_read(codec, mc->reg);
383 if (ret < 0)
384 return ret;
385 if (ret & mask)
386 return -EINVAL;
387
388 return snd_soc_put_volsw(kcontrol, ucontrol);
389}
390
Mark Brown9e6e96a2010-01-29 17:47:12 +0000391static void wm8994_set_drc(struct snd_soc_codec *codec, int drc)
392{
Mark Brownb2c812e2010-04-14 15:35:19 +0900393 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000394 struct wm8994_pdata *pdata = wm8994->pdata;
395 int base = wm8994_drc_base[drc];
396 int cfg = wm8994->drc_cfg[drc];
397 int save, i;
398
399 /* Save any enables; the configuration should clear them. */
400 save = snd_soc_read(codec, base);
401 save &= WM8994_AIF1DAC1_DRC_ENA | WM8994_AIF1ADC1L_DRC_ENA |
402 WM8994_AIF1ADC1R_DRC_ENA;
403
404 for (i = 0; i < WM8994_DRC_REGS; i++)
405 snd_soc_update_bits(codec, base + i, 0xffff,
406 pdata->drc_cfgs[cfg].regs[i]);
407
408 snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_DRC_ENA |
409 WM8994_AIF1ADC1L_DRC_ENA |
410 WM8994_AIF1ADC1R_DRC_ENA, save);
411}
412
413/* Icky as hell but saves code duplication */
414static int wm8994_get_drc(const char *name)
415{
416 if (strcmp(name, "AIF1DRC1 Mode") == 0)
417 return 0;
418 if (strcmp(name, "AIF1DRC2 Mode") == 0)
419 return 1;
420 if (strcmp(name, "AIF2DRC Mode") == 0)
421 return 2;
422 return -EINVAL;
423}
424
425static int wm8994_put_drc_enum(struct snd_kcontrol *kcontrol,
426 struct snd_ctl_elem_value *ucontrol)
427{
428 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000429 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000430 struct wm8994_pdata *pdata = wm8994->pdata;
431 int drc = wm8994_get_drc(kcontrol->id.name);
432 int value = ucontrol->value.integer.value[0];
433
434 if (drc < 0)
435 return drc;
436
437 if (value >= pdata->num_drc_cfgs)
438 return -EINVAL;
439
440 wm8994->drc_cfg[drc] = value;
441
442 wm8994_set_drc(codec, drc);
443
444 return 0;
445}
446
447static int wm8994_get_drc_enum(struct snd_kcontrol *kcontrol,
448 struct snd_ctl_elem_value *ucontrol)
449{
450 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +0900451 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000452 int drc = wm8994_get_drc(kcontrol->id.name);
453
454 ucontrol->value.enumerated.item[0] = wm8994->drc_cfg[drc];
455
456 return 0;
457}
458
459static void wm8994_set_retune_mobile(struct snd_soc_codec *codec, int block)
460{
Mark Brownb2c812e2010-04-14 15:35:19 +0900461 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000462 struct wm8994_pdata *pdata = wm8994->pdata;
463 int base = wm8994_retune_mobile_base[block];
464 int iface, best, best_val, save, i, cfg;
465
466 if (!pdata || !wm8994->num_retune_mobile_texts)
467 return;
468
469 switch (block) {
470 case 0:
471 case 1:
472 iface = 0;
473 break;
474 case 2:
475 iface = 1;
476 break;
477 default:
478 return;
479 }
480
481 /* Find the version of the currently selected configuration
482 * with the nearest sample rate. */
483 cfg = wm8994->retune_mobile_cfg[block];
484 best = 0;
485 best_val = INT_MAX;
486 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
487 if (strcmp(pdata->retune_mobile_cfgs[i].name,
488 wm8994->retune_mobile_texts[cfg]) == 0 &&
489 abs(pdata->retune_mobile_cfgs[i].rate
490 - wm8994->dac_rates[iface]) < best_val) {
491 best = i;
492 best_val = abs(pdata->retune_mobile_cfgs[i].rate
493 - wm8994->dac_rates[iface]);
494 }
495 }
496
497 dev_dbg(codec->dev, "ReTune Mobile %d %s/%dHz for %dHz sample rate\n",
498 block,
499 pdata->retune_mobile_cfgs[best].name,
500 pdata->retune_mobile_cfgs[best].rate,
501 wm8994->dac_rates[iface]);
502
503 /* The EQ will be disabled while reconfiguring it, remember the
504 * current configuration.
505 */
506 save = snd_soc_read(codec, base);
507 save &= WM8994_AIF1DAC1_EQ_ENA;
508
509 for (i = 0; i < WM8994_EQ_REGS; i++)
510 snd_soc_update_bits(codec, base + i, 0xffff,
511 pdata->retune_mobile_cfgs[best].regs[i]);
512
513 snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_EQ_ENA, save);
514}
515
516/* Icky as hell but saves code duplication */
517static int wm8994_get_retune_mobile_block(const char *name)
518{
519 if (strcmp(name, "AIF1.1 EQ Mode") == 0)
520 return 0;
521 if (strcmp(name, "AIF1.2 EQ Mode") == 0)
522 return 1;
523 if (strcmp(name, "AIF2 EQ Mode") == 0)
524 return 2;
525 return -EINVAL;
526}
527
528static int wm8994_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
529 struct snd_ctl_elem_value *ucontrol)
530{
531 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000532 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000533 struct wm8994_pdata *pdata = wm8994->pdata;
534 int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
535 int value = ucontrol->value.integer.value[0];
536
537 if (block < 0)
538 return block;
539
540 if (value >= pdata->num_retune_mobile_cfgs)
541 return -EINVAL;
542
543 wm8994->retune_mobile_cfg[block] = value;
544
545 wm8994_set_retune_mobile(codec, block);
546
547 return 0;
548}
549
550static int wm8994_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
551 struct snd_ctl_elem_value *ucontrol)
552{
553 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brown4a8d9292011-02-16 14:57:17 -0800554 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000555 int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
556
557 ucontrol->value.enumerated.item[0] = wm8994->retune_mobile_cfg[block];
558
559 return 0;
560}
561
Mark Brown96b101e2010-11-18 15:49:38 +0000562static const char *aif_chan_src_text[] = {
Mark Brownf5548852010-08-31 19:39:48 +0100563 "Left", "Right"
564};
565
Mark Brown96b101e2010-11-18 15:49:38 +0000566static const struct soc_enum aif1adcl_src =
567 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_1, 15, 2, aif_chan_src_text);
568
569static const struct soc_enum aif1adcr_src =
570 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_1, 14, 2, aif_chan_src_text);
571
572static const struct soc_enum aif2adcl_src =
573 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_1, 15, 2, aif_chan_src_text);
574
575static const struct soc_enum aif2adcr_src =
576 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_1, 14, 2, aif_chan_src_text);
577
Mark Brownf5548852010-08-31 19:39:48 +0100578static const struct soc_enum aif1dacl_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000579 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_2, 15, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100580
581static const struct soc_enum aif1dacr_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000582 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_2, 14, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100583
584static const struct soc_enum aif2dacl_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000585 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_2, 15, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100586
587static const struct soc_enum aif2dacr_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000588 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_2, 14, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100589
Mark Brown154b26a2010-12-09 12:07:44 +0000590static const char *osr_text[] = {
591 "Low Power", "High Performance",
592};
593
594static const struct soc_enum dac_osr =
595 SOC_ENUM_SINGLE(WM8994_OVERSAMPLING, 0, 2, osr_text);
596
597static const struct soc_enum adc_osr =
598 SOC_ENUM_SINGLE(WM8994_OVERSAMPLING, 1, 2, osr_text);
599
Mark Brown9e6e96a2010-01-29 17:47:12 +0000600static const struct snd_kcontrol_new wm8994_snd_controls[] = {
601SOC_DOUBLE_R_TLV("AIF1ADC1 Volume", WM8994_AIF1_ADC1_LEFT_VOLUME,
602 WM8994_AIF1_ADC1_RIGHT_VOLUME,
603 1, 119, 0, digital_tlv),
604SOC_DOUBLE_R_TLV("AIF1ADC2 Volume", WM8994_AIF1_ADC2_LEFT_VOLUME,
605 WM8994_AIF1_ADC2_RIGHT_VOLUME,
606 1, 119, 0, digital_tlv),
607SOC_DOUBLE_R_TLV("AIF2ADC Volume", WM8994_AIF2_ADC_LEFT_VOLUME,
608 WM8994_AIF2_ADC_RIGHT_VOLUME,
609 1, 119, 0, digital_tlv),
610
Mark Brown96b101e2010-11-18 15:49:38 +0000611SOC_ENUM("AIF1ADCL Source", aif1adcl_src),
612SOC_ENUM("AIF1ADCR Source", aif1adcr_src),
Mark Brown49db7e72010-12-08 13:49:43 +0000613SOC_ENUM("AIF2ADCL Source", aif2adcl_src),
614SOC_ENUM("AIF2ADCR Source", aif2adcr_src),
Mark Brown96b101e2010-11-18 15:49:38 +0000615
Mark Brownf5548852010-08-31 19:39:48 +0100616SOC_ENUM("AIF1DACL Source", aif1dacl_src),
617SOC_ENUM("AIF1DACR Source", aif1dacr_src),
Mark Brown49db7e72010-12-08 13:49:43 +0000618SOC_ENUM("AIF2DACL Source", aif2dacl_src),
619SOC_ENUM("AIF2DACR Source", aif2dacr_src),
Mark Brownf5548852010-08-31 19:39:48 +0100620
Mark Brown9e6e96a2010-01-29 17:47:12 +0000621SOC_DOUBLE_R_TLV("AIF1DAC1 Volume", WM8994_AIF1_DAC1_LEFT_VOLUME,
622 WM8994_AIF1_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
623SOC_DOUBLE_R_TLV("AIF1DAC2 Volume", WM8994_AIF1_DAC2_LEFT_VOLUME,
624 WM8994_AIF1_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
625SOC_DOUBLE_R_TLV("AIF2DAC Volume", WM8994_AIF2_DAC_LEFT_VOLUME,
626 WM8994_AIF2_DAC_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
627
628SOC_SINGLE_TLV("AIF1 Boost Volume", WM8994_AIF1_CONTROL_2, 10, 3, 0, aif_tlv),
629SOC_SINGLE_TLV("AIF2 Boost Volume", WM8994_AIF2_CONTROL_2, 10, 3, 0, aif_tlv),
630
631SOC_SINGLE("AIF1DAC1 EQ Switch", WM8994_AIF1_DAC1_EQ_GAINS_1, 0, 1, 0),
632SOC_SINGLE("AIF1DAC2 EQ Switch", WM8994_AIF1_DAC2_EQ_GAINS_1, 0, 1, 0),
633SOC_SINGLE("AIF2 EQ Switch", WM8994_AIF2_EQ_GAINS_1, 0, 1, 0),
634
635WM8994_DRC_SWITCH("AIF1DAC1 DRC Switch", WM8994_AIF1_DRC1_1, 2),
636WM8994_DRC_SWITCH("AIF1ADC1L DRC Switch", WM8994_AIF1_DRC1_1, 1),
637WM8994_DRC_SWITCH("AIF1ADC1R DRC Switch", WM8994_AIF1_DRC1_1, 0),
638
639WM8994_DRC_SWITCH("AIF1DAC2 DRC Switch", WM8994_AIF1_DRC2_1, 2),
640WM8994_DRC_SWITCH("AIF1ADC2L DRC Switch", WM8994_AIF1_DRC2_1, 1),
641WM8994_DRC_SWITCH("AIF1ADC2R DRC Switch", WM8994_AIF1_DRC2_1, 0),
642
643WM8994_DRC_SWITCH("AIF2DAC DRC Switch", WM8994_AIF2_DRC_1, 2),
644WM8994_DRC_SWITCH("AIF2ADCL DRC Switch", WM8994_AIF2_DRC_1, 1),
645WM8994_DRC_SWITCH("AIF2ADCR DRC Switch", WM8994_AIF2_DRC_1, 0),
646
647SOC_SINGLE_TLV("DAC1 Right Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
648 5, 12, 0, st_tlv),
649SOC_SINGLE_TLV("DAC1 Left Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
650 0, 12, 0, st_tlv),
651SOC_SINGLE_TLV("DAC2 Right Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
652 5, 12, 0, st_tlv),
653SOC_SINGLE_TLV("DAC2 Left Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
654 0, 12, 0, st_tlv),
655SOC_ENUM("Sidetone HPF Mux", sidetone_hpf),
656SOC_SINGLE("Sidetone HPF Switch", WM8994_SIDETONE, 6, 1, 0),
657
Uk Kim146fd572010-12-07 13:58:40 +0000658SOC_ENUM("AIF1ADC1 HPF Mode", aif1adc1_hpf),
659SOC_DOUBLE("AIF1ADC1 HPF Switch", WM8994_AIF1_ADC1_FILTERS, 12, 11, 1, 0),
660
661SOC_ENUM("AIF1ADC2 HPF Mode", aif1adc2_hpf),
662SOC_DOUBLE("AIF1ADC2 HPF Switch", WM8994_AIF1_ADC2_FILTERS, 12, 11, 1, 0),
663
664SOC_ENUM("AIF2ADC HPF Mode", aif2adc_hpf),
665SOC_DOUBLE("AIF2ADC HPF Switch", WM8994_AIF2_ADC_FILTERS, 12, 11, 1, 0),
666
Mark Brown154b26a2010-12-09 12:07:44 +0000667SOC_ENUM("ADC OSR", adc_osr),
668SOC_ENUM("DAC OSR", dac_osr),
669
Mark Brown9e6e96a2010-01-29 17:47:12 +0000670SOC_DOUBLE_R_TLV("DAC1 Volume", WM8994_DAC1_LEFT_VOLUME,
671 WM8994_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
672SOC_DOUBLE_R("DAC1 Switch", WM8994_DAC1_LEFT_VOLUME,
673 WM8994_DAC1_RIGHT_VOLUME, 9, 1, 1),
674
675SOC_DOUBLE_R_TLV("DAC2 Volume", WM8994_DAC2_LEFT_VOLUME,
676 WM8994_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
677SOC_DOUBLE_R("DAC2 Switch", WM8994_DAC2_LEFT_VOLUME,
678 WM8994_DAC2_RIGHT_VOLUME, 9, 1, 1),
679
680SOC_SINGLE_TLV("SPKL DAC2 Volume", WM8994_SPKMIXL_ATTENUATION,
681 6, 1, 1, wm_hubs_spkmix_tlv),
682SOC_SINGLE_TLV("SPKL DAC1 Volume", WM8994_SPKMIXL_ATTENUATION,
683 2, 1, 1, wm_hubs_spkmix_tlv),
684
685SOC_SINGLE_TLV("SPKR DAC2 Volume", WM8994_SPKMIXR_ATTENUATION,
686 6, 1, 1, wm_hubs_spkmix_tlv),
687SOC_SINGLE_TLV("SPKR DAC1 Volume", WM8994_SPKMIXR_ATTENUATION,
688 2, 1, 1, wm_hubs_spkmix_tlv),
689
690SOC_SINGLE_TLV("AIF1DAC1 3D Stereo Volume", WM8994_AIF1_DAC1_FILTERS_2,
691 10, 15, 0, wm8994_3d_tlv),
Mark Brown458350b2010-12-20 14:35:09 +0000692SOC_SINGLE("AIF1DAC1 3D Stereo Switch", WM8994_AIF1_DAC1_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000693 8, 1, 0),
694SOC_SINGLE_TLV("AIF1DAC2 3D Stereo Volume", WM8994_AIF1_DAC2_FILTERS_2,
695 10, 15, 0, wm8994_3d_tlv),
696SOC_SINGLE("AIF1DAC2 3D Stereo Switch", WM8994_AIF1_DAC2_FILTERS_2,
697 8, 1, 0),
Mark Brown458350b2010-12-20 14:35:09 +0000698SOC_SINGLE_TLV("AIF2DAC 3D Stereo Volume", WM8994_AIF2_DAC_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000699 10, 15, 0, wm8994_3d_tlv),
Mark Brown458350b2010-12-20 14:35:09 +0000700SOC_SINGLE("AIF2DAC 3D Stereo Switch", WM8994_AIF2_DAC_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000701 8, 1, 0),
702};
703
704static const struct snd_kcontrol_new wm8994_eq_controls[] = {
705SOC_SINGLE_TLV("AIF1DAC1 EQ1 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 11, 31, 0,
706 eq_tlv),
707SOC_SINGLE_TLV("AIF1DAC1 EQ2 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 6, 31, 0,
708 eq_tlv),
709SOC_SINGLE_TLV("AIF1DAC1 EQ3 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 1, 31, 0,
710 eq_tlv),
711SOC_SINGLE_TLV("AIF1DAC1 EQ4 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 11, 31, 0,
712 eq_tlv),
713SOC_SINGLE_TLV("AIF1DAC1 EQ5 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 6, 31, 0,
714 eq_tlv),
715
716SOC_SINGLE_TLV("AIF1DAC2 EQ1 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 11, 31, 0,
717 eq_tlv),
718SOC_SINGLE_TLV("AIF1DAC2 EQ2 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 6, 31, 0,
719 eq_tlv),
720SOC_SINGLE_TLV("AIF1DAC2 EQ3 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 1, 31, 0,
721 eq_tlv),
722SOC_SINGLE_TLV("AIF1DAC2 EQ4 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 11, 31, 0,
723 eq_tlv),
724SOC_SINGLE_TLV("AIF1DAC2 EQ5 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 6, 31, 0,
725 eq_tlv),
726
727SOC_SINGLE_TLV("AIF2 EQ1 Volume", WM8994_AIF2_EQ_GAINS_1, 11, 31, 0,
728 eq_tlv),
729SOC_SINGLE_TLV("AIF2 EQ2 Volume", WM8994_AIF2_EQ_GAINS_1, 6, 31, 0,
730 eq_tlv),
731SOC_SINGLE_TLV("AIF2 EQ3 Volume", WM8994_AIF2_EQ_GAINS_1, 1, 31, 0,
732 eq_tlv),
733SOC_SINGLE_TLV("AIF2 EQ4 Volume", WM8994_AIF2_EQ_GAINS_2, 11, 31, 0,
734 eq_tlv),
735SOC_SINGLE_TLV("AIF2 EQ5 Volume", WM8994_AIF2_EQ_GAINS_2, 6, 31, 0,
736 eq_tlv),
737};
738
Mark Brown1ddc07d2011-08-16 10:08:48 +0900739static const char *wm8958_ng_text[] = {
740 "30ms", "125ms", "250ms", "500ms",
741};
742
743static const struct soc_enum wm8958_aif1dac1_ng_hold =
744 SOC_ENUM_SINGLE(WM8958_AIF1_DAC1_NOISE_GATE,
745 WM8958_AIF1DAC1_NG_THR_SHIFT, 4, wm8958_ng_text);
746
747static const struct soc_enum wm8958_aif1dac2_ng_hold =
748 SOC_ENUM_SINGLE(WM8958_AIF1_DAC2_NOISE_GATE,
749 WM8958_AIF1DAC2_NG_THR_SHIFT, 4, wm8958_ng_text);
750
751static const struct soc_enum wm8958_aif2dac_ng_hold =
752 SOC_ENUM_SINGLE(WM8958_AIF2_DAC_NOISE_GATE,
753 WM8958_AIF2DAC_NG_THR_SHIFT, 4, wm8958_ng_text);
754
Mark Brownc4431df2010-11-26 15:21:07 +0000755static const struct snd_kcontrol_new wm8958_snd_controls[] = {
756SOC_SINGLE_TLV("AIF3 Boost Volume", WM8958_AIF3_CONTROL_2, 10, 3, 0, aif_tlv),
Mark Brown1ddc07d2011-08-16 10:08:48 +0900757
758SOC_SINGLE("AIF1DAC1 Noise Gate Switch", WM8958_AIF1_DAC1_NOISE_GATE,
759 WM8958_AIF1DAC1_NG_ENA_SHIFT, 1, 0),
760SOC_ENUM("AIF1DAC1 Noise Gate Hold Time", wm8958_aif1dac1_ng_hold),
761SOC_SINGLE_TLV("AIF1DAC1 Noise Gate Threshold Volume",
762 WM8958_AIF1_DAC1_NOISE_GATE, WM8958_AIF1DAC1_NG_THR_SHIFT,
763 7, 1, ng_tlv),
764
765SOC_SINGLE("AIF1DAC2 Noise Gate Switch", WM8958_AIF1_DAC2_NOISE_GATE,
766 WM8958_AIF1DAC2_NG_ENA_SHIFT, 1, 0),
767SOC_ENUM("AIF1DAC2 Noise Gate Hold Time", wm8958_aif1dac2_ng_hold),
768SOC_SINGLE_TLV("AIF1DAC2 Noise Gate Threshold Volume",
769 WM8958_AIF1_DAC2_NOISE_GATE, WM8958_AIF1DAC2_NG_THR_SHIFT,
770 7, 1, ng_tlv),
771
772SOC_SINGLE("AIF2DAC Noise Gate Switch", WM8958_AIF2_DAC_NOISE_GATE,
773 WM8958_AIF2DAC_NG_ENA_SHIFT, 1, 0),
774SOC_ENUM("AIF2DAC Noise Gate Hold Time", wm8958_aif2dac_ng_hold),
775SOC_SINGLE_TLV("AIF2DAC Noise Gate Threshold Volume",
776 WM8958_AIF2_DAC_NOISE_GATE, WM8958_AIF2DAC_NG_THR_SHIFT,
777 7, 1, ng_tlv),
Mark Brownc4431df2010-11-26 15:21:07 +0000778};
779
Mark Brown81204c82011-05-24 17:35:53 +0800780static const struct snd_kcontrol_new wm1811_snd_controls[] = {
781SOC_SINGLE_TLV("MIXINL IN1LP Boost Volume", WM8994_INPUT_MIXER_1, 7, 1, 0,
782 mixin_boost_tlv),
783SOC_SINGLE_TLV("MIXINL IN1RP Boost Volume", WM8994_INPUT_MIXER_1, 8, 1, 0,
784 mixin_boost_tlv),
785};
786
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000787/* We run all mode setting through a function to enforce audio mode */
788static void wm1811_jackdet_set_mode(struct snd_soc_codec *codec, u16 mode)
789{
790 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
791
792 if (wm8994->active_refcount)
793 mode = WM1811_JACKDET_MODE_AUDIO;
794
795 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
796 WM1811_JACKDET_MODE_MASK, mode);
797
798 if (mode == WM1811_JACKDET_MODE_MIC)
799 msleep(2);
800}
801
802static void active_reference(struct snd_soc_codec *codec)
803{
804 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
805
806 mutex_lock(&wm8994->accdet_lock);
807
808 wm8994->active_refcount++;
809
810 dev_dbg(codec->dev, "Active refcount incremented, now %d\n",
811 wm8994->active_refcount);
812
813 if (wm8994->active_refcount == 1) {
814 /* If we're using jack detection go into audio mode */
815 if (wm8994->jackdet && wm8994->jack_cb) {
816 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
817 WM1811_JACKDET_MODE_MASK,
818 WM1811_JACKDET_MODE_AUDIO);
819 msleep(2);
820 }
821 }
822
823 mutex_unlock(&wm8994->accdet_lock);
824}
825
826static void active_dereference(struct snd_soc_codec *codec)
827{
828 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
829 u16 mode;
830
831 mutex_lock(&wm8994->accdet_lock);
832
833 wm8994->active_refcount--;
834
835 dev_dbg(codec->dev, "Active refcount decremented, now %d\n",
836 wm8994->active_refcount);
837
838 if (wm8994->active_refcount == 0) {
839 /* Go into appropriate detection only mode */
840 if (wm8994->jackdet && wm8994->jack_cb) {
841 if (wm8994->jack_mic || wm8994->mic_detecting)
842 mode = WM1811_JACKDET_MODE_MIC;
843 else
844 mode = WM1811_JACKDET_MODE_JACK;
845
846 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
847 WM1811_JACKDET_MODE_MASK,
848 mode);
849 }
850 }
851
852 mutex_unlock(&wm8994->accdet_lock);
853}
854
Mark Brown9e6e96a2010-01-29 17:47:12 +0000855static int clk_sys_event(struct snd_soc_dapm_widget *w,
856 struct snd_kcontrol *kcontrol, int event)
857{
858 struct snd_soc_codec *codec = w->codec;
859
860 switch (event) {
861 case SND_SOC_DAPM_PRE_PMU:
862 return configure_clock(codec);
863
864 case SND_SOC_DAPM_POST_PMD:
865 configure_clock(codec);
866 break;
867 }
868
869 return 0;
870}
871
Mark Brown4b7ed832011-08-10 17:47:33 +0900872static void vmid_reference(struct snd_soc_codec *codec)
873{
874 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
875
876 wm8994->vmid_refcount++;
877
878 dev_dbg(codec->dev, "Referencing VMID, refcount is now %d\n",
879 wm8994->vmid_refcount);
880
881 if (wm8994->vmid_refcount == 1) {
882 /* Startup bias, VMID ramp & buffer */
883 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
884 WM8994_STARTUP_BIAS_ENA |
885 WM8994_VMID_BUF_ENA |
886 WM8994_VMID_RAMP_MASK,
887 WM8994_STARTUP_BIAS_ENA |
888 WM8994_VMID_BUF_ENA |
889 (0x11 << WM8994_VMID_RAMP_SHIFT));
890
891 /* Main bias enable, VMID=2x40k */
892 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
893 WM8994_BIAS_ENA |
894 WM8994_VMID_SEL_MASK,
895 WM8994_BIAS_ENA | 0x2);
896
897 msleep(20);
898 }
899}
900
901static void vmid_dereference(struct snd_soc_codec *codec)
902{
903 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
904
905 wm8994->vmid_refcount--;
906
907 dev_dbg(codec->dev, "Dereferencing VMID, refcount is now %d\n",
908 wm8994->vmid_refcount);
909
910 if (wm8994->vmid_refcount == 0) {
911 /* Switch over to startup biases */
912 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
913 WM8994_BIAS_SRC |
914 WM8994_STARTUP_BIAS_ENA |
915 WM8994_VMID_BUF_ENA |
916 WM8994_VMID_RAMP_MASK,
917 WM8994_BIAS_SRC |
918 WM8994_STARTUP_BIAS_ENA |
919 WM8994_VMID_BUF_ENA |
920 (1 << WM8994_VMID_RAMP_SHIFT));
921
922 /* Disable main biases */
923 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
924 WM8994_BIAS_ENA |
925 WM8994_VMID_SEL_MASK, 0);
926
927 /* Discharge line */
928 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
929 WM8994_LINEOUT1_DISCH |
930 WM8994_LINEOUT2_DISCH,
931 WM8994_LINEOUT1_DISCH |
932 WM8994_LINEOUT2_DISCH);
933
934 msleep(5);
935
936 /* Switch off startup biases */
937 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
938 WM8994_BIAS_SRC |
939 WM8994_STARTUP_BIAS_ENA |
940 WM8994_VMID_BUF_ENA |
941 WM8994_VMID_RAMP_MASK, 0);
942 }
943}
944
945static int vmid_event(struct snd_soc_dapm_widget *w,
946 struct snd_kcontrol *kcontrol, int event)
947{
948 struct snd_soc_codec *codec = w->codec;
949
950 switch (event) {
951 case SND_SOC_DAPM_PRE_PMU:
952 vmid_reference(codec);
953 break;
954
955 case SND_SOC_DAPM_POST_PMD:
956 vmid_dereference(codec);
957 break;
958 }
959
960 return 0;
961}
962
Mark Brown9e6e96a2010-01-29 17:47:12 +0000963static void wm8994_update_class_w(struct snd_soc_codec *codec)
964{
Mark Brownfec6dd82010-10-27 13:48:36 -0700965 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000966 int enable = 1;
967 int source = 0; /* GCC flow analysis can't track enable */
968 int reg, reg_r;
969
970 /* Only support direct DAC->headphone paths */
971 reg = snd_soc_read(codec, WM8994_OUTPUT_MIXER_1);
972 if (!(reg & WM8994_DAC1L_TO_HPOUT1L)) {
Mark Brownee839a22010-04-20 13:57:08 +0900973 dev_vdbg(codec->dev, "HPL connected to output mixer\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000974 enable = 0;
975 }
976
977 reg = snd_soc_read(codec, WM8994_OUTPUT_MIXER_2);
978 if (!(reg & WM8994_DAC1R_TO_HPOUT1R)) {
Mark Brownee839a22010-04-20 13:57:08 +0900979 dev_vdbg(codec->dev, "HPR connected to output mixer\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000980 enable = 0;
981 }
982
983 /* We also need the same setting for L/R and only one path */
984 reg = snd_soc_read(codec, WM8994_DAC1_LEFT_MIXER_ROUTING);
985 switch (reg) {
986 case WM8994_AIF2DACL_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +0900987 dev_vdbg(codec->dev, "Class W source AIF2DAC\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000988 source = 2 << WM8994_CP_DYN_SRC_SEL_SHIFT;
989 break;
990 case WM8994_AIF1DAC2L_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +0900991 dev_vdbg(codec->dev, "Class W source AIF1DAC2\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000992 source = 1 << WM8994_CP_DYN_SRC_SEL_SHIFT;
993 break;
994 case WM8994_AIF1DAC1L_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +0900995 dev_vdbg(codec->dev, "Class W source AIF1DAC1\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000996 source = 0 << WM8994_CP_DYN_SRC_SEL_SHIFT;
997 break;
998 default:
Mark Brownee839a22010-04-20 13:57:08 +0900999 dev_vdbg(codec->dev, "DAC mixer setting: %x\n", reg);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001000 enable = 0;
1001 break;
1002 }
1003
1004 reg_r = snd_soc_read(codec, WM8994_DAC1_RIGHT_MIXER_ROUTING);
1005 if (reg_r != reg) {
Mark Brownee839a22010-04-20 13:57:08 +09001006 dev_vdbg(codec->dev, "Left and right DAC mixers different\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +00001007 enable = 0;
1008 }
1009
1010 if (enable) {
1011 dev_dbg(codec->dev, "Class W enabled\n");
1012 snd_soc_update_bits(codec, WM8994_CLASS_W_1,
1013 WM8994_CP_DYN_PWR |
1014 WM8994_CP_DYN_SRC_SEL_MASK,
1015 source | WM8994_CP_DYN_PWR);
Mark Brownfec6dd82010-10-27 13:48:36 -07001016 wm8994->hubs.class_w = true;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001017
1018 } else {
1019 dev_dbg(codec->dev, "Class W disabled\n");
1020 snd_soc_update_bits(codec, WM8994_CLASS_W_1,
1021 WM8994_CP_DYN_PWR, 0);
Mark Brownfec6dd82010-10-27 13:48:36 -07001022 wm8994->hubs.class_w = false;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001023 }
1024}
1025
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001026static int late_enable_ev(struct snd_soc_dapm_widget *w,
1027 struct snd_kcontrol *kcontrol, int event)
1028{
1029 struct snd_soc_codec *codec = w->codec;
1030 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1031
1032 switch (event) {
1033 case SND_SOC_DAPM_PRE_PMU:
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001034 if (wm8994->aif1clk_enable) {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001035 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1036 WM8994_AIF1CLK_ENA_MASK,
1037 WM8994_AIF1CLK_ENA);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001038 wm8994->aif1clk_enable = 0;
1039 }
1040 if (wm8994->aif2clk_enable) {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001041 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1042 WM8994_AIF2CLK_ENA_MASK,
1043 WM8994_AIF2CLK_ENA);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001044 wm8994->aif2clk_enable = 0;
1045 }
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001046 break;
1047 }
1048
Mark Brownc6b7b572011-03-11 18:13:12 +00001049 /* We may also have postponed startup of DSP, handle that. */
1050 wm8958_aif_ev(w, kcontrol, event);
1051
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001052 return 0;
1053}
1054
1055static int late_disable_ev(struct snd_soc_dapm_widget *w,
1056 struct snd_kcontrol *kcontrol, int event)
1057{
1058 struct snd_soc_codec *codec = w->codec;
1059 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1060
1061 switch (event) {
1062 case SND_SOC_DAPM_POST_PMD:
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001063 if (wm8994->aif1clk_disable) {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001064 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1065 WM8994_AIF1CLK_ENA_MASK, 0);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001066 wm8994->aif1clk_disable = 0;
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001067 }
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001068 if (wm8994->aif2clk_disable) {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001069 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1070 WM8994_AIF2CLK_ENA_MASK, 0);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001071 wm8994->aif2clk_disable = 0;
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001072 }
1073 break;
1074 }
1075
1076 return 0;
1077}
1078
1079static int aif1clk_ev(struct snd_soc_dapm_widget *w,
1080 struct snd_kcontrol *kcontrol, int event)
1081{
1082 struct snd_soc_codec *codec = w->codec;
1083 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1084
1085 switch (event) {
1086 case SND_SOC_DAPM_PRE_PMU:
1087 wm8994->aif1clk_enable = 1;
1088 break;
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001089 case SND_SOC_DAPM_POST_PMD:
1090 wm8994->aif1clk_disable = 1;
1091 break;
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001092 }
1093
1094 return 0;
1095}
1096
1097static int aif2clk_ev(struct snd_soc_dapm_widget *w,
1098 struct snd_kcontrol *kcontrol, int event)
1099{
1100 struct snd_soc_codec *codec = w->codec;
1101 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1102
1103 switch (event) {
1104 case SND_SOC_DAPM_PRE_PMU:
1105 wm8994->aif2clk_enable = 1;
1106 break;
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001107 case SND_SOC_DAPM_POST_PMD:
1108 wm8994->aif2clk_disable = 1;
1109 break;
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001110 }
1111
1112 return 0;
1113}
1114
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001115static int adc_mux_ev(struct snd_soc_dapm_widget *w,
1116 struct snd_kcontrol *kcontrol, int event)
1117{
1118 late_enable_ev(w, kcontrol, event);
1119 return 0;
1120}
1121
Dimitris Papastamosb462c6e2011-03-01 12:54:39 +00001122static int micbias_ev(struct snd_soc_dapm_widget *w,
1123 struct snd_kcontrol *kcontrol, int event)
1124{
1125 late_enable_ev(w, kcontrol, event);
1126 return 0;
1127}
1128
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001129static int dac_ev(struct snd_soc_dapm_widget *w,
1130 struct snd_kcontrol *kcontrol, int event)
1131{
1132 struct snd_soc_codec *codec = w->codec;
1133 unsigned int mask = 1 << w->shift;
1134
1135 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1136 mask, mask);
1137 return 0;
1138}
1139
Mark Brown9e6e96a2010-01-29 17:47:12 +00001140static const char *hp_mux_text[] = {
1141 "Mixer",
1142 "DAC",
1143};
1144
1145#define WM8994_HP_ENUM(xname, xenum) \
1146{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1147 .info = snd_soc_info_enum_double, \
1148 .get = snd_soc_dapm_get_enum_double, \
1149 .put = wm8994_put_hp_enum, \
1150 .private_value = (unsigned long)&xenum }
1151
1152static int wm8994_put_hp_enum(struct snd_kcontrol *kcontrol,
1153 struct snd_ctl_elem_value *ucontrol)
1154{
Jarkko Nikula9d035452011-05-13 19:16:52 +03001155 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1156 struct snd_soc_dapm_widget *w = wlist->widgets[0];
Mark Brown9e6e96a2010-01-29 17:47:12 +00001157 struct snd_soc_codec *codec = w->codec;
1158 int ret;
1159
1160 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
1161
1162 wm8994_update_class_w(codec);
1163
1164 return ret;
1165}
1166
1167static const struct soc_enum hpl_enum =
1168 SOC_ENUM_SINGLE(WM8994_OUTPUT_MIXER_1, 8, 2, hp_mux_text);
1169
1170static const struct snd_kcontrol_new hpl_mux =
1171 WM8994_HP_ENUM("Left Headphone Mux", hpl_enum);
1172
1173static const struct soc_enum hpr_enum =
1174 SOC_ENUM_SINGLE(WM8994_OUTPUT_MIXER_2, 8, 2, hp_mux_text);
1175
1176static const struct snd_kcontrol_new hpr_mux =
1177 WM8994_HP_ENUM("Right Headphone Mux", hpr_enum);
1178
1179static const char *adc_mux_text[] = {
1180 "ADC",
1181 "DMIC",
1182};
1183
1184static const struct soc_enum adc_enum =
1185 SOC_ENUM_SINGLE(0, 0, 2, adc_mux_text);
1186
1187static const struct snd_kcontrol_new adcl_mux =
1188 SOC_DAPM_ENUM_VIRT("ADCL Mux", adc_enum);
1189
1190static const struct snd_kcontrol_new adcr_mux =
1191 SOC_DAPM_ENUM_VIRT("ADCR Mux", adc_enum);
1192
1193static const struct snd_kcontrol_new left_speaker_mixer[] = {
1194SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 9, 1, 0),
1195SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 7, 1, 0),
1196SOC_DAPM_SINGLE("IN1LP Switch", WM8994_SPEAKER_MIXER, 5, 1, 0),
1197SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 3, 1, 0),
1198SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 1, 1, 0),
1199};
1200
1201static const struct snd_kcontrol_new right_speaker_mixer[] = {
1202SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 8, 1, 0),
1203SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 6, 1, 0),
1204SOC_DAPM_SINGLE("IN1RP Switch", WM8994_SPEAKER_MIXER, 4, 1, 0),
1205SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 2, 1, 0),
1206SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 0, 1, 0),
1207};
1208
1209/* Debugging; dump chip status after DAPM transitions */
1210static int post_ev(struct snd_soc_dapm_widget *w,
1211 struct snd_kcontrol *kcontrol, int event)
1212{
1213 struct snd_soc_codec *codec = w->codec;
1214 dev_dbg(codec->dev, "SRC status: %x\n",
1215 snd_soc_read(codec,
1216 WM8994_RATE_STATUS));
1217 return 0;
1218}
1219
1220static const struct snd_kcontrol_new aif1adc1l_mix[] = {
1221SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1222 1, 1, 0),
1223SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1224 0, 1, 0),
1225};
1226
1227static const struct snd_kcontrol_new aif1adc1r_mix[] = {
1228SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1229 1, 1, 0),
1230SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1231 0, 1, 0),
1232};
1233
Mark Browna3257ba2010-07-19 14:02:34 +01001234static const struct snd_kcontrol_new aif1adc2l_mix[] = {
1235SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1236 1, 1, 0),
1237SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1238 0, 1, 0),
1239};
1240
1241static const struct snd_kcontrol_new aif1adc2r_mix[] = {
1242SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1243 1, 1, 0),
1244SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1245 0, 1, 0),
1246};
1247
Mark Brown9e6e96a2010-01-29 17:47:12 +00001248static const struct snd_kcontrol_new aif2dac2l_mix[] = {
1249SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1250 5, 1, 0),
1251SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1252 4, 1, 0),
1253SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1254 2, 1, 0),
1255SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1256 1, 1, 0),
1257SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1258 0, 1, 0),
1259};
1260
1261static const struct snd_kcontrol_new aif2dac2r_mix[] = {
1262SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1263 5, 1, 0),
1264SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1265 4, 1, 0),
1266SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1267 2, 1, 0),
1268SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1269 1, 1, 0),
1270SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1271 0, 1, 0),
1272};
1273
1274#define WM8994_CLASS_W_SWITCH(xname, reg, shift, max, invert) \
1275{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1276 .info = snd_soc_info_volsw, \
1277 .get = snd_soc_dapm_get_volsw, .put = wm8994_put_class_w, \
1278 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
1279
1280static int wm8994_put_class_w(struct snd_kcontrol *kcontrol,
1281 struct snd_ctl_elem_value *ucontrol)
1282{
Jarkko Nikula9d035452011-05-13 19:16:52 +03001283 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
1284 struct snd_soc_dapm_widget *w = wlist->widgets[0];
Mark Brown9e6e96a2010-01-29 17:47:12 +00001285 struct snd_soc_codec *codec = w->codec;
1286 int ret;
1287
1288 ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
1289
1290 wm8994_update_class_w(codec);
1291
1292 return ret;
1293}
1294
1295static const struct snd_kcontrol_new dac1l_mix[] = {
1296WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1297 5, 1, 0),
1298WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1299 4, 1, 0),
1300WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1301 2, 1, 0),
1302WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1303 1, 1, 0),
1304WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1305 0, 1, 0),
1306};
1307
1308static const struct snd_kcontrol_new dac1r_mix[] = {
1309WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1310 5, 1, 0),
1311WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1312 4, 1, 0),
1313WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1314 2, 1, 0),
1315WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1316 1, 1, 0),
1317WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1318 0, 1, 0),
1319};
1320
1321static const char *sidetone_text[] = {
1322 "ADC/DMIC1", "DMIC2",
1323};
1324
1325static const struct soc_enum sidetone1_enum =
1326 SOC_ENUM_SINGLE(WM8994_SIDETONE, 0, 2, sidetone_text);
1327
1328static const struct snd_kcontrol_new sidetone1_mux =
1329 SOC_DAPM_ENUM("Left Sidetone Mux", sidetone1_enum);
1330
1331static const struct soc_enum sidetone2_enum =
1332 SOC_ENUM_SINGLE(WM8994_SIDETONE, 1, 2, sidetone_text);
1333
1334static const struct snd_kcontrol_new sidetone2_mux =
1335 SOC_DAPM_ENUM("Right Sidetone Mux", sidetone2_enum);
1336
1337static const char *aif1dac_text[] = {
1338 "AIF1DACDAT", "AIF3DACDAT",
1339};
1340
1341static const struct soc_enum aif1dac_enum =
1342 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 0, 2, aif1dac_text);
1343
1344static const struct snd_kcontrol_new aif1dac_mux =
1345 SOC_DAPM_ENUM("AIF1DAC Mux", aif1dac_enum);
1346
1347static const char *aif2dac_text[] = {
1348 "AIF2DACDAT", "AIF3DACDAT",
1349};
1350
1351static const struct soc_enum aif2dac_enum =
1352 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 1, 2, aif2dac_text);
1353
1354static const struct snd_kcontrol_new aif2dac_mux =
1355 SOC_DAPM_ENUM("AIF2DAC Mux", aif2dac_enum);
1356
1357static const char *aif2adc_text[] = {
1358 "AIF2ADCDAT", "AIF3DACDAT",
1359};
1360
1361static const struct soc_enum aif2adc_enum =
1362 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 2, 2, aif2adc_text);
1363
1364static const struct snd_kcontrol_new aif2adc_mux =
1365 SOC_DAPM_ENUM("AIF2ADC Mux", aif2adc_enum);
1366
1367static const char *aif3adc_text[] = {
Mark Brownc4431df2010-11-26 15:21:07 +00001368 "AIF1ADCDAT", "AIF2ADCDAT", "AIF2DACDAT", "Mono PCM",
Mark Brown9e6e96a2010-01-29 17:47:12 +00001369};
1370
Mark Brownc4431df2010-11-26 15:21:07 +00001371static const struct soc_enum wm8994_aif3adc_enum =
Mark Brown9e6e96a2010-01-29 17:47:12 +00001372 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 3, 3, aif3adc_text);
1373
Mark Brownc4431df2010-11-26 15:21:07 +00001374static const struct snd_kcontrol_new wm8994_aif3adc_mux =
1375 SOC_DAPM_ENUM("AIF3ADC Mux", wm8994_aif3adc_enum);
1376
1377static const struct soc_enum wm8958_aif3adc_enum =
1378 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 3, 4, aif3adc_text);
1379
1380static const struct snd_kcontrol_new wm8958_aif3adc_mux =
1381 SOC_DAPM_ENUM("AIF3ADC Mux", wm8958_aif3adc_enum);
1382
1383static const char *mono_pcm_out_text[] = {
1384 "None", "AIF2ADCL", "AIF2ADCR",
1385};
1386
1387static const struct soc_enum mono_pcm_out_enum =
1388 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 9, 3, mono_pcm_out_text);
1389
1390static const struct snd_kcontrol_new mono_pcm_out_mux =
1391 SOC_DAPM_ENUM("Mono PCM Out Mux", mono_pcm_out_enum);
1392
1393static const char *aif2dac_src_text[] = {
1394 "AIF2", "AIF3",
1395};
1396
1397/* Note that these two control shouldn't be simultaneously switched to AIF3 */
1398static const struct soc_enum aif2dacl_src_enum =
1399 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 7, 2, aif2dac_src_text);
1400
1401static const struct snd_kcontrol_new aif2dacl_src_mux =
1402 SOC_DAPM_ENUM("AIF2DACL Mux", aif2dacl_src_enum);
1403
1404static const struct soc_enum aif2dacr_src_enum =
1405 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 8, 2, aif2dac_src_text);
1406
1407static const struct snd_kcontrol_new aif2dacr_src_mux =
1408 SOC_DAPM_ENUM("AIF2DACR Mux", aif2dacr_src_enum);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001409
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001410static const struct snd_soc_dapm_widget wm8994_lateclk_revd_widgets[] = {
1411SND_SOC_DAPM_SUPPLY("AIF1CLK", SND_SOC_NOPM, 0, 0, aif1clk_ev,
1412 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1413SND_SOC_DAPM_SUPPLY("AIF2CLK", SND_SOC_NOPM, 0, 0, aif2clk_ev,
1414 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1415
1416SND_SOC_DAPM_PGA_E("Late DAC1L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1417 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1418SND_SOC_DAPM_PGA_E("Late DAC1R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1419 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1420SND_SOC_DAPM_PGA_E("Late DAC2L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1421 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1422SND_SOC_DAPM_PGA_E("Late DAC2R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1423 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Mark Brownb70a51b2011-06-29 00:21:09 -07001424SND_SOC_DAPM_PGA_E("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0,
1425 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1426
1427SND_SOC_DAPM_MIXER_E("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
1428 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer),
1429 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1430SND_SOC_DAPM_MIXER_E("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
1431 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer),
1432 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1433SND_SOC_DAPM_MUX_E("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &hpl_mux,
1434 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1435SND_SOC_DAPM_MUX_E("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &hpr_mux,
1436 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001437
1438SND_SOC_DAPM_POST("Late Disable PGA", late_disable_ev)
1439};
1440
1441static const struct snd_soc_dapm_widget wm8994_lateclk_widgets[] = {
1442SND_SOC_DAPM_SUPPLY("AIF1CLK", WM8994_AIF1_CLOCKING_1, 0, 0, NULL, 0),
Mark Brownb70a51b2011-06-29 00:21:09 -07001443SND_SOC_DAPM_SUPPLY("AIF2CLK", WM8994_AIF2_CLOCKING_1, 0, 0, NULL, 0),
1444SND_SOC_DAPM_PGA("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0),
1445SND_SOC_DAPM_MIXER("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
1446 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
1447SND_SOC_DAPM_MIXER("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
1448 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
1449SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
1450SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001451};
1452
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001453static const struct snd_soc_dapm_widget wm8994_dac_revd_widgets[] = {
1454SND_SOC_DAPM_DAC_E("DAC2L", NULL, SND_SOC_NOPM, 3, 0,
1455 dac_ev, SND_SOC_DAPM_PRE_PMU),
1456SND_SOC_DAPM_DAC_E("DAC2R", NULL, SND_SOC_NOPM, 2, 0,
1457 dac_ev, SND_SOC_DAPM_PRE_PMU),
1458SND_SOC_DAPM_DAC_E("DAC1L", NULL, SND_SOC_NOPM, 1, 0,
1459 dac_ev, SND_SOC_DAPM_PRE_PMU),
1460SND_SOC_DAPM_DAC_E("DAC1R", NULL, SND_SOC_NOPM, 0, 0,
1461 dac_ev, SND_SOC_DAPM_PRE_PMU),
1462};
1463
1464static const struct snd_soc_dapm_widget wm8994_dac_widgets[] = {
1465SND_SOC_DAPM_DAC("DAC2L", NULL, WM8994_POWER_MANAGEMENT_5, 3, 0),
Mark Brown0627bd22011-03-09 19:09:17 +00001466SND_SOC_DAPM_DAC("DAC2R", NULL, WM8994_POWER_MANAGEMENT_5, 2, 0),
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001467SND_SOC_DAPM_DAC("DAC1L", NULL, WM8994_POWER_MANAGEMENT_5, 1, 0),
1468SND_SOC_DAPM_DAC("DAC1R", NULL, WM8994_POWER_MANAGEMENT_5, 0, 0),
1469};
1470
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001471static const struct snd_soc_dapm_widget wm8994_adc_revd_widgets[] = {
1472SND_SOC_DAPM_MUX_E("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux,
1473 adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
1474SND_SOC_DAPM_MUX_E("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux,
1475 adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
1476};
1477
1478static const struct snd_soc_dapm_widget wm8994_adc_widgets[] = {
1479SND_SOC_DAPM_MUX("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux),
1480SND_SOC_DAPM_MUX("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux),
1481};
1482
Mark Brown9e6e96a2010-01-29 17:47:12 +00001483static const struct snd_soc_dapm_widget wm8994_dapm_widgets[] = {
1484SND_SOC_DAPM_INPUT("DMIC1DAT"),
1485SND_SOC_DAPM_INPUT("DMIC2DAT"),
Mark Brown66b47fd2010-07-08 11:25:43 +09001486SND_SOC_DAPM_INPUT("Clock"),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001487
Dimitris Papastamosb462c6e2011-03-01 12:54:39 +00001488SND_SOC_DAPM_SUPPLY_S("MICBIAS Supply", 1, SND_SOC_NOPM, 0, 0, micbias_ev,
1489 SND_SOC_DAPM_PRE_PMU),
Mark Brown4b7ed832011-08-10 17:47:33 +09001490SND_SOC_DAPM_SUPPLY("VMID", SND_SOC_NOPM, 0, 0, vmid_event,
1491 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
Dimitris Papastamosb462c6e2011-03-01 12:54:39 +00001492
Mark Brown9e6e96a2010-01-29 17:47:12 +00001493SND_SOC_DAPM_SUPPLY("CLK_SYS", SND_SOC_NOPM, 0, 0, clk_sys_event,
1494 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1495
1496SND_SOC_DAPM_SUPPLY("DSP1CLK", WM8994_CLOCKING_1, 3, 0, NULL, 0),
1497SND_SOC_DAPM_SUPPLY("DSP2CLK", WM8994_CLOCKING_1, 2, 0, NULL, 0),
1498SND_SOC_DAPM_SUPPLY("DSPINTCLK", WM8994_CLOCKING_1, 1, 0, NULL, 0),
1499
Mark Brown7f94de42011-02-03 16:27:34 +00001500SND_SOC_DAPM_AIF_OUT("AIF1ADC1L", NULL,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001501 0, WM8994_POWER_MANAGEMENT_4, 9, 0),
Mark Brown7f94de42011-02-03 16:27:34 +00001502SND_SOC_DAPM_AIF_OUT("AIF1ADC1R", NULL,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001503 0, WM8994_POWER_MANAGEMENT_4, 8, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001504SND_SOC_DAPM_AIF_IN_E("AIF1DAC1L", NULL, 0,
1505 WM8994_POWER_MANAGEMENT_5, 9, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001506 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brownd6addcc2010-11-26 15:21:08 +00001507SND_SOC_DAPM_AIF_IN_E("AIF1DAC1R", NULL, 0,
1508 WM8994_POWER_MANAGEMENT_5, 8, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001509 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001510
Mark Brown7f94de42011-02-03 16:27:34 +00001511SND_SOC_DAPM_AIF_OUT("AIF1ADC2L", NULL,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001512 0, WM8994_POWER_MANAGEMENT_4, 11, 0),
Mark Brown7f94de42011-02-03 16:27:34 +00001513SND_SOC_DAPM_AIF_OUT("AIF1ADC2R", NULL,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001514 0, WM8994_POWER_MANAGEMENT_4, 10, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001515SND_SOC_DAPM_AIF_IN_E("AIF1DAC2L", NULL, 0,
1516 WM8994_POWER_MANAGEMENT_5, 11, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001517 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brownd6addcc2010-11-26 15:21:08 +00001518SND_SOC_DAPM_AIF_IN_E("AIF1DAC2R", NULL, 0,
1519 WM8994_POWER_MANAGEMENT_5, 10, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001520 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001521
1522SND_SOC_DAPM_MIXER("AIF1ADC1L Mixer", SND_SOC_NOPM, 0, 0,
1523 aif1adc1l_mix, ARRAY_SIZE(aif1adc1l_mix)),
1524SND_SOC_DAPM_MIXER("AIF1ADC1R Mixer", SND_SOC_NOPM, 0, 0,
1525 aif1adc1r_mix, ARRAY_SIZE(aif1adc1r_mix)),
1526
Mark Browna3257ba2010-07-19 14:02:34 +01001527SND_SOC_DAPM_MIXER("AIF1ADC2L Mixer", SND_SOC_NOPM, 0, 0,
1528 aif1adc2l_mix, ARRAY_SIZE(aif1adc2l_mix)),
1529SND_SOC_DAPM_MIXER("AIF1ADC2R Mixer", SND_SOC_NOPM, 0, 0,
1530 aif1adc2r_mix, ARRAY_SIZE(aif1adc2r_mix)),
1531
Mark Brown9e6e96a2010-01-29 17:47:12 +00001532SND_SOC_DAPM_MIXER("AIF2DAC2L Mixer", SND_SOC_NOPM, 0, 0,
1533 aif2dac2l_mix, ARRAY_SIZE(aif2dac2l_mix)),
1534SND_SOC_DAPM_MIXER("AIF2DAC2R Mixer", SND_SOC_NOPM, 0, 0,
1535 aif2dac2r_mix, ARRAY_SIZE(aif2dac2r_mix)),
1536
1537SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &sidetone1_mux),
1538SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &sidetone2_mux),
1539
1540SND_SOC_DAPM_MIXER("DAC1L Mixer", SND_SOC_NOPM, 0, 0,
1541 dac1l_mix, ARRAY_SIZE(dac1l_mix)),
1542SND_SOC_DAPM_MIXER("DAC1R Mixer", SND_SOC_NOPM, 0, 0,
1543 dac1r_mix, ARRAY_SIZE(dac1r_mix)),
1544
1545SND_SOC_DAPM_AIF_OUT("AIF2ADCL", NULL, 0,
1546 WM8994_POWER_MANAGEMENT_4, 13, 0),
1547SND_SOC_DAPM_AIF_OUT("AIF2ADCR", NULL, 0,
1548 WM8994_POWER_MANAGEMENT_4, 12, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001549SND_SOC_DAPM_AIF_IN_E("AIF2DACL", NULL, 0,
1550 WM8994_POWER_MANAGEMENT_5, 13, 0, wm8958_aif_ev,
1551 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1552SND_SOC_DAPM_AIF_IN_E("AIF2DACR", NULL, 0,
1553 WM8994_POWER_MANAGEMENT_5, 12, 0, wm8958_aif_ev,
1554 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001555
1556SND_SOC_DAPM_AIF_IN("AIF1DACDAT", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
1557SND_SOC_DAPM_AIF_IN("AIF2DACDAT", "AIF2 Playback", 0, SND_SOC_NOPM, 0, 0),
Mark Brown7f94de42011-02-03 16:27:34 +00001558SND_SOC_DAPM_AIF_OUT("AIF1ADCDAT", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001559SND_SOC_DAPM_AIF_OUT("AIF2ADCDAT", "AIF2 Capture", 0, SND_SOC_NOPM, 0, 0),
1560
1561SND_SOC_DAPM_MUX("AIF1DAC Mux", SND_SOC_NOPM, 0, 0, &aif1dac_mux),
1562SND_SOC_DAPM_MUX("AIF2DAC Mux", SND_SOC_NOPM, 0, 0, &aif2dac_mux),
1563SND_SOC_DAPM_MUX("AIF2ADC Mux", SND_SOC_NOPM, 0, 0, &aif2adc_mux),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001564
1565SND_SOC_DAPM_AIF_IN("AIF3DACDAT", "AIF3 Playback", 0, SND_SOC_NOPM, 0, 0),
Axel Lin35024f42011-10-20 12:13:24 +08001566SND_SOC_DAPM_AIF_OUT("AIF3ADCDAT", "AIF3 Capture", 0, SND_SOC_NOPM, 0, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001567
1568SND_SOC_DAPM_SUPPLY("TOCLK", WM8994_CLOCKING_1, 4, 0, NULL, 0),
1569
1570SND_SOC_DAPM_ADC("DMIC2L", NULL, WM8994_POWER_MANAGEMENT_4, 5, 0),
1571SND_SOC_DAPM_ADC("DMIC2R", NULL, WM8994_POWER_MANAGEMENT_4, 4, 0),
1572SND_SOC_DAPM_ADC("DMIC1L", NULL, WM8994_POWER_MANAGEMENT_4, 3, 0),
1573SND_SOC_DAPM_ADC("DMIC1R", NULL, WM8994_POWER_MANAGEMENT_4, 2, 0),
1574
1575/* Power is done with the muxes since the ADC power also controls the
1576 * downsampling chain, the chip will automatically manage the analogue
1577 * specific portions.
1578 */
1579SND_SOC_DAPM_ADC("ADCL", NULL, SND_SOC_NOPM, 1, 0),
1580SND_SOC_DAPM_ADC("ADCR", NULL, SND_SOC_NOPM, 0, 0),
1581
Mark Brown9e6e96a2010-01-29 17:47:12 +00001582SND_SOC_DAPM_POST("Debug log", post_ev),
1583};
1584
Mark Brownc4431df2010-11-26 15:21:07 +00001585static const struct snd_soc_dapm_widget wm8994_specific_dapm_widgets[] = {
1586SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8994_aif3adc_mux),
1587};
Mark Brown9e6e96a2010-01-29 17:47:12 +00001588
Mark Brownc4431df2010-11-26 15:21:07 +00001589static const struct snd_soc_dapm_widget wm8958_dapm_widgets[] = {
1590SND_SOC_DAPM_MUX("Mono PCM Out Mux", SND_SOC_NOPM, 0, 0, &mono_pcm_out_mux),
1591SND_SOC_DAPM_MUX("AIF2DACL Mux", SND_SOC_NOPM, 0, 0, &aif2dacl_src_mux),
1592SND_SOC_DAPM_MUX("AIF2DACR Mux", SND_SOC_NOPM, 0, 0, &aif2dacr_src_mux),
1593SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8958_aif3adc_mux),
1594};
1595
1596static const struct snd_soc_dapm_route intercon[] = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00001597 { "CLK_SYS", NULL, "AIF1CLK", check_clk_sys },
1598 { "CLK_SYS", NULL, "AIF2CLK", check_clk_sys },
1599
1600 { "DSP1CLK", NULL, "CLK_SYS" },
1601 { "DSP2CLK", NULL, "CLK_SYS" },
1602 { "DSPINTCLK", NULL, "CLK_SYS" },
1603
1604 { "AIF1ADC1L", NULL, "AIF1CLK" },
1605 { "AIF1ADC1L", NULL, "DSP1CLK" },
1606 { "AIF1ADC1R", NULL, "AIF1CLK" },
1607 { "AIF1ADC1R", NULL, "DSP1CLK" },
1608 { "AIF1ADC1R", NULL, "DSPINTCLK" },
1609
1610 { "AIF1DAC1L", NULL, "AIF1CLK" },
1611 { "AIF1DAC1L", NULL, "DSP1CLK" },
1612 { "AIF1DAC1R", NULL, "AIF1CLK" },
1613 { "AIF1DAC1R", NULL, "DSP1CLK" },
1614 { "AIF1DAC1R", NULL, "DSPINTCLK" },
1615
1616 { "AIF1ADC2L", NULL, "AIF1CLK" },
1617 { "AIF1ADC2L", NULL, "DSP1CLK" },
1618 { "AIF1ADC2R", NULL, "AIF1CLK" },
1619 { "AIF1ADC2R", NULL, "DSP1CLK" },
1620 { "AIF1ADC2R", NULL, "DSPINTCLK" },
1621
1622 { "AIF1DAC2L", NULL, "AIF1CLK" },
1623 { "AIF1DAC2L", NULL, "DSP1CLK" },
1624 { "AIF1DAC2R", NULL, "AIF1CLK" },
1625 { "AIF1DAC2R", NULL, "DSP1CLK" },
1626 { "AIF1DAC2R", NULL, "DSPINTCLK" },
1627
1628 { "AIF2ADCL", NULL, "AIF2CLK" },
1629 { "AIF2ADCL", NULL, "DSP2CLK" },
1630 { "AIF2ADCR", NULL, "AIF2CLK" },
1631 { "AIF2ADCR", NULL, "DSP2CLK" },
1632 { "AIF2ADCR", NULL, "DSPINTCLK" },
1633
1634 { "AIF2DACL", NULL, "AIF2CLK" },
1635 { "AIF2DACL", NULL, "DSP2CLK" },
1636 { "AIF2DACR", NULL, "AIF2CLK" },
1637 { "AIF2DACR", NULL, "DSP2CLK" },
1638 { "AIF2DACR", NULL, "DSPINTCLK" },
1639
1640 { "DMIC1L", NULL, "DMIC1DAT" },
1641 { "DMIC1L", NULL, "CLK_SYS" },
1642 { "DMIC1R", NULL, "DMIC1DAT" },
1643 { "DMIC1R", NULL, "CLK_SYS" },
1644 { "DMIC2L", NULL, "DMIC2DAT" },
1645 { "DMIC2L", NULL, "CLK_SYS" },
1646 { "DMIC2R", NULL, "DMIC2DAT" },
1647 { "DMIC2R", NULL, "CLK_SYS" },
1648
1649 { "ADCL", NULL, "AIF1CLK" },
1650 { "ADCL", NULL, "DSP1CLK" },
1651 { "ADCL", NULL, "DSPINTCLK" },
1652
1653 { "ADCR", NULL, "AIF1CLK" },
1654 { "ADCR", NULL, "DSP1CLK" },
1655 { "ADCR", NULL, "DSPINTCLK" },
1656
1657 { "ADCL Mux", "ADC", "ADCL" },
1658 { "ADCL Mux", "DMIC", "DMIC1L" },
1659 { "ADCR Mux", "ADC", "ADCR" },
1660 { "ADCR Mux", "DMIC", "DMIC1R" },
1661
1662 { "DAC1L", NULL, "AIF1CLK" },
1663 { "DAC1L", NULL, "DSP1CLK" },
1664 { "DAC1L", NULL, "DSPINTCLK" },
1665
1666 { "DAC1R", NULL, "AIF1CLK" },
1667 { "DAC1R", NULL, "DSP1CLK" },
1668 { "DAC1R", NULL, "DSPINTCLK" },
1669
1670 { "DAC2L", NULL, "AIF2CLK" },
1671 { "DAC2L", NULL, "DSP2CLK" },
1672 { "DAC2L", NULL, "DSPINTCLK" },
1673
1674 { "DAC2R", NULL, "AIF2DACR" },
1675 { "DAC2R", NULL, "AIF2CLK" },
1676 { "DAC2R", NULL, "DSP2CLK" },
1677 { "DAC2R", NULL, "DSPINTCLK" },
1678
1679 { "TOCLK", NULL, "CLK_SYS" },
1680
1681 /* AIF1 outputs */
1682 { "AIF1ADC1L", NULL, "AIF1ADC1L Mixer" },
1683 { "AIF1ADC1L Mixer", "ADC/DMIC Switch", "ADCL Mux" },
1684 { "AIF1ADC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1685
1686 { "AIF1ADC1R", NULL, "AIF1ADC1R Mixer" },
1687 { "AIF1ADC1R Mixer", "ADC/DMIC Switch", "ADCR Mux" },
1688 { "AIF1ADC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1689
Mark Browna3257ba2010-07-19 14:02:34 +01001690 { "AIF1ADC2L", NULL, "AIF1ADC2L Mixer" },
1691 { "AIF1ADC2L Mixer", "DMIC Switch", "DMIC2L" },
1692 { "AIF1ADC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1693
1694 { "AIF1ADC2R", NULL, "AIF1ADC2R Mixer" },
1695 { "AIF1ADC2R Mixer", "DMIC Switch", "DMIC2R" },
1696 { "AIF1ADC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1697
Mark Brown9e6e96a2010-01-29 17:47:12 +00001698 /* Pin level routing for AIF3 */
1699 { "AIF1DAC1L", NULL, "AIF1DAC Mux" },
1700 { "AIF1DAC1R", NULL, "AIF1DAC Mux" },
1701 { "AIF1DAC2L", NULL, "AIF1DAC Mux" },
1702 { "AIF1DAC2R", NULL, "AIF1DAC Mux" },
1703
Mark Brown9e6e96a2010-01-29 17:47:12 +00001704 { "AIF1DAC Mux", "AIF1DACDAT", "AIF1DACDAT" },
1705 { "AIF1DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1706 { "AIF2DAC Mux", "AIF2DACDAT", "AIF2DACDAT" },
1707 { "AIF2DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1708 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCL" },
1709 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCR" },
1710 { "AIF2ADC Mux", "AIF3DACDAT", "AIF3ADCDAT" },
1711
1712 /* DAC1 inputs */
Mark Brown9e6e96a2010-01-29 17:47:12 +00001713 { "DAC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1714 { "DAC1L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1715 { "DAC1L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1716 { "DAC1L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1717 { "DAC1L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1718
Mark Brown9e6e96a2010-01-29 17:47:12 +00001719 { "DAC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1720 { "DAC1R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1721 { "DAC1R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1722 { "DAC1R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1723 { "DAC1R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1724
1725 /* DAC2/AIF2 outputs */
1726 { "AIF2ADCL", NULL, "AIF2DAC2L Mixer" },
Mark Brown9e6e96a2010-01-29 17:47:12 +00001727 { "AIF2DAC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1728 { "AIF2DAC2L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1729 { "AIF2DAC2L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1730 { "AIF2DAC2L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1731 { "AIF2DAC2L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1732
1733 { "AIF2ADCR", NULL, "AIF2DAC2R Mixer" },
Mark Brown9e6e96a2010-01-29 17:47:12 +00001734 { "AIF2DAC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1735 { "AIF2DAC2R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1736 { "AIF2DAC2R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1737 { "AIF2DAC2R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1738 { "AIF2DAC2R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1739
Mark Brown7f94de42011-02-03 16:27:34 +00001740 { "AIF1ADCDAT", NULL, "AIF1ADC1L" },
1741 { "AIF1ADCDAT", NULL, "AIF1ADC1R" },
1742 { "AIF1ADCDAT", NULL, "AIF1ADC2L" },
1743 { "AIF1ADCDAT", NULL, "AIF1ADC2R" },
1744
Mark Brown9e6e96a2010-01-29 17:47:12 +00001745 { "AIF2ADCDAT", NULL, "AIF2ADC Mux" },
1746
1747 /* AIF3 output */
1748 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1L" },
1749 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1R" },
1750 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2L" },
1751 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2R" },
1752 { "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCL" },
1753 { "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCR" },
1754 { "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACL" },
1755 { "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACR" },
1756
1757 /* Sidetone */
1758 { "Left Sidetone", "ADC/DMIC1", "ADCL Mux" },
1759 { "Left Sidetone", "DMIC2", "DMIC2L" },
1760 { "Right Sidetone", "ADC/DMIC1", "ADCR Mux" },
1761 { "Right Sidetone", "DMIC2", "DMIC2R" },
1762
1763 /* Output stages */
1764 { "Left Output Mixer", "DAC Switch", "DAC1L" },
1765 { "Right Output Mixer", "DAC Switch", "DAC1R" },
1766
1767 { "SPKL", "DAC1 Switch", "DAC1L" },
1768 { "SPKL", "DAC2 Switch", "DAC2L" },
1769
1770 { "SPKR", "DAC1 Switch", "DAC1R" },
1771 { "SPKR", "DAC2 Switch", "DAC2R" },
1772
1773 { "Left Headphone Mux", "DAC", "DAC1L" },
1774 { "Right Headphone Mux", "DAC", "DAC1R" },
1775};
1776
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001777static const struct snd_soc_dapm_route wm8994_lateclk_revd_intercon[] = {
1778 { "DAC1L", NULL, "Late DAC1L Enable PGA" },
1779 { "Late DAC1L Enable PGA", NULL, "DAC1L Mixer" },
1780 { "DAC1R", NULL, "Late DAC1R Enable PGA" },
1781 { "Late DAC1R Enable PGA", NULL, "DAC1R Mixer" },
1782 { "DAC2L", NULL, "Late DAC2L Enable PGA" },
1783 { "Late DAC2L Enable PGA", NULL, "AIF2DAC2L Mixer" },
1784 { "DAC2R", NULL, "Late DAC2R Enable PGA" },
1785 { "Late DAC2R Enable PGA", NULL, "AIF2DAC2R Mixer" }
1786};
1787
1788static const struct snd_soc_dapm_route wm8994_lateclk_intercon[] = {
1789 { "DAC1L", NULL, "DAC1L Mixer" },
1790 { "DAC1R", NULL, "DAC1R Mixer" },
1791 { "DAC2L", NULL, "AIF2DAC2L Mixer" },
1792 { "DAC2R", NULL, "AIF2DAC2R Mixer" },
1793};
1794
Mark Brown6ed8f142011-02-03 16:27:35 +00001795static const struct snd_soc_dapm_route wm8994_revd_intercon[] = {
1796 { "AIF1DACDAT", NULL, "AIF2DACDAT" },
1797 { "AIF2DACDAT", NULL, "AIF1DACDAT" },
1798 { "AIF1ADCDAT", NULL, "AIF2ADCDAT" },
1799 { "AIF2ADCDAT", NULL, "AIF1ADCDAT" },
Mark Brownb793eb62011-07-14 18:21:37 +09001800 { "MICBIAS1", NULL, "CLK_SYS" },
1801 { "MICBIAS1", NULL, "MICBIAS Supply" },
1802 { "MICBIAS2", NULL, "CLK_SYS" },
1803 { "MICBIAS2", NULL, "MICBIAS Supply" },
Mark Brown6ed8f142011-02-03 16:27:35 +00001804};
1805
Mark Brownc4431df2010-11-26 15:21:07 +00001806static const struct snd_soc_dapm_route wm8994_intercon[] = {
1807 { "AIF2DACL", NULL, "AIF2DAC Mux" },
1808 { "AIF2DACR", NULL, "AIF2DAC Mux" },
Mark Brown4e04ada2011-07-15 15:12:31 +09001809 { "MICBIAS1", NULL, "VMID" },
1810 { "MICBIAS2", NULL, "VMID" },
Mark Brownc4431df2010-11-26 15:21:07 +00001811};
1812
1813static const struct snd_soc_dapm_route wm8958_intercon[] = {
1814 { "AIF2DACL", NULL, "AIF2DACL Mux" },
1815 { "AIF2DACR", NULL, "AIF2DACR Mux" },
1816
1817 { "AIF2DACL Mux", "AIF2", "AIF2DAC Mux" },
1818 { "AIF2DACL Mux", "AIF3", "AIF3DACDAT" },
1819 { "AIF2DACR Mux", "AIF2", "AIF2DAC Mux" },
1820 { "AIF2DACR Mux", "AIF3", "AIF3DACDAT" },
1821
1822 { "Mono PCM Out Mux", "AIF2ADCL", "AIF2ADCL" },
1823 { "Mono PCM Out Mux", "AIF2ADCR", "AIF2ADCR" },
1824
1825 { "AIF3ADC Mux", "Mono PCM", "Mono PCM Out Mux" },
1826};
1827
Mark Brown9e6e96a2010-01-29 17:47:12 +00001828/* The size in bits of the FLL divide multiplied by 10
1829 * to allow rounding later */
1830#define FIXED_FLL_SIZE ((1 << 16) * 10)
1831
1832struct fll_div {
1833 u16 outdiv;
1834 u16 n;
1835 u16 k;
1836 u16 clk_ref_div;
1837 u16 fll_fratio;
1838};
1839
1840static int wm8994_get_fll_config(struct fll_div *fll,
1841 int freq_in, int freq_out)
1842{
1843 u64 Kpart;
1844 unsigned int K, Ndiv, Nmod;
1845
1846 pr_debug("FLL input=%dHz, output=%dHz\n", freq_in, freq_out);
1847
1848 /* Scale the input frequency down to <= 13.5MHz */
1849 fll->clk_ref_div = 0;
1850 while (freq_in > 13500000) {
1851 fll->clk_ref_div++;
1852 freq_in /= 2;
1853
1854 if (fll->clk_ref_div > 3)
1855 return -EINVAL;
1856 }
1857 pr_debug("CLK_REF_DIV=%d, Fref=%dHz\n", fll->clk_ref_div, freq_in);
1858
1859 /* Scale the output to give 90MHz<=Fvco<=100MHz */
1860 fll->outdiv = 3;
1861 while (freq_out * (fll->outdiv + 1) < 90000000) {
1862 fll->outdiv++;
1863 if (fll->outdiv > 63)
1864 return -EINVAL;
1865 }
1866 freq_out *= fll->outdiv + 1;
1867 pr_debug("OUTDIV=%d, Fvco=%dHz\n", fll->outdiv, freq_out);
1868
1869 if (freq_in > 1000000) {
1870 fll->fll_fratio = 0;
Mark Brown7d48a6a2010-04-20 13:36:11 +09001871 } else if (freq_in > 256000) {
1872 fll->fll_fratio = 1;
1873 freq_in *= 2;
1874 } else if (freq_in > 128000) {
1875 fll->fll_fratio = 2;
1876 freq_in *= 4;
1877 } else if (freq_in > 64000) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00001878 fll->fll_fratio = 3;
1879 freq_in *= 8;
Mark Brown7d48a6a2010-04-20 13:36:11 +09001880 } else {
1881 fll->fll_fratio = 4;
1882 freq_in *= 16;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001883 }
1884 pr_debug("FLL_FRATIO=%d, Fref=%dHz\n", fll->fll_fratio, freq_in);
1885
1886 /* Now, calculate N.K */
1887 Ndiv = freq_out / freq_in;
1888
1889 fll->n = Ndiv;
1890 Nmod = freq_out % freq_in;
1891 pr_debug("Nmod=%d\n", Nmod);
1892
1893 /* Calculate fractional part - scale up so we can round. */
1894 Kpart = FIXED_FLL_SIZE * (long long)Nmod;
1895
1896 do_div(Kpart, freq_in);
1897
1898 K = Kpart & 0xFFFFFFFF;
1899
1900 if ((K % 10) >= 5)
1901 K += 5;
1902
1903 /* Move down to proper range now rounding is done */
1904 fll->k = K / 10;
1905
1906 pr_debug("N=%x K=%x\n", fll->n, fll->k);
1907
1908 return 0;
1909}
1910
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001911static int _wm8994_set_fll(struct snd_soc_codec *codec, int id, int src,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001912 unsigned int freq_in, unsigned int freq_out)
1913{
Mark Brownb2c812e2010-04-14 15:35:19 +09001914 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01001915 struct wm8994 *control = wm8994->wm8994;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001916 int reg_offset, ret;
1917 struct fll_div fll;
1918 u16 reg, aif1, aif2;
Mark Brownc7ebf932011-07-12 19:47:59 +09001919 unsigned long timeout;
Mark Brown4b7ed832011-08-10 17:47:33 +09001920 bool was_enabled;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001921
1922 aif1 = snd_soc_read(codec, WM8994_AIF1_CLOCKING_1)
1923 & WM8994_AIF1CLK_ENA;
1924
1925 aif2 = snd_soc_read(codec, WM8994_AIF2_CLOCKING_1)
1926 & WM8994_AIF2CLK_ENA;
1927
1928 switch (id) {
1929 case WM8994_FLL1:
1930 reg_offset = 0;
1931 id = 0;
1932 break;
1933 case WM8994_FLL2:
1934 reg_offset = 0x20;
1935 id = 1;
1936 break;
1937 default:
1938 return -EINVAL;
1939 }
1940
Mark Brown4b7ed832011-08-10 17:47:33 +09001941 reg = snd_soc_read(codec, WM8994_FLL1_CONTROL_1 + reg_offset);
1942 was_enabled = reg & WM8994_FLL1_ENA;
1943
Mark Brown136ff2a2010-04-20 12:56:18 +09001944 switch (src) {
Mark Brown7add84a2010-04-22 02:29:01 +09001945 case 0:
1946 /* Allow no source specification when stopping */
1947 if (freq_out)
1948 return -EINVAL;
Mark Brown4514e892010-12-03 16:02:10 +00001949 src = wm8994->fll[id].src;
Mark Brown7add84a2010-04-22 02:29:01 +09001950 break;
Mark Brown136ff2a2010-04-20 12:56:18 +09001951 case WM8994_FLL_SRC_MCLK1:
1952 case WM8994_FLL_SRC_MCLK2:
1953 case WM8994_FLL_SRC_LRCLK:
1954 case WM8994_FLL_SRC_BCLK:
1955 break;
1956 default:
1957 return -EINVAL;
1958 }
1959
Mark Brown9e6e96a2010-01-29 17:47:12 +00001960 /* Are we changing anything? */
1961 if (wm8994->fll[id].src == src &&
1962 wm8994->fll[id].in == freq_in && wm8994->fll[id].out == freq_out)
1963 return 0;
1964
1965 /* If we're stopping the FLL redo the old config - no
1966 * registers will actually be written but we avoid GCC flow
1967 * analysis bugs spewing warnings.
1968 */
1969 if (freq_out)
1970 ret = wm8994_get_fll_config(&fll, freq_in, freq_out);
1971 else
1972 ret = wm8994_get_fll_config(&fll, wm8994->fll[id].in,
1973 wm8994->fll[id].out);
1974 if (ret < 0)
1975 return ret;
1976
1977 /* Gate the AIF clocks while we reclock */
1978 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1979 WM8994_AIF1CLK_ENA, 0);
1980 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1981 WM8994_AIF2CLK_ENA, 0);
1982
1983 /* We always need to disable the FLL while reconfiguring */
1984 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
1985 WM8994_FLL1_ENA, 0);
1986
1987 reg = (fll.outdiv << WM8994_FLL1_OUTDIV_SHIFT) |
1988 (fll.fll_fratio << WM8994_FLL1_FRATIO_SHIFT);
1989 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_2 + reg_offset,
1990 WM8994_FLL1_OUTDIV_MASK |
1991 WM8994_FLL1_FRATIO_MASK, reg);
1992
1993 snd_soc_write(codec, WM8994_FLL1_CONTROL_3 + reg_offset, fll.k);
1994
1995 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_4 + reg_offset,
1996 WM8994_FLL1_N_MASK,
1997 fll.n << WM8994_FLL1_N_SHIFT);
1998
1999 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_5 + reg_offset,
Mark Brown136ff2a2010-04-20 12:56:18 +09002000 WM8994_FLL1_REFCLK_DIV_MASK |
2001 WM8994_FLL1_REFCLK_SRC_MASK,
2002 (fll.clk_ref_div << WM8994_FLL1_REFCLK_DIV_SHIFT) |
2003 (src - 1));
Mark Brown9e6e96a2010-01-29 17:47:12 +00002004
Mark Brownf0f50392011-07-16 03:12:18 +09002005 /* Clear any pending completion from a previous failure */
2006 try_wait_for_completion(&wm8994->fll_locked[id]);
2007
Mark Brown9e6e96a2010-01-29 17:47:12 +00002008 /* Enable (with fractional mode if required) */
2009 if (freq_out) {
Mark Brown4b7ed832011-08-10 17:47:33 +09002010 /* Enable VMID if we need it */
2011 if (!was_enabled) {
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002012 active_reference(codec);
2013
Mark Brown4b7ed832011-08-10 17:47:33 +09002014 switch (control->type) {
2015 case WM8994:
2016 vmid_reference(codec);
2017 break;
2018 case WM8958:
2019 if (wm8994->revision < 1)
2020 vmid_reference(codec);
2021 break;
2022 default:
2023 break;
2024 }
2025 }
2026
Mark Brown9e6e96a2010-01-29 17:47:12 +00002027 if (fll.k)
2028 reg = WM8994_FLL1_ENA | WM8994_FLL1_FRAC;
2029 else
2030 reg = WM8994_FLL1_ENA;
2031 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
2032 WM8994_FLL1_ENA | WM8994_FLL1_FRAC,
2033 reg);
Mark Brown8e9ddf82011-07-01 17:24:46 -07002034
Mark Brownc7ebf932011-07-12 19:47:59 +09002035 if (wm8994->fll_locked_irq) {
2036 timeout = wait_for_completion_timeout(&wm8994->fll_locked[id],
2037 msecs_to_jiffies(10));
2038 if (timeout == 0)
2039 dev_warn(codec->dev,
2040 "Timed out waiting for FLL lock\n");
2041 } else {
2042 msleep(5);
2043 }
Mark Brown4b7ed832011-08-10 17:47:33 +09002044 } else {
2045 if (was_enabled) {
2046 switch (control->type) {
2047 case WM8994:
2048 vmid_dereference(codec);
2049 break;
2050 case WM8958:
2051 if (wm8994->revision < 1)
2052 vmid_dereference(codec);
2053 break;
2054 default:
2055 break;
2056 }
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002057
2058 active_dereference(codec);
Mark Brown4b7ed832011-08-10 17:47:33 +09002059 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002060 }
2061
2062 wm8994->fll[id].in = freq_in;
2063 wm8994->fll[id].out = freq_out;
Mark Brown136ff2a2010-04-20 12:56:18 +09002064 wm8994->fll[id].src = src;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002065
2066 /* Enable any gated AIF clocks */
2067 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
2068 WM8994_AIF1CLK_ENA, aif1);
2069 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
2070 WM8994_AIF2CLK_ENA, aif2);
2071
2072 configure_clock(codec);
2073
2074 return 0;
2075}
2076
Mark Brownc7ebf932011-07-12 19:47:59 +09002077static irqreturn_t wm8994_fll_locked_irq(int irq, void *data)
2078{
2079 struct completion *completion = data;
2080
2081 complete(completion);
2082
2083 return IRQ_HANDLED;
2084}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002085
Mark Brown66b47fd2010-07-08 11:25:43 +09002086static int opclk_divs[] = { 10, 20, 30, 40, 55, 60, 80, 120, 160 };
2087
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002088static int wm8994_set_fll(struct snd_soc_dai *dai, int id, int src,
2089 unsigned int freq_in, unsigned int freq_out)
2090{
2091 return _wm8994_set_fll(dai->codec, id, src, freq_in, freq_out);
2092}
2093
Mark Brown9e6e96a2010-01-29 17:47:12 +00002094static int wm8994_set_dai_sysclk(struct snd_soc_dai *dai,
2095 int clk_id, unsigned int freq, int dir)
2096{
2097 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09002098 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown66b47fd2010-07-08 11:25:43 +09002099 int i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002100
2101 switch (dai->id) {
2102 case 1:
2103 case 2:
2104 break;
2105
2106 default:
2107 /* AIF3 shares clocking with AIF1/2 */
2108 return -EINVAL;
2109 }
2110
2111 switch (clk_id) {
2112 case WM8994_SYSCLK_MCLK1:
2113 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK1;
2114 wm8994->mclk[0] = freq;
2115 dev_dbg(dai->dev, "AIF%d using MCLK1 at %uHz\n",
2116 dai->id, freq);
2117 break;
2118
2119 case WM8994_SYSCLK_MCLK2:
2120 /* TODO: Set GPIO AF */
2121 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK2;
2122 wm8994->mclk[1] = freq;
2123 dev_dbg(dai->dev, "AIF%d using MCLK2 at %uHz\n",
2124 dai->id, freq);
2125 break;
2126
2127 case WM8994_SYSCLK_FLL1:
2128 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL1;
2129 dev_dbg(dai->dev, "AIF%d using FLL1\n", dai->id);
2130 break;
2131
2132 case WM8994_SYSCLK_FLL2:
2133 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL2;
2134 dev_dbg(dai->dev, "AIF%d using FLL2\n", dai->id);
2135 break;
2136
Mark Brown66b47fd2010-07-08 11:25:43 +09002137 case WM8994_SYSCLK_OPCLK:
2138 /* Special case - a division (times 10) is given and
2139 * no effect on main clocking.
2140 */
2141 if (freq) {
2142 for (i = 0; i < ARRAY_SIZE(opclk_divs); i++)
2143 if (opclk_divs[i] == freq)
2144 break;
2145 if (i == ARRAY_SIZE(opclk_divs))
2146 return -EINVAL;
2147 snd_soc_update_bits(codec, WM8994_CLOCKING_2,
2148 WM8994_OPCLK_DIV_MASK, i);
2149 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
2150 WM8994_OPCLK_ENA, WM8994_OPCLK_ENA);
2151 } else {
2152 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
2153 WM8994_OPCLK_ENA, 0);
2154 }
2155
Mark Brown9e6e96a2010-01-29 17:47:12 +00002156 default:
2157 return -EINVAL;
2158 }
2159
2160 configure_clock(codec);
2161
2162 return 0;
2163}
2164
2165static int wm8994_set_bias_level(struct snd_soc_codec *codec,
2166 enum snd_soc_bias_level level)
2167{
Mark Brownb6b05692010-08-13 12:58:20 +01002168 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01002169 struct wm8994 *control = wm8994->wm8994;
Mark Brownb6b05692010-08-13 12:58:20 +01002170
Mark Brown9e6e96a2010-01-29 17:47:12 +00002171 switch (level) {
2172 case SND_SOC_BIAS_ON:
2173 break;
2174
2175 case SND_SOC_BIAS_PREPARE:
Mark Brown500fa302011-11-29 19:58:19 +00002176 /* MICBIAS into regulating mode */
2177 switch (control->type) {
2178 case WM8958:
2179 case WM1811:
2180 snd_soc_update_bits(codec, WM8958_MICBIAS1,
2181 WM8958_MICB1_MODE, 0);
2182 snd_soc_update_bits(codec, WM8958_MICBIAS2,
2183 WM8958_MICB2_MODE, 0);
2184 break;
2185 default:
2186 break;
2187 }
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002188
2189 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
2190 active_reference(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002191 break;
2192
2193 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002194 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
Mark Brown39fb51a2010-11-26 17:23:43 +00002195 pm_runtime_get_sync(codec->dev);
2196
Mark Brown8bc3c2c2010-11-30 14:56:18 +00002197 switch (control->type) {
2198 case WM8994:
2199 if (wm8994->revision < 4) {
2200 /* Tweak DC servo and DSP
2201 * configuration for improved
2202 * performance. */
2203 snd_soc_write(codec, 0x102, 0x3);
2204 snd_soc_write(codec, 0x56, 0x3);
2205 snd_soc_write(codec, 0x817, 0);
2206 snd_soc_write(codec, 0x102, 0);
2207 }
2208 break;
2209
2210 case WM8958:
2211 if (wm8994->revision == 0) {
2212 /* Optimise performance for rev A */
2213 snd_soc_write(codec, 0x102, 0x3);
2214 snd_soc_write(codec, 0xcb, 0x81);
2215 snd_soc_write(codec, 0x817, 0);
2216 snd_soc_write(codec, 0x102, 0);
2217
2218 snd_soc_update_bits(codec,
2219 WM8958_CHARGE_PUMP_2,
2220 WM8958_CP_DISCH,
2221 WM8958_CP_DISCH);
2222 }
2223 break;
Mark Brown81204c82011-05-24 17:35:53 +08002224
2225 case WM1811:
2226 if (wm8994->revision < 2) {
2227 snd_soc_write(codec, 0x102, 0x3);
2228 snd_soc_write(codec, 0x5d, 0x7e);
2229 snd_soc_write(codec, 0x5e, 0x0);
2230 snd_soc_write(codec, 0x102, 0x0);
2231 }
2232 break;
Mark Brownb6b05692010-08-13 12:58:20 +01002233 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002234
2235 /* Discharge LINEOUT1 & 2 */
2236 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
2237 WM8994_LINEOUT1_DISCH |
2238 WM8994_LINEOUT2_DISCH,
2239 WM8994_LINEOUT1_DISCH |
2240 WM8994_LINEOUT2_DISCH);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002241 }
2242
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002243 if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE)
2244 active_dereference(codec);
2245
Mark Brown500fa302011-11-29 19:58:19 +00002246 /* MICBIAS into bypass mode on newer devices */
2247 switch (control->type) {
2248 case WM8958:
2249 case WM1811:
2250 snd_soc_update_bits(codec, WM8958_MICBIAS1,
2251 WM8958_MICB1_MODE,
2252 WM8958_MICB1_MODE);
2253 snd_soc_update_bits(codec, WM8958_MICBIAS2,
2254 WM8958_MICB2_MODE,
2255 WM8958_MICB2_MODE);
2256 break;
2257 default:
2258 break;
2259 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002260 break;
2261
2262 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002263 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
Mark Brownfbbf5922011-03-11 18:09:04 +00002264 wm8994->cur_fw = NULL;
2265
Mark Brown39fb51a2010-11-26 17:23:43 +00002266 pm_runtime_put(codec->dev);
Mark Brownd522ffb2010-03-30 14:29:14 +01002267 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002268 break;
2269 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002270 codec->dapm.bias_level = level;
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002271
Mark Brown9e6e96a2010-01-29 17:47:12 +00002272 return 0;
2273}
2274
2275static int wm8994_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2276{
2277 struct snd_soc_codec *codec = dai->codec;
Mark Brown2a8a8562011-07-24 12:20:41 +01002278 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2279 struct wm8994 *control = wm8994->wm8994;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002280 int ms_reg;
2281 int aif1_reg;
2282 int ms = 0;
2283 int aif1 = 0;
2284
2285 switch (dai->id) {
2286 case 1:
2287 ms_reg = WM8994_AIF1_MASTER_SLAVE;
2288 aif1_reg = WM8994_AIF1_CONTROL_1;
2289 break;
2290 case 2:
2291 ms_reg = WM8994_AIF2_MASTER_SLAVE;
2292 aif1_reg = WM8994_AIF2_CONTROL_1;
2293 break;
2294 default:
2295 return -EINVAL;
2296 }
2297
2298 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
2299 case SND_SOC_DAIFMT_CBS_CFS:
2300 break;
2301 case SND_SOC_DAIFMT_CBM_CFM:
2302 ms = WM8994_AIF1_MSTR;
2303 break;
2304 default:
2305 return -EINVAL;
2306 }
2307
2308 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2309 case SND_SOC_DAIFMT_DSP_B:
2310 aif1 |= WM8994_AIF1_LRCLK_INV;
2311 case SND_SOC_DAIFMT_DSP_A:
2312 aif1 |= 0x18;
2313 break;
2314 case SND_SOC_DAIFMT_I2S:
2315 aif1 |= 0x10;
2316 break;
2317 case SND_SOC_DAIFMT_RIGHT_J:
2318 break;
2319 case SND_SOC_DAIFMT_LEFT_J:
2320 aif1 |= 0x8;
2321 break;
2322 default:
2323 return -EINVAL;
2324 }
2325
2326 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2327 case SND_SOC_DAIFMT_DSP_A:
2328 case SND_SOC_DAIFMT_DSP_B:
2329 /* frame inversion not valid for DSP modes */
2330 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2331 case SND_SOC_DAIFMT_NB_NF:
2332 break;
2333 case SND_SOC_DAIFMT_IB_NF:
2334 aif1 |= WM8994_AIF1_BCLK_INV;
2335 break;
2336 default:
2337 return -EINVAL;
2338 }
2339 break;
2340
2341 case SND_SOC_DAIFMT_I2S:
2342 case SND_SOC_DAIFMT_RIGHT_J:
2343 case SND_SOC_DAIFMT_LEFT_J:
2344 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2345 case SND_SOC_DAIFMT_NB_NF:
2346 break;
2347 case SND_SOC_DAIFMT_IB_IF:
2348 aif1 |= WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV;
2349 break;
2350 case SND_SOC_DAIFMT_IB_NF:
2351 aif1 |= WM8994_AIF1_BCLK_INV;
2352 break;
2353 case SND_SOC_DAIFMT_NB_IF:
2354 aif1 |= WM8994_AIF1_LRCLK_INV;
2355 break;
2356 default:
2357 return -EINVAL;
2358 }
2359 break;
2360 default:
2361 return -EINVAL;
2362 }
2363
Mark Brownc4431df2010-11-26 15:21:07 +00002364 /* The AIF2 format configuration needs to be mirrored to AIF3
2365 * on WM8958 if it's in use so just do it all the time. */
Mark Brown81204c82011-05-24 17:35:53 +08002366 switch (control->type) {
2367 case WM1811:
2368 case WM8958:
2369 if (dai->id == 2)
2370 snd_soc_update_bits(codec, WM8958_AIF3_CONTROL_1,
2371 WM8994_AIF1_LRCLK_INV |
2372 WM8958_AIF3_FMT_MASK, aif1);
2373 break;
2374
2375 default:
2376 break;
2377 }
Mark Brownc4431df2010-11-26 15:21:07 +00002378
Mark Brown9e6e96a2010-01-29 17:47:12 +00002379 snd_soc_update_bits(codec, aif1_reg,
2380 WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV |
2381 WM8994_AIF1_FMT_MASK,
2382 aif1);
2383 snd_soc_update_bits(codec, ms_reg, WM8994_AIF1_MSTR,
2384 ms);
2385
2386 return 0;
2387}
2388
2389static struct {
2390 int val, rate;
2391} srs[] = {
2392 { 0, 8000 },
2393 { 1, 11025 },
2394 { 2, 12000 },
2395 { 3, 16000 },
2396 { 4, 22050 },
2397 { 5, 24000 },
2398 { 6, 32000 },
2399 { 7, 44100 },
2400 { 8, 48000 },
2401 { 9, 88200 },
2402 { 10, 96000 },
2403};
2404
2405static int fs_ratios[] = {
2406 64, 128, 192, 256, 348, 512, 768, 1024, 1408, 1536
2407};
2408
2409static int bclk_divs[] = {
2410 10, 15, 20, 30, 40, 50, 60, 80, 110, 120, 160, 220, 240, 320, 440, 480,
2411 640, 880, 960, 1280, 1760, 1920
2412};
2413
2414static int wm8994_hw_params(struct snd_pcm_substream *substream,
2415 struct snd_pcm_hw_params *params,
2416 struct snd_soc_dai *dai)
2417{
2418 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09002419 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002420 int aif1_reg;
Mark Brownb1e43d92010-12-07 17:14:56 +00002421 int aif2_reg;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002422 int bclk_reg;
2423 int lrclk_reg;
2424 int rate_reg;
2425 int aif1 = 0;
Mark Brownb1e43d92010-12-07 17:14:56 +00002426 int aif2 = 0;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002427 int bclk = 0;
2428 int lrclk = 0;
2429 int rate_val = 0;
2430 int id = dai->id - 1;
2431
2432 int i, cur_val, best_val, bclk_rate, best;
2433
2434 switch (dai->id) {
2435 case 1:
2436 aif1_reg = WM8994_AIF1_CONTROL_1;
Mark Brownb1e43d92010-12-07 17:14:56 +00002437 aif2_reg = WM8994_AIF1_CONTROL_2;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002438 bclk_reg = WM8994_AIF1_BCLK;
2439 rate_reg = WM8994_AIF1_RATE;
2440 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
Mark Brown7d83d212010-08-23 10:54:43 +01002441 wm8994->lrclk_shared[0]) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002442 lrclk_reg = WM8994_AIF1DAC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002443 } else {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002444 lrclk_reg = WM8994_AIF1ADC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002445 dev_dbg(codec->dev, "AIF1 using split LRCLK\n");
2446 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002447 break;
2448 case 2:
2449 aif1_reg = WM8994_AIF2_CONTROL_1;
Mark Brownb1e43d92010-12-07 17:14:56 +00002450 aif2_reg = WM8994_AIF2_CONTROL_2;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002451 bclk_reg = WM8994_AIF2_BCLK;
2452 rate_reg = WM8994_AIF2_RATE;
2453 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
Mark Brown7d83d212010-08-23 10:54:43 +01002454 wm8994->lrclk_shared[1]) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002455 lrclk_reg = WM8994_AIF2DAC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002456 } else {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002457 lrclk_reg = WM8994_AIF2ADC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002458 dev_dbg(codec->dev, "AIF2 using split LRCLK\n");
2459 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002460 break;
2461 default:
2462 return -EINVAL;
2463 }
2464
2465 bclk_rate = params_rate(params) * 2;
2466 switch (params_format(params)) {
2467 case SNDRV_PCM_FORMAT_S16_LE:
2468 bclk_rate *= 16;
2469 break;
2470 case SNDRV_PCM_FORMAT_S20_3LE:
2471 bclk_rate *= 20;
2472 aif1 |= 0x20;
2473 break;
2474 case SNDRV_PCM_FORMAT_S24_LE:
2475 bclk_rate *= 24;
2476 aif1 |= 0x40;
2477 break;
2478 case SNDRV_PCM_FORMAT_S32_LE:
2479 bclk_rate *= 32;
2480 aif1 |= 0x60;
2481 break;
2482 default:
2483 return -EINVAL;
2484 }
2485
2486 /* Try to find an appropriate sample rate; look for an exact match. */
2487 for (i = 0; i < ARRAY_SIZE(srs); i++)
2488 if (srs[i].rate == params_rate(params))
2489 break;
2490 if (i == ARRAY_SIZE(srs))
2491 return -EINVAL;
2492 rate_val |= srs[i].val << WM8994_AIF1_SR_SHIFT;
2493
2494 dev_dbg(dai->dev, "Sample rate is %dHz\n", srs[i].rate);
2495 dev_dbg(dai->dev, "AIF%dCLK is %dHz, target BCLK %dHz\n",
2496 dai->id, wm8994->aifclk[id], bclk_rate);
2497
Mark Brownb1e43d92010-12-07 17:14:56 +00002498 if (params_channels(params) == 1 &&
2499 (snd_soc_read(codec, aif1_reg) & 0x18) == 0x18)
2500 aif2 |= WM8994_AIF1_MONO;
2501
Mark Brown9e6e96a2010-01-29 17:47:12 +00002502 if (wm8994->aifclk[id] == 0) {
2503 dev_err(dai->dev, "AIF%dCLK not configured\n", dai->id);
2504 return -EINVAL;
2505 }
2506
2507 /* AIFCLK/fs ratio; look for a close match in either direction */
2508 best = 0;
2509 best_val = abs((fs_ratios[0] * params_rate(params))
2510 - wm8994->aifclk[id]);
2511 for (i = 1; i < ARRAY_SIZE(fs_ratios); i++) {
2512 cur_val = abs((fs_ratios[i] * params_rate(params))
2513 - wm8994->aifclk[id]);
2514 if (cur_val >= best_val)
2515 continue;
2516 best = i;
2517 best_val = cur_val;
2518 }
2519 dev_dbg(dai->dev, "Selected AIF%dCLK/fs = %d\n",
2520 dai->id, fs_ratios[best]);
2521 rate_val |= best;
2522
2523 /* We may not get quite the right frequency if using
2524 * approximate clocks so look for the closest match that is
2525 * higher than the target (we need to ensure that there enough
2526 * BCLKs to clock out the samples).
2527 */
2528 best = 0;
2529 for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
Joonyoung Shim07cd8ad2010-02-02 18:53:19 +09002530 cur_val = (wm8994->aifclk[id] * 10 / bclk_divs[i]) - bclk_rate;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002531 if (cur_val < 0) /* BCLK table is sorted */
2532 break;
2533 best = i;
2534 }
Joonyoung Shim07cd8ad2010-02-02 18:53:19 +09002535 bclk_rate = wm8994->aifclk[id] * 10 / bclk_divs[best];
Mark Brown9e6e96a2010-01-29 17:47:12 +00002536 dev_dbg(dai->dev, "Using BCLK_DIV %d for actual BCLK %dHz\n",
2537 bclk_divs[best], bclk_rate);
2538 bclk |= best << WM8994_AIF1_BCLK_DIV_SHIFT;
2539
2540 lrclk = bclk_rate / params_rate(params);
Mark Brownfc07ecd2011-11-28 21:16:56 +00002541 if (!lrclk) {
2542 dev_err(dai->dev, "Unable to generate LRCLK from %dHz BCLK\n",
2543 bclk_rate);
2544 return -EINVAL;
2545 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002546 dev_dbg(dai->dev, "Using LRCLK rate %d for actual LRCLK %dHz\n",
2547 lrclk, bclk_rate / lrclk);
2548
2549 snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
Mark Brownb1e43d92010-12-07 17:14:56 +00002550 snd_soc_update_bits(codec, aif2_reg, WM8994_AIF1_MONO, aif2);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002551 snd_soc_update_bits(codec, bclk_reg, WM8994_AIF1_BCLK_DIV_MASK, bclk);
2552 snd_soc_update_bits(codec, lrclk_reg, WM8994_AIF1DAC_RATE_MASK,
2553 lrclk);
2554 snd_soc_update_bits(codec, rate_reg, WM8994_AIF1_SR_MASK |
2555 WM8994_AIF1CLK_RATE_MASK, rate_val);
2556
2557 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
2558 switch (dai->id) {
2559 case 1:
2560 wm8994->dac_rates[0] = params_rate(params);
2561 wm8994_set_retune_mobile(codec, 0);
2562 wm8994_set_retune_mobile(codec, 1);
2563 break;
2564 case 2:
2565 wm8994->dac_rates[1] = params_rate(params);
2566 wm8994_set_retune_mobile(codec, 2);
2567 break;
2568 }
2569 }
2570
2571 return 0;
2572}
2573
Mark Brownc4431df2010-11-26 15:21:07 +00002574static int wm8994_aif3_hw_params(struct snd_pcm_substream *substream,
2575 struct snd_pcm_hw_params *params,
2576 struct snd_soc_dai *dai)
2577{
2578 struct snd_soc_codec *codec = dai->codec;
Mark Brown2a8a8562011-07-24 12:20:41 +01002579 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2580 struct wm8994 *control = wm8994->wm8994;
Mark Brownc4431df2010-11-26 15:21:07 +00002581 int aif1_reg;
2582 int aif1 = 0;
2583
2584 switch (dai->id) {
2585 case 3:
2586 switch (control->type) {
Mark Brown81204c82011-05-24 17:35:53 +08002587 case WM1811:
Mark Brownc4431df2010-11-26 15:21:07 +00002588 case WM8958:
2589 aif1_reg = WM8958_AIF3_CONTROL_1;
2590 break;
2591 default:
2592 return 0;
2593 }
2594 default:
2595 return 0;
2596 }
2597
2598 switch (params_format(params)) {
2599 case SNDRV_PCM_FORMAT_S16_LE:
2600 break;
2601 case SNDRV_PCM_FORMAT_S20_3LE:
2602 aif1 |= 0x20;
2603 break;
2604 case SNDRV_PCM_FORMAT_S24_LE:
2605 aif1 |= 0x40;
2606 break;
2607 case SNDRV_PCM_FORMAT_S32_LE:
2608 aif1 |= 0x60;
2609 break;
2610 default:
2611 return -EINVAL;
2612 }
2613
2614 return snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
2615}
2616
Mark Brown7d021732011-07-14 17:11:38 +09002617static void wm8994_aif_shutdown(struct snd_pcm_substream *substream,
2618 struct snd_soc_dai *dai)
2619{
2620 struct snd_soc_codec *codec = dai->codec;
2621 int rate_reg = 0;
2622
2623 switch (dai->id) {
2624 case 1:
2625 rate_reg = WM8994_AIF1_RATE;
2626 break;
2627 case 2:
Axel Linc527e6a2011-10-04 22:07:18 +08002628 rate_reg = WM8994_AIF2_RATE;
Mark Brown7d021732011-07-14 17:11:38 +09002629 break;
2630 default:
2631 break;
2632 }
2633
2634 /* If the DAI is idle then configure the divider tree for the
2635 * lowest output rate to save a little power if the clock is
2636 * still active (eg, because it is system clock).
2637 */
2638 if (rate_reg && !dai->playback_active && !dai->capture_active)
2639 snd_soc_update_bits(codec, rate_reg,
2640 WM8994_AIF1_SR_MASK |
2641 WM8994_AIF1CLK_RATE_MASK, 0x9);
2642}
2643
Mark Brown9e6e96a2010-01-29 17:47:12 +00002644static int wm8994_aif_mute(struct snd_soc_dai *codec_dai, int mute)
2645{
2646 struct snd_soc_codec *codec = codec_dai->codec;
2647 int mute_reg;
2648 int reg;
2649
2650 switch (codec_dai->id) {
2651 case 1:
2652 mute_reg = WM8994_AIF1_DAC1_FILTERS_1;
2653 break;
2654 case 2:
2655 mute_reg = WM8994_AIF2_DAC_FILTERS_1;
2656 break;
2657 default:
2658 return -EINVAL;
2659 }
2660
2661 if (mute)
2662 reg = WM8994_AIF1DAC1_MUTE;
2663 else
2664 reg = 0;
2665
2666 snd_soc_update_bits(codec, mute_reg, WM8994_AIF1DAC1_MUTE, reg);
2667
2668 return 0;
2669}
2670
Mark Brown778a76e2010-03-22 22:05:10 +00002671static int wm8994_set_tristate(struct snd_soc_dai *codec_dai, int tristate)
2672{
2673 struct snd_soc_codec *codec = codec_dai->codec;
2674 int reg, val, mask;
2675
2676 switch (codec_dai->id) {
2677 case 1:
2678 reg = WM8994_AIF1_MASTER_SLAVE;
2679 mask = WM8994_AIF1_TRI;
2680 break;
2681 case 2:
2682 reg = WM8994_AIF2_MASTER_SLAVE;
2683 mask = WM8994_AIF2_TRI;
2684 break;
2685 case 3:
2686 reg = WM8994_POWER_MANAGEMENT_6;
2687 mask = WM8994_AIF3_TRI;
2688 break;
2689 default:
2690 return -EINVAL;
2691 }
2692
2693 if (tristate)
2694 val = mask;
2695 else
2696 val = 0;
2697
Qiao Zhou78b3fb42011-01-19 19:10:47 +08002698 return snd_soc_update_bits(codec, reg, mask, val);
Mark Brown778a76e2010-03-22 22:05:10 +00002699}
2700
Mark Brownd09f3ec2011-08-15 11:01:02 +09002701static int wm8994_aif2_probe(struct snd_soc_dai *dai)
2702{
2703 struct snd_soc_codec *codec = dai->codec;
2704
2705 /* Disable the pulls on the AIF if we're using it to save power. */
2706 snd_soc_update_bits(codec, WM8994_GPIO_3,
2707 WM8994_GPN_PU | WM8994_GPN_PD, 0);
2708 snd_soc_update_bits(codec, WM8994_GPIO_4,
2709 WM8994_GPN_PU | WM8994_GPN_PD, 0);
2710 snd_soc_update_bits(codec, WM8994_GPIO_5,
2711 WM8994_GPN_PU | WM8994_GPN_PD, 0);
2712
2713 return 0;
2714}
2715
Mark Brown9e6e96a2010-01-29 17:47:12 +00002716#define WM8994_RATES SNDRV_PCM_RATE_8000_96000
2717
2718#define WM8994_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
Ian Lartey3079aed2010-08-31 23:56:34 +01002719 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002720
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01002721static const struct snd_soc_dai_ops wm8994_aif1_dai_ops = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002722 .set_sysclk = wm8994_set_dai_sysclk,
2723 .set_fmt = wm8994_set_dai_fmt,
2724 .hw_params = wm8994_hw_params,
Mark Brown7d021732011-07-14 17:11:38 +09002725 .shutdown = wm8994_aif_shutdown,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002726 .digital_mute = wm8994_aif_mute,
2727 .set_pll = wm8994_set_fll,
Mark Brown778a76e2010-03-22 22:05:10 +00002728 .set_tristate = wm8994_set_tristate,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002729};
2730
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01002731static const struct snd_soc_dai_ops wm8994_aif2_dai_ops = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002732 .set_sysclk = wm8994_set_dai_sysclk,
2733 .set_fmt = wm8994_set_dai_fmt,
2734 .hw_params = wm8994_hw_params,
Mark Brown7d021732011-07-14 17:11:38 +09002735 .shutdown = wm8994_aif_shutdown,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002736 .digital_mute = wm8994_aif_mute,
2737 .set_pll = wm8994_set_fll,
Mark Brown778a76e2010-03-22 22:05:10 +00002738 .set_tristate = wm8994_set_tristate,
2739};
2740
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01002741static const struct snd_soc_dai_ops wm8994_aif3_dai_ops = {
Mark Brownc4431df2010-11-26 15:21:07 +00002742 .hw_params = wm8994_aif3_hw_params,
Mark Brown778a76e2010-03-22 22:05:10 +00002743 .set_tristate = wm8994_set_tristate,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002744};
2745
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002746static struct snd_soc_dai_driver wm8994_dai[] = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002747 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002748 .name = "wm8994-aif1",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002749 .id = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002750 .playback = {
2751 .stream_name = "AIF1 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002752 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002753 .channels_max = 2,
2754 .rates = WM8994_RATES,
2755 .formats = WM8994_FORMATS,
2756 },
2757 .capture = {
2758 .stream_name = "AIF1 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002759 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002760 .channels_max = 2,
2761 .rates = WM8994_RATES,
2762 .formats = WM8994_FORMATS,
2763 },
2764 .ops = &wm8994_aif1_dai_ops,
2765 },
2766 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002767 .name = "wm8994-aif2",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002768 .id = 2,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002769 .playback = {
2770 .stream_name = "AIF2 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002771 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002772 .channels_max = 2,
2773 .rates = WM8994_RATES,
2774 .formats = WM8994_FORMATS,
2775 },
2776 .capture = {
2777 .stream_name = "AIF2 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002778 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002779 .channels_max = 2,
2780 .rates = WM8994_RATES,
2781 .formats = WM8994_FORMATS,
2782 },
Mark Brownd09f3ec2011-08-15 11:01:02 +09002783 .probe = wm8994_aif2_probe,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002784 .ops = &wm8994_aif2_dai_ops,
2785 },
2786 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002787 .name = "wm8994-aif3",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002788 .id = 3,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002789 .playback = {
2790 .stream_name = "AIF3 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002791 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002792 .channels_max = 2,
2793 .rates = WM8994_RATES,
2794 .formats = WM8994_FORMATS,
2795 },
Dan Carpentera8462bd2010-03-24 14:58:34 +03002796 .capture = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002797 .stream_name = "AIF3 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002798 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002799 .channels_max = 2,
2800 .rates = WM8994_RATES,
2801 .formats = WM8994_FORMATS,
2802 },
Mark Brown778a76e2010-03-22 22:05:10 +00002803 .ops = &wm8994_aif3_dai_ops,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002804 }
2805};
Mark Brown9e6e96a2010-01-29 17:47:12 +00002806
2807#ifdef CONFIG_PM
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002808static int wm8994_suspend(struct snd_soc_codec *codec, pm_message_t state)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002809{
Mark Brownb2c812e2010-04-14 15:35:19 +09002810 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01002811 struct wm8994 *control = wm8994->wm8994;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002812 int i, ret;
2813
Mark Brownca629922011-05-11 14:34:53 +02002814 switch (control->type) {
2815 case WM8994:
2816 snd_soc_update_bits(codec, WM8994_MICBIAS, WM8994_MICD_ENA, 0);
2817 break;
Mark Brown81204c82011-05-24 17:35:53 +08002818 case WM1811:
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002819 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
2820 WM1811_JACKDET_MODE_MASK, 0);
2821 /* Fall through */
Mark Brownca629922011-05-11 14:34:53 +02002822 case WM8958:
2823 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
2824 WM8958_MICD_ENA, 0);
2825 break;
2826 }
2827
Mark Brown9e6e96a2010-01-29 17:47:12 +00002828 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
2829 memcpy(&wm8994->fll_suspend[i], &wm8994->fll[i],
Mark Brownf701a2e2011-03-09 19:31:01 +00002830 sizeof(struct wm8994_fll_config));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002831 ret = _wm8994_set_fll(codec, i + 1, 0, 0, 0);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002832 if (ret < 0)
2833 dev_warn(codec->dev, "Failed to stop FLL%d: %d\n",
2834 i + 1, ret);
2835 }
2836
2837 wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);
2838
2839 return 0;
2840}
2841
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002842static int wm8994_resume(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002843{
Mark Brownb2c812e2010-04-14 15:35:19 +09002844 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01002845 struct wm8994 *control = wm8994->wm8994;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002846 int i, ret;
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00002847 unsigned int val, mask;
2848
2849 if (wm8994->revision < 4) {
2850 /* force a HW read */
2851 val = wm8994_reg_read(codec->control_data,
2852 WM8994_POWER_MANAGEMENT_5);
2853
2854 /* modify the cache only */
2855 codec->cache_only = 1;
2856 mask = WM8994_DAC1R_ENA | WM8994_DAC1L_ENA |
2857 WM8994_DAC2R_ENA | WM8994_DAC2L_ENA;
2858 val &= mask;
2859 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
2860 mask, val);
2861 codec->cache_only = 0;
2862 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002863
2864 /* Restore the registers */
Mark Brownca9aef52010-11-26 17:23:41 +00002865 ret = snd_soc_cache_sync(codec);
2866 if (ret != 0)
2867 dev_err(codec->dev, "Failed to sync cache: %d\n", ret);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002868
2869 wm8994_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
2870
2871 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
Mark Brown6a2f1ee2010-05-10 18:36:37 +01002872 if (!wm8994->fll_suspend[i].out)
2873 continue;
2874
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002875 ret = _wm8994_set_fll(codec, i + 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002876 wm8994->fll_suspend[i].src,
2877 wm8994->fll_suspend[i].in,
2878 wm8994->fll_suspend[i].out);
2879 if (ret < 0)
2880 dev_warn(codec->dev, "Failed to restore FLL%d: %d\n",
2881 i + 1, ret);
2882 }
2883
Mark Brownca629922011-05-11 14:34:53 +02002884 switch (control->type) {
2885 case WM8994:
2886 if (wm8994->micdet[0].jack || wm8994->micdet[1].jack)
2887 snd_soc_update_bits(codec, WM8994_MICBIAS,
2888 WM8994_MICD_ENA, WM8994_MICD_ENA);
2889 break;
Mark Brown81204c82011-05-24 17:35:53 +08002890 case WM1811:
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002891 if (wm8994->jackdet && wm8994->jack_cb) {
2892 /* Restart from idle */
2893 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
2894 WM1811_JACKDET_MODE_MASK,
2895 WM1811_JACKDET_MODE_JACK);
2896 break;
2897 }
Mark Brownca629922011-05-11 14:34:53 +02002898 case WM8958:
2899 if (wm8994->jack_cb)
2900 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
2901 WM8958_MICD_ENA, WM8958_MICD_ENA);
2902 break;
2903 }
2904
Mark Brown9e6e96a2010-01-29 17:47:12 +00002905 return 0;
2906}
2907#else
2908#define wm8994_suspend NULL
2909#define wm8994_resume NULL
2910#endif
2911
2912static void wm8994_handle_retune_mobile_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 struct snd_kcontrol_new controls[] = {
2917 SOC_ENUM_EXT("AIF1.1 EQ Mode",
2918 wm8994->retune_mobile_enum,
2919 wm8994_get_retune_mobile_enum,
2920 wm8994_put_retune_mobile_enum),
2921 SOC_ENUM_EXT("AIF1.2 EQ Mode",
2922 wm8994->retune_mobile_enum,
2923 wm8994_get_retune_mobile_enum,
2924 wm8994_put_retune_mobile_enum),
2925 SOC_ENUM_EXT("AIF2 EQ Mode",
2926 wm8994->retune_mobile_enum,
2927 wm8994_get_retune_mobile_enum,
2928 wm8994_put_retune_mobile_enum),
2929 };
2930 int ret, i, j;
2931 const char **t;
2932
2933 /* We need an array of texts for the enum API but the number
2934 * of texts is likely to be less than the number of
2935 * configurations due to the sample rate dependency of the
2936 * configurations. */
2937 wm8994->num_retune_mobile_texts = 0;
2938 wm8994->retune_mobile_texts = NULL;
2939 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
2940 for (j = 0; j < wm8994->num_retune_mobile_texts; j++) {
2941 if (strcmp(pdata->retune_mobile_cfgs[i].name,
2942 wm8994->retune_mobile_texts[j]) == 0)
2943 break;
2944 }
2945
2946 if (j != wm8994->num_retune_mobile_texts)
2947 continue;
2948
2949 /* Expand the array... */
2950 t = krealloc(wm8994->retune_mobile_texts,
2951 sizeof(char *) *
2952 (wm8994->num_retune_mobile_texts + 1),
2953 GFP_KERNEL);
2954 if (t == NULL)
2955 continue;
2956
2957 /* ...store the new entry... */
2958 t[wm8994->num_retune_mobile_texts] =
2959 pdata->retune_mobile_cfgs[i].name;
2960
2961 /* ...and remember the new version. */
2962 wm8994->num_retune_mobile_texts++;
2963 wm8994->retune_mobile_texts = t;
2964 }
2965
2966 dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
2967 wm8994->num_retune_mobile_texts);
2968
2969 wm8994->retune_mobile_enum.max = wm8994->num_retune_mobile_texts;
2970 wm8994->retune_mobile_enum.texts = wm8994->retune_mobile_texts;
2971
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002972 ret = snd_soc_add_controls(wm8994->codec, controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002973 ARRAY_SIZE(controls));
2974 if (ret != 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002975 dev_err(wm8994->codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002976 "Failed to add ReTune Mobile controls: %d\n", ret);
2977}
2978
2979static void wm8994_handle_pdata(struct wm8994_priv *wm8994)
2980{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002981 struct snd_soc_codec *codec = wm8994->codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002982 struct wm8994_pdata *pdata = wm8994->pdata;
2983 int ret, i;
2984
2985 if (!pdata)
2986 return;
2987
2988 wm_hubs_handle_analogue_pdata(codec, pdata->lineout1_diff,
2989 pdata->lineout2_diff,
2990 pdata->lineout1fb,
2991 pdata->lineout2fb,
2992 pdata->jd_scthr,
2993 pdata->jd_thr,
2994 pdata->micbias1_lvl,
2995 pdata->micbias2_lvl);
2996
2997 dev_dbg(codec->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
2998
2999 if (pdata->num_drc_cfgs) {
3000 struct snd_kcontrol_new controls[] = {
3001 SOC_ENUM_EXT("AIF1DRC1 Mode", wm8994->drc_enum,
3002 wm8994_get_drc_enum, wm8994_put_drc_enum),
3003 SOC_ENUM_EXT("AIF1DRC2 Mode", wm8994->drc_enum,
3004 wm8994_get_drc_enum, wm8994_put_drc_enum),
3005 SOC_ENUM_EXT("AIF2DRC Mode", wm8994->drc_enum,
3006 wm8994_get_drc_enum, wm8994_put_drc_enum),
3007 };
3008
3009 /* We need an array of texts for the enum API */
3010 wm8994->drc_texts = kmalloc(sizeof(char *)
3011 * pdata->num_drc_cfgs, GFP_KERNEL);
3012 if (!wm8994->drc_texts) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003013 dev_err(wm8994->codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003014 "Failed to allocate %d DRC config texts\n",
3015 pdata->num_drc_cfgs);
3016 return;
3017 }
3018
3019 for (i = 0; i < pdata->num_drc_cfgs; i++)
3020 wm8994->drc_texts[i] = pdata->drc_cfgs[i].name;
3021
3022 wm8994->drc_enum.max = pdata->num_drc_cfgs;
3023 wm8994->drc_enum.texts = wm8994->drc_texts;
3024
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003025 ret = snd_soc_add_controls(wm8994->codec, controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003026 ARRAY_SIZE(controls));
3027 if (ret != 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003028 dev_err(wm8994->codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003029 "Failed to add DRC mode controls: %d\n", ret);
3030
3031 for (i = 0; i < WM8994_NUM_DRC; i++)
3032 wm8994_set_drc(codec, i);
3033 }
3034
3035 dev_dbg(codec->dev, "%d ReTune Mobile configurations\n",
3036 pdata->num_retune_mobile_cfgs);
3037
3038 if (pdata->num_retune_mobile_cfgs)
3039 wm8994_handle_retune_mobile_pdata(wm8994);
3040 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003041 snd_soc_add_controls(wm8994->codec, wm8994_eq_controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003042 ARRAY_SIZE(wm8994_eq_controls));
Mark Brown48e028e2011-02-21 17:11:59 -08003043
3044 for (i = 0; i < ARRAY_SIZE(pdata->micbias); i++) {
3045 if (pdata->micbias[i]) {
3046 snd_soc_write(codec, WM8958_MICBIAS1 + i,
3047 pdata->micbias[i] & 0xffff);
3048 }
3049 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00003050}
3051
Mark Brown88766982010-03-29 20:57:12 +01003052/**
3053 * wm8994_mic_detect - Enable microphone detection via the WM8994 IRQ
3054 *
3055 * @codec: WM8994 codec
3056 * @jack: jack to report detection events on
3057 * @micbias: microphone bias to detect on
3058 * @det: value to report for presence detection
3059 * @shrt: value to report for short detection
3060 *
3061 * Enable microphone detection via IRQ on the WM8994. If GPIOs are
3062 * being used to bring out signals to the processor then only platform
Mark Brown5ab230a2010-09-06 14:59:34 +01003063 * data configuration is needed for WM8994 and processor GPIOs should
Mark Brown88766982010-03-29 20:57:12 +01003064 * be configured using snd_soc_jack_add_gpios() instead.
3065 *
3066 * Configuration of detection levels is available via the micbias1_lvl
3067 * and micbias2_lvl platform data members.
3068 */
3069int wm8994_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
3070 int micbias, int det, int shrt)
3071{
Mark Brownb2c812e2010-04-14 15:35:19 +09003072 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown88766982010-03-29 20:57:12 +01003073 struct wm8994_micdet *micdet;
Mark Brown2a8a8562011-07-24 12:20:41 +01003074 struct wm8994 *control = wm8994->wm8994;
Mark Brown88766982010-03-29 20:57:12 +01003075 int reg;
3076
Mark Brown3a423152010-11-26 15:21:06 +00003077 if (control->type != WM8994)
3078 return -EINVAL;
3079
Mark Brown88766982010-03-29 20:57:12 +01003080 switch (micbias) {
3081 case 1:
3082 micdet = &wm8994->micdet[0];
3083 break;
3084 case 2:
3085 micdet = &wm8994->micdet[1];
3086 break;
3087 default:
3088 return -EINVAL;
3089 }
3090
3091 dev_dbg(codec->dev, "Configuring microphone detection on %d: %x %x\n",
3092 micbias, det, shrt);
3093
3094 /* Store the configuration */
3095 micdet->jack = jack;
3096 micdet->det = det;
3097 micdet->shrt = shrt;
3098
3099 /* If either of the jacks is set up then enable detection */
3100 if (wm8994->micdet[0].jack || wm8994->micdet[1].jack)
3101 reg = WM8994_MICD_ENA;
3102 else
3103 reg = 0;
3104
3105 snd_soc_update_bits(codec, WM8994_MICBIAS, WM8994_MICD_ENA, reg);
3106
3107 return 0;
3108}
3109EXPORT_SYMBOL_GPL(wm8994_mic_detect);
3110
3111static irqreturn_t wm8994_mic_irq(int irq, void *data)
3112{
3113 struct wm8994_priv *priv = data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003114 struct snd_soc_codec *codec = priv->codec;
Mark Brown88766982010-03-29 20:57:12 +01003115 int reg;
3116 int report;
3117
Mark Brown7116f452010-12-29 13:05:21 +00003118#ifndef CONFIG_SND_SOC_WM8994_MODULE
Mark Brown2bbb5d62010-12-05 12:50:12 +00003119 trace_snd_soc_jack_irq(dev_name(codec->dev));
Mark Brown7116f452010-12-29 13:05:21 +00003120#endif
Mark Brown2bbb5d62010-12-05 12:50:12 +00003121
Mark Brown88766982010-03-29 20:57:12 +01003122 reg = snd_soc_read(codec, WM8994_INTERRUPT_RAW_STATUS_2);
3123 if (reg < 0) {
3124 dev_err(codec->dev, "Failed to read microphone status: %d\n",
3125 reg);
3126 return IRQ_HANDLED;
3127 }
3128
3129 dev_dbg(codec->dev, "Microphone status: %x\n", reg);
3130
3131 report = 0;
3132 if (reg & WM8994_MIC1_DET_STS)
3133 report |= priv->micdet[0].det;
3134 if (reg & WM8994_MIC1_SHRT_STS)
3135 report |= priv->micdet[0].shrt;
3136 snd_soc_jack_report(priv->micdet[0].jack, report,
3137 priv->micdet[0].det | priv->micdet[0].shrt);
3138
3139 report = 0;
3140 if (reg & WM8994_MIC2_DET_STS)
3141 report |= priv->micdet[1].det;
3142 if (reg & WM8994_MIC2_SHRT_STS)
3143 report |= priv->micdet[1].shrt;
3144 snd_soc_jack_report(priv->micdet[1].jack, report,
3145 priv->micdet[1].det | priv->micdet[1].shrt);
3146
3147 return IRQ_HANDLED;
3148}
3149
Mark Brown821edd22010-11-26 15:21:09 +00003150/* Default microphone detection handler for WM8958 - the user can
3151 * override this if they wish.
3152 */
3153static void wm8958_default_micdet(u16 status, void *data)
3154{
3155 struct snd_soc_codec *codec = data;
3156 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown4585790d2011-11-30 10:55:14 +00003157 int report;
Mark Brown821edd22010-11-26 15:21:09 +00003158
Mark Browna1691342011-11-30 14:56:40 +00003159 dev_dbg(codec->dev, "MICDET %x\n", status);
3160
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003161 /* Either nothing present or just starting detection */
Mark Brownb00adf72011-08-13 11:57:18 +09003162 if (!(status & WM8958_MICD_STS)) {
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003163 if (!wm8994->jackdet) {
3164 /* If nothing present then clear our statuses */
3165 dev_dbg(codec->dev, "Detected open circuit\n");
3166 wm8994->jack_mic = false;
3167 wm8994->mic_detecting = true;
Mark Brown821edd22010-11-26 15:21:09 +00003168
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003169 wm8958_micd_set_rate(codec);
Mark Brown821edd22010-11-26 15:21:09 +00003170
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003171 snd_soc_jack_report(wm8994->micdet[0].jack, 0,
3172 wm8994->btn_mask |
3173 SND_JACK_HEADSET);
3174 }
Mark Brownb00adf72011-08-13 11:57:18 +09003175 return;
3176 }
3177
3178 /* If the measurement is showing a high impedence we've got a
3179 * microphone.
3180 */
Mark Brown157a75e2011-11-30 13:43:51 +00003181 if (wm8994->mic_detecting && (status & 0x600)) {
Mark Brownb00adf72011-08-13 11:57:18 +09003182 dev_dbg(codec->dev, "Detected microphone\n");
3183
Mark Brown157a75e2011-11-30 13:43:51 +00003184 wm8994->mic_detecting = false;
Mark Brownb00adf72011-08-13 11:57:18 +09003185 wm8994->jack_mic = true;
3186
3187 wm8958_micd_set_rate(codec);
3188
3189 snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADSET,
3190 SND_JACK_HEADSET);
3191 }
3192
3193
Mark Brown157a75e2011-11-30 13:43:51 +00003194 if (wm8994->mic_detecting && status & 0x4) {
Mark Brownb00adf72011-08-13 11:57:18 +09003195 dev_dbg(codec->dev, "Detected headphone\n");
Mark Brown157a75e2011-11-30 13:43:51 +00003196 wm8994->mic_detecting = false;
Mark Brownb00adf72011-08-13 11:57:18 +09003197
3198 wm8958_micd_set_rate(codec);
3199
3200 snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADPHONE,
3201 SND_JACK_HEADSET);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003202
3203 /* If we have jackdet that will detect removal */
3204 if (wm8994->jackdet) {
3205 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3206 WM8958_MICD_ENA, 0);
3207
3208 wm1811_jackdet_set_mode(codec,
3209 WM1811_JACKDET_MODE_JACK);
3210 }
Mark Brownb00adf72011-08-13 11:57:18 +09003211 }
3212
3213 /* Report short circuit as a button */
3214 if (wm8994->jack_mic) {
Mark Brown4585790d2011-11-30 10:55:14 +00003215 report = 0;
Mark Brownb00adf72011-08-13 11:57:18 +09003216 if (status & 0x4)
Mark Brown4585790d2011-11-30 10:55:14 +00003217 report |= SND_JACK_BTN_0;
3218
3219 if (status & 0x8)
3220 report |= SND_JACK_BTN_1;
3221
3222 if (status & 0x10)
3223 report |= SND_JACK_BTN_2;
3224
3225 if (status & 0x20)
3226 report |= SND_JACK_BTN_3;
3227
3228 if (status & 0x40)
3229 report |= SND_JACK_BTN_4;
3230
3231 if (status & 0x80)
3232 report |= SND_JACK_BTN_5;
3233
3234 snd_soc_jack_report(wm8994->micdet[0].jack, report,
3235 wm8994->btn_mask);
Mark Brownb00adf72011-08-13 11:57:18 +09003236 }
Mark Brown821edd22010-11-26 15:21:09 +00003237}
3238
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003239static irqreturn_t wm1811_jackdet_irq(int irq, void *data)
3240{
3241 struct wm8994_priv *wm8994 = data;
3242 struct snd_soc_codec *codec = wm8994->codec;
3243 int reg;
3244
3245 mutex_lock(&wm8994->accdet_lock);
3246
3247 reg = snd_soc_read(codec, WM1811_JACKDET_CTRL);
3248 if (reg < 0) {
3249 dev_err(codec->dev, "Failed to read jack status: %d\n", reg);
3250 mutex_unlock(&wm8994->accdet_lock);
3251 return IRQ_NONE;
3252 }
3253
3254 dev_dbg(codec->dev, "JACKDET %x\n", reg);
3255
3256 if (reg & WM1811_JACKDET_LVL) {
3257 dev_dbg(codec->dev, "Jack detected\n");
3258
3259 snd_soc_jack_report(wm8994->micdet[0].jack,
3260 SND_JACK_MECHANICAL, SND_JACK_MECHANICAL);
3261
3262 /*
3263 * Start off measument of microphone impedence to find
3264 * out what's actually there.
3265 */
3266 wm8994->mic_detecting = true;
3267 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_MIC);
3268 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3269 WM8958_MICD_ENA, WM8958_MICD_ENA);
3270 } else {
3271 dev_dbg(codec->dev, "Jack not detected\n");
3272
3273 snd_soc_jack_report(wm8994->micdet[0].jack, 0,
3274 SND_JACK_MECHANICAL | SND_JACK_HEADSET |
3275 wm8994->btn_mask);
3276
3277 wm8994->mic_detecting = false;
3278 wm8994->jack_mic = false;
3279 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3280 WM8958_MICD_ENA, 0);
3281 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_JACK);
3282 }
3283
3284 mutex_unlock(&wm8994->accdet_lock);
3285
3286 return IRQ_HANDLED;
3287}
3288
Mark Brown821edd22010-11-26 15:21:09 +00003289/**
3290 * wm8958_mic_detect - Enable microphone detection via the WM8958 IRQ
3291 *
3292 * @codec: WM8958 codec
3293 * @jack: jack to report detection events on
3294 *
3295 * Enable microphone detection functionality for the WM8958. By
3296 * default simple detection which supports the detection of up to 6
3297 * buttons plus video and microphone functionality is supported.
3298 *
3299 * The WM8958 has an advanced jack detection facility which is able to
3300 * support complex accessory detection, especially when used in
3301 * conjunction with external circuitry. In order to provide maximum
3302 * flexiblity a callback is provided which allows a completely custom
3303 * detection algorithm.
3304 */
3305int wm8958_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
3306 wm8958_micdet_cb cb, void *cb_data)
3307{
3308 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01003309 struct wm8994 *control = wm8994->wm8994;
Mark Brown4585790d2011-11-30 10:55:14 +00003310 u16 micd_lvl_sel;
Mark Brown821edd22010-11-26 15:21:09 +00003311
Mark Brown81204c82011-05-24 17:35:53 +08003312 switch (control->type) {
3313 case WM1811:
3314 case WM8958:
3315 break;
3316 default:
Mark Brown821edd22010-11-26 15:21:09 +00003317 return -EINVAL;
Mark Brown81204c82011-05-24 17:35:53 +08003318 }
Mark Brown821edd22010-11-26 15:21:09 +00003319
3320 if (jack) {
3321 if (!cb) {
3322 dev_dbg(codec->dev, "Using default micdet callback\n");
3323 cb = wm8958_default_micdet;
3324 cb_data = codec;
3325 }
3326
Mark Brown4cdf5e42011-11-29 14:36:17 +00003327 snd_soc_dapm_force_enable_pin(&codec->dapm, "CLK_SYS");
3328
Mark Brown821edd22010-11-26 15:21:09 +00003329 wm8994->micdet[0].jack = jack;
3330 wm8994->jack_cb = cb;
3331 wm8994->jack_cb_data = cb_data;
3332
Mark Brown157a75e2011-11-30 13:43:51 +00003333 wm8994->mic_detecting = true;
Mark Brownb00adf72011-08-13 11:57:18 +09003334 wm8994->jack_mic = false;
3335
3336 wm8958_micd_set_rate(codec);
3337
Mark Brown4585790d2011-11-30 10:55:14 +00003338 /* Detect microphones and short circuits by default */
3339 if (wm8994->pdata->micd_lvl_sel)
3340 micd_lvl_sel = wm8994->pdata->micd_lvl_sel;
3341 else
3342 micd_lvl_sel = 0x41;
3343
3344 wm8994->btn_mask = SND_JACK_BTN_0 | SND_JACK_BTN_1 |
3345 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
3346 SND_JACK_BTN_4 | SND_JACK_BTN_5;
3347
Mark Brownb00adf72011-08-13 11:57:18 +09003348 snd_soc_update_bits(codec, WM8958_MIC_DETECT_2,
Mark Brown4585790d2011-11-30 10:55:14 +00003349 WM8958_MICD_LVL_SEL_MASK, micd_lvl_sel);
Mark Brownb00adf72011-08-13 11:57:18 +09003350
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003351 WARN_ON(codec->dapm.bias_level > SND_SOC_BIAS_STANDBY);
3352
3353 /*
3354 * If we can use jack detection start off with that,
3355 * otherwise jump straight to microphone detection.
3356 */
3357 if (wm8994->jackdet) {
3358 snd_soc_update_bits(codec, WM8994_LDO_1,
3359 WM8994_LDO1_DISCH, 0);
3360 wm1811_jackdet_set_mode(codec,
3361 WM1811_JACKDET_MODE_JACK);
3362 } else {
3363 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3364 WM8958_MICD_ENA, WM8958_MICD_ENA);
3365 }
3366
Mark Brown821edd22010-11-26 15:21:09 +00003367 } else {
3368 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3369 WM8958_MICD_ENA, 0);
Mark Brown4cdf5e42011-11-29 14:36:17 +00003370 snd_soc_dapm_disable_pin(&codec->dapm, "CLK_SYS");
Mark Brown821edd22010-11-26 15:21:09 +00003371 }
3372
3373 return 0;
3374}
3375EXPORT_SYMBOL_GPL(wm8958_mic_detect);
3376
3377static irqreturn_t wm8958_mic_irq(int irq, void *data)
3378{
3379 struct wm8994_priv *wm8994 = data;
3380 struct snd_soc_codec *codec = wm8994->codec;
Mark Brown19940b32011-08-19 18:05:05 +09003381 int reg, count;
Mark Brown821edd22010-11-26 15:21:09 +00003382
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003383 mutex_lock(&wm8994->accdet_lock);
3384
3385 /*
3386 * Jack detection may have detected a removal simulataneously
3387 * with an update of the MICDET status; if so it will have
3388 * stopped detection and we can ignore this interrupt.
3389 */
3390 if (!(snd_soc_read(codec, WM8958_MIC_DETECT_1) & WM8958_MICD_ENA)) {
3391 mutex_unlock(&wm8994->accdet_lock);
3392 return IRQ_HANDLED;
3393 }
3394
Mark Brown19940b32011-08-19 18:05:05 +09003395 /* We may occasionally read a detection without an impedence
3396 * range being provided - if that happens loop again.
3397 */
3398 count = 10;
3399 do {
3400 reg = snd_soc_read(codec, WM8958_MIC_DETECT_3);
3401 if (reg < 0) {
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003402 mutex_unlock(&wm8994->accdet_lock);
Mark Brown19940b32011-08-19 18:05:05 +09003403 dev_err(codec->dev,
3404 "Failed to read mic detect status: %d\n",
3405 reg);
3406 return IRQ_NONE;
3407 }
Mark Brown821edd22010-11-26 15:21:09 +00003408
Mark Brown19940b32011-08-19 18:05:05 +09003409 if (!(reg & WM8958_MICD_VALID)) {
3410 dev_dbg(codec->dev, "Mic detect data not valid\n");
3411 goto out;
3412 }
3413
3414 if (!(reg & WM8958_MICD_STS) || (reg & WM8958_MICD_LVL_MASK))
3415 break;
3416
3417 msleep(1);
3418 } while (count--);
3419
3420 if (count == 0)
3421 dev_warn(codec->dev, "No impedence range reported for jack\n");
Mark Brown821edd22010-11-26 15:21:09 +00003422
Mark Brown7116f452010-12-29 13:05:21 +00003423#ifndef CONFIG_SND_SOC_WM8994_MODULE
Mark Brown2bbb5d62010-12-05 12:50:12 +00003424 trace_snd_soc_jack_irq(dev_name(codec->dev));
Mark Brown7116f452010-12-29 13:05:21 +00003425#endif
Mark Brown2bbb5d62010-12-05 12:50:12 +00003426
Mark Brown821edd22010-11-26 15:21:09 +00003427 if (wm8994->jack_cb)
3428 wm8994->jack_cb(reg, wm8994->jack_cb_data);
3429 else
3430 dev_warn(codec->dev, "Accessory detection with no callback\n");
3431
3432out:
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003433 mutex_unlock(&wm8994->accdet_lock);
3434
Mark Brown821edd22010-11-26 15:21:09 +00003435 return IRQ_HANDLED;
3436}
3437
Mark Brown3b1af3f2011-07-14 12:38:18 +09003438static irqreturn_t wm8994_fifo_error(int irq, void *data)
3439{
3440 struct snd_soc_codec *codec = data;
3441
3442 dev_err(codec->dev, "FIFO error\n");
3443
3444 return IRQ_HANDLED;
3445}
3446
Mark Brownf0b182b2011-08-16 12:01:27 +09003447static irqreturn_t wm8994_temp_warn(int irq, void *data)
3448{
3449 struct snd_soc_codec *codec = data;
3450
3451 dev_err(codec->dev, "Thermal warning\n");
3452
3453 return IRQ_HANDLED;
3454}
3455
3456static irqreturn_t wm8994_temp_shut(int irq, void *data)
3457{
3458 struct snd_soc_codec *codec = data;
3459
3460 dev_crit(codec->dev, "Thermal shutdown\n");
3461
3462 return IRQ_HANDLED;
3463}
3464
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003465static int wm8994_codec_probe(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003466{
Mark Brown3a423152010-11-26 15:21:06 +00003467 struct wm8994 *control;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003468 struct wm8994_priv *wm8994;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003469 struct snd_soc_dapm_context *dapm = &codec->dapm;
Mark Brownec62dbd2010-08-15 14:56:40 +01003470 int ret, i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003471
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003472 codec->control_data = dev_get_drvdata(codec->dev->parent);
Mark Brown3a423152010-11-26 15:21:06 +00003473 control = codec->control_data;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003474
3475 wm8994 = kzalloc(sizeof(struct wm8994_priv), GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003476 if (wm8994 == NULL)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003477 return -ENOMEM;
Mark Brownb2c812e2010-04-14 15:35:19 +09003478 snd_soc_codec_set_drvdata(codec, wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003479
Mark Brown2a8a8562011-07-24 12:20:41 +01003480
3481 wm8994->wm8994 = dev_get_drvdata(codec->dev->parent);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003482 wm8994->pdata = dev_get_platdata(codec->dev->parent);
3483 wm8994->codec = codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003484
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003485 mutex_init(&wm8994->accdet_lock);
3486
Mark Brownc7ebf932011-07-12 19:47:59 +09003487 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
3488 init_completion(&wm8994->fll_locked[i]);
3489
Mark Brown9b7c5252011-02-17 20:05:44 -08003490 if (wm8994->pdata && wm8994->pdata->micdet_irq)
3491 wm8994->micdet_irq = wm8994->pdata->micdet_irq;
3492 else if (wm8994->pdata && wm8994->pdata->irq_base)
3493 wm8994->micdet_irq = wm8994->pdata->irq_base +
3494 WM8994_IRQ_MIC1_DET;
3495
Mark Brown39fb51a2010-11-26 17:23:43 +00003496 pm_runtime_enable(codec->dev);
3497 pm_runtime_resume(codec->dev);
3498
Mark Brownca9aef52010-11-26 17:23:41 +00003499 /* Read our current status back from the chip - we don't want to
3500 * reset as this may interfere with the GPIO or LDO operation. */
3501 for (i = 0; i < WM8994_CACHE_SIZE; i++) {
Dimitris Papastamosd4754ec2011-01-13 12:20:37 +00003502 if (!wm8994_readable(codec, i) || wm8994_volatile(codec, i))
Mark Brownca9aef52010-11-26 17:23:41 +00003503 continue;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003504
Mark Brownca9aef52010-11-26 17:23:41 +00003505 ret = wm8994_reg_read(codec->control_data, i);
3506 if (ret <= 0)
3507 continue;
3508
3509 ret = snd_soc_cache_write(codec, i, ret);
3510 if (ret != 0) {
3511 dev_err(codec->dev,
3512 "Failed to initialise cache for 0x%x: %d\n",
3513 i, ret);
3514 goto err;
3515 }
3516 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00003517
3518 /* Set revision-specific configuration */
Mark Brownb6b05692010-08-13 12:58:20 +01003519 wm8994->revision = snd_soc_read(codec, WM8994_CHIP_REVISION);
Mark Brown3a423152010-11-26 15:21:06 +00003520 switch (control->type) {
3521 case WM8994:
3522 switch (wm8994->revision) {
3523 case 2:
3524 case 3:
Mark Brown4537c4e2011-08-01 13:10:16 +09003525 wm8994->hubs.dcs_codes_l = -5;
3526 wm8994->hubs.dcs_codes_r = -5;
Mark Brown3a423152010-11-26 15:21:06 +00003527 wm8994->hubs.hp_startup_mode = 1;
3528 wm8994->hubs.dcs_readback_mode = 1;
Mark Brownf9acf9f2011-06-07 23:23:52 +01003529 wm8994->hubs.series_startup = 1;
Mark Brown3a423152010-11-26 15:21:06 +00003530 break;
3531 default:
Mark Brown79ef0ab2011-08-01 13:02:17 +09003532 wm8994->hubs.dcs_readback_mode = 2;
Mark Brown3a423152010-11-26 15:21:06 +00003533 break;
3534 }
Mark Brown280ec8b2011-08-10 22:19:19 +09003535 break;
Mark Brown3a423152010-11-26 15:21:06 +00003536
3537 case WM8958:
Mark Brown8437f702010-03-29 17:09:45 +01003538 wm8994->hubs.dcs_readback_mode = 1;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003539 break;
Mark Brown3a423152010-11-26 15:21:06 +00003540
Mark Brown81204c82011-05-24 17:35:53 +08003541 case WM1811:
3542 wm8994->hubs.dcs_readback_mode = 2;
3543 wm8994->hubs.no_series_update = 1;
3544
3545 switch (wm8994->revision) {
3546 case 0:
3547 case 1:
Mark Brownfc8e6e82011-11-28 18:48:46 +00003548 case 2:
3549 case 3:
Mark Brown6473a142011-10-17 19:38:52 +01003550 wm8994->hubs.dcs_codes_l = -9;
3551 wm8994->hubs.dcs_codes_r = -5;
Mark Brown81204c82011-05-24 17:35:53 +08003552 break;
3553 default:
3554 break;
3555 }
3556
3557 snd_soc_update_bits(codec, WM8994_ANALOGUE_HP_1,
3558 WM1811_HPOUT1_ATTN, WM1811_HPOUT1_ATTN);
3559 break;
3560
Mark Brown9e6e96a2010-01-29 17:47:12 +00003561 default:
3562 break;
3563 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00003564
Mark Brown2a8a8562011-07-24 12:20:41 +01003565 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR,
Mark Brown3b1af3f2011-07-14 12:38:18 +09003566 wm8994_fifo_error, "FIFO error", codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01003567 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN,
Mark Brownf0b182b2011-08-16 12:01:27 +09003568 wm8994_temp_warn, "Thermal warning", codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01003569 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT,
Mark Brownf0b182b2011-08-16 12:01:27 +09003570 wm8994_temp_shut, "Thermal shutdown", codec);
Mark Brown3b1af3f2011-07-14 12:38:18 +09003571
Mark Brown2a8a8562011-07-24 12:20:41 +01003572 ret = wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
Mark Brownb30ead52011-07-12 15:47:17 +09003573 wm_hubs_dcs_done, "DC servo done",
3574 &wm8994->hubs);
3575 if (ret == 0)
3576 wm8994->hubs.dcs_done_irq = true;
3577
Mark Brown3a423152010-11-26 15:21:06 +00003578 switch (control->type) {
3579 case WM8994:
Mark Brown9b7c5252011-02-17 20:05:44 -08003580 if (wm8994->micdet_irq) {
3581 ret = request_threaded_irq(wm8994->micdet_irq, NULL,
3582 wm8994_mic_irq,
3583 IRQF_TRIGGER_RISING,
3584 "Mic1 detect",
3585 wm8994);
3586 if (ret != 0)
3587 dev_warn(codec->dev,
3588 "Failed to request Mic1 detect IRQ: %d\n",
3589 ret);
3590 }
Mark Brown88766982010-03-29 20:57:12 +01003591
Mark Brown2a8a8562011-07-24 12:20:41 +01003592 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brown3a423152010-11-26 15:21:06 +00003593 WM8994_IRQ_MIC1_SHRT,
3594 wm8994_mic_irq, "Mic 1 short",
3595 wm8994);
3596 if (ret != 0)
3597 dev_warn(codec->dev,
3598 "Failed to request Mic1 short IRQ: %d\n",
3599 ret);
Mark Brown88766982010-03-29 20:57:12 +01003600
Mark Brown2a8a8562011-07-24 12:20:41 +01003601 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brown3a423152010-11-26 15:21:06 +00003602 WM8994_IRQ_MIC2_DET,
3603 wm8994_mic_irq, "Mic 2 detect",
3604 wm8994);
3605 if (ret != 0)
3606 dev_warn(codec->dev,
3607 "Failed to request Mic2 detect IRQ: %d\n",
3608 ret);
Mark Brown88766982010-03-29 20:57:12 +01003609
Mark Brown2a8a8562011-07-24 12:20:41 +01003610 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brown3a423152010-11-26 15:21:06 +00003611 WM8994_IRQ_MIC2_SHRT,
3612 wm8994_mic_irq, "Mic 2 short",
3613 wm8994);
3614 if (ret != 0)
3615 dev_warn(codec->dev,
3616 "Failed to request Mic2 short IRQ: %d\n",
3617 ret);
3618 break;
Mark Brown821edd22010-11-26 15:21:09 +00003619
3620 case WM8958:
Mark Brown81204c82011-05-24 17:35:53 +08003621 case WM1811:
Mark Brown9b7c5252011-02-17 20:05:44 -08003622 if (wm8994->micdet_irq) {
3623 ret = request_threaded_irq(wm8994->micdet_irq, NULL,
3624 wm8958_mic_irq,
3625 IRQF_TRIGGER_RISING,
3626 "Mic detect",
3627 wm8994);
3628 if (ret != 0)
3629 dev_warn(codec->dev,
3630 "Failed to request Mic detect IRQ: %d\n",
3631 ret);
3632 }
Mark Brown3a423152010-11-26 15:21:06 +00003633 }
Mark Brown88766982010-03-29 20:57:12 +01003634
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003635 switch (control->type) {
3636 case WM1811:
3637 if (wm8994->revision > 1) {
3638 ret = wm8994_request_irq(wm8994->wm8994,
3639 WM8994_IRQ_GPIO(6),
3640 wm1811_jackdet_irq, "JACKDET",
3641 wm8994);
3642 if (ret == 0)
3643 wm8994->jackdet = true;
3644 }
3645 break;
3646 default:
3647 break;
3648 }
3649
Mark Brownc7ebf932011-07-12 19:47:59 +09003650 wm8994->fll_locked_irq = true;
3651 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++) {
Mark Brown2a8a8562011-07-24 12:20:41 +01003652 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brownc7ebf932011-07-12 19:47:59 +09003653 WM8994_IRQ_FLL1_LOCK + i,
3654 wm8994_fll_locked_irq, "FLL lock",
3655 &wm8994->fll_locked[i]);
3656 if (ret != 0)
3657 wm8994->fll_locked_irq = false;
3658 }
3659
Mark Brown9e6e96a2010-01-29 17:47:12 +00003660 /* Remember if AIFnLRCLK is configured as a GPIO. This should be
3661 * configured on init - if a system wants to do this dynamically
3662 * at runtime we can deal with that then.
3663 */
3664 ret = wm8994_reg_read(codec->control_data, WM8994_GPIO_1);
3665 if (ret < 0) {
3666 dev_err(codec->dev, "Failed to read GPIO1 state: %d\n", ret);
Mark Brown88766982010-03-29 20:57:12 +01003667 goto err_irq;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003668 }
3669 if ((ret & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
3670 wm8994->lrclk_shared[0] = 1;
3671 wm8994_dai[0].symmetric_rates = 1;
3672 } else {
3673 wm8994->lrclk_shared[0] = 0;
3674 }
3675
3676 ret = wm8994_reg_read(codec->control_data, WM8994_GPIO_6);
3677 if (ret < 0) {
3678 dev_err(codec->dev, "Failed to read GPIO6 state: %d\n", ret);
Mark Brown88766982010-03-29 20:57:12 +01003679 goto err_irq;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003680 }
3681 if ((ret & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
3682 wm8994->lrclk_shared[1] = 1;
3683 wm8994_dai[1].symmetric_rates = 1;
3684 } else {
3685 wm8994->lrclk_shared[1] = 0;
3686 }
3687
Mark Brown9e6e96a2010-01-29 17:47:12 +00003688 wm8994_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
3689
Mark Brown9e6e96a2010-01-29 17:47:12 +00003690 /* Latch volume updates (right only; we always do left then right). */
Mark Brownbaa81602011-04-06 10:52:42 +09003691 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_LEFT_VOLUME,
3692 WM8994_AIF1DAC1_VU, WM8994_AIF1DAC1_VU);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003693 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_RIGHT_VOLUME,
3694 WM8994_AIF1DAC1_VU, WM8994_AIF1DAC1_VU);
Mark Brownbaa81602011-04-06 10:52:42 +09003695 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_LEFT_VOLUME,
3696 WM8994_AIF1DAC2_VU, WM8994_AIF1DAC2_VU);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003697 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_RIGHT_VOLUME,
3698 WM8994_AIF1DAC2_VU, WM8994_AIF1DAC2_VU);
Mark Brownbaa81602011-04-06 10:52:42 +09003699 snd_soc_update_bits(codec, WM8994_AIF2_DAC_LEFT_VOLUME,
3700 WM8994_AIF2DAC_VU, WM8994_AIF2DAC_VU);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003701 snd_soc_update_bits(codec, WM8994_AIF2_DAC_RIGHT_VOLUME,
3702 WM8994_AIF2DAC_VU, WM8994_AIF2DAC_VU);
Mark Brownbaa81602011-04-06 10:52:42 +09003703 snd_soc_update_bits(codec, WM8994_AIF1_ADC1_LEFT_VOLUME,
3704 WM8994_AIF1ADC1_VU, WM8994_AIF1ADC1_VU);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003705 snd_soc_update_bits(codec, WM8994_AIF1_ADC1_RIGHT_VOLUME,
3706 WM8994_AIF1ADC1_VU, WM8994_AIF1ADC1_VU);
Mark Brownbaa81602011-04-06 10:52:42 +09003707 snd_soc_update_bits(codec, WM8994_AIF1_ADC2_LEFT_VOLUME,
3708 WM8994_AIF1ADC2_VU, WM8994_AIF1ADC2_VU);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003709 snd_soc_update_bits(codec, WM8994_AIF1_ADC2_RIGHT_VOLUME,
3710 WM8994_AIF1ADC2_VU, WM8994_AIF1ADC2_VU);
Mark Brownbaa81602011-04-06 10:52:42 +09003711 snd_soc_update_bits(codec, WM8994_AIF2_ADC_LEFT_VOLUME,
3712 WM8994_AIF2ADC_VU, WM8994_AIF1ADC2_VU);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003713 snd_soc_update_bits(codec, WM8994_AIF2_ADC_RIGHT_VOLUME,
3714 WM8994_AIF2ADC_VU, WM8994_AIF1ADC2_VU);
Mark Brownbaa81602011-04-06 10:52:42 +09003715 snd_soc_update_bits(codec, WM8994_DAC1_LEFT_VOLUME,
3716 WM8994_DAC1_VU, WM8994_DAC1_VU);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003717 snd_soc_update_bits(codec, WM8994_DAC1_RIGHT_VOLUME,
3718 WM8994_DAC1_VU, WM8994_DAC1_VU);
Mark Brownbaa81602011-04-06 10:52:42 +09003719 snd_soc_update_bits(codec, WM8994_DAC2_LEFT_VOLUME,
3720 WM8994_DAC2_VU, WM8994_DAC2_VU);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003721 snd_soc_update_bits(codec, WM8994_DAC2_RIGHT_VOLUME,
3722 WM8994_DAC2_VU, WM8994_DAC2_VU);
3723
3724 /* Set the low bit of the 3D stereo depth so TLV matches */
3725 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_FILTERS_2,
3726 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT,
3727 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT);
3728 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_FILTERS_2,
3729 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT,
3730 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT);
3731 snd_soc_update_bits(codec, WM8994_AIF2_DAC_FILTERS_2,
3732 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT,
3733 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT);
3734
Mark Brown5b739672011-07-06 00:08:43 -07003735 /* Unconditionally enable AIF1 ADC TDM mode on chips which can
3736 * use this; it only affects behaviour on idle TDM clock
3737 * cycles. */
3738 switch (control->type) {
3739 case WM8994:
3740 case WM8958:
3741 snd_soc_update_bits(codec, WM8994_AIF1_CONTROL_1,
3742 WM8994_AIF1ADC_TDM, WM8994_AIF1ADC_TDM);
3743 break;
3744 default:
3745 break;
3746 }
Mark Brownd1ce6b22010-07-20 10:13:14 +01003747
Mark Brown500fa302011-11-29 19:58:19 +00003748 /* Put MICBIAS into bypass mode by default on newer devices */
3749 switch (control->type) {
3750 case WM8958:
3751 case WM1811:
3752 snd_soc_update_bits(codec, WM8958_MICBIAS1,
3753 WM8958_MICB1_MODE, WM8958_MICB1_MODE);
3754 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3755 WM8958_MICB2_MODE, WM8958_MICB2_MODE);
3756 break;
3757 default:
3758 break;
3759 }
3760
Mark Brown9e6e96a2010-01-29 17:47:12 +00003761 wm8994_update_class_w(codec);
3762
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003763 wm8994_handle_pdata(wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003764
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003765 wm_hubs_add_analogue_controls(codec);
3766 snd_soc_add_controls(codec, wm8994_snd_controls,
3767 ARRAY_SIZE(wm8994_snd_controls));
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003768 snd_soc_dapm_new_controls(dapm, wm8994_dapm_widgets,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003769 ARRAY_SIZE(wm8994_dapm_widgets));
Mark Brownc4431df2010-11-26 15:21:07 +00003770
3771 switch (control->type) {
3772 case WM8994:
3773 snd_soc_dapm_new_controls(dapm, wm8994_specific_dapm_widgets,
3774 ARRAY_SIZE(wm8994_specific_dapm_widgets));
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00003775 if (wm8994->revision < 4) {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00003776 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
3777 ARRAY_SIZE(wm8994_lateclk_revd_widgets));
Dimitris Papastamos04d28682011-03-01 11:47:10 +00003778 snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
3779 ARRAY_SIZE(wm8994_adc_revd_widgets));
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00003780 snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
3781 ARRAY_SIZE(wm8994_dac_revd_widgets));
3782 } else {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00003783 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
3784 ARRAY_SIZE(wm8994_lateclk_widgets));
Dimitris Papastamos04d28682011-03-01 11:47:10 +00003785 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
3786 ARRAY_SIZE(wm8994_adc_widgets));
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00003787 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
3788 ARRAY_SIZE(wm8994_dac_widgets));
3789 }
Mark Brownc4431df2010-11-26 15:21:07 +00003790 break;
3791 case WM8958:
3792 snd_soc_add_controls(codec, wm8958_snd_controls,
3793 ARRAY_SIZE(wm8958_snd_controls));
3794 snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
3795 ARRAY_SIZE(wm8958_dapm_widgets));
Mark Brown780e2802011-03-11 18:00:19 +00003796 if (wm8994->revision < 1) {
3797 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
3798 ARRAY_SIZE(wm8994_lateclk_revd_widgets));
3799 snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
3800 ARRAY_SIZE(wm8994_adc_revd_widgets));
3801 snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
3802 ARRAY_SIZE(wm8994_dac_revd_widgets));
3803 } else {
3804 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
3805 ARRAY_SIZE(wm8994_lateclk_widgets));
3806 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
3807 ARRAY_SIZE(wm8994_adc_widgets));
3808 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
3809 ARRAY_SIZE(wm8994_dac_widgets));
3810 }
Mark Brownc4431df2010-11-26 15:21:07 +00003811 break;
Mark Brown81204c82011-05-24 17:35:53 +08003812
3813 case WM1811:
3814 snd_soc_add_controls(codec, wm8958_snd_controls,
3815 ARRAY_SIZE(wm8958_snd_controls));
3816 snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
3817 ARRAY_SIZE(wm8958_dapm_widgets));
3818 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
3819 ARRAY_SIZE(wm8994_lateclk_widgets));
3820 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
3821 ARRAY_SIZE(wm8994_adc_widgets));
3822 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
3823 ARRAY_SIZE(wm8994_dac_widgets));
3824 break;
Mark Brownc4431df2010-11-26 15:21:07 +00003825 }
3826
3827
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003828 wm_hubs_add_analogue_routes(codec, 0, 0);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003829 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
Mark Brown9e6e96a2010-01-29 17:47:12 +00003830
Mark Brownc4431df2010-11-26 15:21:07 +00003831 switch (control->type) {
3832 case WM8994:
3833 snd_soc_dapm_add_routes(dapm, wm8994_intercon,
3834 ARRAY_SIZE(wm8994_intercon));
Mark Brown6ed8f142011-02-03 16:27:35 +00003835
Dimitris Papastamos173efa02011-02-11 16:32:11 +00003836 if (wm8994->revision < 4) {
Mark Brown6ed8f142011-02-03 16:27:35 +00003837 snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
3838 ARRAY_SIZE(wm8994_revd_intercon));
Dimitris Papastamos173efa02011-02-11 16:32:11 +00003839 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
3840 ARRAY_SIZE(wm8994_lateclk_revd_intercon));
3841 } else {
3842 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
3843 ARRAY_SIZE(wm8994_lateclk_intercon));
3844 }
Mark Brownc4431df2010-11-26 15:21:07 +00003845 break;
3846 case WM8958:
Mark Brown780e2802011-03-11 18:00:19 +00003847 if (wm8994->revision < 1) {
3848 snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
3849 ARRAY_SIZE(wm8994_revd_intercon));
3850 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
3851 ARRAY_SIZE(wm8994_lateclk_revd_intercon));
3852 } else {
3853 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
3854 ARRAY_SIZE(wm8994_lateclk_intercon));
3855 snd_soc_dapm_add_routes(dapm, wm8958_intercon,
3856 ARRAY_SIZE(wm8958_intercon));
3857 }
Mark Brownf701a2e2011-03-09 19:31:01 +00003858
3859 wm8958_dsp2_init(codec);
Mark Brownc4431df2010-11-26 15:21:07 +00003860 break;
Mark Brown81204c82011-05-24 17:35:53 +08003861 case WM1811:
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 break;
Mark Brownc4431df2010-11-26 15:21:07 +00003867 }
3868
Mark Brown9e6e96a2010-01-29 17:47:12 +00003869 return 0;
3870
Mark Brown88766982010-03-29 20:57:12 +01003871err_irq:
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003872 if (wm8994->jackdet)
3873 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01003874 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_SHRT, wm8994);
3875 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET, wm8994);
3876 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT, wm8994);
Mark Brown9b7c5252011-02-17 20:05:44 -08003877 if (wm8994->micdet_irq)
3878 free_irq(wm8994->micdet_irq, wm8994);
Mark Brownc7ebf932011-07-12 19:47:59 +09003879 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
Mark Brown2a8a8562011-07-24 12:20:41 +01003880 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
Mark Brownc7ebf932011-07-12 19:47:59 +09003881 &wm8994->fll_locked[i]);
Mark Brown2a8a8562011-07-24 12:20:41 +01003882 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
Mark Brownb30ead52011-07-12 15:47:17 +09003883 &wm8994->hubs);
Mark Brown2a8a8562011-07-24 12:20:41 +01003884 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, codec);
3885 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, codec);
3886 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003887err:
3888 kfree(wm8994);
3889 return ret;
3890}
3891
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003892static int wm8994_codec_remove(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003893{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003894 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01003895 struct wm8994 *control = wm8994->wm8994;
Mark Brownc7ebf932011-07-12 19:47:59 +09003896 int i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003897
3898 wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003899
Mark Brown39fb51a2010-11-26 17:23:43 +00003900 pm_runtime_disable(codec->dev);
3901
Mark Brownc7ebf932011-07-12 19:47:59 +09003902 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
Mark Brown2a8a8562011-07-24 12:20:41 +01003903 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
Mark Brownc7ebf932011-07-12 19:47:59 +09003904 &wm8994->fll_locked[i]);
3905
Mark Brown2a8a8562011-07-24 12:20:41 +01003906 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
Mark Brownb30ead52011-07-12 15:47:17 +09003907 &wm8994->hubs);
Mark Brown2a8a8562011-07-24 12:20:41 +01003908 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, codec);
3909 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, codec);
3910 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, codec);
Mark Brownb30ead52011-07-12 15:47:17 +09003911
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003912 if (wm8994->jackdet)
3913 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);
3914
Mark Brown3a423152010-11-26 15:21:06 +00003915 switch (control->type) {
3916 case WM8994:
Mark Brown9b7c5252011-02-17 20:05:44 -08003917 if (wm8994->micdet_irq)
3918 free_irq(wm8994->micdet_irq, wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01003919 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET,
Mark Brown3a423152010-11-26 15:21:06 +00003920 wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01003921 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT,
Mark Brown3a423152010-11-26 15:21:06 +00003922 wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01003923 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_DET,
Mark Brown3a423152010-11-26 15:21:06 +00003924 wm8994);
3925 break;
Mark Brown821edd22010-11-26 15:21:09 +00003926
Mark Brown81204c82011-05-24 17:35:53 +08003927 case WM1811:
Mark Brown821edd22010-11-26 15:21:09 +00003928 case WM8958:
Mark Brown9b7c5252011-02-17 20:05:44 -08003929 if (wm8994->micdet_irq)
3930 free_irq(wm8994->micdet_irq, wm8994);
Mark Brown821edd22010-11-26 15:21:09 +00003931 break;
Mark Brown3a423152010-11-26 15:21:06 +00003932 }
Mark Brownfbbf5922011-03-11 18:09:04 +00003933 if (wm8994->mbc)
3934 release_firmware(wm8994->mbc);
Mark Brown09e10d72011-03-16 22:57:47 +00003935 if (wm8994->mbc_vss)
3936 release_firmware(wm8994->mbc_vss);
Mark Brown31215872011-03-17 20:23:43 +00003937 if (wm8994->enh_eq)
3938 release_firmware(wm8994->enh_eq);
Axel Lin24fb2b12010-11-23 15:58:39 +08003939 kfree(wm8994->retune_mobile_texts);
3940 kfree(wm8994->drc_texts);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003941 kfree(wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003942
3943 return 0;
3944}
3945
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003946static struct snd_soc_codec_driver soc_codec_dev_wm8994 = {
3947 .probe = wm8994_codec_probe,
3948 .remove = wm8994_codec_remove,
3949 .suspend = wm8994_suspend,
3950 .resume = wm8994_resume,
Mark Brownca9aef52010-11-26 17:23:41 +00003951 .read = wm8994_read,
3952 .write = wm8994_write,
Mark Browneba19fd2010-11-19 16:09:15 +00003953 .readable_register = wm8994_readable,
3954 .volatile_register = wm8994_volatile,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003955 .set_bias_level = wm8994_set_bias_level,
Mark Brownca9aef52010-11-26 17:23:41 +00003956
3957 .reg_cache_size = WM8994_CACHE_SIZE,
3958 .reg_cache_default = wm8994_reg_defaults,
3959 .reg_word_size = 2,
Mark Brown2e19b0c2010-11-26 17:23:42 +00003960 .compress_type = SND_SOC_RBTREE_COMPRESSION,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003961};
3962
3963static int __devinit wm8994_probe(struct platform_device *pdev)
3964{
3965 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wm8994,
3966 wm8994_dai, ARRAY_SIZE(wm8994_dai));
3967}
3968
3969static int __devexit wm8994_remove(struct platform_device *pdev)
3970{
3971 snd_soc_unregister_codec(&pdev->dev);
3972 return 0;
3973}
3974
Mark Brown9e6e96a2010-01-29 17:47:12 +00003975static struct platform_driver wm8994_codec_driver = {
3976 .driver = {
3977 .name = "wm8994-codec",
3978 .owner = THIS_MODULE,
3979 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003980 .probe = wm8994_probe,
3981 .remove = __devexit_p(wm8994_remove),
Mark Brown9e6e96a2010-01-29 17:47:12 +00003982};
3983
Mark Brown5bbcc3c2011-11-23 22:52:08 +00003984module_platform_driver(wm8994_codec_driver);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003985
3986MODULE_DESCRIPTION("ASoC WM8994 driver");
3987MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
3988MODULE_LICENSE("GPL");
3989MODULE_ALIAS("platform:wm8994-codec");