2 * wm8750.c -- WM8750 ALSA SoC audio driver
4 * Copyright 2005 Openedhand Ltd.
6 * Author: Richard Purdie <richard@openedhand.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
20 #include <linux/i2c.h>
21 #include <linux/platform_device.h>
22 #include <linux/spi/spi.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>
32 #define AUDIO_NAME "WM8750"
33 #define WM8750_VERSION "0.12"
35 /* codec private data */
41 * wm8750 register cache
42 * We can't read the WM8750 register space when we
43 * are using 2 wire for device control, so we cache them instead.
45 static const u16 wm8750_reg[] = {
46 0x0097, 0x0097, 0x0079, 0x0079, /* 0 */
47 0x0000, 0x0008, 0x0000, 0x000a, /* 4 */
48 0x0000, 0x0000, 0x00ff, 0x00ff, /* 8 */
49 0x000f, 0x000f, 0x0000, 0x0000, /* 12 */
50 0x0000, 0x007b, 0x0000, 0x0032, /* 16 */
51 0x0000, 0x00c3, 0x00c3, 0x00c0, /* 20 */
52 0x0000, 0x0000, 0x0000, 0x0000, /* 24 */
53 0x0000, 0x0000, 0x0000, 0x0000, /* 28 */
54 0x0000, 0x0000, 0x0050, 0x0050, /* 32 */
55 0x0050, 0x0050, 0x0050, 0x0050, /* 36 */
56 0x0079, 0x0079, 0x0079, /* 40 */
60 * read wm8750 register cache
62 static inline unsigned int wm8750_read_reg_cache(struct snd_soc_codec *codec,
65 u16 *cache = codec->reg_cache;
66 if (reg > WM8750_CACHE_REGNUM)
72 * write wm8750 register cache
74 static inline void wm8750_write_reg_cache(struct snd_soc_codec *codec,
75 unsigned int reg, unsigned int value)
77 u16 *cache = codec->reg_cache;
78 if (reg > WM8750_CACHE_REGNUM)
83 static int wm8750_write(struct snd_soc_codec *codec, unsigned int reg,
89 * D15..D9 WM8753 register offset
90 * D8...D0 register data
92 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
93 data[1] = value & 0x00ff;
95 wm8750_write_reg_cache(codec, reg, value);
96 if (codec->hw_write(codec->control_data, data, 2) == 2)
102 #define wm8750_reset(c) wm8750_write(c, WM8750_RESET, 0)
107 static const char *wm8750_bass[] = {"Linear Control", "Adaptive Boost"};
108 static const char *wm8750_bass_filter[] = { "130Hz @ 48kHz", "200Hz @ 48kHz" };
109 static const char *wm8750_treble[] = {"8kHz", "4kHz"};
110 static const char *wm8750_3d_lc[] = {"200Hz", "500Hz"};
111 static const char *wm8750_3d_uc[] = {"2.2kHz", "1.5kHz"};
112 static const char *wm8750_3d_func[] = {"Capture", "Playback"};
113 static const char *wm8750_alc_func[] = {"Off", "Right", "Left", "Stereo"};
114 static const char *wm8750_ng_type[] = {"Constant PGA Gain",
116 static const char *wm8750_line_mux[] = {"Line 1", "Line 2", "Line 3", "PGA",
118 static const char *wm8750_pga_sel[] = {"Line 1", "Line 2", "Line 3",
120 static const char *wm8750_out3[] = {"VREF", "ROUT1 + Vol", "MonoOut",
122 static const char *wm8750_diff_sel[] = {"Line 1", "Line 2"};
123 static const char *wm8750_adcpol[] = {"Normal", "L Invert", "R Invert",
125 static const char *wm8750_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
126 static const char *wm8750_mono_mux[] = {"Stereo", "Mono (Left)",
127 "Mono (Right)", "Digital Mono"};
129 static const struct soc_enum wm8750_enum[] = {
130 SOC_ENUM_SINGLE(WM8750_BASS, 7, 2, wm8750_bass),
131 SOC_ENUM_SINGLE(WM8750_BASS, 6, 2, wm8750_bass_filter),
132 SOC_ENUM_SINGLE(WM8750_TREBLE, 6, 2, wm8750_treble),
133 SOC_ENUM_SINGLE(WM8750_3D, 5, 2, wm8750_3d_lc),
134 SOC_ENUM_SINGLE(WM8750_3D, 6, 2, wm8750_3d_uc),
135 SOC_ENUM_SINGLE(WM8750_3D, 7, 2, wm8750_3d_func),
136 SOC_ENUM_SINGLE(WM8750_ALC1, 7, 4, wm8750_alc_func),
137 SOC_ENUM_SINGLE(WM8750_NGATE, 1, 2, wm8750_ng_type),
138 SOC_ENUM_SINGLE(WM8750_LOUTM1, 0, 5, wm8750_line_mux),
139 SOC_ENUM_SINGLE(WM8750_ROUTM1, 0, 5, wm8750_line_mux),
140 SOC_ENUM_SINGLE(WM8750_LADCIN, 6, 4, wm8750_pga_sel), /* 10 */
141 SOC_ENUM_SINGLE(WM8750_RADCIN, 6, 4, wm8750_pga_sel),
142 SOC_ENUM_SINGLE(WM8750_ADCTL2, 7, 4, wm8750_out3),
143 SOC_ENUM_SINGLE(WM8750_ADCIN, 8, 2, wm8750_diff_sel),
144 SOC_ENUM_SINGLE(WM8750_ADCDAC, 5, 4, wm8750_adcpol),
145 SOC_ENUM_SINGLE(WM8750_ADCDAC, 1, 4, wm8750_deemph),
146 SOC_ENUM_SINGLE(WM8750_ADCIN, 6, 4, wm8750_mono_mux), /* 16 */
150 static const struct snd_kcontrol_new wm8750_snd_controls[] = {
152 SOC_DOUBLE_R("Capture Volume", WM8750_LINVOL, WM8750_RINVOL, 0, 63, 0),
153 SOC_DOUBLE_R("Capture ZC Switch", WM8750_LINVOL, WM8750_RINVOL, 6, 1, 0),
154 SOC_DOUBLE_R("Capture Switch", WM8750_LINVOL, WM8750_RINVOL, 7, 1, 1),
156 SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8750_LOUT1V,
157 WM8750_ROUT1V, 7, 1, 0),
158 SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8750_LOUT2V,
159 WM8750_ROUT2V, 7, 1, 0),
161 SOC_ENUM("Playback De-emphasis", wm8750_enum[15]),
163 SOC_ENUM("Capture Polarity", wm8750_enum[14]),
164 SOC_SINGLE("Playback 6dB Attenuate", WM8750_ADCDAC, 7, 1, 0),
165 SOC_SINGLE("Capture 6dB Attenuate", WM8750_ADCDAC, 8, 1, 0),
167 SOC_DOUBLE_R("PCM Volume", WM8750_LDAC, WM8750_RDAC, 0, 255, 0),
169 SOC_ENUM("Bass Boost", wm8750_enum[0]),
170 SOC_ENUM("Bass Filter", wm8750_enum[1]),
171 SOC_SINGLE("Bass Volume", WM8750_BASS, 0, 15, 1),
173 SOC_SINGLE("Treble Volume", WM8750_TREBLE, 0, 15, 1),
174 SOC_ENUM("Treble Cut-off", wm8750_enum[2]),
176 SOC_SINGLE("3D Switch", WM8750_3D, 0, 1, 0),
177 SOC_SINGLE("3D Volume", WM8750_3D, 1, 15, 0),
178 SOC_ENUM("3D Lower Cut-off", wm8750_enum[3]),
179 SOC_ENUM("3D Upper Cut-off", wm8750_enum[4]),
180 SOC_ENUM("3D Mode", wm8750_enum[5]),
182 SOC_SINGLE("ALC Capture Target Volume", WM8750_ALC1, 0, 7, 0),
183 SOC_SINGLE("ALC Capture Max Volume", WM8750_ALC1, 4, 7, 0),
184 SOC_ENUM("ALC Capture Function", wm8750_enum[6]),
185 SOC_SINGLE("ALC Capture ZC Switch", WM8750_ALC2, 7, 1, 0),
186 SOC_SINGLE("ALC Capture Hold Time", WM8750_ALC2, 0, 15, 0),
187 SOC_SINGLE("ALC Capture Decay Time", WM8750_ALC3, 4, 15, 0),
188 SOC_SINGLE("ALC Capture Attack Time", WM8750_ALC3, 0, 15, 0),
189 SOC_SINGLE("ALC Capture NG Threshold", WM8750_NGATE, 3, 31, 0),
190 SOC_ENUM("ALC Capture NG Type", wm8750_enum[4]),
191 SOC_SINGLE("ALC Capture NG Switch", WM8750_NGATE, 0, 1, 0),
193 SOC_SINGLE("Left ADC Capture Volume", WM8750_LADC, 0, 255, 0),
194 SOC_SINGLE("Right ADC Capture Volume", WM8750_RADC, 0, 255, 0),
196 SOC_SINGLE("ZC Timeout Switch", WM8750_ADCTL1, 0, 1, 0),
197 SOC_SINGLE("Playback Invert Switch", WM8750_ADCTL1, 1, 1, 0),
199 SOC_SINGLE("Right Speaker Playback Invert Switch", WM8750_ADCTL2, 4, 1, 0),
202 /* ADCDAC Bit 0 - ADCHPD */
203 /* ADCDAC Bit 4 - HPOR */
204 /* ADCTL1 Bit 2,3 - DATSEL */
205 /* ADCTL1 Bit 4,5 - DMONOMIX */
206 /* ADCTL1 Bit 6,7 - VSEL */
207 /* ADCTL2 Bit 2 - LRCM */
208 /* ADCTL2 Bit 3 - TRI */
209 /* ADCTL3 Bit 5 - HPFLREN */
210 /* ADCTL3 Bit 6 - VROI */
211 /* ADCTL3 Bit 7,8 - ADCLRM */
212 /* ADCIN Bit 4 - LDCM */
213 /* ADCIN Bit 5 - RDCM */
215 SOC_DOUBLE_R("Mic Boost", WM8750_LADCIN, WM8750_RADCIN, 4, 3, 0),
217 SOC_DOUBLE_R("Bypass Left Playback Volume", WM8750_LOUTM1,
218 WM8750_LOUTM2, 4, 7, 1),
219 SOC_DOUBLE_R("Bypass Right Playback Volume", WM8750_ROUTM1,
220 WM8750_ROUTM2, 4, 7, 1),
221 SOC_DOUBLE_R("Bypass Mono Playback Volume", WM8750_MOUTM1,
222 WM8750_MOUTM2, 4, 7, 1),
224 SOC_SINGLE("Mono Playback ZC Switch", WM8750_MOUTV, 7, 1, 0),
226 SOC_DOUBLE_R("Headphone Playback Volume", WM8750_LOUT1V, WM8750_ROUT1V,
228 SOC_DOUBLE_R("Speaker Playback Volume", WM8750_LOUT2V, WM8750_ROUT2V,
231 SOC_SINGLE("Mono Playback Volume", WM8750_MOUTV, 0, 127, 0),
235 /* add non dapm controls */
236 static int wm8750_add_controls(struct snd_soc_codec *codec)
240 for (i = 0; i < ARRAY_SIZE(wm8750_snd_controls); i++) {
241 err = snd_ctl_add(codec->card,
242 snd_soc_cnew(&wm8750_snd_controls[i],
255 static const struct snd_kcontrol_new wm8750_left_mixer_controls[] = {
256 SOC_DAPM_SINGLE("Playback Switch", WM8750_LOUTM1, 8, 1, 0),
257 SOC_DAPM_SINGLE("Left Bypass Switch", WM8750_LOUTM1, 7, 1, 0),
258 SOC_DAPM_SINGLE("Right Playback Switch", WM8750_LOUTM2, 8, 1, 0),
259 SOC_DAPM_SINGLE("Right Bypass Switch", WM8750_LOUTM2, 7, 1, 0),
263 static const struct snd_kcontrol_new wm8750_right_mixer_controls[] = {
264 SOC_DAPM_SINGLE("Left Playback Switch", WM8750_ROUTM1, 8, 1, 0),
265 SOC_DAPM_SINGLE("Left Bypass Switch", WM8750_ROUTM1, 7, 1, 0),
266 SOC_DAPM_SINGLE("Playback Switch", WM8750_ROUTM2, 8, 1, 0),
267 SOC_DAPM_SINGLE("Right Bypass Switch", WM8750_ROUTM2, 7, 1, 0),
271 static const struct snd_kcontrol_new wm8750_mono_mixer_controls[] = {
272 SOC_DAPM_SINGLE("Left Playback Switch", WM8750_MOUTM1, 8, 1, 0),
273 SOC_DAPM_SINGLE("Left Bypass Switch", WM8750_MOUTM1, 7, 1, 0),
274 SOC_DAPM_SINGLE("Right Playback Switch", WM8750_MOUTM2, 8, 1, 0),
275 SOC_DAPM_SINGLE("Right Bypass Switch", WM8750_MOUTM2, 7, 1, 0),
279 static const struct snd_kcontrol_new wm8750_left_line_controls =
280 SOC_DAPM_ENUM("Route", wm8750_enum[8]);
283 static const struct snd_kcontrol_new wm8750_right_line_controls =
284 SOC_DAPM_ENUM("Route", wm8750_enum[9]);
287 static const struct snd_kcontrol_new wm8750_left_pga_controls =
288 SOC_DAPM_ENUM("Route", wm8750_enum[10]);
291 static const struct snd_kcontrol_new wm8750_right_pga_controls =
292 SOC_DAPM_ENUM("Route", wm8750_enum[11]);
295 static const struct snd_kcontrol_new wm8750_out3_controls =
296 SOC_DAPM_ENUM("Route", wm8750_enum[12]);
298 /* Differential Mux */
299 static const struct snd_kcontrol_new wm8750_diffmux_controls =
300 SOC_DAPM_ENUM("Route", wm8750_enum[13]);
303 static const struct snd_kcontrol_new wm8750_monomux_controls =
304 SOC_DAPM_ENUM("Route", wm8750_enum[16]);
306 static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = {
307 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
308 &wm8750_left_mixer_controls[0],
309 ARRAY_SIZE(wm8750_left_mixer_controls)),
310 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
311 &wm8750_right_mixer_controls[0],
312 ARRAY_SIZE(wm8750_right_mixer_controls)),
313 SND_SOC_DAPM_MIXER("Mono Mixer", WM8750_PWR2, 2, 0,
314 &wm8750_mono_mixer_controls[0],
315 ARRAY_SIZE(wm8750_mono_mixer_controls)),
317 SND_SOC_DAPM_PGA("Right Out 2", WM8750_PWR2, 3, 0, NULL, 0),
318 SND_SOC_DAPM_PGA("Left Out 2", WM8750_PWR2, 4, 0, NULL, 0),
319 SND_SOC_DAPM_PGA("Right Out 1", WM8750_PWR2, 5, 0, NULL, 0),
320 SND_SOC_DAPM_PGA("Left Out 1", WM8750_PWR2, 6, 0, NULL, 0),
321 SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8750_PWR2, 7, 0),
322 SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8750_PWR2, 8, 0),
324 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8750_PWR1, 1, 0),
325 SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8750_PWR1, 2, 0),
326 SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8750_PWR1, 3, 0),
328 SND_SOC_DAPM_MUX("Left PGA Mux", WM8750_PWR1, 5, 0,
329 &wm8750_left_pga_controls),
330 SND_SOC_DAPM_MUX("Right PGA Mux", WM8750_PWR1, 4, 0,
331 &wm8750_right_pga_controls),
332 SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
333 &wm8750_left_line_controls),
334 SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
335 &wm8750_right_line_controls),
337 SND_SOC_DAPM_MUX("Out3 Mux", SND_SOC_NOPM, 0, 0, &wm8750_out3_controls),
338 SND_SOC_DAPM_PGA("Out 3", WM8750_PWR2, 1, 0, NULL, 0),
339 SND_SOC_DAPM_PGA("Mono Out 1", WM8750_PWR2, 2, 0, NULL, 0),
341 SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
342 &wm8750_diffmux_controls),
343 SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
344 &wm8750_monomux_controls),
345 SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
346 &wm8750_monomux_controls),
348 SND_SOC_DAPM_OUTPUT("LOUT1"),
349 SND_SOC_DAPM_OUTPUT("ROUT1"),
350 SND_SOC_DAPM_OUTPUT("LOUT2"),
351 SND_SOC_DAPM_OUTPUT("ROUT2"),
352 SND_SOC_DAPM_OUTPUT("MONO1"),
353 SND_SOC_DAPM_OUTPUT("OUT3"),
354 SND_SOC_DAPM_OUTPUT("VREF"),
356 SND_SOC_DAPM_INPUT("LINPUT1"),
357 SND_SOC_DAPM_INPUT("LINPUT2"),
358 SND_SOC_DAPM_INPUT("LINPUT3"),
359 SND_SOC_DAPM_INPUT("RINPUT1"),
360 SND_SOC_DAPM_INPUT("RINPUT2"),
361 SND_SOC_DAPM_INPUT("RINPUT3"),
364 static const struct snd_soc_dapm_route audio_map[] = {
366 {"Left Mixer", "Playback Switch", "Left DAC"},
367 {"Left Mixer", "Left Bypass Switch", "Left Line Mux"},
368 {"Left Mixer", "Right Playback Switch", "Right DAC"},
369 {"Left Mixer", "Right Bypass Switch", "Right Line Mux"},
372 {"Right Mixer", "Left Playback Switch", "Left DAC"},
373 {"Right Mixer", "Left Bypass Switch", "Left Line Mux"},
374 {"Right Mixer", "Playback Switch", "Right DAC"},
375 {"Right Mixer", "Right Bypass Switch", "Right Line Mux"},
378 {"Left Out 1", NULL, "Left Mixer"},
379 {"LOUT1", NULL, "Left Out 1"},
382 {"Left Out 2", NULL, "Left Mixer"},
383 {"LOUT2", NULL, "Left Out 2"},
386 {"Right Out 1", NULL, "Right Mixer"},
387 {"ROUT1", NULL, "Right Out 1"},
390 {"Right Out 2", NULL, "Right Mixer"},
391 {"ROUT2", NULL, "Right Out 2"},
394 {"Mono Mixer", "Left Playback Switch", "Left DAC"},
395 {"Mono Mixer", "Left Bypass Switch", "Left Line Mux"},
396 {"Mono Mixer", "Right Playback Switch", "Right DAC"},
397 {"Mono Mixer", "Right Bypass Switch", "Right Line Mux"},
400 {"Mono Out 1", NULL, "Mono Mixer"},
401 {"MONO1", NULL, "Mono Out 1"},
404 {"Out3 Mux", "VREF", "VREF"},
405 {"Out3 Mux", "ROUT1 + Vol", "ROUT1"},
406 {"Out3 Mux", "ROUT1", "Right Mixer"},
407 {"Out3 Mux", "MonoOut", "MONO1"},
408 {"Out 3", NULL, "Out3 Mux"},
409 {"OUT3", NULL, "Out 3"},
412 {"Left Line Mux", "Line 1", "LINPUT1"},
413 {"Left Line Mux", "Line 2", "LINPUT2"},
414 {"Left Line Mux", "Line 3", "LINPUT3"},
415 {"Left Line Mux", "PGA", "Left PGA Mux"},
416 {"Left Line Mux", "Differential", "Differential Mux"},
419 {"Right Line Mux", "Line 1", "RINPUT1"},
420 {"Right Line Mux", "Line 2", "RINPUT2"},
421 {"Right Line Mux", "Line 3", "RINPUT3"},
422 {"Right Line Mux", "PGA", "Right PGA Mux"},
423 {"Right Line Mux", "Differential", "Differential Mux"},
426 {"Left PGA Mux", "Line 1", "LINPUT1"},
427 {"Left PGA Mux", "Line 2", "LINPUT2"},
428 {"Left PGA Mux", "Line 3", "LINPUT3"},
429 {"Left PGA Mux", "Differential", "Differential Mux"},
432 {"Right PGA Mux", "Line 1", "RINPUT1"},
433 {"Right PGA Mux", "Line 2", "RINPUT2"},
434 {"Right PGA Mux", "Line 3", "RINPUT3"},
435 {"Right PGA Mux", "Differential", "Differential Mux"},
437 /* Differential Mux */
438 {"Differential Mux", "Line 1", "LINPUT1"},
439 {"Differential Mux", "Line 1", "RINPUT1"},
440 {"Differential Mux", "Line 2", "LINPUT2"},
441 {"Differential Mux", "Line 2", "RINPUT2"},
444 {"Left ADC Mux", "Stereo", "Left PGA Mux"},
445 {"Left ADC Mux", "Mono (Left)", "Left PGA Mux"},
446 {"Left ADC Mux", "Digital Mono", "Left PGA Mux"},
449 {"Right ADC Mux", "Stereo", "Right PGA Mux"},
450 {"Right ADC Mux", "Mono (Right)", "Right PGA Mux"},
451 {"Right ADC Mux", "Digital Mono", "Right PGA Mux"},
454 {"Left ADC", NULL, "Left ADC Mux"},
455 {"Right ADC", NULL, "Right ADC Mux"},
458 static int wm8750_add_widgets(struct snd_soc_codec *codec)
460 snd_soc_dapm_new_controls(codec, wm8750_dapm_widgets,
461 ARRAY_SIZE(wm8750_dapm_widgets));
463 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
465 snd_soc_dapm_new_widgets(codec);
477 /* codec hifi mclk clock divider coefficients */
478 static const struct _coeff_div coeff_div[] = {
480 {12288000, 8000, 1536, 0x6, 0x0},
481 {11289600, 8000, 1408, 0x16, 0x0},
482 {18432000, 8000, 2304, 0x7, 0x0},
483 {16934400, 8000, 2112, 0x17, 0x0},
484 {12000000, 8000, 1500, 0x6, 0x1},
487 {11289600, 11025, 1024, 0x18, 0x0},
488 {16934400, 11025, 1536, 0x19, 0x0},
489 {12000000, 11025, 1088, 0x19, 0x1},
492 {12288000, 16000, 768, 0xa, 0x0},
493 {18432000, 16000, 1152, 0xb, 0x0},
494 {12000000, 16000, 750, 0xa, 0x1},
497 {11289600, 22050, 512, 0x1a, 0x0},
498 {16934400, 22050, 768, 0x1b, 0x0},
499 {12000000, 22050, 544, 0x1b, 0x1},
502 {12288000, 32000, 384, 0xc, 0x0},
503 {18432000, 32000, 576, 0xd, 0x0},
504 {12000000, 32000, 375, 0xa, 0x1},
507 {11289600, 44100, 256, 0x10, 0x0},
508 {16934400, 44100, 384, 0x11, 0x0},
509 {12000000, 44100, 272, 0x11, 0x1},
512 {12288000, 48000, 256, 0x0, 0x0},
513 {18432000, 48000, 384, 0x1, 0x0},
514 {12000000, 48000, 250, 0x0, 0x1},
517 {11289600, 88200, 128, 0x1e, 0x0},
518 {16934400, 88200, 192, 0x1f, 0x0},
519 {12000000, 88200, 136, 0x1f, 0x1},
522 {12288000, 96000, 128, 0xe, 0x0},
523 {18432000, 96000, 192, 0xf, 0x0},
524 {12000000, 96000, 125, 0xe, 0x1},
527 static inline int get_coeff(int mclk, int rate)
531 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
532 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
536 printk(KERN_ERR "wm8750: could not get coeff for mclk %d @ rate %d\n",
541 static int wm8750_set_dai_sysclk(struct snd_soc_dai *codec_dai,
542 int clk_id, unsigned int freq, int dir)
544 struct snd_soc_codec *codec = codec_dai->codec;
545 struct wm8750_priv *wm8750 = codec->private_data;
553 wm8750->sysclk = freq;
559 static int wm8750_set_dai_fmt(struct snd_soc_dai *codec_dai,
562 struct snd_soc_codec *codec = codec_dai->codec;
565 /* set master/slave audio interface */
566 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
567 case SND_SOC_DAIFMT_CBM_CFM:
570 case SND_SOC_DAIFMT_CBS_CFS:
576 /* interface format */
577 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
578 case SND_SOC_DAIFMT_I2S:
581 case SND_SOC_DAIFMT_RIGHT_J:
583 case SND_SOC_DAIFMT_LEFT_J:
586 case SND_SOC_DAIFMT_DSP_A:
589 case SND_SOC_DAIFMT_DSP_B:
596 /* clock inversion */
597 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
598 case SND_SOC_DAIFMT_NB_NF:
600 case SND_SOC_DAIFMT_IB_IF:
603 case SND_SOC_DAIFMT_IB_NF:
606 case SND_SOC_DAIFMT_NB_IF:
613 wm8750_write(codec, WM8750_IFACE, iface);
617 static int wm8750_pcm_hw_params(struct snd_pcm_substream *substream,
618 struct snd_pcm_hw_params *params)
620 struct snd_soc_pcm_runtime *rtd = substream->private_data;
621 struct snd_soc_device *socdev = rtd->socdev;
622 struct snd_soc_codec *codec = socdev->codec;
623 struct wm8750_priv *wm8750 = codec->private_data;
624 u16 iface = wm8750_read_reg_cache(codec, WM8750_IFACE) & 0x1f3;
625 u16 srate = wm8750_read_reg_cache(codec, WM8750_SRATE) & 0x1c0;
626 int coeff = get_coeff(wm8750->sysclk, params_rate(params));
629 switch (params_format(params)) {
630 case SNDRV_PCM_FORMAT_S16_LE:
632 case SNDRV_PCM_FORMAT_S20_3LE:
635 case SNDRV_PCM_FORMAT_S24_LE:
638 case SNDRV_PCM_FORMAT_S32_LE:
643 /* set iface & srate */
644 wm8750_write(codec, WM8750_IFACE, iface);
646 wm8750_write(codec, WM8750_SRATE, srate |
647 (coeff_div[coeff].sr << 1) | coeff_div[coeff].usb);
652 static int wm8750_mute(struct snd_soc_dai *dai, int mute)
654 struct snd_soc_codec *codec = dai->codec;
655 u16 mute_reg = wm8750_read_reg_cache(codec, WM8750_ADCDAC) & 0xfff7;
658 wm8750_write(codec, WM8750_ADCDAC, mute_reg | 0x8);
660 wm8750_write(codec, WM8750_ADCDAC, mute_reg);
664 static int wm8750_set_bias_level(struct snd_soc_codec *codec,
665 enum snd_soc_bias_level level)
667 u16 pwr_reg = wm8750_read_reg_cache(codec, WM8750_PWR1) & 0xfe3e;
670 case SND_SOC_BIAS_ON:
671 /* set vmid to 50k and unmute dac */
672 wm8750_write(codec, WM8750_PWR1, pwr_reg | 0x00c0);
674 case SND_SOC_BIAS_PREPARE:
675 /* set vmid to 5k for quick power up */
676 wm8750_write(codec, WM8750_PWR1, pwr_reg | 0x01c1);
678 case SND_SOC_BIAS_STANDBY:
679 /* mute dac and set vmid to 500k, enable VREF */
680 wm8750_write(codec, WM8750_PWR1, pwr_reg | 0x0141);
682 case SND_SOC_BIAS_OFF:
683 wm8750_write(codec, WM8750_PWR1, 0x0001);
686 codec->bias_level = level;
690 #define WM8750_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
691 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
692 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
694 #define WM8750_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
695 SNDRV_PCM_FMTBIT_S24_LE)
697 struct snd_soc_dai wm8750_dai = {
700 .stream_name = "Playback",
703 .rates = WM8750_RATES,
704 .formats = WM8750_FORMATS,},
706 .stream_name = "Capture",
709 .rates = WM8750_RATES,
710 .formats = WM8750_FORMATS,},
712 .hw_params = wm8750_pcm_hw_params,
715 .digital_mute = wm8750_mute,
716 .set_fmt = wm8750_set_dai_fmt,
717 .set_sysclk = wm8750_set_dai_sysclk,
720 EXPORT_SYMBOL_GPL(wm8750_dai);
722 static void wm8750_work(struct work_struct *work)
724 struct snd_soc_codec *codec =
725 container_of(work, struct snd_soc_codec, delayed_work.work);
726 wm8750_set_bias_level(codec, codec->bias_level);
729 static int wm8750_suspend(struct platform_device *pdev, pm_message_t state)
731 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
732 struct snd_soc_codec *codec = socdev->codec;
734 wm8750_set_bias_level(codec, SND_SOC_BIAS_OFF);
738 static int wm8750_resume(struct platform_device *pdev)
740 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
741 struct snd_soc_codec *codec = socdev->codec;
744 u16 *cache = codec->reg_cache;
746 /* Sync reg_cache with the hardware */
747 for (i = 0; i < ARRAY_SIZE(wm8750_reg); i++) {
748 if (i == WM8750_RESET)
750 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
751 data[1] = cache[i] & 0x00ff;
752 codec->hw_write(codec->control_data, data, 2);
755 wm8750_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
757 /* charge wm8750 caps */
758 if (codec->suspend_bias_level == SND_SOC_BIAS_ON) {
759 wm8750_set_bias_level(codec, SND_SOC_BIAS_PREPARE);
760 codec->bias_level = SND_SOC_BIAS_ON;
761 schedule_delayed_work(&codec->delayed_work,
762 msecs_to_jiffies(1000));
769 * initialise the WM8750 driver
770 * register the mixer and dsp interfaces with the kernel
772 static int wm8750_init(struct snd_soc_device *socdev)
774 struct snd_soc_codec *codec = socdev->codec;
777 codec->name = "WM8750";
778 codec->owner = THIS_MODULE;
779 codec->read = wm8750_read_reg_cache;
780 codec->write = wm8750_write;
781 codec->set_bias_level = wm8750_set_bias_level;
782 codec->dai = &wm8750_dai;
784 codec->reg_cache_size = ARRAY_SIZE(wm8750_reg);
785 codec->reg_cache = kmemdup(wm8750_reg, sizeof(wm8750_reg), GFP_KERNEL);
786 if (codec->reg_cache == NULL)
792 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
794 printk(KERN_ERR "wm8750: failed to create pcms\n");
798 /* charge output caps */
799 wm8750_set_bias_level(codec, SND_SOC_BIAS_PREPARE);
800 codec->bias_level = SND_SOC_BIAS_STANDBY;
801 schedule_delayed_work(&codec->delayed_work, msecs_to_jiffies(1000));
803 /* set the update bits */
804 reg = wm8750_read_reg_cache(codec, WM8750_LDAC);
805 wm8750_write(codec, WM8750_LDAC, reg | 0x0100);
806 reg = wm8750_read_reg_cache(codec, WM8750_RDAC);
807 wm8750_write(codec, WM8750_RDAC, reg | 0x0100);
808 reg = wm8750_read_reg_cache(codec, WM8750_LOUT1V);
809 wm8750_write(codec, WM8750_LOUT1V, reg | 0x0100);
810 reg = wm8750_read_reg_cache(codec, WM8750_ROUT1V);
811 wm8750_write(codec, WM8750_ROUT1V, reg | 0x0100);
812 reg = wm8750_read_reg_cache(codec, WM8750_LOUT2V);
813 wm8750_write(codec, WM8750_LOUT2V, reg | 0x0100);
814 reg = wm8750_read_reg_cache(codec, WM8750_ROUT2V);
815 wm8750_write(codec, WM8750_ROUT2V, reg | 0x0100);
816 reg = wm8750_read_reg_cache(codec, WM8750_LINVOL);
817 wm8750_write(codec, WM8750_LINVOL, reg | 0x0100);
818 reg = wm8750_read_reg_cache(codec, WM8750_RINVOL);
819 wm8750_write(codec, WM8750_RINVOL, reg | 0x0100);
821 wm8750_add_controls(codec);
822 wm8750_add_widgets(codec);
823 ret = snd_soc_register_card(socdev);
825 printk(KERN_ERR "wm8750: failed to register card\n");
831 snd_soc_free_pcms(socdev);
832 snd_soc_dapm_free(socdev);
834 kfree(codec->reg_cache);
838 /* If the i2c layer weren't so broken, we could pass this kind of data
840 static struct snd_soc_device *wm8750_socdev;
842 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
845 * WM8750 2 wire address is determined by GPIO5
846 * state during powerup.
851 static int wm8750_i2c_probe(struct i2c_client *i2c,
852 const struct i2c_device_id *id)
854 struct snd_soc_device *socdev = wm8750_socdev;
855 struct snd_soc_codec *codec = socdev->codec;
858 i2c_set_clientdata(i2c, codec);
859 codec->control_data = i2c;
861 ret = wm8750_init(socdev);
863 pr_err("failed to initialise WM8750\n");
868 static int wm8750_i2c_remove(struct i2c_client *client)
870 struct snd_soc_codec *codec = i2c_get_clientdata(client);
871 kfree(codec->reg_cache);
875 static const struct i2c_device_id wm8750_i2c_id[] = {
879 MODULE_DEVICE_TABLE(i2c, wm8750_i2c_id);
881 static struct i2c_driver wm8750_i2c_driver = {
883 .name = "WM8750 I2C Codec",
884 .owner = THIS_MODULE,
886 .probe = wm8750_i2c_probe,
887 .remove = wm8750_i2c_remove,
888 .id_table = wm8750_i2c_id,
891 static int wm8750_add_i2c_device(struct platform_device *pdev,
892 const struct wm8750_setup_data *setup)
894 struct i2c_board_info info;
895 struct i2c_adapter *adapter;
896 struct i2c_client *client;
899 ret = i2c_add_driver(&wm8750_i2c_driver);
901 dev_err(&pdev->dev, "can't add i2c driver\n");
905 memset(&info, 0, sizeof(struct i2c_board_info));
906 info.addr = setup->i2c_address;
907 strlcpy(info.type, "wm8750", I2C_NAME_SIZE);
909 adapter = i2c_get_adapter(setup->i2c_bus);
911 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
916 client = i2c_new_device(adapter, &info);
917 i2c_put_adapter(adapter);
919 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
920 (unsigned int)info.addr);
927 i2c_del_driver(&wm8750_i2c_driver);
932 #if defined(CONFIG_SPI_MASTER)
933 static int __devinit wm8750_spi_probe(struct spi_device *spi)
935 struct snd_soc_device *socdev = wm8750_socdev;
936 struct snd_soc_codec *codec = socdev->codec;
939 codec->control_data = spi;
941 ret = wm8750_init(socdev);
943 dev_err(&spi->dev, "failed to initialise WM8750\n");
948 static int __devexit wm8750_spi_remove(struct spi_device *spi)
953 static struct spi_driver wm8750_spi_driver = {
956 .bus = &spi_bus_type,
957 .owner = THIS_MODULE,
959 .probe = wm8750_spi_probe,
960 .remove = __devexit_p(wm8750_spi_remove),
963 static int wm8750_spi_write(struct spi_device *spi, const char *data, int len)
965 struct spi_transfer t;
966 struct spi_message m;
975 spi_message_init(&m);
976 memset(&t, 0, (sizeof t));
981 spi_message_add_tail(&t, &m);
988 static int wm8750_probe(struct platform_device *pdev)
990 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
991 struct wm8750_setup_data *setup = socdev->codec_data;
992 struct snd_soc_codec *codec;
993 struct wm8750_priv *wm8750;
996 pr_info("WM8750 Audio Codec %s", WM8750_VERSION);
997 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
1001 wm8750 = kzalloc(sizeof(struct wm8750_priv), GFP_KERNEL);
1002 if (wm8750 == NULL) {
1007 codec->private_data = wm8750;
1008 socdev->codec = codec;
1009 mutex_init(&codec->mutex);
1010 INIT_LIST_HEAD(&codec->dapm_widgets);
1011 INIT_LIST_HEAD(&codec->dapm_paths);
1012 wm8750_socdev = socdev;
1013 INIT_DELAYED_WORK(&codec->delayed_work, wm8750_work);
1017 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1018 if (setup->i2c_address) {
1019 codec->hw_write = (hw_write_t)i2c_master_send;
1020 ret = wm8750_add_i2c_device(pdev, setup);
1023 #if defined(CONFIG_SPI_MASTER)
1025 codec->hw_write = (hw_write_t)wm8750_spi_write;
1026 ret = spi_register_driver(&wm8750_spi_driver);
1028 printk(KERN_ERR "can't add spi driver");
1033 kfree(codec->private_data);
1040 * This function forces any delayed work to be queued and run.
1042 static int run_delayed_work(struct delayed_work *dwork)
1046 /* cancel any work waiting to be queued. */
1047 ret = cancel_delayed_work(dwork);
1049 /* if there was any work waiting then we run it now and
1050 * wait for it's completion */
1052 schedule_delayed_work(dwork, 0);
1053 flush_scheduled_work();
1058 /* power down chip */
1059 static int wm8750_remove(struct platform_device *pdev)
1061 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1062 struct snd_soc_codec *codec = socdev->codec;
1064 if (codec->control_data)
1065 wm8750_set_bias_level(codec, SND_SOC_BIAS_OFF);
1066 run_delayed_work(&codec->delayed_work);
1067 snd_soc_free_pcms(socdev);
1068 snd_soc_dapm_free(socdev);
1069 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1070 i2c_unregister_device(codec->control_data);
1071 i2c_del_driver(&wm8750_i2c_driver);
1073 #if defined(CONFIG_SPI_MASTER)
1074 spi_unregister_driver(&wm8750_spi_driver);
1076 kfree(codec->private_data);
1082 struct snd_soc_codec_device soc_codec_dev_wm8750 = {
1083 .probe = wm8750_probe,
1084 .remove = wm8750_remove,
1085 .suspend = wm8750_suspend,
1086 .resume = wm8750_resume,
1088 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8750);
1090 MODULE_DESCRIPTION("ASoC WM8750 driver");
1091 MODULE_AUTHOR("Liam Girdwood");
1092 MODULE_LICENSE("GPL");