Full support for Ginger Console
[linux-ginger.git] / sound / soc / codecs / wm8960.c
blobf59703be61c84b6213600edab224c0f71ab63bf6
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 snd_soc_dapm_new_widgets(codec);
311 return 0;
314 static int wm8960_set_dai_fmt(struct snd_soc_dai *codec_dai,
315 unsigned int fmt)
317 struct snd_soc_codec *codec = codec_dai->codec;
318 u16 iface = 0;
320 /* set master/slave audio interface */
321 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
322 case SND_SOC_DAIFMT_CBM_CFM:
323 iface |= 0x0040;
324 break;
325 case SND_SOC_DAIFMT_CBS_CFS:
326 break;
327 default:
328 return -EINVAL;
331 /* interface format */
332 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
333 case SND_SOC_DAIFMT_I2S:
334 iface |= 0x0002;
335 break;
336 case SND_SOC_DAIFMT_RIGHT_J:
337 break;
338 case SND_SOC_DAIFMT_LEFT_J:
339 iface |= 0x0001;
340 break;
341 case SND_SOC_DAIFMT_DSP_A:
342 iface |= 0x0003;
343 break;
344 case SND_SOC_DAIFMT_DSP_B:
345 iface |= 0x0013;
346 break;
347 default:
348 return -EINVAL;
351 /* clock inversion */
352 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
353 case SND_SOC_DAIFMT_NB_NF:
354 break;
355 case SND_SOC_DAIFMT_IB_IF:
356 iface |= 0x0090;
357 break;
358 case SND_SOC_DAIFMT_IB_NF:
359 iface |= 0x0080;
360 break;
361 case SND_SOC_DAIFMT_NB_IF:
362 iface |= 0x0010;
363 break;
364 default:
365 return -EINVAL;
368 /* set iface */
369 snd_soc_write(codec, WM8960_IFACE1, iface);
370 return 0;
373 static int wm8960_hw_params(struct snd_pcm_substream *substream,
374 struct snd_pcm_hw_params *params,
375 struct snd_soc_dai *dai)
377 struct snd_soc_pcm_runtime *rtd = substream->private_data;
378 struct snd_soc_device *socdev = rtd->socdev;
379 struct snd_soc_codec *codec = socdev->card->codec;
380 u16 iface = snd_soc_read(codec, WM8960_IFACE1) & 0xfff3;
382 /* bit size */
383 switch (params_format(params)) {
384 case SNDRV_PCM_FORMAT_S16_LE:
385 break;
386 case SNDRV_PCM_FORMAT_S20_3LE:
387 iface |= 0x0004;
388 break;
389 case SNDRV_PCM_FORMAT_S24_LE:
390 iface |= 0x0008;
391 break;
394 /* set iface */
395 snd_soc_write(codec, WM8960_IFACE1, iface);
396 return 0;
399 static int wm8960_mute(struct snd_soc_dai *dai, int mute)
401 struct snd_soc_codec *codec = dai->codec;
402 u16 mute_reg = snd_soc_read(codec, WM8960_DACCTL1) & 0xfff7;
404 if (mute)
405 snd_soc_write(codec, WM8960_DACCTL1, mute_reg | 0x8);
406 else
407 snd_soc_write(codec, WM8960_DACCTL1, mute_reg);
408 return 0;
411 static int wm8960_set_bias_level(struct snd_soc_codec *codec,
412 enum snd_soc_bias_level level)
414 struct wm8960_data *pdata = codec->dev->platform_data;
415 u16 reg;
417 switch (level) {
418 case SND_SOC_BIAS_ON:
419 break;
421 case SND_SOC_BIAS_PREPARE:
422 /* Set VMID to 2x50k */
423 reg = snd_soc_read(codec, WM8960_POWER1);
424 reg &= ~0x180;
425 reg |= 0x80;
426 snd_soc_write(codec, WM8960_POWER1, reg);
427 break;
429 case SND_SOC_BIAS_STANDBY:
430 if (codec->bias_level == SND_SOC_BIAS_OFF) {
431 /* Enable anti-pop features */
432 snd_soc_write(codec, WM8960_APOP1,
433 WM8960_POBCTRL | WM8960_SOFT_ST |
434 WM8960_BUFDCOPEN | WM8960_BUFIOEN);
436 /* Discharge HP output */
437 reg = WM8960_DISOP;
438 if (pdata)
439 reg |= pdata->dres << 4;
440 snd_soc_write(codec, WM8960_APOP2, reg);
442 msleep(400);
444 snd_soc_write(codec, WM8960_APOP2, 0);
446 /* Enable & ramp VMID at 2x50k */
447 reg = snd_soc_read(codec, WM8960_POWER1);
448 reg |= 0x80;
449 snd_soc_write(codec, WM8960_POWER1, reg);
450 msleep(100);
452 /* Enable VREF */
453 snd_soc_write(codec, WM8960_POWER1, reg | WM8960_VREF);
455 /* Disable anti-pop features */
456 snd_soc_write(codec, WM8960_APOP1, WM8960_BUFIOEN);
459 /* Set VMID to 2x250k */
460 reg = snd_soc_read(codec, WM8960_POWER1);
461 reg &= ~0x180;
462 reg |= 0x100;
463 snd_soc_write(codec, WM8960_POWER1, reg);
464 break;
466 case SND_SOC_BIAS_OFF:
467 /* Enable anti-pop features */
468 snd_soc_write(codec, WM8960_APOP1,
469 WM8960_POBCTRL | WM8960_SOFT_ST |
470 WM8960_BUFDCOPEN | WM8960_BUFIOEN);
472 /* Disable VMID and VREF, let them discharge */
473 snd_soc_write(codec, WM8960_POWER1, 0);
474 msleep(600);
476 snd_soc_write(codec, WM8960_APOP1, 0);
477 break;
480 codec->bias_level = level;
482 return 0;
485 /* PLL divisors */
486 struct _pll_div {
487 u32 pre_div:1;
488 u32 n:4;
489 u32 k:24;
492 /* The size in bits of the pll divide multiplied by 10
493 * to allow rounding later */
494 #define FIXED_PLL_SIZE ((1 << 24) * 10)
496 static int pll_factors(unsigned int source, unsigned int target,
497 struct _pll_div *pll_div)
499 unsigned long long Kpart;
500 unsigned int K, Ndiv, Nmod;
502 pr_debug("WM8960 PLL: setting %dHz->%dHz\n", source, target);
504 /* Scale up target to PLL operating frequency */
505 target *= 4;
507 Ndiv = target / source;
508 if (Ndiv < 6) {
509 source >>= 1;
510 pll_div->pre_div = 1;
511 Ndiv = target / source;
512 } else
513 pll_div->pre_div = 0;
515 if ((Ndiv < 6) || (Ndiv > 12)) {
516 pr_err("WM8960 PLL: Unsupported N=%d\n", Ndiv);
517 return -EINVAL;
520 pll_div->n = Ndiv;
521 Nmod = target % source;
522 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
524 do_div(Kpart, source);
526 K = Kpart & 0xFFFFFFFF;
528 /* Check if we need to round */
529 if ((K % 10) >= 5)
530 K += 5;
532 /* Move down to proper range now rounding is done */
533 K /= 10;
535 pll_div->k = K;
537 pr_debug("WM8960 PLL: N=%x K=%x pre_div=%d\n",
538 pll_div->n, pll_div->k, pll_div->pre_div);
540 return 0;
543 static int wm8960_set_dai_pll(struct snd_soc_dai *codec_dai,
544 int pll_id, unsigned int freq_in, unsigned int freq_out)
546 struct snd_soc_codec *codec = codec_dai->codec;
547 u16 reg;
548 static struct _pll_div pll_div;
549 int ret;
551 if (freq_in && freq_out) {
552 ret = pll_factors(freq_in, freq_out, &pll_div);
553 if (ret != 0)
554 return ret;
557 /* Disable the PLL: even if we are changing the frequency the
558 * PLL needs to be disabled while we do so. */
559 snd_soc_write(codec, WM8960_CLOCK1,
560 snd_soc_read(codec, WM8960_CLOCK1) & ~1);
561 snd_soc_write(codec, WM8960_POWER2,
562 snd_soc_read(codec, WM8960_POWER2) & ~1);
564 if (!freq_in || !freq_out)
565 return 0;
567 reg = snd_soc_read(codec, WM8960_PLL1) & ~0x3f;
568 reg |= pll_div.pre_div << 4;
569 reg |= pll_div.n;
571 if (pll_div.k) {
572 reg |= 0x20;
574 snd_soc_write(codec, WM8960_PLL2, (pll_div.k >> 18) & 0x3f);
575 snd_soc_write(codec, WM8960_PLL3, (pll_div.k >> 9) & 0x1ff);
576 snd_soc_write(codec, WM8960_PLL4, pll_div.k & 0x1ff);
578 snd_soc_write(codec, WM8960_PLL1, reg);
580 /* Turn it on */
581 snd_soc_write(codec, WM8960_POWER2,
582 snd_soc_read(codec, WM8960_POWER2) | 1);
583 msleep(250);
584 snd_soc_write(codec, WM8960_CLOCK1,
585 snd_soc_read(codec, WM8960_CLOCK1) | 1);
587 return 0;
590 static int wm8960_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
591 int div_id, int div)
593 struct snd_soc_codec *codec = codec_dai->codec;
594 u16 reg;
596 switch (div_id) {
597 case WM8960_SYSCLKSEL:
598 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1fe;
599 snd_soc_write(codec, WM8960_CLOCK1, reg | div);
600 break;
601 case WM8960_SYSCLKDIV:
602 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1f9;
603 snd_soc_write(codec, WM8960_CLOCK1, reg | div);
604 break;
605 case WM8960_DACDIV:
606 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1c7;
607 snd_soc_write(codec, WM8960_CLOCK1, reg | div);
608 break;
609 case WM8960_OPCLKDIV:
610 reg = snd_soc_read(codec, WM8960_PLL1) & 0x03f;
611 snd_soc_write(codec, WM8960_PLL1, reg | div);
612 break;
613 case WM8960_DCLKDIV:
614 reg = snd_soc_read(codec, WM8960_CLOCK2) & 0x03f;
615 snd_soc_write(codec, WM8960_CLOCK2, reg | div);
616 break;
617 case WM8960_TOCLKSEL:
618 reg = snd_soc_read(codec, WM8960_ADDCTL1) & 0x1fd;
619 snd_soc_write(codec, WM8960_ADDCTL1, reg | div);
620 break;
621 default:
622 return -EINVAL;
625 return 0;
628 #define WM8960_RATES SNDRV_PCM_RATE_8000_48000
630 #define WM8960_FORMATS \
631 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
632 SNDRV_PCM_FMTBIT_S24_LE)
634 static struct snd_soc_dai_ops wm8960_dai_ops = {
635 .hw_params = wm8960_hw_params,
636 .digital_mute = wm8960_mute,
637 .set_fmt = wm8960_set_dai_fmt,
638 .set_clkdiv = wm8960_set_dai_clkdiv,
639 .set_pll = wm8960_set_dai_pll,
642 struct snd_soc_dai wm8960_dai = {
643 .name = "WM8960",
644 .playback = {
645 .stream_name = "Playback",
646 .channels_min = 1,
647 .channels_max = 2,
648 .rates = WM8960_RATES,
649 .formats = WM8960_FORMATS,},
650 .capture = {
651 .stream_name = "Capture",
652 .channels_min = 1,
653 .channels_max = 2,
654 .rates = WM8960_RATES,
655 .formats = WM8960_FORMATS,},
656 .ops = &wm8960_dai_ops,
657 .symmetric_rates = 1,
659 EXPORT_SYMBOL_GPL(wm8960_dai);
661 static int wm8960_suspend(struct platform_device *pdev, pm_message_t state)
663 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
664 struct snd_soc_codec *codec = socdev->card->codec;
666 wm8960_set_bias_level(codec, SND_SOC_BIAS_OFF);
667 return 0;
670 static int wm8960_resume(struct platform_device *pdev)
672 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
673 struct snd_soc_codec *codec = socdev->card->codec;
674 int i;
675 u8 data[2];
676 u16 *cache = codec->reg_cache;
678 /* Sync reg_cache with the hardware */
679 for (i = 0; i < ARRAY_SIZE(wm8960_reg); i++) {
680 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
681 data[1] = cache[i] & 0x00ff;
682 codec->hw_write(codec->control_data, data, 2);
685 wm8960_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
686 wm8960_set_bias_level(codec, codec->suspend_bias_level);
687 return 0;
690 static struct snd_soc_codec *wm8960_codec;
692 static int wm8960_probe(struct platform_device *pdev)
694 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
695 struct snd_soc_codec *codec;
696 int ret = 0;
698 if (wm8960_codec == NULL) {
699 dev_err(&pdev->dev, "Codec device not registered\n");
700 return -ENODEV;
703 socdev->card->codec = wm8960_codec;
704 codec = wm8960_codec;
706 /* register pcms */
707 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
708 if (ret < 0) {
709 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
710 goto pcm_err;
713 snd_soc_add_controls(codec, wm8960_snd_controls,
714 ARRAY_SIZE(wm8960_snd_controls));
715 wm8960_add_widgets(codec);
716 ret = snd_soc_init_card(socdev);
717 if (ret < 0) {
718 dev_err(codec->dev, "failed to register card: %d\n", ret);
719 goto card_err;
722 return ret;
724 card_err:
725 snd_soc_free_pcms(socdev);
726 snd_soc_dapm_free(socdev);
727 pcm_err:
728 return ret;
731 /* power down chip */
732 static int wm8960_remove(struct platform_device *pdev)
734 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
736 snd_soc_free_pcms(socdev);
737 snd_soc_dapm_free(socdev);
739 return 0;
742 struct snd_soc_codec_device soc_codec_dev_wm8960 = {
743 .probe = wm8960_probe,
744 .remove = wm8960_remove,
745 .suspend = wm8960_suspend,
746 .resume = wm8960_resume,
748 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8960);
750 static int wm8960_register(struct wm8960_priv *wm8960,
751 enum snd_soc_control_type control)
753 struct wm8960_data *pdata = wm8960->codec.dev->platform_data;
754 struct snd_soc_codec *codec = &wm8960->codec;
755 int ret;
756 u16 reg;
758 if (wm8960_codec) {
759 dev_err(codec->dev, "Another WM8960 is registered\n");
760 ret = -EINVAL;
761 goto err;
764 if (!pdata) {
765 dev_warn(codec->dev, "No platform data supplied\n");
766 } else {
767 if (pdata->dres > WM8960_DRES_MAX) {
768 dev_err(codec->dev, "Invalid DRES: %d\n", pdata->dres);
769 pdata->dres = 0;
773 mutex_init(&codec->mutex);
774 INIT_LIST_HEAD(&codec->dapm_widgets);
775 INIT_LIST_HEAD(&codec->dapm_paths);
777 codec->private_data = wm8960;
778 codec->name = "WM8960";
779 codec->owner = THIS_MODULE;
780 codec->bias_level = SND_SOC_BIAS_OFF;
781 codec->set_bias_level = wm8960_set_bias_level;
782 codec->dai = &wm8960_dai;
783 codec->num_dai = 1;
784 codec->reg_cache_size = WM8960_CACHEREGNUM;
785 codec->reg_cache = &wm8960->reg_cache;
787 memcpy(codec->reg_cache, wm8960_reg, sizeof(wm8960_reg));
789 ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
790 if (ret < 0) {
791 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
792 goto err;
795 ret = wm8960_reset(codec);
796 if (ret < 0) {
797 dev_err(codec->dev, "Failed to issue reset\n");
798 goto err;
801 wm8960_dai.dev = codec->dev;
803 wm8960_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
805 /* Latch the update bits */
806 reg = snd_soc_read(codec, WM8960_LINVOL);
807 snd_soc_write(codec, WM8960_LINVOL, reg | 0x100);
808 reg = snd_soc_read(codec, WM8960_RINVOL);
809 snd_soc_write(codec, WM8960_RINVOL, reg | 0x100);
810 reg = snd_soc_read(codec, WM8960_LADC);
811 snd_soc_write(codec, WM8960_LADC, reg | 0x100);
812 reg = snd_soc_read(codec, WM8960_RADC);
813 snd_soc_write(codec, WM8960_RADC, reg | 0x100);
814 reg = snd_soc_read(codec, WM8960_LDAC);
815 snd_soc_write(codec, WM8960_LDAC, reg | 0x100);
816 reg = snd_soc_read(codec, WM8960_RDAC);
817 snd_soc_write(codec, WM8960_RDAC, reg | 0x100);
818 reg = snd_soc_read(codec, WM8960_LOUT1);
819 snd_soc_write(codec, WM8960_LOUT1, reg | 0x100);
820 reg = snd_soc_read(codec, WM8960_ROUT1);
821 snd_soc_write(codec, WM8960_ROUT1, reg | 0x100);
822 reg = snd_soc_read(codec, WM8960_LOUT2);
823 snd_soc_write(codec, WM8960_LOUT2, reg | 0x100);
824 reg = snd_soc_read(codec, WM8960_ROUT2);
825 snd_soc_write(codec, WM8960_ROUT2, reg | 0x100);
827 wm8960_codec = codec;
829 ret = snd_soc_register_codec(codec);
830 if (ret != 0) {
831 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
832 goto err;
835 ret = snd_soc_register_dai(&wm8960_dai);
836 if (ret != 0) {
837 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
838 goto err_codec;
841 return 0;
843 err_codec:
844 snd_soc_unregister_codec(codec);
845 err:
846 kfree(wm8960);
847 return ret;
850 static void wm8960_unregister(struct wm8960_priv *wm8960)
852 wm8960_set_bias_level(&wm8960->codec, SND_SOC_BIAS_OFF);
853 snd_soc_unregister_dai(&wm8960_dai);
854 snd_soc_unregister_codec(&wm8960->codec);
855 kfree(wm8960);
856 wm8960_codec = NULL;
859 static __devinit int wm8960_i2c_probe(struct i2c_client *i2c,
860 const struct i2c_device_id *id)
862 struct wm8960_priv *wm8960;
863 struct snd_soc_codec *codec;
865 wm8960 = kzalloc(sizeof(struct wm8960_priv), GFP_KERNEL);
866 if (wm8960 == NULL)
867 return -ENOMEM;
869 codec = &wm8960->codec;
871 i2c_set_clientdata(i2c, wm8960);
872 codec->control_data = i2c;
874 codec->dev = &i2c->dev;
876 return wm8960_register(wm8960, SND_SOC_I2C);
879 static __devexit int wm8960_i2c_remove(struct i2c_client *client)
881 struct wm8960_priv *wm8960 = i2c_get_clientdata(client);
882 wm8960_unregister(wm8960);
883 return 0;
886 #ifdef CONFIG_PM
887 static int wm8960_i2c_suspend(struct i2c_client *client, pm_message_t msg)
889 return snd_soc_suspend_device(&client->dev);
892 static int wm8960_i2c_resume(struct i2c_client *client)
894 return snd_soc_resume_device(&client->dev);
896 #else
897 #define wm8960_i2c_suspend NULL
898 #define wm8960_i2c_resume NULL
899 #endif
901 static const struct i2c_device_id wm8960_i2c_id[] = {
902 { "wm8960", 0 },
905 MODULE_DEVICE_TABLE(i2c, wm8960_i2c_id);
907 static struct i2c_driver wm8960_i2c_driver = {
908 .driver = {
909 .name = "WM8960 I2C Codec",
910 .owner = THIS_MODULE,
912 .probe = wm8960_i2c_probe,
913 .remove = __devexit_p(wm8960_i2c_remove),
914 .suspend = wm8960_i2c_suspend,
915 .resume = wm8960_i2c_resume,
916 .id_table = wm8960_i2c_id,
919 static int __init wm8960_modinit(void)
921 int ret;
923 ret = i2c_add_driver(&wm8960_i2c_driver);
924 if (ret != 0) {
925 printk(KERN_ERR "Failed to register WM8960 I2C driver: %d\n",
926 ret);
929 return ret;
931 module_init(wm8960_modinit);
933 static void __exit wm8960_exit(void)
935 i2c_del_driver(&wm8960_i2c_driver);
937 module_exit(wm8960_exit);
940 MODULE_DESCRIPTION("ASoC WM8960 driver");
941 MODULE_AUTHOR("Liam Girdwood");
942 MODULE_LICENSE("GPL");