xref: /openbmc/linux/sound/soc/codecs/wm8971.c (revision e8e0929d)
1 /*
2  * wm8971.c  --  WM8971 ALSA SoC Audio driver
3  *
4  * Copyright 2005 Lab126, Inc.
5  *
6  * Author: Kenneth Kiraly <kiraly@lab126.com>
7  *
8  * Based on wm8753.c by Liam Girdwood
9  *
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.
14  */
15 
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>
29 
30 #include "wm8971.h"
31 
32 #define WM8971_VERSION "0.9"
33 
34 #define	WM8971_REG_COUNT		43
35 
36 static struct workqueue_struct *wm8971_workq = NULL;
37 
38 /* codec private data */
39 struct wm8971_priv {
40 	unsigned int sysclk;
41 };
42 
43 /*
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.
47  */
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 */
60 };
61 
62 #define wm8971_reset(c)	snd_soc_write(c, WM8971_RESET, 0)
63 
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"};
84 
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),
100 };
101 
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),
107 
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),
113 
114 	SOC_DOUBLE_R("PCM Volume", WM8971_LDAC, WM8971_RDAC, 0, 255, 0),
115 
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),
122 
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),
127 
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),
131 
132 	SOC_SINGLE("Treble Volume", WM8971_TREBLE, 0, 7, 0),
133 	SOC_ENUM("Treble Cut-off", wm8971_enum[2]),
134 
135 	SOC_SINGLE("Capture Filter Switch", WM8971_ADCDAC, 0, 1, 1),
136 
137 	SOC_SINGLE("ALC Target Volume", WM8971_ALC1, 0, 7, 0),
138 	SOC_SINGLE("ALC Max Volume", WM8971_ALC1, 4, 7, 0),
139 
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),
150 
151 	SOC_SINGLE("Capture 6dB Attenuate", WM8971_ADCDAC, 8, 1, 0),
152 	SOC_SINGLE("Playback 6dB Attenuate", WM8971_ADCDAC, 7, 1, 0),
153 
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]),
157 
158 	SOC_DOUBLE_R("Mic Boost", WM8971_LADCIN, WM8971_RADCIN, 4, 3, 0),
159 };
160 
161 /*
162  * DAPM Controls
163  */
164 
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),
171 };
172 
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),
179 };
180 
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),
187 };
188 
189 /* Left Line Mux */
190 static const struct snd_kcontrol_new wm8971_left_line_controls =
191 SOC_DAPM_ENUM("Route", wm8971_enum[8]);
192 
193 /* Right Line Mux */
194 static const struct snd_kcontrol_new wm8971_right_line_controls =
195 SOC_DAPM_ENUM("Route", wm8971_enum[9]);
196 
197 /* Left PGA Mux */
198 static const struct snd_kcontrol_new wm8971_left_pga_controls =
199 SOC_DAPM_ENUM("Route", wm8971_enum[10]);
200 
201 /* Right PGA Mux */
202 static const struct snd_kcontrol_new wm8971_right_pga_controls =
203 SOC_DAPM_ENUM("Route", wm8971_enum[11]);
204 
205 /* Mono ADC Mux */
206 static const struct snd_kcontrol_new wm8971_monomux_controls =
207 SOC_DAPM_ENUM("Route", wm8971_enum[13]);
208 
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)),
219 
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),
227 
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),
231 
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),
240 
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),
245 
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"),
251 
252 	SND_SOC_DAPM_INPUT("LINPUT1"),
253 	SND_SOC_DAPM_INPUT("RINPUT1"),
254 	SND_SOC_DAPM_INPUT("MIC"),
255 };
256 
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"},
263 
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"},
269 
270 	/* left out 1 */
271 	{"Left Out 1", NULL, "Left Mixer"},
272 	{"LOUT1", NULL, "Left Out 1"},
273 
274 	/* left out 2 */
275 	{"Left Out 2", NULL, "Left Mixer"},
276 	{"LOUT2", NULL, "Left Out 2"},
277 
278 	/* right out 1 */
279 	{"Right Out 1", NULL, "Right Mixer"},
280 	{"ROUT1", NULL, "Right Out 1"},
281 
282 	/* right out 2 */
283 	{"Right Out 2", NULL, "Right Mixer"},
284 	{"ROUT2", NULL, "Right Out 2"},
285 
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"},
291 
292 	/* mono out */
293 	{"Mono Out", NULL, "Mono Mixer"},
294 	{"MONO1", NULL, "Mono Out"},
295 
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"},
300 
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"},
306 
307 	/* Left PGA Mux */
308 	{"Left PGA Mux", "Line", "LINPUT1"},
309 	{"Left PGA Mux", "Differential", "Differential Mux"},
310 
311 	/* Right PGA Mux */
312 	{"Right PGA Mux", "Line", "RINPUT1"},
313 	{"Right PGA Mux", "Differential", "Differential Mux"},
314 
315 	/* Differential Mux */
316 	{"Differential Mux", "Line", "LINPUT1"},
317 	{"Differential Mux", "Line", "RINPUT1"},
318 
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"},
323 
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"},
328 
329 	/* ADC */
330 	{"Left ADC", NULL, "Left ADC Mux"},
331 	{"Right ADC", NULL, "Right ADC Mux"},
332 };
333 
334 static int wm8971_add_widgets(struct snd_soc_codec *codec)
335 {
336 	snd_soc_dapm_new_controls(codec, wm8971_dapm_widgets,
337 				  ARRAY_SIZE(wm8971_dapm_widgets));
338 
339 	snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
340 
341 	snd_soc_dapm_new_widgets(codec);
342 
343 	return 0;
344 }
345 
346 struct _coeff_div {
347 	u32 mclk;
348 	u32 rate;
349 	u16 fs;
350 	u8 sr:5;
351 	u8 usb:1;
352 };
353 
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},
362 
363 	/* 11.025k */
364 	{11289600, 11025, 1024, 0x18, 0x0},
365 	{16934400, 11025, 1536, 0x19, 0x0},
366 	{12000000, 11025, 1088, 0x19, 0x1},
367 
368 	/* 16k */
369 	{12288000, 16000, 768, 0xa, 0x0},
370 	{18432000, 16000, 1152, 0xb, 0x0},
371 	{12000000, 16000, 750, 0xa, 0x1},
372 
373 	/* 22.05k */
374 	{11289600, 22050, 512, 0x1a, 0x0},
375 	{16934400, 22050, 768, 0x1b, 0x0},
376 	{12000000, 22050, 544, 0x1b, 0x1},
377 
378 	/* 32k */
379 	{12288000, 32000, 384, 0xc, 0x0},
380 	{18432000, 32000, 576, 0xd, 0x0},
381 	{12000000, 32000, 375, 0xa, 0x1},
382 
383 	/* 44.1k */
384 	{11289600, 44100, 256, 0x10, 0x0},
385 	{16934400, 44100, 384, 0x11, 0x0},
386 	{12000000, 44100, 272, 0x11, 0x1},
387 
388 	/* 48k */
389 	{12288000, 48000, 256, 0x0, 0x0},
390 	{18432000, 48000, 384, 0x1, 0x0},
391 	{12000000, 48000, 250, 0x0, 0x1},
392 
393 	/* 88.2k */
394 	{11289600, 88200, 128, 0x1e, 0x0},
395 	{16934400, 88200, 192, 0x1f, 0x0},
396 	{12000000, 88200, 136, 0x1f, 0x1},
397 
398 	/* 96k */
399 	{12288000, 96000, 128, 0xe, 0x0},
400 	{18432000, 96000, 192, 0xf, 0x0},
401 	{12000000, 96000, 125, 0xe, 0x1},
402 };
403 
404 static int get_coeff(int mclk, int rate)
405 {
406 	int i;
407 
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;
411 	}
412 	return -EINVAL;
413 }
414 
415 static int wm8971_set_dai_sysclk(struct snd_soc_dai *codec_dai,
416 		int clk_id, unsigned int freq, int dir)
417 {
418 	struct snd_soc_codec *codec = codec_dai->codec;
419 	struct wm8971_priv *wm8971 = codec->private_data;
420 
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;
429 	}
430 	return -EINVAL;
431 }
432 
433 static int wm8971_set_dai_fmt(struct snd_soc_dai *codec_dai,
434 		unsigned int fmt)
435 {
436 	struct snd_soc_codec *codec = codec_dai->codec;
437 	u16 iface = 0;
438 
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;
448 	}
449 
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;
468 	}
469 
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;
485 	}
486 
487 	snd_soc_write(codec, WM8971_IFACE, iface);
488 	return 0;
489 }
490 
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)
494 {
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));
502 
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;
516 	}
517 
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);
523 
524 	return 0;
525 }
526 
527 static int wm8971_mute(struct snd_soc_dai *dai, int mute)
528 {
529 	struct snd_soc_codec *codec = dai->codec;
530 	u16 mute_reg = snd_soc_read(codec, WM8971_ADCDAC) & 0xfff7;
531 
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;
537 }
538 
539 static int wm8971_set_bias_level(struct snd_soc_codec *codec,
540 	enum snd_soc_bias_level level)
541 {
542 	u16 pwr_reg = snd_soc_read(codec, WM8971_PWR1) & 0xfe3e;
543 
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;
558 	}
559 	codec->bias_level = level;
560 	return 0;
561 }
562 
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)
566 
567 #define WM8971_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
568 	SNDRV_PCM_FMTBIT_S24_LE)
569 
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,
575 };
576 
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,
592 };
593 EXPORT_SYMBOL_GPL(wm8971_dai);
594 
595 static void wm8971_work(struct work_struct *work)
596 {
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);
600 }
601 
602 static int wm8971_suspend(struct platform_device *pdev, pm_message_t state)
603 {
604 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
605 	struct snd_soc_codec *codec = socdev->card->codec;
606 
607 	wm8971_set_bias_level(codec, SND_SOC_BIAS_OFF);
608 	return 0;
609 }
610 
611 static int wm8971_resume(struct platform_device *pdev)
612 {
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;
619 
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);
627 	}
628 
629 	wm8971_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
630 
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));
638 	}
639 
640 	return 0;
641 }
642 
643 static int wm8971_init(struct snd_soc_device *socdev,
644 		       enum snd_soc_control_type control)
645 {
646 	struct snd_soc_codec *codec = socdev->card->codec;
647 	int reg, ret = 0;
648 
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);
656 
657 	if (codec->reg_cache == NULL)
658 		return -ENOMEM;
659 
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;
664 	}
665 
666 	wm8971_reset(codec);
667 
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;
673 	}
674 
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));
681 
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);
687 
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);
692 
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);
697 
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);
702 
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;
710 	}
711 	return ret;
712 
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;
719 }
720 
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;
724 
725 #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
726 
727 static int wm8971_i2c_probe(struct i2c_client *i2c,
728 			    const struct i2c_device_id *id)
729 {
730 	struct snd_soc_device *socdev = wm8971_socdev;
731 	struct snd_soc_codec *codec = socdev->card->codec;
732 	int ret;
733 
734 	i2c_set_clientdata(i2c, codec);
735 
736 	codec->control_data = i2c;
737 
738 	ret = wm8971_init(socdev, SND_SOC_I2C);
739 	if (ret < 0)
740 		pr_err("failed to initialise WM8971\n");
741 
742 	return ret;
743 }
744 
745 static int wm8971_i2c_remove(struct i2c_client *client)
746 {
747 	struct snd_soc_codec *codec = i2c_get_clientdata(client);
748 	kfree(codec->reg_cache);
749 	return 0;
750 }
751 
752 static const struct i2c_device_id wm8971_i2c_id[] = {
753 	{ "wm8971", 0 },
754 	{ }
755 };
756 MODULE_DEVICE_TABLE(i2c, wm8971_i2c_id);
757 
758 static struct i2c_driver wm8971_i2c_driver = {
759 	.driver = {
760 		.name = "WM8971 I2C Codec",
761 		.owner = THIS_MODULE,
762 	},
763 	.probe    = wm8971_i2c_probe,
764 	.remove   = wm8971_i2c_remove,
765 	.id_table = wm8971_i2c_id,
766 };
767 
768 static int wm8971_add_i2c_device(struct platform_device *pdev,
769 				 const struct wm8971_setup_data *setup)
770 {
771 	struct i2c_board_info info;
772 	struct i2c_adapter *adapter;
773 	struct i2c_client *client;
774 	int ret;
775 
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;
780 	}
781 
782 	memset(&info, 0, sizeof(struct i2c_board_info));
783 	info.addr = setup->i2c_address;
784 	strlcpy(info.type, "wm8971", I2C_NAME_SIZE);
785 
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;
791 	}
792 
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;
799 	}
800 
801 	return 0;
802 
803 err_driver:
804 	i2c_del_driver(&wm8971_i2c_driver);
805 	return -ENODEV;
806 }
807 
808 #endif
809 
810 static int wm8971_probe(struct platform_device *pdev)
811 {
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;
817 
818 	pr_info("WM8971 Audio Codec %s", WM8971_VERSION);
819 
820 	setup = socdev->codec_data;
821 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
822 	if (codec == NULL)
823 		return -ENOMEM;
824 
825 	wm8971 = kzalloc(sizeof(struct wm8971_priv), GFP_KERNEL);
826 	if (wm8971 == NULL) {
827 		kfree(codec);
828 		return -ENOMEM;
829 	}
830 
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;
837 
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;
844 	}
845 
846 #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
847 	if (setup->i2c_address) {
848 		ret = wm8971_add_i2c_device(pdev, setup);
849 	}
850 #endif
851 	/* Add other interfaces here */
852 
853 	if (ret != 0) {
854 		destroy_workqueue(wm8971_workq);
855 		kfree(codec->private_data);
856 		kfree(codec);
857 	}
858 
859 	return ret;
860 }
861 
862 /* power down chip */
863 static int wm8971_remove(struct platform_device *pdev)
864 {
865 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
866 	struct snd_soc_codec *codec = socdev->card->codec;
867 
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);
880 
881 	return 0;
882 }
883 
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,
889 };
890 
891 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8971);
892 
893 static int __init wm8971_modinit(void)
894 {
895 	return snd_soc_register_dai(&wm8971_dai);
896 }
897 module_init(wm8971_modinit);
898 
899 static void __exit wm8971_exit(void)
900 {
901 	snd_soc_unregister_dai(&wm8971_dai);
902 }
903 module_exit(wm8971_exit);
904 
905 MODULE_DESCRIPTION("ASoC WM8971 driver");
906 MODULE_AUTHOR("Lab126");
907 MODULE_LICENSE("GPL");
908