2 * wm8753.c -- WM8753 ALSA Soc Audio driver
4 * Copyright 2003 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * The WM8753 is a low power, high quality stereo codec with integrated PCM
14 * codec designed for portable digital telephony applications.
18 * This driver support 2 DAI PCM's. This makes the default PCM available for
19 * HiFi audio (e.g. MP3, ogg) playback/capture and the other PCM available for
22 * Please note that the voice PCM can be connected directly to a Bluetooth
23 * codec or GSM modem and thus cannot be read or written to, although it is
24 * available to be configured with snd_hw_params(), etc and kcontrols in the
27 * Fast DAI switching:-
29 * The driver can now fast switch between the DAI configurations via a
30 * an alsa kcontrol. This allows the PCM to remain open.
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/kernel.h>
37 #include <linux/init.h>
38 #include <linux/delay.h>
40 #include <linux/i2c.h>
41 #include <linux/platform_device.h>
42 #include <linux/spi/spi.h>
43 #include <sound/core.h>
44 #include <sound/pcm.h>
45 #include <sound/pcm_params.h>
46 #include <sound/soc.h>
47 #include <sound/soc-dapm.h>
48 #include <sound/initval.h>
49 #include <sound/tlv.h>
50 #include <asm/div64.h>
54 #define WM8753_VERSION "0.16"
56 static int caps_charge = 2000;
57 module_param(caps_charge, int, 0);
58 MODULE_PARM_DESC(caps_charge, "WM8753 cap charge time (msecs)");
60 static void wm8753_set_dai_mode(struct snd_soc_codec *codec,
63 /* codec private data */
70 * wm8753 register cache
71 * We can't read the WM8753 register space when we
72 * are using 2 wire for device control, so we cache them instead.
74 static const u16 wm8753_reg[] = {
75 0x0008, 0x0000, 0x000a, 0x000a,
76 0x0033, 0x0000, 0x0007, 0x00ff,
77 0x00ff, 0x000f, 0x000f, 0x007b,
78 0x0000, 0x0032, 0x0000, 0x00c3,
79 0x00c3, 0x00c0, 0x0000, 0x0000,
80 0x0000, 0x0000, 0x0000, 0x0000,
81 0x0000, 0x0000, 0x0000, 0x0000,
82 0x0000, 0x0000, 0x0000, 0x0055,
83 0x0005, 0x0050, 0x0055, 0x0050,
84 0x0055, 0x0050, 0x0055, 0x0079,
85 0x0079, 0x0079, 0x0079, 0x0079,
86 0x0000, 0x0000, 0x0000, 0x0000,
87 0x0097, 0x0097, 0x0000, 0x0004,
88 0x0000, 0x0083, 0x0024, 0x01ba,
89 0x0000, 0x0083, 0x0024, 0x01ba,
94 * read wm8753 register cache
96 static inline unsigned int wm8753_read_reg_cache(struct snd_soc_codec *codec,
99 u16 *cache = codec->reg_cache;
100 if (reg < 1 || reg >= (ARRAY_SIZE(wm8753_reg) + 1))
102 return cache[reg - 1];
106 * write wm8753 register cache
108 static inline void wm8753_write_reg_cache(struct snd_soc_codec *codec,
109 unsigned int reg, unsigned int value)
111 u16 *cache = codec->reg_cache;
112 if (reg < 1 || reg >= (ARRAY_SIZE(wm8753_reg) + 1))
114 cache[reg - 1] = value;
118 * write to the WM8753 register space
120 static int wm8753_write(struct snd_soc_codec *codec, unsigned int reg,
126 * D15..D9 WM8753 register offset
127 * D8...D0 register data
129 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
130 data[1] = value & 0x00ff;
132 wm8753_write_reg_cache(codec, reg, value);
133 if (codec->hw_write(codec->control_data, data, 2) == 2)
139 #define wm8753_reset(c) wm8753_write(c, WM8753_RESET, 0)
144 static const char *wm8753_base[] = {"Linear Control", "Adaptive Boost"};
145 static const char *wm8753_base_filter[] =
146 {"130Hz @ 48kHz", "200Hz @ 48kHz", "100Hz @ 16kHz", "400Hz @ 48kHz",
147 "100Hz @ 8kHz", "200Hz @ 8kHz"};
148 static const char *wm8753_treble[] = {"8kHz", "4kHz"};
149 static const char *wm8753_alc_func[] = {"Off", "Right", "Left", "Stereo"};
150 static const char *wm8753_ng_type[] = {"Constant PGA Gain", "Mute ADC Output"};
151 static const char *wm8753_3d_func[] = {"Capture", "Playback"};
152 static const char *wm8753_3d_uc[] = {"2.2kHz", "1.5kHz"};
153 static const char *wm8753_3d_lc[] = {"200Hz", "500Hz"};
154 static const char *wm8753_deemp[] = {"None", "32kHz", "44.1kHz", "48kHz"};
155 static const char *wm8753_mono_mix[] = {"Stereo", "Left", "Right", "Mono"};
156 static const char *wm8753_dac_phase[] = {"Non Inverted", "Inverted"};
157 static const char *wm8753_line_mix[] = {"Line 1 + 2", "Line 1 - 2",
159 static const char *wm8753_mono_mux[] = {"Line Mix", "Rx Mix"};
160 static const char *wm8753_right_mux[] = {"Line 2", "Rx Mix"};
161 static const char *wm8753_left_mux[] = {"Line 1", "Rx Mix"};
162 static const char *wm8753_rxmsel[] = {"RXP - RXN", "RXP + RXN", "RXP", "RXN"};
163 static const char *wm8753_sidetone_mux[] = {"Left PGA", "Mic 1", "Mic 2",
165 static const char *wm8753_mono2_src[] = {"Inverted Mono 1", "Left", "Right",
167 static const char *wm8753_out3[] = {"VREF", "ROUT2", "Left + Right"};
168 static const char *wm8753_out4[] = {"VREF", "Capture ST", "LOUT2"};
169 static const char *wm8753_radcsel[] = {"PGA", "Line or RXP-RXN", "Sidetone"};
170 static const char *wm8753_ladcsel[] = {"PGA", "Line or RXP-RXN", "Line"};
171 static const char *wm8753_mono_adc[] = {"Stereo", "Analogue Mix Left",
172 "Analogue Mix Right", "Digital Mono Mix"};
173 static const char *wm8753_adc_hp[] = {"3.4Hz @ 48kHz", "82Hz @ 16k",
174 "82Hz @ 8kHz", "170Hz @ 8kHz"};
175 static const char *wm8753_adc_filter[] = {"HiFi", "Voice"};
176 static const char *wm8753_mic_sel[] = {"Mic 1", "Mic 2", "Mic 3"};
177 static const char *wm8753_dai_mode[] = {"DAI 0", "DAI 1", "DAI 2", "DAI 3"};
178 static const char *wm8753_dat_sel[] = {"Stereo", "Left ADC", "Right ADC",
180 static const char *wm8753_rout2_phase[] = {"Non Inverted", "Inverted"};
182 static const struct soc_enum wm8753_enum[] = {
183 SOC_ENUM_SINGLE(WM8753_BASS, 7, 2, wm8753_base),
184 SOC_ENUM_SINGLE(WM8753_BASS, 4, 6, wm8753_base_filter),
185 SOC_ENUM_SINGLE(WM8753_TREBLE, 6, 2, wm8753_treble),
186 SOC_ENUM_SINGLE(WM8753_ALC1, 7, 4, wm8753_alc_func),
187 SOC_ENUM_SINGLE(WM8753_NGATE, 1, 2, wm8753_ng_type),
188 SOC_ENUM_SINGLE(WM8753_3D, 7, 2, wm8753_3d_func),
189 SOC_ENUM_SINGLE(WM8753_3D, 6, 2, wm8753_3d_uc),
190 SOC_ENUM_SINGLE(WM8753_3D, 5, 2, wm8753_3d_lc),
191 SOC_ENUM_SINGLE(WM8753_DAC, 1, 4, wm8753_deemp),
192 SOC_ENUM_SINGLE(WM8753_DAC, 4, 4, wm8753_mono_mix),
193 SOC_ENUM_SINGLE(WM8753_DAC, 6, 2, wm8753_dac_phase),
194 SOC_ENUM_SINGLE(WM8753_INCTL1, 3, 4, wm8753_line_mix),
195 SOC_ENUM_SINGLE(WM8753_INCTL1, 2, 2, wm8753_mono_mux),
196 SOC_ENUM_SINGLE(WM8753_INCTL1, 1, 2, wm8753_right_mux),
197 SOC_ENUM_SINGLE(WM8753_INCTL1, 0, 2, wm8753_left_mux),
198 SOC_ENUM_SINGLE(WM8753_INCTL2, 6, 4, wm8753_rxmsel),
199 SOC_ENUM_SINGLE(WM8753_INCTL2, 4, 4, wm8753_sidetone_mux),
200 SOC_ENUM_SINGLE(WM8753_OUTCTL, 7, 4, wm8753_mono2_src),
201 SOC_ENUM_SINGLE(WM8753_OUTCTL, 0, 3, wm8753_out3),
202 SOC_ENUM_SINGLE(WM8753_ADCTL2, 7, 3, wm8753_out4),
203 SOC_ENUM_SINGLE(WM8753_ADCIN, 2, 3, wm8753_radcsel),
204 SOC_ENUM_SINGLE(WM8753_ADCIN, 0, 3, wm8753_ladcsel),
205 SOC_ENUM_SINGLE(WM8753_ADCIN, 4, 4, wm8753_mono_adc),
206 SOC_ENUM_SINGLE(WM8753_ADC, 2, 4, wm8753_adc_hp),
207 SOC_ENUM_SINGLE(WM8753_ADC, 4, 2, wm8753_adc_filter),
208 SOC_ENUM_SINGLE(WM8753_MICBIAS, 6, 3, wm8753_mic_sel),
209 SOC_ENUM_SINGLE(WM8753_IOCTL, 2, 4, wm8753_dai_mode),
210 SOC_ENUM_SINGLE(WM8753_ADC, 7, 4, wm8753_dat_sel),
211 SOC_ENUM_SINGLE(WM8753_OUTCTL, 2, 2, wm8753_rout2_phase),
215 static int wm8753_get_dai(struct snd_kcontrol *kcontrol,
216 struct snd_ctl_elem_value *ucontrol)
218 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
219 int mode = wm8753_read_reg_cache(codec, WM8753_IOCTL);
221 ucontrol->value.integer.value[0] = (mode & 0xc) >> 2;
225 static int wm8753_set_dai(struct snd_kcontrol *kcontrol,
226 struct snd_ctl_elem_value *ucontrol)
228 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
229 int mode = wm8753_read_reg_cache(codec, WM8753_IOCTL);
231 if (((mode & 0xc) >> 2) == ucontrol->value.integer.value[0])
235 mode |= (ucontrol->value.integer.value[0] << 2);
237 wm8753_write(codec, WM8753_IOCTL, mode);
238 wm8753_set_dai_mode(codec, ucontrol->value.integer.value[0]);
242 static const DECLARE_TLV_DB_SCALE(rec_mix_tlv, -1500, 300, 0);
243 static const DECLARE_TLV_DB_SCALE(mic_preamp_tlv, 1200, 600, 0);
244 static const DECLARE_TLV_DB_SCALE(adc_tlv, -9750, 50, 1);
245 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
246 static const unsigned int out_tlv[] = {
247 TLV_DB_RANGE_HEAD(2),
248 /* 0000000 - 0101111 = "Analogue mute" */
249 0, 48, TLV_DB_SCALE_ITEM(-25500, 0, 0),
250 48, 127, TLV_DB_SCALE_ITEM(-7300, 100, 0),
252 static const DECLARE_TLV_DB_SCALE(mix_tlv, -1500, 300, 0);
253 static const DECLARE_TLV_DB_SCALE(voice_mix_tlv, -1200, 300, 0);
254 static const DECLARE_TLV_DB_SCALE(pga_tlv, -1725, 75, 0);
256 static const struct snd_kcontrol_new wm8753_snd_controls[] = {
257 SOC_DOUBLE_R_TLV("PCM Volume", WM8753_LDAC, WM8753_RDAC, 0, 255, 0, dac_tlv),
259 SOC_DOUBLE_R_TLV("ADC Capture Volume", WM8753_LADC, WM8753_RADC, 0, 255, 0,
262 SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8753_LOUT1V, WM8753_ROUT1V,
264 SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8753_LOUT2V, WM8753_ROUT2V, 0,
267 SOC_SINGLE_TLV("Mono Playback Volume", WM8753_MOUTV, 0, 127, 0, out_tlv),
269 SOC_DOUBLE_R_TLV("Bypass Playback Volume", WM8753_LOUTM1, WM8753_ROUTM1, 4, 7,
271 SOC_DOUBLE_R_TLV("Sidetone Playback Volume", WM8753_LOUTM2, WM8753_ROUTM2, 4,
273 SOC_DOUBLE_R_TLV("Voice Playback Volume", WM8753_LOUTM2, WM8753_ROUTM2, 0, 7,
276 SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8753_LOUT1V, WM8753_ROUT1V, 7,
278 SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8753_LOUT2V, WM8753_ROUT2V, 7,
281 SOC_SINGLE_TLV("Mono Bypass Playback Volume", WM8753_MOUTM1, 4, 7, 1, mix_tlv),
282 SOC_SINGLE_TLV("Mono Sidetone Playback Volume", WM8753_MOUTM2, 4, 7, 1,
284 SOC_SINGLE_TLV("Mono Voice Playback Volume", WM8753_MOUTM2, 0, 7, 1,
286 SOC_SINGLE("Mono Playback ZC Switch", WM8753_MOUTV, 7, 1, 0),
288 SOC_ENUM("Bass Boost", wm8753_enum[0]),
289 SOC_ENUM("Bass Filter", wm8753_enum[1]),
290 SOC_SINGLE("Bass Volume", WM8753_BASS, 0, 15, 1),
292 SOC_SINGLE("Treble Volume", WM8753_TREBLE, 0, 15, 1),
293 SOC_ENUM("Treble Cut-off", wm8753_enum[2]),
295 SOC_DOUBLE_TLV("Sidetone Capture Volume", WM8753_RECMIX1, 0, 4, 7, 1,
297 SOC_SINGLE_TLV("Voice Sidetone Capture Volume", WM8753_RECMIX2, 0, 7, 1,
300 SOC_DOUBLE_R_TLV("Capture Volume", WM8753_LINVOL, WM8753_RINVOL, 0, 63, 0,
302 SOC_DOUBLE_R("Capture ZC Switch", WM8753_LINVOL, WM8753_RINVOL, 6, 1, 0),
303 SOC_DOUBLE_R("Capture Switch", WM8753_LINVOL, WM8753_RINVOL, 7, 1, 1),
305 SOC_ENUM("Capture Filter Select", wm8753_enum[23]),
306 SOC_ENUM("Capture Filter Cut-off", wm8753_enum[24]),
307 SOC_SINGLE("Capture Filter Switch", WM8753_ADC, 0, 1, 1),
309 SOC_SINGLE("ALC Capture Target Volume", WM8753_ALC1, 0, 7, 0),
310 SOC_SINGLE("ALC Capture Max Volume", WM8753_ALC1, 4, 7, 0),
311 SOC_ENUM("ALC Capture Function", wm8753_enum[3]),
312 SOC_SINGLE("ALC Capture ZC Switch", WM8753_ALC2, 8, 1, 0),
313 SOC_SINGLE("ALC Capture Hold Time", WM8753_ALC2, 0, 15, 1),
314 SOC_SINGLE("ALC Capture Decay Time", WM8753_ALC3, 4, 15, 1),
315 SOC_SINGLE("ALC Capture Attack Time", WM8753_ALC3, 0, 15, 0),
316 SOC_SINGLE("ALC Capture NG Threshold", WM8753_NGATE, 3, 31, 0),
317 SOC_ENUM("ALC Capture NG Type", wm8753_enum[4]),
318 SOC_SINGLE("ALC Capture NG Switch", WM8753_NGATE, 0, 1, 0),
320 SOC_ENUM("3D Function", wm8753_enum[5]),
321 SOC_ENUM("3D Upper Cut-off", wm8753_enum[6]),
322 SOC_ENUM("3D Lower Cut-off", wm8753_enum[7]),
323 SOC_SINGLE("3D Volume", WM8753_3D, 1, 15, 0),
324 SOC_SINGLE("3D Switch", WM8753_3D, 0, 1, 0),
326 SOC_SINGLE("Capture 6dB Attenuate", WM8753_ADCTL1, 2, 1, 0),
327 SOC_SINGLE("Playback 6dB Attenuate", WM8753_ADCTL1, 1, 1, 0),
329 SOC_ENUM("De-emphasis", wm8753_enum[8]),
330 SOC_ENUM("Playback Mono Mix", wm8753_enum[9]),
331 SOC_ENUM("Playback Phase", wm8753_enum[10]),
333 SOC_SINGLE_TLV("Mic2 Capture Volume", WM8753_INCTL1, 7, 3, 0, mic_preamp_tlv),
334 SOC_SINGLE_TLV("Mic1 Capture Volume", WM8753_INCTL1, 5, 3, 0, mic_preamp_tlv),
336 SOC_ENUM_EXT("DAI Mode", wm8753_enum[26], wm8753_get_dai, wm8753_set_dai),
338 SOC_ENUM("ADC Data Select", wm8753_enum[27]),
339 SOC_ENUM("ROUT2 Phase", wm8753_enum[28]),
347 static const struct snd_kcontrol_new wm8753_left_mixer_controls[] = {
348 SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_LOUTM2, 8, 1, 0),
349 SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_LOUTM2, 7, 1, 0),
350 SOC_DAPM_SINGLE("Left Playback Switch", WM8753_LOUTM1, 8, 1, 0),
351 SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_LOUTM1, 7, 1, 0),
355 static const struct snd_kcontrol_new wm8753_right_mixer_controls[] = {
356 SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_ROUTM2, 8, 1, 0),
357 SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_ROUTM2, 7, 1, 0),
358 SOC_DAPM_SINGLE("Right Playback Switch", WM8753_ROUTM1, 8, 1, 0),
359 SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_ROUTM1, 7, 1, 0),
363 static const struct snd_kcontrol_new wm8753_mono_mixer_controls[] = {
364 SOC_DAPM_SINGLE("Left Playback Switch", WM8753_MOUTM1, 8, 1, 0),
365 SOC_DAPM_SINGLE("Right Playback Switch", WM8753_MOUTM2, 8, 1, 0),
366 SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_MOUTM2, 3, 1, 0),
367 SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_MOUTM2, 7, 1, 0),
368 SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_MOUTM1, 7, 1, 0),
372 static const struct snd_kcontrol_new wm8753_mono2_controls =
373 SOC_DAPM_ENUM("Route", wm8753_enum[17]);
376 static const struct snd_kcontrol_new wm8753_out3_controls =
377 SOC_DAPM_ENUM("Route", wm8753_enum[18]);
380 static const struct snd_kcontrol_new wm8753_out4_controls =
381 SOC_DAPM_ENUM("Route", wm8753_enum[19]);
384 static const struct snd_kcontrol_new wm8753_adc_mono_controls =
385 SOC_DAPM_ENUM("Route", wm8753_enum[22]);
388 static const struct snd_kcontrol_new wm8753_record_mixer_controls[] = {
389 SOC_DAPM_SINGLE("Voice Capture Switch", WM8753_RECMIX2, 3, 1, 0),
390 SOC_DAPM_SINGLE("Left Capture Switch", WM8753_RECMIX1, 3, 1, 0),
391 SOC_DAPM_SINGLE("Right Capture Switch", WM8753_RECMIX1, 7, 1, 0),
395 static const struct snd_kcontrol_new wm8753_adc_left_controls =
396 SOC_DAPM_ENUM("Route", wm8753_enum[21]);
399 static const struct snd_kcontrol_new wm8753_adc_right_controls =
400 SOC_DAPM_ENUM("Route", wm8753_enum[20]);
403 static const struct snd_kcontrol_new wm8753_mic_mux_controls =
404 SOC_DAPM_ENUM("Route", wm8753_enum[16]);
407 static const struct snd_kcontrol_new wm8753_alc_mixer_controls[] = {
408 SOC_DAPM_SINGLE("Line Capture Switch", WM8753_INCTL2, 3, 1, 0),
409 SOC_DAPM_SINGLE("Mic2 Capture Switch", WM8753_INCTL2, 2, 1, 0),
410 SOC_DAPM_SINGLE("Mic1 Capture Switch", WM8753_INCTL2, 1, 1, 0),
411 SOC_DAPM_SINGLE("Rx Capture Switch", WM8753_INCTL2, 0, 1, 0),
415 static const struct snd_kcontrol_new wm8753_line_left_controls =
416 SOC_DAPM_ENUM("Route", wm8753_enum[14]);
419 static const struct snd_kcontrol_new wm8753_line_right_controls =
420 SOC_DAPM_ENUM("Route", wm8753_enum[13]);
423 static const struct snd_kcontrol_new wm8753_line_mono_controls =
424 SOC_DAPM_ENUM("Route", wm8753_enum[12]);
426 /* Line mux and mixer */
427 static const struct snd_kcontrol_new wm8753_line_mux_mix_controls =
428 SOC_DAPM_ENUM("Route", wm8753_enum[11]);
430 /* Rx mux and mixer */
431 static const struct snd_kcontrol_new wm8753_rx_mux_mix_controls =
432 SOC_DAPM_ENUM("Route", wm8753_enum[15]);
434 /* Mic Selector Mux */
435 static const struct snd_kcontrol_new wm8753_mic_sel_mux_controls =
436 SOC_DAPM_ENUM("Route", wm8753_enum[25]);
438 static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
439 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8753_PWR1, 5, 0),
440 SND_SOC_DAPM_MIXER("Left Mixer", WM8753_PWR4, 0, 0,
441 &wm8753_left_mixer_controls[0], ARRAY_SIZE(wm8753_left_mixer_controls)),
442 SND_SOC_DAPM_PGA("Left Out 1", WM8753_PWR3, 8, 0, NULL, 0),
443 SND_SOC_DAPM_PGA("Left Out 2", WM8753_PWR3, 6, 0, NULL, 0),
444 SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", WM8753_PWR1, 3, 0),
445 SND_SOC_DAPM_OUTPUT("LOUT1"),
446 SND_SOC_DAPM_OUTPUT("LOUT2"),
447 SND_SOC_DAPM_MIXER("Right Mixer", WM8753_PWR4, 1, 0,
448 &wm8753_right_mixer_controls[0], ARRAY_SIZE(wm8753_right_mixer_controls)),
449 SND_SOC_DAPM_PGA("Right Out 1", WM8753_PWR3, 7, 0, NULL, 0),
450 SND_SOC_DAPM_PGA("Right Out 2", WM8753_PWR3, 5, 0, NULL, 0),
451 SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", WM8753_PWR1, 2, 0),
452 SND_SOC_DAPM_OUTPUT("ROUT1"),
453 SND_SOC_DAPM_OUTPUT("ROUT2"),
454 SND_SOC_DAPM_MIXER("Mono Mixer", WM8753_PWR4, 2, 0,
455 &wm8753_mono_mixer_controls[0], ARRAY_SIZE(wm8753_mono_mixer_controls)),
456 SND_SOC_DAPM_PGA("Mono Out 1", WM8753_PWR3, 2, 0, NULL, 0),
457 SND_SOC_DAPM_PGA("Mono Out 2", WM8753_PWR3, 1, 0, NULL, 0),
458 SND_SOC_DAPM_DAC("Voice DAC", "Voice Playback", WM8753_PWR1, 4, 0),
459 SND_SOC_DAPM_OUTPUT("MONO1"),
460 SND_SOC_DAPM_MUX("Mono 2 Mux", SND_SOC_NOPM, 0, 0, &wm8753_mono2_controls),
461 SND_SOC_DAPM_OUTPUT("MONO2"),
462 SND_SOC_DAPM_MIXER("Out3 Left + Right", -1, 0, 0, NULL, 0),
463 SND_SOC_DAPM_MUX("Out3 Mux", SND_SOC_NOPM, 0, 0, &wm8753_out3_controls),
464 SND_SOC_DAPM_PGA("Out 3", WM8753_PWR3, 4, 0, NULL, 0),
465 SND_SOC_DAPM_OUTPUT("OUT3"),
466 SND_SOC_DAPM_MUX("Out4 Mux", SND_SOC_NOPM, 0, 0, &wm8753_out4_controls),
467 SND_SOC_DAPM_PGA("Out 4", WM8753_PWR3, 3, 0, NULL, 0),
468 SND_SOC_DAPM_OUTPUT("OUT4"),
469 SND_SOC_DAPM_MIXER("Playback Mixer", WM8753_PWR4, 3, 0,
470 &wm8753_record_mixer_controls[0],
471 ARRAY_SIZE(wm8753_record_mixer_controls)),
472 SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8753_PWR2, 3, 0),
473 SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8753_PWR2, 2, 0),
474 SND_SOC_DAPM_MUX("Capture Left Mixer", SND_SOC_NOPM, 0, 0,
475 &wm8753_adc_mono_controls),
476 SND_SOC_DAPM_MUX("Capture Right Mixer", SND_SOC_NOPM, 0, 0,
477 &wm8753_adc_mono_controls),
478 SND_SOC_DAPM_MUX("Capture Left Mux", SND_SOC_NOPM, 0, 0,
479 &wm8753_adc_left_controls),
480 SND_SOC_DAPM_MUX("Capture Right Mux", SND_SOC_NOPM, 0, 0,
481 &wm8753_adc_right_controls),
482 SND_SOC_DAPM_MUX("Mic Sidetone Mux", SND_SOC_NOPM, 0, 0,
483 &wm8753_mic_mux_controls),
484 SND_SOC_DAPM_PGA("Left Capture Volume", WM8753_PWR2, 5, 0, NULL, 0),
485 SND_SOC_DAPM_PGA("Right Capture Volume", WM8753_PWR2, 4, 0, NULL, 0),
486 SND_SOC_DAPM_MIXER("ALC Mixer", WM8753_PWR2, 6, 0,
487 &wm8753_alc_mixer_controls[0], ARRAY_SIZE(wm8753_alc_mixer_controls)),
488 SND_SOC_DAPM_MUX("Line Left Mux", SND_SOC_NOPM, 0, 0,
489 &wm8753_line_left_controls),
490 SND_SOC_DAPM_MUX("Line Right Mux", SND_SOC_NOPM, 0, 0,
491 &wm8753_line_right_controls),
492 SND_SOC_DAPM_MUX("Line Mono Mux", SND_SOC_NOPM, 0, 0,
493 &wm8753_line_mono_controls),
494 SND_SOC_DAPM_MUX("Line Mixer", WM8753_PWR2, 0, 0,
495 &wm8753_line_mux_mix_controls),
496 SND_SOC_DAPM_MUX("Rx Mixer", WM8753_PWR2, 1, 0,
497 &wm8753_rx_mux_mix_controls),
498 SND_SOC_DAPM_PGA("Mic 1 Volume", WM8753_PWR2, 8, 0, NULL, 0),
499 SND_SOC_DAPM_PGA("Mic 2 Volume", WM8753_PWR2, 7, 0, NULL, 0),
500 SND_SOC_DAPM_MUX("Mic Selection Mux", SND_SOC_NOPM, 0, 0,
501 &wm8753_mic_sel_mux_controls),
502 SND_SOC_DAPM_INPUT("LINE1"),
503 SND_SOC_DAPM_INPUT("LINE2"),
504 SND_SOC_DAPM_INPUT("RXP"),
505 SND_SOC_DAPM_INPUT("RXN"),
506 SND_SOC_DAPM_INPUT("ACIN"),
507 SND_SOC_DAPM_OUTPUT("ACOP"),
508 SND_SOC_DAPM_INPUT("MIC1N"),
509 SND_SOC_DAPM_INPUT("MIC1"),
510 SND_SOC_DAPM_INPUT("MIC2N"),
511 SND_SOC_DAPM_INPUT("MIC2"),
512 SND_SOC_DAPM_VMID("VREF"),
515 static const struct snd_soc_dapm_route audio_map[] = {
517 {"Left Mixer", "Left Playback Switch", "Left DAC"},
518 {"Left Mixer", "Voice Playback Switch", "Voice DAC"},
519 {"Left Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
520 {"Left Mixer", "Bypass Playback Switch", "Line Left Mux"},
523 {"Right Mixer", "Right Playback Switch", "Right DAC"},
524 {"Right Mixer", "Voice Playback Switch", "Voice DAC"},
525 {"Right Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
526 {"Right Mixer", "Bypass Playback Switch", "Line Right Mux"},
529 {"Mono Mixer", "Voice Playback Switch", "Voice DAC"},
530 {"Mono Mixer", "Left Playback Switch", "Left DAC"},
531 {"Mono Mixer", "Right Playback Switch", "Right DAC"},
532 {"Mono Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
533 {"Mono Mixer", "Bypass Playback Switch", "Line Mono Mux"},
536 {"Left Out 1", NULL, "Left Mixer"},
537 {"Left Out 2", NULL, "Left Mixer"},
538 {"LOUT1", NULL, "Left Out 1"},
539 {"LOUT2", NULL, "Left Out 2"},
542 {"Right Out 1", NULL, "Right Mixer"},
543 {"Right Out 2", NULL, "Right Mixer"},
544 {"ROUT1", NULL, "Right Out 1"},
545 {"ROUT2", NULL, "Right Out 2"},
548 {"Mono Out 1", NULL, "Mono Mixer"},
549 {"MONO1", NULL, "Mono Out 1"},
552 {"Mono 2 Mux", "Left + Right", "Out3 Left + Right"},
553 {"Mono 2 Mux", "Inverted Mono 1", "MONO1"},
554 {"Mono 2 Mux", "Left", "Left Mixer"},
555 {"Mono 2 Mux", "Right", "Right Mixer"},
556 {"Mono Out 2", NULL, "Mono 2 Mux"},
557 {"MONO2", NULL, "Mono Out 2"},
560 {"Out3 Left + Right", NULL, "Left Mixer"},
561 {"Out3 Left + Right", NULL, "Right Mixer"},
562 {"Out3 Mux", "VREF", "VREF"},
563 {"Out3 Mux", "Left + Right", "Out3 Left + Right"},
564 {"Out3 Mux", "ROUT2", "ROUT2"},
565 {"Out 3", NULL, "Out3 Mux"},
566 {"OUT3", NULL, "Out 3"},
569 {"Out4 Mux", "VREF", "VREF"},
570 {"Out4 Mux", "Capture ST", "Playback Mixer"},
571 {"Out4 Mux", "LOUT2", "LOUT2"},
572 {"Out 4", NULL, "Out4 Mux"},
573 {"OUT4", NULL, "Out 4"},
576 {"Playback Mixer", "Left Capture Switch", "Left Mixer"},
577 {"Playback Mixer", "Voice Capture Switch", "Mono Mixer"},
578 {"Playback Mixer", "Right Capture Switch", "Right Mixer"},
580 /* Mic/SideTone Mux */
581 {"Mic Sidetone Mux", "Left PGA", "Left Capture Volume"},
582 {"Mic Sidetone Mux", "Right PGA", "Right Capture Volume"},
583 {"Mic Sidetone Mux", "Mic 1", "Mic 1 Volume"},
584 {"Mic Sidetone Mux", "Mic 2", "Mic 2 Volume"},
586 /* Capture Left Mux */
587 {"Capture Left Mux", "PGA", "Left Capture Volume"},
588 {"Capture Left Mux", "Line or RXP-RXN", "Line Left Mux"},
589 {"Capture Left Mux", "Line", "LINE1"},
591 /* Capture Right Mux */
592 {"Capture Right Mux", "PGA", "Right Capture Volume"},
593 {"Capture Right Mux", "Line or RXP-RXN", "Line Right Mux"},
594 {"Capture Right Mux", "Sidetone", "Playback Mixer"},
596 /* Mono Capture mixer-mux */
597 {"Capture Right Mixer", "Stereo", "Capture Right Mux"},
598 {"Capture Left Mixer", "Analogue Mix Left", "Capture Left Mux"},
599 {"Capture Left Mixer", "Analogue Mix Left", "Capture Right Mux"},
600 {"Capture Right Mixer", "Analogue Mix Right", "Capture Left Mux"},
601 {"Capture Right Mixer", "Analogue Mix Right", "Capture Right Mux"},
602 {"Capture Left Mixer", "Digital Mono Mix", "Capture Left Mux"},
603 {"Capture Left Mixer", "Digital Mono Mix", "Capture Right Mux"},
604 {"Capture Right Mixer", "Digital Mono Mix", "Capture Left Mux"},
605 {"Capture Right Mixer", "Digital Mono Mix", "Capture Right Mux"},
608 {"Left ADC", NULL, "Capture Left Mixer"},
609 {"Right ADC", NULL, "Capture Right Mixer"},
611 /* Left Capture Volume */
612 {"Left Capture Volume", NULL, "ACIN"},
614 /* Right Capture Volume */
615 {"Right Capture Volume", NULL, "Mic 2 Volume"},
618 {"ALC Mixer", "Line Capture Switch", "Line Mixer"},
619 {"ALC Mixer", "Mic2 Capture Switch", "Mic 2 Volume"},
620 {"ALC Mixer", "Mic1 Capture Switch", "Mic 1 Volume"},
621 {"ALC Mixer", "Rx Capture Switch", "Rx Mixer"},
624 {"Line Left Mux", "Line 1", "LINE1"},
625 {"Line Left Mux", "Rx Mix", "Rx Mixer"},
628 {"Line Right Mux", "Line 2", "LINE2"},
629 {"Line Right Mux", "Rx Mix", "Rx Mixer"},
632 {"Line Mono Mux", "Line Mix", "Line Mixer"},
633 {"Line Mono Mux", "Rx Mix", "Rx Mixer"},
636 {"Line Mixer", "Line 1 + 2", "LINE1"},
637 {"Line Mixer", "Line 1 - 2", "LINE1"},
638 {"Line Mixer", "Line 1 + 2", "LINE2"},
639 {"Line Mixer", "Line 1 - 2", "LINE2"},
640 {"Line Mixer", "Line 1", "LINE1"},
641 {"Line Mixer", "Line 2", "LINE2"},
644 {"Rx Mixer", "RXP - RXN", "RXP"},
645 {"Rx Mixer", "RXP + RXN", "RXP"},
646 {"Rx Mixer", "RXP - RXN", "RXN"},
647 {"Rx Mixer", "RXP + RXN", "RXN"},
648 {"Rx Mixer", "RXP", "RXP"},
649 {"Rx Mixer", "RXN", "RXN"},
652 {"Mic 1 Volume", NULL, "MIC1N"},
653 {"Mic 1 Volume", NULL, "Mic Selection Mux"},
656 {"Mic 2 Volume", NULL, "MIC2N"},
657 {"Mic 2 Volume", NULL, "MIC2"},
659 /* Mic Selector Mux */
660 {"Mic Selection Mux", "Mic 1", "MIC1"},
661 {"Mic Selection Mux", "Mic 2", "MIC2N"},
662 {"Mic Selection Mux", "Mic 3", "MIC2"},
665 {"ACOP", NULL, "ALC Mixer"},
668 static int wm8753_add_widgets(struct snd_soc_codec *codec)
670 snd_soc_dapm_new_controls(codec, wm8753_dapm_widgets,
671 ARRAY_SIZE(wm8753_dapm_widgets));
673 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
675 snd_soc_dapm_new_widgets(codec);
686 /* The size in bits of the pll divide multiplied by 10
687 * to allow rounding later */
688 #define FIXED_PLL_SIZE ((1 << 22) * 10)
690 static void pll_factors(struct _pll_div *pll_div, unsigned int target,
694 unsigned int K, Ndiv, Nmod;
696 Ndiv = target / source;
700 Ndiv = target / source;
704 if ((Ndiv < 6) || (Ndiv > 12))
706 "wm8753: unsupported N = %d\n", Ndiv);
709 Nmod = target % source;
710 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
712 do_div(Kpart, source);
714 K = Kpart & 0xFFFFFFFF;
716 /* Check if we need to round */
720 /* Move down to proper range now rounding is done */
726 static int wm8753_set_dai_pll(struct snd_soc_dai *codec_dai,
727 int pll_id, unsigned int freq_in, unsigned int freq_out)
731 struct snd_soc_codec *codec = codec_dai->codec;
733 if (pll_id < WM8753_PLL1 || pll_id > WM8753_PLL2)
736 if (pll_id == WM8753_PLL1) {
739 reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xffef;
743 reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfff7;
746 if (!freq_in || !freq_out) {
748 wm8753_write(codec, WM8753_PLL1CTL1 + offset, 0x0026);
749 wm8753_write(codec, WM8753_CLOCK, reg);
753 struct _pll_div pll_div;
755 pll_factors(&pll_div, freq_out * 8, freq_in);
757 /* set up N and K PLL divisor ratios */
758 /* bits 8:5 = PLL_N, bits 3:0 = PLL_K[21:18] */
759 value = (pll_div.n << 5) + ((pll_div.k & 0x3c0000) >> 18);
760 wm8753_write(codec, WM8753_PLL1CTL2 + offset, value);
762 /* bits 8:0 = PLL_K[17:9] */
763 value = (pll_div.k & 0x03fe00) >> 9;
764 wm8753_write(codec, WM8753_PLL1CTL3 + offset, value);
766 /* bits 8:0 = PLL_K[8:0] */
767 value = pll_div.k & 0x0001ff;
768 wm8753_write(codec, WM8753_PLL1CTL4 + offset, value);
770 /* set PLL as input and enable */
771 wm8753_write(codec, WM8753_PLL1CTL1 + offset, 0x0027 |
772 (pll_div.div2 << 3));
773 wm8753_write(codec, WM8753_CLOCK, reg | enable);
785 /* codec hifi mclk (after PLL) clock divider coefficients */
786 static const struct _coeff_div coeff_div[] = {
788 {12288000, 8000, 0x6, 0x0},
789 {11289600, 8000, 0x16, 0x0},
790 {18432000, 8000, 0x7, 0x0},
791 {16934400, 8000, 0x17, 0x0},
792 {12000000, 8000, 0x6, 0x1},
795 {11289600, 11025, 0x18, 0x0},
796 {16934400, 11025, 0x19, 0x0},
797 {12000000, 11025, 0x19, 0x1},
800 {12288000, 16000, 0xa, 0x0},
801 {18432000, 16000, 0xb, 0x0},
802 {12000000, 16000, 0xa, 0x1},
805 {11289600, 22050, 0x1a, 0x0},
806 {16934400, 22050, 0x1b, 0x0},
807 {12000000, 22050, 0x1b, 0x1},
810 {12288000, 32000, 0xc, 0x0},
811 {18432000, 32000, 0xd, 0x0},
812 {12000000, 32000, 0xa, 0x1},
815 {11289600, 44100, 0x10, 0x0},
816 {16934400, 44100, 0x11, 0x0},
817 {12000000, 44100, 0x11, 0x1},
820 {12288000, 48000, 0x0, 0x0},
821 {18432000, 48000, 0x1, 0x0},
822 {12000000, 48000, 0x0, 0x1},
825 {11289600, 88200, 0x1e, 0x0},
826 {16934400, 88200, 0x1f, 0x0},
827 {12000000, 88200, 0x1f, 0x1},
830 {12288000, 96000, 0xe, 0x0},
831 {18432000, 96000, 0xf, 0x0},
832 {12000000, 96000, 0xe, 0x1},
835 static int get_coeff(int mclk, int rate)
839 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
840 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
847 * Clock after PLL and dividers
849 static int wm8753_set_dai_sysclk(struct snd_soc_dai *codec_dai,
850 int clk_id, unsigned int freq, int dir)
852 struct snd_soc_codec *codec = codec_dai->codec;
853 struct wm8753_priv *wm8753 = codec->private_data;
861 if (clk_id == WM8753_MCLK) {
862 wm8753->sysclk = freq;
864 } else if (clk_id == WM8753_PCMCLK) {
865 wm8753->pcmclk = freq;
874 * Set's ADC and Voice DAC format.
876 static int wm8753_vdac_adc_set_dai_fmt(struct snd_soc_dai *codec_dai,
879 struct snd_soc_codec *codec = codec_dai->codec;
880 u16 voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x01ec;
882 /* interface format */
883 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
884 case SND_SOC_DAIFMT_I2S:
887 case SND_SOC_DAIFMT_RIGHT_J:
889 case SND_SOC_DAIFMT_LEFT_J:
892 case SND_SOC_DAIFMT_DSP_A:
895 case SND_SOC_DAIFMT_DSP_B:
902 wm8753_write(codec, WM8753_PCM, voice);
907 * Set PCM DAI bit size and sample rate.
909 static int wm8753_pcm_hw_params(struct snd_pcm_substream *substream,
910 struct snd_pcm_hw_params *params,
911 struct snd_soc_dai *dai)
913 struct snd_soc_pcm_runtime *rtd = substream->private_data;
914 struct snd_soc_device *socdev = rtd->socdev;
915 struct snd_soc_codec *codec = socdev->card->codec;
916 struct wm8753_priv *wm8753 = codec->private_data;
917 u16 voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x01f3;
918 u16 srate = wm8753_read_reg_cache(codec, WM8753_SRATE1) & 0x017f;
921 switch (params_format(params)) {
922 case SNDRV_PCM_FORMAT_S16_LE:
924 case SNDRV_PCM_FORMAT_S20_3LE:
927 case SNDRV_PCM_FORMAT_S24_LE:
930 case SNDRV_PCM_FORMAT_S32_LE:
936 if (params_rate(params) * 384 == wm8753->pcmclk)
938 wm8753_write(codec, WM8753_SRATE1, srate);
940 wm8753_write(codec, WM8753_PCM, voice);
945 * Set's PCM dai fmt and BCLK.
947 static int wm8753_pcm_set_dai_fmt(struct snd_soc_dai *codec_dai,
950 struct snd_soc_codec *codec = codec_dai->codec;
953 voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x011f;
954 ioctl = wm8753_read_reg_cache(codec, WM8753_IOCTL) & 0x015d;
956 /* set master/slave audio interface */
957 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
958 case SND_SOC_DAIFMT_CBS_CFS:
960 case SND_SOC_DAIFMT_CBM_CFM:
962 case SND_SOC_DAIFMT_CBM_CFS:
969 /* clock inversion */
970 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
971 case SND_SOC_DAIFMT_DSP_A:
972 case SND_SOC_DAIFMT_DSP_B:
973 /* frame inversion not valid for DSP modes */
974 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
975 case SND_SOC_DAIFMT_NB_NF:
977 case SND_SOC_DAIFMT_IB_NF:
984 case SND_SOC_DAIFMT_I2S:
985 case SND_SOC_DAIFMT_RIGHT_J:
986 case SND_SOC_DAIFMT_LEFT_J:
988 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
989 case SND_SOC_DAIFMT_NB_NF:
991 case SND_SOC_DAIFMT_IB_IF:
994 case SND_SOC_DAIFMT_IB_NF:
997 case SND_SOC_DAIFMT_NB_IF:
1008 wm8753_write(codec, WM8753_PCM, voice);
1009 wm8753_write(codec, WM8753_IOCTL, ioctl);
1013 static int wm8753_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
1014 int div_id, int div)
1016 struct snd_soc_codec *codec = codec_dai->codec;
1021 reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0x003f;
1022 wm8753_write(codec, WM8753_CLOCK, reg | div);
1024 case WM8753_BCLKDIV:
1025 reg = wm8753_read_reg_cache(codec, WM8753_SRATE2) & 0x01c7;
1026 wm8753_write(codec, WM8753_SRATE2, reg | div);
1028 case WM8753_VXCLKDIV:
1029 reg = wm8753_read_reg_cache(codec, WM8753_SRATE2) & 0x003f;
1030 wm8753_write(codec, WM8753_SRATE2, reg | div);
1039 * Set's HiFi DAC format.
1041 static int wm8753_hdac_set_dai_fmt(struct snd_soc_dai *codec_dai,
1044 struct snd_soc_codec *codec = codec_dai->codec;
1045 u16 hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x01e0;
1047 /* interface format */
1048 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1049 case SND_SOC_DAIFMT_I2S:
1052 case SND_SOC_DAIFMT_RIGHT_J:
1054 case SND_SOC_DAIFMT_LEFT_J:
1057 case SND_SOC_DAIFMT_DSP_A:
1060 case SND_SOC_DAIFMT_DSP_B:
1067 wm8753_write(codec, WM8753_HIFI, hifi);
1072 * Set's I2S DAI format.
1074 static int wm8753_i2s_set_dai_fmt(struct snd_soc_dai *codec_dai,
1077 struct snd_soc_codec *codec = codec_dai->codec;
1080 hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x011f;
1081 ioctl = wm8753_read_reg_cache(codec, WM8753_IOCTL) & 0x00ae;
1083 /* set master/slave audio interface */
1084 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1085 case SND_SOC_DAIFMT_CBS_CFS:
1087 case SND_SOC_DAIFMT_CBM_CFM:
1089 case SND_SOC_DAIFMT_CBM_CFS:
1096 /* clock inversion */
1097 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1098 case SND_SOC_DAIFMT_DSP_A:
1099 case SND_SOC_DAIFMT_DSP_B:
1100 /* frame inversion not valid for DSP modes */
1101 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1102 case SND_SOC_DAIFMT_NB_NF:
1104 case SND_SOC_DAIFMT_IB_NF:
1111 case SND_SOC_DAIFMT_I2S:
1112 case SND_SOC_DAIFMT_RIGHT_J:
1113 case SND_SOC_DAIFMT_LEFT_J:
1115 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1116 case SND_SOC_DAIFMT_NB_NF:
1118 case SND_SOC_DAIFMT_IB_IF:
1121 case SND_SOC_DAIFMT_IB_NF:
1124 case SND_SOC_DAIFMT_NB_IF:
1135 wm8753_write(codec, WM8753_HIFI, hifi);
1136 wm8753_write(codec, WM8753_IOCTL, ioctl);
1141 * Set PCM DAI bit size and sample rate.
1143 static int wm8753_i2s_hw_params(struct snd_pcm_substream *substream,
1144 struct snd_pcm_hw_params *params,
1145 struct snd_soc_dai *dai)
1147 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1148 struct snd_soc_device *socdev = rtd->socdev;
1149 struct snd_soc_codec *codec = socdev->card->codec;
1150 struct wm8753_priv *wm8753 = codec->private_data;
1151 u16 srate = wm8753_read_reg_cache(codec, WM8753_SRATE1) & 0x01c0;
1152 u16 hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x01f3;
1155 /* is digital filter coefficient valid ? */
1156 coeff = get_coeff(wm8753->sysclk, params_rate(params));
1158 printk(KERN_ERR "wm8753 invalid MCLK or rate\n");
1161 wm8753_write(codec, WM8753_SRATE1, srate | (coeff_div[coeff].sr << 1) |
1162 coeff_div[coeff].usb);
1165 switch (params_format(params)) {
1166 case SNDRV_PCM_FORMAT_S16_LE:
1168 case SNDRV_PCM_FORMAT_S20_3LE:
1171 case SNDRV_PCM_FORMAT_S24_LE:
1174 case SNDRV_PCM_FORMAT_S32_LE:
1179 wm8753_write(codec, WM8753_HIFI, hifi);
1183 static int wm8753_mode1v_set_dai_fmt(struct snd_soc_dai *codec_dai,
1186 struct snd_soc_codec *codec = codec_dai->codec;
1189 /* set clk source as pcmclk */
1190 clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb;
1191 wm8753_write(codec, WM8753_CLOCK, clock);
1193 if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0)
1195 return wm8753_pcm_set_dai_fmt(codec_dai, fmt);
1198 static int wm8753_mode1h_set_dai_fmt(struct snd_soc_dai *codec_dai,
1201 if (wm8753_hdac_set_dai_fmt(codec_dai, fmt) < 0)
1203 return wm8753_i2s_set_dai_fmt(codec_dai, fmt);
1206 static int wm8753_mode2_set_dai_fmt(struct snd_soc_dai *codec_dai,
1209 struct snd_soc_codec *codec = codec_dai->codec;
1212 /* set clk source as pcmclk */
1213 clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb;
1214 wm8753_write(codec, WM8753_CLOCK, clock);
1216 if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0)
1218 return wm8753_i2s_set_dai_fmt(codec_dai, fmt);
1221 static int wm8753_mode3_4_set_dai_fmt(struct snd_soc_dai *codec_dai,
1224 struct snd_soc_codec *codec = codec_dai->codec;
1227 /* set clk source as mclk */
1228 clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb;
1229 wm8753_write(codec, WM8753_CLOCK, clock | 0x4);
1231 if (wm8753_hdac_set_dai_fmt(codec_dai, fmt) < 0)
1233 if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0)
1235 return wm8753_i2s_set_dai_fmt(codec_dai, fmt);
1238 static int wm8753_mute(struct snd_soc_dai *dai, int mute)
1240 struct snd_soc_codec *codec = dai->codec;
1241 u16 mute_reg = wm8753_read_reg_cache(codec, WM8753_DAC) & 0xfff7;
1243 /* the digital mute covers the HiFi and Voice DAC's on the WM8753.
1244 * make sure we check if they are not both active when we mute */
1245 if (mute && dai->id == 1) {
1246 if (!wm8753_dai[WM8753_DAI_VOICE].playback.active ||
1247 !wm8753_dai[WM8753_DAI_HIFI].playback.active)
1248 wm8753_write(codec, WM8753_DAC, mute_reg | 0x8);
1251 wm8753_write(codec, WM8753_DAC, mute_reg | 0x8);
1253 wm8753_write(codec, WM8753_DAC, mute_reg);
1259 static int wm8753_set_bias_level(struct snd_soc_codec *codec,
1260 enum snd_soc_bias_level level)
1262 u16 pwr_reg = wm8753_read_reg_cache(codec, WM8753_PWR1) & 0xfe3e;
1265 case SND_SOC_BIAS_ON:
1266 /* set vmid to 50k and unmute dac */
1267 wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x00c0);
1269 case SND_SOC_BIAS_PREPARE:
1270 /* set vmid to 5k for quick power up */
1271 wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x01c1);
1273 case SND_SOC_BIAS_STANDBY:
1274 /* mute dac and set vmid to 500k, enable VREF */
1275 wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x0141);
1277 case SND_SOC_BIAS_OFF:
1278 wm8753_write(codec, WM8753_PWR1, 0x0001);
1281 codec->bias_level = level;
1285 #define WM8753_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
1286 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
1287 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
1288 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
1290 #define WM8753_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1291 SNDRV_PCM_FMTBIT_S24_LE)
1294 * The WM8753 supports upto 4 different and mutually exclusive DAI
1295 * configurations. This gives 2 PCM's available for use, hifi and voice.
1296 * NOTE: The Voice PCM cannot play or capture audio to the CPU as it's DAI
1297 * is connected between the wm8753 and a BT codec or GSM modem.
1299 * 1. Voice over PCM DAI - HIFI DAC over HIFI DAI
1300 * 2. Voice over HIFI DAI - HIFI disabled
1301 * 3. Voice disabled - HIFI over HIFI
1302 * 4. Voice disabled - HIFI over HIFI, uses voice DAI LRC for capture
1304 static const struct snd_soc_dai wm8753_all_dai[] = {
1305 /* DAI HiFi mode 1 */
1306 { .name = "WM8753 HiFi",
1309 .stream_name = "HiFi Playback",
1312 .rates = WM8753_RATES,
1313 .formats = WM8753_FORMATS},
1314 .capture = { /* dummy for fast DAI switching */
1315 .stream_name = "Capture",
1318 .rates = WM8753_RATES,
1319 .formats = WM8753_FORMATS},
1321 .hw_params = wm8753_i2s_hw_params,
1322 .digital_mute = wm8753_mute,
1323 .set_fmt = wm8753_mode1h_set_dai_fmt,
1324 .set_clkdiv = wm8753_set_dai_clkdiv,
1325 .set_pll = wm8753_set_dai_pll,
1326 .set_sysclk = wm8753_set_dai_sysclk,
1329 /* DAI Voice mode 1 */
1330 { .name = "WM8753 Voice",
1333 .stream_name = "Voice Playback",
1336 .rates = WM8753_RATES,
1337 .formats = WM8753_FORMATS,},
1339 .stream_name = "Capture",
1342 .rates = WM8753_RATES,
1343 .formats = WM8753_FORMATS,},
1345 .hw_params = wm8753_pcm_hw_params,
1346 .digital_mute = wm8753_mute,
1347 .set_fmt = wm8753_mode1v_set_dai_fmt,
1348 .set_clkdiv = wm8753_set_dai_clkdiv,
1349 .set_pll = wm8753_set_dai_pll,
1350 .set_sysclk = wm8753_set_dai_sysclk,
1353 /* DAI HiFi mode 2 - dummy */
1354 { .name = "WM8753 HiFi",
1357 /* DAI Voice mode 2 */
1358 { .name = "WM8753 Voice",
1361 .stream_name = "Voice Playback",
1364 .rates = WM8753_RATES,
1365 .formats = WM8753_FORMATS,},
1367 .stream_name = "Capture",
1370 .rates = WM8753_RATES,
1371 .formats = WM8753_FORMATS,},
1373 .hw_params = wm8753_pcm_hw_params,
1374 .digital_mute = wm8753_mute,
1375 .set_fmt = wm8753_mode2_set_dai_fmt,
1376 .set_clkdiv = wm8753_set_dai_clkdiv,
1377 .set_pll = wm8753_set_dai_pll,
1378 .set_sysclk = wm8753_set_dai_sysclk,
1381 /* DAI HiFi mode 3 */
1382 { .name = "WM8753 HiFi",
1385 .stream_name = "HiFi Playback",
1388 .rates = WM8753_RATES,
1389 .formats = WM8753_FORMATS,},
1391 .stream_name = "Capture",
1394 .rates = WM8753_RATES,
1395 .formats = WM8753_FORMATS,},
1397 .hw_params = wm8753_i2s_hw_params,
1398 .digital_mute = wm8753_mute,
1399 .set_fmt = wm8753_mode3_4_set_dai_fmt,
1400 .set_clkdiv = wm8753_set_dai_clkdiv,
1401 .set_pll = wm8753_set_dai_pll,
1402 .set_sysclk = wm8753_set_dai_sysclk,
1405 /* DAI Voice mode 3 - dummy */
1406 { .name = "WM8753 Voice",
1409 /* DAI HiFi mode 4 */
1410 { .name = "WM8753 HiFi",
1413 .stream_name = "HiFi Playback",
1416 .rates = WM8753_RATES,
1417 .formats = WM8753_FORMATS,},
1419 .stream_name = "Capture",
1422 .rates = WM8753_RATES,
1423 .formats = WM8753_FORMATS,},
1425 .hw_params = wm8753_i2s_hw_params,
1426 .digital_mute = wm8753_mute,
1427 .set_fmt = wm8753_mode3_4_set_dai_fmt,
1428 .set_clkdiv = wm8753_set_dai_clkdiv,
1429 .set_pll = wm8753_set_dai_pll,
1430 .set_sysclk = wm8753_set_dai_sysclk,
1433 /* DAI Voice mode 4 - dummy */
1434 { .name = "WM8753 Voice",
1439 struct snd_soc_dai wm8753_dai[2];
1440 EXPORT_SYMBOL_GPL(wm8753_dai);
1442 static void wm8753_set_dai_mode(struct snd_soc_codec *codec, unsigned int mode)
1445 int playback_active, capture_active, codec_active, pop_wait;
1448 playback_active = wm8753_dai[0].playback.active;
1449 capture_active = wm8753_dai[0].capture.active;
1450 codec_active = wm8753_dai[0].active;
1451 private_data = wm8753_dai[0].private_data;
1452 pop_wait = wm8753_dai[0].pop_wait;
1453 wm8753_dai[0] = wm8753_all_dai[mode << 1];
1454 wm8753_dai[0].playback.active = playback_active;
1455 wm8753_dai[0].capture.active = capture_active;
1456 wm8753_dai[0].active = codec_active;
1457 wm8753_dai[0].private_data = private_data;
1458 wm8753_dai[0].pop_wait = pop_wait;
1460 playback_active = wm8753_dai[1].playback.active;
1461 capture_active = wm8753_dai[1].capture.active;
1462 codec_active = wm8753_dai[1].active;
1463 private_data = wm8753_dai[1].private_data;
1464 pop_wait = wm8753_dai[1].pop_wait;
1465 wm8753_dai[1] = wm8753_all_dai[(mode << 1) + 1];
1466 wm8753_dai[1].playback.active = playback_active;
1467 wm8753_dai[1].capture.active = capture_active;
1468 wm8753_dai[1].active = codec_active;
1469 wm8753_dai[1].private_data = private_data;
1470 wm8753_dai[1].pop_wait = pop_wait;
1472 wm8753_dai[0].codec = codec;
1473 wm8753_dai[1].codec = codec;
1476 static void wm8753_work(struct work_struct *work)
1478 struct snd_soc_codec *codec =
1479 container_of(work, struct snd_soc_codec, delayed_work.work);
1480 wm8753_set_bias_level(codec, codec->bias_level);
1483 static int wm8753_suspend(struct platform_device *pdev, pm_message_t state)
1485 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1486 struct snd_soc_codec *codec = socdev->card->codec;
1488 /* we only need to suspend if we are a valid card */
1492 wm8753_set_bias_level(codec, SND_SOC_BIAS_OFF);
1496 static int wm8753_resume(struct platform_device *pdev)
1498 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1499 struct snd_soc_codec *codec = socdev->card->codec;
1502 u16 *cache = codec->reg_cache;
1504 /* we only need to resume if we are a valid card */
1508 /* Sync reg_cache with the hardware */
1509 for (i = 0; i < ARRAY_SIZE(wm8753_reg); i++) {
1510 if (i + 1 == WM8753_RESET)
1512 data[0] = ((i + 1) << 1) | ((cache[i] >> 8) & 0x0001);
1513 data[1] = cache[i] & 0x00ff;
1514 codec->hw_write(codec->control_data, data, 2);
1517 wm8753_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1519 /* charge wm8753 caps */
1520 if (codec->suspend_bias_level == SND_SOC_BIAS_ON) {
1521 wm8753_set_bias_level(codec, SND_SOC_BIAS_PREPARE);
1522 codec->bias_level = SND_SOC_BIAS_ON;
1523 schedule_delayed_work(&codec->delayed_work,
1524 msecs_to_jiffies(caps_charge));
1531 * initialise the WM8753 driver
1532 * register the mixer and dsp interfaces with the kernel
1534 static int wm8753_init(struct snd_soc_device *socdev)
1536 struct snd_soc_codec *codec = socdev->card->codec;
1539 codec->name = "WM8753";
1540 codec->owner = THIS_MODULE;
1541 codec->read = wm8753_read_reg_cache;
1542 codec->write = wm8753_write;
1543 codec->set_bias_level = wm8753_set_bias_level;
1544 codec->dai = wm8753_dai;
1546 codec->reg_cache_size = ARRAY_SIZE(wm8753_reg);
1547 codec->reg_cache = kmemdup(wm8753_reg, sizeof(wm8753_reg), GFP_KERNEL);
1549 if (codec->reg_cache == NULL)
1552 wm8753_set_dai_mode(codec, 0);
1554 wm8753_reset(codec);
1557 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
1559 printk(KERN_ERR "wm8753: failed to create pcms\n");
1563 /* charge output caps */
1564 wm8753_set_bias_level(codec, SND_SOC_BIAS_PREPARE);
1565 codec->bias_level = SND_SOC_BIAS_STANDBY;
1566 schedule_delayed_work(&codec->delayed_work,
1567 msecs_to_jiffies(caps_charge));
1569 /* set the update bits */
1570 reg = wm8753_read_reg_cache(codec, WM8753_LDAC);
1571 wm8753_write(codec, WM8753_LDAC, reg | 0x0100);
1572 reg = wm8753_read_reg_cache(codec, WM8753_RDAC);
1573 wm8753_write(codec, WM8753_RDAC, reg | 0x0100);
1574 reg = wm8753_read_reg_cache(codec, WM8753_LADC);
1575 wm8753_write(codec, WM8753_LADC, reg | 0x0100);
1576 reg = wm8753_read_reg_cache(codec, WM8753_RADC);
1577 wm8753_write(codec, WM8753_RADC, reg | 0x0100);
1578 reg = wm8753_read_reg_cache(codec, WM8753_LOUT1V);
1579 wm8753_write(codec, WM8753_LOUT1V, reg | 0x0100);
1580 reg = wm8753_read_reg_cache(codec, WM8753_ROUT1V);
1581 wm8753_write(codec, WM8753_ROUT1V, reg | 0x0100);
1582 reg = wm8753_read_reg_cache(codec, WM8753_LOUT2V);
1583 wm8753_write(codec, WM8753_LOUT2V, reg | 0x0100);
1584 reg = wm8753_read_reg_cache(codec, WM8753_ROUT2V);
1585 wm8753_write(codec, WM8753_ROUT2V, reg | 0x0100);
1586 reg = wm8753_read_reg_cache(codec, WM8753_LINVOL);
1587 wm8753_write(codec, WM8753_LINVOL, reg | 0x0100);
1588 reg = wm8753_read_reg_cache(codec, WM8753_RINVOL);
1589 wm8753_write(codec, WM8753_RINVOL, reg | 0x0100);
1591 snd_soc_add_controls(codec, wm8753_snd_controls,
1592 ARRAY_SIZE(wm8753_snd_controls));
1593 wm8753_add_widgets(codec);
1594 ret = snd_soc_init_card(socdev);
1596 printk(KERN_ERR "wm8753: failed to register card\n");
1603 snd_soc_free_pcms(socdev);
1604 snd_soc_dapm_free(socdev);
1606 kfree(codec->reg_cache);
1610 /* If the i2c layer weren't so broken, we could pass this kind of data
1612 static struct snd_soc_device *wm8753_socdev;
1614 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1617 * WM8753 2 wire address is determined by GPIO5
1618 * state during powerup.
1623 static int wm8753_i2c_probe(struct i2c_client *i2c,
1624 const struct i2c_device_id *id)
1626 struct snd_soc_device *socdev = wm8753_socdev;
1627 struct snd_soc_codec *codec = socdev->card->codec;
1630 i2c_set_clientdata(i2c, codec);
1631 codec->control_data = i2c;
1633 ret = wm8753_init(socdev);
1635 pr_err("failed to initialise WM8753\n");
1640 static int wm8753_i2c_remove(struct i2c_client *client)
1642 struct snd_soc_codec *codec = i2c_get_clientdata(client);
1643 kfree(codec->reg_cache);
1647 static const struct i2c_device_id wm8753_i2c_id[] = {
1651 MODULE_DEVICE_TABLE(i2c, wm8753_i2c_id);
1653 static struct i2c_driver wm8753_i2c_driver = {
1655 .name = "WM8753 I2C Codec",
1656 .owner = THIS_MODULE,
1658 .probe = wm8753_i2c_probe,
1659 .remove = wm8753_i2c_remove,
1660 .id_table = wm8753_i2c_id,
1663 static int wm8753_add_i2c_device(struct platform_device *pdev,
1664 const struct wm8753_setup_data *setup)
1666 struct i2c_board_info info;
1667 struct i2c_adapter *adapter;
1668 struct i2c_client *client;
1671 ret = i2c_add_driver(&wm8753_i2c_driver);
1673 dev_err(&pdev->dev, "can't add i2c driver\n");
1677 memset(&info, 0, sizeof(struct i2c_board_info));
1678 info.addr = setup->i2c_address;
1679 strlcpy(info.type, "wm8753", I2C_NAME_SIZE);
1681 adapter = i2c_get_adapter(setup->i2c_bus);
1683 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
1688 client = i2c_new_device(adapter, &info);
1689 i2c_put_adapter(adapter);
1691 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
1692 (unsigned int)info.addr);
1699 i2c_del_driver(&wm8753_i2c_driver);
1704 #if defined(CONFIG_SPI_MASTER)
1705 static int __devinit wm8753_spi_probe(struct spi_device *spi)
1707 struct snd_soc_device *socdev = wm8753_socdev;
1708 struct snd_soc_codec *codec = socdev->card->codec;
1711 codec->control_data = spi;
1713 ret = wm8753_init(socdev);
1715 dev_err(&spi->dev, "failed to initialise WM8753\n");
1720 static int __devexit wm8753_spi_remove(struct spi_device *spi)
1725 static struct spi_driver wm8753_spi_driver = {
1728 .bus = &spi_bus_type,
1729 .owner = THIS_MODULE,
1731 .probe = wm8753_spi_probe,
1732 .remove = __devexit_p(wm8753_spi_remove),
1735 static int wm8753_spi_write(struct spi_device *spi, const char *data, int len)
1737 struct spi_transfer t;
1738 struct spi_message m;
1747 spi_message_init(&m);
1748 memset(&t, 0, (sizeof t));
1753 spi_message_add_tail(&t, &m);
1761 static int wm8753_probe(struct platform_device *pdev)
1763 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1764 struct wm8753_setup_data *setup;
1765 struct snd_soc_codec *codec;
1766 struct wm8753_priv *wm8753;
1769 pr_info("WM8753 Audio Codec %s", WM8753_VERSION);
1771 setup = socdev->codec_data;
1772 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
1776 wm8753 = kzalloc(sizeof(struct wm8753_priv), GFP_KERNEL);
1777 if (wm8753 == NULL) {
1782 codec->private_data = wm8753;
1783 socdev->card->codec = codec;
1784 mutex_init(&codec->mutex);
1785 INIT_LIST_HEAD(&codec->dapm_widgets);
1786 INIT_LIST_HEAD(&codec->dapm_paths);
1787 wm8753_socdev = socdev;
1788 INIT_DELAYED_WORK(&codec->delayed_work, wm8753_work);
1790 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1791 if (setup->i2c_address) {
1792 codec->hw_write = (hw_write_t)i2c_master_send;
1793 ret = wm8753_add_i2c_device(pdev, setup);
1796 #if defined(CONFIG_SPI_MASTER)
1798 codec->hw_write = (hw_write_t)wm8753_spi_write;
1799 ret = spi_register_driver(&wm8753_spi_driver);
1801 printk(KERN_ERR "can't add spi driver");
1806 kfree(codec->private_data);
1813 * This function forces any delayed work to be queued and run.
1815 static int run_delayed_work(struct delayed_work *dwork)
1819 /* cancel any work waiting to be queued. */
1820 ret = cancel_delayed_work(dwork);
1822 /* if there was any work waiting then we run it now and
1823 * wait for it's completion */
1825 schedule_delayed_work(dwork, 0);
1826 flush_scheduled_work();
1831 /* power down chip */
1832 static int wm8753_remove(struct platform_device *pdev)
1834 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1835 struct snd_soc_codec *codec = socdev->card->codec;
1837 if (codec->control_data)
1838 wm8753_set_bias_level(codec, SND_SOC_BIAS_OFF);
1839 run_delayed_work(&codec->delayed_work);
1840 snd_soc_free_pcms(socdev);
1841 snd_soc_dapm_free(socdev);
1842 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1843 i2c_unregister_device(codec->control_data);
1844 i2c_del_driver(&wm8753_i2c_driver);
1846 #if defined(CONFIG_SPI_MASTER)
1847 spi_unregister_driver(&wm8753_spi_driver);
1849 kfree(codec->private_data);
1855 struct snd_soc_codec_device soc_codec_dev_wm8753 = {
1856 .probe = wm8753_probe,
1857 .remove = wm8753_remove,
1858 .suspend = wm8753_suspend,
1859 .resume = wm8753_resume,
1861 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8753);
1863 static int __init wm8753_modinit(void)
1865 return snd_soc_register_dais(wm8753_dai, ARRAY_SIZE(wm8753_dai));
1867 module_init(wm8753_modinit);
1869 static void __exit wm8753_exit(void)
1871 snd_soc_unregister_dais(wm8753_dai, ARRAY_SIZE(wm8753_dai));
1873 module_exit(wm8753_exit);
1875 MODULE_DESCRIPTION("ASoC WM8753 driver");
1876 MODULE_AUTHOR("Liam Girdwood");
1877 MODULE_LICENSE("GPL");