Revert "microblaze_mmu_v2: Update signal returning address"
[linux/fpc-iii.git] / sound / soc / codecs / wm8960.c
blob96518ac8e24ce9660f6fde240cc951570af6a61c
1 /*
2 * wm8960.c -- WM8960 ALSA SoC Audio driver
4 * Copyright 2007-11 Wolfson Microelectronics, plc
6 * Author: Liam Girdwood
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.
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/pm.h>
18 #include <linux/i2c.h>
19 #include <linux/slab.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc.h>
24 #include <sound/initval.h>
25 #include <sound/tlv.h>
26 #include <sound/wm8960.h>
28 #include "wm8960.h"
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_codec *codec = dai->codec;
511 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
512 u16 iface = snd_soc_read(codec, WM8960_IFACE1) & 0xfff3;
513 int i;
515 /* bit size */
516 switch (params_format(params)) {
517 case SNDRV_PCM_FORMAT_S16_LE:
518 break;
519 case SNDRV_PCM_FORMAT_S20_3LE:
520 iface |= 0x0004;
521 break;
522 case SNDRV_PCM_FORMAT_S24_LE:
523 iface |= 0x0008;
524 break;
527 /* Update filters for the new rate */
528 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
529 wm8960->playback_fs = params_rate(params);
530 wm8960_set_deemph(codec);
531 } else {
532 for (i = 0; i < ARRAY_SIZE(alc_rates); i++)
533 if (alc_rates[i].rate == params_rate(params))
534 snd_soc_update_bits(codec,
535 WM8960_ADDCTL3, 0x7,
536 alc_rates[i].val);
539 /* set iface */
540 snd_soc_write(codec, WM8960_IFACE1, iface);
541 return 0;
544 static int wm8960_mute(struct snd_soc_dai *dai, int mute)
546 struct snd_soc_codec *codec = dai->codec;
548 if (mute)
549 snd_soc_update_bits(codec, WM8960_DACCTL1, 0x8, 0x8);
550 else
551 snd_soc_update_bits(codec, WM8960_DACCTL1, 0x8, 0);
552 return 0;
555 static int wm8960_set_bias_level_out3(struct snd_soc_codec *codec,
556 enum snd_soc_bias_level level)
558 switch (level) {
559 case SND_SOC_BIAS_ON:
560 break;
562 case SND_SOC_BIAS_PREPARE:
563 /* Set VMID to 2x50k */
564 snd_soc_update_bits(codec, WM8960_POWER1, 0x180, 0x80);
565 break;
567 case SND_SOC_BIAS_STANDBY:
568 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
569 snd_soc_cache_sync(codec);
571 /* Enable anti-pop features */
572 snd_soc_write(codec, WM8960_APOP1,
573 WM8960_POBCTRL | WM8960_SOFT_ST |
574 WM8960_BUFDCOPEN | WM8960_BUFIOEN);
576 /* Enable & ramp VMID at 2x50k */
577 snd_soc_update_bits(codec, WM8960_POWER1, 0x80, 0x80);
578 msleep(100);
580 /* Enable VREF */
581 snd_soc_update_bits(codec, WM8960_POWER1, WM8960_VREF,
582 WM8960_VREF);
584 /* Disable anti-pop features */
585 snd_soc_write(codec, WM8960_APOP1, WM8960_BUFIOEN);
588 /* Set VMID to 2x250k */
589 snd_soc_update_bits(codec, WM8960_POWER1, 0x180, 0x100);
590 break;
592 case SND_SOC_BIAS_OFF:
593 /* Enable anti-pop features */
594 snd_soc_write(codec, WM8960_APOP1,
595 WM8960_POBCTRL | WM8960_SOFT_ST |
596 WM8960_BUFDCOPEN | WM8960_BUFIOEN);
598 /* Disable VMID and VREF, let them discharge */
599 snd_soc_write(codec, WM8960_POWER1, 0);
600 msleep(600);
601 break;
604 codec->dapm.bias_level = level;
606 return 0;
609 static int wm8960_set_bias_level_capless(struct snd_soc_codec *codec,
610 enum snd_soc_bias_level level)
612 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
613 int reg;
615 switch (level) {
616 case SND_SOC_BIAS_ON:
617 break;
619 case SND_SOC_BIAS_PREPARE:
620 switch (codec->dapm.bias_level) {
621 case SND_SOC_BIAS_STANDBY:
622 /* Enable anti pop mode */
623 snd_soc_update_bits(codec, WM8960_APOP1,
624 WM8960_POBCTRL | WM8960_SOFT_ST |
625 WM8960_BUFDCOPEN,
626 WM8960_POBCTRL | WM8960_SOFT_ST |
627 WM8960_BUFDCOPEN);
629 /* Enable LOUT1, ROUT1 and OUT3 if they're enabled */
630 reg = 0;
631 if (wm8960->lout1 && wm8960->lout1->power)
632 reg |= WM8960_PWR2_LOUT1;
633 if (wm8960->rout1 && wm8960->rout1->power)
634 reg |= WM8960_PWR2_ROUT1;
635 if (wm8960->out3 && wm8960->out3->power)
636 reg |= WM8960_PWR2_OUT3;
637 snd_soc_update_bits(codec, WM8960_POWER2,
638 WM8960_PWR2_LOUT1 |
639 WM8960_PWR2_ROUT1 |
640 WM8960_PWR2_OUT3, reg);
642 /* Enable VMID at 2*50k */
643 snd_soc_update_bits(codec, WM8960_POWER1,
644 WM8960_VMID_MASK, 0x80);
646 /* Ramp */
647 msleep(100);
649 /* Enable VREF */
650 snd_soc_update_bits(codec, WM8960_POWER1,
651 WM8960_VREF, WM8960_VREF);
653 msleep(100);
654 break;
656 case SND_SOC_BIAS_ON:
657 /* Enable anti-pop mode */
658 snd_soc_update_bits(codec, WM8960_APOP1,
659 WM8960_POBCTRL | WM8960_SOFT_ST |
660 WM8960_BUFDCOPEN,
661 WM8960_POBCTRL | WM8960_SOFT_ST |
662 WM8960_BUFDCOPEN);
664 /* Disable VMID and VREF */
665 snd_soc_update_bits(codec, WM8960_POWER1,
666 WM8960_VREF | WM8960_VMID_MASK, 0);
667 break;
669 case SND_SOC_BIAS_OFF:
670 snd_soc_cache_sync(codec);
671 break;
672 default:
673 break;
675 break;
677 case SND_SOC_BIAS_STANDBY:
678 switch (codec->dapm.bias_level) {
679 case SND_SOC_BIAS_PREPARE:
680 /* Disable HP discharge */
681 snd_soc_update_bits(codec, WM8960_APOP2,
682 WM8960_DISOP | WM8960_DRES_MASK,
685 /* Disable anti-pop features */
686 snd_soc_update_bits(codec, WM8960_APOP1,
687 WM8960_POBCTRL | WM8960_SOFT_ST |
688 WM8960_BUFDCOPEN,
689 WM8960_POBCTRL | WM8960_SOFT_ST |
690 WM8960_BUFDCOPEN);
691 break;
693 default:
694 break;
696 break;
698 case SND_SOC_BIAS_OFF:
699 break;
702 codec->dapm.bias_level = level;
704 return 0;
707 /* PLL divisors */
708 struct _pll_div {
709 u32 pre_div:1;
710 u32 n:4;
711 u32 k:24;
714 /* The size in bits of the pll divide multiplied by 10
715 * to allow rounding later */
716 #define FIXED_PLL_SIZE ((1 << 24) * 10)
718 static int pll_factors(unsigned int source, unsigned int target,
719 struct _pll_div *pll_div)
721 unsigned long long Kpart;
722 unsigned int K, Ndiv, Nmod;
724 pr_debug("WM8960 PLL: setting %dHz->%dHz\n", source, target);
726 /* Scale up target to PLL operating frequency */
727 target *= 4;
729 Ndiv = target / source;
730 if (Ndiv < 6) {
731 source >>= 1;
732 pll_div->pre_div = 1;
733 Ndiv = target / source;
734 } else
735 pll_div->pre_div = 0;
737 if ((Ndiv < 6) || (Ndiv > 12)) {
738 pr_err("WM8960 PLL: Unsupported N=%d\n", Ndiv);
739 return -EINVAL;
742 pll_div->n = Ndiv;
743 Nmod = target % source;
744 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
746 do_div(Kpart, source);
748 K = Kpart & 0xFFFFFFFF;
750 /* Check if we need to round */
751 if ((K % 10) >= 5)
752 K += 5;
754 /* Move down to proper range now rounding is done */
755 K /= 10;
757 pll_div->k = K;
759 pr_debug("WM8960 PLL: N=%x K=%x pre_div=%d\n",
760 pll_div->n, pll_div->k, pll_div->pre_div);
762 return 0;
765 static int wm8960_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
766 int source, unsigned int freq_in, unsigned int freq_out)
768 struct snd_soc_codec *codec = codec_dai->codec;
769 u16 reg;
770 static struct _pll_div pll_div;
771 int ret;
773 if (freq_in && freq_out) {
774 ret = pll_factors(freq_in, freq_out, &pll_div);
775 if (ret != 0)
776 return ret;
779 /* Disable the PLL: even if we are changing the frequency the
780 * PLL needs to be disabled while we do so. */
781 snd_soc_update_bits(codec, WM8960_CLOCK1, 0x1, 0);
782 snd_soc_update_bits(codec, WM8960_POWER2, 0x1, 0);
784 if (!freq_in || !freq_out)
785 return 0;
787 reg = snd_soc_read(codec, WM8960_PLL1) & ~0x3f;
788 reg |= pll_div.pre_div << 4;
789 reg |= pll_div.n;
791 if (pll_div.k) {
792 reg |= 0x20;
794 snd_soc_write(codec, WM8960_PLL2, (pll_div.k >> 18) & 0x3f);
795 snd_soc_write(codec, WM8960_PLL3, (pll_div.k >> 9) & 0x1ff);
796 snd_soc_write(codec, WM8960_PLL4, pll_div.k & 0x1ff);
798 snd_soc_write(codec, WM8960_PLL1, reg);
800 /* Turn it on */
801 snd_soc_update_bits(codec, WM8960_POWER2, 0x1, 0x1);
802 msleep(250);
803 snd_soc_update_bits(codec, WM8960_CLOCK1, 0x1, 0x1);
805 return 0;
808 static int wm8960_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
809 int div_id, int div)
811 struct snd_soc_codec *codec = codec_dai->codec;
812 u16 reg;
814 switch (div_id) {
815 case WM8960_SYSCLKDIV:
816 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1f9;
817 snd_soc_write(codec, WM8960_CLOCK1, reg | div);
818 break;
819 case WM8960_DACDIV:
820 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1c7;
821 snd_soc_write(codec, WM8960_CLOCK1, reg | div);
822 break;
823 case WM8960_OPCLKDIV:
824 reg = snd_soc_read(codec, WM8960_PLL1) & 0x03f;
825 snd_soc_write(codec, WM8960_PLL1, reg | div);
826 break;
827 case WM8960_DCLKDIV:
828 reg = snd_soc_read(codec, WM8960_CLOCK2) & 0x03f;
829 snd_soc_write(codec, WM8960_CLOCK2, reg | div);
830 break;
831 case WM8960_TOCLKSEL:
832 reg = snd_soc_read(codec, WM8960_ADDCTL1) & 0x1fd;
833 snd_soc_write(codec, WM8960_ADDCTL1, reg | div);
834 break;
835 default:
836 return -EINVAL;
839 return 0;
842 static int wm8960_set_bias_level(struct snd_soc_codec *codec,
843 enum snd_soc_bias_level level)
845 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
847 return wm8960->set_bias_level(codec, level);
850 #define WM8960_RATES SNDRV_PCM_RATE_8000_48000
852 #define WM8960_FORMATS \
853 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
854 SNDRV_PCM_FMTBIT_S24_LE)
856 static const struct snd_soc_dai_ops wm8960_dai_ops = {
857 .hw_params = wm8960_hw_params,
858 .digital_mute = wm8960_mute,
859 .set_fmt = wm8960_set_dai_fmt,
860 .set_clkdiv = wm8960_set_dai_clkdiv,
861 .set_pll = wm8960_set_dai_pll,
864 static struct snd_soc_dai_driver wm8960_dai = {
865 .name = "wm8960-hifi",
866 .playback = {
867 .stream_name = "Playback",
868 .channels_min = 1,
869 .channels_max = 2,
870 .rates = WM8960_RATES,
871 .formats = WM8960_FORMATS,},
872 .capture = {
873 .stream_name = "Capture",
874 .channels_min = 1,
875 .channels_max = 2,
876 .rates = WM8960_RATES,
877 .formats = WM8960_FORMATS,},
878 .ops = &wm8960_dai_ops,
879 .symmetric_rates = 1,
882 static int wm8960_suspend(struct snd_soc_codec *codec)
884 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
886 wm8960->set_bias_level(codec, SND_SOC_BIAS_OFF);
887 return 0;
890 static int wm8960_resume(struct snd_soc_codec *codec)
892 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
894 wm8960->set_bias_level(codec, SND_SOC_BIAS_STANDBY);
895 return 0;
898 static int wm8960_probe(struct snd_soc_codec *codec)
900 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
901 struct wm8960_data *pdata = dev_get_platdata(codec->dev);
902 int ret;
904 wm8960->set_bias_level = wm8960_set_bias_level_out3;
906 if (!pdata) {
907 dev_warn(codec->dev, "No platform data supplied\n");
908 } else {
909 if (pdata->dres > WM8960_DRES_MAX) {
910 dev_err(codec->dev, "Invalid DRES: %d\n", pdata->dres);
911 pdata->dres = 0;
914 if (pdata->capless)
915 wm8960->set_bias_level = wm8960_set_bias_level_capless;
918 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8960->control_type);
919 if (ret < 0) {
920 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
921 return ret;
924 ret = wm8960_reset(codec);
925 if (ret < 0) {
926 dev_err(codec->dev, "Failed to issue reset\n");
927 return ret;
930 wm8960->set_bias_level(codec, SND_SOC_BIAS_STANDBY);
932 /* Latch the update bits */
933 snd_soc_update_bits(codec, WM8960_LINVOL, 0x100, 0x100);
934 snd_soc_update_bits(codec, WM8960_RINVOL, 0x100, 0x100);
935 snd_soc_update_bits(codec, WM8960_LADC, 0x100, 0x100);
936 snd_soc_update_bits(codec, WM8960_RADC, 0x100, 0x100);
937 snd_soc_update_bits(codec, WM8960_LDAC, 0x100, 0x100);
938 snd_soc_update_bits(codec, WM8960_RDAC, 0x100, 0x100);
939 snd_soc_update_bits(codec, WM8960_LOUT1, 0x100, 0x100);
940 snd_soc_update_bits(codec, WM8960_ROUT1, 0x100, 0x100);
941 snd_soc_update_bits(codec, WM8960_LOUT2, 0x100, 0x100);
942 snd_soc_update_bits(codec, WM8960_ROUT2, 0x100, 0x100);
944 snd_soc_add_codec_controls(codec, wm8960_snd_controls,
945 ARRAY_SIZE(wm8960_snd_controls));
946 wm8960_add_widgets(codec);
948 return 0;
951 /* power down chip */
952 static int wm8960_remove(struct snd_soc_codec *codec)
954 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
956 wm8960->set_bias_level(codec, SND_SOC_BIAS_OFF);
957 return 0;
960 static struct snd_soc_codec_driver soc_codec_dev_wm8960 = {
961 .probe = wm8960_probe,
962 .remove = wm8960_remove,
963 .suspend = wm8960_suspend,
964 .resume = wm8960_resume,
965 .set_bias_level = wm8960_set_bias_level,
966 .reg_cache_size = ARRAY_SIZE(wm8960_reg),
967 .reg_word_size = sizeof(u16),
968 .reg_cache_default = wm8960_reg,
971 static __devinit int wm8960_i2c_probe(struct i2c_client *i2c,
972 const struct i2c_device_id *id)
974 struct wm8960_priv *wm8960;
975 int ret;
977 wm8960 = devm_kzalloc(&i2c->dev, sizeof(struct wm8960_priv),
978 GFP_KERNEL);
979 if (wm8960 == NULL)
980 return -ENOMEM;
982 i2c_set_clientdata(i2c, wm8960);
983 wm8960->control_type = SND_SOC_I2C;
985 ret = snd_soc_register_codec(&i2c->dev,
986 &soc_codec_dev_wm8960, &wm8960_dai, 1);
988 return ret;
991 static __devexit int wm8960_i2c_remove(struct i2c_client *client)
993 snd_soc_unregister_codec(&client->dev);
994 return 0;
997 static const struct i2c_device_id wm8960_i2c_id[] = {
998 { "wm8960", 0 },
1001 MODULE_DEVICE_TABLE(i2c, wm8960_i2c_id);
1003 static struct i2c_driver wm8960_i2c_driver = {
1004 .driver = {
1005 .name = "wm8960",
1006 .owner = THIS_MODULE,
1008 .probe = wm8960_i2c_probe,
1009 .remove = __devexit_p(wm8960_i2c_remove),
1010 .id_table = wm8960_i2c_id,
1013 static int __init wm8960_modinit(void)
1015 int ret = 0;
1016 ret = i2c_add_driver(&wm8960_i2c_driver);
1017 if (ret != 0) {
1018 printk(KERN_ERR "Failed to register WM8960 I2C driver: %d\n",
1019 ret);
1021 return ret;
1023 module_init(wm8960_modinit);
1025 static void __exit wm8960_exit(void)
1027 i2c_del_driver(&wm8960_i2c_driver);
1029 module_exit(wm8960_exit);
1031 MODULE_DESCRIPTION("ASoC WM8960 driver");
1032 MODULE_AUTHOR("Liam Girdwood");
1033 MODULE_LICENSE("GPL");