ASoC: Only enable thermal shutdown when required on WM9081
[deliverable/linux.git] / sound / soc / codecs / wm9081.c
CommitLineData
86ed3669
MB
1/*
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>
3ee845ac 18#include <linux/device.h>
86ed3669
MB
19#include <linux/pm.h>
20#include <linux/i2c.h>
21#include <linux/platform_device.h>
5a0e3ad6 22#include <linux/slab.h>
86ed3669
MB
23#include <sound/core.h>
24#include <sound/pcm.h>
25#include <sound/pcm_params.h>
26#include <sound/soc.h>
86ed3669
MB
27#include <sound/initval.h>
28#include <sound/tlv.h>
29
30#include <sound/wm9081.h>
31#include "wm9081.h"
32
33static u16 wm9081_reg_defaults[] = {
34 0x0000, /* R0 - Software Reset */
35 0x0000, /* R1 */
36 0x00B9, /* R2 - Analogue Lineout */
37 0x00B9, /* R3 - Analogue Speaker PGA */
38 0x0001, /* R4 - VMID Control */
39 0x0068, /* R5 - Bias Control 1 */
40 0x0000, /* R6 */
41 0x0000, /* R7 - Analogue Mixer */
42 0x0000, /* R8 - Anti Pop Control */
43 0x01DB, /* R9 - Analogue Speaker 1 */
44 0x0018, /* R10 - Analogue Speaker 2 */
45 0x0180, /* R11 - Power Management */
46 0x0000, /* R12 - Clock Control 1 */
47 0x0038, /* R13 - Clock Control 2 */
48 0x4000, /* R14 - Clock Control 3 */
49 0x0000, /* R15 */
50 0x0000, /* R16 - FLL Control 1 */
51 0x0200, /* R17 - FLL Control 2 */
52 0x0000, /* R18 - FLL Control 3 */
53 0x0204, /* R19 - FLL Control 4 */
54 0x0000, /* R20 - FLL Control 5 */
55 0x0000, /* R21 */
56 0x0000, /* R22 - Audio Interface 1 */
57 0x0002, /* R23 - Audio Interface 2 */
58 0x0008, /* R24 - Audio Interface 3 */
59 0x0022, /* R25 - Audio Interface 4 */
60 0x0000, /* R26 - Interrupt Status */
61 0x0006, /* R27 - Interrupt Status Mask */
62 0x0000, /* R28 - Interrupt Polarity */
63 0x0000, /* R29 - Interrupt Control */
64 0x00C0, /* R30 - DAC Digital 1 */
65 0x0008, /* R31 - DAC Digital 2 */
66 0x09AF, /* R32 - DRC 1 */
67 0x4201, /* R33 - DRC 2 */
68 0x0000, /* R34 - DRC 3 */
69 0x0000, /* R35 - DRC 4 */
70 0x0000, /* R36 */
71 0x0000, /* R37 */
72 0x0000, /* R38 - Write Sequencer 1 */
73 0x0000, /* R39 - Write Sequencer 2 */
74 0x0002, /* R40 - MW Slave 1 */
75 0x0000, /* R41 */
76 0x0000, /* R42 - EQ 1 */
77 0x0000, /* R43 - EQ 2 */
78 0x0FCA, /* R44 - EQ 3 */
79 0x0400, /* R45 - EQ 4 */
80 0x00B8, /* R46 - EQ 5 */
81 0x1EB5, /* R47 - EQ 6 */
82 0xF145, /* R48 - EQ 7 */
83 0x0B75, /* R49 - EQ 8 */
84 0x01C5, /* R50 - EQ 9 */
85 0x169E, /* R51 - EQ 10 */
86 0xF829, /* R52 - EQ 11 */
87 0x07AD, /* R53 - EQ 12 */
88 0x1103, /* R54 - EQ 13 */
89 0x1C58, /* R55 - EQ 14 */
90 0xF373, /* R56 - EQ 15 */
91 0x0A54, /* R57 - EQ 16 */
92 0x0558, /* R58 - EQ 17 */
93 0x0564, /* R59 - EQ 18 */
94 0x0559, /* R60 - EQ 19 */
95 0x4000, /* R61 - EQ 20 */
96};
97
98static struct {
99 int ratio;
100 int clk_sys_rate;
101} clk_sys_rates[] = {
102 { 64, 0 },
103 { 128, 1 },
104 { 192, 2 },
105 { 256, 3 },
106 { 384, 4 },
107 { 512, 5 },
108 { 768, 6 },
109 { 1024, 7 },
110 { 1408, 8 },
111 { 1536, 9 },
112};
113
114static struct {
115 int rate;
116 int sample_rate;
117} sample_rates[] = {
118 { 8000, 0 },
119 { 11025, 1 },
120 { 12000, 2 },
121 { 16000, 3 },
122 { 22050, 4 },
123 { 24000, 5 },
124 { 32000, 6 },
125 { 44100, 7 },
126 { 48000, 8 },
127 { 88200, 9 },
128 { 96000, 10 },
129};
130
131static struct {
132 int div; /* *10 due to .5s */
133 int bclk_div;
134} bclk_divs[] = {
135 { 10, 0 },
136 { 15, 1 },
137 { 20, 2 },
138 { 30, 3 },
139 { 40, 4 },
140 { 50, 5 },
141 { 55, 6 },
142 { 60, 7 },
143 { 80, 8 },
144 { 100, 9 },
145 { 110, 10 },
146 { 120, 11 },
147 { 160, 12 },
148 { 200, 13 },
149 { 220, 14 },
150 { 240, 15 },
151 { 250, 16 },
152 { 300, 17 },
153 { 320, 18 },
154 { 440, 19 },
155 { 480, 20 },
156};
157
158struct wm9081_priv {
f0fba2ad 159 enum snd_soc_control_type control_type;
86ed3669
MB
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;
e0026bea 168 int tdm_width;
4a5f7bda 169 struct wm9081_pdata pdata;
86ed3669
MB
170};
171
d4754ec9 172static int wm9081_volatile_register(struct snd_soc_codec *codec, unsigned int reg)
86ed3669
MB
173{
174 switch (reg) {
8d50e447 175 case WM9081_SOFTWARE_RESET:
f8faadb6 176 case WM9081_INTERRUPT_STATUS:
8d50e447 177 return 1;
86ed3669
MB
178 default:
179 return 0;
180 }
181}
182
86ed3669
MB
183static int wm9081_reset(struct snd_soc_codec *codec)
184{
8d50e447 185 return snd_soc_write(codec, WM9081_SOFTWARE_RESET, 0);
86ed3669
MB
186}
187
188static const DECLARE_TLV_DB_SCALE(drc_in_tlv, -4500, 75, 0);
189static const DECLARE_TLV_DB_SCALE(drc_out_tlv, -2250, 75, 0);
190static const DECLARE_TLV_DB_SCALE(drc_min_tlv, -1800, 600, 0);
191static unsigned int drc_max_tlv[] = {
192 TLV_DB_RANGE_HEAD(4),
193 0, 0, TLV_DB_SCALE_ITEM(1200, 0, 0),
194 1, 1, TLV_DB_SCALE_ITEM(1800, 0, 0),
195 2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
196 3, 3, TLV_DB_SCALE_ITEM(3600, 0, 0),
197};
198static const DECLARE_TLV_DB_SCALE(drc_qr_tlv, 1200, 600, 0);
199static const DECLARE_TLV_DB_SCALE(drc_startup_tlv, -300, 50, 0);
200
201static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
202
203static const DECLARE_TLV_DB_SCALE(in_tlv, -600, 600, 0);
204static const DECLARE_TLV_DB_SCALE(dac_tlv, -7200, 75, 1);
205static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
206
207static const char *drc_high_text[] = {
208 "1",
209 "1/2",
210 "1/4",
211 "1/8",
212 "1/16",
213 "0",
214};
215
216static const struct soc_enum drc_high =
217 SOC_ENUM_SINGLE(WM9081_DRC_3, 3, 6, drc_high_text);
218
219static const char *drc_low_text[] = {
220 "1",
221 "1/2",
222 "1/4",
223 "1/8",
224 "0",
225};
226
227static const struct soc_enum drc_low =
228 SOC_ENUM_SINGLE(WM9081_DRC_3, 0, 5, drc_low_text);
229
230static const char *drc_atk_text[] = {
231 "181us",
232 "181us",
233 "363us",
234 "726us",
235 "1.45ms",
236 "2.9ms",
237 "5.8ms",
238 "11.6ms",
239 "23.2ms",
240 "46.4ms",
241 "92.8ms",
242 "185.6ms",
243};
244
245static const struct soc_enum drc_atk =
246 SOC_ENUM_SINGLE(WM9081_DRC_2, 12, 12, drc_atk_text);
247
248static const char *drc_dcy_text[] = {
249 "186ms",
250 "372ms",
251 "743ms",
252 "1.49s",
253 "2.97s",
254 "5.94s",
255 "11.89s",
256 "23.78s",
257 "47.56s",
258};
259
260static const struct soc_enum drc_dcy =
261 SOC_ENUM_SINGLE(WM9081_DRC_2, 8, 9, drc_dcy_text);
262
263static const char *drc_qr_dcy_text[] = {
264 "0.725ms",
265 "1.45ms",
266 "5.8ms",
267};
268
269static const struct soc_enum drc_qr_dcy =
270 SOC_ENUM_SINGLE(WM9081_DRC_2, 4, 3, drc_qr_dcy_text);
271
272static const char *dac_deemph_text[] = {
273 "None",
274 "32kHz",
275 "44.1kHz",
276 "48kHz",
277};
278
279static const struct soc_enum dac_deemph =
280 SOC_ENUM_SINGLE(WM9081_DAC_DIGITAL_2, 1, 4, dac_deemph_text);
281
282static const char *speaker_mode_text[] = {
283 "Class D",
284 "Class AB",
285};
286
287static const struct soc_enum speaker_mode =
288 SOC_ENUM_SINGLE(WM9081_ANALOGUE_SPEAKER_2, 6, 2, speaker_mode_text);
289
290static int speaker_mode_get(struct snd_kcontrol *kcontrol,
291 struct snd_ctl_elem_value *ucontrol)
292{
293 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
294 unsigned int reg;
295
8d50e447 296 reg = snd_soc_read(codec, WM9081_ANALOGUE_SPEAKER_2);
86ed3669
MB
297 if (reg & WM9081_SPK_MODE)
298 ucontrol->value.integer.value[0] = 1;
299 else
300 ucontrol->value.integer.value[0] = 0;
301
302 return 0;
303}
304
305/*
306 * Stop any attempts to change speaker mode while the speaker is enabled.
307 *
25985edc 308 * We also have some special anti-pop controls dependent on speaker
86ed3669
MB
309 * mode which must be changed along with the mode.
310 */
311static int speaker_mode_put(struct snd_kcontrol *kcontrol,
312 struct snd_ctl_elem_value *ucontrol)
313{
314 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
8d50e447
MB
315 unsigned int reg_pwr = snd_soc_read(codec, WM9081_POWER_MANAGEMENT);
316 unsigned int reg2 = snd_soc_read(codec, WM9081_ANALOGUE_SPEAKER_2);
86ed3669
MB
317
318 /* Are we changing anything? */
319 if (ucontrol->value.integer.value[0] ==
320 ((reg2 & WM9081_SPK_MODE) != 0))
321 return 0;
322
323 /* Don't try to change modes while enabled */
324 if (reg_pwr & WM9081_SPK_ENA)
325 return -EINVAL;
326
327 if (ucontrol->value.integer.value[0]) {
328 /* Class AB */
329 reg2 &= ~(WM9081_SPK_INV_MUTE | WM9081_OUT_SPK_CTRL);
330 reg2 |= WM9081_SPK_MODE;
331 } else {
332 /* Class D */
333 reg2 |= WM9081_SPK_INV_MUTE | WM9081_OUT_SPK_CTRL;
334 reg2 &= ~WM9081_SPK_MODE;
335 }
336
8d50e447 337 snd_soc_write(codec, WM9081_ANALOGUE_SPEAKER_2, reg2);
86ed3669
MB
338
339 return 0;
340}
341
342static const struct snd_kcontrol_new wm9081_snd_controls[] = {
343SOC_SINGLE_TLV("IN1 Volume", WM9081_ANALOGUE_MIXER, 1, 1, 1, in_tlv),
344SOC_SINGLE_TLV("IN2 Volume", WM9081_ANALOGUE_MIXER, 3, 1, 1, in_tlv),
345
346SOC_SINGLE_TLV("Playback Volume", WM9081_DAC_DIGITAL_1, 1, 96, 0, dac_tlv),
347
348SOC_SINGLE("LINEOUT Switch", WM9081_ANALOGUE_LINEOUT, 7, 1, 1),
349SOC_SINGLE("LINEOUT ZC Switch", WM9081_ANALOGUE_LINEOUT, 6, 1, 0),
350SOC_SINGLE_TLV("LINEOUT Volume", WM9081_ANALOGUE_LINEOUT, 0, 63, 0, out_tlv),
351
352SOC_SINGLE("DRC Switch", WM9081_DRC_1, 15, 1, 0),
353SOC_ENUM("DRC High Slope", drc_high),
354SOC_ENUM("DRC Low Slope", drc_low),
355SOC_SINGLE_TLV("DRC Input Volume", WM9081_DRC_4, 5, 60, 1, drc_in_tlv),
356SOC_SINGLE_TLV("DRC Output Volume", WM9081_DRC_4, 0, 30, 1, drc_out_tlv),
357SOC_SINGLE_TLV("DRC Minimum Volume", WM9081_DRC_2, 2, 3, 1, drc_min_tlv),
358SOC_SINGLE_TLV("DRC Maximum Volume", WM9081_DRC_2, 0, 3, 0, drc_max_tlv),
359SOC_ENUM("DRC Attack", drc_atk),
360SOC_ENUM("DRC Decay", drc_dcy),
361SOC_SINGLE("DRC Quick Release Switch", WM9081_DRC_1, 2, 1, 0),
362SOC_SINGLE_TLV("DRC Quick Release Volume", WM9081_DRC_2, 6, 3, 0, drc_qr_tlv),
363SOC_ENUM("DRC Quick Release Decay", drc_qr_dcy),
364SOC_SINGLE_TLV("DRC Startup Volume", WM9081_DRC_1, 6, 18, 0, drc_startup_tlv),
365
366SOC_SINGLE("EQ Switch", WM9081_EQ_1, 0, 1, 0),
367
368SOC_SINGLE("Speaker DC Volume", WM9081_ANALOGUE_SPEAKER_1, 3, 5, 0),
369SOC_SINGLE("Speaker AC Volume", WM9081_ANALOGUE_SPEAKER_1, 0, 5, 0),
370SOC_SINGLE("Speaker Switch", WM9081_ANALOGUE_SPEAKER_PGA, 7, 1, 1),
371SOC_SINGLE("Speaker ZC Switch", WM9081_ANALOGUE_SPEAKER_PGA, 6, 1, 0),
372SOC_SINGLE_TLV("Speaker Volume", WM9081_ANALOGUE_SPEAKER_PGA, 0, 63, 0,
373 out_tlv),
374SOC_ENUM("DAC Deemphasis", dac_deemph),
375SOC_ENUM_EXT("Speaker Mode", speaker_mode, speaker_mode_get, speaker_mode_put),
376};
377
378static const struct snd_kcontrol_new wm9081_eq_controls[] = {
379SOC_SINGLE_TLV("EQ1 Volume", WM9081_EQ_1, 11, 24, 0, eq_tlv),
380SOC_SINGLE_TLV("EQ2 Volume", WM9081_EQ_1, 6, 24, 0, eq_tlv),
381SOC_SINGLE_TLV("EQ3 Volume", WM9081_EQ_1, 1, 24, 0, eq_tlv),
382SOC_SINGLE_TLV("EQ4 Volume", WM9081_EQ_2, 11, 24, 0, eq_tlv),
383SOC_SINGLE_TLV("EQ5 Volume", WM9081_EQ_2, 6, 24, 0, eq_tlv),
384};
385
386static const struct snd_kcontrol_new mixer[] = {
387SOC_DAPM_SINGLE("IN1 Switch", WM9081_ANALOGUE_MIXER, 0, 1, 0),
388SOC_DAPM_SINGLE("IN2 Switch", WM9081_ANALOGUE_MIXER, 2, 1, 0),
389SOC_DAPM_SINGLE("Playback Switch", WM9081_ANALOGUE_MIXER, 4, 1, 0),
390};
391
86ed3669
MB
392struct _fll_div {
393 u16 fll_fratio;
394 u16 fll_outdiv;
395 u16 fll_clk_ref_div;
396 u16 n;
397 u16 k;
398};
399
400/* The size in bits of the FLL divide multiplied by 10
401 * to allow rounding later */
402#define FIXED_FLL_SIZE ((1 << 16) * 10)
403
404static struct {
405 unsigned int min;
406 unsigned int max;
407 u16 fll_fratio;
408 int ratio;
409} fll_fratios[] = {
410 { 0, 64000, 4, 16 },
411 { 64000, 128000, 3, 8 },
412 { 128000, 256000, 2, 4 },
413 { 256000, 1000000, 1, 2 },
414 { 1000000, 13500000, 0, 1 },
415};
416
417static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
418 unsigned int Fout)
419{
420 u64 Kpart;
421 unsigned int K, Ndiv, Nmod, target;
422 unsigned int div;
423 int i;
424
425 /* Fref must be <=13.5MHz */
426 div = 1;
427 while ((Fref / div) > 13500000) {
428 div *= 2;
429
430 if (div > 8) {
431 pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
432 Fref);
433 return -EINVAL;
434 }
435 }
436 fll_div->fll_clk_ref_div = div / 2;
437
438 pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
439
440 /* Apply the division for our remaining calculations */
441 Fref /= div;
442
443 /* Fvco should be 90-100MHz; don't check the upper bound */
444 div = 0;
445 target = Fout * 2;
446 while (target < 90000000) {
447 div++;
448 target *= 2;
449 if (div > 7) {
450 pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
451 Fout);
452 return -EINVAL;
453 }
454 }
455 fll_div->fll_outdiv = div;
456
457 pr_debug("Fvco=%dHz\n", target);
458
25985edc 459 /* Find an appropriate FLL_FRATIO and factor it out of the target */
86ed3669
MB
460 for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
461 if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
462 fll_div->fll_fratio = fll_fratios[i].fll_fratio;
463 target /= fll_fratios[i].ratio;
464 break;
465 }
466 }
467 if (i == ARRAY_SIZE(fll_fratios)) {
468 pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
469 return -EINVAL;
470 }
471
472 /* Now, calculate N.K */
473 Ndiv = target / Fref;
474
475 fll_div->n = Ndiv;
476 Nmod = target % Fref;
477 pr_debug("Nmod=%d\n", Nmod);
478
479 /* Calculate fractional part - scale up so we can round. */
480 Kpart = FIXED_FLL_SIZE * (long long)Nmod;
481
482 do_div(Kpart, Fref);
483
484 K = Kpart & 0xFFFFFFFF;
485
486 if ((K % 10) >= 5)
487 K += 5;
488
489 /* Move down to proper range now rounding is done */
490 fll_div->k = K / 10;
491
492 pr_debug("N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
493 fll_div->n, fll_div->k,
494 fll_div->fll_fratio, fll_div->fll_outdiv,
495 fll_div->fll_clk_ref_div);
496
497 return 0;
498}
499
500static int wm9081_set_fll(struct snd_soc_codec *codec, int fll_id,
501 unsigned int Fref, unsigned int Fout)
502{
b2c812e2 503 struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
86ed3669
MB
504 u16 reg1, reg4, reg5;
505 struct _fll_div fll_div;
506 int ret;
507 int clk_sys_reg;
508
509 /* Any change? */
510 if (Fref == wm9081->fll_fref && Fout == wm9081->fll_fout)
511 return 0;
512
513 /* Disable the FLL */
514 if (Fout == 0) {
515 dev_dbg(codec->dev, "FLL disabled\n");
516 wm9081->fll_fref = 0;
517 wm9081->fll_fout = 0;
518
519 return 0;
520 }
521
522 ret = fll_factors(&fll_div, Fref, Fout);
523 if (ret != 0)
524 return ret;
525
8d50e447 526 reg5 = snd_soc_read(codec, WM9081_FLL_CONTROL_5);
86ed3669
MB
527 reg5 &= ~WM9081_FLL_CLK_SRC_MASK;
528
529 switch (fll_id) {
530 case WM9081_SYSCLK_FLL_MCLK:
531 reg5 |= 0x1;
532 break;
533
534 default:
535 dev_err(codec->dev, "Unknown FLL ID %d\n", fll_id);
536 return -EINVAL;
537 }
538
539 /* Disable CLK_SYS while we reconfigure */
8d50e447 540 clk_sys_reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_3);
86ed3669 541 if (clk_sys_reg & WM9081_CLK_SYS_ENA)
8d50e447 542 snd_soc_write(codec, WM9081_CLOCK_CONTROL_3,
86ed3669
MB
543 clk_sys_reg & ~WM9081_CLK_SYS_ENA);
544
545 /* Any FLL configuration change requires that the FLL be
546 * disabled first. */
8d50e447 547 reg1 = snd_soc_read(codec, WM9081_FLL_CONTROL_1);
86ed3669 548 reg1 &= ~WM9081_FLL_ENA;
8d50e447 549 snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1);
86ed3669
MB
550
551 /* Apply the configuration */
552 if (fll_div.k)
553 reg1 |= WM9081_FLL_FRAC_MASK;
554 else
555 reg1 &= ~WM9081_FLL_FRAC_MASK;
8d50e447 556 snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1);
86ed3669 557
8d50e447 558 snd_soc_write(codec, WM9081_FLL_CONTROL_2,
86ed3669
MB
559 (fll_div.fll_outdiv << WM9081_FLL_OUTDIV_SHIFT) |
560 (fll_div.fll_fratio << WM9081_FLL_FRATIO_SHIFT));
8d50e447 561 snd_soc_write(codec, WM9081_FLL_CONTROL_3, fll_div.k);
86ed3669 562
8d50e447 563 reg4 = snd_soc_read(codec, WM9081_FLL_CONTROL_4);
86ed3669
MB
564 reg4 &= ~WM9081_FLL_N_MASK;
565 reg4 |= fll_div.n << WM9081_FLL_N_SHIFT;
8d50e447 566 snd_soc_write(codec, WM9081_FLL_CONTROL_4, reg4);
86ed3669
MB
567
568 reg5 &= ~WM9081_FLL_CLK_REF_DIV_MASK;
569 reg5 |= fll_div.fll_clk_ref_div << WM9081_FLL_CLK_REF_DIV_SHIFT;
8d50e447 570 snd_soc_write(codec, WM9081_FLL_CONTROL_5, reg5);
86ed3669 571
249c5156
MB
572 /* Set gain to the recommended value */
573 snd_soc_update_bits(codec, WM9081_FLL_CONTROL_4,
574 WM9081_FLL_GAIN_MASK, 0);
575
86ed3669 576 /* Enable the FLL */
8d50e447 577 snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1 | WM9081_FLL_ENA);
86ed3669
MB
578
579 /* Then bring CLK_SYS up again if it was disabled */
580 if (clk_sys_reg & WM9081_CLK_SYS_ENA)
8d50e447 581 snd_soc_write(codec, WM9081_CLOCK_CONTROL_3, clk_sys_reg);
86ed3669
MB
582
583 dev_dbg(codec->dev, "FLL enabled at %dHz->%dHz\n", Fref, Fout);
584
585 wm9081->fll_fref = Fref;
586 wm9081->fll_fout = Fout;
587
588 return 0;
589}
590
591static int configure_clock(struct snd_soc_codec *codec)
592{
b2c812e2 593 struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
86ed3669
MB
594 int new_sysclk, i, target;
595 unsigned int reg;
596 int ret = 0;
597 int mclkdiv = 0;
598 int fll = 0;
599
600 switch (wm9081->sysclk_source) {
601 case WM9081_SYSCLK_MCLK:
602 if (wm9081->mclk_rate > 12225000) {
603 mclkdiv = 1;
604 wm9081->sysclk_rate = wm9081->mclk_rate / 2;
605 } else {
606 wm9081->sysclk_rate = wm9081->mclk_rate;
607 }
608 wm9081_set_fll(codec, WM9081_SYSCLK_FLL_MCLK, 0, 0);
609 break;
610
611 case WM9081_SYSCLK_FLL_MCLK:
612 /* If we have a sample rate calculate a CLK_SYS that
613 * gives us a suitable DAC configuration, plus BCLK.
614 * Ideally we would check to see if we can clock
615 * directly from MCLK and only use the FLL if this is
616 * not the case, though care must be taken with free
617 * running mode.
618 */
619 if (wm9081->master && wm9081->bclk) {
620 /* Make sure we can generate CLK_SYS and BCLK
621 * and that we've got 3MHz for optimal
622 * performance. */
623 for (i = 0; i < ARRAY_SIZE(clk_sys_rates); i++) {
624 target = wm9081->fs * clk_sys_rates[i].ratio;
0154724d 625 new_sysclk = target;
86ed3669
MB
626 if (target >= wm9081->bclk &&
627 target > 3000000)
0154724d 628 break;
86ed3669 629 }
4b75e947
MB
630
631 if (i == ARRAY_SIZE(clk_sys_rates))
632 return -EINVAL;
633
86ed3669
MB
634 } else if (wm9081->fs) {
635 for (i = 0; i < ARRAY_SIZE(clk_sys_rates); i++) {
636 new_sysclk = clk_sys_rates[i].ratio
637 * wm9081->fs;
638 if (new_sysclk > 3000000)
639 break;
640 }
4b75e947
MB
641
642 if (i == ARRAY_SIZE(clk_sys_rates))
643 return -EINVAL;
644
86ed3669
MB
645 } else {
646 new_sysclk = 12288000;
647 }
648
649 ret = wm9081_set_fll(codec, WM9081_SYSCLK_FLL_MCLK,
650 wm9081->mclk_rate, new_sysclk);
651 if (ret == 0) {
652 wm9081->sysclk_rate = new_sysclk;
653
654 /* Switch SYSCLK over to FLL */
655 fll = 1;
656 } else {
657 wm9081->sysclk_rate = wm9081->mclk_rate;
658 }
659 break;
660
661 default:
662 return -EINVAL;
663 }
664
8d50e447 665 reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_1);
86ed3669
MB
666 if (mclkdiv)
667 reg |= WM9081_MCLKDIV2;
668 else
669 reg &= ~WM9081_MCLKDIV2;
8d50e447 670 snd_soc_write(codec, WM9081_CLOCK_CONTROL_1, reg);
86ed3669 671
8d50e447 672 reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_3);
86ed3669
MB
673 if (fll)
674 reg |= WM9081_CLK_SRC_SEL;
675 else
676 reg &= ~WM9081_CLK_SRC_SEL;
8d50e447 677 snd_soc_write(codec, WM9081_CLOCK_CONTROL_3, reg);
86ed3669
MB
678
679 dev_dbg(codec->dev, "CLK_SYS is %dHz\n", wm9081->sysclk_rate);
680
681 return ret;
682}
683
684static int clk_sys_event(struct snd_soc_dapm_widget *w,
685 struct snd_kcontrol *kcontrol, int event)
686{
687 struct snd_soc_codec *codec = w->codec;
b2c812e2 688 struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
86ed3669
MB
689
690 /* This should be done on init() for bypass paths */
691 switch (wm9081->sysclk_source) {
692 case WM9081_SYSCLK_MCLK:
693 dev_dbg(codec->dev, "Using %dHz MCLK\n", wm9081->mclk_rate);
694 break;
695 case WM9081_SYSCLK_FLL_MCLK:
696 dev_dbg(codec->dev, "Using %dHz MCLK with FLL\n",
697 wm9081->mclk_rate);
698 break;
699 default:
700 dev_err(codec->dev, "System clock not configured\n");
701 return -EINVAL;
702 }
703
704 switch (event) {
705 case SND_SOC_DAPM_PRE_PMU:
706 configure_clock(codec);
707 break;
708
709 case SND_SOC_DAPM_POST_PMD:
710 /* Disable the FLL if it's running */
711 wm9081_set_fll(codec, 0, 0, 0);
712 break;
713 }
714
715 return 0;
716}
717
718static const struct snd_soc_dapm_widget wm9081_dapm_widgets[] = {
719SND_SOC_DAPM_INPUT("IN1"),
720SND_SOC_DAPM_INPUT("IN2"),
721
722SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM9081_POWER_MANAGEMENT, 0, 0),
723
724SND_SOC_DAPM_MIXER_NAMED_CTL("Mixer", SND_SOC_NOPM, 0, 0,
725 mixer, ARRAY_SIZE(mixer)),
726
727SND_SOC_DAPM_PGA("LINEOUT PGA", WM9081_POWER_MANAGEMENT, 4, 0, NULL, 0),
728
378a90f4 729SND_SOC_DAPM_PGA("Speaker PGA", WM9081_POWER_MANAGEMENT, 2, 0, NULL, 0),
4e8e78e3 730SND_SOC_DAPM_OUT_DRV("Speaker", WM9081_POWER_MANAGEMENT, 1, 0, NULL, 0),
86ed3669
MB
731
732SND_SOC_DAPM_OUTPUT("LINEOUT"),
733SND_SOC_DAPM_OUTPUT("SPKN"),
734SND_SOC_DAPM_OUTPUT("SPKP"),
735
736SND_SOC_DAPM_SUPPLY("CLK_SYS", WM9081_CLOCK_CONTROL_3, 0, 0, clk_sys_event,
737 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
738SND_SOC_DAPM_SUPPLY("CLK_DSP", WM9081_CLOCK_CONTROL_3, 1, 0, NULL, 0),
739SND_SOC_DAPM_SUPPLY("TOCLK", WM9081_CLOCK_CONTROL_3, 2, 0, NULL, 0),
a04e0c86 740SND_SOC_DAPM_SUPPLY("TSENSE", WM9081_POWER_MANAGEMENT, 7, 0, NULL, 0),
86ed3669
MB
741};
742
743
149c7b44 744static const struct snd_soc_dapm_route wm9081_audio_paths[] = {
86ed3669
MB
745 { "DAC", NULL, "CLK_SYS" },
746 { "DAC", NULL, "CLK_DSP" },
747
748 { "Mixer", "IN1 Switch", "IN1" },
749 { "Mixer", "IN2 Switch", "IN2" },
750 { "Mixer", "Playback Switch", "DAC" },
751
752 { "LINEOUT PGA", NULL, "Mixer" },
753 { "LINEOUT PGA", NULL, "TOCLK" },
754 { "LINEOUT PGA", NULL, "CLK_SYS" },
755
756 { "LINEOUT", NULL, "LINEOUT PGA" },
757
758 { "Speaker PGA", NULL, "Mixer" },
759 { "Speaker PGA", NULL, "TOCLK" },
760 { "Speaker PGA", NULL, "CLK_SYS" },
761
378a90f4 762 { "Speaker", NULL, "Speaker PGA" },
a04e0c86 763 { "Speaker", NULL, "TSENSE" },
378a90f4
MB
764
765 { "SPKN", NULL, "Speaker" },
766 { "SPKP", NULL, "Speaker" },
86ed3669
MB
767};
768
769static int wm9081_set_bias_level(struct snd_soc_codec *codec,
770 enum snd_soc_bias_level level)
771{
772 u16 reg;
773
774 switch (level) {
775 case SND_SOC_BIAS_ON:
776 break;
777
778 case SND_SOC_BIAS_PREPARE:
779 /* VMID=2*40k */
8d50e447 780 reg = snd_soc_read(codec, WM9081_VMID_CONTROL);
86ed3669
MB
781 reg &= ~WM9081_VMID_SEL_MASK;
782 reg |= 0x2;
8d50e447 783 snd_soc_write(codec, WM9081_VMID_CONTROL, reg);
86ed3669
MB
784
785 /* Normal bias current */
8d50e447 786 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
86ed3669 787 reg &= ~WM9081_STBY_BIAS_ENA;
8d50e447 788 snd_soc_write(codec, WM9081_BIAS_CONTROL_1, reg);
86ed3669
MB
789 break;
790
791 case SND_SOC_BIAS_STANDBY:
792 /* Initial cold start */
ce6120cc 793 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
86ed3669 794 /* Disable LINEOUT discharge */
8d50e447 795 reg = snd_soc_read(codec, WM9081_ANTI_POP_CONTROL);
86ed3669 796 reg &= ~WM9081_LINEOUT_DISCH;
8d50e447 797 snd_soc_write(codec, WM9081_ANTI_POP_CONTROL, reg);
86ed3669
MB
798
799 /* Select startup bias source */
8d50e447 800 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
86ed3669 801 reg |= WM9081_BIAS_SRC | WM9081_BIAS_ENA;
8d50e447 802 snd_soc_write(codec, WM9081_BIAS_CONTROL_1, reg);
86ed3669
MB
803
804 /* VMID 2*4k; Soft VMID ramp enable */
8d50e447 805 reg = snd_soc_read(codec, WM9081_VMID_CONTROL);
86ed3669 806 reg |= WM9081_VMID_RAMP | 0x6;
8d50e447 807 snd_soc_write(codec, WM9081_VMID_CONTROL, reg);
86ed3669
MB
808
809 mdelay(100);
810
811 /* Normal bias enable & soft start off */
812 reg |= WM9081_BIAS_ENA;
813 reg &= ~WM9081_VMID_RAMP;
8d50e447 814 snd_soc_write(codec, WM9081_VMID_CONTROL, reg);
86ed3669
MB
815
816 /* Standard bias source */
8d50e447 817 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
86ed3669 818 reg &= ~WM9081_BIAS_SRC;
8d50e447 819 snd_soc_write(codec, WM9081_BIAS_CONTROL_1, reg);
86ed3669
MB
820 }
821
822 /* VMID 2*240k */
8d50e447 823 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
86ed3669 824 reg &= ~WM9081_VMID_SEL_MASK;
9f9619a0 825 reg |= 0x04;
8d50e447 826 snd_soc_write(codec, WM9081_VMID_CONTROL, reg);
86ed3669
MB
827
828 /* Standby bias current on */
8d50e447 829 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
86ed3669 830 reg |= WM9081_STBY_BIAS_ENA;
8d50e447 831 snd_soc_write(codec, WM9081_BIAS_CONTROL_1, reg);
86ed3669
MB
832 break;
833
834 case SND_SOC_BIAS_OFF:
835 /* Startup bias source */
8d50e447 836 reg = snd_soc_read(codec, WM9081_BIAS_CONTROL_1);
86ed3669 837 reg |= WM9081_BIAS_SRC;
8d50e447 838 snd_soc_write(codec, WM9081_BIAS_CONTROL_1, reg);
86ed3669
MB
839
840 /* Disable VMID and biases with soft ramping */
8d50e447 841 reg = snd_soc_read(codec, WM9081_VMID_CONTROL);
86ed3669
MB
842 reg &= ~(WM9081_VMID_SEL_MASK | WM9081_BIAS_ENA);
843 reg |= WM9081_VMID_RAMP;
8d50e447 844 snd_soc_write(codec, WM9081_VMID_CONTROL, reg);
86ed3669
MB
845
846 /* Actively discharge LINEOUT */
8d50e447 847 reg = snd_soc_read(codec, WM9081_ANTI_POP_CONTROL);
86ed3669 848 reg |= WM9081_LINEOUT_DISCH;
8d50e447 849 snd_soc_write(codec, WM9081_ANTI_POP_CONTROL, reg);
86ed3669
MB
850 break;
851 }
852
ce6120cc 853 codec->dapm.bias_level = level;
86ed3669
MB
854
855 return 0;
856}
857
858static int wm9081_set_dai_fmt(struct snd_soc_dai *dai,
859 unsigned int fmt)
860{
861 struct snd_soc_codec *codec = dai->codec;
b2c812e2 862 struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
8d50e447 863 unsigned int aif2 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_2);
86ed3669
MB
864
865 aif2 &= ~(WM9081_AIF_BCLK_INV | WM9081_AIF_LRCLK_INV |
866 WM9081_BCLK_DIR | WM9081_LRCLK_DIR | WM9081_AIF_FMT_MASK);
867
868 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
869 case SND_SOC_DAIFMT_CBS_CFS:
870 wm9081->master = 0;
871 break;
872 case SND_SOC_DAIFMT_CBS_CFM:
873 aif2 |= WM9081_LRCLK_DIR;
874 wm9081->master = 1;
875 break;
876 case SND_SOC_DAIFMT_CBM_CFS:
877 aif2 |= WM9081_BCLK_DIR;
878 wm9081->master = 1;
879 break;
880 case SND_SOC_DAIFMT_CBM_CFM:
881 aif2 |= WM9081_LRCLK_DIR | WM9081_BCLK_DIR;
882 wm9081->master = 1;
883 break;
884 default:
885 return -EINVAL;
886 }
887
888 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
889 case SND_SOC_DAIFMT_DSP_B:
890 aif2 |= WM9081_AIF_LRCLK_INV;
891 case SND_SOC_DAIFMT_DSP_A:
892 aif2 |= 0x3;
893 break;
894 case SND_SOC_DAIFMT_I2S:
895 aif2 |= 0x2;
896 break;
897 case SND_SOC_DAIFMT_RIGHT_J:
898 break;
899 case SND_SOC_DAIFMT_LEFT_J:
900 aif2 |= 0x1;
901 break;
902 default:
903 return -EINVAL;
904 }
905
906 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
907 case SND_SOC_DAIFMT_DSP_A:
908 case SND_SOC_DAIFMT_DSP_B:
909 /* frame inversion not valid for DSP modes */
910 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
911 case SND_SOC_DAIFMT_NB_NF:
912 break;
913 case SND_SOC_DAIFMT_IB_NF:
914 aif2 |= WM9081_AIF_BCLK_INV;
915 break;
916 default:
917 return -EINVAL;
918 }
919 break;
920
921 case SND_SOC_DAIFMT_I2S:
922 case SND_SOC_DAIFMT_RIGHT_J:
923 case SND_SOC_DAIFMT_LEFT_J:
924 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
925 case SND_SOC_DAIFMT_NB_NF:
926 break;
927 case SND_SOC_DAIFMT_IB_IF:
928 aif2 |= WM9081_AIF_BCLK_INV | WM9081_AIF_LRCLK_INV;
929 break;
930 case SND_SOC_DAIFMT_IB_NF:
931 aif2 |= WM9081_AIF_BCLK_INV;
932 break;
933 case SND_SOC_DAIFMT_NB_IF:
934 aif2 |= WM9081_AIF_LRCLK_INV;
935 break;
936 default:
937 return -EINVAL;
938 }
939 break;
940 default:
941 return -EINVAL;
942 }
943
8d50e447 944 snd_soc_write(codec, WM9081_AUDIO_INTERFACE_2, aif2);
86ed3669
MB
945
946 return 0;
947}
948
949static int wm9081_hw_params(struct snd_pcm_substream *substream,
950 struct snd_pcm_hw_params *params,
951 struct snd_soc_dai *dai)
952{
953 struct snd_soc_codec *codec = dai->codec;
b2c812e2 954 struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
86ed3669
MB
955 int ret, i, best, best_val, cur_val;
956 unsigned int clk_ctrl2, aif1, aif2, aif3, aif4;
957
8d50e447 958 clk_ctrl2 = snd_soc_read(codec, WM9081_CLOCK_CONTROL_2);
86ed3669
MB
959 clk_ctrl2 &= ~(WM9081_CLK_SYS_RATE_MASK | WM9081_SAMPLE_RATE_MASK);
960
8d50e447 961 aif1 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_1);
86ed3669 962
8d50e447 963 aif2 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_2);
86ed3669
MB
964 aif2 &= ~WM9081_AIF_WL_MASK;
965
8d50e447 966 aif3 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_3);
86ed3669
MB
967 aif3 &= ~WM9081_BCLK_DIV_MASK;
968
8d50e447 969 aif4 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_4);
86ed3669
MB
970 aif4 &= ~WM9081_LRCLK_RATE_MASK;
971
86ed3669 972 wm9081->fs = params_rate(params);
86ed3669 973
e0026bea
MB
974 if (wm9081->tdm_width) {
975 /* If TDM is set up then that fixes our BCLK. */
86ed3669
MB
976 int slots = ((aif1 & WM9081_AIFDAC_TDM_MODE_MASK) >>
977 WM9081_AIFDAC_TDM_MODE_SHIFT) + 1;
e0026bea
MB
978
979 wm9081->bclk = wm9081->fs * wm9081->tdm_width * slots;
980 } else {
981 /* Otherwise work out a BCLK from the sample size */
982 wm9081->bclk = 2 * wm9081->fs;
983
984 switch (params_format(params)) {
985 case SNDRV_PCM_FORMAT_S16_LE:
986 wm9081->bclk *= 16;
987 break;
988 case SNDRV_PCM_FORMAT_S20_3LE:
989 wm9081->bclk *= 20;
990 aif2 |= 0x4;
991 break;
992 case SNDRV_PCM_FORMAT_S24_LE:
993 wm9081->bclk *= 24;
994 aif2 |= 0x8;
995 break;
996 case SNDRV_PCM_FORMAT_S32_LE:
997 wm9081->bclk *= 32;
998 aif2 |= 0xc;
999 break;
1000 default:
1001 return -EINVAL;
1002 }
86ed3669
MB
1003 }
1004
1005 dev_dbg(codec->dev, "Target BCLK is %dHz\n", wm9081->bclk);
1006
1007 ret = configure_clock(codec);
1008 if (ret != 0)
1009 return ret;
1010
1011 /* Select nearest CLK_SYS_RATE */
1012 best = 0;
1013 best_val = abs((wm9081->sysclk_rate / clk_sys_rates[0].ratio)
1014 - wm9081->fs);
1015 for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
1016 cur_val = abs((wm9081->sysclk_rate /
a419aef8 1017 clk_sys_rates[i].ratio) - wm9081->fs);
86ed3669
MB
1018 if (cur_val < best_val) {
1019 best = i;
1020 best_val = cur_val;
1021 }
1022 }
1023 dev_dbg(codec->dev, "Selected CLK_SYS_RATIO of %d\n",
1024 clk_sys_rates[best].ratio);
1025 clk_ctrl2 |= (clk_sys_rates[best].clk_sys_rate
1026 << WM9081_CLK_SYS_RATE_SHIFT);
1027
1028 /* SAMPLE_RATE */
1029 best = 0;
1030 best_val = abs(wm9081->fs - sample_rates[0].rate);
1031 for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
1032 /* Closest match */
1033 cur_val = abs(wm9081->fs - sample_rates[i].rate);
1034 if (cur_val < best_val) {
1035 best = i;
1036 best_val = cur_val;
1037 }
1038 }
1039 dev_dbg(codec->dev, "Selected SAMPLE_RATE of %dHz\n",
1040 sample_rates[best].rate);
0154724d
MB
1041 clk_ctrl2 |= (sample_rates[best].sample_rate
1042 << WM9081_SAMPLE_RATE_SHIFT);
86ed3669
MB
1043
1044 /* BCLK_DIV */
1045 best = 0;
1046 best_val = INT_MAX;
1047 for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
1048 cur_val = ((wm9081->sysclk_rate * 10) / bclk_divs[i].div)
1049 - wm9081->bclk;
1050 if (cur_val < 0) /* Table is sorted */
1051 break;
1052 if (cur_val < best_val) {
1053 best = i;
1054 best_val = cur_val;
1055 }
1056 }
1057 wm9081->bclk = (wm9081->sysclk_rate * 10) / bclk_divs[best].div;
1058 dev_dbg(codec->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
1059 bclk_divs[best].div, wm9081->bclk);
1060 aif3 |= bclk_divs[best].bclk_div;
1061
1062 /* LRCLK is a simple fraction of BCLK */
1063 dev_dbg(codec->dev, "LRCLK_RATE is %d\n", wm9081->bclk / wm9081->fs);
1064 aif4 |= wm9081->bclk / wm9081->fs;
1065
1066 /* Apply a ReTune Mobile configuration if it's in use */
4a5f7bda
MB
1067 if (wm9081->pdata.num_retune_configs) {
1068 struct wm9081_pdata *pdata = &wm9081->pdata;
86ed3669
MB
1069 struct wm9081_retune_mobile_setting *s;
1070 int eq1;
1071
1072 best = 0;
4a5f7bda
MB
1073 best_val = abs(pdata->retune_configs[0].rate - wm9081->fs);
1074 for (i = 0; i < pdata->num_retune_configs; i++) {
1075 cur_val = abs(pdata->retune_configs[i].rate -
1076 wm9081->fs);
86ed3669
MB
1077 if (cur_val < best_val) {
1078 best_val = cur_val;
1079 best = i;
1080 }
1081 }
4a5f7bda 1082 s = &pdata->retune_configs[best];
86ed3669
MB
1083
1084 dev_dbg(codec->dev, "ReTune Mobile %s tuned for %dHz\n",
1085 s->name, s->rate);
1086
1087 /* If the EQ is enabled then disable it while we write out */
8d50e447 1088 eq1 = snd_soc_read(codec, WM9081_EQ_1) & WM9081_EQ_ENA;
86ed3669 1089 if (eq1 & WM9081_EQ_ENA)
8d50e447 1090 snd_soc_write(codec, WM9081_EQ_1, 0);
86ed3669
MB
1091
1092 /* Write out the other values */
1093 for (i = 1; i < ARRAY_SIZE(s->config); i++)
8d50e447 1094 snd_soc_write(codec, WM9081_EQ_1 + i, s->config[i]);
86ed3669
MB
1095
1096 eq1 |= (s->config[0] & ~WM9081_EQ_ENA);
8d50e447 1097 snd_soc_write(codec, WM9081_EQ_1, eq1);
86ed3669
MB
1098 }
1099
8d50e447
MB
1100 snd_soc_write(codec, WM9081_CLOCK_CONTROL_2, clk_ctrl2);
1101 snd_soc_write(codec, WM9081_AUDIO_INTERFACE_2, aif2);
1102 snd_soc_write(codec, WM9081_AUDIO_INTERFACE_3, aif3);
1103 snd_soc_write(codec, WM9081_AUDIO_INTERFACE_4, aif4);
86ed3669
MB
1104
1105 return 0;
1106}
1107
1108static int wm9081_digital_mute(struct snd_soc_dai *codec_dai, int mute)
1109{
1110 struct snd_soc_codec *codec = codec_dai->codec;
1111 unsigned int reg;
1112
8d50e447 1113 reg = snd_soc_read(codec, WM9081_DAC_DIGITAL_2);
86ed3669
MB
1114
1115 if (mute)
1116 reg |= WM9081_DAC_MUTE;
1117 else
1118 reg &= ~WM9081_DAC_MUTE;
1119
8d50e447 1120 snd_soc_write(codec, WM9081_DAC_DIGITAL_2, reg);
86ed3669
MB
1121
1122 return 0;
1123}
1124
da1c6ea6
MB
1125static int wm9081_set_sysclk(struct snd_soc_codec *codec, int clk_id,
1126 int source, unsigned int freq, int dir)
86ed3669 1127{
b2c812e2 1128 struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
86ed3669
MB
1129
1130 switch (clk_id) {
1131 case WM9081_SYSCLK_MCLK:
1132 case WM9081_SYSCLK_FLL_MCLK:
1133 wm9081->sysclk_source = clk_id;
1134 wm9081->mclk_rate = freq;
1135 break;
1136
1137 default:
1138 return -EINVAL;
1139 }
1140
1141 return 0;
1142}
1143
1144static int wm9081_set_tdm_slot(struct snd_soc_dai *dai,
a5479e38 1145 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
86ed3669
MB
1146{
1147 struct snd_soc_codec *codec = dai->codec;
b2c812e2 1148 struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
8d50e447 1149 unsigned int aif1 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_1);
86ed3669
MB
1150
1151 aif1 &= ~(WM9081_AIFDAC_TDM_SLOT_MASK | WM9081_AIFDAC_TDM_MODE_MASK);
1152
e0026bea 1153 if (slots < 0 || slots > 4)
86ed3669
MB
1154 return -EINVAL;
1155
e0026bea
MB
1156 wm9081->tdm_width = slot_width;
1157
1158 if (slots == 0)
1159 slots = 1;
1160
86ed3669
MB
1161 aif1 |= (slots - 1) << WM9081_AIFDAC_TDM_MODE_SHIFT;
1162
a5479e38 1163 switch (rx_mask) {
86ed3669
MB
1164 case 1:
1165 break;
1166 case 2:
1167 aif1 |= 0x10;
1168 break;
1169 case 4:
1170 aif1 |= 0x20;
1171 break;
1172 case 8:
1173 aif1 |= 0x30;
1174 break;
1175 default:
1176 return -EINVAL;
1177 }
1178
8d50e447 1179 snd_soc_write(codec, WM9081_AUDIO_INTERFACE_1, aif1);
86ed3669
MB
1180
1181 return 0;
1182}
1183
1184#define WM9081_RATES SNDRV_PCM_RATE_8000_96000
1185
1186#define WM9081_FORMATS \
1187 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1188 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
1189
1190static struct snd_soc_dai_ops wm9081_dai_ops = {
1191 .hw_params = wm9081_hw_params,
86ed3669
MB
1192 .set_fmt = wm9081_set_dai_fmt,
1193 .digital_mute = wm9081_digital_mute,
1194 .set_tdm_slot = wm9081_set_tdm_slot,
1195};
1196
1197/* We report two channels because the CODEC processes a stereo signal, even
1198 * though it is only capable of handling a mono output.
1199 */
f0fba2ad
LG
1200static struct snd_soc_dai_driver wm9081_dai = {
1201 .name = "wm9081-hifi",
86ed3669
MB
1202 .playback = {
1203 .stream_name = "HiFi Playback",
1204 .channels_min = 1,
1205 .channels_max = 2,
1206 .rates = WM9081_RATES,
1207 .formats = WM9081_FORMATS,
1208 },
1209 .ops = &wm9081_dai_ops,
1210};
86ed3669 1211
f0fba2ad 1212static int wm9081_probe(struct snd_soc_codec *codec)
86ed3669 1213{
f0fba2ad
LG
1214 struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
1215 int ret;
1216 u16 reg;
86ed3669 1217
f0fba2ad
LG
1218 ret = snd_soc_codec_set_cache_io(codec, 8, 16, wm9081->control_type);
1219 if (ret != 0) {
1220 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
1221 return ret;
86ed3669
MB
1222 }
1223
f0fba2ad
LG
1224 reg = snd_soc_read(codec, WM9081_SOFTWARE_RESET);
1225 if (reg != 0x9081) {
1226 dev_err(codec->dev, "Device is not a WM9081: ID=0x%x\n", reg);
1227 ret = -EINVAL;
1228 return ret;
1229 }
86ed3669 1230
f0fba2ad 1231 ret = wm9081_reset(codec);
86ed3669 1232 if (ret < 0) {
f0fba2ad
LG
1233 dev_err(codec->dev, "Failed to issue reset\n");
1234 return ret;
86ed3669
MB
1235 }
1236
4a5f7bda
MB
1237 reg = 0;
1238 if (wm9081->pdata.irq_high)
1239 reg |= WM9081_IRQ_POL;
1240 if (!wm9081->pdata.irq_cmos)
1241 reg |= WM9081_IRQ_OP_CTRL;
1242 snd_soc_update_bits(codec, WM9081_INTERRUPT_CONTROL,
1243 WM9081_IRQ_POL | WM9081_IRQ_OP_CTRL, reg);
1244
f0fba2ad
LG
1245 wm9081_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1246
1247 /* Enable zero cross by default */
1248 reg = snd_soc_read(codec, WM9081_ANALOGUE_LINEOUT);
1249 snd_soc_write(codec, WM9081_ANALOGUE_LINEOUT, reg | WM9081_LINEOUTZC);
1250 reg = snd_soc_read(codec, WM9081_ANALOGUE_SPEAKER_PGA);
1251 snd_soc_write(codec, WM9081_ANALOGUE_SPEAKER_PGA,
1252 reg | WM9081_SPKPGAZC);
1253
4a5f7bda 1254 if (!wm9081->pdata.num_retune_configs) {
86ed3669
MB
1255 dev_dbg(codec->dev,
1256 "No ReTune Mobile data, using normal EQ\n");
1257 snd_soc_add_controls(codec, wm9081_eq_controls,
1258 ARRAY_SIZE(wm9081_eq_controls));
1259 }
1260
86ed3669 1261 return ret;
86ed3669
MB
1262}
1263
f0fba2ad 1264static int wm9081_remove(struct snd_soc_codec *codec)
86ed3669 1265{
f0fba2ad 1266 wm9081_set_bias_level(codec, SND_SOC_BIAS_OFF);
86ed3669
MB
1267 return 0;
1268}
1269
1270#ifdef CONFIG_PM
f0fba2ad 1271static int wm9081_suspend(struct snd_soc_codec *codec, pm_message_t state)
86ed3669 1272{
86ed3669
MB
1273 wm9081_set_bias_level(codec, SND_SOC_BIAS_OFF);
1274
1275 return 0;
1276}
1277
f0fba2ad 1278static int wm9081_resume(struct snd_soc_codec *codec)
86ed3669 1279{
86ed3669
MB
1280 u16 *reg_cache = codec->reg_cache;
1281 int i;
1282
f0fba2ad 1283 for (i = 0; i < codec->driver->reg_cache_size; i++) {
86ed3669
MB
1284 if (i == WM9081_SOFTWARE_RESET)
1285 continue;
1286
8d50e447 1287 snd_soc_write(codec, i, reg_cache[i]);
86ed3669
MB
1288 }
1289
1290 wm9081_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1291
1292 return 0;
1293}
1294#else
1295#define wm9081_suspend NULL
1296#define wm9081_resume NULL
1297#endif
1298
f0fba2ad 1299static struct snd_soc_codec_driver soc_codec_dev_wm9081 = {
86ed3669
MB
1300 .probe = wm9081_probe,
1301 .remove = wm9081_remove,
1302 .suspend = wm9081_suspend,
1303 .resume = wm9081_resume,
63d24b79
MB
1304
1305 .set_sysclk = wm9081_set_sysclk,
f0fba2ad 1306 .set_bias_level = wm9081_set_bias_level,
63d24b79 1307
e5eec34c 1308 .reg_cache_size = ARRAY_SIZE(wm9081_reg_defaults),
f0fba2ad
LG
1309 .reg_word_size = sizeof(u16),
1310 .reg_cache_default = wm9081_reg_defaults,
1311 .volatile_register = wm9081_volatile_register,
63d24b79 1312
680fa1f8
MB
1313 .controls = wm9081_snd_controls,
1314 .num_controls = ARRAY_SIZE(wm9081_snd_controls),
149c7b44
MB
1315 .dapm_widgets = wm9081_dapm_widgets,
1316 .num_dapm_widgets = ARRAY_SIZE(wm9081_dapm_widgets),
1317 .dapm_routes = wm9081_audio_paths,
1318 .num_dapm_routes = ARRAY_SIZE(wm9081_audio_paths),
86ed3669 1319};
86ed3669 1320
f0fba2ad 1321#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
86ed3669
MB
1322static __devinit int wm9081_i2c_probe(struct i2c_client *i2c,
1323 const struct i2c_device_id *id)
1324{
1325 struct wm9081_priv *wm9081;
f0fba2ad 1326 int ret;
86ed3669
MB
1327
1328 wm9081 = kzalloc(sizeof(struct wm9081_priv), GFP_KERNEL);
1329 if (wm9081 == NULL)
1330 return -ENOMEM;
1331
86ed3669 1332 i2c_set_clientdata(i2c, wm9081);
7f984b55 1333 wm9081->control_type = SND_SOC_I2C;
86ed3669 1334
3ee845ac 1335 if (dev_get_platdata(&i2c->dev))
4a5f7bda
MB
1336 memcpy(&wm9081->pdata, dev_get_platdata(&i2c->dev),
1337 sizeof(wm9081->pdata));
3ee845ac 1338
f0fba2ad
LG
1339 ret = snd_soc_register_codec(&i2c->dev,
1340 &soc_codec_dev_wm9081, &wm9081_dai, 1);
1341 if (ret < 0)
1342 kfree(wm9081);
1343 return ret;
86ed3669
MB
1344}
1345
1346static __devexit int wm9081_i2c_remove(struct i2c_client *client)
1347{
f0fba2ad
LG
1348 snd_soc_unregister_codec(&client->dev);
1349 kfree(i2c_get_clientdata(client));
86ed3669
MB
1350 return 0;
1351}
1352
1353static const struct i2c_device_id wm9081_i2c_id[] = {
1354 { "wm9081", 0 },
1355 { }
1356};
1357MODULE_DEVICE_TABLE(i2c, wm9081_i2c_id);
1358
1359static struct i2c_driver wm9081_i2c_driver = {
1360 .driver = {
2031c064 1361 .name = "wm9081",
86ed3669
MB
1362 .owner = THIS_MODULE,
1363 },
1364 .probe = wm9081_i2c_probe,
1365 .remove = __devexit_p(wm9081_i2c_remove),
1366 .id_table = wm9081_i2c_id,
1367};
f0fba2ad 1368#endif
86ed3669
MB
1369
1370static int __init wm9081_modinit(void)
1371{
f0fba2ad
LG
1372 int ret = 0;
1373#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
86ed3669
MB
1374 ret = i2c_add_driver(&wm9081_i2c_driver);
1375 if (ret != 0) {
1376 printk(KERN_ERR "Failed to register WM9081 I2C driver: %d\n",
1377 ret);
1378 }
f0fba2ad 1379#endif
86ed3669
MB
1380 return ret;
1381}
1382module_init(wm9081_modinit);
1383
1384static void __exit wm9081_exit(void)
1385{
f0fba2ad 1386#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
86ed3669 1387 i2c_del_driver(&wm9081_i2c_driver);
f0fba2ad 1388#endif
86ed3669
MB
1389}
1390module_exit(wm9081_exit);
1391
1392
1393MODULE_DESCRIPTION("ASoC WM9081 driver");
1394MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1395MODULE_LICENSE("GPL");
This page took 0.214458 seconds and 5 git commands to generate.