2 * wm8753.c -- WM8753 ALSA Soc Audio driver
4 * Copyright 2003-11 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/of_device.h>
42 #include <linux/regmap.h>
43 #include <linux/spi/spi.h>
44 #include <linux/slab.h>
45 #include <sound/core.h>
46 #include <sound/pcm.h>
47 #include <sound/pcm_params.h>
48 #include <sound/soc.h>
49 #include <sound/initval.h>
50 #include <sound/tlv.h>
51 #include <asm/div64.h>
55 static int caps_charge
= 2000;
56 module_param(caps_charge
, int, 0);
57 MODULE_PARM_DESC(caps_charge
, "WM8753 cap charge time (msecs)");
59 static int wm8753_hifi_write_dai_fmt(struct snd_soc_codec
*codec
,
61 static int wm8753_voice_write_dai_fmt(struct snd_soc_codec
*codec
,
65 * wm8753 register cache
66 * We can't read the WM8753 register space when we
67 * are using 2 wire for device control, so we cache them instead.
69 static const struct reg_default wm8753_reg_defaults
[] = {
136 static bool wm8753_volatile(struct device
*dev
, unsigned int reg
)
138 return reg
== WM8753_RESET
;
141 static bool wm8753_writeable(struct device
*dev
, unsigned int reg
)
143 return reg
<= WM8753_ADCTL2
;
146 /* codec private data */
148 struct regmap
*regmap
;
152 unsigned int voice_fmt
;
153 unsigned int hifi_fmt
;
156 struct delayed_work charge_work
;
159 #define wm8753_reset(c) snd_soc_write(c, WM8753_RESET, 0)
164 static const char *wm8753_base
[] = {"Linear Control", "Adaptive Boost"};
165 static const char *wm8753_base_filter
[] =
166 {"130Hz @ 48kHz", "200Hz @ 48kHz", "100Hz @ 16kHz", "400Hz @ 48kHz",
167 "100Hz @ 8kHz", "200Hz @ 8kHz"};
168 static const char *wm8753_treble
[] = {"8kHz", "4kHz"};
169 static const char *wm8753_alc_func
[] = {"Off", "Right", "Left", "Stereo"};
170 static const char *wm8753_ng_type
[] = {"Constant PGA Gain", "Mute ADC Output"};
171 static const char *wm8753_3d_func
[] = {"Capture", "Playback"};
172 static const char *wm8753_3d_uc
[] = {"2.2kHz", "1.5kHz"};
173 static const char *wm8753_3d_lc
[] = {"200Hz", "500Hz"};
174 static const char *wm8753_deemp
[] = {"None", "32kHz", "44.1kHz", "48kHz"};
175 static const char *wm8753_mono_mix
[] = {"Stereo", "Left", "Right", "Mono"};
176 static const char *wm8753_dac_phase
[] = {"Non Inverted", "Inverted"};
177 static const char *wm8753_line_mix
[] = {"Line 1 + 2", "Line 1 - 2",
179 static const char *wm8753_mono_mux
[] = {"Line Mix", "Rx Mix"};
180 static const char *wm8753_right_mux
[] = {"Line 2", "Rx Mix"};
181 static const char *wm8753_left_mux
[] = {"Line 1", "Rx Mix"};
182 static const char *wm8753_rxmsel
[] = {"RXP - RXN", "RXP + RXN", "RXP", "RXN"};
183 static const char *wm8753_sidetone_mux
[] = {"Left PGA", "Mic 1", "Mic 2",
185 static const char *wm8753_mono2_src
[] = {"Inverted Mono 1", "Left", "Right",
187 static const char *wm8753_out3
[] = {"VREF", "ROUT2", "Left + Right"};
188 static const char *wm8753_out4
[] = {"VREF", "Capture ST", "LOUT2"};
189 static const char *wm8753_radcsel
[] = {"PGA", "Line or RXP-RXN", "Sidetone"};
190 static const char *wm8753_ladcsel
[] = {"PGA", "Line or RXP-RXN", "Line"};
191 static const char *wm8753_mono_adc
[] = {"Stereo", "Analogue Mix Left",
192 "Analogue Mix Right", "Digital Mono Mix"};
193 static const char *wm8753_adc_hp
[] = {"3.4Hz @ 48kHz", "82Hz @ 16k",
194 "82Hz @ 8kHz", "170Hz @ 8kHz"};
195 static const char *wm8753_adc_filter
[] = {"HiFi", "Voice"};
196 static const char *wm8753_mic_sel
[] = {"Mic 1", "Mic 2", "Mic 3"};
197 static const char *wm8753_dai_mode
[] = {"DAI 0", "DAI 1", "DAI 2", "DAI 3"};
198 static const char *wm8753_dat_sel
[] = {"Stereo", "Left ADC", "Right ADC",
200 static const char *wm8753_rout2_phase
[] = {"Non Inverted", "Inverted"};
202 static const struct soc_enum wm8753_enum
[] = {
203 SOC_ENUM_SINGLE(WM8753_BASS
, 7, 2, wm8753_base
),
204 SOC_ENUM_SINGLE(WM8753_BASS
, 4, 6, wm8753_base_filter
),
205 SOC_ENUM_SINGLE(WM8753_TREBLE
, 6, 2, wm8753_treble
),
206 SOC_ENUM_SINGLE(WM8753_ALC1
, 7, 4, wm8753_alc_func
),
207 SOC_ENUM_SINGLE(WM8753_NGATE
, 1, 2, wm8753_ng_type
),
208 SOC_ENUM_SINGLE(WM8753_3D
, 7, 2, wm8753_3d_func
),
209 SOC_ENUM_SINGLE(WM8753_3D
, 6, 2, wm8753_3d_uc
),
210 SOC_ENUM_SINGLE(WM8753_3D
, 5, 2, wm8753_3d_lc
),
211 SOC_ENUM_SINGLE(WM8753_DAC
, 1, 4, wm8753_deemp
),
212 SOC_ENUM_SINGLE(WM8753_DAC
, 4, 4, wm8753_mono_mix
),
213 SOC_ENUM_SINGLE(WM8753_DAC
, 6, 2, wm8753_dac_phase
),
214 SOC_ENUM_SINGLE(WM8753_INCTL1
, 3, 4, wm8753_line_mix
),
215 SOC_ENUM_SINGLE(WM8753_INCTL1
, 2, 2, wm8753_mono_mux
),
216 SOC_ENUM_SINGLE(WM8753_INCTL1
, 1, 2, wm8753_right_mux
),
217 SOC_ENUM_SINGLE(WM8753_INCTL1
, 0, 2, wm8753_left_mux
),
218 SOC_ENUM_SINGLE(WM8753_INCTL2
, 6, 4, wm8753_rxmsel
),
219 SOC_ENUM_SINGLE(WM8753_INCTL2
, 4, 4, wm8753_sidetone_mux
),
220 SOC_ENUM_SINGLE(WM8753_OUTCTL
, 7, 4, wm8753_mono2_src
),
221 SOC_ENUM_SINGLE(WM8753_OUTCTL
, 0, 3, wm8753_out3
),
222 SOC_ENUM_SINGLE(WM8753_ADCTL2
, 7, 3, wm8753_out4
),
223 SOC_ENUM_SINGLE(WM8753_ADCIN
, 2, 3, wm8753_radcsel
),
224 SOC_ENUM_SINGLE(WM8753_ADCIN
, 0, 3, wm8753_ladcsel
),
225 SOC_ENUM_SINGLE(WM8753_ADCIN
, 4, 4, wm8753_mono_adc
),
226 SOC_ENUM_SINGLE(WM8753_ADC
, 2, 4, wm8753_adc_hp
),
227 SOC_ENUM_SINGLE(WM8753_ADC
, 4, 2, wm8753_adc_filter
),
228 SOC_ENUM_SINGLE(WM8753_MICBIAS
, 6, 3, wm8753_mic_sel
),
229 SOC_ENUM_SINGLE(WM8753_IOCTL
, 2, 4, wm8753_dai_mode
),
230 SOC_ENUM_SINGLE(WM8753_ADC
, 7, 4, wm8753_dat_sel
),
231 SOC_ENUM_SINGLE(WM8753_OUTCTL
, 2, 2, wm8753_rout2_phase
),
235 static int wm8753_get_dai(struct snd_kcontrol
*kcontrol
,
236 struct snd_ctl_elem_value
*ucontrol
)
238 struct snd_soc_codec
*codec
= snd_soc_kcontrol_codec(kcontrol
);
239 struct wm8753_priv
*wm8753
= snd_soc_codec_get_drvdata(codec
);
241 ucontrol
->value
.integer
.value
[0] = wm8753
->dai_func
;
245 static int wm8753_set_dai(struct snd_kcontrol
*kcontrol
,
246 struct snd_ctl_elem_value
*ucontrol
)
248 struct snd_soc_codec
*codec
= snd_soc_kcontrol_codec(kcontrol
);
249 struct wm8753_priv
*wm8753
= snd_soc_codec_get_drvdata(codec
);
252 if (wm8753
->dai_func
== ucontrol
->value
.integer
.value
[0])
255 if (snd_soc_codec_is_active(codec
))
258 ioctl
= snd_soc_read(codec
, WM8753_IOCTL
);
260 wm8753
->dai_func
= ucontrol
->value
.integer
.value
[0];
262 if (((ioctl
>> 2) & 0x3) == wm8753
->dai_func
)
265 ioctl
= (ioctl
& 0x1f3) | (wm8753
->dai_func
<< 2);
266 snd_soc_write(codec
, WM8753_IOCTL
, ioctl
);
269 wm8753_hifi_write_dai_fmt(codec
, wm8753
->hifi_fmt
);
270 wm8753_voice_write_dai_fmt(codec
, wm8753
->voice_fmt
);
275 static const DECLARE_TLV_DB_SCALE(rec_mix_tlv
, -1500, 300, 0);
276 static const DECLARE_TLV_DB_SCALE(mic_preamp_tlv
, 1200, 600, 0);
277 static const DECLARE_TLV_DB_SCALE(adc_tlv
, -9750, 50, 1);
278 static const DECLARE_TLV_DB_SCALE(dac_tlv
, -12750, 50, 1);
279 static const unsigned int out_tlv
[] = {
280 TLV_DB_RANGE_HEAD(2),
281 /* 0000000 - 0101111 = "Analogue mute" */
282 0, 48, TLV_DB_SCALE_ITEM(-25500, 0, 0),
283 48, 127, TLV_DB_SCALE_ITEM(-7300, 100, 0),
285 static const DECLARE_TLV_DB_SCALE(mix_tlv
, -1500, 300, 0);
286 static const DECLARE_TLV_DB_SCALE(voice_mix_tlv
, -1200, 300, 0);
287 static const DECLARE_TLV_DB_SCALE(pga_tlv
, -1725, 75, 0);
289 static const struct snd_kcontrol_new wm8753_snd_controls
[] = {
290 SOC_DOUBLE_R_TLV("PCM Volume", WM8753_LDAC
, WM8753_RDAC
, 0, 255, 0, dac_tlv
),
292 SOC_DOUBLE_R_TLV("ADC Capture Volume", WM8753_LADC
, WM8753_RADC
, 0, 255, 0,
295 SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8753_LOUT1V
, WM8753_ROUT1V
,
297 SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8753_LOUT2V
, WM8753_ROUT2V
, 0,
300 SOC_SINGLE_TLV("Mono Playback Volume", WM8753_MOUTV
, 0, 127, 0, out_tlv
),
302 SOC_DOUBLE_R_TLV("Bypass Playback Volume", WM8753_LOUTM1
, WM8753_ROUTM1
, 4, 7,
304 SOC_DOUBLE_R_TLV("Sidetone Playback Volume", WM8753_LOUTM2
, WM8753_ROUTM2
, 4,
306 SOC_DOUBLE_R_TLV("Voice Playback Volume", WM8753_LOUTM2
, WM8753_ROUTM2
, 0, 7,
309 SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8753_LOUT1V
, WM8753_ROUT1V
, 7,
311 SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8753_LOUT2V
, WM8753_ROUT2V
, 7,
314 SOC_SINGLE_TLV("Mono Bypass Playback Volume", WM8753_MOUTM1
, 4, 7, 1, mix_tlv
),
315 SOC_SINGLE_TLV("Mono Sidetone Playback Volume", WM8753_MOUTM2
, 4, 7, 1,
317 SOC_SINGLE_TLV("Mono Voice Playback Volume", WM8753_MOUTM2
, 0, 7, 1,
319 SOC_SINGLE("Mono Playback ZC Switch", WM8753_MOUTV
, 7, 1, 0),
321 SOC_ENUM("Bass Boost", wm8753_enum
[0]),
322 SOC_ENUM("Bass Filter", wm8753_enum
[1]),
323 SOC_SINGLE("Bass Volume", WM8753_BASS
, 0, 15, 1),
325 SOC_SINGLE("Treble Volume", WM8753_TREBLE
, 0, 15, 1),
326 SOC_ENUM("Treble Cut-off", wm8753_enum
[2]),
328 SOC_DOUBLE_TLV("Sidetone Capture Volume", WM8753_RECMIX1
, 0, 4, 7, 1,
330 SOC_SINGLE_TLV("Voice Sidetone Capture Volume", WM8753_RECMIX2
, 0, 7, 1,
333 SOC_DOUBLE_R_TLV("Capture Volume", WM8753_LINVOL
, WM8753_RINVOL
, 0, 63, 0,
335 SOC_DOUBLE_R("Capture ZC Switch", WM8753_LINVOL
, WM8753_RINVOL
, 6, 1, 0),
336 SOC_DOUBLE_R("Capture Switch", WM8753_LINVOL
, WM8753_RINVOL
, 7, 1, 1),
338 SOC_ENUM("Capture Filter Select", wm8753_enum
[23]),
339 SOC_ENUM("Capture Filter Cut-off", wm8753_enum
[24]),
340 SOC_SINGLE("Capture Filter Switch", WM8753_ADC
, 0, 1, 1),
342 SOC_SINGLE("ALC Capture Target Volume", WM8753_ALC1
, 0, 7, 0),
343 SOC_SINGLE("ALC Capture Max Volume", WM8753_ALC1
, 4, 7, 0),
344 SOC_ENUM("ALC Capture Function", wm8753_enum
[3]),
345 SOC_SINGLE("ALC Capture ZC Switch", WM8753_ALC2
, 8, 1, 0),
346 SOC_SINGLE("ALC Capture Hold Time", WM8753_ALC2
, 0, 15, 1),
347 SOC_SINGLE("ALC Capture Decay Time", WM8753_ALC3
, 4, 15, 1),
348 SOC_SINGLE("ALC Capture Attack Time", WM8753_ALC3
, 0, 15, 0),
349 SOC_SINGLE("ALC Capture NG Threshold", WM8753_NGATE
, 3, 31, 0),
350 SOC_ENUM("ALC Capture NG Type", wm8753_enum
[4]),
351 SOC_SINGLE("ALC Capture NG Switch", WM8753_NGATE
, 0, 1, 0),
353 SOC_ENUM("3D Function", wm8753_enum
[5]),
354 SOC_ENUM("3D Upper Cut-off", wm8753_enum
[6]),
355 SOC_ENUM("3D Lower Cut-off", wm8753_enum
[7]),
356 SOC_SINGLE("3D Volume", WM8753_3D
, 1, 15, 0),
357 SOC_SINGLE("3D Switch", WM8753_3D
, 0, 1, 0),
359 SOC_SINGLE("Capture 6dB Attenuate", WM8753_ADCTL1
, 2, 1, 0),
360 SOC_SINGLE("Playback 6dB Attenuate", WM8753_ADCTL1
, 1, 1, 0),
362 SOC_ENUM("De-emphasis", wm8753_enum
[8]),
363 SOC_ENUM("Playback Mono Mix", wm8753_enum
[9]),
364 SOC_ENUM("Playback Phase", wm8753_enum
[10]),
366 SOC_SINGLE_TLV("Mic2 Capture Volume", WM8753_INCTL1
, 7, 3, 0, mic_preamp_tlv
),
367 SOC_SINGLE_TLV("Mic1 Capture Volume", WM8753_INCTL1
, 5, 3, 0, mic_preamp_tlv
),
369 SOC_ENUM_EXT("DAI Mode", wm8753_enum
[26], wm8753_get_dai
, wm8753_set_dai
),
371 SOC_ENUM("ADC Data Select", wm8753_enum
[27]),
372 SOC_ENUM("ROUT2 Phase", wm8753_enum
[28]),
380 static const struct snd_kcontrol_new wm8753_left_mixer_controls
[] = {
381 SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_LOUTM2
, 8, 1, 0),
382 SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_LOUTM2
, 7, 1, 0),
383 SOC_DAPM_SINGLE("Left Playback Switch", WM8753_LOUTM1
, 8, 1, 0),
384 SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_LOUTM1
, 7, 1, 0),
388 static const struct snd_kcontrol_new wm8753_right_mixer_controls
[] = {
389 SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_ROUTM2
, 8, 1, 0),
390 SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_ROUTM2
, 7, 1, 0),
391 SOC_DAPM_SINGLE("Right Playback Switch", WM8753_ROUTM1
, 8, 1, 0),
392 SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_ROUTM1
, 7, 1, 0),
396 static const struct snd_kcontrol_new wm8753_mono_mixer_controls
[] = {
397 SOC_DAPM_SINGLE("Left Playback Switch", WM8753_MOUTM1
, 8, 1, 0),
398 SOC_DAPM_SINGLE("Right Playback Switch", WM8753_MOUTM2
, 8, 1, 0),
399 SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_MOUTM2
, 3, 1, 0),
400 SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_MOUTM2
, 7, 1, 0),
401 SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_MOUTM1
, 7, 1, 0),
405 static const struct snd_kcontrol_new wm8753_mono2_controls
=
406 SOC_DAPM_ENUM("Route", wm8753_enum
[17]);
409 static const struct snd_kcontrol_new wm8753_out3_controls
=
410 SOC_DAPM_ENUM("Route", wm8753_enum
[18]);
413 static const struct snd_kcontrol_new wm8753_out4_controls
=
414 SOC_DAPM_ENUM("Route", wm8753_enum
[19]);
417 static const struct snd_kcontrol_new wm8753_adc_mono_controls
=
418 SOC_DAPM_ENUM("Route", wm8753_enum
[22]);
421 static const struct snd_kcontrol_new wm8753_record_mixer_controls
[] = {
422 SOC_DAPM_SINGLE("Voice Capture Switch", WM8753_RECMIX2
, 3, 1, 0),
423 SOC_DAPM_SINGLE("Left Capture Switch", WM8753_RECMIX1
, 3, 1, 0),
424 SOC_DAPM_SINGLE("Right Capture Switch", WM8753_RECMIX1
, 7, 1, 0),
428 static const struct snd_kcontrol_new wm8753_adc_left_controls
=
429 SOC_DAPM_ENUM("Route", wm8753_enum
[21]);
432 static const struct snd_kcontrol_new wm8753_adc_right_controls
=
433 SOC_DAPM_ENUM("Route", wm8753_enum
[20]);
436 static const struct snd_kcontrol_new wm8753_mic_mux_controls
=
437 SOC_DAPM_ENUM("Route", wm8753_enum
[16]);
440 static const struct snd_kcontrol_new wm8753_alc_mixer_controls
[] = {
441 SOC_DAPM_SINGLE("Line Capture Switch", WM8753_INCTL2
, 3, 1, 0),
442 SOC_DAPM_SINGLE("Mic2 Capture Switch", WM8753_INCTL2
, 2, 1, 0),
443 SOC_DAPM_SINGLE("Mic1 Capture Switch", WM8753_INCTL2
, 1, 1, 0),
444 SOC_DAPM_SINGLE("Rx Capture Switch", WM8753_INCTL2
, 0, 1, 0),
448 static const struct snd_kcontrol_new wm8753_line_left_controls
=
449 SOC_DAPM_ENUM("Route", wm8753_enum
[14]);
452 static const struct snd_kcontrol_new wm8753_line_right_controls
=
453 SOC_DAPM_ENUM("Route", wm8753_enum
[13]);
456 static const struct snd_kcontrol_new wm8753_line_mono_controls
=
457 SOC_DAPM_ENUM("Route", wm8753_enum
[12]);
459 /* Line mux and mixer */
460 static const struct snd_kcontrol_new wm8753_line_mux_mix_controls
=
461 SOC_DAPM_ENUM("Route", wm8753_enum
[11]);
463 /* Rx mux and mixer */
464 static const struct snd_kcontrol_new wm8753_rx_mux_mix_controls
=
465 SOC_DAPM_ENUM("Route", wm8753_enum
[15]);
467 /* Mic Selector Mux */
468 static const struct snd_kcontrol_new wm8753_mic_sel_mux_controls
=
469 SOC_DAPM_ENUM("Route", wm8753_enum
[25]);
471 static const struct snd_soc_dapm_widget wm8753_dapm_widgets
[] = {
472 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8753_PWR1
, 5, 0),
473 SND_SOC_DAPM_MIXER("Left Mixer", WM8753_PWR4
, 0, 0,
474 &wm8753_left_mixer_controls
[0], ARRAY_SIZE(wm8753_left_mixer_controls
)),
475 SND_SOC_DAPM_PGA("Left Out 1", WM8753_PWR3
, 8, 0, NULL
, 0),
476 SND_SOC_DAPM_PGA("Left Out 2", WM8753_PWR3
, 6, 0, NULL
, 0),
477 SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", WM8753_PWR1
, 3, 0),
478 SND_SOC_DAPM_OUTPUT("LOUT1"),
479 SND_SOC_DAPM_OUTPUT("LOUT2"),
480 SND_SOC_DAPM_MIXER("Right Mixer", WM8753_PWR4
, 1, 0,
481 &wm8753_right_mixer_controls
[0], ARRAY_SIZE(wm8753_right_mixer_controls
)),
482 SND_SOC_DAPM_PGA("Right Out 1", WM8753_PWR3
, 7, 0, NULL
, 0),
483 SND_SOC_DAPM_PGA("Right Out 2", WM8753_PWR3
, 5, 0, NULL
, 0),
484 SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", WM8753_PWR1
, 2, 0),
485 SND_SOC_DAPM_OUTPUT("ROUT1"),
486 SND_SOC_DAPM_OUTPUT("ROUT2"),
487 SND_SOC_DAPM_MIXER("Mono Mixer", WM8753_PWR4
, 2, 0,
488 &wm8753_mono_mixer_controls
[0], ARRAY_SIZE(wm8753_mono_mixer_controls
)),
489 SND_SOC_DAPM_PGA("Mono Out 1", WM8753_PWR3
, 2, 0, NULL
, 0),
490 SND_SOC_DAPM_PGA("Mono Out 2", WM8753_PWR3
, 1, 0, NULL
, 0),
491 SND_SOC_DAPM_DAC("Voice DAC", "Voice Playback", WM8753_PWR1
, 4, 0),
492 SND_SOC_DAPM_OUTPUT("MONO1"),
493 SND_SOC_DAPM_MUX("Mono 2 Mux", SND_SOC_NOPM
, 0, 0, &wm8753_mono2_controls
),
494 SND_SOC_DAPM_OUTPUT("MONO2"),
495 SND_SOC_DAPM_MIXER("Out3 Left + Right", -1, 0, 0, NULL
, 0),
496 SND_SOC_DAPM_MUX("Out3 Mux", SND_SOC_NOPM
, 0, 0, &wm8753_out3_controls
),
497 SND_SOC_DAPM_PGA("Out 3", WM8753_PWR3
, 4, 0, NULL
, 0),
498 SND_SOC_DAPM_OUTPUT("OUT3"),
499 SND_SOC_DAPM_MUX("Out4 Mux", SND_SOC_NOPM
, 0, 0, &wm8753_out4_controls
),
500 SND_SOC_DAPM_PGA("Out 4", WM8753_PWR3
, 3, 0, NULL
, 0),
501 SND_SOC_DAPM_OUTPUT("OUT4"),
502 SND_SOC_DAPM_MIXER("Playback Mixer", WM8753_PWR4
, 3, 0,
503 &wm8753_record_mixer_controls
[0],
504 ARRAY_SIZE(wm8753_record_mixer_controls
)),
505 SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8753_PWR2
, 3, 0),
506 SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8753_PWR2
, 2, 0),
507 SND_SOC_DAPM_MUX("Capture Left Mixer", SND_SOC_NOPM
, 0, 0,
508 &wm8753_adc_mono_controls
),
509 SND_SOC_DAPM_MUX("Capture Right Mixer", SND_SOC_NOPM
, 0, 0,
510 &wm8753_adc_mono_controls
),
511 SND_SOC_DAPM_MUX("Capture Left Mux", SND_SOC_NOPM
, 0, 0,
512 &wm8753_adc_left_controls
),
513 SND_SOC_DAPM_MUX("Capture Right Mux", SND_SOC_NOPM
, 0, 0,
514 &wm8753_adc_right_controls
),
515 SND_SOC_DAPM_MUX("Mic Sidetone Mux", SND_SOC_NOPM
, 0, 0,
516 &wm8753_mic_mux_controls
),
517 SND_SOC_DAPM_PGA("Left Capture Volume", WM8753_PWR2
, 5, 0, NULL
, 0),
518 SND_SOC_DAPM_PGA("Right Capture Volume", WM8753_PWR2
, 4, 0, NULL
, 0),
519 SND_SOC_DAPM_MIXER("ALC Mixer", WM8753_PWR2
, 6, 0,
520 &wm8753_alc_mixer_controls
[0], ARRAY_SIZE(wm8753_alc_mixer_controls
)),
521 SND_SOC_DAPM_MUX("Line Left Mux", SND_SOC_NOPM
, 0, 0,
522 &wm8753_line_left_controls
),
523 SND_SOC_DAPM_MUX("Line Right Mux", SND_SOC_NOPM
, 0, 0,
524 &wm8753_line_right_controls
),
525 SND_SOC_DAPM_MUX("Line Mono Mux", SND_SOC_NOPM
, 0, 0,
526 &wm8753_line_mono_controls
),
527 SND_SOC_DAPM_MUX("Line Mixer", WM8753_PWR2
, 0, 0,
528 &wm8753_line_mux_mix_controls
),
529 SND_SOC_DAPM_MUX("Rx Mixer", WM8753_PWR2
, 1, 0,
530 &wm8753_rx_mux_mix_controls
),
531 SND_SOC_DAPM_PGA("Mic 1 Volume", WM8753_PWR2
, 8, 0, NULL
, 0),
532 SND_SOC_DAPM_PGA("Mic 2 Volume", WM8753_PWR2
, 7, 0, NULL
, 0),
533 SND_SOC_DAPM_MUX("Mic Selection Mux", SND_SOC_NOPM
, 0, 0,
534 &wm8753_mic_sel_mux_controls
),
535 SND_SOC_DAPM_INPUT("LINE1"),
536 SND_SOC_DAPM_INPUT("LINE2"),
537 SND_SOC_DAPM_INPUT("RXP"),
538 SND_SOC_DAPM_INPUT("RXN"),
539 SND_SOC_DAPM_INPUT("ACIN"),
540 SND_SOC_DAPM_OUTPUT("ACOP"),
541 SND_SOC_DAPM_INPUT("MIC1N"),
542 SND_SOC_DAPM_INPUT("MIC1"),
543 SND_SOC_DAPM_INPUT("MIC2N"),
544 SND_SOC_DAPM_INPUT("MIC2"),
545 SND_SOC_DAPM_VMID("VREF"),
548 static const struct snd_soc_dapm_route wm8753_dapm_routes
[] = {
550 {"Left Mixer", "Left Playback Switch", "Left DAC"},
551 {"Left Mixer", "Voice Playback Switch", "Voice DAC"},
552 {"Left Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
553 {"Left Mixer", "Bypass Playback Switch", "Line Left Mux"},
556 {"Right Mixer", "Right Playback Switch", "Right DAC"},
557 {"Right Mixer", "Voice Playback Switch", "Voice DAC"},
558 {"Right Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
559 {"Right Mixer", "Bypass Playback Switch", "Line Right Mux"},
562 {"Mono Mixer", "Voice Playback Switch", "Voice DAC"},
563 {"Mono Mixer", "Left Playback Switch", "Left DAC"},
564 {"Mono Mixer", "Right Playback Switch", "Right DAC"},
565 {"Mono Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
566 {"Mono Mixer", "Bypass Playback Switch", "Line Mono Mux"},
569 {"Left Out 1", NULL
, "Left Mixer"},
570 {"Left Out 2", NULL
, "Left Mixer"},
571 {"LOUT1", NULL
, "Left Out 1"},
572 {"LOUT2", NULL
, "Left Out 2"},
575 {"Right Out 1", NULL
, "Right Mixer"},
576 {"Right Out 2", NULL
, "Right Mixer"},
577 {"ROUT1", NULL
, "Right Out 1"},
578 {"ROUT2", NULL
, "Right Out 2"},
581 {"Mono Out 1", NULL
, "Mono Mixer"},
582 {"MONO1", NULL
, "Mono Out 1"},
585 {"Mono 2 Mux", "Left + Right", "Out3 Left + Right"},
586 {"Mono 2 Mux", "Inverted Mono 1", "MONO1"},
587 {"Mono 2 Mux", "Left", "Left Mixer"},
588 {"Mono 2 Mux", "Right", "Right Mixer"},
589 {"Mono Out 2", NULL
, "Mono 2 Mux"},
590 {"MONO2", NULL
, "Mono Out 2"},
593 {"Out3 Left + Right", NULL
, "Left Mixer"},
594 {"Out3 Left + Right", NULL
, "Right Mixer"},
595 {"Out3 Mux", "VREF", "VREF"},
596 {"Out3 Mux", "Left + Right", "Out3 Left + Right"},
597 {"Out3 Mux", "ROUT2", "ROUT2"},
598 {"Out 3", NULL
, "Out3 Mux"},
599 {"OUT3", NULL
, "Out 3"},
602 {"Out4 Mux", "VREF", "VREF"},
603 {"Out4 Mux", "Capture ST", "Playback Mixer"},
604 {"Out4 Mux", "LOUT2", "LOUT2"},
605 {"Out 4", NULL
, "Out4 Mux"},
606 {"OUT4", NULL
, "Out 4"},
609 {"Playback Mixer", "Left Capture Switch", "Left Mixer"},
610 {"Playback Mixer", "Voice Capture Switch", "Mono Mixer"},
611 {"Playback Mixer", "Right Capture Switch", "Right Mixer"},
613 /* Mic/SideTone Mux */
614 {"Mic Sidetone Mux", "Left PGA", "Left Capture Volume"},
615 {"Mic Sidetone Mux", "Right PGA", "Right Capture Volume"},
616 {"Mic Sidetone Mux", "Mic 1", "Mic 1 Volume"},
617 {"Mic Sidetone Mux", "Mic 2", "Mic 2 Volume"},
619 /* Capture Left Mux */
620 {"Capture Left Mux", "PGA", "Left Capture Volume"},
621 {"Capture Left Mux", "Line or RXP-RXN", "Line Left Mux"},
622 {"Capture Left Mux", "Line", "LINE1"},
624 /* Capture Right Mux */
625 {"Capture Right Mux", "PGA", "Right Capture Volume"},
626 {"Capture Right Mux", "Line or RXP-RXN", "Line Right Mux"},
627 {"Capture Right Mux", "Sidetone", "Playback Mixer"},
629 /* Mono Capture mixer-mux */
630 {"Capture Right Mixer", "Stereo", "Capture Right Mux"},
631 {"Capture Left Mixer", "Stereo", "Capture Left Mux"},
632 {"Capture Left Mixer", "Analogue Mix Left", "Capture Left Mux"},
633 {"Capture Left Mixer", "Analogue Mix Left", "Capture Right Mux"},
634 {"Capture Right Mixer", "Analogue Mix Right", "Capture Left Mux"},
635 {"Capture Right Mixer", "Analogue Mix Right", "Capture Right Mux"},
636 {"Capture Left Mixer", "Digital Mono Mix", "Capture Left Mux"},
637 {"Capture Left Mixer", "Digital Mono Mix", "Capture Right Mux"},
638 {"Capture Right Mixer", "Digital Mono Mix", "Capture Left Mux"},
639 {"Capture Right Mixer", "Digital Mono Mix", "Capture Right Mux"},
642 {"Left ADC", NULL
, "Capture Left Mixer"},
643 {"Right ADC", NULL
, "Capture Right Mixer"},
645 /* Left Capture Volume */
646 {"Left Capture Volume", NULL
, "ACIN"},
648 /* Right Capture Volume */
649 {"Right Capture Volume", NULL
, "Mic 2 Volume"},
652 {"ALC Mixer", "Line Capture Switch", "Line Mixer"},
653 {"ALC Mixer", "Mic2 Capture Switch", "Mic 2 Volume"},
654 {"ALC Mixer", "Mic1 Capture Switch", "Mic 1 Volume"},
655 {"ALC Mixer", "Rx Capture Switch", "Rx Mixer"},
658 {"Line Left Mux", "Line 1", "LINE1"},
659 {"Line Left Mux", "Rx Mix", "Rx Mixer"},
662 {"Line Right Mux", "Line 2", "LINE2"},
663 {"Line Right Mux", "Rx Mix", "Rx Mixer"},
666 {"Line Mono Mux", "Line Mix", "Line Mixer"},
667 {"Line Mono Mux", "Rx Mix", "Rx Mixer"},
670 {"Line Mixer", "Line 1 + 2", "LINE1"},
671 {"Line Mixer", "Line 1 - 2", "LINE1"},
672 {"Line Mixer", "Line 1 + 2", "LINE2"},
673 {"Line Mixer", "Line 1 - 2", "LINE2"},
674 {"Line Mixer", "Line 1", "LINE1"},
675 {"Line Mixer", "Line 2", "LINE2"},
678 {"Rx Mixer", "RXP - RXN", "RXP"},
679 {"Rx Mixer", "RXP + RXN", "RXP"},
680 {"Rx Mixer", "RXP - RXN", "RXN"},
681 {"Rx Mixer", "RXP + RXN", "RXN"},
682 {"Rx Mixer", "RXP", "RXP"},
683 {"Rx Mixer", "RXN", "RXN"},
686 {"Mic 1 Volume", NULL
, "MIC1N"},
687 {"Mic 1 Volume", NULL
, "Mic Selection Mux"},
690 {"Mic 2 Volume", NULL
, "MIC2N"},
691 {"Mic 2 Volume", NULL
, "MIC2"},
693 /* Mic Selector Mux */
694 {"Mic Selection Mux", "Mic 1", "MIC1"},
695 {"Mic Selection Mux", "Mic 2", "MIC2N"},
696 {"Mic Selection Mux", "Mic 3", "MIC2"},
699 {"ACOP", NULL
, "ALC Mixer"},
709 /* The size in bits of the pll divide multiplied by 10
710 * to allow rounding later */
711 #define FIXED_PLL_SIZE ((1 << 22) * 10)
713 static void pll_factors(struct _pll_div
*pll_div
, unsigned int target
,
717 unsigned int K
, Ndiv
, Nmod
;
719 Ndiv
= target
/ source
;
723 Ndiv
= target
/ source
;
727 if ((Ndiv
< 6) || (Ndiv
> 12))
729 "wm8753: unsupported N = %u\n", Ndiv
);
732 Nmod
= target
% source
;
733 Kpart
= FIXED_PLL_SIZE
* (long long)Nmod
;
735 do_div(Kpart
, source
);
737 K
= Kpart
& 0xFFFFFFFF;
739 /* Check if we need to round */
743 /* Move down to proper range now rounding is done */
749 static int wm8753_set_dai_pll(struct snd_soc_dai
*codec_dai
, int pll_id
,
750 int source
, unsigned int freq_in
, unsigned int freq_out
)
754 struct snd_soc_codec
*codec
= codec_dai
->codec
;
756 if (pll_id
< WM8753_PLL1
|| pll_id
> WM8753_PLL2
)
759 if (pll_id
== WM8753_PLL1
) {
762 reg
= snd_soc_read(codec
, WM8753_CLOCK
) & 0xffef;
766 reg
= snd_soc_read(codec
, WM8753_CLOCK
) & 0xfff7;
769 if (!freq_in
|| !freq_out
) {
771 snd_soc_write(codec
, WM8753_PLL1CTL1
+ offset
, 0x0026);
772 snd_soc_write(codec
, WM8753_CLOCK
, reg
);
776 struct _pll_div pll_div
;
778 pll_factors(&pll_div
, freq_out
* 8, freq_in
);
780 /* set up N and K PLL divisor ratios */
781 /* bits 8:5 = PLL_N, bits 3:0 = PLL_K[21:18] */
782 value
= (pll_div
.n
<< 5) + ((pll_div
.k
& 0x3c0000) >> 18);
783 snd_soc_write(codec
, WM8753_PLL1CTL2
+ offset
, value
);
785 /* bits 8:0 = PLL_K[17:9] */
786 value
= (pll_div
.k
& 0x03fe00) >> 9;
787 snd_soc_write(codec
, WM8753_PLL1CTL3
+ offset
, value
);
789 /* bits 8:0 = PLL_K[8:0] */
790 value
= pll_div
.k
& 0x0001ff;
791 snd_soc_write(codec
, WM8753_PLL1CTL4
+ offset
, value
);
793 /* set PLL as input and enable */
794 snd_soc_write(codec
, WM8753_PLL1CTL1
+ offset
, 0x0027 |
795 (pll_div
.div2
<< 3));
796 snd_soc_write(codec
, WM8753_CLOCK
, reg
| enable
);
808 /* codec hifi mclk (after PLL) clock divider coefficients */
809 static const struct _coeff_div coeff_div
[] = {
811 {12288000, 8000, 0x6, 0x0},
812 {11289600, 8000, 0x16, 0x0},
813 {18432000, 8000, 0x7, 0x0},
814 {16934400, 8000, 0x17, 0x0},
815 {12000000, 8000, 0x6, 0x1},
818 {11289600, 11025, 0x18, 0x0},
819 {16934400, 11025, 0x19, 0x0},
820 {12000000, 11025, 0x19, 0x1},
823 {12288000, 16000, 0xa, 0x0},
824 {18432000, 16000, 0xb, 0x0},
825 {12000000, 16000, 0xa, 0x1},
828 {11289600, 22050, 0x1a, 0x0},
829 {16934400, 22050, 0x1b, 0x0},
830 {12000000, 22050, 0x1b, 0x1},
833 {12288000, 32000, 0xc, 0x0},
834 {18432000, 32000, 0xd, 0x0},
835 {12000000, 32000, 0xa, 0x1},
838 {11289600, 44100, 0x10, 0x0},
839 {16934400, 44100, 0x11, 0x0},
840 {12000000, 44100, 0x11, 0x1},
843 {12288000, 48000, 0x0, 0x0},
844 {18432000, 48000, 0x1, 0x0},
845 {12000000, 48000, 0x0, 0x1},
848 {11289600, 88200, 0x1e, 0x0},
849 {16934400, 88200, 0x1f, 0x0},
850 {12000000, 88200, 0x1f, 0x1},
853 {12288000, 96000, 0xe, 0x0},
854 {18432000, 96000, 0xf, 0x0},
855 {12000000, 96000, 0xe, 0x1},
858 static int get_coeff(int mclk
, int rate
)
862 for (i
= 0; i
< ARRAY_SIZE(coeff_div
); i
++) {
863 if (coeff_div
[i
].rate
== rate
&& coeff_div
[i
].mclk
== mclk
)
870 * Clock after PLL and dividers
872 static int wm8753_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
873 int clk_id
, unsigned int freq
, int dir
)
875 struct snd_soc_codec
*codec
= codec_dai
->codec
;
876 struct wm8753_priv
*wm8753
= snd_soc_codec_get_drvdata(codec
);
884 if (clk_id
== WM8753_MCLK
) {
885 wm8753
->sysclk
= freq
;
887 } else if (clk_id
== WM8753_PCMCLK
) {
888 wm8753
->pcmclk
= freq
;
897 * Set's ADC and Voice DAC format.
899 static int wm8753_vdac_adc_set_dai_fmt(struct snd_soc_codec
*codec
,
902 u16 voice
= snd_soc_read(codec
, WM8753_PCM
) & 0x01ec;
904 /* interface format */
905 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
906 case SND_SOC_DAIFMT_I2S
:
909 case SND_SOC_DAIFMT_RIGHT_J
:
911 case SND_SOC_DAIFMT_LEFT_J
:
914 case SND_SOC_DAIFMT_DSP_A
:
917 case SND_SOC_DAIFMT_DSP_B
:
924 snd_soc_write(codec
, WM8753_PCM
, voice
);
929 * Set PCM DAI bit size and sample rate.
931 static int wm8753_pcm_hw_params(struct snd_pcm_substream
*substream
,
932 struct snd_pcm_hw_params
*params
,
933 struct snd_soc_dai
*dai
)
935 struct snd_soc_codec
*codec
= dai
->codec
;
936 struct wm8753_priv
*wm8753
= snd_soc_codec_get_drvdata(codec
);
937 u16 voice
= snd_soc_read(codec
, WM8753_PCM
) & 0x01f3;
938 u16 srate
= snd_soc_read(codec
, WM8753_SRATE1
) & 0x017f;
941 switch (params_width(params
)) {
956 if (params_rate(params
) * 384 == wm8753
->pcmclk
)
958 snd_soc_write(codec
, WM8753_SRATE1
, srate
);
960 snd_soc_write(codec
, WM8753_PCM
, voice
);
965 * Set's PCM dai fmt and BCLK.
967 static int wm8753_pcm_set_dai_fmt(struct snd_soc_codec
*codec
,
972 voice
= snd_soc_read(codec
, WM8753_PCM
) & 0x011f;
973 ioctl
= snd_soc_read(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 snd_soc_write(codec
, WM8753_PCM
, voice
);
1028 snd_soc_write(codec
, WM8753_IOCTL
, ioctl
);
1032 static int wm8753_set_dai_clkdiv(struct snd_soc_dai
*codec_dai
,
1033 int div_id
, int div
)
1035 struct snd_soc_codec
*codec
= codec_dai
->codec
;
1040 reg
= snd_soc_read(codec
, WM8753_CLOCK
) & 0x003f;
1041 snd_soc_write(codec
, WM8753_CLOCK
, reg
| div
);
1043 case WM8753_BCLKDIV
:
1044 reg
= snd_soc_read(codec
, WM8753_SRATE2
) & 0x01c7;
1045 snd_soc_write(codec
, WM8753_SRATE2
, reg
| div
);
1047 case WM8753_VXCLKDIV
:
1048 reg
= snd_soc_read(codec
, WM8753_SRATE2
) & 0x003f;
1049 snd_soc_write(codec
, WM8753_SRATE2
, reg
| div
);
1058 * Set's HiFi DAC format.
1060 static int wm8753_hdac_set_dai_fmt(struct snd_soc_codec
*codec
,
1063 u16 hifi
= snd_soc_read(codec
, WM8753_HIFI
) & 0x01e0;
1065 /* interface format */
1066 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
1067 case SND_SOC_DAIFMT_I2S
:
1070 case SND_SOC_DAIFMT_RIGHT_J
:
1072 case SND_SOC_DAIFMT_LEFT_J
:
1075 case SND_SOC_DAIFMT_DSP_A
:
1078 case SND_SOC_DAIFMT_DSP_B
:
1085 snd_soc_write(codec
, WM8753_HIFI
, hifi
);
1090 * Set's I2S DAI format.
1092 static int wm8753_i2s_set_dai_fmt(struct snd_soc_codec
*codec
,
1097 hifi
= snd_soc_read(codec
, WM8753_HIFI
) & 0x011f;
1098 ioctl
= snd_soc_read(codec
, WM8753_IOCTL
) & 0x00ae;
1100 /* set master/slave audio interface */
1101 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
1102 case SND_SOC_DAIFMT_CBS_CFS
:
1104 case SND_SOC_DAIFMT_CBM_CFM
:
1106 case SND_SOC_DAIFMT_CBM_CFS
:
1113 /* clock inversion */
1114 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
1115 case SND_SOC_DAIFMT_DSP_A
:
1116 case SND_SOC_DAIFMT_DSP_B
:
1117 /* frame inversion not valid for DSP modes */
1118 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
1119 case SND_SOC_DAIFMT_NB_NF
:
1121 case SND_SOC_DAIFMT_IB_NF
:
1128 case SND_SOC_DAIFMT_I2S
:
1129 case SND_SOC_DAIFMT_RIGHT_J
:
1130 case SND_SOC_DAIFMT_LEFT_J
:
1132 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
1133 case SND_SOC_DAIFMT_NB_NF
:
1135 case SND_SOC_DAIFMT_IB_IF
:
1138 case SND_SOC_DAIFMT_IB_NF
:
1141 case SND_SOC_DAIFMT_NB_IF
:
1152 snd_soc_write(codec
, WM8753_HIFI
, hifi
);
1153 snd_soc_write(codec
, WM8753_IOCTL
, ioctl
);
1158 * Set PCM DAI bit size and sample rate.
1160 static int wm8753_i2s_hw_params(struct snd_pcm_substream
*substream
,
1161 struct snd_pcm_hw_params
*params
,
1162 struct snd_soc_dai
*dai
)
1164 struct snd_soc_codec
*codec
= dai
->codec
;
1165 struct wm8753_priv
*wm8753
= snd_soc_codec_get_drvdata(codec
);
1166 u16 srate
= snd_soc_read(codec
, WM8753_SRATE1
) & 0x01c0;
1167 u16 hifi
= snd_soc_read(codec
, WM8753_HIFI
) & 0x01f3;
1170 /* is digital filter coefficient valid ? */
1171 coeff
= get_coeff(wm8753
->sysclk
, params_rate(params
));
1173 printk(KERN_ERR
"wm8753 invalid MCLK or rate\n");
1176 snd_soc_write(codec
, WM8753_SRATE1
, srate
| (coeff_div
[coeff
].sr
<< 1) |
1177 coeff_div
[coeff
].usb
);
1180 switch (params_width(params
)) {
1194 snd_soc_write(codec
, WM8753_HIFI
, hifi
);
1198 static int wm8753_mode1v_set_dai_fmt(struct snd_soc_codec
*codec
,
1203 /* set clk source as pcmclk */
1204 clock
= snd_soc_read(codec
, WM8753_CLOCK
) & 0xfffb;
1205 snd_soc_write(codec
, WM8753_CLOCK
, clock
);
1207 return wm8753_vdac_adc_set_dai_fmt(codec
, fmt
);
1210 static int wm8753_mode1h_set_dai_fmt(struct snd_soc_codec
*codec
,
1213 return wm8753_hdac_set_dai_fmt(codec
, fmt
);
1216 static int wm8753_mode2_set_dai_fmt(struct snd_soc_codec
*codec
,
1221 /* set clk source as pcmclk */
1222 clock
= snd_soc_read(codec
, WM8753_CLOCK
) & 0xfffb;
1223 snd_soc_write(codec
, WM8753_CLOCK
, clock
);
1225 return wm8753_vdac_adc_set_dai_fmt(codec
, fmt
);
1228 static int wm8753_mode3_4_set_dai_fmt(struct snd_soc_codec
*codec
,
1233 /* set clk source as mclk */
1234 clock
= snd_soc_read(codec
, WM8753_CLOCK
) & 0xfffb;
1235 snd_soc_write(codec
, WM8753_CLOCK
, clock
| 0x4);
1237 if (wm8753_hdac_set_dai_fmt(codec
, fmt
) < 0)
1239 return wm8753_vdac_adc_set_dai_fmt(codec
, fmt
);
1242 static int wm8753_hifi_write_dai_fmt(struct snd_soc_codec
*codec
,
1245 struct wm8753_priv
*wm8753
= snd_soc_codec_get_drvdata(codec
);
1248 switch (wm8753
->dai_func
) {
1250 ret
= wm8753_mode1h_set_dai_fmt(codec
, fmt
);
1253 ret
= wm8753_mode2_set_dai_fmt(codec
, fmt
);
1257 ret
= wm8753_mode3_4_set_dai_fmt(codec
, fmt
);
1265 return wm8753_i2s_set_dai_fmt(codec
, fmt
);
1268 static int wm8753_hifi_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
1271 struct snd_soc_codec
*codec
= codec_dai
->codec
;
1272 struct wm8753_priv
*wm8753
= snd_soc_codec_get_drvdata(codec
);
1274 wm8753
->hifi_fmt
= fmt
;
1276 return wm8753_hifi_write_dai_fmt(codec
, fmt
);
1279 static int wm8753_voice_write_dai_fmt(struct snd_soc_codec
*codec
,
1282 struct wm8753_priv
*wm8753
= snd_soc_codec_get_drvdata(codec
);
1285 if (wm8753
->dai_func
!= 0)
1288 ret
= wm8753_mode1v_set_dai_fmt(codec
, fmt
);
1291 ret
= wm8753_pcm_set_dai_fmt(codec
, fmt
);
1298 static int wm8753_voice_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
1301 struct snd_soc_codec
*codec
= codec_dai
->codec
;
1302 struct wm8753_priv
*wm8753
= snd_soc_codec_get_drvdata(codec
);
1304 wm8753
->voice_fmt
= fmt
;
1306 return wm8753_voice_write_dai_fmt(codec
, fmt
);
1309 static int wm8753_mute(struct snd_soc_dai
*dai
, int mute
)
1311 struct snd_soc_codec
*codec
= dai
->codec
;
1312 u16 mute_reg
= snd_soc_read(codec
, WM8753_DAC
) & 0xfff7;
1313 struct wm8753_priv
*wm8753
= snd_soc_codec_get_drvdata(codec
);
1315 /* the digital mute covers the HiFi and Voice DAC's on the WM8753.
1316 * make sure we check if they are not both active when we mute */
1317 if (mute
&& wm8753
->dai_func
== 1) {
1318 if (!snd_soc_codec_is_active(codec
))
1319 snd_soc_write(codec
, WM8753_DAC
, mute_reg
| 0x8);
1322 snd_soc_write(codec
, WM8753_DAC
, mute_reg
| 0x8);
1324 snd_soc_write(codec
, WM8753_DAC
, mute_reg
);
1330 static void wm8753_charge_work(struct work_struct
*work
)
1332 struct wm8753_priv
*wm8753
=
1333 container_of(work
, struct wm8753_priv
, charge_work
.work
);
1336 regmap_update_bits(wm8753
->regmap
, WM8753_PWR1
, 0x0180, 0x0100);
1339 static int wm8753_set_bias_level(struct snd_soc_codec
*codec
,
1340 enum snd_soc_bias_level level
)
1342 struct wm8753_priv
*wm8753
= snd_soc_codec_get_drvdata(codec
);
1343 u16 pwr_reg
= snd_soc_read(codec
, WM8753_PWR1
) & 0xfe3e;
1346 case SND_SOC_BIAS_ON
:
1347 /* set vmid to 50k and unmute dac */
1348 snd_soc_write(codec
, WM8753_PWR1
, pwr_reg
| 0x00c0);
1350 case SND_SOC_BIAS_PREPARE
:
1351 /* Wait until fully charged */
1352 flush_delayed_work(&wm8753
->charge_work
);
1354 case SND_SOC_BIAS_STANDBY
:
1355 if (snd_soc_codec_get_bias_level(codec
) == SND_SOC_BIAS_OFF
) {
1356 /* set vmid to 5k for quick power up */
1357 snd_soc_write(codec
, WM8753_PWR1
, pwr_reg
| 0x01c1);
1358 schedule_delayed_work(&wm8753
->charge_work
,
1359 msecs_to_jiffies(caps_charge
));
1361 /* mute dac and set vmid to 500k, enable VREF */
1362 snd_soc_write(codec
, WM8753_PWR1
, pwr_reg
| 0x0141);
1365 case SND_SOC_BIAS_OFF
:
1366 cancel_delayed_work_sync(&wm8753
->charge_work
);
1367 snd_soc_write(codec
, WM8753_PWR1
, 0x0001);
1373 #define WM8753_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
1374 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
1375 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
1376 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
1378 #define WM8753_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1379 SNDRV_PCM_FMTBIT_S24_LE)
1382 * The WM8753 supports up to 4 different and mutually exclusive DAI
1383 * configurations. This gives 2 PCM's available for use, hifi and voice.
1384 * NOTE: The Voice PCM cannot play or capture audio to the CPU as it's DAI
1385 * is connected between the wm8753 and a BT codec or GSM modem.
1387 * 1. Voice over PCM DAI - HIFI DAC over HIFI DAI
1388 * 2. Voice over HIFI DAI - HIFI disabled
1389 * 3. Voice disabled - HIFI over HIFI
1390 * 4. Voice disabled - HIFI over HIFI, uses voice DAI LRC for capture
1392 static const struct snd_soc_dai_ops wm8753_dai_ops_hifi_mode
= {
1393 .hw_params
= wm8753_i2s_hw_params
,
1394 .digital_mute
= wm8753_mute
,
1395 .set_fmt
= wm8753_hifi_set_dai_fmt
,
1396 .set_clkdiv
= wm8753_set_dai_clkdiv
,
1397 .set_pll
= wm8753_set_dai_pll
,
1398 .set_sysclk
= wm8753_set_dai_sysclk
,
1401 static const struct snd_soc_dai_ops wm8753_dai_ops_voice_mode
= {
1402 .hw_params
= wm8753_pcm_hw_params
,
1403 .digital_mute
= wm8753_mute
,
1404 .set_fmt
= wm8753_voice_set_dai_fmt
,
1405 .set_clkdiv
= wm8753_set_dai_clkdiv
,
1406 .set_pll
= wm8753_set_dai_pll
,
1407 .set_sysclk
= wm8753_set_dai_sysclk
,
1410 static struct snd_soc_dai_driver wm8753_dai
[] = {
1411 /* DAI HiFi mode 1 */
1412 { .name
= "wm8753-hifi",
1414 .stream_name
= "HiFi Playback",
1417 .rates
= WM8753_RATES
,
1418 .formats
= WM8753_FORMATS
1420 .capture
= { /* dummy for fast DAI switching */
1421 .stream_name
= "Capture",
1424 .rates
= WM8753_RATES
,
1425 .formats
= WM8753_FORMATS
1427 .ops
= &wm8753_dai_ops_hifi_mode
,
1429 /* DAI Voice mode 1 */
1430 { .name
= "wm8753-voice",
1432 .stream_name
= "Voice Playback",
1435 .rates
= WM8753_RATES
,
1436 .formats
= WM8753_FORMATS
,
1439 .stream_name
= "Capture",
1442 .rates
= WM8753_RATES
,
1443 .formats
= WM8753_FORMATS
,
1445 .ops
= &wm8753_dai_ops_voice_mode
,
1449 static int wm8753_resume(struct snd_soc_codec
*codec
)
1451 struct wm8753_priv
*wm8753
= snd_soc_codec_get_drvdata(codec
);
1453 regcache_sync(wm8753
->regmap
);
1458 static int wm8753_probe(struct snd_soc_codec
*codec
)
1460 struct wm8753_priv
*wm8753
= snd_soc_codec_get_drvdata(codec
);
1463 INIT_DELAYED_WORK(&wm8753
->charge_work
, wm8753_charge_work
);
1465 ret
= wm8753_reset(codec
);
1467 dev_err(codec
->dev
, "Failed to issue reset: %d\n", ret
);
1471 wm8753
->dai_func
= 0;
1473 /* set the update bits */
1474 snd_soc_update_bits(codec
, WM8753_LDAC
, 0x0100, 0x0100);
1475 snd_soc_update_bits(codec
, WM8753_RDAC
, 0x0100, 0x0100);
1476 snd_soc_update_bits(codec
, WM8753_LADC
, 0x0100, 0x0100);
1477 snd_soc_update_bits(codec
, WM8753_RADC
, 0x0100, 0x0100);
1478 snd_soc_update_bits(codec
, WM8753_LOUT1V
, 0x0100, 0x0100);
1479 snd_soc_update_bits(codec
, WM8753_ROUT1V
, 0x0100, 0x0100);
1480 snd_soc_update_bits(codec
, WM8753_LOUT2V
, 0x0100, 0x0100);
1481 snd_soc_update_bits(codec
, WM8753_ROUT2V
, 0x0100, 0x0100);
1482 snd_soc_update_bits(codec
, WM8753_LINVOL
, 0x0100, 0x0100);
1483 snd_soc_update_bits(codec
, WM8753_RINVOL
, 0x0100, 0x0100);
1488 static struct snd_soc_codec_driver soc_codec_dev_wm8753
= {
1489 .probe
= wm8753_probe
,
1490 .resume
= wm8753_resume
,
1491 .set_bias_level
= wm8753_set_bias_level
,
1492 .suspend_bias_off
= true,
1494 .controls
= wm8753_snd_controls
,
1495 .num_controls
= ARRAY_SIZE(wm8753_snd_controls
),
1496 .dapm_widgets
= wm8753_dapm_widgets
,
1497 .num_dapm_widgets
= ARRAY_SIZE(wm8753_dapm_widgets
),
1498 .dapm_routes
= wm8753_dapm_routes
,
1499 .num_dapm_routes
= ARRAY_SIZE(wm8753_dapm_routes
),
1502 static const struct of_device_id wm8753_of_match
[] = {
1503 { .compatible
= "wlf,wm8753", },
1506 MODULE_DEVICE_TABLE(of
, wm8753_of_match
);
1508 static const struct regmap_config wm8753_regmap
= {
1512 .max_register
= WM8753_ADCTL2
,
1513 .writeable_reg
= wm8753_writeable
,
1514 .volatile_reg
= wm8753_volatile
,
1516 .cache_type
= REGCACHE_RBTREE
,
1517 .reg_defaults
= wm8753_reg_defaults
,
1518 .num_reg_defaults
= ARRAY_SIZE(wm8753_reg_defaults
),
1521 #if defined(CONFIG_SPI_MASTER)
1522 static int wm8753_spi_probe(struct spi_device
*spi
)
1524 struct wm8753_priv
*wm8753
;
1527 wm8753
= devm_kzalloc(&spi
->dev
, sizeof(struct wm8753_priv
),
1532 spi_set_drvdata(spi
, wm8753
);
1534 wm8753
->regmap
= devm_regmap_init_spi(spi
, &wm8753_regmap
);
1535 if (IS_ERR(wm8753
->regmap
)) {
1536 ret
= PTR_ERR(wm8753
->regmap
);
1537 dev_err(&spi
->dev
, "Failed to allocate register map: %d\n",
1542 ret
= snd_soc_register_codec(&spi
->dev
, &soc_codec_dev_wm8753
,
1543 wm8753_dai
, ARRAY_SIZE(wm8753_dai
));
1545 dev_err(&spi
->dev
, "Failed to register CODEC: %d\n", ret
);
1550 static int wm8753_spi_remove(struct spi_device
*spi
)
1552 snd_soc_unregister_codec(&spi
->dev
);
1556 static struct spi_driver wm8753_spi_driver
= {
1559 .owner
= THIS_MODULE
,
1560 .of_match_table
= wm8753_of_match
,
1562 .probe
= wm8753_spi_probe
,
1563 .remove
= wm8753_spi_remove
,
1565 #endif /* CONFIG_SPI_MASTER */
1567 #if IS_ENABLED(CONFIG_I2C)
1568 static int wm8753_i2c_probe(struct i2c_client
*i2c
,
1569 const struct i2c_device_id
*id
)
1571 struct wm8753_priv
*wm8753
;
1574 wm8753
= devm_kzalloc(&i2c
->dev
, sizeof(struct wm8753_priv
),
1579 i2c_set_clientdata(i2c
, wm8753
);
1581 wm8753
->regmap
= devm_regmap_init_i2c(i2c
, &wm8753_regmap
);
1582 if (IS_ERR(wm8753
->regmap
)) {
1583 ret
= PTR_ERR(wm8753
->regmap
);
1584 dev_err(&i2c
->dev
, "Failed to allocate register map: %d\n",
1589 ret
= snd_soc_register_codec(&i2c
->dev
, &soc_codec_dev_wm8753
,
1590 wm8753_dai
, ARRAY_SIZE(wm8753_dai
));
1592 dev_err(&i2c
->dev
, "Failed to register CODEC: %d\n", ret
);
1597 static int wm8753_i2c_remove(struct i2c_client
*client
)
1599 snd_soc_unregister_codec(&client
->dev
);
1603 static const struct i2c_device_id wm8753_i2c_id
[] = {
1607 MODULE_DEVICE_TABLE(i2c
, wm8753_i2c_id
);
1609 static struct i2c_driver wm8753_i2c_driver
= {
1612 .owner
= THIS_MODULE
,
1613 .of_match_table
= wm8753_of_match
,
1615 .probe
= wm8753_i2c_probe
,
1616 .remove
= wm8753_i2c_remove
,
1617 .id_table
= wm8753_i2c_id
,
1621 static int __init
wm8753_modinit(void)
1624 #if IS_ENABLED(CONFIG_I2C)
1625 ret
= i2c_add_driver(&wm8753_i2c_driver
);
1627 printk(KERN_ERR
"Failed to register wm8753 I2C driver: %d\n",
1631 #if defined(CONFIG_SPI_MASTER)
1632 ret
= spi_register_driver(&wm8753_spi_driver
);
1634 printk(KERN_ERR
"Failed to register wm8753 SPI driver: %d\n",
1640 module_init(wm8753_modinit
);
1642 static void __exit
wm8753_exit(void)
1644 #if IS_ENABLED(CONFIG_I2C)
1645 i2c_del_driver(&wm8753_i2c_driver
);
1647 #if defined(CONFIG_SPI_MASTER)
1648 spi_unregister_driver(&wm8753_spi_driver
);
1651 module_exit(wm8753_exit
);
1653 MODULE_DESCRIPTION("ASoC WM8753 driver");
1654 MODULE_AUTHOR("Liam Girdwood");
1655 MODULE_LICENSE("GPL");