xref: /openbmc/linux/sound/pci/hda/patch_via.c (revision 10a20af7c944649dc6d1ffa06bc759f5f3a16cd9)
1c577b8a1SJoseph Chan /*
2c577b8a1SJoseph Chan  * Universal Interface for Intel High Definition Audio Codec
3c577b8a1SJoseph Chan  *
48e86597fSLydia Wang  * HD audio interface patch for VIA VT17xx/VT18xx/VT20xx codec
5c577b8a1SJoseph Chan  *
68e86597fSLydia Wang  *  (C) 2006-2009 VIA Technology, Inc.
78e86597fSLydia Wang  *  (C) 2006-2008 Takashi Iwai <tiwai@suse.de>
8c577b8a1SJoseph Chan  *
9c577b8a1SJoseph Chan  *  This driver is free software; you can redistribute it and/or modify
10c577b8a1SJoseph Chan  *  it under the terms of the GNU General Public License as published by
11c577b8a1SJoseph Chan  *  the Free Software Foundation; either version 2 of the License, or
12c577b8a1SJoseph Chan  *  (at your option) any later version.
13c577b8a1SJoseph Chan  *
14c577b8a1SJoseph Chan  *  This driver is distributed in the hope that it will be useful,
15c577b8a1SJoseph Chan  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16c577b8a1SJoseph Chan  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17c577b8a1SJoseph Chan  *  GNU General Public License for more details.
18c577b8a1SJoseph Chan  *
19c577b8a1SJoseph Chan  *  You should have received a copy of the GNU General Public License
20c577b8a1SJoseph Chan  *  along with this program; if not, write to the Free Software
21c577b8a1SJoseph Chan  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22c577b8a1SJoseph Chan  */
23c577b8a1SJoseph Chan 
24c577b8a1SJoseph Chan /* * * * * * * * * * * * * * Release History * * * * * * * * * * * * * * * * */
25c577b8a1SJoseph Chan /*									     */
26c577b8a1SJoseph Chan /* 2006-03-03  Lydia Wang  Create the basic patch to support VT1708 codec    */
27c577b8a1SJoseph Chan /* 2006-03-14  Lydia Wang  Modify hard code for some pin widget nid	     */
28c577b8a1SJoseph Chan /* 2006-08-02  Lydia Wang  Add support to VT1709 codec			     */
29c577b8a1SJoseph Chan /* 2006-09-08  Lydia Wang  Fix internal loopback recording source select bug */
30f7278fd0SJosepch Chan /* 2007-09-12  Lydia Wang  Add EAPD enable during driver initialization	     */
31f7278fd0SJosepch Chan /* 2007-09-17  Lydia Wang  Add VT1708B codec support			    */
3276d9b0ddSHarald Welte /* 2007-11-14  Lydia Wang  Add VT1708A codec HP and CD pin connect config    */
33fb4cb772SHarald Welte /* 2008-02-03  Lydia Wang  Fix Rear channels and Back channels inverse issue */
34d949cac1SHarald Welte /* 2008-03-06  Lydia Wang  Add VT1702 codec and VT1708S codec support	     */
3569e52a80SHarald Welte /* 2008-04-09  Lydia Wang  Add mute front speaker when HP plugin	     */
360aa62aefSHarald Welte /* 2008-04-09  Lydia Wang  Add Independent HP feature			     */
3798aa34c0SHarald Welte /* 2008-05-28  Lydia Wang  Add second S/PDIF Out support for VT1702	     */
38d7426329SHarald Welte /* 2008-09-15  Logan Li	   Add VT1708S Mic Boost workaround/backdoor	     */
398e86597fSLydia Wang /* 2009-02-16  Logan Li	   Add support for VT1718S			     */
408e86597fSLydia Wang /* 2009-03-13  Logan Li	   Add support for VT1716S			     */
418e86597fSLydia Wang /* 2009-04-14  Lydai Wang  Add support for VT1828S and VT2020		     */
428e86597fSLydia Wang /* 2009-07-08  Lydia Wang  Add support for VT2002P			     */
438e86597fSLydia Wang /* 2009-07-21  Lydia Wang  Add support for VT1812			     */
4436dd5c4aSLydia Wang /* 2009-09-19  Lydia Wang  Add support for VT1818S			     */
45c577b8a1SJoseph Chan /*									     */
46c577b8a1SJoseph Chan /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
47c577b8a1SJoseph Chan 
48c577b8a1SJoseph Chan 
49c577b8a1SJoseph Chan #include <linux/init.h>
50c577b8a1SJoseph Chan #include <linux/delay.h>
51c577b8a1SJoseph Chan #include <linux/slab.h>
52c577b8a1SJoseph Chan #include <sound/core.h>
530aa62aefSHarald Welte #include <sound/asoundef.h>
54c577b8a1SJoseph Chan #include "hda_codec.h"
55c577b8a1SJoseph Chan #include "hda_local.h"
56c577b8a1SJoseph Chan 
575b0cb1d8SJaroslav Kysela #define NID_MAPPING		(-1)
585b0cb1d8SJaroslav Kysela 
59c577b8a1SJoseph Chan /* amp values */
60c577b8a1SJoseph Chan #define AMP_VAL_IDX_SHIFT	19
61c577b8a1SJoseph Chan #define AMP_VAL_IDX_MASK	(0x0f<<19)
62c577b8a1SJoseph Chan 
63c577b8a1SJoseph Chan /* Pin Widget NID */
64c577b8a1SJoseph Chan #define VT1708_HP_NID		0x13
65c577b8a1SJoseph Chan #define VT1708_DIGOUT_NID	0x14
66c577b8a1SJoseph Chan #define VT1708_DIGIN_NID	0x16
67f7278fd0SJosepch Chan #define VT1708_DIGIN_PIN	0x26
6876d9b0ddSHarald Welte #define VT1708_HP_PIN_NID	0x20
6976d9b0ddSHarald Welte #define VT1708_CD_PIN_NID	0x24
70c577b8a1SJoseph Chan 
71c577b8a1SJoseph Chan #define VT1709_HP_DAC_NID	0x28
72c577b8a1SJoseph Chan #define VT1709_DIGOUT_NID	0x13
73c577b8a1SJoseph Chan #define VT1709_DIGIN_NID	0x17
74f7278fd0SJosepch Chan #define VT1709_DIGIN_PIN	0x25
75f7278fd0SJosepch Chan 
76f7278fd0SJosepch Chan #define VT1708B_HP_NID		0x25
77f7278fd0SJosepch Chan #define VT1708B_DIGOUT_NID	0x12
78f7278fd0SJosepch Chan #define VT1708B_DIGIN_NID	0x15
79f7278fd0SJosepch Chan #define VT1708B_DIGIN_PIN	0x21
80c577b8a1SJoseph Chan 
81d949cac1SHarald Welte #define VT1708S_HP_NID		0x25
82d949cac1SHarald Welte #define VT1708S_DIGOUT_NID	0x12
83d949cac1SHarald Welte 
84d949cac1SHarald Welte #define VT1702_HP_NID		0x17
85d949cac1SHarald Welte #define VT1702_DIGOUT_NID	0x11
86d949cac1SHarald Welte 
87d7426329SHarald Welte enum VIA_HDA_CODEC {
88d7426329SHarald Welte 	UNKNOWN = -1,
89d7426329SHarald Welte 	VT1708,
90d7426329SHarald Welte 	VT1709_10CH,
91d7426329SHarald Welte 	VT1709_6CH,
92d7426329SHarald Welte 	VT1708B_8CH,
93d7426329SHarald Welte 	VT1708B_4CH,
94d7426329SHarald Welte 	VT1708S,
95518bf3baSLydia Wang 	VT1708BCE,
96d7426329SHarald Welte 	VT1702,
97eb7188caSLydia Wang 	VT1718S,
98f3db423dSLydia Wang 	VT1716S,
9925eaba2fSLydia Wang 	VT2002P,
100ab6734e7SLydia Wang 	VT1812,
101d7426329SHarald Welte 	CODEC_TYPES,
102d7426329SHarald Welte };
103d7426329SHarald Welte 
1041f2e99feSLydia Wang struct via_spec {
1051f2e99feSLydia Wang 	/* codec parameterization */
106f3db423dSLydia Wang 	struct snd_kcontrol_new *mixers[6];
1071f2e99feSLydia Wang 	unsigned int num_mixers;
1081f2e99feSLydia Wang 
1091f2e99feSLydia Wang 	struct hda_verb *init_verbs[5];
1101f2e99feSLydia Wang 	unsigned int num_iverbs;
1111f2e99feSLydia Wang 
1121f2e99feSLydia Wang 	char *stream_name_analog;
1131f2e99feSLydia Wang 	struct hda_pcm_stream *stream_analog_playback;
1141f2e99feSLydia Wang 	struct hda_pcm_stream *stream_analog_capture;
1151f2e99feSLydia Wang 
1161f2e99feSLydia Wang 	char *stream_name_digital;
1171f2e99feSLydia Wang 	struct hda_pcm_stream *stream_digital_playback;
1181f2e99feSLydia Wang 	struct hda_pcm_stream *stream_digital_capture;
1191f2e99feSLydia Wang 
1201f2e99feSLydia Wang 	/* playback */
1211f2e99feSLydia Wang 	struct hda_multi_out multiout;
1221f2e99feSLydia Wang 	hda_nid_t slave_dig_outs[2];
1231f2e99feSLydia Wang 
1241f2e99feSLydia Wang 	/* capture */
1251f2e99feSLydia Wang 	unsigned int num_adc_nids;
1261f2e99feSLydia Wang 	hda_nid_t *adc_nids;
1271f2e99feSLydia Wang 	hda_nid_t mux_nids[3];
1281f2e99feSLydia Wang 	hda_nid_t dig_in_nid;
1291f2e99feSLydia Wang 	hda_nid_t dig_in_pin;
1301f2e99feSLydia Wang 
1311f2e99feSLydia Wang 	/* capture source */
1321f2e99feSLydia Wang 	const struct hda_input_mux *input_mux;
1331f2e99feSLydia Wang 	unsigned int cur_mux[3];
1341f2e99feSLydia Wang 
1351f2e99feSLydia Wang 	/* PCM information */
1361f2e99feSLydia Wang 	struct hda_pcm pcm_rec[3];
1371f2e99feSLydia Wang 
1381f2e99feSLydia Wang 	/* dynamic controls, init_verbs and input_mux */
1391f2e99feSLydia Wang 	struct auto_pin_cfg autocfg;
1401f2e99feSLydia Wang 	struct snd_array kctls;
1411f2e99feSLydia Wang 	struct hda_input_mux private_imux[2];
1421f2e99feSLydia Wang 	hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
1431f2e99feSLydia Wang 
1441f2e99feSLydia Wang 	/* HP mode source */
1451f2e99feSLydia Wang 	const struct hda_input_mux *hp_mux;
1461f2e99feSLydia Wang 	unsigned int hp_independent_mode;
1471f2e99feSLydia Wang 	unsigned int hp_independent_mode_index;
1481f2e99feSLydia Wang 	unsigned int smart51_enabled;
149f3db423dSLydia Wang 	unsigned int dmic_enabled;
1501f2e99feSLydia Wang 	enum VIA_HDA_CODEC codec_type;
1511f2e99feSLydia Wang 
1521f2e99feSLydia Wang 	/* work to check hp jack state */
1531f2e99feSLydia Wang 	struct hda_codec *codec;
1541f2e99feSLydia Wang 	struct delayed_work vt1708_hp_work;
1551f2e99feSLydia Wang 	int vt1708_jack_detectect;
1561f2e99feSLydia Wang 	int vt1708_hp_present;
1571f2e99feSLydia Wang #ifdef CONFIG_SND_HDA_POWER_SAVE
1581f2e99feSLydia Wang 	struct hda_loopback_check loopback;
1591f2e99feSLydia Wang #endif
1601f2e99feSLydia Wang };
1611f2e99feSLydia Wang 
1625b0cb1d8SJaroslav Kysela static struct via_spec * via_new_spec(struct hda_codec *codec)
1635b0cb1d8SJaroslav Kysela {
1645b0cb1d8SJaroslav Kysela 	struct via_spec *spec;
1655b0cb1d8SJaroslav Kysela 
1665b0cb1d8SJaroslav Kysela 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1675b0cb1d8SJaroslav Kysela 	if (spec == NULL)
1685b0cb1d8SJaroslav Kysela 		return NULL;
1695b0cb1d8SJaroslav Kysela 
1705b0cb1d8SJaroslav Kysela 	codec->spec = spec;
1715b0cb1d8SJaroslav Kysela 	spec->codec = codec;
1725b0cb1d8SJaroslav Kysela 	return spec;
1735b0cb1d8SJaroslav Kysela }
1745b0cb1d8SJaroslav Kysela 
175744ff5f4SLydia Wang static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
176d7426329SHarald Welte {
177744ff5f4SLydia Wang 	u32 vendor_id = codec->vendor_id;
178d7426329SHarald Welte 	u16 ven_id = vendor_id >> 16;
179d7426329SHarald Welte 	u16 dev_id = vendor_id & 0xffff;
180d7426329SHarald Welte 	enum VIA_HDA_CODEC codec_type;
181d7426329SHarald Welte 
182d7426329SHarald Welte 	/* get codec type */
183d7426329SHarald Welte 	if (ven_id != 0x1106)
184d7426329SHarald Welte 		codec_type = UNKNOWN;
185d7426329SHarald Welte 	else if (dev_id >= 0x1708 && dev_id <= 0x170b)
186d7426329SHarald Welte 		codec_type = VT1708;
187d7426329SHarald Welte 	else if (dev_id >= 0xe710 && dev_id <= 0xe713)
188d7426329SHarald Welte 		codec_type = VT1709_10CH;
189d7426329SHarald Welte 	else if (dev_id >= 0xe714 && dev_id <= 0xe717)
190d7426329SHarald Welte 		codec_type = VT1709_6CH;
191518bf3baSLydia Wang 	else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
192d7426329SHarald Welte 		codec_type = VT1708B_8CH;
193518bf3baSLydia Wang 		if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
194518bf3baSLydia Wang 			codec_type = VT1708BCE;
195518bf3baSLydia Wang 	} else if (dev_id >= 0xe724 && dev_id <= 0xe727)
196d7426329SHarald Welte 		codec_type = VT1708B_4CH;
197d7426329SHarald Welte 	else if ((dev_id & 0xfff) == 0x397
198d7426329SHarald Welte 		 && (dev_id >> 12) < 8)
199d7426329SHarald Welte 		codec_type = VT1708S;
200d7426329SHarald Welte 	else if ((dev_id & 0xfff) == 0x398
201d7426329SHarald Welte 		 && (dev_id >> 12) < 8)
202d7426329SHarald Welte 		codec_type = VT1702;
203eb7188caSLydia Wang 	else if ((dev_id & 0xfff) == 0x428
204eb7188caSLydia Wang 		 && (dev_id >> 12) < 8)
205eb7188caSLydia Wang 		codec_type = VT1718S;
206f3db423dSLydia Wang 	else if (dev_id == 0x0433 || dev_id == 0xa721)
207f3db423dSLydia Wang 		codec_type = VT1716S;
208bb3c6bfcSLydia Wang 	else if (dev_id == 0x0441 || dev_id == 0x4441)
209bb3c6bfcSLydia Wang 		codec_type = VT1718S;
21025eaba2fSLydia Wang 	else if (dev_id == 0x0438 || dev_id == 0x4438)
21125eaba2fSLydia Wang 		codec_type = VT2002P;
212ab6734e7SLydia Wang 	else if (dev_id == 0x0448)
213ab6734e7SLydia Wang 		codec_type = VT1812;
21436dd5c4aSLydia Wang 	else if (dev_id == 0x0440)
21536dd5c4aSLydia Wang 		codec_type = VT1708S;
216d7426329SHarald Welte 	else
217d7426329SHarald Welte 		codec_type = UNKNOWN;
218d7426329SHarald Welte 	return codec_type;
219d7426329SHarald Welte };
220d7426329SHarald Welte 
22169e52a80SHarald Welte #define VIA_HP_EVENT		0x01
22269e52a80SHarald Welte #define VIA_GPIO_EVENT		0x02
223a34df19aSLydia Wang #define VIA_JACK_EVENT		0x04
224f3db423dSLydia Wang #define VIA_MONO_EVENT		0x08
22525eaba2fSLydia Wang #define VIA_SPEAKER_EVENT	0x10
22625eaba2fSLydia Wang #define VIA_BIND_HP_EVENT	0x20
22769e52a80SHarald Welte 
228c577b8a1SJoseph Chan enum {
229c577b8a1SJoseph Chan 	VIA_CTL_WIDGET_VOL,
230c577b8a1SJoseph Chan 	VIA_CTL_WIDGET_MUTE,
231f5271101SLydia Wang 	VIA_CTL_WIDGET_ANALOG_MUTE,
23225eaba2fSLydia Wang 	VIA_CTL_WIDGET_BIND_PIN_MUTE,
233c577b8a1SJoseph Chan };
234c577b8a1SJoseph Chan 
235c577b8a1SJoseph Chan enum {
236eb14a46cSHarald Welte 	AUTO_SEQ_FRONT = 0,
237c577b8a1SJoseph Chan 	AUTO_SEQ_SURROUND,
238c577b8a1SJoseph Chan 	AUTO_SEQ_CENLFE,
239c577b8a1SJoseph Chan 	AUTO_SEQ_SIDE
240c577b8a1SJoseph Chan };
241c577b8a1SJoseph Chan 
242f5271101SLydia Wang static void analog_low_current_mode(struct hda_codec *codec, int stream_idle);
243f5271101SLydia Wang static void set_jack_power_state(struct hda_codec *codec);
2441f2e99feSLydia Wang static int is_aa_path_mute(struct hda_codec *codec);
2451f2e99feSLydia Wang 
2461f2e99feSLydia Wang static void vt1708_start_hp_work(struct via_spec *spec)
2471f2e99feSLydia Wang {
2481f2e99feSLydia Wang 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
2491f2e99feSLydia Wang 		return;
2501f2e99feSLydia Wang 	snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
2511f2e99feSLydia Wang 			    !spec->vt1708_jack_detectect);
2521f2e99feSLydia Wang 	if (!delayed_work_pending(&spec->vt1708_hp_work))
2531f2e99feSLydia Wang 		schedule_delayed_work(&spec->vt1708_hp_work,
2541f2e99feSLydia Wang 				      msecs_to_jiffies(100));
2551f2e99feSLydia Wang }
2561f2e99feSLydia Wang 
2571f2e99feSLydia Wang static void vt1708_stop_hp_work(struct via_spec *spec)
2581f2e99feSLydia Wang {
2591f2e99feSLydia Wang 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
2601f2e99feSLydia Wang 		return;
2611f2e99feSLydia Wang 	if (snd_hda_get_bool_hint(spec->codec, "analog_loopback_hp_detect") == 1
2621f2e99feSLydia Wang 	    && !is_aa_path_mute(spec->codec))
2631f2e99feSLydia Wang 		return;
2641f2e99feSLydia Wang 	snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
2651f2e99feSLydia Wang 			    !spec->vt1708_jack_detectect);
2661f2e99feSLydia Wang 	cancel_delayed_work(&spec->vt1708_hp_work);
2671f2e99feSLydia Wang 	flush_scheduled_work();
2681f2e99feSLydia Wang }
269f5271101SLydia Wang 
27025eaba2fSLydia Wang 
271f5271101SLydia Wang static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
272f5271101SLydia Wang 				   struct snd_ctl_elem_value *ucontrol)
273f5271101SLydia Wang {
274f5271101SLydia Wang 	int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
275f5271101SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
276f5271101SLydia Wang 
277f5271101SLydia Wang 	set_jack_power_state(codec);
278f5271101SLydia Wang 	analog_low_current_mode(snd_kcontrol_chip(kcontrol), -1);
2791f2e99feSLydia Wang 	if (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1) {
2801f2e99feSLydia Wang 		if (is_aa_path_mute(codec))
2811f2e99feSLydia Wang 			vt1708_start_hp_work(codec->spec);
2821f2e99feSLydia Wang 		else
2831f2e99feSLydia Wang 			vt1708_stop_hp_work(codec->spec);
2841f2e99feSLydia Wang 	}
285f5271101SLydia Wang 	return change;
286f5271101SLydia Wang }
287f5271101SLydia Wang 
288f5271101SLydia Wang /* modify .put = snd_hda_mixer_amp_switch_put */
289f5271101SLydia Wang #define ANALOG_INPUT_MUTE						\
290f5271101SLydia Wang 	{		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,		\
291f5271101SLydia Wang 			.name = NULL,					\
292f5271101SLydia Wang 			.index = 0,					\
293f5271101SLydia Wang 			.info = snd_hda_mixer_amp_switch_info,		\
294f5271101SLydia Wang 			.get = snd_hda_mixer_amp_switch_get,		\
295f5271101SLydia Wang 			.put = analog_input_switch_put,			\
296f5271101SLydia Wang 			.private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
297f5271101SLydia Wang 
29825eaba2fSLydia Wang static void via_hp_bind_automute(struct hda_codec *codec);
29925eaba2fSLydia Wang 
30025eaba2fSLydia Wang static int bind_pin_switch_put(struct snd_kcontrol *kcontrol,
30125eaba2fSLydia Wang 			       struct snd_ctl_elem_value *ucontrol)
30225eaba2fSLydia Wang {
30325eaba2fSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
30425eaba2fSLydia Wang 	struct via_spec *spec = codec->spec;
30525eaba2fSLydia Wang 	int i;
30625eaba2fSLydia Wang 	int change = 0;
30725eaba2fSLydia Wang 
30825eaba2fSLydia Wang 	long *valp = ucontrol->value.integer.value;
30925eaba2fSLydia Wang 	int lmute, rmute;
31025eaba2fSLydia Wang 	if (strstr(kcontrol->id.name, "Switch") == NULL) {
31125eaba2fSLydia Wang 		snd_printd("Invalid control!\n");
31225eaba2fSLydia Wang 		return change;
31325eaba2fSLydia Wang 	}
31425eaba2fSLydia Wang 	change = snd_hda_mixer_amp_switch_put(kcontrol,
31525eaba2fSLydia Wang 					      ucontrol);
31625eaba2fSLydia Wang 	/* Get mute value */
31725eaba2fSLydia Wang 	lmute = *valp ? 0 : HDA_AMP_MUTE;
31825eaba2fSLydia Wang 	valp++;
31925eaba2fSLydia Wang 	rmute = *valp ? 0 : HDA_AMP_MUTE;
32025eaba2fSLydia Wang 
32125eaba2fSLydia Wang 	/* Set hp pins */
32225eaba2fSLydia Wang 	if (!spec->hp_independent_mode) {
32325eaba2fSLydia Wang 		for (i = 0; i < spec->autocfg.hp_outs; i++) {
32425eaba2fSLydia Wang 			snd_hda_codec_amp_update(
32525eaba2fSLydia Wang 				codec, spec->autocfg.hp_pins[i],
32625eaba2fSLydia Wang 				0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
32725eaba2fSLydia Wang 				lmute);
32825eaba2fSLydia Wang 			snd_hda_codec_amp_update(
32925eaba2fSLydia Wang 				codec, spec->autocfg.hp_pins[i],
33025eaba2fSLydia Wang 				1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
33125eaba2fSLydia Wang 				rmute);
33225eaba2fSLydia Wang 		}
33325eaba2fSLydia Wang 	}
33425eaba2fSLydia Wang 
33525eaba2fSLydia Wang 	if (!lmute && !rmute) {
33625eaba2fSLydia Wang 		/* Line Outs */
33725eaba2fSLydia Wang 		for (i = 0; i < spec->autocfg.line_outs; i++)
33825eaba2fSLydia Wang 			snd_hda_codec_amp_stereo(
33925eaba2fSLydia Wang 				codec, spec->autocfg.line_out_pins[i],
34025eaba2fSLydia Wang 				HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
34125eaba2fSLydia Wang 		/* Speakers */
34225eaba2fSLydia Wang 		for (i = 0; i < spec->autocfg.speaker_outs; i++)
34325eaba2fSLydia Wang 			snd_hda_codec_amp_stereo(
34425eaba2fSLydia Wang 				codec, spec->autocfg.speaker_pins[i],
34525eaba2fSLydia Wang 				HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
34625eaba2fSLydia Wang 		/* unmute */
34725eaba2fSLydia Wang 		via_hp_bind_automute(codec);
34825eaba2fSLydia Wang 
34925eaba2fSLydia Wang 	} else {
35025eaba2fSLydia Wang 		if (lmute) {
35125eaba2fSLydia Wang 			/* Mute all left channels */
35225eaba2fSLydia Wang 			for (i = 1; i < spec->autocfg.line_outs; i++)
35325eaba2fSLydia Wang 				snd_hda_codec_amp_update(
35425eaba2fSLydia Wang 					codec,
35525eaba2fSLydia Wang 					spec->autocfg.line_out_pins[i],
35625eaba2fSLydia Wang 					0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
35725eaba2fSLydia Wang 					lmute);
35825eaba2fSLydia Wang 			for (i = 0; i < spec->autocfg.speaker_outs; i++)
35925eaba2fSLydia Wang 				snd_hda_codec_amp_update(
36025eaba2fSLydia Wang 					codec,
36125eaba2fSLydia Wang 					spec->autocfg.speaker_pins[i],
36225eaba2fSLydia Wang 					0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
36325eaba2fSLydia Wang 					lmute);
36425eaba2fSLydia Wang 		}
36525eaba2fSLydia Wang 		if (rmute) {
36625eaba2fSLydia Wang 			/* mute all right channels */
36725eaba2fSLydia Wang 			for (i = 1; i < spec->autocfg.line_outs; i++)
36825eaba2fSLydia Wang 				snd_hda_codec_amp_update(
36925eaba2fSLydia Wang 					codec,
37025eaba2fSLydia Wang 					spec->autocfg.line_out_pins[i],
37125eaba2fSLydia Wang 					1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
37225eaba2fSLydia Wang 					rmute);
37325eaba2fSLydia Wang 			for (i = 0; i < spec->autocfg.speaker_outs; i++)
37425eaba2fSLydia Wang 				snd_hda_codec_amp_update(
37525eaba2fSLydia Wang 					codec,
37625eaba2fSLydia Wang 					spec->autocfg.speaker_pins[i],
37725eaba2fSLydia Wang 					1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
37825eaba2fSLydia Wang 					rmute);
37925eaba2fSLydia Wang 		}
38025eaba2fSLydia Wang 	}
38125eaba2fSLydia Wang 	return change;
38225eaba2fSLydia Wang }
38325eaba2fSLydia Wang 
38425eaba2fSLydia Wang #define BIND_PIN_MUTE							\
38525eaba2fSLydia Wang 	{		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,		\
38625eaba2fSLydia Wang 			.name = NULL,					\
38725eaba2fSLydia Wang 			.index = 0,					\
38825eaba2fSLydia Wang 			.info = snd_hda_mixer_amp_switch_info,		\
38925eaba2fSLydia Wang 			.get = snd_hda_mixer_amp_switch_get,		\
39025eaba2fSLydia Wang 			.put = bind_pin_switch_put,			\
39125eaba2fSLydia Wang 			.private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
39225eaba2fSLydia Wang 
39371eb7dccSLydia Wang static struct snd_kcontrol_new via_control_templates[] = {
394c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME(NULL, 0, 0, 0),
395c577b8a1SJoseph Chan 	HDA_CODEC_MUTE(NULL, 0, 0, 0),
396f5271101SLydia Wang 	ANALOG_INPUT_MUTE,
39725eaba2fSLydia Wang 	BIND_PIN_MUTE,
398c577b8a1SJoseph Chan };
399c577b8a1SJoseph Chan 
400c577b8a1SJoseph Chan static hda_nid_t vt1708_adc_nids[2] = {
401c577b8a1SJoseph Chan 	/* ADC1-2 */
402c577b8a1SJoseph Chan 	0x15, 0x27
403c577b8a1SJoseph Chan };
404c577b8a1SJoseph Chan 
405c577b8a1SJoseph Chan static hda_nid_t vt1709_adc_nids[3] = {
406c577b8a1SJoseph Chan 	/* ADC1-2 */
407c577b8a1SJoseph Chan 	0x14, 0x15, 0x16
408c577b8a1SJoseph Chan };
409c577b8a1SJoseph Chan 
410f7278fd0SJosepch Chan static hda_nid_t vt1708B_adc_nids[2] = {
411f7278fd0SJosepch Chan 	/* ADC1-2 */
412f7278fd0SJosepch Chan 	0x13, 0x14
413f7278fd0SJosepch Chan };
414f7278fd0SJosepch Chan 
415d949cac1SHarald Welte static hda_nid_t vt1708S_adc_nids[2] = {
416d949cac1SHarald Welte 	/* ADC1-2 */
417d949cac1SHarald Welte 	0x13, 0x14
418d949cac1SHarald Welte };
419d949cac1SHarald Welte 
420d949cac1SHarald Welte static hda_nid_t vt1702_adc_nids[3] = {
421d949cac1SHarald Welte 	/* ADC1-2 */
422d949cac1SHarald Welte 	0x12, 0x20, 0x1F
423d949cac1SHarald Welte };
424d949cac1SHarald Welte 
425eb7188caSLydia Wang static hda_nid_t vt1718S_adc_nids[2] = {
426eb7188caSLydia Wang 	/* ADC1-2 */
427eb7188caSLydia Wang 	0x10, 0x11
428eb7188caSLydia Wang };
429eb7188caSLydia Wang 
430f3db423dSLydia Wang static hda_nid_t vt1716S_adc_nids[2] = {
431f3db423dSLydia Wang 	/* ADC1-2 */
432f3db423dSLydia Wang 	0x13, 0x14
433f3db423dSLydia Wang };
434f3db423dSLydia Wang 
43525eaba2fSLydia Wang static hda_nid_t vt2002P_adc_nids[2] = {
43625eaba2fSLydia Wang 	/* ADC1-2 */
43725eaba2fSLydia Wang 	0x10, 0x11
43825eaba2fSLydia Wang };
43925eaba2fSLydia Wang 
440ab6734e7SLydia Wang static hda_nid_t vt1812_adc_nids[2] = {
441ab6734e7SLydia Wang 	/* ADC1-2 */
442ab6734e7SLydia Wang 	0x10, 0x11
443ab6734e7SLydia Wang };
444ab6734e7SLydia Wang 
445ab6734e7SLydia Wang 
446c577b8a1SJoseph Chan /* add dynamic controls */
4477b315bb4STakashi Iwai static int __via_add_control(struct via_spec *spec, int type, const char *name,
4487b315bb4STakashi Iwai 			     int idx, unsigned long val)
449c577b8a1SJoseph Chan {
450c577b8a1SJoseph Chan 	struct snd_kcontrol_new *knew;
451c577b8a1SJoseph Chan 
452603c4019STakashi Iwai 	snd_array_init(&spec->kctls, sizeof(*knew), 32);
453603c4019STakashi Iwai 	knew = snd_array_new(&spec->kctls);
454c577b8a1SJoseph Chan 	if (!knew)
455c577b8a1SJoseph Chan 		return -ENOMEM;
45671eb7dccSLydia Wang 	*knew = via_control_templates[type];
457c577b8a1SJoseph Chan 	knew->name = kstrdup(name, GFP_KERNEL);
458c577b8a1SJoseph Chan 	if (!knew->name)
459c577b8a1SJoseph Chan 		return -ENOMEM;
4604d02d1b6SJaroslav Kysela 	if (get_amp_nid_(val))
4615e26dfd0SJaroslav Kysela 		knew->subdevice = HDA_SUBDEV_AMP_FLAG;
462c577b8a1SJoseph Chan 	knew->private_value = val;
463c577b8a1SJoseph Chan 	return 0;
464c577b8a1SJoseph Chan }
465c577b8a1SJoseph Chan 
4667b315bb4STakashi Iwai #define via_add_control(spec, type, name, val) \
4677b315bb4STakashi Iwai 	__via_add_control(spec, type, name, 0, val)
4687b315bb4STakashi Iwai 
4695b0cb1d8SJaroslav Kysela static struct snd_kcontrol_new *via_clone_control(struct via_spec *spec,
4705b0cb1d8SJaroslav Kysela 						struct snd_kcontrol_new *tmpl)
4715b0cb1d8SJaroslav Kysela {
4725b0cb1d8SJaroslav Kysela 	struct snd_kcontrol_new *knew;
4735b0cb1d8SJaroslav Kysela 
4745b0cb1d8SJaroslav Kysela 	snd_array_init(&spec->kctls, sizeof(*knew), 32);
4755b0cb1d8SJaroslav Kysela 	knew = snd_array_new(&spec->kctls);
4765b0cb1d8SJaroslav Kysela 	if (!knew)
4775b0cb1d8SJaroslav Kysela 		return NULL;
4785b0cb1d8SJaroslav Kysela 	*knew = *tmpl;
4795b0cb1d8SJaroslav Kysela 	knew->name = kstrdup(tmpl->name, GFP_KERNEL);
4805b0cb1d8SJaroslav Kysela 	if (!knew->name)
4815b0cb1d8SJaroslav Kysela 		return NULL;
482b331439dSTakashi Iwai 	return knew;
4835b0cb1d8SJaroslav Kysela }
4845b0cb1d8SJaroslav Kysela 
485603c4019STakashi Iwai static void via_free_kctls(struct hda_codec *codec)
486603c4019STakashi Iwai {
487603c4019STakashi Iwai 	struct via_spec *spec = codec->spec;
488603c4019STakashi Iwai 
489603c4019STakashi Iwai 	if (spec->kctls.list) {
490603c4019STakashi Iwai 		struct snd_kcontrol_new *kctl = spec->kctls.list;
491603c4019STakashi Iwai 		int i;
492603c4019STakashi Iwai 		for (i = 0; i < spec->kctls.used; i++)
493603c4019STakashi Iwai 			kfree(kctl[i].name);
494603c4019STakashi Iwai 	}
495603c4019STakashi Iwai 	snd_array_free(&spec->kctls);
496603c4019STakashi Iwai }
497603c4019STakashi Iwai 
498c577b8a1SJoseph Chan /* create input playback/capture controls for the given pin */
4999510e8ddSLydia Wang static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
5007b315bb4STakashi Iwai 				int type_idx, int idx, int mix_nid)
501c577b8a1SJoseph Chan {
502c577b8a1SJoseph Chan 	char name[32];
503c577b8a1SJoseph Chan 	int err;
504c577b8a1SJoseph Chan 
505c577b8a1SJoseph Chan 	sprintf(name, "%s Playback Volume", ctlname);
5067b315bb4STakashi Iwai 	err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
507c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
508c577b8a1SJoseph Chan 	if (err < 0)
509c577b8a1SJoseph Chan 		return err;
510c577b8a1SJoseph Chan 	sprintf(name, "%s Playback Switch", ctlname);
5117b315bb4STakashi Iwai 	err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
512c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
513c577b8a1SJoseph Chan 	if (err < 0)
514c577b8a1SJoseph Chan 		return err;
515c577b8a1SJoseph Chan 	return 0;
516c577b8a1SJoseph Chan }
517c577b8a1SJoseph Chan 
518c577b8a1SJoseph Chan static void via_auto_set_output_and_unmute(struct hda_codec *codec,
519c577b8a1SJoseph Chan 					   hda_nid_t nid, int pin_type,
520c577b8a1SJoseph Chan 					   int dac_idx)
521c577b8a1SJoseph Chan {
522c577b8a1SJoseph Chan 	/* set as output */
523c577b8a1SJoseph Chan 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
524c577b8a1SJoseph Chan 			    pin_type);
525c577b8a1SJoseph Chan 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
526c577b8a1SJoseph Chan 			    AMP_OUT_UNMUTE);
527d3a11e60STakashi Iwai 	if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
528d3a11e60STakashi Iwai 		snd_hda_codec_write(codec, nid, 0,
529d3a11e60STakashi Iwai 				    AC_VERB_SET_EAPD_BTLENABLE, 0x02);
530c577b8a1SJoseph Chan }
531c577b8a1SJoseph Chan 
532c577b8a1SJoseph Chan 
533c577b8a1SJoseph Chan static void via_auto_init_multi_out(struct hda_codec *codec)
534c577b8a1SJoseph Chan {
535c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
536c577b8a1SJoseph Chan 	int i;
537c577b8a1SJoseph Chan 
538c577b8a1SJoseph Chan 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
539c577b8a1SJoseph Chan 		hda_nid_t nid = spec->autocfg.line_out_pins[i];
540c577b8a1SJoseph Chan 		if (nid)
541c577b8a1SJoseph Chan 			via_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
542c577b8a1SJoseph Chan 	}
543c577b8a1SJoseph Chan }
544c577b8a1SJoseph Chan 
545c577b8a1SJoseph Chan static void via_auto_init_hp_out(struct hda_codec *codec)
546c577b8a1SJoseph Chan {
547c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
548c577b8a1SJoseph Chan 	hda_nid_t pin;
54925eaba2fSLydia Wang 	int i;
550c577b8a1SJoseph Chan 
55125eaba2fSLydia Wang 	for (i = 0; i < spec->autocfg.hp_outs; i++) {
55225eaba2fSLydia Wang 		pin = spec->autocfg.hp_pins[i];
553c577b8a1SJoseph Chan 		if (pin) /* connect to front */
554c577b8a1SJoseph Chan 			via_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
555c577b8a1SJoseph Chan 	}
55625eaba2fSLydia Wang }
557c577b8a1SJoseph Chan 
55832e0191dSClemens Ladisch static int is_smart51_pins(struct via_spec *spec, hda_nid_t pin);
55932e0191dSClemens Ladisch 
560c577b8a1SJoseph Chan static void via_auto_init_analog_input(struct hda_codec *codec)
561c577b8a1SJoseph Chan {
562c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
5637b315bb4STakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
56432e0191dSClemens Ladisch 	unsigned int ctl;
565c577b8a1SJoseph Chan 	int i;
566c577b8a1SJoseph Chan 
5677b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
5687b315bb4STakashi Iwai 		hda_nid_t nid = cfg->inputs[i].pin;
56932e0191dSClemens Ladisch 		if (spec->smart51_enabled && is_smart51_pins(spec, nid))
57032e0191dSClemens Ladisch 			ctl = PIN_OUT;
57186e2959aSTakashi Iwai 		else if (i == AUTO_PIN_MIC)
57232e0191dSClemens Ladisch 			ctl = PIN_VREF50;
57332e0191dSClemens Ladisch 		else
57432e0191dSClemens Ladisch 			ctl = PIN_IN;
575c577b8a1SJoseph Chan 		snd_hda_codec_write(codec, nid, 0,
57632e0191dSClemens Ladisch 				    AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
577c577b8a1SJoseph Chan 	}
578c577b8a1SJoseph Chan }
579f5271101SLydia Wang 
580f5271101SLydia Wang static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
581f5271101SLydia Wang 				unsigned int *affected_parm)
582f5271101SLydia Wang {
583f5271101SLydia Wang 	unsigned parm;
584f5271101SLydia Wang 	unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
585f5271101SLydia Wang 	unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
586f5271101SLydia Wang 		>> AC_DEFCFG_MISC_SHIFT
587f5271101SLydia Wang 		& AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
588d56757abSTakashi Iwai 	unsigned present = snd_hda_jack_detect(codec, nid);
5891564b287SLydia Wang 	struct via_spec *spec = codec->spec;
5901564b287SLydia Wang 	if ((spec->smart51_enabled && is_smart51_pins(spec, nid))
5911564b287SLydia Wang 	    || ((no_presence || present)
5921564b287SLydia Wang 		&& get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
593f5271101SLydia Wang 		*affected_parm = AC_PWRST_D0; /* if it's connected */
594f5271101SLydia Wang 		parm = AC_PWRST_D0;
595f5271101SLydia Wang 	} else
596f5271101SLydia Wang 		parm = AC_PWRST_D3;
597f5271101SLydia Wang 
598f5271101SLydia Wang 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
599f5271101SLydia Wang }
600f5271101SLydia Wang 
601f5271101SLydia Wang static void set_jack_power_state(struct hda_codec *codec)
602f5271101SLydia Wang {
603f5271101SLydia Wang 	struct via_spec *spec = codec->spec;
604f5271101SLydia Wang 	int imux_is_smixer;
605f5271101SLydia Wang 	unsigned int parm;
606f5271101SLydia Wang 
607f5271101SLydia Wang 	if (spec->codec_type == VT1702) {
608f5271101SLydia Wang 		imux_is_smixer = snd_hda_codec_read(
609f5271101SLydia Wang 			codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
610f5271101SLydia Wang 		/* inputs */
611f5271101SLydia Wang 		/* PW 1/2/5 (14h/15h/18h) */
612f5271101SLydia Wang 		parm = AC_PWRST_D3;
613f5271101SLydia Wang 		set_pin_power_state(codec, 0x14, &parm);
614f5271101SLydia Wang 		set_pin_power_state(codec, 0x15, &parm);
615f5271101SLydia Wang 		set_pin_power_state(codec, 0x18, &parm);
616f5271101SLydia Wang 		if (imux_is_smixer)
617f5271101SLydia Wang 			parm = AC_PWRST_D0; /* SW0 = stereo mixer (idx 3) */
618f5271101SLydia Wang 		/* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
619f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE,
620f5271101SLydia Wang 				    parm);
621f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x12, 0, AC_VERB_SET_POWER_STATE,
622f5271101SLydia Wang 				    parm);
623f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE,
624f5271101SLydia Wang 				    parm);
625f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_POWER_STATE,
626f5271101SLydia Wang 				    parm);
627f5271101SLydia Wang 
628f5271101SLydia Wang 		/* outputs */
629f5271101SLydia Wang 		/* PW 3/4 (16h/17h) */
630f5271101SLydia Wang 		parm = AC_PWRST_D3;
631f5271101SLydia Wang 		set_pin_power_state(codec, 0x16, &parm);
632f5271101SLydia Wang 		set_pin_power_state(codec, 0x17, &parm);
633f5271101SLydia Wang 		/* MW0 (1ah), AOW 0/1 (10h/1dh) */
634f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
635f5271101SLydia Wang 				    imux_is_smixer ? AC_PWRST_D0 : parm);
636f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
637f5271101SLydia Wang 				    parm);
638f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE,
639f5271101SLydia Wang 				    parm);
640f5271101SLydia Wang 	} else if (spec->codec_type == VT1708B_8CH
641f5271101SLydia Wang 		   || spec->codec_type == VT1708B_4CH
642f5271101SLydia Wang 		   || spec->codec_type == VT1708S) {
643f5271101SLydia Wang 		/* SW0 (17h) = stereo mixer */
644f5271101SLydia Wang 		int is_8ch = spec->codec_type != VT1708B_4CH;
645f5271101SLydia Wang 		imux_is_smixer = snd_hda_codec_read(
646f5271101SLydia Wang 			codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
647f5271101SLydia Wang 			== ((spec->codec_type == VT1708S)  ? 5 : 0);
648f5271101SLydia Wang 		/* inputs */
649f5271101SLydia Wang 		/* PW 1/2/5 (1ah/1bh/1eh) */
650f5271101SLydia Wang 		parm = AC_PWRST_D3;
651f5271101SLydia Wang 		set_pin_power_state(codec, 0x1a, &parm);
652f5271101SLydia Wang 		set_pin_power_state(codec, 0x1b, &parm);
653f5271101SLydia Wang 		set_pin_power_state(codec, 0x1e, &parm);
654f5271101SLydia Wang 		if (imux_is_smixer)
655f5271101SLydia Wang 			parm = AC_PWRST_D0;
656f5271101SLydia Wang 		/* SW0 (17h), AIW 0/1 (13h/14h) */
657f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE,
658f5271101SLydia Wang 				    parm);
659f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE,
660f5271101SLydia Wang 				    parm);
661f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE,
662f5271101SLydia Wang 				    parm);
663f5271101SLydia Wang 
664f5271101SLydia Wang 		/* outputs */
665f5271101SLydia Wang 		/* PW0 (19h), SW1 (18h), AOW1 (11h) */
666f5271101SLydia Wang 		parm = AC_PWRST_D3;
667f5271101SLydia Wang 		set_pin_power_state(codec, 0x19, &parm);
66832e0191dSClemens Ladisch 		if (spec->smart51_enabled)
66932e0191dSClemens Ladisch 			parm = AC_PWRST_D0;
670f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE,
671f5271101SLydia Wang 				    parm);
672f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE,
673f5271101SLydia Wang 				    parm);
674f5271101SLydia Wang 
675f5271101SLydia Wang 		/* PW6 (22h), SW2 (26h), AOW2 (24h) */
676f5271101SLydia Wang 		if (is_8ch) {
677f5271101SLydia Wang 			parm = AC_PWRST_D3;
678f5271101SLydia Wang 			set_pin_power_state(codec, 0x22, &parm);
67932e0191dSClemens Ladisch 			if (spec->smart51_enabled)
68032e0191dSClemens Ladisch 				parm = AC_PWRST_D0;
681f5271101SLydia Wang 			snd_hda_codec_write(codec, 0x26, 0,
682f5271101SLydia Wang 					    AC_VERB_SET_POWER_STATE, parm);
683f5271101SLydia Wang 			snd_hda_codec_write(codec, 0x24, 0,
684f5271101SLydia Wang 					    AC_VERB_SET_POWER_STATE, parm);
685f5271101SLydia Wang 		}
686f5271101SLydia Wang 
687f5271101SLydia Wang 		/* PW 3/4/7 (1ch/1dh/23h) */
688f5271101SLydia Wang 		parm = AC_PWRST_D3;
689f5271101SLydia Wang 		/* force to D0 for internal Speaker */
690f5271101SLydia Wang 		set_pin_power_state(codec, 0x1c, &parm);
691f5271101SLydia Wang 		set_pin_power_state(codec, 0x1d, &parm);
692f5271101SLydia Wang 		if (is_8ch)
693f5271101SLydia Wang 			set_pin_power_state(codec, 0x23, &parm);
694f5271101SLydia Wang 		/* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
695f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
696f5271101SLydia Wang 				    imux_is_smixer ? AC_PWRST_D0 : parm);
697f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
698f5271101SLydia Wang 				    parm);
699f5271101SLydia Wang 		if (is_8ch) {
700f5271101SLydia Wang 			snd_hda_codec_write(codec, 0x25, 0,
701f5271101SLydia Wang 					    AC_VERB_SET_POWER_STATE, parm);
702f5271101SLydia Wang 			snd_hda_codec_write(codec, 0x27, 0,
703f5271101SLydia Wang 					    AC_VERB_SET_POWER_STATE, parm);
704f5271101SLydia Wang 		}
705eb7188caSLydia Wang 	}  else if (spec->codec_type == VT1718S) {
706eb7188caSLydia Wang 		/* MUX6 (1eh) = stereo mixer */
707eb7188caSLydia Wang 		imux_is_smixer = snd_hda_codec_read(
708eb7188caSLydia Wang 			codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
709eb7188caSLydia Wang 		/* inputs */
710eb7188caSLydia Wang 		/* PW 5/6/7 (29h/2ah/2bh) */
711eb7188caSLydia Wang 		parm = AC_PWRST_D3;
712eb7188caSLydia Wang 		set_pin_power_state(codec, 0x29, &parm);
713eb7188caSLydia Wang 		set_pin_power_state(codec, 0x2a, &parm);
714eb7188caSLydia Wang 		set_pin_power_state(codec, 0x2b, &parm);
715eb7188caSLydia Wang 		if (imux_is_smixer)
716eb7188caSLydia Wang 			parm = AC_PWRST_D0;
717eb7188caSLydia Wang 		/* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
718eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE,
719eb7188caSLydia Wang 				    parm);
720eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE,
721eb7188caSLydia Wang 				    parm);
722eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
723eb7188caSLydia Wang 				    parm);
724eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE,
725eb7188caSLydia Wang 				    parm);
726eb7188caSLydia Wang 
727eb7188caSLydia Wang 		/* outputs */
728eb7188caSLydia Wang 		/* PW3 (27h), MW2 (1ah), AOW3 (bh) */
729eb7188caSLydia Wang 		parm = AC_PWRST_D3;
730eb7188caSLydia Wang 		set_pin_power_state(codec, 0x27, &parm);
731eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
732eb7188caSLydia Wang 				    parm);
733eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0xb, 0, AC_VERB_SET_POWER_STATE,
734eb7188caSLydia Wang 				    parm);
735eb7188caSLydia Wang 
736eb7188caSLydia Wang 		/* PW2 (26h), AOW2 (ah) */
737eb7188caSLydia Wang 		parm = AC_PWRST_D3;
738eb7188caSLydia Wang 		set_pin_power_state(codec, 0x26, &parm);
739eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0xa, 0, AC_VERB_SET_POWER_STATE,
740eb7188caSLydia Wang 				    parm);
741eb7188caSLydia Wang 
742eb7188caSLydia Wang 		/* PW0/1 (24h/25h) */
743eb7188caSLydia Wang 		parm = AC_PWRST_D3;
744eb7188caSLydia Wang 		set_pin_power_state(codec, 0x24, &parm);
745eb7188caSLydia Wang 		set_pin_power_state(codec, 0x25, &parm);
746eb7188caSLydia Wang 		if (!spec->hp_independent_mode) /* check for redirected HP */
747eb7188caSLydia Wang 			set_pin_power_state(codec, 0x28, &parm);
748eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE,
749eb7188caSLydia Wang 				    parm);
750eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0x9, 0, AC_VERB_SET_POWER_STATE,
751eb7188caSLydia Wang 				    parm);
752eb7188caSLydia Wang 		/* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
753eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0x21, 0, AC_VERB_SET_POWER_STATE,
754eb7188caSLydia Wang 				    imux_is_smixer ? AC_PWRST_D0 : parm);
755eb7188caSLydia Wang 		if (spec->hp_independent_mode) {
756eb7188caSLydia Wang 			/* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
757eb7188caSLydia Wang 			parm = AC_PWRST_D3;
758eb7188caSLydia Wang 			set_pin_power_state(codec, 0x28, &parm);
759eb7188caSLydia Wang 			snd_hda_codec_write(codec, 0x1b, 0,
760eb7188caSLydia Wang 					    AC_VERB_SET_POWER_STATE, parm);
761eb7188caSLydia Wang 			snd_hda_codec_write(codec, 0x34, 0,
762eb7188caSLydia Wang 					    AC_VERB_SET_POWER_STATE, parm);
763eb7188caSLydia Wang 			snd_hda_codec_write(codec, 0xc, 0,
764eb7188caSLydia Wang 					    AC_VERB_SET_POWER_STATE, parm);
765eb7188caSLydia Wang 		}
766f3db423dSLydia Wang 	} else if (spec->codec_type == VT1716S) {
767f3db423dSLydia Wang 		unsigned int mono_out, present;
768f3db423dSLydia Wang 		/* SW0 (17h) = stereo mixer */
769f3db423dSLydia Wang 		imux_is_smixer = snd_hda_codec_read(
770f3db423dSLydia Wang 			codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00) ==  5;
771f3db423dSLydia Wang 		/* inputs */
772f3db423dSLydia Wang 		/* PW 1/2/5 (1ah/1bh/1eh) */
773f3db423dSLydia Wang 		parm = AC_PWRST_D3;
774f3db423dSLydia Wang 		set_pin_power_state(codec, 0x1a, &parm);
775f3db423dSLydia Wang 		set_pin_power_state(codec, 0x1b, &parm);
776f3db423dSLydia Wang 		set_pin_power_state(codec, 0x1e, &parm);
777f3db423dSLydia Wang 		if (imux_is_smixer)
778f3db423dSLydia Wang 			parm = AC_PWRST_D0;
779f3db423dSLydia Wang 		/* SW0 (17h), AIW0(13h) */
780f3db423dSLydia Wang 		snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE,
781f3db423dSLydia Wang 				    parm);
782f3db423dSLydia Wang 		snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE,
783f3db423dSLydia Wang 				    parm);
784f3db423dSLydia Wang 
785f3db423dSLydia Wang 		parm = AC_PWRST_D3;
786f3db423dSLydia Wang 		set_pin_power_state(codec, 0x1e, &parm);
787f3db423dSLydia Wang 		/* PW11 (22h) */
788f3db423dSLydia Wang 		if (spec->dmic_enabled)
789f3db423dSLydia Wang 			set_pin_power_state(codec, 0x22, &parm);
790f3db423dSLydia Wang 		else
791f3db423dSLydia Wang 			snd_hda_codec_write(
792f3db423dSLydia Wang 				codec, 0x22, 0,
793f3db423dSLydia Wang 				AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
794f3db423dSLydia Wang 
795f3db423dSLydia Wang 		/* SW2(26h), AIW1(14h) */
796f3db423dSLydia Wang 		snd_hda_codec_write(codec, 0x26, 0, AC_VERB_SET_POWER_STATE,
797f3db423dSLydia Wang 				    parm);
798f3db423dSLydia Wang 		snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE,
799f3db423dSLydia Wang 				    parm);
800f3db423dSLydia Wang 
801f3db423dSLydia Wang 		/* outputs */
802f3db423dSLydia Wang 		/* PW0 (19h), SW1 (18h), AOW1 (11h) */
803f3db423dSLydia Wang 		parm = AC_PWRST_D3;
804f3db423dSLydia Wang 		set_pin_power_state(codec, 0x19, &parm);
805f3db423dSLydia Wang 		/* Smart 5.1 PW2(1bh) */
806f3db423dSLydia Wang 		if (spec->smart51_enabled)
807f3db423dSLydia Wang 			set_pin_power_state(codec, 0x1b, &parm);
808f3db423dSLydia Wang 		snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE,
809f3db423dSLydia Wang 				    parm);
810f3db423dSLydia Wang 		snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE,
811f3db423dSLydia Wang 				    parm);
812f3db423dSLydia Wang 
813f3db423dSLydia Wang 		/* PW7 (23h), SW3 (27h), AOW3 (25h) */
814f3db423dSLydia Wang 		parm = AC_PWRST_D3;
815f3db423dSLydia Wang 		set_pin_power_state(codec, 0x23, &parm);
816f3db423dSLydia Wang 		/* Smart 5.1 PW1(1ah) */
817f3db423dSLydia Wang 		if (spec->smart51_enabled)
818f3db423dSLydia Wang 			set_pin_power_state(codec, 0x1a, &parm);
819f3db423dSLydia Wang 		snd_hda_codec_write(codec, 0x27, 0, AC_VERB_SET_POWER_STATE,
820f3db423dSLydia Wang 				    parm);
821f3db423dSLydia Wang 
822f3db423dSLydia Wang 		/* Smart 5.1 PW5(1eh) */
823f3db423dSLydia Wang 		if (spec->smart51_enabled)
824f3db423dSLydia Wang 			set_pin_power_state(codec, 0x1e, &parm);
825f3db423dSLydia Wang 		snd_hda_codec_write(codec, 0x25, 0, AC_VERB_SET_POWER_STATE,
826f3db423dSLydia Wang 				    parm);
827f3db423dSLydia Wang 
828f3db423dSLydia Wang 		/* Mono out */
829f3db423dSLydia Wang 		/* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
830d56757abSTakashi Iwai 		present = snd_hda_jack_detect(codec, 0x1c);
831f3db423dSLydia Wang 		if (present)
832f3db423dSLydia Wang 			mono_out = 0;
833f3db423dSLydia Wang 		else {
834d56757abSTakashi Iwai 			present = snd_hda_jack_detect(codec, 0x1d);
835f3db423dSLydia Wang 			if (!spec->hp_independent_mode && present)
836f3db423dSLydia Wang 				mono_out = 0;
837f3db423dSLydia Wang 			else
838f3db423dSLydia Wang 				mono_out = 1;
839f3db423dSLydia Wang 		}
840f3db423dSLydia Wang 		parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
841f3db423dSLydia Wang 		snd_hda_codec_write(codec, 0x28, 0, AC_VERB_SET_POWER_STATE,
842f3db423dSLydia Wang 				    parm);
843f3db423dSLydia Wang 		snd_hda_codec_write(codec, 0x29, 0, AC_VERB_SET_POWER_STATE,
844f3db423dSLydia Wang 				    parm);
845f3db423dSLydia Wang 		snd_hda_codec_write(codec, 0x2a, 0, AC_VERB_SET_POWER_STATE,
846f3db423dSLydia Wang 				    parm);
847f3db423dSLydia Wang 
848f3db423dSLydia Wang 		/* PW 3/4 (1ch/1dh) */
849f3db423dSLydia Wang 		parm = AC_PWRST_D3;
850f3db423dSLydia Wang 		set_pin_power_state(codec, 0x1c, &parm);
851f3db423dSLydia Wang 		set_pin_power_state(codec, 0x1d, &parm);
852f3db423dSLydia Wang 		/* HP Independent Mode, power on AOW3 */
853f3db423dSLydia Wang 		if (spec->hp_independent_mode)
854f3db423dSLydia Wang 			snd_hda_codec_write(codec, 0x25, 0,
855f3db423dSLydia Wang 					    AC_VERB_SET_POWER_STATE, parm);
856f3db423dSLydia Wang 
857f3db423dSLydia Wang 		/* force to D0 for internal Speaker */
858f3db423dSLydia Wang 		/* MW0 (16h), AOW0 (10h) */
859f3db423dSLydia Wang 		snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
860f3db423dSLydia Wang 				    imux_is_smixer ? AC_PWRST_D0 : parm);
861f3db423dSLydia Wang 		snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
862f3db423dSLydia Wang 				    mono_out ? AC_PWRST_D0 : parm);
86325eaba2fSLydia Wang 	} else if (spec->codec_type == VT2002P) {
86425eaba2fSLydia Wang 		unsigned int present;
86525eaba2fSLydia Wang 		/* MUX9 (1eh) = stereo mixer */
86625eaba2fSLydia Wang 		imux_is_smixer = snd_hda_codec_read(
86725eaba2fSLydia Wang 			codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
86825eaba2fSLydia Wang 		/* inputs */
86925eaba2fSLydia Wang 		/* PW 5/6/7 (29h/2ah/2bh) */
87025eaba2fSLydia Wang 		parm = AC_PWRST_D3;
87125eaba2fSLydia Wang 		set_pin_power_state(codec, 0x29, &parm);
87225eaba2fSLydia Wang 		set_pin_power_state(codec, 0x2a, &parm);
87325eaba2fSLydia Wang 		set_pin_power_state(codec, 0x2b, &parm);
87425eaba2fSLydia Wang 		if (imux_is_smixer)
87525eaba2fSLydia Wang 			parm = AC_PWRST_D0;
87625eaba2fSLydia Wang 		/* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
87725eaba2fSLydia Wang 		snd_hda_codec_write(codec, 0x1e, 0,
87825eaba2fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
87925eaba2fSLydia Wang 		snd_hda_codec_write(codec, 0x1f, 0,
88025eaba2fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
88125eaba2fSLydia Wang 		snd_hda_codec_write(codec, 0x10, 0,
88225eaba2fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
88325eaba2fSLydia Wang 		snd_hda_codec_write(codec, 0x11, 0,
88425eaba2fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
88525eaba2fSLydia Wang 
88625eaba2fSLydia Wang 		/* outputs */
88725eaba2fSLydia Wang 		/* AOW0 (8h)*/
88825eaba2fSLydia Wang 		snd_hda_codec_write(codec, 0x8, 0,
88925eaba2fSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
89025eaba2fSLydia Wang 
89125eaba2fSLydia Wang 		/* PW4 (26h), MW4 (1ch), MUX4(37h) */
89225eaba2fSLydia Wang 		parm = AC_PWRST_D3;
89325eaba2fSLydia Wang 		set_pin_power_state(codec, 0x26, &parm);
89425eaba2fSLydia Wang 		snd_hda_codec_write(codec, 0x1c, 0,
89525eaba2fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
89625eaba2fSLydia Wang 		snd_hda_codec_write(codec, 0x37,
89725eaba2fSLydia Wang 				    0, AC_VERB_SET_POWER_STATE, parm);
89825eaba2fSLydia Wang 
89925eaba2fSLydia Wang 		/* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
90025eaba2fSLydia Wang 		parm = AC_PWRST_D3;
90125eaba2fSLydia Wang 		set_pin_power_state(codec, 0x25, &parm);
90225eaba2fSLydia Wang 		snd_hda_codec_write(codec, 0x19, 0,
90325eaba2fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
90425eaba2fSLydia Wang 		snd_hda_codec_write(codec, 0x35, 0,
90525eaba2fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
90625eaba2fSLydia Wang 		if (spec->hp_independent_mode)	{
90725eaba2fSLydia Wang 			snd_hda_codec_write(codec, 0x9, 0,
90825eaba2fSLydia Wang 					    AC_VERB_SET_POWER_STATE, parm);
90925eaba2fSLydia Wang 		}
91025eaba2fSLydia Wang 
91125eaba2fSLydia Wang 		/* Class-D */
91225eaba2fSLydia Wang 		/* PW0 (24h), MW0(18h), MUX0(34h) */
913d56757abSTakashi Iwai 		present = snd_hda_jack_detect(codec, 0x25);
91425eaba2fSLydia Wang 		parm = AC_PWRST_D3;
91525eaba2fSLydia Wang 		set_pin_power_state(codec, 0x24, &parm);
91625eaba2fSLydia Wang 		if (present) {
91725eaba2fSLydia Wang 			snd_hda_codec_write(
91825eaba2fSLydia Wang 				codec, 0x18, 0,
91925eaba2fSLydia Wang 				AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
92025eaba2fSLydia Wang 			snd_hda_codec_write(
92125eaba2fSLydia Wang 				codec, 0x34, 0,
92225eaba2fSLydia Wang 				AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
92325eaba2fSLydia Wang 		} else {
92425eaba2fSLydia Wang 			snd_hda_codec_write(
92525eaba2fSLydia Wang 				codec, 0x18, 0,
92625eaba2fSLydia Wang 				AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
92725eaba2fSLydia Wang 			snd_hda_codec_write(
92825eaba2fSLydia Wang 				codec, 0x34, 0,
92925eaba2fSLydia Wang 				AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
93025eaba2fSLydia Wang 		}
93125eaba2fSLydia Wang 
93225eaba2fSLydia Wang 		/* Mono Out */
93325eaba2fSLydia Wang 		/* PW15 (31h), MW8(17h), MUX8(3bh) */
934d56757abSTakashi Iwai 		present = snd_hda_jack_detect(codec, 0x26);
93525eaba2fSLydia Wang 		parm = AC_PWRST_D3;
93625eaba2fSLydia Wang 		set_pin_power_state(codec, 0x31, &parm);
93725eaba2fSLydia Wang 		if (present) {
93825eaba2fSLydia Wang 			snd_hda_codec_write(
93925eaba2fSLydia Wang 				codec, 0x17, 0,
94025eaba2fSLydia Wang 				AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
94125eaba2fSLydia Wang 			snd_hda_codec_write(
94225eaba2fSLydia Wang 				codec, 0x3b, 0,
94325eaba2fSLydia Wang 				AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
94425eaba2fSLydia Wang 		} else {
94525eaba2fSLydia Wang 			snd_hda_codec_write(
94625eaba2fSLydia Wang 				codec, 0x17, 0,
94725eaba2fSLydia Wang 				AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
94825eaba2fSLydia Wang 			snd_hda_codec_write(
94925eaba2fSLydia Wang 				codec, 0x3b, 0,
95025eaba2fSLydia Wang 				AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
95125eaba2fSLydia Wang 		}
95225eaba2fSLydia Wang 
95325eaba2fSLydia Wang 		/* MW9 (21h) */
95425eaba2fSLydia Wang 		if (imux_is_smixer || !is_aa_path_mute(codec))
95525eaba2fSLydia Wang 			snd_hda_codec_write(
95625eaba2fSLydia Wang 				codec, 0x21, 0,
95725eaba2fSLydia Wang 				AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
95825eaba2fSLydia Wang 		else
95925eaba2fSLydia Wang 			snd_hda_codec_write(
96025eaba2fSLydia Wang 				codec, 0x21, 0,
96125eaba2fSLydia Wang 				AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
962ab6734e7SLydia Wang 	} else if (spec->codec_type == VT1812) {
963ab6734e7SLydia Wang 		unsigned int present;
964ab6734e7SLydia Wang 		/* MUX10 (1eh) = stereo mixer */
965ab6734e7SLydia Wang 		imux_is_smixer = snd_hda_codec_read(
966ab6734e7SLydia Wang 			codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
967ab6734e7SLydia Wang 		/* inputs */
968ab6734e7SLydia Wang 		/* PW 5/6/7 (29h/2ah/2bh) */
969ab6734e7SLydia Wang 		parm = AC_PWRST_D3;
970ab6734e7SLydia Wang 		set_pin_power_state(codec, 0x29, &parm);
971ab6734e7SLydia Wang 		set_pin_power_state(codec, 0x2a, &parm);
972ab6734e7SLydia Wang 		set_pin_power_state(codec, 0x2b, &parm);
973ab6734e7SLydia Wang 		if (imux_is_smixer)
974ab6734e7SLydia Wang 			parm = AC_PWRST_D0;
975ab6734e7SLydia Wang 		/* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
976ab6734e7SLydia Wang 		snd_hda_codec_write(codec, 0x1e, 0,
977ab6734e7SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
978ab6734e7SLydia Wang 		snd_hda_codec_write(codec, 0x1f, 0,
979ab6734e7SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
980ab6734e7SLydia Wang 		snd_hda_codec_write(codec, 0x10, 0,
981ab6734e7SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
982ab6734e7SLydia Wang 		snd_hda_codec_write(codec, 0x11, 0,
983ab6734e7SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
984ab6734e7SLydia Wang 
985ab6734e7SLydia Wang 		/* outputs */
986ab6734e7SLydia Wang 		/* AOW0 (8h)*/
987ab6734e7SLydia Wang 		snd_hda_codec_write(codec, 0x8, 0,
988ab6734e7SLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
989ab6734e7SLydia Wang 
990ab6734e7SLydia Wang 		/* PW4 (28h), MW4 (18h), MUX4(38h) */
991ab6734e7SLydia Wang 		parm = AC_PWRST_D3;
992ab6734e7SLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
993ab6734e7SLydia Wang 		snd_hda_codec_write(codec, 0x18, 0,
994ab6734e7SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
995ab6734e7SLydia Wang 		snd_hda_codec_write(codec, 0x38, 0,
996ab6734e7SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
997ab6734e7SLydia Wang 
998ab6734e7SLydia Wang 		/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
999ab6734e7SLydia Wang 		parm = AC_PWRST_D3;
1000ab6734e7SLydia Wang 		set_pin_power_state(codec, 0x25, &parm);
1001ab6734e7SLydia Wang 		snd_hda_codec_write(codec, 0x15, 0,
1002ab6734e7SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
1003ab6734e7SLydia Wang 		snd_hda_codec_write(codec, 0x35, 0,
1004ab6734e7SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
1005ab6734e7SLydia Wang 		if (spec->hp_independent_mode)	{
1006ab6734e7SLydia Wang 			snd_hda_codec_write(codec, 0x9, 0,
1007ab6734e7SLydia Wang 					    AC_VERB_SET_POWER_STATE, parm);
1008ab6734e7SLydia Wang 		}
1009ab6734e7SLydia Wang 
1010ab6734e7SLydia Wang 		/* Internal Speaker */
1011ab6734e7SLydia Wang 		/* PW0 (24h), MW0(14h), MUX0(34h) */
1012d56757abSTakashi Iwai 		present = snd_hda_jack_detect(codec, 0x25);
1013ab6734e7SLydia Wang 		parm = AC_PWRST_D3;
1014ab6734e7SLydia Wang 		set_pin_power_state(codec, 0x24, &parm);
1015ab6734e7SLydia Wang 		if (present) {
1016ab6734e7SLydia Wang 			snd_hda_codec_write(codec, 0x14, 0,
1017ab6734e7SLydia Wang 					    AC_VERB_SET_POWER_STATE,
1018ab6734e7SLydia Wang 					    AC_PWRST_D3);
1019ab6734e7SLydia Wang 			snd_hda_codec_write(codec, 0x34, 0,
1020ab6734e7SLydia Wang 					    AC_VERB_SET_POWER_STATE,
1021ab6734e7SLydia Wang 					    AC_PWRST_D3);
1022ab6734e7SLydia Wang 		} else {
1023ab6734e7SLydia Wang 			snd_hda_codec_write(codec, 0x14, 0,
1024ab6734e7SLydia Wang 					    AC_VERB_SET_POWER_STATE,
1025ab6734e7SLydia Wang 					    AC_PWRST_D0);
1026ab6734e7SLydia Wang 			snd_hda_codec_write(codec, 0x34, 0,
1027ab6734e7SLydia Wang 					    AC_VERB_SET_POWER_STATE,
1028ab6734e7SLydia Wang 					    AC_PWRST_D0);
1029ab6734e7SLydia Wang 		}
1030ab6734e7SLydia Wang 		/* Mono Out */
1031ab6734e7SLydia Wang 		/* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
1032d56757abSTakashi Iwai 		present = snd_hda_jack_detect(codec, 0x28);
1033ab6734e7SLydia Wang 		parm = AC_PWRST_D3;
1034ab6734e7SLydia Wang 		set_pin_power_state(codec, 0x31, &parm);
1035ab6734e7SLydia Wang 		if (present) {
1036ab6734e7SLydia Wang 			snd_hda_codec_write(codec, 0x1c, 0,
1037ab6734e7SLydia Wang 					    AC_VERB_SET_POWER_STATE,
1038ab6734e7SLydia Wang 					    AC_PWRST_D3);
1039ab6734e7SLydia Wang 			snd_hda_codec_write(codec, 0x3c, 0,
1040ab6734e7SLydia Wang 					    AC_VERB_SET_POWER_STATE,
1041ab6734e7SLydia Wang 					    AC_PWRST_D3);
1042ab6734e7SLydia Wang 			snd_hda_codec_write(codec, 0x3e, 0,
1043ab6734e7SLydia Wang 					    AC_VERB_SET_POWER_STATE,
1044ab6734e7SLydia Wang 					    AC_PWRST_D3);
1045ab6734e7SLydia Wang 		} else {
1046ab6734e7SLydia Wang 			snd_hda_codec_write(codec, 0x1c, 0,
1047ab6734e7SLydia Wang 					    AC_VERB_SET_POWER_STATE,
1048ab6734e7SLydia Wang 					    AC_PWRST_D0);
1049ab6734e7SLydia Wang 			snd_hda_codec_write(codec, 0x3c, 0,
1050ab6734e7SLydia Wang 					    AC_VERB_SET_POWER_STATE,
1051ab6734e7SLydia Wang 					    AC_PWRST_D0);
1052ab6734e7SLydia Wang 			snd_hda_codec_write(codec, 0x3e, 0,
1053ab6734e7SLydia Wang 					    AC_VERB_SET_POWER_STATE,
1054ab6734e7SLydia Wang 					    AC_PWRST_D0);
1055ab6734e7SLydia Wang 		}
1056ab6734e7SLydia Wang 
1057ab6734e7SLydia Wang 		/* PW15 (33h), MW15 (1dh), MUX15(3dh) */
1058ab6734e7SLydia Wang 		parm = AC_PWRST_D3;
1059ab6734e7SLydia Wang 		set_pin_power_state(codec, 0x33, &parm);
1060ab6734e7SLydia Wang 		snd_hda_codec_write(codec, 0x1d, 0,
1061ab6734e7SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
1062ab6734e7SLydia Wang 		snd_hda_codec_write(codec, 0x3d, 0,
1063ab6734e7SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
1064ab6734e7SLydia Wang 
1065ab6734e7SLydia Wang 		/* MW9 (21h) */
1066ab6734e7SLydia Wang 		if (imux_is_smixer || !is_aa_path_mute(codec))
1067ab6734e7SLydia Wang 			snd_hda_codec_write(
1068ab6734e7SLydia Wang 				codec, 0x21, 0,
1069ab6734e7SLydia Wang 				AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1070ab6734e7SLydia Wang 		else
1071ab6734e7SLydia Wang 			snd_hda_codec_write(
1072ab6734e7SLydia Wang 				codec, 0x21, 0,
1073ab6734e7SLydia Wang 				AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1074f5271101SLydia Wang 	}
1075f5271101SLydia Wang }
1076f5271101SLydia Wang 
1077c577b8a1SJoseph Chan /*
1078c577b8a1SJoseph Chan  * input MUX handling
1079c577b8a1SJoseph Chan  */
1080c577b8a1SJoseph Chan static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
1081c577b8a1SJoseph Chan 			     struct snd_ctl_elem_info *uinfo)
1082c577b8a1SJoseph Chan {
1083c577b8a1SJoseph Chan 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1084c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1085c577b8a1SJoseph Chan 	return snd_hda_input_mux_info(spec->input_mux, uinfo);
1086c577b8a1SJoseph Chan }
1087c577b8a1SJoseph Chan 
1088c577b8a1SJoseph Chan static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
1089c577b8a1SJoseph Chan 			    struct snd_ctl_elem_value *ucontrol)
1090c577b8a1SJoseph Chan {
1091c577b8a1SJoseph Chan 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1092c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1093c577b8a1SJoseph Chan 	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1094c577b8a1SJoseph Chan 
1095c577b8a1SJoseph Chan 	ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1096c577b8a1SJoseph Chan 	return 0;
1097c577b8a1SJoseph Chan }
1098c577b8a1SJoseph Chan 
1099c577b8a1SJoseph Chan static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
1100c577b8a1SJoseph Chan 			    struct snd_ctl_elem_value *ucontrol)
1101c577b8a1SJoseph Chan {
1102c577b8a1SJoseph Chan 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1103c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1104c577b8a1SJoseph Chan 	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1105c577b8a1SJoseph Chan 
1106337b9d02STakashi Iwai 	if (!spec->mux_nids[adc_idx])
1107337b9d02STakashi Iwai 		return -EINVAL;
1108a80e6e3cSLydia Wang 	/* switch to D0 beofre change index */
1109a80e6e3cSLydia Wang 	if (snd_hda_codec_read(codec, spec->mux_nids[adc_idx], 0,
1110a80e6e3cSLydia Wang 			       AC_VERB_GET_POWER_STATE, 0x00) != AC_PWRST_D0)
1111a80e6e3cSLydia Wang 		snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
1112a80e6e3cSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1113a80e6e3cSLydia Wang 	/* update jack power state */
1114a80e6e3cSLydia Wang 	set_jack_power_state(codec);
1115a80e6e3cSLydia Wang 
1116c577b8a1SJoseph Chan 	return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
1117337b9d02STakashi Iwai 				     spec->mux_nids[adc_idx],
1118c577b8a1SJoseph Chan 				     &spec->cur_mux[adc_idx]);
1119c577b8a1SJoseph Chan }
1120c577b8a1SJoseph Chan 
11210aa62aefSHarald Welte static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
11220aa62aefSHarald Welte 				   struct snd_ctl_elem_info *uinfo)
11230aa62aefSHarald Welte {
11240aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
11250aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
11260aa62aefSHarald Welte 	return snd_hda_input_mux_info(spec->hp_mux, uinfo);
11270aa62aefSHarald Welte }
11280aa62aefSHarald Welte 
11290aa62aefSHarald Welte static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
11300aa62aefSHarald Welte 				  struct snd_ctl_elem_value *ucontrol)
11310aa62aefSHarald Welte {
11320aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
11335b0cb1d8SJaroslav Kysela 	hda_nid_t nid = kcontrol->private_value;
1134eb7188caSLydia Wang 	unsigned int pinsel;
1135eb7188caSLydia Wang 
1136eb7188caSLydia Wang 	/* use !! to translate conn sel 2 for VT1718S */
1137eb7188caSLydia Wang 	pinsel = !!snd_hda_codec_read(codec, nid, 0,
11380aa62aefSHarald Welte 				      AC_VERB_GET_CONNECT_SEL,
11390aa62aefSHarald Welte 				      0x00);
11400aa62aefSHarald Welte 	ucontrol->value.enumerated.item[0] = pinsel;
11410aa62aefSHarald Welte 
11420aa62aefSHarald Welte 	return 0;
11430aa62aefSHarald Welte }
11440aa62aefSHarald Welte 
11450713efebSLydia Wang static void activate_ctl(struct hda_codec *codec, const char *name, int active)
11460713efebSLydia Wang {
11470713efebSLydia Wang 	struct snd_kcontrol *ctl = snd_hda_find_mixer_ctl(codec, name);
11480713efebSLydia Wang 	if (ctl) {
11490713efebSLydia Wang 		ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
11500713efebSLydia Wang 		ctl->vd[0].access |= active
11510713efebSLydia Wang 			? 0 : SNDRV_CTL_ELEM_ACCESS_INACTIVE;
11520713efebSLydia Wang 		snd_ctl_notify(codec->bus->card,
11530713efebSLydia Wang 			       SNDRV_CTL_EVENT_MASK_VALUE, &ctl->id);
11540713efebSLydia Wang 	}
11550713efebSLydia Wang }
11560713efebSLydia Wang 
11575b0cb1d8SJaroslav Kysela static hda_nid_t side_mute_channel(struct via_spec *spec)
11585b0cb1d8SJaroslav Kysela {
11595b0cb1d8SJaroslav Kysela 	switch (spec->codec_type) {
11605b0cb1d8SJaroslav Kysela 	case VT1708:		return 0x1b;
11615b0cb1d8SJaroslav Kysela 	case VT1709_10CH:	return 0x29;
11625b0cb1d8SJaroslav Kysela 	case VT1708B_8CH:	/* fall thru */
11635b0cb1d8SJaroslav Kysela 	case VT1708S:		return 0x27;
11645b0cb1d8SJaroslav Kysela 	default:		return 0;
11655b0cb1d8SJaroslav Kysela 	}
11665b0cb1d8SJaroslav Kysela }
11675b0cb1d8SJaroslav Kysela 
1168cdc1784dSLydia Wang static int update_side_mute_status(struct hda_codec *codec)
1169cdc1784dSLydia Wang {
1170cdc1784dSLydia Wang 	/* mute side channel */
1171cdc1784dSLydia Wang 	struct via_spec *spec = codec->spec;
1172cdc1784dSLydia Wang 	unsigned int parm = spec->hp_independent_mode
1173cdc1784dSLydia Wang 		? AMP_OUT_MUTE : AMP_OUT_UNMUTE;
11745b0cb1d8SJaroslav Kysela 	hda_nid_t sw3 = side_mute_channel(spec);
1175cdc1784dSLydia Wang 
1176cdc1784dSLydia Wang 	if (sw3)
1177cdc1784dSLydia Wang 		snd_hda_codec_write(codec, sw3, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1178cdc1784dSLydia Wang 				    parm);
1179cdc1784dSLydia Wang 	return 0;
1180cdc1784dSLydia Wang }
1181cdc1784dSLydia Wang 
11820aa62aefSHarald Welte static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
11830aa62aefSHarald Welte 				  struct snd_ctl_elem_value *ucontrol)
11840aa62aefSHarald Welte {
11850aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
11860aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
11875b0cb1d8SJaroslav Kysela 	hda_nid_t nid = kcontrol->private_value;
11880aa62aefSHarald Welte 	unsigned int pinsel = ucontrol->value.enumerated.item[0];
1189cdc1784dSLydia Wang 	/* Get Independent Mode index of headphone pin widget */
1190cdc1784dSLydia Wang 	spec->hp_independent_mode = spec->hp_independent_mode_index == pinsel
1191cdc1784dSLydia Wang 		? 1 : 0;
1192cdc1784dSLydia Wang 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, pinsel);
11930aa62aefSHarald Welte 
1194cdc1784dSLydia Wang 	if (spec->multiout.hp_nid && spec->multiout.hp_nid
1195cdc1784dSLydia Wang 	    != spec->multiout.dac_nids[HDA_FRONT])
1196cdc1784dSLydia Wang 		snd_hda_codec_setup_stream(codec, spec->multiout.hp_nid,
11970aa62aefSHarald Welte 					   0, 0, 0);
11980aa62aefSHarald Welte 
1199cdc1784dSLydia Wang 	update_side_mute_status(codec);
12000713efebSLydia Wang 	/* update HP volume/swtich active state */
12010713efebSLydia Wang 	if (spec->codec_type == VT1708S
1202eb7188caSLydia Wang 	    || spec->codec_type == VT1702
1203f3db423dSLydia Wang 	    || spec->codec_type == VT1718S
120425eaba2fSLydia Wang 	    || spec->codec_type == VT1716S
1205ab6734e7SLydia Wang 	    || spec->codec_type == VT2002P
1206ab6734e7SLydia Wang 	    || spec->codec_type == VT1812) {
12070713efebSLydia Wang 		activate_ctl(codec, "Headphone Playback Volume",
12080713efebSLydia Wang 			     spec->hp_independent_mode);
12090713efebSLydia Wang 		activate_ctl(codec, "Headphone Playback Switch",
12100713efebSLydia Wang 			     spec->hp_independent_mode);
12110713efebSLydia Wang 	}
12120aa62aefSHarald Welte 	return 0;
12130aa62aefSHarald Welte }
12140aa62aefSHarald Welte 
12155b0cb1d8SJaroslav Kysela static struct snd_kcontrol_new via_hp_mixer[2] = {
12160aa62aefSHarald Welte 	{
12170aa62aefSHarald Welte 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
12180aa62aefSHarald Welte 		.name = "Independent HP",
12190aa62aefSHarald Welte 		.info = via_independent_hp_info,
12200aa62aefSHarald Welte 		.get = via_independent_hp_get,
12210aa62aefSHarald Welte 		.put = via_independent_hp_put,
12220aa62aefSHarald Welte 	},
12235b0cb1d8SJaroslav Kysela 	{
12245b0cb1d8SJaroslav Kysela 		.iface = NID_MAPPING,
12255b0cb1d8SJaroslav Kysela 		.name = "Independent HP",
12265b0cb1d8SJaroslav Kysela 	},
12270aa62aefSHarald Welte };
12280aa62aefSHarald Welte 
12293d83e577STakashi Iwai static int via_hp_build(struct hda_codec *codec)
12305b0cb1d8SJaroslav Kysela {
12313d83e577STakashi Iwai 	struct via_spec *spec = codec->spec;
12325b0cb1d8SJaroslav Kysela 	struct snd_kcontrol_new *knew;
12335b0cb1d8SJaroslav Kysela 	hda_nid_t nid;
12343d83e577STakashi Iwai 	int nums;
12353d83e577STakashi Iwai 	hda_nid_t conn[HDA_MAX_CONNECTIONS];
12365b0cb1d8SJaroslav Kysela 
12375b0cb1d8SJaroslav Kysela 	switch (spec->codec_type) {
12385b0cb1d8SJaroslav Kysela 	case VT1718S:
12395b0cb1d8SJaroslav Kysela 		nid = 0x34;
12405b0cb1d8SJaroslav Kysela 		break;
12415b0cb1d8SJaroslav Kysela 	case VT2002P:
12425b0cb1d8SJaroslav Kysela 		nid = 0x35;
12435b0cb1d8SJaroslav Kysela 		break;
12445b0cb1d8SJaroslav Kysela 	case VT1812:
12455b0cb1d8SJaroslav Kysela 		nid = 0x3d;
12465b0cb1d8SJaroslav Kysela 		break;
12475b0cb1d8SJaroslav Kysela 	default:
12485b0cb1d8SJaroslav Kysela 		nid = spec->autocfg.hp_pins[0];
12495b0cb1d8SJaroslav Kysela 		break;
12505b0cb1d8SJaroslav Kysela 	}
12515b0cb1d8SJaroslav Kysela 
12523d83e577STakashi Iwai 	nums = snd_hda_get_connections(codec, nid, conn, HDA_MAX_CONNECTIONS);
12533d83e577STakashi Iwai 	if (nums <= 1)
12543d83e577STakashi Iwai 		return 0;
12553d83e577STakashi Iwai 
12563d83e577STakashi Iwai 	knew = via_clone_control(spec, &via_hp_mixer[0]);
12573d83e577STakashi Iwai 	if (knew == NULL)
12583d83e577STakashi Iwai 		return -ENOMEM;
12593d83e577STakashi Iwai 
12605b0cb1d8SJaroslav Kysela 	knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
12615b0cb1d8SJaroslav Kysela 	knew->private_value = nid;
12625b0cb1d8SJaroslav Kysela 
12635b0cb1d8SJaroslav Kysela 	knew = via_clone_control(spec, &via_hp_mixer[1]);
12645b0cb1d8SJaroslav Kysela 	if (knew == NULL)
12655b0cb1d8SJaroslav Kysela 		return -ENOMEM;
12665b0cb1d8SJaroslav Kysela 	knew->subdevice = side_mute_channel(spec);
12675b0cb1d8SJaroslav Kysela 
12685b0cb1d8SJaroslav Kysela 	return 0;
12695b0cb1d8SJaroslav Kysela }
12705b0cb1d8SJaroslav Kysela 
12711564b287SLydia Wang static void notify_aa_path_ctls(struct hda_codec *codec)
12721564b287SLydia Wang {
12731564b287SLydia Wang 	int i;
12741564b287SLydia Wang 	struct snd_ctl_elem_id id;
12751564b287SLydia Wang 	const char *labels[] = {"Mic", "Front Mic", "Line"};
12761564b287SLydia Wang 
12771564b287SLydia Wang 	memset(&id, 0, sizeof(id));
12781564b287SLydia Wang 	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
12791564b287SLydia Wang 	for (i = 0; i < ARRAY_SIZE(labels); i++) {
12801564b287SLydia Wang 		sprintf(id.name, "%s Playback Volume", labels[i]);
12811564b287SLydia Wang 		snd_ctl_notify(codec->bus->card, SNDRV_CTL_EVENT_MASK_VALUE,
12821564b287SLydia Wang 			       &id);
12831564b287SLydia Wang 	}
12841564b287SLydia Wang }
12851564b287SLydia Wang 
12861564b287SLydia Wang static void mute_aa_path(struct hda_codec *codec, int mute)
12871564b287SLydia Wang {
12881564b287SLydia Wang 	struct via_spec *spec = codec->spec;
12891564b287SLydia Wang 	hda_nid_t  nid_mixer;
12901564b287SLydia Wang 	int start_idx;
12911564b287SLydia Wang 	int end_idx;
12921564b287SLydia Wang 	int i;
12931564b287SLydia Wang 	/* get nid of MW0 and start & end index */
12941564b287SLydia Wang 	switch (spec->codec_type) {
12951564b287SLydia Wang 	case VT1708:
12961564b287SLydia Wang 		nid_mixer = 0x17;
12971564b287SLydia Wang 		start_idx = 2;
12981564b287SLydia Wang 		end_idx = 4;
12991564b287SLydia Wang 		break;
13001564b287SLydia Wang 	case VT1709_10CH:
13011564b287SLydia Wang 	case VT1709_6CH:
13021564b287SLydia Wang 		nid_mixer = 0x18;
13031564b287SLydia Wang 		start_idx = 2;
13041564b287SLydia Wang 		end_idx = 4;
13051564b287SLydia Wang 		break;
13061564b287SLydia Wang 	case VT1708B_8CH:
13071564b287SLydia Wang 	case VT1708B_4CH:
13081564b287SLydia Wang 	case VT1708S:
1309f3db423dSLydia Wang 	case VT1716S:
13101564b287SLydia Wang 		nid_mixer = 0x16;
13111564b287SLydia Wang 		start_idx = 2;
13121564b287SLydia Wang 		end_idx = 4;
13131564b287SLydia Wang 		break;
13141564b287SLydia Wang 	default:
13151564b287SLydia Wang 		return;
13161564b287SLydia Wang 	}
13171564b287SLydia Wang 	/* check AA path's mute status */
13181564b287SLydia Wang 	for (i = start_idx; i <= end_idx; i++) {
13191564b287SLydia Wang 		int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
13201564b287SLydia Wang 		snd_hda_codec_amp_stereo(codec, nid_mixer, HDA_INPUT, i,
13211564b287SLydia Wang 					 HDA_AMP_MUTE, val);
13221564b287SLydia Wang 	}
13231564b287SLydia Wang }
13241564b287SLydia Wang static int is_smart51_pins(struct via_spec *spec, hda_nid_t pin)
13251564b287SLydia Wang {
13267b315bb4STakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
13277b315bb4STakashi Iwai 	int i;
13287b315bb4STakashi Iwai 
13297b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
13307b315bb4STakashi Iwai 		if (pin == cfg->inputs[i].pin)
133186e2959aSTakashi Iwai 			return cfg->inputs[i].type <= AUTO_PIN_LINE_IN;
13321564b287SLydia Wang 	}
13337b315bb4STakashi Iwai 	return 0;
13341564b287SLydia Wang }
13351564b287SLydia Wang 
13361564b287SLydia Wang static int via_smart51_info(struct snd_kcontrol *kcontrol,
13371564b287SLydia Wang 			    struct snd_ctl_elem_info *uinfo)
13381564b287SLydia Wang {
13391564b287SLydia Wang 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
13401564b287SLydia Wang 	uinfo->count = 1;
13411564b287SLydia Wang 	uinfo->value.integer.min = 0;
13421564b287SLydia Wang 	uinfo->value.integer.max = 1;
13431564b287SLydia Wang 	return 0;
13441564b287SLydia Wang }
13451564b287SLydia Wang 
13461564b287SLydia Wang static int via_smart51_get(struct snd_kcontrol *kcontrol,
13471564b287SLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
13481564b287SLydia Wang {
13491564b287SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
13501564b287SLydia Wang 	struct via_spec *spec = codec->spec;
13517b315bb4STakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
13521564b287SLydia Wang 	int on = 1;
13531564b287SLydia Wang 	int i;
13541564b287SLydia Wang 
13557b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
13567b315bb4STakashi Iwai 		hda_nid_t nid = cfg->inputs[i].pin;
13577b315bb4STakashi Iwai 		int ctl = snd_hda_codec_read(codec, nid, 0,
13587b315bb4STakashi Iwai 					     AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
135986e2959aSTakashi Iwai 		if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
13607b315bb4STakashi Iwai 			continue;
136186e2959aSTakashi Iwai 		if (cfg->inputs[i].type == AUTO_PIN_MIC &&
13627b315bb4STakashi Iwai 		    spec->hp_independent_mode && spec->codec_type != VT1718S)
13631564b287SLydia Wang 			continue; /* ignore FMic for independent HP */
13647b315bb4STakashi Iwai 		if ((ctl & AC_PINCTL_IN_EN) && !(ctl & AC_PINCTL_OUT_EN))
13651564b287SLydia Wang 			on = 0;
13661564b287SLydia Wang 	}
13671564b287SLydia Wang 	*ucontrol->value.integer.value = on;
13681564b287SLydia Wang 	return 0;
13691564b287SLydia Wang }
13701564b287SLydia Wang 
13711564b287SLydia Wang static int via_smart51_put(struct snd_kcontrol *kcontrol,
13721564b287SLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
13731564b287SLydia Wang {
13741564b287SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
13751564b287SLydia Wang 	struct via_spec *spec = codec->spec;
13767b315bb4STakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
13771564b287SLydia Wang 	int out_in = *ucontrol->value.integer.value
13781564b287SLydia Wang 		? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
13791564b287SLydia Wang 	int i;
13801564b287SLydia Wang 
13817b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
13827b315bb4STakashi Iwai 		hda_nid_t nid = cfg->inputs[i].pin;
13837b315bb4STakashi Iwai 		unsigned int parm;
13847b315bb4STakashi Iwai 
138586e2959aSTakashi Iwai 		if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
13867b315bb4STakashi Iwai 			continue;
138786e2959aSTakashi Iwai 		if (cfg->inputs[i].type == AUTO_PIN_MIC &&
13887b315bb4STakashi Iwai 		    spec->hp_independent_mode && spec->codec_type != VT1718S)
13891564b287SLydia Wang 			continue; /* don't retask FMic for independent HP */
13907b315bb4STakashi Iwai 
13917b315bb4STakashi Iwai 		parm = snd_hda_codec_read(codec, nid, 0,
13921564b287SLydia Wang 					  AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
13931564b287SLydia Wang 		parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
13941564b287SLydia Wang 		parm |= out_in;
13951564b287SLydia Wang 		snd_hda_codec_write(codec, nid, 0,
13961564b287SLydia Wang 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
13971564b287SLydia Wang 				    parm);
13981564b287SLydia Wang 		if (out_in == AC_PINCTL_OUT_EN) {
13991564b287SLydia Wang 			mute_aa_path(codec, 1);
14001564b287SLydia Wang 			notify_aa_path_ctls(codec);
14011564b287SLydia Wang 		}
14027b315bb4STakashi Iwai 		if (spec->codec_type == VT1718S) {
1403eb7188caSLydia Wang 			snd_hda_codec_amp_stereo(
1404eb7188caSLydia Wang 					codec, nid, HDA_OUTPUT, 0, HDA_AMP_MUTE,
1405eb7188caSLydia Wang 					HDA_AMP_UNMUTE);
14061564b287SLydia Wang 		}
140786e2959aSTakashi Iwai 		if (cfg->inputs[i].type == AUTO_PIN_MIC) {
1408f3db423dSLydia Wang 			if (spec->codec_type == VT1708S
1409f3db423dSLydia Wang 			    || spec->codec_type == VT1716S) {
14101564b287SLydia Wang 				/* input = index 1 (AOW3) */
14111564b287SLydia Wang 				snd_hda_codec_write(
14121564b287SLydia Wang 					codec, nid, 0,
14131564b287SLydia Wang 					AC_VERB_SET_CONNECT_SEL, 1);
14141564b287SLydia Wang 				snd_hda_codec_amp_stereo(
14151564b287SLydia Wang 					codec, nid, HDA_OUTPUT,
14161564b287SLydia Wang 					0, HDA_AMP_MUTE, HDA_AMP_UNMUTE);
14171564b287SLydia Wang 			}
14181564b287SLydia Wang 		}
14191564b287SLydia Wang 	}
14201564b287SLydia Wang 	spec->smart51_enabled = *ucontrol->value.integer.value;
14211564b287SLydia Wang 	set_jack_power_state(codec);
14221564b287SLydia Wang 	return 1;
14231564b287SLydia Wang }
14241564b287SLydia Wang 
14255b0cb1d8SJaroslav Kysela static struct snd_kcontrol_new via_smart51_mixer[2] = {
14261564b287SLydia Wang 	{
14271564b287SLydia Wang 	 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
14281564b287SLydia Wang 	 .name = "Smart 5.1",
14291564b287SLydia Wang 	 .count = 1,
14301564b287SLydia Wang 	 .info = via_smart51_info,
14311564b287SLydia Wang 	 .get = via_smart51_get,
14321564b287SLydia Wang 	 .put = via_smart51_put,
14331564b287SLydia Wang 	 },
14345b0cb1d8SJaroslav Kysela 	{
14355b0cb1d8SJaroslav Kysela 	 .iface = NID_MAPPING,
14365b0cb1d8SJaroslav Kysela 	 .name = "Smart 5.1",
14375b0cb1d8SJaroslav Kysela 	}
14381564b287SLydia Wang };
14391564b287SLydia Wang 
14405b0cb1d8SJaroslav Kysela static int via_smart51_build(struct via_spec *spec)
14415b0cb1d8SJaroslav Kysela {
14425b0cb1d8SJaroslav Kysela 	struct snd_kcontrol_new *knew;
14437b315bb4STakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
14445b0cb1d8SJaroslav Kysela 	hda_nid_t nid;
14455b0cb1d8SJaroslav Kysela 	int i;
14465b0cb1d8SJaroslav Kysela 
14475b0cb1d8SJaroslav Kysela 	knew = via_clone_control(spec, &via_smart51_mixer[0]);
14485b0cb1d8SJaroslav Kysela 	if (knew == NULL)
14495b0cb1d8SJaroslav Kysela 		return -ENOMEM;
14505b0cb1d8SJaroslav Kysela 
14517b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
14527b315bb4STakashi Iwai 		nid = cfg->inputs[i].pin;
145386e2959aSTakashi Iwai 		if (cfg->inputs[i].type <= AUTO_PIN_LINE_IN) {
14545b0cb1d8SJaroslav Kysela 			knew = via_clone_control(spec, &via_smart51_mixer[1]);
14555b0cb1d8SJaroslav Kysela 			if (knew == NULL)
14565b0cb1d8SJaroslav Kysela 				return -ENOMEM;
14575b0cb1d8SJaroslav Kysela 			knew->subdevice = nid;
14587b315bb4STakashi Iwai 			break;
14595b0cb1d8SJaroslav Kysela 		}
14605b0cb1d8SJaroslav Kysela 	}
14615b0cb1d8SJaroslav Kysela 
14625b0cb1d8SJaroslav Kysela 	return 0;
14635b0cb1d8SJaroslav Kysela }
14645b0cb1d8SJaroslav Kysela 
1465c577b8a1SJoseph Chan /* capture mixer elements */
1466c577b8a1SJoseph Chan static struct snd_kcontrol_new vt1708_capture_mixer[] = {
1467c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_INPUT),
1468c577b8a1SJoseph Chan 	HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_INPUT),
1469c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x27, 0x0, HDA_INPUT),
1470c577b8a1SJoseph Chan 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x27, 0x0, HDA_INPUT),
1471c577b8a1SJoseph Chan 	{
1472c577b8a1SJoseph Chan 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1473c577b8a1SJoseph Chan 		/* The multiple "Capture Source" controls confuse alsamixer
1474c577b8a1SJoseph Chan 		 * So call somewhat different..
1475c577b8a1SJoseph Chan 		 */
1476c577b8a1SJoseph Chan 		/* .name = "Capture Source", */
1477c577b8a1SJoseph Chan 		.name = "Input Source",
1478c577b8a1SJoseph Chan 		.count = 1,
1479c577b8a1SJoseph Chan 		.info = via_mux_enum_info,
1480c577b8a1SJoseph Chan 		.get = via_mux_enum_get,
1481c577b8a1SJoseph Chan 		.put = via_mux_enum_put,
1482c577b8a1SJoseph Chan 	},
1483c577b8a1SJoseph Chan 	{ } /* end */
1484c577b8a1SJoseph Chan };
1485f5271101SLydia Wang 
1486f5271101SLydia Wang /* check AA path's mute statue */
1487f5271101SLydia Wang static int is_aa_path_mute(struct hda_codec *codec)
1488f5271101SLydia Wang {
1489f5271101SLydia Wang 	int mute = 1;
1490f5271101SLydia Wang 	hda_nid_t  nid_mixer;
1491f5271101SLydia Wang 	int start_idx;
1492f5271101SLydia Wang 	int end_idx;
1493f5271101SLydia Wang 	int i;
1494f5271101SLydia Wang 	struct via_spec *spec = codec->spec;
1495f5271101SLydia Wang 	/* get nid of MW0 and start & end index */
1496f5271101SLydia Wang 	switch (spec->codec_type) {
1497f5271101SLydia Wang 	case VT1708B_8CH:
1498f5271101SLydia Wang 	case VT1708B_4CH:
1499f5271101SLydia Wang 	case VT1708S:
1500f3db423dSLydia Wang 	case VT1716S:
1501f5271101SLydia Wang 		nid_mixer = 0x16;
1502f5271101SLydia Wang 		start_idx = 2;
1503f5271101SLydia Wang 		end_idx = 4;
1504f5271101SLydia Wang 		break;
1505f5271101SLydia Wang 	case VT1702:
1506f5271101SLydia Wang 		nid_mixer = 0x1a;
1507f5271101SLydia Wang 		start_idx = 1;
1508f5271101SLydia Wang 		end_idx = 3;
1509f5271101SLydia Wang 		break;
1510eb7188caSLydia Wang 	case VT1718S:
1511eb7188caSLydia Wang 		nid_mixer = 0x21;
1512eb7188caSLydia Wang 		start_idx = 1;
1513eb7188caSLydia Wang 		end_idx = 3;
1514eb7188caSLydia Wang 		break;
151525eaba2fSLydia Wang 	case VT2002P:
1516ab6734e7SLydia Wang 	case VT1812:
151725eaba2fSLydia Wang 		nid_mixer = 0x21;
151825eaba2fSLydia Wang 		start_idx = 0;
151925eaba2fSLydia Wang 		end_idx = 2;
152025eaba2fSLydia Wang 		break;
1521f5271101SLydia Wang 	default:
1522f5271101SLydia Wang 		return 0;
1523f5271101SLydia Wang 	}
1524f5271101SLydia Wang 	/* check AA path's mute status */
1525f5271101SLydia Wang 	for (i = start_idx; i <= end_idx; i++) {
1526f5271101SLydia Wang 		unsigned int con_list = snd_hda_codec_read(
1527f5271101SLydia Wang 			codec, nid_mixer, 0, AC_VERB_GET_CONNECT_LIST, i/4*4);
1528f5271101SLydia Wang 		int shift = 8 * (i % 4);
1529f5271101SLydia Wang 		hda_nid_t nid_pin = (con_list & (0xff << shift)) >> shift;
1530f5271101SLydia Wang 		unsigned int defconf = snd_hda_codec_get_pincfg(codec, nid_pin);
1531f5271101SLydia Wang 		if (get_defcfg_connect(defconf) == AC_JACK_PORT_COMPLEX) {
1532f5271101SLydia Wang 			/* check mute status while the pin is connected */
1533f5271101SLydia Wang 			int mute_l = snd_hda_codec_amp_read(codec, nid_mixer, 0,
1534f5271101SLydia Wang 							    HDA_INPUT, i) >> 7;
1535f5271101SLydia Wang 			int mute_r = snd_hda_codec_amp_read(codec, nid_mixer, 1,
1536f5271101SLydia Wang 							    HDA_INPUT, i) >> 7;
1537f5271101SLydia Wang 			if (!mute_l || !mute_r) {
1538f5271101SLydia Wang 				mute = 0;
1539f5271101SLydia Wang 				break;
1540f5271101SLydia Wang 			}
1541f5271101SLydia Wang 		}
1542f5271101SLydia Wang 	}
1543f5271101SLydia Wang 	return mute;
1544f5271101SLydia Wang }
1545f5271101SLydia Wang 
1546f5271101SLydia Wang /* enter/exit analog low-current mode */
1547f5271101SLydia Wang static void analog_low_current_mode(struct hda_codec *codec, int stream_idle)
1548f5271101SLydia Wang {
1549f5271101SLydia Wang 	struct via_spec *spec = codec->spec;
1550f5271101SLydia Wang 	static int saved_stream_idle = 1; /* saved stream idle status */
1551f5271101SLydia Wang 	int enable = is_aa_path_mute(codec);
1552f5271101SLydia Wang 	unsigned int verb = 0;
1553f5271101SLydia Wang 	unsigned int parm = 0;
1554f5271101SLydia Wang 
1555f5271101SLydia Wang 	if (stream_idle == -1)	/* stream status did not change */
1556f5271101SLydia Wang 		enable = enable && saved_stream_idle;
1557f5271101SLydia Wang 	else {
1558f5271101SLydia Wang 		enable = enable && stream_idle;
1559f5271101SLydia Wang 		saved_stream_idle = stream_idle;
1560f5271101SLydia Wang 	}
1561f5271101SLydia Wang 
1562f5271101SLydia Wang 	/* decide low current mode's verb & parameter */
1563f5271101SLydia Wang 	switch (spec->codec_type) {
1564f5271101SLydia Wang 	case VT1708B_8CH:
1565f5271101SLydia Wang 	case VT1708B_4CH:
1566f5271101SLydia Wang 		verb = 0xf70;
1567f5271101SLydia Wang 		parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
1568f5271101SLydia Wang 		break;
1569f5271101SLydia Wang 	case VT1708S:
1570eb7188caSLydia Wang 	case VT1718S:
1571f3db423dSLydia Wang 	case VT1716S:
1572f5271101SLydia Wang 		verb = 0xf73;
1573f5271101SLydia Wang 		parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
1574f5271101SLydia Wang 		break;
1575f5271101SLydia Wang 	case VT1702:
1576f5271101SLydia Wang 		verb = 0xf73;
1577f5271101SLydia Wang 		parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
1578f5271101SLydia Wang 		break;
157925eaba2fSLydia Wang 	case VT2002P:
1580ab6734e7SLydia Wang 	case VT1812:
158125eaba2fSLydia Wang 		verb = 0xf93;
158225eaba2fSLydia Wang 		parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
158325eaba2fSLydia Wang 		break;
1584f5271101SLydia Wang 	default:
1585f5271101SLydia Wang 		return;		/* other codecs are not supported */
1586f5271101SLydia Wang 	}
1587f5271101SLydia Wang 	/* send verb */
1588f5271101SLydia Wang 	snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
1589f5271101SLydia Wang }
1590f5271101SLydia Wang 
1591c577b8a1SJoseph Chan /*
1592c577b8a1SJoseph Chan  * generic initialization of ADC, input mixers and output mixers
1593c577b8a1SJoseph Chan  */
1594c577b8a1SJoseph Chan static struct hda_verb vt1708_volume_init_verbs[] = {
1595c577b8a1SJoseph Chan 	/*
1596c577b8a1SJoseph Chan 	 * Unmute ADC0-1 and set the default input to mic-in
1597c577b8a1SJoseph Chan 	 */
1598c577b8a1SJoseph Chan 	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1599c577b8a1SJoseph Chan 	{0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1600c577b8a1SJoseph Chan 
1601c577b8a1SJoseph Chan 
1602f7278fd0SJosepch Chan 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
1603c577b8a1SJoseph Chan 	 * mixer widget
1604c577b8a1SJoseph Chan 	 */
1605c577b8a1SJoseph Chan 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
1606f7278fd0SJosepch Chan 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1607f7278fd0SJosepch Chan 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1608f7278fd0SJosepch Chan 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
1609f7278fd0SJosepch Chan 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
1610f7278fd0SJosepch Chan 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
1611c577b8a1SJoseph Chan 
1612c577b8a1SJoseph Chan 	/*
1613c577b8a1SJoseph Chan 	 * Set up output mixers (0x19 - 0x1b)
1614c577b8a1SJoseph Chan 	 */
1615c577b8a1SJoseph Chan 	/* set vol=0 to output mixers */
1616c577b8a1SJoseph Chan 	{0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1617c577b8a1SJoseph Chan 	{0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1618c577b8a1SJoseph Chan 	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1619c577b8a1SJoseph Chan 
1620bfdc675aSLydia Wang 	/* Setup default input MW0 to PW4 */
1621bfdc675aSLydia Wang 	{0x20, AC_VERB_SET_CONNECT_SEL, 0},
1622c577b8a1SJoseph Chan 	/* PW9 Output enable */
1623c577b8a1SJoseph Chan 	{0x25, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
1624f7278fd0SJosepch Chan 	{ }
1625c577b8a1SJoseph Chan };
1626c577b8a1SJoseph Chan 
1627c577b8a1SJoseph Chan static int via_playback_pcm_open(struct hda_pcm_stream *hinfo,
1628c577b8a1SJoseph Chan 				 struct hda_codec *codec,
1629c577b8a1SJoseph Chan 				 struct snd_pcm_substream *substream)
1630c577b8a1SJoseph Chan {
1631c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
163217314379SLydia Wang 	int idle = substream->pstr->substream_opened == 1
163317314379SLydia Wang 		&& substream->ref_count == 0;
163417314379SLydia Wang 	analog_low_current_mode(codec, idle);
16359a08160bSTakashi Iwai 	return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
16369a08160bSTakashi Iwai 					     hinfo);
1637c577b8a1SJoseph Chan }
1638c577b8a1SJoseph Chan 
16390aa62aefSHarald Welte static void playback_multi_pcm_prep_0(struct hda_codec *codec,
16400aa62aefSHarald Welte 				      unsigned int stream_tag,
16410aa62aefSHarald Welte 				      unsigned int format,
16420aa62aefSHarald Welte 				      struct snd_pcm_substream *substream)
16430aa62aefSHarald Welte {
16440aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
16450aa62aefSHarald Welte 	struct hda_multi_out *mout = &spec->multiout;
16460aa62aefSHarald Welte 	hda_nid_t *nids = mout->dac_nids;
16470aa62aefSHarald Welte 	int chs = substream->runtime->channels;
16480aa62aefSHarald Welte 	int i;
16490aa62aefSHarald Welte 
16500aa62aefSHarald Welte 	mutex_lock(&codec->spdif_mutex);
16510aa62aefSHarald Welte 	if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
16520aa62aefSHarald Welte 		if (chs == 2 &&
16530aa62aefSHarald Welte 		    snd_hda_is_supported_format(codec, mout->dig_out_nid,
16540aa62aefSHarald Welte 						format) &&
16550aa62aefSHarald Welte 		    !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
16560aa62aefSHarald Welte 			mout->dig_out_used = HDA_DIG_ANALOG_DUP;
16570aa62aefSHarald Welte 			/* turn off SPDIF once; otherwise the IEC958 bits won't
16580aa62aefSHarald Welte 			 * be updated */
16590aa62aefSHarald Welte 			if (codec->spdif_ctls & AC_DIG1_ENABLE)
16600aa62aefSHarald Welte 				snd_hda_codec_write(codec, mout->dig_out_nid, 0,
16610aa62aefSHarald Welte 						    AC_VERB_SET_DIGI_CONVERT_1,
16620aa62aefSHarald Welte 						    codec->spdif_ctls &
16630aa62aefSHarald Welte 							~AC_DIG1_ENABLE & 0xff);
16640aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
16650aa62aefSHarald Welte 						   stream_tag, 0, format);
16660aa62aefSHarald Welte 			/* turn on again (if needed) */
16670aa62aefSHarald Welte 			if (codec->spdif_ctls & AC_DIG1_ENABLE)
16680aa62aefSHarald Welte 				snd_hda_codec_write(codec, mout->dig_out_nid, 0,
16690aa62aefSHarald Welte 						    AC_VERB_SET_DIGI_CONVERT_1,
16700aa62aefSHarald Welte 						    codec->spdif_ctls & 0xff);
16710aa62aefSHarald Welte 		} else {
16720aa62aefSHarald Welte 			mout->dig_out_used = 0;
16730aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
16740aa62aefSHarald Welte 						   0, 0, 0);
16750aa62aefSHarald Welte 		}
16760aa62aefSHarald Welte 	}
16770aa62aefSHarald Welte 	mutex_unlock(&codec->spdif_mutex);
16780aa62aefSHarald Welte 
16790aa62aefSHarald Welte 	/* front */
16800aa62aefSHarald Welte 	snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
16810aa62aefSHarald Welte 				   0, format);
16820aa62aefSHarald Welte 
1683eb7188caSLydia Wang 	if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]
1684eb7188caSLydia Wang 	    && !spec->hp_independent_mode)
16850aa62aefSHarald Welte 		/* headphone out will just decode front left/right (stereo) */
16860aa62aefSHarald Welte 		snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
16870aa62aefSHarald Welte 					   0, format);
16880aa62aefSHarald Welte 
16890aa62aefSHarald Welte 	/* extra outputs copied from front */
16900aa62aefSHarald Welte 	for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
16910aa62aefSHarald Welte 		if (mout->extra_out_nid[i])
16920aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec,
16930aa62aefSHarald Welte 						   mout->extra_out_nid[i],
16940aa62aefSHarald Welte 						   stream_tag, 0, format);
16950aa62aefSHarald Welte 
16960aa62aefSHarald Welte 	/* surrounds */
16970aa62aefSHarald Welte 	for (i = 1; i < mout->num_dacs; i++) {
16980aa62aefSHarald Welte 		if (chs >= (i + 1) * 2) /* independent out */
16990aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
17000aa62aefSHarald Welte 						   i * 2, format);
17010aa62aefSHarald Welte 		else /* copy front */
17020aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
17030aa62aefSHarald Welte 						   0, format);
17040aa62aefSHarald Welte 	}
17050aa62aefSHarald Welte }
17060aa62aefSHarald Welte 
17070aa62aefSHarald Welte static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
17080aa62aefSHarald Welte 					  struct hda_codec *codec,
17090aa62aefSHarald Welte 					  unsigned int stream_tag,
17100aa62aefSHarald Welte 					  unsigned int format,
17110aa62aefSHarald Welte 					  struct snd_pcm_substream *substream)
17120aa62aefSHarald Welte {
17130aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
17140aa62aefSHarald Welte 	struct hda_multi_out *mout = &spec->multiout;
17150aa62aefSHarald Welte 	hda_nid_t *nids = mout->dac_nids;
17160aa62aefSHarald Welte 
17170aa62aefSHarald Welte 	if (substream->number == 0)
17180aa62aefSHarald Welte 		playback_multi_pcm_prep_0(codec, stream_tag, format,
17190aa62aefSHarald Welte 					  substream);
17200aa62aefSHarald Welte 	else {
17210aa62aefSHarald Welte 		if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
17220aa62aefSHarald Welte 		    spec->hp_independent_mode)
17230aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, mout->hp_nid,
17240aa62aefSHarald Welte 						   stream_tag, 0, format);
17250aa62aefSHarald Welte 	}
17261f2e99feSLydia Wang 	vt1708_start_hp_work(spec);
17270aa62aefSHarald Welte 	return 0;
17280aa62aefSHarald Welte }
17290aa62aefSHarald Welte 
17300aa62aefSHarald Welte static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
17310aa62aefSHarald Welte 				    struct hda_codec *codec,
17320aa62aefSHarald Welte 				    struct snd_pcm_substream *substream)
17330aa62aefSHarald Welte {
17340aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
17350aa62aefSHarald Welte 	struct hda_multi_out *mout = &spec->multiout;
17360aa62aefSHarald Welte 	hda_nid_t *nids = mout->dac_nids;
17370aa62aefSHarald Welte 	int i;
17380aa62aefSHarald Welte 
17390aa62aefSHarald Welte 	if (substream->number == 0) {
17400aa62aefSHarald Welte 		for (i = 0; i < mout->num_dacs; i++)
17410aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
17420aa62aefSHarald Welte 
17430aa62aefSHarald Welte 		if (mout->hp_nid && !spec->hp_independent_mode)
17440aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, mout->hp_nid,
17450aa62aefSHarald Welte 						   0, 0, 0);
17460aa62aefSHarald Welte 
17470aa62aefSHarald Welte 		for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
17480aa62aefSHarald Welte 			if (mout->extra_out_nid[i])
17490aa62aefSHarald Welte 				snd_hda_codec_setup_stream(codec,
17500aa62aefSHarald Welte 							mout->extra_out_nid[i],
17510aa62aefSHarald Welte 							0, 0, 0);
17520aa62aefSHarald Welte 		mutex_lock(&codec->spdif_mutex);
17530aa62aefSHarald Welte 		if (mout->dig_out_nid &&
17540aa62aefSHarald Welte 		    mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
17550aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
17560aa62aefSHarald Welte 						   0, 0, 0);
17570aa62aefSHarald Welte 			mout->dig_out_used = 0;
17580aa62aefSHarald Welte 		}
17590aa62aefSHarald Welte 		mutex_unlock(&codec->spdif_mutex);
17600aa62aefSHarald Welte 	} else {
17610aa62aefSHarald Welte 		if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
17620aa62aefSHarald Welte 		    spec->hp_independent_mode)
17630aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, mout->hp_nid,
17640aa62aefSHarald Welte 						   0, 0, 0);
17650aa62aefSHarald Welte 	}
17661f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
17670aa62aefSHarald Welte 	return 0;
17680aa62aefSHarald Welte }
17690aa62aefSHarald Welte 
1770c577b8a1SJoseph Chan /*
1771c577b8a1SJoseph Chan  * Digital out
1772c577b8a1SJoseph Chan  */
1773c577b8a1SJoseph Chan static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1774c577b8a1SJoseph Chan 				     struct hda_codec *codec,
1775c577b8a1SJoseph Chan 				     struct snd_pcm_substream *substream)
1776c577b8a1SJoseph Chan {
1777c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1778c577b8a1SJoseph Chan 	return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1779c577b8a1SJoseph Chan }
1780c577b8a1SJoseph Chan 
1781c577b8a1SJoseph Chan static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1782c577b8a1SJoseph Chan 				      struct hda_codec *codec,
1783c577b8a1SJoseph Chan 				      struct snd_pcm_substream *substream)
1784c577b8a1SJoseph Chan {
1785c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1786c577b8a1SJoseph Chan 	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1787c577b8a1SJoseph Chan }
1788c577b8a1SJoseph Chan 
17895691ec7fSHarald Welte static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
179098aa34c0SHarald Welte 					struct hda_codec *codec,
179198aa34c0SHarald Welte 					unsigned int stream_tag,
179298aa34c0SHarald Welte 					unsigned int format,
179398aa34c0SHarald Welte 					struct snd_pcm_substream *substream)
179498aa34c0SHarald Welte {
179598aa34c0SHarald Welte 	struct via_spec *spec = codec->spec;
17969da29271STakashi Iwai 	return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
17979da29271STakashi Iwai 					     stream_tag, format, substream);
17989da29271STakashi Iwai }
17995691ec7fSHarald Welte 
18009da29271STakashi Iwai static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
18019da29271STakashi Iwai 					struct hda_codec *codec,
18029da29271STakashi Iwai 					struct snd_pcm_substream *substream)
18039da29271STakashi Iwai {
18049da29271STakashi Iwai 	struct via_spec *spec = codec->spec;
18059da29271STakashi Iwai 	snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
180698aa34c0SHarald Welte 	return 0;
180798aa34c0SHarald Welte }
180898aa34c0SHarald Welte 
1809c577b8a1SJoseph Chan /*
1810c577b8a1SJoseph Chan  * Analog capture
1811c577b8a1SJoseph Chan  */
1812c577b8a1SJoseph Chan static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1813c577b8a1SJoseph Chan 				   struct hda_codec *codec,
1814c577b8a1SJoseph Chan 				   unsigned int stream_tag,
1815c577b8a1SJoseph Chan 				   unsigned int format,
1816c577b8a1SJoseph Chan 				   struct snd_pcm_substream *substream)
1817c577b8a1SJoseph Chan {
1818c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1819c577b8a1SJoseph Chan 
1820c577b8a1SJoseph Chan 	snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1821c577b8a1SJoseph Chan 				   stream_tag, 0, format);
1822c577b8a1SJoseph Chan 	return 0;
1823c577b8a1SJoseph Chan }
1824c577b8a1SJoseph Chan 
1825c577b8a1SJoseph Chan static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1826c577b8a1SJoseph Chan 				   struct hda_codec *codec,
1827c577b8a1SJoseph Chan 				   struct snd_pcm_substream *substream)
1828c577b8a1SJoseph Chan {
1829c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1830888afa15STakashi Iwai 	snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
1831c577b8a1SJoseph Chan 	return 0;
1832c577b8a1SJoseph Chan }
1833c577b8a1SJoseph Chan 
1834c577b8a1SJoseph Chan static struct hda_pcm_stream vt1708_pcm_analog_playback = {
18350aa62aefSHarald Welte 	.substreams = 2,
1836c577b8a1SJoseph Chan 	.channels_min = 2,
1837c577b8a1SJoseph Chan 	.channels_max = 8,
1838c577b8a1SJoseph Chan 	.nid = 0x10, /* NID to query formats and rates */
1839c577b8a1SJoseph Chan 	.ops = {
1840c577b8a1SJoseph Chan 		.open = via_playback_pcm_open,
18410aa62aefSHarald Welte 		.prepare = via_playback_multi_pcm_prepare,
18420aa62aefSHarald Welte 		.cleanup = via_playback_multi_pcm_cleanup
1843c577b8a1SJoseph Chan 	},
1844c577b8a1SJoseph Chan };
1845c577b8a1SJoseph Chan 
1846bc9b5623STakashi Iwai static struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
1847c873cc25SLydia Wang 	.substreams = 2,
1848bc9b5623STakashi Iwai 	.channels_min = 2,
1849bc9b5623STakashi Iwai 	.channels_max = 8,
1850bc9b5623STakashi Iwai 	.nid = 0x10, /* NID to query formats and rates */
1851bc9b5623STakashi Iwai 	/* We got noisy outputs on the right channel on VT1708 when
1852bc9b5623STakashi Iwai 	 * 24bit samples are used.  Until any workaround is found,
1853bc9b5623STakashi Iwai 	 * disable the 24bit format, so far.
1854bc9b5623STakashi Iwai 	 */
1855bc9b5623STakashi Iwai 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
1856bc9b5623STakashi Iwai 	.ops = {
1857bc9b5623STakashi Iwai 		.open = via_playback_pcm_open,
1858c873cc25SLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
1859c873cc25SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup
1860bc9b5623STakashi Iwai 	},
1861bc9b5623STakashi Iwai };
1862bc9b5623STakashi Iwai 
1863c577b8a1SJoseph Chan static struct hda_pcm_stream vt1708_pcm_analog_capture = {
1864c577b8a1SJoseph Chan 	.substreams = 2,
1865c577b8a1SJoseph Chan 	.channels_min = 2,
1866c577b8a1SJoseph Chan 	.channels_max = 2,
1867c577b8a1SJoseph Chan 	.nid = 0x15, /* NID to query formats and rates */
1868c577b8a1SJoseph Chan 	.ops = {
1869c577b8a1SJoseph Chan 		.prepare = via_capture_pcm_prepare,
1870c577b8a1SJoseph Chan 		.cleanup = via_capture_pcm_cleanup
1871c577b8a1SJoseph Chan 	},
1872c577b8a1SJoseph Chan };
1873c577b8a1SJoseph Chan 
1874c577b8a1SJoseph Chan static struct hda_pcm_stream vt1708_pcm_digital_playback = {
1875c577b8a1SJoseph Chan 	.substreams = 1,
1876c577b8a1SJoseph Chan 	.channels_min = 2,
1877c577b8a1SJoseph Chan 	.channels_max = 2,
1878c577b8a1SJoseph Chan 	/* NID is set in via_build_pcms */
1879c577b8a1SJoseph Chan 	.ops = {
1880c577b8a1SJoseph Chan 		.open = via_dig_playback_pcm_open,
18816b97eb45STakashi Iwai 		.close = via_dig_playback_pcm_close,
18829da29271STakashi Iwai 		.prepare = via_dig_playback_pcm_prepare,
18839da29271STakashi Iwai 		.cleanup = via_dig_playback_pcm_cleanup
1884c577b8a1SJoseph Chan 	},
1885c577b8a1SJoseph Chan };
1886c577b8a1SJoseph Chan 
1887c577b8a1SJoseph Chan static struct hda_pcm_stream vt1708_pcm_digital_capture = {
1888c577b8a1SJoseph Chan 	.substreams = 1,
1889c577b8a1SJoseph Chan 	.channels_min = 2,
1890c577b8a1SJoseph Chan 	.channels_max = 2,
1891c577b8a1SJoseph Chan };
1892c577b8a1SJoseph Chan 
1893c577b8a1SJoseph Chan static int via_build_controls(struct hda_codec *codec)
1894c577b8a1SJoseph Chan {
1895c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
18965b0cb1d8SJaroslav Kysela 	struct snd_kcontrol *kctl;
18975b0cb1d8SJaroslav Kysela 	struct snd_kcontrol_new *knew;
18985b0cb1d8SJaroslav Kysela 	int err, i;
1899c577b8a1SJoseph Chan 
1900c577b8a1SJoseph Chan 	for (i = 0; i < spec->num_mixers; i++) {
1901c577b8a1SJoseph Chan 		err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1902c577b8a1SJoseph Chan 		if (err < 0)
1903c577b8a1SJoseph Chan 			return err;
1904c577b8a1SJoseph Chan 	}
1905c577b8a1SJoseph Chan 
1906c577b8a1SJoseph Chan 	if (spec->multiout.dig_out_nid) {
1907c577b8a1SJoseph Chan 		err = snd_hda_create_spdif_out_ctls(codec,
1908c577b8a1SJoseph Chan 						    spec->multiout.dig_out_nid);
1909c577b8a1SJoseph Chan 		if (err < 0)
1910c577b8a1SJoseph Chan 			return err;
19119a08160bSTakashi Iwai 		err = snd_hda_create_spdif_share_sw(codec,
19129a08160bSTakashi Iwai 						    &spec->multiout);
19139a08160bSTakashi Iwai 		if (err < 0)
19149a08160bSTakashi Iwai 			return err;
19159a08160bSTakashi Iwai 		spec->multiout.share_spdif = 1;
1916c577b8a1SJoseph Chan 	}
1917c577b8a1SJoseph Chan 	if (spec->dig_in_nid) {
1918c577b8a1SJoseph Chan 		err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1919c577b8a1SJoseph Chan 		if (err < 0)
1920c577b8a1SJoseph Chan 			return err;
1921c577b8a1SJoseph Chan 	}
192217314379SLydia Wang 
19235b0cb1d8SJaroslav Kysela 	/* assign Capture Source enums to NID */
19245b0cb1d8SJaroslav Kysela 	kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
19255b0cb1d8SJaroslav Kysela 	for (i = 0; kctl && i < kctl->count; i++) {
192621949f00STakashi Iwai 		err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
19275b0cb1d8SJaroslav Kysela 		if (err < 0)
19285b0cb1d8SJaroslav Kysela 			return err;
19295b0cb1d8SJaroslav Kysela 	}
19305b0cb1d8SJaroslav Kysela 
19315b0cb1d8SJaroslav Kysela 	/* other nid->control mapping */
19325b0cb1d8SJaroslav Kysela 	for (i = 0; i < spec->num_mixers; i++) {
19335b0cb1d8SJaroslav Kysela 		for (knew = spec->mixers[i]; knew->name; knew++) {
19345b0cb1d8SJaroslav Kysela 			if (knew->iface != NID_MAPPING)
19355b0cb1d8SJaroslav Kysela 				continue;
19365b0cb1d8SJaroslav Kysela 			kctl = snd_hda_find_mixer_ctl(codec, knew->name);
19375b0cb1d8SJaroslav Kysela 			if (kctl == NULL)
19385b0cb1d8SJaroslav Kysela 				continue;
19395b0cb1d8SJaroslav Kysela 			err = snd_hda_add_nid(codec, kctl, 0,
19405b0cb1d8SJaroslav Kysela 					      knew->subdevice);
19415b0cb1d8SJaroslav Kysela 		}
19425b0cb1d8SJaroslav Kysela 	}
19435b0cb1d8SJaroslav Kysela 
194417314379SLydia Wang 	/* init power states */
194517314379SLydia Wang 	set_jack_power_state(codec);
194617314379SLydia Wang 	analog_low_current_mode(codec, 1);
194717314379SLydia Wang 
1948603c4019STakashi Iwai 	via_free_kctls(codec); /* no longer needed */
1949c577b8a1SJoseph Chan 	return 0;
1950c577b8a1SJoseph Chan }
1951c577b8a1SJoseph Chan 
1952c577b8a1SJoseph Chan static int via_build_pcms(struct hda_codec *codec)
1953c577b8a1SJoseph Chan {
1954c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1955c577b8a1SJoseph Chan 	struct hda_pcm *info = spec->pcm_rec;
1956c577b8a1SJoseph Chan 
1957c577b8a1SJoseph Chan 	codec->num_pcms = 1;
1958c577b8a1SJoseph Chan 	codec->pcm_info = info;
1959c577b8a1SJoseph Chan 
1960c577b8a1SJoseph Chan 	info->name = spec->stream_name_analog;
1961377ff31aSLydia Wang 	info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1962377ff31aSLydia Wang 		*(spec->stream_analog_playback);
1963377ff31aSLydia Wang 	info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1964377ff31aSLydia Wang 		spec->multiout.dac_nids[0];
1965c577b8a1SJoseph Chan 	info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_analog_capture);
1966c577b8a1SJoseph Chan 	info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
1967c577b8a1SJoseph Chan 
1968c577b8a1SJoseph Chan 	info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1969c577b8a1SJoseph Chan 		spec->multiout.max_channels;
1970c577b8a1SJoseph Chan 
1971c577b8a1SJoseph Chan 	if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1972c577b8a1SJoseph Chan 		codec->num_pcms++;
1973c577b8a1SJoseph Chan 		info++;
1974c577b8a1SJoseph Chan 		info->name = spec->stream_name_digital;
19757ba72ba1STakashi Iwai 		info->pcm_type = HDA_PCM_TYPE_SPDIF;
1976c577b8a1SJoseph Chan 		if (spec->multiout.dig_out_nid) {
1977c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1978c577b8a1SJoseph Chan 				*(spec->stream_digital_playback);
1979c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1980c577b8a1SJoseph Chan 				spec->multiout.dig_out_nid;
1981c577b8a1SJoseph Chan 		}
1982c577b8a1SJoseph Chan 		if (spec->dig_in_nid) {
1983c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1984c577b8a1SJoseph Chan 				*(spec->stream_digital_capture);
1985c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1986c577b8a1SJoseph Chan 				spec->dig_in_nid;
1987c577b8a1SJoseph Chan 		}
1988c577b8a1SJoseph Chan 	}
1989c577b8a1SJoseph Chan 
1990c577b8a1SJoseph Chan 	return 0;
1991c577b8a1SJoseph Chan }
1992c577b8a1SJoseph Chan 
1993c577b8a1SJoseph Chan static void via_free(struct hda_codec *codec)
1994c577b8a1SJoseph Chan {
1995c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1996c577b8a1SJoseph Chan 
1997c577b8a1SJoseph Chan 	if (!spec)
1998c577b8a1SJoseph Chan 		return;
1999c577b8a1SJoseph Chan 
2000603c4019STakashi Iwai 	via_free_kctls(codec);
20011f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
2002c577b8a1SJoseph Chan 	kfree(codec->spec);
2003c577b8a1SJoseph Chan }
2004c577b8a1SJoseph Chan 
200569e52a80SHarald Welte /* mute internal speaker if HP is plugged */
200669e52a80SHarald Welte static void via_hp_automute(struct hda_codec *codec)
200769e52a80SHarald Welte {
2008dcf34c8cSLydia Wang 	unsigned int present = 0;
200969e52a80SHarald Welte 	struct via_spec *spec = codec->spec;
201069e52a80SHarald Welte 
2011d56757abSTakashi Iwai 	present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
2012dcf34c8cSLydia Wang 
2013dcf34c8cSLydia Wang 	if (!spec->hp_independent_mode) {
2014dcf34c8cSLydia Wang 		struct snd_ctl_elem_id id;
2015dcf34c8cSLydia Wang 		/* auto mute */
2016dcf34c8cSLydia Wang 		snd_hda_codec_amp_stereo(
2017dcf34c8cSLydia Wang 			codec, spec->autocfg.line_out_pins[0], HDA_OUTPUT, 0,
2018dcf34c8cSLydia Wang 			HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
2019dcf34c8cSLydia Wang 		/* notify change */
2020dcf34c8cSLydia Wang 		memset(&id, 0, sizeof(id));
2021dcf34c8cSLydia Wang 		id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2022dcf34c8cSLydia Wang 		strcpy(id.name, "Front Playback Switch");
2023dcf34c8cSLydia Wang 		snd_ctl_notify(codec->bus->card, SNDRV_CTL_EVENT_MASK_VALUE,
2024dcf34c8cSLydia Wang 			       &id);
2025dcf34c8cSLydia Wang 	}
202669e52a80SHarald Welte }
202769e52a80SHarald Welte 
2028f3db423dSLydia Wang /* mute mono out if HP or Line out is plugged */
2029f3db423dSLydia Wang static void via_mono_automute(struct hda_codec *codec)
2030f3db423dSLydia Wang {
2031f3db423dSLydia Wang 	unsigned int hp_present, lineout_present;
2032f3db423dSLydia Wang 	struct via_spec *spec = codec->spec;
2033f3db423dSLydia Wang 
2034f3db423dSLydia Wang 	if (spec->codec_type != VT1716S)
2035f3db423dSLydia Wang 		return;
2036f3db423dSLydia Wang 
2037d56757abSTakashi Iwai 	lineout_present = snd_hda_jack_detect(codec,
2038d56757abSTakashi Iwai 					      spec->autocfg.line_out_pins[0]);
2039f3db423dSLydia Wang 
2040f3db423dSLydia Wang 	/* Mute Mono Out if Line Out is plugged */
2041f3db423dSLydia Wang 	if (lineout_present) {
2042f3db423dSLydia Wang 		snd_hda_codec_amp_stereo(
2043f3db423dSLydia Wang 			codec, 0x2A, HDA_OUTPUT, 0, HDA_AMP_MUTE, HDA_AMP_MUTE);
2044f3db423dSLydia Wang 		return;
2045f3db423dSLydia Wang 	}
2046f3db423dSLydia Wang 
2047d56757abSTakashi Iwai 	hp_present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
2048f3db423dSLydia Wang 
2049f3db423dSLydia Wang 	if (!spec->hp_independent_mode)
2050f3db423dSLydia Wang 		snd_hda_codec_amp_stereo(
2051f3db423dSLydia Wang 			codec, 0x2A, HDA_OUTPUT, 0, HDA_AMP_MUTE,
2052f3db423dSLydia Wang 			hp_present ? HDA_AMP_MUTE : 0);
2053f3db423dSLydia Wang }
2054f3db423dSLydia Wang 
205569e52a80SHarald Welte static void via_gpio_control(struct hda_codec *codec)
205669e52a80SHarald Welte {
205769e52a80SHarald Welte 	unsigned int gpio_data;
205869e52a80SHarald Welte 	unsigned int vol_counter;
205969e52a80SHarald Welte 	unsigned int vol;
206069e52a80SHarald Welte 	unsigned int master_vol;
206169e52a80SHarald Welte 
206269e52a80SHarald Welte 	struct via_spec *spec = codec->spec;
206369e52a80SHarald Welte 
206469e52a80SHarald Welte 	gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
206569e52a80SHarald Welte 				       AC_VERB_GET_GPIO_DATA, 0) & 0x03;
206669e52a80SHarald Welte 
206769e52a80SHarald Welte 	vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
206869e52a80SHarald Welte 					  0xF84, 0) & 0x3F0000) >> 16;
206969e52a80SHarald Welte 
207069e52a80SHarald Welte 	vol = vol_counter & 0x1F;
207169e52a80SHarald Welte 	master_vol = snd_hda_codec_read(codec, 0x1A, 0,
207269e52a80SHarald Welte 					AC_VERB_GET_AMP_GAIN_MUTE,
207369e52a80SHarald Welte 					AC_AMP_GET_INPUT);
207469e52a80SHarald Welte 
207569e52a80SHarald Welte 	if (gpio_data == 0x02) {
207669e52a80SHarald Welte 		/* unmute line out */
207769e52a80SHarald Welte 		snd_hda_codec_amp_stereo(codec, spec->autocfg.line_out_pins[0],
207869e52a80SHarald Welte 					 HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
207969e52a80SHarald Welte 
208069e52a80SHarald Welte 		if (vol_counter & 0x20) {
208169e52a80SHarald Welte 			/* decrease volume */
208269e52a80SHarald Welte 			if (vol > master_vol)
208369e52a80SHarald Welte 				vol = master_vol;
208469e52a80SHarald Welte 			snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
208569e52a80SHarald Welte 						 0, HDA_AMP_VOLMASK,
208669e52a80SHarald Welte 						 master_vol-vol);
208769e52a80SHarald Welte 		} else {
208869e52a80SHarald Welte 			/* increase volume */
208969e52a80SHarald Welte 			snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
209069e52a80SHarald Welte 					 HDA_AMP_VOLMASK,
209169e52a80SHarald Welte 					 ((master_vol+vol) > 0x2A) ? 0x2A :
209269e52a80SHarald Welte 					  (master_vol+vol));
209369e52a80SHarald Welte 		}
209469e52a80SHarald Welte 	} else if (!(gpio_data & 0x02)) {
209569e52a80SHarald Welte 		/* mute line out */
209669e52a80SHarald Welte 		snd_hda_codec_amp_stereo(codec,
209769e52a80SHarald Welte 					 spec->autocfg.line_out_pins[0],
209869e52a80SHarald Welte 					 HDA_OUTPUT, 0, HDA_AMP_MUTE,
209969e52a80SHarald Welte 					 HDA_AMP_MUTE);
210069e52a80SHarald Welte 	}
210169e52a80SHarald Welte }
210269e52a80SHarald Welte 
210325eaba2fSLydia Wang /* mute Internal-Speaker if HP is plugged */
210425eaba2fSLydia Wang static void via_speaker_automute(struct hda_codec *codec)
210525eaba2fSLydia Wang {
210625eaba2fSLydia Wang 	unsigned int hp_present;
210725eaba2fSLydia Wang 	struct via_spec *spec = codec->spec;
210825eaba2fSLydia Wang 
2109ab6734e7SLydia Wang 	if (spec->codec_type != VT2002P && spec->codec_type != VT1812)
211025eaba2fSLydia Wang 		return;
211125eaba2fSLydia Wang 
2112d56757abSTakashi Iwai 	hp_present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
211325eaba2fSLydia Wang 
211425eaba2fSLydia Wang 	if (!spec->hp_independent_mode) {
211525eaba2fSLydia Wang 		struct snd_ctl_elem_id id;
211625eaba2fSLydia Wang 		snd_hda_codec_amp_stereo(
211725eaba2fSLydia Wang 			codec, spec->autocfg.speaker_pins[0], HDA_OUTPUT, 0,
211825eaba2fSLydia Wang 			HDA_AMP_MUTE, hp_present ? HDA_AMP_MUTE : 0);
211925eaba2fSLydia Wang 		/* notify change */
212025eaba2fSLydia Wang 		memset(&id, 0, sizeof(id));
212125eaba2fSLydia Wang 		id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
212225eaba2fSLydia Wang 		strcpy(id.name, "Speaker Playback Switch");
212325eaba2fSLydia Wang 		snd_ctl_notify(codec->bus->card, SNDRV_CTL_EVENT_MASK_VALUE,
212425eaba2fSLydia Wang 			       &id);
212525eaba2fSLydia Wang 	}
212625eaba2fSLydia Wang }
212725eaba2fSLydia Wang 
212825eaba2fSLydia Wang /* mute line-out and internal speaker if HP is plugged */
212925eaba2fSLydia Wang static void via_hp_bind_automute(struct hda_codec *codec)
213025eaba2fSLydia Wang {
213101a1796bSakpm@linux-foundation.org 	/* use long instead of int below just to avoid an internal compiler
213201a1796bSakpm@linux-foundation.org 	 * error with gcc 4.0.x
213301a1796bSakpm@linux-foundation.org 	 */
213401a1796bSakpm@linux-foundation.org 	unsigned long hp_present, present = 0;
213525eaba2fSLydia Wang 	struct via_spec *spec = codec->spec;
213625eaba2fSLydia Wang 	int i;
213725eaba2fSLydia Wang 
213825eaba2fSLydia Wang 	if (!spec->autocfg.hp_pins[0] || !spec->autocfg.line_out_pins[0])
213925eaba2fSLydia Wang 		return;
214025eaba2fSLydia Wang 
2141d56757abSTakashi Iwai 	hp_present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
214225eaba2fSLydia Wang 
2143d56757abSTakashi Iwai 	present = snd_hda_jack_detect(codec, spec->autocfg.line_out_pins[0]);
214425eaba2fSLydia Wang 
214525eaba2fSLydia Wang 	if (!spec->hp_independent_mode) {
214625eaba2fSLydia Wang 		/* Mute Line-Outs */
214725eaba2fSLydia Wang 		for (i = 0; i < spec->autocfg.line_outs; i++)
214825eaba2fSLydia Wang 			snd_hda_codec_amp_stereo(
214925eaba2fSLydia Wang 				codec, spec->autocfg.line_out_pins[i],
215025eaba2fSLydia Wang 				HDA_OUTPUT, 0,
215125eaba2fSLydia Wang 				HDA_AMP_MUTE, hp_present ? HDA_AMP_MUTE : 0);
215225eaba2fSLydia Wang 		if (hp_present)
215325eaba2fSLydia Wang 			present = hp_present;
215425eaba2fSLydia Wang 	}
215525eaba2fSLydia Wang 	/* Speakers */
215625eaba2fSLydia Wang 	for (i = 0; i < spec->autocfg.speaker_outs; i++)
215725eaba2fSLydia Wang 		snd_hda_codec_amp_stereo(
215825eaba2fSLydia Wang 			codec, spec->autocfg.speaker_pins[i], HDA_OUTPUT, 0,
215925eaba2fSLydia Wang 			HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
216025eaba2fSLydia Wang }
216125eaba2fSLydia Wang 
216225eaba2fSLydia Wang 
216369e52a80SHarald Welte /* unsolicited event for jack sensing */
216469e52a80SHarald Welte static void via_unsol_event(struct hda_codec *codec,
216569e52a80SHarald Welte 				  unsigned int res)
216669e52a80SHarald Welte {
216769e52a80SHarald Welte 	res >>= 26;
2168a34df19aSLydia Wang 	if (res & VIA_HP_EVENT)
216969e52a80SHarald Welte 		via_hp_automute(codec);
2170a34df19aSLydia Wang 	if (res & VIA_GPIO_EVENT)
217169e52a80SHarald Welte 		via_gpio_control(codec);
2172a34df19aSLydia Wang 	if (res & VIA_JACK_EVENT)
2173a34df19aSLydia Wang 		set_jack_power_state(codec);
2174f3db423dSLydia Wang 	if (res & VIA_MONO_EVENT)
2175f3db423dSLydia Wang 		via_mono_automute(codec);
217625eaba2fSLydia Wang 	if (res & VIA_SPEAKER_EVENT)
217725eaba2fSLydia Wang 		via_speaker_automute(codec);
217825eaba2fSLydia Wang 	if (res & VIA_BIND_HP_EVENT)
217925eaba2fSLydia Wang 		via_hp_bind_automute(codec);
218069e52a80SHarald Welte }
218169e52a80SHarald Welte 
2182c577b8a1SJoseph Chan static int via_init(struct hda_codec *codec)
2183c577b8a1SJoseph Chan {
2184c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
218569e52a80SHarald Welte 	int i;
218669e52a80SHarald Welte 	for (i = 0; i < spec->num_iverbs; i++)
218769e52a80SHarald Welte 		snd_hda_sequence_write(codec, spec->init_verbs[i]);
218869e52a80SHarald Welte 
2189518bf3baSLydia Wang 	spec->codec_type = get_codec_type(codec);
2190518bf3baSLydia Wang 	if (spec->codec_type == VT1708BCE)
2191518bf3baSLydia Wang 		spec->codec_type = VT1708S; /* VT1708BCE & VT1708S are almost
2192518bf3baSLydia Wang 					       same */
2193f7278fd0SJosepch Chan 	/* Lydia Add for EAPD enable */
2194f7278fd0SJosepch Chan 	if (!spec->dig_in_nid) { /* No Digital In connection */
219555d1d6c1STakashi Iwai 		if (spec->dig_in_pin) {
219655d1d6c1STakashi Iwai 			snd_hda_codec_write(codec, spec->dig_in_pin, 0,
2197f7278fd0SJosepch Chan 					    AC_VERB_SET_PIN_WIDGET_CONTROL,
219812b74c80STakashi Iwai 					    PIN_OUT);
219955d1d6c1STakashi Iwai 			snd_hda_codec_write(codec, spec->dig_in_pin, 0,
2200f7278fd0SJosepch Chan 					    AC_VERB_SET_EAPD_BTLENABLE, 0x02);
2201f7278fd0SJosepch Chan 		}
220212b74c80STakashi Iwai 	} else /* enable SPDIF-input pin */
220312b74c80STakashi Iwai 		snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
220412b74c80STakashi Iwai 				    AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
2205f7278fd0SJosepch Chan 
22069da29271STakashi Iwai 	/* assign slave outs */
22079da29271STakashi Iwai 	if (spec->slave_dig_outs[0])
22089da29271STakashi Iwai 		codec->slave_dig_outs = spec->slave_dig_outs;
22095691ec7fSHarald Welte 
2210c577b8a1SJoseph Chan 	return 0;
2211c577b8a1SJoseph Chan }
2212c577b8a1SJoseph Chan 
22131f2e99feSLydia Wang #ifdef SND_HDA_NEEDS_RESUME
22141f2e99feSLydia Wang static int via_suspend(struct hda_codec *codec, pm_message_t state)
22151f2e99feSLydia Wang {
22161f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
22171f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
22181f2e99feSLydia Wang 	return 0;
22191f2e99feSLydia Wang }
22201f2e99feSLydia Wang #endif
22211f2e99feSLydia Wang 
2222cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
2223cb53c626STakashi Iwai static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
2224cb53c626STakashi Iwai {
2225cb53c626STakashi Iwai 	struct via_spec *spec = codec->spec;
2226cb53c626STakashi Iwai 	return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
2227cb53c626STakashi Iwai }
2228cb53c626STakashi Iwai #endif
2229cb53c626STakashi Iwai 
2230c577b8a1SJoseph Chan /*
2231c577b8a1SJoseph Chan  */
2232c577b8a1SJoseph Chan static struct hda_codec_ops via_patch_ops = {
2233c577b8a1SJoseph Chan 	.build_controls = via_build_controls,
2234c577b8a1SJoseph Chan 	.build_pcms = via_build_pcms,
2235c577b8a1SJoseph Chan 	.init = via_init,
2236c577b8a1SJoseph Chan 	.free = via_free,
22371f2e99feSLydia Wang #ifdef SND_HDA_NEEDS_RESUME
22381f2e99feSLydia Wang 	.suspend = via_suspend,
22391f2e99feSLydia Wang #endif
2240cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
2241cb53c626STakashi Iwai 	.check_power_status = via_check_power_status,
2242cb53c626STakashi Iwai #endif
2243c577b8a1SJoseph Chan };
2244c577b8a1SJoseph Chan 
2245c577b8a1SJoseph Chan /* fill in the dac_nids table from the parsed pin configuration */
2246c577b8a1SJoseph Chan static int vt1708_auto_fill_dac_nids(struct via_spec *spec,
2247c577b8a1SJoseph Chan 				     const struct auto_pin_cfg *cfg)
2248c577b8a1SJoseph Chan {
2249c577b8a1SJoseph Chan 	int i;
2250c577b8a1SJoseph Chan 	hda_nid_t nid;
2251c577b8a1SJoseph Chan 
2252c577b8a1SJoseph Chan 	spec->multiout.num_dacs = cfg->line_outs;
2253c577b8a1SJoseph Chan 
2254c577b8a1SJoseph Chan 	spec->multiout.dac_nids = spec->private_dac_nids;
2255c577b8a1SJoseph Chan 
2256c577b8a1SJoseph Chan 	for (i = 0; i < 4; i++) {
2257c577b8a1SJoseph Chan 		nid = cfg->line_out_pins[i];
2258c577b8a1SJoseph Chan 		if (nid) {
2259c577b8a1SJoseph Chan 			/* config dac list */
2260c577b8a1SJoseph Chan 			switch (i) {
2261c577b8a1SJoseph Chan 			case AUTO_SEQ_FRONT:
2262c577b8a1SJoseph Chan 				spec->multiout.dac_nids[i] = 0x10;
2263c577b8a1SJoseph Chan 				break;
2264c577b8a1SJoseph Chan 			case AUTO_SEQ_CENLFE:
2265c577b8a1SJoseph Chan 				spec->multiout.dac_nids[i] = 0x12;
2266c577b8a1SJoseph Chan 				break;
2267c577b8a1SJoseph Chan 			case AUTO_SEQ_SURROUND:
2268fb4cb772SHarald Welte 				spec->multiout.dac_nids[i] = 0x11;
2269c577b8a1SJoseph Chan 				break;
2270c577b8a1SJoseph Chan 			case AUTO_SEQ_SIDE:
2271fb4cb772SHarald Welte 				spec->multiout.dac_nids[i] = 0x13;
2272c577b8a1SJoseph Chan 				break;
2273c577b8a1SJoseph Chan 			}
2274c577b8a1SJoseph Chan 		}
2275c577b8a1SJoseph Chan 	}
2276c577b8a1SJoseph Chan 
2277c577b8a1SJoseph Chan 	return 0;
2278c577b8a1SJoseph Chan }
2279c577b8a1SJoseph Chan 
2280c577b8a1SJoseph Chan /* add playback controls from the parsed DAC table */
2281c577b8a1SJoseph Chan static int vt1708_auto_create_multi_out_ctls(struct via_spec *spec,
2282c577b8a1SJoseph Chan 					     const struct auto_pin_cfg *cfg)
2283c577b8a1SJoseph Chan {
2284c577b8a1SJoseph Chan 	char name[32];
2285c577b8a1SJoseph Chan 	static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
22869645c203SLydia Wang 	hda_nid_t nid, nid_vol, nid_vols[] = {0x17, 0x19, 0x1a, 0x1b};
2287c577b8a1SJoseph Chan 	int i, err;
2288c577b8a1SJoseph Chan 
2289c577b8a1SJoseph Chan 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
2290c577b8a1SJoseph Chan 		nid = cfg->line_out_pins[i];
2291c577b8a1SJoseph Chan 
2292c577b8a1SJoseph Chan 		if (!nid)
2293c577b8a1SJoseph Chan 			continue;
2294c577b8a1SJoseph Chan 
22959645c203SLydia Wang 		nid_vol = nid_vols[i];
2296c577b8a1SJoseph Chan 
2297c577b8a1SJoseph Chan 		if (i == AUTO_SEQ_CENLFE) {
2298c577b8a1SJoseph Chan 			/* Center/LFE */
2299c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2300c577b8a1SJoseph Chan 					"Center Playback Volume",
2301f7278fd0SJosepch Chan 					HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2302f7278fd0SJosepch Chan 							    HDA_OUTPUT));
2303c577b8a1SJoseph Chan 			if (err < 0)
2304c577b8a1SJoseph Chan 				return err;
2305c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2306c577b8a1SJoseph Chan 					      "LFE Playback Volume",
2307f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2308f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2309c577b8a1SJoseph Chan 			if (err < 0)
2310c577b8a1SJoseph Chan 				return err;
2311c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2312c577b8a1SJoseph Chan 					      "Center Playback Switch",
2313f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2314f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2315c577b8a1SJoseph Chan 			if (err < 0)
2316c577b8a1SJoseph Chan 				return err;
2317c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2318c577b8a1SJoseph Chan 					      "LFE Playback Switch",
2319f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2320f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2321c577b8a1SJoseph Chan 			if (err < 0)
2322c577b8a1SJoseph Chan 				return err;
2323c577b8a1SJoseph Chan 		} else if (i == AUTO_SEQ_FRONT) {
2324c577b8a1SJoseph Chan 			/* add control to mixer index 0 */
2325c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2326c577b8a1SJoseph Chan 					      "Master Front Playback Volume",
23279645c203SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2328f7278fd0SJosepch Chan 								  HDA_INPUT));
2329c577b8a1SJoseph Chan 			if (err < 0)
2330c577b8a1SJoseph Chan 				return err;
2331c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2332c577b8a1SJoseph Chan 					      "Master Front Playback Switch",
23339645c203SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2334f7278fd0SJosepch Chan 								  HDA_INPUT));
2335c577b8a1SJoseph Chan 			if (err < 0)
2336c577b8a1SJoseph Chan 				return err;
2337c577b8a1SJoseph Chan 
2338c577b8a1SJoseph Chan 			/* add control to PW3 */
2339c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Volume", chname[i]);
2340c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2341f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2342f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2343c577b8a1SJoseph Chan 			if (err < 0)
2344c577b8a1SJoseph Chan 				return err;
2345c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Switch", chname[i]);
2346c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2347f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2348f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2349c577b8a1SJoseph Chan 			if (err < 0)
2350c577b8a1SJoseph Chan 				return err;
2351c577b8a1SJoseph Chan 		} else {
2352c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Volume", chname[i]);
2353c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2354f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2355f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2356c577b8a1SJoseph Chan 			if (err < 0)
2357c577b8a1SJoseph Chan 				return err;
2358c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Switch", chname[i]);
2359c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2360f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2361f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2362c577b8a1SJoseph Chan 			if (err < 0)
2363c577b8a1SJoseph Chan 				return err;
2364c577b8a1SJoseph Chan 		}
2365c577b8a1SJoseph Chan 	}
2366c577b8a1SJoseph Chan 
2367c577b8a1SJoseph Chan 	return 0;
2368c577b8a1SJoseph Chan }
2369c577b8a1SJoseph Chan 
23700aa62aefSHarald Welte static void create_hp_imux(struct via_spec *spec)
23710aa62aefSHarald Welte {
23720aa62aefSHarald Welte 	int i;
23730aa62aefSHarald Welte 	struct hda_input_mux *imux = &spec->private_imux[1];
23740aa62aefSHarald Welte 	static const char *texts[] = { "OFF", "ON", NULL};
23750aa62aefSHarald Welte 
23760aa62aefSHarald Welte 	/* for hp mode select */
2377*10a20af7STakashi Iwai 	for (i = 0; texts[i]; i++)
2378*10a20af7STakashi Iwai 		snd_hda_add_imux_item(imux, texts[i], i, NULL);
23790aa62aefSHarald Welte 
23800aa62aefSHarald Welte 	spec->hp_mux = &spec->private_imux[1];
23810aa62aefSHarald Welte }
23820aa62aefSHarald Welte 
2383c577b8a1SJoseph Chan static int vt1708_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
2384c577b8a1SJoseph Chan {
2385c577b8a1SJoseph Chan 	int err;
2386c577b8a1SJoseph Chan 
2387c577b8a1SJoseph Chan 	if (!pin)
2388c577b8a1SJoseph Chan 		return 0;
2389c577b8a1SJoseph Chan 
2390c577b8a1SJoseph Chan 	spec->multiout.hp_nid = VT1708_HP_NID; /* AOW3 */
2391cdc1784dSLydia Wang 	spec->hp_independent_mode_index = 1;
2392c577b8a1SJoseph Chan 
2393c577b8a1SJoseph Chan 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2394c577b8a1SJoseph Chan 			      "Headphone Playback Volume",
2395c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2396c577b8a1SJoseph Chan 	if (err < 0)
2397c577b8a1SJoseph Chan 		return err;
2398c577b8a1SJoseph Chan 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2399c577b8a1SJoseph Chan 			      "Headphone Playback Switch",
2400c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2401c577b8a1SJoseph Chan 	if (err < 0)
2402c577b8a1SJoseph Chan 		return err;
2403c577b8a1SJoseph Chan 
24040aa62aefSHarald Welte 	create_hp_imux(spec);
24050aa62aefSHarald Welte 
2406c577b8a1SJoseph Chan 	return 0;
2407c577b8a1SJoseph Chan }
2408c577b8a1SJoseph Chan 
2409c577b8a1SJoseph Chan /* create playback/capture controls for input pins */
2410*10a20af7STakashi Iwai static int vt_auto_create_analog_input_ctls(struct hda_codec *codec,
2411f3268512STakashi Iwai 					    const struct auto_pin_cfg *cfg,
2412f3268512STakashi Iwai 					    hda_nid_t cap_nid,
2413f3268512STakashi Iwai 					    hda_nid_t pin_idxs[], int num_idxs)
2414c577b8a1SJoseph Chan {
2415*10a20af7STakashi Iwai 	struct via_spec *spec = codec->spec;
24160aa62aefSHarald Welte 	struct hda_input_mux *imux = &spec->private_imux[0];
24177b315bb4STakashi Iwai 	int i, err, idx, type, type_idx = 0;
2418c577b8a1SJoseph Chan 
2419c577b8a1SJoseph Chan 	/* for internal loopback recording select */
2420f3268512STakashi Iwai 	for (idx = 0; idx < num_idxs; idx++) {
2421f3268512STakashi Iwai 		if (pin_idxs[idx] == 0xff) {
2422*10a20af7STakashi Iwai 			snd_hda_add_imux_item(imux, "Stereo Mixer", idx, NULL);
2423f3268512STakashi Iwai 			break;
2424f3268512STakashi Iwai 		}
2425f3268512STakashi Iwai 	}
2426c577b8a1SJoseph Chan 
24277b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2428*10a20af7STakashi Iwai 		const char *label;
24297b315bb4STakashi Iwai 		type = cfg->inputs[i].type;
2430f3268512STakashi Iwai 		for (idx = 0; idx < num_idxs; idx++)
24317b315bb4STakashi Iwai 			if (pin_idxs[idx] == cfg->inputs[i].pin)
2432c577b8a1SJoseph Chan 				break;
2433f3268512STakashi Iwai 		if (idx >= num_idxs)
2434f3268512STakashi Iwai 			continue;
24357b315bb4STakashi Iwai 		if (i > 0 && type == cfg->inputs[i - 1].type)
24367b315bb4STakashi Iwai 			type_idx++;
24377b315bb4STakashi Iwai 		else
24387b315bb4STakashi Iwai 			type_idx = 0;
2439*10a20af7STakashi Iwai 		label = hda_get_autocfg_input_label(codec, cfg, i);
2440*10a20af7STakashi Iwai 		err = via_new_analog_input(spec, label, type_idx, idx, cap_nid);
2441c577b8a1SJoseph Chan 		if (err < 0)
2442c577b8a1SJoseph Chan 			return err;
2443*10a20af7STakashi Iwai 		snd_hda_add_imux_item(imux, label, idx, NULL);
2444c577b8a1SJoseph Chan 	}
2445c577b8a1SJoseph Chan 	return 0;
2446c577b8a1SJoseph Chan }
2447c577b8a1SJoseph Chan 
2448f3268512STakashi Iwai /* create playback/capture controls for input pins */
2449*10a20af7STakashi Iwai static int vt1708_auto_create_analog_input_ctls(struct hda_codec *codec,
2450f3268512STakashi Iwai 						const struct auto_pin_cfg *cfg)
2451f3268512STakashi Iwai {
2452f3268512STakashi Iwai 	static hda_nid_t pin_idxs[] = { 0xff, 0x24, 0x1d, 0x1e, 0x21 };
2453*10a20af7STakashi Iwai 	return vt_auto_create_analog_input_ctls(codec, cfg, 0x17, pin_idxs,
2454f3268512STakashi Iwai 						ARRAY_SIZE(pin_idxs));
2455f3268512STakashi Iwai }
2456f3268512STakashi Iwai 
2457cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
2458cb53c626STakashi Iwai static struct hda_amp_list vt1708_loopbacks[] = {
2459cb53c626STakashi Iwai 	{ 0x17, HDA_INPUT, 1 },
2460cb53c626STakashi Iwai 	{ 0x17, HDA_INPUT, 2 },
2461cb53c626STakashi Iwai 	{ 0x17, HDA_INPUT, 3 },
2462cb53c626STakashi Iwai 	{ 0x17, HDA_INPUT, 4 },
2463cb53c626STakashi Iwai 	{ } /* end */
2464cb53c626STakashi Iwai };
2465cb53c626STakashi Iwai #endif
2466cb53c626STakashi Iwai 
246776d9b0ddSHarald Welte static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
246876d9b0ddSHarald Welte {
246976d9b0ddSHarald Welte 	unsigned int def_conf;
247076d9b0ddSHarald Welte 	unsigned char seqassoc;
247176d9b0ddSHarald Welte 
24722f334f92STakashi Iwai 	def_conf = snd_hda_codec_get_pincfg(codec, nid);
247376d9b0ddSHarald Welte 	seqassoc = (unsigned char) get_defcfg_association(def_conf);
247476d9b0ddSHarald Welte 	seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
247582ef9e45SLydia Wang 	if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
247682ef9e45SLydia Wang 	    && (seqassoc == 0xf0 || seqassoc == 0xff)) {
247776d9b0ddSHarald Welte 		def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
24782f334f92STakashi Iwai 		snd_hda_codec_set_pincfg(codec, nid, def_conf);
247976d9b0ddSHarald Welte 	}
248076d9b0ddSHarald Welte 
248176d9b0ddSHarald Welte 	return;
248276d9b0ddSHarald Welte }
248376d9b0ddSHarald Welte 
24841f2e99feSLydia Wang static int vt1708_jack_detectect_get(struct snd_kcontrol *kcontrol,
24851f2e99feSLydia Wang 				     struct snd_ctl_elem_value *ucontrol)
24861f2e99feSLydia Wang {
24871f2e99feSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
24881f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
24891f2e99feSLydia Wang 
24901f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
24911f2e99feSLydia Wang 		return 0;
24921f2e99feSLydia Wang 	spec->vt1708_jack_detectect =
24931f2e99feSLydia Wang 		!((snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8) & 0x1);
24941f2e99feSLydia Wang 	ucontrol->value.integer.value[0] = spec->vt1708_jack_detectect;
24951f2e99feSLydia Wang 	return 0;
24961f2e99feSLydia Wang }
24971f2e99feSLydia Wang 
24981f2e99feSLydia Wang static int vt1708_jack_detectect_put(struct snd_kcontrol *kcontrol,
24991f2e99feSLydia Wang 				     struct snd_ctl_elem_value *ucontrol)
25001f2e99feSLydia Wang {
25011f2e99feSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
25021f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
25031f2e99feSLydia Wang 	int change;
25041f2e99feSLydia Wang 
25051f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
25061f2e99feSLydia Wang 		return 0;
25071f2e99feSLydia Wang 	spec->vt1708_jack_detectect = ucontrol->value.integer.value[0];
25081f2e99feSLydia Wang 	change = (0x1 & (snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8))
25091f2e99feSLydia Wang 		== !spec->vt1708_jack_detectect;
25101f2e99feSLydia Wang 	if (spec->vt1708_jack_detectect) {
25111f2e99feSLydia Wang 		mute_aa_path(codec, 1);
25121f2e99feSLydia Wang 		notify_aa_path_ctls(codec);
25131f2e99feSLydia Wang 	}
25141f2e99feSLydia Wang 	return change;
25151f2e99feSLydia Wang }
25161f2e99feSLydia Wang 
25171f2e99feSLydia Wang static struct snd_kcontrol_new vt1708_jack_detectect[] = {
25181f2e99feSLydia Wang 	{
25191f2e99feSLydia Wang 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
25201f2e99feSLydia Wang 		.name = "Jack Detect",
25211f2e99feSLydia Wang 		.count = 1,
25221f2e99feSLydia Wang 		.info = snd_ctl_boolean_mono_info,
25231f2e99feSLydia Wang 		.get = vt1708_jack_detectect_get,
25241f2e99feSLydia Wang 		.put = vt1708_jack_detectect_put,
25251f2e99feSLydia Wang 	},
25261f2e99feSLydia Wang 	{} /* end */
25271f2e99feSLydia Wang };
25281f2e99feSLydia Wang 
2529c577b8a1SJoseph Chan static int vt1708_parse_auto_config(struct hda_codec *codec)
2530c577b8a1SJoseph Chan {
2531c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
2532c577b8a1SJoseph Chan 	int err;
2533c577b8a1SJoseph Chan 
253476d9b0ddSHarald Welte 	/* Add HP and CD pin config connect bit re-config action */
253576d9b0ddSHarald Welte 	vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
253676d9b0ddSHarald Welte 	vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
253776d9b0ddSHarald Welte 
2538c577b8a1SJoseph Chan 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2539c577b8a1SJoseph Chan 	if (err < 0)
2540c577b8a1SJoseph Chan 		return err;
2541c577b8a1SJoseph Chan 	err = vt1708_auto_fill_dac_nids(spec, &spec->autocfg);
2542c577b8a1SJoseph Chan 	if (err < 0)
2543c577b8a1SJoseph Chan 		return err;
2544c577b8a1SJoseph Chan 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2545c577b8a1SJoseph Chan 		return 0; /* can't find valid BIOS pin config */
2546c577b8a1SJoseph Chan 
2547c577b8a1SJoseph Chan 	err = vt1708_auto_create_multi_out_ctls(spec, &spec->autocfg);
2548c577b8a1SJoseph Chan 	if (err < 0)
2549c577b8a1SJoseph Chan 		return err;
2550c577b8a1SJoseph Chan 	err = vt1708_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2551c577b8a1SJoseph Chan 	if (err < 0)
2552c577b8a1SJoseph Chan 		return err;
2553*10a20af7STakashi Iwai 	err = vt1708_auto_create_analog_input_ctls(codec, &spec->autocfg);
2554c577b8a1SJoseph Chan 	if (err < 0)
2555c577b8a1SJoseph Chan 		return err;
25561f2e99feSLydia Wang 	/* add jack detect on/off control */
25571f2e99feSLydia Wang 	err = snd_hda_add_new_ctls(codec, vt1708_jack_detectect);
25581f2e99feSLydia Wang 	if (err < 0)
25591f2e99feSLydia Wang 		return err;
2560c577b8a1SJoseph Chan 
2561c577b8a1SJoseph Chan 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2562c577b8a1SJoseph Chan 
25630852d7a6STakashi Iwai 	if (spec->autocfg.dig_outs)
2564c577b8a1SJoseph Chan 		spec->multiout.dig_out_nid = VT1708_DIGOUT_NID;
256555d1d6c1STakashi Iwai 	spec->dig_in_pin = VT1708_DIGIN_PIN;
2566c577b8a1SJoseph Chan 	if (spec->autocfg.dig_in_pin)
2567c577b8a1SJoseph Chan 		spec->dig_in_nid = VT1708_DIGIN_NID;
2568c577b8a1SJoseph Chan 
2569603c4019STakashi Iwai 	if (spec->kctls.list)
2570603c4019STakashi Iwai 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
2571c577b8a1SJoseph Chan 
257269e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1708_volume_init_verbs;
2573c577b8a1SJoseph Chan 
25740aa62aefSHarald Welte 	spec->input_mux = &spec->private_imux[0];
25750aa62aefSHarald Welte 
2576f8fdd495SHarald Welte 	if (spec->hp_mux)
25773d83e577STakashi Iwai 		via_hp_build(codec);
2578c577b8a1SJoseph Chan 
25795b0cb1d8SJaroslav Kysela 	via_smart51_build(spec);
2580c577b8a1SJoseph Chan 	return 1;
2581c577b8a1SJoseph Chan }
2582c577b8a1SJoseph Chan 
2583c577b8a1SJoseph Chan /* init callback for auto-configuration model -- overriding the default init */
2584c577b8a1SJoseph Chan static int via_auto_init(struct hda_codec *codec)
2585c577b8a1SJoseph Chan {
258625eaba2fSLydia Wang 	struct via_spec *spec = codec->spec;
258725eaba2fSLydia Wang 
2588c577b8a1SJoseph Chan 	via_init(codec);
2589c577b8a1SJoseph Chan 	via_auto_init_multi_out(codec);
2590c577b8a1SJoseph Chan 	via_auto_init_hp_out(codec);
2591c577b8a1SJoseph Chan 	via_auto_init_analog_input(codec);
2592ab6734e7SLydia Wang 	if (spec->codec_type == VT2002P || spec->codec_type == VT1812) {
259325eaba2fSLydia Wang 		via_hp_bind_automute(codec);
259425eaba2fSLydia Wang 	} else {
259525eaba2fSLydia Wang 		via_hp_automute(codec);
259625eaba2fSLydia Wang 		via_speaker_automute(codec);
259725eaba2fSLydia Wang 	}
259825eaba2fSLydia Wang 
2599c577b8a1SJoseph Chan 	return 0;
2600c577b8a1SJoseph Chan }
2601c577b8a1SJoseph Chan 
26021f2e99feSLydia Wang static void vt1708_update_hp_jack_state(struct work_struct *work)
26031f2e99feSLydia Wang {
26041f2e99feSLydia Wang 	struct via_spec *spec = container_of(work, struct via_spec,
26051f2e99feSLydia Wang 					     vt1708_hp_work.work);
26061f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
26071f2e99feSLydia Wang 		return;
26081f2e99feSLydia Wang 	/* if jack state toggled */
26091f2e99feSLydia Wang 	if (spec->vt1708_hp_present
2610d56757abSTakashi Iwai 	    != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
26111f2e99feSLydia Wang 		spec->vt1708_hp_present ^= 1;
26121f2e99feSLydia Wang 		via_hp_automute(spec->codec);
26131f2e99feSLydia Wang 	}
26141f2e99feSLydia Wang 	vt1708_start_hp_work(spec);
26151f2e99feSLydia Wang }
26161f2e99feSLydia Wang 
2617337b9d02STakashi Iwai static int get_mux_nids(struct hda_codec *codec)
2618337b9d02STakashi Iwai {
2619337b9d02STakashi Iwai 	struct via_spec *spec = codec->spec;
2620337b9d02STakashi Iwai 	hda_nid_t nid, conn[8];
2621337b9d02STakashi Iwai 	unsigned int type;
2622337b9d02STakashi Iwai 	int i, n;
2623337b9d02STakashi Iwai 
2624337b9d02STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
2625337b9d02STakashi Iwai 		nid = spec->adc_nids[i];
2626337b9d02STakashi Iwai 		while (nid) {
2627a22d543aSTakashi Iwai 			type = get_wcaps_type(get_wcaps(codec, nid));
26281c55d521STakashi Iwai 			if (type == AC_WID_PIN)
26291c55d521STakashi Iwai 				break;
2630337b9d02STakashi Iwai 			n = snd_hda_get_connections(codec, nid, conn,
2631337b9d02STakashi Iwai 						    ARRAY_SIZE(conn));
2632337b9d02STakashi Iwai 			if (n <= 0)
2633337b9d02STakashi Iwai 				break;
2634337b9d02STakashi Iwai 			if (n > 1) {
2635337b9d02STakashi Iwai 				spec->mux_nids[i] = nid;
2636337b9d02STakashi Iwai 				break;
2637337b9d02STakashi Iwai 			}
2638337b9d02STakashi Iwai 			nid = conn[0];
2639337b9d02STakashi Iwai 		}
2640337b9d02STakashi Iwai 	}
26411c55d521STakashi Iwai 	return 0;
2642337b9d02STakashi Iwai }
2643337b9d02STakashi Iwai 
2644c577b8a1SJoseph Chan static int patch_vt1708(struct hda_codec *codec)
2645c577b8a1SJoseph Chan {
2646c577b8a1SJoseph Chan 	struct via_spec *spec;
2647c577b8a1SJoseph Chan 	int err;
2648c577b8a1SJoseph Chan 
2649c577b8a1SJoseph Chan 	/* create a codec specific record */
26505b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2651c577b8a1SJoseph Chan 	if (spec == NULL)
2652c577b8a1SJoseph Chan 		return -ENOMEM;
2653c577b8a1SJoseph Chan 
2654c577b8a1SJoseph Chan 	/* automatic parse from the BIOS config */
2655c577b8a1SJoseph Chan 	err = vt1708_parse_auto_config(codec);
2656c577b8a1SJoseph Chan 	if (err < 0) {
2657c577b8a1SJoseph Chan 		via_free(codec);
2658c577b8a1SJoseph Chan 		return err;
2659c577b8a1SJoseph Chan 	} else if (!err) {
2660c577b8a1SJoseph Chan 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
2661c577b8a1SJoseph Chan 		       "from BIOS.  Using genenic mode...\n");
2662c577b8a1SJoseph Chan 	}
2663c577b8a1SJoseph Chan 
2664c577b8a1SJoseph Chan 
2665c577b8a1SJoseph Chan 	spec->stream_name_analog = "VT1708 Analog";
2666c577b8a1SJoseph Chan 	spec->stream_analog_playback = &vt1708_pcm_analog_playback;
2667bc9b5623STakashi Iwai 	/* disable 32bit format on VT1708 */
2668bc9b5623STakashi Iwai 	if (codec->vendor_id == 0x11061708)
2669bc9b5623STakashi Iwai 		spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
2670c577b8a1SJoseph Chan 	spec->stream_analog_capture = &vt1708_pcm_analog_capture;
2671c577b8a1SJoseph Chan 
2672c577b8a1SJoseph Chan 	spec->stream_name_digital = "VT1708 Digital";
2673c577b8a1SJoseph Chan 	spec->stream_digital_playback = &vt1708_pcm_digital_playback;
2674c577b8a1SJoseph Chan 	spec->stream_digital_capture = &vt1708_pcm_digital_capture;
2675c577b8a1SJoseph Chan 
2676c577b8a1SJoseph Chan 
2677c577b8a1SJoseph Chan 	if (!spec->adc_nids && spec->input_mux) {
2678c577b8a1SJoseph Chan 		spec->adc_nids = vt1708_adc_nids;
2679c577b8a1SJoseph Chan 		spec->num_adc_nids = ARRAY_SIZE(vt1708_adc_nids);
26800f67a611STakashi Iwai 		get_mux_nids(codec);
2681c577b8a1SJoseph Chan 		spec->mixers[spec->num_mixers] = vt1708_capture_mixer;
2682c577b8a1SJoseph Chan 		spec->num_mixers++;
2683c577b8a1SJoseph Chan 	}
2684c577b8a1SJoseph Chan 
2685c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
2686c577b8a1SJoseph Chan 
2687c577b8a1SJoseph Chan 	codec->patch_ops.init = via_auto_init;
2688cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
2689cb53c626STakashi Iwai 	spec->loopback.amplist = vt1708_loopbacks;
2690cb53c626STakashi Iwai #endif
26911f2e99feSLydia Wang 	INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
2692c577b8a1SJoseph Chan 	return 0;
2693c577b8a1SJoseph Chan }
2694c577b8a1SJoseph Chan 
2695c577b8a1SJoseph Chan /* capture mixer elements */
2696c577b8a1SJoseph Chan static struct snd_kcontrol_new vt1709_capture_mixer[] = {
2697c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x0, HDA_INPUT),
2698c577b8a1SJoseph Chan 	HDA_CODEC_MUTE("Capture Switch", 0x14, 0x0, HDA_INPUT),
2699c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x15, 0x0, HDA_INPUT),
2700c577b8a1SJoseph Chan 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x15, 0x0, HDA_INPUT),
2701c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x16, 0x0, HDA_INPUT),
2702c577b8a1SJoseph Chan 	HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x16, 0x0, HDA_INPUT),
2703c577b8a1SJoseph Chan 	{
2704c577b8a1SJoseph Chan 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2705c577b8a1SJoseph Chan 		/* The multiple "Capture Source" controls confuse alsamixer
2706c577b8a1SJoseph Chan 		 * So call somewhat different..
2707c577b8a1SJoseph Chan 		 */
2708c577b8a1SJoseph Chan 		/* .name = "Capture Source", */
2709c577b8a1SJoseph Chan 		.name = "Input Source",
2710c577b8a1SJoseph Chan 		.count = 1,
2711c577b8a1SJoseph Chan 		.info = via_mux_enum_info,
2712c577b8a1SJoseph Chan 		.get = via_mux_enum_get,
2713c577b8a1SJoseph Chan 		.put = via_mux_enum_put,
2714c577b8a1SJoseph Chan 	},
2715c577b8a1SJoseph Chan 	{ } /* end */
2716c577b8a1SJoseph Chan };
2717c577b8a1SJoseph Chan 
271869e52a80SHarald Welte static struct hda_verb vt1709_uniwill_init_verbs[] = {
2719a34df19aSLydia Wang 	{0x20, AC_VERB_SET_UNSOLICITED_ENABLE,
2720a34df19aSLydia Wang 	 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
272169e52a80SHarald Welte 	{ }
272269e52a80SHarald Welte };
272369e52a80SHarald Welte 
2724c577b8a1SJoseph Chan /*
2725c577b8a1SJoseph Chan  * generic initialization of ADC, input mixers and output mixers
2726c577b8a1SJoseph Chan  */
2727c577b8a1SJoseph Chan static struct hda_verb vt1709_10ch_volume_init_verbs[] = {
2728c577b8a1SJoseph Chan 	/*
2729c577b8a1SJoseph Chan 	 * Unmute ADC0-2 and set the default input to mic-in
2730c577b8a1SJoseph Chan 	 */
2731c577b8a1SJoseph Chan 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2732c577b8a1SJoseph Chan 	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2733c577b8a1SJoseph Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2734c577b8a1SJoseph Chan 
2735c577b8a1SJoseph Chan 
2736f7278fd0SJosepch Chan 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2737c577b8a1SJoseph Chan 	 * mixer widget
2738c577b8a1SJoseph Chan 	 */
2739c577b8a1SJoseph Chan 	/* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2740f7278fd0SJosepch Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2741f7278fd0SJosepch Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2742f7278fd0SJosepch Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2743f7278fd0SJosepch Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2744f7278fd0SJosepch Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2745c577b8a1SJoseph Chan 
2746c577b8a1SJoseph Chan 	/*
2747c577b8a1SJoseph Chan 	 * Set up output selector (0x1a, 0x1b, 0x29)
2748c577b8a1SJoseph Chan 	 */
2749c577b8a1SJoseph Chan 	/* set vol=0 to output mixers */
2750c577b8a1SJoseph Chan 	{0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2751c577b8a1SJoseph Chan 	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2752c577b8a1SJoseph Chan 	{0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2753c577b8a1SJoseph Chan 
2754c577b8a1SJoseph Chan 	/*
2755c577b8a1SJoseph Chan 	 *  Unmute PW3 and PW4
2756c577b8a1SJoseph Chan 	 */
2757c577b8a1SJoseph Chan 	{0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2758c577b8a1SJoseph Chan 	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2759c577b8a1SJoseph Chan 
2760bfdc675aSLydia Wang 	/* Set input of PW4 as MW0 */
2761bfdc675aSLydia Wang 	{0x20, AC_VERB_SET_CONNECT_SEL, 0},
2762c577b8a1SJoseph Chan 	/* PW9 Output enable */
2763c577b8a1SJoseph Chan 	{0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2764c577b8a1SJoseph Chan 	{ }
2765c577b8a1SJoseph Chan };
2766c577b8a1SJoseph Chan 
2767c577b8a1SJoseph Chan static struct hda_pcm_stream vt1709_10ch_pcm_analog_playback = {
2768c577b8a1SJoseph Chan 	.substreams = 1,
2769c577b8a1SJoseph Chan 	.channels_min = 2,
2770c577b8a1SJoseph Chan 	.channels_max = 10,
2771c577b8a1SJoseph Chan 	.nid = 0x10, /* NID to query formats and rates */
2772c577b8a1SJoseph Chan 	.ops = {
2773c577b8a1SJoseph Chan 		.open = via_playback_pcm_open,
2774c873cc25SLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
2775c873cc25SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup,
2776c577b8a1SJoseph Chan 	},
2777c577b8a1SJoseph Chan };
2778c577b8a1SJoseph Chan 
2779c577b8a1SJoseph Chan static struct hda_pcm_stream vt1709_6ch_pcm_analog_playback = {
2780c577b8a1SJoseph Chan 	.substreams = 1,
2781c577b8a1SJoseph Chan 	.channels_min = 2,
2782c577b8a1SJoseph Chan 	.channels_max = 6,
2783c577b8a1SJoseph Chan 	.nid = 0x10, /* NID to query formats and rates */
2784c577b8a1SJoseph Chan 	.ops = {
2785c577b8a1SJoseph Chan 		.open = via_playback_pcm_open,
2786c873cc25SLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
2787c873cc25SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup,
2788c577b8a1SJoseph Chan 	},
2789c577b8a1SJoseph Chan };
2790c577b8a1SJoseph Chan 
2791c577b8a1SJoseph Chan static struct hda_pcm_stream vt1709_pcm_analog_capture = {
2792c577b8a1SJoseph Chan 	.substreams = 2,
2793c577b8a1SJoseph Chan 	.channels_min = 2,
2794c577b8a1SJoseph Chan 	.channels_max = 2,
2795c577b8a1SJoseph Chan 	.nid = 0x14, /* NID to query formats and rates */
2796c577b8a1SJoseph Chan 	.ops = {
2797c577b8a1SJoseph Chan 		.prepare = via_capture_pcm_prepare,
2798c577b8a1SJoseph Chan 		.cleanup = via_capture_pcm_cleanup
2799c577b8a1SJoseph Chan 	},
2800c577b8a1SJoseph Chan };
2801c577b8a1SJoseph Chan 
2802c577b8a1SJoseph Chan static struct hda_pcm_stream vt1709_pcm_digital_playback = {
2803c577b8a1SJoseph Chan 	.substreams = 1,
2804c577b8a1SJoseph Chan 	.channels_min = 2,
2805c577b8a1SJoseph Chan 	.channels_max = 2,
2806c577b8a1SJoseph Chan 	/* NID is set in via_build_pcms */
2807c577b8a1SJoseph Chan 	.ops = {
2808c577b8a1SJoseph Chan 		.open = via_dig_playback_pcm_open,
2809c577b8a1SJoseph Chan 		.close = via_dig_playback_pcm_close
2810c577b8a1SJoseph Chan 	},
2811c577b8a1SJoseph Chan };
2812c577b8a1SJoseph Chan 
2813c577b8a1SJoseph Chan static struct hda_pcm_stream vt1709_pcm_digital_capture = {
2814c577b8a1SJoseph Chan 	.substreams = 1,
2815c577b8a1SJoseph Chan 	.channels_min = 2,
2816c577b8a1SJoseph Chan 	.channels_max = 2,
2817c577b8a1SJoseph Chan };
2818c577b8a1SJoseph Chan 
2819c577b8a1SJoseph Chan static int vt1709_auto_fill_dac_nids(struct via_spec *spec,
2820c577b8a1SJoseph Chan 				     const struct auto_pin_cfg *cfg)
2821c577b8a1SJoseph Chan {
2822c577b8a1SJoseph Chan 	int i;
2823c577b8a1SJoseph Chan 	hda_nid_t nid;
2824c577b8a1SJoseph Chan 
2825c577b8a1SJoseph Chan 	if (cfg->line_outs == 4)  /* 10 channels */
2826c577b8a1SJoseph Chan 		spec->multiout.num_dacs = cfg->line_outs+1; /* AOW0~AOW4 */
2827c577b8a1SJoseph Chan 	else if (cfg->line_outs == 3) /* 6 channels */
2828c577b8a1SJoseph Chan 		spec->multiout.num_dacs = cfg->line_outs; /* AOW0~AOW2 */
2829c577b8a1SJoseph Chan 
2830c577b8a1SJoseph Chan 	spec->multiout.dac_nids = spec->private_dac_nids;
2831c577b8a1SJoseph Chan 
2832c577b8a1SJoseph Chan 	if (cfg->line_outs == 4) { /* 10 channels */
2833c577b8a1SJoseph Chan 		for (i = 0; i < cfg->line_outs; i++) {
2834c577b8a1SJoseph Chan 			nid = cfg->line_out_pins[i];
2835c577b8a1SJoseph Chan 			if (nid) {
2836c577b8a1SJoseph Chan 				/* config dac list */
2837c577b8a1SJoseph Chan 				switch (i) {
2838c577b8a1SJoseph Chan 				case AUTO_SEQ_FRONT:
2839c577b8a1SJoseph Chan 					/* AOW0 */
2840c577b8a1SJoseph Chan 					spec->multiout.dac_nids[i] = 0x10;
2841c577b8a1SJoseph Chan 					break;
2842c577b8a1SJoseph Chan 				case AUTO_SEQ_CENLFE:
2843c577b8a1SJoseph Chan 					/* AOW2 */
2844c577b8a1SJoseph Chan 					spec->multiout.dac_nids[i] = 0x12;
2845c577b8a1SJoseph Chan 					break;
2846c577b8a1SJoseph Chan 				case AUTO_SEQ_SURROUND:
2847c577b8a1SJoseph Chan 					/* AOW3 */
2848fb4cb772SHarald Welte 					spec->multiout.dac_nids[i] = 0x11;
2849c577b8a1SJoseph Chan 					break;
2850c577b8a1SJoseph Chan 				case AUTO_SEQ_SIDE:
2851c577b8a1SJoseph Chan 					/* AOW1 */
2852fb4cb772SHarald Welte 					spec->multiout.dac_nids[i] = 0x27;
2853c577b8a1SJoseph Chan 					break;
2854c577b8a1SJoseph Chan 				default:
2855c577b8a1SJoseph Chan 					break;
2856c577b8a1SJoseph Chan 				}
2857c577b8a1SJoseph Chan 			}
2858c577b8a1SJoseph Chan 		}
2859c577b8a1SJoseph Chan 		spec->multiout.dac_nids[cfg->line_outs] = 0x28; /* AOW4 */
2860c577b8a1SJoseph Chan 
2861c577b8a1SJoseph Chan 	} else if (cfg->line_outs == 3) { /* 6 channels */
2862c577b8a1SJoseph Chan 		for (i = 0; i < cfg->line_outs; i++) {
2863c577b8a1SJoseph Chan 			nid = cfg->line_out_pins[i];
2864c577b8a1SJoseph Chan 			if (nid) {
2865c577b8a1SJoseph Chan 				/* config dac list */
2866c577b8a1SJoseph Chan 				switch (i) {
2867c577b8a1SJoseph Chan 				case AUTO_SEQ_FRONT:
2868c577b8a1SJoseph Chan 					/* AOW0 */
2869c577b8a1SJoseph Chan 					spec->multiout.dac_nids[i] = 0x10;
2870c577b8a1SJoseph Chan 					break;
2871c577b8a1SJoseph Chan 				case AUTO_SEQ_CENLFE:
2872c577b8a1SJoseph Chan 					/* AOW2 */
2873c577b8a1SJoseph Chan 					spec->multiout.dac_nids[i] = 0x12;
2874c577b8a1SJoseph Chan 					break;
2875c577b8a1SJoseph Chan 				case AUTO_SEQ_SURROUND:
2876c577b8a1SJoseph Chan 					/* AOW1 */
2877c577b8a1SJoseph Chan 					spec->multiout.dac_nids[i] = 0x11;
2878c577b8a1SJoseph Chan 					break;
2879c577b8a1SJoseph Chan 				default:
2880c577b8a1SJoseph Chan 					break;
2881c577b8a1SJoseph Chan 				}
2882c577b8a1SJoseph Chan 			}
2883c577b8a1SJoseph Chan 		}
2884c577b8a1SJoseph Chan 	}
2885c577b8a1SJoseph Chan 
2886c577b8a1SJoseph Chan 	return 0;
2887c577b8a1SJoseph Chan }
2888c577b8a1SJoseph Chan 
2889c577b8a1SJoseph Chan /* add playback controls from the parsed DAC table */
2890c577b8a1SJoseph Chan static int vt1709_auto_create_multi_out_ctls(struct via_spec *spec,
2891c577b8a1SJoseph Chan 					     const struct auto_pin_cfg *cfg)
2892c577b8a1SJoseph Chan {
2893c577b8a1SJoseph Chan 	char name[32];
2894c577b8a1SJoseph Chan 	static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
28954483a2f5SLydia Wang 	hda_nid_t nid, nid_vol, nid_vols[] = {0x18, 0x1a, 0x1b, 0x29};
2896c577b8a1SJoseph Chan 	int i, err;
2897c577b8a1SJoseph Chan 
2898c577b8a1SJoseph Chan 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
2899c577b8a1SJoseph Chan 		nid = cfg->line_out_pins[i];
2900c577b8a1SJoseph Chan 
2901c577b8a1SJoseph Chan 		if (!nid)
2902c577b8a1SJoseph Chan 			continue;
2903c577b8a1SJoseph Chan 
29044483a2f5SLydia Wang 		nid_vol = nid_vols[i];
29054483a2f5SLydia Wang 
2906c577b8a1SJoseph Chan 		if (i == AUTO_SEQ_CENLFE) {
2907c577b8a1SJoseph Chan 			/* Center/LFE */
2908c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2909c577b8a1SJoseph Chan 					      "Center Playback Volume",
29104483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2911f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2912c577b8a1SJoseph Chan 			if (err < 0)
2913c577b8a1SJoseph Chan 				return err;
2914c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2915c577b8a1SJoseph Chan 					      "LFE Playback Volume",
29164483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2917f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2918c577b8a1SJoseph Chan 			if (err < 0)
2919c577b8a1SJoseph Chan 				return err;
2920c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2921c577b8a1SJoseph Chan 					      "Center Playback Switch",
29224483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2923f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2924c577b8a1SJoseph Chan 			if (err < 0)
2925c577b8a1SJoseph Chan 				return err;
2926c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2927c577b8a1SJoseph Chan 					      "LFE Playback Switch",
29284483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2929f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2930c577b8a1SJoseph Chan 			if (err < 0)
2931c577b8a1SJoseph Chan 				return err;
2932c577b8a1SJoseph Chan 		} else if (i == AUTO_SEQ_FRONT) {
29334483a2f5SLydia Wang 			/* ADD control to mixer index 0 */
2934c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2935c577b8a1SJoseph Chan 					      "Master Front Playback Volume",
29364483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2937f7278fd0SJosepch Chan 								  HDA_INPUT));
2938c577b8a1SJoseph Chan 			if (err < 0)
2939c577b8a1SJoseph Chan 				return err;
2940c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2941c577b8a1SJoseph Chan 					      "Master Front Playback Switch",
29424483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2943f7278fd0SJosepch Chan 								  HDA_INPUT));
2944c577b8a1SJoseph Chan 			if (err < 0)
2945c577b8a1SJoseph Chan 				return err;
2946c577b8a1SJoseph Chan 
2947c577b8a1SJoseph Chan 			/* add control to PW3 */
2948c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Volume", chname[i]);
2949c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2950f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2951f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2952c577b8a1SJoseph Chan 			if (err < 0)
2953c577b8a1SJoseph Chan 				return err;
2954c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Switch", chname[i]);
2955c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2956f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2957f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2958c577b8a1SJoseph Chan 			if (err < 0)
2959c577b8a1SJoseph Chan 				return err;
2960c577b8a1SJoseph Chan 		} else if (i == AUTO_SEQ_SURROUND) {
2961c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Volume", chname[i]);
2962c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
29634483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2964f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2965c577b8a1SJoseph Chan 			if (err < 0)
2966c577b8a1SJoseph Chan 				return err;
2967c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Switch", chname[i]);
2968c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
29694483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2970f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2971c577b8a1SJoseph Chan 			if (err < 0)
2972c577b8a1SJoseph Chan 				return err;
2973c577b8a1SJoseph Chan 		} else if (i == AUTO_SEQ_SIDE) {
2974c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Volume", chname[i]);
2975c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
29764483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2977f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2978c577b8a1SJoseph Chan 			if (err < 0)
2979c577b8a1SJoseph Chan 				return err;
2980c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Switch", chname[i]);
2981c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
29824483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2983f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2984c577b8a1SJoseph Chan 			if (err < 0)
2985c577b8a1SJoseph Chan 				return err;
2986c577b8a1SJoseph Chan 		}
2987c577b8a1SJoseph Chan 	}
2988c577b8a1SJoseph Chan 
2989c577b8a1SJoseph Chan 	return 0;
2990c577b8a1SJoseph Chan }
2991c577b8a1SJoseph Chan 
2992c577b8a1SJoseph Chan static int vt1709_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
2993c577b8a1SJoseph Chan {
2994c577b8a1SJoseph Chan 	int err;
2995c577b8a1SJoseph Chan 
2996c577b8a1SJoseph Chan 	if (!pin)
2997c577b8a1SJoseph Chan 		return 0;
2998c577b8a1SJoseph Chan 
2999c577b8a1SJoseph Chan 	if (spec->multiout.num_dacs == 5) /* 10 channels */
3000c577b8a1SJoseph Chan 		spec->multiout.hp_nid = VT1709_HP_DAC_NID;
3001c577b8a1SJoseph Chan 	else if (spec->multiout.num_dacs == 3) /* 6 channels */
3002c577b8a1SJoseph Chan 		spec->multiout.hp_nid = 0;
3003cdc1784dSLydia Wang 	spec->hp_independent_mode_index = 1;
3004c577b8a1SJoseph Chan 
3005c577b8a1SJoseph Chan 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3006c577b8a1SJoseph Chan 			      "Headphone Playback Volume",
3007c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3008c577b8a1SJoseph Chan 	if (err < 0)
3009c577b8a1SJoseph Chan 		return err;
3010c577b8a1SJoseph Chan 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3011c577b8a1SJoseph Chan 			      "Headphone Playback Switch",
3012c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3013c577b8a1SJoseph Chan 	if (err < 0)
3014c577b8a1SJoseph Chan 		return err;
3015c577b8a1SJoseph Chan 
3016c577b8a1SJoseph Chan 	return 0;
3017c577b8a1SJoseph Chan }
3018c577b8a1SJoseph Chan 
3019c577b8a1SJoseph Chan /* create playback/capture controls for input pins */
3020*10a20af7STakashi Iwai static int vt1709_auto_create_analog_input_ctls(struct hda_codec *codec,
3021c577b8a1SJoseph Chan 						const struct auto_pin_cfg *cfg)
3022c577b8a1SJoseph Chan {
3023f3268512STakashi Iwai 	static hda_nid_t pin_idxs[] = { 0xff, 0x23, 0x1d, 0x1e, 0x21 };
3024*10a20af7STakashi Iwai 	return vt_auto_create_analog_input_ctls(codec, cfg, 0x18, pin_idxs,
3025f3268512STakashi Iwai 						ARRAY_SIZE(pin_idxs));
3026c577b8a1SJoseph Chan }
3027c577b8a1SJoseph Chan 
3028c577b8a1SJoseph Chan static int vt1709_parse_auto_config(struct hda_codec *codec)
3029c577b8a1SJoseph Chan {
3030c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
3031c577b8a1SJoseph Chan 	int err;
3032c577b8a1SJoseph Chan 
3033c577b8a1SJoseph Chan 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3034c577b8a1SJoseph Chan 	if (err < 0)
3035c577b8a1SJoseph Chan 		return err;
3036c577b8a1SJoseph Chan 	err = vt1709_auto_fill_dac_nids(spec, &spec->autocfg);
3037c577b8a1SJoseph Chan 	if (err < 0)
3038c577b8a1SJoseph Chan 		return err;
3039c577b8a1SJoseph Chan 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3040c577b8a1SJoseph Chan 		return 0; /* can't find valid BIOS pin config */
3041c577b8a1SJoseph Chan 
3042c577b8a1SJoseph Chan 	err = vt1709_auto_create_multi_out_ctls(spec, &spec->autocfg);
3043c577b8a1SJoseph Chan 	if (err < 0)
3044c577b8a1SJoseph Chan 		return err;
3045c577b8a1SJoseph Chan 	err = vt1709_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
3046c577b8a1SJoseph Chan 	if (err < 0)
3047c577b8a1SJoseph Chan 		return err;
3048*10a20af7STakashi Iwai 	err = vt1709_auto_create_analog_input_ctls(codec, &spec->autocfg);
3049c577b8a1SJoseph Chan 	if (err < 0)
3050c577b8a1SJoseph Chan 		return err;
3051c577b8a1SJoseph Chan 
3052c577b8a1SJoseph Chan 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3053c577b8a1SJoseph Chan 
30540852d7a6STakashi Iwai 	if (spec->autocfg.dig_outs)
3055c577b8a1SJoseph Chan 		spec->multiout.dig_out_nid = VT1709_DIGOUT_NID;
305655d1d6c1STakashi Iwai 	spec->dig_in_pin = VT1709_DIGIN_PIN;
3057c577b8a1SJoseph Chan 	if (spec->autocfg.dig_in_pin)
3058c577b8a1SJoseph Chan 		spec->dig_in_nid = VT1709_DIGIN_NID;
3059c577b8a1SJoseph Chan 
3060603c4019STakashi Iwai 	if (spec->kctls.list)
3061603c4019STakashi Iwai 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
3062c577b8a1SJoseph Chan 
30630aa62aefSHarald Welte 	spec->input_mux = &spec->private_imux[0];
3064c577b8a1SJoseph Chan 
3065f8fdd495SHarald Welte 	if (spec->hp_mux)
30663d83e577STakashi Iwai 		via_hp_build(codec);
3067f8fdd495SHarald Welte 
30685b0cb1d8SJaroslav Kysela 	via_smart51_build(spec);
3069c577b8a1SJoseph Chan 	return 1;
3070c577b8a1SJoseph Chan }
3071c577b8a1SJoseph Chan 
3072cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
3073cb53c626STakashi Iwai static struct hda_amp_list vt1709_loopbacks[] = {
3074cb53c626STakashi Iwai 	{ 0x18, HDA_INPUT, 1 },
3075cb53c626STakashi Iwai 	{ 0x18, HDA_INPUT, 2 },
3076cb53c626STakashi Iwai 	{ 0x18, HDA_INPUT, 3 },
3077cb53c626STakashi Iwai 	{ 0x18, HDA_INPUT, 4 },
3078cb53c626STakashi Iwai 	{ } /* end */
3079cb53c626STakashi Iwai };
3080cb53c626STakashi Iwai #endif
3081cb53c626STakashi Iwai 
3082c577b8a1SJoseph Chan static int patch_vt1709_10ch(struct hda_codec *codec)
3083c577b8a1SJoseph Chan {
3084c577b8a1SJoseph Chan 	struct via_spec *spec;
3085c577b8a1SJoseph Chan 	int err;
3086c577b8a1SJoseph Chan 
3087c577b8a1SJoseph Chan 	/* create a codec specific record */
30885b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3089c577b8a1SJoseph Chan 	if (spec == NULL)
3090c577b8a1SJoseph Chan 		return -ENOMEM;
3091c577b8a1SJoseph Chan 
3092c577b8a1SJoseph Chan 	err = vt1709_parse_auto_config(codec);
3093c577b8a1SJoseph Chan 	if (err < 0) {
3094c577b8a1SJoseph Chan 		via_free(codec);
3095c577b8a1SJoseph Chan 		return err;
3096c577b8a1SJoseph Chan 	} else if (!err) {
3097c577b8a1SJoseph Chan 		printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
3098c577b8a1SJoseph Chan 		       "Using genenic mode...\n");
3099c577b8a1SJoseph Chan 	}
3100c577b8a1SJoseph Chan 
310169e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1709_10ch_volume_init_verbs;
310269e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
3103c577b8a1SJoseph Chan 
3104c577b8a1SJoseph Chan 	spec->stream_name_analog = "VT1709 Analog";
3105c577b8a1SJoseph Chan 	spec->stream_analog_playback = &vt1709_10ch_pcm_analog_playback;
3106c577b8a1SJoseph Chan 	spec->stream_analog_capture = &vt1709_pcm_analog_capture;
3107c577b8a1SJoseph Chan 
3108c577b8a1SJoseph Chan 	spec->stream_name_digital = "VT1709 Digital";
3109c577b8a1SJoseph Chan 	spec->stream_digital_playback = &vt1709_pcm_digital_playback;
3110c577b8a1SJoseph Chan 	spec->stream_digital_capture = &vt1709_pcm_digital_capture;
3111c577b8a1SJoseph Chan 
3112c577b8a1SJoseph Chan 
3113c577b8a1SJoseph Chan 	if (!spec->adc_nids && spec->input_mux) {
3114c577b8a1SJoseph Chan 		spec->adc_nids = vt1709_adc_nids;
3115c577b8a1SJoseph Chan 		spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
3116337b9d02STakashi Iwai 		get_mux_nids(codec);
3117c577b8a1SJoseph Chan 		spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
3118c577b8a1SJoseph Chan 		spec->num_mixers++;
3119c577b8a1SJoseph Chan 	}
3120c577b8a1SJoseph Chan 
3121c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
3122c577b8a1SJoseph Chan 
3123c577b8a1SJoseph Chan 	codec->patch_ops.init = via_auto_init;
312469e52a80SHarald Welte 	codec->patch_ops.unsol_event = via_unsol_event;
3125cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
3126cb53c626STakashi Iwai 	spec->loopback.amplist = vt1709_loopbacks;
3127cb53c626STakashi Iwai #endif
3128c577b8a1SJoseph Chan 
3129c577b8a1SJoseph Chan 	return 0;
3130c577b8a1SJoseph Chan }
3131c577b8a1SJoseph Chan /*
3132c577b8a1SJoseph Chan  * generic initialization of ADC, input mixers and output mixers
3133c577b8a1SJoseph Chan  */
3134c577b8a1SJoseph Chan static struct hda_verb vt1709_6ch_volume_init_verbs[] = {
3135c577b8a1SJoseph Chan 	/*
3136c577b8a1SJoseph Chan 	 * Unmute ADC0-2 and set the default input to mic-in
3137c577b8a1SJoseph Chan 	 */
3138c577b8a1SJoseph Chan 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3139c577b8a1SJoseph Chan 	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3140c577b8a1SJoseph Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3141c577b8a1SJoseph Chan 
3142c577b8a1SJoseph Chan 
3143c577b8a1SJoseph Chan 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3144c577b8a1SJoseph Chan 	 * mixer widget
3145c577b8a1SJoseph Chan 	 */
3146c577b8a1SJoseph Chan 	/* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3147c577b8a1SJoseph Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3148c577b8a1SJoseph Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3149c577b8a1SJoseph Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3150c577b8a1SJoseph Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3151c577b8a1SJoseph Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
3152c577b8a1SJoseph Chan 
3153c577b8a1SJoseph Chan 	/*
3154c577b8a1SJoseph Chan 	 * Set up output selector (0x1a, 0x1b, 0x29)
3155c577b8a1SJoseph Chan 	 */
3156c577b8a1SJoseph Chan 	/* set vol=0 to output mixers */
3157c577b8a1SJoseph Chan 	{0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3158c577b8a1SJoseph Chan 	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3159c577b8a1SJoseph Chan 	{0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3160c577b8a1SJoseph Chan 
3161c577b8a1SJoseph Chan 	/*
3162c577b8a1SJoseph Chan 	 *  Unmute PW3 and PW4
3163c577b8a1SJoseph Chan 	 */
3164c577b8a1SJoseph Chan 	{0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3165c577b8a1SJoseph Chan 	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3166c577b8a1SJoseph Chan 
3167c577b8a1SJoseph Chan 	/* Set input of PW4 as MW0 */
3168c577b8a1SJoseph Chan 	{0x20, AC_VERB_SET_CONNECT_SEL, 0},
3169c577b8a1SJoseph Chan 	/* PW9 Output enable */
3170c577b8a1SJoseph Chan 	{0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3171c577b8a1SJoseph Chan 	{ }
3172c577b8a1SJoseph Chan };
3173c577b8a1SJoseph Chan 
3174c577b8a1SJoseph Chan static int patch_vt1709_6ch(struct hda_codec *codec)
3175c577b8a1SJoseph Chan {
3176c577b8a1SJoseph Chan 	struct via_spec *spec;
3177c577b8a1SJoseph Chan 	int err;
3178c577b8a1SJoseph Chan 
3179c577b8a1SJoseph Chan 	/* create a codec specific record */
31805b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3181c577b8a1SJoseph Chan 	if (spec == NULL)
3182c577b8a1SJoseph Chan 		return -ENOMEM;
3183c577b8a1SJoseph Chan 
3184c577b8a1SJoseph Chan 	err = vt1709_parse_auto_config(codec);
3185c577b8a1SJoseph Chan 	if (err < 0) {
3186c577b8a1SJoseph Chan 		via_free(codec);
3187c577b8a1SJoseph Chan 		return err;
3188c577b8a1SJoseph Chan 	} else if (!err) {
3189c577b8a1SJoseph Chan 		printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
3190c577b8a1SJoseph Chan 		       "Using genenic mode...\n");
3191c577b8a1SJoseph Chan 	}
3192c577b8a1SJoseph Chan 
319369e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1709_6ch_volume_init_verbs;
319469e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
3195c577b8a1SJoseph Chan 
3196c577b8a1SJoseph Chan 	spec->stream_name_analog = "VT1709 Analog";
3197c577b8a1SJoseph Chan 	spec->stream_analog_playback = &vt1709_6ch_pcm_analog_playback;
3198c577b8a1SJoseph Chan 	spec->stream_analog_capture = &vt1709_pcm_analog_capture;
3199c577b8a1SJoseph Chan 
3200c577b8a1SJoseph Chan 	spec->stream_name_digital = "VT1709 Digital";
3201c577b8a1SJoseph Chan 	spec->stream_digital_playback = &vt1709_pcm_digital_playback;
3202c577b8a1SJoseph Chan 	spec->stream_digital_capture = &vt1709_pcm_digital_capture;
3203c577b8a1SJoseph Chan 
3204c577b8a1SJoseph Chan 
3205c577b8a1SJoseph Chan 	if (!spec->adc_nids && spec->input_mux) {
3206c577b8a1SJoseph Chan 		spec->adc_nids = vt1709_adc_nids;
3207c577b8a1SJoseph Chan 		spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
3208337b9d02STakashi Iwai 		get_mux_nids(codec);
3209c577b8a1SJoseph Chan 		spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
3210c577b8a1SJoseph Chan 		spec->num_mixers++;
3211c577b8a1SJoseph Chan 	}
3212c577b8a1SJoseph Chan 
3213c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
3214c577b8a1SJoseph Chan 
3215c577b8a1SJoseph Chan 	codec->patch_ops.init = via_auto_init;
321669e52a80SHarald Welte 	codec->patch_ops.unsol_event = via_unsol_event;
3217cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
3218cb53c626STakashi Iwai 	spec->loopback.amplist = vt1709_loopbacks;
3219cb53c626STakashi Iwai #endif
3220f7278fd0SJosepch Chan 	return 0;
3221f7278fd0SJosepch Chan }
3222f7278fd0SJosepch Chan 
3223f7278fd0SJosepch Chan /* capture mixer elements */
3224f7278fd0SJosepch Chan static struct snd_kcontrol_new vt1708B_capture_mixer[] = {
3225f7278fd0SJosepch Chan 	HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
3226f7278fd0SJosepch Chan 	HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
3227f7278fd0SJosepch Chan 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
3228f7278fd0SJosepch Chan 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
3229f7278fd0SJosepch Chan 	{
3230f7278fd0SJosepch Chan 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3231f7278fd0SJosepch Chan 		/* The multiple "Capture Source" controls confuse alsamixer
3232f7278fd0SJosepch Chan 		 * So call somewhat different..
3233f7278fd0SJosepch Chan 		 */
3234f7278fd0SJosepch Chan 		/* .name = "Capture Source", */
3235f7278fd0SJosepch Chan 		.name = "Input Source",
3236f7278fd0SJosepch Chan 		.count = 1,
3237f7278fd0SJosepch Chan 		.info = via_mux_enum_info,
3238f7278fd0SJosepch Chan 		.get = via_mux_enum_get,
3239f7278fd0SJosepch Chan 		.put = via_mux_enum_put,
3240f7278fd0SJosepch Chan 	},
3241f7278fd0SJosepch Chan 	{ } /* end */
3242f7278fd0SJosepch Chan };
3243f7278fd0SJosepch Chan /*
3244f7278fd0SJosepch Chan  * generic initialization of ADC, input mixers and output mixers
3245f7278fd0SJosepch Chan  */
3246f7278fd0SJosepch Chan static struct hda_verb vt1708B_8ch_volume_init_verbs[] = {
3247f7278fd0SJosepch Chan 	/*
3248f7278fd0SJosepch Chan 	 * Unmute ADC0-1 and set the default input to mic-in
3249f7278fd0SJosepch Chan 	 */
3250f7278fd0SJosepch Chan 	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3251f7278fd0SJosepch Chan 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3252f7278fd0SJosepch Chan 
3253f7278fd0SJosepch Chan 
3254f7278fd0SJosepch Chan 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3255f7278fd0SJosepch Chan 	 * mixer widget
3256f7278fd0SJosepch Chan 	 */
3257f7278fd0SJosepch Chan 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3258f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3259f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3260f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3261f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3262f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
3263f7278fd0SJosepch Chan 
3264f7278fd0SJosepch Chan 	/*
3265f7278fd0SJosepch Chan 	 * Set up output mixers
3266f7278fd0SJosepch Chan 	 */
3267f7278fd0SJosepch Chan 	/* set vol=0 to output mixers */
3268f7278fd0SJosepch Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3269f7278fd0SJosepch Chan 	{0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3270f7278fd0SJosepch Chan 	{0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3271f7278fd0SJosepch Chan 
3272f7278fd0SJosepch Chan 	/* Setup default input to PW4 */
3273bfdc675aSLydia Wang 	{0x1d, AC_VERB_SET_CONNECT_SEL, 0},
3274f7278fd0SJosepch Chan 	/* PW9 Output enable */
3275f7278fd0SJosepch Chan 	{0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3276f7278fd0SJosepch Chan 	/* PW10 Input enable */
3277f7278fd0SJosepch Chan 	{0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
3278f7278fd0SJosepch Chan 	{ }
3279f7278fd0SJosepch Chan };
3280f7278fd0SJosepch Chan 
3281f7278fd0SJosepch Chan static struct hda_verb vt1708B_4ch_volume_init_verbs[] = {
3282f7278fd0SJosepch Chan 	/*
3283f7278fd0SJosepch Chan 	 * Unmute ADC0-1 and set the default input to mic-in
3284f7278fd0SJosepch Chan 	 */
3285f7278fd0SJosepch Chan 	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3286f7278fd0SJosepch Chan 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3287f7278fd0SJosepch Chan 
3288f7278fd0SJosepch Chan 
3289f7278fd0SJosepch Chan 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3290f7278fd0SJosepch Chan 	 * mixer widget
3291f7278fd0SJosepch Chan 	 */
3292f7278fd0SJosepch Chan 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3293f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3294f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3295f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3296f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3297f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
3298f7278fd0SJosepch Chan 
3299f7278fd0SJosepch Chan 	/*
3300f7278fd0SJosepch Chan 	 * Set up output mixers
3301f7278fd0SJosepch Chan 	 */
3302f7278fd0SJosepch Chan 	/* set vol=0 to output mixers */
3303f7278fd0SJosepch Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3304f7278fd0SJosepch Chan 	{0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3305f7278fd0SJosepch Chan 	{0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3306f7278fd0SJosepch Chan 
3307f7278fd0SJosepch Chan 	/* Setup default input of PW4 to MW0 */
3308f7278fd0SJosepch Chan 	{0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
3309f7278fd0SJosepch Chan 	/* PW9 Output enable */
3310f7278fd0SJosepch Chan 	{0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3311f7278fd0SJosepch Chan 	/* PW10 Input enable */
3312f7278fd0SJosepch Chan 	{0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
3313f7278fd0SJosepch Chan 	{ }
3314f7278fd0SJosepch Chan };
3315f7278fd0SJosepch Chan 
331669e52a80SHarald Welte static struct hda_verb vt1708B_uniwill_init_verbs[] = {
3317a34df19aSLydia Wang 	{0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
3318a34df19aSLydia Wang 	 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3319a34df19aSLydia Wang 	{0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3320a34df19aSLydia Wang 	{0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3321a34df19aSLydia Wang 	{0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3322a34df19aSLydia Wang 	{0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3323a34df19aSLydia Wang 	{0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3324a34df19aSLydia Wang 	{0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3325a34df19aSLydia Wang 	{0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
332669e52a80SHarald Welte 	{ }
332769e52a80SHarald Welte };
332869e52a80SHarald Welte 
332917314379SLydia Wang static int via_pcm_open_close(struct hda_pcm_stream *hinfo,
333017314379SLydia Wang 			      struct hda_codec *codec,
333117314379SLydia Wang 			      struct snd_pcm_substream *substream)
333217314379SLydia Wang {
333317314379SLydia Wang 	int idle = substream->pstr->substream_opened == 1
333417314379SLydia Wang 		&& substream->ref_count == 0;
333517314379SLydia Wang 
333617314379SLydia Wang 	analog_low_current_mode(codec, idle);
333717314379SLydia Wang 	return 0;
333817314379SLydia Wang }
333917314379SLydia Wang 
3340f7278fd0SJosepch Chan static struct hda_pcm_stream vt1708B_8ch_pcm_analog_playback = {
33410aa62aefSHarald Welte 	.substreams = 2,
3342f7278fd0SJosepch Chan 	.channels_min = 2,
3343f7278fd0SJosepch Chan 	.channels_max = 8,
3344f7278fd0SJosepch Chan 	.nid = 0x10, /* NID to query formats and rates */
3345f7278fd0SJosepch Chan 	.ops = {
3346f7278fd0SJosepch Chan 		.open = via_playback_pcm_open,
33470aa62aefSHarald Welte 		.prepare = via_playback_multi_pcm_prepare,
334817314379SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup,
334917314379SLydia Wang 		.close = via_pcm_open_close
3350f7278fd0SJosepch Chan 	},
3351f7278fd0SJosepch Chan };
3352f7278fd0SJosepch Chan 
3353f7278fd0SJosepch Chan static struct hda_pcm_stream vt1708B_4ch_pcm_analog_playback = {
33540aa62aefSHarald Welte 	.substreams = 2,
3355f7278fd0SJosepch Chan 	.channels_min = 2,
3356f7278fd0SJosepch Chan 	.channels_max = 4,
3357f7278fd0SJosepch Chan 	.nid = 0x10, /* NID to query formats and rates */
3358f7278fd0SJosepch Chan 	.ops = {
3359f7278fd0SJosepch Chan 		.open = via_playback_pcm_open,
33600aa62aefSHarald Welte 		.prepare = via_playback_multi_pcm_prepare,
33610aa62aefSHarald Welte 		.cleanup = via_playback_multi_pcm_cleanup
3362f7278fd0SJosepch Chan 	},
3363f7278fd0SJosepch Chan };
3364f7278fd0SJosepch Chan 
3365f7278fd0SJosepch Chan static struct hda_pcm_stream vt1708B_pcm_analog_capture = {
3366f7278fd0SJosepch Chan 	.substreams = 2,
3367f7278fd0SJosepch Chan 	.channels_min = 2,
3368f7278fd0SJosepch Chan 	.channels_max = 2,
3369f7278fd0SJosepch Chan 	.nid = 0x13, /* NID to query formats and rates */
3370f7278fd0SJosepch Chan 	.ops = {
337117314379SLydia Wang 		.open = via_pcm_open_close,
3372f7278fd0SJosepch Chan 		.prepare = via_capture_pcm_prepare,
337317314379SLydia Wang 		.cleanup = via_capture_pcm_cleanup,
337417314379SLydia Wang 		.close = via_pcm_open_close
3375f7278fd0SJosepch Chan 	},
3376f7278fd0SJosepch Chan };
3377f7278fd0SJosepch Chan 
3378f7278fd0SJosepch Chan static struct hda_pcm_stream vt1708B_pcm_digital_playback = {
3379f7278fd0SJosepch Chan 	.substreams = 1,
3380f7278fd0SJosepch Chan 	.channels_min = 2,
3381f7278fd0SJosepch Chan 	.channels_max = 2,
3382f7278fd0SJosepch Chan 	/* NID is set in via_build_pcms */
3383f7278fd0SJosepch Chan 	.ops = {
3384f7278fd0SJosepch Chan 		.open = via_dig_playback_pcm_open,
3385f7278fd0SJosepch Chan 		.close = via_dig_playback_pcm_close,
33869da29271STakashi Iwai 		.prepare = via_dig_playback_pcm_prepare,
33879da29271STakashi Iwai 		.cleanup = via_dig_playback_pcm_cleanup
3388f7278fd0SJosepch Chan 	},
3389f7278fd0SJosepch Chan };
3390f7278fd0SJosepch Chan 
3391f7278fd0SJosepch Chan static struct hda_pcm_stream vt1708B_pcm_digital_capture = {
3392f7278fd0SJosepch Chan 	.substreams = 1,
3393f7278fd0SJosepch Chan 	.channels_min = 2,
3394f7278fd0SJosepch Chan 	.channels_max = 2,
3395f7278fd0SJosepch Chan };
3396f7278fd0SJosepch Chan 
3397f7278fd0SJosepch Chan /* fill in the dac_nids table from the parsed pin configuration */
3398f7278fd0SJosepch Chan static int vt1708B_auto_fill_dac_nids(struct via_spec *spec,
3399f7278fd0SJosepch Chan 				     const struct auto_pin_cfg *cfg)
3400f7278fd0SJosepch Chan {
3401f7278fd0SJosepch Chan 	int i;
3402f7278fd0SJosepch Chan 	hda_nid_t nid;
3403f7278fd0SJosepch Chan 
3404f7278fd0SJosepch Chan 	spec->multiout.num_dacs = cfg->line_outs;
3405f7278fd0SJosepch Chan 
3406f7278fd0SJosepch Chan 	spec->multiout.dac_nids = spec->private_dac_nids;
3407f7278fd0SJosepch Chan 
3408f7278fd0SJosepch Chan 	for (i = 0; i < 4; i++) {
3409f7278fd0SJosepch Chan 		nid = cfg->line_out_pins[i];
3410f7278fd0SJosepch Chan 		if (nid) {
3411f7278fd0SJosepch Chan 			/* config dac list */
3412f7278fd0SJosepch Chan 			switch (i) {
3413f7278fd0SJosepch Chan 			case AUTO_SEQ_FRONT:
3414f7278fd0SJosepch Chan 				spec->multiout.dac_nids[i] = 0x10;
3415f7278fd0SJosepch Chan 				break;
3416f7278fd0SJosepch Chan 			case AUTO_SEQ_CENLFE:
3417f7278fd0SJosepch Chan 				spec->multiout.dac_nids[i] = 0x24;
3418f7278fd0SJosepch Chan 				break;
3419f7278fd0SJosepch Chan 			case AUTO_SEQ_SURROUND:
3420fb4cb772SHarald Welte 				spec->multiout.dac_nids[i] = 0x11;
3421f7278fd0SJosepch Chan 				break;
3422f7278fd0SJosepch Chan 			case AUTO_SEQ_SIDE:
3423fb4cb772SHarald Welte 				spec->multiout.dac_nids[i] = 0x25;
3424f7278fd0SJosepch Chan 				break;
3425f7278fd0SJosepch Chan 			}
3426f7278fd0SJosepch Chan 		}
3427f7278fd0SJosepch Chan 	}
3428f7278fd0SJosepch Chan 
3429f7278fd0SJosepch Chan 	return 0;
3430f7278fd0SJosepch Chan }
3431f7278fd0SJosepch Chan 
3432f7278fd0SJosepch Chan /* add playback controls from the parsed DAC table */
3433f7278fd0SJosepch Chan static int vt1708B_auto_create_multi_out_ctls(struct via_spec *spec,
3434f7278fd0SJosepch Chan 					     const struct auto_pin_cfg *cfg)
3435f7278fd0SJosepch Chan {
3436f7278fd0SJosepch Chan 	char name[32];
3437f7278fd0SJosepch Chan 	static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
3438fb4cb772SHarald Welte 	hda_nid_t nid_vols[] = {0x16, 0x18, 0x26, 0x27};
3439f7278fd0SJosepch Chan 	hda_nid_t nid, nid_vol = 0;
3440f7278fd0SJosepch Chan 	int i, err;
3441f7278fd0SJosepch Chan 
3442f7278fd0SJosepch Chan 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
3443f7278fd0SJosepch Chan 		nid = cfg->line_out_pins[i];
3444f7278fd0SJosepch Chan 
3445f7278fd0SJosepch Chan 		if (!nid)
3446f7278fd0SJosepch Chan 			continue;
3447f7278fd0SJosepch Chan 
3448f7278fd0SJosepch Chan 		nid_vol = nid_vols[i];
3449f7278fd0SJosepch Chan 
3450f7278fd0SJosepch Chan 		if (i == AUTO_SEQ_CENLFE) {
3451f7278fd0SJosepch Chan 			/* Center/LFE */
3452f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3453f7278fd0SJosepch Chan 					      "Center Playback Volume",
3454f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
3455f7278fd0SJosepch Chan 								  HDA_OUTPUT));
3456f7278fd0SJosepch Chan 			if (err < 0)
3457f7278fd0SJosepch Chan 				return err;
3458f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3459f7278fd0SJosepch Chan 					      "LFE Playback Volume",
3460f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
3461f7278fd0SJosepch Chan 								  HDA_OUTPUT));
3462f7278fd0SJosepch Chan 			if (err < 0)
3463f7278fd0SJosepch Chan 				return err;
3464f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3465f7278fd0SJosepch Chan 					      "Center Playback Switch",
3466f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
3467f7278fd0SJosepch Chan 								  HDA_OUTPUT));
3468f7278fd0SJosepch Chan 			if (err < 0)
3469f7278fd0SJosepch Chan 				return err;
3470f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3471f7278fd0SJosepch Chan 					      "LFE Playback Switch",
3472f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
3473f7278fd0SJosepch Chan 								  HDA_OUTPUT));
3474f7278fd0SJosepch Chan 			if (err < 0)
3475f7278fd0SJosepch Chan 				return err;
3476f7278fd0SJosepch Chan 		} else if (i == AUTO_SEQ_FRONT) {
3477f7278fd0SJosepch Chan 			/* add control to mixer index 0 */
3478f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3479f7278fd0SJosepch Chan 					      "Master Front Playback Volume",
3480f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3481f7278fd0SJosepch Chan 								  HDA_INPUT));
3482f7278fd0SJosepch Chan 			if (err < 0)
3483f7278fd0SJosepch Chan 				return err;
3484f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3485f7278fd0SJosepch Chan 					      "Master Front Playback Switch",
3486f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3487f7278fd0SJosepch Chan 								  HDA_INPUT));
3488f7278fd0SJosepch Chan 			if (err < 0)
3489f7278fd0SJosepch Chan 				return err;
3490f7278fd0SJosepch Chan 
3491f7278fd0SJosepch Chan 			/* add control to PW3 */
3492f7278fd0SJosepch Chan 			sprintf(name, "%s Playback Volume", chname[i]);
3493f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3494f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
3495f7278fd0SJosepch Chan 								  HDA_OUTPUT));
3496f7278fd0SJosepch Chan 			if (err < 0)
3497f7278fd0SJosepch Chan 				return err;
3498f7278fd0SJosepch Chan 			sprintf(name, "%s Playback Switch", chname[i]);
3499f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3500f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
3501f7278fd0SJosepch Chan 								  HDA_OUTPUT));
3502f7278fd0SJosepch Chan 			if (err < 0)
3503f7278fd0SJosepch Chan 				return err;
3504f7278fd0SJosepch Chan 		} else {
3505f7278fd0SJosepch Chan 			sprintf(name, "%s Playback Volume", chname[i]);
3506f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3507f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3508f7278fd0SJosepch Chan 								  HDA_OUTPUT));
3509f7278fd0SJosepch Chan 			if (err < 0)
3510f7278fd0SJosepch Chan 				return err;
3511f7278fd0SJosepch Chan 			sprintf(name, "%s Playback Switch", chname[i]);
3512f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3513f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3514f7278fd0SJosepch Chan 								  HDA_OUTPUT));
3515f7278fd0SJosepch Chan 			if (err < 0)
3516f7278fd0SJosepch Chan 				return err;
3517f7278fd0SJosepch Chan 		}
3518f7278fd0SJosepch Chan 	}
3519f7278fd0SJosepch Chan 
3520f7278fd0SJosepch Chan 	return 0;
3521f7278fd0SJosepch Chan }
3522f7278fd0SJosepch Chan 
3523f7278fd0SJosepch Chan static int vt1708B_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
3524f7278fd0SJosepch Chan {
3525f7278fd0SJosepch Chan 	int err;
3526f7278fd0SJosepch Chan 
3527f7278fd0SJosepch Chan 	if (!pin)
3528f7278fd0SJosepch Chan 		return 0;
3529f7278fd0SJosepch Chan 
3530f7278fd0SJosepch Chan 	spec->multiout.hp_nid = VT1708B_HP_NID; /* AOW3 */
3531cdc1784dSLydia Wang 	spec->hp_independent_mode_index = 1;
3532f7278fd0SJosepch Chan 
3533f7278fd0SJosepch Chan 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3534f7278fd0SJosepch Chan 			      "Headphone Playback Volume",
3535f7278fd0SJosepch Chan 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3536f7278fd0SJosepch Chan 	if (err < 0)
3537f7278fd0SJosepch Chan 		return err;
3538f7278fd0SJosepch Chan 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3539f7278fd0SJosepch Chan 			      "Headphone Playback Switch",
3540f7278fd0SJosepch Chan 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3541f7278fd0SJosepch Chan 	if (err < 0)
3542f7278fd0SJosepch Chan 		return err;
3543f7278fd0SJosepch Chan 
35440aa62aefSHarald Welte 	create_hp_imux(spec);
35450aa62aefSHarald Welte 
3546f7278fd0SJosepch Chan 	return 0;
3547f7278fd0SJosepch Chan }
3548f7278fd0SJosepch Chan 
3549f7278fd0SJosepch Chan /* create playback/capture controls for input pins */
3550*10a20af7STakashi Iwai static int vt1708B_auto_create_analog_input_ctls(struct hda_codec *codec,
3551f7278fd0SJosepch Chan 						const struct auto_pin_cfg *cfg)
3552f7278fd0SJosepch Chan {
3553f3268512STakashi Iwai 	static hda_nid_t pin_idxs[] = { 0xff, 0x1f, 0x1a, 0x1b, 0x1e };
3554*10a20af7STakashi Iwai 	return vt_auto_create_analog_input_ctls(codec, cfg, 0x16, pin_idxs,
3555f3268512STakashi Iwai 						ARRAY_SIZE(pin_idxs));
3556f7278fd0SJosepch Chan }
3557f7278fd0SJosepch Chan 
3558f7278fd0SJosepch Chan static int vt1708B_parse_auto_config(struct hda_codec *codec)
3559f7278fd0SJosepch Chan {
3560f7278fd0SJosepch Chan 	struct via_spec *spec = codec->spec;
3561f7278fd0SJosepch Chan 	int err;
3562f7278fd0SJosepch Chan 
3563f7278fd0SJosepch Chan 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3564f7278fd0SJosepch Chan 	if (err < 0)
3565f7278fd0SJosepch Chan 		return err;
3566f7278fd0SJosepch Chan 	err = vt1708B_auto_fill_dac_nids(spec, &spec->autocfg);
3567f7278fd0SJosepch Chan 	if (err < 0)
3568f7278fd0SJosepch Chan 		return err;
3569f7278fd0SJosepch Chan 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3570f7278fd0SJosepch Chan 		return 0; /* can't find valid BIOS pin config */
3571f7278fd0SJosepch Chan 
3572f7278fd0SJosepch Chan 	err = vt1708B_auto_create_multi_out_ctls(spec, &spec->autocfg);
3573f7278fd0SJosepch Chan 	if (err < 0)
3574f7278fd0SJosepch Chan 		return err;
3575f7278fd0SJosepch Chan 	err = vt1708B_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
3576f7278fd0SJosepch Chan 	if (err < 0)
3577f7278fd0SJosepch Chan 		return err;
3578*10a20af7STakashi Iwai 	err = vt1708B_auto_create_analog_input_ctls(codec, &spec->autocfg);
3579f7278fd0SJosepch Chan 	if (err < 0)
3580f7278fd0SJosepch Chan 		return err;
3581f7278fd0SJosepch Chan 
3582f7278fd0SJosepch Chan 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3583f7278fd0SJosepch Chan 
35840852d7a6STakashi Iwai 	if (spec->autocfg.dig_outs)
3585f7278fd0SJosepch Chan 		spec->multiout.dig_out_nid = VT1708B_DIGOUT_NID;
358655d1d6c1STakashi Iwai 	spec->dig_in_pin = VT1708B_DIGIN_PIN;
3587f7278fd0SJosepch Chan 	if (spec->autocfg.dig_in_pin)
3588f7278fd0SJosepch Chan 		spec->dig_in_nid = VT1708B_DIGIN_NID;
3589f7278fd0SJosepch Chan 
3590603c4019STakashi Iwai 	if (spec->kctls.list)
3591603c4019STakashi Iwai 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
3592f7278fd0SJosepch Chan 
35930aa62aefSHarald Welte 	spec->input_mux = &spec->private_imux[0];
35940aa62aefSHarald Welte 
3595f8fdd495SHarald Welte 	if (spec->hp_mux)
35963d83e577STakashi Iwai 		via_hp_build(codec);
3597f7278fd0SJosepch Chan 
35985b0cb1d8SJaroslav Kysela 	via_smart51_build(spec);
3599f7278fd0SJosepch Chan 	return 1;
3600f7278fd0SJosepch Chan }
3601f7278fd0SJosepch Chan 
3602f7278fd0SJosepch Chan #ifdef CONFIG_SND_HDA_POWER_SAVE
3603f7278fd0SJosepch Chan static struct hda_amp_list vt1708B_loopbacks[] = {
3604f7278fd0SJosepch Chan 	{ 0x16, HDA_INPUT, 1 },
3605f7278fd0SJosepch Chan 	{ 0x16, HDA_INPUT, 2 },
3606f7278fd0SJosepch Chan 	{ 0x16, HDA_INPUT, 3 },
3607f7278fd0SJosepch Chan 	{ 0x16, HDA_INPUT, 4 },
3608f7278fd0SJosepch Chan 	{ } /* end */
3609f7278fd0SJosepch Chan };
3610f7278fd0SJosepch Chan #endif
3611518bf3baSLydia Wang static int patch_vt1708S(struct hda_codec *codec);
3612f7278fd0SJosepch Chan static int patch_vt1708B_8ch(struct hda_codec *codec)
3613f7278fd0SJosepch Chan {
3614f7278fd0SJosepch Chan 	struct via_spec *spec;
3615f7278fd0SJosepch Chan 	int err;
3616f7278fd0SJosepch Chan 
3617518bf3baSLydia Wang 	if (get_codec_type(codec) == VT1708BCE)
3618518bf3baSLydia Wang 		return patch_vt1708S(codec);
3619f7278fd0SJosepch Chan 	/* create a codec specific record */
36205b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3621f7278fd0SJosepch Chan 	if (spec == NULL)
3622f7278fd0SJosepch Chan 		return -ENOMEM;
3623f7278fd0SJosepch Chan 
3624f7278fd0SJosepch Chan 	/* automatic parse from the BIOS config */
3625f7278fd0SJosepch Chan 	err = vt1708B_parse_auto_config(codec);
3626f7278fd0SJosepch Chan 	if (err < 0) {
3627f7278fd0SJosepch Chan 		via_free(codec);
3628f7278fd0SJosepch Chan 		return err;
3629f7278fd0SJosepch Chan 	} else if (!err) {
3630f7278fd0SJosepch Chan 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
3631f7278fd0SJosepch Chan 		       "from BIOS.  Using genenic mode...\n");
3632f7278fd0SJosepch Chan 	}
3633f7278fd0SJosepch Chan 
363469e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1708B_8ch_volume_init_verbs;
363569e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
3636f7278fd0SJosepch Chan 
3637f7278fd0SJosepch Chan 	spec->stream_name_analog = "VT1708B Analog";
3638f7278fd0SJosepch Chan 	spec->stream_analog_playback = &vt1708B_8ch_pcm_analog_playback;
3639f7278fd0SJosepch Chan 	spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
3640f7278fd0SJosepch Chan 
3641f7278fd0SJosepch Chan 	spec->stream_name_digital = "VT1708B Digital";
3642f7278fd0SJosepch Chan 	spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
3643f7278fd0SJosepch Chan 	spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
3644f7278fd0SJosepch Chan 
3645f7278fd0SJosepch Chan 	if (!spec->adc_nids && spec->input_mux) {
3646f7278fd0SJosepch Chan 		spec->adc_nids = vt1708B_adc_nids;
3647f7278fd0SJosepch Chan 		spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
3648337b9d02STakashi Iwai 		get_mux_nids(codec);
3649f7278fd0SJosepch Chan 		spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
3650f7278fd0SJosepch Chan 		spec->num_mixers++;
3651f7278fd0SJosepch Chan 	}
3652f7278fd0SJosepch Chan 
3653f7278fd0SJosepch Chan 	codec->patch_ops = via_patch_ops;
3654f7278fd0SJosepch Chan 
3655f7278fd0SJosepch Chan 	codec->patch_ops.init = via_auto_init;
365669e52a80SHarald Welte 	codec->patch_ops.unsol_event = via_unsol_event;
3657f7278fd0SJosepch Chan #ifdef CONFIG_SND_HDA_POWER_SAVE
3658f7278fd0SJosepch Chan 	spec->loopback.amplist = vt1708B_loopbacks;
3659f7278fd0SJosepch Chan #endif
3660f7278fd0SJosepch Chan 
3661f7278fd0SJosepch Chan 	return 0;
3662f7278fd0SJosepch Chan }
3663f7278fd0SJosepch Chan 
3664f7278fd0SJosepch Chan static int patch_vt1708B_4ch(struct hda_codec *codec)
3665f7278fd0SJosepch Chan {
3666f7278fd0SJosepch Chan 	struct via_spec *spec;
3667f7278fd0SJosepch Chan 	int err;
3668f7278fd0SJosepch Chan 
3669f7278fd0SJosepch Chan 	/* create a codec specific record */
36705b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3671f7278fd0SJosepch Chan 	if (spec == NULL)
3672f7278fd0SJosepch Chan 		return -ENOMEM;
3673f7278fd0SJosepch Chan 
3674f7278fd0SJosepch Chan 	/* automatic parse from the BIOS config */
3675f7278fd0SJosepch Chan 	err = vt1708B_parse_auto_config(codec);
3676f7278fd0SJosepch Chan 	if (err < 0) {
3677f7278fd0SJosepch Chan 		via_free(codec);
3678f7278fd0SJosepch Chan 		return err;
3679f7278fd0SJosepch Chan 	} else if (!err) {
3680f7278fd0SJosepch Chan 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
3681f7278fd0SJosepch Chan 		       "from BIOS.  Using genenic mode...\n");
3682f7278fd0SJosepch Chan 	}
3683f7278fd0SJosepch Chan 
368469e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1708B_4ch_volume_init_verbs;
368569e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
3686f7278fd0SJosepch Chan 
3687f7278fd0SJosepch Chan 	spec->stream_name_analog = "VT1708B Analog";
3688f7278fd0SJosepch Chan 	spec->stream_analog_playback = &vt1708B_4ch_pcm_analog_playback;
3689f7278fd0SJosepch Chan 	spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
3690f7278fd0SJosepch Chan 
3691f7278fd0SJosepch Chan 	spec->stream_name_digital = "VT1708B Digital";
3692f7278fd0SJosepch Chan 	spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
3693f7278fd0SJosepch Chan 	spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
3694f7278fd0SJosepch Chan 
3695f7278fd0SJosepch Chan 	if (!spec->adc_nids && spec->input_mux) {
3696f7278fd0SJosepch Chan 		spec->adc_nids = vt1708B_adc_nids;
3697f7278fd0SJosepch Chan 		spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
3698337b9d02STakashi Iwai 		get_mux_nids(codec);
3699f7278fd0SJosepch Chan 		spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
3700f7278fd0SJosepch Chan 		spec->num_mixers++;
3701f7278fd0SJosepch Chan 	}
3702f7278fd0SJosepch Chan 
3703f7278fd0SJosepch Chan 	codec->patch_ops = via_patch_ops;
3704f7278fd0SJosepch Chan 
3705f7278fd0SJosepch Chan 	codec->patch_ops.init = via_auto_init;
370669e52a80SHarald Welte 	codec->patch_ops.unsol_event = via_unsol_event;
3707f7278fd0SJosepch Chan #ifdef CONFIG_SND_HDA_POWER_SAVE
3708f7278fd0SJosepch Chan 	spec->loopback.amplist = vt1708B_loopbacks;
3709f7278fd0SJosepch Chan #endif
3710c577b8a1SJoseph Chan 
3711c577b8a1SJoseph Chan 	return 0;
3712c577b8a1SJoseph Chan }
3713c577b8a1SJoseph Chan 
3714d949cac1SHarald Welte /* Patch for VT1708S */
3715d949cac1SHarald Welte 
3716d949cac1SHarald Welte /* capture mixer elements */
3717d949cac1SHarald Welte static struct snd_kcontrol_new vt1708S_capture_mixer[] = {
3718d949cac1SHarald Welte 	HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
3719d949cac1SHarald Welte 	HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
3720d949cac1SHarald Welte 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
3721d949cac1SHarald Welte 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
37226369bcfcSLydia Wang 	HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x1A, 0x0, HDA_INPUT),
37236369bcfcSLydia Wang 	HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x1E, 0x0,
37246369bcfcSLydia Wang 			 HDA_INPUT),
3725d949cac1SHarald Welte 	{
3726d949cac1SHarald Welte 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3727d949cac1SHarald Welte 		/* The multiple "Capture Source" controls confuse alsamixer
3728d949cac1SHarald Welte 		 * So call somewhat different..
3729d949cac1SHarald Welte 		 */
3730d949cac1SHarald Welte 		/* .name = "Capture Source", */
3731d949cac1SHarald Welte 		.name = "Input Source",
3732d949cac1SHarald Welte 		.count = 1,
3733d949cac1SHarald Welte 		.info = via_mux_enum_info,
3734d949cac1SHarald Welte 		.get = via_mux_enum_get,
3735d949cac1SHarald Welte 		.put = via_mux_enum_put,
3736d949cac1SHarald Welte 	},
3737d949cac1SHarald Welte 	{ } /* end */
3738d949cac1SHarald Welte };
3739d949cac1SHarald Welte 
3740d949cac1SHarald Welte static struct hda_verb vt1708S_volume_init_verbs[] = {
3741d949cac1SHarald Welte 	/* Unmute ADC0-1 and set the default input to mic-in */
3742d949cac1SHarald Welte 	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3743d949cac1SHarald Welte 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3744d949cac1SHarald Welte 
3745d949cac1SHarald Welte 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the
3746d949cac1SHarald Welte 	 * analog-loopback mixer widget */
3747d949cac1SHarald Welte 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3748d949cac1SHarald Welte 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3749d949cac1SHarald Welte 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3750d949cac1SHarald Welte 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3751d949cac1SHarald Welte 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3752d949cac1SHarald Welte 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
3753d949cac1SHarald Welte 
3754d949cac1SHarald Welte 	/* Setup default input of PW4 to MW0 */
3755d949cac1SHarald Welte 	{0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
37565691ec7fSHarald Welte 	/* PW9, PW10  Output enable */
3757d949cac1SHarald Welte 	{0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
37585691ec7fSHarald Welte 	{0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3759d7426329SHarald Welte 	/* Enable Mic Boost Volume backdoor */
3760d7426329SHarald Welte 	{0x1, 0xf98, 0x1},
3761bc7e7e5cSLydia Wang 	/* don't bybass mixer */
3762bc7e7e5cSLydia Wang 	{0x1, 0xf88, 0xc0},
3763d949cac1SHarald Welte 	{ }
3764d949cac1SHarald Welte };
3765d949cac1SHarald Welte 
376669e52a80SHarald Welte static struct hda_verb vt1708S_uniwill_init_verbs[] = {
3767a34df19aSLydia Wang 	{0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
3768a34df19aSLydia Wang 	 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3769a34df19aSLydia Wang 	{0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3770a34df19aSLydia Wang 	{0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3771a34df19aSLydia Wang 	{0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3772a34df19aSLydia Wang 	{0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3773a34df19aSLydia Wang 	{0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3774a34df19aSLydia Wang 	{0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3775a34df19aSLydia Wang 	{0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
377669e52a80SHarald Welte 	{ }
377769e52a80SHarald Welte };
377869e52a80SHarald Welte 
3779d949cac1SHarald Welte static struct hda_pcm_stream vt1708S_pcm_analog_playback = {
3780d949cac1SHarald Welte 	.substreams = 2,
3781d949cac1SHarald Welte 	.channels_min = 2,
3782d949cac1SHarald Welte 	.channels_max = 8,
3783d949cac1SHarald Welte 	.nid = 0x10, /* NID to query formats and rates */
3784d949cac1SHarald Welte 	.ops = {
3785d949cac1SHarald Welte 		.open = via_playback_pcm_open,
3786c873cc25SLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
3787c873cc25SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup,
378817314379SLydia Wang 		.close = via_pcm_open_close
3789d949cac1SHarald Welte 	},
3790d949cac1SHarald Welte };
3791d949cac1SHarald Welte 
3792d949cac1SHarald Welte static struct hda_pcm_stream vt1708S_pcm_analog_capture = {
3793d949cac1SHarald Welte 	.substreams = 2,
3794d949cac1SHarald Welte 	.channels_min = 2,
3795d949cac1SHarald Welte 	.channels_max = 2,
3796d949cac1SHarald Welte 	.nid = 0x13, /* NID to query formats and rates */
3797d949cac1SHarald Welte 	.ops = {
379817314379SLydia Wang 		.open = via_pcm_open_close,
3799d949cac1SHarald Welte 		.prepare = via_capture_pcm_prepare,
380017314379SLydia Wang 		.cleanup = via_capture_pcm_cleanup,
380117314379SLydia Wang 		.close = via_pcm_open_close
3802d949cac1SHarald Welte 	},
3803d949cac1SHarald Welte };
3804d949cac1SHarald Welte 
3805d949cac1SHarald Welte static struct hda_pcm_stream vt1708S_pcm_digital_playback = {
38069da29271STakashi Iwai 	.substreams = 1,
3807d949cac1SHarald Welte 	.channels_min = 2,
3808d949cac1SHarald Welte 	.channels_max = 2,
3809d949cac1SHarald Welte 	/* NID is set in via_build_pcms */
3810d949cac1SHarald Welte 	.ops = {
3811d949cac1SHarald Welte 		.open = via_dig_playback_pcm_open,
3812d949cac1SHarald Welte 		.close = via_dig_playback_pcm_close,
38139da29271STakashi Iwai 		.prepare = via_dig_playback_pcm_prepare,
38149da29271STakashi Iwai 		.cleanup = via_dig_playback_pcm_cleanup
3815d949cac1SHarald Welte 	},
3816d949cac1SHarald Welte };
3817d949cac1SHarald Welte 
3818d949cac1SHarald Welte /* fill in the dac_nids table from the parsed pin configuration */
3819d949cac1SHarald Welte static int vt1708S_auto_fill_dac_nids(struct via_spec *spec,
3820d949cac1SHarald Welte 				     const struct auto_pin_cfg *cfg)
3821d949cac1SHarald Welte {
3822d949cac1SHarald Welte 	int i;
3823d949cac1SHarald Welte 	hda_nid_t nid;
3824d949cac1SHarald Welte 
3825d949cac1SHarald Welte 	spec->multiout.num_dacs = cfg->line_outs;
3826d949cac1SHarald Welte 
3827d949cac1SHarald Welte 	spec->multiout.dac_nids = spec->private_dac_nids;
3828d949cac1SHarald Welte 
3829d949cac1SHarald Welte 	for (i = 0; i < 4; i++) {
3830d949cac1SHarald Welte 		nid = cfg->line_out_pins[i];
3831d949cac1SHarald Welte 		if (nid) {
3832d949cac1SHarald Welte 			/* config dac list */
3833d949cac1SHarald Welte 			switch (i) {
3834d949cac1SHarald Welte 			case AUTO_SEQ_FRONT:
3835d949cac1SHarald Welte 				spec->multiout.dac_nids[i] = 0x10;
3836d949cac1SHarald Welte 				break;
3837d949cac1SHarald Welte 			case AUTO_SEQ_CENLFE:
3838d949cac1SHarald Welte 				spec->multiout.dac_nids[i] = 0x24;
3839d949cac1SHarald Welte 				break;
3840d949cac1SHarald Welte 			case AUTO_SEQ_SURROUND:
3841d949cac1SHarald Welte 				spec->multiout.dac_nids[i] = 0x11;
3842d949cac1SHarald Welte 				break;
3843d949cac1SHarald Welte 			case AUTO_SEQ_SIDE:
3844d949cac1SHarald Welte 				spec->multiout.dac_nids[i] = 0x25;
3845d949cac1SHarald Welte 				break;
3846d949cac1SHarald Welte 			}
3847d949cac1SHarald Welte 		}
3848d949cac1SHarald Welte 	}
3849d949cac1SHarald Welte 
385032e0191dSClemens Ladisch 	/* for Smart 5.1, line/mic inputs double as output pins */
385132e0191dSClemens Ladisch 	if (cfg->line_outs == 1) {
385232e0191dSClemens Ladisch 		spec->multiout.num_dacs = 3;
385332e0191dSClemens Ladisch 		spec->multiout.dac_nids[AUTO_SEQ_SURROUND] = 0x11;
385432e0191dSClemens Ladisch 		spec->multiout.dac_nids[AUTO_SEQ_CENLFE] = 0x24;
385532e0191dSClemens Ladisch 	}
385632e0191dSClemens Ladisch 
3857d949cac1SHarald Welte 	return 0;
3858d949cac1SHarald Welte }
3859d949cac1SHarald Welte 
3860d949cac1SHarald Welte /* add playback controls from the parsed DAC table */
3861d949cac1SHarald Welte static int vt1708S_auto_create_multi_out_ctls(struct via_spec *spec,
3862d949cac1SHarald Welte 					     const struct auto_pin_cfg *cfg)
3863d949cac1SHarald Welte {
3864d949cac1SHarald Welte 	char name[32];
3865d949cac1SHarald Welte 	static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
3866d949cac1SHarald Welte 	hda_nid_t nid_vols[] = {0x10, 0x11, 0x24, 0x25};
3867d949cac1SHarald Welte 	hda_nid_t nid_mutes[] = {0x1C, 0x18, 0x26, 0x27};
3868d949cac1SHarald Welte 	hda_nid_t nid, nid_vol, nid_mute;
3869d949cac1SHarald Welte 	int i, err;
3870d949cac1SHarald Welte 
3871d949cac1SHarald Welte 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
3872d949cac1SHarald Welte 		nid = cfg->line_out_pins[i];
3873d949cac1SHarald Welte 
387432e0191dSClemens Ladisch 		/* for Smart 5.1, there are always at least six channels */
387532e0191dSClemens Ladisch 		if (!nid && i > AUTO_SEQ_CENLFE)
3876d949cac1SHarald Welte 			continue;
3877d949cac1SHarald Welte 
3878d949cac1SHarald Welte 		nid_vol = nid_vols[i];
3879d949cac1SHarald Welte 		nid_mute = nid_mutes[i];
3880d949cac1SHarald Welte 
3881d949cac1SHarald Welte 		if (i == AUTO_SEQ_CENLFE) {
3882d949cac1SHarald Welte 			/* Center/LFE */
3883d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3884d949cac1SHarald Welte 					      "Center Playback Volume",
3885d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
3886d949cac1SHarald Welte 								  HDA_OUTPUT));
3887d949cac1SHarald Welte 			if (err < 0)
3888d949cac1SHarald Welte 				return err;
3889d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3890d949cac1SHarald Welte 					      "LFE Playback Volume",
3891d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
3892d949cac1SHarald Welte 								  HDA_OUTPUT));
3893d949cac1SHarald Welte 			if (err < 0)
3894d949cac1SHarald Welte 				return err;
3895d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3896d949cac1SHarald Welte 					      "Center Playback Switch",
3897d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(nid_mute,
3898d949cac1SHarald Welte 								  1, 0,
3899d949cac1SHarald Welte 								  HDA_OUTPUT));
3900d949cac1SHarald Welte 			if (err < 0)
3901d949cac1SHarald Welte 				return err;
3902d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3903d949cac1SHarald Welte 					      "LFE Playback Switch",
3904d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(nid_mute,
3905d949cac1SHarald Welte 								  2, 0,
3906d949cac1SHarald Welte 								  HDA_OUTPUT));
3907d949cac1SHarald Welte 			if (err < 0)
3908d949cac1SHarald Welte 				return err;
3909d949cac1SHarald Welte 		} else if (i == AUTO_SEQ_FRONT) {
3910d949cac1SHarald Welte 			/* add control to mixer index 0 */
3911d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3912d949cac1SHarald Welte 					      "Master Front Playback Volume",
3913d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
3914d949cac1SHarald Welte 								  HDA_INPUT));
3915d949cac1SHarald Welte 			if (err < 0)
3916d949cac1SHarald Welte 				return err;
3917d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3918d949cac1SHarald Welte 					      "Master Front Playback Switch",
3919d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
3920d949cac1SHarald Welte 								  HDA_INPUT));
3921d949cac1SHarald Welte 			if (err < 0)
3922d949cac1SHarald Welte 				return err;
3923d949cac1SHarald Welte 
3924d949cac1SHarald Welte 			/* Front */
3925d949cac1SHarald Welte 			sprintf(name, "%s Playback Volume", chname[i]);
3926d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3927d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3928d949cac1SHarald Welte 								  HDA_OUTPUT));
3929d949cac1SHarald Welte 			if (err < 0)
3930d949cac1SHarald Welte 				return err;
3931d949cac1SHarald Welte 			sprintf(name, "%s Playback Switch", chname[i]);
3932d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3933d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(nid_mute,
3934d949cac1SHarald Welte 								  3, 0,
3935d949cac1SHarald Welte 								  HDA_OUTPUT));
3936d949cac1SHarald Welte 			if (err < 0)
3937d949cac1SHarald Welte 				return err;
3938d949cac1SHarald Welte 		} else {
3939d949cac1SHarald Welte 			sprintf(name, "%s Playback Volume", chname[i]);
3940d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3941d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3942d949cac1SHarald Welte 								  HDA_OUTPUT));
3943d949cac1SHarald Welte 			if (err < 0)
3944d949cac1SHarald Welte 				return err;
3945d949cac1SHarald Welte 			sprintf(name, "%s Playback Switch", chname[i]);
3946d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3947d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(nid_mute,
3948d949cac1SHarald Welte 								  3, 0,
3949d949cac1SHarald Welte 								  HDA_OUTPUT));
3950d949cac1SHarald Welte 			if (err < 0)
3951d949cac1SHarald Welte 				return err;
3952d949cac1SHarald Welte 		}
3953d949cac1SHarald Welte 	}
3954d949cac1SHarald Welte 
3955d949cac1SHarald Welte 	return 0;
3956d949cac1SHarald Welte }
3957d949cac1SHarald Welte 
3958d949cac1SHarald Welte static int vt1708S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
3959d949cac1SHarald Welte {
3960d949cac1SHarald Welte 	int err;
3961d949cac1SHarald Welte 
3962d949cac1SHarald Welte 	if (!pin)
3963d949cac1SHarald Welte 		return 0;
3964d949cac1SHarald Welte 
3965d949cac1SHarald Welte 	spec->multiout.hp_nid = VT1708S_HP_NID; /* AOW3 */
3966cdc1784dSLydia Wang 	spec->hp_independent_mode_index = 1;
3967d949cac1SHarald Welte 
3968d949cac1SHarald Welte 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3969d949cac1SHarald Welte 			      "Headphone Playback Volume",
3970d949cac1SHarald Welte 			      HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
3971d949cac1SHarald Welte 	if (err < 0)
3972d949cac1SHarald Welte 		return err;
3973d949cac1SHarald Welte 
3974d949cac1SHarald Welte 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3975d949cac1SHarald Welte 			      "Headphone Playback Switch",
3976d949cac1SHarald Welte 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3977d949cac1SHarald Welte 	if (err < 0)
3978d949cac1SHarald Welte 		return err;
3979d949cac1SHarald Welte 
39800aa62aefSHarald Welte 	create_hp_imux(spec);
39810aa62aefSHarald Welte 
3982d949cac1SHarald Welte 	return 0;
3983d949cac1SHarald Welte }
3984d949cac1SHarald Welte 
3985d949cac1SHarald Welte /* create playback/capture controls for input pins */
3986*10a20af7STakashi Iwai static int vt1708S_auto_create_analog_input_ctls(struct hda_codec *codec,
3987d949cac1SHarald Welte 						const struct auto_pin_cfg *cfg)
3988d949cac1SHarald Welte {
3989f3268512STakashi Iwai 	static hda_nid_t pin_idxs[] = { 0x1f, 0x1a, 0x1b, 0x1e, 0, 0xff };
3990*10a20af7STakashi Iwai 	return vt_auto_create_analog_input_ctls(codec, cfg, 0x16, pin_idxs,
3991f3268512STakashi Iwai 						ARRAY_SIZE(pin_idxs));
3992d949cac1SHarald Welte }
3993d949cac1SHarald Welte 
39949da29271STakashi Iwai /* fill out digital output widgets; one for master and one for slave outputs */
39959da29271STakashi Iwai static void fill_dig_outs(struct hda_codec *codec)
39969da29271STakashi Iwai {
39979da29271STakashi Iwai 	struct via_spec *spec = codec->spec;
39989da29271STakashi Iwai 	int i;
39999da29271STakashi Iwai 
40009da29271STakashi Iwai 	for (i = 0; i < spec->autocfg.dig_outs; i++) {
40019da29271STakashi Iwai 		hda_nid_t nid;
40029da29271STakashi Iwai 		int conn;
40039da29271STakashi Iwai 
40049da29271STakashi Iwai 		nid = spec->autocfg.dig_out_pins[i];
40059da29271STakashi Iwai 		if (!nid)
40069da29271STakashi Iwai 			continue;
40079da29271STakashi Iwai 		conn = snd_hda_get_connections(codec, nid, &nid, 1);
40089da29271STakashi Iwai 		if (conn < 1)
40099da29271STakashi Iwai 			continue;
40109da29271STakashi Iwai 		if (!spec->multiout.dig_out_nid)
40119da29271STakashi Iwai 			spec->multiout.dig_out_nid = nid;
40129da29271STakashi Iwai 		else {
40139da29271STakashi Iwai 			spec->slave_dig_outs[0] = nid;
40149da29271STakashi Iwai 			break; /* at most two dig outs */
40159da29271STakashi Iwai 		}
40169da29271STakashi Iwai 	}
40179da29271STakashi Iwai }
40189da29271STakashi Iwai 
4019d949cac1SHarald Welte static int vt1708S_parse_auto_config(struct hda_codec *codec)
4020d949cac1SHarald Welte {
4021d949cac1SHarald Welte 	struct via_spec *spec = codec->spec;
4022d949cac1SHarald Welte 	int err;
4023d949cac1SHarald Welte 
40249da29271STakashi Iwai 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4025d949cac1SHarald Welte 	if (err < 0)
4026d949cac1SHarald Welte 		return err;
4027d949cac1SHarald Welte 	err = vt1708S_auto_fill_dac_nids(spec, &spec->autocfg);
4028d949cac1SHarald Welte 	if (err < 0)
4029d949cac1SHarald Welte 		return err;
4030d949cac1SHarald Welte 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
4031d949cac1SHarald Welte 		return 0; /* can't find valid BIOS pin config */
4032d949cac1SHarald Welte 
4033d949cac1SHarald Welte 	err = vt1708S_auto_create_multi_out_ctls(spec, &spec->autocfg);
4034d949cac1SHarald Welte 	if (err < 0)
4035d949cac1SHarald Welte 		return err;
4036d949cac1SHarald Welte 	err = vt1708S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
4037d949cac1SHarald Welte 	if (err < 0)
4038d949cac1SHarald Welte 		return err;
4039*10a20af7STakashi Iwai 	err = vt1708S_auto_create_analog_input_ctls(codec, &spec->autocfg);
4040d949cac1SHarald Welte 	if (err < 0)
4041d949cac1SHarald Welte 		return err;
4042d949cac1SHarald Welte 
4043d949cac1SHarald Welte 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4044d949cac1SHarald Welte 
40459da29271STakashi Iwai 	fill_dig_outs(codec);
404698aa34c0SHarald Welte 
4047603c4019STakashi Iwai 	if (spec->kctls.list)
4048603c4019STakashi Iwai 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
4049d949cac1SHarald Welte 
40500aa62aefSHarald Welte 	spec->input_mux = &spec->private_imux[0];
40510aa62aefSHarald Welte 
4052f8fdd495SHarald Welte 	if (spec->hp_mux)
40533d83e577STakashi Iwai 		via_hp_build(codec);
4054d949cac1SHarald Welte 
40555b0cb1d8SJaroslav Kysela 	via_smart51_build(spec);
4056d949cac1SHarald Welte 	return 1;
4057d949cac1SHarald Welte }
4058d949cac1SHarald Welte 
4059d949cac1SHarald Welte #ifdef CONFIG_SND_HDA_POWER_SAVE
4060d949cac1SHarald Welte static struct hda_amp_list vt1708S_loopbacks[] = {
4061d949cac1SHarald Welte 	{ 0x16, HDA_INPUT, 1 },
4062d949cac1SHarald Welte 	{ 0x16, HDA_INPUT, 2 },
4063d949cac1SHarald Welte 	{ 0x16, HDA_INPUT, 3 },
4064d949cac1SHarald Welte 	{ 0x16, HDA_INPUT, 4 },
4065d949cac1SHarald Welte 	{ } /* end */
4066d949cac1SHarald Welte };
4067d949cac1SHarald Welte #endif
4068d949cac1SHarald Welte 
40696369bcfcSLydia Wang static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
40706369bcfcSLydia Wang 			       int offset, int num_steps, int step_size)
40716369bcfcSLydia Wang {
40726369bcfcSLydia Wang 	snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
40736369bcfcSLydia Wang 				  (offset << AC_AMPCAP_OFFSET_SHIFT) |
40746369bcfcSLydia Wang 				  (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
40756369bcfcSLydia Wang 				  (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
40766369bcfcSLydia Wang 				  (0 << AC_AMPCAP_MUTE_SHIFT));
40776369bcfcSLydia Wang }
40786369bcfcSLydia Wang 
4079d949cac1SHarald Welte static int patch_vt1708S(struct hda_codec *codec)
4080d949cac1SHarald Welte {
4081d949cac1SHarald Welte 	struct via_spec *spec;
4082d949cac1SHarald Welte 	int err;
4083d949cac1SHarald Welte 
4084d949cac1SHarald Welte 	/* create a codec specific record */
40855b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
4086d949cac1SHarald Welte 	if (spec == NULL)
4087d949cac1SHarald Welte 		return -ENOMEM;
4088d949cac1SHarald Welte 
4089d949cac1SHarald Welte 	/* automatic parse from the BIOS config */
4090d949cac1SHarald Welte 	err = vt1708S_parse_auto_config(codec);
4091d949cac1SHarald Welte 	if (err < 0) {
4092d949cac1SHarald Welte 		via_free(codec);
4093d949cac1SHarald Welte 		return err;
4094d949cac1SHarald Welte 	} else if (!err) {
4095d949cac1SHarald Welte 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
4096d949cac1SHarald Welte 		       "from BIOS.  Using genenic mode...\n");
4097d949cac1SHarald Welte 	}
4098d949cac1SHarald Welte 
409969e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1708S_volume_init_verbs;
410069e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1708S_uniwill_init_verbs;
4101d949cac1SHarald Welte 
410236dd5c4aSLydia Wang 	if (codec->vendor_id == 0x11060440)
410336dd5c4aSLydia Wang 		spec->stream_name_analog = "VT1818S Analog";
410436dd5c4aSLydia Wang 	else
4105d949cac1SHarald Welte 		spec->stream_name_analog = "VT1708S Analog";
4106d949cac1SHarald Welte 	spec->stream_analog_playback = &vt1708S_pcm_analog_playback;
4107d949cac1SHarald Welte 	spec->stream_analog_capture = &vt1708S_pcm_analog_capture;
4108d949cac1SHarald Welte 
410936dd5c4aSLydia Wang 	if (codec->vendor_id == 0x11060440)
411036dd5c4aSLydia Wang 		spec->stream_name_digital = "VT1818S Digital";
411136dd5c4aSLydia Wang 	else
4112d949cac1SHarald Welte 		spec->stream_name_digital = "VT1708S Digital";
4113d949cac1SHarald Welte 	spec->stream_digital_playback = &vt1708S_pcm_digital_playback;
4114d949cac1SHarald Welte 
4115d949cac1SHarald Welte 	if (!spec->adc_nids && spec->input_mux) {
4116d949cac1SHarald Welte 		spec->adc_nids = vt1708S_adc_nids;
4117d949cac1SHarald Welte 		spec->num_adc_nids = ARRAY_SIZE(vt1708S_adc_nids);
4118337b9d02STakashi Iwai 		get_mux_nids(codec);
41196369bcfcSLydia Wang 		override_mic_boost(codec, 0x1a, 0, 3, 40);
41206369bcfcSLydia Wang 		override_mic_boost(codec, 0x1e, 0, 3, 40);
4121d949cac1SHarald Welte 		spec->mixers[spec->num_mixers] = vt1708S_capture_mixer;
4122d949cac1SHarald Welte 		spec->num_mixers++;
4123d949cac1SHarald Welte 	}
4124d949cac1SHarald Welte 
4125d949cac1SHarald Welte 	codec->patch_ops = via_patch_ops;
4126d949cac1SHarald Welte 
4127d949cac1SHarald Welte 	codec->patch_ops.init = via_auto_init;
412869e52a80SHarald Welte 	codec->patch_ops.unsol_event = via_unsol_event;
4129d949cac1SHarald Welte #ifdef CONFIG_SND_HDA_POWER_SAVE
4130d949cac1SHarald Welte 	spec->loopback.amplist = vt1708S_loopbacks;
4131d949cac1SHarald Welte #endif
4132d949cac1SHarald Welte 
4133518bf3baSLydia Wang 	/* correct names for VT1708BCE */
4134518bf3baSLydia Wang 	if (get_codec_type(codec) == VT1708BCE)	{
4135518bf3baSLydia Wang 		kfree(codec->chip_name);
4136518bf3baSLydia Wang 		codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
4137518bf3baSLydia Wang 		snprintf(codec->bus->card->mixername,
4138518bf3baSLydia Wang 			 sizeof(codec->bus->card->mixername),
4139518bf3baSLydia Wang 			 "%s %s", codec->vendor_name, codec->chip_name);
4140518bf3baSLydia Wang 		spec->stream_name_analog = "VT1708BCE Analog";
4141518bf3baSLydia Wang 		spec->stream_name_digital = "VT1708BCE Digital";
4142518bf3baSLydia Wang 	}
4143d949cac1SHarald Welte 	return 0;
4144d949cac1SHarald Welte }
4145d949cac1SHarald Welte 
4146d949cac1SHarald Welte /* Patch for VT1702 */
4147d949cac1SHarald Welte 
4148d949cac1SHarald Welte /* capture mixer elements */
4149d949cac1SHarald Welte static struct snd_kcontrol_new vt1702_capture_mixer[] = {
4150d949cac1SHarald Welte 	HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_INPUT),
4151d949cac1SHarald Welte 	HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_INPUT),
4152d949cac1SHarald Welte 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x20, 0x0, HDA_INPUT),
4153d949cac1SHarald Welte 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x20, 0x0, HDA_INPUT),
4154d949cac1SHarald Welte 	HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x1F, 0x0, HDA_INPUT),
4155d949cac1SHarald Welte 	HDA_CODEC_MUTE("Digital Mic Capture Switch", 0x1F, 0x0, HDA_INPUT),
4156d949cac1SHarald Welte 	HDA_CODEC_VOLUME("Digital Mic Boost Capture Volume", 0x1E, 0x0,
4157d949cac1SHarald Welte 			 HDA_INPUT),
4158d949cac1SHarald Welte 	{
4159d949cac1SHarald Welte 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4160d949cac1SHarald Welte 		/* The multiple "Capture Source" controls confuse alsamixer
4161d949cac1SHarald Welte 		 * So call somewhat different..
4162d949cac1SHarald Welte 		 */
4163d949cac1SHarald Welte 		/* .name = "Capture Source", */
4164d949cac1SHarald Welte 		.name = "Input Source",
4165d949cac1SHarald Welte 		.count = 1,
4166d949cac1SHarald Welte 		.info = via_mux_enum_info,
4167d949cac1SHarald Welte 		.get = via_mux_enum_get,
4168d949cac1SHarald Welte 		.put = via_mux_enum_put,
4169d949cac1SHarald Welte 	},
4170d949cac1SHarald Welte 	{ } /* end */
4171d949cac1SHarald Welte };
4172d949cac1SHarald Welte 
4173d949cac1SHarald Welte static struct hda_verb vt1702_volume_init_verbs[] = {
4174d949cac1SHarald Welte 	/*
4175d949cac1SHarald Welte 	 * Unmute ADC0-1 and set the default input to mic-in
4176d949cac1SHarald Welte 	 */
4177d949cac1SHarald Welte 	{0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4178d949cac1SHarald Welte 	{0x1F, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4179d949cac1SHarald Welte 	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4180d949cac1SHarald Welte 
4181d949cac1SHarald Welte 
4182d949cac1SHarald Welte 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
4183d949cac1SHarald Welte 	 * mixer widget
4184d949cac1SHarald Welte 	 */
4185d949cac1SHarald Welte 	/* Amp Indices: Mic1 = 1, Line = 1, Mic2 = 3 */
4186d949cac1SHarald Welte 	{0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4187d949cac1SHarald Welte 	{0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4188d949cac1SHarald Welte 	{0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
4189d949cac1SHarald Welte 	{0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
4190d949cac1SHarald Welte 	{0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
4191d949cac1SHarald Welte 
4192d949cac1SHarald Welte 	/* Setup default input of PW4 to MW0 */
4193d949cac1SHarald Welte 	{0x17, AC_VERB_SET_CONNECT_SEL, 0x1},
4194d949cac1SHarald Welte 	/* PW6 PW7 Output enable */
4195d949cac1SHarald Welte 	{0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
4196d949cac1SHarald Welte 	{0x1C, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
4197bc7e7e5cSLydia Wang 	/* mixer enable */
4198bc7e7e5cSLydia Wang 	{0x1, 0xF88, 0x3},
4199bc7e7e5cSLydia Wang 	/* GPIO 0~2 */
4200bc7e7e5cSLydia Wang 	{0x1, 0xF82, 0x3F},
4201d949cac1SHarald Welte 	{ }
4202d949cac1SHarald Welte };
4203d949cac1SHarald Welte 
420469e52a80SHarald Welte static struct hda_verb vt1702_uniwill_init_verbs[] = {
4205a34df19aSLydia Wang 	{0x17, AC_VERB_SET_UNSOLICITED_ENABLE,
4206a34df19aSLydia Wang 	 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
4207a34df19aSLydia Wang 	{0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4208a34df19aSLydia Wang 	{0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4209a34df19aSLydia Wang 	{0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4210a34df19aSLydia Wang 	{0x18, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
421169e52a80SHarald Welte 	{ }
421269e52a80SHarald Welte };
421369e52a80SHarald Welte 
4214d949cac1SHarald Welte static struct hda_pcm_stream vt1702_pcm_analog_playback = {
42150aa62aefSHarald Welte 	.substreams = 2,
4216d949cac1SHarald Welte 	.channels_min = 2,
4217d949cac1SHarald Welte 	.channels_max = 2,
4218d949cac1SHarald Welte 	.nid = 0x10, /* NID to query formats and rates */
4219d949cac1SHarald Welte 	.ops = {
4220d949cac1SHarald Welte 		.open = via_playback_pcm_open,
42210aa62aefSHarald Welte 		.prepare = via_playback_multi_pcm_prepare,
422217314379SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup,
422317314379SLydia Wang 		.close = via_pcm_open_close
4224d949cac1SHarald Welte 	},
4225d949cac1SHarald Welte };
4226d949cac1SHarald Welte 
4227d949cac1SHarald Welte static struct hda_pcm_stream vt1702_pcm_analog_capture = {
4228d949cac1SHarald Welte 	.substreams = 3,
4229d949cac1SHarald Welte 	.channels_min = 2,
4230d949cac1SHarald Welte 	.channels_max = 2,
4231d949cac1SHarald Welte 	.nid = 0x12, /* NID to query formats and rates */
4232d949cac1SHarald Welte 	.ops = {
423317314379SLydia Wang 		.open = via_pcm_open_close,
4234d949cac1SHarald Welte 		.prepare = via_capture_pcm_prepare,
423517314379SLydia Wang 		.cleanup = via_capture_pcm_cleanup,
423617314379SLydia Wang 		.close = via_pcm_open_close
4237d949cac1SHarald Welte 	},
4238d949cac1SHarald Welte };
4239d949cac1SHarald Welte 
4240d949cac1SHarald Welte static struct hda_pcm_stream vt1702_pcm_digital_playback = {
42415691ec7fSHarald Welte 	.substreams = 2,
4242d949cac1SHarald Welte 	.channels_min = 2,
4243d949cac1SHarald Welte 	.channels_max = 2,
4244d949cac1SHarald Welte 	/* NID is set in via_build_pcms */
4245d949cac1SHarald Welte 	.ops = {
4246d949cac1SHarald Welte 		.open = via_dig_playback_pcm_open,
4247d949cac1SHarald Welte 		.close = via_dig_playback_pcm_close,
42489da29271STakashi Iwai 		.prepare = via_dig_playback_pcm_prepare,
42499da29271STakashi Iwai 		.cleanup = via_dig_playback_pcm_cleanup
4250d949cac1SHarald Welte 	},
4251d949cac1SHarald Welte };
4252d949cac1SHarald Welte 
4253d949cac1SHarald Welte /* fill in the dac_nids table from the parsed pin configuration */
4254d949cac1SHarald Welte static int vt1702_auto_fill_dac_nids(struct via_spec *spec,
4255d949cac1SHarald Welte 				     const struct auto_pin_cfg *cfg)
4256d949cac1SHarald Welte {
4257d949cac1SHarald Welte 	spec->multiout.num_dacs = 1;
4258d949cac1SHarald Welte 	spec->multiout.dac_nids = spec->private_dac_nids;
4259d949cac1SHarald Welte 
4260d949cac1SHarald Welte 	if (cfg->line_out_pins[0]) {
4261d949cac1SHarald Welte 		/* config dac list */
4262d949cac1SHarald Welte 		spec->multiout.dac_nids[0] = 0x10;
4263d949cac1SHarald Welte 	}
4264d949cac1SHarald Welte 
4265d949cac1SHarald Welte 	return 0;
4266d949cac1SHarald Welte }
4267d949cac1SHarald Welte 
4268d949cac1SHarald Welte /* add playback controls from the parsed DAC table */
4269d949cac1SHarald Welte static int vt1702_auto_create_line_out_ctls(struct via_spec *spec,
4270d949cac1SHarald Welte 					     const struct auto_pin_cfg *cfg)
4271d949cac1SHarald Welte {
4272d949cac1SHarald Welte 	int err;
4273d949cac1SHarald Welte 
4274d949cac1SHarald Welte 	if (!cfg->line_out_pins[0])
4275d949cac1SHarald Welte 		return -1;
4276d949cac1SHarald Welte 
4277d949cac1SHarald Welte 	/* add control to mixer index 0 */
4278d949cac1SHarald Welte 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4279d949cac1SHarald Welte 			      "Master Front Playback Volume",
4280d949cac1SHarald Welte 			      HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
4281d949cac1SHarald Welte 	if (err < 0)
4282d949cac1SHarald Welte 		return err;
4283d949cac1SHarald Welte 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4284d949cac1SHarald Welte 			      "Master Front Playback Switch",
4285d949cac1SHarald Welte 			      HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
4286d949cac1SHarald Welte 	if (err < 0)
4287d949cac1SHarald Welte 		return err;
4288d949cac1SHarald Welte 
4289d949cac1SHarald Welte 	/* Front */
4290d949cac1SHarald Welte 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4291d949cac1SHarald Welte 			      "Front Playback Volume",
4292d949cac1SHarald Welte 			      HDA_COMPOSE_AMP_VAL(0x10, 3, 0, HDA_OUTPUT));
4293d949cac1SHarald Welte 	if (err < 0)
4294d949cac1SHarald Welte 		return err;
4295d949cac1SHarald Welte 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4296d949cac1SHarald Welte 			      "Front Playback Switch",
4297d949cac1SHarald Welte 			      HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_OUTPUT));
4298d949cac1SHarald Welte 	if (err < 0)
4299d949cac1SHarald Welte 		return err;
4300d949cac1SHarald Welte 
4301d949cac1SHarald Welte 	return 0;
4302d949cac1SHarald Welte }
4303d949cac1SHarald Welte 
4304d949cac1SHarald Welte static int vt1702_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
4305d949cac1SHarald Welte {
43060713efebSLydia Wang 	int err, i;
43070713efebSLydia Wang 	struct hda_input_mux *imux;
43080713efebSLydia Wang 	static const char *texts[] = { "ON", "OFF", NULL};
4309d949cac1SHarald Welte 	if (!pin)
4310d949cac1SHarald Welte 		return 0;
4311d949cac1SHarald Welte 	spec->multiout.hp_nid = 0x1D;
4312cdc1784dSLydia Wang 	spec->hp_independent_mode_index = 0;
4313d949cac1SHarald Welte 
4314d949cac1SHarald Welte 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4315d949cac1SHarald Welte 			      "Headphone Playback Volume",
4316d949cac1SHarald Welte 			      HDA_COMPOSE_AMP_VAL(0x1D, 3, 0, HDA_OUTPUT));
4317d949cac1SHarald Welte 	if (err < 0)
4318d949cac1SHarald Welte 		return err;
4319d949cac1SHarald Welte 
4320d949cac1SHarald Welte 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4321d949cac1SHarald Welte 			      "Headphone Playback Switch",
4322d949cac1SHarald Welte 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
4323d949cac1SHarald Welte 	if (err < 0)
4324d949cac1SHarald Welte 		return err;
4325d949cac1SHarald Welte 
43260713efebSLydia Wang 	imux = &spec->private_imux[1];
43270aa62aefSHarald Welte 
43280713efebSLydia Wang 	/* for hp mode select */
4329*10a20af7STakashi Iwai 	for (i = 0; texts[i]; i++)
4330*10a20af7STakashi Iwai 		snd_hda_add_imux_item(imux, texts[i], i, NULL);
43310713efebSLydia Wang 
43320713efebSLydia Wang 	spec->hp_mux = &spec->private_imux[1];
4333d949cac1SHarald Welte 	return 0;
4334d949cac1SHarald Welte }
4335d949cac1SHarald Welte 
4336d949cac1SHarald Welte /* create playback/capture controls for input pins */
4337*10a20af7STakashi Iwai static int vt1702_auto_create_analog_input_ctls(struct hda_codec *codec,
4338d949cac1SHarald Welte 						const struct auto_pin_cfg *cfg)
4339d949cac1SHarald Welte {
4340f3268512STakashi Iwai 	static hda_nid_t pin_idxs[] = { 0x14, 0x15, 0x18, 0xff };
4341*10a20af7STakashi Iwai 	return vt_auto_create_analog_input_ctls(codec, cfg, 0x1a, pin_idxs,
4342f3268512STakashi Iwai 						ARRAY_SIZE(pin_idxs));
4343d949cac1SHarald Welte }
4344d949cac1SHarald Welte 
4345d949cac1SHarald Welte static int vt1702_parse_auto_config(struct hda_codec *codec)
4346d949cac1SHarald Welte {
4347d949cac1SHarald Welte 	struct via_spec *spec = codec->spec;
4348d949cac1SHarald Welte 	int err;
4349d949cac1SHarald Welte 
43509da29271STakashi Iwai 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4351d949cac1SHarald Welte 	if (err < 0)
4352d949cac1SHarald Welte 		return err;
4353d949cac1SHarald Welte 	err = vt1702_auto_fill_dac_nids(spec, &spec->autocfg);
4354d949cac1SHarald Welte 	if (err < 0)
4355d949cac1SHarald Welte 		return err;
4356d949cac1SHarald Welte 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
4357d949cac1SHarald Welte 		return 0; /* can't find valid BIOS pin config */
4358d949cac1SHarald Welte 
4359d949cac1SHarald Welte 	err = vt1702_auto_create_line_out_ctls(spec, &spec->autocfg);
4360d949cac1SHarald Welte 	if (err < 0)
4361d949cac1SHarald Welte 		return err;
4362d949cac1SHarald Welte 	err = vt1702_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
4363d949cac1SHarald Welte 	if (err < 0)
4364d949cac1SHarald Welte 		return err;
4365c2c02ea3SLydia Wang 	/* limit AA path volume to 0 dB */
4366c2c02ea3SLydia Wang 	snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
4367c2c02ea3SLydia Wang 				  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
4368c2c02ea3SLydia Wang 				  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
4369c2c02ea3SLydia Wang 				  (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
4370c2c02ea3SLydia Wang 				  (1 << AC_AMPCAP_MUTE_SHIFT));
4371*10a20af7STakashi Iwai 	err = vt1702_auto_create_analog_input_ctls(codec, &spec->autocfg);
4372d949cac1SHarald Welte 	if (err < 0)
4373d949cac1SHarald Welte 		return err;
4374d949cac1SHarald Welte 
4375d949cac1SHarald Welte 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4376d949cac1SHarald Welte 
43779da29271STakashi Iwai 	fill_dig_outs(codec);
437898aa34c0SHarald Welte 
4379603c4019STakashi Iwai 	if (spec->kctls.list)
4380603c4019STakashi Iwai 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
4381d949cac1SHarald Welte 
43820aa62aefSHarald Welte 	spec->input_mux = &spec->private_imux[0];
43830aa62aefSHarald Welte 
4384f8fdd495SHarald Welte 	if (spec->hp_mux)
43853d83e577STakashi Iwai 		via_hp_build(codec);
4386d949cac1SHarald Welte 
4387d949cac1SHarald Welte 	return 1;
4388d949cac1SHarald Welte }
4389d949cac1SHarald Welte 
4390d949cac1SHarald Welte #ifdef CONFIG_SND_HDA_POWER_SAVE
4391d949cac1SHarald Welte static struct hda_amp_list vt1702_loopbacks[] = {
4392d949cac1SHarald Welte 	{ 0x1A, HDA_INPUT, 1 },
4393d949cac1SHarald Welte 	{ 0x1A, HDA_INPUT, 2 },
4394d949cac1SHarald Welte 	{ 0x1A, HDA_INPUT, 3 },
4395d949cac1SHarald Welte 	{ 0x1A, HDA_INPUT, 4 },
4396d949cac1SHarald Welte 	{ } /* end */
4397d949cac1SHarald Welte };
4398d949cac1SHarald Welte #endif
4399d949cac1SHarald Welte 
4400d949cac1SHarald Welte static int patch_vt1702(struct hda_codec *codec)
4401d949cac1SHarald Welte {
4402d949cac1SHarald Welte 	struct via_spec *spec;
4403d949cac1SHarald Welte 	int err;
4404d949cac1SHarald Welte 
4405d949cac1SHarald Welte 	/* create a codec specific record */
44065b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
4407d949cac1SHarald Welte 	if (spec == NULL)
4408d949cac1SHarald Welte 		return -ENOMEM;
4409d949cac1SHarald Welte 
4410d949cac1SHarald Welte 	/* automatic parse from the BIOS config */
4411d949cac1SHarald Welte 	err = vt1702_parse_auto_config(codec);
4412d949cac1SHarald Welte 	if (err < 0) {
4413d949cac1SHarald Welte 		via_free(codec);
4414d949cac1SHarald Welte 		return err;
4415d949cac1SHarald Welte 	} else if (!err) {
4416d949cac1SHarald Welte 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
4417d949cac1SHarald Welte 		       "from BIOS.  Using genenic mode...\n");
4418d949cac1SHarald Welte 	}
4419d949cac1SHarald Welte 
442069e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1702_volume_init_verbs;
442169e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1702_uniwill_init_verbs;
4422d949cac1SHarald Welte 
4423d949cac1SHarald Welte 	spec->stream_name_analog = "VT1702 Analog";
4424d949cac1SHarald Welte 	spec->stream_analog_playback = &vt1702_pcm_analog_playback;
4425d949cac1SHarald Welte 	spec->stream_analog_capture = &vt1702_pcm_analog_capture;
4426d949cac1SHarald Welte 
4427d949cac1SHarald Welte 	spec->stream_name_digital = "VT1702 Digital";
4428d949cac1SHarald Welte 	spec->stream_digital_playback = &vt1702_pcm_digital_playback;
4429d949cac1SHarald Welte 
4430d949cac1SHarald Welte 	if (!spec->adc_nids && spec->input_mux) {
4431d949cac1SHarald Welte 		spec->adc_nids = vt1702_adc_nids;
4432d949cac1SHarald Welte 		spec->num_adc_nids = ARRAY_SIZE(vt1702_adc_nids);
4433337b9d02STakashi Iwai 		get_mux_nids(codec);
4434d949cac1SHarald Welte 		spec->mixers[spec->num_mixers] = vt1702_capture_mixer;
4435d949cac1SHarald Welte 		spec->num_mixers++;
4436d949cac1SHarald Welte 	}
4437d949cac1SHarald Welte 
4438d949cac1SHarald Welte 	codec->patch_ops = via_patch_ops;
4439d949cac1SHarald Welte 
4440d949cac1SHarald Welte 	codec->patch_ops.init = via_auto_init;
444169e52a80SHarald Welte 	codec->patch_ops.unsol_event = via_unsol_event;
4442d949cac1SHarald Welte #ifdef CONFIG_SND_HDA_POWER_SAVE
4443d949cac1SHarald Welte 	spec->loopback.amplist = vt1702_loopbacks;
4444d949cac1SHarald Welte #endif
4445d949cac1SHarald Welte 
4446d949cac1SHarald Welte 	return 0;
4447d949cac1SHarald Welte }
4448d949cac1SHarald Welte 
4449eb7188caSLydia Wang /* Patch for VT1718S */
4450eb7188caSLydia Wang 
4451eb7188caSLydia Wang /* capture mixer elements */
4452eb7188caSLydia Wang static struct snd_kcontrol_new vt1718S_capture_mixer[] = {
4453eb7188caSLydia Wang 	HDA_CODEC_VOLUME("Capture Volume", 0x10, 0x0, HDA_INPUT),
4454eb7188caSLydia Wang 	HDA_CODEC_MUTE("Capture Switch", 0x10, 0x0, HDA_INPUT),
4455eb7188caSLydia Wang 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x11, 0x0, HDA_INPUT),
4456eb7188caSLydia Wang 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x11, 0x0, HDA_INPUT),
4457eb7188caSLydia Wang 	HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x2b, 0x0, HDA_INPUT),
4458eb7188caSLydia Wang 	HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x29, 0x0,
4459eb7188caSLydia Wang 			 HDA_INPUT),
4460eb7188caSLydia Wang 	{
4461eb7188caSLydia Wang 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4462eb7188caSLydia Wang 		/* The multiple "Capture Source" controls confuse alsamixer
4463eb7188caSLydia Wang 		 * So call somewhat different..
4464eb7188caSLydia Wang 		 */
4465eb7188caSLydia Wang 		.name = "Input Source",
4466eb7188caSLydia Wang 		.count = 2,
4467eb7188caSLydia Wang 		.info = via_mux_enum_info,
4468eb7188caSLydia Wang 		.get = via_mux_enum_get,
4469eb7188caSLydia Wang 		.put = via_mux_enum_put,
4470eb7188caSLydia Wang 	},
4471eb7188caSLydia Wang 	{ } /* end */
4472eb7188caSLydia Wang };
4473eb7188caSLydia Wang 
4474eb7188caSLydia Wang static struct hda_verb vt1718S_volume_init_verbs[] = {
4475eb7188caSLydia Wang 	/*
4476eb7188caSLydia Wang 	 * Unmute ADC0-1 and set the default input to mic-in
4477eb7188caSLydia Wang 	 */
4478eb7188caSLydia Wang 	{0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4479eb7188caSLydia Wang 	{0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4480eb7188caSLydia Wang 
4481eb7188caSLydia Wang 
4482eb7188caSLydia Wang 	/* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
4483eb7188caSLydia Wang 	 * mixer widget
4484eb7188caSLydia Wang 	 */
4485eb7188caSLydia Wang 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
4486eb7188caSLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4487eb7188caSLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4488eb7188caSLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
4489eb7188caSLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
4490eb7188caSLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
4491eb7188caSLydia Wang 
4492eb7188caSLydia Wang 	/* Setup default input of Front HP to MW9 */
4493eb7188caSLydia Wang 	{0x28, AC_VERB_SET_CONNECT_SEL, 0x1},
4494eb7188caSLydia Wang 	/* PW9 PW10 Output enable */
4495eb7188caSLydia Wang 	{0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
4496eb7188caSLydia Wang 	{0x2e, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
4497eb7188caSLydia Wang 	/* PW11 Input enable */
4498eb7188caSLydia Wang 	{0x2f, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_IN_EN},
4499eb7188caSLydia Wang 	/* Enable Boost Volume backdoor */
4500eb7188caSLydia Wang 	{0x1, 0xf88, 0x8},
4501eb7188caSLydia Wang 	/* MW0/1/2/3/4: un-mute index 0 (AOWx), mute index 1 (MW9) */
4502eb7188caSLydia Wang 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4503eb7188caSLydia Wang 	{0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4504eb7188caSLydia Wang 	{0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4505eb7188caSLydia Wang 	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4506eb7188caSLydia Wang 	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4507eb7188caSLydia Wang 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4508eb7188caSLydia Wang 	{0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4509eb7188caSLydia Wang 	{0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4510eb7188caSLydia Wang 	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4511eb7188caSLydia Wang 	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4512eb7188caSLydia Wang 	/* set MUX1 = 2 (AOW4), MUX2 = 1 (AOW3) */
4513eb7188caSLydia Wang 	{0x34, AC_VERB_SET_CONNECT_SEL, 0x2},
4514eb7188caSLydia Wang 	{0x35, AC_VERB_SET_CONNECT_SEL, 0x1},
4515eb7188caSLydia Wang 	/* Unmute MW4's index 0 */
4516eb7188caSLydia Wang 	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4517eb7188caSLydia Wang 	{ }
4518eb7188caSLydia Wang };
4519eb7188caSLydia Wang 
4520eb7188caSLydia Wang 
4521eb7188caSLydia Wang static struct hda_verb vt1718S_uniwill_init_verbs[] = {
4522eb7188caSLydia Wang 	{0x28, AC_VERB_SET_UNSOLICITED_ENABLE,
4523eb7188caSLydia Wang 	 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
4524eb7188caSLydia Wang 	{0x24, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4525eb7188caSLydia Wang 	{0x25, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4526eb7188caSLydia Wang 	{0x26, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4527eb7188caSLydia Wang 	{0x27, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4528eb7188caSLydia Wang 	{0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4529eb7188caSLydia Wang 	{0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4530eb7188caSLydia Wang 	{0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4531eb7188caSLydia Wang 	{ }
4532eb7188caSLydia Wang };
4533eb7188caSLydia Wang 
4534eb7188caSLydia Wang static struct hda_pcm_stream vt1718S_pcm_analog_playback = {
4535eb7188caSLydia Wang 	.substreams = 2,
4536eb7188caSLydia Wang 	.channels_min = 2,
4537eb7188caSLydia Wang 	.channels_max = 10,
4538eb7188caSLydia Wang 	.nid = 0x8, /* NID to query formats and rates */
4539eb7188caSLydia Wang 	.ops = {
4540eb7188caSLydia Wang 		.open = via_playback_pcm_open,
4541eb7188caSLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
4542eb7188caSLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup,
4543eb7188caSLydia Wang 		.close = via_pcm_open_close,
4544eb7188caSLydia Wang 	},
4545eb7188caSLydia Wang };
4546eb7188caSLydia Wang 
4547eb7188caSLydia Wang static struct hda_pcm_stream vt1718S_pcm_analog_capture = {
4548eb7188caSLydia Wang 	.substreams = 2,
4549eb7188caSLydia Wang 	.channels_min = 2,
4550eb7188caSLydia Wang 	.channels_max = 2,
4551eb7188caSLydia Wang 	.nid = 0x10, /* NID to query formats and rates */
4552eb7188caSLydia Wang 	.ops = {
4553eb7188caSLydia Wang 		.open = via_pcm_open_close,
4554eb7188caSLydia Wang 		.prepare = via_capture_pcm_prepare,
4555eb7188caSLydia Wang 		.cleanup = via_capture_pcm_cleanup,
4556eb7188caSLydia Wang 		.close = via_pcm_open_close,
4557eb7188caSLydia Wang 	},
4558eb7188caSLydia Wang };
4559eb7188caSLydia Wang 
4560eb7188caSLydia Wang static struct hda_pcm_stream vt1718S_pcm_digital_playback = {
4561eb7188caSLydia Wang 	.substreams = 2,
4562eb7188caSLydia Wang 	.channels_min = 2,
4563eb7188caSLydia Wang 	.channels_max = 2,
4564eb7188caSLydia Wang 	/* NID is set in via_build_pcms */
4565eb7188caSLydia Wang 	.ops = {
4566eb7188caSLydia Wang 		.open = via_dig_playback_pcm_open,
4567eb7188caSLydia Wang 		.close = via_dig_playback_pcm_close,
4568eb7188caSLydia Wang 		.prepare = via_dig_playback_pcm_prepare,
4569eb7188caSLydia Wang 		.cleanup = via_dig_playback_pcm_cleanup
4570eb7188caSLydia Wang 	},
4571eb7188caSLydia Wang };
4572eb7188caSLydia Wang 
4573eb7188caSLydia Wang static struct hda_pcm_stream vt1718S_pcm_digital_capture = {
4574eb7188caSLydia Wang 	.substreams = 1,
4575eb7188caSLydia Wang 	.channels_min = 2,
4576eb7188caSLydia Wang 	.channels_max = 2,
4577eb7188caSLydia Wang };
4578eb7188caSLydia Wang 
4579eb7188caSLydia Wang /* fill in the dac_nids table from the parsed pin configuration */
4580eb7188caSLydia Wang static int vt1718S_auto_fill_dac_nids(struct via_spec *spec,
4581eb7188caSLydia Wang 				     const struct auto_pin_cfg *cfg)
4582eb7188caSLydia Wang {
4583eb7188caSLydia Wang 	int i;
4584eb7188caSLydia Wang 	hda_nid_t nid;
4585eb7188caSLydia Wang 
4586eb7188caSLydia Wang 	spec->multiout.num_dacs = cfg->line_outs;
4587eb7188caSLydia Wang 
4588eb7188caSLydia Wang 	spec->multiout.dac_nids = spec->private_dac_nids;
4589eb7188caSLydia Wang 
4590eb7188caSLydia Wang 	for (i = 0; i < 4; i++) {
4591eb7188caSLydia Wang 		nid = cfg->line_out_pins[i];
4592eb7188caSLydia Wang 		if (nid) {
4593eb7188caSLydia Wang 			/* config dac list */
4594eb7188caSLydia Wang 			switch (i) {
4595eb7188caSLydia Wang 			case AUTO_SEQ_FRONT:
4596eb7188caSLydia Wang 				spec->multiout.dac_nids[i] = 0x8;
4597eb7188caSLydia Wang 				break;
4598eb7188caSLydia Wang 			case AUTO_SEQ_CENLFE:
4599eb7188caSLydia Wang 				spec->multiout.dac_nids[i] = 0xa;
4600eb7188caSLydia Wang 				break;
4601eb7188caSLydia Wang 			case AUTO_SEQ_SURROUND:
4602eb7188caSLydia Wang 				spec->multiout.dac_nids[i] = 0x9;
4603eb7188caSLydia Wang 				break;
4604eb7188caSLydia Wang 			case AUTO_SEQ_SIDE:
4605eb7188caSLydia Wang 				spec->multiout.dac_nids[i] = 0xb;
4606eb7188caSLydia Wang 				break;
4607eb7188caSLydia Wang 			}
4608eb7188caSLydia Wang 		}
4609eb7188caSLydia Wang 	}
4610eb7188caSLydia Wang 
4611eb7188caSLydia Wang 	return 0;
4612eb7188caSLydia Wang }
4613eb7188caSLydia Wang 
4614eb7188caSLydia Wang /* add playback controls from the parsed DAC table */
4615eb7188caSLydia Wang static int vt1718S_auto_create_multi_out_ctls(struct via_spec *spec,
4616eb7188caSLydia Wang 					     const struct auto_pin_cfg *cfg)
4617eb7188caSLydia Wang {
4618eb7188caSLydia Wang 	char name[32];
4619eb7188caSLydia Wang 	static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
4620eb7188caSLydia Wang 	hda_nid_t nid_vols[] = {0x8, 0x9, 0xa, 0xb};
4621eb7188caSLydia Wang 	hda_nid_t nid_mutes[] = {0x24, 0x25, 0x26, 0x27};
4622eb7188caSLydia Wang 	hda_nid_t nid, nid_vol, nid_mute = 0;
4623eb7188caSLydia Wang 	int i, err;
4624eb7188caSLydia Wang 
4625eb7188caSLydia Wang 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
4626eb7188caSLydia Wang 		nid = cfg->line_out_pins[i];
4627eb7188caSLydia Wang 
4628eb7188caSLydia Wang 		if (!nid)
4629eb7188caSLydia Wang 			continue;
4630eb7188caSLydia Wang 		nid_vol = nid_vols[i];
4631eb7188caSLydia Wang 		nid_mute = nid_mutes[i];
4632eb7188caSLydia Wang 
4633eb7188caSLydia Wang 		if (i == AUTO_SEQ_CENLFE) {
4634eb7188caSLydia Wang 			/* Center/LFE */
4635eb7188caSLydia Wang 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4636eb7188caSLydia Wang 					      "Center Playback Volume",
4637eb7188caSLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
4638eb7188caSLydia Wang 								  HDA_OUTPUT));
4639eb7188caSLydia Wang 			if (err < 0)
4640eb7188caSLydia Wang 				return err;
4641eb7188caSLydia Wang 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4642eb7188caSLydia Wang 					      "LFE Playback Volume",
4643eb7188caSLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
4644eb7188caSLydia Wang 								  HDA_OUTPUT));
4645eb7188caSLydia Wang 			if (err < 0)
4646eb7188caSLydia Wang 				return err;
4647eb7188caSLydia Wang 			err = via_add_control(
4648eb7188caSLydia Wang 				spec, VIA_CTL_WIDGET_MUTE,
4649eb7188caSLydia Wang 				"Center Playback Switch",
4650eb7188caSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_mute, 1, 0,
4651eb7188caSLydia Wang 						    HDA_OUTPUT));
4652eb7188caSLydia Wang 			if (err < 0)
4653eb7188caSLydia Wang 				return err;
4654eb7188caSLydia Wang 			err = via_add_control(
4655eb7188caSLydia Wang 				spec, VIA_CTL_WIDGET_MUTE,
4656eb7188caSLydia Wang 				"LFE Playback Switch",
4657eb7188caSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_mute, 2, 0,
4658eb7188caSLydia Wang 						    HDA_OUTPUT));
4659eb7188caSLydia Wang 			if (err < 0)
4660eb7188caSLydia Wang 				return err;
4661eb7188caSLydia Wang 		} else if (i == AUTO_SEQ_FRONT) {
4662eb7188caSLydia Wang 			/* Front */
4663eb7188caSLydia Wang 			sprintf(name, "%s Playback Volume", chname[i]);
4664eb7188caSLydia Wang 			err = via_add_control(
4665eb7188caSLydia Wang 				spec, VIA_CTL_WIDGET_VOL, name,
4666eb7188caSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
4667eb7188caSLydia Wang 			if (err < 0)
4668eb7188caSLydia Wang 				return err;
4669eb7188caSLydia Wang 			sprintf(name, "%s Playback Switch", chname[i]);
4670eb7188caSLydia Wang 			err = via_add_control(
4671eb7188caSLydia Wang 				spec, VIA_CTL_WIDGET_MUTE, name,
4672eb7188caSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
4673eb7188caSLydia Wang 						    HDA_OUTPUT));
4674eb7188caSLydia Wang 			if (err < 0)
4675eb7188caSLydia Wang 				return err;
4676eb7188caSLydia Wang 		} else {
4677eb7188caSLydia Wang 			sprintf(name, "%s Playback Volume", chname[i]);
4678eb7188caSLydia Wang 			err = via_add_control(
4679eb7188caSLydia Wang 				spec, VIA_CTL_WIDGET_VOL, name,
4680eb7188caSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
4681eb7188caSLydia Wang 			if (err < 0)
4682eb7188caSLydia Wang 				return err;
4683eb7188caSLydia Wang 			sprintf(name, "%s Playback Switch", chname[i]);
4684eb7188caSLydia Wang 			err = via_add_control(
4685eb7188caSLydia Wang 				spec, VIA_CTL_WIDGET_MUTE, name,
4686eb7188caSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
4687eb7188caSLydia Wang 						    HDA_OUTPUT));
4688eb7188caSLydia Wang 			if (err < 0)
4689eb7188caSLydia Wang 				return err;
4690eb7188caSLydia Wang 		}
4691eb7188caSLydia Wang 	}
4692eb7188caSLydia Wang 	return 0;
4693eb7188caSLydia Wang }
4694eb7188caSLydia Wang 
4695eb7188caSLydia Wang static int vt1718S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
4696eb7188caSLydia Wang {
4697eb7188caSLydia Wang 	int err;
4698eb7188caSLydia Wang 
4699eb7188caSLydia Wang 	if (!pin)
4700eb7188caSLydia Wang 		return 0;
4701eb7188caSLydia Wang 
4702eb7188caSLydia Wang 	spec->multiout.hp_nid = 0xc; /* AOW4 */
4703eb7188caSLydia Wang 	spec->hp_independent_mode_index = 1;
4704eb7188caSLydia Wang 
4705eb7188caSLydia Wang 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4706eb7188caSLydia Wang 			      "Headphone Playback Volume",
4707eb7188caSLydia Wang 			      HDA_COMPOSE_AMP_VAL(0xc, 3, 0, HDA_OUTPUT));
4708eb7188caSLydia Wang 	if (err < 0)
4709eb7188caSLydia Wang 		return err;
4710eb7188caSLydia Wang 
4711eb7188caSLydia Wang 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4712eb7188caSLydia Wang 			      "Headphone Playback Switch",
4713eb7188caSLydia Wang 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
4714eb7188caSLydia Wang 	if (err < 0)
4715eb7188caSLydia Wang 		return err;
4716eb7188caSLydia Wang 
4717eb7188caSLydia Wang 	create_hp_imux(spec);
4718eb7188caSLydia Wang 	return 0;
4719eb7188caSLydia Wang }
4720eb7188caSLydia Wang 
4721eb7188caSLydia Wang /* create playback/capture controls for input pins */
4722*10a20af7STakashi Iwai static int vt1718S_auto_create_analog_input_ctls(struct hda_codec *codec,
4723eb7188caSLydia Wang 						const struct auto_pin_cfg *cfg)
4724eb7188caSLydia Wang {
4725f3268512STakashi Iwai 	static hda_nid_t pin_idxs[] = { 0x2c, 0x2b, 0x2a, 0x29, 0, 0xff };
4726*10a20af7STakashi Iwai 	return vt_auto_create_analog_input_ctls(codec, cfg, 0x21, pin_idxs,
4727f3268512STakashi Iwai 						ARRAY_SIZE(pin_idxs));
4728eb7188caSLydia Wang }
4729eb7188caSLydia Wang 
4730eb7188caSLydia Wang static int vt1718S_parse_auto_config(struct hda_codec *codec)
4731eb7188caSLydia Wang {
4732eb7188caSLydia Wang 	struct via_spec *spec = codec->spec;
4733eb7188caSLydia Wang 	int err;
4734eb7188caSLydia Wang 
4735eb7188caSLydia Wang 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4736eb7188caSLydia Wang 
4737eb7188caSLydia Wang 	if (err < 0)
4738eb7188caSLydia Wang 		return err;
4739eb7188caSLydia Wang 	err = vt1718S_auto_fill_dac_nids(spec, &spec->autocfg);
4740eb7188caSLydia Wang 	if (err < 0)
4741eb7188caSLydia Wang 		return err;
4742eb7188caSLydia Wang 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
4743eb7188caSLydia Wang 		return 0; /* can't find valid BIOS pin config */
4744eb7188caSLydia Wang 
4745eb7188caSLydia Wang 	err = vt1718S_auto_create_multi_out_ctls(spec, &spec->autocfg);
4746eb7188caSLydia Wang 	if (err < 0)
4747eb7188caSLydia Wang 		return err;
4748eb7188caSLydia Wang 	err = vt1718S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
4749eb7188caSLydia Wang 	if (err < 0)
4750eb7188caSLydia Wang 		return err;
4751*10a20af7STakashi Iwai 	err = vt1718S_auto_create_analog_input_ctls(codec, &spec->autocfg);
4752eb7188caSLydia Wang 	if (err < 0)
4753eb7188caSLydia Wang 		return err;
4754eb7188caSLydia Wang 
4755eb7188caSLydia Wang 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4756eb7188caSLydia Wang 
4757eb7188caSLydia Wang 	fill_dig_outs(codec);
4758eb7188caSLydia Wang 
4759eb7188caSLydia Wang 	if (spec->autocfg.dig_in_pin && codec->vendor_id == 0x11060428)
4760eb7188caSLydia Wang 		spec->dig_in_nid = 0x13;
4761eb7188caSLydia Wang 
4762eb7188caSLydia Wang 	if (spec->kctls.list)
4763eb7188caSLydia Wang 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
4764eb7188caSLydia Wang 
4765eb7188caSLydia Wang 	spec->input_mux = &spec->private_imux[0];
4766eb7188caSLydia Wang 
4767eb7188caSLydia Wang 	if (spec->hp_mux)
47683d83e577STakashi Iwai 		via_hp_build(codec);
4769eb7188caSLydia Wang 
47705b0cb1d8SJaroslav Kysela 	via_smart51_build(spec);
4771eb7188caSLydia Wang 
4772eb7188caSLydia Wang 	return 1;
4773eb7188caSLydia Wang }
4774eb7188caSLydia Wang 
4775eb7188caSLydia Wang #ifdef CONFIG_SND_HDA_POWER_SAVE
4776eb7188caSLydia Wang static struct hda_amp_list vt1718S_loopbacks[] = {
4777eb7188caSLydia Wang 	{ 0x21, HDA_INPUT, 1 },
4778eb7188caSLydia Wang 	{ 0x21, HDA_INPUT, 2 },
4779eb7188caSLydia Wang 	{ 0x21, HDA_INPUT, 3 },
4780eb7188caSLydia Wang 	{ 0x21, HDA_INPUT, 4 },
4781eb7188caSLydia Wang 	{ } /* end */
4782eb7188caSLydia Wang };
4783eb7188caSLydia Wang #endif
4784eb7188caSLydia Wang 
4785eb7188caSLydia Wang static int patch_vt1718S(struct hda_codec *codec)
4786eb7188caSLydia Wang {
4787eb7188caSLydia Wang 	struct via_spec *spec;
4788eb7188caSLydia Wang 	int err;
4789eb7188caSLydia Wang 
4790eb7188caSLydia Wang 	/* create a codec specific record */
47915b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
4792eb7188caSLydia Wang 	if (spec == NULL)
4793eb7188caSLydia Wang 		return -ENOMEM;
4794eb7188caSLydia Wang 
4795eb7188caSLydia Wang 	/* automatic parse from the BIOS config */
4796eb7188caSLydia Wang 	err = vt1718S_parse_auto_config(codec);
4797eb7188caSLydia Wang 	if (err < 0) {
4798eb7188caSLydia Wang 		via_free(codec);
4799eb7188caSLydia Wang 		return err;
4800eb7188caSLydia Wang 	} else if (!err) {
4801eb7188caSLydia Wang 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
4802eb7188caSLydia Wang 		       "from BIOS.  Using genenic mode...\n");
4803eb7188caSLydia Wang 	}
4804eb7188caSLydia Wang 
4805eb7188caSLydia Wang 	spec->init_verbs[spec->num_iverbs++] = vt1718S_volume_init_verbs;
4806eb7188caSLydia Wang 	spec->init_verbs[spec->num_iverbs++] = vt1718S_uniwill_init_verbs;
4807eb7188caSLydia Wang 
4808bb3c6bfcSLydia Wang 	if (codec->vendor_id == 0x11060441)
4809bb3c6bfcSLydia Wang 		spec->stream_name_analog = "VT2020 Analog";
4810bb3c6bfcSLydia Wang 	else if (codec->vendor_id == 0x11064441)
4811bb3c6bfcSLydia Wang 		spec->stream_name_analog = "VT1828S Analog";
4812bb3c6bfcSLydia Wang 	else
4813eb7188caSLydia Wang 		spec->stream_name_analog = "VT1718S Analog";
4814eb7188caSLydia Wang 	spec->stream_analog_playback = &vt1718S_pcm_analog_playback;
4815eb7188caSLydia Wang 	spec->stream_analog_capture = &vt1718S_pcm_analog_capture;
4816eb7188caSLydia Wang 
4817bb3c6bfcSLydia Wang 	if (codec->vendor_id == 0x11060441)
4818bb3c6bfcSLydia Wang 		spec->stream_name_digital = "VT2020 Digital";
4819bb3c6bfcSLydia Wang 	else if (codec->vendor_id == 0x11064441)
4820bb3c6bfcSLydia Wang 		spec->stream_name_digital = "VT1828S Digital";
4821bb3c6bfcSLydia Wang 	else
4822eb7188caSLydia Wang 		spec->stream_name_digital = "VT1718S Digital";
4823eb7188caSLydia Wang 	spec->stream_digital_playback = &vt1718S_pcm_digital_playback;
4824bb3c6bfcSLydia Wang 	if (codec->vendor_id == 0x11060428 || codec->vendor_id == 0x11060441)
4825eb7188caSLydia Wang 		spec->stream_digital_capture = &vt1718S_pcm_digital_capture;
4826eb7188caSLydia Wang 
4827eb7188caSLydia Wang 	if (!spec->adc_nids && spec->input_mux) {
4828eb7188caSLydia Wang 		spec->adc_nids = vt1718S_adc_nids;
4829eb7188caSLydia Wang 		spec->num_adc_nids = ARRAY_SIZE(vt1718S_adc_nids);
4830eb7188caSLydia Wang 		get_mux_nids(codec);
4831bb3c6bfcSLydia Wang 		override_mic_boost(codec, 0x2b, 0, 3, 40);
4832bb3c6bfcSLydia Wang 		override_mic_boost(codec, 0x29, 0, 3, 40);
4833eb7188caSLydia Wang 		spec->mixers[spec->num_mixers] = vt1718S_capture_mixer;
4834eb7188caSLydia Wang 		spec->num_mixers++;
4835eb7188caSLydia Wang 	}
4836eb7188caSLydia Wang 
4837eb7188caSLydia Wang 	codec->patch_ops = via_patch_ops;
4838eb7188caSLydia Wang 
4839eb7188caSLydia Wang 	codec->patch_ops.init = via_auto_init;
48400f48327eSStephen Rothwell 	codec->patch_ops.unsol_event = via_unsol_event;
4841eb7188caSLydia Wang 
4842eb7188caSLydia Wang #ifdef CONFIG_SND_HDA_POWER_SAVE
4843eb7188caSLydia Wang 	spec->loopback.amplist = vt1718S_loopbacks;
4844eb7188caSLydia Wang #endif
4845eb7188caSLydia Wang 
4846eb7188caSLydia Wang 	return 0;
4847eb7188caSLydia Wang }
4848f3db423dSLydia Wang 
4849f3db423dSLydia Wang /* Patch for VT1716S */
4850f3db423dSLydia Wang 
4851f3db423dSLydia Wang static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
4852f3db423dSLydia Wang 			    struct snd_ctl_elem_info *uinfo)
4853f3db423dSLydia Wang {
4854f3db423dSLydia Wang 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
4855f3db423dSLydia Wang 	uinfo->count = 1;
4856f3db423dSLydia Wang 	uinfo->value.integer.min = 0;
4857f3db423dSLydia Wang 	uinfo->value.integer.max = 1;
4858f3db423dSLydia Wang 	return 0;
4859f3db423dSLydia Wang }
4860f3db423dSLydia Wang 
4861f3db423dSLydia Wang static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
4862f3db423dSLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
4863f3db423dSLydia Wang {
4864f3db423dSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4865f3db423dSLydia Wang 	int index = 0;
4866f3db423dSLydia Wang 
4867f3db423dSLydia Wang 	index = snd_hda_codec_read(codec, 0x26, 0,
4868f3db423dSLydia Wang 					       AC_VERB_GET_CONNECT_SEL, 0);
4869f3db423dSLydia Wang 	if (index != -1)
4870f3db423dSLydia Wang 		*ucontrol->value.integer.value = index;
4871f3db423dSLydia Wang 
4872f3db423dSLydia Wang 	return 0;
4873f3db423dSLydia Wang }
4874f3db423dSLydia Wang 
4875f3db423dSLydia Wang static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
4876f3db423dSLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
4877f3db423dSLydia Wang {
4878f3db423dSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4879f3db423dSLydia Wang 	struct via_spec *spec = codec->spec;
4880f3db423dSLydia Wang 	int index = *ucontrol->value.integer.value;
4881f3db423dSLydia Wang 
4882f3db423dSLydia Wang 	snd_hda_codec_write(codec, 0x26, 0,
4883f3db423dSLydia Wang 					       AC_VERB_SET_CONNECT_SEL, index);
4884f3db423dSLydia Wang 	spec->dmic_enabled = index;
4885f3db423dSLydia Wang 	set_jack_power_state(codec);
4886f3db423dSLydia Wang 
4887f3db423dSLydia Wang 	return 1;
4888f3db423dSLydia Wang }
4889f3db423dSLydia Wang 
4890f3db423dSLydia Wang /* capture mixer elements */
4891f3db423dSLydia Wang static struct snd_kcontrol_new vt1716S_capture_mixer[] = {
4892f3db423dSLydia Wang 	HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
4893f3db423dSLydia Wang 	HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
4894f3db423dSLydia Wang 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
4895f3db423dSLydia Wang 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
4896f3db423dSLydia Wang 	HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x1A, 0x0, HDA_INPUT),
4897f3db423dSLydia Wang 	HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x1E, 0x0,
4898f3db423dSLydia Wang 			 HDA_INPUT),
4899f3db423dSLydia Wang 	{
4900f3db423dSLydia Wang 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4901f3db423dSLydia Wang 		.name = "Input Source",
4902f3db423dSLydia Wang 		.count = 1,
4903f3db423dSLydia Wang 		.info = via_mux_enum_info,
4904f3db423dSLydia Wang 		.get = via_mux_enum_get,
4905f3db423dSLydia Wang 		.put = via_mux_enum_put,
4906f3db423dSLydia Wang 	},
4907f3db423dSLydia Wang 	{ } /* end */
4908f3db423dSLydia Wang };
4909f3db423dSLydia Wang 
4910f3db423dSLydia Wang static struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
4911f3db423dSLydia Wang 	HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
4912f3db423dSLydia Wang 	{
4913f3db423dSLydia Wang 	 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4914f3db423dSLydia Wang 	 .name = "Digital Mic Capture Switch",
49155b0cb1d8SJaroslav Kysela 	 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
4916f3db423dSLydia Wang 	 .count = 1,
4917f3db423dSLydia Wang 	 .info = vt1716s_dmic_info,
4918f3db423dSLydia Wang 	 .get = vt1716s_dmic_get,
4919f3db423dSLydia Wang 	 .put = vt1716s_dmic_put,
4920f3db423dSLydia Wang 	 },
4921f3db423dSLydia Wang 	{}			/* end */
4922f3db423dSLydia Wang };
4923f3db423dSLydia Wang 
4924f3db423dSLydia Wang 
4925f3db423dSLydia Wang /* mono-out mixer elements */
4926f3db423dSLydia Wang static struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
4927f3db423dSLydia Wang 	HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
4928f3db423dSLydia Wang 	{ } /* end */
4929f3db423dSLydia Wang };
4930f3db423dSLydia Wang 
4931f3db423dSLydia Wang static struct hda_verb vt1716S_volume_init_verbs[] = {
4932f3db423dSLydia Wang 	/*
4933f3db423dSLydia Wang 	 * Unmute ADC0-1 and set the default input to mic-in
4934f3db423dSLydia Wang 	 */
4935f3db423dSLydia Wang 	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4936f3db423dSLydia Wang 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4937f3db423dSLydia Wang 
4938f3db423dSLydia Wang 
4939f3db423dSLydia Wang 	/* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
4940f3db423dSLydia Wang 	 * mixer widget
4941f3db423dSLydia Wang 	 */
4942f3db423dSLydia Wang 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
4943f3db423dSLydia Wang 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4944f3db423dSLydia Wang 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4945f3db423dSLydia Wang 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
4946f3db423dSLydia Wang 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
4947f3db423dSLydia Wang 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
4948f3db423dSLydia Wang 
4949f3db423dSLydia Wang 	/* MUX Indices: Stereo Mixer = 5 */
4950f3db423dSLydia Wang 	{0x17, AC_VERB_SET_CONNECT_SEL, 0x5},
4951f3db423dSLydia Wang 
4952f3db423dSLydia Wang 	/* Setup default input of PW4 to MW0 */
4953f3db423dSLydia Wang 	{0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
4954f3db423dSLydia Wang 
4955f3db423dSLydia Wang 	/* Setup default input of SW1 as MW0 */
4956f3db423dSLydia Wang 	{0x18, AC_VERB_SET_CONNECT_SEL, 0x1},
4957f3db423dSLydia Wang 
4958f3db423dSLydia Wang 	/* Setup default input of SW4 as AOW0 */
4959f3db423dSLydia Wang 	{0x28, AC_VERB_SET_CONNECT_SEL, 0x1},
4960f3db423dSLydia Wang 
4961f3db423dSLydia Wang 	/* PW9 PW10 Output enable */
4962f3db423dSLydia Wang 	{0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
4963f3db423dSLydia Wang 	{0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
4964f3db423dSLydia Wang 
4965f3db423dSLydia Wang 	/* Unmute SW1, PW12 */
4966f3db423dSLydia Wang 	{0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4967f3db423dSLydia Wang 	{0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4968f3db423dSLydia Wang 	/* PW12 Output enable */
4969f3db423dSLydia Wang 	{0x2a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
4970f3db423dSLydia Wang 	/* Enable Boost Volume backdoor */
4971f3db423dSLydia Wang 	{0x1, 0xf8a, 0x80},
4972f3db423dSLydia Wang 	/* don't bybass mixer */
4973f3db423dSLydia Wang 	{0x1, 0xf88, 0xc0},
4974f3db423dSLydia Wang 	/* Enable mono output */
4975f3db423dSLydia Wang 	{0x1, 0xf90, 0x08},
4976f3db423dSLydia Wang 	{ }
4977f3db423dSLydia Wang };
4978f3db423dSLydia Wang 
4979f3db423dSLydia Wang 
4980f3db423dSLydia Wang static struct hda_verb vt1716S_uniwill_init_verbs[] = {
4981f3db423dSLydia Wang 	{0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
4982f3db423dSLydia Wang 	 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
4983f3db423dSLydia Wang 	{0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4984f3db423dSLydia Wang 	{0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4985f3db423dSLydia Wang 	{0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4986f3db423dSLydia Wang 	{0x1c, AC_VERB_SET_UNSOLICITED_ENABLE,
4987f3db423dSLydia Wang 	 AC_USRSP_EN | VIA_MONO_EVENT | VIA_JACK_EVENT},
4988f3db423dSLydia Wang 	{0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4989f3db423dSLydia Wang 	{0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4990f3db423dSLydia Wang 	{ }
4991f3db423dSLydia Wang };
4992f3db423dSLydia Wang 
4993f3db423dSLydia Wang static struct hda_pcm_stream vt1716S_pcm_analog_playback = {
4994f3db423dSLydia Wang 	.substreams = 2,
4995f3db423dSLydia Wang 	.channels_min = 2,
4996f3db423dSLydia Wang 	.channels_max = 6,
4997f3db423dSLydia Wang 	.nid = 0x10, /* NID to query formats and rates */
4998f3db423dSLydia Wang 	.ops = {
4999f3db423dSLydia Wang 		.open = via_playback_pcm_open,
5000f3db423dSLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
5001f3db423dSLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup,
5002f3db423dSLydia Wang 		.close = via_pcm_open_close,
5003f3db423dSLydia Wang 	},
5004f3db423dSLydia Wang };
5005f3db423dSLydia Wang 
5006f3db423dSLydia Wang static struct hda_pcm_stream vt1716S_pcm_analog_capture = {
5007f3db423dSLydia Wang 	.substreams = 2,
5008f3db423dSLydia Wang 	.channels_min = 2,
5009f3db423dSLydia Wang 	.channels_max = 2,
5010f3db423dSLydia Wang 	.nid = 0x13, /* NID to query formats and rates */
5011f3db423dSLydia Wang 	.ops = {
5012f3db423dSLydia Wang 		.open = via_pcm_open_close,
5013f3db423dSLydia Wang 		.prepare = via_capture_pcm_prepare,
5014f3db423dSLydia Wang 		.cleanup = via_capture_pcm_cleanup,
5015f3db423dSLydia Wang 		.close = via_pcm_open_close,
5016f3db423dSLydia Wang 	},
5017f3db423dSLydia Wang };
5018f3db423dSLydia Wang 
5019f3db423dSLydia Wang static struct hda_pcm_stream vt1716S_pcm_digital_playback = {
5020f3db423dSLydia Wang 	.substreams = 2,
5021f3db423dSLydia Wang 	.channels_min = 2,
5022f3db423dSLydia Wang 	.channels_max = 2,
5023f3db423dSLydia Wang 	/* NID is set in via_build_pcms */
5024f3db423dSLydia Wang 	.ops = {
5025f3db423dSLydia Wang 		.open = via_dig_playback_pcm_open,
5026f3db423dSLydia Wang 		.close = via_dig_playback_pcm_close,
5027f3db423dSLydia Wang 		.prepare = via_dig_playback_pcm_prepare,
5028f3db423dSLydia Wang 		.cleanup = via_dig_playback_pcm_cleanup
5029f3db423dSLydia Wang 	},
5030f3db423dSLydia Wang };
5031f3db423dSLydia Wang 
5032f3db423dSLydia Wang /* fill in the dac_nids table from the parsed pin configuration */
5033f3db423dSLydia Wang static int vt1716S_auto_fill_dac_nids(struct via_spec *spec,
5034f3db423dSLydia Wang 				      const struct auto_pin_cfg *cfg)
5035f3db423dSLydia Wang {	int i;
5036f3db423dSLydia Wang 	hda_nid_t nid;
5037f3db423dSLydia Wang 
5038f3db423dSLydia Wang 	spec->multiout.num_dacs = cfg->line_outs;
5039f3db423dSLydia Wang 
5040f3db423dSLydia Wang 	spec->multiout.dac_nids = spec->private_dac_nids;
5041f3db423dSLydia Wang 
5042f3db423dSLydia Wang 	for (i = 0; i < 3; i++) {
5043f3db423dSLydia Wang 		nid = cfg->line_out_pins[i];
5044f3db423dSLydia Wang 		if (nid) {
5045f3db423dSLydia Wang 			/* config dac list */
5046f3db423dSLydia Wang 			switch (i) {
5047f3db423dSLydia Wang 			case AUTO_SEQ_FRONT:
5048f3db423dSLydia Wang 				spec->multiout.dac_nids[i] = 0x10;
5049f3db423dSLydia Wang 				break;
5050f3db423dSLydia Wang 			case AUTO_SEQ_CENLFE:
5051f3db423dSLydia Wang 				spec->multiout.dac_nids[i] = 0x25;
5052f3db423dSLydia Wang 				break;
5053f3db423dSLydia Wang 			case AUTO_SEQ_SURROUND:
5054f3db423dSLydia Wang 				spec->multiout.dac_nids[i] = 0x11;
5055f3db423dSLydia Wang 				break;
5056f3db423dSLydia Wang 			}
5057f3db423dSLydia Wang 		}
5058f3db423dSLydia Wang 	}
5059f3db423dSLydia Wang 
5060f3db423dSLydia Wang 	return 0;
5061f3db423dSLydia Wang }
5062f3db423dSLydia Wang 
5063f3db423dSLydia Wang /* add playback controls from the parsed DAC table */
5064f3db423dSLydia Wang static int vt1716S_auto_create_multi_out_ctls(struct via_spec *spec,
5065f3db423dSLydia Wang 					      const struct auto_pin_cfg *cfg)
5066f3db423dSLydia Wang {
5067f3db423dSLydia Wang 	char name[32];
5068f3db423dSLydia Wang 	static const char *chname[3] = { "Front", "Surround", "C/LFE" };
5069f3db423dSLydia Wang 	hda_nid_t nid_vols[] = {0x10, 0x11, 0x25};
5070f3db423dSLydia Wang 	hda_nid_t nid_mutes[] = {0x1C, 0x18, 0x27};
5071f3db423dSLydia Wang 	hda_nid_t nid, nid_vol, nid_mute;
5072f3db423dSLydia Wang 	int i, err;
5073f3db423dSLydia Wang 
5074f3db423dSLydia Wang 	for (i = 0; i <= AUTO_SEQ_CENLFE; i++) {
5075f3db423dSLydia Wang 		nid = cfg->line_out_pins[i];
5076f3db423dSLydia Wang 
5077f3db423dSLydia Wang 		if (!nid)
5078f3db423dSLydia Wang 			continue;
5079f3db423dSLydia Wang 
5080f3db423dSLydia Wang 		nid_vol = nid_vols[i];
5081f3db423dSLydia Wang 		nid_mute = nid_mutes[i];
5082f3db423dSLydia Wang 
5083f3db423dSLydia Wang 		if (i == AUTO_SEQ_CENLFE) {
5084f3db423dSLydia Wang 			err = via_add_control(
5085f3db423dSLydia Wang 				spec, VIA_CTL_WIDGET_VOL,
5086f3db423dSLydia Wang 				"Center Playback Volume",
5087f3db423dSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0, HDA_OUTPUT));
5088f3db423dSLydia Wang 			if (err < 0)
5089f3db423dSLydia Wang 				return err;
5090f3db423dSLydia Wang 			err = via_add_control(
5091f3db423dSLydia Wang 				spec, VIA_CTL_WIDGET_VOL,
5092f3db423dSLydia Wang 				"LFE Playback Volume",
5093f3db423dSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0, HDA_OUTPUT));
5094f3db423dSLydia Wang 			if (err < 0)
5095f3db423dSLydia Wang 				return err;
5096f3db423dSLydia Wang 			err = via_add_control(
5097f3db423dSLydia Wang 				spec, VIA_CTL_WIDGET_MUTE,
5098f3db423dSLydia Wang 				"Center Playback Switch",
5099f3db423dSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_mute, 1, 0,
5100f3db423dSLydia Wang 						    HDA_OUTPUT));
5101f3db423dSLydia Wang 			if (err < 0)
5102f3db423dSLydia Wang 				return err;
5103f3db423dSLydia Wang 			err = via_add_control(
5104f3db423dSLydia Wang 				spec, VIA_CTL_WIDGET_MUTE,
5105f3db423dSLydia Wang 				"LFE Playback Switch",
5106f3db423dSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_mute, 2, 0,
5107f3db423dSLydia Wang 						    HDA_OUTPUT));
5108f3db423dSLydia Wang 			if (err < 0)
5109f3db423dSLydia Wang 				return err;
5110f3db423dSLydia Wang 		} else if (i == AUTO_SEQ_FRONT) {
5111f3db423dSLydia Wang 
5112f3db423dSLydia Wang 			err = via_add_control(
5113f3db423dSLydia Wang 				spec, VIA_CTL_WIDGET_VOL,
5114f3db423dSLydia Wang 				"Master Front Playback Volume",
5115f3db423dSLydia Wang 				HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_INPUT));
5116f3db423dSLydia Wang 			if (err < 0)
5117f3db423dSLydia Wang 				return err;
5118f3db423dSLydia Wang 			err = via_add_control(
5119f3db423dSLydia Wang 				spec, VIA_CTL_WIDGET_MUTE,
5120f3db423dSLydia Wang 				"Master Front Playback Switch",
5121f3db423dSLydia Wang 				HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_INPUT));
5122f3db423dSLydia Wang 			if (err < 0)
5123f3db423dSLydia Wang 				return err;
5124f3db423dSLydia Wang 
5125f3db423dSLydia Wang 			sprintf(name, "%s Playback Volume", chname[i]);
5126f3db423dSLydia Wang 			err = via_add_control(
5127f3db423dSLydia Wang 				spec, VIA_CTL_WIDGET_VOL, name,
5128f3db423dSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
5129f3db423dSLydia Wang 			if (err < 0)
5130f3db423dSLydia Wang 				return err;
5131f3db423dSLydia Wang 			sprintf(name, "%s Playback Switch", chname[i]);
5132f3db423dSLydia Wang 			err = via_add_control(
5133f3db423dSLydia Wang 				spec, VIA_CTL_WIDGET_MUTE, name,
5134f3db423dSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
5135f3db423dSLydia Wang 						    HDA_OUTPUT));
5136f3db423dSLydia Wang 			if (err < 0)
5137f3db423dSLydia Wang 				return err;
5138f3db423dSLydia Wang 		} else {
5139f3db423dSLydia Wang 			sprintf(name, "%s Playback Volume", chname[i]);
5140f3db423dSLydia Wang 			err = via_add_control(
5141f3db423dSLydia Wang 				spec, VIA_CTL_WIDGET_VOL, name,
5142f3db423dSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
5143f3db423dSLydia Wang 			if (err < 0)
5144f3db423dSLydia Wang 				return err;
5145f3db423dSLydia Wang 			sprintf(name, "%s Playback Switch", chname[i]);
5146f3db423dSLydia Wang 			err = via_add_control(
5147f3db423dSLydia Wang 				spec, VIA_CTL_WIDGET_MUTE, name,
5148f3db423dSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
5149f3db423dSLydia Wang 						    HDA_OUTPUT));
5150f3db423dSLydia Wang 			if (err < 0)
5151f3db423dSLydia Wang 				return err;
5152f3db423dSLydia Wang 		}
5153f3db423dSLydia Wang 	}
5154f3db423dSLydia Wang 	return 0;
5155f3db423dSLydia Wang }
5156f3db423dSLydia Wang 
5157f3db423dSLydia Wang static int vt1716S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
5158f3db423dSLydia Wang {
5159f3db423dSLydia Wang 	int err;
5160f3db423dSLydia Wang 
5161f3db423dSLydia Wang 	if (!pin)
5162f3db423dSLydia Wang 		return 0;
5163f3db423dSLydia Wang 
5164f3db423dSLydia Wang 	spec->multiout.hp_nid = 0x25; /* AOW3 */
5165f3db423dSLydia Wang 	spec->hp_independent_mode_index = 1;
5166f3db423dSLydia Wang 
5167f3db423dSLydia Wang 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5168f3db423dSLydia Wang 			      "Headphone Playback Volume",
5169f3db423dSLydia Wang 			      HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
5170f3db423dSLydia Wang 	if (err < 0)
5171f3db423dSLydia Wang 		return err;
5172f3db423dSLydia Wang 
5173f3db423dSLydia Wang 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
5174f3db423dSLydia Wang 			      "Headphone Playback Switch",
5175f3db423dSLydia Wang 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
5176f3db423dSLydia Wang 	if (err < 0)
5177f3db423dSLydia Wang 		return err;
5178f3db423dSLydia Wang 
5179f3db423dSLydia Wang 	create_hp_imux(spec);
5180f3db423dSLydia Wang 	return 0;
5181f3db423dSLydia Wang }
5182f3db423dSLydia Wang 
5183f3db423dSLydia Wang /* create playback/capture controls for input pins */
5184*10a20af7STakashi Iwai static int vt1716S_auto_create_analog_input_ctls(struct hda_codec *codec,
5185f3db423dSLydia Wang 						const struct auto_pin_cfg *cfg)
5186f3db423dSLydia Wang {
5187f3268512STakashi Iwai 	static hda_nid_t pin_idxs[] = { 0x1f, 0x1a, 0x1b, 0x1e, 0, 0xff };
5188*10a20af7STakashi Iwai 	return vt_auto_create_analog_input_ctls(codec, cfg, 0x16, pin_idxs,
5189f3268512STakashi Iwai 						ARRAY_SIZE(pin_idxs));
5190f3db423dSLydia Wang }
5191f3db423dSLydia Wang 
5192f3db423dSLydia Wang static int vt1716S_parse_auto_config(struct hda_codec *codec)
5193f3db423dSLydia Wang {
5194f3db423dSLydia Wang 	struct via_spec *spec = codec->spec;
5195f3db423dSLydia Wang 	int err;
5196f3db423dSLydia Wang 
5197f3db423dSLydia Wang 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
5198f3db423dSLydia Wang 	if (err < 0)
5199f3db423dSLydia Wang 		return err;
5200f3db423dSLydia Wang 	err = vt1716S_auto_fill_dac_nids(spec, &spec->autocfg);
5201f3db423dSLydia Wang 	if (err < 0)
5202f3db423dSLydia Wang 		return err;
5203f3db423dSLydia Wang 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
5204f3db423dSLydia Wang 		return 0; /* can't find valid BIOS pin config */
5205f3db423dSLydia Wang 
5206f3db423dSLydia Wang 	err = vt1716S_auto_create_multi_out_ctls(spec, &spec->autocfg);
5207f3db423dSLydia Wang 	if (err < 0)
5208f3db423dSLydia Wang 		return err;
5209f3db423dSLydia Wang 	err = vt1716S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
5210f3db423dSLydia Wang 	if (err < 0)
5211f3db423dSLydia Wang 		return err;
5212*10a20af7STakashi Iwai 	err = vt1716S_auto_create_analog_input_ctls(codec, &spec->autocfg);
5213f3db423dSLydia Wang 	if (err < 0)
5214f3db423dSLydia Wang 		return err;
5215f3db423dSLydia Wang 
5216f3db423dSLydia Wang 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
5217f3db423dSLydia Wang 
5218f3db423dSLydia Wang 	fill_dig_outs(codec);
5219f3db423dSLydia Wang 
5220f3db423dSLydia Wang 	if (spec->kctls.list)
5221f3db423dSLydia Wang 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
5222f3db423dSLydia Wang 
5223f3db423dSLydia Wang 	spec->input_mux = &spec->private_imux[0];
5224f3db423dSLydia Wang 
5225f3db423dSLydia Wang 	if (spec->hp_mux)
52263d83e577STakashi Iwai 		via_hp_build(codec);
5227f3db423dSLydia Wang 
52285b0cb1d8SJaroslav Kysela 	via_smart51_build(spec);
5229f3db423dSLydia Wang 
5230f3db423dSLydia Wang 	return 1;
5231f3db423dSLydia Wang }
5232f3db423dSLydia Wang 
5233f3db423dSLydia Wang #ifdef CONFIG_SND_HDA_POWER_SAVE
5234f3db423dSLydia Wang static struct hda_amp_list vt1716S_loopbacks[] = {
5235f3db423dSLydia Wang 	{ 0x16, HDA_INPUT, 1 },
5236f3db423dSLydia Wang 	{ 0x16, HDA_INPUT, 2 },
5237f3db423dSLydia Wang 	{ 0x16, HDA_INPUT, 3 },
5238f3db423dSLydia Wang 	{ 0x16, HDA_INPUT, 4 },
5239f3db423dSLydia Wang 	{ } /* end */
5240f3db423dSLydia Wang };
5241f3db423dSLydia Wang #endif
5242f3db423dSLydia Wang 
5243f3db423dSLydia Wang static int patch_vt1716S(struct hda_codec *codec)
5244f3db423dSLydia Wang {
5245f3db423dSLydia Wang 	struct via_spec *spec;
5246f3db423dSLydia Wang 	int err;
5247f3db423dSLydia Wang 
5248f3db423dSLydia Wang 	/* create a codec specific record */
52495b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
5250f3db423dSLydia Wang 	if (spec == NULL)
5251f3db423dSLydia Wang 		return -ENOMEM;
5252f3db423dSLydia Wang 
5253f3db423dSLydia Wang 	/* automatic parse from the BIOS config */
5254f3db423dSLydia Wang 	err = vt1716S_parse_auto_config(codec);
5255f3db423dSLydia Wang 	if (err < 0) {
5256f3db423dSLydia Wang 		via_free(codec);
5257f3db423dSLydia Wang 		return err;
5258f3db423dSLydia Wang 	} else if (!err) {
5259f3db423dSLydia Wang 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
5260f3db423dSLydia Wang 		       "from BIOS.  Using genenic mode...\n");
5261f3db423dSLydia Wang 	}
5262f3db423dSLydia Wang 
5263f3db423dSLydia Wang 	spec->init_verbs[spec->num_iverbs++]  = vt1716S_volume_init_verbs;
5264f3db423dSLydia Wang 	spec->init_verbs[spec->num_iverbs++] = vt1716S_uniwill_init_verbs;
5265f3db423dSLydia Wang 
5266f3db423dSLydia Wang 	spec->stream_name_analog = "VT1716S Analog";
5267f3db423dSLydia Wang 	spec->stream_analog_playback = &vt1716S_pcm_analog_playback;
5268f3db423dSLydia Wang 	spec->stream_analog_capture = &vt1716S_pcm_analog_capture;
5269f3db423dSLydia Wang 
5270f3db423dSLydia Wang 	spec->stream_name_digital = "VT1716S Digital";
5271f3db423dSLydia Wang 	spec->stream_digital_playback = &vt1716S_pcm_digital_playback;
5272f3db423dSLydia Wang 
5273f3db423dSLydia Wang 	if (!spec->adc_nids && spec->input_mux) {
5274f3db423dSLydia Wang 		spec->adc_nids = vt1716S_adc_nids;
5275f3db423dSLydia Wang 		spec->num_adc_nids = ARRAY_SIZE(vt1716S_adc_nids);
5276f3db423dSLydia Wang 		get_mux_nids(codec);
5277f3db423dSLydia Wang 		override_mic_boost(codec, 0x1a, 0, 3, 40);
5278f3db423dSLydia Wang 		override_mic_boost(codec, 0x1e, 0, 3, 40);
5279f3db423dSLydia Wang 		spec->mixers[spec->num_mixers] = vt1716S_capture_mixer;
5280f3db423dSLydia Wang 		spec->num_mixers++;
5281f3db423dSLydia Wang 	}
5282f3db423dSLydia Wang 
5283f3db423dSLydia Wang 	spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
5284f3db423dSLydia Wang 	spec->num_mixers++;
5285f3db423dSLydia Wang 
5286f3db423dSLydia Wang 	spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
5287f3db423dSLydia Wang 
5288f3db423dSLydia Wang 	codec->patch_ops = via_patch_ops;
5289f3db423dSLydia Wang 
5290f3db423dSLydia Wang 	codec->patch_ops.init = via_auto_init;
52910f48327eSStephen Rothwell 	codec->patch_ops.unsol_event = via_unsol_event;
5292f3db423dSLydia Wang 
5293f3db423dSLydia Wang #ifdef CONFIG_SND_HDA_POWER_SAVE
5294f3db423dSLydia Wang 	spec->loopback.amplist = vt1716S_loopbacks;
5295f3db423dSLydia Wang #endif
5296f3db423dSLydia Wang 
5297f3db423dSLydia Wang 	return 0;
5298f3db423dSLydia Wang }
529925eaba2fSLydia Wang 
530025eaba2fSLydia Wang /* for vt2002P */
530125eaba2fSLydia Wang 
530225eaba2fSLydia Wang /* capture mixer elements */
530325eaba2fSLydia Wang static struct snd_kcontrol_new vt2002P_capture_mixer[] = {
530425eaba2fSLydia Wang 	HDA_CODEC_VOLUME("Capture Volume", 0x10, 0x0, HDA_INPUT),
530525eaba2fSLydia Wang 	HDA_CODEC_MUTE("Capture Switch", 0x10, 0x0, HDA_INPUT),
530625eaba2fSLydia Wang 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x11, 0x0, HDA_INPUT),
530725eaba2fSLydia Wang 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x11, 0x0, HDA_INPUT),
530825eaba2fSLydia Wang 	HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x2b, 0x0, HDA_INPUT),
530925eaba2fSLydia Wang 	HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x29, 0x0,
531025eaba2fSLydia Wang 			 HDA_INPUT),
531125eaba2fSLydia Wang 	{
531225eaba2fSLydia Wang 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
531325eaba2fSLydia Wang 		/* The multiple "Capture Source" controls confuse alsamixer
531425eaba2fSLydia Wang 		 * So call somewhat different..
531525eaba2fSLydia Wang 		 */
531625eaba2fSLydia Wang 		/* .name = "Capture Source", */
531725eaba2fSLydia Wang 		.name = "Input Source",
531825eaba2fSLydia Wang 		.count = 2,
531925eaba2fSLydia Wang 		.info = via_mux_enum_info,
532025eaba2fSLydia Wang 		.get = via_mux_enum_get,
532125eaba2fSLydia Wang 		.put = via_mux_enum_put,
532225eaba2fSLydia Wang 	},
532325eaba2fSLydia Wang 	{ } /* end */
532425eaba2fSLydia Wang };
532525eaba2fSLydia Wang 
532625eaba2fSLydia Wang static struct hda_verb vt2002P_volume_init_verbs[] = {
532725eaba2fSLydia Wang 	/*
532825eaba2fSLydia Wang 	 * Unmute ADC0-1 and set the default input to mic-in
532925eaba2fSLydia Wang 	 */
533025eaba2fSLydia Wang 	{0x8, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
533125eaba2fSLydia Wang 	{0x9, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
533225eaba2fSLydia Wang 
533325eaba2fSLydia Wang 
533425eaba2fSLydia Wang 	/* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
533525eaba2fSLydia Wang 	 * mixer widget
533625eaba2fSLydia Wang 	 */
533725eaba2fSLydia Wang 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
533825eaba2fSLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
533925eaba2fSLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
534025eaba2fSLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
534125eaba2fSLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
534225eaba2fSLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
534325eaba2fSLydia Wang 
534425eaba2fSLydia Wang 	/* MUX Indices: Mic = 0 */
534525eaba2fSLydia Wang 	{0x1e, AC_VERB_SET_CONNECT_SEL, 0},
534625eaba2fSLydia Wang 	{0x1f, AC_VERB_SET_CONNECT_SEL, 0},
534725eaba2fSLydia Wang 
534825eaba2fSLydia Wang 	/* PW9 Output enable */
534925eaba2fSLydia Wang 	{0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
535025eaba2fSLydia Wang 
535125eaba2fSLydia Wang 	/* Enable Boost Volume backdoor */
535225eaba2fSLydia Wang 	{0x1, 0xfb9, 0x24},
535325eaba2fSLydia Wang 
535425eaba2fSLydia Wang 	/* MW0/1/4/8: un-mute index 0 (MUXx), un-mute index 1 (MW9) */
535525eaba2fSLydia Wang 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
535625eaba2fSLydia Wang 	{0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
535725eaba2fSLydia Wang 	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
535825eaba2fSLydia Wang 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
535925eaba2fSLydia Wang 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
536025eaba2fSLydia Wang 	{0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
536125eaba2fSLydia Wang 	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
536225eaba2fSLydia Wang 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
536325eaba2fSLydia Wang 
536425eaba2fSLydia Wang 	/* set MUX0/1/4/8 = 0 (AOW0) */
536525eaba2fSLydia Wang 	{0x34, AC_VERB_SET_CONNECT_SEL, 0},
536625eaba2fSLydia Wang 	{0x35, AC_VERB_SET_CONNECT_SEL, 0},
536725eaba2fSLydia Wang 	{0x37, AC_VERB_SET_CONNECT_SEL, 0},
536825eaba2fSLydia Wang 	{0x3b, AC_VERB_SET_CONNECT_SEL, 0},
536925eaba2fSLydia Wang 
537025eaba2fSLydia Wang 	/* set PW0 index=0 (MW0) */
537125eaba2fSLydia Wang 	{0x24, AC_VERB_SET_CONNECT_SEL, 0},
537225eaba2fSLydia Wang 
537325eaba2fSLydia Wang 	/* Enable AOW0 to MW9 */
537425eaba2fSLydia Wang 	{0x1, 0xfb8, 0x88},
537525eaba2fSLydia Wang 	{ }
537625eaba2fSLydia Wang };
537725eaba2fSLydia Wang 
537825eaba2fSLydia Wang 
537925eaba2fSLydia Wang static struct hda_verb vt2002P_uniwill_init_verbs[] = {
538025eaba2fSLydia Wang 	{0x25, AC_VERB_SET_UNSOLICITED_ENABLE,
538125eaba2fSLydia Wang 	 AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
538225eaba2fSLydia Wang 	{0x26, AC_VERB_SET_UNSOLICITED_ENABLE,
538325eaba2fSLydia Wang 	 AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
538425eaba2fSLydia Wang 	{0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
538525eaba2fSLydia Wang 	{0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
538625eaba2fSLydia Wang 	{0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
538725eaba2fSLydia Wang 	{ }
538825eaba2fSLydia Wang };
538925eaba2fSLydia Wang 
539025eaba2fSLydia Wang static struct hda_pcm_stream vt2002P_pcm_analog_playback = {
539125eaba2fSLydia Wang 	.substreams = 2,
539225eaba2fSLydia Wang 	.channels_min = 2,
539325eaba2fSLydia Wang 	.channels_max = 2,
539425eaba2fSLydia Wang 	.nid = 0x8, /* NID to query formats and rates */
539525eaba2fSLydia Wang 	.ops = {
539625eaba2fSLydia Wang 		.open = via_playback_pcm_open,
539725eaba2fSLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
539825eaba2fSLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup,
539925eaba2fSLydia Wang 		.close = via_pcm_open_close,
540025eaba2fSLydia Wang 	},
540125eaba2fSLydia Wang };
540225eaba2fSLydia Wang 
540325eaba2fSLydia Wang static struct hda_pcm_stream vt2002P_pcm_analog_capture = {
540425eaba2fSLydia Wang 	.substreams = 2,
540525eaba2fSLydia Wang 	.channels_min = 2,
540625eaba2fSLydia Wang 	.channels_max = 2,
540725eaba2fSLydia Wang 	.nid = 0x10, /* NID to query formats and rates */
540825eaba2fSLydia Wang 	.ops = {
540925eaba2fSLydia Wang 		.open = via_pcm_open_close,
541025eaba2fSLydia Wang 		.prepare = via_capture_pcm_prepare,
541125eaba2fSLydia Wang 		.cleanup = via_capture_pcm_cleanup,
541225eaba2fSLydia Wang 		.close = via_pcm_open_close,
541325eaba2fSLydia Wang 	},
541425eaba2fSLydia Wang };
541525eaba2fSLydia Wang 
541625eaba2fSLydia Wang static struct hda_pcm_stream vt2002P_pcm_digital_playback = {
541725eaba2fSLydia Wang 	.substreams = 1,
541825eaba2fSLydia Wang 	.channels_min = 2,
541925eaba2fSLydia Wang 	.channels_max = 2,
542025eaba2fSLydia Wang 	/* NID is set in via_build_pcms */
542125eaba2fSLydia Wang 	.ops = {
542225eaba2fSLydia Wang 		.open = via_dig_playback_pcm_open,
542325eaba2fSLydia Wang 		.close = via_dig_playback_pcm_close,
542425eaba2fSLydia Wang 		.prepare = via_dig_playback_pcm_prepare,
542525eaba2fSLydia Wang 		.cleanup = via_dig_playback_pcm_cleanup
542625eaba2fSLydia Wang 	},
542725eaba2fSLydia Wang };
542825eaba2fSLydia Wang 
542925eaba2fSLydia Wang /* fill in the dac_nids table from the parsed pin configuration */
543025eaba2fSLydia Wang static int vt2002P_auto_fill_dac_nids(struct via_spec *spec,
543125eaba2fSLydia Wang 				      const struct auto_pin_cfg *cfg)
543225eaba2fSLydia Wang {
543325eaba2fSLydia Wang 	spec->multiout.num_dacs = 1;
543425eaba2fSLydia Wang 	spec->multiout.dac_nids = spec->private_dac_nids;
543525eaba2fSLydia Wang 	if (cfg->line_out_pins[0])
543625eaba2fSLydia Wang 		spec->multiout.dac_nids[0] = 0x8;
543725eaba2fSLydia Wang 	return 0;
543825eaba2fSLydia Wang }
543925eaba2fSLydia Wang 
544025eaba2fSLydia Wang /* add playback controls from the parsed DAC table */
544125eaba2fSLydia Wang static int vt2002P_auto_create_multi_out_ctls(struct via_spec *spec,
544225eaba2fSLydia Wang 					     const struct auto_pin_cfg *cfg)
544325eaba2fSLydia Wang {
544425eaba2fSLydia Wang 	int err;
544525eaba2fSLydia Wang 
544625eaba2fSLydia Wang 	if (!cfg->line_out_pins[0])
544725eaba2fSLydia Wang 		return -1;
544825eaba2fSLydia Wang 
544925eaba2fSLydia Wang 
545025eaba2fSLydia Wang 	/* Line-Out: PortE */
545125eaba2fSLydia Wang 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
545225eaba2fSLydia Wang 			      "Master Front Playback Volume",
545325eaba2fSLydia Wang 			      HDA_COMPOSE_AMP_VAL(0x8, 3, 0, HDA_OUTPUT));
545425eaba2fSLydia Wang 	if (err < 0)
545525eaba2fSLydia Wang 		return err;
545625eaba2fSLydia Wang 	err = via_add_control(spec, VIA_CTL_WIDGET_BIND_PIN_MUTE,
545725eaba2fSLydia Wang 			      "Master Front Playback Switch",
545825eaba2fSLydia Wang 			      HDA_COMPOSE_AMP_VAL(0x26, 3, 0, HDA_OUTPUT));
545925eaba2fSLydia Wang 	if (err < 0)
546025eaba2fSLydia Wang 		return err;
546125eaba2fSLydia Wang 
546225eaba2fSLydia Wang 	return 0;
546325eaba2fSLydia Wang }
546425eaba2fSLydia Wang 
546525eaba2fSLydia Wang static int vt2002P_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
546625eaba2fSLydia Wang {
546725eaba2fSLydia Wang 	int err;
546825eaba2fSLydia Wang 
546925eaba2fSLydia Wang 	if (!pin)
547025eaba2fSLydia Wang 		return 0;
547125eaba2fSLydia Wang 
547225eaba2fSLydia Wang 	spec->multiout.hp_nid = 0x9;
547325eaba2fSLydia Wang 	spec->hp_independent_mode_index = 1;
547425eaba2fSLydia Wang 
547525eaba2fSLydia Wang 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
547625eaba2fSLydia Wang 			      "Headphone Playback Volume",
547725eaba2fSLydia Wang 			      HDA_COMPOSE_AMP_VAL(
547825eaba2fSLydia Wang 				      spec->multiout.hp_nid, 3, 0, HDA_OUTPUT));
547925eaba2fSLydia Wang 	if (err < 0)
548025eaba2fSLydia Wang 		return err;
548125eaba2fSLydia Wang 
548225eaba2fSLydia Wang 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
548325eaba2fSLydia Wang 			      "Headphone Playback Switch",
548425eaba2fSLydia Wang 			      HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
548525eaba2fSLydia Wang 	if (err < 0)
548625eaba2fSLydia Wang 		return err;
548725eaba2fSLydia Wang 
548825eaba2fSLydia Wang 	create_hp_imux(spec);
548925eaba2fSLydia Wang 	return 0;
549025eaba2fSLydia Wang }
549125eaba2fSLydia Wang 
549225eaba2fSLydia Wang /* create playback/capture controls for input pins */
5493*10a20af7STakashi Iwai static int vt2002P_auto_create_analog_input_ctls(struct hda_codec *codec,
549425eaba2fSLydia Wang 						const struct auto_pin_cfg *cfg)
549525eaba2fSLydia Wang {
5496*10a20af7STakashi Iwai 	struct via_spec *spec = codec->spec;
549725eaba2fSLydia Wang 	struct hda_input_mux *imux = &spec->private_imux[0];
5498f3268512STakashi Iwai 	static hda_nid_t pin_idxs[] = { 0x2b, 0x2a, 0x29, 0xff };
5499f3268512STakashi Iwai 	int err;
550025eaba2fSLydia Wang 
5501*10a20af7STakashi Iwai 	err = vt_auto_create_analog_input_ctls(codec, cfg, 0x21, pin_idxs,
5502f3268512STakashi Iwai 					       ARRAY_SIZE(pin_idxs));
550325eaba2fSLydia Wang 	if (err < 0)
550425eaba2fSLydia Wang 		return err;
550525eaba2fSLydia Wang 	/* build volume/mute control of loopback */
55067b315bb4STakashi Iwai 	err = via_new_analog_input(spec, "Stereo Mixer", 0, 3, 0x21);
550725eaba2fSLydia Wang 	if (err < 0)
550825eaba2fSLydia Wang 		return err;
550925eaba2fSLydia Wang 
551025eaba2fSLydia Wang 	/* for digital mic select */
5511*10a20af7STakashi Iwai 	snd_hda_add_imux_item(imux, "Digital Mic", 4, NULL);
551225eaba2fSLydia Wang 
551325eaba2fSLydia Wang 	return 0;
551425eaba2fSLydia Wang }
551525eaba2fSLydia Wang 
551625eaba2fSLydia Wang static int vt2002P_parse_auto_config(struct hda_codec *codec)
551725eaba2fSLydia Wang {
551825eaba2fSLydia Wang 	struct via_spec *spec = codec->spec;
551925eaba2fSLydia Wang 	int err;
552025eaba2fSLydia Wang 
552125eaba2fSLydia Wang 
552225eaba2fSLydia Wang 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
552325eaba2fSLydia Wang 	if (err < 0)
552425eaba2fSLydia Wang 		return err;
552525eaba2fSLydia Wang 
552625eaba2fSLydia Wang 	err = vt2002P_auto_fill_dac_nids(spec, &spec->autocfg);
552725eaba2fSLydia Wang 	if (err < 0)
552825eaba2fSLydia Wang 		return err;
552925eaba2fSLydia Wang 
553025eaba2fSLydia Wang 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
553125eaba2fSLydia Wang 		return 0; /* can't find valid BIOS pin config */
553225eaba2fSLydia Wang 
553325eaba2fSLydia Wang 	err = vt2002P_auto_create_multi_out_ctls(spec, &spec->autocfg);
553425eaba2fSLydia Wang 	if (err < 0)
553525eaba2fSLydia Wang 		return err;
553625eaba2fSLydia Wang 	err = vt2002P_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
553725eaba2fSLydia Wang 	if (err < 0)
553825eaba2fSLydia Wang 		return err;
5539*10a20af7STakashi Iwai 	err = vt2002P_auto_create_analog_input_ctls(codec, &spec->autocfg);
554025eaba2fSLydia Wang 	if (err < 0)
554125eaba2fSLydia Wang 		return err;
554225eaba2fSLydia Wang 
554325eaba2fSLydia Wang 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
554425eaba2fSLydia Wang 
554525eaba2fSLydia Wang 	fill_dig_outs(codec);
554625eaba2fSLydia Wang 
554725eaba2fSLydia Wang 	if (spec->kctls.list)
554825eaba2fSLydia Wang 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
554925eaba2fSLydia Wang 
555025eaba2fSLydia Wang 	spec->input_mux = &spec->private_imux[0];
555125eaba2fSLydia Wang 
555225eaba2fSLydia Wang 	if (spec->hp_mux)
55533d83e577STakashi Iwai 		via_hp_build(codec);
555425eaba2fSLydia Wang 
555525eaba2fSLydia Wang 	return 1;
555625eaba2fSLydia Wang }
555725eaba2fSLydia Wang 
555825eaba2fSLydia Wang #ifdef CONFIG_SND_HDA_POWER_SAVE
555925eaba2fSLydia Wang static struct hda_amp_list vt2002P_loopbacks[] = {
556025eaba2fSLydia Wang 	{ 0x21, HDA_INPUT, 0 },
556125eaba2fSLydia Wang 	{ 0x21, HDA_INPUT, 1 },
556225eaba2fSLydia Wang 	{ 0x21, HDA_INPUT, 2 },
556325eaba2fSLydia Wang 	{ } /* end */
556425eaba2fSLydia Wang };
556525eaba2fSLydia Wang #endif
556625eaba2fSLydia Wang 
556725eaba2fSLydia Wang 
556825eaba2fSLydia Wang /* patch for vt2002P */
556925eaba2fSLydia Wang static int patch_vt2002P(struct hda_codec *codec)
557025eaba2fSLydia Wang {
557125eaba2fSLydia Wang 	struct via_spec *spec;
557225eaba2fSLydia Wang 	int err;
557325eaba2fSLydia Wang 
557425eaba2fSLydia Wang 	/* create a codec specific record */
55755b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
557625eaba2fSLydia Wang 	if (spec == NULL)
557725eaba2fSLydia Wang 		return -ENOMEM;
557825eaba2fSLydia Wang 
557925eaba2fSLydia Wang 	/* automatic parse from the BIOS config */
558025eaba2fSLydia Wang 	err = vt2002P_parse_auto_config(codec);
558125eaba2fSLydia Wang 	if (err < 0) {
558225eaba2fSLydia Wang 		via_free(codec);
558325eaba2fSLydia Wang 		return err;
558425eaba2fSLydia Wang 	} else if (!err) {
558525eaba2fSLydia Wang 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
558625eaba2fSLydia Wang 		       "from BIOS.  Using genenic mode...\n");
558725eaba2fSLydia Wang 	}
558825eaba2fSLydia Wang 
558925eaba2fSLydia Wang 	spec->init_verbs[spec->num_iverbs++]  = vt2002P_volume_init_verbs;
559025eaba2fSLydia Wang 	spec->init_verbs[spec->num_iverbs++] = vt2002P_uniwill_init_verbs;
559125eaba2fSLydia Wang 
559225eaba2fSLydia Wang 	spec->stream_name_analog = "VT2002P Analog";
559325eaba2fSLydia Wang 	spec->stream_analog_playback = &vt2002P_pcm_analog_playback;
559425eaba2fSLydia Wang 	spec->stream_analog_capture = &vt2002P_pcm_analog_capture;
559525eaba2fSLydia Wang 
559625eaba2fSLydia Wang 	spec->stream_name_digital = "VT2002P Digital";
559725eaba2fSLydia Wang 	spec->stream_digital_playback = &vt2002P_pcm_digital_playback;
559825eaba2fSLydia Wang 
559925eaba2fSLydia Wang 	if (!spec->adc_nids && spec->input_mux) {
560025eaba2fSLydia Wang 		spec->adc_nids = vt2002P_adc_nids;
560125eaba2fSLydia Wang 		spec->num_adc_nids = ARRAY_SIZE(vt2002P_adc_nids);
560225eaba2fSLydia Wang 		get_mux_nids(codec);
560325eaba2fSLydia Wang 		override_mic_boost(codec, 0x2b, 0, 3, 40);
560425eaba2fSLydia Wang 		override_mic_boost(codec, 0x29, 0, 3, 40);
560525eaba2fSLydia Wang 		spec->mixers[spec->num_mixers] = vt2002P_capture_mixer;
560625eaba2fSLydia Wang 		spec->num_mixers++;
560725eaba2fSLydia Wang 	}
560825eaba2fSLydia Wang 
560925eaba2fSLydia Wang 	codec->patch_ops = via_patch_ops;
561025eaba2fSLydia Wang 
561125eaba2fSLydia Wang 	codec->patch_ops.init = via_auto_init;
56120f48327eSStephen Rothwell 	codec->patch_ops.unsol_event = via_unsol_event;
561325eaba2fSLydia Wang 
561425eaba2fSLydia Wang #ifdef CONFIG_SND_HDA_POWER_SAVE
561525eaba2fSLydia Wang 	spec->loopback.amplist = vt2002P_loopbacks;
561625eaba2fSLydia Wang #endif
561725eaba2fSLydia Wang 
561825eaba2fSLydia Wang 	return 0;
561925eaba2fSLydia Wang }
5620ab6734e7SLydia Wang 
5621ab6734e7SLydia Wang /* for vt1812 */
5622ab6734e7SLydia Wang 
5623ab6734e7SLydia Wang /* capture mixer elements */
5624ab6734e7SLydia Wang static struct snd_kcontrol_new vt1812_capture_mixer[] = {
5625ab6734e7SLydia Wang 	HDA_CODEC_VOLUME("Capture Volume", 0x10, 0x0, HDA_INPUT),
5626ab6734e7SLydia Wang 	HDA_CODEC_MUTE("Capture Switch", 0x10, 0x0, HDA_INPUT),
5627ab6734e7SLydia Wang 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x11, 0x0, HDA_INPUT),
5628ab6734e7SLydia Wang 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x11, 0x0, HDA_INPUT),
5629ab6734e7SLydia Wang 	HDA_CODEC_MUTE("Mic Boost Capture Volume", 0x2b, 0x0, HDA_INPUT),
5630ab6734e7SLydia Wang 	HDA_CODEC_MUTE("Front Mic Boost Capture Volume", 0x29, 0x0,
5631ab6734e7SLydia Wang 		       HDA_INPUT),
5632ab6734e7SLydia Wang 	{
5633ab6734e7SLydia Wang 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5634ab6734e7SLydia Wang 		/* The multiple "Capture Source" controls confuse alsamixer
5635ab6734e7SLydia Wang 		 * So call somewhat different..
5636ab6734e7SLydia Wang 		 */
5637ab6734e7SLydia Wang 		.name = "Input Source",
5638ab6734e7SLydia Wang 		.count = 2,
5639ab6734e7SLydia Wang 		.info = via_mux_enum_info,
5640ab6734e7SLydia Wang 		.get = via_mux_enum_get,
5641ab6734e7SLydia Wang 		.put = via_mux_enum_put,
5642ab6734e7SLydia Wang 	},
5643ab6734e7SLydia Wang 	{ } /* end */
5644ab6734e7SLydia Wang };
5645ab6734e7SLydia Wang 
5646ab6734e7SLydia Wang static struct hda_verb vt1812_volume_init_verbs[] = {
5647ab6734e7SLydia Wang 	/*
5648ab6734e7SLydia Wang 	 * Unmute ADC0-1 and set the default input to mic-in
5649ab6734e7SLydia Wang 	 */
5650ab6734e7SLydia Wang 	{0x8, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5651ab6734e7SLydia Wang 	{0x9, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5652ab6734e7SLydia Wang 
5653ab6734e7SLydia Wang 
5654ab6734e7SLydia Wang 	/* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
5655ab6734e7SLydia Wang 	 * mixer widget
5656ab6734e7SLydia Wang 	 */
5657ab6734e7SLydia Wang 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
5658ab6734e7SLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
5659ab6734e7SLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5660ab6734e7SLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
5661ab6734e7SLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
5662ab6734e7SLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
5663ab6734e7SLydia Wang 
5664ab6734e7SLydia Wang 	/* MUX Indices: Mic = 0 */
5665ab6734e7SLydia Wang 	{0x1e, AC_VERB_SET_CONNECT_SEL, 0},
5666ab6734e7SLydia Wang 	{0x1f, AC_VERB_SET_CONNECT_SEL, 0},
5667ab6734e7SLydia Wang 
5668ab6734e7SLydia Wang 	/* PW9 Output enable */
5669ab6734e7SLydia Wang 	{0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
5670ab6734e7SLydia Wang 
5671ab6734e7SLydia Wang 	/* Enable Boost Volume backdoor */
5672ab6734e7SLydia Wang 	{0x1, 0xfb9, 0x24},
5673ab6734e7SLydia Wang 
5674ab6734e7SLydia Wang 	/* MW0/1/4/13/15: un-mute index 0 (MUXx), un-mute index 1 (MW9) */
5675ab6734e7SLydia Wang 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5676ab6734e7SLydia Wang 	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5677ab6734e7SLydia Wang 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5678ab6734e7SLydia Wang 	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5679ab6734e7SLydia Wang 	{0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5680ab6734e7SLydia Wang 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5681ab6734e7SLydia Wang 	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5682ab6734e7SLydia Wang 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5683ab6734e7SLydia Wang 	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5684ab6734e7SLydia Wang 	{0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5685ab6734e7SLydia Wang 
5686ab6734e7SLydia Wang 	/* set MUX0/1/4/13/15 = 0 (AOW0) */
5687ab6734e7SLydia Wang 	{0x34, AC_VERB_SET_CONNECT_SEL, 0},
5688ab6734e7SLydia Wang 	{0x35, AC_VERB_SET_CONNECT_SEL, 0},
5689ab6734e7SLydia Wang 	{0x38, AC_VERB_SET_CONNECT_SEL, 0},
5690ab6734e7SLydia Wang 	{0x3c, AC_VERB_SET_CONNECT_SEL, 0},
5691ab6734e7SLydia Wang 	{0x3d, AC_VERB_SET_CONNECT_SEL, 0},
5692ab6734e7SLydia Wang 
5693ab6734e7SLydia Wang 	/* Enable AOW0 to MW9 */
5694ab6734e7SLydia Wang 	{0x1, 0xfb8, 0xa8},
5695ab6734e7SLydia Wang 	{ }
5696ab6734e7SLydia Wang };
5697ab6734e7SLydia Wang 
5698ab6734e7SLydia Wang 
5699ab6734e7SLydia Wang static struct hda_verb vt1812_uniwill_init_verbs[] = {
5700ab6734e7SLydia Wang 	{0x33, AC_VERB_SET_UNSOLICITED_ENABLE,
5701ab6734e7SLydia Wang 	 AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5702ab6734e7SLydia Wang 	{0x25, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT },
5703ab6734e7SLydia Wang 	{0x28, AC_VERB_SET_UNSOLICITED_ENABLE,
5704ab6734e7SLydia Wang 	 AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5705ab6734e7SLydia Wang 	{0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5706ab6734e7SLydia Wang 	{0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5707ab6734e7SLydia Wang 	{0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5708ab6734e7SLydia Wang 	{ }
5709ab6734e7SLydia Wang };
5710ab6734e7SLydia Wang 
5711ab6734e7SLydia Wang static struct hda_pcm_stream vt1812_pcm_analog_playback = {
5712ab6734e7SLydia Wang 	.substreams = 2,
5713ab6734e7SLydia Wang 	.channels_min = 2,
5714ab6734e7SLydia Wang 	.channels_max = 2,
5715ab6734e7SLydia Wang 	.nid = 0x8, /* NID to query formats and rates */
5716ab6734e7SLydia Wang 	.ops = {
5717ab6734e7SLydia Wang 		.open = via_playback_pcm_open,
5718ab6734e7SLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
5719ab6734e7SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup,
5720ab6734e7SLydia Wang 		.close = via_pcm_open_close,
5721ab6734e7SLydia Wang 	},
5722ab6734e7SLydia Wang };
5723ab6734e7SLydia Wang 
5724ab6734e7SLydia Wang static struct hda_pcm_stream vt1812_pcm_analog_capture = {
5725ab6734e7SLydia Wang 	.substreams = 2,
5726ab6734e7SLydia Wang 	.channels_min = 2,
5727ab6734e7SLydia Wang 	.channels_max = 2,
5728ab6734e7SLydia Wang 	.nid = 0x10, /* NID to query formats and rates */
5729ab6734e7SLydia Wang 	.ops = {
5730ab6734e7SLydia Wang 		.open = via_pcm_open_close,
5731ab6734e7SLydia Wang 		.prepare = via_capture_pcm_prepare,
5732ab6734e7SLydia Wang 		.cleanup = via_capture_pcm_cleanup,
5733ab6734e7SLydia Wang 		.close = via_pcm_open_close,
5734ab6734e7SLydia Wang 	},
5735ab6734e7SLydia Wang };
5736ab6734e7SLydia Wang 
5737ab6734e7SLydia Wang static struct hda_pcm_stream vt1812_pcm_digital_playback = {
5738ab6734e7SLydia Wang 	.substreams = 1,
5739ab6734e7SLydia Wang 	.channels_min = 2,
5740ab6734e7SLydia Wang 	.channels_max = 2,
5741ab6734e7SLydia Wang 	/* NID is set in via_build_pcms */
5742ab6734e7SLydia Wang 	.ops = {
5743ab6734e7SLydia Wang 		.open = via_dig_playback_pcm_open,
5744ab6734e7SLydia Wang 		.close = via_dig_playback_pcm_close,
5745ab6734e7SLydia Wang 		.prepare = via_dig_playback_pcm_prepare,
5746ab6734e7SLydia Wang 		.cleanup = via_dig_playback_pcm_cleanup
5747ab6734e7SLydia Wang 	},
5748ab6734e7SLydia Wang };
5749ab6734e7SLydia Wang /* fill in the dac_nids table from the parsed pin configuration */
5750ab6734e7SLydia Wang static int vt1812_auto_fill_dac_nids(struct via_spec *spec,
5751ab6734e7SLydia Wang 				     const struct auto_pin_cfg *cfg)
5752ab6734e7SLydia Wang {
5753ab6734e7SLydia Wang 	spec->multiout.num_dacs = 1;
5754ab6734e7SLydia Wang 	spec->multiout.dac_nids = spec->private_dac_nids;
5755ab6734e7SLydia Wang 	if (cfg->line_out_pins[0])
5756ab6734e7SLydia Wang 		spec->multiout.dac_nids[0] = 0x8;
5757ab6734e7SLydia Wang 	return 0;
5758ab6734e7SLydia Wang }
5759ab6734e7SLydia Wang 
5760ab6734e7SLydia Wang 
5761ab6734e7SLydia Wang /* add playback controls from the parsed DAC table */
5762ab6734e7SLydia Wang static int vt1812_auto_create_multi_out_ctls(struct via_spec *spec,
5763ab6734e7SLydia Wang 					     const struct auto_pin_cfg *cfg)
5764ab6734e7SLydia Wang {
5765ab6734e7SLydia Wang 	int err;
5766ab6734e7SLydia Wang 
5767ab6734e7SLydia Wang 	if (!cfg->line_out_pins[0])
5768ab6734e7SLydia Wang 		return -1;
5769ab6734e7SLydia Wang 
5770ab6734e7SLydia Wang 	/* Line-Out: PortE */
5771ab6734e7SLydia Wang 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
57723d83e577STakashi Iwai 			      "Front Playback Volume",
5773ab6734e7SLydia Wang 			      HDA_COMPOSE_AMP_VAL(0x8, 3, 0, HDA_OUTPUT));
5774ab6734e7SLydia Wang 	if (err < 0)
5775ab6734e7SLydia Wang 		return err;
5776ab6734e7SLydia Wang 	err = via_add_control(spec, VIA_CTL_WIDGET_BIND_PIN_MUTE,
57773d83e577STakashi Iwai 			      "Front Playback Switch",
5778ab6734e7SLydia Wang 			      HDA_COMPOSE_AMP_VAL(0x28, 3, 0, HDA_OUTPUT));
5779ab6734e7SLydia Wang 	if (err < 0)
5780ab6734e7SLydia Wang 		return err;
5781ab6734e7SLydia Wang 
5782ab6734e7SLydia Wang 	return 0;
5783ab6734e7SLydia Wang }
5784ab6734e7SLydia Wang 
5785ab6734e7SLydia Wang static int vt1812_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
5786ab6734e7SLydia Wang {
5787ab6734e7SLydia Wang 	int err;
5788ab6734e7SLydia Wang 
5789ab6734e7SLydia Wang 	if (!pin)
5790ab6734e7SLydia Wang 		return 0;
5791ab6734e7SLydia Wang 
5792ab6734e7SLydia Wang 	spec->multiout.hp_nid = 0x9;
5793ab6734e7SLydia Wang 	spec->hp_independent_mode_index = 1;
5794ab6734e7SLydia Wang 
5795ab6734e7SLydia Wang 
5796ab6734e7SLydia Wang 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5797ab6734e7SLydia Wang 			      "Headphone Playback Volume",
5798ab6734e7SLydia Wang 			      HDA_COMPOSE_AMP_VAL(
5799ab6734e7SLydia Wang 				      spec->multiout.hp_nid, 3, 0, HDA_OUTPUT));
5800ab6734e7SLydia Wang 	if (err < 0)
5801ab6734e7SLydia Wang 		return err;
5802ab6734e7SLydia Wang 
5803ab6734e7SLydia Wang 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
5804ab6734e7SLydia Wang 			      "Headphone Playback Switch",
5805ab6734e7SLydia Wang 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
5806ab6734e7SLydia Wang 	if (err < 0)
5807ab6734e7SLydia Wang 		return err;
5808ab6734e7SLydia Wang 
5809ab6734e7SLydia Wang 	create_hp_imux(spec);
5810ab6734e7SLydia Wang 	return 0;
5811ab6734e7SLydia Wang }
5812ab6734e7SLydia Wang 
5813ab6734e7SLydia Wang /* create playback/capture controls for input pins */
5814*10a20af7STakashi Iwai static int vt1812_auto_create_analog_input_ctls(struct hda_codec *codec,
5815ab6734e7SLydia Wang 						const struct auto_pin_cfg *cfg)
5816ab6734e7SLydia Wang {
5817*10a20af7STakashi Iwai 	struct via_spec *spec = codec->spec;
5818ab6734e7SLydia Wang 	struct hda_input_mux *imux = &spec->private_imux[0];
5819f3268512STakashi Iwai 	static hda_nid_t pin_idxs[] = { 0x2b, 0x2a, 0x29, 0, 0, 0xff };
5820f3268512STakashi Iwai 	int err;
5821ab6734e7SLydia Wang 
5822*10a20af7STakashi Iwai 	err = vt_auto_create_analog_input_ctls(codec, cfg, 0x21, pin_idxs,
5823f3268512STakashi Iwai 					       ARRAY_SIZE(pin_idxs));
5824ab6734e7SLydia Wang 	if (err < 0)
5825ab6734e7SLydia Wang 		return err;
5826f3268512STakashi Iwai 
5827ab6734e7SLydia Wang 	/* build volume/mute control of loopback */
58287b315bb4STakashi Iwai 	err = via_new_analog_input(spec, "Stereo Mixer", 0, 5, 0x21);
5829ab6734e7SLydia Wang 	if (err < 0)
5830ab6734e7SLydia Wang 		return err;
5831ab6734e7SLydia Wang 
5832ab6734e7SLydia Wang 	/* for digital mic select */
5833*10a20af7STakashi Iwai 	snd_hda_add_imux_item(imux, "Digital Mic", 6, NULL);
5834ab6734e7SLydia Wang 
5835ab6734e7SLydia Wang 	return 0;
5836ab6734e7SLydia Wang }
5837ab6734e7SLydia Wang 
5838ab6734e7SLydia Wang static int vt1812_parse_auto_config(struct hda_codec *codec)
5839ab6734e7SLydia Wang {
5840ab6734e7SLydia Wang 	struct via_spec *spec = codec->spec;
5841ab6734e7SLydia Wang 	int err;
5842ab6734e7SLydia Wang 
5843ab6734e7SLydia Wang 
5844ab6734e7SLydia Wang 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
5845ab6734e7SLydia Wang 	if (err < 0)
5846ab6734e7SLydia Wang 		return err;
5847ab6734e7SLydia Wang 	fill_dig_outs(codec);
5848ab6734e7SLydia Wang 	err = vt1812_auto_fill_dac_nids(spec, &spec->autocfg);
5849ab6734e7SLydia Wang 	if (err < 0)
5850ab6734e7SLydia Wang 		return err;
5851ab6734e7SLydia Wang 
5852ab6734e7SLydia Wang 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_outs)
5853ab6734e7SLydia Wang 		return 0; /* can't find valid BIOS pin config */
5854ab6734e7SLydia Wang 
5855ab6734e7SLydia Wang 	err = vt1812_auto_create_multi_out_ctls(spec, &spec->autocfg);
5856ab6734e7SLydia Wang 	if (err < 0)
5857ab6734e7SLydia Wang 		return err;
5858ab6734e7SLydia Wang 	err = vt1812_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
5859ab6734e7SLydia Wang 	if (err < 0)
5860ab6734e7SLydia Wang 		return err;
5861*10a20af7STakashi Iwai 	err = vt1812_auto_create_analog_input_ctls(codec, &spec->autocfg);
5862ab6734e7SLydia Wang 	if (err < 0)
5863ab6734e7SLydia Wang 		return err;
5864ab6734e7SLydia Wang 
5865ab6734e7SLydia Wang 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
5866ab6734e7SLydia Wang 
5867ab6734e7SLydia Wang 	fill_dig_outs(codec);
5868ab6734e7SLydia Wang 
5869ab6734e7SLydia Wang 	if (spec->kctls.list)
5870ab6734e7SLydia Wang 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
5871ab6734e7SLydia Wang 
5872ab6734e7SLydia Wang 	spec->input_mux = &spec->private_imux[0];
5873ab6734e7SLydia Wang 
5874ab6734e7SLydia Wang 	if (spec->hp_mux)
58753d83e577STakashi Iwai 		via_hp_build(codec);
5876ab6734e7SLydia Wang 
5877ab6734e7SLydia Wang 	return 1;
5878ab6734e7SLydia Wang }
5879ab6734e7SLydia Wang 
5880ab6734e7SLydia Wang #ifdef CONFIG_SND_HDA_POWER_SAVE
5881ab6734e7SLydia Wang static struct hda_amp_list vt1812_loopbacks[] = {
5882ab6734e7SLydia Wang 	{ 0x21, HDA_INPUT, 0 },
5883ab6734e7SLydia Wang 	{ 0x21, HDA_INPUT, 1 },
5884ab6734e7SLydia Wang 	{ 0x21, HDA_INPUT, 2 },
5885ab6734e7SLydia Wang 	{ } /* end */
5886ab6734e7SLydia Wang };
5887ab6734e7SLydia Wang #endif
5888ab6734e7SLydia Wang 
5889ab6734e7SLydia Wang 
5890ab6734e7SLydia Wang /* patch for vt1812 */
5891ab6734e7SLydia Wang static int patch_vt1812(struct hda_codec *codec)
5892ab6734e7SLydia Wang {
5893ab6734e7SLydia Wang 	struct via_spec *spec;
5894ab6734e7SLydia Wang 	int err;
5895ab6734e7SLydia Wang 
5896ab6734e7SLydia Wang 	/* create a codec specific record */
58975b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
5898ab6734e7SLydia Wang 	if (spec == NULL)
5899ab6734e7SLydia Wang 		return -ENOMEM;
5900ab6734e7SLydia Wang 
5901ab6734e7SLydia Wang 	/* automatic parse from the BIOS config */
5902ab6734e7SLydia Wang 	err = vt1812_parse_auto_config(codec);
5903ab6734e7SLydia Wang 	if (err < 0) {
5904ab6734e7SLydia Wang 		via_free(codec);
5905ab6734e7SLydia Wang 		return err;
5906ab6734e7SLydia Wang 	} else if (!err) {
5907ab6734e7SLydia Wang 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
5908ab6734e7SLydia Wang 		       "from BIOS.  Using genenic mode...\n");
5909ab6734e7SLydia Wang 	}
5910ab6734e7SLydia Wang 
5911ab6734e7SLydia Wang 
5912ab6734e7SLydia Wang 	spec->init_verbs[spec->num_iverbs++]  = vt1812_volume_init_verbs;
5913ab6734e7SLydia Wang 	spec->init_verbs[spec->num_iverbs++] = vt1812_uniwill_init_verbs;
5914ab6734e7SLydia Wang 
5915ab6734e7SLydia Wang 	spec->stream_name_analog = "VT1812 Analog";
5916ab6734e7SLydia Wang 	spec->stream_analog_playback = &vt1812_pcm_analog_playback;
5917ab6734e7SLydia Wang 	spec->stream_analog_capture = &vt1812_pcm_analog_capture;
5918ab6734e7SLydia Wang 
5919ab6734e7SLydia Wang 	spec->stream_name_digital = "VT1812 Digital";
5920ab6734e7SLydia Wang 	spec->stream_digital_playback = &vt1812_pcm_digital_playback;
5921ab6734e7SLydia Wang 
5922ab6734e7SLydia Wang 
5923ab6734e7SLydia Wang 	if (!spec->adc_nids && spec->input_mux) {
5924ab6734e7SLydia Wang 		spec->adc_nids = vt1812_adc_nids;
5925ab6734e7SLydia Wang 		spec->num_adc_nids = ARRAY_SIZE(vt1812_adc_nids);
5926ab6734e7SLydia Wang 		get_mux_nids(codec);
5927ab6734e7SLydia Wang 		override_mic_boost(codec, 0x2b, 0, 3, 40);
5928ab6734e7SLydia Wang 		override_mic_boost(codec, 0x29, 0, 3, 40);
5929ab6734e7SLydia Wang 		spec->mixers[spec->num_mixers] = vt1812_capture_mixer;
5930ab6734e7SLydia Wang 		spec->num_mixers++;
5931ab6734e7SLydia Wang 	}
5932ab6734e7SLydia Wang 
5933ab6734e7SLydia Wang 	codec->patch_ops = via_patch_ops;
5934ab6734e7SLydia Wang 
5935ab6734e7SLydia Wang 	codec->patch_ops.init = via_auto_init;
59360f48327eSStephen Rothwell 	codec->patch_ops.unsol_event = via_unsol_event;
5937ab6734e7SLydia Wang 
5938ab6734e7SLydia Wang #ifdef CONFIG_SND_HDA_POWER_SAVE
5939ab6734e7SLydia Wang 	spec->loopback.amplist = vt1812_loopbacks;
5940ab6734e7SLydia Wang #endif
5941ab6734e7SLydia Wang 
5942ab6734e7SLydia Wang 	return 0;
5943ab6734e7SLydia Wang }
5944ab6734e7SLydia Wang 
5945c577b8a1SJoseph Chan /*
5946c577b8a1SJoseph Chan  * patch entries
5947c577b8a1SJoseph Chan  */
59481289e9e8STakashi Iwai static struct hda_codec_preset snd_hda_preset_via[] = {
59493218c178STakashi Iwai 	{ .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
59503218c178STakashi Iwai 	{ .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
59513218c178STakashi Iwai 	{ .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
59523218c178STakashi Iwai 	{ .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
59533218c178STakashi Iwai 	{ .id = 0x1106e710, .name = "VT1709 10-Ch",
5954f7278fd0SJosepch Chan 	  .patch = patch_vt1709_10ch},
59553218c178STakashi Iwai 	{ .id = 0x1106e711, .name = "VT1709 10-Ch",
5956f7278fd0SJosepch Chan 	  .patch = patch_vt1709_10ch},
59573218c178STakashi Iwai 	{ .id = 0x1106e712, .name = "VT1709 10-Ch",
5958f7278fd0SJosepch Chan 	  .patch = patch_vt1709_10ch},
59593218c178STakashi Iwai 	{ .id = 0x1106e713, .name = "VT1709 10-Ch",
5960f7278fd0SJosepch Chan 	  .patch = patch_vt1709_10ch},
59613218c178STakashi Iwai 	{ .id = 0x1106e714, .name = "VT1709 6-Ch",
5962f7278fd0SJosepch Chan 	  .patch = patch_vt1709_6ch},
59633218c178STakashi Iwai 	{ .id = 0x1106e715, .name = "VT1709 6-Ch",
5964f7278fd0SJosepch Chan 	  .patch = patch_vt1709_6ch},
59653218c178STakashi Iwai 	{ .id = 0x1106e716, .name = "VT1709 6-Ch",
5966f7278fd0SJosepch Chan 	  .patch = patch_vt1709_6ch},
59673218c178STakashi Iwai 	{ .id = 0x1106e717, .name = "VT1709 6-Ch",
5968f7278fd0SJosepch Chan 	  .patch = patch_vt1709_6ch},
59693218c178STakashi Iwai 	{ .id = 0x1106e720, .name = "VT1708B 8-Ch",
5970f7278fd0SJosepch Chan 	  .patch = patch_vt1708B_8ch},
59713218c178STakashi Iwai 	{ .id = 0x1106e721, .name = "VT1708B 8-Ch",
5972f7278fd0SJosepch Chan 	  .patch = patch_vt1708B_8ch},
59733218c178STakashi Iwai 	{ .id = 0x1106e722, .name = "VT1708B 8-Ch",
5974f7278fd0SJosepch Chan 	  .patch = patch_vt1708B_8ch},
59753218c178STakashi Iwai 	{ .id = 0x1106e723, .name = "VT1708B 8-Ch",
5976f7278fd0SJosepch Chan 	  .patch = patch_vt1708B_8ch},
59773218c178STakashi Iwai 	{ .id = 0x1106e724, .name = "VT1708B 4-Ch",
5978f7278fd0SJosepch Chan 	  .patch = patch_vt1708B_4ch},
59793218c178STakashi Iwai 	{ .id = 0x1106e725, .name = "VT1708B 4-Ch",
5980f7278fd0SJosepch Chan 	  .patch = patch_vt1708B_4ch},
59813218c178STakashi Iwai 	{ .id = 0x1106e726, .name = "VT1708B 4-Ch",
5982f7278fd0SJosepch Chan 	  .patch = patch_vt1708B_4ch},
59833218c178STakashi Iwai 	{ .id = 0x1106e727, .name = "VT1708B 4-Ch",
5984f7278fd0SJosepch Chan 	  .patch = patch_vt1708B_4ch},
59853218c178STakashi Iwai 	{ .id = 0x11060397, .name = "VT1708S",
5986d949cac1SHarald Welte 	  .patch = patch_vt1708S},
59873218c178STakashi Iwai 	{ .id = 0x11061397, .name = "VT1708S",
5988d949cac1SHarald Welte 	  .patch = patch_vt1708S},
59893218c178STakashi Iwai 	{ .id = 0x11062397, .name = "VT1708S",
5990d949cac1SHarald Welte 	  .patch = patch_vt1708S},
59913218c178STakashi Iwai 	{ .id = 0x11063397, .name = "VT1708S",
5992d949cac1SHarald Welte 	  .patch = patch_vt1708S},
59933218c178STakashi Iwai 	{ .id = 0x11064397, .name = "VT1708S",
5994d949cac1SHarald Welte 	  .patch = patch_vt1708S},
59953218c178STakashi Iwai 	{ .id = 0x11065397, .name = "VT1708S",
5996d949cac1SHarald Welte 	  .patch = patch_vt1708S},
59973218c178STakashi Iwai 	{ .id = 0x11066397, .name = "VT1708S",
5998d949cac1SHarald Welte 	  .patch = patch_vt1708S},
59993218c178STakashi Iwai 	{ .id = 0x11067397, .name = "VT1708S",
6000d949cac1SHarald Welte 	  .patch = patch_vt1708S},
60013218c178STakashi Iwai 	{ .id = 0x11060398, .name = "VT1702",
6002d949cac1SHarald Welte 	  .patch = patch_vt1702},
60033218c178STakashi Iwai 	{ .id = 0x11061398, .name = "VT1702",
6004d949cac1SHarald Welte 	  .patch = patch_vt1702},
60053218c178STakashi Iwai 	{ .id = 0x11062398, .name = "VT1702",
6006d949cac1SHarald Welte 	  .patch = patch_vt1702},
60073218c178STakashi Iwai 	{ .id = 0x11063398, .name = "VT1702",
6008d949cac1SHarald Welte 	  .patch = patch_vt1702},
60093218c178STakashi Iwai 	{ .id = 0x11064398, .name = "VT1702",
6010d949cac1SHarald Welte 	  .patch = patch_vt1702},
60113218c178STakashi Iwai 	{ .id = 0x11065398, .name = "VT1702",
6012d949cac1SHarald Welte 	  .patch = patch_vt1702},
60133218c178STakashi Iwai 	{ .id = 0x11066398, .name = "VT1702",
6014d949cac1SHarald Welte 	  .patch = patch_vt1702},
60153218c178STakashi Iwai 	{ .id = 0x11067398, .name = "VT1702",
6016d949cac1SHarald Welte 	  .patch = patch_vt1702},
6017eb7188caSLydia Wang 	{ .id = 0x11060428, .name = "VT1718S",
6018eb7188caSLydia Wang 	  .patch = patch_vt1718S},
6019eb7188caSLydia Wang 	{ .id = 0x11064428, .name = "VT1718S",
6020eb7188caSLydia Wang 	  .patch = patch_vt1718S},
6021bb3c6bfcSLydia Wang 	{ .id = 0x11060441, .name = "VT2020",
6022bb3c6bfcSLydia Wang 	  .patch = patch_vt1718S},
6023bb3c6bfcSLydia Wang 	{ .id = 0x11064441, .name = "VT1828S",
6024bb3c6bfcSLydia Wang 	  .patch = patch_vt1718S},
6025f3db423dSLydia Wang 	{ .id = 0x11060433, .name = "VT1716S",
6026f3db423dSLydia Wang 	  .patch = patch_vt1716S},
6027f3db423dSLydia Wang 	{ .id = 0x1106a721, .name = "VT1716S",
6028f3db423dSLydia Wang 	  .patch = patch_vt1716S},
602925eaba2fSLydia Wang 	{ .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
603025eaba2fSLydia Wang 	{ .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
6031ab6734e7SLydia Wang 	{ .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
603236dd5c4aSLydia Wang 	{ .id = 0x11060440, .name = "VT1818S",
603336dd5c4aSLydia Wang 	  .patch = patch_vt1708S},
6034c577b8a1SJoseph Chan 	{} /* terminator */
6035c577b8a1SJoseph Chan };
60361289e9e8STakashi Iwai 
60371289e9e8STakashi Iwai MODULE_ALIAS("snd-hda-codec-id:1106*");
60381289e9e8STakashi Iwai 
60391289e9e8STakashi Iwai static struct hda_codec_preset_list via_list = {
60401289e9e8STakashi Iwai 	.preset = snd_hda_preset_via,
60411289e9e8STakashi Iwai 	.owner = THIS_MODULE,
60421289e9e8STakashi Iwai };
60431289e9e8STakashi Iwai 
60441289e9e8STakashi Iwai MODULE_LICENSE("GPL");
60451289e9e8STakashi Iwai MODULE_DESCRIPTION("VIA HD-audio codec");
60461289e9e8STakashi Iwai 
60471289e9e8STakashi Iwai static int __init patch_via_init(void)
60481289e9e8STakashi Iwai {
60491289e9e8STakashi Iwai 	return snd_hda_add_codec_preset(&via_list);
60501289e9e8STakashi Iwai }
60511289e9e8STakashi Iwai 
60521289e9e8STakashi Iwai static void __exit patch_via_exit(void)
60531289e9e8STakashi Iwai {
60541289e9e8STakashi Iwai 	snd_hda_delete_codec_preset(&via_list);
60551289e9e8STakashi Iwai }
60561289e9e8STakashi Iwai 
60571289e9e8STakashi Iwai module_init(patch_via_init)
60581289e9e8STakashi Iwai module_exit(patch_via_exit)
6059