blob: e842b08c3242056e1964a350c544f63f653d13db [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 {
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +010033 struct snd_ac97 *ac97;
Liam Girdwood83ac08c2008-02-15 16:43:11 +010034 u32 pll_in; /* PLL input frequency */
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +010035 unsigned int hp_mixer[2];
36 struct mutex lock;
Liam Girdwood83ac08c2008-02-15 16:43:11 +010037};
38
39static unsigned int ac97_read(struct snd_soc_codec *codec,
40 unsigned int reg);
41static int ac97_write(struct snd_soc_codec *codec,
42 unsigned int reg, unsigned int val);
43
44/*
45 * WM9713 register cache
46 * Reg 0x3c bit 15 is used by touch driver.
47 */
48static const u16 wm9713_reg[] = {
49 0x6174, 0x8080, 0x8080, 0x8080,
50 0xc880, 0xe808, 0xe808, 0x0808,
51 0x00da, 0x8000, 0xd600, 0xaaa0,
52 0xaaa0, 0xaaa0, 0x0000, 0x0000,
53 0x0f0f, 0x0040, 0x0000, 0x7f00,
54 0x0405, 0x0410, 0xbb80, 0xbb80,
55 0x0000, 0xbb80, 0x0000, 0x4523,
56 0x0000, 0x2000, 0x7eff, 0xffff,
57 0x0000, 0x0000, 0x0080, 0x0000,
58 0x0000, 0x0000, 0xfffe, 0xffff,
59 0x0000, 0x0000, 0x0000, 0xfffe,
60 0x4000, 0x0000, 0x0000, 0x0000,
61 0xb032, 0x3e00, 0x0000, 0x0000,
62 0x0000, 0x0000, 0x0000, 0x0000,
63 0x0000, 0x0000, 0x0000, 0x0006,
64 0x0001, 0x0000, 0x574d, 0x4c13,
Liam Girdwood83ac08c2008-02-15 16:43:11 +010065};
66
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +010067#define HPL_MIXER 0
68#define HPR_MIXER 1
Liam Girdwood83ac08c2008-02-15 16:43:11 +010069
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"};
Matt Reimera7f0b832014-05-19 09:35:47 -070077static const char *wm9713_mono_pga[] = {"Vmid", "Zh", "Mono", "Inv"};
Liam Girdwood83ac08c2008-02-15 16:43:11 +010078static const char *wm9713_spk_pga[] =
79 {"Vmid", "Zh", "Headphone", "Speaker", "Inv", "Headphone Vmid",
80 "Speaker Vmid", "Inv Vmid"};
81static const char *wm9713_hp_pga[] = {"Vmid", "Zh", "Headphone",
82 "Headphone Vmid"};
83static const char *wm9713_out3_pga[] = {"Vmid", "Zh", "Inv 1", "Inv 1 Vmid"};
84static const char *wm9713_out4_pga[] = {"Vmid", "Zh", "Inv 2", "Inv 2 Vmid"};
85static const char *wm9713_dac_inv[] =
86 {"Off", "Mono", "Speaker", "Left Headphone", "Right Headphone",
87 "Headphone Mono", "NC", "Vmid"};
88static const char *wm9713_bass[] = {"Linear Control", "Adaptive Boost"};
89static const char *wm9713_ng_type[] = {"Constant Gain", "Mute"};
90static const char *wm9713_mic_select[] = {"Mic 1", "Mic 2 A", "Mic 2 B"};
91static const char *wm9713_micb_select[] = {"MPB", "MPA"};
92
93static const struct soc_enum wm9713_enum[] = {
94SOC_ENUM_SINGLE(AC97_LINE, 3, 4, wm9713_mic_mixer), /* record mic mixer 0 */
95SOC_ENUM_SINGLE(AC97_VIDEO, 14, 4, wm9713_rec_mux), /* record mux hp 1 */
96SOC_ENUM_SINGLE(AC97_VIDEO, 9, 4, wm9713_rec_mux), /* record mux mono 2 */
97SOC_ENUM_SINGLE(AC97_VIDEO, 3, 8, wm9713_rec_src), /* record mux left 3 */
98SOC_ENUM_SINGLE(AC97_VIDEO, 0, 8, wm9713_rec_src), /* record mux right 4*/
99SOC_ENUM_DOUBLE(AC97_CD, 14, 6, 2, wm9713_rec_gain), /* record step size 5 */
100SOC_ENUM_SINGLE(AC97_PCI_SVID, 14, 4, wm9713_alc_select), /* alc source select 6*/
101SOC_ENUM_SINGLE(AC97_REC_GAIN, 14, 4, wm9713_mono_pga), /* mono input select 7 */
102SOC_ENUM_SINGLE(AC97_REC_GAIN, 11, 8, wm9713_spk_pga), /* speaker left input select 8 */
103SOC_ENUM_SINGLE(AC97_REC_GAIN, 8, 8, wm9713_spk_pga), /* speaker right input select 9 */
104SOC_ENUM_SINGLE(AC97_REC_GAIN, 6, 3, wm9713_hp_pga), /* headphone left input 10 */
105SOC_ENUM_SINGLE(AC97_REC_GAIN, 4, 3, wm9713_hp_pga), /* headphone right input 11 */
106SOC_ENUM_SINGLE(AC97_REC_GAIN, 2, 4, wm9713_out3_pga), /* out 3 source 12 */
107SOC_ENUM_SINGLE(AC97_REC_GAIN, 0, 4, wm9713_out4_pga), /* out 4 source 13 */
108SOC_ENUM_SINGLE(AC97_REC_GAIN_MIC, 13, 8, wm9713_dac_inv), /* dac invert 1 14 */
109SOC_ENUM_SINGLE(AC97_REC_GAIN_MIC, 10, 8, wm9713_dac_inv), /* dac invert 2 15 */
110SOC_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 15, 2, wm9713_bass), /* bass control 16 */
111SOC_ENUM_SINGLE(AC97_PCI_SVID, 5, 2, wm9713_ng_type), /* noise gate type 17 */
112SOC_ENUM_SINGLE(AC97_3D_CONTROL, 12, 3, wm9713_mic_select), /* mic selection 18 */
Lars-Peter Clausen5bc39b52014-10-30 21:01:10 +0100113SOC_ENUM_SINGLE_VIRT(2, wm9713_micb_select), /* mic selection 19 */
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100114};
115
Mark Browne03a8d22010-01-28 12:36:07 +0000116static const DECLARE_TLV_DB_SCALE(out_tlv, -4650, 150, 0);
117static const DECLARE_TLV_DB_SCALE(main_tlv, -3450, 150, 0);
118static const DECLARE_TLV_DB_SCALE(misc_tlv, -1500, 300, 0);
Lars-Peter Clausen5092e762015-08-02 17:20:09 +0200119static const DECLARE_TLV_DB_RANGE(mic_tlv,
Mark Browne03a8d22010-01-28 12:36:07 +0000120 0, 2, TLV_DB_SCALE_ITEM(1200, 600, 0),
Lars-Peter Clausen5092e762015-08-02 17:20:09 +0200121 3, 3, TLV_DB_SCALE_ITEM(3000, 0, 0)
122);
Mark Browne03a8d22010-01-28 12:36:07 +0000123
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100124static const struct snd_kcontrol_new wm9713_snd_ac97_controls[] = {
Mark Browne03a8d22010-01-28 12:36:07 +0000125SOC_DOUBLE_TLV("Speaker Playback Volume", AC97_MASTER, 8, 0, 31, 1, out_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100126SOC_DOUBLE("Speaker Playback Switch", AC97_MASTER, 15, 7, 1, 1),
Mark Browne03a8d22010-01-28 12:36:07 +0000127SOC_DOUBLE_TLV("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1,
128 out_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100129SOC_DOUBLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 7, 1, 1),
Mark Browne03a8d22010-01-28 12:36:07 +0000130SOC_DOUBLE_TLV("Line In Volume", AC97_PC_BEEP, 8, 0, 31, 1, main_tlv),
131SOC_DOUBLE_TLV("PCM Playback Volume", AC97_PHONE, 8, 0, 31, 1, main_tlv),
132SOC_SINGLE_TLV("Mic 1 Volume", AC97_MIC, 8, 31, 1, main_tlv),
133SOC_SINGLE_TLV("Mic 2 Volume", AC97_MIC, 0, 31, 1, main_tlv),
134SOC_SINGLE_TLV("Mic 1 Preamp Volume", AC97_3D_CONTROL, 10, 3, 0, mic_tlv),
135SOC_SINGLE_TLV("Mic 2 Preamp Volume", AC97_3D_CONTROL, 12, 3, 0, mic_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100136
137SOC_SINGLE("Mic Boost (+20dB) Switch", AC97_LINE, 5, 1, 0),
138SOC_SINGLE("Mic Headphone Mixer Volume", AC97_LINE, 0, 7, 1),
139
140SOC_SINGLE("Capture Switch", AC97_CD, 15, 1, 1),
141SOC_ENUM("Capture Volume Steps", wm9713_enum[5]),
142SOC_DOUBLE("Capture Volume", AC97_CD, 8, 0, 31, 0),
143SOC_SINGLE("Capture ZC Switch", AC97_CD, 7, 1, 0),
144
Mark Browne03a8d22010-01-28 12:36:07 +0000145SOC_SINGLE_TLV("Capture to Headphone Volume", AC97_VIDEO, 11, 7, 1, misc_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100146SOC_SINGLE("Capture to Mono Boost (+20dB) Switch", AC97_VIDEO, 8, 1, 0),
147SOC_SINGLE("Capture ADC Boost (+20dB) Switch", AC97_VIDEO, 6, 1, 0),
148
149SOC_SINGLE("ALC Target Volume", AC97_CODEC_CLASS_REV, 12, 15, 0),
150SOC_SINGLE("ALC Hold Time", AC97_CODEC_CLASS_REV, 8, 15, 0),
Mark Brown13d622b2008-10-30 12:37:10 +0000151SOC_SINGLE("ALC Decay Time", AC97_CODEC_CLASS_REV, 4, 15, 0),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100152SOC_SINGLE("ALC Attack Time", AC97_CODEC_CLASS_REV, 0, 15, 0),
153SOC_ENUM("ALC Function", wm9713_enum[6]),
154SOC_SINGLE("ALC Max Volume", AC97_PCI_SVID, 11, 7, 0),
155SOC_SINGLE("ALC ZC Timeout", AC97_PCI_SVID, 9, 3, 0),
156SOC_SINGLE("ALC ZC Switch", AC97_PCI_SVID, 8, 1, 0),
157SOC_SINGLE("ALC NG Switch", AC97_PCI_SVID, 7, 1, 0),
158SOC_ENUM("ALC NG Type", wm9713_enum[17]),
159SOC_SINGLE("ALC NG Threshold", AC97_PCI_SVID, 0, 31, 0),
160
161SOC_DOUBLE("Speaker Playback ZC Switch", AC97_MASTER, 14, 6, 1, 0),
162SOC_DOUBLE("Headphone Playback ZC Switch", AC97_HEADPHONE, 14, 6, 1, 0),
163
164SOC_SINGLE("Out4 Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
165SOC_SINGLE("Out4 Playback ZC Switch", AC97_MASTER_MONO, 14, 1, 0),
Mark Browne03a8d22010-01-28 12:36:07 +0000166SOC_SINGLE_TLV("Out4 Playback Volume", AC97_MASTER_MONO, 8, 31, 1, out_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100167
168SOC_SINGLE("Out3 Playback Switch", AC97_MASTER_MONO, 7, 1, 1),
169SOC_SINGLE("Out3 Playback ZC Switch", AC97_MASTER_MONO, 6, 1, 0),
Mark Browne03a8d22010-01-28 12:36:07 +0000170SOC_SINGLE_TLV("Out3 Playback Volume", AC97_MASTER_MONO, 0, 31, 1, out_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100171
Mark Browne03a8d22010-01-28 12:36:07 +0000172SOC_SINGLE_TLV("Mono Capture Volume", AC97_MASTER_TONE, 8, 31, 1, main_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100173SOC_SINGLE("Mono Playback Switch", AC97_MASTER_TONE, 7, 1, 1),
174SOC_SINGLE("Mono Playback ZC Switch", AC97_MASTER_TONE, 6, 1, 0),
Mark Browne03a8d22010-01-28 12:36:07 +0000175SOC_SINGLE_TLV("Mono Playback Volume", AC97_MASTER_TONE, 0, 31, 1, out_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100176
Mark Browne03a8d22010-01-28 12:36:07 +0000177SOC_SINGLE_TLV("Headphone Mixer Beep Playback Volume", AC97_AUX, 12, 7, 1,
178 misc_tlv),
179SOC_SINGLE_TLV("Speaker Mixer Beep Playback Volume", AC97_AUX, 8, 7, 1,
180 misc_tlv),
181SOC_SINGLE_TLV("Mono Mixer Beep Playback Volume", AC97_AUX, 4, 7, 1, misc_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100182
Mark Browne03a8d22010-01-28 12:36:07 +0000183SOC_SINGLE_TLV("Voice Playback Headphone Volume", AC97_PCM, 12, 7, 1,
184 misc_tlv),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100185SOC_SINGLE("Voice Playback Master Volume", AC97_PCM, 8, 7, 1),
186SOC_SINGLE("Voice Playback Mono Volume", AC97_PCM, 4, 7, 1),
187
Mark Browne03a8d22010-01-28 12:36:07 +0000188SOC_SINGLE_TLV("Headphone Mixer Aux Playback Volume", AC97_REC_SEL, 12, 7, 1,
189 misc_tlv),
190
191SOC_SINGLE_TLV("Speaker Mixer Voice Playback Volume", AC97_PCM, 8, 7, 1,
192 misc_tlv),
193SOC_SINGLE_TLV("Speaker Mixer Aux Playback Volume", AC97_REC_SEL, 8, 7, 1,
194 misc_tlv),
195
196SOC_SINGLE_TLV("Mono Mixer Voice Playback Volume", AC97_PCM, 4, 7, 1,
197 misc_tlv),
198SOC_SINGLE_TLV("Mono Mixer Aux Playback Volume", AC97_REC_SEL, 4, 7, 1,
199 misc_tlv),
200
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100201SOC_SINGLE("Aux Playback Headphone Volume", AC97_REC_SEL, 12, 7, 1),
202SOC_SINGLE("Aux Playback Master Volume", AC97_REC_SEL, 8, 7, 1),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100203
204SOC_ENUM("Bass Control", wm9713_enum[16]),
205SOC_SINGLE("Bass Cut-off Switch", AC97_GENERAL_PURPOSE, 12, 1, 1),
206SOC_SINGLE("Tone Cut-off Switch", AC97_GENERAL_PURPOSE, 4, 1, 1),
207SOC_SINGLE("Playback Attenuate (-6dB) Switch", AC97_GENERAL_PURPOSE, 6, 1, 0),
208SOC_SINGLE("Bass Volume", AC97_GENERAL_PURPOSE, 8, 15, 1),
209SOC_SINGLE("Tone Volume", AC97_GENERAL_PURPOSE, 0, 15, 1),
210
211SOC_SINGLE("3D Upper Cut-off Switch", AC97_REC_GAIN_MIC, 5, 1, 0),
212SOC_SINGLE("3D Lower Cut-off Switch", AC97_REC_GAIN_MIC, 4, 1, 0),
213SOC_SINGLE("3D Depth", AC97_REC_GAIN_MIC, 0, 15, 1),
214};
215
Mark Brown6bbcb452009-04-13 11:29:10 +0100216static int wm9713_voice_shutdown(struct snd_soc_dapm_widget *w,
217 struct snd_kcontrol *kcontrol, int event)
218{
Lars-Peter Clausenff705082015-01-13 10:27:33 +0100219 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
Mark Brown6bbcb452009-04-13 11:29:10 +0100220 u16 status, rate;
221
Takashi Iwaifb2e3e72013-11-05 18:39:59 +0100222 if (WARN_ON(event != SND_SOC_DAPM_PRE_PMD))
223 return -EINVAL;
Mark Brown6bbcb452009-04-13 11:29:10 +0100224
225 /* Gracefully shut down the voice interface. */
226 status = ac97_read(codec, AC97_EXTENDED_MID) | 0x1000;
227 rate = ac97_read(codec, AC97_HANDSET_RATE) & 0xF0FF;
228 ac97_write(codec, AC97_HANDSET_RATE, rate | 0x0200);
229 schedule_timeout_interruptible(msecs_to_jiffies(1));
230 ac97_write(codec, AC97_HANDSET_RATE, rate | 0x0F00);
231 ac97_write(codec, AC97_EXTENDED_MID, status);
232
233 return 0;
234}
235
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100236static const unsigned int wm9713_mixer_mute_regs[] = {
237 AC97_PC_BEEP,
238 AC97_MASTER_TONE,
239 AC97_PHONE,
240 AC97_REC_SEL,
241 AC97_PCM,
242 AC97_AUX,
243};
Mark Brown6bbcb452009-04-13 11:29:10 +0100244
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100245/* We have to create a fake left and right HP mixers because
246 * the codec only has a single control that is shared by both channels.
247 * This makes it impossible to determine the audio path using the current
248 * register map, thus we add a new (virtual) register to help determine the
249 * audio route within the device.
250 */
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100251static int wm9713_hp_mixer_put(struct snd_kcontrol *kcontrol,
252 struct snd_ctl_elem_value *ucontrol)
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100253{
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100254 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
255 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
256 struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
Takashi Iwai87a8b282015-03-10 12:39:16 +0100257 unsigned int val = ucontrol->value.integer.value[0];
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100258 struct soc_mixer_control *mc =
259 (struct soc_mixer_control *)kcontrol->private_value;
260 unsigned int mixer, mask, shift, old;
261 struct snd_soc_dapm_update update;
262 bool change;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100263
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100264 mixer = mc->shift >> 8;
265 shift = mc->shift & 0xff;
266 mask = (1 << shift);
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100267
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100268 mutex_lock(&wm9713->lock);
269 old = wm9713->hp_mixer[mixer];
Takashi Iwai87a8b282015-03-10 12:39:16 +0100270 if (ucontrol->value.integer.value[0])
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100271 wm9713->hp_mixer[mixer] |= mask;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100272 else
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100273 wm9713->hp_mixer[mixer] &= ~mask;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100274
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100275 change = old != wm9713->hp_mixer[mixer];
276 if (change) {
277 update.kcontrol = kcontrol;
278 update.reg = wm9713_mixer_mute_regs[shift];
279 update.mask = 0x8000;
280 if ((wm9713->hp_mixer[0] & mask) ||
281 (wm9713->hp_mixer[1] & mask))
282 update.val = 0x0;
283 else
284 update.val = 0x8000;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100285
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100286 snd_soc_dapm_mixer_update_power(dapm, kcontrol, val,
287 &update);
288 }
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100289
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100290 mutex_unlock(&wm9713->lock);
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100291
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100292 return change;
293}
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100294
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100295static int wm9713_hp_mixer_get(struct snd_kcontrol *kcontrol,
296 struct snd_ctl_elem_value *ucontrol)
297{
298 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
299 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
300 struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
301 struct soc_mixer_control *mc =
302 (struct soc_mixer_control *)kcontrol->private_value;
303 unsigned int mixer, shift;
304
305 mixer = mc->shift >> 8;
306 shift = mc->shift & 0xff;
307
Takashi Iwai87a8b282015-03-10 12:39:16 +0100308 ucontrol->value.integer.value[0] =
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100309 (wm9713->hp_mixer[mixer] >> shift) & 1;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100310
311 return 0;
312}
313
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100314#define WM9713_HP_MIXER_CTRL(xname, xmixer, xshift) { \
315 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
316 .info = snd_soc_info_volsw, \
317 .get = wm9713_hp_mixer_get, .put = wm9713_hp_mixer_put, \
318 .private_value = SOC_DOUBLE_VALUE(SND_SOC_NOPM, \
319 xshift, xmixer, 1, 0, 0) \
320}
321
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100322/* Left Headphone Mixers */
323static const struct snd_kcontrol_new wm9713_hpl_mixer_controls[] = {
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100324WM9713_HP_MIXER_CTRL("Beep Playback Switch", HPL_MIXER, 5),
325WM9713_HP_MIXER_CTRL("Voice Playback Switch", HPL_MIXER, 4),
326WM9713_HP_MIXER_CTRL("Aux Playback Switch", HPL_MIXER, 3),
327WM9713_HP_MIXER_CTRL("PCM Playback Switch", HPL_MIXER, 2),
328WM9713_HP_MIXER_CTRL("MonoIn Playback Switch", HPL_MIXER, 1),
329WM9713_HP_MIXER_CTRL("Bypass Playback Switch", HPL_MIXER, 0),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100330};
331
332/* Right Headphone Mixers */
333static const struct snd_kcontrol_new wm9713_hpr_mixer_controls[] = {
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100334WM9713_HP_MIXER_CTRL("Beep Playback Switch", HPR_MIXER, 5),
335WM9713_HP_MIXER_CTRL("Voice Playback Switch", HPR_MIXER, 4),
336WM9713_HP_MIXER_CTRL("Aux Playback Switch", HPR_MIXER, 3),
337WM9713_HP_MIXER_CTRL("PCM Playback Switch", HPR_MIXER, 2),
338WM9713_HP_MIXER_CTRL("MonoIn Playback Switch", HPR_MIXER, 1),
339WM9713_HP_MIXER_CTRL("Bypass Playback Switch", HPR_MIXER, 0),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100340};
341
342/* headphone capture mux */
343static const struct snd_kcontrol_new wm9713_hp_rec_mux_controls =
344SOC_DAPM_ENUM("Route", wm9713_enum[1]);
345
346/* headphone mic mux */
347static const struct snd_kcontrol_new wm9713_hp_mic_mux_controls =
348SOC_DAPM_ENUM("Route", wm9713_enum[0]);
349
350/* Speaker Mixer */
351static const struct snd_kcontrol_new wm9713_speaker_mixer_controls[] = {
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100352SOC_DAPM_SINGLE("Beep Playback Switch", AC97_AUX, 11, 1, 1),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100353SOC_DAPM_SINGLE("Voice Playback Switch", AC97_PCM, 11, 1, 1),
354SOC_DAPM_SINGLE("Aux Playback Switch", AC97_REC_SEL, 11, 1, 1),
355SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PHONE, 14, 1, 1),
356SOC_DAPM_SINGLE("MonoIn Playback Switch", AC97_MASTER_TONE, 14, 1, 1),
357SOC_DAPM_SINGLE("Bypass Playback Switch", AC97_PC_BEEP, 14, 1, 1),
358};
359
360/* Mono Mixer */
361static const struct snd_kcontrol_new wm9713_mono_mixer_controls[] = {
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100362SOC_DAPM_SINGLE("Beep Playback Switch", AC97_AUX, 7, 1, 1),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100363SOC_DAPM_SINGLE("Voice Playback Switch", AC97_PCM, 7, 1, 1),
364SOC_DAPM_SINGLE("Aux Playback Switch", AC97_REC_SEL, 7, 1, 1),
365SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PHONE, 13, 1, 1),
366SOC_DAPM_SINGLE("MonoIn Playback Switch", AC97_MASTER_TONE, 13, 1, 1),
367SOC_DAPM_SINGLE("Bypass Playback Switch", AC97_PC_BEEP, 13, 1, 1),
368SOC_DAPM_SINGLE("Mic 1 Sidetone Switch", AC97_LINE, 7, 1, 1),
369SOC_DAPM_SINGLE("Mic 2 Sidetone Switch", AC97_LINE, 6, 1, 1),
370};
371
372/* mono mic mux */
373static const struct snd_kcontrol_new wm9713_mono_mic_mux_controls =
374SOC_DAPM_ENUM("Route", wm9713_enum[2]);
375
376/* mono output mux */
377static const struct snd_kcontrol_new wm9713_mono_mux_controls =
378SOC_DAPM_ENUM("Route", wm9713_enum[7]);
379
380/* speaker left output mux */
381static const struct snd_kcontrol_new wm9713_hp_spkl_mux_controls =
382SOC_DAPM_ENUM("Route", wm9713_enum[8]);
383
384/* speaker right output mux */
385static const struct snd_kcontrol_new wm9713_hp_spkr_mux_controls =
386SOC_DAPM_ENUM("Route", wm9713_enum[9]);
387
388/* headphone left output mux */
389static const struct snd_kcontrol_new wm9713_hpl_out_mux_controls =
390SOC_DAPM_ENUM("Route", wm9713_enum[10]);
391
392/* headphone right output mux */
393static const struct snd_kcontrol_new wm9713_hpr_out_mux_controls =
394SOC_DAPM_ENUM("Route", wm9713_enum[11]);
395
396/* Out3 mux */
397static const struct snd_kcontrol_new wm9713_out3_mux_controls =
398SOC_DAPM_ENUM("Route", wm9713_enum[12]);
399
400/* Out4 mux */
401static const struct snd_kcontrol_new wm9713_out4_mux_controls =
402SOC_DAPM_ENUM("Route", wm9713_enum[13]);
403
404/* DAC inv mux 1 */
405static const struct snd_kcontrol_new wm9713_dac_inv1_mux_controls =
406SOC_DAPM_ENUM("Route", wm9713_enum[14]);
407
408/* DAC inv mux 2 */
409static const struct snd_kcontrol_new wm9713_dac_inv2_mux_controls =
410SOC_DAPM_ENUM("Route", wm9713_enum[15]);
411
412/* Capture source left */
413static const struct snd_kcontrol_new wm9713_rec_srcl_mux_controls =
414SOC_DAPM_ENUM("Route", wm9713_enum[3]);
415
416/* Capture source right */
417static const struct snd_kcontrol_new wm9713_rec_srcr_mux_controls =
418SOC_DAPM_ENUM("Route", wm9713_enum[4]);
419
420/* mic source */
421static const struct snd_kcontrol_new wm9713_mic_sel_mux_controls =
422SOC_DAPM_ENUM("Route", wm9713_enum[18]);
423
424/* mic source B virtual control */
425static const struct snd_kcontrol_new wm9713_micb_sel_mux_controls =
426SOC_DAPM_ENUM("Route", wm9713_enum[19]);
427
428static const struct snd_soc_dapm_widget wm9713_dapm_widgets[] = {
429SND_SOC_DAPM_MUX("Capture Headphone Mux", SND_SOC_NOPM, 0, 0,
430 &wm9713_hp_rec_mux_controls),
431SND_SOC_DAPM_MUX("Sidetone Mux", SND_SOC_NOPM, 0, 0,
432 &wm9713_hp_mic_mux_controls),
433SND_SOC_DAPM_MUX("Capture Mono Mux", SND_SOC_NOPM, 0, 0,
434 &wm9713_mono_mic_mux_controls),
435SND_SOC_DAPM_MUX("Mono Out Mux", SND_SOC_NOPM, 0, 0,
436 &wm9713_mono_mux_controls),
437SND_SOC_DAPM_MUX("Left Speaker Out Mux", SND_SOC_NOPM, 0, 0,
438 &wm9713_hp_spkl_mux_controls),
439SND_SOC_DAPM_MUX("Right Speaker Out Mux", SND_SOC_NOPM, 0, 0,
440 &wm9713_hp_spkr_mux_controls),
441SND_SOC_DAPM_MUX("Left Headphone Out Mux", SND_SOC_NOPM, 0, 0,
442 &wm9713_hpl_out_mux_controls),
443SND_SOC_DAPM_MUX("Right Headphone Out Mux", SND_SOC_NOPM, 0, 0,
444 &wm9713_hpr_out_mux_controls),
445SND_SOC_DAPM_MUX("Out 3 Mux", SND_SOC_NOPM, 0, 0,
446 &wm9713_out3_mux_controls),
447SND_SOC_DAPM_MUX("Out 4 Mux", SND_SOC_NOPM, 0, 0,
448 &wm9713_out4_mux_controls),
449SND_SOC_DAPM_MUX("DAC Inv Mux 1", SND_SOC_NOPM, 0, 0,
450 &wm9713_dac_inv1_mux_controls),
451SND_SOC_DAPM_MUX("DAC Inv Mux 2", SND_SOC_NOPM, 0, 0,
452 &wm9713_dac_inv2_mux_controls),
453SND_SOC_DAPM_MUX("Left Capture Source", SND_SOC_NOPM, 0, 0,
454 &wm9713_rec_srcl_mux_controls),
455SND_SOC_DAPM_MUX("Right Capture Source", SND_SOC_NOPM, 0, 0,
456 &wm9713_rec_srcr_mux_controls),
457SND_SOC_DAPM_MUX("Mic A Source", SND_SOC_NOPM, 0, 0,
458 &wm9713_mic_sel_mux_controls),
459SND_SOC_DAPM_MUX("Mic B Source", SND_SOC_NOPM, 0, 0,
460 &wm9713_micb_sel_mux_controls),
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +0100461SND_SOC_DAPM_MIXER("Left HP Mixer", AC97_EXTENDED_MID, 3, 1,
462 &wm9713_hpl_mixer_controls[0], ARRAY_SIZE(wm9713_hpl_mixer_controls)),
463SND_SOC_DAPM_MIXER("Right HP Mixer", AC97_EXTENDED_MID, 2, 1,
464 &wm9713_hpr_mixer_controls[0], ARRAY_SIZE(wm9713_hpr_mixer_controls)),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100465SND_SOC_DAPM_MIXER("Mono Mixer", AC97_EXTENDED_MID, 0, 1,
466 &wm9713_mono_mixer_controls[0], ARRAY_SIZE(wm9713_mono_mixer_controls)),
467SND_SOC_DAPM_MIXER("Speaker Mixer", AC97_EXTENDED_MID, 1, 1,
468 &wm9713_speaker_mixer_controls[0],
469 ARRAY_SIZE(wm9713_speaker_mixer_controls)),
470SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", AC97_EXTENDED_MID, 7, 1),
471SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", AC97_EXTENDED_MID, 6, 1),
472SND_SOC_DAPM_MIXER("AC97 Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
473SND_SOC_DAPM_MIXER("HP Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
474SND_SOC_DAPM_MIXER("Line Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
475SND_SOC_DAPM_MIXER("Capture Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
Mark Brown6bbcb452009-04-13 11:29:10 +0100476SND_SOC_DAPM_DAC_E("Voice DAC", "Voice Playback", AC97_EXTENDED_MID, 12, 1,
477 wm9713_voice_shutdown, SND_SOC_DAPM_PRE_PMD),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100478SND_SOC_DAPM_DAC("Aux DAC", "Aux Playback", AC97_EXTENDED_MID, 11, 1),
Mark Brownccbc3012008-09-15 15:51:13 +0100479SND_SOC_DAPM_PGA("Left ADC", AC97_EXTENDED_MID, 5, 1, NULL, 0),
480SND_SOC_DAPM_PGA("Right ADC", AC97_EXTENDED_MID, 4, 1, NULL, 0),
481SND_SOC_DAPM_ADC("Left HiFi ADC", "Left HiFi Capture", SND_SOC_NOPM, 0, 0),
482SND_SOC_DAPM_ADC("Right HiFi ADC", "Right HiFi Capture", SND_SOC_NOPM, 0, 0),
483SND_SOC_DAPM_ADC("Left Voice ADC", "Left Voice Capture", SND_SOC_NOPM, 0, 0),
484SND_SOC_DAPM_ADC("Right Voice ADC", "Right Voice Capture", SND_SOC_NOPM, 0, 0),
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100485SND_SOC_DAPM_PGA("Left Headphone", AC97_EXTENDED_MSTATUS, 10, 1, NULL, 0),
486SND_SOC_DAPM_PGA("Right Headphone", AC97_EXTENDED_MSTATUS, 9, 1, NULL, 0),
487SND_SOC_DAPM_PGA("Left Speaker", AC97_EXTENDED_MSTATUS, 8, 1, NULL, 0),
488SND_SOC_DAPM_PGA("Right Speaker", AC97_EXTENDED_MSTATUS, 7, 1, NULL, 0),
489SND_SOC_DAPM_PGA("Out 3", AC97_EXTENDED_MSTATUS, 11, 1, NULL, 0),
490SND_SOC_DAPM_PGA("Out 4", AC97_EXTENDED_MSTATUS, 12, 1, NULL, 0),
491SND_SOC_DAPM_PGA("Mono Out", AC97_EXTENDED_MSTATUS, 13, 1, NULL, 0),
492SND_SOC_DAPM_PGA("Left Line In", AC97_EXTENDED_MSTATUS, 6, 1, NULL, 0),
493SND_SOC_DAPM_PGA("Right Line In", AC97_EXTENDED_MSTATUS, 5, 1, NULL, 0),
494SND_SOC_DAPM_PGA("Mono In", AC97_EXTENDED_MSTATUS, 4, 1, NULL, 0),
495SND_SOC_DAPM_PGA("Mic A PGA", AC97_EXTENDED_MSTATUS, 3, 1, NULL, 0),
496SND_SOC_DAPM_PGA("Mic B PGA", AC97_EXTENDED_MSTATUS, 2, 1, NULL, 0),
497SND_SOC_DAPM_PGA("Mic A Pre Amp", AC97_EXTENDED_MSTATUS, 1, 1, NULL, 0),
498SND_SOC_DAPM_PGA("Mic B Pre Amp", AC97_EXTENDED_MSTATUS, 0, 1, NULL, 0),
499SND_SOC_DAPM_MICBIAS("Mic Bias", AC97_EXTENDED_MSTATUS, 14, 1),
500SND_SOC_DAPM_OUTPUT("MONO"),
501SND_SOC_DAPM_OUTPUT("HPL"),
502SND_SOC_DAPM_OUTPUT("HPR"),
503SND_SOC_DAPM_OUTPUT("SPKL"),
504SND_SOC_DAPM_OUTPUT("SPKR"),
505SND_SOC_DAPM_OUTPUT("OUT3"),
506SND_SOC_DAPM_OUTPUT("OUT4"),
507SND_SOC_DAPM_INPUT("LINEL"),
508SND_SOC_DAPM_INPUT("LINER"),
509SND_SOC_DAPM_INPUT("MONOIN"),
510SND_SOC_DAPM_INPUT("PCBEEP"),
511SND_SOC_DAPM_INPUT("MIC1"),
512SND_SOC_DAPM_INPUT("MIC2A"),
513SND_SOC_DAPM_INPUT("MIC2B"),
514SND_SOC_DAPM_VMID("VMID"),
515};
516
Lu Guanqun7ad7dd12011-03-30 21:53:11 +0800517static const struct snd_soc_dapm_route wm9713_audio_map[] = {
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100518 /* left HP mixer */
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100519 {"Left HP Mixer", "Beep Playback Switch", "PCBEEP"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100520 {"Left HP Mixer", "Voice Playback Switch", "Voice DAC"},
521 {"Left HP Mixer", "Aux Playback Switch", "Aux DAC"},
522 {"Left HP Mixer", "Bypass Playback Switch", "Left Line In"},
523 {"Left HP Mixer", "PCM Playback Switch", "Left DAC"},
524 {"Left HP Mixer", "MonoIn Playback Switch", "Mono In"},
525 {"Left HP Mixer", NULL, "Capture Headphone Mux"},
526
527 /* right HP mixer */
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100528 {"Right HP Mixer", "Beep Playback Switch", "PCBEEP"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100529 {"Right HP Mixer", "Voice Playback Switch", "Voice DAC"},
530 {"Right HP Mixer", "Aux Playback Switch", "Aux DAC"},
531 {"Right HP Mixer", "Bypass Playback Switch", "Right Line In"},
532 {"Right HP Mixer", "PCM Playback Switch", "Right DAC"},
533 {"Right HP Mixer", "MonoIn Playback Switch", "Mono In"},
534 {"Right HP Mixer", NULL, "Capture Headphone Mux"},
535
536 /* virtual mixer - mixes left & right channels for spk and mono */
537 {"AC97 Mixer", NULL, "Left DAC"},
538 {"AC97 Mixer", NULL, "Right DAC"},
539 {"Line Mixer", NULL, "Right Line In"},
540 {"Line Mixer", NULL, "Left Line In"},
541 {"HP Mixer", NULL, "Left HP Mixer"},
542 {"HP Mixer", NULL, "Right HP Mixer"},
543 {"Capture Mixer", NULL, "Left Capture Source"},
544 {"Capture Mixer", NULL, "Right Capture Source"},
545
546 /* speaker mixer */
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100547 {"Speaker Mixer", "Beep Playback Switch", "PCBEEP"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100548 {"Speaker Mixer", "Voice Playback Switch", "Voice DAC"},
549 {"Speaker Mixer", "Aux Playback Switch", "Aux DAC"},
550 {"Speaker Mixer", "Bypass Playback Switch", "Line Mixer"},
551 {"Speaker Mixer", "PCM Playback Switch", "AC97 Mixer"},
552 {"Speaker Mixer", "MonoIn Playback Switch", "Mono In"},
553
554 /* mono mixer */
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100555 {"Mono Mixer", "Beep Playback Switch", "PCBEEP"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100556 {"Mono Mixer", "Voice Playback Switch", "Voice DAC"},
557 {"Mono Mixer", "Aux Playback Switch", "Aux DAC"},
558 {"Mono Mixer", "Bypass Playback Switch", "Line Mixer"},
559 {"Mono Mixer", "PCM Playback Switch", "AC97 Mixer"},
Robert Jarzmikc0bbf482008-03-18 12:08:35 +0100560 {"Mono Mixer", "Mic 1 Sidetone Switch", "Mic A PGA"},
561 {"Mono Mixer", "Mic 2 Sidetone Switch", "Mic B PGA"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100562 {"Mono Mixer", NULL, "Capture Mono Mux"},
563
564 /* DAC inv mux 1 */
565 {"DAC Inv Mux 1", "Mono", "Mono Mixer"},
566 {"DAC Inv Mux 1", "Speaker", "Speaker Mixer"},
567 {"DAC Inv Mux 1", "Left Headphone", "Left HP Mixer"},
568 {"DAC Inv Mux 1", "Right Headphone", "Right HP Mixer"},
569 {"DAC Inv Mux 1", "Headphone Mono", "HP Mixer"},
570
571 /* DAC inv mux 2 */
572 {"DAC Inv Mux 2", "Mono", "Mono Mixer"},
573 {"DAC Inv Mux 2", "Speaker", "Speaker Mixer"},
574 {"DAC Inv Mux 2", "Left Headphone", "Left HP Mixer"},
575 {"DAC Inv Mux 2", "Right Headphone", "Right HP Mixer"},
576 {"DAC Inv Mux 2", "Headphone Mono", "HP Mixer"},
577
578 /* headphone left mux */
579 {"Left Headphone Out Mux", "Headphone", "Left HP Mixer"},
580
581 /* headphone right mux */
582 {"Right Headphone Out Mux", "Headphone", "Right HP Mixer"},
583
584 /* speaker left mux */
585 {"Left Speaker Out Mux", "Headphone", "Left HP Mixer"},
586 {"Left Speaker Out Mux", "Speaker", "Speaker Mixer"},
587 {"Left Speaker Out Mux", "Inv", "DAC Inv Mux 1"},
588
589 /* speaker right mux */
590 {"Right Speaker Out Mux", "Headphone", "Right HP Mixer"},
591 {"Right Speaker Out Mux", "Speaker", "Speaker Mixer"},
592 {"Right Speaker Out Mux", "Inv", "DAC Inv Mux 2"},
593
594 /* mono mux */
595 {"Mono Out Mux", "Mono", "Mono Mixer"},
596 {"Mono Out Mux", "Inv", "DAC Inv Mux 1"},
597
598 /* out 3 mux */
599 {"Out 3 Mux", "Inv 1", "DAC Inv Mux 1"},
600
601 /* out 4 mux */
602 {"Out 4 Mux", "Inv 2", "DAC Inv Mux 2"},
603
604 /* output pga */
605 {"HPL", NULL, "Left Headphone"},
606 {"Left Headphone", NULL, "Left Headphone Out Mux"},
607 {"HPR", NULL, "Right Headphone"},
608 {"Right Headphone", NULL, "Right Headphone Out Mux"},
609 {"OUT3", NULL, "Out 3"},
610 {"Out 3", NULL, "Out 3 Mux"},
611 {"OUT4", NULL, "Out 4"},
612 {"Out 4", NULL, "Out 4 Mux"},
613 {"SPKL", NULL, "Left Speaker"},
614 {"Left Speaker", NULL, "Left Speaker Out Mux"},
615 {"SPKR", NULL, "Right Speaker"},
616 {"Right Speaker", NULL, "Right Speaker Out Mux"},
617 {"MONO", NULL, "Mono Out"},
618 {"Mono Out", NULL, "Mono Out Mux"},
619
620 /* input pga */
621 {"Left Line In", NULL, "LINEL"},
622 {"Right Line In", NULL, "LINER"},
623 {"Mono In", NULL, "MONOIN"},
624 {"Mic A PGA", NULL, "Mic A Pre Amp"},
625 {"Mic B PGA", NULL, "Mic B Pre Amp"},
626
627 /* left capture select */
628 {"Left Capture Source", "Mic 1", "Mic A Pre Amp"},
629 {"Left Capture Source", "Mic 2", "Mic B Pre Amp"},
630 {"Left Capture Source", "Line", "LINEL"},
631 {"Left Capture Source", "Mono In", "MONOIN"},
632 {"Left Capture Source", "Headphone", "Left HP Mixer"},
633 {"Left Capture Source", "Speaker", "Speaker Mixer"},
634 {"Left Capture Source", "Mono Out", "Mono Mixer"},
635
636 /* right capture select */
637 {"Right Capture Source", "Mic 1", "Mic A Pre Amp"},
638 {"Right Capture Source", "Mic 2", "Mic B Pre Amp"},
639 {"Right Capture Source", "Line", "LINER"},
640 {"Right Capture Source", "Mono In", "MONOIN"},
641 {"Right Capture Source", "Headphone", "Right HP Mixer"},
642 {"Right Capture Source", "Speaker", "Speaker Mixer"},
643 {"Right Capture Source", "Mono Out", "Mono Mixer"},
644
645 /* left ADC */
646 {"Left ADC", NULL, "Left Capture Source"},
Mark Brownccbc3012008-09-15 15:51:13 +0100647 {"Left Voice ADC", NULL, "Left ADC"},
648 {"Left HiFi ADC", NULL, "Left ADC"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100649
650 /* right ADC */
651 {"Right ADC", NULL, "Right Capture Source"},
Mark Brownccbc3012008-09-15 15:51:13 +0100652 {"Right Voice ADC", NULL, "Right ADC"},
653 {"Right HiFi ADC", NULL, "Right ADC"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100654
655 /* mic */
656 {"Mic A Pre Amp", NULL, "Mic A Source"},
657 {"Mic A Source", "Mic 1", "MIC1"},
658 {"Mic A Source", "Mic 2 A", "MIC2A"},
659 {"Mic A Source", "Mic 2 B", "Mic B Source"},
660 {"Mic B Pre Amp", "MPB", "Mic B Source"},
661 {"Mic B Source", NULL, "MIC2B"},
662
663 /* headphone capture */
664 {"Capture Headphone Mux", "Stereo", "Capture Mixer"},
665 {"Capture Headphone Mux", "Left", "Left Capture Source"},
666 {"Capture Headphone Mux", "Right", "Right Capture Source"},
667
668 /* mono capture */
669 {"Capture Mono Mux", "Stereo", "Capture Mixer"},
670 {"Capture Mono Mux", "Left", "Left Capture Source"},
671 {"Capture Mono Mux", "Right", "Right Capture Source"},
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100672};
673
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100674static unsigned int ac97_read(struct snd_soc_codec *codec,
675 unsigned int reg)
676{
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100677 struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100678 u16 *cache = codec->reg_cache;
679
680 if (reg == AC97_RESET || reg == AC97_GPIO_STATUS ||
681 reg == AC97_VENDOR_ID1 || reg == AC97_VENDOR_ID2 ||
682 reg == AC97_CD)
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100683 return soc_ac97_ops->read(wm9713->ac97, reg);
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100684 else {
685 reg = reg >> 1;
686
Ian Molton91432e92009-01-17 17:44:23 +0000687 if (reg >= (ARRAY_SIZE(wm9713_reg)))
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100688 return -EIO;
689
690 return cache[reg];
691 }
692}
693
694static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
695 unsigned int val)
696{
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100697 struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
698
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100699 u16 *cache = codec->reg_cache;
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100700 soc_ac97_ops->write(wm9713->ac97, reg, val);
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100701 reg = reg >> 1;
Ian Molton91432e92009-01-17 17:44:23 +0000702 if (reg < (ARRAY_SIZE(wm9713_reg)))
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100703 cache[reg] = val;
704
705 return 0;
706}
707
708/* PLL divisors */
709struct _pll_div {
710 u32 divsel:1;
711 u32 divctl:1;
712 u32 lf:1;
713 u32 n:4;
714 u32 k:24;
715};
716
717/* The size in bits of the PLL divide multiplied by 10
718 * to allow rounding later */
719#define FIXED_PLL_SIZE ((1 << 22) * 10)
720
Lars-Peter Clausena6c2b072014-10-30 21:01:07 +0100721static void pll_factors(struct snd_soc_codec *codec,
722 struct _pll_div *pll_div, unsigned int source)
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100723{
724 u64 Kpart;
725 unsigned int K, Ndiv, Nmod, target;
726
727 /* The the PLL output is always 98.304MHz. */
728 target = 98304000;
729
730 /* If the input frequency is over 14.4MHz then scale it down. */
731 if (source > 14400000) {
732 source >>= 1;
733 pll_div->divsel = 1;
734
735 if (source > 14400000) {
736 source >>= 1;
737 pll_div->divctl = 1;
738 } else
739 pll_div->divctl = 0;
740
741 } else {
742 pll_div->divsel = 0;
743 pll_div->divctl = 0;
744 }
745
746 /* Low frequency sources require an additional divide in the
747 * loop.
748 */
749 if (source < 8192000) {
750 pll_div->lf = 1;
751 target >>= 2;
752 } else
753 pll_div->lf = 0;
754
755 Ndiv = target / source;
756 if ((Ndiv < 5) || (Ndiv > 12))
Lars-Peter Clausena6c2b072014-10-30 21:01:07 +0100757 dev_warn(codec->dev,
Roel Kluin449bd542009-05-27 17:08:39 -0700758 "WM9713 PLL N value %u out of recommended range!\n",
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100759 Ndiv);
760
761 pll_div->n = Ndiv;
762 Nmod = target % source;
763 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
764
765 do_div(Kpart, source);
766
767 K = Kpart & 0xFFFFFFFF;
768
769 /* Check if we need to round */
770 if ((K % 10) >= 5)
771 K += 5;
772
773 /* Move down to proper range now rounding is done */
774 K /= 10;
775
776 pll_div->k = K;
777}
778
779/**
780 * Please note that changing the PLL input frequency may require
781 * resynchronisation with the AC97 controller.
782 */
783static int wm9713_set_pll(struct snd_soc_codec *codec,
784 int pll_id, unsigned int freq_in, unsigned int freq_out)
785{
Mark Brownb2c812e2010-04-14 15:35:19 +0900786 struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100787 u16 reg, reg2;
788 struct _pll_div pll_div;
789
790 /* turn PLL off ? */
Mark Brownc42f69b2009-01-16 16:31:03 +0000791 if (freq_in == 0) {
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100792 /* disable PLL power and select ext source */
793 reg = ac97_read(codec, AC97_HANDSET_RATE);
794 ac97_write(codec, AC97_HANDSET_RATE, reg | 0x0080);
795 reg = ac97_read(codec, AC97_EXTENDED_MID);
796 ac97_write(codec, AC97_EXTENDED_MID, reg | 0x0200);
Mark Brownc42f69b2009-01-16 16:31:03 +0000797 wm9713->pll_in = 0;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100798 return 0;
799 }
800
Lars-Peter Clausena6c2b072014-10-30 21:01:07 +0100801 pll_factors(codec, &pll_div, freq_in);
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100802
803 if (pll_div.k == 0) {
804 reg = (pll_div.n << 12) | (pll_div.lf << 11) |
805 (pll_div.divsel << 9) | (pll_div.divctl << 8);
806 ac97_write(codec, AC97_LINE1_LEVEL, reg);
807 } else {
808 /* write the fractional k to the reg 0x46 pages */
809 reg2 = (pll_div.n << 12) | (pll_div.lf << 11) | (1 << 10) |
810 (pll_div.divsel << 9) | (pll_div.divctl << 8);
811
812 /* K [21:20] */
813 reg = reg2 | (0x5 << 4) | (pll_div.k >> 20);
814 ac97_write(codec, AC97_LINE1_LEVEL, reg);
815
816 /* K [19:16] */
817 reg = reg2 | (0x4 << 4) | ((pll_div.k >> 16) & 0xf);
818 ac97_write(codec, AC97_LINE1_LEVEL, reg);
819
820 /* K [15:12] */
821 reg = reg2 | (0x3 << 4) | ((pll_div.k >> 12) & 0xf);
822 ac97_write(codec, AC97_LINE1_LEVEL, reg);
823
824 /* K [11:8] */
825 reg = reg2 | (0x2 << 4) | ((pll_div.k >> 8) & 0xf);
826 ac97_write(codec, AC97_LINE1_LEVEL, reg);
827
828 /* K [7:4] */
829 reg = reg2 | (0x1 << 4) | ((pll_div.k >> 4) & 0xf);
830 ac97_write(codec, AC97_LINE1_LEVEL, reg);
831
832 reg = reg2 | (0x0 << 4) | (pll_div.k & 0xf); /* K [3:0] */
833 ac97_write(codec, AC97_LINE1_LEVEL, reg);
834 }
835
836 /* turn PLL on and select as source */
837 reg = ac97_read(codec, AC97_EXTENDED_MID);
838 ac97_write(codec, AC97_EXTENDED_MID, reg & 0xfdff);
839 reg = ac97_read(codec, AC97_HANDSET_RATE);
840 ac97_write(codec, AC97_HANDSET_RATE, reg & 0xff7f);
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100841 wm9713->pll_in = freq_in;
842
843 /* wait 10ms AC97 link frames for the link to stabilise */
844 schedule_timeout_interruptible(msecs_to_jiffies(10));
845 return 0;
846}
847
Mark Brown85488032009-09-05 18:52:16 +0100848static int wm9713_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
849 int source, unsigned int freq_in, unsigned int freq_out)
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100850{
851 struct snd_soc_codec *codec = codec_dai->codec;
852 return wm9713_set_pll(codec, pll_id, freq_in, freq_out);
853}
854
855/*
856 * Tristate the PCM DAI lines, tristate can be disabled by calling
857 * wm9713_set_dai_fmt()
858 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100859static int wm9713_set_dai_tristate(struct snd_soc_dai *codec_dai,
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100860 int tristate)
861{
862 struct snd_soc_codec *codec = codec_dai->codec;
863 u16 reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0x9fff;
864
865 if (tristate)
866 ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
867
868 return 0;
869}
870
871/*
872 * Configure WM9713 clock dividers.
873 * Voice DAC needs 256 FS
874 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100875static int wm9713_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100876 int div_id, int div)
877{
878 struct snd_soc_codec *codec = codec_dai->codec;
879 u16 reg;
880
881 switch (div_id) {
882 case WM9713_PCMCLK_DIV:
883 reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xf0ff;
884 ac97_write(codec, AC97_HANDSET_RATE, reg | div);
885 break;
886 case WM9713_CLKA_MULT:
887 reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xfffd;
888 ac97_write(codec, AC97_HANDSET_RATE, reg | div);
889 break;
890 case WM9713_CLKB_MULT:
891 reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xfffb;
892 ac97_write(codec, AC97_HANDSET_RATE, reg | div);
893 break;
894 case WM9713_HIFI_DIV:
895 reg = ac97_read(codec, AC97_HANDSET_RATE) & 0x8fff;
896 ac97_write(codec, AC97_HANDSET_RATE, reg | div);
897 break;
898 case WM9713_PCMBCLK_DIV:
899 reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0xf1ff;
900 ac97_write(codec, AC97_CENTER_LFE_MASTER, reg | div);
901 break;
902 case WM9713_PCMCLK_PLL_DIV:
903 reg = ac97_read(codec, AC97_LINE1_LEVEL) & 0xff80;
904 ac97_write(codec, AC97_LINE1_LEVEL, reg | 0x60 | div);
905 break;
906 case WM9713_HIFI_PLL_DIV:
907 reg = ac97_read(codec, AC97_LINE1_LEVEL) & 0xff80;
908 ac97_write(codec, AC97_LINE1_LEVEL, reg | 0x70 | div);
909 break;
910 default:
911 return -EINVAL;
912 }
913
914 return 0;
915}
916
Liam Girdwoode550e172008-07-07 16:07:52 +0100917static int wm9713_set_dai_fmt(struct snd_soc_dai *codec_dai,
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100918 unsigned int fmt)
919{
920 struct snd_soc_codec *codec = codec_dai->codec;
921 u16 gpio = ac97_read(codec, AC97_GPIO_CFG) & 0xffc5;
922 u16 reg = 0x8000;
923
924 /* clock masters */
925 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
926 case SND_SOC_DAIFMT_CBM_CFM:
927 reg |= 0x4000;
928 gpio |= 0x0010;
929 break;
930 case SND_SOC_DAIFMT_CBM_CFS:
931 reg |= 0x6000;
932 gpio |= 0x0018;
933 break;
934 case SND_SOC_DAIFMT_CBS_CFS:
Mark Brownd133b0c2008-06-11 13:47:11 +0100935 reg |= 0x2000;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100936 gpio |= 0x001a;
937 break;
938 case SND_SOC_DAIFMT_CBS_CFM:
939 gpio |= 0x0012;
940 break;
941 }
942
943 /* clock inversion */
944 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
945 case SND_SOC_DAIFMT_IB_IF:
946 reg |= 0x00c0;
947 break;
948 case SND_SOC_DAIFMT_IB_NF:
949 reg |= 0x0080;
950 break;
951 case SND_SOC_DAIFMT_NB_IF:
952 reg |= 0x0040;
953 break;
954 }
955
956 /* DAI format */
957 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
958 case SND_SOC_DAIFMT_I2S:
959 reg |= 0x0002;
960 break;
961 case SND_SOC_DAIFMT_RIGHT_J:
962 break;
963 case SND_SOC_DAIFMT_LEFT_J:
964 reg |= 0x0001;
965 break;
966 case SND_SOC_DAIFMT_DSP_A:
967 reg |= 0x0003;
968 break;
969 case SND_SOC_DAIFMT_DSP_B:
970 reg |= 0x0043;
971 break;
972 }
973
974 ac97_write(codec, AC97_GPIO_CFG, gpio);
975 ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
976 return 0;
977}
978
979static int wm9713_pcm_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000980 struct snd_pcm_hw_params *params,
981 struct snd_soc_dai *dai)
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100982{
Mark Brown55b8bac2008-11-24 14:05:29 +0000983 struct snd_soc_codec *codec = dai->codec;
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100984 u16 reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0xfff3;
985
Mark Brown563fe712014-07-31 12:55:23 +0100986 switch (params_width(params)) {
987 case 16:
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100988 break;
Mark Brown563fe712014-07-31 12:55:23 +0100989 case 20:
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100990 reg |= 0x0004;
991 break;
Mark Brown563fe712014-07-31 12:55:23 +0100992 case 24:
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100993 reg |= 0x0008;
994 break;
Mark Brown563fe712014-07-31 12:55:23 +0100995 case 32:
Liam Girdwood83ac08c2008-02-15 16:43:11 +0100996 reg |= 0x000c;
997 break;
998 }
999
1000 /* enable PCM interface in master mode */
1001 ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
1002 return 0;
1003}
1004
Mark Browndee89c42008-11-18 22:11:38 +00001005static int ac97_hifi_prepare(struct snd_pcm_substream *substream,
1006 struct snd_soc_dai *dai)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001007{
Mark Brown55b8bac2008-11-24 14:05:29 +00001008 struct snd_soc_codec *codec = dai->codec;
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001009 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001010 int reg;
1011 u16 vra;
1012
1013 vra = ac97_read(codec, AC97_EXTENDED_STATUS);
1014 ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
1015
1016 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1017 reg = AC97_PCM_FRONT_DAC_RATE;
1018 else
1019 reg = AC97_PCM_LR_ADC_RATE;
1020
1021 return ac97_write(codec, reg, runtime->rate);
1022}
1023
Mark Browndee89c42008-11-18 22:11:38 +00001024static int ac97_aux_prepare(struct snd_pcm_substream *substream,
1025 struct snd_soc_dai *dai)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001026{
Mark Brown55b8bac2008-11-24 14:05:29 +00001027 struct snd_soc_codec *codec = dai->codec;
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001028 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001029 u16 vra, xsle;
1030
1031 vra = ac97_read(codec, AC97_EXTENDED_STATUS);
1032 ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
1033 xsle = ac97_read(codec, AC97_PCI_SID);
1034 ac97_write(codec, AC97_PCI_SID, xsle | 0x8000);
1035
1036 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
1037 return -ENODEV;
1038
1039 return ac97_write(codec, AC97_PCM_SURR_DAC_RATE, runtime->rate);
1040}
1041
Mark Brown54155522008-07-02 11:51:19 +01001042#define WM9713_RATES (SNDRV_PCM_RATE_8000 | \
1043 SNDRV_PCM_RATE_11025 | \
1044 SNDRV_PCM_RATE_22050 | \
1045 SNDRV_PCM_RATE_44100 | \
1046 SNDRV_PCM_RATE_48000)
1047
1048#define WM9713_PCM_RATES (SNDRV_PCM_RATE_8000 | \
1049 SNDRV_PCM_RATE_11025 | \
1050 SNDRV_PCM_RATE_16000 | \
1051 SNDRV_PCM_RATE_22050 | \
1052 SNDRV_PCM_RATE_44100 | \
1053 SNDRV_PCM_RATE_48000)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001054
1055#define WM9713_PCM_FORMATS \
Maciej S. Szmigieroe712bfc2015-05-23 18:32:29 +02001056 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1057 SNDRV_PCM_FMTBIT_S24_LE)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001058
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001059static const struct snd_soc_dai_ops wm9713_dai_ops_hifi = {
Eric Miao6335d052009-03-03 09:41:00 +08001060 .prepare = ac97_hifi_prepare,
1061 .set_clkdiv = wm9713_set_dai_clkdiv,
1062 .set_pll = wm9713_set_dai_pll,
1063};
1064
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001065static const struct snd_soc_dai_ops wm9713_dai_ops_aux = {
Eric Miao6335d052009-03-03 09:41:00 +08001066 .prepare = ac97_aux_prepare,
1067 .set_clkdiv = wm9713_set_dai_clkdiv,
1068 .set_pll = wm9713_set_dai_pll,
1069};
1070
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001071static const struct snd_soc_dai_ops wm9713_dai_ops_voice = {
Eric Miao6335d052009-03-03 09:41:00 +08001072 .hw_params = wm9713_pcm_hw_params,
Eric Miao6335d052009-03-03 09:41:00 +08001073 .set_clkdiv = wm9713_set_dai_clkdiv,
1074 .set_pll = wm9713_set_dai_pll,
1075 .set_fmt = wm9713_set_dai_fmt,
1076 .set_tristate = wm9713_set_dai_tristate,
1077};
1078
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001079static struct snd_soc_dai_driver wm9713_dai[] = {
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001080{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001081 .name = "wm9713-hifi",
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001082 .playback = {
1083 .stream_name = "HiFi Playback",
1084 .channels_min = 1,
1085 .channels_max = 2,
1086 .rates = WM9713_RATES,
Mark Brown33f503c2009-05-02 12:24:55 +01001087 .formats = SND_SOC_STD_AC97_FMTS,},
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001088 .capture = {
1089 .stream_name = "HiFi Capture",
1090 .channels_min = 1,
1091 .channels_max = 2,
1092 .rates = WM9713_RATES,
Mark Brown33f503c2009-05-02 12:24:55 +01001093 .formats = SND_SOC_STD_AC97_FMTS,},
Eric Miao6335d052009-03-03 09:41:00 +08001094 .ops = &wm9713_dai_ops_hifi,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001095 },
1096 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001097 .name = "wm9713-aux",
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001098 .playback = {
1099 .stream_name = "Aux Playback",
1100 .channels_min = 1,
1101 .channels_max = 1,
1102 .rates = WM9713_RATES,
Mark Brown33f503c2009-05-02 12:24:55 +01001103 .formats = SND_SOC_STD_AC97_FMTS,},
Eric Miao6335d052009-03-03 09:41:00 +08001104 .ops = &wm9713_dai_ops_aux,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001105 },
1106 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001107 .name = "wm9713-voice",
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001108 .playback = {
1109 .stream_name = "Voice Playback",
1110 .channels_min = 1,
1111 .channels_max = 1,
Mark Brown54155522008-07-02 11:51:19 +01001112 .rates = WM9713_PCM_RATES,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001113 .formats = WM9713_PCM_FORMATS,},
1114 .capture = {
1115 .stream_name = "Voice Capture",
1116 .channels_min = 1,
1117 .channels_max = 2,
Mark Brown54155522008-07-02 11:51:19 +01001118 .rates = WM9713_PCM_RATES,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001119 .formats = WM9713_PCM_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +08001120 .ops = &wm9713_dai_ops_voice,
Mark Brownf4976112009-04-13 10:53:02 +01001121 .symmetric_rates = 1,
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001122 },
1123};
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001124
1125int wm9713_reset(struct snd_soc_codec *codec, int try_warm)
1126{
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +01001127 struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
1128
Mark Brownb047e1c2013-06-26 12:45:59 +01001129 if (try_warm && soc_ac97_ops->warm_reset) {
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +01001130 soc_ac97_ops->warm_reset(wm9713->ac97);
Mark Brownabb68c22008-06-13 16:24:04 +01001131 if (ac97_read(codec, 0) == wm9713_reg[0])
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001132 return 1;
1133 }
1134
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +01001135 soc_ac97_ops->reset(wm9713->ac97);
Mark Brownb047e1c2013-06-26 12:45:59 +01001136 if (soc_ac97_ops->warm_reset)
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +01001137 soc_ac97_ops->warm_reset(wm9713->ac97);
Lars-Peter Clausena6c2b072014-10-30 21:01:07 +01001138 if (ac97_read(codec, 0) != wm9713_reg[0]) {
1139 dev_err(codec->dev, "Failed to reset: AC97 link error\n");
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001140 return -EIO;
Lars-Peter Clausena6c2b072014-10-30 21:01:07 +01001141 }
1142
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001143 return 0;
1144}
1145EXPORT_SYMBOL_GPL(wm9713_reset);
1146
Mark Brown0be98982008-05-19 12:31:28 +02001147static int wm9713_set_bias_level(struct snd_soc_codec *codec,
1148 enum snd_soc_bias_level level)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001149{
1150 u16 reg;
1151
Mark Brown0be98982008-05-19 12:31:28 +02001152 switch (level) {
1153 case SND_SOC_BIAS_ON:
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001154 /* enable thermal shutdown */
1155 reg = ac97_read(codec, AC97_EXTENDED_MID) & 0x1bff;
1156 ac97_write(codec, AC97_EXTENDED_MID, reg);
1157 break;
Mark Brown0be98982008-05-19 12:31:28 +02001158 case SND_SOC_BIAS_PREPARE:
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001159 break;
Mark Brown0be98982008-05-19 12:31:28 +02001160 case SND_SOC_BIAS_STANDBY:
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001161 /* enable master bias and vmid */
1162 reg = ac97_read(codec, AC97_EXTENDED_MID) & 0x3bff;
1163 ac97_write(codec, AC97_EXTENDED_MID, reg);
1164 ac97_write(codec, AC97_POWERDOWN, 0x0000);
1165 break;
Mark Brown0be98982008-05-19 12:31:28 +02001166 case SND_SOC_BIAS_OFF:
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001167 /* disable everything including AC link */
1168 ac97_write(codec, AC97_EXTENDED_MID, 0xffff);
1169 ac97_write(codec, AC97_EXTENDED_MSTATUS, 0xffff);
1170 ac97_write(codec, AC97_POWERDOWN, 0xffff);
1171 break;
1172 }
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001173 return 0;
1174}
1175
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +01001176static int wm9713_soc_suspend(struct snd_soc_codec *codec)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001177{
Mark Brown87b57fe2008-04-14 15:27:30 +02001178 u16 reg;
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001179
Mark Brown87b57fe2008-04-14 15:27:30 +02001180 /* Disable everything except touchpanel - that will be handled
1181 * by the touch driver and left disabled if touch is not in
1182 * use. */
1183 reg = ac97_read(codec, AC97_EXTENDED_MID);
1184 ac97_write(codec, AC97_EXTENDED_MID, reg | 0x7fff);
1185 ac97_write(codec, AC97_EXTENDED_MSTATUS, 0xffff);
1186 ac97_write(codec, AC97_POWERDOWN, 0x6f00);
1187 ac97_write(codec, AC97_POWERDOWN, 0xffff);
1188
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001189 return 0;
1190}
1191
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001192static int wm9713_soc_resume(struct snd_soc_codec *codec)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001193{
Mark Brownb2c812e2010-04-14 15:35:19 +09001194 struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001195 int i, ret;
1196 u16 *cache = codec->reg_cache;
1197
1198 ret = wm9713_reset(codec, 1);
Lars-Peter Clausena6c2b072014-10-30 21:01:07 +01001199 if (ret < 0)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001200 return ret;
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001201
Lars-Peter Clausenbd1204c2015-04-27 22:13:24 +02001202 snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_STANDBY);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001203
1204 /* do we need to re-start the PLL ? */
Mark Brownc42f69b2009-01-16 16:31:03 +00001205 if (wm9713->pll_in)
1206 wm9713_set_pll(codec, 0, wm9713->pll_in, 0);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001207
1208 /* only synchronise the codec if warm reset failed */
1209 if (ret == 0) {
1210 for (i = 2; i < ARRAY_SIZE(wm9713_reg) << 1; i += 2) {
1211 if (i == AC97_POWERDOWN || i == AC97_EXTENDED_MID ||
1212 i == AC97_EXTENDED_MSTATUS || i > 0x66)
1213 continue;
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +01001214 soc_ac97_ops->write(wm9713->ac97, i, cache[i>>1]);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001215 }
1216 }
1217
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001218 return ret;
1219}
1220
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001221static int wm9713_soc_probe(struct snd_soc_codec *codec)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001222{
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +01001223 struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001224 int ret = 0, reg;
1225
Lars-Peter Clausenf8d71be2015-01-23 16:21:37 +01001226 wm9713->ac97 = snd_soc_alloc_ac97_codec(codec);
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +01001227 if (IS_ERR(wm9713->ac97))
1228 return PTR_ERR(wm9713->ac97);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001229
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001230 /* do a cold reset for the controller and then try
1231 * a warm reset followed by an optional cold reset for codec */
1232 wm9713_reset(codec, 0);
1233 ret = wm9713_reset(codec, 1);
Lars-Peter Clausena6c2b072014-10-30 21:01:07 +01001234 if (ret < 0)
Lars-Peter Clausenf8d71be2015-01-23 16:21:37 +01001235 goto err_put_device;
1236
1237 ret = device_add(&wm9713->ac97->dev);
1238 if (ret)
1239 goto err_put_device;
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001240
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001241 /* unmute the adc - move to kcontrol */
1242 reg = ac97_read(codec, AC97_CD) & 0x7fff;
1243 ac97_write(codec, AC97_CD, reg);
1244
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001245 return 0;
1246
Lars-Peter Clausenf8d71be2015-01-23 16:21:37 +01001247err_put_device:
1248 put_device(&wm9713->ac97->dev);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001249 return ret;
1250}
1251
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001252static int wm9713_soc_remove(struct snd_soc_codec *codec)
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001253{
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +01001254 struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
1255
1256 snd_soc_free_ac97_codec(wm9713->ac97);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001257 return 0;
1258}
1259
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001260static struct snd_soc_codec_driver soc_codec_dev_wm9713 = {
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001261 .probe = wm9713_soc_probe,
1262 .remove = wm9713_soc_remove,
1263 .suspend = wm9713_soc_suspend,
1264 .resume = wm9713_soc_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001265 .read = ac97_read,
1266 .write = ac97_write,
1267 .set_bias_level = wm9713_set_bias_level,
Dimitris Papastamose5eec342010-09-10 18:14:56 +01001268 .reg_cache_size = ARRAY_SIZE(wm9713_reg),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001269 .reg_word_size = sizeof(u16),
1270 .reg_cache_step = 2,
1271 .reg_cache_default = wm9713_reg,
Lars-Peter Clausenc1359ca2014-10-30 21:01:08 +01001272
1273 .controls = wm9713_snd_ac97_controls,
1274 .num_controls = ARRAY_SIZE(wm9713_snd_ac97_controls),
Lu Guanqun7ad7dd12011-03-30 21:53:11 +08001275 .dapm_widgets = wm9713_dapm_widgets,
1276 .num_dapm_widgets = ARRAY_SIZE(wm9713_dapm_widgets),
1277 .dapm_routes = wm9713_audio_map,
1278 .num_dapm_routes = ARRAY_SIZE(wm9713_audio_map),
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001279};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001280
Bill Pemberton7a79e942012-12-07 09:26:37 -05001281static int wm9713_probe(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001282{
Lars-Peter Clausen5efe89d2014-10-30 21:01:09 +01001283 struct wm9713_priv *wm9713;
1284
1285 wm9713 = devm_kzalloc(&pdev->dev, sizeof(*wm9713), GFP_KERNEL);
1286 if (wm9713 == NULL)
1287 return -ENOMEM;
1288
Lars-Peter Clausencf1f2eb2014-11-03 19:33:03 +01001289 mutex_init(&wm9713->lock);
1290
Lars-Peter Clausen5efe89d2014-10-30 21:01:09 +01001291 platform_set_drvdata(pdev, wm9713);
1292
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001293 return snd_soc_register_codec(&pdev->dev,
1294 &soc_codec_dev_wm9713, wm9713_dai, ARRAY_SIZE(wm9713_dai));
1295}
1296
Bill Pemberton7a79e942012-12-07 09:26:37 -05001297static int wm9713_remove(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001298{
1299 snd_soc_unregister_codec(&pdev->dev);
1300 return 0;
1301}
1302
1303static struct platform_driver wm9713_codec_driver = {
1304 .driver = {
1305 .name = "wm9713-codec",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001306 },
1307
1308 .probe = wm9713_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -05001309 .remove = wm9713_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001310};
1311
Mark Brown5bbcc3c2011-11-23 22:52:08 +00001312module_platform_driver(wm9713_codec_driver);
Liam Girdwood83ac08c2008-02-15 16:43:11 +01001313
1314MODULE_DESCRIPTION("ASoC WM9713/WM9714 driver");
1315MODULE_AUTHOR("Liam Girdwood");
1316MODULE_LICENSE("GPL");