xref: /openbmc/linux/sound/pci/hda/patch_via.c (revision 4808d12d1dddb046ec86425e5f6766f02e950292)
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"
571835a0f9STakashi Iwai #include "hda_jack.h"
58c577b8a1SJoseph Chan 
59c577b8a1SJoseph Chan /* Pin Widget NID */
6076d9b0ddSHarald Welte #define VT1708_HP_PIN_NID	0x20
6176d9b0ddSHarald Welte #define VT1708_CD_PIN_NID	0x24
62c577b8a1SJoseph Chan 
63d7426329SHarald Welte enum VIA_HDA_CODEC {
64d7426329SHarald Welte 	UNKNOWN = -1,
65d7426329SHarald Welte 	VT1708,
66d7426329SHarald Welte 	VT1709_10CH,
67d7426329SHarald Welte 	VT1709_6CH,
68d7426329SHarald Welte 	VT1708B_8CH,
69d7426329SHarald Welte 	VT1708B_4CH,
70d7426329SHarald Welte 	VT1708S,
71518bf3baSLydia Wang 	VT1708BCE,
72d7426329SHarald Welte 	VT1702,
73eb7188caSLydia Wang 	VT1718S,
74f3db423dSLydia Wang 	VT1716S,
7525eaba2fSLydia Wang 	VT2002P,
76ab6734e7SLydia Wang 	VT1812,
7711890956SLydia Wang 	VT1802,
78d7426329SHarald Welte 	CODEC_TYPES,
79d7426329SHarald Welte };
80d7426329SHarald Welte 
8111890956SLydia Wang #define VT2002P_COMPATIBLE(spec) \
8211890956SLydia Wang 	((spec)->codec_type == VT2002P ||\
8311890956SLydia Wang 	 (spec)->codec_type == VT1812 ||\
8411890956SLydia Wang 	 (spec)->codec_type == VT1802)
8511890956SLydia Wang 
868e3679dcSTakashi Iwai #define MAX_NID_PATH_DEPTH	5
878e3679dcSTakashi Iwai 
8809a9ad69STakashi Iwai /* output-path: DAC -> ... -> pin
8909a9ad69STakashi Iwai  * idx[] contains the source index number of the next widget;
9009a9ad69STakashi Iwai  * e.g. idx[0] is the index of the DAC selected by path[1] widget
9109a9ad69STakashi Iwai  * multi[] indicates whether it's a selector widget with multi-connectors
9209a9ad69STakashi Iwai  * (i.e. the connection selection is mandatory)
9309a9ad69STakashi Iwai  * vol_ctl and mute_ctl contains the NIDs for the assigned mixers
9409a9ad69STakashi Iwai  */
954a79616dSTakashi Iwai struct nid_path {
964a79616dSTakashi Iwai 	int depth;
978e3679dcSTakashi Iwai 	hda_nid_t path[MAX_NID_PATH_DEPTH];
9809a9ad69STakashi Iwai 	unsigned char idx[MAX_NID_PATH_DEPTH];
9909a9ad69STakashi Iwai 	unsigned char multi[MAX_NID_PATH_DEPTH];
10009a9ad69STakashi Iwai 	unsigned int vol_ctl;
10109a9ad69STakashi Iwai 	unsigned int mute_ctl;
1024a79616dSTakashi Iwai };
1034a79616dSTakashi Iwai 
104a86a88eaSTakashi Iwai /* input-path */
105a86a88eaSTakashi Iwai struct via_input {
106a86a88eaSTakashi Iwai 	hda_nid_t pin;	/* input-pin or aa-mix */
107a86a88eaSTakashi Iwai 	int adc_idx;	/* ADC index to be used */
108a86a88eaSTakashi Iwai 	int mux_idx;	/* MUX index (if any) */
109a86a88eaSTakashi Iwai 	const char *label;	/* input-source label */
110a86a88eaSTakashi Iwai };
111a86a88eaSTakashi Iwai 
112de6c74f3STakashi Iwai #define VIA_MAX_ADCS	3
113de6c74f3STakashi Iwai 
1143b607e3dSTakashi Iwai enum {
1153b607e3dSTakashi Iwai 	STREAM_MULTI_OUT = (1 << 0),
1163b607e3dSTakashi Iwai 	STREAM_INDEP_HP = (1 << 1),
1173b607e3dSTakashi Iwai };
1183b607e3dSTakashi Iwai 
1191f2e99feSLydia Wang struct via_spec {
1201f2e99feSLydia Wang 	/* codec parameterization */
12190dd48a1STakashi Iwai 	const struct snd_kcontrol_new *mixers[6];
1221f2e99feSLydia Wang 	unsigned int num_mixers;
1231f2e99feSLydia Wang 
12490dd48a1STakashi Iwai 	const struct hda_verb *init_verbs[5];
1251f2e99feSLydia Wang 	unsigned int num_iverbs;
1261f2e99feSLydia Wang 
12782673bc8STakashi Iwai 	char stream_name_analog[32];
1287eb56e84STakashi Iwai 	char stream_name_hp[32];
12990dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_analog_playback;
13090dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_analog_capture;
1311f2e99feSLydia Wang 
13282673bc8STakashi Iwai 	char stream_name_digital[32];
13390dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_digital_playback;
13490dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_digital_capture;
1351f2e99feSLydia Wang 
1361f2e99feSLydia Wang 	/* playback */
1371f2e99feSLydia Wang 	struct hda_multi_out multiout;
1381f2e99feSLydia Wang 	hda_nid_t slave_dig_outs[2];
139ece8d043STakashi Iwai 	hda_nid_t hp_dac_nid;
1403214b966STakashi Iwai 	hda_nid_t speaker_dac_nid;
1413214b966STakashi Iwai 	int hp_indep_shared;	/* indep HP-DAC is shared with side ch */
1423b607e3dSTakashi Iwai 	int opened_streams;	/* STREAM_* bits */
1433b607e3dSTakashi Iwai 	int active_streams;	/* STREAM_* bits */
1443214b966STakashi Iwai 	int aamix_mode;		/* loopback is enabled for output-path? */
1451f2e99feSLydia Wang 
1463214b966STakashi Iwai 	/* Output-paths:
1473214b966STakashi Iwai 	 * There are different output-paths depending on the setup.
1483214b966STakashi Iwai 	 * out_path, hp_path and speaker_path are primary paths.  If both
1493214b966STakashi Iwai 	 * direct DAC and aa-loopback routes are available, these contain
1503214b966STakashi Iwai 	 * the former paths.  Meanwhile *_mix_path contain the paths with
1513214b966STakashi Iwai 	 * loopback mixer.  (Since the loopback is only for front channel,
1523214b966STakashi Iwai 	 * no out_mix_path for surround channels.)
1533214b966STakashi Iwai 	 * The HP output has another path, hp_indep_path, which is used in
1543214b966STakashi Iwai 	 * the independent-HP mode.
1553214b966STakashi Iwai 	 */
156de6c74f3STakashi Iwai 	struct nid_path out_path[HDA_SIDE + 1];
1573214b966STakashi Iwai 	struct nid_path out_mix_path;
1584a79616dSTakashi Iwai 	struct nid_path hp_path;
1593214b966STakashi Iwai 	struct nid_path hp_mix_path;
1603214b966STakashi Iwai 	struct nid_path hp_indep_path;
1614a918ffeSTakashi Iwai 	struct nid_path speaker_path;
1623214b966STakashi Iwai 	struct nid_path speaker_mix_path;
1634a79616dSTakashi Iwai 
1641f2e99feSLydia Wang 	/* capture */
1651f2e99feSLydia Wang 	unsigned int num_adc_nids;
166de6c74f3STakashi Iwai 	hda_nid_t adc_nids[VIA_MAX_ADCS];
167de6c74f3STakashi Iwai 	hda_nid_t mux_nids[VIA_MAX_ADCS];
168620e2b28STakashi Iwai 	hda_nid_t aa_mix_nid;
1691f2e99feSLydia Wang 	hda_nid_t dig_in_nid;
1701f2e99feSLydia Wang 
1711f2e99feSLydia Wang 	/* capture source */
172a86a88eaSTakashi Iwai 	bool dyn_adc_switch;
173a86a88eaSTakashi Iwai 	int num_inputs;
174a86a88eaSTakashi Iwai 	struct via_input inputs[AUTO_CFG_MAX_INS + 1];
175de6c74f3STakashi Iwai 	unsigned int cur_mux[VIA_MAX_ADCS];
1761f2e99feSLydia Wang 
1773b607e3dSTakashi Iwai 	/* dynamic DAC switching */
1783b607e3dSTakashi Iwai 	unsigned int cur_dac_stream_tag;
1793b607e3dSTakashi Iwai 	unsigned int cur_dac_format;
1803b607e3dSTakashi Iwai 	unsigned int cur_hp_stream_tag;
1813b607e3dSTakashi Iwai 	unsigned int cur_hp_format;
1823b607e3dSTakashi Iwai 
183a86a88eaSTakashi Iwai 	/* dynamic ADC switching */
184a86a88eaSTakashi Iwai 	hda_nid_t cur_adc;
185a86a88eaSTakashi Iwai 	unsigned int cur_adc_stream_tag;
186a86a88eaSTakashi Iwai 	unsigned int cur_adc_format;
187a86a88eaSTakashi Iwai 
1881f2e99feSLydia Wang 	/* PCM information */
1891f2e99feSLydia Wang 	struct hda_pcm pcm_rec[3];
1901f2e99feSLydia Wang 
1911f2e99feSLydia Wang 	/* dynamic controls, init_verbs and input_mux */
1921f2e99feSLydia Wang 	struct auto_pin_cfg autocfg;
1931f2e99feSLydia Wang 	struct snd_array kctls;
1941f2e99feSLydia Wang 	hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
1951f2e99feSLydia Wang 
1961f2e99feSLydia Wang 	/* HP mode source */
1971f2e99feSLydia Wang 	unsigned int hp_independent_mode;
198f3db423dSLydia Wang 	unsigned int dmic_enabled;
19924088a58STakashi Iwai 	unsigned int no_pin_power_ctl;
2001f2e99feSLydia Wang 	enum VIA_HDA_CODEC codec_type;
2011f2e99feSLydia Wang 
202e3d7a143STakashi Iwai 	/* smart51 setup */
203e3d7a143STakashi Iwai 	unsigned int smart51_nums;
204e3d7a143STakashi Iwai 	hda_nid_t smart51_pins[2];
205e3d7a143STakashi Iwai 	int smart51_idxs[2];
206e3d7a143STakashi Iwai 	const char *smart51_labels[2];
207e3d7a143STakashi Iwai 	unsigned int smart51_enabled;
208e3d7a143STakashi Iwai 
2091f2e99feSLydia Wang 	/* work to check hp jack state */
2101f2e99feSLydia Wang 	struct hda_codec *codec;
2111f2e99feSLydia Wang 	struct delayed_work vt1708_hp_work;
212187d333eSTakashi Iwai 	int hp_work_active;
213e06e5a29STakashi Iwai 	int vt1708_jack_detect;
2141f2e99feSLydia Wang 	int vt1708_hp_present;
2153e95b9abSLydia Wang 
2163e95b9abSLydia Wang 	void (*set_widgets_power_state)(struct hda_codec *codec);
2173e95b9abSLydia Wang 
2181f2e99feSLydia Wang 	struct hda_loopback_check loopback;
21913af8e77STakashi Iwai 	int num_loopbacks;
22013af8e77STakashi Iwai 	struct hda_amp_list loopback_list[8];
221a86a88eaSTakashi Iwai 
222a86a88eaSTakashi Iwai 	/* bind capture-volume */
223a86a88eaSTakashi Iwai 	struct hda_bind_ctls *bind_cap_vol;
224a86a88eaSTakashi Iwai 	struct hda_bind_ctls *bind_cap_sw;
2253b607e3dSTakashi Iwai 
2263b607e3dSTakashi Iwai 	struct mutex config_mutex;
2271f2e99feSLydia Wang };
2281f2e99feSLydia Wang 
2290341ccd7SLydia Wang static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
2305b0cb1d8SJaroslav Kysela static struct via_spec * via_new_spec(struct hda_codec *codec)
2315b0cb1d8SJaroslav Kysela {
2325b0cb1d8SJaroslav Kysela 	struct via_spec *spec;
2335b0cb1d8SJaroslav Kysela 
2345b0cb1d8SJaroslav Kysela 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2355b0cb1d8SJaroslav Kysela 	if (spec == NULL)
2365b0cb1d8SJaroslav Kysela 		return NULL;
2375b0cb1d8SJaroslav Kysela 
2383b607e3dSTakashi Iwai 	mutex_init(&spec->config_mutex);
2395b0cb1d8SJaroslav Kysela 	codec->spec = spec;
2405b0cb1d8SJaroslav Kysela 	spec->codec = codec;
2410341ccd7SLydia Wang 	spec->codec_type = get_codec_type(codec);
2420341ccd7SLydia Wang 	/* VT1708BCE & VT1708S are almost same */
2430341ccd7SLydia Wang 	if (spec->codec_type == VT1708BCE)
2440341ccd7SLydia Wang 		spec->codec_type = VT1708S;
2455b0cb1d8SJaroslav Kysela 	return spec;
2465b0cb1d8SJaroslav Kysela }
2475b0cb1d8SJaroslav Kysela 
248744ff5f4SLydia Wang static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
249d7426329SHarald Welte {
250744ff5f4SLydia Wang 	u32 vendor_id = codec->vendor_id;
251d7426329SHarald Welte 	u16 ven_id = vendor_id >> 16;
252d7426329SHarald Welte 	u16 dev_id = vendor_id & 0xffff;
253d7426329SHarald Welte 	enum VIA_HDA_CODEC codec_type;
254d7426329SHarald Welte 
255d7426329SHarald Welte 	/* get codec type */
256d7426329SHarald Welte 	if (ven_id != 0x1106)
257d7426329SHarald Welte 		codec_type = UNKNOWN;
258d7426329SHarald Welte 	else if (dev_id >= 0x1708 && dev_id <= 0x170b)
259d7426329SHarald Welte 		codec_type = VT1708;
260d7426329SHarald Welte 	else if (dev_id >= 0xe710 && dev_id <= 0xe713)
261d7426329SHarald Welte 		codec_type = VT1709_10CH;
262d7426329SHarald Welte 	else if (dev_id >= 0xe714 && dev_id <= 0xe717)
263d7426329SHarald Welte 		codec_type = VT1709_6CH;
264518bf3baSLydia Wang 	else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
265d7426329SHarald Welte 		codec_type = VT1708B_8CH;
266518bf3baSLydia Wang 		if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
267518bf3baSLydia Wang 			codec_type = VT1708BCE;
268518bf3baSLydia Wang 	} else if (dev_id >= 0xe724 && dev_id <= 0xe727)
269d7426329SHarald Welte 		codec_type = VT1708B_4CH;
270d7426329SHarald Welte 	else if ((dev_id & 0xfff) == 0x397
271d7426329SHarald Welte 		 && (dev_id >> 12) < 8)
272d7426329SHarald Welte 		codec_type = VT1708S;
273d7426329SHarald Welte 	else if ((dev_id & 0xfff) == 0x398
274d7426329SHarald Welte 		 && (dev_id >> 12) < 8)
275d7426329SHarald Welte 		codec_type = VT1702;
276eb7188caSLydia Wang 	else if ((dev_id & 0xfff) == 0x428
277eb7188caSLydia Wang 		 && (dev_id >> 12) < 8)
278eb7188caSLydia Wang 		codec_type = VT1718S;
279f3db423dSLydia Wang 	else if (dev_id == 0x0433 || dev_id == 0xa721)
280f3db423dSLydia Wang 		codec_type = VT1716S;
281bb3c6bfcSLydia Wang 	else if (dev_id == 0x0441 || dev_id == 0x4441)
282bb3c6bfcSLydia Wang 		codec_type = VT1718S;
28325eaba2fSLydia Wang 	else if (dev_id == 0x0438 || dev_id == 0x4438)
28425eaba2fSLydia Wang 		codec_type = VT2002P;
285ab6734e7SLydia Wang 	else if (dev_id == 0x0448)
286ab6734e7SLydia Wang 		codec_type = VT1812;
28736dd5c4aSLydia Wang 	else if (dev_id == 0x0440)
28836dd5c4aSLydia Wang 		codec_type = VT1708S;
28911890956SLydia Wang 	else if ((dev_id & 0xfff) == 0x446)
29011890956SLydia Wang 		codec_type = VT1802;
291d7426329SHarald Welte 	else
292d7426329SHarald Welte 		codec_type = UNKNOWN;
293d7426329SHarald Welte 	return codec_type;
294d7426329SHarald Welte };
295d7426329SHarald Welte 
296ec7e7e42SLydia Wang #define VIA_JACK_EVENT		0x20
29769e52a80SHarald Welte #define VIA_HP_EVENT		0x01
29869e52a80SHarald Welte #define VIA_GPIO_EVENT		0x02
2994a918ffeSTakashi Iwai #define VIA_LINE_EVENT		0x03
30069e52a80SHarald Welte 
301c577b8a1SJoseph Chan enum {
302c577b8a1SJoseph Chan 	VIA_CTL_WIDGET_VOL,
303c577b8a1SJoseph Chan 	VIA_CTL_WIDGET_MUTE,
304f5271101SLydia Wang 	VIA_CTL_WIDGET_ANALOG_MUTE,
305c577b8a1SJoseph Chan };
306c577b8a1SJoseph Chan 
307ada509ecSTakashi Iwai static void analog_low_current_mode(struct hda_codec *codec);
308ada509ecSTakashi Iwai static bool is_aa_path_mute(struct hda_codec *codec);
3091f2e99feSLydia Wang 
310187d333eSTakashi Iwai #define hp_detect_with_aa(codec) \
311187d333eSTakashi Iwai 	(snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1 && \
312187d333eSTakashi Iwai 	 !is_aa_path_mute(codec))
3131f2e99feSLydia Wang 
3141f2e99feSLydia Wang static void vt1708_stop_hp_work(struct via_spec *spec)
3151f2e99feSLydia Wang {
3161f2e99feSLydia Wang 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
3171f2e99feSLydia Wang 		return;
318187d333eSTakashi Iwai 	if (spec->hp_work_active) {
319187d333eSTakashi Iwai 		snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 1);
3205b84ba26STejun Heo 		cancel_delayed_work_sync(&spec->vt1708_hp_work);
321187d333eSTakashi Iwai 		spec->hp_work_active = 0;
322187d333eSTakashi Iwai 	}
323187d333eSTakashi Iwai }
324187d333eSTakashi Iwai 
325187d333eSTakashi Iwai static void vt1708_update_hp_work(struct via_spec *spec)
326187d333eSTakashi Iwai {
327187d333eSTakashi Iwai 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
328187d333eSTakashi Iwai 		return;
329187d333eSTakashi Iwai 	if (spec->vt1708_jack_detect &&
330187d333eSTakashi Iwai 	    (spec->active_streams || hp_detect_with_aa(spec->codec))) {
331187d333eSTakashi Iwai 		if (!spec->hp_work_active) {
332187d333eSTakashi Iwai 			snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 0);
333187d333eSTakashi Iwai 			schedule_delayed_work(&spec->vt1708_hp_work,
334187d333eSTakashi Iwai 					      msecs_to_jiffies(100));
335187d333eSTakashi Iwai 			spec->hp_work_active = 1;
336187d333eSTakashi Iwai 		}
337187d333eSTakashi Iwai 	} else if (!hp_detect_with_aa(spec->codec))
338187d333eSTakashi Iwai 		vt1708_stop_hp_work(spec);
3391f2e99feSLydia Wang }
340f5271101SLydia Wang 
3413e95b9abSLydia Wang static void set_widgets_power_state(struct hda_codec *codec)
3423e95b9abSLydia Wang {
3433e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
3443e95b9abSLydia Wang 	if (spec->set_widgets_power_state)
3453e95b9abSLydia Wang 		spec->set_widgets_power_state(codec);
3463e95b9abSLydia Wang }
34725eaba2fSLydia Wang 
348f5271101SLydia Wang static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
349f5271101SLydia Wang 				   struct snd_ctl_elem_value *ucontrol)
350f5271101SLydia Wang {
351f5271101SLydia Wang 	int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
352f5271101SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
353f5271101SLydia Wang 
3543e95b9abSLydia Wang 	set_widgets_power_state(codec);
355ada509ecSTakashi Iwai 	analog_low_current_mode(snd_kcontrol_chip(kcontrol));
356187d333eSTakashi Iwai 	vt1708_update_hp_work(codec->spec);
357f5271101SLydia Wang 	return change;
358f5271101SLydia Wang }
359f5271101SLydia Wang 
360f5271101SLydia Wang /* modify .put = snd_hda_mixer_amp_switch_put */
361f5271101SLydia Wang #define ANALOG_INPUT_MUTE						\
362f5271101SLydia Wang 	{		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,		\
363f5271101SLydia Wang 			.name = NULL,					\
364f5271101SLydia Wang 			.index = 0,					\
365f5271101SLydia Wang 			.info = snd_hda_mixer_amp_switch_info,		\
366f5271101SLydia Wang 			.get = snd_hda_mixer_amp_switch_get,		\
367f5271101SLydia Wang 			.put = analog_input_switch_put,			\
368f5271101SLydia Wang 			.private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
369f5271101SLydia Wang 
37090dd48a1STakashi Iwai static const struct snd_kcontrol_new via_control_templates[] = {
371c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME(NULL, 0, 0, 0),
372c577b8a1SJoseph Chan 	HDA_CODEC_MUTE(NULL, 0, 0, 0),
373f5271101SLydia Wang 	ANALOG_INPUT_MUTE,
374c577b8a1SJoseph Chan };
375c577b8a1SJoseph Chan 
376ab6734e7SLydia Wang 
377c577b8a1SJoseph Chan /* add dynamic controls */
378291c9e33STakashi Iwai static struct snd_kcontrol_new *__via_clone_ctl(struct via_spec *spec,
379291c9e33STakashi Iwai 				const struct snd_kcontrol_new *tmpl,
380291c9e33STakashi Iwai 				const char *name)
381c577b8a1SJoseph Chan {
382c577b8a1SJoseph Chan 	struct snd_kcontrol_new *knew;
383c577b8a1SJoseph Chan 
384603c4019STakashi Iwai 	snd_array_init(&spec->kctls, sizeof(*knew), 32);
385603c4019STakashi Iwai 	knew = snd_array_new(&spec->kctls);
386c577b8a1SJoseph Chan 	if (!knew)
387291c9e33STakashi Iwai 		return NULL;
388291c9e33STakashi Iwai 	*knew = *tmpl;
389291c9e33STakashi Iwai 	if (!name)
390291c9e33STakashi Iwai 		name = tmpl->name;
391291c9e33STakashi Iwai 	if (name) {
392c577b8a1SJoseph Chan 		knew->name = kstrdup(name, GFP_KERNEL);
393c577b8a1SJoseph Chan 		if (!knew->name)
394291c9e33STakashi Iwai 			return NULL;
395291c9e33STakashi Iwai 	}
396291c9e33STakashi Iwai 	return knew;
397291c9e33STakashi Iwai }
398291c9e33STakashi Iwai 
399291c9e33STakashi Iwai static int __via_add_control(struct via_spec *spec, int type, const char *name,
400291c9e33STakashi Iwai 			     int idx, unsigned long val)
401291c9e33STakashi Iwai {
402291c9e33STakashi Iwai 	struct snd_kcontrol_new *knew;
403291c9e33STakashi Iwai 
404291c9e33STakashi Iwai 	knew = __via_clone_ctl(spec, &via_control_templates[type], name);
405291c9e33STakashi Iwai 	if (!knew)
406c577b8a1SJoseph Chan 		return -ENOMEM;
407d7a99cceSTakashi Iwai 	knew->index = idx;
4084d02d1b6SJaroslav Kysela 	if (get_amp_nid_(val))
4095e26dfd0SJaroslav Kysela 		knew->subdevice = HDA_SUBDEV_AMP_FLAG;
410c577b8a1SJoseph Chan 	knew->private_value = val;
411c577b8a1SJoseph Chan 	return 0;
412c577b8a1SJoseph Chan }
413c577b8a1SJoseph Chan 
4147b315bb4STakashi Iwai #define via_add_control(spec, type, name, val) \
4157b315bb4STakashi Iwai 	__via_add_control(spec, type, name, 0, val)
4167b315bb4STakashi Iwai 
417291c9e33STakashi Iwai #define via_clone_control(spec, tmpl) __via_clone_ctl(spec, tmpl, NULL)
4185b0cb1d8SJaroslav Kysela 
419603c4019STakashi Iwai static void via_free_kctls(struct hda_codec *codec)
420603c4019STakashi Iwai {
421603c4019STakashi Iwai 	struct via_spec *spec = codec->spec;
422603c4019STakashi Iwai 
423603c4019STakashi Iwai 	if (spec->kctls.list) {
424603c4019STakashi Iwai 		struct snd_kcontrol_new *kctl = spec->kctls.list;
425603c4019STakashi Iwai 		int i;
426603c4019STakashi Iwai 		for (i = 0; i < spec->kctls.used; i++)
427603c4019STakashi Iwai 			kfree(kctl[i].name);
428603c4019STakashi Iwai 	}
429603c4019STakashi Iwai 	snd_array_free(&spec->kctls);
430603c4019STakashi Iwai }
431603c4019STakashi Iwai 
432c577b8a1SJoseph Chan /* create input playback/capture controls for the given pin */
4339510e8ddSLydia Wang static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
4347b315bb4STakashi Iwai 				int type_idx, int idx, int mix_nid)
435c577b8a1SJoseph Chan {
436c577b8a1SJoseph Chan 	char name[32];
437c577b8a1SJoseph Chan 	int err;
438c577b8a1SJoseph Chan 
439c577b8a1SJoseph Chan 	sprintf(name, "%s Playback Volume", ctlname);
4407b315bb4STakashi Iwai 	err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
441c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
442c577b8a1SJoseph Chan 	if (err < 0)
443c577b8a1SJoseph Chan 		return err;
444c577b8a1SJoseph Chan 	sprintf(name, "%s Playback Switch", ctlname);
4457b315bb4STakashi Iwai 	err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
446c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
447c577b8a1SJoseph Chan 	if (err < 0)
448c577b8a1SJoseph Chan 		return err;
449c577b8a1SJoseph Chan 	return 0;
450c577b8a1SJoseph Chan }
451c577b8a1SJoseph Chan 
4525d41762aSTakashi Iwai #define get_connection_index(codec, mux, nid) \
4538d087c76STakashi Iwai 	snd_hda_get_conn_index(codec, mux, nid, 0)
4545d41762aSTakashi Iwai 
4558df2a312STakashi Iwai static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
4568df2a312STakashi Iwai 			   unsigned int mask)
4578df2a312STakashi Iwai {
458a934d5a9STakashi Iwai 	unsigned int caps;
459a934d5a9STakashi Iwai 	if (!nid)
460a934d5a9STakashi Iwai 		return false;
461a934d5a9STakashi Iwai 	caps = get_wcaps(codec, nid);
4628df2a312STakashi Iwai 	if (dir == HDA_INPUT)
4638df2a312STakashi Iwai 		caps &= AC_WCAP_IN_AMP;
4648df2a312STakashi Iwai 	else
4658df2a312STakashi Iwai 		caps &= AC_WCAP_OUT_AMP;
4668df2a312STakashi Iwai 	if (!caps)
4678df2a312STakashi Iwai 		return false;
4688df2a312STakashi Iwai 	if (query_amp_caps(codec, nid, dir) & mask)
4698df2a312STakashi Iwai 		return true;
4708df2a312STakashi Iwai 	return false;
4718df2a312STakashi Iwai }
4728df2a312STakashi Iwai 
47309a9ad69STakashi Iwai #define have_mute(codec, nid, dir) \
47409a9ad69STakashi Iwai 	check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
4758df2a312STakashi Iwai 
476d69607b3SLydia Wang /* enable/disable the output-route mixers */
477d69607b3SLydia Wang static void activate_output_mix(struct hda_codec *codec, struct nid_path *path,
4783214b966STakashi Iwai 				hda_nid_t mix_nid, int idx, bool enable)
479d69607b3SLydia Wang {
480d69607b3SLydia Wang 	int i, num, val;
481d69607b3SLydia Wang 
482d69607b3SLydia Wang 	if (!path)
483d69607b3SLydia Wang 		return;
484d69607b3SLydia Wang 	num = snd_hda_get_conn_list(codec, mix_nid, NULL);
485d69607b3SLydia Wang 	for (i = 0; i < num; i++) {
4863214b966STakashi Iwai 		if (i == idx)
487d69607b3SLydia Wang 			val = AMP_IN_UNMUTE(i);
488d69607b3SLydia Wang 		else
489d69607b3SLydia Wang 			val = AMP_IN_MUTE(i);
490d69607b3SLydia Wang 		snd_hda_codec_write(codec, mix_nid, 0,
491d69607b3SLydia Wang 				    AC_VERB_SET_AMP_GAIN_MUTE, val);
492d69607b3SLydia Wang 	}
493d69607b3SLydia Wang }
494d69607b3SLydia Wang 
49509a9ad69STakashi Iwai /* enable/disable the output-route */
49609a9ad69STakashi Iwai static void activate_output_path(struct hda_codec *codec, struct nid_path *path,
49709a9ad69STakashi Iwai 				 bool enable, bool force)
4985d41762aSTakashi Iwai {
499d69607b3SLydia Wang 	struct via_spec *spec = codec->spec;
5003214b966STakashi Iwai 	int i;
50109a9ad69STakashi Iwai 	for (i = 0; i < path->depth; i++) {
50209a9ad69STakashi Iwai 		hda_nid_t src, dst;
50309a9ad69STakashi Iwai 		int idx = path->idx[i];
50409a9ad69STakashi Iwai 		src = path->path[i];
50509a9ad69STakashi Iwai 		if (i < path->depth - 1)
50609a9ad69STakashi Iwai 			dst = path->path[i + 1];
50709a9ad69STakashi Iwai 		else
50809a9ad69STakashi Iwai 			dst = 0;
50909a9ad69STakashi Iwai 		if (enable && path->multi[i])
51009a9ad69STakashi Iwai 			snd_hda_codec_write(codec, dst, 0,
5115d41762aSTakashi Iwai 					    AC_VERB_SET_CONNECT_SEL, idx);
5123214b966STakashi Iwai 		if (!force && (dst == spec->aa_mix_nid))
513e5e14681SLydia Wang 			continue;
5143214b966STakashi Iwai 		if (have_mute(codec, dst, HDA_INPUT))
5153214b966STakashi Iwai 			activate_output_mix(codec, path, dst, idx, enable);
51609a9ad69STakashi Iwai 		if (!force && (src == path->vol_ctl || src == path->mute_ctl))
51709a9ad69STakashi Iwai 			continue;
51809a9ad69STakashi Iwai 		if (have_mute(codec, src, HDA_OUTPUT)) {
51909a9ad69STakashi Iwai 			int val = enable ? AMP_OUT_UNMUTE : AMP_OUT_MUTE;
52009a9ad69STakashi Iwai 			snd_hda_codec_write(codec, src, 0,
52109a9ad69STakashi Iwai 					    AC_VERB_SET_AMP_GAIN_MUTE, val);
52209a9ad69STakashi Iwai 		}
52309a9ad69STakashi Iwai 	}
5245d41762aSTakashi Iwai }
5255d41762aSTakashi Iwai 
5265d41762aSTakashi Iwai /* set the given pin as output */
5275d41762aSTakashi Iwai static void init_output_pin(struct hda_codec *codec, hda_nid_t pin,
5285d41762aSTakashi Iwai 			    int pin_type)
5295d41762aSTakashi Iwai {
5305d41762aSTakashi Iwai 	if (!pin)
5315d41762aSTakashi Iwai 		return;
5325d41762aSTakashi Iwai 	snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
5335d41762aSTakashi Iwai 			    pin_type);
5345d41762aSTakashi Iwai 	if (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD)
5355d41762aSTakashi Iwai 		snd_hda_codec_write(codec, pin, 0,
536d3a11e60STakashi Iwai 				    AC_VERB_SET_EAPD_BTLENABLE, 0x02);
537c577b8a1SJoseph Chan }
538c577b8a1SJoseph Chan 
53909a9ad69STakashi Iwai static void via_auto_init_output(struct hda_codec *codec,
540a353fbb1STakashi Iwai 				 struct nid_path *path, int pin_type)
5415d41762aSTakashi Iwai {
5425d41762aSTakashi Iwai 	unsigned int caps;
543d69607b3SLydia Wang 	hda_nid_t pin;
5445d41762aSTakashi Iwai 
54509a9ad69STakashi Iwai 	if (!path->depth)
5465d41762aSTakashi Iwai 		return;
54709a9ad69STakashi Iwai 	pin = path->path[path->depth - 1];
5485d41762aSTakashi Iwai 
5495d41762aSTakashi Iwai 	init_output_pin(codec, pin, pin_type);
5505d41762aSTakashi Iwai 	caps = query_amp_caps(codec, pin, HDA_OUTPUT);
5515d41762aSTakashi Iwai 	if (caps & AC_AMPCAP_MUTE) {
5525d41762aSTakashi Iwai 		unsigned int val;
5535d41762aSTakashi Iwai 		val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
5545d41762aSTakashi Iwai 		snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
5555d41762aSTakashi Iwai 				    AMP_OUT_MUTE | val);
5565d41762aSTakashi Iwai 	}
557a353fbb1STakashi Iwai 	activate_output_path(codec, path, true, true); /* force on */
55809a9ad69STakashi Iwai }
559c577b8a1SJoseph Chan 
560c577b8a1SJoseph Chan static void via_auto_init_multi_out(struct hda_codec *codec)
561c577b8a1SJoseph Chan {
562c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
5633214b966STakashi Iwai 	struct nid_path *path;
564c577b8a1SJoseph Chan 	int i;
565c577b8a1SJoseph Chan 
5663214b966STakashi Iwai 	for (i = 0; i < spec->autocfg.line_outs + spec->smart51_nums; i++) {
5673214b966STakashi Iwai 		path = &spec->out_path[i];
5683214b966STakashi Iwai 		if (!i && spec->aamix_mode && spec->out_mix_path.depth)
5693214b966STakashi Iwai 			path = &spec->out_mix_path;
570a353fbb1STakashi Iwai 		via_auto_init_output(codec, path, PIN_OUT);
5713214b966STakashi Iwai 	}
572c577b8a1SJoseph Chan }
573c577b8a1SJoseph Chan 
574020066d1STakashi Iwai /* deactivate the inactive headphone-paths */
575020066d1STakashi Iwai static void deactivate_hp_paths(struct hda_codec *codec)
576c577b8a1SJoseph Chan {
577c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
5783214b966STakashi Iwai 	int shared = spec->hp_indep_shared;
579c577b8a1SJoseph Chan 
58009a9ad69STakashi Iwai 	if (spec->hp_independent_mode) {
58109a9ad69STakashi Iwai 		activate_output_path(codec, &spec->hp_path, false, false);
5823214b966STakashi Iwai 		activate_output_path(codec, &spec->hp_mix_path, false, false);
5833214b966STakashi Iwai 		if (shared)
5843214b966STakashi Iwai 			activate_output_path(codec, &spec->out_path[shared],
5853214b966STakashi Iwai 					     false, false);
586020066d1STakashi Iwai 	} else if (spec->aamix_mode || !spec->hp_path.depth) {
587020066d1STakashi Iwai 		activate_output_path(codec, &spec->hp_indep_path, false, false);
5883214b966STakashi Iwai 		activate_output_path(codec, &spec->hp_path, false, false);
5893214b966STakashi Iwai 	} else {
590020066d1STakashi Iwai 		activate_output_path(codec, &spec->hp_indep_path, false, false);
5913214b966STakashi Iwai 		activate_output_path(codec, &spec->hp_mix_path, false, false);
59209a9ad69STakashi Iwai 	}
59325eaba2fSLydia Wang }
594c577b8a1SJoseph Chan 
595020066d1STakashi Iwai static void via_auto_init_hp_out(struct hda_codec *codec)
596020066d1STakashi Iwai {
597020066d1STakashi Iwai 	struct via_spec *spec = codec->spec;
598020066d1STakashi Iwai 
599020066d1STakashi Iwai 	if (!spec->hp_path.depth) {
600a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
601020066d1STakashi Iwai 		return;
602020066d1STakashi Iwai 	}
603020066d1STakashi Iwai 	deactivate_hp_paths(codec);
604020066d1STakashi Iwai 	if (spec->hp_independent_mode)
605a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->hp_indep_path, PIN_HP);
606020066d1STakashi Iwai 	else if (spec->aamix_mode)
607a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
608020066d1STakashi Iwai 	else
609a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->hp_path, PIN_HP);
610020066d1STakashi Iwai }
611020066d1STakashi Iwai 
6124a918ffeSTakashi Iwai static void via_auto_init_speaker_out(struct hda_codec *codec)
6134a918ffeSTakashi Iwai {
6144a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
6154a918ffeSTakashi Iwai 
6163214b966STakashi Iwai 	if (!spec->autocfg.speaker_outs)
6173214b966STakashi Iwai 		return;
6183214b966STakashi Iwai 	if (!spec->speaker_path.depth) {
619a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
6203214b966STakashi Iwai 		return;
6213214b966STakashi Iwai 	}
6223214b966STakashi Iwai 	if (!spec->aamix_mode) {
6233214b966STakashi Iwai 		activate_output_path(codec, &spec->speaker_mix_path,
6243214b966STakashi Iwai 				     false, false);
625a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->speaker_path, PIN_OUT);
6263214b966STakashi Iwai 	} else {
6273214b966STakashi Iwai 		activate_output_path(codec, &spec->speaker_path, false, false);
628a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
6293214b966STakashi Iwai 	}
6304a918ffeSTakashi Iwai }
6314a918ffeSTakashi Iwai 
632f4a7828bSTakashi Iwai static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin);
6336e969d91STakashi Iwai static void via_hp_automute(struct hda_codec *codec);
63432e0191dSClemens Ladisch 
635c577b8a1SJoseph Chan static void via_auto_init_analog_input(struct hda_codec *codec)
636c577b8a1SJoseph Chan {
637c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
6387b315bb4STakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
639096a8854STakashi Iwai 	hda_nid_t conn[HDA_MAX_CONNECTIONS];
64032e0191dSClemens Ladisch 	unsigned int ctl;
641096a8854STakashi Iwai 	int i, num_conns;
642c577b8a1SJoseph Chan 
643096a8854STakashi Iwai 	/* init ADCs */
644096a8854STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
645096a8854STakashi Iwai 		snd_hda_codec_write(codec, spec->adc_nids[i], 0,
646096a8854STakashi Iwai 				    AC_VERB_SET_AMP_GAIN_MUTE,
647096a8854STakashi Iwai 				    AMP_IN_UNMUTE(0));
648096a8854STakashi Iwai 	}
649096a8854STakashi Iwai 
650096a8854STakashi Iwai 	/* init pins */
6517b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
6527b315bb4STakashi Iwai 		hda_nid_t nid = cfg->inputs[i].pin;
653f4a7828bSTakashi Iwai 		if (spec->smart51_enabled && is_smart51_pins(codec, nid))
65432e0191dSClemens Ladisch 			ctl = PIN_OUT;
65530649676SDavid Henningsson 		else if (cfg->inputs[i].type == AUTO_PIN_MIC)
65632e0191dSClemens Ladisch 			ctl = PIN_VREF50;
65732e0191dSClemens Ladisch 		else
65832e0191dSClemens Ladisch 			ctl = PIN_IN;
659c577b8a1SJoseph Chan 		snd_hda_codec_write(codec, nid, 0,
66032e0191dSClemens Ladisch 				    AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
661c577b8a1SJoseph Chan 	}
662096a8854STakashi Iwai 
663096a8854STakashi Iwai 	/* init input-src */
664096a8854STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
665a86a88eaSTakashi Iwai 		int adc_idx = spec->inputs[spec->cur_mux[i]].adc_idx;
666a86a88eaSTakashi Iwai 		if (spec->mux_nids[adc_idx]) {
667a86a88eaSTakashi Iwai 			int mux_idx = spec->inputs[spec->cur_mux[i]].mux_idx;
668a86a88eaSTakashi Iwai 			snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
669096a8854STakashi Iwai 					    AC_VERB_SET_CONNECT_SEL,
670a86a88eaSTakashi Iwai 					    mux_idx);
671a86a88eaSTakashi Iwai 		}
672a86a88eaSTakashi Iwai 		if (spec->dyn_adc_switch)
673a86a88eaSTakashi Iwai 			break; /* only one input-src */
674096a8854STakashi Iwai 	}
675096a8854STakashi Iwai 
676096a8854STakashi Iwai 	/* init aa-mixer */
677096a8854STakashi Iwai 	if (!spec->aa_mix_nid)
678096a8854STakashi Iwai 		return;
679096a8854STakashi Iwai 	num_conns = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
680096a8854STakashi Iwai 					    ARRAY_SIZE(conn));
681096a8854STakashi Iwai 	for (i = 0; i < num_conns; i++) {
682096a8854STakashi Iwai 		unsigned int caps = get_wcaps(codec, conn[i]);
683096a8854STakashi Iwai 		if (get_wcaps_type(caps) == AC_WID_PIN)
684096a8854STakashi Iwai 			snd_hda_codec_write(codec, spec->aa_mix_nid, 0,
685096a8854STakashi Iwai 					    AC_VERB_SET_AMP_GAIN_MUTE,
686096a8854STakashi Iwai 					    AMP_IN_MUTE(i));
687096a8854STakashi Iwai 	}
688c577b8a1SJoseph Chan }
689f5271101SLydia Wang 
690f5271101SLydia Wang static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
691f5271101SLydia Wang 				unsigned int *affected_parm)
692f5271101SLydia Wang {
693f5271101SLydia Wang 	unsigned parm;
694f5271101SLydia Wang 	unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
695f5271101SLydia Wang 	unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
696f5271101SLydia Wang 		>> AC_DEFCFG_MISC_SHIFT
697f5271101SLydia Wang 		& AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
6981564b287SLydia Wang 	struct via_spec *spec = codec->spec;
69924088a58STakashi Iwai 	unsigned present = 0;
70024088a58STakashi Iwai 
70124088a58STakashi Iwai 	no_presence |= spec->no_pin_power_ctl;
70224088a58STakashi Iwai 	if (!no_presence)
70324088a58STakashi Iwai 		present = snd_hda_jack_detect(codec, nid);
704f4a7828bSTakashi Iwai 	if ((spec->smart51_enabled && is_smart51_pins(codec, nid))
7051564b287SLydia Wang 	    || ((no_presence || present)
7061564b287SLydia Wang 		&& get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
707f5271101SLydia Wang 		*affected_parm = AC_PWRST_D0; /* if it's connected */
708f5271101SLydia Wang 		parm = AC_PWRST_D0;
709f5271101SLydia Wang 	} else
710f5271101SLydia Wang 		parm = AC_PWRST_D3;
711f5271101SLydia Wang 
712f5271101SLydia Wang 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
713f5271101SLydia Wang }
714f5271101SLydia Wang 
71524088a58STakashi Iwai static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
71624088a58STakashi Iwai 				  struct snd_ctl_elem_info *uinfo)
71724088a58STakashi Iwai {
71824088a58STakashi Iwai 	static const char * const texts[] = {
71924088a58STakashi Iwai 		"Disabled", "Enabled"
72024088a58STakashi Iwai 	};
72124088a58STakashi Iwai 
72224088a58STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
72324088a58STakashi Iwai 	uinfo->count = 1;
72424088a58STakashi Iwai 	uinfo->value.enumerated.items = 2;
72524088a58STakashi Iwai 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
72624088a58STakashi Iwai 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
72724088a58STakashi Iwai 	strcpy(uinfo->value.enumerated.name,
72824088a58STakashi Iwai 	       texts[uinfo->value.enumerated.item]);
72924088a58STakashi Iwai 	return 0;
73024088a58STakashi Iwai }
73124088a58STakashi Iwai 
73224088a58STakashi Iwai static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
73324088a58STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
73424088a58STakashi Iwai {
73524088a58STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
73624088a58STakashi Iwai 	struct via_spec *spec = codec->spec;
73724088a58STakashi Iwai 	ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
73824088a58STakashi Iwai 	return 0;
73924088a58STakashi Iwai }
74024088a58STakashi Iwai 
74124088a58STakashi Iwai static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
74224088a58STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
74324088a58STakashi Iwai {
74424088a58STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
74524088a58STakashi Iwai 	struct via_spec *spec = codec->spec;
74624088a58STakashi Iwai 	unsigned int val = !ucontrol->value.enumerated.item[0];
74724088a58STakashi Iwai 
74824088a58STakashi Iwai 	if (val == spec->no_pin_power_ctl)
74924088a58STakashi Iwai 		return 0;
75024088a58STakashi Iwai 	spec->no_pin_power_ctl = val;
75124088a58STakashi Iwai 	set_widgets_power_state(codec);
75224088a58STakashi Iwai 	return 1;
75324088a58STakashi Iwai }
75424088a58STakashi Iwai 
75524088a58STakashi Iwai static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
75624088a58STakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
75724088a58STakashi Iwai 	.name = "Dynamic Power-Control",
75824088a58STakashi Iwai 	.info = via_pin_power_ctl_info,
75924088a58STakashi Iwai 	.get = via_pin_power_ctl_get,
76024088a58STakashi Iwai 	.put = via_pin_power_ctl_put,
76124088a58STakashi Iwai };
76224088a58STakashi Iwai 
76324088a58STakashi Iwai 
7640aa62aefSHarald Welte static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
7650aa62aefSHarald Welte 				   struct snd_ctl_elem_info *uinfo)
7660aa62aefSHarald Welte {
7678df2a312STakashi Iwai 	static const char * const texts[] = { "OFF", "ON" };
7688df2a312STakashi Iwai 
7698df2a312STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
7708df2a312STakashi Iwai 	uinfo->count = 1;
7718df2a312STakashi Iwai 	uinfo->value.enumerated.items = 2;
7728df2a312STakashi Iwai 	if (uinfo->value.enumerated.item >= 2)
7738df2a312STakashi Iwai 		uinfo->value.enumerated.item = 1;
7748df2a312STakashi Iwai 	strcpy(uinfo->value.enumerated.name,
7758df2a312STakashi Iwai 	       texts[uinfo->value.enumerated.item]);
7768df2a312STakashi Iwai 	return 0;
7770aa62aefSHarald Welte }
7780aa62aefSHarald Welte 
7790aa62aefSHarald Welte static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
7800aa62aefSHarald Welte 				  struct snd_ctl_elem_value *ucontrol)
7810aa62aefSHarald Welte {
7820aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
783cdc1784dSLydia Wang 	struct via_spec *spec = codec->spec;
784cdc1784dSLydia Wang 
785ece8d043STakashi Iwai 	ucontrol->value.enumerated.item[0] = spec->hp_independent_mode;
786cdc1784dSLydia Wang 	return 0;
787cdc1784dSLydia Wang }
788cdc1784dSLydia Wang 
7893b607e3dSTakashi Iwai /* adjust spec->multiout setup according to the current flags */
7903b607e3dSTakashi Iwai static void setup_playback_multi_pcm(struct via_spec *spec)
7913b607e3dSTakashi Iwai {
7923b607e3dSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
7933b607e3dSTakashi Iwai 	spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
7943b607e3dSTakashi Iwai 	spec->multiout.hp_nid = 0;
7953b607e3dSTakashi Iwai 	if (!spec->hp_independent_mode) {
7963b607e3dSTakashi Iwai 		if (!spec->hp_indep_shared)
7973b607e3dSTakashi Iwai 			spec->multiout.hp_nid = spec->hp_dac_nid;
7983b607e3dSTakashi Iwai 	} else {
7993b607e3dSTakashi Iwai 		if (spec->hp_indep_shared)
8003b607e3dSTakashi Iwai 			spec->multiout.num_dacs = cfg->line_outs - 1;
8013b607e3dSTakashi Iwai 	}
8023b607e3dSTakashi Iwai }
8033b607e3dSTakashi Iwai 
8043b607e3dSTakashi Iwai /* update DAC setups according to indep-HP switch;
8053b607e3dSTakashi Iwai  * this function is called only when indep-HP is modified
8063b607e3dSTakashi Iwai  */
8073b607e3dSTakashi Iwai static void switch_indep_hp_dacs(struct hda_codec *codec)
8083b607e3dSTakashi Iwai {
8093b607e3dSTakashi Iwai 	struct via_spec *spec = codec->spec;
8103b607e3dSTakashi Iwai 	int shared = spec->hp_indep_shared;
8113b607e3dSTakashi Iwai 	hda_nid_t shared_dac, hp_dac;
8123b607e3dSTakashi Iwai 
8133b607e3dSTakashi Iwai 	if (!spec->opened_streams)
8143b607e3dSTakashi Iwai 		return;
8153b607e3dSTakashi Iwai 
8163b607e3dSTakashi Iwai 	shared_dac = shared ? spec->multiout.dac_nids[shared] : 0;
8173b607e3dSTakashi Iwai 	hp_dac = spec->hp_dac_nid;
8183b607e3dSTakashi Iwai 	if (spec->hp_independent_mode) {
8193b607e3dSTakashi Iwai 		/* switch to indep-HP mode */
8203b607e3dSTakashi Iwai 		if (spec->active_streams & STREAM_MULTI_OUT) {
8213b607e3dSTakashi Iwai 			__snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
8223b607e3dSTakashi Iwai 			__snd_hda_codec_cleanup_stream(codec, shared_dac, 1);
8233b607e3dSTakashi Iwai 		}
8243b607e3dSTakashi Iwai 		if (spec->active_streams & STREAM_INDEP_HP)
8253b607e3dSTakashi Iwai 			snd_hda_codec_setup_stream(codec, hp_dac,
8263b607e3dSTakashi Iwai 						   spec->cur_hp_stream_tag, 0,
8273b607e3dSTakashi Iwai 						   spec->cur_hp_format);
8283b607e3dSTakashi Iwai 	} else {
8293b607e3dSTakashi Iwai 		/* back to HP or shared-DAC */
8303b607e3dSTakashi Iwai 		if (spec->active_streams & STREAM_INDEP_HP)
8313b607e3dSTakashi Iwai 			__snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
8323b607e3dSTakashi Iwai 		if (spec->active_streams & STREAM_MULTI_OUT) {
8333b607e3dSTakashi Iwai 			hda_nid_t dac;
8343b607e3dSTakashi Iwai 			int ch;
8353b607e3dSTakashi Iwai 			if (shared_dac) { /* reset mutli-ch DAC */
8363b607e3dSTakashi Iwai 				dac = shared_dac;
8373b607e3dSTakashi Iwai 				ch = shared * 2;
8383b607e3dSTakashi Iwai 			} else { /* reset HP DAC */
8393b607e3dSTakashi Iwai 				dac = hp_dac;
8403b607e3dSTakashi Iwai 				ch = 0;
8413b607e3dSTakashi Iwai 			}
8423b607e3dSTakashi Iwai 			snd_hda_codec_setup_stream(codec, dac,
8433b607e3dSTakashi Iwai 						   spec->cur_dac_stream_tag, ch,
8443b607e3dSTakashi Iwai 						   spec->cur_dac_format);
8453b607e3dSTakashi Iwai 		}
8463b607e3dSTakashi Iwai 	}
8473b607e3dSTakashi Iwai 	setup_playback_multi_pcm(spec);
8483b607e3dSTakashi Iwai }
8493b607e3dSTakashi Iwai 
8500aa62aefSHarald Welte static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
8510aa62aefSHarald Welte 				  struct snd_ctl_elem_value *ucontrol)
8520aa62aefSHarald Welte {
8530aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
8540aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
8553214b966STakashi Iwai 	int cur, shared;
8568df2a312STakashi Iwai 
8573b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
85825250505STakashi Iwai 	cur = !!ucontrol->value.enumerated.item[0];
8593b607e3dSTakashi Iwai 	if (spec->hp_independent_mode == cur) {
8603b607e3dSTakashi Iwai 		mutex_unlock(&spec->config_mutex);
86125250505STakashi Iwai 		return 0;
8623b607e3dSTakashi Iwai 	}
86325250505STakashi Iwai 	spec->hp_independent_mode = cur;
8643214b966STakashi Iwai 	shared = spec->hp_indep_shared;
865020066d1STakashi Iwai 	deactivate_hp_paths(codec);
866020066d1STakashi Iwai 	if (cur)
867020066d1STakashi Iwai 		activate_output_path(codec, &spec->hp_indep_path, true, false);
868020066d1STakashi Iwai 	else {
8693214b966STakashi Iwai 		if (shared)
8703214b966STakashi Iwai 			activate_output_path(codec, &spec->out_path[shared],
87125250505STakashi Iwai 					     true, false);
872020066d1STakashi Iwai 		if (spec->aamix_mode || !spec->hp_path.depth)
873020066d1STakashi Iwai 			activate_output_path(codec, &spec->hp_mix_path,
874020066d1STakashi Iwai 					     true, false);
875020066d1STakashi Iwai 		else
876020066d1STakashi Iwai 			activate_output_path(codec, &spec->hp_path,
877020066d1STakashi Iwai 					     true, false);
8788df2a312STakashi Iwai 	}
8790aa62aefSHarald Welte 
8803b607e3dSTakashi Iwai 	switch_indep_hp_dacs(codec);
8813b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
8823b607e3dSTakashi Iwai 
883ce0e5a9eSLydia Wang 	/* update jack power state */
8843e95b9abSLydia Wang 	set_widgets_power_state(codec);
8856e969d91STakashi Iwai 	via_hp_automute(codec);
88625250505STakashi Iwai 	return 1;
8870aa62aefSHarald Welte }
8880aa62aefSHarald Welte 
889ece8d043STakashi Iwai static const struct snd_kcontrol_new via_hp_mixer = {
8900aa62aefSHarald Welte 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
8910aa62aefSHarald Welte 	.name = "Independent HP",
8920aa62aefSHarald Welte 	.info = via_independent_hp_info,
8930aa62aefSHarald Welte 	.get = via_independent_hp_get,
8940aa62aefSHarald Welte 	.put = via_independent_hp_put,
8950aa62aefSHarald Welte };
8960aa62aefSHarald Welte 
8973d83e577STakashi Iwai static int via_hp_build(struct hda_codec *codec)
8985b0cb1d8SJaroslav Kysela {
8993d83e577STakashi Iwai 	struct via_spec *spec = codec->spec;
9005b0cb1d8SJaroslav Kysela 	struct snd_kcontrol_new *knew;
9015b0cb1d8SJaroslav Kysela 	hda_nid_t nid;
9025b0cb1d8SJaroslav Kysela 
9035b0cb1d8SJaroslav Kysela 	nid = spec->autocfg.hp_pins[0];
904ece8d043STakashi Iwai 	knew = via_clone_control(spec, &via_hp_mixer);
9053d83e577STakashi Iwai 	if (knew == NULL)
9063d83e577STakashi Iwai 		return -ENOMEM;
9073d83e577STakashi Iwai 
9085b0cb1d8SJaroslav Kysela 	knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
9095b0cb1d8SJaroslav Kysela 
9105b0cb1d8SJaroslav Kysela 	return 0;
9115b0cb1d8SJaroslav Kysela }
9125b0cb1d8SJaroslav Kysela 
9131564b287SLydia Wang static void notify_aa_path_ctls(struct hda_codec *codec)
9141564b287SLydia Wang {
915e3d7a143STakashi Iwai 	struct via_spec *spec = codec->spec;
9161564b287SLydia Wang 	int i;
9171564b287SLydia Wang 
918e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
919e3d7a143STakashi Iwai 		struct snd_kcontrol *ctl;
920e3d7a143STakashi Iwai 		struct snd_ctl_elem_id id;
9211564b287SLydia Wang 		memset(&id, 0, sizeof(id));
9221564b287SLydia Wang 		id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
923e3d7a143STakashi Iwai 		sprintf(id.name, "%s Playback Volume", spec->smart51_labels[i]);
924525566cbSLydia Wang 		ctl = snd_hda_find_mixer_ctl(codec, id.name);
925525566cbSLydia Wang 		if (ctl)
926525566cbSLydia Wang 			snd_ctl_notify(codec->bus->card,
927525566cbSLydia Wang 					SNDRV_CTL_EVENT_MASK_VALUE,
928525566cbSLydia Wang 					&ctl->id);
9291564b287SLydia Wang 	}
9301564b287SLydia Wang }
9311564b287SLydia Wang 
9321564b287SLydia Wang static void mute_aa_path(struct hda_codec *codec, int mute)
9331564b287SLydia Wang {
9341564b287SLydia Wang 	struct via_spec *spec = codec->spec;
9351564b287SLydia Wang 	int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
936e3d7a143STakashi Iwai 	int i;
937e3d7a143STakashi Iwai 
938e3d7a143STakashi Iwai 	/* check AA path's mute status */
939e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
940e3d7a143STakashi Iwai 		if (spec->smart51_idxs[i] < 0)
941e3d7a143STakashi Iwai 			continue;
942e3d7a143STakashi Iwai 		snd_hda_codec_amp_stereo(codec, spec->aa_mix_nid,
943e3d7a143STakashi Iwai 					 HDA_INPUT, spec->smart51_idxs[i],
9441564b287SLydia Wang 					 HDA_AMP_MUTE, val);
9451564b287SLydia Wang 	}
9461564b287SLydia Wang }
947f4a7828bSTakashi Iwai 
948e3d7a143STakashi Iwai static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
949e3d7a143STakashi Iwai {
950e3d7a143STakashi Iwai 	struct via_spec *spec = codec->spec;
951e3d7a143STakashi Iwai 	int i;
952e3d7a143STakashi Iwai 
953e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++)
954e3d7a143STakashi Iwai 		if (spec->smart51_pins[i] == pin)
955e3d7a143STakashi Iwai 			return true;
956e3d7a143STakashi Iwai 	return false;
957e3d7a143STakashi Iwai }
958e3d7a143STakashi Iwai 
9591564b287SLydia Wang static int via_smart51_get(struct snd_kcontrol *kcontrol,
9601564b287SLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
9611564b287SLydia Wang {
9621564b287SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
9631564b287SLydia Wang 	struct via_spec *spec = codec->spec;
9641564b287SLydia Wang 
965f2b1c9f0STakashi Iwai 	*ucontrol->value.integer.value = spec->smart51_enabled;
9661564b287SLydia Wang 	return 0;
9671564b287SLydia Wang }
9681564b287SLydia Wang 
9691564b287SLydia Wang static int via_smart51_put(struct snd_kcontrol *kcontrol,
9701564b287SLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
9711564b287SLydia Wang {
9721564b287SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
9731564b287SLydia Wang 	struct via_spec *spec = codec->spec;
9741564b287SLydia Wang 	int out_in = *ucontrol->value.integer.value
9751564b287SLydia Wang 		? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
9761564b287SLydia Wang 	int i;
9771564b287SLydia Wang 
978e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
979e3d7a143STakashi Iwai 		hda_nid_t nid = spec->smart51_pins[i];
9807b315bb4STakashi Iwai 		unsigned int parm;
9817b315bb4STakashi Iwai 
9827b315bb4STakashi Iwai 		parm = snd_hda_codec_read(codec, nid, 0,
9831564b287SLydia Wang 					  AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
9841564b287SLydia Wang 		parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
9851564b287SLydia Wang 		parm |= out_in;
9861564b287SLydia Wang 		snd_hda_codec_write(codec, nid, 0,
9871564b287SLydia Wang 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
9881564b287SLydia Wang 				    parm);
9891564b287SLydia Wang 		if (out_in == AC_PINCTL_OUT_EN) {
9901564b287SLydia Wang 			mute_aa_path(codec, 1);
9911564b287SLydia Wang 			notify_aa_path_ctls(codec);
9921564b287SLydia Wang 		}
9931564b287SLydia Wang 	}
9941564b287SLydia Wang 	spec->smart51_enabled = *ucontrol->value.integer.value;
9953e95b9abSLydia Wang 	set_widgets_power_state(codec);
9961564b287SLydia Wang 	return 1;
9971564b287SLydia Wang }
9981564b287SLydia Wang 
9995f4b36d6STakashi Iwai static const struct snd_kcontrol_new via_smart51_mixer = {
10001564b287SLydia Wang 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
10011564b287SLydia Wang 	.name = "Smart 5.1",
10021564b287SLydia Wang 	.count = 1,
1003f2b1c9f0STakashi Iwai 	.info = snd_ctl_boolean_mono_info,
10041564b287SLydia Wang 	.get = via_smart51_get,
10051564b287SLydia Wang 	.put = via_smart51_put,
10061564b287SLydia Wang };
10071564b287SLydia Wang 
1008f4a7828bSTakashi Iwai static int via_smart51_build(struct hda_codec *codec)
10095b0cb1d8SJaroslav Kysela {
1010f4a7828bSTakashi Iwai 	struct via_spec *spec = codec->spec;
10115b0cb1d8SJaroslav Kysela 
1012e3d7a143STakashi Iwai 	if (!spec->smart51_nums)
1013cb34c207SLydia Wang 		return 0;
1014e3d7a143STakashi Iwai 	if (!via_clone_control(spec, &via_smart51_mixer))
10155b0cb1d8SJaroslav Kysela 		return -ENOMEM;
10165b0cb1d8SJaroslav Kysela 	return 0;
10175b0cb1d8SJaroslav Kysela }
10185b0cb1d8SJaroslav Kysela 
1019f5271101SLydia Wang /* check AA path's mute status */
1020ada509ecSTakashi Iwai static bool is_aa_path_mute(struct hda_codec *codec)
1021ada509ecSTakashi Iwai {
1022ada509ecSTakashi Iwai 	struct via_spec *spec = codec->spec;
1023ada509ecSTakashi Iwai 	const struct hda_amp_list *p;
1024ada509ecSTakashi Iwai 	int i, ch, v;
1025ada509ecSTakashi Iwai 
1026ada509ecSTakashi Iwai 	for (i = 0; i < spec->num_loopbacks; i++) {
1027ada509ecSTakashi Iwai 		p = &spec->loopback_list[i];
1028ada509ecSTakashi Iwai 		for (ch = 0; ch < 2; ch++) {
1029ada509ecSTakashi Iwai 			v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
1030ada509ecSTakashi Iwai 						   p->idx);
1031ada509ecSTakashi Iwai 			if (!(v & HDA_AMP_MUTE) && v > 0)
1032ada509ecSTakashi Iwai 				return false;
1033f5271101SLydia Wang 		}
1034f5271101SLydia Wang 	}
1035ada509ecSTakashi Iwai 	return true;
1036f5271101SLydia Wang }
1037f5271101SLydia Wang 
1038f5271101SLydia Wang /* enter/exit analog low-current mode */
1039ada509ecSTakashi Iwai static void analog_low_current_mode(struct hda_codec *codec)
1040f5271101SLydia Wang {
1041f5271101SLydia Wang 	struct via_spec *spec = codec->spec;
1042ada509ecSTakashi Iwai 	bool enable;
1043ada509ecSTakashi Iwai 	unsigned int verb, parm;
1044f5271101SLydia Wang 
10453b607e3dSTakashi Iwai 	enable = is_aa_path_mute(codec) && (spec->opened_streams != 0);
1046f5271101SLydia Wang 
1047f5271101SLydia Wang 	/* decide low current mode's verb & parameter */
1048f5271101SLydia Wang 	switch (spec->codec_type) {
1049f5271101SLydia Wang 	case VT1708B_8CH:
1050f5271101SLydia Wang 	case VT1708B_4CH:
1051f5271101SLydia Wang 		verb = 0xf70;
1052f5271101SLydia Wang 		parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
1053f5271101SLydia Wang 		break;
1054f5271101SLydia Wang 	case VT1708S:
1055eb7188caSLydia Wang 	case VT1718S:
1056f3db423dSLydia Wang 	case VT1716S:
1057f5271101SLydia Wang 		verb = 0xf73;
1058f5271101SLydia Wang 		parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
1059f5271101SLydia Wang 		break;
1060f5271101SLydia Wang 	case VT1702:
1061f5271101SLydia Wang 		verb = 0xf73;
1062f5271101SLydia Wang 		parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
1063f5271101SLydia Wang 		break;
106425eaba2fSLydia Wang 	case VT2002P:
1065ab6734e7SLydia Wang 	case VT1812:
106611890956SLydia Wang 	case VT1802:
106725eaba2fSLydia Wang 		verb = 0xf93;
106825eaba2fSLydia Wang 		parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
106925eaba2fSLydia Wang 		break;
1070f5271101SLydia Wang 	default:
1071f5271101SLydia Wang 		return;		/* other codecs are not supported */
1072f5271101SLydia Wang 	}
1073f5271101SLydia Wang 	/* send verb */
1074f5271101SLydia Wang 	snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
1075f5271101SLydia Wang }
1076f5271101SLydia Wang 
1077c577b8a1SJoseph Chan /*
1078c577b8a1SJoseph Chan  * generic initialization of ADC, input mixers and output mixers
1079c577b8a1SJoseph Chan  */
1080096a8854STakashi Iwai static const struct hda_verb vt1708_init_verbs[] = {
1081aa266fccSLydia Wang 	/* power down jack detect function */
1082aa266fccSLydia Wang 	{0x1, 0xf81, 0x1},
1083f7278fd0SJosepch Chan 	{ }
1084c577b8a1SJoseph Chan };
1085c577b8a1SJoseph Chan 
10863b607e3dSTakashi Iwai static void set_stream_open(struct hda_codec *codec, int bit, bool active)
10877eb56e84STakashi Iwai {
1088ada509ecSTakashi Iwai 	struct via_spec *spec = codec->spec;
1089ada509ecSTakashi Iwai 
1090ada509ecSTakashi Iwai 	if (active)
10913b607e3dSTakashi Iwai 		spec->opened_streams |= bit;
1092ada509ecSTakashi Iwai 	else
10933b607e3dSTakashi Iwai 		spec->opened_streams &= ~bit;
1094ada509ecSTakashi Iwai 	analog_low_current_mode(codec);
10957eb56e84STakashi Iwai }
10967eb56e84STakashi Iwai 
1097ece8d043STakashi Iwai static int via_playback_multi_pcm_open(struct hda_pcm_stream *hinfo,
1098c577b8a1SJoseph Chan 				 struct hda_codec *codec,
1099c577b8a1SJoseph Chan 				 struct snd_pcm_substream *substream)
1100c577b8a1SJoseph Chan {
1101c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
110225250505STakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
1103ada509ecSTakashi Iwai 	int err;
1104ece8d043STakashi Iwai 
110525250505STakashi Iwai 	spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
110625250505STakashi Iwai 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
11073b607e3dSTakashi Iwai 	set_stream_open(codec, STREAM_MULTI_OUT, true);
1108ada509ecSTakashi Iwai 	err = snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
11099a08160bSTakashi Iwai 					    hinfo);
1110ada509ecSTakashi Iwai 	if (err < 0) {
11113b607e3dSTakashi Iwai 		set_stream_open(codec, STREAM_MULTI_OUT, false);
1112ada509ecSTakashi Iwai 		return err;
1113ada509ecSTakashi Iwai 	}
1114ada509ecSTakashi Iwai 	return 0;
1115c577b8a1SJoseph Chan }
1116c577b8a1SJoseph Chan 
1117ece8d043STakashi Iwai static int via_playback_multi_pcm_close(struct hda_pcm_stream *hinfo,
11189af74210STakashi Iwai 				  struct hda_codec *codec,
11199af74210STakashi Iwai 				  struct snd_pcm_substream *substream)
11209af74210STakashi Iwai {
11213b607e3dSTakashi Iwai 	set_stream_open(codec, STREAM_MULTI_OUT, false);
11229af74210STakashi Iwai 	return 0;
11239af74210STakashi Iwai }
11249af74210STakashi Iwai 
11257eb56e84STakashi Iwai static int via_playback_hp_pcm_open(struct hda_pcm_stream *hinfo,
11267eb56e84STakashi Iwai 				    struct hda_codec *codec,
11277eb56e84STakashi Iwai 				    struct snd_pcm_substream *substream)
11287eb56e84STakashi Iwai {
11297eb56e84STakashi Iwai 	struct via_spec *spec = codec->spec;
11307eb56e84STakashi Iwai 
1131ece8d043STakashi Iwai 	if (snd_BUG_ON(!spec->hp_dac_nid))
11327eb56e84STakashi Iwai 		return -EINVAL;
11333b607e3dSTakashi Iwai 	set_stream_open(codec, STREAM_INDEP_HP, true);
1134ece8d043STakashi Iwai 	return 0;
1135ece8d043STakashi Iwai }
1136ece8d043STakashi Iwai 
1137ece8d043STakashi Iwai static int via_playback_hp_pcm_close(struct hda_pcm_stream *hinfo,
1138ece8d043STakashi Iwai 				     struct hda_codec *codec,
1139ece8d043STakashi Iwai 				     struct snd_pcm_substream *substream)
1140ece8d043STakashi Iwai {
11413b607e3dSTakashi Iwai 	set_stream_open(codec, STREAM_INDEP_HP, false);
11427eb56e84STakashi Iwai 	return 0;
11437eb56e84STakashi Iwai }
11447eb56e84STakashi Iwai 
11457eb56e84STakashi Iwai static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
11467eb56e84STakashi Iwai 					  struct hda_codec *codec,
11470aa62aefSHarald Welte 					  unsigned int stream_tag,
11480aa62aefSHarald Welte 					  unsigned int format,
11490aa62aefSHarald Welte 					  struct snd_pcm_substream *substream)
11500aa62aefSHarald Welte {
11510aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
11520aa62aefSHarald Welte 
11533b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
11543b607e3dSTakashi Iwai 	setup_playback_multi_pcm(spec);
1155ece8d043STakashi Iwai 	snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
1156ece8d043STakashi Iwai 					 format, substream);
11573b607e3dSTakashi Iwai 	/* remember for dynamic DAC switch with indep-HP */
11583b607e3dSTakashi Iwai 	spec->active_streams |= STREAM_MULTI_OUT;
11593b607e3dSTakashi Iwai 	spec->cur_dac_stream_tag = stream_tag;
11603b607e3dSTakashi Iwai 	spec->cur_dac_format = format;
11613b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1162187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
11637eb56e84STakashi Iwai 	return 0;
11640aa62aefSHarald Welte }
11650aa62aefSHarald Welte 
11667eb56e84STakashi Iwai static int via_playback_hp_pcm_prepare(struct hda_pcm_stream *hinfo,
11670aa62aefSHarald Welte 				       struct hda_codec *codec,
11680aa62aefSHarald Welte 				       unsigned int stream_tag,
11690aa62aefSHarald Welte 				       unsigned int format,
11700aa62aefSHarald Welte 				       struct snd_pcm_substream *substream)
11710aa62aefSHarald Welte {
11720aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
11730aa62aefSHarald Welte 
11743b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
11753b607e3dSTakashi Iwai 	if (spec->hp_independent_mode)
1176ece8d043STakashi Iwai 		snd_hda_codec_setup_stream(codec, spec->hp_dac_nid,
1177ece8d043STakashi Iwai 					   stream_tag, 0, format);
11783b607e3dSTakashi Iwai 	spec->active_streams |= STREAM_INDEP_HP;
11793b607e3dSTakashi Iwai 	spec->cur_hp_stream_tag = stream_tag;
11803b607e3dSTakashi Iwai 	spec->cur_hp_format = format;
11813b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1182187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
11830aa62aefSHarald Welte 	return 0;
11840aa62aefSHarald Welte }
11850aa62aefSHarald Welte 
11860aa62aefSHarald Welte static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
11870aa62aefSHarald Welte 				    struct hda_codec *codec,
11880aa62aefSHarald Welte 				    struct snd_pcm_substream *substream)
11890aa62aefSHarald Welte {
11900aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
11910aa62aefSHarald Welte 
11923b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
1193ece8d043STakashi Iwai 	snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
11943b607e3dSTakashi Iwai 	spec->active_streams &= ~STREAM_MULTI_OUT;
11953b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1196187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
11977eb56e84STakashi Iwai 	return 0;
11980aa62aefSHarald Welte }
11997eb56e84STakashi Iwai 
12007eb56e84STakashi Iwai static int via_playback_hp_pcm_cleanup(struct hda_pcm_stream *hinfo,
12017eb56e84STakashi Iwai 				       struct hda_codec *codec,
12027eb56e84STakashi Iwai 				       struct snd_pcm_substream *substream)
12037eb56e84STakashi Iwai {
12047eb56e84STakashi Iwai 	struct via_spec *spec = codec->spec;
12057eb56e84STakashi Iwai 
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, 0, 0, 0);
12093b607e3dSTakashi Iwai 	spec->active_streams &= ~STREAM_INDEP_HP;
12103b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1211187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
12120aa62aefSHarald Welte 	return 0;
12130aa62aefSHarald Welte }
12140aa62aefSHarald Welte 
1215c577b8a1SJoseph Chan /*
1216c577b8a1SJoseph Chan  * Digital out
1217c577b8a1SJoseph Chan  */
1218c577b8a1SJoseph Chan static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1219c577b8a1SJoseph Chan 				     struct hda_codec *codec,
1220c577b8a1SJoseph Chan 				     struct snd_pcm_substream *substream)
1221c577b8a1SJoseph Chan {
1222c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1223c577b8a1SJoseph Chan 	return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1224c577b8a1SJoseph Chan }
1225c577b8a1SJoseph Chan 
1226c577b8a1SJoseph Chan static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1227c577b8a1SJoseph Chan 				      struct hda_codec *codec,
1228c577b8a1SJoseph Chan 				      struct snd_pcm_substream *substream)
1229c577b8a1SJoseph Chan {
1230c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1231c577b8a1SJoseph Chan 	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1232c577b8a1SJoseph Chan }
1233c577b8a1SJoseph Chan 
12345691ec7fSHarald Welte static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
123598aa34c0SHarald Welte 					struct hda_codec *codec,
123698aa34c0SHarald Welte 					unsigned int stream_tag,
123798aa34c0SHarald Welte 					unsigned int format,
123898aa34c0SHarald Welte 					struct snd_pcm_substream *substream)
123998aa34c0SHarald Welte {
124098aa34c0SHarald Welte 	struct via_spec *spec = codec->spec;
12419da29271STakashi Iwai 	return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
12429da29271STakashi Iwai 					     stream_tag, format, substream);
12439da29271STakashi Iwai }
12445691ec7fSHarald Welte 
12459da29271STakashi Iwai static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
12469da29271STakashi Iwai 					struct hda_codec *codec,
12479da29271STakashi Iwai 					struct snd_pcm_substream *substream)
12489da29271STakashi Iwai {
12499da29271STakashi Iwai 	struct via_spec *spec = codec->spec;
12509da29271STakashi Iwai 	snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
125198aa34c0SHarald Welte 	return 0;
125298aa34c0SHarald Welte }
125398aa34c0SHarald Welte 
1254c577b8a1SJoseph Chan /*
1255c577b8a1SJoseph Chan  * Analog capture
1256c577b8a1SJoseph Chan  */
1257c577b8a1SJoseph Chan static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1258c577b8a1SJoseph Chan 				   struct hda_codec *codec,
1259c577b8a1SJoseph Chan 				   unsigned int stream_tag,
1260c577b8a1SJoseph Chan 				   unsigned int format,
1261c577b8a1SJoseph Chan 				   struct snd_pcm_substream *substream)
1262c577b8a1SJoseph Chan {
1263c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1264c577b8a1SJoseph Chan 
1265c577b8a1SJoseph Chan 	snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1266c577b8a1SJoseph Chan 				   stream_tag, 0, format);
1267c577b8a1SJoseph Chan 	return 0;
1268c577b8a1SJoseph Chan }
1269c577b8a1SJoseph Chan 
1270c577b8a1SJoseph Chan static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1271c577b8a1SJoseph Chan 				   struct hda_codec *codec,
1272c577b8a1SJoseph Chan 				   struct snd_pcm_substream *substream)
1273c577b8a1SJoseph Chan {
1274c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1275888afa15STakashi Iwai 	snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
1276c577b8a1SJoseph Chan 	return 0;
1277c577b8a1SJoseph Chan }
1278c577b8a1SJoseph Chan 
1279a86a88eaSTakashi Iwai /* analog capture with dynamic ADC switching */
1280a86a88eaSTakashi Iwai static int via_dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1281a86a88eaSTakashi Iwai 					   struct hda_codec *codec,
1282a86a88eaSTakashi Iwai 					   unsigned int stream_tag,
1283a86a88eaSTakashi Iwai 					   unsigned int format,
1284a86a88eaSTakashi Iwai 					   struct snd_pcm_substream *substream)
1285a86a88eaSTakashi Iwai {
1286a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1287a86a88eaSTakashi Iwai 	int adc_idx = spec->inputs[spec->cur_mux[0]].adc_idx;
1288a86a88eaSTakashi Iwai 
12893b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
1290a86a88eaSTakashi Iwai 	spec->cur_adc = spec->adc_nids[adc_idx];
1291a86a88eaSTakashi Iwai 	spec->cur_adc_stream_tag = stream_tag;
1292a86a88eaSTakashi Iwai 	spec->cur_adc_format = format;
1293a86a88eaSTakashi Iwai 	snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
12943b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1295a86a88eaSTakashi Iwai 	return 0;
1296a86a88eaSTakashi Iwai }
1297a86a88eaSTakashi Iwai 
1298a86a88eaSTakashi Iwai static int via_dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1299a86a88eaSTakashi Iwai 					   struct hda_codec *codec,
1300a86a88eaSTakashi Iwai 					   struct snd_pcm_substream *substream)
1301a86a88eaSTakashi Iwai {
1302a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1303a86a88eaSTakashi Iwai 
13043b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
1305a86a88eaSTakashi Iwai 	snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1306a86a88eaSTakashi Iwai 	spec->cur_adc = 0;
13073b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1308a86a88eaSTakashi Iwai 	return 0;
1309a86a88eaSTakashi Iwai }
1310a86a88eaSTakashi Iwai 
1311a86a88eaSTakashi Iwai /* re-setup the stream if running; called from input-src put */
1312a86a88eaSTakashi Iwai static bool via_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
1313a86a88eaSTakashi Iwai {
1314a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1315a86a88eaSTakashi Iwai 	int adc_idx = spec->inputs[cur].adc_idx;
1316a86a88eaSTakashi Iwai 	hda_nid_t adc = spec->adc_nids[adc_idx];
13173b607e3dSTakashi Iwai 	bool ret = false;
1318a86a88eaSTakashi Iwai 
13193b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
1320a86a88eaSTakashi Iwai 	if (spec->cur_adc && spec->cur_adc != adc) {
1321a86a88eaSTakashi Iwai 		/* stream is running, let's swap the current ADC */
1322a86a88eaSTakashi Iwai 		__snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1323a86a88eaSTakashi Iwai 		spec->cur_adc = adc;
1324a86a88eaSTakashi Iwai 		snd_hda_codec_setup_stream(codec, adc,
1325a86a88eaSTakashi Iwai 					   spec->cur_adc_stream_tag, 0,
1326a86a88eaSTakashi Iwai 					   spec->cur_adc_format);
13273b607e3dSTakashi Iwai 		ret = true;
1328a86a88eaSTakashi Iwai 	}
13293b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
13303b607e3dSTakashi Iwai 	return ret;
1331a86a88eaSTakashi Iwai }
1332a86a88eaSTakashi Iwai 
13339af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_analog_playback = {
13347eb56e84STakashi Iwai 	.substreams = 1,
1335c577b8a1SJoseph Chan 	.channels_min = 2,
1336c577b8a1SJoseph Chan 	.channels_max = 8,
13379af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1338c577b8a1SJoseph Chan 	.ops = {
1339ece8d043STakashi Iwai 		.open = via_playback_multi_pcm_open,
1340ece8d043STakashi Iwai 		.close = via_playback_multi_pcm_close,
13410aa62aefSHarald Welte 		.prepare = via_playback_multi_pcm_prepare,
13420aa62aefSHarald Welte 		.cleanup = via_playback_multi_pcm_cleanup
1343c577b8a1SJoseph Chan 	},
1344c577b8a1SJoseph Chan };
1345c577b8a1SJoseph Chan 
13467eb56e84STakashi Iwai static const struct hda_pcm_stream via_pcm_hp_playback = {
13477eb56e84STakashi Iwai 	.substreams = 1,
13487eb56e84STakashi Iwai 	.channels_min = 2,
13497eb56e84STakashi Iwai 	.channels_max = 2,
13507eb56e84STakashi Iwai 	/* NID is set in via_build_pcms */
13517eb56e84STakashi Iwai 	.ops = {
13527eb56e84STakashi Iwai 		.open = via_playback_hp_pcm_open,
1353ece8d043STakashi Iwai 		.close = via_playback_hp_pcm_close,
13547eb56e84STakashi Iwai 		.prepare = via_playback_hp_pcm_prepare,
13557eb56e84STakashi Iwai 		.cleanup = via_playback_hp_pcm_cleanup
13567eb56e84STakashi Iwai 	},
13577eb56e84STakashi Iwai };
13587eb56e84STakashi Iwai 
135990dd48a1STakashi Iwai static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
13607eb56e84STakashi Iwai 	.substreams = 1,
1361bc9b5623STakashi Iwai 	.channels_min = 2,
1362bc9b5623STakashi Iwai 	.channels_max = 8,
13639af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1364bc9b5623STakashi Iwai 	/* We got noisy outputs on the right channel on VT1708 when
1365bc9b5623STakashi Iwai 	 * 24bit samples are used.  Until any workaround is found,
1366bc9b5623STakashi Iwai 	 * disable the 24bit format, so far.
1367bc9b5623STakashi Iwai 	 */
1368bc9b5623STakashi Iwai 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
1369bc9b5623STakashi Iwai 	.ops = {
1370ece8d043STakashi Iwai 		.open = via_playback_multi_pcm_open,
1371ece8d043STakashi Iwai 		.close = via_playback_multi_pcm_close,
1372c873cc25SLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
1373c873cc25SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup
1374bc9b5623STakashi Iwai 	},
1375bc9b5623STakashi Iwai };
1376bc9b5623STakashi Iwai 
13779af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_analog_capture = {
13787eb56e84STakashi Iwai 	.substreams = 1, /* will be changed in via_build_pcms() */
1379c577b8a1SJoseph Chan 	.channels_min = 2,
1380c577b8a1SJoseph Chan 	.channels_max = 2,
13819af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1382c577b8a1SJoseph Chan 	.ops = {
1383c577b8a1SJoseph Chan 		.prepare = via_capture_pcm_prepare,
1384c577b8a1SJoseph Chan 		.cleanup = via_capture_pcm_cleanup
1385c577b8a1SJoseph Chan 	},
1386c577b8a1SJoseph Chan };
1387c577b8a1SJoseph Chan 
1388a86a88eaSTakashi Iwai static const struct hda_pcm_stream via_pcm_dyn_adc_analog_capture = {
1389a86a88eaSTakashi Iwai 	.substreams = 1,
1390a86a88eaSTakashi Iwai 	.channels_min = 2,
1391a86a88eaSTakashi Iwai 	.channels_max = 2,
1392a86a88eaSTakashi Iwai 	/* NID is set in via_build_pcms */
1393a86a88eaSTakashi Iwai 	.ops = {
1394a86a88eaSTakashi Iwai 		.prepare = via_dyn_adc_capture_pcm_prepare,
1395a86a88eaSTakashi Iwai 		.cleanup = via_dyn_adc_capture_pcm_cleanup,
1396a86a88eaSTakashi Iwai 	},
1397a86a88eaSTakashi Iwai };
1398a86a88eaSTakashi Iwai 
13999af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_digital_playback = {
1400c577b8a1SJoseph Chan 	.substreams = 1,
1401c577b8a1SJoseph Chan 	.channels_min = 2,
1402c577b8a1SJoseph Chan 	.channels_max = 2,
1403c577b8a1SJoseph Chan 	/* NID is set in via_build_pcms */
1404c577b8a1SJoseph Chan 	.ops = {
1405c577b8a1SJoseph Chan 		.open = via_dig_playback_pcm_open,
14066b97eb45STakashi Iwai 		.close = via_dig_playback_pcm_close,
14079da29271STakashi Iwai 		.prepare = via_dig_playback_pcm_prepare,
14089da29271STakashi Iwai 		.cleanup = via_dig_playback_pcm_cleanup
1409c577b8a1SJoseph Chan 	},
1410c577b8a1SJoseph Chan };
1411c577b8a1SJoseph Chan 
14129af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_digital_capture = {
1413c577b8a1SJoseph Chan 	.substreams = 1,
1414c577b8a1SJoseph Chan 	.channels_min = 2,
1415c577b8a1SJoseph Chan 	.channels_max = 2,
1416c577b8a1SJoseph Chan };
1417c577b8a1SJoseph Chan 
1418370bafbdSTakashi Iwai /*
1419370bafbdSTakashi Iwai  * slave controls for virtual master
1420370bafbdSTakashi Iwai  */
1421370bafbdSTakashi Iwai static const char * const via_slave_vols[] = {
1422370bafbdSTakashi Iwai 	"Front Playback Volume",
1423370bafbdSTakashi Iwai 	"Surround Playback Volume",
1424370bafbdSTakashi Iwai 	"Center Playback Volume",
1425370bafbdSTakashi Iwai 	"LFE Playback Volume",
1426370bafbdSTakashi Iwai 	"Side Playback Volume",
1427370bafbdSTakashi Iwai 	"Headphone Playback Volume",
1428370bafbdSTakashi Iwai 	"Speaker Playback Volume",
1429370bafbdSTakashi Iwai 	NULL,
1430370bafbdSTakashi Iwai };
1431370bafbdSTakashi Iwai 
1432370bafbdSTakashi Iwai static const char * const via_slave_sws[] = {
1433370bafbdSTakashi Iwai 	"Front Playback Switch",
1434370bafbdSTakashi Iwai 	"Surround Playback Switch",
1435370bafbdSTakashi Iwai 	"Center Playback Switch",
1436370bafbdSTakashi Iwai 	"LFE Playback Switch",
1437370bafbdSTakashi Iwai 	"Side Playback Switch",
1438370bafbdSTakashi Iwai 	"Headphone Playback Switch",
1439370bafbdSTakashi Iwai 	"Speaker Playback Switch",
1440370bafbdSTakashi Iwai 	NULL,
1441370bafbdSTakashi Iwai };
1442370bafbdSTakashi Iwai 
1443c577b8a1SJoseph Chan static int via_build_controls(struct hda_codec *codec)
1444c577b8a1SJoseph Chan {
1445c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
14465b0cb1d8SJaroslav Kysela 	struct snd_kcontrol *kctl;
14475b0cb1d8SJaroslav Kysela 	int err, i;
1448c577b8a1SJoseph Chan 
144924088a58STakashi Iwai 	if (spec->set_widgets_power_state)
145024088a58STakashi Iwai 		if (!via_clone_control(spec, &via_pin_power_ctl_enum))
145124088a58STakashi Iwai 			return -ENOMEM;
145224088a58STakashi Iwai 
1453c577b8a1SJoseph Chan 	for (i = 0; i < spec->num_mixers; i++) {
1454c577b8a1SJoseph Chan 		err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1455c577b8a1SJoseph Chan 		if (err < 0)
1456c577b8a1SJoseph Chan 			return err;
1457c577b8a1SJoseph Chan 	}
1458c577b8a1SJoseph Chan 
1459c577b8a1SJoseph Chan 	if (spec->multiout.dig_out_nid) {
1460c577b8a1SJoseph Chan 		err = snd_hda_create_spdif_out_ctls(codec,
146174b654c9SStephen Warren 						    spec->multiout.dig_out_nid,
1462c577b8a1SJoseph Chan 						    spec->multiout.dig_out_nid);
1463c577b8a1SJoseph Chan 		if (err < 0)
1464c577b8a1SJoseph Chan 			return err;
14659a08160bSTakashi Iwai 		err = snd_hda_create_spdif_share_sw(codec,
14669a08160bSTakashi Iwai 						    &spec->multiout);
14679a08160bSTakashi Iwai 		if (err < 0)
14689a08160bSTakashi Iwai 			return err;
14699a08160bSTakashi Iwai 		spec->multiout.share_spdif = 1;
1470c577b8a1SJoseph Chan 	}
1471c577b8a1SJoseph Chan 	if (spec->dig_in_nid) {
1472c577b8a1SJoseph Chan 		err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1473c577b8a1SJoseph Chan 		if (err < 0)
1474c577b8a1SJoseph Chan 			return err;
1475c577b8a1SJoseph Chan 	}
147617314379SLydia Wang 
1477370bafbdSTakashi Iwai 	/* if we have no master control, let's create it */
1478370bafbdSTakashi Iwai 	if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1479370bafbdSTakashi Iwai 		unsigned int vmaster_tlv[4];
1480370bafbdSTakashi Iwai 		snd_hda_set_vmaster_tlv(codec, spec->multiout.dac_nids[0],
1481370bafbdSTakashi Iwai 					HDA_OUTPUT, vmaster_tlv);
1482370bafbdSTakashi Iwai 		err = snd_hda_add_vmaster(codec, "Master Playback Volume",
1483370bafbdSTakashi Iwai 					  vmaster_tlv, via_slave_vols);
1484370bafbdSTakashi Iwai 		if (err < 0)
1485370bafbdSTakashi Iwai 			return err;
1486370bafbdSTakashi Iwai 	}
1487370bafbdSTakashi Iwai 	if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1488370bafbdSTakashi Iwai 		err = snd_hda_add_vmaster(codec, "Master Playback Switch",
1489370bafbdSTakashi Iwai 					  NULL, via_slave_sws);
1490370bafbdSTakashi Iwai 		if (err < 0)
1491370bafbdSTakashi Iwai 			return err;
1492370bafbdSTakashi Iwai 	}
1493370bafbdSTakashi Iwai 
14945b0cb1d8SJaroslav Kysela 	/* assign Capture Source enums to NID */
14955b0cb1d8SJaroslav Kysela 	kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
14965b0cb1d8SJaroslav Kysela 	for (i = 0; kctl && i < kctl->count; i++) {
149721949f00STakashi Iwai 		err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
14985b0cb1d8SJaroslav Kysela 		if (err < 0)
14995b0cb1d8SJaroslav Kysela 			return err;
15005b0cb1d8SJaroslav Kysela 	}
15015b0cb1d8SJaroslav Kysela 
150217314379SLydia Wang 	/* init power states */
15033e95b9abSLydia Wang 	set_widgets_power_state(codec);
1504ada509ecSTakashi Iwai 	analog_low_current_mode(codec);
150517314379SLydia Wang 
1506603c4019STakashi Iwai 	via_free_kctls(codec); /* no longer needed */
150701a61e12STakashi Iwai 
150801a61e12STakashi Iwai 	err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
150901a61e12STakashi Iwai 	if (err < 0)
151001a61e12STakashi Iwai 		return err;
151101a61e12STakashi Iwai 
1512c577b8a1SJoseph Chan 	return 0;
1513c577b8a1SJoseph Chan }
1514c577b8a1SJoseph Chan 
1515c577b8a1SJoseph Chan static int via_build_pcms(struct hda_codec *codec)
1516c577b8a1SJoseph Chan {
1517c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1518c577b8a1SJoseph Chan 	struct hda_pcm *info = spec->pcm_rec;
1519c577b8a1SJoseph Chan 
1520a5973103STakashi Iwai 	codec->num_pcms = 0;
1521c577b8a1SJoseph Chan 	codec->pcm_info = info;
1522c577b8a1SJoseph Chan 
1523a5973103STakashi Iwai 	if (spec->multiout.num_dacs || spec->num_adc_nids) {
1524a5973103STakashi Iwai 		snprintf(spec->stream_name_analog,
1525a5973103STakashi Iwai 			 sizeof(spec->stream_name_analog),
152682673bc8STakashi Iwai 			 "%s Analog", codec->chip_name);
1527c577b8a1SJoseph Chan 		info->name = spec->stream_name_analog;
15289af74210STakashi Iwai 
1529a5973103STakashi Iwai 		if (spec->multiout.num_dacs) {
15309af74210STakashi Iwai 			if (!spec->stream_analog_playback)
1531a5973103STakashi Iwai 				spec->stream_analog_playback =
1532a5973103STakashi Iwai 					&via_pcm_analog_playback;
1533377ff31aSLydia Wang 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
15349af74210STakashi Iwai 				*spec->stream_analog_playback;
1535377ff31aSLydia Wang 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1536377ff31aSLydia Wang 				spec->multiout.dac_nids[0];
1537c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1538c577b8a1SJoseph Chan 				spec->multiout.max_channels;
1539a5973103STakashi Iwai 		}
15409af74210STakashi Iwai 
1541a86a88eaSTakashi Iwai 		if (!spec->stream_analog_capture) {
1542a86a88eaSTakashi Iwai 			if (spec->dyn_adc_switch)
1543a86a88eaSTakashi Iwai 				spec->stream_analog_capture =
1544a86a88eaSTakashi Iwai 					&via_pcm_dyn_adc_analog_capture;
1545a86a88eaSTakashi Iwai 			else
1546a5973103STakashi Iwai 				spec->stream_analog_capture =
1547a5973103STakashi Iwai 					&via_pcm_analog_capture;
1548a86a88eaSTakashi Iwai 		}
1549a5973103STakashi Iwai 		if (spec->num_adc_nids) {
15509af74210STakashi Iwai 			info->stream[SNDRV_PCM_STREAM_CAPTURE] =
15519af74210STakashi Iwai 				*spec->stream_analog_capture;
1552a5973103STakashi Iwai 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1553a5973103STakashi Iwai 				spec->adc_nids[0];
1554a86a88eaSTakashi Iwai 			if (!spec->dyn_adc_switch)
15559af74210STakashi Iwai 				info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
15569af74210STakashi Iwai 					spec->num_adc_nids;
1557a5973103STakashi Iwai 		}
1558c577b8a1SJoseph Chan 		codec->num_pcms++;
1559c577b8a1SJoseph Chan 		info++;
1560a5973103STakashi Iwai 	}
1561a5973103STakashi Iwai 
1562a5973103STakashi Iwai 	if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
156382673bc8STakashi Iwai 		snprintf(spec->stream_name_digital,
156482673bc8STakashi Iwai 			 sizeof(spec->stream_name_digital),
156582673bc8STakashi Iwai 			 "%s Digital", codec->chip_name);
1566c577b8a1SJoseph Chan 		info->name = spec->stream_name_digital;
15677ba72ba1STakashi Iwai 		info->pcm_type = HDA_PCM_TYPE_SPDIF;
1568c577b8a1SJoseph Chan 		if (spec->multiout.dig_out_nid) {
15699af74210STakashi Iwai 			if (!spec->stream_digital_playback)
15709af74210STakashi Iwai 				spec->stream_digital_playback =
15719af74210STakashi Iwai 					&via_pcm_digital_playback;
1572c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
15739af74210STakashi Iwai 				*spec->stream_digital_playback;
1574c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1575c577b8a1SJoseph Chan 				spec->multiout.dig_out_nid;
1576c577b8a1SJoseph Chan 		}
1577c577b8a1SJoseph Chan 		if (spec->dig_in_nid) {
15789af74210STakashi Iwai 			if (!spec->stream_digital_capture)
15799af74210STakashi Iwai 				spec->stream_digital_capture =
15809af74210STakashi Iwai 					&via_pcm_digital_capture;
1581c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_CAPTURE] =
15829af74210STakashi Iwai 				*spec->stream_digital_capture;
1583c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1584c577b8a1SJoseph Chan 				spec->dig_in_nid;
1585c577b8a1SJoseph Chan 		}
1586a5973103STakashi Iwai 		codec->num_pcms++;
1587a5973103STakashi Iwai 		info++;
1588c577b8a1SJoseph Chan 	}
1589c577b8a1SJoseph Chan 
1590ece8d043STakashi Iwai 	if (spec->hp_dac_nid) {
15917eb56e84STakashi Iwai 		snprintf(spec->stream_name_hp, sizeof(spec->stream_name_hp),
15927eb56e84STakashi Iwai 			 "%s HP", codec->chip_name);
15937eb56e84STakashi Iwai 		info->name = spec->stream_name_hp;
15947eb56e84STakashi Iwai 		info->stream[SNDRV_PCM_STREAM_PLAYBACK] = via_pcm_hp_playback;
15957eb56e84STakashi Iwai 		info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1596ece8d043STakashi Iwai 			spec->hp_dac_nid;
1597a5973103STakashi Iwai 		codec->num_pcms++;
1598a5973103STakashi Iwai 		info++;
15997eb56e84STakashi Iwai 	}
1600c577b8a1SJoseph Chan 	return 0;
1601c577b8a1SJoseph Chan }
1602c577b8a1SJoseph Chan 
1603c577b8a1SJoseph Chan static void via_free(struct hda_codec *codec)
1604c577b8a1SJoseph Chan {
1605c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1606c577b8a1SJoseph Chan 
1607c577b8a1SJoseph Chan 	if (!spec)
1608c577b8a1SJoseph Chan 		return;
1609c577b8a1SJoseph Chan 
1610603c4019STakashi Iwai 	via_free_kctls(codec);
16111f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
1612a86a88eaSTakashi Iwai 	kfree(spec->bind_cap_vol);
1613a86a88eaSTakashi Iwai 	kfree(spec->bind_cap_sw);
1614a86a88eaSTakashi Iwai 	kfree(spec);
1615c577b8a1SJoseph Chan }
1616c577b8a1SJoseph Chan 
161764be285bSTakashi Iwai /* mute/unmute outputs */
161864be285bSTakashi Iwai static void toggle_output_mutes(struct hda_codec *codec, int num_pins,
161964be285bSTakashi Iwai 				hda_nid_t *pins, bool mute)
162064be285bSTakashi Iwai {
162164be285bSTakashi Iwai 	int i;
162294994734STakashi Iwai 	for (i = 0; i < num_pins; i++) {
162394994734STakashi Iwai 		unsigned int parm = snd_hda_codec_read(codec, pins[i], 0,
162494994734STakashi Iwai 					  AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
162594994734STakashi Iwai 		if (parm & AC_PINCTL_IN_EN)
162694994734STakashi Iwai 			continue;
162794994734STakashi Iwai 		if (mute)
162894994734STakashi Iwai 			parm &= ~AC_PINCTL_OUT_EN;
162994994734STakashi Iwai 		else
163094994734STakashi Iwai 			parm |= AC_PINCTL_OUT_EN;
163164be285bSTakashi Iwai 		snd_hda_codec_write(codec, pins[i], 0,
163294994734STakashi Iwai 				    AC_VERB_SET_PIN_WIDGET_CONTROL, parm);
163394994734STakashi Iwai 	}
163464be285bSTakashi Iwai }
163564be285bSTakashi Iwai 
16364a918ffeSTakashi Iwai /* mute internal speaker if line-out is plugged */
16374a918ffeSTakashi Iwai static void via_line_automute(struct hda_codec *codec, int present)
16384a918ffeSTakashi Iwai {
16394a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
16404a918ffeSTakashi Iwai 
16414a918ffeSTakashi Iwai 	if (!spec->autocfg.speaker_outs)
16424a918ffeSTakashi Iwai 		return;
16434a918ffeSTakashi Iwai 	if (!present)
16444a918ffeSTakashi Iwai 		present = snd_hda_jack_detect(codec,
16454a918ffeSTakashi Iwai 					      spec->autocfg.line_out_pins[0]);
16464a918ffeSTakashi Iwai 	toggle_output_mutes(codec, spec->autocfg.speaker_outs,
16474a918ffeSTakashi Iwai 			    spec->autocfg.speaker_pins,
16484a918ffeSTakashi Iwai 			    present);
16494a918ffeSTakashi Iwai }
16504a918ffeSTakashi Iwai 
165169e52a80SHarald Welte /* mute internal speaker if HP is plugged */
165269e52a80SHarald Welte static void via_hp_automute(struct hda_codec *codec)
165369e52a80SHarald Welte {
16544a918ffeSTakashi Iwai 	int present = 0;
16556e969d91STakashi Iwai 	int nums;
165669e52a80SHarald Welte 	struct via_spec *spec = codec->spec;
165769e52a80SHarald Welte 
1658187d333eSTakashi Iwai 	if (!spec->hp_independent_mode && spec->autocfg.hp_pins[0] &&
1659187d333eSTakashi Iwai 	    (spec->codec_type != VT1708 || spec->vt1708_jack_detect))
1660d56757abSTakashi Iwai 		present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
16616e969d91STakashi Iwai 
1662f2b1c9f0STakashi Iwai 	if (spec->smart51_enabled)
1663f2b1c9f0STakashi Iwai 		nums = spec->autocfg.line_outs + spec->smart51_nums;
1664f2b1c9f0STakashi Iwai 	else
1665f2b1c9f0STakashi Iwai 		nums = spec->autocfg.line_outs;
16666e969d91STakashi Iwai 	toggle_output_mutes(codec, nums, spec->autocfg.line_out_pins, present);
16676e969d91STakashi Iwai 
16684a918ffeSTakashi Iwai 	via_line_automute(codec, present);
1669f3db423dSLydia Wang }
1670f3db423dSLydia Wang 
167169e52a80SHarald Welte static void via_gpio_control(struct hda_codec *codec)
167269e52a80SHarald Welte {
167369e52a80SHarald Welte 	unsigned int gpio_data;
167469e52a80SHarald Welte 	unsigned int vol_counter;
167569e52a80SHarald Welte 	unsigned int vol;
167669e52a80SHarald Welte 	unsigned int master_vol;
167769e52a80SHarald Welte 
167869e52a80SHarald Welte 	struct via_spec *spec = codec->spec;
167969e52a80SHarald Welte 
168069e52a80SHarald Welte 	gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
168169e52a80SHarald Welte 				       AC_VERB_GET_GPIO_DATA, 0) & 0x03;
168269e52a80SHarald Welte 
168369e52a80SHarald Welte 	vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
168469e52a80SHarald Welte 					  0xF84, 0) & 0x3F0000) >> 16;
168569e52a80SHarald Welte 
168669e52a80SHarald Welte 	vol = vol_counter & 0x1F;
168769e52a80SHarald Welte 	master_vol = snd_hda_codec_read(codec, 0x1A, 0,
168869e52a80SHarald Welte 					AC_VERB_GET_AMP_GAIN_MUTE,
168969e52a80SHarald Welte 					AC_AMP_GET_INPUT);
169069e52a80SHarald Welte 
169169e52a80SHarald Welte 	if (gpio_data == 0x02) {
169269e52a80SHarald Welte 		/* unmute line out */
16933e0693e2STakashi Iwai 		snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
16943e0693e2STakashi Iwai 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
16953e0693e2STakashi Iwai 				    PIN_OUT);
169669e52a80SHarald Welte 		if (vol_counter & 0x20) {
169769e52a80SHarald Welte 			/* decrease volume */
169869e52a80SHarald Welte 			if (vol > master_vol)
169969e52a80SHarald Welte 				vol = master_vol;
170069e52a80SHarald Welte 			snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
170169e52a80SHarald Welte 						 0, HDA_AMP_VOLMASK,
170269e52a80SHarald Welte 						 master_vol-vol);
170369e52a80SHarald Welte 		} else {
170469e52a80SHarald Welte 			/* increase volume */
170569e52a80SHarald Welte 			snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
170669e52a80SHarald Welte 					 HDA_AMP_VOLMASK,
170769e52a80SHarald Welte 					 ((master_vol+vol) > 0x2A) ? 0x2A :
170869e52a80SHarald Welte 					  (master_vol+vol));
170969e52a80SHarald Welte 		}
171069e52a80SHarald Welte 	} else if (!(gpio_data & 0x02)) {
171169e52a80SHarald Welte 		/* mute line out */
17123e0693e2STakashi Iwai 		snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
17133e0693e2STakashi Iwai 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
17143e0693e2STakashi Iwai 				    0);
171569e52a80SHarald Welte 	}
171669e52a80SHarald Welte }
171769e52a80SHarald Welte 
171869e52a80SHarald Welte /* unsolicited event for jack sensing */
171969e52a80SHarald Welte static void via_unsol_event(struct hda_codec *codec,
172069e52a80SHarald Welte 				  unsigned int res)
172169e52a80SHarald Welte {
172269e52a80SHarald Welte 	res >>= 26;
17233a93897eSTakashi Iwai 	res = snd_hda_jack_get_action(codec, res);
1724ec7e7e42SLydia Wang 
1725a34df19aSLydia Wang 	if (res & VIA_JACK_EVENT)
17263e95b9abSLydia Wang 		set_widgets_power_state(codec);
1727ec7e7e42SLydia Wang 
1728ec7e7e42SLydia Wang 	res &= ~VIA_JACK_EVENT;
1729ec7e7e42SLydia Wang 
173021ce0b65STakashi Iwai 	if (res == VIA_HP_EVENT || res == VIA_LINE_EVENT)
1731ec7e7e42SLydia Wang 		via_hp_automute(codec);
1732ec7e7e42SLydia Wang 	else if (res == VIA_GPIO_EVENT)
1733ec7e7e42SLydia Wang 		via_gpio_control(codec);
173401a61e12STakashi Iwai 	snd_hda_jack_report_sync(codec);
173569e52a80SHarald Welte }
173669e52a80SHarald Welte 
17372a43952aSTakashi Iwai #ifdef CONFIG_PM
17381f2e99feSLydia Wang static int via_suspend(struct hda_codec *codec, pm_message_t state)
17391f2e99feSLydia Wang {
17401f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
17411f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
17421f2e99feSLydia Wang 	return 0;
17431f2e99feSLydia Wang }
17441f2e99feSLydia Wang #endif
17451f2e99feSLydia Wang 
1746cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
1747cb53c626STakashi Iwai static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1748cb53c626STakashi Iwai {
1749cb53c626STakashi Iwai 	struct via_spec *spec = codec->spec;
1750cb53c626STakashi Iwai 	return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1751cb53c626STakashi Iwai }
1752cb53c626STakashi Iwai #endif
1753cb53c626STakashi Iwai 
1754c577b8a1SJoseph Chan /*
1755c577b8a1SJoseph Chan  */
17565d41762aSTakashi Iwai 
17575d41762aSTakashi Iwai static int via_init(struct hda_codec *codec);
17585d41762aSTakashi Iwai 
175990dd48a1STakashi Iwai static const struct hda_codec_ops via_patch_ops = {
1760c577b8a1SJoseph Chan 	.build_controls = via_build_controls,
1761c577b8a1SJoseph Chan 	.build_pcms = via_build_pcms,
1762c577b8a1SJoseph Chan 	.init = via_init,
1763c577b8a1SJoseph Chan 	.free = via_free,
17644a918ffeSTakashi Iwai 	.unsol_event = via_unsol_event,
17652a43952aSTakashi Iwai #ifdef CONFIG_PM
17661f2e99feSLydia Wang 	.suspend = via_suspend,
17671f2e99feSLydia Wang #endif
1768cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
1769cb53c626STakashi Iwai 	.check_power_status = via_check_power_status,
1770cb53c626STakashi Iwai #endif
1771c577b8a1SJoseph Chan };
1772c577b8a1SJoseph Chan 
17734a79616dSTakashi Iwai static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
1774c577b8a1SJoseph Chan {
17754a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
17764a79616dSTakashi Iwai 	int i;
17774a79616dSTakashi Iwai 
17784a79616dSTakashi Iwai 	for (i = 0; i < spec->multiout.num_dacs; i++) {
17794a79616dSTakashi Iwai 		if (spec->multiout.dac_nids[i] == dac)
17804a79616dSTakashi Iwai 			return false;
17814a79616dSTakashi Iwai 	}
1782ece8d043STakashi Iwai 	if (spec->hp_dac_nid == dac)
17834a79616dSTakashi Iwai 		return false;
17844a79616dSTakashi Iwai 	return true;
17854a79616dSTakashi Iwai }
17864a79616dSTakashi Iwai 
17878e3679dcSTakashi Iwai static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
17883214b966STakashi Iwai 				hda_nid_t target_dac, int with_aa_mix,
17893214b966STakashi Iwai 				struct nid_path *path, int depth)
17904a79616dSTakashi Iwai {
17913214b966STakashi Iwai 	struct via_spec *spec = codec->spec;
17924a79616dSTakashi Iwai 	hda_nid_t conn[8];
17934a79616dSTakashi Iwai 	int i, nums;
17944a79616dSTakashi Iwai 
17953214b966STakashi Iwai 	if (nid == spec->aa_mix_nid) {
17963214b966STakashi Iwai 		if (!with_aa_mix)
17973214b966STakashi Iwai 			return false;
17983214b966STakashi Iwai 		with_aa_mix = 2; /* mark aa-mix is included */
17993214b966STakashi Iwai 	}
18003214b966STakashi Iwai 
18014a79616dSTakashi Iwai 	nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
18024a79616dSTakashi Iwai 	for (i = 0; i < nums; i++) {
18034a79616dSTakashi Iwai 		if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
18044a79616dSTakashi Iwai 			continue;
18053214b966STakashi Iwai 		if (conn[i] == target_dac || is_empty_dac(codec, conn[i])) {
18063214b966STakashi Iwai 			/* aa-mix is requested but not included? */
18073214b966STakashi Iwai 			if (!(spec->aa_mix_nid && with_aa_mix == 1))
180809a9ad69STakashi Iwai 				goto found;
18094a79616dSTakashi Iwai 		}
18103214b966STakashi Iwai 	}
18118e3679dcSTakashi Iwai 	if (depth >= MAX_NID_PATH_DEPTH)
18124a79616dSTakashi Iwai 		return false;
18134a79616dSTakashi Iwai 	for (i = 0; i < nums; i++) {
18144a79616dSTakashi Iwai 		unsigned int type;
18154a79616dSTakashi Iwai 		type = get_wcaps_type(get_wcaps(codec, conn[i]));
18163214b966STakashi Iwai 		if (type == AC_WID_AUD_OUT)
18174a79616dSTakashi Iwai 			continue;
18188e3679dcSTakashi Iwai 		if (__parse_output_path(codec, conn[i], target_dac,
18193214b966STakashi Iwai 					with_aa_mix, path, depth + 1))
182009a9ad69STakashi Iwai 			goto found;
18214a79616dSTakashi Iwai 	}
18224a79616dSTakashi Iwai 	return false;
182309a9ad69STakashi Iwai 
182409a9ad69STakashi Iwai  found:
182509a9ad69STakashi Iwai 	path->path[path->depth] = conn[i];
182609a9ad69STakashi Iwai 	path->idx[path->depth] = i;
182709a9ad69STakashi Iwai 	if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
182809a9ad69STakashi Iwai 		path->multi[path->depth] = 1;
182909a9ad69STakashi Iwai 	path->depth++;
183009a9ad69STakashi Iwai 	return true;
18314a79616dSTakashi Iwai }
18324a79616dSTakashi Iwai 
18338e3679dcSTakashi Iwai static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
18343214b966STakashi Iwai 			      hda_nid_t target_dac, int with_aa_mix,
18353214b966STakashi Iwai 			      struct nid_path *path)
18368e3679dcSTakashi Iwai {
18373214b966STakashi Iwai 	if (__parse_output_path(codec, nid, target_dac, with_aa_mix, path, 1)) {
18388e3679dcSTakashi Iwai 		path->path[path->depth] = nid;
18398e3679dcSTakashi Iwai 		path->depth++;
18403214b966STakashi Iwai 		snd_printdd("output-path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
18413214b966STakashi Iwai 			    path->depth, path->path[0], path->path[1],
18423214b966STakashi Iwai 			    path->path[2], path->path[3], path->path[4]);
18438e3679dcSTakashi Iwai 		return true;
18448e3679dcSTakashi Iwai 	}
18458e3679dcSTakashi Iwai 	return false;
18468e3679dcSTakashi Iwai }
18478e3679dcSTakashi Iwai 
18484a79616dSTakashi Iwai static int via_auto_fill_dac_nids(struct hda_codec *codec)
18494a79616dSTakashi Iwai {
18504a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
18514a79616dSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
18525c9a5615SLydia Wang 	int i, dac_num;
1853c577b8a1SJoseph Chan 	hda_nid_t nid;
1854c577b8a1SJoseph Chan 
1855c577b8a1SJoseph Chan 	spec->multiout.dac_nids = spec->private_dac_nids;
18565c9a5615SLydia Wang 	dac_num = 0;
18574a79616dSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
18583214b966STakashi Iwai 		hda_nid_t dac = 0;
1859c577b8a1SJoseph Chan 		nid = cfg->line_out_pins[i];
18604a79616dSTakashi Iwai 		if (!nid)
18614a79616dSTakashi Iwai 			continue;
18623214b966STakashi Iwai 		if (parse_output_path(codec, nid, 0, 0, &spec->out_path[i]))
18633214b966STakashi Iwai 			dac = spec->out_path[i].path[0];
18643214b966STakashi Iwai 		if (!i && parse_output_path(codec, nid, dac, 1,
18653214b966STakashi Iwai 					    &spec->out_mix_path))
18663214b966STakashi Iwai 			dac = spec->out_mix_path.path[0];
18673214b966STakashi Iwai 		if (dac) {
18683214b966STakashi Iwai 			spec->private_dac_nids[i] = dac;
18695c9a5615SLydia Wang 			dac_num++;
1870c577b8a1SJoseph Chan 		}
18715c9a5615SLydia Wang 	}
18723214b966STakashi Iwai 	if (!spec->out_path[0].depth && spec->out_mix_path.depth) {
18733214b966STakashi Iwai 		spec->out_path[0] = spec->out_mix_path;
18743214b966STakashi Iwai 		spec->out_mix_path.depth = 0;
18753214b966STakashi Iwai 	}
18765c9a5615SLydia Wang 	spec->multiout.num_dacs = dac_num;
1877c577b8a1SJoseph Chan 	return 0;
1878c577b8a1SJoseph Chan }
1879c577b8a1SJoseph Chan 
18804a79616dSTakashi Iwai static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
188109a9ad69STakashi Iwai 			  int chs, bool check_dac, struct nid_path *path)
1882c577b8a1SJoseph Chan {
18834a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
1884c577b8a1SJoseph Chan 	char name[32];
188509a9ad69STakashi Iwai 	hda_nid_t dac, pin, sel, nid;
188609a9ad69STakashi Iwai 	int err;
1887a934d5a9STakashi Iwai 
188809a9ad69STakashi Iwai 	dac = check_dac ? path->path[0] : 0;
188909a9ad69STakashi Iwai 	pin = path->path[path->depth - 1];
189009a9ad69STakashi Iwai 	sel = path->depth > 1 ? path->path[1] : 0;
1891c577b8a1SJoseph Chan 
18928df2a312STakashi Iwai 	if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
18934a79616dSTakashi Iwai 		nid = dac;
18948df2a312STakashi Iwai 	else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
18954a79616dSTakashi Iwai 		nid = pin;
1896a934d5a9STakashi Iwai 	else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1897a934d5a9STakashi Iwai 		nid = sel;
18984a79616dSTakashi Iwai 	else
18994a79616dSTakashi Iwai 		nid = 0;
19004a79616dSTakashi Iwai 	if (nid) {
19014a79616dSTakashi Iwai 		sprintf(name, "%s Playback Volume", pfx);
1902c577b8a1SJoseph Chan 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1903a00a5fadSLydia Wang 			      HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1904c577b8a1SJoseph Chan 		if (err < 0)
1905c577b8a1SJoseph Chan 			return err;
190609a9ad69STakashi Iwai 		path->vol_ctl = nid;
1907c577b8a1SJoseph Chan 	}
19084a79616dSTakashi Iwai 
19098df2a312STakashi Iwai 	if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
19104a79616dSTakashi Iwai 		nid = dac;
19118df2a312STakashi Iwai 	else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
19124a79616dSTakashi Iwai 		nid = pin;
1913a934d5a9STakashi Iwai 	else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
1914a934d5a9STakashi Iwai 		nid = sel;
19154a79616dSTakashi Iwai 	else
19164a79616dSTakashi Iwai 		nid = 0;
19174a79616dSTakashi Iwai 	if (nid) {
19184a79616dSTakashi Iwai 		sprintf(name, "%s Playback Switch", pfx);
19194a79616dSTakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
19204a79616dSTakashi Iwai 			      HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
19214a79616dSTakashi Iwai 		if (err < 0)
19224a79616dSTakashi Iwai 			return err;
192309a9ad69STakashi Iwai 		path->mute_ctl = nid;
19244a79616dSTakashi Iwai 	}
19254a79616dSTakashi Iwai 	return 0;
19264a79616dSTakashi Iwai }
19274a79616dSTakashi Iwai 
1928f4a7828bSTakashi Iwai static void mangle_smart51(struct hda_codec *codec)
1929f4a7828bSTakashi Iwai {
1930f4a7828bSTakashi Iwai 	struct via_spec *spec = codec->spec;
1931f4a7828bSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
19320f98c24bSTakashi Iwai 	struct auto_pin_cfg_item *ins = cfg->inputs;
19330f98c24bSTakashi Iwai 	int i, j, nums, attr;
19340f98c24bSTakashi Iwai 	int pins[AUTO_CFG_MAX_INS];
1935f4a7828bSTakashi Iwai 
19360f98c24bSTakashi Iwai 	for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
19370f98c24bSTakashi Iwai 		nums = 0;
1938f4a7828bSTakashi Iwai 		for (i = 0; i < cfg->num_inputs; i++) {
19390f98c24bSTakashi Iwai 			unsigned int def;
19400f98c24bSTakashi Iwai 			if (ins[i].type > AUTO_PIN_LINE_IN)
19410f98c24bSTakashi Iwai 				continue;
19420f98c24bSTakashi Iwai 			def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
19430f98c24bSTakashi Iwai 			if (snd_hda_get_input_pin_attr(def) != attr)
19440f98c24bSTakashi Iwai 				continue;
19450f98c24bSTakashi Iwai 			for (j = 0; j < nums; j++)
19460f98c24bSTakashi Iwai 				if (ins[pins[j]].type < ins[i].type) {
19470f98c24bSTakashi Iwai 					memmove(pins + j + 1, pins + j,
194821d45d2bSTakashi Iwai 						(nums - j) * sizeof(int));
19490f98c24bSTakashi Iwai 					break;
19500f98c24bSTakashi Iwai 				}
19510f98c24bSTakashi Iwai 			pins[j] = i;
1952e3d7a143STakashi Iwai 			nums++;
1953e3d7a143STakashi Iwai 		}
1954e3d7a143STakashi Iwai 		if (cfg->line_outs + nums < 3)
1955f4a7828bSTakashi Iwai 			continue;
19560f98c24bSTakashi Iwai 		for (i = 0; i < nums; i++) {
19570f98c24bSTakashi Iwai 			hda_nid_t pin = ins[pins[i]].pin;
19580f98c24bSTakashi Iwai 			spec->smart51_pins[spec->smart51_nums++] = pin;
19590f98c24bSTakashi Iwai 			cfg->line_out_pins[cfg->line_outs++] = pin;
1960f4a7828bSTakashi Iwai 			if (cfg->line_outs == 3)
1961f4a7828bSTakashi Iwai 				break;
1962f4a7828bSTakashi Iwai 		}
19630f98c24bSTakashi Iwai 		return;
19640f98c24bSTakashi Iwai 	}
1965f4a7828bSTakashi Iwai }
1966f4a7828bSTakashi Iwai 
1967020066d1STakashi Iwai static void copy_path_mixer_ctls(struct nid_path *dst, struct nid_path *src)
1968020066d1STakashi Iwai {
1969020066d1STakashi Iwai 	dst->vol_ctl = src->vol_ctl;
1970020066d1STakashi Iwai 	dst->mute_ctl = src->mute_ctl;
1971020066d1STakashi Iwai }
1972020066d1STakashi Iwai 
19734a79616dSTakashi Iwai /* add playback controls from the parsed DAC table */
19744a79616dSTakashi Iwai static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
19754a79616dSTakashi Iwai {
19764a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
1977f4a7828bSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
19783214b966STakashi Iwai 	struct nid_path *path;
19794a79616dSTakashi Iwai 	static const char * const chname[4] = {
19804a79616dSTakashi Iwai 		"Front", "Surround", "C/LFE", "Side"
19814a79616dSTakashi Iwai 	};
19824a79616dSTakashi Iwai 	int i, idx, err;
1983f4a7828bSTakashi Iwai 	int old_line_outs;
1984f4a7828bSTakashi Iwai 
1985f4a7828bSTakashi Iwai 	/* check smart51 */
1986f4a7828bSTakashi Iwai 	old_line_outs = cfg->line_outs;
1987f4a7828bSTakashi Iwai 	if (cfg->line_outs == 1)
1988f4a7828bSTakashi Iwai 		mangle_smart51(codec);
19894a79616dSTakashi Iwai 
1990e3d7a143STakashi Iwai 	err = via_auto_fill_dac_nids(codec);
1991e3d7a143STakashi Iwai 	if (err < 0)
1992e3d7a143STakashi Iwai 		return err;
1993e3d7a143STakashi Iwai 
19945c9a5615SLydia Wang 	if (spec->multiout.num_dacs < 3) {
19955c9a5615SLydia Wang 		spec->smart51_nums = 0;
19965c9a5615SLydia Wang 		cfg->line_outs = old_line_outs;
19975c9a5615SLydia Wang 	}
19984a79616dSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
19994a79616dSTakashi Iwai 		hda_nid_t pin, dac;
20004a79616dSTakashi Iwai 		pin = cfg->line_out_pins[i];
20014a79616dSTakashi Iwai 		dac = spec->multiout.dac_nids[i];
20024a79616dSTakashi Iwai 		if (!pin || !dac)
20034a79616dSTakashi Iwai 			continue;
20043214b966STakashi Iwai 		path = spec->out_path + i;
20050fe0adf8STakashi Iwai 		if (i == HDA_CLFE) {
20063214b966STakashi Iwai 			err = create_ch_ctls(codec, "Center", 1, true, path);
20074a79616dSTakashi Iwai 			if (err < 0)
20084a79616dSTakashi Iwai 				return err;
20093214b966STakashi Iwai 			err = create_ch_ctls(codec, "LFE", 2, true, path);
20104a79616dSTakashi Iwai 			if (err < 0)
20114a79616dSTakashi Iwai 				return err;
20124a79616dSTakashi Iwai 		} else {
20136aadf41dSTakashi Iwai 			const char *pfx = chname[i];
20146aadf41dSTakashi Iwai 			if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
20156aadf41dSTakashi Iwai 			    cfg->line_outs == 1)
20166aadf41dSTakashi Iwai 				pfx = "Speaker";
20173214b966STakashi Iwai 			err = create_ch_ctls(codec, pfx, 3, true, path);
20184a79616dSTakashi Iwai 			if (err < 0)
20194a79616dSTakashi Iwai 				return err;
20204a79616dSTakashi Iwai 		}
2021020066d1STakashi Iwai 		if (path != spec->out_path + i)
2022020066d1STakashi Iwai 			copy_path_mixer_ctls(&spec->out_path[i], path);
2023020066d1STakashi Iwai 		if (path == spec->out_path && spec->out_mix_path.depth)
2024020066d1STakashi Iwai 			copy_path_mixer_ctls(&spec->out_mix_path, path);
20254a79616dSTakashi Iwai 	}
20264a79616dSTakashi Iwai 
20274a79616dSTakashi Iwai 	idx = get_connection_index(codec, spec->aa_mix_nid,
20284a79616dSTakashi Iwai 				   spec->multiout.dac_nids[0]);
20294a79616dSTakashi Iwai 	if (idx >= 0) {
20304a79616dSTakashi Iwai 		/* add control to mixer */
20313214b966STakashi Iwai 		const char *name;
20323214b966STakashi Iwai 		name = spec->out_mix_path.depth ?
20333214b966STakashi Iwai 			"PCM Loopback Playback Volume" : "PCM Playback Volume";
20343214b966STakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
20354a79616dSTakashi Iwai 				      HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
20364a79616dSTakashi Iwai 							  idx, HDA_INPUT));
20374a79616dSTakashi Iwai 		if (err < 0)
20384a79616dSTakashi Iwai 			return err;
20393214b966STakashi Iwai 		name = spec->out_mix_path.depth ?
20403214b966STakashi Iwai 			"PCM Loopback Playback Switch" : "PCM Playback Switch";
20413214b966STakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
20424a79616dSTakashi Iwai 				      HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
20434a79616dSTakashi Iwai 							  idx, HDA_INPUT));
20444a79616dSTakashi Iwai 		if (err < 0)
20454a79616dSTakashi Iwai 			return err;
2046c577b8a1SJoseph Chan 	}
2047c577b8a1SJoseph Chan 
2048f4a7828bSTakashi Iwai 	cfg->line_outs = old_line_outs;
2049f4a7828bSTakashi Iwai 
2050c577b8a1SJoseph Chan 	return 0;
2051c577b8a1SJoseph Chan }
2052c577b8a1SJoseph Chan 
20534a79616dSTakashi Iwai static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
2054c577b8a1SJoseph Chan {
20554a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
205609a9ad69STakashi Iwai 	struct nid_path *path;
205718bd2c44STakashi Iwai 	bool check_dac;
20583214b966STakashi Iwai 	int i, err;
2059c577b8a1SJoseph Chan 
2060c577b8a1SJoseph Chan 	if (!pin)
2061c577b8a1SJoseph Chan 		return 0;
2062c577b8a1SJoseph Chan 
20633214b966STakashi Iwai 	if (!parse_output_path(codec, pin, 0, 0, &spec->hp_indep_path)) {
20643214b966STakashi Iwai 		for (i = HDA_SIDE; i >= HDA_CLFE; i--) {
20653214b966STakashi Iwai 			if (i < spec->multiout.num_dacs &&
206625250505STakashi Iwai 			    parse_output_path(codec, pin,
20673214b966STakashi Iwai 					      spec->multiout.dac_nids[i], 0,
20683214b966STakashi Iwai 					      &spec->hp_indep_path)) {
20693214b966STakashi Iwai 				spec->hp_indep_shared = i;
20703214b966STakashi Iwai 				break;
207125250505STakashi Iwai 			}
20723214b966STakashi Iwai 		}
20733214b966STakashi Iwai 	}
20743214b966STakashi Iwai 	if (spec->hp_indep_path.depth) {
20753214b966STakashi Iwai 		spec->hp_dac_nid = spec->hp_indep_path.path[0];
20763214b966STakashi Iwai 		if (!spec->hp_indep_shared)
20773214b966STakashi Iwai 			spec->hp_path = spec->hp_indep_path;
20783214b966STakashi Iwai 	}
20793214b966STakashi Iwai 	/* optionally check front-path w/o AA-mix */
20803214b966STakashi Iwai 	if (!spec->hp_path.depth)
20813214b966STakashi Iwai 		parse_output_path(codec, pin,
20823214b966STakashi Iwai 				  spec->multiout.dac_nids[HDA_FRONT], 0,
20833214b966STakashi Iwai 				  &spec->hp_path);
20844a79616dSTakashi Iwai 
2085ece8d043STakashi Iwai 	if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
20863214b966STakashi Iwai 			       1, &spec->hp_mix_path) && !spec->hp_path.depth)
2087ece8d043STakashi Iwai 		return 0;
2088ece8d043STakashi Iwai 
20893214b966STakashi Iwai 	if (spec->hp_path.depth) {
209009a9ad69STakashi Iwai 		path = &spec->hp_path;
209118bd2c44STakashi Iwai 		check_dac = true;
209218bd2c44STakashi Iwai 	} else {
20933214b966STakashi Iwai 		path = &spec->hp_mix_path;
209418bd2c44STakashi Iwai 		check_dac = false;
209518bd2c44STakashi Iwai 	}
209618bd2c44STakashi Iwai 	err = create_ch_ctls(codec, "Headphone", 3, check_dac, path);
20974a79616dSTakashi Iwai 	if (err < 0)
20984a79616dSTakashi Iwai 		return err;
2099020066d1STakashi Iwai 	if (check_dac)
2100020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->hp_mix_path, path);
2101020066d1STakashi Iwai 	else
2102020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->hp_path, path);
2103020066d1STakashi Iwai 	if (spec->hp_indep_path.depth)
2104020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->hp_indep_path, path);
2105c577b8a1SJoseph Chan 	return 0;
2106c577b8a1SJoseph Chan }
2107c577b8a1SJoseph Chan 
21084a918ffeSTakashi Iwai static int via_auto_create_speaker_ctls(struct hda_codec *codec)
21094a918ffeSTakashi Iwai {
21104a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
21113214b966STakashi Iwai 	struct nid_path *path;
21123214b966STakashi Iwai 	bool check_dac;
211381c0a78bSWang Shaoyan 	hda_nid_t pin, dac = 0;
21143214b966STakashi Iwai 	int err;
21154a918ffeSTakashi Iwai 
21164a918ffeSTakashi Iwai 	pin = spec->autocfg.speaker_pins[0];
21174a918ffeSTakashi Iwai 	if (!spec->autocfg.speaker_outs || !pin)
21184a918ffeSTakashi Iwai 		return 0;
21194a918ffeSTakashi Iwai 
21203214b966STakashi Iwai 	if (parse_output_path(codec, pin, 0, 0, &spec->speaker_path))
21218e3679dcSTakashi Iwai 		dac = spec->speaker_path.path[0];
21223214b966STakashi Iwai 	if (!dac)
21233214b966STakashi Iwai 		parse_output_path(codec, pin,
21243214b966STakashi Iwai 				  spec->multiout.dac_nids[HDA_FRONT], 0,
212509a9ad69STakashi Iwai 				  &spec->speaker_path);
21263214b966STakashi Iwai 	if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
21273214b966STakashi Iwai 			       1, &spec->speaker_mix_path) && !dac)
21283214b966STakashi Iwai 		return 0;
21294a918ffeSTakashi Iwai 
21303214b966STakashi Iwai 	/* no AA-path for front? */
21313214b966STakashi Iwai 	if (!spec->out_mix_path.depth && spec->speaker_mix_path.depth)
21323214b966STakashi Iwai 		dac = 0;
21333214b966STakashi Iwai 
21343214b966STakashi Iwai 	spec->speaker_dac_nid = dac;
21353214b966STakashi Iwai 	spec->multiout.extra_out_nid[0] = dac;
21363214b966STakashi Iwai 	if (dac) {
21373214b966STakashi Iwai 		path = &spec->speaker_path;
21383214b966STakashi Iwai 		check_dac = true;
21393214b966STakashi Iwai 	} else {
21403214b966STakashi Iwai 		path = &spec->speaker_mix_path;
21413214b966STakashi Iwai 		check_dac = false;
21423214b966STakashi Iwai 	}
21433214b966STakashi Iwai 	err = create_ch_ctls(codec, "Speaker", 3, check_dac, path);
21443214b966STakashi Iwai 	if (err < 0)
21453214b966STakashi Iwai 		return err;
2146020066d1STakashi Iwai 	if (check_dac)
2147020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->speaker_mix_path, path);
2148020066d1STakashi Iwai 	else
2149020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->speaker_path, path);
21503214b966STakashi Iwai 	return 0;
21513214b966STakashi Iwai }
21523214b966STakashi Iwai 
21533214b966STakashi Iwai #define via_aamix_ctl_info	via_pin_power_ctl_info
21543214b966STakashi Iwai 
21553214b966STakashi Iwai static int via_aamix_ctl_get(struct snd_kcontrol *kcontrol,
21563214b966STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
21573214b966STakashi Iwai {
21583214b966STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
21593214b966STakashi Iwai 	struct via_spec *spec = codec->spec;
21603214b966STakashi Iwai 	ucontrol->value.enumerated.item[0] = spec->aamix_mode;
21613214b966STakashi Iwai 	return 0;
21623214b966STakashi Iwai }
21633214b966STakashi Iwai 
21643214b966STakashi Iwai static void update_aamix_paths(struct hda_codec *codec, int do_mix,
21653214b966STakashi Iwai 			       struct nid_path *nomix, struct nid_path *mix)
21663214b966STakashi Iwai {
21673214b966STakashi Iwai 	if (do_mix) {
21683214b966STakashi Iwai 		activate_output_path(codec, nomix, false, false);
21693214b966STakashi Iwai 		activate_output_path(codec, mix, true, false);
21703214b966STakashi Iwai 	} else {
21713214b966STakashi Iwai 		activate_output_path(codec, mix, false, false);
21723214b966STakashi Iwai 		activate_output_path(codec, nomix, true, false);
21733214b966STakashi Iwai 	}
21743214b966STakashi Iwai }
21753214b966STakashi Iwai 
21763214b966STakashi Iwai static int via_aamix_ctl_put(struct snd_kcontrol *kcontrol,
21773214b966STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
21783214b966STakashi Iwai {
21793214b966STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
21803214b966STakashi Iwai 	struct via_spec *spec = codec->spec;
21813214b966STakashi Iwai 	unsigned int val = ucontrol->value.enumerated.item[0];
21823214b966STakashi Iwai 
21833214b966STakashi Iwai 	if (val == spec->aamix_mode)
21843214b966STakashi Iwai 		return 0;
21853214b966STakashi Iwai 	spec->aamix_mode = val;
21863214b966STakashi Iwai 	/* update front path */
21873214b966STakashi Iwai 	update_aamix_paths(codec, val, &spec->out_path[0], &spec->out_mix_path);
21883214b966STakashi Iwai 	/* update HP path */
21893214b966STakashi Iwai 	if (!spec->hp_independent_mode) {
21903214b966STakashi Iwai 		update_aamix_paths(codec, val, &spec->hp_path,
21913214b966STakashi Iwai 				   &spec->hp_mix_path);
21923214b966STakashi Iwai 	}
21933214b966STakashi Iwai 	/* update speaker path */
21943214b966STakashi Iwai 	update_aamix_paths(codec, val, &spec->speaker_path,
21953214b966STakashi Iwai 			   &spec->speaker_mix_path);
21963214b966STakashi Iwai 	return 1;
21973214b966STakashi Iwai }
21983214b966STakashi Iwai 
21993214b966STakashi Iwai static const struct snd_kcontrol_new via_aamix_ctl_enum = {
22003214b966STakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
22013214b966STakashi Iwai 	.name = "Loopback Mixing",
22023214b966STakashi Iwai 	.info = via_aamix_ctl_info,
22033214b966STakashi Iwai 	.get = via_aamix_ctl_get,
22043214b966STakashi Iwai 	.put = via_aamix_ctl_put,
22053214b966STakashi Iwai };
22063214b966STakashi Iwai 
22073214b966STakashi Iwai static int via_auto_create_loopback_switch(struct hda_codec *codec)
22083214b966STakashi Iwai {
22093214b966STakashi Iwai 	struct via_spec *spec = codec->spec;
22103214b966STakashi Iwai 
2211*4808d12dSTakashi Iwai 	if (!spec->aa_mix_nid)
2212*4808d12dSTakashi Iwai 		return 0; /* no loopback switching available */
2213*4808d12dSTakashi Iwai 	if (!(spec->out_mix_path.depth || spec->hp_mix_path.depth ||
2214*4808d12dSTakashi Iwai 	      spec->speaker_path.depth))
22153214b966STakashi Iwai 		return 0; /* no loopback switching available */
22163214b966STakashi Iwai 	if (!via_clone_control(spec, &via_aamix_ctl_enum))
22173214b966STakashi Iwai 		return -ENOMEM;
22184a918ffeSTakashi Iwai 	return 0;
22194a918ffeSTakashi Iwai }
22204a918ffeSTakashi Iwai 
2221a766d0d7STakashi Iwai /* look for ADCs */
2222a766d0d7STakashi Iwai static int via_fill_adcs(struct hda_codec *codec)
2223a766d0d7STakashi Iwai {
2224a766d0d7STakashi Iwai 	struct via_spec *spec = codec->spec;
2225a766d0d7STakashi Iwai 	hda_nid_t nid = codec->start_nid;
2226a766d0d7STakashi Iwai 	int i;
2227a766d0d7STakashi Iwai 
2228a766d0d7STakashi Iwai 	for (i = 0; i < codec->num_nodes; i++, nid++) {
2229a766d0d7STakashi Iwai 		unsigned int wcaps = get_wcaps(codec, nid);
2230a766d0d7STakashi Iwai 		if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2231a766d0d7STakashi Iwai 			continue;
2232a766d0d7STakashi Iwai 		if (wcaps & AC_WCAP_DIGITAL)
2233a766d0d7STakashi Iwai 			continue;
2234a766d0d7STakashi Iwai 		if (!(wcaps & AC_WCAP_CONN_LIST))
2235a766d0d7STakashi Iwai 			continue;
2236a766d0d7STakashi Iwai 		if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
2237a766d0d7STakashi Iwai 			return -ENOMEM;
2238a766d0d7STakashi Iwai 		spec->adc_nids[spec->num_adc_nids++] = nid;
2239a766d0d7STakashi Iwai 	}
2240a766d0d7STakashi Iwai 	return 0;
2241a766d0d7STakashi Iwai }
2242a766d0d7STakashi Iwai 
2243a86a88eaSTakashi Iwai /* input-src control */
2244a86a88eaSTakashi Iwai static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
2245a86a88eaSTakashi Iwai 			     struct snd_ctl_elem_info *uinfo)
2246a86a88eaSTakashi Iwai {
2247a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2248a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2249a86a88eaSTakashi Iwai 
2250a86a88eaSTakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2251a86a88eaSTakashi Iwai 	uinfo->count = 1;
2252a86a88eaSTakashi Iwai 	uinfo->value.enumerated.items = spec->num_inputs;
2253a86a88eaSTakashi Iwai 	if (uinfo->value.enumerated.item >= spec->num_inputs)
2254a86a88eaSTakashi Iwai 		uinfo->value.enumerated.item = spec->num_inputs - 1;
2255a86a88eaSTakashi Iwai 	strcpy(uinfo->value.enumerated.name,
2256a86a88eaSTakashi Iwai 	       spec->inputs[uinfo->value.enumerated.item].label);
2257a86a88eaSTakashi Iwai 	return 0;
2258a86a88eaSTakashi Iwai }
2259a86a88eaSTakashi Iwai 
2260a86a88eaSTakashi Iwai static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
2261a86a88eaSTakashi Iwai 			    struct snd_ctl_elem_value *ucontrol)
2262a86a88eaSTakashi Iwai {
2263a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2264a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2265a86a88eaSTakashi Iwai 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2266a86a88eaSTakashi Iwai 
2267a86a88eaSTakashi Iwai 	ucontrol->value.enumerated.item[0] = spec->cur_mux[idx];
2268a86a88eaSTakashi Iwai 	return 0;
2269a86a88eaSTakashi Iwai }
2270a86a88eaSTakashi Iwai 
2271a86a88eaSTakashi Iwai static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
2272a86a88eaSTakashi Iwai 			    struct snd_ctl_elem_value *ucontrol)
2273a86a88eaSTakashi Iwai {
2274a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2275a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2276a86a88eaSTakashi Iwai 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2277a86a88eaSTakashi Iwai 	hda_nid_t mux;
2278a86a88eaSTakashi Iwai 	int cur;
2279a86a88eaSTakashi Iwai 
2280a86a88eaSTakashi Iwai 	cur = ucontrol->value.enumerated.item[0];
2281a86a88eaSTakashi Iwai 	if (cur < 0 || cur >= spec->num_inputs)
2282a86a88eaSTakashi Iwai 		return -EINVAL;
2283a86a88eaSTakashi Iwai 	if (spec->cur_mux[idx] == cur)
2284a86a88eaSTakashi Iwai 		return 0;
2285a86a88eaSTakashi Iwai 	spec->cur_mux[idx] = cur;
2286a86a88eaSTakashi Iwai 	if (spec->dyn_adc_switch) {
2287a86a88eaSTakashi Iwai 		int adc_idx = spec->inputs[cur].adc_idx;
2288a86a88eaSTakashi Iwai 		mux = spec->mux_nids[adc_idx];
2289a86a88eaSTakashi Iwai 		via_dyn_adc_pcm_resetup(codec, cur);
2290a86a88eaSTakashi Iwai 	} else {
2291a86a88eaSTakashi Iwai 		mux = spec->mux_nids[idx];
2292a86a88eaSTakashi Iwai 		if (snd_BUG_ON(!mux))
2293a86a88eaSTakashi Iwai 			return -EINVAL;
2294a86a88eaSTakashi Iwai 	}
2295a86a88eaSTakashi Iwai 
2296a86a88eaSTakashi Iwai 	if (mux) {
2297a86a88eaSTakashi Iwai 		/* switch to D0 beofre change index */
2298a86a88eaSTakashi Iwai 		if (snd_hda_codec_read(codec, mux, 0,
2299a86a88eaSTakashi Iwai 			       AC_VERB_GET_POWER_STATE, 0x00) != AC_PWRST_D0)
2300a86a88eaSTakashi Iwai 			snd_hda_codec_write(codec, mux, 0,
2301a86a88eaSTakashi Iwai 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
2302a86a88eaSTakashi Iwai 		snd_hda_codec_write(codec, mux, 0,
2303a86a88eaSTakashi Iwai 				    AC_VERB_SET_CONNECT_SEL,
2304a86a88eaSTakashi Iwai 				    spec->inputs[cur].mux_idx);
2305a86a88eaSTakashi Iwai 	}
2306a86a88eaSTakashi Iwai 
2307a86a88eaSTakashi Iwai 	/* update jack power state */
2308a86a88eaSTakashi Iwai 	set_widgets_power_state(codec);
2309a86a88eaSTakashi Iwai 	return 0;
2310a86a88eaSTakashi Iwai }
2311a766d0d7STakashi Iwai 
2312d7a99cceSTakashi Iwai static const struct snd_kcontrol_new via_input_src_ctl = {
2313d7a99cceSTakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2314d7a99cceSTakashi Iwai 	/* The multiple "Capture Source" controls confuse alsamixer
2315d7a99cceSTakashi Iwai 	 * So call somewhat different..
2316d7a99cceSTakashi Iwai 	 */
2317d7a99cceSTakashi Iwai 	/* .name = "Capture Source", */
2318d7a99cceSTakashi Iwai 	.name = "Input Source",
2319d7a99cceSTakashi Iwai 	.info = via_mux_enum_info,
2320d7a99cceSTakashi Iwai 	.get = via_mux_enum_get,
2321d7a99cceSTakashi Iwai 	.put = via_mux_enum_put,
2322d7a99cceSTakashi Iwai };
2323d7a99cceSTakashi Iwai 
2324a86a88eaSTakashi Iwai static int create_input_src_ctls(struct hda_codec *codec, int count)
2325a86a88eaSTakashi Iwai {
2326a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2327a86a88eaSTakashi Iwai 	struct snd_kcontrol_new *knew;
2328a86a88eaSTakashi Iwai 
2329a86a88eaSTakashi Iwai 	if (spec->num_inputs <= 1 || !count)
2330a86a88eaSTakashi Iwai 		return 0; /* no need for single src */
2331a86a88eaSTakashi Iwai 
2332a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_input_src_ctl);
2333a86a88eaSTakashi Iwai 	if (!knew)
2334a86a88eaSTakashi Iwai 		return -ENOMEM;
2335a86a88eaSTakashi Iwai 	knew->count = count;
2336a86a88eaSTakashi Iwai 	return 0;
2337a86a88eaSTakashi Iwai }
2338a86a88eaSTakashi Iwai 
2339a86a88eaSTakashi Iwai /* add the powersave loopback-list entry */
234013af8e77STakashi Iwai static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
234113af8e77STakashi Iwai {
234213af8e77STakashi Iwai 	struct hda_amp_list *list;
234313af8e77STakashi Iwai 
234413af8e77STakashi Iwai 	if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
234513af8e77STakashi Iwai 		return;
234613af8e77STakashi Iwai 	list = spec->loopback_list + spec->num_loopbacks;
234713af8e77STakashi Iwai 	list->nid = mix;
234813af8e77STakashi Iwai 	list->dir = HDA_INPUT;
234913af8e77STakashi Iwai 	list->idx = idx;
235013af8e77STakashi Iwai 	spec->num_loopbacks++;
235113af8e77STakashi Iwai 	spec->loopback.amplist = spec->loopback_list;
235213af8e77STakashi Iwai }
235313af8e77STakashi Iwai 
2354a86a88eaSTakashi Iwai static bool is_reachable_nid(struct hda_codec *codec, hda_nid_t src,
23558d087c76STakashi Iwai 			     hda_nid_t dst)
2356a86a88eaSTakashi Iwai {
23578d087c76STakashi Iwai 	return snd_hda_get_conn_index(codec, src, dst, 1) >= 0;
2358a86a88eaSTakashi Iwai }
2359a86a88eaSTakashi Iwai 
2360a86a88eaSTakashi Iwai /* add the input-route to the given pin */
2361a86a88eaSTakashi Iwai static bool add_input_route(struct hda_codec *codec, hda_nid_t pin)
2362c577b8a1SJoseph Chan {
236310a20af7STakashi Iwai 	struct via_spec *spec = codec->spec;
2364a86a88eaSTakashi Iwai 	int c, idx;
2365a86a88eaSTakashi Iwai 
2366a86a88eaSTakashi Iwai 	spec->inputs[spec->num_inputs].adc_idx = -1;
2367a86a88eaSTakashi Iwai 	spec->inputs[spec->num_inputs].pin = pin;
2368a86a88eaSTakashi Iwai 	for (c = 0; c < spec->num_adc_nids; c++) {
2369a86a88eaSTakashi Iwai 		if (spec->mux_nids[c]) {
2370a86a88eaSTakashi Iwai 			idx = get_connection_index(codec, spec->mux_nids[c],
2371a86a88eaSTakashi Iwai 						   pin);
2372a86a88eaSTakashi Iwai 			if (idx < 0)
2373a86a88eaSTakashi Iwai 				continue;
2374a86a88eaSTakashi Iwai 			spec->inputs[spec->num_inputs].mux_idx = idx;
2375a86a88eaSTakashi Iwai 		} else {
23768d087c76STakashi Iwai 			if (!is_reachable_nid(codec, spec->adc_nids[c], pin))
2377a86a88eaSTakashi Iwai 				continue;
2378a86a88eaSTakashi Iwai 		}
2379a86a88eaSTakashi Iwai 		spec->inputs[spec->num_inputs].adc_idx = c;
2380a86a88eaSTakashi Iwai 		/* Can primary ADC satisfy all inputs? */
2381a86a88eaSTakashi Iwai 		if (!spec->dyn_adc_switch &&
2382a86a88eaSTakashi Iwai 		    spec->num_inputs > 0 && spec->inputs[0].adc_idx != c) {
2383a86a88eaSTakashi Iwai 			snd_printd(KERN_INFO
2384a86a88eaSTakashi Iwai 				   "via: dynamic ADC switching enabled\n");
2385a86a88eaSTakashi Iwai 			spec->dyn_adc_switch = 1;
2386a86a88eaSTakashi Iwai 		}
2387a86a88eaSTakashi Iwai 		return true;
2388a86a88eaSTakashi Iwai 	}
2389a86a88eaSTakashi Iwai 	return false;
2390a86a88eaSTakashi Iwai }
2391a86a88eaSTakashi Iwai 
2392a86a88eaSTakashi Iwai static int get_mux_nids(struct hda_codec *codec);
2393a86a88eaSTakashi Iwai 
2394a86a88eaSTakashi Iwai /* parse input-routes; fill ADCs, MUXs and input-src entries */
2395a86a88eaSTakashi Iwai static int parse_analog_inputs(struct hda_codec *codec)
2396a86a88eaSTakashi Iwai {
2397a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2398a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2399a86a88eaSTakashi Iwai 	int i, err;
2400a766d0d7STakashi Iwai 
2401a766d0d7STakashi Iwai 	err = via_fill_adcs(codec);
2402a766d0d7STakashi Iwai 	if (err < 0)
2403a766d0d7STakashi Iwai 		return err;
2404a766d0d7STakashi Iwai 	err = get_mux_nids(codec);
2405a766d0d7STakashi Iwai 	if (err < 0)
2406a766d0d7STakashi Iwai 		return err;
2407a766d0d7STakashi Iwai 
2408a86a88eaSTakashi Iwai 	/* fill all input-routes */
2409a86a88eaSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2410a86a88eaSTakashi Iwai 		if (add_input_route(codec, cfg->inputs[i].pin))
2411a86a88eaSTakashi Iwai 			spec->inputs[spec->num_inputs++].label =
2412a86a88eaSTakashi Iwai 				hda_get_autocfg_input_label(codec, cfg, i);
2413a86a88eaSTakashi Iwai 	}
2414a86a88eaSTakashi Iwai 
2415a86a88eaSTakashi Iwai 	/* check for internal loopback recording */
2416a86a88eaSTakashi Iwai 	if (spec->aa_mix_nid &&
2417a86a88eaSTakashi Iwai 	    add_input_route(codec, spec->aa_mix_nid))
2418a86a88eaSTakashi Iwai 		spec->inputs[spec->num_inputs++].label = "Stereo Mixer";
2419a86a88eaSTakashi Iwai 
2420a86a88eaSTakashi Iwai 	return 0;
2421a86a88eaSTakashi Iwai }
2422a86a88eaSTakashi Iwai 
2423a86a88eaSTakashi Iwai /* create analog-loopback volume/switch controls */
2424a86a88eaSTakashi Iwai static int create_loopback_ctls(struct hda_codec *codec)
2425a86a88eaSTakashi Iwai {
2426a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2427a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2428a86a88eaSTakashi Iwai 	const char *prev_label = NULL;
2429a86a88eaSTakashi Iwai 	int type_idx = 0;
2430a86a88eaSTakashi Iwai 	int i, j, err, idx;
2431a86a88eaSTakashi Iwai 
2432a86a88eaSTakashi Iwai 	if (!spec->aa_mix_nid)
2433a766d0d7STakashi Iwai 		return 0;
2434c577b8a1SJoseph Chan 
24357b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2436a86a88eaSTakashi Iwai 		hda_nid_t pin = cfg->inputs[i].pin;
2437a86a88eaSTakashi Iwai 		const char *label = hda_get_autocfg_input_label(codec, cfg, i);
2438a86a88eaSTakashi Iwai 
24391e11cae1STakashi Iwai 		if (prev_label && !strcmp(label, prev_label))
24407b315bb4STakashi Iwai 			type_idx++;
24417b315bb4STakashi Iwai 		else
24427b315bb4STakashi Iwai 			type_idx = 0;
24431e11cae1STakashi Iwai 		prev_label = label;
2444a86a88eaSTakashi Iwai 		idx = get_connection_index(codec, spec->aa_mix_nid, pin);
2445a86a88eaSTakashi Iwai 		if (idx >= 0) {
244616922281SLydia Wang 			err = via_new_analog_input(spec, label, type_idx,
2447a86a88eaSTakashi Iwai 						   idx, spec->aa_mix_nid);
2448c577b8a1SJoseph Chan 			if (err < 0)
2449c577b8a1SJoseph Chan 				return err;
2450a86a88eaSTakashi Iwai 			add_loopback_list(spec, spec->aa_mix_nid, idx);
245113af8e77STakashi Iwai 		}
2452e3d7a143STakashi Iwai 
2453e3d7a143STakashi Iwai 		/* remember the label for smart51 control */
2454e3d7a143STakashi Iwai 		for (j = 0; j < spec->smart51_nums; j++) {
2455a86a88eaSTakashi Iwai 			if (spec->smart51_pins[j] == pin) {
2456e3d7a143STakashi Iwai 				spec->smart51_idxs[j] = idx;
2457e3d7a143STakashi Iwai 				spec->smart51_labels[j] = label;
2458e3d7a143STakashi Iwai 				break;
2459e3d7a143STakashi Iwai 			}
2460e3d7a143STakashi Iwai 		}
2461c577b8a1SJoseph Chan 	}
2462a86a88eaSTakashi Iwai 	return 0;
2463a86a88eaSTakashi Iwai }
2464a86a88eaSTakashi Iwai 
2465a86a88eaSTakashi Iwai /* create mic-boost controls (if present) */
2466a86a88eaSTakashi Iwai static int create_mic_boost_ctls(struct hda_codec *codec)
2467a86a88eaSTakashi Iwai {
2468a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2469a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2470a86a88eaSTakashi Iwai 	int i, err;
2471a86a88eaSTakashi Iwai 
2472a86a88eaSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2473a86a88eaSTakashi Iwai 		hda_nid_t pin = cfg->inputs[i].pin;
2474a86a88eaSTakashi Iwai 		unsigned int caps;
2475a86a88eaSTakashi Iwai 		const char *label;
2476a86a88eaSTakashi Iwai 		char name[32];
2477a86a88eaSTakashi Iwai 
2478a86a88eaSTakashi Iwai 		if (cfg->inputs[i].type != AUTO_PIN_MIC)
2479a86a88eaSTakashi Iwai 			continue;
2480a86a88eaSTakashi Iwai 		caps = query_amp_caps(codec, pin, HDA_INPUT);
2481a86a88eaSTakashi Iwai 		if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
2482a86a88eaSTakashi Iwai 			continue;
2483a86a88eaSTakashi Iwai 		label = hda_get_autocfg_input_label(codec, cfg, i);
2484a86a88eaSTakashi Iwai 		snprintf(name, sizeof(name), "%s Boost Volume", label);
2485a86a88eaSTakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2486a86a88eaSTakashi Iwai 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
2487a86a88eaSTakashi Iwai 		if (err < 0)
2488a86a88eaSTakashi Iwai 			return err;
2489a86a88eaSTakashi Iwai 	}
2490a86a88eaSTakashi Iwai 	return 0;
2491a86a88eaSTakashi Iwai }
2492a86a88eaSTakashi Iwai 
2493a86a88eaSTakashi Iwai /* create capture and input-src controls for multiple streams */
2494a86a88eaSTakashi Iwai static int create_multi_adc_ctls(struct hda_codec *codec)
2495a86a88eaSTakashi Iwai {
2496a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2497a86a88eaSTakashi Iwai 	int i, err;
2498d7a99cceSTakashi Iwai 
2499d7a99cceSTakashi Iwai 	/* create capture mixer elements */
2500d7a99cceSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
2501d7a99cceSTakashi Iwai 		hda_nid_t adc = spec->adc_nids[i];
2502d7a99cceSTakashi Iwai 		err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
2503d7a99cceSTakashi Iwai 					"Capture Volume", i,
2504d7a99cceSTakashi Iwai 					HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2505d7a99cceSTakashi Iwai 							    HDA_INPUT));
2506d7a99cceSTakashi Iwai 		if (err < 0)
2507d7a99cceSTakashi Iwai 			return err;
2508d7a99cceSTakashi Iwai 		err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2509d7a99cceSTakashi Iwai 					"Capture Switch", i,
2510d7a99cceSTakashi Iwai 					HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2511d7a99cceSTakashi Iwai 							    HDA_INPUT));
2512d7a99cceSTakashi Iwai 		if (err < 0)
2513d7a99cceSTakashi Iwai 			return err;
2514d7a99cceSTakashi Iwai 	}
2515d7a99cceSTakashi Iwai 
2516d7a99cceSTakashi Iwai 	/* input-source control */
2517d7a99cceSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++)
2518d7a99cceSTakashi Iwai 		if (!spec->mux_nids[i])
2519d7a99cceSTakashi Iwai 			break;
2520a86a88eaSTakashi Iwai 	err = create_input_src_ctls(codec, i);
2521d7a99cceSTakashi Iwai 	if (err < 0)
2522d7a99cceSTakashi Iwai 		return err;
2523a86a88eaSTakashi Iwai 	return 0;
2524d7a99cceSTakashi Iwai }
2525d7a99cceSTakashi Iwai 
2526a86a88eaSTakashi Iwai /* bind capture volume/switch */
2527a86a88eaSTakashi Iwai static struct snd_kcontrol_new via_bind_cap_vol_ctl =
2528a86a88eaSTakashi Iwai 	HDA_BIND_VOL("Capture Volume", 0);
2529a86a88eaSTakashi Iwai static struct snd_kcontrol_new via_bind_cap_sw_ctl =
2530a86a88eaSTakashi Iwai 	HDA_BIND_SW("Capture Switch", 0);
2531a86a88eaSTakashi Iwai 
2532a86a88eaSTakashi Iwai static int init_bind_ctl(struct via_spec *spec, struct hda_bind_ctls **ctl_ret,
2533a86a88eaSTakashi Iwai 			 struct hda_ctl_ops *ops)
2534a86a88eaSTakashi Iwai {
2535a86a88eaSTakashi Iwai 	struct hda_bind_ctls *ctl;
2536a86a88eaSTakashi Iwai 	int i;
2537a86a88eaSTakashi Iwai 
2538a86a88eaSTakashi Iwai 	ctl = kzalloc(sizeof(*ctl) + sizeof(long) * 4, GFP_KERNEL);
2539a86a88eaSTakashi Iwai 	if (!ctl)
2540a86a88eaSTakashi Iwai 		return -ENOMEM;
2541a86a88eaSTakashi Iwai 	ctl->ops = ops;
2542a86a88eaSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++)
2543a86a88eaSTakashi Iwai 		ctl->values[i] =
2544a86a88eaSTakashi Iwai 			HDA_COMPOSE_AMP_VAL(spec->adc_nids[i], 3, 0, HDA_INPUT);
2545a86a88eaSTakashi Iwai 	*ctl_ret = ctl;
2546a86a88eaSTakashi Iwai 	return 0;
2547a86a88eaSTakashi Iwai }
2548a86a88eaSTakashi Iwai 
2549a86a88eaSTakashi Iwai /* create capture and input-src controls for dynamic ADC-switch case */
2550a86a88eaSTakashi Iwai static int create_dyn_adc_ctls(struct hda_codec *codec)
2551a86a88eaSTakashi Iwai {
2552a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2553a86a88eaSTakashi Iwai 	struct snd_kcontrol_new *knew;
2554a86a88eaSTakashi Iwai 	int err;
2555a86a88eaSTakashi Iwai 
2556a86a88eaSTakashi Iwai 	/* set up the bind capture ctls */
2557a86a88eaSTakashi Iwai 	err = init_bind_ctl(spec, &spec->bind_cap_vol, &snd_hda_bind_vol);
2558a86a88eaSTakashi Iwai 	if (err < 0)
2559a86a88eaSTakashi Iwai 		return err;
2560a86a88eaSTakashi Iwai 	err = init_bind_ctl(spec, &spec->bind_cap_sw, &snd_hda_bind_sw);
2561a86a88eaSTakashi Iwai 	if (err < 0)
2562a86a88eaSTakashi Iwai 		return err;
2563a86a88eaSTakashi Iwai 
2564a86a88eaSTakashi Iwai 	/* create capture mixer elements */
2565a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_bind_cap_vol_ctl);
2566a86a88eaSTakashi Iwai 	if (!knew)
2567a86a88eaSTakashi Iwai 		return -ENOMEM;
2568a86a88eaSTakashi Iwai 	knew->private_value = (long)spec->bind_cap_vol;
2569a86a88eaSTakashi Iwai 
2570a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_bind_cap_sw_ctl);
2571a86a88eaSTakashi Iwai 	if (!knew)
2572a86a88eaSTakashi Iwai 		return -ENOMEM;
2573a86a88eaSTakashi Iwai 	knew->private_value = (long)spec->bind_cap_sw;
2574a86a88eaSTakashi Iwai 
2575a86a88eaSTakashi Iwai 	/* input-source control */
2576a86a88eaSTakashi Iwai 	err = create_input_src_ctls(codec, 1);
2577a86a88eaSTakashi Iwai 	if (err < 0)
2578a86a88eaSTakashi Iwai 		return err;
2579a86a88eaSTakashi Iwai 	return 0;
2580a86a88eaSTakashi Iwai }
2581a86a88eaSTakashi Iwai 
2582a86a88eaSTakashi Iwai /* parse and create capture-related stuff */
2583a86a88eaSTakashi Iwai static int via_auto_create_analog_input_ctls(struct hda_codec *codec)
2584a86a88eaSTakashi Iwai {
2585a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2586a86a88eaSTakashi Iwai 	int err;
2587a86a88eaSTakashi Iwai 
2588a86a88eaSTakashi Iwai 	err = parse_analog_inputs(codec);
2589a86a88eaSTakashi Iwai 	if (err < 0)
2590a86a88eaSTakashi Iwai 		return err;
2591a86a88eaSTakashi Iwai 	if (spec->dyn_adc_switch)
2592a86a88eaSTakashi Iwai 		err = create_dyn_adc_ctls(codec);
2593a86a88eaSTakashi Iwai 	else
2594a86a88eaSTakashi Iwai 		err = create_multi_adc_ctls(codec);
2595a86a88eaSTakashi Iwai 	if (err < 0)
2596a86a88eaSTakashi Iwai 		return err;
2597a86a88eaSTakashi Iwai 	err = create_loopback_ctls(codec);
2598a86a88eaSTakashi Iwai 	if (err < 0)
2599a86a88eaSTakashi Iwai 		return err;
2600a86a88eaSTakashi Iwai 	err = create_mic_boost_ctls(codec);
2601a86a88eaSTakashi Iwai 	if (err < 0)
2602a86a88eaSTakashi Iwai 		return err;
2603c577b8a1SJoseph Chan 	return 0;
2604c577b8a1SJoseph Chan }
2605c577b8a1SJoseph Chan 
260676d9b0ddSHarald Welte static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
260776d9b0ddSHarald Welte {
260876d9b0ddSHarald Welte 	unsigned int def_conf;
260976d9b0ddSHarald Welte 	unsigned char seqassoc;
261076d9b0ddSHarald Welte 
26112f334f92STakashi Iwai 	def_conf = snd_hda_codec_get_pincfg(codec, nid);
261276d9b0ddSHarald Welte 	seqassoc = (unsigned char) get_defcfg_association(def_conf);
261376d9b0ddSHarald Welte 	seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
261482ef9e45SLydia Wang 	if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
261582ef9e45SLydia Wang 	    && (seqassoc == 0xf0 || seqassoc == 0xff)) {
261676d9b0ddSHarald Welte 		def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
26172f334f92STakashi Iwai 		snd_hda_codec_set_pincfg(codec, nid, def_conf);
261876d9b0ddSHarald Welte 	}
261976d9b0ddSHarald Welte 
262076d9b0ddSHarald Welte 	return;
262176d9b0ddSHarald Welte }
262276d9b0ddSHarald Welte 
2623e06e5a29STakashi Iwai static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
26241f2e99feSLydia Wang 				     struct snd_ctl_elem_value *ucontrol)
26251f2e99feSLydia Wang {
26261f2e99feSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
26271f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
26281f2e99feSLydia Wang 
26291f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
26301f2e99feSLydia Wang 		return 0;
2631e06e5a29STakashi Iwai 	ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
26321f2e99feSLydia Wang 	return 0;
26331f2e99feSLydia Wang }
26341f2e99feSLydia Wang 
2635e06e5a29STakashi Iwai static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
26361f2e99feSLydia Wang 				     struct snd_ctl_elem_value *ucontrol)
26371f2e99feSLydia Wang {
26381f2e99feSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
26391f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
2640187d333eSTakashi Iwai 	int val;
26411f2e99feSLydia Wang 
26421f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
26431f2e99feSLydia Wang 		return 0;
2644187d333eSTakashi Iwai 	val = !!ucontrol->value.integer.value[0];
2645187d333eSTakashi Iwai 	if (spec->vt1708_jack_detect == val)
2646187d333eSTakashi Iwai 		return 0;
2647187d333eSTakashi Iwai 	spec->vt1708_jack_detect = val;
2648187d333eSTakashi Iwai 	if (spec->vt1708_jack_detect &&
2649187d333eSTakashi Iwai 	    snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") != 1) {
26501f2e99feSLydia Wang 		mute_aa_path(codec, 1);
26511f2e99feSLydia Wang 		notify_aa_path_ctls(codec);
26521f2e99feSLydia Wang 	}
2653187d333eSTakashi Iwai 	via_hp_automute(codec);
2654187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
2655187d333eSTakashi Iwai 	return 1;
26561f2e99feSLydia Wang }
26571f2e99feSLydia Wang 
2658e06e5a29STakashi Iwai static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
26591f2e99feSLydia Wang 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
26601f2e99feSLydia Wang 	.name = "Jack Detect",
26611f2e99feSLydia Wang 	.count = 1,
26621f2e99feSLydia Wang 	.info = snd_ctl_boolean_mono_info,
2663e06e5a29STakashi Iwai 	.get = vt1708_jack_detect_get,
2664e06e5a29STakashi Iwai 	.put = vt1708_jack_detect_put,
26651f2e99feSLydia Wang };
26661f2e99feSLydia Wang 
266712daef65STakashi Iwai static void fill_dig_outs(struct hda_codec *codec);
266812daef65STakashi Iwai static void fill_dig_in(struct hda_codec *codec);
266912daef65STakashi Iwai 
267012daef65STakashi Iwai static int via_parse_auto_config(struct hda_codec *codec)
2671c577b8a1SJoseph Chan {
2672c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
2673c577b8a1SJoseph Chan 	int err;
2674c577b8a1SJoseph Chan 
2675c577b8a1SJoseph Chan 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2676c577b8a1SJoseph Chan 	if (err < 0)
2677c577b8a1SJoseph Chan 		return err;
2678c577b8a1SJoseph Chan 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
26797f0df88cSTakashi Iwai 		return -EINVAL;
2680c577b8a1SJoseph Chan 
26814a79616dSTakashi Iwai 	err = via_auto_create_multi_out_ctls(codec);
2682c577b8a1SJoseph Chan 	if (err < 0)
2683c577b8a1SJoseph Chan 		return err;
26844a79616dSTakashi Iwai 	err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
2685c577b8a1SJoseph Chan 	if (err < 0)
2686c577b8a1SJoseph Chan 		return err;
26874a918ffeSTakashi Iwai 	err = via_auto_create_speaker_ctls(codec);
26884a918ffeSTakashi Iwai 	if (err < 0)
26894a918ffeSTakashi Iwai 		return err;
26903214b966STakashi Iwai 	err = via_auto_create_loopback_switch(codec);
26913214b966STakashi Iwai 	if (err < 0)
26923214b966STakashi Iwai 		return err;
2693a86a88eaSTakashi Iwai 	err = via_auto_create_analog_input_ctls(codec);
2694c577b8a1SJoseph Chan 	if (err < 0)
2695c577b8a1SJoseph Chan 		return err;
2696c577b8a1SJoseph Chan 
2697c577b8a1SJoseph Chan 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2698c577b8a1SJoseph Chan 
269912daef65STakashi Iwai 	fill_dig_outs(codec);
270012daef65STakashi Iwai 	fill_dig_in(codec);
2701c577b8a1SJoseph Chan 
2702603c4019STakashi Iwai 	if (spec->kctls.list)
2703603c4019STakashi Iwai 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
2704c577b8a1SJoseph Chan 
2705c577b8a1SJoseph Chan 
27063214b966STakashi Iwai 	if (spec->hp_dac_nid && spec->hp_mix_path.depth) {
2707ece8d043STakashi Iwai 		err = via_hp_build(codec);
2708ece8d043STakashi Iwai 		if (err < 0)
2709ece8d043STakashi Iwai 			return err;
2710ece8d043STakashi Iwai 	}
2711c577b8a1SJoseph Chan 
2712f4a7828bSTakashi Iwai 	err = via_smart51_build(codec);
2713f4a7828bSTakashi Iwai 	if (err < 0)
2714f4a7828bSTakashi Iwai 		return err;
2715f4a7828bSTakashi Iwai 
27165d41762aSTakashi Iwai 	/* assign slave outs */
27175d41762aSTakashi Iwai 	if (spec->slave_dig_outs[0])
27185d41762aSTakashi Iwai 		codec->slave_dig_outs = spec->slave_dig_outs;
27195d41762aSTakashi Iwai 
2720c577b8a1SJoseph Chan 	return 1;
2721c577b8a1SJoseph Chan }
2722c577b8a1SJoseph Chan 
27235d41762aSTakashi Iwai static void via_auto_init_dig_outs(struct hda_codec *codec)
2724c577b8a1SJoseph Chan {
272525eaba2fSLydia Wang 	struct via_spec *spec = codec->spec;
27265d41762aSTakashi Iwai 	if (spec->multiout.dig_out_nid)
27275d41762aSTakashi Iwai 		init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
27285d41762aSTakashi Iwai 	if (spec->slave_dig_outs[0])
27295d41762aSTakashi Iwai 		init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
27305d41762aSTakashi Iwai }
273125eaba2fSLydia Wang 
27325d41762aSTakashi Iwai static void via_auto_init_dig_in(struct hda_codec *codec)
27335d41762aSTakashi Iwai {
27345d41762aSTakashi Iwai 	struct via_spec *spec = codec->spec;
27355d41762aSTakashi Iwai 	if (!spec->dig_in_nid)
27365d41762aSTakashi Iwai 		return;
27375d41762aSTakashi Iwai 	snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
27385d41762aSTakashi Iwai 			    AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
27395d41762aSTakashi Iwai }
27405d41762aSTakashi Iwai 
27414a918ffeSTakashi Iwai /* initialize the unsolicited events */
27424a918ffeSTakashi Iwai static void via_auto_init_unsol_event(struct hda_codec *codec)
27434a918ffeSTakashi Iwai {
27444a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
27454a918ffeSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
27464a918ffeSTakashi Iwai 	unsigned int ev;
27474a918ffeSTakashi Iwai 	int i;
27484a918ffeSTakashi Iwai 
27494a918ffeSTakashi Iwai 	if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
27501835a0f9STakashi Iwai 		snd_hda_jack_detect_enable(codec, cfg->hp_pins[0],
27511835a0f9STakashi Iwai 					   VIA_HP_EVENT | VIA_JACK_EVENT);
27524a918ffeSTakashi Iwai 
27534a918ffeSTakashi Iwai 	if (cfg->speaker_pins[0])
27544a918ffeSTakashi Iwai 		ev = VIA_LINE_EVENT;
27554a918ffeSTakashi Iwai 	else
27564a918ffeSTakashi Iwai 		ev = 0;
27574a918ffeSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
27584a918ffeSTakashi Iwai 		if (cfg->line_out_pins[i] &&
27594a918ffeSTakashi Iwai 		    is_jack_detectable(codec, cfg->line_out_pins[i]))
27601835a0f9STakashi Iwai 			snd_hda_jack_detect_enable(codec, cfg->line_out_pins[i],
27611835a0f9STakashi Iwai 						   ev | VIA_JACK_EVENT);
27624a918ffeSTakashi Iwai 	}
27634a918ffeSTakashi Iwai 
27644a918ffeSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
27654a918ffeSTakashi Iwai 		if (is_jack_detectable(codec, cfg->inputs[i].pin))
27661835a0f9STakashi Iwai 			snd_hda_jack_detect_enable(codec, cfg->inputs[i].pin,
27671835a0f9STakashi Iwai 						   VIA_JACK_EVENT);
27684a918ffeSTakashi Iwai 	}
27694a918ffeSTakashi Iwai }
27704a918ffeSTakashi Iwai 
27715d41762aSTakashi Iwai static int via_init(struct hda_codec *codec)
27725d41762aSTakashi Iwai {
27735d41762aSTakashi Iwai 	struct via_spec *spec = codec->spec;
27745d41762aSTakashi Iwai 	int i;
27755d41762aSTakashi Iwai 
27765d41762aSTakashi Iwai 	for (i = 0; i < spec->num_iverbs; i++)
27775d41762aSTakashi Iwai 		snd_hda_sequence_write(codec, spec->init_verbs[i]);
27785d41762aSTakashi Iwai 
2779c577b8a1SJoseph Chan 	via_auto_init_multi_out(codec);
2780c577b8a1SJoseph Chan 	via_auto_init_hp_out(codec);
27814a918ffeSTakashi Iwai 	via_auto_init_speaker_out(codec);
2782c577b8a1SJoseph Chan 	via_auto_init_analog_input(codec);
27835d41762aSTakashi Iwai 	via_auto_init_dig_outs(codec);
27845d41762aSTakashi Iwai 	via_auto_init_dig_in(codec);
278511890956SLydia Wang 
27864a918ffeSTakashi Iwai 	via_auto_init_unsol_event(codec);
27874a918ffeSTakashi Iwai 
278825eaba2fSLydia Wang 	via_hp_automute(codec);
2789187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
279001a61e12STakashi Iwai 	snd_hda_jack_report_sync(codec);
279125eaba2fSLydia Wang 
2792c577b8a1SJoseph Chan 	return 0;
2793c577b8a1SJoseph Chan }
2794c577b8a1SJoseph Chan 
27951f2e99feSLydia Wang static void vt1708_update_hp_jack_state(struct work_struct *work)
27961f2e99feSLydia Wang {
27971f2e99feSLydia Wang 	struct via_spec *spec = container_of(work, struct via_spec,
27981f2e99feSLydia Wang 					     vt1708_hp_work.work);
27991f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
28001f2e99feSLydia Wang 		return;
28011835a0f9STakashi Iwai 	snd_hda_jack_set_dirty_all(spec->codec);
28021f2e99feSLydia Wang 	/* if jack state toggled */
28031f2e99feSLydia Wang 	if (spec->vt1708_hp_present
2804d56757abSTakashi Iwai 	    != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
28051f2e99feSLydia Wang 		spec->vt1708_hp_present ^= 1;
28061f2e99feSLydia Wang 		via_hp_automute(spec->codec);
28071f2e99feSLydia Wang 	}
2808187d333eSTakashi Iwai 	if (spec->vt1708_jack_detect)
2809187d333eSTakashi Iwai 		schedule_delayed_work(&spec->vt1708_hp_work,
2810187d333eSTakashi Iwai 				      msecs_to_jiffies(100));
28111f2e99feSLydia Wang }
28121f2e99feSLydia Wang 
2813337b9d02STakashi Iwai static int get_mux_nids(struct hda_codec *codec)
2814337b9d02STakashi Iwai {
2815337b9d02STakashi Iwai 	struct via_spec *spec = codec->spec;
2816337b9d02STakashi Iwai 	hda_nid_t nid, conn[8];
2817337b9d02STakashi Iwai 	unsigned int type;
2818337b9d02STakashi Iwai 	int i, n;
2819337b9d02STakashi Iwai 
2820337b9d02STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
2821337b9d02STakashi Iwai 		nid = spec->adc_nids[i];
2822337b9d02STakashi Iwai 		while (nid) {
2823a22d543aSTakashi Iwai 			type = get_wcaps_type(get_wcaps(codec, nid));
28241c55d521STakashi Iwai 			if (type == AC_WID_PIN)
28251c55d521STakashi Iwai 				break;
2826337b9d02STakashi Iwai 			n = snd_hda_get_connections(codec, nid, conn,
2827337b9d02STakashi Iwai 						    ARRAY_SIZE(conn));
2828337b9d02STakashi Iwai 			if (n <= 0)
2829337b9d02STakashi Iwai 				break;
2830337b9d02STakashi Iwai 			if (n > 1) {
2831337b9d02STakashi Iwai 				spec->mux_nids[i] = nid;
2832337b9d02STakashi Iwai 				break;
2833337b9d02STakashi Iwai 			}
2834337b9d02STakashi Iwai 			nid = conn[0];
2835337b9d02STakashi Iwai 		}
2836337b9d02STakashi Iwai 	}
28371c55d521STakashi Iwai 	return 0;
2838337b9d02STakashi Iwai }
2839337b9d02STakashi Iwai 
2840c577b8a1SJoseph Chan static int patch_vt1708(struct hda_codec *codec)
2841c577b8a1SJoseph Chan {
2842c577b8a1SJoseph Chan 	struct via_spec *spec;
2843c577b8a1SJoseph Chan 	int err;
2844c577b8a1SJoseph Chan 
2845c577b8a1SJoseph Chan 	/* create a codec specific record */
28465b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2847c577b8a1SJoseph Chan 	if (spec == NULL)
2848c577b8a1SJoseph Chan 		return -ENOMEM;
2849c577b8a1SJoseph Chan 
2850620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x17;
2851620e2b28STakashi Iwai 
285212daef65STakashi Iwai 	/* Add HP and CD pin config connect bit re-config action */
285312daef65STakashi Iwai 	vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
285412daef65STakashi Iwai 	vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
285512daef65STakashi Iwai 
2856c577b8a1SJoseph Chan 	/* automatic parse from the BIOS config */
285712daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2858c577b8a1SJoseph Chan 	if (err < 0) {
2859c577b8a1SJoseph Chan 		via_free(codec);
2860c577b8a1SJoseph Chan 		return err;
2861c577b8a1SJoseph Chan 	}
2862c577b8a1SJoseph Chan 
286312daef65STakashi Iwai 	/* add jack detect on/off control */
286412daef65STakashi Iwai 	if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
286512daef65STakashi Iwai 		return -ENOMEM;
286612daef65STakashi Iwai 
2867bc9b5623STakashi Iwai 	/* disable 32bit format on VT1708 */
2868bc9b5623STakashi Iwai 	if (codec->vendor_id == 0x11061708)
2869bc9b5623STakashi Iwai 		spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
2870c577b8a1SJoseph Chan 
2871e322a36dSLydia Wang 	spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
2872e322a36dSLydia Wang 
2873c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
2874c577b8a1SJoseph Chan 
28751f2e99feSLydia Wang 	INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
2876c577b8a1SJoseph Chan 	return 0;
2877c577b8a1SJoseph Chan }
2878c577b8a1SJoseph Chan 
2879ddd304d8STakashi Iwai static int patch_vt1709(struct hda_codec *codec)
2880c577b8a1SJoseph Chan {
2881c577b8a1SJoseph Chan 	struct via_spec *spec;
2882c577b8a1SJoseph Chan 	int err;
2883c577b8a1SJoseph Chan 
2884c577b8a1SJoseph Chan 	/* create a codec specific record */
28855b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2886c577b8a1SJoseph Chan 	if (spec == NULL)
2887c577b8a1SJoseph Chan 		return -ENOMEM;
2888c577b8a1SJoseph Chan 
2889620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x18;
2890620e2b28STakashi Iwai 
289112daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2892c577b8a1SJoseph Chan 	if (err < 0) {
2893c577b8a1SJoseph Chan 		via_free(codec);
2894c577b8a1SJoseph Chan 		return err;
2895c577b8a1SJoseph Chan 	}
2896c577b8a1SJoseph Chan 
2897c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
2898c577b8a1SJoseph Chan 
2899f7278fd0SJosepch Chan 	return 0;
2900f7278fd0SJosepch Chan }
2901f7278fd0SJosepch Chan 
29023e95b9abSLydia Wang static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
29033e95b9abSLydia Wang {
29043e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
29053e95b9abSLydia Wang 	int imux_is_smixer;
29063e95b9abSLydia Wang 	unsigned int parm;
29073e95b9abSLydia Wang 	int is_8ch = 0;
2908bc92df7fSLydia Wang 	if ((spec->codec_type != VT1708B_4CH) &&
2909bc92df7fSLydia Wang 	    (codec->vendor_id != 0x11064397))
29103e95b9abSLydia Wang 		is_8ch = 1;
29113e95b9abSLydia Wang 
29123e95b9abSLydia Wang 	/* SW0 (17h) = stereo mixer */
29133e95b9abSLydia Wang 	imux_is_smixer =
29143e95b9abSLydia Wang 	(snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
29153e95b9abSLydia Wang 	 == ((spec->codec_type == VT1708S) ? 5 : 0));
29163e95b9abSLydia Wang 	/* inputs */
29173e95b9abSLydia Wang 	/* PW 1/2/5 (1ah/1bh/1eh) */
29183e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29193e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1a, &parm);
29203e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1b, &parm);
29213e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
29223e95b9abSLydia Wang 	if (imux_is_smixer)
29233e95b9abSLydia Wang 		parm = AC_PWRST_D0;
29243e95b9abSLydia Wang 	/* SW0 (17h), AIW 0/1 (13h/14h) */
29253e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
29263e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
29273e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
29283e95b9abSLydia Wang 
29293e95b9abSLydia Wang 	/* outputs */
29303e95b9abSLydia Wang 	/* PW0 (19h), SW1 (18h), AOW1 (11h) */
29313e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29323e95b9abSLydia Wang 	set_pin_power_state(codec, 0x19, &parm);
29333e95b9abSLydia Wang 	if (spec->smart51_enabled)
29343e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1b, &parm);
29353e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
29363e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
29373e95b9abSLydia Wang 
29383e95b9abSLydia Wang 	/* PW6 (22h), SW2 (26h), AOW2 (24h) */
29393e95b9abSLydia Wang 	if (is_8ch) {
29403e95b9abSLydia Wang 		parm = AC_PWRST_D3;
29413e95b9abSLydia Wang 		set_pin_power_state(codec, 0x22, &parm);
29423e95b9abSLydia Wang 		if (spec->smart51_enabled)
29433e95b9abSLydia Wang 			set_pin_power_state(codec, 0x1a, &parm);
29443e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x26, 0,
29453e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
29463e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x24, 0,
29473e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
2948bc92df7fSLydia Wang 	} else if (codec->vendor_id == 0x11064397) {
2949bc92df7fSLydia Wang 		/* PW7(23h), SW2(27h), AOW2(25h) */
2950bc92df7fSLydia Wang 		parm = AC_PWRST_D3;
2951bc92df7fSLydia Wang 		set_pin_power_state(codec, 0x23, &parm);
2952bc92df7fSLydia Wang 		if (spec->smart51_enabled)
2953bc92df7fSLydia Wang 			set_pin_power_state(codec, 0x1a, &parm);
2954bc92df7fSLydia Wang 		snd_hda_codec_write(codec, 0x27, 0,
2955bc92df7fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
2956bc92df7fSLydia Wang 		snd_hda_codec_write(codec, 0x25, 0,
2957bc92df7fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
29583e95b9abSLydia Wang 	}
29593e95b9abSLydia Wang 
29603e95b9abSLydia Wang 	/* PW 3/4/7 (1ch/1dh/23h) */
29613e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29623e95b9abSLydia Wang 	/* force to D0 for internal Speaker */
29633e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1c, &parm);
29643e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1d, &parm);
29653e95b9abSLydia Wang 	if (is_8ch)
29663e95b9abSLydia Wang 		set_pin_power_state(codec, 0x23, &parm);
29673e95b9abSLydia Wang 
29683e95b9abSLydia Wang 	/* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
29693e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
29703e95b9abSLydia Wang 			    imux_is_smixer ? AC_PWRST_D0 : parm);
29713e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
29723e95b9abSLydia Wang 	if (is_8ch) {
29733e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x25, 0,
29743e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
29753e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x27, 0,
29763e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
2977bc92df7fSLydia Wang 	} else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
2978bc92df7fSLydia Wang 		snd_hda_codec_write(codec, 0x25, 0,
2979bc92df7fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
29803e95b9abSLydia Wang }
29813e95b9abSLydia Wang 
2982518bf3baSLydia Wang static int patch_vt1708S(struct hda_codec *codec);
2983ddd304d8STakashi Iwai static int patch_vt1708B(struct hda_codec *codec)
2984f7278fd0SJosepch Chan {
2985f7278fd0SJosepch Chan 	struct via_spec *spec;
2986f7278fd0SJosepch Chan 	int err;
2987f7278fd0SJosepch Chan 
2988518bf3baSLydia Wang 	if (get_codec_type(codec) == VT1708BCE)
2989518bf3baSLydia Wang 		return patch_vt1708S(codec);
2990ddd304d8STakashi Iwai 
2991f7278fd0SJosepch Chan 	/* create a codec specific record */
29925b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2993f7278fd0SJosepch Chan 	if (spec == NULL)
2994f7278fd0SJosepch Chan 		return -ENOMEM;
2995f7278fd0SJosepch Chan 
2996620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
2997620e2b28STakashi Iwai 
2998f7278fd0SJosepch Chan 	/* automatic parse from the BIOS config */
299912daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3000f7278fd0SJosepch Chan 	if (err < 0) {
3001f7278fd0SJosepch Chan 		via_free(codec);
3002f7278fd0SJosepch Chan 		return err;
3003f7278fd0SJosepch Chan 	}
3004f7278fd0SJosepch Chan 
3005f7278fd0SJosepch Chan 	codec->patch_ops = via_patch_ops;
3006f7278fd0SJosepch Chan 
30073e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
30083e95b9abSLydia Wang 
3009f7278fd0SJosepch Chan 	return 0;
3010f7278fd0SJosepch Chan }
3011f7278fd0SJosepch Chan 
3012d949cac1SHarald Welte /* Patch for VT1708S */
3013096a8854STakashi Iwai static const struct hda_verb vt1708S_init_verbs[] = {
3014d7426329SHarald Welte 	/* Enable Mic Boost Volume backdoor */
3015d7426329SHarald Welte 	{0x1, 0xf98, 0x1},
3016bc7e7e5cSLydia Wang 	/* don't bybass mixer */
3017bc7e7e5cSLydia Wang 	{0x1, 0xf88, 0xc0},
3018d949cac1SHarald Welte 	{ }
3019d949cac1SHarald Welte };
3020d949cac1SHarald Welte 
30219da29271STakashi Iwai /* fill out digital output widgets; one for master and one for slave outputs */
30229da29271STakashi Iwai static void fill_dig_outs(struct hda_codec *codec)
30239da29271STakashi Iwai {
30249da29271STakashi Iwai 	struct via_spec *spec = codec->spec;
30259da29271STakashi Iwai 	int i;
30269da29271STakashi Iwai 
30279da29271STakashi Iwai 	for (i = 0; i < spec->autocfg.dig_outs; i++) {
30289da29271STakashi Iwai 		hda_nid_t nid;
30299da29271STakashi Iwai 		int conn;
30309da29271STakashi Iwai 
30319da29271STakashi Iwai 		nid = spec->autocfg.dig_out_pins[i];
30329da29271STakashi Iwai 		if (!nid)
30339da29271STakashi Iwai 			continue;
30349da29271STakashi Iwai 		conn = snd_hda_get_connections(codec, nid, &nid, 1);
30359da29271STakashi Iwai 		if (conn < 1)
30369da29271STakashi Iwai 			continue;
30379da29271STakashi Iwai 		if (!spec->multiout.dig_out_nid)
30389da29271STakashi Iwai 			spec->multiout.dig_out_nid = nid;
30399da29271STakashi Iwai 		else {
30409da29271STakashi Iwai 			spec->slave_dig_outs[0] = nid;
30419da29271STakashi Iwai 			break; /* at most two dig outs */
30429da29271STakashi Iwai 		}
30439da29271STakashi Iwai 	}
30449da29271STakashi Iwai }
30459da29271STakashi Iwai 
304612daef65STakashi Iwai static void fill_dig_in(struct hda_codec *codec)
3047d949cac1SHarald Welte {
3048d949cac1SHarald Welte 	struct via_spec *spec = codec->spec;
304912daef65STakashi Iwai 	hda_nid_t dig_nid;
305012daef65STakashi Iwai 	int i, err;
3051d949cac1SHarald Welte 
305212daef65STakashi Iwai 	if (!spec->autocfg.dig_in_pin)
305312daef65STakashi Iwai 		return;
3054d949cac1SHarald Welte 
305512daef65STakashi Iwai 	dig_nid = codec->start_nid;
305612daef65STakashi Iwai 	for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
305712daef65STakashi Iwai 		unsigned int wcaps = get_wcaps(codec, dig_nid);
305812daef65STakashi Iwai 		if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
305912daef65STakashi Iwai 			continue;
306012daef65STakashi Iwai 		if (!(wcaps & AC_WCAP_DIGITAL))
306112daef65STakashi Iwai 			continue;
306212daef65STakashi Iwai 		if (!(wcaps & AC_WCAP_CONN_LIST))
306312daef65STakashi Iwai 			continue;
306412daef65STakashi Iwai 		err = get_connection_index(codec, dig_nid,
306512daef65STakashi Iwai 					   spec->autocfg.dig_in_pin);
306612daef65STakashi Iwai 		if (err >= 0) {
306712daef65STakashi Iwai 			spec->dig_in_nid = dig_nid;
306812daef65STakashi Iwai 			break;
306912daef65STakashi Iwai 		}
307012daef65STakashi Iwai 	}
3071d949cac1SHarald Welte }
3072d949cac1SHarald Welte 
30736369bcfcSLydia Wang static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
30746369bcfcSLydia Wang 			       int offset, int num_steps, int step_size)
30756369bcfcSLydia Wang {
30766369bcfcSLydia Wang 	snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
30776369bcfcSLydia Wang 				  (offset << AC_AMPCAP_OFFSET_SHIFT) |
30786369bcfcSLydia Wang 				  (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
30796369bcfcSLydia Wang 				  (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
30806369bcfcSLydia Wang 				  (0 << AC_AMPCAP_MUTE_SHIFT));
30816369bcfcSLydia Wang }
30826369bcfcSLydia Wang 
3083d949cac1SHarald Welte static int patch_vt1708S(struct hda_codec *codec)
3084d949cac1SHarald Welte {
3085d949cac1SHarald Welte 	struct via_spec *spec;
3086d949cac1SHarald Welte 	int err;
3087d949cac1SHarald Welte 
3088d949cac1SHarald Welte 	/* create a codec specific record */
30895b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3090d949cac1SHarald Welte 	if (spec == NULL)
3091d949cac1SHarald Welte 		return -ENOMEM;
3092d949cac1SHarald Welte 
3093620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
3094d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1a, 0, 3, 40);
3095d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1e, 0, 3, 40);
3096620e2b28STakashi Iwai 
3097d949cac1SHarald Welte 	/* automatic parse from the BIOS config */
309812daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3099d949cac1SHarald Welte 	if (err < 0) {
3100d949cac1SHarald Welte 		via_free(codec);
3101d949cac1SHarald Welte 		return err;
3102d949cac1SHarald Welte 	}
3103d949cac1SHarald Welte 
3104096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
3105d949cac1SHarald Welte 
3106d949cac1SHarald Welte 	codec->patch_ops = via_patch_ops;
3107d949cac1SHarald Welte 
3108518bf3baSLydia Wang 	/* correct names for VT1708BCE */
3109518bf3baSLydia Wang 	if (get_codec_type(codec) == VT1708BCE)	{
3110518bf3baSLydia Wang 		kfree(codec->chip_name);
3111518bf3baSLydia Wang 		codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
3112518bf3baSLydia Wang 		snprintf(codec->bus->card->mixername,
3113518bf3baSLydia Wang 			 sizeof(codec->bus->card->mixername),
3114518bf3baSLydia Wang 			 "%s %s", codec->vendor_name, codec->chip_name);
3115970f630fSLydia Wang 	}
3116bc92df7fSLydia Wang 	/* correct names for VT1705 */
3117bc92df7fSLydia Wang 	if (codec->vendor_id == 0x11064397)	{
3118bc92df7fSLydia Wang 		kfree(codec->chip_name);
3119bc92df7fSLydia Wang 		codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
3120bc92df7fSLydia Wang 		snprintf(codec->bus->card->mixername,
3121bc92df7fSLydia Wang 			 sizeof(codec->bus->card->mixername),
3122bc92df7fSLydia Wang 			 "%s %s", codec->vendor_name, codec->chip_name);
3123bc92df7fSLydia Wang 	}
31243e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
3125d949cac1SHarald Welte 	return 0;
3126d949cac1SHarald Welte }
3127d949cac1SHarald Welte 
3128d949cac1SHarald Welte /* Patch for VT1702 */
3129d949cac1SHarald Welte 
3130096a8854STakashi Iwai static const struct hda_verb vt1702_init_verbs[] = {
3131bc7e7e5cSLydia Wang 	/* mixer enable */
3132bc7e7e5cSLydia Wang 	{0x1, 0xF88, 0x3},
3133bc7e7e5cSLydia Wang 	/* GPIO 0~2 */
3134bc7e7e5cSLydia Wang 	{0x1, 0xF82, 0x3F},
3135d949cac1SHarald Welte 	{ }
3136d949cac1SHarald Welte };
3137d949cac1SHarald Welte 
31383e95b9abSLydia Wang static void set_widgets_power_state_vt1702(struct hda_codec *codec)
31393e95b9abSLydia Wang {
31403e95b9abSLydia Wang 	int imux_is_smixer =
31413e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
31423e95b9abSLydia Wang 	unsigned int parm;
31433e95b9abSLydia Wang 	/* inputs */
31443e95b9abSLydia Wang 	/* PW 1/2/5 (14h/15h/18h) */
31453e95b9abSLydia Wang 	parm = AC_PWRST_D3;
31463e95b9abSLydia Wang 	set_pin_power_state(codec, 0x14, &parm);
31473e95b9abSLydia Wang 	set_pin_power_state(codec, 0x15, &parm);
31483e95b9abSLydia Wang 	set_pin_power_state(codec, 0x18, &parm);
31493e95b9abSLydia Wang 	if (imux_is_smixer)
31503e95b9abSLydia Wang 		parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
31513e95b9abSLydia Wang 	/* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
31523e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
31533e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x12, 0, AC_VERB_SET_POWER_STATE, parm);
31543e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
31553e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_POWER_STATE, parm);
31563e95b9abSLydia Wang 
31573e95b9abSLydia Wang 	/* outputs */
31583e95b9abSLydia Wang 	/* PW 3/4 (16h/17h) */
31593e95b9abSLydia Wang 	parm = AC_PWRST_D3;
31603e95b9abSLydia Wang 	set_pin_power_state(codec, 0x17, &parm);
31613e95b9abSLydia Wang 	set_pin_power_state(codec, 0x16, &parm);
31623e95b9abSLydia Wang 	/* MW0 (1ah), AOW 0/1 (10h/1dh) */
31633e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
31643e95b9abSLydia Wang 			    imux_is_smixer ? AC_PWRST_D0 : parm);
31653e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
31663e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
31673e95b9abSLydia Wang }
31683e95b9abSLydia Wang 
3169d949cac1SHarald Welte static int patch_vt1702(struct hda_codec *codec)
3170d949cac1SHarald Welte {
3171d949cac1SHarald Welte 	struct via_spec *spec;
3172d949cac1SHarald Welte 	int err;
3173d949cac1SHarald Welte 
3174d949cac1SHarald Welte 	/* create a codec specific record */
31755b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3176d949cac1SHarald Welte 	if (spec == NULL)
3177d949cac1SHarald Welte 		return -ENOMEM;
3178d949cac1SHarald Welte 
3179620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x1a;
3180620e2b28STakashi Iwai 
318112daef65STakashi Iwai 	/* limit AA path volume to 0 dB */
318212daef65STakashi Iwai 	snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
318312daef65STakashi Iwai 				  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
318412daef65STakashi Iwai 				  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
318512daef65STakashi Iwai 				  (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
318612daef65STakashi Iwai 				  (1 << AC_AMPCAP_MUTE_SHIFT));
318712daef65STakashi Iwai 
3188d949cac1SHarald Welte 	/* automatic parse from the BIOS config */
318912daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3190d949cac1SHarald Welte 	if (err < 0) {
3191d949cac1SHarald Welte 		via_free(codec);
3192d949cac1SHarald Welte 		return err;
3193d949cac1SHarald Welte 	}
3194d949cac1SHarald Welte 
3195096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
3196d949cac1SHarald Welte 
3197d949cac1SHarald Welte 	codec->patch_ops = via_patch_ops;
3198d949cac1SHarald Welte 
31993e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1702;
3200d949cac1SHarald Welte 	return 0;
3201d949cac1SHarald Welte }
3202d949cac1SHarald Welte 
3203eb7188caSLydia Wang /* Patch for VT1718S */
3204eb7188caSLydia Wang 
3205096a8854STakashi Iwai static const struct hda_verb vt1718S_init_verbs[] = {
32064ab2d53aSLydia Wang 	/* Enable MW0 adjust Gain 5 */
32074ab2d53aSLydia Wang 	{0x1, 0xfb2, 0x10},
3208eb7188caSLydia Wang 	/* Enable Boost Volume backdoor */
3209eb7188caSLydia Wang 	{0x1, 0xf88, 0x8},
32105d41762aSTakashi Iwai 
3211eb7188caSLydia Wang 	{ }
3212eb7188caSLydia Wang };
3213eb7188caSLydia Wang 
32143e95b9abSLydia Wang static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
32153e95b9abSLydia Wang {
32163e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
32173e95b9abSLydia Wang 	int imux_is_smixer;
32183e95b9abSLydia Wang 	unsigned int parm;
32193e95b9abSLydia Wang 	/* MUX6 (1eh) = stereo mixer */
32203e95b9abSLydia Wang 	imux_is_smixer =
32213e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
32223e95b9abSLydia Wang 	/* inputs */
32233e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
32243e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32253e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
32263e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
32273e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
32283e95b9abSLydia Wang 	if (imux_is_smixer)
32293e95b9abSLydia Wang 		parm = AC_PWRST_D0;
32303e95b9abSLydia Wang 	/* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
32313e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
32323e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
32333e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
32343e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
32353e95b9abSLydia Wang 
32363e95b9abSLydia Wang 	/* outputs */
32373e95b9abSLydia Wang 	/* PW3 (27h), MW2 (1ah), AOW3 (bh) */
32383e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32393e95b9abSLydia Wang 	set_pin_power_state(codec, 0x27, &parm);
32403e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE, parm);
32413e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0xb, 0, AC_VERB_SET_POWER_STATE, parm);
32423e95b9abSLydia Wang 
32433e95b9abSLydia Wang 	/* PW2 (26h), AOW2 (ah) */
32443e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32453e95b9abSLydia Wang 	set_pin_power_state(codec, 0x26, &parm);
32463e95b9abSLydia Wang 	if (spec->smart51_enabled)
32473e95b9abSLydia Wang 		set_pin_power_state(codec, 0x2b, &parm);
32483e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0xa, 0, AC_VERB_SET_POWER_STATE, parm);
32493e95b9abSLydia Wang 
32503e95b9abSLydia Wang 	/* PW0 (24h), AOW0 (8h) */
32513e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32523e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
32533e95b9abSLydia Wang 	if (!spec->hp_independent_mode) /* check for redirected HP */
32543e95b9abSLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
32553e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
32563e95b9abSLydia Wang 	/* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
32573e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x21, 0, AC_VERB_SET_POWER_STATE,
32583e95b9abSLydia Wang 			    imux_is_smixer ? AC_PWRST_D0 : parm);
32593e95b9abSLydia Wang 
32603e95b9abSLydia Wang 	/* PW1 (25h), AOW1 (9h) */
32613e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32623e95b9abSLydia Wang 	set_pin_power_state(codec, 0x25, &parm);
32633e95b9abSLydia Wang 	if (spec->smart51_enabled)
32643e95b9abSLydia Wang 		set_pin_power_state(codec, 0x2a, &parm);
32653e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x9, 0, AC_VERB_SET_POWER_STATE, parm);
32663e95b9abSLydia Wang 
32673e95b9abSLydia Wang 	if (spec->hp_independent_mode) {
32683e95b9abSLydia Wang 		/* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
32693e95b9abSLydia Wang 		parm = AC_PWRST_D3;
32703e95b9abSLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
32713e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x1b, 0,
32723e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
32733e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x34, 0,
32743e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
32753e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0xc, 0,
32763e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
32773e95b9abSLydia Wang 	}
32783e95b9abSLydia Wang }
32793e95b9abSLydia Wang 
328030b45033STakashi Iwai /* Add a connection to the primary DAC from AA-mixer for some codecs
328130b45033STakashi Iwai  * This isn't listed from the raw info, but the chip has a secret connection.
328230b45033STakashi Iwai  */
328330b45033STakashi Iwai static int add_secret_dac_path(struct hda_codec *codec)
328430b45033STakashi Iwai {
328530b45033STakashi Iwai 	struct via_spec *spec = codec->spec;
328630b45033STakashi Iwai 	int i, nums;
328730b45033STakashi Iwai 	hda_nid_t conn[8];
328830b45033STakashi Iwai 	hda_nid_t nid;
328930b45033STakashi Iwai 
329030b45033STakashi Iwai 	if (!spec->aa_mix_nid)
329130b45033STakashi Iwai 		return 0;
329230b45033STakashi Iwai 	nums = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
329330b45033STakashi Iwai 				       ARRAY_SIZE(conn) - 1);
329430b45033STakashi Iwai 	for (i = 0; i < nums; i++) {
329530b45033STakashi Iwai 		if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT)
329630b45033STakashi Iwai 			return 0;
329730b45033STakashi Iwai 	}
329830b45033STakashi Iwai 
329930b45033STakashi Iwai 	/* find the primary DAC and add to the connection list */
330030b45033STakashi Iwai 	nid = codec->start_nid;
330130b45033STakashi Iwai 	for (i = 0; i < codec->num_nodes; i++, nid++) {
330230b45033STakashi Iwai 		unsigned int caps = get_wcaps(codec, nid);
330330b45033STakashi Iwai 		if (get_wcaps_type(caps) == AC_WID_AUD_OUT &&
330430b45033STakashi Iwai 		    !(caps & AC_WCAP_DIGITAL)) {
330530b45033STakashi Iwai 			conn[nums++] = nid;
330630b45033STakashi Iwai 			return snd_hda_override_conn_list(codec,
330730b45033STakashi Iwai 							  spec->aa_mix_nid,
330830b45033STakashi Iwai 							  nums, conn);
330930b45033STakashi Iwai 		}
331030b45033STakashi Iwai 	}
331130b45033STakashi Iwai 	return 0;
331230b45033STakashi Iwai }
331330b45033STakashi Iwai 
331430b45033STakashi Iwai 
3315eb7188caSLydia Wang static int patch_vt1718S(struct hda_codec *codec)
3316eb7188caSLydia Wang {
3317eb7188caSLydia Wang 	struct via_spec *spec;
3318eb7188caSLydia Wang 	int err;
3319eb7188caSLydia Wang 
3320eb7188caSLydia Wang 	/* create a codec specific record */
33215b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3322eb7188caSLydia Wang 	if (spec == NULL)
3323eb7188caSLydia Wang 		return -ENOMEM;
3324eb7188caSLydia Wang 
3325620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
3326d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3327d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
332830b45033STakashi Iwai 	add_secret_dac_path(codec);
3329620e2b28STakashi Iwai 
3330eb7188caSLydia Wang 	/* automatic parse from the BIOS config */
333112daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3332eb7188caSLydia Wang 	if (err < 0) {
3333eb7188caSLydia Wang 		via_free(codec);
3334eb7188caSLydia Wang 		return err;
3335eb7188caSLydia Wang 	}
3336eb7188caSLydia Wang 
3337096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
3338eb7188caSLydia Wang 
3339eb7188caSLydia Wang 	codec->patch_ops = via_patch_ops;
3340eb7188caSLydia Wang 
33413e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1718S;
33423e95b9abSLydia Wang 
3343eb7188caSLydia Wang 	return 0;
3344eb7188caSLydia Wang }
3345f3db423dSLydia Wang 
3346f3db423dSLydia Wang /* Patch for VT1716S */
3347f3db423dSLydia Wang 
3348f3db423dSLydia Wang static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
3349f3db423dSLydia Wang 			    struct snd_ctl_elem_info *uinfo)
3350f3db423dSLydia Wang {
3351f3db423dSLydia Wang 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3352f3db423dSLydia Wang 	uinfo->count = 1;
3353f3db423dSLydia Wang 	uinfo->value.integer.min = 0;
3354f3db423dSLydia Wang 	uinfo->value.integer.max = 1;
3355f3db423dSLydia Wang 	return 0;
3356f3db423dSLydia Wang }
3357f3db423dSLydia Wang 
3358f3db423dSLydia Wang static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
3359f3db423dSLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
3360f3db423dSLydia Wang {
3361f3db423dSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3362f3db423dSLydia Wang 	int index = 0;
3363f3db423dSLydia Wang 
3364f3db423dSLydia Wang 	index = snd_hda_codec_read(codec, 0x26, 0,
3365f3db423dSLydia Wang 					       AC_VERB_GET_CONNECT_SEL, 0);
3366f3db423dSLydia Wang 	if (index != -1)
3367f3db423dSLydia Wang 		*ucontrol->value.integer.value = index;
3368f3db423dSLydia Wang 
3369f3db423dSLydia Wang 	return 0;
3370f3db423dSLydia Wang }
3371f3db423dSLydia Wang 
3372f3db423dSLydia Wang static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
3373f3db423dSLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
3374f3db423dSLydia Wang {
3375f3db423dSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3376f3db423dSLydia Wang 	struct via_spec *spec = codec->spec;
3377f3db423dSLydia Wang 	int index = *ucontrol->value.integer.value;
3378f3db423dSLydia Wang 
3379f3db423dSLydia Wang 	snd_hda_codec_write(codec, 0x26, 0,
3380f3db423dSLydia Wang 					       AC_VERB_SET_CONNECT_SEL, index);
3381f3db423dSLydia Wang 	spec->dmic_enabled = index;
33823e95b9abSLydia Wang 	set_widgets_power_state(codec);
3383f3db423dSLydia Wang 	return 1;
3384f3db423dSLydia Wang }
3385f3db423dSLydia Wang 
338690dd48a1STakashi Iwai static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
3387f3db423dSLydia Wang 	HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
3388f3db423dSLydia Wang 	{
3389f3db423dSLydia Wang 	 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3390f3db423dSLydia Wang 	 .name = "Digital Mic Capture Switch",
33915b0cb1d8SJaroslav Kysela 	 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
3392f3db423dSLydia Wang 	 .count = 1,
3393f3db423dSLydia Wang 	 .info = vt1716s_dmic_info,
3394f3db423dSLydia Wang 	 .get = vt1716s_dmic_get,
3395f3db423dSLydia Wang 	 .put = vt1716s_dmic_put,
3396f3db423dSLydia Wang 	 },
3397f3db423dSLydia Wang 	{}			/* end */
3398f3db423dSLydia Wang };
3399f3db423dSLydia Wang 
3400f3db423dSLydia Wang 
3401f3db423dSLydia Wang /* mono-out mixer elements */
340290dd48a1STakashi Iwai static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
3403f3db423dSLydia Wang 	HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
3404f3db423dSLydia Wang 	{ } /* end */
3405f3db423dSLydia Wang };
3406f3db423dSLydia Wang 
3407096a8854STakashi Iwai static const struct hda_verb vt1716S_init_verbs[] = {
3408f3db423dSLydia Wang 	/* Enable Boost Volume backdoor */
3409f3db423dSLydia Wang 	{0x1, 0xf8a, 0x80},
3410f3db423dSLydia Wang 	/* don't bybass mixer */
3411f3db423dSLydia Wang 	{0x1, 0xf88, 0xc0},
3412f3db423dSLydia Wang 	/* Enable mono output */
3413f3db423dSLydia Wang 	{0x1, 0xf90, 0x08},
3414f3db423dSLydia Wang 	{ }
3415f3db423dSLydia Wang };
3416f3db423dSLydia Wang 
34173e95b9abSLydia Wang static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
34183e95b9abSLydia Wang {
34193e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
34203e95b9abSLydia Wang 	int imux_is_smixer;
34213e95b9abSLydia Wang 	unsigned int parm;
34223e95b9abSLydia Wang 	unsigned int mono_out, present;
34233e95b9abSLydia Wang 	/* SW0 (17h) = stereo mixer */
34243e95b9abSLydia Wang 	imux_is_smixer =
34253e95b9abSLydia Wang 	(snd_hda_codec_read(codec, 0x17, 0,
34263e95b9abSLydia Wang 			    AC_VERB_GET_CONNECT_SEL, 0x00) ==  5);
34273e95b9abSLydia Wang 	/* inputs */
34283e95b9abSLydia Wang 	/* PW 1/2/5 (1ah/1bh/1eh) */
34293e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34303e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1a, &parm);
34313e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1b, &parm);
34323e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
34333e95b9abSLydia Wang 	if (imux_is_smixer)
34343e95b9abSLydia Wang 		parm = AC_PWRST_D0;
34353e95b9abSLydia Wang 	/* SW0 (17h), AIW0(13h) */
34363e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
34373e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
34383e95b9abSLydia Wang 
34393e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34403e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
34413e95b9abSLydia Wang 	/* PW11 (22h) */
34423e95b9abSLydia Wang 	if (spec->dmic_enabled)
34433e95b9abSLydia Wang 		set_pin_power_state(codec, 0x22, &parm);
34443e95b9abSLydia Wang 	else
34453e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x22, 0,
34463e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
34473e95b9abSLydia Wang 
34483e95b9abSLydia Wang 	/* SW2(26h), AIW1(14h) */
34493e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x26, 0, AC_VERB_SET_POWER_STATE, parm);
34503e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
34513e95b9abSLydia Wang 
34523e95b9abSLydia Wang 	/* outputs */
34533e95b9abSLydia Wang 	/* PW0 (19h), SW1 (18h), AOW1 (11h) */
34543e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34553e95b9abSLydia Wang 	set_pin_power_state(codec, 0x19, &parm);
34563e95b9abSLydia Wang 	/* Smart 5.1 PW2(1bh) */
34573e95b9abSLydia Wang 	if (spec->smart51_enabled)
34583e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1b, &parm);
34593e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
34603e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
34613e95b9abSLydia Wang 
34623e95b9abSLydia Wang 	/* PW7 (23h), SW3 (27h), AOW3 (25h) */
34633e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34643e95b9abSLydia Wang 	set_pin_power_state(codec, 0x23, &parm);
34653e95b9abSLydia Wang 	/* Smart 5.1 PW1(1ah) */
34663e95b9abSLydia Wang 	if (spec->smart51_enabled)
34673e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1a, &parm);
34683e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x27, 0, AC_VERB_SET_POWER_STATE, parm);
34693e95b9abSLydia Wang 
34703e95b9abSLydia Wang 	/* Smart 5.1 PW5(1eh) */
34713e95b9abSLydia Wang 	if (spec->smart51_enabled)
34723e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1e, &parm);
34733e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x25, 0, AC_VERB_SET_POWER_STATE, parm);
34743e95b9abSLydia Wang 
34753e95b9abSLydia Wang 	/* Mono out */
34763e95b9abSLydia Wang 	/* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
34773e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x1c);
34783e95b9abSLydia Wang 
34793e95b9abSLydia Wang 	if (present)
34803e95b9abSLydia Wang 		mono_out = 0;
34813e95b9abSLydia Wang 	else {
34823e95b9abSLydia Wang 		present = snd_hda_jack_detect(codec, 0x1d);
34833e95b9abSLydia Wang 		if (!spec->hp_independent_mode && present)
34843e95b9abSLydia Wang 			mono_out = 0;
34853e95b9abSLydia Wang 		else
34863e95b9abSLydia Wang 			mono_out = 1;
34873e95b9abSLydia Wang 	}
34883e95b9abSLydia Wang 	parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
34893e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x28, 0, AC_VERB_SET_POWER_STATE, parm);
34903e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x29, 0, AC_VERB_SET_POWER_STATE, parm);
34913e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x2a, 0, AC_VERB_SET_POWER_STATE, parm);
34923e95b9abSLydia Wang 
34933e95b9abSLydia Wang 	/* PW 3/4 (1ch/1dh) */
34943e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34953e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1c, &parm);
34963e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1d, &parm);
34973e95b9abSLydia Wang 	/* HP Independent Mode, power on AOW3 */
34983e95b9abSLydia Wang 	if (spec->hp_independent_mode)
34993e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x25, 0,
35003e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
35013e95b9abSLydia Wang 
35023e95b9abSLydia Wang 	/* force to D0 for internal Speaker */
35033e95b9abSLydia Wang 	/* MW0 (16h), AOW0 (10h) */
35043e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
35053e95b9abSLydia Wang 			    imux_is_smixer ? AC_PWRST_D0 : parm);
35063e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
35073e95b9abSLydia Wang 			    mono_out ? AC_PWRST_D0 : parm);
35083e95b9abSLydia Wang }
35093e95b9abSLydia Wang 
3510f3db423dSLydia Wang static int patch_vt1716S(struct hda_codec *codec)
3511f3db423dSLydia Wang {
3512f3db423dSLydia Wang 	struct via_spec *spec;
3513f3db423dSLydia Wang 	int err;
3514f3db423dSLydia Wang 
3515f3db423dSLydia Wang 	/* create a codec specific record */
35165b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3517f3db423dSLydia Wang 	if (spec == NULL)
3518f3db423dSLydia Wang 		return -ENOMEM;
3519f3db423dSLydia Wang 
3520620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
3521d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1a, 0, 3, 40);
3522d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1e, 0, 3, 40);
3523620e2b28STakashi Iwai 
3524f3db423dSLydia Wang 	/* automatic parse from the BIOS config */
352512daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3526f3db423dSLydia Wang 	if (err < 0) {
3527f3db423dSLydia Wang 		via_free(codec);
3528f3db423dSLydia Wang 		return err;
3529f3db423dSLydia Wang 	}
3530f3db423dSLydia Wang 
3531096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++]  = vt1716S_init_verbs;
3532f3db423dSLydia Wang 
3533f3db423dSLydia Wang 	spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
3534f3db423dSLydia Wang 	spec->num_mixers++;
3535f3db423dSLydia Wang 
3536f3db423dSLydia Wang 	spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
3537f3db423dSLydia Wang 
3538f3db423dSLydia Wang 	codec->patch_ops = via_patch_ops;
3539f3db423dSLydia Wang 
35403e95b9abSLydia Wang 	spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
3541f3db423dSLydia Wang 	return 0;
3542f3db423dSLydia Wang }
354325eaba2fSLydia Wang 
354425eaba2fSLydia Wang /* for vt2002P */
354525eaba2fSLydia Wang 
3546096a8854STakashi Iwai static const struct hda_verb vt2002P_init_verbs[] = {
3547eadb9a80SLydia Wang 	/* Class-D speaker related verbs */
3548eadb9a80SLydia Wang 	{0x1, 0xfe0, 0x4},
3549eadb9a80SLydia Wang 	{0x1, 0xfe9, 0x80},
3550eadb9a80SLydia Wang 	{0x1, 0xfe2, 0x22},
355125eaba2fSLydia Wang 	/* Enable Boost Volume backdoor */
355225eaba2fSLydia Wang 	{0x1, 0xfb9, 0x24},
355325eaba2fSLydia Wang 	/* Enable AOW0 to MW9 */
355425eaba2fSLydia Wang 	{0x1, 0xfb8, 0x88},
355525eaba2fSLydia Wang 	{ }
355625eaba2fSLydia Wang };
35574a918ffeSTakashi Iwai 
3558096a8854STakashi Iwai static const struct hda_verb vt1802_init_verbs[] = {
355911890956SLydia Wang 	/* Enable Boost Volume backdoor */
356011890956SLydia Wang 	{0x1, 0xfb9, 0x24},
356111890956SLydia Wang 	/* Enable AOW0 to MW9 */
356211890956SLydia Wang 	{0x1, 0xfb8, 0x88},
356311890956SLydia Wang 	{ }
356411890956SLydia Wang };
356525eaba2fSLydia Wang 
35663e95b9abSLydia Wang static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
35673e95b9abSLydia Wang {
35683e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
35693e95b9abSLydia Wang 	int imux_is_smixer;
35703e95b9abSLydia Wang 	unsigned int parm;
35713e95b9abSLydia Wang 	unsigned int present;
35723e95b9abSLydia Wang 	/* MUX9 (1eh) = stereo mixer */
35733e95b9abSLydia Wang 	imux_is_smixer =
35743e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
35753e95b9abSLydia Wang 	/* inputs */
35763e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
35773e95b9abSLydia Wang 	parm = AC_PWRST_D3;
35783e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
35793e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
35803e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
35813e95b9abSLydia Wang 	parm = AC_PWRST_D0;
35823e95b9abSLydia Wang 	/* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
35833e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
35843e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
35853e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
35863e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
35873e95b9abSLydia Wang 
35883e95b9abSLydia Wang 	/* outputs */
35893e95b9abSLydia Wang 	/* AOW0 (8h)*/
35903e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
35913e95b9abSLydia Wang 
359211890956SLydia Wang 	if (spec->codec_type == VT1802) {
359311890956SLydia Wang 		/* PW4 (28h), MW4 (18h), MUX4(38h) */
359411890956SLydia Wang 		parm = AC_PWRST_D3;
359511890956SLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
359611890956SLydia Wang 		snd_hda_codec_write(codec, 0x18, 0,
359711890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
359811890956SLydia Wang 		snd_hda_codec_write(codec, 0x38, 0,
359911890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
360011890956SLydia Wang 	} else {
36013e95b9abSLydia Wang 		/* PW4 (26h), MW4 (1ch), MUX4(37h) */
36023e95b9abSLydia Wang 		parm = AC_PWRST_D3;
36033e95b9abSLydia Wang 		set_pin_power_state(codec, 0x26, &parm);
36043e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x1c, 0,
36053e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
36063e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x37, 0,
36073e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
360811890956SLydia Wang 	}
36093e95b9abSLydia Wang 
361011890956SLydia Wang 	if (spec->codec_type == VT1802) {
361111890956SLydia Wang 		/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
361211890956SLydia Wang 		parm = AC_PWRST_D3;
361311890956SLydia Wang 		set_pin_power_state(codec, 0x25, &parm);
361411890956SLydia Wang 		snd_hda_codec_write(codec, 0x15, 0,
361511890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
361611890956SLydia Wang 		snd_hda_codec_write(codec, 0x35, 0,
361711890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, 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);
36223e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x19, 0,
36233e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
36243e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x35, 0,
36253e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
362611890956SLydia Wang 	}
36273e95b9abSLydia Wang 
36283e95b9abSLydia Wang 	if (spec->hp_independent_mode)
36293e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x9, 0,
36303e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
36313e95b9abSLydia Wang 
36323e95b9abSLydia Wang 	/* Class-D */
36333e95b9abSLydia Wang 	/* PW0 (24h), MW0(18h/14h), MUX0(34h) */
36343e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x25);
36353e95b9abSLydia Wang 
36363e95b9abSLydia Wang 	parm = AC_PWRST_D3;
36373e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
36383e95b9abSLydia Wang 	parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
363911890956SLydia Wang 	if (spec->codec_type == VT1802)
364011890956SLydia Wang 		snd_hda_codec_write(codec, 0x14, 0,
364111890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
364211890956SLydia Wang 	else
36433e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x18, 0,
36443e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
36453e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x34, 0, AC_VERB_SET_POWER_STATE, parm);
36463e95b9abSLydia Wang 
36473e95b9abSLydia Wang 	/* Mono Out */
36483e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x26);
36493e95b9abSLydia Wang 
36503e95b9abSLydia Wang 	parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
365111890956SLydia Wang 	if (spec->codec_type == VT1802) {
365211890956SLydia Wang 		/* PW15 (33h), MW8(1ch), MUX8(3ch) */
365311890956SLydia Wang 		snd_hda_codec_write(codec, 0x33, 0,
365411890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
365511890956SLydia Wang 		snd_hda_codec_write(codec, 0x1c, 0,
365611890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
365711890956SLydia Wang 		snd_hda_codec_write(codec, 0x3c, 0,
365811890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
365911890956SLydia Wang 	} else {
36603e95b9abSLydia Wang 		/* PW15 (31h), MW8(17h), MUX8(3bh) */
36613e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x31, 0,
36623e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
36633e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x17, 0,
36643e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
36653e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3b, 0,
36663e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
366711890956SLydia Wang 	}
36683e95b9abSLydia Wang 	/* MW9 (21h) */
36693e95b9abSLydia Wang 	if (imux_is_smixer || !is_aa_path_mute(codec))
36703e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x21, 0,
36713e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
36723e95b9abSLydia Wang 	else
36733e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x21, 0,
36743e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
36753e95b9abSLydia Wang }
367625eaba2fSLydia Wang 
367725eaba2fSLydia Wang /* patch for vt2002P */
367825eaba2fSLydia Wang static int patch_vt2002P(struct hda_codec *codec)
367925eaba2fSLydia Wang {
368025eaba2fSLydia Wang 	struct via_spec *spec;
368125eaba2fSLydia Wang 	int err;
368225eaba2fSLydia Wang 
368325eaba2fSLydia Wang 	/* create a codec specific record */
36845b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
368525eaba2fSLydia Wang 	if (spec == NULL)
368625eaba2fSLydia Wang 		return -ENOMEM;
368725eaba2fSLydia Wang 
3688620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
3689d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3690d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
369130b45033STakashi Iwai 	add_secret_dac_path(codec);
3692620e2b28STakashi Iwai 
369325eaba2fSLydia Wang 	/* automatic parse from the BIOS config */
369412daef65STakashi Iwai 	err = via_parse_auto_config(codec);
369525eaba2fSLydia Wang 	if (err < 0) {
369625eaba2fSLydia Wang 		via_free(codec);
369725eaba2fSLydia Wang 		return err;
369825eaba2fSLydia Wang 	}
369925eaba2fSLydia Wang 
370011890956SLydia Wang 	if (spec->codec_type == VT1802)
37014a918ffeSTakashi Iwai 		spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
370211890956SLydia Wang 	else
37034a918ffeSTakashi Iwai 		spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
370411890956SLydia Wang 
370525eaba2fSLydia Wang 	codec->patch_ops = via_patch_ops;
370625eaba2fSLydia Wang 
37073e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt2002P;
370825eaba2fSLydia Wang 	return 0;
370925eaba2fSLydia Wang }
3710ab6734e7SLydia Wang 
3711ab6734e7SLydia Wang /* for vt1812 */
3712ab6734e7SLydia Wang 
3713096a8854STakashi Iwai static const struct hda_verb vt1812_init_verbs[] = {
3714ab6734e7SLydia Wang 	/* Enable Boost Volume backdoor */
3715ab6734e7SLydia Wang 	{0x1, 0xfb9, 0x24},
3716ab6734e7SLydia Wang 	/* Enable AOW0 to MW9 */
3717ab6734e7SLydia Wang 	{0x1, 0xfb8, 0xa8},
3718ab6734e7SLydia Wang 	{ }
3719ab6734e7SLydia Wang };
3720ab6734e7SLydia Wang 
37213e95b9abSLydia Wang static void set_widgets_power_state_vt1812(struct hda_codec *codec)
37223e95b9abSLydia Wang {
37233e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
37243e95b9abSLydia Wang 	unsigned int parm;
37253e95b9abSLydia Wang 	unsigned int present;
37263e95b9abSLydia Wang 	/* inputs */
37273e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
37283e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37293e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
37303e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
37313e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
37323e95b9abSLydia Wang 	parm = AC_PWRST_D0;
37333e95b9abSLydia Wang 	/* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
37343e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
37353e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
37363e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
37373e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
37383e95b9abSLydia Wang 
37393e95b9abSLydia Wang 	/* outputs */
37403e95b9abSLydia Wang 	/* AOW0 (8h)*/
37413e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x8, 0,
37423e95b9abSLydia Wang 			    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
37433e95b9abSLydia Wang 
37443e95b9abSLydia Wang 	/* PW4 (28h), MW4 (18h), MUX4(38h) */
37453e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37463e95b9abSLydia Wang 	set_pin_power_state(codec, 0x28, &parm);
37473e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
37483e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x38, 0, AC_VERB_SET_POWER_STATE, parm);
37493e95b9abSLydia Wang 
37503e95b9abSLydia Wang 	/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
37513e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37523e95b9abSLydia Wang 	set_pin_power_state(codec, 0x25, &parm);
37533e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x15, 0, AC_VERB_SET_POWER_STATE, parm);
37543e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x35, 0, AC_VERB_SET_POWER_STATE, parm);
37553e95b9abSLydia Wang 	if (spec->hp_independent_mode)
37563e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x9, 0,
37573e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
37583e95b9abSLydia Wang 
37593e95b9abSLydia Wang 	/* Internal Speaker */
37603e95b9abSLydia Wang 	/* PW0 (24h), MW0(14h), MUX0(34h) */
37613e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x25);
37623e95b9abSLydia Wang 
37633e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37643e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
37653e95b9abSLydia Wang 	if (present) {
37663e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x14, 0,
37673e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
37683e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x34, 0,
37693e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
37703e95b9abSLydia Wang 	} else {
37713e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x14, 0,
37723e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
37733e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x34, 0,
37743e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
37753e95b9abSLydia Wang 	}
37763e95b9abSLydia Wang 
37773e95b9abSLydia Wang 
37783e95b9abSLydia Wang 	/* Mono Out */
37793e95b9abSLydia Wang 	/* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
37803e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x28);
37813e95b9abSLydia Wang 
37823e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37833e95b9abSLydia Wang 	set_pin_power_state(codec, 0x31, &parm);
37843e95b9abSLydia Wang 	if (present) {
37853e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x1c, 0,
37863e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
37873e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3c, 0,
37883e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
37893e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3e, 0,
37903e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
37913e95b9abSLydia Wang 	} else {
37923e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x1c, 0,
37933e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
37943e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3c, 0,
37953e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
37963e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3e, 0,
37973e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
37983e95b9abSLydia Wang 	}
37993e95b9abSLydia Wang 
38003e95b9abSLydia Wang 	/* PW15 (33h), MW15 (1dh), MUX15(3dh) */
38013e95b9abSLydia Wang 	parm = AC_PWRST_D3;
38023e95b9abSLydia Wang 	set_pin_power_state(codec, 0x33, &parm);
38033e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
38043e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x3d, 0, AC_VERB_SET_POWER_STATE, parm);
38053e95b9abSLydia Wang 
38063e95b9abSLydia Wang }
3807ab6734e7SLydia Wang 
3808ab6734e7SLydia Wang /* patch for vt1812 */
3809ab6734e7SLydia Wang static int patch_vt1812(struct hda_codec *codec)
3810ab6734e7SLydia Wang {
3811ab6734e7SLydia Wang 	struct via_spec *spec;
3812ab6734e7SLydia Wang 	int err;
3813ab6734e7SLydia Wang 
3814ab6734e7SLydia Wang 	/* create a codec specific record */
38155b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3816ab6734e7SLydia Wang 	if (spec == NULL)
3817ab6734e7SLydia Wang 		return -ENOMEM;
3818ab6734e7SLydia Wang 
3819620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
3820d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3821d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
382230b45033STakashi Iwai 	add_secret_dac_path(codec);
3823620e2b28STakashi Iwai 
3824ab6734e7SLydia Wang 	/* automatic parse from the BIOS config */
382512daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3826ab6734e7SLydia Wang 	if (err < 0) {
3827ab6734e7SLydia Wang 		via_free(codec);
3828ab6734e7SLydia Wang 		return err;
3829ab6734e7SLydia Wang 	}
3830ab6734e7SLydia Wang 
3831096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++]  = vt1812_init_verbs;
3832ab6734e7SLydia Wang 
3833ab6734e7SLydia Wang 	codec->patch_ops = via_patch_ops;
3834ab6734e7SLydia Wang 
38353e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1812;
3836ab6734e7SLydia Wang 	return 0;
3837ab6734e7SLydia Wang }
3838ab6734e7SLydia Wang 
3839c577b8a1SJoseph Chan /*
3840c577b8a1SJoseph Chan  * patch entries
3841c577b8a1SJoseph Chan  */
384290dd48a1STakashi Iwai static const struct hda_codec_preset snd_hda_preset_via[] = {
38433218c178STakashi Iwai 	{ .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
38443218c178STakashi Iwai 	{ .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
38453218c178STakashi Iwai 	{ .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
38463218c178STakashi Iwai 	{ .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
38473218c178STakashi Iwai 	{ .id = 0x1106e710, .name = "VT1709 10-Ch",
3848ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38493218c178STakashi Iwai 	{ .id = 0x1106e711, .name = "VT1709 10-Ch",
3850ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38513218c178STakashi Iwai 	{ .id = 0x1106e712, .name = "VT1709 10-Ch",
3852ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38533218c178STakashi Iwai 	{ .id = 0x1106e713, .name = "VT1709 10-Ch",
3854ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38553218c178STakashi Iwai 	{ .id = 0x1106e714, .name = "VT1709 6-Ch",
3856ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38573218c178STakashi Iwai 	{ .id = 0x1106e715, .name = "VT1709 6-Ch",
3858ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38593218c178STakashi Iwai 	{ .id = 0x1106e716, .name = "VT1709 6-Ch",
3860ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38613218c178STakashi Iwai 	{ .id = 0x1106e717, .name = "VT1709 6-Ch",
3862ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38633218c178STakashi Iwai 	{ .id = 0x1106e720, .name = "VT1708B 8-Ch",
3864ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38653218c178STakashi Iwai 	{ .id = 0x1106e721, .name = "VT1708B 8-Ch",
3866ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38673218c178STakashi Iwai 	{ .id = 0x1106e722, .name = "VT1708B 8-Ch",
3868ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38693218c178STakashi Iwai 	{ .id = 0x1106e723, .name = "VT1708B 8-Ch",
3870ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38713218c178STakashi Iwai 	{ .id = 0x1106e724, .name = "VT1708B 4-Ch",
3872ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38733218c178STakashi Iwai 	{ .id = 0x1106e725, .name = "VT1708B 4-Ch",
3874ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38753218c178STakashi Iwai 	{ .id = 0x1106e726, .name = "VT1708B 4-Ch",
3876ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38773218c178STakashi Iwai 	{ .id = 0x1106e727, .name = "VT1708B 4-Ch",
3878ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38793218c178STakashi Iwai 	{ .id = 0x11060397, .name = "VT1708S",
3880d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38813218c178STakashi Iwai 	{ .id = 0x11061397, .name = "VT1708S",
3882d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38833218c178STakashi Iwai 	{ .id = 0x11062397, .name = "VT1708S",
3884d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38853218c178STakashi Iwai 	{ .id = 0x11063397, .name = "VT1708S",
3886d949cac1SHarald Welte 	  .patch = patch_vt1708S},
3887bc92df7fSLydia Wang 	{ .id = 0x11064397, .name = "VT1705",
3888d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38893218c178STakashi Iwai 	{ .id = 0x11065397, .name = "VT1708S",
3890d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38913218c178STakashi Iwai 	{ .id = 0x11066397, .name = "VT1708S",
3892d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38933218c178STakashi Iwai 	{ .id = 0x11067397, .name = "VT1708S",
3894d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38953218c178STakashi Iwai 	{ .id = 0x11060398, .name = "VT1702",
3896d949cac1SHarald Welte 	  .patch = patch_vt1702},
38973218c178STakashi Iwai 	{ .id = 0x11061398, .name = "VT1702",
3898d949cac1SHarald Welte 	  .patch = patch_vt1702},
38993218c178STakashi Iwai 	{ .id = 0x11062398, .name = "VT1702",
3900d949cac1SHarald Welte 	  .patch = patch_vt1702},
39013218c178STakashi Iwai 	{ .id = 0x11063398, .name = "VT1702",
3902d949cac1SHarald Welte 	  .patch = patch_vt1702},
39033218c178STakashi Iwai 	{ .id = 0x11064398, .name = "VT1702",
3904d949cac1SHarald Welte 	  .patch = patch_vt1702},
39053218c178STakashi Iwai 	{ .id = 0x11065398, .name = "VT1702",
3906d949cac1SHarald Welte 	  .patch = patch_vt1702},
39073218c178STakashi Iwai 	{ .id = 0x11066398, .name = "VT1702",
3908d949cac1SHarald Welte 	  .patch = patch_vt1702},
39093218c178STakashi Iwai 	{ .id = 0x11067398, .name = "VT1702",
3910d949cac1SHarald Welte 	  .patch = patch_vt1702},
3911eb7188caSLydia Wang 	{ .id = 0x11060428, .name = "VT1718S",
3912eb7188caSLydia Wang 	  .patch = patch_vt1718S},
3913eb7188caSLydia Wang 	{ .id = 0x11064428, .name = "VT1718S",
3914eb7188caSLydia Wang 	  .patch = patch_vt1718S},
3915bb3c6bfcSLydia Wang 	{ .id = 0x11060441, .name = "VT2020",
3916bb3c6bfcSLydia Wang 	  .patch = patch_vt1718S},
3917bb3c6bfcSLydia Wang 	{ .id = 0x11064441, .name = "VT1828S",
3918bb3c6bfcSLydia Wang 	  .patch = patch_vt1718S},
3919f3db423dSLydia Wang 	{ .id = 0x11060433, .name = "VT1716S",
3920f3db423dSLydia Wang 	  .patch = patch_vt1716S},
3921f3db423dSLydia Wang 	{ .id = 0x1106a721, .name = "VT1716S",
3922f3db423dSLydia Wang 	  .patch = patch_vt1716S},
392325eaba2fSLydia Wang 	{ .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
392425eaba2fSLydia Wang 	{ .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
3925ab6734e7SLydia Wang 	{ .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
392636dd5c4aSLydia Wang 	{ .id = 0x11060440, .name = "VT1818S",
392736dd5c4aSLydia Wang 	  .patch = patch_vt1708S},
392811890956SLydia Wang 	{ .id = 0x11060446, .name = "VT1802",
392911890956SLydia Wang 		.patch = patch_vt2002P},
393011890956SLydia Wang 	{ .id = 0x11068446, .name = "VT1802",
393111890956SLydia Wang 		.patch = patch_vt2002P},
3932c577b8a1SJoseph Chan 	{} /* terminator */
3933c577b8a1SJoseph Chan };
39341289e9e8STakashi Iwai 
39351289e9e8STakashi Iwai MODULE_ALIAS("snd-hda-codec-id:1106*");
39361289e9e8STakashi Iwai 
39371289e9e8STakashi Iwai static struct hda_codec_preset_list via_list = {
39381289e9e8STakashi Iwai 	.preset = snd_hda_preset_via,
39391289e9e8STakashi Iwai 	.owner = THIS_MODULE,
39401289e9e8STakashi Iwai };
39411289e9e8STakashi Iwai 
39421289e9e8STakashi Iwai MODULE_LICENSE("GPL");
39431289e9e8STakashi Iwai MODULE_DESCRIPTION("VIA HD-audio codec");
39441289e9e8STakashi Iwai 
39451289e9e8STakashi Iwai static int __init patch_via_init(void)
39461289e9e8STakashi Iwai {
39471289e9e8STakashi Iwai 	return snd_hda_add_codec_preset(&via_list);
39481289e9e8STakashi Iwai }
39491289e9e8STakashi Iwai 
39501289e9e8STakashi Iwai static void __exit patch_via_exit(void)
39511289e9e8STakashi Iwai {
39521289e9e8STakashi Iwai 	snd_hda_delete_codec_preset(&via_list);
39531289e9e8STakashi Iwai }
39541289e9e8STakashi Iwai 
39551289e9e8STakashi Iwai module_init(patch_via_init)
39561289e9e8STakashi Iwai module_exit(patch_via_exit)
3957