xref: /openbmc/linux/sound/pci/hda/patch_via.c (revision 9499473463628a1af4be5aea1ad8d35d3fd341b0)
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 
57c577b8a1SJoseph Chan /* Pin Widget NID */
5876d9b0ddSHarald Welte #define VT1708_HP_PIN_NID	0x20
5976d9b0ddSHarald Welte #define VT1708_CD_PIN_NID	0x24
60c577b8a1SJoseph Chan 
61d7426329SHarald Welte enum VIA_HDA_CODEC {
62d7426329SHarald Welte 	UNKNOWN = -1,
63d7426329SHarald Welte 	VT1708,
64d7426329SHarald Welte 	VT1709_10CH,
65d7426329SHarald Welte 	VT1709_6CH,
66d7426329SHarald Welte 	VT1708B_8CH,
67d7426329SHarald Welte 	VT1708B_4CH,
68d7426329SHarald Welte 	VT1708S,
69518bf3baSLydia Wang 	VT1708BCE,
70d7426329SHarald Welte 	VT1702,
71eb7188caSLydia Wang 	VT1718S,
72f3db423dSLydia Wang 	VT1716S,
7325eaba2fSLydia Wang 	VT2002P,
74ab6734e7SLydia Wang 	VT1812,
7511890956SLydia Wang 	VT1802,
76d7426329SHarald Welte 	CODEC_TYPES,
77d7426329SHarald Welte };
78d7426329SHarald Welte 
7911890956SLydia Wang #define VT2002P_COMPATIBLE(spec) \
8011890956SLydia Wang 	((spec)->codec_type == VT2002P ||\
8111890956SLydia Wang 	 (spec)->codec_type == VT1812 ||\
8211890956SLydia Wang 	 (spec)->codec_type == VT1802)
8311890956SLydia Wang 
848e3679dcSTakashi Iwai #define MAX_NID_PATH_DEPTH	5
858e3679dcSTakashi Iwai 
8609a9ad69STakashi Iwai /* output-path: DAC -> ... -> pin
8709a9ad69STakashi Iwai  * idx[] contains the source index number of the next widget;
8809a9ad69STakashi Iwai  * e.g. idx[0] is the index of the DAC selected by path[1] widget
8909a9ad69STakashi Iwai  * multi[] indicates whether it's a selector widget with multi-connectors
9009a9ad69STakashi Iwai  * (i.e. the connection selection is mandatory)
9109a9ad69STakashi Iwai  * vol_ctl and mute_ctl contains the NIDs for the assigned mixers
9209a9ad69STakashi Iwai  */
934a79616dSTakashi Iwai struct nid_path {
944a79616dSTakashi Iwai 	int depth;
958e3679dcSTakashi Iwai 	hda_nid_t path[MAX_NID_PATH_DEPTH];
9609a9ad69STakashi Iwai 	unsigned char idx[MAX_NID_PATH_DEPTH];
9709a9ad69STakashi Iwai 	unsigned char multi[MAX_NID_PATH_DEPTH];
9809a9ad69STakashi Iwai 	unsigned int vol_ctl;
9909a9ad69STakashi Iwai 	unsigned int mute_ctl;
1004a79616dSTakashi Iwai };
1014a79616dSTakashi Iwai 
102a86a88eaSTakashi Iwai /* input-path */
103a86a88eaSTakashi Iwai struct via_input {
104a86a88eaSTakashi Iwai 	hda_nid_t pin;	/* input-pin or aa-mix */
105a86a88eaSTakashi Iwai 	int adc_idx;	/* ADC index to be used */
106a86a88eaSTakashi Iwai 	int mux_idx;	/* MUX index (if any) */
107a86a88eaSTakashi Iwai 	const char *label;	/* input-source label */
108a86a88eaSTakashi Iwai };
109a86a88eaSTakashi Iwai 
110de6c74f3STakashi Iwai #define VIA_MAX_ADCS	3
111de6c74f3STakashi Iwai 
1121f2e99feSLydia Wang struct via_spec {
1131f2e99feSLydia Wang 	/* codec parameterization */
11490dd48a1STakashi Iwai 	const struct snd_kcontrol_new *mixers[6];
1151f2e99feSLydia Wang 	unsigned int num_mixers;
1161f2e99feSLydia Wang 
11790dd48a1STakashi Iwai 	const struct hda_verb *init_verbs[5];
1181f2e99feSLydia Wang 	unsigned int num_iverbs;
1191f2e99feSLydia Wang 
12082673bc8STakashi Iwai 	char stream_name_analog[32];
1217eb56e84STakashi Iwai 	char stream_name_hp[32];
12290dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_analog_playback;
12390dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_analog_capture;
1241f2e99feSLydia Wang 
12582673bc8STakashi Iwai 	char stream_name_digital[32];
12690dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_digital_playback;
12790dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_digital_capture;
1281f2e99feSLydia Wang 
1291f2e99feSLydia Wang 	/* playback */
1301f2e99feSLydia Wang 	struct hda_multi_out multiout;
1311f2e99feSLydia Wang 	hda_nid_t slave_dig_outs[2];
132ece8d043STakashi Iwai 	hda_nid_t hp_dac_nid;
13325250505STakashi Iwai 	bool hp_indep_shared;	/* indep HP-DAC is shared with side ch */
134ada509ecSTakashi Iwai 	int num_active_streams;
135c4394f5bSLydia Wang 	int dac_mixer_idx;
1361f2e99feSLydia Wang 
137de6c74f3STakashi Iwai 	struct nid_path out_path[HDA_SIDE + 1];
1384a79616dSTakashi Iwai 	struct nid_path hp_path;
1394a79616dSTakashi Iwai 	struct nid_path hp_dep_path;
1404a918ffeSTakashi Iwai 	struct nid_path speaker_path;
1414a79616dSTakashi Iwai 
1421f2e99feSLydia Wang 	/* capture */
1431f2e99feSLydia Wang 	unsigned int num_adc_nids;
144de6c74f3STakashi Iwai 	hda_nid_t adc_nids[VIA_MAX_ADCS];
145de6c74f3STakashi Iwai 	hda_nid_t mux_nids[VIA_MAX_ADCS];
146620e2b28STakashi Iwai 	hda_nid_t aa_mix_nid;
1471f2e99feSLydia Wang 	hda_nid_t dig_in_nid;
1481f2e99feSLydia Wang 
1491f2e99feSLydia Wang 	/* capture source */
150a86a88eaSTakashi Iwai 	bool dyn_adc_switch;
151a86a88eaSTakashi Iwai 	int num_inputs;
152a86a88eaSTakashi Iwai 	struct via_input inputs[AUTO_CFG_MAX_INS + 1];
153de6c74f3STakashi Iwai 	unsigned int cur_mux[VIA_MAX_ADCS];
1541f2e99feSLydia Wang 
155a86a88eaSTakashi Iwai 	/* dynamic ADC switching */
156a86a88eaSTakashi Iwai 	hda_nid_t cur_adc;
157a86a88eaSTakashi Iwai 	unsigned int cur_adc_stream_tag;
158a86a88eaSTakashi Iwai 	unsigned int cur_adc_format;
159a86a88eaSTakashi Iwai 
1601f2e99feSLydia Wang 	/* PCM information */
1611f2e99feSLydia Wang 	struct hda_pcm pcm_rec[3];
1621f2e99feSLydia Wang 
1631f2e99feSLydia Wang 	/* dynamic controls, init_verbs and input_mux */
1641f2e99feSLydia Wang 	struct auto_pin_cfg autocfg;
1651f2e99feSLydia Wang 	struct snd_array kctls;
1661f2e99feSLydia Wang 	hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
1671f2e99feSLydia Wang 
1681f2e99feSLydia Wang 	/* HP mode source */
1691f2e99feSLydia Wang 	unsigned int hp_independent_mode;
170f3db423dSLydia Wang 	unsigned int dmic_enabled;
17124088a58STakashi Iwai 	unsigned int no_pin_power_ctl;
1721f2e99feSLydia Wang 	enum VIA_HDA_CODEC codec_type;
1731f2e99feSLydia Wang 
174e3d7a143STakashi Iwai 	/* smart51 setup */
175e3d7a143STakashi Iwai 	unsigned int smart51_nums;
176e3d7a143STakashi Iwai 	hda_nid_t smart51_pins[2];
177e3d7a143STakashi Iwai 	int smart51_idxs[2];
178e3d7a143STakashi Iwai 	const char *smart51_labels[2];
179e3d7a143STakashi Iwai 	unsigned int smart51_enabled;
180e3d7a143STakashi Iwai 
1811f2e99feSLydia Wang 	/* work to check hp jack state */
1821f2e99feSLydia Wang 	struct hda_codec *codec;
1831f2e99feSLydia Wang 	struct delayed_work vt1708_hp_work;
184e06e5a29STakashi Iwai 	int vt1708_jack_detect;
1851f2e99feSLydia Wang 	int vt1708_hp_present;
1863e95b9abSLydia Wang 
1873e95b9abSLydia Wang 	void (*set_widgets_power_state)(struct hda_codec *codec);
1883e95b9abSLydia Wang 
1891f2e99feSLydia Wang 	struct hda_loopback_check loopback;
19013af8e77STakashi Iwai 	int num_loopbacks;
19113af8e77STakashi Iwai 	struct hda_amp_list loopback_list[8];
192a86a88eaSTakashi Iwai 
193a86a88eaSTakashi Iwai 	/* bind capture-volume */
194a86a88eaSTakashi Iwai 	struct hda_bind_ctls *bind_cap_vol;
195a86a88eaSTakashi Iwai 	struct hda_bind_ctls *bind_cap_sw;
1961f2e99feSLydia Wang };
1971f2e99feSLydia Wang 
1980341ccd7SLydia Wang static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
1995b0cb1d8SJaroslav Kysela static struct via_spec * via_new_spec(struct hda_codec *codec)
2005b0cb1d8SJaroslav Kysela {
2015b0cb1d8SJaroslav Kysela 	struct via_spec *spec;
2025b0cb1d8SJaroslav Kysela 
2035b0cb1d8SJaroslav Kysela 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2045b0cb1d8SJaroslav Kysela 	if (spec == NULL)
2055b0cb1d8SJaroslav Kysela 		return NULL;
2065b0cb1d8SJaroslav Kysela 
2075b0cb1d8SJaroslav Kysela 	codec->spec = spec;
2085b0cb1d8SJaroslav Kysela 	spec->codec = codec;
2090341ccd7SLydia Wang 	spec->codec_type = get_codec_type(codec);
2100341ccd7SLydia Wang 	/* VT1708BCE & VT1708S are almost same */
2110341ccd7SLydia Wang 	if (spec->codec_type == VT1708BCE)
2120341ccd7SLydia Wang 		spec->codec_type = VT1708S;
2135b0cb1d8SJaroslav Kysela 	return spec;
2145b0cb1d8SJaroslav Kysela }
2155b0cb1d8SJaroslav Kysela 
216744ff5f4SLydia Wang static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
217d7426329SHarald Welte {
218744ff5f4SLydia Wang 	u32 vendor_id = codec->vendor_id;
219d7426329SHarald Welte 	u16 ven_id = vendor_id >> 16;
220d7426329SHarald Welte 	u16 dev_id = vendor_id & 0xffff;
221d7426329SHarald Welte 	enum VIA_HDA_CODEC codec_type;
222d7426329SHarald Welte 
223d7426329SHarald Welte 	/* get codec type */
224d7426329SHarald Welte 	if (ven_id != 0x1106)
225d7426329SHarald Welte 		codec_type = UNKNOWN;
226d7426329SHarald Welte 	else if (dev_id >= 0x1708 && dev_id <= 0x170b)
227d7426329SHarald Welte 		codec_type = VT1708;
228d7426329SHarald Welte 	else if (dev_id >= 0xe710 && dev_id <= 0xe713)
229d7426329SHarald Welte 		codec_type = VT1709_10CH;
230d7426329SHarald Welte 	else if (dev_id >= 0xe714 && dev_id <= 0xe717)
231d7426329SHarald Welte 		codec_type = VT1709_6CH;
232518bf3baSLydia Wang 	else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
233d7426329SHarald Welte 		codec_type = VT1708B_8CH;
234518bf3baSLydia Wang 		if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
235518bf3baSLydia Wang 			codec_type = VT1708BCE;
236518bf3baSLydia Wang 	} else if (dev_id >= 0xe724 && dev_id <= 0xe727)
237d7426329SHarald Welte 		codec_type = VT1708B_4CH;
238d7426329SHarald Welte 	else if ((dev_id & 0xfff) == 0x397
239d7426329SHarald Welte 		 && (dev_id >> 12) < 8)
240d7426329SHarald Welte 		codec_type = VT1708S;
241d7426329SHarald Welte 	else if ((dev_id & 0xfff) == 0x398
242d7426329SHarald Welte 		 && (dev_id >> 12) < 8)
243d7426329SHarald Welte 		codec_type = VT1702;
244eb7188caSLydia Wang 	else if ((dev_id & 0xfff) == 0x428
245eb7188caSLydia Wang 		 && (dev_id >> 12) < 8)
246eb7188caSLydia Wang 		codec_type = VT1718S;
247f3db423dSLydia Wang 	else if (dev_id == 0x0433 || dev_id == 0xa721)
248f3db423dSLydia Wang 		codec_type = VT1716S;
249bb3c6bfcSLydia Wang 	else if (dev_id == 0x0441 || dev_id == 0x4441)
250bb3c6bfcSLydia Wang 		codec_type = VT1718S;
25125eaba2fSLydia Wang 	else if (dev_id == 0x0438 || dev_id == 0x4438)
25225eaba2fSLydia Wang 		codec_type = VT2002P;
253ab6734e7SLydia Wang 	else if (dev_id == 0x0448)
254ab6734e7SLydia Wang 		codec_type = VT1812;
25536dd5c4aSLydia Wang 	else if (dev_id == 0x0440)
25636dd5c4aSLydia Wang 		codec_type = VT1708S;
25711890956SLydia Wang 	else if ((dev_id & 0xfff) == 0x446)
25811890956SLydia Wang 		codec_type = VT1802;
259d7426329SHarald Welte 	else
260d7426329SHarald Welte 		codec_type = UNKNOWN;
261d7426329SHarald Welte 	return codec_type;
262d7426329SHarald Welte };
263d7426329SHarald Welte 
264ec7e7e42SLydia Wang #define VIA_JACK_EVENT		0x20
26569e52a80SHarald Welte #define VIA_HP_EVENT		0x01
26669e52a80SHarald Welte #define VIA_GPIO_EVENT		0x02
2674a918ffeSTakashi Iwai #define VIA_LINE_EVENT		0x03
26869e52a80SHarald Welte 
269c577b8a1SJoseph Chan enum {
270c577b8a1SJoseph Chan 	VIA_CTL_WIDGET_VOL,
271c577b8a1SJoseph Chan 	VIA_CTL_WIDGET_MUTE,
272f5271101SLydia Wang 	VIA_CTL_WIDGET_ANALOG_MUTE,
273c577b8a1SJoseph Chan };
274c577b8a1SJoseph Chan 
275ada509ecSTakashi Iwai static void analog_low_current_mode(struct hda_codec *codec);
276ada509ecSTakashi Iwai static bool is_aa_path_mute(struct hda_codec *codec);
2771f2e99feSLydia Wang 
2781f2e99feSLydia Wang static void vt1708_start_hp_work(struct via_spec *spec)
2791f2e99feSLydia Wang {
2801f2e99feSLydia Wang 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
2811f2e99feSLydia Wang 		return;
2821f2e99feSLydia Wang 	snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
283e06e5a29STakashi Iwai 			    !spec->vt1708_jack_detect);
2841f2e99feSLydia Wang 	if (!delayed_work_pending(&spec->vt1708_hp_work))
2851f2e99feSLydia Wang 		schedule_delayed_work(&spec->vt1708_hp_work,
2861f2e99feSLydia Wang 				      msecs_to_jiffies(100));
2871f2e99feSLydia Wang }
2881f2e99feSLydia Wang 
2891f2e99feSLydia Wang static void vt1708_stop_hp_work(struct via_spec *spec)
2901f2e99feSLydia Wang {
2911f2e99feSLydia Wang 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
2921f2e99feSLydia Wang 		return;
2931f2e99feSLydia Wang 	if (snd_hda_get_bool_hint(spec->codec, "analog_loopback_hp_detect") == 1
2941f2e99feSLydia Wang 	    && !is_aa_path_mute(spec->codec))
2951f2e99feSLydia Wang 		return;
2961f2e99feSLydia Wang 	snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
297e06e5a29STakashi Iwai 			    !spec->vt1708_jack_detect);
2985b84ba26STejun Heo 	cancel_delayed_work_sync(&spec->vt1708_hp_work);
2991f2e99feSLydia Wang }
300f5271101SLydia Wang 
3013e95b9abSLydia Wang static void set_widgets_power_state(struct hda_codec *codec)
3023e95b9abSLydia Wang {
3033e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
3043e95b9abSLydia Wang 	if (spec->set_widgets_power_state)
3053e95b9abSLydia Wang 		spec->set_widgets_power_state(codec);
3063e95b9abSLydia Wang }
30725eaba2fSLydia Wang 
308f5271101SLydia Wang static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
309f5271101SLydia Wang 				   struct snd_ctl_elem_value *ucontrol)
310f5271101SLydia Wang {
311f5271101SLydia Wang 	int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
312f5271101SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
313f5271101SLydia Wang 
3143e95b9abSLydia Wang 	set_widgets_power_state(codec);
315ada509ecSTakashi Iwai 	analog_low_current_mode(snd_kcontrol_chip(kcontrol));
3161f2e99feSLydia Wang 	if (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1) {
3171f2e99feSLydia Wang 		if (is_aa_path_mute(codec))
3181f2e99feSLydia Wang 			vt1708_start_hp_work(codec->spec);
3191f2e99feSLydia Wang 		else
3201f2e99feSLydia Wang 			vt1708_stop_hp_work(codec->spec);
3211f2e99feSLydia Wang 	}
322f5271101SLydia Wang 	return change;
323f5271101SLydia Wang }
324f5271101SLydia Wang 
325f5271101SLydia Wang /* modify .put = snd_hda_mixer_amp_switch_put */
326f5271101SLydia Wang #define ANALOG_INPUT_MUTE						\
327f5271101SLydia Wang 	{		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,		\
328f5271101SLydia Wang 			.name = NULL,					\
329f5271101SLydia Wang 			.index = 0,					\
330f5271101SLydia Wang 			.info = snd_hda_mixer_amp_switch_info,		\
331f5271101SLydia Wang 			.get = snd_hda_mixer_amp_switch_get,		\
332f5271101SLydia Wang 			.put = analog_input_switch_put,			\
333f5271101SLydia Wang 			.private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
334f5271101SLydia Wang 
33590dd48a1STakashi Iwai static const struct snd_kcontrol_new via_control_templates[] = {
336c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME(NULL, 0, 0, 0),
337c577b8a1SJoseph Chan 	HDA_CODEC_MUTE(NULL, 0, 0, 0),
338f5271101SLydia Wang 	ANALOG_INPUT_MUTE,
339c577b8a1SJoseph Chan };
340c577b8a1SJoseph Chan 
341ab6734e7SLydia Wang 
342c577b8a1SJoseph Chan /* add dynamic controls */
343291c9e33STakashi Iwai static struct snd_kcontrol_new *__via_clone_ctl(struct via_spec *spec,
344291c9e33STakashi Iwai 				const struct snd_kcontrol_new *tmpl,
345291c9e33STakashi Iwai 				const char *name)
346c577b8a1SJoseph Chan {
347c577b8a1SJoseph Chan 	struct snd_kcontrol_new *knew;
348c577b8a1SJoseph Chan 
349603c4019STakashi Iwai 	snd_array_init(&spec->kctls, sizeof(*knew), 32);
350603c4019STakashi Iwai 	knew = snd_array_new(&spec->kctls);
351c577b8a1SJoseph Chan 	if (!knew)
352291c9e33STakashi Iwai 		return NULL;
353291c9e33STakashi Iwai 	*knew = *tmpl;
354291c9e33STakashi Iwai 	if (!name)
355291c9e33STakashi Iwai 		name = tmpl->name;
356291c9e33STakashi Iwai 	if (name) {
357c577b8a1SJoseph Chan 		knew->name = kstrdup(name, GFP_KERNEL);
358c577b8a1SJoseph Chan 		if (!knew->name)
359291c9e33STakashi Iwai 			return NULL;
360291c9e33STakashi Iwai 	}
361291c9e33STakashi Iwai 	return knew;
362291c9e33STakashi Iwai }
363291c9e33STakashi Iwai 
364291c9e33STakashi Iwai static int __via_add_control(struct via_spec *spec, int type, const char *name,
365291c9e33STakashi Iwai 			     int idx, unsigned long val)
366291c9e33STakashi Iwai {
367291c9e33STakashi Iwai 	struct snd_kcontrol_new *knew;
368291c9e33STakashi Iwai 
369291c9e33STakashi Iwai 	knew = __via_clone_ctl(spec, &via_control_templates[type], name);
370291c9e33STakashi Iwai 	if (!knew)
371c577b8a1SJoseph Chan 		return -ENOMEM;
372d7a99cceSTakashi Iwai 	knew->index = idx;
3734d02d1b6SJaroslav Kysela 	if (get_amp_nid_(val))
3745e26dfd0SJaroslav Kysela 		knew->subdevice = HDA_SUBDEV_AMP_FLAG;
375c577b8a1SJoseph Chan 	knew->private_value = val;
376c577b8a1SJoseph Chan 	return 0;
377c577b8a1SJoseph Chan }
378c577b8a1SJoseph Chan 
3797b315bb4STakashi Iwai #define via_add_control(spec, type, name, val) \
3807b315bb4STakashi Iwai 	__via_add_control(spec, type, name, 0, val)
3817b315bb4STakashi Iwai 
382291c9e33STakashi Iwai #define via_clone_control(spec, tmpl) __via_clone_ctl(spec, tmpl, NULL)
3835b0cb1d8SJaroslav Kysela 
384603c4019STakashi Iwai static void via_free_kctls(struct hda_codec *codec)
385603c4019STakashi Iwai {
386603c4019STakashi Iwai 	struct via_spec *spec = codec->spec;
387603c4019STakashi Iwai 
388603c4019STakashi Iwai 	if (spec->kctls.list) {
389603c4019STakashi Iwai 		struct snd_kcontrol_new *kctl = spec->kctls.list;
390603c4019STakashi Iwai 		int i;
391603c4019STakashi Iwai 		for (i = 0; i < spec->kctls.used; i++)
392603c4019STakashi Iwai 			kfree(kctl[i].name);
393603c4019STakashi Iwai 	}
394603c4019STakashi Iwai 	snd_array_free(&spec->kctls);
395603c4019STakashi Iwai }
396603c4019STakashi Iwai 
397c577b8a1SJoseph Chan /* create input playback/capture controls for the given pin */
3989510e8ddSLydia Wang static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
3997b315bb4STakashi Iwai 				int type_idx, int idx, int mix_nid)
400c577b8a1SJoseph Chan {
401c577b8a1SJoseph Chan 	char name[32];
402c577b8a1SJoseph Chan 	int err;
403c577b8a1SJoseph Chan 
404c577b8a1SJoseph Chan 	sprintf(name, "%s Playback Volume", ctlname);
4057b315bb4STakashi Iwai 	err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
406c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
407c577b8a1SJoseph Chan 	if (err < 0)
408c577b8a1SJoseph Chan 		return err;
409c577b8a1SJoseph Chan 	sprintf(name, "%s Playback Switch", ctlname);
4107b315bb4STakashi Iwai 	err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
411c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
412c577b8a1SJoseph Chan 	if (err < 0)
413c577b8a1SJoseph Chan 		return err;
414c577b8a1SJoseph Chan 	return 0;
415c577b8a1SJoseph Chan }
416c577b8a1SJoseph Chan 
4175d41762aSTakashi Iwai #define get_connection_index(codec, mux, nid) \
4188d087c76STakashi Iwai 	snd_hda_get_conn_index(codec, mux, nid, 0)
4195d41762aSTakashi Iwai 
4208df2a312STakashi Iwai static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
4218df2a312STakashi Iwai 			   unsigned int mask)
4228df2a312STakashi Iwai {
423a934d5a9STakashi Iwai 	unsigned int caps;
424a934d5a9STakashi Iwai 	if (!nid)
425a934d5a9STakashi Iwai 		return false;
426a934d5a9STakashi Iwai 	caps = get_wcaps(codec, nid);
4278df2a312STakashi Iwai 	if (dir == HDA_INPUT)
4288df2a312STakashi Iwai 		caps &= AC_WCAP_IN_AMP;
4298df2a312STakashi Iwai 	else
4308df2a312STakashi Iwai 		caps &= AC_WCAP_OUT_AMP;
4318df2a312STakashi Iwai 	if (!caps)
4328df2a312STakashi Iwai 		return false;
4338df2a312STakashi Iwai 	if (query_amp_caps(codec, nid, dir) & mask)
4348df2a312STakashi Iwai 		return true;
4358df2a312STakashi Iwai 	return false;
4368df2a312STakashi Iwai }
4378df2a312STakashi Iwai 
43809a9ad69STakashi Iwai #define have_mute(codec, nid, dir) \
43909a9ad69STakashi Iwai 	check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
4408df2a312STakashi Iwai 
441d69607b3SLydia Wang static bool is_node_in_path(struct nid_path *path, hda_nid_t nid)
442d69607b3SLydia Wang {
443d69607b3SLydia Wang 	int i;
444d69607b3SLydia Wang 	if (!nid)
445d69607b3SLydia Wang 		return false;
446d69607b3SLydia Wang 	for (i = 0; i < path->depth; i++) {
447d69607b3SLydia Wang 		if (path->path[i] == nid)
448d69607b3SLydia Wang 			return true;
449d69607b3SLydia Wang 	}
450d69607b3SLydia Wang 	return false;
451d69607b3SLydia Wang }
452d69607b3SLydia Wang 
453d69607b3SLydia Wang /* enable/disable the output-route mixers */
454d69607b3SLydia Wang static void activate_output_mix(struct hda_codec *codec, struct nid_path *path,
455d69607b3SLydia Wang 				 hda_nid_t mix_nid, int aa_mix_idx, bool enable)
456d69607b3SLydia Wang {
457d69607b3SLydia Wang 	int i, num, val;
458d69607b3SLydia Wang 	bool hp_path, front_path;
459d69607b3SLydia Wang 	struct via_spec *spec = codec->spec;
460d69607b3SLydia Wang 
461d69607b3SLydia Wang 	if (!path)
462d69607b3SLydia Wang 		return;
463d69607b3SLydia Wang 	num = snd_hda_get_conn_list(codec, mix_nid, NULL);
464d69607b3SLydia Wang 	hp_path = is_node_in_path(path, spec->hp_dac_nid);
465d69607b3SLydia Wang 	front_path = is_node_in_path(path, spec->multiout.dac_nids[0]);
466d69607b3SLydia Wang 
467d69607b3SLydia Wang 	for (i = 0; i < num; i++) {
468d69607b3SLydia Wang 		if (i == aa_mix_idx) {
469d69607b3SLydia Wang 			if (hp_path)
470d69607b3SLydia Wang 				val = enable ? AMP_IN_MUTE(i) :
471d69607b3SLydia Wang 				AMP_IN_UNMUTE(i);
472d69607b3SLydia Wang 			else if (front_path)
473d69607b3SLydia Wang 				val = AMP_IN_UNMUTE(i);
474d69607b3SLydia Wang 			else
475d69607b3SLydia Wang 				val = AMP_IN_MUTE(i);
476d69607b3SLydia Wang 		} else {
477d69607b3SLydia Wang 			if (hp_path)
478d69607b3SLydia Wang 				val = enable ? AMP_IN_UNMUTE(i) :
479d69607b3SLydia Wang 				AMP_IN_MUTE(i);
480d69607b3SLydia Wang 			else if (front_path)
481d69607b3SLydia Wang 				val = AMP_IN_MUTE(i);
482d69607b3SLydia Wang 			else
483d69607b3SLydia Wang 				val = AMP_IN_UNMUTE(i);
484d69607b3SLydia Wang 		}
485d69607b3SLydia Wang 		snd_hda_codec_write(codec, mix_nid, 0,
486d69607b3SLydia Wang 				    AC_VERB_SET_AMP_GAIN_MUTE, val);
487d69607b3SLydia Wang 	}
488d69607b3SLydia Wang }
489d69607b3SLydia Wang 
49009a9ad69STakashi Iwai /* enable/disable the output-route */
49109a9ad69STakashi Iwai static void activate_output_path(struct hda_codec *codec, struct nid_path *path,
49209a9ad69STakashi Iwai 				 bool enable, bool force)
4935d41762aSTakashi Iwai {
494d69607b3SLydia Wang 	int i, val;
495d69607b3SLydia Wang 	struct via_spec *spec = codec->spec;
496d69607b3SLydia Wang 	hda_nid_t aa_mix_nid = spec->aa_mix_nid;
49709a9ad69STakashi Iwai 	for (i = 0; i < path->depth; i++) {
49809a9ad69STakashi Iwai 		hda_nid_t src, dst;
49909a9ad69STakashi Iwai 		int idx = path->idx[i];
50009a9ad69STakashi Iwai 		src = path->path[i];
50109a9ad69STakashi Iwai 		if (i < path->depth - 1)
50209a9ad69STakashi Iwai 			dst = path->path[i + 1];
50309a9ad69STakashi Iwai 		else
50409a9ad69STakashi Iwai 			dst = 0;
50509a9ad69STakashi Iwai 		if (enable && path->multi[i])
50609a9ad69STakashi Iwai 			snd_hda_codec_write(codec, dst, 0,
5075d41762aSTakashi Iwai 					    AC_VERB_SET_CONNECT_SEL, idx);
508b89596a1SLydia Wang 		if (!force
509b89596a1SLydia Wang 		    && get_wcaps_type(get_wcaps(codec, src)) == AC_WID_AUD_OUT
510b89596a1SLydia Wang 		    && get_wcaps_type(get_wcaps(codec, dst)) == AC_WID_AUD_MIX)
511e5e14681SLydia Wang 			continue;
51209a9ad69STakashi Iwai 		if (have_mute(codec, dst, HDA_INPUT)) {
513d69607b3SLydia Wang 			if (dst == aa_mix_nid) {
514d69607b3SLydia Wang 				val = enable ? AMP_IN_UNMUTE(idx) :
51509a9ad69STakashi Iwai 					AMP_IN_MUTE(idx);
51609a9ad69STakashi Iwai 				snd_hda_codec_write(codec, dst, 0,
51709a9ad69STakashi Iwai 					AC_VERB_SET_AMP_GAIN_MUTE, val);
518d69607b3SLydia Wang 			} else {
519d69607b3SLydia Wang 				idx = get_connection_index(codec, dst,
520d69607b3SLydia Wang 							   aa_mix_nid);
521d69607b3SLydia Wang 				if (idx >= 0) {
522d69607b3SLydia Wang 					activate_output_mix(codec, path,
523d69607b3SLydia Wang 							    dst, idx, enable);
524d69607b3SLydia Wang 				}
525d69607b3SLydia Wang 			}
52609a9ad69STakashi Iwai 		}
52709a9ad69STakashi Iwai 		if (!force && (src == path->vol_ctl || src == path->mute_ctl))
52809a9ad69STakashi Iwai 			continue;
52909a9ad69STakashi Iwai 		if (have_mute(codec, src, HDA_OUTPUT)) {
53009a9ad69STakashi Iwai 			int val = enable ? AMP_OUT_UNMUTE : AMP_OUT_MUTE;
53109a9ad69STakashi Iwai 			snd_hda_codec_write(codec, src, 0,
53209a9ad69STakashi Iwai 					    AC_VERB_SET_AMP_GAIN_MUTE, val);
53309a9ad69STakashi Iwai 		}
53409a9ad69STakashi Iwai 	}
5355d41762aSTakashi Iwai }
5365d41762aSTakashi Iwai 
5375d41762aSTakashi Iwai /* set the given pin as output */
5385d41762aSTakashi Iwai static void init_output_pin(struct hda_codec *codec, hda_nid_t pin,
5395d41762aSTakashi Iwai 			    int pin_type)
5405d41762aSTakashi Iwai {
5415d41762aSTakashi Iwai 	if (!pin)
5425d41762aSTakashi Iwai 		return;
5435d41762aSTakashi Iwai 	snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
5445d41762aSTakashi Iwai 			    pin_type);
5455d41762aSTakashi Iwai 	if (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD)
5465d41762aSTakashi Iwai 		snd_hda_codec_write(codec, pin, 0,
547d3a11e60STakashi Iwai 				    AC_VERB_SET_EAPD_BTLENABLE, 0x02);
548c577b8a1SJoseph Chan }
549c577b8a1SJoseph Chan 
55009a9ad69STakashi Iwai static void via_auto_init_output(struct hda_codec *codec,
55109a9ad69STakashi Iwai 				 struct nid_path *path, int pin_type,
552bac4b92cSTakashi Iwai 				 bool with_aa_mix, bool force)
5535d41762aSTakashi Iwai {
5545d41762aSTakashi Iwai 	struct via_spec *spec = codec->spec;
5555d41762aSTakashi Iwai 	unsigned int caps;
556d69607b3SLydia Wang 	hda_nid_t pin;
5575d41762aSTakashi Iwai 
55809a9ad69STakashi Iwai 	if (!path->depth)
5595d41762aSTakashi Iwai 		return;
56009a9ad69STakashi Iwai 	pin = path->path[path->depth - 1];
5615d41762aSTakashi Iwai 
5625d41762aSTakashi Iwai 	init_output_pin(codec, pin, pin_type);
5635d41762aSTakashi Iwai 	caps = query_amp_caps(codec, pin, HDA_OUTPUT);
5645d41762aSTakashi Iwai 	if (caps & AC_AMPCAP_MUTE) {
5655d41762aSTakashi Iwai 		unsigned int val;
5665d41762aSTakashi Iwai 		val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
5675d41762aSTakashi Iwai 		snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
5685d41762aSTakashi Iwai 				    AMP_OUT_MUTE | val);
5695d41762aSTakashi Iwai 	}
5705d41762aSTakashi Iwai 
57109a9ad69STakashi Iwai 	/* initialize the AA-path */
57209a9ad69STakashi Iwai 	if (!spec->aa_mix_nid)
57309a9ad69STakashi Iwai 		return;
574d69607b3SLydia Wang 	activate_output_path(codec, path, true, force);
57509a9ad69STakashi Iwai }
576c577b8a1SJoseph Chan 
577c577b8a1SJoseph Chan static void via_auto_init_multi_out(struct hda_codec *codec)
578c577b8a1SJoseph Chan {
579c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
580c577b8a1SJoseph Chan 	int i;
581c577b8a1SJoseph Chan 
582e3d7a143STakashi Iwai 	for (i = 0; i < spec->autocfg.line_outs + spec->smart51_nums; i++)
583bac4b92cSTakashi Iwai 		/* enable aa-mute only for the front channel */
584bac4b92cSTakashi Iwai 		via_auto_init_output(codec, &spec->out_path[i], PIN_OUT,
585bac4b92cSTakashi Iwai 				     i == 0, true);
586c577b8a1SJoseph Chan }
587c577b8a1SJoseph Chan 
588c577b8a1SJoseph Chan static void via_auto_init_hp_out(struct hda_codec *codec)
589c577b8a1SJoseph Chan {
590c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
591c577b8a1SJoseph Chan 
59209a9ad69STakashi Iwai 	if (!spec->hp_dac_nid) {
593bac4b92cSTakashi Iwai 		via_auto_init_output(codec, &spec->hp_dep_path, PIN_HP,
594bac4b92cSTakashi Iwai 				     true, true);
59509a9ad69STakashi Iwai 		return;
59609a9ad69STakashi Iwai 	}
59709a9ad69STakashi Iwai 	if (spec->hp_independent_mode) {
59809a9ad69STakashi Iwai 		activate_output_path(codec, &spec->hp_dep_path, false, false);
599bac4b92cSTakashi Iwai 		via_auto_init_output(codec, &spec->hp_path, PIN_HP,
600bac4b92cSTakashi Iwai 				     true, true);
60109a9ad69STakashi Iwai 	} else {
60209a9ad69STakashi Iwai 		activate_output_path(codec, &spec->hp_path, false, false);
603bac4b92cSTakashi Iwai 		via_auto_init_output(codec, &spec->hp_dep_path, PIN_HP,
604bac4b92cSTakashi Iwai 				     true, true);
60509a9ad69STakashi Iwai 	}
60625eaba2fSLydia Wang }
607c577b8a1SJoseph Chan 
6084a918ffeSTakashi Iwai static void via_auto_init_speaker_out(struct hda_codec *codec)
6094a918ffeSTakashi Iwai {
6104a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
6114a918ffeSTakashi Iwai 
6124a918ffeSTakashi Iwai 	if (spec->autocfg.speaker_outs)
613bac4b92cSTakashi Iwai 		via_auto_init_output(codec, &spec->speaker_path, PIN_OUT,
614bac4b92cSTakashi Iwai 				     true, true);
6154a918ffeSTakashi Iwai }
6164a918ffeSTakashi Iwai 
617f4a7828bSTakashi Iwai static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin);
6186e969d91STakashi Iwai static void via_hp_automute(struct hda_codec *codec);
61932e0191dSClemens Ladisch 
620c577b8a1SJoseph Chan static void via_auto_init_analog_input(struct hda_codec *codec)
621c577b8a1SJoseph Chan {
622c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
6237b315bb4STakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
624096a8854STakashi Iwai 	hda_nid_t conn[HDA_MAX_CONNECTIONS];
62532e0191dSClemens Ladisch 	unsigned int ctl;
626096a8854STakashi Iwai 	int i, num_conns;
627c577b8a1SJoseph Chan 
628096a8854STakashi Iwai 	/* init ADCs */
629096a8854STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
630096a8854STakashi Iwai 		snd_hda_codec_write(codec, spec->adc_nids[i], 0,
631096a8854STakashi Iwai 				    AC_VERB_SET_AMP_GAIN_MUTE,
632096a8854STakashi Iwai 				    AMP_IN_UNMUTE(0));
633096a8854STakashi Iwai 	}
634096a8854STakashi Iwai 
635096a8854STakashi Iwai 	/* init pins */
6367b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
6377b315bb4STakashi Iwai 		hda_nid_t nid = cfg->inputs[i].pin;
638f4a7828bSTakashi Iwai 		if (spec->smart51_enabled && is_smart51_pins(codec, nid))
63932e0191dSClemens Ladisch 			ctl = PIN_OUT;
64030649676SDavid Henningsson 		else if (cfg->inputs[i].type == AUTO_PIN_MIC)
64132e0191dSClemens Ladisch 			ctl = PIN_VREF50;
64232e0191dSClemens Ladisch 		else
64332e0191dSClemens Ladisch 			ctl = PIN_IN;
644c577b8a1SJoseph Chan 		snd_hda_codec_write(codec, nid, 0,
64532e0191dSClemens Ladisch 				    AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
646c577b8a1SJoseph Chan 	}
647096a8854STakashi Iwai 
648096a8854STakashi Iwai 	/* init input-src */
649096a8854STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
650a86a88eaSTakashi Iwai 		int adc_idx = spec->inputs[spec->cur_mux[i]].adc_idx;
651a86a88eaSTakashi Iwai 		if (spec->mux_nids[adc_idx]) {
652a86a88eaSTakashi Iwai 			int mux_idx = spec->inputs[spec->cur_mux[i]].mux_idx;
653a86a88eaSTakashi Iwai 			snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
654096a8854STakashi Iwai 					    AC_VERB_SET_CONNECT_SEL,
655a86a88eaSTakashi Iwai 					    mux_idx);
656a86a88eaSTakashi Iwai 		}
657a86a88eaSTakashi Iwai 		if (spec->dyn_adc_switch)
658a86a88eaSTakashi Iwai 			break; /* only one input-src */
659096a8854STakashi Iwai 	}
660096a8854STakashi Iwai 
661096a8854STakashi Iwai 	/* init aa-mixer */
662096a8854STakashi Iwai 	if (!spec->aa_mix_nid)
663096a8854STakashi Iwai 		return;
664096a8854STakashi Iwai 	num_conns = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
665096a8854STakashi Iwai 					    ARRAY_SIZE(conn));
666096a8854STakashi Iwai 	for (i = 0; i < num_conns; i++) {
667096a8854STakashi Iwai 		unsigned int caps = get_wcaps(codec, conn[i]);
668096a8854STakashi Iwai 		if (get_wcaps_type(caps) == AC_WID_PIN)
669096a8854STakashi Iwai 			snd_hda_codec_write(codec, spec->aa_mix_nid, 0,
670096a8854STakashi Iwai 					    AC_VERB_SET_AMP_GAIN_MUTE,
671096a8854STakashi Iwai 					    AMP_IN_MUTE(i));
672096a8854STakashi Iwai 	}
673c577b8a1SJoseph Chan }
674f5271101SLydia Wang 
675f5271101SLydia Wang static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
676f5271101SLydia Wang 				unsigned int *affected_parm)
677f5271101SLydia Wang {
678f5271101SLydia Wang 	unsigned parm;
679f5271101SLydia Wang 	unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
680f5271101SLydia Wang 	unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
681f5271101SLydia Wang 		>> AC_DEFCFG_MISC_SHIFT
682f5271101SLydia Wang 		& AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
6831564b287SLydia Wang 	struct via_spec *spec = codec->spec;
68424088a58STakashi Iwai 	unsigned present = 0;
68524088a58STakashi Iwai 
68624088a58STakashi Iwai 	no_presence |= spec->no_pin_power_ctl;
68724088a58STakashi Iwai 	if (!no_presence)
68824088a58STakashi Iwai 		present = snd_hda_jack_detect(codec, nid);
689f4a7828bSTakashi Iwai 	if ((spec->smart51_enabled && is_smart51_pins(codec, nid))
6901564b287SLydia Wang 	    || ((no_presence || present)
6911564b287SLydia Wang 		&& get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
692f5271101SLydia Wang 		*affected_parm = AC_PWRST_D0; /* if it's connected */
693f5271101SLydia Wang 		parm = AC_PWRST_D0;
694f5271101SLydia Wang 	} else
695f5271101SLydia Wang 		parm = AC_PWRST_D3;
696f5271101SLydia Wang 
697f5271101SLydia Wang 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
698f5271101SLydia Wang }
699f5271101SLydia Wang 
70024088a58STakashi Iwai static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
70124088a58STakashi Iwai 				  struct snd_ctl_elem_info *uinfo)
70224088a58STakashi Iwai {
70324088a58STakashi Iwai 	static const char * const texts[] = {
70424088a58STakashi Iwai 		"Disabled", "Enabled"
70524088a58STakashi Iwai 	};
70624088a58STakashi Iwai 
70724088a58STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
70824088a58STakashi Iwai 	uinfo->count = 1;
70924088a58STakashi Iwai 	uinfo->value.enumerated.items = 2;
71024088a58STakashi Iwai 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
71124088a58STakashi Iwai 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
71224088a58STakashi Iwai 	strcpy(uinfo->value.enumerated.name,
71324088a58STakashi Iwai 	       texts[uinfo->value.enumerated.item]);
71424088a58STakashi Iwai 	return 0;
71524088a58STakashi Iwai }
71624088a58STakashi Iwai 
71724088a58STakashi Iwai static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
71824088a58STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
71924088a58STakashi Iwai {
72024088a58STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
72124088a58STakashi Iwai 	struct via_spec *spec = codec->spec;
72224088a58STakashi Iwai 	ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
72324088a58STakashi Iwai 	return 0;
72424088a58STakashi Iwai }
72524088a58STakashi Iwai 
72624088a58STakashi Iwai static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
72724088a58STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
72824088a58STakashi Iwai {
72924088a58STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
73024088a58STakashi Iwai 	struct via_spec *spec = codec->spec;
73124088a58STakashi Iwai 	unsigned int val = !ucontrol->value.enumerated.item[0];
73224088a58STakashi Iwai 
73324088a58STakashi Iwai 	if (val == spec->no_pin_power_ctl)
73424088a58STakashi Iwai 		return 0;
73524088a58STakashi Iwai 	spec->no_pin_power_ctl = val;
73624088a58STakashi Iwai 	set_widgets_power_state(codec);
73724088a58STakashi Iwai 	return 1;
73824088a58STakashi Iwai }
73924088a58STakashi Iwai 
74024088a58STakashi Iwai static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
74124088a58STakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
74224088a58STakashi Iwai 	.name = "Dynamic Power-Control",
74324088a58STakashi Iwai 	.info = via_pin_power_ctl_info,
74424088a58STakashi Iwai 	.get = via_pin_power_ctl_get,
74524088a58STakashi Iwai 	.put = via_pin_power_ctl_put,
74624088a58STakashi Iwai };
74724088a58STakashi Iwai 
74824088a58STakashi Iwai 
7490aa62aefSHarald Welte static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
7500aa62aefSHarald Welte 				   struct snd_ctl_elem_info *uinfo)
7510aa62aefSHarald Welte {
7528df2a312STakashi Iwai 	static const char * const texts[] = { "OFF", "ON" };
7538df2a312STakashi Iwai 
7548df2a312STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
7558df2a312STakashi Iwai 	uinfo->count = 1;
7568df2a312STakashi Iwai 	uinfo->value.enumerated.items = 2;
7578df2a312STakashi Iwai 	if (uinfo->value.enumerated.item >= 2)
7588df2a312STakashi Iwai 		uinfo->value.enumerated.item = 1;
7598df2a312STakashi Iwai 	strcpy(uinfo->value.enumerated.name,
7608df2a312STakashi Iwai 	       texts[uinfo->value.enumerated.item]);
7618df2a312STakashi Iwai 	return 0;
7620aa62aefSHarald Welte }
7630aa62aefSHarald Welte 
7640aa62aefSHarald Welte static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
7650aa62aefSHarald Welte 				  struct snd_ctl_elem_value *ucontrol)
7660aa62aefSHarald Welte {
7670aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
768cdc1784dSLydia Wang 	struct via_spec *spec = codec->spec;
769cdc1784dSLydia Wang 
770ece8d043STakashi Iwai 	ucontrol->value.enumerated.item[0] = spec->hp_independent_mode;
771cdc1784dSLydia Wang 	return 0;
772cdc1784dSLydia Wang }
773cdc1784dSLydia Wang 
7740aa62aefSHarald Welte static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
7750aa62aefSHarald Welte 				  struct snd_ctl_elem_value *ucontrol)
7760aa62aefSHarald Welte {
7770aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7780aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
77925250505STakashi Iwai 	int cur;
7808df2a312STakashi Iwai 
78125250505STakashi Iwai 	/* no independent-hp status change during PCM playback is running */
78225250505STakashi Iwai 	if (spec->num_active_streams)
78325250505STakashi Iwai 		return -EBUSY;
78425250505STakashi Iwai 
78525250505STakashi Iwai 	cur = !!ucontrol->value.enumerated.item[0];
78625250505STakashi Iwai 	if (spec->hp_independent_mode == cur)
78725250505STakashi Iwai 		return 0;
78825250505STakashi Iwai 	spec->hp_independent_mode = cur;
78925250505STakashi Iwai 	if (cur) {
79009a9ad69STakashi Iwai 		activate_output_path(codec, &spec->hp_dep_path, false, false);
79109a9ad69STakashi Iwai 		activate_output_path(codec, &spec->hp_path, true, false);
79225250505STakashi Iwai 		if (spec->hp_indep_shared)
79325250505STakashi Iwai 			activate_output_path(codec, &spec->out_path[HDA_SIDE],
79425250505STakashi Iwai 					     false, false);
79509a9ad69STakashi Iwai 	} else {
79609a9ad69STakashi Iwai 		activate_output_path(codec, &spec->hp_path, false, false);
79709a9ad69STakashi Iwai 		activate_output_path(codec, &spec->hp_dep_path, true, false);
79825250505STakashi Iwai 		if (spec->hp_indep_shared)
79925250505STakashi Iwai 			activate_output_path(codec, &spec->out_path[HDA_SIDE],
80025250505STakashi Iwai 					     true, false);
8018df2a312STakashi Iwai 	}
8020aa62aefSHarald Welte 
803ce0e5a9eSLydia Wang 	/* update jack power state */
8043e95b9abSLydia Wang 	set_widgets_power_state(codec);
8056e969d91STakashi Iwai 	via_hp_automute(codec);
80625250505STakashi Iwai 	return 1;
8070aa62aefSHarald Welte }
8080aa62aefSHarald Welte 
809ece8d043STakashi Iwai static const struct snd_kcontrol_new via_hp_mixer = {
8100aa62aefSHarald Welte 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
8110aa62aefSHarald Welte 	.name = "Independent HP",
8120aa62aefSHarald Welte 	.info = via_independent_hp_info,
8130aa62aefSHarald Welte 	.get = via_independent_hp_get,
8140aa62aefSHarald Welte 	.put = via_independent_hp_put,
8150aa62aefSHarald Welte };
8160aa62aefSHarald Welte 
8173d83e577STakashi Iwai static int via_hp_build(struct hda_codec *codec)
8185b0cb1d8SJaroslav Kysela {
8193d83e577STakashi Iwai 	struct via_spec *spec = codec->spec;
8205b0cb1d8SJaroslav Kysela 	struct snd_kcontrol_new *knew;
8215b0cb1d8SJaroslav Kysela 	hda_nid_t nid;
8225b0cb1d8SJaroslav Kysela 
8235b0cb1d8SJaroslav Kysela 	nid = spec->autocfg.hp_pins[0];
824ece8d043STakashi Iwai 	knew = via_clone_control(spec, &via_hp_mixer);
8253d83e577STakashi Iwai 	if (knew == NULL)
8263d83e577STakashi Iwai 		return -ENOMEM;
8273d83e577STakashi Iwai 
8285b0cb1d8SJaroslav Kysela 	knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
8295b0cb1d8SJaroslav Kysela 
8305b0cb1d8SJaroslav Kysela 	return 0;
8315b0cb1d8SJaroslav Kysela }
8325b0cb1d8SJaroslav Kysela 
8331564b287SLydia Wang static void notify_aa_path_ctls(struct hda_codec *codec)
8341564b287SLydia Wang {
835e3d7a143STakashi Iwai 	struct via_spec *spec = codec->spec;
8361564b287SLydia Wang 	int i;
8371564b287SLydia Wang 
838e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
839e3d7a143STakashi Iwai 		struct snd_kcontrol *ctl;
840e3d7a143STakashi Iwai 		struct snd_ctl_elem_id id;
8411564b287SLydia Wang 		memset(&id, 0, sizeof(id));
8421564b287SLydia Wang 		id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
843e3d7a143STakashi Iwai 		sprintf(id.name, "%s Playback Volume", spec->smart51_labels[i]);
844525566cbSLydia Wang 		ctl = snd_hda_find_mixer_ctl(codec, id.name);
845525566cbSLydia Wang 		if (ctl)
846525566cbSLydia Wang 			snd_ctl_notify(codec->bus->card,
847525566cbSLydia Wang 					SNDRV_CTL_EVENT_MASK_VALUE,
848525566cbSLydia Wang 					&ctl->id);
8491564b287SLydia Wang 	}
8501564b287SLydia Wang }
8511564b287SLydia Wang 
8521564b287SLydia Wang static void mute_aa_path(struct hda_codec *codec, int mute)
8531564b287SLydia Wang {
8541564b287SLydia Wang 	struct via_spec *spec = codec->spec;
8551564b287SLydia Wang 	int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
856e3d7a143STakashi Iwai 	int i;
857e3d7a143STakashi Iwai 
858e3d7a143STakashi Iwai 	/* check AA path's mute status */
859e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
860e3d7a143STakashi Iwai 		if (spec->smart51_idxs[i] < 0)
861e3d7a143STakashi Iwai 			continue;
862e3d7a143STakashi Iwai 		snd_hda_codec_amp_stereo(codec, spec->aa_mix_nid,
863e3d7a143STakashi Iwai 					 HDA_INPUT, spec->smart51_idxs[i],
8641564b287SLydia Wang 					 HDA_AMP_MUTE, val);
8651564b287SLydia Wang 	}
8661564b287SLydia Wang }
867f4a7828bSTakashi Iwai 
868e3d7a143STakashi Iwai static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
869e3d7a143STakashi Iwai {
870e3d7a143STakashi Iwai 	struct via_spec *spec = codec->spec;
871e3d7a143STakashi Iwai 	int i;
872e3d7a143STakashi Iwai 
873e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++)
874e3d7a143STakashi Iwai 		if (spec->smart51_pins[i] == pin)
875e3d7a143STakashi Iwai 			return true;
876e3d7a143STakashi Iwai 	return false;
877e3d7a143STakashi Iwai }
878e3d7a143STakashi Iwai 
8791564b287SLydia Wang static int via_smart51_get(struct snd_kcontrol *kcontrol,
8801564b287SLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
8811564b287SLydia Wang {
8821564b287SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
8831564b287SLydia Wang 	struct via_spec *spec = codec->spec;
8841564b287SLydia Wang 
885f2b1c9f0STakashi Iwai 	*ucontrol->value.integer.value = spec->smart51_enabled;
8861564b287SLydia Wang 	return 0;
8871564b287SLydia Wang }
8881564b287SLydia Wang 
8891564b287SLydia Wang static int via_smart51_put(struct snd_kcontrol *kcontrol,
8901564b287SLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
8911564b287SLydia Wang {
8921564b287SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
8931564b287SLydia Wang 	struct via_spec *spec = codec->spec;
8941564b287SLydia Wang 	int out_in = *ucontrol->value.integer.value
8951564b287SLydia Wang 		? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
8961564b287SLydia Wang 	int i;
8971564b287SLydia Wang 
898e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
899e3d7a143STakashi Iwai 		hda_nid_t nid = spec->smart51_pins[i];
9007b315bb4STakashi Iwai 		unsigned int parm;
9017b315bb4STakashi Iwai 
9027b315bb4STakashi Iwai 		parm = snd_hda_codec_read(codec, nid, 0,
9031564b287SLydia Wang 					  AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
9041564b287SLydia Wang 		parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
9051564b287SLydia Wang 		parm |= out_in;
9061564b287SLydia Wang 		snd_hda_codec_write(codec, nid, 0,
9071564b287SLydia Wang 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
9081564b287SLydia Wang 				    parm);
9091564b287SLydia Wang 		if (out_in == AC_PINCTL_OUT_EN) {
9101564b287SLydia Wang 			mute_aa_path(codec, 1);
9111564b287SLydia Wang 			notify_aa_path_ctls(codec);
9121564b287SLydia Wang 		}
9131564b287SLydia Wang 	}
9141564b287SLydia Wang 	spec->smart51_enabled = *ucontrol->value.integer.value;
9153e95b9abSLydia Wang 	set_widgets_power_state(codec);
9161564b287SLydia Wang 	return 1;
9171564b287SLydia Wang }
9181564b287SLydia Wang 
9195f4b36d6STakashi Iwai static const struct snd_kcontrol_new via_smart51_mixer = {
9201564b287SLydia Wang 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
9211564b287SLydia Wang 	.name = "Smart 5.1",
9221564b287SLydia Wang 	.count = 1,
923f2b1c9f0STakashi Iwai 	.info = snd_ctl_boolean_mono_info,
9241564b287SLydia Wang 	.get = via_smart51_get,
9251564b287SLydia Wang 	.put = via_smart51_put,
9261564b287SLydia Wang };
9271564b287SLydia Wang 
928f4a7828bSTakashi Iwai static int via_smart51_build(struct hda_codec *codec)
9295b0cb1d8SJaroslav Kysela {
930f4a7828bSTakashi Iwai 	struct via_spec *spec = codec->spec;
9315b0cb1d8SJaroslav Kysela 
932e3d7a143STakashi Iwai 	if (!spec->smart51_nums)
933cb34c207SLydia Wang 		return 0;
934e3d7a143STakashi Iwai 	if (!via_clone_control(spec, &via_smart51_mixer))
9355b0cb1d8SJaroslav Kysela 		return -ENOMEM;
9365b0cb1d8SJaroslav Kysela 	return 0;
9375b0cb1d8SJaroslav Kysela }
9385b0cb1d8SJaroslav Kysela 
939f5271101SLydia Wang /* check AA path's mute status */
940ada509ecSTakashi Iwai static bool is_aa_path_mute(struct hda_codec *codec)
941ada509ecSTakashi Iwai {
942ada509ecSTakashi Iwai 	struct via_spec *spec = codec->spec;
943ada509ecSTakashi Iwai 	const struct hda_amp_list *p;
944ada509ecSTakashi Iwai 	int i, ch, v;
945ada509ecSTakashi Iwai 
946ada509ecSTakashi Iwai 	for (i = 0; i < spec->num_loopbacks; i++) {
947ada509ecSTakashi Iwai 		p = &spec->loopback_list[i];
948ada509ecSTakashi Iwai 		for (ch = 0; ch < 2; ch++) {
949ada509ecSTakashi Iwai 			v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
950ada509ecSTakashi Iwai 						   p->idx);
951ada509ecSTakashi Iwai 			if (!(v & HDA_AMP_MUTE) && v > 0)
952ada509ecSTakashi Iwai 				return false;
953f5271101SLydia Wang 		}
954f5271101SLydia Wang 	}
955ada509ecSTakashi Iwai 	return true;
956f5271101SLydia Wang }
957f5271101SLydia Wang 
958f5271101SLydia Wang /* enter/exit analog low-current mode */
959ada509ecSTakashi Iwai static void analog_low_current_mode(struct hda_codec *codec)
960f5271101SLydia Wang {
961f5271101SLydia Wang 	struct via_spec *spec = codec->spec;
962ada509ecSTakashi Iwai 	bool enable;
963ada509ecSTakashi Iwai 	unsigned int verb, parm;
964f5271101SLydia Wang 
965ada509ecSTakashi Iwai 	enable = is_aa_path_mute(codec) && (spec->num_active_streams > 0);
966f5271101SLydia Wang 
967f5271101SLydia Wang 	/* decide low current mode's verb & parameter */
968f5271101SLydia Wang 	switch (spec->codec_type) {
969f5271101SLydia Wang 	case VT1708B_8CH:
970f5271101SLydia Wang 	case VT1708B_4CH:
971f5271101SLydia Wang 		verb = 0xf70;
972f5271101SLydia Wang 		parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
973f5271101SLydia Wang 		break;
974f5271101SLydia Wang 	case VT1708S:
975eb7188caSLydia Wang 	case VT1718S:
976f3db423dSLydia Wang 	case VT1716S:
977f5271101SLydia Wang 		verb = 0xf73;
978f5271101SLydia Wang 		parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
979f5271101SLydia Wang 		break;
980f5271101SLydia Wang 	case VT1702:
981f5271101SLydia Wang 		verb = 0xf73;
982f5271101SLydia Wang 		parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
983f5271101SLydia Wang 		break;
98425eaba2fSLydia Wang 	case VT2002P:
985ab6734e7SLydia Wang 	case VT1812:
98611890956SLydia Wang 	case VT1802:
98725eaba2fSLydia Wang 		verb = 0xf93;
98825eaba2fSLydia Wang 		parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
98925eaba2fSLydia Wang 		break;
990f5271101SLydia Wang 	default:
991f5271101SLydia Wang 		return;		/* other codecs are not supported */
992f5271101SLydia Wang 	}
993f5271101SLydia Wang 	/* send verb */
994f5271101SLydia Wang 	snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
995f5271101SLydia Wang }
996f5271101SLydia Wang 
997c577b8a1SJoseph Chan /*
998c577b8a1SJoseph Chan  * generic initialization of ADC, input mixers and output mixers
999c577b8a1SJoseph Chan  */
1000096a8854STakashi Iwai static const struct hda_verb vt1708_init_verbs[] = {
1001aa266fccSLydia Wang 	/* power down jack detect function */
1002aa266fccSLydia Wang 	{0x1, 0xf81, 0x1},
1003f7278fd0SJosepch Chan 	{ }
1004c577b8a1SJoseph Chan };
1005c577b8a1SJoseph Chan 
1006ada509ecSTakashi Iwai static void set_stream_active(struct hda_codec *codec, bool active)
10077eb56e84STakashi Iwai {
1008ada509ecSTakashi Iwai 	struct via_spec *spec = codec->spec;
1009ada509ecSTakashi Iwai 
1010ada509ecSTakashi Iwai 	if (active)
1011ada509ecSTakashi Iwai 		spec->num_active_streams++;
1012ada509ecSTakashi Iwai 	else
1013ada509ecSTakashi Iwai 		spec->num_active_streams--;
1014ada509ecSTakashi Iwai 	analog_low_current_mode(codec);
10157eb56e84STakashi Iwai }
10167eb56e84STakashi Iwai 
1017ece8d043STakashi Iwai static int via_playback_multi_pcm_open(struct hda_pcm_stream *hinfo,
1018c577b8a1SJoseph Chan 				 struct hda_codec *codec,
1019c577b8a1SJoseph Chan 				 struct snd_pcm_substream *substream)
1020c577b8a1SJoseph Chan {
1021c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
102225250505STakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
1023ada509ecSTakashi Iwai 	int err;
1024ece8d043STakashi Iwai 
102525250505STakashi Iwai 	spec->multiout.hp_nid = 0;
102625250505STakashi Iwai 	spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
102725250505STakashi Iwai 	if (!spec->hp_independent_mode) {
102825250505STakashi Iwai 		if (!spec->hp_indep_shared)
1029ece8d043STakashi Iwai 			spec->multiout.hp_nid = spec->hp_dac_nid;
103025250505STakashi Iwai 	} else {
103125250505STakashi Iwai 		if (spec->hp_indep_shared)
103225250505STakashi Iwai 			spec->multiout.num_dacs = cfg->line_outs - 1;
103325250505STakashi Iwai 	}
103425250505STakashi Iwai 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
1035ada509ecSTakashi Iwai 	set_stream_active(codec, true);
1036ada509ecSTakashi Iwai 	err = snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
10379a08160bSTakashi Iwai 					    hinfo);
1038ada509ecSTakashi Iwai 	if (err < 0) {
1039ada509ecSTakashi Iwai 		spec->multiout.hp_nid = 0;
1040ada509ecSTakashi Iwai 		set_stream_active(codec, false);
1041ada509ecSTakashi Iwai 		return err;
1042ada509ecSTakashi Iwai 	}
1043ada509ecSTakashi Iwai 	return 0;
1044c577b8a1SJoseph Chan }
1045c577b8a1SJoseph Chan 
1046ece8d043STakashi Iwai static int via_playback_multi_pcm_close(struct hda_pcm_stream *hinfo,
10479af74210STakashi Iwai 				  struct hda_codec *codec,
10489af74210STakashi Iwai 				  struct snd_pcm_substream *substream)
10499af74210STakashi Iwai {
1050ece8d043STakashi Iwai 	struct via_spec *spec = codec->spec;
1051ece8d043STakashi Iwai 
1052ece8d043STakashi Iwai 	spec->multiout.hp_nid = 0;
1053ada509ecSTakashi Iwai 	set_stream_active(codec, false);
10549af74210STakashi Iwai 	return 0;
10559af74210STakashi Iwai }
10569af74210STakashi Iwai 
10577eb56e84STakashi Iwai static int via_playback_hp_pcm_open(struct hda_pcm_stream *hinfo,
10587eb56e84STakashi Iwai 				    struct hda_codec *codec,
10597eb56e84STakashi Iwai 				    struct snd_pcm_substream *substream)
10607eb56e84STakashi Iwai {
10617eb56e84STakashi Iwai 	struct via_spec *spec = codec->spec;
10627eb56e84STakashi Iwai 
1063ece8d043STakashi Iwai 	if (snd_BUG_ON(!spec->hp_dac_nid))
10647eb56e84STakashi Iwai 		return -EINVAL;
1065ece8d043STakashi Iwai 	if (!spec->hp_independent_mode || spec->multiout.hp_nid)
1066ece8d043STakashi Iwai 		return -EBUSY;
1067ada509ecSTakashi Iwai 	set_stream_active(codec, true);
1068ece8d043STakashi Iwai 	return 0;
1069ece8d043STakashi Iwai }
1070ece8d043STakashi Iwai 
1071ece8d043STakashi Iwai static int via_playback_hp_pcm_close(struct hda_pcm_stream *hinfo,
1072ece8d043STakashi Iwai 				     struct hda_codec *codec,
1073ece8d043STakashi Iwai 				     struct snd_pcm_substream *substream)
1074ece8d043STakashi Iwai {
1075ada509ecSTakashi Iwai 	set_stream_active(codec, false);
10767eb56e84STakashi Iwai 	return 0;
10777eb56e84STakashi Iwai }
10787eb56e84STakashi Iwai 
10797eb56e84STakashi Iwai static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
10807eb56e84STakashi Iwai 					  struct hda_codec *codec,
10810aa62aefSHarald Welte 					  unsigned int stream_tag,
10820aa62aefSHarald Welte 					  unsigned int format,
10830aa62aefSHarald Welte 					  struct snd_pcm_substream *substream)
10840aa62aefSHarald Welte {
10850aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
10860aa62aefSHarald Welte 
1087ece8d043STakashi Iwai 	snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
1088ece8d043STakashi Iwai 					 format, substream);
10897eb56e84STakashi Iwai 	vt1708_start_hp_work(spec);
10907eb56e84STakashi Iwai 	return 0;
10910aa62aefSHarald Welte }
10920aa62aefSHarald Welte 
10937eb56e84STakashi Iwai static int via_playback_hp_pcm_prepare(struct hda_pcm_stream *hinfo,
10940aa62aefSHarald Welte 				       struct hda_codec *codec,
10950aa62aefSHarald Welte 				       unsigned int stream_tag,
10960aa62aefSHarald Welte 				       unsigned int format,
10970aa62aefSHarald Welte 				       struct snd_pcm_substream *substream)
10980aa62aefSHarald Welte {
10990aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
11000aa62aefSHarald Welte 
1101ece8d043STakashi Iwai 	snd_hda_codec_setup_stream(codec, spec->hp_dac_nid,
1102ece8d043STakashi Iwai 				   stream_tag, 0, format);
11031f2e99feSLydia Wang 	vt1708_start_hp_work(spec);
11040aa62aefSHarald Welte 	return 0;
11050aa62aefSHarald Welte }
11060aa62aefSHarald Welte 
11070aa62aefSHarald Welte static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
11080aa62aefSHarald Welte 				    struct hda_codec *codec,
11090aa62aefSHarald Welte 				    struct snd_pcm_substream *substream)
11100aa62aefSHarald Welte {
11110aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
11120aa62aefSHarald Welte 
1113ece8d043STakashi Iwai 	snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
11147eb56e84STakashi Iwai 	vt1708_stop_hp_work(spec);
11157eb56e84STakashi Iwai 	return 0;
11160aa62aefSHarald Welte }
11177eb56e84STakashi Iwai 
11187eb56e84STakashi Iwai static int via_playback_hp_pcm_cleanup(struct hda_pcm_stream *hinfo,
11197eb56e84STakashi Iwai 				       struct hda_codec *codec,
11207eb56e84STakashi Iwai 				       struct snd_pcm_substream *substream)
11217eb56e84STakashi Iwai {
11227eb56e84STakashi Iwai 	struct via_spec *spec = codec->spec;
11237eb56e84STakashi Iwai 
1124ece8d043STakashi Iwai 	snd_hda_codec_setup_stream(codec, spec->hp_dac_nid, 0, 0, 0);
11251f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
11260aa62aefSHarald Welte 	return 0;
11270aa62aefSHarald Welte }
11280aa62aefSHarald Welte 
1129c577b8a1SJoseph Chan /*
1130c577b8a1SJoseph Chan  * Digital out
1131c577b8a1SJoseph Chan  */
1132c577b8a1SJoseph Chan static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1133c577b8a1SJoseph Chan 				     struct hda_codec *codec,
1134c577b8a1SJoseph Chan 				     struct snd_pcm_substream *substream)
1135c577b8a1SJoseph Chan {
1136c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1137c577b8a1SJoseph Chan 	return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1138c577b8a1SJoseph Chan }
1139c577b8a1SJoseph Chan 
1140c577b8a1SJoseph Chan static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1141c577b8a1SJoseph Chan 				      struct hda_codec *codec,
1142c577b8a1SJoseph Chan 				      struct snd_pcm_substream *substream)
1143c577b8a1SJoseph Chan {
1144c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1145c577b8a1SJoseph Chan 	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1146c577b8a1SJoseph Chan }
1147c577b8a1SJoseph Chan 
11485691ec7fSHarald Welte static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
114998aa34c0SHarald Welte 					struct hda_codec *codec,
115098aa34c0SHarald Welte 					unsigned int stream_tag,
115198aa34c0SHarald Welte 					unsigned int format,
115298aa34c0SHarald Welte 					struct snd_pcm_substream *substream)
115398aa34c0SHarald Welte {
115498aa34c0SHarald Welte 	struct via_spec *spec = codec->spec;
11559da29271STakashi Iwai 	return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
11569da29271STakashi Iwai 					     stream_tag, format, substream);
11579da29271STakashi Iwai }
11585691ec7fSHarald Welte 
11599da29271STakashi Iwai static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
11609da29271STakashi Iwai 					struct hda_codec *codec,
11619da29271STakashi Iwai 					struct snd_pcm_substream *substream)
11629da29271STakashi Iwai {
11639da29271STakashi Iwai 	struct via_spec *spec = codec->spec;
11649da29271STakashi Iwai 	snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
116598aa34c0SHarald Welte 	return 0;
116698aa34c0SHarald Welte }
116798aa34c0SHarald Welte 
1168c577b8a1SJoseph Chan /*
1169c577b8a1SJoseph Chan  * Analog capture
1170c577b8a1SJoseph Chan  */
1171c577b8a1SJoseph Chan static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1172c577b8a1SJoseph Chan 				   struct hda_codec *codec,
1173c577b8a1SJoseph Chan 				   unsigned int stream_tag,
1174c577b8a1SJoseph Chan 				   unsigned int format,
1175c577b8a1SJoseph Chan 				   struct snd_pcm_substream *substream)
1176c577b8a1SJoseph Chan {
1177c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1178c577b8a1SJoseph Chan 
1179c577b8a1SJoseph Chan 	snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1180c577b8a1SJoseph Chan 				   stream_tag, 0, format);
1181c577b8a1SJoseph Chan 	return 0;
1182c577b8a1SJoseph Chan }
1183c577b8a1SJoseph Chan 
1184c577b8a1SJoseph Chan static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1185c577b8a1SJoseph Chan 				   struct hda_codec *codec,
1186c577b8a1SJoseph Chan 				   struct snd_pcm_substream *substream)
1187c577b8a1SJoseph Chan {
1188c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1189888afa15STakashi Iwai 	snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
1190c577b8a1SJoseph Chan 	return 0;
1191c577b8a1SJoseph Chan }
1192c577b8a1SJoseph Chan 
1193a86a88eaSTakashi Iwai /* analog capture with dynamic ADC switching */
1194a86a88eaSTakashi Iwai static int via_dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1195a86a88eaSTakashi Iwai 					   struct hda_codec *codec,
1196a86a88eaSTakashi Iwai 					   unsigned int stream_tag,
1197a86a88eaSTakashi Iwai 					   unsigned int format,
1198a86a88eaSTakashi Iwai 					   struct snd_pcm_substream *substream)
1199a86a88eaSTakashi Iwai {
1200a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1201a86a88eaSTakashi Iwai 	int adc_idx = spec->inputs[spec->cur_mux[0]].adc_idx;
1202a86a88eaSTakashi Iwai 
1203a86a88eaSTakashi Iwai 	spec->cur_adc = spec->adc_nids[adc_idx];
1204a86a88eaSTakashi Iwai 	spec->cur_adc_stream_tag = stream_tag;
1205a86a88eaSTakashi Iwai 	spec->cur_adc_format = format;
1206a86a88eaSTakashi Iwai 	snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
1207a86a88eaSTakashi Iwai 	return 0;
1208a86a88eaSTakashi Iwai }
1209a86a88eaSTakashi Iwai 
1210a86a88eaSTakashi Iwai static int via_dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1211a86a88eaSTakashi Iwai 					   struct hda_codec *codec,
1212a86a88eaSTakashi Iwai 					   struct snd_pcm_substream *substream)
1213a86a88eaSTakashi Iwai {
1214a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1215a86a88eaSTakashi Iwai 
1216a86a88eaSTakashi Iwai 	snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1217a86a88eaSTakashi Iwai 	spec->cur_adc = 0;
1218a86a88eaSTakashi Iwai 	return 0;
1219a86a88eaSTakashi Iwai }
1220a86a88eaSTakashi Iwai 
1221a86a88eaSTakashi Iwai /* re-setup the stream if running; called from input-src put */
1222a86a88eaSTakashi Iwai static bool via_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
1223a86a88eaSTakashi Iwai {
1224a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1225a86a88eaSTakashi Iwai 	int adc_idx = spec->inputs[cur].adc_idx;
1226a86a88eaSTakashi Iwai 	hda_nid_t adc = spec->adc_nids[adc_idx];
1227a86a88eaSTakashi Iwai 
1228a86a88eaSTakashi Iwai 	if (spec->cur_adc && spec->cur_adc != adc) {
1229a86a88eaSTakashi Iwai 		/* stream is running, let's swap the current ADC */
1230a86a88eaSTakashi Iwai 		__snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1231a86a88eaSTakashi Iwai 		spec->cur_adc = adc;
1232a86a88eaSTakashi Iwai 		snd_hda_codec_setup_stream(codec, adc,
1233a86a88eaSTakashi Iwai 					   spec->cur_adc_stream_tag, 0,
1234a86a88eaSTakashi Iwai 					   spec->cur_adc_format);
1235a86a88eaSTakashi Iwai 		return true;
1236a86a88eaSTakashi Iwai 	}
1237a86a88eaSTakashi Iwai 	return false;
1238a86a88eaSTakashi Iwai }
1239a86a88eaSTakashi Iwai 
12409af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_analog_playback = {
12417eb56e84STakashi Iwai 	.substreams = 1,
1242c577b8a1SJoseph Chan 	.channels_min = 2,
1243c577b8a1SJoseph Chan 	.channels_max = 8,
12449af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1245c577b8a1SJoseph Chan 	.ops = {
1246ece8d043STakashi Iwai 		.open = via_playback_multi_pcm_open,
1247ece8d043STakashi Iwai 		.close = via_playback_multi_pcm_close,
12480aa62aefSHarald Welte 		.prepare = via_playback_multi_pcm_prepare,
12490aa62aefSHarald Welte 		.cleanup = via_playback_multi_pcm_cleanup
1250c577b8a1SJoseph Chan 	},
1251c577b8a1SJoseph Chan };
1252c577b8a1SJoseph Chan 
12537eb56e84STakashi Iwai static const struct hda_pcm_stream via_pcm_hp_playback = {
12547eb56e84STakashi Iwai 	.substreams = 1,
12557eb56e84STakashi Iwai 	.channels_min = 2,
12567eb56e84STakashi Iwai 	.channels_max = 2,
12577eb56e84STakashi Iwai 	/* NID is set in via_build_pcms */
12587eb56e84STakashi Iwai 	.ops = {
12597eb56e84STakashi Iwai 		.open = via_playback_hp_pcm_open,
1260ece8d043STakashi Iwai 		.close = via_playback_hp_pcm_close,
12617eb56e84STakashi Iwai 		.prepare = via_playback_hp_pcm_prepare,
12627eb56e84STakashi Iwai 		.cleanup = via_playback_hp_pcm_cleanup
12637eb56e84STakashi Iwai 	},
12647eb56e84STakashi Iwai };
12657eb56e84STakashi Iwai 
126690dd48a1STakashi Iwai static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
12677eb56e84STakashi Iwai 	.substreams = 1,
1268bc9b5623STakashi Iwai 	.channels_min = 2,
1269bc9b5623STakashi Iwai 	.channels_max = 8,
12709af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1271bc9b5623STakashi Iwai 	/* We got noisy outputs on the right channel on VT1708 when
1272bc9b5623STakashi Iwai 	 * 24bit samples are used.  Until any workaround is found,
1273bc9b5623STakashi Iwai 	 * disable the 24bit format, so far.
1274bc9b5623STakashi Iwai 	 */
1275bc9b5623STakashi Iwai 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
1276bc9b5623STakashi Iwai 	.ops = {
1277ece8d043STakashi Iwai 		.open = via_playback_multi_pcm_open,
1278ece8d043STakashi Iwai 		.close = via_playback_multi_pcm_close,
1279c873cc25SLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
1280c873cc25SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup
1281bc9b5623STakashi Iwai 	},
1282bc9b5623STakashi Iwai };
1283bc9b5623STakashi Iwai 
12849af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_analog_capture = {
12857eb56e84STakashi Iwai 	.substreams = 1, /* will be changed in via_build_pcms() */
1286c577b8a1SJoseph Chan 	.channels_min = 2,
1287c577b8a1SJoseph Chan 	.channels_max = 2,
12889af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1289c577b8a1SJoseph Chan 	.ops = {
1290c577b8a1SJoseph Chan 		.prepare = via_capture_pcm_prepare,
1291c577b8a1SJoseph Chan 		.cleanup = via_capture_pcm_cleanup
1292c577b8a1SJoseph Chan 	},
1293c577b8a1SJoseph Chan };
1294c577b8a1SJoseph Chan 
1295a86a88eaSTakashi Iwai static const struct hda_pcm_stream via_pcm_dyn_adc_analog_capture = {
1296a86a88eaSTakashi Iwai 	.substreams = 1,
1297a86a88eaSTakashi Iwai 	.channels_min = 2,
1298a86a88eaSTakashi Iwai 	.channels_max = 2,
1299a86a88eaSTakashi Iwai 	/* NID is set in via_build_pcms */
1300a86a88eaSTakashi Iwai 	.ops = {
1301a86a88eaSTakashi Iwai 		.prepare = via_dyn_adc_capture_pcm_prepare,
1302a86a88eaSTakashi Iwai 		.cleanup = via_dyn_adc_capture_pcm_cleanup,
1303a86a88eaSTakashi Iwai 	},
1304a86a88eaSTakashi Iwai };
1305a86a88eaSTakashi Iwai 
13069af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_digital_playback = {
1307c577b8a1SJoseph Chan 	.substreams = 1,
1308c577b8a1SJoseph Chan 	.channels_min = 2,
1309c577b8a1SJoseph Chan 	.channels_max = 2,
1310c577b8a1SJoseph Chan 	/* NID is set in via_build_pcms */
1311c577b8a1SJoseph Chan 	.ops = {
1312c577b8a1SJoseph Chan 		.open = via_dig_playback_pcm_open,
13136b97eb45STakashi Iwai 		.close = via_dig_playback_pcm_close,
13149da29271STakashi Iwai 		.prepare = via_dig_playback_pcm_prepare,
13159da29271STakashi Iwai 		.cleanup = via_dig_playback_pcm_cleanup
1316c577b8a1SJoseph Chan 	},
1317c577b8a1SJoseph Chan };
1318c577b8a1SJoseph Chan 
13199af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_digital_capture = {
1320c577b8a1SJoseph Chan 	.substreams = 1,
1321c577b8a1SJoseph Chan 	.channels_min = 2,
1322c577b8a1SJoseph Chan 	.channels_max = 2,
1323c577b8a1SJoseph Chan };
1324c577b8a1SJoseph Chan 
1325370bafbdSTakashi Iwai /*
1326370bafbdSTakashi Iwai  * slave controls for virtual master
1327370bafbdSTakashi Iwai  */
1328370bafbdSTakashi Iwai static const char * const via_slave_vols[] = {
1329370bafbdSTakashi Iwai 	"Front Playback Volume",
1330370bafbdSTakashi Iwai 	"Surround Playback Volume",
1331370bafbdSTakashi Iwai 	"Center Playback Volume",
1332370bafbdSTakashi Iwai 	"LFE Playback Volume",
1333370bafbdSTakashi Iwai 	"Side Playback Volume",
1334370bafbdSTakashi Iwai 	"Headphone Playback Volume",
1335370bafbdSTakashi Iwai 	"Speaker Playback Volume",
1336370bafbdSTakashi Iwai 	NULL,
1337370bafbdSTakashi Iwai };
1338370bafbdSTakashi Iwai 
1339370bafbdSTakashi Iwai static const char * const via_slave_sws[] = {
1340370bafbdSTakashi Iwai 	"Front Playback Switch",
1341370bafbdSTakashi Iwai 	"Surround Playback Switch",
1342370bafbdSTakashi Iwai 	"Center Playback Switch",
1343370bafbdSTakashi Iwai 	"LFE Playback Switch",
1344370bafbdSTakashi Iwai 	"Side Playback Switch",
1345370bafbdSTakashi Iwai 	"Headphone Playback Switch",
1346370bafbdSTakashi Iwai 	"Speaker Playback Switch",
1347370bafbdSTakashi Iwai 	NULL,
1348370bafbdSTakashi Iwai };
1349370bafbdSTakashi Iwai 
1350c577b8a1SJoseph Chan static int via_build_controls(struct hda_codec *codec)
1351c577b8a1SJoseph Chan {
1352c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
13535b0cb1d8SJaroslav Kysela 	struct snd_kcontrol *kctl;
13545b0cb1d8SJaroslav Kysela 	int err, i;
1355c577b8a1SJoseph Chan 
135624088a58STakashi Iwai 	if (spec->set_widgets_power_state)
135724088a58STakashi Iwai 		if (!via_clone_control(spec, &via_pin_power_ctl_enum))
135824088a58STakashi Iwai 			return -ENOMEM;
135924088a58STakashi Iwai 
1360c577b8a1SJoseph Chan 	for (i = 0; i < spec->num_mixers; i++) {
1361c577b8a1SJoseph Chan 		err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1362c577b8a1SJoseph Chan 		if (err < 0)
1363c577b8a1SJoseph Chan 			return err;
1364c577b8a1SJoseph Chan 	}
1365c577b8a1SJoseph Chan 
1366c577b8a1SJoseph Chan 	if (spec->multiout.dig_out_nid) {
1367c577b8a1SJoseph Chan 		err = snd_hda_create_spdif_out_ctls(codec,
136874b654c9SStephen Warren 						    spec->multiout.dig_out_nid,
1369c577b8a1SJoseph Chan 						    spec->multiout.dig_out_nid);
1370c577b8a1SJoseph Chan 		if (err < 0)
1371c577b8a1SJoseph Chan 			return err;
13729a08160bSTakashi Iwai 		err = snd_hda_create_spdif_share_sw(codec,
13739a08160bSTakashi Iwai 						    &spec->multiout);
13749a08160bSTakashi Iwai 		if (err < 0)
13759a08160bSTakashi Iwai 			return err;
13769a08160bSTakashi Iwai 		spec->multiout.share_spdif = 1;
1377c577b8a1SJoseph Chan 	}
1378c577b8a1SJoseph Chan 	if (spec->dig_in_nid) {
1379c577b8a1SJoseph Chan 		err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1380c577b8a1SJoseph Chan 		if (err < 0)
1381c577b8a1SJoseph Chan 			return err;
1382c577b8a1SJoseph Chan 	}
138317314379SLydia Wang 
1384370bafbdSTakashi Iwai 	/* if we have no master control, let's create it */
1385370bafbdSTakashi Iwai 	if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1386370bafbdSTakashi Iwai 		unsigned int vmaster_tlv[4];
1387370bafbdSTakashi Iwai 		snd_hda_set_vmaster_tlv(codec, spec->multiout.dac_nids[0],
1388370bafbdSTakashi Iwai 					HDA_OUTPUT, vmaster_tlv);
1389370bafbdSTakashi Iwai 		err = snd_hda_add_vmaster(codec, "Master Playback Volume",
1390370bafbdSTakashi Iwai 					  vmaster_tlv, via_slave_vols);
1391370bafbdSTakashi Iwai 		if (err < 0)
1392370bafbdSTakashi Iwai 			return err;
1393370bafbdSTakashi Iwai 	}
1394370bafbdSTakashi Iwai 	if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1395370bafbdSTakashi Iwai 		err = snd_hda_add_vmaster(codec, "Master Playback Switch",
1396370bafbdSTakashi Iwai 					  NULL, via_slave_sws);
1397370bafbdSTakashi Iwai 		if (err < 0)
1398370bafbdSTakashi Iwai 			return err;
1399370bafbdSTakashi Iwai 	}
1400370bafbdSTakashi Iwai 
14015b0cb1d8SJaroslav Kysela 	/* assign Capture Source enums to NID */
14025b0cb1d8SJaroslav Kysela 	kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
14035b0cb1d8SJaroslav Kysela 	for (i = 0; kctl && i < kctl->count; i++) {
140421949f00STakashi Iwai 		err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
14055b0cb1d8SJaroslav Kysela 		if (err < 0)
14065b0cb1d8SJaroslav Kysela 			return err;
14075b0cb1d8SJaroslav Kysela 	}
14085b0cb1d8SJaroslav Kysela 
140917314379SLydia Wang 	/* init power states */
14103e95b9abSLydia Wang 	set_widgets_power_state(codec);
1411ada509ecSTakashi Iwai 	analog_low_current_mode(codec);
141217314379SLydia Wang 
1413603c4019STakashi Iwai 	via_free_kctls(codec); /* no longer needed */
1414c577b8a1SJoseph Chan 	return 0;
1415c577b8a1SJoseph Chan }
1416c577b8a1SJoseph Chan 
1417c577b8a1SJoseph Chan static int via_build_pcms(struct hda_codec *codec)
1418c577b8a1SJoseph Chan {
1419c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1420c577b8a1SJoseph Chan 	struct hda_pcm *info = spec->pcm_rec;
1421c577b8a1SJoseph Chan 
1422c577b8a1SJoseph Chan 	codec->num_pcms = 1;
1423c577b8a1SJoseph Chan 	codec->pcm_info = info;
1424c577b8a1SJoseph Chan 
142582673bc8STakashi Iwai 	snprintf(spec->stream_name_analog, sizeof(spec->stream_name_analog),
142682673bc8STakashi Iwai 		 "%s Analog", codec->chip_name);
1427c577b8a1SJoseph Chan 	info->name = spec->stream_name_analog;
14289af74210STakashi Iwai 
14299af74210STakashi Iwai 	if (!spec->stream_analog_playback)
14309af74210STakashi Iwai 		spec->stream_analog_playback = &via_pcm_analog_playback;
1431377ff31aSLydia Wang 	info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
14329af74210STakashi Iwai 		*spec->stream_analog_playback;
1433377ff31aSLydia Wang 	info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1434377ff31aSLydia Wang 		spec->multiout.dac_nids[0];
1435c577b8a1SJoseph Chan 	info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1436c577b8a1SJoseph Chan 		spec->multiout.max_channels;
14379af74210STakashi Iwai 
1438a86a88eaSTakashi Iwai 	if (!spec->stream_analog_capture) {
1439a86a88eaSTakashi Iwai 		if (spec->dyn_adc_switch)
1440a86a88eaSTakashi Iwai 			spec->stream_analog_capture =
1441a86a88eaSTakashi Iwai 				&via_pcm_dyn_adc_analog_capture;
1442a86a88eaSTakashi Iwai 		else
14439af74210STakashi Iwai 			spec->stream_analog_capture = &via_pcm_analog_capture;
1444a86a88eaSTakashi Iwai 	}
14459af74210STakashi Iwai 	info->stream[SNDRV_PCM_STREAM_CAPTURE] =
14469af74210STakashi Iwai 		*spec->stream_analog_capture;
14479af74210STakashi Iwai 	info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
1448a86a88eaSTakashi Iwai 	if (!spec->dyn_adc_switch)
14499af74210STakashi Iwai 		info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
14509af74210STakashi Iwai 			spec->num_adc_nids;
1451c577b8a1SJoseph Chan 
1452c577b8a1SJoseph Chan 	if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1453c577b8a1SJoseph Chan 		codec->num_pcms++;
1454c577b8a1SJoseph Chan 		info++;
145582673bc8STakashi Iwai 		snprintf(spec->stream_name_digital,
145682673bc8STakashi Iwai 			 sizeof(spec->stream_name_digital),
145782673bc8STakashi Iwai 			 "%s Digital", codec->chip_name);
1458c577b8a1SJoseph Chan 		info->name = spec->stream_name_digital;
14597ba72ba1STakashi Iwai 		info->pcm_type = HDA_PCM_TYPE_SPDIF;
1460c577b8a1SJoseph Chan 		if (spec->multiout.dig_out_nid) {
14619af74210STakashi Iwai 			if (!spec->stream_digital_playback)
14629af74210STakashi Iwai 				spec->stream_digital_playback =
14639af74210STakashi Iwai 					&via_pcm_digital_playback;
1464c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
14659af74210STakashi Iwai 				*spec->stream_digital_playback;
1466c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1467c577b8a1SJoseph Chan 				spec->multiout.dig_out_nid;
1468c577b8a1SJoseph Chan 		}
1469c577b8a1SJoseph Chan 		if (spec->dig_in_nid) {
14709af74210STakashi Iwai 			if (!spec->stream_digital_capture)
14719af74210STakashi Iwai 				spec->stream_digital_capture =
14729af74210STakashi Iwai 					&via_pcm_digital_capture;
1473c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_CAPTURE] =
14749af74210STakashi Iwai 				*spec->stream_digital_capture;
1475c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1476c577b8a1SJoseph Chan 				spec->dig_in_nid;
1477c577b8a1SJoseph Chan 		}
1478c577b8a1SJoseph Chan 	}
1479c577b8a1SJoseph Chan 
1480ece8d043STakashi Iwai 	if (spec->hp_dac_nid) {
14817eb56e84STakashi Iwai 		codec->num_pcms++;
14827eb56e84STakashi Iwai 		info++;
14837eb56e84STakashi Iwai 		snprintf(spec->stream_name_hp, sizeof(spec->stream_name_hp),
14847eb56e84STakashi Iwai 			 "%s HP", codec->chip_name);
14857eb56e84STakashi Iwai 		info->name = spec->stream_name_hp;
14867eb56e84STakashi Iwai 		info->stream[SNDRV_PCM_STREAM_PLAYBACK] = via_pcm_hp_playback;
14877eb56e84STakashi Iwai 		info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1488ece8d043STakashi Iwai 			spec->hp_dac_nid;
14897eb56e84STakashi Iwai 	}
1490c577b8a1SJoseph Chan 	return 0;
1491c577b8a1SJoseph Chan }
1492c577b8a1SJoseph Chan 
1493c577b8a1SJoseph Chan static void via_free(struct hda_codec *codec)
1494c577b8a1SJoseph Chan {
1495c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1496c577b8a1SJoseph Chan 
1497c577b8a1SJoseph Chan 	if (!spec)
1498c577b8a1SJoseph Chan 		return;
1499c577b8a1SJoseph Chan 
1500603c4019STakashi Iwai 	via_free_kctls(codec);
15011f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
1502a86a88eaSTakashi Iwai 	kfree(spec->bind_cap_vol);
1503a86a88eaSTakashi Iwai 	kfree(spec->bind_cap_sw);
1504a86a88eaSTakashi Iwai 	kfree(spec);
1505c577b8a1SJoseph Chan }
1506c577b8a1SJoseph Chan 
150764be285bSTakashi Iwai /* mute/unmute outputs */
150864be285bSTakashi Iwai static void toggle_output_mutes(struct hda_codec *codec, int num_pins,
150964be285bSTakashi Iwai 				hda_nid_t *pins, bool mute)
151064be285bSTakashi Iwai {
151164be285bSTakashi Iwai 	int i;
1512*94994734STakashi Iwai 	for (i = 0; i < num_pins; i++) {
1513*94994734STakashi Iwai 		unsigned int parm = snd_hda_codec_read(codec, pins[i], 0,
1514*94994734STakashi Iwai 					  AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1515*94994734STakashi Iwai 		if (parm & AC_PINCTL_IN_EN)
1516*94994734STakashi Iwai 			continue;
1517*94994734STakashi Iwai 		if (mute)
1518*94994734STakashi Iwai 			parm &= ~AC_PINCTL_OUT_EN;
1519*94994734STakashi Iwai 		else
1520*94994734STakashi Iwai 			parm |= AC_PINCTL_OUT_EN;
152164be285bSTakashi Iwai 		snd_hda_codec_write(codec, pins[i], 0,
1522*94994734STakashi Iwai 				    AC_VERB_SET_PIN_WIDGET_CONTROL, parm);
1523*94994734STakashi Iwai 	}
152464be285bSTakashi Iwai }
152564be285bSTakashi Iwai 
15264a918ffeSTakashi Iwai /* mute internal speaker if line-out is plugged */
15274a918ffeSTakashi Iwai static void via_line_automute(struct hda_codec *codec, int present)
15284a918ffeSTakashi Iwai {
15294a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
15304a918ffeSTakashi Iwai 
15314a918ffeSTakashi Iwai 	if (!spec->autocfg.speaker_outs)
15324a918ffeSTakashi Iwai 		return;
15334a918ffeSTakashi Iwai 	if (!present)
15344a918ffeSTakashi Iwai 		present = snd_hda_jack_detect(codec,
15354a918ffeSTakashi Iwai 					      spec->autocfg.line_out_pins[0]);
15364a918ffeSTakashi Iwai 	toggle_output_mutes(codec, spec->autocfg.speaker_outs,
15374a918ffeSTakashi Iwai 			    spec->autocfg.speaker_pins,
15384a918ffeSTakashi Iwai 			    present);
15394a918ffeSTakashi Iwai }
15404a918ffeSTakashi Iwai 
154169e52a80SHarald Welte /* mute internal speaker if HP is plugged */
154269e52a80SHarald Welte static void via_hp_automute(struct hda_codec *codec)
154369e52a80SHarald Welte {
15444a918ffeSTakashi Iwai 	int present = 0;
15456e969d91STakashi Iwai 	int nums;
154669e52a80SHarald Welte 	struct via_spec *spec = codec->spec;
154769e52a80SHarald Welte 
15486e969d91STakashi Iwai 	if (!spec->hp_independent_mode && spec->autocfg.hp_pins[0])
1549d56757abSTakashi Iwai 		present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
15506e969d91STakashi Iwai 
1551f2b1c9f0STakashi Iwai 	if (spec->smart51_enabled)
1552f2b1c9f0STakashi Iwai 		nums = spec->autocfg.line_outs + spec->smart51_nums;
1553f2b1c9f0STakashi Iwai 	else
1554f2b1c9f0STakashi Iwai 		nums = spec->autocfg.line_outs;
15556e969d91STakashi Iwai 	toggle_output_mutes(codec, nums, spec->autocfg.line_out_pins, present);
15566e969d91STakashi Iwai 
15574a918ffeSTakashi Iwai 	via_line_automute(codec, present);
1558f3db423dSLydia Wang }
1559f3db423dSLydia Wang 
156069e52a80SHarald Welte static void via_gpio_control(struct hda_codec *codec)
156169e52a80SHarald Welte {
156269e52a80SHarald Welte 	unsigned int gpio_data;
156369e52a80SHarald Welte 	unsigned int vol_counter;
156469e52a80SHarald Welte 	unsigned int vol;
156569e52a80SHarald Welte 	unsigned int master_vol;
156669e52a80SHarald Welte 
156769e52a80SHarald Welte 	struct via_spec *spec = codec->spec;
156869e52a80SHarald Welte 
156969e52a80SHarald Welte 	gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
157069e52a80SHarald Welte 				       AC_VERB_GET_GPIO_DATA, 0) & 0x03;
157169e52a80SHarald Welte 
157269e52a80SHarald Welte 	vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
157369e52a80SHarald Welte 					  0xF84, 0) & 0x3F0000) >> 16;
157469e52a80SHarald Welte 
157569e52a80SHarald Welte 	vol = vol_counter & 0x1F;
157669e52a80SHarald Welte 	master_vol = snd_hda_codec_read(codec, 0x1A, 0,
157769e52a80SHarald Welte 					AC_VERB_GET_AMP_GAIN_MUTE,
157869e52a80SHarald Welte 					AC_AMP_GET_INPUT);
157969e52a80SHarald Welte 
158069e52a80SHarald Welte 	if (gpio_data == 0x02) {
158169e52a80SHarald Welte 		/* unmute line out */
15823e0693e2STakashi Iwai 		snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
15833e0693e2STakashi Iwai 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
15843e0693e2STakashi Iwai 				    PIN_OUT);
158569e52a80SHarald Welte 		if (vol_counter & 0x20) {
158669e52a80SHarald Welte 			/* decrease volume */
158769e52a80SHarald Welte 			if (vol > master_vol)
158869e52a80SHarald Welte 				vol = master_vol;
158969e52a80SHarald Welte 			snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
159069e52a80SHarald Welte 						 0, HDA_AMP_VOLMASK,
159169e52a80SHarald Welte 						 master_vol-vol);
159269e52a80SHarald Welte 		} else {
159369e52a80SHarald Welte 			/* increase volume */
159469e52a80SHarald Welte 			snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
159569e52a80SHarald Welte 					 HDA_AMP_VOLMASK,
159669e52a80SHarald Welte 					 ((master_vol+vol) > 0x2A) ? 0x2A :
159769e52a80SHarald Welte 					  (master_vol+vol));
159869e52a80SHarald Welte 		}
159969e52a80SHarald Welte 	} else if (!(gpio_data & 0x02)) {
160069e52a80SHarald Welte 		/* mute line out */
16013e0693e2STakashi Iwai 		snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
16023e0693e2STakashi Iwai 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
16033e0693e2STakashi Iwai 				    0);
160469e52a80SHarald Welte 	}
160569e52a80SHarald Welte }
160669e52a80SHarald Welte 
160769e52a80SHarald Welte /* unsolicited event for jack sensing */
160869e52a80SHarald Welte static void via_unsol_event(struct hda_codec *codec,
160969e52a80SHarald Welte 				  unsigned int res)
161069e52a80SHarald Welte {
161169e52a80SHarald Welte 	res >>= 26;
1612ec7e7e42SLydia Wang 
1613a34df19aSLydia Wang 	if (res & VIA_JACK_EVENT)
16143e95b9abSLydia Wang 		set_widgets_power_state(codec);
1615ec7e7e42SLydia Wang 
1616ec7e7e42SLydia Wang 	res &= ~VIA_JACK_EVENT;
1617ec7e7e42SLydia Wang 
161821ce0b65STakashi Iwai 	if (res == VIA_HP_EVENT || res == VIA_LINE_EVENT)
1619ec7e7e42SLydia Wang 		via_hp_automute(codec);
1620ec7e7e42SLydia Wang 	else if (res == VIA_GPIO_EVENT)
1621ec7e7e42SLydia Wang 		via_gpio_control(codec);
162269e52a80SHarald Welte }
162369e52a80SHarald Welte 
16241f2e99feSLydia Wang #ifdef SND_HDA_NEEDS_RESUME
16251f2e99feSLydia Wang static int via_suspend(struct hda_codec *codec, pm_message_t state)
16261f2e99feSLydia Wang {
16271f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
16281f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
16291f2e99feSLydia Wang 	return 0;
16301f2e99feSLydia Wang }
16311f2e99feSLydia Wang #endif
16321f2e99feSLydia Wang 
1633cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
1634cb53c626STakashi Iwai static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1635cb53c626STakashi Iwai {
1636cb53c626STakashi Iwai 	struct via_spec *spec = codec->spec;
1637cb53c626STakashi Iwai 	return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1638cb53c626STakashi Iwai }
1639cb53c626STakashi Iwai #endif
1640cb53c626STakashi Iwai 
1641c577b8a1SJoseph Chan /*
1642c577b8a1SJoseph Chan  */
16435d41762aSTakashi Iwai 
16445d41762aSTakashi Iwai static int via_init(struct hda_codec *codec);
16455d41762aSTakashi Iwai 
164690dd48a1STakashi Iwai static const struct hda_codec_ops via_patch_ops = {
1647c577b8a1SJoseph Chan 	.build_controls = via_build_controls,
1648c577b8a1SJoseph Chan 	.build_pcms = via_build_pcms,
1649c577b8a1SJoseph Chan 	.init = via_init,
1650c577b8a1SJoseph Chan 	.free = via_free,
16514a918ffeSTakashi Iwai 	.unsol_event = via_unsol_event,
16521f2e99feSLydia Wang #ifdef SND_HDA_NEEDS_RESUME
16531f2e99feSLydia Wang 	.suspend = via_suspend,
16541f2e99feSLydia Wang #endif
1655cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
1656cb53c626STakashi Iwai 	.check_power_status = via_check_power_status,
1657cb53c626STakashi Iwai #endif
1658c577b8a1SJoseph Chan };
1659c577b8a1SJoseph Chan 
16604a79616dSTakashi Iwai static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
1661c577b8a1SJoseph Chan {
16624a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
16634a79616dSTakashi Iwai 	int i;
16644a79616dSTakashi Iwai 
16654a79616dSTakashi Iwai 	for (i = 0; i < spec->multiout.num_dacs; i++) {
16664a79616dSTakashi Iwai 		if (spec->multiout.dac_nids[i] == dac)
16674a79616dSTakashi Iwai 			return false;
16684a79616dSTakashi Iwai 	}
1669ece8d043STakashi Iwai 	if (spec->hp_dac_nid == dac)
16704a79616dSTakashi Iwai 		return false;
16714a79616dSTakashi Iwai 	return true;
16724a79616dSTakashi Iwai }
16734a79616dSTakashi Iwai 
16748e3679dcSTakashi Iwai static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
16754a79616dSTakashi Iwai 			      hda_nid_t target_dac, struct nid_path *path,
16764a79616dSTakashi Iwai 			      int depth, int wid_type)
16774a79616dSTakashi Iwai {
16784a79616dSTakashi Iwai 	hda_nid_t conn[8];
16794a79616dSTakashi Iwai 	int i, nums;
16804a79616dSTakashi Iwai 
16814a79616dSTakashi Iwai 	nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
16824a79616dSTakashi Iwai 	for (i = 0; i < nums; i++) {
16834a79616dSTakashi Iwai 		if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
16844a79616dSTakashi Iwai 			continue;
168509a9ad69STakashi Iwai 		if (conn[i] == target_dac || is_empty_dac(codec, conn[i]))
168609a9ad69STakashi Iwai 			goto found;
16874a79616dSTakashi Iwai 	}
16888e3679dcSTakashi Iwai 	if (depth >= MAX_NID_PATH_DEPTH)
16894a79616dSTakashi Iwai 		return false;
16904a79616dSTakashi Iwai 	for (i = 0; i < nums; i++) {
16914a79616dSTakashi Iwai 		unsigned int type;
16924a79616dSTakashi Iwai 		type = get_wcaps_type(get_wcaps(codec, conn[i]));
16934a79616dSTakashi Iwai 		if (type == AC_WID_AUD_OUT ||
16944a79616dSTakashi Iwai 		    (wid_type != -1 && type != wid_type))
16954a79616dSTakashi Iwai 			continue;
16968e3679dcSTakashi Iwai 		if (__parse_output_path(codec, conn[i], target_dac,
169709a9ad69STakashi Iwai 				      path, depth + 1, AC_WID_AUD_SEL))
169809a9ad69STakashi Iwai 			goto found;
16994a79616dSTakashi Iwai 	}
17004a79616dSTakashi Iwai 	return false;
170109a9ad69STakashi Iwai 
170209a9ad69STakashi Iwai  found:
170309a9ad69STakashi Iwai 	path->path[path->depth] = conn[i];
170409a9ad69STakashi Iwai 	path->idx[path->depth] = i;
170509a9ad69STakashi Iwai 	if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
170609a9ad69STakashi Iwai 		path->multi[path->depth] = 1;
170709a9ad69STakashi Iwai 	path->depth++;
170809a9ad69STakashi Iwai 	return true;
17094a79616dSTakashi Iwai }
17104a79616dSTakashi Iwai 
17118e3679dcSTakashi Iwai static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
17128e3679dcSTakashi Iwai 			      hda_nid_t target_dac, struct nid_path *path)
17138e3679dcSTakashi Iwai {
17148e3679dcSTakashi Iwai 	if (__parse_output_path(codec, nid, target_dac, path, 1, -1)) {
17158e3679dcSTakashi Iwai 		path->path[path->depth] = nid;
17168e3679dcSTakashi Iwai 		path->depth++;
17178e3679dcSTakashi Iwai 		return true;
17188e3679dcSTakashi Iwai 	}
17198e3679dcSTakashi Iwai 	return false;
17208e3679dcSTakashi Iwai }
17218e3679dcSTakashi Iwai 
17224a79616dSTakashi Iwai static int via_auto_fill_dac_nids(struct hda_codec *codec)
17234a79616dSTakashi Iwai {
17244a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
17254a79616dSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
17265c9a5615SLydia Wang 	int i, dac_num;
1727c577b8a1SJoseph Chan 	hda_nid_t nid;
1728c577b8a1SJoseph Chan 
1729c577b8a1SJoseph Chan 	spec->multiout.dac_nids = spec->private_dac_nids;
17305c9a5615SLydia Wang 	dac_num = 0;
17314a79616dSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
1732c577b8a1SJoseph Chan 		nid = cfg->line_out_pins[i];
17334a79616dSTakashi Iwai 		if (!nid)
17344a79616dSTakashi Iwai 			continue;
17355c9a5615SLydia Wang 		if (parse_output_path(codec, nid, 0, &spec->out_path[i])) {
17368e3679dcSTakashi Iwai 			spec->private_dac_nids[i] = spec->out_path[i].path[0];
17375c9a5615SLydia Wang 			dac_num++;
1738c577b8a1SJoseph Chan 		}
17395c9a5615SLydia Wang 	}
17405c9a5615SLydia Wang 	spec->multiout.num_dacs = dac_num;
1741c577b8a1SJoseph Chan 	return 0;
1742c577b8a1SJoseph Chan }
1743c577b8a1SJoseph Chan 
17444a79616dSTakashi Iwai static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
174509a9ad69STakashi Iwai 			  int chs, bool check_dac, struct nid_path *path)
1746c577b8a1SJoseph Chan {
17474a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
1748c577b8a1SJoseph Chan 	char name[32];
174909a9ad69STakashi Iwai 	hda_nid_t dac, pin, sel, nid;
175009a9ad69STakashi Iwai 	int err;
1751a934d5a9STakashi Iwai 
175209a9ad69STakashi Iwai 	dac = check_dac ? path->path[0] : 0;
175309a9ad69STakashi Iwai 	pin = path->path[path->depth - 1];
175409a9ad69STakashi Iwai 	sel = path->depth > 1 ? path->path[1] : 0;
1755c577b8a1SJoseph Chan 
17568df2a312STakashi Iwai 	if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
17574a79616dSTakashi Iwai 		nid = dac;
17588df2a312STakashi Iwai 	else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
17594a79616dSTakashi Iwai 		nid = pin;
1760a934d5a9STakashi Iwai 	else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1761a934d5a9STakashi Iwai 		nid = sel;
17624a79616dSTakashi Iwai 	else
17634a79616dSTakashi Iwai 		nid = 0;
17644a79616dSTakashi Iwai 	if (nid) {
17654a79616dSTakashi Iwai 		sprintf(name, "%s Playback Volume", pfx);
1766c577b8a1SJoseph Chan 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1767a00a5fadSLydia Wang 			      HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1768c577b8a1SJoseph Chan 		if (err < 0)
1769c577b8a1SJoseph Chan 			return err;
177009a9ad69STakashi Iwai 		path->vol_ctl = nid;
1771c577b8a1SJoseph Chan 	}
17724a79616dSTakashi Iwai 
17738df2a312STakashi Iwai 	if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
17744a79616dSTakashi Iwai 		nid = dac;
17758df2a312STakashi Iwai 	else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
17764a79616dSTakashi Iwai 		nid = pin;
1777a934d5a9STakashi Iwai 	else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
1778a934d5a9STakashi Iwai 		nid = sel;
17794a79616dSTakashi Iwai 	else
17804a79616dSTakashi Iwai 		nid = 0;
17814a79616dSTakashi Iwai 	if (nid) {
17824a79616dSTakashi Iwai 		sprintf(name, "%s Playback Switch", pfx);
17834a79616dSTakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
17844a79616dSTakashi Iwai 			      HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
17854a79616dSTakashi Iwai 		if (err < 0)
17864a79616dSTakashi Iwai 			return err;
178709a9ad69STakashi Iwai 		path->mute_ctl = nid;
17884a79616dSTakashi Iwai 	}
17894a79616dSTakashi Iwai 	return 0;
17904a79616dSTakashi Iwai }
17914a79616dSTakashi Iwai 
1792f4a7828bSTakashi Iwai static void mangle_smart51(struct hda_codec *codec)
1793f4a7828bSTakashi Iwai {
1794f4a7828bSTakashi Iwai 	struct via_spec *spec = codec->spec;
1795f4a7828bSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
17960f98c24bSTakashi Iwai 	struct auto_pin_cfg_item *ins = cfg->inputs;
17970f98c24bSTakashi Iwai 	int i, j, nums, attr;
17980f98c24bSTakashi Iwai 	int pins[AUTO_CFG_MAX_INS];
1799f4a7828bSTakashi Iwai 
18000f98c24bSTakashi Iwai 	for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
18010f98c24bSTakashi Iwai 		nums = 0;
1802f4a7828bSTakashi Iwai 		for (i = 0; i < cfg->num_inputs; i++) {
18030f98c24bSTakashi Iwai 			unsigned int def;
18040f98c24bSTakashi Iwai 			if (ins[i].type > AUTO_PIN_LINE_IN)
18050f98c24bSTakashi Iwai 				continue;
18060f98c24bSTakashi Iwai 			def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
18070f98c24bSTakashi Iwai 			if (snd_hda_get_input_pin_attr(def) != attr)
18080f98c24bSTakashi Iwai 				continue;
18090f98c24bSTakashi Iwai 			for (j = 0; j < nums; j++)
18100f98c24bSTakashi Iwai 				if (ins[pins[j]].type < ins[i].type) {
18110f98c24bSTakashi Iwai 					memmove(pins + j + 1, pins + j,
181221d45d2bSTakashi Iwai 						(nums - j) * sizeof(int));
18130f98c24bSTakashi Iwai 					break;
18140f98c24bSTakashi Iwai 				}
18150f98c24bSTakashi Iwai 			pins[j] = i;
1816e3d7a143STakashi Iwai 			nums++;
1817e3d7a143STakashi Iwai 		}
1818e3d7a143STakashi Iwai 		if (cfg->line_outs + nums < 3)
1819f4a7828bSTakashi Iwai 			continue;
18200f98c24bSTakashi Iwai 		for (i = 0; i < nums; i++) {
18210f98c24bSTakashi Iwai 			hda_nid_t pin = ins[pins[i]].pin;
18220f98c24bSTakashi Iwai 			spec->smart51_pins[spec->smart51_nums++] = pin;
18230f98c24bSTakashi Iwai 			cfg->line_out_pins[cfg->line_outs++] = pin;
1824f4a7828bSTakashi Iwai 			if (cfg->line_outs == 3)
1825f4a7828bSTakashi Iwai 				break;
1826f4a7828bSTakashi Iwai 		}
18270f98c24bSTakashi Iwai 		return;
18280f98c24bSTakashi Iwai 	}
1829f4a7828bSTakashi Iwai }
1830f4a7828bSTakashi Iwai 
18314a79616dSTakashi Iwai /* add playback controls from the parsed DAC table */
18324a79616dSTakashi Iwai static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
18334a79616dSTakashi Iwai {
18344a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
1835f4a7828bSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
18364a79616dSTakashi Iwai 	static const char * const chname[4] = {
18374a79616dSTakashi Iwai 		"Front", "Surround", "C/LFE", "Side"
18384a79616dSTakashi Iwai 	};
18394a79616dSTakashi Iwai 	int i, idx, err;
1840f4a7828bSTakashi Iwai 	int old_line_outs;
1841f4a7828bSTakashi Iwai 
1842f4a7828bSTakashi Iwai 	/* check smart51 */
1843f4a7828bSTakashi Iwai 	old_line_outs = cfg->line_outs;
1844f4a7828bSTakashi Iwai 	if (cfg->line_outs == 1)
1845f4a7828bSTakashi Iwai 		mangle_smart51(codec);
18464a79616dSTakashi Iwai 
1847e3d7a143STakashi Iwai 	err = via_auto_fill_dac_nids(codec);
1848e3d7a143STakashi Iwai 	if (err < 0)
1849e3d7a143STakashi Iwai 		return err;
1850e3d7a143STakashi Iwai 
18515c9a5615SLydia Wang 	if (spec->multiout.num_dacs < 3) {
18525c9a5615SLydia Wang 		spec->smart51_nums = 0;
18535c9a5615SLydia Wang 		cfg->line_outs = old_line_outs;
18545c9a5615SLydia Wang 	}
18554a79616dSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
18564a79616dSTakashi Iwai 		hda_nid_t pin, dac;
18574a79616dSTakashi Iwai 		pin = cfg->line_out_pins[i];
18584a79616dSTakashi Iwai 		dac = spec->multiout.dac_nids[i];
18594a79616dSTakashi Iwai 		if (!pin || !dac)
18604a79616dSTakashi Iwai 			continue;
18610fe0adf8STakashi Iwai 		if (i == HDA_CLFE) {
186209a9ad69STakashi Iwai 			err = create_ch_ctls(codec, "Center", 1, true,
186309a9ad69STakashi Iwai 					     &spec->out_path[i]);
18644a79616dSTakashi Iwai 			if (err < 0)
18654a79616dSTakashi Iwai 				return err;
186609a9ad69STakashi Iwai 			err = create_ch_ctls(codec, "LFE", 2, true,
186709a9ad69STakashi Iwai 					     &spec->out_path[i]);
18684a79616dSTakashi Iwai 			if (err < 0)
18694a79616dSTakashi Iwai 				return err;
18704a79616dSTakashi Iwai 		} else {
18716aadf41dSTakashi Iwai 			const char *pfx = chname[i];
18726aadf41dSTakashi Iwai 			if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
18736aadf41dSTakashi Iwai 			    cfg->line_outs == 1)
18746aadf41dSTakashi Iwai 				pfx = "Speaker";
187509a9ad69STakashi Iwai 			err = create_ch_ctls(codec, pfx, 3, true,
187609a9ad69STakashi Iwai 					     &spec->out_path[i]);
18774a79616dSTakashi Iwai 			if (err < 0)
18784a79616dSTakashi Iwai 				return err;
18794a79616dSTakashi Iwai 		}
18804a79616dSTakashi Iwai 	}
18814a79616dSTakashi Iwai 
18824a79616dSTakashi Iwai 	idx = get_connection_index(codec, spec->aa_mix_nid,
18834a79616dSTakashi Iwai 				   spec->multiout.dac_nids[0]);
1884c4394f5bSLydia Wang 	if (idx < 0 && spec->dac_mixer_idx)
1885c4394f5bSLydia Wang 		idx = spec->dac_mixer_idx;
18864a79616dSTakashi Iwai 	if (idx >= 0) {
18874a79616dSTakashi Iwai 		/* add control to mixer */
18884a79616dSTakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
18894a79616dSTakashi Iwai 				      "PCM Playback Volume",
18904a79616dSTakashi Iwai 				      HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
18914a79616dSTakashi Iwai 							  idx, HDA_INPUT));
18924a79616dSTakashi Iwai 		if (err < 0)
18934a79616dSTakashi Iwai 			return err;
18944a79616dSTakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
18954a79616dSTakashi Iwai 				      "PCM Playback Switch",
18964a79616dSTakashi Iwai 				      HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
18974a79616dSTakashi Iwai 							  idx, HDA_INPUT));
18984a79616dSTakashi Iwai 		if (err < 0)
18994a79616dSTakashi Iwai 			return err;
1900c577b8a1SJoseph Chan 	}
1901c577b8a1SJoseph Chan 
1902f4a7828bSTakashi Iwai 	cfg->line_outs = old_line_outs;
1903f4a7828bSTakashi Iwai 
1904c577b8a1SJoseph Chan 	return 0;
1905c577b8a1SJoseph Chan }
1906c577b8a1SJoseph Chan 
19074a79616dSTakashi Iwai static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
1908c577b8a1SJoseph Chan {
19094a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
191009a9ad69STakashi Iwai 	struct nid_path *path;
191118bd2c44STakashi Iwai 	bool check_dac;
1912c577b8a1SJoseph Chan 	int err;
1913c577b8a1SJoseph Chan 
1914c577b8a1SJoseph Chan 	if (!pin)
1915c577b8a1SJoseph Chan 		return 0;
1916c577b8a1SJoseph Chan 
19178df2a312STakashi Iwai 	if (parse_output_path(codec, pin, 0, &spec->hp_path))
19188e3679dcSTakashi Iwai 		spec->hp_dac_nid = spec->hp_path.path[0];
191925250505STakashi Iwai 	else if (spec->multiout.dac_nids[HDA_SIDE] &&
192025250505STakashi Iwai 		 parse_output_path(codec, pin,
192125250505STakashi Iwai 				   spec->multiout.dac_nids[HDA_SIDE],
192225250505STakashi Iwai 				   &spec->hp_path)) {
192325250505STakashi Iwai 		spec->hp_dac_nid = spec->hp_path.path[0];
192425250505STakashi Iwai 		spec->hp_indep_shared = true;
1925a2a870c8SLydia Wang 	} else if (spec->multiout.dac_nids[HDA_CLFE] &&
1926a2a870c8SLydia Wang 		 parse_output_path(codec, pin,
1927a2a870c8SLydia Wang 				   spec->multiout.dac_nids[HDA_CLFE],
1928a2a870c8SLydia Wang 				   &spec->hp_path)) {
1929a2a870c8SLydia Wang 		spec->hp_dac_nid = spec->hp_path.path[0];
1930a2a870c8SLydia Wang 		spec->hp_indep_shared = true;
193125250505STakashi Iwai 	}
19324a79616dSTakashi Iwai 
1933ece8d043STakashi Iwai 	if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
19348e3679dcSTakashi Iwai 			       &spec->hp_dep_path) &&
1935ece8d043STakashi Iwai 	    !spec->hp_dac_nid)
1936ece8d043STakashi Iwai 		return 0;
1937ece8d043STakashi Iwai 
193818bd2c44STakashi Iwai 	if (spec->hp_dac_nid && !spec->hp_indep_shared) {
193909a9ad69STakashi Iwai 		path = &spec->hp_path;
194018bd2c44STakashi Iwai 		check_dac = true;
194118bd2c44STakashi Iwai 	} else {
194209a9ad69STakashi Iwai 		path = &spec->hp_dep_path;
194318bd2c44STakashi Iwai 		check_dac = false;
194418bd2c44STakashi Iwai 	}
194518bd2c44STakashi Iwai 	err = create_ch_ctls(codec, "Headphone", 3, check_dac, path);
19464a79616dSTakashi Iwai 	if (err < 0)
19474a79616dSTakashi Iwai 		return err;
194809a9ad69STakashi Iwai 	if (spec->hp_dac_nid) {
194909a9ad69STakashi Iwai 		spec->hp_dep_path.vol_ctl = spec->hp_path.vol_ctl;
195009a9ad69STakashi Iwai 		spec->hp_dep_path.mute_ctl = spec->hp_path.mute_ctl;
195109a9ad69STakashi Iwai 	}
19520aa62aefSHarald Welte 
1953c577b8a1SJoseph Chan 	return 0;
1954c577b8a1SJoseph Chan }
1955c577b8a1SJoseph Chan 
19564a918ffeSTakashi Iwai static int via_auto_create_speaker_ctls(struct hda_codec *codec)
19574a918ffeSTakashi Iwai {
19584a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
19594a918ffeSTakashi Iwai 	hda_nid_t pin, dac;
19604a918ffeSTakashi Iwai 
19614a918ffeSTakashi Iwai 	pin = spec->autocfg.speaker_pins[0];
19624a918ffeSTakashi Iwai 	if (!spec->autocfg.speaker_outs || !pin)
19634a918ffeSTakashi Iwai 		return 0;
19644a918ffeSTakashi Iwai 
19658e3679dcSTakashi Iwai 	if (parse_output_path(codec, pin, 0, &spec->speaker_path)) {
19668e3679dcSTakashi Iwai 		dac = spec->speaker_path.path[0];
19674a918ffeSTakashi Iwai 		spec->multiout.extra_out_nid[0] = dac;
196809a9ad69STakashi Iwai 		return create_ch_ctls(codec, "Speaker", 3, true,
196909a9ad69STakashi Iwai 				      &spec->speaker_path);
19704a918ffeSTakashi Iwai 	}
19714a918ffeSTakashi Iwai 	if (parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
19728e3679dcSTakashi Iwai 			      &spec->speaker_path))
197309a9ad69STakashi Iwai 		return create_ch_ctls(codec, "Speaker", 3, false,
197409a9ad69STakashi Iwai 				      &spec->speaker_path);
19754a918ffeSTakashi Iwai 
19764a918ffeSTakashi Iwai 	return 0;
19774a918ffeSTakashi Iwai }
19784a918ffeSTakashi Iwai 
1979a766d0d7STakashi Iwai /* look for ADCs */
1980a766d0d7STakashi Iwai static int via_fill_adcs(struct hda_codec *codec)
1981a766d0d7STakashi Iwai {
1982a766d0d7STakashi Iwai 	struct via_spec *spec = codec->spec;
1983a766d0d7STakashi Iwai 	hda_nid_t nid = codec->start_nid;
1984a766d0d7STakashi Iwai 	int i;
1985a766d0d7STakashi Iwai 
1986a766d0d7STakashi Iwai 	for (i = 0; i < codec->num_nodes; i++, nid++) {
1987a766d0d7STakashi Iwai 		unsigned int wcaps = get_wcaps(codec, nid);
1988a766d0d7STakashi Iwai 		if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
1989a766d0d7STakashi Iwai 			continue;
1990a766d0d7STakashi Iwai 		if (wcaps & AC_WCAP_DIGITAL)
1991a766d0d7STakashi Iwai 			continue;
1992a766d0d7STakashi Iwai 		if (!(wcaps & AC_WCAP_CONN_LIST))
1993a766d0d7STakashi Iwai 			continue;
1994a766d0d7STakashi Iwai 		if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
1995a766d0d7STakashi Iwai 			return -ENOMEM;
1996a766d0d7STakashi Iwai 		spec->adc_nids[spec->num_adc_nids++] = nid;
1997a766d0d7STakashi Iwai 	}
1998a766d0d7STakashi Iwai 	return 0;
1999a766d0d7STakashi Iwai }
2000a766d0d7STakashi Iwai 
2001a86a88eaSTakashi Iwai /* input-src control */
2002a86a88eaSTakashi Iwai static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
2003a86a88eaSTakashi Iwai 			     struct snd_ctl_elem_info *uinfo)
2004a86a88eaSTakashi Iwai {
2005a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2006a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2007a86a88eaSTakashi Iwai 
2008a86a88eaSTakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2009a86a88eaSTakashi Iwai 	uinfo->count = 1;
2010a86a88eaSTakashi Iwai 	uinfo->value.enumerated.items = spec->num_inputs;
2011a86a88eaSTakashi Iwai 	if (uinfo->value.enumerated.item >= spec->num_inputs)
2012a86a88eaSTakashi Iwai 		uinfo->value.enumerated.item = spec->num_inputs - 1;
2013a86a88eaSTakashi Iwai 	strcpy(uinfo->value.enumerated.name,
2014a86a88eaSTakashi Iwai 	       spec->inputs[uinfo->value.enumerated.item].label);
2015a86a88eaSTakashi Iwai 	return 0;
2016a86a88eaSTakashi Iwai }
2017a86a88eaSTakashi Iwai 
2018a86a88eaSTakashi Iwai static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
2019a86a88eaSTakashi Iwai 			    struct snd_ctl_elem_value *ucontrol)
2020a86a88eaSTakashi Iwai {
2021a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2022a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2023a86a88eaSTakashi Iwai 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2024a86a88eaSTakashi Iwai 
2025a86a88eaSTakashi Iwai 	ucontrol->value.enumerated.item[0] = spec->cur_mux[idx];
2026a86a88eaSTakashi Iwai 	return 0;
2027a86a88eaSTakashi Iwai }
2028a86a88eaSTakashi Iwai 
2029a86a88eaSTakashi Iwai static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
2030a86a88eaSTakashi Iwai 			    struct snd_ctl_elem_value *ucontrol)
2031a86a88eaSTakashi Iwai {
2032a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2033a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2034a86a88eaSTakashi Iwai 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2035a86a88eaSTakashi Iwai 	hda_nid_t mux;
2036a86a88eaSTakashi Iwai 	int cur;
2037a86a88eaSTakashi Iwai 
2038a86a88eaSTakashi Iwai 	cur = ucontrol->value.enumerated.item[0];
2039a86a88eaSTakashi Iwai 	if (cur < 0 || cur >= spec->num_inputs)
2040a86a88eaSTakashi Iwai 		return -EINVAL;
2041a86a88eaSTakashi Iwai 	if (spec->cur_mux[idx] == cur)
2042a86a88eaSTakashi Iwai 		return 0;
2043a86a88eaSTakashi Iwai 	spec->cur_mux[idx] = cur;
2044a86a88eaSTakashi Iwai 	if (spec->dyn_adc_switch) {
2045a86a88eaSTakashi Iwai 		int adc_idx = spec->inputs[cur].adc_idx;
2046a86a88eaSTakashi Iwai 		mux = spec->mux_nids[adc_idx];
2047a86a88eaSTakashi Iwai 		via_dyn_adc_pcm_resetup(codec, cur);
2048a86a88eaSTakashi Iwai 	} else {
2049a86a88eaSTakashi Iwai 		mux = spec->mux_nids[idx];
2050a86a88eaSTakashi Iwai 		if (snd_BUG_ON(!mux))
2051a86a88eaSTakashi Iwai 			return -EINVAL;
2052a86a88eaSTakashi Iwai 	}
2053a86a88eaSTakashi Iwai 
2054a86a88eaSTakashi Iwai 	if (mux) {
2055a86a88eaSTakashi Iwai 		/* switch to D0 beofre change index */
2056a86a88eaSTakashi Iwai 		if (snd_hda_codec_read(codec, mux, 0,
2057a86a88eaSTakashi Iwai 			       AC_VERB_GET_POWER_STATE, 0x00) != AC_PWRST_D0)
2058a86a88eaSTakashi Iwai 			snd_hda_codec_write(codec, mux, 0,
2059a86a88eaSTakashi Iwai 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
2060a86a88eaSTakashi Iwai 		snd_hda_codec_write(codec, mux, 0,
2061a86a88eaSTakashi Iwai 				    AC_VERB_SET_CONNECT_SEL,
2062a86a88eaSTakashi Iwai 				    spec->inputs[cur].mux_idx);
2063a86a88eaSTakashi Iwai 	}
2064a86a88eaSTakashi Iwai 
2065a86a88eaSTakashi Iwai 	/* update jack power state */
2066a86a88eaSTakashi Iwai 	set_widgets_power_state(codec);
2067a86a88eaSTakashi Iwai 	return 0;
2068a86a88eaSTakashi Iwai }
2069a766d0d7STakashi Iwai 
2070d7a99cceSTakashi Iwai static const struct snd_kcontrol_new via_input_src_ctl = {
2071d7a99cceSTakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2072d7a99cceSTakashi Iwai 	/* The multiple "Capture Source" controls confuse alsamixer
2073d7a99cceSTakashi Iwai 	 * So call somewhat different..
2074d7a99cceSTakashi Iwai 	 */
2075d7a99cceSTakashi Iwai 	/* .name = "Capture Source", */
2076d7a99cceSTakashi Iwai 	.name = "Input Source",
2077d7a99cceSTakashi Iwai 	.info = via_mux_enum_info,
2078d7a99cceSTakashi Iwai 	.get = via_mux_enum_get,
2079d7a99cceSTakashi Iwai 	.put = via_mux_enum_put,
2080d7a99cceSTakashi Iwai };
2081d7a99cceSTakashi Iwai 
2082a86a88eaSTakashi Iwai static int create_input_src_ctls(struct hda_codec *codec, int count)
2083a86a88eaSTakashi Iwai {
2084a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2085a86a88eaSTakashi Iwai 	struct snd_kcontrol_new *knew;
2086a86a88eaSTakashi Iwai 
2087a86a88eaSTakashi Iwai 	if (spec->num_inputs <= 1 || !count)
2088a86a88eaSTakashi Iwai 		return 0; /* no need for single src */
2089a86a88eaSTakashi Iwai 
2090a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_input_src_ctl);
2091a86a88eaSTakashi Iwai 	if (!knew)
2092a86a88eaSTakashi Iwai 		return -ENOMEM;
2093a86a88eaSTakashi Iwai 	knew->count = count;
2094a86a88eaSTakashi Iwai 	return 0;
2095a86a88eaSTakashi Iwai }
2096a86a88eaSTakashi Iwai 
2097a86a88eaSTakashi Iwai /* add the powersave loopback-list entry */
209813af8e77STakashi Iwai static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
209913af8e77STakashi Iwai {
210013af8e77STakashi Iwai 	struct hda_amp_list *list;
210113af8e77STakashi Iwai 
210213af8e77STakashi Iwai 	if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
210313af8e77STakashi Iwai 		return;
210413af8e77STakashi Iwai 	list = spec->loopback_list + spec->num_loopbacks;
210513af8e77STakashi Iwai 	list->nid = mix;
210613af8e77STakashi Iwai 	list->dir = HDA_INPUT;
210713af8e77STakashi Iwai 	list->idx = idx;
210813af8e77STakashi Iwai 	spec->num_loopbacks++;
210913af8e77STakashi Iwai 	spec->loopback.amplist = spec->loopback_list;
211013af8e77STakashi Iwai }
211113af8e77STakashi Iwai 
2112a86a88eaSTakashi Iwai static bool is_reachable_nid(struct hda_codec *codec, hda_nid_t src,
21138d087c76STakashi Iwai 			     hda_nid_t dst)
2114a86a88eaSTakashi Iwai {
21158d087c76STakashi Iwai 	return snd_hda_get_conn_index(codec, src, dst, 1) >= 0;
2116a86a88eaSTakashi Iwai }
2117a86a88eaSTakashi Iwai 
2118a86a88eaSTakashi Iwai /* add the input-route to the given pin */
2119a86a88eaSTakashi Iwai static bool add_input_route(struct hda_codec *codec, hda_nid_t pin)
2120c577b8a1SJoseph Chan {
212110a20af7STakashi Iwai 	struct via_spec *spec = codec->spec;
2122a86a88eaSTakashi Iwai 	int c, idx;
2123a86a88eaSTakashi Iwai 
2124a86a88eaSTakashi Iwai 	spec->inputs[spec->num_inputs].adc_idx = -1;
2125a86a88eaSTakashi Iwai 	spec->inputs[spec->num_inputs].pin = pin;
2126a86a88eaSTakashi Iwai 	for (c = 0; c < spec->num_adc_nids; c++) {
2127a86a88eaSTakashi Iwai 		if (spec->mux_nids[c]) {
2128a86a88eaSTakashi Iwai 			idx = get_connection_index(codec, spec->mux_nids[c],
2129a86a88eaSTakashi Iwai 						   pin);
2130a86a88eaSTakashi Iwai 			if (idx < 0)
2131a86a88eaSTakashi Iwai 				continue;
2132a86a88eaSTakashi Iwai 			spec->inputs[spec->num_inputs].mux_idx = idx;
2133a86a88eaSTakashi Iwai 		} else {
21348d087c76STakashi Iwai 			if (!is_reachable_nid(codec, spec->adc_nids[c], pin))
2135a86a88eaSTakashi Iwai 				continue;
2136a86a88eaSTakashi Iwai 		}
2137a86a88eaSTakashi Iwai 		spec->inputs[spec->num_inputs].adc_idx = c;
2138a86a88eaSTakashi Iwai 		/* Can primary ADC satisfy all inputs? */
2139a86a88eaSTakashi Iwai 		if (!spec->dyn_adc_switch &&
2140a86a88eaSTakashi Iwai 		    spec->num_inputs > 0 && spec->inputs[0].adc_idx != c) {
2141a86a88eaSTakashi Iwai 			snd_printd(KERN_INFO
2142a86a88eaSTakashi Iwai 				   "via: dynamic ADC switching enabled\n");
2143a86a88eaSTakashi Iwai 			spec->dyn_adc_switch = 1;
2144a86a88eaSTakashi Iwai 		}
2145a86a88eaSTakashi Iwai 		return true;
2146a86a88eaSTakashi Iwai 	}
2147a86a88eaSTakashi Iwai 	return false;
2148a86a88eaSTakashi Iwai }
2149a86a88eaSTakashi Iwai 
2150a86a88eaSTakashi Iwai static int get_mux_nids(struct hda_codec *codec);
2151a86a88eaSTakashi Iwai 
2152a86a88eaSTakashi Iwai /* parse input-routes; fill ADCs, MUXs and input-src entries */
2153a86a88eaSTakashi Iwai static int parse_analog_inputs(struct hda_codec *codec)
2154a86a88eaSTakashi Iwai {
2155a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2156a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2157a86a88eaSTakashi Iwai 	int i, err;
2158a766d0d7STakashi Iwai 
2159a766d0d7STakashi Iwai 	err = via_fill_adcs(codec);
2160a766d0d7STakashi Iwai 	if (err < 0)
2161a766d0d7STakashi Iwai 		return err;
2162a766d0d7STakashi Iwai 	err = get_mux_nids(codec);
2163a766d0d7STakashi Iwai 	if (err < 0)
2164a766d0d7STakashi Iwai 		return err;
2165a766d0d7STakashi Iwai 
2166a86a88eaSTakashi Iwai 	/* fill all input-routes */
2167a86a88eaSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2168a86a88eaSTakashi Iwai 		if (add_input_route(codec, cfg->inputs[i].pin))
2169a86a88eaSTakashi Iwai 			spec->inputs[spec->num_inputs++].label =
2170a86a88eaSTakashi Iwai 				hda_get_autocfg_input_label(codec, cfg, i);
2171a86a88eaSTakashi Iwai 	}
2172a86a88eaSTakashi Iwai 
2173a86a88eaSTakashi Iwai 	/* check for internal loopback recording */
2174a86a88eaSTakashi Iwai 	if (spec->aa_mix_nid &&
2175a86a88eaSTakashi Iwai 	    add_input_route(codec, spec->aa_mix_nid))
2176a86a88eaSTakashi Iwai 		spec->inputs[spec->num_inputs++].label = "Stereo Mixer";
2177a86a88eaSTakashi Iwai 
2178a86a88eaSTakashi Iwai 	return 0;
2179a86a88eaSTakashi Iwai }
2180a86a88eaSTakashi Iwai 
2181a86a88eaSTakashi Iwai /* create analog-loopback volume/switch controls */
2182a86a88eaSTakashi Iwai static int create_loopback_ctls(struct hda_codec *codec)
2183a86a88eaSTakashi Iwai {
2184a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2185a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2186a86a88eaSTakashi Iwai 	const char *prev_label = NULL;
2187a86a88eaSTakashi Iwai 	int type_idx = 0;
2188a86a88eaSTakashi Iwai 	int i, j, err, idx;
2189a86a88eaSTakashi Iwai 
2190a86a88eaSTakashi Iwai 	if (!spec->aa_mix_nid)
2191a766d0d7STakashi Iwai 		return 0;
2192c577b8a1SJoseph Chan 
21937b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2194a86a88eaSTakashi Iwai 		hda_nid_t pin = cfg->inputs[i].pin;
2195a86a88eaSTakashi Iwai 		const char *label = hda_get_autocfg_input_label(codec, cfg, i);
2196a86a88eaSTakashi Iwai 
21971e11cae1STakashi Iwai 		if (prev_label && !strcmp(label, prev_label))
21987b315bb4STakashi Iwai 			type_idx++;
21997b315bb4STakashi Iwai 		else
22007b315bb4STakashi Iwai 			type_idx = 0;
22011e11cae1STakashi Iwai 		prev_label = label;
2202a86a88eaSTakashi Iwai 		idx = get_connection_index(codec, spec->aa_mix_nid, pin);
2203a86a88eaSTakashi Iwai 		if (idx >= 0) {
220416922281SLydia Wang 			err = via_new_analog_input(spec, label, type_idx,
2205a86a88eaSTakashi Iwai 						   idx, spec->aa_mix_nid);
2206c577b8a1SJoseph Chan 			if (err < 0)
2207c577b8a1SJoseph Chan 				return err;
2208a86a88eaSTakashi Iwai 			add_loopback_list(spec, spec->aa_mix_nid, idx);
220913af8e77STakashi Iwai 		}
2210e3d7a143STakashi Iwai 
2211e3d7a143STakashi Iwai 		/* remember the label for smart51 control */
2212e3d7a143STakashi Iwai 		for (j = 0; j < spec->smart51_nums; j++) {
2213a86a88eaSTakashi Iwai 			if (spec->smart51_pins[j] == pin) {
2214e3d7a143STakashi Iwai 				spec->smart51_idxs[j] = idx;
2215e3d7a143STakashi Iwai 				spec->smart51_labels[j] = label;
2216e3d7a143STakashi Iwai 				break;
2217e3d7a143STakashi Iwai 			}
2218e3d7a143STakashi Iwai 		}
2219c577b8a1SJoseph Chan 	}
2220a86a88eaSTakashi Iwai 	return 0;
2221a86a88eaSTakashi Iwai }
2222a86a88eaSTakashi Iwai 
2223a86a88eaSTakashi Iwai /* create mic-boost controls (if present) */
2224a86a88eaSTakashi Iwai static int create_mic_boost_ctls(struct hda_codec *codec)
2225a86a88eaSTakashi Iwai {
2226a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2227a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2228a86a88eaSTakashi Iwai 	int i, err;
2229a86a88eaSTakashi Iwai 
2230a86a88eaSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2231a86a88eaSTakashi Iwai 		hda_nid_t pin = cfg->inputs[i].pin;
2232a86a88eaSTakashi Iwai 		unsigned int caps;
2233a86a88eaSTakashi Iwai 		const char *label;
2234a86a88eaSTakashi Iwai 		char name[32];
2235a86a88eaSTakashi Iwai 
2236a86a88eaSTakashi Iwai 		if (cfg->inputs[i].type != AUTO_PIN_MIC)
2237a86a88eaSTakashi Iwai 			continue;
2238a86a88eaSTakashi Iwai 		caps = query_amp_caps(codec, pin, HDA_INPUT);
2239a86a88eaSTakashi Iwai 		if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
2240a86a88eaSTakashi Iwai 			continue;
2241a86a88eaSTakashi Iwai 		label = hda_get_autocfg_input_label(codec, cfg, i);
2242a86a88eaSTakashi Iwai 		snprintf(name, sizeof(name), "%s Boost Volume", label);
2243a86a88eaSTakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2244a86a88eaSTakashi Iwai 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
2245a86a88eaSTakashi Iwai 		if (err < 0)
2246a86a88eaSTakashi Iwai 			return err;
2247a86a88eaSTakashi Iwai 	}
2248a86a88eaSTakashi Iwai 	return 0;
2249a86a88eaSTakashi Iwai }
2250a86a88eaSTakashi Iwai 
2251a86a88eaSTakashi Iwai /* create capture and input-src controls for multiple streams */
2252a86a88eaSTakashi Iwai static int create_multi_adc_ctls(struct hda_codec *codec)
2253a86a88eaSTakashi Iwai {
2254a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2255a86a88eaSTakashi Iwai 	int i, err;
2256d7a99cceSTakashi Iwai 
2257d7a99cceSTakashi Iwai 	/* create capture mixer elements */
2258d7a99cceSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
2259d7a99cceSTakashi Iwai 		hda_nid_t adc = spec->adc_nids[i];
2260d7a99cceSTakashi Iwai 		err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
2261d7a99cceSTakashi Iwai 					"Capture Volume", i,
2262d7a99cceSTakashi Iwai 					HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2263d7a99cceSTakashi Iwai 							    HDA_INPUT));
2264d7a99cceSTakashi Iwai 		if (err < 0)
2265d7a99cceSTakashi Iwai 			return err;
2266d7a99cceSTakashi Iwai 		err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2267d7a99cceSTakashi Iwai 					"Capture Switch", i,
2268d7a99cceSTakashi Iwai 					HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2269d7a99cceSTakashi Iwai 							    HDA_INPUT));
2270d7a99cceSTakashi Iwai 		if (err < 0)
2271d7a99cceSTakashi Iwai 			return err;
2272d7a99cceSTakashi Iwai 	}
2273d7a99cceSTakashi Iwai 
2274d7a99cceSTakashi Iwai 	/* input-source control */
2275d7a99cceSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++)
2276d7a99cceSTakashi Iwai 		if (!spec->mux_nids[i])
2277d7a99cceSTakashi Iwai 			break;
2278a86a88eaSTakashi Iwai 	err = create_input_src_ctls(codec, i);
2279d7a99cceSTakashi Iwai 	if (err < 0)
2280d7a99cceSTakashi Iwai 		return err;
2281a86a88eaSTakashi Iwai 	return 0;
2282d7a99cceSTakashi Iwai }
2283d7a99cceSTakashi Iwai 
2284a86a88eaSTakashi Iwai /* bind capture volume/switch */
2285a86a88eaSTakashi Iwai static struct snd_kcontrol_new via_bind_cap_vol_ctl =
2286a86a88eaSTakashi Iwai 	HDA_BIND_VOL("Capture Volume", 0);
2287a86a88eaSTakashi Iwai static struct snd_kcontrol_new via_bind_cap_sw_ctl =
2288a86a88eaSTakashi Iwai 	HDA_BIND_SW("Capture Switch", 0);
2289a86a88eaSTakashi Iwai 
2290a86a88eaSTakashi Iwai static int init_bind_ctl(struct via_spec *spec, struct hda_bind_ctls **ctl_ret,
2291a86a88eaSTakashi Iwai 			 struct hda_ctl_ops *ops)
2292a86a88eaSTakashi Iwai {
2293a86a88eaSTakashi Iwai 	struct hda_bind_ctls *ctl;
2294a86a88eaSTakashi Iwai 	int i;
2295a86a88eaSTakashi Iwai 
2296a86a88eaSTakashi Iwai 	ctl = kzalloc(sizeof(*ctl) + sizeof(long) * 4, GFP_KERNEL);
2297a86a88eaSTakashi Iwai 	if (!ctl)
2298a86a88eaSTakashi Iwai 		return -ENOMEM;
2299a86a88eaSTakashi Iwai 	ctl->ops = ops;
2300a86a88eaSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++)
2301a86a88eaSTakashi Iwai 		ctl->values[i] =
2302a86a88eaSTakashi Iwai 			HDA_COMPOSE_AMP_VAL(spec->adc_nids[i], 3, 0, HDA_INPUT);
2303a86a88eaSTakashi Iwai 	*ctl_ret = ctl;
2304a86a88eaSTakashi Iwai 	return 0;
2305a86a88eaSTakashi Iwai }
2306a86a88eaSTakashi Iwai 
2307a86a88eaSTakashi Iwai /* create capture and input-src controls for dynamic ADC-switch case */
2308a86a88eaSTakashi Iwai static int create_dyn_adc_ctls(struct hda_codec *codec)
2309a86a88eaSTakashi Iwai {
2310a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2311a86a88eaSTakashi Iwai 	struct snd_kcontrol_new *knew;
2312a86a88eaSTakashi Iwai 	int err;
2313a86a88eaSTakashi Iwai 
2314a86a88eaSTakashi Iwai 	/* set up the bind capture ctls */
2315a86a88eaSTakashi Iwai 	err = init_bind_ctl(spec, &spec->bind_cap_vol, &snd_hda_bind_vol);
2316a86a88eaSTakashi Iwai 	if (err < 0)
2317a86a88eaSTakashi Iwai 		return err;
2318a86a88eaSTakashi Iwai 	err = init_bind_ctl(spec, &spec->bind_cap_sw, &snd_hda_bind_sw);
2319a86a88eaSTakashi Iwai 	if (err < 0)
2320a86a88eaSTakashi Iwai 		return err;
2321a86a88eaSTakashi Iwai 
2322a86a88eaSTakashi Iwai 	/* create capture mixer elements */
2323a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_bind_cap_vol_ctl);
2324a86a88eaSTakashi Iwai 	if (!knew)
2325a86a88eaSTakashi Iwai 		return -ENOMEM;
2326a86a88eaSTakashi Iwai 	knew->private_value = (long)spec->bind_cap_vol;
2327a86a88eaSTakashi Iwai 
2328a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_bind_cap_sw_ctl);
2329a86a88eaSTakashi Iwai 	if (!knew)
2330a86a88eaSTakashi Iwai 		return -ENOMEM;
2331a86a88eaSTakashi Iwai 	knew->private_value = (long)spec->bind_cap_sw;
2332a86a88eaSTakashi Iwai 
2333a86a88eaSTakashi Iwai 	/* input-source control */
2334a86a88eaSTakashi Iwai 	err = create_input_src_ctls(codec, 1);
2335a86a88eaSTakashi Iwai 	if (err < 0)
2336a86a88eaSTakashi Iwai 		return err;
2337a86a88eaSTakashi Iwai 	return 0;
2338a86a88eaSTakashi Iwai }
2339a86a88eaSTakashi Iwai 
2340a86a88eaSTakashi Iwai /* parse and create capture-related stuff */
2341a86a88eaSTakashi Iwai static int via_auto_create_analog_input_ctls(struct hda_codec *codec)
2342a86a88eaSTakashi Iwai {
2343a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2344a86a88eaSTakashi Iwai 	int err;
2345a86a88eaSTakashi Iwai 
2346a86a88eaSTakashi Iwai 	err = parse_analog_inputs(codec);
2347a86a88eaSTakashi Iwai 	if (err < 0)
2348a86a88eaSTakashi Iwai 		return err;
2349a86a88eaSTakashi Iwai 	if (spec->dyn_adc_switch)
2350a86a88eaSTakashi Iwai 		err = create_dyn_adc_ctls(codec);
2351a86a88eaSTakashi Iwai 	else
2352a86a88eaSTakashi Iwai 		err = create_multi_adc_ctls(codec);
2353a86a88eaSTakashi Iwai 	if (err < 0)
2354a86a88eaSTakashi Iwai 		return err;
2355a86a88eaSTakashi Iwai 	err = create_loopback_ctls(codec);
2356a86a88eaSTakashi Iwai 	if (err < 0)
2357a86a88eaSTakashi Iwai 		return err;
2358a86a88eaSTakashi Iwai 	err = create_mic_boost_ctls(codec);
2359a86a88eaSTakashi Iwai 	if (err < 0)
2360a86a88eaSTakashi Iwai 		return err;
2361c577b8a1SJoseph Chan 	return 0;
2362c577b8a1SJoseph Chan }
2363c577b8a1SJoseph Chan 
236476d9b0ddSHarald Welte static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
236576d9b0ddSHarald Welte {
236676d9b0ddSHarald Welte 	unsigned int def_conf;
236776d9b0ddSHarald Welte 	unsigned char seqassoc;
236876d9b0ddSHarald Welte 
23692f334f92STakashi Iwai 	def_conf = snd_hda_codec_get_pincfg(codec, nid);
237076d9b0ddSHarald Welte 	seqassoc = (unsigned char) get_defcfg_association(def_conf);
237176d9b0ddSHarald Welte 	seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
237282ef9e45SLydia Wang 	if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
237382ef9e45SLydia Wang 	    && (seqassoc == 0xf0 || seqassoc == 0xff)) {
237476d9b0ddSHarald Welte 		def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
23752f334f92STakashi Iwai 		snd_hda_codec_set_pincfg(codec, nid, def_conf);
237676d9b0ddSHarald Welte 	}
237776d9b0ddSHarald Welte 
237876d9b0ddSHarald Welte 	return;
237976d9b0ddSHarald Welte }
238076d9b0ddSHarald Welte 
2381e06e5a29STakashi Iwai static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
23821f2e99feSLydia Wang 				     struct snd_ctl_elem_value *ucontrol)
23831f2e99feSLydia Wang {
23841f2e99feSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
23851f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
23861f2e99feSLydia Wang 
23871f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
23881f2e99feSLydia Wang 		return 0;
2389e06e5a29STakashi Iwai 	spec->vt1708_jack_detect =
23901f2e99feSLydia Wang 		!((snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8) & 0x1);
2391e06e5a29STakashi Iwai 	ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
23921f2e99feSLydia Wang 	return 0;
23931f2e99feSLydia Wang }
23941f2e99feSLydia Wang 
2395e06e5a29STakashi Iwai static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
23961f2e99feSLydia Wang 				     struct snd_ctl_elem_value *ucontrol)
23971f2e99feSLydia Wang {
23981f2e99feSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
23991f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
24001f2e99feSLydia Wang 	int change;
24011f2e99feSLydia Wang 
24021f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
24031f2e99feSLydia Wang 		return 0;
2404e06e5a29STakashi Iwai 	spec->vt1708_jack_detect = ucontrol->value.integer.value[0];
24051f2e99feSLydia Wang 	change = (0x1 & (snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8))
2406e06e5a29STakashi Iwai 		== !spec->vt1708_jack_detect;
2407e06e5a29STakashi Iwai 	if (spec->vt1708_jack_detect) {
24081f2e99feSLydia Wang 		mute_aa_path(codec, 1);
24091f2e99feSLydia Wang 		notify_aa_path_ctls(codec);
24101f2e99feSLydia Wang 	}
24111f2e99feSLydia Wang 	return change;
24121f2e99feSLydia Wang }
24131f2e99feSLydia Wang 
2414e06e5a29STakashi Iwai static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
24151f2e99feSLydia Wang 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
24161f2e99feSLydia Wang 	.name = "Jack Detect",
24171f2e99feSLydia Wang 	.count = 1,
24181f2e99feSLydia Wang 	.info = snd_ctl_boolean_mono_info,
2419e06e5a29STakashi Iwai 	.get = vt1708_jack_detect_get,
2420e06e5a29STakashi Iwai 	.put = vt1708_jack_detect_put,
24211f2e99feSLydia Wang };
24221f2e99feSLydia Wang 
242312daef65STakashi Iwai static void fill_dig_outs(struct hda_codec *codec);
242412daef65STakashi Iwai static void fill_dig_in(struct hda_codec *codec);
242512daef65STakashi Iwai 
242612daef65STakashi Iwai static int via_parse_auto_config(struct hda_codec *codec)
2427c577b8a1SJoseph Chan {
2428c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
2429c577b8a1SJoseph Chan 	int err;
2430c577b8a1SJoseph Chan 
2431c577b8a1SJoseph Chan 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2432c577b8a1SJoseph Chan 	if (err < 0)
2433c577b8a1SJoseph Chan 		return err;
2434c577b8a1SJoseph Chan 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
24357f0df88cSTakashi Iwai 		return -EINVAL;
2436c577b8a1SJoseph Chan 
24374a79616dSTakashi Iwai 	err = via_auto_create_multi_out_ctls(codec);
2438c577b8a1SJoseph Chan 	if (err < 0)
2439c577b8a1SJoseph Chan 		return err;
24404a79616dSTakashi Iwai 	err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
2441c577b8a1SJoseph Chan 	if (err < 0)
2442c577b8a1SJoseph Chan 		return err;
24434a918ffeSTakashi Iwai 	err = via_auto_create_speaker_ctls(codec);
24444a918ffeSTakashi Iwai 	if (err < 0)
24454a918ffeSTakashi Iwai 		return err;
2446a86a88eaSTakashi Iwai 	err = via_auto_create_analog_input_ctls(codec);
2447c577b8a1SJoseph Chan 	if (err < 0)
2448c577b8a1SJoseph Chan 		return err;
2449c577b8a1SJoseph Chan 
2450c577b8a1SJoseph Chan 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2451c577b8a1SJoseph Chan 
245212daef65STakashi Iwai 	fill_dig_outs(codec);
245312daef65STakashi Iwai 	fill_dig_in(codec);
2454c577b8a1SJoseph Chan 
2455603c4019STakashi Iwai 	if (spec->kctls.list)
2456603c4019STakashi Iwai 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
2457c577b8a1SJoseph Chan 
2458c577b8a1SJoseph Chan 
24598df2a312STakashi Iwai 	if (spec->hp_dac_nid && spec->hp_dep_path.depth) {
2460ece8d043STakashi Iwai 		err = via_hp_build(codec);
2461ece8d043STakashi Iwai 		if (err < 0)
2462ece8d043STakashi Iwai 			return err;
2463ece8d043STakashi Iwai 	}
2464c577b8a1SJoseph Chan 
2465f4a7828bSTakashi Iwai 	err = via_smart51_build(codec);
2466f4a7828bSTakashi Iwai 	if (err < 0)
2467f4a7828bSTakashi Iwai 		return err;
2468f4a7828bSTakashi Iwai 
24695d41762aSTakashi Iwai 	/* assign slave outs */
24705d41762aSTakashi Iwai 	if (spec->slave_dig_outs[0])
24715d41762aSTakashi Iwai 		codec->slave_dig_outs = spec->slave_dig_outs;
24725d41762aSTakashi Iwai 
2473c577b8a1SJoseph Chan 	return 1;
2474c577b8a1SJoseph Chan }
2475c577b8a1SJoseph Chan 
24765d41762aSTakashi Iwai static void via_auto_init_dig_outs(struct hda_codec *codec)
2477c577b8a1SJoseph Chan {
247825eaba2fSLydia Wang 	struct via_spec *spec = codec->spec;
24795d41762aSTakashi Iwai 	if (spec->multiout.dig_out_nid)
24805d41762aSTakashi Iwai 		init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
24815d41762aSTakashi Iwai 	if (spec->slave_dig_outs[0])
24825d41762aSTakashi Iwai 		init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
24835d41762aSTakashi Iwai }
248425eaba2fSLydia Wang 
24855d41762aSTakashi Iwai static void via_auto_init_dig_in(struct hda_codec *codec)
24865d41762aSTakashi Iwai {
24875d41762aSTakashi Iwai 	struct via_spec *spec = codec->spec;
24885d41762aSTakashi Iwai 	if (!spec->dig_in_nid)
24895d41762aSTakashi Iwai 		return;
24905d41762aSTakashi Iwai 	snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
24915d41762aSTakashi Iwai 			    AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
24925d41762aSTakashi Iwai }
24935d41762aSTakashi Iwai 
24944a918ffeSTakashi Iwai /* initialize the unsolicited events */
24954a918ffeSTakashi Iwai static void via_auto_init_unsol_event(struct hda_codec *codec)
24964a918ffeSTakashi Iwai {
24974a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
24984a918ffeSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
24994a918ffeSTakashi Iwai 	unsigned int ev;
25004a918ffeSTakashi Iwai 	int i;
25014a918ffeSTakashi Iwai 
25024a918ffeSTakashi Iwai 	if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
25034a918ffeSTakashi Iwai 		snd_hda_codec_write(codec, cfg->hp_pins[0], 0,
25044a918ffeSTakashi Iwai 				AC_VERB_SET_UNSOLICITED_ENABLE,
25054a918ffeSTakashi Iwai 				AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT);
25064a918ffeSTakashi Iwai 
25074a918ffeSTakashi Iwai 	if (cfg->speaker_pins[0])
25084a918ffeSTakashi Iwai 		ev = VIA_LINE_EVENT;
25094a918ffeSTakashi Iwai 	else
25104a918ffeSTakashi Iwai 		ev = 0;
25114a918ffeSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
25124a918ffeSTakashi Iwai 		if (cfg->line_out_pins[i] &&
25134a918ffeSTakashi Iwai 		    is_jack_detectable(codec, cfg->line_out_pins[i]))
251463f10d2cSLydia Wang 			snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
25154a918ffeSTakashi Iwai 				AC_VERB_SET_UNSOLICITED_ENABLE,
25164a918ffeSTakashi Iwai 				AC_USRSP_EN | ev | VIA_JACK_EVENT);
25174a918ffeSTakashi Iwai 	}
25184a918ffeSTakashi Iwai 
25194a918ffeSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
25204a918ffeSTakashi Iwai 		if (is_jack_detectable(codec, cfg->inputs[i].pin))
25214a918ffeSTakashi Iwai 			snd_hda_codec_write(codec, cfg->inputs[i].pin, 0,
25224a918ffeSTakashi Iwai 				AC_VERB_SET_UNSOLICITED_ENABLE,
25234a918ffeSTakashi Iwai 				AC_USRSP_EN | VIA_JACK_EVENT);
25244a918ffeSTakashi Iwai 	}
25254a918ffeSTakashi Iwai }
25264a918ffeSTakashi Iwai 
25275d41762aSTakashi Iwai static int via_init(struct hda_codec *codec)
25285d41762aSTakashi Iwai {
25295d41762aSTakashi Iwai 	struct via_spec *spec = codec->spec;
25305d41762aSTakashi Iwai 	int i;
25315d41762aSTakashi Iwai 
25325d41762aSTakashi Iwai 	for (i = 0; i < spec->num_iverbs; i++)
25335d41762aSTakashi Iwai 		snd_hda_sequence_write(codec, spec->init_verbs[i]);
25345d41762aSTakashi Iwai 
2535c577b8a1SJoseph Chan 	via_auto_init_multi_out(codec);
2536c577b8a1SJoseph Chan 	via_auto_init_hp_out(codec);
25374a918ffeSTakashi Iwai 	via_auto_init_speaker_out(codec);
2538c577b8a1SJoseph Chan 	via_auto_init_analog_input(codec);
25395d41762aSTakashi Iwai 	via_auto_init_dig_outs(codec);
25405d41762aSTakashi Iwai 	via_auto_init_dig_in(codec);
254111890956SLydia Wang 
25424a918ffeSTakashi Iwai 	via_auto_init_unsol_event(codec);
25434a918ffeSTakashi Iwai 
254425eaba2fSLydia Wang 	via_hp_automute(codec);
254525eaba2fSLydia Wang 
2546c577b8a1SJoseph Chan 	return 0;
2547c577b8a1SJoseph Chan }
2548c577b8a1SJoseph Chan 
25491f2e99feSLydia Wang static void vt1708_update_hp_jack_state(struct work_struct *work)
25501f2e99feSLydia Wang {
25511f2e99feSLydia Wang 	struct via_spec *spec = container_of(work, struct via_spec,
25521f2e99feSLydia Wang 					     vt1708_hp_work.work);
25531f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
25541f2e99feSLydia Wang 		return;
25551f2e99feSLydia Wang 	/* if jack state toggled */
25561f2e99feSLydia Wang 	if (spec->vt1708_hp_present
2557d56757abSTakashi Iwai 	    != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
25581f2e99feSLydia Wang 		spec->vt1708_hp_present ^= 1;
25591f2e99feSLydia Wang 		via_hp_automute(spec->codec);
25601f2e99feSLydia Wang 	}
25611f2e99feSLydia Wang 	vt1708_start_hp_work(spec);
25621f2e99feSLydia Wang }
25631f2e99feSLydia Wang 
2564337b9d02STakashi Iwai static int get_mux_nids(struct hda_codec *codec)
2565337b9d02STakashi Iwai {
2566337b9d02STakashi Iwai 	struct via_spec *spec = codec->spec;
2567337b9d02STakashi Iwai 	hda_nid_t nid, conn[8];
2568337b9d02STakashi Iwai 	unsigned int type;
2569337b9d02STakashi Iwai 	int i, n;
2570337b9d02STakashi Iwai 
2571337b9d02STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
2572337b9d02STakashi Iwai 		nid = spec->adc_nids[i];
2573337b9d02STakashi Iwai 		while (nid) {
2574a22d543aSTakashi Iwai 			type = get_wcaps_type(get_wcaps(codec, nid));
25751c55d521STakashi Iwai 			if (type == AC_WID_PIN)
25761c55d521STakashi Iwai 				break;
2577337b9d02STakashi Iwai 			n = snd_hda_get_connections(codec, nid, conn,
2578337b9d02STakashi Iwai 						    ARRAY_SIZE(conn));
2579337b9d02STakashi Iwai 			if (n <= 0)
2580337b9d02STakashi Iwai 				break;
2581337b9d02STakashi Iwai 			if (n > 1) {
2582337b9d02STakashi Iwai 				spec->mux_nids[i] = nid;
2583337b9d02STakashi Iwai 				break;
2584337b9d02STakashi Iwai 			}
2585337b9d02STakashi Iwai 			nid = conn[0];
2586337b9d02STakashi Iwai 		}
2587337b9d02STakashi Iwai 	}
25881c55d521STakashi Iwai 	return 0;
2589337b9d02STakashi Iwai }
2590337b9d02STakashi Iwai 
2591c577b8a1SJoseph Chan static int patch_vt1708(struct hda_codec *codec)
2592c577b8a1SJoseph Chan {
2593c577b8a1SJoseph Chan 	struct via_spec *spec;
2594c577b8a1SJoseph Chan 	int err;
2595c577b8a1SJoseph Chan 
2596c577b8a1SJoseph Chan 	/* create a codec specific record */
25975b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2598c577b8a1SJoseph Chan 	if (spec == NULL)
2599c577b8a1SJoseph Chan 		return -ENOMEM;
2600c577b8a1SJoseph Chan 
2601620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x17;
2602620e2b28STakashi Iwai 
260312daef65STakashi Iwai 	/* Add HP and CD pin config connect bit re-config action */
260412daef65STakashi Iwai 	vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
260512daef65STakashi Iwai 	vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
260612daef65STakashi Iwai 
2607c577b8a1SJoseph Chan 	/* automatic parse from the BIOS config */
260812daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2609c577b8a1SJoseph Chan 	if (err < 0) {
2610c577b8a1SJoseph Chan 		via_free(codec);
2611c577b8a1SJoseph Chan 		return err;
2612c577b8a1SJoseph Chan 	}
2613c577b8a1SJoseph Chan 
261412daef65STakashi Iwai 	/* add jack detect on/off control */
261512daef65STakashi Iwai 	if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
261612daef65STakashi Iwai 		return -ENOMEM;
261712daef65STakashi Iwai 
2618bc9b5623STakashi Iwai 	/* disable 32bit format on VT1708 */
2619bc9b5623STakashi Iwai 	if (codec->vendor_id == 0x11061708)
2620bc9b5623STakashi Iwai 		spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
2621c577b8a1SJoseph Chan 
2622e322a36dSLydia Wang 	spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
2623e322a36dSLydia Wang 
2624c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
2625c577b8a1SJoseph Chan 
26261f2e99feSLydia Wang 	INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
2627c577b8a1SJoseph Chan 	return 0;
2628c577b8a1SJoseph Chan }
2629c577b8a1SJoseph Chan 
2630ddd304d8STakashi Iwai static int patch_vt1709(struct hda_codec *codec)
2631c577b8a1SJoseph Chan {
2632c577b8a1SJoseph Chan 	struct via_spec *spec;
2633c577b8a1SJoseph Chan 	int err;
2634c577b8a1SJoseph Chan 
2635c577b8a1SJoseph Chan 	/* create a codec specific record */
26365b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2637c577b8a1SJoseph Chan 	if (spec == NULL)
2638c577b8a1SJoseph Chan 		return -ENOMEM;
2639c577b8a1SJoseph Chan 
2640620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x18;
2641620e2b28STakashi Iwai 
264212daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2643c577b8a1SJoseph Chan 	if (err < 0) {
2644c577b8a1SJoseph Chan 		via_free(codec);
2645c577b8a1SJoseph Chan 		return err;
2646c577b8a1SJoseph Chan 	}
2647c577b8a1SJoseph Chan 
2648c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
2649c577b8a1SJoseph Chan 
2650f7278fd0SJosepch Chan 	return 0;
2651f7278fd0SJosepch Chan }
2652f7278fd0SJosepch Chan 
26533e95b9abSLydia Wang static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
26543e95b9abSLydia Wang {
26553e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
26563e95b9abSLydia Wang 	int imux_is_smixer;
26573e95b9abSLydia Wang 	unsigned int parm;
26583e95b9abSLydia Wang 	int is_8ch = 0;
2659bc92df7fSLydia Wang 	if ((spec->codec_type != VT1708B_4CH) &&
2660bc92df7fSLydia Wang 	    (codec->vendor_id != 0x11064397))
26613e95b9abSLydia Wang 		is_8ch = 1;
26623e95b9abSLydia Wang 
26633e95b9abSLydia Wang 	/* SW0 (17h) = stereo mixer */
26643e95b9abSLydia Wang 	imux_is_smixer =
26653e95b9abSLydia Wang 	(snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
26663e95b9abSLydia Wang 	 == ((spec->codec_type == VT1708S) ? 5 : 0));
26673e95b9abSLydia Wang 	/* inputs */
26683e95b9abSLydia Wang 	/* PW 1/2/5 (1ah/1bh/1eh) */
26693e95b9abSLydia Wang 	parm = AC_PWRST_D3;
26703e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1a, &parm);
26713e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1b, &parm);
26723e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
26733e95b9abSLydia Wang 	if (imux_is_smixer)
26743e95b9abSLydia Wang 		parm = AC_PWRST_D0;
26753e95b9abSLydia Wang 	/* SW0 (17h), AIW 0/1 (13h/14h) */
26763e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
26773e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
26783e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
26793e95b9abSLydia Wang 
26803e95b9abSLydia Wang 	/* outputs */
26813e95b9abSLydia Wang 	/* PW0 (19h), SW1 (18h), AOW1 (11h) */
26823e95b9abSLydia Wang 	parm = AC_PWRST_D3;
26833e95b9abSLydia Wang 	set_pin_power_state(codec, 0x19, &parm);
26843e95b9abSLydia Wang 	if (spec->smart51_enabled)
26853e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1b, &parm);
26863e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
26873e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
26883e95b9abSLydia Wang 
26893e95b9abSLydia Wang 	/* PW6 (22h), SW2 (26h), AOW2 (24h) */
26903e95b9abSLydia Wang 	if (is_8ch) {
26913e95b9abSLydia Wang 		parm = AC_PWRST_D3;
26923e95b9abSLydia Wang 		set_pin_power_state(codec, 0x22, &parm);
26933e95b9abSLydia Wang 		if (spec->smart51_enabled)
26943e95b9abSLydia Wang 			set_pin_power_state(codec, 0x1a, &parm);
26953e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x26, 0,
26963e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
26973e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x24, 0,
26983e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
2699bc92df7fSLydia Wang 	} else if (codec->vendor_id == 0x11064397) {
2700bc92df7fSLydia Wang 		/* PW7(23h), SW2(27h), AOW2(25h) */
2701bc92df7fSLydia Wang 		parm = AC_PWRST_D3;
2702bc92df7fSLydia Wang 		set_pin_power_state(codec, 0x23, &parm);
2703bc92df7fSLydia Wang 		if (spec->smart51_enabled)
2704bc92df7fSLydia Wang 			set_pin_power_state(codec, 0x1a, &parm);
2705bc92df7fSLydia Wang 		snd_hda_codec_write(codec, 0x27, 0,
2706bc92df7fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
2707bc92df7fSLydia Wang 		snd_hda_codec_write(codec, 0x25, 0,
2708bc92df7fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
27093e95b9abSLydia Wang 	}
27103e95b9abSLydia Wang 
27113e95b9abSLydia Wang 	/* PW 3/4/7 (1ch/1dh/23h) */
27123e95b9abSLydia Wang 	parm = AC_PWRST_D3;
27133e95b9abSLydia Wang 	/* force to D0 for internal Speaker */
27143e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1c, &parm);
27153e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1d, &parm);
27163e95b9abSLydia Wang 	if (is_8ch)
27173e95b9abSLydia Wang 		set_pin_power_state(codec, 0x23, &parm);
27183e95b9abSLydia Wang 
27193e95b9abSLydia Wang 	/* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
27203e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
27213e95b9abSLydia Wang 			    imux_is_smixer ? AC_PWRST_D0 : parm);
27223e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
27233e95b9abSLydia Wang 	if (is_8ch) {
27243e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x25, 0,
27253e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
27263e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x27, 0,
27273e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
2728bc92df7fSLydia Wang 	} else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
2729bc92df7fSLydia Wang 		snd_hda_codec_write(codec, 0x25, 0,
2730bc92df7fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
27313e95b9abSLydia Wang }
27323e95b9abSLydia Wang 
2733518bf3baSLydia Wang static int patch_vt1708S(struct hda_codec *codec);
2734ddd304d8STakashi Iwai static int patch_vt1708B(struct hda_codec *codec)
2735f7278fd0SJosepch Chan {
2736f7278fd0SJosepch Chan 	struct via_spec *spec;
2737f7278fd0SJosepch Chan 	int err;
2738f7278fd0SJosepch Chan 
2739518bf3baSLydia Wang 	if (get_codec_type(codec) == VT1708BCE)
2740518bf3baSLydia Wang 		return patch_vt1708S(codec);
2741ddd304d8STakashi Iwai 
2742f7278fd0SJosepch Chan 	/* create a codec specific record */
27435b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2744f7278fd0SJosepch Chan 	if (spec == NULL)
2745f7278fd0SJosepch Chan 		return -ENOMEM;
2746f7278fd0SJosepch Chan 
2747620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
2748620e2b28STakashi Iwai 
2749f7278fd0SJosepch Chan 	/* automatic parse from the BIOS config */
275012daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2751f7278fd0SJosepch Chan 	if (err < 0) {
2752f7278fd0SJosepch Chan 		via_free(codec);
2753f7278fd0SJosepch Chan 		return err;
2754f7278fd0SJosepch Chan 	}
2755f7278fd0SJosepch Chan 
2756f7278fd0SJosepch Chan 	codec->patch_ops = via_patch_ops;
2757f7278fd0SJosepch Chan 
27583e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
27593e95b9abSLydia Wang 
2760f7278fd0SJosepch Chan 	return 0;
2761f7278fd0SJosepch Chan }
2762f7278fd0SJosepch Chan 
2763d949cac1SHarald Welte /* Patch for VT1708S */
2764096a8854STakashi Iwai static const struct hda_verb vt1708S_init_verbs[] = {
2765d7426329SHarald Welte 	/* Enable Mic Boost Volume backdoor */
2766d7426329SHarald Welte 	{0x1, 0xf98, 0x1},
2767bc7e7e5cSLydia Wang 	/* don't bybass mixer */
2768bc7e7e5cSLydia Wang 	{0x1, 0xf88, 0xc0},
2769d949cac1SHarald Welte 	{ }
2770d949cac1SHarald Welte };
2771d949cac1SHarald Welte 
27729da29271STakashi Iwai /* fill out digital output widgets; one for master and one for slave outputs */
27739da29271STakashi Iwai static void fill_dig_outs(struct hda_codec *codec)
27749da29271STakashi Iwai {
27759da29271STakashi Iwai 	struct via_spec *spec = codec->spec;
27769da29271STakashi Iwai 	int i;
27779da29271STakashi Iwai 
27789da29271STakashi Iwai 	for (i = 0; i < spec->autocfg.dig_outs; i++) {
27799da29271STakashi Iwai 		hda_nid_t nid;
27809da29271STakashi Iwai 		int conn;
27819da29271STakashi Iwai 
27829da29271STakashi Iwai 		nid = spec->autocfg.dig_out_pins[i];
27839da29271STakashi Iwai 		if (!nid)
27849da29271STakashi Iwai 			continue;
27859da29271STakashi Iwai 		conn = snd_hda_get_connections(codec, nid, &nid, 1);
27869da29271STakashi Iwai 		if (conn < 1)
27879da29271STakashi Iwai 			continue;
27889da29271STakashi Iwai 		if (!spec->multiout.dig_out_nid)
27899da29271STakashi Iwai 			spec->multiout.dig_out_nid = nid;
27909da29271STakashi Iwai 		else {
27919da29271STakashi Iwai 			spec->slave_dig_outs[0] = nid;
27929da29271STakashi Iwai 			break; /* at most two dig outs */
27939da29271STakashi Iwai 		}
27949da29271STakashi Iwai 	}
27959da29271STakashi Iwai }
27969da29271STakashi Iwai 
279712daef65STakashi Iwai static void fill_dig_in(struct hda_codec *codec)
2798d949cac1SHarald Welte {
2799d949cac1SHarald Welte 	struct via_spec *spec = codec->spec;
280012daef65STakashi Iwai 	hda_nid_t dig_nid;
280112daef65STakashi Iwai 	int i, err;
2802d949cac1SHarald Welte 
280312daef65STakashi Iwai 	if (!spec->autocfg.dig_in_pin)
280412daef65STakashi Iwai 		return;
2805d949cac1SHarald Welte 
280612daef65STakashi Iwai 	dig_nid = codec->start_nid;
280712daef65STakashi Iwai 	for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
280812daef65STakashi Iwai 		unsigned int wcaps = get_wcaps(codec, dig_nid);
280912daef65STakashi Iwai 		if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
281012daef65STakashi Iwai 			continue;
281112daef65STakashi Iwai 		if (!(wcaps & AC_WCAP_DIGITAL))
281212daef65STakashi Iwai 			continue;
281312daef65STakashi Iwai 		if (!(wcaps & AC_WCAP_CONN_LIST))
281412daef65STakashi Iwai 			continue;
281512daef65STakashi Iwai 		err = get_connection_index(codec, dig_nid,
281612daef65STakashi Iwai 					   spec->autocfg.dig_in_pin);
281712daef65STakashi Iwai 		if (err >= 0) {
281812daef65STakashi Iwai 			spec->dig_in_nid = dig_nid;
281912daef65STakashi Iwai 			break;
282012daef65STakashi Iwai 		}
282112daef65STakashi Iwai 	}
2822d949cac1SHarald Welte }
2823d949cac1SHarald Welte 
28246369bcfcSLydia Wang static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
28256369bcfcSLydia Wang 			       int offset, int num_steps, int step_size)
28266369bcfcSLydia Wang {
28276369bcfcSLydia Wang 	snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
28286369bcfcSLydia Wang 				  (offset << AC_AMPCAP_OFFSET_SHIFT) |
28296369bcfcSLydia Wang 				  (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
28306369bcfcSLydia Wang 				  (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
28316369bcfcSLydia Wang 				  (0 << AC_AMPCAP_MUTE_SHIFT));
28326369bcfcSLydia Wang }
28336369bcfcSLydia Wang 
2834d949cac1SHarald Welte static int patch_vt1708S(struct hda_codec *codec)
2835d949cac1SHarald Welte {
2836d949cac1SHarald Welte 	struct via_spec *spec;
2837d949cac1SHarald Welte 	int err;
2838d949cac1SHarald Welte 
2839d949cac1SHarald Welte 	/* create a codec specific record */
28405b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2841d949cac1SHarald Welte 	if (spec == NULL)
2842d949cac1SHarald Welte 		return -ENOMEM;
2843d949cac1SHarald Welte 
2844620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
2845d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1a, 0, 3, 40);
2846d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1e, 0, 3, 40);
2847620e2b28STakashi Iwai 
2848d949cac1SHarald Welte 	/* automatic parse from the BIOS config */
284912daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2850d949cac1SHarald Welte 	if (err < 0) {
2851d949cac1SHarald Welte 		via_free(codec);
2852d949cac1SHarald Welte 		return err;
2853d949cac1SHarald Welte 	}
2854d949cac1SHarald Welte 
2855096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
2856d949cac1SHarald Welte 
2857d949cac1SHarald Welte 	codec->patch_ops = via_patch_ops;
2858d949cac1SHarald Welte 
2859518bf3baSLydia Wang 	/* correct names for VT1708BCE */
2860518bf3baSLydia Wang 	if (get_codec_type(codec) == VT1708BCE)	{
2861518bf3baSLydia Wang 		kfree(codec->chip_name);
2862518bf3baSLydia Wang 		codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
2863518bf3baSLydia Wang 		snprintf(codec->bus->card->mixername,
2864518bf3baSLydia Wang 			 sizeof(codec->bus->card->mixername),
2865518bf3baSLydia Wang 			 "%s %s", codec->vendor_name, codec->chip_name);
2866970f630fSLydia Wang 	}
2867bc92df7fSLydia Wang 	/* correct names for VT1705 */
2868bc92df7fSLydia Wang 	if (codec->vendor_id == 0x11064397)	{
2869bc92df7fSLydia Wang 		kfree(codec->chip_name);
2870bc92df7fSLydia Wang 		codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
2871bc92df7fSLydia Wang 		snprintf(codec->bus->card->mixername,
2872bc92df7fSLydia Wang 			 sizeof(codec->bus->card->mixername),
2873bc92df7fSLydia Wang 			 "%s %s", codec->vendor_name, codec->chip_name);
2874bc92df7fSLydia Wang 	}
28753e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
2876d949cac1SHarald Welte 	return 0;
2877d949cac1SHarald Welte }
2878d949cac1SHarald Welte 
2879d949cac1SHarald Welte /* Patch for VT1702 */
2880d949cac1SHarald Welte 
2881096a8854STakashi Iwai static const struct hda_verb vt1702_init_verbs[] = {
2882bc7e7e5cSLydia Wang 	/* mixer enable */
2883bc7e7e5cSLydia Wang 	{0x1, 0xF88, 0x3},
2884bc7e7e5cSLydia Wang 	/* GPIO 0~2 */
2885bc7e7e5cSLydia Wang 	{0x1, 0xF82, 0x3F},
2886d949cac1SHarald Welte 	{ }
2887d949cac1SHarald Welte };
2888d949cac1SHarald Welte 
28893e95b9abSLydia Wang static void set_widgets_power_state_vt1702(struct hda_codec *codec)
28903e95b9abSLydia Wang {
28913e95b9abSLydia Wang 	int imux_is_smixer =
28923e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
28933e95b9abSLydia Wang 	unsigned int parm;
28943e95b9abSLydia Wang 	/* inputs */
28953e95b9abSLydia Wang 	/* PW 1/2/5 (14h/15h/18h) */
28963e95b9abSLydia Wang 	parm = AC_PWRST_D3;
28973e95b9abSLydia Wang 	set_pin_power_state(codec, 0x14, &parm);
28983e95b9abSLydia Wang 	set_pin_power_state(codec, 0x15, &parm);
28993e95b9abSLydia Wang 	set_pin_power_state(codec, 0x18, &parm);
29003e95b9abSLydia Wang 	if (imux_is_smixer)
29013e95b9abSLydia Wang 		parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
29023e95b9abSLydia Wang 	/* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
29033e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
29043e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x12, 0, AC_VERB_SET_POWER_STATE, parm);
29053e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
29063e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_POWER_STATE, parm);
29073e95b9abSLydia Wang 
29083e95b9abSLydia Wang 	/* outputs */
29093e95b9abSLydia Wang 	/* PW 3/4 (16h/17h) */
29103e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29113e95b9abSLydia Wang 	set_pin_power_state(codec, 0x17, &parm);
29123e95b9abSLydia Wang 	set_pin_power_state(codec, 0x16, &parm);
29133e95b9abSLydia Wang 	/* MW0 (1ah), AOW 0/1 (10h/1dh) */
29143e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
29153e95b9abSLydia Wang 			    imux_is_smixer ? AC_PWRST_D0 : parm);
29163e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
29173e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
29183e95b9abSLydia Wang }
29193e95b9abSLydia Wang 
2920d949cac1SHarald Welte static int patch_vt1702(struct hda_codec *codec)
2921d949cac1SHarald Welte {
2922d949cac1SHarald Welte 	struct via_spec *spec;
2923d949cac1SHarald Welte 	int err;
2924d949cac1SHarald Welte 
2925d949cac1SHarald Welte 	/* create a codec specific record */
29265b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2927d949cac1SHarald Welte 	if (spec == NULL)
2928d949cac1SHarald Welte 		return -ENOMEM;
2929d949cac1SHarald Welte 
2930620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x1a;
2931620e2b28STakashi Iwai 
293212daef65STakashi Iwai 	/* limit AA path volume to 0 dB */
293312daef65STakashi Iwai 	snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
293412daef65STakashi Iwai 				  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
293512daef65STakashi Iwai 				  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
293612daef65STakashi Iwai 				  (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
293712daef65STakashi Iwai 				  (1 << AC_AMPCAP_MUTE_SHIFT));
293812daef65STakashi Iwai 
2939d949cac1SHarald Welte 	/* automatic parse from the BIOS config */
294012daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2941d949cac1SHarald Welte 	if (err < 0) {
2942d949cac1SHarald Welte 		via_free(codec);
2943d949cac1SHarald Welte 		return err;
2944d949cac1SHarald Welte 	}
2945d949cac1SHarald Welte 
2946096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
2947d949cac1SHarald Welte 
2948d949cac1SHarald Welte 	codec->patch_ops = via_patch_ops;
2949d949cac1SHarald Welte 
29503e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1702;
2951d949cac1SHarald Welte 	return 0;
2952d949cac1SHarald Welte }
2953d949cac1SHarald Welte 
2954eb7188caSLydia Wang /* Patch for VT1718S */
2955eb7188caSLydia Wang 
2956096a8854STakashi Iwai static const struct hda_verb vt1718S_init_verbs[] = {
29574ab2d53aSLydia Wang 	/* Enable MW0 adjust Gain 5 */
29584ab2d53aSLydia Wang 	{0x1, 0xfb2, 0x10},
2959eb7188caSLydia Wang 	/* Enable Boost Volume backdoor */
2960eb7188caSLydia Wang 	{0x1, 0xf88, 0x8},
29615d41762aSTakashi Iwai 
2962eb7188caSLydia Wang 	{ }
2963eb7188caSLydia Wang };
2964eb7188caSLydia Wang 
29653e95b9abSLydia Wang static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
29663e95b9abSLydia Wang {
29673e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
29683e95b9abSLydia Wang 	int imux_is_smixer;
29693e95b9abSLydia Wang 	unsigned int parm;
29703e95b9abSLydia Wang 	/* MUX6 (1eh) = stereo mixer */
29713e95b9abSLydia Wang 	imux_is_smixer =
29723e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
29733e95b9abSLydia Wang 	/* inputs */
29743e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
29753e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29763e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
29773e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
29783e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
29793e95b9abSLydia Wang 	if (imux_is_smixer)
29803e95b9abSLydia Wang 		parm = AC_PWRST_D0;
29813e95b9abSLydia Wang 	/* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
29823e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
29833e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
29843e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
29853e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
29863e95b9abSLydia Wang 
29873e95b9abSLydia Wang 	/* outputs */
29883e95b9abSLydia Wang 	/* PW3 (27h), MW2 (1ah), AOW3 (bh) */
29893e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29903e95b9abSLydia Wang 	set_pin_power_state(codec, 0x27, &parm);
29913e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE, parm);
29923e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0xb, 0, AC_VERB_SET_POWER_STATE, parm);
29933e95b9abSLydia Wang 
29943e95b9abSLydia Wang 	/* PW2 (26h), AOW2 (ah) */
29953e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29963e95b9abSLydia Wang 	set_pin_power_state(codec, 0x26, &parm);
29973e95b9abSLydia Wang 	if (spec->smart51_enabled)
29983e95b9abSLydia Wang 		set_pin_power_state(codec, 0x2b, &parm);
29993e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0xa, 0, AC_VERB_SET_POWER_STATE, parm);
30003e95b9abSLydia Wang 
30013e95b9abSLydia Wang 	/* PW0 (24h), AOW0 (8h) */
30023e95b9abSLydia Wang 	parm = AC_PWRST_D3;
30033e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
30043e95b9abSLydia Wang 	if (!spec->hp_independent_mode) /* check for redirected HP */
30053e95b9abSLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
30063e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
30073e95b9abSLydia Wang 	/* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
30083e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x21, 0, AC_VERB_SET_POWER_STATE,
30093e95b9abSLydia Wang 			    imux_is_smixer ? AC_PWRST_D0 : parm);
30103e95b9abSLydia Wang 
30113e95b9abSLydia Wang 	/* PW1 (25h), AOW1 (9h) */
30123e95b9abSLydia Wang 	parm = AC_PWRST_D3;
30133e95b9abSLydia Wang 	set_pin_power_state(codec, 0x25, &parm);
30143e95b9abSLydia Wang 	if (spec->smart51_enabled)
30153e95b9abSLydia Wang 		set_pin_power_state(codec, 0x2a, &parm);
30163e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x9, 0, AC_VERB_SET_POWER_STATE, parm);
30173e95b9abSLydia Wang 
30183e95b9abSLydia Wang 	if (spec->hp_independent_mode) {
30193e95b9abSLydia Wang 		/* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
30203e95b9abSLydia Wang 		parm = AC_PWRST_D3;
30213e95b9abSLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
30223e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x1b, 0,
30233e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
30243e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x34, 0,
30253e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
30263e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0xc, 0,
30273e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
30283e95b9abSLydia Wang 	}
30293e95b9abSLydia Wang }
30303e95b9abSLydia Wang 
3031eb7188caSLydia Wang static int patch_vt1718S(struct hda_codec *codec)
3032eb7188caSLydia Wang {
3033eb7188caSLydia Wang 	struct via_spec *spec;
3034eb7188caSLydia Wang 	int err;
3035eb7188caSLydia Wang 
3036eb7188caSLydia Wang 	/* create a codec specific record */
30375b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3038eb7188caSLydia Wang 	if (spec == NULL)
3039eb7188caSLydia Wang 		return -ENOMEM;
3040eb7188caSLydia Wang 
3041620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
3042d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3043d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
3044c4394f5bSLydia Wang 	spec->dac_mixer_idx = 5;
3045620e2b28STakashi Iwai 
3046eb7188caSLydia Wang 	/* automatic parse from the BIOS config */
304712daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3048eb7188caSLydia Wang 	if (err < 0) {
3049eb7188caSLydia Wang 		via_free(codec);
3050eb7188caSLydia Wang 		return err;
3051eb7188caSLydia Wang 	}
3052eb7188caSLydia Wang 
3053096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
3054eb7188caSLydia Wang 
3055eb7188caSLydia Wang 	codec->patch_ops = via_patch_ops;
3056eb7188caSLydia Wang 
30573e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1718S;
30583e95b9abSLydia Wang 
3059eb7188caSLydia Wang 	return 0;
3060eb7188caSLydia Wang }
3061f3db423dSLydia Wang 
3062f3db423dSLydia Wang /* Patch for VT1716S */
3063f3db423dSLydia Wang 
3064f3db423dSLydia Wang static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
3065f3db423dSLydia Wang 			    struct snd_ctl_elem_info *uinfo)
3066f3db423dSLydia Wang {
3067f3db423dSLydia Wang 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3068f3db423dSLydia Wang 	uinfo->count = 1;
3069f3db423dSLydia Wang 	uinfo->value.integer.min = 0;
3070f3db423dSLydia Wang 	uinfo->value.integer.max = 1;
3071f3db423dSLydia Wang 	return 0;
3072f3db423dSLydia Wang }
3073f3db423dSLydia Wang 
3074f3db423dSLydia Wang static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
3075f3db423dSLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
3076f3db423dSLydia Wang {
3077f3db423dSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3078f3db423dSLydia Wang 	int index = 0;
3079f3db423dSLydia Wang 
3080f3db423dSLydia Wang 	index = snd_hda_codec_read(codec, 0x26, 0,
3081f3db423dSLydia Wang 					       AC_VERB_GET_CONNECT_SEL, 0);
3082f3db423dSLydia Wang 	if (index != -1)
3083f3db423dSLydia Wang 		*ucontrol->value.integer.value = index;
3084f3db423dSLydia Wang 
3085f3db423dSLydia Wang 	return 0;
3086f3db423dSLydia Wang }
3087f3db423dSLydia Wang 
3088f3db423dSLydia Wang static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
3089f3db423dSLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
3090f3db423dSLydia Wang {
3091f3db423dSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3092f3db423dSLydia Wang 	struct via_spec *spec = codec->spec;
3093f3db423dSLydia Wang 	int index = *ucontrol->value.integer.value;
3094f3db423dSLydia Wang 
3095f3db423dSLydia Wang 	snd_hda_codec_write(codec, 0x26, 0,
3096f3db423dSLydia Wang 					       AC_VERB_SET_CONNECT_SEL, index);
3097f3db423dSLydia Wang 	spec->dmic_enabled = index;
30983e95b9abSLydia Wang 	set_widgets_power_state(codec);
3099f3db423dSLydia Wang 	return 1;
3100f3db423dSLydia Wang }
3101f3db423dSLydia Wang 
310290dd48a1STakashi Iwai static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
3103f3db423dSLydia Wang 	HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
3104f3db423dSLydia Wang 	{
3105f3db423dSLydia Wang 	 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3106f3db423dSLydia Wang 	 .name = "Digital Mic Capture Switch",
31075b0cb1d8SJaroslav Kysela 	 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
3108f3db423dSLydia Wang 	 .count = 1,
3109f3db423dSLydia Wang 	 .info = vt1716s_dmic_info,
3110f3db423dSLydia Wang 	 .get = vt1716s_dmic_get,
3111f3db423dSLydia Wang 	 .put = vt1716s_dmic_put,
3112f3db423dSLydia Wang 	 },
3113f3db423dSLydia Wang 	{}			/* end */
3114f3db423dSLydia Wang };
3115f3db423dSLydia Wang 
3116f3db423dSLydia Wang 
3117f3db423dSLydia Wang /* mono-out mixer elements */
311890dd48a1STakashi Iwai static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
3119f3db423dSLydia Wang 	HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
3120f3db423dSLydia Wang 	{ } /* end */
3121f3db423dSLydia Wang };
3122f3db423dSLydia Wang 
3123096a8854STakashi Iwai static const struct hda_verb vt1716S_init_verbs[] = {
3124f3db423dSLydia Wang 	/* Enable Boost Volume backdoor */
3125f3db423dSLydia Wang 	{0x1, 0xf8a, 0x80},
3126f3db423dSLydia Wang 	/* don't bybass mixer */
3127f3db423dSLydia Wang 	{0x1, 0xf88, 0xc0},
3128f3db423dSLydia Wang 	/* Enable mono output */
3129f3db423dSLydia Wang 	{0x1, 0xf90, 0x08},
3130f3db423dSLydia Wang 	{ }
3131f3db423dSLydia Wang };
3132f3db423dSLydia Wang 
31333e95b9abSLydia Wang static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
31343e95b9abSLydia Wang {
31353e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
31363e95b9abSLydia Wang 	int imux_is_smixer;
31373e95b9abSLydia Wang 	unsigned int parm;
31383e95b9abSLydia Wang 	unsigned int mono_out, present;
31393e95b9abSLydia Wang 	/* SW0 (17h) = stereo mixer */
31403e95b9abSLydia Wang 	imux_is_smixer =
31413e95b9abSLydia Wang 	(snd_hda_codec_read(codec, 0x17, 0,
31423e95b9abSLydia Wang 			    AC_VERB_GET_CONNECT_SEL, 0x00) ==  5);
31433e95b9abSLydia Wang 	/* inputs */
31443e95b9abSLydia Wang 	/* PW 1/2/5 (1ah/1bh/1eh) */
31453e95b9abSLydia Wang 	parm = AC_PWRST_D3;
31463e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1a, &parm);
31473e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1b, &parm);
31483e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
31493e95b9abSLydia Wang 	if (imux_is_smixer)
31503e95b9abSLydia Wang 		parm = AC_PWRST_D0;
31513e95b9abSLydia Wang 	/* SW0 (17h), AIW0(13h) */
31523e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
31533e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
31543e95b9abSLydia Wang 
31553e95b9abSLydia Wang 	parm = AC_PWRST_D3;
31563e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
31573e95b9abSLydia Wang 	/* PW11 (22h) */
31583e95b9abSLydia Wang 	if (spec->dmic_enabled)
31593e95b9abSLydia Wang 		set_pin_power_state(codec, 0x22, &parm);
31603e95b9abSLydia Wang 	else
31613e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x22, 0,
31623e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
31633e95b9abSLydia Wang 
31643e95b9abSLydia Wang 	/* SW2(26h), AIW1(14h) */
31653e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x26, 0, AC_VERB_SET_POWER_STATE, parm);
31663e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
31673e95b9abSLydia Wang 
31683e95b9abSLydia Wang 	/* outputs */
31693e95b9abSLydia Wang 	/* PW0 (19h), SW1 (18h), AOW1 (11h) */
31703e95b9abSLydia Wang 	parm = AC_PWRST_D3;
31713e95b9abSLydia Wang 	set_pin_power_state(codec, 0x19, &parm);
31723e95b9abSLydia Wang 	/* Smart 5.1 PW2(1bh) */
31733e95b9abSLydia Wang 	if (spec->smart51_enabled)
31743e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1b, &parm);
31753e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
31763e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
31773e95b9abSLydia Wang 
31783e95b9abSLydia Wang 	/* PW7 (23h), SW3 (27h), AOW3 (25h) */
31793e95b9abSLydia Wang 	parm = AC_PWRST_D3;
31803e95b9abSLydia Wang 	set_pin_power_state(codec, 0x23, &parm);
31813e95b9abSLydia Wang 	/* Smart 5.1 PW1(1ah) */
31823e95b9abSLydia Wang 	if (spec->smart51_enabled)
31833e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1a, &parm);
31843e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x27, 0, AC_VERB_SET_POWER_STATE, parm);
31853e95b9abSLydia Wang 
31863e95b9abSLydia Wang 	/* Smart 5.1 PW5(1eh) */
31873e95b9abSLydia Wang 	if (spec->smart51_enabled)
31883e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1e, &parm);
31893e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x25, 0, AC_VERB_SET_POWER_STATE, parm);
31903e95b9abSLydia Wang 
31913e95b9abSLydia Wang 	/* Mono out */
31923e95b9abSLydia Wang 	/* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
31933e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x1c);
31943e95b9abSLydia Wang 
31953e95b9abSLydia Wang 	if (present)
31963e95b9abSLydia Wang 		mono_out = 0;
31973e95b9abSLydia Wang 	else {
31983e95b9abSLydia Wang 		present = snd_hda_jack_detect(codec, 0x1d);
31993e95b9abSLydia Wang 		if (!spec->hp_independent_mode && present)
32003e95b9abSLydia Wang 			mono_out = 0;
32013e95b9abSLydia Wang 		else
32023e95b9abSLydia Wang 			mono_out = 1;
32033e95b9abSLydia Wang 	}
32043e95b9abSLydia Wang 	parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
32053e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x28, 0, AC_VERB_SET_POWER_STATE, parm);
32063e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x29, 0, AC_VERB_SET_POWER_STATE, parm);
32073e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x2a, 0, AC_VERB_SET_POWER_STATE, parm);
32083e95b9abSLydia Wang 
32093e95b9abSLydia Wang 	/* PW 3/4 (1ch/1dh) */
32103e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32113e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1c, &parm);
32123e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1d, &parm);
32133e95b9abSLydia Wang 	/* HP Independent Mode, power on AOW3 */
32143e95b9abSLydia Wang 	if (spec->hp_independent_mode)
32153e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x25, 0,
32163e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
32173e95b9abSLydia Wang 
32183e95b9abSLydia Wang 	/* force to D0 for internal Speaker */
32193e95b9abSLydia Wang 	/* MW0 (16h), AOW0 (10h) */
32203e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
32213e95b9abSLydia Wang 			    imux_is_smixer ? AC_PWRST_D0 : parm);
32223e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
32233e95b9abSLydia Wang 			    mono_out ? AC_PWRST_D0 : parm);
32243e95b9abSLydia Wang }
32253e95b9abSLydia Wang 
3226f3db423dSLydia Wang static int patch_vt1716S(struct hda_codec *codec)
3227f3db423dSLydia Wang {
3228f3db423dSLydia Wang 	struct via_spec *spec;
3229f3db423dSLydia Wang 	int err;
3230f3db423dSLydia Wang 
3231f3db423dSLydia Wang 	/* create a codec specific record */
32325b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3233f3db423dSLydia Wang 	if (spec == NULL)
3234f3db423dSLydia Wang 		return -ENOMEM;
3235f3db423dSLydia Wang 
3236620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
3237d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1a, 0, 3, 40);
3238d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1e, 0, 3, 40);
3239620e2b28STakashi Iwai 
3240f3db423dSLydia Wang 	/* automatic parse from the BIOS config */
324112daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3242f3db423dSLydia Wang 	if (err < 0) {
3243f3db423dSLydia Wang 		via_free(codec);
3244f3db423dSLydia Wang 		return err;
3245f3db423dSLydia Wang 	}
3246f3db423dSLydia Wang 
3247096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++]  = vt1716S_init_verbs;
3248f3db423dSLydia Wang 
3249f3db423dSLydia Wang 	spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
3250f3db423dSLydia Wang 	spec->num_mixers++;
3251f3db423dSLydia Wang 
3252f3db423dSLydia Wang 	spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
3253f3db423dSLydia Wang 
3254f3db423dSLydia Wang 	codec->patch_ops = via_patch_ops;
3255f3db423dSLydia Wang 
32563e95b9abSLydia Wang 	spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
3257f3db423dSLydia Wang 	return 0;
3258f3db423dSLydia Wang }
325925eaba2fSLydia Wang 
326025eaba2fSLydia Wang /* for vt2002P */
326125eaba2fSLydia Wang 
3262096a8854STakashi Iwai static const struct hda_verb vt2002P_init_verbs[] = {
3263eadb9a80SLydia Wang 	/* Class-D speaker related verbs */
3264eadb9a80SLydia Wang 	{0x1, 0xfe0, 0x4},
3265eadb9a80SLydia Wang 	{0x1, 0xfe9, 0x80},
3266eadb9a80SLydia Wang 	{0x1, 0xfe2, 0x22},
326725eaba2fSLydia Wang 	/* Enable Boost Volume backdoor */
326825eaba2fSLydia Wang 	{0x1, 0xfb9, 0x24},
326925eaba2fSLydia Wang 	/* Enable AOW0 to MW9 */
327025eaba2fSLydia Wang 	{0x1, 0xfb8, 0x88},
327125eaba2fSLydia Wang 	{ }
327225eaba2fSLydia Wang };
32734a918ffeSTakashi Iwai 
3274096a8854STakashi Iwai static const struct hda_verb vt1802_init_verbs[] = {
327511890956SLydia Wang 	/* Enable Boost Volume backdoor */
327611890956SLydia Wang 	{0x1, 0xfb9, 0x24},
327711890956SLydia Wang 	/* Enable AOW0 to MW9 */
327811890956SLydia Wang 	{0x1, 0xfb8, 0x88},
327911890956SLydia Wang 	{ }
328011890956SLydia Wang };
328125eaba2fSLydia Wang 
32823e95b9abSLydia Wang static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
32833e95b9abSLydia Wang {
32843e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
32853e95b9abSLydia Wang 	int imux_is_smixer;
32863e95b9abSLydia Wang 	unsigned int parm;
32873e95b9abSLydia Wang 	unsigned int present;
32883e95b9abSLydia Wang 	/* MUX9 (1eh) = stereo mixer */
32893e95b9abSLydia Wang 	imux_is_smixer =
32903e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
32913e95b9abSLydia Wang 	/* inputs */
32923e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
32933e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32943e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
32953e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
32963e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
32973e95b9abSLydia Wang 	parm = AC_PWRST_D0;
32983e95b9abSLydia Wang 	/* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
32993e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
33003e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
33013e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
33023e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
33033e95b9abSLydia Wang 
33043e95b9abSLydia Wang 	/* outputs */
33053e95b9abSLydia Wang 	/* AOW0 (8h)*/
33063e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
33073e95b9abSLydia Wang 
330811890956SLydia Wang 	if (spec->codec_type == VT1802) {
330911890956SLydia Wang 		/* PW4 (28h), MW4 (18h), MUX4(38h) */
331011890956SLydia Wang 		parm = AC_PWRST_D3;
331111890956SLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
331211890956SLydia Wang 		snd_hda_codec_write(codec, 0x18, 0,
331311890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
331411890956SLydia Wang 		snd_hda_codec_write(codec, 0x38, 0,
331511890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
331611890956SLydia Wang 	} else {
33173e95b9abSLydia Wang 		/* PW4 (26h), MW4 (1ch), MUX4(37h) */
33183e95b9abSLydia Wang 		parm = AC_PWRST_D3;
33193e95b9abSLydia Wang 		set_pin_power_state(codec, 0x26, &parm);
33203e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x1c, 0,
33213e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
33223e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x37, 0,
33233e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
332411890956SLydia Wang 	}
33253e95b9abSLydia Wang 
332611890956SLydia Wang 	if (spec->codec_type == VT1802) {
332711890956SLydia Wang 		/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
332811890956SLydia Wang 		parm = AC_PWRST_D3;
332911890956SLydia Wang 		set_pin_power_state(codec, 0x25, &parm);
333011890956SLydia Wang 		snd_hda_codec_write(codec, 0x15, 0,
333111890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
333211890956SLydia Wang 		snd_hda_codec_write(codec, 0x35, 0,
333311890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
333411890956SLydia Wang 	} else {
33353e95b9abSLydia Wang 		/* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
33363e95b9abSLydia Wang 		parm = AC_PWRST_D3;
33373e95b9abSLydia Wang 		set_pin_power_state(codec, 0x25, &parm);
33383e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x19, 0,
33393e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
33403e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x35, 0,
33413e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
334211890956SLydia Wang 	}
33433e95b9abSLydia Wang 
33443e95b9abSLydia Wang 	if (spec->hp_independent_mode)
33453e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x9, 0,
33463e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
33473e95b9abSLydia Wang 
33483e95b9abSLydia Wang 	/* Class-D */
33493e95b9abSLydia Wang 	/* PW0 (24h), MW0(18h/14h), MUX0(34h) */
33503e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x25);
33513e95b9abSLydia Wang 
33523e95b9abSLydia Wang 	parm = AC_PWRST_D3;
33533e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
33543e95b9abSLydia Wang 	parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
335511890956SLydia Wang 	if (spec->codec_type == VT1802)
335611890956SLydia Wang 		snd_hda_codec_write(codec, 0x14, 0,
335711890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
335811890956SLydia Wang 	else
33593e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x18, 0,
33603e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
33613e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x34, 0, AC_VERB_SET_POWER_STATE, parm);
33623e95b9abSLydia Wang 
33633e95b9abSLydia Wang 	/* Mono Out */
33643e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x26);
33653e95b9abSLydia Wang 
33663e95b9abSLydia Wang 	parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
336711890956SLydia Wang 	if (spec->codec_type == VT1802) {
336811890956SLydia Wang 		/* PW15 (33h), MW8(1ch), MUX8(3ch) */
336911890956SLydia Wang 		snd_hda_codec_write(codec, 0x33, 0,
337011890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
337111890956SLydia Wang 		snd_hda_codec_write(codec, 0x1c, 0,
337211890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
337311890956SLydia Wang 		snd_hda_codec_write(codec, 0x3c, 0,
337411890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
337511890956SLydia Wang 	} else {
33763e95b9abSLydia Wang 		/* PW15 (31h), MW8(17h), MUX8(3bh) */
33773e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x31, 0,
33783e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
33793e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x17, 0,
33803e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
33813e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3b, 0,
33823e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
338311890956SLydia Wang 	}
33843e95b9abSLydia Wang 	/* MW9 (21h) */
33853e95b9abSLydia Wang 	if (imux_is_smixer || !is_aa_path_mute(codec))
33863e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x21, 0,
33873e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
33883e95b9abSLydia Wang 	else
33893e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x21, 0,
33903e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
33913e95b9abSLydia Wang }
339225eaba2fSLydia Wang 
339325eaba2fSLydia Wang /* patch for vt2002P */
339425eaba2fSLydia Wang static int patch_vt2002P(struct hda_codec *codec)
339525eaba2fSLydia Wang {
339625eaba2fSLydia Wang 	struct via_spec *spec;
339725eaba2fSLydia Wang 	int err;
339825eaba2fSLydia Wang 
339925eaba2fSLydia Wang 	/* create a codec specific record */
34005b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
340125eaba2fSLydia Wang 	if (spec == NULL)
340225eaba2fSLydia Wang 		return -ENOMEM;
340325eaba2fSLydia Wang 
3404620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
34055c9a5615SLydia Wang 	spec->dac_mixer_idx = 3;
3406d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3407d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
3408620e2b28STakashi Iwai 
340925eaba2fSLydia Wang 	/* automatic parse from the BIOS config */
341012daef65STakashi Iwai 	err = via_parse_auto_config(codec);
341125eaba2fSLydia Wang 	if (err < 0) {
341225eaba2fSLydia Wang 		via_free(codec);
341325eaba2fSLydia Wang 		return err;
341425eaba2fSLydia Wang 	}
341525eaba2fSLydia Wang 
341611890956SLydia Wang 	if (spec->codec_type == VT1802)
34174a918ffeSTakashi Iwai 		spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
341811890956SLydia Wang 	else
34194a918ffeSTakashi Iwai 		spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
342011890956SLydia Wang 
342125eaba2fSLydia Wang 	codec->patch_ops = via_patch_ops;
342225eaba2fSLydia Wang 
34233e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt2002P;
342425eaba2fSLydia Wang 	return 0;
342525eaba2fSLydia Wang }
3426ab6734e7SLydia Wang 
3427ab6734e7SLydia Wang /* for vt1812 */
3428ab6734e7SLydia Wang 
3429096a8854STakashi Iwai static const struct hda_verb vt1812_init_verbs[] = {
3430ab6734e7SLydia Wang 	/* Enable Boost Volume backdoor */
3431ab6734e7SLydia Wang 	{0x1, 0xfb9, 0x24},
3432ab6734e7SLydia Wang 	/* Enable AOW0 to MW9 */
3433ab6734e7SLydia Wang 	{0x1, 0xfb8, 0xa8},
3434ab6734e7SLydia Wang 	{ }
3435ab6734e7SLydia Wang };
3436ab6734e7SLydia Wang 
34373e95b9abSLydia Wang static void set_widgets_power_state_vt1812(struct hda_codec *codec)
34383e95b9abSLydia Wang {
34393e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
34403e95b9abSLydia Wang 	int imux_is_smixer =
34413e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
34423e95b9abSLydia Wang 	unsigned int parm;
34433e95b9abSLydia Wang 	unsigned int present;
34443e95b9abSLydia Wang 	/* MUX10 (1eh) = stereo mixer */
34453e95b9abSLydia Wang 	imux_is_smixer =
34463e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
34473e95b9abSLydia Wang 	/* inputs */
34483e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
34493e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34503e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
34513e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
34523e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
34533e95b9abSLydia Wang 	parm = AC_PWRST_D0;
34543e95b9abSLydia Wang 	/* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
34553e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
34563e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
34573e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
34583e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
34593e95b9abSLydia Wang 
34603e95b9abSLydia Wang 	/* outputs */
34613e95b9abSLydia Wang 	/* AOW0 (8h)*/
34623e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x8, 0,
34633e95b9abSLydia Wang 			    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
34643e95b9abSLydia Wang 
34653e95b9abSLydia Wang 	/* PW4 (28h), MW4 (18h), MUX4(38h) */
34663e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34673e95b9abSLydia Wang 	set_pin_power_state(codec, 0x28, &parm);
34683e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
34693e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x38, 0, AC_VERB_SET_POWER_STATE, parm);
34703e95b9abSLydia Wang 
34713e95b9abSLydia Wang 	/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
34723e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34733e95b9abSLydia Wang 	set_pin_power_state(codec, 0x25, &parm);
34743e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x15, 0, AC_VERB_SET_POWER_STATE, parm);
34753e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x35, 0, AC_VERB_SET_POWER_STATE, parm);
34763e95b9abSLydia Wang 	if (spec->hp_independent_mode)
34773e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x9, 0,
34783e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
34793e95b9abSLydia Wang 
34803e95b9abSLydia Wang 	/* Internal Speaker */
34813e95b9abSLydia Wang 	/* PW0 (24h), MW0(14h), MUX0(34h) */
34823e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x25);
34833e95b9abSLydia Wang 
34843e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34853e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
34863e95b9abSLydia Wang 	if (present) {
34873e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x14, 0,
34883e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
34893e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x34, 0,
34903e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
34913e95b9abSLydia Wang 	} else {
34923e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x14, 0,
34933e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
34943e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x34, 0,
34953e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
34963e95b9abSLydia Wang 	}
34973e95b9abSLydia Wang 
34983e95b9abSLydia Wang 
34993e95b9abSLydia Wang 	/* Mono Out */
35003e95b9abSLydia Wang 	/* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
35013e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x28);
35023e95b9abSLydia Wang 
35033e95b9abSLydia Wang 	parm = AC_PWRST_D3;
35043e95b9abSLydia Wang 	set_pin_power_state(codec, 0x31, &parm);
35053e95b9abSLydia Wang 	if (present) {
35063e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x1c, 0,
35073e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
35083e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3c, 0,
35093e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
35103e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3e, 0,
35113e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
35123e95b9abSLydia Wang 	} else {
35133e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x1c, 0,
35143e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
35153e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3c, 0,
35163e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
35173e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3e, 0,
35183e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
35193e95b9abSLydia Wang 	}
35203e95b9abSLydia Wang 
35213e95b9abSLydia Wang 	/* PW15 (33h), MW15 (1dh), MUX15(3dh) */
35223e95b9abSLydia Wang 	parm = AC_PWRST_D3;
35233e95b9abSLydia Wang 	set_pin_power_state(codec, 0x33, &parm);
35243e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
35253e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x3d, 0, AC_VERB_SET_POWER_STATE, parm);
35263e95b9abSLydia Wang 
35273e95b9abSLydia Wang }
3528ab6734e7SLydia Wang 
3529ab6734e7SLydia Wang /* patch for vt1812 */
3530ab6734e7SLydia Wang static int patch_vt1812(struct hda_codec *codec)
3531ab6734e7SLydia Wang {
3532ab6734e7SLydia Wang 	struct via_spec *spec;
3533ab6734e7SLydia Wang 	int err;
3534ab6734e7SLydia Wang 
3535ab6734e7SLydia Wang 	/* create a codec specific record */
35365b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3537ab6734e7SLydia Wang 	if (spec == NULL)
3538ab6734e7SLydia Wang 		return -ENOMEM;
3539ab6734e7SLydia Wang 
3540620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
3541d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3542d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
354328dc10a5SLydia Wang 	spec->dac_mixer_idx = 5;
3544620e2b28STakashi Iwai 
3545ab6734e7SLydia Wang 	/* automatic parse from the BIOS config */
354612daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3547ab6734e7SLydia Wang 	if (err < 0) {
3548ab6734e7SLydia Wang 		via_free(codec);
3549ab6734e7SLydia Wang 		return err;
3550ab6734e7SLydia Wang 	}
3551ab6734e7SLydia Wang 
3552096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++]  = vt1812_init_verbs;
3553ab6734e7SLydia Wang 
3554ab6734e7SLydia Wang 	codec->patch_ops = via_patch_ops;
3555ab6734e7SLydia Wang 
35563e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1812;
3557ab6734e7SLydia Wang 	return 0;
3558ab6734e7SLydia Wang }
3559ab6734e7SLydia Wang 
3560c577b8a1SJoseph Chan /*
3561c577b8a1SJoseph Chan  * patch entries
3562c577b8a1SJoseph Chan  */
356390dd48a1STakashi Iwai static const struct hda_codec_preset snd_hda_preset_via[] = {
35643218c178STakashi Iwai 	{ .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
35653218c178STakashi Iwai 	{ .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
35663218c178STakashi Iwai 	{ .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
35673218c178STakashi Iwai 	{ .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
35683218c178STakashi Iwai 	{ .id = 0x1106e710, .name = "VT1709 10-Ch",
3569ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
35703218c178STakashi Iwai 	{ .id = 0x1106e711, .name = "VT1709 10-Ch",
3571ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
35723218c178STakashi Iwai 	{ .id = 0x1106e712, .name = "VT1709 10-Ch",
3573ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
35743218c178STakashi Iwai 	{ .id = 0x1106e713, .name = "VT1709 10-Ch",
3575ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
35763218c178STakashi Iwai 	{ .id = 0x1106e714, .name = "VT1709 6-Ch",
3577ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
35783218c178STakashi Iwai 	{ .id = 0x1106e715, .name = "VT1709 6-Ch",
3579ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
35803218c178STakashi Iwai 	{ .id = 0x1106e716, .name = "VT1709 6-Ch",
3581ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
35823218c178STakashi Iwai 	{ .id = 0x1106e717, .name = "VT1709 6-Ch",
3583ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
35843218c178STakashi Iwai 	{ .id = 0x1106e720, .name = "VT1708B 8-Ch",
3585ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
35863218c178STakashi Iwai 	{ .id = 0x1106e721, .name = "VT1708B 8-Ch",
3587ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
35883218c178STakashi Iwai 	{ .id = 0x1106e722, .name = "VT1708B 8-Ch",
3589ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
35903218c178STakashi Iwai 	{ .id = 0x1106e723, .name = "VT1708B 8-Ch",
3591ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
35923218c178STakashi Iwai 	{ .id = 0x1106e724, .name = "VT1708B 4-Ch",
3593ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
35943218c178STakashi Iwai 	{ .id = 0x1106e725, .name = "VT1708B 4-Ch",
3595ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
35963218c178STakashi Iwai 	{ .id = 0x1106e726, .name = "VT1708B 4-Ch",
3597ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
35983218c178STakashi Iwai 	{ .id = 0x1106e727, .name = "VT1708B 4-Ch",
3599ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
36003218c178STakashi Iwai 	{ .id = 0x11060397, .name = "VT1708S",
3601d949cac1SHarald Welte 	  .patch = patch_vt1708S},
36023218c178STakashi Iwai 	{ .id = 0x11061397, .name = "VT1708S",
3603d949cac1SHarald Welte 	  .patch = patch_vt1708S},
36043218c178STakashi Iwai 	{ .id = 0x11062397, .name = "VT1708S",
3605d949cac1SHarald Welte 	  .patch = patch_vt1708S},
36063218c178STakashi Iwai 	{ .id = 0x11063397, .name = "VT1708S",
3607d949cac1SHarald Welte 	  .patch = patch_vt1708S},
3608bc92df7fSLydia Wang 	{ .id = 0x11064397, .name = "VT1705",
3609d949cac1SHarald Welte 	  .patch = patch_vt1708S},
36103218c178STakashi Iwai 	{ .id = 0x11065397, .name = "VT1708S",
3611d949cac1SHarald Welte 	  .patch = patch_vt1708S},
36123218c178STakashi Iwai 	{ .id = 0x11066397, .name = "VT1708S",
3613d949cac1SHarald Welte 	  .patch = patch_vt1708S},
36143218c178STakashi Iwai 	{ .id = 0x11067397, .name = "VT1708S",
3615d949cac1SHarald Welte 	  .patch = patch_vt1708S},
36163218c178STakashi Iwai 	{ .id = 0x11060398, .name = "VT1702",
3617d949cac1SHarald Welte 	  .patch = patch_vt1702},
36183218c178STakashi Iwai 	{ .id = 0x11061398, .name = "VT1702",
3619d949cac1SHarald Welte 	  .patch = patch_vt1702},
36203218c178STakashi Iwai 	{ .id = 0x11062398, .name = "VT1702",
3621d949cac1SHarald Welte 	  .patch = patch_vt1702},
36223218c178STakashi Iwai 	{ .id = 0x11063398, .name = "VT1702",
3623d949cac1SHarald Welte 	  .patch = patch_vt1702},
36243218c178STakashi Iwai 	{ .id = 0x11064398, .name = "VT1702",
3625d949cac1SHarald Welte 	  .patch = patch_vt1702},
36263218c178STakashi Iwai 	{ .id = 0x11065398, .name = "VT1702",
3627d949cac1SHarald Welte 	  .patch = patch_vt1702},
36283218c178STakashi Iwai 	{ .id = 0x11066398, .name = "VT1702",
3629d949cac1SHarald Welte 	  .patch = patch_vt1702},
36303218c178STakashi Iwai 	{ .id = 0x11067398, .name = "VT1702",
3631d949cac1SHarald Welte 	  .patch = patch_vt1702},
3632eb7188caSLydia Wang 	{ .id = 0x11060428, .name = "VT1718S",
3633eb7188caSLydia Wang 	  .patch = patch_vt1718S},
3634eb7188caSLydia Wang 	{ .id = 0x11064428, .name = "VT1718S",
3635eb7188caSLydia Wang 	  .patch = patch_vt1718S},
3636bb3c6bfcSLydia Wang 	{ .id = 0x11060441, .name = "VT2020",
3637bb3c6bfcSLydia Wang 	  .patch = patch_vt1718S},
3638bb3c6bfcSLydia Wang 	{ .id = 0x11064441, .name = "VT1828S",
3639bb3c6bfcSLydia Wang 	  .patch = patch_vt1718S},
3640f3db423dSLydia Wang 	{ .id = 0x11060433, .name = "VT1716S",
3641f3db423dSLydia Wang 	  .patch = patch_vt1716S},
3642f3db423dSLydia Wang 	{ .id = 0x1106a721, .name = "VT1716S",
3643f3db423dSLydia Wang 	  .patch = patch_vt1716S},
364425eaba2fSLydia Wang 	{ .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
364525eaba2fSLydia Wang 	{ .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
3646ab6734e7SLydia Wang 	{ .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
364736dd5c4aSLydia Wang 	{ .id = 0x11060440, .name = "VT1818S",
364836dd5c4aSLydia Wang 	  .patch = patch_vt1708S},
364911890956SLydia Wang 	{ .id = 0x11060446, .name = "VT1802",
365011890956SLydia Wang 		.patch = patch_vt2002P},
365111890956SLydia Wang 	{ .id = 0x11068446, .name = "VT1802",
365211890956SLydia Wang 		.patch = patch_vt2002P},
3653c577b8a1SJoseph Chan 	{} /* terminator */
3654c577b8a1SJoseph Chan };
36551289e9e8STakashi Iwai 
36561289e9e8STakashi Iwai MODULE_ALIAS("snd-hda-codec-id:1106*");
36571289e9e8STakashi Iwai 
36581289e9e8STakashi Iwai static struct hda_codec_preset_list via_list = {
36591289e9e8STakashi Iwai 	.preset = snd_hda_preset_via,
36601289e9e8STakashi Iwai 	.owner = THIS_MODULE,
36611289e9e8STakashi Iwai };
36621289e9e8STakashi Iwai 
36631289e9e8STakashi Iwai MODULE_LICENSE("GPL");
36641289e9e8STakashi Iwai MODULE_DESCRIPTION("VIA HD-audio codec");
36651289e9e8STakashi Iwai 
36661289e9e8STakashi Iwai static int __init patch_via_init(void)
36671289e9e8STakashi Iwai {
36681289e9e8STakashi Iwai 	return snd_hda_add_codec_preset(&via_list);
36691289e9e8STakashi Iwai }
36701289e9e8STakashi Iwai 
36711289e9e8STakashi Iwai static void __exit patch_via_exit(void)
36721289e9e8STakashi Iwai {
36731289e9e8STakashi Iwai 	snd_hda_delete_codec_preset(&via_list);
36741289e9e8STakashi Iwai }
36751289e9e8STakashi Iwai 
36761289e9e8STakashi Iwai module_init(patch_via_init)
36771289e9e8STakashi Iwai module_exit(patch_via_exit)
3678