xref: /openbmc/linux/sound/pci/hda/patch_via.c (revision 6162552b0de6ba80937c3dd53e084967851cd199)
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>
52da155d5bSPaul Gortmaker #include <linux/module.h>
53c577b8a1SJoseph Chan #include <sound/core.h>
540aa62aefSHarald Welte #include <sound/asoundef.h>
55c577b8a1SJoseph Chan #include "hda_codec.h"
56c577b8a1SJoseph Chan #include "hda_local.h"
57128bc4baSTakashi Iwai #include "hda_auto_parser.h"
581835a0f9STakashi Iwai #include "hda_jack.h"
59c577b8a1SJoseph Chan 
60c577b8a1SJoseph Chan /* Pin Widget NID */
6176d9b0ddSHarald Welte #define VT1708_HP_PIN_NID	0x20
6276d9b0ddSHarald Welte #define VT1708_CD_PIN_NID	0x24
63c577b8a1SJoseph Chan 
64d7426329SHarald Welte enum VIA_HDA_CODEC {
65d7426329SHarald Welte 	UNKNOWN = -1,
66d7426329SHarald Welte 	VT1708,
67d7426329SHarald Welte 	VT1709_10CH,
68d7426329SHarald Welte 	VT1709_6CH,
69d7426329SHarald Welte 	VT1708B_8CH,
70d7426329SHarald Welte 	VT1708B_4CH,
71d7426329SHarald Welte 	VT1708S,
72518bf3baSLydia Wang 	VT1708BCE,
73d7426329SHarald Welte 	VT1702,
74eb7188caSLydia Wang 	VT1718S,
75f3db423dSLydia Wang 	VT1716S,
7625eaba2fSLydia Wang 	VT2002P,
77ab6734e7SLydia Wang 	VT1812,
7811890956SLydia Wang 	VT1802,
79d7426329SHarald Welte 	CODEC_TYPES,
80d7426329SHarald Welte };
81d7426329SHarald Welte 
8211890956SLydia Wang #define VT2002P_COMPATIBLE(spec) \
8311890956SLydia Wang 	((spec)->codec_type == VT2002P ||\
8411890956SLydia Wang 	 (spec)->codec_type == VT1812 ||\
8511890956SLydia Wang 	 (spec)->codec_type == VT1802)
8611890956SLydia Wang 
878e3679dcSTakashi Iwai #define MAX_NID_PATH_DEPTH	5
888e3679dcSTakashi Iwai 
8909a9ad69STakashi Iwai /* output-path: DAC -> ... -> pin
9009a9ad69STakashi Iwai  * idx[] contains the source index number of the next widget;
9109a9ad69STakashi Iwai  * e.g. idx[0] is the index of the DAC selected by path[1] widget
9209a9ad69STakashi Iwai  * multi[] indicates whether it's a selector widget with multi-connectors
9309a9ad69STakashi Iwai  * (i.e. the connection selection is mandatory)
9409a9ad69STakashi Iwai  * vol_ctl and mute_ctl contains the NIDs for the assigned mixers
9509a9ad69STakashi Iwai  */
964a79616dSTakashi Iwai struct nid_path {
974a79616dSTakashi Iwai 	int depth;
988e3679dcSTakashi Iwai 	hda_nid_t path[MAX_NID_PATH_DEPTH];
9909a9ad69STakashi Iwai 	unsigned char idx[MAX_NID_PATH_DEPTH];
10009a9ad69STakashi Iwai 	unsigned char multi[MAX_NID_PATH_DEPTH];
10109a9ad69STakashi Iwai 	unsigned int vol_ctl;
10209a9ad69STakashi Iwai 	unsigned int mute_ctl;
1034a79616dSTakashi Iwai };
1044a79616dSTakashi Iwai 
105a86a88eaSTakashi Iwai /* input-path */
106a86a88eaSTakashi Iwai struct via_input {
107a86a88eaSTakashi Iwai 	hda_nid_t pin;	/* input-pin or aa-mix */
108a86a88eaSTakashi Iwai 	int adc_idx;	/* ADC index to be used */
109a86a88eaSTakashi Iwai 	int mux_idx;	/* MUX index (if any) */
110a86a88eaSTakashi Iwai 	const char *label;	/* input-source label */
111a86a88eaSTakashi Iwai };
112a86a88eaSTakashi Iwai 
113de6c74f3STakashi Iwai #define VIA_MAX_ADCS	3
114de6c74f3STakashi Iwai 
1153b607e3dSTakashi Iwai enum {
1163b607e3dSTakashi Iwai 	STREAM_MULTI_OUT = (1 << 0),
1173b607e3dSTakashi Iwai 	STREAM_INDEP_HP = (1 << 1),
1183b607e3dSTakashi Iwai };
1193b607e3dSTakashi Iwai 
1201f2e99feSLydia Wang struct via_spec {
1211f2e99feSLydia Wang 	/* codec parameterization */
12290dd48a1STakashi Iwai 	const struct snd_kcontrol_new *mixers[6];
1231f2e99feSLydia Wang 	unsigned int num_mixers;
1241f2e99feSLydia Wang 
12590dd48a1STakashi Iwai 	const struct hda_verb *init_verbs[5];
1261f2e99feSLydia Wang 	unsigned int num_iverbs;
1271f2e99feSLydia Wang 
12882673bc8STakashi Iwai 	char stream_name_analog[32];
1297eb56e84STakashi Iwai 	char stream_name_hp[32];
13090dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_analog_playback;
13190dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_analog_capture;
1321f2e99feSLydia Wang 
13382673bc8STakashi Iwai 	char stream_name_digital[32];
13490dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_digital_playback;
13590dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_digital_capture;
1361f2e99feSLydia Wang 
1371f2e99feSLydia Wang 	/* playback */
1381f2e99feSLydia Wang 	struct hda_multi_out multiout;
1391f2e99feSLydia Wang 	hda_nid_t slave_dig_outs[2];
140ece8d043STakashi Iwai 	hda_nid_t hp_dac_nid;
1413214b966STakashi Iwai 	hda_nid_t speaker_dac_nid;
1423214b966STakashi Iwai 	int hp_indep_shared;	/* indep HP-DAC is shared with side ch */
1433b607e3dSTakashi Iwai 	int opened_streams;	/* STREAM_* bits */
1443b607e3dSTakashi Iwai 	int active_streams;	/* STREAM_* bits */
1453214b966STakashi Iwai 	int aamix_mode;		/* loopback is enabled for output-path? */
1461f2e99feSLydia Wang 
1473214b966STakashi Iwai 	/* Output-paths:
1483214b966STakashi Iwai 	 * There are different output-paths depending on the setup.
1493214b966STakashi Iwai 	 * out_path, hp_path and speaker_path are primary paths.  If both
1503214b966STakashi Iwai 	 * direct DAC and aa-loopback routes are available, these contain
1513214b966STakashi Iwai 	 * the former paths.  Meanwhile *_mix_path contain the paths with
1523214b966STakashi Iwai 	 * loopback mixer.  (Since the loopback is only for front channel,
1533214b966STakashi Iwai 	 * no out_mix_path for surround channels.)
1543214b966STakashi Iwai 	 * The HP output has another path, hp_indep_path, which is used in
1553214b966STakashi Iwai 	 * the independent-HP mode.
1563214b966STakashi Iwai 	 */
157de6c74f3STakashi Iwai 	struct nid_path out_path[HDA_SIDE + 1];
1583214b966STakashi Iwai 	struct nid_path out_mix_path;
1594a79616dSTakashi Iwai 	struct nid_path hp_path;
1603214b966STakashi Iwai 	struct nid_path hp_mix_path;
1613214b966STakashi Iwai 	struct nid_path hp_indep_path;
1624a918ffeSTakashi Iwai 	struct nid_path speaker_path;
1633214b966STakashi Iwai 	struct nid_path speaker_mix_path;
1644a79616dSTakashi Iwai 
1651f2e99feSLydia Wang 	/* capture */
1661f2e99feSLydia Wang 	unsigned int num_adc_nids;
167de6c74f3STakashi Iwai 	hda_nid_t adc_nids[VIA_MAX_ADCS];
168de6c74f3STakashi Iwai 	hda_nid_t mux_nids[VIA_MAX_ADCS];
169620e2b28STakashi Iwai 	hda_nid_t aa_mix_nid;
1701f2e99feSLydia Wang 	hda_nid_t dig_in_nid;
1711f2e99feSLydia Wang 
1721f2e99feSLydia Wang 	/* capture source */
173a86a88eaSTakashi Iwai 	bool dyn_adc_switch;
174a86a88eaSTakashi Iwai 	int num_inputs;
175a86a88eaSTakashi Iwai 	struct via_input inputs[AUTO_CFG_MAX_INS + 1];
176de6c74f3STakashi Iwai 	unsigned int cur_mux[VIA_MAX_ADCS];
1771f2e99feSLydia Wang 
1783b607e3dSTakashi Iwai 	/* dynamic DAC switching */
1793b607e3dSTakashi Iwai 	unsigned int cur_dac_stream_tag;
1803b607e3dSTakashi Iwai 	unsigned int cur_dac_format;
1813b607e3dSTakashi Iwai 	unsigned int cur_hp_stream_tag;
1823b607e3dSTakashi Iwai 	unsigned int cur_hp_format;
1833b607e3dSTakashi Iwai 
184a86a88eaSTakashi Iwai 	/* dynamic ADC switching */
185a86a88eaSTakashi Iwai 	hda_nid_t cur_adc;
186a86a88eaSTakashi Iwai 	unsigned int cur_adc_stream_tag;
187a86a88eaSTakashi Iwai 	unsigned int cur_adc_format;
188a86a88eaSTakashi Iwai 
1891f2e99feSLydia Wang 	/* PCM information */
1901f2e99feSLydia Wang 	struct hda_pcm pcm_rec[3];
1911f2e99feSLydia Wang 
1921f2e99feSLydia Wang 	/* dynamic controls, init_verbs and input_mux */
1931f2e99feSLydia Wang 	struct auto_pin_cfg autocfg;
1941f2e99feSLydia Wang 	struct snd_array kctls;
1951f2e99feSLydia Wang 	hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
1961f2e99feSLydia Wang 
1971f2e99feSLydia Wang 	/* HP mode source */
1981f2e99feSLydia Wang 	unsigned int hp_independent_mode;
199f3db423dSLydia Wang 	unsigned int dmic_enabled;
20024088a58STakashi Iwai 	unsigned int no_pin_power_ctl;
2011f2e99feSLydia Wang 	enum VIA_HDA_CODEC codec_type;
2021f2e99feSLydia Wang 
203e9d010c2STakashi Iwai 	/* analog low-power control */
204e9d010c2STakashi Iwai 	bool alc_mode;
205e9d010c2STakashi Iwai 
206e3d7a143STakashi Iwai 	/* smart51 setup */
207e3d7a143STakashi Iwai 	unsigned int smart51_nums;
208e3d7a143STakashi Iwai 	hda_nid_t smart51_pins[2];
209e3d7a143STakashi Iwai 	int smart51_idxs[2];
210e3d7a143STakashi Iwai 	const char *smart51_labels[2];
211e3d7a143STakashi Iwai 	unsigned int smart51_enabled;
212e3d7a143STakashi Iwai 
2131f2e99feSLydia Wang 	/* work to check hp jack state */
2141f2e99feSLydia Wang 	struct hda_codec *codec;
2151f2e99feSLydia Wang 	struct delayed_work vt1708_hp_work;
216187d333eSTakashi Iwai 	int hp_work_active;
217e06e5a29STakashi Iwai 	int vt1708_jack_detect;
2181f2e99feSLydia Wang 	int vt1708_hp_present;
2193e95b9abSLydia Wang 
2203e95b9abSLydia Wang 	void (*set_widgets_power_state)(struct hda_codec *codec);
2213e95b9abSLydia Wang 
2221f2e99feSLydia Wang 	struct hda_loopback_check loopback;
22313af8e77STakashi Iwai 	int num_loopbacks;
22413af8e77STakashi Iwai 	struct hda_amp_list loopback_list[8];
225a86a88eaSTakashi Iwai 
226a86a88eaSTakashi Iwai 	/* bind capture-volume */
227a86a88eaSTakashi Iwai 	struct hda_bind_ctls *bind_cap_vol;
228a86a88eaSTakashi Iwai 	struct hda_bind_ctls *bind_cap_sw;
2293b607e3dSTakashi Iwai 
2303b607e3dSTakashi Iwai 	struct mutex config_mutex;
2311f2e99feSLydia Wang };
2321f2e99feSLydia Wang 
2330341ccd7SLydia Wang static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
2345b0cb1d8SJaroslav Kysela static struct via_spec * via_new_spec(struct hda_codec *codec)
2355b0cb1d8SJaroslav Kysela {
2365b0cb1d8SJaroslav Kysela 	struct via_spec *spec;
2375b0cb1d8SJaroslav Kysela 
2385b0cb1d8SJaroslav Kysela 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2395b0cb1d8SJaroslav Kysela 	if (spec == NULL)
2405b0cb1d8SJaroslav Kysela 		return NULL;
2415b0cb1d8SJaroslav Kysela 
2423b607e3dSTakashi Iwai 	mutex_init(&spec->config_mutex);
2435b0cb1d8SJaroslav Kysela 	codec->spec = spec;
2445b0cb1d8SJaroslav Kysela 	spec->codec = codec;
2450341ccd7SLydia Wang 	spec->codec_type = get_codec_type(codec);
2460341ccd7SLydia Wang 	/* VT1708BCE & VT1708S are almost same */
2470341ccd7SLydia Wang 	if (spec->codec_type == VT1708BCE)
2480341ccd7SLydia Wang 		spec->codec_type = VT1708S;
2495b0cb1d8SJaroslav Kysela 	return spec;
2505b0cb1d8SJaroslav Kysela }
2515b0cb1d8SJaroslav Kysela 
252744ff5f4SLydia Wang static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
253d7426329SHarald Welte {
254744ff5f4SLydia Wang 	u32 vendor_id = codec->vendor_id;
255d7426329SHarald Welte 	u16 ven_id = vendor_id >> 16;
256d7426329SHarald Welte 	u16 dev_id = vendor_id & 0xffff;
257d7426329SHarald Welte 	enum VIA_HDA_CODEC codec_type;
258d7426329SHarald Welte 
259d7426329SHarald Welte 	/* get codec type */
260d7426329SHarald Welte 	if (ven_id != 0x1106)
261d7426329SHarald Welte 		codec_type = UNKNOWN;
262d7426329SHarald Welte 	else if (dev_id >= 0x1708 && dev_id <= 0x170b)
263d7426329SHarald Welte 		codec_type = VT1708;
264d7426329SHarald Welte 	else if (dev_id >= 0xe710 && dev_id <= 0xe713)
265d7426329SHarald Welte 		codec_type = VT1709_10CH;
266d7426329SHarald Welte 	else if (dev_id >= 0xe714 && dev_id <= 0xe717)
267d7426329SHarald Welte 		codec_type = VT1709_6CH;
268518bf3baSLydia Wang 	else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
269d7426329SHarald Welte 		codec_type = VT1708B_8CH;
270518bf3baSLydia Wang 		if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
271518bf3baSLydia Wang 			codec_type = VT1708BCE;
272518bf3baSLydia Wang 	} else if (dev_id >= 0xe724 && dev_id <= 0xe727)
273d7426329SHarald Welte 		codec_type = VT1708B_4CH;
274d7426329SHarald Welte 	else if ((dev_id & 0xfff) == 0x397
275d7426329SHarald Welte 		 && (dev_id >> 12) < 8)
276d7426329SHarald Welte 		codec_type = VT1708S;
277d7426329SHarald Welte 	else if ((dev_id & 0xfff) == 0x398
278d7426329SHarald Welte 		 && (dev_id >> 12) < 8)
279d7426329SHarald Welte 		codec_type = VT1702;
280eb7188caSLydia Wang 	else if ((dev_id & 0xfff) == 0x428
281eb7188caSLydia Wang 		 && (dev_id >> 12) < 8)
282eb7188caSLydia Wang 		codec_type = VT1718S;
283f3db423dSLydia Wang 	else if (dev_id == 0x0433 || dev_id == 0xa721)
284f3db423dSLydia Wang 		codec_type = VT1716S;
285bb3c6bfcSLydia Wang 	else if (dev_id == 0x0441 || dev_id == 0x4441)
286bb3c6bfcSLydia Wang 		codec_type = VT1718S;
28725eaba2fSLydia Wang 	else if (dev_id == 0x0438 || dev_id == 0x4438)
28825eaba2fSLydia Wang 		codec_type = VT2002P;
289ab6734e7SLydia Wang 	else if (dev_id == 0x0448)
290ab6734e7SLydia Wang 		codec_type = VT1812;
29136dd5c4aSLydia Wang 	else if (dev_id == 0x0440)
29236dd5c4aSLydia Wang 		codec_type = VT1708S;
29311890956SLydia Wang 	else if ((dev_id & 0xfff) == 0x446)
29411890956SLydia Wang 		codec_type = VT1802;
295d7426329SHarald Welte 	else
296d7426329SHarald Welte 		codec_type = UNKNOWN;
297d7426329SHarald Welte 	return codec_type;
298d7426329SHarald Welte };
299d7426329SHarald Welte 
300ec7e7e42SLydia Wang #define VIA_JACK_EVENT		0x20
30169e52a80SHarald Welte #define VIA_HP_EVENT		0x01
30269e52a80SHarald Welte #define VIA_GPIO_EVENT		0x02
3034a918ffeSTakashi Iwai #define VIA_LINE_EVENT		0x03
30469e52a80SHarald Welte 
305c577b8a1SJoseph Chan enum {
306c577b8a1SJoseph Chan 	VIA_CTL_WIDGET_VOL,
307c577b8a1SJoseph Chan 	VIA_CTL_WIDGET_MUTE,
308f5271101SLydia Wang 	VIA_CTL_WIDGET_ANALOG_MUTE,
309c577b8a1SJoseph Chan };
310c577b8a1SJoseph Chan 
311ada509ecSTakashi Iwai static void analog_low_current_mode(struct hda_codec *codec);
312ada509ecSTakashi Iwai static bool is_aa_path_mute(struct hda_codec *codec);
3131f2e99feSLydia Wang 
314187d333eSTakashi Iwai #define hp_detect_with_aa(codec) \
315187d333eSTakashi Iwai 	(snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1 && \
316187d333eSTakashi Iwai 	 !is_aa_path_mute(codec))
3171f2e99feSLydia Wang 
3181f2e99feSLydia Wang static void vt1708_stop_hp_work(struct via_spec *spec)
3191f2e99feSLydia Wang {
3201f2e99feSLydia Wang 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
3211f2e99feSLydia Wang 		return;
322187d333eSTakashi Iwai 	if (spec->hp_work_active) {
323187d333eSTakashi Iwai 		snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 1);
3245b84ba26STejun Heo 		cancel_delayed_work_sync(&spec->vt1708_hp_work);
325187d333eSTakashi Iwai 		spec->hp_work_active = 0;
326187d333eSTakashi Iwai 	}
327187d333eSTakashi Iwai }
328187d333eSTakashi Iwai 
329187d333eSTakashi Iwai static void vt1708_update_hp_work(struct via_spec *spec)
330187d333eSTakashi Iwai {
331187d333eSTakashi Iwai 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
332187d333eSTakashi Iwai 		return;
333187d333eSTakashi Iwai 	if (spec->vt1708_jack_detect &&
334187d333eSTakashi Iwai 	    (spec->active_streams || hp_detect_with_aa(spec->codec))) {
335187d333eSTakashi Iwai 		if (!spec->hp_work_active) {
336187d333eSTakashi Iwai 			snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 0);
337187d333eSTakashi Iwai 			schedule_delayed_work(&spec->vt1708_hp_work,
338187d333eSTakashi Iwai 					      msecs_to_jiffies(100));
339187d333eSTakashi Iwai 			spec->hp_work_active = 1;
340187d333eSTakashi Iwai 		}
341187d333eSTakashi Iwai 	} else if (!hp_detect_with_aa(spec->codec))
342187d333eSTakashi Iwai 		vt1708_stop_hp_work(spec);
3431f2e99feSLydia Wang }
344f5271101SLydia Wang 
3453e95b9abSLydia Wang static void set_widgets_power_state(struct hda_codec *codec)
3463e95b9abSLydia Wang {
3473e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
3483e95b9abSLydia Wang 	if (spec->set_widgets_power_state)
3493e95b9abSLydia Wang 		spec->set_widgets_power_state(codec);
3503e95b9abSLydia Wang }
35125eaba2fSLydia Wang 
352f5271101SLydia Wang static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
353f5271101SLydia Wang 				   struct snd_ctl_elem_value *ucontrol)
354f5271101SLydia Wang {
355f5271101SLydia Wang 	int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
356f5271101SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
357f5271101SLydia Wang 
3583e95b9abSLydia Wang 	set_widgets_power_state(codec);
359ada509ecSTakashi Iwai 	analog_low_current_mode(snd_kcontrol_chip(kcontrol));
360187d333eSTakashi Iwai 	vt1708_update_hp_work(codec->spec);
361f5271101SLydia Wang 	return change;
362f5271101SLydia Wang }
363f5271101SLydia Wang 
364f5271101SLydia Wang /* modify .put = snd_hda_mixer_amp_switch_put */
365f5271101SLydia Wang #define ANALOG_INPUT_MUTE						\
366f5271101SLydia Wang 	{		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,		\
367f5271101SLydia Wang 			.name = NULL,					\
368f5271101SLydia Wang 			.index = 0,					\
369f5271101SLydia Wang 			.info = snd_hda_mixer_amp_switch_info,		\
370f5271101SLydia Wang 			.get = snd_hda_mixer_amp_switch_get,		\
371f5271101SLydia Wang 			.put = analog_input_switch_put,			\
372f5271101SLydia Wang 			.private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
373f5271101SLydia Wang 
37490dd48a1STakashi Iwai static const struct snd_kcontrol_new via_control_templates[] = {
375c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME(NULL, 0, 0, 0),
376c577b8a1SJoseph Chan 	HDA_CODEC_MUTE(NULL, 0, 0, 0),
377f5271101SLydia Wang 	ANALOG_INPUT_MUTE,
378c577b8a1SJoseph Chan };
379c577b8a1SJoseph Chan 
380ab6734e7SLydia Wang 
381c577b8a1SJoseph Chan /* add dynamic controls */
382291c9e33STakashi Iwai static struct snd_kcontrol_new *__via_clone_ctl(struct via_spec *spec,
383291c9e33STakashi Iwai 				const struct snd_kcontrol_new *tmpl,
384291c9e33STakashi Iwai 				const char *name)
385c577b8a1SJoseph Chan {
386c577b8a1SJoseph Chan 	struct snd_kcontrol_new *knew;
387c577b8a1SJoseph Chan 
388603c4019STakashi Iwai 	snd_array_init(&spec->kctls, sizeof(*knew), 32);
389603c4019STakashi Iwai 	knew = snd_array_new(&spec->kctls);
390c577b8a1SJoseph Chan 	if (!knew)
391291c9e33STakashi Iwai 		return NULL;
392291c9e33STakashi Iwai 	*knew = *tmpl;
393291c9e33STakashi Iwai 	if (!name)
394291c9e33STakashi Iwai 		name = tmpl->name;
395291c9e33STakashi Iwai 	if (name) {
396c577b8a1SJoseph Chan 		knew->name = kstrdup(name, GFP_KERNEL);
397c577b8a1SJoseph Chan 		if (!knew->name)
398291c9e33STakashi Iwai 			return NULL;
399291c9e33STakashi Iwai 	}
400291c9e33STakashi Iwai 	return knew;
401291c9e33STakashi Iwai }
402291c9e33STakashi Iwai 
403291c9e33STakashi Iwai static int __via_add_control(struct via_spec *spec, int type, const char *name,
404291c9e33STakashi Iwai 			     int idx, unsigned long val)
405291c9e33STakashi Iwai {
406291c9e33STakashi Iwai 	struct snd_kcontrol_new *knew;
407291c9e33STakashi Iwai 
408291c9e33STakashi Iwai 	knew = __via_clone_ctl(spec, &via_control_templates[type], name);
409291c9e33STakashi Iwai 	if (!knew)
410c577b8a1SJoseph Chan 		return -ENOMEM;
411d7a99cceSTakashi Iwai 	knew->index = idx;
4124d02d1b6SJaroslav Kysela 	if (get_amp_nid_(val))
4135e26dfd0SJaroslav Kysela 		knew->subdevice = HDA_SUBDEV_AMP_FLAG;
414c577b8a1SJoseph Chan 	knew->private_value = val;
415c577b8a1SJoseph Chan 	return 0;
416c577b8a1SJoseph Chan }
417c577b8a1SJoseph Chan 
4187b315bb4STakashi Iwai #define via_add_control(spec, type, name, val) \
4197b315bb4STakashi Iwai 	__via_add_control(spec, type, name, 0, val)
4207b315bb4STakashi Iwai 
421291c9e33STakashi Iwai #define via_clone_control(spec, tmpl) __via_clone_ctl(spec, tmpl, NULL)
4225b0cb1d8SJaroslav Kysela 
423603c4019STakashi Iwai static void via_free_kctls(struct hda_codec *codec)
424603c4019STakashi Iwai {
425603c4019STakashi Iwai 	struct via_spec *spec = codec->spec;
426603c4019STakashi Iwai 
427603c4019STakashi Iwai 	if (spec->kctls.list) {
428603c4019STakashi Iwai 		struct snd_kcontrol_new *kctl = spec->kctls.list;
429603c4019STakashi Iwai 		int i;
430603c4019STakashi Iwai 		for (i = 0; i < spec->kctls.used; i++)
431603c4019STakashi Iwai 			kfree(kctl[i].name);
432603c4019STakashi Iwai 	}
433603c4019STakashi Iwai 	snd_array_free(&spec->kctls);
434603c4019STakashi Iwai }
435603c4019STakashi Iwai 
436c577b8a1SJoseph Chan /* create input playback/capture controls for the given pin */
4379510e8ddSLydia Wang static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
4387b315bb4STakashi Iwai 				int type_idx, int idx, int mix_nid)
439c577b8a1SJoseph Chan {
440c577b8a1SJoseph Chan 	char name[32];
441c577b8a1SJoseph Chan 	int err;
442c577b8a1SJoseph Chan 
443c577b8a1SJoseph Chan 	sprintf(name, "%s Playback Volume", ctlname);
4447b315bb4STakashi Iwai 	err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
445c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
446c577b8a1SJoseph Chan 	if (err < 0)
447c577b8a1SJoseph Chan 		return err;
448c577b8a1SJoseph Chan 	sprintf(name, "%s Playback Switch", ctlname);
4497b315bb4STakashi Iwai 	err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
450c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
451c577b8a1SJoseph Chan 	if (err < 0)
452c577b8a1SJoseph Chan 		return err;
453c577b8a1SJoseph Chan 	return 0;
454c577b8a1SJoseph Chan }
455c577b8a1SJoseph Chan 
4565d41762aSTakashi Iwai #define get_connection_index(codec, mux, nid) \
4578d087c76STakashi Iwai 	snd_hda_get_conn_index(codec, mux, nid, 0)
4585d41762aSTakashi Iwai 
4598df2a312STakashi Iwai static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
4608df2a312STakashi Iwai 			   unsigned int mask)
4618df2a312STakashi Iwai {
462a934d5a9STakashi Iwai 	unsigned int caps;
463a934d5a9STakashi Iwai 	if (!nid)
464a934d5a9STakashi Iwai 		return false;
465a934d5a9STakashi Iwai 	caps = get_wcaps(codec, nid);
4668df2a312STakashi Iwai 	if (dir == HDA_INPUT)
4678df2a312STakashi Iwai 		caps &= AC_WCAP_IN_AMP;
4688df2a312STakashi Iwai 	else
4698df2a312STakashi Iwai 		caps &= AC_WCAP_OUT_AMP;
4708df2a312STakashi Iwai 	if (!caps)
4718df2a312STakashi Iwai 		return false;
4728df2a312STakashi Iwai 	if (query_amp_caps(codec, nid, dir) & mask)
4738df2a312STakashi Iwai 		return true;
4748df2a312STakashi Iwai 	return false;
4758df2a312STakashi Iwai }
4768df2a312STakashi Iwai 
47709a9ad69STakashi Iwai #define have_mute(codec, nid, dir) \
47809a9ad69STakashi Iwai 	check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
4798df2a312STakashi Iwai 
480d69607b3SLydia Wang /* enable/disable the output-route mixers */
481d69607b3SLydia Wang static void activate_output_mix(struct hda_codec *codec, struct nid_path *path,
4823214b966STakashi Iwai 				hda_nid_t mix_nid, int idx, bool enable)
483d69607b3SLydia Wang {
484d69607b3SLydia Wang 	int i, num, val;
485d69607b3SLydia Wang 
486d69607b3SLydia Wang 	if (!path)
487d69607b3SLydia Wang 		return;
48809cf03b8STakashi Iwai 	num = snd_hda_get_num_conns(codec, mix_nid);
489d69607b3SLydia Wang 	for (i = 0; i < num; i++) {
4903214b966STakashi Iwai 		if (i == idx)
491d69607b3SLydia Wang 			val = AMP_IN_UNMUTE(i);
492d69607b3SLydia Wang 		else
493d69607b3SLydia Wang 			val = AMP_IN_MUTE(i);
494d69607b3SLydia Wang 		snd_hda_codec_write(codec, mix_nid, 0,
495d69607b3SLydia Wang 				    AC_VERB_SET_AMP_GAIN_MUTE, val);
496d69607b3SLydia Wang 	}
497d69607b3SLydia Wang }
498d69607b3SLydia Wang 
49909a9ad69STakashi Iwai /* enable/disable the output-route */
50009a9ad69STakashi Iwai static void activate_output_path(struct hda_codec *codec, struct nid_path *path,
50109a9ad69STakashi Iwai 				 bool enable, bool force)
5025d41762aSTakashi Iwai {
503d69607b3SLydia Wang 	struct via_spec *spec = codec->spec;
5043214b966STakashi Iwai 	int i;
50509a9ad69STakashi Iwai 	for (i = 0; i < path->depth; i++) {
50609a9ad69STakashi Iwai 		hda_nid_t src, dst;
50709a9ad69STakashi Iwai 		int idx = path->idx[i];
50809a9ad69STakashi Iwai 		src = path->path[i];
50909a9ad69STakashi Iwai 		if (i < path->depth - 1)
51009a9ad69STakashi Iwai 			dst = path->path[i + 1];
51109a9ad69STakashi Iwai 		else
51209a9ad69STakashi Iwai 			dst = 0;
51309a9ad69STakashi Iwai 		if (enable && path->multi[i])
51409a9ad69STakashi Iwai 			snd_hda_codec_write(codec, dst, 0,
5155d41762aSTakashi Iwai 					    AC_VERB_SET_CONNECT_SEL, idx);
5163214b966STakashi Iwai 		if (!force && (dst == spec->aa_mix_nid))
517e5e14681SLydia Wang 			continue;
5183214b966STakashi Iwai 		if (have_mute(codec, dst, HDA_INPUT))
5193214b966STakashi Iwai 			activate_output_mix(codec, path, dst, idx, enable);
52009a9ad69STakashi Iwai 		if (!force && (src == path->vol_ctl || src == path->mute_ctl))
52109a9ad69STakashi Iwai 			continue;
52209a9ad69STakashi Iwai 		if (have_mute(codec, src, HDA_OUTPUT)) {
52309a9ad69STakashi Iwai 			int val = enable ? AMP_OUT_UNMUTE : AMP_OUT_MUTE;
52409a9ad69STakashi Iwai 			snd_hda_codec_write(codec, src, 0,
52509a9ad69STakashi Iwai 					    AC_VERB_SET_AMP_GAIN_MUTE, val);
52609a9ad69STakashi Iwai 		}
52709a9ad69STakashi Iwai 	}
5285d41762aSTakashi Iwai }
5295d41762aSTakashi Iwai 
5305d41762aSTakashi Iwai /* set the given pin as output */
5315d41762aSTakashi Iwai static void init_output_pin(struct hda_codec *codec, hda_nid_t pin,
5325d41762aSTakashi Iwai 			    int pin_type)
5335d41762aSTakashi Iwai {
5345d41762aSTakashi Iwai 	if (!pin)
5355d41762aSTakashi Iwai 		return;
536cdd03cedSTakashi Iwai 	snd_hda_set_pin_ctl(codec, pin, pin_type);
5375d41762aSTakashi Iwai 	if (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD)
5385d41762aSTakashi Iwai 		snd_hda_codec_write(codec, pin, 0,
539d3a11e60STakashi Iwai 				    AC_VERB_SET_EAPD_BTLENABLE, 0x02);
540c577b8a1SJoseph Chan }
541c577b8a1SJoseph Chan 
54209a9ad69STakashi Iwai static void via_auto_init_output(struct hda_codec *codec,
543a353fbb1STakashi Iwai 				 struct nid_path *path, int pin_type)
5445d41762aSTakashi Iwai {
5455d41762aSTakashi Iwai 	unsigned int caps;
546d69607b3SLydia Wang 	hda_nid_t pin;
5475d41762aSTakashi Iwai 
54809a9ad69STakashi Iwai 	if (!path->depth)
5495d41762aSTakashi Iwai 		return;
55009a9ad69STakashi Iwai 	pin = path->path[path->depth - 1];
5515d41762aSTakashi Iwai 
5525d41762aSTakashi Iwai 	init_output_pin(codec, pin, pin_type);
55377e314f7STakashi Iwai 	if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
5545d41762aSTakashi Iwai 		caps = query_amp_caps(codec, pin, HDA_OUTPUT);
55577e314f7STakashi Iwai 	else
55677e314f7STakashi Iwai 		caps = 0;
5575d41762aSTakashi Iwai 	if (caps & AC_AMPCAP_MUTE) {
5585d41762aSTakashi Iwai 		unsigned int val;
5595d41762aSTakashi Iwai 		val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
5605d41762aSTakashi Iwai 		snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
5615d41762aSTakashi Iwai 				    AMP_OUT_MUTE | val);
5625d41762aSTakashi Iwai 	}
563a353fbb1STakashi Iwai 	activate_output_path(codec, path, true, true); /* force on */
56409a9ad69STakashi Iwai }
565c577b8a1SJoseph Chan 
566c577b8a1SJoseph Chan static void via_auto_init_multi_out(struct hda_codec *codec)
567c577b8a1SJoseph Chan {
568c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
5693214b966STakashi Iwai 	struct nid_path *path;
570c577b8a1SJoseph Chan 	int i;
571c577b8a1SJoseph Chan 
5723214b966STakashi Iwai 	for (i = 0; i < spec->autocfg.line_outs + spec->smart51_nums; i++) {
5733214b966STakashi Iwai 		path = &spec->out_path[i];
5743214b966STakashi Iwai 		if (!i && spec->aamix_mode && spec->out_mix_path.depth)
5753214b966STakashi Iwai 			path = &spec->out_mix_path;
576a353fbb1STakashi Iwai 		via_auto_init_output(codec, path, PIN_OUT);
5773214b966STakashi Iwai 	}
578c577b8a1SJoseph Chan }
579c577b8a1SJoseph Chan 
580020066d1STakashi Iwai /* deactivate the inactive headphone-paths */
581020066d1STakashi Iwai static void deactivate_hp_paths(struct hda_codec *codec)
582c577b8a1SJoseph Chan {
583c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
5843214b966STakashi Iwai 	int shared = spec->hp_indep_shared;
585c577b8a1SJoseph Chan 
58609a9ad69STakashi Iwai 	if (spec->hp_independent_mode) {
58709a9ad69STakashi Iwai 		activate_output_path(codec, &spec->hp_path, false, false);
5883214b966STakashi Iwai 		activate_output_path(codec, &spec->hp_mix_path, false, false);
5893214b966STakashi Iwai 		if (shared)
5903214b966STakashi Iwai 			activate_output_path(codec, &spec->out_path[shared],
5913214b966STakashi Iwai 					     false, false);
592020066d1STakashi Iwai 	} else if (spec->aamix_mode || !spec->hp_path.depth) {
593020066d1STakashi Iwai 		activate_output_path(codec, &spec->hp_indep_path, false, false);
5943214b966STakashi Iwai 		activate_output_path(codec, &spec->hp_path, false, false);
5953214b966STakashi Iwai 	} else {
596020066d1STakashi Iwai 		activate_output_path(codec, &spec->hp_indep_path, false, false);
5973214b966STakashi Iwai 		activate_output_path(codec, &spec->hp_mix_path, false, false);
59809a9ad69STakashi Iwai 	}
59925eaba2fSLydia Wang }
600c577b8a1SJoseph Chan 
601020066d1STakashi Iwai static void via_auto_init_hp_out(struct hda_codec *codec)
602020066d1STakashi Iwai {
603020066d1STakashi Iwai 	struct via_spec *spec = codec->spec;
604020066d1STakashi Iwai 
605020066d1STakashi Iwai 	if (!spec->hp_path.depth) {
606a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
607020066d1STakashi Iwai 		return;
608020066d1STakashi Iwai 	}
609020066d1STakashi Iwai 	deactivate_hp_paths(codec);
610020066d1STakashi Iwai 	if (spec->hp_independent_mode)
611a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->hp_indep_path, PIN_HP);
612020066d1STakashi Iwai 	else if (spec->aamix_mode)
613a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
614020066d1STakashi Iwai 	else
615a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->hp_path, PIN_HP);
616020066d1STakashi Iwai }
617020066d1STakashi Iwai 
6184a918ffeSTakashi Iwai static void via_auto_init_speaker_out(struct hda_codec *codec)
6194a918ffeSTakashi Iwai {
6204a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
6214a918ffeSTakashi Iwai 
6223214b966STakashi Iwai 	if (!spec->autocfg.speaker_outs)
6233214b966STakashi Iwai 		return;
6243214b966STakashi Iwai 	if (!spec->speaker_path.depth) {
625a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
6263214b966STakashi Iwai 		return;
6273214b966STakashi Iwai 	}
6283214b966STakashi Iwai 	if (!spec->aamix_mode) {
6293214b966STakashi Iwai 		activate_output_path(codec, &spec->speaker_mix_path,
6303214b966STakashi Iwai 				     false, false);
631a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->speaker_path, PIN_OUT);
6323214b966STakashi Iwai 	} else {
6333214b966STakashi Iwai 		activate_output_path(codec, &spec->speaker_path, false, false);
634a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
6353214b966STakashi Iwai 	}
6364a918ffeSTakashi Iwai }
6374a918ffeSTakashi Iwai 
638f4a7828bSTakashi Iwai static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin);
6396e969d91STakashi Iwai static void via_hp_automute(struct hda_codec *codec);
64032e0191dSClemens Ladisch 
641c577b8a1SJoseph Chan static void via_auto_init_analog_input(struct hda_codec *codec)
642c577b8a1SJoseph Chan {
643c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
6447b315bb4STakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
645096a8854STakashi Iwai 	hda_nid_t conn[HDA_MAX_CONNECTIONS];
64632e0191dSClemens Ladisch 	unsigned int ctl;
647096a8854STakashi Iwai 	int i, num_conns;
648c577b8a1SJoseph Chan 
649096a8854STakashi Iwai 	/* init ADCs */
650096a8854STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
65177e314f7STakashi Iwai 		hda_nid_t nid = spec->adc_nids[i];
65277e314f7STakashi Iwai 		if (!(get_wcaps(codec, nid) & AC_WCAP_IN_AMP) ||
65377e314f7STakashi Iwai 		    !(query_amp_caps(codec, nid, HDA_INPUT) & AC_AMPCAP_MUTE))
65477e314f7STakashi Iwai 			continue;
655096a8854STakashi Iwai 		snd_hda_codec_write(codec, spec->adc_nids[i], 0,
656096a8854STakashi Iwai 				    AC_VERB_SET_AMP_GAIN_MUTE,
657096a8854STakashi Iwai 				    AMP_IN_UNMUTE(0));
658096a8854STakashi Iwai 	}
659096a8854STakashi Iwai 
660096a8854STakashi Iwai 	/* init pins */
6617b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
6627b315bb4STakashi Iwai 		hda_nid_t nid = cfg->inputs[i].pin;
663f4a7828bSTakashi Iwai 		if (spec->smart51_enabled && is_smart51_pins(codec, nid))
66432e0191dSClemens Ladisch 			ctl = PIN_OUT;
6654740860bSTakashi Iwai 		else {
66632e0191dSClemens Ladisch 			ctl = PIN_IN;
6674740860bSTakashi Iwai 			if (cfg->inputs[i].type == AUTO_PIN_MIC)
6684740860bSTakashi Iwai 				ctl |= snd_hda_get_default_vref(codec, nid);
6694740860bSTakashi Iwai 		}
670cdd03cedSTakashi Iwai 		snd_hda_set_pin_ctl(codec, nid, ctl);
671c577b8a1SJoseph Chan 	}
672096a8854STakashi Iwai 
673096a8854STakashi Iwai 	/* init input-src */
674096a8854STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
675a86a88eaSTakashi Iwai 		int adc_idx = spec->inputs[spec->cur_mux[i]].adc_idx;
676fc1156c0STakashi Iwai 		/* secondary ADCs must have the unique MUX */
677fc1156c0STakashi Iwai 		if (i > 0 && !spec->mux_nids[i])
678fc1156c0STakashi Iwai 			break;
679a86a88eaSTakashi Iwai 		if (spec->mux_nids[adc_idx]) {
680a86a88eaSTakashi Iwai 			int mux_idx = spec->inputs[spec->cur_mux[i]].mux_idx;
681a86a88eaSTakashi Iwai 			snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
682096a8854STakashi Iwai 					    AC_VERB_SET_CONNECT_SEL,
683a86a88eaSTakashi Iwai 					    mux_idx);
684a86a88eaSTakashi Iwai 		}
685a86a88eaSTakashi Iwai 		if (spec->dyn_adc_switch)
686a86a88eaSTakashi Iwai 			break; /* only one input-src */
687096a8854STakashi Iwai 	}
688096a8854STakashi Iwai 
689096a8854STakashi Iwai 	/* init aa-mixer */
690096a8854STakashi Iwai 	if (!spec->aa_mix_nid)
691096a8854STakashi Iwai 		return;
692096a8854STakashi Iwai 	num_conns = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
693096a8854STakashi Iwai 					    ARRAY_SIZE(conn));
694096a8854STakashi Iwai 	for (i = 0; i < num_conns; i++) {
695096a8854STakashi Iwai 		unsigned int caps = get_wcaps(codec, conn[i]);
696096a8854STakashi Iwai 		if (get_wcaps_type(caps) == AC_WID_PIN)
697096a8854STakashi Iwai 			snd_hda_codec_write(codec, spec->aa_mix_nid, 0,
698096a8854STakashi Iwai 					    AC_VERB_SET_AMP_GAIN_MUTE,
699096a8854STakashi Iwai 					    AMP_IN_MUTE(i));
700096a8854STakashi Iwai 	}
701c577b8a1SJoseph Chan }
702f5271101SLydia Wang 
703054d867eSTakashi Iwai static void update_power_state(struct hda_codec *codec, hda_nid_t nid,
704054d867eSTakashi Iwai 			       unsigned int parm)
705054d867eSTakashi Iwai {
706054d867eSTakashi Iwai 	if (snd_hda_codec_read(codec, nid, 0,
707054d867eSTakashi Iwai 			       AC_VERB_GET_POWER_STATE, 0) == parm)
708054d867eSTakashi Iwai 		return;
709054d867eSTakashi Iwai 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
710054d867eSTakashi Iwai }
711054d867eSTakashi Iwai 
712f5271101SLydia Wang static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
713f5271101SLydia Wang 				unsigned int *affected_parm)
714f5271101SLydia Wang {
715f5271101SLydia Wang 	unsigned parm;
716f5271101SLydia Wang 	unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
717f5271101SLydia Wang 	unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
718f5271101SLydia Wang 		>> AC_DEFCFG_MISC_SHIFT
719f5271101SLydia Wang 		& AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
7201564b287SLydia Wang 	struct via_spec *spec = codec->spec;
72124088a58STakashi Iwai 	unsigned present = 0;
72224088a58STakashi Iwai 
72324088a58STakashi Iwai 	no_presence |= spec->no_pin_power_ctl;
72424088a58STakashi Iwai 	if (!no_presence)
72524088a58STakashi Iwai 		present = snd_hda_jack_detect(codec, nid);
726f4a7828bSTakashi Iwai 	if ((spec->smart51_enabled && is_smart51_pins(codec, nid))
7271564b287SLydia Wang 	    || ((no_presence || present)
7281564b287SLydia Wang 		&& get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
729f5271101SLydia Wang 		*affected_parm = AC_PWRST_D0; /* if it's connected */
730f5271101SLydia Wang 		parm = AC_PWRST_D0;
731f5271101SLydia Wang 	} else
732f5271101SLydia Wang 		parm = AC_PWRST_D3;
733f5271101SLydia Wang 
734054d867eSTakashi Iwai 	update_power_state(codec, nid, parm);
735f5271101SLydia Wang }
736f5271101SLydia Wang 
73724088a58STakashi Iwai static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
73824088a58STakashi Iwai 				  struct snd_ctl_elem_info *uinfo)
73924088a58STakashi Iwai {
74024088a58STakashi Iwai 	static const char * const texts[] = {
74124088a58STakashi Iwai 		"Disabled", "Enabled"
74224088a58STakashi Iwai 	};
74324088a58STakashi Iwai 
74424088a58STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
74524088a58STakashi Iwai 	uinfo->count = 1;
74624088a58STakashi Iwai 	uinfo->value.enumerated.items = 2;
74724088a58STakashi Iwai 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
74824088a58STakashi Iwai 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
74924088a58STakashi Iwai 	strcpy(uinfo->value.enumerated.name,
75024088a58STakashi Iwai 	       texts[uinfo->value.enumerated.item]);
75124088a58STakashi Iwai 	return 0;
75224088a58STakashi Iwai }
75324088a58STakashi Iwai 
75424088a58STakashi Iwai static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
75524088a58STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
75624088a58STakashi Iwai {
75724088a58STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
75824088a58STakashi Iwai 	struct via_spec *spec = codec->spec;
75924088a58STakashi Iwai 	ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
76024088a58STakashi Iwai 	return 0;
76124088a58STakashi Iwai }
76224088a58STakashi Iwai 
76324088a58STakashi Iwai static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
76424088a58STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
76524088a58STakashi Iwai {
76624088a58STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
76724088a58STakashi Iwai 	struct via_spec *spec = codec->spec;
76824088a58STakashi Iwai 	unsigned int val = !ucontrol->value.enumerated.item[0];
76924088a58STakashi Iwai 
77024088a58STakashi Iwai 	if (val == spec->no_pin_power_ctl)
77124088a58STakashi Iwai 		return 0;
77224088a58STakashi Iwai 	spec->no_pin_power_ctl = val;
77324088a58STakashi Iwai 	set_widgets_power_state(codec);
774e9d010c2STakashi Iwai 	analog_low_current_mode(codec);
77524088a58STakashi Iwai 	return 1;
77624088a58STakashi Iwai }
77724088a58STakashi Iwai 
77824088a58STakashi Iwai static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
77924088a58STakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
78024088a58STakashi Iwai 	.name = "Dynamic Power-Control",
78124088a58STakashi Iwai 	.info = via_pin_power_ctl_info,
78224088a58STakashi Iwai 	.get = via_pin_power_ctl_get,
78324088a58STakashi Iwai 	.put = via_pin_power_ctl_put,
78424088a58STakashi Iwai };
78524088a58STakashi Iwai 
78624088a58STakashi Iwai 
7870aa62aefSHarald Welte static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
7880aa62aefSHarald Welte 				   struct snd_ctl_elem_info *uinfo)
7890aa62aefSHarald Welte {
7908df2a312STakashi Iwai 	static const char * const texts[] = { "OFF", "ON" };
7918df2a312STakashi Iwai 
7928df2a312STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
7938df2a312STakashi Iwai 	uinfo->count = 1;
7948df2a312STakashi Iwai 	uinfo->value.enumerated.items = 2;
7958df2a312STakashi Iwai 	if (uinfo->value.enumerated.item >= 2)
7968df2a312STakashi Iwai 		uinfo->value.enumerated.item = 1;
7978df2a312STakashi Iwai 	strcpy(uinfo->value.enumerated.name,
7988df2a312STakashi Iwai 	       texts[uinfo->value.enumerated.item]);
7998df2a312STakashi Iwai 	return 0;
8000aa62aefSHarald Welte }
8010aa62aefSHarald Welte 
8020aa62aefSHarald Welte static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
8030aa62aefSHarald Welte 				  struct snd_ctl_elem_value *ucontrol)
8040aa62aefSHarald Welte {
8050aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
806cdc1784dSLydia Wang 	struct via_spec *spec = codec->spec;
807cdc1784dSLydia Wang 
808ece8d043STakashi Iwai 	ucontrol->value.enumerated.item[0] = spec->hp_independent_mode;
809cdc1784dSLydia Wang 	return 0;
810cdc1784dSLydia Wang }
811cdc1784dSLydia Wang 
8123b607e3dSTakashi Iwai /* adjust spec->multiout setup according to the current flags */
8133b607e3dSTakashi Iwai static void setup_playback_multi_pcm(struct via_spec *spec)
8143b607e3dSTakashi Iwai {
8153b607e3dSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
8163b607e3dSTakashi Iwai 	spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
8173b607e3dSTakashi Iwai 	spec->multiout.hp_nid = 0;
8183b607e3dSTakashi Iwai 	if (!spec->hp_independent_mode) {
8193b607e3dSTakashi Iwai 		if (!spec->hp_indep_shared)
8203b607e3dSTakashi Iwai 			spec->multiout.hp_nid = spec->hp_dac_nid;
8213b607e3dSTakashi Iwai 	} else {
8223b607e3dSTakashi Iwai 		if (spec->hp_indep_shared)
8233b607e3dSTakashi Iwai 			spec->multiout.num_dacs = cfg->line_outs - 1;
8243b607e3dSTakashi Iwai 	}
8253b607e3dSTakashi Iwai }
8263b607e3dSTakashi Iwai 
8273b607e3dSTakashi Iwai /* update DAC setups according to indep-HP switch;
8283b607e3dSTakashi Iwai  * this function is called only when indep-HP is modified
8293b607e3dSTakashi Iwai  */
8303b607e3dSTakashi Iwai static void switch_indep_hp_dacs(struct hda_codec *codec)
8313b607e3dSTakashi Iwai {
8323b607e3dSTakashi Iwai 	struct via_spec *spec = codec->spec;
8333b607e3dSTakashi Iwai 	int shared = spec->hp_indep_shared;
8343b607e3dSTakashi Iwai 	hda_nid_t shared_dac, hp_dac;
8353b607e3dSTakashi Iwai 
8363b607e3dSTakashi Iwai 	if (!spec->opened_streams)
8373b607e3dSTakashi Iwai 		return;
8383b607e3dSTakashi Iwai 
8393b607e3dSTakashi Iwai 	shared_dac = shared ? spec->multiout.dac_nids[shared] : 0;
8403b607e3dSTakashi Iwai 	hp_dac = spec->hp_dac_nid;
8413b607e3dSTakashi Iwai 	if (spec->hp_independent_mode) {
8423b607e3dSTakashi Iwai 		/* switch to indep-HP mode */
8433b607e3dSTakashi Iwai 		if (spec->active_streams & STREAM_MULTI_OUT) {
8443b607e3dSTakashi Iwai 			__snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
8453b607e3dSTakashi Iwai 			__snd_hda_codec_cleanup_stream(codec, shared_dac, 1);
8463b607e3dSTakashi Iwai 		}
8473b607e3dSTakashi Iwai 		if (spec->active_streams & STREAM_INDEP_HP)
8483b607e3dSTakashi Iwai 			snd_hda_codec_setup_stream(codec, hp_dac,
8493b607e3dSTakashi Iwai 						   spec->cur_hp_stream_tag, 0,
8503b607e3dSTakashi Iwai 						   spec->cur_hp_format);
8513b607e3dSTakashi Iwai 	} else {
8523b607e3dSTakashi Iwai 		/* back to HP or shared-DAC */
8533b607e3dSTakashi Iwai 		if (spec->active_streams & STREAM_INDEP_HP)
8543b607e3dSTakashi Iwai 			__snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
8553b607e3dSTakashi Iwai 		if (spec->active_streams & STREAM_MULTI_OUT) {
8563b607e3dSTakashi Iwai 			hda_nid_t dac;
8573b607e3dSTakashi Iwai 			int ch;
8583b607e3dSTakashi Iwai 			if (shared_dac) { /* reset mutli-ch DAC */
8593b607e3dSTakashi Iwai 				dac = shared_dac;
8603b607e3dSTakashi Iwai 				ch = shared * 2;
8613b607e3dSTakashi Iwai 			} else { /* reset HP DAC */
8623b607e3dSTakashi Iwai 				dac = hp_dac;
8633b607e3dSTakashi Iwai 				ch = 0;
8643b607e3dSTakashi Iwai 			}
8653b607e3dSTakashi Iwai 			snd_hda_codec_setup_stream(codec, dac,
8663b607e3dSTakashi Iwai 						   spec->cur_dac_stream_tag, ch,
8673b607e3dSTakashi Iwai 						   spec->cur_dac_format);
8683b607e3dSTakashi Iwai 		}
8693b607e3dSTakashi Iwai 	}
8703b607e3dSTakashi Iwai 	setup_playback_multi_pcm(spec);
8713b607e3dSTakashi Iwai }
8723b607e3dSTakashi Iwai 
8730aa62aefSHarald Welte static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
8740aa62aefSHarald Welte 				  struct snd_ctl_elem_value *ucontrol)
8750aa62aefSHarald Welte {
8760aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
8770aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
8783214b966STakashi Iwai 	int cur, shared;
8798df2a312STakashi Iwai 
8803b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
88125250505STakashi Iwai 	cur = !!ucontrol->value.enumerated.item[0];
8823b607e3dSTakashi Iwai 	if (spec->hp_independent_mode == cur) {
8833b607e3dSTakashi Iwai 		mutex_unlock(&spec->config_mutex);
88425250505STakashi Iwai 		return 0;
8853b607e3dSTakashi Iwai 	}
88625250505STakashi Iwai 	spec->hp_independent_mode = cur;
8873214b966STakashi Iwai 	shared = spec->hp_indep_shared;
888020066d1STakashi Iwai 	deactivate_hp_paths(codec);
889020066d1STakashi Iwai 	if (cur)
890020066d1STakashi Iwai 		activate_output_path(codec, &spec->hp_indep_path, true, false);
891020066d1STakashi Iwai 	else {
8923214b966STakashi Iwai 		if (shared)
8933214b966STakashi Iwai 			activate_output_path(codec, &spec->out_path[shared],
89425250505STakashi Iwai 					     true, false);
895020066d1STakashi Iwai 		if (spec->aamix_mode || !spec->hp_path.depth)
896020066d1STakashi Iwai 			activate_output_path(codec, &spec->hp_mix_path,
897020066d1STakashi Iwai 					     true, false);
898020066d1STakashi Iwai 		else
899020066d1STakashi Iwai 			activate_output_path(codec, &spec->hp_path,
900020066d1STakashi Iwai 					     true, false);
9018df2a312STakashi Iwai 	}
9020aa62aefSHarald Welte 
9033b607e3dSTakashi Iwai 	switch_indep_hp_dacs(codec);
9043b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
9053b607e3dSTakashi Iwai 
906ce0e5a9eSLydia Wang 	/* update jack power state */
9073e95b9abSLydia Wang 	set_widgets_power_state(codec);
9086e969d91STakashi Iwai 	via_hp_automute(codec);
90925250505STakashi Iwai 	return 1;
9100aa62aefSHarald Welte }
9110aa62aefSHarald Welte 
912ece8d043STakashi Iwai static const struct snd_kcontrol_new via_hp_mixer = {
9130aa62aefSHarald Welte 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
9140aa62aefSHarald Welte 	.name = "Independent HP",
9150aa62aefSHarald Welte 	.info = via_independent_hp_info,
9160aa62aefSHarald Welte 	.get = via_independent_hp_get,
9170aa62aefSHarald Welte 	.put = via_independent_hp_put,
9180aa62aefSHarald Welte };
9190aa62aefSHarald Welte 
9203d83e577STakashi Iwai static int via_hp_build(struct hda_codec *codec)
9215b0cb1d8SJaroslav Kysela {
9223d83e577STakashi Iwai 	struct via_spec *spec = codec->spec;
9235b0cb1d8SJaroslav Kysela 	struct snd_kcontrol_new *knew;
9245b0cb1d8SJaroslav Kysela 	hda_nid_t nid;
9255b0cb1d8SJaroslav Kysela 
9265b0cb1d8SJaroslav Kysela 	nid = spec->autocfg.hp_pins[0];
927ece8d043STakashi Iwai 	knew = via_clone_control(spec, &via_hp_mixer);
9283d83e577STakashi Iwai 	if (knew == NULL)
9293d83e577STakashi Iwai 		return -ENOMEM;
9303d83e577STakashi Iwai 
9315b0cb1d8SJaroslav Kysela 	knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
9325b0cb1d8SJaroslav Kysela 
9335b0cb1d8SJaroslav Kysela 	return 0;
9345b0cb1d8SJaroslav Kysela }
9355b0cb1d8SJaroslav Kysela 
9361564b287SLydia Wang static void notify_aa_path_ctls(struct hda_codec *codec)
9371564b287SLydia Wang {
938e3d7a143STakashi Iwai 	struct via_spec *spec = codec->spec;
9391564b287SLydia Wang 	int i;
9401564b287SLydia Wang 
941e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
942e3d7a143STakashi Iwai 		struct snd_kcontrol *ctl;
943e3d7a143STakashi Iwai 		struct snd_ctl_elem_id id;
9441564b287SLydia Wang 		memset(&id, 0, sizeof(id));
9451564b287SLydia Wang 		id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
946e3d7a143STakashi Iwai 		sprintf(id.name, "%s Playback Volume", spec->smart51_labels[i]);
947525566cbSLydia Wang 		ctl = snd_hda_find_mixer_ctl(codec, id.name);
948525566cbSLydia Wang 		if (ctl)
949525566cbSLydia Wang 			snd_ctl_notify(codec->bus->card,
950525566cbSLydia Wang 					SNDRV_CTL_EVENT_MASK_VALUE,
951525566cbSLydia Wang 					&ctl->id);
9521564b287SLydia Wang 	}
9531564b287SLydia Wang }
9541564b287SLydia Wang 
9551564b287SLydia Wang static void mute_aa_path(struct hda_codec *codec, int mute)
9561564b287SLydia Wang {
9571564b287SLydia Wang 	struct via_spec *spec = codec->spec;
9581564b287SLydia Wang 	int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
959e3d7a143STakashi Iwai 	int i;
960e3d7a143STakashi Iwai 
961e3d7a143STakashi Iwai 	/* check AA path's mute status */
962e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
963e3d7a143STakashi Iwai 		if (spec->smart51_idxs[i] < 0)
964e3d7a143STakashi Iwai 			continue;
965e3d7a143STakashi Iwai 		snd_hda_codec_amp_stereo(codec, spec->aa_mix_nid,
966e3d7a143STakashi Iwai 					 HDA_INPUT, spec->smart51_idxs[i],
9671564b287SLydia Wang 					 HDA_AMP_MUTE, val);
9681564b287SLydia Wang 	}
9691564b287SLydia Wang }
970f4a7828bSTakashi Iwai 
971e3d7a143STakashi Iwai static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
972e3d7a143STakashi Iwai {
973e3d7a143STakashi Iwai 	struct via_spec *spec = codec->spec;
974e3d7a143STakashi Iwai 	int i;
975e3d7a143STakashi Iwai 
976e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++)
977e3d7a143STakashi Iwai 		if (spec->smart51_pins[i] == pin)
978e3d7a143STakashi Iwai 			return true;
979e3d7a143STakashi Iwai 	return false;
980e3d7a143STakashi Iwai }
981e3d7a143STakashi Iwai 
9821564b287SLydia Wang static int via_smart51_get(struct snd_kcontrol *kcontrol,
9831564b287SLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
9841564b287SLydia Wang {
9851564b287SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
9861564b287SLydia Wang 	struct via_spec *spec = codec->spec;
9871564b287SLydia Wang 
988f2b1c9f0STakashi Iwai 	*ucontrol->value.integer.value = spec->smart51_enabled;
9891564b287SLydia Wang 	return 0;
9901564b287SLydia Wang }
9911564b287SLydia Wang 
9921564b287SLydia Wang static int via_smart51_put(struct snd_kcontrol *kcontrol,
9931564b287SLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
9941564b287SLydia Wang {
9951564b287SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
9961564b287SLydia Wang 	struct via_spec *spec = codec->spec;
9971564b287SLydia Wang 	int out_in = *ucontrol->value.integer.value
9981564b287SLydia Wang 		? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
9991564b287SLydia Wang 	int i;
10001564b287SLydia Wang 
1001e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
1002e3d7a143STakashi Iwai 		hda_nid_t nid = spec->smart51_pins[i];
10037b315bb4STakashi Iwai 		unsigned int parm;
10047b315bb4STakashi Iwai 
10057b315bb4STakashi Iwai 		parm = snd_hda_codec_read(codec, nid, 0,
10061564b287SLydia Wang 					  AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
10071564b287SLydia Wang 		parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
10081564b287SLydia Wang 		parm |= out_in;
1009cdd03cedSTakashi Iwai 		snd_hda_set_pin_ctl(codec, nid, parm);
10101564b287SLydia Wang 		if (out_in == AC_PINCTL_OUT_EN) {
10111564b287SLydia Wang 			mute_aa_path(codec, 1);
10121564b287SLydia Wang 			notify_aa_path_ctls(codec);
10131564b287SLydia Wang 		}
10141564b287SLydia Wang 	}
10151564b287SLydia Wang 	spec->smart51_enabled = *ucontrol->value.integer.value;
10163e95b9abSLydia Wang 	set_widgets_power_state(codec);
10171564b287SLydia Wang 	return 1;
10181564b287SLydia Wang }
10191564b287SLydia Wang 
10205f4b36d6STakashi Iwai static const struct snd_kcontrol_new via_smart51_mixer = {
10211564b287SLydia Wang 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
10221564b287SLydia Wang 	.name = "Smart 5.1",
10231564b287SLydia Wang 	.count = 1,
1024f2b1c9f0STakashi Iwai 	.info = snd_ctl_boolean_mono_info,
10251564b287SLydia Wang 	.get = via_smart51_get,
10261564b287SLydia Wang 	.put = via_smart51_put,
10271564b287SLydia Wang };
10281564b287SLydia Wang 
1029f4a7828bSTakashi Iwai static int via_smart51_build(struct hda_codec *codec)
10305b0cb1d8SJaroslav Kysela {
1031f4a7828bSTakashi Iwai 	struct via_spec *spec = codec->spec;
10325b0cb1d8SJaroslav Kysela 
1033e3d7a143STakashi Iwai 	if (!spec->smart51_nums)
1034cb34c207SLydia Wang 		return 0;
1035e3d7a143STakashi Iwai 	if (!via_clone_control(spec, &via_smart51_mixer))
10365b0cb1d8SJaroslav Kysela 		return -ENOMEM;
10375b0cb1d8SJaroslav Kysela 	return 0;
10385b0cb1d8SJaroslav Kysela }
10395b0cb1d8SJaroslav Kysela 
1040f5271101SLydia Wang /* check AA path's mute status */
1041ada509ecSTakashi Iwai static bool is_aa_path_mute(struct hda_codec *codec)
1042ada509ecSTakashi Iwai {
1043ada509ecSTakashi Iwai 	struct via_spec *spec = codec->spec;
1044ada509ecSTakashi Iwai 	const struct hda_amp_list *p;
1045ada509ecSTakashi Iwai 	int i, ch, v;
1046ada509ecSTakashi Iwai 
1047ada509ecSTakashi Iwai 	for (i = 0; i < spec->num_loopbacks; i++) {
1048ada509ecSTakashi Iwai 		p = &spec->loopback_list[i];
1049ada509ecSTakashi Iwai 		for (ch = 0; ch < 2; ch++) {
1050ada509ecSTakashi Iwai 			v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
1051ada509ecSTakashi Iwai 						   p->idx);
1052ada509ecSTakashi Iwai 			if (!(v & HDA_AMP_MUTE) && v > 0)
1053ada509ecSTakashi Iwai 				return false;
1054f5271101SLydia Wang 		}
1055f5271101SLydia Wang 	}
1056ada509ecSTakashi Iwai 	return true;
1057f5271101SLydia Wang }
1058f5271101SLydia Wang 
1059f5271101SLydia Wang /* enter/exit analog low-current mode */
1060e9d010c2STakashi Iwai static void __analog_low_current_mode(struct hda_codec *codec, bool force)
1061f5271101SLydia Wang {
1062f5271101SLydia Wang 	struct via_spec *spec = codec->spec;
1063ada509ecSTakashi Iwai 	bool enable;
1064ada509ecSTakashi Iwai 	unsigned int verb, parm;
1065f5271101SLydia Wang 
1066e9d010c2STakashi Iwai 	if (spec->no_pin_power_ctl)
1067e9d010c2STakashi Iwai 		enable = false;
1068e9d010c2STakashi Iwai 	else
106992433923STakashi Iwai 		enable = is_aa_path_mute(codec) && !spec->opened_streams;
1070e9d010c2STakashi Iwai 	if (enable == spec->alc_mode && !force)
1071e9d010c2STakashi Iwai 		return;
1072e9d010c2STakashi Iwai 	spec->alc_mode = enable;
1073f5271101SLydia Wang 
1074f5271101SLydia Wang 	/* decide low current mode's verb & parameter */
1075f5271101SLydia Wang 	switch (spec->codec_type) {
1076f5271101SLydia Wang 	case VT1708B_8CH:
1077f5271101SLydia Wang 	case VT1708B_4CH:
1078f5271101SLydia Wang 		verb = 0xf70;
1079f5271101SLydia Wang 		parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
1080f5271101SLydia Wang 		break;
1081f5271101SLydia Wang 	case VT1708S:
1082eb7188caSLydia Wang 	case VT1718S:
1083f3db423dSLydia Wang 	case VT1716S:
1084f5271101SLydia Wang 		verb = 0xf73;
1085f5271101SLydia Wang 		parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
1086f5271101SLydia Wang 		break;
1087f5271101SLydia Wang 	case VT1702:
1088f5271101SLydia Wang 		verb = 0xf73;
1089f5271101SLydia Wang 		parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
1090f5271101SLydia Wang 		break;
109125eaba2fSLydia Wang 	case VT2002P:
1092ab6734e7SLydia Wang 	case VT1812:
109311890956SLydia Wang 	case VT1802:
109425eaba2fSLydia Wang 		verb = 0xf93;
109525eaba2fSLydia Wang 		parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
109625eaba2fSLydia Wang 		break;
1097f5271101SLydia Wang 	default:
1098f5271101SLydia Wang 		return;		/* other codecs are not supported */
1099f5271101SLydia Wang 	}
1100f5271101SLydia Wang 	/* send verb */
1101f5271101SLydia Wang 	snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
1102f5271101SLydia Wang }
1103f5271101SLydia Wang 
1104e9d010c2STakashi Iwai static void analog_low_current_mode(struct hda_codec *codec)
1105e9d010c2STakashi Iwai {
1106e9d010c2STakashi Iwai 	return __analog_low_current_mode(codec, false);
1107e9d010c2STakashi Iwai }
1108e9d010c2STakashi Iwai 
1109c577b8a1SJoseph Chan /*
1110c577b8a1SJoseph Chan  * generic initialization of ADC, input mixers and output mixers
1111c577b8a1SJoseph Chan  */
1112096a8854STakashi Iwai static const struct hda_verb vt1708_init_verbs[] = {
1113aa266fccSLydia Wang 	/* power down jack detect function */
1114aa266fccSLydia Wang 	{0x1, 0xf81, 0x1},
1115f7278fd0SJosepch Chan 	{ }
1116c577b8a1SJoseph Chan };
1117c577b8a1SJoseph Chan 
11183b607e3dSTakashi Iwai static void set_stream_open(struct hda_codec *codec, int bit, bool active)
11197eb56e84STakashi Iwai {
1120ada509ecSTakashi Iwai 	struct via_spec *spec = codec->spec;
1121ada509ecSTakashi Iwai 
1122ada509ecSTakashi Iwai 	if (active)
11233b607e3dSTakashi Iwai 		spec->opened_streams |= bit;
1124ada509ecSTakashi Iwai 	else
11253b607e3dSTakashi Iwai 		spec->opened_streams &= ~bit;
1126ada509ecSTakashi Iwai 	analog_low_current_mode(codec);
11277eb56e84STakashi Iwai }
11287eb56e84STakashi Iwai 
1129ece8d043STakashi Iwai static int via_playback_multi_pcm_open(struct hda_pcm_stream *hinfo,
1130c577b8a1SJoseph Chan 				 struct hda_codec *codec,
1131c577b8a1SJoseph Chan 				 struct snd_pcm_substream *substream)
1132c577b8a1SJoseph Chan {
1133c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
113425250505STakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
1135ada509ecSTakashi Iwai 	int err;
1136ece8d043STakashi Iwai 
113725250505STakashi Iwai 	spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
113825250505STakashi Iwai 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
11393b607e3dSTakashi Iwai 	set_stream_open(codec, STREAM_MULTI_OUT, true);
1140ada509ecSTakashi Iwai 	err = snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
11419a08160bSTakashi Iwai 					    hinfo);
1142ada509ecSTakashi Iwai 	if (err < 0) {
11433b607e3dSTakashi Iwai 		set_stream_open(codec, STREAM_MULTI_OUT, false);
1144ada509ecSTakashi Iwai 		return err;
1145ada509ecSTakashi Iwai 	}
1146ada509ecSTakashi Iwai 	return 0;
1147c577b8a1SJoseph Chan }
1148c577b8a1SJoseph Chan 
1149ece8d043STakashi Iwai static int via_playback_multi_pcm_close(struct hda_pcm_stream *hinfo,
11509af74210STakashi Iwai 				  struct hda_codec *codec,
11519af74210STakashi Iwai 				  struct snd_pcm_substream *substream)
11529af74210STakashi Iwai {
11533b607e3dSTakashi Iwai 	set_stream_open(codec, STREAM_MULTI_OUT, false);
11549af74210STakashi Iwai 	return 0;
11559af74210STakashi Iwai }
11569af74210STakashi Iwai 
11577eb56e84STakashi Iwai static int via_playback_hp_pcm_open(struct hda_pcm_stream *hinfo,
11587eb56e84STakashi Iwai 				    struct hda_codec *codec,
11597eb56e84STakashi Iwai 				    struct snd_pcm_substream *substream)
11607eb56e84STakashi Iwai {
11617eb56e84STakashi Iwai 	struct via_spec *spec = codec->spec;
11627eb56e84STakashi Iwai 
1163ece8d043STakashi Iwai 	if (snd_BUG_ON(!spec->hp_dac_nid))
11647eb56e84STakashi Iwai 		return -EINVAL;
11653b607e3dSTakashi Iwai 	set_stream_open(codec, STREAM_INDEP_HP, true);
1166ece8d043STakashi Iwai 	return 0;
1167ece8d043STakashi Iwai }
1168ece8d043STakashi Iwai 
1169ece8d043STakashi Iwai static int via_playback_hp_pcm_close(struct hda_pcm_stream *hinfo,
1170ece8d043STakashi Iwai 				     struct hda_codec *codec,
1171ece8d043STakashi Iwai 				     struct snd_pcm_substream *substream)
1172ece8d043STakashi Iwai {
11733b607e3dSTakashi Iwai 	set_stream_open(codec, STREAM_INDEP_HP, false);
11747eb56e84STakashi Iwai 	return 0;
11757eb56e84STakashi Iwai }
11767eb56e84STakashi Iwai 
11777eb56e84STakashi Iwai static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
11787eb56e84STakashi Iwai 					  struct hda_codec *codec,
11790aa62aefSHarald Welte 					  unsigned int stream_tag,
11800aa62aefSHarald Welte 					  unsigned int format,
11810aa62aefSHarald Welte 					  struct snd_pcm_substream *substream)
11820aa62aefSHarald Welte {
11830aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
11840aa62aefSHarald Welte 
11853b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
11863b607e3dSTakashi Iwai 	setup_playback_multi_pcm(spec);
1187ece8d043STakashi Iwai 	snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
1188ece8d043STakashi Iwai 					 format, substream);
11893b607e3dSTakashi Iwai 	/* remember for dynamic DAC switch with indep-HP */
11903b607e3dSTakashi Iwai 	spec->active_streams |= STREAM_MULTI_OUT;
11913b607e3dSTakashi Iwai 	spec->cur_dac_stream_tag = stream_tag;
11923b607e3dSTakashi Iwai 	spec->cur_dac_format = format;
11933b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1194187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
11957eb56e84STakashi Iwai 	return 0;
11960aa62aefSHarald Welte }
11970aa62aefSHarald Welte 
11987eb56e84STakashi Iwai static int via_playback_hp_pcm_prepare(struct hda_pcm_stream *hinfo,
11990aa62aefSHarald Welte 				       struct hda_codec *codec,
12000aa62aefSHarald Welte 				       unsigned int stream_tag,
12010aa62aefSHarald Welte 				       unsigned int format,
12020aa62aefSHarald Welte 				       struct snd_pcm_substream *substream)
12030aa62aefSHarald Welte {
12040aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
12050aa62aefSHarald Welte 
12063b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
12073b607e3dSTakashi Iwai 	if (spec->hp_independent_mode)
1208ece8d043STakashi Iwai 		snd_hda_codec_setup_stream(codec, spec->hp_dac_nid,
1209ece8d043STakashi Iwai 					   stream_tag, 0, format);
12103b607e3dSTakashi Iwai 	spec->active_streams |= STREAM_INDEP_HP;
12113b607e3dSTakashi Iwai 	spec->cur_hp_stream_tag = stream_tag;
12123b607e3dSTakashi Iwai 	spec->cur_hp_format = format;
12133b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1214187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
12150aa62aefSHarald Welte 	return 0;
12160aa62aefSHarald Welte }
12170aa62aefSHarald Welte 
12180aa62aefSHarald Welte static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
12190aa62aefSHarald Welte 				    struct hda_codec *codec,
12200aa62aefSHarald Welte 				    struct snd_pcm_substream *substream)
12210aa62aefSHarald Welte {
12220aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
12230aa62aefSHarald Welte 
12243b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
1225ece8d043STakashi Iwai 	snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
12263b607e3dSTakashi Iwai 	spec->active_streams &= ~STREAM_MULTI_OUT;
12273b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1228187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
12297eb56e84STakashi Iwai 	return 0;
12300aa62aefSHarald Welte }
12317eb56e84STakashi Iwai 
12327eb56e84STakashi Iwai static int via_playback_hp_pcm_cleanup(struct hda_pcm_stream *hinfo,
12337eb56e84STakashi Iwai 				       struct hda_codec *codec,
12347eb56e84STakashi Iwai 				       struct snd_pcm_substream *substream)
12357eb56e84STakashi Iwai {
12367eb56e84STakashi Iwai 	struct via_spec *spec = codec->spec;
12377eb56e84STakashi Iwai 
12383b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
12393b607e3dSTakashi Iwai 	if (spec->hp_independent_mode)
1240ece8d043STakashi Iwai 		snd_hda_codec_setup_stream(codec, spec->hp_dac_nid, 0, 0, 0);
12413b607e3dSTakashi Iwai 	spec->active_streams &= ~STREAM_INDEP_HP;
12423b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1243187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
12440aa62aefSHarald Welte 	return 0;
12450aa62aefSHarald Welte }
12460aa62aefSHarald Welte 
1247c577b8a1SJoseph Chan /*
1248c577b8a1SJoseph Chan  * Digital out
1249c577b8a1SJoseph Chan  */
1250c577b8a1SJoseph Chan static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1251c577b8a1SJoseph Chan 				     struct hda_codec *codec,
1252c577b8a1SJoseph Chan 				     struct snd_pcm_substream *substream)
1253c577b8a1SJoseph Chan {
1254c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1255c577b8a1SJoseph Chan 	return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1256c577b8a1SJoseph Chan }
1257c577b8a1SJoseph Chan 
1258c577b8a1SJoseph Chan static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1259c577b8a1SJoseph Chan 				      struct hda_codec *codec,
1260c577b8a1SJoseph Chan 				      struct snd_pcm_substream *substream)
1261c577b8a1SJoseph Chan {
1262c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1263c577b8a1SJoseph Chan 	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1264c577b8a1SJoseph Chan }
1265c577b8a1SJoseph Chan 
12665691ec7fSHarald Welte static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
126798aa34c0SHarald Welte 					struct hda_codec *codec,
126898aa34c0SHarald Welte 					unsigned int stream_tag,
126998aa34c0SHarald Welte 					unsigned int format,
127098aa34c0SHarald Welte 					struct snd_pcm_substream *substream)
127198aa34c0SHarald Welte {
127298aa34c0SHarald Welte 	struct via_spec *spec = codec->spec;
12739da29271STakashi Iwai 	return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
12749da29271STakashi Iwai 					     stream_tag, format, substream);
12759da29271STakashi Iwai }
12765691ec7fSHarald Welte 
12779da29271STakashi Iwai static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
12789da29271STakashi Iwai 					struct hda_codec *codec,
12799da29271STakashi Iwai 					struct snd_pcm_substream *substream)
12809da29271STakashi Iwai {
12819da29271STakashi Iwai 	struct via_spec *spec = codec->spec;
12829da29271STakashi Iwai 	snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
128398aa34c0SHarald Welte 	return 0;
128498aa34c0SHarald Welte }
128598aa34c0SHarald Welte 
1286c577b8a1SJoseph Chan /*
1287c577b8a1SJoseph Chan  * Analog capture
1288c577b8a1SJoseph Chan  */
1289c577b8a1SJoseph Chan static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1290c577b8a1SJoseph Chan 				   struct hda_codec *codec,
1291c577b8a1SJoseph Chan 				   unsigned int stream_tag,
1292c577b8a1SJoseph Chan 				   unsigned int format,
1293c577b8a1SJoseph Chan 				   struct snd_pcm_substream *substream)
1294c577b8a1SJoseph Chan {
1295c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1296c577b8a1SJoseph Chan 
1297c577b8a1SJoseph Chan 	snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1298c577b8a1SJoseph Chan 				   stream_tag, 0, format);
1299c577b8a1SJoseph Chan 	return 0;
1300c577b8a1SJoseph Chan }
1301c577b8a1SJoseph Chan 
1302c577b8a1SJoseph Chan static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1303c577b8a1SJoseph Chan 				   struct hda_codec *codec,
1304c577b8a1SJoseph Chan 				   struct snd_pcm_substream *substream)
1305c577b8a1SJoseph Chan {
1306c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1307888afa15STakashi Iwai 	snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
1308c577b8a1SJoseph Chan 	return 0;
1309c577b8a1SJoseph Chan }
1310c577b8a1SJoseph Chan 
1311a86a88eaSTakashi Iwai /* analog capture with dynamic ADC switching */
1312a86a88eaSTakashi Iwai static int via_dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1313a86a88eaSTakashi Iwai 					   struct hda_codec *codec,
1314a86a88eaSTakashi Iwai 					   unsigned int stream_tag,
1315a86a88eaSTakashi Iwai 					   unsigned int format,
1316a86a88eaSTakashi Iwai 					   struct snd_pcm_substream *substream)
1317a86a88eaSTakashi Iwai {
1318a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1319a86a88eaSTakashi Iwai 	int adc_idx = spec->inputs[spec->cur_mux[0]].adc_idx;
1320a86a88eaSTakashi Iwai 
13213b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
1322a86a88eaSTakashi Iwai 	spec->cur_adc = spec->adc_nids[adc_idx];
1323a86a88eaSTakashi Iwai 	spec->cur_adc_stream_tag = stream_tag;
1324a86a88eaSTakashi Iwai 	spec->cur_adc_format = format;
1325a86a88eaSTakashi Iwai 	snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
13263b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1327a86a88eaSTakashi Iwai 	return 0;
1328a86a88eaSTakashi Iwai }
1329a86a88eaSTakashi Iwai 
1330a86a88eaSTakashi Iwai static int via_dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1331a86a88eaSTakashi Iwai 					   struct hda_codec *codec,
1332a86a88eaSTakashi Iwai 					   struct snd_pcm_substream *substream)
1333a86a88eaSTakashi Iwai {
1334a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1335a86a88eaSTakashi Iwai 
13363b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
1337a86a88eaSTakashi Iwai 	snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1338a86a88eaSTakashi Iwai 	spec->cur_adc = 0;
13393b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1340a86a88eaSTakashi Iwai 	return 0;
1341a86a88eaSTakashi Iwai }
1342a86a88eaSTakashi Iwai 
1343a86a88eaSTakashi Iwai /* re-setup the stream if running; called from input-src put */
1344a86a88eaSTakashi Iwai static bool via_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
1345a86a88eaSTakashi Iwai {
1346a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1347a86a88eaSTakashi Iwai 	int adc_idx = spec->inputs[cur].adc_idx;
1348a86a88eaSTakashi Iwai 	hda_nid_t adc = spec->adc_nids[adc_idx];
13493b607e3dSTakashi Iwai 	bool ret = false;
1350a86a88eaSTakashi Iwai 
13513b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
1352a86a88eaSTakashi Iwai 	if (spec->cur_adc && spec->cur_adc != adc) {
1353a86a88eaSTakashi Iwai 		/* stream is running, let's swap the current ADC */
1354a86a88eaSTakashi Iwai 		__snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1355a86a88eaSTakashi Iwai 		spec->cur_adc = adc;
1356a86a88eaSTakashi Iwai 		snd_hda_codec_setup_stream(codec, adc,
1357a86a88eaSTakashi Iwai 					   spec->cur_adc_stream_tag, 0,
1358a86a88eaSTakashi Iwai 					   spec->cur_adc_format);
13593b607e3dSTakashi Iwai 		ret = true;
1360a86a88eaSTakashi Iwai 	}
13613b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
13623b607e3dSTakashi Iwai 	return ret;
1363a86a88eaSTakashi Iwai }
1364a86a88eaSTakashi Iwai 
13659af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_analog_playback = {
13667eb56e84STakashi Iwai 	.substreams = 1,
1367c577b8a1SJoseph Chan 	.channels_min = 2,
1368c577b8a1SJoseph Chan 	.channels_max = 8,
13699af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1370c577b8a1SJoseph Chan 	.ops = {
1371ece8d043STakashi Iwai 		.open = via_playback_multi_pcm_open,
1372ece8d043STakashi Iwai 		.close = via_playback_multi_pcm_close,
13730aa62aefSHarald Welte 		.prepare = via_playback_multi_pcm_prepare,
13740aa62aefSHarald Welte 		.cleanup = via_playback_multi_pcm_cleanup
1375c577b8a1SJoseph Chan 	},
1376c577b8a1SJoseph Chan };
1377c577b8a1SJoseph Chan 
13787eb56e84STakashi Iwai static const struct hda_pcm_stream via_pcm_hp_playback = {
13797eb56e84STakashi Iwai 	.substreams = 1,
13807eb56e84STakashi Iwai 	.channels_min = 2,
13817eb56e84STakashi Iwai 	.channels_max = 2,
13827eb56e84STakashi Iwai 	/* NID is set in via_build_pcms */
13837eb56e84STakashi Iwai 	.ops = {
13847eb56e84STakashi Iwai 		.open = via_playback_hp_pcm_open,
1385ece8d043STakashi Iwai 		.close = via_playback_hp_pcm_close,
13867eb56e84STakashi Iwai 		.prepare = via_playback_hp_pcm_prepare,
13877eb56e84STakashi Iwai 		.cleanup = via_playback_hp_pcm_cleanup
13887eb56e84STakashi Iwai 	},
13897eb56e84STakashi Iwai };
13907eb56e84STakashi Iwai 
139190dd48a1STakashi Iwai static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
13927eb56e84STakashi Iwai 	.substreams = 1,
1393bc9b5623STakashi Iwai 	.channels_min = 2,
1394bc9b5623STakashi Iwai 	.channels_max = 8,
13959af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1396bc9b5623STakashi Iwai 	/* We got noisy outputs on the right channel on VT1708 when
1397bc9b5623STakashi Iwai 	 * 24bit samples are used.  Until any workaround is found,
1398bc9b5623STakashi Iwai 	 * disable the 24bit format, so far.
1399bc9b5623STakashi Iwai 	 */
1400bc9b5623STakashi Iwai 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
1401bc9b5623STakashi Iwai 	.ops = {
1402ece8d043STakashi Iwai 		.open = via_playback_multi_pcm_open,
1403ece8d043STakashi Iwai 		.close = via_playback_multi_pcm_close,
1404c873cc25SLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
1405c873cc25SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup
1406bc9b5623STakashi Iwai 	},
1407bc9b5623STakashi Iwai };
1408bc9b5623STakashi Iwai 
14099af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_analog_capture = {
14107eb56e84STakashi Iwai 	.substreams = 1, /* will be changed in via_build_pcms() */
1411c577b8a1SJoseph Chan 	.channels_min = 2,
1412c577b8a1SJoseph Chan 	.channels_max = 2,
14139af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1414c577b8a1SJoseph Chan 	.ops = {
1415c577b8a1SJoseph Chan 		.prepare = via_capture_pcm_prepare,
1416c577b8a1SJoseph Chan 		.cleanup = via_capture_pcm_cleanup
1417c577b8a1SJoseph Chan 	},
1418c577b8a1SJoseph Chan };
1419c577b8a1SJoseph Chan 
1420a86a88eaSTakashi Iwai static const struct hda_pcm_stream via_pcm_dyn_adc_analog_capture = {
1421a86a88eaSTakashi Iwai 	.substreams = 1,
1422a86a88eaSTakashi Iwai 	.channels_min = 2,
1423a86a88eaSTakashi Iwai 	.channels_max = 2,
1424a86a88eaSTakashi Iwai 	/* NID is set in via_build_pcms */
1425a86a88eaSTakashi Iwai 	.ops = {
1426a86a88eaSTakashi Iwai 		.prepare = via_dyn_adc_capture_pcm_prepare,
1427a86a88eaSTakashi Iwai 		.cleanup = via_dyn_adc_capture_pcm_cleanup,
1428a86a88eaSTakashi Iwai 	},
1429a86a88eaSTakashi Iwai };
1430a86a88eaSTakashi Iwai 
14319af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_digital_playback = {
1432c577b8a1SJoseph Chan 	.substreams = 1,
1433c577b8a1SJoseph Chan 	.channels_min = 2,
1434c577b8a1SJoseph Chan 	.channels_max = 2,
1435c577b8a1SJoseph Chan 	/* NID is set in via_build_pcms */
1436c577b8a1SJoseph Chan 	.ops = {
1437c577b8a1SJoseph Chan 		.open = via_dig_playback_pcm_open,
14386b97eb45STakashi Iwai 		.close = via_dig_playback_pcm_close,
14399da29271STakashi Iwai 		.prepare = via_dig_playback_pcm_prepare,
14409da29271STakashi Iwai 		.cleanup = via_dig_playback_pcm_cleanup
1441c577b8a1SJoseph Chan 	},
1442c577b8a1SJoseph Chan };
1443c577b8a1SJoseph Chan 
14449af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_digital_capture = {
1445c577b8a1SJoseph Chan 	.substreams = 1,
1446c577b8a1SJoseph Chan 	.channels_min = 2,
1447c577b8a1SJoseph Chan 	.channels_max = 2,
1448c577b8a1SJoseph Chan };
1449c577b8a1SJoseph Chan 
1450370bafbdSTakashi Iwai /*
1451370bafbdSTakashi Iwai  * slave controls for virtual master
1452370bafbdSTakashi Iwai  */
14539322ca54STakashi Iwai static const char * const via_slave_pfxs[] = {
14549322ca54STakashi Iwai 	"Front", "Surround", "Center", "LFE", "Side",
14559322ca54STakashi Iwai 	"Headphone", "Speaker",
1456370bafbdSTakashi Iwai 	NULL,
1457370bafbdSTakashi Iwai };
1458370bafbdSTakashi Iwai 
1459c577b8a1SJoseph Chan static int via_build_controls(struct hda_codec *codec)
1460c577b8a1SJoseph Chan {
1461c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
14625b0cb1d8SJaroslav Kysela 	struct snd_kcontrol *kctl;
14635b0cb1d8SJaroslav Kysela 	int err, i;
1464c577b8a1SJoseph Chan 
1465b5bcc189STakashi Iwai 	spec->no_pin_power_ctl = 1;
146624088a58STakashi Iwai 	if (spec->set_widgets_power_state)
146724088a58STakashi Iwai 		if (!via_clone_control(spec, &via_pin_power_ctl_enum))
146824088a58STakashi Iwai 			return -ENOMEM;
146924088a58STakashi Iwai 
1470c577b8a1SJoseph Chan 	for (i = 0; i < spec->num_mixers; i++) {
1471c577b8a1SJoseph Chan 		err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1472c577b8a1SJoseph Chan 		if (err < 0)
1473c577b8a1SJoseph Chan 			return err;
1474c577b8a1SJoseph Chan 	}
1475c577b8a1SJoseph Chan 
1476c577b8a1SJoseph Chan 	if (spec->multiout.dig_out_nid) {
1477c577b8a1SJoseph Chan 		err = snd_hda_create_spdif_out_ctls(codec,
147874b654c9SStephen Warren 						    spec->multiout.dig_out_nid,
1479c577b8a1SJoseph Chan 						    spec->multiout.dig_out_nid);
1480c577b8a1SJoseph Chan 		if (err < 0)
1481c577b8a1SJoseph Chan 			return err;
14829a08160bSTakashi Iwai 		err = snd_hda_create_spdif_share_sw(codec,
14839a08160bSTakashi Iwai 						    &spec->multiout);
14849a08160bSTakashi Iwai 		if (err < 0)
14859a08160bSTakashi Iwai 			return err;
14869a08160bSTakashi Iwai 		spec->multiout.share_spdif = 1;
1487c577b8a1SJoseph Chan 	}
1488c577b8a1SJoseph Chan 	if (spec->dig_in_nid) {
1489c577b8a1SJoseph Chan 		err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1490c577b8a1SJoseph Chan 		if (err < 0)
1491c577b8a1SJoseph Chan 			return err;
1492c577b8a1SJoseph Chan 	}
149317314379SLydia Wang 
1494370bafbdSTakashi Iwai 	/* if we have no master control, let's create it */
1495370bafbdSTakashi Iwai 	if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1496370bafbdSTakashi Iwai 		unsigned int vmaster_tlv[4];
1497370bafbdSTakashi Iwai 		snd_hda_set_vmaster_tlv(codec, spec->multiout.dac_nids[0],
1498370bafbdSTakashi Iwai 					HDA_OUTPUT, vmaster_tlv);
1499370bafbdSTakashi Iwai 		err = snd_hda_add_vmaster(codec, "Master Playback Volume",
15009322ca54STakashi Iwai 					  vmaster_tlv, via_slave_pfxs,
15019322ca54STakashi Iwai 					  "Playback Volume");
1502370bafbdSTakashi Iwai 		if (err < 0)
1503370bafbdSTakashi Iwai 			return err;
1504370bafbdSTakashi Iwai 	}
1505370bafbdSTakashi Iwai 	if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1506370bafbdSTakashi Iwai 		err = snd_hda_add_vmaster(codec, "Master Playback Switch",
15079322ca54STakashi Iwai 					  NULL, via_slave_pfxs,
15089322ca54STakashi Iwai 					  "Playback Switch");
1509370bafbdSTakashi Iwai 		if (err < 0)
1510370bafbdSTakashi Iwai 			return err;
1511370bafbdSTakashi Iwai 	}
1512370bafbdSTakashi Iwai 
15135b0cb1d8SJaroslav Kysela 	/* assign Capture Source enums to NID */
15145b0cb1d8SJaroslav Kysela 	kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
15155b0cb1d8SJaroslav Kysela 	for (i = 0; kctl && i < kctl->count; i++) {
151677e314f7STakashi Iwai 		if (!spec->mux_nids[i])
151777e314f7STakashi Iwai 			continue;
151821949f00STakashi Iwai 		err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
15195b0cb1d8SJaroslav Kysela 		if (err < 0)
15205b0cb1d8SJaroslav Kysela 			return err;
15215b0cb1d8SJaroslav Kysela 	}
15225b0cb1d8SJaroslav Kysela 
1523603c4019STakashi Iwai 	via_free_kctls(codec); /* no longer needed */
152401a61e12STakashi Iwai 
152501a61e12STakashi Iwai 	err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
152601a61e12STakashi Iwai 	if (err < 0)
152701a61e12STakashi Iwai 		return err;
152801a61e12STakashi Iwai 
1529c577b8a1SJoseph Chan 	return 0;
1530c577b8a1SJoseph Chan }
1531c577b8a1SJoseph Chan 
1532c577b8a1SJoseph Chan static int via_build_pcms(struct hda_codec *codec)
1533c577b8a1SJoseph Chan {
1534c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1535c577b8a1SJoseph Chan 	struct hda_pcm *info = spec->pcm_rec;
1536c577b8a1SJoseph Chan 
1537a5973103STakashi Iwai 	codec->num_pcms = 0;
1538c577b8a1SJoseph Chan 	codec->pcm_info = info;
1539c577b8a1SJoseph Chan 
1540a5973103STakashi Iwai 	if (spec->multiout.num_dacs || spec->num_adc_nids) {
1541a5973103STakashi Iwai 		snprintf(spec->stream_name_analog,
1542a5973103STakashi Iwai 			 sizeof(spec->stream_name_analog),
154382673bc8STakashi Iwai 			 "%s Analog", codec->chip_name);
1544c577b8a1SJoseph Chan 		info->name = spec->stream_name_analog;
15459af74210STakashi Iwai 
1546a5973103STakashi Iwai 		if (spec->multiout.num_dacs) {
15479af74210STakashi Iwai 			if (!spec->stream_analog_playback)
1548a5973103STakashi Iwai 				spec->stream_analog_playback =
1549a5973103STakashi Iwai 					&via_pcm_analog_playback;
1550377ff31aSLydia Wang 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
15519af74210STakashi Iwai 				*spec->stream_analog_playback;
1552377ff31aSLydia Wang 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1553377ff31aSLydia Wang 				spec->multiout.dac_nids[0];
1554c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1555c577b8a1SJoseph Chan 				spec->multiout.max_channels;
1556a5973103STakashi Iwai 		}
15579af74210STakashi Iwai 
1558a86a88eaSTakashi Iwai 		if (!spec->stream_analog_capture) {
1559a86a88eaSTakashi Iwai 			if (spec->dyn_adc_switch)
1560a86a88eaSTakashi Iwai 				spec->stream_analog_capture =
1561a86a88eaSTakashi Iwai 					&via_pcm_dyn_adc_analog_capture;
1562a86a88eaSTakashi Iwai 			else
1563a5973103STakashi Iwai 				spec->stream_analog_capture =
1564a5973103STakashi Iwai 					&via_pcm_analog_capture;
1565a86a88eaSTakashi Iwai 		}
1566a5973103STakashi Iwai 		if (spec->num_adc_nids) {
15679af74210STakashi Iwai 			info->stream[SNDRV_PCM_STREAM_CAPTURE] =
15689af74210STakashi Iwai 				*spec->stream_analog_capture;
1569a5973103STakashi Iwai 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1570a5973103STakashi Iwai 				spec->adc_nids[0];
1571a86a88eaSTakashi Iwai 			if (!spec->dyn_adc_switch)
15729af74210STakashi Iwai 				info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
15739af74210STakashi Iwai 					spec->num_adc_nids;
1574a5973103STakashi Iwai 		}
1575c577b8a1SJoseph Chan 		codec->num_pcms++;
1576c577b8a1SJoseph Chan 		info++;
1577a5973103STakashi Iwai 	}
1578a5973103STakashi Iwai 
1579a5973103STakashi Iwai 	if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
158082673bc8STakashi Iwai 		snprintf(spec->stream_name_digital,
158182673bc8STakashi Iwai 			 sizeof(spec->stream_name_digital),
158282673bc8STakashi Iwai 			 "%s Digital", codec->chip_name);
1583c577b8a1SJoseph Chan 		info->name = spec->stream_name_digital;
15847ba72ba1STakashi Iwai 		info->pcm_type = HDA_PCM_TYPE_SPDIF;
1585c577b8a1SJoseph Chan 		if (spec->multiout.dig_out_nid) {
15869af74210STakashi Iwai 			if (!spec->stream_digital_playback)
15879af74210STakashi Iwai 				spec->stream_digital_playback =
15889af74210STakashi Iwai 					&via_pcm_digital_playback;
1589c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
15909af74210STakashi Iwai 				*spec->stream_digital_playback;
1591c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1592c577b8a1SJoseph Chan 				spec->multiout.dig_out_nid;
1593c577b8a1SJoseph Chan 		}
1594c577b8a1SJoseph Chan 		if (spec->dig_in_nid) {
15959af74210STakashi Iwai 			if (!spec->stream_digital_capture)
15969af74210STakashi Iwai 				spec->stream_digital_capture =
15979af74210STakashi Iwai 					&via_pcm_digital_capture;
1598c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_CAPTURE] =
15999af74210STakashi Iwai 				*spec->stream_digital_capture;
1600c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1601c577b8a1SJoseph Chan 				spec->dig_in_nid;
1602c577b8a1SJoseph Chan 		}
1603a5973103STakashi Iwai 		codec->num_pcms++;
1604a5973103STakashi Iwai 		info++;
1605c577b8a1SJoseph Chan 	}
1606c577b8a1SJoseph Chan 
1607ece8d043STakashi Iwai 	if (spec->hp_dac_nid) {
16087eb56e84STakashi Iwai 		snprintf(spec->stream_name_hp, sizeof(spec->stream_name_hp),
16097eb56e84STakashi Iwai 			 "%s HP", codec->chip_name);
16107eb56e84STakashi Iwai 		info->name = spec->stream_name_hp;
16117eb56e84STakashi Iwai 		info->stream[SNDRV_PCM_STREAM_PLAYBACK] = via_pcm_hp_playback;
16127eb56e84STakashi Iwai 		info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1613ece8d043STakashi Iwai 			spec->hp_dac_nid;
1614a5973103STakashi Iwai 		codec->num_pcms++;
1615a5973103STakashi Iwai 		info++;
16167eb56e84STakashi Iwai 	}
1617c577b8a1SJoseph Chan 	return 0;
1618c577b8a1SJoseph Chan }
1619c577b8a1SJoseph Chan 
1620c577b8a1SJoseph Chan static void via_free(struct hda_codec *codec)
1621c577b8a1SJoseph Chan {
1622c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1623c577b8a1SJoseph Chan 
1624c577b8a1SJoseph Chan 	if (!spec)
1625c577b8a1SJoseph Chan 		return;
1626c577b8a1SJoseph Chan 
1627603c4019STakashi Iwai 	via_free_kctls(codec);
16281f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
1629a86a88eaSTakashi Iwai 	kfree(spec->bind_cap_vol);
1630a86a88eaSTakashi Iwai 	kfree(spec->bind_cap_sw);
1631a86a88eaSTakashi Iwai 	kfree(spec);
1632c577b8a1SJoseph Chan }
1633c577b8a1SJoseph Chan 
163464be285bSTakashi Iwai /* mute/unmute outputs */
163564be285bSTakashi Iwai static void toggle_output_mutes(struct hda_codec *codec, int num_pins,
163664be285bSTakashi Iwai 				hda_nid_t *pins, bool mute)
163764be285bSTakashi Iwai {
163864be285bSTakashi Iwai 	int i;
163994994734STakashi Iwai 	for (i = 0; i < num_pins; i++) {
164094994734STakashi Iwai 		unsigned int parm = snd_hda_codec_read(codec, pins[i], 0,
164194994734STakashi Iwai 					  AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
164294994734STakashi Iwai 		if (parm & AC_PINCTL_IN_EN)
164394994734STakashi Iwai 			continue;
164494994734STakashi Iwai 		if (mute)
164594994734STakashi Iwai 			parm &= ~AC_PINCTL_OUT_EN;
164694994734STakashi Iwai 		else
164794994734STakashi Iwai 			parm |= AC_PINCTL_OUT_EN;
1648cdd03cedSTakashi Iwai 		snd_hda_set_pin_ctl(codec, pins[i], parm);
164994994734STakashi Iwai 	}
165064be285bSTakashi Iwai }
165164be285bSTakashi Iwai 
16524a918ffeSTakashi Iwai /* mute internal speaker if line-out is plugged */
16534a918ffeSTakashi Iwai static void via_line_automute(struct hda_codec *codec, int present)
16544a918ffeSTakashi Iwai {
16554a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
16564a918ffeSTakashi Iwai 
16574a918ffeSTakashi Iwai 	if (!spec->autocfg.speaker_outs)
16584a918ffeSTakashi Iwai 		return;
16594a918ffeSTakashi Iwai 	if (!present)
16604a918ffeSTakashi Iwai 		present = snd_hda_jack_detect(codec,
16614a918ffeSTakashi Iwai 					      spec->autocfg.line_out_pins[0]);
16624a918ffeSTakashi Iwai 	toggle_output_mutes(codec, spec->autocfg.speaker_outs,
16634a918ffeSTakashi Iwai 			    spec->autocfg.speaker_pins,
16644a918ffeSTakashi Iwai 			    present);
16654a918ffeSTakashi Iwai }
16664a918ffeSTakashi Iwai 
166769e52a80SHarald Welte /* mute internal speaker if HP is plugged */
166869e52a80SHarald Welte static void via_hp_automute(struct hda_codec *codec)
166969e52a80SHarald Welte {
16704a918ffeSTakashi Iwai 	int present = 0;
16716e969d91STakashi Iwai 	int nums;
167269e52a80SHarald Welte 	struct via_spec *spec = codec->spec;
167369e52a80SHarald Welte 
1674187d333eSTakashi Iwai 	if (!spec->hp_independent_mode && spec->autocfg.hp_pins[0] &&
1675187d333eSTakashi Iwai 	    (spec->codec_type != VT1708 || spec->vt1708_jack_detect))
1676d56757abSTakashi Iwai 		present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
16776e969d91STakashi Iwai 
1678f2b1c9f0STakashi Iwai 	if (spec->smart51_enabled)
1679f2b1c9f0STakashi Iwai 		nums = spec->autocfg.line_outs + spec->smart51_nums;
1680f2b1c9f0STakashi Iwai 	else
1681f2b1c9f0STakashi Iwai 		nums = spec->autocfg.line_outs;
16826e969d91STakashi Iwai 	toggle_output_mutes(codec, nums, spec->autocfg.line_out_pins, present);
16836e969d91STakashi Iwai 
16844a918ffeSTakashi Iwai 	via_line_automute(codec, present);
1685f3db423dSLydia Wang }
1686f3db423dSLydia Wang 
168769e52a80SHarald Welte static void via_gpio_control(struct hda_codec *codec)
168869e52a80SHarald Welte {
168969e52a80SHarald Welte 	unsigned int gpio_data;
169069e52a80SHarald Welte 	unsigned int vol_counter;
169169e52a80SHarald Welte 	unsigned int vol;
169269e52a80SHarald Welte 	unsigned int master_vol;
169369e52a80SHarald Welte 
169469e52a80SHarald Welte 	struct via_spec *spec = codec->spec;
169569e52a80SHarald Welte 
169669e52a80SHarald Welte 	gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
169769e52a80SHarald Welte 				       AC_VERB_GET_GPIO_DATA, 0) & 0x03;
169869e52a80SHarald Welte 
169969e52a80SHarald Welte 	vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
170069e52a80SHarald Welte 					  0xF84, 0) & 0x3F0000) >> 16;
170169e52a80SHarald Welte 
170269e52a80SHarald Welte 	vol = vol_counter & 0x1F;
170369e52a80SHarald Welte 	master_vol = snd_hda_codec_read(codec, 0x1A, 0,
170469e52a80SHarald Welte 					AC_VERB_GET_AMP_GAIN_MUTE,
170569e52a80SHarald Welte 					AC_AMP_GET_INPUT);
170669e52a80SHarald Welte 
170769e52a80SHarald Welte 	if (gpio_data == 0x02) {
170869e52a80SHarald Welte 		/* unmute line out */
1709cdd03cedSTakashi Iwai 		snd_hda_set_pin_ctl(codec, spec->autocfg.line_out_pins[0],
17103e0693e2STakashi Iwai 				    PIN_OUT);
171169e52a80SHarald Welte 		if (vol_counter & 0x20) {
171269e52a80SHarald Welte 			/* decrease volume */
171369e52a80SHarald Welte 			if (vol > master_vol)
171469e52a80SHarald Welte 				vol = master_vol;
171569e52a80SHarald Welte 			snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
171669e52a80SHarald Welte 						 0, HDA_AMP_VOLMASK,
171769e52a80SHarald Welte 						 master_vol-vol);
171869e52a80SHarald Welte 		} else {
171969e52a80SHarald Welte 			/* increase volume */
172069e52a80SHarald Welte 			snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
172169e52a80SHarald Welte 					 HDA_AMP_VOLMASK,
172269e52a80SHarald Welte 					 ((master_vol+vol) > 0x2A) ? 0x2A :
172369e52a80SHarald Welte 					  (master_vol+vol));
172469e52a80SHarald Welte 		}
172569e52a80SHarald Welte 	} else if (!(gpio_data & 0x02)) {
172669e52a80SHarald Welte 		/* mute line out */
1727cdd03cedSTakashi Iwai 		snd_hda_set_pin_ctl(codec, spec->autocfg.line_out_pins[0], 0);
172869e52a80SHarald Welte 	}
172969e52a80SHarald Welte }
173069e52a80SHarald Welte 
173169e52a80SHarald Welte /* unsolicited event for jack sensing */
173269e52a80SHarald Welte static void via_unsol_event(struct hda_codec *codec,
173369e52a80SHarald Welte 				  unsigned int res)
173469e52a80SHarald Welte {
173569e52a80SHarald Welte 	res >>= 26;
17363a93897eSTakashi Iwai 	res = snd_hda_jack_get_action(codec, res);
1737ec7e7e42SLydia Wang 
1738a34df19aSLydia Wang 	if (res & VIA_JACK_EVENT)
17393e95b9abSLydia Wang 		set_widgets_power_state(codec);
1740ec7e7e42SLydia Wang 
1741ec7e7e42SLydia Wang 	res &= ~VIA_JACK_EVENT;
1742ec7e7e42SLydia Wang 
174321ce0b65STakashi Iwai 	if (res == VIA_HP_EVENT || res == VIA_LINE_EVENT)
1744ec7e7e42SLydia Wang 		via_hp_automute(codec);
1745ec7e7e42SLydia Wang 	else if (res == VIA_GPIO_EVENT)
1746ec7e7e42SLydia Wang 		via_gpio_control(codec);
174701a61e12STakashi Iwai 	snd_hda_jack_report_sync(codec);
174869e52a80SHarald Welte }
174969e52a80SHarald Welte 
17502a43952aSTakashi Iwai #ifdef CONFIG_PM
175168cb2b55STakashi Iwai static int via_suspend(struct hda_codec *codec)
17521f2e99feSLydia Wang {
17531f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
17541f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
17551f2e99feSLydia Wang 	return 0;
17561f2e99feSLydia Wang }
17571f2e99feSLydia Wang #endif
17581f2e99feSLydia Wang 
1759cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
1760cb53c626STakashi Iwai static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1761cb53c626STakashi Iwai {
1762cb53c626STakashi Iwai 	struct via_spec *spec = codec->spec;
1763cb53c626STakashi Iwai 	return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1764cb53c626STakashi Iwai }
1765cb53c626STakashi Iwai #endif
1766cb53c626STakashi Iwai 
1767c577b8a1SJoseph Chan /*
1768c577b8a1SJoseph Chan  */
17695d41762aSTakashi Iwai 
17705d41762aSTakashi Iwai static int via_init(struct hda_codec *codec);
17715d41762aSTakashi Iwai 
177290dd48a1STakashi Iwai static const struct hda_codec_ops via_patch_ops = {
1773c577b8a1SJoseph Chan 	.build_controls = via_build_controls,
1774c577b8a1SJoseph Chan 	.build_pcms = via_build_pcms,
1775c577b8a1SJoseph Chan 	.init = via_init,
1776c577b8a1SJoseph Chan 	.free = via_free,
17774a918ffeSTakashi Iwai 	.unsol_event = via_unsol_event,
17782a43952aSTakashi Iwai #ifdef CONFIG_PM
17791f2e99feSLydia Wang 	.suspend = via_suspend,
17801f2e99feSLydia Wang #endif
1781cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
1782cb53c626STakashi Iwai 	.check_power_status = via_check_power_status,
1783cb53c626STakashi Iwai #endif
1784c577b8a1SJoseph Chan };
1785c577b8a1SJoseph Chan 
17864a79616dSTakashi Iwai static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
1787c577b8a1SJoseph Chan {
17884a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
17894a79616dSTakashi Iwai 	int i;
17904a79616dSTakashi Iwai 
17914a79616dSTakashi Iwai 	for (i = 0; i < spec->multiout.num_dacs; i++) {
17924a79616dSTakashi Iwai 		if (spec->multiout.dac_nids[i] == dac)
17934a79616dSTakashi Iwai 			return false;
17944a79616dSTakashi Iwai 	}
1795ece8d043STakashi Iwai 	if (spec->hp_dac_nid == dac)
17964a79616dSTakashi Iwai 		return false;
17974a79616dSTakashi Iwai 	return true;
17984a79616dSTakashi Iwai }
17994a79616dSTakashi Iwai 
18008e3679dcSTakashi Iwai static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
18013214b966STakashi Iwai 				hda_nid_t target_dac, int with_aa_mix,
18023214b966STakashi Iwai 				struct nid_path *path, int depth)
18034a79616dSTakashi Iwai {
18043214b966STakashi Iwai 	struct via_spec *spec = codec->spec;
18054a79616dSTakashi Iwai 	hda_nid_t conn[8];
18064a79616dSTakashi Iwai 	int i, nums;
18074a79616dSTakashi Iwai 
18083214b966STakashi Iwai 	if (nid == spec->aa_mix_nid) {
18093214b966STakashi Iwai 		if (!with_aa_mix)
18103214b966STakashi Iwai 			return false;
18113214b966STakashi Iwai 		with_aa_mix = 2; /* mark aa-mix is included */
18123214b966STakashi Iwai 	}
18133214b966STakashi Iwai 
18144a79616dSTakashi Iwai 	nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
18154a79616dSTakashi Iwai 	for (i = 0; i < nums; i++) {
18164a79616dSTakashi Iwai 		if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
18174a79616dSTakashi Iwai 			continue;
18183214b966STakashi Iwai 		if (conn[i] == target_dac || is_empty_dac(codec, conn[i])) {
18193214b966STakashi Iwai 			/* aa-mix is requested but not included? */
18203214b966STakashi Iwai 			if (!(spec->aa_mix_nid && with_aa_mix == 1))
182109a9ad69STakashi Iwai 				goto found;
18224a79616dSTakashi Iwai 		}
18233214b966STakashi Iwai 	}
18248e3679dcSTakashi Iwai 	if (depth >= MAX_NID_PATH_DEPTH)
18254a79616dSTakashi Iwai 		return false;
18264a79616dSTakashi Iwai 	for (i = 0; i < nums; i++) {
18274a79616dSTakashi Iwai 		unsigned int type;
18284a79616dSTakashi Iwai 		type = get_wcaps_type(get_wcaps(codec, conn[i]));
18293214b966STakashi Iwai 		if (type == AC_WID_AUD_OUT)
18304a79616dSTakashi Iwai 			continue;
18318e3679dcSTakashi Iwai 		if (__parse_output_path(codec, conn[i], target_dac,
18323214b966STakashi Iwai 					with_aa_mix, path, depth + 1))
183309a9ad69STakashi Iwai 			goto found;
18344a79616dSTakashi Iwai 	}
18354a79616dSTakashi Iwai 	return false;
183609a9ad69STakashi Iwai 
183709a9ad69STakashi Iwai  found:
183809a9ad69STakashi Iwai 	path->path[path->depth] = conn[i];
183909a9ad69STakashi Iwai 	path->idx[path->depth] = i;
184009a9ad69STakashi Iwai 	if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
184109a9ad69STakashi Iwai 		path->multi[path->depth] = 1;
184209a9ad69STakashi Iwai 	path->depth++;
184309a9ad69STakashi Iwai 	return true;
18444a79616dSTakashi Iwai }
18454a79616dSTakashi Iwai 
18468e3679dcSTakashi Iwai static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
18473214b966STakashi Iwai 			      hda_nid_t target_dac, int with_aa_mix,
18483214b966STakashi Iwai 			      struct nid_path *path)
18498e3679dcSTakashi Iwai {
18503214b966STakashi Iwai 	if (__parse_output_path(codec, nid, target_dac, with_aa_mix, path, 1)) {
18518e3679dcSTakashi Iwai 		path->path[path->depth] = nid;
18528e3679dcSTakashi Iwai 		path->depth++;
18533214b966STakashi Iwai 		snd_printdd("output-path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
18543214b966STakashi Iwai 			    path->depth, path->path[0], path->path[1],
18553214b966STakashi Iwai 			    path->path[2], path->path[3], path->path[4]);
18568e3679dcSTakashi Iwai 		return true;
18578e3679dcSTakashi Iwai 	}
18588e3679dcSTakashi Iwai 	return false;
18598e3679dcSTakashi Iwai }
18608e3679dcSTakashi Iwai 
18614a79616dSTakashi Iwai static int via_auto_fill_dac_nids(struct hda_codec *codec)
18624a79616dSTakashi Iwai {
18634a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
18644a79616dSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
18655c9a5615SLydia Wang 	int i, dac_num;
1866c577b8a1SJoseph Chan 	hda_nid_t nid;
1867c577b8a1SJoseph Chan 
1868c577b8a1SJoseph Chan 	spec->multiout.dac_nids = spec->private_dac_nids;
18695c9a5615SLydia Wang 	dac_num = 0;
18704a79616dSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
18713214b966STakashi Iwai 		hda_nid_t dac = 0;
1872c577b8a1SJoseph Chan 		nid = cfg->line_out_pins[i];
18734a79616dSTakashi Iwai 		if (!nid)
18744a79616dSTakashi Iwai 			continue;
18753214b966STakashi Iwai 		if (parse_output_path(codec, nid, 0, 0, &spec->out_path[i]))
18763214b966STakashi Iwai 			dac = spec->out_path[i].path[0];
18773214b966STakashi Iwai 		if (!i && parse_output_path(codec, nid, dac, 1,
18783214b966STakashi Iwai 					    &spec->out_mix_path))
18793214b966STakashi Iwai 			dac = spec->out_mix_path.path[0];
18803214b966STakashi Iwai 		if (dac) {
18813214b966STakashi Iwai 			spec->private_dac_nids[i] = dac;
18825c9a5615SLydia Wang 			dac_num++;
1883c577b8a1SJoseph Chan 		}
18845c9a5615SLydia Wang 	}
18853214b966STakashi Iwai 	if (!spec->out_path[0].depth && spec->out_mix_path.depth) {
18863214b966STakashi Iwai 		spec->out_path[0] = spec->out_mix_path;
18873214b966STakashi Iwai 		spec->out_mix_path.depth = 0;
18883214b966STakashi Iwai 	}
18895c9a5615SLydia Wang 	spec->multiout.num_dacs = dac_num;
1890c577b8a1SJoseph Chan 	return 0;
1891c577b8a1SJoseph Chan }
1892c577b8a1SJoseph Chan 
18934a79616dSTakashi Iwai static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
189409a9ad69STakashi Iwai 			  int chs, bool check_dac, struct nid_path *path)
1895c577b8a1SJoseph Chan {
18964a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
1897c577b8a1SJoseph Chan 	char name[32];
189809a9ad69STakashi Iwai 	hda_nid_t dac, pin, sel, nid;
189909a9ad69STakashi Iwai 	int err;
1900a934d5a9STakashi Iwai 
190109a9ad69STakashi Iwai 	dac = check_dac ? path->path[0] : 0;
190209a9ad69STakashi Iwai 	pin = path->path[path->depth - 1];
190309a9ad69STakashi Iwai 	sel = path->depth > 1 ? path->path[1] : 0;
1904c577b8a1SJoseph Chan 
19058df2a312STakashi Iwai 	if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
19064a79616dSTakashi Iwai 		nid = dac;
19078df2a312STakashi Iwai 	else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
19084a79616dSTakashi Iwai 		nid = pin;
1909a934d5a9STakashi Iwai 	else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1910a934d5a9STakashi Iwai 		nid = sel;
19114a79616dSTakashi Iwai 	else
19124a79616dSTakashi Iwai 		nid = 0;
19134a79616dSTakashi Iwai 	if (nid) {
19144a79616dSTakashi Iwai 		sprintf(name, "%s Playback Volume", pfx);
1915c577b8a1SJoseph Chan 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1916a00a5fadSLydia Wang 			      HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1917c577b8a1SJoseph Chan 		if (err < 0)
1918c577b8a1SJoseph Chan 			return err;
191909a9ad69STakashi Iwai 		path->vol_ctl = nid;
1920c577b8a1SJoseph Chan 	}
19214a79616dSTakashi Iwai 
19228df2a312STakashi Iwai 	if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
19234a79616dSTakashi Iwai 		nid = dac;
19248df2a312STakashi Iwai 	else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
19254a79616dSTakashi Iwai 		nid = pin;
1926a934d5a9STakashi Iwai 	else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
1927a934d5a9STakashi Iwai 		nid = sel;
19284a79616dSTakashi Iwai 	else
19294a79616dSTakashi Iwai 		nid = 0;
19304a79616dSTakashi Iwai 	if (nid) {
19314a79616dSTakashi Iwai 		sprintf(name, "%s Playback Switch", pfx);
19324a79616dSTakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
19334a79616dSTakashi Iwai 			      HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
19344a79616dSTakashi Iwai 		if (err < 0)
19354a79616dSTakashi Iwai 			return err;
193609a9ad69STakashi Iwai 		path->mute_ctl = nid;
19374a79616dSTakashi Iwai 	}
19384a79616dSTakashi Iwai 	return 0;
19394a79616dSTakashi Iwai }
19404a79616dSTakashi Iwai 
1941f4a7828bSTakashi Iwai static void mangle_smart51(struct hda_codec *codec)
1942f4a7828bSTakashi Iwai {
1943f4a7828bSTakashi Iwai 	struct via_spec *spec = codec->spec;
1944f4a7828bSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
19450f98c24bSTakashi Iwai 	struct auto_pin_cfg_item *ins = cfg->inputs;
19460f98c24bSTakashi Iwai 	int i, j, nums, attr;
19470f98c24bSTakashi Iwai 	int pins[AUTO_CFG_MAX_INS];
1948f4a7828bSTakashi Iwai 
19490f98c24bSTakashi Iwai 	for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
19500f98c24bSTakashi Iwai 		nums = 0;
1951f4a7828bSTakashi Iwai 		for (i = 0; i < cfg->num_inputs; i++) {
19520f98c24bSTakashi Iwai 			unsigned int def;
19530f98c24bSTakashi Iwai 			if (ins[i].type > AUTO_PIN_LINE_IN)
19540f98c24bSTakashi Iwai 				continue;
19550f98c24bSTakashi Iwai 			def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
19560f98c24bSTakashi Iwai 			if (snd_hda_get_input_pin_attr(def) != attr)
19570f98c24bSTakashi Iwai 				continue;
19580f98c24bSTakashi Iwai 			for (j = 0; j < nums; j++)
19590f98c24bSTakashi Iwai 				if (ins[pins[j]].type < ins[i].type) {
19600f98c24bSTakashi Iwai 					memmove(pins + j + 1, pins + j,
196121d45d2bSTakashi Iwai 						(nums - j) * sizeof(int));
19620f98c24bSTakashi Iwai 					break;
19630f98c24bSTakashi Iwai 				}
19640f98c24bSTakashi Iwai 			pins[j] = i;
1965e3d7a143STakashi Iwai 			nums++;
1966e3d7a143STakashi Iwai 		}
1967e3d7a143STakashi Iwai 		if (cfg->line_outs + nums < 3)
1968f4a7828bSTakashi Iwai 			continue;
19690f98c24bSTakashi Iwai 		for (i = 0; i < nums; i++) {
19700f98c24bSTakashi Iwai 			hda_nid_t pin = ins[pins[i]].pin;
19710f98c24bSTakashi Iwai 			spec->smart51_pins[spec->smart51_nums++] = pin;
19720f98c24bSTakashi Iwai 			cfg->line_out_pins[cfg->line_outs++] = pin;
1973f4a7828bSTakashi Iwai 			if (cfg->line_outs == 3)
1974f4a7828bSTakashi Iwai 				break;
1975f4a7828bSTakashi Iwai 		}
19760f98c24bSTakashi Iwai 		return;
19770f98c24bSTakashi Iwai 	}
1978f4a7828bSTakashi Iwai }
1979f4a7828bSTakashi Iwai 
1980020066d1STakashi Iwai static void copy_path_mixer_ctls(struct nid_path *dst, struct nid_path *src)
1981020066d1STakashi Iwai {
1982020066d1STakashi Iwai 	dst->vol_ctl = src->vol_ctl;
1983020066d1STakashi Iwai 	dst->mute_ctl = src->mute_ctl;
1984020066d1STakashi Iwai }
1985020066d1STakashi Iwai 
19864a79616dSTakashi Iwai /* add playback controls from the parsed DAC table */
19874a79616dSTakashi Iwai static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
19884a79616dSTakashi Iwai {
19894a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
1990f4a7828bSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
19913214b966STakashi Iwai 	struct nid_path *path;
19924a79616dSTakashi Iwai 	static const char * const chname[4] = {
19934a79616dSTakashi Iwai 		"Front", "Surround", "C/LFE", "Side"
19944a79616dSTakashi Iwai 	};
19954a79616dSTakashi Iwai 	int i, idx, err;
1996f4a7828bSTakashi Iwai 	int old_line_outs;
1997f4a7828bSTakashi Iwai 
1998f4a7828bSTakashi Iwai 	/* check smart51 */
1999f4a7828bSTakashi Iwai 	old_line_outs = cfg->line_outs;
2000f4a7828bSTakashi Iwai 	if (cfg->line_outs == 1)
2001f4a7828bSTakashi Iwai 		mangle_smart51(codec);
20024a79616dSTakashi Iwai 
2003e3d7a143STakashi Iwai 	err = via_auto_fill_dac_nids(codec);
2004e3d7a143STakashi Iwai 	if (err < 0)
2005e3d7a143STakashi Iwai 		return err;
2006e3d7a143STakashi Iwai 
20075c9a5615SLydia Wang 	if (spec->multiout.num_dacs < 3) {
20085c9a5615SLydia Wang 		spec->smart51_nums = 0;
20095c9a5615SLydia Wang 		cfg->line_outs = old_line_outs;
20105c9a5615SLydia Wang 	}
20114a79616dSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
20124a79616dSTakashi Iwai 		hda_nid_t pin, dac;
20134a79616dSTakashi Iwai 		pin = cfg->line_out_pins[i];
20144a79616dSTakashi Iwai 		dac = spec->multiout.dac_nids[i];
20154a79616dSTakashi Iwai 		if (!pin || !dac)
20164a79616dSTakashi Iwai 			continue;
20173214b966STakashi Iwai 		path = spec->out_path + i;
20180fe0adf8STakashi Iwai 		if (i == HDA_CLFE) {
20193214b966STakashi Iwai 			err = create_ch_ctls(codec, "Center", 1, true, path);
20204a79616dSTakashi Iwai 			if (err < 0)
20214a79616dSTakashi Iwai 				return err;
20223214b966STakashi Iwai 			err = create_ch_ctls(codec, "LFE", 2, true, path);
20234a79616dSTakashi Iwai 			if (err < 0)
20244a79616dSTakashi Iwai 				return err;
20254a79616dSTakashi Iwai 		} else {
20266aadf41dSTakashi Iwai 			const char *pfx = chname[i];
20276aadf41dSTakashi Iwai 			if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
20286aadf41dSTakashi Iwai 			    cfg->line_outs == 1)
20296aadf41dSTakashi Iwai 				pfx = "Speaker";
20303214b966STakashi Iwai 			err = create_ch_ctls(codec, pfx, 3, true, path);
20314a79616dSTakashi Iwai 			if (err < 0)
20324a79616dSTakashi Iwai 				return err;
20334a79616dSTakashi Iwai 		}
2034020066d1STakashi Iwai 		if (path != spec->out_path + i)
2035020066d1STakashi Iwai 			copy_path_mixer_ctls(&spec->out_path[i], path);
2036020066d1STakashi Iwai 		if (path == spec->out_path && spec->out_mix_path.depth)
2037020066d1STakashi Iwai 			copy_path_mixer_ctls(&spec->out_mix_path, path);
20384a79616dSTakashi Iwai 	}
20394a79616dSTakashi Iwai 
20404a79616dSTakashi Iwai 	idx = get_connection_index(codec, spec->aa_mix_nid,
20414a79616dSTakashi Iwai 				   spec->multiout.dac_nids[0]);
20424a79616dSTakashi Iwai 	if (idx >= 0) {
20434a79616dSTakashi Iwai 		/* add control to mixer */
20443214b966STakashi Iwai 		const char *name;
20453214b966STakashi Iwai 		name = spec->out_mix_path.depth ?
20463214b966STakashi Iwai 			"PCM Loopback Playback Volume" : "PCM Playback Volume";
20473214b966STakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
20484a79616dSTakashi Iwai 				      HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
20494a79616dSTakashi Iwai 							  idx, HDA_INPUT));
20504a79616dSTakashi Iwai 		if (err < 0)
20514a79616dSTakashi Iwai 			return err;
20523214b966STakashi Iwai 		name = spec->out_mix_path.depth ?
20533214b966STakashi Iwai 			"PCM Loopback Playback Switch" : "PCM Playback Switch";
20543214b966STakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
20554a79616dSTakashi Iwai 				      HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
20564a79616dSTakashi Iwai 							  idx, HDA_INPUT));
20574a79616dSTakashi Iwai 		if (err < 0)
20584a79616dSTakashi Iwai 			return err;
2059c577b8a1SJoseph Chan 	}
2060c577b8a1SJoseph Chan 
2061f4a7828bSTakashi Iwai 	cfg->line_outs = old_line_outs;
2062f4a7828bSTakashi Iwai 
2063c577b8a1SJoseph Chan 	return 0;
2064c577b8a1SJoseph Chan }
2065c577b8a1SJoseph Chan 
20664a79616dSTakashi Iwai static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
2067c577b8a1SJoseph Chan {
20684a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
206909a9ad69STakashi Iwai 	struct nid_path *path;
207018bd2c44STakashi Iwai 	bool check_dac;
20713214b966STakashi Iwai 	int i, err;
2072c577b8a1SJoseph Chan 
2073c577b8a1SJoseph Chan 	if (!pin)
2074c577b8a1SJoseph Chan 		return 0;
2075c577b8a1SJoseph Chan 
20763214b966STakashi Iwai 	if (!parse_output_path(codec, pin, 0, 0, &spec->hp_indep_path)) {
20773214b966STakashi Iwai 		for (i = HDA_SIDE; i >= HDA_CLFE; i--) {
20783214b966STakashi Iwai 			if (i < spec->multiout.num_dacs &&
207925250505STakashi Iwai 			    parse_output_path(codec, pin,
20803214b966STakashi Iwai 					      spec->multiout.dac_nids[i], 0,
20813214b966STakashi Iwai 					      &spec->hp_indep_path)) {
20823214b966STakashi Iwai 				spec->hp_indep_shared = i;
20833214b966STakashi Iwai 				break;
208425250505STakashi Iwai 			}
20853214b966STakashi Iwai 		}
20863214b966STakashi Iwai 	}
20873214b966STakashi Iwai 	if (spec->hp_indep_path.depth) {
20883214b966STakashi Iwai 		spec->hp_dac_nid = spec->hp_indep_path.path[0];
20893214b966STakashi Iwai 		if (!spec->hp_indep_shared)
20903214b966STakashi Iwai 			spec->hp_path = spec->hp_indep_path;
20913214b966STakashi Iwai 	}
20923214b966STakashi Iwai 	/* optionally check front-path w/o AA-mix */
20933214b966STakashi Iwai 	if (!spec->hp_path.depth)
20943214b966STakashi Iwai 		parse_output_path(codec, pin,
20953214b966STakashi Iwai 				  spec->multiout.dac_nids[HDA_FRONT], 0,
20963214b966STakashi Iwai 				  &spec->hp_path);
20974a79616dSTakashi Iwai 
2098ece8d043STakashi Iwai 	if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
20993214b966STakashi Iwai 			       1, &spec->hp_mix_path) && !spec->hp_path.depth)
2100ece8d043STakashi Iwai 		return 0;
2101ece8d043STakashi Iwai 
21023214b966STakashi Iwai 	if (spec->hp_path.depth) {
210309a9ad69STakashi Iwai 		path = &spec->hp_path;
210418bd2c44STakashi Iwai 		check_dac = true;
210518bd2c44STakashi Iwai 	} else {
21063214b966STakashi Iwai 		path = &spec->hp_mix_path;
210718bd2c44STakashi Iwai 		check_dac = false;
210818bd2c44STakashi Iwai 	}
210918bd2c44STakashi Iwai 	err = create_ch_ctls(codec, "Headphone", 3, check_dac, path);
21104a79616dSTakashi Iwai 	if (err < 0)
21114a79616dSTakashi Iwai 		return err;
2112020066d1STakashi Iwai 	if (check_dac)
2113020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->hp_mix_path, path);
2114020066d1STakashi Iwai 	else
2115020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->hp_path, path);
2116020066d1STakashi Iwai 	if (spec->hp_indep_path.depth)
2117020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->hp_indep_path, path);
2118c577b8a1SJoseph Chan 	return 0;
2119c577b8a1SJoseph Chan }
2120c577b8a1SJoseph Chan 
21214a918ffeSTakashi Iwai static int via_auto_create_speaker_ctls(struct hda_codec *codec)
21224a918ffeSTakashi Iwai {
21234a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
21243214b966STakashi Iwai 	struct nid_path *path;
21253214b966STakashi Iwai 	bool check_dac;
212681c0a78bSWang Shaoyan 	hda_nid_t pin, dac = 0;
21273214b966STakashi Iwai 	int err;
21284a918ffeSTakashi Iwai 
21294a918ffeSTakashi Iwai 	pin = spec->autocfg.speaker_pins[0];
21304a918ffeSTakashi Iwai 	if (!spec->autocfg.speaker_outs || !pin)
21314a918ffeSTakashi Iwai 		return 0;
21324a918ffeSTakashi Iwai 
21333214b966STakashi Iwai 	if (parse_output_path(codec, pin, 0, 0, &spec->speaker_path))
21348e3679dcSTakashi Iwai 		dac = spec->speaker_path.path[0];
21353214b966STakashi Iwai 	if (!dac)
21363214b966STakashi Iwai 		parse_output_path(codec, pin,
21373214b966STakashi Iwai 				  spec->multiout.dac_nids[HDA_FRONT], 0,
213809a9ad69STakashi Iwai 				  &spec->speaker_path);
21393214b966STakashi Iwai 	if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
21403214b966STakashi Iwai 			       1, &spec->speaker_mix_path) && !dac)
21413214b966STakashi Iwai 		return 0;
21424a918ffeSTakashi Iwai 
21433214b966STakashi Iwai 	/* no AA-path for front? */
21443214b966STakashi Iwai 	if (!spec->out_mix_path.depth && spec->speaker_mix_path.depth)
21453214b966STakashi Iwai 		dac = 0;
21463214b966STakashi Iwai 
21473214b966STakashi Iwai 	spec->speaker_dac_nid = dac;
21483214b966STakashi Iwai 	spec->multiout.extra_out_nid[0] = dac;
21493214b966STakashi Iwai 	if (dac) {
21503214b966STakashi Iwai 		path = &spec->speaker_path;
21513214b966STakashi Iwai 		check_dac = true;
21523214b966STakashi Iwai 	} else {
21533214b966STakashi Iwai 		path = &spec->speaker_mix_path;
21543214b966STakashi Iwai 		check_dac = false;
21553214b966STakashi Iwai 	}
21563214b966STakashi Iwai 	err = create_ch_ctls(codec, "Speaker", 3, check_dac, path);
21573214b966STakashi Iwai 	if (err < 0)
21583214b966STakashi Iwai 		return err;
2159020066d1STakashi Iwai 	if (check_dac)
2160020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->speaker_mix_path, path);
2161020066d1STakashi Iwai 	else
2162020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->speaker_path, path);
21633214b966STakashi Iwai 	return 0;
21643214b966STakashi Iwai }
21653214b966STakashi Iwai 
21663214b966STakashi Iwai #define via_aamix_ctl_info	via_pin_power_ctl_info
21673214b966STakashi Iwai 
21683214b966STakashi Iwai static int via_aamix_ctl_get(struct snd_kcontrol *kcontrol,
21693214b966STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
21703214b966STakashi Iwai {
21713214b966STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
21723214b966STakashi Iwai 	struct via_spec *spec = codec->spec;
21733214b966STakashi Iwai 	ucontrol->value.enumerated.item[0] = spec->aamix_mode;
21743214b966STakashi Iwai 	return 0;
21753214b966STakashi Iwai }
21763214b966STakashi Iwai 
21773214b966STakashi Iwai static void update_aamix_paths(struct hda_codec *codec, int do_mix,
21783214b966STakashi Iwai 			       struct nid_path *nomix, struct nid_path *mix)
21793214b966STakashi Iwai {
21803214b966STakashi Iwai 	if (do_mix) {
21813214b966STakashi Iwai 		activate_output_path(codec, nomix, false, false);
21823214b966STakashi Iwai 		activate_output_path(codec, mix, true, false);
21833214b966STakashi Iwai 	} else {
21843214b966STakashi Iwai 		activate_output_path(codec, mix, false, false);
21853214b966STakashi Iwai 		activate_output_path(codec, nomix, true, false);
21863214b966STakashi Iwai 	}
21873214b966STakashi Iwai }
21883214b966STakashi Iwai 
21893214b966STakashi Iwai static int via_aamix_ctl_put(struct snd_kcontrol *kcontrol,
21903214b966STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
21913214b966STakashi Iwai {
21923214b966STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
21933214b966STakashi Iwai 	struct via_spec *spec = codec->spec;
21943214b966STakashi Iwai 	unsigned int val = ucontrol->value.enumerated.item[0];
21953214b966STakashi Iwai 
21963214b966STakashi Iwai 	if (val == spec->aamix_mode)
21973214b966STakashi Iwai 		return 0;
21983214b966STakashi Iwai 	spec->aamix_mode = val;
21993214b966STakashi Iwai 	/* update front path */
22003214b966STakashi Iwai 	update_aamix_paths(codec, val, &spec->out_path[0], &spec->out_mix_path);
22013214b966STakashi Iwai 	/* update HP path */
22023214b966STakashi Iwai 	if (!spec->hp_independent_mode) {
22033214b966STakashi Iwai 		update_aamix_paths(codec, val, &spec->hp_path,
22043214b966STakashi Iwai 				   &spec->hp_mix_path);
22053214b966STakashi Iwai 	}
22063214b966STakashi Iwai 	/* update speaker path */
22073214b966STakashi Iwai 	update_aamix_paths(codec, val, &spec->speaker_path,
22083214b966STakashi Iwai 			   &spec->speaker_mix_path);
22093214b966STakashi Iwai 	return 1;
22103214b966STakashi Iwai }
22113214b966STakashi Iwai 
22123214b966STakashi Iwai static const struct snd_kcontrol_new via_aamix_ctl_enum = {
22133214b966STakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
22143214b966STakashi Iwai 	.name = "Loopback Mixing",
22153214b966STakashi Iwai 	.info = via_aamix_ctl_info,
22163214b966STakashi Iwai 	.get = via_aamix_ctl_get,
22173214b966STakashi Iwai 	.put = via_aamix_ctl_put,
22183214b966STakashi Iwai };
22193214b966STakashi Iwai 
22203214b966STakashi Iwai static int via_auto_create_loopback_switch(struct hda_codec *codec)
22213214b966STakashi Iwai {
22223214b966STakashi Iwai 	struct via_spec *spec = codec->spec;
22233214b966STakashi Iwai 
22244808d12dSTakashi Iwai 	if (!spec->aa_mix_nid)
22254808d12dSTakashi Iwai 		return 0; /* no loopback switching available */
22264808d12dSTakashi Iwai 	if (!(spec->out_mix_path.depth || spec->hp_mix_path.depth ||
22274808d12dSTakashi Iwai 	      spec->speaker_path.depth))
22283214b966STakashi Iwai 		return 0; /* no loopback switching available */
22293214b966STakashi Iwai 	if (!via_clone_control(spec, &via_aamix_ctl_enum))
22303214b966STakashi Iwai 		return -ENOMEM;
22314a918ffeSTakashi Iwai 	return 0;
22324a918ffeSTakashi Iwai }
22334a918ffeSTakashi Iwai 
2234a766d0d7STakashi Iwai /* look for ADCs */
2235a766d0d7STakashi Iwai static int via_fill_adcs(struct hda_codec *codec)
2236a766d0d7STakashi Iwai {
2237a766d0d7STakashi Iwai 	struct via_spec *spec = codec->spec;
2238a766d0d7STakashi Iwai 	hda_nid_t nid = codec->start_nid;
2239a766d0d7STakashi Iwai 	int i;
2240a766d0d7STakashi Iwai 
2241a766d0d7STakashi Iwai 	for (i = 0; i < codec->num_nodes; i++, nid++) {
2242a766d0d7STakashi Iwai 		unsigned int wcaps = get_wcaps(codec, nid);
2243a766d0d7STakashi Iwai 		if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2244a766d0d7STakashi Iwai 			continue;
2245a766d0d7STakashi Iwai 		if (wcaps & AC_WCAP_DIGITAL)
2246a766d0d7STakashi Iwai 			continue;
2247a766d0d7STakashi Iwai 		if (!(wcaps & AC_WCAP_CONN_LIST))
2248a766d0d7STakashi Iwai 			continue;
2249a766d0d7STakashi Iwai 		if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
2250a766d0d7STakashi Iwai 			return -ENOMEM;
2251a766d0d7STakashi Iwai 		spec->adc_nids[spec->num_adc_nids++] = nid;
2252a766d0d7STakashi Iwai 	}
2253a766d0d7STakashi Iwai 	return 0;
2254a766d0d7STakashi Iwai }
2255a766d0d7STakashi Iwai 
2256a86a88eaSTakashi Iwai /* input-src control */
2257a86a88eaSTakashi Iwai static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
2258a86a88eaSTakashi Iwai 			     struct snd_ctl_elem_info *uinfo)
2259a86a88eaSTakashi Iwai {
2260a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2261a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2262a86a88eaSTakashi Iwai 
2263a86a88eaSTakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2264a86a88eaSTakashi Iwai 	uinfo->count = 1;
2265a86a88eaSTakashi Iwai 	uinfo->value.enumerated.items = spec->num_inputs;
2266a86a88eaSTakashi Iwai 	if (uinfo->value.enumerated.item >= spec->num_inputs)
2267a86a88eaSTakashi Iwai 		uinfo->value.enumerated.item = spec->num_inputs - 1;
2268a86a88eaSTakashi Iwai 	strcpy(uinfo->value.enumerated.name,
2269a86a88eaSTakashi Iwai 	       spec->inputs[uinfo->value.enumerated.item].label);
2270a86a88eaSTakashi Iwai 	return 0;
2271a86a88eaSTakashi Iwai }
2272a86a88eaSTakashi Iwai 
2273a86a88eaSTakashi Iwai static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
2274a86a88eaSTakashi Iwai 			    struct snd_ctl_elem_value *ucontrol)
2275a86a88eaSTakashi Iwai {
2276a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2277a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2278a86a88eaSTakashi Iwai 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2279a86a88eaSTakashi Iwai 
2280a86a88eaSTakashi Iwai 	ucontrol->value.enumerated.item[0] = spec->cur_mux[idx];
2281a86a88eaSTakashi Iwai 	return 0;
2282a86a88eaSTakashi Iwai }
2283a86a88eaSTakashi Iwai 
2284a86a88eaSTakashi Iwai static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
2285a86a88eaSTakashi Iwai 			    struct snd_ctl_elem_value *ucontrol)
2286a86a88eaSTakashi Iwai {
2287a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2288a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2289a86a88eaSTakashi Iwai 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2290a86a88eaSTakashi Iwai 	hda_nid_t mux;
2291a86a88eaSTakashi Iwai 	int cur;
2292a86a88eaSTakashi Iwai 
2293a86a88eaSTakashi Iwai 	cur = ucontrol->value.enumerated.item[0];
2294a86a88eaSTakashi Iwai 	if (cur < 0 || cur >= spec->num_inputs)
2295a86a88eaSTakashi Iwai 		return -EINVAL;
2296a86a88eaSTakashi Iwai 	if (spec->cur_mux[idx] == cur)
2297a86a88eaSTakashi Iwai 		return 0;
2298a86a88eaSTakashi Iwai 	spec->cur_mux[idx] = cur;
2299a86a88eaSTakashi Iwai 	if (spec->dyn_adc_switch) {
2300a86a88eaSTakashi Iwai 		int adc_idx = spec->inputs[cur].adc_idx;
2301a86a88eaSTakashi Iwai 		mux = spec->mux_nids[adc_idx];
2302a86a88eaSTakashi Iwai 		via_dyn_adc_pcm_resetup(codec, cur);
2303a86a88eaSTakashi Iwai 	} else {
2304a86a88eaSTakashi Iwai 		mux = spec->mux_nids[idx];
2305a86a88eaSTakashi Iwai 		if (snd_BUG_ON(!mux))
2306a86a88eaSTakashi Iwai 			return -EINVAL;
2307a86a88eaSTakashi Iwai 	}
2308a86a88eaSTakashi Iwai 
2309a86a88eaSTakashi Iwai 	if (mux) {
2310a86a88eaSTakashi Iwai 		/* switch to D0 beofre change index */
2311054d867eSTakashi Iwai 		update_power_state(codec, mux, AC_PWRST_D0);
2312a86a88eaSTakashi Iwai 		snd_hda_codec_write(codec, mux, 0,
2313a86a88eaSTakashi Iwai 				    AC_VERB_SET_CONNECT_SEL,
2314a86a88eaSTakashi Iwai 				    spec->inputs[cur].mux_idx);
2315a86a88eaSTakashi Iwai 	}
2316a86a88eaSTakashi Iwai 
2317a86a88eaSTakashi Iwai 	/* update jack power state */
2318a86a88eaSTakashi Iwai 	set_widgets_power_state(codec);
2319a86a88eaSTakashi Iwai 	return 0;
2320a86a88eaSTakashi Iwai }
2321a766d0d7STakashi Iwai 
2322d7a99cceSTakashi Iwai static const struct snd_kcontrol_new via_input_src_ctl = {
2323d7a99cceSTakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2324d7a99cceSTakashi Iwai 	/* The multiple "Capture Source" controls confuse alsamixer
2325d7a99cceSTakashi Iwai 	 * So call somewhat different..
2326d7a99cceSTakashi Iwai 	 */
2327d7a99cceSTakashi Iwai 	/* .name = "Capture Source", */
2328d7a99cceSTakashi Iwai 	.name = "Input Source",
2329d7a99cceSTakashi Iwai 	.info = via_mux_enum_info,
2330d7a99cceSTakashi Iwai 	.get = via_mux_enum_get,
2331d7a99cceSTakashi Iwai 	.put = via_mux_enum_put,
2332d7a99cceSTakashi Iwai };
2333d7a99cceSTakashi Iwai 
2334a86a88eaSTakashi Iwai static int create_input_src_ctls(struct hda_codec *codec, int count)
2335a86a88eaSTakashi Iwai {
2336a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2337a86a88eaSTakashi Iwai 	struct snd_kcontrol_new *knew;
2338a86a88eaSTakashi Iwai 
2339a86a88eaSTakashi Iwai 	if (spec->num_inputs <= 1 || !count)
2340a86a88eaSTakashi Iwai 		return 0; /* no need for single src */
2341a86a88eaSTakashi Iwai 
2342a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_input_src_ctl);
2343a86a88eaSTakashi Iwai 	if (!knew)
2344a86a88eaSTakashi Iwai 		return -ENOMEM;
2345a86a88eaSTakashi Iwai 	knew->count = count;
2346a86a88eaSTakashi Iwai 	return 0;
2347a86a88eaSTakashi Iwai }
2348a86a88eaSTakashi Iwai 
2349a86a88eaSTakashi Iwai /* add the powersave loopback-list entry */
235013af8e77STakashi Iwai static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
235113af8e77STakashi Iwai {
235213af8e77STakashi Iwai 	struct hda_amp_list *list;
235313af8e77STakashi Iwai 
235413af8e77STakashi Iwai 	if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
235513af8e77STakashi Iwai 		return;
235613af8e77STakashi Iwai 	list = spec->loopback_list + spec->num_loopbacks;
235713af8e77STakashi Iwai 	list->nid = mix;
235813af8e77STakashi Iwai 	list->dir = HDA_INPUT;
235913af8e77STakashi Iwai 	list->idx = idx;
236013af8e77STakashi Iwai 	spec->num_loopbacks++;
236113af8e77STakashi Iwai 	spec->loopback.amplist = spec->loopback_list;
236213af8e77STakashi Iwai }
236313af8e77STakashi Iwai 
2364a86a88eaSTakashi Iwai static bool is_reachable_nid(struct hda_codec *codec, hda_nid_t src,
23658d087c76STakashi Iwai 			     hda_nid_t dst)
2366a86a88eaSTakashi Iwai {
23678d087c76STakashi Iwai 	return snd_hda_get_conn_index(codec, src, dst, 1) >= 0;
2368a86a88eaSTakashi Iwai }
2369a86a88eaSTakashi Iwai 
2370a86a88eaSTakashi Iwai /* add the input-route to the given pin */
2371a86a88eaSTakashi Iwai static bool add_input_route(struct hda_codec *codec, hda_nid_t pin)
2372c577b8a1SJoseph Chan {
237310a20af7STakashi Iwai 	struct via_spec *spec = codec->spec;
2374a86a88eaSTakashi Iwai 	int c, idx;
2375a86a88eaSTakashi Iwai 
2376a86a88eaSTakashi Iwai 	spec->inputs[spec->num_inputs].adc_idx = -1;
2377a86a88eaSTakashi Iwai 	spec->inputs[spec->num_inputs].pin = pin;
2378a86a88eaSTakashi Iwai 	for (c = 0; c < spec->num_adc_nids; c++) {
2379a86a88eaSTakashi Iwai 		if (spec->mux_nids[c]) {
2380a86a88eaSTakashi Iwai 			idx = get_connection_index(codec, spec->mux_nids[c],
2381a86a88eaSTakashi Iwai 						   pin);
2382a86a88eaSTakashi Iwai 			if (idx < 0)
2383a86a88eaSTakashi Iwai 				continue;
2384a86a88eaSTakashi Iwai 			spec->inputs[spec->num_inputs].mux_idx = idx;
2385a86a88eaSTakashi Iwai 		} else {
23868d087c76STakashi Iwai 			if (!is_reachable_nid(codec, spec->adc_nids[c], pin))
2387a86a88eaSTakashi Iwai 				continue;
2388a86a88eaSTakashi Iwai 		}
2389a86a88eaSTakashi Iwai 		spec->inputs[spec->num_inputs].adc_idx = c;
2390a86a88eaSTakashi Iwai 		/* Can primary ADC satisfy all inputs? */
2391a86a88eaSTakashi Iwai 		if (!spec->dyn_adc_switch &&
2392a86a88eaSTakashi Iwai 		    spec->num_inputs > 0 && spec->inputs[0].adc_idx != c) {
2393a86a88eaSTakashi Iwai 			snd_printd(KERN_INFO
2394a86a88eaSTakashi Iwai 				   "via: dynamic ADC switching enabled\n");
2395a86a88eaSTakashi Iwai 			spec->dyn_adc_switch = 1;
2396a86a88eaSTakashi Iwai 		}
2397a86a88eaSTakashi Iwai 		return true;
2398a86a88eaSTakashi Iwai 	}
2399a86a88eaSTakashi Iwai 	return false;
2400a86a88eaSTakashi Iwai }
2401a86a88eaSTakashi Iwai 
2402a86a88eaSTakashi Iwai static int get_mux_nids(struct hda_codec *codec);
2403a86a88eaSTakashi Iwai 
2404a86a88eaSTakashi Iwai /* parse input-routes; fill ADCs, MUXs and input-src entries */
2405a86a88eaSTakashi Iwai static int parse_analog_inputs(struct hda_codec *codec)
2406a86a88eaSTakashi Iwai {
2407a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2408a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2409a86a88eaSTakashi Iwai 	int i, err;
2410a766d0d7STakashi Iwai 
2411a766d0d7STakashi Iwai 	err = via_fill_adcs(codec);
2412a766d0d7STakashi Iwai 	if (err < 0)
2413a766d0d7STakashi Iwai 		return err;
2414a766d0d7STakashi Iwai 	err = get_mux_nids(codec);
2415a766d0d7STakashi Iwai 	if (err < 0)
2416a766d0d7STakashi Iwai 		return err;
2417a766d0d7STakashi Iwai 
2418a86a88eaSTakashi Iwai 	/* fill all input-routes */
2419a86a88eaSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2420a86a88eaSTakashi Iwai 		if (add_input_route(codec, cfg->inputs[i].pin))
2421a86a88eaSTakashi Iwai 			spec->inputs[spec->num_inputs++].label =
2422a86a88eaSTakashi Iwai 				hda_get_autocfg_input_label(codec, cfg, i);
2423a86a88eaSTakashi Iwai 	}
2424a86a88eaSTakashi Iwai 
2425a86a88eaSTakashi Iwai 	/* check for internal loopback recording */
2426a86a88eaSTakashi Iwai 	if (spec->aa_mix_nid &&
2427a86a88eaSTakashi Iwai 	    add_input_route(codec, spec->aa_mix_nid))
2428a86a88eaSTakashi Iwai 		spec->inputs[spec->num_inputs++].label = "Stereo Mixer";
2429a86a88eaSTakashi Iwai 
2430a86a88eaSTakashi Iwai 	return 0;
2431a86a88eaSTakashi Iwai }
2432a86a88eaSTakashi Iwai 
2433a86a88eaSTakashi Iwai /* create analog-loopback volume/switch controls */
2434a86a88eaSTakashi Iwai static int create_loopback_ctls(struct hda_codec *codec)
2435a86a88eaSTakashi Iwai {
2436a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2437a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2438a86a88eaSTakashi Iwai 	const char *prev_label = NULL;
2439a86a88eaSTakashi Iwai 	int type_idx = 0;
2440a86a88eaSTakashi Iwai 	int i, j, err, idx;
2441a86a88eaSTakashi Iwai 
2442a86a88eaSTakashi Iwai 	if (!spec->aa_mix_nid)
2443a766d0d7STakashi Iwai 		return 0;
2444c577b8a1SJoseph Chan 
24457b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2446a86a88eaSTakashi Iwai 		hda_nid_t pin = cfg->inputs[i].pin;
2447a86a88eaSTakashi Iwai 		const char *label = hda_get_autocfg_input_label(codec, cfg, i);
2448a86a88eaSTakashi Iwai 
24491e11cae1STakashi Iwai 		if (prev_label && !strcmp(label, prev_label))
24507b315bb4STakashi Iwai 			type_idx++;
24517b315bb4STakashi Iwai 		else
24527b315bb4STakashi Iwai 			type_idx = 0;
24531e11cae1STakashi Iwai 		prev_label = label;
2454a86a88eaSTakashi Iwai 		idx = get_connection_index(codec, spec->aa_mix_nid, pin);
2455a86a88eaSTakashi Iwai 		if (idx >= 0) {
245616922281SLydia Wang 			err = via_new_analog_input(spec, label, type_idx,
2457a86a88eaSTakashi Iwai 						   idx, spec->aa_mix_nid);
2458c577b8a1SJoseph Chan 			if (err < 0)
2459c577b8a1SJoseph Chan 				return err;
2460a86a88eaSTakashi Iwai 			add_loopback_list(spec, spec->aa_mix_nid, idx);
246113af8e77STakashi Iwai 		}
2462e3d7a143STakashi Iwai 
2463e3d7a143STakashi Iwai 		/* remember the label for smart51 control */
2464e3d7a143STakashi Iwai 		for (j = 0; j < spec->smart51_nums; j++) {
2465a86a88eaSTakashi Iwai 			if (spec->smart51_pins[j] == pin) {
2466e3d7a143STakashi Iwai 				spec->smart51_idxs[j] = idx;
2467e3d7a143STakashi Iwai 				spec->smart51_labels[j] = label;
2468e3d7a143STakashi Iwai 				break;
2469e3d7a143STakashi Iwai 			}
2470e3d7a143STakashi Iwai 		}
2471c577b8a1SJoseph Chan 	}
2472a86a88eaSTakashi Iwai 	return 0;
2473a86a88eaSTakashi Iwai }
2474a86a88eaSTakashi Iwai 
2475a86a88eaSTakashi Iwai /* create mic-boost controls (if present) */
2476a86a88eaSTakashi Iwai static int create_mic_boost_ctls(struct hda_codec *codec)
2477a86a88eaSTakashi Iwai {
2478a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2479a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
24808d8bbc6fSTakashi Iwai 	const char *prev_label = NULL;
24818d8bbc6fSTakashi Iwai 	int type_idx = 0;
2482a86a88eaSTakashi Iwai 	int i, err;
2483a86a88eaSTakashi Iwai 
2484a86a88eaSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2485a86a88eaSTakashi Iwai 		hda_nid_t pin = cfg->inputs[i].pin;
2486a86a88eaSTakashi Iwai 		unsigned int caps;
2487a86a88eaSTakashi Iwai 		const char *label;
2488a86a88eaSTakashi Iwai 		char name[32];
2489a86a88eaSTakashi Iwai 
2490a86a88eaSTakashi Iwai 		if (cfg->inputs[i].type != AUTO_PIN_MIC)
2491a86a88eaSTakashi Iwai 			continue;
2492a86a88eaSTakashi Iwai 		caps = query_amp_caps(codec, pin, HDA_INPUT);
2493a86a88eaSTakashi Iwai 		if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
2494a86a88eaSTakashi Iwai 			continue;
2495a86a88eaSTakashi Iwai 		label = hda_get_autocfg_input_label(codec, cfg, i);
24968d8bbc6fSTakashi Iwai 		if (prev_label && !strcmp(label, prev_label))
24978d8bbc6fSTakashi Iwai 			type_idx++;
24988d8bbc6fSTakashi Iwai 		else
24998d8bbc6fSTakashi Iwai 			type_idx = 0;
25008d8bbc6fSTakashi Iwai 		prev_label = label;
2501a86a88eaSTakashi Iwai 		snprintf(name, sizeof(name), "%s Boost Volume", label);
25028d8bbc6fSTakashi Iwai 		err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
2503a86a88eaSTakashi Iwai 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
2504a86a88eaSTakashi Iwai 		if (err < 0)
2505a86a88eaSTakashi Iwai 			return err;
2506a86a88eaSTakashi Iwai 	}
2507a86a88eaSTakashi Iwai 	return 0;
2508a86a88eaSTakashi Iwai }
2509a86a88eaSTakashi Iwai 
2510a86a88eaSTakashi Iwai /* create capture and input-src controls for multiple streams */
2511a86a88eaSTakashi Iwai static int create_multi_adc_ctls(struct hda_codec *codec)
2512a86a88eaSTakashi Iwai {
2513a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2514a86a88eaSTakashi Iwai 	int i, err;
2515d7a99cceSTakashi Iwai 
2516d7a99cceSTakashi Iwai 	/* create capture mixer elements */
2517d7a99cceSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
2518d7a99cceSTakashi Iwai 		hda_nid_t adc = spec->adc_nids[i];
2519d7a99cceSTakashi Iwai 		err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
2520d7a99cceSTakashi Iwai 					"Capture Volume", i,
2521d7a99cceSTakashi Iwai 					HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2522d7a99cceSTakashi Iwai 							    HDA_INPUT));
2523d7a99cceSTakashi Iwai 		if (err < 0)
2524d7a99cceSTakashi Iwai 			return err;
2525d7a99cceSTakashi Iwai 		err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2526d7a99cceSTakashi Iwai 					"Capture Switch", i,
2527d7a99cceSTakashi Iwai 					HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2528d7a99cceSTakashi Iwai 							    HDA_INPUT));
2529d7a99cceSTakashi Iwai 		if (err < 0)
2530d7a99cceSTakashi Iwai 			return err;
2531d7a99cceSTakashi Iwai 	}
2532d7a99cceSTakashi Iwai 
2533d7a99cceSTakashi Iwai 	/* input-source control */
2534d7a99cceSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++)
2535d7a99cceSTakashi Iwai 		if (!spec->mux_nids[i])
2536d7a99cceSTakashi Iwai 			break;
2537a86a88eaSTakashi Iwai 	err = create_input_src_ctls(codec, i);
2538d7a99cceSTakashi Iwai 	if (err < 0)
2539d7a99cceSTakashi Iwai 		return err;
2540a86a88eaSTakashi Iwai 	return 0;
2541d7a99cceSTakashi Iwai }
2542d7a99cceSTakashi Iwai 
2543a86a88eaSTakashi Iwai /* bind capture volume/switch */
2544a86a88eaSTakashi Iwai static struct snd_kcontrol_new via_bind_cap_vol_ctl =
2545a86a88eaSTakashi Iwai 	HDA_BIND_VOL("Capture Volume", 0);
2546a86a88eaSTakashi Iwai static struct snd_kcontrol_new via_bind_cap_sw_ctl =
2547a86a88eaSTakashi Iwai 	HDA_BIND_SW("Capture Switch", 0);
2548a86a88eaSTakashi Iwai 
2549a86a88eaSTakashi Iwai static int init_bind_ctl(struct via_spec *spec, struct hda_bind_ctls **ctl_ret,
2550a86a88eaSTakashi Iwai 			 struct hda_ctl_ops *ops)
2551a86a88eaSTakashi Iwai {
2552a86a88eaSTakashi Iwai 	struct hda_bind_ctls *ctl;
2553a86a88eaSTakashi Iwai 	int i;
2554a86a88eaSTakashi Iwai 
2555a86a88eaSTakashi Iwai 	ctl = kzalloc(sizeof(*ctl) + sizeof(long) * 4, GFP_KERNEL);
2556a86a88eaSTakashi Iwai 	if (!ctl)
2557a86a88eaSTakashi Iwai 		return -ENOMEM;
2558a86a88eaSTakashi Iwai 	ctl->ops = ops;
2559a86a88eaSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++)
2560a86a88eaSTakashi Iwai 		ctl->values[i] =
2561a86a88eaSTakashi Iwai 			HDA_COMPOSE_AMP_VAL(spec->adc_nids[i], 3, 0, HDA_INPUT);
2562a86a88eaSTakashi Iwai 	*ctl_ret = ctl;
2563a86a88eaSTakashi Iwai 	return 0;
2564a86a88eaSTakashi Iwai }
2565a86a88eaSTakashi Iwai 
2566a86a88eaSTakashi Iwai /* create capture and input-src controls for dynamic ADC-switch case */
2567a86a88eaSTakashi Iwai static int create_dyn_adc_ctls(struct hda_codec *codec)
2568a86a88eaSTakashi Iwai {
2569a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2570a86a88eaSTakashi Iwai 	struct snd_kcontrol_new *knew;
2571a86a88eaSTakashi Iwai 	int err;
2572a86a88eaSTakashi Iwai 
2573a86a88eaSTakashi Iwai 	/* set up the bind capture ctls */
2574a86a88eaSTakashi Iwai 	err = init_bind_ctl(spec, &spec->bind_cap_vol, &snd_hda_bind_vol);
2575a86a88eaSTakashi Iwai 	if (err < 0)
2576a86a88eaSTakashi Iwai 		return err;
2577a86a88eaSTakashi Iwai 	err = init_bind_ctl(spec, &spec->bind_cap_sw, &snd_hda_bind_sw);
2578a86a88eaSTakashi Iwai 	if (err < 0)
2579a86a88eaSTakashi Iwai 		return err;
2580a86a88eaSTakashi Iwai 
2581a86a88eaSTakashi Iwai 	/* create capture mixer elements */
2582a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_bind_cap_vol_ctl);
2583a86a88eaSTakashi Iwai 	if (!knew)
2584a86a88eaSTakashi Iwai 		return -ENOMEM;
2585a86a88eaSTakashi Iwai 	knew->private_value = (long)spec->bind_cap_vol;
2586a86a88eaSTakashi Iwai 
2587a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_bind_cap_sw_ctl);
2588a86a88eaSTakashi Iwai 	if (!knew)
2589a86a88eaSTakashi Iwai 		return -ENOMEM;
2590a86a88eaSTakashi Iwai 	knew->private_value = (long)spec->bind_cap_sw;
2591a86a88eaSTakashi Iwai 
2592a86a88eaSTakashi Iwai 	/* input-source control */
2593a86a88eaSTakashi Iwai 	err = create_input_src_ctls(codec, 1);
2594a86a88eaSTakashi Iwai 	if (err < 0)
2595a86a88eaSTakashi Iwai 		return err;
2596a86a88eaSTakashi Iwai 	return 0;
2597a86a88eaSTakashi Iwai }
2598a86a88eaSTakashi Iwai 
2599a86a88eaSTakashi Iwai /* parse and create capture-related stuff */
2600a86a88eaSTakashi Iwai static int via_auto_create_analog_input_ctls(struct hda_codec *codec)
2601a86a88eaSTakashi Iwai {
2602a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2603a86a88eaSTakashi Iwai 	int err;
2604a86a88eaSTakashi Iwai 
2605a86a88eaSTakashi Iwai 	err = parse_analog_inputs(codec);
2606a86a88eaSTakashi Iwai 	if (err < 0)
2607a86a88eaSTakashi Iwai 		return err;
2608a86a88eaSTakashi Iwai 	if (spec->dyn_adc_switch)
2609a86a88eaSTakashi Iwai 		err = create_dyn_adc_ctls(codec);
2610a86a88eaSTakashi Iwai 	else
2611a86a88eaSTakashi Iwai 		err = create_multi_adc_ctls(codec);
2612a86a88eaSTakashi Iwai 	if (err < 0)
2613a86a88eaSTakashi Iwai 		return err;
2614a86a88eaSTakashi Iwai 	err = create_loopback_ctls(codec);
2615a86a88eaSTakashi Iwai 	if (err < 0)
2616a86a88eaSTakashi Iwai 		return err;
2617a86a88eaSTakashi Iwai 	err = create_mic_boost_ctls(codec);
2618a86a88eaSTakashi Iwai 	if (err < 0)
2619a86a88eaSTakashi Iwai 		return err;
2620c577b8a1SJoseph Chan 	return 0;
2621c577b8a1SJoseph Chan }
2622c577b8a1SJoseph Chan 
262376d9b0ddSHarald Welte static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
262476d9b0ddSHarald Welte {
262576d9b0ddSHarald Welte 	unsigned int def_conf;
262676d9b0ddSHarald Welte 	unsigned char seqassoc;
262776d9b0ddSHarald Welte 
26282f334f92STakashi Iwai 	def_conf = snd_hda_codec_get_pincfg(codec, nid);
262976d9b0ddSHarald Welte 	seqassoc = (unsigned char) get_defcfg_association(def_conf);
263076d9b0ddSHarald Welte 	seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
263182ef9e45SLydia Wang 	if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
263282ef9e45SLydia Wang 	    && (seqassoc == 0xf0 || seqassoc == 0xff)) {
263376d9b0ddSHarald Welte 		def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
26342f334f92STakashi Iwai 		snd_hda_codec_set_pincfg(codec, nid, def_conf);
263576d9b0ddSHarald Welte 	}
263676d9b0ddSHarald Welte 
263776d9b0ddSHarald Welte 	return;
263876d9b0ddSHarald Welte }
263976d9b0ddSHarald Welte 
2640e06e5a29STakashi Iwai static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
26411f2e99feSLydia Wang 				     struct snd_ctl_elem_value *ucontrol)
26421f2e99feSLydia Wang {
26431f2e99feSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
26441f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
26451f2e99feSLydia Wang 
26461f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
26471f2e99feSLydia Wang 		return 0;
2648e06e5a29STakashi Iwai 	ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
26491f2e99feSLydia Wang 	return 0;
26501f2e99feSLydia Wang }
26511f2e99feSLydia Wang 
2652e06e5a29STakashi Iwai static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
26531f2e99feSLydia Wang 				     struct snd_ctl_elem_value *ucontrol)
26541f2e99feSLydia Wang {
26551f2e99feSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
26561f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
2657187d333eSTakashi Iwai 	int val;
26581f2e99feSLydia Wang 
26591f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
26601f2e99feSLydia Wang 		return 0;
2661187d333eSTakashi Iwai 	val = !!ucontrol->value.integer.value[0];
2662187d333eSTakashi Iwai 	if (spec->vt1708_jack_detect == val)
2663187d333eSTakashi Iwai 		return 0;
2664187d333eSTakashi Iwai 	spec->vt1708_jack_detect = val;
2665187d333eSTakashi Iwai 	if (spec->vt1708_jack_detect &&
2666187d333eSTakashi Iwai 	    snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") != 1) {
26671f2e99feSLydia Wang 		mute_aa_path(codec, 1);
26681f2e99feSLydia Wang 		notify_aa_path_ctls(codec);
26691f2e99feSLydia Wang 	}
2670187d333eSTakashi Iwai 	via_hp_automute(codec);
2671187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
2672187d333eSTakashi Iwai 	return 1;
26731f2e99feSLydia Wang }
26741f2e99feSLydia Wang 
2675e06e5a29STakashi Iwai static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
26761f2e99feSLydia Wang 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
26771f2e99feSLydia Wang 	.name = "Jack Detect",
26781f2e99feSLydia Wang 	.count = 1,
26791f2e99feSLydia Wang 	.info = snd_ctl_boolean_mono_info,
2680e06e5a29STakashi Iwai 	.get = vt1708_jack_detect_get,
2681e06e5a29STakashi Iwai 	.put = vt1708_jack_detect_put,
26821f2e99feSLydia Wang };
26831f2e99feSLydia Wang 
268412daef65STakashi Iwai static void fill_dig_outs(struct hda_codec *codec);
268512daef65STakashi Iwai static void fill_dig_in(struct hda_codec *codec);
268612daef65STakashi Iwai 
268712daef65STakashi Iwai static int via_parse_auto_config(struct hda_codec *codec)
2688c577b8a1SJoseph Chan {
2689c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
2690c577b8a1SJoseph Chan 	int err;
2691c577b8a1SJoseph Chan 
2692c577b8a1SJoseph Chan 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2693c577b8a1SJoseph Chan 	if (err < 0)
2694c577b8a1SJoseph Chan 		return err;
2695c577b8a1SJoseph Chan 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
26967f0df88cSTakashi Iwai 		return -EINVAL;
2697c577b8a1SJoseph Chan 
26984a79616dSTakashi Iwai 	err = via_auto_create_multi_out_ctls(codec);
2699c577b8a1SJoseph Chan 	if (err < 0)
2700c577b8a1SJoseph Chan 		return err;
27014a79616dSTakashi Iwai 	err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
2702c577b8a1SJoseph Chan 	if (err < 0)
2703c577b8a1SJoseph Chan 		return err;
27044a918ffeSTakashi Iwai 	err = via_auto_create_speaker_ctls(codec);
27054a918ffeSTakashi Iwai 	if (err < 0)
27064a918ffeSTakashi Iwai 		return err;
27073214b966STakashi Iwai 	err = via_auto_create_loopback_switch(codec);
27083214b966STakashi Iwai 	if (err < 0)
27093214b966STakashi Iwai 		return err;
2710a86a88eaSTakashi Iwai 	err = via_auto_create_analog_input_ctls(codec);
2711c577b8a1SJoseph Chan 	if (err < 0)
2712c577b8a1SJoseph Chan 		return err;
2713c577b8a1SJoseph Chan 
2714c577b8a1SJoseph Chan 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2715c577b8a1SJoseph Chan 
271612daef65STakashi Iwai 	fill_dig_outs(codec);
271712daef65STakashi Iwai 	fill_dig_in(codec);
2718c577b8a1SJoseph Chan 
2719603c4019STakashi Iwai 	if (spec->kctls.list)
2720603c4019STakashi Iwai 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
2721c577b8a1SJoseph Chan 
2722c577b8a1SJoseph Chan 
27233214b966STakashi Iwai 	if (spec->hp_dac_nid && spec->hp_mix_path.depth) {
2724ece8d043STakashi Iwai 		err = via_hp_build(codec);
2725ece8d043STakashi Iwai 		if (err < 0)
2726ece8d043STakashi Iwai 			return err;
2727ece8d043STakashi Iwai 	}
2728c577b8a1SJoseph Chan 
2729f4a7828bSTakashi Iwai 	err = via_smart51_build(codec);
2730f4a7828bSTakashi Iwai 	if (err < 0)
2731f4a7828bSTakashi Iwai 		return err;
2732f4a7828bSTakashi Iwai 
27335d41762aSTakashi Iwai 	/* assign slave outs */
27345d41762aSTakashi Iwai 	if (spec->slave_dig_outs[0])
27355d41762aSTakashi Iwai 		codec->slave_dig_outs = spec->slave_dig_outs;
27365d41762aSTakashi Iwai 
2737c577b8a1SJoseph Chan 	return 1;
2738c577b8a1SJoseph Chan }
2739c577b8a1SJoseph Chan 
27405d41762aSTakashi Iwai static void via_auto_init_dig_outs(struct hda_codec *codec)
2741c577b8a1SJoseph Chan {
274225eaba2fSLydia Wang 	struct via_spec *spec = codec->spec;
27435d41762aSTakashi Iwai 	if (spec->multiout.dig_out_nid)
27445d41762aSTakashi Iwai 		init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
27455d41762aSTakashi Iwai 	if (spec->slave_dig_outs[0])
27465d41762aSTakashi Iwai 		init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
27475d41762aSTakashi Iwai }
274825eaba2fSLydia Wang 
27495d41762aSTakashi Iwai static void via_auto_init_dig_in(struct hda_codec *codec)
27505d41762aSTakashi Iwai {
27515d41762aSTakashi Iwai 	struct via_spec *spec = codec->spec;
27525d41762aSTakashi Iwai 	if (!spec->dig_in_nid)
27535d41762aSTakashi Iwai 		return;
2754cdd03cedSTakashi Iwai 	snd_hda_set_pin_ctl(codec, spec->autocfg.dig_in_pin, PIN_IN);
27555d41762aSTakashi Iwai }
27565d41762aSTakashi Iwai 
27574a918ffeSTakashi Iwai /* initialize the unsolicited events */
27584a918ffeSTakashi Iwai static void via_auto_init_unsol_event(struct hda_codec *codec)
27594a918ffeSTakashi Iwai {
27604a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
27614a918ffeSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
27624a918ffeSTakashi Iwai 	unsigned int ev;
27634a918ffeSTakashi Iwai 	int i;
27644a918ffeSTakashi Iwai 
27654a918ffeSTakashi Iwai 	if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
27661835a0f9STakashi Iwai 		snd_hda_jack_detect_enable(codec, cfg->hp_pins[0],
27671835a0f9STakashi Iwai 					   VIA_HP_EVENT | VIA_JACK_EVENT);
27684a918ffeSTakashi Iwai 
27694a918ffeSTakashi Iwai 	if (cfg->speaker_pins[0])
27704a918ffeSTakashi Iwai 		ev = VIA_LINE_EVENT;
27714a918ffeSTakashi Iwai 	else
27724a918ffeSTakashi Iwai 		ev = 0;
27734a918ffeSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
27744a918ffeSTakashi Iwai 		if (cfg->line_out_pins[i] &&
27754a918ffeSTakashi Iwai 		    is_jack_detectable(codec, cfg->line_out_pins[i]))
27761835a0f9STakashi Iwai 			snd_hda_jack_detect_enable(codec, cfg->line_out_pins[i],
27771835a0f9STakashi Iwai 						   ev | VIA_JACK_EVENT);
27784a918ffeSTakashi Iwai 	}
27794a918ffeSTakashi Iwai 
27804a918ffeSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
27814a918ffeSTakashi Iwai 		if (is_jack_detectable(codec, cfg->inputs[i].pin))
27821835a0f9STakashi Iwai 			snd_hda_jack_detect_enable(codec, cfg->inputs[i].pin,
27831835a0f9STakashi Iwai 						   VIA_JACK_EVENT);
27844a918ffeSTakashi Iwai 	}
27854a918ffeSTakashi Iwai }
27864a918ffeSTakashi Iwai 
27875d41762aSTakashi Iwai static int via_init(struct hda_codec *codec)
27885d41762aSTakashi Iwai {
27895d41762aSTakashi Iwai 	struct via_spec *spec = codec->spec;
27905d41762aSTakashi Iwai 	int i;
27915d41762aSTakashi Iwai 
27925d41762aSTakashi Iwai 	for (i = 0; i < spec->num_iverbs; i++)
27935d41762aSTakashi Iwai 		snd_hda_sequence_write(codec, spec->init_verbs[i]);
27945d41762aSTakashi Iwai 
2795e9d010c2STakashi Iwai 	/* init power states */
2796e9d010c2STakashi Iwai 	set_widgets_power_state(codec);
2797e9d010c2STakashi Iwai 	__analog_low_current_mode(codec, true);
2798e9d010c2STakashi Iwai 
2799c577b8a1SJoseph Chan 	via_auto_init_multi_out(codec);
2800c577b8a1SJoseph Chan 	via_auto_init_hp_out(codec);
28014a918ffeSTakashi Iwai 	via_auto_init_speaker_out(codec);
2802c577b8a1SJoseph Chan 	via_auto_init_analog_input(codec);
28035d41762aSTakashi Iwai 	via_auto_init_dig_outs(codec);
28045d41762aSTakashi Iwai 	via_auto_init_dig_in(codec);
280511890956SLydia Wang 
28064a918ffeSTakashi Iwai 	via_auto_init_unsol_event(codec);
28074a918ffeSTakashi Iwai 
280825eaba2fSLydia Wang 	via_hp_automute(codec);
2809187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
281001a61e12STakashi Iwai 	snd_hda_jack_report_sync(codec);
281125eaba2fSLydia Wang 
2812c577b8a1SJoseph Chan 	return 0;
2813c577b8a1SJoseph Chan }
2814c577b8a1SJoseph Chan 
28151f2e99feSLydia Wang static void vt1708_update_hp_jack_state(struct work_struct *work)
28161f2e99feSLydia Wang {
28171f2e99feSLydia Wang 	struct via_spec *spec = container_of(work, struct via_spec,
28181f2e99feSLydia Wang 					     vt1708_hp_work.work);
28191f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
28201f2e99feSLydia Wang 		return;
28211835a0f9STakashi Iwai 	snd_hda_jack_set_dirty_all(spec->codec);
28221f2e99feSLydia Wang 	/* if jack state toggled */
28231f2e99feSLydia Wang 	if (spec->vt1708_hp_present
2824d56757abSTakashi Iwai 	    != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
28251f2e99feSLydia Wang 		spec->vt1708_hp_present ^= 1;
28261f2e99feSLydia Wang 		via_hp_automute(spec->codec);
28271f2e99feSLydia Wang 	}
2828187d333eSTakashi Iwai 	if (spec->vt1708_jack_detect)
2829187d333eSTakashi Iwai 		schedule_delayed_work(&spec->vt1708_hp_work,
2830187d333eSTakashi Iwai 				      msecs_to_jiffies(100));
28311f2e99feSLydia Wang }
28321f2e99feSLydia Wang 
2833337b9d02STakashi Iwai static int get_mux_nids(struct hda_codec *codec)
2834337b9d02STakashi Iwai {
2835337b9d02STakashi Iwai 	struct via_spec *spec = codec->spec;
2836337b9d02STakashi Iwai 	hda_nid_t nid, conn[8];
2837337b9d02STakashi Iwai 	unsigned int type;
2838337b9d02STakashi Iwai 	int i, n;
2839337b9d02STakashi Iwai 
2840337b9d02STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
2841337b9d02STakashi Iwai 		nid = spec->adc_nids[i];
2842337b9d02STakashi Iwai 		while (nid) {
2843a22d543aSTakashi Iwai 			type = get_wcaps_type(get_wcaps(codec, nid));
28441c55d521STakashi Iwai 			if (type == AC_WID_PIN)
28451c55d521STakashi Iwai 				break;
2846337b9d02STakashi Iwai 			n = snd_hda_get_connections(codec, nid, conn,
2847337b9d02STakashi Iwai 						    ARRAY_SIZE(conn));
2848337b9d02STakashi Iwai 			if (n <= 0)
2849337b9d02STakashi Iwai 				break;
2850337b9d02STakashi Iwai 			if (n > 1) {
2851337b9d02STakashi Iwai 				spec->mux_nids[i] = nid;
2852337b9d02STakashi Iwai 				break;
2853337b9d02STakashi Iwai 			}
2854337b9d02STakashi Iwai 			nid = conn[0];
2855337b9d02STakashi Iwai 		}
2856337b9d02STakashi Iwai 	}
28571c55d521STakashi Iwai 	return 0;
2858337b9d02STakashi Iwai }
2859337b9d02STakashi Iwai 
2860c577b8a1SJoseph Chan static int patch_vt1708(struct hda_codec *codec)
2861c577b8a1SJoseph Chan {
2862c577b8a1SJoseph Chan 	struct via_spec *spec;
2863c577b8a1SJoseph Chan 	int err;
2864c577b8a1SJoseph Chan 
2865c577b8a1SJoseph Chan 	/* create a codec specific record */
28665b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2867c577b8a1SJoseph Chan 	if (spec == NULL)
2868c577b8a1SJoseph Chan 		return -ENOMEM;
2869c577b8a1SJoseph Chan 
2870620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x17;
2871620e2b28STakashi Iwai 
287212daef65STakashi Iwai 	/* Add HP and CD pin config connect bit re-config action */
287312daef65STakashi Iwai 	vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
287412daef65STakashi Iwai 	vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
287512daef65STakashi Iwai 
2876c577b8a1SJoseph Chan 	/* automatic parse from the BIOS config */
287712daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2878c577b8a1SJoseph Chan 	if (err < 0) {
2879c577b8a1SJoseph Chan 		via_free(codec);
2880c577b8a1SJoseph Chan 		return err;
2881c577b8a1SJoseph Chan 	}
2882c577b8a1SJoseph Chan 
288312daef65STakashi Iwai 	/* add jack detect on/off control */
288412daef65STakashi Iwai 	if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
288512daef65STakashi Iwai 		return -ENOMEM;
288612daef65STakashi Iwai 
2887bc9b5623STakashi Iwai 	/* disable 32bit format on VT1708 */
2888bc9b5623STakashi Iwai 	if (codec->vendor_id == 0x11061708)
2889bc9b5623STakashi Iwai 		spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
2890c577b8a1SJoseph Chan 
2891e322a36dSLydia Wang 	spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
2892e322a36dSLydia Wang 
2893c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
2894c577b8a1SJoseph Chan 
28951f2e99feSLydia Wang 	INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
2896c577b8a1SJoseph Chan 	return 0;
2897c577b8a1SJoseph Chan }
2898c577b8a1SJoseph Chan 
2899ddd304d8STakashi Iwai static int patch_vt1709(struct hda_codec *codec)
2900c577b8a1SJoseph Chan {
2901c577b8a1SJoseph Chan 	struct via_spec *spec;
2902c577b8a1SJoseph Chan 	int err;
2903c577b8a1SJoseph Chan 
2904c577b8a1SJoseph Chan 	/* create a codec specific record */
29055b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2906c577b8a1SJoseph Chan 	if (spec == NULL)
2907c577b8a1SJoseph Chan 		return -ENOMEM;
2908c577b8a1SJoseph Chan 
2909620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x18;
2910620e2b28STakashi Iwai 
291112daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2912c577b8a1SJoseph Chan 	if (err < 0) {
2913c577b8a1SJoseph Chan 		via_free(codec);
2914c577b8a1SJoseph Chan 		return err;
2915c577b8a1SJoseph Chan 	}
2916c577b8a1SJoseph Chan 
2917c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
2918c577b8a1SJoseph Chan 
2919f7278fd0SJosepch Chan 	return 0;
2920f7278fd0SJosepch Chan }
2921f7278fd0SJosepch Chan 
29223e95b9abSLydia Wang static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
29233e95b9abSLydia Wang {
29243e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
29253e95b9abSLydia Wang 	int imux_is_smixer;
29263e95b9abSLydia Wang 	unsigned int parm;
29273e95b9abSLydia Wang 	int is_8ch = 0;
2928bc92df7fSLydia Wang 	if ((spec->codec_type != VT1708B_4CH) &&
2929bc92df7fSLydia Wang 	    (codec->vendor_id != 0x11064397))
29303e95b9abSLydia Wang 		is_8ch = 1;
29313e95b9abSLydia Wang 
29323e95b9abSLydia Wang 	/* SW0 (17h) = stereo mixer */
29333e95b9abSLydia Wang 	imux_is_smixer =
29343e95b9abSLydia Wang 	(snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
29353e95b9abSLydia Wang 	 == ((spec->codec_type == VT1708S) ? 5 : 0));
29363e95b9abSLydia Wang 	/* inputs */
29373e95b9abSLydia Wang 	/* PW 1/2/5 (1ah/1bh/1eh) */
29383e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29393e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1a, &parm);
29403e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1b, &parm);
29413e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
29423e95b9abSLydia Wang 	if (imux_is_smixer)
29433e95b9abSLydia Wang 		parm = AC_PWRST_D0;
29443e95b9abSLydia Wang 	/* SW0 (17h), AIW 0/1 (13h/14h) */
2945054d867eSTakashi Iwai 	update_power_state(codec, 0x17, parm);
2946054d867eSTakashi Iwai 	update_power_state(codec, 0x13, parm);
2947054d867eSTakashi Iwai 	update_power_state(codec, 0x14, parm);
29483e95b9abSLydia Wang 
29493e95b9abSLydia Wang 	/* outputs */
29503e95b9abSLydia Wang 	/* PW0 (19h), SW1 (18h), AOW1 (11h) */
29513e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29523e95b9abSLydia Wang 	set_pin_power_state(codec, 0x19, &parm);
29533e95b9abSLydia Wang 	if (spec->smart51_enabled)
29543e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1b, &parm);
2955054d867eSTakashi Iwai 	update_power_state(codec, 0x18, parm);
2956054d867eSTakashi Iwai 	update_power_state(codec, 0x11, parm);
29573e95b9abSLydia Wang 
29583e95b9abSLydia Wang 	/* PW6 (22h), SW2 (26h), AOW2 (24h) */
29593e95b9abSLydia Wang 	if (is_8ch) {
29603e95b9abSLydia Wang 		parm = AC_PWRST_D3;
29613e95b9abSLydia Wang 		set_pin_power_state(codec, 0x22, &parm);
29623e95b9abSLydia Wang 		if (spec->smart51_enabled)
29633e95b9abSLydia Wang 			set_pin_power_state(codec, 0x1a, &parm);
2964054d867eSTakashi Iwai 		update_power_state(codec, 0x26, parm);
2965054d867eSTakashi Iwai 		update_power_state(codec, 0x24, parm);
2966bc92df7fSLydia Wang 	} else if (codec->vendor_id == 0x11064397) {
2967bc92df7fSLydia Wang 		/* PW7(23h), SW2(27h), AOW2(25h) */
2968bc92df7fSLydia Wang 		parm = AC_PWRST_D3;
2969bc92df7fSLydia Wang 		set_pin_power_state(codec, 0x23, &parm);
2970bc92df7fSLydia Wang 		if (spec->smart51_enabled)
2971bc92df7fSLydia Wang 			set_pin_power_state(codec, 0x1a, &parm);
2972054d867eSTakashi Iwai 		update_power_state(codec, 0x27, parm);
2973054d867eSTakashi Iwai 		update_power_state(codec, 0x25, parm);
29743e95b9abSLydia Wang 	}
29753e95b9abSLydia Wang 
29763e95b9abSLydia Wang 	/* PW 3/4/7 (1ch/1dh/23h) */
29773e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29783e95b9abSLydia Wang 	/* force to D0 for internal Speaker */
29793e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1c, &parm);
29803e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1d, &parm);
29813e95b9abSLydia Wang 	if (is_8ch)
29823e95b9abSLydia Wang 		set_pin_power_state(codec, 0x23, &parm);
29833e95b9abSLydia Wang 
29843e95b9abSLydia Wang 	/* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
2985054d867eSTakashi Iwai 	update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
2986054d867eSTakashi Iwai 	update_power_state(codec, 0x10, parm);
29873e95b9abSLydia Wang 	if (is_8ch) {
2988054d867eSTakashi Iwai 		update_power_state(codec, 0x25, parm);
2989054d867eSTakashi Iwai 		update_power_state(codec, 0x27, parm);
2990bc92df7fSLydia Wang 	} else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
2991054d867eSTakashi Iwai 		update_power_state(codec, 0x25, parm);
29923e95b9abSLydia Wang }
29933e95b9abSLydia Wang 
2994518bf3baSLydia Wang static int patch_vt1708S(struct hda_codec *codec);
2995ddd304d8STakashi Iwai static int patch_vt1708B(struct hda_codec *codec)
2996f7278fd0SJosepch Chan {
2997f7278fd0SJosepch Chan 	struct via_spec *spec;
2998f7278fd0SJosepch Chan 	int err;
2999f7278fd0SJosepch Chan 
3000518bf3baSLydia Wang 	if (get_codec_type(codec) == VT1708BCE)
3001518bf3baSLydia Wang 		return patch_vt1708S(codec);
3002ddd304d8STakashi Iwai 
3003f7278fd0SJosepch Chan 	/* create a codec specific record */
30045b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3005f7278fd0SJosepch Chan 	if (spec == NULL)
3006f7278fd0SJosepch Chan 		return -ENOMEM;
3007f7278fd0SJosepch Chan 
3008620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
3009620e2b28STakashi Iwai 
3010f7278fd0SJosepch Chan 	/* automatic parse from the BIOS config */
301112daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3012f7278fd0SJosepch Chan 	if (err < 0) {
3013f7278fd0SJosepch Chan 		via_free(codec);
3014f7278fd0SJosepch Chan 		return err;
3015f7278fd0SJosepch Chan 	}
3016f7278fd0SJosepch Chan 
3017f7278fd0SJosepch Chan 	codec->patch_ops = via_patch_ops;
3018f7278fd0SJosepch Chan 
30193e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
30203e95b9abSLydia Wang 
3021f7278fd0SJosepch Chan 	return 0;
3022f7278fd0SJosepch Chan }
3023f7278fd0SJosepch Chan 
3024d949cac1SHarald Welte /* Patch for VT1708S */
3025096a8854STakashi Iwai static const struct hda_verb vt1708S_init_verbs[] = {
3026d7426329SHarald Welte 	/* Enable Mic Boost Volume backdoor */
3027d7426329SHarald Welte 	{0x1, 0xf98, 0x1},
3028bc7e7e5cSLydia Wang 	/* don't bybass mixer */
3029bc7e7e5cSLydia Wang 	{0x1, 0xf88, 0xc0},
3030d949cac1SHarald Welte 	{ }
3031d949cac1SHarald Welte };
3032d949cac1SHarald Welte 
30339da29271STakashi Iwai /* fill out digital output widgets; one for master and one for slave outputs */
30349da29271STakashi Iwai static void fill_dig_outs(struct hda_codec *codec)
30359da29271STakashi Iwai {
30369da29271STakashi Iwai 	struct via_spec *spec = codec->spec;
30379da29271STakashi Iwai 	int i;
30389da29271STakashi Iwai 
30399da29271STakashi Iwai 	for (i = 0; i < spec->autocfg.dig_outs; i++) {
30409da29271STakashi Iwai 		hda_nid_t nid;
30419da29271STakashi Iwai 		int conn;
30429da29271STakashi Iwai 
30439da29271STakashi Iwai 		nid = spec->autocfg.dig_out_pins[i];
30449da29271STakashi Iwai 		if (!nid)
30459da29271STakashi Iwai 			continue;
30469da29271STakashi Iwai 		conn = snd_hda_get_connections(codec, nid, &nid, 1);
30479da29271STakashi Iwai 		if (conn < 1)
30489da29271STakashi Iwai 			continue;
30499da29271STakashi Iwai 		if (!spec->multiout.dig_out_nid)
30509da29271STakashi Iwai 			spec->multiout.dig_out_nid = nid;
30519da29271STakashi Iwai 		else {
30529da29271STakashi Iwai 			spec->slave_dig_outs[0] = nid;
30539da29271STakashi Iwai 			break; /* at most two dig outs */
30549da29271STakashi Iwai 		}
30559da29271STakashi Iwai 	}
30569da29271STakashi Iwai }
30579da29271STakashi Iwai 
305812daef65STakashi Iwai static void fill_dig_in(struct hda_codec *codec)
3059d949cac1SHarald Welte {
3060d949cac1SHarald Welte 	struct via_spec *spec = codec->spec;
306112daef65STakashi Iwai 	hda_nid_t dig_nid;
306212daef65STakashi Iwai 	int i, err;
3063d949cac1SHarald Welte 
306412daef65STakashi Iwai 	if (!spec->autocfg.dig_in_pin)
306512daef65STakashi Iwai 		return;
3066d949cac1SHarald Welte 
306712daef65STakashi Iwai 	dig_nid = codec->start_nid;
306812daef65STakashi Iwai 	for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
306912daef65STakashi Iwai 		unsigned int wcaps = get_wcaps(codec, dig_nid);
307012daef65STakashi Iwai 		if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
307112daef65STakashi Iwai 			continue;
307212daef65STakashi Iwai 		if (!(wcaps & AC_WCAP_DIGITAL))
307312daef65STakashi Iwai 			continue;
307412daef65STakashi Iwai 		if (!(wcaps & AC_WCAP_CONN_LIST))
307512daef65STakashi Iwai 			continue;
307612daef65STakashi Iwai 		err = get_connection_index(codec, dig_nid,
307712daef65STakashi Iwai 					   spec->autocfg.dig_in_pin);
307812daef65STakashi Iwai 		if (err >= 0) {
307912daef65STakashi Iwai 			spec->dig_in_nid = dig_nid;
308012daef65STakashi Iwai 			break;
308112daef65STakashi Iwai 		}
308212daef65STakashi Iwai 	}
3083d949cac1SHarald Welte }
3084d949cac1SHarald Welte 
30856369bcfcSLydia Wang static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
30866369bcfcSLydia Wang 			       int offset, int num_steps, int step_size)
30876369bcfcSLydia Wang {
30886369bcfcSLydia Wang 	snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
30896369bcfcSLydia Wang 				  (offset << AC_AMPCAP_OFFSET_SHIFT) |
30906369bcfcSLydia Wang 				  (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
30916369bcfcSLydia Wang 				  (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
30926369bcfcSLydia Wang 				  (0 << AC_AMPCAP_MUTE_SHIFT));
30936369bcfcSLydia Wang }
30946369bcfcSLydia Wang 
3095d949cac1SHarald Welte static int patch_vt1708S(struct hda_codec *codec)
3096d949cac1SHarald Welte {
3097d949cac1SHarald Welte 	struct via_spec *spec;
3098d949cac1SHarald Welte 	int err;
3099d949cac1SHarald Welte 
3100d949cac1SHarald Welte 	/* create a codec specific record */
31015b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3102d949cac1SHarald Welte 	if (spec == NULL)
3103d949cac1SHarald Welte 		return -ENOMEM;
3104d949cac1SHarald Welte 
3105620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
3106d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1a, 0, 3, 40);
3107d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1e, 0, 3, 40);
3108620e2b28STakashi Iwai 
3109d949cac1SHarald Welte 	/* automatic parse from the BIOS config */
311012daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3111d949cac1SHarald Welte 	if (err < 0) {
3112d949cac1SHarald Welte 		via_free(codec);
3113d949cac1SHarald Welte 		return err;
3114d949cac1SHarald Welte 	}
3115d949cac1SHarald Welte 
3116096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
3117d949cac1SHarald Welte 
3118d949cac1SHarald Welte 	codec->patch_ops = via_patch_ops;
3119d949cac1SHarald Welte 
3120518bf3baSLydia Wang 	/* correct names for VT1708BCE */
3121518bf3baSLydia Wang 	if (get_codec_type(codec) == VT1708BCE)	{
3122518bf3baSLydia Wang 		kfree(codec->chip_name);
3123518bf3baSLydia Wang 		codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
3124518bf3baSLydia Wang 		snprintf(codec->bus->card->mixername,
3125518bf3baSLydia Wang 			 sizeof(codec->bus->card->mixername),
3126518bf3baSLydia Wang 			 "%s %s", codec->vendor_name, codec->chip_name);
3127970f630fSLydia Wang 	}
3128bc92df7fSLydia Wang 	/* correct names for VT1705 */
3129bc92df7fSLydia Wang 	if (codec->vendor_id == 0x11064397)	{
3130bc92df7fSLydia Wang 		kfree(codec->chip_name);
3131bc92df7fSLydia Wang 		codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
3132bc92df7fSLydia Wang 		snprintf(codec->bus->card->mixername,
3133bc92df7fSLydia Wang 			 sizeof(codec->bus->card->mixername),
3134bc92df7fSLydia Wang 			 "%s %s", codec->vendor_name, codec->chip_name);
3135bc92df7fSLydia Wang 	}
31363e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
3137d949cac1SHarald Welte 	return 0;
3138d949cac1SHarald Welte }
3139d949cac1SHarald Welte 
3140d949cac1SHarald Welte /* Patch for VT1702 */
3141d949cac1SHarald Welte 
3142096a8854STakashi Iwai static const struct hda_verb vt1702_init_verbs[] = {
3143bc7e7e5cSLydia Wang 	/* mixer enable */
3144bc7e7e5cSLydia Wang 	{0x1, 0xF88, 0x3},
3145bc7e7e5cSLydia Wang 	/* GPIO 0~2 */
3146bc7e7e5cSLydia Wang 	{0x1, 0xF82, 0x3F},
3147d949cac1SHarald Welte 	{ }
3148d949cac1SHarald Welte };
3149d949cac1SHarald Welte 
31503e95b9abSLydia Wang static void set_widgets_power_state_vt1702(struct hda_codec *codec)
31513e95b9abSLydia Wang {
31523e95b9abSLydia Wang 	int imux_is_smixer =
31533e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
31543e95b9abSLydia Wang 	unsigned int parm;
31553e95b9abSLydia Wang 	/* inputs */
31563e95b9abSLydia Wang 	/* PW 1/2/5 (14h/15h/18h) */
31573e95b9abSLydia Wang 	parm = AC_PWRST_D3;
31583e95b9abSLydia Wang 	set_pin_power_state(codec, 0x14, &parm);
31593e95b9abSLydia Wang 	set_pin_power_state(codec, 0x15, &parm);
31603e95b9abSLydia Wang 	set_pin_power_state(codec, 0x18, &parm);
31613e95b9abSLydia Wang 	if (imux_is_smixer)
31623e95b9abSLydia Wang 		parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
31633e95b9abSLydia Wang 	/* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
3164054d867eSTakashi Iwai 	update_power_state(codec, 0x13, parm);
3165054d867eSTakashi Iwai 	update_power_state(codec, 0x12, parm);
3166054d867eSTakashi Iwai 	update_power_state(codec, 0x1f, parm);
3167054d867eSTakashi Iwai 	update_power_state(codec, 0x20, parm);
31683e95b9abSLydia Wang 
31693e95b9abSLydia Wang 	/* outputs */
31703e95b9abSLydia Wang 	/* PW 3/4 (16h/17h) */
31713e95b9abSLydia Wang 	parm = AC_PWRST_D3;
31723e95b9abSLydia Wang 	set_pin_power_state(codec, 0x17, &parm);
31733e95b9abSLydia Wang 	set_pin_power_state(codec, 0x16, &parm);
31743e95b9abSLydia Wang 	/* MW0 (1ah), AOW 0/1 (10h/1dh) */
3175054d867eSTakashi Iwai 	update_power_state(codec, 0x1a, imux_is_smixer ? AC_PWRST_D0 : parm);
3176054d867eSTakashi Iwai 	update_power_state(codec, 0x10, parm);
3177054d867eSTakashi Iwai 	update_power_state(codec, 0x1d, parm);
31783e95b9abSLydia Wang }
31793e95b9abSLydia Wang 
3180d949cac1SHarald Welte static int patch_vt1702(struct hda_codec *codec)
3181d949cac1SHarald Welte {
3182d949cac1SHarald Welte 	struct via_spec *spec;
3183d949cac1SHarald Welte 	int err;
3184d949cac1SHarald Welte 
3185d949cac1SHarald Welte 	/* create a codec specific record */
31865b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3187d949cac1SHarald Welte 	if (spec == NULL)
3188d949cac1SHarald Welte 		return -ENOMEM;
3189d949cac1SHarald Welte 
3190620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x1a;
3191620e2b28STakashi Iwai 
319212daef65STakashi Iwai 	/* limit AA path volume to 0 dB */
319312daef65STakashi Iwai 	snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
319412daef65STakashi Iwai 				  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
319512daef65STakashi Iwai 				  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
319612daef65STakashi Iwai 				  (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
319712daef65STakashi Iwai 				  (1 << AC_AMPCAP_MUTE_SHIFT));
319812daef65STakashi Iwai 
3199d949cac1SHarald Welte 	/* automatic parse from the BIOS config */
320012daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3201d949cac1SHarald Welte 	if (err < 0) {
3202d949cac1SHarald Welte 		via_free(codec);
3203d949cac1SHarald Welte 		return err;
3204d949cac1SHarald Welte 	}
3205d949cac1SHarald Welte 
3206096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
3207d949cac1SHarald Welte 
3208d949cac1SHarald Welte 	codec->patch_ops = via_patch_ops;
3209d949cac1SHarald Welte 
32103e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1702;
3211d949cac1SHarald Welte 	return 0;
3212d949cac1SHarald Welte }
3213d949cac1SHarald Welte 
3214eb7188caSLydia Wang /* Patch for VT1718S */
3215eb7188caSLydia Wang 
3216096a8854STakashi Iwai static const struct hda_verb vt1718S_init_verbs[] = {
32174ab2d53aSLydia Wang 	/* Enable MW0 adjust Gain 5 */
32184ab2d53aSLydia Wang 	{0x1, 0xfb2, 0x10},
3219eb7188caSLydia Wang 	/* Enable Boost Volume backdoor */
3220eb7188caSLydia Wang 	{0x1, 0xf88, 0x8},
32215d41762aSTakashi Iwai 
3222eb7188caSLydia Wang 	{ }
3223eb7188caSLydia Wang };
3224eb7188caSLydia Wang 
32253e95b9abSLydia Wang static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
32263e95b9abSLydia Wang {
32273e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
32283e95b9abSLydia Wang 	int imux_is_smixer;
3229*6162552bSTakashi Iwai 	unsigned int parm, parm2;
32303e95b9abSLydia Wang 	/* MUX6 (1eh) = stereo mixer */
32313e95b9abSLydia Wang 	imux_is_smixer =
32323e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
32333e95b9abSLydia Wang 	/* inputs */
32343e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
32353e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32363e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
32373e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
32383e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
32393e95b9abSLydia Wang 	if (imux_is_smixer)
32403e95b9abSLydia Wang 		parm = AC_PWRST_D0;
32413e95b9abSLydia Wang 	/* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
3242054d867eSTakashi Iwai 	update_power_state(codec, 0x1e, parm);
3243054d867eSTakashi Iwai 	update_power_state(codec, 0x1f, parm);
3244054d867eSTakashi Iwai 	update_power_state(codec, 0x10, parm);
3245054d867eSTakashi Iwai 	update_power_state(codec, 0x11, parm);
32463e95b9abSLydia Wang 
32473e95b9abSLydia Wang 	/* outputs */
32483e95b9abSLydia Wang 	/* PW3 (27h), MW2 (1ah), AOW3 (bh) */
32493e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32503e95b9abSLydia Wang 	set_pin_power_state(codec, 0x27, &parm);
3251054d867eSTakashi Iwai 	update_power_state(codec, 0x1a, parm);
3252*6162552bSTakashi Iwai 	parm2 = parm; /* for pin 0x0b */
32533e95b9abSLydia Wang 
32543e95b9abSLydia Wang 	/* PW2 (26h), AOW2 (ah) */
32553e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32563e95b9abSLydia Wang 	set_pin_power_state(codec, 0x26, &parm);
32573e95b9abSLydia Wang 	if (spec->smart51_enabled)
32583e95b9abSLydia Wang 		set_pin_power_state(codec, 0x2b, &parm);
3259054d867eSTakashi Iwai 	update_power_state(codec, 0xa, parm);
32603e95b9abSLydia Wang 
32613e95b9abSLydia Wang 	/* PW0 (24h), AOW0 (8h) */
32623e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32633e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
32643e95b9abSLydia Wang 	if (!spec->hp_independent_mode) /* check for redirected HP */
32653e95b9abSLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
3266054d867eSTakashi Iwai 	update_power_state(codec, 0x8, parm);
3267*6162552bSTakashi Iwai 	if (!spec->hp_independent_mode && parm2 != AC_PWRST_D3)
3268*6162552bSTakashi Iwai 		parm = parm2;
3269*6162552bSTakashi Iwai 	update_power_state(codec, 0xb, parm);
32703e95b9abSLydia Wang 	/* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
3271054d867eSTakashi Iwai 	update_power_state(codec, 0x21, imux_is_smixer ? AC_PWRST_D0 : parm);
32723e95b9abSLydia Wang 
32733e95b9abSLydia Wang 	/* PW1 (25h), AOW1 (9h) */
32743e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32753e95b9abSLydia Wang 	set_pin_power_state(codec, 0x25, &parm);
32763e95b9abSLydia Wang 	if (spec->smart51_enabled)
32773e95b9abSLydia Wang 		set_pin_power_state(codec, 0x2a, &parm);
3278054d867eSTakashi Iwai 	update_power_state(codec, 0x9, parm);
32793e95b9abSLydia Wang 
32803e95b9abSLydia Wang 	if (spec->hp_independent_mode) {
32813e95b9abSLydia Wang 		/* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
32823e95b9abSLydia Wang 		parm = AC_PWRST_D3;
32833e95b9abSLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
3284054d867eSTakashi Iwai 		update_power_state(codec, 0x1b, parm);
3285054d867eSTakashi Iwai 		update_power_state(codec, 0x34, parm);
3286054d867eSTakashi Iwai 		update_power_state(codec, 0xc, parm);
32873e95b9abSLydia Wang 	}
32883e95b9abSLydia Wang }
32893e95b9abSLydia Wang 
329030b45033STakashi Iwai /* Add a connection to the primary DAC from AA-mixer for some codecs
329130b45033STakashi Iwai  * This isn't listed from the raw info, but the chip has a secret connection.
329230b45033STakashi Iwai  */
329330b45033STakashi Iwai static int add_secret_dac_path(struct hda_codec *codec)
329430b45033STakashi Iwai {
329530b45033STakashi Iwai 	struct via_spec *spec = codec->spec;
329630b45033STakashi Iwai 	int i, nums;
329730b45033STakashi Iwai 	hda_nid_t conn[8];
329830b45033STakashi Iwai 	hda_nid_t nid;
329930b45033STakashi Iwai 
330030b45033STakashi Iwai 	if (!spec->aa_mix_nid)
330130b45033STakashi Iwai 		return 0;
330230b45033STakashi Iwai 	nums = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
330330b45033STakashi Iwai 				       ARRAY_SIZE(conn) - 1);
330430b45033STakashi Iwai 	for (i = 0; i < nums; i++) {
330530b45033STakashi Iwai 		if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT)
330630b45033STakashi Iwai 			return 0;
330730b45033STakashi Iwai 	}
330830b45033STakashi Iwai 
330930b45033STakashi Iwai 	/* find the primary DAC and add to the connection list */
331030b45033STakashi Iwai 	nid = codec->start_nid;
331130b45033STakashi Iwai 	for (i = 0; i < codec->num_nodes; i++, nid++) {
331230b45033STakashi Iwai 		unsigned int caps = get_wcaps(codec, nid);
331330b45033STakashi Iwai 		if (get_wcaps_type(caps) == AC_WID_AUD_OUT &&
331430b45033STakashi Iwai 		    !(caps & AC_WCAP_DIGITAL)) {
331530b45033STakashi Iwai 			conn[nums++] = nid;
331630b45033STakashi Iwai 			return snd_hda_override_conn_list(codec,
331730b45033STakashi Iwai 							  spec->aa_mix_nid,
331830b45033STakashi Iwai 							  nums, conn);
331930b45033STakashi Iwai 		}
332030b45033STakashi Iwai 	}
332130b45033STakashi Iwai 	return 0;
332230b45033STakashi Iwai }
332330b45033STakashi Iwai 
332430b45033STakashi Iwai 
3325eb7188caSLydia Wang static int patch_vt1718S(struct hda_codec *codec)
3326eb7188caSLydia Wang {
3327eb7188caSLydia Wang 	struct via_spec *spec;
3328eb7188caSLydia Wang 	int err;
3329eb7188caSLydia Wang 
3330eb7188caSLydia Wang 	/* create a codec specific record */
33315b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3332eb7188caSLydia Wang 	if (spec == NULL)
3333eb7188caSLydia Wang 		return -ENOMEM;
3334eb7188caSLydia Wang 
3335620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
3336d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3337d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
333830b45033STakashi Iwai 	add_secret_dac_path(codec);
3339620e2b28STakashi Iwai 
3340eb7188caSLydia Wang 	/* automatic parse from the BIOS config */
334112daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3342eb7188caSLydia Wang 	if (err < 0) {
3343eb7188caSLydia Wang 		via_free(codec);
3344eb7188caSLydia Wang 		return err;
3345eb7188caSLydia Wang 	}
3346eb7188caSLydia Wang 
3347096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
3348eb7188caSLydia Wang 
3349eb7188caSLydia Wang 	codec->patch_ops = via_patch_ops;
3350eb7188caSLydia Wang 
33513e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1718S;
33523e95b9abSLydia Wang 
3353eb7188caSLydia Wang 	return 0;
3354eb7188caSLydia Wang }
3355f3db423dSLydia Wang 
3356f3db423dSLydia Wang /* Patch for VT1716S */
3357f3db423dSLydia Wang 
3358f3db423dSLydia Wang static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
3359f3db423dSLydia Wang 			    struct snd_ctl_elem_info *uinfo)
3360f3db423dSLydia Wang {
3361f3db423dSLydia Wang 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3362f3db423dSLydia Wang 	uinfo->count = 1;
3363f3db423dSLydia Wang 	uinfo->value.integer.min = 0;
3364f3db423dSLydia Wang 	uinfo->value.integer.max = 1;
3365f3db423dSLydia Wang 	return 0;
3366f3db423dSLydia Wang }
3367f3db423dSLydia Wang 
3368f3db423dSLydia Wang static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
3369f3db423dSLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
3370f3db423dSLydia Wang {
3371f3db423dSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3372f3db423dSLydia Wang 	int index = 0;
3373f3db423dSLydia Wang 
3374f3db423dSLydia Wang 	index = snd_hda_codec_read(codec, 0x26, 0,
3375f3db423dSLydia Wang 					       AC_VERB_GET_CONNECT_SEL, 0);
3376f3db423dSLydia Wang 	if (index != -1)
3377f3db423dSLydia Wang 		*ucontrol->value.integer.value = index;
3378f3db423dSLydia Wang 
3379f3db423dSLydia Wang 	return 0;
3380f3db423dSLydia Wang }
3381f3db423dSLydia Wang 
3382f3db423dSLydia Wang static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
3383f3db423dSLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
3384f3db423dSLydia Wang {
3385f3db423dSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3386f3db423dSLydia Wang 	struct via_spec *spec = codec->spec;
3387f3db423dSLydia Wang 	int index = *ucontrol->value.integer.value;
3388f3db423dSLydia Wang 
3389f3db423dSLydia Wang 	snd_hda_codec_write(codec, 0x26, 0,
3390f3db423dSLydia Wang 					       AC_VERB_SET_CONNECT_SEL, index);
3391f3db423dSLydia Wang 	spec->dmic_enabled = index;
33923e95b9abSLydia Wang 	set_widgets_power_state(codec);
3393f3db423dSLydia Wang 	return 1;
3394f3db423dSLydia Wang }
3395f3db423dSLydia Wang 
339690dd48a1STakashi Iwai static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
3397f3db423dSLydia Wang 	HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
3398f3db423dSLydia Wang 	{
3399f3db423dSLydia Wang 	 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3400f3db423dSLydia Wang 	 .name = "Digital Mic Capture Switch",
34015b0cb1d8SJaroslav Kysela 	 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
3402f3db423dSLydia Wang 	 .count = 1,
3403f3db423dSLydia Wang 	 .info = vt1716s_dmic_info,
3404f3db423dSLydia Wang 	 .get = vt1716s_dmic_get,
3405f3db423dSLydia Wang 	 .put = vt1716s_dmic_put,
3406f3db423dSLydia Wang 	 },
3407f3db423dSLydia Wang 	{}			/* end */
3408f3db423dSLydia Wang };
3409f3db423dSLydia Wang 
3410f3db423dSLydia Wang 
3411f3db423dSLydia Wang /* mono-out mixer elements */
341290dd48a1STakashi Iwai static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
3413f3db423dSLydia Wang 	HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
3414f3db423dSLydia Wang 	{ } /* end */
3415f3db423dSLydia Wang };
3416f3db423dSLydia Wang 
3417096a8854STakashi Iwai static const struct hda_verb vt1716S_init_verbs[] = {
3418f3db423dSLydia Wang 	/* Enable Boost Volume backdoor */
3419f3db423dSLydia Wang 	{0x1, 0xf8a, 0x80},
3420f3db423dSLydia Wang 	/* don't bybass mixer */
3421f3db423dSLydia Wang 	{0x1, 0xf88, 0xc0},
3422f3db423dSLydia Wang 	/* Enable mono output */
3423f3db423dSLydia Wang 	{0x1, 0xf90, 0x08},
3424f3db423dSLydia Wang 	{ }
3425f3db423dSLydia Wang };
3426f3db423dSLydia Wang 
34273e95b9abSLydia Wang static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
34283e95b9abSLydia Wang {
34293e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
34303e95b9abSLydia Wang 	int imux_is_smixer;
34313e95b9abSLydia Wang 	unsigned int parm;
34323e95b9abSLydia Wang 	unsigned int mono_out, present;
34333e95b9abSLydia Wang 	/* SW0 (17h) = stereo mixer */
34343e95b9abSLydia Wang 	imux_is_smixer =
34353e95b9abSLydia Wang 	(snd_hda_codec_read(codec, 0x17, 0,
34363e95b9abSLydia Wang 			    AC_VERB_GET_CONNECT_SEL, 0x00) ==  5);
34373e95b9abSLydia Wang 	/* inputs */
34383e95b9abSLydia Wang 	/* PW 1/2/5 (1ah/1bh/1eh) */
34393e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34403e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1a, &parm);
34413e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1b, &parm);
34423e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
34433e95b9abSLydia Wang 	if (imux_is_smixer)
34443e95b9abSLydia Wang 		parm = AC_PWRST_D0;
34453e95b9abSLydia Wang 	/* SW0 (17h), AIW0(13h) */
3446054d867eSTakashi Iwai 	update_power_state(codec, 0x17, parm);
3447054d867eSTakashi Iwai 	update_power_state(codec, 0x13, parm);
34483e95b9abSLydia Wang 
34493e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34503e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
34513e95b9abSLydia Wang 	/* PW11 (22h) */
34523e95b9abSLydia Wang 	if (spec->dmic_enabled)
34533e95b9abSLydia Wang 		set_pin_power_state(codec, 0x22, &parm);
34543e95b9abSLydia Wang 	else
3455054d867eSTakashi Iwai 		update_power_state(codec, 0x22, AC_PWRST_D3);
34563e95b9abSLydia Wang 
34573e95b9abSLydia Wang 	/* SW2(26h), AIW1(14h) */
3458054d867eSTakashi Iwai 	update_power_state(codec, 0x26, parm);
3459054d867eSTakashi Iwai 	update_power_state(codec, 0x14, parm);
34603e95b9abSLydia Wang 
34613e95b9abSLydia Wang 	/* outputs */
34623e95b9abSLydia Wang 	/* PW0 (19h), SW1 (18h), AOW1 (11h) */
34633e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34643e95b9abSLydia Wang 	set_pin_power_state(codec, 0x19, &parm);
34653e95b9abSLydia Wang 	/* Smart 5.1 PW2(1bh) */
34663e95b9abSLydia Wang 	if (spec->smart51_enabled)
34673e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1b, &parm);
3468054d867eSTakashi Iwai 	update_power_state(codec, 0x18, parm);
3469054d867eSTakashi Iwai 	update_power_state(codec, 0x11, parm);
34703e95b9abSLydia Wang 
34713e95b9abSLydia Wang 	/* PW7 (23h), SW3 (27h), AOW3 (25h) */
34723e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34733e95b9abSLydia Wang 	set_pin_power_state(codec, 0x23, &parm);
34743e95b9abSLydia Wang 	/* Smart 5.1 PW1(1ah) */
34753e95b9abSLydia Wang 	if (spec->smart51_enabled)
34763e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1a, &parm);
3477054d867eSTakashi Iwai 	update_power_state(codec, 0x27, parm);
34783e95b9abSLydia Wang 
34793e95b9abSLydia Wang 	/* Smart 5.1 PW5(1eh) */
34803e95b9abSLydia Wang 	if (spec->smart51_enabled)
34813e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1e, &parm);
3482054d867eSTakashi Iwai 	update_power_state(codec, 0x25, parm);
34833e95b9abSLydia Wang 
34843e95b9abSLydia Wang 	/* Mono out */
34853e95b9abSLydia Wang 	/* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
34863e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x1c);
34873e95b9abSLydia Wang 
34883e95b9abSLydia Wang 	if (present)
34893e95b9abSLydia Wang 		mono_out = 0;
34903e95b9abSLydia Wang 	else {
34913e95b9abSLydia Wang 		present = snd_hda_jack_detect(codec, 0x1d);
34923e95b9abSLydia Wang 		if (!spec->hp_independent_mode && present)
34933e95b9abSLydia Wang 			mono_out = 0;
34943e95b9abSLydia Wang 		else
34953e95b9abSLydia Wang 			mono_out = 1;
34963e95b9abSLydia Wang 	}
34973e95b9abSLydia Wang 	parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
3498054d867eSTakashi Iwai 	update_power_state(codec, 0x28, parm);
3499054d867eSTakashi Iwai 	update_power_state(codec, 0x29, parm);
3500054d867eSTakashi Iwai 	update_power_state(codec, 0x2a, parm);
35013e95b9abSLydia Wang 
35023e95b9abSLydia Wang 	/* PW 3/4 (1ch/1dh) */
35033e95b9abSLydia Wang 	parm = AC_PWRST_D3;
35043e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1c, &parm);
35053e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1d, &parm);
35063e95b9abSLydia Wang 	/* HP Independent Mode, power on AOW3 */
35073e95b9abSLydia Wang 	if (spec->hp_independent_mode)
3508054d867eSTakashi Iwai 		update_power_state(codec, 0x25, parm);
35093e95b9abSLydia Wang 
35103e95b9abSLydia Wang 	/* force to D0 for internal Speaker */
35113e95b9abSLydia Wang 	/* MW0 (16h), AOW0 (10h) */
3512054d867eSTakashi Iwai 	update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
3513054d867eSTakashi Iwai 	update_power_state(codec, 0x10, mono_out ? AC_PWRST_D0 : parm);
35143e95b9abSLydia Wang }
35153e95b9abSLydia Wang 
3516f3db423dSLydia Wang static int patch_vt1716S(struct hda_codec *codec)
3517f3db423dSLydia Wang {
3518f3db423dSLydia Wang 	struct via_spec *spec;
3519f3db423dSLydia Wang 	int err;
3520f3db423dSLydia Wang 
3521f3db423dSLydia Wang 	/* create a codec specific record */
35225b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3523f3db423dSLydia Wang 	if (spec == NULL)
3524f3db423dSLydia Wang 		return -ENOMEM;
3525f3db423dSLydia Wang 
3526620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
3527d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1a, 0, 3, 40);
3528d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1e, 0, 3, 40);
3529620e2b28STakashi Iwai 
3530f3db423dSLydia Wang 	/* automatic parse from the BIOS config */
353112daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3532f3db423dSLydia Wang 	if (err < 0) {
3533f3db423dSLydia Wang 		via_free(codec);
3534f3db423dSLydia Wang 		return err;
3535f3db423dSLydia Wang 	}
3536f3db423dSLydia Wang 
3537096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++]  = vt1716S_init_verbs;
3538f3db423dSLydia Wang 
3539f3db423dSLydia Wang 	spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
3540f3db423dSLydia Wang 	spec->num_mixers++;
3541f3db423dSLydia Wang 
3542f3db423dSLydia Wang 	spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
3543f3db423dSLydia Wang 
3544f3db423dSLydia Wang 	codec->patch_ops = via_patch_ops;
3545f3db423dSLydia Wang 
35463e95b9abSLydia Wang 	spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
3547f3db423dSLydia Wang 	return 0;
3548f3db423dSLydia Wang }
354925eaba2fSLydia Wang 
355025eaba2fSLydia Wang /* for vt2002P */
355125eaba2fSLydia Wang 
3552096a8854STakashi Iwai static const struct hda_verb vt2002P_init_verbs[] = {
3553eadb9a80SLydia Wang 	/* Class-D speaker related verbs */
3554eadb9a80SLydia Wang 	{0x1, 0xfe0, 0x4},
3555eadb9a80SLydia Wang 	{0x1, 0xfe9, 0x80},
3556eadb9a80SLydia Wang 	{0x1, 0xfe2, 0x22},
355725eaba2fSLydia Wang 	/* Enable Boost Volume backdoor */
355825eaba2fSLydia Wang 	{0x1, 0xfb9, 0x24},
355925eaba2fSLydia Wang 	/* Enable AOW0 to MW9 */
356025eaba2fSLydia Wang 	{0x1, 0xfb8, 0x88},
356125eaba2fSLydia Wang 	{ }
356225eaba2fSLydia Wang };
35634a918ffeSTakashi Iwai 
3564096a8854STakashi Iwai static const struct hda_verb vt1802_init_verbs[] = {
356511890956SLydia Wang 	/* Enable Boost Volume backdoor */
356611890956SLydia Wang 	{0x1, 0xfb9, 0x24},
356711890956SLydia Wang 	/* Enable AOW0 to MW9 */
356811890956SLydia Wang 	{0x1, 0xfb8, 0x88},
356911890956SLydia Wang 	{ }
357011890956SLydia Wang };
357125eaba2fSLydia Wang 
35723e95b9abSLydia Wang static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
35733e95b9abSLydia Wang {
35743e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
35753e95b9abSLydia Wang 	int imux_is_smixer;
35763e95b9abSLydia Wang 	unsigned int parm;
35773e95b9abSLydia Wang 	unsigned int present;
35783e95b9abSLydia Wang 	/* MUX9 (1eh) = stereo mixer */
35793e95b9abSLydia Wang 	imux_is_smixer =
35803e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
35813e95b9abSLydia Wang 	/* inputs */
35823e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
35833e95b9abSLydia Wang 	parm = AC_PWRST_D3;
35843e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
35853e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
35863e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
35873e95b9abSLydia Wang 	parm = AC_PWRST_D0;
35883e95b9abSLydia Wang 	/* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
3589054d867eSTakashi Iwai 	update_power_state(codec, 0x1e, parm);
3590054d867eSTakashi Iwai 	update_power_state(codec, 0x1f, parm);
3591054d867eSTakashi Iwai 	update_power_state(codec, 0x10, parm);
3592054d867eSTakashi Iwai 	update_power_state(codec, 0x11, parm);
35933e95b9abSLydia Wang 
35943e95b9abSLydia Wang 	/* outputs */
35953e95b9abSLydia Wang 	/* AOW0 (8h)*/
3596054d867eSTakashi Iwai 	update_power_state(codec, 0x8, parm);
35973e95b9abSLydia Wang 
359811890956SLydia Wang 	if (spec->codec_type == VT1802) {
359911890956SLydia Wang 		/* PW4 (28h), MW4 (18h), MUX4(38h) */
360011890956SLydia Wang 		parm = AC_PWRST_D3;
360111890956SLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
3602054d867eSTakashi Iwai 		update_power_state(codec, 0x18, parm);
3603054d867eSTakashi Iwai 		update_power_state(codec, 0x38, parm);
360411890956SLydia Wang 	} else {
36053e95b9abSLydia Wang 		/* PW4 (26h), MW4 (1ch), MUX4(37h) */
36063e95b9abSLydia Wang 		parm = AC_PWRST_D3;
36073e95b9abSLydia Wang 		set_pin_power_state(codec, 0x26, &parm);
3608054d867eSTakashi Iwai 		update_power_state(codec, 0x1c, parm);
3609054d867eSTakashi Iwai 		update_power_state(codec, 0x37, parm);
361011890956SLydia Wang 	}
36113e95b9abSLydia Wang 
361211890956SLydia Wang 	if (spec->codec_type == VT1802) {
361311890956SLydia Wang 		/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
361411890956SLydia Wang 		parm = AC_PWRST_D3;
361511890956SLydia Wang 		set_pin_power_state(codec, 0x25, &parm);
3616054d867eSTakashi Iwai 		update_power_state(codec, 0x15, parm);
3617054d867eSTakashi Iwai 		update_power_state(codec, 0x35, parm);
361811890956SLydia Wang 	} else {
36193e95b9abSLydia Wang 		/* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
36203e95b9abSLydia Wang 		parm = AC_PWRST_D3;
36213e95b9abSLydia Wang 		set_pin_power_state(codec, 0x25, &parm);
3622054d867eSTakashi Iwai 		update_power_state(codec, 0x19, parm);
3623054d867eSTakashi Iwai 		update_power_state(codec, 0x35, parm);
362411890956SLydia Wang 	}
36253e95b9abSLydia Wang 
36263e95b9abSLydia Wang 	if (spec->hp_independent_mode)
3627054d867eSTakashi Iwai 		update_power_state(codec, 0x9, AC_PWRST_D0);
36283e95b9abSLydia Wang 
36293e95b9abSLydia Wang 	/* Class-D */
36303e95b9abSLydia Wang 	/* PW0 (24h), MW0(18h/14h), MUX0(34h) */
36313e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x25);
36323e95b9abSLydia Wang 
36333e95b9abSLydia Wang 	parm = AC_PWRST_D3;
36343e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
36353e95b9abSLydia Wang 	parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
363611890956SLydia Wang 	if (spec->codec_type == VT1802)
3637054d867eSTakashi Iwai 		update_power_state(codec, 0x14, parm);
363811890956SLydia Wang 	else
3639054d867eSTakashi Iwai 		update_power_state(codec, 0x18, parm);
3640054d867eSTakashi Iwai 	update_power_state(codec, 0x34, parm);
36413e95b9abSLydia Wang 
36423e95b9abSLydia Wang 	/* Mono Out */
36433e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x26);
36443e95b9abSLydia Wang 
36453e95b9abSLydia Wang 	parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
364611890956SLydia Wang 	if (spec->codec_type == VT1802) {
364711890956SLydia Wang 		/* PW15 (33h), MW8(1ch), MUX8(3ch) */
3648054d867eSTakashi Iwai 		update_power_state(codec, 0x33, parm);
3649054d867eSTakashi Iwai 		update_power_state(codec, 0x1c, parm);
3650054d867eSTakashi Iwai 		update_power_state(codec, 0x3c, parm);
365111890956SLydia Wang 	} else {
36523e95b9abSLydia Wang 		/* PW15 (31h), MW8(17h), MUX8(3bh) */
3653054d867eSTakashi Iwai 		update_power_state(codec, 0x31, parm);
3654054d867eSTakashi Iwai 		update_power_state(codec, 0x17, parm);
3655054d867eSTakashi Iwai 		update_power_state(codec, 0x3b, parm);
365611890956SLydia Wang 	}
36573e95b9abSLydia Wang 	/* MW9 (21h) */
36583e95b9abSLydia Wang 	if (imux_is_smixer || !is_aa_path_mute(codec))
3659054d867eSTakashi Iwai 		update_power_state(codec, 0x21, AC_PWRST_D0);
36603e95b9abSLydia Wang 	else
3661054d867eSTakashi Iwai 		update_power_state(codec, 0x21, AC_PWRST_D3);
36623e95b9abSLydia Wang }
366325eaba2fSLydia Wang 
366425eaba2fSLydia Wang /* patch for vt2002P */
366525eaba2fSLydia Wang static int patch_vt2002P(struct hda_codec *codec)
366625eaba2fSLydia Wang {
366725eaba2fSLydia Wang 	struct via_spec *spec;
366825eaba2fSLydia Wang 	int err;
366925eaba2fSLydia Wang 
367025eaba2fSLydia Wang 	/* create a codec specific record */
36715b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
367225eaba2fSLydia Wang 	if (spec == NULL)
367325eaba2fSLydia Wang 		return -ENOMEM;
367425eaba2fSLydia Wang 
3675620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
3676d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3677d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
367830b45033STakashi Iwai 	add_secret_dac_path(codec);
3679620e2b28STakashi Iwai 
368025eaba2fSLydia Wang 	/* automatic parse from the BIOS config */
368112daef65STakashi Iwai 	err = via_parse_auto_config(codec);
368225eaba2fSLydia Wang 	if (err < 0) {
368325eaba2fSLydia Wang 		via_free(codec);
368425eaba2fSLydia Wang 		return err;
368525eaba2fSLydia Wang 	}
368625eaba2fSLydia Wang 
368711890956SLydia Wang 	if (spec->codec_type == VT1802)
36884a918ffeSTakashi Iwai 		spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
368911890956SLydia Wang 	else
36904a918ffeSTakashi Iwai 		spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
369111890956SLydia Wang 
369225eaba2fSLydia Wang 	codec->patch_ops = via_patch_ops;
369325eaba2fSLydia Wang 
36943e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt2002P;
369525eaba2fSLydia Wang 	return 0;
369625eaba2fSLydia Wang }
3697ab6734e7SLydia Wang 
3698ab6734e7SLydia Wang /* for vt1812 */
3699ab6734e7SLydia Wang 
3700096a8854STakashi Iwai static const struct hda_verb vt1812_init_verbs[] = {
3701ab6734e7SLydia Wang 	/* Enable Boost Volume backdoor */
3702ab6734e7SLydia Wang 	{0x1, 0xfb9, 0x24},
3703ab6734e7SLydia Wang 	/* Enable AOW0 to MW9 */
3704ab6734e7SLydia Wang 	{0x1, 0xfb8, 0xa8},
3705ab6734e7SLydia Wang 	{ }
3706ab6734e7SLydia Wang };
3707ab6734e7SLydia Wang 
37083e95b9abSLydia Wang static void set_widgets_power_state_vt1812(struct hda_codec *codec)
37093e95b9abSLydia Wang {
37103e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
37113e95b9abSLydia Wang 	unsigned int parm;
37123e95b9abSLydia Wang 	unsigned int present;
37133e95b9abSLydia Wang 	/* inputs */
37143e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
37153e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37163e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
37173e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
37183e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
37193e95b9abSLydia Wang 	parm = AC_PWRST_D0;
37203e95b9abSLydia Wang 	/* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
3721054d867eSTakashi Iwai 	update_power_state(codec, 0x1e, parm);
3722054d867eSTakashi Iwai 	update_power_state(codec, 0x1f, parm);
3723054d867eSTakashi Iwai 	update_power_state(codec, 0x10, parm);
3724054d867eSTakashi Iwai 	update_power_state(codec, 0x11, parm);
37253e95b9abSLydia Wang 
37263e95b9abSLydia Wang 	/* outputs */
37273e95b9abSLydia Wang 	/* AOW0 (8h)*/
3728054d867eSTakashi Iwai 	update_power_state(codec, 0x8, AC_PWRST_D0);
37293e95b9abSLydia Wang 
37303e95b9abSLydia Wang 	/* PW4 (28h), MW4 (18h), MUX4(38h) */
37313e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37323e95b9abSLydia Wang 	set_pin_power_state(codec, 0x28, &parm);
3733054d867eSTakashi Iwai 	update_power_state(codec, 0x18, parm);
3734054d867eSTakashi Iwai 	update_power_state(codec, 0x38, parm);
37353e95b9abSLydia Wang 
37363e95b9abSLydia Wang 	/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
37373e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37383e95b9abSLydia Wang 	set_pin_power_state(codec, 0x25, &parm);
3739054d867eSTakashi Iwai 	update_power_state(codec, 0x15, parm);
3740054d867eSTakashi Iwai 	update_power_state(codec, 0x35, parm);
37413e95b9abSLydia Wang 	if (spec->hp_independent_mode)
3742054d867eSTakashi Iwai 		update_power_state(codec, 0x9, AC_PWRST_D0);
37433e95b9abSLydia Wang 
37443e95b9abSLydia Wang 	/* Internal Speaker */
37453e95b9abSLydia Wang 	/* PW0 (24h), MW0(14h), MUX0(34h) */
37463e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x25);
37473e95b9abSLydia Wang 
37483e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37493e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
37503e95b9abSLydia Wang 	if (present) {
3751054d867eSTakashi Iwai 		update_power_state(codec, 0x14, AC_PWRST_D3);
3752054d867eSTakashi Iwai 		update_power_state(codec, 0x34, AC_PWRST_D3);
37533e95b9abSLydia Wang 	} else {
3754054d867eSTakashi Iwai 		update_power_state(codec, 0x14, AC_PWRST_D0);
3755054d867eSTakashi Iwai 		update_power_state(codec, 0x34, AC_PWRST_D0);
37563e95b9abSLydia Wang 	}
37573e95b9abSLydia Wang 
37583e95b9abSLydia Wang 
37593e95b9abSLydia Wang 	/* Mono Out */
37603e95b9abSLydia Wang 	/* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
37613e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x28);
37623e95b9abSLydia Wang 
37633e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37643e95b9abSLydia Wang 	set_pin_power_state(codec, 0x31, &parm);
37653e95b9abSLydia Wang 	if (present) {
3766054d867eSTakashi Iwai 		update_power_state(codec, 0x1c, AC_PWRST_D3);
3767054d867eSTakashi Iwai 		update_power_state(codec, 0x3c, AC_PWRST_D3);
3768054d867eSTakashi Iwai 		update_power_state(codec, 0x3e, AC_PWRST_D3);
37693e95b9abSLydia Wang 	} else {
3770054d867eSTakashi Iwai 		update_power_state(codec, 0x1c, AC_PWRST_D0);
3771054d867eSTakashi Iwai 		update_power_state(codec, 0x3c, AC_PWRST_D0);
3772054d867eSTakashi Iwai 		update_power_state(codec, 0x3e, AC_PWRST_D0);
37733e95b9abSLydia Wang 	}
37743e95b9abSLydia Wang 
37753e95b9abSLydia Wang 	/* PW15 (33h), MW15 (1dh), MUX15(3dh) */
37763e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37773e95b9abSLydia Wang 	set_pin_power_state(codec, 0x33, &parm);
3778054d867eSTakashi Iwai 	update_power_state(codec, 0x1d, parm);
3779054d867eSTakashi Iwai 	update_power_state(codec, 0x3d, parm);
37803e95b9abSLydia Wang 
37813e95b9abSLydia Wang }
3782ab6734e7SLydia Wang 
3783ab6734e7SLydia Wang /* patch for vt1812 */
3784ab6734e7SLydia Wang static int patch_vt1812(struct hda_codec *codec)
3785ab6734e7SLydia Wang {
3786ab6734e7SLydia Wang 	struct via_spec *spec;
3787ab6734e7SLydia Wang 	int err;
3788ab6734e7SLydia Wang 
3789ab6734e7SLydia Wang 	/* create a codec specific record */
37905b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3791ab6734e7SLydia Wang 	if (spec == NULL)
3792ab6734e7SLydia Wang 		return -ENOMEM;
3793ab6734e7SLydia Wang 
3794620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
3795d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3796d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
379730b45033STakashi Iwai 	add_secret_dac_path(codec);
3798620e2b28STakashi Iwai 
3799ab6734e7SLydia Wang 	/* automatic parse from the BIOS config */
380012daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3801ab6734e7SLydia Wang 	if (err < 0) {
3802ab6734e7SLydia Wang 		via_free(codec);
3803ab6734e7SLydia Wang 		return err;
3804ab6734e7SLydia Wang 	}
3805ab6734e7SLydia Wang 
3806096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++]  = vt1812_init_verbs;
3807ab6734e7SLydia Wang 
3808ab6734e7SLydia Wang 	codec->patch_ops = via_patch_ops;
3809ab6734e7SLydia Wang 
38103e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1812;
3811ab6734e7SLydia Wang 	return 0;
3812ab6734e7SLydia Wang }
3813ab6734e7SLydia Wang 
3814c577b8a1SJoseph Chan /*
3815c577b8a1SJoseph Chan  * patch entries
3816c577b8a1SJoseph Chan  */
381790dd48a1STakashi Iwai static const struct hda_codec_preset snd_hda_preset_via[] = {
38183218c178STakashi Iwai 	{ .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
38193218c178STakashi Iwai 	{ .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
38203218c178STakashi Iwai 	{ .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
38213218c178STakashi Iwai 	{ .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
38223218c178STakashi Iwai 	{ .id = 0x1106e710, .name = "VT1709 10-Ch",
3823ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38243218c178STakashi Iwai 	{ .id = 0x1106e711, .name = "VT1709 10-Ch",
3825ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38263218c178STakashi Iwai 	{ .id = 0x1106e712, .name = "VT1709 10-Ch",
3827ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38283218c178STakashi Iwai 	{ .id = 0x1106e713, .name = "VT1709 10-Ch",
3829ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38303218c178STakashi Iwai 	{ .id = 0x1106e714, .name = "VT1709 6-Ch",
3831ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38323218c178STakashi Iwai 	{ .id = 0x1106e715, .name = "VT1709 6-Ch",
3833ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38343218c178STakashi Iwai 	{ .id = 0x1106e716, .name = "VT1709 6-Ch",
3835ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38363218c178STakashi Iwai 	{ .id = 0x1106e717, .name = "VT1709 6-Ch",
3837ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38383218c178STakashi Iwai 	{ .id = 0x1106e720, .name = "VT1708B 8-Ch",
3839ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38403218c178STakashi Iwai 	{ .id = 0x1106e721, .name = "VT1708B 8-Ch",
3841ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38423218c178STakashi Iwai 	{ .id = 0x1106e722, .name = "VT1708B 8-Ch",
3843ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38443218c178STakashi Iwai 	{ .id = 0x1106e723, .name = "VT1708B 8-Ch",
3845ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38463218c178STakashi Iwai 	{ .id = 0x1106e724, .name = "VT1708B 4-Ch",
3847ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38483218c178STakashi Iwai 	{ .id = 0x1106e725, .name = "VT1708B 4-Ch",
3849ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38503218c178STakashi Iwai 	{ .id = 0x1106e726, .name = "VT1708B 4-Ch",
3851ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38523218c178STakashi Iwai 	{ .id = 0x1106e727, .name = "VT1708B 4-Ch",
3853ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38543218c178STakashi Iwai 	{ .id = 0x11060397, .name = "VT1708S",
3855d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38563218c178STakashi Iwai 	{ .id = 0x11061397, .name = "VT1708S",
3857d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38583218c178STakashi Iwai 	{ .id = 0x11062397, .name = "VT1708S",
3859d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38603218c178STakashi Iwai 	{ .id = 0x11063397, .name = "VT1708S",
3861d949cac1SHarald Welte 	  .patch = patch_vt1708S},
3862bc92df7fSLydia Wang 	{ .id = 0x11064397, .name = "VT1705",
3863d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38643218c178STakashi Iwai 	{ .id = 0x11065397, .name = "VT1708S",
3865d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38663218c178STakashi Iwai 	{ .id = 0x11066397, .name = "VT1708S",
3867d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38683218c178STakashi Iwai 	{ .id = 0x11067397, .name = "VT1708S",
3869d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38703218c178STakashi Iwai 	{ .id = 0x11060398, .name = "VT1702",
3871d949cac1SHarald Welte 	  .patch = patch_vt1702},
38723218c178STakashi Iwai 	{ .id = 0x11061398, .name = "VT1702",
3873d949cac1SHarald Welte 	  .patch = patch_vt1702},
38743218c178STakashi Iwai 	{ .id = 0x11062398, .name = "VT1702",
3875d949cac1SHarald Welte 	  .patch = patch_vt1702},
38763218c178STakashi Iwai 	{ .id = 0x11063398, .name = "VT1702",
3877d949cac1SHarald Welte 	  .patch = patch_vt1702},
38783218c178STakashi Iwai 	{ .id = 0x11064398, .name = "VT1702",
3879d949cac1SHarald Welte 	  .patch = patch_vt1702},
38803218c178STakashi Iwai 	{ .id = 0x11065398, .name = "VT1702",
3881d949cac1SHarald Welte 	  .patch = patch_vt1702},
38823218c178STakashi Iwai 	{ .id = 0x11066398, .name = "VT1702",
3883d949cac1SHarald Welte 	  .patch = patch_vt1702},
38843218c178STakashi Iwai 	{ .id = 0x11067398, .name = "VT1702",
3885d949cac1SHarald Welte 	  .patch = patch_vt1702},
3886eb7188caSLydia Wang 	{ .id = 0x11060428, .name = "VT1718S",
3887eb7188caSLydia Wang 	  .patch = patch_vt1718S},
3888eb7188caSLydia Wang 	{ .id = 0x11064428, .name = "VT1718S",
3889eb7188caSLydia Wang 	  .patch = patch_vt1718S},
3890bb3c6bfcSLydia Wang 	{ .id = 0x11060441, .name = "VT2020",
3891bb3c6bfcSLydia Wang 	  .patch = patch_vt1718S},
3892bb3c6bfcSLydia Wang 	{ .id = 0x11064441, .name = "VT1828S",
3893bb3c6bfcSLydia Wang 	  .patch = patch_vt1718S},
3894f3db423dSLydia Wang 	{ .id = 0x11060433, .name = "VT1716S",
3895f3db423dSLydia Wang 	  .patch = patch_vt1716S},
3896f3db423dSLydia Wang 	{ .id = 0x1106a721, .name = "VT1716S",
3897f3db423dSLydia Wang 	  .patch = patch_vt1716S},
389825eaba2fSLydia Wang 	{ .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
389925eaba2fSLydia Wang 	{ .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
3900ab6734e7SLydia Wang 	{ .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
390136dd5c4aSLydia Wang 	{ .id = 0x11060440, .name = "VT1818S",
390236dd5c4aSLydia Wang 	  .patch = patch_vt1708S},
390311890956SLydia Wang 	{ .id = 0x11060446, .name = "VT1802",
390411890956SLydia Wang 		.patch = patch_vt2002P},
390511890956SLydia Wang 	{ .id = 0x11068446, .name = "VT1802",
390611890956SLydia Wang 		.patch = patch_vt2002P},
3907c577b8a1SJoseph Chan 	{} /* terminator */
3908c577b8a1SJoseph Chan };
39091289e9e8STakashi Iwai 
39101289e9e8STakashi Iwai MODULE_ALIAS("snd-hda-codec-id:1106*");
39111289e9e8STakashi Iwai 
39121289e9e8STakashi Iwai static struct hda_codec_preset_list via_list = {
39131289e9e8STakashi Iwai 	.preset = snd_hda_preset_via,
39141289e9e8STakashi Iwai 	.owner = THIS_MODULE,
39151289e9e8STakashi Iwai };
39161289e9e8STakashi Iwai 
39171289e9e8STakashi Iwai MODULE_LICENSE("GPL");
39181289e9e8STakashi Iwai MODULE_DESCRIPTION("VIA HD-audio codec");
39191289e9e8STakashi Iwai 
39201289e9e8STakashi Iwai static int __init patch_via_init(void)
39211289e9e8STakashi Iwai {
39221289e9e8STakashi Iwai 	return snd_hda_add_codec_preset(&via_list);
39231289e9e8STakashi Iwai }
39241289e9e8STakashi Iwai 
39251289e9e8STakashi Iwai static void __exit patch_via_exit(void)
39261289e9e8STakashi Iwai {
39271289e9e8STakashi Iwai 	snd_hda_delete_codec_preset(&via_list);
39281289e9e8STakashi Iwai }
39291289e9e8STakashi Iwai 
39301289e9e8STakashi Iwai module_init(patch_via_init)
39311289e9e8STakashi Iwai module_exit(patch_via_exit)
3932