1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * wm9713.c -- ALSA Soc WM9713 codec support
4 *
5 * Copyright 2006-10 Wolfson Microelectronics PLC.
6 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 *
8 * Features:-
9 *
10 * o Support for AC97 Codec, Voice DAC and Aux DAC
11 * o Support for DAPM
12 */
13
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/mfd/wm97xx.h>
17 #include <linux/module.h>
18 #include <linux/device.h>
19 #include <linux/regmap.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/ac97_codec.h>
23 #include <sound/ac97/codec.h>
24 #include <sound/ac97/compat.h>
25 #include <sound/initval.h>
26 #include <sound/pcm_params.h>
27 #include <sound/tlv.h>
28 #include <sound/soc.h>
29
30 #include "wm9713.h"
31
32 #define WM9713_VENDOR_ID 0x574d4c13
33 #define WM9713_VENDOR_ID_MASK 0xffffffff
34
35 struct wm9713_priv {
36 struct snd_ac97 *ac97;
37 u32 pll_in; /* PLL input frequency */
38 unsigned int hp_mixer[2];
39 struct mutex lock;
40 struct wm97xx_platform_data *mfd_pdata;
41 };
42
43 #define HPL_MIXER 0
44 #define HPR_MIXER 1
45
46 static const char *wm9713_mic_mixer[] = {"Stereo", "Mic 1", "Mic 2", "Mute"};
47 static const char *wm9713_rec_mux[] = {"Stereo", "Left", "Right", "Mute"};
48 static const char *wm9713_rec_src[] =
49 {"Mic 1", "Mic 2", "Line", "Mono In", "Headphone", "Speaker",
50 "Mono Out", "Zh"};
51 static const char *wm9713_rec_gain[] = {"+1.5dB Steps", "+0.75dB Steps"};
52 static const char *wm9713_alc_select[] = {"None", "Left", "Right", "Stereo"};
53 static const char *wm9713_mono_pga[] = {"Vmid", "Zh", "Mono", "Inv"};
54 static const char *wm9713_spk_pga[] =
55 {"Vmid", "Zh", "Headphone", "Speaker", "Inv", "Headphone Vmid",
56 "Speaker Vmid", "Inv Vmid"};
57 static const char *wm9713_hp_pga[] = {"Vmid", "Zh", "Headphone",
58 "Headphone Vmid"};
59 static const char *wm9713_out3_pga[] = {"Vmid", "Zh", "Inv 1", "Inv 1 Vmid"};
60 static const char *wm9713_out4_pga[] = {"Vmid", "Zh", "Inv 2", "Inv 2 Vmid"};
61 static const char *wm9713_dac_inv[] =
62 {"Off", "Mono", "Speaker", "Left Headphone", "Right Headphone",
63 "Headphone Mono", "NC", "Vmid"};
64 static const char *wm9713_bass[] = {"Linear Control", "Adaptive Boost"};
65 static const char *wm9713_ng_type[] = {"Constant Gain", "Mute"};
66 static const char *wm9713_mic_select[] = {"Mic 1", "Mic 2 A", "Mic 2 B"};
67 static const char *wm9713_micb_select[] = {"MPB", "MPA"};
68
69 static const struct soc_enum wm9713_enum[] = {
70 SOC_ENUM_SINGLE(AC97_LINE, 3, 4, wm9713_mic_mixer), /* record mic mixer 0 */
71 SOC_ENUM_SINGLE(AC97_VIDEO, 14, 4, wm9713_rec_mux), /* record mux hp 1 */
72 SOC_ENUM_SINGLE(AC97_VIDEO, 9, 4, wm9713_rec_mux), /* record mux mono 2 */
73 SOC_ENUM_SINGLE(AC97_VIDEO, 3, 8, wm9713_rec_src), /* record mux left 3 */
74 SOC_ENUM_SINGLE(AC97_VIDEO, 0, 8, wm9713_rec_src), /* record mux right 4*/
75 SOC_ENUM_DOUBLE(AC97_CD, 14, 6, 2, wm9713_rec_gain), /* record step size 5 */
76 SOC_ENUM_SINGLE(AC97_PCI_SVID, 14, 4, wm9713_alc_select), /* alc source select 6*/
77 SOC_ENUM_SINGLE(AC97_REC_GAIN, 14, 4, wm9713_mono_pga), /* mono input select 7 */
78 SOC_ENUM_SINGLE(AC97_REC_GAIN, 11, 8, wm9713_spk_pga), /* speaker left input select 8 */
79 SOC_ENUM_SINGLE(AC97_REC_GAIN, 8, 8, wm9713_spk_pga), /* speaker right input select 9 */
80 SOC_ENUM_SINGLE(AC97_REC_GAIN, 6, 3, wm9713_hp_pga), /* headphone left input 10 */
81 SOC_ENUM_SINGLE(AC97_REC_GAIN, 4, 3, wm9713_hp_pga), /* headphone right input 11 */
82 SOC_ENUM_SINGLE(AC97_REC_GAIN, 2, 4, wm9713_out3_pga), /* out 3 source 12 */
83 SOC_ENUM_SINGLE(AC97_REC_GAIN, 0, 4, wm9713_out4_pga), /* out 4 source 13 */
84 SOC_ENUM_SINGLE(AC97_REC_GAIN_MIC, 13, 8, wm9713_dac_inv), /* dac invert 1 14 */
85 SOC_ENUM_SINGLE(AC97_REC_GAIN_MIC, 10, 8, wm9713_dac_inv), /* dac invert 2 15 */
86 SOC_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 15, 2, wm9713_bass), /* bass control 16 */
87 SOC_ENUM_SINGLE(AC97_PCI_SVID, 5, 2, wm9713_ng_type), /* noise gate type 17 */
88 SOC_ENUM_SINGLE(AC97_3D_CONTROL, 12, 3, wm9713_mic_select), /* mic selection 18 */
89 SOC_ENUM_SINGLE_VIRT(2, wm9713_micb_select), /* mic selection 19 */
90 };
91
92 static const DECLARE_TLV_DB_SCALE(out_tlv, -4650, 150, 0);
93 static const DECLARE_TLV_DB_SCALE(main_tlv, -3450, 150, 0);
94 static const DECLARE_TLV_DB_SCALE(misc_tlv, -1500, 300, 0);
95 static const DECLARE_TLV_DB_RANGE(mic_tlv,
96 0, 2, TLV_DB_SCALE_ITEM(1200, 600, 0),
97 3, 3, TLV_DB_SCALE_ITEM(3000, 0, 0)
98 );
99
100 static const struct snd_kcontrol_new wm9713_snd_ac97_controls[] = {
101 SOC_DOUBLE_TLV("Speaker Playback Volume", AC97_MASTER, 8, 0, 31, 1, out_tlv),
102 SOC_DOUBLE("Speaker Playback Switch", AC97_MASTER, 15, 7, 1, 1),
103 SOC_DOUBLE_TLV("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1,
104 out_tlv),
105 SOC_DOUBLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 7, 1, 1),
106 SOC_DOUBLE_TLV("Line In Volume", AC97_PC_BEEP, 8, 0, 31, 1, main_tlv),
107 SOC_DOUBLE_TLV("PCM Playback Volume", AC97_PHONE, 8, 0, 31, 1, main_tlv),
108 SOC_SINGLE_TLV("Mic 1 Volume", AC97_MIC, 8, 31, 1, main_tlv),
109 SOC_SINGLE_TLV("Mic 2 Volume", AC97_MIC, 0, 31, 1, main_tlv),
110 SOC_SINGLE_TLV("Mic 1 Preamp Volume", AC97_3D_CONTROL, 10, 3, 0, mic_tlv),
111 SOC_SINGLE_TLV("Mic 2 Preamp Volume", AC97_3D_CONTROL, 12, 3, 0, mic_tlv),
112
113 SOC_SINGLE("Mic Boost (+20dB) Switch", AC97_LINE, 5, 1, 0),
114 SOC_SINGLE("Mic Headphone Mixer Volume", AC97_LINE, 0, 7, 1),
115
116 SOC_SINGLE("Capture Switch", AC97_CD, 15, 1, 1),
117 SOC_ENUM("Capture Volume Steps", wm9713_enum[5]),
118 SOC_DOUBLE("Capture Volume", AC97_CD, 8, 0, 31, 0),
119 SOC_SINGLE("Capture ZC Switch", AC97_CD, 7, 1, 0),
120
121 SOC_SINGLE_TLV("Capture to Headphone Volume", AC97_VIDEO, 11, 7, 1, misc_tlv),
122 SOC_SINGLE("Capture to Mono Boost (+20dB) Switch", AC97_VIDEO, 8, 1, 0),
123 SOC_SINGLE("Capture ADC Boost (+20dB) Switch", AC97_VIDEO, 6, 1, 0),
124
125 SOC_SINGLE("ALC Target Volume", AC97_CODEC_CLASS_REV, 12, 15, 0),
126 SOC_SINGLE("ALC Hold Time", AC97_CODEC_CLASS_REV, 8, 15, 0),
127 SOC_SINGLE("ALC Decay Time", AC97_CODEC_CLASS_REV, 4, 15, 0),
128 SOC_SINGLE("ALC Attack Time", AC97_CODEC_CLASS_REV, 0, 15, 0),
129 SOC_ENUM("ALC Function", wm9713_enum[6]),
130 SOC_SINGLE("ALC Max Volume", AC97_PCI_SVID, 11, 7, 0),
131 SOC_SINGLE("ALC ZC Timeout", AC97_PCI_SVID, 9, 3, 0),
132 SOC_SINGLE("ALC ZC Switch", AC97_PCI_SVID, 8, 1, 0),
133 SOC_SINGLE("ALC NG Switch", AC97_PCI_SVID, 7, 1, 0),
134 SOC_ENUM("ALC NG Type", wm9713_enum[17]),
135 SOC_SINGLE("ALC NG Threshold", AC97_PCI_SVID, 0, 31, 0),
136
137 SOC_DOUBLE("Speaker Playback ZC Switch", AC97_MASTER, 14, 6, 1, 0),
138 SOC_DOUBLE("Headphone Playback ZC Switch", AC97_HEADPHONE, 14, 6, 1, 0),
139
140 SOC_SINGLE("Out4 Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
141 SOC_SINGLE("Out4 Playback ZC Switch", AC97_MASTER_MONO, 14, 1, 0),
142 SOC_SINGLE_TLV("Out4 Playback Volume", AC97_MASTER_MONO, 8, 31, 1, out_tlv),
143
144 SOC_SINGLE("Out3 Playback Switch", AC97_MASTER_MONO, 7, 1, 1),
145 SOC_SINGLE("Out3 Playback ZC Switch", AC97_MASTER_MONO, 6, 1, 0),
146 SOC_SINGLE_TLV("Out3 Playback Volume", AC97_MASTER_MONO, 0, 31, 1, out_tlv),
147
148 SOC_SINGLE_TLV("Mono Capture Volume", AC97_MASTER_TONE, 8, 31, 1, main_tlv),
149 SOC_SINGLE("Mono Playback Switch", AC97_MASTER_TONE, 7, 1, 1),
150 SOC_SINGLE("Mono Playback ZC Switch", AC97_MASTER_TONE, 6, 1, 0),
151 SOC_SINGLE_TLV("Mono Playback Volume", AC97_MASTER_TONE, 0, 31, 1, out_tlv),
152
153 SOC_SINGLE_TLV("Headphone Mixer Beep Playback Volume", AC97_AUX, 12, 7, 1,
154 misc_tlv),
155 SOC_SINGLE_TLV("Speaker Mixer Beep Playback Volume", AC97_AUX, 8, 7, 1,
156 misc_tlv),
157 SOC_SINGLE_TLV("Mono Mixer Beep Playback Volume", AC97_AUX, 4, 7, 1, misc_tlv),
158
159 SOC_SINGLE_TLV("Voice Playback Headphone Volume", AC97_PCM, 12, 7, 1,
160 misc_tlv),
161 SOC_SINGLE("Voice Playback Master Volume", AC97_PCM, 8, 7, 1),
162 SOC_SINGLE("Voice Playback Mono Volume", AC97_PCM, 4, 7, 1),
163
164 SOC_SINGLE_TLV("Headphone Mixer Aux Playback Volume", AC97_REC_SEL, 12, 7, 1,
165 misc_tlv),
166
167 SOC_SINGLE_TLV("Speaker Mixer Voice Playback Volume", AC97_PCM, 8, 7, 1,
168 misc_tlv),
169 SOC_SINGLE_TLV("Speaker Mixer Aux Playback Volume", AC97_REC_SEL, 8, 7, 1,
170 misc_tlv),
171
172 SOC_SINGLE_TLV("Mono Mixer Voice Playback Volume", AC97_PCM, 4, 7, 1,
173 misc_tlv),
174 SOC_SINGLE_TLV("Mono Mixer Aux Playback Volume", AC97_REC_SEL, 4, 7, 1,
175 misc_tlv),
176
177 SOC_SINGLE("Aux Playback Headphone Volume", AC97_REC_SEL, 12, 7, 1),
178 SOC_SINGLE("Aux Playback Master Volume", AC97_REC_SEL, 8, 7, 1),
179
180 SOC_ENUM("Bass Control", wm9713_enum[16]),
181 SOC_SINGLE("Bass Cut-off Switch", AC97_GENERAL_PURPOSE, 12, 1, 1),
182 SOC_SINGLE("Tone Cut-off Switch", AC97_GENERAL_PURPOSE, 4, 1, 1),
183 SOC_SINGLE("Playback Attenuate (-6dB) Switch", AC97_GENERAL_PURPOSE, 6, 1, 0),
184 SOC_SINGLE("Bass Volume", AC97_GENERAL_PURPOSE, 8, 15, 1),
185 SOC_SINGLE("Tone Volume", AC97_GENERAL_PURPOSE, 0, 15, 1),
186
187 SOC_SINGLE("3D Upper Cut-off Switch", AC97_REC_GAIN_MIC, 5, 1, 0),
188 SOC_SINGLE("3D Lower Cut-off Switch", AC97_REC_GAIN_MIC, 4, 1, 0),
189 SOC_SINGLE("3D Depth", AC97_REC_GAIN_MIC, 0, 15, 1),
190 };
191
wm9713_voice_shutdown(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)192 static int wm9713_voice_shutdown(struct snd_soc_dapm_widget *w,
193 struct snd_kcontrol *kcontrol, int event)
194 {
195 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
196
197 if (WARN_ON(event != SND_SOC_DAPM_PRE_PMD))
198 return -EINVAL;
199
200 /* Gracefully shut down the voice interface. */
201 snd_soc_component_update_bits(component, AC97_HANDSET_RATE, 0x0f00, 0x0200);
202 schedule_timeout_interruptible(msecs_to_jiffies(1));
203 snd_soc_component_update_bits(component, AC97_HANDSET_RATE, 0x0f00, 0x0f00);
204 snd_soc_component_update_bits(component, AC97_EXTENDED_MID, 0x1000, 0x1000);
205
206 return 0;
207 }
208
209 static const unsigned int wm9713_mixer_mute_regs[] = {
210 AC97_PC_BEEP,
211 AC97_MASTER_TONE,
212 AC97_PHONE,
213 AC97_REC_SEL,
214 AC97_PCM,
215 AC97_AUX,
216 };
217
218 /* We have to create a fake left and right HP mixers because
219 * the codec only has a single control that is shared by both channels.
220 * This makes it impossible to determine the audio path using the current
221 * register map, thus we add a new (virtual) register to help determine the
222 * audio route within the device.
223 */
wm9713_hp_mixer_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)224 static int wm9713_hp_mixer_put(struct snd_kcontrol *kcontrol,
225 struct snd_ctl_elem_value *ucontrol)
226 {
227 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
228 struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
229 struct wm9713_priv *wm9713 = snd_soc_component_get_drvdata(component);
230 unsigned int val = ucontrol->value.integer.value[0];
231 struct soc_mixer_control *mc =
232 (struct soc_mixer_control *)kcontrol->private_value;
233 unsigned int mixer, mask, shift, old;
234 struct snd_soc_dapm_update update = {};
235 bool change;
236
237 mixer = mc->shift >> 8;
238 shift = mc->shift & 0xff;
239 mask = (1 << shift);
240
241 mutex_lock(&wm9713->lock);
242 old = wm9713->hp_mixer[mixer];
243 if (ucontrol->value.integer.value[0])
244 wm9713->hp_mixer[mixer] |= mask;
245 else
246 wm9713->hp_mixer[mixer] &= ~mask;
247
248 change = old != wm9713->hp_mixer[mixer];
249 if (change) {
250 update.kcontrol = kcontrol;
251 update.reg = wm9713_mixer_mute_regs[shift];
252 update.mask = 0x8000;
253 if ((wm9713->hp_mixer[0] & mask) ||
254 (wm9713->hp_mixer[1] & mask))
255 update.val = 0x0;
256 else
257 update.val = 0x8000;
258
259 snd_soc_dapm_mixer_update_power(dapm, kcontrol, val,
260 &update);
261 }
262
263 mutex_unlock(&wm9713->lock);
264
265 return change;
266 }
267
wm9713_hp_mixer_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)268 static int wm9713_hp_mixer_get(struct snd_kcontrol *kcontrol,
269 struct snd_ctl_elem_value *ucontrol)
270 {
271 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
272 struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
273 struct wm9713_priv *wm9713 = snd_soc_component_get_drvdata(component);
274 struct soc_mixer_control *mc =
275 (struct soc_mixer_control *)kcontrol->private_value;
276 unsigned int mixer, shift;
277
278 mixer = mc->shift >> 8;
279 shift = mc->shift & 0xff;
280
281 ucontrol->value.integer.value[0] =
282 (wm9713->hp_mixer[mixer] >> shift) & 1;
283
284 return 0;
285 }
286
287 #define WM9713_HP_MIXER_CTRL(xname, xmixer, xshift) \
288 SOC_DOUBLE_EXT(xname, SND_SOC_NOPM, xshift, xmixer, 1, 0, \
289 wm9713_hp_mixer_get, wm9713_hp_mixer_put)
290
291 /* Left Headphone Mixers */
292 static const struct snd_kcontrol_new wm9713_hpl_mixer_controls[] = {
293 WM9713_HP_MIXER_CTRL("Beep Playback Switch", HPL_MIXER, 5),
294 WM9713_HP_MIXER_CTRL("Voice Playback Switch", HPL_MIXER, 4),
295 WM9713_HP_MIXER_CTRL("Aux Playback Switch", HPL_MIXER, 3),
296 WM9713_HP_MIXER_CTRL("PCM Playback Switch", HPL_MIXER, 2),
297 WM9713_HP_MIXER_CTRL("MonoIn Playback Switch", HPL_MIXER, 1),
298 WM9713_HP_MIXER_CTRL("Bypass Playback Switch", HPL_MIXER, 0),
299 };
300
301 /* Right Headphone Mixers */
302 static const struct snd_kcontrol_new wm9713_hpr_mixer_controls[] = {
303 WM9713_HP_MIXER_CTRL("Beep Playback Switch", HPR_MIXER, 5),
304 WM9713_HP_MIXER_CTRL("Voice Playback Switch", HPR_MIXER, 4),
305 WM9713_HP_MIXER_CTRL("Aux Playback Switch", HPR_MIXER, 3),
306 WM9713_HP_MIXER_CTRL("PCM Playback Switch", HPR_MIXER, 2),
307 WM9713_HP_MIXER_CTRL("MonoIn Playback Switch", HPR_MIXER, 1),
308 WM9713_HP_MIXER_CTRL("Bypass Playback Switch", HPR_MIXER, 0),
309 };
310
311 /* headphone capture mux */
312 static const struct snd_kcontrol_new wm9713_hp_rec_mux_controls =
313 SOC_DAPM_ENUM("Route", wm9713_enum[1]);
314
315 /* headphone mic mux */
316 static const struct snd_kcontrol_new wm9713_hp_mic_mux_controls =
317 SOC_DAPM_ENUM("Route", wm9713_enum[0]);
318
319 /* Speaker Mixer */
320 static const struct snd_kcontrol_new wm9713_speaker_mixer_controls[] = {
321 SOC_DAPM_SINGLE("Beep Playback Switch", AC97_AUX, 11, 1, 1),
322 SOC_DAPM_SINGLE("Voice Playback Switch", AC97_PCM, 11, 1, 1),
323 SOC_DAPM_SINGLE("Aux Playback Switch", AC97_REC_SEL, 11, 1, 1),
324 SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PHONE, 14, 1, 1),
325 SOC_DAPM_SINGLE("MonoIn Playback Switch", AC97_MASTER_TONE, 14, 1, 1),
326 SOC_DAPM_SINGLE("Bypass Playback Switch", AC97_PC_BEEP, 14, 1, 1),
327 };
328
329 /* Mono Mixer */
330 static const struct snd_kcontrol_new wm9713_mono_mixer_controls[] = {
331 SOC_DAPM_SINGLE("Beep Playback Switch", AC97_AUX, 7, 1, 1),
332 SOC_DAPM_SINGLE("Voice Playback Switch", AC97_PCM, 7, 1, 1),
333 SOC_DAPM_SINGLE("Aux Playback Switch", AC97_REC_SEL, 7, 1, 1),
334 SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PHONE, 13, 1, 1),
335 SOC_DAPM_SINGLE("MonoIn Playback Switch", AC97_MASTER_TONE, 13, 1, 1),
336 SOC_DAPM_SINGLE("Bypass Playback Switch", AC97_PC_BEEP, 13, 1, 1),
337 SOC_DAPM_SINGLE("Mic 1 Sidetone Switch", AC97_LINE, 7, 1, 1),
338 SOC_DAPM_SINGLE("Mic 2 Sidetone Switch", AC97_LINE, 6, 1, 1),
339 };
340
341 /* mono mic mux */
342 static const struct snd_kcontrol_new wm9713_mono_mic_mux_controls =
343 SOC_DAPM_ENUM("Route", wm9713_enum[2]);
344
345 /* mono output mux */
346 static const struct snd_kcontrol_new wm9713_mono_mux_controls =
347 SOC_DAPM_ENUM("Route", wm9713_enum[7]);
348
349 /* speaker left output mux */
350 static const struct snd_kcontrol_new wm9713_hp_spkl_mux_controls =
351 SOC_DAPM_ENUM("Route", wm9713_enum[8]);
352
353 /* speaker right output mux */
354 static const struct snd_kcontrol_new wm9713_hp_spkr_mux_controls =
355 SOC_DAPM_ENUM("Route", wm9713_enum[9]);
356
357 /* headphone left output mux */
358 static const struct snd_kcontrol_new wm9713_hpl_out_mux_controls =
359 SOC_DAPM_ENUM("Route", wm9713_enum[10]);
360
361 /* headphone right output mux */
362 static const struct snd_kcontrol_new wm9713_hpr_out_mux_controls =
363 SOC_DAPM_ENUM("Route", wm9713_enum[11]);
364
365 /* Out3 mux */
366 static const struct snd_kcontrol_new wm9713_out3_mux_controls =
367 SOC_DAPM_ENUM("Route", wm9713_enum[12]);
368
369 /* Out4 mux */
370 static const struct snd_kcontrol_new wm9713_out4_mux_controls =
371 SOC_DAPM_ENUM("Route", wm9713_enum[13]);
372
373 /* DAC inv mux 1 */
374 static const struct snd_kcontrol_new wm9713_dac_inv1_mux_controls =
375 SOC_DAPM_ENUM("Route", wm9713_enum[14]);
376
377 /* DAC inv mux 2 */
378 static const struct snd_kcontrol_new wm9713_dac_inv2_mux_controls =
379 SOC_DAPM_ENUM("Route", wm9713_enum[15]);
380
381 /* Capture source left */
382 static const struct snd_kcontrol_new wm9713_rec_srcl_mux_controls =
383 SOC_DAPM_ENUM("Route", wm9713_enum[3]);
384
385 /* Capture source right */
386 static const struct snd_kcontrol_new wm9713_rec_srcr_mux_controls =
387 SOC_DAPM_ENUM("Route", wm9713_enum[4]);
388
389 /* mic source */
390 static const struct snd_kcontrol_new wm9713_mic_sel_mux_controls =
391 SOC_DAPM_ENUM("Route", wm9713_enum[18]);
392
393 /* mic source B virtual control */
394 static const struct snd_kcontrol_new wm9713_micb_sel_mux_controls =
395 SOC_DAPM_ENUM("Route", wm9713_enum[19]);
396
397 static const struct snd_soc_dapm_widget wm9713_dapm_widgets[] = {
398 SND_SOC_DAPM_MUX("Capture Headphone Mux", SND_SOC_NOPM, 0, 0,
399 &wm9713_hp_rec_mux_controls),
400 SND_SOC_DAPM_MUX("Sidetone Mux", SND_SOC_NOPM, 0, 0,
401 &wm9713_hp_mic_mux_controls),
402 SND_SOC_DAPM_MUX("Capture Mono Mux", SND_SOC_NOPM, 0, 0,
403 &wm9713_mono_mic_mux_controls),
404 SND_SOC_DAPM_MUX("Mono Out Mux", SND_SOC_NOPM, 0, 0,
405 &wm9713_mono_mux_controls),
406 SND_SOC_DAPM_MUX("Left Speaker Out Mux", SND_SOC_NOPM, 0, 0,
407 &wm9713_hp_spkl_mux_controls),
408 SND_SOC_DAPM_MUX("Right Speaker Out Mux", SND_SOC_NOPM, 0, 0,
409 &wm9713_hp_spkr_mux_controls),
410 SND_SOC_DAPM_MUX("Left Headphone Out Mux", SND_SOC_NOPM, 0, 0,
411 &wm9713_hpl_out_mux_controls),
412 SND_SOC_DAPM_MUX("Right Headphone Out Mux", SND_SOC_NOPM, 0, 0,
413 &wm9713_hpr_out_mux_controls),
414 SND_SOC_DAPM_MUX("Out 3 Mux", SND_SOC_NOPM, 0, 0,
415 &wm9713_out3_mux_controls),
416 SND_SOC_DAPM_MUX("Out 4 Mux", SND_SOC_NOPM, 0, 0,
417 &wm9713_out4_mux_controls),
418 SND_SOC_DAPM_MUX("DAC Inv Mux 1", SND_SOC_NOPM, 0, 0,
419 &wm9713_dac_inv1_mux_controls),
420 SND_SOC_DAPM_MUX("DAC Inv Mux 2", SND_SOC_NOPM, 0, 0,
421 &wm9713_dac_inv2_mux_controls),
422 SND_SOC_DAPM_MUX("Left Capture Source", SND_SOC_NOPM, 0, 0,
423 &wm9713_rec_srcl_mux_controls),
424 SND_SOC_DAPM_MUX("Right Capture Source", SND_SOC_NOPM, 0, 0,
425 &wm9713_rec_srcr_mux_controls),
426 SND_SOC_DAPM_MUX("Mic A Source", SND_SOC_NOPM, 0, 0,
427 &wm9713_mic_sel_mux_controls),
428 SND_SOC_DAPM_MUX("Mic B Source", SND_SOC_NOPM, 0, 0,
429 &wm9713_micb_sel_mux_controls),
430 SND_SOC_DAPM_MIXER("Left HP Mixer", AC97_EXTENDED_MID, 3, 1,
431 &wm9713_hpl_mixer_controls[0], ARRAY_SIZE(wm9713_hpl_mixer_controls)),
432 SND_SOC_DAPM_MIXER("Right HP Mixer", AC97_EXTENDED_MID, 2, 1,
433 &wm9713_hpr_mixer_controls[0], ARRAY_SIZE(wm9713_hpr_mixer_controls)),
434 SND_SOC_DAPM_MIXER("Mono Mixer", AC97_EXTENDED_MID, 0, 1,
435 &wm9713_mono_mixer_controls[0], ARRAY_SIZE(wm9713_mono_mixer_controls)),
436 SND_SOC_DAPM_MIXER("Speaker Mixer", AC97_EXTENDED_MID, 1, 1,
437 &wm9713_speaker_mixer_controls[0],
438 ARRAY_SIZE(wm9713_speaker_mixer_controls)),
439 SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", AC97_EXTENDED_MID, 7, 1),
440 SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", AC97_EXTENDED_MID, 6, 1),
441 SND_SOC_DAPM_MIXER("AC97 Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
442 SND_SOC_DAPM_MIXER("HP Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
443 SND_SOC_DAPM_MIXER("Line Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
444 SND_SOC_DAPM_MIXER("Capture Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
445 SND_SOC_DAPM_DAC_E("Voice DAC", "Voice Playback", AC97_EXTENDED_MID, 12, 1,
446 wm9713_voice_shutdown, SND_SOC_DAPM_PRE_PMD),
447 SND_SOC_DAPM_DAC("Aux DAC", "Aux Playback", AC97_EXTENDED_MID, 11, 1),
448 SND_SOC_DAPM_PGA("Left ADC", AC97_EXTENDED_MID, 5, 1, NULL, 0),
449 SND_SOC_DAPM_PGA("Right ADC", AC97_EXTENDED_MID, 4, 1, NULL, 0),
450 SND_SOC_DAPM_ADC("Left HiFi ADC", "Left HiFi Capture", SND_SOC_NOPM, 0, 0),
451 SND_SOC_DAPM_ADC("Right HiFi ADC", "Right HiFi Capture", SND_SOC_NOPM, 0, 0),
452 SND_SOC_DAPM_ADC("Left Voice ADC", "Left Voice Capture", SND_SOC_NOPM, 0, 0),
453 SND_SOC_DAPM_ADC("Right Voice ADC", "Right Voice Capture", SND_SOC_NOPM, 0, 0),
454 SND_SOC_DAPM_PGA("Left Headphone", AC97_EXTENDED_MSTATUS, 10, 1, NULL, 0),
455 SND_SOC_DAPM_PGA("Right Headphone", AC97_EXTENDED_MSTATUS, 9, 1, NULL, 0),
456 SND_SOC_DAPM_PGA("Left Speaker", AC97_EXTENDED_MSTATUS, 8, 1, NULL, 0),
457 SND_SOC_DAPM_PGA("Right Speaker", AC97_EXTENDED_MSTATUS, 7, 1, NULL, 0),
458 SND_SOC_DAPM_PGA("Out 3", AC97_EXTENDED_MSTATUS, 11, 1, NULL, 0),
459 SND_SOC_DAPM_PGA("Out 4", AC97_EXTENDED_MSTATUS, 12, 1, NULL, 0),
460 SND_SOC_DAPM_PGA("Mono Out", AC97_EXTENDED_MSTATUS, 13, 1, NULL, 0),
461 SND_SOC_DAPM_PGA("Left Line In", AC97_EXTENDED_MSTATUS, 6, 1, NULL, 0),
462 SND_SOC_DAPM_PGA("Right Line In", AC97_EXTENDED_MSTATUS, 5, 1, NULL, 0),
463 SND_SOC_DAPM_PGA("Mono In", AC97_EXTENDED_MSTATUS, 4, 1, NULL, 0),
464 SND_SOC_DAPM_PGA("Mic A PGA", AC97_EXTENDED_MSTATUS, 3, 1, NULL, 0),
465 SND_SOC_DAPM_PGA("Mic B PGA", AC97_EXTENDED_MSTATUS, 2, 1, NULL, 0),
466 SND_SOC_DAPM_PGA("Mic A Pre Amp", AC97_EXTENDED_MSTATUS, 1, 1, NULL, 0),
467 SND_SOC_DAPM_PGA("Mic B Pre Amp", AC97_EXTENDED_MSTATUS, 0, 1, NULL, 0),
468 SND_SOC_DAPM_MICBIAS("Mic Bias", AC97_EXTENDED_MSTATUS, 14, 1),
469 SND_SOC_DAPM_OUTPUT("MONO"),
470 SND_SOC_DAPM_OUTPUT("HPL"),
471 SND_SOC_DAPM_OUTPUT("HPR"),
472 SND_SOC_DAPM_OUTPUT("SPKL"),
473 SND_SOC_DAPM_OUTPUT("SPKR"),
474 SND_SOC_DAPM_OUTPUT("OUT3"),
475 SND_SOC_DAPM_OUTPUT("OUT4"),
476 SND_SOC_DAPM_INPUT("LINEL"),
477 SND_SOC_DAPM_INPUT("LINER"),
478 SND_SOC_DAPM_INPUT("MONOIN"),
479 SND_SOC_DAPM_INPUT("PCBEEP"),
480 SND_SOC_DAPM_INPUT("MIC1"),
481 SND_SOC_DAPM_INPUT("MIC2A"),
482 SND_SOC_DAPM_INPUT("MIC2B"),
483 SND_SOC_DAPM_VMID("VMID"),
484 };
485
486 static const struct snd_soc_dapm_route wm9713_audio_map[] = {
487 /* left HP mixer */
488 {"Left HP Mixer", "Beep Playback Switch", "PCBEEP"},
489 {"Left HP Mixer", "Voice Playback Switch", "Voice DAC"},
490 {"Left HP Mixer", "Aux Playback Switch", "Aux DAC"},
491 {"Left HP Mixer", "Bypass Playback Switch", "Left Line In"},
492 {"Left HP Mixer", "PCM Playback Switch", "Left DAC"},
493 {"Left HP Mixer", "MonoIn Playback Switch", "Mono In"},
494 {"Left HP Mixer", NULL, "Capture Headphone Mux"},
495
496 /* right HP mixer */
497 {"Right HP Mixer", "Beep Playback Switch", "PCBEEP"},
498 {"Right HP Mixer", "Voice Playback Switch", "Voice DAC"},
499 {"Right HP Mixer", "Aux Playback Switch", "Aux DAC"},
500 {"Right HP Mixer", "Bypass Playback Switch", "Right Line In"},
501 {"Right HP Mixer", "PCM Playback Switch", "Right DAC"},
502 {"Right HP Mixer", "MonoIn Playback Switch", "Mono In"},
503 {"Right HP Mixer", NULL, "Capture Headphone Mux"},
504
505 /* virtual mixer - mixes left & right channels for spk and mono */
506 {"AC97 Mixer", NULL, "Left DAC"},
507 {"AC97 Mixer", NULL, "Right DAC"},
508 {"Line Mixer", NULL, "Right Line In"},
509 {"Line Mixer", NULL, "Left Line In"},
510 {"HP Mixer", NULL, "Left HP Mixer"},
511 {"HP Mixer", NULL, "Right HP Mixer"},
512 {"Capture Mixer", NULL, "Left Capture Source"},
513 {"Capture Mixer", NULL, "Right Capture Source"},
514
515 /* speaker mixer */
516 {"Speaker Mixer", "Beep Playback Switch", "PCBEEP"},
517 {"Speaker Mixer", "Voice Playback Switch", "Voice DAC"},
518 {"Speaker Mixer", "Aux Playback Switch", "Aux DAC"},
519 {"Speaker Mixer", "Bypass Playback Switch", "Line Mixer"},
520 {"Speaker Mixer", "PCM Playback Switch", "AC97 Mixer"},
521 {"Speaker Mixer", "MonoIn Playback Switch", "Mono In"},
522
523 /* mono mixer */
524 {"Mono Mixer", "Beep Playback Switch", "PCBEEP"},
525 {"Mono Mixer", "Voice Playback Switch", "Voice DAC"},
526 {"Mono Mixer", "Aux Playback Switch", "Aux DAC"},
527 {"Mono Mixer", "Bypass Playback Switch", "Line Mixer"},
528 {"Mono Mixer", "PCM Playback Switch", "AC97 Mixer"},
529 {"Mono Mixer", "Mic 1 Sidetone Switch", "Mic A PGA"},
530 {"Mono Mixer", "Mic 2 Sidetone Switch", "Mic B PGA"},
531 {"Mono Mixer", NULL, "Capture Mono Mux"},
532
533 /* DAC inv mux 1 */
534 {"DAC Inv Mux 1", "Mono", "Mono Mixer"},
535 {"DAC Inv Mux 1", "Speaker", "Speaker Mixer"},
536 {"DAC Inv Mux 1", "Left Headphone", "Left HP Mixer"},
537 {"DAC Inv Mux 1", "Right Headphone", "Right HP Mixer"},
538 {"DAC Inv Mux 1", "Headphone Mono", "HP Mixer"},
539
540 /* DAC inv mux 2 */
541 {"DAC Inv Mux 2", "Mono", "Mono Mixer"},
542 {"DAC Inv Mux 2", "Speaker", "Speaker Mixer"},
543 {"DAC Inv Mux 2", "Left Headphone", "Left HP Mixer"},
544 {"DAC Inv Mux 2", "Right Headphone", "Right HP Mixer"},
545 {"DAC Inv Mux 2", "Headphone Mono", "HP Mixer"},
546
547 /* headphone left mux */
548 {"Left Headphone Out Mux", "Headphone", "Left HP Mixer"},
549
550 /* headphone right mux */
551 {"Right Headphone Out Mux", "Headphone", "Right HP Mixer"},
552
553 /* speaker left mux */
554 {"Left Speaker Out Mux", "Headphone", "Left HP Mixer"},
555 {"Left Speaker Out Mux", "Speaker", "Speaker Mixer"},
556 {"Left Speaker Out Mux", "Inv", "DAC Inv Mux 1"},
557
558 /* speaker right mux */
559 {"Right Speaker Out Mux", "Headphone", "Right HP Mixer"},
560 {"Right Speaker Out Mux", "Speaker", "Speaker Mixer"},
561 {"Right Speaker Out Mux", "Inv", "DAC Inv Mux 2"},
562
563 /* mono mux */
564 {"Mono Out Mux", "Mono", "Mono Mixer"},
565 {"Mono Out Mux", "Inv", "DAC Inv Mux 1"},
566
567 /* out 3 mux */
568 {"Out 3 Mux", "Inv 1", "DAC Inv Mux 1"},
569
570 /* out 4 mux */
571 {"Out 4 Mux", "Inv 2", "DAC Inv Mux 2"},
572
573 /* output pga */
574 {"HPL", NULL, "Left Headphone"},
575 {"Left Headphone", NULL, "Left Headphone Out Mux"},
576 {"HPR", NULL, "Right Headphone"},
577 {"Right Headphone", NULL, "Right Headphone Out Mux"},
578 {"OUT3", NULL, "Out 3"},
579 {"Out 3", NULL, "Out 3 Mux"},
580 {"OUT4", NULL, "Out 4"},
581 {"Out 4", NULL, "Out 4 Mux"},
582 {"SPKL", NULL, "Left Speaker"},
583 {"Left Speaker", NULL, "Left Speaker Out Mux"},
584 {"SPKR", NULL, "Right Speaker"},
585 {"Right Speaker", NULL, "Right Speaker Out Mux"},
586 {"MONO", NULL, "Mono Out"},
587 {"Mono Out", NULL, "Mono Out Mux"},
588
589 /* input pga */
590 {"Left Line In", NULL, "LINEL"},
591 {"Right Line In", NULL, "LINER"},
592 {"Mono In", NULL, "MONOIN"},
593 {"Mic A PGA", NULL, "Mic A Pre Amp"},
594 {"Mic B PGA", NULL, "Mic B Pre Amp"},
595
596 /* left capture select */
597 {"Left Capture Source", "Mic 1", "Mic A Pre Amp"},
598 {"Left Capture Source", "Mic 2", "Mic B Pre Amp"},
599 {"Left Capture Source", "Line", "LINEL"},
600 {"Left Capture Source", "Mono In", "MONOIN"},
601 {"Left Capture Source", "Headphone", "Left HP Mixer"},
602 {"Left Capture Source", "Speaker", "Speaker Mixer"},
603 {"Left Capture Source", "Mono Out", "Mono Mixer"},
604
605 /* right capture select */
606 {"Right Capture Source", "Mic 1", "Mic A Pre Amp"},
607 {"Right Capture Source", "Mic 2", "Mic B Pre Amp"},
608 {"Right Capture Source", "Line", "LINER"},
609 {"Right Capture Source", "Mono In", "MONOIN"},
610 {"Right Capture Source", "Headphone", "Right HP Mixer"},
611 {"Right Capture Source", "Speaker", "Speaker Mixer"},
612 {"Right Capture Source", "Mono Out", "Mono Mixer"},
613
614 /* left ADC */
615 {"Left ADC", NULL, "Left Capture Source"},
616 {"Left Voice ADC", NULL, "Left ADC"},
617 {"Left HiFi ADC", NULL, "Left ADC"},
618
619 /* right ADC */
620 {"Right ADC", NULL, "Right Capture Source"},
621 {"Right Voice ADC", NULL, "Right ADC"},
622 {"Right HiFi ADC", NULL, "Right ADC"},
623
624 /* mic */
625 {"Mic A Pre Amp", NULL, "Mic A Source"},
626 {"Mic A Source", "Mic 1", "MIC1"},
627 {"Mic A Source", "Mic 2 A", "MIC2A"},
628 {"Mic A Source", "Mic 2 B", "Mic B Source"},
629 {"Mic B Pre Amp", "MPB", "Mic B Source"},
630 {"Mic B Source", NULL, "MIC2B"},
631
632 /* headphone capture */
633 {"Capture Headphone Mux", "Stereo", "Capture Mixer"},
634 {"Capture Headphone Mux", "Left", "Left Capture Source"},
635 {"Capture Headphone Mux", "Right", "Right Capture Source"},
636
637 /* mono capture */
638 {"Capture Mono Mux", "Stereo", "Capture Mixer"},
639 {"Capture Mono Mux", "Left", "Left Capture Source"},
640 {"Capture Mono Mux", "Right", "Right Capture Source"},
641 };
642
wm9713_readable_reg(struct device * dev,unsigned int reg)643 static bool wm9713_readable_reg(struct device *dev, unsigned int reg)
644 {
645 switch (reg) {
646 case AC97_RESET ... AC97_PCM_SURR_DAC_RATE:
647 case AC97_PCM_LR_ADC_RATE:
648 case AC97_CENTER_LFE_MASTER:
649 case AC97_SPDIF ... AC97_LINE1_LEVEL:
650 case AC97_GPIO_CFG ... 0x5c:
651 case AC97_CODEC_CLASS_REV ... AC97_PCI_SID:
652 case 0x74 ... AC97_VENDOR_ID2:
653 return true;
654 default:
655 return false;
656 }
657 }
658
wm9713_writeable_reg(struct device * dev,unsigned int reg)659 static bool wm9713_writeable_reg(struct device *dev, unsigned int reg)
660 {
661 switch (reg) {
662 case AC97_VENDOR_ID1:
663 case AC97_VENDOR_ID2:
664 return false;
665 default:
666 return wm9713_readable_reg(dev, reg);
667 }
668 }
669
670 static const struct reg_default wm9713_reg_defaults[] = {
671 { 0x02, 0x8080 }, /* Speaker Output Volume */
672 { 0x04, 0x8080 }, /* Headphone Output Volume */
673 { 0x06, 0x8080 }, /* Out3/OUT4 Volume */
674 { 0x08, 0xc880 }, /* Mono Volume */
675 { 0x0a, 0xe808 }, /* LINEIN Volume */
676 { 0x0c, 0xe808 }, /* DAC PGA Volume */
677 { 0x0e, 0x0808 }, /* MIC PGA Volume */
678 { 0x10, 0x00da }, /* MIC Routing Control */
679 { 0x12, 0x8000 }, /* Record PGA Volume */
680 { 0x14, 0xd600 }, /* Record Routing */
681 { 0x16, 0xaaa0 }, /* PCBEEP Volume */
682 { 0x18, 0xaaa0 }, /* VxDAC Volume */
683 { 0x1a, 0xaaa0 }, /* AUXDAC Volume */
684 { 0x1c, 0x0000 }, /* Output PGA Mux */
685 { 0x1e, 0x0000 }, /* DAC 3D control */
686 { 0x20, 0x0f0f }, /* DAC Tone Control*/
687 { 0x22, 0x0040 }, /* MIC Input Select & Bias */
688 { 0x24, 0x0000 }, /* Output Volume Mapping & Jack */
689 { 0x26, 0x7f00 }, /* Powerdown Ctrl/Stat*/
690 { 0x28, 0x0405 }, /* Extended Audio ID */
691 { 0x2a, 0x0410 }, /* Extended Audio Start/Ctrl */
692 { 0x2c, 0xbb80 }, /* Audio DACs Sample Rate */
693 { 0x2e, 0xbb80 }, /* AUXDAC Sample Rate */
694 { 0x32, 0xbb80 }, /* Audio ADCs Sample Rate */
695 { 0x36, 0x4523 }, /* PCM codec control */
696 { 0x3a, 0x2000 }, /* SPDIF control */
697 { 0x3c, 0xfdff }, /* Powerdown 1 */
698 { 0x3e, 0xffff }, /* Powerdown 2 */
699 { 0x40, 0x0000 }, /* General Purpose */
700 { 0x42, 0x0000 }, /* Fast Power-Up Control */
701 { 0x44, 0x0080 }, /* MCLK/PLL Control */
702 { 0x46, 0x0000 }, /* MCLK/PLL Control */
703 { 0x4c, 0xfffe }, /* GPIO Pin Configuration */
704 { 0x4e, 0xffff }, /* GPIO Pin Polarity / Type */
705 { 0x50, 0x0000 }, /* GPIO Pin Sticky */
706 { 0x52, 0x0000 }, /* GPIO Pin Wake-Up */
707 /* GPIO Pin Status */
708 { 0x56, 0xfffe }, /* GPIO Pin Sharing */
709 { 0x58, 0x4000 }, /* GPIO PullUp/PullDown */
710 { 0x5a, 0x0000 }, /* Additional Functions 1 */
711 { 0x5c, 0x0000 }, /* Additional Functions 2 */
712 { 0x60, 0xb032 }, /* ALC Control */
713 { 0x62, 0x3e00 }, /* ALC / Noise Gate Control */
714 { 0x64, 0x0000 }, /* AUXDAC input control */
715 { 0x74, 0x0000 }, /* Digitiser Reg 1 */
716 { 0x76, 0x0006 }, /* Digitiser Reg 2 */
717 { 0x78, 0x0001 }, /* Digitiser Reg 3 */
718 { 0x7a, 0x0000 }, /* Digitiser Read Back */
719 };
720
721 static const struct regmap_config wm9713_regmap_config = {
722 .reg_bits = 16,
723 .reg_stride = 2,
724 .val_bits = 16,
725 .max_register = 0x7e,
726 .cache_type = REGCACHE_MAPLE,
727
728 .reg_defaults = wm9713_reg_defaults,
729 .num_reg_defaults = ARRAY_SIZE(wm9713_reg_defaults),
730 .volatile_reg = regmap_ac97_default_volatile,
731 .readable_reg = wm9713_readable_reg,
732 .writeable_reg = wm9713_writeable_reg,
733 };
734
735 /* PLL divisors */
736 struct _pll_div {
737 u32 divsel:1;
738 u32 divctl:1;
739 u32 lf:1;
740 u32 n:4;
741 u32 k:24;
742 };
743
744 /* The size in bits of the PLL divide multiplied by 10
745 * to allow rounding later */
746 #define FIXED_PLL_SIZE ((1 << 22) * 10)
747
pll_factors(struct snd_soc_component * component,struct _pll_div * pll_div,unsigned int source)748 static void pll_factors(struct snd_soc_component *component,
749 struct _pll_div *pll_div, unsigned int source)
750 {
751 u64 Kpart;
752 unsigned int K, Ndiv, Nmod, target;
753
754 /* The PLL output is always 98.304MHz. */
755 target = 98304000;
756
757 /* If the input frequency is over 14.4MHz then scale it down. */
758 if (source > 14400000) {
759 source >>= 1;
760 pll_div->divsel = 1;
761
762 if (source > 14400000) {
763 source >>= 1;
764 pll_div->divctl = 1;
765 } else
766 pll_div->divctl = 0;
767
768 } else {
769 pll_div->divsel = 0;
770 pll_div->divctl = 0;
771 }
772
773 /* Low frequency sources require an additional divide in the
774 * loop.
775 */
776 if (source < 8192000) {
777 pll_div->lf = 1;
778 target >>= 2;
779 } else
780 pll_div->lf = 0;
781
782 Ndiv = target / source;
783 if ((Ndiv < 5) || (Ndiv > 12))
784 dev_warn(component->dev,
785 "WM9713 PLL N value %u out of recommended range!\n",
786 Ndiv);
787
788 pll_div->n = Ndiv;
789 Nmod = target % source;
790 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
791
792 do_div(Kpart, source);
793
794 K = Kpart & 0xFFFFFFFF;
795
796 /* Check if we need to round */
797 if ((K % 10) >= 5)
798 K += 5;
799
800 /* Move down to proper range now rounding is done */
801 K /= 10;
802
803 pll_div->k = K;
804 }
805
806 /*
807 * Please note that changing the PLL input frequency may require
808 * resynchronisation with the AC97 controller.
809 */
wm9713_set_pll(struct snd_soc_component * component,int pll_id,unsigned int freq_in,unsigned int freq_out)810 static int wm9713_set_pll(struct snd_soc_component *component,
811 int pll_id, unsigned int freq_in, unsigned int freq_out)
812 {
813 struct wm9713_priv *wm9713 = snd_soc_component_get_drvdata(component);
814 u16 reg, reg2;
815 struct _pll_div pll_div;
816
817 /* turn PLL off ? */
818 if (freq_in == 0) {
819 /* disable PLL power and select ext source */
820 snd_soc_component_update_bits(component, AC97_HANDSET_RATE, 0x0080, 0x0080);
821 snd_soc_component_update_bits(component, AC97_EXTENDED_MID, 0x0200, 0x0200);
822 wm9713->pll_in = 0;
823 return 0;
824 }
825
826 pll_factors(component, &pll_div, freq_in);
827
828 if (pll_div.k == 0) {
829 reg = (pll_div.n << 12) | (pll_div.lf << 11) |
830 (pll_div.divsel << 9) | (pll_div.divctl << 8);
831 snd_soc_component_write(component, AC97_LINE1_LEVEL, reg);
832 } else {
833 /* write the fractional k to the reg 0x46 pages */
834 reg2 = (pll_div.n << 12) | (pll_div.lf << 11) | (1 << 10) |
835 (pll_div.divsel << 9) | (pll_div.divctl << 8);
836
837 /* K [21:20] */
838 reg = reg2 | (0x5 << 4) | (pll_div.k >> 20);
839 snd_soc_component_write(component, AC97_LINE1_LEVEL, reg);
840
841 /* K [19:16] */
842 reg = reg2 | (0x4 << 4) | ((pll_div.k >> 16) & 0xf);
843 snd_soc_component_write(component, AC97_LINE1_LEVEL, reg);
844
845 /* K [15:12] */
846 reg = reg2 | (0x3 << 4) | ((pll_div.k >> 12) & 0xf);
847 snd_soc_component_write(component, AC97_LINE1_LEVEL, reg);
848
849 /* K [11:8] */
850 reg = reg2 | (0x2 << 4) | ((pll_div.k >> 8) & 0xf);
851 snd_soc_component_write(component, AC97_LINE1_LEVEL, reg);
852
853 /* K [7:4] */
854 reg = reg2 | (0x1 << 4) | ((pll_div.k >> 4) & 0xf);
855 snd_soc_component_write(component, AC97_LINE1_LEVEL, reg);
856
857 reg = reg2 | (0x0 << 4) | (pll_div.k & 0xf); /* K [3:0] */
858 snd_soc_component_write(component, AC97_LINE1_LEVEL, reg);
859 }
860
861 /* turn PLL on and select as source */
862 snd_soc_component_update_bits(component, AC97_EXTENDED_MID, 0x0200, 0x0000);
863 snd_soc_component_update_bits(component, AC97_HANDSET_RATE, 0x0080, 0x0000);
864 wm9713->pll_in = freq_in;
865
866 /* wait 10ms AC97 link frames for the link to stabilise */
867 schedule_timeout_interruptible(msecs_to_jiffies(10));
868 return 0;
869 }
870
wm9713_set_dai_pll(struct snd_soc_dai * codec_dai,int pll_id,int source,unsigned int freq_in,unsigned int freq_out)871 static int wm9713_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
872 int source, unsigned int freq_in, unsigned int freq_out)
873 {
874 struct snd_soc_component *component = codec_dai->component;
875 return wm9713_set_pll(component, pll_id, freq_in, freq_out);
876 }
877
878 /*
879 * Tristate the PCM DAI lines, tristate can be disabled by calling
880 * wm9713_set_dai_fmt()
881 */
wm9713_set_dai_tristate(struct snd_soc_dai * codec_dai,int tristate)882 static int wm9713_set_dai_tristate(struct snd_soc_dai *codec_dai,
883 int tristate)
884 {
885 struct snd_soc_component *component = codec_dai->component;
886
887 if (tristate)
888 snd_soc_component_update_bits(component, AC97_CENTER_LFE_MASTER,
889 0x6000, 0x0000);
890
891 return 0;
892 }
893
894 /*
895 * Configure WM9713 clock dividers.
896 * Voice DAC needs 256 FS
897 */
wm9713_set_dai_clkdiv(struct snd_soc_dai * codec_dai,int div_id,int div)898 static int wm9713_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
899 int div_id, int div)
900 {
901 struct snd_soc_component *component = codec_dai->component;
902
903 switch (div_id) {
904 case WM9713_PCMCLK_DIV:
905 snd_soc_component_update_bits(component, AC97_HANDSET_RATE, 0x0f00, div);
906 break;
907 case WM9713_CLKA_MULT:
908 snd_soc_component_update_bits(component, AC97_HANDSET_RATE, 0x0002, div);
909 break;
910 case WM9713_CLKB_MULT:
911 snd_soc_component_update_bits(component, AC97_HANDSET_RATE, 0x0004, div);
912 break;
913 case WM9713_HIFI_DIV:
914 snd_soc_component_update_bits(component, AC97_HANDSET_RATE, 0x7000, div);
915 break;
916 case WM9713_PCMBCLK_DIV:
917 snd_soc_component_update_bits(component, AC97_CENTER_LFE_MASTER, 0x0e00, div);
918 break;
919 case WM9713_PCMCLK_PLL_DIV:
920 snd_soc_component_update_bits(component, AC97_LINE1_LEVEL,
921 0x007f, div | 0x60);
922 break;
923 case WM9713_HIFI_PLL_DIV:
924 snd_soc_component_update_bits(component, AC97_LINE1_LEVEL,
925 0x007f, div | 0x70);
926 break;
927 default:
928 return -EINVAL;
929 }
930
931 return 0;
932 }
933
wm9713_set_dai_fmt(struct snd_soc_dai * codec_dai,unsigned int fmt)934 static int wm9713_set_dai_fmt(struct snd_soc_dai *codec_dai,
935 unsigned int fmt)
936 {
937 struct snd_soc_component *component = codec_dai->component;
938 u16 gpio = snd_soc_component_read(component, AC97_GPIO_CFG) & 0xffc5;
939 u16 reg = 0x8000;
940
941 /* clock masters */
942 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
943 case SND_SOC_DAIFMT_CBP_CFP:
944 reg |= 0x4000;
945 gpio |= 0x0010;
946 break;
947 case SND_SOC_DAIFMT_CBP_CFC:
948 reg |= 0x6000;
949 gpio |= 0x0018;
950 break;
951 case SND_SOC_DAIFMT_CBC_CFC:
952 reg |= 0x2000;
953 gpio |= 0x001a;
954 break;
955 case SND_SOC_DAIFMT_CBC_CFP:
956 gpio |= 0x0012;
957 break;
958 }
959
960 /* clock inversion */
961 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
962 case SND_SOC_DAIFMT_IB_IF:
963 reg |= 0x00c0;
964 break;
965 case SND_SOC_DAIFMT_IB_NF:
966 reg |= 0x0080;
967 break;
968 case SND_SOC_DAIFMT_NB_IF:
969 reg |= 0x0040;
970 break;
971 }
972
973 /* DAI format */
974 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
975 case SND_SOC_DAIFMT_I2S:
976 reg |= 0x0002;
977 break;
978 case SND_SOC_DAIFMT_RIGHT_J:
979 break;
980 case SND_SOC_DAIFMT_LEFT_J:
981 reg |= 0x0001;
982 break;
983 case SND_SOC_DAIFMT_DSP_A:
984 reg |= 0x0003;
985 break;
986 case SND_SOC_DAIFMT_DSP_B:
987 reg |= 0x0043;
988 break;
989 }
990
991 snd_soc_component_write(component, AC97_GPIO_CFG, gpio);
992 snd_soc_component_write(component, AC97_CENTER_LFE_MASTER, reg);
993 return 0;
994 }
995
wm9713_pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)996 static int wm9713_pcm_hw_params(struct snd_pcm_substream *substream,
997 struct snd_pcm_hw_params *params,
998 struct snd_soc_dai *dai)
999 {
1000 struct snd_soc_component *component = dai->component;
1001
1002 /* enable PCM interface in master mode */
1003 switch (params_width(params)) {
1004 case 16:
1005 break;
1006 case 20:
1007 snd_soc_component_update_bits(component, AC97_CENTER_LFE_MASTER,
1008 0x000c, 0x0004);
1009 break;
1010 case 24:
1011 snd_soc_component_update_bits(component, AC97_CENTER_LFE_MASTER,
1012 0x000c, 0x0008);
1013 break;
1014 case 32:
1015 snd_soc_component_update_bits(component, AC97_CENTER_LFE_MASTER,
1016 0x000c, 0x000c);
1017 break;
1018 }
1019 return 0;
1020 }
1021
ac97_hifi_prepare(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1022 static int ac97_hifi_prepare(struct snd_pcm_substream *substream,
1023 struct snd_soc_dai *dai)
1024 {
1025 struct snd_soc_component *component = dai->component;
1026 struct snd_pcm_runtime *runtime = substream->runtime;
1027 int reg;
1028
1029 snd_soc_component_update_bits(component, AC97_EXTENDED_STATUS, 0x0001, 0x0001);
1030
1031 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1032 reg = AC97_PCM_FRONT_DAC_RATE;
1033 else
1034 reg = AC97_PCM_LR_ADC_RATE;
1035
1036 return snd_soc_component_write(component, reg, runtime->rate);
1037 }
1038
ac97_aux_prepare(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1039 static int ac97_aux_prepare(struct snd_pcm_substream *substream,
1040 struct snd_soc_dai *dai)
1041 {
1042 struct snd_soc_component *component = dai->component;
1043 struct snd_pcm_runtime *runtime = substream->runtime;
1044
1045 snd_soc_component_update_bits(component, AC97_EXTENDED_STATUS, 0x0001, 0x0001);
1046 snd_soc_component_update_bits(component, AC97_PCI_SID, 0x8000, 0x8000);
1047
1048 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
1049 return -ENODEV;
1050
1051 return snd_soc_component_write(component, AC97_PCM_SURR_DAC_RATE, runtime->rate);
1052 }
1053
1054 #define WM9713_RATES (SNDRV_PCM_RATE_8000 | \
1055 SNDRV_PCM_RATE_11025 | \
1056 SNDRV_PCM_RATE_22050 | \
1057 SNDRV_PCM_RATE_44100 | \
1058 SNDRV_PCM_RATE_48000)
1059
1060 #define WM9713_PCM_RATES (SNDRV_PCM_RATE_8000 | \
1061 SNDRV_PCM_RATE_11025 | \
1062 SNDRV_PCM_RATE_16000 | \
1063 SNDRV_PCM_RATE_22050 | \
1064 SNDRV_PCM_RATE_44100 | \
1065 SNDRV_PCM_RATE_48000)
1066
1067 #define WM9713_PCM_FORMATS \
1068 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1069 SNDRV_PCM_FMTBIT_S24_LE)
1070
1071 static const struct snd_soc_dai_ops wm9713_dai_ops_hifi = {
1072 .prepare = ac97_hifi_prepare,
1073 .set_clkdiv = wm9713_set_dai_clkdiv,
1074 .set_pll = wm9713_set_dai_pll,
1075 };
1076
1077 static const struct snd_soc_dai_ops wm9713_dai_ops_aux = {
1078 .prepare = ac97_aux_prepare,
1079 .set_clkdiv = wm9713_set_dai_clkdiv,
1080 .set_pll = wm9713_set_dai_pll,
1081 };
1082
1083 static const struct snd_soc_dai_ops wm9713_dai_ops_voice = {
1084 .hw_params = wm9713_pcm_hw_params,
1085 .set_clkdiv = wm9713_set_dai_clkdiv,
1086 .set_pll = wm9713_set_dai_pll,
1087 .set_fmt = wm9713_set_dai_fmt,
1088 .set_tristate = wm9713_set_dai_tristate,
1089 };
1090
1091 static struct snd_soc_dai_driver wm9713_dai[] = {
1092 {
1093 .name = "wm9713-hifi",
1094 .playback = {
1095 .stream_name = "HiFi Playback",
1096 .channels_min = 1,
1097 .channels_max = 2,
1098 .rates = WM9713_RATES,
1099 .formats = SND_SOC_STD_AC97_FMTS,},
1100 .capture = {
1101 .stream_name = "HiFi Capture",
1102 .channels_min = 1,
1103 .channels_max = 2,
1104 .rates = WM9713_RATES,
1105 .formats = SND_SOC_STD_AC97_FMTS,},
1106 .ops = &wm9713_dai_ops_hifi,
1107 },
1108 {
1109 .name = "wm9713-aux",
1110 .playback = {
1111 .stream_name = "Aux Playback",
1112 .channels_min = 1,
1113 .channels_max = 1,
1114 .rates = WM9713_RATES,
1115 .formats = SND_SOC_STD_AC97_FMTS,},
1116 .ops = &wm9713_dai_ops_aux,
1117 },
1118 {
1119 .name = "wm9713-voice",
1120 .playback = {
1121 .stream_name = "Voice Playback",
1122 .channels_min = 1,
1123 .channels_max = 1,
1124 .rates = WM9713_PCM_RATES,
1125 .formats = WM9713_PCM_FORMATS,},
1126 .capture = {
1127 .stream_name = "Voice Capture",
1128 .channels_min = 1,
1129 .channels_max = 2,
1130 .rates = WM9713_PCM_RATES,
1131 .formats = WM9713_PCM_FORMATS,},
1132 .ops = &wm9713_dai_ops_voice,
1133 .symmetric_rate = 1,
1134 },
1135 };
1136
wm9713_set_bias_level(struct snd_soc_component * component,enum snd_soc_bias_level level)1137 static int wm9713_set_bias_level(struct snd_soc_component *component,
1138 enum snd_soc_bias_level level)
1139 {
1140 switch (level) {
1141 case SND_SOC_BIAS_ON:
1142 /* enable thermal shutdown */
1143 snd_soc_component_update_bits(component, AC97_EXTENDED_MID, 0xe400, 0x0000);
1144 break;
1145 case SND_SOC_BIAS_PREPARE:
1146 break;
1147 case SND_SOC_BIAS_STANDBY:
1148 /* enable master bias and vmid */
1149 snd_soc_component_update_bits(component, AC97_EXTENDED_MID, 0xc400, 0x0000);
1150 snd_soc_component_write(component, AC97_POWERDOWN, 0x0000);
1151 break;
1152 case SND_SOC_BIAS_OFF:
1153 /* disable everything including AC link */
1154 snd_soc_component_write(component, AC97_EXTENDED_MID, 0xffff);
1155 snd_soc_component_write(component, AC97_EXTENDED_MSTATUS, 0xffff);
1156 snd_soc_component_write(component, AC97_POWERDOWN, 0xffff);
1157 break;
1158 }
1159 return 0;
1160 }
1161
wm9713_soc_suspend(struct snd_soc_component * component)1162 static int wm9713_soc_suspend(struct snd_soc_component *component)
1163 {
1164 /* Disable everything except touchpanel - that will be handled
1165 * by the touch driver and left disabled if touch is not in
1166 * use. */
1167 snd_soc_component_update_bits(component, AC97_EXTENDED_MID, 0x7fff,
1168 0x7fff);
1169 snd_soc_component_write(component, AC97_EXTENDED_MSTATUS, 0xffff);
1170 snd_soc_component_write(component, AC97_POWERDOWN, 0x6f00);
1171 snd_soc_component_write(component, AC97_POWERDOWN, 0xffff);
1172
1173 return 0;
1174 }
1175
wm9713_soc_resume(struct snd_soc_component * component)1176 static int wm9713_soc_resume(struct snd_soc_component *component)
1177 {
1178 struct wm9713_priv *wm9713 = snd_soc_component_get_drvdata(component);
1179 int ret;
1180
1181 ret = snd_ac97_reset(wm9713->ac97, true, WM9713_VENDOR_ID,
1182 WM9713_VENDOR_ID_MASK);
1183 if (ret < 0)
1184 return ret;
1185
1186 snd_soc_component_force_bias_level(component, SND_SOC_BIAS_STANDBY);
1187
1188 /* do we need to re-start the PLL ? */
1189 if (wm9713->pll_in)
1190 wm9713_set_pll(component, 0, wm9713->pll_in, 0);
1191
1192 /* only synchronise the codec if warm reset failed */
1193 if (ret == 0) {
1194 regcache_mark_dirty(component->regmap);
1195 snd_soc_component_cache_sync(component);
1196 }
1197
1198 return ret;
1199 }
1200
wm9713_soc_probe(struct snd_soc_component * component)1201 static int wm9713_soc_probe(struct snd_soc_component *component)
1202 {
1203 struct wm9713_priv *wm9713 = snd_soc_component_get_drvdata(component);
1204 struct regmap *regmap = NULL;
1205
1206 if (wm9713->mfd_pdata) {
1207 wm9713->ac97 = wm9713->mfd_pdata->ac97;
1208 regmap = wm9713->mfd_pdata->regmap;
1209 } else if (IS_ENABLED(CONFIG_SND_SOC_AC97_BUS)) {
1210 wm9713->ac97 = snd_soc_new_ac97_component(component, WM9713_VENDOR_ID,
1211 WM9713_VENDOR_ID_MASK);
1212 if (IS_ERR(wm9713->ac97))
1213 return PTR_ERR(wm9713->ac97);
1214 regmap = regmap_init_ac97(wm9713->ac97, &wm9713_regmap_config);
1215 if (IS_ERR(regmap)) {
1216 snd_soc_free_ac97_component(wm9713->ac97);
1217 return PTR_ERR(regmap);
1218 }
1219 } else {
1220 return -ENXIO;
1221 }
1222
1223 snd_soc_component_init_regmap(component, regmap);
1224
1225 /* unmute the adc - move to kcontrol */
1226 snd_soc_component_update_bits(component, AC97_CD, 0x7fff, 0x0000);
1227
1228 return 0;
1229 }
1230
wm9713_soc_remove(struct snd_soc_component * component)1231 static void wm9713_soc_remove(struct snd_soc_component *component)
1232 {
1233 struct wm9713_priv *wm9713 = snd_soc_component_get_drvdata(component);
1234
1235 if (IS_ENABLED(CONFIG_SND_SOC_AC97_BUS) && !wm9713->mfd_pdata) {
1236 snd_soc_component_exit_regmap(component);
1237 snd_soc_free_ac97_component(wm9713->ac97);
1238 }
1239 }
1240
1241 static const struct snd_soc_component_driver soc_component_dev_wm9713 = {
1242 .probe = wm9713_soc_probe,
1243 .remove = wm9713_soc_remove,
1244 .suspend = wm9713_soc_suspend,
1245 .resume = wm9713_soc_resume,
1246 .set_bias_level = wm9713_set_bias_level,
1247 .controls = wm9713_snd_ac97_controls,
1248 .num_controls = ARRAY_SIZE(wm9713_snd_ac97_controls),
1249 .dapm_widgets = wm9713_dapm_widgets,
1250 .num_dapm_widgets = ARRAY_SIZE(wm9713_dapm_widgets),
1251 .dapm_routes = wm9713_audio_map,
1252 .num_dapm_routes = ARRAY_SIZE(wm9713_audio_map),
1253 .idle_bias_on = 1,
1254 .use_pmdown_time = 1,
1255 .endianness = 1,
1256 };
1257
wm9713_probe(struct platform_device * pdev)1258 static int wm9713_probe(struct platform_device *pdev)
1259 {
1260 struct wm9713_priv *wm9713;
1261
1262 wm9713 = devm_kzalloc(&pdev->dev, sizeof(*wm9713), GFP_KERNEL);
1263 if (wm9713 == NULL)
1264 return -ENOMEM;
1265
1266 mutex_init(&wm9713->lock);
1267
1268 wm9713->mfd_pdata = dev_get_platdata(&pdev->dev);
1269 platform_set_drvdata(pdev, wm9713);
1270
1271 return devm_snd_soc_register_component(&pdev->dev,
1272 &soc_component_dev_wm9713, wm9713_dai, ARRAY_SIZE(wm9713_dai));
1273 }
1274
1275 static struct platform_driver wm9713_codec_driver = {
1276 .driver = {
1277 .name = "wm9713-codec",
1278 },
1279
1280 .probe = wm9713_probe,
1281 };
1282
1283 module_platform_driver(wm9713_codec_driver);
1284
1285 MODULE_DESCRIPTION("ASoC WM9713/WM9714 driver");
1286 MODULE_AUTHOR("Liam Girdwood");
1287 MODULE_LICENSE("GPL");
1288