xref: /openbmc/linux/sound/pci/hda/patch_via.c (revision 2525050518496dfd6905abfa8d6d34288eed36d7)
1c577b8a1SJoseph Chan /*
2c577b8a1SJoseph Chan  * Universal Interface for Intel High Definition Audio Codec
3c577b8a1SJoseph Chan  *
48e86597fSLydia Wang  * HD audio interface patch for VIA VT17xx/VT18xx/VT20xx codec
5c577b8a1SJoseph Chan  *
68e86597fSLydia Wang  *  (C) 2006-2009 VIA Technology, Inc.
78e86597fSLydia Wang  *  (C) 2006-2008 Takashi Iwai <tiwai@suse.de>
8c577b8a1SJoseph Chan  *
9c577b8a1SJoseph Chan  *  This driver is free software; you can redistribute it and/or modify
10c577b8a1SJoseph Chan  *  it under the terms of the GNU General Public License as published by
11c577b8a1SJoseph Chan  *  the Free Software Foundation; either version 2 of the License, or
12c577b8a1SJoseph Chan  *  (at your option) any later version.
13c577b8a1SJoseph Chan  *
14c577b8a1SJoseph Chan  *  This driver is distributed in the hope that it will be useful,
15c577b8a1SJoseph Chan  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16c577b8a1SJoseph Chan  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17c577b8a1SJoseph Chan  *  GNU General Public License for more details.
18c577b8a1SJoseph Chan  *
19c577b8a1SJoseph Chan  *  You should have received a copy of the GNU General Public License
20c577b8a1SJoseph Chan  *  along with this program; if not, write to the Free Software
21c577b8a1SJoseph Chan  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22c577b8a1SJoseph Chan  */
23c577b8a1SJoseph Chan 
24c577b8a1SJoseph Chan /* * * * * * * * * * * * * * Release History * * * * * * * * * * * * * * * * */
25c577b8a1SJoseph Chan /*									     */
26c577b8a1SJoseph Chan /* 2006-03-03  Lydia Wang  Create the basic patch to support VT1708 codec    */
27c577b8a1SJoseph Chan /* 2006-03-14  Lydia Wang  Modify hard code for some pin widget nid	     */
28c577b8a1SJoseph Chan /* 2006-08-02  Lydia Wang  Add support to VT1709 codec			     */
29c577b8a1SJoseph Chan /* 2006-09-08  Lydia Wang  Fix internal loopback recording source select bug */
30f7278fd0SJosepch Chan /* 2007-09-12  Lydia Wang  Add EAPD enable during driver initialization	     */
31f7278fd0SJosepch Chan /* 2007-09-17  Lydia Wang  Add VT1708B codec support			    */
3276d9b0ddSHarald Welte /* 2007-11-14  Lydia Wang  Add VT1708A codec HP and CD pin connect config    */
33fb4cb772SHarald Welte /* 2008-02-03  Lydia Wang  Fix Rear channels and Back channels inverse issue */
34d949cac1SHarald Welte /* 2008-03-06  Lydia Wang  Add VT1702 codec and VT1708S codec support	     */
3569e52a80SHarald Welte /* 2008-04-09  Lydia Wang  Add mute front speaker when HP plugin	     */
360aa62aefSHarald Welte /* 2008-04-09  Lydia Wang  Add Independent HP feature			     */
3798aa34c0SHarald Welte /* 2008-05-28  Lydia Wang  Add second S/PDIF Out support for VT1702	     */
38d7426329SHarald Welte /* 2008-09-15  Logan Li	   Add VT1708S Mic Boost workaround/backdoor	     */
398e86597fSLydia Wang /* 2009-02-16  Logan Li	   Add support for VT1718S			     */
408e86597fSLydia Wang /* 2009-03-13  Logan Li	   Add support for VT1716S			     */
418e86597fSLydia Wang /* 2009-04-14  Lydai Wang  Add support for VT1828S and VT2020		     */
428e86597fSLydia Wang /* 2009-07-08  Lydia Wang  Add support for VT2002P			     */
438e86597fSLydia Wang /* 2009-07-21  Lydia Wang  Add support for VT1812			     */
4436dd5c4aSLydia Wang /* 2009-09-19  Lydia Wang  Add support for VT1818S			     */
45c577b8a1SJoseph Chan /*									     */
46c577b8a1SJoseph Chan /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
47c577b8a1SJoseph Chan 
48c577b8a1SJoseph Chan 
49c577b8a1SJoseph Chan #include <linux/init.h>
50c577b8a1SJoseph Chan #include <linux/delay.h>
51c577b8a1SJoseph Chan #include <linux/slab.h>
52c577b8a1SJoseph Chan #include <sound/core.h>
530aa62aefSHarald Welte #include <sound/asoundef.h>
54c577b8a1SJoseph Chan #include "hda_codec.h"
55c577b8a1SJoseph Chan #include "hda_local.h"
56c577b8a1SJoseph Chan 
57c577b8a1SJoseph Chan /* Pin Widget NID */
5876d9b0ddSHarald Welte #define VT1708_HP_PIN_NID	0x20
5976d9b0ddSHarald Welte #define VT1708_CD_PIN_NID	0x24
60c577b8a1SJoseph Chan 
61d7426329SHarald Welte enum VIA_HDA_CODEC {
62d7426329SHarald Welte 	UNKNOWN = -1,
63d7426329SHarald Welte 	VT1708,
64d7426329SHarald Welte 	VT1709_10CH,
65d7426329SHarald Welte 	VT1709_6CH,
66d7426329SHarald Welte 	VT1708B_8CH,
67d7426329SHarald Welte 	VT1708B_4CH,
68d7426329SHarald Welte 	VT1708S,
69518bf3baSLydia Wang 	VT1708BCE,
70d7426329SHarald Welte 	VT1702,
71eb7188caSLydia Wang 	VT1718S,
72f3db423dSLydia Wang 	VT1716S,
7325eaba2fSLydia Wang 	VT2002P,
74ab6734e7SLydia Wang 	VT1812,
7511890956SLydia Wang 	VT1802,
76d7426329SHarald Welte 	CODEC_TYPES,
77d7426329SHarald Welte };
78d7426329SHarald Welte 
7911890956SLydia Wang #define VT2002P_COMPATIBLE(spec) \
8011890956SLydia Wang 	((spec)->codec_type == VT2002P ||\
8111890956SLydia Wang 	 (spec)->codec_type == VT1812 ||\
8211890956SLydia Wang 	 (spec)->codec_type == VT1802)
8311890956SLydia Wang 
848e3679dcSTakashi Iwai #define MAX_NID_PATH_DEPTH	5
858e3679dcSTakashi Iwai 
8609a9ad69STakashi Iwai /* output-path: DAC -> ... -> pin
8709a9ad69STakashi Iwai  * idx[] contains the source index number of the next widget;
8809a9ad69STakashi Iwai  * e.g. idx[0] is the index of the DAC selected by path[1] widget
8909a9ad69STakashi Iwai  * multi[] indicates whether it's a selector widget with multi-connectors
9009a9ad69STakashi Iwai  * (i.e. the connection selection is mandatory)
9109a9ad69STakashi Iwai  * vol_ctl and mute_ctl contains the NIDs for the assigned mixers
9209a9ad69STakashi Iwai  */
934a79616dSTakashi Iwai struct nid_path {
944a79616dSTakashi Iwai 	int depth;
958e3679dcSTakashi Iwai 	hda_nid_t path[MAX_NID_PATH_DEPTH];
9609a9ad69STakashi Iwai 	unsigned char idx[MAX_NID_PATH_DEPTH];
9709a9ad69STakashi Iwai 	unsigned char multi[MAX_NID_PATH_DEPTH];
9809a9ad69STakashi Iwai 	unsigned int vol_ctl;
9909a9ad69STakashi Iwai 	unsigned int mute_ctl;
1004a79616dSTakashi Iwai };
1014a79616dSTakashi Iwai 
102a86a88eaSTakashi Iwai /* input-path */
103a86a88eaSTakashi Iwai struct via_input {
104a86a88eaSTakashi Iwai 	hda_nid_t pin;	/* input-pin or aa-mix */
105a86a88eaSTakashi Iwai 	int adc_idx;	/* ADC index to be used */
106a86a88eaSTakashi Iwai 	int mux_idx;	/* MUX index (if any) */
107a86a88eaSTakashi Iwai 	const char *label;	/* input-source label */
108a86a88eaSTakashi Iwai };
109a86a88eaSTakashi Iwai 
1101f2e99feSLydia Wang struct via_spec {
1111f2e99feSLydia Wang 	/* codec parameterization */
11290dd48a1STakashi Iwai 	const struct snd_kcontrol_new *mixers[6];
1131f2e99feSLydia Wang 	unsigned int num_mixers;
1141f2e99feSLydia Wang 
11590dd48a1STakashi Iwai 	const struct hda_verb *init_verbs[5];
1161f2e99feSLydia Wang 	unsigned int num_iverbs;
1171f2e99feSLydia Wang 
11882673bc8STakashi Iwai 	char stream_name_analog[32];
1197eb56e84STakashi Iwai 	char stream_name_hp[32];
12090dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_analog_playback;
12190dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_analog_capture;
1221f2e99feSLydia Wang 
12382673bc8STakashi Iwai 	char stream_name_digital[32];
12490dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_digital_playback;
12590dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_digital_capture;
1261f2e99feSLydia Wang 
1271f2e99feSLydia Wang 	/* playback */
1281f2e99feSLydia Wang 	struct hda_multi_out multiout;
1291f2e99feSLydia Wang 	hda_nid_t slave_dig_outs[2];
130ece8d043STakashi Iwai 	hda_nid_t hp_dac_nid;
131*25250505STakashi Iwai 	bool hp_indep_shared;	/* indep HP-DAC is shared with side ch */
132ada509ecSTakashi Iwai 	int num_active_streams;
1331f2e99feSLydia Wang 
1344a79616dSTakashi Iwai 	struct nid_path out_path[4];
1354a79616dSTakashi Iwai 	struct nid_path hp_path;
1364a79616dSTakashi Iwai 	struct nid_path hp_dep_path;
1374a918ffeSTakashi Iwai 	struct nid_path speaker_path;
1384a79616dSTakashi Iwai 
1391f2e99feSLydia Wang 	/* capture */
1401f2e99feSLydia Wang 	unsigned int num_adc_nids;
141a766d0d7STakashi Iwai 	hda_nid_t adc_nids[3];
1421f2e99feSLydia Wang 	hda_nid_t mux_nids[3];
143620e2b28STakashi Iwai 	hda_nid_t aa_mix_nid;
1441f2e99feSLydia Wang 	hda_nid_t dig_in_nid;
1451f2e99feSLydia Wang 
1461f2e99feSLydia Wang 	/* capture source */
147a86a88eaSTakashi Iwai 	bool dyn_adc_switch;
148a86a88eaSTakashi Iwai 	int num_inputs;
149a86a88eaSTakashi Iwai 	struct via_input inputs[AUTO_CFG_MAX_INS + 1];
1501f2e99feSLydia Wang 	unsigned int cur_mux[3];
1511f2e99feSLydia Wang 
152a86a88eaSTakashi Iwai 	/* dynamic ADC switching */
153a86a88eaSTakashi Iwai 	hda_nid_t cur_adc;
154a86a88eaSTakashi Iwai 	unsigned int cur_adc_stream_tag;
155a86a88eaSTakashi Iwai 	unsigned int cur_adc_format;
156a86a88eaSTakashi Iwai 
1571f2e99feSLydia Wang 	/* PCM information */
1581f2e99feSLydia Wang 	struct hda_pcm pcm_rec[3];
1591f2e99feSLydia Wang 
1601f2e99feSLydia Wang 	/* dynamic controls, init_verbs and input_mux */
1611f2e99feSLydia Wang 	struct auto_pin_cfg autocfg;
1621f2e99feSLydia Wang 	struct snd_array kctls;
1631f2e99feSLydia Wang 	hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
1641f2e99feSLydia Wang 
1651f2e99feSLydia Wang 	/* HP mode source */
1661f2e99feSLydia Wang 	unsigned int hp_independent_mode;
167f3db423dSLydia Wang 	unsigned int dmic_enabled;
16824088a58STakashi Iwai 	unsigned int no_pin_power_ctl;
1691f2e99feSLydia Wang 	enum VIA_HDA_CODEC codec_type;
1701f2e99feSLydia Wang 
171e3d7a143STakashi Iwai 	/* smart51 setup */
172e3d7a143STakashi Iwai 	unsigned int smart51_nums;
173e3d7a143STakashi Iwai 	hda_nid_t smart51_pins[2];
174e3d7a143STakashi Iwai 	int smart51_idxs[2];
175e3d7a143STakashi Iwai 	const char *smart51_labels[2];
176e3d7a143STakashi Iwai 	unsigned int smart51_enabled;
177e3d7a143STakashi Iwai 
1781f2e99feSLydia Wang 	/* work to check hp jack state */
1791f2e99feSLydia Wang 	struct hda_codec *codec;
1801f2e99feSLydia Wang 	struct delayed_work vt1708_hp_work;
181e06e5a29STakashi Iwai 	int vt1708_jack_detect;
1821f2e99feSLydia Wang 	int vt1708_hp_present;
1833e95b9abSLydia Wang 
1843e95b9abSLydia Wang 	void (*set_widgets_power_state)(struct hda_codec *codec);
1853e95b9abSLydia Wang 
1861f2e99feSLydia Wang 	struct hda_loopback_check loopback;
18713af8e77STakashi Iwai 	int num_loopbacks;
18813af8e77STakashi Iwai 	struct hda_amp_list loopback_list[8];
189a86a88eaSTakashi Iwai 
190a86a88eaSTakashi Iwai 	/* bind capture-volume */
191a86a88eaSTakashi Iwai 	struct hda_bind_ctls *bind_cap_vol;
192a86a88eaSTakashi Iwai 	struct hda_bind_ctls *bind_cap_sw;
1931f2e99feSLydia Wang };
1941f2e99feSLydia Wang 
1950341ccd7SLydia Wang static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
1965b0cb1d8SJaroslav Kysela static struct via_spec * via_new_spec(struct hda_codec *codec)
1975b0cb1d8SJaroslav Kysela {
1985b0cb1d8SJaroslav Kysela 	struct via_spec *spec;
1995b0cb1d8SJaroslav Kysela 
2005b0cb1d8SJaroslav Kysela 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2015b0cb1d8SJaroslav Kysela 	if (spec == NULL)
2025b0cb1d8SJaroslav Kysela 		return NULL;
2035b0cb1d8SJaroslav Kysela 
2045b0cb1d8SJaroslav Kysela 	codec->spec = spec;
2055b0cb1d8SJaroslav Kysela 	spec->codec = codec;
2060341ccd7SLydia Wang 	spec->codec_type = get_codec_type(codec);
2070341ccd7SLydia Wang 	/* VT1708BCE & VT1708S are almost same */
2080341ccd7SLydia Wang 	if (spec->codec_type == VT1708BCE)
2090341ccd7SLydia Wang 		spec->codec_type = VT1708S;
2105b0cb1d8SJaroslav Kysela 	return spec;
2115b0cb1d8SJaroslav Kysela }
2125b0cb1d8SJaroslav Kysela 
213744ff5f4SLydia Wang static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
214d7426329SHarald Welte {
215744ff5f4SLydia Wang 	u32 vendor_id = codec->vendor_id;
216d7426329SHarald Welte 	u16 ven_id = vendor_id >> 16;
217d7426329SHarald Welte 	u16 dev_id = vendor_id & 0xffff;
218d7426329SHarald Welte 	enum VIA_HDA_CODEC codec_type;
219d7426329SHarald Welte 
220d7426329SHarald Welte 	/* get codec type */
221d7426329SHarald Welte 	if (ven_id != 0x1106)
222d7426329SHarald Welte 		codec_type = UNKNOWN;
223d7426329SHarald Welte 	else if (dev_id >= 0x1708 && dev_id <= 0x170b)
224d7426329SHarald Welte 		codec_type = VT1708;
225d7426329SHarald Welte 	else if (dev_id >= 0xe710 && dev_id <= 0xe713)
226d7426329SHarald Welte 		codec_type = VT1709_10CH;
227d7426329SHarald Welte 	else if (dev_id >= 0xe714 && dev_id <= 0xe717)
228d7426329SHarald Welte 		codec_type = VT1709_6CH;
229518bf3baSLydia Wang 	else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
230d7426329SHarald Welte 		codec_type = VT1708B_8CH;
231518bf3baSLydia Wang 		if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
232518bf3baSLydia Wang 			codec_type = VT1708BCE;
233518bf3baSLydia Wang 	} else if (dev_id >= 0xe724 && dev_id <= 0xe727)
234d7426329SHarald Welte 		codec_type = VT1708B_4CH;
235d7426329SHarald Welte 	else if ((dev_id & 0xfff) == 0x397
236d7426329SHarald Welte 		 && (dev_id >> 12) < 8)
237d7426329SHarald Welte 		codec_type = VT1708S;
238d7426329SHarald Welte 	else if ((dev_id & 0xfff) == 0x398
239d7426329SHarald Welte 		 && (dev_id >> 12) < 8)
240d7426329SHarald Welte 		codec_type = VT1702;
241eb7188caSLydia Wang 	else if ((dev_id & 0xfff) == 0x428
242eb7188caSLydia Wang 		 && (dev_id >> 12) < 8)
243eb7188caSLydia Wang 		codec_type = VT1718S;
244f3db423dSLydia Wang 	else if (dev_id == 0x0433 || dev_id == 0xa721)
245f3db423dSLydia Wang 		codec_type = VT1716S;
246bb3c6bfcSLydia Wang 	else if (dev_id == 0x0441 || dev_id == 0x4441)
247bb3c6bfcSLydia Wang 		codec_type = VT1718S;
24825eaba2fSLydia Wang 	else if (dev_id == 0x0438 || dev_id == 0x4438)
24925eaba2fSLydia Wang 		codec_type = VT2002P;
250ab6734e7SLydia Wang 	else if (dev_id == 0x0448)
251ab6734e7SLydia Wang 		codec_type = VT1812;
25236dd5c4aSLydia Wang 	else if (dev_id == 0x0440)
25336dd5c4aSLydia Wang 		codec_type = VT1708S;
25411890956SLydia Wang 	else if ((dev_id & 0xfff) == 0x446)
25511890956SLydia Wang 		codec_type = VT1802;
256d7426329SHarald Welte 	else
257d7426329SHarald Welte 		codec_type = UNKNOWN;
258d7426329SHarald Welte 	return codec_type;
259d7426329SHarald Welte };
260d7426329SHarald Welte 
261ec7e7e42SLydia Wang #define VIA_JACK_EVENT		0x20
26269e52a80SHarald Welte #define VIA_HP_EVENT		0x01
26369e52a80SHarald Welte #define VIA_GPIO_EVENT		0x02
2644a918ffeSTakashi Iwai #define VIA_LINE_EVENT		0x03
26569e52a80SHarald Welte 
266c577b8a1SJoseph Chan enum {
267c577b8a1SJoseph Chan 	VIA_CTL_WIDGET_VOL,
268c577b8a1SJoseph Chan 	VIA_CTL_WIDGET_MUTE,
269f5271101SLydia Wang 	VIA_CTL_WIDGET_ANALOG_MUTE,
270c577b8a1SJoseph Chan };
271c577b8a1SJoseph Chan 
272ada509ecSTakashi Iwai static void analog_low_current_mode(struct hda_codec *codec);
273ada509ecSTakashi Iwai static bool is_aa_path_mute(struct hda_codec *codec);
2741f2e99feSLydia Wang 
2751f2e99feSLydia Wang static void vt1708_start_hp_work(struct via_spec *spec)
2761f2e99feSLydia Wang {
2771f2e99feSLydia Wang 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
2781f2e99feSLydia Wang 		return;
2791f2e99feSLydia Wang 	snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
280e06e5a29STakashi Iwai 			    !spec->vt1708_jack_detect);
2811f2e99feSLydia Wang 	if (!delayed_work_pending(&spec->vt1708_hp_work))
2821f2e99feSLydia Wang 		schedule_delayed_work(&spec->vt1708_hp_work,
2831f2e99feSLydia Wang 				      msecs_to_jiffies(100));
2841f2e99feSLydia Wang }
2851f2e99feSLydia Wang 
2861f2e99feSLydia Wang static void vt1708_stop_hp_work(struct via_spec *spec)
2871f2e99feSLydia Wang {
2881f2e99feSLydia Wang 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
2891f2e99feSLydia Wang 		return;
2901f2e99feSLydia Wang 	if (snd_hda_get_bool_hint(spec->codec, "analog_loopback_hp_detect") == 1
2911f2e99feSLydia Wang 	    && !is_aa_path_mute(spec->codec))
2921f2e99feSLydia Wang 		return;
2931f2e99feSLydia Wang 	snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
294e06e5a29STakashi Iwai 			    !spec->vt1708_jack_detect);
2955b84ba26STejun Heo 	cancel_delayed_work_sync(&spec->vt1708_hp_work);
2961f2e99feSLydia Wang }
297f5271101SLydia Wang 
2983e95b9abSLydia Wang static void set_widgets_power_state(struct hda_codec *codec)
2993e95b9abSLydia Wang {
3003e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
3013e95b9abSLydia Wang 	if (spec->set_widgets_power_state)
3023e95b9abSLydia Wang 		spec->set_widgets_power_state(codec);
3033e95b9abSLydia Wang }
30425eaba2fSLydia Wang 
305f5271101SLydia Wang static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
306f5271101SLydia Wang 				   struct snd_ctl_elem_value *ucontrol)
307f5271101SLydia Wang {
308f5271101SLydia Wang 	int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
309f5271101SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
310f5271101SLydia Wang 
3113e95b9abSLydia Wang 	set_widgets_power_state(codec);
312ada509ecSTakashi Iwai 	analog_low_current_mode(snd_kcontrol_chip(kcontrol));
3131f2e99feSLydia Wang 	if (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1) {
3141f2e99feSLydia Wang 		if (is_aa_path_mute(codec))
3151f2e99feSLydia Wang 			vt1708_start_hp_work(codec->spec);
3161f2e99feSLydia Wang 		else
3171f2e99feSLydia Wang 			vt1708_stop_hp_work(codec->spec);
3181f2e99feSLydia Wang 	}
319f5271101SLydia Wang 	return change;
320f5271101SLydia Wang }
321f5271101SLydia Wang 
322f5271101SLydia Wang /* modify .put = snd_hda_mixer_amp_switch_put */
323f5271101SLydia Wang #define ANALOG_INPUT_MUTE						\
324f5271101SLydia Wang 	{		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,		\
325f5271101SLydia Wang 			.name = NULL,					\
326f5271101SLydia Wang 			.index = 0,					\
327f5271101SLydia Wang 			.info = snd_hda_mixer_amp_switch_info,		\
328f5271101SLydia Wang 			.get = snd_hda_mixer_amp_switch_get,		\
329f5271101SLydia Wang 			.put = analog_input_switch_put,			\
330f5271101SLydia Wang 			.private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
331f5271101SLydia Wang 
33290dd48a1STakashi Iwai static const struct snd_kcontrol_new via_control_templates[] = {
333c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME(NULL, 0, 0, 0),
334c577b8a1SJoseph Chan 	HDA_CODEC_MUTE(NULL, 0, 0, 0),
335f5271101SLydia Wang 	ANALOG_INPUT_MUTE,
336c577b8a1SJoseph Chan };
337c577b8a1SJoseph Chan 
338ab6734e7SLydia Wang 
339c577b8a1SJoseph Chan /* add dynamic controls */
340291c9e33STakashi Iwai static struct snd_kcontrol_new *__via_clone_ctl(struct via_spec *spec,
341291c9e33STakashi Iwai 				const struct snd_kcontrol_new *tmpl,
342291c9e33STakashi Iwai 				const char *name)
343c577b8a1SJoseph Chan {
344c577b8a1SJoseph Chan 	struct snd_kcontrol_new *knew;
345c577b8a1SJoseph Chan 
346603c4019STakashi Iwai 	snd_array_init(&spec->kctls, sizeof(*knew), 32);
347603c4019STakashi Iwai 	knew = snd_array_new(&spec->kctls);
348c577b8a1SJoseph Chan 	if (!knew)
349291c9e33STakashi Iwai 		return NULL;
350291c9e33STakashi Iwai 	*knew = *tmpl;
351291c9e33STakashi Iwai 	if (!name)
352291c9e33STakashi Iwai 		name = tmpl->name;
353291c9e33STakashi Iwai 	if (name) {
354c577b8a1SJoseph Chan 		knew->name = kstrdup(name, GFP_KERNEL);
355c577b8a1SJoseph Chan 		if (!knew->name)
356291c9e33STakashi Iwai 			return NULL;
357291c9e33STakashi Iwai 	}
358291c9e33STakashi Iwai 	return knew;
359291c9e33STakashi Iwai }
360291c9e33STakashi Iwai 
361291c9e33STakashi Iwai static int __via_add_control(struct via_spec *spec, int type, const char *name,
362291c9e33STakashi Iwai 			     int idx, unsigned long val)
363291c9e33STakashi Iwai {
364291c9e33STakashi Iwai 	struct snd_kcontrol_new *knew;
365291c9e33STakashi Iwai 
366291c9e33STakashi Iwai 	knew = __via_clone_ctl(spec, &via_control_templates[type], name);
367291c9e33STakashi Iwai 	if (!knew)
368c577b8a1SJoseph Chan 		return -ENOMEM;
369d7a99cceSTakashi Iwai 	knew->index = idx;
3704d02d1b6SJaroslav Kysela 	if (get_amp_nid_(val))
3715e26dfd0SJaroslav Kysela 		knew->subdevice = HDA_SUBDEV_AMP_FLAG;
372c577b8a1SJoseph Chan 	knew->private_value = val;
373c577b8a1SJoseph Chan 	return 0;
374c577b8a1SJoseph Chan }
375c577b8a1SJoseph Chan 
3767b315bb4STakashi Iwai #define via_add_control(spec, type, name, val) \
3777b315bb4STakashi Iwai 	__via_add_control(spec, type, name, 0, val)
3787b315bb4STakashi Iwai 
379291c9e33STakashi Iwai #define via_clone_control(spec, tmpl) __via_clone_ctl(spec, tmpl, NULL)
3805b0cb1d8SJaroslav Kysela 
381603c4019STakashi Iwai static void via_free_kctls(struct hda_codec *codec)
382603c4019STakashi Iwai {
383603c4019STakashi Iwai 	struct via_spec *spec = codec->spec;
384603c4019STakashi Iwai 
385603c4019STakashi Iwai 	if (spec->kctls.list) {
386603c4019STakashi Iwai 		struct snd_kcontrol_new *kctl = spec->kctls.list;
387603c4019STakashi Iwai 		int i;
388603c4019STakashi Iwai 		for (i = 0; i < spec->kctls.used; i++)
389603c4019STakashi Iwai 			kfree(kctl[i].name);
390603c4019STakashi Iwai 	}
391603c4019STakashi Iwai 	snd_array_free(&spec->kctls);
392603c4019STakashi Iwai }
393603c4019STakashi Iwai 
394c577b8a1SJoseph Chan /* create input playback/capture controls for the given pin */
3959510e8ddSLydia Wang static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
3967b315bb4STakashi Iwai 				int type_idx, int idx, int mix_nid)
397c577b8a1SJoseph Chan {
398c577b8a1SJoseph Chan 	char name[32];
399c577b8a1SJoseph Chan 	int err;
400c577b8a1SJoseph Chan 
401c577b8a1SJoseph Chan 	sprintf(name, "%s Playback Volume", ctlname);
4027b315bb4STakashi Iwai 	err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
403c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
404c577b8a1SJoseph Chan 	if (err < 0)
405c577b8a1SJoseph Chan 		return err;
406c577b8a1SJoseph Chan 	sprintf(name, "%s Playback Switch", ctlname);
4077b315bb4STakashi Iwai 	err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
408c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
409c577b8a1SJoseph Chan 	if (err < 0)
410c577b8a1SJoseph Chan 		return err;
411c577b8a1SJoseph Chan 	return 0;
412c577b8a1SJoseph Chan }
413c577b8a1SJoseph Chan 
4145d41762aSTakashi Iwai #define get_connection_index(codec, mux, nid) \
4158d087c76STakashi Iwai 	snd_hda_get_conn_index(codec, mux, nid, 0)
4165d41762aSTakashi Iwai 
4178df2a312STakashi Iwai static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
4188df2a312STakashi Iwai 			   unsigned int mask)
4198df2a312STakashi Iwai {
420a934d5a9STakashi Iwai 	unsigned int caps;
421a934d5a9STakashi Iwai 	if (!nid)
422a934d5a9STakashi Iwai 		return false;
423a934d5a9STakashi Iwai 	caps = get_wcaps(codec, nid);
4248df2a312STakashi Iwai 	if (dir == HDA_INPUT)
4258df2a312STakashi Iwai 		caps &= AC_WCAP_IN_AMP;
4268df2a312STakashi Iwai 	else
4278df2a312STakashi Iwai 		caps &= AC_WCAP_OUT_AMP;
4288df2a312STakashi Iwai 	if (!caps)
4298df2a312STakashi Iwai 		return false;
4308df2a312STakashi Iwai 	if (query_amp_caps(codec, nid, dir) & mask)
4318df2a312STakashi Iwai 		return true;
4328df2a312STakashi Iwai 	return false;
4338df2a312STakashi Iwai }
4348df2a312STakashi Iwai 
43509a9ad69STakashi Iwai #define have_mute(codec, nid, dir) \
43609a9ad69STakashi Iwai 	check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
4378df2a312STakashi Iwai 
43809a9ad69STakashi Iwai /* enable/disable the output-route */
43909a9ad69STakashi Iwai static void activate_output_path(struct hda_codec *codec, struct nid_path *path,
44009a9ad69STakashi Iwai 				 bool enable, bool force)
4415d41762aSTakashi Iwai {
44209a9ad69STakashi Iwai 	int i;
44309a9ad69STakashi Iwai 	for (i = 0; i < path->depth; i++) {
44409a9ad69STakashi Iwai 		hda_nid_t src, dst;
44509a9ad69STakashi Iwai 		int idx = path->idx[i];
44609a9ad69STakashi Iwai 		src = path->path[i];
44709a9ad69STakashi Iwai 		if (i < path->depth - 1)
44809a9ad69STakashi Iwai 			dst = path->path[i + 1];
44909a9ad69STakashi Iwai 		else
45009a9ad69STakashi Iwai 			dst = 0;
45109a9ad69STakashi Iwai 		if (enable && path->multi[i])
45209a9ad69STakashi Iwai 			snd_hda_codec_write(codec, dst, 0,
4535d41762aSTakashi Iwai 					    AC_VERB_SET_CONNECT_SEL, idx);
45409a9ad69STakashi Iwai 		if (have_mute(codec, dst, HDA_INPUT)) {
45509a9ad69STakashi Iwai 			int val = enable ? AMP_IN_UNMUTE(idx) :
45609a9ad69STakashi Iwai 				AMP_IN_MUTE(idx);
45709a9ad69STakashi Iwai 			snd_hda_codec_write(codec, dst, 0,
45809a9ad69STakashi Iwai 					    AC_VERB_SET_AMP_GAIN_MUTE, val);
45909a9ad69STakashi Iwai 		}
46009a9ad69STakashi Iwai 		if (!force && (src == path->vol_ctl || src == path->mute_ctl))
46109a9ad69STakashi Iwai 			continue;
46209a9ad69STakashi Iwai 		if (have_mute(codec, src, HDA_OUTPUT)) {
46309a9ad69STakashi Iwai 			int val = enable ? AMP_OUT_UNMUTE : AMP_OUT_MUTE;
46409a9ad69STakashi Iwai 			snd_hda_codec_write(codec, src, 0,
46509a9ad69STakashi Iwai 					    AC_VERB_SET_AMP_GAIN_MUTE, val);
46609a9ad69STakashi Iwai 		}
46709a9ad69STakashi Iwai 	}
4685d41762aSTakashi Iwai }
4695d41762aSTakashi Iwai 
4705d41762aSTakashi Iwai /* set the given pin as output */
4715d41762aSTakashi Iwai static void init_output_pin(struct hda_codec *codec, hda_nid_t pin,
4725d41762aSTakashi Iwai 			    int pin_type)
4735d41762aSTakashi Iwai {
4745d41762aSTakashi Iwai 	if (!pin)
4755d41762aSTakashi Iwai 		return;
4765d41762aSTakashi Iwai 	snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
4775d41762aSTakashi Iwai 			    pin_type);
4785d41762aSTakashi Iwai 	if (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD)
4795d41762aSTakashi Iwai 		snd_hda_codec_write(codec, pin, 0,
480d3a11e60STakashi Iwai 				    AC_VERB_SET_EAPD_BTLENABLE, 0x02);
481c577b8a1SJoseph Chan }
482c577b8a1SJoseph Chan 
48309a9ad69STakashi Iwai static void via_auto_init_output(struct hda_codec *codec,
48409a9ad69STakashi Iwai 				 struct nid_path *path, int pin_type,
48509a9ad69STakashi Iwai 				 bool force)
4865d41762aSTakashi Iwai {
4875d41762aSTakashi Iwai 	struct via_spec *spec = codec->spec;
4885d41762aSTakashi Iwai 	unsigned int caps;
48909a9ad69STakashi Iwai 	hda_nid_t pin, nid;
49009a9ad69STakashi Iwai 	int i, idx;
4915d41762aSTakashi Iwai 
49209a9ad69STakashi Iwai 	if (!path->depth)
4935d41762aSTakashi Iwai 		return;
49409a9ad69STakashi Iwai 	pin = path->path[path->depth - 1];
4955d41762aSTakashi Iwai 
4965d41762aSTakashi Iwai 	init_output_pin(codec, pin, pin_type);
4975d41762aSTakashi Iwai 	caps = query_amp_caps(codec, pin, HDA_OUTPUT);
4985d41762aSTakashi Iwai 	if (caps & AC_AMPCAP_MUTE) {
4995d41762aSTakashi Iwai 		unsigned int val;
5005d41762aSTakashi Iwai 		val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
5015d41762aSTakashi Iwai 		snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
5025d41762aSTakashi Iwai 				    AMP_OUT_MUTE | val);
5035d41762aSTakashi Iwai 	}
5045d41762aSTakashi Iwai 
50509a9ad69STakashi Iwai 	activate_output_path(codec, path, true, force);
5065d41762aSTakashi Iwai 
50709a9ad69STakashi Iwai 	/* initialize the AA-path */
50809a9ad69STakashi Iwai 	if (!spec->aa_mix_nid)
50909a9ad69STakashi Iwai 		return;
51009a9ad69STakashi Iwai 	for (i = path->depth - 1; i > 0; i--) {
51109a9ad69STakashi Iwai 		nid = path->path[i];
51209a9ad69STakashi Iwai 		idx = get_connection_index(codec, nid, spec->aa_mix_nid);
51309a9ad69STakashi Iwai 		if (idx >= 0) {
51409a9ad69STakashi Iwai 			if (have_mute(codec, nid, HDA_INPUT))
51509a9ad69STakashi Iwai 				snd_hda_codec_write(codec, nid, 0,
51609a9ad69STakashi Iwai 						    AC_VERB_SET_AMP_GAIN_MUTE,
51709a9ad69STakashi Iwai 						    AMP_IN_UNMUTE(idx));
51809a9ad69STakashi Iwai 			break;
51909a9ad69STakashi Iwai 		}
52009a9ad69STakashi Iwai 	}
52109a9ad69STakashi Iwai }
522c577b8a1SJoseph Chan 
523c577b8a1SJoseph Chan static void via_auto_init_multi_out(struct hda_codec *codec)
524c577b8a1SJoseph Chan {
525c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
526c577b8a1SJoseph Chan 	int i;
527c577b8a1SJoseph Chan 
528e3d7a143STakashi Iwai 	for (i = 0; i < spec->autocfg.line_outs + spec->smart51_nums; i++)
52909a9ad69STakashi Iwai 		via_auto_init_output(codec, &spec->out_path[i], PIN_OUT, true);
530c577b8a1SJoseph Chan }
531c577b8a1SJoseph Chan 
532c577b8a1SJoseph Chan static void via_auto_init_hp_out(struct hda_codec *codec)
533c577b8a1SJoseph Chan {
534c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
535c577b8a1SJoseph Chan 
53609a9ad69STakashi Iwai 	if (!spec->hp_dac_nid) {
53709a9ad69STakashi Iwai 		via_auto_init_output(codec, &spec->hp_dep_path, PIN_HP, true);
53809a9ad69STakashi Iwai 		return;
53909a9ad69STakashi Iwai 	}
54009a9ad69STakashi Iwai 	if (spec->hp_independent_mode) {
54109a9ad69STakashi Iwai 		activate_output_path(codec, &spec->hp_dep_path, false, false);
54209a9ad69STakashi Iwai 		via_auto_init_output(codec, &spec->hp_path, PIN_HP, true);
54309a9ad69STakashi Iwai 	} else {
54409a9ad69STakashi Iwai 		activate_output_path(codec, &spec->hp_path, false, false);
54509a9ad69STakashi Iwai 		via_auto_init_output(codec, &spec->hp_dep_path, PIN_HP, true);
54609a9ad69STakashi Iwai 	}
54725eaba2fSLydia Wang }
548c577b8a1SJoseph Chan 
5494a918ffeSTakashi Iwai static void via_auto_init_speaker_out(struct hda_codec *codec)
5504a918ffeSTakashi Iwai {
5514a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
5524a918ffeSTakashi Iwai 
5534a918ffeSTakashi Iwai 	if (spec->autocfg.speaker_outs)
55409a9ad69STakashi Iwai 		via_auto_init_output(codec, &spec->speaker_path, PIN_OUT, true);
5554a918ffeSTakashi Iwai }
5564a918ffeSTakashi Iwai 
557f4a7828bSTakashi Iwai static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin);
55832e0191dSClemens Ladisch 
559c577b8a1SJoseph Chan static void via_auto_init_analog_input(struct hda_codec *codec)
560c577b8a1SJoseph Chan {
561c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
5627b315bb4STakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
563096a8854STakashi Iwai 	hda_nid_t conn[HDA_MAX_CONNECTIONS];
56432e0191dSClemens Ladisch 	unsigned int ctl;
565096a8854STakashi Iwai 	int i, num_conns;
566c577b8a1SJoseph Chan 
567096a8854STakashi Iwai 	/* init ADCs */
568096a8854STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
569096a8854STakashi Iwai 		snd_hda_codec_write(codec, spec->adc_nids[i], 0,
570096a8854STakashi Iwai 				    AC_VERB_SET_AMP_GAIN_MUTE,
571096a8854STakashi Iwai 				    AMP_IN_UNMUTE(0));
572096a8854STakashi Iwai 	}
573096a8854STakashi Iwai 
574096a8854STakashi Iwai 	/* init pins */
5757b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
5767b315bb4STakashi Iwai 		hda_nid_t nid = cfg->inputs[i].pin;
577f4a7828bSTakashi Iwai 		if (spec->smart51_enabled && is_smart51_pins(codec, nid))
57832e0191dSClemens Ladisch 			ctl = PIN_OUT;
57930649676SDavid Henningsson 		else if (cfg->inputs[i].type == AUTO_PIN_MIC)
58032e0191dSClemens Ladisch 			ctl = PIN_VREF50;
58132e0191dSClemens Ladisch 		else
58232e0191dSClemens Ladisch 			ctl = PIN_IN;
583c577b8a1SJoseph Chan 		snd_hda_codec_write(codec, nid, 0,
58432e0191dSClemens Ladisch 				    AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
585c577b8a1SJoseph Chan 	}
586096a8854STakashi Iwai 
587096a8854STakashi Iwai 	/* init input-src */
588096a8854STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
589a86a88eaSTakashi Iwai 		int adc_idx = spec->inputs[spec->cur_mux[i]].adc_idx;
590a86a88eaSTakashi Iwai 		if (spec->mux_nids[adc_idx]) {
591a86a88eaSTakashi Iwai 			int mux_idx = spec->inputs[spec->cur_mux[i]].mux_idx;
592a86a88eaSTakashi Iwai 			snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
593096a8854STakashi Iwai 					    AC_VERB_SET_CONNECT_SEL,
594a86a88eaSTakashi Iwai 					    mux_idx);
595a86a88eaSTakashi Iwai 		}
596a86a88eaSTakashi Iwai 		if (spec->dyn_adc_switch)
597a86a88eaSTakashi Iwai 			break; /* only one input-src */
598096a8854STakashi Iwai 	}
599096a8854STakashi Iwai 
600096a8854STakashi Iwai 	/* init aa-mixer */
601096a8854STakashi Iwai 	if (!spec->aa_mix_nid)
602096a8854STakashi Iwai 		return;
603096a8854STakashi Iwai 	num_conns = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
604096a8854STakashi Iwai 					    ARRAY_SIZE(conn));
605096a8854STakashi Iwai 	for (i = 0; i < num_conns; i++) {
606096a8854STakashi Iwai 		unsigned int caps = get_wcaps(codec, conn[i]);
607096a8854STakashi Iwai 		if (get_wcaps_type(caps) == AC_WID_PIN)
608096a8854STakashi Iwai 			snd_hda_codec_write(codec, spec->aa_mix_nid, 0,
609096a8854STakashi Iwai 					    AC_VERB_SET_AMP_GAIN_MUTE,
610096a8854STakashi Iwai 					    AMP_IN_MUTE(i));
611096a8854STakashi Iwai 	}
612c577b8a1SJoseph Chan }
613f5271101SLydia Wang 
614f5271101SLydia Wang static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
615f5271101SLydia Wang 				unsigned int *affected_parm)
616f5271101SLydia Wang {
617f5271101SLydia Wang 	unsigned parm;
618f5271101SLydia Wang 	unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
619f5271101SLydia Wang 	unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
620f5271101SLydia Wang 		>> AC_DEFCFG_MISC_SHIFT
621f5271101SLydia Wang 		& AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
6221564b287SLydia Wang 	struct via_spec *spec = codec->spec;
62324088a58STakashi Iwai 	unsigned present = 0;
62424088a58STakashi Iwai 
62524088a58STakashi Iwai 	no_presence |= spec->no_pin_power_ctl;
62624088a58STakashi Iwai 	if (!no_presence)
62724088a58STakashi Iwai 		present = snd_hda_jack_detect(codec, nid);
628f4a7828bSTakashi Iwai 	if ((spec->smart51_enabled && is_smart51_pins(codec, nid))
6291564b287SLydia Wang 	    || ((no_presence || present)
6301564b287SLydia Wang 		&& get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
631f5271101SLydia Wang 		*affected_parm = AC_PWRST_D0; /* if it's connected */
632f5271101SLydia Wang 		parm = AC_PWRST_D0;
633f5271101SLydia Wang 	} else
634f5271101SLydia Wang 		parm = AC_PWRST_D3;
635f5271101SLydia Wang 
636f5271101SLydia Wang 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
637f5271101SLydia Wang }
638f5271101SLydia Wang 
63924088a58STakashi Iwai static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
64024088a58STakashi Iwai 				  struct snd_ctl_elem_info *uinfo)
64124088a58STakashi Iwai {
64224088a58STakashi Iwai 	static const char * const texts[] = {
64324088a58STakashi Iwai 		"Disabled", "Enabled"
64424088a58STakashi Iwai 	};
64524088a58STakashi Iwai 
64624088a58STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
64724088a58STakashi Iwai 	uinfo->count = 1;
64824088a58STakashi Iwai 	uinfo->value.enumerated.items = 2;
64924088a58STakashi Iwai 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
65024088a58STakashi Iwai 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
65124088a58STakashi Iwai 	strcpy(uinfo->value.enumerated.name,
65224088a58STakashi Iwai 	       texts[uinfo->value.enumerated.item]);
65324088a58STakashi Iwai 	return 0;
65424088a58STakashi Iwai }
65524088a58STakashi Iwai 
65624088a58STakashi Iwai static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
65724088a58STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
65824088a58STakashi Iwai {
65924088a58STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
66024088a58STakashi Iwai 	struct via_spec *spec = codec->spec;
66124088a58STakashi Iwai 	ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
66224088a58STakashi Iwai 	return 0;
66324088a58STakashi Iwai }
66424088a58STakashi Iwai 
66524088a58STakashi Iwai static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
66624088a58STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
66724088a58STakashi Iwai {
66824088a58STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
66924088a58STakashi Iwai 	struct via_spec *spec = codec->spec;
67024088a58STakashi Iwai 	unsigned int val = !ucontrol->value.enumerated.item[0];
67124088a58STakashi Iwai 
67224088a58STakashi Iwai 	if (val == spec->no_pin_power_ctl)
67324088a58STakashi Iwai 		return 0;
67424088a58STakashi Iwai 	spec->no_pin_power_ctl = val;
67524088a58STakashi Iwai 	set_widgets_power_state(codec);
67624088a58STakashi Iwai 	return 1;
67724088a58STakashi Iwai }
67824088a58STakashi Iwai 
67924088a58STakashi Iwai static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
68024088a58STakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
68124088a58STakashi Iwai 	.name = "Dynamic Power-Control",
68224088a58STakashi Iwai 	.info = via_pin_power_ctl_info,
68324088a58STakashi Iwai 	.get = via_pin_power_ctl_get,
68424088a58STakashi Iwai 	.put = via_pin_power_ctl_put,
68524088a58STakashi Iwai };
68624088a58STakashi Iwai 
68724088a58STakashi Iwai 
6880aa62aefSHarald Welte static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
6890aa62aefSHarald Welte 				   struct snd_ctl_elem_info *uinfo)
6900aa62aefSHarald Welte {
6918df2a312STakashi Iwai 	static const char * const texts[] = { "OFF", "ON" };
6928df2a312STakashi Iwai 
6938df2a312STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
6948df2a312STakashi Iwai 	uinfo->count = 1;
6958df2a312STakashi Iwai 	uinfo->value.enumerated.items = 2;
6968df2a312STakashi Iwai 	if (uinfo->value.enumerated.item >= 2)
6978df2a312STakashi Iwai 		uinfo->value.enumerated.item = 1;
6988df2a312STakashi Iwai 	strcpy(uinfo->value.enumerated.name,
6998df2a312STakashi Iwai 	       texts[uinfo->value.enumerated.item]);
7008df2a312STakashi Iwai 	return 0;
7010aa62aefSHarald Welte }
7020aa62aefSHarald Welte 
7030aa62aefSHarald Welte static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
7040aa62aefSHarald Welte 				  struct snd_ctl_elem_value *ucontrol)
7050aa62aefSHarald Welte {
7060aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
707cdc1784dSLydia Wang 	struct via_spec *spec = codec->spec;
708cdc1784dSLydia Wang 
709ece8d043STakashi Iwai 	ucontrol->value.enumerated.item[0] = spec->hp_independent_mode;
710cdc1784dSLydia Wang 	return 0;
711cdc1784dSLydia Wang }
712cdc1784dSLydia Wang 
7130aa62aefSHarald Welte static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
7140aa62aefSHarald Welte 				  struct snd_ctl_elem_value *ucontrol)
7150aa62aefSHarald Welte {
7160aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7170aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
718*25250505STakashi Iwai 	int cur;
7198df2a312STakashi Iwai 
720*25250505STakashi Iwai 	/* no independent-hp status change during PCM playback is running */
721*25250505STakashi Iwai 	if (spec->num_active_streams)
722*25250505STakashi Iwai 		return -EBUSY;
723*25250505STakashi Iwai 
724*25250505STakashi Iwai 	cur = !!ucontrol->value.enumerated.item[0];
725*25250505STakashi Iwai 	if (spec->hp_independent_mode == cur)
726*25250505STakashi Iwai 		return 0;
727*25250505STakashi Iwai 	spec->hp_independent_mode = cur;
728*25250505STakashi Iwai 	if (cur) {
72909a9ad69STakashi Iwai 		activate_output_path(codec, &spec->hp_dep_path, false, false);
73009a9ad69STakashi Iwai 		activate_output_path(codec, &spec->hp_path, true, false);
731*25250505STakashi Iwai 		if (spec->hp_indep_shared)
732*25250505STakashi Iwai 			activate_output_path(codec, &spec->out_path[HDA_SIDE],
733*25250505STakashi Iwai 					     false, false);
73409a9ad69STakashi Iwai 	} else {
73509a9ad69STakashi Iwai 		activate_output_path(codec, &spec->hp_path, false, false);
73609a9ad69STakashi Iwai 		activate_output_path(codec, &spec->hp_dep_path, true, false);
737*25250505STakashi Iwai 		if (spec->hp_indep_shared)
738*25250505STakashi Iwai 			activate_output_path(codec, &spec->out_path[HDA_SIDE],
739*25250505STakashi Iwai 					     true, false);
7408df2a312STakashi Iwai 	}
7410aa62aefSHarald Welte 
742ce0e5a9eSLydia Wang 	/* update jack power state */
7433e95b9abSLydia Wang 	set_widgets_power_state(codec);
744*25250505STakashi Iwai 	return 1;
7450aa62aefSHarald Welte }
7460aa62aefSHarald Welte 
747ece8d043STakashi Iwai static const struct snd_kcontrol_new via_hp_mixer = {
7480aa62aefSHarald Welte 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7490aa62aefSHarald Welte 	.name = "Independent HP",
7500aa62aefSHarald Welte 	.info = via_independent_hp_info,
7510aa62aefSHarald Welte 	.get = via_independent_hp_get,
7520aa62aefSHarald Welte 	.put = via_independent_hp_put,
7530aa62aefSHarald Welte };
7540aa62aefSHarald Welte 
7553d83e577STakashi Iwai static int via_hp_build(struct hda_codec *codec)
7565b0cb1d8SJaroslav Kysela {
7573d83e577STakashi Iwai 	struct via_spec *spec = codec->spec;
7585b0cb1d8SJaroslav Kysela 	struct snd_kcontrol_new *knew;
7595b0cb1d8SJaroslav Kysela 	hda_nid_t nid;
7605b0cb1d8SJaroslav Kysela 
7615b0cb1d8SJaroslav Kysela 	nid = spec->autocfg.hp_pins[0];
762ece8d043STakashi Iwai 	knew = via_clone_control(spec, &via_hp_mixer);
7633d83e577STakashi Iwai 	if (knew == NULL)
7643d83e577STakashi Iwai 		return -ENOMEM;
7653d83e577STakashi Iwai 
7665b0cb1d8SJaroslav Kysela 	knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
7675b0cb1d8SJaroslav Kysela 
7685b0cb1d8SJaroslav Kysela 	return 0;
7695b0cb1d8SJaroslav Kysela }
7705b0cb1d8SJaroslav Kysela 
7711564b287SLydia Wang static void notify_aa_path_ctls(struct hda_codec *codec)
7721564b287SLydia Wang {
773e3d7a143STakashi Iwai 	struct via_spec *spec = codec->spec;
7741564b287SLydia Wang 	int i;
7751564b287SLydia Wang 
776e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
777e3d7a143STakashi Iwai 		struct snd_kcontrol *ctl;
778e3d7a143STakashi Iwai 		struct snd_ctl_elem_id id;
7791564b287SLydia Wang 		memset(&id, 0, sizeof(id));
7801564b287SLydia Wang 		id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
781e3d7a143STakashi Iwai 		sprintf(id.name, "%s Playback Volume", spec->smart51_labels[i]);
782525566cbSLydia Wang 		ctl = snd_hda_find_mixer_ctl(codec, id.name);
783525566cbSLydia Wang 		if (ctl)
784525566cbSLydia Wang 			snd_ctl_notify(codec->bus->card,
785525566cbSLydia Wang 					SNDRV_CTL_EVENT_MASK_VALUE,
786525566cbSLydia Wang 					&ctl->id);
7871564b287SLydia Wang 	}
7881564b287SLydia Wang }
7891564b287SLydia Wang 
7901564b287SLydia Wang static void mute_aa_path(struct hda_codec *codec, int mute)
7911564b287SLydia Wang {
7921564b287SLydia Wang 	struct via_spec *spec = codec->spec;
7931564b287SLydia Wang 	int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
794e3d7a143STakashi Iwai 	int i;
795e3d7a143STakashi Iwai 
796e3d7a143STakashi Iwai 	/* check AA path's mute status */
797e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
798e3d7a143STakashi Iwai 		if (spec->smart51_idxs[i] < 0)
799e3d7a143STakashi Iwai 			continue;
800e3d7a143STakashi Iwai 		snd_hda_codec_amp_stereo(codec, spec->aa_mix_nid,
801e3d7a143STakashi Iwai 					 HDA_INPUT, spec->smart51_idxs[i],
8021564b287SLydia Wang 					 HDA_AMP_MUTE, val);
8031564b287SLydia Wang 	}
8041564b287SLydia Wang }
805f4a7828bSTakashi Iwai 
806e3d7a143STakashi Iwai static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
807e3d7a143STakashi Iwai {
808e3d7a143STakashi Iwai 	struct via_spec *spec = codec->spec;
809e3d7a143STakashi Iwai 	int i;
810e3d7a143STakashi Iwai 
811e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++)
812e3d7a143STakashi Iwai 		if (spec->smart51_pins[i] == pin)
813e3d7a143STakashi Iwai 			return true;
814e3d7a143STakashi Iwai 	return false;
815e3d7a143STakashi Iwai }
816e3d7a143STakashi Iwai 
8171564b287SLydia Wang static int via_smart51_get(struct snd_kcontrol *kcontrol,
8181564b287SLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
8191564b287SLydia Wang {
8201564b287SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
8211564b287SLydia Wang 	struct via_spec *spec = codec->spec;
8221564b287SLydia Wang 
823f2b1c9f0STakashi Iwai 	*ucontrol->value.integer.value = spec->smart51_enabled;
8241564b287SLydia Wang 	return 0;
8251564b287SLydia Wang }
8261564b287SLydia Wang 
8271564b287SLydia Wang static int via_smart51_put(struct snd_kcontrol *kcontrol,
8281564b287SLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
8291564b287SLydia Wang {
8301564b287SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
8311564b287SLydia Wang 	struct via_spec *spec = codec->spec;
8321564b287SLydia Wang 	int out_in = *ucontrol->value.integer.value
8331564b287SLydia Wang 		? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
8341564b287SLydia Wang 	int i;
8351564b287SLydia Wang 
836e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
837e3d7a143STakashi Iwai 		hda_nid_t nid = spec->smart51_pins[i];
8387b315bb4STakashi Iwai 		unsigned int parm;
8397b315bb4STakashi Iwai 
8407b315bb4STakashi Iwai 		parm = snd_hda_codec_read(codec, nid, 0,
8411564b287SLydia Wang 					  AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
8421564b287SLydia Wang 		parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
8431564b287SLydia Wang 		parm |= out_in;
8441564b287SLydia Wang 		snd_hda_codec_write(codec, nid, 0,
8451564b287SLydia Wang 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
8461564b287SLydia Wang 				    parm);
8471564b287SLydia Wang 		if (out_in == AC_PINCTL_OUT_EN) {
8481564b287SLydia Wang 			mute_aa_path(codec, 1);
8491564b287SLydia Wang 			notify_aa_path_ctls(codec);
8501564b287SLydia Wang 		}
8511564b287SLydia Wang 	}
8521564b287SLydia Wang 	spec->smart51_enabled = *ucontrol->value.integer.value;
8533e95b9abSLydia Wang 	set_widgets_power_state(codec);
8541564b287SLydia Wang 	return 1;
8551564b287SLydia Wang }
8561564b287SLydia Wang 
8575f4b36d6STakashi Iwai static const struct snd_kcontrol_new via_smart51_mixer = {
8581564b287SLydia Wang 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
8591564b287SLydia Wang 	.name = "Smart 5.1",
8601564b287SLydia Wang 	.count = 1,
861f2b1c9f0STakashi Iwai 	.info = snd_ctl_boolean_mono_info,
8621564b287SLydia Wang 	.get = via_smart51_get,
8631564b287SLydia Wang 	.put = via_smart51_put,
8641564b287SLydia Wang };
8651564b287SLydia Wang 
866f4a7828bSTakashi Iwai static int via_smart51_build(struct hda_codec *codec)
8675b0cb1d8SJaroslav Kysela {
868f4a7828bSTakashi Iwai 	struct via_spec *spec = codec->spec;
8695b0cb1d8SJaroslav Kysela 
870e3d7a143STakashi Iwai 	if (!spec->smart51_nums)
871cb34c207SLydia Wang 		return 0;
872e3d7a143STakashi Iwai 	if (!via_clone_control(spec, &via_smart51_mixer))
8735b0cb1d8SJaroslav Kysela 		return -ENOMEM;
8745b0cb1d8SJaroslav Kysela 	return 0;
8755b0cb1d8SJaroslav Kysela }
8765b0cb1d8SJaroslav Kysela 
877f5271101SLydia Wang /* check AA path's mute status */
878ada509ecSTakashi Iwai static bool is_aa_path_mute(struct hda_codec *codec)
879ada509ecSTakashi Iwai {
880ada509ecSTakashi Iwai 	struct via_spec *spec = codec->spec;
881ada509ecSTakashi Iwai 	const struct hda_amp_list *p;
882ada509ecSTakashi Iwai 	int i, ch, v;
883ada509ecSTakashi Iwai 
884ada509ecSTakashi Iwai 	for (i = 0; i < spec->num_loopbacks; i++) {
885ada509ecSTakashi Iwai 		p = &spec->loopback_list[i];
886ada509ecSTakashi Iwai 		for (ch = 0; ch < 2; ch++) {
887ada509ecSTakashi Iwai 			v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
888ada509ecSTakashi Iwai 						   p->idx);
889ada509ecSTakashi Iwai 			if (!(v & HDA_AMP_MUTE) && v > 0)
890ada509ecSTakashi Iwai 				return false;
891f5271101SLydia Wang 		}
892f5271101SLydia Wang 	}
893ada509ecSTakashi Iwai 	return true;
894f5271101SLydia Wang }
895f5271101SLydia Wang 
896f5271101SLydia Wang /* enter/exit analog low-current mode */
897ada509ecSTakashi Iwai static void analog_low_current_mode(struct hda_codec *codec)
898f5271101SLydia Wang {
899f5271101SLydia Wang 	struct via_spec *spec = codec->spec;
900ada509ecSTakashi Iwai 	bool enable;
901ada509ecSTakashi Iwai 	unsigned int verb, parm;
902f5271101SLydia Wang 
903ada509ecSTakashi Iwai 	enable = is_aa_path_mute(codec) && (spec->num_active_streams > 0);
904f5271101SLydia Wang 
905f5271101SLydia Wang 	/* decide low current mode's verb & parameter */
906f5271101SLydia Wang 	switch (spec->codec_type) {
907f5271101SLydia Wang 	case VT1708B_8CH:
908f5271101SLydia Wang 	case VT1708B_4CH:
909f5271101SLydia Wang 		verb = 0xf70;
910f5271101SLydia Wang 		parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
911f5271101SLydia Wang 		break;
912f5271101SLydia Wang 	case VT1708S:
913eb7188caSLydia Wang 	case VT1718S:
914f3db423dSLydia Wang 	case VT1716S:
915f5271101SLydia Wang 		verb = 0xf73;
916f5271101SLydia Wang 		parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
917f5271101SLydia Wang 		break;
918f5271101SLydia Wang 	case VT1702:
919f5271101SLydia Wang 		verb = 0xf73;
920f5271101SLydia Wang 		parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
921f5271101SLydia Wang 		break;
92225eaba2fSLydia Wang 	case VT2002P:
923ab6734e7SLydia Wang 	case VT1812:
92411890956SLydia Wang 	case VT1802:
92525eaba2fSLydia Wang 		verb = 0xf93;
92625eaba2fSLydia Wang 		parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
92725eaba2fSLydia Wang 		break;
928f5271101SLydia Wang 	default:
929f5271101SLydia Wang 		return;		/* other codecs are not supported */
930f5271101SLydia Wang 	}
931f5271101SLydia Wang 	/* send verb */
932f5271101SLydia Wang 	snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
933f5271101SLydia Wang }
934f5271101SLydia Wang 
935c577b8a1SJoseph Chan /*
936c577b8a1SJoseph Chan  * generic initialization of ADC, input mixers and output mixers
937c577b8a1SJoseph Chan  */
938096a8854STakashi Iwai static const struct hda_verb vt1708_init_verbs[] = {
939aa266fccSLydia Wang 	/* power down jack detect function */
940aa266fccSLydia Wang 	{0x1, 0xf81, 0x1},
941f7278fd0SJosepch Chan 	{ }
942c577b8a1SJoseph Chan };
943c577b8a1SJoseph Chan 
944ada509ecSTakashi Iwai static void set_stream_active(struct hda_codec *codec, bool active)
9457eb56e84STakashi Iwai {
946ada509ecSTakashi Iwai 	struct via_spec *spec = codec->spec;
947ada509ecSTakashi Iwai 
948ada509ecSTakashi Iwai 	if (active)
949ada509ecSTakashi Iwai 		spec->num_active_streams++;
950ada509ecSTakashi Iwai 	else
951ada509ecSTakashi Iwai 		spec->num_active_streams--;
952ada509ecSTakashi Iwai 	analog_low_current_mode(codec);
9537eb56e84STakashi Iwai }
9547eb56e84STakashi Iwai 
955ece8d043STakashi Iwai static int via_playback_multi_pcm_open(struct hda_pcm_stream *hinfo,
956c577b8a1SJoseph Chan 				 struct hda_codec *codec,
957c577b8a1SJoseph Chan 				 struct snd_pcm_substream *substream)
958c577b8a1SJoseph Chan {
959c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
960*25250505STakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
961ada509ecSTakashi Iwai 	int err;
962ece8d043STakashi Iwai 
963*25250505STakashi Iwai 	spec->multiout.hp_nid = 0;
964*25250505STakashi Iwai 	spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
965*25250505STakashi Iwai 	if (!spec->hp_independent_mode) {
966*25250505STakashi Iwai 		if (!spec->hp_indep_shared)
967ece8d043STakashi Iwai 			spec->multiout.hp_nid = spec->hp_dac_nid;
968*25250505STakashi Iwai 	} else {
969*25250505STakashi Iwai 		if (spec->hp_indep_shared)
970*25250505STakashi Iwai 			spec->multiout.num_dacs = cfg->line_outs - 1;
971*25250505STakashi Iwai 	}
972*25250505STakashi Iwai 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
973ada509ecSTakashi Iwai 	set_stream_active(codec, true);
974ada509ecSTakashi Iwai 	err = snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
9759a08160bSTakashi Iwai 					    hinfo);
976ada509ecSTakashi Iwai 	if (err < 0) {
977ada509ecSTakashi Iwai 		spec->multiout.hp_nid = 0;
978ada509ecSTakashi Iwai 		set_stream_active(codec, false);
979ada509ecSTakashi Iwai 		return err;
980ada509ecSTakashi Iwai 	}
981ada509ecSTakashi Iwai 	return 0;
982c577b8a1SJoseph Chan }
983c577b8a1SJoseph Chan 
984ece8d043STakashi Iwai static int via_playback_multi_pcm_close(struct hda_pcm_stream *hinfo,
9859af74210STakashi Iwai 				  struct hda_codec *codec,
9869af74210STakashi Iwai 				  struct snd_pcm_substream *substream)
9879af74210STakashi Iwai {
988ece8d043STakashi Iwai 	struct via_spec *spec = codec->spec;
989ece8d043STakashi Iwai 
990ece8d043STakashi Iwai 	spec->multiout.hp_nid = 0;
991ada509ecSTakashi Iwai 	set_stream_active(codec, false);
9929af74210STakashi Iwai 	return 0;
9939af74210STakashi Iwai }
9949af74210STakashi Iwai 
9957eb56e84STakashi Iwai static int via_playback_hp_pcm_open(struct hda_pcm_stream *hinfo,
9967eb56e84STakashi Iwai 				    struct hda_codec *codec,
9977eb56e84STakashi Iwai 				    struct snd_pcm_substream *substream)
9987eb56e84STakashi Iwai {
9997eb56e84STakashi Iwai 	struct via_spec *spec = codec->spec;
10007eb56e84STakashi Iwai 
1001ece8d043STakashi Iwai 	if (snd_BUG_ON(!spec->hp_dac_nid))
10027eb56e84STakashi Iwai 		return -EINVAL;
1003ece8d043STakashi Iwai 	if (!spec->hp_independent_mode || spec->multiout.hp_nid)
1004ece8d043STakashi Iwai 		return -EBUSY;
1005ada509ecSTakashi Iwai 	set_stream_active(codec, true);
1006ece8d043STakashi Iwai 	return 0;
1007ece8d043STakashi Iwai }
1008ece8d043STakashi Iwai 
1009ece8d043STakashi Iwai static int via_playback_hp_pcm_close(struct hda_pcm_stream *hinfo,
1010ece8d043STakashi Iwai 				     struct hda_codec *codec,
1011ece8d043STakashi Iwai 				     struct snd_pcm_substream *substream)
1012ece8d043STakashi Iwai {
1013ada509ecSTakashi Iwai 	set_stream_active(codec, false);
10147eb56e84STakashi Iwai 	return 0;
10157eb56e84STakashi Iwai }
10167eb56e84STakashi Iwai 
10177eb56e84STakashi Iwai static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
10187eb56e84STakashi Iwai 					  struct hda_codec *codec,
10190aa62aefSHarald Welte 					  unsigned int stream_tag,
10200aa62aefSHarald Welte 					  unsigned int format,
10210aa62aefSHarald Welte 					  struct snd_pcm_substream *substream)
10220aa62aefSHarald Welte {
10230aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
10240aa62aefSHarald Welte 
1025ece8d043STakashi Iwai 	snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
1026ece8d043STakashi Iwai 					 format, substream);
10277eb56e84STakashi Iwai 	vt1708_start_hp_work(spec);
10287eb56e84STakashi Iwai 	return 0;
10290aa62aefSHarald Welte }
10300aa62aefSHarald Welte 
10317eb56e84STakashi Iwai static int via_playback_hp_pcm_prepare(struct hda_pcm_stream *hinfo,
10320aa62aefSHarald Welte 				       struct hda_codec *codec,
10330aa62aefSHarald Welte 				       unsigned int stream_tag,
10340aa62aefSHarald Welte 				       unsigned int format,
10350aa62aefSHarald Welte 				       struct snd_pcm_substream *substream)
10360aa62aefSHarald Welte {
10370aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
10380aa62aefSHarald Welte 
1039ece8d043STakashi Iwai 	snd_hda_codec_setup_stream(codec, spec->hp_dac_nid,
1040ece8d043STakashi Iwai 				   stream_tag, 0, format);
10411f2e99feSLydia Wang 	vt1708_start_hp_work(spec);
10420aa62aefSHarald Welte 	return 0;
10430aa62aefSHarald Welte }
10440aa62aefSHarald Welte 
10450aa62aefSHarald Welte static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
10460aa62aefSHarald Welte 				    struct hda_codec *codec,
10470aa62aefSHarald Welte 				    struct snd_pcm_substream *substream)
10480aa62aefSHarald Welte {
10490aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
10500aa62aefSHarald Welte 
1051ece8d043STakashi Iwai 	snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
10527eb56e84STakashi Iwai 	vt1708_stop_hp_work(spec);
10537eb56e84STakashi Iwai 	return 0;
10540aa62aefSHarald Welte }
10557eb56e84STakashi Iwai 
10567eb56e84STakashi Iwai static int via_playback_hp_pcm_cleanup(struct hda_pcm_stream *hinfo,
10577eb56e84STakashi Iwai 				       struct hda_codec *codec,
10587eb56e84STakashi Iwai 				       struct snd_pcm_substream *substream)
10597eb56e84STakashi Iwai {
10607eb56e84STakashi Iwai 	struct via_spec *spec = codec->spec;
10617eb56e84STakashi Iwai 
1062ece8d043STakashi Iwai 	snd_hda_codec_setup_stream(codec, spec->hp_dac_nid, 0, 0, 0);
10631f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
10640aa62aefSHarald Welte 	return 0;
10650aa62aefSHarald Welte }
10660aa62aefSHarald Welte 
1067c577b8a1SJoseph Chan /*
1068c577b8a1SJoseph Chan  * Digital out
1069c577b8a1SJoseph Chan  */
1070c577b8a1SJoseph Chan static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1071c577b8a1SJoseph Chan 				     struct hda_codec *codec,
1072c577b8a1SJoseph Chan 				     struct snd_pcm_substream *substream)
1073c577b8a1SJoseph Chan {
1074c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1075c577b8a1SJoseph Chan 	return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1076c577b8a1SJoseph Chan }
1077c577b8a1SJoseph Chan 
1078c577b8a1SJoseph Chan static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1079c577b8a1SJoseph Chan 				      struct hda_codec *codec,
1080c577b8a1SJoseph Chan 				      struct snd_pcm_substream *substream)
1081c577b8a1SJoseph Chan {
1082c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1083c577b8a1SJoseph Chan 	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1084c577b8a1SJoseph Chan }
1085c577b8a1SJoseph Chan 
10865691ec7fSHarald Welte static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
108798aa34c0SHarald Welte 					struct hda_codec *codec,
108898aa34c0SHarald Welte 					unsigned int stream_tag,
108998aa34c0SHarald Welte 					unsigned int format,
109098aa34c0SHarald Welte 					struct snd_pcm_substream *substream)
109198aa34c0SHarald Welte {
109298aa34c0SHarald Welte 	struct via_spec *spec = codec->spec;
10939da29271STakashi Iwai 	return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
10949da29271STakashi Iwai 					     stream_tag, format, substream);
10959da29271STakashi Iwai }
10965691ec7fSHarald Welte 
10979da29271STakashi Iwai static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
10989da29271STakashi Iwai 					struct hda_codec *codec,
10999da29271STakashi Iwai 					struct snd_pcm_substream *substream)
11009da29271STakashi Iwai {
11019da29271STakashi Iwai 	struct via_spec *spec = codec->spec;
11029da29271STakashi Iwai 	snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
110398aa34c0SHarald Welte 	return 0;
110498aa34c0SHarald Welte }
110598aa34c0SHarald Welte 
1106c577b8a1SJoseph Chan /*
1107c577b8a1SJoseph Chan  * Analog capture
1108c577b8a1SJoseph Chan  */
1109c577b8a1SJoseph Chan static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1110c577b8a1SJoseph Chan 				   struct hda_codec *codec,
1111c577b8a1SJoseph Chan 				   unsigned int stream_tag,
1112c577b8a1SJoseph Chan 				   unsigned int format,
1113c577b8a1SJoseph Chan 				   struct snd_pcm_substream *substream)
1114c577b8a1SJoseph Chan {
1115c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1116c577b8a1SJoseph Chan 
1117c577b8a1SJoseph Chan 	snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1118c577b8a1SJoseph Chan 				   stream_tag, 0, format);
1119c577b8a1SJoseph Chan 	return 0;
1120c577b8a1SJoseph Chan }
1121c577b8a1SJoseph Chan 
1122c577b8a1SJoseph Chan static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1123c577b8a1SJoseph Chan 				   struct hda_codec *codec,
1124c577b8a1SJoseph Chan 				   struct snd_pcm_substream *substream)
1125c577b8a1SJoseph Chan {
1126c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1127888afa15STakashi Iwai 	snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
1128c577b8a1SJoseph Chan 	return 0;
1129c577b8a1SJoseph Chan }
1130c577b8a1SJoseph Chan 
1131a86a88eaSTakashi Iwai /* analog capture with dynamic ADC switching */
1132a86a88eaSTakashi Iwai static int via_dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1133a86a88eaSTakashi Iwai 					   struct hda_codec *codec,
1134a86a88eaSTakashi Iwai 					   unsigned int stream_tag,
1135a86a88eaSTakashi Iwai 					   unsigned int format,
1136a86a88eaSTakashi Iwai 					   struct snd_pcm_substream *substream)
1137a86a88eaSTakashi Iwai {
1138a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1139a86a88eaSTakashi Iwai 	int adc_idx = spec->inputs[spec->cur_mux[0]].adc_idx;
1140a86a88eaSTakashi Iwai 
1141a86a88eaSTakashi Iwai 	spec->cur_adc = spec->adc_nids[adc_idx];
1142a86a88eaSTakashi Iwai 	spec->cur_adc_stream_tag = stream_tag;
1143a86a88eaSTakashi Iwai 	spec->cur_adc_format = format;
1144a86a88eaSTakashi Iwai 	snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
1145a86a88eaSTakashi Iwai 	return 0;
1146a86a88eaSTakashi Iwai }
1147a86a88eaSTakashi Iwai 
1148a86a88eaSTakashi Iwai static int via_dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1149a86a88eaSTakashi Iwai 					   struct hda_codec *codec,
1150a86a88eaSTakashi Iwai 					   struct snd_pcm_substream *substream)
1151a86a88eaSTakashi Iwai {
1152a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1153a86a88eaSTakashi Iwai 
1154a86a88eaSTakashi Iwai 	snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1155a86a88eaSTakashi Iwai 	spec->cur_adc = 0;
1156a86a88eaSTakashi Iwai 	return 0;
1157a86a88eaSTakashi Iwai }
1158a86a88eaSTakashi Iwai 
1159a86a88eaSTakashi Iwai /* re-setup the stream if running; called from input-src put */
1160a86a88eaSTakashi Iwai static bool via_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
1161a86a88eaSTakashi Iwai {
1162a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1163a86a88eaSTakashi Iwai 	int adc_idx = spec->inputs[cur].adc_idx;
1164a86a88eaSTakashi Iwai 	hda_nid_t adc = spec->adc_nids[adc_idx];
1165a86a88eaSTakashi Iwai 
1166a86a88eaSTakashi Iwai 	if (spec->cur_adc && spec->cur_adc != adc) {
1167a86a88eaSTakashi Iwai 		/* stream is running, let's swap the current ADC */
1168a86a88eaSTakashi Iwai 		__snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1169a86a88eaSTakashi Iwai 		spec->cur_adc = adc;
1170a86a88eaSTakashi Iwai 		snd_hda_codec_setup_stream(codec, adc,
1171a86a88eaSTakashi Iwai 					   spec->cur_adc_stream_tag, 0,
1172a86a88eaSTakashi Iwai 					   spec->cur_adc_format);
1173a86a88eaSTakashi Iwai 		return true;
1174a86a88eaSTakashi Iwai 	}
1175a86a88eaSTakashi Iwai 	return false;
1176a86a88eaSTakashi Iwai }
1177a86a88eaSTakashi Iwai 
11789af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_analog_playback = {
11797eb56e84STakashi Iwai 	.substreams = 1,
1180c577b8a1SJoseph Chan 	.channels_min = 2,
1181c577b8a1SJoseph Chan 	.channels_max = 8,
11829af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1183c577b8a1SJoseph Chan 	.ops = {
1184ece8d043STakashi Iwai 		.open = via_playback_multi_pcm_open,
1185ece8d043STakashi Iwai 		.close = via_playback_multi_pcm_close,
11860aa62aefSHarald Welte 		.prepare = via_playback_multi_pcm_prepare,
11870aa62aefSHarald Welte 		.cleanup = via_playback_multi_pcm_cleanup
1188c577b8a1SJoseph Chan 	},
1189c577b8a1SJoseph Chan };
1190c577b8a1SJoseph Chan 
11917eb56e84STakashi Iwai static const struct hda_pcm_stream via_pcm_hp_playback = {
11927eb56e84STakashi Iwai 	.substreams = 1,
11937eb56e84STakashi Iwai 	.channels_min = 2,
11947eb56e84STakashi Iwai 	.channels_max = 2,
11957eb56e84STakashi Iwai 	/* NID is set in via_build_pcms */
11967eb56e84STakashi Iwai 	.ops = {
11977eb56e84STakashi Iwai 		.open = via_playback_hp_pcm_open,
1198ece8d043STakashi Iwai 		.close = via_playback_hp_pcm_close,
11997eb56e84STakashi Iwai 		.prepare = via_playback_hp_pcm_prepare,
12007eb56e84STakashi Iwai 		.cleanup = via_playback_hp_pcm_cleanup
12017eb56e84STakashi Iwai 	},
12027eb56e84STakashi Iwai };
12037eb56e84STakashi Iwai 
120490dd48a1STakashi Iwai static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
12057eb56e84STakashi Iwai 	.substreams = 1,
1206bc9b5623STakashi Iwai 	.channels_min = 2,
1207bc9b5623STakashi Iwai 	.channels_max = 8,
12089af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1209bc9b5623STakashi Iwai 	/* We got noisy outputs on the right channel on VT1708 when
1210bc9b5623STakashi Iwai 	 * 24bit samples are used.  Until any workaround is found,
1211bc9b5623STakashi Iwai 	 * disable the 24bit format, so far.
1212bc9b5623STakashi Iwai 	 */
1213bc9b5623STakashi Iwai 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
1214bc9b5623STakashi Iwai 	.ops = {
1215ece8d043STakashi Iwai 		.open = via_playback_multi_pcm_open,
1216ece8d043STakashi Iwai 		.close = via_playback_multi_pcm_close,
1217c873cc25SLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
1218c873cc25SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup
1219bc9b5623STakashi Iwai 	},
1220bc9b5623STakashi Iwai };
1221bc9b5623STakashi Iwai 
12229af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_analog_capture = {
12237eb56e84STakashi Iwai 	.substreams = 1, /* will be changed in via_build_pcms() */
1224c577b8a1SJoseph Chan 	.channels_min = 2,
1225c577b8a1SJoseph Chan 	.channels_max = 2,
12269af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1227c577b8a1SJoseph Chan 	.ops = {
1228c577b8a1SJoseph Chan 		.prepare = via_capture_pcm_prepare,
1229c577b8a1SJoseph Chan 		.cleanup = via_capture_pcm_cleanup
1230c577b8a1SJoseph Chan 	},
1231c577b8a1SJoseph Chan };
1232c577b8a1SJoseph Chan 
1233a86a88eaSTakashi Iwai static const struct hda_pcm_stream via_pcm_dyn_adc_analog_capture = {
1234a86a88eaSTakashi Iwai 	.substreams = 1,
1235a86a88eaSTakashi Iwai 	.channels_min = 2,
1236a86a88eaSTakashi Iwai 	.channels_max = 2,
1237a86a88eaSTakashi Iwai 	/* NID is set in via_build_pcms */
1238a86a88eaSTakashi Iwai 	.ops = {
1239a86a88eaSTakashi Iwai 		.prepare = via_dyn_adc_capture_pcm_prepare,
1240a86a88eaSTakashi Iwai 		.cleanup = via_dyn_adc_capture_pcm_cleanup,
1241a86a88eaSTakashi Iwai 	},
1242a86a88eaSTakashi Iwai };
1243a86a88eaSTakashi Iwai 
12449af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_digital_playback = {
1245c577b8a1SJoseph Chan 	.substreams = 1,
1246c577b8a1SJoseph Chan 	.channels_min = 2,
1247c577b8a1SJoseph Chan 	.channels_max = 2,
1248c577b8a1SJoseph Chan 	/* NID is set in via_build_pcms */
1249c577b8a1SJoseph Chan 	.ops = {
1250c577b8a1SJoseph Chan 		.open = via_dig_playback_pcm_open,
12516b97eb45STakashi Iwai 		.close = via_dig_playback_pcm_close,
12529da29271STakashi Iwai 		.prepare = via_dig_playback_pcm_prepare,
12539da29271STakashi Iwai 		.cleanup = via_dig_playback_pcm_cleanup
1254c577b8a1SJoseph Chan 	},
1255c577b8a1SJoseph Chan };
1256c577b8a1SJoseph Chan 
12579af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_digital_capture = {
1258c577b8a1SJoseph Chan 	.substreams = 1,
1259c577b8a1SJoseph Chan 	.channels_min = 2,
1260c577b8a1SJoseph Chan 	.channels_max = 2,
1261c577b8a1SJoseph Chan };
1262c577b8a1SJoseph Chan 
1263370bafbdSTakashi Iwai /*
1264370bafbdSTakashi Iwai  * slave controls for virtual master
1265370bafbdSTakashi Iwai  */
1266370bafbdSTakashi Iwai static const char * const via_slave_vols[] = {
1267370bafbdSTakashi Iwai 	"Front Playback Volume",
1268370bafbdSTakashi Iwai 	"Surround Playback Volume",
1269370bafbdSTakashi Iwai 	"Center Playback Volume",
1270370bafbdSTakashi Iwai 	"LFE Playback Volume",
1271370bafbdSTakashi Iwai 	"Side Playback Volume",
1272370bafbdSTakashi Iwai 	"Headphone Playback Volume",
1273370bafbdSTakashi Iwai 	"Speaker Playback Volume",
1274370bafbdSTakashi Iwai 	NULL,
1275370bafbdSTakashi Iwai };
1276370bafbdSTakashi Iwai 
1277370bafbdSTakashi Iwai static const char * const via_slave_sws[] = {
1278370bafbdSTakashi Iwai 	"Front Playback Switch",
1279370bafbdSTakashi Iwai 	"Surround Playback Switch",
1280370bafbdSTakashi Iwai 	"Center Playback Switch",
1281370bafbdSTakashi Iwai 	"LFE Playback Switch",
1282370bafbdSTakashi Iwai 	"Side Playback Switch",
1283370bafbdSTakashi Iwai 	"Headphone Playback Switch",
1284370bafbdSTakashi Iwai 	"Speaker Playback Switch",
1285370bafbdSTakashi Iwai 	NULL,
1286370bafbdSTakashi Iwai };
1287370bafbdSTakashi Iwai 
1288c577b8a1SJoseph Chan static int via_build_controls(struct hda_codec *codec)
1289c577b8a1SJoseph Chan {
1290c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
12915b0cb1d8SJaroslav Kysela 	struct snd_kcontrol *kctl;
12925b0cb1d8SJaroslav Kysela 	int err, i;
1293c577b8a1SJoseph Chan 
129424088a58STakashi Iwai 	if (spec->set_widgets_power_state)
129524088a58STakashi Iwai 		if (!via_clone_control(spec, &via_pin_power_ctl_enum))
129624088a58STakashi Iwai 			return -ENOMEM;
129724088a58STakashi Iwai 
1298c577b8a1SJoseph Chan 	for (i = 0; i < spec->num_mixers; i++) {
1299c577b8a1SJoseph Chan 		err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1300c577b8a1SJoseph Chan 		if (err < 0)
1301c577b8a1SJoseph Chan 			return err;
1302c577b8a1SJoseph Chan 	}
1303c577b8a1SJoseph Chan 
1304c577b8a1SJoseph Chan 	if (spec->multiout.dig_out_nid) {
1305c577b8a1SJoseph Chan 		err = snd_hda_create_spdif_out_ctls(codec,
130674b654c9SStephen Warren 						    spec->multiout.dig_out_nid,
1307c577b8a1SJoseph Chan 						    spec->multiout.dig_out_nid);
1308c577b8a1SJoseph Chan 		if (err < 0)
1309c577b8a1SJoseph Chan 			return err;
13109a08160bSTakashi Iwai 		err = snd_hda_create_spdif_share_sw(codec,
13119a08160bSTakashi Iwai 						    &spec->multiout);
13129a08160bSTakashi Iwai 		if (err < 0)
13139a08160bSTakashi Iwai 			return err;
13149a08160bSTakashi Iwai 		spec->multiout.share_spdif = 1;
1315c577b8a1SJoseph Chan 	}
1316c577b8a1SJoseph Chan 	if (spec->dig_in_nid) {
1317c577b8a1SJoseph Chan 		err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1318c577b8a1SJoseph Chan 		if (err < 0)
1319c577b8a1SJoseph Chan 			return err;
1320c577b8a1SJoseph Chan 	}
132117314379SLydia Wang 
1322370bafbdSTakashi Iwai 	/* if we have no master control, let's create it */
1323370bafbdSTakashi Iwai 	if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1324370bafbdSTakashi Iwai 		unsigned int vmaster_tlv[4];
1325370bafbdSTakashi Iwai 		snd_hda_set_vmaster_tlv(codec, spec->multiout.dac_nids[0],
1326370bafbdSTakashi Iwai 					HDA_OUTPUT, vmaster_tlv);
1327370bafbdSTakashi Iwai 		err = snd_hda_add_vmaster(codec, "Master Playback Volume",
1328370bafbdSTakashi Iwai 					  vmaster_tlv, via_slave_vols);
1329370bafbdSTakashi Iwai 		if (err < 0)
1330370bafbdSTakashi Iwai 			return err;
1331370bafbdSTakashi Iwai 	}
1332370bafbdSTakashi Iwai 	if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1333370bafbdSTakashi Iwai 		err = snd_hda_add_vmaster(codec, "Master Playback Switch",
1334370bafbdSTakashi Iwai 					  NULL, via_slave_sws);
1335370bafbdSTakashi Iwai 		if (err < 0)
1336370bafbdSTakashi Iwai 			return err;
1337370bafbdSTakashi Iwai 	}
1338370bafbdSTakashi Iwai 
13395b0cb1d8SJaroslav Kysela 	/* assign Capture Source enums to NID */
13405b0cb1d8SJaroslav Kysela 	kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
13415b0cb1d8SJaroslav Kysela 	for (i = 0; kctl && i < kctl->count; i++) {
134221949f00STakashi Iwai 		err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
13435b0cb1d8SJaroslav Kysela 		if (err < 0)
13445b0cb1d8SJaroslav Kysela 			return err;
13455b0cb1d8SJaroslav Kysela 	}
13465b0cb1d8SJaroslav Kysela 
134717314379SLydia Wang 	/* init power states */
13483e95b9abSLydia Wang 	set_widgets_power_state(codec);
1349ada509ecSTakashi Iwai 	analog_low_current_mode(codec);
135017314379SLydia Wang 
1351603c4019STakashi Iwai 	via_free_kctls(codec); /* no longer needed */
1352c577b8a1SJoseph Chan 	return 0;
1353c577b8a1SJoseph Chan }
1354c577b8a1SJoseph Chan 
1355c577b8a1SJoseph Chan static int via_build_pcms(struct hda_codec *codec)
1356c577b8a1SJoseph Chan {
1357c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1358c577b8a1SJoseph Chan 	struct hda_pcm *info = spec->pcm_rec;
1359c577b8a1SJoseph Chan 
1360c577b8a1SJoseph Chan 	codec->num_pcms = 1;
1361c577b8a1SJoseph Chan 	codec->pcm_info = info;
1362c577b8a1SJoseph Chan 
136382673bc8STakashi Iwai 	snprintf(spec->stream_name_analog, sizeof(spec->stream_name_analog),
136482673bc8STakashi Iwai 		 "%s Analog", codec->chip_name);
1365c577b8a1SJoseph Chan 	info->name = spec->stream_name_analog;
13669af74210STakashi Iwai 
13679af74210STakashi Iwai 	if (!spec->stream_analog_playback)
13689af74210STakashi Iwai 		spec->stream_analog_playback = &via_pcm_analog_playback;
1369377ff31aSLydia Wang 	info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
13709af74210STakashi Iwai 		*spec->stream_analog_playback;
1371377ff31aSLydia Wang 	info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1372377ff31aSLydia Wang 		spec->multiout.dac_nids[0];
1373c577b8a1SJoseph Chan 	info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1374c577b8a1SJoseph Chan 		spec->multiout.max_channels;
13759af74210STakashi Iwai 
1376a86a88eaSTakashi Iwai 	if (!spec->stream_analog_capture) {
1377a86a88eaSTakashi Iwai 		if (spec->dyn_adc_switch)
1378a86a88eaSTakashi Iwai 			spec->stream_analog_capture =
1379a86a88eaSTakashi Iwai 				&via_pcm_dyn_adc_analog_capture;
1380a86a88eaSTakashi Iwai 		else
13819af74210STakashi Iwai 			spec->stream_analog_capture = &via_pcm_analog_capture;
1382a86a88eaSTakashi Iwai 	}
13839af74210STakashi Iwai 	info->stream[SNDRV_PCM_STREAM_CAPTURE] =
13849af74210STakashi Iwai 		*spec->stream_analog_capture;
13859af74210STakashi Iwai 	info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
1386a86a88eaSTakashi Iwai 	if (!spec->dyn_adc_switch)
13879af74210STakashi Iwai 		info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
13889af74210STakashi Iwai 			spec->num_adc_nids;
1389c577b8a1SJoseph Chan 
1390c577b8a1SJoseph Chan 	if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1391c577b8a1SJoseph Chan 		codec->num_pcms++;
1392c577b8a1SJoseph Chan 		info++;
139382673bc8STakashi Iwai 		snprintf(spec->stream_name_digital,
139482673bc8STakashi Iwai 			 sizeof(spec->stream_name_digital),
139582673bc8STakashi Iwai 			 "%s Digital", codec->chip_name);
1396c577b8a1SJoseph Chan 		info->name = spec->stream_name_digital;
13977ba72ba1STakashi Iwai 		info->pcm_type = HDA_PCM_TYPE_SPDIF;
1398c577b8a1SJoseph Chan 		if (spec->multiout.dig_out_nid) {
13999af74210STakashi Iwai 			if (!spec->stream_digital_playback)
14009af74210STakashi Iwai 				spec->stream_digital_playback =
14019af74210STakashi Iwai 					&via_pcm_digital_playback;
1402c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
14039af74210STakashi Iwai 				*spec->stream_digital_playback;
1404c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1405c577b8a1SJoseph Chan 				spec->multiout.dig_out_nid;
1406c577b8a1SJoseph Chan 		}
1407c577b8a1SJoseph Chan 		if (spec->dig_in_nid) {
14089af74210STakashi Iwai 			if (!spec->stream_digital_capture)
14099af74210STakashi Iwai 				spec->stream_digital_capture =
14109af74210STakashi Iwai 					&via_pcm_digital_capture;
1411c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_CAPTURE] =
14129af74210STakashi Iwai 				*spec->stream_digital_capture;
1413c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1414c577b8a1SJoseph Chan 				spec->dig_in_nid;
1415c577b8a1SJoseph Chan 		}
1416c577b8a1SJoseph Chan 	}
1417c577b8a1SJoseph Chan 
1418ece8d043STakashi Iwai 	if (spec->hp_dac_nid) {
14197eb56e84STakashi Iwai 		codec->num_pcms++;
14207eb56e84STakashi Iwai 		info++;
14217eb56e84STakashi Iwai 		snprintf(spec->stream_name_hp, sizeof(spec->stream_name_hp),
14227eb56e84STakashi Iwai 			 "%s HP", codec->chip_name);
14237eb56e84STakashi Iwai 		info->name = spec->stream_name_hp;
14247eb56e84STakashi Iwai 		info->stream[SNDRV_PCM_STREAM_PLAYBACK] = via_pcm_hp_playback;
14257eb56e84STakashi Iwai 		info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1426ece8d043STakashi Iwai 			spec->hp_dac_nid;
14277eb56e84STakashi Iwai 	}
1428c577b8a1SJoseph Chan 	return 0;
1429c577b8a1SJoseph Chan }
1430c577b8a1SJoseph Chan 
1431c577b8a1SJoseph Chan static void via_free(struct hda_codec *codec)
1432c577b8a1SJoseph Chan {
1433c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1434c577b8a1SJoseph Chan 
1435c577b8a1SJoseph Chan 	if (!spec)
1436c577b8a1SJoseph Chan 		return;
1437c577b8a1SJoseph Chan 
1438603c4019STakashi Iwai 	via_free_kctls(codec);
14391f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
1440a86a88eaSTakashi Iwai 	kfree(spec->bind_cap_vol);
1441a86a88eaSTakashi Iwai 	kfree(spec->bind_cap_sw);
1442a86a88eaSTakashi Iwai 	kfree(spec);
1443c577b8a1SJoseph Chan }
1444c577b8a1SJoseph Chan 
144564be285bSTakashi Iwai /* mute/unmute outputs */
144664be285bSTakashi Iwai static void toggle_output_mutes(struct hda_codec *codec, int num_pins,
144764be285bSTakashi Iwai 				hda_nid_t *pins, bool mute)
144864be285bSTakashi Iwai {
144964be285bSTakashi Iwai 	int i;
145064be285bSTakashi Iwai 	for (i = 0; i < num_pins; i++)
145164be285bSTakashi Iwai 		snd_hda_codec_write(codec, pins[i], 0,
145264be285bSTakashi Iwai 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
145364be285bSTakashi Iwai 				    mute ? 0 : PIN_OUT);
145464be285bSTakashi Iwai }
145564be285bSTakashi Iwai 
14564a918ffeSTakashi Iwai /* mute internal speaker if line-out is plugged */
14574a918ffeSTakashi Iwai static void via_line_automute(struct hda_codec *codec, int present)
14584a918ffeSTakashi Iwai {
14594a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
14604a918ffeSTakashi Iwai 
14614a918ffeSTakashi Iwai 	if (!spec->autocfg.speaker_outs)
14624a918ffeSTakashi Iwai 		return;
14634a918ffeSTakashi Iwai 	if (!present)
14644a918ffeSTakashi Iwai 		present = snd_hda_jack_detect(codec,
14654a918ffeSTakashi Iwai 					      spec->autocfg.line_out_pins[0]);
14664a918ffeSTakashi Iwai 	toggle_output_mutes(codec, spec->autocfg.speaker_outs,
14674a918ffeSTakashi Iwai 			    spec->autocfg.speaker_pins,
14684a918ffeSTakashi Iwai 			    present);
14694a918ffeSTakashi Iwai }
14704a918ffeSTakashi Iwai 
147169e52a80SHarald Welte /* mute internal speaker if HP is plugged */
147269e52a80SHarald Welte static void via_hp_automute(struct hda_codec *codec)
147369e52a80SHarald Welte {
14744a918ffeSTakashi Iwai 	int present = 0;
147569e52a80SHarald Welte 	struct via_spec *spec = codec->spec;
147669e52a80SHarald Welte 
14774a918ffeSTakashi Iwai 	if (!spec->hp_independent_mode && spec->autocfg.hp_pins[0]) {
1478f2b1c9f0STakashi Iwai 		int nums;
1479d56757abSTakashi Iwai 		present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
1480f2b1c9f0STakashi Iwai 		if (spec->smart51_enabled)
1481f2b1c9f0STakashi Iwai 			nums = spec->autocfg.line_outs + spec->smart51_nums;
1482f2b1c9f0STakashi Iwai 		else
1483f2b1c9f0STakashi Iwai 			nums = spec->autocfg.line_outs;
1484f2b1c9f0STakashi Iwai 		toggle_output_mutes(codec, nums,
148564be285bSTakashi Iwai 				    spec->autocfg.line_out_pins,
148664be285bSTakashi Iwai 				    present);
148769e52a80SHarald Welte 	}
14884a918ffeSTakashi Iwai 	via_line_automute(codec, present);
1489f3db423dSLydia Wang }
1490f3db423dSLydia Wang 
149169e52a80SHarald Welte static void via_gpio_control(struct hda_codec *codec)
149269e52a80SHarald Welte {
149369e52a80SHarald Welte 	unsigned int gpio_data;
149469e52a80SHarald Welte 	unsigned int vol_counter;
149569e52a80SHarald Welte 	unsigned int vol;
149669e52a80SHarald Welte 	unsigned int master_vol;
149769e52a80SHarald Welte 
149869e52a80SHarald Welte 	struct via_spec *spec = codec->spec;
149969e52a80SHarald Welte 
150069e52a80SHarald Welte 	gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
150169e52a80SHarald Welte 				       AC_VERB_GET_GPIO_DATA, 0) & 0x03;
150269e52a80SHarald Welte 
150369e52a80SHarald Welte 	vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
150469e52a80SHarald Welte 					  0xF84, 0) & 0x3F0000) >> 16;
150569e52a80SHarald Welte 
150669e52a80SHarald Welte 	vol = vol_counter & 0x1F;
150769e52a80SHarald Welte 	master_vol = snd_hda_codec_read(codec, 0x1A, 0,
150869e52a80SHarald Welte 					AC_VERB_GET_AMP_GAIN_MUTE,
150969e52a80SHarald Welte 					AC_AMP_GET_INPUT);
151069e52a80SHarald Welte 
151169e52a80SHarald Welte 	if (gpio_data == 0x02) {
151269e52a80SHarald Welte 		/* unmute line out */
15133e0693e2STakashi Iwai 		snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
15143e0693e2STakashi Iwai 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
15153e0693e2STakashi Iwai 				    PIN_OUT);
151669e52a80SHarald Welte 		if (vol_counter & 0x20) {
151769e52a80SHarald Welte 			/* decrease volume */
151869e52a80SHarald Welte 			if (vol > master_vol)
151969e52a80SHarald Welte 				vol = master_vol;
152069e52a80SHarald Welte 			snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
152169e52a80SHarald Welte 						 0, HDA_AMP_VOLMASK,
152269e52a80SHarald Welte 						 master_vol-vol);
152369e52a80SHarald Welte 		} else {
152469e52a80SHarald Welte 			/* increase volume */
152569e52a80SHarald Welte 			snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
152669e52a80SHarald Welte 					 HDA_AMP_VOLMASK,
152769e52a80SHarald Welte 					 ((master_vol+vol) > 0x2A) ? 0x2A :
152869e52a80SHarald Welte 					  (master_vol+vol));
152969e52a80SHarald Welte 		}
153069e52a80SHarald Welte 	} else if (!(gpio_data & 0x02)) {
153169e52a80SHarald Welte 		/* mute line out */
15323e0693e2STakashi Iwai 		snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
15333e0693e2STakashi Iwai 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
15343e0693e2STakashi Iwai 				    0);
153569e52a80SHarald Welte 	}
153669e52a80SHarald Welte }
153769e52a80SHarald Welte 
153869e52a80SHarald Welte /* unsolicited event for jack sensing */
153969e52a80SHarald Welte static void via_unsol_event(struct hda_codec *codec,
154069e52a80SHarald Welte 				  unsigned int res)
154169e52a80SHarald Welte {
154269e52a80SHarald Welte 	res >>= 26;
1543ec7e7e42SLydia Wang 
1544a34df19aSLydia Wang 	if (res & VIA_JACK_EVENT)
15453e95b9abSLydia Wang 		set_widgets_power_state(codec);
1546ec7e7e42SLydia Wang 
1547ec7e7e42SLydia Wang 	res &= ~VIA_JACK_EVENT;
1548ec7e7e42SLydia Wang 
1549ec7e7e42SLydia Wang 	if (res == VIA_HP_EVENT)
1550ec7e7e42SLydia Wang 		via_hp_automute(codec);
1551ec7e7e42SLydia Wang 	else if (res == VIA_GPIO_EVENT)
1552ec7e7e42SLydia Wang 		via_gpio_control(codec);
15534a918ffeSTakashi Iwai 	else if (res == VIA_LINE_EVENT)
15544a918ffeSTakashi Iwai 		via_line_automute(codec, false);
155569e52a80SHarald Welte }
155669e52a80SHarald Welte 
15571f2e99feSLydia Wang #ifdef SND_HDA_NEEDS_RESUME
15581f2e99feSLydia Wang static int via_suspend(struct hda_codec *codec, pm_message_t state)
15591f2e99feSLydia Wang {
15601f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
15611f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
15621f2e99feSLydia Wang 	return 0;
15631f2e99feSLydia Wang }
15641f2e99feSLydia Wang #endif
15651f2e99feSLydia Wang 
1566cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
1567cb53c626STakashi Iwai static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1568cb53c626STakashi Iwai {
1569cb53c626STakashi Iwai 	struct via_spec *spec = codec->spec;
1570cb53c626STakashi Iwai 	return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1571cb53c626STakashi Iwai }
1572cb53c626STakashi Iwai #endif
1573cb53c626STakashi Iwai 
1574c577b8a1SJoseph Chan /*
1575c577b8a1SJoseph Chan  */
15765d41762aSTakashi Iwai 
15775d41762aSTakashi Iwai static int via_init(struct hda_codec *codec);
15785d41762aSTakashi Iwai 
157990dd48a1STakashi Iwai static const struct hda_codec_ops via_patch_ops = {
1580c577b8a1SJoseph Chan 	.build_controls = via_build_controls,
1581c577b8a1SJoseph Chan 	.build_pcms = via_build_pcms,
1582c577b8a1SJoseph Chan 	.init = via_init,
1583c577b8a1SJoseph Chan 	.free = via_free,
15844a918ffeSTakashi Iwai 	.unsol_event = via_unsol_event,
15851f2e99feSLydia Wang #ifdef SND_HDA_NEEDS_RESUME
15861f2e99feSLydia Wang 	.suspend = via_suspend,
15871f2e99feSLydia Wang #endif
1588cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
1589cb53c626STakashi Iwai 	.check_power_status = via_check_power_status,
1590cb53c626STakashi Iwai #endif
1591c577b8a1SJoseph Chan };
1592c577b8a1SJoseph Chan 
15934a79616dSTakashi Iwai static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
1594c577b8a1SJoseph Chan {
15954a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
15964a79616dSTakashi Iwai 	int i;
15974a79616dSTakashi Iwai 
15984a79616dSTakashi Iwai 	for (i = 0; i < spec->multiout.num_dacs; i++) {
15994a79616dSTakashi Iwai 		if (spec->multiout.dac_nids[i] == dac)
16004a79616dSTakashi Iwai 			return false;
16014a79616dSTakashi Iwai 	}
1602ece8d043STakashi Iwai 	if (spec->hp_dac_nid == dac)
16034a79616dSTakashi Iwai 		return false;
16044a79616dSTakashi Iwai 	return true;
16054a79616dSTakashi Iwai }
16064a79616dSTakashi Iwai 
16078e3679dcSTakashi Iwai static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
16084a79616dSTakashi Iwai 			      hda_nid_t target_dac, struct nid_path *path,
16094a79616dSTakashi Iwai 			      int depth, int wid_type)
16104a79616dSTakashi Iwai {
16114a79616dSTakashi Iwai 	hda_nid_t conn[8];
16124a79616dSTakashi Iwai 	int i, nums;
16134a79616dSTakashi Iwai 
16144a79616dSTakashi Iwai 	nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
16154a79616dSTakashi Iwai 	for (i = 0; i < nums; i++) {
16164a79616dSTakashi Iwai 		if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
16174a79616dSTakashi Iwai 			continue;
161809a9ad69STakashi Iwai 		if (conn[i] == target_dac || is_empty_dac(codec, conn[i]))
161909a9ad69STakashi Iwai 			goto found;
16204a79616dSTakashi Iwai 	}
16218e3679dcSTakashi Iwai 	if (depth >= MAX_NID_PATH_DEPTH)
16224a79616dSTakashi Iwai 		return false;
16234a79616dSTakashi Iwai 	for (i = 0; i < nums; i++) {
16244a79616dSTakashi Iwai 		unsigned int type;
16254a79616dSTakashi Iwai 		type = get_wcaps_type(get_wcaps(codec, conn[i]));
16264a79616dSTakashi Iwai 		if (type == AC_WID_AUD_OUT ||
16274a79616dSTakashi Iwai 		    (wid_type != -1 && type != wid_type))
16284a79616dSTakashi Iwai 			continue;
16298e3679dcSTakashi Iwai 		if (__parse_output_path(codec, conn[i], target_dac,
163009a9ad69STakashi Iwai 				      path, depth + 1, AC_WID_AUD_SEL))
163109a9ad69STakashi Iwai 			goto found;
16324a79616dSTakashi Iwai 	}
16334a79616dSTakashi Iwai 	return false;
163409a9ad69STakashi Iwai 
163509a9ad69STakashi Iwai  found:
163609a9ad69STakashi Iwai 	path->path[path->depth] = conn[i];
163709a9ad69STakashi Iwai 	path->idx[path->depth] = i;
163809a9ad69STakashi Iwai 	if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
163909a9ad69STakashi Iwai 		path->multi[path->depth] = 1;
164009a9ad69STakashi Iwai 	path->depth++;
164109a9ad69STakashi Iwai 	return true;
16424a79616dSTakashi Iwai }
16434a79616dSTakashi Iwai 
16448e3679dcSTakashi Iwai static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
16458e3679dcSTakashi Iwai 			      hda_nid_t target_dac, struct nid_path *path)
16468e3679dcSTakashi Iwai {
16478e3679dcSTakashi Iwai 	if (__parse_output_path(codec, nid, target_dac, path, 1, -1)) {
16488e3679dcSTakashi Iwai 		path->path[path->depth] = nid;
16498e3679dcSTakashi Iwai 		path->depth++;
16508e3679dcSTakashi Iwai 		return true;
16518e3679dcSTakashi Iwai 	}
16528e3679dcSTakashi Iwai 	return false;
16538e3679dcSTakashi Iwai }
16548e3679dcSTakashi Iwai 
16554a79616dSTakashi Iwai static int via_auto_fill_dac_nids(struct hda_codec *codec)
16564a79616dSTakashi Iwai {
16574a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
16584a79616dSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
1659c577b8a1SJoseph Chan 	int i;
1660c577b8a1SJoseph Chan 	hda_nid_t nid;
1661c577b8a1SJoseph Chan 
1662c577b8a1SJoseph Chan 	spec->multiout.dac_nids = spec->private_dac_nids;
16634a79616dSTakashi Iwai 	spec->multiout.num_dacs = cfg->line_outs;
16644a79616dSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
1665c577b8a1SJoseph Chan 		nid = cfg->line_out_pins[i];
16664a79616dSTakashi Iwai 		if (!nid)
16674a79616dSTakashi Iwai 			continue;
16688e3679dcSTakashi Iwai 		if (parse_output_path(codec, nid, 0, &spec->out_path[i]))
16698e3679dcSTakashi Iwai 			spec->private_dac_nids[i] = spec->out_path[i].path[0];
1670c577b8a1SJoseph Chan 	}
1671c577b8a1SJoseph Chan 	return 0;
1672c577b8a1SJoseph Chan }
1673c577b8a1SJoseph Chan 
16744a79616dSTakashi Iwai static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
167509a9ad69STakashi Iwai 			  int chs, bool check_dac, struct nid_path *path)
1676c577b8a1SJoseph Chan {
16774a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
1678c577b8a1SJoseph Chan 	char name[32];
167909a9ad69STakashi Iwai 	hda_nid_t dac, pin, sel, nid;
168009a9ad69STakashi Iwai 	int err;
1681a934d5a9STakashi Iwai 
168209a9ad69STakashi Iwai 	dac = check_dac ? path->path[0] : 0;
168309a9ad69STakashi Iwai 	pin = path->path[path->depth - 1];
168409a9ad69STakashi Iwai 	sel = path->depth > 1 ? path->path[1] : 0;
1685c577b8a1SJoseph Chan 
16868df2a312STakashi Iwai 	if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
16874a79616dSTakashi Iwai 		nid = dac;
16888df2a312STakashi Iwai 	else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
16894a79616dSTakashi Iwai 		nid = pin;
1690a934d5a9STakashi Iwai 	else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1691a934d5a9STakashi Iwai 		nid = sel;
16924a79616dSTakashi Iwai 	else
16934a79616dSTakashi Iwai 		nid = 0;
16944a79616dSTakashi Iwai 	if (nid) {
16954a79616dSTakashi Iwai 		sprintf(name, "%s Playback Volume", pfx);
1696c577b8a1SJoseph Chan 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1697a00a5fadSLydia Wang 			      HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1698c577b8a1SJoseph Chan 		if (err < 0)
1699c577b8a1SJoseph Chan 			return err;
170009a9ad69STakashi Iwai 		path->vol_ctl = nid;
1701c577b8a1SJoseph Chan 	}
17024a79616dSTakashi Iwai 
17038df2a312STakashi Iwai 	if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
17044a79616dSTakashi Iwai 		nid = dac;
17058df2a312STakashi Iwai 	else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
17064a79616dSTakashi Iwai 		nid = pin;
1707a934d5a9STakashi Iwai 	else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
1708a934d5a9STakashi Iwai 		nid = sel;
17094a79616dSTakashi Iwai 	else
17104a79616dSTakashi Iwai 		nid = 0;
17114a79616dSTakashi Iwai 	if (nid) {
17124a79616dSTakashi Iwai 		sprintf(name, "%s Playback Switch", pfx);
17134a79616dSTakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
17144a79616dSTakashi Iwai 			      HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
17154a79616dSTakashi Iwai 		if (err < 0)
17164a79616dSTakashi Iwai 			return err;
171709a9ad69STakashi Iwai 		path->mute_ctl = nid;
17184a79616dSTakashi Iwai 	}
17194a79616dSTakashi Iwai 	return 0;
17204a79616dSTakashi Iwai }
17214a79616dSTakashi Iwai 
1722f4a7828bSTakashi Iwai static void mangle_smart51(struct hda_codec *codec)
1723f4a7828bSTakashi Iwai {
1724f4a7828bSTakashi Iwai 	struct via_spec *spec = codec->spec;
1725f4a7828bSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
17260f98c24bSTakashi Iwai 	struct auto_pin_cfg_item *ins = cfg->inputs;
17270f98c24bSTakashi Iwai 	int i, j, nums, attr;
17280f98c24bSTakashi Iwai 	int pins[AUTO_CFG_MAX_INS];
1729f4a7828bSTakashi Iwai 
17300f98c24bSTakashi Iwai 	for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
17310f98c24bSTakashi Iwai 		nums = 0;
1732f4a7828bSTakashi Iwai 		for (i = 0; i < cfg->num_inputs; i++) {
17330f98c24bSTakashi Iwai 			unsigned int def;
17340f98c24bSTakashi Iwai 			if (ins[i].type > AUTO_PIN_LINE_IN)
17350f98c24bSTakashi Iwai 				continue;
17360f98c24bSTakashi Iwai 			def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
17370f98c24bSTakashi Iwai 			if (snd_hda_get_input_pin_attr(def) != attr)
17380f98c24bSTakashi Iwai 				continue;
17390f98c24bSTakashi Iwai 			for (j = 0; j < nums; j++)
17400f98c24bSTakashi Iwai 				if (ins[pins[j]].type < ins[i].type) {
17410f98c24bSTakashi Iwai 					memmove(pins + j + 1, pins + j,
17420f98c24bSTakashi Iwai 						(nums - j - 1) * sizeof(int));
17430f98c24bSTakashi Iwai 					break;
17440f98c24bSTakashi Iwai 				}
17450f98c24bSTakashi Iwai 			pins[j] = i;
1746e3d7a143STakashi Iwai 			nums++;
1747e3d7a143STakashi Iwai 		}
1748e3d7a143STakashi Iwai 		if (cfg->line_outs + nums < 3)
1749f4a7828bSTakashi Iwai 			continue;
17500f98c24bSTakashi Iwai 		for (i = 0; i < nums; i++) {
17510f98c24bSTakashi Iwai 			hda_nid_t pin = ins[pins[i]].pin;
17520f98c24bSTakashi Iwai 			spec->smart51_pins[spec->smart51_nums++] = pin;
17530f98c24bSTakashi Iwai 			cfg->line_out_pins[cfg->line_outs++] = pin;
1754f4a7828bSTakashi Iwai 			if (cfg->line_outs == 3)
1755f4a7828bSTakashi Iwai 				break;
1756f4a7828bSTakashi Iwai 		}
17570f98c24bSTakashi Iwai 		return;
17580f98c24bSTakashi Iwai 	}
1759f4a7828bSTakashi Iwai }
1760f4a7828bSTakashi Iwai 
17614a79616dSTakashi Iwai /* add playback controls from the parsed DAC table */
17624a79616dSTakashi Iwai static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
17634a79616dSTakashi Iwai {
17644a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
1765f4a7828bSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
17664a79616dSTakashi Iwai 	static const char * const chname[4] = {
17674a79616dSTakashi Iwai 		"Front", "Surround", "C/LFE", "Side"
17684a79616dSTakashi Iwai 	};
17694a79616dSTakashi Iwai 	int i, idx, err;
1770f4a7828bSTakashi Iwai 	int old_line_outs;
1771f4a7828bSTakashi Iwai 
1772f4a7828bSTakashi Iwai 	/* check smart51 */
1773f4a7828bSTakashi Iwai 	old_line_outs = cfg->line_outs;
1774f4a7828bSTakashi Iwai 	if (cfg->line_outs == 1)
1775f4a7828bSTakashi Iwai 		mangle_smart51(codec);
17764a79616dSTakashi Iwai 
1777e3d7a143STakashi Iwai 	err = via_auto_fill_dac_nids(codec);
1778e3d7a143STakashi Iwai 	if (err < 0)
1779e3d7a143STakashi Iwai 		return err;
1780e3d7a143STakashi Iwai 
17814a79616dSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
17824a79616dSTakashi Iwai 		hda_nid_t pin, dac;
17834a79616dSTakashi Iwai 		pin = cfg->line_out_pins[i];
17844a79616dSTakashi Iwai 		dac = spec->multiout.dac_nids[i];
17854a79616dSTakashi Iwai 		if (!pin || !dac)
17864a79616dSTakashi Iwai 			continue;
17870fe0adf8STakashi Iwai 		if (i == HDA_CLFE) {
178809a9ad69STakashi Iwai 			err = create_ch_ctls(codec, "Center", 1, true,
178909a9ad69STakashi Iwai 					     &spec->out_path[i]);
17904a79616dSTakashi Iwai 			if (err < 0)
17914a79616dSTakashi Iwai 				return err;
179209a9ad69STakashi Iwai 			err = create_ch_ctls(codec, "LFE", 2, true,
179309a9ad69STakashi Iwai 					     &spec->out_path[i]);
17944a79616dSTakashi Iwai 			if (err < 0)
17954a79616dSTakashi Iwai 				return err;
17964a79616dSTakashi Iwai 		} else {
17976aadf41dSTakashi Iwai 			const char *pfx = chname[i];
17986aadf41dSTakashi Iwai 			if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
17996aadf41dSTakashi Iwai 			    cfg->line_outs == 1)
18006aadf41dSTakashi Iwai 				pfx = "Speaker";
180109a9ad69STakashi Iwai 			err = create_ch_ctls(codec, pfx, 3, true,
180209a9ad69STakashi Iwai 					     &spec->out_path[i]);
18034a79616dSTakashi Iwai 			if (err < 0)
18044a79616dSTakashi Iwai 				return err;
18054a79616dSTakashi Iwai 		}
18064a79616dSTakashi Iwai 	}
18074a79616dSTakashi Iwai 
18084a79616dSTakashi Iwai 	idx = get_connection_index(codec, spec->aa_mix_nid,
18094a79616dSTakashi Iwai 				   spec->multiout.dac_nids[0]);
18104a79616dSTakashi Iwai 	if (idx >= 0) {
18114a79616dSTakashi Iwai 		/* add control to mixer */
18124a79616dSTakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
18134a79616dSTakashi Iwai 				      "PCM Playback Volume",
18144a79616dSTakashi Iwai 				      HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
18154a79616dSTakashi Iwai 							  idx, HDA_INPUT));
18164a79616dSTakashi Iwai 		if (err < 0)
18174a79616dSTakashi Iwai 			return err;
18184a79616dSTakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
18194a79616dSTakashi Iwai 				      "PCM Playback Switch",
18204a79616dSTakashi Iwai 				      HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
18214a79616dSTakashi Iwai 							  idx, HDA_INPUT));
18224a79616dSTakashi Iwai 		if (err < 0)
18234a79616dSTakashi Iwai 			return err;
1824c577b8a1SJoseph Chan 	}
1825c577b8a1SJoseph Chan 
1826f4a7828bSTakashi Iwai 	cfg->line_outs = old_line_outs;
1827f4a7828bSTakashi Iwai 
1828c577b8a1SJoseph Chan 	return 0;
1829c577b8a1SJoseph Chan }
1830c577b8a1SJoseph Chan 
18314a79616dSTakashi Iwai static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
1832c577b8a1SJoseph Chan {
18334a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
183409a9ad69STakashi Iwai 	struct nid_path *path;
1835c577b8a1SJoseph Chan 	int err;
1836c577b8a1SJoseph Chan 
1837c577b8a1SJoseph Chan 	if (!pin)
1838c577b8a1SJoseph Chan 		return 0;
1839c577b8a1SJoseph Chan 
18408df2a312STakashi Iwai 	if (parse_output_path(codec, pin, 0, &spec->hp_path))
18418e3679dcSTakashi Iwai 		spec->hp_dac_nid = spec->hp_path.path[0];
1842*25250505STakashi Iwai 	else if (spec->multiout.dac_nids[HDA_SIDE] &&
1843*25250505STakashi Iwai 		 parse_output_path(codec, pin,
1844*25250505STakashi Iwai 				   spec->multiout.dac_nids[HDA_SIDE],
1845*25250505STakashi Iwai 				   &spec->hp_path)) {
1846*25250505STakashi Iwai 		spec->hp_dac_nid = spec->hp_path.path[0];
1847*25250505STakashi Iwai 		spec->hp_indep_shared = true;
1848*25250505STakashi Iwai 	}
18494a79616dSTakashi Iwai 
1850ece8d043STakashi Iwai 	if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
18518e3679dcSTakashi Iwai 			       &spec->hp_dep_path) &&
1852ece8d043STakashi Iwai 	    !spec->hp_dac_nid)
1853ece8d043STakashi Iwai 		return 0;
1854ece8d043STakashi Iwai 
1855*25250505STakashi Iwai 	if (spec->hp_dac_nid && !spec->hp_indep_shared)
185609a9ad69STakashi Iwai 		path = &spec->hp_path;
185709a9ad69STakashi Iwai 	else
185809a9ad69STakashi Iwai 		path = &spec->hp_dep_path;
185909a9ad69STakashi Iwai 	err = create_ch_ctls(codec, "Headphone", 3, false, path);
18604a79616dSTakashi Iwai 	if (err < 0)
18614a79616dSTakashi Iwai 		return err;
186209a9ad69STakashi Iwai 	if (spec->hp_dac_nid) {
186309a9ad69STakashi Iwai 		spec->hp_dep_path.vol_ctl = spec->hp_path.vol_ctl;
186409a9ad69STakashi Iwai 		spec->hp_dep_path.mute_ctl = spec->hp_path.mute_ctl;
186509a9ad69STakashi Iwai 	}
18660aa62aefSHarald Welte 
1867c577b8a1SJoseph Chan 	return 0;
1868c577b8a1SJoseph Chan }
1869c577b8a1SJoseph Chan 
18704a918ffeSTakashi Iwai static int via_auto_create_speaker_ctls(struct hda_codec *codec)
18714a918ffeSTakashi Iwai {
18724a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
18734a918ffeSTakashi Iwai 	hda_nid_t pin, dac;
18744a918ffeSTakashi Iwai 
18754a918ffeSTakashi Iwai 	pin = spec->autocfg.speaker_pins[0];
18764a918ffeSTakashi Iwai 	if (!spec->autocfg.speaker_outs || !pin)
18774a918ffeSTakashi Iwai 		return 0;
18784a918ffeSTakashi Iwai 
18798e3679dcSTakashi Iwai 	if (parse_output_path(codec, pin, 0, &spec->speaker_path)) {
18808e3679dcSTakashi Iwai 		dac = spec->speaker_path.path[0];
18814a918ffeSTakashi Iwai 		spec->multiout.extra_out_nid[0] = dac;
188209a9ad69STakashi Iwai 		return create_ch_ctls(codec, "Speaker", 3, true,
188309a9ad69STakashi Iwai 				      &spec->speaker_path);
18844a918ffeSTakashi Iwai 	}
18854a918ffeSTakashi Iwai 	if (parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
18868e3679dcSTakashi Iwai 			      &spec->speaker_path))
188709a9ad69STakashi Iwai 		return create_ch_ctls(codec, "Speaker", 3, false,
188809a9ad69STakashi Iwai 				      &spec->speaker_path);
18894a918ffeSTakashi Iwai 
18904a918ffeSTakashi Iwai 	return 0;
18914a918ffeSTakashi Iwai }
18924a918ffeSTakashi Iwai 
1893a766d0d7STakashi Iwai /* look for ADCs */
1894a766d0d7STakashi Iwai static int via_fill_adcs(struct hda_codec *codec)
1895a766d0d7STakashi Iwai {
1896a766d0d7STakashi Iwai 	struct via_spec *spec = codec->spec;
1897a766d0d7STakashi Iwai 	hda_nid_t nid = codec->start_nid;
1898a766d0d7STakashi Iwai 	int i;
1899a766d0d7STakashi Iwai 
1900a766d0d7STakashi Iwai 	for (i = 0; i < codec->num_nodes; i++, nid++) {
1901a766d0d7STakashi Iwai 		unsigned int wcaps = get_wcaps(codec, nid);
1902a766d0d7STakashi Iwai 		if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
1903a766d0d7STakashi Iwai 			continue;
1904a766d0d7STakashi Iwai 		if (wcaps & AC_WCAP_DIGITAL)
1905a766d0d7STakashi Iwai 			continue;
1906a766d0d7STakashi Iwai 		if (!(wcaps & AC_WCAP_CONN_LIST))
1907a766d0d7STakashi Iwai 			continue;
1908a766d0d7STakashi Iwai 		if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
1909a766d0d7STakashi Iwai 			return -ENOMEM;
1910a766d0d7STakashi Iwai 		spec->adc_nids[spec->num_adc_nids++] = nid;
1911a766d0d7STakashi Iwai 	}
1912a766d0d7STakashi Iwai 	return 0;
1913a766d0d7STakashi Iwai }
1914a766d0d7STakashi Iwai 
1915a86a88eaSTakashi Iwai /* input-src control */
1916a86a88eaSTakashi Iwai static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
1917a86a88eaSTakashi Iwai 			     struct snd_ctl_elem_info *uinfo)
1918a86a88eaSTakashi Iwai {
1919a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1920a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1921a86a88eaSTakashi Iwai 
1922a86a88eaSTakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1923a86a88eaSTakashi Iwai 	uinfo->count = 1;
1924a86a88eaSTakashi Iwai 	uinfo->value.enumerated.items = spec->num_inputs;
1925a86a88eaSTakashi Iwai 	if (uinfo->value.enumerated.item >= spec->num_inputs)
1926a86a88eaSTakashi Iwai 		uinfo->value.enumerated.item = spec->num_inputs - 1;
1927a86a88eaSTakashi Iwai 	strcpy(uinfo->value.enumerated.name,
1928a86a88eaSTakashi Iwai 	       spec->inputs[uinfo->value.enumerated.item].label);
1929a86a88eaSTakashi Iwai 	return 0;
1930a86a88eaSTakashi Iwai }
1931a86a88eaSTakashi Iwai 
1932a86a88eaSTakashi Iwai static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
1933a86a88eaSTakashi Iwai 			    struct snd_ctl_elem_value *ucontrol)
1934a86a88eaSTakashi Iwai {
1935a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1936a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1937a86a88eaSTakashi Iwai 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1938a86a88eaSTakashi Iwai 
1939a86a88eaSTakashi Iwai 	ucontrol->value.enumerated.item[0] = spec->cur_mux[idx];
1940a86a88eaSTakashi Iwai 	return 0;
1941a86a88eaSTakashi Iwai }
1942a86a88eaSTakashi Iwai 
1943a86a88eaSTakashi Iwai static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
1944a86a88eaSTakashi Iwai 			    struct snd_ctl_elem_value *ucontrol)
1945a86a88eaSTakashi Iwai {
1946a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1947a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1948a86a88eaSTakashi Iwai 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1949a86a88eaSTakashi Iwai 	hda_nid_t mux;
1950a86a88eaSTakashi Iwai 	int cur;
1951a86a88eaSTakashi Iwai 
1952a86a88eaSTakashi Iwai 	cur = ucontrol->value.enumerated.item[0];
1953a86a88eaSTakashi Iwai 	if (cur < 0 || cur >= spec->num_inputs)
1954a86a88eaSTakashi Iwai 		return -EINVAL;
1955a86a88eaSTakashi Iwai 	if (spec->cur_mux[idx] == cur)
1956a86a88eaSTakashi Iwai 		return 0;
1957a86a88eaSTakashi Iwai 	spec->cur_mux[idx] = cur;
1958a86a88eaSTakashi Iwai 	if (spec->dyn_adc_switch) {
1959a86a88eaSTakashi Iwai 		int adc_idx = spec->inputs[cur].adc_idx;
1960a86a88eaSTakashi Iwai 		mux = spec->mux_nids[adc_idx];
1961a86a88eaSTakashi Iwai 		via_dyn_adc_pcm_resetup(codec, cur);
1962a86a88eaSTakashi Iwai 	} else {
1963a86a88eaSTakashi Iwai 		mux = spec->mux_nids[idx];
1964a86a88eaSTakashi Iwai 		if (snd_BUG_ON(!mux))
1965a86a88eaSTakashi Iwai 			return -EINVAL;
1966a86a88eaSTakashi Iwai 	}
1967a86a88eaSTakashi Iwai 
1968a86a88eaSTakashi Iwai 	if (mux) {
1969a86a88eaSTakashi Iwai 		/* switch to D0 beofre change index */
1970a86a88eaSTakashi Iwai 		if (snd_hda_codec_read(codec, mux, 0,
1971a86a88eaSTakashi Iwai 			       AC_VERB_GET_POWER_STATE, 0x00) != AC_PWRST_D0)
1972a86a88eaSTakashi Iwai 			snd_hda_codec_write(codec, mux, 0,
1973a86a88eaSTakashi Iwai 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1974a86a88eaSTakashi Iwai 		snd_hda_codec_write(codec, mux, 0,
1975a86a88eaSTakashi Iwai 				    AC_VERB_SET_CONNECT_SEL,
1976a86a88eaSTakashi Iwai 				    spec->inputs[cur].mux_idx);
1977a86a88eaSTakashi Iwai 	}
1978a86a88eaSTakashi Iwai 
1979a86a88eaSTakashi Iwai 	/* update jack power state */
1980a86a88eaSTakashi Iwai 	set_widgets_power_state(codec);
1981a86a88eaSTakashi Iwai 	return 0;
1982a86a88eaSTakashi Iwai }
1983a766d0d7STakashi Iwai 
1984d7a99cceSTakashi Iwai static const struct snd_kcontrol_new via_input_src_ctl = {
1985d7a99cceSTakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1986d7a99cceSTakashi Iwai 	/* The multiple "Capture Source" controls confuse alsamixer
1987d7a99cceSTakashi Iwai 	 * So call somewhat different..
1988d7a99cceSTakashi Iwai 	 */
1989d7a99cceSTakashi Iwai 	/* .name = "Capture Source", */
1990d7a99cceSTakashi Iwai 	.name = "Input Source",
1991d7a99cceSTakashi Iwai 	.info = via_mux_enum_info,
1992d7a99cceSTakashi Iwai 	.get = via_mux_enum_get,
1993d7a99cceSTakashi Iwai 	.put = via_mux_enum_put,
1994d7a99cceSTakashi Iwai };
1995d7a99cceSTakashi Iwai 
1996a86a88eaSTakashi Iwai static int create_input_src_ctls(struct hda_codec *codec, int count)
1997a86a88eaSTakashi Iwai {
1998a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1999a86a88eaSTakashi Iwai 	struct snd_kcontrol_new *knew;
2000a86a88eaSTakashi Iwai 
2001a86a88eaSTakashi Iwai 	if (spec->num_inputs <= 1 || !count)
2002a86a88eaSTakashi Iwai 		return 0; /* no need for single src */
2003a86a88eaSTakashi Iwai 
2004a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_input_src_ctl);
2005a86a88eaSTakashi Iwai 	if (!knew)
2006a86a88eaSTakashi Iwai 		return -ENOMEM;
2007a86a88eaSTakashi Iwai 	knew->count = count;
2008a86a88eaSTakashi Iwai 	return 0;
2009a86a88eaSTakashi Iwai }
2010a86a88eaSTakashi Iwai 
2011a86a88eaSTakashi Iwai /* add the powersave loopback-list entry */
201213af8e77STakashi Iwai static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
201313af8e77STakashi Iwai {
201413af8e77STakashi Iwai 	struct hda_amp_list *list;
201513af8e77STakashi Iwai 
201613af8e77STakashi Iwai 	if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
201713af8e77STakashi Iwai 		return;
201813af8e77STakashi Iwai 	list = spec->loopback_list + spec->num_loopbacks;
201913af8e77STakashi Iwai 	list->nid = mix;
202013af8e77STakashi Iwai 	list->dir = HDA_INPUT;
202113af8e77STakashi Iwai 	list->idx = idx;
202213af8e77STakashi Iwai 	spec->num_loopbacks++;
202313af8e77STakashi Iwai 	spec->loopback.amplist = spec->loopback_list;
202413af8e77STakashi Iwai }
202513af8e77STakashi Iwai 
2026a86a88eaSTakashi Iwai static bool is_reachable_nid(struct hda_codec *codec, hda_nid_t src,
20278d087c76STakashi Iwai 			     hda_nid_t dst)
2028a86a88eaSTakashi Iwai {
20298d087c76STakashi Iwai 	return snd_hda_get_conn_index(codec, src, dst, 1) >= 0;
2030a86a88eaSTakashi Iwai }
2031a86a88eaSTakashi Iwai 
2032a86a88eaSTakashi Iwai /* add the input-route to the given pin */
2033a86a88eaSTakashi Iwai static bool add_input_route(struct hda_codec *codec, hda_nid_t pin)
2034c577b8a1SJoseph Chan {
203510a20af7STakashi Iwai 	struct via_spec *spec = codec->spec;
2036a86a88eaSTakashi Iwai 	int c, idx;
2037a86a88eaSTakashi Iwai 
2038a86a88eaSTakashi Iwai 	spec->inputs[spec->num_inputs].adc_idx = -1;
2039a86a88eaSTakashi Iwai 	spec->inputs[spec->num_inputs].pin = pin;
2040a86a88eaSTakashi Iwai 	for (c = 0; c < spec->num_adc_nids; c++) {
2041a86a88eaSTakashi Iwai 		if (spec->mux_nids[c]) {
2042a86a88eaSTakashi Iwai 			idx = get_connection_index(codec, spec->mux_nids[c],
2043a86a88eaSTakashi Iwai 						   pin);
2044a86a88eaSTakashi Iwai 			if (idx < 0)
2045a86a88eaSTakashi Iwai 				continue;
2046a86a88eaSTakashi Iwai 			spec->inputs[spec->num_inputs].mux_idx = idx;
2047a86a88eaSTakashi Iwai 		} else {
20488d087c76STakashi Iwai 			if (!is_reachable_nid(codec, spec->adc_nids[c], pin))
2049a86a88eaSTakashi Iwai 				continue;
2050a86a88eaSTakashi Iwai 		}
2051a86a88eaSTakashi Iwai 		spec->inputs[spec->num_inputs].adc_idx = c;
2052a86a88eaSTakashi Iwai 		/* Can primary ADC satisfy all inputs? */
2053a86a88eaSTakashi Iwai 		if (!spec->dyn_adc_switch &&
2054a86a88eaSTakashi Iwai 		    spec->num_inputs > 0 && spec->inputs[0].adc_idx != c) {
2055a86a88eaSTakashi Iwai 			snd_printd(KERN_INFO
2056a86a88eaSTakashi Iwai 				   "via: dynamic ADC switching enabled\n");
2057a86a88eaSTakashi Iwai 			spec->dyn_adc_switch = 1;
2058a86a88eaSTakashi Iwai 		}
2059a86a88eaSTakashi Iwai 		return true;
2060a86a88eaSTakashi Iwai 	}
2061a86a88eaSTakashi Iwai 	return false;
2062a86a88eaSTakashi Iwai }
2063a86a88eaSTakashi Iwai 
2064a86a88eaSTakashi Iwai static int get_mux_nids(struct hda_codec *codec);
2065a86a88eaSTakashi Iwai 
2066a86a88eaSTakashi Iwai /* parse input-routes; fill ADCs, MUXs and input-src entries */
2067a86a88eaSTakashi Iwai static int parse_analog_inputs(struct hda_codec *codec)
2068a86a88eaSTakashi Iwai {
2069a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2070a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2071a86a88eaSTakashi Iwai 	int i, err;
2072a766d0d7STakashi Iwai 
2073a766d0d7STakashi Iwai 	err = via_fill_adcs(codec);
2074a766d0d7STakashi Iwai 	if (err < 0)
2075a766d0d7STakashi Iwai 		return err;
2076a766d0d7STakashi Iwai 	err = get_mux_nids(codec);
2077a766d0d7STakashi Iwai 	if (err < 0)
2078a766d0d7STakashi Iwai 		return err;
2079a766d0d7STakashi Iwai 
2080a86a88eaSTakashi Iwai 	/* fill all input-routes */
2081a86a88eaSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2082a86a88eaSTakashi Iwai 		if (add_input_route(codec, cfg->inputs[i].pin))
2083a86a88eaSTakashi Iwai 			spec->inputs[spec->num_inputs++].label =
2084a86a88eaSTakashi Iwai 				hda_get_autocfg_input_label(codec, cfg, i);
2085a86a88eaSTakashi Iwai 	}
2086a86a88eaSTakashi Iwai 
2087a86a88eaSTakashi Iwai 	/* check for internal loopback recording */
2088a86a88eaSTakashi Iwai 	if (spec->aa_mix_nid &&
2089a86a88eaSTakashi Iwai 	    add_input_route(codec, spec->aa_mix_nid))
2090a86a88eaSTakashi Iwai 		spec->inputs[spec->num_inputs++].label = "Stereo Mixer";
2091a86a88eaSTakashi Iwai 
2092a86a88eaSTakashi Iwai 	return 0;
2093a86a88eaSTakashi Iwai }
2094a86a88eaSTakashi Iwai 
2095a86a88eaSTakashi Iwai /* create analog-loopback volume/switch controls */
2096a86a88eaSTakashi Iwai static int create_loopback_ctls(struct hda_codec *codec)
2097a86a88eaSTakashi Iwai {
2098a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2099a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2100a86a88eaSTakashi Iwai 	const char *prev_label = NULL;
2101a86a88eaSTakashi Iwai 	int type_idx = 0;
2102a86a88eaSTakashi Iwai 	int i, j, err, idx;
2103a86a88eaSTakashi Iwai 
2104a86a88eaSTakashi Iwai 	if (!spec->aa_mix_nid)
2105a766d0d7STakashi Iwai 		return 0;
2106c577b8a1SJoseph Chan 
21077b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2108a86a88eaSTakashi Iwai 		hda_nid_t pin = cfg->inputs[i].pin;
2109a86a88eaSTakashi Iwai 		const char *label = hda_get_autocfg_input_label(codec, cfg, i);
2110a86a88eaSTakashi Iwai 
21111e11cae1STakashi Iwai 		if (prev_label && !strcmp(label, prev_label))
21127b315bb4STakashi Iwai 			type_idx++;
21137b315bb4STakashi Iwai 		else
21147b315bb4STakashi Iwai 			type_idx = 0;
21151e11cae1STakashi Iwai 		prev_label = label;
2116a86a88eaSTakashi Iwai 		idx = get_connection_index(codec, spec->aa_mix_nid, pin);
2117a86a88eaSTakashi Iwai 		if (idx >= 0) {
211816922281SLydia Wang 			err = via_new_analog_input(spec, label, type_idx,
2119a86a88eaSTakashi Iwai 						   idx, spec->aa_mix_nid);
2120c577b8a1SJoseph Chan 			if (err < 0)
2121c577b8a1SJoseph Chan 				return err;
2122a86a88eaSTakashi Iwai 			add_loopback_list(spec, spec->aa_mix_nid, idx);
212313af8e77STakashi Iwai 		}
2124e3d7a143STakashi Iwai 
2125e3d7a143STakashi Iwai 		/* remember the label for smart51 control */
2126e3d7a143STakashi Iwai 		for (j = 0; j < spec->smart51_nums; j++) {
2127a86a88eaSTakashi Iwai 			if (spec->smart51_pins[j] == pin) {
2128e3d7a143STakashi Iwai 				spec->smart51_idxs[j] = idx;
2129e3d7a143STakashi Iwai 				spec->smart51_labels[j] = label;
2130e3d7a143STakashi Iwai 				break;
2131e3d7a143STakashi Iwai 			}
2132e3d7a143STakashi Iwai 		}
2133c577b8a1SJoseph Chan 	}
2134a86a88eaSTakashi Iwai 	return 0;
2135a86a88eaSTakashi Iwai }
2136a86a88eaSTakashi Iwai 
2137a86a88eaSTakashi Iwai /* create mic-boost controls (if present) */
2138a86a88eaSTakashi Iwai static int create_mic_boost_ctls(struct hda_codec *codec)
2139a86a88eaSTakashi Iwai {
2140a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2141a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2142a86a88eaSTakashi Iwai 	int i, err;
2143a86a88eaSTakashi Iwai 
2144a86a88eaSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2145a86a88eaSTakashi Iwai 		hda_nid_t pin = cfg->inputs[i].pin;
2146a86a88eaSTakashi Iwai 		unsigned int caps;
2147a86a88eaSTakashi Iwai 		const char *label;
2148a86a88eaSTakashi Iwai 		char name[32];
2149a86a88eaSTakashi Iwai 
2150a86a88eaSTakashi Iwai 		if (cfg->inputs[i].type != AUTO_PIN_MIC)
2151a86a88eaSTakashi Iwai 			continue;
2152a86a88eaSTakashi Iwai 		caps = query_amp_caps(codec, pin, HDA_INPUT);
2153a86a88eaSTakashi Iwai 		if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
2154a86a88eaSTakashi Iwai 			continue;
2155a86a88eaSTakashi Iwai 		label = hda_get_autocfg_input_label(codec, cfg, i);
2156a86a88eaSTakashi Iwai 		snprintf(name, sizeof(name), "%s Boost Volume", label);
2157a86a88eaSTakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2158a86a88eaSTakashi Iwai 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
2159a86a88eaSTakashi Iwai 		if (err < 0)
2160a86a88eaSTakashi Iwai 			return err;
2161a86a88eaSTakashi Iwai 	}
2162a86a88eaSTakashi Iwai 	return 0;
2163a86a88eaSTakashi Iwai }
2164a86a88eaSTakashi Iwai 
2165a86a88eaSTakashi Iwai /* create capture and input-src controls for multiple streams */
2166a86a88eaSTakashi Iwai static int create_multi_adc_ctls(struct hda_codec *codec)
2167a86a88eaSTakashi Iwai {
2168a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2169a86a88eaSTakashi Iwai 	int i, err;
2170d7a99cceSTakashi Iwai 
2171d7a99cceSTakashi Iwai 	/* create capture mixer elements */
2172d7a99cceSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
2173d7a99cceSTakashi Iwai 		hda_nid_t adc = spec->adc_nids[i];
2174d7a99cceSTakashi Iwai 		err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
2175d7a99cceSTakashi Iwai 					"Capture Volume", i,
2176d7a99cceSTakashi Iwai 					HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2177d7a99cceSTakashi Iwai 							    HDA_INPUT));
2178d7a99cceSTakashi Iwai 		if (err < 0)
2179d7a99cceSTakashi Iwai 			return err;
2180d7a99cceSTakashi Iwai 		err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2181d7a99cceSTakashi Iwai 					"Capture Switch", i,
2182d7a99cceSTakashi Iwai 					HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2183d7a99cceSTakashi Iwai 							    HDA_INPUT));
2184d7a99cceSTakashi Iwai 		if (err < 0)
2185d7a99cceSTakashi Iwai 			return err;
2186d7a99cceSTakashi Iwai 	}
2187d7a99cceSTakashi Iwai 
2188d7a99cceSTakashi Iwai 	/* input-source control */
2189d7a99cceSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++)
2190d7a99cceSTakashi Iwai 		if (!spec->mux_nids[i])
2191d7a99cceSTakashi Iwai 			break;
2192a86a88eaSTakashi Iwai 	err = create_input_src_ctls(codec, i);
2193d7a99cceSTakashi Iwai 	if (err < 0)
2194d7a99cceSTakashi Iwai 		return err;
2195a86a88eaSTakashi Iwai 	return 0;
2196d7a99cceSTakashi Iwai }
2197d7a99cceSTakashi Iwai 
2198a86a88eaSTakashi Iwai /* bind capture volume/switch */
2199a86a88eaSTakashi Iwai static struct snd_kcontrol_new via_bind_cap_vol_ctl =
2200a86a88eaSTakashi Iwai 	HDA_BIND_VOL("Capture Volume", 0);
2201a86a88eaSTakashi Iwai static struct snd_kcontrol_new via_bind_cap_sw_ctl =
2202a86a88eaSTakashi Iwai 	HDA_BIND_SW("Capture Switch", 0);
2203a86a88eaSTakashi Iwai 
2204a86a88eaSTakashi Iwai static int init_bind_ctl(struct via_spec *spec, struct hda_bind_ctls **ctl_ret,
2205a86a88eaSTakashi Iwai 			 struct hda_ctl_ops *ops)
2206a86a88eaSTakashi Iwai {
2207a86a88eaSTakashi Iwai 	struct hda_bind_ctls *ctl;
2208a86a88eaSTakashi Iwai 	int i;
2209a86a88eaSTakashi Iwai 
2210a86a88eaSTakashi Iwai 	ctl = kzalloc(sizeof(*ctl) + sizeof(long) * 4, GFP_KERNEL);
2211a86a88eaSTakashi Iwai 	if (!ctl)
2212a86a88eaSTakashi Iwai 		return -ENOMEM;
2213a86a88eaSTakashi Iwai 	ctl->ops = ops;
2214a86a88eaSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++)
2215a86a88eaSTakashi Iwai 		ctl->values[i] =
2216a86a88eaSTakashi Iwai 			HDA_COMPOSE_AMP_VAL(spec->adc_nids[i], 3, 0, HDA_INPUT);
2217a86a88eaSTakashi Iwai 	*ctl_ret = ctl;
2218a86a88eaSTakashi Iwai 	return 0;
2219a86a88eaSTakashi Iwai }
2220a86a88eaSTakashi Iwai 
2221a86a88eaSTakashi Iwai /* create capture and input-src controls for dynamic ADC-switch case */
2222a86a88eaSTakashi Iwai static int create_dyn_adc_ctls(struct hda_codec *codec)
2223a86a88eaSTakashi Iwai {
2224a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2225a86a88eaSTakashi Iwai 	struct snd_kcontrol_new *knew;
2226a86a88eaSTakashi Iwai 	int err;
2227a86a88eaSTakashi Iwai 
2228a86a88eaSTakashi Iwai 	/* set up the bind capture ctls */
2229a86a88eaSTakashi Iwai 	err = init_bind_ctl(spec, &spec->bind_cap_vol, &snd_hda_bind_vol);
2230a86a88eaSTakashi Iwai 	if (err < 0)
2231a86a88eaSTakashi Iwai 		return err;
2232a86a88eaSTakashi Iwai 	err = init_bind_ctl(spec, &spec->bind_cap_sw, &snd_hda_bind_sw);
2233a86a88eaSTakashi Iwai 	if (err < 0)
2234a86a88eaSTakashi Iwai 		return err;
2235a86a88eaSTakashi Iwai 
2236a86a88eaSTakashi Iwai 	/* create capture mixer elements */
2237a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_bind_cap_vol_ctl);
2238a86a88eaSTakashi Iwai 	if (!knew)
2239a86a88eaSTakashi Iwai 		return -ENOMEM;
2240a86a88eaSTakashi Iwai 	knew->private_value = (long)spec->bind_cap_vol;
2241a86a88eaSTakashi Iwai 
2242a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_bind_cap_sw_ctl);
2243a86a88eaSTakashi Iwai 	if (!knew)
2244a86a88eaSTakashi Iwai 		return -ENOMEM;
2245a86a88eaSTakashi Iwai 	knew->private_value = (long)spec->bind_cap_sw;
2246a86a88eaSTakashi Iwai 
2247a86a88eaSTakashi Iwai 	/* input-source control */
2248a86a88eaSTakashi Iwai 	err = create_input_src_ctls(codec, 1);
2249a86a88eaSTakashi Iwai 	if (err < 0)
2250a86a88eaSTakashi Iwai 		return err;
2251a86a88eaSTakashi Iwai 	return 0;
2252a86a88eaSTakashi Iwai }
2253a86a88eaSTakashi Iwai 
2254a86a88eaSTakashi Iwai /* parse and create capture-related stuff */
2255a86a88eaSTakashi Iwai static int via_auto_create_analog_input_ctls(struct hda_codec *codec)
2256a86a88eaSTakashi Iwai {
2257a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2258a86a88eaSTakashi Iwai 	int err;
2259a86a88eaSTakashi Iwai 
2260a86a88eaSTakashi Iwai 	err = parse_analog_inputs(codec);
2261a86a88eaSTakashi Iwai 	if (err < 0)
2262a86a88eaSTakashi Iwai 		return err;
2263a86a88eaSTakashi Iwai 	if (spec->dyn_adc_switch)
2264a86a88eaSTakashi Iwai 		err = create_dyn_adc_ctls(codec);
2265a86a88eaSTakashi Iwai 	else
2266a86a88eaSTakashi Iwai 		err = create_multi_adc_ctls(codec);
2267a86a88eaSTakashi Iwai 	if (err < 0)
2268a86a88eaSTakashi Iwai 		return err;
2269a86a88eaSTakashi Iwai 	err = create_loopback_ctls(codec);
2270a86a88eaSTakashi Iwai 	if (err < 0)
2271a86a88eaSTakashi Iwai 		return err;
2272a86a88eaSTakashi Iwai 	err = create_mic_boost_ctls(codec);
2273a86a88eaSTakashi Iwai 	if (err < 0)
2274a86a88eaSTakashi Iwai 		return err;
2275c577b8a1SJoseph Chan 	return 0;
2276c577b8a1SJoseph Chan }
2277c577b8a1SJoseph Chan 
227876d9b0ddSHarald Welte static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
227976d9b0ddSHarald Welte {
228076d9b0ddSHarald Welte 	unsigned int def_conf;
228176d9b0ddSHarald Welte 	unsigned char seqassoc;
228276d9b0ddSHarald Welte 
22832f334f92STakashi Iwai 	def_conf = snd_hda_codec_get_pincfg(codec, nid);
228476d9b0ddSHarald Welte 	seqassoc = (unsigned char) get_defcfg_association(def_conf);
228576d9b0ddSHarald Welte 	seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
228682ef9e45SLydia Wang 	if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
228782ef9e45SLydia Wang 	    && (seqassoc == 0xf0 || seqassoc == 0xff)) {
228876d9b0ddSHarald Welte 		def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
22892f334f92STakashi Iwai 		snd_hda_codec_set_pincfg(codec, nid, def_conf);
229076d9b0ddSHarald Welte 	}
229176d9b0ddSHarald Welte 
229276d9b0ddSHarald Welte 	return;
229376d9b0ddSHarald Welte }
229476d9b0ddSHarald Welte 
2295e06e5a29STakashi Iwai static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
22961f2e99feSLydia Wang 				     struct snd_ctl_elem_value *ucontrol)
22971f2e99feSLydia Wang {
22981f2e99feSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
22991f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
23001f2e99feSLydia Wang 
23011f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
23021f2e99feSLydia Wang 		return 0;
2303e06e5a29STakashi Iwai 	spec->vt1708_jack_detect =
23041f2e99feSLydia Wang 		!((snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8) & 0x1);
2305e06e5a29STakashi Iwai 	ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
23061f2e99feSLydia Wang 	return 0;
23071f2e99feSLydia Wang }
23081f2e99feSLydia Wang 
2309e06e5a29STakashi Iwai static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
23101f2e99feSLydia Wang 				     struct snd_ctl_elem_value *ucontrol)
23111f2e99feSLydia Wang {
23121f2e99feSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
23131f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
23141f2e99feSLydia Wang 	int change;
23151f2e99feSLydia Wang 
23161f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
23171f2e99feSLydia Wang 		return 0;
2318e06e5a29STakashi Iwai 	spec->vt1708_jack_detect = ucontrol->value.integer.value[0];
23191f2e99feSLydia Wang 	change = (0x1 & (snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8))
2320e06e5a29STakashi Iwai 		== !spec->vt1708_jack_detect;
2321e06e5a29STakashi Iwai 	if (spec->vt1708_jack_detect) {
23221f2e99feSLydia Wang 		mute_aa_path(codec, 1);
23231f2e99feSLydia Wang 		notify_aa_path_ctls(codec);
23241f2e99feSLydia Wang 	}
23251f2e99feSLydia Wang 	return change;
23261f2e99feSLydia Wang }
23271f2e99feSLydia Wang 
2328e06e5a29STakashi Iwai static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
23291f2e99feSLydia Wang 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
23301f2e99feSLydia Wang 	.name = "Jack Detect",
23311f2e99feSLydia Wang 	.count = 1,
23321f2e99feSLydia Wang 	.info = snd_ctl_boolean_mono_info,
2333e06e5a29STakashi Iwai 	.get = vt1708_jack_detect_get,
2334e06e5a29STakashi Iwai 	.put = vt1708_jack_detect_put,
23351f2e99feSLydia Wang };
23361f2e99feSLydia Wang 
233712daef65STakashi Iwai static void fill_dig_outs(struct hda_codec *codec);
233812daef65STakashi Iwai static void fill_dig_in(struct hda_codec *codec);
233912daef65STakashi Iwai 
234012daef65STakashi Iwai static int via_parse_auto_config(struct hda_codec *codec)
2341c577b8a1SJoseph Chan {
2342c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
2343c577b8a1SJoseph Chan 	int err;
2344c577b8a1SJoseph Chan 
2345c577b8a1SJoseph Chan 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2346c577b8a1SJoseph Chan 	if (err < 0)
2347c577b8a1SJoseph Chan 		return err;
2348c577b8a1SJoseph Chan 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
23497f0df88cSTakashi Iwai 		return -EINVAL;
2350c577b8a1SJoseph Chan 
23514a79616dSTakashi Iwai 	err = via_auto_create_multi_out_ctls(codec);
2352c577b8a1SJoseph Chan 	if (err < 0)
2353c577b8a1SJoseph Chan 		return err;
23544a79616dSTakashi Iwai 	err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
2355c577b8a1SJoseph Chan 	if (err < 0)
2356c577b8a1SJoseph Chan 		return err;
23574a918ffeSTakashi Iwai 	err = via_auto_create_speaker_ctls(codec);
23584a918ffeSTakashi Iwai 	if (err < 0)
23594a918ffeSTakashi Iwai 		return err;
2360a86a88eaSTakashi Iwai 	err = via_auto_create_analog_input_ctls(codec);
2361c577b8a1SJoseph Chan 	if (err < 0)
2362c577b8a1SJoseph Chan 		return err;
2363c577b8a1SJoseph Chan 
2364c577b8a1SJoseph Chan 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2365c577b8a1SJoseph Chan 
236612daef65STakashi Iwai 	fill_dig_outs(codec);
236712daef65STakashi Iwai 	fill_dig_in(codec);
2368c577b8a1SJoseph Chan 
2369603c4019STakashi Iwai 	if (spec->kctls.list)
2370603c4019STakashi Iwai 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
2371c577b8a1SJoseph Chan 
2372c577b8a1SJoseph Chan 
23738df2a312STakashi Iwai 	if (spec->hp_dac_nid && spec->hp_dep_path.depth) {
2374ece8d043STakashi Iwai 		err = via_hp_build(codec);
2375ece8d043STakashi Iwai 		if (err < 0)
2376ece8d043STakashi Iwai 			return err;
2377ece8d043STakashi Iwai 	}
2378c577b8a1SJoseph Chan 
2379f4a7828bSTakashi Iwai 	err = via_smart51_build(codec);
2380f4a7828bSTakashi Iwai 	if (err < 0)
2381f4a7828bSTakashi Iwai 		return err;
2382f4a7828bSTakashi Iwai 
23835d41762aSTakashi Iwai 	/* assign slave outs */
23845d41762aSTakashi Iwai 	if (spec->slave_dig_outs[0])
23855d41762aSTakashi Iwai 		codec->slave_dig_outs = spec->slave_dig_outs;
23865d41762aSTakashi Iwai 
2387c577b8a1SJoseph Chan 	return 1;
2388c577b8a1SJoseph Chan }
2389c577b8a1SJoseph Chan 
23905d41762aSTakashi Iwai static void via_auto_init_dig_outs(struct hda_codec *codec)
2391c577b8a1SJoseph Chan {
239225eaba2fSLydia Wang 	struct via_spec *spec = codec->spec;
23935d41762aSTakashi Iwai 	if (spec->multiout.dig_out_nid)
23945d41762aSTakashi Iwai 		init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
23955d41762aSTakashi Iwai 	if (spec->slave_dig_outs[0])
23965d41762aSTakashi Iwai 		init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
23975d41762aSTakashi Iwai }
239825eaba2fSLydia Wang 
23995d41762aSTakashi Iwai static void via_auto_init_dig_in(struct hda_codec *codec)
24005d41762aSTakashi Iwai {
24015d41762aSTakashi Iwai 	struct via_spec *spec = codec->spec;
24025d41762aSTakashi Iwai 	if (!spec->dig_in_nid)
24035d41762aSTakashi Iwai 		return;
24045d41762aSTakashi Iwai 	snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
24055d41762aSTakashi Iwai 			    AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
24065d41762aSTakashi Iwai }
24075d41762aSTakashi Iwai 
24084a918ffeSTakashi Iwai /* initialize the unsolicited events */
24094a918ffeSTakashi Iwai static void via_auto_init_unsol_event(struct hda_codec *codec)
24104a918ffeSTakashi Iwai {
24114a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
24124a918ffeSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
24134a918ffeSTakashi Iwai 	unsigned int ev;
24144a918ffeSTakashi Iwai 	int i;
24154a918ffeSTakashi Iwai 
24164a918ffeSTakashi Iwai 	if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
24174a918ffeSTakashi Iwai 		snd_hda_codec_write(codec, cfg->hp_pins[0], 0,
24184a918ffeSTakashi Iwai 				AC_VERB_SET_UNSOLICITED_ENABLE,
24194a918ffeSTakashi Iwai 				AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT);
24204a918ffeSTakashi Iwai 
24214a918ffeSTakashi Iwai 	if (cfg->speaker_pins[0])
24224a918ffeSTakashi Iwai 		ev = VIA_LINE_EVENT;
24234a918ffeSTakashi Iwai 	else
24244a918ffeSTakashi Iwai 		ev = 0;
24254a918ffeSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
24264a918ffeSTakashi Iwai 		if (cfg->line_out_pins[i] &&
24274a918ffeSTakashi Iwai 		    is_jack_detectable(codec, cfg->line_out_pins[i]))
242863f10d2cSLydia Wang 			snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
24294a918ffeSTakashi Iwai 				AC_VERB_SET_UNSOLICITED_ENABLE,
24304a918ffeSTakashi Iwai 				AC_USRSP_EN | ev | VIA_JACK_EVENT);
24314a918ffeSTakashi Iwai 	}
24324a918ffeSTakashi Iwai 
24334a918ffeSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
24344a918ffeSTakashi Iwai 		if (is_jack_detectable(codec, cfg->inputs[i].pin))
24354a918ffeSTakashi Iwai 			snd_hda_codec_write(codec, cfg->inputs[i].pin, 0,
24364a918ffeSTakashi Iwai 				AC_VERB_SET_UNSOLICITED_ENABLE,
24374a918ffeSTakashi Iwai 				AC_USRSP_EN | VIA_JACK_EVENT);
24384a918ffeSTakashi Iwai 	}
24394a918ffeSTakashi Iwai }
24404a918ffeSTakashi Iwai 
24415d41762aSTakashi Iwai static int via_init(struct hda_codec *codec)
24425d41762aSTakashi Iwai {
24435d41762aSTakashi Iwai 	struct via_spec *spec = codec->spec;
24445d41762aSTakashi Iwai 	int i;
24455d41762aSTakashi Iwai 
24465d41762aSTakashi Iwai 	for (i = 0; i < spec->num_iverbs; i++)
24475d41762aSTakashi Iwai 		snd_hda_sequence_write(codec, spec->init_verbs[i]);
24485d41762aSTakashi Iwai 
2449c577b8a1SJoseph Chan 	via_auto_init_multi_out(codec);
2450c577b8a1SJoseph Chan 	via_auto_init_hp_out(codec);
24514a918ffeSTakashi Iwai 	via_auto_init_speaker_out(codec);
2452c577b8a1SJoseph Chan 	via_auto_init_analog_input(codec);
24535d41762aSTakashi Iwai 	via_auto_init_dig_outs(codec);
24545d41762aSTakashi Iwai 	via_auto_init_dig_in(codec);
245511890956SLydia Wang 
24564a918ffeSTakashi Iwai 	via_auto_init_unsol_event(codec);
24574a918ffeSTakashi Iwai 
245825eaba2fSLydia Wang 	via_hp_automute(codec);
24594a918ffeSTakashi Iwai 	via_line_automute(codec, false);
246025eaba2fSLydia Wang 
2461c577b8a1SJoseph Chan 	return 0;
2462c577b8a1SJoseph Chan }
2463c577b8a1SJoseph Chan 
24641f2e99feSLydia Wang static void vt1708_update_hp_jack_state(struct work_struct *work)
24651f2e99feSLydia Wang {
24661f2e99feSLydia Wang 	struct via_spec *spec = container_of(work, struct via_spec,
24671f2e99feSLydia Wang 					     vt1708_hp_work.work);
24681f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
24691f2e99feSLydia Wang 		return;
24701f2e99feSLydia Wang 	/* if jack state toggled */
24711f2e99feSLydia Wang 	if (spec->vt1708_hp_present
2472d56757abSTakashi Iwai 	    != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
24731f2e99feSLydia Wang 		spec->vt1708_hp_present ^= 1;
24741f2e99feSLydia Wang 		via_hp_automute(spec->codec);
24751f2e99feSLydia Wang 	}
24761f2e99feSLydia Wang 	vt1708_start_hp_work(spec);
24771f2e99feSLydia Wang }
24781f2e99feSLydia Wang 
2479337b9d02STakashi Iwai static int get_mux_nids(struct hda_codec *codec)
2480337b9d02STakashi Iwai {
2481337b9d02STakashi Iwai 	struct via_spec *spec = codec->spec;
2482337b9d02STakashi Iwai 	hda_nid_t nid, conn[8];
2483337b9d02STakashi Iwai 	unsigned int type;
2484337b9d02STakashi Iwai 	int i, n;
2485337b9d02STakashi Iwai 
2486337b9d02STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
2487337b9d02STakashi Iwai 		nid = spec->adc_nids[i];
2488337b9d02STakashi Iwai 		while (nid) {
2489a22d543aSTakashi Iwai 			type = get_wcaps_type(get_wcaps(codec, nid));
24901c55d521STakashi Iwai 			if (type == AC_WID_PIN)
24911c55d521STakashi Iwai 				break;
2492337b9d02STakashi Iwai 			n = snd_hda_get_connections(codec, nid, conn,
2493337b9d02STakashi Iwai 						    ARRAY_SIZE(conn));
2494337b9d02STakashi Iwai 			if (n <= 0)
2495337b9d02STakashi Iwai 				break;
2496337b9d02STakashi Iwai 			if (n > 1) {
2497337b9d02STakashi Iwai 				spec->mux_nids[i] = nid;
2498337b9d02STakashi Iwai 				break;
2499337b9d02STakashi Iwai 			}
2500337b9d02STakashi Iwai 			nid = conn[0];
2501337b9d02STakashi Iwai 		}
2502337b9d02STakashi Iwai 	}
25031c55d521STakashi Iwai 	return 0;
2504337b9d02STakashi Iwai }
2505337b9d02STakashi Iwai 
2506c577b8a1SJoseph Chan static int patch_vt1708(struct hda_codec *codec)
2507c577b8a1SJoseph Chan {
2508c577b8a1SJoseph Chan 	struct via_spec *spec;
2509c577b8a1SJoseph Chan 	int err;
2510c577b8a1SJoseph Chan 
2511c577b8a1SJoseph Chan 	/* create a codec specific record */
25125b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2513c577b8a1SJoseph Chan 	if (spec == NULL)
2514c577b8a1SJoseph Chan 		return -ENOMEM;
2515c577b8a1SJoseph Chan 
2516620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x17;
2517620e2b28STakashi Iwai 
251812daef65STakashi Iwai 	/* Add HP and CD pin config connect bit re-config action */
251912daef65STakashi Iwai 	vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
252012daef65STakashi Iwai 	vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
252112daef65STakashi Iwai 
2522c577b8a1SJoseph Chan 	/* automatic parse from the BIOS config */
252312daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2524c577b8a1SJoseph Chan 	if (err < 0) {
2525c577b8a1SJoseph Chan 		via_free(codec);
2526c577b8a1SJoseph Chan 		return err;
2527c577b8a1SJoseph Chan 	}
2528c577b8a1SJoseph Chan 
252912daef65STakashi Iwai 	/* add jack detect on/off control */
253012daef65STakashi Iwai 	if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
253112daef65STakashi Iwai 		return -ENOMEM;
253212daef65STakashi Iwai 
2533bc9b5623STakashi Iwai 	/* disable 32bit format on VT1708 */
2534bc9b5623STakashi Iwai 	if (codec->vendor_id == 0x11061708)
2535bc9b5623STakashi Iwai 		spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
2536c577b8a1SJoseph Chan 
2537e322a36dSLydia Wang 	spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
2538e322a36dSLydia Wang 
2539c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
2540c577b8a1SJoseph Chan 
25411f2e99feSLydia Wang 	INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
2542c577b8a1SJoseph Chan 	return 0;
2543c577b8a1SJoseph Chan }
2544c577b8a1SJoseph Chan 
2545ddd304d8STakashi Iwai static int patch_vt1709(struct hda_codec *codec)
2546c577b8a1SJoseph Chan {
2547c577b8a1SJoseph Chan 	struct via_spec *spec;
2548c577b8a1SJoseph Chan 	int err;
2549c577b8a1SJoseph Chan 
2550c577b8a1SJoseph Chan 	/* create a codec specific record */
25515b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2552c577b8a1SJoseph Chan 	if (spec == NULL)
2553c577b8a1SJoseph Chan 		return -ENOMEM;
2554c577b8a1SJoseph Chan 
2555620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x18;
2556620e2b28STakashi Iwai 
255712daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2558c577b8a1SJoseph Chan 	if (err < 0) {
2559c577b8a1SJoseph Chan 		via_free(codec);
2560c577b8a1SJoseph Chan 		return err;
2561c577b8a1SJoseph Chan 	}
2562c577b8a1SJoseph Chan 
2563c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
2564c577b8a1SJoseph Chan 
2565f7278fd0SJosepch Chan 	return 0;
2566f7278fd0SJosepch Chan }
2567f7278fd0SJosepch Chan 
25683e95b9abSLydia Wang static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
25693e95b9abSLydia Wang {
25703e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
25713e95b9abSLydia Wang 	int imux_is_smixer;
25723e95b9abSLydia Wang 	unsigned int parm;
25733e95b9abSLydia Wang 	int is_8ch = 0;
2574bc92df7fSLydia Wang 	if ((spec->codec_type != VT1708B_4CH) &&
2575bc92df7fSLydia Wang 	    (codec->vendor_id != 0x11064397))
25763e95b9abSLydia Wang 		is_8ch = 1;
25773e95b9abSLydia Wang 
25783e95b9abSLydia Wang 	/* SW0 (17h) = stereo mixer */
25793e95b9abSLydia Wang 	imux_is_smixer =
25803e95b9abSLydia Wang 	(snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
25813e95b9abSLydia Wang 	 == ((spec->codec_type == VT1708S) ? 5 : 0));
25823e95b9abSLydia Wang 	/* inputs */
25833e95b9abSLydia Wang 	/* PW 1/2/5 (1ah/1bh/1eh) */
25843e95b9abSLydia Wang 	parm = AC_PWRST_D3;
25853e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1a, &parm);
25863e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1b, &parm);
25873e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
25883e95b9abSLydia Wang 	if (imux_is_smixer)
25893e95b9abSLydia Wang 		parm = AC_PWRST_D0;
25903e95b9abSLydia Wang 	/* SW0 (17h), AIW 0/1 (13h/14h) */
25913e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
25923e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
25933e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
25943e95b9abSLydia Wang 
25953e95b9abSLydia Wang 	/* outputs */
25963e95b9abSLydia Wang 	/* PW0 (19h), SW1 (18h), AOW1 (11h) */
25973e95b9abSLydia Wang 	parm = AC_PWRST_D3;
25983e95b9abSLydia Wang 	set_pin_power_state(codec, 0x19, &parm);
25993e95b9abSLydia Wang 	if (spec->smart51_enabled)
26003e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1b, &parm);
26013e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
26023e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
26033e95b9abSLydia Wang 
26043e95b9abSLydia Wang 	/* PW6 (22h), SW2 (26h), AOW2 (24h) */
26053e95b9abSLydia Wang 	if (is_8ch) {
26063e95b9abSLydia Wang 		parm = AC_PWRST_D3;
26073e95b9abSLydia Wang 		set_pin_power_state(codec, 0x22, &parm);
26083e95b9abSLydia Wang 		if (spec->smart51_enabled)
26093e95b9abSLydia Wang 			set_pin_power_state(codec, 0x1a, &parm);
26103e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x26, 0,
26113e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
26123e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x24, 0,
26133e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
2614bc92df7fSLydia Wang 	} else if (codec->vendor_id == 0x11064397) {
2615bc92df7fSLydia Wang 		/* PW7(23h), SW2(27h), AOW2(25h) */
2616bc92df7fSLydia Wang 		parm = AC_PWRST_D3;
2617bc92df7fSLydia Wang 		set_pin_power_state(codec, 0x23, &parm);
2618bc92df7fSLydia Wang 		if (spec->smart51_enabled)
2619bc92df7fSLydia Wang 			set_pin_power_state(codec, 0x1a, &parm);
2620bc92df7fSLydia Wang 		snd_hda_codec_write(codec, 0x27, 0,
2621bc92df7fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
2622bc92df7fSLydia Wang 		snd_hda_codec_write(codec, 0x25, 0,
2623bc92df7fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
26243e95b9abSLydia Wang 	}
26253e95b9abSLydia Wang 
26263e95b9abSLydia Wang 	/* PW 3/4/7 (1ch/1dh/23h) */
26273e95b9abSLydia Wang 	parm = AC_PWRST_D3;
26283e95b9abSLydia Wang 	/* force to D0 for internal Speaker */
26293e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1c, &parm);
26303e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1d, &parm);
26313e95b9abSLydia Wang 	if (is_8ch)
26323e95b9abSLydia Wang 		set_pin_power_state(codec, 0x23, &parm);
26333e95b9abSLydia Wang 
26343e95b9abSLydia Wang 	/* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
26353e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
26363e95b9abSLydia Wang 			    imux_is_smixer ? AC_PWRST_D0 : parm);
26373e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
26383e95b9abSLydia Wang 	if (is_8ch) {
26393e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x25, 0,
26403e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
26413e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x27, 0,
26423e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
2643bc92df7fSLydia Wang 	} else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
2644bc92df7fSLydia Wang 		snd_hda_codec_write(codec, 0x25, 0,
2645bc92df7fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
26463e95b9abSLydia Wang }
26473e95b9abSLydia Wang 
2648518bf3baSLydia Wang static int patch_vt1708S(struct hda_codec *codec);
2649ddd304d8STakashi Iwai static int patch_vt1708B(struct hda_codec *codec)
2650f7278fd0SJosepch Chan {
2651f7278fd0SJosepch Chan 	struct via_spec *spec;
2652f7278fd0SJosepch Chan 	int err;
2653f7278fd0SJosepch Chan 
2654518bf3baSLydia Wang 	if (get_codec_type(codec) == VT1708BCE)
2655518bf3baSLydia Wang 		return patch_vt1708S(codec);
2656ddd304d8STakashi Iwai 
2657f7278fd0SJosepch Chan 	/* create a codec specific record */
26585b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2659f7278fd0SJosepch Chan 	if (spec == NULL)
2660f7278fd0SJosepch Chan 		return -ENOMEM;
2661f7278fd0SJosepch Chan 
2662620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
2663620e2b28STakashi Iwai 
2664f7278fd0SJosepch Chan 	/* automatic parse from the BIOS config */
266512daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2666f7278fd0SJosepch Chan 	if (err < 0) {
2667f7278fd0SJosepch Chan 		via_free(codec);
2668f7278fd0SJosepch Chan 		return err;
2669f7278fd0SJosepch Chan 	}
2670f7278fd0SJosepch Chan 
2671f7278fd0SJosepch Chan 	codec->patch_ops = via_patch_ops;
2672f7278fd0SJosepch Chan 
26733e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
26743e95b9abSLydia Wang 
2675f7278fd0SJosepch Chan 	return 0;
2676f7278fd0SJosepch Chan }
2677f7278fd0SJosepch Chan 
2678d949cac1SHarald Welte /* Patch for VT1708S */
2679096a8854STakashi Iwai static const struct hda_verb vt1708S_init_verbs[] = {
2680d7426329SHarald Welte 	/* Enable Mic Boost Volume backdoor */
2681d7426329SHarald Welte 	{0x1, 0xf98, 0x1},
2682bc7e7e5cSLydia Wang 	/* don't bybass mixer */
2683bc7e7e5cSLydia Wang 	{0x1, 0xf88, 0xc0},
2684d949cac1SHarald Welte 	{ }
2685d949cac1SHarald Welte };
2686d949cac1SHarald Welte 
26879da29271STakashi Iwai /* fill out digital output widgets; one for master and one for slave outputs */
26889da29271STakashi Iwai static void fill_dig_outs(struct hda_codec *codec)
26899da29271STakashi Iwai {
26909da29271STakashi Iwai 	struct via_spec *spec = codec->spec;
26919da29271STakashi Iwai 	int i;
26929da29271STakashi Iwai 
26939da29271STakashi Iwai 	for (i = 0; i < spec->autocfg.dig_outs; i++) {
26949da29271STakashi Iwai 		hda_nid_t nid;
26959da29271STakashi Iwai 		int conn;
26969da29271STakashi Iwai 
26979da29271STakashi Iwai 		nid = spec->autocfg.dig_out_pins[i];
26989da29271STakashi Iwai 		if (!nid)
26999da29271STakashi Iwai 			continue;
27009da29271STakashi Iwai 		conn = snd_hda_get_connections(codec, nid, &nid, 1);
27019da29271STakashi Iwai 		if (conn < 1)
27029da29271STakashi Iwai 			continue;
27039da29271STakashi Iwai 		if (!spec->multiout.dig_out_nid)
27049da29271STakashi Iwai 			spec->multiout.dig_out_nid = nid;
27059da29271STakashi Iwai 		else {
27069da29271STakashi Iwai 			spec->slave_dig_outs[0] = nid;
27079da29271STakashi Iwai 			break; /* at most two dig outs */
27089da29271STakashi Iwai 		}
27099da29271STakashi Iwai 	}
27109da29271STakashi Iwai }
27119da29271STakashi Iwai 
271212daef65STakashi Iwai static void fill_dig_in(struct hda_codec *codec)
2713d949cac1SHarald Welte {
2714d949cac1SHarald Welte 	struct via_spec *spec = codec->spec;
271512daef65STakashi Iwai 	hda_nid_t dig_nid;
271612daef65STakashi Iwai 	int i, err;
2717d949cac1SHarald Welte 
271812daef65STakashi Iwai 	if (!spec->autocfg.dig_in_pin)
271912daef65STakashi Iwai 		return;
2720d949cac1SHarald Welte 
272112daef65STakashi Iwai 	dig_nid = codec->start_nid;
272212daef65STakashi Iwai 	for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
272312daef65STakashi Iwai 		unsigned int wcaps = get_wcaps(codec, dig_nid);
272412daef65STakashi Iwai 		if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
272512daef65STakashi Iwai 			continue;
272612daef65STakashi Iwai 		if (!(wcaps & AC_WCAP_DIGITAL))
272712daef65STakashi Iwai 			continue;
272812daef65STakashi Iwai 		if (!(wcaps & AC_WCAP_CONN_LIST))
272912daef65STakashi Iwai 			continue;
273012daef65STakashi Iwai 		err = get_connection_index(codec, dig_nid,
273112daef65STakashi Iwai 					   spec->autocfg.dig_in_pin);
273212daef65STakashi Iwai 		if (err >= 0) {
273312daef65STakashi Iwai 			spec->dig_in_nid = dig_nid;
273412daef65STakashi Iwai 			break;
273512daef65STakashi Iwai 		}
273612daef65STakashi Iwai 	}
2737d949cac1SHarald Welte }
2738d949cac1SHarald Welte 
27396369bcfcSLydia Wang static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
27406369bcfcSLydia Wang 			       int offset, int num_steps, int step_size)
27416369bcfcSLydia Wang {
27426369bcfcSLydia Wang 	snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
27436369bcfcSLydia Wang 				  (offset << AC_AMPCAP_OFFSET_SHIFT) |
27446369bcfcSLydia Wang 				  (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
27456369bcfcSLydia Wang 				  (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
27466369bcfcSLydia Wang 				  (0 << AC_AMPCAP_MUTE_SHIFT));
27476369bcfcSLydia Wang }
27486369bcfcSLydia Wang 
2749d949cac1SHarald Welte static int patch_vt1708S(struct hda_codec *codec)
2750d949cac1SHarald Welte {
2751d949cac1SHarald Welte 	struct via_spec *spec;
2752d949cac1SHarald Welte 	int err;
2753d949cac1SHarald Welte 
2754d949cac1SHarald Welte 	/* create a codec specific record */
27555b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2756d949cac1SHarald Welte 	if (spec == NULL)
2757d949cac1SHarald Welte 		return -ENOMEM;
2758d949cac1SHarald Welte 
2759620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
2760d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1a, 0, 3, 40);
2761d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1e, 0, 3, 40);
2762620e2b28STakashi Iwai 
2763d949cac1SHarald Welte 	/* automatic parse from the BIOS config */
276412daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2765d949cac1SHarald Welte 	if (err < 0) {
2766d949cac1SHarald Welte 		via_free(codec);
2767d949cac1SHarald Welte 		return err;
2768d949cac1SHarald Welte 	}
2769d949cac1SHarald Welte 
2770096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
2771d949cac1SHarald Welte 
2772d949cac1SHarald Welte 	codec->patch_ops = via_patch_ops;
2773d949cac1SHarald Welte 
2774518bf3baSLydia Wang 	/* correct names for VT1708BCE */
2775518bf3baSLydia Wang 	if (get_codec_type(codec) == VT1708BCE)	{
2776518bf3baSLydia Wang 		kfree(codec->chip_name);
2777518bf3baSLydia Wang 		codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
2778518bf3baSLydia Wang 		snprintf(codec->bus->card->mixername,
2779518bf3baSLydia Wang 			 sizeof(codec->bus->card->mixername),
2780518bf3baSLydia Wang 			 "%s %s", codec->vendor_name, codec->chip_name);
2781970f630fSLydia Wang 	}
2782bc92df7fSLydia Wang 	/* correct names for VT1705 */
2783bc92df7fSLydia Wang 	if (codec->vendor_id == 0x11064397)	{
2784bc92df7fSLydia Wang 		kfree(codec->chip_name);
2785bc92df7fSLydia Wang 		codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
2786bc92df7fSLydia Wang 		snprintf(codec->bus->card->mixername,
2787bc92df7fSLydia Wang 			 sizeof(codec->bus->card->mixername),
2788bc92df7fSLydia Wang 			 "%s %s", codec->vendor_name, codec->chip_name);
2789bc92df7fSLydia Wang 	}
27903e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
2791d949cac1SHarald Welte 	return 0;
2792d949cac1SHarald Welte }
2793d949cac1SHarald Welte 
2794d949cac1SHarald Welte /* Patch for VT1702 */
2795d949cac1SHarald Welte 
2796096a8854STakashi Iwai static const struct hda_verb vt1702_init_verbs[] = {
2797bc7e7e5cSLydia Wang 	/* mixer enable */
2798bc7e7e5cSLydia Wang 	{0x1, 0xF88, 0x3},
2799bc7e7e5cSLydia Wang 	/* GPIO 0~2 */
2800bc7e7e5cSLydia Wang 	{0x1, 0xF82, 0x3F},
2801d949cac1SHarald Welte 	{ }
2802d949cac1SHarald Welte };
2803d949cac1SHarald Welte 
28043e95b9abSLydia Wang static void set_widgets_power_state_vt1702(struct hda_codec *codec)
28053e95b9abSLydia Wang {
28063e95b9abSLydia Wang 	int imux_is_smixer =
28073e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
28083e95b9abSLydia Wang 	unsigned int parm;
28093e95b9abSLydia Wang 	/* inputs */
28103e95b9abSLydia Wang 	/* PW 1/2/5 (14h/15h/18h) */
28113e95b9abSLydia Wang 	parm = AC_PWRST_D3;
28123e95b9abSLydia Wang 	set_pin_power_state(codec, 0x14, &parm);
28133e95b9abSLydia Wang 	set_pin_power_state(codec, 0x15, &parm);
28143e95b9abSLydia Wang 	set_pin_power_state(codec, 0x18, &parm);
28153e95b9abSLydia Wang 	if (imux_is_smixer)
28163e95b9abSLydia Wang 		parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
28173e95b9abSLydia Wang 	/* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
28183e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
28193e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x12, 0, AC_VERB_SET_POWER_STATE, parm);
28203e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
28213e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_POWER_STATE, parm);
28223e95b9abSLydia Wang 
28233e95b9abSLydia Wang 	/* outputs */
28243e95b9abSLydia Wang 	/* PW 3/4 (16h/17h) */
28253e95b9abSLydia Wang 	parm = AC_PWRST_D3;
28263e95b9abSLydia Wang 	set_pin_power_state(codec, 0x17, &parm);
28273e95b9abSLydia Wang 	set_pin_power_state(codec, 0x16, &parm);
28283e95b9abSLydia Wang 	/* MW0 (1ah), AOW 0/1 (10h/1dh) */
28293e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
28303e95b9abSLydia Wang 			    imux_is_smixer ? AC_PWRST_D0 : parm);
28313e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
28323e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
28333e95b9abSLydia Wang }
28343e95b9abSLydia Wang 
2835d949cac1SHarald Welte static int patch_vt1702(struct hda_codec *codec)
2836d949cac1SHarald Welte {
2837d949cac1SHarald Welte 	struct via_spec *spec;
2838d949cac1SHarald Welte 	int err;
2839d949cac1SHarald Welte 
2840d949cac1SHarald Welte 	/* create a codec specific record */
28415b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2842d949cac1SHarald Welte 	if (spec == NULL)
2843d949cac1SHarald Welte 		return -ENOMEM;
2844d949cac1SHarald Welte 
2845620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x1a;
2846620e2b28STakashi Iwai 
284712daef65STakashi Iwai 	/* limit AA path volume to 0 dB */
284812daef65STakashi Iwai 	snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
284912daef65STakashi Iwai 				  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
285012daef65STakashi Iwai 				  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
285112daef65STakashi Iwai 				  (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
285212daef65STakashi Iwai 				  (1 << AC_AMPCAP_MUTE_SHIFT));
285312daef65STakashi Iwai 
2854d949cac1SHarald Welte 	/* automatic parse from the BIOS config */
285512daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2856d949cac1SHarald Welte 	if (err < 0) {
2857d949cac1SHarald Welte 		via_free(codec);
2858d949cac1SHarald Welte 		return err;
2859d949cac1SHarald Welte 	}
2860d949cac1SHarald Welte 
2861096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
2862d949cac1SHarald Welte 
2863d949cac1SHarald Welte 	codec->patch_ops = via_patch_ops;
2864d949cac1SHarald Welte 
28653e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1702;
2866d949cac1SHarald Welte 	return 0;
2867d949cac1SHarald Welte }
2868d949cac1SHarald Welte 
2869eb7188caSLydia Wang /* Patch for VT1718S */
2870eb7188caSLydia Wang 
2871096a8854STakashi Iwai static const struct hda_verb vt1718S_init_verbs[] = {
28724ab2d53aSLydia Wang 	/* Enable MW0 adjust Gain 5 */
28734ab2d53aSLydia Wang 	{0x1, 0xfb2, 0x10},
2874eb7188caSLydia Wang 	/* Enable Boost Volume backdoor */
2875eb7188caSLydia Wang 	{0x1, 0xf88, 0x8},
28765d41762aSTakashi Iwai 
2877eb7188caSLydia Wang 	{ }
2878eb7188caSLydia Wang };
2879eb7188caSLydia Wang 
28803e95b9abSLydia Wang static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
28813e95b9abSLydia Wang {
28823e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
28833e95b9abSLydia Wang 	int imux_is_smixer;
28843e95b9abSLydia Wang 	unsigned int parm;
28853e95b9abSLydia Wang 	/* MUX6 (1eh) = stereo mixer */
28863e95b9abSLydia Wang 	imux_is_smixer =
28873e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
28883e95b9abSLydia Wang 	/* inputs */
28893e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
28903e95b9abSLydia Wang 	parm = AC_PWRST_D3;
28913e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
28923e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
28933e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
28943e95b9abSLydia Wang 	if (imux_is_smixer)
28953e95b9abSLydia Wang 		parm = AC_PWRST_D0;
28963e95b9abSLydia Wang 	/* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
28973e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
28983e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
28993e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
29003e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
29013e95b9abSLydia Wang 
29023e95b9abSLydia Wang 	/* outputs */
29033e95b9abSLydia Wang 	/* PW3 (27h), MW2 (1ah), AOW3 (bh) */
29043e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29053e95b9abSLydia Wang 	set_pin_power_state(codec, 0x27, &parm);
29063e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE, parm);
29073e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0xb, 0, AC_VERB_SET_POWER_STATE, parm);
29083e95b9abSLydia Wang 
29093e95b9abSLydia Wang 	/* PW2 (26h), AOW2 (ah) */
29103e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29113e95b9abSLydia Wang 	set_pin_power_state(codec, 0x26, &parm);
29123e95b9abSLydia Wang 	if (spec->smart51_enabled)
29133e95b9abSLydia Wang 		set_pin_power_state(codec, 0x2b, &parm);
29143e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0xa, 0, AC_VERB_SET_POWER_STATE, parm);
29153e95b9abSLydia Wang 
29163e95b9abSLydia Wang 	/* PW0 (24h), AOW0 (8h) */
29173e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29183e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
29193e95b9abSLydia Wang 	if (!spec->hp_independent_mode) /* check for redirected HP */
29203e95b9abSLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
29213e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
29223e95b9abSLydia Wang 	/* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
29233e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x21, 0, AC_VERB_SET_POWER_STATE,
29243e95b9abSLydia Wang 			    imux_is_smixer ? AC_PWRST_D0 : parm);
29253e95b9abSLydia Wang 
29263e95b9abSLydia Wang 	/* PW1 (25h), AOW1 (9h) */
29273e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29283e95b9abSLydia Wang 	set_pin_power_state(codec, 0x25, &parm);
29293e95b9abSLydia Wang 	if (spec->smart51_enabled)
29303e95b9abSLydia Wang 		set_pin_power_state(codec, 0x2a, &parm);
29313e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x9, 0, AC_VERB_SET_POWER_STATE, parm);
29323e95b9abSLydia Wang 
29333e95b9abSLydia Wang 	if (spec->hp_independent_mode) {
29343e95b9abSLydia Wang 		/* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
29353e95b9abSLydia Wang 		parm = AC_PWRST_D3;
29363e95b9abSLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
29373e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x1b, 0,
29383e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
29393e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x34, 0,
29403e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
29413e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0xc, 0,
29423e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
29433e95b9abSLydia Wang 	}
29443e95b9abSLydia Wang }
29453e95b9abSLydia Wang 
2946eb7188caSLydia Wang static int patch_vt1718S(struct hda_codec *codec)
2947eb7188caSLydia Wang {
2948eb7188caSLydia Wang 	struct via_spec *spec;
2949eb7188caSLydia Wang 	int err;
2950eb7188caSLydia Wang 
2951eb7188caSLydia Wang 	/* create a codec specific record */
29525b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2953eb7188caSLydia Wang 	if (spec == NULL)
2954eb7188caSLydia Wang 		return -ENOMEM;
2955eb7188caSLydia Wang 
2956620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
2957d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
2958d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
2959620e2b28STakashi Iwai 
2960eb7188caSLydia Wang 	/* automatic parse from the BIOS config */
296112daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2962eb7188caSLydia Wang 	if (err < 0) {
2963eb7188caSLydia Wang 		via_free(codec);
2964eb7188caSLydia Wang 		return err;
2965eb7188caSLydia Wang 	}
2966eb7188caSLydia Wang 
2967096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
2968eb7188caSLydia Wang 
2969eb7188caSLydia Wang 	codec->patch_ops = via_patch_ops;
2970eb7188caSLydia Wang 
29713e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1718S;
29723e95b9abSLydia Wang 
2973eb7188caSLydia Wang 	return 0;
2974eb7188caSLydia Wang }
2975f3db423dSLydia Wang 
2976f3db423dSLydia Wang /* Patch for VT1716S */
2977f3db423dSLydia Wang 
2978f3db423dSLydia Wang static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
2979f3db423dSLydia Wang 			    struct snd_ctl_elem_info *uinfo)
2980f3db423dSLydia Wang {
2981f3db423dSLydia Wang 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2982f3db423dSLydia Wang 	uinfo->count = 1;
2983f3db423dSLydia Wang 	uinfo->value.integer.min = 0;
2984f3db423dSLydia Wang 	uinfo->value.integer.max = 1;
2985f3db423dSLydia Wang 	return 0;
2986f3db423dSLydia Wang }
2987f3db423dSLydia Wang 
2988f3db423dSLydia Wang static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
2989f3db423dSLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
2990f3db423dSLydia Wang {
2991f3db423dSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2992f3db423dSLydia Wang 	int index = 0;
2993f3db423dSLydia Wang 
2994f3db423dSLydia Wang 	index = snd_hda_codec_read(codec, 0x26, 0,
2995f3db423dSLydia Wang 					       AC_VERB_GET_CONNECT_SEL, 0);
2996f3db423dSLydia Wang 	if (index != -1)
2997f3db423dSLydia Wang 		*ucontrol->value.integer.value = index;
2998f3db423dSLydia Wang 
2999f3db423dSLydia Wang 	return 0;
3000f3db423dSLydia Wang }
3001f3db423dSLydia Wang 
3002f3db423dSLydia Wang static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
3003f3db423dSLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
3004f3db423dSLydia Wang {
3005f3db423dSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3006f3db423dSLydia Wang 	struct via_spec *spec = codec->spec;
3007f3db423dSLydia Wang 	int index = *ucontrol->value.integer.value;
3008f3db423dSLydia Wang 
3009f3db423dSLydia Wang 	snd_hda_codec_write(codec, 0x26, 0,
3010f3db423dSLydia Wang 					       AC_VERB_SET_CONNECT_SEL, index);
3011f3db423dSLydia Wang 	spec->dmic_enabled = index;
30123e95b9abSLydia Wang 	set_widgets_power_state(codec);
3013f3db423dSLydia Wang 	return 1;
3014f3db423dSLydia Wang }
3015f3db423dSLydia Wang 
301690dd48a1STakashi Iwai static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
3017f3db423dSLydia Wang 	HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
3018f3db423dSLydia Wang 	{
3019f3db423dSLydia Wang 	 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3020f3db423dSLydia Wang 	 .name = "Digital Mic Capture Switch",
30215b0cb1d8SJaroslav Kysela 	 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
3022f3db423dSLydia Wang 	 .count = 1,
3023f3db423dSLydia Wang 	 .info = vt1716s_dmic_info,
3024f3db423dSLydia Wang 	 .get = vt1716s_dmic_get,
3025f3db423dSLydia Wang 	 .put = vt1716s_dmic_put,
3026f3db423dSLydia Wang 	 },
3027f3db423dSLydia Wang 	{}			/* end */
3028f3db423dSLydia Wang };
3029f3db423dSLydia Wang 
3030f3db423dSLydia Wang 
3031f3db423dSLydia Wang /* mono-out mixer elements */
303290dd48a1STakashi Iwai static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
3033f3db423dSLydia Wang 	HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
3034f3db423dSLydia Wang 	{ } /* end */
3035f3db423dSLydia Wang };
3036f3db423dSLydia Wang 
3037096a8854STakashi Iwai static const struct hda_verb vt1716S_init_verbs[] = {
3038f3db423dSLydia Wang 	/* Enable Boost Volume backdoor */
3039f3db423dSLydia Wang 	{0x1, 0xf8a, 0x80},
3040f3db423dSLydia Wang 	/* don't bybass mixer */
3041f3db423dSLydia Wang 	{0x1, 0xf88, 0xc0},
3042f3db423dSLydia Wang 	/* Enable mono output */
3043f3db423dSLydia Wang 	{0x1, 0xf90, 0x08},
3044f3db423dSLydia Wang 	{ }
3045f3db423dSLydia Wang };
3046f3db423dSLydia Wang 
30473e95b9abSLydia Wang static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
30483e95b9abSLydia Wang {
30493e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
30503e95b9abSLydia Wang 	int imux_is_smixer;
30513e95b9abSLydia Wang 	unsigned int parm;
30523e95b9abSLydia Wang 	unsigned int mono_out, present;
30533e95b9abSLydia Wang 	/* SW0 (17h) = stereo mixer */
30543e95b9abSLydia Wang 	imux_is_smixer =
30553e95b9abSLydia Wang 	(snd_hda_codec_read(codec, 0x17, 0,
30563e95b9abSLydia Wang 			    AC_VERB_GET_CONNECT_SEL, 0x00) ==  5);
30573e95b9abSLydia Wang 	/* inputs */
30583e95b9abSLydia Wang 	/* PW 1/2/5 (1ah/1bh/1eh) */
30593e95b9abSLydia Wang 	parm = AC_PWRST_D3;
30603e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1a, &parm);
30613e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1b, &parm);
30623e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
30633e95b9abSLydia Wang 	if (imux_is_smixer)
30643e95b9abSLydia Wang 		parm = AC_PWRST_D0;
30653e95b9abSLydia Wang 	/* SW0 (17h), AIW0(13h) */
30663e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
30673e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
30683e95b9abSLydia Wang 
30693e95b9abSLydia Wang 	parm = AC_PWRST_D3;
30703e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
30713e95b9abSLydia Wang 	/* PW11 (22h) */
30723e95b9abSLydia Wang 	if (spec->dmic_enabled)
30733e95b9abSLydia Wang 		set_pin_power_state(codec, 0x22, &parm);
30743e95b9abSLydia Wang 	else
30753e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x22, 0,
30763e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
30773e95b9abSLydia Wang 
30783e95b9abSLydia Wang 	/* SW2(26h), AIW1(14h) */
30793e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x26, 0, AC_VERB_SET_POWER_STATE, parm);
30803e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
30813e95b9abSLydia Wang 
30823e95b9abSLydia Wang 	/* outputs */
30833e95b9abSLydia Wang 	/* PW0 (19h), SW1 (18h), AOW1 (11h) */
30843e95b9abSLydia Wang 	parm = AC_PWRST_D3;
30853e95b9abSLydia Wang 	set_pin_power_state(codec, 0x19, &parm);
30863e95b9abSLydia Wang 	/* Smart 5.1 PW2(1bh) */
30873e95b9abSLydia Wang 	if (spec->smart51_enabled)
30883e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1b, &parm);
30893e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
30903e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
30913e95b9abSLydia Wang 
30923e95b9abSLydia Wang 	/* PW7 (23h), SW3 (27h), AOW3 (25h) */
30933e95b9abSLydia Wang 	parm = AC_PWRST_D3;
30943e95b9abSLydia Wang 	set_pin_power_state(codec, 0x23, &parm);
30953e95b9abSLydia Wang 	/* Smart 5.1 PW1(1ah) */
30963e95b9abSLydia Wang 	if (spec->smart51_enabled)
30973e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1a, &parm);
30983e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x27, 0, AC_VERB_SET_POWER_STATE, parm);
30993e95b9abSLydia Wang 
31003e95b9abSLydia Wang 	/* Smart 5.1 PW5(1eh) */
31013e95b9abSLydia Wang 	if (spec->smart51_enabled)
31023e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1e, &parm);
31033e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x25, 0, AC_VERB_SET_POWER_STATE, parm);
31043e95b9abSLydia Wang 
31053e95b9abSLydia Wang 	/* Mono out */
31063e95b9abSLydia Wang 	/* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
31073e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x1c);
31083e95b9abSLydia Wang 
31093e95b9abSLydia Wang 	if (present)
31103e95b9abSLydia Wang 		mono_out = 0;
31113e95b9abSLydia Wang 	else {
31123e95b9abSLydia Wang 		present = snd_hda_jack_detect(codec, 0x1d);
31133e95b9abSLydia Wang 		if (!spec->hp_independent_mode && present)
31143e95b9abSLydia Wang 			mono_out = 0;
31153e95b9abSLydia Wang 		else
31163e95b9abSLydia Wang 			mono_out = 1;
31173e95b9abSLydia Wang 	}
31183e95b9abSLydia Wang 	parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
31193e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x28, 0, AC_VERB_SET_POWER_STATE, parm);
31203e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x29, 0, AC_VERB_SET_POWER_STATE, parm);
31213e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x2a, 0, AC_VERB_SET_POWER_STATE, parm);
31223e95b9abSLydia Wang 
31233e95b9abSLydia Wang 	/* PW 3/4 (1ch/1dh) */
31243e95b9abSLydia Wang 	parm = AC_PWRST_D3;
31253e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1c, &parm);
31263e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1d, &parm);
31273e95b9abSLydia Wang 	/* HP Independent Mode, power on AOW3 */
31283e95b9abSLydia Wang 	if (spec->hp_independent_mode)
31293e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x25, 0,
31303e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
31313e95b9abSLydia Wang 
31323e95b9abSLydia Wang 	/* force to D0 for internal Speaker */
31333e95b9abSLydia Wang 	/* MW0 (16h), AOW0 (10h) */
31343e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
31353e95b9abSLydia Wang 			    imux_is_smixer ? AC_PWRST_D0 : parm);
31363e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
31373e95b9abSLydia Wang 			    mono_out ? AC_PWRST_D0 : parm);
31383e95b9abSLydia Wang }
31393e95b9abSLydia Wang 
3140f3db423dSLydia Wang static int patch_vt1716S(struct hda_codec *codec)
3141f3db423dSLydia Wang {
3142f3db423dSLydia Wang 	struct via_spec *spec;
3143f3db423dSLydia Wang 	int err;
3144f3db423dSLydia Wang 
3145f3db423dSLydia Wang 	/* create a codec specific record */
31465b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3147f3db423dSLydia Wang 	if (spec == NULL)
3148f3db423dSLydia Wang 		return -ENOMEM;
3149f3db423dSLydia Wang 
3150620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
3151d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1a, 0, 3, 40);
3152d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1e, 0, 3, 40);
3153620e2b28STakashi Iwai 
3154f3db423dSLydia Wang 	/* automatic parse from the BIOS config */
315512daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3156f3db423dSLydia Wang 	if (err < 0) {
3157f3db423dSLydia Wang 		via_free(codec);
3158f3db423dSLydia Wang 		return err;
3159f3db423dSLydia Wang 	}
3160f3db423dSLydia Wang 
3161096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++]  = vt1716S_init_verbs;
3162f3db423dSLydia Wang 
3163f3db423dSLydia Wang 	spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
3164f3db423dSLydia Wang 	spec->num_mixers++;
3165f3db423dSLydia Wang 
3166f3db423dSLydia Wang 	spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
3167f3db423dSLydia Wang 
3168f3db423dSLydia Wang 	codec->patch_ops = via_patch_ops;
3169f3db423dSLydia Wang 
31703e95b9abSLydia Wang 	spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
3171f3db423dSLydia Wang 	return 0;
3172f3db423dSLydia Wang }
317325eaba2fSLydia Wang 
317425eaba2fSLydia Wang /* for vt2002P */
317525eaba2fSLydia Wang 
3176096a8854STakashi Iwai static const struct hda_verb vt2002P_init_verbs[] = {
3177eadb9a80SLydia Wang 	/* Class-D speaker related verbs */
3178eadb9a80SLydia Wang 	{0x1, 0xfe0, 0x4},
3179eadb9a80SLydia Wang 	{0x1, 0xfe9, 0x80},
3180eadb9a80SLydia Wang 	{0x1, 0xfe2, 0x22},
318125eaba2fSLydia Wang 	/* Enable Boost Volume backdoor */
318225eaba2fSLydia Wang 	{0x1, 0xfb9, 0x24},
318325eaba2fSLydia Wang 	/* Enable AOW0 to MW9 */
318425eaba2fSLydia Wang 	{0x1, 0xfb8, 0x88},
318525eaba2fSLydia Wang 	{ }
318625eaba2fSLydia Wang };
31874a918ffeSTakashi Iwai 
3188096a8854STakashi Iwai static const struct hda_verb vt1802_init_verbs[] = {
318911890956SLydia Wang 	/* Enable Boost Volume backdoor */
319011890956SLydia Wang 	{0x1, 0xfb9, 0x24},
319111890956SLydia Wang 	/* Enable AOW0 to MW9 */
319211890956SLydia Wang 	{0x1, 0xfb8, 0x88},
319311890956SLydia Wang 	{ }
319411890956SLydia Wang };
319525eaba2fSLydia Wang 
31963e95b9abSLydia Wang static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
31973e95b9abSLydia Wang {
31983e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
31993e95b9abSLydia Wang 	int imux_is_smixer;
32003e95b9abSLydia Wang 	unsigned int parm;
32013e95b9abSLydia Wang 	unsigned int present;
32023e95b9abSLydia Wang 	/* MUX9 (1eh) = stereo mixer */
32033e95b9abSLydia Wang 	imux_is_smixer =
32043e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
32053e95b9abSLydia Wang 	/* inputs */
32063e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
32073e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32083e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
32093e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
32103e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
32113e95b9abSLydia Wang 	parm = AC_PWRST_D0;
32123e95b9abSLydia Wang 	/* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
32133e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
32143e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
32153e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
32163e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
32173e95b9abSLydia Wang 
32183e95b9abSLydia Wang 	/* outputs */
32193e95b9abSLydia Wang 	/* AOW0 (8h)*/
32203e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
32213e95b9abSLydia Wang 
322211890956SLydia Wang 	if (spec->codec_type == VT1802) {
322311890956SLydia Wang 		/* PW4 (28h), MW4 (18h), MUX4(38h) */
322411890956SLydia Wang 		parm = AC_PWRST_D3;
322511890956SLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
322611890956SLydia Wang 		snd_hda_codec_write(codec, 0x18, 0,
322711890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
322811890956SLydia Wang 		snd_hda_codec_write(codec, 0x38, 0,
322911890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
323011890956SLydia Wang 	} else {
32313e95b9abSLydia Wang 		/* PW4 (26h), MW4 (1ch), MUX4(37h) */
32323e95b9abSLydia Wang 		parm = AC_PWRST_D3;
32333e95b9abSLydia Wang 		set_pin_power_state(codec, 0x26, &parm);
32343e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x1c, 0,
32353e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
32363e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x37, 0,
32373e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
323811890956SLydia Wang 	}
32393e95b9abSLydia Wang 
324011890956SLydia Wang 	if (spec->codec_type == VT1802) {
324111890956SLydia Wang 		/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
324211890956SLydia Wang 		parm = AC_PWRST_D3;
324311890956SLydia Wang 		set_pin_power_state(codec, 0x25, &parm);
324411890956SLydia Wang 		snd_hda_codec_write(codec, 0x15, 0,
324511890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
324611890956SLydia Wang 		snd_hda_codec_write(codec, 0x35, 0,
324711890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
324811890956SLydia Wang 	} else {
32493e95b9abSLydia Wang 		/* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
32503e95b9abSLydia Wang 		parm = AC_PWRST_D3;
32513e95b9abSLydia Wang 		set_pin_power_state(codec, 0x25, &parm);
32523e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x19, 0,
32533e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
32543e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x35, 0,
32553e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
325611890956SLydia Wang 	}
32573e95b9abSLydia Wang 
32583e95b9abSLydia Wang 	if (spec->hp_independent_mode)
32593e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x9, 0,
32603e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
32613e95b9abSLydia Wang 
32623e95b9abSLydia Wang 	/* Class-D */
32633e95b9abSLydia Wang 	/* PW0 (24h), MW0(18h/14h), MUX0(34h) */
32643e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x25);
32653e95b9abSLydia Wang 
32663e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32673e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
32683e95b9abSLydia Wang 	parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
326911890956SLydia Wang 	if (spec->codec_type == VT1802)
327011890956SLydia Wang 		snd_hda_codec_write(codec, 0x14, 0,
327111890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
327211890956SLydia Wang 	else
32733e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x18, 0,
32743e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
32753e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x34, 0, AC_VERB_SET_POWER_STATE, parm);
32763e95b9abSLydia Wang 
32773e95b9abSLydia Wang 	/* Mono Out */
32783e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x26);
32793e95b9abSLydia Wang 
32803e95b9abSLydia Wang 	parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
328111890956SLydia Wang 	if (spec->codec_type == VT1802) {
328211890956SLydia Wang 		/* PW15 (33h), MW8(1ch), MUX8(3ch) */
328311890956SLydia Wang 		snd_hda_codec_write(codec, 0x33, 0,
328411890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
328511890956SLydia Wang 		snd_hda_codec_write(codec, 0x1c, 0,
328611890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
328711890956SLydia Wang 		snd_hda_codec_write(codec, 0x3c, 0,
328811890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
328911890956SLydia Wang 	} else {
32903e95b9abSLydia Wang 		/* PW15 (31h), MW8(17h), MUX8(3bh) */
32913e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x31, 0,
32923e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
32933e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x17, 0,
32943e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
32953e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3b, 0,
32963e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
329711890956SLydia Wang 	}
32983e95b9abSLydia Wang 	/* MW9 (21h) */
32993e95b9abSLydia Wang 	if (imux_is_smixer || !is_aa_path_mute(codec))
33003e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x21, 0,
33013e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
33023e95b9abSLydia Wang 	else
33033e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x21, 0,
33043e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
33053e95b9abSLydia Wang }
330625eaba2fSLydia Wang 
330725eaba2fSLydia Wang /* patch for vt2002P */
330825eaba2fSLydia Wang static int patch_vt2002P(struct hda_codec *codec)
330925eaba2fSLydia Wang {
331025eaba2fSLydia Wang 	struct via_spec *spec;
331125eaba2fSLydia Wang 	int err;
331225eaba2fSLydia Wang 
331325eaba2fSLydia Wang 	/* create a codec specific record */
33145b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
331525eaba2fSLydia Wang 	if (spec == NULL)
331625eaba2fSLydia Wang 		return -ENOMEM;
331725eaba2fSLydia Wang 
3318620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
3319d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3320d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
3321620e2b28STakashi Iwai 
332225eaba2fSLydia Wang 	/* automatic parse from the BIOS config */
332312daef65STakashi Iwai 	err = via_parse_auto_config(codec);
332425eaba2fSLydia Wang 	if (err < 0) {
332525eaba2fSLydia Wang 		via_free(codec);
332625eaba2fSLydia Wang 		return err;
332725eaba2fSLydia Wang 	}
332825eaba2fSLydia Wang 
332911890956SLydia Wang 	if (spec->codec_type == VT1802)
33304a918ffeSTakashi Iwai 		spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
333111890956SLydia Wang 	else
33324a918ffeSTakashi Iwai 		spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
333311890956SLydia Wang 
333425eaba2fSLydia Wang 	codec->patch_ops = via_patch_ops;
333525eaba2fSLydia Wang 
33363e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt2002P;
333725eaba2fSLydia Wang 	return 0;
333825eaba2fSLydia Wang }
3339ab6734e7SLydia Wang 
3340ab6734e7SLydia Wang /* for vt1812 */
3341ab6734e7SLydia Wang 
3342096a8854STakashi Iwai static const struct hda_verb vt1812_init_verbs[] = {
3343ab6734e7SLydia Wang 	/* Enable Boost Volume backdoor */
3344ab6734e7SLydia Wang 	{0x1, 0xfb9, 0x24},
3345ab6734e7SLydia Wang 	/* Enable AOW0 to MW9 */
3346ab6734e7SLydia Wang 	{0x1, 0xfb8, 0xa8},
3347ab6734e7SLydia Wang 	{ }
3348ab6734e7SLydia Wang };
3349ab6734e7SLydia Wang 
33503e95b9abSLydia Wang static void set_widgets_power_state_vt1812(struct hda_codec *codec)
33513e95b9abSLydia Wang {
33523e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
33533e95b9abSLydia Wang 	int imux_is_smixer =
33543e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
33553e95b9abSLydia Wang 	unsigned int parm;
33563e95b9abSLydia Wang 	unsigned int present;
33573e95b9abSLydia Wang 	/* MUX10 (1eh) = stereo mixer */
33583e95b9abSLydia Wang 	imux_is_smixer =
33593e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
33603e95b9abSLydia Wang 	/* inputs */
33613e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
33623e95b9abSLydia Wang 	parm = AC_PWRST_D3;
33633e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
33643e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
33653e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
33663e95b9abSLydia Wang 	parm = AC_PWRST_D0;
33673e95b9abSLydia Wang 	/* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
33683e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
33693e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
33703e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
33713e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
33723e95b9abSLydia Wang 
33733e95b9abSLydia Wang 	/* outputs */
33743e95b9abSLydia Wang 	/* AOW0 (8h)*/
33753e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x8, 0,
33763e95b9abSLydia Wang 			    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
33773e95b9abSLydia Wang 
33783e95b9abSLydia Wang 	/* PW4 (28h), MW4 (18h), MUX4(38h) */
33793e95b9abSLydia Wang 	parm = AC_PWRST_D3;
33803e95b9abSLydia Wang 	set_pin_power_state(codec, 0x28, &parm);
33813e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
33823e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x38, 0, AC_VERB_SET_POWER_STATE, parm);
33833e95b9abSLydia Wang 
33843e95b9abSLydia Wang 	/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
33853e95b9abSLydia Wang 	parm = AC_PWRST_D3;
33863e95b9abSLydia Wang 	set_pin_power_state(codec, 0x25, &parm);
33873e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x15, 0, AC_VERB_SET_POWER_STATE, parm);
33883e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x35, 0, AC_VERB_SET_POWER_STATE, parm);
33893e95b9abSLydia Wang 	if (spec->hp_independent_mode)
33903e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x9, 0,
33913e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
33923e95b9abSLydia Wang 
33933e95b9abSLydia Wang 	/* Internal Speaker */
33943e95b9abSLydia Wang 	/* PW0 (24h), MW0(14h), MUX0(34h) */
33953e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x25);
33963e95b9abSLydia Wang 
33973e95b9abSLydia Wang 	parm = AC_PWRST_D3;
33983e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
33993e95b9abSLydia Wang 	if (present) {
34003e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x14, 0,
34013e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
34023e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x34, 0,
34033e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
34043e95b9abSLydia Wang 	} else {
34053e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x14, 0,
34063e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
34073e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x34, 0,
34083e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
34093e95b9abSLydia Wang 	}
34103e95b9abSLydia Wang 
34113e95b9abSLydia Wang 
34123e95b9abSLydia Wang 	/* Mono Out */
34133e95b9abSLydia Wang 	/* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
34143e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x28);
34153e95b9abSLydia Wang 
34163e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34173e95b9abSLydia Wang 	set_pin_power_state(codec, 0x31, &parm);
34183e95b9abSLydia Wang 	if (present) {
34193e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x1c, 0,
34203e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
34213e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3c, 0,
34223e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
34233e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3e, 0,
34243e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
34253e95b9abSLydia Wang 	} else {
34263e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x1c, 0,
34273e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
34283e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3c, 0,
34293e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
34303e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3e, 0,
34313e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
34323e95b9abSLydia Wang 	}
34333e95b9abSLydia Wang 
34343e95b9abSLydia Wang 	/* PW15 (33h), MW15 (1dh), MUX15(3dh) */
34353e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34363e95b9abSLydia Wang 	set_pin_power_state(codec, 0x33, &parm);
34373e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
34383e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x3d, 0, AC_VERB_SET_POWER_STATE, parm);
34393e95b9abSLydia Wang 
34403e95b9abSLydia Wang }
3441ab6734e7SLydia Wang 
3442ab6734e7SLydia Wang /* patch for vt1812 */
3443ab6734e7SLydia Wang static int patch_vt1812(struct hda_codec *codec)
3444ab6734e7SLydia Wang {
3445ab6734e7SLydia Wang 	struct via_spec *spec;
3446ab6734e7SLydia Wang 	int err;
3447ab6734e7SLydia Wang 
3448ab6734e7SLydia Wang 	/* create a codec specific record */
34495b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3450ab6734e7SLydia Wang 	if (spec == NULL)
3451ab6734e7SLydia Wang 		return -ENOMEM;
3452ab6734e7SLydia Wang 
3453620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
3454d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3455d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
3456620e2b28STakashi Iwai 
3457ab6734e7SLydia Wang 	/* automatic parse from the BIOS config */
345812daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3459ab6734e7SLydia Wang 	if (err < 0) {
3460ab6734e7SLydia Wang 		via_free(codec);
3461ab6734e7SLydia Wang 		return err;
3462ab6734e7SLydia Wang 	}
3463ab6734e7SLydia Wang 
3464096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++]  = vt1812_init_verbs;
3465ab6734e7SLydia Wang 
3466ab6734e7SLydia Wang 	codec->patch_ops = via_patch_ops;
3467ab6734e7SLydia Wang 
34683e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1812;
3469ab6734e7SLydia Wang 	return 0;
3470ab6734e7SLydia Wang }
3471ab6734e7SLydia Wang 
3472c577b8a1SJoseph Chan /*
3473c577b8a1SJoseph Chan  * patch entries
3474c577b8a1SJoseph Chan  */
347590dd48a1STakashi Iwai static const struct hda_codec_preset snd_hda_preset_via[] = {
34763218c178STakashi Iwai 	{ .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
34773218c178STakashi Iwai 	{ .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
34783218c178STakashi Iwai 	{ .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
34793218c178STakashi Iwai 	{ .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
34803218c178STakashi Iwai 	{ .id = 0x1106e710, .name = "VT1709 10-Ch",
3481ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
34823218c178STakashi Iwai 	{ .id = 0x1106e711, .name = "VT1709 10-Ch",
3483ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
34843218c178STakashi Iwai 	{ .id = 0x1106e712, .name = "VT1709 10-Ch",
3485ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
34863218c178STakashi Iwai 	{ .id = 0x1106e713, .name = "VT1709 10-Ch",
3487ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
34883218c178STakashi Iwai 	{ .id = 0x1106e714, .name = "VT1709 6-Ch",
3489ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
34903218c178STakashi Iwai 	{ .id = 0x1106e715, .name = "VT1709 6-Ch",
3491ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
34923218c178STakashi Iwai 	{ .id = 0x1106e716, .name = "VT1709 6-Ch",
3493ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
34943218c178STakashi Iwai 	{ .id = 0x1106e717, .name = "VT1709 6-Ch",
3495ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
34963218c178STakashi Iwai 	{ .id = 0x1106e720, .name = "VT1708B 8-Ch",
3497ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
34983218c178STakashi Iwai 	{ .id = 0x1106e721, .name = "VT1708B 8-Ch",
3499ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
35003218c178STakashi Iwai 	{ .id = 0x1106e722, .name = "VT1708B 8-Ch",
3501ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
35023218c178STakashi Iwai 	{ .id = 0x1106e723, .name = "VT1708B 8-Ch",
3503ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
35043218c178STakashi Iwai 	{ .id = 0x1106e724, .name = "VT1708B 4-Ch",
3505ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
35063218c178STakashi Iwai 	{ .id = 0x1106e725, .name = "VT1708B 4-Ch",
3507ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
35083218c178STakashi Iwai 	{ .id = 0x1106e726, .name = "VT1708B 4-Ch",
3509ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
35103218c178STakashi Iwai 	{ .id = 0x1106e727, .name = "VT1708B 4-Ch",
3511ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
35123218c178STakashi Iwai 	{ .id = 0x11060397, .name = "VT1708S",
3513d949cac1SHarald Welte 	  .patch = patch_vt1708S},
35143218c178STakashi Iwai 	{ .id = 0x11061397, .name = "VT1708S",
3515d949cac1SHarald Welte 	  .patch = patch_vt1708S},
35163218c178STakashi Iwai 	{ .id = 0x11062397, .name = "VT1708S",
3517d949cac1SHarald Welte 	  .patch = patch_vt1708S},
35183218c178STakashi Iwai 	{ .id = 0x11063397, .name = "VT1708S",
3519d949cac1SHarald Welte 	  .patch = patch_vt1708S},
3520bc92df7fSLydia Wang 	{ .id = 0x11064397, .name = "VT1705",
3521d949cac1SHarald Welte 	  .patch = patch_vt1708S},
35223218c178STakashi Iwai 	{ .id = 0x11065397, .name = "VT1708S",
3523d949cac1SHarald Welte 	  .patch = patch_vt1708S},
35243218c178STakashi Iwai 	{ .id = 0x11066397, .name = "VT1708S",
3525d949cac1SHarald Welte 	  .patch = patch_vt1708S},
35263218c178STakashi Iwai 	{ .id = 0x11067397, .name = "VT1708S",
3527d949cac1SHarald Welte 	  .patch = patch_vt1708S},
35283218c178STakashi Iwai 	{ .id = 0x11060398, .name = "VT1702",
3529d949cac1SHarald Welte 	  .patch = patch_vt1702},
35303218c178STakashi Iwai 	{ .id = 0x11061398, .name = "VT1702",
3531d949cac1SHarald Welte 	  .patch = patch_vt1702},
35323218c178STakashi Iwai 	{ .id = 0x11062398, .name = "VT1702",
3533d949cac1SHarald Welte 	  .patch = patch_vt1702},
35343218c178STakashi Iwai 	{ .id = 0x11063398, .name = "VT1702",
3535d949cac1SHarald Welte 	  .patch = patch_vt1702},
35363218c178STakashi Iwai 	{ .id = 0x11064398, .name = "VT1702",
3537d949cac1SHarald Welte 	  .patch = patch_vt1702},
35383218c178STakashi Iwai 	{ .id = 0x11065398, .name = "VT1702",
3539d949cac1SHarald Welte 	  .patch = patch_vt1702},
35403218c178STakashi Iwai 	{ .id = 0x11066398, .name = "VT1702",
3541d949cac1SHarald Welte 	  .patch = patch_vt1702},
35423218c178STakashi Iwai 	{ .id = 0x11067398, .name = "VT1702",
3543d949cac1SHarald Welte 	  .patch = patch_vt1702},
3544eb7188caSLydia Wang 	{ .id = 0x11060428, .name = "VT1718S",
3545eb7188caSLydia Wang 	  .patch = patch_vt1718S},
3546eb7188caSLydia Wang 	{ .id = 0x11064428, .name = "VT1718S",
3547eb7188caSLydia Wang 	  .patch = patch_vt1718S},
3548bb3c6bfcSLydia Wang 	{ .id = 0x11060441, .name = "VT2020",
3549bb3c6bfcSLydia Wang 	  .patch = patch_vt1718S},
3550bb3c6bfcSLydia Wang 	{ .id = 0x11064441, .name = "VT1828S",
3551bb3c6bfcSLydia Wang 	  .patch = patch_vt1718S},
3552f3db423dSLydia Wang 	{ .id = 0x11060433, .name = "VT1716S",
3553f3db423dSLydia Wang 	  .patch = patch_vt1716S},
3554f3db423dSLydia Wang 	{ .id = 0x1106a721, .name = "VT1716S",
3555f3db423dSLydia Wang 	  .patch = patch_vt1716S},
355625eaba2fSLydia Wang 	{ .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
355725eaba2fSLydia Wang 	{ .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
3558ab6734e7SLydia Wang 	{ .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
355936dd5c4aSLydia Wang 	{ .id = 0x11060440, .name = "VT1818S",
356036dd5c4aSLydia Wang 	  .patch = patch_vt1708S},
356111890956SLydia Wang 	{ .id = 0x11060446, .name = "VT1802",
356211890956SLydia Wang 		.patch = patch_vt2002P},
356311890956SLydia Wang 	{ .id = 0x11068446, .name = "VT1802",
356411890956SLydia Wang 		.patch = patch_vt2002P},
3565c577b8a1SJoseph Chan 	{} /* terminator */
3566c577b8a1SJoseph Chan };
35671289e9e8STakashi Iwai 
35681289e9e8STakashi Iwai MODULE_ALIAS("snd-hda-codec-id:1106*");
35691289e9e8STakashi Iwai 
35701289e9e8STakashi Iwai static struct hda_codec_preset_list via_list = {
35711289e9e8STakashi Iwai 	.preset = snd_hda_preset_via,
35721289e9e8STakashi Iwai 	.owner = THIS_MODULE,
35731289e9e8STakashi Iwai };
35741289e9e8STakashi Iwai 
35751289e9e8STakashi Iwai MODULE_LICENSE("GPL");
35761289e9e8STakashi Iwai MODULE_DESCRIPTION("VIA HD-audio codec");
35771289e9e8STakashi Iwai 
35781289e9e8STakashi Iwai static int __init patch_via_init(void)
35791289e9e8STakashi Iwai {
35801289e9e8STakashi Iwai 	return snd_hda_add_codec_preset(&via_list);
35811289e9e8STakashi Iwai }
35821289e9e8STakashi Iwai 
35831289e9e8STakashi Iwai static void __exit patch_via_exit(void)
35841289e9e8STakashi Iwai {
35851289e9e8STakashi Iwai 	snd_hda_delete_codec_preset(&via_list);
35861289e9e8STakashi Iwai }
35871289e9e8STakashi Iwai 
35881289e9e8STakashi Iwai module_init(patch_via_init)
35891289e9e8STakashi Iwai module_exit(patch_via_exit)
3590