blob: f91b49e1ece309dcd6175f967eb016a26451de3f [file] [log] [blame]
Mark Brown9e6e96a2010-01-29 17:47:12 +00001/*
2 * wm8994.c -- WM8994 ALSA SoC Audio driver
3 *
Mark Brown656baae2012-05-23 12:39:07 +01004 * Copyright 2009-12 Wolfson Microelectronics plc
Mark Brown9e6e96a2010-01-29 17:47:12 +00005 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/init.h>
17#include <linux/delay.h>
18#include <linux/pm.h>
Mark Brownd1a0a292013-05-10 21:40:10 +010019#include <linux/gcd.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000020#include <linux/i2c.h>
21#include <linux/platform_device.h>
Mark Brown39fb51a2010-11-26 17:23:43 +000022#include <linux/pm_runtime.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000023#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000025#include <sound/core.h>
Mark Brown821edd22010-11-26 15:21:09 +000026#include <sound/jack.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000027#include <sound/pcm.h>
28#include <sound/pcm_params.h>
29#include <sound/soc.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000030#include <sound/initval.h>
31#include <sound/tlv.h>
Mark Brown2bbb5d62010-12-05 12:50:12 +000032#include <trace/events/asoc.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000033
34#include <linux/mfd/wm8994/core.h>
35#include <linux/mfd/wm8994/registers.h>
36#include <linux/mfd/wm8994/pdata.h>
37#include <linux/mfd/wm8994/gpio.h>
38
39#include "wm8994.h"
40#include "wm_hubs.h"
41
Mark Brownaf6b6fe2011-11-30 20:32:05 +000042#define WM1811_JACKDET_MODE_NONE 0x0000
43#define WM1811_JACKDET_MODE_JACK 0x0100
44#define WM1811_JACKDET_MODE_MIC 0x0080
45#define WM1811_JACKDET_MODE_AUDIO 0x0180
46
Mark Brown9e6e96a2010-01-29 17:47:12 +000047#define WM8994_NUM_DRC 3
48#define WM8994_NUM_EQ 3
49
Mark Brownbfd37bb2012-06-05 12:31:32 +010050static struct {
51 unsigned int reg;
52 unsigned int mask;
53} wm8994_vu_bits[] = {
54 { WM8994_LEFT_LINE_INPUT_1_2_VOLUME, WM8994_IN1_VU },
55 { WM8994_RIGHT_LINE_INPUT_1_2_VOLUME, WM8994_IN1_VU },
56 { WM8994_LEFT_LINE_INPUT_3_4_VOLUME, WM8994_IN2_VU },
57 { WM8994_RIGHT_LINE_INPUT_3_4_VOLUME, WM8994_IN2_VU },
58 { WM8994_SPEAKER_VOLUME_LEFT, WM8994_SPKOUT_VU },
59 { WM8994_SPEAKER_VOLUME_RIGHT, WM8994_SPKOUT_VU },
60 { WM8994_LEFT_OUTPUT_VOLUME, WM8994_HPOUT1_VU },
61 { WM8994_RIGHT_OUTPUT_VOLUME, WM8994_HPOUT1_VU },
62 { WM8994_LEFT_OPGA_VOLUME, WM8994_MIXOUT_VU },
63 { WM8994_RIGHT_OPGA_VOLUME, WM8994_MIXOUT_VU },
64
65 { WM8994_AIF1_DAC1_LEFT_VOLUME, WM8994_AIF1DAC1_VU },
66 { WM8994_AIF1_DAC1_RIGHT_VOLUME, WM8994_AIF1DAC1_VU },
67 { WM8994_AIF1_DAC2_LEFT_VOLUME, WM8994_AIF1DAC2_VU },
68 { WM8994_AIF1_DAC2_RIGHT_VOLUME, WM8994_AIF1DAC2_VU },
69 { WM8994_AIF2_DAC_LEFT_VOLUME, WM8994_AIF2DAC_VU },
70 { WM8994_AIF2_DAC_RIGHT_VOLUME, WM8994_AIF2DAC_VU },
71 { WM8994_AIF1_ADC1_LEFT_VOLUME, WM8994_AIF1ADC1_VU },
72 { WM8994_AIF1_ADC1_RIGHT_VOLUME, WM8994_AIF1ADC1_VU },
73 { WM8994_AIF1_ADC2_LEFT_VOLUME, WM8994_AIF1ADC2_VU },
74 { WM8994_AIF1_ADC2_RIGHT_VOLUME, WM8994_AIF1ADC2_VU },
75 { WM8994_AIF2_ADC_LEFT_VOLUME, WM8994_AIF2ADC_VU },
76 { WM8994_AIF2_ADC_RIGHT_VOLUME, WM8994_AIF1ADC2_VU },
77 { WM8994_DAC1_LEFT_VOLUME, WM8994_DAC1_VU },
78 { WM8994_DAC1_RIGHT_VOLUME, WM8994_DAC1_VU },
79 { WM8994_DAC2_LEFT_VOLUME, WM8994_DAC2_VU },
80 { WM8994_DAC2_RIGHT_VOLUME, WM8994_DAC2_VU },
81};
82
Mark Brown9e6e96a2010-01-29 17:47:12 +000083static int wm8994_drc_base[] = {
84 WM8994_AIF1_DRC1_1,
85 WM8994_AIF1_DRC2_1,
86 WM8994_AIF2_DRC_1,
87};
88
89static int wm8994_retune_mobile_base[] = {
90 WM8994_AIF1_DAC1_EQ_GAINS_1,
91 WM8994_AIF1_DAC2_EQ_GAINS_1,
92 WM8994_AIF2_EQ_GAINS_1,
93};
94
Mark Brownaf6b6fe2011-11-30 20:32:05 +000095static const struct wm8958_micd_rate micdet_rates[] = {
Mark Brownb00adf72011-08-13 11:57:18 +090096 { 32768, true, 1, 4 },
97 { 32768, false, 1, 1 },
Mark Brown604533d2011-12-01 12:51:25 +000098 { 44100 * 256, true, 7, 10 },
99 { 44100 * 256, false, 7, 10 },
Mark Brownb00adf72011-08-13 11:57:18 +0900100};
101
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000102static const struct wm8958_micd_rate jackdet_rates[] = {
103 { 32768, true, 0, 1 },
104 { 32768, false, 0, 1 },
Mark Browne9d9a962012-04-26 16:07:32 +0100105 { 44100 * 256, true, 10, 10 },
106 { 44100 * 256, false, 7, 8 },
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000107};
108
Mark Brownb00adf72011-08-13 11:57:18 +0900109static void wm8958_micd_set_rate(struct snd_soc_codec *codec)
110{
111 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900112 struct wm8994 *control = wm8994->wm8994;
Mark Brownb00adf72011-08-13 11:57:18 +0900113 int best, i, sysclk, val;
114 bool idle;
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000115 const struct wm8958_micd_rate *rates;
116 int num_rates;
Mark Brownb00adf72011-08-13 11:57:18 +0900117
Mark Brownb00adf72011-08-13 11:57:18 +0900118 idle = !wm8994->jack_mic;
119
120 sysclk = snd_soc_read(codec, WM8994_CLOCKING_1);
121 if (sysclk & WM8994_SYSCLK_SRC)
122 sysclk = wm8994->aifclk[1];
123 else
124 sysclk = wm8994->aifclk[0];
125
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900126 if (control->pdata.micd_rates) {
127 rates = control->pdata.micd_rates;
128 num_rates = control->pdata.num_micd_rates;
Mark Browncd1707a2011-12-01 13:44:25 +0000129 } else if (wm8994->jackdet) {
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000130 rates = jackdet_rates;
131 num_rates = ARRAY_SIZE(jackdet_rates);
132 } else {
133 rates = micdet_rates;
134 num_rates = ARRAY_SIZE(micdet_rates);
135 }
136
Mark Brownb00adf72011-08-13 11:57:18 +0900137 best = 0;
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000138 for (i = 0; i < num_rates; i++) {
139 if (rates[i].idle != idle)
Mark Brownb00adf72011-08-13 11:57:18 +0900140 continue;
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000141 if (abs(rates[i].sysclk - sysclk) <
142 abs(rates[best].sysclk - sysclk))
Mark Brownb00adf72011-08-13 11:57:18 +0900143 best = i;
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000144 else if (rates[best].idle != idle)
Mark Brownb00adf72011-08-13 11:57:18 +0900145 best = i;
146 }
147
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000148 val = rates[best].start << WM8958_MICD_BIAS_STARTTIME_SHIFT
149 | rates[best].rate << WM8958_MICD_RATE_SHIFT;
Mark Brownb00adf72011-08-13 11:57:18 +0900150
Mark Brown3a334ad2012-04-26 17:02:16 +0100151 dev_dbg(codec->dev, "MICD rate %d,%d for %dHz %s\n",
152 rates[best].start, rates[best].rate, sysclk,
153 idle ? "idle" : "active");
154
Mark Brownb00adf72011-08-13 11:57:18 +0900155 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
156 WM8958_MICD_BIAS_STARTTIME_MASK |
157 WM8958_MICD_RATE_MASK, val);
158}
159
Mark Brown9e6e96a2010-01-29 17:47:12 +0000160static int configure_aif_clock(struct snd_soc_codec *codec, int aif)
161{
Mark Brownb2c812e2010-04-14 15:35:19 +0900162 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000163 int rate;
164 int reg1 = 0;
165 int offset;
166
167 if (aif)
168 offset = 4;
169 else
170 offset = 0;
171
172 switch (wm8994->sysclk[aif]) {
173 case WM8994_SYSCLK_MCLK1:
174 rate = wm8994->mclk[0];
175 break;
176
177 case WM8994_SYSCLK_MCLK2:
178 reg1 |= 0x8;
179 rate = wm8994->mclk[1];
180 break;
181
182 case WM8994_SYSCLK_FLL1:
183 reg1 |= 0x10;
184 rate = wm8994->fll[0].out;
185 break;
186
187 case WM8994_SYSCLK_FLL2:
188 reg1 |= 0x18;
189 rate = wm8994->fll[1].out;
190 break;
191
192 default:
193 return -EINVAL;
194 }
195
196 if (rate >= 13500000) {
197 rate /= 2;
198 reg1 |= WM8994_AIF1CLK_DIV;
199
200 dev_dbg(codec->dev, "Dividing AIF%d clock to %dHz\n",
201 aif + 1, rate);
202 }
Mark Brown5e5e2be2010-04-25 12:20:30 +0100203
Mark Brown9e6e96a2010-01-29 17:47:12 +0000204 wm8994->aifclk[aif] = rate;
205
206 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1 + offset,
207 WM8994_AIF1CLK_SRC_MASK | WM8994_AIF1CLK_DIV,
208 reg1);
209
210 return 0;
211}
212
213static int configure_clock(struct snd_soc_codec *codec)
214{
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +0200215 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
Mark Brownb2c812e2010-04-14 15:35:19 +0900216 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Axel Lin04f45c42011-10-04 20:07:03 +0800217 int change, new;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000218
219 /* Bring up the AIF clocks first */
220 configure_aif_clock(codec, 0);
221 configure_aif_clock(codec, 1);
222
223 /* Then switch CLK_SYS over to the higher of them; a change
224 * can only happen as a result of a clocking change which can
225 * only be made outside of DAPM so we can safely redo the
226 * clocking.
227 */
228
229 /* If they're equal it doesn't matter which is used */
Mark Brownb00adf72011-08-13 11:57:18 +0900230 if (wm8994->aifclk[0] == wm8994->aifclk[1]) {
231 wm8958_micd_set_rate(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000232 return 0;
Mark Brownb00adf72011-08-13 11:57:18 +0900233 }
Mark Brown9e6e96a2010-01-29 17:47:12 +0000234
235 if (wm8994->aifclk[0] < wm8994->aifclk[1])
236 new = WM8994_SYSCLK_SRC;
237 else
238 new = 0;
239
Axel Lin04f45c42011-10-04 20:07:03 +0800240 change = snd_soc_update_bits(codec, WM8994_CLOCKING_1,
241 WM8994_SYSCLK_SRC, new);
Mark Brown52ac7ab2011-12-01 12:43:26 +0000242 if (change)
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +0200243 snd_soc_dapm_sync(dapm);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000244
Mark Brownb00adf72011-08-13 11:57:18 +0900245 wm8958_micd_set_rate(codec);
246
Mark Brown9e6e96a2010-01-29 17:47:12 +0000247 return 0;
248}
249
250static int check_clk_sys(struct snd_soc_dapm_widget *source,
251 struct snd_soc_dapm_widget *sink)
252{
Lars-Peter Clausen12e30802015-01-13 10:27:27 +0100253 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
254 int reg = snd_soc_read(codec, WM8994_CLOCKING_1);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000255 const char *clk;
256
257 /* Check what we're currently using for CLK_SYS */
258 if (reg & WM8994_SYSCLK_SRC)
259 clk = "AIF2CLK";
260 else
261 clk = "AIF1CLK";
262
263 return strcmp(source->name, clk) == 0;
264}
265
266static const char *sidetone_hpf_text[] = {
267 "2.7kHz", "1.35kHz", "675Hz", "370Hz", "180Hz", "90Hz", "45Hz"
268};
269
Takashi Iwaie61a35b2014-02-18 09:45:30 +0100270static SOC_ENUM_SINGLE_DECL(sidetone_hpf,
271 WM8994_SIDETONE, 7, sidetone_hpf_text);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000272
Uk Kim146fd572010-12-07 13:58:40 +0000273static const char *adc_hpf_text[] = {
274 "HiFi", "Voice 1", "Voice 2", "Voice 3"
275};
276
Takashi Iwaie61a35b2014-02-18 09:45:30 +0100277static SOC_ENUM_SINGLE_DECL(aif1adc1_hpf,
278 WM8994_AIF1_ADC1_FILTERS, 13, adc_hpf_text);
Uk Kim146fd572010-12-07 13:58:40 +0000279
Takashi Iwaie61a35b2014-02-18 09:45:30 +0100280static SOC_ENUM_SINGLE_DECL(aif1adc2_hpf,
281 WM8994_AIF1_ADC2_FILTERS, 13, adc_hpf_text);
Uk Kim146fd572010-12-07 13:58:40 +0000282
Takashi Iwaie61a35b2014-02-18 09:45:30 +0100283static SOC_ENUM_SINGLE_DECL(aif2adc_hpf,
284 WM8994_AIF2_ADC_FILTERS, 13, adc_hpf_text);
Uk Kim146fd572010-12-07 13:58:40 +0000285
Mark Brown9e6e96a2010-01-29 17:47:12 +0000286static const DECLARE_TLV_DB_SCALE(aif_tlv, 0, 600, 0);
287static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
288static const DECLARE_TLV_DB_SCALE(st_tlv, -3600, 300, 0);
289static const DECLARE_TLV_DB_SCALE(wm8994_3d_tlv, -1600, 183, 0);
290static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
Mark Brown1ddc07d2011-08-16 10:08:48 +0900291static const DECLARE_TLV_DB_SCALE(ng_tlv, -10200, 600, 0);
Mark Brown81204c82011-05-24 17:35:53 +0800292static const DECLARE_TLV_DB_SCALE(mixin_boost_tlv, 0, 900, 0);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000293
294#define WM8994_DRC_SWITCH(xname, reg, shift) \
Lars-Peter Clausen6e065092013-06-19 19:33:59 +0200295 SOC_SINGLE_EXT(xname, reg, shift, 1, 0, \
296 snd_soc_get_volsw, wm8994_put_drc_sw)
Mark Brown9e6e96a2010-01-29 17:47:12 +0000297
298static int wm8994_put_drc_sw(struct snd_kcontrol *kcontrol,
299 struct snd_ctl_elem_value *ucontrol)
300{
301 struct soc_mixer_control *mc =
302 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausenea53bf72014-03-18 09:02:04 +0100303 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000304 int mask, ret;
305
306 /* Can't enable both ADC and DAC paths simultaneously */
307 if (mc->shift == WM8994_AIF1DAC1_DRC_ENA_SHIFT)
308 mask = WM8994_AIF1ADC1L_DRC_ENA_MASK |
309 WM8994_AIF1ADC1R_DRC_ENA_MASK;
310 else
311 mask = WM8994_AIF1DAC1_DRC_ENA_MASK;
312
313 ret = snd_soc_read(codec, mc->reg);
314 if (ret < 0)
315 return ret;
316 if (ret & mask)
317 return -EINVAL;
318
319 return snd_soc_put_volsw(kcontrol, ucontrol);
320}
321
Mark Brown9e6e96a2010-01-29 17:47:12 +0000322static void wm8994_set_drc(struct snd_soc_codec *codec, int drc)
323{
Mark Brownb2c812e2010-04-14 15:35:19 +0900324 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900325 struct wm8994 *control = wm8994->wm8994;
326 struct wm8994_pdata *pdata = &control->pdata;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000327 int base = wm8994_drc_base[drc];
328 int cfg = wm8994->drc_cfg[drc];
329 int save, i;
330
331 /* Save any enables; the configuration should clear them. */
332 save = snd_soc_read(codec, base);
333 save &= WM8994_AIF1DAC1_DRC_ENA | WM8994_AIF1ADC1L_DRC_ENA |
334 WM8994_AIF1ADC1R_DRC_ENA;
335
336 for (i = 0; i < WM8994_DRC_REGS; i++)
337 snd_soc_update_bits(codec, base + i, 0xffff,
338 pdata->drc_cfgs[cfg].regs[i]);
339
340 snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_DRC_ENA |
341 WM8994_AIF1ADC1L_DRC_ENA |
342 WM8994_AIF1ADC1R_DRC_ENA, save);
343}
344
345/* Icky as hell but saves code duplication */
346static int wm8994_get_drc(const char *name)
347{
348 if (strcmp(name, "AIF1DRC1 Mode") == 0)
349 return 0;
350 if (strcmp(name, "AIF1DRC2 Mode") == 0)
351 return 1;
352 if (strcmp(name, "AIF2DRC Mode") == 0)
353 return 2;
354 return -EINVAL;
355}
356
357static int wm8994_put_drc_enum(struct snd_kcontrol *kcontrol,
358 struct snd_ctl_elem_value *ucontrol)
359{
Lars-Peter Clausenea53bf72014-03-18 09:02:04 +0100360 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000361 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900362 struct wm8994 *control = wm8994->wm8994;
363 struct wm8994_pdata *pdata = &control->pdata;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000364 int drc = wm8994_get_drc(kcontrol->id.name);
Takashi Iwai8019c0b2016-02-29 18:01:15 +0100365 int value = ucontrol->value.enumerated.item[0];
Mark Brown9e6e96a2010-01-29 17:47:12 +0000366
367 if (drc < 0)
368 return drc;
369
370 if (value >= pdata->num_drc_cfgs)
371 return -EINVAL;
372
373 wm8994->drc_cfg[drc] = value;
374
375 wm8994_set_drc(codec, drc);
376
377 return 0;
378}
379
380static int wm8994_get_drc_enum(struct snd_kcontrol *kcontrol,
381 struct snd_ctl_elem_value *ucontrol)
382{
Lars-Peter Clausenea53bf72014-03-18 09:02:04 +0100383 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +0900384 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000385 int drc = wm8994_get_drc(kcontrol->id.name);
386
Vinod Koul7d6898b2013-05-28 15:06:42 +0530387 if (drc < 0)
388 return drc;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000389 ucontrol->value.enumerated.item[0] = wm8994->drc_cfg[drc];
390
391 return 0;
392}
393
394static void wm8994_set_retune_mobile(struct snd_soc_codec *codec, int block)
395{
Mark Brownb2c812e2010-04-14 15:35:19 +0900396 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900397 struct wm8994 *control = wm8994->wm8994;
398 struct wm8994_pdata *pdata = &control->pdata;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000399 int base = wm8994_retune_mobile_base[block];
400 int iface, best, best_val, save, i, cfg;
401
402 if (!pdata || !wm8994->num_retune_mobile_texts)
403 return;
404
405 switch (block) {
406 case 0:
407 case 1:
408 iface = 0;
409 break;
410 case 2:
411 iface = 1;
412 break;
413 default:
414 return;
415 }
416
417 /* Find the version of the currently selected configuration
418 * with the nearest sample rate. */
419 cfg = wm8994->retune_mobile_cfg[block];
420 best = 0;
421 best_val = INT_MAX;
422 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
423 if (strcmp(pdata->retune_mobile_cfgs[i].name,
424 wm8994->retune_mobile_texts[cfg]) == 0 &&
425 abs(pdata->retune_mobile_cfgs[i].rate
426 - wm8994->dac_rates[iface]) < best_val) {
427 best = i;
428 best_val = abs(pdata->retune_mobile_cfgs[i].rate
429 - wm8994->dac_rates[iface]);
430 }
431 }
432
433 dev_dbg(codec->dev, "ReTune Mobile %d %s/%dHz for %dHz sample rate\n",
434 block,
435 pdata->retune_mobile_cfgs[best].name,
436 pdata->retune_mobile_cfgs[best].rate,
437 wm8994->dac_rates[iface]);
438
439 /* The EQ will be disabled while reconfiguring it, remember the
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +0200440 * current configuration.
Mark Brown9e6e96a2010-01-29 17:47:12 +0000441 */
442 save = snd_soc_read(codec, base);
443 save &= WM8994_AIF1DAC1_EQ_ENA;
444
445 for (i = 0; i < WM8994_EQ_REGS; i++)
446 snd_soc_update_bits(codec, base + i, 0xffff,
447 pdata->retune_mobile_cfgs[best].regs[i]);
448
449 snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_EQ_ENA, save);
450}
451
452/* Icky as hell but saves code duplication */
453static int wm8994_get_retune_mobile_block(const char *name)
454{
455 if (strcmp(name, "AIF1.1 EQ Mode") == 0)
456 return 0;
457 if (strcmp(name, "AIF1.2 EQ Mode") == 0)
458 return 1;
459 if (strcmp(name, "AIF2 EQ Mode") == 0)
460 return 2;
461 return -EINVAL;
462}
463
464static int wm8994_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
465 struct snd_ctl_elem_value *ucontrol)
466{
Lars-Peter Clausenea53bf72014-03-18 09:02:04 +0100467 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000468 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900469 struct wm8994 *control = wm8994->wm8994;
470 struct wm8994_pdata *pdata = &control->pdata;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000471 int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
Takashi Iwai8019c0b2016-02-29 18:01:15 +0100472 int value = ucontrol->value.enumerated.item[0];
Mark Brown9e6e96a2010-01-29 17:47:12 +0000473
474 if (block < 0)
475 return block;
476
477 if (value >= pdata->num_retune_mobile_cfgs)
478 return -EINVAL;
479
480 wm8994->retune_mobile_cfg[block] = value;
481
482 wm8994_set_retune_mobile(codec, block);
483
484 return 0;
485}
486
487static int wm8994_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
488 struct snd_ctl_elem_value *ucontrol)
489{
Lars-Peter Clausenea53bf72014-03-18 09:02:04 +0100490 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
Mark Brown4a8d9292011-02-16 14:57:17 -0800491 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000492 int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
493
Vinod Koul7d6898b2013-05-28 15:06:42 +0530494 if (block < 0)
495 return block;
496
Mark Brown9e6e96a2010-01-29 17:47:12 +0000497 ucontrol->value.enumerated.item[0] = wm8994->retune_mobile_cfg[block];
498
499 return 0;
500}
501
Mark Brown96b101e2010-11-18 15:49:38 +0000502static const char *aif_chan_src_text[] = {
Mark Brownf5548852010-08-31 19:39:48 +0100503 "Left", "Right"
504};
505
Takashi Iwaie61a35b2014-02-18 09:45:30 +0100506static SOC_ENUM_SINGLE_DECL(aif1adcl_src,
507 WM8994_AIF1_CONTROL_1, 15, aif_chan_src_text);
Mark Brown96b101e2010-11-18 15:49:38 +0000508
Takashi Iwaie61a35b2014-02-18 09:45:30 +0100509static SOC_ENUM_SINGLE_DECL(aif1adcr_src,
510 WM8994_AIF1_CONTROL_1, 14, aif_chan_src_text);
Mark Brown96b101e2010-11-18 15:49:38 +0000511
Takashi Iwaie61a35b2014-02-18 09:45:30 +0100512static SOC_ENUM_SINGLE_DECL(aif2adcl_src,
513 WM8994_AIF2_CONTROL_1, 15, aif_chan_src_text);
Mark Brown96b101e2010-11-18 15:49:38 +0000514
Takashi Iwaie61a35b2014-02-18 09:45:30 +0100515static SOC_ENUM_SINGLE_DECL(aif2adcr_src,
516 WM8994_AIF2_CONTROL_1, 14, aif_chan_src_text);
Mark Brown96b101e2010-11-18 15:49:38 +0000517
Takashi Iwaie61a35b2014-02-18 09:45:30 +0100518static SOC_ENUM_SINGLE_DECL(aif1dacl_src,
519 WM8994_AIF1_CONTROL_2, 15, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100520
Takashi Iwaie61a35b2014-02-18 09:45:30 +0100521static SOC_ENUM_SINGLE_DECL(aif1dacr_src,
522 WM8994_AIF1_CONTROL_2, 14, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100523
Takashi Iwaie61a35b2014-02-18 09:45:30 +0100524static SOC_ENUM_SINGLE_DECL(aif2dacl_src,
525 WM8994_AIF2_CONTROL_2, 15, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100526
Takashi Iwaie61a35b2014-02-18 09:45:30 +0100527static SOC_ENUM_SINGLE_DECL(aif2dacr_src,
528 WM8994_AIF2_CONTROL_2, 14, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100529
Mark Brown154b26a2010-12-09 12:07:44 +0000530static const char *osr_text[] = {
531 "Low Power", "High Performance",
532};
533
Takashi Iwaie61a35b2014-02-18 09:45:30 +0100534static SOC_ENUM_SINGLE_DECL(dac_osr,
535 WM8994_OVERSAMPLING, 0, osr_text);
Mark Brown154b26a2010-12-09 12:07:44 +0000536
Takashi Iwaie61a35b2014-02-18 09:45:30 +0100537static SOC_ENUM_SINGLE_DECL(adc_osr,
538 WM8994_OVERSAMPLING, 1, osr_text);
Mark Brown154b26a2010-12-09 12:07:44 +0000539
Mark Brown9e6e96a2010-01-29 17:47:12 +0000540static const struct snd_kcontrol_new wm8994_snd_controls[] = {
541SOC_DOUBLE_R_TLV("AIF1ADC1 Volume", WM8994_AIF1_ADC1_LEFT_VOLUME,
542 WM8994_AIF1_ADC1_RIGHT_VOLUME,
543 1, 119, 0, digital_tlv),
544SOC_DOUBLE_R_TLV("AIF1ADC2 Volume", WM8994_AIF1_ADC2_LEFT_VOLUME,
545 WM8994_AIF1_ADC2_RIGHT_VOLUME,
546 1, 119, 0, digital_tlv),
547SOC_DOUBLE_R_TLV("AIF2ADC Volume", WM8994_AIF2_ADC_LEFT_VOLUME,
548 WM8994_AIF2_ADC_RIGHT_VOLUME,
549 1, 119, 0, digital_tlv),
550
Mark Brown96b101e2010-11-18 15:49:38 +0000551SOC_ENUM("AIF1ADCL Source", aif1adcl_src),
552SOC_ENUM("AIF1ADCR Source", aif1adcr_src),
Mark Brown49db7e72010-12-08 13:49:43 +0000553SOC_ENUM("AIF2ADCL Source", aif2adcl_src),
554SOC_ENUM("AIF2ADCR Source", aif2adcr_src),
Mark Brown96b101e2010-11-18 15:49:38 +0000555
Mark Brownf5548852010-08-31 19:39:48 +0100556SOC_ENUM("AIF1DACL Source", aif1dacl_src),
557SOC_ENUM("AIF1DACR Source", aif1dacr_src),
Mark Brown49db7e72010-12-08 13:49:43 +0000558SOC_ENUM("AIF2DACL Source", aif2dacl_src),
559SOC_ENUM("AIF2DACR Source", aif2dacr_src),
Mark Brownf5548852010-08-31 19:39:48 +0100560
Mark Brown9e6e96a2010-01-29 17:47:12 +0000561SOC_DOUBLE_R_TLV("AIF1DAC1 Volume", WM8994_AIF1_DAC1_LEFT_VOLUME,
562 WM8994_AIF1_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
563SOC_DOUBLE_R_TLV("AIF1DAC2 Volume", WM8994_AIF1_DAC2_LEFT_VOLUME,
564 WM8994_AIF1_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
565SOC_DOUBLE_R_TLV("AIF2DAC Volume", WM8994_AIF2_DAC_LEFT_VOLUME,
566 WM8994_AIF2_DAC_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
567
568SOC_SINGLE_TLV("AIF1 Boost Volume", WM8994_AIF1_CONTROL_2, 10, 3, 0, aif_tlv),
569SOC_SINGLE_TLV("AIF2 Boost Volume", WM8994_AIF2_CONTROL_2, 10, 3, 0, aif_tlv),
570
571SOC_SINGLE("AIF1DAC1 EQ Switch", WM8994_AIF1_DAC1_EQ_GAINS_1, 0, 1, 0),
572SOC_SINGLE("AIF1DAC2 EQ Switch", WM8994_AIF1_DAC2_EQ_GAINS_1, 0, 1, 0),
573SOC_SINGLE("AIF2 EQ Switch", WM8994_AIF2_EQ_GAINS_1, 0, 1, 0),
574
575WM8994_DRC_SWITCH("AIF1DAC1 DRC Switch", WM8994_AIF1_DRC1_1, 2),
576WM8994_DRC_SWITCH("AIF1ADC1L DRC Switch", WM8994_AIF1_DRC1_1, 1),
577WM8994_DRC_SWITCH("AIF1ADC1R DRC Switch", WM8994_AIF1_DRC1_1, 0),
578
579WM8994_DRC_SWITCH("AIF1DAC2 DRC Switch", WM8994_AIF1_DRC2_1, 2),
580WM8994_DRC_SWITCH("AIF1ADC2L DRC Switch", WM8994_AIF1_DRC2_1, 1),
581WM8994_DRC_SWITCH("AIF1ADC2R DRC Switch", WM8994_AIF1_DRC2_1, 0),
582
583WM8994_DRC_SWITCH("AIF2DAC DRC Switch", WM8994_AIF2_DRC_1, 2),
584WM8994_DRC_SWITCH("AIF2ADCL DRC Switch", WM8994_AIF2_DRC_1, 1),
585WM8994_DRC_SWITCH("AIF2ADCR DRC Switch", WM8994_AIF2_DRC_1, 0),
586
587SOC_SINGLE_TLV("DAC1 Right Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
588 5, 12, 0, st_tlv),
589SOC_SINGLE_TLV("DAC1 Left Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
590 0, 12, 0, st_tlv),
591SOC_SINGLE_TLV("DAC2 Right Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
592 5, 12, 0, st_tlv),
593SOC_SINGLE_TLV("DAC2 Left Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
594 0, 12, 0, st_tlv),
595SOC_ENUM("Sidetone HPF Mux", sidetone_hpf),
596SOC_SINGLE("Sidetone HPF Switch", WM8994_SIDETONE, 6, 1, 0),
597
Uk Kim146fd572010-12-07 13:58:40 +0000598SOC_ENUM("AIF1ADC1 HPF Mode", aif1adc1_hpf),
599SOC_DOUBLE("AIF1ADC1 HPF Switch", WM8994_AIF1_ADC1_FILTERS, 12, 11, 1, 0),
600
601SOC_ENUM("AIF1ADC2 HPF Mode", aif1adc2_hpf),
602SOC_DOUBLE("AIF1ADC2 HPF Switch", WM8994_AIF1_ADC2_FILTERS, 12, 11, 1, 0),
603
604SOC_ENUM("AIF2ADC HPF Mode", aif2adc_hpf),
605SOC_DOUBLE("AIF2ADC HPF Switch", WM8994_AIF2_ADC_FILTERS, 12, 11, 1, 0),
606
Mark Brown154b26a2010-12-09 12:07:44 +0000607SOC_ENUM("ADC OSR", adc_osr),
608SOC_ENUM("DAC OSR", dac_osr),
609
Mark Brown9e6e96a2010-01-29 17:47:12 +0000610SOC_DOUBLE_R_TLV("DAC1 Volume", WM8994_DAC1_LEFT_VOLUME,
611 WM8994_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
612SOC_DOUBLE_R("DAC1 Switch", WM8994_DAC1_LEFT_VOLUME,
613 WM8994_DAC1_RIGHT_VOLUME, 9, 1, 1),
614
615SOC_DOUBLE_R_TLV("DAC2 Volume", WM8994_DAC2_LEFT_VOLUME,
616 WM8994_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
617SOC_DOUBLE_R("DAC2 Switch", WM8994_DAC2_LEFT_VOLUME,
618 WM8994_DAC2_RIGHT_VOLUME, 9, 1, 1),
619
620SOC_SINGLE_TLV("SPKL DAC2 Volume", WM8994_SPKMIXL_ATTENUATION,
621 6, 1, 1, wm_hubs_spkmix_tlv),
622SOC_SINGLE_TLV("SPKL DAC1 Volume", WM8994_SPKMIXL_ATTENUATION,
623 2, 1, 1, wm_hubs_spkmix_tlv),
624
625SOC_SINGLE_TLV("SPKR DAC2 Volume", WM8994_SPKMIXR_ATTENUATION,
626 6, 1, 1, wm_hubs_spkmix_tlv),
627SOC_SINGLE_TLV("SPKR DAC1 Volume", WM8994_SPKMIXR_ATTENUATION,
628 2, 1, 1, wm_hubs_spkmix_tlv),
629
630SOC_SINGLE_TLV("AIF1DAC1 3D Stereo Volume", WM8994_AIF1_DAC1_FILTERS_2,
631 10, 15, 0, wm8994_3d_tlv),
Mark Brown458350b2010-12-20 14:35:09 +0000632SOC_SINGLE("AIF1DAC1 3D Stereo Switch", WM8994_AIF1_DAC1_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000633 8, 1, 0),
634SOC_SINGLE_TLV("AIF1DAC2 3D Stereo Volume", WM8994_AIF1_DAC2_FILTERS_2,
635 10, 15, 0, wm8994_3d_tlv),
636SOC_SINGLE("AIF1DAC2 3D Stereo Switch", WM8994_AIF1_DAC2_FILTERS_2,
637 8, 1, 0),
Mark Brown458350b2010-12-20 14:35:09 +0000638SOC_SINGLE_TLV("AIF2DAC 3D Stereo Volume", WM8994_AIF2_DAC_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000639 10, 15, 0, wm8994_3d_tlv),
Mark Brown458350b2010-12-20 14:35:09 +0000640SOC_SINGLE("AIF2DAC 3D Stereo Switch", WM8994_AIF2_DAC_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000641 8, 1, 0),
642};
643
644static const struct snd_kcontrol_new wm8994_eq_controls[] = {
645SOC_SINGLE_TLV("AIF1DAC1 EQ1 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 11, 31, 0,
646 eq_tlv),
647SOC_SINGLE_TLV("AIF1DAC1 EQ2 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 6, 31, 0,
648 eq_tlv),
649SOC_SINGLE_TLV("AIF1DAC1 EQ3 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 1, 31, 0,
650 eq_tlv),
651SOC_SINGLE_TLV("AIF1DAC1 EQ4 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 11, 31, 0,
652 eq_tlv),
653SOC_SINGLE_TLV("AIF1DAC1 EQ5 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 6, 31, 0,
654 eq_tlv),
655
656SOC_SINGLE_TLV("AIF1DAC2 EQ1 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 11, 31, 0,
657 eq_tlv),
658SOC_SINGLE_TLV("AIF1DAC2 EQ2 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 6, 31, 0,
659 eq_tlv),
660SOC_SINGLE_TLV("AIF1DAC2 EQ3 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 1, 31, 0,
661 eq_tlv),
662SOC_SINGLE_TLV("AIF1DAC2 EQ4 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 11, 31, 0,
663 eq_tlv),
664SOC_SINGLE_TLV("AIF1DAC2 EQ5 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 6, 31, 0,
665 eq_tlv),
666
667SOC_SINGLE_TLV("AIF2 EQ1 Volume", WM8994_AIF2_EQ_GAINS_1, 11, 31, 0,
668 eq_tlv),
669SOC_SINGLE_TLV("AIF2 EQ2 Volume", WM8994_AIF2_EQ_GAINS_1, 6, 31, 0,
670 eq_tlv),
671SOC_SINGLE_TLV("AIF2 EQ3 Volume", WM8994_AIF2_EQ_GAINS_1, 1, 31, 0,
672 eq_tlv),
673SOC_SINGLE_TLV("AIF2 EQ4 Volume", WM8994_AIF2_EQ_GAINS_2, 11, 31, 0,
674 eq_tlv),
675SOC_SINGLE_TLV("AIF2 EQ5 Volume", WM8994_AIF2_EQ_GAINS_2, 6, 31, 0,
676 eq_tlv),
677};
678
Mark Brown45a690f2012-08-15 19:20:54 +0100679static const struct snd_kcontrol_new wm8994_drc_controls[] = {
680SND_SOC_BYTES_MASK("AIF1.1 DRC", WM8994_AIF1_DRC1_1, 5,
681 WM8994_AIF1DAC1_DRC_ENA | WM8994_AIF1ADC1L_DRC_ENA |
682 WM8994_AIF1ADC1R_DRC_ENA),
683SND_SOC_BYTES_MASK("AIF1.2 DRC", WM8994_AIF1_DRC2_1, 5,
684 WM8994_AIF1DAC2_DRC_ENA | WM8994_AIF1ADC2L_DRC_ENA |
685 WM8994_AIF1ADC2R_DRC_ENA),
686SND_SOC_BYTES_MASK("AIF2 DRC", WM8994_AIF2_DRC_1, 5,
687 WM8994_AIF2DAC_DRC_ENA | WM8994_AIF2ADCL_DRC_ENA |
688 WM8994_AIF2ADCR_DRC_ENA),
689};
690
Mark Brown1ddc07d2011-08-16 10:08:48 +0900691static const char *wm8958_ng_text[] = {
692 "30ms", "125ms", "250ms", "500ms",
693};
694
Takashi Iwaie61a35b2014-02-18 09:45:30 +0100695static SOC_ENUM_SINGLE_DECL(wm8958_aif1dac1_ng_hold,
696 WM8958_AIF1_DAC1_NOISE_GATE,
697 WM8958_AIF1DAC1_NG_THR_SHIFT,
698 wm8958_ng_text);
Mark Brown1ddc07d2011-08-16 10:08:48 +0900699
Takashi Iwaie61a35b2014-02-18 09:45:30 +0100700static SOC_ENUM_SINGLE_DECL(wm8958_aif1dac2_ng_hold,
701 WM8958_AIF1_DAC2_NOISE_GATE,
702 WM8958_AIF1DAC2_NG_THR_SHIFT,
703 wm8958_ng_text);
Mark Brown1ddc07d2011-08-16 10:08:48 +0900704
Takashi Iwaie61a35b2014-02-18 09:45:30 +0100705static SOC_ENUM_SINGLE_DECL(wm8958_aif2dac_ng_hold,
706 WM8958_AIF2_DAC_NOISE_GATE,
707 WM8958_AIF2DAC_NG_THR_SHIFT,
708 wm8958_ng_text);
Mark Brown1ddc07d2011-08-16 10:08:48 +0900709
Mark Brownc4431df2010-11-26 15:21:07 +0000710static const struct snd_kcontrol_new wm8958_snd_controls[] = {
711SOC_SINGLE_TLV("AIF3 Boost Volume", WM8958_AIF3_CONTROL_2, 10, 3, 0, aif_tlv),
Mark Brown1ddc07d2011-08-16 10:08:48 +0900712
713SOC_SINGLE("AIF1DAC1 Noise Gate Switch", WM8958_AIF1_DAC1_NOISE_GATE,
714 WM8958_AIF1DAC1_NG_ENA_SHIFT, 1, 0),
715SOC_ENUM("AIF1DAC1 Noise Gate Hold Time", wm8958_aif1dac1_ng_hold),
716SOC_SINGLE_TLV("AIF1DAC1 Noise Gate Threshold Volume",
717 WM8958_AIF1_DAC1_NOISE_GATE, WM8958_AIF1DAC1_NG_THR_SHIFT,
718 7, 1, ng_tlv),
719
720SOC_SINGLE("AIF1DAC2 Noise Gate Switch", WM8958_AIF1_DAC2_NOISE_GATE,
721 WM8958_AIF1DAC2_NG_ENA_SHIFT, 1, 0),
722SOC_ENUM("AIF1DAC2 Noise Gate Hold Time", wm8958_aif1dac2_ng_hold),
723SOC_SINGLE_TLV("AIF1DAC2 Noise Gate Threshold Volume",
724 WM8958_AIF1_DAC2_NOISE_GATE, WM8958_AIF1DAC2_NG_THR_SHIFT,
725 7, 1, ng_tlv),
726
727SOC_SINGLE("AIF2DAC Noise Gate Switch", WM8958_AIF2_DAC_NOISE_GATE,
728 WM8958_AIF2DAC_NG_ENA_SHIFT, 1, 0),
729SOC_ENUM("AIF2DAC Noise Gate Hold Time", wm8958_aif2dac_ng_hold),
730SOC_SINGLE_TLV("AIF2DAC Noise Gate Threshold Volume",
731 WM8958_AIF2_DAC_NOISE_GATE, WM8958_AIF2DAC_NG_THR_SHIFT,
732 7, 1, ng_tlv),
Mark Brownc4431df2010-11-26 15:21:07 +0000733};
734
Mark Brown81204c82011-05-24 17:35:53 +0800735static const struct snd_kcontrol_new wm1811_snd_controls[] = {
736SOC_SINGLE_TLV("MIXINL IN1LP Boost Volume", WM8994_INPUT_MIXER_1, 7, 1, 0,
737 mixin_boost_tlv),
738SOC_SINGLE_TLV("MIXINL IN1RP Boost Volume", WM8994_INPUT_MIXER_1, 8, 1, 0,
739 mixin_boost_tlv),
740};
741
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000742/* We run all mode setting through a function to enforce audio mode */
743static void wm1811_jackdet_set_mode(struct snd_soc_codec *codec, u16 mode)
744{
745 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
746
Mark Brown78b76db2012-11-22 17:02:09 +0900747 if (!wm8994->jackdet || !wm8994->micdet[0].jack)
Mark Brown28e33262012-03-03 00:10:02 +0000748 return;
749
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000750 if (wm8994->active_refcount)
751 mode = WM1811_JACKDET_MODE_AUDIO;
752
Mark Brown4752a882012-03-04 02:16:01 +0000753 if (mode == wm8994->jackdet_mode)
Mark Brown1defde22012-03-03 20:02:49 +0000754 return;
755
Mark Brown4752a882012-03-04 02:16:01 +0000756 wm8994->jackdet_mode = mode;
757
758 /* Always use audio mode to detect while the system is active */
759 if (mode != WM1811_JACKDET_MODE_NONE)
760 mode = WM1811_JACKDET_MODE_AUDIO;
761
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000762 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
763 WM1811_JACKDET_MODE_MASK, mode);
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000764}
765
766static void active_reference(struct snd_soc_codec *codec)
767{
768 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
769
770 mutex_lock(&wm8994->accdet_lock);
771
772 wm8994->active_refcount++;
773
774 dev_dbg(codec->dev, "Active refcount incremented, now %d\n",
775 wm8994->active_refcount);
776
Mark Brown1defde22012-03-03 20:02:49 +0000777 /* If we're using jack detection go into audio mode */
778 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_AUDIO);
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000779
780 mutex_unlock(&wm8994->accdet_lock);
781}
782
783static void active_dereference(struct snd_soc_codec *codec)
784{
785 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
786 u16 mode;
787
788 mutex_lock(&wm8994->accdet_lock);
789
790 wm8994->active_refcount--;
791
792 dev_dbg(codec->dev, "Active refcount decremented, now %d\n",
793 wm8994->active_refcount);
794
795 if (wm8994->active_refcount == 0) {
796 /* Go into appropriate detection only mode */
Mark Brown1defde22012-03-03 20:02:49 +0000797 if (wm8994->jack_mic || wm8994->mic_detecting)
798 mode = WM1811_JACKDET_MODE_MIC;
799 else
800 mode = WM1811_JACKDET_MODE_JACK;
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000801
Mark Brown1defde22012-03-03 20:02:49 +0000802 wm1811_jackdet_set_mode(codec, mode);
Mark Brownaf6b6fe2011-11-30 20:32:05 +0000803 }
804
805 mutex_unlock(&wm8994->accdet_lock);
806}
807
Mark Brown9e6e96a2010-01-29 17:47:12 +0000808static int clk_sys_event(struct snd_soc_dapm_widget *w,
809 struct snd_kcontrol *kcontrol, int event)
810{
Lars-Peter Clausen12e30802015-01-13 10:27:27 +0100811 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
Mark Brown99af79d2012-07-25 23:03:36 +0100812 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000813
814 switch (event) {
815 case SND_SOC_DAPM_PRE_PMU:
816 return configure_clock(codec);
817
Mark Brown99af79d2012-07-25 23:03:36 +0100818 case SND_SOC_DAPM_POST_PMU:
819 /*
820 * JACKDET won't run until we start the clock and it
821 * only reports deltas, make sure we notify the state
822 * up the stack on startup. Use a *very* generous
823 * timeout for paranoia, there's no urgency and we
824 * don't want false reports.
825 */
826 if (wm8994->jackdet && !wm8994->clk_has_run) {
Mark Brown68defe52013-07-18 22:47:02 +0100827 queue_delayed_work(system_power_efficient_wq,
828 &wm8994->jackdet_bootstrap,
829 msecs_to_jiffies(1000));
Mark Brown99af79d2012-07-25 23:03:36 +0100830 wm8994->clk_has_run = true;
831 }
832 break;
833
Mark Brown9e6e96a2010-01-29 17:47:12 +0000834 case SND_SOC_DAPM_POST_PMD:
835 configure_clock(codec);
836 break;
837 }
838
839 return 0;
840}
841
Mark Brown4b7ed832011-08-10 17:47:33 +0900842static void vmid_reference(struct snd_soc_codec *codec)
843{
844 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
845
Mark Browndb966f82012-02-06 12:07:08 +0000846 pm_runtime_get_sync(codec->dev);
847
Mark Brown4b7ed832011-08-10 17:47:33 +0900848 wm8994->vmid_refcount++;
849
850 dev_dbg(codec->dev, "Referencing VMID, refcount is now %d\n",
851 wm8994->vmid_refcount);
852
853 if (wm8994->vmid_refcount == 1) {
Mark Browncc6d5a82012-02-11 23:09:53 +0000854 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
Mark Browncc6d5a82012-02-11 23:09:53 +0000855 WM8994_LINEOUT1_DISCH |
Mark Brown22f8d052012-03-19 17:32:06 +0000856 WM8994_LINEOUT2_DISCH, 0);
Mark Browncc6d5a82012-02-11 23:09:53 +0000857
Mark Brownf7085642012-02-21 16:24:00 +0000858 wm_hubs_vmid_ena(codec);
859
Mark Brown22f8d052012-03-19 17:32:06 +0000860 switch (wm8994->vmid_mode) {
861 default:
Mark Browncbd71f32012-05-09 19:11:03 +0100862 WARN_ON(NULL == "Invalid VMID mode");
Gustavo A. R. Silva9d943052017-11-08 14:04:43 -0600863 /* fall through */
Mark Brown22f8d052012-03-19 17:32:06 +0000864 case WM8994_VMID_NORMAL:
865 /* Startup bias, VMID ramp & buffer */
866 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
867 WM8994_BIAS_SRC |
868 WM8994_VMID_DISCH |
869 WM8994_STARTUP_BIAS_ENA |
870 WM8994_VMID_BUF_ENA |
871 WM8994_VMID_RAMP_MASK,
872 WM8994_BIAS_SRC |
873 WM8994_STARTUP_BIAS_ENA |
874 WM8994_VMID_BUF_ENA |
Mark Browna3a1d9d2012-08-22 17:23:56 +0100875 (0x2 << WM8994_VMID_RAMP_SHIFT));
Mark Brown4b7ed832011-08-10 17:47:33 +0900876
Mark Brown22f8d052012-03-19 17:32:06 +0000877 /* Main bias enable, VMID=2x40k */
878 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
879 WM8994_BIAS_ENA |
880 WM8994_VMID_SEL_MASK,
881 WM8994_BIAS_ENA | 0x2);
Mark Brown4b7ed832011-08-10 17:47:33 +0900882
Mark Browna3a1d9d2012-08-22 17:23:56 +0100883 msleep(300);
Mark Browncc6d5a82012-02-11 23:09:53 +0000884
Mark Brown22f8d052012-03-19 17:32:06 +0000885 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
886 WM8994_VMID_RAMP_MASK |
887 WM8994_BIAS_SRC,
888 0);
889 break;
890
891 case WM8994_VMID_FORCE:
892 /* Startup bias, slow VMID ramp & buffer */
893 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
894 WM8994_BIAS_SRC |
895 WM8994_VMID_DISCH |
896 WM8994_STARTUP_BIAS_ENA |
897 WM8994_VMID_BUF_ENA |
898 WM8994_VMID_RAMP_MASK,
899 WM8994_BIAS_SRC |
900 WM8994_STARTUP_BIAS_ENA |
901 WM8994_VMID_BUF_ENA |
902 (0x2 << WM8994_VMID_RAMP_SHIFT));
903
904 /* Main bias enable, VMID=2x40k */
905 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
906 WM8994_BIAS_ENA |
907 WM8994_VMID_SEL_MASK,
908 WM8994_BIAS_ENA | 0x2);
909
910 msleep(400);
911
912 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
913 WM8994_VMID_RAMP_MASK |
914 WM8994_BIAS_SRC,
915 0);
916 break;
917 }
Mark Brown4b7ed832011-08-10 17:47:33 +0900918 }
919}
920
921static void vmid_dereference(struct snd_soc_codec *codec)
922{
923 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
924
925 wm8994->vmid_refcount--;
926
927 dev_dbg(codec->dev, "Dereferencing VMID, refcount is now %d\n",
928 wm8994->vmid_refcount);
929
930 if (wm8994->vmid_refcount == 0) {
Mark Brown22f8d052012-03-19 17:32:06 +0000931 if (wm8994->hubs.lineout1_se)
932 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_3,
933 WM8994_LINEOUT1N_ENA |
934 WM8994_LINEOUT1P_ENA,
935 WM8994_LINEOUT1N_ENA |
936 WM8994_LINEOUT1P_ENA);
937
938 if (wm8994->hubs.lineout2_se)
939 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_3,
940 WM8994_LINEOUT2N_ENA |
941 WM8994_LINEOUT2P_ENA,
942 WM8994_LINEOUT2N_ENA |
943 WM8994_LINEOUT2P_ENA);
944
945 /* Start discharging VMID */
Mark Brown4b7ed832011-08-10 17:47:33 +0900946 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
947 WM8994_BIAS_SRC |
Mark Brown22f8d052012-03-19 17:32:06 +0000948 WM8994_VMID_DISCH,
Mark Brown4b7ed832011-08-10 17:47:33 +0900949 WM8994_BIAS_SRC |
Mark Brown22f8d052012-03-19 17:32:06 +0000950 WM8994_VMID_DISCH);
Mark Brown4b7ed832011-08-10 17:47:33 +0900951
Mark Brownf95be9d2012-08-22 17:25:37 +0100952 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
953 WM8994_VMID_SEL_MASK, 0);
Mark Brown4b7ed832011-08-10 17:47:33 +0900954
Mark Brownf95be9d2012-08-22 17:25:37 +0100955 msleep(400);
Mark Browne85b26c2012-02-11 23:10:30 +0000956
Mark Brown22f8d052012-03-19 17:32:06 +0000957 /* Active discharge */
Mark Brown4b7ed832011-08-10 17:47:33 +0900958 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
959 WM8994_LINEOUT1_DISCH |
960 WM8994_LINEOUT2_DISCH,
961 WM8994_LINEOUT1_DISCH |
962 WM8994_LINEOUT2_DISCH);
963
Mark Brown22f8d052012-03-19 17:32:06 +0000964 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_3,
965 WM8994_LINEOUT1N_ENA |
966 WM8994_LINEOUT1P_ENA |
967 WM8994_LINEOUT2N_ENA |
968 WM8994_LINEOUT2P_ENA, 0);
969
Mark Brown4b7ed832011-08-10 17:47:33 +0900970 /* Switch off startup biases */
971 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
972 WM8994_BIAS_SRC |
973 WM8994_STARTUP_BIAS_ENA |
974 WM8994_VMID_BUF_ENA |
975 WM8994_VMID_RAMP_MASK, 0);
Mark Brown22f8d052012-03-19 17:32:06 +0000976
977 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
Mark Brownf95be9d2012-08-22 17:25:37 +0100978 WM8994_VMID_SEL_MASK, 0);
Mark Brown4b7ed832011-08-10 17:47:33 +0900979 }
Mark Browndb966f82012-02-06 12:07:08 +0000980
981 pm_runtime_put(codec->dev);
Mark Brown4b7ed832011-08-10 17:47:33 +0900982}
983
984static int vmid_event(struct snd_soc_dapm_widget *w,
985 struct snd_kcontrol *kcontrol, int event)
986{
Lars-Peter Clausen12e30802015-01-13 10:27:27 +0100987 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
Mark Brown4b7ed832011-08-10 17:47:33 +0900988
989 switch (event) {
990 case SND_SOC_DAPM_PRE_PMU:
991 vmid_reference(codec);
992 break;
993
994 case SND_SOC_DAPM_POST_PMD:
995 vmid_dereference(codec);
996 break;
997 }
998
999 return 0;
1000}
1001
Mark Brownc3403042012-04-26 21:29:29 +01001002static bool wm8994_check_class_w_digital(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00001003{
Mark Brown9e6e96a2010-01-29 17:47:12 +00001004 int source = 0; /* GCC flow analysis can't track enable */
1005 int reg, reg_r;
1006
Mark Brownc3403042012-04-26 21:29:29 +01001007 /* We also need the same AIF source for L/R and only one path */
Mark Brown9e6e96a2010-01-29 17:47:12 +00001008 reg = snd_soc_read(codec, WM8994_DAC1_LEFT_MIXER_ROUTING);
1009 switch (reg) {
1010 case WM8994_AIF2DACL_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +09001011 dev_vdbg(codec->dev, "Class W source AIF2DAC\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +00001012 source = 2 << WM8994_CP_DYN_SRC_SEL_SHIFT;
1013 break;
1014 case WM8994_AIF1DAC2L_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +09001015 dev_vdbg(codec->dev, "Class W source AIF1DAC2\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +00001016 source = 1 << WM8994_CP_DYN_SRC_SEL_SHIFT;
1017 break;
1018 case WM8994_AIF1DAC1L_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +09001019 dev_vdbg(codec->dev, "Class W source AIF1DAC1\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +00001020 source = 0 << WM8994_CP_DYN_SRC_SEL_SHIFT;
1021 break;
1022 default:
Mark Brownee839a22010-04-20 13:57:08 +09001023 dev_vdbg(codec->dev, "DAC mixer setting: %x\n", reg);
Mark Brownc3403042012-04-26 21:29:29 +01001024 return false;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001025 }
1026
1027 reg_r = snd_soc_read(codec, WM8994_DAC1_RIGHT_MIXER_ROUTING);
1028 if (reg_r != reg) {
Mark Brownee839a22010-04-20 13:57:08 +09001029 dev_vdbg(codec->dev, "Left and right DAC mixers different\n");
Mark Brownc3403042012-04-26 21:29:29 +01001030 return false;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001031 }
1032
Mark Brownc3403042012-04-26 21:29:29 +01001033 /* Set the source up */
1034 snd_soc_update_bits(codec, WM8994_CLASS_W_1,
1035 WM8994_CP_DYN_SRC_SEL_MASK, source);
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02001036
Mark Brownc3403042012-04-26 21:29:29 +01001037 return true;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001038}
1039
Mark Brown1a383362012-04-12 19:47:11 +01001040static int aif1clk_ev(struct snd_soc_dapm_widget *w,
1041 struct snd_kcontrol *kcontrol, int event)
1042{
Lars-Peter Clausen12e30802015-01-13 10:27:27 +01001043 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
Mark Brown79748cd2012-10-01 15:28:30 +01001044 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Vinod Kould3134e22013-05-28 15:41:57 +05301045 struct wm8994 *control = wm8994->wm8994;
Mark Brown1a383362012-04-12 19:47:11 +01001046 int mask = WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC1R_ENA;
Mark Brownbfd37bb2012-06-05 12:31:32 +01001047 int i;
Mark Brown1a383362012-04-12 19:47:11 +01001048 int dac;
1049 int adc;
1050 int val;
1051
1052 switch (control->type) {
1053 case WM8994:
1054 case WM8958:
1055 mask |= WM8994_AIF1DAC2L_ENA | WM8994_AIF1DAC2R_ENA;
1056 break;
1057 default:
1058 break;
1059 }
1060
1061 switch (event) {
1062 case SND_SOC_DAPM_PRE_PMU:
Mark Brown79748cd2012-10-01 15:28:30 +01001063 /* Don't enable timeslot 2 if not in use */
1064 if (wm8994->channels[0] <= 2)
1065 mask &= ~(WM8994_AIF1DAC2L_ENA | WM8994_AIF1DAC2R_ENA);
1066
Mark Brown1a383362012-04-12 19:47:11 +01001067 val = snd_soc_read(codec, WM8994_AIF1_CONTROL_1);
1068 if ((val & WM8994_AIF1ADCL_SRC) &&
1069 (val & WM8994_AIF1ADCR_SRC))
1070 adc = WM8994_AIF1ADC1R_ENA | WM8994_AIF1ADC2R_ENA;
1071 else if (!(val & WM8994_AIF1ADCL_SRC) &&
1072 !(val & WM8994_AIF1ADCR_SRC))
1073 adc = WM8994_AIF1ADC1L_ENA | WM8994_AIF1ADC2L_ENA;
1074 else
1075 adc = WM8994_AIF1ADC1R_ENA | WM8994_AIF1ADC2R_ENA |
1076 WM8994_AIF1ADC1L_ENA | WM8994_AIF1ADC2L_ENA;
1077
1078 val = snd_soc_read(codec, WM8994_AIF1_CONTROL_2);
1079 if ((val & WM8994_AIF1DACL_SRC) &&
1080 (val & WM8994_AIF1DACR_SRC))
1081 dac = WM8994_AIF1DAC1R_ENA | WM8994_AIF1DAC2R_ENA;
1082 else if (!(val & WM8994_AIF1DACL_SRC) &&
1083 !(val & WM8994_AIF1DACR_SRC))
1084 dac = WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC2L_ENA;
1085 else
1086 dac = WM8994_AIF1DAC1R_ENA | WM8994_AIF1DAC2R_ENA |
1087 WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC2L_ENA;
1088
1089 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
1090 mask, adc);
1091 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1092 mask, dac);
1093 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
1094 WM8994_AIF1DSPCLK_ENA |
1095 WM8994_SYSDSPCLK_ENA,
1096 WM8994_AIF1DSPCLK_ENA |
1097 WM8994_SYSDSPCLK_ENA);
1098 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4, mask,
1099 WM8994_AIF1ADC1R_ENA |
1100 WM8994_AIF1ADC1L_ENA |
1101 WM8994_AIF1ADC2R_ENA |
1102 WM8994_AIF1ADC2L_ENA);
1103 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5, mask,
1104 WM8994_AIF1DAC1R_ENA |
1105 WM8994_AIF1DAC1L_ENA |
1106 WM8994_AIF1DAC2R_ENA |
1107 WM8994_AIF1DAC2L_ENA);
1108 break;
1109
Mark Brownbfd37bb2012-06-05 12:31:32 +01001110 case SND_SOC_DAPM_POST_PMU:
1111 for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
1112 snd_soc_write(codec, wm8994_vu_bits[i].reg,
1113 snd_soc_read(codec,
1114 wm8994_vu_bits[i].reg));
1115 break;
1116
Mark Brown1a383362012-04-12 19:47:11 +01001117 case SND_SOC_DAPM_PRE_PMD:
1118 case SND_SOC_DAPM_POST_PMD:
1119 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1120 mask, 0);
1121 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
1122 mask, 0);
1123
1124 val = snd_soc_read(codec, WM8994_CLOCKING_1);
1125 if (val & WM8994_AIF2DSPCLK_ENA)
1126 val = WM8994_SYSDSPCLK_ENA;
1127 else
1128 val = 0;
1129 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
1130 WM8994_SYSDSPCLK_ENA |
1131 WM8994_AIF1DSPCLK_ENA, val);
1132 break;
1133 }
1134
1135 return 0;
1136}
1137
1138static int aif2clk_ev(struct snd_soc_dapm_widget *w,
1139 struct snd_kcontrol *kcontrol, int event)
1140{
Lars-Peter Clausen12e30802015-01-13 10:27:27 +01001141 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
Mark Brownbfd37bb2012-06-05 12:31:32 +01001142 int i;
Mark Brown1a383362012-04-12 19:47:11 +01001143 int dac;
1144 int adc;
1145 int val;
1146
1147 switch (event) {
1148 case SND_SOC_DAPM_PRE_PMU:
1149 val = snd_soc_read(codec, WM8994_AIF2_CONTROL_1);
1150 if ((val & WM8994_AIF2ADCL_SRC) &&
1151 (val & WM8994_AIF2ADCR_SRC))
1152 adc = WM8994_AIF2ADCR_ENA;
1153 else if (!(val & WM8994_AIF2ADCL_SRC) &&
1154 !(val & WM8994_AIF2ADCR_SRC))
1155 adc = WM8994_AIF2ADCL_ENA;
1156 else
1157 adc = WM8994_AIF2ADCL_ENA | WM8994_AIF2ADCR_ENA;
1158
1159
1160 val = snd_soc_read(codec, WM8994_AIF2_CONTROL_2);
1161 if ((val & WM8994_AIF2DACL_SRC) &&
1162 (val & WM8994_AIF2DACR_SRC))
1163 dac = WM8994_AIF2DACR_ENA;
1164 else if (!(val & WM8994_AIF2DACL_SRC) &&
1165 !(val & WM8994_AIF2DACR_SRC))
1166 dac = WM8994_AIF2DACL_ENA;
1167 else
1168 dac = WM8994_AIF2DACL_ENA | WM8994_AIF2DACR_ENA;
1169
1170 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
1171 WM8994_AIF2ADCL_ENA |
1172 WM8994_AIF2ADCR_ENA, adc);
1173 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1174 WM8994_AIF2DACL_ENA |
1175 WM8994_AIF2DACR_ENA, dac);
1176 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
1177 WM8994_AIF2DSPCLK_ENA |
1178 WM8994_SYSDSPCLK_ENA,
1179 WM8994_AIF2DSPCLK_ENA |
1180 WM8994_SYSDSPCLK_ENA);
1181 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
1182 WM8994_AIF2ADCL_ENA |
1183 WM8994_AIF2ADCR_ENA,
1184 WM8994_AIF2ADCL_ENA |
1185 WM8994_AIF2ADCR_ENA);
1186 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1187 WM8994_AIF2DACL_ENA |
1188 WM8994_AIF2DACR_ENA,
1189 WM8994_AIF2DACL_ENA |
1190 WM8994_AIF2DACR_ENA);
1191 break;
1192
Mark Brownbfd37bb2012-06-05 12:31:32 +01001193 case SND_SOC_DAPM_POST_PMU:
1194 for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
1195 snd_soc_write(codec, wm8994_vu_bits[i].reg,
1196 snd_soc_read(codec,
1197 wm8994_vu_bits[i].reg));
1198 break;
1199
Mark Brown1a383362012-04-12 19:47:11 +01001200 case SND_SOC_DAPM_PRE_PMD:
1201 case SND_SOC_DAPM_POST_PMD:
1202 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1203 WM8994_AIF2DACL_ENA |
1204 WM8994_AIF2DACR_ENA, 0);
Mark Brownc7f5f232012-05-15 18:13:00 +01001205 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
Mark Brown1a383362012-04-12 19:47:11 +01001206 WM8994_AIF2ADCL_ENA |
1207 WM8994_AIF2ADCR_ENA, 0);
1208
1209 val = snd_soc_read(codec, WM8994_CLOCKING_1);
1210 if (val & WM8994_AIF1DSPCLK_ENA)
1211 val = WM8994_SYSDSPCLK_ENA;
1212 else
1213 val = 0;
1214 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
1215 WM8994_SYSDSPCLK_ENA |
1216 WM8994_AIF2DSPCLK_ENA, val);
1217 break;
1218 }
1219
1220 return 0;
1221}
1222
1223static int aif1clk_late_ev(struct snd_soc_dapm_widget *w,
1224 struct snd_kcontrol *kcontrol, int event)
1225{
Lars-Peter Clausen12e30802015-01-13 10:27:27 +01001226 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
Mark Brown1a383362012-04-12 19:47:11 +01001227 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1228
1229 switch (event) {
1230 case SND_SOC_DAPM_PRE_PMU:
1231 wm8994->aif1clk_enable = 1;
1232 break;
1233 case SND_SOC_DAPM_POST_PMD:
1234 wm8994->aif1clk_disable = 1;
1235 break;
1236 }
1237
1238 return 0;
1239}
1240
1241static int aif2clk_late_ev(struct snd_soc_dapm_widget *w,
1242 struct snd_kcontrol *kcontrol, int event)
1243{
Lars-Peter Clausen12e30802015-01-13 10:27:27 +01001244 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
Mark Brown1a383362012-04-12 19:47:11 +01001245 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1246
1247 switch (event) {
1248 case SND_SOC_DAPM_PRE_PMU:
1249 wm8994->aif2clk_enable = 1;
1250 break;
1251 case SND_SOC_DAPM_POST_PMD:
1252 wm8994->aif2clk_disable = 1;
1253 break;
1254 }
1255
1256 return 0;
1257}
1258
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001259static int late_enable_ev(struct snd_soc_dapm_widget *w,
1260 struct snd_kcontrol *kcontrol, int event)
1261{
Lars-Peter Clausen12e30802015-01-13 10:27:27 +01001262 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001263 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1264
1265 switch (event) {
1266 case SND_SOC_DAPM_PRE_PMU:
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001267 if (wm8994->aif1clk_enable) {
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001268 aif1clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMU);
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001269 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1270 WM8994_AIF1CLK_ENA_MASK,
1271 WM8994_AIF1CLK_ENA);
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001272 aif1clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMU);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001273 wm8994->aif1clk_enable = 0;
1274 }
1275 if (wm8994->aif2clk_enable) {
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001276 aif2clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMU);
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001277 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1278 WM8994_AIF2CLK_ENA_MASK,
1279 WM8994_AIF2CLK_ENA);
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001280 aif2clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMU);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001281 wm8994->aif2clk_enable = 0;
1282 }
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001283 break;
1284 }
1285
Mark Brownc6b7b572011-03-11 18:13:12 +00001286 /* We may also have postponed startup of DSP, handle that. */
1287 wm8958_aif_ev(w, kcontrol, event);
1288
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001289 return 0;
1290}
1291
1292static int late_disable_ev(struct snd_soc_dapm_widget *w,
1293 struct snd_kcontrol *kcontrol, int event)
1294{
Lars-Peter Clausen12e30802015-01-13 10:27:27 +01001295 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001296 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1297
1298 switch (event) {
1299 case SND_SOC_DAPM_POST_PMD:
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001300 if (wm8994->aif1clk_disable) {
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001301 aif1clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMD);
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001302 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1303 WM8994_AIF1CLK_ENA_MASK, 0);
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001304 aif1clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMD);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001305 wm8994->aif1clk_disable = 0;
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001306 }
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001307 if (wm8994->aif2clk_disable) {
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001308 aif2clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMD);
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001309 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1310 WM8994_AIF2CLK_ENA_MASK, 0);
Mark Brownc8fdc1b2012-06-05 12:25:19 +01001311 aif2clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMD);
Dimitris Papastamosa3cff812011-02-28 17:24:11 +00001312 wm8994->aif2clk_disable = 0;
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001313 }
1314 break;
1315 }
1316
1317 return 0;
1318}
1319
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001320static int adc_mux_ev(struct snd_soc_dapm_widget *w,
1321 struct snd_kcontrol *kcontrol, int event)
1322{
1323 late_enable_ev(w, kcontrol, event);
1324 return 0;
1325}
1326
Dimitris Papastamosb462c6e2011-03-01 12:54:39 +00001327static int micbias_ev(struct snd_soc_dapm_widget *w,
1328 struct snd_kcontrol *kcontrol, int event)
1329{
1330 late_enable_ev(w, kcontrol, event);
1331 return 0;
1332}
1333
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001334static int dac_ev(struct snd_soc_dapm_widget *w,
1335 struct snd_kcontrol *kcontrol, int event)
1336{
Lars-Peter Clausen12e30802015-01-13 10:27:27 +01001337 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001338 unsigned int mask = 1 << w->shift;
1339
1340 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1341 mask, mask);
1342 return 0;
1343}
1344
Mark Brown9e6e96a2010-01-29 17:47:12 +00001345static const char *adc_mux_text[] = {
1346 "ADC",
1347 "DMIC",
1348};
1349
Lars-Peter Clausen86d4c9a2014-02-28 08:31:09 +01001350static SOC_ENUM_SINGLE_VIRT_DECL(adc_enum, adc_mux_text);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001351
1352static const struct snd_kcontrol_new adcl_mux =
Lars-Peter Clausen37d20302014-04-14 21:31:09 +02001353 SOC_DAPM_ENUM("ADCL Mux", adc_enum);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001354
1355static const struct snd_kcontrol_new adcr_mux =
Lars-Peter Clausen37d20302014-04-14 21:31:09 +02001356 SOC_DAPM_ENUM("ADCR Mux", adc_enum);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001357
1358static const struct snd_kcontrol_new left_speaker_mixer[] = {
1359SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 9, 1, 0),
1360SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 7, 1, 0),
1361SOC_DAPM_SINGLE("IN1LP Switch", WM8994_SPEAKER_MIXER, 5, 1, 0),
1362SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 3, 1, 0),
1363SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 1, 1, 0),
1364};
1365
1366static const struct snd_kcontrol_new right_speaker_mixer[] = {
1367SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 8, 1, 0),
1368SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 6, 1, 0),
1369SOC_DAPM_SINGLE("IN1RP Switch", WM8994_SPEAKER_MIXER, 4, 1, 0),
1370SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 2, 1, 0),
1371SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 0, 1, 0),
1372};
1373
1374/* Debugging; dump chip status after DAPM transitions */
1375static int post_ev(struct snd_soc_dapm_widget *w,
1376 struct snd_kcontrol *kcontrol, int event)
1377{
Lars-Peter Clausen12e30802015-01-13 10:27:27 +01001378 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001379 dev_dbg(codec->dev, "SRC status: %x\n",
1380 snd_soc_read(codec,
1381 WM8994_RATE_STATUS));
1382 return 0;
1383}
1384
1385static const struct snd_kcontrol_new aif1adc1l_mix[] = {
1386SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1387 1, 1, 0),
1388SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1389 0, 1, 0),
1390};
1391
1392static const struct snd_kcontrol_new aif1adc1r_mix[] = {
1393SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1394 1, 1, 0),
1395SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1396 0, 1, 0),
1397};
1398
Mark Browna3257ba2010-07-19 14:02:34 +01001399static const struct snd_kcontrol_new aif1adc2l_mix[] = {
1400SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1401 1, 1, 0),
1402SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1403 0, 1, 0),
1404};
1405
1406static const struct snd_kcontrol_new aif1adc2r_mix[] = {
1407SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1408 1, 1, 0),
1409SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1410 0, 1, 0),
1411};
1412
Mark Brown9e6e96a2010-01-29 17:47:12 +00001413static const struct snd_kcontrol_new aif2dac2l_mix[] = {
1414SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1415 5, 1, 0),
1416SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1417 4, 1, 0),
1418SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1419 2, 1, 0),
1420SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1421 1, 1, 0),
1422SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1423 0, 1, 0),
1424};
1425
1426static const struct snd_kcontrol_new aif2dac2r_mix[] = {
1427SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1428 5, 1, 0),
1429SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1430 4, 1, 0),
1431SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1432 2, 1, 0),
1433SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1434 1, 1, 0),
1435SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1436 0, 1, 0),
1437};
1438
1439#define WM8994_CLASS_W_SWITCH(xname, reg, shift, max, invert) \
Lars-Peter Clausen6e065092013-06-19 19:33:59 +02001440 SOC_SINGLE_EXT(xname, reg, shift, max, invert, \
Mark Browned6a2772013-08-05 23:34:17 +01001441 snd_soc_dapm_get_volsw, wm8994_put_class_w)
Mark Brown9e6e96a2010-01-29 17:47:12 +00001442
1443static int wm8994_put_class_w(struct snd_kcontrol *kcontrol,
1444 struct snd_ctl_elem_value *ucontrol)
1445{
Lars-Peter Clauseneee5d7f2013-07-29 17:13:57 +02001446 struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001447 int ret;
1448
1449 ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
1450
Mark Brownc3403042012-04-26 21:29:29 +01001451 wm_hubs_update_class_w(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001452
1453 return ret;
1454}
1455
1456static const struct snd_kcontrol_new dac1l_mix[] = {
1457WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1458 5, 1, 0),
1459WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1460 4, 1, 0),
1461WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1462 2, 1, 0),
1463WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1464 1, 1, 0),
1465WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1466 0, 1, 0),
1467};
1468
1469static const struct snd_kcontrol_new dac1r_mix[] = {
1470WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1471 5, 1, 0),
1472WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1473 4, 1, 0),
1474WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1475 2, 1, 0),
1476WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1477 1, 1, 0),
1478WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1479 0, 1, 0),
1480};
1481
1482static const char *sidetone_text[] = {
1483 "ADC/DMIC1", "DMIC2",
1484};
1485
Takashi Iwaie61a35b2014-02-18 09:45:30 +01001486static SOC_ENUM_SINGLE_DECL(sidetone1_enum,
1487 WM8994_SIDETONE, 0, sidetone_text);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001488
1489static const struct snd_kcontrol_new sidetone1_mux =
1490 SOC_DAPM_ENUM("Left Sidetone Mux", sidetone1_enum);
1491
Takashi Iwaie61a35b2014-02-18 09:45:30 +01001492static SOC_ENUM_SINGLE_DECL(sidetone2_enum,
1493 WM8994_SIDETONE, 1, sidetone_text);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001494
1495static const struct snd_kcontrol_new sidetone2_mux =
1496 SOC_DAPM_ENUM("Right Sidetone Mux", sidetone2_enum);
1497
1498static const char *aif1dac_text[] = {
1499 "AIF1DACDAT", "AIF3DACDAT",
1500};
1501
Mark Brown50941962013-05-30 13:42:26 +01001502static const char *loopback_text[] = {
1503 "None", "ADCDAT",
1504};
1505
Takashi Iwaie61a35b2014-02-18 09:45:30 +01001506static SOC_ENUM_SINGLE_DECL(aif1_loopback_enum,
1507 WM8994_AIF1_CONTROL_2,
1508 WM8994_AIF1_LOOPBACK_SHIFT,
1509 loopback_text);
Mark Brown50941962013-05-30 13:42:26 +01001510
1511static const struct snd_kcontrol_new aif1_loopback =
1512 SOC_DAPM_ENUM("AIF1 Loopback", aif1_loopback_enum);
1513
Takashi Iwaie61a35b2014-02-18 09:45:30 +01001514static SOC_ENUM_SINGLE_DECL(aif2_loopback_enum,
1515 WM8994_AIF2_CONTROL_2,
1516 WM8994_AIF2_LOOPBACK_SHIFT,
1517 loopback_text);
Mark Brown50941962013-05-30 13:42:26 +01001518
1519static const struct snd_kcontrol_new aif2_loopback =
1520 SOC_DAPM_ENUM("AIF2 Loopback", aif2_loopback_enum);
1521
Takashi Iwaie61a35b2014-02-18 09:45:30 +01001522static SOC_ENUM_SINGLE_DECL(aif1dac_enum,
1523 WM8994_POWER_MANAGEMENT_6, 0, aif1dac_text);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001524
1525static const struct snd_kcontrol_new aif1dac_mux =
1526 SOC_DAPM_ENUM("AIF1DAC Mux", aif1dac_enum);
1527
1528static const char *aif2dac_text[] = {
1529 "AIF2DACDAT", "AIF3DACDAT",
1530};
1531
Takashi Iwaie61a35b2014-02-18 09:45:30 +01001532static SOC_ENUM_SINGLE_DECL(aif2dac_enum,
1533 WM8994_POWER_MANAGEMENT_6, 1, aif2dac_text);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001534
1535static const struct snd_kcontrol_new aif2dac_mux =
1536 SOC_DAPM_ENUM("AIF2DAC Mux", aif2dac_enum);
1537
1538static const char *aif2adc_text[] = {
1539 "AIF2ADCDAT", "AIF3DACDAT",
1540};
1541
Takashi Iwaie61a35b2014-02-18 09:45:30 +01001542static SOC_ENUM_SINGLE_DECL(aif2adc_enum,
1543 WM8994_POWER_MANAGEMENT_6, 2, aif2adc_text);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001544
1545static const struct snd_kcontrol_new aif2adc_mux =
1546 SOC_DAPM_ENUM("AIF2ADC Mux", aif2adc_enum);
1547
1548static const char *aif3adc_text[] = {
Mark Brownc4431df2010-11-26 15:21:07 +00001549 "AIF1ADCDAT", "AIF2ADCDAT", "AIF2DACDAT", "Mono PCM",
Mark Brown9e6e96a2010-01-29 17:47:12 +00001550};
1551
Takashi Iwaie61a35b2014-02-18 09:45:30 +01001552static SOC_ENUM_SINGLE_DECL(wm8994_aif3adc_enum,
1553 WM8994_POWER_MANAGEMENT_6, 3, aif3adc_text);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001554
Mark Brownc4431df2010-11-26 15:21:07 +00001555static const struct snd_kcontrol_new wm8994_aif3adc_mux =
1556 SOC_DAPM_ENUM("AIF3ADC Mux", wm8994_aif3adc_enum);
1557
Takashi Iwaie61a35b2014-02-18 09:45:30 +01001558static SOC_ENUM_SINGLE_DECL(wm8958_aif3adc_enum,
1559 WM8994_POWER_MANAGEMENT_6, 3, aif3adc_text);
Mark Brownc4431df2010-11-26 15:21:07 +00001560
1561static const struct snd_kcontrol_new wm8958_aif3adc_mux =
1562 SOC_DAPM_ENUM("AIF3ADC Mux", wm8958_aif3adc_enum);
1563
1564static const char *mono_pcm_out_text[] = {
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02001565 "None", "AIF2ADCL", "AIF2ADCR",
Mark Brownc4431df2010-11-26 15:21:07 +00001566};
1567
Takashi Iwaie61a35b2014-02-18 09:45:30 +01001568static SOC_ENUM_SINGLE_DECL(mono_pcm_out_enum,
1569 WM8994_POWER_MANAGEMENT_6, 9, mono_pcm_out_text);
Mark Brownc4431df2010-11-26 15:21:07 +00001570
1571static const struct snd_kcontrol_new mono_pcm_out_mux =
1572 SOC_DAPM_ENUM("Mono PCM Out Mux", mono_pcm_out_enum);
1573
1574static const char *aif2dac_src_text[] = {
1575 "AIF2", "AIF3",
1576};
1577
1578/* Note that these two control shouldn't be simultaneously switched to AIF3 */
Takashi Iwaie61a35b2014-02-18 09:45:30 +01001579static SOC_ENUM_SINGLE_DECL(aif2dacl_src_enum,
1580 WM8994_POWER_MANAGEMENT_6, 7, aif2dac_src_text);
Mark Brownc4431df2010-11-26 15:21:07 +00001581
1582static const struct snd_kcontrol_new aif2dacl_src_mux =
1583 SOC_DAPM_ENUM("AIF2DACL Mux", aif2dacl_src_enum);
1584
Takashi Iwaie61a35b2014-02-18 09:45:30 +01001585static SOC_ENUM_SINGLE_DECL(aif2dacr_src_enum,
1586 WM8994_POWER_MANAGEMENT_6, 8, aif2dac_src_text);
Mark Brownc4431df2010-11-26 15:21:07 +00001587
1588static const struct snd_kcontrol_new aif2dacr_src_mux =
1589 SOC_DAPM_ENUM("AIF2DACR Mux", aif2dacr_src_enum);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001590
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001591static const struct snd_soc_dapm_widget wm8994_lateclk_revd_widgets[] = {
Mark Brown1a383362012-04-12 19:47:11 +01001592SND_SOC_DAPM_SUPPLY("AIF1CLK", SND_SOC_NOPM, 0, 0, aif1clk_late_ev,
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001593 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown1a383362012-04-12 19:47:11 +01001594SND_SOC_DAPM_SUPPLY("AIF2CLK", SND_SOC_NOPM, 0, 0, aif2clk_late_ev,
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001595 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1596
1597SND_SOC_DAPM_PGA_E("Late DAC1L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1598 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1599SND_SOC_DAPM_PGA_E("Late DAC1R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1600 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1601SND_SOC_DAPM_PGA_E("Late DAC2L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1602 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1603SND_SOC_DAPM_PGA_E("Late DAC2R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1604 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Mark Brownb70a51b2011-06-29 00:21:09 -07001605SND_SOC_DAPM_PGA_E("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0,
1606 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1607
1608SND_SOC_DAPM_MIXER_E("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
1609 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer),
1610 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1611SND_SOC_DAPM_MIXER_E("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
1612 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer),
1613 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Mark Brownc3403042012-04-26 21:29:29 +01001614SND_SOC_DAPM_MUX_E("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpl_mux,
Mark Brownb70a51b2011-06-29 00:21:09 -07001615 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Mark Brownc3403042012-04-26 21:29:29 +01001616SND_SOC_DAPM_MUX_E("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpr_mux,
Mark Brownb70a51b2011-06-29 00:21:09 -07001617 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001618
1619SND_SOC_DAPM_POST("Late Disable PGA", late_disable_ev)
1620};
1621
1622static const struct snd_soc_dapm_widget wm8994_lateclk_widgets[] = {
Mark Brown1a383362012-04-12 19:47:11 +01001623SND_SOC_DAPM_SUPPLY("AIF1CLK", WM8994_AIF1_CLOCKING_1, 0, 0, aif1clk_ev,
Mark Brownbfd37bb2012-06-05 12:31:32 +01001624 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
1625 SND_SOC_DAPM_PRE_PMD),
Mark Brown1a383362012-04-12 19:47:11 +01001626SND_SOC_DAPM_SUPPLY("AIF2CLK", WM8994_AIF2_CLOCKING_1, 0, 0, aif2clk_ev,
Mark Brownbfd37bb2012-06-05 12:31:32 +01001627 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
1628 SND_SOC_DAPM_PRE_PMD),
Mark Brownb70a51b2011-06-29 00:21:09 -07001629SND_SOC_DAPM_PGA("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0),
1630SND_SOC_DAPM_MIXER("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
1631 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
1632SND_SOC_DAPM_MIXER("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
1633 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
Mark Brownc3403042012-04-26 21:29:29 +01001634SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpl_mux),
1635SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpr_mux),
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001636};
1637
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001638static const struct snd_soc_dapm_widget wm8994_dac_revd_widgets[] = {
1639SND_SOC_DAPM_DAC_E("DAC2L", NULL, SND_SOC_NOPM, 3, 0,
1640 dac_ev, SND_SOC_DAPM_PRE_PMU),
1641SND_SOC_DAPM_DAC_E("DAC2R", NULL, SND_SOC_NOPM, 2, 0,
1642 dac_ev, SND_SOC_DAPM_PRE_PMU),
1643SND_SOC_DAPM_DAC_E("DAC1L", NULL, SND_SOC_NOPM, 1, 0,
1644 dac_ev, SND_SOC_DAPM_PRE_PMU),
1645SND_SOC_DAPM_DAC_E("DAC1R", NULL, SND_SOC_NOPM, 0, 0,
1646 dac_ev, SND_SOC_DAPM_PRE_PMU),
1647};
1648
1649static const struct snd_soc_dapm_widget wm8994_dac_widgets[] = {
1650SND_SOC_DAPM_DAC("DAC2L", NULL, WM8994_POWER_MANAGEMENT_5, 3, 0),
Mark Brown0627bd22011-03-09 19:09:17 +00001651SND_SOC_DAPM_DAC("DAC2R", NULL, WM8994_POWER_MANAGEMENT_5, 2, 0),
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001652SND_SOC_DAPM_DAC("DAC1L", NULL, WM8994_POWER_MANAGEMENT_5, 1, 0),
1653SND_SOC_DAPM_DAC("DAC1R", NULL, WM8994_POWER_MANAGEMENT_5, 0, 0),
1654};
1655
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001656static const struct snd_soc_dapm_widget wm8994_adc_revd_widgets[] = {
Lars-Peter Clausen37d20302014-04-14 21:31:09 +02001657SND_SOC_DAPM_MUX_E("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux,
Mark Brown87b86ad2011-08-14 13:39:20 +09001658 adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
Lars-Peter Clausen37d20302014-04-14 21:31:09 +02001659SND_SOC_DAPM_MUX_E("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux,
Mark Brown87b86ad2011-08-14 13:39:20 +09001660 adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001661};
1662
1663static const struct snd_soc_dapm_widget wm8994_adc_widgets[] = {
Lars-Peter Clausen37d20302014-04-14 21:31:09 +02001664SND_SOC_DAPM_MUX("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux),
1665SND_SOC_DAPM_MUX("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux),
Dimitris Papastamos04d28682011-03-01 11:47:10 +00001666};
1667
Mark Brown9e6e96a2010-01-29 17:47:12 +00001668static const struct snd_soc_dapm_widget wm8994_dapm_widgets[] = {
1669SND_SOC_DAPM_INPUT("DMIC1DAT"),
1670SND_SOC_DAPM_INPUT("DMIC2DAT"),
Mark Brown66b47fd2010-07-08 11:25:43 +09001671SND_SOC_DAPM_INPUT("Clock"),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001672
Dimitris Papastamosb462c6e2011-03-01 12:54:39 +00001673SND_SOC_DAPM_SUPPLY_S("MICBIAS Supply", 1, SND_SOC_NOPM, 0, 0, micbias_ev,
1674 SND_SOC_DAPM_PRE_PMU),
Mark Brown4b7ed832011-08-10 17:47:33 +09001675SND_SOC_DAPM_SUPPLY("VMID", SND_SOC_NOPM, 0, 0, vmid_event,
1676 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
Dimitris Papastamosb462c6e2011-03-01 12:54:39 +00001677
Mark Brown9e6e96a2010-01-29 17:47:12 +00001678SND_SOC_DAPM_SUPPLY("CLK_SYS", SND_SOC_NOPM, 0, 0, clk_sys_event,
Mark Brown99af79d2012-07-25 23:03:36 +01001679 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
1680 SND_SOC_DAPM_PRE_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001681
Mark Brown1a383362012-04-12 19:47:11 +01001682SND_SOC_DAPM_SUPPLY("DSP1CLK", SND_SOC_NOPM, 3, 0, NULL, 0),
1683SND_SOC_DAPM_SUPPLY("DSP2CLK", SND_SOC_NOPM, 2, 0, NULL, 0),
1684SND_SOC_DAPM_SUPPLY("DSPINTCLK", SND_SOC_NOPM, 1, 0, NULL, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001685
Mark Brown7f94de42011-02-03 16:27:34 +00001686SND_SOC_DAPM_AIF_OUT("AIF1ADC1L", NULL,
Mark Brown1a383362012-04-12 19:47:11 +01001687 0, SND_SOC_NOPM, 9, 0),
Mark Brown7f94de42011-02-03 16:27:34 +00001688SND_SOC_DAPM_AIF_OUT("AIF1ADC1R", NULL,
Mark Brown1a383362012-04-12 19:47:11 +01001689 0, SND_SOC_NOPM, 8, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001690SND_SOC_DAPM_AIF_IN_E("AIF1DAC1L", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001691 SND_SOC_NOPM, 9, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001692 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brownd6addcc2010-11-26 15:21:08 +00001693SND_SOC_DAPM_AIF_IN_E("AIF1DAC1R", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001694 SND_SOC_NOPM, 8, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001695 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001696
Mark Brown7f94de42011-02-03 16:27:34 +00001697SND_SOC_DAPM_AIF_OUT("AIF1ADC2L", NULL,
Mark Brown1a383362012-04-12 19:47:11 +01001698 0, SND_SOC_NOPM, 11, 0),
Mark Brown7f94de42011-02-03 16:27:34 +00001699SND_SOC_DAPM_AIF_OUT("AIF1ADC2R", NULL,
Mark Brown1a383362012-04-12 19:47:11 +01001700 0, SND_SOC_NOPM, 10, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001701SND_SOC_DAPM_AIF_IN_E("AIF1DAC2L", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001702 SND_SOC_NOPM, 11, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001703 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brownd6addcc2010-11-26 15:21:08 +00001704SND_SOC_DAPM_AIF_IN_E("AIF1DAC2R", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001705 SND_SOC_NOPM, 10, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001706 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001707
1708SND_SOC_DAPM_MIXER("AIF1ADC1L Mixer", SND_SOC_NOPM, 0, 0,
1709 aif1adc1l_mix, ARRAY_SIZE(aif1adc1l_mix)),
1710SND_SOC_DAPM_MIXER("AIF1ADC1R Mixer", SND_SOC_NOPM, 0, 0,
1711 aif1adc1r_mix, ARRAY_SIZE(aif1adc1r_mix)),
1712
Mark Browna3257ba2010-07-19 14:02:34 +01001713SND_SOC_DAPM_MIXER("AIF1ADC2L Mixer", SND_SOC_NOPM, 0, 0,
1714 aif1adc2l_mix, ARRAY_SIZE(aif1adc2l_mix)),
1715SND_SOC_DAPM_MIXER("AIF1ADC2R Mixer", SND_SOC_NOPM, 0, 0,
1716 aif1adc2r_mix, ARRAY_SIZE(aif1adc2r_mix)),
1717
Mark Brown9e6e96a2010-01-29 17:47:12 +00001718SND_SOC_DAPM_MIXER("AIF2DAC2L Mixer", SND_SOC_NOPM, 0, 0,
1719 aif2dac2l_mix, ARRAY_SIZE(aif2dac2l_mix)),
1720SND_SOC_DAPM_MIXER("AIF2DAC2R Mixer", SND_SOC_NOPM, 0, 0,
1721 aif2dac2r_mix, ARRAY_SIZE(aif2dac2r_mix)),
1722
1723SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &sidetone1_mux),
1724SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &sidetone2_mux),
1725
1726SND_SOC_DAPM_MIXER("DAC1L Mixer", SND_SOC_NOPM, 0, 0,
1727 dac1l_mix, ARRAY_SIZE(dac1l_mix)),
1728SND_SOC_DAPM_MIXER("DAC1R Mixer", SND_SOC_NOPM, 0, 0,
1729 dac1r_mix, ARRAY_SIZE(dac1r_mix)),
1730
1731SND_SOC_DAPM_AIF_OUT("AIF2ADCL", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001732 SND_SOC_NOPM, 13, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001733SND_SOC_DAPM_AIF_OUT("AIF2ADCR", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001734 SND_SOC_NOPM, 12, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001735SND_SOC_DAPM_AIF_IN_E("AIF2DACL", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001736 SND_SOC_NOPM, 13, 0, wm8958_aif_ev,
Mark Brownd6addcc2010-11-26 15:21:08 +00001737 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1738SND_SOC_DAPM_AIF_IN_E("AIF2DACR", NULL, 0,
Mark Brown1a383362012-04-12 19:47:11 +01001739 SND_SOC_NOPM, 12, 0, wm8958_aif_ev,
Mark Brownd6addcc2010-11-26 15:21:08 +00001740 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001741
Mark Brown5567d8c2012-02-16 21:43:29 -08001742SND_SOC_DAPM_AIF_IN("AIF1DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1743SND_SOC_DAPM_AIF_IN("AIF2DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1744SND_SOC_DAPM_AIF_OUT("AIF1ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1745SND_SOC_DAPM_AIF_OUT("AIF2ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001746
1747SND_SOC_DAPM_MUX("AIF1DAC Mux", SND_SOC_NOPM, 0, 0, &aif1dac_mux),
1748SND_SOC_DAPM_MUX("AIF2DAC Mux", SND_SOC_NOPM, 0, 0, &aif2dac_mux),
1749SND_SOC_DAPM_MUX("AIF2ADC Mux", SND_SOC_NOPM, 0, 0, &aif2adc_mux),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001750
Mark Brown5567d8c2012-02-16 21:43:29 -08001751SND_SOC_DAPM_AIF_IN("AIF3DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
1752SND_SOC_DAPM_AIF_OUT("AIF3ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001753
1754SND_SOC_DAPM_SUPPLY("TOCLK", WM8994_CLOCKING_1, 4, 0, NULL, 0),
1755
1756SND_SOC_DAPM_ADC("DMIC2L", NULL, WM8994_POWER_MANAGEMENT_4, 5, 0),
1757SND_SOC_DAPM_ADC("DMIC2R", NULL, WM8994_POWER_MANAGEMENT_4, 4, 0),
1758SND_SOC_DAPM_ADC("DMIC1L", NULL, WM8994_POWER_MANAGEMENT_4, 3, 0),
1759SND_SOC_DAPM_ADC("DMIC1R", NULL, WM8994_POWER_MANAGEMENT_4, 2, 0),
1760
1761/* Power is done with the muxes since the ADC power also controls the
1762 * downsampling chain, the chip will automatically manage the analogue
1763 * specific portions.
1764 */
1765SND_SOC_DAPM_ADC("ADCL", NULL, SND_SOC_NOPM, 1, 0),
1766SND_SOC_DAPM_ADC("ADCR", NULL, SND_SOC_NOPM, 0, 0),
1767
Mark Brown50941962013-05-30 13:42:26 +01001768SND_SOC_DAPM_MUX("AIF1 Loopback", SND_SOC_NOPM, 0, 0, &aif1_loopback),
1769SND_SOC_DAPM_MUX("AIF2 Loopback", SND_SOC_NOPM, 0, 0, &aif2_loopback),
1770
Mark Brown9e6e96a2010-01-29 17:47:12 +00001771SND_SOC_DAPM_POST("Debug log", post_ev),
1772};
1773
Mark Brownc4431df2010-11-26 15:21:07 +00001774static const struct snd_soc_dapm_widget wm8994_specific_dapm_widgets[] = {
1775SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8994_aif3adc_mux),
1776};
Mark Brown9e6e96a2010-01-29 17:47:12 +00001777
Mark Brownc4431df2010-11-26 15:21:07 +00001778static const struct snd_soc_dapm_widget wm8958_dapm_widgets[] = {
Mark Brown8c5b8422012-04-17 20:49:05 +01001779SND_SOC_DAPM_SUPPLY("AIF3", WM8994_POWER_MANAGEMENT_6, 5, 1, NULL, 0),
Mark Brownc4431df2010-11-26 15:21:07 +00001780SND_SOC_DAPM_MUX("Mono PCM Out Mux", SND_SOC_NOPM, 0, 0, &mono_pcm_out_mux),
1781SND_SOC_DAPM_MUX("AIF2DACL Mux", SND_SOC_NOPM, 0, 0, &aif2dacl_src_mux),
1782SND_SOC_DAPM_MUX("AIF2DACR Mux", SND_SOC_NOPM, 0, 0, &aif2dacr_src_mux),
1783SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8958_aif3adc_mux),
1784};
1785
1786static const struct snd_soc_dapm_route intercon[] = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00001787 { "CLK_SYS", NULL, "AIF1CLK", check_clk_sys },
1788 { "CLK_SYS", NULL, "AIF2CLK", check_clk_sys },
1789
1790 { "DSP1CLK", NULL, "CLK_SYS" },
1791 { "DSP2CLK", NULL, "CLK_SYS" },
1792 { "DSPINTCLK", NULL, "CLK_SYS" },
1793
1794 { "AIF1ADC1L", NULL, "AIF1CLK" },
1795 { "AIF1ADC1L", NULL, "DSP1CLK" },
1796 { "AIF1ADC1R", NULL, "AIF1CLK" },
1797 { "AIF1ADC1R", NULL, "DSP1CLK" },
1798 { "AIF1ADC1R", NULL, "DSPINTCLK" },
1799
1800 { "AIF1DAC1L", NULL, "AIF1CLK" },
1801 { "AIF1DAC1L", NULL, "DSP1CLK" },
1802 { "AIF1DAC1R", NULL, "AIF1CLK" },
1803 { "AIF1DAC1R", NULL, "DSP1CLK" },
1804 { "AIF1DAC1R", NULL, "DSPINTCLK" },
1805
1806 { "AIF1ADC2L", NULL, "AIF1CLK" },
1807 { "AIF1ADC2L", NULL, "DSP1CLK" },
1808 { "AIF1ADC2R", NULL, "AIF1CLK" },
1809 { "AIF1ADC2R", NULL, "DSP1CLK" },
1810 { "AIF1ADC2R", NULL, "DSPINTCLK" },
1811
1812 { "AIF1DAC2L", NULL, "AIF1CLK" },
1813 { "AIF1DAC2L", NULL, "DSP1CLK" },
1814 { "AIF1DAC2R", NULL, "AIF1CLK" },
1815 { "AIF1DAC2R", NULL, "DSP1CLK" },
1816 { "AIF1DAC2R", NULL, "DSPINTCLK" },
1817
1818 { "AIF2ADCL", NULL, "AIF2CLK" },
1819 { "AIF2ADCL", NULL, "DSP2CLK" },
1820 { "AIF2ADCR", NULL, "AIF2CLK" },
1821 { "AIF2ADCR", NULL, "DSP2CLK" },
1822 { "AIF2ADCR", NULL, "DSPINTCLK" },
1823
1824 { "AIF2DACL", NULL, "AIF2CLK" },
1825 { "AIF2DACL", NULL, "DSP2CLK" },
1826 { "AIF2DACR", NULL, "AIF2CLK" },
1827 { "AIF2DACR", NULL, "DSP2CLK" },
1828 { "AIF2DACR", NULL, "DSPINTCLK" },
1829
1830 { "DMIC1L", NULL, "DMIC1DAT" },
1831 { "DMIC1L", NULL, "CLK_SYS" },
1832 { "DMIC1R", NULL, "DMIC1DAT" },
1833 { "DMIC1R", NULL, "CLK_SYS" },
1834 { "DMIC2L", NULL, "DMIC2DAT" },
1835 { "DMIC2L", NULL, "CLK_SYS" },
1836 { "DMIC2R", NULL, "DMIC2DAT" },
1837 { "DMIC2R", NULL, "CLK_SYS" },
1838
1839 { "ADCL", NULL, "AIF1CLK" },
1840 { "ADCL", NULL, "DSP1CLK" },
1841 { "ADCL", NULL, "DSPINTCLK" },
1842
1843 { "ADCR", NULL, "AIF1CLK" },
1844 { "ADCR", NULL, "DSP1CLK" },
1845 { "ADCR", NULL, "DSPINTCLK" },
1846
1847 { "ADCL Mux", "ADC", "ADCL" },
1848 { "ADCL Mux", "DMIC", "DMIC1L" },
1849 { "ADCR Mux", "ADC", "ADCR" },
1850 { "ADCR Mux", "DMIC", "DMIC1R" },
1851
1852 { "DAC1L", NULL, "AIF1CLK" },
1853 { "DAC1L", NULL, "DSP1CLK" },
1854 { "DAC1L", NULL, "DSPINTCLK" },
1855
1856 { "DAC1R", NULL, "AIF1CLK" },
1857 { "DAC1R", NULL, "DSP1CLK" },
1858 { "DAC1R", NULL, "DSPINTCLK" },
1859
1860 { "DAC2L", NULL, "AIF2CLK" },
1861 { "DAC2L", NULL, "DSP2CLK" },
1862 { "DAC2L", NULL, "DSPINTCLK" },
1863
1864 { "DAC2R", NULL, "AIF2DACR" },
1865 { "DAC2R", NULL, "AIF2CLK" },
1866 { "DAC2R", NULL, "DSP2CLK" },
1867 { "DAC2R", NULL, "DSPINTCLK" },
1868
1869 { "TOCLK", NULL, "CLK_SYS" },
1870
Mark Brown5567d8c2012-02-16 21:43:29 -08001871 { "AIF1DACDAT", NULL, "AIF1 Playback" },
1872 { "AIF2DACDAT", NULL, "AIF2 Playback" },
1873 { "AIF3DACDAT", NULL, "AIF3 Playback" },
1874
1875 { "AIF1 Capture", NULL, "AIF1ADCDAT" },
1876 { "AIF2 Capture", NULL, "AIF2ADCDAT" },
1877 { "AIF3 Capture", NULL, "AIF3ADCDAT" },
1878
Mark Brown9e6e96a2010-01-29 17:47:12 +00001879 /* AIF1 outputs */
1880 { "AIF1ADC1L", NULL, "AIF1ADC1L Mixer" },
1881 { "AIF1ADC1L Mixer", "ADC/DMIC Switch", "ADCL Mux" },
1882 { "AIF1ADC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1883
1884 { "AIF1ADC1R", NULL, "AIF1ADC1R Mixer" },
1885 { "AIF1ADC1R Mixer", "ADC/DMIC Switch", "ADCR Mux" },
1886 { "AIF1ADC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1887
Mark Browna3257ba2010-07-19 14:02:34 +01001888 { "AIF1ADC2L", NULL, "AIF1ADC2L Mixer" },
1889 { "AIF1ADC2L Mixer", "DMIC Switch", "DMIC2L" },
1890 { "AIF1ADC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1891
1892 { "AIF1ADC2R", NULL, "AIF1ADC2R Mixer" },
1893 { "AIF1ADC2R Mixer", "DMIC Switch", "DMIC2R" },
1894 { "AIF1ADC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1895
Mark Brown9e6e96a2010-01-29 17:47:12 +00001896 /* Pin level routing for AIF3 */
1897 { "AIF1DAC1L", NULL, "AIF1DAC Mux" },
1898 { "AIF1DAC1R", NULL, "AIF1DAC Mux" },
1899 { "AIF1DAC2L", NULL, "AIF1DAC Mux" },
1900 { "AIF1DAC2R", NULL, "AIF1DAC Mux" },
1901
Mark Brown50941962013-05-30 13:42:26 +01001902 { "AIF1DAC Mux", "AIF1DACDAT", "AIF1 Loopback" },
Mark Brown9e6e96a2010-01-29 17:47:12 +00001903 { "AIF1DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
Mark Brown50941962013-05-30 13:42:26 +01001904 { "AIF2DAC Mux", "AIF2DACDAT", "AIF2 Loopback" },
Mark Brown9e6e96a2010-01-29 17:47:12 +00001905 { "AIF2DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1906 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCL" },
1907 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCR" },
1908 { "AIF2ADC Mux", "AIF3DACDAT", "AIF3ADCDAT" },
1909
1910 /* DAC1 inputs */
Mark Brown9e6e96a2010-01-29 17:47:12 +00001911 { "DAC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1912 { "DAC1L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1913 { "DAC1L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1914 { "DAC1L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1915 { "DAC1L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1916
Mark Brown9e6e96a2010-01-29 17:47:12 +00001917 { "DAC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1918 { "DAC1R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1919 { "DAC1R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1920 { "DAC1R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1921 { "DAC1R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1922
1923 /* DAC2/AIF2 outputs */
1924 { "AIF2ADCL", NULL, "AIF2DAC2L Mixer" },
Mark Brown9e6e96a2010-01-29 17:47:12 +00001925 { "AIF2DAC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1926 { "AIF2DAC2L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1927 { "AIF2DAC2L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1928 { "AIF2DAC2L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1929 { "AIF2DAC2L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1930
1931 { "AIF2ADCR", NULL, "AIF2DAC2R Mixer" },
Mark Brown9e6e96a2010-01-29 17:47:12 +00001932 { "AIF2DAC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1933 { "AIF2DAC2R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1934 { "AIF2DAC2R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1935 { "AIF2DAC2R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1936 { "AIF2DAC2R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1937
Mark Brown7f94de42011-02-03 16:27:34 +00001938 { "AIF1ADCDAT", NULL, "AIF1ADC1L" },
1939 { "AIF1ADCDAT", NULL, "AIF1ADC1R" },
1940 { "AIF1ADCDAT", NULL, "AIF1ADC2L" },
1941 { "AIF1ADCDAT", NULL, "AIF1ADC2R" },
1942
Mark Brown9e6e96a2010-01-29 17:47:12 +00001943 { "AIF2ADCDAT", NULL, "AIF2ADC Mux" },
1944
1945 /* AIF3 output */
Zidan Wange134cb2042015-08-14 11:22:16 +08001946 { "AIF3ADC Mux", "AIF1ADCDAT", "AIF1ADC1L" },
1947 { "AIF3ADC Mux", "AIF1ADCDAT", "AIF1ADC1R" },
1948 { "AIF3ADC Mux", "AIF1ADCDAT", "AIF1ADC2L" },
1949 { "AIF3ADC Mux", "AIF1ADCDAT", "AIF1ADC2R" },
1950 { "AIF3ADC Mux", "AIF2ADCDAT", "AIF2ADCL" },
1951 { "AIF3ADC Mux", "AIF2ADCDAT", "AIF2ADCR" },
1952 { "AIF3ADC Mux", "AIF2DACDAT", "AIF2DACL" },
1953 { "AIF3ADC Mux", "AIF2DACDAT", "AIF2DACR" },
1954
1955 { "AIF3ADCDAT", NULL, "AIF3ADC Mux" },
Mark Brown9e6e96a2010-01-29 17:47:12 +00001956
Mark Brown50941962013-05-30 13:42:26 +01001957 /* Loopback */
1958 { "AIF1 Loopback", "ADCDAT", "AIF1ADCDAT" },
1959 { "AIF1 Loopback", "None", "AIF1DACDAT" },
1960 { "AIF2 Loopback", "ADCDAT", "AIF2ADCDAT" },
1961 { "AIF2 Loopback", "None", "AIF2DACDAT" },
1962
Mark Brown9e6e96a2010-01-29 17:47:12 +00001963 /* Sidetone */
1964 { "Left Sidetone", "ADC/DMIC1", "ADCL Mux" },
1965 { "Left Sidetone", "DMIC2", "DMIC2L" },
1966 { "Right Sidetone", "ADC/DMIC1", "ADCR Mux" },
1967 { "Right Sidetone", "DMIC2", "DMIC2R" },
1968
1969 /* Output stages */
1970 { "Left Output Mixer", "DAC Switch", "DAC1L" },
1971 { "Right Output Mixer", "DAC Switch", "DAC1R" },
1972
1973 { "SPKL", "DAC1 Switch", "DAC1L" },
1974 { "SPKL", "DAC2 Switch", "DAC2L" },
1975
1976 { "SPKR", "DAC1 Switch", "DAC1R" },
1977 { "SPKR", "DAC2 Switch", "DAC2R" },
1978
1979 { "Left Headphone Mux", "DAC", "DAC1L" },
1980 { "Right Headphone Mux", "DAC", "DAC1R" },
1981};
1982
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001983static const struct snd_soc_dapm_route wm8994_lateclk_revd_intercon[] = {
1984 { "DAC1L", NULL, "Late DAC1L Enable PGA" },
1985 { "Late DAC1L Enable PGA", NULL, "DAC1L Mixer" },
1986 { "DAC1R", NULL, "Late DAC1R Enable PGA" },
1987 { "Late DAC1R Enable PGA", NULL, "DAC1R Mixer" },
1988 { "DAC2L", NULL, "Late DAC2L Enable PGA" },
1989 { "Late DAC2L Enable PGA", NULL, "AIF2DAC2L Mixer" },
1990 { "DAC2R", NULL, "Late DAC2R Enable PGA" },
1991 { "Late DAC2R Enable PGA", NULL, "AIF2DAC2R Mixer" }
1992};
1993
1994static const struct snd_soc_dapm_route wm8994_lateclk_intercon[] = {
1995 { "DAC1L", NULL, "DAC1L Mixer" },
1996 { "DAC1R", NULL, "DAC1R Mixer" },
1997 { "DAC2L", NULL, "AIF2DAC2L Mixer" },
1998 { "DAC2R", NULL, "AIF2DAC2R Mixer" },
1999};
2000
Mark Brown6ed8f142011-02-03 16:27:35 +00002001static const struct snd_soc_dapm_route wm8994_revd_intercon[] = {
2002 { "AIF1DACDAT", NULL, "AIF2DACDAT" },
2003 { "AIF2DACDAT", NULL, "AIF1DACDAT" },
2004 { "AIF1ADCDAT", NULL, "AIF2ADCDAT" },
2005 { "AIF2ADCDAT", NULL, "AIF1ADCDAT" },
Mark Brownb793eb62011-07-14 18:21:37 +09002006 { "MICBIAS1", NULL, "CLK_SYS" },
2007 { "MICBIAS1", NULL, "MICBIAS Supply" },
2008 { "MICBIAS2", NULL, "CLK_SYS" },
2009 { "MICBIAS2", NULL, "MICBIAS Supply" },
Mark Brown6ed8f142011-02-03 16:27:35 +00002010};
2011
Mark Brownc4431df2010-11-26 15:21:07 +00002012static const struct snd_soc_dapm_route wm8994_intercon[] = {
2013 { "AIF2DACL", NULL, "AIF2DAC Mux" },
2014 { "AIF2DACR", NULL, "AIF2DAC Mux" },
Mark Brown4e04ada2011-07-15 15:12:31 +09002015 { "MICBIAS1", NULL, "VMID" },
2016 { "MICBIAS2", NULL, "VMID" },
Mark Brownc4431df2010-11-26 15:21:07 +00002017};
2018
2019static const struct snd_soc_dapm_route wm8958_intercon[] = {
2020 { "AIF2DACL", NULL, "AIF2DACL Mux" },
2021 { "AIF2DACR", NULL, "AIF2DACR Mux" },
2022
2023 { "AIF2DACL Mux", "AIF2", "AIF2DAC Mux" },
2024 { "AIF2DACL Mux", "AIF3", "AIF3DACDAT" },
2025 { "AIF2DACR Mux", "AIF2", "AIF2DAC Mux" },
2026 { "AIF2DACR Mux", "AIF3", "AIF3DACDAT" },
2027
Mark Brown8c5b8422012-04-17 20:49:05 +01002028 { "AIF3DACDAT", NULL, "AIF3" },
2029 { "AIF3ADCDAT", NULL, "AIF3" },
2030
Mark Brownc4431df2010-11-26 15:21:07 +00002031 { "Mono PCM Out Mux", "AIF2ADCL", "AIF2ADCL" },
2032 { "Mono PCM Out Mux", "AIF2ADCR", "AIF2ADCR" },
2033
2034 { "AIF3ADC Mux", "Mono PCM", "Mono PCM Out Mux" },
2035};
2036
Mark Brown9e6e96a2010-01-29 17:47:12 +00002037/* The size in bits of the FLL divide multiplied by 10
2038 * to allow rounding later */
2039#define FIXED_FLL_SIZE ((1 << 16) * 10)
2040
2041struct fll_div {
2042 u16 outdiv;
2043 u16 n;
2044 u16 k;
Mark Brownd1a0a292013-05-10 21:40:10 +01002045 u16 lambda;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002046 u16 clk_ref_div;
2047 u16 fll_fratio;
2048};
2049
Mark Brownd1a0a292013-05-10 21:40:10 +01002050static int wm8994_get_fll_config(struct wm8994 *control, struct fll_div *fll,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002051 int freq_in, int freq_out)
2052{
2053 u64 Kpart;
Mark Brownd1a0a292013-05-10 21:40:10 +01002054 unsigned int K, Ndiv, Nmod, gcd_fll;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002055
2056 pr_debug("FLL input=%dHz, output=%dHz\n", freq_in, freq_out);
2057
2058 /* Scale the input frequency down to <= 13.5MHz */
2059 fll->clk_ref_div = 0;
2060 while (freq_in > 13500000) {
2061 fll->clk_ref_div++;
2062 freq_in /= 2;
2063
2064 if (fll->clk_ref_div > 3)
2065 return -EINVAL;
2066 }
2067 pr_debug("CLK_REF_DIV=%d, Fref=%dHz\n", fll->clk_ref_div, freq_in);
2068
2069 /* Scale the output to give 90MHz<=Fvco<=100MHz */
2070 fll->outdiv = 3;
2071 while (freq_out * (fll->outdiv + 1) < 90000000) {
2072 fll->outdiv++;
2073 if (fll->outdiv > 63)
2074 return -EINVAL;
2075 }
2076 freq_out *= fll->outdiv + 1;
2077 pr_debug("OUTDIV=%d, Fvco=%dHz\n", fll->outdiv, freq_out);
2078
2079 if (freq_in > 1000000) {
2080 fll->fll_fratio = 0;
Mark Brown7d48a6a2010-04-20 13:36:11 +09002081 } else if (freq_in > 256000) {
2082 fll->fll_fratio = 1;
2083 freq_in *= 2;
2084 } else if (freq_in > 128000) {
2085 fll->fll_fratio = 2;
2086 freq_in *= 4;
2087 } else if (freq_in > 64000) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002088 fll->fll_fratio = 3;
2089 freq_in *= 8;
Mark Brown7d48a6a2010-04-20 13:36:11 +09002090 } else {
2091 fll->fll_fratio = 4;
2092 freq_in *= 16;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002093 }
2094 pr_debug("FLL_FRATIO=%d, Fref=%dHz\n", fll->fll_fratio, freq_in);
2095
2096 /* Now, calculate N.K */
2097 Ndiv = freq_out / freq_in;
2098
2099 fll->n = Ndiv;
2100 Nmod = freq_out % freq_in;
2101 pr_debug("Nmod=%d\n", Nmod);
2102
Mark Brownd1a0a292013-05-10 21:40:10 +01002103 switch (control->type) {
2104 case WM8994:
2105 /* Calculate fractional part - scale up so we can round. */
2106 Kpart = FIXED_FLL_SIZE * (long long)Nmod;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002107
Mark Brownd1a0a292013-05-10 21:40:10 +01002108 do_div(Kpart, freq_in);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002109
Mark Brownd1a0a292013-05-10 21:40:10 +01002110 K = Kpart & 0xFFFFFFFF;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002111
Mark Brownd1a0a292013-05-10 21:40:10 +01002112 if ((K % 10) >= 5)
2113 K += 5;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002114
Mark Brownd1a0a292013-05-10 21:40:10 +01002115 /* Move down to proper range now rounding is done */
2116 fll->k = K / 10;
Mark Brownf7dbd392013-05-28 12:52:09 +01002117 fll->lambda = 0;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002118
Mark Brownd1a0a292013-05-10 21:40:10 +01002119 pr_debug("N=%x K=%x\n", fll->n, fll->k);
Dan Carpenter571ab6c2013-05-15 10:09:43 +03002120 break;
Mark Brownd1a0a292013-05-10 21:40:10 +01002121
2122 default:
2123 gcd_fll = gcd(freq_out, freq_in);
2124
2125 fll->k = (freq_out - (freq_in * fll->n)) / gcd_fll;
2126 fll->lambda = freq_in / gcd_fll;
2127
2128 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002129
2130 return 0;
2131}
2132
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002133static int _wm8994_set_fll(struct snd_soc_codec *codec, int id, int src,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002134 unsigned int freq_in, unsigned int freq_out)
2135{
Mark Brownb2c812e2010-04-14 15:35:19 +09002136 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01002137 struct wm8994 *control = wm8994->wm8994;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002138 int reg_offset, ret;
2139 struct fll_div fll;
Mark Browne413ba82012-03-29 14:49:27 +01002140 u16 reg, clk1, aif_reg, aif_src;
Mark Brownc7ebf932011-07-12 19:47:59 +09002141 unsigned long timeout;
Mark Brown4b7ed832011-08-10 17:47:33 +09002142 bool was_enabled;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002143
Mark Brown9e6e96a2010-01-29 17:47:12 +00002144 switch (id) {
2145 case WM8994_FLL1:
2146 reg_offset = 0;
2147 id = 0;
Mark Browne413ba82012-03-29 14:49:27 +01002148 aif_src = 0x10;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002149 break;
2150 case WM8994_FLL2:
2151 reg_offset = 0x20;
2152 id = 1;
Mark Browne413ba82012-03-29 14:49:27 +01002153 aif_src = 0x18;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002154 break;
2155 default:
2156 return -EINVAL;
2157 }
2158
Mark Brown4b7ed832011-08-10 17:47:33 +09002159 reg = snd_soc_read(codec, WM8994_FLL1_CONTROL_1 + reg_offset);
2160 was_enabled = reg & WM8994_FLL1_ENA;
2161
Mark Brown136ff2a2010-04-20 12:56:18 +09002162 switch (src) {
Mark Brown7add84a2010-04-22 02:29:01 +09002163 case 0:
2164 /* Allow no source specification when stopping */
2165 if (freq_out)
2166 return -EINVAL;
Mark Brown4514e892010-12-03 16:02:10 +00002167 src = wm8994->fll[id].src;
Mark Brown7add84a2010-04-22 02:29:01 +09002168 break;
Mark Brown136ff2a2010-04-20 12:56:18 +09002169 case WM8994_FLL_SRC_MCLK1:
2170 case WM8994_FLL_SRC_MCLK2:
2171 case WM8994_FLL_SRC_LRCLK:
2172 case WM8994_FLL_SRC_BCLK:
2173 break;
Mark Brownfbfe6982012-07-23 20:14:43 +01002174 case WM8994_FLL_SRC_INTERNAL:
2175 freq_in = 12000000;
2176 freq_out = 12000000;
2177 break;
Mark Brown136ff2a2010-04-20 12:56:18 +09002178 default:
2179 return -EINVAL;
2180 }
2181
Mark Brown9e6e96a2010-01-29 17:47:12 +00002182 /* Are we changing anything? */
2183 if (wm8994->fll[id].src == src &&
2184 wm8994->fll[id].in == freq_in && wm8994->fll[id].out == freq_out)
2185 return 0;
2186
2187 /* If we're stopping the FLL redo the old config - no
2188 * registers will actually be written but we avoid GCC flow
2189 * analysis bugs spewing warnings.
2190 */
2191 if (freq_out)
Mark Brownd1a0a292013-05-10 21:40:10 +01002192 ret = wm8994_get_fll_config(control, &fll, freq_in, freq_out);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002193 else
Mark Brownd1a0a292013-05-10 21:40:10 +01002194 ret = wm8994_get_fll_config(control, &fll, wm8994->fll[id].in,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002195 wm8994->fll[id].out);
2196 if (ret < 0)
2197 return ret;
2198
Mark Browne413ba82012-03-29 14:49:27 +01002199 /* Make sure that we're not providing SYSCLK right now */
2200 clk1 = snd_soc_read(codec, WM8994_CLOCKING_1);
2201 if (clk1 & WM8994_SYSCLK_SRC)
2202 aif_reg = WM8994_AIF2_CLOCKING_1;
2203 else
2204 aif_reg = WM8994_AIF1_CLOCKING_1;
2205 reg = snd_soc_read(codec, aif_reg);
2206
2207 if ((reg & WM8994_AIF1CLK_ENA) &&
2208 (reg & WM8994_AIF1CLK_SRC_MASK) == aif_src) {
2209 dev_err(codec->dev, "FLL%d is currently providing SYSCLK\n",
2210 id + 1);
2211 return -EBUSY;
2212 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002213
2214 /* We always need to disable the FLL while reconfiguring */
2215 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
2216 WM8994_FLL1_ENA, 0);
2217
Mark Brown20dc24a2012-04-05 12:55:20 +01002218 if (wm8994->fll_byp && src == WM8994_FLL_SRC_BCLK &&
Kyung-Kwee Ryue05854d2012-04-24 18:01:48 +01002219 freq_in == freq_out && freq_out) {
Mark Brown20dc24a2012-04-05 12:55:20 +01002220 dev_dbg(codec->dev, "Bypassing FLL%d\n", id + 1);
2221 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_5 + reg_offset,
2222 WM8958_FLL1_BYP, WM8958_FLL1_BYP);
2223 goto out;
2224 }
2225
Mark Brown9e6e96a2010-01-29 17:47:12 +00002226 reg = (fll.outdiv << WM8994_FLL1_OUTDIV_SHIFT) |
2227 (fll.fll_fratio << WM8994_FLL1_FRATIO_SHIFT);
2228 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_2 + reg_offset,
2229 WM8994_FLL1_OUTDIV_MASK |
2230 WM8994_FLL1_FRATIO_MASK, reg);
2231
Mark Brownb16db742012-03-03 15:33:23 +00002232 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_3 + reg_offset,
2233 WM8994_FLL1_K_MASK, fll.k);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002234
2235 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_4 + reg_offset,
2236 WM8994_FLL1_N_MASK,
Mark Brown7435d4e2012-07-26 14:49:11 +01002237 fll.n << WM8994_FLL1_N_SHIFT);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002238
Mark Brownd1a0a292013-05-10 21:40:10 +01002239 if (fll.lambda) {
2240 snd_soc_update_bits(codec, WM8958_FLL1_EFS_1 + reg_offset,
2241 WM8958_FLL1_LAMBDA_MASK,
2242 fll.lambda);
2243 snd_soc_update_bits(codec, WM8958_FLL1_EFS_2 + reg_offset,
2244 WM8958_FLL1_EFS_ENA, WM8958_FLL1_EFS_ENA);
2245 } else {
2246 snd_soc_update_bits(codec, WM8958_FLL1_EFS_2 + reg_offset,
2247 WM8958_FLL1_EFS_ENA, 0);
2248 }
2249
Mark Brown9e6e96a2010-01-29 17:47:12 +00002250 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_5 + reg_offset,
Mark Brownfbfe6982012-07-23 20:14:43 +01002251 WM8994_FLL1_FRC_NCO | WM8958_FLL1_BYP |
Mark Brown136ff2a2010-04-20 12:56:18 +09002252 WM8994_FLL1_REFCLK_DIV_MASK |
2253 WM8994_FLL1_REFCLK_SRC_MASK,
Mark Brownfbfe6982012-07-23 20:14:43 +01002254 ((src == WM8994_FLL_SRC_INTERNAL)
2255 << WM8994_FLL1_FRC_NCO_SHIFT) |
Mark Brown136ff2a2010-04-20 12:56:18 +09002256 (fll.clk_ref_div << WM8994_FLL1_REFCLK_DIV_SHIFT) |
2257 (src - 1));
Mark Brown9e6e96a2010-01-29 17:47:12 +00002258
Mark Brownf0f50392011-07-16 03:12:18 +09002259 /* Clear any pending completion from a previous failure */
2260 try_wait_for_completion(&wm8994->fll_locked[id]);
2261
Mark Brown9e6e96a2010-01-29 17:47:12 +00002262 /* Enable (with fractional mode if required) */
2263 if (freq_out) {
Mark Brown4b7ed832011-08-10 17:47:33 +09002264 /* Enable VMID if we need it */
2265 if (!was_enabled) {
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002266 active_reference(codec);
2267
Mark Brown4b7ed832011-08-10 17:47:33 +09002268 switch (control->type) {
2269 case WM8994:
2270 vmid_reference(codec);
2271 break;
2272 case WM8958:
Mark Brownda445afe2013-03-12 17:46:09 +00002273 if (control->revision < 1)
Mark Brown4b7ed832011-08-10 17:47:33 +09002274 vmid_reference(codec);
2275 break;
2276 default:
2277 break;
2278 }
2279 }
2280
Mark Brownfbfe6982012-07-23 20:14:43 +01002281 reg = WM8994_FLL1_ENA;
2282
Mark Brown9e6e96a2010-01-29 17:47:12 +00002283 if (fll.k)
Mark Brownfbfe6982012-07-23 20:14:43 +01002284 reg |= WM8994_FLL1_FRAC;
2285 if (src == WM8994_FLL_SRC_INTERNAL)
2286 reg |= WM8994_FLL1_OSC_ENA;
2287
Mark Brown9e6e96a2010-01-29 17:47:12 +00002288 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
Mark Brownfbfe6982012-07-23 20:14:43 +01002289 WM8994_FLL1_ENA | WM8994_FLL1_OSC_ENA |
2290 WM8994_FLL1_FRAC, reg);
Mark Brown8e9ddf82011-07-01 17:24:46 -07002291
Mark Brownc7ebf932011-07-12 19:47:59 +09002292 if (wm8994->fll_locked_irq) {
2293 timeout = wait_for_completion_timeout(&wm8994->fll_locked[id],
2294 msecs_to_jiffies(10));
2295 if (timeout == 0)
2296 dev_warn(codec->dev,
2297 "Timed out waiting for FLL lock\n");
2298 } else {
2299 msleep(5);
2300 }
Mark Brown4b7ed832011-08-10 17:47:33 +09002301 } else {
2302 if (was_enabled) {
2303 switch (control->type) {
2304 case WM8994:
2305 vmid_dereference(codec);
2306 break;
2307 case WM8958:
Mark Brownda445afe2013-03-12 17:46:09 +00002308 if (control->revision < 1)
Mark Brown4b7ed832011-08-10 17:47:33 +09002309 vmid_dereference(codec);
2310 break;
2311 default:
2312 break;
2313 }
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002314
2315 active_dereference(codec);
Mark Brown4b7ed832011-08-10 17:47:33 +09002316 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002317 }
2318
Mark Brown20dc24a2012-04-05 12:55:20 +01002319out:
Mark Brown9e6e96a2010-01-29 17:47:12 +00002320 wm8994->fll[id].in = freq_in;
2321 wm8994->fll[id].out = freq_out;
Mark Brown136ff2a2010-04-20 12:56:18 +09002322 wm8994->fll[id].src = src;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002323
Mark Brown9e6e96a2010-01-29 17:47:12 +00002324 configure_clock(codec);
2325
Mark Browncd220002012-10-24 10:56:30 +01002326 /*
2327 * If SYSCLK will be less than 50kHz adjust AIFnCLK dividers
2328 * for detection.
2329 */
2330 if (max(wm8994->aifclk[0], wm8994->aifclk[1]) < 50000) {
2331 dev_dbg(codec->dev, "Configuring AIFs for 128fs\n");
Mark Brownd3725762013-01-29 23:17:12 +08002332
2333 wm8994->aifdiv[0] = snd_soc_read(codec, WM8994_AIF1_RATE)
2334 & WM8994_AIF1CLK_RATE_MASK;
2335 wm8994->aifdiv[1] = snd_soc_read(codec, WM8994_AIF2_RATE)
2336 & WM8994_AIF1CLK_RATE_MASK;
2337
Mark Browncd220002012-10-24 10:56:30 +01002338 snd_soc_update_bits(codec, WM8994_AIF1_RATE,
2339 WM8994_AIF1CLK_RATE_MASK, 0x1);
2340 snd_soc_update_bits(codec, WM8994_AIF2_RATE,
2341 WM8994_AIF2CLK_RATE_MASK, 0x1);
Mark Brownd3725762013-01-29 23:17:12 +08002342 } else if (wm8994->aifdiv[0]) {
2343 snd_soc_update_bits(codec, WM8994_AIF1_RATE,
2344 WM8994_AIF1CLK_RATE_MASK,
2345 wm8994->aifdiv[0]);
2346 snd_soc_update_bits(codec, WM8994_AIF2_RATE,
2347 WM8994_AIF2CLK_RATE_MASK,
2348 wm8994->aifdiv[1]);
2349
2350 wm8994->aifdiv[0] = 0;
2351 wm8994->aifdiv[1] = 0;
Mark Browncd220002012-10-24 10:56:30 +01002352 }
2353
Mark Brown9e6e96a2010-01-29 17:47:12 +00002354 return 0;
2355}
2356
Mark Brownc7ebf932011-07-12 19:47:59 +09002357static irqreturn_t wm8994_fll_locked_irq(int irq, void *data)
2358{
2359 struct completion *completion = data;
2360
2361 complete(completion);
2362
2363 return IRQ_HANDLED;
2364}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002365
Mark Brown66b47fd2010-07-08 11:25:43 +09002366static int opclk_divs[] = { 10, 20, 30, 40, 55, 60, 80, 120, 160 };
2367
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002368static int wm8994_set_fll(struct snd_soc_dai *dai, int id, int src,
2369 unsigned int freq_in, unsigned int freq_out)
2370{
2371 return _wm8994_set_fll(dai->codec, id, src, freq_in, freq_out);
2372}
2373
Mark Brown9e6e96a2010-01-29 17:47:12 +00002374static int wm8994_set_dai_sysclk(struct snd_soc_dai *dai,
2375 int clk_id, unsigned int freq, int dir)
2376{
2377 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09002378 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown66b47fd2010-07-08 11:25:43 +09002379 int i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002380
2381 switch (dai->id) {
2382 case 1:
2383 case 2:
2384 break;
2385
2386 default:
2387 /* AIF3 shares clocking with AIF1/2 */
2388 return -EINVAL;
2389 }
2390
2391 switch (clk_id) {
2392 case WM8994_SYSCLK_MCLK1:
2393 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK1;
2394 wm8994->mclk[0] = freq;
2395 dev_dbg(dai->dev, "AIF%d using MCLK1 at %uHz\n",
2396 dai->id, freq);
2397 break;
2398
2399 case WM8994_SYSCLK_MCLK2:
2400 /* TODO: Set GPIO AF */
2401 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK2;
2402 wm8994->mclk[1] = freq;
2403 dev_dbg(dai->dev, "AIF%d using MCLK2 at %uHz\n",
2404 dai->id, freq);
2405 break;
2406
2407 case WM8994_SYSCLK_FLL1:
2408 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL1;
2409 dev_dbg(dai->dev, "AIF%d using FLL1\n", dai->id);
2410 break;
2411
2412 case WM8994_SYSCLK_FLL2:
2413 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL2;
2414 dev_dbg(dai->dev, "AIF%d using FLL2\n", dai->id);
2415 break;
2416
Mark Brown66b47fd2010-07-08 11:25:43 +09002417 case WM8994_SYSCLK_OPCLK:
2418 /* Special case - a division (times 10) is given and
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02002419 * no effect on main clocking.
Mark Brown66b47fd2010-07-08 11:25:43 +09002420 */
2421 if (freq) {
2422 for (i = 0; i < ARRAY_SIZE(opclk_divs); i++)
2423 if (opclk_divs[i] == freq)
2424 break;
2425 if (i == ARRAY_SIZE(opclk_divs))
2426 return -EINVAL;
2427 snd_soc_update_bits(codec, WM8994_CLOCKING_2,
2428 WM8994_OPCLK_DIV_MASK, i);
2429 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
2430 WM8994_OPCLK_ENA, WM8994_OPCLK_ENA);
2431 } else {
2432 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
2433 WM8994_OPCLK_ENA, 0);
2434 }
2435
Mark Brown9e6e96a2010-01-29 17:47:12 +00002436 default:
2437 return -EINVAL;
2438 }
2439
2440 configure_clock(codec);
2441
Mark Brown67300492012-10-24 10:56:30 +01002442 /*
2443 * If SYSCLK will be less than 50kHz adjust AIFnCLK dividers
2444 * for detection.
2445 */
2446 if (max(wm8994->aifclk[0], wm8994->aifclk[1]) < 50000) {
2447 dev_dbg(codec->dev, "Configuring AIFs for 128fs\n");
Mark Brownd3725762013-01-29 23:17:12 +08002448
2449 wm8994->aifdiv[0] = snd_soc_read(codec, WM8994_AIF1_RATE)
2450 & WM8994_AIF1CLK_RATE_MASK;
2451 wm8994->aifdiv[1] = snd_soc_read(codec, WM8994_AIF2_RATE)
2452 & WM8994_AIF1CLK_RATE_MASK;
2453
Mark Brown67300492012-10-24 10:56:30 +01002454 snd_soc_update_bits(codec, WM8994_AIF1_RATE,
2455 WM8994_AIF1CLK_RATE_MASK, 0x1);
2456 snd_soc_update_bits(codec, WM8994_AIF2_RATE,
2457 WM8994_AIF2CLK_RATE_MASK, 0x1);
Mark Brownd3725762013-01-29 23:17:12 +08002458 } else if (wm8994->aifdiv[0]) {
2459 snd_soc_update_bits(codec, WM8994_AIF1_RATE,
2460 WM8994_AIF1CLK_RATE_MASK,
2461 wm8994->aifdiv[0]);
2462 snd_soc_update_bits(codec, WM8994_AIF2_RATE,
2463 WM8994_AIF2CLK_RATE_MASK,
2464 wm8994->aifdiv[1]);
2465
2466 wm8994->aifdiv[0] = 0;
2467 wm8994->aifdiv[1] = 0;
Mark Brown67300492012-10-24 10:56:30 +01002468 }
2469
Mark Brown9e6e96a2010-01-29 17:47:12 +00002470 return 0;
2471}
2472
2473static int wm8994_set_bias_level(struct snd_soc_codec *codec,
2474 enum snd_soc_bias_level level)
2475{
Mark Brownb6b05692010-08-13 12:58:20 +01002476 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01002477 struct wm8994 *control = wm8994->wm8994;
Mark Brownb6b05692010-08-13 12:58:20 +01002478
Mark Brown5f2f3892012-02-08 18:51:42 +00002479 wm_hubs_set_bias_level(codec, level);
2480
Mark Brown9e6e96a2010-01-29 17:47:12 +00002481 switch (level) {
2482 case SND_SOC_BIAS_ON:
2483 break;
2484
2485 case SND_SOC_BIAS_PREPARE:
Mark Brown500fa302011-11-29 19:58:19 +00002486 /* MICBIAS into regulating mode */
2487 switch (control->type) {
2488 case WM8958:
2489 case WM1811:
2490 snd_soc_update_bits(codec, WM8958_MICBIAS1,
2491 WM8958_MICB1_MODE, 0);
2492 snd_soc_update_bits(codec, WM8958_MICBIAS2,
2493 WM8958_MICB2_MODE, 0);
2494 break;
2495 default:
2496 break;
2497 }
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002498
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02002499 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_STANDBY)
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002500 active_reference(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002501 break;
2502
2503 case SND_SOC_BIAS_STANDBY:
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02002504 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
Mark Brown8bc3c2c2010-11-30 14:56:18 +00002505 switch (control->type) {
Mark Brown8bc3c2c2010-11-30 14:56:18 +00002506 case WM8958:
Mark Brownda445afe2013-03-12 17:46:09 +00002507 if (control->revision == 0) {
Mark Brown8bc3c2c2010-11-30 14:56:18 +00002508 /* Optimise performance for rev A */
Mark Brown8bc3c2c2010-11-30 14:56:18 +00002509 snd_soc_update_bits(codec,
2510 WM8958_CHARGE_PUMP_2,
2511 WM8958_CP_DISCH,
2512 WM8958_CP_DISCH);
2513 }
2514 break;
Mark Brown81204c82011-05-24 17:35:53 +08002515
Mark Brown462835e2012-01-21 12:11:53 +00002516 default:
Mark Brown81204c82011-05-24 17:35:53 +08002517 break;
Mark Brownb6b05692010-08-13 12:58:20 +01002518 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002519
2520 /* Discharge LINEOUT1 & 2 */
2521 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
2522 WM8994_LINEOUT1_DISCH |
2523 WM8994_LINEOUT2_DISCH,
2524 WM8994_LINEOUT1_DISCH |
2525 WM8994_LINEOUT2_DISCH);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002526 }
2527
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02002528 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_PREPARE)
Mark Brownaf6b6fe2011-11-30 20:32:05 +00002529 active_dereference(codec);
2530
Mark Brown500fa302011-11-29 19:58:19 +00002531 /* MICBIAS into bypass mode on newer devices */
2532 switch (control->type) {
2533 case WM8958:
2534 case WM1811:
2535 snd_soc_update_bits(codec, WM8958_MICBIAS1,
2536 WM8958_MICB1_MODE,
2537 WM8958_MICB1_MODE);
2538 snd_soc_update_bits(codec, WM8958_MICBIAS2,
2539 WM8958_MICB2_MODE,
2540 WM8958_MICB2_MODE);
2541 break;
2542 default:
2543 break;
2544 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002545 break;
2546
2547 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02002548 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_STANDBY)
Mark Brownfbbf5922011-03-11 18:09:04 +00002549 wm8994->cur_fw = NULL;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002550 break;
2551 }
Mark Brown5f2f3892012-02-08 18:51:42 +00002552
Mark Brown9e6e96a2010-01-29 17:47:12 +00002553 return 0;
2554}
2555
Mark Brown22f8d052012-03-19 17:32:06 +00002556int wm8994_vmid_mode(struct snd_soc_codec *codec, enum wm8994_vmid_mode mode)
2557{
2558 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02002559 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
Mark Brown22f8d052012-03-19 17:32:06 +00002560
2561 switch (mode) {
2562 case WM8994_VMID_NORMAL:
Charles Keepaxbabce822014-02-18 15:22:19 +00002563 snd_soc_dapm_mutex_lock(dapm);
2564
Mark Brown22f8d052012-03-19 17:32:06 +00002565 if (wm8994->hubs.lineout1_se) {
Charles Keepaxbabce822014-02-18 15:22:19 +00002566 snd_soc_dapm_disable_pin_unlocked(dapm,
2567 "LINEOUT1N Driver");
2568 snd_soc_dapm_disable_pin_unlocked(dapm,
2569 "LINEOUT1P Driver");
Mark Brown22f8d052012-03-19 17:32:06 +00002570 }
2571 if (wm8994->hubs.lineout2_se) {
Charles Keepaxbabce822014-02-18 15:22:19 +00002572 snd_soc_dapm_disable_pin_unlocked(dapm,
2573 "LINEOUT2N Driver");
2574 snd_soc_dapm_disable_pin_unlocked(dapm,
2575 "LINEOUT2P Driver");
Mark Brown22f8d052012-03-19 17:32:06 +00002576 }
2577
2578 /* Do the sync with the old mode to allow it to clean up */
Charles Keepaxbabce822014-02-18 15:22:19 +00002579 snd_soc_dapm_sync_unlocked(dapm);
Mark Brown22f8d052012-03-19 17:32:06 +00002580 wm8994->vmid_mode = mode;
Charles Keepaxbabce822014-02-18 15:22:19 +00002581
2582 snd_soc_dapm_mutex_unlock(dapm);
Mark Brown22f8d052012-03-19 17:32:06 +00002583 break;
2584
2585 case WM8994_VMID_FORCE:
Charles Keepaxbabce822014-02-18 15:22:19 +00002586 snd_soc_dapm_mutex_lock(dapm);
2587
Mark Brown22f8d052012-03-19 17:32:06 +00002588 if (wm8994->hubs.lineout1_se) {
Charles Keepaxbabce822014-02-18 15:22:19 +00002589 snd_soc_dapm_force_enable_pin_unlocked(dapm,
2590 "LINEOUT1N Driver");
2591 snd_soc_dapm_force_enable_pin_unlocked(dapm,
2592 "LINEOUT1P Driver");
Mark Brown22f8d052012-03-19 17:32:06 +00002593 }
2594 if (wm8994->hubs.lineout2_se) {
Charles Keepaxbabce822014-02-18 15:22:19 +00002595 snd_soc_dapm_force_enable_pin_unlocked(dapm,
2596 "LINEOUT2N Driver");
2597 snd_soc_dapm_force_enable_pin_unlocked(dapm,
2598 "LINEOUT2P Driver");
Mark Brown22f8d052012-03-19 17:32:06 +00002599 }
2600
2601 wm8994->vmid_mode = mode;
Charles Keepaxbabce822014-02-18 15:22:19 +00002602 snd_soc_dapm_sync_unlocked(dapm);
2603
2604 snd_soc_dapm_mutex_unlock(dapm);
Mark Brown22f8d052012-03-19 17:32:06 +00002605 break;
2606
2607 default:
2608 return -EINVAL;
2609 }
2610
2611 return 0;
2612}
2613
Mark Brown9e6e96a2010-01-29 17:47:12 +00002614static int wm8994_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2615{
2616 struct snd_soc_codec *codec = dai->codec;
Mark Brown2a8a8562011-07-24 12:20:41 +01002617 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2618 struct wm8994 *control = wm8994->wm8994;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002619 int ms_reg;
2620 int aif1_reg;
Mark Brown435705e2013-05-20 11:16:10 -05002621 int dac_reg;
2622 int adc_reg;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002623 int ms = 0;
2624 int aif1 = 0;
Mark Brown435705e2013-05-20 11:16:10 -05002625 int lrclk = 0;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002626
2627 switch (dai->id) {
2628 case 1:
2629 ms_reg = WM8994_AIF1_MASTER_SLAVE;
2630 aif1_reg = WM8994_AIF1_CONTROL_1;
Mark Brown435705e2013-05-20 11:16:10 -05002631 dac_reg = WM8994_AIF1DAC_LRCLK;
2632 adc_reg = WM8994_AIF1ADC_LRCLK;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002633 break;
2634 case 2:
2635 ms_reg = WM8994_AIF2_MASTER_SLAVE;
2636 aif1_reg = WM8994_AIF2_CONTROL_1;
Mark Brown435705e2013-05-20 11:16:10 -05002637 dac_reg = WM8994_AIF1DAC_LRCLK;
2638 adc_reg = WM8994_AIF1ADC_LRCLK;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002639 break;
2640 default:
2641 return -EINVAL;
2642 }
2643
2644 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
2645 case SND_SOC_DAIFMT_CBS_CFS:
2646 break;
2647 case SND_SOC_DAIFMT_CBM_CFM:
2648 ms = WM8994_AIF1_MSTR;
2649 break;
2650 default:
2651 return -EINVAL;
2652 }
2653
2654 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2655 case SND_SOC_DAIFMT_DSP_B:
2656 aif1 |= WM8994_AIF1_LRCLK_INV;
Mark Brown435705e2013-05-20 11:16:10 -05002657 lrclk |= WM8958_AIF1_LRCLK_INV;
Gustavo A. R. Silva9d943052017-11-08 14:04:43 -06002658 /* fall through */
Mark Brown9e6e96a2010-01-29 17:47:12 +00002659 case SND_SOC_DAIFMT_DSP_A:
2660 aif1 |= 0x18;
2661 break;
2662 case SND_SOC_DAIFMT_I2S:
2663 aif1 |= 0x10;
2664 break;
2665 case SND_SOC_DAIFMT_RIGHT_J:
2666 break;
2667 case SND_SOC_DAIFMT_LEFT_J:
2668 aif1 |= 0x8;
2669 break;
2670 default:
2671 return -EINVAL;
2672 }
2673
2674 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2675 case SND_SOC_DAIFMT_DSP_A:
2676 case SND_SOC_DAIFMT_DSP_B:
2677 /* frame inversion not valid for DSP modes */
2678 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2679 case SND_SOC_DAIFMT_NB_NF:
2680 break;
2681 case SND_SOC_DAIFMT_IB_NF:
2682 aif1 |= WM8994_AIF1_BCLK_INV;
2683 break;
2684 default:
2685 return -EINVAL;
2686 }
2687 break;
2688
2689 case SND_SOC_DAIFMT_I2S:
2690 case SND_SOC_DAIFMT_RIGHT_J:
2691 case SND_SOC_DAIFMT_LEFT_J:
2692 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2693 case SND_SOC_DAIFMT_NB_NF:
2694 break;
2695 case SND_SOC_DAIFMT_IB_IF:
2696 aif1 |= WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV;
Mark Brown435705e2013-05-20 11:16:10 -05002697 lrclk |= WM8958_AIF1_LRCLK_INV;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002698 break;
2699 case SND_SOC_DAIFMT_IB_NF:
2700 aif1 |= WM8994_AIF1_BCLK_INV;
2701 break;
2702 case SND_SOC_DAIFMT_NB_IF:
2703 aif1 |= WM8994_AIF1_LRCLK_INV;
Mark Brown435705e2013-05-20 11:16:10 -05002704 lrclk |= WM8958_AIF1_LRCLK_INV;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002705 break;
2706 default:
2707 return -EINVAL;
2708 }
2709 break;
2710 default:
2711 return -EINVAL;
2712 }
2713
Mark Brownc4431df2010-11-26 15:21:07 +00002714 /* The AIF2 format configuration needs to be mirrored to AIF3
2715 * on WM8958 if it's in use so just do it all the time. */
Mark Brown81204c82011-05-24 17:35:53 +08002716 switch (control->type) {
2717 case WM1811:
2718 case WM8958:
2719 if (dai->id == 2)
2720 snd_soc_update_bits(codec, WM8958_AIF3_CONTROL_1,
2721 WM8994_AIF1_LRCLK_INV |
2722 WM8958_AIF3_FMT_MASK, aif1);
2723 break;
2724
2725 default:
2726 break;
2727 }
Mark Brownc4431df2010-11-26 15:21:07 +00002728
Mark Brown9e6e96a2010-01-29 17:47:12 +00002729 snd_soc_update_bits(codec, aif1_reg,
2730 WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV |
2731 WM8994_AIF1_FMT_MASK,
2732 aif1);
2733 snd_soc_update_bits(codec, ms_reg, WM8994_AIF1_MSTR,
2734 ms);
Mark Brown435705e2013-05-20 11:16:10 -05002735 snd_soc_update_bits(codec, dac_reg,
2736 WM8958_AIF1_LRCLK_INV, lrclk);
2737 snd_soc_update_bits(codec, adc_reg,
2738 WM8958_AIF1_LRCLK_INV, lrclk);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002739
2740 return 0;
2741}
2742
2743static struct {
2744 int val, rate;
2745} srs[] = {
2746 { 0, 8000 },
2747 { 1, 11025 },
2748 { 2, 12000 },
2749 { 3, 16000 },
2750 { 4, 22050 },
2751 { 5, 24000 },
2752 { 6, 32000 },
2753 { 7, 44100 },
2754 { 8, 48000 },
2755 { 9, 88200 },
2756 { 10, 96000 },
2757};
2758
2759static int fs_ratios[] = {
Zidan Wang17fc2e02015-05-12 14:58:50 +08002760 64, 128, 192, 256, 384, 512, 768, 1024, 1408, 1536
Mark Brown9e6e96a2010-01-29 17:47:12 +00002761};
2762
2763static int bclk_divs[] = {
2764 10, 15, 20, 30, 40, 50, 60, 80, 110, 120, 160, 220, 240, 320, 440, 480,
2765 640, 880, 960, 1280, 1760, 1920
2766};
2767
2768static int wm8994_hw_params(struct snd_pcm_substream *substream,
2769 struct snd_pcm_hw_params *params,
2770 struct snd_soc_dai *dai)
2771{
2772 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09002773 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown3cf956e2013-03-20 10:12:10 +01002774 struct wm8994 *control = wm8994->wm8994;
2775 struct wm8994_pdata *pdata = &control->pdata;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002776 int aif1_reg;
Mark Brownb1e43d92010-12-07 17:14:56 +00002777 int aif2_reg;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002778 int bclk_reg;
2779 int lrclk_reg;
2780 int rate_reg;
2781 int aif1 = 0;
Mark Brownb1e43d92010-12-07 17:14:56 +00002782 int aif2 = 0;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002783 int bclk = 0;
2784 int lrclk = 0;
2785 int rate_val = 0;
2786 int id = dai->id - 1;
2787
2788 int i, cur_val, best_val, bclk_rate, best;
2789
2790 switch (dai->id) {
2791 case 1:
2792 aif1_reg = WM8994_AIF1_CONTROL_1;
Mark Brownb1e43d92010-12-07 17:14:56 +00002793 aif2_reg = WM8994_AIF1_CONTROL_2;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002794 bclk_reg = WM8994_AIF1_BCLK;
2795 rate_reg = WM8994_AIF1_RATE;
2796 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
Mark Brown7d83d212010-08-23 10:54:43 +01002797 wm8994->lrclk_shared[0]) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002798 lrclk_reg = WM8994_AIF1DAC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002799 } else {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002800 lrclk_reg = WM8994_AIF1ADC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002801 dev_dbg(codec->dev, "AIF1 using split LRCLK\n");
2802 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002803 break;
2804 case 2:
2805 aif1_reg = WM8994_AIF2_CONTROL_1;
Mark Brownb1e43d92010-12-07 17:14:56 +00002806 aif2_reg = WM8994_AIF2_CONTROL_2;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002807 bclk_reg = WM8994_AIF2_BCLK;
2808 rate_reg = WM8994_AIF2_RATE;
2809 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
Mark Brown7d83d212010-08-23 10:54:43 +01002810 wm8994->lrclk_shared[1]) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002811 lrclk_reg = WM8994_AIF2DAC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002812 } else {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002813 lrclk_reg = WM8994_AIF2ADC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002814 dev_dbg(codec->dev, "AIF2 using split LRCLK\n");
2815 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002816 break;
2817 default:
2818 return -EINVAL;
2819 }
2820
Mark Brown79748cd2012-10-01 15:28:30 +01002821 bclk_rate = params_rate(params);
Mark Browne29fe492014-07-31 12:54:58 +01002822 switch (params_width(params)) {
2823 case 16:
Mark Brown9e6e96a2010-01-29 17:47:12 +00002824 bclk_rate *= 16;
2825 break;
Mark Browne29fe492014-07-31 12:54:58 +01002826 case 20:
Mark Brown9e6e96a2010-01-29 17:47:12 +00002827 bclk_rate *= 20;
2828 aif1 |= 0x20;
2829 break;
Mark Browne29fe492014-07-31 12:54:58 +01002830 case 24:
Mark Brown9e6e96a2010-01-29 17:47:12 +00002831 bclk_rate *= 24;
2832 aif1 |= 0x40;
2833 break;
Mark Browne29fe492014-07-31 12:54:58 +01002834 case 32:
Mark Brown9e6e96a2010-01-29 17:47:12 +00002835 bclk_rate *= 32;
2836 aif1 |= 0x60;
2837 break;
2838 default:
2839 return -EINVAL;
2840 }
2841
Mark Brown79748cd2012-10-01 15:28:30 +01002842 wm8994->channels[id] = params_channels(params);
Mark Brown3cf956e2013-03-20 10:12:10 +01002843 if (pdata->max_channels_clocked[id] &&
2844 wm8994->channels[id] > pdata->max_channels_clocked[id]) {
2845 dev_dbg(dai->dev, "Constraining channels to %d from %d\n",
2846 pdata->max_channels_clocked[id], wm8994->channels[id]);
2847 wm8994->channels[id] = pdata->max_channels_clocked[id];
2848 }
2849
2850 switch (wm8994->channels[id]) {
Mark Brown79748cd2012-10-01 15:28:30 +01002851 case 1:
2852 case 2:
2853 bclk_rate *= 2;
2854 break;
2855 default:
2856 bclk_rate *= 4;
2857 break;
2858 }
2859
Mark Brown9e6e96a2010-01-29 17:47:12 +00002860 /* Try to find an appropriate sample rate; look for an exact match. */
2861 for (i = 0; i < ARRAY_SIZE(srs); i++)
2862 if (srs[i].rate == params_rate(params))
2863 break;
2864 if (i == ARRAY_SIZE(srs))
2865 return -EINVAL;
2866 rate_val |= srs[i].val << WM8994_AIF1_SR_SHIFT;
2867
2868 dev_dbg(dai->dev, "Sample rate is %dHz\n", srs[i].rate);
2869 dev_dbg(dai->dev, "AIF%dCLK is %dHz, target BCLK %dHz\n",
2870 dai->id, wm8994->aifclk[id], bclk_rate);
2871
Mark Brown3cf956e2013-03-20 10:12:10 +01002872 if (wm8994->channels[id] == 1 &&
Mark Brownb1e43d92010-12-07 17:14:56 +00002873 (snd_soc_read(codec, aif1_reg) & 0x18) == 0x18)
2874 aif2 |= WM8994_AIF1_MONO;
2875
Mark Brown9e6e96a2010-01-29 17:47:12 +00002876 if (wm8994->aifclk[id] == 0) {
2877 dev_err(dai->dev, "AIF%dCLK not configured\n", dai->id);
2878 return -EINVAL;
2879 }
2880
2881 /* AIFCLK/fs ratio; look for a close match in either direction */
2882 best = 0;
2883 best_val = abs((fs_ratios[0] * params_rate(params))
2884 - wm8994->aifclk[id]);
2885 for (i = 1; i < ARRAY_SIZE(fs_ratios); i++) {
2886 cur_val = abs((fs_ratios[i] * params_rate(params))
2887 - wm8994->aifclk[id]);
2888 if (cur_val >= best_val)
2889 continue;
2890 best = i;
2891 best_val = cur_val;
2892 }
2893 dev_dbg(dai->dev, "Selected AIF%dCLK/fs = %d\n",
2894 dai->id, fs_ratios[best]);
2895 rate_val |= best;
2896
2897 /* We may not get quite the right frequency if using
2898 * approximate clocks so look for the closest match that is
2899 * higher than the target (we need to ensure that there enough
2900 * BCLKs to clock out the samples).
2901 */
2902 best = 0;
2903 for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
Joonyoung Shim07cd8ad2010-02-02 18:53:19 +09002904 cur_val = (wm8994->aifclk[id] * 10 / bclk_divs[i]) - bclk_rate;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002905 if (cur_val < 0) /* BCLK table is sorted */
2906 break;
2907 best = i;
2908 }
Joonyoung Shim07cd8ad2010-02-02 18:53:19 +09002909 bclk_rate = wm8994->aifclk[id] * 10 / bclk_divs[best];
Mark Brown9e6e96a2010-01-29 17:47:12 +00002910 dev_dbg(dai->dev, "Using BCLK_DIV %d for actual BCLK %dHz\n",
2911 bclk_divs[best], bclk_rate);
2912 bclk |= best << WM8994_AIF1_BCLK_DIV_SHIFT;
2913
2914 lrclk = bclk_rate / params_rate(params);
Mark Brownfc07ecd2011-11-28 21:16:56 +00002915 if (!lrclk) {
2916 dev_err(dai->dev, "Unable to generate LRCLK from %dHz BCLK\n",
2917 bclk_rate);
2918 return -EINVAL;
2919 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002920 dev_dbg(dai->dev, "Using LRCLK rate %d for actual LRCLK %dHz\n",
2921 lrclk, bclk_rate / lrclk);
2922
2923 snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
Mark Brownb1e43d92010-12-07 17:14:56 +00002924 snd_soc_update_bits(codec, aif2_reg, WM8994_AIF1_MONO, aif2);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002925 snd_soc_update_bits(codec, bclk_reg, WM8994_AIF1_BCLK_DIV_MASK, bclk);
2926 snd_soc_update_bits(codec, lrclk_reg, WM8994_AIF1DAC_RATE_MASK,
2927 lrclk);
2928 snd_soc_update_bits(codec, rate_reg, WM8994_AIF1_SR_MASK |
2929 WM8994_AIF1CLK_RATE_MASK, rate_val);
2930
2931 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
2932 switch (dai->id) {
2933 case 1:
2934 wm8994->dac_rates[0] = params_rate(params);
2935 wm8994_set_retune_mobile(codec, 0);
2936 wm8994_set_retune_mobile(codec, 1);
2937 break;
2938 case 2:
2939 wm8994->dac_rates[1] = params_rate(params);
2940 wm8994_set_retune_mobile(codec, 2);
2941 break;
2942 }
2943 }
2944
2945 return 0;
2946}
2947
Mark Brownc4431df2010-11-26 15:21:07 +00002948static int wm8994_aif3_hw_params(struct snd_pcm_substream *substream,
2949 struct snd_pcm_hw_params *params,
2950 struct snd_soc_dai *dai)
2951{
2952 struct snd_soc_codec *codec = dai->codec;
Mark Brown2a8a8562011-07-24 12:20:41 +01002953 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2954 struct wm8994 *control = wm8994->wm8994;
Mark Brownc4431df2010-11-26 15:21:07 +00002955 int aif1_reg;
2956 int aif1 = 0;
2957
2958 switch (dai->id) {
2959 case 3:
2960 switch (control->type) {
Mark Brown81204c82011-05-24 17:35:53 +08002961 case WM1811:
Mark Brownc4431df2010-11-26 15:21:07 +00002962 case WM8958:
2963 aif1_reg = WM8958_AIF3_CONTROL_1;
2964 break;
2965 default:
2966 return 0;
2967 }
Dan Carpenter4495e46f2013-04-30 10:24:41 +03002968 break;
Mark Brownc4431df2010-11-26 15:21:07 +00002969 default:
2970 return 0;
2971 }
2972
Mark Browne29fe492014-07-31 12:54:58 +01002973 switch (params_width(params)) {
2974 case 16:
Mark Brownc4431df2010-11-26 15:21:07 +00002975 break;
Mark Browne29fe492014-07-31 12:54:58 +01002976 case 20:
Mark Brownc4431df2010-11-26 15:21:07 +00002977 aif1 |= 0x20;
2978 break;
Mark Browne29fe492014-07-31 12:54:58 +01002979 case 24:
Mark Brownc4431df2010-11-26 15:21:07 +00002980 aif1 |= 0x40;
2981 break;
Mark Browne29fe492014-07-31 12:54:58 +01002982 case 32:
Mark Brownc4431df2010-11-26 15:21:07 +00002983 aif1 |= 0x60;
2984 break;
2985 default:
2986 return -EINVAL;
2987 }
2988
2989 return snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
2990}
2991
Mark Brown9e6e96a2010-01-29 17:47:12 +00002992static int wm8994_aif_mute(struct snd_soc_dai *codec_dai, int mute)
2993{
2994 struct snd_soc_codec *codec = codec_dai->codec;
2995 int mute_reg;
2996 int reg;
2997
2998 switch (codec_dai->id) {
2999 case 1:
3000 mute_reg = WM8994_AIF1_DAC1_FILTERS_1;
3001 break;
3002 case 2:
3003 mute_reg = WM8994_AIF2_DAC_FILTERS_1;
3004 break;
3005 default:
3006 return -EINVAL;
3007 }
3008
3009 if (mute)
3010 reg = WM8994_AIF1DAC1_MUTE;
3011 else
3012 reg = 0;
3013
3014 snd_soc_update_bits(codec, mute_reg, WM8994_AIF1DAC1_MUTE, reg);
3015
3016 return 0;
3017}
3018
Mark Brown778a76e2010-03-22 22:05:10 +00003019static int wm8994_set_tristate(struct snd_soc_dai *codec_dai, int tristate)
3020{
3021 struct snd_soc_codec *codec = codec_dai->codec;
3022 int reg, val, mask;
3023
3024 switch (codec_dai->id) {
3025 case 1:
3026 reg = WM8994_AIF1_MASTER_SLAVE;
3027 mask = WM8994_AIF1_TRI;
3028 break;
3029 case 2:
3030 reg = WM8994_AIF2_MASTER_SLAVE;
3031 mask = WM8994_AIF2_TRI;
3032 break;
Mark Brown778a76e2010-03-22 22:05:10 +00003033 default:
3034 return -EINVAL;
3035 }
3036
3037 if (tristate)
3038 val = mask;
3039 else
3040 val = 0;
3041
Qiao Zhou78b3fb42011-01-19 19:10:47 +08003042 return snd_soc_update_bits(codec, reg, mask, val);
Mark Brown778a76e2010-03-22 22:05:10 +00003043}
3044
Mark Brownd09f3ec2011-08-15 11:01:02 +09003045static int wm8994_aif2_probe(struct snd_soc_dai *dai)
3046{
3047 struct snd_soc_codec *codec = dai->codec;
3048
3049 /* Disable the pulls on the AIF if we're using it to save power. */
3050 snd_soc_update_bits(codec, WM8994_GPIO_3,
3051 WM8994_GPN_PU | WM8994_GPN_PD, 0);
3052 snd_soc_update_bits(codec, WM8994_GPIO_4,
3053 WM8994_GPN_PU | WM8994_GPN_PD, 0);
3054 snd_soc_update_bits(codec, WM8994_GPIO_5,
3055 WM8994_GPN_PU | WM8994_GPN_PD, 0);
3056
3057 return 0;
3058}
3059
Mark Brown9e6e96a2010-01-29 17:47:12 +00003060#define WM8994_RATES SNDRV_PCM_RATE_8000_96000
3061
3062#define WM8994_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
Ian Lartey3079aed2010-08-31 23:56:34 +01003063 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003064
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01003065static const struct snd_soc_dai_ops wm8994_aif1_dai_ops = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00003066 .set_sysclk = wm8994_set_dai_sysclk,
3067 .set_fmt = wm8994_set_dai_fmt,
3068 .hw_params = wm8994_hw_params,
3069 .digital_mute = wm8994_aif_mute,
3070 .set_pll = wm8994_set_fll,
Mark Brown778a76e2010-03-22 22:05:10 +00003071 .set_tristate = wm8994_set_tristate,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003072};
3073
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01003074static const struct snd_soc_dai_ops wm8994_aif2_dai_ops = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00003075 .set_sysclk = wm8994_set_dai_sysclk,
3076 .set_fmt = wm8994_set_dai_fmt,
3077 .hw_params = wm8994_hw_params,
3078 .digital_mute = wm8994_aif_mute,
3079 .set_pll = wm8994_set_fll,
Mark Brown778a76e2010-03-22 22:05:10 +00003080 .set_tristate = wm8994_set_tristate,
3081};
3082
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01003083static const struct snd_soc_dai_ops wm8994_aif3_dai_ops = {
Mark Brownc4431df2010-11-26 15:21:07 +00003084 .hw_params = wm8994_aif3_hw_params,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003085};
3086
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003087static struct snd_soc_dai_driver wm8994_dai[] = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00003088 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003089 .name = "wm8994-aif1",
Mark Brown8c7f78b2010-10-12 15:56:09 +01003090 .id = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003091 .playback = {
3092 .stream_name = "AIF1 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00003093 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003094 .channels_max = 2,
3095 .rates = WM8994_RATES,
3096 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00003097 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003098 },
3099 .capture = {
3100 .stream_name = "AIF1 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00003101 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003102 .channels_max = 2,
3103 .rates = WM8994_RATES,
3104 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00003105 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003106 },
3107 .ops = &wm8994_aif1_dai_ops,
3108 },
3109 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003110 .name = "wm8994-aif2",
Mark Brown8c7f78b2010-10-12 15:56:09 +01003111 .id = 2,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003112 .playback = {
3113 .stream_name = "AIF2 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00003114 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003115 .channels_max = 2,
3116 .rates = WM8994_RATES,
3117 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00003118 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003119 },
3120 .capture = {
3121 .stream_name = "AIF2 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00003122 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003123 .channels_max = 2,
3124 .rates = WM8994_RATES,
3125 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00003126 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003127 },
Mark Brownd09f3ec2011-08-15 11:01:02 +09003128 .probe = wm8994_aif2_probe,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003129 .ops = &wm8994_aif2_dai_ops,
3130 },
3131 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003132 .name = "wm8994-aif3",
Mark Brown8c7f78b2010-10-12 15:56:09 +01003133 .id = 3,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003134 .playback = {
3135 .stream_name = "AIF3 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00003136 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003137 .channels_max = 2,
3138 .rates = WM8994_RATES,
3139 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00003140 .sig_bits = 24,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003141 },
Dan Carpentera8462bd2010-03-24 14:58:34 +03003142 .capture = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00003143 .stream_name = "AIF3 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00003144 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003145 .channels_max = 2,
3146 .rates = WM8994_RATES,
3147 .formats = WM8994_FORMATS,
Mark Brown99b02922012-01-17 11:50:26 +00003148 .sig_bits = 24,
3149 },
Mark Brown778a76e2010-03-22 22:05:10 +00003150 .ops = &wm8994_aif3_dai_ops,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003151 }
3152};
Mark Brown9e6e96a2010-01-29 17:47:12 +00003153
3154#ifdef CONFIG_PM
Mark Brown4752a882012-03-04 02:16:01 +00003155static int wm8994_codec_suspend(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003156{
Mark Brownb2c812e2010-04-14 15:35:19 +09003157 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003158 int i, ret;
3159
3160 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
3161 memcpy(&wm8994->fll_suspend[i], &wm8994->fll[i],
Mark Brownf701a2e2011-03-09 19:31:01 +00003162 sizeof(struct wm8994_fll_config));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003163 ret = _wm8994_set_fll(codec, i + 1, 0, 0, 0);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003164 if (ret < 0)
3165 dev_warn(codec->dev, "Failed to stop FLL%d: %d\n",
3166 i + 1, ret);
3167 }
3168
Lars-Peter Clausenbd1204c2015-04-27 22:13:24 +02003169 snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_OFF);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003170
3171 return 0;
3172}
3173
Mark Brown4752a882012-03-04 02:16:01 +00003174static int wm8994_codec_resume(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003175{
Mark Brownb2c812e2010-04-14 15:35:19 +09003176 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003177 int i, ret;
3178
Mark Brown9e6e96a2010-01-29 17:47:12 +00003179 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
Mark Brown6a2f1ee2010-05-10 18:36:37 +01003180 if (!wm8994->fll_suspend[i].out)
3181 continue;
3182
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003183 ret = _wm8994_set_fll(codec, i + 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003184 wm8994->fll_suspend[i].src,
3185 wm8994->fll_suspend[i].in,
3186 wm8994->fll_suspend[i].out);
3187 if (ret < 0)
3188 dev_warn(codec->dev, "Failed to restore FLL%d: %d\n",
3189 i + 1, ret);
3190 }
3191
3192 return 0;
3193}
3194#else
Mark Brown4752a882012-03-04 02:16:01 +00003195#define wm8994_codec_suspend NULL
3196#define wm8994_codec_resume NULL
Mark Brown9e6e96a2010-01-29 17:47:12 +00003197#endif
3198
3199static void wm8994_handle_retune_mobile_pdata(struct wm8994_priv *wm8994)
3200{
Mark Brown8cb8e832012-07-25 18:10:03 +01003201 struct snd_soc_codec *codec = wm8994->hubs.codec;
Mark Brownd9dd4ad2012-10-08 18:36:09 +09003202 struct wm8994 *control = wm8994->wm8994;
3203 struct wm8994_pdata *pdata = &control->pdata;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003204 struct snd_kcontrol_new controls[] = {
3205 SOC_ENUM_EXT("AIF1.1 EQ Mode",
3206 wm8994->retune_mobile_enum,
3207 wm8994_get_retune_mobile_enum,
3208 wm8994_put_retune_mobile_enum),
3209 SOC_ENUM_EXT("AIF1.2 EQ Mode",
3210 wm8994->retune_mobile_enum,
3211 wm8994_get_retune_mobile_enum,
3212 wm8994_put_retune_mobile_enum),
3213 SOC_ENUM_EXT("AIF2 EQ Mode",
3214 wm8994->retune_mobile_enum,
3215 wm8994_get_retune_mobile_enum,
3216 wm8994_put_retune_mobile_enum),
3217 };
3218 int ret, i, j;
3219 const char **t;
3220
3221 /* We need an array of texts for the enum API but the number
3222 * of texts is likely to be less than the number of
3223 * configurations due to the sample rate dependency of the
3224 * configurations. */
3225 wm8994->num_retune_mobile_texts = 0;
3226 wm8994->retune_mobile_texts = NULL;
3227 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
3228 for (j = 0; j < wm8994->num_retune_mobile_texts; j++) {
3229 if (strcmp(pdata->retune_mobile_cfgs[i].name,
3230 wm8994->retune_mobile_texts[j]) == 0)
3231 break;
3232 }
3233
3234 if (j != wm8994->num_retune_mobile_texts)
3235 continue;
3236
3237 /* Expand the array... */
3238 t = krealloc(wm8994->retune_mobile_texts,
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02003239 sizeof(char *) *
Mark Brown9e6e96a2010-01-29 17:47:12 +00003240 (wm8994->num_retune_mobile_texts + 1),
3241 GFP_KERNEL);
3242 if (t == NULL)
3243 continue;
3244
3245 /* ...store the new entry... */
Jesper Juhlc1a4ecd2012-04-09 00:40:32 +02003246 t[wm8994->num_retune_mobile_texts] =
Mark Brown9e6e96a2010-01-29 17:47:12 +00003247 pdata->retune_mobile_cfgs[i].name;
3248
3249 /* ...and remember the new version. */
3250 wm8994->num_retune_mobile_texts++;
3251 wm8994->retune_mobile_texts = t;
3252 }
3253
3254 dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
3255 wm8994->num_retune_mobile_texts);
3256
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01003257 wm8994->retune_mobile_enum.items = wm8994->num_retune_mobile_texts;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003258 wm8994->retune_mobile_enum.texts = wm8994->retune_mobile_texts;
3259
Mark Brown8cb8e832012-07-25 18:10:03 +01003260 ret = snd_soc_add_codec_controls(wm8994->hubs.codec, controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003261 ARRAY_SIZE(controls));
3262 if (ret != 0)
Mark Brown8cb8e832012-07-25 18:10:03 +01003263 dev_err(wm8994->hubs.codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003264 "Failed to add ReTune Mobile controls: %d\n", ret);
3265}
3266
3267static void wm8994_handle_pdata(struct wm8994_priv *wm8994)
3268{
Mark Brown8cb8e832012-07-25 18:10:03 +01003269 struct snd_soc_codec *codec = wm8994->hubs.codec;
Mark Brownd9dd4ad2012-10-08 18:36:09 +09003270 struct wm8994 *control = wm8994->wm8994;
3271 struct wm8994_pdata *pdata = &control->pdata;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003272 int ret, i;
3273
3274 if (!pdata)
3275 return;
3276
3277 wm_hubs_handle_analogue_pdata(codec, pdata->lineout1_diff,
3278 pdata->lineout2_diff,
3279 pdata->lineout1fb,
3280 pdata->lineout2fb,
3281 pdata->jd_scthr,
3282 pdata->jd_thr,
Mark Brown02e79472012-08-21 17:54:52 +01003283 pdata->micb1_delay,
3284 pdata->micb2_delay,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003285 pdata->micbias1_lvl,
3286 pdata->micbias2_lvl);
3287
3288 dev_dbg(codec->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
3289
3290 if (pdata->num_drc_cfgs) {
3291 struct snd_kcontrol_new controls[] = {
3292 SOC_ENUM_EXT("AIF1DRC1 Mode", wm8994->drc_enum,
3293 wm8994_get_drc_enum, wm8994_put_drc_enum),
3294 SOC_ENUM_EXT("AIF1DRC2 Mode", wm8994->drc_enum,
3295 wm8994_get_drc_enum, wm8994_put_drc_enum),
3296 SOC_ENUM_EXT("AIF2DRC Mode", wm8994->drc_enum,
3297 wm8994_get_drc_enum, wm8994_put_drc_enum),
3298 };
3299
3300 /* We need an array of texts for the enum API */
Mark Brown8cb8e832012-07-25 18:10:03 +01003301 wm8994->drc_texts = devm_kzalloc(wm8994->hubs.codec->dev,
Mark Brown7270ceb2011-12-01 14:00:19 +00003302 sizeof(char *) * pdata->num_drc_cfgs, GFP_KERNEL);
Sachin Kamat549f66e2014-06-20 15:29:10 +05303303 if (!wm8994->drc_texts)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003304 return;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003305
3306 for (i = 0; i < pdata->num_drc_cfgs; i++)
3307 wm8994->drc_texts[i] = pdata->drc_cfgs[i].name;
3308
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01003309 wm8994->drc_enum.items = pdata->num_drc_cfgs;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003310 wm8994->drc_enum.texts = wm8994->drc_texts;
3311
Mark Brown8cb8e832012-07-25 18:10:03 +01003312 ret = snd_soc_add_codec_controls(wm8994->hubs.codec, controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003313 ARRAY_SIZE(controls));
Mark Brown9e6e96a2010-01-29 17:47:12 +00003314 for (i = 0; i < WM8994_NUM_DRC; i++)
3315 wm8994_set_drc(codec, i);
Mark Brown45a690f2012-08-15 19:20:54 +01003316 } else {
3317 ret = snd_soc_add_codec_controls(wm8994->hubs.codec,
3318 wm8994_drc_controls,
3319 ARRAY_SIZE(wm8994_drc_controls));
Mark Brown9e6e96a2010-01-29 17:47:12 +00003320 }
3321
Mark Brown45a690f2012-08-15 19:20:54 +01003322 if (ret != 0)
3323 dev_err(wm8994->hubs.codec->dev,
3324 "Failed to add DRC mode controls: %d\n", ret);
3325
3326
Mark Brown9e6e96a2010-01-29 17:47:12 +00003327 dev_dbg(codec->dev, "%d ReTune Mobile configurations\n",
3328 pdata->num_retune_mobile_cfgs);
3329
3330 if (pdata->num_retune_mobile_cfgs)
3331 wm8994_handle_retune_mobile_pdata(wm8994);
3332 else
Mark Brown8cb8e832012-07-25 18:10:03 +01003333 snd_soc_add_codec_controls(wm8994->hubs.codec, wm8994_eq_controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00003334 ARRAY_SIZE(wm8994_eq_controls));
Mark Brown48e028e2011-02-21 17:11:59 -08003335
3336 for (i = 0; i < ARRAY_SIZE(pdata->micbias); i++) {
3337 if (pdata->micbias[i]) {
3338 snd_soc_write(codec, WM8958_MICBIAS1 + i,
3339 pdata->micbias[i] & 0xffff);
3340 }
3341 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00003342}
3343
Mark Brown88766982010-03-29 20:57:12 +01003344/**
3345 * wm8994_mic_detect - Enable microphone detection via the WM8994 IRQ
3346 *
3347 * @codec: WM8994 codec
3348 * @jack: jack to report detection events on
3349 * @micbias: microphone bias to detect on
Mark Brown88766982010-03-29 20:57:12 +01003350 *
3351 * Enable microphone detection via IRQ on the WM8994. If GPIOs are
3352 * being used to bring out signals to the processor then only platform
Mark Brown5ab230a2010-09-06 14:59:34 +01003353 * data configuration is needed for WM8994 and processor GPIOs should
Mark Brown88766982010-03-29 20:57:12 +01003354 * be configured using snd_soc_jack_add_gpios() instead.
3355 *
3356 * Configuration of detection levels is available via the micbias1_lvl
3357 * and micbias2_lvl platform data members.
3358 */
3359int wm8994_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
Mark Brown87092e32012-02-06 18:50:39 +00003360 int micbias)
Mark Brown88766982010-03-29 20:57:12 +01003361{
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02003362 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
Mark Brownb2c812e2010-04-14 15:35:19 +09003363 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown88766982010-03-29 20:57:12 +01003364 struct wm8994_micdet *micdet;
Mark Brown2a8a8562011-07-24 12:20:41 +01003365 struct wm8994 *control = wm8994->wm8994;
Mark Brown87092e32012-02-06 18:50:39 +00003366 int reg, ret;
Mark Brown88766982010-03-29 20:57:12 +01003367
Mark Brown87092e32012-02-06 18:50:39 +00003368 if (control->type != WM8994) {
3369 dev_warn(codec->dev, "Not a WM8994\n");
Mark Brown3a423152010-11-26 15:21:06 +00003370 return -EINVAL;
Mark Brown87092e32012-02-06 18:50:39 +00003371 }
Mark Brown3a423152010-11-26 15:21:06 +00003372
Mark Brown88766982010-03-29 20:57:12 +01003373 switch (micbias) {
3374 case 1:
3375 micdet = &wm8994->micdet[0];
Mark Brown87092e32012-02-06 18:50:39 +00003376 if (jack)
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02003377 ret = snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
Mark Brown87092e32012-02-06 18:50:39 +00003378 else
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02003379 ret = snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
Mark Brown88766982010-03-29 20:57:12 +01003380 break;
3381 case 2:
3382 micdet = &wm8994->micdet[1];
Mark Brown87092e32012-02-06 18:50:39 +00003383 if (jack)
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02003384 ret = snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
Mark Brown87092e32012-02-06 18:50:39 +00003385 else
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02003386 ret = snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
Mark Brown88766982010-03-29 20:57:12 +01003387 break;
3388 default:
Mark Brown87092e32012-02-06 18:50:39 +00003389 dev_warn(codec->dev, "Invalid MICBIAS %d\n", micbias);
Mark Brown88766982010-03-29 20:57:12 +01003390 return -EINVAL;
Mark Brown87092e32012-02-06 18:50:39 +00003391 }
Mark Brown88766982010-03-29 20:57:12 +01003392
Mark Brown87092e32012-02-06 18:50:39 +00003393 if (ret != 0)
3394 dev_warn(codec->dev, "Failed to configure MICBIAS%d: %d\n",
3395 micbias, ret);
3396
3397 dev_dbg(codec->dev, "Configuring microphone detection on %d %p\n",
3398 micbias, jack);
Mark Brown88766982010-03-29 20:57:12 +01003399
3400 /* Store the configuration */
3401 micdet->jack = jack;
Mark Brown87092e32012-02-06 18:50:39 +00003402 micdet->detecting = true;
Mark Brown88766982010-03-29 20:57:12 +01003403
3404 /* If either of the jacks is set up then enable detection */
3405 if (wm8994->micdet[0].jack || wm8994->micdet[1].jack)
3406 reg = WM8994_MICD_ENA;
Mark Brown87092e32012-02-06 18:50:39 +00003407 else
Mark Brown88766982010-03-29 20:57:12 +01003408 reg = 0;
3409
3410 snd_soc_update_bits(codec, WM8994_MICBIAS, WM8994_MICD_ENA, reg);
3411
Chris Rattrayd9f34df2012-07-31 14:51:34 +01003412 /* enable MICDET and MICSHRT deboune */
3413 snd_soc_update_bits(codec, WM8994_IRQ_DEBOUNCE,
3414 WM8994_MIC1_DET_DB_MASK | WM8994_MIC1_SHRT_DB_MASK |
3415 WM8994_MIC2_DET_DB_MASK | WM8994_MIC2_SHRT_DB_MASK,
3416 WM8994_MIC1_DET_DB | WM8994_MIC1_SHRT_DB);
3417
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02003418 snd_soc_dapm_sync(dapm);
Mark Brown87092e32012-02-06 18:50:39 +00003419
Mark Brown88766982010-03-29 20:57:12 +01003420 return 0;
3421}
3422EXPORT_SYMBOL_GPL(wm8994_mic_detect);
3423
Mark Browne9b54de42012-05-09 19:20:59 +01003424static void wm8994_mic_work(struct work_struct *work)
Mark Brown88766982010-03-29 20:57:12 +01003425{
Mark Browne9b54de42012-05-09 19:20:59 +01003426 struct wm8994_priv *priv = container_of(work,
3427 struct wm8994_priv,
3428 mic_work.work);
Mark Brownfdfc4f32012-05-09 19:24:39 +01003429 struct regmap *regmap = priv->wm8994->regmap;
3430 struct device *dev = priv->wm8994->dev;
3431 unsigned int reg;
3432 int ret;
Mark Brown88766982010-03-29 20:57:12 +01003433 int report;
3434
Mark Brownb8176622012-07-24 15:48:57 +01003435 pm_runtime_get_sync(dev);
3436
Mark Brownfdfc4f32012-05-09 19:24:39 +01003437 ret = regmap_read(regmap, WM8994_INTERRUPT_RAW_STATUS_2, &reg);
3438 if (ret < 0) {
3439 dev_err(dev, "Failed to read microphone status: %d\n",
3440 ret);
Mark Brownb8176622012-07-24 15:48:57 +01003441 pm_runtime_put(dev);
Mark Browne9b54de42012-05-09 19:20:59 +01003442 return;
Mark Brown88766982010-03-29 20:57:12 +01003443 }
3444
Mark Brownfdfc4f32012-05-09 19:24:39 +01003445 dev_dbg(dev, "Microphone status: %x\n", reg);
Mark Brown88766982010-03-29 20:57:12 +01003446
3447 report = 0;
Mark Brown87092e32012-02-06 18:50:39 +00003448 if (reg & WM8994_MIC1_DET_STS) {
3449 if (priv->micdet[0].detecting)
3450 report = SND_JACK_HEADSET;
3451 }
3452 if (reg & WM8994_MIC1_SHRT_STS) {
3453 if (priv->micdet[0].detecting)
3454 report = SND_JACK_HEADPHONE;
3455 else
3456 report |= SND_JACK_BTN_0;
3457 }
3458 if (report)
3459 priv->micdet[0].detecting = false;
3460 else
3461 priv->micdet[0].detecting = true;
3462
Mark Brown88766982010-03-29 20:57:12 +01003463 snd_soc_jack_report(priv->micdet[0].jack, report,
Mark Brown87092e32012-02-06 18:50:39 +00003464 SND_JACK_HEADSET | SND_JACK_BTN_0);
Mark Brown88766982010-03-29 20:57:12 +01003465
3466 report = 0;
Mark Brown87092e32012-02-06 18:50:39 +00003467 if (reg & WM8994_MIC2_DET_STS) {
3468 if (priv->micdet[1].detecting)
3469 report = SND_JACK_HEADSET;
3470 }
3471 if (reg & WM8994_MIC2_SHRT_STS) {
3472 if (priv->micdet[1].detecting)
3473 report = SND_JACK_HEADPHONE;
3474 else
3475 report |= SND_JACK_BTN_0;
3476 }
3477 if (report)
3478 priv->micdet[1].detecting = false;
3479 else
3480 priv->micdet[1].detecting = true;
3481
Mark Brown88766982010-03-29 20:57:12 +01003482 snd_soc_jack_report(priv->micdet[1].jack, report,
Mark Brown87092e32012-02-06 18:50:39 +00003483 SND_JACK_HEADSET | SND_JACK_BTN_0);
Mark Brownb8176622012-07-24 15:48:57 +01003484
3485 pm_runtime_put(dev);
Mark Browne9b54de42012-05-09 19:20:59 +01003486}
3487
3488static irqreturn_t wm8994_mic_irq(int irq, void *data)
3489{
3490 struct wm8994_priv *priv = data;
Mark Brown8cb8e832012-07-25 18:10:03 +01003491 struct snd_soc_codec *codec = priv->hubs.codec;
Mark Browne9b54de42012-05-09 19:20:59 +01003492
3493#ifndef CONFIG_SND_SOC_WM8994_MODULE
3494 trace_snd_soc_jack_irq(dev_name(codec->dev));
3495#endif
3496
3497 pm_wakeup_event(codec->dev, 300);
3498
Mark Brown68defe52013-07-18 22:47:02 +01003499 queue_delayed_work(system_power_efficient_wq,
3500 &priv->mic_work, msecs_to_jiffies(250));
Mark Brown88766982010-03-29 20:57:12 +01003501
3502 return IRQ_HANDLED;
3503}
3504
Charles Keepaxb3831412014-06-16 21:24:03 +01003505/* Should be called with accdet_lock held */
Mark Brownf02b0de2012-10-01 16:41:09 +01003506static void wm1811_micd_stop(struct snd_soc_codec *codec)
Mark Brown821edd22010-11-26 15:21:09 +00003507{
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02003508 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
Mark Brownf02b0de2012-10-01 16:41:09 +01003509 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
3510
3511 if (!wm8994->jackdet)
3512 return;
3513
Mark Brownf02b0de2012-10-01 16:41:09 +01003514 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1, WM8958_MICD_ENA, 0);
3515
3516 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_JACK);
3517
Mark Brownf02b0de2012-10-01 16:41:09 +01003518 if (wm8994->wm8994->pdata.jd_ext_cap)
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02003519 snd_soc_dapm_disable_pin(dapm, "MICBIAS2");
Mark Brownf02b0de2012-10-01 16:41:09 +01003520}
3521
Mark Brown78b76db2012-11-22 17:02:09 +09003522static void wm8958_button_det(struct snd_soc_codec *codec, u16 status)
Mark Brown821edd22010-11-26 15:21:09 +00003523{
Mark Brown821edd22010-11-26 15:21:09 +00003524 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown4585790d2011-11-30 10:55:14 +00003525 int report;
Mark Brown821edd22010-11-26 15:21:09 +00003526
Mark Brown78b76db2012-11-22 17:02:09 +09003527 report = 0;
3528 if (status & 0x4)
3529 report |= SND_JACK_BTN_0;
3530
3531 if (status & 0x8)
3532 report |= SND_JACK_BTN_1;
3533
3534 if (status & 0x10)
3535 report |= SND_JACK_BTN_2;
3536
3537 if (status & 0x20)
3538 report |= SND_JACK_BTN_3;
3539
3540 if (status & 0x40)
3541 report |= SND_JACK_BTN_4;
3542
3543 if (status & 0x80)
3544 report |= SND_JACK_BTN_5;
3545
3546 snd_soc_jack_report(wm8994->micdet[0].jack, report,
3547 wm8994->btn_mask);
3548}
3549
Mark Brown70bd3b22013-05-29 20:28:16 +01003550static void wm8958_open_circuit_work(struct work_struct *work)
3551{
3552 struct wm8994_priv *wm8994 = container_of(work,
3553 struct wm8994_priv,
3554 open_circuit_work.work);
3555 struct device *dev = wm8994->wm8994->dev;
3556
Mark Brown70bd3b22013-05-29 20:28:16 +01003557 mutex_lock(&wm8994->accdet_lock);
3558
Charles Keepaxb3831412014-06-16 21:24:03 +01003559 wm1811_micd_stop(wm8994->hubs.codec);
3560
Mark Brown70bd3b22013-05-29 20:28:16 +01003561 dev_dbg(dev, "Reporting open circuit\n");
3562
3563 wm8994->jack_mic = false;
3564 wm8994->mic_detecting = true;
3565
3566 wm8958_micd_set_rate(wm8994->hubs.codec);
3567
3568 snd_soc_jack_report(wm8994->micdet[0].jack, 0,
3569 wm8994->btn_mask |
3570 SND_JACK_HEADSET);
3571
3572 mutex_unlock(&wm8994->accdet_lock);
3573}
3574
Mark Brown98869f62012-12-03 16:14:37 +09003575static void wm8958_mic_id(void *data, u16 status)
Mark Brown78b76db2012-11-22 17:02:09 +09003576{
Mark Brown98869f62012-12-03 16:14:37 +09003577 struct snd_soc_codec *codec = data;
Mark Brown78b76db2012-11-22 17:02:09 +09003578 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Browna1691342011-11-30 14:56:40 +00003579
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003580 /* Either nothing present or just starting detection */
Mark Brownb00adf72011-08-13 11:57:18 +09003581 if (!(status & WM8958_MICD_STS)) {
Mark Brownf02b0de2012-10-01 16:41:09 +01003582 /* If nothing present then clear our statuses */
3583 dev_dbg(codec->dev, "Detected open circuit\n");
Mark Brown821edd22010-11-26 15:21:09 +00003584
Mark Brown68defe52013-07-18 22:47:02 +01003585 queue_delayed_work(system_power_efficient_wq,
3586 &wm8994->open_circuit_work,
3587 msecs_to_jiffies(2500));
Mark Brownb00adf72011-08-13 11:57:18 +09003588 return;
3589 }
3590
3591 /* If the measurement is showing a high impedence we've got a
3592 * microphone.
3593 */
Mark Brown78b76db2012-11-22 17:02:09 +09003594 if (status & 0x600) {
Mark Brownb00adf72011-08-13 11:57:18 +09003595 dev_dbg(codec->dev, "Detected microphone\n");
3596
Mark Brown157a75e2011-11-30 13:43:51 +00003597 wm8994->mic_detecting = false;
Mark Brownb00adf72011-08-13 11:57:18 +09003598 wm8994->jack_mic = true;
3599
3600 wm8958_micd_set_rate(codec);
3601
3602 snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADSET,
3603 SND_JACK_HEADSET);
3604 }
3605
3606
Mark Brown78b76db2012-11-22 17:02:09 +09003607 if (status & 0xfc) {
Mark Brownb00adf72011-08-13 11:57:18 +09003608 dev_dbg(codec->dev, "Detected headphone\n");
Mark Brown157a75e2011-11-30 13:43:51 +00003609 wm8994->mic_detecting = false;
Mark Brownb00adf72011-08-13 11:57:18 +09003610
3611 wm8958_micd_set_rate(codec);
3612
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003613 /* If we have jackdet that will detect removal */
Mark Brownf02b0de2012-10-01 16:41:09 +01003614 wm1811_micd_stop(codec);
Mark Brownecd17322012-03-12 16:34:35 +00003615
3616 snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADPHONE,
3617 SND_JACK_HEADSET);
Mark Brownb00adf72011-08-13 11:57:18 +09003618 }
Mark Brown821edd22010-11-26 15:21:09 +00003619}
Mark Brown9e6e96a2010-01-29 17:47:12 +00003620
Mark Brownc0cc3f12012-09-28 16:50:15 +01003621/* Deferred mic detection to allow for extra settling time */
3622static void wm1811_mic_work(struct work_struct *work)
3623{
3624 struct wm8994_priv *wm8994 = container_of(work, struct wm8994_priv,
3625 mic_work.work);
Mark Brownd9dd4ad2012-10-08 18:36:09 +09003626 struct wm8994 *control = wm8994->wm8994;
Mark Brownc0cc3f12012-09-28 16:50:15 +01003627 struct snd_soc_codec *codec = wm8994->hubs.codec;
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02003628 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
Mark Brown821edd22010-11-26 15:21:09 +00003629
Mark Brownc0cc3f12012-09-28 16:50:15 +01003630 pm_runtime_get_sync(codec->dev);
Mark Brown821edd22010-11-26 15:21:09 +00003631
Mark Brownc0cc3f12012-09-28 16:50:15 +01003632 /* If required for an external cap force MICBIAS on */
Mark Brownd9dd4ad2012-10-08 18:36:09 +09003633 if (control->pdata.jd_ext_cap) {
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02003634 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS2");
3635 snd_soc_dapm_sync(dapm);
Mark Brown821edd22010-11-26 15:21:09 +00003636 }
Mark Brownc0cc3f12012-09-28 16:50:15 +01003637
3638 mutex_lock(&wm8994->accdet_lock);
3639
3640 dev_dbg(codec->dev, "Starting mic detection\n");
3641
Mark Brown63dd5452012-11-22 20:44:32 +09003642 /* Use a user-supplied callback if we have one */
3643 if (wm8994->micd_cb) {
3644 wm8994->micd_cb(wm8994->micd_cb_data);
3645 } else {
3646 /*
3647 * Start off measument of microphone impedence to find out
3648 * what's actually there.
3649 */
3650 wm8994->mic_detecting = true;
3651 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_MIC);
Mark Brownc0cc3f12012-09-28 16:50:15 +01003652
Mark Brown63dd5452012-11-22 20:44:32 +09003653 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3654 WM8958_MICD_ENA, WM8958_MICD_ENA);
3655 }
Mark Brownc0cc3f12012-09-28 16:50:15 +01003656
3657 mutex_unlock(&wm8994->accdet_lock);
3658
3659 pm_runtime_put(codec->dev);
Mark Brown821edd22010-11-26 15:21:09 +00003660}
3661
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003662static irqreturn_t wm1811_jackdet_irq(int irq, void *data)
3663{
3664 struct wm8994_priv *wm8994 = data;
Mark Brownd9dd4ad2012-10-08 18:36:09 +09003665 struct wm8994 *control = wm8994->wm8994;
Mark Brown8cb8e832012-07-25 18:10:03 +01003666 struct snd_soc_codec *codec = wm8994->hubs.codec;
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02003667 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
Mark Brownc0cc3f12012-09-28 16:50:15 +01003668 int reg, delay;
Mark Brownc9865642012-03-12 16:31:50 +00003669 bool present;
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003670
Mark Brownb8176622012-07-24 15:48:57 +01003671 pm_runtime_get_sync(codec->dev);
3672
Mark Brown2da1c4b2013-05-30 13:42:29 +01003673 cancel_delayed_work_sync(&wm8994->mic_complete_work);
3674
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003675 mutex_lock(&wm8994->accdet_lock);
3676
3677 reg = snd_soc_read(codec, WM1811_JACKDET_CTRL);
3678 if (reg < 0) {
3679 dev_err(codec->dev, "Failed to read jack status: %d\n", reg);
3680 mutex_unlock(&wm8994->accdet_lock);
Mark Brownb8176622012-07-24 15:48:57 +01003681 pm_runtime_put(codec->dev);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003682 return IRQ_NONE;
3683 }
3684
3685 dev_dbg(codec->dev, "JACKDET %x\n", reg);
3686
Mark Brownc9865642012-03-12 16:31:50 +00003687 present = reg & WM1811_JACKDET_LVL;
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003688
Mark Brownc9865642012-03-12 16:31:50 +00003689 if (present) {
3690 dev_dbg(codec->dev, "Jack detected\n");
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003691
Mark Browne9d9a962012-04-26 16:07:32 +01003692 wm8958_micd_set_rate(codec);
3693
Mark Brown55a27782012-02-21 13:45:53 +00003694 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3695 WM8958_MICB2_DISCH, 0);
3696
Mark Brown378ec0c2012-03-01 19:01:43 +00003697 /* Disable debounce while inserted */
3698 snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
3699 WM1811_JACKDET_DB, 0);
3700
Mark Brownd9dd4ad2012-10-08 18:36:09 +09003701 delay = control->pdata.micdet_delay;
Mark Brown68defe52013-07-18 22:47:02 +01003702 queue_delayed_work(system_power_efficient_wq,
3703 &wm8994->mic_work,
3704 msecs_to_jiffies(delay));
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003705 } else {
3706 dev_dbg(codec->dev, "Jack not detected\n");
3707
Mark Brownc0cc3f12012-09-28 16:50:15 +01003708 cancel_delayed_work_sync(&wm8994->mic_work);
3709
Mark Brown55a27782012-02-21 13:45:53 +00003710 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3711 WM8958_MICB2_DISCH, WM8958_MICB2_DISCH);
3712
Mark Brown378ec0c2012-03-01 19:01:43 +00003713 /* Enable debounce while removed */
3714 snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
3715 WM1811_JACKDET_DB, WM1811_JACKDET_DB);
3716
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003717 wm8994->mic_detecting = false;
3718 wm8994->jack_mic = false;
3719 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3720 WM8958_MICD_ENA, 0);
3721 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_JACK);
3722 }
3723
3724 mutex_unlock(&wm8994->accdet_lock);
3725
Mark Brownc0cc3f12012-09-28 16:50:15 +01003726 /* Turn off MICBIAS if it was on for an external cap */
Mark Brownd9dd4ad2012-10-08 18:36:09 +09003727 if (control->pdata.jd_ext_cap && !present)
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02003728 snd_soc_dapm_disable_pin(dapm, "MICBIAS2");
Mark Brownc9865642012-03-12 16:31:50 +00003729
3730 if (present)
3731 snd_soc_jack_report(wm8994->micdet[0].jack,
3732 SND_JACK_MECHANICAL, SND_JACK_MECHANICAL);
3733 else
3734 snd_soc_jack_report(wm8994->micdet[0].jack, 0,
3735 SND_JACK_MECHANICAL | SND_JACK_HEADSET |
3736 wm8994->btn_mask);
3737
Mark Brown99af79d2012-07-25 23:03:36 +01003738 /* Since we only report deltas force an update, ensures we
3739 * avoid bootstrapping issues with the core. */
3740 snd_soc_jack_report(wm8994->micdet[0].jack, 0, 0);
3741
Mark Brownb8176622012-07-24 15:48:57 +01003742 pm_runtime_put(codec->dev);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003743 return IRQ_HANDLED;
3744}
3745
Mark Brown99af79d2012-07-25 23:03:36 +01003746static void wm1811_jackdet_bootstrap(struct work_struct *work)
3747{
3748 struct wm8994_priv *wm8994 = container_of(work,
3749 struct wm8994_priv,
3750 jackdet_bootstrap.work);
3751 wm1811_jackdet_irq(0, wm8994);
3752}
3753
Mark Brown821edd22010-11-26 15:21:09 +00003754/**
3755 * wm8958_mic_detect - Enable microphone detection via the WM8958 IRQ
3756 *
3757 * @codec: WM8958 codec
3758 * @jack: jack to report detection events on
3759 *
3760 * Enable microphone detection functionality for the WM8958. By
3761 * default simple detection which supports the detection of up to 6
3762 * buttons plus video and microphone functionality is supported.
3763 *
3764 * The WM8958 has an advanced jack detection facility which is able to
3765 * support complex accessory detection, especially when used in
3766 * conjunction with external circuitry. In order to provide maximum
3767 * flexiblity a callback is provided which allows a completely custom
3768 * detection algorithm.
3769 */
3770int wm8958_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
Mark Brown98869f62012-12-03 16:14:37 +09003771 wm1811_micdet_cb det_cb, void *det_cb_data,
3772 wm1811_mic_id_cb id_cb, void *id_cb_data)
Mark Brown821edd22010-11-26 15:21:09 +00003773{
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02003774 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
Mark Brown821edd22010-11-26 15:21:09 +00003775 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01003776 struct wm8994 *control = wm8994->wm8994;
Mark Brown4585790d2011-11-30 10:55:14 +00003777 u16 micd_lvl_sel;
Mark Brown821edd22010-11-26 15:21:09 +00003778
Mark Brown81204c82011-05-24 17:35:53 +08003779 switch (control->type) {
3780 case WM1811:
3781 case WM8958:
3782 break;
3783 default:
Mark Brown821edd22010-11-26 15:21:09 +00003784 return -EINVAL;
Mark Brown81204c82011-05-24 17:35:53 +08003785 }
Mark Brown821edd22010-11-26 15:21:09 +00003786
3787 if (jack) {
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02003788 snd_soc_dapm_force_enable_pin(dapm, "CLK_SYS");
3789 snd_soc_dapm_sync(dapm);
Mark Brown4cdf5e42011-11-29 14:36:17 +00003790
Mark Brown821edd22010-11-26 15:21:09 +00003791 wm8994->micdet[0].jack = jack;
Mark Brown821edd22010-11-26 15:21:09 +00003792
Mark Brown98869f62012-12-03 16:14:37 +09003793 if (det_cb) {
3794 wm8994->micd_cb = det_cb;
3795 wm8994->micd_cb_data = det_cb_data;
Mark Brown63dd5452012-11-22 20:44:32 +09003796 } else {
3797 wm8994->mic_detecting = true;
3798 wm8994->jack_mic = false;
3799 }
Mark Brownb00adf72011-08-13 11:57:18 +09003800
Mark Brown98869f62012-12-03 16:14:37 +09003801 if (id_cb) {
3802 wm8994->mic_id_cb = id_cb;
3803 wm8994->mic_id_cb_data = id_cb_data;
3804 } else {
3805 wm8994->mic_id_cb = wm8958_mic_id;
3806 wm8994->mic_id_cb_data = codec;
3807 }
Mark Brownb00adf72011-08-13 11:57:18 +09003808
3809 wm8958_micd_set_rate(codec);
3810
Mark Brown4585790d2011-11-30 10:55:14 +00003811 /* Detect microphones and short circuits by default */
Mark Brownd9dd4ad2012-10-08 18:36:09 +09003812 if (control->pdata.micd_lvl_sel)
3813 micd_lvl_sel = control->pdata.micd_lvl_sel;
Mark Brown4585790d2011-11-30 10:55:14 +00003814 else
3815 micd_lvl_sel = 0x41;
3816
3817 wm8994->btn_mask = SND_JACK_BTN_0 | SND_JACK_BTN_1 |
3818 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
3819 SND_JACK_BTN_4 | SND_JACK_BTN_5;
3820
Mark Brownb00adf72011-08-13 11:57:18 +09003821 snd_soc_update_bits(codec, WM8958_MIC_DETECT_2,
Mark Brown4585790d2011-11-30 10:55:14 +00003822 WM8958_MICD_LVL_SEL_MASK, micd_lvl_sel);
Mark Brownb00adf72011-08-13 11:57:18 +09003823
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02003824 WARN_ON(snd_soc_codec_get_bias_level(codec) > SND_SOC_BIAS_STANDBY);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003825
3826 /*
3827 * If we can use jack detection start off with that,
3828 * otherwise jump straight to microphone detection.
3829 */
3830 if (wm8994->jackdet) {
Mark Brown99af79d2012-07-25 23:03:36 +01003831 /* Disable debounce for the initial detect */
3832 snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
3833 WM1811_JACKDET_DB, 0);
3834
Mark Brown55a27782012-02-21 13:45:53 +00003835 snd_soc_update_bits(codec, WM8958_MICBIAS2,
3836 WM8958_MICB2_DISCH,
3837 WM8958_MICB2_DISCH);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003838 snd_soc_update_bits(codec, WM8994_LDO_1,
3839 WM8994_LDO1_DISCH, 0);
3840 wm1811_jackdet_set_mode(codec,
3841 WM1811_JACKDET_MODE_JACK);
3842 } else {
3843 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3844 WM8958_MICD_ENA, WM8958_MICD_ENA);
3845 }
3846
Mark Brown821edd22010-11-26 15:21:09 +00003847 } else {
3848 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3849 WM8958_MICD_ENA, 0);
Mark Brownafaf1592012-03-03 18:46:36 +00003850 wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_NONE);
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02003851 snd_soc_dapm_disable_pin(dapm, "CLK_SYS");
3852 snd_soc_dapm_sync(dapm);
Mark Brown821edd22010-11-26 15:21:09 +00003853 }
3854
3855 return 0;
3856}
3857EXPORT_SYMBOL_GPL(wm8958_mic_detect);
3858
Mark Brown2da1c4b2013-05-30 13:42:29 +01003859static void wm8958_mic_work(struct work_struct *work)
3860{
3861 struct wm8994_priv *wm8994 = container_of(work,
3862 struct wm8994_priv,
3863 mic_complete_work.work);
3864 struct snd_soc_codec *codec = wm8994->hubs.codec;
3865
Mark Brown2da1c4b2013-05-30 13:42:29 +01003866 pm_runtime_get_sync(codec->dev);
3867
3868 mutex_lock(&wm8994->accdet_lock);
3869
3870 wm8994->mic_id_cb(wm8994->mic_id_cb_data, wm8994->mic_status);
3871
3872 mutex_unlock(&wm8994->accdet_lock);
3873
3874 pm_runtime_put(codec->dev);
Mark Brown2da1c4b2013-05-30 13:42:29 +01003875}
3876
Mark Brown821edd22010-11-26 15:21:09 +00003877static irqreturn_t wm8958_mic_irq(int irq, void *data)
3878{
3879 struct wm8994_priv *wm8994 = data;
Mark Brown8cb8e832012-07-25 18:10:03 +01003880 struct snd_soc_codec *codec = wm8994->hubs.codec;
Mark Brown2da1c4b2013-05-30 13:42:29 +01003881 int reg, count, ret, id_delay;
Mark Brown821edd22010-11-26 15:21:09 +00003882
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003883 /*
3884 * Jack detection may have detected a removal simulataneously
3885 * with an update of the MICDET status; if so it will have
3886 * stopped detection and we can ignore this interrupt.
3887 */
Mark Brownc9865642012-03-12 16:31:50 +00003888 if (!(snd_soc_read(codec, WM8958_MIC_DETECT_1) & WM8958_MICD_ENA))
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003889 return IRQ_HANDLED;
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003890
Mark Brown2da1c4b2013-05-30 13:42:29 +01003891 cancel_delayed_work_sync(&wm8994->mic_complete_work);
Mark Brown70bd3b22013-05-29 20:28:16 +01003892 cancel_delayed_work_sync(&wm8994->open_circuit_work);
3893
Mark Brownb8176622012-07-24 15:48:57 +01003894 pm_runtime_get_sync(codec->dev);
3895
Mark Brown19940b32011-08-19 18:05:05 +09003896 /* We may occasionally read a detection without an impedence
3897 * range being provided - if that happens loop again.
3898 */
3899 count = 10;
3900 do {
3901 reg = snd_soc_read(codec, WM8958_MIC_DETECT_3);
3902 if (reg < 0) {
3903 dev_err(codec->dev,
3904 "Failed to read mic detect status: %d\n",
3905 reg);
Mark Brownb8176622012-07-24 15:48:57 +01003906 pm_runtime_put(codec->dev);
Mark Brown19940b32011-08-19 18:05:05 +09003907 return IRQ_NONE;
3908 }
Mark Brown821edd22010-11-26 15:21:09 +00003909
Mark Brown19940b32011-08-19 18:05:05 +09003910 if (!(reg & WM8958_MICD_VALID)) {
3911 dev_dbg(codec->dev, "Mic detect data not valid\n");
3912 goto out;
3913 }
3914
3915 if (!(reg & WM8958_MICD_STS) || (reg & WM8958_MICD_LVL_MASK))
3916 break;
3917
3918 msleep(1);
3919 } while (count--);
3920
3921 if (count == 0)
Masanari Iidaec8f53f2012-11-02 00:28:50 +09003922 dev_warn(codec->dev, "No impedance range reported for jack\n");
Mark Brown821edd22010-11-26 15:21:09 +00003923
Mark Brown7116f452010-12-29 13:05:21 +00003924#ifndef CONFIG_SND_SOC_WM8994_MODULE
Mark Brown2bbb5d62010-12-05 12:50:12 +00003925 trace_snd_soc_jack_irq(dev_name(codec->dev));
Mark Brown7116f452010-12-29 13:05:21 +00003926#endif
Mark Brown2bbb5d62010-12-05 12:50:12 +00003927
Mark Browne874de42012-12-03 15:58:55 +09003928 /* Avoid a transient report when the accessory is being removed */
3929 if (wm8994->jackdet) {
Mark Brown8afd0ef2012-12-07 17:10:05 +09003930 ret = snd_soc_read(codec, WM1811_JACKDET_CTRL);
3931 if (ret < 0) {
Mark Browne874de42012-12-03 15:58:55 +09003932 dev_err(codec->dev, "Failed to read jack status: %d\n",
Mark Brown8afd0ef2012-12-07 17:10:05 +09003933 ret);
3934 } else if (!(ret & WM1811_JACKDET_LVL)) {
Mark Browne874de42012-12-03 15:58:55 +09003935 dev_dbg(codec->dev, "Ignoring removed jack\n");
Mark Brown9e430882013-05-29 18:38:46 +01003936 goto out;
Mark Browne874de42012-12-03 15:58:55 +09003937 }
Mark Brown9767a582013-05-28 12:52:08 +01003938 } else if (!(reg & WM8958_MICD_STS)) {
3939 snd_soc_jack_report(wm8994->micdet[0].jack, 0,
3940 SND_JACK_MECHANICAL | SND_JACK_HEADSET |
3941 wm8994->btn_mask);
Mark Brown7afce3f2013-05-30 13:42:27 +01003942 wm8994->mic_detecting = true;
Mark Brown9767a582013-05-28 12:52:08 +01003943 goto out;
Mark Browne874de42012-12-03 15:58:55 +09003944 }
3945
Mark Brown2da1c4b2013-05-30 13:42:29 +01003946 wm8994->mic_status = reg;
3947 id_delay = wm8994->wm8994->pdata.mic_id_delay;
3948
Mark Brown78b76db2012-11-22 17:02:09 +09003949 if (wm8994->mic_detecting)
Mark Brown68defe52013-07-18 22:47:02 +01003950 queue_delayed_work(system_power_efficient_wq,
3951 &wm8994->mic_complete_work,
3952 msecs_to_jiffies(id_delay));
Mark Brown821edd22010-11-26 15:21:09 +00003953 else
Mark Brown78b76db2012-11-22 17:02:09 +09003954 wm8958_button_det(codec, reg);
Mark Brown821edd22010-11-26 15:21:09 +00003955
3956out:
Mark Brownb8176622012-07-24 15:48:57 +01003957 pm_runtime_put(codec->dev);
Mark Brown821edd22010-11-26 15:21:09 +00003958 return IRQ_HANDLED;
3959}
3960
Mark Brown3b1af3f2011-07-14 12:38:18 +09003961static irqreturn_t wm8994_fifo_error(int irq, void *data)
3962{
3963 struct snd_soc_codec *codec = data;
3964
3965 dev_err(codec->dev, "FIFO error\n");
3966
3967 return IRQ_HANDLED;
3968}
3969
Mark Brownf0b182b2011-08-16 12:01:27 +09003970static irqreturn_t wm8994_temp_warn(int irq, void *data)
3971{
3972 struct snd_soc_codec *codec = data;
3973
3974 dev_err(codec->dev, "Thermal warning\n");
3975
3976 return IRQ_HANDLED;
3977}
3978
3979static irqreturn_t wm8994_temp_shut(int irq, void *data)
3980{
3981 struct snd_soc_codec *codec = data;
3982
3983 dev_crit(codec->dev, "Thermal shutdown\n");
3984
3985 return IRQ_HANDLED;
3986}
3987
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003988static int wm8994_codec_probe(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003989{
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02003990 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
Mark Brownd9a76662011-07-24 12:49:52 +01003991 struct wm8994 *control = dev_get_drvdata(codec->dev->parent);
Mark Brown2bc16ed2012-03-03 23:24:39 +00003992 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brownd9a76662011-07-24 12:49:52 +01003993 unsigned int reg;
Mark Brownec62dbd2010-08-15 14:56:40 +01003994 int ret, i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003995
Mark Brown8cb8e832012-07-25 18:10:03 +01003996 wm8994->hubs.codec = codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003997
Mark Brownaf6b6fe2011-11-30 20:32:05 +00003998 mutex_init(&wm8994->accdet_lock);
Mark Brown99af79d2012-07-25 23:03:36 +01003999 INIT_DELAYED_WORK(&wm8994->jackdet_bootstrap,
4000 wm1811_jackdet_bootstrap);
Mark Brown70bd3b22013-05-29 20:28:16 +01004001 INIT_DELAYED_WORK(&wm8994->open_circuit_work,
4002 wm8958_open_circuit_work);
Mark Brownaf6b6fe2011-11-30 20:32:05 +00004003
Mark Brownc0cc3f12012-09-28 16:50:15 +01004004 switch (control->type) {
4005 case WM8994:
4006 INIT_DELAYED_WORK(&wm8994->mic_work, wm8994_mic_work);
4007 break;
4008 case WM1811:
4009 INIT_DELAYED_WORK(&wm8994->mic_work, wm1811_mic_work);
4010 break;
4011 default:
4012 break;
4013 }
4014
Mark Brown2da1c4b2013-05-30 13:42:29 +01004015 INIT_DELAYED_WORK(&wm8994->mic_complete_work, wm8958_mic_work);
4016
Mark Brownc7ebf932011-07-12 19:47:59 +09004017 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
4018 init_completion(&wm8994->fll_locked[i]);
4019
Mark Brownd9dd4ad2012-10-08 18:36:09 +09004020 wm8994->micdet_irq = control->pdata.micdet_irq;
Mark Brown9b7c5252011-02-17 20:05:44 -08004021
Mark Brownf959dee2012-01-31 16:16:47 +00004022 /* By default use idle_bias_off, will override for WM8994 */
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02004023 dapm->idle_bias_off = 1;
Mark Brownf959dee2012-01-31 16:16:47 +00004024
Mark Brown9e6e96a2010-01-29 17:47:12 +00004025 /* Set revision-specific configuration */
Mark Brown3a423152010-11-26 15:21:06 +00004026 switch (control->type) {
4027 case WM8994:
Mark Brownf959dee2012-01-31 16:16:47 +00004028 /* Single ended line outputs should have VMID on. */
Mark Brownd9dd4ad2012-10-08 18:36:09 +09004029 if (!control->pdata.lineout1_diff ||
4030 !control->pdata.lineout2_diff)
Lars-Peter Clausen8e09bac2015-06-01 10:11:00 +02004031 dapm->idle_bias_off = 0;
Mark Brownf959dee2012-01-31 16:16:47 +00004032
Mark Brownda445afe2013-03-12 17:46:09 +00004033 switch (control->revision) {
Mark Brown3a423152010-11-26 15:21:06 +00004034 case 2:
4035 case 3:
Mark Brown4537c4e2011-08-01 13:10:16 +09004036 wm8994->hubs.dcs_codes_l = -5;
4037 wm8994->hubs.dcs_codes_r = -5;
Mark Brown3a423152010-11-26 15:21:06 +00004038 wm8994->hubs.hp_startup_mode = 1;
4039 wm8994->hubs.dcs_readback_mode = 1;
Mark Brownf9acf9f2011-06-07 23:23:52 +01004040 wm8994->hubs.series_startup = 1;
Mark Brown3a423152010-11-26 15:21:06 +00004041 break;
4042 default:
Mark Brown79ef0ab2011-08-01 13:02:17 +09004043 wm8994->hubs.dcs_readback_mode = 2;
Mark Brown3a423152010-11-26 15:21:06 +00004044 break;
4045 }
Mark Brown280ec8b2011-08-10 22:19:19 +09004046 break;
Mark Brown3a423152010-11-26 15:21:06 +00004047
4048 case WM8958:
Mark Brown8437f702010-03-29 17:09:45 +01004049 wm8994->hubs.dcs_readback_mode = 1;
Mark Brown29fdc362012-02-21 10:50:50 +00004050 wm8994->hubs.hp_startup_mode = 1;
Mark Brown20dc24a2012-04-05 12:55:20 +01004051
Mark Brownda445afe2013-03-12 17:46:09 +00004052 switch (control->revision) {
Mark Brown20dc24a2012-04-05 12:55:20 +01004053 case 0:
4054 break;
4055 default:
4056 wm8994->fll_byp = true;
4057 break;
4058 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00004059 break;
Mark Brown3a423152010-11-26 15:21:06 +00004060
Mark Brown81204c82011-05-24 17:35:53 +08004061 case WM1811:
4062 wm8994->hubs.dcs_readback_mode = 2;
4063 wm8994->hubs.no_series_update = 1;
Mark Brown29fdc362012-02-21 10:50:50 +00004064 wm8994->hubs.hp_startup_mode = 1;
Mark Brownaf31a222012-04-26 20:06:56 +01004065 wm8994->hubs.no_cache_dac_hp_direct = true;
Mark Brown20dc24a2012-04-05 12:55:20 +01004066 wm8994->fll_byp = true;
Mark Brown81204c82011-05-24 17:35:53 +08004067
Mark Brown72222be32012-11-28 13:46:56 +00004068 wm8994->hubs.dcs_codes_l = -9;
4069 wm8994->hubs.dcs_codes_r = -7;
Mark Brown81204c82011-05-24 17:35:53 +08004070
4071 snd_soc_update_bits(codec, WM8994_ANALOGUE_HP_1,
4072 WM1811_HPOUT1_ATTN, WM1811_HPOUT1_ATTN);
4073 break;
4074
Mark Brown9e6e96a2010-01-29 17:47:12 +00004075 default:
4076 break;
4077 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00004078
Mark Brown2a8a8562011-07-24 12:20:41 +01004079 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR,
Mark Brown3b1af3f2011-07-14 12:38:18 +09004080 wm8994_fifo_error, "FIFO error", codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01004081 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN,
Mark Brownf0b182b2011-08-16 12:01:27 +09004082 wm8994_temp_warn, "Thermal warning", codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01004083 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT,
Mark Brownf0b182b2011-08-16 12:01:27 +09004084 wm8994_temp_shut, "Thermal shutdown", codec);
Mark Brown3b1af3f2011-07-14 12:38:18 +09004085
Mark Brown3a423152010-11-26 15:21:06 +00004086 switch (control->type) {
4087 case WM8994:
Nikesh Oswaldfe8f1f2014-08-13 10:05:45 +01004088 if (wm8994->micdet_irq)
Mark Brown9b7c5252011-02-17 20:05:44 -08004089 ret = request_threaded_irq(wm8994->micdet_irq, NULL,
4090 wm8994_mic_irq,
Fabio Estevamd78395c2015-05-12 01:22:58 -03004091 IRQF_TRIGGER_RISING |
4092 IRQF_ONESHOT,
Mark Brown9b7c5252011-02-17 20:05:44 -08004093 "Mic1 detect",
4094 wm8994);
Nikesh Oswaldfe8f1f2014-08-13 10:05:45 +01004095 else
4096 ret = wm8994_request_irq(wm8994->wm8994,
4097 WM8994_IRQ_MIC1_DET,
4098 wm8994_mic_irq, "Mic 1 detect",
4099 wm8994);
4100
4101 if (ret != 0)
4102 dev_warn(codec->dev,
4103 "Failed to request Mic1 detect IRQ: %d\n",
4104 ret);
4105
Mark Brown88766982010-03-29 20:57:12 +01004106
Mark Brown2a8a8562011-07-24 12:20:41 +01004107 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brown3a423152010-11-26 15:21:06 +00004108 WM8994_IRQ_MIC1_SHRT,
4109 wm8994_mic_irq, "Mic 1 short",
4110 wm8994);
4111 if (ret != 0)
4112 dev_warn(codec->dev,
4113 "Failed to request Mic1 short IRQ: %d\n",
4114 ret);
Mark Brown88766982010-03-29 20:57:12 +01004115
Mark Brown2a8a8562011-07-24 12:20:41 +01004116 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brown3a423152010-11-26 15:21:06 +00004117 WM8994_IRQ_MIC2_DET,
4118 wm8994_mic_irq, "Mic 2 detect",
4119 wm8994);
4120 if (ret != 0)
4121 dev_warn(codec->dev,
4122 "Failed to request Mic2 detect IRQ: %d\n",
4123 ret);
Mark Brown88766982010-03-29 20:57:12 +01004124
Mark Brown2a8a8562011-07-24 12:20:41 +01004125 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brown3a423152010-11-26 15:21:06 +00004126 WM8994_IRQ_MIC2_SHRT,
4127 wm8994_mic_irq, "Mic 2 short",
4128 wm8994);
4129 if (ret != 0)
4130 dev_warn(codec->dev,
4131 "Failed to request Mic2 short IRQ: %d\n",
4132 ret);
4133 break;
Mark Brown821edd22010-11-26 15:21:09 +00004134
4135 case WM8958:
Mark Brown81204c82011-05-24 17:35:53 +08004136 case WM1811:
Mark Brown9b7c5252011-02-17 20:05:44 -08004137 if (wm8994->micdet_irq) {
4138 ret = request_threaded_irq(wm8994->micdet_irq, NULL,
4139 wm8958_mic_irq,
Fabio Estevamd78395c2015-05-12 01:22:58 -03004140 IRQF_TRIGGER_RISING |
4141 IRQF_ONESHOT,
Mark Brown9b7c5252011-02-17 20:05:44 -08004142 "Mic detect",
4143 wm8994);
4144 if (ret != 0)
4145 dev_warn(codec->dev,
4146 "Failed to request Mic detect IRQ: %d\n",
4147 ret);
Mark Brownb4046d02012-07-18 19:11:30 +01004148 } else {
4149 wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_MIC1_DET,
4150 wm8958_mic_irq, "Mic detect",
4151 wm8994);
Mark Brown9b7c5252011-02-17 20:05:44 -08004152 }
Mark Brown3a423152010-11-26 15:21:06 +00004153 }
Mark Brown88766982010-03-29 20:57:12 +01004154
Mark Brownaf6b6fe2011-11-30 20:32:05 +00004155 switch (control->type) {
4156 case WM1811:
Mark Brownda445afe2013-03-12 17:46:09 +00004157 if (control->cust_id > 1 || control->revision > 1) {
Mark Brownaf6b6fe2011-11-30 20:32:05 +00004158 ret = wm8994_request_irq(wm8994->wm8994,
4159 WM8994_IRQ_GPIO(6),
4160 wm1811_jackdet_irq, "JACKDET",
4161 wm8994);
4162 if (ret == 0)
4163 wm8994->jackdet = true;
4164 }
4165 break;
4166 default:
4167 break;
4168 }
4169
Mark Brownc7ebf932011-07-12 19:47:59 +09004170 wm8994->fll_locked_irq = true;
4171 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++) {
Mark Brown2a8a8562011-07-24 12:20:41 +01004172 ret = wm8994_request_irq(wm8994->wm8994,
Mark Brownc7ebf932011-07-12 19:47:59 +09004173 WM8994_IRQ_FLL1_LOCK + i,
4174 wm8994_fll_locked_irq, "FLL lock",
4175 &wm8994->fll_locked[i]);
4176 if (ret != 0)
4177 wm8994->fll_locked_irq = false;
4178 }
4179
Mark Brown27060b3c2012-02-06 18:42:14 +00004180 /* Make sure we can read from the GPIOs if they're inputs */
4181 pm_runtime_get_sync(codec->dev);
4182
Mark Brown9e6e96a2010-01-29 17:47:12 +00004183 /* Remember if AIFnLRCLK is configured as a GPIO. This should be
4184 * configured on init - if a system wants to do this dynamically
4185 * at runtime we can deal with that then.
4186 */
Mark Brownd9a76662011-07-24 12:49:52 +01004187 ret = regmap_read(control->regmap, WM8994_GPIO_1, &reg);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004188 if (ret < 0) {
4189 dev_err(codec->dev, "Failed to read GPIO1 state: %d\n", ret);
Mark Brown88766982010-03-29 20:57:12 +01004190 goto err_irq;
Mark Brown9e6e96a2010-01-29 17:47:12 +00004191 }
Mark Brownd9a76662011-07-24 12:49:52 +01004192 if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00004193 wm8994->lrclk_shared[0] = 1;
4194 wm8994_dai[0].symmetric_rates = 1;
4195 } else {
4196 wm8994->lrclk_shared[0] = 0;
4197 }
4198
Mark Brownd9a76662011-07-24 12:49:52 +01004199 ret = regmap_read(control->regmap, WM8994_GPIO_6, &reg);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004200 if (ret < 0) {
4201 dev_err(codec->dev, "Failed to read GPIO6 state: %d\n", ret);
Mark Brown88766982010-03-29 20:57:12 +01004202 goto err_irq;
Mark Brown9e6e96a2010-01-29 17:47:12 +00004203 }
Mark Brownd9a76662011-07-24 12:49:52 +01004204 if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00004205 wm8994->lrclk_shared[1] = 1;
4206 wm8994_dai[1].symmetric_rates = 1;
4207 } else {
4208 wm8994->lrclk_shared[1] = 0;
4209 }
4210
Mark Brown27060b3c2012-02-06 18:42:14 +00004211 pm_runtime_put(codec->dev);
4212
Mark Brownbfd37bb2012-06-05 12:31:32 +01004213 /* Latch volume update bits */
4214 for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
4215 snd_soc_update_bits(codec, wm8994_vu_bits[i].reg,
4216 wm8994_vu_bits[i].mask,
4217 wm8994_vu_bits[i].mask);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004218
4219 /* Set the low bit of the 3D stereo depth so TLV matches */
4220 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_FILTERS_2,
4221 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT,
4222 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT);
4223 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_FILTERS_2,
4224 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT,
4225 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT);
4226 snd_soc_update_bits(codec, WM8994_AIF2_DAC_FILTERS_2,
4227 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT,
4228 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT);
4229
Mark Brown5b739672011-07-06 00:08:43 -07004230 /* Unconditionally enable AIF1 ADC TDM mode on chips which can
4231 * use this; it only affects behaviour on idle TDM clock
4232 * cycles. */
4233 switch (control->type) {
4234 case WM8994:
4235 case WM8958:
4236 snd_soc_update_bits(codec, WM8994_AIF1_CONTROL_1,
4237 WM8994_AIF1ADC_TDM, WM8994_AIF1ADC_TDM);
4238 break;
4239 default:
4240 break;
4241 }
Mark Brownd1ce6b22010-07-20 10:13:14 +01004242
Mark Brown500fa302011-11-29 19:58:19 +00004243 /* Put MICBIAS into bypass mode by default on newer devices */
4244 switch (control->type) {
4245 case WM8958:
4246 case WM1811:
4247 snd_soc_update_bits(codec, WM8958_MICBIAS1,
4248 WM8958_MICB1_MODE, WM8958_MICB1_MODE);
4249 snd_soc_update_bits(codec, WM8958_MICBIAS2,
4250 WM8958_MICB2_MODE, WM8958_MICB2_MODE);
4251 break;
4252 default:
4253 break;
4254 }
4255
Mark Brownc3403042012-04-26 21:29:29 +01004256 wm8994->hubs.check_class_w_digital = wm8994_check_class_w_digital;
4257 wm_hubs_update_class_w(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004258
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004259 wm8994_handle_pdata(wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004260
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004261 wm_hubs_add_analogue_controls(codec);
Liam Girdwood022658b2012-02-03 17:43:09 +00004262 snd_soc_add_codec_controls(codec, wm8994_snd_controls,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004263 ARRAY_SIZE(wm8994_snd_controls));
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004264 snd_soc_dapm_new_controls(dapm, wm8994_dapm_widgets,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004265 ARRAY_SIZE(wm8994_dapm_widgets));
Mark Brownc4431df2010-11-26 15:21:07 +00004266
4267 switch (control->type) {
4268 case WM8994:
4269 snd_soc_dapm_new_controls(dapm, wm8994_specific_dapm_widgets,
4270 ARRAY_SIZE(wm8994_specific_dapm_widgets));
Mark Brownda445afe2013-03-12 17:46:09 +00004271 if (control->revision < 4) {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00004272 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
4273 ARRAY_SIZE(wm8994_lateclk_revd_widgets));
Dimitris Papastamos04d28682011-03-01 11:47:10 +00004274 snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
4275 ARRAY_SIZE(wm8994_adc_revd_widgets));
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00004276 snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
4277 ARRAY_SIZE(wm8994_dac_revd_widgets));
4278 } else {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00004279 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
4280 ARRAY_SIZE(wm8994_lateclk_widgets));
Dimitris Papastamos04d28682011-03-01 11:47:10 +00004281 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
4282 ARRAY_SIZE(wm8994_adc_widgets));
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00004283 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
4284 ARRAY_SIZE(wm8994_dac_widgets));
4285 }
Mark Brownc4431df2010-11-26 15:21:07 +00004286 break;
4287 case WM8958:
Liam Girdwood022658b2012-02-03 17:43:09 +00004288 snd_soc_add_codec_controls(codec, wm8958_snd_controls,
Mark Brownc4431df2010-11-26 15:21:07 +00004289 ARRAY_SIZE(wm8958_snd_controls));
4290 snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
4291 ARRAY_SIZE(wm8958_dapm_widgets));
Mark Brownda445afe2013-03-12 17:46:09 +00004292 if (control->revision < 1) {
Mark Brown780e2802011-03-11 18:00:19 +00004293 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
4294 ARRAY_SIZE(wm8994_lateclk_revd_widgets));
4295 snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
4296 ARRAY_SIZE(wm8994_adc_revd_widgets));
4297 snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
4298 ARRAY_SIZE(wm8994_dac_revd_widgets));
4299 } else {
4300 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
4301 ARRAY_SIZE(wm8994_lateclk_widgets));
4302 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
4303 ARRAY_SIZE(wm8994_adc_widgets));
4304 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
4305 ARRAY_SIZE(wm8994_dac_widgets));
4306 }
Mark Brownc4431df2010-11-26 15:21:07 +00004307 break;
Mark Brown81204c82011-05-24 17:35:53 +08004308
4309 case WM1811:
Liam Girdwood022658b2012-02-03 17:43:09 +00004310 snd_soc_add_codec_controls(codec, wm8958_snd_controls,
Mark Brown81204c82011-05-24 17:35:53 +08004311 ARRAY_SIZE(wm8958_snd_controls));
4312 snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
4313 ARRAY_SIZE(wm8958_dapm_widgets));
4314 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
4315 ARRAY_SIZE(wm8994_lateclk_widgets));
4316 snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
4317 ARRAY_SIZE(wm8994_adc_widgets));
4318 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
4319 ARRAY_SIZE(wm8994_dac_widgets));
4320 break;
Mark Brownc4431df2010-11-26 15:21:07 +00004321 }
Mark Brownc4431df2010-11-26 15:21:07 +00004322
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004323 wm_hubs_add_analogue_routes(codec, 0, 0);
wangbiaob888edb2013-11-22 10:44:30 +08004324 ret = wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
4325 wm_hubs_dcs_done, "DC servo done",
4326 &wm8994->hubs);
4327 if (ret == 0)
4328 wm8994->hubs.dcs_done_irq = true;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004329 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
Mark Brown9e6e96a2010-01-29 17:47:12 +00004330
Mark Brownc4431df2010-11-26 15:21:07 +00004331 switch (control->type) {
4332 case WM8994:
4333 snd_soc_dapm_add_routes(dapm, wm8994_intercon,
4334 ARRAY_SIZE(wm8994_intercon));
Mark Brown6ed8f142011-02-03 16:27:35 +00004335
Mark Brownda445afe2013-03-12 17:46:09 +00004336 if (control->revision < 4) {
Mark Brown6ed8f142011-02-03 16:27:35 +00004337 snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
4338 ARRAY_SIZE(wm8994_revd_intercon));
Dimitris Papastamos173efa02011-02-11 16:32:11 +00004339 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
4340 ARRAY_SIZE(wm8994_lateclk_revd_intercon));
4341 } else {
4342 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
4343 ARRAY_SIZE(wm8994_lateclk_intercon));
4344 }
Mark Brownc4431df2010-11-26 15:21:07 +00004345 break;
4346 case WM8958:
Mark Brownda445afe2013-03-12 17:46:09 +00004347 if (control->revision < 1) {
Chris Rattray15676932012-08-09 10:10:54 +01004348 snd_soc_dapm_add_routes(dapm, wm8994_intercon,
4349 ARRAY_SIZE(wm8994_intercon));
Mark Brown780e2802011-03-11 18:00:19 +00004350 snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
4351 ARRAY_SIZE(wm8994_revd_intercon));
4352 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
4353 ARRAY_SIZE(wm8994_lateclk_revd_intercon));
4354 } else {
4355 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
4356 ARRAY_SIZE(wm8994_lateclk_intercon));
4357 snd_soc_dapm_add_routes(dapm, wm8958_intercon,
4358 ARRAY_SIZE(wm8958_intercon));
4359 }
Mark Brownf701a2e2011-03-09 19:31:01 +00004360
4361 wm8958_dsp2_init(codec);
Mark Brownc4431df2010-11-26 15:21:07 +00004362 break;
Mark Brown81204c82011-05-24 17:35:53 +08004363 case WM1811:
4364 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
4365 ARRAY_SIZE(wm8994_lateclk_intercon));
4366 snd_soc_dapm_add_routes(dapm, wm8958_intercon,
4367 ARRAY_SIZE(wm8958_intercon));
4368 break;
Mark Brownc4431df2010-11-26 15:21:07 +00004369 }
4370
Mark Brown9e6e96a2010-01-29 17:47:12 +00004371 return 0;
4372
Mark Brown88766982010-03-29 20:57:12 +01004373err_irq:
Mark Brownaf6b6fe2011-11-30 20:32:05 +00004374 if (wm8994->jackdet)
4375 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01004376 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_SHRT, wm8994);
4377 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET, wm8994);
4378 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT, wm8994);
Mark Brown9b7c5252011-02-17 20:05:44 -08004379 if (wm8994->micdet_irq)
4380 free_irq(wm8994->micdet_irq, wm8994);
Mark Brownc7ebf932011-07-12 19:47:59 +09004381 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
Mark Brown2a8a8562011-07-24 12:20:41 +01004382 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
Mark Brownc7ebf932011-07-12 19:47:59 +09004383 &wm8994->fll_locked[i]);
Mark Brown2a8a8562011-07-24 12:20:41 +01004384 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
Mark Brownb30ead52011-07-12 15:47:17 +09004385 &wm8994->hubs);
Mark Brown2a8a8562011-07-24 12:20:41 +01004386 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, codec);
4387 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, codec);
4388 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, codec);
Mark Browna421a0e2011-12-29 11:08:34 +00004389
Mark Brown9e6e96a2010-01-29 17:47:12 +00004390 return ret;
4391}
4392
Jesper Juhl34ff0f92012-04-09 22:52:19 +02004393static int wm8994_codec_remove(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00004394{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004395 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown2a8a8562011-07-24 12:20:41 +01004396 struct wm8994 *control = wm8994->wm8994;
Mark Brownc7ebf932011-07-12 19:47:59 +09004397 int i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00004398
Mark Brownc7ebf932011-07-12 19:47:59 +09004399 for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
Mark Brown2a8a8562011-07-24 12:20:41 +01004400 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
Mark Brownc7ebf932011-07-12 19:47:59 +09004401 &wm8994->fll_locked[i]);
4402
Mark Brown2a8a8562011-07-24 12:20:41 +01004403 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
Mark Brownb30ead52011-07-12 15:47:17 +09004404 &wm8994->hubs);
Mark Brown2a8a8562011-07-24 12:20:41 +01004405 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, codec);
4406 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, codec);
4407 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, codec);
Mark Brownb30ead52011-07-12 15:47:17 +09004408
Mark Brownaf6b6fe2011-11-30 20:32:05 +00004409 if (wm8994->jackdet)
4410 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);
4411
Mark Brown3a423152010-11-26 15:21:06 +00004412 switch (control->type) {
4413 case WM8994:
Mark Brown9b7c5252011-02-17 20:05:44 -08004414 if (wm8994->micdet_irq)
4415 free_irq(wm8994->micdet_irq, wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01004416 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET,
Mark Brown3a423152010-11-26 15:21:06 +00004417 wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01004418 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT,
Mark Brown3a423152010-11-26 15:21:06 +00004419 wm8994);
Mark Brown2a8a8562011-07-24 12:20:41 +01004420 wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_DET,
Mark Brown3a423152010-11-26 15:21:06 +00004421 wm8994);
4422 break;
Mark Brown821edd22010-11-26 15:21:09 +00004423
Mark Brown81204c82011-05-24 17:35:53 +08004424 case WM1811:
Mark Brown821edd22010-11-26 15:21:09 +00004425 case WM8958:
Mark Brown9b7c5252011-02-17 20:05:44 -08004426 if (wm8994->micdet_irq)
4427 free_irq(wm8994->micdet_irq, wm8994);
Mark Brown821edd22010-11-26 15:21:09 +00004428 break;
Mark Brown3a423152010-11-26 15:21:06 +00004429 }
Jesper Juhl34ff0f92012-04-09 22:52:19 +02004430 release_firmware(wm8994->mbc);
4431 release_firmware(wm8994->mbc_vss);
4432 release_firmware(wm8994->enh_eq);
Axel Lin24fb2b12010-11-23 15:58:39 +08004433 kfree(wm8994->retune_mobile_texts);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004434 return 0;
4435}
4436
Xiubo Li7a34b1c2014-04-03 07:53:59 +08004437static struct regmap *wm8994_get_regmap(struct device *dev)
Xiubo Lic0b6f59b2014-03-26 13:40:32 +08004438{
4439 struct wm8994 *control = dev_get_drvdata(dev->parent);
4440
4441 return control->regmap;
4442}
4443
Julia Lawallf802d6c2016-08-31 23:52:27 +02004444static const struct snd_soc_codec_driver soc_codec_dev_wm8994 = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004445 .probe = wm8994_codec_probe,
4446 .remove = wm8994_codec_remove,
Mark Brown4752a882012-03-04 02:16:01 +00004447 .suspend = wm8994_codec_suspend,
4448 .resume = wm8994_codec_resume,
Xiubo Lic0b6f59b2014-03-26 13:40:32 +08004449 .get_regmap = wm8994_get_regmap,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004450 .set_bias_level = wm8994_set_bias_level,
4451};
4452
Bill Pemberton7a79e942012-12-07 09:26:37 -05004453static int wm8994_probe(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004454{
Mark Brown2bc16ed2012-03-03 23:24:39 +00004455 struct wm8994_priv *wm8994;
4456
4457 wm8994 = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_priv),
4458 GFP_KERNEL);
4459 if (wm8994 == NULL)
4460 return -ENOMEM;
4461 platform_set_drvdata(pdev, wm8994);
4462
Lars-Peter Clausenfabfad22014-11-09 17:01:02 +01004463 mutex_init(&wm8994->fw_lock);
4464
Mark Brown2bc16ed2012-03-03 23:24:39 +00004465 wm8994->wm8994 = dev_get_drvdata(pdev->dev.parent);
Mark Brown2bc16ed2012-03-03 23:24:39 +00004466
Mark Brown57e265c2013-07-01 20:46:34 +01004467 pm_runtime_enable(&pdev->dev);
4468 pm_runtime_idle(&pdev->dev);
4469
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004470 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wm8994,
4471 wm8994_dai, ARRAY_SIZE(wm8994_dai));
4472}
4473
Bill Pemberton7a79e942012-12-07 09:26:37 -05004474static int wm8994_remove(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004475{
4476 snd_soc_unregister_codec(&pdev->dev);
Mark Brown57e265c2013-07-01 20:46:34 +01004477 pm_runtime_disable(&pdev->dev);
4478
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004479 return 0;
4480}
4481
Mark Brown4752a882012-03-04 02:16:01 +00004482#ifdef CONFIG_PM_SLEEP
4483static int wm8994_suspend(struct device *dev)
4484{
4485 struct wm8994_priv *wm8994 = dev_get_drvdata(dev);
4486
4487 /* Drop down to power saving mode when system is suspended */
4488 if (wm8994->jackdet && !wm8994->active_refcount)
4489 regmap_update_bits(wm8994->wm8994->regmap, WM8994_ANTIPOP_2,
4490 WM1811_JACKDET_MODE_MASK,
4491 wm8994->jackdet_mode);
4492
4493 return 0;
4494}
4495
4496static int wm8994_resume(struct device *dev)
4497{
4498 struct wm8994_priv *wm8994 = dev_get_drvdata(dev);
4499
Mark Brown78b76db2012-11-22 17:02:09 +09004500 if (wm8994->jackdet && wm8994->jackdet_mode)
Mark Brown4752a882012-03-04 02:16:01 +00004501 regmap_update_bits(wm8994->wm8994->regmap, WM8994_ANTIPOP_2,
4502 WM1811_JACKDET_MODE_MASK,
4503 WM1811_JACKDET_MODE_AUDIO);
4504
4505 return 0;
4506}
4507#endif
4508
4509static const struct dev_pm_ops wm8994_pm_ops = {
4510 SET_SYSTEM_SLEEP_PM_OPS(wm8994_suspend, wm8994_resume)
4511};
4512
Mark Brown9e6e96a2010-01-29 17:47:12 +00004513static struct platform_driver wm8994_codec_driver = {
4514 .driver = {
Mark Brown4752a882012-03-04 02:16:01 +00004515 .name = "wm8994-codec",
Mark Brown4752a882012-03-04 02:16:01 +00004516 .pm = &wm8994_pm_ops,
4517 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004518 .probe = wm8994_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -05004519 .remove = wm8994_remove,
Mark Brown9e6e96a2010-01-29 17:47:12 +00004520};
4521
Mark Brown5bbcc3c2011-11-23 22:52:08 +00004522module_platform_driver(wm8994_codec_driver);
Mark Brown9e6e96a2010-01-29 17:47:12 +00004523
4524MODULE_DESCRIPTION("ASoC WM8994 driver");
4525MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
4526MODULE_LICENSE("GPL");
4527MODULE_ALIAS("platform:wm8994-codec");