blob: 3e308ad97ddfd05d4303a03792354fede3a706c5 [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;
105 bool jack_is_mic;
106 bool jack_is_video;
107
Mark Brownb6b05692010-08-13 12:58:20 +0100108 int revision;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000109 struct wm8994_pdata *pdata;
110};
111
Mark Brown9e6e96a2010-01-29 17:47:12 +0000112static int wm8994_readable(unsigned int reg)
113{
Mark Browne88ff1e2010-07-09 00:12:08 +0900114 switch (reg) {
115 case WM8994_GPIO_1:
116 case WM8994_GPIO_2:
117 case WM8994_GPIO_3:
118 case WM8994_GPIO_4:
119 case WM8994_GPIO_5:
120 case WM8994_GPIO_6:
121 case WM8994_GPIO_7:
122 case WM8994_GPIO_8:
123 case WM8994_GPIO_9:
124 case WM8994_GPIO_10:
125 case WM8994_GPIO_11:
126 case WM8994_INTERRUPT_STATUS_1:
127 case WM8994_INTERRUPT_STATUS_2:
128 case WM8994_INTERRUPT_RAW_STATUS_2:
129 return 1;
130 default:
131 break;
132 }
133
Mark Brown7b306da2010-11-16 20:11:40 +0000134 if (reg >= WM8994_CACHE_SIZE)
Mark Brown9e6e96a2010-01-29 17:47:12 +0000135 return 0;
Mark Brown7b306da2010-11-16 20:11:40 +0000136 return wm8994_access_masks[reg].readable != 0;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000137}
138
139static int wm8994_volatile(unsigned int reg)
140{
Mark Brownca9aef52010-11-26 17:23:41 +0000141 if (reg >= WM8994_CACHE_SIZE)
Mark Brown9e6e96a2010-01-29 17:47:12 +0000142 return 1;
143
144 switch (reg) {
145 case WM8994_SOFTWARE_RESET:
146 case WM8994_CHIP_REVISION:
147 case WM8994_DC_SERVO_1:
148 case WM8994_DC_SERVO_READBACK:
149 case WM8994_RATE_STATUS:
150 case WM8994_LDO_1:
151 case WM8994_LDO_2:
Mark Brownd6addcc2010-11-26 15:21:08 +0000152 case WM8958_DSP2_EXECCONTROL:
Mark Brown821edd22010-11-26 15:21:09 +0000153 case WM8958_MIC_DETECT_3:
Mark Brown9e6e96a2010-01-29 17:47:12 +0000154 return 1;
155 default:
156 return 0;
157 }
158}
159
160static int wm8994_write(struct snd_soc_codec *codec, unsigned int reg,
161 unsigned int value)
162{
Mark Brownca9aef52010-11-26 17:23:41 +0000163 int ret;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000164
165 BUG_ON(reg > WM8994_MAX_REGISTER);
166
Mark Brownca9aef52010-11-26 17:23:41 +0000167 if (!wm8994_volatile(reg)) {
168 ret = snd_soc_cache_write(codec, reg, value);
169 if (ret != 0)
170 dev_err(codec->dev, "Cache write to %x failed: %d\n",
171 reg, ret);
172 }
Mark Brown9e6e96a2010-01-29 17:47:12 +0000173
174 return wm8994_reg_write(codec->control_data, reg, value);
175}
176
177static unsigned int wm8994_read(struct snd_soc_codec *codec,
178 unsigned int reg)
179{
Mark Brownca9aef52010-11-26 17:23:41 +0000180 unsigned int val;
181 int ret;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000182
183 BUG_ON(reg > WM8994_MAX_REGISTER);
184
Mark Brownca9aef52010-11-26 17:23:41 +0000185 if (!wm8994_volatile(reg) && wm8994_readable(reg) &&
186 reg < codec->driver->reg_cache_size) {
187 ret = snd_soc_cache_read(codec, reg, &val);
188 if (ret >= 0)
189 return val;
190 else
191 dev_err(codec->dev, "Cache read from %x failed: %d\n",
192 reg, ret);
193 }
194
195 return wm8994_reg_read(codec->control_data, reg);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000196}
197
198static int configure_aif_clock(struct snd_soc_codec *codec, int aif)
199{
Mark Brownb2c812e2010-04-14 15:35:19 +0900200 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000201 int rate;
202 int reg1 = 0;
203 int offset;
204
205 if (aif)
206 offset = 4;
207 else
208 offset = 0;
209
210 switch (wm8994->sysclk[aif]) {
211 case WM8994_SYSCLK_MCLK1:
212 rate = wm8994->mclk[0];
213 break;
214
215 case WM8994_SYSCLK_MCLK2:
216 reg1 |= 0x8;
217 rate = wm8994->mclk[1];
218 break;
219
220 case WM8994_SYSCLK_FLL1:
221 reg1 |= 0x10;
222 rate = wm8994->fll[0].out;
223 break;
224
225 case WM8994_SYSCLK_FLL2:
226 reg1 |= 0x18;
227 rate = wm8994->fll[1].out;
228 break;
229
230 default:
231 return -EINVAL;
232 }
233
234 if (rate >= 13500000) {
235 rate /= 2;
236 reg1 |= WM8994_AIF1CLK_DIV;
237
238 dev_dbg(codec->dev, "Dividing AIF%d clock to %dHz\n",
239 aif + 1, rate);
240 }
Mark Brown5e5e2be2010-04-25 12:20:30 +0100241
242 if (rate && rate < 3000000)
243 dev_warn(codec->dev, "AIF%dCLK is %dHz, should be >=3MHz for optimal performance\n",
244 aif + 1, rate);
245
Mark Brown9e6e96a2010-01-29 17:47:12 +0000246 wm8994->aifclk[aif] = rate;
247
248 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1 + offset,
249 WM8994_AIF1CLK_SRC_MASK | WM8994_AIF1CLK_DIV,
250 reg1);
251
252 return 0;
253}
254
255static int configure_clock(struct snd_soc_codec *codec)
256{
Mark Brownb2c812e2010-04-14 15:35:19 +0900257 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000258 int old, new;
259
260 /* Bring up the AIF clocks first */
261 configure_aif_clock(codec, 0);
262 configure_aif_clock(codec, 1);
263
264 /* Then switch CLK_SYS over to the higher of them; a change
265 * can only happen as a result of a clocking change which can
266 * only be made outside of DAPM so we can safely redo the
267 * clocking.
268 */
269
270 /* If they're equal it doesn't matter which is used */
271 if (wm8994->aifclk[0] == wm8994->aifclk[1])
272 return 0;
273
274 if (wm8994->aifclk[0] < wm8994->aifclk[1])
275 new = WM8994_SYSCLK_SRC;
276 else
277 new = 0;
278
279 old = snd_soc_read(codec, WM8994_CLOCKING_1) & WM8994_SYSCLK_SRC;
280
281 /* If there's no change then we're done. */
282 if (old == new)
283 return 0;
284
285 snd_soc_update_bits(codec, WM8994_CLOCKING_1, WM8994_SYSCLK_SRC, new);
286
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200287 snd_soc_dapm_sync(&codec->dapm);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000288
289 return 0;
290}
291
292static int check_clk_sys(struct snd_soc_dapm_widget *source,
293 struct snd_soc_dapm_widget *sink)
294{
295 int reg = snd_soc_read(source->codec, WM8994_CLOCKING_1);
296 const char *clk;
297
298 /* Check what we're currently using for CLK_SYS */
299 if (reg & WM8994_SYSCLK_SRC)
300 clk = "AIF2CLK";
301 else
302 clk = "AIF1CLK";
303
304 return strcmp(source->name, clk) == 0;
305}
306
307static const char *sidetone_hpf_text[] = {
308 "2.7kHz", "1.35kHz", "675Hz", "370Hz", "180Hz", "90Hz", "45Hz"
309};
310
311static const struct soc_enum sidetone_hpf =
312 SOC_ENUM_SINGLE(WM8994_SIDETONE, 7, 7, sidetone_hpf_text);
313
Uk Kim146fd572010-12-07 13:58:40 +0000314static const char *adc_hpf_text[] = {
315 "HiFi", "Voice 1", "Voice 2", "Voice 3"
316};
317
318static const struct soc_enum aif1adc1_hpf =
319 SOC_ENUM_SINGLE(WM8994_AIF1_ADC1_FILTERS, 13, 4, adc_hpf_text);
320
321static const struct soc_enum aif1adc2_hpf =
322 SOC_ENUM_SINGLE(WM8994_AIF1_ADC2_FILTERS, 13, 4, adc_hpf_text);
323
324static const struct soc_enum aif2adc_hpf =
325 SOC_ENUM_SINGLE(WM8994_AIF2_ADC_FILTERS, 13, 4, adc_hpf_text);
326
Mark Brown9e6e96a2010-01-29 17:47:12 +0000327static const DECLARE_TLV_DB_SCALE(aif_tlv, 0, 600, 0);
328static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
329static const DECLARE_TLV_DB_SCALE(st_tlv, -3600, 300, 0);
330static const DECLARE_TLV_DB_SCALE(wm8994_3d_tlv, -1600, 183, 0);
331static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
332
333#define WM8994_DRC_SWITCH(xname, reg, shift) \
334{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
335 .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,\
336 .put = wm8994_put_drc_sw, \
337 .private_value = SOC_SINGLE_VALUE(reg, shift, 1, 0) }
338
339static int wm8994_put_drc_sw(struct snd_kcontrol *kcontrol,
340 struct snd_ctl_elem_value *ucontrol)
341{
342 struct soc_mixer_control *mc =
343 (struct soc_mixer_control *)kcontrol->private_value;
344 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
345 int mask, ret;
346
347 /* Can't enable both ADC and DAC paths simultaneously */
348 if (mc->shift == WM8994_AIF1DAC1_DRC_ENA_SHIFT)
349 mask = WM8994_AIF1ADC1L_DRC_ENA_MASK |
350 WM8994_AIF1ADC1R_DRC_ENA_MASK;
351 else
352 mask = WM8994_AIF1DAC1_DRC_ENA_MASK;
353
354 ret = snd_soc_read(codec, mc->reg);
355 if (ret < 0)
356 return ret;
357 if (ret & mask)
358 return -EINVAL;
359
360 return snd_soc_put_volsw(kcontrol, ucontrol);
361}
362
Mark Brown9e6e96a2010-01-29 17:47:12 +0000363static void wm8994_set_drc(struct snd_soc_codec *codec, int drc)
364{
Mark Brownb2c812e2010-04-14 15:35:19 +0900365 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000366 struct wm8994_pdata *pdata = wm8994->pdata;
367 int base = wm8994_drc_base[drc];
368 int cfg = wm8994->drc_cfg[drc];
369 int save, i;
370
371 /* Save any enables; the configuration should clear them. */
372 save = snd_soc_read(codec, base);
373 save &= WM8994_AIF1DAC1_DRC_ENA | WM8994_AIF1ADC1L_DRC_ENA |
374 WM8994_AIF1ADC1R_DRC_ENA;
375
376 for (i = 0; i < WM8994_DRC_REGS; i++)
377 snd_soc_update_bits(codec, base + i, 0xffff,
378 pdata->drc_cfgs[cfg].regs[i]);
379
380 snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_DRC_ENA |
381 WM8994_AIF1ADC1L_DRC_ENA |
382 WM8994_AIF1ADC1R_DRC_ENA, save);
383}
384
385/* Icky as hell but saves code duplication */
386static int wm8994_get_drc(const char *name)
387{
388 if (strcmp(name, "AIF1DRC1 Mode") == 0)
389 return 0;
390 if (strcmp(name, "AIF1DRC2 Mode") == 0)
391 return 1;
392 if (strcmp(name, "AIF2DRC Mode") == 0)
393 return 2;
394 return -EINVAL;
395}
396
397static int wm8994_put_drc_enum(struct snd_kcontrol *kcontrol,
398 struct snd_ctl_elem_value *ucontrol)
399{
400 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000401 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000402 struct wm8994_pdata *pdata = wm8994->pdata;
403 int drc = wm8994_get_drc(kcontrol->id.name);
404 int value = ucontrol->value.integer.value[0];
405
406 if (drc < 0)
407 return drc;
408
409 if (value >= pdata->num_drc_cfgs)
410 return -EINVAL;
411
412 wm8994->drc_cfg[drc] = value;
413
414 wm8994_set_drc(codec, drc);
415
416 return 0;
417}
418
419static int wm8994_get_drc_enum(struct snd_kcontrol *kcontrol,
420 struct snd_ctl_elem_value *ucontrol)
421{
422 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +0900423 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000424 int drc = wm8994_get_drc(kcontrol->id.name);
425
426 ucontrol->value.enumerated.item[0] = wm8994->drc_cfg[drc];
427
428 return 0;
429}
430
431static void wm8994_set_retune_mobile(struct snd_soc_codec *codec, int block)
432{
Mark Brownb2c812e2010-04-14 15:35:19 +0900433 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000434 struct wm8994_pdata *pdata = wm8994->pdata;
435 int base = wm8994_retune_mobile_base[block];
436 int iface, best, best_val, save, i, cfg;
437
438 if (!pdata || !wm8994->num_retune_mobile_texts)
439 return;
440
441 switch (block) {
442 case 0:
443 case 1:
444 iface = 0;
445 break;
446 case 2:
447 iface = 1;
448 break;
449 default:
450 return;
451 }
452
453 /* Find the version of the currently selected configuration
454 * with the nearest sample rate. */
455 cfg = wm8994->retune_mobile_cfg[block];
456 best = 0;
457 best_val = INT_MAX;
458 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
459 if (strcmp(pdata->retune_mobile_cfgs[i].name,
460 wm8994->retune_mobile_texts[cfg]) == 0 &&
461 abs(pdata->retune_mobile_cfgs[i].rate
462 - wm8994->dac_rates[iface]) < best_val) {
463 best = i;
464 best_val = abs(pdata->retune_mobile_cfgs[i].rate
465 - wm8994->dac_rates[iface]);
466 }
467 }
468
469 dev_dbg(codec->dev, "ReTune Mobile %d %s/%dHz for %dHz sample rate\n",
470 block,
471 pdata->retune_mobile_cfgs[best].name,
472 pdata->retune_mobile_cfgs[best].rate,
473 wm8994->dac_rates[iface]);
474
475 /* The EQ will be disabled while reconfiguring it, remember the
476 * current configuration.
477 */
478 save = snd_soc_read(codec, base);
479 save &= WM8994_AIF1DAC1_EQ_ENA;
480
481 for (i = 0; i < WM8994_EQ_REGS; i++)
482 snd_soc_update_bits(codec, base + i, 0xffff,
483 pdata->retune_mobile_cfgs[best].regs[i]);
484
485 snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_EQ_ENA, save);
486}
487
488/* Icky as hell but saves code duplication */
489static int wm8994_get_retune_mobile_block(const char *name)
490{
491 if (strcmp(name, "AIF1.1 EQ Mode") == 0)
492 return 0;
493 if (strcmp(name, "AIF1.2 EQ Mode") == 0)
494 return 1;
495 if (strcmp(name, "AIF2 EQ Mode") == 0)
496 return 2;
497 return -EINVAL;
498}
499
500static int wm8994_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
501 struct snd_ctl_elem_value *ucontrol)
502{
503 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000504 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000505 struct wm8994_pdata *pdata = wm8994->pdata;
506 int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
507 int value = ucontrol->value.integer.value[0];
508
509 if (block < 0)
510 return block;
511
512 if (value >= pdata->num_retune_mobile_cfgs)
513 return -EINVAL;
514
515 wm8994->retune_mobile_cfg[block] = value;
516
517 wm8994_set_retune_mobile(codec, block);
518
519 return 0;
520}
521
522static int wm8994_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
523 struct snd_ctl_elem_value *ucontrol)
524{
525 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000526 struct wm8994_priv *wm8994 =snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000527 int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
528
529 ucontrol->value.enumerated.item[0] = wm8994->retune_mobile_cfg[block];
530
531 return 0;
532}
533
Mark Brown96b101e2010-11-18 15:49:38 +0000534static const char *aif_chan_src_text[] = {
Mark Brownf5548852010-08-31 19:39:48 +0100535 "Left", "Right"
536};
537
Mark Brown96b101e2010-11-18 15:49:38 +0000538static const struct soc_enum aif1adcl_src =
539 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_1, 15, 2, aif_chan_src_text);
540
541static const struct soc_enum aif1adcr_src =
542 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_1, 14, 2, aif_chan_src_text);
543
544static const struct soc_enum aif2adcl_src =
545 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_1, 15, 2, aif_chan_src_text);
546
547static const struct soc_enum aif2adcr_src =
548 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_1, 14, 2, aif_chan_src_text);
549
Mark Brownf5548852010-08-31 19:39:48 +0100550static const struct soc_enum aif1dacl_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000551 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_2, 15, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100552
553static const struct soc_enum aif1dacr_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000554 SOC_ENUM_SINGLE(WM8994_AIF1_CONTROL_2, 14, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100555
556static const struct soc_enum aif2dacl_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000557 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_2, 15, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100558
559static const struct soc_enum aif2dacr_src =
Mark Brown96b101e2010-11-18 15:49:38 +0000560 SOC_ENUM_SINGLE(WM8994_AIF2_CONTROL_2, 14, 2, aif_chan_src_text);
Mark Brownf5548852010-08-31 19:39:48 +0100561
Mark Brown154b26a2010-12-09 12:07:44 +0000562static const char *osr_text[] = {
563 "Low Power", "High Performance",
564};
565
566static const struct soc_enum dac_osr =
567 SOC_ENUM_SINGLE(WM8994_OVERSAMPLING, 0, 2, osr_text);
568
569static const struct soc_enum adc_osr =
570 SOC_ENUM_SINGLE(WM8994_OVERSAMPLING, 1, 2, osr_text);
571
Mark Brownd6addcc2010-11-26 15:21:08 +0000572static void wm8958_mbc_apply(struct snd_soc_codec *codec, int mbc, int start)
573{
574 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown131d8102010-11-30 17:03:39 +0000575 struct wm8994_pdata *pdata = wm8994->pdata;
Mark Brownd6addcc2010-11-26 15:21:08 +0000576 int pwr_reg = snd_soc_read(codec, WM8994_POWER_MANAGEMENT_5);
Mark Brown131d8102010-11-30 17:03:39 +0000577 int ena, reg, aif, i;
Mark Brownd6addcc2010-11-26 15:21:08 +0000578
579 switch (mbc) {
580 case 0:
581 pwr_reg &= (WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC1R_ENA);
582 aif = 0;
583 break;
584 case 1:
585 pwr_reg &= (WM8994_AIF1DAC2L_ENA | WM8994_AIF1DAC2R_ENA);
586 aif = 0;
587 break;
588 case 2:
589 pwr_reg &= (WM8994_AIF2DACL_ENA | WM8994_AIF2DACR_ENA);
590 aif = 1;
591 break;
592 default:
593 BUG();
594 return;
595 }
596
597 /* We can only enable the MBC if the AIF is enabled and we
598 * want it to be enabled. */
599 ena = pwr_reg && wm8994->mbc_ena[mbc];
600
601 reg = snd_soc_read(codec, WM8958_DSP2_PROGRAM);
602
603 dev_dbg(codec->dev, "MBC %d startup: %d, power: %x, DSP: %x\n",
604 mbc, start, pwr_reg, reg);
605
606 if (start && ena) {
607 /* If the DSP is already running then noop */
608 if (reg & WM8958_DSP2_ENA)
609 return;
610
611 /* Switch the clock over to the appropriate AIF */
612 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
613 WM8958_DSP2CLK_SRC | WM8958_DSP2CLK_ENA,
614 aif << WM8958_DSP2CLK_SRC_SHIFT |
615 WM8958_DSP2CLK_ENA);
616
617 snd_soc_update_bits(codec, WM8958_DSP2_PROGRAM,
618 WM8958_DSP2_ENA, WM8958_DSP2_ENA);
619
Mark Brown131d8102010-11-30 17:03:39 +0000620 /* If we've got user supplied MBC settings use them */
621 if (pdata && pdata->num_mbc_cfgs) {
622 struct wm8958_mbc_cfg *cfg
623 = &pdata->mbc_cfgs[wm8994->mbc_cfg];
624
625 for (i = 0; i < ARRAY_SIZE(cfg->coeff_regs); i++)
626 snd_soc_write(codec, i + WM8958_MBC_BAND_1_K_1,
627 cfg->coeff_regs[i]);
628
629 for (i = 0; i < ARRAY_SIZE(cfg->cutoff_regs); i++)
630 snd_soc_write(codec,
631 i + WM8958_MBC_BAND_2_LOWER_CUTOFF_C1_1,
632 cfg->cutoff_regs[i]);
633 }
Mark Brownd6addcc2010-11-26 15:21:08 +0000634
635 /* Run the DSP */
636 snd_soc_write(codec, WM8958_DSP2_EXECCONTROL,
637 WM8958_DSP2_RUNR);
638
639 /* And we're off! */
640 snd_soc_update_bits(codec, WM8958_DSP2_CONFIG,
641 WM8958_MBC_ENA | WM8958_MBC_SEL_MASK,
642 mbc << WM8958_MBC_SEL_SHIFT |
643 WM8958_MBC_ENA);
644 } else {
645 /* If the DSP is already stopped then noop */
646 if (!(reg & WM8958_DSP2_ENA))
647 return;
648
649 snd_soc_update_bits(codec, WM8958_DSP2_CONFIG,
650 WM8958_MBC_ENA, 0);
651 snd_soc_update_bits(codec, WM8958_DSP2_PROGRAM,
652 WM8958_DSP2_ENA, 0);
653 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
654 WM8958_DSP2CLK_ENA, 0);
655 }
656}
657
658static int wm8958_aif_ev(struct snd_soc_dapm_widget *w,
659 struct snd_kcontrol *kcontrol, int event)
660{
661 struct snd_soc_codec *codec = w->codec;
662 int mbc;
663
664 switch (w->shift) {
665 case 13:
666 case 12:
667 mbc = 2;
668 break;
669 case 11:
670 case 10:
671 mbc = 1;
672 break;
673 case 9:
674 case 8:
675 mbc = 0;
676 break;
677 default:
678 BUG();
679 return -EINVAL;
680 }
681
682 switch (event) {
683 case SND_SOC_DAPM_POST_PMU:
684 wm8958_mbc_apply(codec, mbc, 1);
685 break;
686 case SND_SOC_DAPM_POST_PMD:
687 wm8958_mbc_apply(codec, mbc, 0);
688 break;
689 }
690
691 return 0;
692}
693
Mark Brown131d8102010-11-30 17:03:39 +0000694static int wm8958_put_mbc_enum(struct snd_kcontrol *kcontrol,
695 struct snd_ctl_elem_value *ucontrol)
696{
697 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
698 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
699 struct wm8994_pdata *pdata = wm8994->pdata;
700 int value = ucontrol->value.integer.value[0];
701 int reg;
702
703 /* Don't allow on the fly reconfiguration */
704 reg = snd_soc_read(codec, WM8994_CLOCKING_1);
705 if (reg < 0 || reg & WM8958_DSP2CLK_ENA)
706 return -EBUSY;
707
708 if (value >= pdata->num_mbc_cfgs)
709 return -EINVAL;
710
711 wm8994->mbc_cfg = value;
712
713 return 0;
714}
715
716static int wm8958_get_mbc_enum(struct snd_kcontrol *kcontrol,
717 struct snd_ctl_elem_value *ucontrol)
718{
719 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
720 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
721
722 ucontrol->value.enumerated.item[0] = wm8994->mbc_cfg;
723
724 return 0;
725}
726
Mark Brownd6addcc2010-11-26 15:21:08 +0000727static int wm8958_mbc_info(struct snd_kcontrol *kcontrol,
728 struct snd_ctl_elem_info *uinfo)
729{
730 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
731 uinfo->count = 1;
732 uinfo->value.integer.min = 0;
733 uinfo->value.integer.max = 1;
734 return 0;
735}
736
737static int wm8958_mbc_get(struct snd_kcontrol *kcontrol,
738 struct snd_ctl_elem_value *ucontrol)
739{
740 int mbc = kcontrol->private_value;
741 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
742 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
743
744 ucontrol->value.integer.value[0] = wm8994->mbc_ena[mbc];
745
746 return 0;
747}
748
749static int wm8958_mbc_put(struct snd_kcontrol *kcontrol,
750 struct snd_ctl_elem_value *ucontrol)
751{
752 int mbc = kcontrol->private_value;
753 int i;
754 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
755 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
756
757 if (ucontrol->value.integer.value[0] > 1)
758 return -EINVAL;
759
760 for (i = 0; i < ARRAY_SIZE(wm8994->mbc_ena); i++) {
761 if (mbc != i && wm8994->mbc_ena[i]) {
762 dev_dbg(codec->dev, "MBC %d active already\n", mbc);
763 return -EBUSY;
764 }
765 }
766
767 wm8994->mbc_ena[mbc] = ucontrol->value.integer.value[0];
768
769 wm8958_mbc_apply(codec, mbc, wm8994->mbc_ena[mbc]);
770
771 return 0;
772}
773
774#define WM8958_MBC_SWITCH(xname, xval) {\
775 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
776 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,\
777 .info = wm8958_mbc_info, \
778 .get = wm8958_mbc_get, .put = wm8958_mbc_put, \
779 .private_value = xval }
780
Mark Brown9e6e96a2010-01-29 17:47:12 +0000781static const struct snd_kcontrol_new wm8994_snd_controls[] = {
782SOC_DOUBLE_R_TLV("AIF1ADC1 Volume", WM8994_AIF1_ADC1_LEFT_VOLUME,
783 WM8994_AIF1_ADC1_RIGHT_VOLUME,
784 1, 119, 0, digital_tlv),
785SOC_DOUBLE_R_TLV("AIF1ADC2 Volume", WM8994_AIF1_ADC2_LEFT_VOLUME,
786 WM8994_AIF1_ADC2_RIGHT_VOLUME,
787 1, 119, 0, digital_tlv),
788SOC_DOUBLE_R_TLV("AIF2ADC Volume", WM8994_AIF2_ADC_LEFT_VOLUME,
789 WM8994_AIF2_ADC_RIGHT_VOLUME,
790 1, 119, 0, digital_tlv),
791
Mark Brown96b101e2010-11-18 15:49:38 +0000792SOC_ENUM("AIF1ADCL Source", aif1adcl_src),
793SOC_ENUM("AIF1ADCR Source", aif1adcr_src),
Mark Brown49db7e72010-12-08 13:49:43 +0000794SOC_ENUM("AIF2ADCL Source", aif2adcl_src),
795SOC_ENUM("AIF2ADCR Source", aif2adcr_src),
Mark Brown96b101e2010-11-18 15:49:38 +0000796
Mark Brownf5548852010-08-31 19:39:48 +0100797SOC_ENUM("AIF1DACL Source", aif1dacl_src),
798SOC_ENUM("AIF1DACR Source", aif1dacr_src),
Mark Brown49db7e72010-12-08 13:49:43 +0000799SOC_ENUM("AIF2DACL Source", aif2dacl_src),
800SOC_ENUM("AIF2DACR Source", aif2dacr_src),
Mark Brownf5548852010-08-31 19:39:48 +0100801
Mark Brown9e6e96a2010-01-29 17:47:12 +0000802SOC_DOUBLE_R_TLV("AIF1DAC1 Volume", WM8994_AIF1_DAC1_LEFT_VOLUME,
803 WM8994_AIF1_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
804SOC_DOUBLE_R_TLV("AIF1DAC2 Volume", WM8994_AIF1_DAC2_LEFT_VOLUME,
805 WM8994_AIF1_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
806SOC_DOUBLE_R_TLV("AIF2DAC Volume", WM8994_AIF2_DAC_LEFT_VOLUME,
807 WM8994_AIF2_DAC_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
808
809SOC_SINGLE_TLV("AIF1 Boost Volume", WM8994_AIF1_CONTROL_2, 10, 3, 0, aif_tlv),
810SOC_SINGLE_TLV("AIF2 Boost Volume", WM8994_AIF2_CONTROL_2, 10, 3, 0, aif_tlv),
811
812SOC_SINGLE("AIF1DAC1 EQ Switch", WM8994_AIF1_DAC1_EQ_GAINS_1, 0, 1, 0),
813SOC_SINGLE("AIF1DAC2 EQ Switch", WM8994_AIF1_DAC2_EQ_GAINS_1, 0, 1, 0),
814SOC_SINGLE("AIF2 EQ Switch", WM8994_AIF2_EQ_GAINS_1, 0, 1, 0),
815
816WM8994_DRC_SWITCH("AIF1DAC1 DRC Switch", WM8994_AIF1_DRC1_1, 2),
817WM8994_DRC_SWITCH("AIF1ADC1L DRC Switch", WM8994_AIF1_DRC1_1, 1),
818WM8994_DRC_SWITCH("AIF1ADC1R DRC Switch", WM8994_AIF1_DRC1_1, 0),
819
820WM8994_DRC_SWITCH("AIF1DAC2 DRC Switch", WM8994_AIF1_DRC2_1, 2),
821WM8994_DRC_SWITCH("AIF1ADC2L DRC Switch", WM8994_AIF1_DRC2_1, 1),
822WM8994_DRC_SWITCH("AIF1ADC2R DRC Switch", WM8994_AIF1_DRC2_1, 0),
823
824WM8994_DRC_SWITCH("AIF2DAC DRC Switch", WM8994_AIF2_DRC_1, 2),
825WM8994_DRC_SWITCH("AIF2ADCL DRC Switch", WM8994_AIF2_DRC_1, 1),
826WM8994_DRC_SWITCH("AIF2ADCR DRC Switch", WM8994_AIF2_DRC_1, 0),
827
828SOC_SINGLE_TLV("DAC1 Right Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
829 5, 12, 0, st_tlv),
830SOC_SINGLE_TLV("DAC1 Left Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
831 0, 12, 0, st_tlv),
832SOC_SINGLE_TLV("DAC2 Right Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
833 5, 12, 0, st_tlv),
834SOC_SINGLE_TLV("DAC2 Left Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
835 0, 12, 0, st_tlv),
836SOC_ENUM("Sidetone HPF Mux", sidetone_hpf),
837SOC_SINGLE("Sidetone HPF Switch", WM8994_SIDETONE, 6, 1, 0),
838
Uk Kim146fd572010-12-07 13:58:40 +0000839SOC_ENUM("AIF1ADC1 HPF Mode", aif1adc1_hpf),
840SOC_DOUBLE("AIF1ADC1 HPF Switch", WM8994_AIF1_ADC1_FILTERS, 12, 11, 1, 0),
841
842SOC_ENUM("AIF1ADC2 HPF Mode", aif1adc2_hpf),
843SOC_DOUBLE("AIF1ADC2 HPF Switch", WM8994_AIF1_ADC2_FILTERS, 12, 11, 1, 0),
844
845SOC_ENUM("AIF2ADC HPF Mode", aif2adc_hpf),
846SOC_DOUBLE("AIF2ADC HPF Switch", WM8994_AIF2_ADC_FILTERS, 12, 11, 1, 0),
847
Mark Brown154b26a2010-12-09 12:07:44 +0000848SOC_ENUM("ADC OSR", adc_osr),
849SOC_ENUM("DAC OSR", dac_osr),
850
Mark Brown9e6e96a2010-01-29 17:47:12 +0000851SOC_DOUBLE_R_TLV("DAC1 Volume", WM8994_DAC1_LEFT_VOLUME,
852 WM8994_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
853SOC_DOUBLE_R("DAC1 Switch", WM8994_DAC1_LEFT_VOLUME,
854 WM8994_DAC1_RIGHT_VOLUME, 9, 1, 1),
855
856SOC_DOUBLE_R_TLV("DAC2 Volume", WM8994_DAC2_LEFT_VOLUME,
857 WM8994_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
858SOC_DOUBLE_R("DAC2 Switch", WM8994_DAC2_LEFT_VOLUME,
859 WM8994_DAC2_RIGHT_VOLUME, 9, 1, 1),
860
861SOC_SINGLE_TLV("SPKL DAC2 Volume", WM8994_SPKMIXL_ATTENUATION,
862 6, 1, 1, wm_hubs_spkmix_tlv),
863SOC_SINGLE_TLV("SPKL DAC1 Volume", WM8994_SPKMIXL_ATTENUATION,
864 2, 1, 1, wm_hubs_spkmix_tlv),
865
866SOC_SINGLE_TLV("SPKR DAC2 Volume", WM8994_SPKMIXR_ATTENUATION,
867 6, 1, 1, wm_hubs_spkmix_tlv),
868SOC_SINGLE_TLV("SPKR DAC1 Volume", WM8994_SPKMIXR_ATTENUATION,
869 2, 1, 1, wm_hubs_spkmix_tlv),
870
871SOC_SINGLE_TLV("AIF1DAC1 3D Stereo Volume", WM8994_AIF1_DAC1_FILTERS_2,
872 10, 15, 0, wm8994_3d_tlv),
Mark Brown458350b2010-12-20 14:35:09 +0000873SOC_SINGLE("AIF1DAC1 3D Stereo Switch", WM8994_AIF1_DAC1_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000874 8, 1, 0),
875SOC_SINGLE_TLV("AIF1DAC2 3D Stereo Volume", WM8994_AIF1_DAC2_FILTERS_2,
876 10, 15, 0, wm8994_3d_tlv),
877SOC_SINGLE("AIF1DAC2 3D Stereo Switch", WM8994_AIF1_DAC2_FILTERS_2,
878 8, 1, 0),
Mark Brown458350b2010-12-20 14:35:09 +0000879SOC_SINGLE_TLV("AIF2DAC 3D Stereo Volume", WM8994_AIF2_DAC_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000880 10, 15, 0, wm8994_3d_tlv),
Mark Brown458350b2010-12-20 14:35:09 +0000881SOC_SINGLE("AIF2DAC 3D Stereo Switch", WM8994_AIF2_DAC_FILTERS_2,
Mark Brown9e6e96a2010-01-29 17:47:12 +0000882 8, 1, 0),
883};
884
885static const struct snd_kcontrol_new wm8994_eq_controls[] = {
886SOC_SINGLE_TLV("AIF1DAC1 EQ1 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 11, 31, 0,
887 eq_tlv),
888SOC_SINGLE_TLV("AIF1DAC1 EQ2 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 6, 31, 0,
889 eq_tlv),
890SOC_SINGLE_TLV("AIF1DAC1 EQ3 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 1, 31, 0,
891 eq_tlv),
892SOC_SINGLE_TLV("AIF1DAC1 EQ4 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 11, 31, 0,
893 eq_tlv),
894SOC_SINGLE_TLV("AIF1DAC1 EQ5 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 6, 31, 0,
895 eq_tlv),
896
897SOC_SINGLE_TLV("AIF1DAC2 EQ1 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 11, 31, 0,
898 eq_tlv),
899SOC_SINGLE_TLV("AIF1DAC2 EQ2 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 6, 31, 0,
900 eq_tlv),
901SOC_SINGLE_TLV("AIF1DAC2 EQ3 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 1, 31, 0,
902 eq_tlv),
903SOC_SINGLE_TLV("AIF1DAC2 EQ4 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 11, 31, 0,
904 eq_tlv),
905SOC_SINGLE_TLV("AIF1DAC2 EQ5 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 6, 31, 0,
906 eq_tlv),
907
908SOC_SINGLE_TLV("AIF2 EQ1 Volume", WM8994_AIF2_EQ_GAINS_1, 11, 31, 0,
909 eq_tlv),
910SOC_SINGLE_TLV("AIF2 EQ2 Volume", WM8994_AIF2_EQ_GAINS_1, 6, 31, 0,
911 eq_tlv),
912SOC_SINGLE_TLV("AIF2 EQ3 Volume", WM8994_AIF2_EQ_GAINS_1, 1, 31, 0,
913 eq_tlv),
914SOC_SINGLE_TLV("AIF2 EQ4 Volume", WM8994_AIF2_EQ_GAINS_2, 11, 31, 0,
915 eq_tlv),
916SOC_SINGLE_TLV("AIF2 EQ5 Volume", WM8994_AIF2_EQ_GAINS_2, 6, 31, 0,
917 eq_tlv),
918};
919
Mark Brownc4431df2010-11-26 15:21:07 +0000920static const struct snd_kcontrol_new wm8958_snd_controls[] = {
921SOC_SINGLE_TLV("AIF3 Boost Volume", WM8958_AIF3_CONTROL_2, 10, 3, 0, aif_tlv),
Mark Brownd6addcc2010-11-26 15:21:08 +0000922WM8958_MBC_SWITCH("AIF1DAC1 MBC Switch", 0),
923WM8958_MBC_SWITCH("AIF1DAC2 MBC Switch", 1),
924WM8958_MBC_SWITCH("AIF2DAC MBC Switch", 2),
Mark Brownc4431df2010-11-26 15:21:07 +0000925};
926
Mark Brown9e6e96a2010-01-29 17:47:12 +0000927static int clk_sys_event(struct snd_soc_dapm_widget *w,
928 struct snd_kcontrol *kcontrol, int event)
929{
930 struct snd_soc_codec *codec = w->codec;
931
932 switch (event) {
933 case SND_SOC_DAPM_PRE_PMU:
934 return configure_clock(codec);
935
936 case SND_SOC_DAPM_POST_PMD:
937 configure_clock(codec);
938 break;
939 }
940
941 return 0;
942}
943
944static void wm8994_update_class_w(struct snd_soc_codec *codec)
945{
Mark Brownfec6dd82010-10-27 13:48:36 -0700946 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000947 int enable = 1;
948 int source = 0; /* GCC flow analysis can't track enable */
949 int reg, reg_r;
950
951 /* Only support direct DAC->headphone paths */
952 reg = snd_soc_read(codec, WM8994_OUTPUT_MIXER_1);
953 if (!(reg & WM8994_DAC1L_TO_HPOUT1L)) {
Mark Brownee839a22010-04-20 13:57:08 +0900954 dev_vdbg(codec->dev, "HPL connected to output mixer\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000955 enable = 0;
956 }
957
958 reg = snd_soc_read(codec, WM8994_OUTPUT_MIXER_2);
959 if (!(reg & WM8994_DAC1R_TO_HPOUT1R)) {
Mark Brownee839a22010-04-20 13:57:08 +0900960 dev_vdbg(codec->dev, "HPR connected to output mixer\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000961 enable = 0;
962 }
963
964 /* We also need the same setting for L/R and only one path */
965 reg = snd_soc_read(codec, WM8994_DAC1_LEFT_MIXER_ROUTING);
966 switch (reg) {
967 case WM8994_AIF2DACL_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +0900968 dev_vdbg(codec->dev, "Class W source AIF2DAC\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000969 source = 2 << WM8994_CP_DYN_SRC_SEL_SHIFT;
970 break;
971 case WM8994_AIF1DAC2L_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +0900972 dev_vdbg(codec->dev, "Class W source AIF1DAC2\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000973 source = 1 << WM8994_CP_DYN_SRC_SEL_SHIFT;
974 break;
975 case WM8994_AIF1DAC1L_TO_DAC1L:
Mark Brownee839a22010-04-20 13:57:08 +0900976 dev_vdbg(codec->dev, "Class W source AIF1DAC1\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000977 source = 0 << WM8994_CP_DYN_SRC_SEL_SHIFT;
978 break;
979 default:
Mark Brownee839a22010-04-20 13:57:08 +0900980 dev_vdbg(codec->dev, "DAC mixer setting: %x\n", reg);
Mark Brown9e6e96a2010-01-29 17:47:12 +0000981 enable = 0;
982 break;
983 }
984
985 reg_r = snd_soc_read(codec, WM8994_DAC1_RIGHT_MIXER_ROUTING);
986 if (reg_r != reg) {
Mark Brownee839a22010-04-20 13:57:08 +0900987 dev_vdbg(codec->dev, "Left and right DAC mixers different\n");
Mark Brown9e6e96a2010-01-29 17:47:12 +0000988 enable = 0;
989 }
990
991 if (enable) {
992 dev_dbg(codec->dev, "Class W enabled\n");
993 snd_soc_update_bits(codec, WM8994_CLASS_W_1,
994 WM8994_CP_DYN_PWR |
995 WM8994_CP_DYN_SRC_SEL_MASK,
996 source | WM8994_CP_DYN_PWR);
Mark Brownfec6dd82010-10-27 13:48:36 -0700997 wm8994->hubs.class_w = true;
Mark Brown9e6e96a2010-01-29 17:47:12 +0000998
999 } else {
1000 dev_dbg(codec->dev, "Class W disabled\n");
1001 snd_soc_update_bits(codec, WM8994_CLASS_W_1,
1002 WM8994_CP_DYN_PWR, 0);
Mark Brownfec6dd82010-10-27 13:48:36 -07001003 wm8994->hubs.class_w = false;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001004 }
1005}
1006
1007static const char *hp_mux_text[] = {
1008 "Mixer",
1009 "DAC",
1010};
1011
1012#define WM8994_HP_ENUM(xname, xenum) \
1013{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1014 .info = snd_soc_info_enum_double, \
1015 .get = snd_soc_dapm_get_enum_double, \
1016 .put = wm8994_put_hp_enum, \
1017 .private_value = (unsigned long)&xenum }
1018
1019static int wm8994_put_hp_enum(struct snd_kcontrol *kcontrol,
1020 struct snd_ctl_elem_value *ucontrol)
1021{
1022 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
1023 struct snd_soc_codec *codec = w->codec;
1024 int ret;
1025
1026 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
1027
1028 wm8994_update_class_w(codec);
1029
1030 return ret;
1031}
1032
1033static const struct soc_enum hpl_enum =
1034 SOC_ENUM_SINGLE(WM8994_OUTPUT_MIXER_1, 8, 2, hp_mux_text);
1035
1036static const struct snd_kcontrol_new hpl_mux =
1037 WM8994_HP_ENUM("Left Headphone Mux", hpl_enum);
1038
1039static const struct soc_enum hpr_enum =
1040 SOC_ENUM_SINGLE(WM8994_OUTPUT_MIXER_2, 8, 2, hp_mux_text);
1041
1042static const struct snd_kcontrol_new hpr_mux =
1043 WM8994_HP_ENUM("Right Headphone Mux", hpr_enum);
1044
1045static const char *adc_mux_text[] = {
1046 "ADC",
1047 "DMIC",
1048};
1049
1050static const struct soc_enum adc_enum =
1051 SOC_ENUM_SINGLE(0, 0, 2, adc_mux_text);
1052
1053static const struct snd_kcontrol_new adcl_mux =
1054 SOC_DAPM_ENUM_VIRT("ADCL Mux", adc_enum);
1055
1056static const struct snd_kcontrol_new adcr_mux =
1057 SOC_DAPM_ENUM_VIRT("ADCR Mux", adc_enum);
1058
1059static const struct snd_kcontrol_new left_speaker_mixer[] = {
1060SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 9, 1, 0),
1061SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 7, 1, 0),
1062SOC_DAPM_SINGLE("IN1LP Switch", WM8994_SPEAKER_MIXER, 5, 1, 0),
1063SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 3, 1, 0),
1064SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 1, 1, 0),
1065};
1066
1067static const struct snd_kcontrol_new right_speaker_mixer[] = {
1068SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 8, 1, 0),
1069SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 6, 1, 0),
1070SOC_DAPM_SINGLE("IN1RP Switch", WM8994_SPEAKER_MIXER, 4, 1, 0),
1071SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 2, 1, 0),
1072SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 0, 1, 0),
1073};
1074
1075/* Debugging; dump chip status after DAPM transitions */
1076static int post_ev(struct snd_soc_dapm_widget *w,
1077 struct snd_kcontrol *kcontrol, int event)
1078{
1079 struct snd_soc_codec *codec = w->codec;
1080 dev_dbg(codec->dev, "SRC status: %x\n",
1081 snd_soc_read(codec,
1082 WM8994_RATE_STATUS));
1083 return 0;
1084}
1085
1086static const struct snd_kcontrol_new aif1adc1l_mix[] = {
1087SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1088 1, 1, 0),
1089SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
1090 0, 1, 0),
1091};
1092
1093static const struct snd_kcontrol_new aif1adc1r_mix[] = {
1094SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1095 1, 1, 0),
1096SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
1097 0, 1, 0),
1098};
1099
Mark Browna3257ba2010-07-19 14:02:34 +01001100static const struct snd_kcontrol_new aif1adc2l_mix[] = {
1101SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1102 1, 1, 0),
1103SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
1104 0, 1, 0),
1105};
1106
1107static const struct snd_kcontrol_new aif1adc2r_mix[] = {
1108SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1109 1, 1, 0),
1110SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
1111 0, 1, 0),
1112};
1113
Mark Brown9e6e96a2010-01-29 17:47:12 +00001114static const struct snd_kcontrol_new aif2dac2l_mix[] = {
1115SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1116 5, 1, 0),
1117SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1118 4, 1, 0),
1119SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1120 2, 1, 0),
1121SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1122 1, 1, 0),
1123SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
1124 0, 1, 0),
1125};
1126
1127static const struct snd_kcontrol_new aif2dac2r_mix[] = {
1128SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1129 5, 1, 0),
1130SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1131 4, 1, 0),
1132SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1133 2, 1, 0),
1134SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1135 1, 1, 0),
1136SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
1137 0, 1, 0),
1138};
1139
1140#define WM8994_CLASS_W_SWITCH(xname, reg, shift, max, invert) \
1141{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1142 .info = snd_soc_info_volsw, \
1143 .get = snd_soc_dapm_get_volsw, .put = wm8994_put_class_w, \
1144 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
1145
1146static int wm8994_put_class_w(struct snd_kcontrol *kcontrol,
1147 struct snd_ctl_elem_value *ucontrol)
1148{
1149 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
1150 struct snd_soc_codec *codec = w->codec;
1151 int ret;
1152
1153 ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
1154
1155 wm8994_update_class_w(codec);
1156
1157 return ret;
1158}
1159
1160static const struct snd_kcontrol_new dac1l_mix[] = {
1161WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1162 5, 1, 0),
1163WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1164 4, 1, 0),
1165WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1166 2, 1, 0),
1167WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1168 1, 1, 0),
1169WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
1170 0, 1, 0),
1171};
1172
1173static const struct snd_kcontrol_new dac1r_mix[] = {
1174WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1175 5, 1, 0),
1176WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1177 4, 1, 0),
1178WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1179 2, 1, 0),
1180WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1181 1, 1, 0),
1182WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
1183 0, 1, 0),
1184};
1185
1186static const char *sidetone_text[] = {
1187 "ADC/DMIC1", "DMIC2",
1188};
1189
1190static const struct soc_enum sidetone1_enum =
1191 SOC_ENUM_SINGLE(WM8994_SIDETONE, 0, 2, sidetone_text);
1192
1193static const struct snd_kcontrol_new sidetone1_mux =
1194 SOC_DAPM_ENUM("Left Sidetone Mux", sidetone1_enum);
1195
1196static const struct soc_enum sidetone2_enum =
1197 SOC_ENUM_SINGLE(WM8994_SIDETONE, 1, 2, sidetone_text);
1198
1199static const struct snd_kcontrol_new sidetone2_mux =
1200 SOC_DAPM_ENUM("Right Sidetone Mux", sidetone2_enum);
1201
1202static const char *aif1dac_text[] = {
1203 "AIF1DACDAT", "AIF3DACDAT",
1204};
1205
1206static const struct soc_enum aif1dac_enum =
1207 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 0, 2, aif1dac_text);
1208
1209static const struct snd_kcontrol_new aif1dac_mux =
1210 SOC_DAPM_ENUM("AIF1DAC Mux", aif1dac_enum);
1211
1212static const char *aif2dac_text[] = {
1213 "AIF2DACDAT", "AIF3DACDAT",
1214};
1215
1216static const struct soc_enum aif2dac_enum =
1217 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 1, 2, aif2dac_text);
1218
1219static const struct snd_kcontrol_new aif2dac_mux =
1220 SOC_DAPM_ENUM("AIF2DAC Mux", aif2dac_enum);
1221
1222static const char *aif2adc_text[] = {
1223 "AIF2ADCDAT", "AIF3DACDAT",
1224};
1225
1226static const struct soc_enum aif2adc_enum =
1227 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 2, 2, aif2adc_text);
1228
1229static const struct snd_kcontrol_new aif2adc_mux =
1230 SOC_DAPM_ENUM("AIF2ADC Mux", aif2adc_enum);
1231
1232static const char *aif3adc_text[] = {
Mark Brownc4431df2010-11-26 15:21:07 +00001233 "AIF1ADCDAT", "AIF2ADCDAT", "AIF2DACDAT", "Mono PCM",
Mark Brown9e6e96a2010-01-29 17:47:12 +00001234};
1235
Mark Brownc4431df2010-11-26 15:21:07 +00001236static const struct soc_enum wm8994_aif3adc_enum =
Mark Brown9e6e96a2010-01-29 17:47:12 +00001237 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 3, 3, aif3adc_text);
1238
Mark Brownc4431df2010-11-26 15:21:07 +00001239static const struct snd_kcontrol_new wm8994_aif3adc_mux =
1240 SOC_DAPM_ENUM("AIF3ADC Mux", wm8994_aif3adc_enum);
1241
1242static const struct soc_enum wm8958_aif3adc_enum =
1243 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 3, 4, aif3adc_text);
1244
1245static const struct snd_kcontrol_new wm8958_aif3adc_mux =
1246 SOC_DAPM_ENUM("AIF3ADC Mux", wm8958_aif3adc_enum);
1247
1248static const char *mono_pcm_out_text[] = {
1249 "None", "AIF2ADCL", "AIF2ADCR",
1250};
1251
1252static const struct soc_enum mono_pcm_out_enum =
1253 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 9, 3, mono_pcm_out_text);
1254
1255static const struct snd_kcontrol_new mono_pcm_out_mux =
1256 SOC_DAPM_ENUM("Mono PCM Out Mux", mono_pcm_out_enum);
1257
1258static const char *aif2dac_src_text[] = {
1259 "AIF2", "AIF3",
1260};
1261
1262/* Note that these two control shouldn't be simultaneously switched to AIF3 */
1263static const struct soc_enum aif2dacl_src_enum =
1264 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 7, 2, aif2dac_src_text);
1265
1266static const struct snd_kcontrol_new aif2dacl_src_mux =
1267 SOC_DAPM_ENUM("AIF2DACL Mux", aif2dacl_src_enum);
1268
1269static const struct soc_enum aif2dacr_src_enum =
1270 SOC_ENUM_SINGLE(WM8994_POWER_MANAGEMENT_6, 8, 2, aif2dac_src_text);
1271
1272static const struct snd_kcontrol_new aif2dacr_src_mux =
1273 SOC_DAPM_ENUM("AIF2DACR Mux", aif2dacr_src_enum);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001274
1275static const struct snd_soc_dapm_widget wm8994_dapm_widgets[] = {
1276SND_SOC_DAPM_INPUT("DMIC1DAT"),
1277SND_SOC_DAPM_INPUT("DMIC2DAT"),
Mark Brown66b47fd2010-07-08 11:25:43 +09001278SND_SOC_DAPM_INPUT("Clock"),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001279
1280SND_SOC_DAPM_SUPPLY("CLK_SYS", SND_SOC_NOPM, 0, 0, clk_sys_event,
1281 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1282
1283SND_SOC_DAPM_SUPPLY("DSP1CLK", WM8994_CLOCKING_1, 3, 0, NULL, 0),
1284SND_SOC_DAPM_SUPPLY("DSP2CLK", WM8994_CLOCKING_1, 2, 0, NULL, 0),
1285SND_SOC_DAPM_SUPPLY("DSPINTCLK", WM8994_CLOCKING_1, 1, 0, NULL, 0),
1286
1287SND_SOC_DAPM_SUPPLY("AIF1CLK", WM8994_AIF1_CLOCKING_1, 0, 0, NULL, 0),
1288SND_SOC_DAPM_SUPPLY("AIF2CLK", WM8994_AIF2_CLOCKING_1, 0, 0, NULL, 0),
1289
Mark Brown7f94de42011-02-03 16:27:34 +00001290SND_SOC_DAPM_AIF_OUT("AIF1ADC1L", NULL,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001291 0, WM8994_POWER_MANAGEMENT_4, 9, 0),
Mark Brown7f94de42011-02-03 16:27:34 +00001292SND_SOC_DAPM_AIF_OUT("AIF1ADC1R", NULL,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001293 0, WM8994_POWER_MANAGEMENT_4, 8, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001294SND_SOC_DAPM_AIF_IN_E("AIF1DAC1L", NULL, 0,
1295 WM8994_POWER_MANAGEMENT_5, 9, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001296 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brownd6addcc2010-11-26 15:21:08 +00001297SND_SOC_DAPM_AIF_IN_E("AIF1DAC1R", NULL, 0,
1298 WM8994_POWER_MANAGEMENT_5, 8, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001299 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001300
Mark Brown7f94de42011-02-03 16:27:34 +00001301SND_SOC_DAPM_AIF_OUT("AIF1ADC2L", NULL,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001302 0, WM8994_POWER_MANAGEMENT_4, 11, 0),
Mark Brown7f94de42011-02-03 16:27:34 +00001303SND_SOC_DAPM_AIF_OUT("AIF1ADC2R", NULL,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001304 0, WM8994_POWER_MANAGEMENT_4, 10, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001305SND_SOC_DAPM_AIF_IN_E("AIF1DAC2L", NULL, 0,
1306 WM8994_POWER_MANAGEMENT_5, 11, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001307 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brownd6addcc2010-11-26 15:21:08 +00001308SND_SOC_DAPM_AIF_IN_E("AIF1DAC2R", NULL, 0,
1309 WM8994_POWER_MANAGEMENT_5, 10, 0, wm8958_aif_ev,
Mark Brownb2822a82010-11-30 16:59:29 +00001310 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001311
1312SND_SOC_DAPM_MIXER("AIF1ADC1L Mixer", SND_SOC_NOPM, 0, 0,
1313 aif1adc1l_mix, ARRAY_SIZE(aif1adc1l_mix)),
1314SND_SOC_DAPM_MIXER("AIF1ADC1R Mixer", SND_SOC_NOPM, 0, 0,
1315 aif1adc1r_mix, ARRAY_SIZE(aif1adc1r_mix)),
1316
Mark Browna3257ba2010-07-19 14:02:34 +01001317SND_SOC_DAPM_MIXER("AIF1ADC2L Mixer", SND_SOC_NOPM, 0, 0,
1318 aif1adc2l_mix, ARRAY_SIZE(aif1adc2l_mix)),
1319SND_SOC_DAPM_MIXER("AIF1ADC2R Mixer", SND_SOC_NOPM, 0, 0,
1320 aif1adc2r_mix, ARRAY_SIZE(aif1adc2r_mix)),
1321
Mark Brown9e6e96a2010-01-29 17:47:12 +00001322SND_SOC_DAPM_MIXER("AIF2DAC2L Mixer", SND_SOC_NOPM, 0, 0,
1323 aif2dac2l_mix, ARRAY_SIZE(aif2dac2l_mix)),
1324SND_SOC_DAPM_MIXER("AIF2DAC2R Mixer", SND_SOC_NOPM, 0, 0,
1325 aif2dac2r_mix, ARRAY_SIZE(aif2dac2r_mix)),
1326
1327SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &sidetone1_mux),
1328SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &sidetone2_mux),
1329
1330SND_SOC_DAPM_MIXER("DAC1L Mixer", SND_SOC_NOPM, 0, 0,
1331 dac1l_mix, ARRAY_SIZE(dac1l_mix)),
1332SND_SOC_DAPM_MIXER("DAC1R Mixer", SND_SOC_NOPM, 0, 0,
1333 dac1r_mix, ARRAY_SIZE(dac1r_mix)),
1334
1335SND_SOC_DAPM_AIF_OUT("AIF2ADCL", NULL, 0,
1336 WM8994_POWER_MANAGEMENT_4, 13, 0),
1337SND_SOC_DAPM_AIF_OUT("AIF2ADCR", NULL, 0,
1338 WM8994_POWER_MANAGEMENT_4, 12, 0),
Mark Brownd6addcc2010-11-26 15:21:08 +00001339SND_SOC_DAPM_AIF_IN_E("AIF2DACL", NULL, 0,
1340 WM8994_POWER_MANAGEMENT_5, 13, 0, wm8958_aif_ev,
1341 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1342SND_SOC_DAPM_AIF_IN_E("AIF2DACR", NULL, 0,
1343 WM8994_POWER_MANAGEMENT_5, 12, 0, wm8958_aif_ev,
1344 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001345
1346SND_SOC_DAPM_AIF_IN("AIF1DACDAT", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
1347SND_SOC_DAPM_AIF_IN("AIF2DACDAT", "AIF2 Playback", 0, SND_SOC_NOPM, 0, 0),
Mark Brown7f94de42011-02-03 16:27:34 +00001348SND_SOC_DAPM_AIF_OUT("AIF1ADCDAT", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001349SND_SOC_DAPM_AIF_OUT("AIF2ADCDAT", "AIF2 Capture", 0, SND_SOC_NOPM, 0, 0),
1350
1351SND_SOC_DAPM_MUX("AIF1DAC Mux", SND_SOC_NOPM, 0, 0, &aif1dac_mux),
1352SND_SOC_DAPM_MUX("AIF2DAC Mux", SND_SOC_NOPM, 0, 0, &aif2dac_mux),
1353SND_SOC_DAPM_MUX("AIF2ADC Mux", SND_SOC_NOPM, 0, 0, &aif2adc_mux),
Mark Brown9e6e96a2010-01-29 17:47:12 +00001354
1355SND_SOC_DAPM_AIF_IN("AIF3DACDAT", "AIF3 Playback", 0, SND_SOC_NOPM, 0, 0),
1356SND_SOC_DAPM_AIF_IN("AIF3ADCDAT", "AIF3 Capture", 0, SND_SOC_NOPM, 0, 0),
1357
1358SND_SOC_DAPM_SUPPLY("TOCLK", WM8994_CLOCKING_1, 4, 0, NULL, 0),
1359
1360SND_SOC_DAPM_ADC("DMIC2L", NULL, WM8994_POWER_MANAGEMENT_4, 5, 0),
1361SND_SOC_DAPM_ADC("DMIC2R", NULL, WM8994_POWER_MANAGEMENT_4, 4, 0),
1362SND_SOC_DAPM_ADC("DMIC1L", NULL, WM8994_POWER_MANAGEMENT_4, 3, 0),
1363SND_SOC_DAPM_ADC("DMIC1R", NULL, WM8994_POWER_MANAGEMENT_4, 2, 0),
1364
1365/* Power is done with the muxes since the ADC power also controls the
1366 * downsampling chain, the chip will automatically manage the analogue
1367 * specific portions.
1368 */
1369SND_SOC_DAPM_ADC("ADCL", NULL, SND_SOC_NOPM, 1, 0),
1370SND_SOC_DAPM_ADC("ADCR", NULL, SND_SOC_NOPM, 0, 0),
1371
1372SND_SOC_DAPM_MUX("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux),
1373SND_SOC_DAPM_MUX("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux),
1374
1375SND_SOC_DAPM_DAC("DAC2L", NULL, WM8994_POWER_MANAGEMENT_5, 3, 0),
1376SND_SOC_DAPM_DAC("DAC2R", NULL, WM8994_POWER_MANAGEMENT_5, 2, 0),
1377SND_SOC_DAPM_DAC("DAC1L", NULL, WM8994_POWER_MANAGEMENT_5, 1, 0),
1378SND_SOC_DAPM_DAC("DAC1R", NULL, WM8994_POWER_MANAGEMENT_5, 0, 0),
1379
1380SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
1381SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
1382
1383SND_SOC_DAPM_MIXER("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
1384 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
1385SND_SOC_DAPM_MIXER("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
1386 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
1387
1388SND_SOC_DAPM_POST("Debug log", post_ev),
1389};
1390
Mark Brownc4431df2010-11-26 15:21:07 +00001391static const struct snd_soc_dapm_widget wm8994_specific_dapm_widgets[] = {
1392SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8994_aif3adc_mux),
1393};
Mark Brown9e6e96a2010-01-29 17:47:12 +00001394
Mark Brownc4431df2010-11-26 15:21:07 +00001395static const struct snd_soc_dapm_widget wm8958_dapm_widgets[] = {
1396SND_SOC_DAPM_MUX("Mono PCM Out Mux", SND_SOC_NOPM, 0, 0, &mono_pcm_out_mux),
1397SND_SOC_DAPM_MUX("AIF2DACL Mux", SND_SOC_NOPM, 0, 0, &aif2dacl_src_mux),
1398SND_SOC_DAPM_MUX("AIF2DACR Mux", SND_SOC_NOPM, 0, 0, &aif2dacr_src_mux),
1399SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8958_aif3adc_mux),
1400};
1401
1402static const struct snd_soc_dapm_route intercon[] = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00001403 { "CLK_SYS", NULL, "AIF1CLK", check_clk_sys },
1404 { "CLK_SYS", NULL, "AIF2CLK", check_clk_sys },
1405
1406 { "DSP1CLK", NULL, "CLK_SYS" },
1407 { "DSP2CLK", NULL, "CLK_SYS" },
1408 { "DSPINTCLK", NULL, "CLK_SYS" },
1409
1410 { "AIF1ADC1L", NULL, "AIF1CLK" },
1411 { "AIF1ADC1L", NULL, "DSP1CLK" },
1412 { "AIF1ADC1R", NULL, "AIF1CLK" },
1413 { "AIF1ADC1R", NULL, "DSP1CLK" },
1414 { "AIF1ADC1R", NULL, "DSPINTCLK" },
1415
1416 { "AIF1DAC1L", NULL, "AIF1CLK" },
1417 { "AIF1DAC1L", NULL, "DSP1CLK" },
1418 { "AIF1DAC1R", NULL, "AIF1CLK" },
1419 { "AIF1DAC1R", NULL, "DSP1CLK" },
1420 { "AIF1DAC1R", NULL, "DSPINTCLK" },
1421
1422 { "AIF1ADC2L", NULL, "AIF1CLK" },
1423 { "AIF1ADC2L", NULL, "DSP1CLK" },
1424 { "AIF1ADC2R", NULL, "AIF1CLK" },
1425 { "AIF1ADC2R", NULL, "DSP1CLK" },
1426 { "AIF1ADC2R", NULL, "DSPINTCLK" },
1427
1428 { "AIF1DAC2L", NULL, "AIF1CLK" },
1429 { "AIF1DAC2L", NULL, "DSP1CLK" },
1430 { "AIF1DAC2R", NULL, "AIF1CLK" },
1431 { "AIF1DAC2R", NULL, "DSP1CLK" },
1432 { "AIF1DAC2R", NULL, "DSPINTCLK" },
1433
1434 { "AIF2ADCL", NULL, "AIF2CLK" },
1435 { "AIF2ADCL", NULL, "DSP2CLK" },
1436 { "AIF2ADCR", NULL, "AIF2CLK" },
1437 { "AIF2ADCR", NULL, "DSP2CLK" },
1438 { "AIF2ADCR", NULL, "DSPINTCLK" },
1439
1440 { "AIF2DACL", NULL, "AIF2CLK" },
1441 { "AIF2DACL", NULL, "DSP2CLK" },
1442 { "AIF2DACR", NULL, "AIF2CLK" },
1443 { "AIF2DACR", NULL, "DSP2CLK" },
1444 { "AIF2DACR", NULL, "DSPINTCLK" },
1445
1446 { "DMIC1L", NULL, "DMIC1DAT" },
1447 { "DMIC1L", NULL, "CLK_SYS" },
1448 { "DMIC1R", NULL, "DMIC1DAT" },
1449 { "DMIC1R", NULL, "CLK_SYS" },
1450 { "DMIC2L", NULL, "DMIC2DAT" },
1451 { "DMIC2L", NULL, "CLK_SYS" },
1452 { "DMIC2R", NULL, "DMIC2DAT" },
1453 { "DMIC2R", NULL, "CLK_SYS" },
1454
1455 { "ADCL", NULL, "AIF1CLK" },
1456 { "ADCL", NULL, "DSP1CLK" },
1457 { "ADCL", NULL, "DSPINTCLK" },
1458
1459 { "ADCR", NULL, "AIF1CLK" },
1460 { "ADCR", NULL, "DSP1CLK" },
1461 { "ADCR", NULL, "DSPINTCLK" },
1462
1463 { "ADCL Mux", "ADC", "ADCL" },
1464 { "ADCL Mux", "DMIC", "DMIC1L" },
1465 { "ADCR Mux", "ADC", "ADCR" },
1466 { "ADCR Mux", "DMIC", "DMIC1R" },
1467
1468 { "DAC1L", NULL, "AIF1CLK" },
1469 { "DAC1L", NULL, "DSP1CLK" },
1470 { "DAC1L", NULL, "DSPINTCLK" },
1471
1472 { "DAC1R", NULL, "AIF1CLK" },
1473 { "DAC1R", NULL, "DSP1CLK" },
1474 { "DAC1R", NULL, "DSPINTCLK" },
1475
1476 { "DAC2L", NULL, "AIF2CLK" },
1477 { "DAC2L", NULL, "DSP2CLK" },
1478 { "DAC2L", NULL, "DSPINTCLK" },
1479
1480 { "DAC2R", NULL, "AIF2DACR" },
1481 { "DAC2R", NULL, "AIF2CLK" },
1482 { "DAC2R", NULL, "DSP2CLK" },
1483 { "DAC2R", NULL, "DSPINTCLK" },
1484
1485 { "TOCLK", NULL, "CLK_SYS" },
1486
1487 /* AIF1 outputs */
1488 { "AIF1ADC1L", NULL, "AIF1ADC1L Mixer" },
1489 { "AIF1ADC1L Mixer", "ADC/DMIC Switch", "ADCL Mux" },
1490 { "AIF1ADC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1491
1492 { "AIF1ADC1R", NULL, "AIF1ADC1R Mixer" },
1493 { "AIF1ADC1R Mixer", "ADC/DMIC Switch", "ADCR Mux" },
1494 { "AIF1ADC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1495
Mark Browna3257ba2010-07-19 14:02:34 +01001496 { "AIF1ADC2L", NULL, "AIF1ADC2L Mixer" },
1497 { "AIF1ADC2L Mixer", "DMIC Switch", "DMIC2L" },
1498 { "AIF1ADC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1499
1500 { "AIF1ADC2R", NULL, "AIF1ADC2R Mixer" },
1501 { "AIF1ADC2R Mixer", "DMIC Switch", "DMIC2R" },
1502 { "AIF1ADC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1503
Mark Brown9e6e96a2010-01-29 17:47:12 +00001504 /* Pin level routing for AIF3 */
1505 { "AIF1DAC1L", NULL, "AIF1DAC Mux" },
1506 { "AIF1DAC1R", NULL, "AIF1DAC Mux" },
1507 { "AIF1DAC2L", NULL, "AIF1DAC Mux" },
1508 { "AIF1DAC2R", NULL, "AIF1DAC Mux" },
1509
Mark Brown9e6e96a2010-01-29 17:47:12 +00001510 { "AIF1DAC Mux", "AIF1DACDAT", "AIF1DACDAT" },
1511 { "AIF1DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1512 { "AIF2DAC Mux", "AIF2DACDAT", "AIF2DACDAT" },
1513 { "AIF2DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
1514 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCL" },
1515 { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCR" },
1516 { "AIF2ADC Mux", "AIF3DACDAT", "AIF3ADCDAT" },
1517
1518 /* DAC1 inputs */
1519 { "DAC1L", NULL, "DAC1L Mixer" },
1520 { "DAC1L Mixer", "AIF2 Switch", "AIF2DACL" },
1521 { "DAC1L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1522 { "DAC1L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1523 { "DAC1L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1524 { "DAC1L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1525
1526 { "DAC1R", NULL, "DAC1R Mixer" },
1527 { "DAC1R Mixer", "AIF2 Switch", "AIF2DACR" },
1528 { "DAC1R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1529 { "DAC1R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1530 { "DAC1R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1531 { "DAC1R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1532
1533 /* DAC2/AIF2 outputs */
1534 { "AIF2ADCL", NULL, "AIF2DAC2L Mixer" },
1535 { "DAC2L", NULL, "AIF2DAC2L Mixer" },
1536 { "AIF2DAC2L Mixer", "AIF2 Switch", "AIF2DACL" },
1537 { "AIF2DAC2L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
1538 { "AIF2DAC2L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
1539 { "AIF2DAC2L Mixer", "Left Sidetone Switch", "Left Sidetone" },
1540 { "AIF2DAC2L Mixer", "Right Sidetone Switch", "Right Sidetone" },
1541
1542 { "AIF2ADCR", NULL, "AIF2DAC2R Mixer" },
1543 { "DAC2R", NULL, "AIF2DAC2R Mixer" },
1544 { "AIF2DAC2R Mixer", "AIF2 Switch", "AIF2DACR" },
1545 { "AIF2DAC2R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
1546 { "AIF2DAC2R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
1547 { "AIF2DAC2R Mixer", "Left Sidetone Switch", "Left Sidetone" },
1548 { "AIF2DAC2R Mixer", "Right Sidetone Switch", "Right Sidetone" },
1549
Mark Brown7f94de42011-02-03 16:27:34 +00001550 { "AIF1ADCDAT", NULL, "AIF1ADC1L" },
1551 { "AIF1ADCDAT", NULL, "AIF1ADC1R" },
1552 { "AIF1ADCDAT", NULL, "AIF1ADC2L" },
1553 { "AIF1ADCDAT", NULL, "AIF1ADC2R" },
1554
Mark Brown9e6e96a2010-01-29 17:47:12 +00001555 { "AIF2ADCDAT", NULL, "AIF2ADC Mux" },
1556
1557 /* AIF3 output */
1558 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1L" },
1559 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC1R" },
1560 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2L" },
1561 { "AIF3ADCDAT", "AIF1ADCDAT", "AIF1ADC2R" },
1562 { "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCL" },
1563 { "AIF3ADCDAT", "AIF2ADCDAT", "AIF2ADCR" },
1564 { "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACL" },
1565 { "AIF3ADCDAT", "AIF2DACDAT", "AIF2DACR" },
1566
1567 /* Sidetone */
1568 { "Left Sidetone", "ADC/DMIC1", "ADCL Mux" },
1569 { "Left Sidetone", "DMIC2", "DMIC2L" },
1570 { "Right Sidetone", "ADC/DMIC1", "ADCR Mux" },
1571 { "Right Sidetone", "DMIC2", "DMIC2R" },
1572
1573 /* Output stages */
1574 { "Left Output Mixer", "DAC Switch", "DAC1L" },
1575 { "Right Output Mixer", "DAC Switch", "DAC1R" },
1576
1577 { "SPKL", "DAC1 Switch", "DAC1L" },
1578 { "SPKL", "DAC2 Switch", "DAC2L" },
1579
1580 { "SPKR", "DAC1 Switch", "DAC1R" },
1581 { "SPKR", "DAC2 Switch", "DAC2R" },
1582
1583 { "Left Headphone Mux", "DAC", "DAC1L" },
1584 { "Right Headphone Mux", "DAC", "DAC1R" },
1585};
1586
Mark Brownc4431df2010-11-26 15:21:07 +00001587static const struct snd_soc_dapm_route wm8994_intercon[] = {
1588 { "AIF2DACL", NULL, "AIF2DAC Mux" },
1589 { "AIF2DACR", NULL, "AIF2DAC Mux" },
1590};
1591
1592static const struct snd_soc_dapm_route wm8958_intercon[] = {
1593 { "AIF2DACL", NULL, "AIF2DACL Mux" },
1594 { "AIF2DACR", NULL, "AIF2DACR Mux" },
1595
1596 { "AIF2DACL Mux", "AIF2", "AIF2DAC Mux" },
1597 { "AIF2DACL Mux", "AIF3", "AIF3DACDAT" },
1598 { "AIF2DACR Mux", "AIF2", "AIF2DAC Mux" },
1599 { "AIF2DACR Mux", "AIF3", "AIF3DACDAT" },
1600
1601 { "Mono PCM Out Mux", "AIF2ADCL", "AIF2ADCL" },
1602 { "Mono PCM Out Mux", "AIF2ADCR", "AIF2ADCR" },
1603
1604 { "AIF3ADC Mux", "Mono PCM", "Mono PCM Out Mux" },
1605};
1606
Mark Brown9e6e96a2010-01-29 17:47:12 +00001607/* The size in bits of the FLL divide multiplied by 10
1608 * to allow rounding later */
1609#define FIXED_FLL_SIZE ((1 << 16) * 10)
1610
1611struct fll_div {
1612 u16 outdiv;
1613 u16 n;
1614 u16 k;
1615 u16 clk_ref_div;
1616 u16 fll_fratio;
1617};
1618
1619static int wm8994_get_fll_config(struct fll_div *fll,
1620 int freq_in, int freq_out)
1621{
1622 u64 Kpart;
1623 unsigned int K, Ndiv, Nmod;
1624
1625 pr_debug("FLL input=%dHz, output=%dHz\n", freq_in, freq_out);
1626
1627 /* Scale the input frequency down to <= 13.5MHz */
1628 fll->clk_ref_div = 0;
1629 while (freq_in > 13500000) {
1630 fll->clk_ref_div++;
1631 freq_in /= 2;
1632
1633 if (fll->clk_ref_div > 3)
1634 return -EINVAL;
1635 }
1636 pr_debug("CLK_REF_DIV=%d, Fref=%dHz\n", fll->clk_ref_div, freq_in);
1637
1638 /* Scale the output to give 90MHz<=Fvco<=100MHz */
1639 fll->outdiv = 3;
1640 while (freq_out * (fll->outdiv + 1) < 90000000) {
1641 fll->outdiv++;
1642 if (fll->outdiv > 63)
1643 return -EINVAL;
1644 }
1645 freq_out *= fll->outdiv + 1;
1646 pr_debug("OUTDIV=%d, Fvco=%dHz\n", fll->outdiv, freq_out);
1647
1648 if (freq_in > 1000000) {
1649 fll->fll_fratio = 0;
Mark Brown7d48a6a2010-04-20 13:36:11 +09001650 } else if (freq_in > 256000) {
1651 fll->fll_fratio = 1;
1652 freq_in *= 2;
1653 } else if (freq_in > 128000) {
1654 fll->fll_fratio = 2;
1655 freq_in *= 4;
1656 } else if (freq_in > 64000) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00001657 fll->fll_fratio = 3;
1658 freq_in *= 8;
Mark Brown7d48a6a2010-04-20 13:36:11 +09001659 } else {
1660 fll->fll_fratio = 4;
1661 freq_in *= 16;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001662 }
1663 pr_debug("FLL_FRATIO=%d, Fref=%dHz\n", fll->fll_fratio, freq_in);
1664
1665 /* Now, calculate N.K */
1666 Ndiv = freq_out / freq_in;
1667
1668 fll->n = Ndiv;
1669 Nmod = freq_out % freq_in;
1670 pr_debug("Nmod=%d\n", Nmod);
1671
1672 /* Calculate fractional part - scale up so we can round. */
1673 Kpart = FIXED_FLL_SIZE * (long long)Nmod;
1674
1675 do_div(Kpart, freq_in);
1676
1677 K = Kpart & 0xFFFFFFFF;
1678
1679 if ((K % 10) >= 5)
1680 K += 5;
1681
1682 /* Move down to proper range now rounding is done */
1683 fll->k = K / 10;
1684
1685 pr_debug("N=%x K=%x\n", fll->n, fll->k);
1686
1687 return 0;
1688}
1689
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001690static int _wm8994_set_fll(struct snd_soc_codec *codec, int id, int src,
Mark Brown9e6e96a2010-01-29 17:47:12 +00001691 unsigned int freq_in, unsigned int freq_out)
1692{
Mark Brownb2c812e2010-04-14 15:35:19 +09001693 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001694 int reg_offset, ret;
1695 struct fll_div fll;
1696 u16 reg, aif1, aif2;
1697
1698 aif1 = snd_soc_read(codec, WM8994_AIF1_CLOCKING_1)
1699 & WM8994_AIF1CLK_ENA;
1700
1701 aif2 = snd_soc_read(codec, WM8994_AIF2_CLOCKING_1)
1702 & WM8994_AIF2CLK_ENA;
1703
1704 switch (id) {
1705 case WM8994_FLL1:
1706 reg_offset = 0;
1707 id = 0;
1708 break;
1709 case WM8994_FLL2:
1710 reg_offset = 0x20;
1711 id = 1;
1712 break;
1713 default:
1714 return -EINVAL;
1715 }
1716
Mark Brown136ff2a2010-04-20 12:56:18 +09001717 switch (src) {
Mark Brown7add84a2010-04-22 02:29:01 +09001718 case 0:
1719 /* Allow no source specification when stopping */
1720 if (freq_out)
1721 return -EINVAL;
Mark Brown4514e892010-12-03 16:02:10 +00001722 src = wm8994->fll[id].src;
Mark Brown7add84a2010-04-22 02:29:01 +09001723 break;
Mark Brown136ff2a2010-04-20 12:56:18 +09001724 case WM8994_FLL_SRC_MCLK1:
1725 case WM8994_FLL_SRC_MCLK2:
1726 case WM8994_FLL_SRC_LRCLK:
1727 case WM8994_FLL_SRC_BCLK:
1728 break;
1729 default:
1730 return -EINVAL;
1731 }
1732
Mark Brown9e6e96a2010-01-29 17:47:12 +00001733 /* Are we changing anything? */
1734 if (wm8994->fll[id].src == src &&
1735 wm8994->fll[id].in == freq_in && wm8994->fll[id].out == freq_out)
1736 return 0;
1737
1738 /* If we're stopping the FLL redo the old config - no
1739 * registers will actually be written but we avoid GCC flow
1740 * analysis bugs spewing warnings.
1741 */
1742 if (freq_out)
1743 ret = wm8994_get_fll_config(&fll, freq_in, freq_out);
1744 else
1745 ret = wm8994_get_fll_config(&fll, wm8994->fll[id].in,
1746 wm8994->fll[id].out);
1747 if (ret < 0)
1748 return ret;
1749
1750 /* Gate the AIF clocks while we reclock */
1751 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1752 WM8994_AIF1CLK_ENA, 0);
1753 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1754 WM8994_AIF2CLK_ENA, 0);
1755
1756 /* We always need to disable the FLL while reconfiguring */
1757 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
1758 WM8994_FLL1_ENA, 0);
1759
1760 reg = (fll.outdiv << WM8994_FLL1_OUTDIV_SHIFT) |
1761 (fll.fll_fratio << WM8994_FLL1_FRATIO_SHIFT);
1762 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_2 + reg_offset,
1763 WM8994_FLL1_OUTDIV_MASK |
1764 WM8994_FLL1_FRATIO_MASK, reg);
1765
1766 snd_soc_write(codec, WM8994_FLL1_CONTROL_3 + reg_offset, fll.k);
1767
1768 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_4 + reg_offset,
1769 WM8994_FLL1_N_MASK,
1770 fll.n << WM8994_FLL1_N_SHIFT);
1771
1772 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_5 + reg_offset,
Mark Brown136ff2a2010-04-20 12:56:18 +09001773 WM8994_FLL1_REFCLK_DIV_MASK |
1774 WM8994_FLL1_REFCLK_SRC_MASK,
1775 (fll.clk_ref_div << WM8994_FLL1_REFCLK_DIV_SHIFT) |
1776 (src - 1));
Mark Brown9e6e96a2010-01-29 17:47:12 +00001777
1778 /* Enable (with fractional mode if required) */
1779 if (freq_out) {
1780 if (fll.k)
1781 reg = WM8994_FLL1_ENA | WM8994_FLL1_FRAC;
1782 else
1783 reg = WM8994_FLL1_ENA;
1784 snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
1785 WM8994_FLL1_ENA | WM8994_FLL1_FRAC,
1786 reg);
1787 }
1788
1789 wm8994->fll[id].in = freq_in;
1790 wm8994->fll[id].out = freq_out;
Mark Brown136ff2a2010-04-20 12:56:18 +09001791 wm8994->fll[id].src = src;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001792
1793 /* Enable any gated AIF clocks */
1794 snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
1795 WM8994_AIF1CLK_ENA, aif1);
1796 snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
1797 WM8994_AIF2CLK_ENA, aif2);
1798
1799 configure_clock(codec);
1800
1801 return 0;
1802}
1803
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001804
Mark Brown66b47fd2010-07-08 11:25:43 +09001805static int opclk_divs[] = { 10, 20, 30, 40, 55, 60, 80, 120, 160 };
1806
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001807static int wm8994_set_fll(struct snd_soc_dai *dai, int id, int src,
1808 unsigned int freq_in, unsigned int freq_out)
1809{
1810 return _wm8994_set_fll(dai->codec, id, src, freq_in, freq_out);
1811}
1812
Mark Brown9e6e96a2010-01-29 17:47:12 +00001813static int wm8994_set_dai_sysclk(struct snd_soc_dai *dai,
1814 int clk_id, unsigned int freq, int dir)
1815{
1816 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001817 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown66b47fd2010-07-08 11:25:43 +09001818 int i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00001819
1820 switch (dai->id) {
1821 case 1:
1822 case 2:
1823 break;
1824
1825 default:
1826 /* AIF3 shares clocking with AIF1/2 */
1827 return -EINVAL;
1828 }
1829
1830 switch (clk_id) {
1831 case WM8994_SYSCLK_MCLK1:
1832 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK1;
1833 wm8994->mclk[0] = freq;
1834 dev_dbg(dai->dev, "AIF%d using MCLK1 at %uHz\n",
1835 dai->id, freq);
1836 break;
1837
1838 case WM8994_SYSCLK_MCLK2:
1839 /* TODO: Set GPIO AF */
1840 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK2;
1841 wm8994->mclk[1] = freq;
1842 dev_dbg(dai->dev, "AIF%d using MCLK2 at %uHz\n",
1843 dai->id, freq);
1844 break;
1845
1846 case WM8994_SYSCLK_FLL1:
1847 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL1;
1848 dev_dbg(dai->dev, "AIF%d using FLL1\n", dai->id);
1849 break;
1850
1851 case WM8994_SYSCLK_FLL2:
1852 wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL2;
1853 dev_dbg(dai->dev, "AIF%d using FLL2\n", dai->id);
1854 break;
1855
Mark Brown66b47fd2010-07-08 11:25:43 +09001856 case WM8994_SYSCLK_OPCLK:
1857 /* Special case - a division (times 10) is given and
1858 * no effect on main clocking.
1859 */
1860 if (freq) {
1861 for (i = 0; i < ARRAY_SIZE(opclk_divs); i++)
1862 if (opclk_divs[i] == freq)
1863 break;
1864 if (i == ARRAY_SIZE(opclk_divs))
1865 return -EINVAL;
1866 snd_soc_update_bits(codec, WM8994_CLOCKING_2,
1867 WM8994_OPCLK_DIV_MASK, i);
1868 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
1869 WM8994_OPCLK_ENA, WM8994_OPCLK_ENA);
1870 } else {
1871 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
1872 WM8994_OPCLK_ENA, 0);
1873 }
1874
Mark Brown9e6e96a2010-01-29 17:47:12 +00001875 default:
1876 return -EINVAL;
1877 }
1878
1879 configure_clock(codec);
1880
1881 return 0;
1882}
1883
1884static int wm8994_set_bias_level(struct snd_soc_codec *codec,
1885 enum snd_soc_bias_level level)
1886{
Mark Brown3a423152010-11-26 15:21:06 +00001887 struct wm8994 *control = codec->control_data;
Mark Brownb6b05692010-08-13 12:58:20 +01001888 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1889
Mark Brown9e6e96a2010-01-29 17:47:12 +00001890 switch (level) {
1891 case SND_SOC_BIAS_ON:
1892 break;
1893
1894 case SND_SOC_BIAS_PREPARE:
1895 /* VMID=2x40k */
1896 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
1897 WM8994_VMID_SEL_MASK, 0x2);
1898 break;
1899
1900 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001901 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
Mark Brown39fb51a2010-11-26 17:23:43 +00001902 pm_runtime_get_sync(codec->dev);
1903
Mark Brown8bc3c2c2010-11-30 14:56:18 +00001904 switch (control->type) {
1905 case WM8994:
1906 if (wm8994->revision < 4) {
1907 /* Tweak DC servo and DSP
1908 * configuration for improved
1909 * performance. */
1910 snd_soc_write(codec, 0x102, 0x3);
1911 snd_soc_write(codec, 0x56, 0x3);
1912 snd_soc_write(codec, 0x817, 0);
1913 snd_soc_write(codec, 0x102, 0);
1914 }
1915 break;
1916
1917 case WM8958:
1918 if (wm8994->revision == 0) {
1919 /* Optimise performance for rev A */
1920 snd_soc_write(codec, 0x102, 0x3);
1921 snd_soc_write(codec, 0xcb, 0x81);
1922 snd_soc_write(codec, 0x817, 0);
1923 snd_soc_write(codec, 0x102, 0);
1924
1925 snd_soc_update_bits(codec,
1926 WM8958_CHARGE_PUMP_2,
1927 WM8958_CP_DISCH,
1928 WM8958_CP_DISCH);
1929 }
1930 break;
Mark Brownb6b05692010-08-13 12:58:20 +01001931 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00001932
1933 /* Discharge LINEOUT1 & 2 */
1934 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
1935 WM8994_LINEOUT1_DISCH |
1936 WM8994_LINEOUT2_DISCH,
1937 WM8994_LINEOUT1_DISCH |
1938 WM8994_LINEOUT2_DISCH);
1939
1940 /* Startup bias, VMID ramp & buffer */
1941 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
1942 WM8994_STARTUP_BIAS_ENA |
1943 WM8994_VMID_BUF_ENA |
1944 WM8994_VMID_RAMP_MASK,
1945 WM8994_STARTUP_BIAS_ENA |
1946 WM8994_VMID_BUF_ENA |
1947 (0x11 << WM8994_VMID_RAMP_SHIFT));
1948
1949 /* Main bias enable, VMID=2x40k */
1950 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
1951 WM8994_BIAS_ENA |
1952 WM8994_VMID_SEL_MASK,
1953 WM8994_BIAS_ENA | 0x2);
1954
1955 msleep(20);
1956 }
1957
1958 /* VMID=2x500k */
1959 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
1960 WM8994_VMID_SEL_MASK, 0x4);
1961
1962 break;
1963
1964 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001965 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
Mark Brownd522ffb2010-03-30 14:29:14 +01001966 /* Switch over to startup biases */
1967 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
1968 WM8994_BIAS_SRC |
1969 WM8994_STARTUP_BIAS_ENA |
1970 WM8994_VMID_BUF_ENA |
1971 WM8994_VMID_RAMP_MASK,
1972 WM8994_BIAS_SRC |
1973 WM8994_STARTUP_BIAS_ENA |
1974 WM8994_VMID_BUF_ENA |
1975 (1 << WM8994_VMID_RAMP_SHIFT));
Mark Brown9e6e96a2010-01-29 17:47:12 +00001976
Mark Brownd522ffb2010-03-30 14:29:14 +01001977 /* Disable main biases */
1978 snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
1979 WM8994_BIAS_ENA |
1980 WM8994_VMID_SEL_MASK, 0);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001981
Mark Brownd522ffb2010-03-30 14:29:14 +01001982 /* Discharge line */
1983 snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
1984 WM8994_LINEOUT1_DISCH |
1985 WM8994_LINEOUT2_DISCH,
1986 WM8994_LINEOUT1_DISCH |
1987 WM8994_LINEOUT2_DISCH);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001988
Mark Brownd522ffb2010-03-30 14:29:14 +01001989 msleep(5);
Mark Brown9e6e96a2010-01-29 17:47:12 +00001990
Mark Brownd522ffb2010-03-30 14:29:14 +01001991 /* Switch off startup biases */
1992 snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
1993 WM8994_BIAS_SRC |
1994 WM8994_STARTUP_BIAS_ENA |
1995 WM8994_VMID_BUF_ENA |
1996 WM8994_VMID_RAMP_MASK, 0);
Mark Brown39fb51a2010-11-26 17:23:43 +00001997
1998 pm_runtime_put(codec->dev);
Mark Brownd522ffb2010-03-30 14:29:14 +01001999 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002000 break;
2001 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002002 codec->dapm.bias_level = level;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002003 return 0;
2004}
2005
2006static int wm8994_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2007{
2008 struct snd_soc_codec *codec = dai->codec;
Mark Brownc4431df2010-11-26 15:21:07 +00002009 struct wm8994 *control = codec->control_data;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002010 int ms_reg;
2011 int aif1_reg;
2012 int ms = 0;
2013 int aif1 = 0;
2014
2015 switch (dai->id) {
2016 case 1:
2017 ms_reg = WM8994_AIF1_MASTER_SLAVE;
2018 aif1_reg = WM8994_AIF1_CONTROL_1;
2019 break;
2020 case 2:
2021 ms_reg = WM8994_AIF2_MASTER_SLAVE;
2022 aif1_reg = WM8994_AIF2_CONTROL_1;
2023 break;
2024 default:
2025 return -EINVAL;
2026 }
2027
2028 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
2029 case SND_SOC_DAIFMT_CBS_CFS:
2030 break;
2031 case SND_SOC_DAIFMT_CBM_CFM:
2032 ms = WM8994_AIF1_MSTR;
2033 break;
2034 default:
2035 return -EINVAL;
2036 }
2037
2038 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2039 case SND_SOC_DAIFMT_DSP_B:
2040 aif1 |= WM8994_AIF1_LRCLK_INV;
2041 case SND_SOC_DAIFMT_DSP_A:
2042 aif1 |= 0x18;
2043 break;
2044 case SND_SOC_DAIFMT_I2S:
2045 aif1 |= 0x10;
2046 break;
2047 case SND_SOC_DAIFMT_RIGHT_J:
2048 break;
2049 case SND_SOC_DAIFMT_LEFT_J:
2050 aif1 |= 0x8;
2051 break;
2052 default:
2053 return -EINVAL;
2054 }
2055
2056 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2057 case SND_SOC_DAIFMT_DSP_A:
2058 case SND_SOC_DAIFMT_DSP_B:
2059 /* frame inversion not valid for DSP modes */
2060 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2061 case SND_SOC_DAIFMT_NB_NF:
2062 break;
2063 case SND_SOC_DAIFMT_IB_NF:
2064 aif1 |= WM8994_AIF1_BCLK_INV;
2065 break;
2066 default:
2067 return -EINVAL;
2068 }
2069 break;
2070
2071 case SND_SOC_DAIFMT_I2S:
2072 case SND_SOC_DAIFMT_RIGHT_J:
2073 case SND_SOC_DAIFMT_LEFT_J:
2074 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2075 case SND_SOC_DAIFMT_NB_NF:
2076 break;
2077 case SND_SOC_DAIFMT_IB_IF:
2078 aif1 |= WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV;
2079 break;
2080 case SND_SOC_DAIFMT_IB_NF:
2081 aif1 |= WM8994_AIF1_BCLK_INV;
2082 break;
2083 case SND_SOC_DAIFMT_NB_IF:
2084 aif1 |= WM8994_AIF1_LRCLK_INV;
2085 break;
2086 default:
2087 return -EINVAL;
2088 }
2089 break;
2090 default:
2091 return -EINVAL;
2092 }
2093
Mark Brownc4431df2010-11-26 15:21:07 +00002094 /* The AIF2 format configuration needs to be mirrored to AIF3
2095 * on WM8958 if it's in use so just do it all the time. */
2096 if (control->type == WM8958 && dai->id == 2)
2097 snd_soc_update_bits(codec, WM8958_AIF3_CONTROL_1,
2098 WM8994_AIF1_LRCLK_INV |
2099 WM8958_AIF3_FMT_MASK, aif1);
2100
Mark Brown9e6e96a2010-01-29 17:47:12 +00002101 snd_soc_update_bits(codec, aif1_reg,
2102 WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV |
2103 WM8994_AIF1_FMT_MASK,
2104 aif1);
2105 snd_soc_update_bits(codec, ms_reg, WM8994_AIF1_MSTR,
2106 ms);
2107
2108 return 0;
2109}
2110
2111static struct {
2112 int val, rate;
2113} srs[] = {
2114 { 0, 8000 },
2115 { 1, 11025 },
2116 { 2, 12000 },
2117 { 3, 16000 },
2118 { 4, 22050 },
2119 { 5, 24000 },
2120 { 6, 32000 },
2121 { 7, 44100 },
2122 { 8, 48000 },
2123 { 9, 88200 },
2124 { 10, 96000 },
2125};
2126
2127static int fs_ratios[] = {
2128 64, 128, 192, 256, 348, 512, 768, 1024, 1408, 1536
2129};
2130
2131static int bclk_divs[] = {
2132 10, 15, 20, 30, 40, 50, 60, 80, 110, 120, 160, 220, 240, 320, 440, 480,
2133 640, 880, 960, 1280, 1760, 1920
2134};
2135
2136static int wm8994_hw_params(struct snd_pcm_substream *substream,
2137 struct snd_pcm_hw_params *params,
2138 struct snd_soc_dai *dai)
2139{
2140 struct snd_soc_codec *codec = dai->codec;
Mark Brownc4431df2010-11-26 15:21:07 +00002141 struct wm8994 *control = codec->control_data;
Mark Brownb2c812e2010-04-14 15:35:19 +09002142 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002143 int aif1_reg;
Mark Brownb1e43d92010-12-07 17:14:56 +00002144 int aif2_reg;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002145 int bclk_reg;
2146 int lrclk_reg;
2147 int rate_reg;
2148 int aif1 = 0;
Mark Brownb1e43d92010-12-07 17:14:56 +00002149 int aif2 = 0;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002150 int bclk = 0;
2151 int lrclk = 0;
2152 int rate_val = 0;
2153 int id = dai->id - 1;
2154
2155 int i, cur_val, best_val, bclk_rate, best;
2156
2157 switch (dai->id) {
2158 case 1:
2159 aif1_reg = WM8994_AIF1_CONTROL_1;
Mark Brownb1e43d92010-12-07 17:14:56 +00002160 aif2_reg = WM8994_AIF1_CONTROL_2;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002161 bclk_reg = WM8994_AIF1_BCLK;
2162 rate_reg = WM8994_AIF1_RATE;
2163 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
Mark Brown7d83d212010-08-23 10:54:43 +01002164 wm8994->lrclk_shared[0]) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002165 lrclk_reg = WM8994_AIF1DAC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002166 } else {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002167 lrclk_reg = WM8994_AIF1ADC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002168 dev_dbg(codec->dev, "AIF1 using split LRCLK\n");
2169 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002170 break;
2171 case 2:
2172 aif1_reg = WM8994_AIF2_CONTROL_1;
Mark Brownb1e43d92010-12-07 17:14:56 +00002173 aif2_reg = WM8994_AIF2_CONTROL_2;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002174 bclk_reg = WM8994_AIF2_BCLK;
2175 rate_reg = WM8994_AIF2_RATE;
2176 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
Mark Brown7d83d212010-08-23 10:54:43 +01002177 wm8994->lrclk_shared[1]) {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002178 lrclk_reg = WM8994_AIF2DAC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002179 } else {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002180 lrclk_reg = WM8994_AIF2ADC_LRCLK;
Mark Brown7d83d212010-08-23 10:54:43 +01002181 dev_dbg(codec->dev, "AIF2 using split LRCLK\n");
2182 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002183 break;
Mark Brownc4431df2010-11-26 15:21:07 +00002184 case 3:
2185 switch (control->type) {
2186 case WM8958:
2187 aif1_reg = WM8958_AIF3_CONTROL_1;
2188 break;
2189 default:
2190 return 0;
2191 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002192 default:
2193 return -EINVAL;
2194 }
2195
2196 bclk_rate = params_rate(params) * 2;
2197 switch (params_format(params)) {
2198 case SNDRV_PCM_FORMAT_S16_LE:
2199 bclk_rate *= 16;
2200 break;
2201 case SNDRV_PCM_FORMAT_S20_3LE:
2202 bclk_rate *= 20;
2203 aif1 |= 0x20;
2204 break;
2205 case SNDRV_PCM_FORMAT_S24_LE:
2206 bclk_rate *= 24;
2207 aif1 |= 0x40;
2208 break;
2209 case SNDRV_PCM_FORMAT_S32_LE:
2210 bclk_rate *= 32;
2211 aif1 |= 0x60;
2212 break;
2213 default:
2214 return -EINVAL;
2215 }
2216
2217 /* Try to find an appropriate sample rate; look for an exact match. */
2218 for (i = 0; i < ARRAY_SIZE(srs); i++)
2219 if (srs[i].rate == params_rate(params))
2220 break;
2221 if (i == ARRAY_SIZE(srs))
2222 return -EINVAL;
2223 rate_val |= srs[i].val << WM8994_AIF1_SR_SHIFT;
2224
2225 dev_dbg(dai->dev, "Sample rate is %dHz\n", srs[i].rate);
2226 dev_dbg(dai->dev, "AIF%dCLK is %dHz, target BCLK %dHz\n",
2227 dai->id, wm8994->aifclk[id], bclk_rate);
2228
Mark Brownb1e43d92010-12-07 17:14:56 +00002229 if (params_channels(params) == 1 &&
2230 (snd_soc_read(codec, aif1_reg) & 0x18) == 0x18)
2231 aif2 |= WM8994_AIF1_MONO;
2232
Mark Brown9e6e96a2010-01-29 17:47:12 +00002233 if (wm8994->aifclk[id] == 0) {
2234 dev_err(dai->dev, "AIF%dCLK not configured\n", dai->id);
2235 return -EINVAL;
2236 }
2237
2238 /* AIFCLK/fs ratio; look for a close match in either direction */
2239 best = 0;
2240 best_val = abs((fs_ratios[0] * params_rate(params))
2241 - wm8994->aifclk[id]);
2242 for (i = 1; i < ARRAY_SIZE(fs_ratios); i++) {
2243 cur_val = abs((fs_ratios[i] * params_rate(params))
2244 - wm8994->aifclk[id]);
2245 if (cur_val >= best_val)
2246 continue;
2247 best = i;
2248 best_val = cur_val;
2249 }
2250 dev_dbg(dai->dev, "Selected AIF%dCLK/fs = %d\n",
2251 dai->id, fs_ratios[best]);
2252 rate_val |= best;
2253
2254 /* We may not get quite the right frequency if using
2255 * approximate clocks so look for the closest match that is
2256 * higher than the target (we need to ensure that there enough
2257 * BCLKs to clock out the samples).
2258 */
2259 best = 0;
2260 for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
Joonyoung Shim07cd8ad2010-02-02 18:53:19 +09002261 cur_val = (wm8994->aifclk[id] * 10 / bclk_divs[i]) - bclk_rate;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002262 if (cur_val < 0) /* BCLK table is sorted */
2263 break;
2264 best = i;
2265 }
Joonyoung Shim07cd8ad2010-02-02 18:53:19 +09002266 bclk_rate = wm8994->aifclk[id] * 10 / bclk_divs[best];
Mark Brown9e6e96a2010-01-29 17:47:12 +00002267 dev_dbg(dai->dev, "Using BCLK_DIV %d for actual BCLK %dHz\n",
2268 bclk_divs[best], bclk_rate);
2269 bclk |= best << WM8994_AIF1_BCLK_DIV_SHIFT;
2270
2271 lrclk = bclk_rate / params_rate(params);
2272 dev_dbg(dai->dev, "Using LRCLK rate %d for actual LRCLK %dHz\n",
2273 lrclk, bclk_rate / lrclk);
2274
2275 snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
Mark Brownb1e43d92010-12-07 17:14:56 +00002276 snd_soc_update_bits(codec, aif2_reg, WM8994_AIF1_MONO, aif2);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002277 snd_soc_update_bits(codec, bclk_reg, WM8994_AIF1_BCLK_DIV_MASK, bclk);
2278 snd_soc_update_bits(codec, lrclk_reg, WM8994_AIF1DAC_RATE_MASK,
2279 lrclk);
2280 snd_soc_update_bits(codec, rate_reg, WM8994_AIF1_SR_MASK |
2281 WM8994_AIF1CLK_RATE_MASK, rate_val);
2282
2283 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
2284 switch (dai->id) {
2285 case 1:
2286 wm8994->dac_rates[0] = params_rate(params);
2287 wm8994_set_retune_mobile(codec, 0);
2288 wm8994_set_retune_mobile(codec, 1);
2289 break;
2290 case 2:
2291 wm8994->dac_rates[1] = params_rate(params);
2292 wm8994_set_retune_mobile(codec, 2);
2293 break;
2294 }
2295 }
2296
2297 return 0;
2298}
2299
Mark Brownc4431df2010-11-26 15:21:07 +00002300static int wm8994_aif3_hw_params(struct snd_pcm_substream *substream,
2301 struct snd_pcm_hw_params *params,
2302 struct snd_soc_dai *dai)
2303{
2304 struct snd_soc_codec *codec = dai->codec;
2305 struct wm8994 *control = codec->control_data;
2306 int aif1_reg;
2307 int aif1 = 0;
2308
2309 switch (dai->id) {
2310 case 3:
2311 switch (control->type) {
2312 case WM8958:
2313 aif1_reg = WM8958_AIF3_CONTROL_1;
2314 break;
2315 default:
2316 return 0;
2317 }
2318 default:
2319 return 0;
2320 }
2321
2322 switch (params_format(params)) {
2323 case SNDRV_PCM_FORMAT_S16_LE:
2324 break;
2325 case SNDRV_PCM_FORMAT_S20_3LE:
2326 aif1 |= 0x20;
2327 break;
2328 case SNDRV_PCM_FORMAT_S24_LE:
2329 aif1 |= 0x40;
2330 break;
2331 case SNDRV_PCM_FORMAT_S32_LE:
2332 aif1 |= 0x60;
2333 break;
2334 default:
2335 return -EINVAL;
2336 }
2337
2338 return snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
2339}
2340
Mark Brown9e6e96a2010-01-29 17:47:12 +00002341static int wm8994_aif_mute(struct snd_soc_dai *codec_dai, int mute)
2342{
2343 struct snd_soc_codec *codec = codec_dai->codec;
2344 int mute_reg;
2345 int reg;
2346
2347 switch (codec_dai->id) {
2348 case 1:
2349 mute_reg = WM8994_AIF1_DAC1_FILTERS_1;
2350 break;
2351 case 2:
2352 mute_reg = WM8994_AIF2_DAC_FILTERS_1;
2353 break;
2354 default:
2355 return -EINVAL;
2356 }
2357
2358 if (mute)
2359 reg = WM8994_AIF1DAC1_MUTE;
2360 else
2361 reg = 0;
2362
2363 snd_soc_update_bits(codec, mute_reg, WM8994_AIF1DAC1_MUTE, reg);
2364
2365 return 0;
2366}
2367
Mark Brown778a76e2010-03-22 22:05:10 +00002368static int wm8994_set_tristate(struct snd_soc_dai *codec_dai, int tristate)
2369{
2370 struct snd_soc_codec *codec = codec_dai->codec;
2371 int reg, val, mask;
2372
2373 switch (codec_dai->id) {
2374 case 1:
2375 reg = WM8994_AIF1_MASTER_SLAVE;
2376 mask = WM8994_AIF1_TRI;
2377 break;
2378 case 2:
2379 reg = WM8994_AIF2_MASTER_SLAVE;
2380 mask = WM8994_AIF2_TRI;
2381 break;
2382 case 3:
2383 reg = WM8994_POWER_MANAGEMENT_6;
2384 mask = WM8994_AIF3_TRI;
2385 break;
2386 default:
2387 return -EINVAL;
2388 }
2389
2390 if (tristate)
2391 val = mask;
2392 else
2393 val = 0;
2394
Qiao Zhou78b3fb42011-01-19 19:10:47 +08002395 return snd_soc_update_bits(codec, reg, mask, val);
Mark Brown778a76e2010-03-22 22:05:10 +00002396}
2397
Mark Brown9e6e96a2010-01-29 17:47:12 +00002398#define WM8994_RATES SNDRV_PCM_RATE_8000_96000
2399
2400#define WM8994_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
Ian Lartey3079aed2010-08-31 23:56:34 +01002401 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002402
2403static struct snd_soc_dai_ops wm8994_aif1_dai_ops = {
2404 .set_sysclk = wm8994_set_dai_sysclk,
2405 .set_fmt = wm8994_set_dai_fmt,
2406 .hw_params = wm8994_hw_params,
2407 .digital_mute = wm8994_aif_mute,
2408 .set_pll = wm8994_set_fll,
Mark Brown778a76e2010-03-22 22:05:10 +00002409 .set_tristate = wm8994_set_tristate,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002410};
2411
2412static struct snd_soc_dai_ops wm8994_aif2_dai_ops = {
2413 .set_sysclk = wm8994_set_dai_sysclk,
2414 .set_fmt = wm8994_set_dai_fmt,
2415 .hw_params = wm8994_hw_params,
2416 .digital_mute = wm8994_aif_mute,
2417 .set_pll = wm8994_set_fll,
Mark Brown778a76e2010-03-22 22:05:10 +00002418 .set_tristate = wm8994_set_tristate,
2419};
2420
2421static struct snd_soc_dai_ops wm8994_aif3_dai_ops = {
Mark Brownc4431df2010-11-26 15:21:07 +00002422 .hw_params = wm8994_aif3_hw_params,
Mark Brown778a76e2010-03-22 22:05:10 +00002423 .set_tristate = wm8994_set_tristate,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002424};
2425
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002426static struct snd_soc_dai_driver wm8994_dai[] = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002427 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002428 .name = "wm8994-aif1",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002429 .id = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002430 .playback = {
2431 .stream_name = "AIF1 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002432 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002433 .channels_max = 2,
2434 .rates = WM8994_RATES,
2435 .formats = WM8994_FORMATS,
2436 },
2437 .capture = {
2438 .stream_name = "AIF1 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002439 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002440 .channels_max = 2,
2441 .rates = WM8994_RATES,
2442 .formats = WM8994_FORMATS,
2443 },
2444 .ops = &wm8994_aif1_dai_ops,
2445 },
2446 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002447 .name = "wm8994-aif2",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002448 .id = 2,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002449 .playback = {
2450 .stream_name = "AIF2 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002451 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002452 .channels_max = 2,
2453 .rates = WM8994_RATES,
2454 .formats = WM8994_FORMATS,
2455 },
2456 .capture = {
2457 .stream_name = "AIF2 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002458 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002459 .channels_max = 2,
2460 .rates = WM8994_RATES,
2461 .formats = WM8994_FORMATS,
2462 },
2463 .ops = &wm8994_aif2_dai_ops,
2464 },
2465 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002466 .name = "wm8994-aif3",
Mark Brown8c7f78b2010-10-12 15:56:09 +01002467 .id = 3,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002468 .playback = {
2469 .stream_name = "AIF3 Playback",
Mark Brownb1e43d92010-12-07 17:14:56 +00002470 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002471 .channels_max = 2,
2472 .rates = WM8994_RATES,
2473 .formats = WM8994_FORMATS,
2474 },
Dan Carpentera8462bd2010-03-24 14:58:34 +03002475 .capture = {
Mark Brown9e6e96a2010-01-29 17:47:12 +00002476 .stream_name = "AIF3 Capture",
Mark Brownb1e43d92010-12-07 17:14:56 +00002477 .channels_min = 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002478 .channels_max = 2,
2479 .rates = WM8994_RATES,
2480 .formats = WM8994_FORMATS,
2481 },
Mark Brown778a76e2010-03-22 22:05:10 +00002482 .ops = &wm8994_aif3_dai_ops,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002483 }
2484};
Mark Brown9e6e96a2010-01-29 17:47:12 +00002485
2486#ifdef CONFIG_PM
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002487static int wm8994_suspend(struct snd_soc_codec *codec, pm_message_t state)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002488{
Mark Brownb2c812e2010-04-14 15:35:19 +09002489 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002490 int i, ret;
2491
2492 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
2493 memcpy(&wm8994->fll_suspend[i], &wm8994->fll[i],
2494 sizeof(struct fll_config));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002495 ret = _wm8994_set_fll(codec, i + 1, 0, 0, 0);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002496 if (ret < 0)
2497 dev_warn(codec->dev, "Failed to stop FLL%d: %d\n",
2498 i + 1, ret);
2499 }
2500
2501 wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);
2502
2503 return 0;
2504}
2505
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002506static int wm8994_resume(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002507{
Mark Brownb2c812e2010-04-14 15:35:19 +09002508 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002509 int i, ret;
2510
2511 /* Restore the registers */
Mark Brownca9aef52010-11-26 17:23:41 +00002512 ret = snd_soc_cache_sync(codec);
2513 if (ret != 0)
2514 dev_err(codec->dev, "Failed to sync cache: %d\n", ret);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002515
2516 wm8994_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
2517
2518 for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
Mark Brown6a2f1ee2010-05-10 18:36:37 +01002519 if (!wm8994->fll_suspend[i].out)
2520 continue;
2521
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002522 ret = _wm8994_set_fll(codec, i + 1,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002523 wm8994->fll_suspend[i].src,
2524 wm8994->fll_suspend[i].in,
2525 wm8994->fll_suspend[i].out);
2526 if (ret < 0)
2527 dev_warn(codec->dev, "Failed to restore FLL%d: %d\n",
2528 i + 1, ret);
2529 }
2530
2531 return 0;
2532}
2533#else
2534#define wm8994_suspend NULL
2535#define wm8994_resume NULL
2536#endif
2537
2538static void wm8994_handle_retune_mobile_pdata(struct wm8994_priv *wm8994)
2539{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002540 struct snd_soc_codec *codec = wm8994->codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002541 struct wm8994_pdata *pdata = wm8994->pdata;
2542 struct snd_kcontrol_new controls[] = {
2543 SOC_ENUM_EXT("AIF1.1 EQ Mode",
2544 wm8994->retune_mobile_enum,
2545 wm8994_get_retune_mobile_enum,
2546 wm8994_put_retune_mobile_enum),
2547 SOC_ENUM_EXT("AIF1.2 EQ Mode",
2548 wm8994->retune_mobile_enum,
2549 wm8994_get_retune_mobile_enum,
2550 wm8994_put_retune_mobile_enum),
2551 SOC_ENUM_EXT("AIF2 EQ Mode",
2552 wm8994->retune_mobile_enum,
2553 wm8994_get_retune_mobile_enum,
2554 wm8994_put_retune_mobile_enum),
2555 };
2556 int ret, i, j;
2557 const char **t;
2558
2559 /* We need an array of texts for the enum API but the number
2560 * of texts is likely to be less than the number of
2561 * configurations due to the sample rate dependency of the
2562 * configurations. */
2563 wm8994->num_retune_mobile_texts = 0;
2564 wm8994->retune_mobile_texts = NULL;
2565 for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
2566 for (j = 0; j < wm8994->num_retune_mobile_texts; j++) {
2567 if (strcmp(pdata->retune_mobile_cfgs[i].name,
2568 wm8994->retune_mobile_texts[j]) == 0)
2569 break;
2570 }
2571
2572 if (j != wm8994->num_retune_mobile_texts)
2573 continue;
2574
2575 /* Expand the array... */
2576 t = krealloc(wm8994->retune_mobile_texts,
2577 sizeof(char *) *
2578 (wm8994->num_retune_mobile_texts + 1),
2579 GFP_KERNEL);
2580 if (t == NULL)
2581 continue;
2582
2583 /* ...store the new entry... */
2584 t[wm8994->num_retune_mobile_texts] =
2585 pdata->retune_mobile_cfgs[i].name;
2586
2587 /* ...and remember the new version. */
2588 wm8994->num_retune_mobile_texts++;
2589 wm8994->retune_mobile_texts = t;
2590 }
2591
2592 dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
2593 wm8994->num_retune_mobile_texts);
2594
2595 wm8994->retune_mobile_enum.max = wm8994->num_retune_mobile_texts;
2596 wm8994->retune_mobile_enum.texts = wm8994->retune_mobile_texts;
2597
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002598 ret = snd_soc_add_controls(wm8994->codec, controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002599 ARRAY_SIZE(controls));
2600 if (ret != 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002601 dev_err(wm8994->codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002602 "Failed to add ReTune Mobile controls: %d\n", ret);
2603}
2604
2605static void wm8994_handle_pdata(struct wm8994_priv *wm8994)
2606{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002607 struct snd_soc_codec *codec = wm8994->codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002608 struct wm8994_pdata *pdata = wm8994->pdata;
2609 int ret, i;
2610
2611 if (!pdata)
2612 return;
2613
2614 wm_hubs_handle_analogue_pdata(codec, pdata->lineout1_diff,
2615 pdata->lineout2_diff,
2616 pdata->lineout1fb,
2617 pdata->lineout2fb,
2618 pdata->jd_scthr,
2619 pdata->jd_thr,
2620 pdata->micbias1_lvl,
2621 pdata->micbias2_lvl);
2622
2623 dev_dbg(codec->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
2624
2625 if (pdata->num_drc_cfgs) {
2626 struct snd_kcontrol_new controls[] = {
2627 SOC_ENUM_EXT("AIF1DRC1 Mode", wm8994->drc_enum,
2628 wm8994_get_drc_enum, wm8994_put_drc_enum),
2629 SOC_ENUM_EXT("AIF1DRC2 Mode", wm8994->drc_enum,
2630 wm8994_get_drc_enum, wm8994_put_drc_enum),
2631 SOC_ENUM_EXT("AIF2DRC Mode", wm8994->drc_enum,
2632 wm8994_get_drc_enum, wm8994_put_drc_enum),
2633 };
2634
2635 /* We need an array of texts for the enum API */
2636 wm8994->drc_texts = kmalloc(sizeof(char *)
2637 * pdata->num_drc_cfgs, GFP_KERNEL);
2638 if (!wm8994->drc_texts) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002639 dev_err(wm8994->codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002640 "Failed to allocate %d DRC config texts\n",
2641 pdata->num_drc_cfgs);
2642 return;
2643 }
2644
2645 for (i = 0; i < pdata->num_drc_cfgs; i++)
2646 wm8994->drc_texts[i] = pdata->drc_cfgs[i].name;
2647
2648 wm8994->drc_enum.max = pdata->num_drc_cfgs;
2649 wm8994->drc_enum.texts = wm8994->drc_texts;
2650
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002651 ret = snd_soc_add_controls(wm8994->codec, controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002652 ARRAY_SIZE(controls));
2653 if (ret != 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002654 dev_err(wm8994->codec->dev,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002655 "Failed to add DRC mode controls: %d\n", ret);
2656
2657 for (i = 0; i < WM8994_NUM_DRC; i++)
2658 wm8994_set_drc(codec, i);
2659 }
2660
2661 dev_dbg(codec->dev, "%d ReTune Mobile configurations\n",
2662 pdata->num_retune_mobile_cfgs);
2663
Mark Brown131d8102010-11-30 17:03:39 +00002664 if (pdata->num_mbc_cfgs) {
2665 struct snd_kcontrol_new control[] = {
2666 SOC_ENUM_EXT("MBC Mode", wm8994->mbc_enum,
2667 wm8958_get_mbc_enum, wm8958_put_mbc_enum),
2668 };
2669
2670 /* We need an array of texts for the enum API */
2671 wm8994->mbc_texts = kmalloc(sizeof(char *)
2672 * pdata->num_mbc_cfgs, GFP_KERNEL);
2673 if (!wm8994->mbc_texts) {
2674 dev_err(wm8994->codec->dev,
2675 "Failed to allocate %d MBC config texts\n",
2676 pdata->num_mbc_cfgs);
2677 return;
2678 }
2679
2680 for (i = 0; i < pdata->num_mbc_cfgs; i++)
2681 wm8994->mbc_texts[i] = pdata->mbc_cfgs[i].name;
2682
2683 wm8994->mbc_enum.max = pdata->num_mbc_cfgs;
2684 wm8994->mbc_enum.texts = wm8994->mbc_texts;
2685
2686 ret = snd_soc_add_controls(wm8994->codec, control, 1);
2687 if (ret != 0)
2688 dev_err(wm8994->codec->dev,
2689 "Failed to add MBC mode controls: %d\n", ret);
2690 }
2691
Mark Brown9e6e96a2010-01-29 17:47:12 +00002692 if (pdata->num_retune_mobile_cfgs)
2693 wm8994_handle_retune_mobile_pdata(wm8994);
2694 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002695 snd_soc_add_controls(wm8994->codec, wm8994_eq_controls,
Mark Brown9e6e96a2010-01-29 17:47:12 +00002696 ARRAY_SIZE(wm8994_eq_controls));
2697}
2698
Mark Brown88766982010-03-29 20:57:12 +01002699/**
2700 * wm8994_mic_detect - Enable microphone detection via the WM8994 IRQ
2701 *
2702 * @codec: WM8994 codec
2703 * @jack: jack to report detection events on
2704 * @micbias: microphone bias to detect on
2705 * @det: value to report for presence detection
2706 * @shrt: value to report for short detection
2707 *
2708 * Enable microphone detection via IRQ on the WM8994. If GPIOs are
2709 * being used to bring out signals to the processor then only platform
Mark Brown5ab230a2010-09-06 14:59:34 +01002710 * data configuration is needed for WM8994 and processor GPIOs should
Mark Brown88766982010-03-29 20:57:12 +01002711 * be configured using snd_soc_jack_add_gpios() instead.
2712 *
2713 * Configuration of detection levels is available via the micbias1_lvl
2714 * and micbias2_lvl platform data members.
2715 */
2716int wm8994_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
2717 int micbias, int det, int shrt)
2718{
Mark Brownb2c812e2010-04-14 15:35:19 +09002719 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown88766982010-03-29 20:57:12 +01002720 struct wm8994_micdet *micdet;
Mark Brown3a423152010-11-26 15:21:06 +00002721 struct wm8994 *control = codec->control_data;
Mark Brown88766982010-03-29 20:57:12 +01002722 int reg;
2723
Mark Brown3a423152010-11-26 15:21:06 +00002724 if (control->type != WM8994)
2725 return -EINVAL;
2726
Mark Brown88766982010-03-29 20:57:12 +01002727 switch (micbias) {
2728 case 1:
2729 micdet = &wm8994->micdet[0];
2730 break;
2731 case 2:
2732 micdet = &wm8994->micdet[1];
2733 break;
2734 default:
2735 return -EINVAL;
2736 }
2737
2738 dev_dbg(codec->dev, "Configuring microphone detection on %d: %x %x\n",
2739 micbias, det, shrt);
2740
2741 /* Store the configuration */
2742 micdet->jack = jack;
2743 micdet->det = det;
2744 micdet->shrt = shrt;
2745
2746 /* If either of the jacks is set up then enable detection */
2747 if (wm8994->micdet[0].jack || wm8994->micdet[1].jack)
2748 reg = WM8994_MICD_ENA;
2749 else
2750 reg = 0;
2751
2752 snd_soc_update_bits(codec, WM8994_MICBIAS, WM8994_MICD_ENA, reg);
2753
2754 return 0;
2755}
2756EXPORT_SYMBOL_GPL(wm8994_mic_detect);
2757
2758static irqreturn_t wm8994_mic_irq(int irq, void *data)
2759{
2760 struct wm8994_priv *priv = data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002761 struct snd_soc_codec *codec = priv->codec;
Mark Brown88766982010-03-29 20:57:12 +01002762 int reg;
2763 int report;
2764
Mark Brown7116f452010-12-29 13:05:21 +00002765#ifndef CONFIG_SND_SOC_WM8994_MODULE
Mark Brown2bbb5d62010-12-05 12:50:12 +00002766 trace_snd_soc_jack_irq(dev_name(codec->dev));
Mark Brown7116f452010-12-29 13:05:21 +00002767#endif
Mark Brown2bbb5d62010-12-05 12:50:12 +00002768
Mark Brown88766982010-03-29 20:57:12 +01002769 reg = snd_soc_read(codec, WM8994_INTERRUPT_RAW_STATUS_2);
2770 if (reg < 0) {
2771 dev_err(codec->dev, "Failed to read microphone status: %d\n",
2772 reg);
2773 return IRQ_HANDLED;
2774 }
2775
2776 dev_dbg(codec->dev, "Microphone status: %x\n", reg);
2777
2778 report = 0;
2779 if (reg & WM8994_MIC1_DET_STS)
2780 report |= priv->micdet[0].det;
2781 if (reg & WM8994_MIC1_SHRT_STS)
2782 report |= priv->micdet[0].shrt;
2783 snd_soc_jack_report(priv->micdet[0].jack, report,
2784 priv->micdet[0].det | priv->micdet[0].shrt);
2785
2786 report = 0;
2787 if (reg & WM8994_MIC2_DET_STS)
2788 report |= priv->micdet[1].det;
2789 if (reg & WM8994_MIC2_SHRT_STS)
2790 report |= priv->micdet[1].shrt;
2791 snd_soc_jack_report(priv->micdet[1].jack, report,
2792 priv->micdet[1].det | priv->micdet[1].shrt);
2793
2794 return IRQ_HANDLED;
2795}
2796
Mark Brown821edd22010-11-26 15:21:09 +00002797/* Default microphone detection handler for WM8958 - the user can
2798 * override this if they wish.
2799 */
2800static void wm8958_default_micdet(u16 status, void *data)
2801{
2802 struct snd_soc_codec *codec = data;
2803 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2804 int report = 0;
2805
2806 /* If nothing present then clear our statuses */
2807 if (!(status & WM8958_MICD_STS)) {
2808 wm8994->jack_is_video = false;
2809 wm8994->jack_is_mic = false;
2810 goto done;
2811 }
2812
2813 /* Assume anything over 475 ohms is a microphone and remember
2814 * that we've seen one (since buttons override it) */
2815 if (status & 0x600)
2816 wm8994->jack_is_mic = true;
2817 if (wm8994->jack_is_mic)
2818 report |= SND_JACK_MICROPHONE;
2819
2820 /* Video has an impedence of approximately 75 ohms; assume
2821 * this isn't used as a button and remember it since buttons
2822 * override it. */
2823 if (status & 0x40)
2824 wm8994->jack_is_video = true;
2825 if (wm8994->jack_is_video)
2826 report |= SND_JACK_VIDEOOUT;
2827
2828 /* Everything else is buttons; just assign slots */
2829 if (status & 0x4)
2830 report |= SND_JACK_BTN_0;
2831 if (status & 0x8)
2832 report |= SND_JACK_BTN_1;
2833 if (status & 0x10)
2834 report |= SND_JACK_BTN_2;
2835 if (status & 0x20)
2836 report |= SND_JACK_BTN_3;
2837 if (status & 0x80)
2838 report |= SND_JACK_BTN_4;
2839 if (status & 0x100)
2840 report |= SND_JACK_BTN_5;
2841
2842done:
2843 snd_soc_jack_report(wm8994->micdet[0].jack,
2844 SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2 |
2845 SND_JACK_BTN_3 | SND_JACK_BTN_4 | SND_JACK_BTN_5 |
2846 SND_JACK_MICROPHONE | SND_JACK_VIDEOOUT,
2847 report);
2848}
2849
2850/**
2851 * wm8958_mic_detect - Enable microphone detection via the WM8958 IRQ
2852 *
2853 * @codec: WM8958 codec
2854 * @jack: jack to report detection events on
2855 *
2856 * Enable microphone detection functionality for the WM8958. By
2857 * default simple detection which supports the detection of up to 6
2858 * buttons plus video and microphone functionality is supported.
2859 *
2860 * The WM8958 has an advanced jack detection facility which is able to
2861 * support complex accessory detection, especially when used in
2862 * conjunction with external circuitry. In order to provide maximum
2863 * flexiblity a callback is provided which allows a completely custom
2864 * detection algorithm.
2865 */
2866int wm8958_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
2867 wm8958_micdet_cb cb, void *cb_data)
2868{
2869 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
2870 struct wm8994 *control = codec->control_data;
2871
2872 if (control->type != WM8958)
2873 return -EINVAL;
2874
2875 if (jack) {
2876 if (!cb) {
2877 dev_dbg(codec->dev, "Using default micdet callback\n");
2878 cb = wm8958_default_micdet;
2879 cb_data = codec;
2880 }
2881
2882 wm8994->micdet[0].jack = jack;
2883 wm8994->jack_cb = cb;
2884 wm8994->jack_cb_data = cb_data;
2885
2886 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
2887 WM8958_MICD_ENA, WM8958_MICD_ENA);
2888 } else {
2889 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
2890 WM8958_MICD_ENA, 0);
2891 }
2892
2893 return 0;
2894}
2895EXPORT_SYMBOL_GPL(wm8958_mic_detect);
2896
2897static irqreturn_t wm8958_mic_irq(int irq, void *data)
2898{
2899 struct wm8994_priv *wm8994 = data;
2900 struct snd_soc_codec *codec = wm8994->codec;
2901 int reg;
2902
2903 reg = snd_soc_read(codec, WM8958_MIC_DETECT_3);
2904 if (reg < 0) {
2905 dev_err(codec->dev, "Failed to read mic detect status: %d\n",
2906 reg);
2907 return IRQ_NONE;
2908 }
2909
2910 if (!(reg & WM8958_MICD_VALID)) {
2911 dev_dbg(codec->dev, "Mic detect data not valid\n");
2912 goto out;
2913 }
2914
Mark Brown7116f452010-12-29 13:05:21 +00002915#ifndef CONFIG_SND_SOC_WM8994_MODULE
Mark Brown2bbb5d62010-12-05 12:50:12 +00002916 trace_snd_soc_jack_irq(dev_name(codec->dev));
Mark Brown7116f452010-12-29 13:05:21 +00002917#endif
Mark Brown2bbb5d62010-12-05 12:50:12 +00002918
Mark Brown821edd22010-11-26 15:21:09 +00002919 if (wm8994->jack_cb)
2920 wm8994->jack_cb(reg, wm8994->jack_cb_data);
2921 else
2922 dev_warn(codec->dev, "Accessory detection with no callback\n");
2923
2924out:
2925 return IRQ_HANDLED;
2926}
2927
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002928static int wm8994_codec_probe(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002929{
Mark Brown3a423152010-11-26 15:21:06 +00002930 struct wm8994 *control;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002931 struct wm8994_priv *wm8994;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002932 struct snd_soc_dapm_context *dapm = &codec->dapm;
Mark Brownec62dbd2010-08-15 14:56:40 +01002933 int ret, i;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002934
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002935 codec->control_data = dev_get_drvdata(codec->dev->parent);
Mark Brown3a423152010-11-26 15:21:06 +00002936 control = codec->control_data;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002937
2938 wm8994 = kzalloc(sizeof(struct wm8994_priv), GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002939 if (wm8994 == NULL)
Mark Brown9e6e96a2010-01-29 17:47:12 +00002940 return -ENOMEM;
Mark Brownb2c812e2010-04-14 15:35:19 +09002941 snd_soc_codec_set_drvdata(codec, wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00002942
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002943 wm8994->pdata = dev_get_platdata(codec->dev->parent);
2944 wm8994->codec = codec;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002945
Mark Brown39fb51a2010-11-26 17:23:43 +00002946 pm_runtime_enable(codec->dev);
2947 pm_runtime_resume(codec->dev);
2948
Mark Brownca9aef52010-11-26 17:23:41 +00002949 /* Read our current status back from the chip - we don't want to
2950 * reset as this may interfere with the GPIO or LDO operation. */
2951 for (i = 0; i < WM8994_CACHE_SIZE; i++) {
2952 if (!wm8994_readable(i) || wm8994_volatile(i))
2953 continue;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002954
Mark Brownca9aef52010-11-26 17:23:41 +00002955 ret = wm8994_reg_read(codec->control_data, i);
2956 if (ret <= 0)
2957 continue;
2958
2959 ret = snd_soc_cache_write(codec, i, ret);
2960 if (ret != 0) {
2961 dev_err(codec->dev,
2962 "Failed to initialise cache for 0x%x: %d\n",
2963 i, ret);
2964 goto err;
2965 }
2966 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002967
2968 /* Set revision-specific configuration */
Mark Brownb6b05692010-08-13 12:58:20 +01002969 wm8994->revision = snd_soc_read(codec, WM8994_CHIP_REVISION);
Mark Brown3a423152010-11-26 15:21:06 +00002970 switch (control->type) {
2971 case WM8994:
2972 switch (wm8994->revision) {
2973 case 2:
2974 case 3:
2975 wm8994->hubs.dcs_codes = -5;
2976 wm8994->hubs.hp_startup_mode = 1;
2977 wm8994->hubs.dcs_readback_mode = 1;
2978 break;
2979 default:
2980 wm8994->hubs.dcs_readback_mode = 1;
2981 break;
2982 }
2983
2984 case WM8958:
Mark Brown8437f702010-03-29 17:09:45 +01002985 wm8994->hubs.dcs_readback_mode = 1;
Mark Brown9e6e96a2010-01-29 17:47:12 +00002986 break;
Mark Brown3a423152010-11-26 15:21:06 +00002987
Mark Brown9e6e96a2010-01-29 17:47:12 +00002988 default:
2989 break;
2990 }
Mark Brown9e6e96a2010-01-29 17:47:12 +00002991
Mark Brown3a423152010-11-26 15:21:06 +00002992 switch (control->type) {
2993 case WM8994:
2994 ret = wm8994_request_irq(codec->control_data,
2995 WM8994_IRQ_MIC1_DET,
2996 wm8994_mic_irq, "Mic 1 detect",
2997 wm8994);
2998 if (ret != 0)
2999 dev_warn(codec->dev,
3000 "Failed to request Mic1 detect IRQ: %d\n",
3001 ret);
Mark Brown88766982010-03-29 20:57:12 +01003002
Mark Brown3a423152010-11-26 15:21:06 +00003003 ret = wm8994_request_irq(codec->control_data,
3004 WM8994_IRQ_MIC1_SHRT,
3005 wm8994_mic_irq, "Mic 1 short",
3006 wm8994);
3007 if (ret != 0)
3008 dev_warn(codec->dev,
3009 "Failed to request Mic1 short IRQ: %d\n",
3010 ret);
Mark Brown88766982010-03-29 20:57:12 +01003011
Mark Brown3a423152010-11-26 15:21:06 +00003012 ret = wm8994_request_irq(codec->control_data,
3013 WM8994_IRQ_MIC2_DET,
3014 wm8994_mic_irq, "Mic 2 detect",
3015 wm8994);
3016 if (ret != 0)
3017 dev_warn(codec->dev,
3018 "Failed to request Mic2 detect IRQ: %d\n",
3019 ret);
Mark Brown88766982010-03-29 20:57:12 +01003020
Mark Brown3a423152010-11-26 15:21:06 +00003021 ret = wm8994_request_irq(codec->control_data,
3022 WM8994_IRQ_MIC2_SHRT,
3023 wm8994_mic_irq, "Mic 2 short",
3024 wm8994);
3025 if (ret != 0)
3026 dev_warn(codec->dev,
3027 "Failed to request Mic2 short IRQ: %d\n",
3028 ret);
3029 break;
Mark Brown821edd22010-11-26 15:21:09 +00003030
3031 case WM8958:
3032 ret = wm8994_request_irq(codec->control_data,
3033 WM8994_IRQ_MIC1_DET,
3034 wm8958_mic_irq, "Mic detect",
3035 wm8994);
3036 if (ret != 0)
3037 dev_warn(codec->dev,
3038 "Failed to request Mic detect IRQ: %d\n",
3039 ret);
3040 break;
Mark Brown3a423152010-11-26 15:21:06 +00003041 }
Mark Brown88766982010-03-29 20:57:12 +01003042
Mark Brown9e6e96a2010-01-29 17:47:12 +00003043 /* Remember if AIFnLRCLK is configured as a GPIO. This should be
3044 * configured on init - if a system wants to do this dynamically
3045 * at runtime we can deal with that then.
3046 */
3047 ret = wm8994_reg_read(codec->control_data, WM8994_GPIO_1);
3048 if (ret < 0) {
3049 dev_err(codec->dev, "Failed to read GPIO1 state: %d\n", ret);
Mark Brown88766982010-03-29 20:57:12 +01003050 goto err_irq;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003051 }
3052 if ((ret & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
3053 wm8994->lrclk_shared[0] = 1;
3054 wm8994_dai[0].symmetric_rates = 1;
3055 } else {
3056 wm8994->lrclk_shared[0] = 0;
3057 }
3058
3059 ret = wm8994_reg_read(codec->control_data, WM8994_GPIO_6);
3060 if (ret < 0) {
3061 dev_err(codec->dev, "Failed to read GPIO6 state: %d\n", ret);
Mark Brown88766982010-03-29 20:57:12 +01003062 goto err_irq;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003063 }
3064 if ((ret & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
3065 wm8994->lrclk_shared[1] = 1;
3066 wm8994_dai[1].symmetric_rates = 1;
3067 } else {
3068 wm8994->lrclk_shared[1] = 0;
3069 }
3070
Mark Brown9e6e96a2010-01-29 17:47:12 +00003071 wm8994_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
3072
Mark Brown9e6e96a2010-01-29 17:47:12 +00003073 /* Latch volume updates (right only; we always do left then right). */
3074 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_RIGHT_VOLUME,
3075 WM8994_AIF1DAC1_VU, WM8994_AIF1DAC1_VU);
3076 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_RIGHT_VOLUME,
3077 WM8994_AIF1DAC2_VU, WM8994_AIF1DAC2_VU);
3078 snd_soc_update_bits(codec, WM8994_AIF2_DAC_RIGHT_VOLUME,
3079 WM8994_AIF2DAC_VU, WM8994_AIF2DAC_VU);
3080 snd_soc_update_bits(codec, WM8994_AIF1_ADC1_RIGHT_VOLUME,
3081 WM8994_AIF1ADC1_VU, WM8994_AIF1ADC1_VU);
3082 snd_soc_update_bits(codec, WM8994_AIF1_ADC2_RIGHT_VOLUME,
3083 WM8994_AIF1ADC2_VU, WM8994_AIF1ADC2_VU);
3084 snd_soc_update_bits(codec, WM8994_AIF2_ADC_RIGHT_VOLUME,
3085 WM8994_AIF2ADC_VU, WM8994_AIF1ADC2_VU);
3086 snd_soc_update_bits(codec, WM8994_DAC1_RIGHT_VOLUME,
3087 WM8994_DAC1_VU, WM8994_DAC1_VU);
3088 snd_soc_update_bits(codec, WM8994_DAC2_RIGHT_VOLUME,
3089 WM8994_DAC2_VU, WM8994_DAC2_VU);
3090
3091 /* Set the low bit of the 3D stereo depth so TLV matches */
3092 snd_soc_update_bits(codec, WM8994_AIF1_DAC1_FILTERS_2,
3093 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT,
3094 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT);
3095 snd_soc_update_bits(codec, WM8994_AIF1_DAC2_FILTERS_2,
3096 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT,
3097 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT);
3098 snd_soc_update_bits(codec, WM8994_AIF2_DAC_FILTERS_2,
3099 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT,
3100 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT);
3101
Mark Brownd1ce6b22010-07-20 10:13:14 +01003102 /* Unconditionally enable AIF1 ADC TDM mode; it only affects
3103 * behaviour on idle TDM clock cycles. */
3104 snd_soc_update_bits(codec, WM8994_AIF1_CONTROL_1,
3105 WM8994_AIF1ADC_TDM, WM8994_AIF1ADC_TDM);
3106
Mark Brown9e6e96a2010-01-29 17:47:12 +00003107 wm8994_update_class_w(codec);
3108
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003109 wm8994_handle_pdata(wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003110
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003111 wm_hubs_add_analogue_controls(codec);
3112 snd_soc_add_controls(codec, wm8994_snd_controls,
3113 ARRAY_SIZE(wm8994_snd_controls));
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003114 snd_soc_dapm_new_controls(dapm, wm8994_dapm_widgets,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003115 ARRAY_SIZE(wm8994_dapm_widgets));
Mark Brownc4431df2010-11-26 15:21:07 +00003116
3117 switch (control->type) {
3118 case WM8994:
3119 snd_soc_dapm_new_controls(dapm, wm8994_specific_dapm_widgets,
3120 ARRAY_SIZE(wm8994_specific_dapm_widgets));
3121 break;
3122 case WM8958:
3123 snd_soc_add_controls(codec, wm8958_snd_controls,
3124 ARRAY_SIZE(wm8958_snd_controls));
3125 snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
3126 ARRAY_SIZE(wm8958_dapm_widgets));
3127 break;
3128 }
3129
3130
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003131 wm_hubs_add_analogue_routes(codec, 0, 0);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003132 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
Mark Brown9e6e96a2010-01-29 17:47:12 +00003133
Mark Brownc4431df2010-11-26 15:21:07 +00003134 switch (control->type) {
3135 case WM8994:
3136 snd_soc_dapm_add_routes(dapm, wm8994_intercon,
3137 ARRAY_SIZE(wm8994_intercon));
3138 break;
3139 case WM8958:
3140 snd_soc_dapm_add_routes(dapm, wm8958_intercon,
3141 ARRAY_SIZE(wm8958_intercon));
3142 break;
3143 }
3144
Mark Brown9e6e96a2010-01-29 17:47:12 +00003145 return 0;
3146
Mark Brown88766982010-03-29 20:57:12 +01003147err_irq:
3148 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC2_SHRT, wm8994);
3149 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC2_DET, wm8994);
3150 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC1_SHRT, wm8994);
3151 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC1_DET, wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003152err:
3153 kfree(wm8994);
3154 return ret;
3155}
3156
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003157static int wm8994_codec_remove(struct snd_soc_codec *codec)
Mark Brown9e6e96a2010-01-29 17:47:12 +00003158{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003159 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
Mark Brown3a423152010-11-26 15:21:06 +00003160 struct wm8994 *control = codec->control_data;
Mark Brown9e6e96a2010-01-29 17:47:12 +00003161
3162 wm8994_set_bias_level(codec, SND_SOC_BIAS_OFF);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003163
Mark Brown39fb51a2010-11-26 17:23:43 +00003164 pm_runtime_disable(codec->dev);
3165
Mark Brown3a423152010-11-26 15:21:06 +00003166 switch (control->type) {
3167 case WM8994:
3168 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC2_SHRT,
3169 wm8994);
3170 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC2_DET,
3171 wm8994);
3172 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC1_SHRT,
3173 wm8994);
3174 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC1_DET,
3175 wm8994);
3176 break;
Mark Brown821edd22010-11-26 15:21:09 +00003177
3178 case WM8958:
3179 wm8994_free_irq(codec->control_data, WM8994_IRQ_MIC1_DET,
3180 wm8994);
3181 break;
Mark Brown3a423152010-11-26 15:21:06 +00003182 }
Axel Lin24fb2b12010-11-23 15:58:39 +08003183 kfree(wm8994->retune_mobile_texts);
3184 kfree(wm8994->drc_texts);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003185 kfree(wm8994);
Mark Brown9e6e96a2010-01-29 17:47:12 +00003186
3187 return 0;
3188}
3189
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003190static struct snd_soc_codec_driver soc_codec_dev_wm8994 = {
3191 .probe = wm8994_codec_probe,
3192 .remove = wm8994_codec_remove,
3193 .suspend = wm8994_suspend,
3194 .resume = wm8994_resume,
Mark Brownca9aef52010-11-26 17:23:41 +00003195 .read = wm8994_read,
3196 .write = wm8994_write,
Mark Browneba19fd2010-11-19 16:09:15 +00003197 .readable_register = wm8994_readable,
3198 .volatile_register = wm8994_volatile,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003199 .set_bias_level = wm8994_set_bias_level,
Mark Brownca9aef52010-11-26 17:23:41 +00003200
3201 .reg_cache_size = WM8994_CACHE_SIZE,
3202 .reg_cache_default = wm8994_reg_defaults,
3203 .reg_word_size = 2,
Mark Brown2e19b0c2010-11-26 17:23:42 +00003204 .compress_type = SND_SOC_RBTREE_COMPRESSION,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003205};
3206
3207static int __devinit wm8994_probe(struct platform_device *pdev)
3208{
3209 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wm8994,
3210 wm8994_dai, ARRAY_SIZE(wm8994_dai));
3211}
3212
3213static int __devexit wm8994_remove(struct platform_device *pdev)
3214{
3215 snd_soc_unregister_codec(&pdev->dev);
3216 return 0;
3217}
3218
Mark Brown9e6e96a2010-01-29 17:47:12 +00003219static struct platform_driver wm8994_codec_driver = {
3220 .driver = {
3221 .name = "wm8994-codec",
3222 .owner = THIS_MODULE,
3223 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003224 .probe = wm8994_probe,
3225 .remove = __devexit_p(wm8994_remove),
Mark Brown9e6e96a2010-01-29 17:47:12 +00003226};
3227
3228static __init int wm8994_init(void)
3229{
3230 return platform_driver_register(&wm8994_codec_driver);
3231}
3232module_init(wm8994_init);
3233
3234static __exit void wm8994_exit(void)
3235{
3236 platform_driver_unregister(&wm8994_codec_driver);
3237}
3238module_exit(wm8994_exit);
3239
3240
3241MODULE_DESCRIPTION("ASoC WM8994 driver");
3242MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
3243MODULE_LICENSE("GPL");
3244MODULE_ALIAS("platform:wm8994-codec");