blob: 3eb19fb71d17209e0bccc071ed3f481471201276 [file] [log] [blame]
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001/*
2 * wm9713.c -- ALSA Soc WM9713 codec support
3 *
Mark Brown656baae2012-05-23 12:39:07 +01004 * Copyright 2006-10 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>
Liam Girdwood83ac08c2008-02-15 16:43:11 +010029
30#include "wm9713.h"
31
Liam Girdwood83ac08c2008-02-15 16:43:11 +010032struct wm9713_priv {
33 u32 pll_in; /* PLL input frequency */
Liam Girdwood83ac08c2008-02-15 16:43:11 +010034};
35
36static unsigned int ac97_read(struct snd_soc_codec *codec,
37 unsigned int reg);
38static int ac97_write(struct snd_soc_codec *codec,
39 unsigned int reg, unsigned int val);
40
41/*
42 * WM9713 register cache
43 * Reg 0x3c bit 15 is used by touch driver.
44 */
45static const u16 wm9713_reg[] = {
46 0x6174, 0x8080, 0x8080, 0x8080,
47 0xc880, 0xe808, 0xe808, 0x0808,
48 0x00da, 0x8000, 0xd600, 0xaaa0,
49 0xaaa0, 0xaaa0, 0x0000, 0x0000,
50 0x0f0f, 0x0040, 0x0000, 0x7f00,
51 0x0405, 0x0410, 0xbb80, 0xbb80,
52 0x0000, 0xbb80, 0x0000, 0x4523,
53 0x0000, 0x2000, 0x7eff, 0xffff,
54 0x0000, 0x0000, 0x0080, 0x0000,
55 0x0000, 0x0000, 0xfffe, 0xffff,
56 0x0000, 0x0000, 0x0000, 0xfffe,
57 0x4000, 0x0000, 0x0000, 0x0000,
58 0xb032, 0x3e00, 0x0000, 0x0000,
59 0x0000, 0x0000, 0x0000, 0x0000,
60 0x0000, 0x0000, 0x0000, 0x0006,
61 0x0001, 0x0000, 0x574d, 0x4c13,
62 0x0000, 0x0000, 0x0000
63};
64
65/* virtual HP mixers regs */
66#define HPL_MIXER 0x80
67#define HPR_MIXER 0x82
68#define MICB_MUX 0x82
69
70static const char *wm9713_mic_mixer[] = {"Stereo", "Mic 1", "Mic 2", "Mute"};
71static const char *wm9713_rec_mux[] = {"Stereo", "Left", "Right", "Mute"};
72static const char *wm9713_rec_src[] =
73 {"Mic 1", "Mic 2", "Line", "Mono In", "Headphone", "Speaker",
74 "Mono Out", "Zh"};
75static const char *wm9713_rec_gain[] = {"+1.5dB Steps", "+0.75dB Steps"};
76static const char *wm9713_alc_select[] = {"None", "Left", "Right", "Stereo"};
77static const char *wm9713_mono_pga[] = {"Vmid", "Zh", "Mono", "Inv",
78 "Mono Vmid", "Inv Vmid"};
79static const char *wm9713_spk_pga[] =
80 {"Vmid", "Zh", "Headphone", "Speaker", "Inv", "Headphone Vmid",
81 "Speaker Vmid", "Inv Vmid"};
82static const char *wm9713_hp_pga[] = {"Vmid", "Zh", "Headphone",
83 "Headphone Vmid"};
84static const char *wm9713_out3_pga[] = {"Vmid", "Zh", "Inv 1", "Inv 1 Vmid"};
85static const char *wm9713_out4_pga[] = {"Vmid", "Zh", "Inv 2", "Inv 2 Vmid"};
86static const char *wm9713_dac_inv[] =
87 {"Off", "Mono", "Speaker", "Left Headphone", "Right Headphone",
88 "Headphone Mono", "NC", "Vmid"};
89static const char *wm9713_bass[] = {"Linear Control", "Adaptive Boost"};
90static const char *wm9713_ng_type[] = {"Constant Gain", "Mute"};
91static const char *wm9713_mic_select[] = {"Mic 1", "Mic 2 A", "Mic 2 B"};
92static const char *wm9713_micb_select[] = {"MPB", "MPA"};
93
94static const struct soc_enum wm9713_enum[] = {
95SOC_ENUM_SINGLE(AC97_LINE, 3, 4, wm9713_mic_mixer), /* record mic mixer 0 */
96SOC_ENUM_SINGLE(AC97_VIDEO, 14, 4, wm9713_rec_mux), /* record mux hp 1 */
97SOC_ENUM_SINGLE(AC97_VIDEO, 9, 4, wm9713_rec_mux), /* record mux mono 2 */
98SOC_ENUM_SINGLE(AC97_VIDEO, 3, 8, wm9713_rec_src), /* record mux left 3 */
99SOC_ENUM_SINGLE(AC97_VIDEO, 0, 8, wm9713_rec_src), /* record mux right 4*/
100SOC_ENUM_DOUBLE(AC97_CD, 14, 6, 2, wm9713_rec_gain), /* record step size 5 */
101SOC_ENUM_SINGLE(AC97_PCI_SVID, 14, 4, wm9713_alc_select), /* alc source select 6*/
102SOC_ENUM_SINGLE(AC97_REC_GAIN, 14, 4, wm9713_mono_pga), /* mono input select 7 */
103SOC_ENUM_SINGLE(AC97_REC_GAIN, 11, 8, wm9713_spk_pga), /* speaker left input select 8 */
104SOC_ENUM_SINGLE(AC97_REC_GAIN, 8, 8, wm9713_spk_pga), /* speaker right input select 9 */
105SOC_ENUM_SINGLE(AC97_REC_GAIN, 6, 3, wm9713_hp_pga), /* headphone left input 10 */
106SOC_ENUM_SINGLE(AC97_REC_GAIN, 4, 3, wm9713_hp_pga), /* headphone right input 11 */
107SOC_ENUM_SINGLE(AC97_REC_GAIN, 2, 4, wm9713_out3_pga), /* out 3 source 12 */
108SOC_ENUM_SINGLE(AC97_REC_GAIN, 0, 4, wm9713_out4_pga), /* out 4 source 13 */
109SOC_ENUM_SINGLE(AC97_REC_GAIN_MIC, 13, 8, wm9713_dac_inv), /* dac invert 1 14 */
110SOC_ENUM_SINGLE(AC97_REC_GAIN_MIC, 10, 8, wm9713_dac_inv), /* dac invert 2 15 */
111SOC_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 15, 2, wm9713_bass), /* bass control 16 */
112SOC_ENUM_SINGLE(AC97_PCI_SVID, 5, 2, wm9713_ng_type), /* noise gate type 17 */
113SOC_ENUM_SINGLE(AC97_3D_CONTROL, 12, 3, wm9713_mic_select), /* mic selection 18 */
114SOC_ENUM_SINGLE(MICB_MUX, 0, 2, wm9713_micb_select), /* mic selection 19 */
115};
116
Mark Browne03a8d22010-01-28 12:36:07 +0000117static const DECLARE_TLV_DB_SCALE(out_tlv, -4650, 150, 0);
118static const DECLARE_TLV_DB_SCALE(main_tlv, -3450, 150, 0);
119static const DECLARE_TLV_DB_SCALE(misc_tlv, -1500, 300, 0);
120static unsigned int mic_tlv[] = {
121 TLV_DB_RANGE_HEAD(2),
122 0, 2, TLV_DB_SCALE_ITEM(1200, 600, 0),
123 3, 3, TLV_DB_SCALE_ITEM(3000, 0, 0),
124};
125
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100126static const struct snd_kcontrol_new wm9713_snd_ac97_controls[] = {
Mark Browne03a8d22010-01-28 12:36:07 +0000127SOC_DOUBLE_TLV("Speaker Playback Volume", AC97_MASTER, 8, 0, 31, 1, out_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100128SOC_DOUBLE("Speaker Playback Switch", AC97_MASTER, 15, 7, 1, 1),
Mark Browne03a8d22010-01-28 12:36:07 +0000129SOC_DOUBLE_TLV("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1,
130 out_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100131SOC_DOUBLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 7, 1, 1),
Mark Browne03a8d22010-01-28 12:36:07 +0000132SOC_DOUBLE_TLV("Line In Volume", AC97_PC_BEEP, 8, 0, 31, 1, main_tlv),
133SOC_DOUBLE_TLV("PCM Playback Volume", AC97_PHONE, 8, 0, 31, 1, main_tlv),
134SOC_SINGLE_TLV("Mic 1 Volume", AC97_MIC, 8, 31, 1, main_tlv),
135SOC_SINGLE_TLV("Mic 2 Volume", AC97_MIC, 0, 31, 1, main_tlv),
136SOC_SINGLE_TLV("Mic 1 Preamp Volume", AC97_3D_CONTROL, 10, 3, 0, mic_tlv),
137SOC_SINGLE_TLV("Mic 2 Preamp Volume", AC97_3D_CONTROL, 12, 3, 0, mic_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100138
139SOC_SINGLE("Mic Boost (+20dB) Switch", AC97_LINE, 5, 1, 0),
140SOC_SINGLE("Mic Headphone Mixer Volume", AC97_LINE, 0, 7, 1),
141
142SOC_SINGLE("Capture Switch", AC97_CD, 15, 1, 1),
143SOC_ENUM("Capture Volume Steps", wm9713_enum[5]),
144SOC_DOUBLE("Capture Volume", AC97_CD, 8, 0, 31, 0),
145SOC_SINGLE("Capture ZC Switch", AC97_CD, 7, 1, 0),
146
Mark Browne03a8d22010-01-28 12:36:07 +0000147SOC_SINGLE_TLV("Capture to Headphone Volume", AC97_VIDEO, 11, 7, 1, misc_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100148SOC_SINGLE("Capture to Mono Boost (+20dB) Switch", AC97_VIDEO, 8, 1, 0),
149SOC_SINGLE("Capture ADC Boost (+20dB) Switch", AC97_VIDEO, 6, 1, 0),
150
151SOC_SINGLE("ALC Target Volume", AC97_CODEC_CLASS_REV, 12, 15, 0),
152SOC_SINGLE("ALC Hold Time", AC97_CODEC_CLASS_REV, 8, 15, 0),
Mark Brown13d622b2008-10-30 12:37:10 +0000153SOC_SINGLE("ALC Decay Time", AC97_CODEC_CLASS_REV, 4, 15, 0),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100154SOC_SINGLE("ALC Attack Time", AC97_CODEC_CLASS_REV, 0, 15, 0),
155SOC_ENUM("ALC Function", wm9713_enum[6]),
156SOC_SINGLE("ALC Max Volume", AC97_PCI_SVID, 11, 7, 0),
157SOC_SINGLE("ALC ZC Timeout", AC97_PCI_SVID, 9, 3, 0),
158SOC_SINGLE("ALC ZC Switch", AC97_PCI_SVID, 8, 1, 0),
159SOC_SINGLE("ALC NG Switch", AC97_PCI_SVID, 7, 1, 0),
160SOC_ENUM("ALC NG Type", wm9713_enum[17]),
161SOC_SINGLE("ALC NG Threshold", AC97_PCI_SVID, 0, 31, 0),
162
163SOC_DOUBLE("Speaker Playback ZC Switch", AC97_MASTER, 14, 6, 1, 0),
164SOC_DOUBLE("Headphone Playback ZC Switch", AC97_HEADPHONE, 14, 6, 1, 0),
165
166SOC_SINGLE("Out4 Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
167SOC_SINGLE("Out4 Playback ZC Switch", AC97_MASTER_MONO, 14, 1, 0),
Mark Browne03a8d22010-01-28 12:36:07 +0000168SOC_SINGLE_TLV("Out4 Playback Volume", AC97_MASTER_MONO, 8, 31, 1, out_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100169
170SOC_SINGLE("Out3 Playback Switch", AC97_MASTER_MONO, 7, 1, 1),
171SOC_SINGLE("Out3 Playback ZC Switch", AC97_MASTER_MONO, 6, 1, 0),
Mark Browne03a8d22010-01-28 12:36:07 +0000172SOC_SINGLE_TLV("Out3 Playback Volume", AC97_MASTER_MONO, 0, 31, 1, out_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100173
Mark Browne03a8d22010-01-28 12:36:07 +0000174SOC_SINGLE_TLV("Mono Capture Volume", AC97_MASTER_TONE, 8, 31, 1, main_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100175SOC_SINGLE("Mono Playback Switch", AC97_MASTER_TONE, 7, 1, 1),
176SOC_SINGLE("Mono Playback ZC Switch", AC97_MASTER_TONE, 6, 1, 0),
Mark Browne03a8d22010-01-28 12:36:07 +0000177SOC_SINGLE_TLV("Mono Playback Volume", AC97_MASTER_TONE, 0, 31, 1, out_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100178
Mark Browne03a8d22010-01-28 12:36:07 +0000179SOC_SINGLE_TLV("Headphone Mixer Beep Playback Volume", AC97_AUX, 12, 7, 1,
180 misc_tlv),
181SOC_SINGLE_TLV("Speaker Mixer Beep Playback Volume", AC97_AUX, 8, 7, 1,
182 misc_tlv),
183SOC_SINGLE_TLV("Mono Mixer Beep Playback Volume", AC97_AUX, 4, 7, 1, misc_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100184
Mark Browne03a8d22010-01-28 12:36:07 +0000185SOC_SINGLE_TLV("Voice Playback Headphone Volume", AC97_PCM, 12, 7, 1,
186 misc_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100187SOC_SINGLE("Voice Playback Master Volume", AC97_PCM, 8, 7, 1),
188SOC_SINGLE("Voice Playback Mono Volume", AC97_PCM, 4, 7, 1),
189
Mark Browne03a8d22010-01-28 12:36:07 +0000190SOC_SINGLE_TLV("Headphone Mixer Aux Playback Volume", AC97_REC_SEL, 12, 7, 1,
191 misc_tlv),
192
193SOC_SINGLE_TLV("Speaker Mixer Voice Playback Volume", AC97_PCM, 8, 7, 1,
194 misc_tlv),
195SOC_SINGLE_TLV("Speaker Mixer Aux Playback Volume", AC97_REC_SEL, 8, 7, 1,
196 misc_tlv),
197
198SOC_SINGLE_TLV("Mono Mixer Voice Playback Volume", AC97_PCM, 4, 7, 1,
199 misc_tlv),
200SOC_SINGLE_TLV("Mono Mixer Aux Playback Volume", AC97_REC_SEL, 4, 7, 1,
201 misc_tlv),
202
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100203SOC_SINGLE("Aux Playback Headphone Volume", AC97_REC_SEL, 12, 7, 1),
204SOC_SINGLE("Aux Playback Master Volume", AC97_REC_SEL, 8, 7, 1),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100205
206SOC_ENUM("Bass Control", wm9713_enum[16]),
207SOC_SINGLE("Bass Cut-off Switch", AC97_GENERAL_PURPOSE, 12, 1, 1),
208SOC_SINGLE("Tone Cut-off Switch", AC97_GENERAL_PURPOSE, 4, 1, 1),
209SOC_SINGLE("Playback Attenuate (-6dB) Switch", AC97_GENERAL_PURPOSE, 6, 1, 0),
210SOC_SINGLE("Bass Volume", AC97_GENERAL_PURPOSE, 8, 15, 1),
211SOC_SINGLE("Tone Volume", AC97_GENERAL_PURPOSE, 0, 15, 1),
212
213SOC_SINGLE("3D Upper Cut-off Switch", AC97_REC_GAIN_MIC, 5, 1, 0),
214SOC_SINGLE("3D Lower Cut-off Switch", AC97_REC_GAIN_MIC, 4, 1, 0),
215SOC_SINGLE("3D Depth", AC97_REC_GAIN_MIC, 0, 15, 1),
216};
217
Mark Brown6bbcb452009-04-13 11:29:10 +0100218static int wm9713_voice_shutdown(struct snd_soc_dapm_widget *w,
219 struct snd_kcontrol *kcontrol, int event)
220{
221 struct snd_soc_codec *codec = w->codec;
222 u16 status, rate;
223
224 BUG_ON(event != SND_SOC_DAPM_PRE_PMD);
225
226 /* Gracefully shut down the voice interface. */
227 status = ac97_read(codec, AC97_EXTENDED_MID) | 0x1000;
228 rate = ac97_read(codec, AC97_HANDSET_RATE) & 0xF0FF;
229 ac97_write(codec, AC97_HANDSET_RATE, rate | 0x0200);
230 schedule_timeout_interruptible(msecs_to_jiffies(1));
231 ac97_write(codec, AC97_HANDSET_RATE, rate | 0x0F00);
232 ac97_write(codec, AC97_EXTENDED_MID, status);
233
234 return 0;
235}
236
237
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100238/* We have to create a fake left and right HP mixers because
239 * the codec only has a single control that is shared by both channels.
240 * This makes it impossible to determine the audio path using the current
241 * register map, thus we add a new (virtual) register to help determine the
242 * audio route within the device.
243 */
244static int mixer_event(struct snd_soc_dapm_widget *w,
245 struct snd_kcontrol *kcontrol, int event)
246{
247 u16 l, r, beep, tone, phone, rec, pcm, aux;
248
249 l = ac97_read(w->codec, HPL_MIXER);
250 r = ac97_read(w->codec, HPR_MIXER);
251 beep = ac97_read(w->codec, AC97_PC_BEEP);
252 tone = ac97_read(w->codec, AC97_MASTER_TONE);
253 phone = ac97_read(w->codec, AC97_PHONE);
254 rec = ac97_read(w->codec, AC97_REC_SEL);
255 pcm = ac97_read(w->codec, AC97_PCM);
256 aux = ac97_read(w->codec, AC97_AUX);
257
258 if (event & SND_SOC_DAPM_PRE_REG)
259 return 0;
260 if ((l & 0x1) || (r & 0x1))
261 ac97_write(w->codec, AC97_PC_BEEP, beep & 0x7fff);
262 else
263 ac97_write(w->codec, AC97_PC_BEEP, beep | 0x8000);
264
265 if ((l & 0x2) || (r & 0x2))
266 ac97_write(w->codec, AC97_MASTER_TONE, tone & 0x7fff);
267 else
268 ac97_write(w->codec, AC97_MASTER_TONE, tone | 0x8000);
269
270 if ((l & 0x4) || (r & 0x4))
271 ac97_write(w->codec, AC97_PHONE, phone & 0x7fff);
272 else
273 ac97_write(w->codec, AC97_PHONE, phone | 0x8000);
274
275 if ((l & 0x8) || (r & 0x8))
276 ac97_write(w->codec, AC97_REC_SEL, rec & 0x7fff);
277 else
278 ac97_write(w->codec, AC97_REC_SEL, rec | 0x8000);
279
280 if ((l & 0x10) || (r & 0x10))
281 ac97_write(w->codec, AC97_PCM, pcm & 0x7fff);
282 else
283 ac97_write(w->codec, AC97_PCM, pcm | 0x8000);
284
285 if ((l & 0x20) || (r & 0x20))
286 ac97_write(w->codec, AC97_AUX, aux & 0x7fff);
287 else
288 ac97_write(w->codec, AC97_AUX, aux | 0x8000);
289
290 return 0;
291}
292
293/* Left Headphone Mixers */
294static const struct snd_kcontrol_new wm9713_hpl_mixer_controls[] = {
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100295SOC_DAPM_SINGLE("Beep Playback Switch", HPL_MIXER, 5, 1, 0),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100296SOC_DAPM_SINGLE("Voice Playback Switch", HPL_MIXER, 4, 1, 0),
297SOC_DAPM_SINGLE("Aux Playback Switch", HPL_MIXER, 3, 1, 0),
298SOC_DAPM_SINGLE("PCM Playback Switch", HPL_MIXER, 2, 1, 0),
299SOC_DAPM_SINGLE("MonoIn Playback Switch", HPL_MIXER, 1, 1, 0),
300SOC_DAPM_SINGLE("Bypass Playback Switch", HPL_MIXER, 0, 1, 0),
301};
302
303/* Right Headphone Mixers */
304static const struct snd_kcontrol_new wm9713_hpr_mixer_controls[] = {
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100305SOC_DAPM_SINGLE("Beep Playback Switch", HPR_MIXER, 5, 1, 0),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100306SOC_DAPM_SINGLE("Voice Playback Switch", HPR_MIXER, 4, 1, 0),
307SOC_DAPM_SINGLE("Aux Playback Switch", HPR_MIXER, 3, 1, 0),
308SOC_DAPM_SINGLE("PCM Playback Switch", HPR_MIXER, 2, 1, 0),
309SOC_DAPM_SINGLE("MonoIn Playback Switch", HPR_MIXER, 1, 1, 0),
310SOC_DAPM_SINGLE("Bypass Playback Switch", HPR_MIXER, 0, 1, 0),
311};
312
313/* headphone capture mux */
314static const struct snd_kcontrol_new wm9713_hp_rec_mux_controls =
315SOC_DAPM_ENUM("Route", wm9713_enum[1]);
316
317/* headphone mic mux */
318static const struct snd_kcontrol_new wm9713_hp_mic_mux_controls =
319SOC_DAPM_ENUM("Route", wm9713_enum[0]);
320
321/* Speaker Mixer */
322static const struct snd_kcontrol_new wm9713_speaker_mixer_controls[] = {
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100323SOC_DAPM_SINGLE("Beep Playback Switch", AC97_AUX, 11, 1, 1),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100324SOC_DAPM_SINGLE("Voice Playback Switch", AC97_PCM, 11, 1, 1),
325SOC_DAPM_SINGLE("Aux Playback Switch", AC97_REC_SEL, 11, 1, 1),
326SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PHONE, 14, 1, 1),
327SOC_DAPM_SINGLE("MonoIn Playback Switch", AC97_MASTER_TONE, 14, 1, 1),
328SOC_DAPM_SINGLE("Bypass Playback Switch", AC97_PC_BEEP, 14, 1, 1),
329};
330
331/* Mono Mixer */
332static const struct snd_kcontrol_new wm9713_mono_mixer_controls[] = {
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100333SOC_DAPM_SINGLE("Beep Playback Switch", AC97_AUX, 7, 1, 1),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100334SOC_DAPM_SINGLE("Voice Playback Switch", AC97_PCM, 7, 1, 1),
335SOC_DAPM_SINGLE("Aux Playback Switch", AC97_REC_SEL, 7, 1, 1),
336SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PHONE, 13, 1, 1),
337SOC_DAPM_SINGLE("MonoIn Playback Switch", AC97_MASTER_TONE, 13, 1, 1),
338SOC_DAPM_SINGLE("Bypass Playback Switch", AC97_PC_BEEP, 13, 1, 1),
339SOC_DAPM_SINGLE("Mic 1 Sidetone Switch", AC97_LINE, 7, 1, 1),
340SOC_DAPM_SINGLE("Mic 2 Sidetone Switch", AC97_LINE, 6, 1, 1),
341};
342
343/* mono mic mux */
344static const struct snd_kcontrol_new wm9713_mono_mic_mux_controls =
345SOC_DAPM_ENUM("Route", wm9713_enum[2]);
346
347/* mono output mux */
348static const struct snd_kcontrol_new wm9713_mono_mux_controls =
349SOC_DAPM_ENUM("Route", wm9713_enum[7]);
350
351/* speaker left output mux */
352static const struct snd_kcontrol_new wm9713_hp_spkl_mux_controls =
353SOC_DAPM_ENUM("Route", wm9713_enum[8]);
354
355/* speaker right output mux */
356static const struct snd_kcontrol_new wm9713_hp_spkr_mux_controls =
357SOC_DAPM_ENUM("Route", wm9713_enum[9]);
358
359/* headphone left output mux */
360static const struct snd_kcontrol_new wm9713_hpl_out_mux_controls =
361SOC_DAPM_ENUM("Route", wm9713_enum[10]);
362
363/* headphone right output mux */
364static const struct snd_kcontrol_new wm9713_hpr_out_mux_controls =
365SOC_DAPM_ENUM("Route", wm9713_enum[11]);
366
367/* Out3 mux */
368static const struct snd_kcontrol_new wm9713_out3_mux_controls =
369SOC_DAPM_ENUM("Route", wm9713_enum[12]);
370
371/* Out4 mux */
372static const struct snd_kcontrol_new wm9713_out4_mux_controls =
373SOC_DAPM_ENUM("Route", wm9713_enum[13]);
374
375/* DAC inv mux 1 */
376static const struct snd_kcontrol_new wm9713_dac_inv1_mux_controls =
377SOC_DAPM_ENUM("Route", wm9713_enum[14]);
378
379/* DAC inv mux 2 */
380static const struct snd_kcontrol_new wm9713_dac_inv2_mux_controls =
381SOC_DAPM_ENUM("Route", wm9713_enum[15]);
382
383/* Capture source left */
384static const struct snd_kcontrol_new wm9713_rec_srcl_mux_controls =
385SOC_DAPM_ENUM("Route", wm9713_enum[3]);
386
387/* Capture source right */
388static const struct snd_kcontrol_new wm9713_rec_srcr_mux_controls =
389SOC_DAPM_ENUM("Route", wm9713_enum[4]);
390
391/* mic source */
392static const struct snd_kcontrol_new wm9713_mic_sel_mux_controls =
393SOC_DAPM_ENUM("Route", wm9713_enum[18]);
394
395/* mic source B virtual control */
396static const struct snd_kcontrol_new wm9713_micb_sel_mux_controls =
397SOC_DAPM_ENUM("Route", wm9713_enum[19]);
398
399static const struct snd_soc_dapm_widget wm9713_dapm_widgets[] = {
400SND_SOC_DAPM_MUX("Capture Headphone Mux", SND_SOC_NOPM, 0, 0,
401 &wm9713_hp_rec_mux_controls),
402SND_SOC_DAPM_MUX("Sidetone Mux", SND_SOC_NOPM, 0, 0,
403 &wm9713_hp_mic_mux_controls),
404SND_SOC_DAPM_MUX("Capture Mono Mux", SND_SOC_NOPM, 0, 0,
405 &wm9713_mono_mic_mux_controls),
406SND_SOC_DAPM_MUX("Mono Out Mux", SND_SOC_NOPM, 0, 0,
407 &wm9713_mono_mux_controls),
408SND_SOC_DAPM_MUX("Left Speaker Out Mux", SND_SOC_NOPM, 0, 0,
409 &wm9713_hp_spkl_mux_controls),
410SND_SOC_DAPM_MUX("Right Speaker Out Mux", SND_SOC_NOPM, 0, 0,
411 &wm9713_hp_spkr_mux_controls),
412SND_SOC_DAPM_MUX("Left Headphone Out Mux", SND_SOC_NOPM, 0, 0,
413 &wm9713_hpl_out_mux_controls),
414SND_SOC_DAPM_MUX("Right Headphone Out Mux", SND_SOC_NOPM, 0, 0,
415 &wm9713_hpr_out_mux_controls),
416SND_SOC_DAPM_MUX("Out 3 Mux", SND_SOC_NOPM, 0, 0,
417 &wm9713_out3_mux_controls),
418SND_SOC_DAPM_MUX("Out 4 Mux", SND_SOC_NOPM, 0, 0,
419 &wm9713_out4_mux_controls),
420SND_SOC_DAPM_MUX("DAC Inv Mux 1", SND_SOC_NOPM, 0, 0,
421 &wm9713_dac_inv1_mux_controls),
422SND_SOC_DAPM_MUX("DAC Inv Mux 2", SND_SOC_NOPM, 0, 0,
423 &wm9713_dac_inv2_mux_controls),
424SND_SOC_DAPM_MUX("Left Capture Source", SND_SOC_NOPM, 0, 0,
425 &wm9713_rec_srcl_mux_controls),
426SND_SOC_DAPM_MUX("Right Capture Source", SND_SOC_NOPM, 0, 0,
427 &wm9713_rec_srcr_mux_controls),
428SND_SOC_DAPM_MUX("Mic A Source", SND_SOC_NOPM, 0, 0,
429 &wm9713_mic_sel_mux_controls),
430SND_SOC_DAPM_MUX("Mic B Source", SND_SOC_NOPM, 0, 0,
431 &wm9713_micb_sel_mux_controls),
432SND_SOC_DAPM_MIXER_E("Left HP Mixer", AC97_EXTENDED_MID, 3, 1,
433 &wm9713_hpl_mixer_controls[0], ARRAY_SIZE(wm9713_hpl_mixer_controls),
434 mixer_event, SND_SOC_DAPM_POST_REG),
435SND_SOC_DAPM_MIXER_E("Right HP Mixer", AC97_EXTENDED_MID, 2, 1,
436 &wm9713_hpr_mixer_controls[0], ARRAY_SIZE(wm9713_hpr_mixer_controls),
437 mixer_event, SND_SOC_DAPM_POST_REG),
438SND_SOC_DAPM_MIXER("Mono Mixer", AC97_EXTENDED_MID, 0, 1,
439 &wm9713_mono_mixer_controls[0], ARRAY_SIZE(wm9713_mono_mixer_controls)),
440SND_SOC_DAPM_MIXER("Speaker Mixer", AC97_EXTENDED_MID, 1, 1,
441 &wm9713_speaker_mixer_controls[0],
442 ARRAY_SIZE(wm9713_speaker_mixer_controls)),
443SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", AC97_EXTENDED_MID, 7, 1),
444SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", AC97_EXTENDED_MID, 6, 1),
445SND_SOC_DAPM_MIXER("AC97 Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
446SND_SOC_DAPM_MIXER("HP Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
447SND_SOC_DAPM_MIXER("Line Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
448SND_SOC_DAPM_MIXER("Capture Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
Mark Brown6bbcb452009-04-13 11:29:10 +0100449SND_SOC_DAPM_DAC_E("Voice DAC", "Voice Playback", AC97_EXTENDED_MID, 12, 1,
450 wm9713_voice_shutdown, SND_SOC_DAPM_PRE_PMD),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100451SND_SOC_DAPM_DAC("Aux DAC", "Aux Playback", AC97_EXTENDED_MID, 11, 1),
Mark Brownccbc3012008-09-15 15:51:13 +0100452SND_SOC_DAPM_PGA("Left ADC", AC97_EXTENDED_MID, 5, 1, NULL, 0),
453SND_SOC_DAPM_PGA("Right ADC", AC97_EXTENDED_MID, 4, 1, NULL, 0),
454SND_SOC_DAPM_ADC("Left HiFi ADC", "Left HiFi Capture", SND_SOC_NOPM, 0, 0),
455SND_SOC_DAPM_ADC("Right HiFi ADC", "Right HiFi Capture", SND_SOC_NOPM, 0, 0),
456SND_SOC_DAPM_ADC("Left Voice ADC", "Left Voice Capture", SND_SOC_NOPM, 0, 0),
457SND_SOC_DAPM_ADC("Right Voice ADC", "Right Voice Capture", SND_SOC_NOPM, 0, 0),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100458SND_SOC_DAPM_PGA("Left Headphone", AC97_EXTENDED_MSTATUS, 10, 1, NULL, 0),
459SND_SOC_DAPM_PGA("Right Headphone", AC97_EXTENDED_MSTATUS, 9, 1, NULL, 0),
460SND_SOC_DAPM_PGA("Left Speaker", AC97_EXTENDED_MSTATUS, 8, 1, NULL, 0),
461SND_SOC_DAPM_PGA("Right Speaker", AC97_EXTENDED_MSTATUS, 7, 1, NULL, 0),
462SND_SOC_DAPM_PGA("Out 3", AC97_EXTENDED_MSTATUS, 11, 1, NULL, 0),
463SND_SOC_DAPM_PGA("Out 4", AC97_EXTENDED_MSTATUS, 12, 1, NULL, 0),
464SND_SOC_DAPM_PGA("Mono Out", AC97_EXTENDED_MSTATUS, 13, 1, NULL, 0),
465SND_SOC_DAPM_PGA("Left Line In", AC97_EXTENDED_MSTATUS, 6, 1, NULL, 0),
466SND_SOC_DAPM_PGA("Right Line In", AC97_EXTENDED_MSTATUS, 5, 1, NULL, 0),
467SND_SOC_DAPM_PGA("Mono In", AC97_EXTENDED_MSTATUS, 4, 1, NULL, 0),
468SND_SOC_DAPM_PGA("Mic A PGA", AC97_EXTENDED_MSTATUS, 3, 1, NULL, 0),
469SND_SOC_DAPM_PGA("Mic B PGA", AC97_EXTENDED_MSTATUS, 2, 1, NULL, 0),
470SND_SOC_DAPM_PGA("Mic A Pre Amp", AC97_EXTENDED_MSTATUS, 1, 1, NULL, 0),
471SND_SOC_DAPM_PGA("Mic B Pre Amp", AC97_EXTENDED_MSTATUS, 0, 1, NULL, 0),
472SND_SOC_DAPM_MICBIAS("Mic Bias", AC97_EXTENDED_MSTATUS, 14, 1),
473SND_SOC_DAPM_OUTPUT("MONO"),
474SND_SOC_DAPM_OUTPUT("HPL"),
475SND_SOC_DAPM_OUTPUT("HPR"),
476SND_SOC_DAPM_OUTPUT("SPKL"),
477SND_SOC_DAPM_OUTPUT("SPKR"),
478SND_SOC_DAPM_OUTPUT("OUT3"),
479SND_SOC_DAPM_OUTPUT("OUT4"),
480SND_SOC_DAPM_INPUT("LINEL"),
481SND_SOC_DAPM_INPUT("LINER"),
482SND_SOC_DAPM_INPUT("MONOIN"),
483SND_SOC_DAPM_INPUT("PCBEEP"),
484SND_SOC_DAPM_INPUT("MIC1"),
485SND_SOC_DAPM_INPUT("MIC2A"),
486SND_SOC_DAPM_INPUT("MIC2B"),
487SND_SOC_DAPM_VMID("VMID"),
488};
489
Lu Guanqun7ad7dd12011-03-30 21:53:11 +0800490static const struct snd_soc_dapm_route wm9713_audio_map[] = {
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100491 /* left HP mixer */
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100492 {"Left HP Mixer", "Beep Playback Switch", "PCBEEP"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100493 {"Left HP Mixer", "Voice Playback Switch", "Voice DAC"},
494 {"Left HP Mixer", "Aux Playback Switch", "Aux DAC"},
495 {"Left HP Mixer", "Bypass Playback Switch", "Left Line In"},
496 {"Left HP Mixer", "PCM Playback Switch", "Left DAC"},
497 {"Left HP Mixer", "MonoIn Playback Switch", "Mono In"},
498 {"Left HP Mixer", NULL, "Capture Headphone Mux"},
499
500 /* right HP mixer */
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100501 {"Right HP Mixer", "Beep Playback Switch", "PCBEEP"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100502 {"Right HP Mixer", "Voice Playback Switch", "Voice DAC"},
503 {"Right HP Mixer", "Aux Playback Switch", "Aux DAC"},
504 {"Right HP Mixer", "Bypass Playback Switch", "Right Line In"},
505 {"Right HP Mixer", "PCM Playback Switch", "Right DAC"},
506 {"Right HP Mixer", "MonoIn Playback Switch", "Mono In"},
507 {"Right HP Mixer", NULL, "Capture Headphone Mux"},
508
509 /* virtual mixer - mixes left & right channels for spk and mono */
510 {"AC97 Mixer", NULL, "Left DAC"},
511 {"AC97 Mixer", NULL, "Right DAC"},
512 {"Line Mixer", NULL, "Right Line In"},
513 {"Line Mixer", NULL, "Left Line In"},
514 {"HP Mixer", NULL, "Left HP Mixer"},
515 {"HP Mixer", NULL, "Right HP Mixer"},
516 {"Capture Mixer", NULL, "Left Capture Source"},
517 {"Capture Mixer", NULL, "Right Capture Source"},
518
519 /* speaker mixer */
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100520 {"Speaker Mixer", "Beep Playback Switch", "PCBEEP"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100521 {"Speaker Mixer", "Voice Playback Switch", "Voice DAC"},
522 {"Speaker Mixer", "Aux Playback Switch", "Aux DAC"},
523 {"Speaker Mixer", "Bypass Playback Switch", "Line Mixer"},
524 {"Speaker Mixer", "PCM Playback Switch", "AC97 Mixer"},
525 {"Speaker Mixer", "MonoIn Playback Switch", "Mono In"},
526
527 /* mono mixer */
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100528 {"Mono Mixer", "Beep Playback Switch", "PCBEEP"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100529 {"Mono Mixer", "Voice Playback Switch", "Voice DAC"},
530 {"Mono Mixer", "Aux Playback Switch", "Aux DAC"},
531 {"Mono Mixer", "Bypass Playback Switch", "Line Mixer"},
532 {"Mono Mixer", "PCM Playback Switch", "AC97 Mixer"},
Robert Jarzmikc0bbf482008-03-18 12:08:35 +0100533 {"Mono Mixer", "Mic 1 Sidetone Switch", "Mic A PGA"},
534 {"Mono Mixer", "Mic 2 Sidetone Switch", "Mic B PGA"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100535 {"Mono Mixer", NULL, "Capture Mono Mux"},
536
537 /* DAC inv mux 1 */
538 {"DAC Inv Mux 1", "Mono", "Mono Mixer"},
539 {"DAC Inv Mux 1", "Speaker", "Speaker Mixer"},
540 {"DAC Inv Mux 1", "Left Headphone", "Left HP Mixer"},
541 {"DAC Inv Mux 1", "Right Headphone", "Right HP Mixer"},
542 {"DAC Inv Mux 1", "Headphone Mono", "HP Mixer"},
543
544 /* DAC inv mux 2 */
545 {"DAC Inv Mux 2", "Mono", "Mono Mixer"},
546 {"DAC Inv Mux 2", "Speaker", "Speaker Mixer"},
547 {"DAC Inv Mux 2", "Left Headphone", "Left HP Mixer"},
548 {"DAC Inv Mux 2", "Right Headphone", "Right HP Mixer"},
549 {"DAC Inv Mux 2", "Headphone Mono", "HP Mixer"},
550
551 /* headphone left mux */
552 {"Left Headphone Out Mux", "Headphone", "Left HP Mixer"},
553
554 /* headphone right mux */
555 {"Right Headphone Out Mux", "Headphone", "Right HP Mixer"},
556
557 /* speaker left mux */
558 {"Left Speaker Out Mux", "Headphone", "Left HP Mixer"},
559 {"Left Speaker Out Mux", "Speaker", "Speaker Mixer"},
560 {"Left Speaker Out Mux", "Inv", "DAC Inv Mux 1"},
561
562 /* speaker right mux */
563 {"Right Speaker Out Mux", "Headphone", "Right HP Mixer"},
564 {"Right Speaker Out Mux", "Speaker", "Speaker Mixer"},
565 {"Right Speaker Out Mux", "Inv", "DAC Inv Mux 2"},
566
567 /* mono mux */
568 {"Mono Out Mux", "Mono", "Mono Mixer"},
569 {"Mono Out Mux", "Inv", "DAC Inv Mux 1"},
570
571 /* out 3 mux */
572 {"Out 3 Mux", "Inv 1", "DAC Inv Mux 1"},
573
574 /* out 4 mux */
575 {"Out 4 Mux", "Inv 2", "DAC Inv Mux 2"},
576
577 /* output pga */
578 {"HPL", NULL, "Left Headphone"},
579 {"Left Headphone", NULL, "Left Headphone Out Mux"},
580 {"HPR", NULL, "Right Headphone"},
581 {"Right Headphone", NULL, "Right Headphone Out Mux"},
582 {"OUT3", NULL, "Out 3"},
583 {"Out 3", NULL, "Out 3 Mux"},
584 {"OUT4", NULL, "Out 4"},
585 {"Out 4", NULL, "Out 4 Mux"},
586 {"SPKL", NULL, "Left Speaker"},
587 {"Left Speaker", NULL, "Left Speaker Out Mux"},
588 {"SPKR", NULL, "Right Speaker"},
589 {"Right Speaker", NULL, "Right Speaker Out Mux"},
590 {"MONO", NULL, "Mono Out"},
591 {"Mono Out", NULL, "Mono Out Mux"},
592
593 /* input pga */
594 {"Left Line In", NULL, "LINEL"},
595 {"Right Line In", NULL, "LINER"},
596 {"Mono In", NULL, "MONOIN"},
597 {"Mic A PGA", NULL, "Mic A Pre Amp"},
598 {"Mic B PGA", NULL, "Mic B Pre Amp"},
599
600 /* left capture select */
601 {"Left Capture Source", "Mic 1", "Mic A Pre Amp"},
602 {"Left Capture Source", "Mic 2", "Mic B Pre Amp"},
603 {"Left Capture Source", "Line", "LINEL"},
604 {"Left Capture Source", "Mono In", "MONOIN"},
605 {"Left Capture Source", "Headphone", "Left HP Mixer"},
606 {"Left Capture Source", "Speaker", "Speaker Mixer"},
607 {"Left Capture Source", "Mono Out", "Mono Mixer"},
608
609 /* right capture select */
610 {"Right Capture Source", "Mic 1", "Mic A Pre Amp"},
611 {"Right Capture Source", "Mic 2", "Mic B Pre Amp"},
612 {"Right Capture Source", "Line", "LINER"},
613 {"Right Capture Source", "Mono In", "MONOIN"},
614 {"Right Capture Source", "Headphone", "Right HP Mixer"},
615 {"Right Capture Source", "Speaker", "Speaker Mixer"},
616 {"Right Capture Source", "Mono Out", "Mono Mixer"},
617
618 /* left ADC */
619 {"Left ADC", NULL, "Left Capture Source"},
Mark Brownccbc3012008-09-15 15:51:13 +0100620 {"Left Voice ADC", NULL, "Left ADC"},
621 {"Left HiFi ADC", NULL, "Left ADC"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100622
623 /* right ADC */
624 {"Right ADC", NULL, "Right Capture Source"},
Mark Brownccbc3012008-09-15 15:51:13 +0100625 {"Right Voice ADC", NULL, "Right ADC"},
626 {"Right HiFi ADC", NULL, "Right ADC"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100627
628 /* mic */
629 {"Mic A Pre Amp", NULL, "Mic A Source"},
630 {"Mic A Source", "Mic 1", "MIC1"},
631 {"Mic A Source", "Mic 2 A", "MIC2A"},
632 {"Mic A Source", "Mic 2 B", "Mic B Source"},
633 {"Mic B Pre Amp", "MPB", "Mic B Source"},
634 {"Mic B Source", NULL, "MIC2B"},
635
636 /* headphone capture */
637 {"Capture Headphone Mux", "Stereo", "Capture Mixer"},
638 {"Capture Headphone Mux", "Left", "Left Capture Source"},
639 {"Capture Headphone Mux", "Right", "Right Capture Source"},
640
641 /* mono capture */
642 {"Capture Mono Mux", "Stereo", "Capture Mixer"},
643 {"Capture Mono Mux", "Left", "Left Capture Source"},
644 {"Capture Mono Mux", "Right", "Right Capture Source"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100645};
646
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100647static unsigned int ac97_read(struct snd_soc_codec *codec,
648 unsigned int reg)
649{
650 u16 *cache = codec->reg_cache;
651
652 if (reg == AC97_RESET || reg == AC97_GPIO_STATUS ||
653 reg == AC97_VENDOR_ID1 || reg == AC97_VENDOR_ID2 ||
654 reg == AC97_CD)
655 return soc_ac97_ops.read(codec->ac97, reg);
656 else {
657 reg = reg >> 1;
658
Ian Molton91432e92009-01-17 17:44:23 +0000659 if (reg >= (ARRAY_SIZE(wm9713_reg)))
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100660 return -EIO;
661
662 return cache[reg];
663 }
664}
665
666static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
667 unsigned int val)
668{
669 u16 *cache = codec->reg_cache;
670 if (reg < 0x7c)
671 soc_ac97_ops.write(codec->ac97, reg, val);
672 reg = reg >> 1;
Ian Molton91432e92009-01-17 17:44:23 +0000673 if (reg < (ARRAY_SIZE(wm9713_reg)))
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100674 cache[reg] = val;
675
676 return 0;
677}
678
679/* PLL divisors */
680struct _pll_div {
681 u32 divsel:1;
682 u32 divctl:1;
683 u32 lf:1;
684 u32 n:4;
685 u32 k:24;
686};
687
688/* The size in bits of the PLL divide multiplied by 10
689 * to allow rounding later */
690#define FIXED_PLL_SIZE ((1 << 22) * 10)
691
692static void pll_factors(struct _pll_div *pll_div, unsigned int source)
693{
694 u64 Kpart;
695 unsigned int K, Ndiv, Nmod, target;
696
697 /* The the PLL output is always 98.304MHz. */
698 target = 98304000;
699
700 /* If the input frequency is over 14.4MHz then scale it down. */
701 if (source > 14400000) {
702 source >>= 1;
703 pll_div->divsel = 1;
704
705 if (source > 14400000) {
706 source >>= 1;
707 pll_div->divctl = 1;
708 } else
709 pll_div->divctl = 0;
710
711 } else {
712 pll_div->divsel = 0;
713 pll_div->divctl = 0;
714 }
715
716 /* Low frequency sources require an additional divide in the
717 * loop.
718 */
719 if (source < 8192000) {
720 pll_div->lf = 1;
721 target >>= 2;
722 } else
723 pll_div->lf = 0;
724
725 Ndiv = target / source;
726 if ((Ndiv < 5) || (Ndiv > 12))
727 printk(KERN_WARNING
Roel Kluin449bd542009-05-27 17:08:39 -0700728 "WM9713 PLL N value %u out of recommended range!\n",
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100729 Ndiv);
730
731 pll_div->n = Ndiv;
732 Nmod = target % source;
733 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
734
735 do_div(Kpart, source);
736
737 K = Kpart & 0xFFFFFFFF;
738
739 /* Check if we need to round */
740 if ((K % 10) >= 5)
741 K += 5;
742
743 /* Move down to proper range now rounding is done */
744 K /= 10;
745
746 pll_div->k = K;
747}
748
749/**
750 * Please note that changing the PLL input frequency may require
751 * resynchronisation with the AC97 controller.
752 */
753static int wm9713_set_pll(struct snd_soc_codec *codec,
754 int pll_id, unsigned int freq_in, unsigned int freq_out)
755{
Mark Brownb2c812e2010-04-14 15:35:19 +0900756 struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100757 u16 reg, reg2;
758 struct _pll_div pll_div;
759
760 /* turn PLL off ? */
Mark Brownc42f69b2009-01-16 16:31:03 +0000761 if (freq_in == 0) {
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100762 /* disable PLL power and select ext source */
763 reg = ac97_read(codec, AC97_HANDSET_RATE);
764 ac97_write(codec, AC97_HANDSET_RATE, reg | 0x0080);
765 reg = ac97_read(codec, AC97_EXTENDED_MID);
766 ac97_write(codec, AC97_EXTENDED_MID, reg | 0x0200);
Mark Brownc42f69b2009-01-16 16:31:03 +0000767 wm9713->pll_in = 0;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100768 return 0;
769 }
770
771 pll_factors(&pll_div, freq_in);
772
773 if (pll_div.k == 0) {
774 reg = (pll_div.n << 12) | (pll_div.lf << 11) |
775 (pll_div.divsel << 9) | (pll_div.divctl << 8);
776 ac97_write(codec, AC97_LINE1_LEVEL, reg);
777 } else {
778 /* write the fractional k to the reg 0x46 pages */
779 reg2 = (pll_div.n << 12) | (pll_div.lf << 11) | (1 << 10) |
780 (pll_div.divsel << 9) | (pll_div.divctl << 8);
781
782 /* K [21:20] */
783 reg = reg2 | (0x5 << 4) | (pll_div.k >> 20);
784 ac97_write(codec, AC97_LINE1_LEVEL, reg);
785
786 /* K [19:16] */
787 reg = reg2 | (0x4 << 4) | ((pll_div.k >> 16) & 0xf);
788 ac97_write(codec, AC97_LINE1_LEVEL, reg);
789
790 /* K [15:12] */
791 reg = reg2 | (0x3 << 4) | ((pll_div.k >> 12) & 0xf);
792 ac97_write(codec, AC97_LINE1_LEVEL, reg);
793
794 /* K [11:8] */
795 reg = reg2 | (0x2 << 4) | ((pll_div.k >> 8) & 0xf);
796 ac97_write(codec, AC97_LINE1_LEVEL, reg);
797
798 /* K [7:4] */
799 reg = reg2 | (0x1 << 4) | ((pll_div.k >> 4) & 0xf);
800 ac97_write(codec, AC97_LINE1_LEVEL, reg);
801
802 reg = reg2 | (0x0 << 4) | (pll_div.k & 0xf); /* K [3:0] */
803 ac97_write(codec, AC97_LINE1_LEVEL, reg);
804 }
805
806 /* turn PLL on and select as source */
807 reg = ac97_read(codec, AC97_EXTENDED_MID);
808 ac97_write(codec, AC97_EXTENDED_MID, reg & 0xfdff);
809 reg = ac97_read(codec, AC97_HANDSET_RATE);
810 ac97_write(codec, AC97_HANDSET_RATE, reg & 0xff7f);
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100811 wm9713->pll_in = freq_in;
812
813 /* wait 10ms AC97 link frames for the link to stabilise */
814 schedule_timeout_interruptible(msecs_to_jiffies(10));
815 return 0;
816}
817
Mark Brown85488032009-09-05 18:52:16 +0100818static int wm9713_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
819 int source, unsigned int freq_in, unsigned int freq_out)
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100820{
821 struct snd_soc_codec *codec = codec_dai->codec;
822 return wm9713_set_pll(codec, pll_id, freq_in, freq_out);
823}
824
825/*
826 * Tristate the PCM DAI lines, tristate can be disabled by calling
827 * wm9713_set_dai_fmt()
828 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100829static int wm9713_set_dai_tristate(struct snd_soc_dai *codec_dai,
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100830 int tristate)
831{
832 struct snd_soc_codec *codec = codec_dai->codec;
833 u16 reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0x9fff;
834
835 if (tristate)
836 ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
837
838 return 0;
839}
840
841/*
842 * Configure WM9713 clock dividers.
843 * Voice DAC needs 256 FS
844 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100845static int wm9713_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100846 int div_id, int div)
847{
848 struct snd_soc_codec *codec = codec_dai->codec;
849 u16 reg;
850
851 switch (div_id) {
852 case WM9713_PCMCLK_DIV:
853 reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xf0ff;
854 ac97_write(codec, AC97_HANDSET_RATE, reg | div);
855 break;
856 case WM9713_CLKA_MULT:
857 reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xfffd;
858 ac97_write(codec, AC97_HANDSET_RATE, reg | div);
859 break;
860 case WM9713_CLKB_MULT:
861 reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xfffb;
862 ac97_write(codec, AC97_HANDSET_RATE, reg | div);
863 break;
864 case WM9713_HIFI_DIV:
865 reg = ac97_read(codec, AC97_HANDSET_RATE) & 0x8fff;
866 ac97_write(codec, AC97_HANDSET_RATE, reg | div);
867 break;
868 case WM9713_PCMBCLK_DIV:
869 reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0xf1ff;
870 ac97_write(codec, AC97_CENTER_LFE_MASTER, reg | div);
871 break;
872 case WM9713_PCMCLK_PLL_DIV:
873 reg = ac97_read(codec, AC97_LINE1_LEVEL) & 0xff80;
874 ac97_write(codec, AC97_LINE1_LEVEL, reg | 0x60 | div);
875 break;
876 case WM9713_HIFI_PLL_DIV:
877 reg = ac97_read(codec, AC97_LINE1_LEVEL) & 0xff80;
878 ac97_write(codec, AC97_LINE1_LEVEL, reg | 0x70 | div);
879 break;
880 default:
881 return -EINVAL;
882 }
883
884 return 0;
885}
886
Liam Girdwoode550e172008-07-07 16:07:52 +0100887static int wm9713_set_dai_fmt(struct snd_soc_dai *codec_dai,
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100888 unsigned int fmt)
889{
890 struct snd_soc_codec *codec = codec_dai->codec;
891 u16 gpio = ac97_read(codec, AC97_GPIO_CFG) & 0xffc5;
892 u16 reg = 0x8000;
893
894 /* clock masters */
895 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
896 case SND_SOC_DAIFMT_CBM_CFM:
897 reg |= 0x4000;
898 gpio |= 0x0010;
899 break;
900 case SND_SOC_DAIFMT_CBM_CFS:
901 reg |= 0x6000;
902 gpio |= 0x0018;
903 break;
904 case SND_SOC_DAIFMT_CBS_CFS:
Mark Brownd133b0c2008-06-11 13:47:11 +0100905 reg |= 0x2000;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100906 gpio |= 0x001a;
907 break;
908 case SND_SOC_DAIFMT_CBS_CFM:
909 gpio |= 0x0012;
910 break;
911 }
912
913 /* clock inversion */
914 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
915 case SND_SOC_DAIFMT_IB_IF:
916 reg |= 0x00c0;
917 break;
918 case SND_SOC_DAIFMT_IB_NF:
919 reg |= 0x0080;
920 break;
921 case SND_SOC_DAIFMT_NB_IF:
922 reg |= 0x0040;
923 break;
924 }
925
926 /* DAI format */
927 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
928 case SND_SOC_DAIFMT_I2S:
929 reg |= 0x0002;
930 break;
931 case SND_SOC_DAIFMT_RIGHT_J:
932 break;
933 case SND_SOC_DAIFMT_LEFT_J:
934 reg |= 0x0001;
935 break;
936 case SND_SOC_DAIFMT_DSP_A:
937 reg |= 0x0003;
938 break;
939 case SND_SOC_DAIFMT_DSP_B:
940 reg |= 0x0043;
941 break;
942 }
943
944 ac97_write(codec, AC97_GPIO_CFG, gpio);
945 ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
946 return 0;
947}
948
949static int wm9713_pcm_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000950 struct snd_pcm_hw_params *params,
951 struct snd_soc_dai *dai)
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100952{
Mark Brown55b8bac2008-11-24 14:05:29 +0000953 struct snd_soc_codec *codec = dai->codec;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100954 u16 reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0xfff3;
955
956 switch (params_format(params)) {
957 case SNDRV_PCM_FORMAT_S16_LE:
958 break;
959 case SNDRV_PCM_FORMAT_S20_3LE:
960 reg |= 0x0004;
961 break;
962 case SNDRV_PCM_FORMAT_S24_LE:
963 reg |= 0x0008;
964 break;
965 case SNDRV_PCM_FORMAT_S32_LE:
966 reg |= 0x000c;
967 break;
968 }
969
970 /* enable PCM interface in master mode */
971 ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
972 return 0;
973}
974
Mark Browndee89c42008-11-18 22:11:38 +0000975static int ac97_hifi_prepare(struct snd_pcm_substream *substream,
976 struct snd_soc_dai *dai)
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100977{
Mark Brown55b8bac2008-11-24 14:05:29 +0000978 struct snd_soc_codec *codec = dai->codec;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100979 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100980 int reg;
981 u16 vra;
982
983 vra = ac97_read(codec, AC97_EXTENDED_STATUS);
984 ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
985
986 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
987 reg = AC97_PCM_FRONT_DAC_RATE;
988 else
989 reg = AC97_PCM_LR_ADC_RATE;
990
991 return ac97_write(codec, reg, runtime->rate);
992}
993
Mark Browndee89c42008-11-18 22:11:38 +0000994static int ac97_aux_prepare(struct snd_pcm_substream *substream,
995 struct snd_soc_dai *dai)
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100996{
Mark Brown55b8bac2008-11-24 14:05:29 +0000997 struct snd_soc_codec *codec = dai->codec;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100998 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100999 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
Mark Brown54155522008-07-02 11:51:19 +01001012#define WM9713_RATES (SNDRV_PCM_RATE_8000 | \
1013 SNDRV_PCM_RATE_11025 | \
1014 SNDRV_PCM_RATE_22050 | \
1015 SNDRV_PCM_RATE_44100 | \
1016 SNDRV_PCM_RATE_48000)
1017
1018#define WM9713_PCM_RATES (SNDRV_PCM_RATE_8000 | \
1019 SNDRV_PCM_RATE_11025 | \
1020 SNDRV_PCM_RATE_16000 | \
1021 SNDRV_PCM_RATE_22050 | \
1022 SNDRV_PCM_RATE_44100 | \
1023 SNDRV_PCM_RATE_48000)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001024
1025#define WM9713_PCM_FORMATS \
1026 (SNDRV_PCM_FORMAT_S16_LE | SNDRV_PCM_FORMAT_S20_3LE | \
1027 SNDRV_PCM_FORMAT_S24_LE)
1028
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001029static const struct snd_soc_dai_ops wm9713_dai_ops_hifi = {
Eric Miao6335d052009-03-03 09:41:00 +08001030 .prepare = ac97_hifi_prepare,
1031 .set_clkdiv = wm9713_set_dai_clkdiv,
1032 .set_pll = wm9713_set_dai_pll,
1033};
1034
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001035static const struct snd_soc_dai_ops wm9713_dai_ops_aux = {
Eric Miao6335d052009-03-03 09:41:00 +08001036 .prepare = ac97_aux_prepare,
1037 .set_clkdiv = wm9713_set_dai_clkdiv,
1038 .set_pll = wm9713_set_dai_pll,
1039};
1040
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001041static const struct snd_soc_dai_ops wm9713_dai_ops_voice = {
Eric Miao6335d052009-03-03 09:41:00 +08001042 .hw_params = wm9713_pcm_hw_params,
Eric Miao6335d052009-03-03 09:41:00 +08001043 .set_clkdiv = wm9713_set_dai_clkdiv,
1044 .set_pll = wm9713_set_dai_pll,
1045 .set_fmt = wm9713_set_dai_fmt,
1046 .set_tristate = wm9713_set_dai_tristate,
1047};
1048
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001049static struct snd_soc_dai_driver wm9713_dai[] = {
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001050{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001051 .name = "wm9713-hifi",
Mark Brown3ba9e10a2008-11-24 18:01:05 +00001052 .ac97_control = 1,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001053 .playback = {
1054 .stream_name = "HiFi Playback",
1055 .channels_min = 1,
1056 .channels_max = 2,
1057 .rates = WM9713_RATES,
Mark Brown33f503c2009-05-02 12:24:55 +01001058 .formats = SND_SOC_STD_AC97_FMTS,},
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001059 .capture = {
1060 .stream_name = "HiFi Capture",
1061 .channels_min = 1,
1062 .channels_max = 2,
1063 .rates = WM9713_RATES,
Mark Brown33f503c2009-05-02 12:24:55 +01001064 .formats = SND_SOC_STD_AC97_FMTS,},
Eric Miao6335d052009-03-03 09:41:00 +08001065 .ops = &wm9713_dai_ops_hifi,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001066 },
1067 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001068 .name = "wm9713-aux",
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001069 .playback = {
1070 .stream_name = "Aux Playback",
1071 .channels_min = 1,
1072 .channels_max = 1,
1073 .rates = WM9713_RATES,
Mark Brown33f503c2009-05-02 12:24:55 +01001074 .formats = SND_SOC_STD_AC97_FMTS,},
Eric Miao6335d052009-03-03 09:41:00 +08001075 .ops = &wm9713_dai_ops_aux,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001076 },
1077 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001078 .name = "wm9713-voice",
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001079 .playback = {
1080 .stream_name = "Voice Playback",
1081 .channels_min = 1,
1082 .channels_max = 1,
Mark Brown54155522008-07-02 11:51:19 +01001083 .rates = WM9713_PCM_RATES,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001084 .formats = WM9713_PCM_FORMATS,},
1085 .capture = {
1086 .stream_name = "Voice Capture",
1087 .channels_min = 1,
1088 .channels_max = 2,
Mark Brown54155522008-07-02 11:51:19 +01001089 .rates = WM9713_PCM_RATES,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001090 .formats = WM9713_PCM_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +08001091 .ops = &wm9713_dai_ops_voice,
Mark Brownf4976112009-04-13 10:53:02 +01001092 .symmetric_rates = 1,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001093 },
1094};
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001095
1096int wm9713_reset(struct snd_soc_codec *codec, int try_warm)
1097{
1098 if (try_warm && soc_ac97_ops.warm_reset) {
1099 soc_ac97_ops.warm_reset(codec->ac97);
Mark Brownabb68c262008-06-13 16:24:04 +01001100 if (ac97_read(codec, 0) == wm9713_reg[0])
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001101 return 1;
1102 }
1103
1104 soc_ac97_ops.reset(codec->ac97);
Mark Browne775f6c2008-10-28 15:04:35 +00001105 if (soc_ac97_ops.warm_reset)
1106 soc_ac97_ops.warm_reset(codec->ac97);
Mark Brownabb68c262008-06-13 16:24:04 +01001107 if (ac97_read(codec, 0) != wm9713_reg[0])
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001108 return -EIO;
1109 return 0;
1110}
1111EXPORT_SYMBOL_GPL(wm9713_reset);
1112
Mark Brown0be98982008-05-19 12:31:28 +02001113static int wm9713_set_bias_level(struct snd_soc_codec *codec,
1114 enum snd_soc_bias_level level)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001115{
1116 u16 reg;
1117
Mark Brown0be98982008-05-19 12:31:28 +02001118 switch (level) {
1119 case SND_SOC_BIAS_ON:
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001120 /* enable thermal shutdown */
1121 reg = ac97_read(codec, AC97_EXTENDED_MID) & 0x1bff;
1122 ac97_write(codec, AC97_EXTENDED_MID, reg);
1123 break;
Mark Brown0be98982008-05-19 12:31:28 +02001124 case SND_SOC_BIAS_PREPARE:
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001125 break;
Mark Brown0be98982008-05-19 12:31:28 +02001126 case SND_SOC_BIAS_STANDBY:
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001127 /* enable master bias and vmid */
1128 reg = ac97_read(codec, AC97_EXTENDED_MID) & 0x3bff;
1129 ac97_write(codec, AC97_EXTENDED_MID, reg);
1130 ac97_write(codec, AC97_POWERDOWN, 0x0000);
1131 break;
Mark Brown0be98982008-05-19 12:31:28 +02001132 case SND_SOC_BIAS_OFF:
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001133 /* disable everything including AC link */
1134 ac97_write(codec, AC97_EXTENDED_MID, 0xffff);
1135 ac97_write(codec, AC97_EXTENDED_MSTATUS, 0xffff);
1136 ac97_write(codec, AC97_POWERDOWN, 0xffff);
1137 break;
1138 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001139 codec->dapm.bias_level = level;
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001140 return 0;
1141}
1142
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +01001143static int wm9713_soc_suspend(struct snd_soc_codec *codec)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001144{
Mark Brown87b57fe2008-04-14 15:27:30 +02001145 u16 reg;
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001146
Mark Brown87b57fe2008-04-14 15:27:30 +02001147 /* Disable everything except touchpanel - that will be handled
1148 * by the touch driver and left disabled if touch is not in
1149 * use. */
1150 reg = ac97_read(codec, AC97_EXTENDED_MID);
1151 ac97_write(codec, AC97_EXTENDED_MID, reg | 0x7fff);
1152 ac97_write(codec, AC97_EXTENDED_MSTATUS, 0xffff);
1153 ac97_write(codec, AC97_POWERDOWN, 0x6f00);
1154 ac97_write(codec, AC97_POWERDOWN, 0xffff);
1155
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001156 return 0;
1157}
1158
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001159static int wm9713_soc_resume(struct snd_soc_codec *codec)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001160{
Mark Brownb2c812e2010-04-14 15:35:19 +09001161 struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001162 int i, ret;
1163 u16 *cache = codec->reg_cache;
1164
1165 ret = wm9713_reset(codec, 1);
1166 if (ret < 0) {
1167 printk(KERN_ERR "could not reset AC97 codec\n");
1168 return ret;
1169 }
1170
Mark Brown0be98982008-05-19 12:31:28 +02001171 wm9713_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001172
1173 /* do we need to re-start the PLL ? */
Mark Brownc42f69b2009-01-16 16:31:03 +00001174 if (wm9713->pll_in)
1175 wm9713_set_pll(codec, 0, wm9713->pll_in, 0);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001176
1177 /* only synchronise the codec if warm reset failed */
1178 if (ret == 0) {
1179 for (i = 2; i < ARRAY_SIZE(wm9713_reg) << 1; i += 2) {
1180 if (i == AC97_POWERDOWN || i == AC97_EXTENDED_MID ||
1181 i == AC97_EXTENDED_MSTATUS || i > 0x66)
1182 continue;
1183 soc_ac97_ops.write(codec->ac97, i, cache[i>>1]);
1184 }
1185 }
1186
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001187 return ret;
1188}
1189
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001190static int wm9713_soc_probe(struct snd_soc_codec *codec)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001191{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001192 struct wm9713_priv *wm9713;
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001193 int ret = 0, reg;
1194
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001195 wm9713 = kzalloc(sizeof(struct wm9713_priv), GFP_KERNEL);
1196 if (wm9713 == NULL)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001197 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001198 snd_soc_codec_set_drvdata(codec, wm9713);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001199
1200 ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
1201 if (ret < 0)
1202 goto codec_err;
1203
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001204 /* do a cold reset for the controller and then try
1205 * a warm reset followed by an optional cold reset for codec */
1206 wm9713_reset(codec, 0);
1207 ret = wm9713_reset(codec, 1);
1208 if (ret < 0) {
Mark Brown39639fa2008-11-21 14:28:49 +00001209 printk(KERN_ERR "Failed to reset WM9713: AC97 link error\n");
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001210 goto reset_err;
1211 }
1212
Mark Brown0be98982008-05-19 12:31:28 +02001213 wm9713_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001214
1215 /* unmute the adc - move to kcontrol */
1216 reg = ac97_read(codec, AC97_CD) & 0x7fff;
1217 ac97_write(codec, AC97_CD, reg);
1218
Liam Girdwood022658b2012-02-03 17:43:09 +00001219 snd_soc_add_codec_controls(codec, wm9713_snd_ac97_controls,
Ian Molton3e8e1952009-01-09 00:23:21 +00001220 ARRAY_SIZE(wm9713_snd_ac97_controls));
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001221
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001222 return 0;
1223
1224reset_err:
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001225 snd_soc_free_ac97_codec(codec);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001226codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001227 kfree(wm9713);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001228 return ret;
1229}
1230
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001231static int wm9713_soc_remove(struct snd_soc_codec *codec)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001232{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001233 struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001234 snd_soc_free_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001235 kfree(wm9713);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001236 return 0;
1237}
1238
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001239static struct snd_soc_codec_driver soc_codec_dev_wm9713 = {
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001240 .probe = wm9713_soc_probe,
1241 .remove = wm9713_soc_remove,
1242 .suspend = wm9713_soc_suspend,
1243 .resume = wm9713_soc_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001244 .read = ac97_read,
1245 .write = ac97_write,
1246 .set_bias_level = wm9713_set_bias_level,
Dimitris Papastamose5eec342010-09-10 18:14:56 +01001247 .reg_cache_size = ARRAY_SIZE(wm9713_reg),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001248 .reg_word_size = sizeof(u16),
1249 .reg_cache_step = 2,
1250 .reg_cache_default = wm9713_reg,
Lu Guanqun7ad7dd12011-03-30 21:53:11 +08001251 .dapm_widgets = wm9713_dapm_widgets,
1252 .num_dapm_widgets = ARRAY_SIZE(wm9713_dapm_widgets),
1253 .dapm_routes = wm9713_audio_map,
1254 .num_dapm_routes = ARRAY_SIZE(wm9713_audio_map),
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001255};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001256
1257static __devinit int wm9713_probe(struct platform_device *pdev)
1258{
1259 return snd_soc_register_codec(&pdev->dev,
1260 &soc_codec_dev_wm9713, wm9713_dai, ARRAY_SIZE(wm9713_dai));
1261}
1262
1263static int __devexit wm9713_remove(struct platform_device *pdev)
1264{
1265 snd_soc_unregister_codec(&pdev->dev);
1266 return 0;
1267}
1268
1269static struct platform_driver wm9713_codec_driver = {
1270 .driver = {
1271 .name = "wm9713-codec",
1272 .owner = THIS_MODULE,
1273 },
1274
1275 .probe = wm9713_probe,
1276 .remove = __devexit_p(wm9713_remove),
1277};
1278
Mark Brown5bbcc3c2011-11-23 22:52:08 +00001279module_platform_driver(wm9713_codec_driver);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001280
1281MODULE_DESCRIPTION("ASoC WM9713/WM9714 driver");
1282MODULE_AUTHOR("Liam Girdwood");
1283MODULE_LICENSE("GPL");