blob: 59d361145b15357dbebdd61a7e0d095e1347b046 [file] [log] [blame]
Mark Brown9e6e96a2010-01-29 17:47:12 +00001/*
2 * wm8994.c -- WM8994 ALSA SoC Audio driver
3 *
4 * Copyright 2009 Wolfson Microelectronics plc
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/init.h>
17#include <linux/delay.h>
18#include <linux/pm.h>
19#include <linux/i2c.h>
20#include <linux/platform_device.h>
Mark Brown39fb51a2010-11-26 17:23:43 +000021#include <linux/pm_runtime.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000022#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000024#include <sound/core.h>
Mark Brown821edd22010-11-26 15:21:09 +000025#include <sound/jack.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000026#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/soc.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000029#include <sound/initval.h>
30#include <sound/tlv.h>
31
32#include <linux/mfd/wm8994/core.h>
33#include <linux/mfd/wm8994/registers.h>
34#include <linux/mfd/wm8994/pdata.h>
35#include <linux/mfd/wm8994/gpio.h>
36
37#include "wm8994.h"
38#include "wm_hubs.h"
39
Mark Brown9e6e96a2010-01-29 17:47:12 +000040struct fll_config {
41 int src;
42 int in;
43 int out;
44};
45
46#define WM8994_NUM_DRC 3
47#define WM8994_NUM_EQ 3
48
49static int wm8994_drc_base[] = {
50 WM8994_AIF1_DRC1_1,
51 WM8994_AIF1_DRC2_1,
52 WM8994_AIF2_DRC_1,
53};
54
55static int wm8994_retune_mobile_base[] = {
56 WM8994_AIF1_DAC1_EQ_GAINS_1,
57 WM8994_AIF1_DAC2_EQ_GAINS_1,
58 WM8994_AIF2_EQ_GAINS_1,
59};
60
Mark Brown88766982010-03-29 20:57:12 +010061struct wm8994_micdet {
62 struct snd_soc_jack *jack;
63 int det;
64 int shrt;
65};
66
Mark Brown9e6e96a2010-01-29 17:47:12 +000067/* codec private data */
68struct wm8994_priv {
69 struct wm_hubs_data hubs;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000070 enum snd_soc_control_type control_type;
71 void *control_data;
72 struct snd_soc_codec *codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +000073 int sysclk[2];
74 int sysclk_rate[2];
75 int mclk[2];
76 int aifclk[2];
77 struct fll_config fll[2], fll_suspend[2];
78
79 int dac_rates[2];
80 int lrclk_shared[2];
81
Mark Brownd6addcc2010-11-26 15:21:08 +000082 int mbc_ena[3];
83
Mark Brown9e6e96a2010-01-29 17:47:12 +000084 /* Platform dependant DRC configuration */
85 const char **drc_texts;
86 int drc_cfg[WM8994_NUM_DRC];
87 struct soc_enum drc_enum;
88
89 /* Platform dependant ReTune mobile configuration */
90 int num_retune_mobile_texts;
91 const char **retune_mobile_texts;
92 int retune_mobile_cfg[WM8994_NUM_EQ];
93 struct soc_enum retune_mobile_enum;
94
Mark Brown131d8102010-11-30 17:03:39 +000095 /* Platform dependant MBC configuration */
96 int mbc_cfg;
97 const char **mbc_texts;
98 struct soc_enum mbc_enum;
99
Mark Brown88766982010-03-29 20:57:12 +0100100 struct wm8994_micdet micdet[2];
101
Mark Brown821edd22010-11-26 15:21:09 +0000102 wm8958_micdet_cb jack_cb;
103 void *jack_cb_data;
104 bool jack_is_mic;
105 bool jack_is_video;
106
Mark Brownb6b05692010-08-13 12:58:20 +0100107 int revision;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000108 struct wm8994_pdata *pdata;
109};
110
Mark Brown9e6e96a2010-01-29 17:47:12 +0000111static int wm8994_readable(unsigned int reg)
112{
Mark Browne88ff1e2010-07-09 00:12:08 +0900113 switch (reg) {
114 case WM8994_GPIO_1:
115 case WM8994_GPIO_2:
116 case WM8994_GPIO_3:
117 case WM8994_GPIO_4:
118 case WM8994_GPIO_5:
119 case WM8994_GPIO_6:
120 case WM8994_GPIO_7:
121 case WM8994_GPIO_8:
122 case WM8994_GPIO_9:
123 case WM8994_GPIO_10:
124 case WM8994_GPIO_11:
125 case WM8994_INTERRUPT_STATUS_1:
126 case WM8994_INTERRUPT_STATUS_2:
127 case WM8994_INTERRUPT_RAW_STATUS_2:
128 return 1;
129 default:
130 break;
131 }
132
Mark Brown7b306da2010-11-16 20:11:40 +0000133 if (reg >= WM8994_CACHE_SIZE)
Mark Brown9e6e96a2010-01-29 17:47:12 +0000134 return 0;
Mark Brown7b306da2010-11-16 20:11:40 +0000135 return wm8994_access_masks[reg].readable != 0;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000136}
137
138static int wm8994_volatile(unsigned int reg)
139{
Mark Brownca9aef52010-11-26 17:23:41 +0000140 if (reg >= WM8994_CACHE_SIZE)
Mark Brown9e6e96a2010-01-29 17:47:12 +0000141 return 1;
142
143 switch (reg) {
144 case WM8994_SOFTWARE_RESET:
145 case WM8994_CHIP_REVISION:
146 case WM8994_DC_SERVO_1:
147 case WM8994_DC_SERVO_READBACK:
148 case WM8994_RATE_STATUS:
149 case WM8994_LDO_1:
150 case WM8994_LDO_2:
Mark Brownd6addcc2010-11-26 15:21:08 +0000151 case WM8958_DSP2_EXECCONTROL:
Mark Brown821edd22010-11-26 15:21:09 +0000152 case WM8958_MIC_DETECT_3:
Mark Brown9e6e96a2010-01-29 17:47:12 +0000153 return 1;
154 default:
155 return 0;
156 }
157}
158
159static int wm8994_write(struct snd_soc_codec *codec, unsigned int reg,
160 unsigned int value)
161{
Mark Brownca9aef52010-11-26 17:23:41 +0000162 int ret;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000163
164 BUG_ON(reg > WM8994_MAX_REGISTER);
165
Mark Brownca9aef52010-11-26 17:23:41 +0000166 if (!wm8994_volatile(reg)) {
167 ret = snd_soc_cache_write(codec, reg, value);
168 if (ret != 0)
169 dev_err(codec->dev, "Cache write to %x failed: %d\n",
170 reg, ret);
171 }
Mark Brown9e6e96a2010-01-29 17:47:12 +0000172
173 return wm8994_reg_write(codec->control_data, reg, value);
174}
175
176static unsigned int wm8994_read(struct snd_soc_codec *codec,
177 unsigned int reg)
178{
Mark Brownca9aef52010-11-26 17:23:41 +0000179 unsigned int val;
180 int ret;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000181
182 BUG_ON(reg > WM8994_MAX_REGISTER);
183
Mark Brownca9aef52010-11-26 17:23:41 +0000184 if (!wm8994_volatile(reg) && wm8994_readable(reg) &&
185 reg < codec->driver->reg_cache_size) {
186 ret = snd_soc_cache_read(codec, reg, &val);
187 if (ret >= 0)
188 return val;
189 else
190 dev_err(codec->dev, "Cache read from %x failed: %d\n",
191 reg, ret);
192 }
193
194 return wm8994_reg_read(codec->control_data, reg);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000195}
196
197static int configure_aif_clock(struct snd_soc_codec *codec, int aif)
198{
Mark Brownb2c812e2010-04-14 15:35:19 +0900199 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000200 int rate;
201 int reg1 = 0;
202 int offset;
203
204 if (aif)
205 offset = 4;
206 else
207 offset = 0;
208
209 switch (wm8994->sysclk[aif]) {
210 case WM8994_SYSCLK_MCLK1:
211 rate = wm8994->mclk[0];
212 break;
213
214 case WM8994_SYSCLK_MCLK2:
215 reg1 |= 0x8;
216 rate = wm8994->mclk[1];
217 break;
218
219 case WM8994_SYSCLK_FLL1:
220 reg1 |= 0x10;
221 rate = wm8994->fll[0].out;
222 break;
223
224 case WM8994_SYSCLK_FLL2:
225 reg1 |= 0x18;
226 rate = wm8994->fll[1].out;
227 break;
228
229 default:
230 return -EINVAL;
231 }
232
233 if (rate >= 13500000) {
234 rate /= 2;
235 reg1 |= WM8994_AIF1CLK_DIV;
236
237 dev_dbg(codec->dev, "Dividing AIF%d clock to %dHz\n",
238 aif + 1, rate);
239 }
Mark Brown5e5e2be2010-04-25 12:20:30 +0100240
241 if (rate && rate < 3000000)
242 dev_warn(codec->dev, "AIF%dCLK is %dHz, should be >=3MHz for optimal performance\n",
243 aif + 1, rate);
244
Mark Brown9e6e96a2010-01-29 17:47:12 +0000245 wm8994->aifclk[aif] = rate;
246
247 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1 + offset,
248 WM8994_AIF1CLK_SRC_MASK | WM8994_AIF1CLK_DIV,
249 reg1);
250
251 return 0;
252}
253
254static int configure_clock(struct snd_soc_codec *codec)
255{
Mark Brownb2c812e2010-04-14 15:35:19 +0900256 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000257 int old, new;
258
259 /* Bring up the AIF clocks first */
260 configure_aif_clock(codec, 0);
261 configure_aif_clock(codec, 1);
262
263 /* Then switch CLK_SYS over to the higher of them; a change
264 * can only happen as a result of a clocking change which can
265 * only be made outside of DAPM so we can safely redo the
266 * clocking.
267 */
268
269 /* If they're equal it doesn't matter which is used */
270 if (wm8994->aifclk[0] == wm8994->aifclk[1])
271 return 0;
272
273 if (wm8994->aifclk[0] < wm8994->aifclk[1])
274 new = WM8994_SYSCLK_SRC;
275 else
276 new = 0;
277
278 old = snd_soc_read(codec, WM8994_CLOCKING_1) & WM8994_SYSCLK_SRC;
279
280 /* If there's no change then we're done. */
281 if (old == new)
282 return 0;
283
284 snd_soc_update_bits(codec, WM8994_CLOCKING_1, WM8994_SYSCLK_SRC, new);
285
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200286 snd_soc_dapm_sync(&codec->dapm);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000287
288 return 0;
289}
290
291static int check_clk_sys(struct snd_soc_dapm_widget *source,
292 struct snd_soc_dapm_widget *sink)
293{
294 int reg = snd_soc_read(source->codec, WM8994_CLOCKING_1);
295 const char *clk;
296
297 /* Check what we're currently using for CLK_SYS */
298 if (reg & WM8994_SYSCLK_SRC)
299 clk = "AIF2CLK";
300 else
301 clk = "AIF1CLK";
302
303 return strcmp(source->name, clk) == 0;
304}
305
306static const char *sidetone_hpf_text[] = {
307 "2.7kHz", "1.35kHz", "675Hz", "370Hz", "180Hz", "90Hz", "45Hz"
308};
309
310static const struct soc_enum sidetone_hpf =
311 SOC_ENUM_SINGLE(WM8994_SIDETONE, 7, 7, sidetone_hpf_text);
312
313static const DECLARE_TLV_DB_SCALE(aif_tlv, 0, 600, 0);
314static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
315static const DECLARE_TLV_DB_SCALE(st_tlv, -3600, 300, 0);
316static const DECLARE_TLV_DB_SCALE(wm8994_3d_tlv, -1600, 183, 0);
317static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
318
319#define WM8994_DRC_SWITCH(xname, reg, shift) \
320{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
321 .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,\
322 .put = wm8994_put_drc_sw, \
323 .private_value = SOC_SINGLE_VALUE(reg, shift, 1, 0) }
324
325static int wm8994_put_drc_sw(struct snd_kcontrol *kcontrol,
326 struct snd_ctl_elem_value *ucontrol)
327{
328 struct soc_mixer_control *mc =
329 (struct soc_mixer_control *)kcontrol->private_value;
330 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
331 int mask, ret;
332
333 /* Can't enable both ADC and DAC paths simultaneously */
334 if (mc->shift == WM8994_AIF1DAC1_DRC_ENA_SHIFT)
335 mask = WM8994_AIF1ADC1L_DRC_ENA_MASK |
336 WM8994_AIF1ADC1R_DRC_ENA_MASK;
337 else
338 mask = WM8994_AIF1DAC1_DRC_ENA_MASK;
339
340 ret = snd_soc_read(codec, mc->reg);
341 if (ret < 0)
342 return ret;
343 if (ret & mask)
344 return -EINVAL;
345
346 return snd_soc_put_volsw(kcontrol, ucontrol);
347}
348
Mark Brown9e6e96a2010-01-29 17:47:12 +0000349static void wm8994_set_drc(struct snd_soc_codec *codec, int drc)
350{
Mark Brownb2c812e2010-04-14 15:35:19 +0900351 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000352 struct wm8994_pdata *pdata = wm8994->pdata;
353 int base = wm8994_drc_base[drc];
354 int cfg = wm8994->drc_cfg[drc];
355 int save, i;
356
357 /* Save any enables; the configuration should clear them. */
358 save = snd_soc_read(codec, base);
359 save &= WM8994_AIF1DAC1_DRC_ENA | WM8994_AIF1ADC1L_DRC_ENA |
360 WM8994_AIF1ADC1R_DRC_ENA;
361
362 for (i = 0; i < WM8994_DRC_REGS; i++)
363 snd_soc_update_bits(codec, base + i, 0xffff,
364 pdata->drc_cfgs[cfg].regs[i]);
365
366 snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_DRC_ENA |
367 WM8994_AIF1ADC1L_DRC_ENA |
368 WM8994_AIF1ADC1R_DRC_ENA, save);
369}
370
371/* Icky as hell but saves code duplication */
372static int wm8994_get_drc(const char *name)
373{
374 if (strcmp(name, "AIF1DRC1 Mode") == 0)
375 return 0;
376 if (strcmp(name, "AIF1DRC2 Mode") == 0)
377 return 1;
378 if (strcmp(name, "AIF2DRC Mode") == 0)
379 return 2;
380 return -EINVAL;
381}
382
383static int wm8994_put_drc_enum(struct snd_kcontrol *kcontrol,
384 struct snd_ctl_elem_value *ucontrol)
385{
386 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000387 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000388 struct wm8994_pdata *pdata = wm8994->pdata;
389 int drc = wm8994_get_drc(kcontrol->id.name);
390 int value = ucontrol->value.integer.value[0];
391
392 if (drc < 0)
393 return drc;
394
395 if (value >= pdata->num_drc_cfgs)
396 return -EINVAL;
397
398 wm8994->drc_cfg[drc] = value;
399
400 wm8994_set_drc(codec, drc);
401
402 return 0;
403}
404
405static int wm8994_get_drc_enum(struct snd_kcontrol *kcontrol,
406 struct snd_ctl_elem_value *ucontrol)
407{
408 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +0900409 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000410 int drc = wm8994_get_drc(kcontrol->id.name);
411
412 ucontrol->value.enumerated.item[0] = wm8994->drc_cfg[drc];
413
414 return 0;
415}
416
417static void wm8994_set_retune_mobile(struct snd_soc_codec *codec, int block)
418{
Mark Brownb2c812e2010-04-14 15:35:19 +0900419 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000420 struct wm8994_pdata *pdata = wm8994->pdata;
421 int base = wm8994_retune_mobile_base[block];
422 int iface, best, best_val, save, i, cfg;
423
424 if (!pdata || !wm8994->num_retune_mobile_texts)
425 return;
426
427 switch (block) {
428 case 0:
429 case 1:
430 iface = 0;
431 break;
432 case 2:
433 iface = 1;
434 break;
435 default:
436 return;
437 }
438
439 /* Find the version of the currently selected configuration
440 * with the nearest sample rate. */
441 cfg = wm8994->retune_mobile_cfg[block];
442 best = 0;
443 best_val = INT_MAX;
444 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
445 if (strcmp(pdata->retune_mobile_cfgs[i].name,
446 wm8994->retune_mobile_texts[cfg]) == 0 &&
447 abs(pdata->retune_mobile_cfgs[i].rate
448 - wm8994->dac_rates[iface]) < best_val) {
449 best = i;
450 best_val = abs(pdata->retune_mobile_cfgs[i].rate
451 - wm8994->dac_rates[iface]);
452 }
453 }
454
455 dev_dbg(codec->dev, "ReTune Mobile %d %s/%dHz for %dHz sample rate\n",
456 block,
457 pdata->retune_mobile_cfgs[best].name,
458 pdata->retune_mobile_cfgs[best].rate,
459 wm8994->dac_rates[iface]);
460
461 /* The EQ will be disabled while reconfiguring it, remember the
462 * current configuration.
463 */
464 save = snd_soc_read(codec, base);
465 save &= WM8994_AIF1DAC1_EQ_ENA;
466
467 for (i = 0; i < WM8994_EQ_REGS; i++)
468 snd_soc_update_bits(codec, base + i, 0xffff,
469 pdata->retune_mobile_cfgs[best].regs[i]);
470
471 snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_EQ_ENA, save);
472}
473
474/* Icky as hell but saves code duplication */
475static int wm8994_get_retune_mobile_block(const char *name)
476{
477 if (strcmp(name, "AIF1.1 EQ Mode") == 0)
478 return 0;
479 if (strcmp(name, "AIF1.2 EQ Mode") == 0)
480 return 1;
481 if (strcmp(name, "AIF2 EQ Mode") == 0)
482 return 2;
483 return -EINVAL;
484}
485
486static int wm8994_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
487 struct snd_ctl_elem_value *ucontrol)
488{
489 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000490 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000491 struct wm8994_pdata *pdata = wm8994->pdata;
492 int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
493 int value = ucontrol->value.integer.value[0];
494
495 if (block < 0)
496 return block;
497
498 if (value >= pdata->num_retune_mobile_cfgs)
499 return -EINVAL;
500
501 wm8994->retune_mobile_cfg[block] = value;
502
503 wm8994_set_retune_mobile(codec, block);
504
505 return 0;
506}
507
508static int wm8994_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
509 struct snd_ctl_elem_value *ucontrol)
510{
511 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000512 struct wm8994_priv *wm8994 =snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000513 int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
514
515 ucontrol->value.enumerated.item[0] = wm8994->retune_mobile_cfg[block];
516
517 return 0;
518}
519
Mark Brown96b101e2010-11-18 15:49:38 +0000520static const char *aif_chan_src_text[] = {
Mark Brownf5548852010-08-31 19:39:48 +0100521 "Left", "Right"
522};
523
Mark Brown96b101e2010-11-18 15:49:38 +0000524static const struct soc_enum aif1adcl_src =
525 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_1, 15, 2, aif_chan_src_text);
526
527static const struct soc_enum aif1adcr_src =
528 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_1, 14, 2, aif_chan_src_text);
529
530static const struct soc_enum aif2adcl_src =
531 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_1, 15, 2, aif_chan_src_text);
532
533static const struct soc_enum aif2adcr_src =
534 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_1, 14, 2, aif_chan_src_text);
535
Mark Brownf5548852010-08-31 19:39:48 +0100536static const struct soc_enum aif1dacl_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000537 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_2, 15, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100538
539static const struct soc_enum aif1dacr_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000540 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_2, 14, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100541
542static const struct soc_enum aif2dacl_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000543 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_2, 15, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100544
545static const struct soc_enum aif2dacr_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000546 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_2, 14, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100547
Mark Brownd6addcc2010-11-26 15:21:08 +0000548static void wm8958_mbc_apply(struct snd_soc_codec *codec, int mbc, int start)
549{
550 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown131d8102010-11-30 17:03:39 +0000551 struct wm8994_pdata *pdata = wm8994->pdata;
Mark Brownd6addcc2010-11-26 15:21:08 +0000552 int pwr_reg = snd_soc_read(codec, WM8994_POWER_MANAGEMENT_5);
Mark Brown131d8102010-11-30 17:03:39 +0000553 int ena, reg, aif, i;
Mark Brownd6addcc2010-11-26 15:21:08 +0000554
555 switch (mbc) {
556 case 0:
557 pwr_reg &= (WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC1R_ENA);
558 aif = 0;
559 break;
560 case 1:
561 pwr_reg &= (WM8994_AIF1DAC2L_ENA | WM8994_AIF1DAC2R_ENA);
562 aif = 0;
563 break;
564 case 2:
565 pwr_reg &= (WM8994_AIF2DACL_ENA | WM8994_AIF2DACR_ENA);
566 aif = 1;
567 break;
568 default:
569 BUG();
570 return;
571 }
572
573 /* We can only enable the MBC if the AIF is enabled and we
574 * want it to be enabled. */
575 ena = pwr_reg && wm8994->mbc_ena[mbc];
576
577 reg = snd_soc_read(codec, WM8958_DSP2_PROGRAM);
578
579 dev_dbg(codec->dev, "MBC %d startup: %d, power: %x, DSP: %x\n",
580 mbc, start, pwr_reg, reg);
581
582 if (start && ena) {
583 /* If the DSP is already running then noop */
584 if (reg & WM8958_DSP2_ENA)
585 return;
586
587 /* Switch the clock over to the appropriate AIF */
588 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
589 WM8958_DSP2CLK_SRC | WM8958_DSP2CLK_ENA,
590 aif << WM8958_DSP2CLK_SRC_SHIFT |
591 WM8958_DSP2CLK_ENA);
592
593 snd_soc_update_bits(codec, WM8958_DSP2_PROGRAM,
594 WM8958_DSP2_ENA, WM8958_DSP2_ENA);
595
Mark Brown131d8102010-11-30 17:03:39 +0000596 /* If we've got user supplied MBC settings use them */
597 if (pdata && pdata->num_mbc_cfgs) {
598 struct wm8958_mbc_cfg *cfg
599 = &pdata->mbc_cfgs[wm8994->mbc_cfg];
600
601 for (i = 0; i < ARRAY_SIZE(cfg->coeff_regs); i++)
602 snd_soc_write(codec, i + WM8958_MBC_BAND_1_K_1,
603 cfg->coeff_regs[i]);
604
605 for (i = 0; i < ARRAY_SIZE(cfg->cutoff_regs); i++)
606 snd_soc_write(codec,
607 i + WM8958_MBC_BAND_2_LOWER_CUTOFF_C1_1,
608 cfg->cutoff_regs[i]);
609 }
Mark Brownd6addcc2010-11-26 15:21:08 +0000610
611 /* Run the DSP */
612 snd_soc_write(codec, WM8958_DSP2_EXECCONTROL,
613 WM8958_DSP2_RUNR);
614
615 /* And we're off! */
616 snd_soc_update_bits(codec, WM8958_DSP2_CONFIG,
617 WM8958_MBC_ENA | WM8958_MBC_SEL_MASK,
618 mbc << WM8958_MBC_SEL_SHIFT |
619 WM8958_MBC_ENA);
620 } else {
621 /* If the DSP is already stopped then noop */
622 if (!(reg & WM8958_DSP2_ENA))
623 return;
624
625 snd_soc_update_bits(codec, WM8958_DSP2_CONFIG,
626 WM8958_MBC_ENA, 0);
627 snd_soc_update_bits(codec, WM8958_DSP2_PROGRAM,
628 WM8958_DSP2_ENA, 0);
629 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
630 WM8958_DSP2CLK_ENA, 0);
631 }
632}
633
634static int wm8958_aif_ev(struct snd_soc_dapm_widget *w,
635 struct snd_kcontrol *kcontrol, int event)
636{
637 struct snd_soc_codec *codec = w->codec;
638 int mbc;
639
640 switch (w->shift) {
641 case 13:
642 case 12:
643 mbc = 2;
644 break;
645 case 11:
646 case 10:
647 mbc = 1;
648 break;
649 case 9:
650 case 8:
651 mbc = 0;
652 break;
653 default:
654 BUG();
655 return -EINVAL;
656 }
657
658 switch (event) {
659 case SND_SOC_DAPM_POST_PMU:
660 wm8958_mbc_apply(codec, mbc, 1);
661 break;
662 case SND_SOC_DAPM_POST_PMD:
663 wm8958_mbc_apply(codec, mbc, 0);
664 break;
665 }
666
667 return 0;
668}
669
Mark Brown131d8102010-11-30 17:03:39 +0000670static int wm8958_put_mbc_enum(struct snd_kcontrol *kcontrol,
671 struct snd_ctl_elem_value *ucontrol)
672{
673 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
674 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
675 struct wm8994_pdata *pdata = wm8994->pdata;
676 int value = ucontrol->value.integer.value[0];
677 int reg;
678
679 /* Don't allow on the fly reconfiguration */
680 reg = snd_soc_read(codec, WM8994_CLOCKING_1);
681 if (reg < 0 || reg & WM8958_DSP2CLK_ENA)
682 return -EBUSY;
683
684 if (value >= pdata->num_mbc_cfgs)
685 return -EINVAL;
686
687 wm8994->mbc_cfg = value;
688
689 return 0;
690}
691
692static int wm8958_get_mbc_enum(struct snd_kcontrol *kcontrol,
693 struct snd_ctl_elem_value *ucontrol)
694{
695 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
696 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
697
698 ucontrol->value.enumerated.item[0] = wm8994->mbc_cfg;
699
700 return 0;
701}
702
Mark Brownd6addcc2010-11-26 15:21:08 +0000703static int wm8958_mbc_info(struct snd_kcontrol *kcontrol,
704 struct snd_ctl_elem_info *uinfo)
705{
706 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
707 uinfo->count = 1;
708 uinfo->value.integer.min = 0;
709 uinfo->value.integer.max = 1;
710 return 0;
711}
712
713static int wm8958_mbc_get(struct snd_kcontrol *kcontrol,
714 struct snd_ctl_elem_value *ucontrol)
715{
716 int mbc = kcontrol->private_value;
717 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
718 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
719
720 ucontrol->value.integer.value[0] = wm8994->mbc_ena[mbc];
721
722 return 0;
723}
724
725static int wm8958_mbc_put(struct snd_kcontrol *kcontrol,
726 struct snd_ctl_elem_value *ucontrol)
727{
728 int mbc = kcontrol->private_value;
729 int i;
730 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
731 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
732
733 if (ucontrol->value.integer.value[0] > 1)
734 return -EINVAL;
735
736 for (i = 0; i < ARRAY_SIZE(wm8994->mbc_ena); i++) {
737 if (mbc != i && wm8994->mbc_ena[i]) {
738 dev_dbg(codec->dev, "MBC %d active already\n", mbc);
739 return -EBUSY;
740 }
741 }
742
743 wm8994->mbc_ena[mbc] = ucontrol->value.integer.value[0];
744
745 wm8958_mbc_apply(codec, mbc, wm8994->mbc_ena[mbc]);
746
747 return 0;
748}
749
750#define WM8958_MBC_SWITCH(xname, xval) {\
751 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
752 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,\
753 .info = wm8958_mbc_info, \
754 .get = wm8958_mbc_get, .put = wm8958_mbc_put, \
755 .private_value = xval }
756
Mark Brown9e6e96a2010-01-29 17:47:12 +0000757static const struct snd_kcontrol_new wm8994_snd_controls[] = {
758SOC_DOUBLE_R_TLV("AIF1ADC1 Volume", WM8994_AIF1_ADC1_LEFT_VOLUME,
759 WM8994_AIF1_ADC1_RIGHT_VOLUME,
760 1, 119, 0, digital_tlv),
761SOC_DOUBLE_R_TLV("AIF1ADC2 Volume", WM8994_AIF1_ADC2_LEFT_VOLUME,
762 WM8994_AIF1_ADC2_RIGHT_VOLUME,
763 1, 119, 0, digital_tlv),
764SOC_DOUBLE_R_TLV("AIF2ADC Volume", WM8994_AIF2_ADC_LEFT_VOLUME,
765 WM8994_AIF2_ADC_RIGHT_VOLUME,
766 1, 119, 0, digital_tlv),
767
Mark Brown96b101e2010-11-18 15:49:38 +0000768SOC_ENUM("AIF1ADCL Source", aif1adcl_src),
769SOC_ENUM("AIF1ADCR Source", aif1adcr_src),
770SOC_ENUM("AIF2ADCL Source", aif1adcl_src),
771SOC_ENUM("AIF2ADCR Source", aif1adcr_src),
772
Mark Brownf5548852010-08-31 19:39:48 +0100773SOC_ENUM("AIF1DACL Source", aif1dacl_src),
774SOC_ENUM("AIF1DACR Source", aif1dacr_src),
775SOC_ENUM("AIF2DACL Source", aif1dacl_src),
776SOC_ENUM("AIF2DACR Source", aif1dacr_src),
777
Mark Brown9e6e96a2010-01-29 17:47:12 +0000778SOC_DOUBLE_R_TLV("AIF1DAC1 Volume", WM8994_AIF1_DAC1_LEFT_VOLUME,
779 WM8994_AIF1_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
780SOC_DOUBLE_R_TLV("AIF1DAC2 Volume", WM8994_AIF1_DAC2_LEFT_VOLUME,
781 WM8994_AIF1_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
782SOC_DOUBLE_R_TLV("AIF2DAC Volume", WM8994_AIF2_DAC_LEFT_VOLUME,
783 WM8994_AIF2_DAC_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
784
785SOC_SINGLE_TLV("AIF1 Boost Volume", WM8994_AIF1_CONTROL_2, 10, 3, 0, aif_tlv),
786SOC_SINGLE_TLV("AIF2 Boost Volume", WM8994_AIF2_CONTROL_2, 10, 3, 0, aif_tlv),
787
788SOC_SINGLE("AIF1DAC1 EQ Switch", WM8994_AIF1_DAC1_EQ_GAINS_1, 0, 1, 0),
789SOC_SINGLE("AIF1DAC2 EQ Switch", WM8994_AIF1_DAC2_EQ_GAINS_1, 0, 1, 0),
790SOC_SINGLE("AIF2 EQ Switch", WM8994_AIF2_EQ_GAINS_1, 0, 1, 0),
791
792WM8994_DRC_SWITCH("AIF1DAC1 DRC Switch", WM8994_AIF1_DRC1_1, 2),
793WM8994_DRC_SWITCH("AIF1ADC1L DRC Switch", WM8994_AIF1_DRC1_1, 1),
794WM8994_DRC_SWITCH("AIF1ADC1R DRC Switch", WM8994_AIF1_DRC1_1, 0),
795
796WM8994_DRC_SWITCH("AIF1DAC2 DRC Switch", WM8994_AIF1_DRC2_1, 2),
797WM8994_DRC_SWITCH("AIF1ADC2L DRC Switch", WM8994_AIF1_DRC2_1, 1),
798WM8994_DRC_SWITCH("AIF1ADC2R DRC Switch", WM8994_AIF1_DRC2_1, 0),
799
800WM8994_DRC_SWITCH("AIF2DAC DRC Switch", WM8994_AIF2_DRC_1, 2),
801WM8994_DRC_SWITCH("AIF2ADCL DRC Switch", WM8994_AIF2_DRC_1, 1),
802WM8994_DRC_SWITCH("AIF2ADCR DRC Switch", WM8994_AIF2_DRC_1, 0),
803
804SOC_SINGLE_TLV("DAC1 Right Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
805 5, 12, 0, st_tlv),
806SOC_SINGLE_TLV("DAC1 Left Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
807 0, 12, 0, st_tlv),
808SOC_SINGLE_TLV("DAC2 Right Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
809 5, 12, 0, st_tlv),
810SOC_SINGLE_TLV("DAC2 Left Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
811 0, 12, 0, st_tlv),
812SOC_ENUM("Sidetone HPF Mux", sidetone_hpf),
813SOC_SINGLE("Sidetone HPF Switch", WM8994_SIDETONE, 6, 1, 0),
814
815SOC_DOUBLE_R_TLV("DAC1 Volume", WM8994_DAC1_LEFT_VOLUME,
816 WM8994_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
817SOC_DOUBLE_R("DAC1 Switch", WM8994_DAC1_LEFT_VOLUME,
818 WM8994_DAC1_RIGHT_VOLUME, 9, 1, 1),
819
820SOC_DOUBLE_R_TLV("DAC2 Volume", WM8994_DAC2_LEFT_VOLUME,
821 WM8994_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
822SOC_DOUBLE_R("DAC2 Switch", WM8994_DAC2_LEFT_VOLUME,
823 WM8994_DAC2_RIGHT_VOLUME, 9, 1, 1),
824
825SOC_SINGLE_TLV("SPKL DAC2 Volume", WM8994_SPKMIXL_ATTENUATION,
826 6, 1, 1, wm_hubs_spkmix_tlv),
827SOC_SINGLE_TLV("SPKL DAC1 Volume", WM8994_SPKMIXL_ATTENUATION,
828 2, 1, 1, wm_hubs_spkmix_tlv),
829
830SOC_SINGLE_TLV("SPKR DAC2 Volume", WM8994_SPKMIXR_ATTENUATION,
831 6, 1, 1, wm_hubs_spkmix_tlv),
832SOC_SINGLE_TLV("SPKR DAC1 Volume", WM8994_SPKMIXR_ATTENUATION,
833 2, 1, 1, wm_hubs_spkmix_tlv),
834
835SOC_SINGLE_TLV("AIF1DAC1 3D Stereo Volume", WM8994_AIF1_DAC1_FILTERS_2,
836 10, 15, 0, wm8994_3d_tlv),
837SOC_SINGLE("AIF1DAC1 3D Stereo Switch", WM8994_AIF1_DAC2_FILTERS_2,
838 8, 1, 0),
839SOC_SINGLE_TLV("AIF1DAC2 3D Stereo Volume", WM8994_AIF1_DAC2_FILTERS_2,
840 10, 15, 0, wm8994_3d_tlv),
841SOC_SINGLE("AIF1DAC2 3D Stereo Switch", WM8994_AIF1_DAC2_FILTERS_2,
842 8, 1, 0),
843SOC_SINGLE_TLV("AIF2DAC 3D Stereo Volume", WM8994_AIF1_DAC1_FILTERS_2,
844 10, 15, 0, wm8994_3d_tlv),
845SOC_SINGLE("AIF2DAC 3D Stereo Switch", WM8994_AIF1_DAC2_FILTERS_2,
846 8, 1, 0),
847};
848
849static const struct snd_kcontrol_new wm8994_eq_controls[] = {
850SOC_SINGLE_TLV("AIF1DAC1 EQ1 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 11, 31, 0,
851 eq_tlv),
852SOC_SINGLE_TLV("AIF1DAC1 EQ2 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 6, 31, 0,
853 eq_tlv),
854SOC_SINGLE_TLV("AIF1DAC1 EQ3 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 1, 31, 0,
855 eq_tlv),
856SOC_SINGLE_TLV("AIF1DAC1 EQ4 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 11, 31, 0,
857 eq_tlv),
858SOC_SINGLE_TLV("AIF1DAC1 EQ5 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 6, 31, 0,
859 eq_tlv),
860
861SOC_SINGLE_TLV("AIF1DAC2 EQ1 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 11, 31, 0,
862 eq_tlv),
863SOC_SINGLE_TLV("AIF1DAC2 EQ2 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 6, 31, 0,
864 eq_tlv),
865SOC_SINGLE_TLV("AIF1DAC2 EQ3 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 1, 31, 0,
866 eq_tlv),
867SOC_SINGLE_TLV("AIF1DAC2 EQ4 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 11, 31, 0,
868 eq_tlv),
869SOC_SINGLE_TLV("AIF1DAC2 EQ5 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 6, 31, 0,
870 eq_tlv),
871
872SOC_SINGLE_TLV("AIF2 EQ1 Volume", WM8994_AIF2_EQ_GAINS_1, 11, 31, 0,
873 eq_tlv),
874SOC_SINGLE_TLV("AIF2 EQ2 Volume", WM8994_AIF2_EQ_GAINS_1, 6, 31, 0,
875 eq_tlv),
876SOC_SINGLE_TLV("AIF2 EQ3 Volume", WM8994_AIF2_EQ_GAINS_1, 1, 31, 0,
877 eq_tlv),
878SOC_SINGLE_TLV("AIF2 EQ4 Volume", WM8994_AIF2_EQ_GAINS_2, 11, 31, 0,
879 eq_tlv),
880SOC_SINGLE_TLV("AIF2 EQ5 Volume", WM8994_AIF2_EQ_GAINS_2, 6, 31, 0,
881 eq_tlv),
882};
883
Mark Brownc4431df2010-11-26 15:21:07 +0000884static const struct snd_kcontrol_new wm8958_snd_controls[] = {
885SOC_SINGLE_TLV("AIF3 Boost Volume", WM8958_AIF3_CONTROL_2, 10, 3, 0, aif_tlv),
Mark Brownd6addcc2010-11-26 15:21:08 +0000886WM8958_MBC_SWITCH("AIF1DAC1 MBC Switch", 0),
887WM8958_MBC_SWITCH("AIF1DAC2 MBC Switch", 1),
888WM8958_MBC_SWITCH("AIF2DAC MBC Switch", 2),
Mark Brownc4431df2010-11-26 15:21:07 +0000889};
890
Mark Brown9e6e96a2010-01-29 17:47:12 +0000891static int clk_sys_event(struct snd_soc_dapm_widget *w,
892 struct snd_kcontrol *kcontrol, int event)
893{
894 struct snd_soc_codec *codec = w->codec;
895
896 switch (event) {
897 case SND_SOC_DAPM_PRE_PMU:
898 return configure_clock(codec);
899
900 case SND_SOC_DAPM_POST_PMD:
901 configure_clock(codec);
902 break;
903 }
904
905 return 0;
906}
907
908static void wm8994_update_class_w(struct snd_soc_codec *codec)
909{
Mark Brownfec6dd82010-10-27 13:48:36 -0700910 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000911 int enable = 1;
912 int source = 0; /* GCC flow analysis can't track enable */
913 int reg, reg_r;
914
915 /* Only support direct DAC->headphone paths */
916 reg = snd_soc_read(codec, WM8994_OUTPUT_MIXER_1);
917 if (!(reg & WM8994_DAC1L_TO_HPOUT1L)) {
Mark Brownee839a22010-04-20 13:57:08 +0900918 dev_vdbg(codec->dev, "HPL connected to output mixer\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000919 enable = 0;
920 }
921
922 reg = snd_soc_read(codec, WM8994_OUTPUT_MIXER_2);
923 if (!(reg & WM8994_DAC1R_TO_HPOUT1R)) {
Mark Brownee839a22010-04-20 13:57:08 +0900924 dev_vdbg(codec->dev, "HPR connected to output mixer\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000925 enable = 0;
926 }
927
928 /* We also need the same setting for L/R and only one path */
929 reg = snd_soc_read(codec, WM8994_DAC1_LEFT_MIXER_ROUTING);
930 switch (reg) {
931 case WM8994_AIF2DACL_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +0900932 dev_vdbg(codec->dev, "Class W source AIF2DAC\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000933 source = 2 << WM8994_CP_DYN_SRC_SEL_SHIFT;
934 break;
935 case WM8994_AIF1DAC2L_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +0900936 dev_vdbg(codec->dev, "Class W source AIF1DAC2\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000937 source = 1 << WM8994_CP_DYN_SRC_SEL_SHIFT;
938 break;
939 case WM8994_AIF1DAC1L_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +0900940 dev_vdbg(codec->dev, "Class W source AIF1DAC1\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000941 source = 0 << WM8994_CP_DYN_SRC_SEL_SHIFT;
942 break;
943 default:
Mark Brownee839a22010-04-20 13:57:08 +0900944 dev_vdbg(codec->dev, "DAC mixer setting: %x\n", reg);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000945 enable = 0;
946 break;
947 }
948
949 reg_r = snd_soc_read(codec, WM8994_DAC1_RIGHT_MIXER_ROUTING);
950 if (reg_r != reg) {
Mark Brownee839a22010-04-20 13:57:08 +0900951 dev_vdbg(codec->dev, "Left and right DAC mixers different\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000952 enable = 0;
953 }
954
955 if (enable) {
956 dev_dbg(codec->dev, "Class W enabled\n");
957 snd_soc_update_bits(codec, WM8994_CLASS_W_1,
958 WM8994_CP_DYN_PWR |
959 WM8994_CP_DYN_SRC_SEL_MASK,
960 source | WM8994_CP_DYN_PWR);
Mark Brownfec6dd82010-10-27 13:48:36 -0700961 wm8994->hubs.class_w = true;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000962
963 } else {
964 dev_dbg(codec->dev, "Class W disabled\n");
965 snd_soc_update_bits(codec, WM8994_CLASS_W_1,
966 WM8994_CP_DYN_PWR, 0);
Mark Brownfec6dd82010-10-27 13:48:36 -0700967 wm8994->hubs.class_w = false;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000968 }
969}
970
971static const char *hp_mux_text[] = {
972 "Mixer",
973 "DAC",
974};
975
976#define WM8994_HP_ENUM(xname, xenum) \
977{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
978 .info = snd_soc_info_enum_double, \
979 .get = snd_soc_dapm_get_enum_double, \
980 .put = wm8994_put_hp_enum, \
981 .private_value = (unsigned long)&xenum }
982
983static int wm8994_put_hp_enum(struct snd_kcontrol *kcontrol,
984 struct snd_ctl_elem_value *ucontrol)
985{
986 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
987 struct snd_soc_codec *codec = w->codec;
988 int ret;
989
990 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
991
992 wm8994_update_class_w(codec);
993
994 return ret;
995}
996
997static const struct soc_enum hpl_enum =
998 SOC_ENUM_SINGLE(WM8994_OUTPUT_MIXER_1, 8, 2, hp_mux_text);
999
1000static const struct snd_kcontrol_new hpl_mux =
1001 WM8994_HP_ENUM("Left Headphone Mux", hpl_enum);
1002
1003static const struct soc_enum hpr_enum =
1004 SOC_ENUM_SINGLE(WM8994_OUTPUT_MIXER_2, 8, 2, hp_mux_text);
1005
1006static const struct snd_kcontrol_new hpr_mux =
1007 WM8994_HP_ENUM("Right Headphone Mux", hpr_enum);
1008
1009static const char *adc_mux_text[] = {
1010 "ADC",
1011 "DMIC",
1012};
1013
1014static const struct soc_enum adc_enum =
1015 SOC_ENUM_SINGLE(0, 0, 2, adc_mux_text);
1016
1017static const struct snd_kcontrol_new adcl_mux =
1018 SOC_DAPM_ENUM_VIRT("ADCL Mux", adc_enum);
1019
1020static const struct snd_kcontrol_new adcr_mux =
1021 SOC_DAPM_ENUM_VIRT("ADCR Mux", adc_enum);
1022
1023static const struct snd_kcontrol_new left_speaker_mixer[] = {
1024SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 9, 1, 0),
1025SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 7, 1, 0),
1026SOC_DAPM_SINGLE("IN1LP Switch", WM8994_SPEAKER_MIXER, 5, 1, 0),
1027SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 3, 1, 0),
1028SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 1, 1, 0),
1029};
1030
1031static const struct snd_kcontrol_new right_speaker_mixer[] = {
1032SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 8, 1, 0),
1033SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 6, 1, 0),
1034SOC_DAPM_SINGLE("IN1RP Switch", WM8994_SPEAKER_MIXER, 4, 1, 0),
1035SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 2, 1, 0),
1036SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 0, 1, 0),
1037};
1038
1039/* Debugging; dump chip status after DAPM transitions */
1040static int post_ev(struct snd_soc_dapm_widget *w,
1041 struct snd_kcontrol *kcontrol, int event)
1042{
1043 struct snd_soc_codec *codec = w->codec;
1044 dev_dbg(codec->dev, "SRC status: %x\n",
1045 snd_soc_read(codec,
1046 WM8994_RATE_STATUS));
1047 return 0;
1048}
1049
1050static const struct snd_kcontrol_new aif1adc1l_mix[] = {
1051SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1052 1, 1, 0),
1053SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1054 0, 1, 0),
1055};
1056
1057static const struct snd_kcontrol_new aif1adc1r_mix[] = {
1058SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1059 1, 1, 0),
1060SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1061 0, 1, 0),
1062};
1063
Mark Browna3257ba2010-07-19 14:02:34 +01001064static const struct snd_kcontrol_new aif1adc2l_mix[] = {
1065SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1066 1, 1, 0),
1067SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1068 0, 1, 0),
1069};
1070
1071static const struct snd_kcontrol_new aif1adc2r_mix[] = {
1072SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1073 1, 1, 0),
1074SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1075 0, 1, 0),
1076};
1077
Mark Brown9e6e96a2010-01-29 17:47:12 +00001078static const struct snd_kcontrol_new aif2dac2l_mix[] = {
1079SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1080 5, 1, 0),
1081SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1082 4, 1, 0),
1083SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1084 2, 1, 0),
1085SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1086 1, 1, 0),
1087SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1088 0, 1, 0),
1089};
1090
1091static const struct snd_kcontrol_new aif2dac2r_mix[] = {
1092SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1093 5, 1, 0),
1094SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1095 4, 1, 0),
1096SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1097 2, 1, 0),
1098SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1099 1, 1, 0),
1100SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1101 0, 1, 0),
1102};
1103
1104#define WM8994_CLASS_W_SWITCH(xname, reg, shift, max, invert) \
1105{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1106 .info = snd_soc_info_volsw, \
1107 .get = snd_soc_dapm_get_volsw, .put = wm8994_put_class_w, \
1108 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
1109
1110static int wm8994_put_class_w(struct snd_kcontrol *kcontrol,
1111 struct snd_ctl_elem_value *ucontrol)
1112{
1113 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
1114 struct snd_soc_codec *codec = w->codec;
1115 int ret;
1116
1117 ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
1118
1119 wm8994_update_class_w(codec);
1120
1121 return ret;
1122}
1123
1124static const struct snd_kcontrol_new dac1l_mix[] = {
1125WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1126 5, 1, 0),
1127WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1128 4, 1, 0),
1129WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1130 2, 1, 0),
1131WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1132 1, 1, 0),
1133WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1134 0, 1, 0),
1135};
1136
1137static const struct snd_kcontrol_new dac1r_mix[] = {
1138WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1139 5, 1, 0),
1140WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1141 4, 1, 0),
1142WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1143 2, 1, 0),
1144WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1145 1, 1, 0),
1146WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1147 0, 1, 0),
1148};
1149
1150static const char *sidetone_text[] = {
1151 "ADC/DMIC1", "DMIC2",
1152};
1153
1154static const struct soc_enum sidetone1_enum =
1155 SOC_ENUM_SINGLE(WM8994_SIDETONE, 0, 2, sidetone_text);
1156
1157static const struct snd_kcontrol_new sidetone1_mux =
1158 SOC_DAPM_ENUM("Left Sidetone Mux", sidetone1_enum);
1159
1160static const struct soc_enum sidetone2_enum =
1161 SOC_ENUM_SINGLE(WM8994_SIDETONE, 1, 2, sidetone_text);
1162
1163static const struct snd_kcontrol_new sidetone2_mux =
1164 SOC_DAPM_ENUM("Right Sidetone Mux", sidetone2_enum);
1165
1166static const char *aif1dac_text[] = {
1167 "AIF1DACDAT", "AIF3DACDAT",
1168};
1169
1170static const struct soc_enum aif1dac_enum =
1171 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 0, 2, aif1dac_text);
1172
1173static const struct snd_kcontrol_new aif1dac_mux =
1174 SOC_DAPM_ENUM("AIF1DAC Mux", aif1dac_enum);
1175
1176static const char *aif2dac_text[] = {
1177 "AIF2DACDAT", "AIF3DACDAT",
1178};
1179
1180static const struct soc_enum aif2dac_enum =
1181 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 1, 2, aif2dac_text);
1182
1183static const struct snd_kcontrol_new aif2dac_mux =
1184 SOC_DAPM_ENUM("AIF2DAC Mux", aif2dac_enum);
1185
1186static const char *aif2adc_text[] = {
1187 "AIF2ADCDAT", "AIF3DACDAT",
1188};
1189
1190static const struct soc_enum aif2adc_enum =
1191 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 2, 2, aif2adc_text);
1192
1193static const struct snd_kcontrol_new aif2adc_mux =
1194 SOC_DAPM_ENUM("AIF2ADC Mux", aif2adc_enum);
1195
1196static const char *aif3adc_text[] = {
Mark Brownc4431df2010-11-26 15:21:07 +00001197 "AIF1ADCDAT", "AIF2ADCDAT", "AIF2DACDAT", "Mono PCM",
Mark Brown9e6e96a2010-01-29 17:47:12 +00001198};
1199
Mark Brownc4431df2010-11-26 15:21:07 +00001200static const struct soc_enum wm8994_aif3adc_enum =
Mark Brown9e6e96a2010-01-29 17:47:12 +00001201 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 3, 3, aif3adc_text);
1202
Mark Brownc4431df2010-11-26 15:21:07 +00001203static const struct snd_kcontrol_new wm8994_aif3adc_mux =
1204 SOC_DAPM_ENUM("AIF3ADC Mux", wm8994_aif3adc_enum);
1205
1206static const struct soc_enum wm8958_aif3adc_enum =
1207 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 3, 4, aif3adc_text);
1208
1209static const struct snd_kcontrol_new wm8958_aif3adc_mux =
1210 SOC_DAPM_ENUM("AIF3ADC Mux", wm8958_aif3adc_enum);
1211
1212static const char *mono_pcm_out_text[] = {
1213 "None", "AIF2ADCL", "AIF2ADCR",
1214};
1215
1216static const struct soc_enum mono_pcm_out_enum =
1217 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 9, 3, mono_pcm_out_text);
1218
1219static const struct snd_kcontrol_new mono_pcm_out_mux =
1220 SOC_DAPM_ENUM("Mono PCM Out Mux", mono_pcm_out_enum);
1221
1222static const char *aif2dac_src_text[] = {
1223 "AIF2", "AIF3",
1224};
1225
1226/* Note that these two control shouldn't be simultaneously switched to AIF3 */
1227static const struct soc_enum aif2dacl_src_enum =
1228 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 7, 2, aif2dac_src_text);
1229
1230static const struct snd_kcontrol_new aif2dacl_src_mux =
1231 SOC_DAPM_ENUM("AIF2DACL Mux", aif2dacl_src_enum);
1232
1233static const struct soc_enum aif2dacr_src_enum =
1234 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 8, 2, aif2dac_src_text);
1235
1236static const struct snd_kcontrol_new aif2dacr_src_mux =
1237 SOC_DAPM_ENUM("AIF2DACR Mux", aif2dacr_src_enum);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001238
1239static const struct snd_soc_dapm_widget wm8994_dapm_widgets[] = {
1240SND_SOC_DAPM_INPUT("DMIC1DAT"),
1241SND_SOC_DAPM_INPUT("DMIC2DAT"),
Mark Brown66b47fd2010-07-08 11:25:43 +09001242SND_SOC_DAPM_INPUT("Clock"),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001243
1244SND_SOC_DAPM_SUPPLY("CLK_SYS", SND_SOC_NOPM, 0, 0, clk_sys_event,
1245 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1246
1247SND_SOC_DAPM_SUPPLY("DSP1CLK", WM8994_CLOCKING_1, 3, 0, NULL, 0),
1248SND_SOC_DAPM_SUPPLY("DSP2CLK", WM8994_CLOCKING_1, 2, 0, NULL, 0),
1249SND_SOC_DAPM_SUPPLY("DSPINTCLK", WM8994_CLOCKING_1, 1, 0, NULL, 0),
1250
1251SND_SOC_DAPM_SUPPLY("AIF1CLK", WM8994_AIF1_CLOCKING_1, 0, 0, NULL, 0),
1252SND_SOC_DAPM_SUPPLY("AIF2CLK", WM8994_AIF2_CLOCKING_1, 0, 0, NULL, 0),
1253
1254SND_SOC_DAPM_AIF_OUT("AIF1ADC1L", "AIF1 Capture",
1255 0, WM8994_POWER_MANAGEMENT_4, 9, 0),
1256SND_SOC_DAPM_AIF_OUT("AIF1ADC1R", "AIF1 Capture",
1257 0, WM8994_POWER_MANAGEMENT_4, 8, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001258SND_SOC_DAPM_AIF_IN_E("AIF1DAC1L", NULL, 0,
1259 WM8994_POWER_MANAGEMENT_5, 9, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001260 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brownd6addcc2010-11-26 15:21:08 +00001261SND_SOC_DAPM_AIF_IN_E("AIF1DAC1R", NULL, 0,
1262 WM8994_POWER_MANAGEMENT_5, 8, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001263 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001264
1265SND_SOC_DAPM_AIF_OUT("AIF1ADC2L", "AIF1 Capture",
1266 0, WM8994_POWER_MANAGEMENT_4, 11, 0),
1267SND_SOC_DAPM_AIF_OUT("AIF1ADC2R", "AIF1 Capture",
1268 0, WM8994_POWER_MANAGEMENT_4, 10, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001269SND_SOC_DAPM_AIF_IN_E("AIF1DAC2L", NULL, 0,
1270 WM8994_POWER_MANAGEMENT_5, 11, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001271 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brownd6addcc2010-11-26 15:21:08 +00001272SND_SOC_DAPM_AIF_IN_E("AIF1DAC2R", NULL, 0,
1273 WM8994_POWER_MANAGEMENT_5, 10, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001274 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001275
1276SND_SOC_DAPM_MIXER("AIF1ADC1L Mixer", SND_SOC_NOPM, 0, 0,
1277 aif1adc1l_mix, ARRAY_SIZE(aif1adc1l_mix)),
1278SND_SOC_DAPM_MIXER("AIF1ADC1R Mixer", SND_SOC_NOPM, 0, 0,
1279 aif1adc1r_mix, ARRAY_SIZE(aif1adc1r_mix)),
1280
Mark Browna3257ba2010-07-19 14:02:34 +01001281SND_SOC_DAPM_MIXER("AIF1ADC2L Mixer", SND_SOC_NOPM, 0, 0,
1282 aif1adc2l_mix, ARRAY_SIZE(aif1adc2l_mix)),
1283SND_SOC_DAPM_MIXER("AIF1ADC2R Mixer", SND_SOC_NOPM, 0, 0,
1284 aif1adc2r_mix, ARRAY_SIZE(aif1adc2r_mix)),
1285
Mark Brown9e6e96a2010-01-29 17:47:12 +00001286SND_SOC_DAPM_MIXER("AIF2DAC2L Mixer", SND_SOC_NOPM, 0, 0,
1287 aif2dac2l_mix, ARRAY_SIZE(aif2dac2l_mix)),
1288SND_SOC_DAPM_MIXER("AIF2DAC2R Mixer", SND_SOC_NOPM, 0, 0,
1289 aif2dac2r_mix, ARRAY_SIZE(aif2dac2r_mix)),
1290
1291SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &sidetone1_mux),
1292SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &sidetone2_mux),
1293
1294SND_SOC_DAPM_MIXER("DAC1L Mixer", SND_SOC_NOPM, 0, 0,
1295 dac1l_mix, ARRAY_SIZE(dac1l_mix)),
1296SND_SOC_DAPM_MIXER("DAC1R Mixer", SND_SOC_NOPM, 0, 0,
1297 dac1r_mix, ARRAY_SIZE(dac1r_mix)),
1298
1299SND_SOC_DAPM_AIF_OUT("AIF2ADCL", NULL, 0,
1300 WM8994_POWER_MANAGEMENT_4, 13, 0),
1301SND_SOC_DAPM_AIF_OUT("AIF2ADCR", NULL, 0,
1302 WM8994_POWER_MANAGEMENT_4, 12, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001303SND_SOC_DAPM_AIF_IN_E("AIF2DACL", NULL, 0,
1304 WM8994_POWER_MANAGEMENT_5, 13, 0, wm8958_aif_ev,
1305 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1306SND_SOC_DAPM_AIF_IN_E("AIF2DACR", NULL, 0,
1307 WM8994_POWER_MANAGEMENT_5, 12, 0, wm8958_aif_ev,
1308 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001309
1310SND_SOC_DAPM_AIF_IN("AIF1DACDAT", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
1311SND_SOC_DAPM_AIF_IN("AIF2DACDAT", "AIF2 Playback", 0, SND_SOC_NOPM, 0, 0),
1312SND_SOC_DAPM_AIF_OUT("AIF2ADCDAT", "AIF2 Capture", 0, SND_SOC_NOPM, 0, 0),
1313
1314SND_SOC_DAPM_MUX("AIF1DAC Mux", SND_SOC_NOPM, 0, 0, &aif1dac_mux),
1315SND_SOC_DAPM_MUX("AIF2DAC Mux", SND_SOC_NOPM, 0, 0, &aif2dac_mux),
1316SND_SOC_DAPM_MUX("AIF2ADC Mux", SND_SOC_NOPM, 0, 0, &aif2adc_mux),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001317
1318SND_SOC_DAPM_AIF_IN("AIF3DACDAT", "AIF3 Playback", 0, SND_SOC_NOPM, 0, 0),
1319SND_SOC_DAPM_AIF_IN("AIF3ADCDAT", "AIF3 Capture", 0, SND_SOC_NOPM, 0, 0),
1320
1321SND_SOC_DAPM_SUPPLY("TOCLK", WM8994_CLOCKING_1, 4, 0, NULL, 0),
1322
1323SND_SOC_DAPM_ADC("DMIC2L", NULL, WM8994_POWER_MANAGEMENT_4, 5, 0),
1324SND_SOC_DAPM_ADC("DMIC2R", NULL, WM8994_POWER_MANAGEMENT_4, 4, 0),
1325SND_SOC_DAPM_ADC("DMIC1L", NULL, WM8994_POWER_MANAGEMENT_4, 3, 0),
1326SND_SOC_DAPM_ADC("DMIC1R", NULL, WM8994_POWER_MANAGEMENT_4, 2, 0),
1327
1328/* Power is done with the muxes since the ADC power also controls the
1329 * downsampling chain, the chip will automatically manage the analogue
1330 * specific portions.
1331 */
1332SND_SOC_DAPM_ADC("ADCL", NULL, SND_SOC_NOPM, 1, 0),
1333SND_SOC_DAPM_ADC("ADCR", NULL, SND_SOC_NOPM, 0, 0),
1334
1335SND_SOC_DAPM_MUX("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux),
1336SND_SOC_DAPM_MUX("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux),
1337
1338SND_SOC_DAPM_DAC("DAC2L", NULL, WM8994_POWER_MANAGEMENT_5, 3, 0),
1339SND_SOC_DAPM_DAC("DAC2R", NULL, WM8994_POWER_MANAGEMENT_5, 2, 0),
1340SND_SOC_DAPM_DAC("DAC1L", NULL, WM8994_POWER_MANAGEMENT_5, 1, 0),
1341SND_SOC_DAPM_DAC("DAC1R", NULL, WM8994_POWER_MANAGEMENT_5, 0, 0),
1342
1343SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
1344SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
1345
1346SND_SOC_DAPM_MIXER("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
1347 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
1348SND_SOC_DAPM_MIXER("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
1349 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
1350
1351SND_SOC_DAPM_POST("Debug log", post_ev),
1352};
1353
Mark Brownc4431df2010-11-26 15:21:07 +00001354static const struct snd_soc_dapm_widget wm8994_specific_dapm_widgets[] = {
1355SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8994_aif3adc_mux),
1356};
Mark Brown9e6e96a2010-01-29 17:47:12 +00001357
Mark Brownc4431df2010-11-26 15:21:07 +00001358static const struct snd_soc_dapm_widget wm8958_dapm_widgets[] = {
1359SND_SOC_DAPM_MUX("Mono PCM Out Mux", SND_SOC_NOPM, 0, 0, &mono_pcm_out_mux),
1360SND_SOC_DAPM_MUX("AIF2DACL Mux", SND_SOC_NOPM, 0, 0, &aif2dacl_src_mux),
1361SND_SOC_DAPM_MUX("AIF2DACR Mux", SND_SOC_NOPM, 0, 0, &aif2dacr_src_mux),
1362SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8958_aif3adc_mux),
1363};
1364
1365static const struct snd_soc_dapm_route intercon[] = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00001366 { "CLK_SYS", NULL, "AIF1CLK", check_clk_sys },
1367 { "CLK_SYS", NULL, "AIF2CLK", check_clk_sys },
1368
1369 { "DSP1CLK", NULL, "CLK_SYS" },
1370 { "DSP2CLK", NULL, "CLK_SYS" },
1371 { "DSPINTCLK", NULL, "CLK_SYS" },
1372
1373 { "AIF1ADC1L", NULL, "AIF1CLK" },
1374 { "AIF1ADC1L", NULL, "DSP1CLK" },
1375 { "AIF1ADC1R", NULL, "AIF1CLK" },
1376 { "AIF1ADC1R", NULL, "DSP1CLK" },
1377 { "AIF1ADC1R", NULL, "DSPINTCLK" },
1378
1379 { "AIF1DAC1L", NULL, "AIF1CLK" },
1380 { "AIF1DAC1L", NULL, "DSP1CLK" },
1381 { "AIF1DAC1R", NULL, "AIF1CLK" },
1382 { "AIF1DAC1R", NULL, "DSP1CLK" },
1383 { "AIF1DAC1R", NULL, "DSPINTCLK" },
1384
1385 { "AIF1ADC2L", NULL, "AIF1CLK" },
1386 { "AIF1ADC2L", NULL, "DSP1CLK" },
1387 { "AIF1ADC2R", NULL, "AIF1CLK" },
1388 { "AIF1ADC2R", NULL, "DSP1CLK" },
1389 { "AIF1ADC2R", NULL, "DSPINTCLK" },
1390
1391 { "AIF1DAC2L", NULL, "AIF1CLK" },
1392 { "AIF1DAC2L", NULL, "DSP1CLK" },
1393 { "AIF1DAC2R", NULL, "AIF1CLK" },
1394 { "AIF1DAC2R", NULL, "DSP1CLK" },
1395 { "AIF1DAC2R", NULL, "DSPINTCLK" },
1396
1397 { "AIF2ADCL", NULL, "AIF2CLK" },
1398 { "AIF2ADCL", NULL, "DSP2CLK" },
1399 { "AIF2ADCR", NULL, "AIF2CLK" },
1400 { "AIF2ADCR", NULL, "DSP2CLK" },
1401 { "AIF2ADCR", NULL, "DSPINTCLK" },
1402
1403 { "AIF2DACL", NULL, "AIF2CLK" },
1404 { "AIF2DACL", NULL, "DSP2CLK" },
1405 { "AIF2DACR", NULL, "AIF2CLK" },
1406 { "AIF2DACR", NULL, "DSP2CLK" },
1407 { "AIF2DACR", NULL, "DSPINTCLK" },
1408
1409 { "DMIC1L", NULL, "DMIC1DAT" },
1410 { "DMIC1L", NULL, "CLK_SYS" },
1411 { "DMIC1R", NULL, "DMIC1DAT" },
1412 { "DMIC1R", NULL, "CLK_SYS" },
1413 { "DMIC2L", NULL, "DMIC2DAT" },
1414 { "DMIC2L", NULL, "CLK_SYS" },
1415 { "DMIC2R", NULL, "DMIC2DAT" },
1416 { "DMIC2R", NULL, "CLK_SYS" },
1417
1418 { "ADCL", NULL, "AIF1CLK" },
1419 { "ADCL", NULL, "DSP1CLK" },
1420 { "ADCL", NULL, "DSPINTCLK" },
1421
1422 { "ADCR", NULL, "AIF1CLK" },
1423 { "ADCR", NULL, "DSP1CLK" },
1424 { "ADCR", NULL, "DSPINTCLK" },
1425
1426 { "ADCL Mux", "ADC", "ADCL" },
1427 { "ADCL Mux", "DMIC", "DMIC1L" },
1428 { "ADCR Mux", "ADC", "ADCR" },
1429 { "ADCR Mux", "DMIC", "DMIC1R" },
1430
1431 { "DAC1L", NULL, "AIF1CLK" },
1432 { "DAC1L", NULL, "DSP1CLK" },
1433 { "DAC1L", NULL, "DSPINTCLK" },
1434
1435 { "DAC1R", NULL, "AIF1CLK" },
1436 { "DAC1R", NULL, "DSP1CLK" },
1437 { "DAC1R", NULL, "DSPINTCLK" },
1438
1439 { "DAC2L", NULL, "AIF2CLK" },
1440 { "DAC2L", NULL, "DSP2CLK" },
1441 { "DAC2L", NULL, "DSPINTCLK" },
1442
1443 { "DAC2R", NULL, "AIF2DACR" },
1444 { "DAC2R", NULL, "AIF2CLK" },
1445 { "DAC2R", NULL, "DSP2CLK" },
1446 { "DAC2R", NULL, "DSPINTCLK" },
1447
1448 { "TOCLK", NULL, "CLK_SYS" },
1449
1450 /* AIF1 outputs */
1451 { "AIF1ADC1L", NULL, "AIF1ADC1L Mixer" },
1452 { "AIF1ADC1L Mixer", "ADC/DMIC Switch", "ADCL Mux" },
1453 { "AIF1ADC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1454
1455 { "AIF1ADC1R", NULL, "AIF1ADC1R Mixer" },
1456 { "AIF1ADC1R Mixer", "ADC/DMIC Switch", "ADCR Mux" },
1457 { "AIF1ADC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1458
Mark Browna3257ba2010-07-19 14:02:34 +01001459 { "AIF1ADC2L", NULL, "AIF1ADC2L Mixer" },
1460 { "AIF1ADC2L Mixer", "DMIC Switch", "DMIC2L" },
1461 { "AIF1ADC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1462
1463 { "AIF1ADC2R", NULL, "AIF1ADC2R Mixer" },
1464 { "AIF1ADC2R Mixer", "DMIC Switch", "DMIC2R" },
1465 { "AIF1ADC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1466
Mark Brown9e6e96a2010-01-29 17:47:12 +00001467 /* Pin level routing for AIF3 */
1468 { "AIF1DAC1L", NULL, "AIF1DAC Mux" },
1469 { "AIF1DAC1R", NULL, "AIF1DAC Mux" },
1470 { "AIF1DAC2L", NULL, "AIF1DAC Mux" },
1471 { "AIF1DAC2R", NULL, "AIF1DAC Mux" },
1472
Mark Brown9e6e96a2010-01-29 17:47:12 +00001473 { "AIF1DAC Mux", "AIF1DACDAT", "AIF1DACDAT" },
1474 { "AIF1DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1475 { "AIF2DAC Mux", "AIF2DACDAT", "AIF2DACDAT" },
1476 { "AIF2DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1477 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCL" },
1478 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCR" },
1479 { "AIF2ADC Mux", "AIF3DACDAT", "AIF3ADCDAT" },
1480
1481 /* DAC1 inputs */
1482 { "DAC1L", NULL, "DAC1L Mixer" },
1483 { "DAC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1484 { "DAC1L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1485 { "DAC1L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1486 { "DAC1L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1487 { "DAC1L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1488
1489 { "DAC1R", NULL, "DAC1R Mixer" },
1490 { "DAC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1491 { "DAC1R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1492 { "DAC1R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1493 { "DAC1R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1494 { "DAC1R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1495
1496 /* DAC2/AIF2 outputs */
1497 { "AIF2ADCL", NULL, "AIF2DAC2L Mixer" },
1498 { "DAC2L", NULL, "AIF2DAC2L Mixer" },
1499 { "AIF2DAC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1500 { "AIF2DAC2L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1501 { "AIF2DAC2L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1502 { "AIF2DAC2L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1503 { "AIF2DAC2L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1504
1505 { "AIF2ADCR", NULL, "AIF2DAC2R Mixer" },
1506 { "DAC2R", NULL, "AIF2DAC2R Mixer" },
1507 { "AIF2DAC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1508 { "AIF2DAC2R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1509 { "AIF2DAC2R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1510 { "AIF2DAC2R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1511 { "AIF2DAC2R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1512
1513 { "AIF2ADCDAT", NULL, "AIF2ADC Mux" },
1514
1515 /* AIF3 output */
1516 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1L" },
1517 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1R" },
1518 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2L" },
1519 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2R" },
1520 { "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCL" },
1521 { "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCR" },
1522 { "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACL" },
1523 { "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACR" },
1524
1525 /* Sidetone */
1526 { "Left Sidetone", "ADC/DMIC1", "ADCL Mux" },
1527 { "Left Sidetone", "DMIC2", "DMIC2L" },
1528 { "Right Sidetone", "ADC/DMIC1", "ADCR Mux" },
1529 { "Right Sidetone", "DMIC2", "DMIC2R" },
1530
1531 /* Output stages */
1532 { "Left Output Mixer", "DAC Switch", "DAC1L" },
1533 { "Right Output Mixer", "DAC Switch", "DAC1R" },
1534
1535 { "SPKL", "DAC1 Switch", "DAC1L" },
1536 { "SPKL", "DAC2 Switch", "DAC2L" },
1537
1538 { "SPKR", "DAC1 Switch", "DAC1R" },
1539 { "SPKR", "DAC2 Switch", "DAC2R" },
1540
1541 { "Left Headphone Mux", "DAC", "DAC1L" },
1542 { "Right Headphone Mux", "DAC", "DAC1R" },
1543};
1544
Mark Brownc4431df2010-11-26 15:21:07 +00001545static const struct snd_soc_dapm_route wm8994_intercon[] = {
1546 { "AIF2DACL", NULL, "AIF2DAC Mux" },
1547 { "AIF2DACR", NULL, "AIF2DAC Mux" },
1548};
1549
1550static const struct snd_soc_dapm_route wm8958_intercon[] = {
1551 { "AIF2DACL", NULL, "AIF2DACL Mux" },
1552 { "AIF2DACR", NULL, "AIF2DACR Mux" },
1553
1554 { "AIF2DACL Mux", "AIF2", "AIF2DAC Mux" },
1555 { "AIF2DACL Mux", "AIF3", "AIF3DACDAT" },
1556 { "AIF2DACR Mux", "AIF2", "AIF2DAC Mux" },
1557 { "AIF2DACR Mux", "AIF3", "AIF3DACDAT" },
1558
1559 { "Mono PCM Out Mux", "AIF2ADCL", "AIF2ADCL" },
1560 { "Mono PCM Out Mux", "AIF2ADCR", "AIF2ADCR" },
1561
1562 { "AIF3ADC Mux", "Mono PCM", "Mono PCM Out Mux" },
1563};
1564
Mark Brown9e6e96a2010-01-29 17:47:12 +00001565/* The size in bits of the FLL divide multiplied by 10
1566 * to allow rounding later */
1567#define FIXED_FLL_SIZE ((1 << 16) * 10)
1568
1569struct fll_div {
1570 u16 outdiv;
1571 u16 n;
1572 u16 k;
1573 u16 clk_ref_div;
1574 u16 fll_fratio;
1575};
1576
1577static int wm8994_get_fll_config(struct fll_div *fll,
1578 int freq_in, int freq_out)
1579{
1580 u64 Kpart;
1581 unsigned int K, Ndiv, Nmod;
1582
1583 pr_debug("FLL input=%dHz, output=%dHz\n", freq_in, freq_out);
1584
1585 /* Scale the input frequency down to <= 13.5MHz */
1586 fll->clk_ref_div = 0;
1587 while (freq_in > 13500000) {
1588 fll->clk_ref_div++;
1589 freq_in /= 2;
1590
1591 if (fll->clk_ref_div > 3)
1592 return -EINVAL;
1593 }
1594 pr_debug("CLK_REF_DIV=%d, Fref=%dHz\n", fll->clk_ref_div, freq_in);
1595
1596 /* Scale the output to give 90MHz<=Fvco<=100MHz */
1597 fll->outdiv = 3;
1598 while (freq_out * (fll->outdiv + 1) < 90000000) {
1599 fll->outdiv++;
1600 if (fll->outdiv > 63)
1601 return -EINVAL;
1602 }
1603 freq_out *= fll->outdiv + 1;
1604 pr_debug("OUTDIV=%d, Fvco=%dHz\n", fll->outdiv, freq_out);
1605
1606 if (freq_in > 1000000) {
1607 fll->fll_fratio = 0;
Mark Brown7d48a6a2010-04-20 13:36:11 +09001608 } else if (freq_in > 256000) {
1609 fll->fll_fratio = 1;
1610 freq_in *= 2;
1611 } else if (freq_in > 128000) {
1612 fll->fll_fratio = 2;
1613 freq_in *= 4;
1614 } else if (freq_in > 64000) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00001615 fll->fll_fratio = 3;
1616 freq_in *= 8;
Mark Brown7d48a6a2010-04-20 13:36:11 +09001617 } else {
1618 fll->fll_fratio = 4;
1619 freq_in *= 16;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001620 }
1621 pr_debug("FLL_FRATIO=%d, Fref=%dHz\n", fll->fll_fratio, freq_in);
1622
1623 /* Now, calculate N.K */
1624 Ndiv = freq_out / freq_in;
1625
1626 fll->n = Ndiv;
1627 Nmod = freq_out % freq_in;
1628 pr_debug("Nmod=%d\n", Nmod);
1629
1630 /* Calculate fractional part - scale up so we can round. */
1631 Kpart = FIXED_FLL_SIZE * (long long)Nmod;
1632
1633 do_div(Kpart, freq_in);
1634
1635 K = Kpart & 0xFFFFFFFF;
1636
1637 if ((K % 10) >= 5)
1638 K += 5;
1639
1640 /* Move down to proper range now rounding is done */
1641 fll->k = K / 10;
1642
1643 pr_debug("N=%x K=%x\n", fll->n, fll->k);
1644
1645 return 0;
1646}
1647
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001648static int _wm8994_set_fll(struct snd_soc_codec *codec, int id, int src,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001649 unsigned int freq_in, unsigned int freq_out)
1650{
Mark Brownb2c812e2010-04-14 15:35:19 +09001651 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001652 int reg_offset, ret;
1653 struct fll_div fll;
1654 u16 reg, aif1, aif2;
1655
1656 aif1 = snd_soc_read(codec, WM8994_AIF1_CLOCKING_1)
1657 & WM8994_AIF1CLK_ENA;
1658
1659 aif2 = snd_soc_read(codec, WM8994_AIF2_CLOCKING_1)
1660 & WM8994_AIF2CLK_ENA;
1661
1662 switch (id) {
1663 case WM8994_FLL1:
1664 reg_offset = 0;
1665 id = 0;
1666 break;
1667 case WM8994_FLL2:
1668 reg_offset = 0x20;
1669 id = 1;
1670 break;
1671 default:
1672 return -EINVAL;
1673 }
1674
Mark Brown136ff2a2010-04-20 12:56:18 +09001675 switch (src) {
Mark Brown7add84a2010-04-22 02:29:01 +09001676 case 0:
1677 /* Allow no source specification when stopping */
1678 if (freq_out)
1679 return -EINVAL;
1680 break;
Mark Brown136ff2a2010-04-20 12:56:18 +09001681 case WM8994_FLL_SRC_MCLK1:
1682 case WM8994_FLL_SRC_MCLK2:
1683 case WM8994_FLL_SRC_LRCLK:
1684 case WM8994_FLL_SRC_BCLK:
1685 break;
1686 default:
1687 return -EINVAL;
1688 }
1689
Mark Brown9e6e96a2010-01-29 17:47:12 +00001690 /* Are we changing anything? */
1691 if (wm8994->fll[id].src == src &&
1692 wm8994->fll[id].in == freq_in && wm8994->fll[id].out == freq_out)
1693 return 0;
1694
1695 /* If we're stopping the FLL redo the old config - no
1696 * registers will actually be written but we avoid GCC flow
1697 * analysis bugs spewing warnings.
1698 */
1699 if (freq_out)
1700 ret = wm8994_get_fll_config(&fll, freq_in, freq_out);
1701 else
1702 ret = wm8994_get_fll_config(&fll, wm8994->fll[id].in,
1703 wm8994->fll[id].out);
1704 if (ret < 0)
1705 return ret;
1706
1707 /* Gate the AIF clocks while we reclock */
1708 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1709 WM8994_AIF1CLK_ENA, 0);
1710 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1711 WM8994_AIF2CLK_ENA, 0);
1712
1713 /* We always need to disable the FLL while reconfiguring */
1714 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
1715 WM8994_FLL1_ENA, 0);
1716
1717 reg = (fll.outdiv << WM8994_FLL1_OUTDIV_SHIFT) |
1718 (fll.fll_fratio << WM8994_FLL1_FRATIO_SHIFT);
1719 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_2 + reg_offset,
1720 WM8994_FLL1_OUTDIV_MASK |
1721 WM8994_FLL1_FRATIO_MASK, reg);
1722
1723 snd_soc_write(codec, WM8994_FLL1_CONTROL_3 + reg_offset, fll.k);
1724
1725 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_4 + reg_offset,
1726 WM8994_FLL1_N_MASK,
1727 fll.n << WM8994_FLL1_N_SHIFT);
1728
1729 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_5 + reg_offset,
Mark Brown136ff2a2010-04-20 12:56:18 +09001730 WM8994_FLL1_REFCLK_DIV_MASK |
1731 WM8994_FLL1_REFCLK_SRC_MASK,
1732 (fll.clk_ref_div << WM8994_FLL1_REFCLK_DIV_SHIFT) |
1733 (src - 1));
Mark Brown9e6e96a2010-01-29 17:47:12 +00001734
1735 /* Enable (with fractional mode if required) */
1736 if (freq_out) {
1737 if (fll.k)
1738 reg = WM8994_FLL1_ENA | WM8994_FLL1_FRAC;
1739 else
1740 reg = WM8994_FLL1_ENA;
1741 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
1742 WM8994_FLL1_ENA | WM8994_FLL1_FRAC,
1743 reg);
1744 }
1745
1746 wm8994->fll[id].in = freq_in;
1747 wm8994->fll[id].out = freq_out;
Mark Brown136ff2a2010-04-20 12:56:18 +09001748 wm8994->fll[id].src = src;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001749
1750 /* Enable any gated AIF clocks */
1751 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1752 WM8994_AIF1CLK_ENA, aif1);
1753 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1754 WM8994_AIF2CLK_ENA, aif2);
1755
1756 configure_clock(codec);
1757
1758 return 0;
1759}
1760
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001761
Mark Brown66b47fd2010-07-08 11:25:43 +09001762static int opclk_divs[] = { 10, 20, 30, 40, 55, 60, 80, 120, 160 };
1763
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001764static int wm8994_set_fll(struct snd_soc_dai *dai, int id, int src,
1765 unsigned int freq_in, unsigned int freq_out)
1766{
1767 return _wm8994_set_fll(dai->codec, id, src, freq_in, freq_out);
1768}
1769
Mark Brown9e6e96a2010-01-29 17:47:12 +00001770static int wm8994_set_dai_sysclk(struct snd_soc_dai *dai,
1771 int clk_id, unsigned int freq, int dir)
1772{
1773 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001774 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown66b47fd2010-07-08 11:25:43 +09001775 int i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001776
1777 switch (dai->id) {
1778 case 1:
1779 case 2:
1780 break;
1781
1782 default:
1783 /* AIF3 shares clocking with AIF1/2 */
1784 return -EINVAL;
1785 }
1786
1787 switch (clk_id) {
1788 case WM8994_SYSCLK_MCLK1:
1789 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK1;
1790 wm8994->mclk[0] = freq;
1791 dev_dbg(dai->dev, "AIF%d using MCLK1 at %uHz\n",
1792 dai->id, freq);
1793 break;
1794
1795 case WM8994_SYSCLK_MCLK2:
1796 /* TODO: Set GPIO AF */
1797 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK2;
1798 wm8994->mclk[1] = freq;
1799 dev_dbg(dai->dev, "AIF%d using MCLK2 at %uHz\n",
1800 dai->id, freq);
1801 break;
1802
1803 case WM8994_SYSCLK_FLL1:
1804 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL1;
1805 dev_dbg(dai->dev, "AIF%d using FLL1\n", dai->id);
1806 break;
1807
1808 case WM8994_SYSCLK_FLL2:
1809 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL2;
1810 dev_dbg(dai->dev, "AIF%d using FLL2\n", dai->id);
1811 break;
1812
Mark Brown66b47fd2010-07-08 11:25:43 +09001813 case WM8994_SYSCLK_OPCLK:
1814 /* Special case - a division (times 10) is given and
1815 * no effect on main clocking.
1816 */
1817 if (freq) {
1818 for (i = 0; i < ARRAY_SIZE(opclk_divs); i++)
1819 if (opclk_divs[i] == freq)
1820 break;
1821 if (i == ARRAY_SIZE(opclk_divs))
1822 return -EINVAL;
1823 snd_soc_update_bits(codec, WM8994_CLOCKING_2,
1824 WM8994_OPCLK_DIV_MASK, i);
1825 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
1826 WM8994_OPCLK_ENA, WM8994_OPCLK_ENA);
1827 } else {
1828 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
1829 WM8994_OPCLK_ENA, 0);
1830 }
1831
Mark Brown9e6e96a2010-01-29 17:47:12 +00001832 default:
1833 return -EINVAL;
1834 }
1835
1836 configure_clock(codec);
1837
1838 return 0;
1839}
1840
1841static int wm8994_set_bias_level(struct snd_soc_codec *codec,
1842 enum snd_soc_bias_level level)
1843{
Mark Brown3a423152010-11-26 15:21:06 +00001844 struct wm8994 *control = codec->control_data;
Mark Brownb6b05692010-08-13 12:58:20 +01001845 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1846
Mark Brown9e6e96a2010-01-29 17:47:12 +00001847 switch (level) {
1848 case SND_SOC_BIAS_ON:
1849 break;
1850
1851 case SND_SOC_BIAS_PREPARE:
1852 /* VMID=2x40k */
1853 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
1854 WM8994_VMID_SEL_MASK, 0x2);
1855 break;
1856
1857 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001858 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
Mark Brown39fb51a2010-11-26 17:23:43 +00001859 pm_runtime_get_sync(codec->dev);
1860
Mark Brown0c17b392010-08-11 18:03:54 +01001861 /* Tweak DC servo and DSP configuration for
1862 * improved performance. */
Mark Brown3a423152010-11-26 15:21:06 +00001863 if (control->type == WM8994 && wm8994->revision < 4) {
Mark Brownb6b05692010-08-13 12:58:20 +01001864 /* Tweak DC servo and DSP configuration for
1865 * improved performance. */
1866 snd_soc_write(codec, 0x102, 0x3);
1867 snd_soc_write(codec, 0x56, 0x3);
1868 snd_soc_write(codec, 0x817, 0);
1869 snd_soc_write(codec, 0x102, 0);
1870 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00001871
1872 /* Discharge LINEOUT1 & 2 */
1873 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
1874 WM8994_LINEOUT1_DISCH |
1875 WM8994_LINEOUT2_DISCH,
1876 WM8994_LINEOUT1_DISCH |
1877 WM8994_LINEOUT2_DISCH);
1878
1879 /* Startup bias, VMID ramp & buffer */
1880 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
1881 WM8994_STARTUP_BIAS_ENA |
1882 WM8994_VMID_BUF_ENA |
1883 WM8994_VMID_RAMP_MASK,
1884 WM8994_STARTUP_BIAS_ENA |
1885 WM8994_VMID_BUF_ENA |
1886 (0x11 << WM8994_VMID_RAMP_SHIFT));
1887
1888 /* Main bias enable, VMID=2x40k */
1889 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
1890 WM8994_BIAS_ENA |
1891 WM8994_VMID_SEL_MASK,
1892 WM8994_BIAS_ENA | 0x2);
1893
1894 msleep(20);
1895 }
1896
1897 /* VMID=2x500k */
1898 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
1899 WM8994_VMID_SEL_MASK, 0x4);
1900
1901 break;
1902
1903 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001904 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
Mark Brownd522ffb2010-03-30 14:29:14 +01001905 /* Switch over to startup biases */
1906 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
1907 WM8994_BIAS_SRC |
1908 WM8994_STARTUP_BIAS_ENA |
1909 WM8994_VMID_BUF_ENA |
1910 WM8994_VMID_RAMP_MASK,
1911 WM8994_BIAS_SRC |
1912 WM8994_STARTUP_BIAS_ENA |
1913 WM8994_VMID_BUF_ENA |
1914 (1 << WM8994_VMID_RAMP_SHIFT));
Mark Brown9e6e96a2010-01-29 17:47:12 +00001915
Mark Brownd522ffb2010-03-30 14:29:14 +01001916 /* Disable main biases */
1917 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
1918 WM8994_BIAS_ENA |
1919 WM8994_VMID_SEL_MASK, 0);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001920
Mark Brownd522ffb2010-03-30 14:29:14 +01001921 /* Discharge line */
1922 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
1923 WM8994_LINEOUT1_DISCH |
1924 WM8994_LINEOUT2_DISCH,
1925 WM8994_LINEOUT1_DISCH |
1926 WM8994_LINEOUT2_DISCH);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001927
Mark Brownd522ffb2010-03-30 14:29:14 +01001928 msleep(5);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001929
Mark Brownd522ffb2010-03-30 14:29:14 +01001930 /* Switch off startup biases */
1931 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
1932 WM8994_BIAS_SRC |
1933 WM8994_STARTUP_BIAS_ENA |
1934 WM8994_VMID_BUF_ENA |
1935 WM8994_VMID_RAMP_MASK, 0);
Mark Brown39fb51a2010-11-26 17:23:43 +00001936
1937 pm_runtime_put(codec->dev);
Mark Brownd522ffb2010-03-30 14:29:14 +01001938 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00001939 break;
1940 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001941 codec->dapm.bias_level = level;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001942 return 0;
1943}
1944
1945static int wm8994_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1946{
1947 struct snd_soc_codec *codec = dai->codec;
Mark Brownc4431df2010-11-26 15:21:07 +00001948 struct wm8994 *control = codec->control_data;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001949 int ms_reg;
1950 int aif1_reg;
1951 int ms = 0;
1952 int aif1 = 0;
1953
1954 switch (dai->id) {
1955 case 1:
1956 ms_reg = WM8994_AIF1_MASTER_SLAVE;
1957 aif1_reg = WM8994_AIF1_CONTROL_1;
1958 break;
1959 case 2:
1960 ms_reg = WM8994_AIF2_MASTER_SLAVE;
1961 aif1_reg = WM8994_AIF2_CONTROL_1;
1962 break;
1963 default:
1964 return -EINVAL;
1965 }
1966
1967 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1968 case SND_SOC_DAIFMT_CBS_CFS:
1969 break;
1970 case SND_SOC_DAIFMT_CBM_CFM:
1971 ms = WM8994_AIF1_MSTR;
1972 break;
1973 default:
1974 return -EINVAL;
1975 }
1976
1977 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1978 case SND_SOC_DAIFMT_DSP_B:
1979 aif1 |= WM8994_AIF1_LRCLK_INV;
1980 case SND_SOC_DAIFMT_DSP_A:
1981 aif1 |= 0x18;
1982 break;
1983 case SND_SOC_DAIFMT_I2S:
1984 aif1 |= 0x10;
1985 break;
1986 case SND_SOC_DAIFMT_RIGHT_J:
1987 break;
1988 case SND_SOC_DAIFMT_LEFT_J:
1989 aif1 |= 0x8;
1990 break;
1991 default:
1992 return -EINVAL;
1993 }
1994
1995 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1996 case SND_SOC_DAIFMT_DSP_A:
1997 case SND_SOC_DAIFMT_DSP_B:
1998 /* frame inversion not valid for DSP modes */
1999 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2000 case SND_SOC_DAIFMT_NB_NF:
2001 break;
2002 case SND_SOC_DAIFMT_IB_NF:
2003 aif1 |= WM8994_AIF1_BCLK_INV;
2004 break;
2005 default:
2006 return -EINVAL;
2007 }
2008 break;
2009
2010 case SND_SOC_DAIFMT_I2S:
2011 case SND_SOC_DAIFMT_RIGHT_J:
2012 case SND_SOC_DAIFMT_LEFT_J:
2013 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2014 case SND_SOC_DAIFMT_NB_NF:
2015 break;
2016 case SND_SOC_DAIFMT_IB_IF:
2017 aif1 |= WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV;
2018 break;
2019 case SND_SOC_DAIFMT_IB_NF:
2020 aif1 |= WM8994_AIF1_BCLK_INV;
2021 break;
2022 case SND_SOC_DAIFMT_NB_IF:
2023 aif1 |= WM8994_AIF1_LRCLK_INV;
2024 break;
2025 default:
2026 return -EINVAL;
2027 }
2028 break;
2029 default:
2030 return -EINVAL;
2031 }
2032
Mark Brownc4431df2010-11-26 15:21:07 +00002033 /* The AIF2 format configuration needs to be mirrored to AIF3
2034 * on WM8958 if it's in use so just do it all the time. */
2035 if (control->type == WM8958 && dai->id == 2)
2036 snd_soc_update_bits(codec, WM8958_AIF3_CONTROL_1,
2037 WM8994_AIF1_LRCLK_INV |
2038 WM8958_AIF3_FMT_MASK, aif1);
2039
Mark Brown9e6e96a2010-01-29 17:47:12 +00002040 snd_soc_update_bits(codec, aif1_reg,
2041 WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV |
2042 WM8994_AIF1_FMT_MASK,
2043 aif1);
2044 snd_soc_update_bits(codec, ms_reg, WM8994_AIF1_MSTR,
2045 ms);
2046
2047 return 0;
2048}
2049
2050static struct {
2051 int val, rate;
2052} srs[] = {
2053 { 0, 8000 },
2054 { 1, 11025 },
2055 { 2, 12000 },
2056 { 3, 16000 },
2057 { 4, 22050 },
2058 { 5, 24000 },
2059 { 6, 32000 },
2060 { 7, 44100 },
2061 { 8, 48000 },
2062 { 9, 88200 },
2063 { 10, 96000 },
2064};
2065
2066static int fs_ratios[] = {
2067 64, 128, 192, 256, 348, 512, 768, 1024, 1408, 1536
2068};
2069
2070static int bclk_divs[] = {
2071 10, 15, 20, 30, 40, 50, 60, 80, 110, 120, 160, 220, 240, 320, 440, 480,
2072 640, 880, 960, 1280, 1760, 1920
2073};
2074
2075static int wm8994_hw_params(struct snd_pcm_substream *substream,
2076 struct snd_pcm_hw_params *params,
2077 struct snd_soc_dai *dai)
2078{
2079 struct snd_soc_codec *codec = dai->codec;
Mark Brownc4431df2010-11-26 15:21:07 +00002080 struct wm8994 *control = codec->control_data;
Mark Brownb2c812e2010-04-14 15:35:19 +09002081 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002082 int aif1_reg;
2083 int bclk_reg;
2084 int lrclk_reg;
2085 int rate_reg;
2086 int aif1 = 0;
2087 int bclk = 0;
2088 int lrclk = 0;
2089 int rate_val = 0;
2090 int id = dai->id - 1;
2091
2092 int i, cur_val, best_val, bclk_rate, best;
2093
2094 switch (dai->id) {
2095 case 1:
2096 aif1_reg = WM8994_AIF1_CONTROL_1;
2097 bclk_reg = WM8994_AIF1_BCLK;
2098 rate_reg = WM8994_AIF1_RATE;
2099 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
Mark Brown7d83d212010-08-23 10:54:43 +01002100 wm8994->lrclk_shared[0]) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002101 lrclk_reg = WM8994_AIF1DAC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002102 } else {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002103 lrclk_reg = WM8994_AIF1ADC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002104 dev_dbg(codec->dev, "AIF1 using split LRCLK\n");
2105 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002106 break;
2107 case 2:
2108 aif1_reg = WM8994_AIF2_CONTROL_1;
2109 bclk_reg = WM8994_AIF2_BCLK;
2110 rate_reg = WM8994_AIF2_RATE;
2111 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
Mark Brown7d83d212010-08-23 10:54:43 +01002112 wm8994->lrclk_shared[1]) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002113 lrclk_reg = WM8994_AIF2DAC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002114 } else {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002115 lrclk_reg = WM8994_AIF2ADC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002116 dev_dbg(codec->dev, "AIF2 using split LRCLK\n");
2117 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002118 break;
Mark Brownc4431df2010-11-26 15:21:07 +00002119 case 3:
2120 switch (control->type) {
2121 case WM8958:
2122 aif1_reg = WM8958_AIF3_CONTROL_1;
2123 break;
2124 default:
2125 return 0;
2126 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002127 default:
2128 return -EINVAL;
2129 }
2130
2131 bclk_rate = params_rate(params) * 2;
2132 switch (params_format(params)) {
2133 case SNDRV_PCM_FORMAT_S16_LE:
2134 bclk_rate *= 16;
2135 break;
2136 case SNDRV_PCM_FORMAT_S20_3LE:
2137 bclk_rate *= 20;
2138 aif1 |= 0x20;
2139 break;
2140 case SNDRV_PCM_FORMAT_S24_LE:
2141 bclk_rate *= 24;
2142 aif1 |= 0x40;
2143 break;
2144 case SNDRV_PCM_FORMAT_S32_LE:
2145 bclk_rate *= 32;
2146 aif1 |= 0x60;
2147 break;
2148 default:
2149 return -EINVAL;
2150 }
2151
2152 /* Try to find an appropriate sample rate; look for an exact match. */
2153 for (i = 0; i < ARRAY_SIZE(srs); i++)
2154 if (srs[i].rate == params_rate(params))
2155 break;
2156 if (i == ARRAY_SIZE(srs))
2157 return -EINVAL;
2158 rate_val |= srs[i].val << WM8994_AIF1_SR_SHIFT;
2159
2160 dev_dbg(dai->dev, "Sample rate is %dHz\n", srs[i].rate);
2161 dev_dbg(dai->dev, "AIF%dCLK is %dHz, target BCLK %dHz\n",
2162 dai->id, wm8994->aifclk[id], bclk_rate);
2163
2164 if (wm8994->aifclk[id] == 0) {
2165 dev_err(dai->dev, "AIF%dCLK not configured\n", dai->id);
2166 return -EINVAL;
2167 }
2168
2169 /* AIFCLK/fs ratio; look for a close match in either direction */
2170 best = 0;
2171 best_val = abs((fs_ratios[0] * params_rate(params))
2172 - wm8994->aifclk[id]);
2173 for (i = 1; i < ARRAY_SIZE(fs_ratios); i++) {
2174 cur_val = abs((fs_ratios[i] * params_rate(params))
2175 - wm8994->aifclk[id]);
2176 if (cur_val >= best_val)
2177 continue;
2178 best = i;
2179 best_val = cur_val;
2180 }
2181 dev_dbg(dai->dev, "Selected AIF%dCLK/fs = %d\n",
2182 dai->id, fs_ratios[best]);
2183 rate_val |= best;
2184
2185 /* We may not get quite the right frequency if using
2186 * approximate clocks so look for the closest match that is
2187 * higher than the target (we need to ensure that there enough
2188 * BCLKs to clock out the samples).
2189 */
2190 best = 0;
2191 for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
Joonyoung Shim07cd8ad2010-02-02 18:53:19 +09002192 cur_val = (wm8994->aifclk[id] * 10 / bclk_divs[i]) - bclk_rate;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002193 if (cur_val < 0) /* BCLK table is sorted */
2194 break;
2195 best = i;
2196 }
Joonyoung Shim07cd8ad2010-02-02 18:53:19 +09002197 bclk_rate = wm8994->aifclk[id] * 10 / bclk_divs[best];
Mark Brown9e6e96a2010-01-29 17:47:12 +00002198 dev_dbg(dai->dev, "Using BCLK_DIV %d for actual BCLK %dHz\n",
2199 bclk_divs[best], bclk_rate);
2200 bclk |= best << WM8994_AIF1_BCLK_DIV_SHIFT;
2201
2202 lrclk = bclk_rate / params_rate(params);
2203 dev_dbg(dai->dev, "Using LRCLK rate %d for actual LRCLK %dHz\n",
2204 lrclk, bclk_rate / lrclk);
2205
2206 snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
2207 snd_soc_update_bits(codec, bclk_reg, WM8994_AIF1_BCLK_DIV_MASK, bclk);
2208 snd_soc_update_bits(codec, lrclk_reg, WM8994_AIF1DAC_RATE_MASK,
2209 lrclk);
2210 snd_soc_update_bits(codec, rate_reg, WM8994_AIF1_SR_MASK |
2211 WM8994_AIF1CLK_RATE_MASK, rate_val);
2212
2213 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
2214 switch (dai->id) {
2215 case 1:
2216 wm8994->dac_rates[0] = params_rate(params);
2217 wm8994_set_retune_mobile(codec, 0);
2218 wm8994_set_retune_mobile(codec, 1);
2219 break;
2220 case 2:
2221 wm8994->dac_rates[1] = params_rate(params);
2222 wm8994_set_retune_mobile(codec, 2);
2223 break;
2224 }
2225 }
2226
2227 return 0;
2228}
2229
Mark Brownc4431df2010-11-26 15:21:07 +00002230static int wm8994_aif3_hw_params(struct snd_pcm_substream *substream,
2231 struct snd_pcm_hw_params *params,
2232 struct snd_soc_dai *dai)
2233{
2234 struct snd_soc_codec *codec = dai->codec;
2235 struct wm8994 *control = codec->control_data;
2236 int aif1_reg;
2237 int aif1 = 0;
2238
2239 switch (dai->id) {
2240 case 3:
2241 switch (control->type) {
2242 case WM8958:
2243 aif1_reg = WM8958_AIF3_CONTROL_1;
2244 break;
2245 default:
2246 return 0;
2247 }
2248 default:
2249 return 0;
2250 }
2251
2252 switch (params_format(params)) {
2253 case SNDRV_PCM_FORMAT_S16_LE:
2254 break;
2255 case SNDRV_PCM_FORMAT_S20_3LE:
2256 aif1 |= 0x20;
2257 break;
2258 case SNDRV_PCM_FORMAT_S24_LE:
2259 aif1 |= 0x40;
2260 break;
2261 case SNDRV_PCM_FORMAT_S32_LE:
2262 aif1 |= 0x60;
2263 break;
2264 default:
2265 return -EINVAL;
2266 }
2267
2268 return snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
2269}
2270
Mark Brown9e6e96a2010-01-29 17:47:12 +00002271static int wm8994_aif_mute(struct snd_soc_dai *codec_dai, int mute)
2272{
2273 struct snd_soc_codec *codec = codec_dai->codec;
2274 int mute_reg;
2275 int reg;
2276
2277 switch (codec_dai->id) {
2278 case 1:
2279 mute_reg = WM8994_AIF1_DAC1_FILTERS_1;
2280 break;
2281 case 2:
2282 mute_reg = WM8994_AIF2_DAC_FILTERS_1;
2283 break;
2284 default:
2285 return -EINVAL;
2286 }
2287
2288 if (mute)
2289 reg = WM8994_AIF1DAC1_MUTE;
2290 else
2291 reg = 0;
2292
2293 snd_soc_update_bits(codec, mute_reg, WM8994_AIF1DAC1_MUTE, reg);
2294
2295 return 0;
2296}
2297
Mark Brown778a76e2010-03-22 22:05:10 +00002298static int wm8994_set_tristate(struct snd_soc_dai *codec_dai, int tristate)
2299{
2300 struct snd_soc_codec *codec = codec_dai->codec;
2301 int reg, val, mask;
2302
2303 switch (codec_dai->id) {
2304 case 1:
2305 reg = WM8994_AIF1_MASTER_SLAVE;
2306 mask = WM8994_AIF1_TRI;
2307 break;
2308 case 2:
2309 reg = WM8994_AIF2_MASTER_SLAVE;
2310 mask = WM8994_AIF2_TRI;
2311 break;
2312 case 3:
2313 reg = WM8994_POWER_MANAGEMENT_6;
2314 mask = WM8994_AIF3_TRI;
2315 break;
2316 default:
2317 return -EINVAL;
2318 }
2319
2320 if (tristate)
2321 val = mask;
2322 else
2323 val = 0;
2324
2325 return snd_soc_update_bits(codec, reg, mask, reg);
2326}
2327
Mark Brown9e6e96a2010-01-29 17:47:12 +00002328#define WM8994_RATES SNDRV_PCM_RATE_8000_96000
2329
2330#define WM8994_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
Ian Lartey3079aed2010-08-31 23:56:34 +01002331 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002332
2333static struct snd_soc_dai_ops wm8994_aif1_dai_ops = {
2334 .set_sysclk = wm8994_set_dai_sysclk,
2335 .set_fmt = wm8994_set_dai_fmt,
2336 .hw_params = wm8994_hw_params,
2337 .digital_mute = wm8994_aif_mute,
2338 .set_pll = wm8994_set_fll,
Mark Brown778a76e2010-03-22 22:05:10 +00002339 .set_tristate = wm8994_set_tristate,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002340};
2341
2342static struct snd_soc_dai_ops wm8994_aif2_dai_ops = {
2343 .set_sysclk = wm8994_set_dai_sysclk,
2344 .set_fmt = wm8994_set_dai_fmt,
2345 .hw_params = wm8994_hw_params,
2346 .digital_mute = wm8994_aif_mute,
2347 .set_pll = wm8994_set_fll,
Mark Brown778a76e2010-03-22 22:05:10 +00002348 .set_tristate = wm8994_set_tristate,
2349};
2350
2351static struct snd_soc_dai_ops wm8994_aif3_dai_ops = {
Mark Brownc4431df2010-11-26 15:21:07 +00002352 .hw_params = wm8994_aif3_hw_params,
Mark Brown778a76e2010-03-22 22:05:10 +00002353 .set_tristate = wm8994_set_tristate,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002354};
2355
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002356static struct snd_soc_dai_driver wm8994_dai[] = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002357 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002358 .name = "wm8994-aif1",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002359 .id = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002360 .playback = {
2361 .stream_name = "AIF1 Playback",
2362 .channels_min = 2,
2363 .channels_max = 2,
2364 .rates = WM8994_RATES,
2365 .formats = WM8994_FORMATS,
2366 },
2367 .capture = {
2368 .stream_name = "AIF1 Capture",
2369 .channels_min = 2,
2370 .channels_max = 2,
2371 .rates = WM8994_RATES,
2372 .formats = WM8994_FORMATS,
2373 },
2374 .ops = &wm8994_aif1_dai_ops,
2375 },
2376 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002377 .name = "wm8994-aif2",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002378 .id = 2,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002379 .playback = {
2380 .stream_name = "AIF2 Playback",
2381 .channels_min = 2,
2382 .channels_max = 2,
2383 .rates = WM8994_RATES,
2384 .formats = WM8994_FORMATS,
2385 },
2386 .capture = {
2387 .stream_name = "AIF2 Capture",
2388 .channels_min = 2,
2389 .channels_max = 2,
2390 .rates = WM8994_RATES,
2391 .formats = WM8994_FORMATS,
2392 },
2393 .ops = &wm8994_aif2_dai_ops,
2394 },
2395 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002396 .name = "wm8994-aif3",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002397 .id = 3,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002398 .playback = {
2399 .stream_name = "AIF3 Playback",
2400 .channels_min = 2,
2401 .channels_max = 2,
2402 .rates = WM8994_RATES,
2403 .formats = WM8994_FORMATS,
2404 },
Dan Carpentera8462bd2010-03-24 14:58:34 +03002405 .capture = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002406 .stream_name = "AIF3 Capture",
2407 .channels_min = 2,
2408 .channels_max = 2,
2409 .rates = WM8994_RATES,
2410 .formats = WM8994_FORMATS,
2411 },
Mark Brown778a76e2010-03-22 22:05:10 +00002412 .ops = &wm8994_aif3_dai_ops,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002413 }
2414};
Mark Brown9e6e96a2010-01-29 17:47:12 +00002415
2416#ifdef CONFIG_PM
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002417static int wm8994_suspend(struct snd_soc_codec *codec, pm_message_t state)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002418{
Mark Brownb2c812e2010-04-14 15:35:19 +09002419 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002420 int i, ret;
2421
2422 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
2423 memcpy(&wm8994->fll_suspend[i], &wm8994->fll[i],
2424 sizeof(struct fll_config));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002425 ret = _wm8994_set_fll(codec, i + 1, 0, 0, 0);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002426 if (ret < 0)
2427 dev_warn(codec->dev, "Failed to stop FLL%d: %d\n",
2428 i + 1, ret);
2429 }
2430
2431 wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);
2432
2433 return 0;
2434}
2435
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002436static int wm8994_resume(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002437{
Mark Brownb2c812e2010-04-14 15:35:19 +09002438 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002439 int i, ret;
2440
2441 /* Restore the registers */
Mark Brownca9aef52010-11-26 17:23:41 +00002442 ret = snd_soc_cache_sync(codec);
2443 if (ret != 0)
2444 dev_err(codec->dev, "Failed to sync cache: %d\n", ret);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002445
2446 wm8994_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
2447
2448 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
Mark Brown6a2f1ee2010-05-10 18:36:37 +01002449 if (!wm8994->fll_suspend[i].out)
2450 continue;
2451
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002452 ret = _wm8994_set_fll(codec, i + 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002453 wm8994->fll_suspend[i].src,
2454 wm8994->fll_suspend[i].in,
2455 wm8994->fll_suspend[i].out);
2456 if (ret < 0)
2457 dev_warn(codec->dev, "Failed to restore FLL%d: %d\n",
2458 i + 1, ret);
2459 }
2460
2461 return 0;
2462}
2463#else
2464#define wm8994_suspend NULL
2465#define wm8994_resume NULL
2466#endif
2467
2468static void wm8994_handle_retune_mobile_pdata(struct wm8994_priv *wm8994)
2469{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002470 struct snd_soc_codec *codec = wm8994->codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002471 struct wm8994_pdata *pdata = wm8994->pdata;
2472 struct snd_kcontrol_new controls[] = {
2473 SOC_ENUM_EXT("AIF1.1 EQ Mode",
2474 wm8994->retune_mobile_enum,
2475 wm8994_get_retune_mobile_enum,
2476 wm8994_put_retune_mobile_enum),
2477 SOC_ENUM_EXT("AIF1.2 EQ Mode",
2478 wm8994->retune_mobile_enum,
2479 wm8994_get_retune_mobile_enum,
2480 wm8994_put_retune_mobile_enum),
2481 SOC_ENUM_EXT("AIF2 EQ Mode",
2482 wm8994->retune_mobile_enum,
2483 wm8994_get_retune_mobile_enum,
2484 wm8994_put_retune_mobile_enum),
2485 };
2486 int ret, i, j;
2487 const char **t;
2488
2489 /* We need an array of texts for the enum API but the number
2490 * of texts is likely to be less than the number of
2491 * configurations due to the sample rate dependency of the
2492 * configurations. */
2493 wm8994->num_retune_mobile_texts = 0;
2494 wm8994->retune_mobile_texts = NULL;
2495 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
2496 for (j = 0; j < wm8994->num_retune_mobile_texts; j++) {
2497 if (strcmp(pdata->retune_mobile_cfgs[i].name,
2498 wm8994->retune_mobile_texts[j]) == 0)
2499 break;
2500 }
2501
2502 if (j != wm8994->num_retune_mobile_texts)
2503 continue;
2504
2505 /* Expand the array... */
2506 t = krealloc(wm8994->retune_mobile_texts,
2507 sizeof(char *) *
2508 (wm8994->num_retune_mobile_texts + 1),
2509 GFP_KERNEL);
2510 if (t == NULL)
2511 continue;
2512
2513 /* ...store the new entry... */
2514 t[wm8994->num_retune_mobile_texts] =
2515 pdata->retune_mobile_cfgs[i].name;
2516
2517 /* ...and remember the new version. */
2518 wm8994->num_retune_mobile_texts++;
2519 wm8994->retune_mobile_texts = t;
2520 }
2521
2522 dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
2523 wm8994->num_retune_mobile_texts);
2524
2525 wm8994->retune_mobile_enum.max = wm8994->num_retune_mobile_texts;
2526 wm8994->retune_mobile_enum.texts = wm8994->retune_mobile_texts;
2527
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002528 ret = snd_soc_add_controls(wm8994->codec, controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002529 ARRAY_SIZE(controls));
2530 if (ret != 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002531 dev_err(wm8994->codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002532 "Failed to add ReTune Mobile controls: %d\n", ret);
2533}
2534
2535static void wm8994_handle_pdata(struct wm8994_priv *wm8994)
2536{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002537 struct snd_soc_codec *codec = wm8994->codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002538 struct wm8994_pdata *pdata = wm8994->pdata;
2539 int ret, i;
2540
2541 if (!pdata)
2542 return;
2543
2544 wm_hubs_handle_analogue_pdata(codec, pdata->lineout1_diff,
2545 pdata->lineout2_diff,
2546 pdata->lineout1fb,
2547 pdata->lineout2fb,
2548 pdata->jd_scthr,
2549 pdata->jd_thr,
2550 pdata->micbias1_lvl,
2551 pdata->micbias2_lvl);
2552
2553 dev_dbg(codec->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
2554
2555 if (pdata->num_drc_cfgs) {
2556 struct snd_kcontrol_new controls[] = {
2557 SOC_ENUM_EXT("AIF1DRC1 Mode", wm8994->drc_enum,
2558 wm8994_get_drc_enum, wm8994_put_drc_enum),
2559 SOC_ENUM_EXT("AIF1DRC2 Mode", wm8994->drc_enum,
2560 wm8994_get_drc_enum, wm8994_put_drc_enum),
2561 SOC_ENUM_EXT("AIF2DRC Mode", wm8994->drc_enum,
2562 wm8994_get_drc_enum, wm8994_put_drc_enum),
2563 };
2564
2565 /* We need an array of texts for the enum API */
2566 wm8994->drc_texts = kmalloc(sizeof(char *)
2567 * pdata->num_drc_cfgs, GFP_KERNEL);
2568 if (!wm8994->drc_texts) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002569 dev_err(wm8994->codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002570 "Failed to allocate %d DRC config texts\n",
2571 pdata->num_drc_cfgs);
2572 return;
2573 }
2574
2575 for (i = 0; i < pdata->num_drc_cfgs; i++)
2576 wm8994->drc_texts[i] = pdata->drc_cfgs[i].name;
2577
2578 wm8994->drc_enum.max = pdata->num_drc_cfgs;
2579 wm8994->drc_enum.texts = wm8994->drc_texts;
2580
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002581 ret = snd_soc_add_controls(wm8994->codec, controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002582 ARRAY_SIZE(controls));
2583 if (ret != 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002584 dev_err(wm8994->codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002585 "Failed to add DRC mode controls: %d\n", ret);
2586
2587 for (i = 0; i < WM8994_NUM_DRC; i++)
2588 wm8994_set_drc(codec, i);
2589 }
2590
2591 dev_dbg(codec->dev, "%d ReTune Mobile configurations\n",
2592 pdata->num_retune_mobile_cfgs);
2593
Mark Brown131d8102010-11-30 17:03:39 +00002594 if (pdata->num_mbc_cfgs) {
2595 struct snd_kcontrol_new control[] = {
2596 SOC_ENUM_EXT("MBC Mode", wm8994->mbc_enum,
2597 wm8958_get_mbc_enum, wm8958_put_mbc_enum),
2598 };
2599
2600 /* We need an array of texts for the enum API */
2601 wm8994->mbc_texts = kmalloc(sizeof(char *)
2602 * pdata->num_mbc_cfgs, GFP_KERNEL);
2603 if (!wm8994->mbc_texts) {
2604 dev_err(wm8994->codec->dev,
2605 "Failed to allocate %d MBC config texts\n",
2606 pdata->num_mbc_cfgs);
2607 return;
2608 }
2609
2610 for (i = 0; i < pdata->num_mbc_cfgs; i++)
2611 wm8994->mbc_texts[i] = pdata->mbc_cfgs[i].name;
2612
2613 wm8994->mbc_enum.max = pdata->num_mbc_cfgs;
2614 wm8994->mbc_enum.texts = wm8994->mbc_texts;
2615
2616 ret = snd_soc_add_controls(wm8994->codec, control, 1);
2617 if (ret != 0)
2618 dev_err(wm8994->codec->dev,
2619 "Failed to add MBC mode controls: %d\n", ret);
2620 }
2621
Mark Brown9e6e96a2010-01-29 17:47:12 +00002622 if (pdata->num_retune_mobile_cfgs)
2623 wm8994_handle_retune_mobile_pdata(wm8994);
2624 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002625 snd_soc_add_controls(wm8994->codec, wm8994_eq_controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002626 ARRAY_SIZE(wm8994_eq_controls));
2627}
2628
Mark Brown88766982010-03-29 20:57:12 +01002629/**
2630 * wm8994_mic_detect - Enable microphone detection via the WM8994 IRQ
2631 *
2632 * @codec: WM8994 codec
2633 * @jack: jack to report detection events on
2634 * @micbias: microphone bias to detect on
2635 * @det: value to report for presence detection
2636 * @shrt: value to report for short detection
2637 *
2638 * Enable microphone detection via IRQ on the WM8994. If GPIOs are
2639 * being used to bring out signals to the processor then only platform
Mark Brown5ab230a2010-09-06 14:59:34 +01002640 * data configuration is needed for WM8994 and processor GPIOs should
Mark Brown88766982010-03-29 20:57:12 +01002641 * be configured using snd_soc_jack_add_gpios() instead.
2642 *
2643 * Configuration of detection levels is available via the micbias1_lvl
2644 * and micbias2_lvl platform data members.
2645 */
2646int wm8994_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
2647 int micbias, int det, int shrt)
2648{
Mark Brownb2c812e2010-04-14 15:35:19 +09002649 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown88766982010-03-29 20:57:12 +01002650 struct wm8994_micdet *micdet;
Mark Brown3a423152010-11-26 15:21:06 +00002651 struct wm8994 *control = codec->control_data;
Mark Brown88766982010-03-29 20:57:12 +01002652 int reg;
2653
Mark Brown3a423152010-11-26 15:21:06 +00002654 if (control->type != WM8994)
2655 return -EINVAL;
2656
Mark Brown88766982010-03-29 20:57:12 +01002657 switch (micbias) {
2658 case 1:
2659 micdet = &wm8994->micdet[0];
2660 break;
2661 case 2:
2662 micdet = &wm8994->micdet[1];
2663 break;
2664 default:
2665 return -EINVAL;
2666 }
2667
2668 dev_dbg(codec->dev, "Configuring microphone detection on %d: %x %x\n",
2669 micbias, det, shrt);
2670
2671 /* Store the configuration */
2672 micdet->jack = jack;
2673 micdet->det = det;
2674 micdet->shrt = shrt;
2675
2676 /* If either of the jacks is set up then enable detection */
2677 if (wm8994->micdet[0].jack || wm8994->micdet[1].jack)
2678 reg = WM8994_MICD_ENA;
2679 else
2680 reg = 0;
2681
2682 snd_soc_update_bits(codec, WM8994_MICBIAS, WM8994_MICD_ENA, reg);
2683
2684 return 0;
2685}
2686EXPORT_SYMBOL_GPL(wm8994_mic_detect);
2687
2688static irqreturn_t wm8994_mic_irq(int irq, void *data)
2689{
2690 struct wm8994_priv *priv = data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002691 struct snd_soc_codec *codec = priv->codec;
Mark Brown88766982010-03-29 20:57:12 +01002692 int reg;
2693 int report;
2694
2695 reg = snd_soc_read(codec, WM8994_INTERRUPT_RAW_STATUS_2);
2696 if (reg < 0) {
2697 dev_err(codec->dev, "Failed to read microphone status: %d\n",
2698 reg);
2699 return IRQ_HANDLED;
2700 }
2701
2702 dev_dbg(codec->dev, "Microphone status: %x\n", reg);
2703
2704 report = 0;
2705 if (reg & WM8994_MIC1_DET_STS)
2706 report |= priv->micdet[0].det;
2707 if (reg & WM8994_MIC1_SHRT_STS)
2708 report |= priv->micdet[0].shrt;
2709 snd_soc_jack_report(priv->micdet[0].jack, report,
2710 priv->micdet[0].det | priv->micdet[0].shrt);
2711
2712 report = 0;
2713 if (reg & WM8994_MIC2_DET_STS)
2714 report |= priv->micdet[1].det;
2715 if (reg & WM8994_MIC2_SHRT_STS)
2716 report |= priv->micdet[1].shrt;
2717 snd_soc_jack_report(priv->micdet[1].jack, report,
2718 priv->micdet[1].det | priv->micdet[1].shrt);
2719
2720 return IRQ_HANDLED;
2721}
2722
Mark Brown821edd22010-11-26 15:21:09 +00002723/* Default microphone detection handler for WM8958 - the user can
2724 * override this if they wish.
2725 */
2726static void wm8958_default_micdet(u16 status, void *data)
2727{
2728 struct snd_soc_codec *codec = data;
2729 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2730 int report = 0;
2731
2732 /* If nothing present then clear our statuses */
2733 if (!(status & WM8958_MICD_STS)) {
2734 wm8994->jack_is_video = false;
2735 wm8994->jack_is_mic = false;
2736 goto done;
2737 }
2738
2739 /* Assume anything over 475 ohms is a microphone and remember
2740 * that we've seen one (since buttons override it) */
2741 if (status & 0x600)
2742 wm8994->jack_is_mic = true;
2743 if (wm8994->jack_is_mic)
2744 report |= SND_JACK_MICROPHONE;
2745
2746 /* Video has an impedence of approximately 75 ohms; assume
2747 * this isn't used as a button and remember it since buttons
2748 * override it. */
2749 if (status & 0x40)
2750 wm8994->jack_is_video = true;
2751 if (wm8994->jack_is_video)
2752 report |= SND_JACK_VIDEOOUT;
2753
2754 /* Everything else is buttons; just assign slots */
2755 if (status & 0x4)
2756 report |= SND_JACK_BTN_0;
2757 if (status & 0x8)
2758 report |= SND_JACK_BTN_1;
2759 if (status & 0x10)
2760 report |= SND_JACK_BTN_2;
2761 if (status & 0x20)
2762 report |= SND_JACK_BTN_3;
2763 if (status & 0x80)
2764 report |= SND_JACK_BTN_4;
2765 if (status & 0x100)
2766 report |= SND_JACK_BTN_5;
2767
2768done:
2769 snd_soc_jack_report(wm8994->micdet[0].jack,
2770 SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2 |
2771 SND_JACK_BTN_3 | SND_JACK_BTN_4 | SND_JACK_BTN_5 |
2772 SND_JACK_MICROPHONE | SND_JACK_VIDEOOUT,
2773 report);
2774}
2775
2776/**
2777 * wm8958_mic_detect - Enable microphone detection via the WM8958 IRQ
2778 *
2779 * @codec: WM8958 codec
2780 * @jack: jack to report detection events on
2781 *
2782 * Enable microphone detection functionality for the WM8958. By
2783 * default simple detection which supports the detection of up to 6
2784 * buttons plus video and microphone functionality is supported.
2785 *
2786 * The WM8958 has an advanced jack detection facility which is able to
2787 * support complex accessory detection, especially when used in
2788 * conjunction with external circuitry. In order to provide maximum
2789 * flexiblity a callback is provided which allows a completely custom
2790 * detection algorithm.
2791 */
2792int wm8958_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
2793 wm8958_micdet_cb cb, void *cb_data)
2794{
2795 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2796 struct wm8994 *control = codec->control_data;
2797
2798 if (control->type != WM8958)
2799 return -EINVAL;
2800
2801 if (jack) {
2802 if (!cb) {
2803 dev_dbg(codec->dev, "Using default micdet callback\n");
2804 cb = wm8958_default_micdet;
2805 cb_data = codec;
2806 }
2807
2808 wm8994->micdet[0].jack = jack;
2809 wm8994->jack_cb = cb;
2810 wm8994->jack_cb_data = cb_data;
2811
2812 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
2813 WM8958_MICD_ENA, WM8958_MICD_ENA);
2814 } else {
2815 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
2816 WM8958_MICD_ENA, 0);
2817 }
2818
2819 return 0;
2820}
2821EXPORT_SYMBOL_GPL(wm8958_mic_detect);
2822
2823static irqreturn_t wm8958_mic_irq(int irq, void *data)
2824{
2825 struct wm8994_priv *wm8994 = data;
2826 struct snd_soc_codec *codec = wm8994->codec;
2827 int reg;
2828
2829 reg = snd_soc_read(codec, WM8958_MIC_DETECT_3);
2830 if (reg < 0) {
2831 dev_err(codec->dev, "Failed to read mic detect status: %d\n",
2832 reg);
2833 return IRQ_NONE;
2834 }
2835
2836 if (!(reg & WM8958_MICD_VALID)) {
2837 dev_dbg(codec->dev, "Mic detect data not valid\n");
2838 goto out;
2839 }
2840
2841 if (wm8994->jack_cb)
2842 wm8994->jack_cb(reg, wm8994->jack_cb_data);
2843 else
2844 dev_warn(codec->dev, "Accessory detection with no callback\n");
2845
2846out:
2847 return IRQ_HANDLED;
2848}
2849
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002850static int wm8994_codec_probe(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002851{
Mark Brown3a423152010-11-26 15:21:06 +00002852 struct wm8994 *control;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002853 struct wm8994_priv *wm8994;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002854 struct snd_soc_dapm_context *dapm = &codec->dapm;
Mark Brownec62dbd2010-08-15 14:56:40 +01002855 int ret, i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002856
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002857 codec->control_data = dev_get_drvdata(codec->dev->parent);
Mark Brown3a423152010-11-26 15:21:06 +00002858 control = codec->control_data;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002859
2860 wm8994 = kzalloc(sizeof(struct wm8994_priv), GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002861 if (wm8994 == NULL)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002862 return -ENOMEM;
Mark Brownb2c812e2010-04-14 15:35:19 +09002863 snd_soc_codec_set_drvdata(codec, wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002864
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002865 wm8994->pdata = dev_get_platdata(codec->dev->parent);
2866 wm8994->codec = codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002867
Mark Brown39fb51a2010-11-26 17:23:43 +00002868 pm_runtime_enable(codec->dev);
2869 pm_runtime_resume(codec->dev);
2870
Mark Brownca9aef52010-11-26 17:23:41 +00002871 /* Read our current status back from the chip - we don't want to
2872 * reset as this may interfere with the GPIO or LDO operation. */
2873 for (i = 0; i < WM8994_CACHE_SIZE; i++) {
2874 if (!wm8994_readable(i) || wm8994_volatile(i))
2875 continue;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002876
Mark Brownca9aef52010-11-26 17:23:41 +00002877 ret = wm8994_reg_read(codec->control_data, i);
2878 if (ret <= 0)
2879 continue;
2880
2881 ret = snd_soc_cache_write(codec, i, ret);
2882 if (ret != 0) {
2883 dev_err(codec->dev,
2884 "Failed to initialise cache for 0x%x: %d\n",
2885 i, ret);
2886 goto err;
2887 }
2888 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002889
2890 /* Set revision-specific configuration */
Mark Brownb6b05692010-08-13 12:58:20 +01002891 wm8994->revision = snd_soc_read(codec, WM8994_CHIP_REVISION);
Mark Brown3a423152010-11-26 15:21:06 +00002892 switch (control->type) {
2893 case WM8994:
2894 switch (wm8994->revision) {
2895 case 2:
2896 case 3:
2897 wm8994->hubs.dcs_codes = -5;
2898 wm8994->hubs.hp_startup_mode = 1;
2899 wm8994->hubs.dcs_readback_mode = 1;
2900 break;
2901 default:
2902 wm8994->hubs.dcs_readback_mode = 1;
2903 break;
2904 }
2905
2906 case WM8958:
Mark Brown8437f702010-03-29 17:09:45 +01002907 wm8994->hubs.dcs_readback_mode = 1;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002908 break;
Mark Brown3a423152010-11-26 15:21:06 +00002909
Mark Brown9e6e96a2010-01-29 17:47:12 +00002910 default:
2911 break;
2912 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002913
Mark Brown3a423152010-11-26 15:21:06 +00002914 switch (control->type) {
2915 case WM8994:
2916 ret = wm8994_request_irq(codec->control_data,
2917 WM8994_IRQ_MIC1_DET,
2918 wm8994_mic_irq, "Mic 1 detect",
2919 wm8994);
2920 if (ret != 0)
2921 dev_warn(codec->dev,
2922 "Failed to request Mic1 detect IRQ: %d\n",
2923 ret);
Mark Brown88766982010-03-29 20:57:12 +01002924
Mark Brown3a423152010-11-26 15:21:06 +00002925 ret = wm8994_request_irq(codec->control_data,
2926 WM8994_IRQ_MIC1_SHRT,
2927 wm8994_mic_irq, "Mic 1 short",
2928 wm8994);
2929 if (ret != 0)
2930 dev_warn(codec->dev,
2931 "Failed to request Mic1 short IRQ: %d\n",
2932 ret);
Mark Brown88766982010-03-29 20:57:12 +01002933
Mark Brown3a423152010-11-26 15:21:06 +00002934 ret = wm8994_request_irq(codec->control_data,
2935 WM8994_IRQ_MIC2_DET,
2936 wm8994_mic_irq, "Mic 2 detect",
2937 wm8994);
2938 if (ret != 0)
2939 dev_warn(codec->dev,
2940 "Failed to request Mic2 detect IRQ: %d\n",
2941 ret);
Mark Brown88766982010-03-29 20:57:12 +01002942
Mark Brown3a423152010-11-26 15:21:06 +00002943 ret = wm8994_request_irq(codec->control_data,
2944 WM8994_IRQ_MIC2_SHRT,
2945 wm8994_mic_irq, "Mic 2 short",
2946 wm8994);
2947 if (ret != 0)
2948 dev_warn(codec->dev,
2949 "Failed to request Mic2 short IRQ: %d\n",
2950 ret);
2951 break;
Mark Brown821edd22010-11-26 15:21:09 +00002952
2953 case WM8958:
2954 ret = wm8994_request_irq(codec->control_data,
2955 WM8994_IRQ_MIC1_DET,
2956 wm8958_mic_irq, "Mic detect",
2957 wm8994);
2958 if (ret != 0)
2959 dev_warn(codec->dev,
2960 "Failed to request Mic detect IRQ: %d\n",
2961 ret);
2962 break;
Mark Brown3a423152010-11-26 15:21:06 +00002963 }
Mark Brown88766982010-03-29 20:57:12 +01002964
Mark Brown9e6e96a2010-01-29 17:47:12 +00002965 /* Remember if AIFnLRCLK is configured as a GPIO. This should be
2966 * configured on init - if a system wants to do this dynamically
2967 * at runtime we can deal with that then.
2968 */
2969 ret = wm8994_reg_read(codec->control_data, WM8994_GPIO_1);
2970 if (ret < 0) {
2971 dev_err(codec->dev, "Failed to read GPIO1 state: %d\n", ret);
Mark Brown88766982010-03-29 20:57:12 +01002972 goto err_irq;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002973 }
2974 if ((ret & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
2975 wm8994->lrclk_shared[0] = 1;
2976 wm8994_dai[0].symmetric_rates = 1;
2977 } else {
2978 wm8994->lrclk_shared[0] = 0;
2979 }
2980
2981 ret = wm8994_reg_read(codec->control_data, WM8994_GPIO_6);
2982 if (ret < 0) {
2983 dev_err(codec->dev, "Failed to read GPIO6 state: %d\n", ret);
Mark Brown88766982010-03-29 20:57:12 +01002984 goto err_irq;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002985 }
2986 if ((ret & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
2987 wm8994->lrclk_shared[1] = 1;
2988 wm8994_dai[1].symmetric_rates = 1;
2989 } else {
2990 wm8994->lrclk_shared[1] = 0;
2991 }
2992
Mark Brown9e6e96a2010-01-29 17:47:12 +00002993 wm8994_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
2994
Mark Brown9e6e96a2010-01-29 17:47:12 +00002995 /* Latch volume updates (right only; we always do left then right). */
2996 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_RIGHT_VOLUME,
2997 WM8994_AIF1DAC1_VU, WM8994_AIF1DAC1_VU);
2998 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_RIGHT_VOLUME,
2999 WM8994_AIF1DAC2_VU, WM8994_AIF1DAC2_VU);
3000 snd_soc_update_bits(codec, WM8994_AIF2_DAC_RIGHT_VOLUME,
3001 WM8994_AIF2DAC_VU, WM8994_AIF2DAC_VU);
3002 snd_soc_update_bits(codec, WM8994_AIF1_ADC1_RIGHT_VOLUME,
3003 WM8994_AIF1ADC1_VU, WM8994_AIF1ADC1_VU);
3004 snd_soc_update_bits(codec, WM8994_AIF1_ADC2_RIGHT_VOLUME,
3005 WM8994_AIF1ADC2_VU, WM8994_AIF1ADC2_VU);
3006 snd_soc_update_bits(codec, WM8994_AIF2_ADC_RIGHT_VOLUME,
3007 WM8994_AIF2ADC_VU, WM8994_AIF1ADC2_VU);
3008 snd_soc_update_bits(codec, WM8994_DAC1_RIGHT_VOLUME,
3009 WM8994_DAC1_VU, WM8994_DAC1_VU);
3010 snd_soc_update_bits(codec, WM8994_DAC2_RIGHT_VOLUME,
3011 WM8994_DAC2_VU, WM8994_DAC2_VU);
3012
3013 /* Set the low bit of the 3D stereo depth so TLV matches */
3014 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_FILTERS_2,
3015 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT,
3016 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT);
3017 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_FILTERS_2,
3018 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT,
3019 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT);
3020 snd_soc_update_bits(codec, WM8994_AIF2_DAC_FILTERS_2,
3021 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT,
3022 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT);
3023
Mark Brownd1ce6b22010-07-20 10:13:14 +01003024 /* Unconditionally enable AIF1 ADC TDM mode; it only affects
3025 * behaviour on idle TDM clock cycles. */
3026 snd_soc_update_bits(codec, WM8994_AIF1_CONTROL_1,
3027 WM8994_AIF1ADC_TDM, WM8994_AIF1ADC_TDM);
3028
Mark Brown9e6e96a2010-01-29 17:47:12 +00003029 wm8994_update_class_w(codec);
3030
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003031 wm8994_handle_pdata(wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003032
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003033 wm_hubs_add_analogue_controls(codec);
3034 snd_soc_add_controls(codec, wm8994_snd_controls,
3035 ARRAY_SIZE(wm8994_snd_controls));
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003036 snd_soc_dapm_new_controls(dapm, wm8994_dapm_widgets,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003037 ARRAY_SIZE(wm8994_dapm_widgets));
Mark Brownc4431df2010-11-26 15:21:07 +00003038
3039 switch (control->type) {
3040 case WM8994:
3041 snd_soc_dapm_new_controls(dapm, wm8994_specific_dapm_widgets,
3042 ARRAY_SIZE(wm8994_specific_dapm_widgets));
3043 break;
3044 case WM8958:
3045 snd_soc_add_controls(codec, wm8958_snd_controls,
3046 ARRAY_SIZE(wm8958_snd_controls));
3047 snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
3048 ARRAY_SIZE(wm8958_dapm_widgets));
3049 break;
3050 }
3051
3052
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003053 wm_hubs_add_analogue_routes(codec, 0, 0);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003054 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
Mark Brown9e6e96a2010-01-29 17:47:12 +00003055
Mark Brownc4431df2010-11-26 15:21:07 +00003056 switch (control->type) {
3057 case WM8994:
3058 snd_soc_dapm_add_routes(dapm, wm8994_intercon,
3059 ARRAY_SIZE(wm8994_intercon));
3060 break;
3061 case WM8958:
3062 snd_soc_dapm_add_routes(dapm, wm8958_intercon,
3063 ARRAY_SIZE(wm8958_intercon));
3064 break;
3065 }
3066
Mark Brown9e6e96a2010-01-29 17:47:12 +00003067 return 0;
3068
Mark Brown88766982010-03-29 20:57:12 +01003069err_irq:
3070 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC2_SHRT, wm8994);
3071 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC2_DET, wm8994);
3072 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC1_SHRT, wm8994);
3073 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC1_DET, wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003074err:
3075 kfree(wm8994);
3076 return ret;
3077}
3078
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003079static int wm8994_codec_remove(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003080{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003081 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown3a423152010-11-26 15:21:06 +00003082 struct wm8994 *control = codec->control_data;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003083
3084 wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003085
Mark Brown39fb51a2010-11-26 17:23:43 +00003086 pm_runtime_disable(codec->dev);
3087
Mark Brown3a423152010-11-26 15:21:06 +00003088 switch (control->type) {
3089 case WM8994:
3090 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC2_SHRT,
3091 wm8994);
3092 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC2_DET,
3093 wm8994);
3094 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC1_SHRT,
3095 wm8994);
3096 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC1_DET,
3097 wm8994);
3098 break;
Mark Brown821edd22010-11-26 15:21:09 +00003099
3100 case WM8958:
3101 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC1_DET,
3102 wm8994);
3103 break;
Mark Brown3a423152010-11-26 15:21:06 +00003104 }
Axel Lin24fb2b12010-11-23 15:58:39 +08003105 kfree(wm8994->retune_mobile_texts);
3106 kfree(wm8994->drc_texts);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003107 kfree(wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003108
3109 return 0;
3110}
3111
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003112static struct snd_soc_codec_driver soc_codec_dev_wm8994 = {
3113 .probe = wm8994_codec_probe,
3114 .remove = wm8994_codec_remove,
3115 .suspend = wm8994_suspend,
3116 .resume = wm8994_resume,
Mark Brownca9aef52010-11-26 17:23:41 +00003117 .read = wm8994_read,
3118 .write = wm8994_write,
Mark Browneba19fd2010-11-19 16:09:15 +00003119 .readable_register = wm8994_readable,
3120 .volatile_register = wm8994_volatile,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003121 .set_bias_level = wm8994_set_bias_level,
Mark Brownca9aef52010-11-26 17:23:41 +00003122
3123 .reg_cache_size = WM8994_CACHE_SIZE,
3124 .reg_cache_default = wm8994_reg_defaults,
3125 .reg_word_size = 2,
Mark Brown2e19b0c2010-11-26 17:23:42 +00003126 .compress_type = SND_SOC_RBTREE_COMPRESSION,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003127};
3128
3129static int __devinit wm8994_probe(struct platform_device *pdev)
3130{
3131 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wm8994,
3132 wm8994_dai, ARRAY_SIZE(wm8994_dai));
3133}
3134
3135static int __devexit wm8994_remove(struct platform_device *pdev)
3136{
3137 snd_soc_unregister_codec(&pdev->dev);
3138 return 0;
3139}
3140
Mark Brown9e6e96a2010-01-29 17:47:12 +00003141static struct platform_driver wm8994_codec_driver = {
3142 .driver = {
3143 .name = "wm8994-codec",
3144 .owner = THIS_MODULE,
3145 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003146 .probe = wm8994_probe,
3147 .remove = __devexit_p(wm8994_remove),
Mark Brown9e6e96a2010-01-29 17:47:12 +00003148};
3149
3150static __init int wm8994_init(void)
3151{
3152 return platform_driver_register(&wm8994_codec_driver);
3153}
3154module_init(wm8994_init);
3155
3156static __exit void wm8994_exit(void)
3157{
3158 platform_driver_unregister(&wm8994_codec_driver);
3159}
3160module_exit(wm8994_exit);
3161
3162
3163MODULE_DESCRIPTION("ASoC WM8994 driver");
3164MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
3165MODULE_LICENSE("GPL");
3166MODULE_ALIAS("platform:wm8994-codec");