Staging/vt66*: kconfig, depends on WLAN
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / soc / codecs / wm8960.c
blobd07bcc1e1c603ed9a86e06c2e293bdb51358d271
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/platform_device.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/soc-dapm.h>
23 #include <sound/initval.h>
24 #include <sound/tlv.h>
26 #include "wm8960.h"
28 #define AUDIO_NAME "wm8960"
30 struct snd_soc_codec_device soc_codec_dev_wm8960;
32 /* R25 - Power 1 */
33 #define WM8960_VREF 0x40
35 /* R28 - Anti-pop 1 */
36 #define WM8960_POBCTRL 0x80
37 #define WM8960_BUFDCOPEN 0x10
38 #define WM8960_BUFIOEN 0x08
39 #define WM8960_SOFT_ST 0x04
40 #define WM8960_HPSTBY 0x01
42 /* R29 - Anti-pop 2 */
43 #define WM8960_DISOP 0x40
46 * wm8960 register cache
47 * We can't read the WM8960 register space when we are
48 * using 2 wire for device control, so we cache them instead.
50 static const u16 wm8960_reg[WM8960_CACHEREGNUM] = {
51 0x0097, 0x0097, 0x0000, 0x0000,
52 0x0000, 0x0008, 0x0000, 0x000a,
53 0x01c0, 0x0000, 0x00ff, 0x00ff,
54 0x0000, 0x0000, 0x0000, 0x0000,
55 0x0000, 0x007b, 0x0100, 0x0032,
56 0x0000, 0x00c3, 0x00c3, 0x01c0,
57 0x0000, 0x0000, 0x0000, 0x0000,
58 0x0000, 0x0000, 0x0000, 0x0000,
59 0x0100, 0x0100, 0x0050, 0x0050,
60 0x0050, 0x0050, 0x0000, 0x0000,
61 0x0000, 0x0000, 0x0040, 0x0000,
62 0x0000, 0x0050, 0x0050, 0x0000,
63 0x0002, 0x0037, 0x004d, 0x0080,
64 0x0008, 0x0031, 0x0026, 0x00e9,
67 struct wm8960_priv {
68 u16 reg_cache[WM8960_CACHEREGNUM];
69 struct snd_soc_codec codec;
72 #define wm8960_reset(c) snd_soc_write(c, WM8960_RESET, 0)
74 /* enumerated controls */
75 static const char *wm8960_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
76 static const char *wm8960_polarity[] = {"No Inversion", "Left Inverted",
77 "Right Inverted", "Stereo Inversion"};
78 static const char *wm8960_3d_upper_cutoff[] = {"High", "Low"};
79 static const char *wm8960_3d_lower_cutoff[] = {"Low", "High"};
80 static const char *wm8960_alcfunc[] = {"Off", "Right", "Left", "Stereo"};
81 static const char *wm8960_alcmode[] = {"ALC", "Limiter"};
83 static const struct soc_enum wm8960_enum[] = {
84 SOC_ENUM_SINGLE(WM8960_DACCTL1, 1, 4, wm8960_deemph),
85 SOC_ENUM_SINGLE(WM8960_DACCTL1, 5, 4, wm8960_polarity),
86 SOC_ENUM_SINGLE(WM8960_DACCTL2, 5, 4, wm8960_polarity),
87 SOC_ENUM_SINGLE(WM8960_3D, 6, 2, wm8960_3d_upper_cutoff),
88 SOC_ENUM_SINGLE(WM8960_3D, 5, 2, wm8960_3d_lower_cutoff),
89 SOC_ENUM_SINGLE(WM8960_ALC1, 7, 4, wm8960_alcfunc),
90 SOC_ENUM_SINGLE(WM8960_ALC3, 8, 2, wm8960_alcmode),
93 static const DECLARE_TLV_DB_SCALE(adc_tlv, -9700, 50, 0);
94 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 50, 1);
95 static const DECLARE_TLV_DB_SCALE(bypass_tlv, -2100, 300, 0);
96 static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
98 static const struct snd_kcontrol_new wm8960_snd_controls[] = {
99 SOC_DOUBLE_R_TLV("Capture Volume", WM8960_LINVOL, WM8960_RINVOL,
100 0, 63, 0, adc_tlv),
101 SOC_DOUBLE_R("Capture Volume ZC Switch", WM8960_LINVOL, WM8960_RINVOL,
102 6, 1, 0),
103 SOC_DOUBLE_R("Capture Switch", WM8960_LINVOL, WM8960_RINVOL,
104 7, 1, 0),
106 SOC_DOUBLE_R_TLV("Playback Volume", WM8960_LDAC, WM8960_RDAC,
107 0, 255, 0, dac_tlv),
109 SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8960_LOUT1, WM8960_ROUT1,
110 0, 127, 0, out_tlv),
111 SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8960_LOUT1, WM8960_ROUT1,
112 7, 1, 0),
114 SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8960_LOUT2, WM8960_ROUT2,
115 0, 127, 0, out_tlv),
116 SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8960_LOUT2, WM8960_ROUT2,
117 7, 1, 0),
118 SOC_SINGLE("Speaker DC Volume", WM8960_CLASSD3, 3, 5, 0),
119 SOC_SINGLE("Speaker AC Volume", WM8960_CLASSD3, 0, 5, 0),
121 SOC_SINGLE("PCM Playback -6dB Switch", WM8960_DACCTL1, 7, 1, 0),
122 SOC_ENUM("ADC Polarity", wm8960_enum[1]),
123 SOC_ENUM("Playback De-emphasis", wm8960_enum[0]),
124 SOC_SINGLE("ADC High Pass Filter Switch", WM8960_DACCTL1, 0, 1, 0),
126 SOC_ENUM("DAC Polarity", wm8960_enum[2]),
128 SOC_ENUM("3D Filter Upper Cut-Off", wm8960_enum[3]),
129 SOC_ENUM("3D Filter Lower Cut-Off", wm8960_enum[4]),
130 SOC_SINGLE("3D Volume", WM8960_3D, 1, 15, 0),
131 SOC_SINGLE("3D Switch", WM8960_3D, 0, 1, 0),
133 SOC_ENUM("ALC Function", wm8960_enum[5]),
134 SOC_SINGLE("ALC Max Gain", WM8960_ALC1, 4, 7, 0),
135 SOC_SINGLE("ALC Target", WM8960_ALC1, 0, 15, 1),
136 SOC_SINGLE("ALC Min Gain", WM8960_ALC2, 4, 7, 0),
137 SOC_SINGLE("ALC Hold Time", WM8960_ALC2, 0, 15, 0),
138 SOC_ENUM("ALC Mode", wm8960_enum[6]),
139 SOC_SINGLE("ALC Decay", WM8960_ALC3, 4, 15, 0),
140 SOC_SINGLE("ALC Attack", WM8960_ALC3, 0, 15, 0),
142 SOC_SINGLE("Noise Gate Threshold", WM8960_NOISEG, 3, 31, 0),
143 SOC_SINGLE("Noise Gate Switch", WM8960_NOISEG, 0, 1, 0),
145 SOC_DOUBLE_R("ADC PCM Capture Volume", WM8960_LINPATH, WM8960_RINPATH,
146 0, 127, 0),
148 SOC_SINGLE_TLV("Left Output Mixer Boost Bypass Volume",
149 WM8960_BYPASS1, 4, 7, 1, bypass_tlv),
150 SOC_SINGLE_TLV("Left Output Mixer LINPUT3 Volume",
151 WM8960_LOUTMIX, 4, 7, 1, bypass_tlv),
152 SOC_SINGLE_TLV("Right Output Mixer Boost Bypass Volume",
153 WM8960_BYPASS2, 4, 7, 1, bypass_tlv),
154 SOC_SINGLE_TLV("Right Output Mixer RINPUT3 Volume",
155 WM8960_ROUTMIX, 4, 7, 1, bypass_tlv),
158 static const struct snd_kcontrol_new wm8960_lin_boost[] = {
159 SOC_DAPM_SINGLE("LINPUT2 Switch", WM8960_LINPATH, 6, 1, 0),
160 SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LINPATH, 7, 1, 0),
161 SOC_DAPM_SINGLE("LINPUT1 Switch", WM8960_LINPATH, 8, 1, 0),
164 static const struct snd_kcontrol_new wm8960_lin[] = {
165 SOC_DAPM_SINGLE("Boost Switch", WM8960_LINPATH, 3, 1, 0),
168 static const struct snd_kcontrol_new wm8960_rin_boost[] = {
169 SOC_DAPM_SINGLE("RINPUT2 Switch", WM8960_RINPATH, 6, 1, 0),
170 SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_RINPATH, 7, 1, 0),
171 SOC_DAPM_SINGLE("RINPUT1 Switch", WM8960_RINPATH, 8, 1, 0),
174 static const struct snd_kcontrol_new wm8960_rin[] = {
175 SOC_DAPM_SINGLE("Boost Switch", WM8960_RINPATH, 3, 1, 0),
178 static const struct snd_kcontrol_new wm8960_loutput_mixer[] = {
179 SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_LOUTMIX, 8, 1, 0),
180 SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LOUTMIX, 7, 1, 0),
181 SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS1, 7, 1, 0),
184 static const struct snd_kcontrol_new wm8960_routput_mixer[] = {
185 SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_ROUTMIX, 8, 1, 0),
186 SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_ROUTMIX, 7, 1, 0),
187 SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS2, 7, 1, 0),
190 static const struct snd_kcontrol_new wm8960_mono_out[] = {
191 SOC_DAPM_SINGLE("Left Switch", WM8960_MONOMIX1, 7, 1, 0),
192 SOC_DAPM_SINGLE("Right Switch", WM8960_MONOMIX2, 7, 1, 0),
195 static const struct snd_soc_dapm_widget wm8960_dapm_widgets[] = {
196 SND_SOC_DAPM_INPUT("LINPUT1"),
197 SND_SOC_DAPM_INPUT("RINPUT1"),
198 SND_SOC_DAPM_INPUT("LINPUT2"),
199 SND_SOC_DAPM_INPUT("RINPUT2"),
200 SND_SOC_DAPM_INPUT("LINPUT3"),
201 SND_SOC_DAPM_INPUT("RINPUT3"),
203 SND_SOC_DAPM_MICBIAS("MICB", WM8960_POWER1, 1, 0),
205 SND_SOC_DAPM_MIXER("Left Boost Mixer", WM8960_POWER1, 5, 0,
206 wm8960_lin_boost, ARRAY_SIZE(wm8960_lin_boost)),
207 SND_SOC_DAPM_MIXER("Right Boost Mixer", WM8960_POWER1, 4, 0,
208 wm8960_rin_boost, ARRAY_SIZE(wm8960_rin_boost)),
210 SND_SOC_DAPM_MIXER("Left Input Mixer", WM8960_POWER3, 5, 0,
211 wm8960_lin, ARRAY_SIZE(wm8960_lin)),
212 SND_SOC_DAPM_MIXER("Right Input Mixer", WM8960_POWER3, 4, 0,
213 wm8960_rin, ARRAY_SIZE(wm8960_rin)),
215 SND_SOC_DAPM_ADC("Left ADC", "Capture", WM8960_POWER2, 3, 0),
216 SND_SOC_DAPM_ADC("Right ADC", "Capture", WM8960_POWER2, 2, 0),
218 SND_SOC_DAPM_DAC("Left DAC", "Playback", WM8960_POWER2, 8, 0),
219 SND_SOC_DAPM_DAC("Right DAC", "Playback", WM8960_POWER2, 7, 0),
221 SND_SOC_DAPM_MIXER("Left Output Mixer", WM8960_POWER3, 3, 0,
222 &wm8960_loutput_mixer[0],
223 ARRAY_SIZE(wm8960_loutput_mixer)),
224 SND_SOC_DAPM_MIXER("Right Output Mixer", WM8960_POWER3, 2, 0,
225 &wm8960_routput_mixer[0],
226 ARRAY_SIZE(wm8960_routput_mixer)),
228 SND_SOC_DAPM_MIXER("Mono Output Mixer", WM8960_POWER2, 1, 0,
229 &wm8960_mono_out[0],
230 ARRAY_SIZE(wm8960_mono_out)),
232 SND_SOC_DAPM_PGA("LOUT1 PGA", WM8960_POWER2, 6, 0, NULL, 0),
233 SND_SOC_DAPM_PGA("ROUT1 PGA", WM8960_POWER2, 5, 0, NULL, 0),
235 SND_SOC_DAPM_PGA("Left Speaker PGA", WM8960_POWER2, 4, 0, NULL, 0),
236 SND_SOC_DAPM_PGA("Right Speaker PGA", WM8960_POWER2, 3, 0, NULL, 0),
238 SND_SOC_DAPM_PGA("Right Speaker Output", WM8960_CLASSD1, 7, 0, NULL, 0),
239 SND_SOC_DAPM_PGA("Left Speaker Output", WM8960_CLASSD1, 6, 0, NULL, 0),
241 SND_SOC_DAPM_OUTPUT("SPK_LP"),
242 SND_SOC_DAPM_OUTPUT("SPK_LN"),
243 SND_SOC_DAPM_OUTPUT("HP_L"),
244 SND_SOC_DAPM_OUTPUT("HP_R"),
245 SND_SOC_DAPM_OUTPUT("SPK_RP"),
246 SND_SOC_DAPM_OUTPUT("SPK_RN"),
247 SND_SOC_DAPM_OUTPUT("OUT3"),
250 static const struct snd_soc_dapm_route audio_paths[] = {
251 { "Left Boost Mixer", "LINPUT1 Switch", "LINPUT1" },
252 { "Left Boost Mixer", "LINPUT2 Switch", "LINPUT2" },
253 { "Left Boost Mixer", "LINPUT3 Switch", "LINPUT3" },
255 { "Left Input Mixer", "Boost Switch", "Left Boost Mixer", },
256 { "Left Input Mixer", NULL, "LINPUT1", }, /* Really Boost Switch */
257 { "Left Input Mixer", NULL, "LINPUT2" },
258 { "Left Input Mixer", NULL, "LINPUT3" },
260 { "Right Boost Mixer", "RINPUT1 Switch", "RINPUT1" },
261 { "Right Boost Mixer", "RINPUT2 Switch", "RINPUT2" },
262 { "Right Boost Mixer", "RINPUT3 Switch", "RINPUT3" },
264 { "Right Input Mixer", "Boost Switch", "Right Boost Mixer", },
265 { "Right Input Mixer", NULL, "RINPUT1", }, /* Really Boost Switch */
266 { "Right Input Mixer", NULL, "RINPUT2" },
267 { "Right Input Mixer", NULL, "LINPUT3" },
269 { "Left ADC", NULL, "Left Input Mixer" },
270 { "Right ADC", NULL, "Right Input Mixer" },
272 { "Left Output Mixer", "LINPUT3 Switch", "LINPUT3" },
273 { "Left Output Mixer", "Boost Bypass Switch", "Left Boost Mixer"} ,
274 { "Left Output Mixer", "PCM Playback Switch", "Left DAC" },
276 { "Right Output Mixer", "RINPUT3 Switch", "RINPUT3" },
277 { "Right Output Mixer", "Boost Bypass Switch", "Right Boost Mixer" } ,
278 { "Right Output Mixer", "PCM Playback Switch", "Right DAC" },
280 { "Mono Output Mixer", "Left Switch", "Left Output Mixer" },
281 { "Mono Output Mixer", "Right Switch", "Right Output Mixer" },
283 { "LOUT1 PGA", NULL, "Left Output Mixer" },
284 { "ROUT1 PGA", NULL, "Right Output Mixer" },
286 { "HP_L", NULL, "LOUT1 PGA" },
287 { "HP_R", NULL, "ROUT1 PGA" },
289 { "Left Speaker PGA", NULL, "Left Output Mixer" },
290 { "Right Speaker PGA", NULL, "Right Output Mixer" },
292 { "Left Speaker Output", NULL, "Left Speaker PGA" },
293 { "Right Speaker Output", NULL, "Right Speaker PGA" },
295 { "SPK_LN", NULL, "Left Speaker Output" },
296 { "SPK_LP", NULL, "Left Speaker Output" },
297 { "SPK_RN", NULL, "Right Speaker Output" },
298 { "SPK_RP", NULL, "Right Speaker Output" },
300 { "OUT3", NULL, "Mono Output Mixer", }
303 static int wm8960_add_widgets(struct snd_soc_codec *codec)
305 snd_soc_dapm_new_controls(codec, wm8960_dapm_widgets,
306 ARRAY_SIZE(wm8960_dapm_widgets));
308 snd_soc_dapm_add_routes(codec, audio_paths, ARRAY_SIZE(audio_paths));
310 return 0;
313 static int wm8960_set_dai_fmt(struct snd_soc_dai *codec_dai,
314 unsigned int fmt)
316 struct snd_soc_codec *codec = codec_dai->codec;
317 u16 iface = 0;
319 /* set master/slave audio interface */
320 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
321 case SND_SOC_DAIFMT_CBM_CFM:
322 iface |= 0x0040;
323 break;
324 case SND_SOC_DAIFMT_CBS_CFS:
325 break;
326 default:
327 return -EINVAL;
330 /* interface format */
331 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
332 case SND_SOC_DAIFMT_I2S:
333 iface |= 0x0002;
334 break;
335 case SND_SOC_DAIFMT_RIGHT_J:
336 break;
337 case SND_SOC_DAIFMT_LEFT_J:
338 iface |= 0x0001;
339 break;
340 case SND_SOC_DAIFMT_DSP_A:
341 iface |= 0x0003;
342 break;
343 case SND_SOC_DAIFMT_DSP_B:
344 iface |= 0x0013;
345 break;
346 default:
347 return -EINVAL;
350 /* clock inversion */
351 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
352 case SND_SOC_DAIFMT_NB_NF:
353 break;
354 case SND_SOC_DAIFMT_IB_IF:
355 iface |= 0x0090;
356 break;
357 case SND_SOC_DAIFMT_IB_NF:
358 iface |= 0x0080;
359 break;
360 case SND_SOC_DAIFMT_NB_IF:
361 iface |= 0x0010;
362 break;
363 default:
364 return -EINVAL;
367 /* set iface */
368 snd_soc_write(codec, WM8960_IFACE1, iface);
369 return 0;
372 static int wm8960_hw_params(struct snd_pcm_substream *substream,
373 struct snd_pcm_hw_params *params,
374 struct snd_soc_dai *dai)
376 struct snd_soc_pcm_runtime *rtd = substream->private_data;
377 struct snd_soc_device *socdev = rtd->socdev;
378 struct snd_soc_codec *codec = socdev->card->codec;
379 u16 iface = snd_soc_read(codec, WM8960_IFACE1) & 0xfff3;
381 /* bit size */
382 switch (params_format(params)) {
383 case SNDRV_PCM_FORMAT_S16_LE:
384 break;
385 case SNDRV_PCM_FORMAT_S20_3LE:
386 iface |= 0x0004;
387 break;
388 case SNDRV_PCM_FORMAT_S24_LE:
389 iface |= 0x0008;
390 break;
393 /* set iface */
394 snd_soc_write(codec, WM8960_IFACE1, iface);
395 return 0;
398 static int wm8960_mute(struct snd_soc_dai *dai, int mute)
400 struct snd_soc_codec *codec = dai->codec;
401 u16 mute_reg = snd_soc_read(codec, WM8960_DACCTL1) & 0xfff7;
403 if (mute)
404 snd_soc_write(codec, WM8960_DACCTL1, mute_reg | 0x8);
405 else
406 snd_soc_write(codec, WM8960_DACCTL1, mute_reg);
407 return 0;
410 static int wm8960_set_bias_level(struct snd_soc_codec *codec,
411 enum snd_soc_bias_level level)
413 struct wm8960_data *pdata = codec->dev->platform_data;
414 u16 reg;
416 switch (level) {
417 case SND_SOC_BIAS_ON:
418 break;
420 case SND_SOC_BIAS_PREPARE:
421 /* Set VMID to 2x50k */
422 reg = snd_soc_read(codec, WM8960_POWER1);
423 reg &= ~0x180;
424 reg |= 0x80;
425 snd_soc_write(codec, WM8960_POWER1, reg);
426 break;
428 case SND_SOC_BIAS_STANDBY:
429 if (codec->bias_level == SND_SOC_BIAS_OFF) {
430 /* Enable anti-pop features */
431 snd_soc_write(codec, WM8960_APOP1,
432 WM8960_POBCTRL | WM8960_SOFT_ST |
433 WM8960_BUFDCOPEN | WM8960_BUFIOEN);
435 /* Discharge HP output */
436 reg = WM8960_DISOP;
437 if (pdata)
438 reg |= pdata->dres << 4;
439 snd_soc_write(codec, WM8960_APOP2, reg);
441 msleep(400);
443 snd_soc_write(codec, WM8960_APOP2, 0);
445 /* Enable & ramp VMID at 2x50k */
446 reg = snd_soc_read(codec, WM8960_POWER1);
447 reg |= 0x80;
448 snd_soc_write(codec, WM8960_POWER1, reg);
449 msleep(100);
451 /* Enable VREF */
452 snd_soc_write(codec, WM8960_POWER1, reg | WM8960_VREF);
454 /* Disable anti-pop features */
455 snd_soc_write(codec, WM8960_APOP1, WM8960_BUFIOEN);
458 /* Set VMID to 2x250k */
459 reg = snd_soc_read(codec, WM8960_POWER1);
460 reg &= ~0x180;
461 reg |= 0x100;
462 snd_soc_write(codec, WM8960_POWER1, reg);
463 break;
465 case SND_SOC_BIAS_OFF:
466 /* Enable anti-pop features */
467 snd_soc_write(codec, WM8960_APOP1,
468 WM8960_POBCTRL | WM8960_SOFT_ST |
469 WM8960_BUFDCOPEN | WM8960_BUFIOEN);
471 /* Disable VMID and VREF, let them discharge */
472 snd_soc_write(codec, WM8960_POWER1, 0);
473 msleep(600);
475 snd_soc_write(codec, WM8960_APOP1, 0);
476 break;
479 codec->bias_level = level;
481 return 0;
484 /* PLL divisors */
485 struct _pll_div {
486 u32 pre_div:1;
487 u32 n:4;
488 u32 k:24;
491 /* The size in bits of the pll divide multiplied by 10
492 * to allow rounding later */
493 #define FIXED_PLL_SIZE ((1 << 24) * 10)
495 static int pll_factors(unsigned int source, unsigned int target,
496 struct _pll_div *pll_div)
498 unsigned long long Kpart;
499 unsigned int K, Ndiv, Nmod;
501 pr_debug("WM8960 PLL: setting %dHz->%dHz\n", source, target);
503 /* Scale up target to PLL operating frequency */
504 target *= 4;
506 Ndiv = target / source;
507 if (Ndiv < 6) {
508 source >>= 1;
509 pll_div->pre_div = 1;
510 Ndiv = target / source;
511 } else
512 pll_div->pre_div = 0;
514 if ((Ndiv < 6) || (Ndiv > 12)) {
515 pr_err("WM8960 PLL: Unsupported N=%d\n", Ndiv);
516 return -EINVAL;
519 pll_div->n = Ndiv;
520 Nmod = target % source;
521 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
523 do_div(Kpart, source);
525 K = Kpart & 0xFFFFFFFF;
527 /* Check if we need to round */
528 if ((K % 10) >= 5)
529 K += 5;
531 /* Move down to proper range now rounding is done */
532 K /= 10;
534 pll_div->k = K;
536 pr_debug("WM8960 PLL: N=%x K=%x pre_div=%d\n",
537 pll_div->n, pll_div->k, pll_div->pre_div);
539 return 0;
542 static int wm8960_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
543 int source, unsigned int freq_in, unsigned int freq_out)
545 struct snd_soc_codec *codec = codec_dai->codec;
546 u16 reg;
547 static struct _pll_div pll_div;
548 int ret;
550 if (freq_in && freq_out) {
551 ret = pll_factors(freq_in, freq_out, &pll_div);
552 if (ret != 0)
553 return ret;
556 /* Disable the PLL: even if we are changing the frequency the
557 * PLL needs to be disabled while we do so. */
558 snd_soc_write(codec, WM8960_CLOCK1,
559 snd_soc_read(codec, WM8960_CLOCK1) & ~1);
560 snd_soc_write(codec, WM8960_POWER2,
561 snd_soc_read(codec, WM8960_POWER2) & ~1);
563 if (!freq_in || !freq_out)
564 return 0;
566 reg = snd_soc_read(codec, WM8960_PLL1) & ~0x3f;
567 reg |= pll_div.pre_div << 4;
568 reg |= pll_div.n;
570 if (pll_div.k) {
571 reg |= 0x20;
573 snd_soc_write(codec, WM8960_PLL2, (pll_div.k >> 18) & 0x3f);
574 snd_soc_write(codec, WM8960_PLL3, (pll_div.k >> 9) & 0x1ff);
575 snd_soc_write(codec, WM8960_PLL4, pll_div.k & 0x1ff);
577 snd_soc_write(codec, WM8960_PLL1, reg);
579 /* Turn it on */
580 snd_soc_write(codec, WM8960_POWER2,
581 snd_soc_read(codec, WM8960_POWER2) | 1);
582 msleep(250);
583 snd_soc_write(codec, WM8960_CLOCK1,
584 snd_soc_read(codec, WM8960_CLOCK1) | 1);
586 return 0;
589 static int wm8960_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
590 int div_id, int div)
592 struct snd_soc_codec *codec = codec_dai->codec;
593 u16 reg;
595 switch (div_id) {
596 case WM8960_SYSCLKSEL:
597 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1fe;
598 snd_soc_write(codec, WM8960_CLOCK1, reg | div);
599 break;
600 case WM8960_SYSCLKDIV:
601 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1f9;
602 snd_soc_write(codec, WM8960_CLOCK1, reg | div);
603 break;
604 case WM8960_DACDIV:
605 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1c7;
606 snd_soc_write(codec, WM8960_CLOCK1, reg | div);
607 break;
608 case WM8960_OPCLKDIV:
609 reg = snd_soc_read(codec, WM8960_PLL1) & 0x03f;
610 snd_soc_write(codec, WM8960_PLL1, reg | div);
611 break;
612 case WM8960_DCLKDIV:
613 reg = snd_soc_read(codec, WM8960_CLOCK2) & 0x03f;
614 snd_soc_write(codec, WM8960_CLOCK2, reg | div);
615 break;
616 case WM8960_TOCLKSEL:
617 reg = snd_soc_read(codec, WM8960_ADDCTL1) & 0x1fd;
618 snd_soc_write(codec, WM8960_ADDCTL1, reg | div);
619 break;
620 default:
621 return -EINVAL;
624 return 0;
627 #define WM8960_RATES SNDRV_PCM_RATE_8000_48000
629 #define WM8960_FORMATS \
630 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
631 SNDRV_PCM_FMTBIT_S24_LE)
633 static struct snd_soc_dai_ops wm8960_dai_ops = {
634 .hw_params = wm8960_hw_params,
635 .digital_mute = wm8960_mute,
636 .set_fmt = wm8960_set_dai_fmt,
637 .set_clkdiv = wm8960_set_dai_clkdiv,
638 .set_pll = wm8960_set_dai_pll,
641 struct snd_soc_dai wm8960_dai = {
642 .name = "WM8960",
643 .playback = {
644 .stream_name = "Playback",
645 .channels_min = 1,
646 .channels_max = 2,
647 .rates = WM8960_RATES,
648 .formats = WM8960_FORMATS,},
649 .capture = {
650 .stream_name = "Capture",
651 .channels_min = 1,
652 .channels_max = 2,
653 .rates = WM8960_RATES,
654 .formats = WM8960_FORMATS,},
655 .ops = &wm8960_dai_ops,
656 .symmetric_rates = 1,
658 EXPORT_SYMBOL_GPL(wm8960_dai);
660 static int wm8960_suspend(struct platform_device *pdev, pm_message_t state)
662 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
663 struct snd_soc_codec *codec = socdev->card->codec;
665 wm8960_set_bias_level(codec, SND_SOC_BIAS_OFF);
666 return 0;
669 static int wm8960_resume(struct platform_device *pdev)
671 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
672 struct snd_soc_codec *codec = socdev->card->codec;
673 int i;
674 u8 data[2];
675 u16 *cache = codec->reg_cache;
677 /* Sync reg_cache with the hardware */
678 for (i = 0; i < ARRAY_SIZE(wm8960_reg); i++) {
679 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
680 data[1] = cache[i] & 0x00ff;
681 codec->hw_write(codec->control_data, data, 2);
684 wm8960_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
685 wm8960_set_bias_level(codec, codec->suspend_bias_level);
686 return 0;
689 static struct snd_soc_codec *wm8960_codec;
691 static int wm8960_probe(struct platform_device *pdev)
693 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
694 struct snd_soc_codec *codec;
695 int ret = 0;
697 if (wm8960_codec == NULL) {
698 dev_err(&pdev->dev, "Codec device not registered\n");
699 return -ENODEV;
702 socdev->card->codec = wm8960_codec;
703 codec = wm8960_codec;
705 /* register pcms */
706 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
707 if (ret < 0) {
708 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
709 goto pcm_err;
712 snd_soc_add_controls(codec, wm8960_snd_controls,
713 ARRAY_SIZE(wm8960_snd_controls));
714 wm8960_add_widgets(codec);
716 return ret;
718 pcm_err:
719 return ret;
722 /* power down chip */
723 static int wm8960_remove(struct platform_device *pdev)
725 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
727 snd_soc_free_pcms(socdev);
728 snd_soc_dapm_free(socdev);
730 return 0;
733 struct snd_soc_codec_device soc_codec_dev_wm8960 = {
734 .probe = wm8960_probe,
735 .remove = wm8960_remove,
736 .suspend = wm8960_suspend,
737 .resume = wm8960_resume,
739 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8960);
741 static int wm8960_register(struct wm8960_priv *wm8960,
742 enum snd_soc_control_type control)
744 struct wm8960_data *pdata = wm8960->codec.dev->platform_data;
745 struct snd_soc_codec *codec = &wm8960->codec;
746 int ret;
747 u16 reg;
749 if (wm8960_codec) {
750 dev_err(codec->dev, "Another WM8960 is registered\n");
751 ret = -EINVAL;
752 goto err;
755 if (!pdata) {
756 dev_warn(codec->dev, "No platform data supplied\n");
757 } else {
758 if (pdata->dres > WM8960_DRES_MAX) {
759 dev_err(codec->dev, "Invalid DRES: %d\n", pdata->dres);
760 pdata->dres = 0;
764 mutex_init(&codec->mutex);
765 INIT_LIST_HEAD(&codec->dapm_widgets);
766 INIT_LIST_HEAD(&codec->dapm_paths);
768 codec->private_data = wm8960;
769 codec->name = "WM8960";
770 codec->owner = THIS_MODULE;
771 codec->bias_level = SND_SOC_BIAS_OFF;
772 codec->set_bias_level = wm8960_set_bias_level;
773 codec->dai = &wm8960_dai;
774 codec->num_dai = 1;
775 codec->reg_cache_size = WM8960_CACHEREGNUM;
776 codec->reg_cache = &wm8960->reg_cache;
778 memcpy(codec->reg_cache, wm8960_reg, sizeof(wm8960_reg));
780 ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
781 if (ret < 0) {
782 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
783 goto err;
786 ret = wm8960_reset(codec);
787 if (ret < 0) {
788 dev_err(codec->dev, "Failed to issue reset\n");
789 goto err;
792 wm8960_dai.dev = codec->dev;
794 wm8960_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
796 /* Latch the update bits */
797 reg = snd_soc_read(codec, WM8960_LINVOL);
798 snd_soc_write(codec, WM8960_LINVOL, reg | 0x100);
799 reg = snd_soc_read(codec, WM8960_RINVOL);
800 snd_soc_write(codec, WM8960_RINVOL, reg | 0x100);
801 reg = snd_soc_read(codec, WM8960_LADC);
802 snd_soc_write(codec, WM8960_LADC, reg | 0x100);
803 reg = snd_soc_read(codec, WM8960_RADC);
804 snd_soc_write(codec, WM8960_RADC, reg | 0x100);
805 reg = snd_soc_read(codec, WM8960_LDAC);
806 snd_soc_write(codec, WM8960_LDAC, reg | 0x100);
807 reg = snd_soc_read(codec, WM8960_RDAC);
808 snd_soc_write(codec, WM8960_RDAC, reg | 0x100);
809 reg = snd_soc_read(codec, WM8960_LOUT1);
810 snd_soc_write(codec, WM8960_LOUT1, reg | 0x100);
811 reg = snd_soc_read(codec, WM8960_ROUT1);
812 snd_soc_write(codec, WM8960_ROUT1, reg | 0x100);
813 reg = snd_soc_read(codec, WM8960_LOUT2);
814 snd_soc_write(codec, WM8960_LOUT2, reg | 0x100);
815 reg = snd_soc_read(codec, WM8960_ROUT2);
816 snd_soc_write(codec, WM8960_ROUT2, reg | 0x100);
818 wm8960_codec = codec;
820 ret = snd_soc_register_codec(codec);
821 if (ret != 0) {
822 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
823 goto err;
826 ret = snd_soc_register_dai(&wm8960_dai);
827 if (ret != 0) {
828 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
829 goto err_codec;
832 return 0;
834 err_codec:
835 snd_soc_unregister_codec(codec);
836 err:
837 kfree(wm8960);
838 return ret;
841 static void wm8960_unregister(struct wm8960_priv *wm8960)
843 wm8960_set_bias_level(&wm8960->codec, SND_SOC_BIAS_OFF);
844 snd_soc_unregister_dai(&wm8960_dai);
845 snd_soc_unregister_codec(&wm8960->codec);
846 kfree(wm8960);
847 wm8960_codec = NULL;
850 static __devinit int wm8960_i2c_probe(struct i2c_client *i2c,
851 const struct i2c_device_id *id)
853 struct wm8960_priv *wm8960;
854 struct snd_soc_codec *codec;
856 wm8960 = kzalloc(sizeof(struct wm8960_priv), GFP_KERNEL);
857 if (wm8960 == NULL)
858 return -ENOMEM;
860 codec = &wm8960->codec;
862 i2c_set_clientdata(i2c, wm8960);
863 codec->control_data = i2c;
865 codec->dev = &i2c->dev;
867 return wm8960_register(wm8960, SND_SOC_I2C);
870 static __devexit int wm8960_i2c_remove(struct i2c_client *client)
872 struct wm8960_priv *wm8960 = i2c_get_clientdata(client);
873 wm8960_unregister(wm8960);
874 return 0;
877 static const struct i2c_device_id wm8960_i2c_id[] = {
878 { "wm8960", 0 },
881 MODULE_DEVICE_TABLE(i2c, wm8960_i2c_id);
883 static struct i2c_driver wm8960_i2c_driver = {
884 .driver = {
885 .name = "WM8960 I2C Codec",
886 .owner = THIS_MODULE,
888 .probe = wm8960_i2c_probe,
889 .remove = __devexit_p(wm8960_i2c_remove),
890 .id_table = wm8960_i2c_id,
893 static int __init wm8960_modinit(void)
895 int ret;
897 ret = i2c_add_driver(&wm8960_i2c_driver);
898 if (ret != 0) {
899 printk(KERN_ERR "Failed to register WM8960 I2C driver: %d\n",
900 ret);
903 return ret;
905 module_init(wm8960_modinit);
907 static void __exit wm8960_exit(void)
909 i2c_del_driver(&wm8960_i2c_driver);
911 module_exit(wm8960_exit);
914 MODULE_DESCRIPTION("ASoC WM8960 driver");
915 MODULE_AUTHOR("Liam Girdwood");
916 MODULE_LICENSE("GPL");