xfs: validate acl count
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / soc / codecs / wm8971.c
blobd66efb0546eadf3846aa81adaff2003bd8675dcf
1 /*
2 * wm8971.c -- WM8971 ALSA SoC Audio driver
4 * Copyright 2005 Lab126, Inc.
6 * Author: Kenneth Kiraly <kiraly@lab126.com>
8 * Based on wm8753.c by Liam Girdwood
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/init.h>
19 #include <linux/delay.h>
20 #include <linux/pm.h>
21 #include <linux/i2c.h>
22 #include <linux/platform_device.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/soc-dapm.h>
28 #include <sound/initval.h>
30 #include "wm8971.h"
32 #define WM8971_VERSION "0.9"
34 #define WM8971_REG_COUNT 43
36 static struct workqueue_struct *wm8971_workq = NULL;
38 /* codec private data */
39 struct wm8971_priv {
40 unsigned int sysclk;
44 * wm8971 register cache
45 * We can't read the WM8971 register space when we
46 * are using 2 wire for device control, so we cache them instead.
48 static const u16 wm8971_reg[] = {
49 0x0097, 0x0097, 0x0079, 0x0079, /* 0 */
50 0x0000, 0x0008, 0x0000, 0x000a, /* 4 */
51 0x0000, 0x0000, 0x00ff, 0x00ff, /* 8 */
52 0x000f, 0x000f, 0x0000, 0x0000, /* 12 */
53 0x0000, 0x007b, 0x0000, 0x0032, /* 16 */
54 0x0000, 0x00c3, 0x00c3, 0x00c0, /* 20 */
55 0x0000, 0x0000, 0x0000, 0x0000, /* 24 */
56 0x0000, 0x0000, 0x0000, 0x0000, /* 28 */
57 0x0000, 0x0000, 0x0050, 0x0050, /* 32 */
58 0x0050, 0x0050, 0x0050, 0x0050, /* 36 */
59 0x0079, 0x0079, 0x0079, /* 40 */
62 #define wm8971_reset(c) snd_soc_write(c, WM8971_RESET, 0)
64 /* WM8971 Controls */
65 static const char *wm8971_bass[] = { "Linear Control", "Adaptive Boost" };
66 static const char *wm8971_bass_filter[] = { "130Hz @ 48kHz",
67 "200Hz @ 48kHz" };
68 static const char *wm8971_treble[] = { "8kHz", "4kHz" };
69 static const char *wm8971_alc_func[] = { "Off", "Right", "Left", "Stereo" };
70 static const char *wm8971_ng_type[] = { "Constant PGA Gain",
71 "Mute ADC Output" };
72 static const char *wm8971_deemp[] = { "None", "32kHz", "44.1kHz", "48kHz" };
73 static const char *wm8971_mono_mux[] = {"Stereo", "Mono (Left)",
74 "Mono (Right)", "Digital Mono"};
75 static const char *wm8971_dac_phase[] = { "Non Inverted", "Inverted" };
76 static const char *wm8971_lline_mux[] = {"Line", "NC", "NC", "PGA",
77 "Differential"};
78 static const char *wm8971_rline_mux[] = {"Line", "Mic", "NC", "PGA",
79 "Differential"};
80 static const char *wm8971_lpga_sel[] = {"Line", "NC", "NC", "Differential"};
81 static const char *wm8971_rpga_sel[] = {"Line", "Mic", "NC", "Differential"};
82 static const char *wm8971_adcpol[] = {"Normal", "L Invert", "R Invert",
83 "L + R Invert"};
85 static const struct soc_enum wm8971_enum[] = {
86 SOC_ENUM_SINGLE(WM8971_BASS, 7, 2, wm8971_bass), /* 0 */
87 SOC_ENUM_SINGLE(WM8971_BASS, 6, 2, wm8971_bass_filter),
88 SOC_ENUM_SINGLE(WM8971_TREBLE, 6, 2, wm8971_treble),
89 SOC_ENUM_SINGLE(WM8971_ALC1, 7, 4, wm8971_alc_func),
90 SOC_ENUM_SINGLE(WM8971_NGATE, 1, 2, wm8971_ng_type), /* 4 */
91 SOC_ENUM_SINGLE(WM8971_ADCDAC, 1, 4, wm8971_deemp),
92 SOC_ENUM_SINGLE(WM8971_ADCTL1, 4, 4, wm8971_mono_mux),
93 SOC_ENUM_SINGLE(WM8971_ADCTL1, 1, 2, wm8971_dac_phase),
94 SOC_ENUM_SINGLE(WM8971_LOUTM1, 0, 5, wm8971_lline_mux), /* 8 */
95 SOC_ENUM_SINGLE(WM8971_ROUTM1, 0, 5, wm8971_rline_mux),
96 SOC_ENUM_SINGLE(WM8971_LADCIN, 6, 4, wm8971_lpga_sel),
97 SOC_ENUM_SINGLE(WM8971_RADCIN, 6, 4, wm8971_rpga_sel),
98 SOC_ENUM_SINGLE(WM8971_ADCDAC, 5, 4, wm8971_adcpol), /* 12 */
99 SOC_ENUM_SINGLE(WM8971_ADCIN, 6, 4, wm8971_mono_mux),
102 static const struct snd_kcontrol_new wm8971_snd_controls[] = {
103 SOC_DOUBLE_R("Capture Volume", WM8971_LINVOL, WM8971_RINVOL, 0, 63, 0),
104 SOC_DOUBLE_R("Capture ZC Switch", WM8971_LINVOL, WM8971_RINVOL,
105 6, 1, 0),
106 SOC_DOUBLE_R("Capture Switch", WM8971_LINVOL, WM8971_RINVOL, 7, 1, 1),
108 SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8971_LOUT1V,
109 WM8971_ROUT1V, 7, 1, 0),
110 SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8971_LOUT2V,
111 WM8971_ROUT2V, 7, 1, 0),
112 SOC_SINGLE("Mono Playback ZC Switch", WM8971_MOUTV, 7, 1, 0),
114 SOC_DOUBLE_R("PCM Volume", WM8971_LDAC, WM8971_RDAC, 0, 255, 0),
116 SOC_DOUBLE_R("Bypass Left Playback Volume", WM8971_LOUTM1,
117 WM8971_LOUTM2, 4, 7, 1),
118 SOC_DOUBLE_R("Bypass Right Playback Volume", WM8971_ROUTM1,
119 WM8971_ROUTM2, 4, 7, 1),
120 SOC_DOUBLE_R("Bypass Mono Playback Volume", WM8971_MOUTM1,
121 WM8971_MOUTM2, 4, 7, 1),
123 SOC_DOUBLE_R("Headphone Playback Volume", WM8971_LOUT1V,
124 WM8971_ROUT1V, 0, 127, 0),
125 SOC_DOUBLE_R("Speaker Playback Volume", WM8971_LOUT2V,
126 WM8971_ROUT2V, 0, 127, 0),
128 SOC_ENUM("Bass Boost", wm8971_enum[0]),
129 SOC_ENUM("Bass Filter", wm8971_enum[1]),
130 SOC_SINGLE("Bass Volume", WM8971_BASS, 0, 7, 1),
132 SOC_SINGLE("Treble Volume", WM8971_TREBLE, 0, 7, 0),
133 SOC_ENUM("Treble Cut-off", wm8971_enum[2]),
135 SOC_SINGLE("Capture Filter Switch", WM8971_ADCDAC, 0, 1, 1),
137 SOC_SINGLE("ALC Target Volume", WM8971_ALC1, 0, 7, 0),
138 SOC_SINGLE("ALC Max Volume", WM8971_ALC1, 4, 7, 0),
140 SOC_SINGLE("ALC Capture Target Volume", WM8971_ALC1, 0, 7, 0),
141 SOC_SINGLE("ALC Capture Max Volume", WM8971_ALC1, 4, 7, 0),
142 SOC_ENUM("ALC Capture Function", wm8971_enum[3]),
143 SOC_SINGLE("ALC Capture ZC Switch", WM8971_ALC2, 7, 1, 0),
144 SOC_SINGLE("ALC Capture Hold Time", WM8971_ALC2, 0, 15, 0),
145 SOC_SINGLE("ALC Capture Decay Time", WM8971_ALC3, 4, 15, 0),
146 SOC_SINGLE("ALC Capture Attack Time", WM8971_ALC3, 0, 15, 0),
147 SOC_SINGLE("ALC Capture NG Threshold", WM8971_NGATE, 3, 31, 0),
148 SOC_ENUM("ALC Capture NG Type", wm8971_enum[4]),
149 SOC_SINGLE("ALC Capture NG Switch", WM8971_NGATE, 0, 1, 0),
151 SOC_SINGLE("Capture 6dB Attenuate", WM8971_ADCDAC, 8, 1, 0),
152 SOC_SINGLE("Playback 6dB Attenuate", WM8971_ADCDAC, 7, 1, 0),
154 SOC_ENUM("Playback De-emphasis", wm8971_enum[5]),
155 SOC_ENUM("Playback Function", wm8971_enum[6]),
156 SOC_ENUM("Playback Phase", wm8971_enum[7]),
158 SOC_DOUBLE_R("Mic Boost", WM8971_LADCIN, WM8971_RADCIN, 4, 3, 0),
162 * DAPM Controls
165 /* Left Mixer */
166 static const struct snd_kcontrol_new wm8971_left_mixer_controls[] = {
167 SOC_DAPM_SINGLE("Playback Switch", WM8971_LOUTM1, 8, 1, 0),
168 SOC_DAPM_SINGLE("Left Bypass Switch", WM8971_LOUTM1, 7, 1, 0),
169 SOC_DAPM_SINGLE("Right Playback Switch", WM8971_LOUTM2, 8, 1, 0),
170 SOC_DAPM_SINGLE("Right Bypass Switch", WM8971_LOUTM2, 7, 1, 0),
173 /* Right Mixer */
174 static const struct snd_kcontrol_new wm8971_right_mixer_controls[] = {
175 SOC_DAPM_SINGLE("Left Playback Switch", WM8971_ROUTM1, 8, 1, 0),
176 SOC_DAPM_SINGLE("Left Bypass Switch", WM8971_ROUTM1, 7, 1, 0),
177 SOC_DAPM_SINGLE("Playback Switch", WM8971_ROUTM2, 8, 1, 0),
178 SOC_DAPM_SINGLE("Right Bypass Switch", WM8971_ROUTM2, 7, 1, 0),
181 /* Mono Mixer */
182 static const struct snd_kcontrol_new wm8971_mono_mixer_controls[] = {
183 SOC_DAPM_SINGLE("Left Playback Switch", WM8971_MOUTM1, 8, 1, 0),
184 SOC_DAPM_SINGLE("Left Bypass Switch", WM8971_MOUTM1, 7, 1, 0),
185 SOC_DAPM_SINGLE("Right Playback Switch", WM8971_MOUTM2, 8, 1, 0),
186 SOC_DAPM_SINGLE("Right Bypass Switch", WM8971_MOUTM2, 7, 1, 0),
189 /* Left Line Mux */
190 static const struct snd_kcontrol_new wm8971_left_line_controls =
191 SOC_DAPM_ENUM("Route", wm8971_enum[8]);
193 /* Right Line Mux */
194 static const struct snd_kcontrol_new wm8971_right_line_controls =
195 SOC_DAPM_ENUM("Route", wm8971_enum[9]);
197 /* Left PGA Mux */
198 static const struct snd_kcontrol_new wm8971_left_pga_controls =
199 SOC_DAPM_ENUM("Route", wm8971_enum[10]);
201 /* Right PGA Mux */
202 static const struct snd_kcontrol_new wm8971_right_pga_controls =
203 SOC_DAPM_ENUM("Route", wm8971_enum[11]);
205 /* Mono ADC Mux */
206 static const struct snd_kcontrol_new wm8971_monomux_controls =
207 SOC_DAPM_ENUM("Route", wm8971_enum[13]);
209 static const struct snd_soc_dapm_widget wm8971_dapm_widgets[] = {
210 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
211 &wm8971_left_mixer_controls[0],
212 ARRAY_SIZE(wm8971_left_mixer_controls)),
213 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
214 &wm8971_right_mixer_controls[0],
215 ARRAY_SIZE(wm8971_right_mixer_controls)),
216 SND_SOC_DAPM_MIXER("Mono Mixer", WM8971_PWR2, 2, 0,
217 &wm8971_mono_mixer_controls[0],
218 ARRAY_SIZE(wm8971_mono_mixer_controls)),
220 SND_SOC_DAPM_PGA("Right Out 2", WM8971_PWR2, 3, 0, NULL, 0),
221 SND_SOC_DAPM_PGA("Left Out 2", WM8971_PWR2, 4, 0, NULL, 0),
222 SND_SOC_DAPM_PGA("Right Out 1", WM8971_PWR2, 5, 0, NULL, 0),
223 SND_SOC_DAPM_PGA("Left Out 1", WM8971_PWR2, 6, 0, NULL, 0),
224 SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8971_PWR2, 7, 0),
225 SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8971_PWR2, 8, 0),
226 SND_SOC_DAPM_PGA("Mono Out 1", WM8971_PWR2, 2, 0, NULL, 0),
228 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8971_PWR1, 1, 0),
229 SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8971_PWR1, 2, 0),
230 SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8971_PWR1, 3, 0),
232 SND_SOC_DAPM_MUX("Left PGA Mux", WM8971_PWR1, 5, 0,
233 &wm8971_left_pga_controls),
234 SND_SOC_DAPM_MUX("Right PGA Mux", WM8971_PWR1, 4, 0,
235 &wm8971_right_pga_controls),
236 SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
237 &wm8971_left_line_controls),
238 SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
239 &wm8971_right_line_controls),
241 SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
242 &wm8971_monomux_controls),
243 SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
244 &wm8971_monomux_controls),
246 SND_SOC_DAPM_OUTPUT("LOUT1"),
247 SND_SOC_DAPM_OUTPUT("ROUT1"),
248 SND_SOC_DAPM_OUTPUT("LOUT2"),
249 SND_SOC_DAPM_OUTPUT("ROUT2"),
250 SND_SOC_DAPM_OUTPUT("MONO"),
252 SND_SOC_DAPM_INPUT("LINPUT1"),
253 SND_SOC_DAPM_INPUT("RINPUT1"),
254 SND_SOC_DAPM_INPUT("MIC"),
257 static const struct snd_soc_dapm_route audio_map[] = {
258 /* left mixer */
259 {"Left Mixer", "Playback Switch", "Left DAC"},
260 {"Left Mixer", "Left Bypass Switch", "Left Line Mux"},
261 {"Left Mixer", "Right Playback Switch", "Right DAC"},
262 {"Left Mixer", "Right Bypass Switch", "Right Line Mux"},
264 /* right mixer */
265 {"Right Mixer", "Left Playback Switch", "Left DAC"},
266 {"Right Mixer", "Left Bypass Switch", "Left Line Mux"},
267 {"Right Mixer", "Playback Switch", "Right DAC"},
268 {"Right Mixer", "Right Bypass Switch", "Right Line Mux"},
270 /* left out 1 */
271 {"Left Out 1", NULL, "Left Mixer"},
272 {"LOUT1", NULL, "Left Out 1"},
274 /* left out 2 */
275 {"Left Out 2", NULL, "Left Mixer"},
276 {"LOUT2", NULL, "Left Out 2"},
278 /* right out 1 */
279 {"Right Out 1", NULL, "Right Mixer"},
280 {"ROUT1", NULL, "Right Out 1"},
282 /* right out 2 */
283 {"Right Out 2", NULL, "Right Mixer"},
284 {"ROUT2", NULL, "Right Out 2"},
286 /* mono mixer */
287 {"Mono Mixer", "Left Playback Switch", "Left DAC"},
288 {"Mono Mixer", "Left Bypass Switch", "Left Line Mux"},
289 {"Mono Mixer", "Right Playback Switch", "Right DAC"},
290 {"Mono Mixer", "Right Bypass Switch", "Right Line Mux"},
292 /* mono out */
293 {"Mono Out", NULL, "Mono Mixer"},
294 {"MONO1", NULL, "Mono Out"},
296 /* Left Line Mux */
297 {"Left Line Mux", "Line", "LINPUT1"},
298 {"Left Line Mux", "PGA", "Left PGA Mux"},
299 {"Left Line Mux", "Differential", "Differential Mux"},
301 /* Right Line Mux */
302 {"Right Line Mux", "Line", "RINPUT1"},
303 {"Right Line Mux", "Mic", "MIC"},
304 {"Right Line Mux", "PGA", "Right PGA Mux"},
305 {"Right Line Mux", "Differential", "Differential Mux"},
307 /* Left PGA Mux */
308 {"Left PGA Mux", "Line", "LINPUT1"},
309 {"Left PGA Mux", "Differential", "Differential Mux"},
311 /* Right PGA Mux */
312 {"Right PGA Mux", "Line", "RINPUT1"},
313 {"Right PGA Mux", "Differential", "Differential Mux"},
315 /* Differential Mux */
316 {"Differential Mux", "Line", "LINPUT1"},
317 {"Differential Mux", "Line", "RINPUT1"},
319 /* Left ADC Mux */
320 {"Left ADC Mux", "Stereo", "Left PGA Mux"},
321 {"Left ADC Mux", "Mono (Left)", "Left PGA Mux"},
322 {"Left ADC Mux", "Digital Mono", "Left PGA Mux"},
324 /* Right ADC Mux */
325 {"Right ADC Mux", "Stereo", "Right PGA Mux"},
326 {"Right ADC Mux", "Mono (Right)", "Right PGA Mux"},
327 {"Right ADC Mux", "Digital Mono", "Right PGA Mux"},
329 /* ADC */
330 {"Left ADC", NULL, "Left ADC Mux"},
331 {"Right ADC", NULL, "Right ADC Mux"},
334 static int wm8971_add_widgets(struct snd_soc_codec *codec)
336 snd_soc_dapm_new_controls(codec, wm8971_dapm_widgets,
337 ARRAY_SIZE(wm8971_dapm_widgets));
339 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
341 snd_soc_dapm_new_widgets(codec);
343 return 0;
346 struct _coeff_div {
347 u32 mclk;
348 u32 rate;
349 u16 fs;
350 u8 sr:5;
351 u8 usb:1;
354 /* codec hifi mclk clock divider coefficients */
355 static const struct _coeff_div coeff_div[] = {
356 /* 8k */
357 {12288000, 8000, 1536, 0x6, 0x0},
358 {11289600, 8000, 1408, 0x16, 0x0},
359 {18432000, 8000, 2304, 0x7, 0x0},
360 {16934400, 8000, 2112, 0x17, 0x0},
361 {12000000, 8000, 1500, 0x6, 0x1},
363 /* 11.025k */
364 {11289600, 11025, 1024, 0x18, 0x0},
365 {16934400, 11025, 1536, 0x19, 0x0},
366 {12000000, 11025, 1088, 0x19, 0x1},
368 /* 16k */
369 {12288000, 16000, 768, 0xa, 0x0},
370 {18432000, 16000, 1152, 0xb, 0x0},
371 {12000000, 16000, 750, 0xa, 0x1},
373 /* 22.05k */
374 {11289600, 22050, 512, 0x1a, 0x0},
375 {16934400, 22050, 768, 0x1b, 0x0},
376 {12000000, 22050, 544, 0x1b, 0x1},
378 /* 32k */
379 {12288000, 32000, 384, 0xc, 0x0},
380 {18432000, 32000, 576, 0xd, 0x0},
381 {12000000, 32000, 375, 0xa, 0x1},
383 /* 44.1k */
384 {11289600, 44100, 256, 0x10, 0x0},
385 {16934400, 44100, 384, 0x11, 0x0},
386 {12000000, 44100, 272, 0x11, 0x1},
388 /* 48k */
389 {12288000, 48000, 256, 0x0, 0x0},
390 {18432000, 48000, 384, 0x1, 0x0},
391 {12000000, 48000, 250, 0x0, 0x1},
393 /* 88.2k */
394 {11289600, 88200, 128, 0x1e, 0x0},
395 {16934400, 88200, 192, 0x1f, 0x0},
396 {12000000, 88200, 136, 0x1f, 0x1},
398 /* 96k */
399 {12288000, 96000, 128, 0xe, 0x0},
400 {18432000, 96000, 192, 0xf, 0x0},
401 {12000000, 96000, 125, 0xe, 0x1},
404 static int get_coeff(int mclk, int rate)
406 int i;
408 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
409 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
410 return i;
412 return -EINVAL;
415 static int wm8971_set_dai_sysclk(struct snd_soc_dai *codec_dai,
416 int clk_id, unsigned int freq, int dir)
418 struct snd_soc_codec *codec = codec_dai->codec;
419 struct wm8971_priv *wm8971 = codec->private_data;
421 switch (freq) {
422 case 11289600:
423 case 12000000:
424 case 12288000:
425 case 16934400:
426 case 18432000:
427 wm8971->sysclk = freq;
428 return 0;
430 return -EINVAL;
433 static int wm8971_set_dai_fmt(struct snd_soc_dai *codec_dai,
434 unsigned int fmt)
436 struct snd_soc_codec *codec = codec_dai->codec;
437 u16 iface = 0;
439 /* set master/slave audio interface */
440 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
441 case SND_SOC_DAIFMT_CBM_CFM:
442 iface = 0x0040;
443 break;
444 case SND_SOC_DAIFMT_CBS_CFS:
445 break;
446 default:
447 return -EINVAL;
450 /* interface format */
451 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
452 case SND_SOC_DAIFMT_I2S:
453 iface |= 0x0002;
454 break;
455 case SND_SOC_DAIFMT_RIGHT_J:
456 break;
457 case SND_SOC_DAIFMT_LEFT_J:
458 iface |= 0x0001;
459 break;
460 case SND_SOC_DAIFMT_DSP_A:
461 iface |= 0x0003;
462 break;
463 case SND_SOC_DAIFMT_DSP_B:
464 iface |= 0x0013;
465 break;
466 default:
467 return -EINVAL;
470 /* clock inversion */
471 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
472 case SND_SOC_DAIFMT_NB_NF:
473 break;
474 case SND_SOC_DAIFMT_IB_IF:
475 iface |= 0x0090;
476 break;
477 case SND_SOC_DAIFMT_IB_NF:
478 iface |= 0x0080;
479 break;
480 case SND_SOC_DAIFMT_NB_IF:
481 iface |= 0x0010;
482 break;
483 default:
484 return -EINVAL;
487 snd_soc_write(codec, WM8971_IFACE, iface);
488 return 0;
491 static int wm8971_pcm_hw_params(struct snd_pcm_substream *substream,
492 struct snd_pcm_hw_params *params,
493 struct snd_soc_dai *dai)
495 struct snd_soc_pcm_runtime *rtd = substream->private_data;
496 struct snd_soc_device *socdev = rtd->socdev;
497 struct snd_soc_codec *codec = socdev->card->codec;
498 struct wm8971_priv *wm8971 = codec->private_data;
499 u16 iface = snd_soc_read(codec, WM8971_IFACE) & 0x1f3;
500 u16 srate = snd_soc_read(codec, WM8971_SRATE) & 0x1c0;
501 int coeff = get_coeff(wm8971->sysclk, params_rate(params));
503 /* bit size */
504 switch (params_format(params)) {
505 case SNDRV_PCM_FORMAT_S16_LE:
506 break;
507 case SNDRV_PCM_FORMAT_S20_3LE:
508 iface |= 0x0004;
509 break;
510 case SNDRV_PCM_FORMAT_S24_LE:
511 iface |= 0x0008;
512 break;
513 case SNDRV_PCM_FORMAT_S32_LE:
514 iface |= 0x000c;
515 break;
518 /* set iface & srate */
519 snd_soc_write(codec, WM8971_IFACE, iface);
520 if (coeff >= 0)
521 snd_soc_write(codec, WM8971_SRATE, srate |
522 (coeff_div[coeff].sr << 1) | coeff_div[coeff].usb);
524 return 0;
527 static int wm8971_mute(struct snd_soc_dai *dai, int mute)
529 struct snd_soc_codec *codec = dai->codec;
530 u16 mute_reg = snd_soc_read(codec, WM8971_ADCDAC) & 0xfff7;
532 if (mute)
533 snd_soc_write(codec, WM8971_ADCDAC, mute_reg | 0x8);
534 else
535 snd_soc_write(codec, WM8971_ADCDAC, mute_reg);
536 return 0;
539 static int wm8971_set_bias_level(struct snd_soc_codec *codec,
540 enum snd_soc_bias_level level)
542 u16 pwr_reg = snd_soc_read(codec, WM8971_PWR1) & 0xfe3e;
544 switch (level) {
545 case SND_SOC_BIAS_ON:
546 /* set vmid to 50k and unmute dac */
547 snd_soc_write(codec, WM8971_PWR1, pwr_reg | 0x00c1);
548 break;
549 case SND_SOC_BIAS_PREPARE:
550 break;
551 case SND_SOC_BIAS_STANDBY:
552 /* mute dac and set vmid to 500k, enable VREF */
553 snd_soc_write(codec, WM8971_PWR1, pwr_reg | 0x0140);
554 break;
555 case SND_SOC_BIAS_OFF:
556 snd_soc_write(codec, WM8971_PWR1, 0x0001);
557 break;
559 codec->bias_level = level;
560 return 0;
563 #define WM8971_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
564 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
565 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
567 #define WM8971_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
568 SNDRV_PCM_FMTBIT_S24_LE)
570 static struct snd_soc_dai_ops wm8971_dai_ops = {
571 .hw_params = wm8971_pcm_hw_params,
572 .digital_mute = wm8971_mute,
573 .set_fmt = wm8971_set_dai_fmt,
574 .set_sysclk = wm8971_set_dai_sysclk,
577 struct snd_soc_dai wm8971_dai = {
578 .name = "WM8971",
579 .playback = {
580 .stream_name = "Playback",
581 .channels_min = 1,
582 .channels_max = 2,
583 .rates = WM8971_RATES,
584 .formats = WM8971_FORMATS,},
585 .capture = {
586 .stream_name = "Capture",
587 .channels_min = 1,
588 .channels_max = 2,
589 .rates = WM8971_RATES,
590 .formats = WM8971_FORMATS,},
591 .ops = &wm8971_dai_ops,
593 EXPORT_SYMBOL_GPL(wm8971_dai);
595 static void wm8971_work(struct work_struct *work)
597 struct snd_soc_codec *codec =
598 container_of(work, struct snd_soc_codec, delayed_work.work);
599 wm8971_set_bias_level(codec, codec->bias_level);
602 static int wm8971_suspend(struct platform_device *pdev, pm_message_t state)
604 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
605 struct snd_soc_codec *codec = socdev->card->codec;
607 wm8971_set_bias_level(codec, SND_SOC_BIAS_OFF);
608 return 0;
611 static int wm8971_resume(struct platform_device *pdev)
613 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
614 struct snd_soc_codec *codec = socdev->card->codec;
615 int i;
616 u8 data[2];
617 u16 *cache = codec->reg_cache;
618 u16 reg;
620 /* Sync reg_cache with the hardware */
621 for (i = 0; i < ARRAY_SIZE(wm8971_reg); i++) {
622 if (i + 1 == WM8971_RESET)
623 continue;
624 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
625 data[1] = cache[i] & 0x00ff;
626 codec->hw_write(codec->control_data, data, 2);
629 wm8971_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
631 /* charge wm8971 caps */
632 if (codec->suspend_bias_level == SND_SOC_BIAS_ON) {
633 reg = snd_soc_read(codec, WM8971_PWR1) & 0xfe3e;
634 snd_soc_write(codec, WM8971_PWR1, reg | 0x01c0);
635 codec->bias_level = SND_SOC_BIAS_ON;
636 queue_delayed_work(wm8971_workq, &codec->delayed_work,
637 msecs_to_jiffies(1000));
640 return 0;
643 static int wm8971_init(struct snd_soc_device *socdev,
644 enum snd_soc_control_type control)
646 struct snd_soc_codec *codec = socdev->card->codec;
647 int reg, ret = 0;
649 codec->name = "WM8971";
650 codec->owner = THIS_MODULE;
651 codec->set_bias_level = wm8971_set_bias_level;
652 codec->dai = &wm8971_dai;
653 codec->reg_cache_size = ARRAY_SIZE(wm8971_reg);
654 codec->num_dai = 1;
655 codec->reg_cache = kmemdup(wm8971_reg, sizeof(wm8971_reg), GFP_KERNEL);
657 if (codec->reg_cache == NULL)
658 return -ENOMEM;
660 ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
661 if (ret < 0) {
662 printk(KERN_ERR "wm8971: failed to set cache I/O: %d\n", ret);
663 goto err;
666 wm8971_reset(codec);
668 /* register pcms */
669 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
670 if (ret < 0) {
671 printk(KERN_ERR "wm8971: failed to create pcms\n");
672 goto err;
675 /* charge output caps - set vmid to 5k for quick power up */
676 reg = snd_soc_read(codec, WM8971_PWR1) & 0xfe3e;
677 snd_soc_write(codec, WM8971_PWR1, reg | 0x01c0);
678 codec->bias_level = SND_SOC_BIAS_STANDBY;
679 queue_delayed_work(wm8971_workq, &codec->delayed_work,
680 msecs_to_jiffies(1000));
682 /* set the update bits */
683 reg = snd_soc_read(codec, WM8971_LDAC);
684 snd_soc_write(codec, WM8971_LDAC, reg | 0x0100);
685 reg = snd_soc_read(codec, WM8971_RDAC);
686 snd_soc_write(codec, WM8971_RDAC, reg | 0x0100);
688 reg = snd_soc_read(codec, WM8971_LOUT1V);
689 snd_soc_write(codec, WM8971_LOUT1V, reg | 0x0100);
690 reg = snd_soc_read(codec, WM8971_ROUT1V);
691 snd_soc_write(codec, WM8971_ROUT1V, reg | 0x0100);
693 reg = snd_soc_read(codec, WM8971_LOUT2V);
694 snd_soc_write(codec, WM8971_LOUT2V, reg | 0x0100);
695 reg = snd_soc_read(codec, WM8971_ROUT2V);
696 snd_soc_write(codec, WM8971_ROUT2V, reg | 0x0100);
698 reg = snd_soc_read(codec, WM8971_LINVOL);
699 snd_soc_write(codec, WM8971_LINVOL, reg | 0x0100);
700 reg = snd_soc_read(codec, WM8971_RINVOL);
701 snd_soc_write(codec, WM8971_RINVOL, reg | 0x0100);
703 snd_soc_add_controls(codec, wm8971_snd_controls,
704 ARRAY_SIZE(wm8971_snd_controls));
705 wm8971_add_widgets(codec);
706 ret = snd_soc_init_card(socdev);
707 if (ret < 0) {
708 printk(KERN_ERR "wm8971: failed to register card\n");
709 goto card_err;
711 return ret;
713 card_err:
714 snd_soc_free_pcms(socdev);
715 snd_soc_dapm_free(socdev);
716 err:
717 kfree(codec->reg_cache);
718 return ret;
721 /* If the i2c layer weren't so broken, we could pass this kind of data
722 around */
723 static struct snd_soc_device *wm8971_socdev;
725 #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
727 static int wm8971_i2c_probe(struct i2c_client *i2c,
728 const struct i2c_device_id *id)
730 struct snd_soc_device *socdev = wm8971_socdev;
731 struct snd_soc_codec *codec = socdev->card->codec;
732 int ret;
734 i2c_set_clientdata(i2c, codec);
736 codec->control_data = i2c;
738 ret = wm8971_init(socdev, SND_SOC_I2C);
739 if (ret < 0)
740 pr_err("failed to initialise WM8971\n");
742 return ret;
745 static int wm8971_i2c_remove(struct i2c_client *client)
747 struct snd_soc_codec *codec = i2c_get_clientdata(client);
748 kfree(codec->reg_cache);
749 return 0;
752 static const struct i2c_device_id wm8971_i2c_id[] = {
753 { "wm8971", 0 },
756 MODULE_DEVICE_TABLE(i2c, wm8971_i2c_id);
758 static struct i2c_driver wm8971_i2c_driver = {
759 .driver = {
760 .name = "WM8971 I2C Codec",
761 .owner = THIS_MODULE,
763 .probe = wm8971_i2c_probe,
764 .remove = wm8971_i2c_remove,
765 .id_table = wm8971_i2c_id,
768 static int wm8971_add_i2c_device(struct platform_device *pdev,
769 const struct wm8971_setup_data *setup)
771 struct i2c_board_info info;
772 struct i2c_adapter *adapter;
773 struct i2c_client *client;
774 int ret;
776 ret = i2c_add_driver(&wm8971_i2c_driver);
777 if (ret != 0) {
778 dev_err(&pdev->dev, "can't add i2c driver\n");
779 return ret;
782 memset(&info, 0, sizeof(struct i2c_board_info));
783 info.addr = setup->i2c_address;
784 strlcpy(info.type, "wm8971", I2C_NAME_SIZE);
786 adapter = i2c_get_adapter(setup->i2c_bus);
787 if (!adapter) {
788 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
789 setup->i2c_bus);
790 goto err_driver;
793 client = i2c_new_device(adapter, &info);
794 i2c_put_adapter(adapter);
795 if (!client) {
796 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
797 (unsigned int)info.addr);
798 goto err_driver;
801 return 0;
803 err_driver:
804 i2c_del_driver(&wm8971_i2c_driver);
805 return -ENODEV;
808 #endif
810 static int wm8971_probe(struct platform_device *pdev)
812 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
813 struct wm8971_setup_data *setup;
814 struct snd_soc_codec *codec;
815 struct wm8971_priv *wm8971;
816 int ret = 0;
818 pr_info("WM8971 Audio Codec %s", WM8971_VERSION);
820 setup = socdev->codec_data;
821 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
822 if (codec == NULL)
823 return -ENOMEM;
825 wm8971 = kzalloc(sizeof(struct wm8971_priv), GFP_KERNEL);
826 if (wm8971 == NULL) {
827 kfree(codec);
828 return -ENOMEM;
831 codec->private_data = wm8971;
832 socdev->card->codec = codec;
833 mutex_init(&codec->mutex);
834 INIT_LIST_HEAD(&codec->dapm_widgets);
835 INIT_LIST_HEAD(&codec->dapm_paths);
836 wm8971_socdev = socdev;
838 INIT_DELAYED_WORK(&codec->delayed_work, wm8971_work);
839 wm8971_workq = create_workqueue("wm8971");
840 if (wm8971_workq == NULL) {
841 kfree(codec->private_data);
842 kfree(codec);
843 return -ENOMEM;
846 #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
847 if (setup->i2c_address) {
848 ret = wm8971_add_i2c_device(pdev, setup);
850 #endif
851 /* Add other interfaces here */
853 if (ret != 0) {
854 destroy_workqueue(wm8971_workq);
855 kfree(codec->private_data);
856 kfree(codec);
859 return ret;
862 /* power down chip */
863 static int wm8971_remove(struct platform_device *pdev)
865 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
866 struct snd_soc_codec *codec = socdev->card->codec;
868 if (codec->control_data)
869 wm8971_set_bias_level(codec, SND_SOC_BIAS_OFF);
870 if (wm8971_workq)
871 destroy_workqueue(wm8971_workq);
872 snd_soc_free_pcms(socdev);
873 snd_soc_dapm_free(socdev);
874 #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
875 i2c_unregister_device(codec->control_data);
876 i2c_del_driver(&wm8971_i2c_driver);
877 #endif
878 kfree(codec->private_data);
879 kfree(codec);
881 return 0;
884 struct snd_soc_codec_device soc_codec_dev_wm8971 = {
885 .probe = wm8971_probe,
886 .remove = wm8971_remove,
887 .suspend = wm8971_suspend,
888 .resume = wm8971_resume,
891 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8971);
893 static int __init wm8971_modinit(void)
895 return snd_soc_register_dai(&wm8971_dai);
897 module_init(wm8971_modinit);
899 static void __exit wm8971_exit(void)
901 snd_soc_unregister_dai(&wm8971_dai);
903 module_exit(wm8971_exit);
905 MODULE_DESCRIPTION("ASoC WM8971 driver");
906 MODULE_AUTHOR("Lab126");
907 MODULE_LICENSE("GPL");