blob: 87b236b160160ec6ba9705d39507dbaaffdddb62 [file] [log] [blame]
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001/*
2 * wm9713.c -- ALSA Soc WM9713 codec support
3 *
4 * Copyright 2006 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood83ac08c2008-02-15 16:43:11 +01006 *
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.
11 *
Liam Girdwood83ac08c2008-02-15 16:43:11 +010012 * Features:-
13 *
14 * o Support for AC97 Codec, Voice DAC and Aux DAC
15 * o Support for DAPM
16 */
17
18#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Liam Girdwood83ac08c2008-02-15 16:43:11 +010020#include <linux/module.h>
21#include <linux/device.h>
22#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/ac97_codec.h>
25#include <sound/initval.h>
26#include <sound/pcm_params.h>
Mark Browne03a8d22010-01-28 12:36:07 +000027#include <sound/tlv.h>
Liam Girdwood83ac08c2008-02-15 16:43:11 +010028#include <sound/soc.h>
29#include <sound/soc-dapm.h>
30
31#include "wm9713.h"
32
Liam Girdwood83ac08c2008-02-15 16:43:11 +010033struct wm9713_priv {
34 u32 pll_in; /* PLL input frequency */
Liam Girdwood83ac08c2008-02-15 16:43:11 +010035};
36
37static unsigned int ac97_read(struct snd_soc_codec *codec,
38 unsigned int reg);
39static int ac97_write(struct snd_soc_codec *codec,
40 unsigned int reg, unsigned int val);
41
42/*
43 * WM9713 register cache
44 * Reg 0x3c bit 15 is used by touch driver.
45 */
46static const u16 wm9713_reg[] = {
47 0x6174, 0x8080, 0x8080, 0x8080,
48 0xc880, 0xe808, 0xe808, 0x0808,
49 0x00da, 0x8000, 0xd600, 0xaaa0,
50 0xaaa0, 0xaaa0, 0x0000, 0x0000,
51 0x0f0f, 0x0040, 0x0000, 0x7f00,
52 0x0405, 0x0410, 0xbb80, 0xbb80,
53 0x0000, 0xbb80, 0x0000, 0x4523,
54 0x0000, 0x2000, 0x7eff, 0xffff,
55 0x0000, 0x0000, 0x0080, 0x0000,
56 0x0000, 0x0000, 0xfffe, 0xffff,
57 0x0000, 0x0000, 0x0000, 0xfffe,
58 0x4000, 0x0000, 0x0000, 0x0000,
59 0xb032, 0x3e00, 0x0000, 0x0000,
60 0x0000, 0x0000, 0x0000, 0x0000,
61 0x0000, 0x0000, 0x0000, 0x0006,
62 0x0001, 0x0000, 0x574d, 0x4c13,
63 0x0000, 0x0000, 0x0000
64};
65
66/* virtual HP mixers regs */
67#define HPL_MIXER 0x80
68#define HPR_MIXER 0x82
69#define MICB_MUX 0x82
70
71static const char *wm9713_mic_mixer[] = {"Stereo", "Mic 1", "Mic 2", "Mute"};
72static const char *wm9713_rec_mux[] = {"Stereo", "Left", "Right", "Mute"};
73static const char *wm9713_rec_src[] =
74 {"Mic 1", "Mic 2", "Line", "Mono In", "Headphone", "Speaker",
75 "Mono Out", "Zh"};
76static const char *wm9713_rec_gain[] = {"+1.5dB Steps", "+0.75dB Steps"};
77static const char *wm9713_alc_select[] = {"None", "Left", "Right", "Stereo"};
78static const char *wm9713_mono_pga[] = {"Vmid", "Zh", "Mono", "Inv",
79 "Mono Vmid", "Inv Vmid"};
80static const char *wm9713_spk_pga[] =
81 {"Vmid", "Zh", "Headphone", "Speaker", "Inv", "Headphone Vmid",
82 "Speaker Vmid", "Inv Vmid"};
83static const char *wm9713_hp_pga[] = {"Vmid", "Zh", "Headphone",
84 "Headphone Vmid"};
85static const char *wm9713_out3_pga[] = {"Vmid", "Zh", "Inv 1", "Inv 1 Vmid"};
86static const char *wm9713_out4_pga[] = {"Vmid", "Zh", "Inv 2", "Inv 2 Vmid"};
87static const char *wm9713_dac_inv[] =
88 {"Off", "Mono", "Speaker", "Left Headphone", "Right Headphone",
89 "Headphone Mono", "NC", "Vmid"};
90static const char *wm9713_bass[] = {"Linear Control", "Adaptive Boost"};
91static const char *wm9713_ng_type[] = {"Constant Gain", "Mute"};
92static const char *wm9713_mic_select[] = {"Mic 1", "Mic 2 A", "Mic 2 B"};
93static const char *wm9713_micb_select[] = {"MPB", "MPA"};
94
95static const struct soc_enum wm9713_enum[] = {
96SOC_ENUM_SINGLE(AC97_LINE, 3, 4, wm9713_mic_mixer), /* record mic mixer 0 */
97SOC_ENUM_SINGLE(AC97_VIDEO, 14, 4, wm9713_rec_mux), /* record mux hp 1 */
98SOC_ENUM_SINGLE(AC97_VIDEO, 9, 4, wm9713_rec_mux), /* record mux mono 2 */
99SOC_ENUM_SINGLE(AC97_VIDEO, 3, 8, wm9713_rec_src), /* record mux left 3 */
100SOC_ENUM_SINGLE(AC97_VIDEO, 0, 8, wm9713_rec_src), /* record mux right 4*/
101SOC_ENUM_DOUBLE(AC97_CD, 14, 6, 2, wm9713_rec_gain), /* record step size 5 */
102SOC_ENUM_SINGLE(AC97_PCI_SVID, 14, 4, wm9713_alc_select), /* alc source select 6*/
103SOC_ENUM_SINGLE(AC97_REC_GAIN, 14, 4, wm9713_mono_pga), /* mono input select 7 */
104SOC_ENUM_SINGLE(AC97_REC_GAIN, 11, 8, wm9713_spk_pga), /* speaker left input select 8 */
105SOC_ENUM_SINGLE(AC97_REC_GAIN, 8, 8, wm9713_spk_pga), /* speaker right input select 9 */
106SOC_ENUM_SINGLE(AC97_REC_GAIN, 6, 3, wm9713_hp_pga), /* headphone left input 10 */
107SOC_ENUM_SINGLE(AC97_REC_GAIN, 4, 3, wm9713_hp_pga), /* headphone right input 11 */
108SOC_ENUM_SINGLE(AC97_REC_GAIN, 2, 4, wm9713_out3_pga), /* out 3 source 12 */
109SOC_ENUM_SINGLE(AC97_REC_GAIN, 0, 4, wm9713_out4_pga), /* out 4 source 13 */
110SOC_ENUM_SINGLE(AC97_REC_GAIN_MIC, 13, 8, wm9713_dac_inv), /* dac invert 1 14 */
111SOC_ENUM_SINGLE(AC97_REC_GAIN_MIC, 10, 8, wm9713_dac_inv), /* dac invert 2 15 */
112SOC_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 15, 2, wm9713_bass), /* bass control 16 */
113SOC_ENUM_SINGLE(AC97_PCI_SVID, 5, 2, wm9713_ng_type), /* noise gate type 17 */
114SOC_ENUM_SINGLE(AC97_3D_CONTROL, 12, 3, wm9713_mic_select), /* mic selection 18 */
115SOC_ENUM_SINGLE(MICB_MUX, 0, 2, wm9713_micb_select), /* mic selection 19 */
116};
117
Mark Browne03a8d22010-01-28 12:36:07 +0000118static const DECLARE_TLV_DB_SCALE(out_tlv, -4650, 150, 0);
119static const DECLARE_TLV_DB_SCALE(main_tlv, -3450, 150, 0);
120static const DECLARE_TLV_DB_SCALE(misc_tlv, -1500, 300, 0);
121static unsigned int mic_tlv[] = {
122 TLV_DB_RANGE_HEAD(2),
123 0, 2, TLV_DB_SCALE_ITEM(1200, 600, 0),
124 3, 3, TLV_DB_SCALE_ITEM(3000, 0, 0),
125};
126
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100127static const struct snd_kcontrol_new wm9713_snd_ac97_controls[] = {
Mark Browne03a8d22010-01-28 12:36:07 +0000128SOC_DOUBLE_TLV("Speaker Playback Volume", AC97_MASTER, 8, 0, 31, 1, out_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100129SOC_DOUBLE("Speaker Playback Switch", AC97_MASTER, 15, 7, 1, 1),
Mark Browne03a8d22010-01-28 12:36:07 +0000130SOC_DOUBLE_TLV("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1,
131 out_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100132SOC_DOUBLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 7, 1, 1),
Mark Browne03a8d22010-01-28 12:36:07 +0000133SOC_DOUBLE_TLV("Line In Volume", AC97_PC_BEEP, 8, 0, 31, 1, main_tlv),
134SOC_DOUBLE_TLV("PCM Playback Volume", AC97_PHONE, 8, 0, 31, 1, main_tlv),
135SOC_SINGLE_TLV("Mic 1 Volume", AC97_MIC, 8, 31, 1, main_tlv),
136SOC_SINGLE_TLV("Mic 2 Volume", AC97_MIC, 0, 31, 1, main_tlv),
137SOC_SINGLE_TLV("Mic 1 Preamp Volume", AC97_3D_CONTROL, 10, 3, 0, mic_tlv),
138SOC_SINGLE_TLV("Mic 2 Preamp Volume", AC97_3D_CONTROL, 12, 3, 0, mic_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100139
140SOC_SINGLE("Mic Boost (+20dB) Switch", AC97_LINE, 5, 1, 0),
141SOC_SINGLE("Mic Headphone Mixer Volume", AC97_LINE, 0, 7, 1),
142
143SOC_SINGLE("Capture Switch", AC97_CD, 15, 1, 1),
144SOC_ENUM("Capture Volume Steps", wm9713_enum[5]),
145SOC_DOUBLE("Capture Volume", AC97_CD, 8, 0, 31, 0),
146SOC_SINGLE("Capture ZC Switch", AC97_CD, 7, 1, 0),
147
Mark Browne03a8d22010-01-28 12:36:07 +0000148SOC_SINGLE_TLV("Capture to Headphone Volume", AC97_VIDEO, 11, 7, 1, misc_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100149SOC_SINGLE("Capture to Mono Boost (+20dB) Switch", AC97_VIDEO, 8, 1, 0),
150SOC_SINGLE("Capture ADC Boost (+20dB) Switch", AC97_VIDEO, 6, 1, 0),
151
152SOC_SINGLE("ALC Target Volume", AC97_CODEC_CLASS_REV, 12, 15, 0),
153SOC_SINGLE("ALC Hold Time", AC97_CODEC_CLASS_REV, 8, 15, 0),
Mark Brown13d622b2008-10-30 12:37:10 +0000154SOC_SINGLE("ALC Decay Time", AC97_CODEC_CLASS_REV, 4, 15, 0),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100155SOC_SINGLE("ALC Attack Time", AC97_CODEC_CLASS_REV, 0, 15, 0),
156SOC_ENUM("ALC Function", wm9713_enum[6]),
157SOC_SINGLE("ALC Max Volume", AC97_PCI_SVID, 11, 7, 0),
158SOC_SINGLE("ALC ZC Timeout", AC97_PCI_SVID, 9, 3, 0),
159SOC_SINGLE("ALC ZC Switch", AC97_PCI_SVID, 8, 1, 0),
160SOC_SINGLE("ALC NG Switch", AC97_PCI_SVID, 7, 1, 0),
161SOC_ENUM("ALC NG Type", wm9713_enum[17]),
162SOC_SINGLE("ALC NG Threshold", AC97_PCI_SVID, 0, 31, 0),
163
164SOC_DOUBLE("Speaker Playback ZC Switch", AC97_MASTER, 14, 6, 1, 0),
165SOC_DOUBLE("Headphone Playback ZC Switch", AC97_HEADPHONE, 14, 6, 1, 0),
166
167SOC_SINGLE("Out4 Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
168SOC_SINGLE("Out4 Playback ZC Switch", AC97_MASTER_MONO, 14, 1, 0),
Mark Browne03a8d22010-01-28 12:36:07 +0000169SOC_SINGLE_TLV("Out4 Playback Volume", AC97_MASTER_MONO, 8, 31, 1, out_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100170
171SOC_SINGLE("Out3 Playback Switch", AC97_MASTER_MONO, 7, 1, 1),
172SOC_SINGLE("Out3 Playback ZC Switch", AC97_MASTER_MONO, 6, 1, 0),
Mark Browne03a8d22010-01-28 12:36:07 +0000173SOC_SINGLE_TLV("Out3 Playback Volume", AC97_MASTER_MONO, 0, 31, 1, out_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100174
Mark Browne03a8d22010-01-28 12:36:07 +0000175SOC_SINGLE_TLV("Mono Capture Volume", AC97_MASTER_TONE, 8, 31, 1, main_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100176SOC_SINGLE("Mono Playback Switch", AC97_MASTER_TONE, 7, 1, 1),
177SOC_SINGLE("Mono Playback ZC Switch", AC97_MASTER_TONE, 6, 1, 0),
Mark Browne03a8d22010-01-28 12:36:07 +0000178SOC_SINGLE_TLV("Mono Playback Volume", AC97_MASTER_TONE, 0, 31, 1, out_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100179
Mark Browne03a8d22010-01-28 12:36:07 +0000180SOC_SINGLE_TLV("Headphone Mixer Beep Playback Volume", AC97_AUX, 12, 7, 1,
181 misc_tlv),
182SOC_SINGLE_TLV("Speaker Mixer Beep Playback Volume", AC97_AUX, 8, 7, 1,
183 misc_tlv),
184SOC_SINGLE_TLV("Mono Mixer Beep Playback Volume", AC97_AUX, 4, 7, 1, misc_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100185
Mark Browne03a8d22010-01-28 12:36:07 +0000186SOC_SINGLE_TLV("Voice Playback Headphone Volume", AC97_PCM, 12, 7, 1,
187 misc_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100188SOC_SINGLE("Voice Playback Master Volume", AC97_PCM, 8, 7, 1),
189SOC_SINGLE("Voice Playback Mono Volume", AC97_PCM, 4, 7, 1),
190
Mark Browne03a8d22010-01-28 12:36:07 +0000191SOC_SINGLE_TLV("Headphone Mixer Aux Playback Volume", AC97_REC_SEL, 12, 7, 1,
192 misc_tlv),
193
194SOC_SINGLE_TLV("Speaker Mixer Voice Playback Volume", AC97_PCM, 8, 7, 1,
195 misc_tlv),
196SOC_SINGLE_TLV("Speaker Mixer Aux Playback Volume", AC97_REC_SEL, 8, 7, 1,
197 misc_tlv),
198
199SOC_SINGLE_TLV("Mono Mixer Voice Playback Volume", AC97_PCM, 4, 7, 1,
200 misc_tlv),
201SOC_SINGLE_TLV("Mono Mixer Aux Playback Volume", AC97_REC_SEL, 4, 7, 1,
202 misc_tlv),
203
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100204SOC_SINGLE("Aux Playback Headphone Volume", AC97_REC_SEL, 12, 7, 1),
205SOC_SINGLE("Aux Playback Master Volume", AC97_REC_SEL, 8, 7, 1),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100206
207SOC_ENUM("Bass Control", wm9713_enum[16]),
208SOC_SINGLE("Bass Cut-off Switch", AC97_GENERAL_PURPOSE, 12, 1, 1),
209SOC_SINGLE("Tone Cut-off Switch", AC97_GENERAL_PURPOSE, 4, 1, 1),
210SOC_SINGLE("Playback Attenuate (-6dB) Switch", AC97_GENERAL_PURPOSE, 6, 1, 0),
211SOC_SINGLE("Bass Volume", AC97_GENERAL_PURPOSE, 8, 15, 1),
212SOC_SINGLE("Tone Volume", AC97_GENERAL_PURPOSE, 0, 15, 1),
213
214SOC_SINGLE("3D Upper Cut-off Switch", AC97_REC_GAIN_MIC, 5, 1, 0),
215SOC_SINGLE("3D Lower Cut-off Switch", AC97_REC_GAIN_MIC, 4, 1, 0),
216SOC_SINGLE("3D Depth", AC97_REC_GAIN_MIC, 0, 15, 1),
217};
218
Mark Brown6bbcb452009-04-13 11:29:10 +0100219static int wm9713_voice_shutdown(struct snd_soc_dapm_widget *w,
220 struct snd_kcontrol *kcontrol, int event)
221{
222 struct snd_soc_codec *codec = w->codec;
223 u16 status, rate;
224
225 BUG_ON(event != SND_SOC_DAPM_PRE_PMD);
226
227 /* Gracefully shut down the voice interface. */
228 status = ac97_read(codec, AC97_EXTENDED_MID) | 0x1000;
229 rate = ac97_read(codec, AC97_HANDSET_RATE) & 0xF0FF;
230 ac97_write(codec, AC97_HANDSET_RATE, rate | 0x0200);
231 schedule_timeout_interruptible(msecs_to_jiffies(1));
232 ac97_write(codec, AC97_HANDSET_RATE, rate | 0x0F00);
233 ac97_write(codec, AC97_EXTENDED_MID, status);
234
235 return 0;
236}
237
238
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100239/* We have to create a fake left and right HP mixers because
240 * the codec only has a single control that is shared by both channels.
241 * This makes it impossible to determine the audio path using the current
242 * register map, thus we add a new (virtual) register to help determine the
243 * audio route within the device.
244 */
245static int mixer_event(struct snd_soc_dapm_widget *w,
246 struct snd_kcontrol *kcontrol, int event)
247{
248 u16 l, r, beep, tone, phone, rec, pcm, aux;
249
250 l = ac97_read(w->codec, HPL_MIXER);
251 r = ac97_read(w->codec, HPR_MIXER);
252 beep = ac97_read(w->codec, AC97_PC_BEEP);
253 tone = ac97_read(w->codec, AC97_MASTER_TONE);
254 phone = ac97_read(w->codec, AC97_PHONE);
255 rec = ac97_read(w->codec, AC97_REC_SEL);
256 pcm = ac97_read(w->codec, AC97_PCM);
257 aux = ac97_read(w->codec, AC97_AUX);
258
259 if (event & SND_SOC_DAPM_PRE_REG)
260 return 0;
261 if ((l & 0x1) || (r & 0x1))
262 ac97_write(w->codec, AC97_PC_BEEP, beep & 0x7fff);
263 else
264 ac97_write(w->codec, AC97_PC_BEEP, beep | 0x8000);
265
266 if ((l & 0x2) || (r & 0x2))
267 ac97_write(w->codec, AC97_MASTER_TONE, tone & 0x7fff);
268 else
269 ac97_write(w->codec, AC97_MASTER_TONE, tone | 0x8000);
270
271 if ((l & 0x4) || (r & 0x4))
272 ac97_write(w->codec, AC97_PHONE, phone & 0x7fff);
273 else
274 ac97_write(w->codec, AC97_PHONE, phone | 0x8000);
275
276 if ((l & 0x8) || (r & 0x8))
277 ac97_write(w->codec, AC97_REC_SEL, rec & 0x7fff);
278 else
279 ac97_write(w->codec, AC97_REC_SEL, rec | 0x8000);
280
281 if ((l & 0x10) || (r & 0x10))
282 ac97_write(w->codec, AC97_PCM, pcm & 0x7fff);
283 else
284 ac97_write(w->codec, AC97_PCM, pcm | 0x8000);
285
286 if ((l & 0x20) || (r & 0x20))
287 ac97_write(w->codec, AC97_AUX, aux & 0x7fff);
288 else
289 ac97_write(w->codec, AC97_AUX, aux | 0x8000);
290
291 return 0;
292}
293
294/* Left Headphone Mixers */
295static const struct snd_kcontrol_new wm9713_hpl_mixer_controls[] = {
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100296SOC_DAPM_SINGLE("Beep Playback Switch", HPL_MIXER, 5, 1, 0),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100297SOC_DAPM_SINGLE("Voice Playback Switch", HPL_MIXER, 4, 1, 0),
298SOC_DAPM_SINGLE("Aux Playback Switch", HPL_MIXER, 3, 1, 0),
299SOC_DAPM_SINGLE("PCM Playback Switch", HPL_MIXER, 2, 1, 0),
300SOC_DAPM_SINGLE("MonoIn Playback Switch", HPL_MIXER, 1, 1, 0),
301SOC_DAPM_SINGLE("Bypass Playback Switch", HPL_MIXER, 0, 1, 0),
302};
303
304/* Right Headphone Mixers */
305static const struct snd_kcontrol_new wm9713_hpr_mixer_controls[] = {
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100306SOC_DAPM_SINGLE("Beep Playback Switch", HPR_MIXER, 5, 1, 0),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100307SOC_DAPM_SINGLE("Voice Playback Switch", HPR_MIXER, 4, 1, 0),
308SOC_DAPM_SINGLE("Aux Playback Switch", HPR_MIXER, 3, 1, 0),
309SOC_DAPM_SINGLE("PCM Playback Switch", HPR_MIXER, 2, 1, 0),
310SOC_DAPM_SINGLE("MonoIn Playback Switch", HPR_MIXER, 1, 1, 0),
311SOC_DAPM_SINGLE("Bypass Playback Switch", HPR_MIXER, 0, 1, 0),
312};
313
314/* headphone capture mux */
315static const struct snd_kcontrol_new wm9713_hp_rec_mux_controls =
316SOC_DAPM_ENUM("Route", wm9713_enum[1]);
317
318/* headphone mic mux */
319static const struct snd_kcontrol_new wm9713_hp_mic_mux_controls =
320SOC_DAPM_ENUM("Route", wm9713_enum[0]);
321
322/* Speaker Mixer */
323static const struct snd_kcontrol_new wm9713_speaker_mixer_controls[] = {
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100324SOC_DAPM_SINGLE("Beep Playback Switch", AC97_AUX, 11, 1, 1),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100325SOC_DAPM_SINGLE("Voice Playback Switch", AC97_PCM, 11, 1, 1),
326SOC_DAPM_SINGLE("Aux Playback Switch", AC97_REC_SEL, 11, 1, 1),
327SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PHONE, 14, 1, 1),
328SOC_DAPM_SINGLE("MonoIn Playback Switch", AC97_MASTER_TONE, 14, 1, 1),
329SOC_DAPM_SINGLE("Bypass Playback Switch", AC97_PC_BEEP, 14, 1, 1),
330};
331
332/* Mono Mixer */
333static const struct snd_kcontrol_new wm9713_mono_mixer_controls[] = {
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100334SOC_DAPM_SINGLE("Beep Playback Switch", AC97_AUX, 7, 1, 1),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100335SOC_DAPM_SINGLE("Voice Playback Switch", AC97_PCM, 7, 1, 1),
336SOC_DAPM_SINGLE("Aux Playback Switch", AC97_REC_SEL, 7, 1, 1),
337SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PHONE, 13, 1, 1),
338SOC_DAPM_SINGLE("MonoIn Playback Switch", AC97_MASTER_TONE, 13, 1, 1),
339SOC_DAPM_SINGLE("Bypass Playback Switch", AC97_PC_BEEP, 13, 1, 1),
340SOC_DAPM_SINGLE("Mic 1 Sidetone Switch", AC97_LINE, 7, 1, 1),
341SOC_DAPM_SINGLE("Mic 2 Sidetone Switch", AC97_LINE, 6, 1, 1),
342};
343
344/* mono mic mux */
345static const struct snd_kcontrol_new wm9713_mono_mic_mux_controls =
346SOC_DAPM_ENUM("Route", wm9713_enum[2]);
347
348/* mono output mux */
349static const struct snd_kcontrol_new wm9713_mono_mux_controls =
350SOC_DAPM_ENUM("Route", wm9713_enum[7]);
351
352/* speaker left output mux */
353static const struct snd_kcontrol_new wm9713_hp_spkl_mux_controls =
354SOC_DAPM_ENUM("Route", wm9713_enum[8]);
355
356/* speaker right output mux */
357static const struct snd_kcontrol_new wm9713_hp_spkr_mux_controls =
358SOC_DAPM_ENUM("Route", wm9713_enum[9]);
359
360/* headphone left output mux */
361static const struct snd_kcontrol_new wm9713_hpl_out_mux_controls =
362SOC_DAPM_ENUM("Route", wm9713_enum[10]);
363
364/* headphone right output mux */
365static const struct snd_kcontrol_new wm9713_hpr_out_mux_controls =
366SOC_DAPM_ENUM("Route", wm9713_enum[11]);
367
368/* Out3 mux */
369static const struct snd_kcontrol_new wm9713_out3_mux_controls =
370SOC_DAPM_ENUM("Route", wm9713_enum[12]);
371
372/* Out4 mux */
373static const struct snd_kcontrol_new wm9713_out4_mux_controls =
374SOC_DAPM_ENUM("Route", wm9713_enum[13]);
375
376/* DAC inv mux 1 */
377static const struct snd_kcontrol_new wm9713_dac_inv1_mux_controls =
378SOC_DAPM_ENUM("Route", wm9713_enum[14]);
379
380/* DAC inv mux 2 */
381static const struct snd_kcontrol_new wm9713_dac_inv2_mux_controls =
382SOC_DAPM_ENUM("Route", wm9713_enum[15]);
383
384/* Capture source left */
385static const struct snd_kcontrol_new wm9713_rec_srcl_mux_controls =
386SOC_DAPM_ENUM("Route", wm9713_enum[3]);
387
388/* Capture source right */
389static const struct snd_kcontrol_new wm9713_rec_srcr_mux_controls =
390SOC_DAPM_ENUM("Route", wm9713_enum[4]);
391
392/* mic source */
393static const struct snd_kcontrol_new wm9713_mic_sel_mux_controls =
394SOC_DAPM_ENUM("Route", wm9713_enum[18]);
395
396/* mic source B virtual control */
397static const struct snd_kcontrol_new wm9713_micb_sel_mux_controls =
398SOC_DAPM_ENUM("Route", wm9713_enum[19]);
399
400static const struct snd_soc_dapm_widget wm9713_dapm_widgets[] = {
401SND_SOC_DAPM_MUX("Capture Headphone Mux", SND_SOC_NOPM, 0, 0,
402 &wm9713_hp_rec_mux_controls),
403SND_SOC_DAPM_MUX("Sidetone Mux", SND_SOC_NOPM, 0, 0,
404 &wm9713_hp_mic_mux_controls),
405SND_SOC_DAPM_MUX("Capture Mono Mux", SND_SOC_NOPM, 0, 0,
406 &wm9713_mono_mic_mux_controls),
407SND_SOC_DAPM_MUX("Mono Out Mux", SND_SOC_NOPM, 0, 0,
408 &wm9713_mono_mux_controls),
409SND_SOC_DAPM_MUX("Left Speaker Out Mux", SND_SOC_NOPM, 0, 0,
410 &wm9713_hp_spkl_mux_controls),
411SND_SOC_DAPM_MUX("Right Speaker Out Mux", SND_SOC_NOPM, 0, 0,
412 &wm9713_hp_spkr_mux_controls),
413SND_SOC_DAPM_MUX("Left Headphone Out Mux", SND_SOC_NOPM, 0, 0,
414 &wm9713_hpl_out_mux_controls),
415SND_SOC_DAPM_MUX("Right Headphone Out Mux", SND_SOC_NOPM, 0, 0,
416 &wm9713_hpr_out_mux_controls),
417SND_SOC_DAPM_MUX("Out 3 Mux", SND_SOC_NOPM, 0, 0,
418 &wm9713_out3_mux_controls),
419SND_SOC_DAPM_MUX("Out 4 Mux", SND_SOC_NOPM, 0, 0,
420 &wm9713_out4_mux_controls),
421SND_SOC_DAPM_MUX("DAC Inv Mux 1", SND_SOC_NOPM, 0, 0,
422 &wm9713_dac_inv1_mux_controls),
423SND_SOC_DAPM_MUX("DAC Inv Mux 2", SND_SOC_NOPM, 0, 0,
424 &wm9713_dac_inv2_mux_controls),
425SND_SOC_DAPM_MUX("Left Capture Source", SND_SOC_NOPM, 0, 0,
426 &wm9713_rec_srcl_mux_controls),
427SND_SOC_DAPM_MUX("Right Capture Source", SND_SOC_NOPM, 0, 0,
428 &wm9713_rec_srcr_mux_controls),
429SND_SOC_DAPM_MUX("Mic A Source", SND_SOC_NOPM, 0, 0,
430 &wm9713_mic_sel_mux_controls),
431SND_SOC_DAPM_MUX("Mic B Source", SND_SOC_NOPM, 0, 0,
432 &wm9713_micb_sel_mux_controls),
433SND_SOC_DAPM_MIXER_E("Left HP Mixer", AC97_EXTENDED_MID, 3, 1,
434 &wm9713_hpl_mixer_controls[0], ARRAY_SIZE(wm9713_hpl_mixer_controls),
435 mixer_event, SND_SOC_DAPM_POST_REG),
436SND_SOC_DAPM_MIXER_E("Right HP Mixer", AC97_EXTENDED_MID, 2, 1,
437 &wm9713_hpr_mixer_controls[0], ARRAY_SIZE(wm9713_hpr_mixer_controls),
438 mixer_event, SND_SOC_DAPM_POST_REG),
439SND_SOC_DAPM_MIXER("Mono Mixer", AC97_EXTENDED_MID, 0, 1,
440 &wm9713_mono_mixer_controls[0], ARRAY_SIZE(wm9713_mono_mixer_controls)),
441SND_SOC_DAPM_MIXER("Speaker Mixer", AC97_EXTENDED_MID, 1, 1,
442 &wm9713_speaker_mixer_controls[0],
443 ARRAY_SIZE(wm9713_speaker_mixer_controls)),
444SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", AC97_EXTENDED_MID, 7, 1),
445SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", AC97_EXTENDED_MID, 6, 1),
446SND_SOC_DAPM_MIXER("AC97 Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
447SND_SOC_DAPM_MIXER("HP Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
448SND_SOC_DAPM_MIXER("Line Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
449SND_SOC_DAPM_MIXER("Capture Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
Mark Brown6bbcb452009-04-13 11:29:10 +0100450SND_SOC_DAPM_DAC_E("Voice DAC", "Voice Playback", AC97_EXTENDED_MID, 12, 1,
451 wm9713_voice_shutdown, SND_SOC_DAPM_PRE_PMD),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100452SND_SOC_DAPM_DAC("Aux DAC", "Aux Playback", AC97_EXTENDED_MID, 11, 1),
Mark Brownccbc3012008-09-15 15:51:13 +0100453SND_SOC_DAPM_PGA("Left ADC", AC97_EXTENDED_MID, 5, 1, NULL, 0),
454SND_SOC_DAPM_PGA("Right ADC", AC97_EXTENDED_MID, 4, 1, NULL, 0),
455SND_SOC_DAPM_ADC("Left HiFi ADC", "Left HiFi Capture", SND_SOC_NOPM, 0, 0),
456SND_SOC_DAPM_ADC("Right HiFi ADC", "Right HiFi Capture", SND_SOC_NOPM, 0, 0),
457SND_SOC_DAPM_ADC("Left Voice ADC", "Left Voice Capture", SND_SOC_NOPM, 0, 0),
458SND_SOC_DAPM_ADC("Right Voice ADC", "Right Voice Capture", SND_SOC_NOPM, 0, 0),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100459SND_SOC_DAPM_PGA("Left Headphone", AC97_EXTENDED_MSTATUS, 10, 1, NULL, 0),
460SND_SOC_DAPM_PGA("Right Headphone", AC97_EXTENDED_MSTATUS, 9, 1, NULL, 0),
461SND_SOC_DAPM_PGA("Left Speaker", AC97_EXTENDED_MSTATUS, 8, 1, NULL, 0),
462SND_SOC_DAPM_PGA("Right Speaker", AC97_EXTENDED_MSTATUS, 7, 1, NULL, 0),
463SND_SOC_DAPM_PGA("Out 3", AC97_EXTENDED_MSTATUS, 11, 1, NULL, 0),
464SND_SOC_DAPM_PGA("Out 4", AC97_EXTENDED_MSTATUS, 12, 1, NULL, 0),
465SND_SOC_DAPM_PGA("Mono Out", AC97_EXTENDED_MSTATUS, 13, 1, NULL, 0),
466SND_SOC_DAPM_PGA("Left Line In", AC97_EXTENDED_MSTATUS, 6, 1, NULL, 0),
467SND_SOC_DAPM_PGA("Right Line In", AC97_EXTENDED_MSTATUS, 5, 1, NULL, 0),
468SND_SOC_DAPM_PGA("Mono In", AC97_EXTENDED_MSTATUS, 4, 1, NULL, 0),
469SND_SOC_DAPM_PGA("Mic A PGA", AC97_EXTENDED_MSTATUS, 3, 1, NULL, 0),
470SND_SOC_DAPM_PGA("Mic B PGA", AC97_EXTENDED_MSTATUS, 2, 1, NULL, 0),
471SND_SOC_DAPM_PGA("Mic A Pre Amp", AC97_EXTENDED_MSTATUS, 1, 1, NULL, 0),
472SND_SOC_DAPM_PGA("Mic B Pre Amp", AC97_EXTENDED_MSTATUS, 0, 1, NULL, 0),
473SND_SOC_DAPM_MICBIAS("Mic Bias", AC97_EXTENDED_MSTATUS, 14, 1),
474SND_SOC_DAPM_OUTPUT("MONO"),
475SND_SOC_DAPM_OUTPUT("HPL"),
476SND_SOC_DAPM_OUTPUT("HPR"),
477SND_SOC_DAPM_OUTPUT("SPKL"),
478SND_SOC_DAPM_OUTPUT("SPKR"),
479SND_SOC_DAPM_OUTPUT("OUT3"),
480SND_SOC_DAPM_OUTPUT("OUT4"),
481SND_SOC_DAPM_INPUT("LINEL"),
482SND_SOC_DAPM_INPUT("LINER"),
483SND_SOC_DAPM_INPUT("MONOIN"),
484SND_SOC_DAPM_INPUT("PCBEEP"),
485SND_SOC_DAPM_INPUT("MIC1"),
486SND_SOC_DAPM_INPUT("MIC2A"),
487SND_SOC_DAPM_INPUT("MIC2B"),
488SND_SOC_DAPM_VMID("VMID"),
489};
490
Mark Browna65f0562008-05-13 14:54:43 +0200491static const struct snd_soc_dapm_route audio_map[] = {
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100492 /* left HP mixer */
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100493 {"Left HP Mixer", "Beep Playback Switch", "PCBEEP"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100494 {"Left HP Mixer", "Voice Playback Switch", "Voice DAC"},
495 {"Left HP Mixer", "Aux Playback Switch", "Aux DAC"},
496 {"Left HP Mixer", "Bypass Playback Switch", "Left Line In"},
497 {"Left HP Mixer", "PCM Playback Switch", "Left DAC"},
498 {"Left HP Mixer", "MonoIn Playback Switch", "Mono In"},
499 {"Left HP Mixer", NULL, "Capture Headphone Mux"},
500
501 /* right HP mixer */
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100502 {"Right HP Mixer", "Beep Playback Switch", "PCBEEP"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100503 {"Right HP Mixer", "Voice Playback Switch", "Voice DAC"},
504 {"Right HP Mixer", "Aux Playback Switch", "Aux DAC"},
505 {"Right HP Mixer", "Bypass Playback Switch", "Right Line In"},
506 {"Right HP Mixer", "PCM Playback Switch", "Right DAC"},
507 {"Right HP Mixer", "MonoIn Playback Switch", "Mono In"},
508 {"Right HP Mixer", NULL, "Capture Headphone Mux"},
509
510 /* virtual mixer - mixes left & right channels for spk and mono */
511 {"AC97 Mixer", NULL, "Left DAC"},
512 {"AC97 Mixer", NULL, "Right DAC"},
513 {"Line Mixer", NULL, "Right Line In"},
514 {"Line Mixer", NULL, "Left Line In"},
515 {"HP Mixer", NULL, "Left HP Mixer"},
516 {"HP Mixer", NULL, "Right HP Mixer"},
517 {"Capture Mixer", NULL, "Left Capture Source"},
518 {"Capture Mixer", NULL, "Right Capture Source"},
519
520 /* speaker mixer */
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100521 {"Speaker Mixer", "Beep Playback Switch", "PCBEEP"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100522 {"Speaker Mixer", "Voice Playback Switch", "Voice DAC"},
523 {"Speaker Mixer", "Aux Playback Switch", "Aux DAC"},
524 {"Speaker Mixer", "Bypass Playback Switch", "Line Mixer"},
525 {"Speaker Mixer", "PCM Playback Switch", "AC97 Mixer"},
526 {"Speaker Mixer", "MonoIn Playback Switch", "Mono In"},
527
528 /* mono mixer */
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100529 {"Mono Mixer", "Beep Playback Switch", "PCBEEP"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100530 {"Mono Mixer", "Voice Playback Switch", "Voice DAC"},
531 {"Mono Mixer", "Aux Playback Switch", "Aux DAC"},
532 {"Mono Mixer", "Bypass Playback Switch", "Line Mixer"},
533 {"Mono Mixer", "PCM Playback Switch", "AC97 Mixer"},
Robert Jarzmikc0bbf482008-03-18 12:08:35 +0100534 {"Mono Mixer", "Mic 1 Sidetone Switch", "Mic A PGA"},
535 {"Mono Mixer", "Mic 2 Sidetone Switch", "Mic B PGA"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100536 {"Mono Mixer", NULL, "Capture Mono Mux"},
537
538 /* DAC inv mux 1 */
539 {"DAC Inv Mux 1", "Mono", "Mono Mixer"},
540 {"DAC Inv Mux 1", "Speaker", "Speaker Mixer"},
541 {"DAC Inv Mux 1", "Left Headphone", "Left HP Mixer"},
542 {"DAC Inv Mux 1", "Right Headphone", "Right HP Mixer"},
543 {"DAC Inv Mux 1", "Headphone Mono", "HP Mixer"},
544
545 /* DAC inv mux 2 */
546 {"DAC Inv Mux 2", "Mono", "Mono Mixer"},
547 {"DAC Inv Mux 2", "Speaker", "Speaker Mixer"},
548 {"DAC Inv Mux 2", "Left Headphone", "Left HP Mixer"},
549 {"DAC Inv Mux 2", "Right Headphone", "Right HP Mixer"},
550 {"DAC Inv Mux 2", "Headphone Mono", "HP Mixer"},
551
552 /* headphone left mux */
553 {"Left Headphone Out Mux", "Headphone", "Left HP Mixer"},
554
555 /* headphone right mux */
556 {"Right Headphone Out Mux", "Headphone", "Right HP Mixer"},
557
558 /* speaker left mux */
559 {"Left Speaker Out Mux", "Headphone", "Left HP Mixer"},
560 {"Left Speaker Out Mux", "Speaker", "Speaker Mixer"},
561 {"Left Speaker Out Mux", "Inv", "DAC Inv Mux 1"},
562
563 /* speaker right mux */
564 {"Right Speaker Out Mux", "Headphone", "Right HP Mixer"},
565 {"Right Speaker Out Mux", "Speaker", "Speaker Mixer"},
566 {"Right Speaker Out Mux", "Inv", "DAC Inv Mux 2"},
567
568 /* mono mux */
569 {"Mono Out Mux", "Mono", "Mono Mixer"},
570 {"Mono Out Mux", "Inv", "DAC Inv Mux 1"},
571
572 /* out 3 mux */
573 {"Out 3 Mux", "Inv 1", "DAC Inv Mux 1"},
574
575 /* out 4 mux */
576 {"Out 4 Mux", "Inv 2", "DAC Inv Mux 2"},
577
578 /* output pga */
579 {"HPL", NULL, "Left Headphone"},
580 {"Left Headphone", NULL, "Left Headphone Out Mux"},
581 {"HPR", NULL, "Right Headphone"},
582 {"Right Headphone", NULL, "Right Headphone Out Mux"},
583 {"OUT3", NULL, "Out 3"},
584 {"Out 3", NULL, "Out 3 Mux"},
585 {"OUT4", NULL, "Out 4"},
586 {"Out 4", NULL, "Out 4 Mux"},
587 {"SPKL", NULL, "Left Speaker"},
588 {"Left Speaker", NULL, "Left Speaker Out Mux"},
589 {"SPKR", NULL, "Right Speaker"},
590 {"Right Speaker", NULL, "Right Speaker Out Mux"},
591 {"MONO", NULL, "Mono Out"},
592 {"Mono Out", NULL, "Mono Out Mux"},
593
594 /* input pga */
595 {"Left Line In", NULL, "LINEL"},
596 {"Right Line In", NULL, "LINER"},
597 {"Mono In", NULL, "MONOIN"},
598 {"Mic A PGA", NULL, "Mic A Pre Amp"},
599 {"Mic B PGA", NULL, "Mic B Pre Amp"},
600
601 /* left capture select */
602 {"Left Capture Source", "Mic 1", "Mic A Pre Amp"},
603 {"Left Capture Source", "Mic 2", "Mic B Pre Amp"},
604 {"Left Capture Source", "Line", "LINEL"},
605 {"Left Capture Source", "Mono In", "MONOIN"},
606 {"Left Capture Source", "Headphone", "Left HP Mixer"},
607 {"Left Capture Source", "Speaker", "Speaker Mixer"},
608 {"Left Capture Source", "Mono Out", "Mono Mixer"},
609
610 /* right capture select */
611 {"Right Capture Source", "Mic 1", "Mic A Pre Amp"},
612 {"Right Capture Source", "Mic 2", "Mic B Pre Amp"},
613 {"Right Capture Source", "Line", "LINER"},
614 {"Right Capture Source", "Mono In", "MONOIN"},
615 {"Right Capture Source", "Headphone", "Right HP Mixer"},
616 {"Right Capture Source", "Speaker", "Speaker Mixer"},
617 {"Right Capture Source", "Mono Out", "Mono Mixer"},
618
619 /* left ADC */
620 {"Left ADC", NULL, "Left Capture Source"},
Mark Brownccbc3012008-09-15 15:51:13 +0100621 {"Left Voice ADC", NULL, "Left ADC"},
622 {"Left HiFi ADC", NULL, "Left ADC"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100623
624 /* right ADC */
625 {"Right ADC", NULL, "Right Capture Source"},
Mark Brownccbc3012008-09-15 15:51:13 +0100626 {"Right Voice ADC", NULL, "Right ADC"},
627 {"Right HiFi ADC", NULL, "Right ADC"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100628
629 /* mic */
630 {"Mic A Pre Amp", NULL, "Mic A Source"},
631 {"Mic A Source", "Mic 1", "MIC1"},
632 {"Mic A Source", "Mic 2 A", "MIC2A"},
633 {"Mic A Source", "Mic 2 B", "Mic B Source"},
634 {"Mic B Pre Amp", "MPB", "Mic B Source"},
635 {"Mic B Source", NULL, "MIC2B"},
636
637 /* headphone capture */
638 {"Capture Headphone Mux", "Stereo", "Capture Mixer"},
639 {"Capture Headphone Mux", "Left", "Left Capture Source"},
640 {"Capture Headphone Mux", "Right", "Right Capture Source"},
641
642 /* mono capture */
643 {"Capture Mono Mux", "Stereo", "Capture Mixer"},
644 {"Capture Mono Mux", "Left", "Left Capture Source"},
645 {"Capture Mono Mux", "Right", "Right Capture Source"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100646};
647
648static int wm9713_add_widgets(struct snd_soc_codec *codec)
649{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200650 struct snd_soc_dapm_context *dapm = &codec->dapm;
651
652 snd_soc_dapm_new_controls(dapm, wm9713_dapm_widgets,
Mark Browna65f0562008-05-13 14:54:43 +0200653 ARRAY_SIZE(wm9713_dapm_widgets));
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100654
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200655 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100656
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100657 return 0;
658}
659
660static unsigned int ac97_read(struct snd_soc_codec *codec,
661 unsigned int reg)
662{
663 u16 *cache = codec->reg_cache;
664
665 if (reg == AC97_RESET || reg == AC97_GPIO_STATUS ||
666 reg == AC97_VENDOR_ID1 || reg == AC97_VENDOR_ID2 ||
667 reg == AC97_CD)
668 return soc_ac97_ops.read(codec->ac97, reg);
669 else {
670 reg = reg >> 1;
671
Ian Molton91432e92009-01-17 17:44:23 +0000672 if (reg >= (ARRAY_SIZE(wm9713_reg)))
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100673 return -EIO;
674
675 return cache[reg];
676 }
677}
678
679static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
680 unsigned int val)
681{
682 u16 *cache = codec->reg_cache;
683 if (reg < 0x7c)
684 soc_ac97_ops.write(codec->ac97, reg, val);
685 reg = reg >> 1;
Ian Molton91432e92009-01-17 17:44:23 +0000686 if (reg < (ARRAY_SIZE(wm9713_reg)))
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100687 cache[reg] = val;
688
689 return 0;
690}
691
692/* PLL divisors */
693struct _pll_div {
694 u32 divsel:1;
695 u32 divctl:1;
696 u32 lf:1;
697 u32 n:4;
698 u32 k:24;
699};
700
701/* The size in bits of the PLL divide multiplied by 10
702 * to allow rounding later */
703#define FIXED_PLL_SIZE ((1 << 22) * 10)
704
705static void pll_factors(struct _pll_div *pll_div, unsigned int source)
706{
707 u64 Kpart;
708 unsigned int K, Ndiv, Nmod, target;
709
710 /* The the PLL output is always 98.304MHz. */
711 target = 98304000;
712
713 /* If the input frequency is over 14.4MHz then scale it down. */
714 if (source > 14400000) {
715 source >>= 1;
716 pll_div->divsel = 1;
717
718 if (source > 14400000) {
719 source >>= 1;
720 pll_div->divctl = 1;
721 } else
722 pll_div->divctl = 0;
723
724 } else {
725 pll_div->divsel = 0;
726 pll_div->divctl = 0;
727 }
728
729 /* Low frequency sources require an additional divide in the
730 * loop.
731 */
732 if (source < 8192000) {
733 pll_div->lf = 1;
734 target >>= 2;
735 } else
736 pll_div->lf = 0;
737
738 Ndiv = target / source;
739 if ((Ndiv < 5) || (Ndiv > 12))
740 printk(KERN_WARNING
Roel Kluin449bd542009-05-27 17:08:39 -0700741 "WM9713 PLL N value %u out of recommended range!\n",
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100742 Ndiv);
743
744 pll_div->n = Ndiv;
745 Nmod = target % source;
746 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
747
748 do_div(Kpart, source);
749
750 K = Kpart & 0xFFFFFFFF;
751
752 /* Check if we need to round */
753 if ((K % 10) >= 5)
754 K += 5;
755
756 /* Move down to proper range now rounding is done */
757 K /= 10;
758
759 pll_div->k = K;
760}
761
762/**
763 * Please note that changing the PLL input frequency may require
764 * resynchronisation with the AC97 controller.
765 */
766static int wm9713_set_pll(struct snd_soc_codec *codec,
767 int pll_id, unsigned int freq_in, unsigned int freq_out)
768{
Mark Brownb2c812e2010-04-14 15:35:19 +0900769 struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100770 u16 reg, reg2;
771 struct _pll_div pll_div;
772
773 /* turn PLL off ? */
Mark Brownc42f69b2009-01-16 16:31:03 +0000774 if (freq_in == 0) {
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100775 /* disable PLL power and select ext source */
776 reg = ac97_read(codec, AC97_HANDSET_RATE);
777 ac97_write(codec, AC97_HANDSET_RATE, reg | 0x0080);
778 reg = ac97_read(codec, AC97_EXTENDED_MID);
779 ac97_write(codec, AC97_EXTENDED_MID, reg | 0x0200);
Mark Brownc42f69b2009-01-16 16:31:03 +0000780 wm9713->pll_in = 0;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100781 return 0;
782 }
783
784 pll_factors(&pll_div, freq_in);
785
786 if (pll_div.k == 0) {
787 reg = (pll_div.n << 12) | (pll_div.lf << 11) |
788 (pll_div.divsel << 9) | (pll_div.divctl << 8);
789 ac97_write(codec, AC97_LINE1_LEVEL, reg);
790 } else {
791 /* write the fractional k to the reg 0x46 pages */
792 reg2 = (pll_div.n << 12) | (pll_div.lf << 11) | (1 << 10) |
793 (pll_div.divsel << 9) | (pll_div.divctl << 8);
794
795 /* K [21:20] */
796 reg = reg2 | (0x5 << 4) | (pll_div.k >> 20);
797 ac97_write(codec, AC97_LINE1_LEVEL, reg);
798
799 /* K [19:16] */
800 reg = reg2 | (0x4 << 4) | ((pll_div.k >> 16) & 0xf);
801 ac97_write(codec, AC97_LINE1_LEVEL, reg);
802
803 /* K [15:12] */
804 reg = reg2 | (0x3 << 4) | ((pll_div.k >> 12) & 0xf);
805 ac97_write(codec, AC97_LINE1_LEVEL, reg);
806
807 /* K [11:8] */
808 reg = reg2 | (0x2 << 4) | ((pll_div.k >> 8) & 0xf);
809 ac97_write(codec, AC97_LINE1_LEVEL, reg);
810
811 /* K [7:4] */
812 reg = reg2 | (0x1 << 4) | ((pll_div.k >> 4) & 0xf);
813 ac97_write(codec, AC97_LINE1_LEVEL, reg);
814
815 reg = reg2 | (0x0 << 4) | (pll_div.k & 0xf); /* K [3:0] */
816 ac97_write(codec, AC97_LINE1_LEVEL, reg);
817 }
818
819 /* turn PLL on and select as source */
820 reg = ac97_read(codec, AC97_EXTENDED_MID);
821 ac97_write(codec, AC97_EXTENDED_MID, reg & 0xfdff);
822 reg = ac97_read(codec, AC97_HANDSET_RATE);
823 ac97_write(codec, AC97_HANDSET_RATE, reg & 0xff7f);
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100824 wm9713->pll_in = freq_in;
825
826 /* wait 10ms AC97 link frames for the link to stabilise */
827 schedule_timeout_interruptible(msecs_to_jiffies(10));
828 return 0;
829}
830
Mark Brown85488032009-09-05 18:52:16 +0100831static int wm9713_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
832 int source, unsigned int freq_in, unsigned int freq_out)
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100833{
834 struct snd_soc_codec *codec = codec_dai->codec;
835 return wm9713_set_pll(codec, pll_id, freq_in, freq_out);
836}
837
838/*
839 * Tristate the PCM DAI lines, tristate can be disabled by calling
840 * wm9713_set_dai_fmt()
841 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100842static int wm9713_set_dai_tristate(struct snd_soc_dai *codec_dai,
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100843 int tristate)
844{
845 struct snd_soc_codec *codec = codec_dai->codec;
846 u16 reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0x9fff;
847
848 if (tristate)
849 ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
850
851 return 0;
852}
853
854/*
855 * Configure WM9713 clock dividers.
856 * Voice DAC needs 256 FS
857 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100858static int wm9713_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100859 int div_id, int div)
860{
861 struct snd_soc_codec *codec = codec_dai->codec;
862 u16 reg;
863
864 switch (div_id) {
865 case WM9713_PCMCLK_DIV:
866 reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xf0ff;
867 ac97_write(codec, AC97_HANDSET_RATE, reg | div);
868 break;
869 case WM9713_CLKA_MULT:
870 reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xfffd;
871 ac97_write(codec, AC97_HANDSET_RATE, reg | div);
872 break;
873 case WM9713_CLKB_MULT:
874 reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xfffb;
875 ac97_write(codec, AC97_HANDSET_RATE, reg | div);
876 break;
877 case WM9713_HIFI_DIV:
878 reg = ac97_read(codec, AC97_HANDSET_RATE) & 0x8fff;
879 ac97_write(codec, AC97_HANDSET_RATE, reg | div);
880 break;
881 case WM9713_PCMBCLK_DIV:
882 reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0xf1ff;
883 ac97_write(codec, AC97_CENTER_LFE_MASTER, reg | div);
884 break;
885 case WM9713_PCMCLK_PLL_DIV:
886 reg = ac97_read(codec, AC97_LINE1_LEVEL) & 0xff80;
887 ac97_write(codec, AC97_LINE1_LEVEL, reg | 0x60 | div);
888 break;
889 case WM9713_HIFI_PLL_DIV:
890 reg = ac97_read(codec, AC97_LINE1_LEVEL) & 0xff80;
891 ac97_write(codec, AC97_LINE1_LEVEL, reg | 0x70 | div);
892 break;
893 default:
894 return -EINVAL;
895 }
896
897 return 0;
898}
899
Liam Girdwoode550e172008-07-07 16:07:52 +0100900static int wm9713_set_dai_fmt(struct snd_soc_dai *codec_dai,
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100901 unsigned int fmt)
902{
903 struct snd_soc_codec *codec = codec_dai->codec;
904 u16 gpio = ac97_read(codec, AC97_GPIO_CFG) & 0xffc5;
905 u16 reg = 0x8000;
906
907 /* clock masters */
908 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
909 case SND_SOC_DAIFMT_CBM_CFM:
910 reg |= 0x4000;
911 gpio |= 0x0010;
912 break;
913 case SND_SOC_DAIFMT_CBM_CFS:
914 reg |= 0x6000;
915 gpio |= 0x0018;
916 break;
917 case SND_SOC_DAIFMT_CBS_CFS:
Mark Brownd133b0c2008-06-11 13:47:11 +0100918 reg |= 0x2000;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100919 gpio |= 0x001a;
920 break;
921 case SND_SOC_DAIFMT_CBS_CFM:
922 gpio |= 0x0012;
923 break;
924 }
925
926 /* clock inversion */
927 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
928 case SND_SOC_DAIFMT_IB_IF:
929 reg |= 0x00c0;
930 break;
931 case SND_SOC_DAIFMT_IB_NF:
932 reg |= 0x0080;
933 break;
934 case SND_SOC_DAIFMT_NB_IF:
935 reg |= 0x0040;
936 break;
937 }
938
939 /* DAI format */
940 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
941 case SND_SOC_DAIFMT_I2S:
942 reg |= 0x0002;
943 break;
944 case SND_SOC_DAIFMT_RIGHT_J:
945 break;
946 case SND_SOC_DAIFMT_LEFT_J:
947 reg |= 0x0001;
948 break;
949 case SND_SOC_DAIFMT_DSP_A:
950 reg |= 0x0003;
951 break;
952 case SND_SOC_DAIFMT_DSP_B:
953 reg |= 0x0043;
954 break;
955 }
956
957 ac97_write(codec, AC97_GPIO_CFG, gpio);
958 ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
959 return 0;
960}
961
962static int wm9713_pcm_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000963 struct snd_pcm_hw_params *params,
964 struct snd_soc_dai *dai)
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100965{
Mark Brown55b8bac2008-11-24 14:05:29 +0000966 struct snd_soc_codec *codec = dai->codec;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100967 u16 reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0xfff3;
968
969 switch (params_format(params)) {
970 case SNDRV_PCM_FORMAT_S16_LE:
971 break;
972 case SNDRV_PCM_FORMAT_S20_3LE:
973 reg |= 0x0004;
974 break;
975 case SNDRV_PCM_FORMAT_S24_LE:
976 reg |= 0x0008;
977 break;
978 case SNDRV_PCM_FORMAT_S32_LE:
979 reg |= 0x000c;
980 break;
981 }
982
983 /* enable PCM interface in master mode */
984 ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
985 return 0;
986}
987
Mark Browndee89c42008-11-18 22:11:38 +0000988static int ac97_hifi_prepare(struct snd_pcm_substream *substream,
989 struct snd_soc_dai *dai)
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100990{
Mark Brown55b8bac2008-11-24 14:05:29 +0000991 struct snd_soc_codec *codec = dai->codec;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100992 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100993 int reg;
994 u16 vra;
995
996 vra = ac97_read(codec, AC97_EXTENDED_STATUS);
997 ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
998
999 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1000 reg = AC97_PCM_FRONT_DAC_RATE;
1001 else
1002 reg = AC97_PCM_LR_ADC_RATE;
1003
1004 return ac97_write(codec, reg, runtime->rate);
1005}
1006
Mark Browndee89c42008-11-18 22:11:38 +00001007static int ac97_aux_prepare(struct snd_pcm_substream *substream,
1008 struct snd_soc_dai *dai)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001009{
Mark Brown55b8bac2008-11-24 14:05:29 +00001010 struct snd_soc_codec *codec = dai->codec;
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001011 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001012 u16 vra, xsle;
1013
1014 vra = ac97_read(codec, AC97_EXTENDED_STATUS);
1015 ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
1016 xsle = ac97_read(codec, AC97_PCI_SID);
1017 ac97_write(codec, AC97_PCI_SID, xsle | 0x8000);
1018
1019 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
1020 return -ENODEV;
1021
1022 return ac97_write(codec, AC97_PCM_SURR_DAC_RATE, runtime->rate);
1023}
1024
Mark Brown54155522008-07-02 11:51:19 +01001025#define WM9713_RATES (SNDRV_PCM_RATE_8000 | \
1026 SNDRV_PCM_RATE_11025 | \
1027 SNDRV_PCM_RATE_22050 | \
1028 SNDRV_PCM_RATE_44100 | \
1029 SNDRV_PCM_RATE_48000)
1030
1031#define WM9713_PCM_RATES (SNDRV_PCM_RATE_8000 | \
1032 SNDRV_PCM_RATE_11025 | \
1033 SNDRV_PCM_RATE_16000 | \
1034 SNDRV_PCM_RATE_22050 | \
1035 SNDRV_PCM_RATE_44100 | \
1036 SNDRV_PCM_RATE_48000)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001037
1038#define WM9713_PCM_FORMATS \
1039 (SNDRV_PCM_FORMAT_S16_LE | SNDRV_PCM_FORMAT_S20_3LE | \
1040 SNDRV_PCM_FORMAT_S24_LE)
1041
Eric Miao6335d052009-03-03 09:41:00 +08001042static struct snd_soc_dai_ops wm9713_dai_ops_hifi = {
1043 .prepare = ac97_hifi_prepare,
1044 .set_clkdiv = wm9713_set_dai_clkdiv,
1045 .set_pll = wm9713_set_dai_pll,
1046};
1047
1048static struct snd_soc_dai_ops wm9713_dai_ops_aux = {
1049 .prepare = ac97_aux_prepare,
1050 .set_clkdiv = wm9713_set_dai_clkdiv,
1051 .set_pll = wm9713_set_dai_pll,
1052};
1053
1054static struct snd_soc_dai_ops wm9713_dai_ops_voice = {
1055 .hw_params = wm9713_pcm_hw_params,
Eric Miao6335d052009-03-03 09:41:00 +08001056 .set_clkdiv = wm9713_set_dai_clkdiv,
1057 .set_pll = wm9713_set_dai_pll,
1058 .set_fmt = wm9713_set_dai_fmt,
1059 .set_tristate = wm9713_set_dai_tristate,
1060};
1061
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001062static struct snd_soc_dai_driver wm9713_dai[] = {
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001063{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001064 .name = "wm9713-hifi",
Mark Brown3ba9e10a2008-11-24 18:01:05 +00001065 .ac97_control = 1,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001066 .playback = {
1067 .stream_name = "HiFi Playback",
1068 .channels_min = 1,
1069 .channels_max = 2,
1070 .rates = WM9713_RATES,
Mark Brown33f503c2009-05-02 12:24:55 +01001071 .formats = SND_SOC_STD_AC97_FMTS,},
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001072 .capture = {
1073 .stream_name = "HiFi Capture",
1074 .channels_min = 1,
1075 .channels_max = 2,
1076 .rates = WM9713_RATES,
Mark Brown33f503c2009-05-02 12:24:55 +01001077 .formats = SND_SOC_STD_AC97_FMTS,},
Eric Miao6335d052009-03-03 09:41:00 +08001078 .ops = &wm9713_dai_ops_hifi,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001079 },
1080 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001081 .name = "wm9713-aux",
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001082 .playback = {
1083 .stream_name = "Aux Playback",
1084 .channels_min = 1,
1085 .channels_max = 1,
1086 .rates = WM9713_RATES,
Mark Brown33f503c2009-05-02 12:24:55 +01001087 .formats = SND_SOC_STD_AC97_FMTS,},
Eric Miao6335d052009-03-03 09:41:00 +08001088 .ops = &wm9713_dai_ops_aux,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001089 },
1090 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001091 .name = "wm9713-voice",
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001092 .playback = {
1093 .stream_name = "Voice Playback",
1094 .channels_min = 1,
1095 .channels_max = 1,
Mark Brown54155522008-07-02 11:51:19 +01001096 .rates = WM9713_PCM_RATES,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001097 .formats = WM9713_PCM_FORMATS,},
1098 .capture = {
1099 .stream_name = "Voice Capture",
1100 .channels_min = 1,
1101 .channels_max = 2,
Mark Brown54155522008-07-02 11:51:19 +01001102 .rates = WM9713_PCM_RATES,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001103 .formats = WM9713_PCM_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +08001104 .ops = &wm9713_dai_ops_voice,
Mark Brownf4976112009-04-13 10:53:02 +01001105 .symmetric_rates = 1,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001106 },
1107};
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001108
1109int wm9713_reset(struct snd_soc_codec *codec, int try_warm)
1110{
1111 if (try_warm && soc_ac97_ops.warm_reset) {
1112 soc_ac97_ops.warm_reset(codec->ac97);
Mark Brownabb68c22008-06-13 16:24:04 +01001113 if (ac97_read(codec, 0) == wm9713_reg[0])
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001114 return 1;
1115 }
1116
1117 soc_ac97_ops.reset(codec->ac97);
Mark Browne775f6c2008-10-28 15:04:35 +00001118 if (soc_ac97_ops.warm_reset)
1119 soc_ac97_ops.warm_reset(codec->ac97);
Mark Brownabb68c22008-06-13 16:24:04 +01001120 if (ac97_read(codec, 0) != wm9713_reg[0])
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001121 return -EIO;
1122 return 0;
1123}
1124EXPORT_SYMBOL_GPL(wm9713_reset);
1125
Mark Brown0be98982008-05-19 12:31:28 +02001126static int wm9713_set_bias_level(struct snd_soc_codec *codec,
1127 enum snd_soc_bias_level level)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001128{
1129 u16 reg;
1130
Mark Brown0be98982008-05-19 12:31:28 +02001131 switch (level) {
1132 case SND_SOC_BIAS_ON:
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001133 /* enable thermal shutdown */
1134 reg = ac97_read(codec, AC97_EXTENDED_MID) & 0x1bff;
1135 ac97_write(codec, AC97_EXTENDED_MID, reg);
1136 break;
Mark Brown0be98982008-05-19 12:31:28 +02001137 case SND_SOC_BIAS_PREPARE:
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001138 break;
Mark Brown0be98982008-05-19 12:31:28 +02001139 case SND_SOC_BIAS_STANDBY:
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001140 /* enable master bias and vmid */
1141 reg = ac97_read(codec, AC97_EXTENDED_MID) & 0x3bff;
1142 ac97_write(codec, AC97_EXTENDED_MID, reg);
1143 ac97_write(codec, AC97_POWERDOWN, 0x0000);
1144 break;
Mark Brown0be98982008-05-19 12:31:28 +02001145 case SND_SOC_BIAS_OFF:
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001146 /* disable everything including AC link */
1147 ac97_write(codec, AC97_EXTENDED_MID, 0xffff);
1148 ac97_write(codec, AC97_EXTENDED_MSTATUS, 0xffff);
1149 ac97_write(codec, AC97_POWERDOWN, 0xffff);
1150 break;
1151 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001152 codec->dapm.bias_level = level;
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001153 return 0;
1154}
1155
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001156static int wm9713_soc_suspend(struct snd_soc_codec *codec,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001157 pm_message_t state)
1158{
Mark Brown87b57fe2008-04-14 15:27:30 +02001159 u16 reg;
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001160
Mark Brown87b57fe2008-04-14 15:27:30 +02001161 /* Disable everything except touchpanel - that will be handled
1162 * by the touch driver and left disabled if touch is not in
1163 * use. */
1164 reg = ac97_read(codec, AC97_EXTENDED_MID);
1165 ac97_write(codec, AC97_EXTENDED_MID, reg | 0x7fff);
1166 ac97_write(codec, AC97_EXTENDED_MSTATUS, 0xffff);
1167 ac97_write(codec, AC97_POWERDOWN, 0x6f00);
1168 ac97_write(codec, AC97_POWERDOWN, 0xffff);
1169
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001170 return 0;
1171}
1172
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001173static int wm9713_soc_resume(struct snd_soc_codec *codec)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001174{
Mark Brownb2c812e2010-04-14 15:35:19 +09001175 struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001176 int i, ret;
1177 u16 *cache = codec->reg_cache;
1178
1179 ret = wm9713_reset(codec, 1);
1180 if (ret < 0) {
1181 printk(KERN_ERR "could not reset AC97 codec\n");
1182 return ret;
1183 }
1184
Mark Brown0be98982008-05-19 12:31:28 +02001185 wm9713_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001186
1187 /* do we need to re-start the PLL ? */
Mark Brownc42f69b2009-01-16 16:31:03 +00001188 if (wm9713->pll_in)
1189 wm9713_set_pll(codec, 0, wm9713->pll_in, 0);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001190
1191 /* only synchronise the codec if warm reset failed */
1192 if (ret == 0) {
1193 for (i = 2; i < ARRAY_SIZE(wm9713_reg) << 1; i += 2) {
1194 if (i == AC97_POWERDOWN || i == AC97_EXTENDED_MID ||
1195 i == AC97_EXTENDED_MSTATUS || i > 0x66)
1196 continue;
1197 soc_ac97_ops.write(codec->ac97, i, cache[i>>1]);
1198 }
1199 }
1200
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001201 return ret;
1202}
1203
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001204static int wm9713_soc_probe(struct snd_soc_codec *codec)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001205{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001206 struct wm9713_priv *wm9713;
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001207 int ret = 0, reg;
1208
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001209 wm9713 = kzalloc(sizeof(struct wm9713_priv), GFP_KERNEL);
1210 if (wm9713 == NULL)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001211 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001212 snd_soc_codec_set_drvdata(codec, wm9713);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001213
1214 ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
1215 if (ret < 0)
1216 goto codec_err;
1217
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001218 /* do a cold reset for the controller and then try
1219 * a warm reset followed by an optional cold reset for codec */
1220 wm9713_reset(codec, 0);
1221 ret = wm9713_reset(codec, 1);
1222 if (ret < 0) {
Mark Brown39639fa2008-11-21 14:28:49 +00001223 printk(KERN_ERR "Failed to reset WM9713: AC97 link error\n");
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001224 goto reset_err;
1225 }
1226
Mark Brown0be98982008-05-19 12:31:28 +02001227 wm9713_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001228
1229 /* unmute the adc - move to kcontrol */
1230 reg = ac97_read(codec, AC97_CD) & 0x7fff;
1231 ac97_write(codec, AC97_CD, reg);
1232
Ian Molton3e8e1952009-01-09 00:23:21 +00001233 snd_soc_add_controls(codec, wm9713_snd_ac97_controls,
1234 ARRAY_SIZE(wm9713_snd_ac97_controls));
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001235 wm9713_add_widgets(codec);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001236
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001237 return 0;
1238
1239reset_err:
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001240 snd_soc_free_ac97_codec(codec);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001241codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001242 kfree(wm9713);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001243 return ret;
1244}
1245
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001246static int wm9713_soc_remove(struct snd_soc_codec *codec)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001247{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001248 struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001249 snd_soc_free_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001250 kfree(wm9713);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001251 return 0;
1252}
1253
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001254static struct snd_soc_codec_driver soc_codec_dev_wm9713 = {
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001255 .probe = wm9713_soc_probe,
1256 .remove = wm9713_soc_remove,
1257 .suspend = wm9713_soc_suspend,
1258 .resume = wm9713_soc_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001259 .read = ac97_read,
1260 .write = ac97_write,
1261 .set_bias_level = wm9713_set_bias_level,
Dimitris Papastamose5eec342010-09-10 18:14:56 +01001262 .reg_cache_size = ARRAY_SIZE(wm9713_reg),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001263 .reg_word_size = sizeof(u16),
1264 .reg_cache_step = 2,
1265 .reg_cache_default = wm9713_reg,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001266};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001267
1268static __devinit int wm9713_probe(struct platform_device *pdev)
1269{
1270 return snd_soc_register_codec(&pdev->dev,
1271 &soc_codec_dev_wm9713, wm9713_dai, ARRAY_SIZE(wm9713_dai));
1272}
1273
1274static int __devexit wm9713_remove(struct platform_device *pdev)
1275{
1276 snd_soc_unregister_codec(&pdev->dev);
1277 return 0;
1278}
1279
1280static struct platform_driver wm9713_codec_driver = {
1281 .driver = {
1282 .name = "wm9713-codec",
1283 .owner = THIS_MODULE,
1284 },
1285
1286 .probe = wm9713_probe,
1287 .remove = __devexit_p(wm9713_remove),
1288};
1289
1290static int __init wm9713_init(void)
1291{
1292 return platform_driver_register(&wm9713_codec_driver);
1293}
1294module_init(wm9713_init);
1295
1296static void __exit wm9713_exit(void)
1297{
1298 platform_driver_unregister(&wm9713_codec_driver);
1299}
1300module_exit(wm9713_exit);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001301
1302MODULE_DESCRIPTION("ASoC WM9713/WM9714 driver");
1303MODULE_AUTHOR("Liam Girdwood");
1304MODULE_LICENSE("GPL");