blob: 0dc14115f109675e61aadba2471c9bf0968c8a1f [file] [log] [blame]
Mark Brown9e6e96a2010-01-29 17:47:12 +00001/*
2 * wm8994.c -- WM8994 ALSA SoC Audio driver
3 *
4 * Copyright 2009 Wolfson Microelectronics plc
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/init.h>
17#include <linux/delay.h>
18#include <linux/pm.h>
19#include <linux/i2c.h>
20#include <linux/platform_device.h>
Mark Brown39fb51a2010-11-26 17:23:43 +000021#include <linux/pm_runtime.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000022#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000024#include <sound/core.h>
Mark Brown821edd22010-11-26 15:21:09 +000025#include <sound/jack.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000026#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/soc.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000029#include <sound/initval.h>
30#include <sound/tlv.h>
Mark Brown2bbb5d62010-12-05 12:50:12 +000031#include <trace/events/asoc.h>
Mark Brown9e6e96a2010-01-29 17:47:12 +000032
33#include <linux/mfd/wm8994/core.h>
34#include <linux/mfd/wm8994/registers.h>
35#include <linux/mfd/wm8994/pdata.h>
36#include <linux/mfd/wm8994/gpio.h>
37
38#include "wm8994.h"
39#include "wm_hubs.h"
40
Mark Brown9e6e96a2010-01-29 17:47:12 +000041struct fll_config {
42 int src;
43 int in;
44 int out;
45};
46
47#define WM8994_NUM_DRC 3
48#define WM8994_NUM_EQ 3
49
50static int wm8994_drc_base[] = {
51 WM8994_AIF1_DRC1_1,
52 WM8994_AIF1_DRC2_1,
53 WM8994_AIF2_DRC_1,
54};
55
56static int wm8994_retune_mobile_base[] = {
57 WM8994_AIF1_DAC1_EQ_GAINS_1,
58 WM8994_AIF1_DAC2_EQ_GAINS_1,
59 WM8994_AIF2_EQ_GAINS_1,
60};
61
Mark Brown88766982010-03-29 20:57:12 +010062struct wm8994_micdet {
63 struct snd_soc_jack *jack;
64 int det;
65 int shrt;
66};
67
Mark Brown9e6e96a2010-01-29 17:47:12 +000068/* codec private data */
69struct wm8994_priv {
70 struct wm_hubs_data hubs;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000071 enum snd_soc_control_type control_type;
72 void *control_data;
73 struct snd_soc_codec *codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +000074 int sysclk[2];
75 int sysclk_rate[2];
76 int mclk[2];
77 int aifclk[2];
78 struct fll_config fll[2], fll_suspend[2];
79
80 int dac_rates[2];
81 int lrclk_shared[2];
82
Mark Brownd6addcc2010-11-26 15:21:08 +000083 int mbc_ena[3];
84
Mark Brown9e6e96a2010-01-29 17:47:12 +000085 /* Platform dependant DRC configuration */
86 const char **drc_texts;
87 int drc_cfg[WM8994_NUM_DRC];
88 struct soc_enum drc_enum;
89
90 /* Platform dependant ReTune mobile configuration */
91 int num_retune_mobile_texts;
92 const char **retune_mobile_texts;
93 int retune_mobile_cfg[WM8994_NUM_EQ];
94 struct soc_enum retune_mobile_enum;
95
Mark Brown131d8102010-11-30 17:03:39 +000096 /* Platform dependant MBC configuration */
97 int mbc_cfg;
98 const char **mbc_texts;
99 struct soc_enum mbc_enum;
100
Mark Brown88766982010-03-29 20:57:12 +0100101 struct wm8994_micdet micdet[2];
102
Mark Brown821edd22010-11-26 15:21:09 +0000103 wm8958_micdet_cb jack_cb;
104 void *jack_cb_data;
Mark Brown9b7c5252011-02-17 20:05:44 -0800105 int micdet_irq;
Mark Brown821edd22010-11-26 15:21:09 +0000106
Mark Brownb6b05692010-08-13 12:58:20 +0100107 int revision;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000108 struct wm8994_pdata *pdata;
Dimitris Papastamos173efa02011-02-11 16:32:11 +0000109
110 unsigned int aif1clk_enable:1;
111 unsigned int aif2clk_enable:1;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000112};
113
Dimitris Papastamosd4754ec2011-01-13 12:20:37 +0000114static int wm8994_readable(struct snd_soc_codec *codec, unsigned int reg)
Mark Brown9e6e96a2010-01-29 17:47:12 +0000115{
Mark Browne88ff1e2010-07-09 00:12:08 +0900116 switch (reg) {
117 case WM8994_GPIO_1:
118 case WM8994_GPIO_2:
119 case WM8994_GPIO_3:
120 case WM8994_GPIO_4:
121 case WM8994_GPIO_5:
122 case WM8994_GPIO_6:
123 case WM8994_GPIO_7:
124 case WM8994_GPIO_8:
125 case WM8994_GPIO_9:
126 case WM8994_GPIO_10:
127 case WM8994_GPIO_11:
128 case WM8994_INTERRUPT_STATUS_1:
129 case WM8994_INTERRUPT_STATUS_2:
130 case WM8994_INTERRUPT_RAW_STATUS_2:
131 return 1;
132 default:
133 break;
134 }
135
Mark Brown7b306da2010-11-16 20:11:40 +0000136 if (reg >= WM8994_CACHE_SIZE)
Mark Brown9e6e96a2010-01-29 17:47:12 +0000137 return 0;
Mark Brown7b306da2010-11-16 20:11:40 +0000138 return wm8994_access_masks[reg].readable != 0;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000139}
140
Dimitris Papastamosd4754ec2011-01-13 12:20:37 +0000141static int wm8994_volatile(struct snd_soc_codec *codec, unsigned int reg)
Mark Brown9e6e96a2010-01-29 17:47:12 +0000142{
Mark Brownca9aef52010-11-26 17:23:41 +0000143 if (reg >= WM8994_CACHE_SIZE)
Mark Brown9e6e96a2010-01-29 17:47:12 +0000144 return 1;
145
146 switch (reg) {
147 case WM8994_SOFTWARE_RESET:
148 case WM8994_CHIP_REVISION:
149 case WM8994_DC_SERVO_1:
150 case WM8994_DC_SERVO_READBACK:
151 case WM8994_RATE_STATUS:
152 case WM8994_LDO_1:
153 case WM8994_LDO_2:
Mark Brownd6addcc2010-11-26 15:21:08 +0000154 case WM8958_DSP2_EXECCONTROL:
Mark Brown821edd22010-11-26 15:21:09 +0000155 case WM8958_MIC_DETECT_3:
Mark Brown9e6e96a2010-01-29 17:47:12 +0000156 return 1;
157 default:
158 return 0;
159 }
160}
161
162static int wm8994_write(struct snd_soc_codec *codec, unsigned int reg,
163 unsigned int value)
164{
Mark Brownca9aef52010-11-26 17:23:41 +0000165 int ret;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000166
167 BUG_ON(reg > WM8994_MAX_REGISTER);
168
Dimitris Papastamosd4754ec2011-01-13 12:20:37 +0000169 if (!wm8994_volatile(codec, reg)) {
Mark Brownca9aef52010-11-26 17:23:41 +0000170 ret = snd_soc_cache_write(codec, reg, value);
171 if (ret != 0)
172 dev_err(codec->dev, "Cache write to %x failed: %d\n",
173 reg, ret);
174 }
Mark Brown9e6e96a2010-01-29 17:47:12 +0000175
176 return wm8994_reg_write(codec->control_data, reg, value);
177}
178
179static unsigned int wm8994_read(struct snd_soc_codec *codec,
180 unsigned int reg)
181{
Mark Brownca9aef52010-11-26 17:23:41 +0000182 unsigned int val;
183 int ret;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000184
185 BUG_ON(reg > WM8994_MAX_REGISTER);
186
Dimitris Papastamosd4754ec2011-01-13 12:20:37 +0000187 if (!wm8994_volatile(codec, reg) && wm8994_readable(codec, reg) &&
Mark Brownca9aef52010-11-26 17:23:41 +0000188 reg < codec->driver->reg_cache_size) {
189 ret = snd_soc_cache_read(codec, reg, &val);
190 if (ret >= 0)
191 return val;
192 else
193 dev_err(codec->dev, "Cache read from %x failed: %d\n",
194 reg, ret);
195 }
196
197 return wm8994_reg_read(codec->control_data, reg);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000198}
199
200static int configure_aif_clock(struct snd_soc_codec *codec, int aif)
201{
Mark Brownb2c812e2010-04-14 15:35:19 +0900202 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000203 int rate;
204 int reg1 = 0;
205 int offset;
206
207 if (aif)
208 offset = 4;
209 else
210 offset = 0;
211
212 switch (wm8994->sysclk[aif]) {
213 case WM8994_SYSCLK_MCLK1:
214 rate = wm8994->mclk[0];
215 break;
216
217 case WM8994_SYSCLK_MCLK2:
218 reg1 |= 0x8;
219 rate = wm8994->mclk[1];
220 break;
221
222 case WM8994_SYSCLK_FLL1:
223 reg1 |= 0x10;
224 rate = wm8994->fll[0].out;
225 break;
226
227 case WM8994_SYSCLK_FLL2:
228 reg1 |= 0x18;
229 rate = wm8994->fll[1].out;
230 break;
231
232 default:
233 return -EINVAL;
234 }
235
236 if (rate >= 13500000) {
237 rate /= 2;
238 reg1 |= WM8994_AIF1CLK_DIV;
239
240 dev_dbg(codec->dev, "Dividing AIF%d clock to %dHz\n",
241 aif + 1, rate);
242 }
Mark Brown5e5e2be2010-04-25 12:20:30 +0100243
244 if (rate && rate < 3000000)
245 dev_warn(codec->dev, "AIF%dCLK is %dHz, should be >=3MHz for optimal performance\n",
246 aif + 1, rate);
247
Mark Brown9e6e96a2010-01-29 17:47:12 +0000248 wm8994->aifclk[aif] = rate;
249
250 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1 + offset,
251 WM8994_AIF1CLK_SRC_MASK | WM8994_AIF1CLK_DIV,
252 reg1);
253
254 return 0;
255}
256
257static int configure_clock(struct snd_soc_codec *codec)
258{
Mark Brownb2c812e2010-04-14 15:35:19 +0900259 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000260 int old, new;
261
262 /* Bring up the AIF clocks first */
263 configure_aif_clock(codec, 0);
264 configure_aif_clock(codec, 1);
265
266 /* Then switch CLK_SYS over to the higher of them; a change
267 * can only happen as a result of a clocking change which can
268 * only be made outside of DAPM so we can safely redo the
269 * clocking.
270 */
271
272 /* If they're equal it doesn't matter which is used */
273 if (wm8994->aifclk[0] == wm8994->aifclk[1])
274 return 0;
275
276 if (wm8994->aifclk[0] < wm8994->aifclk[1])
277 new = WM8994_SYSCLK_SRC;
278 else
279 new = 0;
280
281 old = snd_soc_read(codec, WM8994_CLOCKING_1) & WM8994_SYSCLK_SRC;
282
283 /* If there's no change then we're done. */
284 if (old == new)
285 return 0;
286
287 snd_soc_update_bits(codec, WM8994_CLOCKING_1, WM8994_SYSCLK_SRC, new);
288
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200289 snd_soc_dapm_sync(&codec->dapm);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000290
291 return 0;
292}
293
294static int check_clk_sys(struct snd_soc_dapm_widget *source,
295 struct snd_soc_dapm_widget *sink)
296{
297 int reg = snd_soc_read(source->codec, WM8994_CLOCKING_1);
298 const char *clk;
299
300 /* Check what we're currently using for CLK_SYS */
301 if (reg & WM8994_SYSCLK_SRC)
302 clk = "AIF2CLK";
303 else
304 clk = "AIF1CLK";
305
306 return strcmp(source->name, clk) == 0;
307}
308
309static const char *sidetone_hpf_text[] = {
310 "2.7kHz", "1.35kHz", "675Hz", "370Hz", "180Hz", "90Hz", "45Hz"
311};
312
313static const struct soc_enum sidetone_hpf =
314 SOC_ENUM_SINGLE(WM8994_SIDETONE, 7, 7, sidetone_hpf_text);
315
Uk Kim146fd572010-12-07 13:58:40 +0000316static const char *adc_hpf_text[] = {
317 "HiFi", "Voice 1", "Voice 2", "Voice 3"
318};
319
320static const struct soc_enum aif1adc1_hpf =
321 SOC_ENUM_SINGLE(WM8994_AIF1_ADC1_FILTERS, 13, 4, adc_hpf_text);
322
323static const struct soc_enum aif1adc2_hpf =
324 SOC_ENUM_SINGLE(WM8994_AIF1_ADC2_FILTERS, 13, 4, adc_hpf_text);
325
326static const struct soc_enum aif2adc_hpf =
327 SOC_ENUM_SINGLE(WM8994_AIF2_ADC_FILTERS, 13, 4, adc_hpf_text);
328
Mark Brown9e6e96a2010-01-29 17:47:12 +0000329static const DECLARE_TLV_DB_SCALE(aif_tlv, 0, 600, 0);
330static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
331static const DECLARE_TLV_DB_SCALE(st_tlv, -3600, 300, 0);
332static const DECLARE_TLV_DB_SCALE(wm8994_3d_tlv, -1600, 183, 0);
333static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
334
335#define WM8994_DRC_SWITCH(xname, reg, shift) \
336{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
337 .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,\
338 .put = wm8994_put_drc_sw, \
339 .private_value = SOC_SINGLE_VALUE(reg, shift, 1, 0) }
340
341static int wm8994_put_drc_sw(struct snd_kcontrol *kcontrol,
342 struct snd_ctl_elem_value *ucontrol)
343{
344 struct soc_mixer_control *mc =
345 (struct soc_mixer_control *)kcontrol->private_value;
346 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
347 int mask, ret;
348
349 /* Can't enable both ADC and DAC paths simultaneously */
350 if (mc->shift == WM8994_AIF1DAC1_DRC_ENA_SHIFT)
351 mask = WM8994_AIF1ADC1L_DRC_ENA_MASK |
352 WM8994_AIF1ADC1R_DRC_ENA_MASK;
353 else
354 mask = WM8994_AIF1DAC1_DRC_ENA_MASK;
355
356 ret = snd_soc_read(codec, mc->reg);
357 if (ret < 0)
358 return ret;
359 if (ret & mask)
360 return -EINVAL;
361
362 return snd_soc_put_volsw(kcontrol, ucontrol);
363}
364
Mark Brown9e6e96a2010-01-29 17:47:12 +0000365static void wm8994_set_drc(struct snd_soc_codec *codec, int drc)
366{
Mark Brownb2c812e2010-04-14 15:35:19 +0900367 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000368 struct wm8994_pdata *pdata = wm8994->pdata;
369 int base = wm8994_drc_base[drc];
370 int cfg = wm8994->drc_cfg[drc];
371 int save, i;
372
373 /* Save any enables; the configuration should clear them. */
374 save = snd_soc_read(codec, base);
375 save &= WM8994_AIF1DAC1_DRC_ENA | WM8994_AIF1ADC1L_DRC_ENA |
376 WM8994_AIF1ADC1R_DRC_ENA;
377
378 for (i = 0; i < WM8994_DRC_REGS; i++)
379 snd_soc_update_bits(codec, base + i, 0xffff,
380 pdata->drc_cfgs[cfg].regs[i]);
381
382 snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_DRC_ENA |
383 WM8994_AIF1ADC1L_DRC_ENA |
384 WM8994_AIF1ADC1R_DRC_ENA, save);
385}
386
387/* Icky as hell but saves code duplication */
388static int wm8994_get_drc(const char *name)
389{
390 if (strcmp(name, "AIF1DRC1 Mode") == 0)
391 return 0;
392 if (strcmp(name, "AIF1DRC2 Mode") == 0)
393 return 1;
394 if (strcmp(name, "AIF2DRC Mode") == 0)
395 return 2;
396 return -EINVAL;
397}
398
399static int wm8994_put_drc_enum(struct snd_kcontrol *kcontrol,
400 struct snd_ctl_elem_value *ucontrol)
401{
402 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000403 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000404 struct wm8994_pdata *pdata = wm8994->pdata;
405 int drc = wm8994_get_drc(kcontrol->id.name);
406 int value = ucontrol->value.integer.value[0];
407
408 if (drc < 0)
409 return drc;
410
411 if (value >= pdata->num_drc_cfgs)
412 return -EINVAL;
413
414 wm8994->drc_cfg[drc] = value;
415
416 wm8994_set_drc(codec, drc);
417
418 return 0;
419}
420
421static int wm8994_get_drc_enum(struct snd_kcontrol *kcontrol,
422 struct snd_ctl_elem_value *ucontrol)
423{
424 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +0900425 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000426 int drc = wm8994_get_drc(kcontrol->id.name);
427
428 ucontrol->value.enumerated.item[0] = wm8994->drc_cfg[drc];
429
430 return 0;
431}
432
433static void wm8994_set_retune_mobile(struct snd_soc_codec *codec, int block)
434{
Mark Brownb2c812e2010-04-14 15:35:19 +0900435 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000436 struct wm8994_pdata *pdata = wm8994->pdata;
437 int base = wm8994_retune_mobile_base[block];
438 int iface, best, best_val, save, i, cfg;
439
440 if (!pdata || !wm8994->num_retune_mobile_texts)
441 return;
442
443 switch (block) {
444 case 0:
445 case 1:
446 iface = 0;
447 break;
448 case 2:
449 iface = 1;
450 break;
451 default:
452 return;
453 }
454
455 /* Find the version of the currently selected configuration
456 * with the nearest sample rate. */
457 cfg = wm8994->retune_mobile_cfg[block];
458 best = 0;
459 best_val = INT_MAX;
460 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
461 if (strcmp(pdata->retune_mobile_cfgs[i].name,
462 wm8994->retune_mobile_texts[cfg]) == 0 &&
463 abs(pdata->retune_mobile_cfgs[i].rate
464 - wm8994->dac_rates[iface]) < best_val) {
465 best = i;
466 best_val = abs(pdata->retune_mobile_cfgs[i].rate
467 - wm8994->dac_rates[iface]);
468 }
469 }
470
471 dev_dbg(codec->dev, "ReTune Mobile %d %s/%dHz for %dHz sample rate\n",
472 block,
473 pdata->retune_mobile_cfgs[best].name,
474 pdata->retune_mobile_cfgs[best].rate,
475 wm8994->dac_rates[iface]);
476
477 /* The EQ will be disabled while reconfiguring it, remember the
478 * current configuration.
479 */
480 save = snd_soc_read(codec, base);
481 save &= WM8994_AIF1DAC1_EQ_ENA;
482
483 for (i = 0; i < WM8994_EQ_REGS; i++)
484 snd_soc_update_bits(codec, base + i, 0xffff,
485 pdata->retune_mobile_cfgs[best].regs[i]);
486
487 snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_EQ_ENA, save);
488}
489
490/* Icky as hell but saves code duplication */
491static int wm8994_get_retune_mobile_block(const char *name)
492{
493 if (strcmp(name, "AIF1.1 EQ Mode") == 0)
494 return 0;
495 if (strcmp(name, "AIF1.2 EQ Mode") == 0)
496 return 1;
497 if (strcmp(name, "AIF2 EQ Mode") == 0)
498 return 2;
499 return -EINVAL;
500}
501
502static int wm8994_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
503 struct snd_ctl_elem_value *ucontrol)
504{
505 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000506 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000507 struct wm8994_pdata *pdata = wm8994->pdata;
508 int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
509 int value = ucontrol->value.integer.value[0];
510
511 if (block < 0)
512 return block;
513
514 if (value >= pdata->num_retune_mobile_cfgs)
515 return -EINVAL;
516
517 wm8994->retune_mobile_cfg[block] = value;
518
519 wm8994_set_retune_mobile(codec, block);
520
521 return 0;
522}
523
524static int wm8994_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
525 struct snd_ctl_elem_value *ucontrol)
526{
527 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brown4a8d9292011-02-16 14:57:17 -0800528 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000529 int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
530
531 ucontrol->value.enumerated.item[0] = wm8994->retune_mobile_cfg[block];
532
533 return 0;
534}
535
Mark Brown96b101e2010-11-18 15:49:38 +0000536static const char *aif_chan_src_text[] = {
Mark Brownf5548852010-08-31 19:39:48 +0100537 "Left", "Right"
538};
539
Mark Brown96b101e2010-11-18 15:49:38 +0000540static const struct soc_enum aif1adcl_src =
541 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_1, 15, 2, aif_chan_src_text);
542
543static const struct soc_enum aif1adcr_src =
544 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_1, 14, 2, aif_chan_src_text);
545
546static const struct soc_enum aif2adcl_src =
547 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_1, 15, 2, aif_chan_src_text);
548
549static const struct soc_enum aif2adcr_src =
550 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_1, 14, 2, aif_chan_src_text);
551
Mark Brownf5548852010-08-31 19:39:48 +0100552static const struct soc_enum aif1dacl_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000553 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_2, 15, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100554
555static const struct soc_enum aif1dacr_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000556 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_2, 14, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100557
558static const struct soc_enum aif2dacl_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000559 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_2, 15, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100560
561static const struct soc_enum aif2dacr_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000562 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_2, 14, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100563
Mark Brown154b26a2010-12-09 12:07:44 +0000564static const char *osr_text[] = {
565 "Low Power", "High Performance",
566};
567
568static const struct soc_enum dac_osr =
569 SOC_ENUM_SINGLE(WM8994_OVERSAMPLING, 0, 2, osr_text);
570
571static const struct soc_enum adc_osr =
572 SOC_ENUM_SINGLE(WM8994_OVERSAMPLING, 1, 2, osr_text);
573
Mark Brownd6addcc2010-11-26 15:21:08 +0000574static void wm8958_mbc_apply(struct snd_soc_codec *codec, int mbc, int start)
575{
576 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown131d8102010-11-30 17:03:39 +0000577 struct wm8994_pdata *pdata = wm8994->pdata;
Mark Brownd6addcc2010-11-26 15:21:08 +0000578 int pwr_reg = snd_soc_read(codec, WM8994_POWER_MANAGEMENT_5);
Mark Brown131d8102010-11-30 17:03:39 +0000579 int ena, reg, aif, i;
Mark Brownd6addcc2010-11-26 15:21:08 +0000580
581 switch (mbc) {
582 case 0:
583 pwr_reg &= (WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC1R_ENA);
584 aif = 0;
585 break;
586 case 1:
587 pwr_reg &= (WM8994_AIF1DAC2L_ENA | WM8994_AIF1DAC2R_ENA);
588 aif = 0;
589 break;
590 case 2:
591 pwr_reg &= (WM8994_AIF2DACL_ENA | WM8994_AIF2DACR_ENA);
592 aif = 1;
593 break;
594 default:
595 BUG();
596 return;
597 }
598
599 /* We can only enable the MBC if the AIF is enabled and we
600 * want it to be enabled. */
601 ena = pwr_reg && wm8994->mbc_ena[mbc];
602
603 reg = snd_soc_read(codec, WM8958_DSP2_PROGRAM);
604
605 dev_dbg(codec->dev, "MBC %d startup: %d, power: %x, DSP: %x\n",
606 mbc, start, pwr_reg, reg);
607
608 if (start && ena) {
609 /* If the DSP is already running then noop */
610 if (reg & WM8958_DSP2_ENA)
611 return;
612
613 /* Switch the clock over to the appropriate AIF */
614 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
615 WM8958_DSP2CLK_SRC | WM8958_DSP2CLK_ENA,
616 aif << WM8958_DSP2CLK_SRC_SHIFT |
617 WM8958_DSP2CLK_ENA);
618
619 snd_soc_update_bits(codec, WM8958_DSP2_PROGRAM,
620 WM8958_DSP2_ENA, WM8958_DSP2_ENA);
621
Mark Brown131d8102010-11-30 17:03:39 +0000622 /* If we've got user supplied MBC settings use them */
623 if (pdata && pdata->num_mbc_cfgs) {
624 struct wm8958_mbc_cfg *cfg
625 = &pdata->mbc_cfgs[wm8994->mbc_cfg];
626
627 for (i = 0; i < ARRAY_SIZE(cfg->coeff_regs); i++)
628 snd_soc_write(codec, i + WM8958_MBC_BAND_1_K_1,
629 cfg->coeff_regs[i]);
630
631 for (i = 0; i < ARRAY_SIZE(cfg->cutoff_regs); i++)
632 snd_soc_write(codec,
633 i + WM8958_MBC_BAND_2_LOWER_CUTOFF_C1_1,
634 cfg->cutoff_regs[i]);
635 }
Mark Brownd6addcc2010-11-26 15:21:08 +0000636
637 /* Run the DSP */
638 snd_soc_write(codec, WM8958_DSP2_EXECCONTROL,
639 WM8958_DSP2_RUNR);
640
641 /* And we're off! */
642 snd_soc_update_bits(codec, WM8958_DSP2_CONFIG,
643 WM8958_MBC_ENA | WM8958_MBC_SEL_MASK,
644 mbc << WM8958_MBC_SEL_SHIFT |
645 WM8958_MBC_ENA);
646 } else {
647 /* If the DSP is already stopped then noop */
648 if (!(reg & WM8958_DSP2_ENA))
649 return;
650
651 snd_soc_update_bits(codec, WM8958_DSP2_CONFIG,
652 WM8958_MBC_ENA, 0);
653 snd_soc_update_bits(codec, WM8958_DSP2_PROGRAM,
654 WM8958_DSP2_ENA, 0);
655 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
656 WM8958_DSP2CLK_ENA, 0);
657 }
658}
659
660static int wm8958_aif_ev(struct snd_soc_dapm_widget *w,
661 struct snd_kcontrol *kcontrol, int event)
662{
663 struct snd_soc_codec *codec = w->codec;
664 int mbc;
665
666 switch (w->shift) {
667 case 13:
668 case 12:
669 mbc = 2;
670 break;
671 case 11:
672 case 10:
673 mbc = 1;
674 break;
675 case 9:
676 case 8:
677 mbc = 0;
678 break;
679 default:
680 BUG();
681 return -EINVAL;
682 }
683
684 switch (event) {
685 case SND_SOC_DAPM_POST_PMU:
686 wm8958_mbc_apply(codec, mbc, 1);
687 break;
688 case SND_SOC_DAPM_POST_PMD:
689 wm8958_mbc_apply(codec, mbc, 0);
690 break;
691 }
692
693 return 0;
694}
695
Mark Brown131d8102010-11-30 17:03:39 +0000696static int wm8958_put_mbc_enum(struct snd_kcontrol *kcontrol,
697 struct snd_ctl_elem_value *ucontrol)
698{
699 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
700 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
701 struct wm8994_pdata *pdata = wm8994->pdata;
702 int value = ucontrol->value.integer.value[0];
703 int reg;
704
705 /* Don't allow on the fly reconfiguration */
706 reg = snd_soc_read(codec, WM8994_CLOCKING_1);
707 if (reg < 0 || reg & WM8958_DSP2CLK_ENA)
708 return -EBUSY;
709
710 if (value >= pdata->num_mbc_cfgs)
711 return -EINVAL;
712
713 wm8994->mbc_cfg = value;
714
715 return 0;
716}
717
718static int wm8958_get_mbc_enum(struct snd_kcontrol *kcontrol,
719 struct snd_ctl_elem_value *ucontrol)
720{
721 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
722 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
723
724 ucontrol->value.enumerated.item[0] = wm8994->mbc_cfg;
725
726 return 0;
727}
728
Mark Brownd6addcc2010-11-26 15:21:08 +0000729static int wm8958_mbc_info(struct snd_kcontrol *kcontrol,
730 struct snd_ctl_elem_info *uinfo)
731{
732 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
733 uinfo->count = 1;
734 uinfo->value.integer.min = 0;
735 uinfo->value.integer.max = 1;
736 return 0;
737}
738
739static int wm8958_mbc_get(struct snd_kcontrol *kcontrol,
740 struct snd_ctl_elem_value *ucontrol)
741{
742 int mbc = kcontrol->private_value;
743 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
744 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
745
746 ucontrol->value.integer.value[0] = wm8994->mbc_ena[mbc];
747
748 return 0;
749}
750
751static int wm8958_mbc_put(struct snd_kcontrol *kcontrol,
752 struct snd_ctl_elem_value *ucontrol)
753{
754 int mbc = kcontrol->private_value;
755 int i;
756 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
757 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
758
759 if (ucontrol->value.integer.value[0] > 1)
760 return -EINVAL;
761
762 for (i = 0; i < ARRAY_SIZE(wm8994->mbc_ena); i++) {
763 if (mbc != i && wm8994->mbc_ena[i]) {
764 dev_dbg(codec->dev, "MBC %d active already\n", mbc);
765 return -EBUSY;
766 }
767 }
768
769 wm8994->mbc_ena[mbc] = ucontrol->value.integer.value[0];
770
771 wm8958_mbc_apply(codec, mbc, wm8994->mbc_ena[mbc]);
772
773 return 0;
774}
775
776#define WM8958_MBC_SWITCH(xname, xval) {\
777 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
778 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,\
779 .info = wm8958_mbc_info, \
780 .get = wm8958_mbc_get, .put = wm8958_mbc_put, \
781 .private_value = xval }
782
Mark Brown9e6e96a2010-01-29 17:47:12 +0000783static const struct snd_kcontrol_new wm8994_snd_controls[] = {
784SOC_DOUBLE_R_TLV("AIF1ADC1 Volume", WM8994_AIF1_ADC1_LEFT_VOLUME,
785 WM8994_AIF1_ADC1_RIGHT_VOLUME,
786 1, 119, 0, digital_tlv),
787SOC_DOUBLE_R_TLV("AIF1ADC2 Volume", WM8994_AIF1_ADC2_LEFT_VOLUME,
788 WM8994_AIF1_ADC2_RIGHT_VOLUME,
789 1, 119, 0, digital_tlv),
790SOC_DOUBLE_R_TLV("AIF2ADC Volume", WM8994_AIF2_ADC_LEFT_VOLUME,
791 WM8994_AIF2_ADC_RIGHT_VOLUME,
792 1, 119, 0, digital_tlv),
793
Mark Brown96b101e2010-11-18 15:49:38 +0000794SOC_ENUM("AIF1ADCL Source", aif1adcl_src),
795SOC_ENUM("AIF1ADCR Source", aif1adcr_src),
Mark Brown49db7e72010-12-08 13:49:43 +0000796SOC_ENUM("AIF2ADCL Source", aif2adcl_src),
797SOC_ENUM("AIF2ADCR Source", aif2adcr_src),
Mark Brown96b101e2010-11-18 15:49:38 +0000798
Mark Brownf5548852010-08-31 19:39:48 +0100799SOC_ENUM("AIF1DACL Source", aif1dacl_src),
800SOC_ENUM("AIF1DACR Source", aif1dacr_src),
Mark Brown49db7e72010-12-08 13:49:43 +0000801SOC_ENUM("AIF2DACL Source", aif2dacl_src),
802SOC_ENUM("AIF2DACR Source", aif2dacr_src),
Mark Brownf5548852010-08-31 19:39:48 +0100803
Mark Brown9e6e96a2010-01-29 17:47:12 +0000804SOC_DOUBLE_R_TLV("AIF1DAC1 Volume", WM8994_AIF1_DAC1_LEFT_VOLUME,
805 WM8994_AIF1_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
806SOC_DOUBLE_R_TLV("AIF1DAC2 Volume", WM8994_AIF1_DAC2_LEFT_VOLUME,
807 WM8994_AIF1_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
808SOC_DOUBLE_R_TLV("AIF2DAC Volume", WM8994_AIF2_DAC_LEFT_VOLUME,
809 WM8994_AIF2_DAC_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
810
811SOC_SINGLE_TLV("AIF1 Boost Volume", WM8994_AIF1_CONTROL_2, 10, 3, 0, aif_tlv),
812SOC_SINGLE_TLV("AIF2 Boost Volume", WM8994_AIF2_CONTROL_2, 10, 3, 0, aif_tlv),
813
814SOC_SINGLE("AIF1DAC1 EQ Switch", WM8994_AIF1_DAC1_EQ_GAINS_1, 0, 1, 0),
815SOC_SINGLE("AIF1DAC2 EQ Switch", WM8994_AIF1_DAC2_EQ_GAINS_1, 0, 1, 0),
816SOC_SINGLE("AIF2 EQ Switch", WM8994_AIF2_EQ_GAINS_1, 0, 1, 0),
817
818WM8994_DRC_SWITCH("AIF1DAC1 DRC Switch", WM8994_AIF1_DRC1_1, 2),
819WM8994_DRC_SWITCH("AIF1ADC1L DRC Switch", WM8994_AIF1_DRC1_1, 1),
820WM8994_DRC_SWITCH("AIF1ADC1R DRC Switch", WM8994_AIF1_DRC1_1, 0),
821
822WM8994_DRC_SWITCH("AIF1DAC2 DRC Switch", WM8994_AIF1_DRC2_1, 2),
823WM8994_DRC_SWITCH("AIF1ADC2L DRC Switch", WM8994_AIF1_DRC2_1, 1),
824WM8994_DRC_SWITCH("AIF1ADC2R DRC Switch", WM8994_AIF1_DRC2_1, 0),
825
826WM8994_DRC_SWITCH("AIF2DAC DRC Switch", WM8994_AIF2_DRC_1, 2),
827WM8994_DRC_SWITCH("AIF2ADCL DRC Switch", WM8994_AIF2_DRC_1, 1),
828WM8994_DRC_SWITCH("AIF2ADCR DRC Switch", WM8994_AIF2_DRC_1, 0),
829
830SOC_SINGLE_TLV("DAC1 Right Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
831 5, 12, 0, st_tlv),
832SOC_SINGLE_TLV("DAC1 Left Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
833 0, 12, 0, st_tlv),
834SOC_SINGLE_TLV("DAC2 Right Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
835 5, 12, 0, st_tlv),
836SOC_SINGLE_TLV("DAC2 Left Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
837 0, 12, 0, st_tlv),
838SOC_ENUM("Sidetone HPF Mux", sidetone_hpf),
839SOC_SINGLE("Sidetone HPF Switch", WM8994_SIDETONE, 6, 1, 0),
840
Uk Kim146fd572010-12-07 13:58:40 +0000841SOC_ENUM("AIF1ADC1 HPF Mode", aif1adc1_hpf),
842SOC_DOUBLE("AIF1ADC1 HPF Switch", WM8994_AIF1_ADC1_FILTERS, 12, 11, 1, 0),
843
844SOC_ENUM("AIF1ADC2 HPF Mode", aif1adc2_hpf),
845SOC_DOUBLE("AIF1ADC2 HPF Switch", WM8994_AIF1_ADC2_FILTERS, 12, 11, 1, 0),
846
847SOC_ENUM("AIF2ADC HPF Mode", aif2adc_hpf),
848SOC_DOUBLE("AIF2ADC HPF Switch", WM8994_AIF2_ADC_FILTERS, 12, 11, 1, 0),
849
Mark Brown154b26a2010-12-09 12:07:44 +0000850SOC_ENUM("ADC OSR", adc_osr),
851SOC_ENUM("DAC OSR", dac_osr),
852
Mark Brown9e6e96a2010-01-29 17:47:12 +0000853SOC_DOUBLE_R_TLV("DAC1 Volume", WM8994_DAC1_LEFT_VOLUME,
854 WM8994_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
855SOC_DOUBLE_R("DAC1 Switch", WM8994_DAC1_LEFT_VOLUME,
856 WM8994_DAC1_RIGHT_VOLUME, 9, 1, 1),
857
858SOC_DOUBLE_R_TLV("DAC2 Volume", WM8994_DAC2_LEFT_VOLUME,
859 WM8994_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
860SOC_DOUBLE_R("DAC2 Switch", WM8994_DAC2_LEFT_VOLUME,
861 WM8994_DAC2_RIGHT_VOLUME, 9, 1, 1),
862
863SOC_SINGLE_TLV("SPKL DAC2 Volume", WM8994_SPKMIXL_ATTENUATION,
864 6, 1, 1, wm_hubs_spkmix_tlv),
865SOC_SINGLE_TLV("SPKL DAC1 Volume", WM8994_SPKMIXL_ATTENUATION,
866 2, 1, 1, wm_hubs_spkmix_tlv),
867
868SOC_SINGLE_TLV("SPKR DAC2 Volume", WM8994_SPKMIXR_ATTENUATION,
869 6, 1, 1, wm_hubs_spkmix_tlv),
870SOC_SINGLE_TLV("SPKR DAC1 Volume", WM8994_SPKMIXR_ATTENUATION,
871 2, 1, 1, wm_hubs_spkmix_tlv),
872
873SOC_SINGLE_TLV("AIF1DAC1 3D Stereo Volume", WM8994_AIF1_DAC1_FILTERS_2,
874 10, 15, 0, wm8994_3d_tlv),
Mark Brown458350b2010-12-20 14:35:09 +0000875SOC_SINGLE("AIF1DAC1 3D Stereo Switch", WM8994_AIF1_DAC1_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000876 8, 1, 0),
877SOC_SINGLE_TLV("AIF1DAC2 3D Stereo Volume", WM8994_AIF1_DAC2_FILTERS_2,
878 10, 15, 0, wm8994_3d_tlv),
879SOC_SINGLE("AIF1DAC2 3D Stereo Switch", WM8994_AIF1_DAC2_FILTERS_2,
880 8, 1, 0),
Mark Brown458350b2010-12-20 14:35:09 +0000881SOC_SINGLE_TLV("AIF2DAC 3D Stereo Volume", WM8994_AIF2_DAC_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000882 10, 15, 0, wm8994_3d_tlv),
Mark Brown458350b2010-12-20 14:35:09 +0000883SOC_SINGLE("AIF2DAC 3D Stereo Switch", WM8994_AIF2_DAC_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000884 8, 1, 0),
885};
886
887static const struct snd_kcontrol_new wm8994_eq_controls[] = {
888SOC_SINGLE_TLV("AIF1DAC1 EQ1 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 11, 31, 0,
889 eq_tlv),
890SOC_SINGLE_TLV("AIF1DAC1 EQ2 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 6, 31, 0,
891 eq_tlv),
892SOC_SINGLE_TLV("AIF1DAC1 EQ3 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 1, 31, 0,
893 eq_tlv),
894SOC_SINGLE_TLV("AIF1DAC1 EQ4 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 11, 31, 0,
895 eq_tlv),
896SOC_SINGLE_TLV("AIF1DAC1 EQ5 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 6, 31, 0,
897 eq_tlv),
898
899SOC_SINGLE_TLV("AIF1DAC2 EQ1 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 11, 31, 0,
900 eq_tlv),
901SOC_SINGLE_TLV("AIF1DAC2 EQ2 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 6, 31, 0,
902 eq_tlv),
903SOC_SINGLE_TLV("AIF1DAC2 EQ3 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 1, 31, 0,
904 eq_tlv),
905SOC_SINGLE_TLV("AIF1DAC2 EQ4 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 11, 31, 0,
906 eq_tlv),
907SOC_SINGLE_TLV("AIF1DAC2 EQ5 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 6, 31, 0,
908 eq_tlv),
909
910SOC_SINGLE_TLV("AIF2 EQ1 Volume", WM8994_AIF2_EQ_GAINS_1, 11, 31, 0,
911 eq_tlv),
912SOC_SINGLE_TLV("AIF2 EQ2 Volume", WM8994_AIF2_EQ_GAINS_1, 6, 31, 0,
913 eq_tlv),
914SOC_SINGLE_TLV("AIF2 EQ3 Volume", WM8994_AIF2_EQ_GAINS_1, 1, 31, 0,
915 eq_tlv),
916SOC_SINGLE_TLV("AIF2 EQ4 Volume", WM8994_AIF2_EQ_GAINS_2, 11, 31, 0,
917 eq_tlv),
918SOC_SINGLE_TLV("AIF2 EQ5 Volume", WM8994_AIF2_EQ_GAINS_2, 6, 31, 0,
919 eq_tlv),
920};
921
Mark Brownc4431df2010-11-26 15:21:07 +0000922static const struct snd_kcontrol_new wm8958_snd_controls[] = {
923SOC_SINGLE_TLV("AIF3 Boost Volume", WM8958_AIF3_CONTROL_2, 10, 3, 0, aif_tlv),
Mark Brownd6addcc2010-11-26 15:21:08 +0000924WM8958_MBC_SWITCH("AIF1DAC1 MBC Switch", 0),
925WM8958_MBC_SWITCH("AIF1DAC2 MBC Switch", 1),
926WM8958_MBC_SWITCH("AIF2DAC MBC Switch", 2),
Mark Brownc4431df2010-11-26 15:21:07 +0000927};
928
Mark Brown9e6e96a2010-01-29 17:47:12 +0000929static int clk_sys_event(struct snd_soc_dapm_widget *w,
930 struct snd_kcontrol *kcontrol, int event)
931{
932 struct snd_soc_codec *codec = w->codec;
933
934 switch (event) {
935 case SND_SOC_DAPM_PRE_PMU:
936 return configure_clock(codec);
937
938 case SND_SOC_DAPM_POST_PMD:
939 configure_clock(codec);
940 break;
941 }
942
943 return 0;
944}
945
946static void wm8994_update_class_w(struct snd_soc_codec *codec)
947{
Mark Brownfec6dd82010-10-27 13:48:36 -0700948 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000949 int enable = 1;
950 int source = 0; /* GCC flow analysis can't track enable */
951 int reg, reg_r;
952
953 /* Only support direct DAC->headphone paths */
954 reg = snd_soc_read(codec, WM8994_OUTPUT_MIXER_1);
955 if (!(reg & WM8994_DAC1L_TO_HPOUT1L)) {
Mark Brownee839a22010-04-20 13:57:08 +0900956 dev_vdbg(codec->dev, "HPL connected to output mixer\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000957 enable = 0;
958 }
959
960 reg = snd_soc_read(codec, WM8994_OUTPUT_MIXER_2);
961 if (!(reg & WM8994_DAC1R_TO_HPOUT1R)) {
Mark Brownee839a22010-04-20 13:57:08 +0900962 dev_vdbg(codec->dev, "HPR connected to output mixer\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000963 enable = 0;
964 }
965
966 /* We also need the same setting for L/R and only one path */
967 reg = snd_soc_read(codec, WM8994_DAC1_LEFT_MIXER_ROUTING);
968 switch (reg) {
969 case WM8994_AIF2DACL_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +0900970 dev_vdbg(codec->dev, "Class W source AIF2DAC\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000971 source = 2 << WM8994_CP_DYN_SRC_SEL_SHIFT;
972 break;
973 case WM8994_AIF1DAC2L_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +0900974 dev_vdbg(codec->dev, "Class W source AIF1DAC2\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000975 source = 1 << WM8994_CP_DYN_SRC_SEL_SHIFT;
976 break;
977 case WM8994_AIF1DAC1L_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +0900978 dev_vdbg(codec->dev, "Class W source AIF1DAC1\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000979 source = 0 << WM8994_CP_DYN_SRC_SEL_SHIFT;
980 break;
981 default:
Mark Brownee839a22010-04-20 13:57:08 +0900982 dev_vdbg(codec->dev, "DAC mixer setting: %x\n", reg);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000983 enable = 0;
984 break;
985 }
986
987 reg_r = snd_soc_read(codec, WM8994_DAC1_RIGHT_MIXER_ROUTING);
988 if (reg_r != reg) {
Mark Brownee839a22010-04-20 13:57:08 +0900989 dev_vdbg(codec->dev, "Left and right DAC mixers different\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000990 enable = 0;
991 }
992
993 if (enable) {
994 dev_dbg(codec->dev, "Class W enabled\n");
995 snd_soc_update_bits(codec, WM8994_CLASS_W_1,
996 WM8994_CP_DYN_PWR |
997 WM8994_CP_DYN_SRC_SEL_MASK,
998 source | WM8994_CP_DYN_PWR);
Mark Brownfec6dd82010-10-27 13:48:36 -0700999 wm8994->hubs.class_w = true;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001000
1001 } else {
1002 dev_dbg(codec->dev, "Class W disabled\n");
1003 snd_soc_update_bits(codec, WM8994_CLASS_W_1,
1004 WM8994_CP_DYN_PWR, 0);
Mark Brownfec6dd82010-10-27 13:48:36 -07001005 wm8994->hubs.class_w = false;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001006 }
1007}
1008
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001009static int late_enable_ev(struct snd_soc_dapm_widget *w,
1010 struct snd_kcontrol *kcontrol, int event)
1011{
1012 struct snd_soc_codec *codec = w->codec;
1013 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1014
1015 switch (event) {
1016 case SND_SOC_DAPM_PRE_PMU:
1017 if (wm8994->aif1clk_enable)
1018 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1019 WM8994_AIF1CLK_ENA_MASK,
1020 WM8994_AIF1CLK_ENA);
1021 if (wm8994->aif2clk_enable)
1022 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1023 WM8994_AIF2CLK_ENA_MASK,
1024 WM8994_AIF2CLK_ENA);
1025 break;
1026 }
1027
1028 return 0;
1029}
1030
1031static int late_disable_ev(struct snd_soc_dapm_widget *w,
1032 struct snd_kcontrol *kcontrol, int event)
1033{
1034 struct snd_soc_codec *codec = w->codec;
1035 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1036
1037 switch (event) {
1038 case SND_SOC_DAPM_POST_PMD:
1039 if (wm8994->aif1clk_enable) {
1040 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1041 WM8994_AIF1CLK_ENA_MASK, 0);
1042 wm8994->aif1clk_enable = 0;
1043 }
1044 if (wm8994->aif2clk_enable) {
1045 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1046 WM8994_AIF2CLK_ENA_MASK, 0);
1047 wm8994->aif2clk_enable = 0;
1048 }
1049 break;
1050 }
1051
1052 return 0;
1053}
1054
1055static int aif1clk_ev(struct snd_soc_dapm_widget *w,
1056 struct snd_kcontrol *kcontrol, int event)
1057{
1058 struct snd_soc_codec *codec = w->codec;
1059 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1060
1061 switch (event) {
1062 case SND_SOC_DAPM_PRE_PMU:
1063 wm8994->aif1clk_enable = 1;
1064 break;
1065 }
1066
1067 return 0;
1068}
1069
1070static int aif2clk_ev(struct snd_soc_dapm_widget *w,
1071 struct snd_kcontrol *kcontrol, int event)
1072{
1073 struct snd_soc_codec *codec = w->codec;
1074 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1075
1076 switch (event) {
1077 case SND_SOC_DAPM_PRE_PMU:
1078 wm8994->aif2clk_enable = 1;
1079 break;
1080 }
1081
1082 return 0;
1083}
1084
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001085static int dac_ev(struct snd_soc_dapm_widget *w,
1086 struct snd_kcontrol *kcontrol, int event)
1087{
1088 struct snd_soc_codec *codec = w->codec;
1089 unsigned int mask = 1 << w->shift;
1090
1091 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
1092 mask, mask);
1093 return 0;
1094}
1095
Mark Brown9e6e96a2010-01-29 17:47:12 +00001096static const char *hp_mux_text[] = {
1097 "Mixer",
1098 "DAC",
1099};
1100
1101#define WM8994_HP_ENUM(xname, xenum) \
1102{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1103 .info = snd_soc_info_enum_double, \
1104 .get = snd_soc_dapm_get_enum_double, \
1105 .put = wm8994_put_hp_enum, \
1106 .private_value = (unsigned long)&xenum }
1107
1108static int wm8994_put_hp_enum(struct snd_kcontrol *kcontrol,
1109 struct snd_ctl_elem_value *ucontrol)
1110{
1111 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
1112 struct snd_soc_codec *codec = w->codec;
1113 int ret;
1114
1115 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
1116
1117 wm8994_update_class_w(codec);
1118
1119 return ret;
1120}
1121
1122static const struct soc_enum hpl_enum =
1123 SOC_ENUM_SINGLE(WM8994_OUTPUT_MIXER_1, 8, 2, hp_mux_text);
1124
1125static const struct snd_kcontrol_new hpl_mux =
1126 WM8994_HP_ENUM("Left Headphone Mux", hpl_enum);
1127
1128static const struct soc_enum hpr_enum =
1129 SOC_ENUM_SINGLE(WM8994_OUTPUT_MIXER_2, 8, 2, hp_mux_text);
1130
1131static const struct snd_kcontrol_new hpr_mux =
1132 WM8994_HP_ENUM("Right Headphone Mux", hpr_enum);
1133
1134static const char *adc_mux_text[] = {
1135 "ADC",
1136 "DMIC",
1137};
1138
1139static const struct soc_enum adc_enum =
1140 SOC_ENUM_SINGLE(0, 0, 2, adc_mux_text);
1141
1142static const struct snd_kcontrol_new adcl_mux =
1143 SOC_DAPM_ENUM_VIRT("ADCL Mux", adc_enum);
1144
1145static const struct snd_kcontrol_new adcr_mux =
1146 SOC_DAPM_ENUM_VIRT("ADCR Mux", adc_enum);
1147
1148static const struct snd_kcontrol_new left_speaker_mixer[] = {
1149SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 9, 1, 0),
1150SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 7, 1, 0),
1151SOC_DAPM_SINGLE("IN1LP Switch", WM8994_SPEAKER_MIXER, 5, 1, 0),
1152SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 3, 1, 0),
1153SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 1, 1, 0),
1154};
1155
1156static const struct snd_kcontrol_new right_speaker_mixer[] = {
1157SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 8, 1, 0),
1158SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 6, 1, 0),
1159SOC_DAPM_SINGLE("IN1RP Switch", WM8994_SPEAKER_MIXER, 4, 1, 0),
1160SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 2, 1, 0),
1161SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 0, 1, 0),
1162};
1163
1164/* Debugging; dump chip status after DAPM transitions */
1165static int post_ev(struct snd_soc_dapm_widget *w,
1166 struct snd_kcontrol *kcontrol, int event)
1167{
1168 struct snd_soc_codec *codec = w->codec;
1169 dev_dbg(codec->dev, "SRC status: %x\n",
1170 snd_soc_read(codec,
1171 WM8994_RATE_STATUS));
1172 return 0;
1173}
1174
1175static const struct snd_kcontrol_new aif1adc1l_mix[] = {
1176SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1177 1, 1, 0),
1178SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1179 0, 1, 0),
1180};
1181
1182static const struct snd_kcontrol_new aif1adc1r_mix[] = {
1183SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1184 1, 1, 0),
1185SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1186 0, 1, 0),
1187};
1188
Mark Browna3257ba2010-07-19 14:02:34 +01001189static const struct snd_kcontrol_new aif1adc2l_mix[] = {
1190SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1191 1, 1, 0),
1192SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1193 0, 1, 0),
1194};
1195
1196static const struct snd_kcontrol_new aif1adc2r_mix[] = {
1197SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1198 1, 1, 0),
1199SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1200 0, 1, 0),
1201};
1202
Mark Brown9e6e96a2010-01-29 17:47:12 +00001203static const struct snd_kcontrol_new aif2dac2l_mix[] = {
1204SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1205 5, 1, 0),
1206SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1207 4, 1, 0),
1208SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1209 2, 1, 0),
1210SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1211 1, 1, 0),
1212SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1213 0, 1, 0),
1214};
1215
1216static const struct snd_kcontrol_new aif2dac2r_mix[] = {
1217SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1218 5, 1, 0),
1219SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1220 4, 1, 0),
1221SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1222 2, 1, 0),
1223SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1224 1, 1, 0),
1225SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1226 0, 1, 0),
1227};
1228
1229#define WM8994_CLASS_W_SWITCH(xname, reg, shift, max, invert) \
1230{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1231 .info = snd_soc_info_volsw, \
1232 .get = snd_soc_dapm_get_volsw, .put = wm8994_put_class_w, \
1233 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
1234
1235static int wm8994_put_class_w(struct snd_kcontrol *kcontrol,
1236 struct snd_ctl_elem_value *ucontrol)
1237{
1238 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
1239 struct snd_soc_codec *codec = w->codec;
1240 int ret;
1241
1242 ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
1243
1244 wm8994_update_class_w(codec);
1245
1246 return ret;
1247}
1248
1249static const struct snd_kcontrol_new dac1l_mix[] = {
1250WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1251 5, 1, 0),
1252WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1253 4, 1, 0),
1254WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1255 2, 1, 0),
1256WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1257 1, 1, 0),
1258WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1259 0, 1, 0),
1260};
1261
1262static const struct snd_kcontrol_new dac1r_mix[] = {
1263WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1264 5, 1, 0),
1265WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1266 4, 1, 0),
1267WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1268 2, 1, 0),
1269WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1270 1, 1, 0),
1271WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1272 0, 1, 0),
1273};
1274
1275static const char *sidetone_text[] = {
1276 "ADC/DMIC1", "DMIC2",
1277};
1278
1279static const struct soc_enum sidetone1_enum =
1280 SOC_ENUM_SINGLE(WM8994_SIDETONE, 0, 2, sidetone_text);
1281
1282static const struct snd_kcontrol_new sidetone1_mux =
1283 SOC_DAPM_ENUM("Left Sidetone Mux", sidetone1_enum);
1284
1285static const struct soc_enum sidetone2_enum =
1286 SOC_ENUM_SINGLE(WM8994_SIDETONE, 1, 2, sidetone_text);
1287
1288static const struct snd_kcontrol_new sidetone2_mux =
1289 SOC_DAPM_ENUM("Right Sidetone Mux", sidetone2_enum);
1290
1291static const char *aif1dac_text[] = {
1292 "AIF1DACDAT", "AIF3DACDAT",
1293};
1294
1295static const struct soc_enum aif1dac_enum =
1296 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 0, 2, aif1dac_text);
1297
1298static const struct snd_kcontrol_new aif1dac_mux =
1299 SOC_DAPM_ENUM("AIF1DAC Mux", aif1dac_enum);
1300
1301static const char *aif2dac_text[] = {
1302 "AIF2DACDAT", "AIF3DACDAT",
1303};
1304
1305static const struct soc_enum aif2dac_enum =
1306 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 1, 2, aif2dac_text);
1307
1308static const struct snd_kcontrol_new aif2dac_mux =
1309 SOC_DAPM_ENUM("AIF2DAC Mux", aif2dac_enum);
1310
1311static const char *aif2adc_text[] = {
1312 "AIF2ADCDAT", "AIF3DACDAT",
1313};
1314
1315static const struct soc_enum aif2adc_enum =
1316 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 2, 2, aif2adc_text);
1317
1318static const struct snd_kcontrol_new aif2adc_mux =
1319 SOC_DAPM_ENUM("AIF2ADC Mux", aif2adc_enum);
1320
1321static const char *aif3adc_text[] = {
Mark Brownc4431df2010-11-26 15:21:07 +00001322 "AIF1ADCDAT", "AIF2ADCDAT", "AIF2DACDAT", "Mono PCM",
Mark Brown9e6e96a2010-01-29 17:47:12 +00001323};
1324
Mark Brownc4431df2010-11-26 15:21:07 +00001325static const struct soc_enum wm8994_aif3adc_enum =
Mark Brown9e6e96a2010-01-29 17:47:12 +00001326 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 3, 3, aif3adc_text);
1327
Mark Brownc4431df2010-11-26 15:21:07 +00001328static const struct snd_kcontrol_new wm8994_aif3adc_mux =
1329 SOC_DAPM_ENUM("AIF3ADC Mux", wm8994_aif3adc_enum);
1330
1331static const struct soc_enum wm8958_aif3adc_enum =
1332 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 3, 4, aif3adc_text);
1333
1334static const struct snd_kcontrol_new wm8958_aif3adc_mux =
1335 SOC_DAPM_ENUM("AIF3ADC Mux", wm8958_aif3adc_enum);
1336
1337static const char *mono_pcm_out_text[] = {
1338 "None", "AIF2ADCL", "AIF2ADCR",
1339};
1340
1341static const struct soc_enum mono_pcm_out_enum =
1342 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 9, 3, mono_pcm_out_text);
1343
1344static const struct snd_kcontrol_new mono_pcm_out_mux =
1345 SOC_DAPM_ENUM("Mono PCM Out Mux", mono_pcm_out_enum);
1346
1347static const char *aif2dac_src_text[] = {
1348 "AIF2", "AIF3",
1349};
1350
1351/* Note that these two control shouldn't be simultaneously switched to AIF3 */
1352static const struct soc_enum aif2dacl_src_enum =
1353 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 7, 2, aif2dac_src_text);
1354
1355static const struct snd_kcontrol_new aif2dacl_src_mux =
1356 SOC_DAPM_ENUM("AIF2DACL Mux", aif2dacl_src_enum);
1357
1358static const struct soc_enum aif2dacr_src_enum =
1359 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 8, 2, aif2dac_src_text);
1360
1361static const struct snd_kcontrol_new aif2dacr_src_mux =
1362 SOC_DAPM_ENUM("AIF2DACR Mux", aif2dacr_src_enum);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001363
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001364static const struct snd_soc_dapm_widget wm8994_lateclk_revd_widgets[] = {
1365SND_SOC_DAPM_SUPPLY("AIF1CLK", SND_SOC_NOPM, 0, 0, aif1clk_ev,
1366 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1367SND_SOC_DAPM_SUPPLY("AIF2CLK", SND_SOC_NOPM, 0, 0, aif2clk_ev,
1368 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1369
1370SND_SOC_DAPM_PGA_E("Late DAC1L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1371 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1372SND_SOC_DAPM_PGA_E("Late DAC1R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1373 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1374SND_SOC_DAPM_PGA_E("Late DAC2L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1375 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1376SND_SOC_DAPM_PGA_E("Late DAC2R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
1377 late_enable_ev, SND_SOC_DAPM_PRE_PMU),
1378
1379SND_SOC_DAPM_POST("Late Disable PGA", late_disable_ev)
1380};
1381
1382static const struct snd_soc_dapm_widget wm8994_lateclk_widgets[] = {
1383SND_SOC_DAPM_SUPPLY("AIF1CLK", WM8994_AIF1_CLOCKING_1, 0, 0, NULL, 0),
1384SND_SOC_DAPM_SUPPLY("AIF2CLK", WM8994_AIF2_CLOCKING_1, 0, 0, NULL, 0)
1385};
1386
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00001387static const struct snd_soc_dapm_widget wm8994_dac_revd_widgets[] = {
1388SND_SOC_DAPM_DAC_E("DAC2L", NULL, SND_SOC_NOPM, 3, 0,
1389 dac_ev, SND_SOC_DAPM_PRE_PMU),
1390SND_SOC_DAPM_DAC_E("DAC2R", NULL, SND_SOC_NOPM, 2, 0,
1391 dac_ev, SND_SOC_DAPM_PRE_PMU),
1392SND_SOC_DAPM_DAC_E("DAC1L", NULL, SND_SOC_NOPM, 1, 0,
1393 dac_ev, SND_SOC_DAPM_PRE_PMU),
1394SND_SOC_DAPM_DAC_E("DAC1R", NULL, SND_SOC_NOPM, 0, 0,
1395 dac_ev, SND_SOC_DAPM_PRE_PMU),
1396};
1397
1398static const struct snd_soc_dapm_widget wm8994_dac_widgets[] = {
1399SND_SOC_DAPM_DAC("DAC2L", NULL, WM8994_POWER_MANAGEMENT_5, 3, 0),
1400SND_SOC_DAPM_DAC("DAC1R", NULL, WM8994_POWER_MANAGEMENT_5, 2, 0),
1401SND_SOC_DAPM_DAC("DAC1L", NULL, WM8994_POWER_MANAGEMENT_5, 1, 0),
1402SND_SOC_DAPM_DAC("DAC1R", NULL, WM8994_POWER_MANAGEMENT_5, 0, 0),
1403};
1404
Mark Brown9e6e96a2010-01-29 17:47:12 +00001405static const struct snd_soc_dapm_widget wm8994_dapm_widgets[] = {
1406SND_SOC_DAPM_INPUT("DMIC1DAT"),
1407SND_SOC_DAPM_INPUT("DMIC2DAT"),
Mark Brown66b47fd2010-07-08 11:25:43 +09001408SND_SOC_DAPM_INPUT("Clock"),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001409
1410SND_SOC_DAPM_SUPPLY("CLK_SYS", SND_SOC_NOPM, 0, 0, clk_sys_event,
1411 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1412
1413SND_SOC_DAPM_SUPPLY("DSP1CLK", WM8994_CLOCKING_1, 3, 0, NULL, 0),
1414SND_SOC_DAPM_SUPPLY("DSP2CLK", WM8994_CLOCKING_1, 2, 0, NULL, 0),
1415SND_SOC_DAPM_SUPPLY("DSPINTCLK", WM8994_CLOCKING_1, 1, 0, NULL, 0),
1416
Mark Brown7f94de42011-02-03 16:27:34 +00001417SND_SOC_DAPM_AIF_OUT("AIF1ADC1L", NULL,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001418 0, WM8994_POWER_MANAGEMENT_4, 9, 0),
Mark Brown7f94de42011-02-03 16:27:34 +00001419SND_SOC_DAPM_AIF_OUT("AIF1ADC1R", NULL,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001420 0, WM8994_POWER_MANAGEMENT_4, 8, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001421SND_SOC_DAPM_AIF_IN_E("AIF1DAC1L", NULL, 0,
1422 WM8994_POWER_MANAGEMENT_5, 9, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001423 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brownd6addcc2010-11-26 15:21:08 +00001424SND_SOC_DAPM_AIF_IN_E("AIF1DAC1R", NULL, 0,
1425 WM8994_POWER_MANAGEMENT_5, 8, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001426 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001427
Mark Brown7f94de42011-02-03 16:27:34 +00001428SND_SOC_DAPM_AIF_OUT("AIF1ADC2L", NULL,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001429 0, WM8994_POWER_MANAGEMENT_4, 11, 0),
Mark Brown7f94de42011-02-03 16:27:34 +00001430SND_SOC_DAPM_AIF_OUT("AIF1ADC2R", NULL,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001431 0, WM8994_POWER_MANAGEMENT_4, 10, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001432SND_SOC_DAPM_AIF_IN_E("AIF1DAC2L", NULL, 0,
1433 WM8994_POWER_MANAGEMENT_5, 11, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001434 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brownd6addcc2010-11-26 15:21:08 +00001435SND_SOC_DAPM_AIF_IN_E("AIF1DAC2R", NULL, 0,
1436 WM8994_POWER_MANAGEMENT_5, 10, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001437 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001438
1439SND_SOC_DAPM_MIXER("AIF1ADC1L Mixer", SND_SOC_NOPM, 0, 0,
1440 aif1adc1l_mix, ARRAY_SIZE(aif1adc1l_mix)),
1441SND_SOC_DAPM_MIXER("AIF1ADC1R Mixer", SND_SOC_NOPM, 0, 0,
1442 aif1adc1r_mix, ARRAY_SIZE(aif1adc1r_mix)),
1443
Mark Browna3257ba2010-07-19 14:02:34 +01001444SND_SOC_DAPM_MIXER("AIF1ADC2L Mixer", SND_SOC_NOPM, 0, 0,
1445 aif1adc2l_mix, ARRAY_SIZE(aif1adc2l_mix)),
1446SND_SOC_DAPM_MIXER("AIF1ADC2R Mixer", SND_SOC_NOPM, 0, 0,
1447 aif1adc2r_mix, ARRAY_SIZE(aif1adc2r_mix)),
1448
Mark Brown9e6e96a2010-01-29 17:47:12 +00001449SND_SOC_DAPM_MIXER("AIF2DAC2L Mixer", SND_SOC_NOPM, 0, 0,
1450 aif2dac2l_mix, ARRAY_SIZE(aif2dac2l_mix)),
1451SND_SOC_DAPM_MIXER("AIF2DAC2R Mixer", SND_SOC_NOPM, 0, 0,
1452 aif2dac2r_mix, ARRAY_SIZE(aif2dac2r_mix)),
1453
1454SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &sidetone1_mux),
1455SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &sidetone2_mux),
1456
1457SND_SOC_DAPM_MIXER("DAC1L Mixer", SND_SOC_NOPM, 0, 0,
1458 dac1l_mix, ARRAY_SIZE(dac1l_mix)),
1459SND_SOC_DAPM_MIXER("DAC1R Mixer", SND_SOC_NOPM, 0, 0,
1460 dac1r_mix, ARRAY_SIZE(dac1r_mix)),
1461
1462SND_SOC_DAPM_AIF_OUT("AIF2ADCL", NULL, 0,
1463 WM8994_POWER_MANAGEMENT_4, 13, 0),
1464SND_SOC_DAPM_AIF_OUT("AIF2ADCR", NULL, 0,
1465 WM8994_POWER_MANAGEMENT_4, 12, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001466SND_SOC_DAPM_AIF_IN_E("AIF2DACL", NULL, 0,
1467 WM8994_POWER_MANAGEMENT_5, 13, 0, wm8958_aif_ev,
1468 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1469SND_SOC_DAPM_AIF_IN_E("AIF2DACR", NULL, 0,
1470 WM8994_POWER_MANAGEMENT_5, 12, 0, wm8958_aif_ev,
1471 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001472
1473SND_SOC_DAPM_AIF_IN("AIF1DACDAT", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
1474SND_SOC_DAPM_AIF_IN("AIF2DACDAT", "AIF2 Playback", 0, SND_SOC_NOPM, 0, 0),
Mark Brown7f94de42011-02-03 16:27:34 +00001475SND_SOC_DAPM_AIF_OUT("AIF1ADCDAT", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001476SND_SOC_DAPM_AIF_OUT("AIF2ADCDAT", "AIF2 Capture", 0, SND_SOC_NOPM, 0, 0),
1477
1478SND_SOC_DAPM_MUX("AIF1DAC Mux", SND_SOC_NOPM, 0, 0, &aif1dac_mux),
1479SND_SOC_DAPM_MUX("AIF2DAC Mux", SND_SOC_NOPM, 0, 0, &aif2dac_mux),
1480SND_SOC_DAPM_MUX("AIF2ADC Mux", SND_SOC_NOPM, 0, 0, &aif2adc_mux),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001481
1482SND_SOC_DAPM_AIF_IN("AIF3DACDAT", "AIF3 Playback", 0, SND_SOC_NOPM, 0, 0),
1483SND_SOC_DAPM_AIF_IN("AIF3ADCDAT", "AIF3 Capture", 0, SND_SOC_NOPM, 0, 0),
1484
1485SND_SOC_DAPM_SUPPLY("TOCLK", WM8994_CLOCKING_1, 4, 0, NULL, 0),
1486
1487SND_SOC_DAPM_ADC("DMIC2L", NULL, WM8994_POWER_MANAGEMENT_4, 5, 0),
1488SND_SOC_DAPM_ADC("DMIC2R", NULL, WM8994_POWER_MANAGEMENT_4, 4, 0),
1489SND_SOC_DAPM_ADC("DMIC1L", NULL, WM8994_POWER_MANAGEMENT_4, 3, 0),
1490SND_SOC_DAPM_ADC("DMIC1R", NULL, WM8994_POWER_MANAGEMENT_4, 2, 0),
1491
1492/* Power is done with the muxes since the ADC power also controls the
1493 * downsampling chain, the chip will automatically manage the analogue
1494 * specific portions.
1495 */
1496SND_SOC_DAPM_ADC("ADCL", NULL, SND_SOC_NOPM, 1, 0),
1497SND_SOC_DAPM_ADC("ADCR", NULL, SND_SOC_NOPM, 0, 0),
1498
1499SND_SOC_DAPM_MUX("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux),
1500SND_SOC_DAPM_MUX("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux),
1501
Mark Brown9e6e96a2010-01-29 17:47:12 +00001502SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
1503SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
1504
1505SND_SOC_DAPM_MIXER("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
1506 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
1507SND_SOC_DAPM_MIXER("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
1508 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
1509
1510SND_SOC_DAPM_POST("Debug log", post_ev),
1511};
1512
Mark Brownc4431df2010-11-26 15:21:07 +00001513static const struct snd_soc_dapm_widget wm8994_specific_dapm_widgets[] = {
1514SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8994_aif3adc_mux),
1515};
Mark Brown9e6e96a2010-01-29 17:47:12 +00001516
Mark Brownc4431df2010-11-26 15:21:07 +00001517static const struct snd_soc_dapm_widget wm8958_dapm_widgets[] = {
1518SND_SOC_DAPM_MUX("Mono PCM Out Mux", SND_SOC_NOPM, 0, 0, &mono_pcm_out_mux),
1519SND_SOC_DAPM_MUX("AIF2DACL Mux", SND_SOC_NOPM, 0, 0, &aif2dacl_src_mux),
1520SND_SOC_DAPM_MUX("AIF2DACR Mux", SND_SOC_NOPM, 0, 0, &aif2dacr_src_mux),
1521SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8958_aif3adc_mux),
1522};
1523
1524static const struct snd_soc_dapm_route intercon[] = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00001525 { "CLK_SYS", NULL, "AIF1CLK", check_clk_sys },
1526 { "CLK_SYS", NULL, "AIF2CLK", check_clk_sys },
1527
1528 { "DSP1CLK", NULL, "CLK_SYS" },
1529 { "DSP2CLK", NULL, "CLK_SYS" },
1530 { "DSPINTCLK", NULL, "CLK_SYS" },
1531
1532 { "AIF1ADC1L", NULL, "AIF1CLK" },
1533 { "AIF1ADC1L", NULL, "DSP1CLK" },
1534 { "AIF1ADC1R", NULL, "AIF1CLK" },
1535 { "AIF1ADC1R", NULL, "DSP1CLK" },
1536 { "AIF1ADC1R", NULL, "DSPINTCLK" },
1537
1538 { "AIF1DAC1L", NULL, "AIF1CLK" },
1539 { "AIF1DAC1L", NULL, "DSP1CLK" },
1540 { "AIF1DAC1R", NULL, "AIF1CLK" },
1541 { "AIF1DAC1R", NULL, "DSP1CLK" },
1542 { "AIF1DAC1R", NULL, "DSPINTCLK" },
1543
1544 { "AIF1ADC2L", NULL, "AIF1CLK" },
1545 { "AIF1ADC2L", NULL, "DSP1CLK" },
1546 { "AIF1ADC2R", NULL, "AIF1CLK" },
1547 { "AIF1ADC2R", NULL, "DSP1CLK" },
1548 { "AIF1ADC2R", NULL, "DSPINTCLK" },
1549
1550 { "AIF1DAC2L", NULL, "AIF1CLK" },
1551 { "AIF1DAC2L", NULL, "DSP1CLK" },
1552 { "AIF1DAC2R", NULL, "AIF1CLK" },
1553 { "AIF1DAC2R", NULL, "DSP1CLK" },
1554 { "AIF1DAC2R", NULL, "DSPINTCLK" },
1555
1556 { "AIF2ADCL", NULL, "AIF2CLK" },
1557 { "AIF2ADCL", NULL, "DSP2CLK" },
1558 { "AIF2ADCR", NULL, "AIF2CLK" },
1559 { "AIF2ADCR", NULL, "DSP2CLK" },
1560 { "AIF2ADCR", NULL, "DSPINTCLK" },
1561
1562 { "AIF2DACL", NULL, "AIF2CLK" },
1563 { "AIF2DACL", NULL, "DSP2CLK" },
1564 { "AIF2DACR", NULL, "AIF2CLK" },
1565 { "AIF2DACR", NULL, "DSP2CLK" },
1566 { "AIF2DACR", NULL, "DSPINTCLK" },
1567
1568 { "DMIC1L", NULL, "DMIC1DAT" },
1569 { "DMIC1L", NULL, "CLK_SYS" },
1570 { "DMIC1R", NULL, "DMIC1DAT" },
1571 { "DMIC1R", NULL, "CLK_SYS" },
1572 { "DMIC2L", NULL, "DMIC2DAT" },
1573 { "DMIC2L", NULL, "CLK_SYS" },
1574 { "DMIC2R", NULL, "DMIC2DAT" },
1575 { "DMIC2R", NULL, "CLK_SYS" },
1576
1577 { "ADCL", NULL, "AIF1CLK" },
1578 { "ADCL", NULL, "DSP1CLK" },
1579 { "ADCL", NULL, "DSPINTCLK" },
1580
1581 { "ADCR", NULL, "AIF1CLK" },
1582 { "ADCR", NULL, "DSP1CLK" },
1583 { "ADCR", NULL, "DSPINTCLK" },
1584
1585 { "ADCL Mux", "ADC", "ADCL" },
1586 { "ADCL Mux", "DMIC", "DMIC1L" },
1587 { "ADCR Mux", "ADC", "ADCR" },
1588 { "ADCR Mux", "DMIC", "DMIC1R" },
1589
1590 { "DAC1L", NULL, "AIF1CLK" },
1591 { "DAC1L", NULL, "DSP1CLK" },
1592 { "DAC1L", NULL, "DSPINTCLK" },
1593
1594 { "DAC1R", NULL, "AIF1CLK" },
1595 { "DAC1R", NULL, "DSP1CLK" },
1596 { "DAC1R", NULL, "DSPINTCLK" },
1597
1598 { "DAC2L", NULL, "AIF2CLK" },
1599 { "DAC2L", NULL, "DSP2CLK" },
1600 { "DAC2L", NULL, "DSPINTCLK" },
1601
1602 { "DAC2R", NULL, "AIF2DACR" },
1603 { "DAC2R", NULL, "AIF2CLK" },
1604 { "DAC2R", NULL, "DSP2CLK" },
1605 { "DAC2R", NULL, "DSPINTCLK" },
1606
1607 { "TOCLK", NULL, "CLK_SYS" },
1608
1609 /* AIF1 outputs */
1610 { "AIF1ADC1L", NULL, "AIF1ADC1L Mixer" },
1611 { "AIF1ADC1L Mixer", "ADC/DMIC Switch", "ADCL Mux" },
1612 { "AIF1ADC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1613
1614 { "AIF1ADC1R", NULL, "AIF1ADC1R Mixer" },
1615 { "AIF1ADC1R Mixer", "ADC/DMIC Switch", "ADCR Mux" },
1616 { "AIF1ADC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1617
Mark Browna3257ba2010-07-19 14:02:34 +01001618 { "AIF1ADC2L", NULL, "AIF1ADC2L Mixer" },
1619 { "AIF1ADC2L Mixer", "DMIC Switch", "DMIC2L" },
1620 { "AIF1ADC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1621
1622 { "AIF1ADC2R", NULL, "AIF1ADC2R Mixer" },
1623 { "AIF1ADC2R Mixer", "DMIC Switch", "DMIC2R" },
1624 { "AIF1ADC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1625
Mark Brown9e6e96a2010-01-29 17:47:12 +00001626 /* Pin level routing for AIF3 */
1627 { "AIF1DAC1L", NULL, "AIF1DAC Mux" },
1628 { "AIF1DAC1R", NULL, "AIF1DAC Mux" },
1629 { "AIF1DAC2L", NULL, "AIF1DAC Mux" },
1630 { "AIF1DAC2R", NULL, "AIF1DAC Mux" },
1631
Mark Brown9e6e96a2010-01-29 17:47:12 +00001632 { "AIF1DAC Mux", "AIF1DACDAT", "AIF1DACDAT" },
1633 { "AIF1DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1634 { "AIF2DAC Mux", "AIF2DACDAT", "AIF2DACDAT" },
1635 { "AIF2DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1636 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCL" },
1637 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCR" },
1638 { "AIF2ADC Mux", "AIF3DACDAT", "AIF3ADCDAT" },
1639
1640 /* DAC1 inputs */
Mark Brown9e6e96a2010-01-29 17:47:12 +00001641 { "DAC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1642 { "DAC1L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1643 { "DAC1L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1644 { "DAC1L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1645 { "DAC1L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1646
Mark Brown9e6e96a2010-01-29 17:47:12 +00001647 { "DAC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1648 { "DAC1R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1649 { "DAC1R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1650 { "DAC1R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1651 { "DAC1R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1652
1653 /* DAC2/AIF2 outputs */
1654 { "AIF2ADCL", NULL, "AIF2DAC2L Mixer" },
Mark Brown9e6e96a2010-01-29 17:47:12 +00001655 { "AIF2DAC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1656 { "AIF2DAC2L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1657 { "AIF2DAC2L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1658 { "AIF2DAC2L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1659 { "AIF2DAC2L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1660
1661 { "AIF2ADCR", NULL, "AIF2DAC2R Mixer" },
Mark Brown9e6e96a2010-01-29 17:47:12 +00001662 { "AIF2DAC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1663 { "AIF2DAC2R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1664 { "AIF2DAC2R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1665 { "AIF2DAC2R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1666 { "AIF2DAC2R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1667
Mark Brown7f94de42011-02-03 16:27:34 +00001668 { "AIF1ADCDAT", NULL, "AIF1ADC1L" },
1669 { "AIF1ADCDAT", NULL, "AIF1ADC1R" },
1670 { "AIF1ADCDAT", NULL, "AIF1ADC2L" },
1671 { "AIF1ADCDAT", NULL, "AIF1ADC2R" },
1672
Mark Brown9e6e96a2010-01-29 17:47:12 +00001673 { "AIF2ADCDAT", NULL, "AIF2ADC Mux" },
1674
1675 /* AIF3 output */
1676 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1L" },
1677 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1R" },
1678 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2L" },
1679 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2R" },
1680 { "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCL" },
1681 { "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCR" },
1682 { "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACL" },
1683 { "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACR" },
1684
1685 /* Sidetone */
1686 { "Left Sidetone", "ADC/DMIC1", "ADCL Mux" },
1687 { "Left Sidetone", "DMIC2", "DMIC2L" },
1688 { "Right Sidetone", "ADC/DMIC1", "ADCR Mux" },
1689 { "Right Sidetone", "DMIC2", "DMIC2R" },
1690
1691 /* Output stages */
1692 { "Left Output Mixer", "DAC Switch", "DAC1L" },
1693 { "Right Output Mixer", "DAC Switch", "DAC1R" },
1694
1695 { "SPKL", "DAC1 Switch", "DAC1L" },
1696 { "SPKL", "DAC2 Switch", "DAC2L" },
1697
1698 { "SPKR", "DAC1 Switch", "DAC1R" },
1699 { "SPKR", "DAC2 Switch", "DAC2R" },
1700
1701 { "Left Headphone Mux", "DAC", "DAC1L" },
1702 { "Right Headphone Mux", "DAC", "DAC1R" },
1703};
1704
Dimitris Papastamos173efa02011-02-11 16:32:11 +00001705static const struct snd_soc_dapm_route wm8994_lateclk_revd_intercon[] = {
1706 { "DAC1L", NULL, "Late DAC1L Enable PGA" },
1707 { "Late DAC1L Enable PGA", NULL, "DAC1L Mixer" },
1708 { "DAC1R", NULL, "Late DAC1R Enable PGA" },
1709 { "Late DAC1R Enable PGA", NULL, "DAC1R Mixer" },
1710 { "DAC2L", NULL, "Late DAC2L Enable PGA" },
1711 { "Late DAC2L Enable PGA", NULL, "AIF2DAC2L Mixer" },
1712 { "DAC2R", NULL, "Late DAC2R Enable PGA" },
1713 { "Late DAC2R Enable PGA", NULL, "AIF2DAC2R Mixer" }
1714};
1715
1716static const struct snd_soc_dapm_route wm8994_lateclk_intercon[] = {
1717 { "DAC1L", NULL, "DAC1L Mixer" },
1718 { "DAC1R", NULL, "DAC1R Mixer" },
1719 { "DAC2L", NULL, "AIF2DAC2L Mixer" },
1720 { "DAC2R", NULL, "AIF2DAC2R Mixer" },
1721};
1722
Mark Brown6ed8f142011-02-03 16:27:35 +00001723static const struct snd_soc_dapm_route wm8994_revd_intercon[] = {
1724 { "AIF1DACDAT", NULL, "AIF2DACDAT" },
1725 { "AIF2DACDAT", NULL, "AIF1DACDAT" },
1726 { "AIF1ADCDAT", NULL, "AIF2ADCDAT" },
1727 { "AIF2ADCDAT", NULL, "AIF1ADCDAT" },
1728};
1729
Mark Brownc4431df2010-11-26 15:21:07 +00001730static const struct snd_soc_dapm_route wm8994_intercon[] = {
1731 { "AIF2DACL", NULL, "AIF2DAC Mux" },
1732 { "AIF2DACR", NULL, "AIF2DAC Mux" },
1733};
1734
1735static const struct snd_soc_dapm_route wm8958_intercon[] = {
1736 { "AIF2DACL", NULL, "AIF2DACL Mux" },
1737 { "AIF2DACR", NULL, "AIF2DACR Mux" },
1738
1739 { "AIF2DACL Mux", "AIF2", "AIF2DAC Mux" },
1740 { "AIF2DACL Mux", "AIF3", "AIF3DACDAT" },
1741 { "AIF2DACR Mux", "AIF2", "AIF2DAC Mux" },
1742 { "AIF2DACR Mux", "AIF3", "AIF3DACDAT" },
1743
1744 { "Mono PCM Out Mux", "AIF2ADCL", "AIF2ADCL" },
1745 { "Mono PCM Out Mux", "AIF2ADCR", "AIF2ADCR" },
1746
1747 { "AIF3ADC Mux", "Mono PCM", "Mono PCM Out Mux" },
1748};
1749
Mark Brown9e6e96a2010-01-29 17:47:12 +00001750/* The size in bits of the FLL divide multiplied by 10
1751 * to allow rounding later */
1752#define FIXED_FLL_SIZE ((1 << 16) * 10)
1753
1754struct fll_div {
1755 u16 outdiv;
1756 u16 n;
1757 u16 k;
1758 u16 clk_ref_div;
1759 u16 fll_fratio;
1760};
1761
1762static int wm8994_get_fll_config(struct fll_div *fll,
1763 int freq_in, int freq_out)
1764{
1765 u64 Kpart;
1766 unsigned int K, Ndiv, Nmod;
1767
1768 pr_debug("FLL input=%dHz, output=%dHz\n", freq_in, freq_out);
1769
1770 /* Scale the input frequency down to <= 13.5MHz */
1771 fll->clk_ref_div = 0;
1772 while (freq_in > 13500000) {
1773 fll->clk_ref_div++;
1774 freq_in /= 2;
1775
1776 if (fll->clk_ref_div > 3)
1777 return -EINVAL;
1778 }
1779 pr_debug("CLK_REF_DIV=%d, Fref=%dHz\n", fll->clk_ref_div, freq_in);
1780
1781 /* Scale the output to give 90MHz<=Fvco<=100MHz */
1782 fll->outdiv = 3;
1783 while (freq_out * (fll->outdiv + 1) < 90000000) {
1784 fll->outdiv++;
1785 if (fll->outdiv > 63)
1786 return -EINVAL;
1787 }
1788 freq_out *= fll->outdiv + 1;
1789 pr_debug("OUTDIV=%d, Fvco=%dHz\n", fll->outdiv, freq_out);
1790
1791 if (freq_in > 1000000) {
1792 fll->fll_fratio = 0;
Mark Brown7d48a6a2010-04-20 13:36:11 +09001793 } else if (freq_in > 256000) {
1794 fll->fll_fratio = 1;
1795 freq_in *= 2;
1796 } else if (freq_in > 128000) {
1797 fll->fll_fratio = 2;
1798 freq_in *= 4;
1799 } else if (freq_in > 64000) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00001800 fll->fll_fratio = 3;
1801 freq_in *= 8;
Mark Brown7d48a6a2010-04-20 13:36:11 +09001802 } else {
1803 fll->fll_fratio = 4;
1804 freq_in *= 16;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001805 }
1806 pr_debug("FLL_FRATIO=%d, Fref=%dHz\n", fll->fll_fratio, freq_in);
1807
1808 /* Now, calculate N.K */
1809 Ndiv = freq_out / freq_in;
1810
1811 fll->n = Ndiv;
1812 Nmod = freq_out % freq_in;
1813 pr_debug("Nmod=%d\n", Nmod);
1814
1815 /* Calculate fractional part - scale up so we can round. */
1816 Kpart = FIXED_FLL_SIZE * (long long)Nmod;
1817
1818 do_div(Kpart, freq_in);
1819
1820 K = Kpart & 0xFFFFFFFF;
1821
1822 if ((K % 10) >= 5)
1823 K += 5;
1824
1825 /* Move down to proper range now rounding is done */
1826 fll->k = K / 10;
1827
1828 pr_debug("N=%x K=%x\n", fll->n, fll->k);
1829
1830 return 0;
1831}
1832
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001833static int _wm8994_set_fll(struct snd_soc_codec *codec, int id, int src,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001834 unsigned int freq_in, unsigned int freq_out)
1835{
Mark Brownb2c812e2010-04-14 15:35:19 +09001836 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001837 int reg_offset, ret;
1838 struct fll_div fll;
1839 u16 reg, aif1, aif2;
1840
1841 aif1 = snd_soc_read(codec, WM8994_AIF1_CLOCKING_1)
1842 & WM8994_AIF1CLK_ENA;
1843
1844 aif2 = snd_soc_read(codec, WM8994_AIF2_CLOCKING_1)
1845 & WM8994_AIF2CLK_ENA;
1846
1847 switch (id) {
1848 case WM8994_FLL1:
1849 reg_offset = 0;
1850 id = 0;
1851 break;
1852 case WM8994_FLL2:
1853 reg_offset = 0x20;
1854 id = 1;
1855 break;
1856 default:
1857 return -EINVAL;
1858 }
1859
Mark Brown136ff2a2010-04-20 12:56:18 +09001860 switch (src) {
Mark Brown7add84a2010-04-22 02:29:01 +09001861 case 0:
1862 /* Allow no source specification when stopping */
1863 if (freq_out)
1864 return -EINVAL;
Mark Brown4514e892010-12-03 16:02:10 +00001865 src = wm8994->fll[id].src;
Mark Brown7add84a2010-04-22 02:29:01 +09001866 break;
Mark Brown136ff2a2010-04-20 12:56:18 +09001867 case WM8994_FLL_SRC_MCLK1:
1868 case WM8994_FLL_SRC_MCLK2:
1869 case WM8994_FLL_SRC_LRCLK:
1870 case WM8994_FLL_SRC_BCLK:
1871 break;
1872 default:
1873 return -EINVAL;
1874 }
1875
Mark Brown9e6e96a2010-01-29 17:47:12 +00001876 /* Are we changing anything? */
1877 if (wm8994->fll[id].src == src &&
1878 wm8994->fll[id].in == freq_in && wm8994->fll[id].out == freq_out)
1879 return 0;
1880
1881 /* If we're stopping the FLL redo the old config - no
1882 * registers will actually be written but we avoid GCC flow
1883 * analysis bugs spewing warnings.
1884 */
1885 if (freq_out)
1886 ret = wm8994_get_fll_config(&fll, freq_in, freq_out);
1887 else
1888 ret = wm8994_get_fll_config(&fll, wm8994->fll[id].in,
1889 wm8994->fll[id].out);
1890 if (ret < 0)
1891 return ret;
1892
1893 /* Gate the AIF clocks while we reclock */
1894 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1895 WM8994_AIF1CLK_ENA, 0);
1896 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1897 WM8994_AIF2CLK_ENA, 0);
1898
1899 /* We always need to disable the FLL while reconfiguring */
1900 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
1901 WM8994_FLL1_ENA, 0);
1902
1903 reg = (fll.outdiv << WM8994_FLL1_OUTDIV_SHIFT) |
1904 (fll.fll_fratio << WM8994_FLL1_FRATIO_SHIFT);
1905 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_2 + reg_offset,
1906 WM8994_FLL1_OUTDIV_MASK |
1907 WM8994_FLL1_FRATIO_MASK, reg);
1908
1909 snd_soc_write(codec, WM8994_FLL1_CONTROL_3 + reg_offset, fll.k);
1910
1911 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_4 + reg_offset,
1912 WM8994_FLL1_N_MASK,
1913 fll.n << WM8994_FLL1_N_SHIFT);
1914
1915 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_5 + reg_offset,
Mark Brown136ff2a2010-04-20 12:56:18 +09001916 WM8994_FLL1_REFCLK_DIV_MASK |
1917 WM8994_FLL1_REFCLK_SRC_MASK,
1918 (fll.clk_ref_div << WM8994_FLL1_REFCLK_DIV_SHIFT) |
1919 (src - 1));
Mark Brown9e6e96a2010-01-29 17:47:12 +00001920
1921 /* Enable (with fractional mode if required) */
1922 if (freq_out) {
1923 if (fll.k)
1924 reg = WM8994_FLL1_ENA | WM8994_FLL1_FRAC;
1925 else
1926 reg = WM8994_FLL1_ENA;
1927 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
1928 WM8994_FLL1_ENA | WM8994_FLL1_FRAC,
1929 reg);
1930 }
1931
1932 wm8994->fll[id].in = freq_in;
1933 wm8994->fll[id].out = freq_out;
Mark Brown136ff2a2010-04-20 12:56:18 +09001934 wm8994->fll[id].src = src;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001935
1936 /* Enable any gated AIF clocks */
1937 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1938 WM8994_AIF1CLK_ENA, aif1);
1939 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1940 WM8994_AIF2CLK_ENA, aif2);
1941
1942 configure_clock(codec);
1943
1944 return 0;
1945}
1946
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001947
Mark Brown66b47fd2010-07-08 11:25:43 +09001948static int opclk_divs[] = { 10, 20, 30, 40, 55, 60, 80, 120, 160 };
1949
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001950static int wm8994_set_fll(struct snd_soc_dai *dai, int id, int src,
1951 unsigned int freq_in, unsigned int freq_out)
1952{
1953 return _wm8994_set_fll(dai->codec, id, src, freq_in, freq_out);
1954}
1955
Mark Brown9e6e96a2010-01-29 17:47:12 +00001956static int wm8994_set_dai_sysclk(struct snd_soc_dai *dai,
1957 int clk_id, unsigned int freq, int dir)
1958{
1959 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001960 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown66b47fd2010-07-08 11:25:43 +09001961 int i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001962
1963 switch (dai->id) {
1964 case 1:
1965 case 2:
1966 break;
1967
1968 default:
1969 /* AIF3 shares clocking with AIF1/2 */
1970 return -EINVAL;
1971 }
1972
1973 switch (clk_id) {
1974 case WM8994_SYSCLK_MCLK1:
1975 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK1;
1976 wm8994->mclk[0] = freq;
1977 dev_dbg(dai->dev, "AIF%d using MCLK1 at %uHz\n",
1978 dai->id, freq);
1979 break;
1980
1981 case WM8994_SYSCLK_MCLK2:
1982 /* TODO: Set GPIO AF */
1983 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK2;
1984 wm8994->mclk[1] = freq;
1985 dev_dbg(dai->dev, "AIF%d using MCLK2 at %uHz\n",
1986 dai->id, freq);
1987 break;
1988
1989 case WM8994_SYSCLK_FLL1:
1990 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL1;
1991 dev_dbg(dai->dev, "AIF%d using FLL1\n", dai->id);
1992 break;
1993
1994 case WM8994_SYSCLK_FLL2:
1995 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL2;
1996 dev_dbg(dai->dev, "AIF%d using FLL2\n", dai->id);
1997 break;
1998
Mark Brown66b47fd2010-07-08 11:25:43 +09001999 case WM8994_SYSCLK_OPCLK:
2000 /* Special case - a division (times 10) is given and
2001 * no effect on main clocking.
2002 */
2003 if (freq) {
2004 for (i = 0; i < ARRAY_SIZE(opclk_divs); i++)
2005 if (opclk_divs[i] == freq)
2006 break;
2007 if (i == ARRAY_SIZE(opclk_divs))
2008 return -EINVAL;
2009 snd_soc_update_bits(codec, WM8994_CLOCKING_2,
2010 WM8994_OPCLK_DIV_MASK, i);
2011 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
2012 WM8994_OPCLK_ENA, WM8994_OPCLK_ENA);
2013 } else {
2014 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
2015 WM8994_OPCLK_ENA, 0);
2016 }
2017
Mark Brown9e6e96a2010-01-29 17:47:12 +00002018 default:
2019 return -EINVAL;
2020 }
2021
2022 configure_clock(codec);
2023
2024 return 0;
2025}
2026
2027static int wm8994_set_bias_level(struct snd_soc_codec *codec,
2028 enum snd_soc_bias_level level)
2029{
Mark Brown3a423152010-11-26 15:21:06 +00002030 struct wm8994 *control = codec->control_data;
Mark Brownb6b05692010-08-13 12:58:20 +01002031 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2032
Mark Brown9e6e96a2010-01-29 17:47:12 +00002033 switch (level) {
2034 case SND_SOC_BIAS_ON:
2035 break;
2036
2037 case SND_SOC_BIAS_PREPARE:
2038 /* VMID=2x40k */
2039 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
2040 WM8994_VMID_SEL_MASK, 0x2);
2041 break;
2042
2043 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002044 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
Mark Brown39fb51a2010-11-26 17:23:43 +00002045 pm_runtime_get_sync(codec->dev);
2046
Mark Brown8bc3c2c2010-11-30 14:56:18 +00002047 switch (control->type) {
2048 case WM8994:
2049 if (wm8994->revision < 4) {
2050 /* Tweak DC servo and DSP
2051 * configuration for improved
2052 * performance. */
2053 snd_soc_write(codec, 0x102, 0x3);
2054 snd_soc_write(codec, 0x56, 0x3);
2055 snd_soc_write(codec, 0x817, 0);
2056 snd_soc_write(codec, 0x102, 0);
2057 }
2058 break;
2059
2060 case WM8958:
2061 if (wm8994->revision == 0) {
2062 /* Optimise performance for rev A */
2063 snd_soc_write(codec, 0x102, 0x3);
2064 snd_soc_write(codec, 0xcb, 0x81);
2065 snd_soc_write(codec, 0x817, 0);
2066 snd_soc_write(codec, 0x102, 0);
2067
2068 snd_soc_update_bits(codec,
2069 WM8958_CHARGE_PUMP_2,
2070 WM8958_CP_DISCH,
2071 WM8958_CP_DISCH);
2072 }
2073 break;
Mark Brownb6b05692010-08-13 12:58:20 +01002074 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002075
2076 /* Discharge LINEOUT1 & 2 */
2077 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
2078 WM8994_LINEOUT1_DISCH |
2079 WM8994_LINEOUT2_DISCH,
2080 WM8994_LINEOUT1_DISCH |
2081 WM8994_LINEOUT2_DISCH);
2082
2083 /* Startup bias, VMID ramp & buffer */
2084 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
2085 WM8994_STARTUP_BIAS_ENA |
2086 WM8994_VMID_BUF_ENA |
2087 WM8994_VMID_RAMP_MASK,
2088 WM8994_STARTUP_BIAS_ENA |
2089 WM8994_VMID_BUF_ENA |
2090 (0x11 << WM8994_VMID_RAMP_SHIFT));
2091
2092 /* Main bias enable, VMID=2x40k */
2093 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
2094 WM8994_BIAS_ENA |
2095 WM8994_VMID_SEL_MASK,
2096 WM8994_BIAS_ENA | 0x2);
2097
2098 msleep(20);
2099 }
2100
2101 /* VMID=2x500k */
2102 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
2103 WM8994_VMID_SEL_MASK, 0x4);
2104
2105 break;
2106
2107 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002108 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
Mark Brownd522ffb2010-03-30 14:29:14 +01002109 /* Switch over to startup biases */
2110 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
2111 WM8994_BIAS_SRC |
2112 WM8994_STARTUP_BIAS_ENA |
2113 WM8994_VMID_BUF_ENA |
2114 WM8994_VMID_RAMP_MASK,
2115 WM8994_BIAS_SRC |
2116 WM8994_STARTUP_BIAS_ENA |
2117 WM8994_VMID_BUF_ENA |
2118 (1 << WM8994_VMID_RAMP_SHIFT));
Mark Brown9e6e96a2010-01-29 17:47:12 +00002119
Mark Brownd522ffb2010-03-30 14:29:14 +01002120 /* Disable main biases */
2121 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
2122 WM8994_BIAS_ENA |
2123 WM8994_VMID_SEL_MASK, 0);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002124
Mark Brownd522ffb2010-03-30 14:29:14 +01002125 /* Discharge line */
2126 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
2127 WM8994_LINEOUT1_DISCH |
2128 WM8994_LINEOUT2_DISCH,
2129 WM8994_LINEOUT1_DISCH |
2130 WM8994_LINEOUT2_DISCH);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002131
Mark Brownd522ffb2010-03-30 14:29:14 +01002132 msleep(5);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002133
Mark Brownd522ffb2010-03-30 14:29:14 +01002134 /* Switch off startup biases */
2135 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
2136 WM8994_BIAS_SRC |
2137 WM8994_STARTUP_BIAS_ENA |
2138 WM8994_VMID_BUF_ENA |
2139 WM8994_VMID_RAMP_MASK, 0);
Mark Brown39fb51a2010-11-26 17:23:43 +00002140
2141 pm_runtime_put(codec->dev);
Mark Brownd522ffb2010-03-30 14:29:14 +01002142 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002143 break;
2144 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002145 codec->dapm.bias_level = level;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002146 return 0;
2147}
2148
2149static int wm8994_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2150{
2151 struct snd_soc_codec *codec = dai->codec;
Mark Brownc4431df2010-11-26 15:21:07 +00002152 struct wm8994 *control = codec->control_data;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002153 int ms_reg;
2154 int aif1_reg;
2155 int ms = 0;
2156 int aif1 = 0;
2157
2158 switch (dai->id) {
2159 case 1:
2160 ms_reg = WM8994_AIF1_MASTER_SLAVE;
2161 aif1_reg = WM8994_AIF1_CONTROL_1;
2162 break;
2163 case 2:
2164 ms_reg = WM8994_AIF2_MASTER_SLAVE;
2165 aif1_reg = WM8994_AIF2_CONTROL_1;
2166 break;
2167 default:
2168 return -EINVAL;
2169 }
2170
2171 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
2172 case SND_SOC_DAIFMT_CBS_CFS:
2173 break;
2174 case SND_SOC_DAIFMT_CBM_CFM:
2175 ms = WM8994_AIF1_MSTR;
2176 break;
2177 default:
2178 return -EINVAL;
2179 }
2180
2181 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2182 case SND_SOC_DAIFMT_DSP_B:
2183 aif1 |= WM8994_AIF1_LRCLK_INV;
2184 case SND_SOC_DAIFMT_DSP_A:
2185 aif1 |= 0x18;
2186 break;
2187 case SND_SOC_DAIFMT_I2S:
2188 aif1 |= 0x10;
2189 break;
2190 case SND_SOC_DAIFMT_RIGHT_J:
2191 break;
2192 case SND_SOC_DAIFMT_LEFT_J:
2193 aif1 |= 0x8;
2194 break;
2195 default:
2196 return -EINVAL;
2197 }
2198
2199 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2200 case SND_SOC_DAIFMT_DSP_A:
2201 case SND_SOC_DAIFMT_DSP_B:
2202 /* frame inversion not valid for DSP modes */
2203 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2204 case SND_SOC_DAIFMT_NB_NF:
2205 break;
2206 case SND_SOC_DAIFMT_IB_NF:
2207 aif1 |= WM8994_AIF1_BCLK_INV;
2208 break;
2209 default:
2210 return -EINVAL;
2211 }
2212 break;
2213
2214 case SND_SOC_DAIFMT_I2S:
2215 case SND_SOC_DAIFMT_RIGHT_J:
2216 case SND_SOC_DAIFMT_LEFT_J:
2217 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2218 case SND_SOC_DAIFMT_NB_NF:
2219 break;
2220 case SND_SOC_DAIFMT_IB_IF:
2221 aif1 |= WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV;
2222 break;
2223 case SND_SOC_DAIFMT_IB_NF:
2224 aif1 |= WM8994_AIF1_BCLK_INV;
2225 break;
2226 case SND_SOC_DAIFMT_NB_IF:
2227 aif1 |= WM8994_AIF1_LRCLK_INV;
2228 break;
2229 default:
2230 return -EINVAL;
2231 }
2232 break;
2233 default:
2234 return -EINVAL;
2235 }
2236
Mark Brownc4431df2010-11-26 15:21:07 +00002237 /* The AIF2 format configuration needs to be mirrored to AIF3
2238 * on WM8958 if it's in use so just do it all the time. */
2239 if (control->type == WM8958 && dai->id == 2)
2240 snd_soc_update_bits(codec, WM8958_AIF3_CONTROL_1,
2241 WM8994_AIF1_LRCLK_INV |
2242 WM8958_AIF3_FMT_MASK, aif1);
2243
Mark Brown9e6e96a2010-01-29 17:47:12 +00002244 snd_soc_update_bits(codec, aif1_reg,
2245 WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV |
2246 WM8994_AIF1_FMT_MASK,
2247 aif1);
2248 snd_soc_update_bits(codec, ms_reg, WM8994_AIF1_MSTR,
2249 ms);
2250
2251 return 0;
2252}
2253
2254static struct {
2255 int val, rate;
2256} srs[] = {
2257 { 0, 8000 },
2258 { 1, 11025 },
2259 { 2, 12000 },
2260 { 3, 16000 },
2261 { 4, 22050 },
2262 { 5, 24000 },
2263 { 6, 32000 },
2264 { 7, 44100 },
2265 { 8, 48000 },
2266 { 9, 88200 },
2267 { 10, 96000 },
2268};
2269
2270static int fs_ratios[] = {
2271 64, 128, 192, 256, 348, 512, 768, 1024, 1408, 1536
2272};
2273
2274static int bclk_divs[] = {
2275 10, 15, 20, 30, 40, 50, 60, 80, 110, 120, 160, 220, 240, 320, 440, 480,
2276 640, 880, 960, 1280, 1760, 1920
2277};
2278
2279static int wm8994_hw_params(struct snd_pcm_substream *substream,
2280 struct snd_pcm_hw_params *params,
2281 struct snd_soc_dai *dai)
2282{
2283 struct snd_soc_codec *codec = dai->codec;
Mark Brownc4431df2010-11-26 15:21:07 +00002284 struct wm8994 *control = codec->control_data;
Mark Brownb2c812e2010-04-14 15:35:19 +09002285 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002286 int aif1_reg;
Mark Brownb1e43d92010-12-07 17:14:56 +00002287 int aif2_reg;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002288 int bclk_reg;
2289 int lrclk_reg;
2290 int rate_reg;
2291 int aif1 = 0;
Mark Brownb1e43d92010-12-07 17:14:56 +00002292 int aif2 = 0;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002293 int bclk = 0;
2294 int lrclk = 0;
2295 int rate_val = 0;
2296 int id = dai->id - 1;
2297
2298 int i, cur_val, best_val, bclk_rate, best;
2299
2300 switch (dai->id) {
2301 case 1:
2302 aif1_reg = WM8994_AIF1_CONTROL_1;
Mark Brownb1e43d92010-12-07 17:14:56 +00002303 aif2_reg = WM8994_AIF1_CONTROL_2;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002304 bclk_reg = WM8994_AIF1_BCLK;
2305 rate_reg = WM8994_AIF1_RATE;
2306 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
Mark Brown7d83d212010-08-23 10:54:43 +01002307 wm8994->lrclk_shared[0]) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002308 lrclk_reg = WM8994_AIF1DAC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002309 } else {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002310 lrclk_reg = WM8994_AIF1ADC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002311 dev_dbg(codec->dev, "AIF1 using split LRCLK\n");
2312 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002313 break;
2314 case 2:
2315 aif1_reg = WM8994_AIF2_CONTROL_1;
Mark Brownb1e43d92010-12-07 17:14:56 +00002316 aif2_reg = WM8994_AIF2_CONTROL_2;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002317 bclk_reg = WM8994_AIF2_BCLK;
2318 rate_reg = WM8994_AIF2_RATE;
2319 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
Mark Brown7d83d212010-08-23 10:54:43 +01002320 wm8994->lrclk_shared[1]) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002321 lrclk_reg = WM8994_AIF2DAC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002322 } else {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002323 lrclk_reg = WM8994_AIF2ADC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002324 dev_dbg(codec->dev, "AIF2 using split LRCLK\n");
2325 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002326 break;
Mark Brownc4431df2010-11-26 15:21:07 +00002327 case 3:
2328 switch (control->type) {
2329 case WM8958:
2330 aif1_reg = WM8958_AIF3_CONTROL_1;
2331 break;
2332 default:
2333 return 0;
2334 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002335 default:
2336 return -EINVAL;
2337 }
2338
2339 bclk_rate = params_rate(params) * 2;
2340 switch (params_format(params)) {
2341 case SNDRV_PCM_FORMAT_S16_LE:
2342 bclk_rate *= 16;
2343 break;
2344 case SNDRV_PCM_FORMAT_S20_3LE:
2345 bclk_rate *= 20;
2346 aif1 |= 0x20;
2347 break;
2348 case SNDRV_PCM_FORMAT_S24_LE:
2349 bclk_rate *= 24;
2350 aif1 |= 0x40;
2351 break;
2352 case SNDRV_PCM_FORMAT_S32_LE:
2353 bclk_rate *= 32;
2354 aif1 |= 0x60;
2355 break;
2356 default:
2357 return -EINVAL;
2358 }
2359
2360 /* Try to find an appropriate sample rate; look for an exact match. */
2361 for (i = 0; i < ARRAY_SIZE(srs); i++)
2362 if (srs[i].rate == params_rate(params))
2363 break;
2364 if (i == ARRAY_SIZE(srs))
2365 return -EINVAL;
2366 rate_val |= srs[i].val << WM8994_AIF1_SR_SHIFT;
2367
2368 dev_dbg(dai->dev, "Sample rate is %dHz\n", srs[i].rate);
2369 dev_dbg(dai->dev, "AIF%dCLK is %dHz, target BCLK %dHz\n",
2370 dai->id, wm8994->aifclk[id], bclk_rate);
2371
Mark Brownb1e43d92010-12-07 17:14:56 +00002372 if (params_channels(params) == 1 &&
2373 (snd_soc_read(codec, aif1_reg) & 0x18) == 0x18)
2374 aif2 |= WM8994_AIF1_MONO;
2375
Mark Brown9e6e96a2010-01-29 17:47:12 +00002376 if (wm8994->aifclk[id] == 0) {
2377 dev_err(dai->dev, "AIF%dCLK not configured\n", dai->id);
2378 return -EINVAL;
2379 }
2380
2381 /* AIFCLK/fs ratio; look for a close match in either direction */
2382 best = 0;
2383 best_val = abs((fs_ratios[0] * params_rate(params))
2384 - wm8994->aifclk[id]);
2385 for (i = 1; i < ARRAY_SIZE(fs_ratios); i++) {
2386 cur_val = abs((fs_ratios[i] * params_rate(params))
2387 - wm8994->aifclk[id]);
2388 if (cur_val >= best_val)
2389 continue;
2390 best = i;
2391 best_val = cur_val;
2392 }
2393 dev_dbg(dai->dev, "Selected AIF%dCLK/fs = %d\n",
2394 dai->id, fs_ratios[best]);
2395 rate_val |= best;
2396
2397 /* We may not get quite the right frequency if using
2398 * approximate clocks so look for the closest match that is
2399 * higher than the target (we need to ensure that there enough
2400 * BCLKs to clock out the samples).
2401 */
2402 best = 0;
2403 for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
Joonyoung Shim07cd8ad2010-02-02 18:53:19 +09002404 cur_val = (wm8994->aifclk[id] * 10 / bclk_divs[i]) - bclk_rate;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002405 if (cur_val < 0) /* BCLK table is sorted */
2406 break;
2407 best = i;
2408 }
Joonyoung Shim07cd8ad2010-02-02 18:53:19 +09002409 bclk_rate = wm8994->aifclk[id] * 10 / bclk_divs[best];
Mark Brown9e6e96a2010-01-29 17:47:12 +00002410 dev_dbg(dai->dev, "Using BCLK_DIV %d for actual BCLK %dHz\n",
2411 bclk_divs[best], bclk_rate);
2412 bclk |= best << WM8994_AIF1_BCLK_DIV_SHIFT;
2413
2414 lrclk = bclk_rate / params_rate(params);
2415 dev_dbg(dai->dev, "Using LRCLK rate %d for actual LRCLK %dHz\n",
2416 lrclk, bclk_rate / lrclk);
2417
2418 snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
Mark Brownb1e43d92010-12-07 17:14:56 +00002419 snd_soc_update_bits(codec, aif2_reg, WM8994_AIF1_MONO, aif2);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002420 snd_soc_update_bits(codec, bclk_reg, WM8994_AIF1_BCLK_DIV_MASK, bclk);
2421 snd_soc_update_bits(codec, lrclk_reg, WM8994_AIF1DAC_RATE_MASK,
2422 lrclk);
2423 snd_soc_update_bits(codec, rate_reg, WM8994_AIF1_SR_MASK |
2424 WM8994_AIF1CLK_RATE_MASK, rate_val);
2425
2426 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
2427 switch (dai->id) {
2428 case 1:
2429 wm8994->dac_rates[0] = params_rate(params);
2430 wm8994_set_retune_mobile(codec, 0);
2431 wm8994_set_retune_mobile(codec, 1);
2432 break;
2433 case 2:
2434 wm8994->dac_rates[1] = params_rate(params);
2435 wm8994_set_retune_mobile(codec, 2);
2436 break;
2437 }
2438 }
2439
2440 return 0;
2441}
2442
Mark Brownc4431df2010-11-26 15:21:07 +00002443static int wm8994_aif3_hw_params(struct snd_pcm_substream *substream,
2444 struct snd_pcm_hw_params *params,
2445 struct snd_soc_dai *dai)
2446{
2447 struct snd_soc_codec *codec = dai->codec;
2448 struct wm8994 *control = codec->control_data;
2449 int aif1_reg;
2450 int aif1 = 0;
2451
2452 switch (dai->id) {
2453 case 3:
2454 switch (control->type) {
2455 case WM8958:
2456 aif1_reg = WM8958_AIF3_CONTROL_1;
2457 break;
2458 default:
2459 return 0;
2460 }
2461 default:
2462 return 0;
2463 }
2464
2465 switch (params_format(params)) {
2466 case SNDRV_PCM_FORMAT_S16_LE:
2467 break;
2468 case SNDRV_PCM_FORMAT_S20_3LE:
2469 aif1 |= 0x20;
2470 break;
2471 case SNDRV_PCM_FORMAT_S24_LE:
2472 aif1 |= 0x40;
2473 break;
2474 case SNDRV_PCM_FORMAT_S32_LE:
2475 aif1 |= 0x60;
2476 break;
2477 default:
2478 return -EINVAL;
2479 }
2480
2481 return snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
2482}
2483
Mark Brown9e6e96a2010-01-29 17:47:12 +00002484static int wm8994_aif_mute(struct snd_soc_dai *codec_dai, int mute)
2485{
2486 struct snd_soc_codec *codec = codec_dai->codec;
2487 int mute_reg;
2488 int reg;
2489
2490 switch (codec_dai->id) {
2491 case 1:
2492 mute_reg = WM8994_AIF1_DAC1_FILTERS_1;
2493 break;
2494 case 2:
2495 mute_reg = WM8994_AIF2_DAC_FILTERS_1;
2496 break;
2497 default:
2498 return -EINVAL;
2499 }
2500
2501 if (mute)
2502 reg = WM8994_AIF1DAC1_MUTE;
2503 else
2504 reg = 0;
2505
2506 snd_soc_update_bits(codec, mute_reg, WM8994_AIF1DAC1_MUTE, reg);
2507
2508 return 0;
2509}
2510
Mark Brown778a76e2010-03-22 22:05:10 +00002511static int wm8994_set_tristate(struct snd_soc_dai *codec_dai, int tristate)
2512{
2513 struct snd_soc_codec *codec = codec_dai->codec;
2514 int reg, val, mask;
2515
2516 switch (codec_dai->id) {
2517 case 1:
2518 reg = WM8994_AIF1_MASTER_SLAVE;
2519 mask = WM8994_AIF1_TRI;
2520 break;
2521 case 2:
2522 reg = WM8994_AIF2_MASTER_SLAVE;
2523 mask = WM8994_AIF2_TRI;
2524 break;
2525 case 3:
2526 reg = WM8994_POWER_MANAGEMENT_6;
2527 mask = WM8994_AIF3_TRI;
2528 break;
2529 default:
2530 return -EINVAL;
2531 }
2532
2533 if (tristate)
2534 val = mask;
2535 else
2536 val = 0;
2537
Qiao Zhou78b3fb42011-01-19 19:10:47 +08002538 return snd_soc_update_bits(codec, reg, mask, val);
Mark Brown778a76e2010-03-22 22:05:10 +00002539}
2540
Mark Brown9e6e96a2010-01-29 17:47:12 +00002541#define WM8994_RATES SNDRV_PCM_RATE_8000_96000
2542
2543#define WM8994_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
Ian Lartey3079aed2010-08-31 23:56:34 +01002544 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002545
2546static struct snd_soc_dai_ops wm8994_aif1_dai_ops = {
2547 .set_sysclk = wm8994_set_dai_sysclk,
2548 .set_fmt = wm8994_set_dai_fmt,
2549 .hw_params = wm8994_hw_params,
2550 .digital_mute = wm8994_aif_mute,
2551 .set_pll = wm8994_set_fll,
Mark Brown778a76e2010-03-22 22:05:10 +00002552 .set_tristate = wm8994_set_tristate,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002553};
2554
2555static struct snd_soc_dai_ops wm8994_aif2_dai_ops = {
2556 .set_sysclk = wm8994_set_dai_sysclk,
2557 .set_fmt = wm8994_set_dai_fmt,
2558 .hw_params = wm8994_hw_params,
2559 .digital_mute = wm8994_aif_mute,
2560 .set_pll = wm8994_set_fll,
Mark Brown778a76e2010-03-22 22:05:10 +00002561 .set_tristate = wm8994_set_tristate,
2562};
2563
2564static struct snd_soc_dai_ops wm8994_aif3_dai_ops = {
Mark Brownc4431df2010-11-26 15:21:07 +00002565 .hw_params = wm8994_aif3_hw_params,
Mark Brown778a76e2010-03-22 22:05:10 +00002566 .set_tristate = wm8994_set_tristate,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002567};
2568
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002569static struct snd_soc_dai_driver wm8994_dai[] = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002570 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002571 .name = "wm8994-aif1",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002572 .id = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002573 .playback = {
2574 .stream_name = "AIF1 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002575 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002576 .channels_max = 2,
2577 .rates = WM8994_RATES,
2578 .formats = WM8994_FORMATS,
2579 },
2580 .capture = {
2581 .stream_name = "AIF1 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002582 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002583 .channels_max = 2,
2584 .rates = WM8994_RATES,
2585 .formats = WM8994_FORMATS,
2586 },
2587 .ops = &wm8994_aif1_dai_ops,
2588 },
2589 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002590 .name = "wm8994-aif2",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002591 .id = 2,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002592 .playback = {
2593 .stream_name = "AIF2 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002594 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002595 .channels_max = 2,
2596 .rates = WM8994_RATES,
2597 .formats = WM8994_FORMATS,
2598 },
2599 .capture = {
2600 .stream_name = "AIF2 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002601 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002602 .channels_max = 2,
2603 .rates = WM8994_RATES,
2604 .formats = WM8994_FORMATS,
2605 },
2606 .ops = &wm8994_aif2_dai_ops,
2607 },
2608 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002609 .name = "wm8994-aif3",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002610 .id = 3,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002611 .playback = {
2612 .stream_name = "AIF3 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002613 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002614 .channels_max = 2,
2615 .rates = WM8994_RATES,
2616 .formats = WM8994_FORMATS,
2617 },
Dan Carpentera8462bd2010-03-24 14:58:34 +03002618 .capture = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002619 .stream_name = "AIF3 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002620 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002621 .channels_max = 2,
2622 .rates = WM8994_RATES,
2623 .formats = WM8994_FORMATS,
2624 },
Mark Brown778a76e2010-03-22 22:05:10 +00002625 .ops = &wm8994_aif3_dai_ops,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002626 }
2627};
Mark Brown9e6e96a2010-01-29 17:47:12 +00002628
2629#ifdef CONFIG_PM
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002630static int wm8994_suspend(struct snd_soc_codec *codec, pm_message_t state)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002631{
Mark Brownb2c812e2010-04-14 15:35:19 +09002632 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002633 int i, ret;
2634
2635 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
2636 memcpy(&wm8994->fll_suspend[i], &wm8994->fll[i],
2637 sizeof(struct fll_config));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002638 ret = _wm8994_set_fll(codec, i + 1, 0, 0, 0);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002639 if (ret < 0)
2640 dev_warn(codec->dev, "Failed to stop FLL%d: %d\n",
2641 i + 1, ret);
2642 }
2643
2644 wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);
2645
2646 return 0;
2647}
2648
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002649static int wm8994_resume(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002650{
Mark Brownb2c812e2010-04-14 15:35:19 +09002651 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002652 int i, ret;
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00002653 unsigned int val, mask;
2654
2655 if (wm8994->revision < 4) {
2656 /* force a HW read */
2657 val = wm8994_reg_read(codec->control_data,
2658 WM8994_POWER_MANAGEMENT_5);
2659
2660 /* modify the cache only */
2661 codec->cache_only = 1;
2662 mask = WM8994_DAC1R_ENA | WM8994_DAC1L_ENA |
2663 WM8994_DAC2R_ENA | WM8994_DAC2L_ENA;
2664 val &= mask;
2665 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
2666 mask, val);
2667 codec->cache_only = 0;
2668 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002669
2670 /* Restore the registers */
Mark Brownca9aef52010-11-26 17:23:41 +00002671 ret = snd_soc_cache_sync(codec);
2672 if (ret != 0)
2673 dev_err(codec->dev, "Failed to sync cache: %d\n", ret);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002674
2675 wm8994_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
2676
2677 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
Mark Brown6a2f1ee2010-05-10 18:36:37 +01002678 if (!wm8994->fll_suspend[i].out)
2679 continue;
2680
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002681 ret = _wm8994_set_fll(codec, i + 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002682 wm8994->fll_suspend[i].src,
2683 wm8994->fll_suspend[i].in,
2684 wm8994->fll_suspend[i].out);
2685 if (ret < 0)
2686 dev_warn(codec->dev, "Failed to restore FLL%d: %d\n",
2687 i + 1, ret);
2688 }
2689
2690 return 0;
2691}
2692#else
2693#define wm8994_suspend NULL
2694#define wm8994_resume NULL
2695#endif
2696
2697static void wm8994_handle_retune_mobile_pdata(struct wm8994_priv *wm8994)
2698{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002699 struct snd_soc_codec *codec = wm8994->codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002700 struct wm8994_pdata *pdata = wm8994->pdata;
2701 struct snd_kcontrol_new controls[] = {
2702 SOC_ENUM_EXT("AIF1.1 EQ Mode",
2703 wm8994->retune_mobile_enum,
2704 wm8994_get_retune_mobile_enum,
2705 wm8994_put_retune_mobile_enum),
2706 SOC_ENUM_EXT("AIF1.2 EQ Mode",
2707 wm8994->retune_mobile_enum,
2708 wm8994_get_retune_mobile_enum,
2709 wm8994_put_retune_mobile_enum),
2710 SOC_ENUM_EXT("AIF2 EQ Mode",
2711 wm8994->retune_mobile_enum,
2712 wm8994_get_retune_mobile_enum,
2713 wm8994_put_retune_mobile_enum),
2714 };
2715 int ret, i, j;
2716 const char **t;
2717
2718 /* We need an array of texts for the enum API but the number
2719 * of texts is likely to be less than the number of
2720 * configurations due to the sample rate dependency of the
2721 * configurations. */
2722 wm8994->num_retune_mobile_texts = 0;
2723 wm8994->retune_mobile_texts = NULL;
2724 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
2725 for (j = 0; j < wm8994->num_retune_mobile_texts; j++) {
2726 if (strcmp(pdata->retune_mobile_cfgs[i].name,
2727 wm8994->retune_mobile_texts[j]) == 0)
2728 break;
2729 }
2730
2731 if (j != wm8994->num_retune_mobile_texts)
2732 continue;
2733
2734 /* Expand the array... */
2735 t = krealloc(wm8994->retune_mobile_texts,
2736 sizeof(char *) *
2737 (wm8994->num_retune_mobile_texts + 1),
2738 GFP_KERNEL);
2739 if (t == NULL)
2740 continue;
2741
2742 /* ...store the new entry... */
2743 t[wm8994->num_retune_mobile_texts] =
2744 pdata->retune_mobile_cfgs[i].name;
2745
2746 /* ...and remember the new version. */
2747 wm8994->num_retune_mobile_texts++;
2748 wm8994->retune_mobile_texts = t;
2749 }
2750
2751 dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
2752 wm8994->num_retune_mobile_texts);
2753
2754 wm8994->retune_mobile_enum.max = wm8994->num_retune_mobile_texts;
2755 wm8994->retune_mobile_enum.texts = wm8994->retune_mobile_texts;
2756
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002757 ret = snd_soc_add_controls(wm8994->codec, controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002758 ARRAY_SIZE(controls));
2759 if (ret != 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002760 dev_err(wm8994->codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002761 "Failed to add ReTune Mobile controls: %d\n", ret);
2762}
2763
2764static void wm8994_handle_pdata(struct wm8994_priv *wm8994)
2765{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002766 struct snd_soc_codec *codec = wm8994->codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002767 struct wm8994_pdata *pdata = wm8994->pdata;
2768 int ret, i;
2769
2770 if (!pdata)
2771 return;
2772
2773 wm_hubs_handle_analogue_pdata(codec, pdata->lineout1_diff,
2774 pdata->lineout2_diff,
2775 pdata->lineout1fb,
2776 pdata->lineout2fb,
2777 pdata->jd_scthr,
2778 pdata->jd_thr,
2779 pdata->micbias1_lvl,
2780 pdata->micbias2_lvl);
2781
2782 dev_dbg(codec->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
2783
2784 if (pdata->num_drc_cfgs) {
2785 struct snd_kcontrol_new controls[] = {
2786 SOC_ENUM_EXT("AIF1DRC1 Mode", wm8994->drc_enum,
2787 wm8994_get_drc_enum, wm8994_put_drc_enum),
2788 SOC_ENUM_EXT("AIF1DRC2 Mode", wm8994->drc_enum,
2789 wm8994_get_drc_enum, wm8994_put_drc_enum),
2790 SOC_ENUM_EXT("AIF2DRC Mode", wm8994->drc_enum,
2791 wm8994_get_drc_enum, wm8994_put_drc_enum),
2792 };
2793
2794 /* We need an array of texts for the enum API */
2795 wm8994->drc_texts = kmalloc(sizeof(char *)
2796 * pdata->num_drc_cfgs, GFP_KERNEL);
2797 if (!wm8994->drc_texts) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002798 dev_err(wm8994->codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002799 "Failed to allocate %d DRC config texts\n",
2800 pdata->num_drc_cfgs);
2801 return;
2802 }
2803
2804 for (i = 0; i < pdata->num_drc_cfgs; i++)
2805 wm8994->drc_texts[i] = pdata->drc_cfgs[i].name;
2806
2807 wm8994->drc_enum.max = pdata->num_drc_cfgs;
2808 wm8994->drc_enum.texts = wm8994->drc_texts;
2809
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002810 ret = snd_soc_add_controls(wm8994->codec, controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002811 ARRAY_SIZE(controls));
2812 if (ret != 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002813 dev_err(wm8994->codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002814 "Failed to add DRC mode controls: %d\n", ret);
2815
2816 for (i = 0; i < WM8994_NUM_DRC; i++)
2817 wm8994_set_drc(codec, i);
2818 }
2819
2820 dev_dbg(codec->dev, "%d ReTune Mobile configurations\n",
2821 pdata->num_retune_mobile_cfgs);
2822
Mark Brown131d8102010-11-30 17:03:39 +00002823 if (pdata->num_mbc_cfgs) {
2824 struct snd_kcontrol_new control[] = {
2825 SOC_ENUM_EXT("MBC Mode", wm8994->mbc_enum,
2826 wm8958_get_mbc_enum, wm8958_put_mbc_enum),
2827 };
2828
2829 /* We need an array of texts for the enum API */
2830 wm8994->mbc_texts = kmalloc(sizeof(char *)
2831 * pdata->num_mbc_cfgs, GFP_KERNEL);
2832 if (!wm8994->mbc_texts) {
2833 dev_err(wm8994->codec->dev,
2834 "Failed to allocate %d MBC config texts\n",
2835 pdata->num_mbc_cfgs);
2836 return;
2837 }
2838
2839 for (i = 0; i < pdata->num_mbc_cfgs; i++)
2840 wm8994->mbc_texts[i] = pdata->mbc_cfgs[i].name;
2841
2842 wm8994->mbc_enum.max = pdata->num_mbc_cfgs;
2843 wm8994->mbc_enum.texts = wm8994->mbc_texts;
2844
2845 ret = snd_soc_add_controls(wm8994->codec, control, 1);
2846 if (ret != 0)
2847 dev_err(wm8994->codec->dev,
2848 "Failed to add MBC mode controls: %d\n", ret);
2849 }
2850
Mark Brown9e6e96a2010-01-29 17:47:12 +00002851 if (pdata->num_retune_mobile_cfgs)
2852 wm8994_handle_retune_mobile_pdata(wm8994);
2853 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002854 snd_soc_add_controls(wm8994->codec, wm8994_eq_controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002855 ARRAY_SIZE(wm8994_eq_controls));
Mark Brown48e028e2011-02-21 17:11:59 -08002856
2857 for (i = 0; i < ARRAY_SIZE(pdata->micbias); i++) {
2858 if (pdata->micbias[i]) {
2859 snd_soc_write(codec, WM8958_MICBIAS1 + i,
2860 pdata->micbias[i] & 0xffff);
2861 }
2862 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002863}
2864
Mark Brown88766982010-03-29 20:57:12 +01002865/**
2866 * wm8994_mic_detect - Enable microphone detection via the WM8994 IRQ
2867 *
2868 * @codec: WM8994 codec
2869 * @jack: jack to report detection events on
2870 * @micbias: microphone bias to detect on
2871 * @det: value to report for presence detection
2872 * @shrt: value to report for short detection
2873 *
2874 * Enable microphone detection via IRQ on the WM8994. If GPIOs are
2875 * being used to bring out signals to the processor then only platform
Mark Brown5ab230a2010-09-06 14:59:34 +01002876 * data configuration is needed for WM8994 and processor GPIOs should
Mark Brown88766982010-03-29 20:57:12 +01002877 * be configured using snd_soc_jack_add_gpios() instead.
2878 *
2879 * Configuration of detection levels is available via the micbias1_lvl
2880 * and micbias2_lvl platform data members.
2881 */
2882int wm8994_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
2883 int micbias, int det, int shrt)
2884{
Mark Brownb2c812e2010-04-14 15:35:19 +09002885 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown88766982010-03-29 20:57:12 +01002886 struct wm8994_micdet *micdet;
Mark Brown3a423152010-11-26 15:21:06 +00002887 struct wm8994 *control = codec->control_data;
Mark Brown88766982010-03-29 20:57:12 +01002888 int reg;
2889
Mark Brown3a423152010-11-26 15:21:06 +00002890 if (control->type != WM8994)
2891 return -EINVAL;
2892
Mark Brown88766982010-03-29 20:57:12 +01002893 switch (micbias) {
2894 case 1:
2895 micdet = &wm8994->micdet[0];
2896 break;
2897 case 2:
2898 micdet = &wm8994->micdet[1];
2899 break;
2900 default:
2901 return -EINVAL;
2902 }
2903
2904 dev_dbg(codec->dev, "Configuring microphone detection on %d: %x %x\n",
2905 micbias, det, shrt);
2906
2907 /* Store the configuration */
2908 micdet->jack = jack;
2909 micdet->det = det;
2910 micdet->shrt = shrt;
2911
2912 /* If either of the jacks is set up then enable detection */
2913 if (wm8994->micdet[0].jack || wm8994->micdet[1].jack)
2914 reg = WM8994_MICD_ENA;
2915 else
2916 reg = 0;
2917
2918 snd_soc_update_bits(codec, WM8994_MICBIAS, WM8994_MICD_ENA, reg);
2919
2920 return 0;
2921}
2922EXPORT_SYMBOL_GPL(wm8994_mic_detect);
2923
2924static irqreturn_t wm8994_mic_irq(int irq, void *data)
2925{
2926 struct wm8994_priv *priv = data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002927 struct snd_soc_codec *codec = priv->codec;
Mark Brown88766982010-03-29 20:57:12 +01002928 int reg;
2929 int report;
2930
Mark Brown7116f452010-12-29 13:05:21 +00002931#ifndef CONFIG_SND_SOC_WM8994_MODULE
Mark Brown2bbb5d62010-12-05 12:50:12 +00002932 trace_snd_soc_jack_irq(dev_name(codec->dev));
Mark Brown7116f452010-12-29 13:05:21 +00002933#endif
Mark Brown2bbb5d62010-12-05 12:50:12 +00002934
Mark Brown88766982010-03-29 20:57:12 +01002935 reg = snd_soc_read(codec, WM8994_INTERRUPT_RAW_STATUS_2);
2936 if (reg < 0) {
2937 dev_err(codec->dev, "Failed to read microphone status: %d\n",
2938 reg);
2939 return IRQ_HANDLED;
2940 }
2941
2942 dev_dbg(codec->dev, "Microphone status: %x\n", reg);
2943
2944 report = 0;
2945 if (reg & WM8994_MIC1_DET_STS)
2946 report |= priv->micdet[0].det;
2947 if (reg & WM8994_MIC1_SHRT_STS)
2948 report |= priv->micdet[0].shrt;
2949 snd_soc_jack_report(priv->micdet[0].jack, report,
2950 priv->micdet[0].det | priv->micdet[0].shrt);
2951
2952 report = 0;
2953 if (reg & WM8994_MIC2_DET_STS)
2954 report |= priv->micdet[1].det;
2955 if (reg & WM8994_MIC2_SHRT_STS)
2956 report |= priv->micdet[1].shrt;
2957 snd_soc_jack_report(priv->micdet[1].jack, report,
2958 priv->micdet[1].det | priv->micdet[1].shrt);
2959
2960 return IRQ_HANDLED;
2961}
2962
Mark Brown821edd22010-11-26 15:21:09 +00002963/* Default microphone detection handler for WM8958 - the user can
2964 * override this if they wish.
2965 */
2966static void wm8958_default_micdet(u16 status, void *data)
2967{
2968 struct snd_soc_codec *codec = data;
2969 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2970 int report = 0;
2971
2972 /* If nothing present then clear our statuses */
Mark Brown864c4bd2011-02-21 20:51:13 -08002973 if (!(status & WM8958_MICD_STS))
Mark Brown821edd22010-11-26 15:21:09 +00002974 goto done;
Mark Brown821edd22010-11-26 15:21:09 +00002975
Mark Brown864c4bd2011-02-21 20:51:13 -08002976 report = SND_JACK_MICROPHONE;
Mark Brown821edd22010-11-26 15:21:09 +00002977
2978 /* Everything else is buttons; just assign slots */
Mark Brown864c4bd2011-02-21 20:51:13 -08002979 if (status & 0x1c0)
Mark Brown821edd22010-11-26 15:21:09 +00002980 report |= SND_JACK_BTN_0;
Mark Brown821edd22010-11-26 15:21:09 +00002981
2982done:
Mark Brown406e56c2011-02-21 20:41:25 -08002983 snd_soc_jack_report(wm8994->micdet[0].jack, report,
Mark Brown864c4bd2011-02-21 20:51:13 -08002984 SND_JACK_BTN_0 | SND_JACK_MICROPHONE);
Mark Brown821edd22010-11-26 15:21:09 +00002985}
2986
2987/**
2988 * wm8958_mic_detect - Enable microphone detection via the WM8958 IRQ
2989 *
2990 * @codec: WM8958 codec
2991 * @jack: jack to report detection events on
2992 *
2993 * Enable microphone detection functionality for the WM8958. By
2994 * default simple detection which supports the detection of up to 6
2995 * buttons plus video and microphone functionality is supported.
2996 *
2997 * The WM8958 has an advanced jack detection facility which is able to
2998 * support complex accessory detection, especially when used in
2999 * conjunction with external circuitry. In order to provide maximum
3000 * flexiblity a callback is provided which allows a completely custom
3001 * detection algorithm.
3002 */
3003int wm8958_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
3004 wm8958_micdet_cb cb, void *cb_data)
3005{
3006 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
3007 struct wm8994 *control = codec->control_data;
3008
3009 if (control->type != WM8958)
3010 return -EINVAL;
3011
3012 if (jack) {
3013 if (!cb) {
3014 dev_dbg(codec->dev, "Using default micdet callback\n");
3015 cb = wm8958_default_micdet;
3016 cb_data = codec;
3017 }
3018
3019 wm8994->micdet[0].jack = jack;
3020 wm8994->jack_cb = cb;
3021 wm8994->jack_cb_data = cb_data;
3022
3023 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3024 WM8958_MICD_ENA, WM8958_MICD_ENA);
3025 } else {
3026 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
3027 WM8958_MICD_ENA, 0);
3028 }
3029
3030 return 0;
3031}
3032EXPORT_SYMBOL_GPL(wm8958_mic_detect);
3033
3034static irqreturn_t wm8958_mic_irq(int irq, void *data)
3035{
3036 struct wm8994_priv *wm8994 = data;
3037 struct snd_soc_codec *codec = wm8994->codec;
3038 int reg;
3039
3040 reg = snd_soc_read(codec, WM8958_MIC_DETECT_3);
3041 if (reg < 0) {
3042 dev_err(codec->dev, "Failed to read mic detect status: %d\n",
3043 reg);
3044 return IRQ_NONE;
3045 }
3046
3047 if (!(reg & WM8958_MICD_VALID)) {
3048 dev_dbg(codec->dev, "Mic detect data not valid\n");
3049 goto out;
3050 }
3051
Mark Brown7116f452010-12-29 13:05:21 +00003052#ifndef CONFIG_SND_SOC_WM8994_MODULE
Mark Brown2bbb5d62010-12-05 12:50:12 +00003053 trace_snd_soc_jack_irq(dev_name(codec->dev));
Mark Brown7116f452010-12-29 13:05:21 +00003054#endif
Mark Brown2bbb5d62010-12-05 12:50:12 +00003055
Mark Brown821edd22010-11-26 15:21:09 +00003056 if (wm8994->jack_cb)
3057 wm8994->jack_cb(reg, wm8994->jack_cb_data);
3058 else
3059 dev_warn(codec->dev, "Accessory detection with no callback\n");
3060
3061out:
3062 return IRQ_HANDLED;
3063}
3064
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003065static int wm8994_codec_probe(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003066{
Mark Brown3a423152010-11-26 15:21:06 +00003067 struct wm8994 *control;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003068 struct wm8994_priv *wm8994;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003069 struct snd_soc_dapm_context *dapm = &codec->dapm;
Mark Brownec62dbd2010-08-15 14:56:40 +01003070 int ret, i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003071
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003072 codec->control_data = dev_get_drvdata(codec->dev->parent);
Mark Brown3a423152010-11-26 15:21:06 +00003073 control = codec->control_data;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003074
3075 wm8994 = kzalloc(sizeof(struct wm8994_priv), GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003076 if (wm8994 == NULL)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003077 return -ENOMEM;
Mark Brownb2c812e2010-04-14 15:35:19 +09003078 snd_soc_codec_set_drvdata(codec, wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003079
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003080 wm8994->pdata = dev_get_platdata(codec->dev->parent);
3081 wm8994->codec = codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003082
Mark Brown9b7c5252011-02-17 20:05:44 -08003083 if (wm8994->pdata && wm8994->pdata->micdet_irq)
3084 wm8994->micdet_irq = wm8994->pdata->micdet_irq;
3085 else if (wm8994->pdata && wm8994->pdata->irq_base)
3086 wm8994->micdet_irq = wm8994->pdata->irq_base +
3087 WM8994_IRQ_MIC1_DET;
3088
Mark Brown39fb51a2010-11-26 17:23:43 +00003089 pm_runtime_enable(codec->dev);
3090 pm_runtime_resume(codec->dev);
3091
Mark Brownca9aef52010-11-26 17:23:41 +00003092 /* Read our current status back from the chip - we don't want to
3093 * reset as this may interfere with the GPIO or LDO operation. */
3094 for (i = 0; i < WM8994_CACHE_SIZE; i++) {
Dimitris Papastamosd4754ec2011-01-13 12:20:37 +00003095 if (!wm8994_readable(codec, i) || wm8994_volatile(codec, i))
Mark Brownca9aef52010-11-26 17:23:41 +00003096 continue;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003097
Mark Brownca9aef52010-11-26 17:23:41 +00003098 ret = wm8994_reg_read(codec->control_data, i);
3099 if (ret <= 0)
3100 continue;
3101
3102 ret = snd_soc_cache_write(codec, i, ret);
3103 if (ret != 0) {
3104 dev_err(codec->dev,
3105 "Failed to initialise cache for 0x%x: %d\n",
3106 i, ret);
3107 goto err;
3108 }
3109 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00003110
3111 /* Set revision-specific configuration */
Mark Brownb6b05692010-08-13 12:58:20 +01003112 wm8994->revision = snd_soc_read(codec, WM8994_CHIP_REVISION);
Mark Brown3a423152010-11-26 15:21:06 +00003113 switch (control->type) {
3114 case WM8994:
3115 switch (wm8994->revision) {
3116 case 2:
3117 case 3:
3118 wm8994->hubs.dcs_codes = -5;
3119 wm8994->hubs.hp_startup_mode = 1;
3120 wm8994->hubs.dcs_readback_mode = 1;
3121 break;
3122 default:
3123 wm8994->hubs.dcs_readback_mode = 1;
3124 break;
3125 }
3126
3127 case WM8958:
Mark Brown8437f702010-03-29 17:09:45 +01003128 wm8994->hubs.dcs_readback_mode = 1;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003129 break;
Mark Brown3a423152010-11-26 15:21:06 +00003130
Mark Brown9e6e96a2010-01-29 17:47:12 +00003131 default:
3132 break;
3133 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00003134
Mark Brown3a423152010-11-26 15:21:06 +00003135 switch (control->type) {
3136 case WM8994:
Mark Brown9b7c5252011-02-17 20:05:44 -08003137 if (wm8994->micdet_irq) {
3138 ret = request_threaded_irq(wm8994->micdet_irq, NULL,
3139 wm8994_mic_irq,
3140 IRQF_TRIGGER_RISING,
3141 "Mic1 detect",
3142 wm8994);
3143 if (ret != 0)
3144 dev_warn(codec->dev,
3145 "Failed to request Mic1 detect IRQ: %d\n",
3146 ret);
3147 }
Mark Brown88766982010-03-29 20:57:12 +01003148
Mark Brown3a423152010-11-26 15:21:06 +00003149 ret = wm8994_request_irq(codec->control_data,
3150 WM8994_IRQ_MIC1_SHRT,
3151 wm8994_mic_irq, "Mic 1 short",
3152 wm8994);
3153 if (ret != 0)
3154 dev_warn(codec->dev,
3155 "Failed to request Mic1 short IRQ: %d\n",
3156 ret);
Mark Brown88766982010-03-29 20:57:12 +01003157
Mark Brown3a423152010-11-26 15:21:06 +00003158 ret = wm8994_request_irq(codec->control_data,
3159 WM8994_IRQ_MIC2_DET,
3160 wm8994_mic_irq, "Mic 2 detect",
3161 wm8994);
3162 if (ret != 0)
3163 dev_warn(codec->dev,
3164 "Failed to request Mic2 detect IRQ: %d\n",
3165 ret);
Mark Brown88766982010-03-29 20:57:12 +01003166
Mark Brown3a423152010-11-26 15:21:06 +00003167 ret = wm8994_request_irq(codec->control_data,
3168 WM8994_IRQ_MIC2_SHRT,
3169 wm8994_mic_irq, "Mic 2 short",
3170 wm8994);
3171 if (ret != 0)
3172 dev_warn(codec->dev,
3173 "Failed to request Mic2 short IRQ: %d\n",
3174 ret);
3175 break;
Mark Brown821edd22010-11-26 15:21:09 +00003176
3177 case WM8958:
Mark Brown9b7c5252011-02-17 20:05:44 -08003178 if (wm8994->micdet_irq) {
3179 ret = request_threaded_irq(wm8994->micdet_irq, NULL,
3180 wm8958_mic_irq,
3181 IRQF_TRIGGER_RISING,
3182 "Mic detect",
3183 wm8994);
3184 if (ret != 0)
3185 dev_warn(codec->dev,
3186 "Failed to request Mic detect IRQ: %d\n",
3187 ret);
3188 }
Mark Brown3a423152010-11-26 15:21:06 +00003189 }
Mark Brown88766982010-03-29 20:57:12 +01003190
Mark Brown9e6e96a2010-01-29 17:47:12 +00003191 /* Remember if AIFnLRCLK is configured as a GPIO. This should be
3192 * configured on init - if a system wants to do this dynamically
3193 * at runtime we can deal with that then.
3194 */
3195 ret = wm8994_reg_read(codec->control_data, WM8994_GPIO_1);
3196 if (ret < 0) {
3197 dev_err(codec->dev, "Failed to read GPIO1 state: %d\n", ret);
Mark Brown88766982010-03-29 20:57:12 +01003198 goto err_irq;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003199 }
3200 if ((ret & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
3201 wm8994->lrclk_shared[0] = 1;
3202 wm8994_dai[0].symmetric_rates = 1;
3203 } else {
3204 wm8994->lrclk_shared[0] = 0;
3205 }
3206
3207 ret = wm8994_reg_read(codec->control_data, WM8994_GPIO_6);
3208 if (ret < 0) {
3209 dev_err(codec->dev, "Failed to read GPIO6 state: %d\n", ret);
Mark Brown88766982010-03-29 20:57:12 +01003210 goto err_irq;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003211 }
3212 if ((ret & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
3213 wm8994->lrclk_shared[1] = 1;
3214 wm8994_dai[1].symmetric_rates = 1;
3215 } else {
3216 wm8994->lrclk_shared[1] = 0;
3217 }
3218
Mark Brown9e6e96a2010-01-29 17:47:12 +00003219 wm8994_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
3220
Mark Brown9e6e96a2010-01-29 17:47:12 +00003221 /* Latch volume updates (right only; we always do left then right). */
3222 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_RIGHT_VOLUME,
3223 WM8994_AIF1DAC1_VU, WM8994_AIF1DAC1_VU);
3224 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_RIGHT_VOLUME,
3225 WM8994_AIF1DAC2_VU, WM8994_AIF1DAC2_VU);
3226 snd_soc_update_bits(codec, WM8994_AIF2_DAC_RIGHT_VOLUME,
3227 WM8994_AIF2DAC_VU, WM8994_AIF2DAC_VU);
3228 snd_soc_update_bits(codec, WM8994_AIF1_ADC1_RIGHT_VOLUME,
3229 WM8994_AIF1ADC1_VU, WM8994_AIF1ADC1_VU);
3230 snd_soc_update_bits(codec, WM8994_AIF1_ADC2_RIGHT_VOLUME,
3231 WM8994_AIF1ADC2_VU, WM8994_AIF1ADC2_VU);
3232 snd_soc_update_bits(codec, WM8994_AIF2_ADC_RIGHT_VOLUME,
3233 WM8994_AIF2ADC_VU, WM8994_AIF1ADC2_VU);
3234 snd_soc_update_bits(codec, WM8994_DAC1_RIGHT_VOLUME,
3235 WM8994_DAC1_VU, WM8994_DAC1_VU);
3236 snd_soc_update_bits(codec, WM8994_DAC2_RIGHT_VOLUME,
3237 WM8994_DAC2_VU, WM8994_DAC2_VU);
3238
3239 /* Set the low bit of the 3D stereo depth so TLV matches */
3240 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_FILTERS_2,
3241 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT,
3242 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT);
3243 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_FILTERS_2,
3244 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT,
3245 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT);
3246 snd_soc_update_bits(codec, WM8994_AIF2_DAC_FILTERS_2,
3247 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT,
3248 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT);
3249
Mark Brownd1ce6b22010-07-20 10:13:14 +01003250 /* Unconditionally enable AIF1 ADC TDM mode; it only affects
3251 * behaviour on idle TDM clock cycles. */
3252 snd_soc_update_bits(codec, WM8994_AIF1_CONTROL_1,
3253 WM8994_AIF1ADC_TDM, WM8994_AIF1ADC_TDM);
3254
Mark Brown9e6e96a2010-01-29 17:47:12 +00003255 wm8994_update_class_w(codec);
3256
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003257 wm8994_handle_pdata(wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003258
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003259 wm_hubs_add_analogue_controls(codec);
3260 snd_soc_add_controls(codec, wm8994_snd_controls,
3261 ARRAY_SIZE(wm8994_snd_controls));
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003262 snd_soc_dapm_new_controls(dapm, wm8994_dapm_widgets,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003263 ARRAY_SIZE(wm8994_dapm_widgets));
Mark Brownc4431df2010-11-26 15:21:07 +00003264
3265 switch (control->type) {
3266 case WM8994:
3267 snd_soc_dapm_new_controls(dapm, wm8994_specific_dapm_widgets,
3268 ARRAY_SIZE(wm8994_specific_dapm_widgets));
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00003269 if (wm8994->revision < 4) {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00003270 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
3271 ARRAY_SIZE(wm8994_lateclk_revd_widgets));
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00003272 snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
3273 ARRAY_SIZE(wm8994_dac_revd_widgets));
3274 } else {
Dimitris Papastamos173efa02011-02-11 16:32:11 +00003275 snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
3276 ARRAY_SIZE(wm8994_lateclk_widgets));
Dimitris Papastamosc52fd022011-02-11 16:32:12 +00003277 snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
3278 ARRAY_SIZE(wm8994_dac_widgets));
3279 }
Mark Brownc4431df2010-11-26 15:21:07 +00003280 break;
3281 case WM8958:
3282 snd_soc_add_controls(codec, wm8958_snd_controls,
3283 ARRAY_SIZE(wm8958_snd_controls));
3284 snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
3285 ARRAY_SIZE(wm8958_dapm_widgets));
3286 break;
3287 }
3288
3289
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003290 wm_hubs_add_analogue_routes(codec, 0, 0);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003291 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
Mark Brown9e6e96a2010-01-29 17:47:12 +00003292
Mark Brownc4431df2010-11-26 15:21:07 +00003293 switch (control->type) {
3294 case WM8994:
3295 snd_soc_dapm_add_routes(dapm, wm8994_intercon,
3296 ARRAY_SIZE(wm8994_intercon));
Mark Brown6ed8f142011-02-03 16:27:35 +00003297
Dimitris Papastamos173efa02011-02-11 16:32:11 +00003298 if (wm8994->revision < 4) {
Mark Brown6ed8f142011-02-03 16:27:35 +00003299 snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
3300 ARRAY_SIZE(wm8994_revd_intercon));
Dimitris Papastamos173efa02011-02-11 16:32:11 +00003301 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
3302 ARRAY_SIZE(wm8994_lateclk_revd_intercon));
3303 } else {
3304 snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
3305 ARRAY_SIZE(wm8994_lateclk_intercon));
3306 }
Mark Brownc4431df2010-11-26 15:21:07 +00003307 break;
3308 case WM8958:
3309 snd_soc_dapm_add_routes(dapm, wm8958_intercon,
3310 ARRAY_SIZE(wm8958_intercon));
3311 break;
3312 }
3313
Mark Brown9e6e96a2010-01-29 17:47:12 +00003314 return 0;
3315
Mark Brown88766982010-03-29 20:57:12 +01003316err_irq:
3317 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC2_SHRT, wm8994);
3318 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC2_DET, wm8994);
3319 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC1_SHRT, wm8994);
Mark Brown9b7c5252011-02-17 20:05:44 -08003320 if (wm8994->micdet_irq)
3321 free_irq(wm8994->micdet_irq, wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003322err:
3323 kfree(wm8994);
3324 return ret;
3325}
3326
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003327static int wm8994_codec_remove(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003328{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003329 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown3a423152010-11-26 15:21:06 +00003330 struct wm8994 *control = codec->control_data;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003331
3332 wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003333
Mark Brown39fb51a2010-11-26 17:23:43 +00003334 pm_runtime_disable(codec->dev);
3335
Mark Brown3a423152010-11-26 15:21:06 +00003336 switch (control->type) {
3337 case WM8994:
Mark Brown9b7c5252011-02-17 20:05:44 -08003338 if (wm8994->micdet_irq)
3339 free_irq(wm8994->micdet_irq, wm8994);
Mark Brown3a423152010-11-26 15:21:06 +00003340 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC2_DET,
3341 wm8994);
3342 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC1_SHRT,
3343 wm8994);
3344 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC1_DET,
3345 wm8994);
3346 break;
Mark Brown821edd22010-11-26 15:21:09 +00003347
3348 case WM8958:
Mark Brown9b7c5252011-02-17 20:05:44 -08003349 if (wm8994->micdet_irq)
3350 free_irq(wm8994->micdet_irq, wm8994);
Mark Brown821edd22010-11-26 15:21:09 +00003351 break;
Mark Brown3a423152010-11-26 15:21:06 +00003352 }
Axel Lin24fb2b12010-11-23 15:58:39 +08003353 kfree(wm8994->retune_mobile_texts);
3354 kfree(wm8994->drc_texts);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003355 kfree(wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003356
3357 return 0;
3358}
3359
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003360static struct snd_soc_codec_driver soc_codec_dev_wm8994 = {
3361 .probe = wm8994_codec_probe,
3362 .remove = wm8994_codec_remove,
3363 .suspend = wm8994_suspend,
3364 .resume = wm8994_resume,
Mark Brownca9aef52010-11-26 17:23:41 +00003365 .read = wm8994_read,
3366 .write = wm8994_write,
Mark Browneba19fd2010-11-19 16:09:15 +00003367 .readable_register = wm8994_readable,
3368 .volatile_register = wm8994_volatile,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003369 .set_bias_level = wm8994_set_bias_level,
Mark Brownca9aef52010-11-26 17:23:41 +00003370
3371 .reg_cache_size = WM8994_CACHE_SIZE,
3372 .reg_cache_default = wm8994_reg_defaults,
3373 .reg_word_size = 2,
Mark Brown2e19b0c2010-11-26 17:23:42 +00003374 .compress_type = SND_SOC_RBTREE_COMPRESSION,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003375};
3376
3377static int __devinit wm8994_probe(struct platform_device *pdev)
3378{
3379 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wm8994,
3380 wm8994_dai, ARRAY_SIZE(wm8994_dai));
3381}
3382
3383static int __devexit wm8994_remove(struct platform_device *pdev)
3384{
3385 snd_soc_unregister_codec(&pdev->dev);
3386 return 0;
3387}
3388
Mark Brown9e6e96a2010-01-29 17:47:12 +00003389static struct platform_driver wm8994_codec_driver = {
3390 .driver = {
3391 .name = "wm8994-codec",
3392 .owner = THIS_MODULE,
3393 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003394 .probe = wm8994_probe,
3395 .remove = __devexit_p(wm8994_remove),
Mark Brown9e6e96a2010-01-29 17:47:12 +00003396};
3397
3398static __init int wm8994_init(void)
3399{
3400 return platform_driver_register(&wm8994_codec_driver);
3401}
3402module_init(wm8994_init);
3403
3404static __exit void wm8994_exit(void)
3405{
3406 platform_driver_unregister(&wm8994_codec_driver);
3407}
3408module_exit(wm8994_exit);
3409
3410
3411MODULE_DESCRIPTION("ASoC WM8994 driver");
3412MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
3413MODULE_LICENSE("GPL");
3414MODULE_ALIAS("platform:wm8994-codec");