2 * wm8753.c -- WM8753 ALSA Soc Audio driver
4 * Copyright 2003 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood
6 * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 * The WM8753 is a low power, high quality stereo codec with integrated PCM
15 * codec designed for portable digital telephony applications.
19 * This driver support 2 DAI PCM's. This makes the default PCM available for
20 * HiFi audio (e.g. MP3, ogg) playback/capture and the other PCM available for
23 * Please note that the voice PCM can be connected directly to a Bluetooth
24 * codec or GSM modem and thus cannot be read or written to, although it is
25 * available to be configured with snd_hw_params(), etc and kcontrols in the
28 * Fast DAI switching:-
30 * The driver can now fast switch between the DAI configurations via a
31 * an alsa kcontrol. This allows the PCM to remain open.
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/kernel.h>
38 #include <linux/init.h>
39 #include <linux/delay.h>
41 #include <linux/i2c.h>
42 #include <linux/platform_device.h>
43 #include <linux/spi/spi.h>
44 #include <sound/core.h>
45 #include <sound/pcm.h>
46 #include <sound/pcm_params.h>
47 #include <sound/soc.h>
48 #include <sound/soc-dapm.h>
49 #include <sound/initval.h>
50 #include <sound/tlv.h>
51 #include <asm/div64.h>
55 #define WM8753_VERSION "0.16"
57 static int caps_charge = 2000;
58 module_param(caps_charge, int, 0);
59 MODULE_PARM_DESC(caps_charge, "WM8753 cap charge time (msecs)");
61 static void wm8753_set_dai_mode(struct snd_soc_codec *codec,
64 /* codec private data */
71 * wm8753 register cache
72 * We can't read the WM8753 register space when we
73 * are using 2 wire for device control, so we cache them instead.
75 static const u16 wm8753_reg[] = {
76 0x0008, 0x0000, 0x000a, 0x000a,
77 0x0033, 0x0000, 0x0007, 0x00ff,
78 0x00ff, 0x000f, 0x000f, 0x007b,
79 0x0000, 0x0032, 0x0000, 0x00c3,
80 0x00c3, 0x00c0, 0x0000, 0x0000,
81 0x0000, 0x0000, 0x0000, 0x0000,
82 0x0000, 0x0000, 0x0000, 0x0000,
83 0x0000, 0x0000, 0x0000, 0x0055,
84 0x0005, 0x0050, 0x0055, 0x0050,
85 0x0055, 0x0050, 0x0055, 0x0079,
86 0x0079, 0x0079, 0x0079, 0x0079,
87 0x0000, 0x0000, 0x0000, 0x0000,
88 0x0097, 0x0097, 0x0000, 0x0004,
89 0x0000, 0x0083, 0x0024, 0x01ba,
90 0x0000, 0x0083, 0x0024, 0x01ba,
95 * read wm8753 register cache
97 static inline unsigned int wm8753_read_reg_cache(struct snd_soc_codec *codec,
100 u16 *cache = codec->reg_cache;
101 if (reg < 1 || reg > (ARRAY_SIZE(wm8753_reg) + 1))
103 return cache[reg - 1];
107 * write wm8753 register cache
109 static inline void wm8753_write_reg_cache(struct snd_soc_codec *codec,
110 unsigned int reg, unsigned int value)
112 u16 *cache = codec->reg_cache;
113 if (reg < 1 || reg > 0x3f)
115 cache[reg - 1] = value;
119 * write to the WM8753 register space
121 static int wm8753_write(struct snd_soc_codec *codec, unsigned int reg,
127 * D15..D9 WM8753 register offset
128 * D8...D0 register data
130 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
131 data[1] = value & 0x00ff;
133 wm8753_write_reg_cache(codec, reg, value);
134 if (codec->hw_write(codec->control_data, data, 2) == 2)
140 #define wm8753_reset(c) wm8753_write(c, WM8753_RESET, 0)
145 static const char *wm8753_base[] = {"Linear Control", "Adaptive Boost"};
146 static const char *wm8753_base_filter[] =
147 {"130Hz @ 48kHz", "200Hz @ 48kHz", "100Hz @ 16kHz", "400Hz @ 48kHz",
148 "100Hz @ 8kHz", "200Hz @ 8kHz"};
149 static const char *wm8753_treble[] = {"8kHz", "4kHz"};
150 static const char *wm8753_alc_func[] = {"Off", "Right", "Left", "Stereo"};
151 static const char *wm8753_ng_type[] = {"Constant PGA Gain", "Mute ADC Output"};
152 static const char *wm8753_3d_func[] = {"Capture", "Playback"};
153 static const char *wm8753_3d_uc[] = {"2.2kHz", "1.5kHz"};
154 static const char *wm8753_3d_lc[] = {"200Hz", "500Hz"};
155 static const char *wm8753_deemp[] = {"None", "32kHz", "44.1kHz", "48kHz"};
156 static const char *wm8753_mono_mix[] = {"Stereo", "Left", "Right", "Mono"};
157 static const char *wm8753_dac_phase[] = {"Non Inverted", "Inverted"};
158 static const char *wm8753_line_mix[] = {"Line 1 + 2", "Line 1 - 2",
160 static const char *wm8753_mono_mux[] = {"Line Mix", "Rx Mix"};
161 static const char *wm8753_right_mux[] = {"Line 2", "Rx Mix"};
162 static const char *wm8753_left_mux[] = {"Line 1", "Rx Mix"};
163 static const char *wm8753_rxmsel[] = {"RXP - RXN", "RXP + RXN", "RXP", "RXN"};
164 static const char *wm8753_sidetone_mux[] = {"Left PGA", "Mic 1", "Mic 2",
166 static const char *wm8753_mono2_src[] = {"Inverted Mono 1", "Left", "Right",
168 static const char *wm8753_out3[] = {"VREF", "ROUT2", "Left + Right"};
169 static const char *wm8753_out4[] = {"VREF", "Capture ST", "LOUT2"};
170 static const char *wm8753_radcsel[] = {"PGA", "Line or RXP-RXN", "Sidetone"};
171 static const char *wm8753_ladcsel[] = {"PGA", "Line or RXP-RXN", "Line"};
172 static const char *wm8753_mono_adc[] = {"Stereo", "Analogue Mix Left",
173 "Analogue Mix Right", "Digital Mono Mix"};
174 static const char *wm8753_adc_hp[] = {"3.4Hz @ 48kHz", "82Hz @ 16k",
175 "82Hz @ 8kHz", "170Hz @ 8kHz"};
176 static const char *wm8753_adc_filter[] = {"HiFi", "Voice"};
177 static const char *wm8753_mic_sel[] = {"Mic 1", "Mic 2", "Mic 3"};
178 static const char *wm8753_dai_mode[] = {"DAI 0", "DAI 1", "DAI 2", "DAI 3"};
179 static const char *wm8753_dat_sel[] = {"Stereo", "Left ADC", "Right ADC",
181 static const char *wm8753_rout2_phase[] = {"Non Inverted", "Inverted"};
183 static const struct soc_enum wm8753_enum[] = {
184 SOC_ENUM_SINGLE(WM8753_BASS, 7, 2, wm8753_base),
185 SOC_ENUM_SINGLE(WM8753_BASS, 4, 6, wm8753_base_filter),
186 SOC_ENUM_SINGLE(WM8753_TREBLE, 6, 2, wm8753_treble),
187 SOC_ENUM_SINGLE(WM8753_ALC1, 7, 4, wm8753_alc_func),
188 SOC_ENUM_SINGLE(WM8753_NGATE, 1, 2, wm8753_ng_type),
189 SOC_ENUM_SINGLE(WM8753_3D, 7, 2, wm8753_3d_func),
190 SOC_ENUM_SINGLE(WM8753_3D, 6, 2, wm8753_3d_uc),
191 SOC_ENUM_SINGLE(WM8753_3D, 5, 2, wm8753_3d_lc),
192 SOC_ENUM_SINGLE(WM8753_DAC, 1, 4, wm8753_deemp),
193 SOC_ENUM_SINGLE(WM8753_DAC, 4, 4, wm8753_mono_mix),
194 SOC_ENUM_SINGLE(WM8753_DAC, 6, 2, wm8753_dac_phase),
195 SOC_ENUM_SINGLE(WM8753_INCTL1, 3, 4, wm8753_line_mix),
196 SOC_ENUM_SINGLE(WM8753_INCTL1, 2, 2, wm8753_mono_mux),
197 SOC_ENUM_SINGLE(WM8753_INCTL1, 1, 2, wm8753_right_mux),
198 SOC_ENUM_SINGLE(WM8753_INCTL1, 0, 2, wm8753_left_mux),
199 SOC_ENUM_SINGLE(WM8753_INCTL2, 6, 4, wm8753_rxmsel),
200 SOC_ENUM_SINGLE(WM8753_INCTL2, 4, 4, wm8753_sidetone_mux),
201 SOC_ENUM_SINGLE(WM8753_OUTCTL, 7, 4, wm8753_mono2_src),
202 SOC_ENUM_SINGLE(WM8753_OUTCTL, 0, 3, wm8753_out3),
203 SOC_ENUM_SINGLE(WM8753_ADCTL2, 7, 3, wm8753_out4),
204 SOC_ENUM_SINGLE(WM8753_ADCIN, 2, 3, wm8753_radcsel),
205 SOC_ENUM_SINGLE(WM8753_ADCIN, 0, 3, wm8753_ladcsel),
206 SOC_ENUM_SINGLE(WM8753_ADCIN, 4, 4, wm8753_mono_adc),
207 SOC_ENUM_SINGLE(WM8753_ADC, 2, 4, wm8753_adc_hp),
208 SOC_ENUM_SINGLE(WM8753_ADC, 4, 2, wm8753_adc_filter),
209 SOC_ENUM_SINGLE(WM8753_MICBIAS, 6, 3, wm8753_mic_sel),
210 SOC_ENUM_SINGLE(WM8753_IOCTL, 2, 4, wm8753_dai_mode),
211 SOC_ENUM_SINGLE(WM8753_ADC, 7, 4, wm8753_dat_sel),
212 SOC_ENUM_SINGLE(WM8753_OUTCTL, 2, 2, wm8753_rout2_phase),
216 static int wm8753_get_dai(struct snd_kcontrol *kcontrol,
217 struct snd_ctl_elem_value *ucontrol)
219 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
220 int mode = wm8753_read_reg_cache(codec, WM8753_IOCTL);
222 ucontrol->value.integer.value[0] = (mode & 0xc) >> 2;
226 static int wm8753_set_dai(struct snd_kcontrol *kcontrol,
227 struct snd_ctl_elem_value *ucontrol)
229 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
230 int mode = wm8753_read_reg_cache(codec, WM8753_IOCTL);
232 if (((mode & 0xc) >> 2) == ucontrol->value.integer.value[0])
236 mode |= (ucontrol->value.integer.value[0] << 2);
238 wm8753_write(codec, WM8753_IOCTL, mode);
239 wm8753_set_dai_mode(codec, ucontrol->value.integer.value[0]);
243 static const DECLARE_TLV_DB_SCALE(rec_mix_tlv, -1500, 300, 0);
244 static const DECLARE_TLV_DB_SCALE(mic_preamp_tlv, 1200, 600, 0);
245 static const DECLARE_TLV_DB_SCALE(adc_tlv, -9750, 50, 1);
246 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
247 static const unsigned int out_tlv[] = {
248 TLV_DB_RANGE_HEAD(2),
249 /* 0000000 - 0101111 = "Analogue mute" */
250 0, 48, TLV_DB_SCALE_ITEM(-25500, 0, 0),
251 48, 127, TLV_DB_SCALE_ITEM(-7300, 100, 0),
253 static const DECLARE_TLV_DB_SCALE(mix_tlv, -1500, 300, 0);
254 static const DECLARE_TLV_DB_SCALE(voice_mix_tlv, -1200, 300, 0);
255 static const DECLARE_TLV_DB_SCALE(pga_tlv, -1725, 75, 0);
257 static const struct snd_kcontrol_new wm8753_snd_controls[] = {
258 SOC_DOUBLE_R_TLV("PCM Volume", WM8753_LDAC, WM8753_RDAC, 0, 255, 0, dac_tlv),
260 SOC_DOUBLE_R_TLV("ADC Capture Volume", WM8753_LADC, WM8753_RADC, 0, 255, 0,
263 SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8753_LOUT1V, WM8753_ROUT1V,
265 SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8753_LOUT2V, WM8753_ROUT2V, 0,
268 SOC_SINGLE_TLV("Mono Playback Volume", WM8753_MOUTV, 0, 127, 0, out_tlv),
270 SOC_DOUBLE_R_TLV("Bypass Playback Volume", WM8753_LOUTM1, WM8753_ROUTM1, 4, 7,
272 SOC_DOUBLE_R_TLV("Sidetone Playback Volume", WM8753_LOUTM2, WM8753_ROUTM2, 4,
274 SOC_DOUBLE_R_TLV("Voice Playback Volume", WM8753_LOUTM2, WM8753_ROUTM2, 0, 7,
277 SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8753_LOUT1V, WM8753_ROUT1V, 7,
279 SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8753_LOUT2V, WM8753_ROUT2V, 7,
282 SOC_SINGLE_TLV("Mono Bypass Playback Volume", WM8753_MOUTM1, 4, 7, 1, mix_tlv),
283 SOC_SINGLE_TLV("Mono Sidetone Playback Volume", WM8753_MOUTM2, 4, 7, 1,
285 SOC_SINGLE_TLV("Mono Voice Playback Volume", WM8753_MOUTM2, 0, 7, 1,
287 SOC_SINGLE("Mono Playback ZC Switch", WM8753_MOUTV, 7, 1, 0),
289 SOC_ENUM("Bass Boost", wm8753_enum[0]),
290 SOC_ENUM("Bass Filter", wm8753_enum[1]),
291 SOC_SINGLE("Bass Volume", WM8753_BASS, 0, 15, 1),
293 SOC_SINGLE("Treble Volume", WM8753_TREBLE, 0, 15, 1),
294 SOC_ENUM("Treble Cut-off", wm8753_enum[2]),
296 SOC_DOUBLE_TLV("Sidetone Capture Volume", WM8753_RECMIX1, 0, 4, 7, 1,
298 SOC_SINGLE_TLV("Voice Sidetone Capture Volume", WM8753_RECMIX2, 0, 7, 1,
301 SOC_DOUBLE_R_TLV("Capture Volume", WM8753_LINVOL, WM8753_RINVOL, 0, 63, 0,
303 SOC_DOUBLE_R("Capture ZC Switch", WM8753_LINVOL, WM8753_RINVOL, 6, 1, 0),
304 SOC_DOUBLE_R("Capture Switch", WM8753_LINVOL, WM8753_RINVOL, 7, 1, 1),
306 SOC_ENUM("Capture Filter Select", wm8753_enum[23]),
307 SOC_ENUM("Capture Filter Cut-off", wm8753_enum[24]),
308 SOC_SINGLE("Capture Filter Switch", WM8753_ADC, 0, 1, 1),
310 SOC_SINGLE("ALC Capture Target Volume", WM8753_ALC1, 0, 7, 0),
311 SOC_SINGLE("ALC Capture Max Volume", WM8753_ALC1, 4, 7, 0),
312 SOC_ENUM("ALC Capture Function", wm8753_enum[3]),
313 SOC_SINGLE("ALC Capture ZC Switch", WM8753_ALC2, 8, 1, 0),
314 SOC_SINGLE("ALC Capture Hold Time", WM8753_ALC2, 0, 15, 1),
315 SOC_SINGLE("ALC Capture Decay Time", WM8753_ALC3, 4, 15, 1),
316 SOC_SINGLE("ALC Capture Attack Time", WM8753_ALC3, 0, 15, 0),
317 SOC_SINGLE("ALC Capture NG Threshold", WM8753_NGATE, 3, 31, 0),
318 SOC_ENUM("ALC Capture NG Type", wm8753_enum[4]),
319 SOC_SINGLE("ALC Capture NG Switch", WM8753_NGATE, 0, 1, 0),
321 SOC_ENUM("3D Function", wm8753_enum[5]),
322 SOC_ENUM("3D Upper Cut-off", wm8753_enum[6]),
323 SOC_ENUM("3D Lower Cut-off", wm8753_enum[7]),
324 SOC_SINGLE("3D Volume", WM8753_3D, 1, 15, 0),
325 SOC_SINGLE("3D Switch", WM8753_3D, 0, 1, 0),
327 SOC_SINGLE("Capture 6dB Attenuate", WM8753_ADCTL1, 2, 1, 0),
328 SOC_SINGLE("Playback 6dB Attenuate", WM8753_ADCTL1, 1, 1, 0),
330 SOC_ENUM("De-emphasis", wm8753_enum[8]),
331 SOC_ENUM("Playback Mono Mix", wm8753_enum[9]),
332 SOC_ENUM("Playback Phase", wm8753_enum[10]),
334 SOC_SINGLE_TLV("Mic2 Capture Volume", WM8753_INCTL1, 7, 3, 0, mic_preamp_tlv),
335 SOC_SINGLE_TLV("Mic1 Capture Volume", WM8753_INCTL1, 5, 3, 0, mic_preamp_tlv),
337 SOC_ENUM_EXT("DAI Mode", wm8753_enum[26], wm8753_get_dai, wm8753_set_dai),
339 SOC_ENUM("ADC Data Select", wm8753_enum[27]),
340 SOC_ENUM("ROUT2 Phase", wm8753_enum[28]),
343 /* add non dapm controls */
344 static int wm8753_add_controls(struct snd_soc_codec *codec)
348 for (i = 0; i < ARRAY_SIZE(wm8753_snd_controls); i++) {
349 err = snd_ctl_add(codec->card,
350 snd_soc_cnew(&wm8753_snd_controls[i],
363 static const struct snd_kcontrol_new wm8753_left_mixer_controls[] = {
364 SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_LOUTM2, 8, 1, 0),
365 SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_LOUTM2, 7, 1, 0),
366 SOC_DAPM_SINGLE("Left Playback Switch", WM8753_LOUTM1, 8, 1, 0),
367 SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_LOUTM1, 7, 1, 0),
371 static const struct snd_kcontrol_new wm8753_right_mixer_controls[] = {
372 SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_ROUTM2, 8, 1, 0),
373 SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_ROUTM2, 7, 1, 0),
374 SOC_DAPM_SINGLE("Right Playback Switch", WM8753_ROUTM1, 8, 1, 0),
375 SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_ROUTM1, 7, 1, 0),
379 static const struct snd_kcontrol_new wm8753_mono_mixer_controls[] = {
380 SOC_DAPM_SINGLE("Left Playback Switch", WM8753_MOUTM1, 8, 1, 0),
381 SOC_DAPM_SINGLE("Right Playback Switch", WM8753_MOUTM2, 8, 1, 0),
382 SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_MOUTM2, 3, 1, 0),
383 SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_MOUTM2, 7, 1, 0),
384 SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_MOUTM1, 7, 1, 0),
388 static const struct snd_kcontrol_new wm8753_mono2_controls =
389 SOC_DAPM_ENUM("Route", wm8753_enum[17]);
392 static const struct snd_kcontrol_new wm8753_out3_controls =
393 SOC_DAPM_ENUM("Route", wm8753_enum[18]);
396 static const struct snd_kcontrol_new wm8753_out4_controls =
397 SOC_DAPM_ENUM("Route", wm8753_enum[19]);
400 static const struct snd_kcontrol_new wm8753_adc_mono_controls =
401 SOC_DAPM_ENUM("Route", wm8753_enum[22]);
404 static const struct snd_kcontrol_new wm8753_record_mixer_controls[] = {
405 SOC_DAPM_SINGLE("Voice Capture Switch", WM8753_RECMIX2, 3, 1, 0),
406 SOC_DAPM_SINGLE("Left Capture Switch", WM8753_RECMIX1, 3, 1, 0),
407 SOC_DAPM_SINGLE("Right Capture Switch", WM8753_RECMIX1, 7, 1, 0),
411 static const struct snd_kcontrol_new wm8753_adc_left_controls =
412 SOC_DAPM_ENUM("Route", wm8753_enum[21]);
415 static const struct snd_kcontrol_new wm8753_adc_right_controls =
416 SOC_DAPM_ENUM("Route", wm8753_enum[20]);
419 static const struct snd_kcontrol_new wm8753_mic_mux_controls =
420 SOC_DAPM_ENUM("Route", wm8753_enum[16]);
423 static const struct snd_kcontrol_new wm8753_alc_mixer_controls[] = {
424 SOC_DAPM_SINGLE("Line Capture Switch", WM8753_INCTL2, 3, 1, 0),
425 SOC_DAPM_SINGLE("Mic2 Capture Switch", WM8753_INCTL2, 2, 1, 0),
426 SOC_DAPM_SINGLE("Mic1 Capture Switch", WM8753_INCTL2, 1, 1, 0),
427 SOC_DAPM_SINGLE("Rx Capture Switch", WM8753_INCTL2, 0, 1, 0),
431 static const struct snd_kcontrol_new wm8753_line_left_controls =
432 SOC_DAPM_ENUM("Route", wm8753_enum[14]);
435 static const struct snd_kcontrol_new wm8753_line_right_controls =
436 SOC_DAPM_ENUM("Route", wm8753_enum[13]);
439 static const struct snd_kcontrol_new wm8753_line_mono_controls =
440 SOC_DAPM_ENUM("Route", wm8753_enum[12]);
442 /* Line mux and mixer */
443 static const struct snd_kcontrol_new wm8753_line_mux_mix_controls =
444 SOC_DAPM_ENUM("Route", wm8753_enum[11]);
446 /* Rx mux and mixer */
447 static const struct snd_kcontrol_new wm8753_rx_mux_mix_controls =
448 SOC_DAPM_ENUM("Route", wm8753_enum[15]);
450 /* Mic Selector Mux */
451 static const struct snd_kcontrol_new wm8753_mic_sel_mux_controls =
452 SOC_DAPM_ENUM("Route", wm8753_enum[25]);
454 static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
455 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8753_PWR1, 5, 0),
456 SND_SOC_DAPM_MIXER("Left Mixer", WM8753_PWR4, 0, 0,
457 &wm8753_left_mixer_controls[0], ARRAY_SIZE(wm8753_left_mixer_controls)),
458 SND_SOC_DAPM_PGA("Left Out 1", WM8753_PWR3, 8, 0, NULL, 0),
459 SND_SOC_DAPM_PGA("Left Out 2", WM8753_PWR3, 6, 0, NULL, 0),
460 SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", WM8753_PWR1, 3, 0),
461 SND_SOC_DAPM_OUTPUT("LOUT1"),
462 SND_SOC_DAPM_OUTPUT("LOUT2"),
463 SND_SOC_DAPM_MIXER("Right Mixer", WM8753_PWR4, 1, 0,
464 &wm8753_right_mixer_controls[0], ARRAY_SIZE(wm8753_right_mixer_controls)),
465 SND_SOC_DAPM_PGA("Right Out 1", WM8753_PWR3, 7, 0, NULL, 0),
466 SND_SOC_DAPM_PGA("Right Out 2", WM8753_PWR3, 5, 0, NULL, 0),
467 SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", WM8753_PWR1, 2, 0),
468 SND_SOC_DAPM_OUTPUT("ROUT1"),
469 SND_SOC_DAPM_OUTPUT("ROUT2"),
470 SND_SOC_DAPM_MIXER("Mono Mixer", WM8753_PWR4, 2, 0,
471 &wm8753_mono_mixer_controls[0], ARRAY_SIZE(wm8753_mono_mixer_controls)),
472 SND_SOC_DAPM_PGA("Mono Out 1", WM8753_PWR3, 2, 0, NULL, 0),
473 SND_SOC_DAPM_PGA("Mono Out 2", WM8753_PWR3, 1, 0, NULL, 0),
474 SND_SOC_DAPM_DAC("Voice DAC", "Voice Playback", WM8753_PWR1, 4, 0),
475 SND_SOC_DAPM_OUTPUT("MONO1"),
476 SND_SOC_DAPM_MUX("Mono 2 Mux", SND_SOC_NOPM, 0, 0, &wm8753_mono2_controls),
477 SND_SOC_DAPM_OUTPUT("MONO2"),
478 SND_SOC_DAPM_MIXER("Out3 Left + Right", -1, 0, 0, NULL, 0),
479 SND_SOC_DAPM_MUX("Out3 Mux", SND_SOC_NOPM, 0, 0, &wm8753_out3_controls),
480 SND_SOC_DAPM_PGA("Out 3", WM8753_PWR3, 4, 0, NULL, 0),
481 SND_SOC_DAPM_OUTPUT("OUT3"),
482 SND_SOC_DAPM_MUX("Out4 Mux", SND_SOC_NOPM, 0, 0, &wm8753_out4_controls),
483 SND_SOC_DAPM_PGA("Out 4", WM8753_PWR3, 3, 0, NULL, 0),
484 SND_SOC_DAPM_OUTPUT("OUT4"),
485 SND_SOC_DAPM_MIXER("Playback Mixer", WM8753_PWR4, 3, 0,
486 &wm8753_record_mixer_controls[0],
487 ARRAY_SIZE(wm8753_record_mixer_controls)),
488 SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8753_PWR2, 3, 0),
489 SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8753_PWR2, 2, 0),
490 SND_SOC_DAPM_MUX("Capture Left Mixer", SND_SOC_NOPM, 0, 0,
491 &wm8753_adc_mono_controls),
492 SND_SOC_DAPM_MUX("Capture Right Mixer", SND_SOC_NOPM, 0, 0,
493 &wm8753_adc_mono_controls),
494 SND_SOC_DAPM_MUX("Capture Left Mux", SND_SOC_NOPM, 0, 0,
495 &wm8753_adc_left_controls),
496 SND_SOC_DAPM_MUX("Capture Right Mux", SND_SOC_NOPM, 0, 0,
497 &wm8753_adc_right_controls),
498 SND_SOC_DAPM_MUX("Mic Sidetone Mux", SND_SOC_NOPM, 0, 0,
499 &wm8753_mic_mux_controls),
500 SND_SOC_DAPM_PGA("Left Capture Volume", WM8753_PWR2, 5, 0, NULL, 0),
501 SND_SOC_DAPM_PGA("Right Capture Volume", WM8753_PWR2, 4, 0, NULL, 0),
502 SND_SOC_DAPM_MIXER("ALC Mixer", WM8753_PWR2, 6, 0,
503 &wm8753_alc_mixer_controls[0], ARRAY_SIZE(wm8753_alc_mixer_controls)),
504 SND_SOC_DAPM_MUX("Line Left Mux", SND_SOC_NOPM, 0, 0,
505 &wm8753_line_left_controls),
506 SND_SOC_DAPM_MUX("Line Right Mux", SND_SOC_NOPM, 0, 0,
507 &wm8753_line_right_controls),
508 SND_SOC_DAPM_MUX("Line Mono Mux", SND_SOC_NOPM, 0, 0,
509 &wm8753_line_mono_controls),
510 SND_SOC_DAPM_MUX("Line Mixer", WM8753_PWR2, 0, 0,
511 &wm8753_line_mux_mix_controls),
512 SND_SOC_DAPM_MUX("Rx Mixer", WM8753_PWR2, 1, 0,
513 &wm8753_rx_mux_mix_controls),
514 SND_SOC_DAPM_PGA("Mic 1 Volume", WM8753_PWR2, 8, 0, NULL, 0),
515 SND_SOC_DAPM_PGA("Mic 2 Volume", WM8753_PWR2, 7, 0, NULL, 0),
516 SND_SOC_DAPM_MUX("Mic Selection Mux", SND_SOC_NOPM, 0, 0,
517 &wm8753_mic_sel_mux_controls),
518 SND_SOC_DAPM_INPUT("LINE1"),
519 SND_SOC_DAPM_INPUT("LINE2"),
520 SND_SOC_DAPM_INPUT("RXP"),
521 SND_SOC_DAPM_INPUT("RXN"),
522 SND_SOC_DAPM_INPUT("ACIN"),
523 SND_SOC_DAPM_OUTPUT("ACOP"),
524 SND_SOC_DAPM_INPUT("MIC1N"),
525 SND_SOC_DAPM_INPUT("MIC1"),
526 SND_SOC_DAPM_INPUT("MIC2N"),
527 SND_SOC_DAPM_INPUT("MIC2"),
528 SND_SOC_DAPM_VMID("VREF"),
531 static const struct snd_soc_dapm_route audio_map[] = {
533 {"Left Mixer", "Left Playback Switch", "Left DAC"},
534 {"Left Mixer", "Voice Playback Switch", "Voice DAC"},
535 {"Left Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
536 {"Left Mixer", "Bypass Playback Switch", "Line Left Mux"},
539 {"Right Mixer", "Right Playback Switch", "Right DAC"},
540 {"Right Mixer", "Voice Playback Switch", "Voice DAC"},
541 {"Right Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
542 {"Right Mixer", "Bypass Playback Switch", "Line Right Mux"},
545 {"Mono Mixer", "Voice Playback Switch", "Voice DAC"},
546 {"Mono Mixer", "Left Playback Switch", "Left DAC"},
547 {"Mono Mixer", "Right Playback Switch", "Right DAC"},
548 {"Mono Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
549 {"Mono Mixer", "Bypass Playback Switch", "Line Mono Mux"},
552 {"Left Out 1", NULL, "Left Mixer"},
553 {"Left Out 2", NULL, "Left Mixer"},
554 {"LOUT1", NULL, "Left Out 1"},
555 {"LOUT2", NULL, "Left Out 2"},
558 {"Right Out 1", NULL, "Right Mixer"},
559 {"Right Out 2", NULL, "Right Mixer"},
560 {"ROUT1", NULL, "Right Out 1"},
561 {"ROUT2", NULL, "Right Out 2"},
564 {"Mono Out 1", NULL, "Mono Mixer"},
565 {"MONO1", NULL, "Mono Out 1"},
568 {"Mono 2 Mux", "Left + Right", "Out3 Left + Right"},
569 {"Mono 2 Mux", "Inverted Mono 1", "MONO1"},
570 {"Mono 2 Mux", "Left", "Left Mixer"},
571 {"Mono 2 Mux", "Right", "Right Mixer"},
572 {"Mono Out 2", NULL, "Mono 2 Mux"},
573 {"MONO2", NULL, "Mono Out 2"},
576 {"Out3 Left + Right", NULL, "Left Mixer"},
577 {"Out3 Left + Right", NULL, "Right Mixer"},
578 {"Out3 Mux", "VREF", "VREF"},
579 {"Out3 Mux", "Left + Right", "Out3 Left + Right"},
580 {"Out3 Mux", "ROUT2", "ROUT2"},
581 {"Out 3", NULL, "Out3 Mux"},
582 {"OUT3", NULL, "Out 3"},
585 {"Out4 Mux", "VREF", "VREF"},
586 {"Out4 Mux", "Capture ST", "Playback Mixer"},
587 {"Out4 Mux", "LOUT2", "LOUT2"},
588 {"Out 4", NULL, "Out4 Mux"},
589 {"OUT4", NULL, "Out 4"},
592 {"Playback Mixer", "Left Capture Switch", "Left Mixer"},
593 {"Playback Mixer", "Voice Capture Switch", "Mono Mixer"},
594 {"Playback Mixer", "Right Capture Switch", "Right Mixer"},
596 /* Mic/SideTone Mux */
597 {"Mic Sidetone Mux", "Left PGA", "Left Capture Volume"},
598 {"Mic Sidetone Mux", "Right PGA", "Right Capture Volume"},
599 {"Mic Sidetone Mux", "Mic 1", "Mic 1 Volume"},
600 {"Mic Sidetone Mux", "Mic 2", "Mic 2 Volume"},
602 /* Capture Left Mux */
603 {"Capture Left Mux", "PGA", "Left Capture Volume"},
604 {"Capture Left Mux", "Line or RXP-RXN", "Line Left Mux"},
605 {"Capture Left Mux", "Line", "LINE1"},
607 /* Capture Right Mux */
608 {"Capture Right Mux", "PGA", "Right Capture Volume"},
609 {"Capture Right Mux", "Line or RXP-RXN", "Line Right Mux"},
610 {"Capture Right Mux", "Sidetone", "Playback Mixer"},
612 /* Mono Capture mixer-mux */
613 {"Capture Right Mixer", "Stereo", "Capture Right Mux"},
614 {"Capture Left Mixer", "Analogue Mix Left", "Capture Left Mux"},
615 {"Capture Left Mixer", "Analogue Mix Left", "Capture Right Mux"},
616 {"Capture Right Mixer", "Analogue Mix Right", "Capture Left Mux"},
617 {"Capture Right Mixer", "Analogue Mix Right", "Capture Right Mux"},
618 {"Capture Left Mixer", "Digital Mono Mix", "Capture Left Mux"},
619 {"Capture Left Mixer", "Digital Mono Mix", "Capture Right Mux"},
620 {"Capture Right Mixer", "Digital Mono Mix", "Capture Left Mux"},
621 {"Capture Right Mixer", "Digital Mono Mix", "Capture Right Mux"},
624 {"Left ADC", NULL, "Capture Left Mixer"},
625 {"Right ADC", NULL, "Capture Right Mixer"},
627 /* Left Capture Volume */
628 {"Left Capture Volume", NULL, "ACIN"},
630 /* Right Capture Volume */
631 {"Right Capture Volume", NULL, "Mic 2 Volume"},
634 {"ALC Mixer", "Line Capture Switch", "Line Mixer"},
635 {"ALC Mixer", "Mic2 Capture Switch", "Mic 2 Volume"},
636 {"ALC Mixer", "Mic1 Capture Switch", "Mic 1 Volume"},
637 {"ALC Mixer", "Rx Capture Switch", "Rx Mixer"},
640 {"Line Left Mux", "Line 1", "LINE1"},
641 {"Line Left Mux", "Rx Mix", "Rx Mixer"},
644 {"Line Right Mux", "Line 2", "LINE2"},
645 {"Line Right Mux", "Rx Mix", "Rx Mixer"},
648 {"Line Mono Mux", "Line Mix", "Line Mixer"},
649 {"Line Mono Mux", "Rx Mix", "Rx Mixer"},
652 {"Line Mixer", "Line 1 + 2", "LINE1"},
653 {"Line Mixer", "Line 1 - 2", "LINE1"},
654 {"Line Mixer", "Line 1 + 2", "LINE2"},
655 {"Line Mixer", "Line 1 - 2", "LINE2"},
656 {"Line Mixer", "Line 1", "LINE1"},
657 {"Line Mixer", "Line 2", "LINE2"},
660 {"Rx Mixer", "RXP - RXN", "RXP"},
661 {"Rx Mixer", "RXP + RXN", "RXP"},
662 {"Rx Mixer", "RXP - RXN", "RXN"},
663 {"Rx Mixer", "RXP + RXN", "RXN"},
664 {"Rx Mixer", "RXP", "RXP"},
665 {"Rx Mixer", "RXN", "RXN"},
668 {"Mic 1 Volume", NULL, "MIC1N"},
669 {"Mic 1 Volume", NULL, "Mic Selection Mux"},
672 {"Mic 2 Volume", NULL, "MIC2N"},
673 {"Mic 2 Volume", NULL, "MIC2"},
675 /* Mic Selector Mux */
676 {"Mic Selection Mux", "Mic 1", "MIC1"},
677 {"Mic Selection Mux", "Mic 2", "MIC2N"},
678 {"Mic Selection Mux", "Mic 3", "MIC2"},
681 {"ACOP", NULL, "ALC Mixer"},
684 static int wm8753_add_widgets(struct snd_soc_codec *codec)
686 snd_soc_dapm_new_controls(codec, wm8753_dapm_widgets,
687 ARRAY_SIZE(wm8753_dapm_widgets));
689 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
691 snd_soc_dapm_new_widgets(codec);
702 /* The size in bits of the pll divide multiplied by 10
703 * to allow rounding later */
704 #define FIXED_PLL_SIZE ((1 << 22) * 10)
706 static void pll_factors(struct _pll_div *pll_div, unsigned int target,
710 unsigned int K, Ndiv, Nmod;
712 Ndiv = target / source;
716 Ndiv = target / source;
720 if ((Ndiv < 6) || (Ndiv > 12))
722 "wm8753: unsupported N = %d\n", Ndiv);
725 Nmod = target % source;
726 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
728 do_div(Kpart, source);
730 K = Kpart & 0xFFFFFFFF;
732 /* Check if we need to round */
736 /* Move down to proper range now rounding is done */
742 static int wm8753_set_dai_pll(struct snd_soc_dai *codec_dai,
743 int pll_id, unsigned int freq_in, unsigned int freq_out)
747 struct snd_soc_codec *codec = codec_dai->codec;
749 if (pll_id < WM8753_PLL1 || pll_id > WM8753_PLL2)
752 if (pll_id == WM8753_PLL1) {
755 reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xffef;
759 reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfff7;
762 if (!freq_in || !freq_out) {
764 wm8753_write(codec, WM8753_PLL1CTL1 + offset, 0x0026);
765 wm8753_write(codec, WM8753_CLOCK, reg);
769 struct _pll_div pll_div;
771 pll_factors(&pll_div, freq_out * 8, freq_in);
773 /* set up N and K PLL divisor ratios */
774 /* bits 8:5 = PLL_N, bits 3:0 = PLL_K[21:18] */
775 value = (pll_div.n << 5) + ((pll_div.k & 0x3c0000) >> 18);
776 wm8753_write(codec, WM8753_PLL1CTL2 + offset, value);
778 /* bits 8:0 = PLL_K[17:9] */
779 value = (pll_div.k & 0x03fe00) >> 9;
780 wm8753_write(codec, WM8753_PLL1CTL3 + offset, value);
782 /* bits 8:0 = PLL_K[8:0] */
783 value = pll_div.k & 0x0001ff;
784 wm8753_write(codec, WM8753_PLL1CTL4 + offset, value);
786 /* set PLL as input and enable */
787 wm8753_write(codec, WM8753_PLL1CTL1 + offset, 0x0027 |
788 (pll_div.div2 << 3));
789 wm8753_write(codec, WM8753_CLOCK, reg | enable);
801 /* codec hifi mclk (after PLL) clock divider coefficients */
802 static const struct _coeff_div coeff_div[] = {
804 {12288000, 8000, 0x6, 0x0},
805 {11289600, 8000, 0x16, 0x0},
806 {18432000, 8000, 0x7, 0x0},
807 {16934400, 8000, 0x17, 0x0},
808 {12000000, 8000, 0x6, 0x1},
811 {11289600, 11025, 0x18, 0x0},
812 {16934400, 11025, 0x19, 0x0},
813 {12000000, 11025, 0x19, 0x1},
816 {12288000, 16000, 0xa, 0x0},
817 {18432000, 16000, 0xb, 0x0},
818 {12000000, 16000, 0xa, 0x1},
821 {11289600, 22050, 0x1a, 0x0},
822 {16934400, 22050, 0x1b, 0x0},
823 {12000000, 22050, 0x1b, 0x1},
826 {12288000, 32000, 0xc, 0x0},
827 {18432000, 32000, 0xd, 0x0},
828 {12000000, 32000, 0xa, 0x1},
831 {11289600, 44100, 0x10, 0x0},
832 {16934400, 44100, 0x11, 0x0},
833 {12000000, 44100, 0x11, 0x1},
836 {12288000, 48000, 0x0, 0x0},
837 {18432000, 48000, 0x1, 0x0},
838 {12000000, 48000, 0x0, 0x1},
841 {11289600, 88200, 0x1e, 0x0},
842 {16934400, 88200, 0x1f, 0x0},
843 {12000000, 88200, 0x1f, 0x1},
846 {12288000, 96000, 0xe, 0x0},
847 {18432000, 96000, 0xf, 0x0},
848 {12000000, 96000, 0xe, 0x1},
851 static int get_coeff(int mclk, int rate)
855 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
856 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
863 * Clock after PLL and dividers
865 static int wm8753_set_dai_sysclk(struct snd_soc_dai *codec_dai,
866 int clk_id, unsigned int freq, int dir)
868 struct snd_soc_codec *codec = codec_dai->codec;
869 struct wm8753_priv *wm8753 = codec->private_data;
877 if (clk_id == WM8753_MCLK) {
878 wm8753->sysclk = freq;
880 } else if (clk_id == WM8753_PCMCLK) {
881 wm8753->pcmclk = freq;
890 * Set's ADC and Voice DAC format.
892 static int wm8753_vdac_adc_set_dai_fmt(struct snd_soc_dai *codec_dai,
895 struct snd_soc_codec *codec = codec_dai->codec;
896 u16 voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x01ec;
898 /* interface format */
899 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
900 case SND_SOC_DAIFMT_I2S:
903 case SND_SOC_DAIFMT_RIGHT_J:
905 case SND_SOC_DAIFMT_LEFT_J:
908 case SND_SOC_DAIFMT_DSP_A:
911 case SND_SOC_DAIFMT_DSP_B:
918 wm8753_write(codec, WM8753_PCM, voice);
923 * Set PCM DAI bit size and sample rate.
925 static int wm8753_pcm_hw_params(struct snd_pcm_substream *substream,
926 struct snd_pcm_hw_params *params)
928 struct snd_soc_pcm_runtime *rtd = substream->private_data;
929 struct snd_soc_device *socdev = rtd->socdev;
930 struct snd_soc_codec *codec = socdev->codec;
931 struct wm8753_priv *wm8753 = codec->private_data;
932 u16 voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x01f3;
933 u16 srate = wm8753_read_reg_cache(codec, WM8753_SRATE1) & 0x017f;
936 switch (params_format(params)) {
937 case SNDRV_PCM_FORMAT_S16_LE:
939 case SNDRV_PCM_FORMAT_S20_3LE:
942 case SNDRV_PCM_FORMAT_S24_LE:
945 case SNDRV_PCM_FORMAT_S32_LE:
951 if (params_rate(params) * 384 == wm8753->pcmclk)
953 wm8753_write(codec, WM8753_SRATE1, srate);
955 wm8753_write(codec, WM8753_PCM, voice);
960 * Set's PCM dai fmt and BCLK.
962 static int wm8753_pcm_set_dai_fmt(struct snd_soc_dai *codec_dai,
965 struct snd_soc_codec *codec = codec_dai->codec;
968 voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x011f;
969 ioctl = wm8753_read_reg_cache(codec, WM8753_IOCTL) & 0x015d;
971 /* set master/slave audio interface */
972 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
973 case SND_SOC_DAIFMT_CBS_CFS:
975 case SND_SOC_DAIFMT_CBM_CFM:
977 case SND_SOC_DAIFMT_CBM_CFS:
984 /* clock inversion */
985 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
986 case SND_SOC_DAIFMT_DSP_A:
987 case SND_SOC_DAIFMT_DSP_B:
988 /* frame inversion not valid for DSP modes */
989 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
990 case SND_SOC_DAIFMT_NB_NF:
992 case SND_SOC_DAIFMT_IB_NF:
999 case SND_SOC_DAIFMT_I2S:
1000 case SND_SOC_DAIFMT_RIGHT_J:
1001 case SND_SOC_DAIFMT_LEFT_J:
1003 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1004 case SND_SOC_DAIFMT_NB_NF:
1006 case SND_SOC_DAIFMT_IB_IF:
1009 case SND_SOC_DAIFMT_IB_NF:
1012 case SND_SOC_DAIFMT_NB_IF:
1023 wm8753_write(codec, WM8753_PCM, voice);
1024 wm8753_write(codec, WM8753_IOCTL, ioctl);
1028 static int wm8753_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
1029 int div_id, int div)
1031 struct snd_soc_codec *codec = codec_dai->codec;
1036 reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0x003f;
1037 wm8753_write(codec, WM8753_CLOCK, reg | div);
1039 case WM8753_BCLKDIV:
1040 reg = wm8753_read_reg_cache(codec, WM8753_SRATE2) & 0x01c7;
1041 wm8753_write(codec, WM8753_SRATE2, reg | div);
1043 case WM8753_VXCLKDIV:
1044 reg = wm8753_read_reg_cache(codec, WM8753_SRATE2) & 0x003f;
1045 wm8753_write(codec, WM8753_SRATE2, reg | div);
1054 * Set's HiFi DAC format.
1056 static int wm8753_hdac_set_dai_fmt(struct snd_soc_dai *codec_dai,
1059 struct snd_soc_codec *codec = codec_dai->codec;
1060 u16 hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x01e0;
1062 /* interface format */
1063 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1064 case SND_SOC_DAIFMT_I2S:
1067 case SND_SOC_DAIFMT_RIGHT_J:
1069 case SND_SOC_DAIFMT_LEFT_J:
1072 case SND_SOC_DAIFMT_DSP_A:
1075 case SND_SOC_DAIFMT_DSP_B:
1082 wm8753_write(codec, WM8753_HIFI, hifi);
1087 * Set's I2S DAI format.
1089 static int wm8753_i2s_set_dai_fmt(struct snd_soc_dai *codec_dai,
1092 struct snd_soc_codec *codec = codec_dai->codec;
1095 hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x011f;
1096 ioctl = wm8753_read_reg_cache(codec, WM8753_IOCTL) & 0x00ae;
1098 /* set master/slave audio interface */
1099 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1100 case SND_SOC_DAIFMT_CBS_CFS:
1102 case SND_SOC_DAIFMT_CBM_CFM:
1104 case SND_SOC_DAIFMT_CBM_CFS:
1111 /* clock inversion */
1112 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1113 case SND_SOC_DAIFMT_DSP_A:
1114 case SND_SOC_DAIFMT_DSP_B:
1115 /* frame inversion not valid for DSP modes */
1116 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1117 case SND_SOC_DAIFMT_NB_NF:
1119 case SND_SOC_DAIFMT_IB_NF:
1126 case SND_SOC_DAIFMT_I2S:
1127 case SND_SOC_DAIFMT_RIGHT_J:
1128 case SND_SOC_DAIFMT_LEFT_J:
1130 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1131 case SND_SOC_DAIFMT_NB_NF:
1133 case SND_SOC_DAIFMT_IB_IF:
1136 case SND_SOC_DAIFMT_IB_NF:
1139 case SND_SOC_DAIFMT_NB_IF:
1150 wm8753_write(codec, WM8753_HIFI, hifi);
1151 wm8753_write(codec, WM8753_IOCTL, ioctl);
1156 * Set PCM DAI bit size and sample rate.
1158 static int wm8753_i2s_hw_params(struct snd_pcm_substream *substream,
1159 struct snd_pcm_hw_params *params)
1161 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1162 struct snd_soc_device *socdev = rtd->socdev;
1163 struct snd_soc_codec *codec = socdev->codec;
1164 struct wm8753_priv *wm8753 = codec->private_data;
1165 u16 srate = wm8753_read_reg_cache(codec, WM8753_SRATE1) & 0x01c0;
1166 u16 hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x01f3;
1169 /* is digital filter coefficient valid ? */
1170 coeff = get_coeff(wm8753->sysclk, params_rate(params));
1172 printk(KERN_ERR "wm8753 invalid MCLK or rate\n");
1175 wm8753_write(codec, WM8753_SRATE1, srate | (coeff_div[coeff].sr << 1) |
1176 coeff_div[coeff].usb);
1179 switch (params_format(params)) {
1180 case SNDRV_PCM_FORMAT_S16_LE:
1182 case SNDRV_PCM_FORMAT_S20_3LE:
1185 case SNDRV_PCM_FORMAT_S24_LE:
1188 case SNDRV_PCM_FORMAT_S32_LE:
1193 wm8753_write(codec, WM8753_HIFI, hifi);
1197 static int wm8753_mode1v_set_dai_fmt(struct snd_soc_dai *codec_dai,
1200 struct snd_soc_codec *codec = codec_dai->codec;
1203 /* set clk source as pcmclk */
1204 clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb;
1205 wm8753_write(codec, WM8753_CLOCK, clock);
1207 if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0)
1209 return wm8753_pcm_set_dai_fmt(codec_dai, fmt);
1212 static int wm8753_mode1h_set_dai_fmt(struct snd_soc_dai *codec_dai,
1215 if (wm8753_hdac_set_dai_fmt(codec_dai, fmt) < 0)
1217 return wm8753_i2s_set_dai_fmt(codec_dai, fmt);
1220 static int wm8753_mode2_set_dai_fmt(struct snd_soc_dai *codec_dai,
1223 struct snd_soc_codec *codec = codec_dai->codec;
1226 /* set clk source as pcmclk */
1227 clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb;
1228 wm8753_write(codec, WM8753_CLOCK, clock);
1230 if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0)
1232 return wm8753_i2s_set_dai_fmt(codec_dai, fmt);
1235 static int wm8753_mode3_4_set_dai_fmt(struct snd_soc_dai *codec_dai,
1238 struct snd_soc_codec *codec = codec_dai->codec;
1241 /* set clk source as mclk */
1242 clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb;
1243 wm8753_write(codec, WM8753_CLOCK, clock | 0x4);
1245 if (wm8753_hdac_set_dai_fmt(codec_dai, fmt) < 0)
1247 if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0)
1249 return wm8753_i2s_set_dai_fmt(codec_dai, fmt);
1252 static int wm8753_mute(struct snd_soc_dai *dai, int mute)
1254 struct snd_soc_codec *codec = dai->codec;
1255 u16 mute_reg = wm8753_read_reg_cache(codec, WM8753_DAC) & 0xfff7;
1257 /* the digital mute covers the HiFi and Voice DAC's on the WM8753.
1258 * make sure we check if they are not both active when we mute */
1259 if (mute && dai->id == 1) {
1260 if (!wm8753_dai[WM8753_DAI_VOICE].playback.active ||
1261 !wm8753_dai[WM8753_DAI_HIFI].playback.active)
1262 wm8753_write(codec, WM8753_DAC, mute_reg | 0x8);
1265 wm8753_write(codec, WM8753_DAC, mute_reg | 0x8);
1267 wm8753_write(codec, WM8753_DAC, mute_reg);
1273 static int wm8753_set_bias_level(struct snd_soc_codec *codec,
1274 enum snd_soc_bias_level level)
1276 u16 pwr_reg = wm8753_read_reg_cache(codec, WM8753_PWR1) & 0xfe3e;
1279 case SND_SOC_BIAS_ON:
1280 /* set vmid to 50k and unmute dac */
1281 wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x00c0);
1283 case SND_SOC_BIAS_PREPARE:
1284 /* set vmid to 5k for quick power up */
1285 wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x01c1);
1287 case SND_SOC_BIAS_STANDBY:
1288 /* mute dac and set vmid to 500k, enable VREF */
1289 wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x0141);
1291 case SND_SOC_BIAS_OFF:
1292 wm8753_write(codec, WM8753_PWR1, 0x0001);
1295 codec->bias_level = level;
1299 #define WM8753_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
1300 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
1301 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
1302 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
1304 #define WM8753_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1305 SNDRV_PCM_FMTBIT_S24_LE)
1308 * The WM8753 supports upto 4 different and mutually exclusive DAI
1309 * configurations. This gives 2 PCM's available for use, hifi and voice.
1310 * NOTE: The Voice PCM cannot play or capture audio to the CPU as it's DAI
1311 * is connected between the wm8753 and a BT codec or GSM modem.
1313 * 1. Voice over PCM DAI - HIFI DAC over HIFI DAI
1314 * 2. Voice over HIFI DAI - HIFI disabled
1315 * 3. Voice disabled - HIFI over HIFI
1316 * 4. Voice disabled - HIFI over HIFI, uses voice DAI LRC for capture
1318 static const struct snd_soc_dai wm8753_all_dai[] = {
1319 /* DAI HiFi mode 1 */
1320 { .name = "WM8753 HiFi",
1323 .stream_name = "HiFi Playback",
1326 .rates = WM8753_RATES,
1327 .formats = WM8753_FORMATS,},
1328 .capture = { /* dummy for fast DAI switching */
1329 .stream_name = "Capture",
1332 .rates = WM8753_RATES,
1333 .formats = WM8753_FORMATS,},
1335 .hw_params = wm8753_i2s_hw_params,},
1337 .digital_mute = wm8753_mute,
1338 .set_fmt = wm8753_mode1h_set_dai_fmt,
1339 .set_clkdiv = wm8753_set_dai_clkdiv,
1340 .set_pll = wm8753_set_dai_pll,
1341 .set_sysclk = wm8753_set_dai_sysclk,
1344 /* DAI Voice mode 1 */
1345 { .name = "WM8753 Voice",
1348 .stream_name = "Voice Playback",
1351 .rates = WM8753_RATES,
1352 .formats = WM8753_FORMATS,},
1354 .stream_name = "Capture",
1357 .rates = WM8753_RATES,
1358 .formats = WM8753_FORMATS,},
1360 .hw_params = wm8753_pcm_hw_params,},
1362 .digital_mute = wm8753_mute,
1363 .set_fmt = wm8753_mode1v_set_dai_fmt,
1364 .set_clkdiv = wm8753_set_dai_clkdiv,
1365 .set_pll = wm8753_set_dai_pll,
1366 .set_sysclk = wm8753_set_dai_sysclk,
1369 /* DAI HiFi mode 2 - dummy */
1370 { .name = "WM8753 HiFi",
1373 /* DAI Voice mode 2 */
1374 { .name = "WM8753 Voice",
1377 .stream_name = "Voice Playback",
1380 .rates = WM8753_RATES,
1381 .formats = WM8753_FORMATS,},
1383 .stream_name = "Capture",
1386 .rates = WM8753_RATES,
1387 .formats = WM8753_FORMATS,},
1389 .hw_params = wm8753_pcm_hw_params,},
1391 .digital_mute = wm8753_mute,
1392 .set_fmt = wm8753_mode2_set_dai_fmt,
1393 .set_clkdiv = wm8753_set_dai_clkdiv,
1394 .set_pll = wm8753_set_dai_pll,
1395 .set_sysclk = wm8753_set_dai_sysclk,
1398 /* DAI HiFi mode 3 */
1399 { .name = "WM8753 HiFi",
1402 .stream_name = "HiFi Playback",
1405 .rates = WM8753_RATES,
1406 .formats = WM8753_FORMATS,},
1408 .stream_name = "Capture",
1411 .rates = WM8753_RATES,
1412 .formats = WM8753_FORMATS,},
1414 .hw_params = wm8753_i2s_hw_params,},
1416 .digital_mute = wm8753_mute,
1417 .set_fmt = wm8753_mode3_4_set_dai_fmt,
1418 .set_clkdiv = wm8753_set_dai_clkdiv,
1419 .set_pll = wm8753_set_dai_pll,
1420 .set_sysclk = wm8753_set_dai_sysclk,
1423 /* DAI Voice mode 3 - dummy */
1424 { .name = "WM8753 Voice",
1427 /* DAI HiFi mode 4 */
1428 { .name = "WM8753 HiFi",
1431 .stream_name = "HiFi Playback",
1434 .rates = WM8753_RATES,
1435 .formats = WM8753_FORMATS,},
1437 .stream_name = "Capture",
1440 .rates = WM8753_RATES,
1441 .formats = WM8753_FORMATS,},
1443 .hw_params = wm8753_i2s_hw_params,},
1445 .digital_mute = wm8753_mute,
1446 .set_fmt = wm8753_mode3_4_set_dai_fmt,
1447 .set_clkdiv = wm8753_set_dai_clkdiv,
1448 .set_pll = wm8753_set_dai_pll,
1449 .set_sysclk = wm8753_set_dai_sysclk,
1452 /* DAI Voice mode 4 - dummy */
1453 { .name = "WM8753 Voice",
1458 struct snd_soc_dai wm8753_dai[2];
1459 EXPORT_SYMBOL_GPL(wm8753_dai);
1461 static void wm8753_set_dai_mode(struct snd_soc_codec *codec, unsigned int mode)
1464 int playback_active, capture_active, codec_active, pop_wait;
1467 playback_active = wm8753_dai[0].playback.active;
1468 capture_active = wm8753_dai[0].capture.active;
1469 codec_active = wm8753_dai[0].active;
1470 private_data = wm8753_dai[0].private_data;
1471 pop_wait = wm8753_dai[0].pop_wait;
1472 wm8753_dai[0] = wm8753_all_dai[mode << 1];
1473 wm8753_dai[0].playback.active = playback_active;
1474 wm8753_dai[0].capture.active = capture_active;
1475 wm8753_dai[0].active = codec_active;
1476 wm8753_dai[0].private_data = private_data;
1477 wm8753_dai[0].pop_wait = pop_wait;
1479 playback_active = wm8753_dai[1].playback.active;
1480 capture_active = wm8753_dai[1].capture.active;
1481 codec_active = wm8753_dai[1].active;
1482 private_data = wm8753_dai[1].private_data;
1483 pop_wait = wm8753_dai[1].pop_wait;
1484 wm8753_dai[1] = wm8753_all_dai[(mode << 1) + 1];
1485 wm8753_dai[1].playback.active = playback_active;
1486 wm8753_dai[1].capture.active = capture_active;
1487 wm8753_dai[1].active = codec_active;
1488 wm8753_dai[1].private_data = private_data;
1489 wm8753_dai[1].pop_wait = pop_wait;
1491 wm8753_dai[0].codec = codec;
1492 wm8753_dai[1].codec = codec;
1495 static void wm8753_work(struct work_struct *work)
1497 struct snd_soc_codec *codec =
1498 container_of(work, struct snd_soc_codec, delayed_work.work);
1499 wm8753_set_bias_level(codec, codec->bias_level);
1502 static int wm8753_suspend(struct platform_device *pdev, pm_message_t state)
1504 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1505 struct snd_soc_codec *codec = socdev->codec;
1507 /* we only need to suspend if we are a valid card */
1511 wm8753_set_bias_level(codec, SND_SOC_BIAS_OFF);
1515 static int wm8753_resume(struct platform_device *pdev)
1517 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1518 struct snd_soc_codec *codec = socdev->codec;
1521 u16 *cache = codec->reg_cache;
1523 /* we only need to resume if we are a valid card */
1527 /* Sync reg_cache with the hardware */
1528 for (i = 0; i < ARRAY_SIZE(wm8753_reg); i++) {
1529 if (i + 1 == WM8753_RESET)
1531 data[0] = ((i + 1) << 1) | ((cache[i] >> 8) & 0x0001);
1532 data[1] = cache[i] & 0x00ff;
1533 codec->hw_write(codec->control_data, data, 2);
1536 wm8753_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1538 /* charge wm8753 caps */
1539 if (codec->suspend_bias_level == SND_SOC_BIAS_ON) {
1540 wm8753_set_bias_level(codec, SND_SOC_BIAS_PREPARE);
1541 codec->bias_level = SND_SOC_BIAS_ON;
1542 schedule_delayed_work(&codec->delayed_work,
1543 msecs_to_jiffies(caps_charge));
1550 * initialise the WM8753 driver
1551 * register the mixer and dsp interfaces with the kernel
1553 static int wm8753_init(struct snd_soc_device *socdev)
1555 struct snd_soc_codec *codec = socdev->codec;
1558 codec->name = "WM8753";
1559 codec->owner = THIS_MODULE;
1560 codec->read = wm8753_read_reg_cache;
1561 codec->write = wm8753_write;
1562 codec->set_bias_level = wm8753_set_bias_level;
1563 codec->dai = wm8753_dai;
1565 codec->reg_cache_size = ARRAY_SIZE(wm8753_reg);
1566 codec->reg_cache = kmemdup(wm8753_reg, sizeof(wm8753_reg), GFP_KERNEL);
1568 if (codec->reg_cache == NULL)
1571 wm8753_set_dai_mode(codec, 0);
1573 wm8753_reset(codec);
1576 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
1578 printk(KERN_ERR "wm8753: failed to create pcms\n");
1582 /* charge output caps */
1583 wm8753_set_bias_level(codec, SND_SOC_BIAS_PREPARE);
1584 codec->bias_level = SND_SOC_BIAS_STANDBY;
1585 schedule_delayed_work(&codec->delayed_work,
1586 msecs_to_jiffies(caps_charge));
1588 /* set the update bits */
1589 reg = wm8753_read_reg_cache(codec, WM8753_LDAC);
1590 wm8753_write(codec, WM8753_LDAC, reg | 0x0100);
1591 reg = wm8753_read_reg_cache(codec, WM8753_RDAC);
1592 wm8753_write(codec, WM8753_RDAC, reg | 0x0100);
1593 reg = wm8753_read_reg_cache(codec, WM8753_LADC);
1594 wm8753_write(codec, WM8753_LADC, reg | 0x0100);
1595 reg = wm8753_read_reg_cache(codec, WM8753_RADC);
1596 wm8753_write(codec, WM8753_RADC, reg | 0x0100);
1597 reg = wm8753_read_reg_cache(codec, WM8753_LOUT1V);
1598 wm8753_write(codec, WM8753_LOUT1V, reg | 0x0100);
1599 reg = wm8753_read_reg_cache(codec, WM8753_ROUT1V);
1600 wm8753_write(codec, WM8753_ROUT1V, reg | 0x0100);
1601 reg = wm8753_read_reg_cache(codec, WM8753_LOUT2V);
1602 wm8753_write(codec, WM8753_LOUT2V, reg | 0x0100);
1603 reg = wm8753_read_reg_cache(codec, WM8753_ROUT2V);
1604 wm8753_write(codec, WM8753_ROUT2V, reg | 0x0100);
1605 reg = wm8753_read_reg_cache(codec, WM8753_LINVOL);
1606 wm8753_write(codec, WM8753_LINVOL, reg | 0x0100);
1607 reg = wm8753_read_reg_cache(codec, WM8753_RINVOL);
1608 wm8753_write(codec, WM8753_RINVOL, reg | 0x0100);
1610 wm8753_add_controls(codec);
1611 wm8753_add_widgets(codec);
1612 ret = snd_soc_register_card(socdev);
1614 printk(KERN_ERR "wm8753: failed to register card\n");
1621 snd_soc_free_pcms(socdev);
1622 snd_soc_dapm_free(socdev);
1624 kfree(codec->reg_cache);
1628 /* If the i2c layer weren't so broken, we could pass this kind of data
1630 static struct snd_soc_device *wm8753_socdev;
1632 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1635 * WM8753 2 wire address is determined by GPIO5
1636 * state during powerup.
1641 static int wm8753_i2c_probe(struct i2c_client *i2c,
1642 const struct i2c_device_id *id)
1644 struct snd_soc_device *socdev = wm8753_socdev;
1645 struct snd_soc_codec *codec = socdev->codec;
1648 i2c_set_clientdata(i2c, codec);
1649 codec->control_data = i2c;
1651 ret = wm8753_init(socdev);
1653 pr_err("failed to initialise WM8753\n");
1658 static int wm8753_i2c_remove(struct i2c_client *client)
1660 struct snd_soc_codec *codec = i2c_get_clientdata(client);
1661 kfree(codec->reg_cache);
1665 static const struct i2c_device_id wm8753_i2c_id[] = {
1669 MODULE_DEVICE_TABLE(i2c, wm8753_i2c_id);
1671 static struct i2c_driver wm8753_i2c_driver = {
1673 .name = "WM8753 I2C Codec",
1674 .owner = THIS_MODULE,
1676 .probe = wm8753_i2c_probe,
1677 .remove = wm8753_i2c_remove,
1678 .id_table = wm8753_i2c_id,
1681 static int wm8753_add_i2c_device(struct platform_device *pdev,
1682 const struct wm8753_setup_data *setup)
1684 struct i2c_board_info info;
1685 struct i2c_adapter *adapter;
1686 struct i2c_client *client;
1689 ret = i2c_add_driver(&wm8753_i2c_driver);
1691 dev_err(&pdev->dev, "can't add i2c driver\n");
1695 memset(&info, 0, sizeof(struct i2c_board_info));
1696 info.addr = setup->i2c_address;
1697 strlcpy(info.type, "wm8753", I2C_NAME_SIZE);
1699 adapter = i2c_get_adapter(setup->i2c_bus);
1701 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
1706 client = i2c_new_device(adapter, &info);
1707 i2c_put_adapter(adapter);
1709 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
1710 (unsigned int)info.addr);
1717 i2c_del_driver(&wm8753_i2c_driver);
1722 #if defined(CONFIG_SPI_MASTER)
1723 static int __devinit wm8753_spi_probe(struct spi_device *spi)
1725 struct snd_soc_device *socdev = wm8753_socdev;
1726 struct snd_soc_codec *codec = socdev->codec;
1729 codec->control_data = spi;
1731 ret = wm8753_init(socdev);
1733 dev_err(&spi->dev, "failed to initialise WM8753\n");
1738 static int __devexit wm8753_spi_remove(struct spi_device *spi)
1743 static struct spi_driver wm8753_spi_driver = {
1746 .bus = &spi_bus_type,
1747 .owner = THIS_MODULE,
1749 .probe = wm8753_spi_probe,
1750 .remove = __devexit_p(wm8753_spi_remove),
1753 static int wm8753_spi_write(struct spi_device *spi, const char *data, int len)
1755 struct spi_transfer t;
1756 struct spi_message m;
1765 spi_message_init(&m);
1766 memset(&t, 0, (sizeof t));
1771 spi_message_add_tail(&t, &m);
1779 static int wm8753_probe(struct platform_device *pdev)
1781 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1782 struct wm8753_setup_data *setup;
1783 struct snd_soc_codec *codec;
1784 struct wm8753_priv *wm8753;
1787 pr_info("WM8753 Audio Codec %s", WM8753_VERSION);
1789 setup = socdev->codec_data;
1790 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
1794 wm8753 = kzalloc(sizeof(struct wm8753_priv), GFP_KERNEL);
1795 if (wm8753 == NULL) {
1800 codec->private_data = wm8753;
1801 socdev->codec = codec;
1802 mutex_init(&codec->mutex);
1803 INIT_LIST_HEAD(&codec->dapm_widgets);
1804 INIT_LIST_HEAD(&codec->dapm_paths);
1805 wm8753_socdev = socdev;
1806 INIT_DELAYED_WORK(&codec->delayed_work, wm8753_work);
1808 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1809 if (setup->i2c_address) {
1810 codec->hw_write = (hw_write_t)i2c_master_send;
1811 ret = wm8753_add_i2c_device(pdev, setup);
1814 #if defined(CONFIG_SPI_MASTER)
1816 codec->hw_write = (hw_write_t)wm8753_spi_write;
1817 ret = spi_register_driver(&wm8753_spi_driver);
1819 printk(KERN_ERR "can't add spi driver");
1824 kfree(codec->private_data);
1831 * This function forces any delayed work to be queued and run.
1833 static int run_delayed_work(struct delayed_work *dwork)
1837 /* cancel any work waiting to be queued. */
1838 ret = cancel_delayed_work(dwork);
1840 /* if there was any work waiting then we run it now and
1841 * wait for it's completion */
1843 schedule_delayed_work(dwork, 0);
1844 flush_scheduled_work();
1849 /* power down chip */
1850 static int wm8753_remove(struct platform_device *pdev)
1852 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1853 struct snd_soc_codec *codec = socdev->codec;
1855 if (codec->control_data)
1856 wm8753_set_bias_level(codec, SND_SOC_BIAS_OFF);
1857 run_delayed_work(&codec->delayed_work);
1858 snd_soc_free_pcms(socdev);
1859 snd_soc_dapm_free(socdev);
1860 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1861 i2c_unregister_device(codec->control_data);
1862 i2c_del_driver(&wm8753_i2c_driver);
1864 #if defined(CONFIG_SPI_MASTER)
1865 spi_unregister_driver(&wm8753_spi_driver);
1867 kfree(codec->private_data);
1873 struct snd_soc_codec_device soc_codec_dev_wm8753 = {
1874 .probe = wm8753_probe,
1875 .remove = wm8753_remove,
1876 .suspend = wm8753_suspend,
1877 .resume = wm8753_resume,
1879 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8753);
1881 MODULE_DESCRIPTION("ASoC WM8753 driver");
1882 MODULE_AUTHOR("Liam Girdwood");
1883 MODULE_LICENSE("GPL");