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/version.h>
38 #include <linux/kernel.h>
39 #include <linux/init.h>
40 #include <linux/delay.h>
42 #include <linux/i2c.h>
43 #include <linux/platform_device.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 AUDIO_NAME "wm8753"
56 #define WM8753_VERSION "0.16"
62 #define WM8753_DEBUG 0
65 #define dbg(format, arg...) \
66 printk(KERN_DEBUG AUDIO_NAME ": " format "\n" , ## arg)
68 #define dbg(format, arg...) do {} while (0)
70 #define err(format, arg...) \
71 printk(KERN_ERR AUDIO_NAME ": " format "\n" , ## arg)
72 #define info(format, arg...) \
73 printk(KERN_INFO AUDIO_NAME ": " format "\n" , ## arg)
74 #define warn(format, arg...) \
75 printk(KERN_WARNING AUDIO_NAME ": " format "\n" , ## arg)
77 static int caps_charge = 2000;
78 module_param(caps_charge, int, 0);
79 MODULE_PARM_DESC(caps_charge, "WM8753 cap charge time (msecs)");
81 static void wm8753_set_dai_mode(struct snd_soc_codec *codec,
84 /* codec private data */
91 * wm8753 register cache
92 * We can't read the WM8753 register space when we
93 * are using 2 wire for device control, so we cache them instead.
95 static const u16 wm8753_reg[] = {
96 0x0008, 0x0000, 0x000a, 0x000a,
97 0x0033, 0x0000, 0x0007, 0x00ff,
98 0x00ff, 0x000f, 0x000f, 0x007b,
99 0x0000, 0x0032, 0x0000, 0x00c3,
100 0x00c3, 0x00c0, 0x0000, 0x0000,
101 0x0000, 0x0000, 0x0000, 0x0000,
102 0x0000, 0x0000, 0x0000, 0x0000,
103 0x0000, 0x0000, 0x0000, 0x0055,
104 0x0005, 0x0050, 0x0055, 0x0050,
105 0x0055, 0x0050, 0x0055, 0x0079,
106 0x0079, 0x0079, 0x0079, 0x0079,
107 0x0000, 0x0000, 0x0000, 0x0000,
108 0x0097, 0x0097, 0x0000, 0x0004,
109 0x0000, 0x0083, 0x0024, 0x01ba,
110 0x0000, 0x0083, 0x0024, 0x01ba,
115 * read wm8753 register cache
117 static inline unsigned int wm8753_read_reg_cache(struct snd_soc_codec *codec,
120 u16 *cache = codec->reg_cache;
121 if (reg < 1 || reg > (ARRAY_SIZE(wm8753_reg) + 1))
123 return cache[reg - 1];
127 * write wm8753 register cache
129 static inline void wm8753_write_reg_cache(struct snd_soc_codec *codec,
130 unsigned int reg, unsigned int value)
132 u16 *cache = codec->reg_cache;
133 if (reg < 1 || reg > 0x3f)
135 cache[reg - 1] = value;
139 * write to the WM8753 register space
141 static int wm8753_write(struct snd_soc_codec *codec, unsigned int reg,
147 * D15..D9 WM8753 register offset
148 * D8...D0 register data
150 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
151 data[1] = value & 0x00ff;
153 wm8753_write_reg_cache(codec, reg, value);
154 if (codec->hw_write(codec->control_data, data, 2) == 2)
160 #define wm8753_reset(c) wm8753_write(c, WM8753_RESET, 0)
165 static const char *wm8753_base[] = {"Linear Control", "Adaptive Boost"};
166 static const char *wm8753_base_filter[] =
167 {"130Hz @ 48kHz", "200Hz @ 48kHz", "100Hz @ 16kHz", "400Hz @ 48kHz",
168 "100Hz @ 8kHz", "200Hz @ 8kHz"};
169 static const char *wm8753_treble[] = {"8kHz", "4kHz"};
170 static const char *wm8753_alc_func[] = {"Off", "Right", "Left", "Stereo"};
171 static const char *wm8753_ng_type[] = {"Constant PGA Gain", "Mute ADC Output"};
172 static const char *wm8753_3d_func[] = {"Capture", "Playback"};
173 static const char *wm8753_3d_uc[] = {"2.2kHz", "1.5kHz"};
174 static const char *wm8753_3d_lc[] = {"200Hz", "500Hz"};
175 static const char *wm8753_deemp[] = {"None", "32kHz", "44.1kHz", "48kHz"};
176 static const char *wm8753_mono_mix[] = {"Stereo", "Left", "Right", "Mono"};
177 static const char *wm8753_dac_phase[] = {"Non Inverted", "Inverted"};
178 static const char *wm8753_line_mix[] = {"Line 1 + 2", "Line 1 - 2",
180 static const char *wm8753_mono_mux[] = {"Line Mix", "Rx Mix"};
181 static const char *wm8753_right_mux[] = {"Line 2", "Rx Mix"};
182 static const char *wm8753_left_mux[] = {"Line 1", "Rx Mix"};
183 static const char *wm8753_rxmsel[] = {"RXP - RXN", "RXP + RXN", "RXP", "RXN"};
184 static const char *wm8753_sidetone_mux[] = {"Left PGA", "Mic 1", "Mic 2",
186 static const char *wm8753_mono2_src[] = {"Inverted Mono 1", "Left", "Right",
188 static const char *wm8753_out3[] = {"VREF", "ROUT2", "Left + Right"};
189 static const char *wm8753_out4[] = {"VREF", "Capture ST", "LOUT2"};
190 static const char *wm8753_radcsel[] = {"PGA", "Line or RXP-RXN", "Sidetone"};
191 static const char *wm8753_ladcsel[] = {"PGA", "Line or RXP-RXN", "Line"};
192 static const char *wm8753_mono_adc[] = {"Stereo", "Analogue Mix Left",
193 "Analogue Mix Right", "Digital Mono Mix"};
194 static const char *wm8753_adc_hp[] = {"3.4Hz @ 48kHz", "82Hz @ 16k",
195 "82Hz @ 8kHz", "170Hz @ 8kHz"};
196 static const char *wm8753_adc_filter[] = {"HiFi", "Voice"};
197 static const char *wm8753_mic_sel[] = {"Mic 1", "Mic 2", "Mic 3"};
198 static const char *wm8753_dai_mode[] = {"DAI 0", "DAI 1", "DAI 2", "DAI 3"};
199 static const char *wm8753_dat_sel[] = {"Stereo", "Left ADC", "Right ADC",
201 static const char *wm8753_rout2_phase[] = {"Non Inverted", "Inverted"};
203 static const struct soc_enum wm8753_enum[] = {
204 SOC_ENUM_SINGLE(WM8753_BASS, 7, 2, wm8753_base),
205 SOC_ENUM_SINGLE(WM8753_BASS, 4, 6, wm8753_base_filter),
206 SOC_ENUM_SINGLE(WM8753_TREBLE, 6, 2, wm8753_treble),
207 SOC_ENUM_SINGLE(WM8753_ALC1, 7, 4, wm8753_alc_func),
208 SOC_ENUM_SINGLE(WM8753_NGATE, 1, 2, wm8753_ng_type),
209 SOC_ENUM_SINGLE(WM8753_3D, 7, 2, wm8753_3d_func),
210 SOC_ENUM_SINGLE(WM8753_3D, 6, 2, wm8753_3d_uc),
211 SOC_ENUM_SINGLE(WM8753_3D, 5, 2, wm8753_3d_lc),
212 SOC_ENUM_SINGLE(WM8753_DAC, 1, 4, wm8753_deemp),
213 SOC_ENUM_SINGLE(WM8753_DAC, 4, 4, wm8753_mono_mix),
214 SOC_ENUM_SINGLE(WM8753_DAC, 6, 2, wm8753_dac_phase),
215 SOC_ENUM_SINGLE(WM8753_INCTL1, 3, 4, wm8753_line_mix),
216 SOC_ENUM_SINGLE(WM8753_INCTL1, 2, 2, wm8753_mono_mux),
217 SOC_ENUM_SINGLE(WM8753_INCTL1, 1, 2, wm8753_right_mux),
218 SOC_ENUM_SINGLE(WM8753_INCTL1, 0, 2, wm8753_left_mux),
219 SOC_ENUM_SINGLE(WM8753_INCTL2, 6, 4, wm8753_rxmsel),
220 SOC_ENUM_SINGLE(WM8753_INCTL2, 4, 4, wm8753_sidetone_mux),
221 SOC_ENUM_SINGLE(WM8753_OUTCTL, 7, 4, wm8753_mono2_src),
222 SOC_ENUM_SINGLE(WM8753_OUTCTL, 0, 3, wm8753_out3),
223 SOC_ENUM_SINGLE(WM8753_ADCTL2, 7, 3, wm8753_out4),
224 SOC_ENUM_SINGLE(WM8753_ADCIN, 2, 3, wm8753_radcsel),
225 SOC_ENUM_SINGLE(WM8753_ADCIN, 0, 3, wm8753_ladcsel),
226 SOC_ENUM_SINGLE(WM8753_ADCIN, 4, 4, wm8753_mono_adc),
227 SOC_ENUM_SINGLE(WM8753_ADC, 2, 4, wm8753_adc_hp),
228 SOC_ENUM_SINGLE(WM8753_ADC, 4, 2, wm8753_adc_filter),
229 SOC_ENUM_SINGLE(WM8753_MICBIAS, 6, 3, wm8753_mic_sel),
230 SOC_ENUM_SINGLE(WM8753_IOCTL, 2, 4, wm8753_dai_mode),
231 SOC_ENUM_SINGLE(WM8753_ADC, 7, 4, wm8753_dat_sel),
232 SOC_ENUM_SINGLE(WM8753_OUTCTL, 2, 2, wm8753_rout2_phase),
236 static int wm8753_get_dai(struct snd_kcontrol *kcontrol,
237 struct snd_ctl_elem_value *ucontrol)
239 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
240 int mode = wm8753_read_reg_cache(codec, WM8753_IOCTL);
242 ucontrol->value.integer.value[0] = (mode & 0xc) >> 2;
246 static int wm8753_set_dai(struct snd_kcontrol *kcontrol,
247 struct snd_ctl_elem_value *ucontrol)
249 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
250 int mode = wm8753_read_reg_cache(codec, WM8753_IOCTL);
252 if (((mode & 0xc) >> 2) == ucontrol->value.integer.value[0])
256 mode |= (ucontrol->value.integer.value[0] << 2);
258 wm8753_write(codec, WM8753_IOCTL, mode);
259 wm8753_set_dai_mode(codec, ucontrol->value.integer.value[0]);
263 static const DECLARE_TLV_DB_LINEAR(rec_mix_tlv, -1500, 600);
265 static const struct snd_kcontrol_new wm8753_snd_controls[] = {
266 SOC_DOUBLE_R("PCM Volume", WM8753_LDAC, WM8753_RDAC, 0, 255, 0),
268 SOC_DOUBLE_R("ADC Capture Volume", WM8753_LADC, WM8753_RADC, 0, 255, 0),
270 SOC_DOUBLE_R("Headphone Playback Volume", WM8753_LOUT1V, WM8753_ROUT1V, 0, 127, 0),
271 SOC_DOUBLE_R("Speaker Playback Volume", WM8753_LOUT2V, WM8753_ROUT2V, 0, 127, 0),
273 SOC_SINGLE("Mono Playback Volume", WM8753_MOUTV, 0, 127, 0),
275 SOC_DOUBLE_R("Bypass Playback Volume", WM8753_LOUTM1, WM8753_ROUTM1, 4, 7, 1),
276 SOC_DOUBLE_R("Sidetone Playback Volume", WM8753_LOUTM2, WM8753_ROUTM2, 4, 7, 1),
277 SOC_DOUBLE_R("Voice Playback Volume", WM8753_LOUTM2, WM8753_ROUTM2, 0, 7, 1),
279 SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8753_LOUT1V, WM8753_ROUT1V, 7, 1, 0),
280 SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8753_LOUT2V, WM8753_ROUT2V, 7, 1, 0),
282 SOC_SINGLE("Mono Bypass Playback Volume", WM8753_MOUTM1, 4, 7, 1),
283 SOC_SINGLE("Mono Sidetone Playback Volume", WM8753_MOUTM2, 4, 7, 1),
284 SOC_SINGLE("Mono Voice Playback Volume", WM8753_MOUTM2, 0, 7, 1),
285 SOC_SINGLE("Mono Playback ZC Switch", WM8753_MOUTV, 7, 1, 0),
287 SOC_ENUM("Bass Boost", wm8753_enum[0]),
288 SOC_ENUM("Bass Filter", wm8753_enum[1]),
289 SOC_SINGLE("Bass Volume", WM8753_BASS, 0, 15, 1),
291 SOC_SINGLE("Treble Volume", WM8753_TREBLE, 0, 15, 1),
292 SOC_ENUM("Treble Cut-off", wm8753_enum[2]),
294 SOC_DOUBLE_TLV("Sidetone Capture Volume", WM8753_RECMIX1, 0, 4, 7, 1, rec_mix_tlv),
295 SOC_SINGLE_TLV("Voice Sidetone Capture Volume", WM8753_RECMIX2, 0, 7, 1, rec_mix_tlv),
297 SOC_DOUBLE_R("Capture Volume", WM8753_LINVOL, WM8753_RINVOL, 0, 63, 0),
298 SOC_DOUBLE_R("Capture ZC Switch", WM8753_LINVOL, WM8753_RINVOL, 6, 1, 0),
299 SOC_DOUBLE_R("Capture Switch", WM8753_LINVOL, WM8753_RINVOL, 7, 1, 1),
301 SOC_ENUM("Capture Filter Select", wm8753_enum[23]),
302 SOC_ENUM("Capture Filter Cut-off", wm8753_enum[24]),
303 SOC_SINGLE("Capture Filter Switch", WM8753_ADC, 0, 1, 1),
305 SOC_SINGLE("ALC Capture Target Volume", WM8753_ALC1, 0, 7, 0),
306 SOC_SINGLE("ALC Capture Max Volume", WM8753_ALC1, 4, 7, 0),
307 SOC_ENUM("ALC Capture Function", wm8753_enum[3]),
308 SOC_SINGLE("ALC Capture ZC Switch", WM8753_ALC2, 8, 1, 0),
309 SOC_SINGLE("ALC Capture Hold Time", WM8753_ALC2, 0, 15, 1),
310 SOC_SINGLE("ALC Capture Decay Time", WM8753_ALC3, 4, 15, 1),
311 SOC_SINGLE("ALC Capture Attack Time", WM8753_ALC3, 0, 15, 0),
312 SOC_SINGLE("ALC Capture NG Threshold", WM8753_NGATE, 3, 31, 0),
313 SOC_ENUM("ALC Capture NG Type", wm8753_enum[4]),
314 SOC_SINGLE("ALC Capture NG Switch", WM8753_NGATE, 0, 1, 0),
316 SOC_ENUM("3D Function", wm8753_enum[5]),
317 SOC_ENUM("3D Upper Cut-off", wm8753_enum[6]),
318 SOC_ENUM("3D Lower Cut-off", wm8753_enum[7]),
319 SOC_SINGLE("3D Volume", WM8753_3D, 1, 15, 0),
320 SOC_SINGLE("3D Switch", WM8753_3D, 0, 1, 0),
322 SOC_SINGLE("Capture 6dB Attenuate", WM8753_ADCTL1, 2, 1, 0),
323 SOC_SINGLE("Playback 6dB Attenuate", WM8753_ADCTL1, 1, 1, 0),
325 SOC_ENUM("De-emphasis", wm8753_enum[8]),
326 SOC_ENUM("Playback Mono Mix", wm8753_enum[9]),
327 SOC_ENUM("Playback Phase", wm8753_enum[10]),
329 SOC_SINGLE("Mic2 Capture Volume", WM8753_INCTL1, 7, 3, 0),
330 SOC_SINGLE("Mic1 Capture Volume", WM8753_INCTL1, 5, 3, 0),
332 SOC_ENUM_EXT("DAI Mode", wm8753_enum[26], wm8753_get_dai, wm8753_set_dai),
334 SOC_ENUM("ADC Data Select", wm8753_enum[27]),
335 SOC_ENUM("ROUT2 Phase", wm8753_enum[28]),
338 /* add non dapm controls */
339 static int wm8753_add_controls(struct snd_soc_codec *codec)
343 for (i = 0; i < ARRAY_SIZE(wm8753_snd_controls); i++) {
344 err = snd_ctl_add(codec->card,
345 snd_soc_cnew(&wm8753_snd_controls[i],
358 static const struct snd_kcontrol_new wm8753_left_mixer_controls[] = {
359 SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_LOUTM2, 8, 1, 0),
360 SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_LOUTM2, 7, 1, 0),
361 SOC_DAPM_SINGLE("Left Playback Switch", WM8753_LOUTM1, 8, 1, 0),
362 SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_LOUTM1, 7, 1, 0),
366 static const struct snd_kcontrol_new wm8753_right_mixer_controls[] = {
367 SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_ROUTM2, 8, 1, 0),
368 SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_ROUTM2, 7, 1, 0),
369 SOC_DAPM_SINGLE("Right Playback Switch", WM8753_ROUTM1, 8, 1, 0),
370 SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_ROUTM1, 7, 1, 0),
374 static const struct snd_kcontrol_new wm8753_mono_mixer_controls[] = {
375 SOC_DAPM_SINGLE("Left Playback Switch", WM8753_MOUTM1, 8, 1, 0),
376 SOC_DAPM_SINGLE("Right Playback Switch", WM8753_MOUTM2, 8, 1, 0),
377 SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_MOUTM2, 3, 1, 0),
378 SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_MOUTM2, 7, 1, 0),
379 SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_MOUTM1, 7, 1, 0),
383 static const struct snd_kcontrol_new wm8753_mono2_controls =
384 SOC_DAPM_ENUM("Route", wm8753_enum[17]);
387 static const struct snd_kcontrol_new wm8753_out3_controls =
388 SOC_DAPM_ENUM("Route", wm8753_enum[18]);
391 static const struct snd_kcontrol_new wm8753_out4_controls =
392 SOC_DAPM_ENUM("Route", wm8753_enum[19]);
395 static const struct snd_kcontrol_new wm8753_adc_mono_controls =
396 SOC_DAPM_ENUM("Route", wm8753_enum[22]);
399 static const struct snd_kcontrol_new wm8753_record_mixer_controls[] = {
400 SOC_DAPM_SINGLE("Voice Capture Switch", WM8753_RECMIX2, 3, 1, 0),
401 SOC_DAPM_SINGLE("Left Capture Switch", WM8753_RECMIX1, 3, 1, 0),
402 SOC_DAPM_SINGLE("Right Capture Switch", WM8753_RECMIX1, 7, 1, 0),
406 static const struct snd_kcontrol_new wm8753_adc_left_controls =
407 SOC_DAPM_ENUM("Route", wm8753_enum[21]);
410 static const struct snd_kcontrol_new wm8753_adc_right_controls =
411 SOC_DAPM_ENUM("Route", wm8753_enum[20]);
414 static const struct snd_kcontrol_new wm8753_mic_mux_controls =
415 SOC_DAPM_ENUM("Route", wm8753_enum[16]);
418 static const struct snd_kcontrol_new wm8753_alc_mixer_controls[] = {
419 SOC_DAPM_SINGLE("Line Capture Switch", WM8753_INCTL2, 3, 1, 0),
420 SOC_DAPM_SINGLE("Mic2 Capture Switch", WM8753_INCTL2, 2, 1, 0),
421 SOC_DAPM_SINGLE("Mic1 Capture Switch", WM8753_INCTL2, 1, 1, 0),
422 SOC_DAPM_SINGLE("Rx Capture Switch", WM8753_INCTL2, 0, 1, 0),
426 static const struct snd_kcontrol_new wm8753_line_left_controls =
427 SOC_DAPM_ENUM("Route", wm8753_enum[14]);
430 static const struct snd_kcontrol_new wm8753_line_right_controls =
431 SOC_DAPM_ENUM("Route", wm8753_enum[13]);
434 static const struct snd_kcontrol_new wm8753_line_mono_controls =
435 SOC_DAPM_ENUM("Route", wm8753_enum[12]);
437 /* Line mux and mixer */
438 static const struct snd_kcontrol_new wm8753_line_mux_mix_controls =
439 SOC_DAPM_ENUM("Route", wm8753_enum[11]);
441 /* Rx mux and mixer */
442 static const struct snd_kcontrol_new wm8753_rx_mux_mix_controls =
443 SOC_DAPM_ENUM("Route", wm8753_enum[15]);
445 /* Mic Selector Mux */
446 static const struct snd_kcontrol_new wm8753_mic_sel_mux_controls =
447 SOC_DAPM_ENUM("Route", wm8753_enum[25]);
449 static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
450 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8753_PWR1, 5, 0),
451 SND_SOC_DAPM_MIXER("Left Mixer", WM8753_PWR4, 0, 0,
452 &wm8753_left_mixer_controls[0], ARRAY_SIZE(wm8753_left_mixer_controls)),
453 SND_SOC_DAPM_PGA("Left Out 1", WM8753_PWR3, 8, 0, NULL, 0),
454 SND_SOC_DAPM_PGA("Left Out 2", WM8753_PWR3, 6, 0, NULL, 0),
455 SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", WM8753_PWR1, 3, 0),
456 SND_SOC_DAPM_OUTPUT("LOUT1"),
457 SND_SOC_DAPM_OUTPUT("LOUT2"),
458 SND_SOC_DAPM_MIXER("Right Mixer", WM8753_PWR4, 1, 0,
459 &wm8753_right_mixer_controls[0], ARRAY_SIZE(wm8753_right_mixer_controls)),
460 SND_SOC_DAPM_PGA("Right Out 1", WM8753_PWR3, 7, 0, NULL, 0),
461 SND_SOC_DAPM_PGA("Right Out 2", WM8753_PWR3, 5, 0, NULL, 0),
462 SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", WM8753_PWR1, 2, 0),
463 SND_SOC_DAPM_OUTPUT("ROUT1"),
464 SND_SOC_DAPM_OUTPUT("ROUT2"),
465 SND_SOC_DAPM_MIXER("Mono Mixer", WM8753_PWR4, 2, 0,
466 &wm8753_mono_mixer_controls[0], ARRAY_SIZE(wm8753_mono_mixer_controls)),
467 SND_SOC_DAPM_PGA("Mono Out 1", WM8753_PWR3, 2, 0, NULL, 0),
468 SND_SOC_DAPM_PGA("Mono Out 2", WM8753_PWR3, 1, 0, NULL, 0),
469 SND_SOC_DAPM_DAC("Voice DAC", "Voice Playback", WM8753_PWR1, 4, 0),
470 SND_SOC_DAPM_OUTPUT("MONO1"),
471 SND_SOC_DAPM_MUX("Mono 2 Mux", SND_SOC_NOPM, 0, 0, &wm8753_mono2_controls),
472 SND_SOC_DAPM_OUTPUT("MONO2"),
473 SND_SOC_DAPM_MIXER("Out3 Left + Right", -1, 0, 0, NULL, 0),
474 SND_SOC_DAPM_MUX("Out3 Mux", SND_SOC_NOPM, 0, 0, &wm8753_out3_controls),
475 SND_SOC_DAPM_PGA("Out 3", WM8753_PWR3, 4, 0, NULL, 0),
476 SND_SOC_DAPM_OUTPUT("OUT3"),
477 SND_SOC_DAPM_MUX("Out4 Mux", SND_SOC_NOPM, 0, 0, &wm8753_out4_controls),
478 SND_SOC_DAPM_PGA("Out 4", WM8753_PWR3, 3, 0, NULL, 0),
479 SND_SOC_DAPM_OUTPUT("OUT4"),
480 SND_SOC_DAPM_MIXER("Playback Mixer", WM8753_PWR4, 3, 0,
481 &wm8753_record_mixer_controls[0],
482 ARRAY_SIZE(wm8753_record_mixer_controls)),
483 SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8753_PWR2, 3, 0),
484 SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8753_PWR2, 2, 0),
485 SND_SOC_DAPM_MUX("Capture Left Mixer", SND_SOC_NOPM, 0, 0,
486 &wm8753_adc_mono_controls),
487 SND_SOC_DAPM_MUX("Capture Right Mixer", SND_SOC_NOPM, 0, 0,
488 &wm8753_adc_mono_controls),
489 SND_SOC_DAPM_MUX("Capture Left Mux", SND_SOC_NOPM, 0, 0,
490 &wm8753_adc_left_controls),
491 SND_SOC_DAPM_MUX("Capture Right Mux", SND_SOC_NOPM, 0, 0,
492 &wm8753_adc_right_controls),
493 SND_SOC_DAPM_MUX("Mic Sidetone Mux", SND_SOC_NOPM, 0, 0,
494 &wm8753_mic_mux_controls),
495 SND_SOC_DAPM_PGA("Left Capture Volume", WM8753_PWR2, 5, 0, NULL, 0),
496 SND_SOC_DAPM_PGA("Right Capture Volume", WM8753_PWR2, 4, 0, NULL, 0),
497 SND_SOC_DAPM_MIXER("ALC Mixer", WM8753_PWR2, 6, 0,
498 &wm8753_alc_mixer_controls[0], ARRAY_SIZE(wm8753_alc_mixer_controls)),
499 SND_SOC_DAPM_MUX("Line Left Mux", SND_SOC_NOPM, 0, 0,
500 &wm8753_line_left_controls),
501 SND_SOC_DAPM_MUX("Line Right Mux", SND_SOC_NOPM, 0, 0,
502 &wm8753_line_right_controls),
503 SND_SOC_DAPM_MUX("Line Mono Mux", SND_SOC_NOPM, 0, 0,
504 &wm8753_line_mono_controls),
505 SND_SOC_DAPM_MUX("Line Mixer", WM8753_PWR2, 0, 0,
506 &wm8753_line_mux_mix_controls),
507 SND_SOC_DAPM_MUX("Rx Mixer", WM8753_PWR2, 1, 0,
508 &wm8753_rx_mux_mix_controls),
509 SND_SOC_DAPM_PGA("Mic 1 Volume", WM8753_PWR2, 8, 0, NULL, 0),
510 SND_SOC_DAPM_PGA("Mic 2 Volume", WM8753_PWR2, 7, 0, NULL, 0),
511 SND_SOC_DAPM_MUX("Mic Selection Mux", SND_SOC_NOPM, 0, 0,
512 &wm8753_mic_sel_mux_controls),
513 SND_SOC_DAPM_INPUT("LINE1"),
514 SND_SOC_DAPM_INPUT("LINE2"),
515 SND_SOC_DAPM_INPUT("RXP"),
516 SND_SOC_DAPM_INPUT("RXN"),
517 SND_SOC_DAPM_INPUT("ACIN"),
518 SND_SOC_DAPM_OUTPUT("ACOP"),
519 SND_SOC_DAPM_INPUT("MIC1N"),
520 SND_SOC_DAPM_INPUT("MIC1"),
521 SND_SOC_DAPM_INPUT("MIC2N"),
522 SND_SOC_DAPM_INPUT("MIC2"),
523 SND_SOC_DAPM_VMID("VREF"),
526 static const char *audio_map[][3] = {
528 {"Left Mixer", "Left Playback Switch", "Left DAC"},
529 {"Left Mixer", "Voice Playback Switch", "Voice DAC"},
530 {"Left Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
531 {"Left Mixer", "Bypass Playback Switch", "Line Left Mux"},
534 {"Right Mixer", "Right Playback Switch", "Right DAC"},
535 {"Right Mixer", "Voice Playback Switch", "Voice DAC"},
536 {"Right Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
537 {"Right Mixer", "Bypass Playback Switch", "Line Right Mux"},
540 {"Mono Mixer", "Voice Playback Switch", "Voice DAC"},
541 {"Mono Mixer", "Left Playback Switch", "Left DAC"},
542 {"Mono Mixer", "Right Playback Switch", "Right DAC"},
543 {"Mono Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
544 {"Mono Mixer", "Bypass Playback Switch", "Line Mono Mux"},
547 {"Left Out 1", NULL, "Left Mixer"},
548 {"Left Out 2", NULL, "Left Mixer"},
549 {"LOUT1", NULL, "Left Out 1"},
550 {"LOUT2", NULL, "Left Out 2"},
553 {"Right Out 1", NULL, "Right Mixer"},
554 {"Right Out 2", NULL, "Right Mixer"},
555 {"ROUT1", NULL, "Right Out 1"},
556 {"ROUT2", NULL, "Right Out 2"},
559 {"Mono Out 1", NULL, "Mono Mixer"},
560 {"MONO1", NULL, "Mono Out 1"},
563 {"Mono 2 Mux", "Left + Right", "Out3 Left + Right"},
564 {"Mono 2 Mux", "Inverted Mono 1", "MONO1"},
565 {"Mono 2 Mux", "Left", "Left Mixer"},
566 {"Mono 2 Mux", "Right", "Right Mixer"},
567 {"Mono Out 2", NULL, "Mono 2 Mux"},
568 {"MONO2", NULL, "Mono Out 2"},
571 {"Out3 Left + Right", NULL, "Left Mixer"},
572 {"Out3 Left + Right", NULL, "Right Mixer"},
573 {"Out3 Mux", "VREF", "VREF"},
574 {"Out3 Mux", "Left + Right", "Out3 Left + Right"},
575 {"Out3 Mux", "ROUT2", "ROUT2"},
576 {"Out 3", NULL, "Out3 Mux"},
577 {"OUT3", NULL, "Out 3"},
580 {"Out4 Mux", "VREF", "VREF"},
581 {"Out4 Mux", "Capture ST", "Capture ST Mixer"},
582 {"Out4 Mux", "LOUT2", "LOUT2"},
583 {"Out 4", NULL, "Out4 Mux"},
584 {"OUT4", NULL, "Out 4"},
587 {"Playback Mixer", "Left Capture Switch", "Left Mixer"},
588 {"Playback Mixer", "Voice Capture Switch", "Mono Mixer"},
589 {"Playback Mixer", "Right Capture Switch", "Right Mixer"},
591 /* Mic/SideTone Mux */
592 {"Mic Sidetone Mux", "Left PGA", "Left Capture Volume"},
593 {"Mic Sidetone Mux", "Right PGA", "Right Capture Volume"},
594 {"Mic Sidetone Mux", "Mic 1", "Mic 1 Volume"},
595 {"Mic Sidetone Mux", "Mic 2", "Mic 2 Volume"},
597 /* Capture Left Mux */
598 {"Capture Left Mux", "PGA", "Left Capture Volume"},
599 {"Capture Left Mux", "Line or RXP-RXN", "Line Left Mux"},
600 {"Capture Left Mux", "Line", "LINE1"},
602 /* Capture Right Mux */
603 {"Capture Right Mux", "PGA", "Right Capture Volume"},
604 {"Capture Right Mux", "Line or RXP-RXN", "Line Right Mux"},
605 {"Capture Right Mux", "Sidetone", "Capture ST Mixer"},
607 /* Mono Capture mixer-mux */
608 {"Capture Right Mixer", "Stereo", "Capture Right Mux"},
609 {"Capture Left Mixer", "Analogue Mix Left", "Capture Left Mux"},
610 {"Capture Left Mixer", "Analogue Mix Left", "Capture Right Mux"},
611 {"Capture Right Mixer", "Analogue Mix Right", "Capture Left Mux"},
612 {"Capture Right Mixer", "Analogue Mix Right", "Capture Right Mux"},
613 {"Capture Left Mixer", "Digital Mono Mix", "Capture Left Mux"},
614 {"Capture Left Mixer", "Digital Mono Mix", "Capture Right Mux"},
615 {"Capture Right Mixer", "Digital Mono Mix", "Capture Left Mux"},
616 {"Capture Right Mixer", "Digital Mono Mix", "Capture Right Mux"},
619 {"Left ADC", NULL, "Capture Left Mixer"},
620 {"Right ADC", NULL, "Capture Right Mixer"},
622 /* Left Capture Volume */
623 {"Left Capture Volume", NULL, "ACIN"},
625 /* Right Capture Volume */
626 {"Right Capture Volume", NULL, "Mic 2 Volume"},
629 {"ALC Mixer", "Line Capture Switch", "Line Mixer"},
630 {"ALC Mixer", "Mic2 Capture Switch", "Mic 2 Volume"},
631 {"ALC Mixer", "Mic1 Capture Switch", "Mic 1 Volume"},
632 {"ALC Mixer", "Rx Capture Switch", "Rx Mixer"},
635 {"Line Left Mux", "Line 1", "LINE1"},
636 {"Line Left Mux", "Rx Mix", "Rx Mixer"},
639 {"Line Right Mux", "Line 2", "LINE2"},
640 {"Line Right Mux", "Rx Mix", "Rx Mixer"},
643 {"Line Mono Mux", "Line Mix", "Line Mixer"},
644 {"Line Mono Mux", "Rx Mix", "Rx Mixer"},
647 {"Line Mixer", "Line 1 + 2", "LINE1"},
648 {"Line Mixer", "Line 1 - 2", "LINE1"},
649 {"Line Mixer", "Line 1 + 2", "LINE2"},
650 {"Line Mixer", "Line 1 - 2", "LINE2"},
651 {"Line Mixer", "Line 1", "LINE1"},
652 {"Line Mixer", "Line 2", "LINE2"},
655 {"Rx Mixer", "RXP - RXN", "RXP"},
656 {"Rx Mixer", "RXP + RXN", "RXP"},
657 {"Rx Mixer", "RXP - RXN", "RXN"},
658 {"Rx Mixer", "RXP + RXN", "RXN"},
659 {"Rx Mixer", "RXP", "RXP"},
660 {"Rx Mixer", "RXN", "RXN"},
663 {"Mic 1 Volume", NULL, "MIC1N"},
664 {"Mic 1 Volume", NULL, "Mic Selection Mux"},
667 {"Mic 2 Volume", NULL, "MIC2N"},
668 {"Mic 2 Volume", NULL, "MIC2"},
670 /* Mic Selector Mux */
671 {"Mic Selection Mux", "Mic 1", "MIC1"},
672 {"Mic Selection Mux", "Mic 2", "MIC2N"},
673 {"Mic Selection Mux", "Mic 3", "MIC2"},
676 {"ACOP", NULL, "ALC Mixer"},
682 static int wm8753_add_widgets(struct snd_soc_codec *codec)
686 for (i = 0; i < ARRAY_SIZE(wm8753_dapm_widgets); i++)
687 snd_soc_dapm_new_control(codec, &wm8753_dapm_widgets[i]);
689 /* set up the WM8753 audio map */
690 for (i = 0; audio_map[i][0] != NULL; i++) {
691 snd_soc_dapm_connect_input(codec, audio_map[i][0],
692 audio_map[i][1], audio_map[i][2]);
695 snd_soc_dapm_new_widgets(codec);
706 /* The size in bits of the pll divide multiplied by 10
707 * to allow rounding later */
708 #define FIXED_PLL_SIZE ((1 << 22) * 10)
710 static void pll_factors(struct _pll_div *pll_div, unsigned int target,
714 unsigned int K, Ndiv, Nmod;
716 Ndiv = target / source;
720 Ndiv = target / source;
724 if ((Ndiv < 6) || (Ndiv > 12))
726 "wm8753: unsupported N = %d\n", Ndiv);
729 Nmod = target % source;
730 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
732 do_div(Kpart, source);
734 K = Kpart & 0xFFFFFFFF;
736 /* Check if we need to round */
740 /* Move down to proper range now rounding is done */
746 static int wm8753_set_dai_pll(struct snd_soc_codec_dai *codec_dai,
747 int pll_id, unsigned int freq_in, unsigned int freq_out)
751 struct snd_soc_codec *codec = codec_dai->codec;
753 if (pll_id < WM8753_PLL1 || pll_id > WM8753_PLL2)
756 if (pll_id == WM8753_PLL1) {
759 reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xffef;
763 reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfff7;
766 if (!freq_in || !freq_out) {
768 wm8753_write(codec, WM8753_PLL1CTL1 + offset, 0x0026);
769 wm8753_write(codec, WM8753_CLOCK, reg);
773 struct _pll_div pll_div;
775 pll_factors(&pll_div, freq_out * 8, freq_in);
777 /* set up N and K PLL divisor ratios */
778 /* bits 8:5 = PLL_N, bits 3:0 = PLL_K[21:18] */
779 value = (pll_div.n << 5) + ((pll_div.k & 0x3c0000) >> 18);
780 wm8753_write(codec, WM8753_PLL1CTL2 + offset, value);
782 /* bits 8:0 = PLL_K[17:9] */
783 value = (pll_div.k & 0x03fe00) >> 9;
784 wm8753_write(codec, WM8753_PLL1CTL3 + offset, value);
786 /* bits 8:0 = PLL_K[8:0] */
787 value = pll_div.k & 0x0001ff;
788 wm8753_write(codec, WM8753_PLL1CTL4 + offset, value);
790 /* set PLL as input and enable */
791 wm8753_write(codec, WM8753_PLL1CTL1 + offset, 0x0027 |
792 (pll_div.div2 << 3));
793 wm8753_write(codec, WM8753_CLOCK, reg | enable);
805 /* codec hifi mclk (after PLL) clock divider coefficients */
806 static const struct _coeff_div coeff_div[] = {
808 {12288000, 8000, 0x6, 0x0},
809 {11289600, 8000, 0x16, 0x0},
810 {18432000, 8000, 0x7, 0x0},
811 {16934400, 8000, 0x17, 0x0},
812 {12000000, 8000, 0x6, 0x1},
815 {11289600, 11025, 0x18, 0x0},
816 {16934400, 11025, 0x19, 0x0},
817 {12000000, 11025, 0x19, 0x1},
820 {12288000, 16000, 0xa, 0x0},
821 {18432000, 16000, 0xb, 0x0},
822 {12000000, 16000, 0xa, 0x1},
825 {11289600, 22050, 0x1a, 0x0},
826 {16934400, 22050, 0x1b, 0x0},
827 {12000000, 22050, 0x1b, 0x1},
830 {12288000, 32000, 0xc, 0x0},
831 {18432000, 32000, 0xd, 0x0},
832 {12000000, 32000, 0xa, 0x1},
835 {11289600, 44100, 0x10, 0x0},
836 {16934400, 44100, 0x11, 0x0},
837 {12000000, 44100, 0x11, 0x1},
840 {12288000, 48000, 0x0, 0x0},
841 {18432000, 48000, 0x1, 0x0},
842 {12000000, 48000, 0x0, 0x1},
845 {11289600, 88200, 0x1e, 0x0},
846 {16934400, 88200, 0x1f, 0x0},
847 {12000000, 88200, 0x1f, 0x1},
850 {12288000, 96000, 0xe, 0x0},
851 {18432000, 96000, 0xf, 0x0},
852 {12000000, 96000, 0xe, 0x1},
855 static int get_coeff(int mclk, int rate)
859 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
860 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
867 * Clock after PLL and dividers
869 static int wm8753_set_dai_sysclk(struct snd_soc_codec_dai *codec_dai,
870 int clk_id, unsigned int freq, int dir)
872 struct snd_soc_codec *codec = codec_dai->codec;
873 struct wm8753_priv *wm8753 = codec->private_data;
881 if (clk_id == WM8753_MCLK) {
882 wm8753->sysclk = freq;
884 } else if (clk_id == WM8753_PCMCLK) {
885 wm8753->pcmclk = freq;
894 * Set's ADC and Voice DAC format.
896 static int wm8753_vdac_adc_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
899 struct snd_soc_codec *codec = codec_dai->codec;
900 u16 voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x01ec;
902 /* interface format */
903 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
904 case SND_SOC_DAIFMT_I2S:
907 case SND_SOC_DAIFMT_RIGHT_J:
909 case SND_SOC_DAIFMT_LEFT_J:
912 case SND_SOC_DAIFMT_DSP_A:
915 case SND_SOC_DAIFMT_DSP_B:
922 wm8753_write(codec, WM8753_PCM, voice);
927 * Set PCM DAI bit size and sample rate.
929 static int wm8753_pcm_hw_params(struct snd_pcm_substream *substream,
930 struct snd_pcm_hw_params *params)
932 struct snd_soc_pcm_runtime *rtd = substream->private_data;
933 struct snd_soc_device *socdev = rtd->socdev;
934 struct snd_soc_codec *codec = socdev->codec;
935 struct wm8753_priv *wm8753 = codec->private_data;
936 u16 voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x01f3;
937 u16 srate = wm8753_read_reg_cache(codec, WM8753_SRATE1) & 0x017f;
940 switch (params_format(params)) {
941 case SNDRV_PCM_FORMAT_S16_LE:
943 case SNDRV_PCM_FORMAT_S20_3LE:
946 case SNDRV_PCM_FORMAT_S24_LE:
949 case SNDRV_PCM_FORMAT_S32_LE:
955 if (params_rate(params) * 384 == wm8753->pcmclk)
957 wm8753_write(codec, WM8753_SRATE1, srate);
959 wm8753_write(codec, WM8753_PCM, voice);
964 * Set's PCM dai fmt and BCLK.
966 static int wm8753_pcm_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
969 struct snd_soc_codec *codec = codec_dai->codec;
972 voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x011f;
973 ioctl = wm8753_read_reg_cache(codec, WM8753_IOCTL) & 0x015d;
975 /* set master/slave audio interface */
976 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
977 case SND_SOC_DAIFMT_CBS_CFS:
979 case SND_SOC_DAIFMT_CBM_CFM:
981 case SND_SOC_DAIFMT_CBM_CFS:
988 /* clock inversion */
989 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
990 case SND_SOC_DAIFMT_DSP_A:
991 case SND_SOC_DAIFMT_DSP_B:
992 /* frame inversion not valid for DSP modes */
993 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
994 case SND_SOC_DAIFMT_NB_NF:
996 case SND_SOC_DAIFMT_IB_NF:
1003 case SND_SOC_DAIFMT_I2S:
1004 case SND_SOC_DAIFMT_RIGHT_J:
1005 case SND_SOC_DAIFMT_LEFT_J:
1007 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1008 case SND_SOC_DAIFMT_NB_NF:
1010 case SND_SOC_DAIFMT_IB_IF:
1013 case SND_SOC_DAIFMT_IB_NF:
1016 case SND_SOC_DAIFMT_NB_IF:
1027 wm8753_write(codec, WM8753_PCM, voice);
1028 wm8753_write(codec, WM8753_IOCTL, ioctl);
1032 static int wm8753_set_dai_clkdiv(struct snd_soc_codec_dai *codec_dai,
1033 int div_id, int div)
1035 struct snd_soc_codec *codec = codec_dai->codec;
1040 reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0x003f;
1041 wm8753_write(codec, WM8753_CLOCK, reg | div);
1043 case WM8753_BCLKDIV:
1044 reg = wm8753_read_reg_cache(codec, WM8753_SRATE2) & 0x01c7;
1045 wm8753_write(codec, WM8753_SRATE2, reg | div);
1047 case WM8753_VXCLKDIV:
1048 reg = wm8753_read_reg_cache(codec, WM8753_SRATE2) & 0x003f;
1049 wm8753_write(codec, WM8753_SRATE2, reg | div);
1058 * Set's HiFi DAC format.
1060 static int wm8753_hdac_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
1063 struct snd_soc_codec *codec = codec_dai->codec;
1064 u16 hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x01e0;
1066 /* interface format */
1067 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1068 case SND_SOC_DAIFMT_I2S:
1071 case SND_SOC_DAIFMT_RIGHT_J:
1073 case SND_SOC_DAIFMT_LEFT_J:
1076 case SND_SOC_DAIFMT_DSP_A:
1079 case SND_SOC_DAIFMT_DSP_B:
1086 wm8753_write(codec, WM8753_HIFI, hifi);
1091 * Set's I2S DAI format.
1093 static int wm8753_i2s_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
1096 struct snd_soc_codec *codec = codec_dai->codec;
1099 hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x011f;
1100 ioctl = wm8753_read_reg_cache(codec, WM8753_IOCTL) & 0x00ae;
1102 /* set master/slave audio interface */
1103 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1104 case SND_SOC_DAIFMT_CBS_CFS:
1106 case SND_SOC_DAIFMT_CBM_CFM:
1108 case SND_SOC_DAIFMT_CBM_CFS:
1115 /* clock inversion */
1116 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1117 case SND_SOC_DAIFMT_DSP_A:
1118 case SND_SOC_DAIFMT_DSP_B:
1119 /* frame inversion not valid for DSP modes */
1120 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1121 case SND_SOC_DAIFMT_NB_NF:
1123 case SND_SOC_DAIFMT_IB_NF:
1130 case SND_SOC_DAIFMT_I2S:
1131 case SND_SOC_DAIFMT_RIGHT_J:
1132 case SND_SOC_DAIFMT_LEFT_J:
1134 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1135 case SND_SOC_DAIFMT_NB_NF:
1137 case SND_SOC_DAIFMT_IB_IF:
1140 case SND_SOC_DAIFMT_IB_NF:
1143 case SND_SOC_DAIFMT_NB_IF:
1154 wm8753_write(codec, WM8753_HIFI, hifi);
1155 wm8753_write(codec, WM8753_IOCTL, ioctl);
1160 * Set PCM DAI bit size and sample rate.
1162 static int wm8753_i2s_hw_params(struct snd_pcm_substream *substream,
1163 struct snd_pcm_hw_params *params)
1165 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1166 struct snd_soc_device *socdev = rtd->socdev;
1167 struct snd_soc_codec *codec = socdev->codec;
1168 struct wm8753_priv *wm8753 = codec->private_data;
1169 u16 srate = wm8753_read_reg_cache(codec, WM8753_SRATE1) & 0x01c0;
1170 u16 hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x01f3;
1173 /* is digital filter coefficient valid ? */
1174 coeff = get_coeff(wm8753->sysclk, params_rate(params));
1176 printk(KERN_ERR "wm8753 invalid MCLK or rate\n");
1179 wm8753_write(codec, WM8753_SRATE1, srate | (coeff_div[coeff].sr << 1) |
1180 coeff_div[coeff].usb);
1183 switch (params_format(params)) {
1184 case SNDRV_PCM_FORMAT_S16_LE:
1186 case SNDRV_PCM_FORMAT_S20_3LE:
1189 case SNDRV_PCM_FORMAT_S24_LE:
1192 case SNDRV_PCM_FORMAT_S32_LE:
1197 wm8753_write(codec, WM8753_HIFI, hifi);
1201 static int wm8753_mode1v_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
1204 struct snd_soc_codec *codec = codec_dai->codec;
1207 /* set clk source as pcmclk */
1208 clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb;
1209 wm8753_write(codec, WM8753_CLOCK, clock);
1211 if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0)
1213 return wm8753_pcm_set_dai_fmt(codec_dai, fmt);
1216 static int wm8753_mode1h_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
1219 if (wm8753_hdac_set_dai_fmt(codec_dai, fmt) < 0)
1221 return wm8753_i2s_set_dai_fmt(codec_dai, fmt);
1224 static int wm8753_mode2_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
1227 struct snd_soc_codec *codec = codec_dai->codec;
1230 /* set clk source as pcmclk */
1231 clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb;
1232 wm8753_write(codec, WM8753_CLOCK, clock);
1234 if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0)
1236 return wm8753_i2s_set_dai_fmt(codec_dai, fmt);
1239 static int wm8753_mode3_4_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
1242 struct snd_soc_codec *codec = codec_dai->codec;
1245 /* set clk source as mclk */
1246 clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb;
1247 wm8753_write(codec, WM8753_CLOCK, clock | 0x4);
1249 if (wm8753_hdac_set_dai_fmt(codec_dai, fmt) < 0)
1251 if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0)
1253 return wm8753_i2s_set_dai_fmt(codec_dai, fmt);
1256 static int wm8753_mute(struct snd_soc_codec_dai *dai, int mute)
1258 struct snd_soc_codec *codec = dai->codec;
1259 u16 mute_reg = wm8753_read_reg_cache(codec, WM8753_DAC) & 0xfff7;
1261 /* the digital mute covers the HiFi and Voice DAC's on the WM8753.
1262 * make sure we check if they are not both active when we mute */
1263 if (mute && dai->id == 1) {
1264 if (!wm8753_dai[WM8753_DAI_VOICE].playback.active ||
1265 !wm8753_dai[WM8753_DAI_HIFI].playback.active)
1266 wm8753_write(codec, WM8753_DAC, mute_reg | 0x8);
1269 wm8753_write(codec, WM8753_DAC, mute_reg | 0x8);
1271 wm8753_write(codec, WM8753_DAC, mute_reg);
1277 static int wm8753_dapm_event(struct snd_soc_codec *codec, int event)
1279 u16 pwr_reg = wm8753_read_reg_cache(codec, WM8753_PWR1) & 0xfe3e;
1282 case SNDRV_CTL_POWER_D0: /* full On */
1283 /* set vmid to 50k and unmute dac */
1284 wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x00c0);
1286 case SNDRV_CTL_POWER_D1: /* partial On */
1287 case SNDRV_CTL_POWER_D2: /* partial On */
1288 /* set vmid to 5k for quick power up */
1289 wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x01c1);
1291 case SNDRV_CTL_POWER_D3hot: /* Off, with power */
1292 /* mute dac and set vmid to 500k, enable VREF */
1293 wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x0141);
1295 case SNDRV_CTL_POWER_D3cold: /* Off, without power */
1296 wm8753_write(codec, WM8753_PWR1, 0x0001);
1299 codec->dapm_state = event;
1303 #define WM8753_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
1304 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
1305 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
1306 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
1308 #define WM8753_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1309 SNDRV_PCM_FMTBIT_S24_LE)
1312 * The WM8753 supports upto 4 different and mutually exclusive DAI
1313 * configurations. This gives 2 PCM's available for use, hifi and voice.
1314 * NOTE: The Voice PCM cannot play or capture audio to the CPU as it's DAI
1315 * is connected between the wm8753 and a BT codec or GSM modem.
1317 * 1. Voice over PCM DAI - HIFI DAC over HIFI DAI
1318 * 2. Voice over HIFI DAI - HIFI disabled
1319 * 3. Voice disabled - HIFI over HIFI
1320 * 4. Voice disabled - HIFI over HIFI, uses voice DAI LRC for capture
1322 static const struct snd_soc_codec_dai wm8753_all_dai[] = {
1323 /* DAI HiFi mode 1 */
1324 { .name = "WM8753 HiFi",
1327 .stream_name = "HiFi Playback",
1330 .rates = WM8753_RATES,
1331 .formats = WM8753_FORMATS,},
1332 .capture = { /* dummy for fast DAI switching */
1333 .stream_name = "Capture",
1336 .rates = WM8753_RATES,
1337 .formats = WM8753_FORMATS,},
1339 .hw_params = wm8753_i2s_hw_params,},
1341 .digital_mute = wm8753_mute,
1342 .set_fmt = wm8753_mode1h_set_dai_fmt,
1343 .set_clkdiv = wm8753_set_dai_clkdiv,
1344 .set_pll = wm8753_set_dai_pll,
1345 .set_sysclk = wm8753_set_dai_sysclk,
1348 /* DAI Voice mode 1 */
1349 { .name = "WM8753 Voice",
1352 .stream_name = "Voice Playback",
1355 .rates = WM8753_RATES,
1356 .formats = WM8753_FORMATS,},
1358 .stream_name = "Capture",
1361 .rates = WM8753_RATES,
1362 .formats = WM8753_FORMATS,},
1364 .hw_params = wm8753_pcm_hw_params,},
1366 .digital_mute = wm8753_mute,
1367 .set_fmt = wm8753_mode1v_set_dai_fmt,
1368 .set_clkdiv = wm8753_set_dai_clkdiv,
1369 .set_pll = wm8753_set_dai_pll,
1370 .set_sysclk = wm8753_set_dai_sysclk,
1373 /* DAI HiFi mode 2 - dummy */
1374 { .name = "WM8753 HiFi",
1377 /* DAI Voice mode 2 */
1378 { .name = "WM8753 Voice",
1381 .stream_name = "Voice Playback",
1384 .rates = WM8753_RATES,
1385 .formats = WM8753_FORMATS,},
1387 .stream_name = "Capture",
1390 .rates = WM8753_RATES,
1391 .formats = WM8753_FORMATS,},
1393 .hw_params = wm8753_pcm_hw_params,},
1395 .digital_mute = wm8753_mute,
1396 .set_fmt = wm8753_mode2_set_dai_fmt,
1397 .set_clkdiv = wm8753_set_dai_clkdiv,
1398 .set_pll = wm8753_set_dai_pll,
1399 .set_sysclk = wm8753_set_dai_sysclk,
1402 /* DAI HiFi mode 3 */
1403 { .name = "WM8753 HiFi",
1406 .stream_name = "HiFi Playback",
1409 .rates = WM8753_RATES,
1410 .formats = WM8753_FORMATS,},
1412 .stream_name = "Capture",
1415 .rates = WM8753_RATES,
1416 .formats = WM8753_FORMATS,},
1418 .hw_params = wm8753_i2s_hw_params,},
1420 .digital_mute = wm8753_mute,
1421 .set_fmt = wm8753_mode3_4_set_dai_fmt,
1422 .set_clkdiv = wm8753_set_dai_clkdiv,
1423 .set_pll = wm8753_set_dai_pll,
1424 .set_sysclk = wm8753_set_dai_sysclk,
1427 /* DAI Voice mode 3 - dummy */
1428 { .name = "WM8753 Voice",
1431 /* DAI HiFi mode 4 */
1432 { .name = "WM8753 HiFi",
1435 .stream_name = "HiFi Playback",
1438 .rates = WM8753_RATES,
1439 .formats = WM8753_FORMATS,},
1441 .stream_name = "Capture",
1444 .rates = WM8753_RATES,
1445 .formats = WM8753_FORMATS,},
1447 .hw_params = wm8753_i2s_hw_params,},
1449 .digital_mute = wm8753_mute,
1450 .set_fmt = wm8753_mode3_4_set_dai_fmt,
1451 .set_clkdiv = wm8753_set_dai_clkdiv,
1452 .set_pll = wm8753_set_dai_pll,
1453 .set_sysclk = wm8753_set_dai_sysclk,
1456 /* DAI Voice mode 4 - dummy */
1457 { .name = "WM8753 Voice",
1462 struct snd_soc_codec_dai wm8753_dai[2];
1463 EXPORT_SYMBOL_GPL(wm8753_dai);
1465 static void wm8753_set_dai_mode(struct snd_soc_codec *codec, unsigned int mode)
1468 int playback_active, capture_active, codec_active, pop_wait;
1471 playback_active = wm8753_dai[0].playback.active;
1472 capture_active = wm8753_dai[0].capture.active;
1473 codec_active = wm8753_dai[0].active;
1474 private_data = wm8753_dai[0].private_data;
1475 pop_wait = wm8753_dai[0].pop_wait;
1476 wm8753_dai[0] = wm8753_all_dai[mode << 1];
1477 wm8753_dai[0].playback.active = playback_active;
1478 wm8753_dai[0].capture.active = capture_active;
1479 wm8753_dai[0].active = codec_active;
1480 wm8753_dai[0].private_data = private_data;
1481 wm8753_dai[0].pop_wait = pop_wait;
1483 playback_active = wm8753_dai[1].playback.active;
1484 capture_active = wm8753_dai[1].capture.active;
1485 codec_active = wm8753_dai[1].active;
1486 private_data = wm8753_dai[1].private_data;
1487 pop_wait = wm8753_dai[1].pop_wait;
1488 wm8753_dai[1] = wm8753_all_dai[(mode << 1) + 1];
1489 wm8753_dai[1].playback.active = playback_active;
1490 wm8753_dai[1].capture.active = capture_active;
1491 wm8753_dai[1].active = codec_active;
1492 wm8753_dai[1].private_data = private_data;
1493 wm8753_dai[1].pop_wait = pop_wait;
1495 wm8753_dai[0].codec = codec;
1496 wm8753_dai[1].codec = codec;
1499 static void wm8753_work(struct work_struct *work)
1501 struct snd_soc_codec *codec =
1502 container_of(work, struct snd_soc_codec, delayed_work.work);
1503 wm8753_dapm_event(codec, codec->dapm_state);
1506 static int wm8753_suspend(struct platform_device *pdev, pm_message_t state)
1508 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1509 struct snd_soc_codec *codec = socdev->codec;
1511 /* we only need to suspend if we are a valid card */
1515 wm8753_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
1519 static int wm8753_resume(struct platform_device *pdev)
1521 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1522 struct snd_soc_codec *codec = socdev->codec;
1525 u16 *cache = codec->reg_cache;
1527 /* we only need to resume if we are a valid card */
1531 /* Sync reg_cache with the hardware */
1532 for (i = 0; i < ARRAY_SIZE(wm8753_reg); i++) {
1533 if (i + 1 == WM8753_RESET)
1535 data[0] = ((i + 1) << 1) | ((cache[i] >> 8) & 0x0001);
1536 data[1] = cache[i] & 0x00ff;
1537 codec->hw_write(codec->control_data, data, 2);
1540 wm8753_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
1542 /* charge wm8753 caps */
1543 if (codec->suspend_dapm_state == SNDRV_CTL_POWER_D0) {
1544 wm8753_dapm_event(codec, SNDRV_CTL_POWER_D2);
1545 codec->dapm_state = SNDRV_CTL_POWER_D0;
1546 schedule_delayed_work(&codec->delayed_work,
1547 msecs_to_jiffies(caps_charge));
1554 * initialise the WM8753 driver
1555 * register the mixer and dsp interfaces with the kernel
1557 static int wm8753_init(struct snd_soc_device *socdev)
1559 struct snd_soc_codec *codec = socdev->codec;
1562 codec->name = "WM8753";
1563 codec->owner = THIS_MODULE;
1564 codec->read = wm8753_read_reg_cache;
1565 codec->write = wm8753_write;
1566 codec->dapm_event = wm8753_dapm_event;
1567 codec->dai = wm8753_dai;
1569 codec->reg_cache_size = sizeof(wm8753_reg);
1570 codec->reg_cache = kmemdup(wm8753_reg, sizeof(wm8753_reg), GFP_KERNEL);
1572 if (codec->reg_cache == NULL)
1575 wm8753_set_dai_mode(codec, 0);
1577 wm8753_reset(codec);
1580 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
1582 printk(KERN_ERR "wm8753: failed to create pcms\n");
1586 /* charge output caps */
1587 wm8753_dapm_event(codec, SNDRV_CTL_POWER_D2);
1588 codec->dapm_state = SNDRV_CTL_POWER_D3hot;
1589 schedule_delayed_work(&codec->delayed_work,
1590 msecs_to_jiffies(caps_charge));
1592 /* set the update bits */
1593 reg = wm8753_read_reg_cache(codec, WM8753_LDAC);
1594 wm8753_write(codec, WM8753_LDAC, reg | 0x0100);
1595 reg = wm8753_read_reg_cache(codec, WM8753_RDAC);
1596 wm8753_write(codec, WM8753_RDAC, reg | 0x0100);
1597 reg = wm8753_read_reg_cache(codec, WM8753_LADC);
1598 wm8753_write(codec, WM8753_LADC, reg | 0x0100);
1599 reg = wm8753_read_reg_cache(codec, WM8753_RADC);
1600 wm8753_write(codec, WM8753_RADC, reg | 0x0100);
1601 reg = wm8753_read_reg_cache(codec, WM8753_LOUT1V);
1602 wm8753_write(codec, WM8753_LOUT1V, reg | 0x0100);
1603 reg = wm8753_read_reg_cache(codec, WM8753_ROUT1V);
1604 wm8753_write(codec, WM8753_ROUT1V, reg | 0x0100);
1605 reg = wm8753_read_reg_cache(codec, WM8753_LOUT2V);
1606 wm8753_write(codec, WM8753_LOUT2V, reg | 0x0100);
1607 reg = wm8753_read_reg_cache(codec, WM8753_ROUT2V);
1608 wm8753_write(codec, WM8753_ROUT2V, reg | 0x0100);
1609 reg = wm8753_read_reg_cache(codec, WM8753_LINVOL);
1610 wm8753_write(codec, WM8753_LINVOL, reg | 0x0100);
1611 reg = wm8753_read_reg_cache(codec, WM8753_RINVOL);
1612 wm8753_write(codec, WM8753_RINVOL, reg | 0x0100);
1614 wm8753_add_controls(codec);
1615 wm8753_add_widgets(codec);
1616 ret = snd_soc_register_card(socdev);
1618 printk(KERN_ERR "wm8753: failed to register card\n");
1625 snd_soc_free_pcms(socdev);
1626 snd_soc_dapm_free(socdev);
1628 kfree(codec->reg_cache);
1632 /* If the i2c layer weren't so broken, we could pass this kind of data
1634 static struct snd_soc_device *wm8753_socdev;
1636 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1639 * WM8753 2 wire address is determined by GPIO5
1640 * state during powerup.
1644 static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END };
1646 /* Magic definition of all other variables and things */
1649 static struct i2c_driver wm8753_i2c_driver;
1650 static struct i2c_client client_template;
1652 static int wm8753_codec_probe(struct i2c_adapter *adap, int addr, int kind)
1654 struct snd_soc_device *socdev = wm8753_socdev;
1655 struct wm8753_setup_data *setup = socdev->codec_data;
1656 struct snd_soc_codec *codec = socdev->codec;
1657 struct i2c_client *i2c;
1660 if (addr != setup->i2c_address)
1663 client_template.adapter = adap;
1664 client_template.addr = addr;
1666 i2c = kmemdup(&client_template, sizeof(client_template), GFP_KERNEL);
1671 i2c_set_clientdata(i2c, codec);
1672 codec->control_data = i2c;
1674 ret = i2c_attach_client(i2c);
1676 err("failed to attach codec at addr %x\n", addr);
1680 ret = wm8753_init(socdev);
1682 err("failed to initialise WM8753\n");
1694 static int wm8753_i2c_detach(struct i2c_client *client)
1696 struct snd_soc_codec *codec = i2c_get_clientdata(client);
1697 i2c_detach_client(client);
1698 kfree(codec->reg_cache);
1703 static int wm8753_i2c_attach(struct i2c_adapter *adap)
1705 return i2c_probe(adap, &addr_data, wm8753_codec_probe);
1708 /* corgi i2c codec control layer */
1709 static struct i2c_driver wm8753_i2c_driver = {
1711 .name = "WM8753 I2C Codec",
1712 .owner = THIS_MODULE,
1714 .id = I2C_DRIVERID_WM8753,
1715 .attach_adapter = wm8753_i2c_attach,
1716 .detach_client = wm8753_i2c_detach,
1720 static struct i2c_client client_template = {
1722 .driver = &wm8753_i2c_driver,
1726 static int wm8753_probe(struct platform_device *pdev)
1728 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1729 struct wm8753_setup_data *setup;
1730 struct snd_soc_codec *codec;
1731 struct wm8753_priv *wm8753;
1734 info("WM8753 Audio Codec %s", WM8753_VERSION);
1736 setup = socdev->codec_data;
1737 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
1741 wm8753 = kzalloc(sizeof(struct wm8753_priv), GFP_KERNEL);
1742 if (wm8753 == NULL) {
1747 codec->private_data = wm8753;
1748 socdev->codec = codec;
1749 mutex_init(&codec->mutex);
1750 INIT_LIST_HEAD(&codec->dapm_widgets);
1751 INIT_LIST_HEAD(&codec->dapm_paths);
1752 wm8753_socdev = socdev;
1753 INIT_DELAYED_WORK(&codec->delayed_work, wm8753_work);
1755 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1756 if (setup->i2c_address) {
1757 normal_i2c[0] = setup->i2c_address;
1758 codec->hw_write = (hw_write_t)i2c_master_send;
1759 ret = i2c_add_driver(&wm8753_i2c_driver);
1761 printk(KERN_ERR "can't add i2c driver");
1764 /* Add other interfaces here */
1770 * This function forces any delayed work to be queued and run.
1772 static int run_delayed_work(struct delayed_work *dwork)
1776 /* cancel any work waiting to be queued. */
1777 ret = cancel_delayed_work(dwork);
1779 /* if there was any work waiting then we run it now and
1780 * wait for it's completion */
1782 schedule_delayed_work(dwork, 0);
1783 flush_scheduled_work();
1788 /* power down chip */
1789 static int wm8753_remove(struct platform_device *pdev)
1791 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1792 struct snd_soc_codec *codec = socdev->codec;
1794 if (codec->control_data)
1795 wm8753_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
1796 run_delayed_work(&codec->delayed_work);
1797 snd_soc_free_pcms(socdev);
1798 snd_soc_dapm_free(socdev);
1799 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1800 i2c_del_driver(&wm8753_i2c_driver);
1802 kfree(codec->private_data);
1808 struct snd_soc_codec_device soc_codec_dev_wm8753 = {
1809 .probe = wm8753_probe,
1810 .remove = wm8753_remove,
1811 .suspend = wm8753_suspend,
1812 .resume = wm8753_resume,
1814 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8753);
1816 MODULE_DESCRIPTION("ASoC WM8753 driver");
1817 MODULE_AUTHOR("Liam Girdwood");
1818 MODULE_LICENSE("GPL");