ASoC: Drop unused state parameter from CODEC suspend callback
[linux-2.6.git] / sound / soc / codecs / wm8960.c
blob55b9a25cd1b382abc9e44c2010df21f3f56cd12b
1 /*
2 * wm8960.c -- WM8960 ALSA SoC Audio driver
4 * Author: Liam Girdwood
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/pm.h>
16 #include <linux/i2c.h>
17 #include <linux/slab.h>
18 #include <sound/core.h>
19 #include <sound/pcm.h>
20 #include <sound/pcm_params.h>
21 #include <sound/soc.h>
22 #include <sound/initval.h>
23 #include <sound/tlv.h>
24 #include <sound/wm8960.h>
26 #include "wm8960.h"
28 #define AUDIO_NAME "wm8960"
30 /* R25 - Power 1 */
31 #define WM8960_VMID_MASK 0x180
32 #define WM8960_VREF 0x40
34 /* R26 - Power 2 */
35 #define WM8960_PWR2_LOUT1 0x40
36 #define WM8960_PWR2_ROUT1 0x20
37 #define WM8960_PWR2_OUT3 0x02
39 /* R28 - Anti-pop 1 */
40 #define WM8960_POBCTRL 0x80
41 #define WM8960_BUFDCOPEN 0x10
42 #define WM8960_BUFIOEN 0x08
43 #define WM8960_SOFT_ST 0x04
44 #define WM8960_HPSTBY 0x01
46 /* R29 - Anti-pop 2 */
47 #define WM8960_DISOP 0x40
48 #define WM8960_DRES_MASK 0x30
51 * wm8960 register cache
52 * We can't read the WM8960 register space when we are
53 * using 2 wire for device control, so we cache them instead.
55 static const u16 wm8960_reg[WM8960_CACHEREGNUM] = {
56 0x0097, 0x0097, 0x0000, 0x0000,
57 0x0000, 0x0008, 0x0000, 0x000a,
58 0x01c0, 0x0000, 0x00ff, 0x00ff,
59 0x0000, 0x0000, 0x0000, 0x0000,
60 0x0000, 0x007b, 0x0100, 0x0032,
61 0x0000, 0x00c3, 0x00c3, 0x01c0,
62 0x0000, 0x0000, 0x0000, 0x0000,
63 0x0000, 0x0000, 0x0000, 0x0000,
64 0x0100, 0x0100, 0x0050, 0x0050,
65 0x0050, 0x0050, 0x0000, 0x0000,
66 0x0000, 0x0000, 0x0040, 0x0000,
67 0x0000, 0x0050, 0x0050, 0x0000,
68 0x0002, 0x0037, 0x004d, 0x0080,
69 0x0008, 0x0031, 0x0026, 0x00e9,
72 struct wm8960_priv {
73 enum snd_soc_control_type control_type;
74 int (*set_bias_level)(struct snd_soc_codec *,
75 enum snd_soc_bias_level level);
76 struct snd_soc_dapm_widget *lout1;
77 struct snd_soc_dapm_widget *rout1;
78 struct snd_soc_dapm_widget *out3;
79 bool deemph;
80 int playback_fs;
83 #define wm8960_reset(c) snd_soc_write(c, WM8960_RESET, 0)
85 /* enumerated controls */
86 static const char *wm8960_polarity[] = {"No Inversion", "Left Inverted",
87 "Right Inverted", "Stereo Inversion"};
88 static const char *wm8960_3d_upper_cutoff[] = {"High", "Low"};
89 static const char *wm8960_3d_lower_cutoff[] = {"Low", "High"};
90 static const char *wm8960_alcfunc[] = {"Off", "Right", "Left", "Stereo"};
91 static const char *wm8960_alcmode[] = {"ALC", "Limiter"};
93 static const struct soc_enum wm8960_enum[] = {
94 SOC_ENUM_SINGLE(WM8960_DACCTL1, 5, 4, wm8960_polarity),
95 SOC_ENUM_SINGLE(WM8960_DACCTL2, 5, 4, wm8960_polarity),
96 SOC_ENUM_SINGLE(WM8960_3D, 6, 2, wm8960_3d_upper_cutoff),
97 SOC_ENUM_SINGLE(WM8960_3D, 5, 2, wm8960_3d_lower_cutoff),
98 SOC_ENUM_SINGLE(WM8960_ALC1, 7, 4, wm8960_alcfunc),
99 SOC_ENUM_SINGLE(WM8960_ALC3, 8, 2, wm8960_alcmode),
102 static const int deemph_settings[] = { 0, 32000, 44100, 48000 };
104 static int wm8960_set_deemph(struct snd_soc_codec *codec)
106 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
107 int val, i, best;
109 /* If we're using deemphasis select the nearest available sample
110 * rate.
112 if (wm8960->deemph) {
113 best = 1;
114 for (i = 2; i < ARRAY_SIZE(deemph_settings); i++) {
115 if (abs(deemph_settings[i] - wm8960->playback_fs) <
116 abs(deemph_settings[best] - wm8960->playback_fs))
117 best = i;
120 val = best << 1;
121 } else {
122 val = 0;
125 dev_dbg(codec->dev, "Set deemphasis %d\n", val);
127 return snd_soc_update_bits(codec, WM8960_DACCTL1,
128 0x6, val);
131 static int wm8960_get_deemph(struct snd_kcontrol *kcontrol,
132 struct snd_ctl_elem_value *ucontrol)
134 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
135 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
137 ucontrol->value.enumerated.item[0] = wm8960->deemph;
138 return 0;
141 static int wm8960_put_deemph(struct snd_kcontrol *kcontrol,
142 struct snd_ctl_elem_value *ucontrol)
144 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
145 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
146 int deemph = ucontrol->value.enumerated.item[0];
148 if (deemph > 1)
149 return -EINVAL;
151 wm8960->deemph = deemph;
153 return wm8960_set_deemph(codec);
156 static const DECLARE_TLV_DB_SCALE(adc_tlv, -9700, 50, 0);
157 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 50, 1);
158 static const DECLARE_TLV_DB_SCALE(bypass_tlv, -2100, 300, 0);
159 static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
161 static const struct snd_kcontrol_new wm8960_snd_controls[] = {
162 SOC_DOUBLE_R_TLV("Capture Volume", WM8960_LINVOL, WM8960_RINVOL,
163 0, 63, 0, adc_tlv),
164 SOC_DOUBLE_R("Capture Volume ZC Switch", WM8960_LINVOL, WM8960_RINVOL,
165 6, 1, 0),
166 SOC_DOUBLE_R("Capture Switch", WM8960_LINVOL, WM8960_RINVOL,
167 7, 1, 0),
169 SOC_DOUBLE_R_TLV("Playback Volume", WM8960_LDAC, WM8960_RDAC,
170 0, 255, 0, dac_tlv),
172 SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8960_LOUT1, WM8960_ROUT1,
173 0, 127, 0, out_tlv),
174 SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8960_LOUT1, WM8960_ROUT1,
175 7, 1, 0),
177 SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8960_LOUT2, WM8960_ROUT2,
178 0, 127, 0, out_tlv),
179 SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8960_LOUT2, WM8960_ROUT2,
180 7, 1, 0),
181 SOC_SINGLE("Speaker DC Volume", WM8960_CLASSD3, 3, 5, 0),
182 SOC_SINGLE("Speaker AC Volume", WM8960_CLASSD3, 0, 5, 0),
184 SOC_SINGLE("PCM Playback -6dB Switch", WM8960_DACCTL1, 7, 1, 0),
185 SOC_ENUM("ADC Polarity", wm8960_enum[0]),
186 SOC_SINGLE("ADC High Pass Filter Switch", WM8960_DACCTL1, 0, 1, 0),
188 SOC_ENUM("DAC Polarity", wm8960_enum[2]),
189 SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
190 wm8960_get_deemph, wm8960_put_deemph),
192 SOC_ENUM("3D Filter Upper Cut-Off", wm8960_enum[2]),
193 SOC_ENUM("3D Filter Lower Cut-Off", wm8960_enum[3]),
194 SOC_SINGLE("3D Volume", WM8960_3D, 1, 15, 0),
195 SOC_SINGLE("3D Switch", WM8960_3D, 0, 1, 0),
197 SOC_ENUM("ALC Function", wm8960_enum[4]),
198 SOC_SINGLE("ALC Max Gain", WM8960_ALC1, 4, 7, 0),
199 SOC_SINGLE("ALC Target", WM8960_ALC1, 0, 15, 1),
200 SOC_SINGLE("ALC Min Gain", WM8960_ALC2, 4, 7, 0),
201 SOC_SINGLE("ALC Hold Time", WM8960_ALC2, 0, 15, 0),
202 SOC_ENUM("ALC Mode", wm8960_enum[5]),
203 SOC_SINGLE("ALC Decay", WM8960_ALC3, 4, 15, 0),
204 SOC_SINGLE("ALC Attack", WM8960_ALC3, 0, 15, 0),
206 SOC_SINGLE("Noise Gate Threshold", WM8960_NOISEG, 3, 31, 0),
207 SOC_SINGLE("Noise Gate Switch", WM8960_NOISEG, 0, 1, 0),
209 SOC_DOUBLE_R("ADC PCM Capture Volume", WM8960_LINPATH, WM8960_RINPATH,
210 0, 127, 0),
212 SOC_SINGLE_TLV("Left Output Mixer Boost Bypass Volume",
213 WM8960_BYPASS1, 4, 7, 1, bypass_tlv),
214 SOC_SINGLE_TLV("Left Output Mixer LINPUT3 Volume",
215 WM8960_LOUTMIX, 4, 7, 1, bypass_tlv),
216 SOC_SINGLE_TLV("Right Output Mixer Boost Bypass Volume",
217 WM8960_BYPASS2, 4, 7, 1, bypass_tlv),
218 SOC_SINGLE_TLV("Right Output Mixer RINPUT3 Volume",
219 WM8960_ROUTMIX, 4, 7, 1, bypass_tlv),
222 static const struct snd_kcontrol_new wm8960_lin_boost[] = {
223 SOC_DAPM_SINGLE("LINPUT2 Switch", WM8960_LINPATH, 6, 1, 0),
224 SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LINPATH, 7, 1, 0),
225 SOC_DAPM_SINGLE("LINPUT1 Switch", WM8960_LINPATH, 8, 1, 0),
228 static const struct snd_kcontrol_new wm8960_lin[] = {
229 SOC_DAPM_SINGLE("Boost Switch", WM8960_LINPATH, 3, 1, 0),
232 static const struct snd_kcontrol_new wm8960_rin_boost[] = {
233 SOC_DAPM_SINGLE("RINPUT2 Switch", WM8960_RINPATH, 6, 1, 0),
234 SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_RINPATH, 7, 1, 0),
235 SOC_DAPM_SINGLE("RINPUT1 Switch", WM8960_RINPATH, 8, 1, 0),
238 static const struct snd_kcontrol_new wm8960_rin[] = {
239 SOC_DAPM_SINGLE("Boost Switch", WM8960_RINPATH, 3, 1, 0),
242 static const struct snd_kcontrol_new wm8960_loutput_mixer[] = {
243 SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_LOUTMIX, 8, 1, 0),
244 SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LOUTMIX, 7, 1, 0),
245 SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS1, 7, 1, 0),
248 static const struct snd_kcontrol_new wm8960_routput_mixer[] = {
249 SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_ROUTMIX, 8, 1, 0),
250 SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_ROUTMIX, 7, 1, 0),
251 SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS2, 7, 1, 0),
254 static const struct snd_kcontrol_new wm8960_mono_out[] = {
255 SOC_DAPM_SINGLE("Left Switch", WM8960_MONOMIX1, 7, 1, 0),
256 SOC_DAPM_SINGLE("Right Switch", WM8960_MONOMIX2, 7, 1, 0),
259 static const struct snd_soc_dapm_widget wm8960_dapm_widgets[] = {
260 SND_SOC_DAPM_INPUT("LINPUT1"),
261 SND_SOC_DAPM_INPUT("RINPUT1"),
262 SND_SOC_DAPM_INPUT("LINPUT2"),
263 SND_SOC_DAPM_INPUT("RINPUT2"),
264 SND_SOC_DAPM_INPUT("LINPUT3"),
265 SND_SOC_DAPM_INPUT("RINPUT3"),
267 SND_SOC_DAPM_SUPPLY("MICB", WM8960_POWER1, 1, 0, NULL, 0),
269 SND_SOC_DAPM_MIXER("Left Boost Mixer", WM8960_POWER1, 5, 0,
270 wm8960_lin_boost, ARRAY_SIZE(wm8960_lin_boost)),
271 SND_SOC_DAPM_MIXER("Right Boost Mixer", WM8960_POWER1, 4, 0,
272 wm8960_rin_boost, ARRAY_SIZE(wm8960_rin_boost)),
274 SND_SOC_DAPM_MIXER("Left Input Mixer", WM8960_POWER3, 5, 0,
275 wm8960_lin, ARRAY_SIZE(wm8960_lin)),
276 SND_SOC_DAPM_MIXER("Right Input Mixer", WM8960_POWER3, 4, 0,
277 wm8960_rin, ARRAY_SIZE(wm8960_rin)),
279 SND_SOC_DAPM_ADC("Left ADC", "Capture", WM8960_POWER2, 3, 0),
280 SND_SOC_DAPM_ADC("Right ADC", "Capture", WM8960_POWER2, 2, 0),
282 SND_SOC_DAPM_DAC("Left DAC", "Playback", WM8960_POWER2, 8, 0),
283 SND_SOC_DAPM_DAC("Right DAC", "Playback", WM8960_POWER2, 7, 0),
285 SND_SOC_DAPM_MIXER("Left Output Mixer", WM8960_POWER3, 3, 0,
286 &wm8960_loutput_mixer[0],
287 ARRAY_SIZE(wm8960_loutput_mixer)),
288 SND_SOC_DAPM_MIXER("Right Output Mixer", WM8960_POWER3, 2, 0,
289 &wm8960_routput_mixer[0],
290 ARRAY_SIZE(wm8960_routput_mixer)),
292 SND_SOC_DAPM_PGA("LOUT1 PGA", WM8960_POWER2, 6, 0, NULL, 0),
293 SND_SOC_DAPM_PGA("ROUT1 PGA", WM8960_POWER2, 5, 0, NULL, 0),
295 SND_SOC_DAPM_PGA("Left Speaker PGA", WM8960_POWER2, 4, 0, NULL, 0),
296 SND_SOC_DAPM_PGA("Right Speaker PGA", WM8960_POWER2, 3, 0, NULL, 0),
298 SND_SOC_DAPM_PGA("Right Speaker Output", WM8960_CLASSD1, 7, 0, NULL, 0),
299 SND_SOC_DAPM_PGA("Left Speaker Output", WM8960_CLASSD1, 6, 0, NULL, 0),
301 SND_SOC_DAPM_OUTPUT("SPK_LP"),
302 SND_SOC_DAPM_OUTPUT("SPK_LN"),
303 SND_SOC_DAPM_OUTPUT("HP_L"),
304 SND_SOC_DAPM_OUTPUT("HP_R"),
305 SND_SOC_DAPM_OUTPUT("SPK_RP"),
306 SND_SOC_DAPM_OUTPUT("SPK_RN"),
307 SND_SOC_DAPM_OUTPUT("OUT3"),
310 static const struct snd_soc_dapm_widget wm8960_dapm_widgets_out3[] = {
311 SND_SOC_DAPM_MIXER("Mono Output Mixer", WM8960_POWER2, 1, 0,
312 &wm8960_mono_out[0],
313 ARRAY_SIZE(wm8960_mono_out)),
316 /* Represent OUT3 as a PGA so that it gets turned on with LOUT1/ROUT1 */
317 static const struct snd_soc_dapm_widget wm8960_dapm_widgets_capless[] = {
318 SND_SOC_DAPM_PGA("OUT3 VMID", WM8960_POWER2, 1, 0, NULL, 0),
321 static const struct snd_soc_dapm_route audio_paths[] = {
322 { "Left Boost Mixer", "LINPUT1 Switch", "LINPUT1" },
323 { "Left Boost Mixer", "LINPUT2 Switch", "LINPUT2" },
324 { "Left Boost Mixer", "LINPUT3 Switch", "LINPUT3" },
326 { "Left Input Mixer", "Boost Switch", "Left Boost Mixer", },
327 { "Left Input Mixer", NULL, "LINPUT1", }, /* Really Boost Switch */
328 { "Left Input Mixer", NULL, "LINPUT2" },
329 { "Left Input Mixer", NULL, "LINPUT3" },
331 { "Right Boost Mixer", "RINPUT1 Switch", "RINPUT1" },
332 { "Right Boost Mixer", "RINPUT2 Switch", "RINPUT2" },
333 { "Right Boost Mixer", "RINPUT3 Switch", "RINPUT3" },
335 { "Right Input Mixer", "Boost Switch", "Right Boost Mixer", },
336 { "Right Input Mixer", NULL, "RINPUT1", }, /* Really Boost Switch */
337 { "Right Input Mixer", NULL, "RINPUT2" },
338 { "Right Input Mixer", NULL, "LINPUT3" },
340 { "Left ADC", NULL, "Left Input Mixer" },
341 { "Right ADC", NULL, "Right Input Mixer" },
343 { "Left Output Mixer", "LINPUT3 Switch", "LINPUT3" },
344 { "Left Output Mixer", "Boost Bypass Switch", "Left Boost Mixer"} ,
345 { "Left Output Mixer", "PCM Playback Switch", "Left DAC" },
347 { "Right Output Mixer", "RINPUT3 Switch", "RINPUT3" },
348 { "Right Output Mixer", "Boost Bypass Switch", "Right Boost Mixer" } ,
349 { "Right Output Mixer", "PCM Playback Switch", "Right DAC" },
351 { "LOUT1 PGA", NULL, "Left Output Mixer" },
352 { "ROUT1 PGA", NULL, "Right Output Mixer" },
354 { "HP_L", NULL, "LOUT1 PGA" },
355 { "HP_R", NULL, "ROUT1 PGA" },
357 { "Left Speaker PGA", NULL, "Left Output Mixer" },
358 { "Right Speaker PGA", NULL, "Right Output Mixer" },
360 { "Left Speaker Output", NULL, "Left Speaker PGA" },
361 { "Right Speaker Output", NULL, "Right Speaker PGA" },
363 { "SPK_LN", NULL, "Left Speaker Output" },
364 { "SPK_LP", NULL, "Left Speaker Output" },
365 { "SPK_RN", NULL, "Right Speaker Output" },
366 { "SPK_RP", NULL, "Right Speaker Output" },
369 static const struct snd_soc_dapm_route audio_paths_out3[] = {
370 { "Mono Output Mixer", "Left Switch", "Left Output Mixer" },
371 { "Mono Output Mixer", "Right Switch", "Right Output Mixer" },
373 { "OUT3", NULL, "Mono Output Mixer", }
376 static const struct snd_soc_dapm_route audio_paths_capless[] = {
377 { "HP_L", NULL, "OUT3 VMID" },
378 { "HP_R", NULL, "OUT3 VMID" },
380 { "OUT3 VMID", NULL, "Left Output Mixer" },
381 { "OUT3 VMID", NULL, "Right Output Mixer" },
384 static int wm8960_add_widgets(struct snd_soc_codec *codec)
386 struct wm8960_data *pdata = codec->dev->platform_data;
387 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
388 struct snd_soc_dapm_context *dapm = &codec->dapm;
389 struct snd_soc_dapm_widget *w;
391 snd_soc_dapm_new_controls(dapm, wm8960_dapm_widgets,
392 ARRAY_SIZE(wm8960_dapm_widgets));
394 snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths));
396 /* In capless mode OUT3 is used to provide VMID for the
397 * headphone outputs, otherwise it is used as a mono mixer.
399 if (pdata && pdata->capless) {
400 snd_soc_dapm_new_controls(dapm, wm8960_dapm_widgets_capless,
401 ARRAY_SIZE(wm8960_dapm_widgets_capless));
403 snd_soc_dapm_add_routes(dapm, audio_paths_capless,
404 ARRAY_SIZE(audio_paths_capless));
405 } else {
406 snd_soc_dapm_new_controls(dapm, wm8960_dapm_widgets_out3,
407 ARRAY_SIZE(wm8960_dapm_widgets_out3));
409 snd_soc_dapm_add_routes(dapm, audio_paths_out3,
410 ARRAY_SIZE(audio_paths_out3));
413 /* We need to power up the headphone output stage out of
414 * sequence for capless mode. To save scanning the widget
415 * list each time to find the desired power state do so now
416 * and save the result.
418 list_for_each_entry(w, &codec->card->widgets, list) {
419 if (w->dapm != &codec->dapm)
420 continue;
421 if (strcmp(w->name, "LOUT1 PGA") == 0)
422 wm8960->lout1 = w;
423 if (strcmp(w->name, "ROUT1 PGA") == 0)
424 wm8960->rout1 = w;
425 if (strcmp(w->name, "OUT3 VMID") == 0)
426 wm8960->out3 = w;
429 return 0;
432 static int wm8960_set_dai_fmt(struct snd_soc_dai *codec_dai,
433 unsigned int fmt)
435 struct snd_soc_codec *codec = codec_dai->codec;
436 u16 iface = 0;
438 /* set master/slave audio interface */
439 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
440 case SND_SOC_DAIFMT_CBM_CFM:
441 iface |= 0x0040;
442 break;
443 case SND_SOC_DAIFMT_CBS_CFS:
444 break;
445 default:
446 return -EINVAL;
449 /* interface format */
450 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
451 case SND_SOC_DAIFMT_I2S:
452 iface |= 0x0002;
453 break;
454 case SND_SOC_DAIFMT_RIGHT_J:
455 break;
456 case SND_SOC_DAIFMT_LEFT_J:
457 iface |= 0x0001;
458 break;
459 case SND_SOC_DAIFMT_DSP_A:
460 iface |= 0x0003;
461 break;
462 case SND_SOC_DAIFMT_DSP_B:
463 iface |= 0x0013;
464 break;
465 default:
466 return -EINVAL;
469 /* clock inversion */
470 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
471 case SND_SOC_DAIFMT_NB_NF:
472 break;
473 case SND_SOC_DAIFMT_IB_IF:
474 iface |= 0x0090;
475 break;
476 case SND_SOC_DAIFMT_IB_NF:
477 iface |= 0x0080;
478 break;
479 case SND_SOC_DAIFMT_NB_IF:
480 iface |= 0x0010;
481 break;
482 default:
483 return -EINVAL;
486 /* set iface */
487 snd_soc_write(codec, WM8960_IFACE1, iface);
488 return 0;
491 static struct {
492 int rate;
493 unsigned int val;
494 } alc_rates[] = {
495 { 48000, 0 },
496 { 44100, 0 },
497 { 32000, 1 },
498 { 22050, 2 },
499 { 24000, 2 },
500 { 16000, 3 },
501 { 11250, 4 },
502 { 12000, 4 },
503 { 8000, 5 },
506 static int wm8960_hw_params(struct snd_pcm_substream *substream,
507 struct snd_pcm_hw_params *params,
508 struct snd_soc_dai *dai)
510 struct snd_soc_pcm_runtime *rtd = substream->private_data;
511 struct snd_soc_codec *codec = rtd->codec;
512 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
513 u16 iface = snd_soc_read(codec, WM8960_IFACE1) & 0xfff3;
514 int i;
516 /* bit size */
517 switch (params_format(params)) {
518 case SNDRV_PCM_FORMAT_S16_LE:
519 break;
520 case SNDRV_PCM_FORMAT_S20_3LE:
521 iface |= 0x0004;
522 break;
523 case SNDRV_PCM_FORMAT_S24_LE:
524 iface |= 0x0008;
525 break;
528 /* Update filters for the new rate */
529 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
530 wm8960->playback_fs = params_rate(params);
531 wm8960_set_deemph(codec);
532 } else {
533 for (i = 0; i < ARRAY_SIZE(alc_rates); i++)
534 if (alc_rates[i].rate == params_rate(params))
535 snd_soc_update_bits(codec,
536 WM8960_ADDCTL3, 0x7,
537 alc_rates[i].val);
540 /* set iface */
541 snd_soc_write(codec, WM8960_IFACE1, iface);
542 return 0;
545 static int wm8960_mute(struct snd_soc_dai *dai, int mute)
547 struct snd_soc_codec *codec = dai->codec;
548 u16 mute_reg = snd_soc_read(codec, WM8960_DACCTL1) & 0xfff7;
550 if (mute)
551 snd_soc_write(codec, WM8960_DACCTL1, mute_reg | 0x8);
552 else
553 snd_soc_write(codec, WM8960_DACCTL1, mute_reg);
554 return 0;
557 static int wm8960_set_bias_level_out3(struct snd_soc_codec *codec,
558 enum snd_soc_bias_level level)
560 u16 reg;
562 switch (level) {
563 case SND_SOC_BIAS_ON:
564 break;
566 case SND_SOC_BIAS_PREPARE:
567 /* Set VMID to 2x50k */
568 reg = snd_soc_read(codec, WM8960_POWER1);
569 reg &= ~0x180;
570 reg |= 0x80;
571 snd_soc_write(codec, WM8960_POWER1, reg);
572 break;
574 case SND_SOC_BIAS_STANDBY:
575 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
576 snd_soc_cache_sync(codec);
578 /* Enable anti-pop features */
579 snd_soc_write(codec, WM8960_APOP1,
580 WM8960_POBCTRL | WM8960_SOFT_ST |
581 WM8960_BUFDCOPEN | WM8960_BUFIOEN);
583 /* Enable & ramp VMID at 2x50k */
584 reg = snd_soc_read(codec, WM8960_POWER1);
585 reg |= 0x80;
586 snd_soc_write(codec, WM8960_POWER1, reg);
587 msleep(100);
589 /* Enable VREF */
590 snd_soc_write(codec, WM8960_POWER1, reg | WM8960_VREF);
592 /* Disable anti-pop features */
593 snd_soc_write(codec, WM8960_APOP1, WM8960_BUFIOEN);
596 /* Set VMID to 2x250k */
597 reg = snd_soc_read(codec, WM8960_POWER1);
598 reg &= ~0x180;
599 reg |= 0x100;
600 snd_soc_write(codec, WM8960_POWER1, reg);
601 break;
603 case SND_SOC_BIAS_OFF:
604 /* Enable anti-pop features */
605 snd_soc_write(codec, WM8960_APOP1,
606 WM8960_POBCTRL | WM8960_SOFT_ST |
607 WM8960_BUFDCOPEN | WM8960_BUFIOEN);
609 /* Disable VMID and VREF, let them discharge */
610 snd_soc_write(codec, WM8960_POWER1, 0);
611 msleep(600);
612 break;
615 codec->dapm.bias_level = level;
617 return 0;
620 static int wm8960_set_bias_level_capless(struct snd_soc_codec *codec,
621 enum snd_soc_bias_level level)
623 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
624 int reg;
626 switch (level) {
627 case SND_SOC_BIAS_ON:
628 break;
630 case SND_SOC_BIAS_PREPARE:
631 switch (codec->dapm.bias_level) {
632 case SND_SOC_BIAS_STANDBY:
633 /* Enable anti pop mode */
634 snd_soc_update_bits(codec, WM8960_APOP1,
635 WM8960_POBCTRL | WM8960_SOFT_ST |
636 WM8960_BUFDCOPEN,
637 WM8960_POBCTRL | WM8960_SOFT_ST |
638 WM8960_BUFDCOPEN);
640 /* Enable LOUT1, ROUT1 and OUT3 if they're enabled */
641 reg = 0;
642 if (wm8960->lout1 && wm8960->lout1->power)
643 reg |= WM8960_PWR2_LOUT1;
644 if (wm8960->rout1 && wm8960->rout1->power)
645 reg |= WM8960_PWR2_ROUT1;
646 if (wm8960->out3 && wm8960->out3->power)
647 reg |= WM8960_PWR2_OUT3;
648 snd_soc_update_bits(codec, WM8960_POWER2,
649 WM8960_PWR2_LOUT1 |
650 WM8960_PWR2_ROUT1 |
651 WM8960_PWR2_OUT3, reg);
653 /* Enable VMID at 2*50k */
654 snd_soc_update_bits(codec, WM8960_POWER1,
655 WM8960_VMID_MASK, 0x80);
657 /* Ramp */
658 msleep(100);
660 /* Enable VREF */
661 snd_soc_update_bits(codec, WM8960_POWER1,
662 WM8960_VREF, WM8960_VREF);
664 msleep(100);
665 break;
667 case SND_SOC_BIAS_ON:
668 /* Enable anti-pop mode */
669 snd_soc_update_bits(codec, WM8960_APOP1,
670 WM8960_POBCTRL | WM8960_SOFT_ST |
671 WM8960_BUFDCOPEN,
672 WM8960_POBCTRL | WM8960_SOFT_ST |
673 WM8960_BUFDCOPEN);
675 /* Disable VMID and VREF */
676 snd_soc_update_bits(codec, WM8960_POWER1,
677 WM8960_VREF | WM8960_VMID_MASK, 0);
678 break;
680 case SND_SOC_BIAS_OFF:
681 snd_soc_cache_sync(codec);
682 break;
683 default:
684 break;
686 break;
688 case SND_SOC_BIAS_STANDBY:
689 switch (codec->dapm.bias_level) {
690 case SND_SOC_BIAS_PREPARE:
691 /* Disable HP discharge */
692 snd_soc_update_bits(codec, WM8960_APOP2,
693 WM8960_DISOP | WM8960_DRES_MASK,
696 /* Disable anti-pop features */
697 snd_soc_update_bits(codec, WM8960_APOP1,
698 WM8960_POBCTRL | WM8960_SOFT_ST |
699 WM8960_BUFDCOPEN,
700 WM8960_POBCTRL | WM8960_SOFT_ST |
701 WM8960_BUFDCOPEN);
702 break;
704 default:
705 break;
707 break;
709 case SND_SOC_BIAS_OFF:
710 break;
713 codec->dapm.bias_level = level;
715 return 0;
718 /* PLL divisors */
719 struct _pll_div {
720 u32 pre_div:1;
721 u32 n:4;
722 u32 k:24;
725 /* The size in bits of the pll divide multiplied by 10
726 * to allow rounding later */
727 #define FIXED_PLL_SIZE ((1 << 24) * 10)
729 static int pll_factors(unsigned int source, unsigned int target,
730 struct _pll_div *pll_div)
732 unsigned long long Kpart;
733 unsigned int K, Ndiv, Nmod;
735 pr_debug("WM8960 PLL: setting %dHz->%dHz\n", source, target);
737 /* Scale up target to PLL operating frequency */
738 target *= 4;
740 Ndiv = target / source;
741 if (Ndiv < 6) {
742 source >>= 1;
743 pll_div->pre_div = 1;
744 Ndiv = target / source;
745 } else
746 pll_div->pre_div = 0;
748 if ((Ndiv < 6) || (Ndiv > 12)) {
749 pr_err("WM8960 PLL: Unsupported N=%d\n", Ndiv);
750 return -EINVAL;
753 pll_div->n = Ndiv;
754 Nmod = target % source;
755 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
757 do_div(Kpart, source);
759 K = Kpart & 0xFFFFFFFF;
761 /* Check if we need to round */
762 if ((K % 10) >= 5)
763 K += 5;
765 /* Move down to proper range now rounding is done */
766 K /= 10;
768 pll_div->k = K;
770 pr_debug("WM8960 PLL: N=%x K=%x pre_div=%d\n",
771 pll_div->n, pll_div->k, pll_div->pre_div);
773 return 0;
776 static int wm8960_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
777 int source, unsigned int freq_in, unsigned int freq_out)
779 struct snd_soc_codec *codec = codec_dai->codec;
780 u16 reg;
781 static struct _pll_div pll_div;
782 int ret;
784 if (freq_in && freq_out) {
785 ret = pll_factors(freq_in, freq_out, &pll_div);
786 if (ret != 0)
787 return ret;
790 /* Disable the PLL: even if we are changing the frequency the
791 * PLL needs to be disabled while we do so. */
792 snd_soc_write(codec, WM8960_CLOCK1,
793 snd_soc_read(codec, WM8960_CLOCK1) & ~1);
794 snd_soc_write(codec, WM8960_POWER2,
795 snd_soc_read(codec, WM8960_POWER2) & ~1);
797 if (!freq_in || !freq_out)
798 return 0;
800 reg = snd_soc_read(codec, WM8960_PLL1) & ~0x3f;
801 reg |= pll_div.pre_div << 4;
802 reg |= pll_div.n;
804 if (pll_div.k) {
805 reg |= 0x20;
807 snd_soc_write(codec, WM8960_PLL2, (pll_div.k >> 18) & 0x3f);
808 snd_soc_write(codec, WM8960_PLL3, (pll_div.k >> 9) & 0x1ff);
809 snd_soc_write(codec, WM8960_PLL4, pll_div.k & 0x1ff);
811 snd_soc_write(codec, WM8960_PLL1, reg);
813 /* Turn it on */
814 snd_soc_write(codec, WM8960_POWER2,
815 snd_soc_read(codec, WM8960_POWER2) | 1);
816 msleep(250);
817 snd_soc_write(codec, WM8960_CLOCK1,
818 snd_soc_read(codec, WM8960_CLOCK1) | 1);
820 return 0;
823 static int wm8960_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
824 int div_id, int div)
826 struct snd_soc_codec *codec = codec_dai->codec;
827 u16 reg;
829 switch (div_id) {
830 case WM8960_SYSCLKDIV:
831 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1f9;
832 snd_soc_write(codec, WM8960_CLOCK1, reg | div);
833 break;
834 case WM8960_DACDIV:
835 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1c7;
836 snd_soc_write(codec, WM8960_CLOCK1, reg | div);
837 break;
838 case WM8960_OPCLKDIV:
839 reg = snd_soc_read(codec, WM8960_PLL1) & 0x03f;
840 snd_soc_write(codec, WM8960_PLL1, reg | div);
841 break;
842 case WM8960_DCLKDIV:
843 reg = snd_soc_read(codec, WM8960_CLOCK2) & 0x03f;
844 snd_soc_write(codec, WM8960_CLOCK2, reg | div);
845 break;
846 case WM8960_TOCLKSEL:
847 reg = snd_soc_read(codec, WM8960_ADDCTL1) & 0x1fd;
848 snd_soc_write(codec, WM8960_ADDCTL1, reg | div);
849 break;
850 default:
851 return -EINVAL;
854 return 0;
857 static int wm8960_set_bias_level(struct snd_soc_codec *codec,
858 enum snd_soc_bias_level level)
860 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
862 return wm8960->set_bias_level(codec, level);
865 #define WM8960_RATES SNDRV_PCM_RATE_8000_48000
867 #define WM8960_FORMATS \
868 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
869 SNDRV_PCM_FMTBIT_S24_LE)
871 static const struct snd_soc_dai_ops wm8960_dai_ops = {
872 .hw_params = wm8960_hw_params,
873 .digital_mute = wm8960_mute,
874 .set_fmt = wm8960_set_dai_fmt,
875 .set_clkdiv = wm8960_set_dai_clkdiv,
876 .set_pll = wm8960_set_dai_pll,
879 static struct snd_soc_dai_driver wm8960_dai = {
880 .name = "wm8960-hifi",
881 .playback = {
882 .stream_name = "Playback",
883 .channels_min = 1,
884 .channels_max = 2,
885 .rates = WM8960_RATES,
886 .formats = WM8960_FORMATS,},
887 .capture = {
888 .stream_name = "Capture",
889 .channels_min = 1,
890 .channels_max = 2,
891 .rates = WM8960_RATES,
892 .formats = WM8960_FORMATS,},
893 .ops = &wm8960_dai_ops,
894 .symmetric_rates = 1,
897 static int wm8960_suspend(struct snd_soc_codec *codec)
899 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
901 wm8960->set_bias_level(codec, SND_SOC_BIAS_OFF);
902 return 0;
905 static int wm8960_resume(struct snd_soc_codec *codec)
907 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
909 wm8960->set_bias_level(codec, SND_SOC_BIAS_STANDBY);
910 return 0;
913 static int wm8960_probe(struct snd_soc_codec *codec)
915 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
916 struct wm8960_data *pdata = dev_get_platdata(codec->dev);
917 int ret;
918 u16 reg;
920 wm8960->set_bias_level = wm8960_set_bias_level_out3;
922 if (!pdata) {
923 dev_warn(codec->dev, "No platform data supplied\n");
924 } else {
925 if (pdata->dres > WM8960_DRES_MAX) {
926 dev_err(codec->dev, "Invalid DRES: %d\n", pdata->dres);
927 pdata->dres = 0;
930 if (pdata->capless)
931 wm8960->set_bias_level = wm8960_set_bias_level_capless;
934 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8960->control_type);
935 if (ret < 0) {
936 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
937 return ret;
940 ret = wm8960_reset(codec);
941 if (ret < 0) {
942 dev_err(codec->dev, "Failed to issue reset\n");
943 return ret;
946 wm8960->set_bias_level(codec, SND_SOC_BIAS_STANDBY);
948 /* Latch the update bits */
949 reg = snd_soc_read(codec, WM8960_LINVOL);
950 snd_soc_write(codec, WM8960_LINVOL, reg | 0x100);
951 reg = snd_soc_read(codec, WM8960_RINVOL);
952 snd_soc_write(codec, WM8960_RINVOL, reg | 0x100);
953 reg = snd_soc_read(codec, WM8960_LADC);
954 snd_soc_write(codec, WM8960_LADC, reg | 0x100);
955 reg = snd_soc_read(codec, WM8960_RADC);
956 snd_soc_write(codec, WM8960_RADC, reg | 0x100);
957 reg = snd_soc_read(codec, WM8960_LDAC);
958 snd_soc_write(codec, WM8960_LDAC, reg | 0x100);
959 reg = snd_soc_read(codec, WM8960_RDAC);
960 snd_soc_write(codec, WM8960_RDAC, reg | 0x100);
961 reg = snd_soc_read(codec, WM8960_LOUT1);
962 snd_soc_write(codec, WM8960_LOUT1, reg | 0x100);
963 reg = snd_soc_read(codec, WM8960_ROUT1);
964 snd_soc_write(codec, WM8960_ROUT1, reg | 0x100);
965 reg = snd_soc_read(codec, WM8960_LOUT2);
966 snd_soc_write(codec, WM8960_LOUT2, reg | 0x100);
967 reg = snd_soc_read(codec, WM8960_ROUT2);
968 snd_soc_write(codec, WM8960_ROUT2, reg | 0x100);
970 snd_soc_add_controls(codec, wm8960_snd_controls,
971 ARRAY_SIZE(wm8960_snd_controls));
972 wm8960_add_widgets(codec);
974 return 0;
977 /* power down chip */
978 static int wm8960_remove(struct snd_soc_codec *codec)
980 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
982 wm8960->set_bias_level(codec, SND_SOC_BIAS_OFF);
983 return 0;
986 static struct snd_soc_codec_driver soc_codec_dev_wm8960 = {
987 .probe = wm8960_probe,
988 .remove = wm8960_remove,
989 .suspend = wm8960_suspend,
990 .resume = wm8960_resume,
991 .set_bias_level = wm8960_set_bias_level,
992 .reg_cache_size = ARRAY_SIZE(wm8960_reg),
993 .reg_word_size = sizeof(u16),
994 .reg_cache_default = wm8960_reg,
997 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
998 static __devinit int wm8960_i2c_probe(struct i2c_client *i2c,
999 const struct i2c_device_id *id)
1001 struct wm8960_priv *wm8960;
1002 int ret;
1004 wm8960 = kzalloc(sizeof(struct wm8960_priv), GFP_KERNEL);
1005 if (wm8960 == NULL)
1006 return -ENOMEM;
1008 i2c_set_clientdata(i2c, wm8960);
1009 wm8960->control_type = SND_SOC_I2C;
1011 ret = snd_soc_register_codec(&i2c->dev,
1012 &soc_codec_dev_wm8960, &wm8960_dai, 1);
1013 if (ret < 0)
1014 kfree(wm8960);
1015 return ret;
1018 static __devexit int wm8960_i2c_remove(struct i2c_client *client)
1020 snd_soc_unregister_codec(&client->dev);
1021 kfree(i2c_get_clientdata(client));
1022 return 0;
1025 static const struct i2c_device_id wm8960_i2c_id[] = {
1026 { "wm8960", 0 },
1029 MODULE_DEVICE_TABLE(i2c, wm8960_i2c_id);
1031 static struct i2c_driver wm8960_i2c_driver = {
1032 .driver = {
1033 .name = "wm8960-codec",
1034 .owner = THIS_MODULE,
1036 .probe = wm8960_i2c_probe,
1037 .remove = __devexit_p(wm8960_i2c_remove),
1038 .id_table = wm8960_i2c_id,
1040 #endif
1042 static int __init wm8960_modinit(void)
1044 int ret = 0;
1045 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1046 ret = i2c_add_driver(&wm8960_i2c_driver);
1047 if (ret != 0) {
1048 printk(KERN_ERR "Failed to register WM8960 I2C driver: %d\n",
1049 ret);
1051 #endif
1052 return ret;
1054 module_init(wm8960_modinit);
1056 static void __exit wm8960_exit(void)
1058 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1059 i2c_del_driver(&wm8960_i2c_driver);
1060 #endif
1062 module_exit(wm8960_exit);
1064 MODULE_DESCRIPTION("ASoC WM8960 driver");
1065 MODULE_AUTHOR("Liam Girdwood");
1066 MODULE_LICENSE("GPL");