2 * wm9705.c -- ALSA Soc WM9705 codec support
4 * Copyright 2008 Ian Molton <spyro@f2s.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; Version 2 of the License only.
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/mfd/wm97xx.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/device.h>
18 #include <linux/regmap.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/ac97_codec.h>
22 #include <sound/ac97/codec.h>
23 #include <sound/ac97/compat.h>
24 #include <sound/initval.h>
25 #include <sound/soc.h>
27 #define WM9705_VENDOR_ID 0x574d4c05
28 #define WM9705_VENDOR_ID_MASK 0xffffffff
31 struct snd_ac97
*ac97
;
32 struct wm97xx_platform_data
*mfd_pdata
;
35 static const struct reg_default wm9705_reg_defaults
[] = {
66 static const struct regmap_config wm9705_regmap_config
= {
71 .cache_type
= REGCACHE_RBTREE
,
73 .volatile_reg
= regmap_ac97_default_volatile
,
75 .reg_defaults
= wm9705_reg_defaults
,
76 .num_reg_defaults
= ARRAY_SIZE(wm9705_reg_defaults
),
79 static const struct snd_kcontrol_new wm9705_snd_ac97_controls
[] = {
80 SOC_DOUBLE("Master Playback Volume", AC97_MASTER
, 8, 0, 31, 1),
81 SOC_SINGLE("Master Playback Switch", AC97_MASTER
, 15, 1, 1),
82 SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE
, 8, 0, 31, 1),
83 SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE
, 15, 1, 1),
84 SOC_DOUBLE("PCM Playback Volume", AC97_PCM
, 8, 0, 31, 1),
85 SOC_SINGLE("PCM Playback Switch", AC97_PCM
, 15, 1, 1),
86 SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO
, 0, 31, 1),
87 SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO
, 15, 1, 1),
88 SOC_SINGLE("PCBeep Playback Volume", AC97_PC_BEEP
, 1, 15, 1),
89 SOC_SINGLE("Phone Playback Volume", AC97_PHONE
, 0, 31, 1),
90 SOC_DOUBLE("Line Playback Volume", AC97_LINE
, 8, 0, 31, 1),
91 SOC_DOUBLE("CD Playback Volume", AC97_CD
, 8, 0, 31, 1),
92 SOC_SINGLE("Mic Playback Volume", AC97_MIC
, 0, 31, 1),
93 SOC_SINGLE("Mic 20dB Boost Switch", AC97_MIC
, 6, 1, 0),
94 SOC_DOUBLE("Capture Volume", AC97_REC_GAIN
, 8, 0, 15, 0),
95 SOC_SINGLE("Capture Switch", AC97_REC_GAIN
, 15, 1, 1),
98 static const char *wm9705_mic
[] = {"Mic 1", "Mic 2"};
99 static const char *wm9705_rec_sel
[] = {"Mic", "CD", "NC", "NC",
100 "Line", "Stereo Mix", "Mono Mix", "Phone"};
102 static SOC_ENUM_SINGLE_DECL(wm9705_enum_mic
,
103 AC97_GENERAL_PURPOSE
, 8, wm9705_mic
);
104 static SOC_ENUM_SINGLE_DECL(wm9705_enum_rec_l
,
105 AC97_REC_SEL
, 8, wm9705_rec_sel
);
106 static SOC_ENUM_SINGLE_DECL(wm9705_enum_rec_r
,
107 AC97_REC_SEL
, 0, wm9705_rec_sel
);
109 /* Headphone Mixer */
110 static const struct snd_kcontrol_new wm9705_hp_mixer_controls
[] = {
111 SOC_DAPM_SINGLE("PCBeep Playback Switch", AC97_PC_BEEP
, 15, 1, 1),
112 SOC_DAPM_SINGLE("CD Playback Switch", AC97_CD
, 15, 1, 1),
113 SOC_DAPM_SINGLE("Mic Playback Switch", AC97_MIC
, 15, 1, 1),
114 SOC_DAPM_SINGLE("Phone Playback Switch", AC97_PHONE
, 15, 1, 1),
115 SOC_DAPM_SINGLE("Line Playback Switch", AC97_LINE
, 15, 1, 1),
119 static const struct snd_kcontrol_new wm9705_mic_src_controls
=
120 SOC_DAPM_ENUM("Route", wm9705_enum_mic
);
123 static const struct snd_kcontrol_new wm9705_capture_selectl_controls
=
124 SOC_DAPM_ENUM("Route", wm9705_enum_rec_l
);
125 static const struct snd_kcontrol_new wm9705_capture_selectr_controls
=
126 SOC_DAPM_ENUM("Route", wm9705_enum_rec_r
);
129 static const struct snd_soc_dapm_widget wm9705_dapm_widgets
[] = {
130 SND_SOC_DAPM_MUX("Mic Source", SND_SOC_NOPM
, 0, 0,
131 &wm9705_mic_src_controls
),
132 SND_SOC_DAPM_MUX("Left Capture Source", SND_SOC_NOPM
, 0, 0,
133 &wm9705_capture_selectl_controls
),
134 SND_SOC_DAPM_MUX("Right Capture Source", SND_SOC_NOPM
, 0, 0,
135 &wm9705_capture_selectr_controls
),
136 SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback",
138 SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback",
140 SND_SOC_DAPM_MIXER_NAMED_CTL("HP Mixer", SND_SOC_NOPM
, 0, 0,
141 &wm9705_hp_mixer_controls
[0],
142 ARRAY_SIZE(wm9705_hp_mixer_controls
)),
143 SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM
, 0, 0, NULL
, 0),
144 SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture", SND_SOC_NOPM
, 0, 0),
145 SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture", SND_SOC_NOPM
, 0, 0),
146 SND_SOC_DAPM_PGA("Headphone PGA", SND_SOC_NOPM
, 0, 0, NULL
, 0),
147 SND_SOC_DAPM_PGA("Speaker PGA", SND_SOC_NOPM
, 0, 0, NULL
, 0),
148 SND_SOC_DAPM_PGA("Line PGA", SND_SOC_NOPM
, 0, 0, NULL
, 0),
149 SND_SOC_DAPM_PGA("Line out PGA", SND_SOC_NOPM
, 0, 0, NULL
, 0),
150 SND_SOC_DAPM_PGA("Mono PGA", SND_SOC_NOPM
, 0, 0, NULL
, 0),
151 SND_SOC_DAPM_PGA("Phone PGA", SND_SOC_NOPM
, 0, 0, NULL
, 0),
152 SND_SOC_DAPM_PGA("Mic PGA", SND_SOC_NOPM
, 0, 0, NULL
, 0),
153 SND_SOC_DAPM_PGA("PCBEEP PGA", SND_SOC_NOPM
, 0, 0, NULL
, 0),
154 SND_SOC_DAPM_PGA("CD PGA", SND_SOC_NOPM
, 0, 0, NULL
, 0),
155 SND_SOC_DAPM_PGA("ADC PGA", SND_SOC_NOPM
, 0, 0, NULL
, 0),
156 SND_SOC_DAPM_OUTPUT("HPOUTL"),
157 SND_SOC_DAPM_OUTPUT("HPOUTR"),
158 SND_SOC_DAPM_OUTPUT("LOUT"),
159 SND_SOC_DAPM_OUTPUT("ROUT"),
160 SND_SOC_DAPM_OUTPUT("MONOOUT"),
161 SND_SOC_DAPM_INPUT("PHONE"),
162 SND_SOC_DAPM_INPUT("LINEINL"),
163 SND_SOC_DAPM_INPUT("LINEINR"),
164 SND_SOC_DAPM_INPUT("CDINL"),
165 SND_SOC_DAPM_INPUT("CDINR"),
166 SND_SOC_DAPM_INPUT("PCBEEP"),
167 SND_SOC_DAPM_INPUT("MIC1"),
168 SND_SOC_DAPM_INPUT("MIC2"),
172 * WM9705 has no switches to disable the route from the inputs to the HP mixer
173 * so in order to prevent active inputs from forcing the audio outputs to be
174 * constantly enabled, we use the mutes on those inputs to simulate such
177 static const struct snd_soc_dapm_route wm9705_audio_map
[] = {
179 {"HP Mixer", "PCBeep Playback Switch", "PCBEEP PGA"},
180 {"HP Mixer", "CD Playback Switch", "CD PGA"},
181 {"HP Mixer", "Mic Playback Switch", "Mic PGA"},
182 {"HP Mixer", "Phone Playback Switch", "Phone PGA"},
183 {"HP Mixer", "Line Playback Switch", "Line PGA"},
184 {"HP Mixer", NULL
, "Left DAC"},
185 {"HP Mixer", NULL
, "Right DAC"},
188 {"Mono Mixer", NULL
, "HP Mixer"},
191 {"Headphone PGA", NULL
, "HP Mixer"},
192 {"HPOUTL", NULL
, "Headphone PGA"},
193 {"HPOUTR", NULL
, "Headphone PGA"},
194 {"Line out PGA", NULL
, "HP Mixer"},
195 {"LOUT", NULL
, "Line out PGA"},
196 {"ROUT", NULL
, "Line out PGA"},
197 {"Mono PGA", NULL
, "Mono Mixer"},
198 {"MONOOUT", NULL
, "Mono PGA"},
201 {"CD PGA", NULL
, "CDINL"},
202 {"CD PGA", NULL
, "CDINR"},
203 {"Line PGA", NULL
, "LINEINL"},
204 {"Line PGA", NULL
, "LINEINR"},
205 {"Phone PGA", NULL
, "PHONE"},
206 {"Mic Source", "Mic 1", "MIC1"},
207 {"Mic Source", "Mic 2", "MIC2"},
208 {"Mic PGA", NULL
, "Mic Source"},
209 {"PCBEEP PGA", NULL
, "PCBEEP"},
211 /* Left capture selector */
212 {"Left Capture Source", "Mic", "Mic Source"},
213 {"Left Capture Source", "CD", "CDINL"},
214 {"Left Capture Source", "Line", "LINEINL"},
215 {"Left Capture Source", "Stereo Mix", "HP Mixer"},
216 {"Left Capture Source", "Mono Mix", "HP Mixer"},
217 {"Left Capture Source", "Phone", "PHONE"},
219 /* Right capture source */
220 {"Right Capture Source", "Mic", "Mic Source"},
221 {"Right Capture Source", "CD", "CDINR"},
222 {"Right Capture Source", "Line", "LINEINR"},
223 {"Right Capture Source", "Stereo Mix", "HP Mixer"},
224 {"Right Capture Source", "Mono Mix", "HP Mixer"},
225 {"Right Capture Source", "Phone", "PHONE"},
227 {"ADC PGA", NULL
, "Left Capture Source"},
228 {"ADC PGA", NULL
, "Right Capture Source"},
231 {"Left ADC", NULL
, "ADC PGA"},
232 {"Right ADC", NULL
, "ADC PGA"},
235 static int ac97_prepare(struct snd_pcm_substream
*substream
,
236 struct snd_soc_dai
*dai
)
238 struct snd_soc_codec
*codec
= dai
->codec
;
241 snd_soc_update_bits(codec
, AC97_EXTENDED_STATUS
, 0x1, 0x1);
243 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
244 reg
= AC97_PCM_FRONT_DAC_RATE
;
246 reg
= AC97_PCM_LR_ADC_RATE
;
248 return snd_soc_write(codec
, reg
, substream
->runtime
->rate
);
251 #define WM9705_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | \
252 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
253 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
254 SNDRV_PCM_RATE_48000)
256 static const struct snd_soc_dai_ops wm9705_dai_ops
= {
257 .prepare
= ac97_prepare
,
260 static struct snd_soc_dai_driver wm9705_dai
[] = {
262 .name
= "wm9705-hifi",
264 .stream_name
= "HiFi Playback",
267 .rates
= WM9705_AC97_RATES
,
268 .formats
= SND_SOC_STD_AC97_FMTS
,
271 .stream_name
= "HiFi Capture",
274 .rates
= WM9705_AC97_RATES
,
275 .formats
= SND_SOC_STD_AC97_FMTS
,
277 .ops
= &wm9705_dai_ops
,
280 .name
= "wm9705-aux",
282 .stream_name
= "Aux Playback",
285 .rates
= WM9705_AC97_RATES
,
286 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
292 static int wm9705_soc_suspend(struct snd_soc_codec
*codec
)
294 regcache_cache_bypass(codec
->component
.regmap
, true);
295 snd_soc_write(codec
, AC97_POWERDOWN
, 0xffff);
296 regcache_cache_bypass(codec
->component
.regmap
, false);
301 static int wm9705_soc_resume(struct snd_soc_codec
*codec
)
303 struct wm9705_priv
*wm9705
= snd_soc_codec_get_drvdata(codec
);
306 ret
= snd_ac97_reset(wm9705
->ac97
, true, WM9705_VENDOR_ID
,
307 WM9705_VENDOR_ID_MASK
);
311 regcache_sync(codec
->component
.regmap
);
316 #define wm9705_soc_suspend NULL
317 #define wm9705_soc_resume NULL
320 static int wm9705_soc_probe(struct snd_soc_codec
*codec
)
322 struct wm9705_priv
*wm9705
= snd_soc_codec_get_drvdata(codec
);
323 struct regmap
*regmap
;
325 if (wm9705
->mfd_pdata
) {
326 wm9705
->ac97
= wm9705
->mfd_pdata
->ac97
;
327 regmap
= wm9705
->mfd_pdata
->regmap
;
329 #ifdef CONFIG_SND_SOC_AC97_BUS
330 wm9705
->ac97
= snd_soc_new_ac97_codec(codec
, WM9705_VENDOR_ID
,
331 WM9705_VENDOR_ID_MASK
);
332 if (IS_ERR(wm9705
->ac97
)) {
333 dev_err(codec
->dev
, "Failed to register AC97 codec\n");
334 return PTR_ERR(wm9705
->ac97
);
337 regmap
= regmap_init_ac97(wm9705
->ac97
, &wm9705_regmap_config
);
338 if (IS_ERR(regmap
)) {
339 snd_soc_free_ac97_codec(wm9705
->ac97
);
340 return PTR_ERR(regmap
);
345 snd_soc_codec_set_drvdata(codec
, wm9705
->ac97
);
346 snd_soc_codec_init_regmap(codec
, regmap
);
351 static int wm9705_soc_remove(struct snd_soc_codec
*codec
)
353 #ifdef CONFIG_SND_SOC_AC97_BUS
354 struct wm9705_priv
*wm9705
= snd_soc_codec_get_drvdata(codec
);
356 if (!wm9705
->mfd_pdata
) {
357 snd_soc_codec_exit_regmap(codec
);
358 snd_soc_free_ac97_codec(wm9705
->ac97
);
364 static const struct snd_soc_codec_driver soc_codec_dev_wm9705
= {
365 .probe
= wm9705_soc_probe
,
366 .remove
= wm9705_soc_remove
,
367 .suspend
= wm9705_soc_suspend
,
368 .resume
= wm9705_soc_resume
,
370 .component_driver
= {
371 .controls
= wm9705_snd_ac97_controls
,
372 .num_controls
= ARRAY_SIZE(wm9705_snd_ac97_controls
),
373 .dapm_widgets
= wm9705_dapm_widgets
,
374 .num_dapm_widgets
= ARRAY_SIZE(wm9705_dapm_widgets
),
375 .dapm_routes
= wm9705_audio_map
,
376 .num_dapm_routes
= ARRAY_SIZE(wm9705_audio_map
),
380 static int wm9705_probe(struct platform_device
*pdev
)
382 struct wm9705_priv
*wm9705
;
384 wm9705
= devm_kzalloc(&pdev
->dev
, sizeof(*wm9705
), GFP_KERNEL
);
388 wm9705
->mfd_pdata
= dev_get_platdata(&pdev
->dev
);
389 platform_set_drvdata(pdev
, wm9705
);
391 return snd_soc_register_codec(&pdev
->dev
,
392 &soc_codec_dev_wm9705
, wm9705_dai
, ARRAY_SIZE(wm9705_dai
));
395 static int wm9705_remove(struct platform_device
*pdev
)
397 snd_soc_unregister_codec(&pdev
->dev
);
401 static struct platform_driver wm9705_codec_driver
= {
403 .name
= "wm9705-codec",
406 .probe
= wm9705_probe
,
407 .remove
= wm9705_remove
,
410 module_platform_driver(wm9705_codec_driver
);
412 MODULE_DESCRIPTION("ASoC WM9705 driver");
413 MODULE_AUTHOR("Ian Molton");
414 MODULE_LICENSE("GPL v2");