blob: 40390afa75f3ee8ed24cadf5d0ed193bf3be750d [file] [log] [blame]
Mark Brownf2644a22009-04-07 19:20:14 +01001/*
2 * wm8960.c -- WM8960 ALSA SoC Audio driver
3 *
4 * Author: Liam Girdwood
5 *
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 */
10
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>
25
26#include "wm8960.h"
27
28#define AUDIO_NAME "wm8960"
29
30struct snd_soc_codec_device soc_codec_dev_wm8960;
31
32/* R25 - Power 1 */
33#define WM8960_VREF 0x40
34
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
41
42/* R29 - Anti-pop 2 */
43#define WM8960_DISOP 0x40
44
45/*
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.
49 */
50static 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,
65};
66
67struct wm8960_priv {
68 u16 reg_cache[WM8960_CACHEREGNUM];
69 struct snd_soc_codec codec;
70};
71
Mark Brown17a52fd2009-07-05 17:24:50 +010072#define wm8960_reset(c) snd_soc_write(c, WM8960_RESET, 0)
Mark Brownf2644a22009-04-07 19:20:14 +010073
74/* enumerated controls */
75static const char *wm8960_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
76static const char *wm8960_polarity[] = {"No Inversion", "Left Inverted",
77 "Right Inverted", "Stereo Inversion"};
78static const char *wm8960_3d_upper_cutoff[] = {"High", "Low"};
79static const char *wm8960_3d_lower_cutoff[] = {"Low", "High"};
80static const char *wm8960_alcfunc[] = {"Off", "Right", "Left", "Stereo"};
81static const char *wm8960_alcmode[] = {"ALC", "Limiter"};
82
83static 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),
91};
92
93static const DECLARE_TLV_DB_SCALE(adc_tlv, -9700, 50, 0);
94static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 50, 1);
95static const DECLARE_TLV_DB_SCALE(bypass_tlv, -2100, 300, 0);
96static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
97
98static const struct snd_kcontrol_new wm8960_snd_controls[] = {
99SOC_DOUBLE_R_TLV("Capture Volume", WM8960_LINVOL, WM8960_RINVOL,
100 0, 63, 0, adc_tlv),
101SOC_DOUBLE_R("Capture Volume ZC Switch", WM8960_LINVOL, WM8960_RINVOL,
102 6, 1, 0),
103SOC_DOUBLE_R("Capture Switch", WM8960_LINVOL, WM8960_RINVOL,
104 7, 1, 0),
105
106SOC_DOUBLE_R_TLV("Playback Volume", WM8960_LDAC, WM8960_RDAC,
107 0, 255, 0, dac_tlv),
108
109SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8960_LOUT1, WM8960_ROUT1,
110 0, 127, 0, out_tlv),
111SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8960_LOUT1, WM8960_ROUT1,
112 7, 1, 0),
113
114SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8960_LOUT2, WM8960_ROUT2,
115 0, 127, 0, out_tlv),
116SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8960_LOUT2, WM8960_ROUT2,
117 7, 1, 0),
118SOC_SINGLE("Speaker DC Volume", WM8960_CLASSD3, 3, 5, 0),
119SOC_SINGLE("Speaker AC Volume", WM8960_CLASSD3, 0, 5, 0),
120
121SOC_SINGLE("PCM Playback -6dB Switch", WM8960_DACCTL1, 7, 1, 0),
122SOC_ENUM("ADC Polarity", wm8960_enum[1]),
123SOC_ENUM("Playback De-emphasis", wm8960_enum[0]),
124SOC_SINGLE("ADC High Pass Filter Switch", WM8960_DACCTL1, 0, 1, 0),
125
126SOC_ENUM("DAC Polarity", wm8960_enum[2]),
127
128SOC_ENUM("3D Filter Upper Cut-Off", wm8960_enum[3]),
129SOC_ENUM("3D Filter Lower Cut-Off", wm8960_enum[4]),
130SOC_SINGLE("3D Volume", WM8960_3D, 1, 15, 0),
131SOC_SINGLE("3D Switch", WM8960_3D, 0, 1, 0),
132
133SOC_ENUM("ALC Function", wm8960_enum[5]),
134SOC_SINGLE("ALC Max Gain", WM8960_ALC1, 4, 7, 0),
135SOC_SINGLE("ALC Target", WM8960_ALC1, 0, 15, 1),
136SOC_SINGLE("ALC Min Gain", WM8960_ALC2, 4, 7, 0),
137SOC_SINGLE("ALC Hold Time", WM8960_ALC2, 0, 15, 0),
138SOC_ENUM("ALC Mode", wm8960_enum[6]),
139SOC_SINGLE("ALC Decay", WM8960_ALC3, 4, 15, 0),
140SOC_SINGLE("ALC Attack", WM8960_ALC3, 0, 15, 0),
141
142SOC_SINGLE("Noise Gate Threshold", WM8960_NOISEG, 3, 31, 0),
143SOC_SINGLE("Noise Gate Switch", WM8960_NOISEG, 0, 1, 0),
144
145SOC_DOUBLE_R("ADC PCM Capture Volume", WM8960_LINPATH, WM8960_RINPATH,
146 0, 127, 0),
147
148SOC_SINGLE_TLV("Left Output Mixer Boost Bypass Volume",
149 WM8960_BYPASS1, 4, 7, 1, bypass_tlv),
150SOC_SINGLE_TLV("Left Output Mixer LINPUT3 Volume",
151 WM8960_LOUTMIX, 4, 7, 1, bypass_tlv),
152SOC_SINGLE_TLV("Right Output Mixer Boost Bypass Volume",
153 WM8960_BYPASS2, 4, 7, 1, bypass_tlv),
154SOC_SINGLE_TLV("Right Output Mixer RINPUT3 Volume",
155 WM8960_ROUTMIX, 4, 7, 1, bypass_tlv),
156};
157
158static const struct snd_kcontrol_new wm8960_lin_boost[] = {
159SOC_DAPM_SINGLE("LINPUT2 Switch", WM8960_LINPATH, 6, 1, 0),
160SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LINPATH, 7, 1, 0),
161SOC_DAPM_SINGLE("LINPUT1 Switch", WM8960_LINPATH, 8, 1, 0),
162};
163
164static const struct snd_kcontrol_new wm8960_lin[] = {
165SOC_DAPM_SINGLE("Boost Switch", WM8960_LINPATH, 3, 1, 0),
166};
167
168static const struct snd_kcontrol_new wm8960_rin_boost[] = {
169SOC_DAPM_SINGLE("RINPUT2 Switch", WM8960_RINPATH, 6, 1, 0),
170SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_RINPATH, 7, 1, 0),
171SOC_DAPM_SINGLE("RINPUT1 Switch", WM8960_RINPATH, 8, 1, 0),
172};
173
174static const struct snd_kcontrol_new wm8960_rin[] = {
175SOC_DAPM_SINGLE("Boost Switch", WM8960_RINPATH, 3, 1, 0),
176};
177
178static const struct snd_kcontrol_new wm8960_loutput_mixer[] = {
179SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_LOUTMIX, 8, 1, 0),
180SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LOUTMIX, 7, 1, 0),
181SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS1, 7, 1, 0),
182};
183
184static const struct snd_kcontrol_new wm8960_routput_mixer[] = {
185SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_ROUTMIX, 8, 1, 0),
186SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_ROUTMIX, 7, 1, 0),
187SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS2, 7, 1, 0),
188};
189
190static const struct snd_kcontrol_new wm8960_mono_out[] = {
191SOC_DAPM_SINGLE("Left Switch", WM8960_MONOMIX1, 7, 1, 0),
192SOC_DAPM_SINGLE("Right Switch", WM8960_MONOMIX2, 7, 1, 0),
193};
194
195static const struct snd_soc_dapm_widget wm8960_dapm_widgets[] = {
196SND_SOC_DAPM_INPUT("LINPUT1"),
197SND_SOC_DAPM_INPUT("RINPUT1"),
198SND_SOC_DAPM_INPUT("LINPUT2"),
199SND_SOC_DAPM_INPUT("RINPUT2"),
200SND_SOC_DAPM_INPUT("LINPUT3"),
201SND_SOC_DAPM_INPUT("RINPUT3"),
202
203SND_SOC_DAPM_MICBIAS("MICB", WM8960_POWER1, 1, 0),
204
205SND_SOC_DAPM_MIXER("Left Boost Mixer", WM8960_POWER1, 5, 0,
206 wm8960_lin_boost, ARRAY_SIZE(wm8960_lin_boost)),
207SND_SOC_DAPM_MIXER("Right Boost Mixer", WM8960_POWER1, 4, 0,
208 wm8960_rin_boost, ARRAY_SIZE(wm8960_rin_boost)),
209
210SND_SOC_DAPM_MIXER("Left Input Mixer", WM8960_POWER3, 5, 0,
211 wm8960_lin, ARRAY_SIZE(wm8960_lin)),
212SND_SOC_DAPM_MIXER("Right Input Mixer", WM8960_POWER3, 4, 0,
213 wm8960_rin, ARRAY_SIZE(wm8960_rin)),
214
215SND_SOC_DAPM_ADC("Left ADC", "Capture", WM8960_POWER2, 3, 0),
216SND_SOC_DAPM_ADC("Right ADC", "Capture", WM8960_POWER2, 2, 0),
217
218SND_SOC_DAPM_DAC("Left DAC", "Playback", WM8960_POWER2, 8, 0),
219SND_SOC_DAPM_DAC("Right DAC", "Playback", WM8960_POWER2, 7, 0),
220
221SND_SOC_DAPM_MIXER("Left Output Mixer", WM8960_POWER3, 3, 0,
222 &wm8960_loutput_mixer[0],
223 ARRAY_SIZE(wm8960_loutput_mixer)),
224SND_SOC_DAPM_MIXER("Right Output Mixer", WM8960_POWER3, 2, 0,
225 &wm8960_routput_mixer[0],
226 ARRAY_SIZE(wm8960_routput_mixer)),
227
228SND_SOC_DAPM_MIXER("Mono Output Mixer", WM8960_POWER2, 1, 0,
229 &wm8960_mono_out[0],
230 ARRAY_SIZE(wm8960_mono_out)),
231
232SND_SOC_DAPM_PGA("LOUT1 PGA", WM8960_POWER2, 6, 0, NULL, 0),
233SND_SOC_DAPM_PGA("ROUT1 PGA", WM8960_POWER2, 5, 0, NULL, 0),
234
235SND_SOC_DAPM_PGA("Left Speaker PGA", WM8960_POWER2, 4, 0, NULL, 0),
236SND_SOC_DAPM_PGA("Right Speaker PGA", WM8960_POWER2, 3, 0, NULL, 0),
237
238SND_SOC_DAPM_PGA("Right Speaker Output", WM8960_CLASSD1, 7, 0, NULL, 0),
239SND_SOC_DAPM_PGA("Left Speaker Output", WM8960_CLASSD1, 6, 0, NULL, 0),
240
241SND_SOC_DAPM_OUTPUT("SPK_LP"),
242SND_SOC_DAPM_OUTPUT("SPK_LN"),
243SND_SOC_DAPM_OUTPUT("HP_L"),
244SND_SOC_DAPM_OUTPUT("HP_R"),
245SND_SOC_DAPM_OUTPUT("SPK_RP"),
246SND_SOC_DAPM_OUTPUT("SPK_RN"),
247SND_SOC_DAPM_OUTPUT("OUT3"),
248};
249
250static 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" },
254
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" },
259
260 { "Right Boost Mixer", "RINPUT1 Switch", "RINPUT1" },
261 { "Right Boost Mixer", "RINPUT2 Switch", "RINPUT2" },
262 { "Right Boost Mixer", "RINPUT3 Switch", "RINPUT3" },
263
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" },
268
269 { "Left ADC", NULL, "Left Input Mixer" },
270 { "Right ADC", NULL, "Right Input Mixer" },
271
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" },
275
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" },
279
280 { "Mono Output Mixer", "Left Switch", "Left Output Mixer" },
281 { "Mono Output Mixer", "Right Switch", "Right Output Mixer" },
282
283 { "LOUT1 PGA", NULL, "Left Output Mixer" },
284 { "ROUT1 PGA", NULL, "Right Output Mixer" },
285
286 { "HP_L", NULL, "LOUT1 PGA" },
287 { "HP_R", NULL, "ROUT1 PGA" },
288
289 { "Left Speaker PGA", NULL, "Left Output Mixer" },
290 { "Right Speaker PGA", NULL, "Right Output Mixer" },
291
292 { "Left Speaker Output", NULL, "Left Speaker PGA" },
293 { "Right Speaker Output", NULL, "Right Speaker PGA" },
294
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" },
299
300 { "OUT3", NULL, "Mono Output Mixer", }
301};
302
303static int wm8960_add_widgets(struct snd_soc_codec *codec)
304{
305 snd_soc_dapm_new_controls(codec, wm8960_dapm_widgets,
306 ARRAY_SIZE(wm8960_dapm_widgets));
307
308 snd_soc_dapm_add_routes(codec, audio_paths, ARRAY_SIZE(audio_paths));
309
310 snd_soc_dapm_new_widgets(codec);
311 return 0;
312}
313
314static int wm8960_set_dai_fmt(struct snd_soc_dai *codec_dai,
315 unsigned int fmt)
316{
317 struct snd_soc_codec *codec = codec_dai->codec;
318 u16 iface = 0;
319
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;
329 }
330
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;
349 }
350
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;
366 }
367
368 /* set iface */
Mark Brown17a52fd2009-07-05 17:24:50 +0100369 snd_soc_write(codec, WM8960_IFACE1, iface);
Mark Brownf2644a22009-04-07 19:20:14 +0100370 return 0;
371}
372
373static int wm8960_hw_params(struct snd_pcm_substream *substream,
374 struct snd_pcm_hw_params *params,
375 struct snd_soc_dai *dai)
376{
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;
Mark Brown17a52fd2009-07-05 17:24:50 +0100380 u16 iface = snd_soc_read(codec, WM8960_IFACE1) & 0xfff3;
Mark Brownf2644a22009-04-07 19:20:14 +0100381
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;
392 }
393
394 /* set iface */
Mark Brown17a52fd2009-07-05 17:24:50 +0100395 snd_soc_write(codec, WM8960_IFACE1, iface);
Mark Brownf2644a22009-04-07 19:20:14 +0100396 return 0;
397}
398
399static int wm8960_mute(struct snd_soc_dai *dai, int mute)
400{
401 struct snd_soc_codec *codec = dai->codec;
Mark Brown17a52fd2009-07-05 17:24:50 +0100402 u16 mute_reg = snd_soc_read(codec, WM8960_DACCTL1) & 0xfff7;
Mark Brownf2644a22009-04-07 19:20:14 +0100403
404 if (mute)
Mark Brown17a52fd2009-07-05 17:24:50 +0100405 snd_soc_write(codec, WM8960_DACCTL1, mute_reg | 0x8);
Mark Brownf2644a22009-04-07 19:20:14 +0100406 else
Mark Brown17a52fd2009-07-05 17:24:50 +0100407 snd_soc_write(codec, WM8960_DACCTL1, mute_reg);
Mark Brownf2644a22009-04-07 19:20:14 +0100408 return 0;
409}
410
411static int wm8960_set_bias_level(struct snd_soc_codec *codec,
412 enum snd_soc_bias_level level)
413{
414 struct wm8960_data *pdata = codec->dev->platform_data;
415 u16 reg;
416
417 switch (level) {
418 case SND_SOC_BIAS_ON:
419 break;
420
421 case SND_SOC_BIAS_PREPARE:
422 /* Set VMID to 2x50k */
Mark Brown17a52fd2009-07-05 17:24:50 +0100423 reg = snd_soc_read(codec, WM8960_POWER1);
Mark Brownf2644a22009-04-07 19:20:14 +0100424 reg &= ~0x180;
425 reg |= 0x80;
Mark Brown17a52fd2009-07-05 17:24:50 +0100426 snd_soc_write(codec, WM8960_POWER1, reg);
Mark Brownf2644a22009-04-07 19:20:14 +0100427 break;
428
429 case SND_SOC_BIAS_STANDBY:
430 if (codec->bias_level == SND_SOC_BIAS_OFF) {
431 /* Enable anti-pop features */
Mark Brown17a52fd2009-07-05 17:24:50 +0100432 snd_soc_write(codec, WM8960_APOP1,
Mark Brownf2644a22009-04-07 19:20:14 +0100433 WM8960_POBCTRL | WM8960_SOFT_ST |
434 WM8960_BUFDCOPEN | WM8960_BUFIOEN);
435
436 /* Discharge HP output */
437 reg = WM8960_DISOP;
438 if (pdata)
439 reg |= pdata->dres << 4;
Mark Brown17a52fd2009-07-05 17:24:50 +0100440 snd_soc_write(codec, WM8960_APOP2, reg);
Mark Brownf2644a22009-04-07 19:20:14 +0100441
442 msleep(400);
443
Mark Brown17a52fd2009-07-05 17:24:50 +0100444 snd_soc_write(codec, WM8960_APOP2, 0);
Mark Brownf2644a22009-04-07 19:20:14 +0100445
446 /* Enable & ramp VMID at 2x50k */
Mark Brown17a52fd2009-07-05 17:24:50 +0100447 reg = snd_soc_read(codec, WM8960_POWER1);
Mark Brownf2644a22009-04-07 19:20:14 +0100448 reg |= 0x80;
Mark Brown17a52fd2009-07-05 17:24:50 +0100449 snd_soc_write(codec, WM8960_POWER1, reg);
Mark Brownf2644a22009-04-07 19:20:14 +0100450 msleep(100);
451
452 /* Enable VREF */
Mark Brown17a52fd2009-07-05 17:24:50 +0100453 snd_soc_write(codec, WM8960_POWER1, reg | WM8960_VREF);
Mark Brownf2644a22009-04-07 19:20:14 +0100454
455 /* Disable anti-pop features */
Mark Brown17a52fd2009-07-05 17:24:50 +0100456 snd_soc_write(codec, WM8960_APOP1, WM8960_BUFIOEN);
Mark Brownf2644a22009-04-07 19:20:14 +0100457 }
458
459 /* Set VMID to 2x250k */
Mark Brown17a52fd2009-07-05 17:24:50 +0100460 reg = snd_soc_read(codec, WM8960_POWER1);
Mark Brownf2644a22009-04-07 19:20:14 +0100461 reg &= ~0x180;
462 reg |= 0x100;
Mark Brown17a52fd2009-07-05 17:24:50 +0100463 snd_soc_write(codec, WM8960_POWER1, reg);
Mark Brownf2644a22009-04-07 19:20:14 +0100464 break;
465
466 case SND_SOC_BIAS_OFF:
467 /* Enable anti-pop features */
Mark Brown17a52fd2009-07-05 17:24:50 +0100468 snd_soc_write(codec, WM8960_APOP1,
Mark Brownf2644a22009-04-07 19:20:14 +0100469 WM8960_POBCTRL | WM8960_SOFT_ST |
470 WM8960_BUFDCOPEN | WM8960_BUFIOEN);
471
472 /* Disable VMID and VREF, let them discharge */
Mark Brown17a52fd2009-07-05 17:24:50 +0100473 snd_soc_write(codec, WM8960_POWER1, 0);
Mark Brownf2644a22009-04-07 19:20:14 +0100474 msleep(600);
475
Mark Brown17a52fd2009-07-05 17:24:50 +0100476 snd_soc_write(codec, WM8960_APOP1, 0);
Mark Brownf2644a22009-04-07 19:20:14 +0100477 break;
478 }
479
480 codec->bias_level = level;
481
482 return 0;
483}
484
485/* PLL divisors */
486struct _pll_div {
487 u32 pre_div:1;
488 u32 n:4;
489 u32 k:24;
490};
491
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)
495
496static int pll_factors(unsigned int source, unsigned int target,
497 struct _pll_div *pll_div)
498{
499 unsigned long long Kpart;
500 unsigned int K, Ndiv, Nmod;
501
502 pr_debug("WM8960 PLL: setting %dHz->%dHz\n", source, target);
503
504 /* Scale up target to PLL operating frequency */
505 target *= 4;
506
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;
514
515 if ((Ndiv < 6) || (Ndiv > 12)) {
516 pr_err("WM8960 PLL: Unsupported N=%d\n", Ndiv);
517 return -EINVAL;
518 }
519
520 pll_div->n = Ndiv;
521 Nmod = target % source;
522 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
523
524 do_div(Kpart, source);
525
526 K = Kpart & 0xFFFFFFFF;
527
528 /* Check if we need to round */
529 if ((K % 10) >= 5)
530 K += 5;
531
532 /* Move down to proper range now rounding is done */
533 K /= 10;
534
535 pll_div->k = K;
536
537 pr_debug("WM8960 PLL: N=%x K=%x pre_div=%d\n",
538 pll_div->n, pll_div->k, pll_div->pre_div);
539
540 return 0;
541}
542
Mark Brown85488032009-09-05 18:52:16 +0100543static int wm8960_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
544 int source, unsigned int freq_in, unsigned int freq_out)
Mark Brownf2644a22009-04-07 19:20:14 +0100545{
546 struct snd_soc_codec *codec = codec_dai->codec;
547 u16 reg;
548 static struct _pll_div pll_div;
549 int ret;
550
551 if (freq_in && freq_out) {
552 ret = pll_factors(freq_in, freq_out, &pll_div);
553 if (ret != 0)
554 return ret;
555 }
556
557 /* Disable the PLL: even if we are changing the frequency the
558 * PLL needs to be disabled while we do so. */
Mark Brown17a52fd2009-07-05 17:24:50 +0100559 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);
Mark Brownf2644a22009-04-07 19:20:14 +0100563
564 if (!freq_in || !freq_out)
565 return 0;
566
Mark Brown17a52fd2009-07-05 17:24:50 +0100567 reg = snd_soc_read(codec, WM8960_PLL1) & ~0x3f;
Mark Brownf2644a22009-04-07 19:20:14 +0100568 reg |= pll_div.pre_div << 4;
569 reg |= pll_div.n;
570
571 if (pll_div.k) {
572 reg |= 0x20;
573
Mark Brown17a52fd2009-07-05 17:24:50 +0100574 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);
Mark Brownf2644a22009-04-07 19:20:14 +0100577 }
Mark Brown17a52fd2009-07-05 17:24:50 +0100578 snd_soc_write(codec, WM8960_PLL1, reg);
Mark Brownf2644a22009-04-07 19:20:14 +0100579
580 /* Turn it on */
Mark Brown17a52fd2009-07-05 17:24:50 +0100581 snd_soc_write(codec, WM8960_POWER2,
582 snd_soc_read(codec, WM8960_POWER2) | 1);
Mark Brownf2644a22009-04-07 19:20:14 +0100583 msleep(250);
Mark Brown17a52fd2009-07-05 17:24:50 +0100584 snd_soc_write(codec, WM8960_CLOCK1,
585 snd_soc_read(codec, WM8960_CLOCK1) | 1);
Mark Brownf2644a22009-04-07 19:20:14 +0100586
587 return 0;
588}
589
590static int wm8960_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
591 int div_id, int div)
592{
593 struct snd_soc_codec *codec = codec_dai->codec;
594 u16 reg;
595
596 switch (div_id) {
597 case WM8960_SYSCLKSEL:
Mark Brown17a52fd2009-07-05 17:24:50 +0100598 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1fe;
599 snd_soc_write(codec, WM8960_CLOCK1, reg | div);
Mark Brownf2644a22009-04-07 19:20:14 +0100600 break;
601 case WM8960_SYSCLKDIV:
Mark Brown17a52fd2009-07-05 17:24:50 +0100602 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1f9;
603 snd_soc_write(codec, WM8960_CLOCK1, reg | div);
Mark Brownf2644a22009-04-07 19:20:14 +0100604 break;
605 case WM8960_DACDIV:
Mark Brown17a52fd2009-07-05 17:24:50 +0100606 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1c7;
607 snd_soc_write(codec, WM8960_CLOCK1, reg | div);
Mark Brownf2644a22009-04-07 19:20:14 +0100608 break;
609 case WM8960_OPCLKDIV:
Mark Brown17a52fd2009-07-05 17:24:50 +0100610 reg = snd_soc_read(codec, WM8960_PLL1) & 0x03f;
611 snd_soc_write(codec, WM8960_PLL1, reg | div);
Mark Brownf2644a22009-04-07 19:20:14 +0100612 break;
613 case WM8960_DCLKDIV:
Mark Brown17a52fd2009-07-05 17:24:50 +0100614 reg = snd_soc_read(codec, WM8960_CLOCK2) & 0x03f;
615 snd_soc_write(codec, WM8960_CLOCK2, reg | div);
Mark Brownf2644a22009-04-07 19:20:14 +0100616 break;
617 case WM8960_TOCLKSEL:
Mark Brown17a52fd2009-07-05 17:24:50 +0100618 reg = snd_soc_read(codec, WM8960_ADDCTL1) & 0x1fd;
619 snd_soc_write(codec, WM8960_ADDCTL1, reg | div);
Mark Brownf2644a22009-04-07 19:20:14 +0100620 break;
621 default:
622 return -EINVAL;
623 }
624
625 return 0;
626}
627
628#define WM8960_RATES SNDRV_PCM_RATE_8000_48000
629
630#define WM8960_FORMATS \
631 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
632 SNDRV_PCM_FMTBIT_S24_LE)
633
634static 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,
640};
641
642struct 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,
658};
659EXPORT_SYMBOL_GPL(wm8960_dai);
660
661static int wm8960_suspend(struct platform_device *pdev, pm_message_t state)
662{
663 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
664 struct snd_soc_codec *codec = socdev->card->codec;
665
666 wm8960_set_bias_level(codec, SND_SOC_BIAS_OFF);
667 return 0;
668}
669
670static int wm8960_resume(struct platform_device *pdev)
671{
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;
677
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);
683 }
684
685 wm8960_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
686 wm8960_set_bias_level(codec, codec->suspend_bias_level);
687 return 0;
688}
689
690static struct snd_soc_codec *wm8960_codec;
691
692static int wm8960_probe(struct platform_device *pdev)
693{
694 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
695 struct snd_soc_codec *codec;
696 int ret = 0;
697
698 if (wm8960_codec == NULL) {
699 dev_err(&pdev->dev, "Codec device not registered\n");
700 return -ENODEV;
701 }
702
703 socdev->card->codec = wm8960_codec;
704 codec = wm8960_codec;
705
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;
711 }
712
713 snd_soc_add_controls(codec, wm8960_snd_controls,
714 ARRAY_SIZE(wm8960_snd_controls));
715 wm8960_add_widgets(codec);
Mark Brownf2644a22009-04-07 19:20:14 +0100716
717 return ret;
718
Mark Brownf2644a22009-04-07 19:20:14 +0100719pcm_err:
720 return ret;
721}
722
723/* power down chip */
724static int wm8960_remove(struct platform_device *pdev)
725{
726 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
727
728 snd_soc_free_pcms(socdev);
729 snd_soc_dapm_free(socdev);
730
731 return 0;
732}
733
734struct snd_soc_codec_device soc_codec_dev_wm8960 = {
735 .probe = wm8960_probe,
736 .remove = wm8960_remove,
737 .suspend = wm8960_suspend,
738 .resume = wm8960_resume,
739};
740EXPORT_SYMBOL_GPL(soc_codec_dev_wm8960);
741
Mark Brown7084a422009-07-10 22:24:27 +0100742static int wm8960_register(struct wm8960_priv *wm8960,
743 enum snd_soc_control_type control)
Mark Brownf2644a22009-04-07 19:20:14 +0100744{
745 struct wm8960_data *pdata = wm8960->codec.dev->platform_data;
746 struct snd_soc_codec *codec = &wm8960->codec;
747 int ret;
748 u16 reg;
749
750 if (wm8960_codec) {
751 dev_err(codec->dev, "Another WM8960 is registered\n");
Mark Brown1a014172009-07-05 15:47:03 +0100752 ret = -EINVAL;
753 goto err;
Mark Brownf2644a22009-04-07 19:20:14 +0100754 }
755
756 if (!pdata) {
757 dev_warn(codec->dev, "No platform data supplied\n");
758 } else {
759 if (pdata->dres > WM8960_DRES_MAX) {
760 dev_err(codec->dev, "Invalid DRES: %d\n", pdata->dres);
761 pdata->dres = 0;
762 }
763 }
764
765 mutex_init(&codec->mutex);
766 INIT_LIST_HEAD(&codec->dapm_widgets);
767 INIT_LIST_HEAD(&codec->dapm_paths);
768
769 codec->private_data = wm8960;
770 codec->name = "WM8960";
771 codec->owner = THIS_MODULE;
Mark Brownf2644a22009-04-07 19:20:14 +0100772 codec->bias_level = SND_SOC_BIAS_OFF;
773 codec->set_bias_level = wm8960_set_bias_level;
774 codec->dai = &wm8960_dai;
775 codec->num_dai = 1;
776 codec->reg_cache_size = WM8960_CACHEREGNUM;
777 codec->reg_cache = &wm8960->reg_cache;
778
779 memcpy(codec->reg_cache, wm8960_reg, sizeof(wm8960_reg));
780
Mark Brown7084a422009-07-10 22:24:27 +0100781 ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
Mark Brown17a52fd2009-07-05 17:24:50 +0100782 if (ret < 0) {
783 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
784 goto err;
785 }
786
Mark Brownf2644a22009-04-07 19:20:14 +0100787 ret = wm8960_reset(codec);
788 if (ret < 0) {
789 dev_err(codec->dev, "Failed to issue reset\n");
Mark Brown1a014172009-07-05 15:47:03 +0100790 goto err;
Mark Brownf2644a22009-04-07 19:20:14 +0100791 }
792
793 wm8960_dai.dev = codec->dev;
794
795 wm8960_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
796
797 /* Latch the update bits */
Mark Brown17a52fd2009-07-05 17:24:50 +0100798 reg = snd_soc_read(codec, WM8960_LINVOL);
799 snd_soc_write(codec, WM8960_LINVOL, reg | 0x100);
800 reg = snd_soc_read(codec, WM8960_RINVOL);
801 snd_soc_write(codec, WM8960_RINVOL, reg | 0x100);
802 reg = snd_soc_read(codec, WM8960_LADC);
803 snd_soc_write(codec, WM8960_LADC, reg | 0x100);
804 reg = snd_soc_read(codec, WM8960_RADC);
805 snd_soc_write(codec, WM8960_RADC, reg | 0x100);
806 reg = snd_soc_read(codec, WM8960_LDAC);
807 snd_soc_write(codec, WM8960_LDAC, reg | 0x100);
808 reg = snd_soc_read(codec, WM8960_RDAC);
809 snd_soc_write(codec, WM8960_RDAC, reg | 0x100);
810 reg = snd_soc_read(codec, WM8960_LOUT1);
811 snd_soc_write(codec, WM8960_LOUT1, reg | 0x100);
812 reg = snd_soc_read(codec, WM8960_ROUT1);
813 snd_soc_write(codec, WM8960_ROUT1, reg | 0x100);
814 reg = snd_soc_read(codec, WM8960_LOUT2);
815 snd_soc_write(codec, WM8960_LOUT2, reg | 0x100);
816 reg = snd_soc_read(codec, WM8960_ROUT2);
817 snd_soc_write(codec, WM8960_ROUT2, reg | 0x100);
Mark Brownf2644a22009-04-07 19:20:14 +0100818
819 wm8960_codec = codec;
820
821 ret = snd_soc_register_codec(codec);
822 if (ret != 0) {
823 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
Mark Brown1a014172009-07-05 15:47:03 +0100824 goto err;
Mark Brownf2644a22009-04-07 19:20:14 +0100825 }
826
827 ret = snd_soc_register_dai(&wm8960_dai);
828 if (ret != 0) {
829 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
Mark Brown1a014172009-07-05 15:47:03 +0100830 goto err_codec;
Mark Brownf2644a22009-04-07 19:20:14 +0100831 }
832
833 return 0;
Mark Brown1a014172009-07-05 15:47:03 +0100834
835err_codec:
836 snd_soc_unregister_codec(codec);
837err:
838 kfree(wm8960);
839 return ret;
Mark Brownf2644a22009-04-07 19:20:14 +0100840}
841
842static void wm8960_unregister(struct wm8960_priv *wm8960)
843{
844 wm8960_set_bias_level(&wm8960->codec, SND_SOC_BIAS_OFF);
845 snd_soc_unregister_dai(&wm8960_dai);
846 snd_soc_unregister_codec(&wm8960->codec);
847 kfree(wm8960);
848 wm8960_codec = NULL;
849}
850
851static __devinit int wm8960_i2c_probe(struct i2c_client *i2c,
852 const struct i2c_device_id *id)
853{
854 struct wm8960_priv *wm8960;
855 struct snd_soc_codec *codec;
856
857 wm8960 = kzalloc(sizeof(struct wm8960_priv), GFP_KERNEL);
858 if (wm8960 == NULL)
859 return -ENOMEM;
860
861 codec = &wm8960->codec;
Mark Brownf2644a22009-04-07 19:20:14 +0100862
863 i2c_set_clientdata(i2c, wm8960);
864 codec->control_data = i2c;
865
866 codec->dev = &i2c->dev;
867
Mark Brown7084a422009-07-10 22:24:27 +0100868 return wm8960_register(wm8960, SND_SOC_I2C);
Mark Brownf2644a22009-04-07 19:20:14 +0100869}
870
871static __devexit int wm8960_i2c_remove(struct i2c_client *client)
872{
873 struct wm8960_priv *wm8960 = i2c_get_clientdata(client);
874 wm8960_unregister(wm8960);
875 return 0;
876}
877
878static const struct i2c_device_id wm8960_i2c_id[] = {
879 { "wm8960", 0 },
880 { }
881};
882MODULE_DEVICE_TABLE(i2c, wm8960_i2c_id);
883
884static struct i2c_driver wm8960_i2c_driver = {
885 .driver = {
886 .name = "WM8960 I2C Codec",
887 .owner = THIS_MODULE,
888 },
889 .probe = wm8960_i2c_probe,
890 .remove = __devexit_p(wm8960_i2c_remove),
Mark Brownf2644a22009-04-07 19:20:14 +0100891 .id_table = wm8960_i2c_id,
892};
893
894static int __init wm8960_modinit(void)
895{
896 int ret;
897
898 ret = i2c_add_driver(&wm8960_i2c_driver);
899 if (ret != 0) {
900 printk(KERN_ERR "Failed to register WM8960 I2C driver: %d\n",
901 ret);
902 }
903
904 return ret;
905}
906module_init(wm8960_modinit);
907
908static void __exit wm8960_exit(void)
909{
910 i2c_del_driver(&wm8960_i2c_driver);
911}
912module_exit(wm8960_exit);
913
914
915MODULE_DESCRIPTION("ASoC WM8960 driver");
916MODULE_AUTHOR("Liam Girdwood");
917MODULE_LICENSE("GPL");