Merge remote-tracking branch 'remotes/tegra/android-tegra-2.6.36-honeycomb-mr1' into...
[firefly-linux-kernel-4.4.55.git] / sound / soc / codecs / wm8988.c
1 /*
2  * wm8988.c -- WM8988 ALSA SoC audio driver
3  *
4  * Copyright 2009 Wolfson Microelectronics plc
5  * Copyright 2005 Openedhand Ltd.
6  *
7  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/pm.h>
19 #include <linux/i2c.h>
20 #include <linux/spi/spi.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/tlv.h>
27 #include <sound/soc.h>
28 #include <sound/soc-dapm.h>
29 #include <sound/initval.h>
30
31 #include <mach/iomux.h>
32 #include <mach/gpio.h>
33
34 #include "wm8988.h"
35
36
37 #if 0
38 #define DBG(x...) printk(KERN_INFO x)
39 #else
40 #define DBG(x...) do { } while (0)
41 #endif
42
43 /*
44  * wm8988 register cache
45  * We can't read the WM8988 register space when we
46  * are using 2 wire for device control, so we cache them instead.
47  */
48 static const u16 wm8988_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 /* codec private data */
63 struct wm8988_priv {
64         unsigned int sysclk;
65         struct snd_soc_codec codec;
66         struct snd_pcm_hw_constraint_list *sysclk_constraints;
67         u16 reg_cache[WM8988_NUM_REG];
68 };
69
70
71 #define wm8988_reset(c) snd_soc_write(c, WM8988_RESET, 0)
72
73 /*
74  * WM8988 Controls
75  */
76
77 static const char *bass_boost_txt[] = {"Linear Control", "Adaptive Boost"};
78 static const struct soc_enum bass_boost =
79         SOC_ENUM_SINGLE(WM8988_BASS, 7, 2, bass_boost_txt);
80
81 static const char *bass_filter_txt[] = { "130Hz @ 48kHz", "200Hz @ 48kHz" };
82 static const struct soc_enum bass_filter =
83         SOC_ENUM_SINGLE(WM8988_BASS, 6, 2, bass_filter_txt);
84
85 static const char *treble_txt[] = {"8kHz", "4kHz"};
86 static const struct soc_enum treble =
87         SOC_ENUM_SINGLE(WM8988_TREBLE, 6, 2, treble_txt);
88
89 static const char *stereo_3d_lc_txt[] = {"200Hz", "500Hz"};
90 static const struct soc_enum stereo_3d_lc =
91         SOC_ENUM_SINGLE(WM8988_3D, 5, 2, stereo_3d_lc_txt);
92
93 static const char *stereo_3d_uc_txt[] = {"2.2kHz", "1.5kHz"};
94 static const struct soc_enum stereo_3d_uc =
95         SOC_ENUM_SINGLE(WM8988_3D, 6, 2, stereo_3d_uc_txt);
96
97 static const char *stereo_3d_func_txt[] = {"Capture", "Playback"};
98 static const struct soc_enum stereo_3d_func =
99         SOC_ENUM_SINGLE(WM8988_3D, 7, 2, stereo_3d_func_txt);
100
101 static const char *alc_func_txt[] = {"Off", "Right", "Left", "Stereo"};
102 static const struct soc_enum alc_func =
103         SOC_ENUM_SINGLE(WM8988_ALC1, 7, 4, alc_func_txt);
104
105 static const char *ng_type_txt[] = {"Constant PGA Gain",
106                                     "Mute ADC Output"};
107 static const struct soc_enum ng_type =
108         SOC_ENUM_SINGLE(WM8988_NGATE, 1, 2, ng_type_txt);
109
110 static const char *deemph_txt[] = {"None", "32Khz", "44.1Khz", "48Khz"};
111 static const struct soc_enum deemph =
112         SOC_ENUM_SINGLE(WM8988_ADCDAC, 1, 4, deemph_txt);
113
114 static const char *adcpol_txt[] = {"Normal", "L Invert", "R Invert",
115                                    "L + R Invert"};
116 static const struct soc_enum adcpol =
117         SOC_ENUM_SINGLE(WM8988_ADCDAC, 5, 4, adcpol_txt);
118
119 static const DECLARE_TLV_DB_SCALE(pga_tlv, -1725, 75, 0);
120 static const DECLARE_TLV_DB_SCALE(adc_tlv, -9750, 50, 1);
121 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
122 static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
123 static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
124
125 static const struct snd_kcontrol_new wm8988_snd_controls[] = {
126
127 SOC_ENUM("Bass Boost", bass_boost),
128 SOC_ENUM("Bass Filter", bass_filter),
129 SOC_SINGLE("Bass Volume", WM8988_BASS, 0, 15, 1),
130
131 SOC_SINGLE("Treble Volume", WM8988_TREBLE, 0, 15, 0),
132 SOC_ENUM("Treble Cut-off", treble),
133
134 SOC_SINGLE("3D Switch", WM8988_3D, 0, 1, 0),
135 SOC_SINGLE("3D Volume", WM8988_3D, 1, 15, 0),
136 SOC_ENUM("3D Lower Cut-off", stereo_3d_lc),
137 SOC_ENUM("3D Upper Cut-off", stereo_3d_uc),
138 SOC_ENUM("3D Mode", stereo_3d_func),
139
140 SOC_SINGLE("ALC Capture Target Volume", WM8988_ALC1, 0, 7, 0),
141 SOC_SINGLE("ALC Capture Max Volume", WM8988_ALC1, 4, 7, 0),
142 SOC_ENUM("ALC Capture Function", alc_func),
143 SOC_SINGLE("ALC Capture ZC Switch", WM8988_ALC2, 7, 1, 0),
144 SOC_SINGLE("ALC Capture Hold Time", WM8988_ALC2, 0, 15, 0),
145 SOC_SINGLE("ALC Capture Decay Time", WM8988_ALC3, 4, 15, 0),
146 SOC_SINGLE("ALC Capture Attack Time", WM8988_ALC3, 0, 15, 0),
147 SOC_SINGLE("ALC Capture NG Threshold", WM8988_NGATE, 3, 31, 0),
148 SOC_ENUM("ALC Capture NG Type", ng_type),
149 SOC_SINGLE("ALC Capture NG Switch", WM8988_NGATE, 0, 1, 0),
150
151 SOC_SINGLE("ZC Timeout Switch", WM8988_ADCTL1, 0, 1, 0),
152
153 SOC_DOUBLE_R_TLV("Capture Digital Volume", WM8988_LADC, WM8988_RADC,
154                  0, 255, 0, adc_tlv),
155 SOC_DOUBLE_R_TLV("Capture Volume", WM8988_LINVOL, WM8988_RINVOL,
156                  0, 63, 0, pga_tlv),
157 SOC_DOUBLE_R("Capture ZC Switch", WM8988_LINVOL, WM8988_RINVOL, 6, 1, 0),
158 SOC_DOUBLE_R("Capture Switch", WM8988_LINVOL, WM8988_RINVOL, 7, 1, 1),
159
160 SOC_ENUM("Playback De-emphasis", deemph),
161
162 SOC_ENUM("Capture Polarity", adcpol),
163 SOC_SINGLE("Playback 6dB Attenuate", WM8988_ADCDAC, 7, 1, 0),
164 SOC_SINGLE("Capture 6dB Attenuate", WM8988_ADCDAC, 8, 1, 0),
165
166 SOC_DOUBLE_R_TLV("PCM Volume", WM8988_LDAC, WM8988_RDAC, 0, 255, 0, dac_tlv),
167
168 SOC_SINGLE_TLV("Left Mixer Left Bypass Volume", WM8988_LOUTM1, 4, 7, 1,
169                bypass_tlv),
170 SOC_SINGLE_TLV("Left Mixer Right Bypass Volume", WM8988_LOUTM2, 4, 7, 1,
171                bypass_tlv),
172 SOC_SINGLE_TLV("Right Mixer Left Bypass Volume", WM8988_ROUTM1, 4, 7, 1,
173                bypass_tlv),
174 SOC_SINGLE_TLV("Right Mixer Right Bypass Volume", WM8988_ROUTM2, 4, 7, 1,
175                bypass_tlv),
176
177 SOC_DOUBLE_R("Output 1 Playback ZC Switch", WM8988_LOUT1V,
178              WM8988_ROUT1V, 7, 1, 0),
179 SOC_DOUBLE_R_TLV("Output 1 Playback Volume", WM8988_LOUT1V, WM8988_ROUT1V,
180                  0, 127, 0, out_tlv),
181
182 SOC_DOUBLE_R("Output 2 Playback ZC Switch", WM8988_LOUT2V,
183              WM8988_ROUT2V, 7, 1, 0),
184 SOC_DOUBLE_R_TLV("Output 2 Playback Volume", WM8988_LOUT2V, WM8988_ROUT2V,
185                  0, 127, 0, out_tlv),
186
187 };
188
189 /*
190  * DAPM Controls
191  */
192
193 static int wm8988_lrc_control(struct snd_soc_dapm_widget *w,
194                               struct snd_kcontrol *kcontrol, int event)
195 {
196         struct snd_soc_codec *codec = w->codec;
197         u16 adctl2 = snd_soc_read(codec, WM8988_ADCTL2);
198
199         /* Use the DAC to gate LRC if active, otherwise use ADC */
200         if (snd_soc_read(codec, WM8988_PWR2) & 0x180)
201                 adctl2 &= ~0x4;
202         else
203                 adctl2 |= 0x4;
204
205         DBG("Enter::%s----%d, adctl2 = %x\n",__FUNCTION__,__LINE__,adctl2);
206         
207         return snd_soc_write(codec, WM8988_ADCTL2, adctl2);
208 }
209
210 static const char *wm8988_line_texts[] = {
211         "Line 1", "Line 2", "PGA", "Differential"};
212
213 static const unsigned int wm8988_line_values[] = {
214         0, 1, 3, 4};
215
216 static const struct soc_enum wm8988_lline_enum =
217         SOC_VALUE_ENUM_SINGLE(WM8988_LOUTM1, 0, 7,
218                               ARRAY_SIZE(wm8988_line_texts),
219                               wm8988_line_texts,
220                               wm8988_line_values);
221 static const struct snd_kcontrol_new wm8988_left_line_controls =
222         SOC_DAPM_VALUE_ENUM("Route", wm8988_lline_enum);
223
224 static const struct soc_enum wm8988_rline_enum =
225         SOC_VALUE_ENUM_SINGLE(WM8988_ROUTM1, 0, 7,
226                               ARRAY_SIZE(wm8988_line_texts),
227                               wm8988_line_texts,
228                               wm8988_line_values);
229 static const struct snd_kcontrol_new wm8988_right_line_controls =
230         SOC_DAPM_VALUE_ENUM("Route", wm8988_lline_enum);
231
232 /* Left Mixer */
233 static const struct snd_kcontrol_new wm8988_left_mixer_controls[] = {
234         SOC_DAPM_SINGLE("Playback Switch", WM8988_LOUTM1, 8, 1, 0),
235         SOC_DAPM_SINGLE("Left Bypass Switch", WM8988_LOUTM1, 7, 1, 0),
236         SOC_DAPM_SINGLE("Right Playback Switch", WM8988_LOUTM2, 8, 1, 0),
237         SOC_DAPM_SINGLE("Right Bypass Switch", WM8988_LOUTM2, 7, 1, 0),
238 };
239
240 /* Right Mixer */
241 static const struct snd_kcontrol_new wm8988_right_mixer_controls[] = {
242         SOC_DAPM_SINGLE("Left Playback Switch", WM8988_ROUTM1, 8, 1, 0),
243         SOC_DAPM_SINGLE("Left Bypass Switch", WM8988_ROUTM1, 7, 1, 0),
244         SOC_DAPM_SINGLE("Playback Switch", WM8988_ROUTM2, 8, 1, 0),
245         SOC_DAPM_SINGLE("Right Bypass Switch", WM8988_ROUTM2, 7, 1, 0),
246 };
247
248 static const char *wm8988_pga_sel[] = {"Line 1", "Line 2", "Differential"};
249 static const unsigned int wm8988_pga_val[] = { 0, 1, 3 };
250
251 /* Left PGA Mux */
252 static const struct soc_enum wm8988_lpga_enum =
253         SOC_VALUE_ENUM_SINGLE(WM8988_LADCIN, 6, 3,
254                               ARRAY_SIZE(wm8988_pga_sel),
255                               wm8988_pga_sel,
256                               wm8988_pga_val);
257 static const struct snd_kcontrol_new wm8988_left_pga_controls =
258         SOC_DAPM_VALUE_ENUM("Route", wm8988_lpga_enum);
259
260 /* Right PGA Mux */
261 static const struct soc_enum wm8988_rpga_enum =
262         SOC_VALUE_ENUM_SINGLE(WM8988_RADCIN, 6, 3,
263                               ARRAY_SIZE(wm8988_pga_sel),
264                               wm8988_pga_sel,
265                               wm8988_pga_val);
266 static const struct snd_kcontrol_new wm8988_right_pga_controls =
267         SOC_DAPM_VALUE_ENUM("Route", wm8988_rpga_enum);
268
269 /* Differential Mux */
270 static const char *wm8988_diff_sel[] = {"Line 1", "Line 2"};
271 static const struct soc_enum diffmux =
272         SOC_ENUM_SINGLE(WM8988_ADCIN, 8, 2, wm8988_diff_sel);
273 static const struct snd_kcontrol_new wm8988_diffmux_controls =
274         SOC_DAPM_ENUM("Route", diffmux);
275
276 /* Mono ADC Mux */
277 static const char *wm8988_mono_mux[] = {"Stereo", "Mono (Left)",
278         "Mono (Right)", "Digital Mono"};
279 static const struct soc_enum monomux =
280         SOC_ENUM_SINGLE(WM8988_ADCIN, 6, 4, wm8988_mono_mux);
281 static const struct snd_kcontrol_new wm8988_monomux_controls =
282         SOC_DAPM_ENUM("Route", monomux);
283
284 static const struct snd_soc_dapm_widget wm8988_dapm_widgets[] = {
285         SND_SOC_DAPM_MICBIAS("Mic Bias", WM8988_PWR1, 1, 0),
286
287         SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
288                 &wm8988_diffmux_controls),
289         SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
290                 &wm8988_monomux_controls),
291         SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
292                 &wm8988_monomux_controls),
293
294         SND_SOC_DAPM_MUX("Left PGA Mux", WM8988_PWR1, 5, 0,
295                 &wm8988_left_pga_controls),
296         SND_SOC_DAPM_MUX("Right PGA Mux", WM8988_PWR1, 4, 0,
297                 &wm8988_right_pga_controls),
298
299         SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
300                 &wm8988_left_line_controls),
301         SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
302                 &wm8988_right_line_controls),
303
304         SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8988_PWR1, 2, 0),
305         SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8988_PWR1, 3, 0),
306
307         SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8988_PWR2, 7, 0),
308         SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8988_PWR2, 8, 0),
309
310         SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
311                 &wm8988_left_mixer_controls[0],
312                 ARRAY_SIZE(wm8988_left_mixer_controls)),
313         SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
314                 &wm8988_right_mixer_controls[0],
315                 ARRAY_SIZE(wm8988_right_mixer_controls)),
316
317         SND_SOC_DAPM_PGA("Right Out 2", WM8988_PWR2, 3, 0, NULL, 0),
318         SND_SOC_DAPM_PGA("Left Out 2", WM8988_PWR2, 4, 0, NULL, 0),
319         SND_SOC_DAPM_PGA("Right Out 1", WM8988_PWR2, 5, 0, NULL, 0),
320         SND_SOC_DAPM_PGA("Left Out 1", WM8988_PWR2, 6, 0, NULL, 0),
321
322         SND_SOC_DAPM_POST("LRC control", wm8988_lrc_control),
323
324         SND_SOC_DAPM_OUTPUT("LOUT1"),
325         SND_SOC_DAPM_OUTPUT("ROUT1"),
326         SND_SOC_DAPM_OUTPUT("LOUT2"),
327         SND_SOC_DAPM_OUTPUT("ROUT2"),
328         SND_SOC_DAPM_OUTPUT("VREF"),
329
330         SND_SOC_DAPM_INPUT("LINPUT1"),
331         SND_SOC_DAPM_INPUT("LINPUT2"),
332         SND_SOC_DAPM_INPUT("RINPUT1"),
333         SND_SOC_DAPM_INPUT("RINPUT2"),
334 };
335
336 static const struct snd_soc_dapm_route audio_map[] = {
337
338         { "Left Line Mux", "Line 1", "LINPUT1" },
339         { "Left Line Mux", "Line 2", "LINPUT2" },
340         { "Left Line Mux", "PGA", "Left PGA Mux" },
341         { "Left Line Mux", "Differential", "Differential Mux" },
342
343         { "Right Line Mux", "Line 1", "RINPUT1" },
344         { "Right Line Mux", "Line 2", "RINPUT2" },
345         { "Right Line Mux", "PGA", "Right PGA Mux" },
346         { "Right Line Mux", "Differential", "Differential Mux" },
347
348         { "Left PGA Mux", "Line 1", "LINPUT1" },
349         { "Left PGA Mux", "Line 2", "LINPUT2" },
350         { "Left PGA Mux", "Differential", "Differential Mux" },
351
352         { "Right PGA Mux", "Line 1", "RINPUT1" },
353         { "Right PGA Mux", "Line 2", "RINPUT2" },
354         { "Right PGA Mux", "Differential", "Differential Mux" },
355
356         { "Differential Mux", "Line 1", "LINPUT1" },
357         { "Differential Mux", "Line 1", "RINPUT1" },
358         { "Differential Mux", "Line 2", "LINPUT2" },
359         { "Differential Mux", "Line 2", "RINPUT2" },
360
361         { "Left ADC Mux", "Stereo", "Left PGA Mux" },
362         { "Left ADC Mux", "Mono (Left)", "Left PGA Mux" },
363         { "Left ADC Mux", "Digital Mono", "Left PGA Mux" },
364
365         { "Right ADC Mux", "Stereo", "Right PGA Mux" },
366         { "Right ADC Mux", "Mono (Right)", "Right PGA Mux" },
367         { "Right ADC Mux", "Digital Mono", "Right PGA Mux" },
368
369         { "Left ADC", NULL, "Left ADC Mux" },
370         { "Right ADC", NULL, "Right ADC Mux" },
371
372         { "Left Line Mux", "Line 1", "LINPUT1" },
373         { "Left Line Mux", "Line 2", "LINPUT2" },
374         { "Left Line Mux", "PGA", "Left PGA Mux" },
375         { "Left Line Mux", "Differential", "Differential Mux" },
376
377         { "Right Line Mux", "Line 1", "RINPUT1" },
378         { "Right Line Mux", "Line 2", "RINPUT2" },
379         { "Right Line Mux", "PGA", "Right PGA Mux" },
380         { "Right Line Mux", "Differential", "Differential Mux" },
381
382         { "Left Mixer", "Playback Switch", "Left DAC" },
383         { "Left Mixer", "Left Bypass Switch", "Left Line Mux" },
384         { "Left Mixer", "Right Playback Switch", "Right DAC" },
385         { "Left Mixer", "Right Bypass Switch", "Right Line Mux" },
386
387         { "Right Mixer", "Left Playback Switch", "Left DAC" },
388         { "Right Mixer", "Left Bypass Switch", "Left Line Mux" },
389         { "Right Mixer", "Playback Switch", "Right DAC" },
390         { "Right Mixer", "Right Bypass Switch", "Right Line Mux" },
391
392         { "Left Out 1", NULL, "Left Mixer" },
393         { "LOUT1", NULL, "Left Out 1" },
394         { "Right Out 1", NULL, "Right Mixer" },
395         { "ROUT1", NULL, "Right Out 1" },
396
397         { "Left Out 2", NULL, "Left Mixer" },
398         { "LOUT2", NULL, "Left Out 2" },
399         { "Right Out 2", NULL, "Right Mixer" },
400         { "ROUT2", NULL, "Right Out 2" },
401 };
402
403 struct _coeff_div {
404         u32 mclk;
405         u32 rate;
406         u16 fs;
407         u8 sr:5;
408         u8 usb:1;
409 };
410
411 /* codec hifi mclk clock divider coefficients */
412 static const struct _coeff_div coeff_div[] = {
413         /* 8k */
414         {12288000, 8000, 1536, 0x6, 0x0},
415         {11289600, 8000, 1408, 0x16, 0x0},
416         {18432000, 8000, 2304, 0x7, 0x0},
417         {16934400, 8000, 2112, 0x17, 0x0},
418         {12000000, 8000, 1500, 0x6, 0x1},
419
420         /* 11.025k */
421         {11289600, 11025, 1024, 0x18, 0x0},
422         {16934400, 11025, 1536, 0x19, 0x0},
423         {12000000, 11025, 1088, 0x19, 0x1},
424
425         /* 16k */
426         {12288000, 16000, 768, 0xa, 0x0},
427         {18432000, 16000, 1152, 0xb, 0x0},
428         {12000000, 16000, 750, 0xa, 0x1},
429
430         /* 22.05k */
431         {11289600, 22050, 512, 0x1a, 0x0},
432         {16934400, 22050, 768, 0x1b, 0x0},
433         {12000000, 22050, 544, 0x1b, 0x1},
434
435         /* 32k */
436         {12288000, 32000, 384, 0xc, 0x0},
437         {18432000, 32000, 576, 0xd, 0x0},
438         {12000000, 32000, 375, 0xa, 0x1},
439
440         /* 44.1k */
441         {11289600, 44100, 256, 0x10, 0x0},
442         {16934400, 44100, 384, 0x11, 0x0},
443         {12000000, 44100, 272, 0x11, 0x1},
444
445         /* 48k */
446         {12288000, 48000, 256, 0x0, 0x0},
447         {18432000, 48000, 384, 0x1, 0x0},
448         {12000000, 48000, 250, 0x0, 0x1},
449
450         /* 88.2k */
451         {11289600, 88200, 128, 0x1e, 0x0},
452         {16934400, 88200, 192, 0x1f, 0x0},
453         {12000000, 88200, 136, 0x1f, 0x1},
454
455         /* 96k */
456         {12288000, 96000, 128, 0xe, 0x0},
457         {18432000, 96000, 192, 0xf, 0x0},
458         {12000000, 96000, 125, 0xe, 0x1},
459 };
460
461 static inline int get_coeff(int mclk, int rate)
462 {
463         int i;
464
465         for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
466                 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
467                         return i;
468         }
469
470         return -EINVAL;
471 }
472
473 /* The set of rates we can generate from the above for each SYSCLK */
474
475 static unsigned int rates_12288[] = {
476         8000, 12000, 16000, 24000, 24000, 32000, 48000, 96000,
477 };
478
479 static struct snd_pcm_hw_constraint_list constraints_12288 = {
480         .count  = ARRAY_SIZE(rates_12288),
481         .list   = rates_12288,
482 };
483
484 static unsigned int rates_112896[] = {
485         8000, 11025, 22050, 44100,
486 };
487
488 static struct snd_pcm_hw_constraint_list constraints_112896 = {
489         .count  = ARRAY_SIZE(rates_112896),
490         .list   = rates_112896,
491 };
492
493 static unsigned int rates_12[] = {
494         8000, 11025, 12000, 16000, 22050, 2400, 32000, 41100, 48000,
495         48000, 88235, 96000,
496 };
497
498 static struct snd_pcm_hw_constraint_list constraints_12 = {
499         .count  = ARRAY_SIZE(rates_12),
500         .list   = rates_12,
501 };
502
503 /*
504  * Note that this should be called from init rather than from hw_params.
505  */
506 static int wm8988_set_dai_sysclk(struct snd_soc_dai *codec_dai,
507                 int clk_id, unsigned int freq, int dir)
508 {
509         struct snd_soc_codec *codec = codec_dai->codec;
510         struct wm8988_priv *wm8988 = snd_soc_codec_get_drvdata(codec);
511
512         switch (freq) {
513         case 11289600:
514         case 18432000:
515         case 22579200:
516         case 36864000:
517                 wm8988->sysclk_constraints = &constraints_112896;
518                 wm8988->sysclk = freq;
519                 return 0;
520
521         case 12288000:
522         case 16934400:
523         case 24576000:
524         case 33868800:
525                 wm8988->sysclk_constraints = &constraints_12288;
526                 wm8988->sysclk = freq;
527                 return 0;
528
529         case 12000000:
530         case 24000000:
531                 wm8988->sysclk_constraints = &constraints_12;
532                 wm8988->sysclk = freq;
533                 return 0;
534         }
535         return -EINVAL;
536 }
537
538 static int wm8988_set_dai_fmt(struct snd_soc_dai *codec_dai,
539                 unsigned int fmt)
540 {
541         struct snd_soc_codec *codec = codec_dai->codec;
542         u16 iface = 0;
543
544         /* set master/slave audio interface */
545         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
546         case SND_SOC_DAIFMT_CBM_CFM:
547                 iface = 0x0040;
548                 break;
549         case SND_SOC_DAIFMT_CBS_CFS:
550                 break;
551         default:
552                 return -EINVAL;
553         }
554
555         /* interface format */
556         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
557         case SND_SOC_DAIFMT_I2S:
558                 iface |= 0x0002;
559                 break;
560         case SND_SOC_DAIFMT_RIGHT_J:
561                 break;
562         case SND_SOC_DAIFMT_LEFT_J:
563                 iface |= 0x0001;
564                 break;
565         case SND_SOC_DAIFMT_DSP_A:
566                 iface |= 0x0003;
567                 break;
568         case SND_SOC_DAIFMT_DSP_B:
569                 iface |= 0x0013;
570                 break;
571         default:
572                 return -EINVAL;
573         }
574
575         /* clock inversion */
576         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
577         case SND_SOC_DAIFMT_NB_NF:
578                 break;
579         case SND_SOC_DAIFMT_IB_IF:
580                 iface |= 0x0090;
581                 break;
582         case SND_SOC_DAIFMT_IB_NF:
583                 iface |= 0x0080;
584                 break;
585         case SND_SOC_DAIFMT_NB_IF:
586                 iface |= 0x0010;
587                 break;
588         default:
589                 return -EINVAL;
590         }
591
592         DBG("Enter::%s----%d  iface=%x\n",__FUNCTION__,__LINE__,iface);
593         snd_soc_write(codec, WM8988_IFACE, iface);
594         return 0;
595 }
596
597 static int wm8988_pcm_startup(struct snd_pcm_substream *substream,
598                               struct snd_soc_dai *dai)
599 {
600         struct snd_soc_codec *codec = dai->codec;
601         struct wm8988_priv *wm8988 = snd_soc_codec_get_drvdata(codec);
602
603         /* The set of sample rates that can be supported depends on the
604          * MCLK supplied to the CODEC - enforce this.
605          */
606         DBG("Enter::%s----%d  wm8988->sysclk=%d\n",__FUNCTION__,__LINE__,wm8988->sysclk); 
607         if (!wm8988->sysclk) {
608                 dev_err(codec->dev,
609                         "No MCLK configured, call set_sysclk() on init\n");
610                 return -EINVAL;
611         }
612
613         snd_pcm_hw_constraint_list(substream->runtime, 0,
614                                    SNDRV_PCM_HW_PARAM_RATE,
615                                    wm8988->sysclk_constraints);
616
617         return 0;
618 }
619
620 static int wm8988_pcm_hw_params(struct snd_pcm_substream *substream,
621                                 struct snd_pcm_hw_params *params,
622                                 struct snd_soc_dai *dai)
623 {
624         struct snd_soc_pcm_runtime *rtd = substream->private_data;
625         struct snd_soc_device *socdev = rtd->socdev;
626         struct snd_soc_codec *codec = socdev->card->codec;
627         struct wm8988_priv *wm8988 = snd_soc_codec_get_drvdata(codec);
628         u16 iface = snd_soc_read(codec, WM8988_IFACE) & 0x1f3;
629         u16 srate = snd_soc_read(codec, WM8988_SRATE) & 0x180;
630         int coeff;
631         
632         coeff = get_coeff(wm8988->sysclk, params_rate(params));
633         if (coeff < 0) {
634                 coeff = get_coeff(wm8988->sysclk / 2, params_rate(params));
635                 srate |= 0x40;
636         }
637         if (coeff < 0) {
638                 dev_err(codec->dev,
639                         "Unable to configure sample rate %dHz with %dHz MCLK\n",
640                         params_rate(params), wm8988->sysclk);
641                 return coeff;
642         }
643
644         /* bit size */
645         switch (params_format(params)) {
646         case SNDRV_PCM_FORMAT_S16_LE:
647                 break;
648         case SNDRV_PCM_FORMAT_S20_3LE:
649                 iface |= 0x0004;
650                 break;
651         case SNDRV_PCM_FORMAT_S24_LE:
652                 iface |= 0x0008;
653                 break;
654         case SNDRV_PCM_FORMAT_S32_LE:
655                 iface |= 0x000c;
656                 break;
657         }
658         DBG("Enter::%s----%d  iface=%x srate =%x rate=%d\n",__FUNCTION__,__LINE__,iface,srate,params_rate(params));
659
660         /* set iface & srate */
661         snd_soc_write(codec, WM8988_IFACE, iface);
662         if (coeff >= 0)
663                 snd_soc_write(codec, WM8988_SRATE, srate |
664                         (coeff_div[coeff].sr << 1) | coeff_div[coeff].usb);
665
666         return 0;
667 }
668
669 static int wm8988_mute(struct snd_soc_dai *dai, int mute)
670 {
671         struct snd_soc_codec *codec = dai->codec;
672         u16 mute_reg = snd_soc_read(codec, WM8988_ADCDAC) & 0xfff7;
673         DBG("Enter::%s----%d--mute=%d\n",__FUNCTION__,__LINE__,mute);
674         if (mute)
675                 snd_soc_write(codec, WM8988_ADCDAC, mute_reg | 0x8);
676         else
677                 snd_soc_write(codec, WM8988_ADCDAC, mute_reg);
678         return 0;
679 }
680
681 static int wm8988_set_bias_level(struct snd_soc_codec *codec,
682                                  enum snd_soc_bias_level level)
683 {
684         u16 pwr_reg = snd_soc_read(codec, WM8988_PWR1) & ~0x1c1;
685         DBG("Enter::%s----%d level =%d\n",__FUNCTION__,__LINE__,level);
686         switch (level) {
687         case SND_SOC_BIAS_ON:
688                 break;
689
690         case SND_SOC_BIAS_PREPARE:
691                 /* VREF, VMID=2x50k, digital enabled */
692                 snd_soc_write(codec, WM8988_PWR1, pwr_reg | 0x00c0);
693                 break;
694
695         case SND_SOC_BIAS_STANDBY:
696                 if (codec->bias_level == SND_SOC_BIAS_OFF) {
697                         /* VREF, VMID=2x5k */
698                         snd_soc_write(codec, WM8988_PWR1, pwr_reg | 0x1c1);
699
700                         /* Charge caps */
701                         msleep(100);
702                 }
703
704                 /* VREF, VMID=2*500k, digital stopped */
705                 snd_soc_write(codec, WM8988_PWR1, pwr_reg | 0x0141);
706                 break;
707
708         case SND_SOC_BIAS_OFF:
709                 snd_soc_write(codec, WM8988_PWR1, 0x0000);
710                 break;
711         }
712         codec->bias_level = level;
713         return 0;
714 }
715
716 #define WM8988_RATES SNDRV_PCM_RATE_8000_96000
717
718 #define WM8988_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
719         SNDRV_PCM_FMTBIT_S24_LE)
720
721 static struct snd_soc_dai_ops wm8988_ops = {
722         .startup = wm8988_pcm_startup,
723         .hw_params = wm8988_pcm_hw_params,
724         .set_fmt = wm8988_set_dai_fmt,
725         .set_sysclk = wm8988_set_dai_sysclk,
726         .digital_mute = wm8988_mute,
727 };
728
729 struct snd_soc_dai wm8988_dai = {
730         .name = "WM8988",
731         .playback = {
732                 .stream_name = "Playback",
733                 .channels_min = 1,
734                 .channels_max = 2,
735                 .rates = WM8988_RATES,
736                 .formats = WM8988_FORMATS,
737         },
738         .capture = {
739                 .stream_name = "Capture",
740                 .channels_min = 1,
741                 .channels_max = 2,
742                 .rates = WM8988_RATES,
743                 .formats = WM8988_FORMATS,
744          },
745         .ops = &wm8988_ops,
746         .symmetric_rates = 1,
747 };
748 EXPORT_SYMBOL_GPL(wm8988_dai);
749
750 static int wm8988_suspend(struct platform_device *pdev, pm_message_t state)
751 {
752         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
753         struct snd_soc_codec *codec = socdev->card->codec;
754         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
755         wm8988_set_bias_level(codec, SND_SOC_BIAS_OFF);
756         return 0;
757 }
758
759 static int wm8988_resume(struct platform_device *pdev)
760 {
761         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
762         struct snd_soc_codec *codec = socdev->card->codec;
763         int i;
764         u8 data[2];
765         u16 *cache = codec->reg_cache;
766         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
767         /* Sync reg_cache with the hardware */
768         for (i = 0; i < WM8988_NUM_REG; i++) {
769                 if (i == WM8988_RESET)
770                         continue;
771                 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
772                 data[1] = cache[i] & 0x00ff;
773                 codec->hw_write(codec->control_data, data, 2);
774         }
775
776         wm8988_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
777
778         return 0;
779 }
780
781 static struct snd_soc_codec *wm8988_codec;
782
783 static int wm8988_probe(struct platform_device *pdev)
784 {
785         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
786         struct snd_soc_codec *codec;
787         int ret = 0;
788
789         if (wm8988_codec == NULL) {
790                 dev_err(&pdev->dev, "Codec device not registered\n");
791                 return -ENODEV;
792         }
793
794         socdev->card->codec = wm8988_codec;
795         codec = wm8988_codec;
796
797         /* register pcms */
798         ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
799         if (ret < 0) {
800                 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
801                 goto pcm_err;
802         }
803
804         snd_soc_add_controls(codec, wm8988_snd_controls,
805                                 ARRAY_SIZE(wm8988_snd_controls));
806         snd_soc_dapm_new_controls(codec, wm8988_dapm_widgets,
807                                   ARRAY_SIZE(wm8988_dapm_widgets));
808         snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
809
810         return ret;
811
812 pcm_err:
813         return ret;
814 }
815
816 static int wm8988_remove(struct platform_device *pdev)
817 {
818         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
819
820         snd_soc_free_pcms(socdev);
821         snd_soc_dapm_free(socdev);
822
823         return 0;
824 }
825
826 struct snd_soc_codec_device soc_codec_dev_wm8988 = {
827         .probe =        wm8988_probe,
828         .remove =       wm8988_remove,
829         .suspend =      wm8988_suspend,
830         .resume =       wm8988_resume,
831 };
832 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8988);
833
834 static int wm8988_register(struct wm8988_priv *wm8988,
835                            enum snd_soc_control_type control)
836 {
837         struct snd_soc_codec *codec = &wm8988->codec;
838         int ret;
839         u16 reg;
840
841         if (wm8988_codec) {
842                 dev_err(codec->dev, "Another WM8988 is registered\n");
843                 ret = -EINVAL;
844                 goto err;
845         }
846
847         mutex_init(&codec->mutex);
848         INIT_LIST_HEAD(&codec->dapm_widgets);
849         INIT_LIST_HEAD(&codec->dapm_paths);
850
851         snd_soc_codec_set_drvdata(codec, wm8988);
852         codec->name = "WM8988";
853         codec->owner = THIS_MODULE;
854         codec->dai = &wm8988_dai;
855         codec->num_dai = 1;
856         codec->reg_cache_size = ARRAY_SIZE(wm8988->reg_cache);
857         codec->reg_cache = &wm8988->reg_cache;
858         codec->bias_level = SND_SOC_BIAS_OFF;
859         codec->set_bias_level = wm8988_set_bias_level;
860
861         memcpy(codec->reg_cache, wm8988_reg,
862                sizeof(wm8988_reg));
863
864         ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
865         if (ret < 0) {
866                 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
867                 goto err;
868         }
869
870         ret = wm8988_reset(codec);
871         if (ret < 0) {
872                 dev_err(codec->dev, "Failed to issue reset\n");
873                 goto err;
874         }
875 #if 1
876                 /*disable speaker */
877                 gpio_request(RK2818_PIN_PF7, "WM8988"); 
878                 rk2818_mux_api_set(GPIOE_SPI1_FLASH_SEL_NAME, IOMUXA_GPIO1_A3B7);
879                 gpio_direction_output(RK2818_PIN_PF7,GPIO_HIGH);
880                 
881 #endif
882         /* set the update bits (we always update left then right) */
883         reg = snd_soc_read(codec, WM8988_RADC);
884         snd_soc_write(codec, WM8988_RADC, reg | 0x100);
885         reg = snd_soc_read(codec, WM8988_RDAC);
886         snd_soc_write(codec, WM8988_RDAC, reg | 0x0100);
887         reg = snd_soc_read(codec, WM8988_ROUT1V);
888         snd_soc_write(codec, WM8988_ROUT1V, reg | 0x0100);
889         reg = snd_soc_read(codec, WM8988_ROUT2V);
890         snd_soc_write(codec, WM8988_ROUT2V, reg | 0x0100);
891         reg = snd_soc_read(codec, WM8988_RINVOL);
892         snd_soc_write(codec, WM8988_RINVOL, reg | 0x0100); 
893         
894         snd_soc_write(codec, WM8988_LOUTM1, 0x120); 
895     snd_soc_write(codec, WM8988_ROUTM2, 0x120);  
896         snd_soc_write(codec, WM8988_LOUTM2, 0x0070);
897         snd_soc_write(codec, WM8988_ROUTM1, 0x0070);
898         
899         snd_soc_write(codec, WM8988_LOUT1V, 0x017f); 
900         snd_soc_write(codec, WM8988_ROUT1V, 0x017f);
901         snd_soc_write(codec, WM8988_LDAC, 0xff);  
902         snd_soc_write(codec, WM8988_RDAC, 0x1ff);//vol set 
903         
904         snd_soc_write(codec, WM8988_SRATE,0x100);  ///SET MCLK/8
905         snd_soc_write(codec, WM8988_PWR1, 0x1cc);  ///(0x80|0x40|0x20|0x08|0x04|0x10|0x02));
906         snd_soc_write(codec, WM8988_PWR2, 0x1e0);  //power r l out1
907
908
909         wm8988_set_bias_level(&wm8988->codec, SND_SOC_BIAS_STANDBY);
910
911         wm8988_dai.dev = codec->dev;
912
913         wm8988_codec = codec;
914
915         ret = snd_soc_register_codec(codec);
916         if (ret != 0) {
917                 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
918                 goto err;
919         }
920
921         ret = snd_soc_register_dai(&wm8988_dai);
922         if (ret != 0) {
923                 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
924                 goto err_codec;
925         }
926
927         return 0;
928
929 err_codec:
930         snd_soc_unregister_codec(codec);
931 err:
932         kfree(wm8988);
933         return ret;
934 }
935
936 static void wm8988_unregister(struct wm8988_priv *wm8988)
937 {
938         wm8988_set_bias_level(&wm8988->codec, SND_SOC_BIAS_OFF);
939         snd_soc_unregister_dai(&wm8988_dai);
940         snd_soc_unregister_codec(&wm8988->codec);
941         kfree(wm8988);
942         wm8988_codec = NULL;
943 }
944
945 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
946 static int wm8988_i2c_probe(struct i2c_client *i2c,
947                             const struct i2c_device_id *id)
948 {
949         struct wm8988_priv *wm8988;
950         struct snd_soc_codec *codec;
951
952         wm8988 = kzalloc(sizeof(struct wm8988_priv), GFP_KERNEL);
953         if (wm8988 == NULL)
954                 return -ENOMEM;
955
956         codec = &wm8988->codec;
957
958         i2c_set_clientdata(i2c, wm8988);
959         codec->control_data = i2c;
960
961         codec->dev = &i2c->dev;
962
963         return wm8988_register(wm8988, SND_SOC_I2C);
964 }
965
966 static int wm8988_i2c_remove(struct i2c_client *client)
967 {
968         struct wm8988_priv *wm8988 = i2c_get_clientdata(client);
969         wm8988_unregister(wm8988);
970         return 0;
971 }
972
973 static const struct i2c_device_id wm8988_i2c_id[] = {
974         { "wm8988", 0 },
975         { }
976 };
977 MODULE_DEVICE_TABLE(i2c, wm8988_i2c_id);
978
979 static struct i2c_driver wm8988_i2c_driver = {
980         .driver = {
981                 .name = "WM8988",
982                 .owner = THIS_MODULE,
983         },
984         .probe = wm8988_i2c_probe,
985         .remove = wm8988_i2c_remove,
986         .id_table = wm8988_i2c_id,
987 };
988 #endif
989
990 #if defined(CONFIG_SPI_MASTER)
991 static int __devinit wm8988_spi_probe(struct spi_device *spi)
992 {
993         struct wm8988_priv *wm8988;
994         struct snd_soc_codec *codec;
995
996         wm8988 = kzalloc(sizeof(struct wm8988_priv), GFP_KERNEL);
997         if (wm8988 == NULL)
998                 return -ENOMEM;
999
1000         codec = &wm8988->codec;
1001         codec->control_data = spi;
1002         codec->dev = &spi->dev;
1003
1004         dev_set_drvdata(&spi->dev, wm8988);
1005
1006         return wm8988_register(wm8988, SND_SOC_SPI);
1007 }
1008
1009 static int __devexit wm8988_spi_remove(struct spi_device *spi)
1010 {
1011         struct wm8988_priv *wm8988 = dev_get_drvdata(&spi->dev);
1012
1013         wm8988_unregister(wm8988);
1014
1015         return 0;
1016 }
1017
1018 static struct spi_driver wm8988_spi_driver = {
1019         .driver = {
1020                 .name   = "wm8988",
1021                 .bus    = &spi_bus_type,
1022                 .owner  = THIS_MODULE,
1023         },
1024         .probe          = wm8988_spi_probe,
1025         .remove         = __devexit_p(wm8988_spi_remove),
1026 };
1027 #endif
1028
1029 static int __init wm8988_modinit(void)
1030 {
1031         int ret;
1032
1033 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1034         ret = i2c_add_driver(&wm8988_i2c_driver);
1035         if (ret != 0)
1036                 pr_err("WM8988: Unable to register I2C driver: %d\n", ret);
1037 #endif
1038 #if defined(CONFIG_SPI_MASTER)
1039         ret = spi_register_driver(&wm8988_spi_driver);
1040         if (ret != 0)
1041                 pr_err("WM8988: Unable to register SPI driver: %d\n", ret);
1042 #endif
1043         return ret;
1044 }
1045 module_init(wm8988_modinit);
1046
1047 static void __exit wm8988_exit(void)
1048 {
1049 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1050         i2c_del_driver(&wm8988_i2c_driver);
1051 #endif
1052 #if defined(CONFIG_SPI_MASTER)
1053         spi_unregister_driver(&wm8988_spi_driver);
1054 #endif
1055 }
1056 module_exit(wm8988_exit);
1057
1058
1059 MODULE_DESCRIPTION("ASoC WM8988 driver");
1060 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1061 MODULE_LICENSE("GPL");