blob: c468497314ba4291314e856e26d3bb50c9c9b5b7 [file] [log] [blame]
Mark Brown86ed3662009-05-22 15:01:19 +01001/*
2 * wm9081.c -- WM9081 ALSA SoC Audio driver
3 *
4 * Author: Mark Brown
5 *
6 * Copyright 2009 Wolfson Microelectronics plc
7 *
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.
11 *
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>
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>
28
29#include <sound/wm9081.h>
30#include "wm9081.h"
31
32static 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 */
95};
96
97static 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 },
111};
112
113static 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 },
128};
129
130static 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 },
155};
156
157struct 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;
Mark Browne0026be2009-08-11 16:28:39 +0100168 int tdm_width;
Mark Brown86ed3662009-05-22 15:01:19 +0100169 struct wm9081_retune_mobile_config *retune;
170};
171
Mark Brown8d50e442009-07-10 23:12:01 +0100172static int wm9081_volatile_register(unsigned int reg)
Mark Brown86ed3662009-05-22 15:01:19 +0100173{
174 switch (reg) {
Mark Brown8d50e442009-07-10 23:12:01 +0100175 case WM9081_SOFTWARE_RESET:
176 return 1;
Mark Brown86ed3662009-05-22 15:01:19 +0100177 default:
178 return 0;
179 }
180}
181
Mark Brown86ed3662009-05-22 15:01:19 +0100182static int wm9081_reset(struct snd_soc_codec *codec)
183{
Mark Brown8d50e442009-07-10 23:12:01 +0100184 return snd_soc_write(codec, WM9081_SOFTWARE_RESET, 0);
Mark Brown86ed3662009-05-22 15:01:19 +0100185}
186
187static const DECLARE_TLV_DB_SCALE(drc_in_tlv, -4500, 75, 0);
188static const DECLARE_TLV_DB_SCALE(drc_out_tlv, -2250, 75, 0);
189static const DECLARE_TLV_DB_SCALE(drc_min_tlv, -1800, 600, 0);
190static 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),
196};
197static const DECLARE_TLV_DB_SCALE(drc_qr_tlv, 1200, 600, 0);
198static const DECLARE_TLV_DB_SCALE(drc_startup_tlv, -300, 50, 0);
199
200static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
201
202static const DECLARE_TLV_DB_SCALE(in_tlv, -600, 600, 0);
203static const DECLARE_TLV_DB_SCALE(dac_tlv, -7200, 75, 1);
204static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
205
206static const char *drc_high_text[] = {
207 "1",
208 "1/2",
209 "1/4",
210 "1/8",
211 "1/16",
212 "0",
213};
214
215static const struct soc_enum drc_high =
216 SOC_ENUM_SINGLE(WM9081_DRC_3, 3, 6, drc_high_text);
217
218static const char *drc_low_text[] = {
219 "1",
220 "1/2",
221 "1/4",
222 "1/8",
223 "0",
224};
225
226static const struct soc_enum drc_low =
227 SOC_ENUM_SINGLE(WM9081_DRC_3, 0, 5, drc_low_text);
228
229static 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",
242};
243
244static const struct soc_enum drc_atk =
245 SOC_ENUM_SINGLE(WM9081_DRC_2, 12, 12, drc_atk_text);
246
247static 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",
257};
258
259static const struct soc_enum drc_dcy =
260 SOC_ENUM_SINGLE(WM9081_DRC_2, 8, 9, drc_dcy_text);
261
262static const char *drc_qr_dcy_text[] = {
263 "0.725ms",
264 "1.45ms",
265 "5.8ms",
266};
267
268static const struct soc_enum drc_qr_dcy =
269 SOC_ENUM_SINGLE(WM9081_DRC_2, 4, 3, drc_qr_dcy_text);
270
271static const char *dac_deemph_text[] = {
272 "None",
273 "32kHz",
274 "44.1kHz",
275 "48kHz",
276};
277
278static const struct soc_enum dac_deemph =
279 SOC_ENUM_SINGLE(WM9081_DAC_DIGITAL_2, 1, 4, dac_deemph_text);
280
281static const char *speaker_mode_text[] = {
282 "Class D",
283 "Class AB",
284};
285
286static const struct soc_enum speaker_mode =
287 SOC_ENUM_SINGLE(WM9081_ANALOGUE_SPEAKER_2, 6, 2, speaker_mode_text);
288
289static int speaker_mode_get(struct snd_kcontrol *kcontrol,
290 struct snd_ctl_elem_value *ucontrol)
291{
292 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
293 unsigned int reg;
294
Mark Brown8d50e442009-07-10 23:12:01 +0100295 reg = snd_soc_read(codec, WM9081_ANALOGUE_SPEAKER_2);
Mark Brown86ed3662009-05-22 15:01:19 +0100296 if (reg & WM9081_SPK_MODE)
297 ucontrol->value.integer.value[0] = 1;
298 else
299 ucontrol->value.integer.value[0] = 0;
300
301 return 0;
302}
303
304/*
305 * Stop any attempts to change speaker mode while the speaker is enabled.
306 *
307 * We also have some special anti-pop controls dependant on speaker
308 * mode which must be changed along with the mode.
309 */
310static int speaker_mode_put(struct snd_kcontrol *kcontrol,
311 struct snd_ctl_elem_value *ucontrol)
312{
313 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brown8d50e442009-07-10 23:12:01 +0100314 unsigned int reg_pwr = snd_soc_read(codec, WM9081_POWER_MANAGEMENT);
315 unsigned int reg2 = snd_soc_read(codec, WM9081_ANALOGUE_SPEAKER_2);
Mark Brown86ed3662009-05-22 15:01:19 +0100316
317 /* Are we changing anything? */
318 if (ucontrol->value.integer.value[0] ==
319 ((reg2 & WM9081_SPK_MODE) != 0))
320 return 0;
321
322 /* Don't try to change modes while enabled */
323 if (reg_pwr & WM9081_SPK_ENA)
324 return -EINVAL;
325
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;
334 }
335
Mark Brown8d50e442009-07-10 23:12:01 +0100336 snd_soc_write(codec, WM9081_ANALOGUE_SPEAKER_2, reg2);
Mark Brown86ed3662009-05-22 15:01:19 +0100337
338 return 0;
339}
340
341static const struct snd_kcontrol_new wm9081_snd_controls[] = {
342SOC_SINGLE_TLV("IN1 Volume", WM9081_ANALOGUE_MIXER, 1, 1, 1, in_tlv),
343SOC_SINGLE_TLV("IN2 Volume", WM9081_ANALOGUE_MIXER, 3, 1, 1, in_tlv),
344
345SOC_SINGLE_TLV("Playback Volume", WM9081_DAC_DIGITAL_1, 1, 96, 0, dac_tlv),
346
347SOC_SINGLE("LINEOUT Switch", WM9081_ANALOGUE_LINEOUT, 7, 1, 1),
348SOC_SINGLE("LINEOUT ZC Switch", WM9081_ANALOGUE_LINEOUT, 6, 1, 0),
349SOC_SINGLE_TLV("LINEOUT Volume", WM9081_ANALOGUE_LINEOUT, 0, 63, 0, out_tlv),
350
351SOC_SINGLE("DRC Switch", WM9081_DRC_1, 15, 1, 0),
352SOC_ENUM("DRC High Slope", drc_high),
353SOC_ENUM("DRC Low Slope", drc_low),
354SOC_SINGLE_TLV("DRC Input Volume", WM9081_DRC_4, 5, 60, 1, drc_in_tlv),
355SOC_SINGLE_TLV("DRC Output Volume", WM9081_DRC_4, 0, 30, 1, drc_out_tlv),
356SOC_SINGLE_TLV("DRC Minimum Volume", WM9081_DRC_2, 2, 3, 1, drc_min_tlv),
357SOC_SINGLE_TLV("DRC Maximum Volume", WM9081_DRC_2, 0, 3, 0, drc_max_tlv),
358SOC_ENUM("DRC Attack", drc_atk),
359SOC_ENUM("DRC Decay", drc_dcy),
360SOC_SINGLE("DRC Quick Release Switch", WM9081_DRC_1, 2, 1, 0),
361SOC_SINGLE_TLV("DRC Quick Release Volume", WM9081_DRC_2, 6, 3, 0, drc_qr_tlv),
362SOC_ENUM("DRC Quick Release Decay", drc_qr_dcy),
363SOC_SINGLE_TLV("DRC Startup Volume", WM9081_DRC_1, 6, 18, 0, drc_startup_tlv),
364
365SOC_SINGLE("EQ Switch", WM9081_EQ_1, 0, 1, 0),
366
367SOC_SINGLE("Speaker DC Volume", WM9081_ANALOGUE_SPEAKER_1, 3, 5, 0),
368SOC_SINGLE("Speaker AC Volume", WM9081_ANALOGUE_SPEAKER_1, 0, 5, 0),
369SOC_SINGLE("Speaker Switch", WM9081_ANALOGUE_SPEAKER_PGA, 7, 1, 1),
370SOC_SINGLE("Speaker ZC Switch", WM9081_ANALOGUE_SPEAKER_PGA, 6, 1, 0),
371SOC_SINGLE_TLV("Speaker Volume", WM9081_ANALOGUE_SPEAKER_PGA, 0, 63, 0,
372 out_tlv),
373SOC_ENUM("DAC Deemphasis", dac_deemph),
374SOC_ENUM_EXT("Speaker Mode", speaker_mode, speaker_mode_get, speaker_mode_put),
375};
376
377static const struct snd_kcontrol_new wm9081_eq_controls[] = {
378SOC_SINGLE_TLV("EQ1 Volume", WM9081_EQ_1, 11, 24, 0, eq_tlv),
379SOC_SINGLE_TLV("EQ2 Volume", WM9081_EQ_1, 6, 24, 0, eq_tlv),
380SOC_SINGLE_TLV("EQ3 Volume", WM9081_EQ_1, 1, 24, 0, eq_tlv),
381SOC_SINGLE_TLV("EQ4 Volume", WM9081_EQ_2, 11, 24, 0, eq_tlv),
382SOC_SINGLE_TLV("EQ5 Volume", WM9081_EQ_2, 6, 24, 0, eq_tlv),
383};
384
385static const struct snd_kcontrol_new mixer[] = {
386SOC_DAPM_SINGLE("IN1 Switch", WM9081_ANALOGUE_MIXER, 0, 1, 0),
387SOC_DAPM_SINGLE("IN2 Switch", WM9081_ANALOGUE_MIXER, 2, 1, 0),
388SOC_DAPM_SINGLE("Playback Switch", WM9081_ANALOGUE_MIXER, 4, 1, 0),
389};
390
391static int speaker_event(struct snd_soc_dapm_widget *w,
392 struct snd_kcontrol *kcontrol, int event)
393{
394 struct snd_soc_codec *codec = w->codec;
Mark Brown8d50e442009-07-10 23:12:01 +0100395 unsigned int reg = snd_soc_read(codec, WM9081_POWER_MANAGEMENT);
Mark Brown86ed3662009-05-22 15:01:19 +0100396
397 switch (event) {
398 case SND_SOC_DAPM_POST_PMU:
399 reg |= WM9081_SPK_ENA;
400 break;
401
402 case SND_SOC_DAPM_PRE_PMD:
403 reg &= ~WM9081_SPK_ENA;
404 break;
405 }
406
Mark Brown8d50e442009-07-10 23:12:01 +0100407 snd_soc_write(codec, WM9081_POWER_MANAGEMENT, reg);
Mark Brown86ed3662009-05-22 15:01:19 +0100408
409 return 0;
410}
411
412struct _fll_div {
413 u16 fll_fratio;
414 u16 fll_outdiv;
415 u16 fll_clk_ref_div;
416 u16 n;
417 u16 k;
418};
419
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)
423
424static 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 },
435};
436
437static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
438 unsigned int Fout)
439{
440 u64 Kpart;
441 unsigned int K, Ndiv, Nmod, target;
442 unsigned int div;
443 int i;
444
445 /* Fref must be <=13.5MHz */
446 div = 1;
447 while ((Fref / div) > 13500000) {
448 div *= 2;
449
450 if (div > 8) {
451 pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
452 Fref);
453 return -EINVAL;
454 }
455 }
456 fll_div->fll_clk_ref_div = div / 2;
457
458 pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
459
460 /* Apply the division for our remaining calculations */
461 Fref /= div;
462
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;
473 }
474 }
475 fll_div->fll_outdiv = div;
476
477 pr_debug("Fvco=%dHz\n", target);
478
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;
485 }
486 }
487 if (i == ARRAY_SIZE(fll_fratios)) {
488 pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
489 return -EINVAL;
490 }
491
492 /* Now, calculate N.K */
493 Ndiv = target / Fref;
494
495 fll_div->n = Ndiv;
496 Nmod = target % Fref;
497 pr_debug("Nmod=%d\n", Nmod);
498
499 /* Calculate fractional part - scale up so we can round. */
500 Kpart = FIXED_FLL_SIZE * (long long)Nmod;
501
502 do_div(Kpart, Fref);
503
504 K = Kpart & 0xFFFFFFFF;
505
506 if ((K % 10) >= 5)
507 K += 5;
508
509 /* Move down to proper range now rounding is done */
510 fll_div->k = K / 10;
511
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);
516
517 return 0;
518}
519
520static int wm9081_set_fll(struct snd_soc_codec *codec, int fll_id,
521 unsigned int Fref, unsigned int Fout)
522{
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;
528
529 /* Any change? */
530 if (Fref == wm9081->fll_fref && Fout == wm9081->fll_fout)
531 return 0;
532
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;
538
539 return 0;
540 }
541
542 ret = fll_factors(&fll_div, Fref, Fout);
543 if (ret != 0)
544 return ret;
545
Mark Brown8d50e442009-07-10 23:12:01 +0100546 reg5 = snd_soc_read(codec, WM9081_FLL_CONTROL_5);
Mark Brown86ed3662009-05-22 15:01:19 +0100547 reg5 &= ~WM9081_FLL_CLK_SRC_MASK;
548
549 switch (fll_id) {
550 case WM9081_SYSCLK_FLL_MCLK:
551 reg5 |= 0x1;
552 break;
553
554 default:
555 dev_err(codec->dev, "Unknown FLL ID %d\n", fll_id);
556 return -EINVAL;
557 }
558
559 /* Disable CLK_SYS while we reconfigure */
Mark Brown8d50e442009-07-10 23:12:01 +0100560 clk_sys_reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_3);
Mark Brown86ed3662009-05-22 15:01:19 +0100561 if (clk_sys_reg & WM9081_CLK_SYS_ENA)
Mark Brown8d50e442009-07-10 23:12:01 +0100562 snd_soc_write(codec, WM9081_CLOCK_CONTROL_3,
Mark Brown86ed3662009-05-22 15:01:19 +0100563 clk_sys_reg & ~WM9081_CLK_SYS_ENA);
564
565 /* Any FLL configuration change requires that the FLL be
566 * disabled first. */
Mark Brown8d50e442009-07-10 23:12:01 +0100567 reg1 = snd_soc_read(codec, WM9081_FLL_CONTROL_1);
Mark Brown86ed3662009-05-22 15:01:19 +0100568 reg1 &= ~WM9081_FLL_ENA;
Mark Brown8d50e442009-07-10 23:12:01 +0100569 snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1);
Mark Brown86ed3662009-05-22 15:01:19 +0100570
571 /* Apply the configuration */
572 if (fll_div.k)
573 reg1 |= WM9081_FLL_FRAC_MASK;
574 else
575 reg1 &= ~WM9081_FLL_FRAC_MASK;
Mark Brown8d50e442009-07-10 23:12:01 +0100576 snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1);
Mark Brown86ed3662009-05-22 15:01:19 +0100577
Mark Brown8d50e442009-07-10 23:12:01 +0100578 snd_soc_write(codec, WM9081_FLL_CONTROL_2,
Mark Brown86ed3662009-05-22 15:01:19 +0100579 (fll_div.fll_outdiv << WM9081_FLL_OUTDIV_SHIFT) |
580 (fll_div.fll_fratio << WM9081_FLL_FRATIO_SHIFT));
Mark Brown8d50e442009-07-10 23:12:01 +0100581 snd_soc_write(codec, WM9081_FLL_CONTROL_3, fll_div.k);
Mark Brown86ed3662009-05-22 15:01:19 +0100582
Mark Brown8d50e442009-07-10 23:12:01 +0100583 reg4 = snd_soc_read(codec, WM9081_FLL_CONTROL_4);
Mark Brown86ed3662009-05-22 15:01:19 +0100584 reg4 &= ~WM9081_FLL_N_MASK;
585 reg4 |= fll_div.n << WM9081_FLL_N_SHIFT;
Mark Brown8d50e442009-07-10 23:12:01 +0100586 snd_soc_write(codec, WM9081_FLL_CONTROL_4, reg4);
Mark Brown86ed3662009-05-22 15:01:19 +0100587
588 reg5 &= ~WM9081_FLL_CLK_REF_DIV_MASK;
589 reg5 |= fll_div.fll_clk_ref_div << WM9081_FLL_CLK_REF_DIV_SHIFT;
Mark Brown8d50e442009-07-10 23:12:01 +0100590 snd_soc_write(codec, WM9081_FLL_CONTROL_5, reg5);
Mark Brown86ed3662009-05-22 15:01:19 +0100591
592 /* Enable the FLL */
Mark Brown8d50e442009-07-10 23:12:01 +0100593 snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1 | WM9081_FLL_ENA);
Mark Brown86ed3662009-05-22 15:01:19 +0100594
595 /* Then bring CLK_SYS up again if it was disabled */
596 if (clk_sys_reg & WM9081_CLK_SYS_ENA)
Mark Brown8d50e442009-07-10 23:12:01 +0100597 snd_soc_write(codec, WM9081_CLOCK_CONTROL_3, clk_sys_reg);
Mark Brown86ed3662009-05-22 15:01:19 +0100598
599 dev_dbg(codec->dev, "FLL enabled at %dHz->%dHz\n", Fref, Fout);
600
601 wm9081->fll_fref = Fref;
602 wm9081->fll_fout = Fout;
603
604 return 0;
605}
606
607static int configure_clock(struct snd_soc_codec *codec)
608{
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;
615
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;
623 }
624 wm9081_set_fll(codec, WM9081_SYSCLK_FLL_MCLK, 0, 0);
625 break;
626
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.
634 */
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;
Mark Brown01547242009-05-23 00:01:05 +0100641 new_sysclk = target;
Mark Brown86ed3662009-05-22 15:01:19 +0100642 if (target >= wm9081->bclk &&
643 target > 3000000)
Mark Brown01547242009-05-23 00:01:05 +0100644 break;
Mark Brown86ed3662009-05-22 15:01:19 +0100645 }
Mark Brown4b75e942009-07-15 10:36:51 +0100646
647 if (i == ARRAY_SIZE(clk_sys_rates))
648 return -EINVAL;
649
Mark Brown86ed3662009-05-22 15:01:19 +0100650 } 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;
656 }
Mark Brown4b75e942009-07-15 10:36:51 +0100657
658 if (i == ARRAY_SIZE(clk_sys_rates))
659 return -EINVAL;
660
Mark Brown86ed3662009-05-22 15:01:19 +0100661 } else {
662 new_sysclk = 12288000;
663 }
664
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;
669
670 /* Switch SYSCLK over to FLL */
671 fll = 1;
672 } else {
673 wm9081->sysclk_rate = wm9081->mclk_rate;
674 }
675 break;
676
677 default:
678 return -EINVAL;
679 }
680
Mark Brown8d50e442009-07-10 23:12:01 +0100681 reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_1);
Mark Brown86ed3662009-05-22 15:01:19 +0100682 if (mclkdiv)
683 reg |= WM9081_MCLKDIV2;
684 else
685 reg &= ~WM9081_MCLKDIV2;
Mark Brown8d50e442009-07-10 23:12:01 +0100686 snd_soc_write(codec, WM9081_CLOCK_CONTROL_1, reg);
Mark Brown86ed3662009-05-22 15:01:19 +0100687
Mark Brown8d50e442009-07-10 23:12:01 +0100688 reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_3);
Mark Brown86ed3662009-05-22 15:01:19 +0100689 if (fll)
690 reg |= WM9081_CLK_SRC_SEL;
691 else
692 reg &= ~WM9081_CLK_SRC_SEL;
Mark Brown8d50e442009-07-10 23:12:01 +0100693 snd_soc_write(codec, WM9081_CLOCK_CONTROL_3, reg);
Mark Brown86ed3662009-05-22 15:01:19 +0100694
695 dev_dbg(codec->dev, "CLK_SYS is %dHz\n", wm9081->sysclk_rate);
696
697 return ret;
698}
699
700static int clk_sys_event(struct snd_soc_dapm_widget *w,
701 struct snd_kcontrol *kcontrol, int event)
702{
703 struct snd_soc_codec *codec = w->codec;
704 struct wm9081_priv *wm9081 = codec->private_data;
705
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;
718 }
719
720 switch (event) {
721 case SND_SOC_DAPM_PRE_PMU:
722 configure_clock(codec);
723 break;
724
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;
729 }
730
731 return 0;
732}
733
734static const struct snd_soc_dapm_widget wm9081_dapm_widgets[] = {
735SND_SOC_DAPM_INPUT("IN1"),
736SND_SOC_DAPM_INPUT("IN2"),
737
738SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM9081_POWER_MANAGEMENT, 0, 0),
739
740SND_SOC_DAPM_MIXER_NAMED_CTL("Mixer", SND_SOC_NOPM, 0, 0,
741 mixer, ARRAY_SIZE(mixer)),
742
743SND_SOC_DAPM_PGA("LINEOUT PGA", WM9081_POWER_MANAGEMENT, 4, 0, NULL, 0),
744
745SND_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),
748
749SND_SOC_DAPM_OUTPUT("LINEOUT"),
750SND_SOC_DAPM_OUTPUT("SPKN"),
751SND_SOC_DAPM_OUTPUT("SPKP"),
752
753SND_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),
755SND_SOC_DAPM_SUPPLY("CLK_DSP", WM9081_CLOCK_CONTROL_3, 1, 0, NULL, 0),
756SND_SOC_DAPM_SUPPLY("TOCLK", WM9081_CLOCK_CONTROL_3, 2, 0, NULL, 0),
757};
758
759
760static const struct snd_soc_dapm_route audio_paths[] = {
761 { "DAC", NULL, "CLK_SYS" },
762 { "DAC", NULL, "CLK_DSP" },
763
764 { "Mixer", "IN1 Switch", "IN1" },
765 { "Mixer", "IN2 Switch", "IN2" },
766 { "Mixer", "Playback Switch", "DAC" },
767
768 { "LINEOUT PGA", NULL, "Mixer" },
769 { "LINEOUT PGA", NULL, "TOCLK" },
770 { "LINEOUT PGA", NULL, "CLK_SYS" },
771
772 { "LINEOUT", NULL, "LINEOUT PGA" },
773
774 { "Speaker PGA", NULL, "Mixer" },
775 { "Speaker PGA", NULL, "TOCLK" },
776 { "Speaker PGA", NULL, "CLK_SYS" },
777
778 { "SPKN", NULL, "Speaker PGA" },
779 { "SPKP", NULL, "Speaker PGA" },
780};
781
782static int wm9081_set_bias_level(struct snd_soc_codec *codec,
783 enum snd_soc_bias_level level)
784{
785 u16 reg;
786
787 switch (level) {
788 case SND_SOC_BIAS_ON:
789 break;
790
791 case SND_SOC_BIAS_PREPARE:
792 /* VMID=2*40k */
Mark Brown8d50e442009-07-10 23:12:01 +0100793 reg = snd_soc_read(codec, WM9081_VMID_CONTROL);
Mark Brown86ed3662009-05-22 15:01:19 +0100794 reg &= ~WM9081_VMID_SEL_MASK;
795 reg |= 0x2;
Mark Brown8d50e442009-07-10 23:12:01 +0100796 snd_soc_write(codec, WM9081_VMID_CONTROL, reg);
Mark Brown86ed3662009-05-22 15:01:19 +0100797
798 /* Normal bias current */
Mark Brown8d50e442009-07-10 23:12:01 +0100799 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
Mark Brown86ed3662009-05-22 15:01:19 +0100800 reg &= ~WM9081_STBY_BIAS_ENA;
Mark Brown8d50e442009-07-10 23:12:01 +0100801 snd_soc_write(codec, WM9081_BIAS_CONTROL_1, reg);
Mark Brown86ed3662009-05-22 15:01:19 +0100802 break;
803
804 case SND_SOC_BIAS_STANDBY:
805 /* Initial cold start */
806 if (codec->bias_level == SND_SOC_BIAS_OFF) {
807 /* Disable LINEOUT discharge */
Mark Brown8d50e442009-07-10 23:12:01 +0100808 reg = snd_soc_read(codec, WM9081_ANTI_POP_CONTROL);
Mark Brown86ed3662009-05-22 15:01:19 +0100809 reg &= ~WM9081_LINEOUT_DISCH;
Mark Brown8d50e442009-07-10 23:12:01 +0100810 snd_soc_write(codec, WM9081_ANTI_POP_CONTROL, reg);
Mark Brown86ed3662009-05-22 15:01:19 +0100811
812 /* Select startup bias source */
Mark Brown8d50e442009-07-10 23:12:01 +0100813 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
Mark Brown86ed3662009-05-22 15:01:19 +0100814 reg |= WM9081_BIAS_SRC | WM9081_BIAS_ENA;
Mark Brown8d50e442009-07-10 23:12:01 +0100815 snd_soc_write(codec, WM9081_BIAS_CONTROL_1, reg);
Mark Brown86ed3662009-05-22 15:01:19 +0100816
817 /* VMID 2*4k; Soft VMID ramp enable */
Mark Brown8d50e442009-07-10 23:12:01 +0100818 reg = snd_soc_read(codec, WM9081_VMID_CONTROL);
Mark Brown86ed3662009-05-22 15:01:19 +0100819 reg |= WM9081_VMID_RAMP | 0x6;
Mark Brown8d50e442009-07-10 23:12:01 +0100820 snd_soc_write(codec, WM9081_VMID_CONTROL, reg);
Mark Brown86ed3662009-05-22 15:01:19 +0100821
822 mdelay(100);
823
824 /* Normal bias enable & soft start off */
825 reg |= WM9081_BIAS_ENA;
826 reg &= ~WM9081_VMID_RAMP;
Mark Brown8d50e442009-07-10 23:12:01 +0100827 snd_soc_write(codec, WM9081_VMID_CONTROL, reg);
Mark Brown86ed3662009-05-22 15:01:19 +0100828
829 /* Standard bias source */
Mark Brown8d50e442009-07-10 23:12:01 +0100830 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
Mark Brown86ed3662009-05-22 15:01:19 +0100831 reg &= ~WM9081_BIAS_SRC;
Mark Brown8d50e442009-07-10 23:12:01 +0100832 snd_soc_write(codec, WM9081_BIAS_CONTROL_1, reg);
Mark Brown86ed3662009-05-22 15:01:19 +0100833 }
834
835 /* VMID 2*240k */
Mark Brown8d50e442009-07-10 23:12:01 +0100836 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
Mark Brown86ed3662009-05-22 15:01:19 +0100837 reg &= ~WM9081_VMID_SEL_MASK;
838 reg |= 0x40;
Mark Brown8d50e442009-07-10 23:12:01 +0100839 snd_soc_write(codec, WM9081_VMID_CONTROL, reg);
Mark Brown86ed3662009-05-22 15:01:19 +0100840
841 /* Standby bias current on */
Mark Brown8d50e442009-07-10 23:12:01 +0100842 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
Mark Brown86ed3662009-05-22 15:01:19 +0100843 reg |= WM9081_STBY_BIAS_ENA;
Mark Brown8d50e442009-07-10 23:12:01 +0100844 snd_soc_write(codec, WM9081_BIAS_CONTROL_1, reg);
Mark Brown86ed3662009-05-22 15:01:19 +0100845 break;
846
847 case SND_SOC_BIAS_OFF:
848 /* Startup bias source */
Mark Brown8d50e442009-07-10 23:12:01 +0100849 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
Mark Brown86ed3662009-05-22 15:01:19 +0100850 reg |= WM9081_BIAS_SRC;
Mark Brown8d50e442009-07-10 23:12:01 +0100851 snd_soc_write(codec, WM9081_BIAS_CONTROL_1, reg);
Mark Brown86ed3662009-05-22 15:01:19 +0100852
853 /* Disable VMID and biases with soft ramping */
Mark Brown8d50e442009-07-10 23:12:01 +0100854 reg = snd_soc_read(codec, WM9081_VMID_CONTROL);
Mark Brown86ed3662009-05-22 15:01:19 +0100855 reg &= ~(WM9081_VMID_SEL_MASK | WM9081_BIAS_ENA);
856 reg |= WM9081_VMID_RAMP;
Mark Brown8d50e442009-07-10 23:12:01 +0100857 snd_soc_write(codec, WM9081_VMID_CONTROL, reg);
Mark Brown86ed3662009-05-22 15:01:19 +0100858
859 /* Actively discharge LINEOUT */
Mark Brown8d50e442009-07-10 23:12:01 +0100860 reg = snd_soc_read(codec, WM9081_ANTI_POP_CONTROL);
Mark Brown86ed3662009-05-22 15:01:19 +0100861 reg |= WM9081_LINEOUT_DISCH;
Mark Brown8d50e442009-07-10 23:12:01 +0100862 snd_soc_write(codec, WM9081_ANTI_POP_CONTROL, reg);
Mark Brown86ed3662009-05-22 15:01:19 +0100863 break;
864 }
865
866 codec->bias_level = level;
867
868 return 0;
869}
870
871static int wm9081_set_dai_fmt(struct snd_soc_dai *dai,
872 unsigned int fmt)
873{
874 struct snd_soc_codec *codec = dai->codec;
875 struct wm9081_priv *wm9081 = codec->private_data;
Mark Brown8d50e442009-07-10 23:12:01 +0100876 unsigned int aif2 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_2);
Mark Brown86ed3662009-05-22 15:01:19 +0100877
878 aif2 &= ~(WM9081_AIF_BCLK_INV | WM9081_AIF_LRCLK_INV |
879 WM9081_BCLK_DIR | WM9081_LRCLK_DIR | WM9081_AIF_FMT_MASK);
880
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;
899 }
900
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;
917 }
918
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;
931 }
932 break;
933
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;
951 }
952 break;
953 default:
954 return -EINVAL;
955 }
956
Mark Brown8d50e442009-07-10 23:12:01 +0100957 snd_soc_write(codec, WM9081_AUDIO_INTERFACE_2, aif2);
Mark Brown86ed3662009-05-22 15:01:19 +0100958
959 return 0;
960}
961
962static int wm9081_hw_params(struct snd_pcm_substream *substream,
963 struct snd_pcm_hw_params *params,
964 struct snd_soc_dai *dai)
965{
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;
970
Mark Brown8d50e442009-07-10 23:12:01 +0100971 clk_ctrl2 = snd_soc_read(codec, WM9081_CLOCK_CONTROL_2);
Mark Brown86ed3662009-05-22 15:01:19 +0100972 clk_ctrl2 &= ~(WM9081_CLK_SYS_RATE_MASK | WM9081_SAMPLE_RATE_MASK);
973
Mark Brown8d50e442009-07-10 23:12:01 +0100974 aif1 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_1);
Mark Brown86ed3662009-05-22 15:01:19 +0100975
Mark Brown8d50e442009-07-10 23:12:01 +0100976 aif2 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_2);
Mark Brown86ed3662009-05-22 15:01:19 +0100977 aif2 &= ~WM9081_AIF_WL_MASK;
978
Mark Brown8d50e442009-07-10 23:12:01 +0100979 aif3 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_3);
Mark Brown86ed3662009-05-22 15:01:19 +0100980 aif3 &= ~WM9081_BCLK_DIV_MASK;
981
Mark Brown8d50e442009-07-10 23:12:01 +0100982 aif4 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_4);
Mark Brown86ed3662009-05-22 15:01:19 +0100983 aif4 &= ~WM9081_LRCLK_RATE_MASK;
984
Mark Brown86ed3662009-05-22 15:01:19 +0100985 wm9081->fs = params_rate(params);
Mark Brown86ed3662009-05-22 15:01:19 +0100986
Mark Browne0026be2009-08-11 16:28:39 +0100987 if (wm9081->tdm_width) {
988 /* If TDM is set up then that fixes our BCLK. */
Mark Brown86ed3662009-05-22 15:01:19 +0100989 int slots = ((aif1 & WM9081_AIFDAC_TDM_MODE_MASK) >>
990 WM9081_AIFDAC_TDM_MODE_SHIFT) + 1;
Mark Browne0026be2009-08-11 16:28:39 +0100991
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;
996
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;
1015 }
Mark Brown86ed3662009-05-22 15:01:19 +01001016 }
1017
1018 dev_dbg(codec->dev, "Target BCLK is %dHz\n", wm9081->bclk);
1019
1020 ret = configure_clock(codec);
1021 if (ret != 0)
1022 return ret;
1023
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 /
Joe Perchesa419aef2009-08-18 11:18:35 -07001030 clk_sys_rates[i].ratio) - wm9081->fs);
Mark Brown86ed3662009-05-22 15:01:19 +01001031 if (cur_val < best_val) {
1032 best = i;
1033 best_val = cur_val;
1034 }
1035 }
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);
1040
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;
1050 }
1051 }
1052 dev_dbg(codec->dev, "Selected SAMPLE_RATE of %dHz\n",
1053 sample_rates[best].rate);
Mark Brown01547242009-05-23 00:01:05 +01001054 clk_ctrl2 |= (sample_rates[best].sample_rate
1055 << WM9081_SAMPLE_RATE_SHIFT);
Mark Brown86ed3662009-05-22 15:01:19 +01001056
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;
1068 }
1069 }
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;
1074
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;
1078
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;
1084
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;
1092 }
1093 }
1094 s = &retune->configs[best];
1095
1096 dev_dbg(codec->dev, "ReTune Mobile %s tuned for %dHz\n",
1097 s->name, s->rate);
1098
1099 /* If the EQ is enabled then disable it while we write out */
Mark Brown8d50e442009-07-10 23:12:01 +01001100 eq1 = snd_soc_read(codec, WM9081_EQ_1) & WM9081_EQ_ENA;
Mark Brown86ed3662009-05-22 15:01:19 +01001101 if (eq1 & WM9081_EQ_ENA)
Mark Brown8d50e442009-07-10 23:12:01 +01001102 snd_soc_write(codec, WM9081_EQ_1, 0);
Mark Brown86ed3662009-05-22 15:01:19 +01001103
1104 /* Write out the other values */
1105 for (i = 1; i < ARRAY_SIZE(s->config); i++)
Mark Brown8d50e442009-07-10 23:12:01 +01001106 snd_soc_write(codec, WM9081_EQ_1 + i, s->config[i]);
Mark Brown86ed3662009-05-22 15:01:19 +01001107
1108 eq1 |= (s->config[0] & ~WM9081_EQ_ENA);
Mark Brown8d50e442009-07-10 23:12:01 +01001109 snd_soc_write(codec, WM9081_EQ_1, eq1);
Mark Brown86ed3662009-05-22 15:01:19 +01001110 }
1111
Mark Brown8d50e442009-07-10 23:12:01 +01001112 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);
Mark Brown86ed3662009-05-22 15:01:19 +01001116
1117 return 0;
1118}
1119
1120static int wm9081_digital_mute(struct snd_soc_dai *codec_dai, int mute)
1121{
1122 struct snd_soc_codec *codec = codec_dai->codec;
1123 unsigned int reg;
1124
Mark Brown8d50e442009-07-10 23:12:01 +01001125 reg = snd_soc_read(codec, WM9081_DAC_DIGITAL_2);
Mark Brown86ed3662009-05-22 15:01:19 +01001126
1127 if (mute)
1128 reg |= WM9081_DAC_MUTE;
1129 else
1130 reg &= ~WM9081_DAC_MUTE;
1131
Mark Brown8d50e442009-07-10 23:12:01 +01001132 snd_soc_write(codec, WM9081_DAC_DIGITAL_2, reg);
Mark Brown86ed3662009-05-22 15:01:19 +01001133
1134 return 0;
1135}
1136
1137static int wm9081_set_sysclk(struct snd_soc_dai *codec_dai,
1138 int clk_id, unsigned int freq, int dir)
1139{
1140 struct snd_soc_codec *codec = codec_dai->codec;
1141 struct wm9081_priv *wm9081 = codec->private_data;
1142
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;
1149
1150 default:
1151 return -EINVAL;
1152 }
1153
1154 return 0;
1155}
1156
1157static int wm9081_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03001158 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Mark Brown86ed3662009-05-22 15:01:19 +01001159{
1160 struct snd_soc_codec *codec = dai->codec;
Mark Browne0026be2009-08-11 16:28:39 +01001161 struct wm9081_priv *wm9081 = codec->private_data;
Mark Brown8d50e442009-07-10 23:12:01 +01001162 unsigned int aif1 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_1);
Mark Brown86ed3662009-05-22 15:01:19 +01001163
1164 aif1 &= ~(WM9081_AIFDAC_TDM_SLOT_MASK | WM9081_AIFDAC_TDM_MODE_MASK);
1165
Mark Browne0026be2009-08-11 16:28:39 +01001166 if (slots < 0 || slots > 4)
Mark Brown86ed3662009-05-22 15:01:19 +01001167 return -EINVAL;
1168
Mark Browne0026be2009-08-11 16:28:39 +01001169 wm9081->tdm_width = slot_width;
1170
1171 if (slots == 0)
1172 slots = 1;
1173
Mark Brown86ed3662009-05-22 15:01:19 +01001174 aif1 |= (slots - 1) << WM9081_AIFDAC_TDM_MODE_SHIFT;
1175
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03001176 switch (rx_mask) {
Mark Brown86ed3662009-05-22 15:01:19 +01001177 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;
1190 }
1191
Mark Brown8d50e442009-07-10 23:12:01 +01001192 snd_soc_write(codec, WM9081_AUDIO_INTERFACE_1, aif1);
Mark Brown86ed3662009-05-22 15:01:19 +01001193
1194 return 0;
1195}
1196
1197#define WM9081_RATES SNDRV_PCM_RATE_8000_96000
1198
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)
1202
1203static 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,
1209};
1210
1211/* We report two channels because the CODEC processes a stereo signal, even
1212 * though it is only capable of handling a mono output.
1213 */
1214struct 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,
1222 },
1223 .ops = &wm9081_dai_ops,
1224};
1225EXPORT_SYMBOL_GPL(wm9081_dai);
1226
1227
1228static struct snd_soc_codec *wm9081_codec;
1229
1230static int wm9081_probe(struct platform_device *pdev)
1231{
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;
1236
1237 if (wm9081_codec == NULL) {
1238 dev_err(&pdev->dev, "Codec device not registered\n");
1239 return -ENODEV;
1240 }
1241
1242 socdev->card->codec = wm9081_codec;
1243 codec = wm9081_codec;
1244 wm9081 = codec->private_data;
1245
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;
1251 }
1252
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));
1260 }
1261
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));
Mark Brown86ed3662009-05-22 15:01:19 +01001265
Mark Brown86ed3662009-05-22 15:01:19 +01001266 return ret;
1267
Mark Brown86ed3662009-05-22 15:01:19 +01001268pcm_err:
1269 return ret;
1270}
1271
1272static int wm9081_remove(struct platform_device *pdev)
1273{
1274 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1275
1276 snd_soc_free_pcms(socdev);
1277 snd_soc_dapm_free(socdev);
1278
1279 return 0;
1280}
1281
1282#ifdef CONFIG_PM
1283static int wm9081_suspend(struct platform_device *pdev, pm_message_t state)
1284{
1285 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1286 struct snd_soc_codec *codec = socdev->card->codec;
1287
1288 wm9081_set_bias_level(codec, SND_SOC_BIAS_OFF);
1289
1290 return 0;
1291}
1292
1293static int wm9081_resume(struct platform_device *pdev)
1294{
1295 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1296 struct snd_soc_codec *codec = socdev->card->codec;
1297 u16 *reg_cache = codec->reg_cache;
1298 int i;
1299
1300 for (i = 0; i < codec->reg_cache_size; i++) {
1301 if (i == WM9081_SOFTWARE_RESET)
1302 continue;
1303
Mark Brown8d50e442009-07-10 23:12:01 +01001304 snd_soc_write(codec, i, reg_cache[i]);
Mark Brown86ed3662009-05-22 15:01:19 +01001305 }
1306
1307 wm9081_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1308
1309 return 0;
1310}
1311#else
1312#define wm9081_suspend NULL
1313#define wm9081_resume NULL
1314#endif
1315
1316struct snd_soc_codec_device soc_codec_dev_wm9081 = {
1317 .probe = wm9081_probe,
1318 .remove = wm9081_remove,
1319 .suspend = wm9081_suspend,
1320 .resume = wm9081_resume,
1321};
1322EXPORT_SYMBOL_GPL(soc_codec_dev_wm9081);
1323
Mark Brown8d50e442009-07-10 23:12:01 +01001324static int wm9081_register(struct wm9081_priv *wm9081,
1325 enum snd_soc_control_type control)
Mark Brown86ed3662009-05-22 15:01:19 +01001326{
1327 struct snd_soc_codec *codec = &wm9081->codec;
1328 int ret;
1329 u16 reg;
1330
1331 if (wm9081_codec) {
1332 dev_err(codec->dev, "Another WM9081 is registered\n");
1333 ret = -EINVAL;
1334 goto err;
1335 }
1336
1337 mutex_init(&codec->mutex);
1338 INIT_LIST_HEAD(&codec->dapm_widgets);
1339 INIT_LIST_HEAD(&codec->dapm_paths);
1340
1341 codec->private_data = wm9081;
1342 codec->name = "WM9081";
1343 codec->owner = THIS_MODULE;
Mark Brown86ed3662009-05-22 15:01:19 +01001344 codec->dai = &wm9081_dai;
1345 codec->num_dai = 1;
1346 codec->reg_cache_size = ARRAY_SIZE(wm9081->reg_cache);
1347 codec->reg_cache = &wm9081->reg_cache;
1348 codec->bias_level = SND_SOC_BIAS_OFF;
1349 codec->set_bias_level = wm9081_set_bias_level;
Mark Brown8d50e442009-07-10 23:12:01 +01001350 codec->volatile_register = wm9081_volatile_register;
Mark Brown86ed3662009-05-22 15:01:19 +01001351
1352 memcpy(codec->reg_cache, wm9081_reg_defaults,
1353 sizeof(wm9081_reg_defaults));
1354
Mark Brown8d50e442009-07-10 23:12:01 +01001355 ret = snd_soc_codec_set_cache_io(codec, 8, 16, control);
1356 if (ret != 0) {
1357 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
1358 return ret;
1359 }
1360
1361 reg = snd_soc_read(codec, WM9081_SOFTWARE_RESET);
Mark Brown86ed3662009-05-22 15:01:19 +01001362 if (reg != 0x9081) {
1363 dev_err(codec->dev, "Device is not a WM9081: ID=0x%x\n", reg);
1364 ret = -EINVAL;
1365 goto err;
1366 }
1367
1368 ret = wm9081_reset(codec);
1369 if (ret < 0) {
1370 dev_err(codec->dev, "Failed to issue reset\n");
1371 return ret;
1372 }
1373
1374 wm9081_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1375
1376 /* Enable zero cross by default */
Mark Brown8d50e442009-07-10 23:12:01 +01001377 reg = snd_soc_read(codec, WM9081_ANALOGUE_LINEOUT);
1378 snd_soc_write(codec, WM9081_ANALOGUE_LINEOUT, reg | WM9081_LINEOUTZC);
1379 reg = snd_soc_read(codec, WM9081_ANALOGUE_SPEAKER_PGA);
1380 snd_soc_write(codec, WM9081_ANALOGUE_SPEAKER_PGA,
Mark Brown86ed3662009-05-22 15:01:19 +01001381 reg | WM9081_SPKPGAZC);
1382
1383 wm9081_dai.dev = codec->dev;
1384
1385 wm9081_codec = codec;
1386
1387 ret = snd_soc_register_codec(codec);
1388 if (ret != 0) {
1389 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
1390 return ret;
1391 }
1392
1393 ret = snd_soc_register_dai(&wm9081_dai);
1394 if (ret != 0) {
1395 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
1396 snd_soc_unregister_codec(codec);
1397 return ret;
1398 }
1399
1400 return 0;
1401
1402err:
1403 kfree(wm9081);
1404 return ret;
1405}
1406
1407static void wm9081_unregister(struct wm9081_priv *wm9081)
1408{
1409 wm9081_set_bias_level(&wm9081->codec, SND_SOC_BIAS_OFF);
1410 snd_soc_unregister_dai(&wm9081_dai);
1411 snd_soc_unregister_codec(&wm9081->codec);
1412 kfree(wm9081);
1413 wm9081_codec = NULL;
1414}
1415
1416static __devinit int wm9081_i2c_probe(struct i2c_client *i2c,
1417 const struct i2c_device_id *id)
1418{
1419 struct wm9081_priv *wm9081;
1420 struct snd_soc_codec *codec;
1421
1422 wm9081 = kzalloc(sizeof(struct wm9081_priv), GFP_KERNEL);
1423 if (wm9081 == NULL)
1424 return -ENOMEM;
1425
1426 codec = &wm9081->codec;
1427 codec->hw_write = (hw_write_t)i2c_master_send;
1428 wm9081->retune = i2c->dev.platform_data;
1429
1430 i2c_set_clientdata(i2c, wm9081);
1431 codec->control_data = i2c;
1432
1433 codec->dev = &i2c->dev;
1434
Mark Brown8d50e442009-07-10 23:12:01 +01001435 return wm9081_register(wm9081, SND_SOC_I2C);
Mark Brown86ed3662009-05-22 15:01:19 +01001436}
1437
1438static __devexit int wm9081_i2c_remove(struct i2c_client *client)
1439{
1440 struct wm9081_priv *wm9081 = i2c_get_clientdata(client);
1441 wm9081_unregister(wm9081);
1442 return 0;
1443}
1444
1445static const struct i2c_device_id wm9081_i2c_id[] = {
1446 { "wm9081", 0 },
1447 { }
1448};
1449MODULE_DEVICE_TABLE(i2c, wm9081_i2c_id);
1450
1451static struct i2c_driver wm9081_i2c_driver = {
1452 .driver = {
1453 .name = "wm9081",
1454 .owner = THIS_MODULE,
1455 },
1456 .probe = wm9081_i2c_probe,
1457 .remove = __devexit_p(wm9081_i2c_remove),
Mark Brown86ed3662009-05-22 15:01:19 +01001458 .id_table = wm9081_i2c_id,
1459};
1460
1461static int __init wm9081_modinit(void)
1462{
1463 int ret;
1464
1465 ret = i2c_add_driver(&wm9081_i2c_driver);
1466 if (ret != 0) {
1467 printk(KERN_ERR "Failed to register WM9081 I2C driver: %d\n",
1468 ret);
1469 }
1470
1471 return ret;
1472}
1473module_init(wm9081_modinit);
1474
1475static void __exit wm9081_exit(void)
1476{
1477 i2c_del_driver(&wm9081_i2c_driver);
1478}
1479module_exit(wm9081_exit);
1480
1481
1482MODULE_DESCRIPTION("ASoC WM9081 driver");
1483MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1484MODULE_LICENSE("GPL");