Merge remote-tracking branch 'asoc/topic/rt5670' into asoc-next
[deliverable/linux.git] / sound / soc / codecs / wm9705.c
CommitLineData
2aceefef
IM
1/*
2 * wm9705.c -- ALSA Soc WM9705 codec support
3 *
4 * Copyright 2008 Ian Molton <spyro@f2s.com>
5 *
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.
9 *
10 */
11
12#include <linux/init.h>
5a0e3ad6 13#include <linux/slab.h>
2aceefef
IM
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/device.h>
17#include <sound/core.h>
18#include <sound/pcm.h>
19#include <sound/ac97_codec.h>
20#include <sound/initval.h>
21#include <sound/soc.h>
2aceefef 22
01e097d6
MB
23#include "wm9705.h"
24
2aceefef
IM
25/*
26 * WM9705 register cache
27 */
28static const u16 wm9705_reg[] = {
29 0x6150, 0x8000, 0x8000, 0x8000, /* 0x0 */
30 0x0000, 0x8000, 0x8008, 0x8008, /* 0x8 */
31 0x8808, 0x8808, 0x8808, 0x8808, /* 0x10 */
32 0x8808, 0x0000, 0x8000, 0x0000, /* 0x18 */
33 0x0000, 0x0000, 0x0000, 0x000f, /* 0x20 */
34 0x0605, 0x0000, 0xbb80, 0x0000, /* 0x28 */
35 0x0000, 0xbb80, 0x0000, 0x0000, /* 0x30 */
36 0x0000, 0x2000, 0x0000, 0x0000, /* 0x38 */
37 0x0000, 0x0000, 0x0000, 0x0000, /* 0x40 */
38 0x0000, 0x0000, 0x0000, 0x0000, /* 0x48 */
39 0x0000, 0x0000, 0x0000, 0x0000, /* 0x50 */
40 0x0000, 0x0000, 0x0000, 0x0000, /* 0x58 */
41 0x0000, 0x0000, 0x0000, 0x0000, /* 0x60 */
42 0x0000, 0x0000, 0x0000, 0x0000, /* 0x68 */
43 0x0000, 0x0808, 0x0000, 0x0006, /* 0x70 */
44 0x0000, 0x0000, 0x574d, 0x4c05, /* 0x78 */
45};
46
47static const struct snd_kcontrol_new wm9705_snd_ac97_controls[] = {
48 SOC_DOUBLE("Master Playback Volume", AC97_MASTER, 8, 0, 31, 1),
49 SOC_SINGLE("Master Playback Switch", AC97_MASTER, 15, 1, 1),
50 SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
51 SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 1, 1),
52 SOC_DOUBLE("PCM Playback Volume", AC97_PCM, 8, 0, 31, 1),
53 SOC_SINGLE("PCM Playback Switch", AC97_PCM, 15, 1, 1),
54 SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1),
55 SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
56 SOC_SINGLE("PCBeep Playback Volume", AC97_PC_BEEP, 1, 15, 1),
57 SOC_SINGLE("Phone Playback Volume", AC97_PHONE, 0, 31, 1),
58 SOC_DOUBLE("Line Playback Volume", AC97_LINE, 8, 0, 31, 1),
59 SOC_DOUBLE("CD Playback Volume", AC97_CD, 8, 0, 31, 1),
60 SOC_SINGLE("Mic Playback Volume", AC97_MIC, 0, 31, 1),
61 SOC_SINGLE("Mic 20dB Boost Switch", AC97_MIC, 6, 1, 0),
927b0aea
IM
62 SOC_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0),
63 SOC_SINGLE("Capture Switch", AC97_REC_GAIN, 15, 1, 1),
2aceefef
IM
64};
65
66static const char *wm9705_mic[] = {"Mic 1", "Mic 2"};
67static const char *wm9705_rec_sel[] = {"Mic", "CD", "NC", "NC",
68 "Line", "Stereo Mix", "Mono Mix", "Phone"};
69
09981dcb
TI
70static SOC_ENUM_SINGLE_DECL(wm9705_enum_mic,
71 AC97_GENERAL_PURPOSE, 8, wm9705_mic);
72static SOC_ENUM_SINGLE_DECL(wm9705_enum_rec_l,
73 AC97_REC_SEL, 8, wm9705_rec_sel);
74static SOC_ENUM_SINGLE_DECL(wm9705_enum_rec_r,
75 AC97_REC_SEL, 0, wm9705_rec_sel);
2aceefef
IM
76
77/* Headphone Mixer */
78static const struct snd_kcontrol_new wm9705_hp_mixer_controls[] = {
79 SOC_DAPM_SINGLE("PCBeep Playback Switch", AC97_PC_BEEP, 15, 1, 1),
80 SOC_DAPM_SINGLE("CD Playback Switch", AC97_CD, 15, 1, 1),
81 SOC_DAPM_SINGLE("Mic Playback Switch", AC97_MIC, 15, 1, 1),
82 SOC_DAPM_SINGLE("Phone Playback Switch", AC97_PHONE, 15, 1, 1),
83 SOC_DAPM_SINGLE("Line Playback Switch", AC97_LINE, 15, 1, 1),
84};
85
86/* Mic source */
87static const struct snd_kcontrol_new wm9705_mic_src_controls =
88 SOC_DAPM_ENUM("Route", wm9705_enum_mic);
89
90/* Capture source */
91static const struct snd_kcontrol_new wm9705_capture_selectl_controls =
92 SOC_DAPM_ENUM("Route", wm9705_enum_rec_l);
93static const struct snd_kcontrol_new wm9705_capture_selectr_controls =
94 SOC_DAPM_ENUM("Route", wm9705_enum_rec_r);
95
96/* DAPM widgets */
97static const struct snd_soc_dapm_widget wm9705_dapm_widgets[] = {
98 SND_SOC_DAPM_MUX("Mic Source", SND_SOC_NOPM, 0, 0,
99 &wm9705_mic_src_controls),
100 SND_SOC_DAPM_MUX("Left Capture Source", SND_SOC_NOPM, 0, 0,
101 &wm9705_capture_selectl_controls),
102 SND_SOC_DAPM_MUX("Right Capture Source", SND_SOC_NOPM, 0, 0,
103 &wm9705_capture_selectr_controls),
104 SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback",
105 SND_SOC_NOPM, 0, 0),
106 SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback",
107 SND_SOC_NOPM, 0, 0),
108 SND_SOC_DAPM_MIXER_NAMED_CTL("HP Mixer", SND_SOC_NOPM, 0, 0,
109 &wm9705_hp_mixer_controls[0],
110 ARRAY_SIZE(wm9705_hp_mixer_controls)),
111 SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
112 SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture", SND_SOC_NOPM, 0, 0),
113 SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture", SND_SOC_NOPM, 0, 0),
114 SND_SOC_DAPM_PGA("Headphone PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
115 SND_SOC_DAPM_PGA("Speaker PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
116 SND_SOC_DAPM_PGA("Line PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
117 SND_SOC_DAPM_PGA("Line out PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
118 SND_SOC_DAPM_PGA("Mono PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
119 SND_SOC_DAPM_PGA("Phone PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
120 SND_SOC_DAPM_PGA("Mic PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
121 SND_SOC_DAPM_PGA("PCBEEP PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
122 SND_SOC_DAPM_PGA("CD PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
123 SND_SOC_DAPM_PGA("ADC PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
124 SND_SOC_DAPM_OUTPUT("HPOUTL"),
125 SND_SOC_DAPM_OUTPUT("HPOUTR"),
126 SND_SOC_DAPM_OUTPUT("LOUT"),
127 SND_SOC_DAPM_OUTPUT("ROUT"),
128 SND_SOC_DAPM_OUTPUT("MONOOUT"),
129 SND_SOC_DAPM_INPUT("PHONE"),
130 SND_SOC_DAPM_INPUT("LINEINL"),
131 SND_SOC_DAPM_INPUT("LINEINR"),
132 SND_SOC_DAPM_INPUT("CDINL"),
133 SND_SOC_DAPM_INPUT("CDINR"),
134 SND_SOC_DAPM_INPUT("PCBEEP"),
135 SND_SOC_DAPM_INPUT("MIC1"),
136 SND_SOC_DAPM_INPUT("MIC2"),
137};
138
139/* Audio map
140 * WM9705 has no switches to disable the route from the inputs to the HP mixer
141 * so in order to prevent active inputs from forcing the audio outputs to be
142 * constantly enabled, we use the mutes on those inputs to simulate such
143 * controls.
144 */
97a58d6e 145static const struct snd_soc_dapm_route wm9705_audio_map[] = {
2aceefef
IM
146 /* HP mixer */
147 {"HP Mixer", "PCBeep Playback Switch", "PCBEEP PGA"},
148 {"HP Mixer", "CD Playback Switch", "CD PGA"},
149 {"HP Mixer", "Mic Playback Switch", "Mic PGA"},
150 {"HP Mixer", "Phone Playback Switch", "Phone PGA"},
151 {"HP Mixer", "Line Playback Switch", "Line PGA"},
152 {"HP Mixer", NULL, "Left DAC"},
153 {"HP Mixer", NULL, "Right DAC"},
154
155 /* mono mixer */
156 {"Mono Mixer", NULL, "HP Mixer"},
157
158 /* outputs */
159 {"Headphone PGA", NULL, "HP Mixer"},
160 {"HPOUTL", NULL, "Headphone PGA"},
161 {"HPOUTR", NULL, "Headphone PGA"},
162 {"Line out PGA", NULL, "HP Mixer"},
163 {"LOUT", NULL, "Line out PGA"},
164 {"ROUT", NULL, "Line out PGA"},
165 {"Mono PGA", NULL, "Mono Mixer"},
166 {"MONOOUT", NULL, "Mono PGA"},
167
168 /* inputs */
169 {"CD PGA", NULL, "CDINL"},
170 {"CD PGA", NULL, "CDINR"},
171 {"Line PGA", NULL, "LINEINL"},
172 {"Line PGA", NULL, "LINEINR"},
173 {"Phone PGA", NULL, "PHONE"},
174 {"Mic Source", "Mic 1", "MIC1"},
175 {"Mic Source", "Mic 2", "MIC2"},
176 {"Mic PGA", NULL, "Mic Source"},
177 {"PCBEEP PGA", NULL, "PCBEEP"},
178
179 /* Left capture selector */
180 {"Left Capture Source", "Mic", "Mic Source"},
181 {"Left Capture Source", "CD", "CDINL"},
182 {"Left Capture Source", "Line", "LINEINL"},
183 {"Left Capture Source", "Stereo Mix", "HP Mixer"},
184 {"Left Capture Source", "Mono Mix", "HP Mixer"},
185 {"Left Capture Source", "Phone", "PHONE"},
186
187 /* Right capture source */
188 {"Right Capture Source", "Mic", "Mic Source"},
189 {"Right Capture Source", "CD", "CDINR"},
190 {"Right Capture Source", "Line", "LINEINR"},
191 {"Right Capture Source", "Stereo Mix", "HP Mixer"},
192 {"Right Capture Source", "Mono Mix", "HP Mixer"},
193 {"Right Capture Source", "Phone", "PHONE"},
194
195 {"ADC PGA", NULL, "Left Capture Source"},
196 {"ADC PGA", NULL, "Right Capture Source"},
197
198 /* ADC's */
199 {"Left ADC", NULL, "ADC PGA"},
200 {"Right ADC", NULL, "ADC PGA"},
201};
202
2aceefef
IM
203/* We use a register cache to enhance read performance. */
204static unsigned int ac97_read(struct snd_soc_codec *codec, unsigned int reg)
205{
358a8bb5 206 struct snd_ac97 *ac97 = snd_soc_codec_get_drvdata(codec);
2aceefef
IM
207 u16 *cache = codec->reg_cache;
208
209 switch (reg) {
210 case AC97_RESET:
211 case AC97_VENDOR_ID1:
212 case AC97_VENDOR_ID2:
358a8bb5 213 return soc_ac97_ops->read(ac97, reg);
2aceefef
IM
214 default:
215 reg = reg >> 1;
216
217 if (reg >= (ARRAY_SIZE(wm9705_reg)))
218 return -EIO;
219
220 return cache[reg];
221 }
222}
223
224static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
225 unsigned int val)
226{
358a8bb5 227 struct snd_ac97 *ac97 = snd_soc_codec_get_drvdata(codec);
2aceefef
IM
228 u16 *cache = codec->reg_cache;
229
358a8bb5 230 soc_ac97_ops->write(ac97, reg, val);
2aceefef
IM
231 reg = reg >> 1;
232 if (reg < (ARRAY_SIZE(wm9705_reg)))
233 cache[reg] = val;
234
235 return 0;
236}
237
238static int ac97_prepare(struct snd_pcm_substream *substream,
239 struct snd_soc_dai *dai)
240{
e6968a17 241 struct snd_soc_codec *codec = dai->codec;
2aceefef
IM
242 int reg;
243 u16 vra;
244
245 vra = ac97_read(codec, AC97_EXTENDED_STATUS);
246 ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
247
248 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
249 reg = AC97_PCM_FRONT_DAC_RATE;
250 else
251 reg = AC97_PCM_LR_ADC_RATE;
252
019ec505 253 return ac97_write(codec, reg, substream->runtime->rate);
2aceefef
IM
254}
255
256#define WM9705_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | \
257 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
258 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
259 SNDRV_PCM_RATE_48000)
260
85e7652d 261static const struct snd_soc_dai_ops wm9705_dai_ops = {
6335d055
EM
262 .prepare = ac97_prepare,
263};
264
f0fba2ad 265static struct snd_soc_dai_driver wm9705_dai[] = {
2aceefef 266 {
f0fba2ad 267 .name = "wm9705-hifi",
2aceefef
IM
268 .playback = {
269 .stream_name = "HiFi Playback",
270 .channels_min = 1,
271 .channels_max = 2,
272 .rates = WM9705_AC97_RATES,
33f503c9 273 .formats = SND_SOC_STD_AC97_FMTS,
2aceefef
IM
274 },
275 .capture = {
276 .stream_name = "HiFi Capture",
277 .channels_min = 1,
278 .channels_max = 2,
279 .rates = WM9705_AC97_RATES,
33f503c9 280 .formats = SND_SOC_STD_AC97_FMTS,
2aceefef 281 },
6335d055 282 .ops = &wm9705_dai_ops,
2aceefef
IM
283 },
284 {
f0fba2ad 285 .name = "wm9705-aux",
2aceefef
IM
286 .playback = {
287 .stream_name = "Aux Playback",
288 .channels_min = 1,
289 .channels_max = 1,
290 .rates = WM9705_AC97_RATES,
291 .formats = SNDRV_PCM_FMTBIT_S16_LE,
292 },
293 }
294};
2aceefef
IM
295
296static int wm9705_reset(struct snd_soc_codec *codec)
297{
358a8bb5
LPC
298 struct snd_ac97 *ac97 = snd_soc_codec_get_drvdata(codec);
299
b047e1cc 300 if (soc_ac97_ops->reset) {
358a8bb5 301 soc_ac97_ops->reset(ac97);
2aceefef
IM
302 if (ac97_read(codec, 0) == wm9705_reg[0])
303 return 0; /* Success */
304 }
305
9cf766f6
LPC
306 dev_err(codec->dev, "Failed to reset: AC97 link error\n");
307
2aceefef
IM
308 return -EIO;
309}
310
0a11b168 311#ifdef CONFIG_PM
84b315ee 312static int wm9705_soc_suspend(struct snd_soc_codec *codec)
0a11b168 313{
358a8bb5
LPC
314 struct snd_ac97 *ac97 = snd_soc_codec_get_drvdata(codec);
315
316 soc_ac97_ops->write(ac97, AC97_POWERDOWN, 0xffff);
0a11b168
MB
317
318 return 0;
319}
320
f0fba2ad 321static int wm9705_soc_resume(struct snd_soc_codec *codec)
0a11b168 322{
358a8bb5 323 struct snd_ac97 *ac97 = snd_soc_codec_get_drvdata(codec);
0a11b168
MB
324 int i, ret;
325 u16 *cache = codec->reg_cache;
326
327 ret = wm9705_reset(codec);
9cf766f6 328 if (ret < 0)
0a11b168 329 return ret;
0a11b168
MB
330
331 for (i = 2; i < ARRAY_SIZE(wm9705_reg) << 1; i += 2) {
358a8bb5 332 soc_ac97_ops->write(ac97, i, cache[i>>1]);
0a11b168
MB
333 }
334
335 return 0;
336}
337#else
338#define wm9705_soc_suspend NULL
339#define wm9705_soc_resume NULL
340#endif
341
f0fba2ad 342static int wm9705_soc_probe(struct snd_soc_codec *codec)
2aceefef 343{
358a8bb5 344 struct snd_ac97 *ac97;
2aceefef
IM
345 int ret = 0;
346
f8d71be5 347 ac97 = snd_soc_alloc_ac97_codec(codec);
358a8bb5
LPC
348 if (IS_ERR(ac97)) {
349 ret = PTR_ERR(ac97);
9cf766f6 350 dev_err(codec->dev, "Failed to register AC97 codec\n");
f0fba2ad 351 return ret;
2aceefef
IM
352 }
353
2aceefef
IM
354 ret = wm9705_reset(codec);
355 if (ret)
f8d71be5
LPC
356 goto err_put_device;
357
358 ret = device_add(&ac97->dev);
359 if (ret)
360 goto err_put_device;
361
362 snd_soc_codec_set_drvdata(codec, ac97);
2aceefef 363
2aceefef
IM
364 return 0;
365
f8d71be5
LPC
366err_put_device:
367 put_device(&ac97->dev);
2aceefef
IM
368 return ret;
369}
370
f0fba2ad 371static int wm9705_soc_remove(struct snd_soc_codec *codec)
2aceefef 372{
358a8bb5
LPC
373 struct snd_ac97 *ac97 = snd_soc_codec_get_drvdata(codec);
374
375 snd_soc_free_ac97_codec(ac97);
2aceefef
IM
376 return 0;
377}
378
f0fba2ad 379static struct snd_soc_codec_driver soc_codec_dev_wm9705 = {
2aceefef
IM
380 .probe = wm9705_soc_probe,
381 .remove = wm9705_soc_remove,
0a11b168
MB
382 .suspend = wm9705_soc_suspend,
383 .resume = wm9705_soc_resume,
f0fba2ad
LG
384 .read = ac97_read,
385 .write = ac97_write,
e5eec34c 386 .reg_cache_size = ARRAY_SIZE(wm9705_reg),
f0fba2ad
LG
387 .reg_word_size = sizeof(u16),
388 .reg_cache_step = 2,
389 .reg_cache_default = wm9705_reg,
d7cabb08
LPC
390
391 .controls = wm9705_snd_ac97_controls,
392 .num_controls = ARRAY_SIZE(wm9705_snd_ac97_controls),
97a58d6e
LG
393 .dapm_widgets = wm9705_dapm_widgets,
394 .num_dapm_widgets = ARRAY_SIZE(wm9705_dapm_widgets),
395 .dapm_routes = wm9705_audio_map,
396 .num_dapm_routes = ARRAY_SIZE(wm9705_audio_map),
f0fba2ad
LG
397};
398
7a79e94e 399static int wm9705_probe(struct platform_device *pdev)
f0fba2ad
LG
400{
401 return snd_soc_register_codec(&pdev->dev,
402 &soc_codec_dev_wm9705, wm9705_dai, ARRAY_SIZE(wm9705_dai));
403}
404
7a79e94e 405static int wm9705_remove(struct platform_device *pdev)
f0fba2ad
LG
406{
407 snd_soc_unregister_codec(&pdev->dev);
408 return 0;
409}
410
411static struct platform_driver wm9705_codec_driver = {
412 .driver = {
413 .name = "wm9705-codec",
f0fba2ad
LG
414 },
415
416 .probe = wm9705_probe,
7a79e94e 417 .remove = wm9705_remove,
2aceefef 418};
f0fba2ad 419
5bbcc3c0 420module_platform_driver(wm9705_codec_driver);
2aceefef
IM
421
422MODULE_DESCRIPTION("ASoC WM9705 driver");
423MODULE_AUTHOR("Ian Molton");
424MODULE_LICENSE("GPL v2");
This page took 0.270674 seconds and 5 git commands to generate.