Full support for Ginger Console
[linux-ginger.git] / sound / soc / codecs / wm9081.c
blob686e5aa972067106f1f88056c5d367020a0c1356
1 /*
2 * wm9081.c -- WM9081 ALSA SoC Audio driver
4 * Author: Mark Brown
6 * Copyright 2009 Wolfson Microelectronics plc
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
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>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/soc-dapm.h>
26 #include <sound/initval.h>
27 #include <sound/tlv.h>
29 #include <sound/wm9081.h>
30 #include "wm9081.h"
32 static u16 wm9081_reg_defaults[] = {
33 0x0000, /* R0 - Software Reset */
34 0x0000, /* R1 */
35 0x00B9, /* R2 - Analogue Lineout */
36 0x00B9, /* R3 - Analogue Speaker PGA */
37 0x0001, /* R4 - VMID Control */
38 0x0068, /* R5 - Bias Control 1 */
39 0x0000, /* R6 */
40 0x0000, /* R7 - Analogue Mixer */
41 0x0000, /* R8 - Anti Pop Control */
42 0x01DB, /* R9 - Analogue Speaker 1 */
43 0x0018, /* R10 - Analogue Speaker 2 */
44 0x0180, /* R11 - Power Management */
45 0x0000, /* R12 - Clock Control 1 */
46 0x0038, /* R13 - Clock Control 2 */
47 0x4000, /* R14 - Clock Control 3 */
48 0x0000, /* R15 */
49 0x0000, /* R16 - FLL Control 1 */
50 0x0200, /* R17 - FLL Control 2 */
51 0x0000, /* R18 - FLL Control 3 */
52 0x0204, /* R19 - FLL Control 4 */
53 0x0000, /* R20 - FLL Control 5 */
54 0x0000, /* R21 */
55 0x0000, /* R22 - Audio Interface 1 */
56 0x0002, /* R23 - Audio Interface 2 */
57 0x0008, /* R24 - Audio Interface 3 */
58 0x0022, /* R25 - Audio Interface 4 */
59 0x0000, /* R26 - Interrupt Status */
60 0x0006, /* R27 - Interrupt Status Mask */
61 0x0000, /* R28 - Interrupt Polarity */
62 0x0000, /* R29 - Interrupt Control */
63 0x00C0, /* R30 - DAC Digital 1 */
64 0x0008, /* R31 - DAC Digital 2 */
65 0x09AF, /* R32 - DRC 1 */
66 0x4201, /* R33 - DRC 2 */
67 0x0000, /* R34 - DRC 3 */
68 0x0000, /* R35 - DRC 4 */
69 0x0000, /* R36 */
70 0x0000, /* R37 */
71 0x0000, /* R38 - Write Sequencer 1 */
72 0x0000, /* R39 - Write Sequencer 2 */
73 0x0002, /* R40 - MW Slave 1 */
74 0x0000, /* R41 */
75 0x0000, /* R42 - EQ 1 */
76 0x0000, /* R43 - EQ 2 */
77 0x0FCA, /* R44 - EQ 3 */
78 0x0400, /* R45 - EQ 4 */
79 0x00B8, /* R46 - EQ 5 */
80 0x1EB5, /* R47 - EQ 6 */
81 0xF145, /* R48 - EQ 7 */
82 0x0B75, /* R49 - EQ 8 */
83 0x01C5, /* R50 - EQ 9 */
84 0x169E, /* R51 - EQ 10 */
85 0xF829, /* R52 - EQ 11 */
86 0x07AD, /* R53 - EQ 12 */
87 0x1103, /* R54 - EQ 13 */
88 0x1C58, /* R55 - EQ 14 */
89 0xF373, /* R56 - EQ 15 */
90 0x0A54, /* R57 - EQ 16 */
91 0x0558, /* R58 - EQ 17 */
92 0x0564, /* R59 - EQ 18 */
93 0x0559, /* R60 - EQ 19 */
94 0x4000, /* R61 - EQ 20 */
97 static struct {
98 int ratio;
99 int clk_sys_rate;
100 } clk_sys_rates[] = {
101 { 64, 0 },
102 { 128, 1 },
103 { 192, 2 },
104 { 256, 3 },
105 { 384, 4 },
106 { 512, 5 },
107 { 768, 6 },
108 { 1024, 7 },
109 { 1408, 8 },
110 { 1536, 9 },
113 static struct {
114 int rate;
115 int sample_rate;
116 } sample_rates[] = {
117 { 8000, 0 },
118 { 11025, 1 },
119 { 12000, 2 },
120 { 16000, 3 },
121 { 22050, 4 },
122 { 24000, 5 },
123 { 32000, 6 },
124 { 44100, 7 },
125 { 48000, 8 },
126 { 88200, 9 },
127 { 96000, 10 },
130 static struct {
131 int div; /* *10 due to .5s */
132 int bclk_div;
133 } bclk_divs[] = {
134 { 10, 0 },
135 { 15, 1 },
136 { 20, 2 },
137 { 30, 3 },
138 { 40, 4 },
139 { 50, 5 },
140 { 55, 6 },
141 { 60, 7 },
142 { 80, 8 },
143 { 100, 9 },
144 { 110, 10 },
145 { 120, 11 },
146 { 160, 12 },
147 { 200, 13 },
148 { 220, 14 },
149 { 240, 15 },
150 { 250, 16 },
151 { 300, 17 },
152 { 320, 18 },
153 { 440, 19 },
154 { 480, 20 },
157 struct wm9081_priv {
158 struct snd_soc_codec codec;
159 u16 reg_cache[WM9081_MAX_REGISTER + 1];
160 int sysclk_source;
161 int mclk_rate;
162 int sysclk_rate;
163 int fs;
164 int bclk;
165 int master;
166 int fll_fref;
167 int fll_fout;
168 int tdm_width;
169 struct wm9081_retune_mobile_config *retune;
172 static int wm9081_volatile_register(unsigned int reg)
174 switch (reg) {
175 case WM9081_SOFTWARE_RESET:
176 return 1;
177 default:
178 return 0;
182 static int wm9081_reset(struct snd_soc_codec *codec)
184 return snd_soc_write(codec, WM9081_SOFTWARE_RESET, 0);
187 static const DECLARE_TLV_DB_SCALE(drc_in_tlv, -4500, 75, 0);
188 static const DECLARE_TLV_DB_SCALE(drc_out_tlv, -2250, 75, 0);
189 static const DECLARE_TLV_DB_SCALE(drc_min_tlv, -1800, 600, 0);
190 static unsigned int drc_max_tlv[] = {
191 TLV_DB_RANGE_HEAD(4),
192 0, 0, TLV_DB_SCALE_ITEM(1200, 0, 0),
193 1, 1, TLV_DB_SCALE_ITEM(1800, 0, 0),
194 2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
195 3, 3, TLV_DB_SCALE_ITEM(3600, 0, 0),
197 static const DECLARE_TLV_DB_SCALE(drc_qr_tlv, 1200, 600, 0);
198 static const DECLARE_TLV_DB_SCALE(drc_startup_tlv, -300, 50, 0);
200 static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
202 static const DECLARE_TLV_DB_SCALE(in_tlv, -600, 600, 0);
203 static const DECLARE_TLV_DB_SCALE(dac_tlv, -7200, 75, 1);
204 static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
206 static const char *drc_high_text[] = {
207 "1",
208 "1/2",
209 "1/4",
210 "1/8",
211 "1/16",
212 "0",
215 static const struct soc_enum drc_high =
216 SOC_ENUM_SINGLE(WM9081_DRC_3, 3, 6, drc_high_text);
218 static const char *drc_low_text[] = {
219 "1",
220 "1/2",
221 "1/4",
222 "1/8",
223 "0",
226 static const struct soc_enum drc_low =
227 SOC_ENUM_SINGLE(WM9081_DRC_3, 0, 5, drc_low_text);
229 static const char *drc_atk_text[] = {
230 "181us",
231 "181us",
232 "363us",
233 "726us",
234 "1.45ms",
235 "2.9ms",
236 "5.8ms",
237 "11.6ms",
238 "23.2ms",
239 "46.4ms",
240 "92.8ms",
241 "185.6ms",
244 static const struct soc_enum drc_atk =
245 SOC_ENUM_SINGLE(WM9081_DRC_2, 12, 12, drc_atk_text);
247 static const char *drc_dcy_text[] = {
248 "186ms",
249 "372ms",
250 "743ms",
251 "1.49s",
252 "2.97s",
253 "5.94s",
254 "11.89s",
255 "23.78s",
256 "47.56s",
259 static const struct soc_enum drc_dcy =
260 SOC_ENUM_SINGLE(WM9081_DRC_2, 8, 9, drc_dcy_text);
262 static const char *drc_qr_dcy_text[] = {
263 "0.725ms",
264 "1.45ms",
265 "5.8ms",
268 static const struct soc_enum drc_qr_dcy =
269 SOC_ENUM_SINGLE(WM9081_DRC_2, 4, 3, drc_qr_dcy_text);
271 static const char *dac_deemph_text[] = {
272 "None",
273 "32kHz",
274 "44.1kHz",
275 "48kHz",
278 static const struct soc_enum dac_deemph =
279 SOC_ENUM_SINGLE(WM9081_DAC_DIGITAL_2, 1, 4, dac_deemph_text);
281 static const char *speaker_mode_text[] = {
282 "Class D",
283 "Class AB",
286 static const struct soc_enum speaker_mode =
287 SOC_ENUM_SINGLE(WM9081_ANALOGUE_SPEAKER_2, 6, 2, speaker_mode_text);
289 static int speaker_mode_get(struct snd_kcontrol *kcontrol,
290 struct snd_ctl_elem_value *ucontrol)
292 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
293 unsigned int reg;
295 reg = snd_soc_read(codec, WM9081_ANALOGUE_SPEAKER_2);
296 if (reg & WM9081_SPK_MODE)
297 ucontrol->value.integer.value[0] = 1;
298 else
299 ucontrol->value.integer.value[0] = 0;
301 return 0;
305 * Stop any attempts to change speaker mode while the speaker is enabled.
307 * We also have some special anti-pop controls dependant on speaker
308 * mode which must be changed along with the mode.
310 static int speaker_mode_put(struct snd_kcontrol *kcontrol,
311 struct snd_ctl_elem_value *ucontrol)
313 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
314 unsigned int reg_pwr = snd_soc_read(codec, WM9081_POWER_MANAGEMENT);
315 unsigned int reg2 = snd_soc_read(codec, WM9081_ANALOGUE_SPEAKER_2);
317 /* Are we changing anything? */
318 if (ucontrol->value.integer.value[0] ==
319 ((reg2 & WM9081_SPK_MODE) != 0))
320 return 0;
322 /* Don't try to change modes while enabled */
323 if (reg_pwr & WM9081_SPK_ENA)
324 return -EINVAL;
326 if (ucontrol->value.integer.value[0]) {
327 /* Class AB */
328 reg2 &= ~(WM9081_SPK_INV_MUTE | WM9081_OUT_SPK_CTRL);
329 reg2 |= WM9081_SPK_MODE;
330 } else {
331 /* Class D */
332 reg2 |= WM9081_SPK_INV_MUTE | WM9081_OUT_SPK_CTRL;
333 reg2 &= ~WM9081_SPK_MODE;
336 snd_soc_write(codec, WM9081_ANALOGUE_SPEAKER_2, reg2);
338 return 0;
341 static const struct snd_kcontrol_new wm9081_snd_controls[] = {
342 SOC_SINGLE_TLV("IN1 Volume", WM9081_ANALOGUE_MIXER, 1, 1, 1, in_tlv),
343 SOC_SINGLE_TLV("IN2 Volume", WM9081_ANALOGUE_MIXER, 3, 1, 1, in_tlv),
345 SOC_SINGLE_TLV("Playback Volume", WM9081_DAC_DIGITAL_1, 1, 96, 0, dac_tlv),
347 SOC_SINGLE("LINEOUT Switch", WM9081_ANALOGUE_LINEOUT, 7, 1, 1),
348 SOC_SINGLE("LINEOUT ZC Switch", WM9081_ANALOGUE_LINEOUT, 6, 1, 0),
349 SOC_SINGLE_TLV("LINEOUT Volume", WM9081_ANALOGUE_LINEOUT, 0, 63, 0, out_tlv),
351 SOC_SINGLE("DRC Switch", WM9081_DRC_1, 15, 1, 0),
352 SOC_ENUM("DRC High Slope", drc_high),
353 SOC_ENUM("DRC Low Slope", drc_low),
354 SOC_SINGLE_TLV("DRC Input Volume", WM9081_DRC_4, 5, 60, 1, drc_in_tlv),
355 SOC_SINGLE_TLV("DRC Output Volume", WM9081_DRC_4, 0, 30, 1, drc_out_tlv),
356 SOC_SINGLE_TLV("DRC Minimum Volume", WM9081_DRC_2, 2, 3, 1, drc_min_tlv),
357 SOC_SINGLE_TLV("DRC Maximum Volume", WM9081_DRC_2, 0, 3, 0, drc_max_tlv),
358 SOC_ENUM("DRC Attack", drc_atk),
359 SOC_ENUM("DRC Decay", drc_dcy),
360 SOC_SINGLE("DRC Quick Release Switch", WM9081_DRC_1, 2, 1, 0),
361 SOC_SINGLE_TLV("DRC Quick Release Volume", WM9081_DRC_2, 6, 3, 0, drc_qr_tlv),
362 SOC_ENUM("DRC Quick Release Decay", drc_qr_dcy),
363 SOC_SINGLE_TLV("DRC Startup Volume", WM9081_DRC_1, 6, 18, 0, drc_startup_tlv),
365 SOC_SINGLE("EQ Switch", WM9081_EQ_1, 0, 1, 0),
367 SOC_SINGLE("Speaker DC Volume", WM9081_ANALOGUE_SPEAKER_1, 3, 5, 0),
368 SOC_SINGLE("Speaker AC Volume", WM9081_ANALOGUE_SPEAKER_1, 0, 5, 0),
369 SOC_SINGLE("Speaker Switch", WM9081_ANALOGUE_SPEAKER_PGA, 7, 1, 1),
370 SOC_SINGLE("Speaker ZC Switch", WM9081_ANALOGUE_SPEAKER_PGA, 6, 1, 0),
371 SOC_SINGLE_TLV("Speaker Volume", WM9081_ANALOGUE_SPEAKER_PGA, 0, 63, 0,
372 out_tlv),
373 SOC_ENUM("DAC Deemphasis", dac_deemph),
374 SOC_ENUM_EXT("Speaker Mode", speaker_mode, speaker_mode_get, speaker_mode_put),
377 static const struct snd_kcontrol_new wm9081_eq_controls[] = {
378 SOC_SINGLE_TLV("EQ1 Volume", WM9081_EQ_1, 11, 24, 0, eq_tlv),
379 SOC_SINGLE_TLV("EQ2 Volume", WM9081_EQ_1, 6, 24, 0, eq_tlv),
380 SOC_SINGLE_TLV("EQ3 Volume", WM9081_EQ_1, 1, 24, 0, eq_tlv),
381 SOC_SINGLE_TLV("EQ4 Volume", WM9081_EQ_2, 11, 24, 0, eq_tlv),
382 SOC_SINGLE_TLV("EQ5 Volume", WM9081_EQ_2, 6, 24, 0, eq_tlv),
385 static const struct snd_kcontrol_new mixer[] = {
386 SOC_DAPM_SINGLE("IN1 Switch", WM9081_ANALOGUE_MIXER, 0, 1, 0),
387 SOC_DAPM_SINGLE("IN2 Switch", WM9081_ANALOGUE_MIXER, 2, 1, 0),
388 SOC_DAPM_SINGLE("Playback Switch", WM9081_ANALOGUE_MIXER, 4, 1, 0),
391 static int speaker_event(struct snd_soc_dapm_widget *w,
392 struct snd_kcontrol *kcontrol, int event)
394 struct snd_soc_codec *codec = w->codec;
395 unsigned int reg = snd_soc_read(codec, WM9081_POWER_MANAGEMENT);
397 switch (event) {
398 case SND_SOC_DAPM_POST_PMU:
399 reg |= WM9081_SPK_ENA;
400 break;
402 case SND_SOC_DAPM_PRE_PMD:
403 reg &= ~WM9081_SPK_ENA;
404 break;
407 snd_soc_write(codec, WM9081_POWER_MANAGEMENT, reg);
409 return 0;
412 struct _fll_div {
413 u16 fll_fratio;
414 u16 fll_outdiv;
415 u16 fll_clk_ref_div;
416 u16 n;
417 u16 k;
420 /* The size in bits of the FLL divide multiplied by 10
421 * to allow rounding later */
422 #define FIXED_FLL_SIZE ((1 << 16) * 10)
424 static struct {
425 unsigned int min;
426 unsigned int max;
427 u16 fll_fratio;
428 int ratio;
429 } fll_fratios[] = {
430 { 0, 64000, 4, 16 },
431 { 64000, 128000, 3, 8 },
432 { 128000, 256000, 2, 4 },
433 { 256000, 1000000, 1, 2 },
434 { 1000000, 13500000, 0, 1 },
437 static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
438 unsigned int Fout)
440 u64 Kpart;
441 unsigned int K, Ndiv, Nmod, target;
442 unsigned int div;
443 int i;
445 /* Fref must be <=13.5MHz */
446 div = 1;
447 while ((Fref / div) > 13500000) {
448 div *= 2;
450 if (div > 8) {
451 pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
452 Fref);
453 return -EINVAL;
456 fll_div->fll_clk_ref_div = div / 2;
458 pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
460 /* Apply the division for our remaining calculations */
461 Fref /= div;
463 /* Fvco should be 90-100MHz; don't check the upper bound */
464 div = 0;
465 target = Fout * 2;
466 while (target < 90000000) {
467 div++;
468 target *= 2;
469 if (div > 7) {
470 pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
471 Fout);
472 return -EINVAL;
475 fll_div->fll_outdiv = div;
477 pr_debug("Fvco=%dHz\n", target);
479 /* Find an appropraite FLL_FRATIO and factor it out of the target */
480 for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
481 if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
482 fll_div->fll_fratio = fll_fratios[i].fll_fratio;
483 target /= fll_fratios[i].ratio;
484 break;
487 if (i == ARRAY_SIZE(fll_fratios)) {
488 pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
489 return -EINVAL;
492 /* Now, calculate N.K */
493 Ndiv = target / Fref;
495 fll_div->n = Ndiv;
496 Nmod = target % Fref;
497 pr_debug("Nmod=%d\n", Nmod);
499 /* Calculate fractional part - scale up so we can round. */
500 Kpart = FIXED_FLL_SIZE * (long long)Nmod;
502 do_div(Kpart, Fref);
504 K = Kpart & 0xFFFFFFFF;
506 if ((K % 10) >= 5)
507 K += 5;
509 /* Move down to proper range now rounding is done */
510 fll_div->k = K / 10;
512 pr_debug("N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
513 fll_div->n, fll_div->k,
514 fll_div->fll_fratio, fll_div->fll_outdiv,
515 fll_div->fll_clk_ref_div);
517 return 0;
520 static int wm9081_set_fll(struct snd_soc_codec *codec, int fll_id,
521 unsigned int Fref, unsigned int Fout)
523 struct wm9081_priv *wm9081 = codec->private_data;
524 u16 reg1, reg4, reg5;
525 struct _fll_div fll_div;
526 int ret;
527 int clk_sys_reg;
529 /* Any change? */
530 if (Fref == wm9081->fll_fref && Fout == wm9081->fll_fout)
531 return 0;
533 /* Disable the FLL */
534 if (Fout == 0) {
535 dev_dbg(codec->dev, "FLL disabled\n");
536 wm9081->fll_fref = 0;
537 wm9081->fll_fout = 0;
539 return 0;
542 ret = fll_factors(&fll_div, Fref, Fout);
543 if (ret != 0)
544 return ret;
546 reg5 = snd_soc_read(codec, WM9081_FLL_CONTROL_5);
547 reg5 &= ~WM9081_FLL_CLK_SRC_MASK;
549 switch (fll_id) {
550 case WM9081_SYSCLK_FLL_MCLK:
551 reg5 |= 0x1;
552 break;
554 default:
555 dev_err(codec->dev, "Unknown FLL ID %d\n", fll_id);
556 return -EINVAL;
559 /* Disable CLK_SYS while we reconfigure */
560 clk_sys_reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_3);
561 if (clk_sys_reg & WM9081_CLK_SYS_ENA)
562 snd_soc_write(codec, WM9081_CLOCK_CONTROL_3,
563 clk_sys_reg & ~WM9081_CLK_SYS_ENA);
565 /* Any FLL configuration change requires that the FLL be
566 * disabled first. */
567 reg1 = snd_soc_read(codec, WM9081_FLL_CONTROL_1);
568 reg1 &= ~WM9081_FLL_ENA;
569 snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1);
571 /* Apply the configuration */
572 if (fll_div.k)
573 reg1 |= WM9081_FLL_FRAC_MASK;
574 else
575 reg1 &= ~WM9081_FLL_FRAC_MASK;
576 snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1);
578 snd_soc_write(codec, WM9081_FLL_CONTROL_2,
579 (fll_div.fll_outdiv << WM9081_FLL_OUTDIV_SHIFT) |
580 (fll_div.fll_fratio << WM9081_FLL_FRATIO_SHIFT));
581 snd_soc_write(codec, WM9081_FLL_CONTROL_3, fll_div.k);
583 reg4 = snd_soc_read(codec, WM9081_FLL_CONTROL_4);
584 reg4 &= ~WM9081_FLL_N_MASK;
585 reg4 |= fll_div.n << WM9081_FLL_N_SHIFT;
586 snd_soc_write(codec, WM9081_FLL_CONTROL_4, reg4);
588 reg5 &= ~WM9081_FLL_CLK_REF_DIV_MASK;
589 reg5 |= fll_div.fll_clk_ref_div << WM9081_FLL_CLK_REF_DIV_SHIFT;
590 snd_soc_write(codec, WM9081_FLL_CONTROL_5, reg5);
592 /* Enable the FLL */
593 snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1 | WM9081_FLL_ENA);
595 /* Then bring CLK_SYS up again if it was disabled */
596 if (clk_sys_reg & WM9081_CLK_SYS_ENA)
597 snd_soc_write(codec, WM9081_CLOCK_CONTROL_3, clk_sys_reg);
599 dev_dbg(codec->dev, "FLL enabled at %dHz->%dHz\n", Fref, Fout);
601 wm9081->fll_fref = Fref;
602 wm9081->fll_fout = Fout;
604 return 0;
607 static int configure_clock(struct snd_soc_codec *codec)
609 struct wm9081_priv *wm9081 = codec->private_data;
610 int new_sysclk, i, target;
611 unsigned int reg;
612 int ret = 0;
613 int mclkdiv = 0;
614 int fll = 0;
616 switch (wm9081->sysclk_source) {
617 case WM9081_SYSCLK_MCLK:
618 if (wm9081->mclk_rate > 12225000) {
619 mclkdiv = 1;
620 wm9081->sysclk_rate = wm9081->mclk_rate / 2;
621 } else {
622 wm9081->sysclk_rate = wm9081->mclk_rate;
624 wm9081_set_fll(codec, WM9081_SYSCLK_FLL_MCLK, 0, 0);
625 break;
627 case WM9081_SYSCLK_FLL_MCLK:
628 /* If we have a sample rate calculate a CLK_SYS that
629 * gives us a suitable DAC configuration, plus BCLK.
630 * Ideally we would check to see if we can clock
631 * directly from MCLK and only use the FLL if this is
632 * not the case, though care must be taken with free
633 * running mode.
635 if (wm9081->master && wm9081->bclk) {
636 /* Make sure we can generate CLK_SYS and BCLK
637 * and that we've got 3MHz for optimal
638 * performance. */
639 for (i = 0; i < ARRAY_SIZE(clk_sys_rates); i++) {
640 target = wm9081->fs * clk_sys_rates[i].ratio;
641 new_sysclk = target;
642 if (target >= wm9081->bclk &&
643 target > 3000000)
644 break;
647 if (i == ARRAY_SIZE(clk_sys_rates))
648 return -EINVAL;
650 } else if (wm9081->fs) {
651 for (i = 0; i < ARRAY_SIZE(clk_sys_rates); i++) {
652 new_sysclk = clk_sys_rates[i].ratio
653 * wm9081->fs;
654 if (new_sysclk > 3000000)
655 break;
658 if (i == ARRAY_SIZE(clk_sys_rates))
659 return -EINVAL;
661 } else {
662 new_sysclk = 12288000;
665 ret = wm9081_set_fll(codec, WM9081_SYSCLK_FLL_MCLK,
666 wm9081->mclk_rate, new_sysclk);
667 if (ret == 0) {
668 wm9081->sysclk_rate = new_sysclk;
670 /* Switch SYSCLK over to FLL */
671 fll = 1;
672 } else {
673 wm9081->sysclk_rate = wm9081->mclk_rate;
675 break;
677 default:
678 return -EINVAL;
681 reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_1);
682 if (mclkdiv)
683 reg |= WM9081_MCLKDIV2;
684 else
685 reg &= ~WM9081_MCLKDIV2;
686 snd_soc_write(codec, WM9081_CLOCK_CONTROL_1, reg);
688 reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_3);
689 if (fll)
690 reg |= WM9081_CLK_SRC_SEL;
691 else
692 reg &= ~WM9081_CLK_SRC_SEL;
693 snd_soc_write(codec, WM9081_CLOCK_CONTROL_3, reg);
695 dev_dbg(codec->dev, "CLK_SYS is %dHz\n", wm9081->sysclk_rate);
697 return ret;
700 static int clk_sys_event(struct snd_soc_dapm_widget *w,
701 struct snd_kcontrol *kcontrol, int event)
703 struct snd_soc_codec *codec = w->codec;
704 struct wm9081_priv *wm9081 = codec->private_data;
706 /* This should be done on init() for bypass paths */
707 switch (wm9081->sysclk_source) {
708 case WM9081_SYSCLK_MCLK:
709 dev_dbg(codec->dev, "Using %dHz MCLK\n", wm9081->mclk_rate);
710 break;
711 case WM9081_SYSCLK_FLL_MCLK:
712 dev_dbg(codec->dev, "Using %dHz MCLK with FLL\n",
713 wm9081->mclk_rate);
714 break;
715 default:
716 dev_err(codec->dev, "System clock not configured\n");
717 return -EINVAL;
720 switch (event) {
721 case SND_SOC_DAPM_PRE_PMU:
722 configure_clock(codec);
723 break;
725 case SND_SOC_DAPM_POST_PMD:
726 /* Disable the FLL if it's running */
727 wm9081_set_fll(codec, 0, 0, 0);
728 break;
731 return 0;
734 static const struct snd_soc_dapm_widget wm9081_dapm_widgets[] = {
735 SND_SOC_DAPM_INPUT("IN1"),
736 SND_SOC_DAPM_INPUT("IN2"),
738 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM9081_POWER_MANAGEMENT, 0, 0),
740 SND_SOC_DAPM_MIXER_NAMED_CTL("Mixer", SND_SOC_NOPM, 0, 0,
741 mixer, ARRAY_SIZE(mixer)),
743 SND_SOC_DAPM_PGA("LINEOUT PGA", WM9081_POWER_MANAGEMENT, 4, 0, NULL, 0),
745 SND_SOC_DAPM_PGA_E("Speaker PGA", WM9081_POWER_MANAGEMENT, 2, 0, NULL, 0,
746 speaker_event,
747 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
749 SND_SOC_DAPM_OUTPUT("LINEOUT"),
750 SND_SOC_DAPM_OUTPUT("SPKN"),
751 SND_SOC_DAPM_OUTPUT("SPKP"),
753 SND_SOC_DAPM_SUPPLY("CLK_SYS", WM9081_CLOCK_CONTROL_3, 0, 0, clk_sys_event,
754 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
755 SND_SOC_DAPM_SUPPLY("CLK_DSP", WM9081_CLOCK_CONTROL_3, 1, 0, NULL, 0),
756 SND_SOC_DAPM_SUPPLY("TOCLK", WM9081_CLOCK_CONTROL_3, 2, 0, NULL, 0),
760 static const struct snd_soc_dapm_route audio_paths[] = {
761 { "DAC", NULL, "CLK_SYS" },
762 { "DAC", NULL, "CLK_DSP" },
764 { "Mixer", "IN1 Switch", "IN1" },
765 { "Mixer", "IN2 Switch", "IN2" },
766 { "Mixer", "Playback Switch", "DAC" },
768 { "LINEOUT PGA", NULL, "Mixer" },
769 { "LINEOUT PGA", NULL, "TOCLK" },
770 { "LINEOUT PGA", NULL, "CLK_SYS" },
772 { "LINEOUT", NULL, "LINEOUT PGA" },
774 { "Speaker PGA", NULL, "Mixer" },
775 { "Speaker PGA", NULL, "TOCLK" },
776 { "Speaker PGA", NULL, "CLK_SYS" },
778 { "SPKN", NULL, "Speaker PGA" },
779 { "SPKP", NULL, "Speaker PGA" },
782 static int wm9081_set_bias_level(struct snd_soc_codec *codec,
783 enum snd_soc_bias_level level)
785 u16 reg;
787 switch (level) {
788 case SND_SOC_BIAS_ON:
789 break;
791 case SND_SOC_BIAS_PREPARE:
792 /* VMID=2*40k */
793 reg = snd_soc_read(codec, WM9081_VMID_CONTROL);
794 reg &= ~WM9081_VMID_SEL_MASK;
795 reg |= 0x2;
796 snd_soc_write(codec, WM9081_VMID_CONTROL, reg);
798 /* Normal bias current */
799 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
800 reg &= ~WM9081_STBY_BIAS_ENA;
801 snd_soc_write(codec, WM9081_BIAS_CONTROL_1, reg);
802 break;
804 case SND_SOC_BIAS_STANDBY:
805 /* Initial cold start */
806 if (codec->bias_level == SND_SOC_BIAS_OFF) {
807 /* Disable LINEOUT discharge */
808 reg = snd_soc_read(codec, WM9081_ANTI_POP_CONTROL);
809 reg &= ~WM9081_LINEOUT_DISCH;
810 snd_soc_write(codec, WM9081_ANTI_POP_CONTROL, reg);
812 /* Select startup bias source */
813 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
814 reg |= WM9081_BIAS_SRC | WM9081_BIAS_ENA;
815 snd_soc_write(codec, WM9081_BIAS_CONTROL_1, reg);
817 /* VMID 2*4k; Soft VMID ramp enable */
818 reg = snd_soc_read(codec, WM9081_VMID_CONTROL);
819 reg |= WM9081_VMID_RAMP | 0x6;
820 snd_soc_write(codec, WM9081_VMID_CONTROL, reg);
822 mdelay(100);
824 /* Normal bias enable & soft start off */
825 reg |= WM9081_BIAS_ENA;
826 reg &= ~WM9081_VMID_RAMP;
827 snd_soc_write(codec, WM9081_VMID_CONTROL, reg);
829 /* Standard bias source */
830 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
831 reg &= ~WM9081_BIAS_SRC;
832 snd_soc_write(codec, WM9081_BIAS_CONTROL_1, reg);
835 /* VMID 2*240k */
836 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
837 reg &= ~WM9081_VMID_SEL_MASK;
838 reg |= 0x40;
839 snd_soc_write(codec, WM9081_VMID_CONTROL, reg);
841 /* Standby bias current on */
842 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
843 reg |= WM9081_STBY_BIAS_ENA;
844 snd_soc_write(codec, WM9081_BIAS_CONTROL_1, reg);
845 break;
847 case SND_SOC_BIAS_OFF:
848 /* Startup bias source */
849 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
850 reg |= WM9081_BIAS_SRC;
851 snd_soc_write(codec, WM9081_BIAS_CONTROL_1, reg);
853 /* Disable VMID and biases with soft ramping */
854 reg = snd_soc_read(codec, WM9081_VMID_CONTROL);
855 reg &= ~(WM9081_VMID_SEL_MASK | WM9081_BIAS_ENA);
856 reg |= WM9081_VMID_RAMP;
857 snd_soc_write(codec, WM9081_VMID_CONTROL, reg);
859 /* Actively discharge LINEOUT */
860 reg = snd_soc_read(codec, WM9081_ANTI_POP_CONTROL);
861 reg |= WM9081_LINEOUT_DISCH;
862 snd_soc_write(codec, WM9081_ANTI_POP_CONTROL, reg);
863 break;
866 codec->bias_level = level;
868 return 0;
871 static int wm9081_set_dai_fmt(struct snd_soc_dai *dai,
872 unsigned int fmt)
874 struct snd_soc_codec *codec = dai->codec;
875 struct wm9081_priv *wm9081 = codec->private_data;
876 unsigned int aif2 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_2);
878 aif2 &= ~(WM9081_AIF_BCLK_INV | WM9081_AIF_LRCLK_INV |
879 WM9081_BCLK_DIR | WM9081_LRCLK_DIR | WM9081_AIF_FMT_MASK);
881 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
882 case SND_SOC_DAIFMT_CBS_CFS:
883 wm9081->master = 0;
884 break;
885 case SND_SOC_DAIFMT_CBS_CFM:
886 aif2 |= WM9081_LRCLK_DIR;
887 wm9081->master = 1;
888 break;
889 case SND_SOC_DAIFMT_CBM_CFS:
890 aif2 |= WM9081_BCLK_DIR;
891 wm9081->master = 1;
892 break;
893 case SND_SOC_DAIFMT_CBM_CFM:
894 aif2 |= WM9081_LRCLK_DIR | WM9081_BCLK_DIR;
895 wm9081->master = 1;
896 break;
897 default:
898 return -EINVAL;
901 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
902 case SND_SOC_DAIFMT_DSP_B:
903 aif2 |= WM9081_AIF_LRCLK_INV;
904 case SND_SOC_DAIFMT_DSP_A:
905 aif2 |= 0x3;
906 break;
907 case SND_SOC_DAIFMT_I2S:
908 aif2 |= 0x2;
909 break;
910 case SND_SOC_DAIFMT_RIGHT_J:
911 break;
912 case SND_SOC_DAIFMT_LEFT_J:
913 aif2 |= 0x1;
914 break;
915 default:
916 return -EINVAL;
919 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
920 case SND_SOC_DAIFMT_DSP_A:
921 case SND_SOC_DAIFMT_DSP_B:
922 /* frame inversion not valid for DSP modes */
923 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
924 case SND_SOC_DAIFMT_NB_NF:
925 break;
926 case SND_SOC_DAIFMT_IB_NF:
927 aif2 |= WM9081_AIF_BCLK_INV;
928 break;
929 default:
930 return -EINVAL;
932 break;
934 case SND_SOC_DAIFMT_I2S:
935 case SND_SOC_DAIFMT_RIGHT_J:
936 case SND_SOC_DAIFMT_LEFT_J:
937 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
938 case SND_SOC_DAIFMT_NB_NF:
939 break;
940 case SND_SOC_DAIFMT_IB_IF:
941 aif2 |= WM9081_AIF_BCLK_INV | WM9081_AIF_LRCLK_INV;
942 break;
943 case SND_SOC_DAIFMT_IB_NF:
944 aif2 |= WM9081_AIF_BCLK_INV;
945 break;
946 case SND_SOC_DAIFMT_NB_IF:
947 aif2 |= WM9081_AIF_LRCLK_INV;
948 break;
949 default:
950 return -EINVAL;
952 break;
953 default:
954 return -EINVAL;
957 snd_soc_write(codec, WM9081_AUDIO_INTERFACE_2, aif2);
959 return 0;
962 static int wm9081_hw_params(struct snd_pcm_substream *substream,
963 struct snd_pcm_hw_params *params,
964 struct snd_soc_dai *dai)
966 struct snd_soc_codec *codec = dai->codec;
967 struct wm9081_priv *wm9081 = codec->private_data;
968 int ret, i, best, best_val, cur_val;
969 unsigned int clk_ctrl2, aif1, aif2, aif3, aif4;
971 clk_ctrl2 = snd_soc_read(codec, WM9081_CLOCK_CONTROL_2);
972 clk_ctrl2 &= ~(WM9081_CLK_SYS_RATE_MASK | WM9081_SAMPLE_RATE_MASK);
974 aif1 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_1);
976 aif2 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_2);
977 aif2 &= ~WM9081_AIF_WL_MASK;
979 aif3 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_3);
980 aif3 &= ~WM9081_BCLK_DIV_MASK;
982 aif4 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_4);
983 aif4 &= ~WM9081_LRCLK_RATE_MASK;
985 wm9081->fs = params_rate(params);
987 if (wm9081->tdm_width) {
988 /* If TDM is set up then that fixes our BCLK. */
989 int slots = ((aif1 & WM9081_AIFDAC_TDM_MODE_MASK) >>
990 WM9081_AIFDAC_TDM_MODE_SHIFT) + 1;
992 wm9081->bclk = wm9081->fs * wm9081->tdm_width * slots;
993 } else {
994 /* Otherwise work out a BCLK from the sample size */
995 wm9081->bclk = 2 * wm9081->fs;
997 switch (params_format(params)) {
998 case SNDRV_PCM_FORMAT_S16_LE:
999 wm9081->bclk *= 16;
1000 break;
1001 case SNDRV_PCM_FORMAT_S20_3LE:
1002 wm9081->bclk *= 20;
1003 aif2 |= 0x4;
1004 break;
1005 case SNDRV_PCM_FORMAT_S24_LE:
1006 wm9081->bclk *= 24;
1007 aif2 |= 0x8;
1008 break;
1009 case SNDRV_PCM_FORMAT_S32_LE:
1010 wm9081->bclk *= 32;
1011 aif2 |= 0xc;
1012 break;
1013 default:
1014 return -EINVAL;
1018 dev_dbg(codec->dev, "Target BCLK is %dHz\n", wm9081->bclk);
1020 ret = configure_clock(codec);
1021 if (ret != 0)
1022 return ret;
1024 /* Select nearest CLK_SYS_RATE */
1025 best = 0;
1026 best_val = abs((wm9081->sysclk_rate / clk_sys_rates[0].ratio)
1027 - wm9081->fs);
1028 for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
1029 cur_val = abs((wm9081->sysclk_rate /
1030 clk_sys_rates[i].ratio) - wm9081->fs);
1031 if (cur_val < best_val) {
1032 best = i;
1033 best_val = cur_val;
1036 dev_dbg(codec->dev, "Selected CLK_SYS_RATIO of %d\n",
1037 clk_sys_rates[best].ratio);
1038 clk_ctrl2 |= (clk_sys_rates[best].clk_sys_rate
1039 << WM9081_CLK_SYS_RATE_SHIFT);
1041 /* SAMPLE_RATE */
1042 best = 0;
1043 best_val = abs(wm9081->fs - sample_rates[0].rate);
1044 for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
1045 /* Closest match */
1046 cur_val = abs(wm9081->fs - sample_rates[i].rate);
1047 if (cur_val < best_val) {
1048 best = i;
1049 best_val = cur_val;
1052 dev_dbg(codec->dev, "Selected SAMPLE_RATE of %dHz\n",
1053 sample_rates[best].rate);
1054 clk_ctrl2 |= (sample_rates[best].sample_rate
1055 << WM9081_SAMPLE_RATE_SHIFT);
1057 /* BCLK_DIV */
1058 best = 0;
1059 best_val = INT_MAX;
1060 for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
1061 cur_val = ((wm9081->sysclk_rate * 10) / bclk_divs[i].div)
1062 - wm9081->bclk;
1063 if (cur_val < 0) /* Table is sorted */
1064 break;
1065 if (cur_val < best_val) {
1066 best = i;
1067 best_val = cur_val;
1070 wm9081->bclk = (wm9081->sysclk_rate * 10) / bclk_divs[best].div;
1071 dev_dbg(codec->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
1072 bclk_divs[best].div, wm9081->bclk);
1073 aif3 |= bclk_divs[best].bclk_div;
1075 /* LRCLK is a simple fraction of BCLK */
1076 dev_dbg(codec->dev, "LRCLK_RATE is %d\n", wm9081->bclk / wm9081->fs);
1077 aif4 |= wm9081->bclk / wm9081->fs;
1079 /* Apply a ReTune Mobile configuration if it's in use */
1080 if (wm9081->retune) {
1081 struct wm9081_retune_mobile_config *retune = wm9081->retune;
1082 struct wm9081_retune_mobile_setting *s;
1083 int eq1;
1085 best = 0;
1086 best_val = abs(retune->configs[0].rate - wm9081->fs);
1087 for (i = 0; i < retune->num_configs; i++) {
1088 cur_val = abs(retune->configs[i].rate - wm9081->fs);
1089 if (cur_val < best_val) {
1090 best_val = cur_val;
1091 best = i;
1094 s = &retune->configs[best];
1096 dev_dbg(codec->dev, "ReTune Mobile %s tuned for %dHz\n",
1097 s->name, s->rate);
1099 /* If the EQ is enabled then disable it while we write out */
1100 eq1 = snd_soc_read(codec, WM9081_EQ_1) & WM9081_EQ_ENA;
1101 if (eq1 & WM9081_EQ_ENA)
1102 snd_soc_write(codec, WM9081_EQ_1, 0);
1104 /* Write out the other values */
1105 for (i = 1; i < ARRAY_SIZE(s->config); i++)
1106 snd_soc_write(codec, WM9081_EQ_1 + i, s->config[i]);
1108 eq1 |= (s->config[0] & ~WM9081_EQ_ENA);
1109 snd_soc_write(codec, WM9081_EQ_1, eq1);
1112 snd_soc_write(codec, WM9081_CLOCK_CONTROL_2, clk_ctrl2);
1113 snd_soc_write(codec, WM9081_AUDIO_INTERFACE_2, aif2);
1114 snd_soc_write(codec, WM9081_AUDIO_INTERFACE_3, aif3);
1115 snd_soc_write(codec, WM9081_AUDIO_INTERFACE_4, aif4);
1117 return 0;
1120 static int wm9081_digital_mute(struct snd_soc_dai *codec_dai, int mute)
1122 struct snd_soc_codec *codec = codec_dai->codec;
1123 unsigned int reg;
1125 reg = snd_soc_read(codec, WM9081_DAC_DIGITAL_2);
1127 if (mute)
1128 reg |= WM9081_DAC_MUTE;
1129 else
1130 reg &= ~WM9081_DAC_MUTE;
1132 snd_soc_write(codec, WM9081_DAC_DIGITAL_2, reg);
1134 return 0;
1137 static int wm9081_set_sysclk(struct snd_soc_dai *codec_dai,
1138 int clk_id, unsigned int freq, int dir)
1140 struct snd_soc_codec *codec = codec_dai->codec;
1141 struct wm9081_priv *wm9081 = codec->private_data;
1143 switch (clk_id) {
1144 case WM9081_SYSCLK_MCLK:
1145 case WM9081_SYSCLK_FLL_MCLK:
1146 wm9081->sysclk_source = clk_id;
1147 wm9081->mclk_rate = freq;
1148 break;
1150 default:
1151 return -EINVAL;
1154 return 0;
1157 static int wm9081_set_tdm_slot(struct snd_soc_dai *dai,
1158 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
1160 struct snd_soc_codec *codec = dai->codec;
1161 struct wm9081_priv *wm9081 = codec->private_data;
1162 unsigned int aif1 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_1);
1164 aif1 &= ~(WM9081_AIFDAC_TDM_SLOT_MASK | WM9081_AIFDAC_TDM_MODE_MASK);
1166 if (slots < 0 || slots > 4)
1167 return -EINVAL;
1169 wm9081->tdm_width = slot_width;
1171 if (slots == 0)
1172 slots = 1;
1174 aif1 |= (slots - 1) << WM9081_AIFDAC_TDM_MODE_SHIFT;
1176 switch (rx_mask) {
1177 case 1:
1178 break;
1179 case 2:
1180 aif1 |= 0x10;
1181 break;
1182 case 4:
1183 aif1 |= 0x20;
1184 break;
1185 case 8:
1186 aif1 |= 0x30;
1187 break;
1188 default:
1189 return -EINVAL;
1192 snd_soc_write(codec, WM9081_AUDIO_INTERFACE_1, aif1);
1194 return 0;
1197 #define WM9081_RATES SNDRV_PCM_RATE_8000_96000
1199 #define WM9081_FORMATS \
1200 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1201 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
1203 static struct snd_soc_dai_ops wm9081_dai_ops = {
1204 .hw_params = wm9081_hw_params,
1205 .set_sysclk = wm9081_set_sysclk,
1206 .set_fmt = wm9081_set_dai_fmt,
1207 .digital_mute = wm9081_digital_mute,
1208 .set_tdm_slot = wm9081_set_tdm_slot,
1211 /* We report two channels because the CODEC processes a stereo signal, even
1212 * though it is only capable of handling a mono output.
1214 struct snd_soc_dai wm9081_dai = {
1215 .name = "WM9081",
1216 .playback = {
1217 .stream_name = "HiFi Playback",
1218 .channels_min = 1,
1219 .channels_max = 2,
1220 .rates = WM9081_RATES,
1221 .formats = WM9081_FORMATS,
1223 .ops = &wm9081_dai_ops,
1225 EXPORT_SYMBOL_GPL(wm9081_dai);
1228 static struct snd_soc_codec *wm9081_codec;
1230 static int wm9081_probe(struct platform_device *pdev)
1232 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1233 struct snd_soc_codec *codec;
1234 struct wm9081_priv *wm9081;
1235 int ret = 0;
1237 if (wm9081_codec == NULL) {
1238 dev_err(&pdev->dev, "Codec device not registered\n");
1239 return -ENODEV;
1242 socdev->card->codec = wm9081_codec;
1243 codec = wm9081_codec;
1244 wm9081 = codec->private_data;
1246 /* register pcms */
1247 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
1248 if (ret < 0) {
1249 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
1250 goto pcm_err;
1253 snd_soc_add_controls(codec, wm9081_snd_controls,
1254 ARRAY_SIZE(wm9081_snd_controls));
1255 if (!wm9081->retune) {
1256 dev_dbg(codec->dev,
1257 "No ReTune Mobile data, using normal EQ\n");
1258 snd_soc_add_controls(codec, wm9081_eq_controls,
1259 ARRAY_SIZE(wm9081_eq_controls));
1262 snd_soc_dapm_new_controls(codec, wm9081_dapm_widgets,
1263 ARRAY_SIZE(wm9081_dapm_widgets));
1264 snd_soc_dapm_add_routes(codec, audio_paths, ARRAY_SIZE(audio_paths));
1265 snd_soc_dapm_new_widgets(codec);
1267 ret = snd_soc_init_card(socdev);
1268 if (ret < 0) {
1269 dev_err(codec->dev, "failed to register card: %d\n", ret);
1270 goto card_err;
1273 return ret;
1275 card_err:
1276 snd_soc_free_pcms(socdev);
1277 snd_soc_dapm_free(socdev);
1278 pcm_err:
1279 return ret;
1282 static int wm9081_remove(struct platform_device *pdev)
1284 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1286 snd_soc_free_pcms(socdev);
1287 snd_soc_dapm_free(socdev);
1289 return 0;
1292 #ifdef CONFIG_PM
1293 static int wm9081_suspend(struct platform_device *pdev, pm_message_t state)
1295 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1296 struct snd_soc_codec *codec = socdev->card->codec;
1298 wm9081_set_bias_level(codec, SND_SOC_BIAS_OFF);
1300 return 0;
1303 static int wm9081_resume(struct platform_device *pdev)
1305 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1306 struct snd_soc_codec *codec = socdev->card->codec;
1307 u16 *reg_cache = codec->reg_cache;
1308 int i;
1310 for (i = 0; i < codec->reg_cache_size; i++) {
1311 if (i == WM9081_SOFTWARE_RESET)
1312 continue;
1314 snd_soc_write(codec, i, reg_cache[i]);
1317 wm9081_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1319 return 0;
1321 #else
1322 #define wm9081_suspend NULL
1323 #define wm9081_resume NULL
1324 #endif
1326 struct snd_soc_codec_device soc_codec_dev_wm9081 = {
1327 .probe = wm9081_probe,
1328 .remove = wm9081_remove,
1329 .suspend = wm9081_suspend,
1330 .resume = wm9081_resume,
1332 EXPORT_SYMBOL_GPL(soc_codec_dev_wm9081);
1334 static int wm9081_register(struct wm9081_priv *wm9081,
1335 enum snd_soc_control_type control)
1337 struct snd_soc_codec *codec = &wm9081->codec;
1338 int ret;
1339 u16 reg;
1341 if (wm9081_codec) {
1342 dev_err(codec->dev, "Another WM9081 is registered\n");
1343 ret = -EINVAL;
1344 goto err;
1347 mutex_init(&codec->mutex);
1348 INIT_LIST_HEAD(&codec->dapm_widgets);
1349 INIT_LIST_HEAD(&codec->dapm_paths);
1351 codec->private_data = wm9081;
1352 codec->name = "WM9081";
1353 codec->owner = THIS_MODULE;
1354 codec->dai = &wm9081_dai;
1355 codec->num_dai = 1;
1356 codec->reg_cache_size = ARRAY_SIZE(wm9081->reg_cache);
1357 codec->reg_cache = &wm9081->reg_cache;
1358 codec->bias_level = SND_SOC_BIAS_OFF;
1359 codec->set_bias_level = wm9081_set_bias_level;
1360 codec->volatile_register = wm9081_volatile_register;
1362 memcpy(codec->reg_cache, wm9081_reg_defaults,
1363 sizeof(wm9081_reg_defaults));
1365 ret = snd_soc_codec_set_cache_io(codec, 8, 16, control);
1366 if (ret != 0) {
1367 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
1368 return ret;
1371 reg = snd_soc_read(codec, WM9081_SOFTWARE_RESET);
1372 if (reg != 0x9081) {
1373 dev_err(codec->dev, "Device is not a WM9081: ID=0x%x\n", reg);
1374 ret = -EINVAL;
1375 goto err;
1378 ret = wm9081_reset(codec);
1379 if (ret < 0) {
1380 dev_err(codec->dev, "Failed to issue reset\n");
1381 return ret;
1384 wm9081_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1386 /* Enable zero cross by default */
1387 reg = snd_soc_read(codec, WM9081_ANALOGUE_LINEOUT);
1388 snd_soc_write(codec, WM9081_ANALOGUE_LINEOUT, reg | WM9081_LINEOUTZC);
1389 reg = snd_soc_read(codec, WM9081_ANALOGUE_SPEAKER_PGA);
1390 snd_soc_write(codec, WM9081_ANALOGUE_SPEAKER_PGA,
1391 reg | WM9081_SPKPGAZC);
1393 wm9081_dai.dev = codec->dev;
1395 wm9081_codec = codec;
1397 ret = snd_soc_register_codec(codec);
1398 if (ret != 0) {
1399 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
1400 return ret;
1403 ret = snd_soc_register_dai(&wm9081_dai);
1404 if (ret != 0) {
1405 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
1406 snd_soc_unregister_codec(codec);
1407 return ret;
1410 return 0;
1412 err:
1413 kfree(wm9081);
1414 return ret;
1417 static void wm9081_unregister(struct wm9081_priv *wm9081)
1419 wm9081_set_bias_level(&wm9081->codec, SND_SOC_BIAS_OFF);
1420 snd_soc_unregister_dai(&wm9081_dai);
1421 snd_soc_unregister_codec(&wm9081->codec);
1422 kfree(wm9081);
1423 wm9081_codec = NULL;
1426 static __devinit int wm9081_i2c_probe(struct i2c_client *i2c,
1427 const struct i2c_device_id *id)
1429 struct wm9081_priv *wm9081;
1430 struct snd_soc_codec *codec;
1432 wm9081 = kzalloc(sizeof(struct wm9081_priv), GFP_KERNEL);
1433 if (wm9081 == NULL)
1434 return -ENOMEM;
1436 codec = &wm9081->codec;
1437 codec->hw_write = (hw_write_t)i2c_master_send;
1438 wm9081->retune = i2c->dev.platform_data;
1440 i2c_set_clientdata(i2c, wm9081);
1441 codec->control_data = i2c;
1443 codec->dev = &i2c->dev;
1445 return wm9081_register(wm9081, SND_SOC_I2C);
1448 static __devexit int wm9081_i2c_remove(struct i2c_client *client)
1450 struct wm9081_priv *wm9081 = i2c_get_clientdata(client);
1451 wm9081_unregister(wm9081);
1452 return 0;
1455 #ifdef CONFIG_PM
1456 static int wm9081_i2c_suspend(struct i2c_client *client, pm_message_t msg)
1458 return snd_soc_suspend_device(&client->dev);
1461 static int wm9081_i2c_resume(struct i2c_client *client)
1463 return snd_soc_resume_device(&client->dev);
1465 #else
1466 #define wm9081_i2c_suspend NULL
1467 #define wm9081_i2c_resume NULL
1468 #endif
1470 static const struct i2c_device_id wm9081_i2c_id[] = {
1471 { "wm9081", 0 },
1474 MODULE_DEVICE_TABLE(i2c, wm9081_i2c_id);
1476 static struct i2c_driver wm9081_i2c_driver = {
1477 .driver = {
1478 .name = "wm9081",
1479 .owner = THIS_MODULE,
1481 .probe = wm9081_i2c_probe,
1482 .remove = __devexit_p(wm9081_i2c_remove),
1483 .suspend = wm9081_i2c_suspend,
1484 .resume = wm9081_i2c_resume,
1485 .id_table = wm9081_i2c_id,
1488 static int __init wm9081_modinit(void)
1490 int ret;
1492 ret = i2c_add_driver(&wm9081_i2c_driver);
1493 if (ret != 0) {
1494 printk(KERN_ERR "Failed to register WM9081 I2C driver: %d\n",
1495 ret);
1498 return ret;
1500 module_init(wm9081_modinit);
1502 static void __exit wm9081_exit(void)
1504 i2c_del_driver(&wm9081_i2c_driver);
1506 module_exit(wm9081_exit);
1509 MODULE_DESCRIPTION("ASoC WM9081 driver");
1510 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1511 MODULE_LICENSE("GPL");