aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sound/soc/codecs/Kconfig4
-rw-r--r--sound/soc/codecs/Makefile2
-rw-r--r--sound/soc/codecs/wm9713.c1289
-rw-r--r--sound/soc/codecs/wm9713.h53
4 files changed, 1348 insertions, 0 deletions
diff --git a/sound/soc/codecs/Kconfig b/sound/soc/codecs/Kconfig
index 898a7d363284..3903ab7dfa4a 100644
--- a/sound/soc/codecs/Kconfig
+++ b/sound/soc/codecs/Kconfig
@@ -18,6 +18,10 @@ config SND_SOC_WM9712
18 tristate 18 tristate
19 depends on SND_SOC 19 depends on SND_SOC
20 20
21config SND_SOC_WM9713
22 tristate
23 depends on SND_SOC
24
21# Cirrus Logic CS4270 Codec 25# Cirrus Logic CS4270 Codec
22config SND_SOC_CS4270 26config SND_SOC_CS4270
23 tristate 27 tristate
diff --git a/sound/soc/codecs/Makefile b/sound/soc/codecs/Makefile
index c6e5338c2666..4e1314c9d3ec 100644
--- a/sound/soc/codecs/Makefile
+++ b/sound/soc/codecs/Makefile
@@ -3,6 +3,7 @@ snd-soc-wm8731-objs := wm8731.o
3snd-soc-wm8750-objs := wm8750.o 3snd-soc-wm8750-objs := wm8750.o
4snd-soc-wm8753-objs := wm8753.o 4snd-soc-wm8753-objs := wm8753.o
5snd-soc-wm9712-objs := wm9712.o 5snd-soc-wm9712-objs := wm9712.o
6snd-soc-wm9713-objs := wm9713.o
6snd-soc-cs4270-objs := cs4270.o 7snd-soc-cs4270-objs := cs4270.o
7snd-soc-tlv320aic3x-objs := tlv320aic3x.o 8snd-soc-tlv320aic3x-objs := tlv320aic3x.o
8 9
@@ -11,5 +12,6 @@ obj-$(CONFIG_SND_SOC_WM8731) += snd-soc-wm8731.o
11obj-$(CONFIG_SND_SOC_WM8750) += snd-soc-wm8750.o 12obj-$(CONFIG_SND_SOC_WM8750) += snd-soc-wm8750.o
12obj-$(CONFIG_SND_SOC_WM8753) += snd-soc-wm8753.o 13obj-$(CONFIG_SND_SOC_WM8753) += snd-soc-wm8753.o
13obj-$(CONFIG_SND_SOC_WM9712) += snd-soc-wm9712.o 14obj-$(CONFIG_SND_SOC_WM9712) += snd-soc-wm9712.o
15obj-$(CONFIG_SND_SOC_WM9713) += snd-soc-wm9713.o
14obj-$(CONFIG_SND_SOC_CS4270) += snd-soc-cs4270.o 16obj-$(CONFIG_SND_SOC_CS4270) += snd-soc-cs4270.o
15obj-$(CONFIG_SND_SOC_TLV320AIC3X) += snd-soc-tlv320aic3x.o 17obj-$(CONFIG_SND_SOC_TLV320AIC3X) += snd-soc-tlv320aic3x.o
diff --git a/sound/soc/codecs/wm9713.c b/sound/soc/codecs/wm9713.c
new file mode 100644
index 000000000000..c3d0afdc0997
--- /dev/null
+++ b/sound/soc/codecs/wm9713.c
@@ -0,0 +1,1289 @@
1/*
2 * wm9713.c -- ALSA Soc WM9713 codec support
3 *
4 * Copyright 2006 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood
6 * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
7 *
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.
12 *
13 * Revision history
14 * 4th Feb 2006 Initial version.
15 *
16 * Features:-
17 *
18 * o Support for AC97 Codec, Voice DAC and Aux DAC
19 * o Support for DAPM
20 */
21
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/device.h>
25#include <sound/core.h>
26#include <sound/pcm.h>
27#include <sound/ac97_codec.h>
28#include <sound/initval.h>
29#include <sound/pcm_params.h>
30#include <sound/soc.h>
31#include <sound/soc-dapm.h>
32
33#include "wm9713.h"
34
35#define WM9713_VERSION "0.15"
36
37struct wm9713_priv {
38 u32 pll_in; /* PLL input frequency */
39 u32 pll_out; /* PLL output frequency */
40};
41
42static unsigned int ac97_read(struct snd_soc_codec *codec,
43 unsigned int reg);
44static int ac97_write(struct snd_soc_codec *codec,
45 unsigned int reg, unsigned int val);
46
47/*
48 * WM9713 register cache
49 * Reg 0x3c bit 15 is used by touch driver.
50 */
51static const u16 wm9713_reg[] = {
52 0x6174, 0x8080, 0x8080, 0x8080,
53 0xc880, 0xe808, 0xe808, 0x0808,
54 0x00da, 0x8000, 0xd600, 0xaaa0,
55 0xaaa0, 0xaaa0, 0x0000, 0x0000,
56 0x0f0f, 0x0040, 0x0000, 0x7f00,
57 0x0405, 0x0410, 0xbb80, 0xbb80,
58 0x0000, 0xbb80, 0x0000, 0x4523,
59 0x0000, 0x2000, 0x7eff, 0xffff,
60 0x0000, 0x0000, 0x0080, 0x0000,
61 0x0000, 0x0000, 0xfffe, 0xffff,
62 0x0000, 0x0000, 0x0000, 0xfffe,
63 0x4000, 0x0000, 0x0000, 0x0000,
64 0xb032, 0x3e00, 0x0000, 0x0000,
65 0x0000, 0x0000, 0x0000, 0x0000,
66 0x0000, 0x0000, 0x0000, 0x0006,
67 0x0001, 0x0000, 0x574d, 0x4c13,
68 0x0000, 0x0000, 0x0000
69};
70
71/* virtual HP mixers regs */
72#define HPL_MIXER 0x80
73#define HPR_MIXER 0x82
74#define MICB_MUX 0x82
75
76static const char *wm9713_mic_mixer[] = {"Stereo", "Mic 1", "Mic 2", "Mute"};
77static const char *wm9713_rec_mux[] = {"Stereo", "Left", "Right", "Mute"};
78static const char *wm9713_rec_src[] =
79 {"Mic 1", "Mic 2", "Line", "Mono In", "Headphone", "Speaker",
80 "Mono Out", "Zh"};
81static const char *wm9713_rec_gain[] = {"+1.5dB Steps", "+0.75dB Steps"};
82static const char *wm9713_alc_select[] = {"None", "Left", "Right", "Stereo"};
83static const char *wm9713_mono_pga[] = {"Vmid", "Zh", "Mono", "Inv",
84 "Mono Vmid", "Inv Vmid"};
85static const char *wm9713_spk_pga[] =
86 {"Vmid", "Zh", "Headphone", "Speaker", "Inv", "Headphone Vmid",
87 "Speaker Vmid", "Inv Vmid"};
88static const char *wm9713_hp_pga[] = {"Vmid", "Zh", "Headphone",
89 "Headphone Vmid"};
90static const char *wm9713_out3_pga[] = {"Vmid", "Zh", "Inv 1", "Inv 1 Vmid"};
91static const char *wm9713_out4_pga[] = {"Vmid", "Zh", "Inv 2", "Inv 2 Vmid"};
92static const char *wm9713_dac_inv[] =
93 {"Off", "Mono", "Speaker", "Left Headphone", "Right Headphone",
94 "Headphone Mono", "NC", "Vmid"};
95static const char *wm9713_bass[] = {"Linear Control", "Adaptive Boost"};
96static const char *wm9713_ng_type[] = {"Constant Gain", "Mute"};
97static const char *wm9713_mic_select[] = {"Mic 1", "Mic 2 A", "Mic 2 B"};
98static const char *wm9713_micb_select[] = {"MPB", "MPA"};
99
100static const struct soc_enum wm9713_enum[] = {
101SOC_ENUM_SINGLE(AC97_LINE, 3, 4, wm9713_mic_mixer), /* record mic mixer 0 */
102SOC_ENUM_SINGLE(AC97_VIDEO, 14, 4, wm9713_rec_mux), /* record mux hp 1 */
103SOC_ENUM_SINGLE(AC97_VIDEO, 9, 4, wm9713_rec_mux), /* record mux mono 2 */
104SOC_ENUM_SINGLE(AC97_VIDEO, 3, 8, wm9713_rec_src), /* record mux left 3 */
105SOC_ENUM_SINGLE(AC97_VIDEO, 0, 8, wm9713_rec_src), /* record mux right 4*/
106SOC_ENUM_DOUBLE(AC97_CD, 14, 6, 2, wm9713_rec_gain), /* record step size 5 */
107SOC_ENUM_SINGLE(AC97_PCI_SVID, 14, 4, wm9713_alc_select), /* alc source select 6*/
108SOC_ENUM_SINGLE(AC97_REC_GAIN, 14, 4, wm9713_mono_pga), /* mono input select 7 */
109SOC_ENUM_SINGLE(AC97_REC_GAIN, 11, 8, wm9713_spk_pga), /* speaker left input select 8 */
110SOC_ENUM_SINGLE(AC97_REC_GAIN, 8, 8, wm9713_spk_pga), /* speaker right input select 9 */
111SOC_ENUM_SINGLE(AC97_REC_GAIN, 6, 3, wm9713_hp_pga), /* headphone left input 10 */
112SOC_ENUM_SINGLE(AC97_REC_GAIN, 4, 3, wm9713_hp_pga), /* headphone right input 11 */
113SOC_ENUM_SINGLE(AC97_REC_GAIN, 2, 4, wm9713_out3_pga), /* out 3 source 12 */
114SOC_ENUM_SINGLE(AC97_REC_GAIN, 0, 4, wm9713_out4_pga), /* out 4 source 13 */
115SOC_ENUM_SINGLE(AC97_REC_GAIN_MIC, 13, 8, wm9713_dac_inv), /* dac invert 1 14 */
116SOC_ENUM_SINGLE(AC97_REC_GAIN_MIC, 10, 8, wm9713_dac_inv), /* dac invert 2 15 */
117SOC_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 15, 2, wm9713_bass), /* bass control 16 */
118SOC_ENUM_SINGLE(AC97_PCI_SVID, 5, 2, wm9713_ng_type), /* noise gate type 17 */
119SOC_ENUM_SINGLE(AC97_3D_CONTROL, 12, 3, wm9713_mic_select), /* mic selection 18 */
120SOC_ENUM_SINGLE(MICB_MUX, 0, 2, wm9713_micb_select), /* mic selection 19 */
121};
122
123static const struct snd_kcontrol_new wm9713_snd_ac97_controls[] = {
124SOC_DOUBLE("Speaker Playback Volume", AC97_MASTER, 8, 0, 31, 1),
125SOC_DOUBLE("Speaker Playback Switch", AC97_MASTER, 15, 7, 1, 1),
126SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
127SOC_DOUBLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 7, 1, 1),
128SOC_DOUBLE("Line In Volume", AC97_PC_BEEP, 8, 0, 31, 1),
129SOC_DOUBLE("PCM Playback Volume", AC97_PHONE, 8, 0, 31, 1),
130SOC_SINGLE("Mic 1 Volume", AC97_MIC, 8, 31, 1),
131SOC_SINGLE("Mic 2 Volume", AC97_MIC, 0, 31, 1),
132
133SOC_SINGLE("Mic Boost (+20dB) Switch", AC97_LINE, 5, 1, 0),
134SOC_SINGLE("Mic Headphone Mixer Volume", AC97_LINE, 0, 7, 1),
135
136SOC_SINGLE("Capture Switch", AC97_CD, 15, 1, 1),
137SOC_ENUM("Capture Volume Steps", wm9713_enum[5]),
138SOC_DOUBLE("Capture Volume", AC97_CD, 8, 0, 31, 0),
139SOC_SINGLE("Capture ZC Switch", AC97_CD, 7, 1, 0),
140
141SOC_SINGLE("Capture to Headphone Volume", AC97_VIDEO, 11, 7, 1),
142SOC_SINGLE("Capture to Mono Boost (+20dB) Switch", AC97_VIDEO, 8, 1, 0),
143SOC_SINGLE("Capture ADC Boost (+20dB) Switch", AC97_VIDEO, 6, 1, 0),
144
145SOC_SINGLE("ALC Target Volume", AC97_CODEC_CLASS_REV, 12, 15, 0),
146SOC_SINGLE("ALC Hold Time", AC97_CODEC_CLASS_REV, 8, 15, 0),
147SOC_SINGLE("ALC Decay Time ", AC97_CODEC_CLASS_REV, 4, 15, 0),
148SOC_SINGLE("ALC Attack Time", AC97_CODEC_CLASS_REV, 0, 15, 0),
149SOC_ENUM("ALC Function", wm9713_enum[6]),
150SOC_SINGLE("ALC Max Volume", AC97_PCI_SVID, 11, 7, 0),
151SOC_SINGLE("ALC ZC Timeout", AC97_PCI_SVID, 9, 3, 0),
152SOC_SINGLE("ALC ZC Switch", AC97_PCI_SVID, 8, 1, 0),
153SOC_SINGLE("ALC NG Switch", AC97_PCI_SVID, 7, 1, 0),
154SOC_ENUM("ALC NG Type", wm9713_enum[17]),
155SOC_SINGLE("ALC NG Threshold", AC97_PCI_SVID, 0, 31, 0),
156
157SOC_DOUBLE("Speaker Playback ZC Switch", AC97_MASTER, 14, 6, 1, 0),
158SOC_DOUBLE("Headphone Playback ZC Switch", AC97_HEADPHONE, 14, 6, 1, 0),
159
160SOC_SINGLE("Out4 Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
161SOC_SINGLE("Out4 Playback ZC Switch", AC97_MASTER_MONO, 14, 1, 0),
162SOC_SINGLE("Out4 Playback Volume", AC97_MASTER_MONO, 8, 63, 1),
163
164SOC_SINGLE("Out3 Playback Switch", AC97_MASTER_MONO, 7, 1, 1),
165SOC_SINGLE("Out3 Playback ZC Switch", AC97_MASTER_MONO, 6, 1, 0),
166SOC_SINGLE("Out3 Playback Volume", AC97_MASTER_MONO, 0, 63, 1),
167
168SOC_SINGLE("Mono Capture Volume", AC97_MASTER_TONE, 8, 31, 1),
169SOC_SINGLE("Mono Playback Switch", AC97_MASTER_TONE, 7, 1, 1),
170SOC_SINGLE("Mono Playback ZC Switch", AC97_MASTER_TONE, 6, 1, 0),
171SOC_SINGLE("Mono Playback Volume", AC97_MASTER_TONE, 0, 31, 1),
172
173SOC_SINGLE("PC Beep Playback Headphone Volume", AC97_AUX, 12, 7, 1),
174SOC_SINGLE("PC Beep Playback Speaker Volume", AC97_AUX, 8, 7, 1),
175SOC_SINGLE("PC Beep Playback Mono Volume", AC97_AUX, 4, 7, 1),
176
177SOC_SINGLE("Voice Playback Headphone Volume", AC97_PCM, 12, 7, 1),
178SOC_SINGLE("Voice Playback Master Volume", AC97_PCM, 8, 7, 1),
179SOC_SINGLE("Voice Playback Mono Volume", AC97_PCM, 4, 7, 1),
180
181SOC_SINGLE("Aux Playback Headphone Volume", AC97_REC_SEL, 12, 7, 1),
182SOC_SINGLE("Aux Playback Master Volume", AC97_REC_SEL, 8, 7, 1),
183SOC_SINGLE("Aux Playback Mono Volume", AC97_REC_SEL, 4, 7, 1),
184
185SOC_ENUM("Bass Control", wm9713_enum[16]),
186SOC_SINGLE("Bass Cut-off Switch", AC97_GENERAL_PURPOSE, 12, 1, 1),
187SOC_SINGLE("Tone Cut-off Switch", AC97_GENERAL_PURPOSE, 4, 1, 1),
188SOC_SINGLE("Playback Attenuate (-6dB) Switch", AC97_GENERAL_PURPOSE, 6, 1, 0),
189SOC_SINGLE("Bass Volume", AC97_GENERAL_PURPOSE, 8, 15, 1),
190SOC_SINGLE("Tone Volume", AC97_GENERAL_PURPOSE, 0, 15, 1),
191
192SOC_SINGLE("3D Upper Cut-off Switch", AC97_REC_GAIN_MIC, 5, 1, 0),
193SOC_SINGLE("3D Lower Cut-off Switch", AC97_REC_GAIN_MIC, 4, 1, 0),
194SOC_SINGLE("3D Depth", AC97_REC_GAIN_MIC, 0, 15, 1),
195};
196
197/* add non dapm controls */
198static int wm9713_add_controls(struct snd_soc_codec *codec)
199{
200 int err, i;
201
202 for (i = 0; i < ARRAY_SIZE(wm9713_snd_ac97_controls); i++) {
203 err = snd_ctl_add(codec->card,
204 snd_soc_cnew(&wm9713_snd_ac97_controls[i],
205 codec, NULL));
206 if (err < 0)
207 return err;
208 }
209 return 0;
210}
211
212/* We have to create a fake left and right HP mixers because
213 * the codec only has a single control that is shared by both channels.
214 * This makes it impossible to determine the audio path using the current
215 * register map, thus we add a new (virtual) register to help determine the
216 * audio route within the device.
217 */
218static int mixer_event(struct snd_soc_dapm_widget *w,
219 struct snd_kcontrol *kcontrol, int event)
220{
221 u16 l, r, beep, tone, phone, rec, pcm, aux;
222
223 l = ac97_read(w->codec, HPL_MIXER);
224 r = ac97_read(w->codec, HPR_MIXER);
225 beep = ac97_read(w->codec, AC97_PC_BEEP);
226 tone = ac97_read(w->codec, AC97_MASTER_TONE);
227 phone = ac97_read(w->codec, AC97_PHONE);
228 rec = ac97_read(w->codec, AC97_REC_SEL);
229 pcm = ac97_read(w->codec, AC97_PCM);
230 aux = ac97_read(w->codec, AC97_AUX);
231
232 if (event & SND_SOC_DAPM_PRE_REG)
233 return 0;
234 if ((l & 0x1) || (r & 0x1))
235 ac97_write(w->codec, AC97_PC_BEEP, beep & 0x7fff);
236 else
237 ac97_write(w->codec, AC97_PC_BEEP, beep | 0x8000);
238
239 if ((l & 0x2) || (r & 0x2))
240 ac97_write(w->codec, AC97_MASTER_TONE, tone & 0x7fff);
241 else
242 ac97_write(w->codec, AC97_MASTER_TONE, tone | 0x8000);
243
244 if ((l & 0x4) || (r & 0x4))
245 ac97_write(w->codec, AC97_PHONE, phone & 0x7fff);
246 else
247 ac97_write(w->codec, AC97_PHONE, phone | 0x8000);
248
249 if ((l & 0x8) || (r & 0x8))
250 ac97_write(w->codec, AC97_REC_SEL, rec & 0x7fff);
251 else
252 ac97_write(w->codec, AC97_REC_SEL, rec | 0x8000);
253
254 if ((l & 0x10) || (r & 0x10))
255 ac97_write(w->codec, AC97_PCM, pcm & 0x7fff);
256 else
257 ac97_write(w->codec, AC97_PCM, pcm | 0x8000);
258
259 if ((l & 0x20) || (r & 0x20))
260 ac97_write(w->codec, AC97_AUX, aux & 0x7fff);
261 else
262 ac97_write(w->codec, AC97_AUX, aux | 0x8000);
263
264 return 0;
265}
266
267/* Left Headphone Mixers */
268static const struct snd_kcontrol_new wm9713_hpl_mixer_controls[] = {
269SOC_DAPM_SINGLE("PC Beep Playback Switch", HPL_MIXER, 5, 1, 0),
270SOC_DAPM_SINGLE("Voice Playback Switch", HPL_MIXER, 4, 1, 0),
271SOC_DAPM_SINGLE("Aux Playback Switch", HPL_MIXER, 3, 1, 0),
272SOC_DAPM_SINGLE("PCM Playback Switch", HPL_MIXER, 2, 1, 0),
273SOC_DAPM_SINGLE("MonoIn Playback Switch", HPL_MIXER, 1, 1, 0),
274SOC_DAPM_SINGLE("Bypass Playback Switch", HPL_MIXER, 0, 1, 0),
275};
276
277/* Right Headphone Mixers */
278static const struct snd_kcontrol_new wm9713_hpr_mixer_controls[] = {
279SOC_DAPM_SINGLE("PC Beep Playback Switch", HPR_MIXER, 5, 1, 0),
280SOC_DAPM_SINGLE("Voice Playback Switch", HPR_MIXER, 4, 1, 0),
281SOC_DAPM_SINGLE("Aux Playback Switch", HPR_MIXER, 3, 1, 0),
282SOC_DAPM_SINGLE("PCM Playback Switch", HPR_MIXER, 2, 1, 0),
283SOC_DAPM_SINGLE("MonoIn Playback Switch", HPR_MIXER, 1, 1, 0),
284SOC_DAPM_SINGLE("Bypass Playback Switch", HPR_MIXER, 0, 1, 0),
285};
286
287/* headphone capture mux */
288static const struct snd_kcontrol_new wm9713_hp_rec_mux_controls =
289SOC_DAPM_ENUM("Route", wm9713_enum[1]);
290
291/* headphone mic mux */
292static const struct snd_kcontrol_new wm9713_hp_mic_mux_controls =
293SOC_DAPM_ENUM("Route", wm9713_enum[0]);
294
295/* Speaker Mixer */
296static const struct snd_kcontrol_new wm9713_speaker_mixer_controls[] = {
297SOC_DAPM_SINGLE("PC Beep Playback Switch", AC97_AUX, 11, 1, 1),
298SOC_DAPM_SINGLE("Voice Playback Switch", AC97_PCM, 11, 1, 1),
299SOC_DAPM_SINGLE("Aux Playback Switch", AC97_REC_SEL, 11, 1, 1),
300SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PHONE, 14, 1, 1),
301SOC_DAPM_SINGLE("MonoIn Playback Switch", AC97_MASTER_TONE, 14, 1, 1),
302SOC_DAPM_SINGLE("Bypass Playback Switch", AC97_PC_BEEP, 14, 1, 1),
303};
304
305/* Mono Mixer */
306static const struct snd_kcontrol_new wm9713_mono_mixer_controls[] = {
307SOC_DAPM_SINGLE("PC Beep Playback Switch", AC97_AUX, 7, 1, 1),
308SOC_DAPM_SINGLE("Voice Playback Switch", AC97_PCM, 7, 1, 1),
309SOC_DAPM_SINGLE("Aux Playback Switch", AC97_REC_SEL, 7, 1, 1),
310SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PHONE, 13, 1, 1),
311SOC_DAPM_SINGLE("MonoIn Playback Switch", AC97_MASTER_TONE, 13, 1, 1),
312SOC_DAPM_SINGLE("Bypass Playback Switch", AC97_PC_BEEP, 13, 1, 1),
313SOC_DAPM_SINGLE("Mic 1 Sidetone Switch", AC97_LINE, 7, 1, 1),
314SOC_DAPM_SINGLE("Mic 2 Sidetone Switch", AC97_LINE, 6, 1, 1),
315};
316
317/* mono mic mux */
318static const struct snd_kcontrol_new wm9713_mono_mic_mux_controls =
319SOC_DAPM_ENUM("Route", wm9713_enum[2]);
320
321/* mono output mux */
322static const struct snd_kcontrol_new wm9713_mono_mux_controls =
323SOC_DAPM_ENUM("Route", wm9713_enum[7]);
324
325/* speaker left output mux */
326static const struct snd_kcontrol_new wm9713_hp_spkl_mux_controls =
327SOC_DAPM_ENUM("Route", wm9713_enum[8]);
328
329/* speaker right output mux */
330static const struct snd_kcontrol_new wm9713_hp_spkr_mux_controls =
331SOC_DAPM_ENUM("Route", wm9713_enum[9]);
332
333/* headphone left output mux */
334static const struct snd_kcontrol_new wm9713_hpl_out_mux_controls =
335SOC_DAPM_ENUM("Route", wm9713_enum[10]);
336
337/* headphone right output mux */
338static const struct snd_kcontrol_new wm9713_hpr_out_mux_controls =
339SOC_DAPM_ENUM("Route", wm9713_enum[11]);
340
341/* Out3 mux */
342static const struct snd_kcontrol_new wm9713_out3_mux_controls =
343SOC_DAPM_ENUM("Route", wm9713_enum[12]);
344
345/* Out4 mux */
346static const struct snd_kcontrol_new wm9713_out4_mux_controls =
347SOC_DAPM_ENUM("Route", wm9713_enum[13]);
348
349/* DAC inv mux 1 */
350static const struct snd_kcontrol_new wm9713_dac_inv1_mux_controls =
351SOC_DAPM_ENUM("Route", wm9713_enum[14]);
352
353/* DAC inv mux 2 */
354static const struct snd_kcontrol_new wm9713_dac_inv2_mux_controls =
355SOC_DAPM_ENUM("Route", wm9713_enum[15]);
356
357/* Capture source left */
358static const struct snd_kcontrol_new wm9713_rec_srcl_mux_controls =
359SOC_DAPM_ENUM("Route", wm9713_enum[3]);
360
361/* Capture source right */
362static const struct snd_kcontrol_new wm9713_rec_srcr_mux_controls =
363SOC_DAPM_ENUM("Route", wm9713_enum[4]);
364
365/* mic source */
366static const struct snd_kcontrol_new wm9713_mic_sel_mux_controls =
367SOC_DAPM_ENUM("Route", wm9713_enum[18]);
368
369/* mic source B virtual control */
370static const struct snd_kcontrol_new wm9713_micb_sel_mux_controls =
371SOC_DAPM_ENUM("Route", wm9713_enum[19]);
372
373static const struct snd_soc_dapm_widget wm9713_dapm_widgets[] = {
374SND_SOC_DAPM_MUX("Capture Headphone Mux", SND_SOC_NOPM, 0, 0,
375 &wm9713_hp_rec_mux_controls),
376SND_SOC_DAPM_MUX("Sidetone Mux", SND_SOC_NOPM, 0, 0,
377 &wm9713_hp_mic_mux_controls),
378SND_SOC_DAPM_MUX("Capture Mono Mux", SND_SOC_NOPM, 0, 0,
379 &wm9713_mono_mic_mux_controls),
380SND_SOC_DAPM_MUX("Mono Out Mux", SND_SOC_NOPM, 0, 0,
381 &wm9713_mono_mux_controls),
382SND_SOC_DAPM_MUX("Left Speaker Out Mux", SND_SOC_NOPM, 0, 0,
383 &wm9713_hp_spkl_mux_controls),
384SND_SOC_DAPM_MUX("Right Speaker Out Mux", SND_SOC_NOPM, 0, 0,
385 &wm9713_hp_spkr_mux_controls),
386SND_SOC_DAPM_MUX("Left Headphone Out Mux", SND_SOC_NOPM, 0, 0,
387 &wm9713_hpl_out_mux_controls),
388SND_SOC_DAPM_MUX("Right Headphone Out Mux", SND_SOC_NOPM, 0, 0,
389 &wm9713_hpr_out_mux_controls),
390SND_SOC_DAPM_MUX("Out 3 Mux", SND_SOC_NOPM, 0, 0,
391 &wm9713_out3_mux_controls),
392SND_SOC_DAPM_MUX("Out 4 Mux", SND_SOC_NOPM, 0, 0,
393 &wm9713_out4_mux_controls),
394SND_SOC_DAPM_MUX("DAC Inv Mux 1", SND_SOC_NOPM, 0, 0,
395 &wm9713_dac_inv1_mux_controls),
396SND_SOC_DAPM_MUX("DAC Inv Mux 2", SND_SOC_NOPM, 0, 0,
397 &wm9713_dac_inv2_mux_controls),
398SND_SOC_DAPM_MUX("Left Capture Source", SND_SOC_NOPM, 0, 0,
399 &wm9713_rec_srcl_mux_controls),
400SND_SOC_DAPM_MUX("Right Capture Source", SND_SOC_NOPM, 0, 0,
401 &wm9713_rec_srcr_mux_controls),
402SND_SOC_DAPM_MUX("Mic A Source", SND_SOC_NOPM, 0, 0,
403 &wm9713_mic_sel_mux_controls),
404SND_SOC_DAPM_MUX("Mic B Source", SND_SOC_NOPM, 0, 0,
405 &wm9713_micb_sel_mux_controls),
406SND_SOC_DAPM_MIXER_E("Left HP Mixer", AC97_EXTENDED_MID, 3, 1,
407 &wm9713_hpl_mixer_controls[0], ARRAY_SIZE(wm9713_hpl_mixer_controls),
408 mixer_event, SND_SOC_DAPM_POST_REG),
409SND_SOC_DAPM_MIXER_E("Right HP Mixer", AC97_EXTENDED_MID, 2, 1,
410 &wm9713_hpr_mixer_controls[0], ARRAY_SIZE(wm9713_hpr_mixer_controls),
411 mixer_event, SND_SOC_DAPM_POST_REG),
412SND_SOC_DAPM_MIXER("Mono Mixer", AC97_EXTENDED_MID, 0, 1,
413 &wm9713_mono_mixer_controls[0], ARRAY_SIZE(wm9713_mono_mixer_controls)),
414SND_SOC_DAPM_MIXER("Speaker Mixer", AC97_EXTENDED_MID, 1, 1,
415 &wm9713_speaker_mixer_controls[0],
416 ARRAY_SIZE(wm9713_speaker_mixer_controls)),
417SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", AC97_EXTENDED_MID, 7, 1),
418SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", AC97_EXTENDED_MID, 6, 1),
419SND_SOC_DAPM_MIXER("AC97 Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
420SND_SOC_DAPM_MIXER("HP Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
421SND_SOC_DAPM_MIXER("Line Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
422SND_SOC_DAPM_MIXER("Capture Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
423SND_SOC_DAPM_DAC("Voice DAC", "Voice Playback", AC97_EXTENDED_MID, 12, 1),
424SND_SOC_DAPM_DAC("Aux DAC", "Aux Playback", AC97_EXTENDED_MID, 11, 1),
425SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture", AC97_EXTENDED_MID, 5, 1),
426SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture", AC97_EXTENDED_MID, 4, 1),
427SND_SOC_DAPM_PGA("Left Headphone", AC97_EXTENDED_MSTATUS, 10, 1, NULL, 0),
428SND_SOC_DAPM_PGA("Right Headphone", AC97_EXTENDED_MSTATUS, 9, 1, NULL, 0),
429SND_SOC_DAPM_PGA("Left Speaker", AC97_EXTENDED_MSTATUS, 8, 1, NULL, 0),
430SND_SOC_DAPM_PGA("Right Speaker", AC97_EXTENDED_MSTATUS, 7, 1, NULL, 0),
431SND_SOC_DAPM_PGA("Out 3", AC97_EXTENDED_MSTATUS, 11, 1, NULL, 0),
432SND_SOC_DAPM_PGA("Out 4", AC97_EXTENDED_MSTATUS, 12, 1, NULL, 0),
433SND_SOC_DAPM_PGA("Mono Out", AC97_EXTENDED_MSTATUS, 13, 1, NULL, 0),
434SND_SOC_DAPM_PGA("Left Line In", AC97_EXTENDED_MSTATUS, 6, 1, NULL, 0),
435SND_SOC_DAPM_PGA("Right Line In", AC97_EXTENDED_MSTATUS, 5, 1, NULL, 0),
436SND_SOC_DAPM_PGA("Mono In", AC97_EXTENDED_MSTATUS, 4, 1, NULL, 0),
437SND_SOC_DAPM_PGA("Mic A PGA", AC97_EXTENDED_MSTATUS, 3, 1, NULL, 0),
438SND_SOC_DAPM_PGA("Mic B PGA", AC97_EXTENDED_MSTATUS, 2, 1, NULL, 0),
439SND_SOC_DAPM_PGA("Mic A Pre Amp", AC97_EXTENDED_MSTATUS, 1, 1, NULL, 0),
440SND_SOC_DAPM_PGA("Mic B Pre Amp", AC97_EXTENDED_MSTATUS, 0, 1, NULL, 0),
441SND_SOC_DAPM_MICBIAS("Mic Bias", AC97_EXTENDED_MSTATUS, 14, 1),
442SND_SOC_DAPM_OUTPUT("MONO"),
443SND_SOC_DAPM_OUTPUT("HPL"),
444SND_SOC_DAPM_OUTPUT("HPR"),
445SND_SOC_DAPM_OUTPUT("SPKL"),
446SND_SOC_DAPM_OUTPUT("SPKR"),
447SND_SOC_DAPM_OUTPUT("OUT3"),
448SND_SOC_DAPM_OUTPUT("OUT4"),
449SND_SOC_DAPM_INPUT("LINEL"),
450SND_SOC_DAPM_INPUT("LINER"),
451SND_SOC_DAPM_INPUT("MONOIN"),
452SND_SOC_DAPM_INPUT("PCBEEP"),
453SND_SOC_DAPM_INPUT("MIC1"),
454SND_SOC_DAPM_INPUT("MIC2A"),
455SND_SOC_DAPM_INPUT("MIC2B"),
456SND_SOC_DAPM_VMID("VMID"),
457};
458
459static const char *audio_map[][3] = {
460 /* left HP mixer */
461 {"Left HP Mixer", "PC Beep Playback Switch", "PCBEEP"},
462 {"Left HP Mixer", "Voice Playback Switch", "Voice DAC"},
463 {"Left HP Mixer", "Aux Playback Switch", "Aux DAC"},
464 {"Left HP Mixer", "Bypass Playback Switch", "Left Line In"},
465 {"Left HP Mixer", "PCM Playback Switch", "Left DAC"},
466 {"Left HP Mixer", "MonoIn Playback Switch", "Mono In"},
467 {"Left HP Mixer", NULL, "Capture Headphone Mux"},
468
469 /* right HP mixer */
470 {"Right HP Mixer", "PC Beep Playback Switch", "PCBEEP"},
471 {"Right HP Mixer", "Voice Playback Switch", "Voice DAC"},
472 {"Right HP Mixer", "Aux Playback Switch", "Aux DAC"},
473 {"Right HP Mixer", "Bypass Playback Switch", "Right Line In"},
474 {"Right HP Mixer", "PCM Playback Switch", "Right DAC"},
475 {"Right HP Mixer", "MonoIn Playback Switch", "Mono In"},
476 {"Right HP Mixer", NULL, "Capture Headphone Mux"},
477
478 /* virtual mixer - mixes left & right channels for spk and mono */
479 {"AC97 Mixer", NULL, "Left DAC"},
480 {"AC97 Mixer", NULL, "Right DAC"},
481 {"Line Mixer", NULL, "Right Line In"},
482 {"Line Mixer", NULL, "Left Line In"},
483 {"HP Mixer", NULL, "Left HP Mixer"},
484 {"HP Mixer", NULL, "Right HP Mixer"},
485 {"Capture Mixer", NULL, "Left Capture Source"},
486 {"Capture Mixer", NULL, "Right Capture Source"},
487
488 /* speaker mixer */
489 {"Speaker Mixer", "PC Beep Playback Switch", "PCBEEP"},
490 {"Speaker Mixer", "Voice Playback Switch", "Voice DAC"},
491 {"Speaker Mixer", "Aux Playback Switch", "Aux DAC"},
492 {"Speaker Mixer", "Bypass Playback Switch", "Line Mixer"},
493 {"Speaker Mixer", "PCM Playback Switch", "AC97 Mixer"},
494 {"Speaker Mixer", "MonoIn Playback Switch", "Mono In"},
495
496 /* mono mixer */
497 {"Mono Mixer", "PC Beep Playback Switch", "PCBEEP"},
498 {"Mono Mixer", "Voice Playback Switch", "Voice DAC"},
499 {"Mono Mixer", "Aux Playback Switch", "Aux DAC"},
500 {"Mono Mixer", "Bypass Playback Switch", "Line Mixer"},
501 {"Mono Mixer", "PCM Playback Switch", "AC97 Mixer"},
502 {"Mono Mixer", NULL, "Capture Mono Mux"},
503
504 /* DAC inv mux 1 */
505 {"DAC Inv Mux 1", "Mono", "Mono Mixer"},
506 {"DAC Inv Mux 1", "Speaker", "Speaker Mixer"},
507 {"DAC Inv Mux 1", "Left Headphone", "Left HP Mixer"},
508 {"DAC Inv Mux 1", "Right Headphone", "Right HP Mixer"},
509 {"DAC Inv Mux 1", "Headphone Mono", "HP Mixer"},
510
511 /* DAC inv mux 2 */
512 {"DAC Inv Mux 2", "Mono", "Mono Mixer"},
513 {"DAC Inv Mux 2", "Speaker", "Speaker Mixer"},
514 {"DAC Inv Mux 2", "Left Headphone", "Left HP Mixer"},
515 {"DAC Inv Mux 2", "Right Headphone", "Right HP Mixer"},
516 {"DAC Inv Mux 2", "Headphone Mono", "HP Mixer"},
517
518 /* headphone left mux */
519 {"Left Headphone Out Mux", "Headphone", "Left HP Mixer"},
520
521 /* headphone right mux */
522 {"Right Headphone Out Mux", "Headphone", "Right HP Mixer"},
523
524 /* speaker left mux */
525 {"Left Speaker Out Mux", "Headphone", "Left HP Mixer"},
526 {"Left Speaker Out Mux", "Speaker", "Speaker Mixer"},
527 {"Left Speaker Out Mux", "Inv", "DAC Inv Mux 1"},
528
529 /* speaker right mux */
530 {"Right Speaker Out Mux", "Headphone", "Right HP Mixer"},
531 {"Right Speaker Out Mux", "Speaker", "Speaker Mixer"},
532 {"Right Speaker Out Mux", "Inv", "DAC Inv Mux 2"},
533
534 /* mono mux */
535 {"Mono Out Mux", "Mono", "Mono Mixer"},
536 {"Mono Out Mux", "Inv", "DAC Inv Mux 1"},
537
538 /* out 3 mux */
539 {"Out 3 Mux", "Inv 1", "DAC Inv Mux 1"},
540
541 /* out 4 mux */
542 {"Out 4 Mux", "Inv 2", "DAC Inv Mux 2"},
543
544 /* output pga */
545 {"HPL", NULL, "Left Headphone"},
546 {"Left Headphone", NULL, "Left Headphone Out Mux"},
547 {"HPR", NULL, "Right Headphone"},
548 {"Right Headphone", NULL, "Right Headphone Out Mux"},
549 {"OUT3", NULL, "Out 3"},
550 {"Out 3", NULL, "Out 3 Mux"},
551 {"OUT4", NULL, "Out 4"},
552 {"Out 4", NULL, "Out 4 Mux"},
553 {"SPKL", NULL, "Left Speaker"},
554 {"Left Speaker", NULL, "Left Speaker Out Mux"},
555 {"SPKR", NULL, "Right Speaker"},
556 {"Right Speaker", NULL, "Right Speaker Out Mux"},
557 {"MONO", NULL, "Mono Out"},
558 {"Mono Out", NULL, "Mono Out Mux"},
559
560 /* input pga */
561 {"Left Line In", NULL, "LINEL"},
562 {"Right Line In", NULL, "LINER"},
563 {"Mono In", NULL, "MONOIN"},
564 {"Mic A PGA", NULL, "Mic A Pre Amp"},
565 {"Mic B PGA", NULL, "Mic B Pre Amp"},
566
567 /* left capture select */
568 {"Left Capture Source", "Mic 1", "Mic A Pre Amp"},
569 {"Left Capture Source", "Mic 2", "Mic B Pre Amp"},
570 {"Left Capture Source", "Line", "LINEL"},
571 {"Left Capture Source", "Mono In", "MONOIN"},
572 {"Left Capture Source", "Headphone", "Left HP Mixer"},
573 {"Left Capture Source", "Speaker", "Speaker Mixer"},
574 {"Left Capture Source", "Mono Out", "Mono Mixer"},
575
576 /* right capture select */
577 {"Right Capture Source", "Mic 1", "Mic A Pre Amp"},
578 {"Right Capture Source", "Mic 2", "Mic B Pre Amp"},
579 {"Right Capture Source", "Line", "LINER"},
580 {"Right Capture Source", "Mono In", "MONOIN"},
581 {"Right Capture Source", "Headphone", "Right HP Mixer"},
582 {"Right Capture Source", "Speaker", "Speaker Mixer"},
583 {"Right Capture Source", "Mono Out", "Mono Mixer"},
584
585 /* left ADC */
586 {"Left ADC", NULL, "Left Capture Source"},
587
588 /* right ADC */
589 {"Right ADC", NULL, "Right Capture Source"},
590
591 /* mic */
592 {"Mic A Pre Amp", NULL, "Mic A Source"},
593 {"Mic A Source", "Mic 1", "MIC1"},
594 {"Mic A Source", "Mic 2 A", "MIC2A"},
595 {"Mic A Source", "Mic 2 B", "Mic B Source"},
596 {"Mic B Pre Amp", "MPB", "Mic B Source"},
597 {"Mic B Source", NULL, "MIC2B"},
598
599 /* headphone capture */
600 {"Capture Headphone Mux", "Stereo", "Capture Mixer"},
601 {"Capture Headphone Mux", "Left", "Left Capture Source"},
602 {"Capture Headphone Mux", "Right", "Right Capture Source"},
603
604 /* mono capture */
605 {"Capture Mono Mux", "Stereo", "Capture Mixer"},
606 {"Capture Mono Mux", "Left", "Left Capture Source"},
607 {"Capture Mono Mux", "Right", "Right Capture Source"},
608
609 {NULL, NULL, NULL},
610};
611
612static int wm9713_add_widgets(struct snd_soc_codec *codec)
613{
614 int i;
615
616 for (i = 0; i < ARRAY_SIZE(wm9713_dapm_widgets); i++)
617 snd_soc_dapm_new_control(codec, &wm9713_dapm_widgets[i]);
618
619 /* set up audio path audio_mapnects */
620 for (i = 0; audio_map[i][0] != NULL; i++)
621 snd_soc_dapm_connect_input(codec, audio_map[i][0],
622 audio_map[i][1], audio_map[i][2]);
623
624 snd_soc_dapm_new_widgets(codec);
625 return 0;
626}
627
628static unsigned int ac97_read(struct snd_soc_codec *codec,
629 unsigned int reg)
630{
631 u16 *cache = codec->reg_cache;
632
633 if (reg == AC97_RESET || reg == AC97_GPIO_STATUS ||
634 reg == AC97_VENDOR_ID1 || reg == AC97_VENDOR_ID2 ||
635 reg == AC97_CD)
636 return soc_ac97_ops.read(codec->ac97, reg);
637 else {
638 reg = reg >> 1;
639
640 if (reg > (ARRAY_SIZE(wm9713_reg)))
641 return -EIO;
642
643 return cache[reg];
644 }
645}
646
647static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
648 unsigned int val)
649{
650 u16 *cache = codec->reg_cache;
651 if (reg < 0x7c)
652 soc_ac97_ops.write(codec->ac97, reg, val);
653 reg = reg >> 1;
654 if (reg <= (ARRAY_SIZE(wm9713_reg)))
655 cache[reg] = val;
656
657 return 0;
658}
659
660/* PLL divisors */
661struct _pll_div {
662 u32 divsel:1;
663 u32 divctl:1;
664 u32 lf:1;
665 u32 n:4;
666 u32 k:24;
667};
668
669/* The size in bits of the PLL divide multiplied by 10
670 * to allow rounding later */
671#define FIXED_PLL_SIZE ((1 << 22) * 10)
672
673static void pll_factors(struct _pll_div *pll_div, unsigned int source)
674{
675 u64 Kpart;
676 unsigned int K, Ndiv, Nmod, target;
677
678 /* The the PLL output is always 98.304MHz. */
679 target = 98304000;
680
681 /* If the input frequency is over 14.4MHz then scale it down. */
682 if (source > 14400000) {
683 source >>= 1;
684 pll_div->divsel = 1;
685
686 if (source > 14400000) {
687 source >>= 1;
688 pll_div->divctl = 1;
689 } else
690 pll_div->divctl = 0;
691
692 } else {
693 pll_div->divsel = 0;
694 pll_div->divctl = 0;
695 }
696
697 /* Low frequency sources require an additional divide in the
698 * loop.
699 */
700 if (source < 8192000) {
701 pll_div->lf = 1;
702 target >>= 2;
703 } else
704 pll_div->lf = 0;
705
706 Ndiv = target / source;
707 if ((Ndiv < 5) || (Ndiv > 12))
708 printk(KERN_WARNING
709 "WM9713 PLL N value %d out of recommended range!\n",
710 Ndiv);
711
712 pll_div->n = Ndiv;
713 Nmod = target % source;
714 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
715
716 do_div(Kpart, source);
717
718 K = Kpart & 0xFFFFFFFF;
719
720 /* Check if we need to round */
721 if ((K % 10) >= 5)
722 K += 5;
723
724 /* Move down to proper range now rounding is done */
725 K /= 10;
726
727 pll_div->k = K;
728}
729
730/**
731 * Please note that changing the PLL input frequency may require
732 * resynchronisation with the AC97 controller.
733 */
734static int wm9713_set_pll(struct snd_soc_codec *codec,
735 int pll_id, unsigned int freq_in, unsigned int freq_out)
736{
737 struct wm9713_priv *wm9713 = codec->private_data;
738 u16 reg, reg2;
739 struct _pll_div pll_div;
740
741 /* turn PLL off ? */
742 if (freq_in == 0 || freq_out == 0) {
743 /* disable PLL power and select ext source */
744 reg = ac97_read(codec, AC97_HANDSET_RATE);
745 ac97_write(codec, AC97_HANDSET_RATE, reg | 0x0080);
746 reg = ac97_read(codec, AC97_EXTENDED_MID);
747 ac97_write(codec, AC97_EXTENDED_MID, reg | 0x0200);
748 wm9713->pll_out = 0;
749 return 0;
750 }
751
752 pll_factors(&pll_div, freq_in);
753
754 if (pll_div.k == 0) {
755 reg = (pll_div.n << 12) | (pll_div.lf << 11) |
756 (pll_div.divsel << 9) | (pll_div.divctl << 8);
757 ac97_write(codec, AC97_LINE1_LEVEL, reg);
758 } else {
759 /* write the fractional k to the reg 0x46 pages */
760 reg2 = (pll_div.n << 12) | (pll_div.lf << 11) | (1 << 10) |
761 (pll_div.divsel << 9) | (pll_div.divctl << 8);
762
763 /* K [21:20] */
764 reg = reg2 | (0x5 << 4) | (pll_div.k >> 20);
765 ac97_write(codec, AC97_LINE1_LEVEL, reg);
766
767 /* K [19:16] */
768 reg = reg2 | (0x4 << 4) | ((pll_div.k >> 16) & 0xf);
769 ac97_write(codec, AC97_LINE1_LEVEL, reg);
770
771 /* K [15:12] */
772 reg = reg2 | (0x3 << 4) | ((pll_div.k >> 12) & 0xf);
773 ac97_write(codec, AC97_LINE1_LEVEL, reg);
774
775 /* K [11:8] */
776 reg = reg2 | (0x2 << 4) | ((pll_div.k >> 8) & 0xf);
777 ac97_write(codec, AC97_LINE1_LEVEL, reg);
778
779 /* K [7:4] */
780 reg = reg2 | (0x1 << 4) | ((pll_div.k >> 4) & 0xf);
781 ac97_write(codec, AC97_LINE1_LEVEL, reg);
782
783 reg = reg2 | (0x0 << 4) | (pll_div.k & 0xf); /* K [3:0] */
784 ac97_write(codec, AC97_LINE1_LEVEL, reg);
785 }
786
787 /* turn PLL on and select as source */
788 reg = ac97_read(codec, AC97_EXTENDED_MID);
789 ac97_write(codec, AC97_EXTENDED_MID, reg & 0xfdff);
790 reg = ac97_read(codec, AC97_HANDSET_RATE);
791 ac97_write(codec, AC97_HANDSET_RATE, reg & 0xff7f);
792 wm9713->pll_out = freq_out;
793 wm9713->pll_in = freq_in;
794
795 /* wait 10ms AC97 link frames for the link to stabilise */
796 schedule_timeout_interruptible(msecs_to_jiffies(10));
797 return 0;
798}
799
800static int wm9713_set_dai_pll(struct snd_soc_codec_dai *codec_dai,
801 int pll_id, unsigned int freq_in, unsigned int freq_out)
802{
803 struct snd_soc_codec *codec = codec_dai->codec;
804 return wm9713_set_pll(codec, pll_id, freq_in, freq_out);
805}
806
807/*
808 * Tristate the PCM DAI lines, tristate can be disabled by calling
809 * wm9713_set_dai_fmt()
810 */
811static int wm9713_set_dai_tristate(struct snd_soc_codec_dai *codec_dai,
812 int tristate)
813{
814 struct snd_soc_codec *codec = codec_dai->codec;
815 u16 reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0x9fff;
816
817 if (tristate)
818 ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
819
820 return 0;
821}
822
823/*
824 * Configure WM9713 clock dividers.
825 * Voice DAC needs 256 FS
826 */
827static int wm9713_set_dai_clkdiv(struct snd_soc_codec_dai *codec_dai,
828 int div_id, int div)
829{
830 struct snd_soc_codec *codec = codec_dai->codec;
831 u16 reg;
832
833 switch (div_id) {
834 case WM9713_PCMCLK_DIV:
835 reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xf0ff;
836 ac97_write(codec, AC97_HANDSET_RATE, reg | div);
837 break;
838 case WM9713_CLKA_MULT:
839 reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xfffd;
840 ac97_write(codec, AC97_HANDSET_RATE, reg | div);
841 break;
842 case WM9713_CLKB_MULT:
843 reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xfffb;
844 ac97_write(codec, AC97_HANDSET_RATE, reg | div);
845 break;
846 case WM9713_HIFI_DIV:
847 reg = ac97_read(codec, AC97_HANDSET_RATE) & 0x8fff;
848 ac97_write(codec, AC97_HANDSET_RATE, reg | div);
849 break;
850 case WM9713_PCMBCLK_DIV:
851 reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0xf1ff;
852 ac97_write(codec, AC97_CENTER_LFE_MASTER, reg | div);
853 break;
854 case WM9713_PCMCLK_PLL_DIV:
855 reg = ac97_read(codec, AC97_LINE1_LEVEL) & 0xff80;
856 ac97_write(codec, AC97_LINE1_LEVEL, reg | 0x60 | div);
857 break;
858 case WM9713_HIFI_PLL_DIV:
859 reg = ac97_read(codec, AC97_LINE1_LEVEL) & 0xff80;
860 ac97_write(codec, AC97_LINE1_LEVEL, reg | 0x70 | div);
861 break;
862 default:
863 return -EINVAL;
864 }
865
866 return 0;
867}
868
869static int wm9713_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
870 unsigned int fmt)
871{
872 struct snd_soc_codec *codec = codec_dai->codec;
873 u16 gpio = ac97_read(codec, AC97_GPIO_CFG) & 0xffc5;
874 u16 reg = 0x8000;
875
876 /* clock masters */
877 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
878 case SND_SOC_DAIFMT_CBM_CFM:
879 reg |= 0x4000;
880 gpio |= 0x0010;
881 break;
882 case SND_SOC_DAIFMT_CBM_CFS:
883 reg |= 0x6000;
884 gpio |= 0x0018;
885 break;
886 case SND_SOC_DAIFMT_CBS_CFS:
887 reg |= 0x0200;
888 gpio |= 0x001a;
889 break;
890 case SND_SOC_DAIFMT_CBS_CFM:
891 gpio |= 0x0012;
892 break;
893 }
894
895 /* clock inversion */
896 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
897 case SND_SOC_DAIFMT_IB_IF:
898 reg |= 0x00c0;
899 break;
900 case SND_SOC_DAIFMT_IB_NF:
901 reg |= 0x0080;
902 break;
903 case SND_SOC_DAIFMT_NB_IF:
904 reg |= 0x0040;
905 break;
906 }
907
908 /* DAI format */
909 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
910 case SND_SOC_DAIFMT_I2S:
911 reg |= 0x0002;
912 break;
913 case SND_SOC_DAIFMT_RIGHT_J:
914 break;
915 case SND_SOC_DAIFMT_LEFT_J:
916 reg |= 0x0001;
917 break;
918 case SND_SOC_DAIFMT_DSP_A:
919 reg |= 0x0003;
920 break;
921 case SND_SOC_DAIFMT_DSP_B:
922 reg |= 0x0043;
923 break;
924 }
925
926 ac97_write(codec, AC97_GPIO_CFG, gpio);
927 ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
928 return 0;
929}
930
931static int wm9713_pcm_hw_params(struct snd_pcm_substream *substream,
932 struct snd_pcm_hw_params *params)
933{
934 struct snd_soc_pcm_runtime *rtd = substream->private_data;
935 struct snd_soc_device *socdev = rtd->socdev;
936 struct snd_soc_codec *codec = socdev->codec;
937 u16 reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0xfff3;
938
939 switch (params_format(params)) {
940 case SNDRV_PCM_FORMAT_S16_LE:
941 break;
942 case SNDRV_PCM_FORMAT_S20_3LE:
943 reg |= 0x0004;
944 break;
945 case SNDRV_PCM_FORMAT_S24_LE:
946 reg |= 0x0008;
947 break;
948 case SNDRV_PCM_FORMAT_S32_LE:
949 reg |= 0x000c;
950 break;
951 }
952
953 /* enable PCM interface in master mode */
954 ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
955 return 0;
956}
957
958static void wm9713_voiceshutdown(struct snd_pcm_substream *substream)
959{
960 struct snd_soc_pcm_runtime *rtd = substream->private_data;
961 struct snd_soc_device *socdev = rtd->socdev;
962 struct snd_soc_codec *codec = socdev->codec;
963 u16 status;
964
965 /* Gracefully shut down the voice interface. */
966 status = ac97_read(codec, AC97_EXTENDED_STATUS) | 0x1000;
967 ac97_write(codec, AC97_HANDSET_RATE, 0x0280);
968 schedule_timeout_interruptible(msecs_to_jiffies(1));
969 ac97_write(codec, AC97_HANDSET_RATE, 0x0F80);
970 ac97_write(codec, AC97_EXTENDED_MID, status);
971}
972
973static int ac97_hifi_prepare(struct snd_pcm_substream *substream)
974{
975 struct snd_pcm_runtime *runtime = substream->runtime;
976 struct snd_soc_pcm_runtime *rtd = substream->private_data;
977 struct snd_soc_device *socdev = rtd->socdev;
978 struct snd_soc_codec *codec = socdev->codec;
979 int reg;
980 u16 vra;
981
982 vra = ac97_read(codec, AC97_EXTENDED_STATUS);
983 ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
984
985 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
986 reg = AC97_PCM_FRONT_DAC_RATE;
987 else
988 reg = AC97_PCM_LR_ADC_RATE;
989
990 return ac97_write(codec, reg, runtime->rate);
991}
992
993static int ac97_aux_prepare(struct snd_pcm_substream *substream)
994{
995 struct snd_pcm_runtime *runtime = substream->runtime;
996 struct snd_soc_pcm_runtime *rtd = substream->private_data;
997 struct snd_soc_device *socdev = rtd->socdev;
998 struct snd_soc_codec *codec = socdev->codec;
999 u16 vra, xsle;
1000
1001 vra = ac97_read(codec, AC97_EXTENDED_STATUS);
1002 ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
1003 xsle = ac97_read(codec, AC97_PCI_SID);
1004 ac97_write(codec, AC97_PCI_SID, xsle | 0x8000);
1005
1006 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
1007 return -ENODEV;
1008
1009 return ac97_write(codec, AC97_PCM_SURR_DAC_RATE, runtime->rate);
1010}
1011
1012#define WM9713_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
1013 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 |\
1014 SNDRV_PCM_RATE_48000)
1015
1016#define WM9713_PCM_FORMATS \
1017 (SNDRV_PCM_FORMAT_S16_LE | SNDRV_PCM_FORMAT_S20_3LE | \
1018 SNDRV_PCM_FORMAT_S24_LE)
1019
1020struct snd_soc_codec_dai wm9713_dai[] = {
1021{
1022 .name = "AC97 HiFi",
1023 .type = SND_SOC_DAI_AC97_BUS,
1024 .playback = {
1025 .stream_name = "HiFi Playback",
1026 .channels_min = 1,
1027 .channels_max = 2,
1028 .rates = WM9713_RATES,
1029 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
1030 .capture = {
1031 .stream_name = "HiFi Capture",
1032 .channels_min = 1,
1033 .channels_max = 2,
1034 .rates = WM9713_RATES,
1035 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
1036 .ops = {
1037 .prepare = ac97_hifi_prepare,},
1038 .dai_ops = {
1039 .set_clkdiv = wm9713_set_dai_clkdiv,
1040 .set_pll = wm9713_set_dai_pll,},
1041 },
1042 {
1043 .name = "AC97 Aux",
1044 .playback = {
1045 .stream_name = "Aux Playback",
1046 .channels_min = 1,
1047 .channels_max = 1,
1048 .rates = WM9713_RATES,
1049 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
1050 .ops = {
1051 .prepare = ac97_aux_prepare,},
1052 .dai_ops = {
1053 .set_clkdiv = wm9713_set_dai_clkdiv,
1054 .set_pll = wm9713_set_dai_pll,},
1055 },
1056 {
1057 .name = "WM9713 Voice",
1058 .playback = {
1059 .stream_name = "Voice Playback",
1060 .channels_min = 1,
1061 .channels_max = 1,
1062 .rates = WM9713_RATES,
1063 .formats = WM9713_PCM_FORMATS,},
1064 .capture = {
1065 .stream_name = "Voice Capture",
1066 .channels_min = 1,
1067 .channels_max = 2,
1068 .rates = WM9713_RATES,
1069 .formats = WM9713_PCM_FORMATS,},
1070 .ops = {
1071 .hw_params = wm9713_pcm_hw_params,
1072 .shutdown = wm9713_voiceshutdown,},
1073 .dai_ops = {
1074 .set_clkdiv = wm9713_set_dai_clkdiv,
1075 .set_pll = wm9713_set_dai_pll,
1076 .set_fmt = wm9713_set_dai_fmt,
1077 .set_tristate = wm9713_set_dai_tristate,
1078 },
1079 },
1080};
1081EXPORT_SYMBOL_GPL(wm9713_dai);
1082
1083int wm9713_reset(struct snd_soc_codec *codec, int try_warm)
1084{
1085 if (try_warm && soc_ac97_ops.warm_reset) {
1086 soc_ac97_ops.warm_reset(codec->ac97);
1087 if (!(ac97_read(codec, 0) & 0x8000))
1088 return 1;
1089 }
1090
1091 soc_ac97_ops.reset(codec->ac97);
1092 if (ac97_read(codec, 0) & 0x8000)
1093 return -EIO;
1094 return 0;
1095}
1096EXPORT_SYMBOL_GPL(wm9713_reset);
1097
1098static int wm9713_dapm_event(struct snd_soc_codec *codec, int event)
1099{
1100 u16 reg;
1101
1102 switch (event) {
1103 case SNDRV_CTL_POWER_D0: /* full On */
1104 /* enable thermal shutdown */
1105 reg = ac97_read(codec, AC97_EXTENDED_MID) & 0x1bff;
1106 ac97_write(codec, AC97_EXTENDED_MID, reg);
1107 break;
1108 case SNDRV_CTL_POWER_D1: /* partial On */
1109 case SNDRV_CTL_POWER_D2: /* partial On */
1110 break;
1111 case SNDRV_CTL_POWER_D3hot: /* Off, with power */
1112 /* enable master bias and vmid */
1113 reg = ac97_read(codec, AC97_EXTENDED_MID) & 0x3bff;
1114 ac97_write(codec, AC97_EXTENDED_MID, reg);
1115 ac97_write(codec, AC97_POWERDOWN, 0x0000);
1116 break;
1117 case SNDRV_CTL_POWER_D3cold: /* Off, without power */
1118 /* disable everything including AC link */
1119 ac97_write(codec, AC97_EXTENDED_MID, 0xffff);
1120 ac97_write(codec, AC97_EXTENDED_MSTATUS, 0xffff);
1121 ac97_write(codec, AC97_POWERDOWN, 0xffff);
1122 break;
1123 }
1124 codec->dapm_state = event;
1125 return 0;
1126}
1127
1128static int wm9713_soc_suspend(struct platform_device *pdev,
1129 pm_message_t state)
1130{
1131 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1132 struct snd_soc_codec *codec = socdev->codec;
1133
1134 wm9713_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
1135 return 0;
1136}
1137
1138static int wm9713_soc_resume(struct platform_device *pdev)
1139{
1140 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1141 struct snd_soc_codec *codec = socdev->codec;
1142 struct wm9713_priv *wm9713 = codec->private_data;
1143 int i, ret;
1144 u16 *cache = codec->reg_cache;
1145
1146 ret = wm9713_reset(codec, 1);
1147 if (ret < 0) {
1148 printk(KERN_ERR "could not reset AC97 codec\n");
1149 return ret;
1150 }
1151
1152 wm9713_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
1153
1154 /* do we need to re-start the PLL ? */
1155 if (wm9713->pll_out)
1156 wm9713_set_pll(codec, 0, wm9713->pll_in, wm9713->pll_out);
1157
1158 /* only synchronise the codec if warm reset failed */
1159 if (ret == 0) {
1160 for (i = 2; i < ARRAY_SIZE(wm9713_reg) << 1; i += 2) {
1161 if (i == AC97_POWERDOWN || i == AC97_EXTENDED_MID ||
1162 i == AC97_EXTENDED_MSTATUS || i > 0x66)
1163 continue;
1164 soc_ac97_ops.write(codec->ac97, i, cache[i>>1]);
1165 }
1166 }
1167
1168 if (codec->suspend_dapm_state == SNDRV_CTL_POWER_D0)
1169 wm9713_dapm_event(codec, SNDRV_CTL_POWER_D0);
1170
1171 return ret;
1172}
1173
1174static int wm9713_soc_probe(struct platform_device *pdev)
1175{
1176 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1177 struct snd_soc_codec *codec;
1178 int ret = 0, reg;
1179
1180 printk(KERN_INFO "WM9713/WM9714 SoC Audio Codec %s\n", WM9713_VERSION);
1181
1182 socdev->codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
1183 if (socdev->codec == NULL)
1184 return -ENOMEM;
1185 codec = socdev->codec;
1186 mutex_init(&codec->mutex);
1187
1188 codec->reg_cache = kmemdup(wm9713_reg, sizeof(wm9713_reg), GFP_KERNEL);
1189 if (codec->reg_cache == NULL) {
1190 ret = -ENOMEM;
1191 goto cache_err;
1192 }
1193 codec->reg_cache_size = sizeof(wm9713_reg);
1194 codec->reg_cache_step = 2;
1195
1196 codec->private_data = kzalloc(sizeof(struct wm9713_priv), GFP_KERNEL);
1197 if (codec->private_data == NULL) {
1198 ret = -ENOMEM;
1199 goto priv_err;
1200 }
1201
1202 codec->name = "WM9713";
1203 codec->owner = THIS_MODULE;
1204 codec->dai = wm9713_dai;
1205 codec->num_dai = ARRAY_SIZE(wm9713_dai);
1206 codec->write = ac97_write;
1207 codec->read = ac97_read;
1208 codec->dapm_event = wm9713_dapm_event;
1209 INIT_LIST_HEAD(&codec->dapm_widgets);
1210 INIT_LIST_HEAD(&codec->dapm_paths);
1211
1212 ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
1213 if (ret < 0)
1214 goto codec_err;
1215
1216 /* register pcms */
1217 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
1218 if (ret < 0)
1219 goto pcm_err;
1220
1221 /* do a cold reset for the controller and then try
1222 * a warm reset followed by an optional cold reset for codec */
1223 wm9713_reset(codec, 0);
1224 ret = wm9713_reset(codec, 1);
1225 if (ret < 0) {
1226 printk(KERN_ERR "AC97 link error\n");
1227 goto reset_err;
1228 }
1229
1230 wm9713_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
1231
1232 /* unmute the adc - move to kcontrol */
1233 reg = ac97_read(codec, AC97_CD) & 0x7fff;
1234 ac97_write(codec, AC97_CD, reg);
1235
1236 wm9713_add_controls(codec);
1237 wm9713_add_widgets(codec);
1238 ret = snd_soc_register_card(socdev);
1239 if (ret < 0)
1240 goto reset_err;
1241 return 0;
1242
1243reset_err:
1244 snd_soc_free_pcms(socdev);
1245
1246pcm_err:
1247 snd_soc_free_ac97_codec(codec);
1248
1249codec_err:
1250 kfree(codec->private_data);
1251
1252priv_err:
1253 kfree(codec->reg_cache);
1254
1255cache_err:
1256 kfree(socdev->codec);
1257 socdev->codec = NULL;
1258 return ret;
1259}
1260
1261static int wm9713_soc_remove(struct platform_device *pdev)
1262{
1263 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1264 struct snd_soc_codec *codec = socdev->codec;
1265
1266 if (codec == NULL)
1267 return 0;
1268
1269 snd_soc_dapm_free(socdev);
1270 snd_soc_free_pcms(socdev);
1271 snd_soc_free_ac97_codec(codec);
1272 kfree(codec->private_data);
1273 kfree(codec->reg_cache);
1274 kfree(codec->dai);
1275 kfree(codec);
1276 return 0;
1277}
1278
1279struct snd_soc_codec_device soc_codec_dev_wm9713 = {
1280 .probe = wm9713_soc_probe,
1281 .remove = wm9713_soc_remove,
1282 .suspend = wm9713_soc_suspend,
1283 .resume = wm9713_soc_resume,
1284};
1285EXPORT_SYMBOL_GPL(soc_codec_dev_wm9713);
1286
1287MODULE_DESCRIPTION("ASoC WM9713/WM9714 driver");
1288MODULE_AUTHOR("Liam Girdwood");
1289MODULE_LICENSE("GPL");
diff --git a/sound/soc/codecs/wm9713.h b/sound/soc/codecs/wm9713.h
new file mode 100644
index 000000000000..d357b6c8134b
--- /dev/null
+++ b/sound/soc/codecs/wm9713.h
@@ -0,0 +1,53 @@
1/*
2 * wm9713.h -- WM9713 Soc Audio driver
3 */
4
5#ifndef _WM9713_H
6#define _WM9713_H
7
8/* clock inputs */
9#define WM9713_CLKA_PIN 0
10#define WM9713_CLKB_PIN 1
11
12/* clock divider ID's */
13#define WM9713_PCMCLK_DIV 0
14#define WM9713_CLKA_MULT 1
15#define WM9713_CLKB_MULT 2
16#define WM9713_HIFI_DIV 3
17#define WM9713_PCMBCLK_DIV 4
18#define WM9713_PCMCLK_PLL_DIV 5
19#define WM9713_HIFI_PLL_DIV 6
20
21/* Calculate the appropriate bit mask for the external PCM clock divider */
22#define WM9713_PCMDIV(x) ((x - 1) << 8)
23
24/* Calculate the appropriate bit mask for the external HiFi clock divider */
25#define WM9713_HIFIDIV(x) ((x - 1) << 12)
26
27/* MCLK clock mulitipliers */
28#define WM9713_CLKA_X1 (0 << 1)
29#define WM9713_CLKA_X2 (1 << 1)
30#define WM9713_CLKB_X1 (0 << 2)
31#define WM9713_CLKB_X2 (1 << 2)
32
33/* MCLK clock MUX */
34#define WM9713_CLK_MUX_A (0 << 0)
35#define WM9713_CLK_MUX_B (1 << 0)
36
37/* Voice DAI BCLK divider */
38#define WM9713_PCMBCLK_DIV_1 (0 << 9)
39#define WM9713_PCMBCLK_DIV_2 (1 << 9)
40#define WM9713_PCMBCLK_DIV_4 (2 << 9)
41#define WM9713_PCMBCLK_DIV_8 (3 << 9)
42#define WM9713_PCMBCLK_DIV_16 (4 << 9)
43
44#define WM9713_DAI_AC97_HIFI 0
45#define WM9713_DAI_AC97_AUX 1
46#define WM9713_DAI_PCM_VOICE 2
47
48extern struct snd_soc_codec_device soc_codec_dev_wm9713;
49extern struct snd_soc_codec_dai wm9713_dai[3];
50
51int wm9713_reset(struct snd_soc_codec *codec, int try_warm);
52
53#endif