xref: /openbmc/linux/sound/pci/hda/patch_via.c (revision da155d5b40587815a4397e1a69382fe2366d940b)
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>
52*da155d5bSPaul Gortmaker #include <linux/module.h>
53c577b8a1SJoseph Chan #include <sound/core.h>
540aa62aefSHarald Welte #include <sound/asoundef.h>
55c577b8a1SJoseph Chan #include "hda_codec.h"
56c577b8a1SJoseph Chan #include "hda_local.h"
57c577b8a1SJoseph Chan 
58c577b8a1SJoseph Chan /* Pin Widget NID */
5976d9b0ddSHarald Welte #define VT1708_HP_PIN_NID	0x20
6076d9b0ddSHarald Welte #define VT1708_CD_PIN_NID	0x24
61c577b8a1SJoseph Chan 
62d7426329SHarald Welte enum VIA_HDA_CODEC {
63d7426329SHarald Welte 	UNKNOWN = -1,
64d7426329SHarald Welte 	VT1708,
65d7426329SHarald Welte 	VT1709_10CH,
66d7426329SHarald Welte 	VT1709_6CH,
67d7426329SHarald Welte 	VT1708B_8CH,
68d7426329SHarald Welte 	VT1708B_4CH,
69d7426329SHarald Welte 	VT1708S,
70518bf3baSLydia Wang 	VT1708BCE,
71d7426329SHarald Welte 	VT1702,
72eb7188caSLydia Wang 	VT1718S,
73f3db423dSLydia Wang 	VT1716S,
7425eaba2fSLydia Wang 	VT2002P,
75ab6734e7SLydia Wang 	VT1812,
7611890956SLydia Wang 	VT1802,
77d7426329SHarald Welte 	CODEC_TYPES,
78d7426329SHarald Welte };
79d7426329SHarald Welte 
8011890956SLydia Wang #define VT2002P_COMPATIBLE(spec) \
8111890956SLydia Wang 	((spec)->codec_type == VT2002P ||\
8211890956SLydia Wang 	 (spec)->codec_type == VT1812 ||\
8311890956SLydia Wang 	 (spec)->codec_type == VT1802)
8411890956SLydia Wang 
858e3679dcSTakashi Iwai #define MAX_NID_PATH_DEPTH	5
868e3679dcSTakashi Iwai 
8709a9ad69STakashi Iwai /* output-path: DAC -> ... -> pin
8809a9ad69STakashi Iwai  * idx[] contains the source index number of the next widget;
8909a9ad69STakashi Iwai  * e.g. idx[0] is the index of the DAC selected by path[1] widget
9009a9ad69STakashi Iwai  * multi[] indicates whether it's a selector widget with multi-connectors
9109a9ad69STakashi Iwai  * (i.e. the connection selection is mandatory)
9209a9ad69STakashi Iwai  * vol_ctl and mute_ctl contains the NIDs for the assigned mixers
9309a9ad69STakashi Iwai  */
944a79616dSTakashi Iwai struct nid_path {
954a79616dSTakashi Iwai 	int depth;
968e3679dcSTakashi Iwai 	hda_nid_t path[MAX_NID_PATH_DEPTH];
9709a9ad69STakashi Iwai 	unsigned char idx[MAX_NID_PATH_DEPTH];
9809a9ad69STakashi Iwai 	unsigned char multi[MAX_NID_PATH_DEPTH];
9909a9ad69STakashi Iwai 	unsigned int vol_ctl;
10009a9ad69STakashi Iwai 	unsigned int mute_ctl;
1014a79616dSTakashi Iwai };
1024a79616dSTakashi Iwai 
103a86a88eaSTakashi Iwai /* input-path */
104a86a88eaSTakashi Iwai struct via_input {
105a86a88eaSTakashi Iwai 	hda_nid_t pin;	/* input-pin or aa-mix */
106a86a88eaSTakashi Iwai 	int adc_idx;	/* ADC index to be used */
107a86a88eaSTakashi Iwai 	int mux_idx;	/* MUX index (if any) */
108a86a88eaSTakashi Iwai 	const char *label;	/* input-source label */
109a86a88eaSTakashi Iwai };
110a86a88eaSTakashi Iwai 
111de6c74f3STakashi Iwai #define VIA_MAX_ADCS	3
112de6c74f3STakashi Iwai 
1133b607e3dSTakashi Iwai enum {
1143b607e3dSTakashi Iwai 	STREAM_MULTI_OUT = (1 << 0),
1153b607e3dSTakashi Iwai 	STREAM_INDEP_HP = (1 << 1),
1163b607e3dSTakashi Iwai };
1173b607e3dSTakashi Iwai 
1181f2e99feSLydia Wang struct via_spec {
1191f2e99feSLydia Wang 	/* codec parameterization */
12090dd48a1STakashi Iwai 	const struct snd_kcontrol_new *mixers[6];
1211f2e99feSLydia Wang 	unsigned int num_mixers;
1221f2e99feSLydia Wang 
12390dd48a1STakashi Iwai 	const struct hda_verb *init_verbs[5];
1241f2e99feSLydia Wang 	unsigned int num_iverbs;
1251f2e99feSLydia Wang 
12682673bc8STakashi Iwai 	char stream_name_analog[32];
1277eb56e84STakashi Iwai 	char stream_name_hp[32];
12890dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_analog_playback;
12990dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_analog_capture;
1301f2e99feSLydia Wang 
13182673bc8STakashi Iwai 	char stream_name_digital[32];
13290dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_digital_playback;
13390dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_digital_capture;
1341f2e99feSLydia Wang 
1351f2e99feSLydia Wang 	/* playback */
1361f2e99feSLydia Wang 	struct hda_multi_out multiout;
1371f2e99feSLydia Wang 	hda_nid_t slave_dig_outs[2];
138ece8d043STakashi Iwai 	hda_nid_t hp_dac_nid;
1393214b966STakashi Iwai 	hda_nid_t speaker_dac_nid;
1403214b966STakashi Iwai 	int hp_indep_shared;	/* indep HP-DAC is shared with side ch */
1413b607e3dSTakashi Iwai 	int opened_streams;	/* STREAM_* bits */
1423b607e3dSTakashi Iwai 	int active_streams;	/* STREAM_* bits */
1433214b966STakashi Iwai 	int aamix_mode;		/* loopback is enabled for output-path? */
1441f2e99feSLydia Wang 
1453214b966STakashi Iwai 	/* Output-paths:
1463214b966STakashi Iwai 	 * There are different output-paths depending on the setup.
1473214b966STakashi Iwai 	 * out_path, hp_path and speaker_path are primary paths.  If both
1483214b966STakashi Iwai 	 * direct DAC and aa-loopback routes are available, these contain
1493214b966STakashi Iwai 	 * the former paths.  Meanwhile *_mix_path contain the paths with
1503214b966STakashi Iwai 	 * loopback mixer.  (Since the loopback is only for front channel,
1513214b966STakashi Iwai 	 * no out_mix_path for surround channels.)
1523214b966STakashi Iwai 	 * The HP output has another path, hp_indep_path, which is used in
1533214b966STakashi Iwai 	 * the independent-HP mode.
1543214b966STakashi Iwai 	 */
155de6c74f3STakashi Iwai 	struct nid_path out_path[HDA_SIDE + 1];
1563214b966STakashi Iwai 	struct nid_path out_mix_path;
1574a79616dSTakashi Iwai 	struct nid_path hp_path;
1583214b966STakashi Iwai 	struct nid_path hp_mix_path;
1593214b966STakashi Iwai 	struct nid_path hp_indep_path;
1604a918ffeSTakashi Iwai 	struct nid_path speaker_path;
1613214b966STakashi Iwai 	struct nid_path speaker_mix_path;
1624a79616dSTakashi Iwai 
1631f2e99feSLydia Wang 	/* capture */
1641f2e99feSLydia Wang 	unsigned int num_adc_nids;
165de6c74f3STakashi Iwai 	hda_nid_t adc_nids[VIA_MAX_ADCS];
166de6c74f3STakashi Iwai 	hda_nid_t mux_nids[VIA_MAX_ADCS];
167620e2b28STakashi Iwai 	hda_nid_t aa_mix_nid;
1681f2e99feSLydia Wang 	hda_nid_t dig_in_nid;
1691f2e99feSLydia Wang 
1701f2e99feSLydia Wang 	/* capture source */
171a86a88eaSTakashi Iwai 	bool dyn_adc_switch;
172a86a88eaSTakashi Iwai 	int num_inputs;
173a86a88eaSTakashi Iwai 	struct via_input inputs[AUTO_CFG_MAX_INS + 1];
174de6c74f3STakashi Iwai 	unsigned int cur_mux[VIA_MAX_ADCS];
1751f2e99feSLydia Wang 
1763b607e3dSTakashi Iwai 	/* dynamic DAC switching */
1773b607e3dSTakashi Iwai 	unsigned int cur_dac_stream_tag;
1783b607e3dSTakashi Iwai 	unsigned int cur_dac_format;
1793b607e3dSTakashi Iwai 	unsigned int cur_hp_stream_tag;
1803b607e3dSTakashi Iwai 	unsigned int cur_hp_format;
1813b607e3dSTakashi Iwai 
182a86a88eaSTakashi Iwai 	/* dynamic ADC switching */
183a86a88eaSTakashi Iwai 	hda_nid_t cur_adc;
184a86a88eaSTakashi Iwai 	unsigned int cur_adc_stream_tag;
185a86a88eaSTakashi Iwai 	unsigned int cur_adc_format;
186a86a88eaSTakashi Iwai 
1871f2e99feSLydia Wang 	/* PCM information */
1881f2e99feSLydia Wang 	struct hda_pcm pcm_rec[3];
1891f2e99feSLydia Wang 
1901f2e99feSLydia Wang 	/* dynamic controls, init_verbs and input_mux */
1911f2e99feSLydia Wang 	struct auto_pin_cfg autocfg;
1921f2e99feSLydia Wang 	struct snd_array kctls;
1931f2e99feSLydia Wang 	hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
1941f2e99feSLydia Wang 
1951f2e99feSLydia Wang 	/* HP mode source */
1961f2e99feSLydia Wang 	unsigned int hp_independent_mode;
197f3db423dSLydia Wang 	unsigned int dmic_enabled;
19824088a58STakashi Iwai 	unsigned int no_pin_power_ctl;
1991f2e99feSLydia Wang 	enum VIA_HDA_CODEC codec_type;
2001f2e99feSLydia Wang 
201e3d7a143STakashi Iwai 	/* smart51 setup */
202e3d7a143STakashi Iwai 	unsigned int smart51_nums;
203e3d7a143STakashi Iwai 	hda_nid_t smart51_pins[2];
204e3d7a143STakashi Iwai 	int smart51_idxs[2];
205e3d7a143STakashi Iwai 	const char *smart51_labels[2];
206e3d7a143STakashi Iwai 	unsigned int smart51_enabled;
207e3d7a143STakashi Iwai 
2081f2e99feSLydia Wang 	/* work to check hp jack state */
2091f2e99feSLydia Wang 	struct hda_codec *codec;
2101f2e99feSLydia Wang 	struct delayed_work vt1708_hp_work;
211e06e5a29STakashi Iwai 	int vt1708_jack_detect;
2121f2e99feSLydia Wang 	int vt1708_hp_present;
2133e95b9abSLydia Wang 
2143e95b9abSLydia Wang 	void (*set_widgets_power_state)(struct hda_codec *codec);
2153e95b9abSLydia Wang 
2161f2e99feSLydia Wang 	struct hda_loopback_check loopback;
21713af8e77STakashi Iwai 	int num_loopbacks;
21813af8e77STakashi Iwai 	struct hda_amp_list loopback_list[8];
219a86a88eaSTakashi Iwai 
220a86a88eaSTakashi Iwai 	/* bind capture-volume */
221a86a88eaSTakashi Iwai 	struct hda_bind_ctls *bind_cap_vol;
222a86a88eaSTakashi Iwai 	struct hda_bind_ctls *bind_cap_sw;
2233b607e3dSTakashi Iwai 
2243b607e3dSTakashi Iwai 	struct mutex config_mutex;
2251f2e99feSLydia Wang };
2261f2e99feSLydia Wang 
2270341ccd7SLydia Wang static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
2285b0cb1d8SJaroslav Kysela static struct via_spec * via_new_spec(struct hda_codec *codec)
2295b0cb1d8SJaroslav Kysela {
2305b0cb1d8SJaroslav Kysela 	struct via_spec *spec;
2315b0cb1d8SJaroslav Kysela 
2325b0cb1d8SJaroslav Kysela 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2335b0cb1d8SJaroslav Kysela 	if (spec == NULL)
2345b0cb1d8SJaroslav Kysela 		return NULL;
2355b0cb1d8SJaroslav Kysela 
2363b607e3dSTakashi Iwai 	mutex_init(&spec->config_mutex);
2375b0cb1d8SJaroslav Kysela 	codec->spec = spec;
2385b0cb1d8SJaroslav Kysela 	spec->codec = codec;
2390341ccd7SLydia Wang 	spec->codec_type = get_codec_type(codec);
2400341ccd7SLydia Wang 	/* VT1708BCE & VT1708S are almost same */
2410341ccd7SLydia Wang 	if (spec->codec_type == VT1708BCE)
2420341ccd7SLydia Wang 		spec->codec_type = VT1708S;
2435b0cb1d8SJaroslav Kysela 	return spec;
2445b0cb1d8SJaroslav Kysela }
2455b0cb1d8SJaroslav Kysela 
246744ff5f4SLydia Wang static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
247d7426329SHarald Welte {
248744ff5f4SLydia Wang 	u32 vendor_id = codec->vendor_id;
249d7426329SHarald Welte 	u16 ven_id = vendor_id >> 16;
250d7426329SHarald Welte 	u16 dev_id = vendor_id & 0xffff;
251d7426329SHarald Welte 	enum VIA_HDA_CODEC codec_type;
252d7426329SHarald Welte 
253d7426329SHarald Welte 	/* get codec type */
254d7426329SHarald Welte 	if (ven_id != 0x1106)
255d7426329SHarald Welte 		codec_type = UNKNOWN;
256d7426329SHarald Welte 	else if (dev_id >= 0x1708 && dev_id <= 0x170b)
257d7426329SHarald Welte 		codec_type = VT1708;
258d7426329SHarald Welte 	else if (dev_id >= 0xe710 && dev_id <= 0xe713)
259d7426329SHarald Welte 		codec_type = VT1709_10CH;
260d7426329SHarald Welte 	else if (dev_id >= 0xe714 && dev_id <= 0xe717)
261d7426329SHarald Welte 		codec_type = VT1709_6CH;
262518bf3baSLydia Wang 	else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
263d7426329SHarald Welte 		codec_type = VT1708B_8CH;
264518bf3baSLydia Wang 		if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
265518bf3baSLydia Wang 			codec_type = VT1708BCE;
266518bf3baSLydia Wang 	} else if (dev_id >= 0xe724 && dev_id <= 0xe727)
267d7426329SHarald Welte 		codec_type = VT1708B_4CH;
268d7426329SHarald Welte 	else if ((dev_id & 0xfff) == 0x397
269d7426329SHarald Welte 		 && (dev_id >> 12) < 8)
270d7426329SHarald Welte 		codec_type = VT1708S;
271d7426329SHarald Welte 	else if ((dev_id & 0xfff) == 0x398
272d7426329SHarald Welte 		 && (dev_id >> 12) < 8)
273d7426329SHarald Welte 		codec_type = VT1702;
274eb7188caSLydia Wang 	else if ((dev_id & 0xfff) == 0x428
275eb7188caSLydia Wang 		 && (dev_id >> 12) < 8)
276eb7188caSLydia Wang 		codec_type = VT1718S;
277f3db423dSLydia Wang 	else if (dev_id == 0x0433 || dev_id == 0xa721)
278f3db423dSLydia Wang 		codec_type = VT1716S;
279bb3c6bfcSLydia Wang 	else if (dev_id == 0x0441 || dev_id == 0x4441)
280bb3c6bfcSLydia Wang 		codec_type = VT1718S;
28125eaba2fSLydia Wang 	else if (dev_id == 0x0438 || dev_id == 0x4438)
28225eaba2fSLydia Wang 		codec_type = VT2002P;
283ab6734e7SLydia Wang 	else if (dev_id == 0x0448)
284ab6734e7SLydia Wang 		codec_type = VT1812;
28536dd5c4aSLydia Wang 	else if (dev_id == 0x0440)
28636dd5c4aSLydia Wang 		codec_type = VT1708S;
28711890956SLydia Wang 	else if ((dev_id & 0xfff) == 0x446)
28811890956SLydia Wang 		codec_type = VT1802;
289d7426329SHarald Welte 	else
290d7426329SHarald Welte 		codec_type = UNKNOWN;
291d7426329SHarald Welte 	return codec_type;
292d7426329SHarald Welte };
293d7426329SHarald Welte 
294ec7e7e42SLydia Wang #define VIA_JACK_EVENT		0x20
29569e52a80SHarald Welte #define VIA_HP_EVENT		0x01
29669e52a80SHarald Welte #define VIA_GPIO_EVENT		0x02
2974a918ffeSTakashi Iwai #define VIA_LINE_EVENT		0x03
29869e52a80SHarald Welte 
299c577b8a1SJoseph Chan enum {
300c577b8a1SJoseph Chan 	VIA_CTL_WIDGET_VOL,
301c577b8a1SJoseph Chan 	VIA_CTL_WIDGET_MUTE,
302f5271101SLydia Wang 	VIA_CTL_WIDGET_ANALOG_MUTE,
303c577b8a1SJoseph Chan };
304c577b8a1SJoseph Chan 
305ada509ecSTakashi Iwai static void analog_low_current_mode(struct hda_codec *codec);
306ada509ecSTakashi Iwai static bool is_aa_path_mute(struct hda_codec *codec);
3071f2e99feSLydia Wang 
3081f2e99feSLydia Wang static void vt1708_start_hp_work(struct via_spec *spec)
3091f2e99feSLydia Wang {
3101f2e99feSLydia Wang 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
3111f2e99feSLydia Wang 		return;
3121f2e99feSLydia Wang 	snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
313e06e5a29STakashi Iwai 			    !spec->vt1708_jack_detect);
3141f2e99feSLydia Wang 	if (!delayed_work_pending(&spec->vt1708_hp_work))
3151f2e99feSLydia Wang 		schedule_delayed_work(&spec->vt1708_hp_work,
3161f2e99feSLydia Wang 				      msecs_to_jiffies(100));
3171f2e99feSLydia Wang }
3181f2e99feSLydia Wang 
3191f2e99feSLydia Wang static void vt1708_stop_hp_work(struct via_spec *spec)
3201f2e99feSLydia Wang {
3211f2e99feSLydia Wang 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
3221f2e99feSLydia Wang 		return;
3231f2e99feSLydia Wang 	if (snd_hda_get_bool_hint(spec->codec, "analog_loopback_hp_detect") == 1
3241f2e99feSLydia Wang 	    && !is_aa_path_mute(spec->codec))
3251f2e99feSLydia Wang 		return;
3261f2e99feSLydia Wang 	snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
327e06e5a29STakashi Iwai 			    !spec->vt1708_jack_detect);
3285b84ba26STejun Heo 	cancel_delayed_work_sync(&spec->vt1708_hp_work);
3291f2e99feSLydia Wang }
330f5271101SLydia Wang 
3313e95b9abSLydia Wang static void set_widgets_power_state(struct hda_codec *codec)
3323e95b9abSLydia Wang {
3333e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
3343e95b9abSLydia Wang 	if (spec->set_widgets_power_state)
3353e95b9abSLydia Wang 		spec->set_widgets_power_state(codec);
3363e95b9abSLydia Wang }
33725eaba2fSLydia Wang 
338f5271101SLydia Wang static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
339f5271101SLydia Wang 				   struct snd_ctl_elem_value *ucontrol)
340f5271101SLydia Wang {
341f5271101SLydia Wang 	int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
342f5271101SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
343f5271101SLydia Wang 
3443e95b9abSLydia Wang 	set_widgets_power_state(codec);
345ada509ecSTakashi Iwai 	analog_low_current_mode(snd_kcontrol_chip(kcontrol));
3461f2e99feSLydia Wang 	if (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1) {
3471f2e99feSLydia Wang 		if (is_aa_path_mute(codec))
3481f2e99feSLydia Wang 			vt1708_start_hp_work(codec->spec);
3491f2e99feSLydia Wang 		else
3501f2e99feSLydia Wang 			vt1708_stop_hp_work(codec->spec);
3511f2e99feSLydia Wang 	}
352f5271101SLydia Wang 	return change;
353f5271101SLydia Wang }
354f5271101SLydia Wang 
355f5271101SLydia Wang /* modify .put = snd_hda_mixer_amp_switch_put */
356f5271101SLydia Wang #define ANALOG_INPUT_MUTE						\
357f5271101SLydia Wang 	{		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,		\
358f5271101SLydia Wang 			.name = NULL,					\
359f5271101SLydia Wang 			.index = 0,					\
360f5271101SLydia Wang 			.info = snd_hda_mixer_amp_switch_info,		\
361f5271101SLydia Wang 			.get = snd_hda_mixer_amp_switch_get,		\
362f5271101SLydia Wang 			.put = analog_input_switch_put,			\
363f5271101SLydia Wang 			.private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
364f5271101SLydia Wang 
36590dd48a1STakashi Iwai static const struct snd_kcontrol_new via_control_templates[] = {
366c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME(NULL, 0, 0, 0),
367c577b8a1SJoseph Chan 	HDA_CODEC_MUTE(NULL, 0, 0, 0),
368f5271101SLydia Wang 	ANALOG_INPUT_MUTE,
369c577b8a1SJoseph Chan };
370c577b8a1SJoseph Chan 
371ab6734e7SLydia Wang 
372c577b8a1SJoseph Chan /* add dynamic controls */
373291c9e33STakashi Iwai static struct snd_kcontrol_new *__via_clone_ctl(struct via_spec *spec,
374291c9e33STakashi Iwai 				const struct snd_kcontrol_new *tmpl,
375291c9e33STakashi Iwai 				const char *name)
376c577b8a1SJoseph Chan {
377c577b8a1SJoseph Chan 	struct snd_kcontrol_new *knew;
378c577b8a1SJoseph Chan 
379603c4019STakashi Iwai 	snd_array_init(&spec->kctls, sizeof(*knew), 32);
380603c4019STakashi Iwai 	knew = snd_array_new(&spec->kctls);
381c577b8a1SJoseph Chan 	if (!knew)
382291c9e33STakashi Iwai 		return NULL;
383291c9e33STakashi Iwai 	*knew = *tmpl;
384291c9e33STakashi Iwai 	if (!name)
385291c9e33STakashi Iwai 		name = tmpl->name;
386291c9e33STakashi Iwai 	if (name) {
387c577b8a1SJoseph Chan 		knew->name = kstrdup(name, GFP_KERNEL);
388c577b8a1SJoseph Chan 		if (!knew->name)
389291c9e33STakashi Iwai 			return NULL;
390291c9e33STakashi Iwai 	}
391291c9e33STakashi Iwai 	return knew;
392291c9e33STakashi Iwai }
393291c9e33STakashi Iwai 
394291c9e33STakashi Iwai static int __via_add_control(struct via_spec *spec, int type, const char *name,
395291c9e33STakashi Iwai 			     int idx, unsigned long val)
396291c9e33STakashi Iwai {
397291c9e33STakashi Iwai 	struct snd_kcontrol_new *knew;
398291c9e33STakashi Iwai 
399291c9e33STakashi Iwai 	knew = __via_clone_ctl(spec, &via_control_templates[type], name);
400291c9e33STakashi Iwai 	if (!knew)
401c577b8a1SJoseph Chan 		return -ENOMEM;
402d7a99cceSTakashi Iwai 	knew->index = idx;
4034d02d1b6SJaroslav Kysela 	if (get_amp_nid_(val))
4045e26dfd0SJaroslav Kysela 		knew->subdevice = HDA_SUBDEV_AMP_FLAG;
405c577b8a1SJoseph Chan 	knew->private_value = val;
406c577b8a1SJoseph Chan 	return 0;
407c577b8a1SJoseph Chan }
408c577b8a1SJoseph Chan 
4097b315bb4STakashi Iwai #define via_add_control(spec, type, name, val) \
4107b315bb4STakashi Iwai 	__via_add_control(spec, type, name, 0, val)
4117b315bb4STakashi Iwai 
412291c9e33STakashi Iwai #define via_clone_control(spec, tmpl) __via_clone_ctl(spec, tmpl, NULL)
4135b0cb1d8SJaroslav Kysela 
414603c4019STakashi Iwai static void via_free_kctls(struct hda_codec *codec)
415603c4019STakashi Iwai {
416603c4019STakashi Iwai 	struct via_spec *spec = codec->spec;
417603c4019STakashi Iwai 
418603c4019STakashi Iwai 	if (spec->kctls.list) {
419603c4019STakashi Iwai 		struct snd_kcontrol_new *kctl = spec->kctls.list;
420603c4019STakashi Iwai 		int i;
421603c4019STakashi Iwai 		for (i = 0; i < spec->kctls.used; i++)
422603c4019STakashi Iwai 			kfree(kctl[i].name);
423603c4019STakashi Iwai 	}
424603c4019STakashi Iwai 	snd_array_free(&spec->kctls);
425603c4019STakashi Iwai }
426603c4019STakashi Iwai 
427c577b8a1SJoseph Chan /* create input playback/capture controls for the given pin */
4289510e8ddSLydia Wang static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
4297b315bb4STakashi Iwai 				int type_idx, int idx, int mix_nid)
430c577b8a1SJoseph Chan {
431c577b8a1SJoseph Chan 	char name[32];
432c577b8a1SJoseph Chan 	int err;
433c577b8a1SJoseph Chan 
434c577b8a1SJoseph Chan 	sprintf(name, "%s Playback Volume", ctlname);
4357b315bb4STakashi Iwai 	err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
436c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
437c577b8a1SJoseph Chan 	if (err < 0)
438c577b8a1SJoseph Chan 		return err;
439c577b8a1SJoseph Chan 	sprintf(name, "%s Playback Switch", ctlname);
4407b315bb4STakashi Iwai 	err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
441c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
442c577b8a1SJoseph Chan 	if (err < 0)
443c577b8a1SJoseph Chan 		return err;
444c577b8a1SJoseph Chan 	return 0;
445c577b8a1SJoseph Chan }
446c577b8a1SJoseph Chan 
4475d41762aSTakashi Iwai #define get_connection_index(codec, mux, nid) \
4488d087c76STakashi Iwai 	snd_hda_get_conn_index(codec, mux, nid, 0)
4495d41762aSTakashi Iwai 
4508df2a312STakashi Iwai static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
4518df2a312STakashi Iwai 			   unsigned int mask)
4528df2a312STakashi Iwai {
453a934d5a9STakashi Iwai 	unsigned int caps;
454a934d5a9STakashi Iwai 	if (!nid)
455a934d5a9STakashi Iwai 		return false;
456a934d5a9STakashi Iwai 	caps = get_wcaps(codec, nid);
4578df2a312STakashi Iwai 	if (dir == HDA_INPUT)
4588df2a312STakashi Iwai 		caps &= AC_WCAP_IN_AMP;
4598df2a312STakashi Iwai 	else
4608df2a312STakashi Iwai 		caps &= AC_WCAP_OUT_AMP;
4618df2a312STakashi Iwai 	if (!caps)
4628df2a312STakashi Iwai 		return false;
4638df2a312STakashi Iwai 	if (query_amp_caps(codec, nid, dir) & mask)
4648df2a312STakashi Iwai 		return true;
4658df2a312STakashi Iwai 	return false;
4668df2a312STakashi Iwai }
4678df2a312STakashi Iwai 
46809a9ad69STakashi Iwai #define have_mute(codec, nid, dir) \
46909a9ad69STakashi Iwai 	check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
4708df2a312STakashi Iwai 
471d69607b3SLydia Wang /* enable/disable the output-route mixers */
472d69607b3SLydia Wang static void activate_output_mix(struct hda_codec *codec, struct nid_path *path,
4733214b966STakashi Iwai 				hda_nid_t mix_nid, int idx, bool enable)
474d69607b3SLydia Wang {
475d69607b3SLydia Wang 	int i, num, val;
476d69607b3SLydia Wang 
477d69607b3SLydia Wang 	if (!path)
478d69607b3SLydia Wang 		return;
479d69607b3SLydia Wang 	num = snd_hda_get_conn_list(codec, mix_nid, NULL);
480d69607b3SLydia Wang 	for (i = 0; i < num; i++) {
4813214b966STakashi Iwai 		if (i == idx)
482d69607b3SLydia Wang 			val = AMP_IN_UNMUTE(i);
483d69607b3SLydia Wang 		else
484d69607b3SLydia Wang 			val = AMP_IN_MUTE(i);
485d69607b3SLydia Wang 		snd_hda_codec_write(codec, mix_nid, 0,
486d69607b3SLydia Wang 				    AC_VERB_SET_AMP_GAIN_MUTE, val);
487d69607b3SLydia Wang 	}
488d69607b3SLydia Wang }
489d69607b3SLydia Wang 
49009a9ad69STakashi Iwai /* enable/disable the output-route */
49109a9ad69STakashi Iwai static void activate_output_path(struct hda_codec *codec, struct nid_path *path,
49209a9ad69STakashi Iwai 				 bool enable, bool force)
4935d41762aSTakashi Iwai {
494d69607b3SLydia Wang 	struct via_spec *spec = codec->spec;
4953214b966STakashi Iwai 	int i;
49609a9ad69STakashi Iwai 	for (i = 0; i < path->depth; i++) {
49709a9ad69STakashi Iwai 		hda_nid_t src, dst;
49809a9ad69STakashi Iwai 		int idx = path->idx[i];
49909a9ad69STakashi Iwai 		src = path->path[i];
50009a9ad69STakashi Iwai 		if (i < path->depth - 1)
50109a9ad69STakashi Iwai 			dst = path->path[i + 1];
50209a9ad69STakashi Iwai 		else
50309a9ad69STakashi Iwai 			dst = 0;
50409a9ad69STakashi Iwai 		if (enable && path->multi[i])
50509a9ad69STakashi Iwai 			snd_hda_codec_write(codec, dst, 0,
5065d41762aSTakashi Iwai 					    AC_VERB_SET_CONNECT_SEL, idx);
5073214b966STakashi Iwai 		if (!force && (dst == spec->aa_mix_nid))
508e5e14681SLydia Wang 			continue;
5093214b966STakashi Iwai 		if (have_mute(codec, dst, HDA_INPUT))
5103214b966STakashi Iwai 			activate_output_mix(codec, path, dst, idx, enable);
51109a9ad69STakashi Iwai 		if (!force && (src == path->vol_ctl || src == path->mute_ctl))
51209a9ad69STakashi Iwai 			continue;
51309a9ad69STakashi Iwai 		if (have_mute(codec, src, HDA_OUTPUT)) {
51409a9ad69STakashi Iwai 			int val = enable ? AMP_OUT_UNMUTE : AMP_OUT_MUTE;
51509a9ad69STakashi Iwai 			snd_hda_codec_write(codec, src, 0,
51609a9ad69STakashi Iwai 					    AC_VERB_SET_AMP_GAIN_MUTE, val);
51709a9ad69STakashi Iwai 		}
51809a9ad69STakashi Iwai 	}
5195d41762aSTakashi Iwai }
5205d41762aSTakashi Iwai 
5215d41762aSTakashi Iwai /* set the given pin as output */
5225d41762aSTakashi Iwai static void init_output_pin(struct hda_codec *codec, hda_nid_t pin,
5235d41762aSTakashi Iwai 			    int pin_type)
5245d41762aSTakashi Iwai {
5255d41762aSTakashi Iwai 	if (!pin)
5265d41762aSTakashi Iwai 		return;
5275d41762aSTakashi Iwai 	snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
5285d41762aSTakashi Iwai 			    pin_type);
5295d41762aSTakashi Iwai 	if (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD)
5305d41762aSTakashi Iwai 		snd_hda_codec_write(codec, pin, 0,
531d3a11e60STakashi Iwai 				    AC_VERB_SET_EAPD_BTLENABLE, 0x02);
532c577b8a1SJoseph Chan }
533c577b8a1SJoseph Chan 
53409a9ad69STakashi Iwai static void via_auto_init_output(struct hda_codec *codec,
535a353fbb1STakashi Iwai 				 struct nid_path *path, int pin_type)
5365d41762aSTakashi Iwai {
5375d41762aSTakashi Iwai 	unsigned int caps;
538d69607b3SLydia Wang 	hda_nid_t pin;
5395d41762aSTakashi Iwai 
54009a9ad69STakashi Iwai 	if (!path->depth)
5415d41762aSTakashi Iwai 		return;
54209a9ad69STakashi Iwai 	pin = path->path[path->depth - 1];
5435d41762aSTakashi Iwai 
5445d41762aSTakashi Iwai 	init_output_pin(codec, pin, pin_type);
5455d41762aSTakashi Iwai 	caps = query_amp_caps(codec, pin, HDA_OUTPUT);
5465d41762aSTakashi Iwai 	if (caps & AC_AMPCAP_MUTE) {
5475d41762aSTakashi Iwai 		unsigned int val;
5485d41762aSTakashi Iwai 		val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
5495d41762aSTakashi Iwai 		snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
5505d41762aSTakashi Iwai 				    AMP_OUT_MUTE | val);
5515d41762aSTakashi Iwai 	}
552a353fbb1STakashi Iwai 	activate_output_path(codec, path, true, true); /* force on */
55309a9ad69STakashi Iwai }
554c577b8a1SJoseph Chan 
555c577b8a1SJoseph Chan static void via_auto_init_multi_out(struct hda_codec *codec)
556c577b8a1SJoseph Chan {
557c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
5583214b966STakashi Iwai 	struct nid_path *path;
559c577b8a1SJoseph Chan 	int i;
560c577b8a1SJoseph Chan 
5613214b966STakashi Iwai 	for (i = 0; i < spec->autocfg.line_outs + spec->smart51_nums; i++) {
5623214b966STakashi Iwai 		path = &spec->out_path[i];
5633214b966STakashi Iwai 		if (!i && spec->aamix_mode && spec->out_mix_path.depth)
5643214b966STakashi Iwai 			path = &spec->out_mix_path;
565a353fbb1STakashi Iwai 		via_auto_init_output(codec, path, PIN_OUT);
5663214b966STakashi Iwai 	}
567c577b8a1SJoseph Chan }
568c577b8a1SJoseph Chan 
569020066d1STakashi Iwai /* deactivate the inactive headphone-paths */
570020066d1STakashi Iwai static void deactivate_hp_paths(struct hda_codec *codec)
571c577b8a1SJoseph Chan {
572c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
5733214b966STakashi Iwai 	int shared = spec->hp_indep_shared;
574c577b8a1SJoseph Chan 
57509a9ad69STakashi Iwai 	if (spec->hp_independent_mode) {
57609a9ad69STakashi Iwai 		activate_output_path(codec, &spec->hp_path, false, false);
5773214b966STakashi Iwai 		activate_output_path(codec, &spec->hp_mix_path, false, false);
5783214b966STakashi Iwai 		if (shared)
5793214b966STakashi Iwai 			activate_output_path(codec, &spec->out_path[shared],
5803214b966STakashi Iwai 					     false, false);
581020066d1STakashi Iwai 	} else if (spec->aamix_mode || !spec->hp_path.depth) {
582020066d1STakashi Iwai 		activate_output_path(codec, &spec->hp_indep_path, false, false);
5833214b966STakashi Iwai 		activate_output_path(codec, &spec->hp_path, false, false);
5843214b966STakashi Iwai 	} else {
585020066d1STakashi Iwai 		activate_output_path(codec, &spec->hp_indep_path, false, false);
5863214b966STakashi Iwai 		activate_output_path(codec, &spec->hp_mix_path, false, false);
58709a9ad69STakashi Iwai 	}
58825eaba2fSLydia Wang }
589c577b8a1SJoseph Chan 
590020066d1STakashi Iwai static void via_auto_init_hp_out(struct hda_codec *codec)
591020066d1STakashi Iwai {
592020066d1STakashi Iwai 	struct via_spec *spec = codec->spec;
593020066d1STakashi Iwai 
594020066d1STakashi Iwai 	if (!spec->hp_path.depth) {
595a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
596020066d1STakashi Iwai 		return;
597020066d1STakashi Iwai 	}
598020066d1STakashi Iwai 	deactivate_hp_paths(codec);
599020066d1STakashi Iwai 	if (spec->hp_independent_mode)
600a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->hp_indep_path, PIN_HP);
601020066d1STakashi Iwai 	else if (spec->aamix_mode)
602a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
603020066d1STakashi Iwai 	else
604a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->hp_path, PIN_HP);
605020066d1STakashi Iwai }
606020066d1STakashi Iwai 
6074a918ffeSTakashi Iwai static void via_auto_init_speaker_out(struct hda_codec *codec)
6084a918ffeSTakashi Iwai {
6094a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
6104a918ffeSTakashi Iwai 
6113214b966STakashi Iwai 	if (!spec->autocfg.speaker_outs)
6123214b966STakashi Iwai 		return;
6133214b966STakashi Iwai 	if (!spec->speaker_path.depth) {
614a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
6153214b966STakashi Iwai 		return;
6163214b966STakashi Iwai 	}
6173214b966STakashi Iwai 	if (!spec->aamix_mode) {
6183214b966STakashi Iwai 		activate_output_path(codec, &spec->speaker_mix_path,
6193214b966STakashi Iwai 				     false, false);
620a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->speaker_path, PIN_OUT);
6213214b966STakashi Iwai 	} else {
6223214b966STakashi Iwai 		activate_output_path(codec, &spec->speaker_path, false, false);
623a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
6243214b966STakashi Iwai 	}
6254a918ffeSTakashi Iwai }
6264a918ffeSTakashi Iwai 
627f4a7828bSTakashi Iwai static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin);
6286e969d91STakashi Iwai static void via_hp_automute(struct hda_codec *codec);
62932e0191dSClemens Ladisch 
630c577b8a1SJoseph Chan static void via_auto_init_analog_input(struct hda_codec *codec)
631c577b8a1SJoseph Chan {
632c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
6337b315bb4STakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
634096a8854STakashi Iwai 	hda_nid_t conn[HDA_MAX_CONNECTIONS];
63532e0191dSClemens Ladisch 	unsigned int ctl;
636096a8854STakashi Iwai 	int i, num_conns;
637c577b8a1SJoseph Chan 
638096a8854STakashi Iwai 	/* init ADCs */
639096a8854STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
640096a8854STakashi Iwai 		snd_hda_codec_write(codec, spec->adc_nids[i], 0,
641096a8854STakashi Iwai 				    AC_VERB_SET_AMP_GAIN_MUTE,
642096a8854STakashi Iwai 				    AMP_IN_UNMUTE(0));
643096a8854STakashi Iwai 	}
644096a8854STakashi Iwai 
645096a8854STakashi Iwai 	/* init pins */
6467b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
6477b315bb4STakashi Iwai 		hda_nid_t nid = cfg->inputs[i].pin;
648f4a7828bSTakashi Iwai 		if (spec->smart51_enabled && is_smart51_pins(codec, nid))
64932e0191dSClemens Ladisch 			ctl = PIN_OUT;
65030649676SDavid Henningsson 		else if (cfg->inputs[i].type == AUTO_PIN_MIC)
65132e0191dSClemens Ladisch 			ctl = PIN_VREF50;
65232e0191dSClemens Ladisch 		else
65332e0191dSClemens Ladisch 			ctl = PIN_IN;
654c577b8a1SJoseph Chan 		snd_hda_codec_write(codec, nid, 0,
65532e0191dSClemens Ladisch 				    AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
656c577b8a1SJoseph Chan 	}
657096a8854STakashi Iwai 
658096a8854STakashi Iwai 	/* init input-src */
659096a8854STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
660a86a88eaSTakashi Iwai 		int adc_idx = spec->inputs[spec->cur_mux[i]].adc_idx;
661a86a88eaSTakashi Iwai 		if (spec->mux_nids[adc_idx]) {
662a86a88eaSTakashi Iwai 			int mux_idx = spec->inputs[spec->cur_mux[i]].mux_idx;
663a86a88eaSTakashi Iwai 			snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
664096a8854STakashi Iwai 					    AC_VERB_SET_CONNECT_SEL,
665a86a88eaSTakashi Iwai 					    mux_idx);
666a86a88eaSTakashi Iwai 		}
667a86a88eaSTakashi Iwai 		if (spec->dyn_adc_switch)
668a86a88eaSTakashi Iwai 			break; /* only one input-src */
669096a8854STakashi Iwai 	}
670096a8854STakashi Iwai 
671096a8854STakashi Iwai 	/* init aa-mixer */
672096a8854STakashi Iwai 	if (!spec->aa_mix_nid)
673096a8854STakashi Iwai 		return;
674096a8854STakashi Iwai 	num_conns = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
675096a8854STakashi Iwai 					    ARRAY_SIZE(conn));
676096a8854STakashi Iwai 	for (i = 0; i < num_conns; i++) {
677096a8854STakashi Iwai 		unsigned int caps = get_wcaps(codec, conn[i]);
678096a8854STakashi Iwai 		if (get_wcaps_type(caps) == AC_WID_PIN)
679096a8854STakashi Iwai 			snd_hda_codec_write(codec, spec->aa_mix_nid, 0,
680096a8854STakashi Iwai 					    AC_VERB_SET_AMP_GAIN_MUTE,
681096a8854STakashi Iwai 					    AMP_IN_MUTE(i));
682096a8854STakashi Iwai 	}
683c577b8a1SJoseph Chan }
684f5271101SLydia Wang 
685f5271101SLydia Wang static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
686f5271101SLydia Wang 				unsigned int *affected_parm)
687f5271101SLydia Wang {
688f5271101SLydia Wang 	unsigned parm;
689f5271101SLydia Wang 	unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
690f5271101SLydia Wang 	unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
691f5271101SLydia Wang 		>> AC_DEFCFG_MISC_SHIFT
692f5271101SLydia Wang 		& AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
6931564b287SLydia Wang 	struct via_spec *spec = codec->spec;
69424088a58STakashi Iwai 	unsigned present = 0;
69524088a58STakashi Iwai 
69624088a58STakashi Iwai 	no_presence |= spec->no_pin_power_ctl;
69724088a58STakashi Iwai 	if (!no_presence)
69824088a58STakashi Iwai 		present = snd_hda_jack_detect(codec, nid);
699f4a7828bSTakashi Iwai 	if ((spec->smart51_enabled && is_smart51_pins(codec, nid))
7001564b287SLydia Wang 	    || ((no_presence || present)
7011564b287SLydia Wang 		&& get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
702f5271101SLydia Wang 		*affected_parm = AC_PWRST_D0; /* if it's connected */
703f5271101SLydia Wang 		parm = AC_PWRST_D0;
704f5271101SLydia Wang 	} else
705f5271101SLydia Wang 		parm = AC_PWRST_D3;
706f5271101SLydia Wang 
707f5271101SLydia Wang 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
708f5271101SLydia Wang }
709f5271101SLydia Wang 
71024088a58STakashi Iwai static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
71124088a58STakashi Iwai 				  struct snd_ctl_elem_info *uinfo)
71224088a58STakashi Iwai {
71324088a58STakashi Iwai 	static const char * const texts[] = {
71424088a58STakashi Iwai 		"Disabled", "Enabled"
71524088a58STakashi Iwai 	};
71624088a58STakashi Iwai 
71724088a58STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
71824088a58STakashi Iwai 	uinfo->count = 1;
71924088a58STakashi Iwai 	uinfo->value.enumerated.items = 2;
72024088a58STakashi Iwai 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
72124088a58STakashi Iwai 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
72224088a58STakashi Iwai 	strcpy(uinfo->value.enumerated.name,
72324088a58STakashi Iwai 	       texts[uinfo->value.enumerated.item]);
72424088a58STakashi Iwai 	return 0;
72524088a58STakashi Iwai }
72624088a58STakashi Iwai 
72724088a58STakashi Iwai static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
72824088a58STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
72924088a58STakashi Iwai {
73024088a58STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
73124088a58STakashi Iwai 	struct via_spec *spec = codec->spec;
73224088a58STakashi Iwai 	ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
73324088a58STakashi Iwai 	return 0;
73424088a58STakashi Iwai }
73524088a58STakashi Iwai 
73624088a58STakashi Iwai static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
73724088a58STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
73824088a58STakashi Iwai {
73924088a58STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
74024088a58STakashi Iwai 	struct via_spec *spec = codec->spec;
74124088a58STakashi Iwai 	unsigned int val = !ucontrol->value.enumerated.item[0];
74224088a58STakashi Iwai 
74324088a58STakashi Iwai 	if (val == spec->no_pin_power_ctl)
74424088a58STakashi Iwai 		return 0;
74524088a58STakashi Iwai 	spec->no_pin_power_ctl = val;
74624088a58STakashi Iwai 	set_widgets_power_state(codec);
74724088a58STakashi Iwai 	return 1;
74824088a58STakashi Iwai }
74924088a58STakashi Iwai 
75024088a58STakashi Iwai static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
75124088a58STakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
75224088a58STakashi Iwai 	.name = "Dynamic Power-Control",
75324088a58STakashi Iwai 	.info = via_pin_power_ctl_info,
75424088a58STakashi Iwai 	.get = via_pin_power_ctl_get,
75524088a58STakashi Iwai 	.put = via_pin_power_ctl_put,
75624088a58STakashi Iwai };
75724088a58STakashi Iwai 
75824088a58STakashi Iwai 
7590aa62aefSHarald Welte static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
7600aa62aefSHarald Welte 				   struct snd_ctl_elem_info *uinfo)
7610aa62aefSHarald Welte {
7628df2a312STakashi Iwai 	static const char * const texts[] = { "OFF", "ON" };
7638df2a312STakashi Iwai 
7648df2a312STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
7658df2a312STakashi Iwai 	uinfo->count = 1;
7668df2a312STakashi Iwai 	uinfo->value.enumerated.items = 2;
7678df2a312STakashi Iwai 	if (uinfo->value.enumerated.item >= 2)
7688df2a312STakashi Iwai 		uinfo->value.enumerated.item = 1;
7698df2a312STakashi Iwai 	strcpy(uinfo->value.enumerated.name,
7708df2a312STakashi Iwai 	       texts[uinfo->value.enumerated.item]);
7718df2a312STakashi Iwai 	return 0;
7720aa62aefSHarald Welte }
7730aa62aefSHarald Welte 
7740aa62aefSHarald Welte static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
7750aa62aefSHarald Welte 				  struct snd_ctl_elem_value *ucontrol)
7760aa62aefSHarald Welte {
7770aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
778cdc1784dSLydia Wang 	struct via_spec *spec = codec->spec;
779cdc1784dSLydia Wang 
780ece8d043STakashi Iwai 	ucontrol->value.enumerated.item[0] = spec->hp_independent_mode;
781cdc1784dSLydia Wang 	return 0;
782cdc1784dSLydia Wang }
783cdc1784dSLydia Wang 
7843b607e3dSTakashi Iwai /* adjust spec->multiout setup according to the current flags */
7853b607e3dSTakashi Iwai static void setup_playback_multi_pcm(struct via_spec *spec)
7863b607e3dSTakashi Iwai {
7873b607e3dSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
7883b607e3dSTakashi Iwai 	spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
7893b607e3dSTakashi Iwai 	spec->multiout.hp_nid = 0;
7903b607e3dSTakashi Iwai 	if (!spec->hp_independent_mode) {
7913b607e3dSTakashi Iwai 		if (!spec->hp_indep_shared)
7923b607e3dSTakashi Iwai 			spec->multiout.hp_nid = spec->hp_dac_nid;
7933b607e3dSTakashi Iwai 	} else {
7943b607e3dSTakashi Iwai 		if (spec->hp_indep_shared)
7953b607e3dSTakashi Iwai 			spec->multiout.num_dacs = cfg->line_outs - 1;
7963b607e3dSTakashi Iwai 	}
7973b607e3dSTakashi Iwai }
7983b607e3dSTakashi Iwai 
7993b607e3dSTakashi Iwai /* update DAC setups according to indep-HP switch;
8003b607e3dSTakashi Iwai  * this function is called only when indep-HP is modified
8013b607e3dSTakashi Iwai  */
8023b607e3dSTakashi Iwai static void switch_indep_hp_dacs(struct hda_codec *codec)
8033b607e3dSTakashi Iwai {
8043b607e3dSTakashi Iwai 	struct via_spec *spec = codec->spec;
8053b607e3dSTakashi Iwai 	int shared = spec->hp_indep_shared;
8063b607e3dSTakashi Iwai 	hda_nid_t shared_dac, hp_dac;
8073b607e3dSTakashi Iwai 
8083b607e3dSTakashi Iwai 	if (!spec->opened_streams)
8093b607e3dSTakashi Iwai 		return;
8103b607e3dSTakashi Iwai 
8113b607e3dSTakashi Iwai 	shared_dac = shared ? spec->multiout.dac_nids[shared] : 0;
8123b607e3dSTakashi Iwai 	hp_dac = spec->hp_dac_nid;
8133b607e3dSTakashi Iwai 	if (spec->hp_independent_mode) {
8143b607e3dSTakashi Iwai 		/* switch to indep-HP mode */
8153b607e3dSTakashi Iwai 		if (spec->active_streams & STREAM_MULTI_OUT) {
8163b607e3dSTakashi Iwai 			__snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
8173b607e3dSTakashi Iwai 			__snd_hda_codec_cleanup_stream(codec, shared_dac, 1);
8183b607e3dSTakashi Iwai 		}
8193b607e3dSTakashi Iwai 		if (spec->active_streams & STREAM_INDEP_HP)
8203b607e3dSTakashi Iwai 			snd_hda_codec_setup_stream(codec, hp_dac,
8213b607e3dSTakashi Iwai 						   spec->cur_hp_stream_tag, 0,
8223b607e3dSTakashi Iwai 						   spec->cur_hp_format);
8233b607e3dSTakashi Iwai 	} else {
8243b607e3dSTakashi Iwai 		/* back to HP or shared-DAC */
8253b607e3dSTakashi Iwai 		if (spec->active_streams & STREAM_INDEP_HP)
8263b607e3dSTakashi Iwai 			__snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
8273b607e3dSTakashi Iwai 		if (spec->active_streams & STREAM_MULTI_OUT) {
8283b607e3dSTakashi Iwai 			hda_nid_t dac;
8293b607e3dSTakashi Iwai 			int ch;
8303b607e3dSTakashi Iwai 			if (shared_dac) { /* reset mutli-ch DAC */
8313b607e3dSTakashi Iwai 				dac = shared_dac;
8323b607e3dSTakashi Iwai 				ch = shared * 2;
8333b607e3dSTakashi Iwai 			} else { /* reset HP DAC */
8343b607e3dSTakashi Iwai 				dac = hp_dac;
8353b607e3dSTakashi Iwai 				ch = 0;
8363b607e3dSTakashi Iwai 			}
8373b607e3dSTakashi Iwai 			snd_hda_codec_setup_stream(codec, dac,
8383b607e3dSTakashi Iwai 						   spec->cur_dac_stream_tag, ch,
8393b607e3dSTakashi Iwai 						   spec->cur_dac_format);
8403b607e3dSTakashi Iwai 		}
8413b607e3dSTakashi Iwai 	}
8423b607e3dSTakashi Iwai 	setup_playback_multi_pcm(spec);
8433b607e3dSTakashi Iwai }
8443b607e3dSTakashi Iwai 
8450aa62aefSHarald Welte static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
8460aa62aefSHarald Welte 				  struct snd_ctl_elem_value *ucontrol)
8470aa62aefSHarald Welte {
8480aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
8490aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
8503214b966STakashi Iwai 	int cur, shared;
8518df2a312STakashi Iwai 
8523b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
85325250505STakashi Iwai 	cur = !!ucontrol->value.enumerated.item[0];
8543b607e3dSTakashi Iwai 	if (spec->hp_independent_mode == cur) {
8553b607e3dSTakashi Iwai 		mutex_unlock(&spec->config_mutex);
85625250505STakashi Iwai 		return 0;
8573b607e3dSTakashi Iwai 	}
85825250505STakashi Iwai 	spec->hp_independent_mode = cur;
8593214b966STakashi Iwai 	shared = spec->hp_indep_shared;
860020066d1STakashi Iwai 	deactivate_hp_paths(codec);
861020066d1STakashi Iwai 	if (cur)
862020066d1STakashi Iwai 		activate_output_path(codec, &spec->hp_indep_path, true, false);
863020066d1STakashi Iwai 	else {
8643214b966STakashi Iwai 		if (shared)
8653214b966STakashi Iwai 			activate_output_path(codec, &spec->out_path[shared],
86625250505STakashi Iwai 					     true, false);
867020066d1STakashi Iwai 		if (spec->aamix_mode || !spec->hp_path.depth)
868020066d1STakashi Iwai 			activate_output_path(codec, &spec->hp_mix_path,
869020066d1STakashi Iwai 					     true, false);
870020066d1STakashi Iwai 		else
871020066d1STakashi Iwai 			activate_output_path(codec, &spec->hp_path,
872020066d1STakashi Iwai 					     true, false);
8738df2a312STakashi Iwai 	}
8740aa62aefSHarald Welte 
8753b607e3dSTakashi Iwai 	switch_indep_hp_dacs(codec);
8763b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
8773b607e3dSTakashi Iwai 
878ce0e5a9eSLydia Wang 	/* update jack power state */
8793e95b9abSLydia Wang 	set_widgets_power_state(codec);
8806e969d91STakashi Iwai 	via_hp_automute(codec);
88125250505STakashi Iwai 	return 1;
8820aa62aefSHarald Welte }
8830aa62aefSHarald Welte 
884ece8d043STakashi Iwai static const struct snd_kcontrol_new via_hp_mixer = {
8850aa62aefSHarald Welte 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
8860aa62aefSHarald Welte 	.name = "Independent HP",
8870aa62aefSHarald Welte 	.info = via_independent_hp_info,
8880aa62aefSHarald Welte 	.get = via_independent_hp_get,
8890aa62aefSHarald Welte 	.put = via_independent_hp_put,
8900aa62aefSHarald Welte };
8910aa62aefSHarald Welte 
8923d83e577STakashi Iwai static int via_hp_build(struct hda_codec *codec)
8935b0cb1d8SJaroslav Kysela {
8943d83e577STakashi Iwai 	struct via_spec *spec = codec->spec;
8955b0cb1d8SJaroslav Kysela 	struct snd_kcontrol_new *knew;
8965b0cb1d8SJaroslav Kysela 	hda_nid_t nid;
8975b0cb1d8SJaroslav Kysela 
8985b0cb1d8SJaroslav Kysela 	nid = spec->autocfg.hp_pins[0];
899ece8d043STakashi Iwai 	knew = via_clone_control(spec, &via_hp_mixer);
9003d83e577STakashi Iwai 	if (knew == NULL)
9013d83e577STakashi Iwai 		return -ENOMEM;
9023d83e577STakashi Iwai 
9035b0cb1d8SJaroslav Kysela 	knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
9045b0cb1d8SJaroslav Kysela 
9055b0cb1d8SJaroslav Kysela 	return 0;
9065b0cb1d8SJaroslav Kysela }
9075b0cb1d8SJaroslav Kysela 
9081564b287SLydia Wang static void notify_aa_path_ctls(struct hda_codec *codec)
9091564b287SLydia Wang {
910e3d7a143STakashi Iwai 	struct via_spec *spec = codec->spec;
9111564b287SLydia Wang 	int i;
9121564b287SLydia Wang 
913e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
914e3d7a143STakashi Iwai 		struct snd_kcontrol *ctl;
915e3d7a143STakashi Iwai 		struct snd_ctl_elem_id id;
9161564b287SLydia Wang 		memset(&id, 0, sizeof(id));
9171564b287SLydia Wang 		id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
918e3d7a143STakashi Iwai 		sprintf(id.name, "%s Playback Volume", spec->smart51_labels[i]);
919525566cbSLydia Wang 		ctl = snd_hda_find_mixer_ctl(codec, id.name);
920525566cbSLydia Wang 		if (ctl)
921525566cbSLydia Wang 			snd_ctl_notify(codec->bus->card,
922525566cbSLydia Wang 					SNDRV_CTL_EVENT_MASK_VALUE,
923525566cbSLydia Wang 					&ctl->id);
9241564b287SLydia Wang 	}
9251564b287SLydia Wang }
9261564b287SLydia Wang 
9271564b287SLydia Wang static void mute_aa_path(struct hda_codec *codec, int mute)
9281564b287SLydia Wang {
9291564b287SLydia Wang 	struct via_spec *spec = codec->spec;
9301564b287SLydia Wang 	int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
931e3d7a143STakashi Iwai 	int i;
932e3d7a143STakashi Iwai 
933e3d7a143STakashi Iwai 	/* check AA path's mute status */
934e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
935e3d7a143STakashi Iwai 		if (spec->smart51_idxs[i] < 0)
936e3d7a143STakashi Iwai 			continue;
937e3d7a143STakashi Iwai 		snd_hda_codec_amp_stereo(codec, spec->aa_mix_nid,
938e3d7a143STakashi Iwai 					 HDA_INPUT, spec->smart51_idxs[i],
9391564b287SLydia Wang 					 HDA_AMP_MUTE, val);
9401564b287SLydia Wang 	}
9411564b287SLydia Wang }
942f4a7828bSTakashi Iwai 
943e3d7a143STakashi Iwai static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
944e3d7a143STakashi Iwai {
945e3d7a143STakashi Iwai 	struct via_spec *spec = codec->spec;
946e3d7a143STakashi Iwai 	int i;
947e3d7a143STakashi Iwai 
948e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++)
949e3d7a143STakashi Iwai 		if (spec->smart51_pins[i] == pin)
950e3d7a143STakashi Iwai 			return true;
951e3d7a143STakashi Iwai 	return false;
952e3d7a143STakashi Iwai }
953e3d7a143STakashi Iwai 
9541564b287SLydia Wang static int via_smart51_get(struct snd_kcontrol *kcontrol,
9551564b287SLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
9561564b287SLydia Wang {
9571564b287SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
9581564b287SLydia Wang 	struct via_spec *spec = codec->spec;
9591564b287SLydia Wang 
960f2b1c9f0STakashi Iwai 	*ucontrol->value.integer.value = spec->smart51_enabled;
9611564b287SLydia Wang 	return 0;
9621564b287SLydia Wang }
9631564b287SLydia Wang 
9641564b287SLydia Wang static int via_smart51_put(struct snd_kcontrol *kcontrol,
9651564b287SLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
9661564b287SLydia Wang {
9671564b287SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
9681564b287SLydia Wang 	struct via_spec *spec = codec->spec;
9691564b287SLydia Wang 	int out_in = *ucontrol->value.integer.value
9701564b287SLydia Wang 		? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
9711564b287SLydia Wang 	int i;
9721564b287SLydia Wang 
973e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
974e3d7a143STakashi Iwai 		hda_nid_t nid = spec->smart51_pins[i];
9757b315bb4STakashi Iwai 		unsigned int parm;
9767b315bb4STakashi Iwai 
9777b315bb4STakashi Iwai 		parm = snd_hda_codec_read(codec, nid, 0,
9781564b287SLydia Wang 					  AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
9791564b287SLydia Wang 		parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
9801564b287SLydia Wang 		parm |= out_in;
9811564b287SLydia Wang 		snd_hda_codec_write(codec, nid, 0,
9821564b287SLydia Wang 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
9831564b287SLydia Wang 				    parm);
9841564b287SLydia Wang 		if (out_in == AC_PINCTL_OUT_EN) {
9851564b287SLydia Wang 			mute_aa_path(codec, 1);
9861564b287SLydia Wang 			notify_aa_path_ctls(codec);
9871564b287SLydia Wang 		}
9881564b287SLydia Wang 	}
9891564b287SLydia Wang 	spec->smart51_enabled = *ucontrol->value.integer.value;
9903e95b9abSLydia Wang 	set_widgets_power_state(codec);
9911564b287SLydia Wang 	return 1;
9921564b287SLydia Wang }
9931564b287SLydia Wang 
9945f4b36d6STakashi Iwai static const struct snd_kcontrol_new via_smart51_mixer = {
9951564b287SLydia Wang 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
9961564b287SLydia Wang 	.name = "Smart 5.1",
9971564b287SLydia Wang 	.count = 1,
998f2b1c9f0STakashi Iwai 	.info = snd_ctl_boolean_mono_info,
9991564b287SLydia Wang 	.get = via_smart51_get,
10001564b287SLydia Wang 	.put = via_smart51_put,
10011564b287SLydia Wang };
10021564b287SLydia Wang 
1003f4a7828bSTakashi Iwai static int via_smart51_build(struct hda_codec *codec)
10045b0cb1d8SJaroslav Kysela {
1005f4a7828bSTakashi Iwai 	struct via_spec *spec = codec->spec;
10065b0cb1d8SJaroslav Kysela 
1007e3d7a143STakashi Iwai 	if (!spec->smart51_nums)
1008cb34c207SLydia Wang 		return 0;
1009e3d7a143STakashi Iwai 	if (!via_clone_control(spec, &via_smart51_mixer))
10105b0cb1d8SJaroslav Kysela 		return -ENOMEM;
10115b0cb1d8SJaroslav Kysela 	return 0;
10125b0cb1d8SJaroslav Kysela }
10135b0cb1d8SJaroslav Kysela 
1014f5271101SLydia Wang /* check AA path's mute status */
1015ada509ecSTakashi Iwai static bool is_aa_path_mute(struct hda_codec *codec)
1016ada509ecSTakashi Iwai {
1017ada509ecSTakashi Iwai 	struct via_spec *spec = codec->spec;
1018ada509ecSTakashi Iwai 	const struct hda_amp_list *p;
1019ada509ecSTakashi Iwai 	int i, ch, v;
1020ada509ecSTakashi Iwai 
1021ada509ecSTakashi Iwai 	for (i = 0; i < spec->num_loopbacks; i++) {
1022ada509ecSTakashi Iwai 		p = &spec->loopback_list[i];
1023ada509ecSTakashi Iwai 		for (ch = 0; ch < 2; ch++) {
1024ada509ecSTakashi Iwai 			v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
1025ada509ecSTakashi Iwai 						   p->idx);
1026ada509ecSTakashi Iwai 			if (!(v & HDA_AMP_MUTE) && v > 0)
1027ada509ecSTakashi Iwai 				return false;
1028f5271101SLydia Wang 		}
1029f5271101SLydia Wang 	}
1030ada509ecSTakashi Iwai 	return true;
1031f5271101SLydia Wang }
1032f5271101SLydia Wang 
1033f5271101SLydia Wang /* enter/exit analog low-current mode */
1034ada509ecSTakashi Iwai static void analog_low_current_mode(struct hda_codec *codec)
1035f5271101SLydia Wang {
1036f5271101SLydia Wang 	struct via_spec *spec = codec->spec;
1037ada509ecSTakashi Iwai 	bool enable;
1038ada509ecSTakashi Iwai 	unsigned int verb, parm;
1039f5271101SLydia Wang 
10403b607e3dSTakashi Iwai 	enable = is_aa_path_mute(codec) && (spec->opened_streams != 0);
1041f5271101SLydia Wang 
1042f5271101SLydia Wang 	/* decide low current mode's verb & parameter */
1043f5271101SLydia Wang 	switch (spec->codec_type) {
1044f5271101SLydia Wang 	case VT1708B_8CH:
1045f5271101SLydia Wang 	case VT1708B_4CH:
1046f5271101SLydia Wang 		verb = 0xf70;
1047f5271101SLydia Wang 		parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
1048f5271101SLydia Wang 		break;
1049f5271101SLydia Wang 	case VT1708S:
1050eb7188caSLydia Wang 	case VT1718S:
1051f3db423dSLydia Wang 	case VT1716S:
1052f5271101SLydia Wang 		verb = 0xf73;
1053f5271101SLydia Wang 		parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
1054f5271101SLydia Wang 		break;
1055f5271101SLydia Wang 	case VT1702:
1056f5271101SLydia Wang 		verb = 0xf73;
1057f5271101SLydia Wang 		parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
1058f5271101SLydia Wang 		break;
105925eaba2fSLydia Wang 	case VT2002P:
1060ab6734e7SLydia Wang 	case VT1812:
106111890956SLydia Wang 	case VT1802:
106225eaba2fSLydia Wang 		verb = 0xf93;
106325eaba2fSLydia Wang 		parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
106425eaba2fSLydia Wang 		break;
1065f5271101SLydia Wang 	default:
1066f5271101SLydia Wang 		return;		/* other codecs are not supported */
1067f5271101SLydia Wang 	}
1068f5271101SLydia Wang 	/* send verb */
1069f5271101SLydia Wang 	snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
1070f5271101SLydia Wang }
1071f5271101SLydia Wang 
1072c577b8a1SJoseph Chan /*
1073c577b8a1SJoseph Chan  * generic initialization of ADC, input mixers and output mixers
1074c577b8a1SJoseph Chan  */
1075096a8854STakashi Iwai static const struct hda_verb vt1708_init_verbs[] = {
1076aa266fccSLydia Wang 	/* power down jack detect function */
1077aa266fccSLydia Wang 	{0x1, 0xf81, 0x1},
1078f7278fd0SJosepch Chan 	{ }
1079c577b8a1SJoseph Chan };
1080c577b8a1SJoseph Chan 
10813b607e3dSTakashi Iwai static void set_stream_open(struct hda_codec *codec, int bit, bool active)
10827eb56e84STakashi Iwai {
1083ada509ecSTakashi Iwai 	struct via_spec *spec = codec->spec;
1084ada509ecSTakashi Iwai 
1085ada509ecSTakashi Iwai 	if (active)
10863b607e3dSTakashi Iwai 		spec->opened_streams |= bit;
1087ada509ecSTakashi Iwai 	else
10883b607e3dSTakashi Iwai 		spec->opened_streams &= ~bit;
1089ada509ecSTakashi Iwai 	analog_low_current_mode(codec);
10907eb56e84STakashi Iwai }
10917eb56e84STakashi Iwai 
1092ece8d043STakashi Iwai static int via_playback_multi_pcm_open(struct hda_pcm_stream *hinfo,
1093c577b8a1SJoseph Chan 				 struct hda_codec *codec,
1094c577b8a1SJoseph Chan 				 struct snd_pcm_substream *substream)
1095c577b8a1SJoseph Chan {
1096c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
109725250505STakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
1098ada509ecSTakashi Iwai 	int err;
1099ece8d043STakashi Iwai 
110025250505STakashi Iwai 	spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
110125250505STakashi Iwai 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
11023b607e3dSTakashi Iwai 	set_stream_open(codec, STREAM_MULTI_OUT, true);
1103ada509ecSTakashi Iwai 	err = snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
11049a08160bSTakashi Iwai 					    hinfo);
1105ada509ecSTakashi Iwai 	if (err < 0) {
11063b607e3dSTakashi Iwai 		set_stream_open(codec, STREAM_MULTI_OUT, false);
1107ada509ecSTakashi Iwai 		return err;
1108ada509ecSTakashi Iwai 	}
1109ada509ecSTakashi Iwai 	return 0;
1110c577b8a1SJoseph Chan }
1111c577b8a1SJoseph Chan 
1112ece8d043STakashi Iwai static int via_playback_multi_pcm_close(struct hda_pcm_stream *hinfo,
11139af74210STakashi Iwai 				  struct hda_codec *codec,
11149af74210STakashi Iwai 				  struct snd_pcm_substream *substream)
11159af74210STakashi Iwai {
11163b607e3dSTakashi Iwai 	set_stream_open(codec, STREAM_MULTI_OUT, false);
11179af74210STakashi Iwai 	return 0;
11189af74210STakashi Iwai }
11199af74210STakashi Iwai 
11207eb56e84STakashi Iwai static int via_playback_hp_pcm_open(struct hda_pcm_stream *hinfo,
11217eb56e84STakashi Iwai 				    struct hda_codec *codec,
11227eb56e84STakashi Iwai 				    struct snd_pcm_substream *substream)
11237eb56e84STakashi Iwai {
11247eb56e84STakashi Iwai 	struct via_spec *spec = codec->spec;
11257eb56e84STakashi Iwai 
1126ece8d043STakashi Iwai 	if (snd_BUG_ON(!spec->hp_dac_nid))
11277eb56e84STakashi Iwai 		return -EINVAL;
11283b607e3dSTakashi Iwai 	set_stream_open(codec, STREAM_INDEP_HP, true);
1129ece8d043STakashi Iwai 	return 0;
1130ece8d043STakashi Iwai }
1131ece8d043STakashi Iwai 
1132ece8d043STakashi Iwai static int via_playback_hp_pcm_close(struct hda_pcm_stream *hinfo,
1133ece8d043STakashi Iwai 				     struct hda_codec *codec,
1134ece8d043STakashi Iwai 				     struct snd_pcm_substream *substream)
1135ece8d043STakashi Iwai {
11363b607e3dSTakashi Iwai 	set_stream_open(codec, STREAM_INDEP_HP, false);
11377eb56e84STakashi Iwai 	return 0;
11387eb56e84STakashi Iwai }
11397eb56e84STakashi Iwai 
11407eb56e84STakashi Iwai static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
11417eb56e84STakashi Iwai 					  struct hda_codec *codec,
11420aa62aefSHarald Welte 					  unsigned int stream_tag,
11430aa62aefSHarald Welte 					  unsigned int format,
11440aa62aefSHarald Welte 					  struct snd_pcm_substream *substream)
11450aa62aefSHarald Welte {
11460aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
11470aa62aefSHarald Welte 
11483b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
11493b607e3dSTakashi Iwai 	setup_playback_multi_pcm(spec);
1150ece8d043STakashi Iwai 	snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
1151ece8d043STakashi Iwai 					 format, substream);
11523b607e3dSTakashi Iwai 	/* remember for dynamic DAC switch with indep-HP */
11533b607e3dSTakashi Iwai 	spec->active_streams |= STREAM_MULTI_OUT;
11543b607e3dSTakashi Iwai 	spec->cur_dac_stream_tag = stream_tag;
11553b607e3dSTakashi Iwai 	spec->cur_dac_format = format;
11563b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
11577eb56e84STakashi Iwai 	vt1708_start_hp_work(spec);
11587eb56e84STakashi Iwai 	return 0;
11590aa62aefSHarald Welte }
11600aa62aefSHarald Welte 
11617eb56e84STakashi Iwai static int via_playback_hp_pcm_prepare(struct hda_pcm_stream *hinfo,
11620aa62aefSHarald Welte 				       struct hda_codec *codec,
11630aa62aefSHarald Welte 				       unsigned int stream_tag,
11640aa62aefSHarald Welte 				       unsigned int format,
11650aa62aefSHarald Welte 				       struct snd_pcm_substream *substream)
11660aa62aefSHarald Welte {
11670aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
11680aa62aefSHarald Welte 
11693b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
11703b607e3dSTakashi Iwai 	if (spec->hp_independent_mode)
1171ece8d043STakashi Iwai 		snd_hda_codec_setup_stream(codec, spec->hp_dac_nid,
1172ece8d043STakashi Iwai 					   stream_tag, 0, format);
11733b607e3dSTakashi Iwai 	spec->active_streams |= STREAM_INDEP_HP;
11743b607e3dSTakashi Iwai 	spec->cur_hp_stream_tag = stream_tag;
11753b607e3dSTakashi Iwai 	spec->cur_hp_format = format;
11763b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
11771f2e99feSLydia Wang 	vt1708_start_hp_work(spec);
11780aa62aefSHarald Welte 	return 0;
11790aa62aefSHarald Welte }
11800aa62aefSHarald Welte 
11810aa62aefSHarald Welte static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
11820aa62aefSHarald Welte 				    struct hda_codec *codec,
11830aa62aefSHarald Welte 				    struct snd_pcm_substream *substream)
11840aa62aefSHarald Welte {
11850aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
11860aa62aefSHarald Welte 
11873b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
1188ece8d043STakashi Iwai 	snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
11893b607e3dSTakashi Iwai 	spec->active_streams &= ~STREAM_MULTI_OUT;
11903b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
11917eb56e84STakashi Iwai 	vt1708_stop_hp_work(spec);
11927eb56e84STakashi Iwai 	return 0;
11930aa62aefSHarald Welte }
11947eb56e84STakashi Iwai 
11957eb56e84STakashi Iwai static int via_playback_hp_pcm_cleanup(struct hda_pcm_stream *hinfo,
11967eb56e84STakashi Iwai 				       struct hda_codec *codec,
11977eb56e84STakashi Iwai 				       struct snd_pcm_substream *substream)
11987eb56e84STakashi Iwai {
11997eb56e84STakashi Iwai 	struct via_spec *spec = codec->spec;
12007eb56e84STakashi Iwai 
12013b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
12023b607e3dSTakashi Iwai 	if (spec->hp_independent_mode)
1203ece8d043STakashi Iwai 		snd_hda_codec_setup_stream(codec, spec->hp_dac_nid, 0, 0, 0);
12043b607e3dSTakashi Iwai 	spec->active_streams &= ~STREAM_INDEP_HP;
12053b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
12061f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
12070aa62aefSHarald Welte 	return 0;
12080aa62aefSHarald Welte }
12090aa62aefSHarald Welte 
1210c577b8a1SJoseph Chan /*
1211c577b8a1SJoseph Chan  * Digital out
1212c577b8a1SJoseph Chan  */
1213c577b8a1SJoseph Chan static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1214c577b8a1SJoseph Chan 				     struct hda_codec *codec,
1215c577b8a1SJoseph Chan 				     struct snd_pcm_substream *substream)
1216c577b8a1SJoseph Chan {
1217c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1218c577b8a1SJoseph Chan 	return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1219c577b8a1SJoseph Chan }
1220c577b8a1SJoseph Chan 
1221c577b8a1SJoseph Chan static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1222c577b8a1SJoseph Chan 				      struct hda_codec *codec,
1223c577b8a1SJoseph Chan 				      struct snd_pcm_substream *substream)
1224c577b8a1SJoseph Chan {
1225c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1226c577b8a1SJoseph Chan 	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1227c577b8a1SJoseph Chan }
1228c577b8a1SJoseph Chan 
12295691ec7fSHarald Welte static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
123098aa34c0SHarald Welte 					struct hda_codec *codec,
123198aa34c0SHarald Welte 					unsigned int stream_tag,
123298aa34c0SHarald Welte 					unsigned int format,
123398aa34c0SHarald Welte 					struct snd_pcm_substream *substream)
123498aa34c0SHarald Welte {
123598aa34c0SHarald Welte 	struct via_spec *spec = codec->spec;
12369da29271STakashi Iwai 	return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
12379da29271STakashi Iwai 					     stream_tag, format, substream);
12389da29271STakashi Iwai }
12395691ec7fSHarald Welte 
12409da29271STakashi Iwai static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
12419da29271STakashi Iwai 					struct hda_codec *codec,
12429da29271STakashi Iwai 					struct snd_pcm_substream *substream)
12439da29271STakashi Iwai {
12449da29271STakashi Iwai 	struct via_spec *spec = codec->spec;
12459da29271STakashi Iwai 	snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
124698aa34c0SHarald Welte 	return 0;
124798aa34c0SHarald Welte }
124898aa34c0SHarald Welte 
1249c577b8a1SJoseph Chan /*
1250c577b8a1SJoseph Chan  * Analog capture
1251c577b8a1SJoseph Chan  */
1252c577b8a1SJoseph Chan static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1253c577b8a1SJoseph Chan 				   struct hda_codec *codec,
1254c577b8a1SJoseph Chan 				   unsigned int stream_tag,
1255c577b8a1SJoseph Chan 				   unsigned int format,
1256c577b8a1SJoseph Chan 				   struct snd_pcm_substream *substream)
1257c577b8a1SJoseph Chan {
1258c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1259c577b8a1SJoseph Chan 
1260c577b8a1SJoseph Chan 	snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1261c577b8a1SJoseph Chan 				   stream_tag, 0, format);
1262c577b8a1SJoseph Chan 	return 0;
1263c577b8a1SJoseph Chan }
1264c577b8a1SJoseph Chan 
1265c577b8a1SJoseph Chan static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1266c577b8a1SJoseph Chan 				   struct hda_codec *codec,
1267c577b8a1SJoseph Chan 				   struct snd_pcm_substream *substream)
1268c577b8a1SJoseph Chan {
1269c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1270888afa15STakashi Iwai 	snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
1271c577b8a1SJoseph Chan 	return 0;
1272c577b8a1SJoseph Chan }
1273c577b8a1SJoseph Chan 
1274a86a88eaSTakashi Iwai /* analog capture with dynamic ADC switching */
1275a86a88eaSTakashi Iwai static int via_dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1276a86a88eaSTakashi Iwai 					   struct hda_codec *codec,
1277a86a88eaSTakashi Iwai 					   unsigned int stream_tag,
1278a86a88eaSTakashi Iwai 					   unsigned int format,
1279a86a88eaSTakashi Iwai 					   struct snd_pcm_substream *substream)
1280a86a88eaSTakashi Iwai {
1281a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1282a86a88eaSTakashi Iwai 	int adc_idx = spec->inputs[spec->cur_mux[0]].adc_idx;
1283a86a88eaSTakashi Iwai 
12843b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
1285a86a88eaSTakashi Iwai 	spec->cur_adc = spec->adc_nids[adc_idx];
1286a86a88eaSTakashi Iwai 	spec->cur_adc_stream_tag = stream_tag;
1287a86a88eaSTakashi Iwai 	spec->cur_adc_format = format;
1288a86a88eaSTakashi Iwai 	snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
12893b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1290a86a88eaSTakashi Iwai 	return 0;
1291a86a88eaSTakashi Iwai }
1292a86a88eaSTakashi Iwai 
1293a86a88eaSTakashi Iwai static int via_dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1294a86a88eaSTakashi Iwai 					   struct hda_codec *codec,
1295a86a88eaSTakashi Iwai 					   struct snd_pcm_substream *substream)
1296a86a88eaSTakashi Iwai {
1297a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1298a86a88eaSTakashi Iwai 
12993b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
1300a86a88eaSTakashi Iwai 	snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1301a86a88eaSTakashi Iwai 	spec->cur_adc = 0;
13023b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1303a86a88eaSTakashi Iwai 	return 0;
1304a86a88eaSTakashi Iwai }
1305a86a88eaSTakashi Iwai 
1306a86a88eaSTakashi Iwai /* re-setup the stream if running; called from input-src put */
1307a86a88eaSTakashi Iwai static bool via_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
1308a86a88eaSTakashi Iwai {
1309a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1310a86a88eaSTakashi Iwai 	int adc_idx = spec->inputs[cur].adc_idx;
1311a86a88eaSTakashi Iwai 	hda_nid_t adc = spec->adc_nids[adc_idx];
13123b607e3dSTakashi Iwai 	bool ret = false;
1313a86a88eaSTakashi Iwai 
13143b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
1315a86a88eaSTakashi Iwai 	if (spec->cur_adc && spec->cur_adc != adc) {
1316a86a88eaSTakashi Iwai 		/* stream is running, let's swap the current ADC */
1317a86a88eaSTakashi Iwai 		__snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1318a86a88eaSTakashi Iwai 		spec->cur_adc = adc;
1319a86a88eaSTakashi Iwai 		snd_hda_codec_setup_stream(codec, adc,
1320a86a88eaSTakashi Iwai 					   spec->cur_adc_stream_tag, 0,
1321a86a88eaSTakashi Iwai 					   spec->cur_adc_format);
13223b607e3dSTakashi Iwai 		ret = true;
1323a86a88eaSTakashi Iwai 	}
13243b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
13253b607e3dSTakashi Iwai 	return ret;
1326a86a88eaSTakashi Iwai }
1327a86a88eaSTakashi Iwai 
13289af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_analog_playback = {
13297eb56e84STakashi Iwai 	.substreams = 1,
1330c577b8a1SJoseph Chan 	.channels_min = 2,
1331c577b8a1SJoseph Chan 	.channels_max = 8,
13329af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1333c577b8a1SJoseph Chan 	.ops = {
1334ece8d043STakashi Iwai 		.open = via_playback_multi_pcm_open,
1335ece8d043STakashi Iwai 		.close = via_playback_multi_pcm_close,
13360aa62aefSHarald Welte 		.prepare = via_playback_multi_pcm_prepare,
13370aa62aefSHarald Welte 		.cleanup = via_playback_multi_pcm_cleanup
1338c577b8a1SJoseph Chan 	},
1339c577b8a1SJoseph Chan };
1340c577b8a1SJoseph Chan 
13417eb56e84STakashi Iwai static const struct hda_pcm_stream via_pcm_hp_playback = {
13427eb56e84STakashi Iwai 	.substreams = 1,
13437eb56e84STakashi Iwai 	.channels_min = 2,
13447eb56e84STakashi Iwai 	.channels_max = 2,
13457eb56e84STakashi Iwai 	/* NID is set in via_build_pcms */
13467eb56e84STakashi Iwai 	.ops = {
13477eb56e84STakashi Iwai 		.open = via_playback_hp_pcm_open,
1348ece8d043STakashi Iwai 		.close = via_playback_hp_pcm_close,
13497eb56e84STakashi Iwai 		.prepare = via_playback_hp_pcm_prepare,
13507eb56e84STakashi Iwai 		.cleanup = via_playback_hp_pcm_cleanup
13517eb56e84STakashi Iwai 	},
13527eb56e84STakashi Iwai };
13537eb56e84STakashi Iwai 
135490dd48a1STakashi Iwai static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
13557eb56e84STakashi Iwai 	.substreams = 1,
1356bc9b5623STakashi Iwai 	.channels_min = 2,
1357bc9b5623STakashi Iwai 	.channels_max = 8,
13589af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1359bc9b5623STakashi Iwai 	/* We got noisy outputs on the right channel on VT1708 when
1360bc9b5623STakashi Iwai 	 * 24bit samples are used.  Until any workaround is found,
1361bc9b5623STakashi Iwai 	 * disable the 24bit format, so far.
1362bc9b5623STakashi Iwai 	 */
1363bc9b5623STakashi Iwai 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
1364bc9b5623STakashi Iwai 	.ops = {
1365ece8d043STakashi Iwai 		.open = via_playback_multi_pcm_open,
1366ece8d043STakashi Iwai 		.close = via_playback_multi_pcm_close,
1367c873cc25SLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
1368c873cc25SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup
1369bc9b5623STakashi Iwai 	},
1370bc9b5623STakashi Iwai };
1371bc9b5623STakashi Iwai 
13729af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_analog_capture = {
13737eb56e84STakashi Iwai 	.substreams = 1, /* will be changed in via_build_pcms() */
1374c577b8a1SJoseph Chan 	.channels_min = 2,
1375c577b8a1SJoseph Chan 	.channels_max = 2,
13769af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1377c577b8a1SJoseph Chan 	.ops = {
1378c577b8a1SJoseph Chan 		.prepare = via_capture_pcm_prepare,
1379c577b8a1SJoseph Chan 		.cleanup = via_capture_pcm_cleanup
1380c577b8a1SJoseph Chan 	},
1381c577b8a1SJoseph Chan };
1382c577b8a1SJoseph Chan 
1383a86a88eaSTakashi Iwai static const struct hda_pcm_stream via_pcm_dyn_adc_analog_capture = {
1384a86a88eaSTakashi Iwai 	.substreams = 1,
1385a86a88eaSTakashi Iwai 	.channels_min = 2,
1386a86a88eaSTakashi Iwai 	.channels_max = 2,
1387a86a88eaSTakashi Iwai 	/* NID is set in via_build_pcms */
1388a86a88eaSTakashi Iwai 	.ops = {
1389a86a88eaSTakashi Iwai 		.prepare = via_dyn_adc_capture_pcm_prepare,
1390a86a88eaSTakashi Iwai 		.cleanup = via_dyn_adc_capture_pcm_cleanup,
1391a86a88eaSTakashi Iwai 	},
1392a86a88eaSTakashi Iwai };
1393a86a88eaSTakashi Iwai 
13949af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_digital_playback = {
1395c577b8a1SJoseph Chan 	.substreams = 1,
1396c577b8a1SJoseph Chan 	.channels_min = 2,
1397c577b8a1SJoseph Chan 	.channels_max = 2,
1398c577b8a1SJoseph Chan 	/* NID is set in via_build_pcms */
1399c577b8a1SJoseph Chan 	.ops = {
1400c577b8a1SJoseph Chan 		.open = via_dig_playback_pcm_open,
14016b97eb45STakashi Iwai 		.close = via_dig_playback_pcm_close,
14029da29271STakashi Iwai 		.prepare = via_dig_playback_pcm_prepare,
14039da29271STakashi Iwai 		.cleanup = via_dig_playback_pcm_cleanup
1404c577b8a1SJoseph Chan 	},
1405c577b8a1SJoseph Chan };
1406c577b8a1SJoseph Chan 
14079af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_digital_capture = {
1408c577b8a1SJoseph Chan 	.substreams = 1,
1409c577b8a1SJoseph Chan 	.channels_min = 2,
1410c577b8a1SJoseph Chan 	.channels_max = 2,
1411c577b8a1SJoseph Chan };
1412c577b8a1SJoseph Chan 
1413370bafbdSTakashi Iwai /*
1414370bafbdSTakashi Iwai  * slave controls for virtual master
1415370bafbdSTakashi Iwai  */
1416370bafbdSTakashi Iwai static const char * const via_slave_vols[] = {
1417370bafbdSTakashi Iwai 	"Front Playback Volume",
1418370bafbdSTakashi Iwai 	"Surround Playback Volume",
1419370bafbdSTakashi Iwai 	"Center Playback Volume",
1420370bafbdSTakashi Iwai 	"LFE Playback Volume",
1421370bafbdSTakashi Iwai 	"Side Playback Volume",
1422370bafbdSTakashi Iwai 	"Headphone Playback Volume",
1423370bafbdSTakashi Iwai 	"Speaker Playback Volume",
1424370bafbdSTakashi Iwai 	NULL,
1425370bafbdSTakashi Iwai };
1426370bafbdSTakashi Iwai 
1427370bafbdSTakashi Iwai static const char * const via_slave_sws[] = {
1428370bafbdSTakashi Iwai 	"Front Playback Switch",
1429370bafbdSTakashi Iwai 	"Surround Playback Switch",
1430370bafbdSTakashi Iwai 	"Center Playback Switch",
1431370bafbdSTakashi Iwai 	"LFE Playback Switch",
1432370bafbdSTakashi Iwai 	"Side Playback Switch",
1433370bafbdSTakashi Iwai 	"Headphone Playback Switch",
1434370bafbdSTakashi Iwai 	"Speaker Playback Switch",
1435370bafbdSTakashi Iwai 	NULL,
1436370bafbdSTakashi Iwai };
1437370bafbdSTakashi Iwai 
1438c577b8a1SJoseph Chan static int via_build_controls(struct hda_codec *codec)
1439c577b8a1SJoseph Chan {
1440c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
14415b0cb1d8SJaroslav Kysela 	struct snd_kcontrol *kctl;
14425b0cb1d8SJaroslav Kysela 	int err, i;
1443c577b8a1SJoseph Chan 
144424088a58STakashi Iwai 	if (spec->set_widgets_power_state)
144524088a58STakashi Iwai 		if (!via_clone_control(spec, &via_pin_power_ctl_enum))
144624088a58STakashi Iwai 			return -ENOMEM;
144724088a58STakashi Iwai 
1448c577b8a1SJoseph Chan 	for (i = 0; i < spec->num_mixers; i++) {
1449c577b8a1SJoseph Chan 		err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1450c577b8a1SJoseph Chan 		if (err < 0)
1451c577b8a1SJoseph Chan 			return err;
1452c577b8a1SJoseph Chan 	}
1453c577b8a1SJoseph Chan 
1454c577b8a1SJoseph Chan 	if (spec->multiout.dig_out_nid) {
1455c577b8a1SJoseph Chan 		err = snd_hda_create_spdif_out_ctls(codec,
145674b654c9SStephen Warren 						    spec->multiout.dig_out_nid,
1457c577b8a1SJoseph Chan 						    spec->multiout.dig_out_nid);
1458c577b8a1SJoseph Chan 		if (err < 0)
1459c577b8a1SJoseph Chan 			return err;
14609a08160bSTakashi Iwai 		err = snd_hda_create_spdif_share_sw(codec,
14619a08160bSTakashi Iwai 						    &spec->multiout);
14629a08160bSTakashi Iwai 		if (err < 0)
14639a08160bSTakashi Iwai 			return err;
14649a08160bSTakashi Iwai 		spec->multiout.share_spdif = 1;
1465c577b8a1SJoseph Chan 	}
1466c577b8a1SJoseph Chan 	if (spec->dig_in_nid) {
1467c577b8a1SJoseph Chan 		err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1468c577b8a1SJoseph Chan 		if (err < 0)
1469c577b8a1SJoseph Chan 			return err;
1470c577b8a1SJoseph Chan 	}
147117314379SLydia Wang 
1472370bafbdSTakashi Iwai 	/* if we have no master control, let's create it */
1473370bafbdSTakashi Iwai 	if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1474370bafbdSTakashi Iwai 		unsigned int vmaster_tlv[4];
1475370bafbdSTakashi Iwai 		snd_hda_set_vmaster_tlv(codec, spec->multiout.dac_nids[0],
1476370bafbdSTakashi Iwai 					HDA_OUTPUT, vmaster_tlv);
1477370bafbdSTakashi Iwai 		err = snd_hda_add_vmaster(codec, "Master Playback Volume",
1478370bafbdSTakashi Iwai 					  vmaster_tlv, via_slave_vols);
1479370bafbdSTakashi Iwai 		if (err < 0)
1480370bafbdSTakashi Iwai 			return err;
1481370bafbdSTakashi Iwai 	}
1482370bafbdSTakashi Iwai 	if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1483370bafbdSTakashi Iwai 		err = snd_hda_add_vmaster(codec, "Master Playback Switch",
1484370bafbdSTakashi Iwai 					  NULL, via_slave_sws);
1485370bafbdSTakashi Iwai 		if (err < 0)
1486370bafbdSTakashi Iwai 			return err;
1487370bafbdSTakashi Iwai 	}
1488370bafbdSTakashi Iwai 
14895b0cb1d8SJaroslav Kysela 	/* assign Capture Source enums to NID */
14905b0cb1d8SJaroslav Kysela 	kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
14915b0cb1d8SJaroslav Kysela 	for (i = 0; kctl && i < kctl->count; i++) {
149221949f00STakashi Iwai 		err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
14935b0cb1d8SJaroslav Kysela 		if (err < 0)
14945b0cb1d8SJaroslav Kysela 			return err;
14955b0cb1d8SJaroslav Kysela 	}
14965b0cb1d8SJaroslav Kysela 
149717314379SLydia Wang 	/* init power states */
14983e95b9abSLydia Wang 	set_widgets_power_state(codec);
1499ada509ecSTakashi Iwai 	analog_low_current_mode(codec);
150017314379SLydia Wang 
1501603c4019STakashi Iwai 	via_free_kctls(codec); /* no longer needed */
1502c577b8a1SJoseph Chan 	return 0;
1503c577b8a1SJoseph Chan }
1504c577b8a1SJoseph Chan 
1505c577b8a1SJoseph Chan static int via_build_pcms(struct hda_codec *codec)
1506c577b8a1SJoseph Chan {
1507c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1508c577b8a1SJoseph Chan 	struct hda_pcm *info = spec->pcm_rec;
1509c577b8a1SJoseph Chan 
1510a5973103STakashi Iwai 	codec->num_pcms = 0;
1511c577b8a1SJoseph Chan 	codec->pcm_info = info;
1512c577b8a1SJoseph Chan 
1513a5973103STakashi Iwai 	if (spec->multiout.num_dacs || spec->num_adc_nids) {
1514a5973103STakashi Iwai 		snprintf(spec->stream_name_analog,
1515a5973103STakashi Iwai 			 sizeof(spec->stream_name_analog),
151682673bc8STakashi Iwai 			 "%s Analog", codec->chip_name);
1517c577b8a1SJoseph Chan 		info->name = spec->stream_name_analog;
15189af74210STakashi Iwai 
1519a5973103STakashi Iwai 		if (spec->multiout.num_dacs) {
15209af74210STakashi Iwai 			if (!spec->stream_analog_playback)
1521a5973103STakashi Iwai 				spec->stream_analog_playback =
1522a5973103STakashi Iwai 					&via_pcm_analog_playback;
1523377ff31aSLydia Wang 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
15249af74210STakashi Iwai 				*spec->stream_analog_playback;
1525377ff31aSLydia Wang 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1526377ff31aSLydia Wang 				spec->multiout.dac_nids[0];
1527c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1528c577b8a1SJoseph Chan 				spec->multiout.max_channels;
1529a5973103STakashi Iwai 		}
15309af74210STakashi Iwai 
1531a86a88eaSTakashi Iwai 		if (!spec->stream_analog_capture) {
1532a86a88eaSTakashi Iwai 			if (spec->dyn_adc_switch)
1533a86a88eaSTakashi Iwai 				spec->stream_analog_capture =
1534a86a88eaSTakashi Iwai 					&via_pcm_dyn_adc_analog_capture;
1535a86a88eaSTakashi Iwai 			else
1536a5973103STakashi Iwai 				spec->stream_analog_capture =
1537a5973103STakashi Iwai 					&via_pcm_analog_capture;
1538a86a88eaSTakashi Iwai 		}
1539a5973103STakashi Iwai 		if (spec->num_adc_nids) {
15409af74210STakashi Iwai 			info->stream[SNDRV_PCM_STREAM_CAPTURE] =
15419af74210STakashi Iwai 				*spec->stream_analog_capture;
1542a5973103STakashi Iwai 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1543a5973103STakashi Iwai 				spec->adc_nids[0];
1544a86a88eaSTakashi Iwai 			if (!spec->dyn_adc_switch)
15459af74210STakashi Iwai 				info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
15469af74210STakashi Iwai 					spec->num_adc_nids;
1547a5973103STakashi Iwai 		}
1548c577b8a1SJoseph Chan 		codec->num_pcms++;
1549c577b8a1SJoseph Chan 		info++;
1550a5973103STakashi Iwai 	}
1551a5973103STakashi Iwai 
1552a5973103STakashi Iwai 	if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
155382673bc8STakashi Iwai 		snprintf(spec->stream_name_digital,
155482673bc8STakashi Iwai 			 sizeof(spec->stream_name_digital),
155582673bc8STakashi Iwai 			 "%s Digital", codec->chip_name);
1556c577b8a1SJoseph Chan 		info->name = spec->stream_name_digital;
15577ba72ba1STakashi Iwai 		info->pcm_type = HDA_PCM_TYPE_SPDIF;
1558c577b8a1SJoseph Chan 		if (spec->multiout.dig_out_nid) {
15599af74210STakashi Iwai 			if (!spec->stream_digital_playback)
15609af74210STakashi Iwai 				spec->stream_digital_playback =
15619af74210STakashi Iwai 					&via_pcm_digital_playback;
1562c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
15639af74210STakashi Iwai 				*spec->stream_digital_playback;
1564c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1565c577b8a1SJoseph Chan 				spec->multiout.dig_out_nid;
1566c577b8a1SJoseph Chan 		}
1567c577b8a1SJoseph Chan 		if (spec->dig_in_nid) {
15689af74210STakashi Iwai 			if (!spec->stream_digital_capture)
15699af74210STakashi Iwai 				spec->stream_digital_capture =
15709af74210STakashi Iwai 					&via_pcm_digital_capture;
1571c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_CAPTURE] =
15729af74210STakashi Iwai 				*spec->stream_digital_capture;
1573c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1574c577b8a1SJoseph Chan 				spec->dig_in_nid;
1575c577b8a1SJoseph Chan 		}
1576a5973103STakashi Iwai 		codec->num_pcms++;
1577a5973103STakashi Iwai 		info++;
1578c577b8a1SJoseph Chan 	}
1579c577b8a1SJoseph Chan 
1580ece8d043STakashi Iwai 	if (spec->hp_dac_nid) {
15817eb56e84STakashi Iwai 		snprintf(spec->stream_name_hp, sizeof(spec->stream_name_hp),
15827eb56e84STakashi Iwai 			 "%s HP", codec->chip_name);
15837eb56e84STakashi Iwai 		info->name = spec->stream_name_hp;
15847eb56e84STakashi Iwai 		info->stream[SNDRV_PCM_STREAM_PLAYBACK] = via_pcm_hp_playback;
15857eb56e84STakashi Iwai 		info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1586ece8d043STakashi Iwai 			spec->hp_dac_nid;
1587a5973103STakashi Iwai 		codec->num_pcms++;
1588a5973103STakashi Iwai 		info++;
15897eb56e84STakashi Iwai 	}
1590c577b8a1SJoseph Chan 	return 0;
1591c577b8a1SJoseph Chan }
1592c577b8a1SJoseph Chan 
1593c577b8a1SJoseph Chan static void via_free(struct hda_codec *codec)
1594c577b8a1SJoseph Chan {
1595c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1596c577b8a1SJoseph Chan 
1597c577b8a1SJoseph Chan 	if (!spec)
1598c577b8a1SJoseph Chan 		return;
1599c577b8a1SJoseph Chan 
1600603c4019STakashi Iwai 	via_free_kctls(codec);
16011f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
1602a86a88eaSTakashi Iwai 	kfree(spec->bind_cap_vol);
1603a86a88eaSTakashi Iwai 	kfree(spec->bind_cap_sw);
1604a86a88eaSTakashi Iwai 	kfree(spec);
1605c577b8a1SJoseph Chan }
1606c577b8a1SJoseph Chan 
160764be285bSTakashi Iwai /* mute/unmute outputs */
160864be285bSTakashi Iwai static void toggle_output_mutes(struct hda_codec *codec, int num_pins,
160964be285bSTakashi Iwai 				hda_nid_t *pins, bool mute)
161064be285bSTakashi Iwai {
161164be285bSTakashi Iwai 	int i;
161294994734STakashi Iwai 	for (i = 0; i < num_pins; i++) {
161394994734STakashi Iwai 		unsigned int parm = snd_hda_codec_read(codec, pins[i], 0,
161494994734STakashi Iwai 					  AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
161594994734STakashi Iwai 		if (parm & AC_PINCTL_IN_EN)
161694994734STakashi Iwai 			continue;
161794994734STakashi Iwai 		if (mute)
161894994734STakashi Iwai 			parm &= ~AC_PINCTL_OUT_EN;
161994994734STakashi Iwai 		else
162094994734STakashi Iwai 			parm |= AC_PINCTL_OUT_EN;
162164be285bSTakashi Iwai 		snd_hda_codec_write(codec, pins[i], 0,
162294994734STakashi Iwai 				    AC_VERB_SET_PIN_WIDGET_CONTROL, parm);
162394994734STakashi Iwai 	}
162464be285bSTakashi Iwai }
162564be285bSTakashi Iwai 
16264a918ffeSTakashi Iwai /* mute internal speaker if line-out is plugged */
16274a918ffeSTakashi Iwai static void via_line_automute(struct hda_codec *codec, int present)
16284a918ffeSTakashi Iwai {
16294a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
16304a918ffeSTakashi Iwai 
16314a918ffeSTakashi Iwai 	if (!spec->autocfg.speaker_outs)
16324a918ffeSTakashi Iwai 		return;
16334a918ffeSTakashi Iwai 	if (!present)
16344a918ffeSTakashi Iwai 		present = snd_hda_jack_detect(codec,
16354a918ffeSTakashi Iwai 					      spec->autocfg.line_out_pins[0]);
16364a918ffeSTakashi Iwai 	toggle_output_mutes(codec, spec->autocfg.speaker_outs,
16374a918ffeSTakashi Iwai 			    spec->autocfg.speaker_pins,
16384a918ffeSTakashi Iwai 			    present);
16394a918ffeSTakashi Iwai }
16404a918ffeSTakashi Iwai 
164169e52a80SHarald Welte /* mute internal speaker if HP is plugged */
164269e52a80SHarald Welte static void via_hp_automute(struct hda_codec *codec)
164369e52a80SHarald Welte {
16444a918ffeSTakashi Iwai 	int present = 0;
16456e969d91STakashi Iwai 	int nums;
164669e52a80SHarald Welte 	struct via_spec *spec = codec->spec;
164769e52a80SHarald Welte 
16486e969d91STakashi Iwai 	if (!spec->hp_independent_mode && spec->autocfg.hp_pins[0])
1649d56757abSTakashi Iwai 		present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
16506e969d91STakashi Iwai 
1651f2b1c9f0STakashi Iwai 	if (spec->smart51_enabled)
1652f2b1c9f0STakashi Iwai 		nums = spec->autocfg.line_outs + spec->smart51_nums;
1653f2b1c9f0STakashi Iwai 	else
1654f2b1c9f0STakashi Iwai 		nums = spec->autocfg.line_outs;
16556e969d91STakashi Iwai 	toggle_output_mutes(codec, nums, spec->autocfg.line_out_pins, present);
16566e969d91STakashi Iwai 
16574a918ffeSTakashi Iwai 	via_line_automute(codec, present);
1658f3db423dSLydia Wang }
1659f3db423dSLydia Wang 
166069e52a80SHarald Welte static void via_gpio_control(struct hda_codec *codec)
166169e52a80SHarald Welte {
166269e52a80SHarald Welte 	unsigned int gpio_data;
166369e52a80SHarald Welte 	unsigned int vol_counter;
166469e52a80SHarald Welte 	unsigned int vol;
166569e52a80SHarald Welte 	unsigned int master_vol;
166669e52a80SHarald Welte 
166769e52a80SHarald Welte 	struct via_spec *spec = codec->spec;
166869e52a80SHarald Welte 
166969e52a80SHarald Welte 	gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
167069e52a80SHarald Welte 				       AC_VERB_GET_GPIO_DATA, 0) & 0x03;
167169e52a80SHarald Welte 
167269e52a80SHarald Welte 	vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
167369e52a80SHarald Welte 					  0xF84, 0) & 0x3F0000) >> 16;
167469e52a80SHarald Welte 
167569e52a80SHarald Welte 	vol = vol_counter & 0x1F;
167669e52a80SHarald Welte 	master_vol = snd_hda_codec_read(codec, 0x1A, 0,
167769e52a80SHarald Welte 					AC_VERB_GET_AMP_GAIN_MUTE,
167869e52a80SHarald Welte 					AC_AMP_GET_INPUT);
167969e52a80SHarald Welte 
168069e52a80SHarald Welte 	if (gpio_data == 0x02) {
168169e52a80SHarald Welte 		/* unmute line out */
16823e0693e2STakashi Iwai 		snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
16833e0693e2STakashi Iwai 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
16843e0693e2STakashi Iwai 				    PIN_OUT);
168569e52a80SHarald Welte 		if (vol_counter & 0x20) {
168669e52a80SHarald Welte 			/* decrease volume */
168769e52a80SHarald Welte 			if (vol > master_vol)
168869e52a80SHarald Welte 				vol = master_vol;
168969e52a80SHarald Welte 			snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
169069e52a80SHarald Welte 						 0, HDA_AMP_VOLMASK,
169169e52a80SHarald Welte 						 master_vol-vol);
169269e52a80SHarald Welte 		} else {
169369e52a80SHarald Welte 			/* increase volume */
169469e52a80SHarald Welte 			snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
169569e52a80SHarald Welte 					 HDA_AMP_VOLMASK,
169669e52a80SHarald Welte 					 ((master_vol+vol) > 0x2A) ? 0x2A :
169769e52a80SHarald Welte 					  (master_vol+vol));
169869e52a80SHarald Welte 		}
169969e52a80SHarald Welte 	} else if (!(gpio_data & 0x02)) {
170069e52a80SHarald Welte 		/* mute line out */
17013e0693e2STakashi Iwai 		snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
17023e0693e2STakashi Iwai 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
17033e0693e2STakashi Iwai 				    0);
170469e52a80SHarald Welte 	}
170569e52a80SHarald Welte }
170669e52a80SHarald Welte 
170769e52a80SHarald Welte /* unsolicited event for jack sensing */
170869e52a80SHarald Welte static void via_unsol_event(struct hda_codec *codec,
170969e52a80SHarald Welte 				  unsigned int res)
171069e52a80SHarald Welte {
171169e52a80SHarald Welte 	res >>= 26;
1712ec7e7e42SLydia Wang 
1713a34df19aSLydia Wang 	if (res & VIA_JACK_EVENT)
17143e95b9abSLydia Wang 		set_widgets_power_state(codec);
1715ec7e7e42SLydia Wang 
1716ec7e7e42SLydia Wang 	res &= ~VIA_JACK_EVENT;
1717ec7e7e42SLydia Wang 
171821ce0b65STakashi Iwai 	if (res == VIA_HP_EVENT || res == VIA_LINE_EVENT)
1719ec7e7e42SLydia Wang 		via_hp_automute(codec);
1720ec7e7e42SLydia Wang 	else if (res == VIA_GPIO_EVENT)
1721ec7e7e42SLydia Wang 		via_gpio_control(codec);
172269e52a80SHarald Welte }
172369e52a80SHarald Welte 
17242a43952aSTakashi Iwai #ifdef CONFIG_PM
17251f2e99feSLydia Wang static int via_suspend(struct hda_codec *codec, pm_message_t state)
17261f2e99feSLydia Wang {
17271f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
17281f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
17291f2e99feSLydia Wang 	return 0;
17301f2e99feSLydia Wang }
17311f2e99feSLydia Wang #endif
17321f2e99feSLydia Wang 
1733cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
1734cb53c626STakashi Iwai static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1735cb53c626STakashi Iwai {
1736cb53c626STakashi Iwai 	struct via_spec *spec = codec->spec;
1737cb53c626STakashi Iwai 	return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1738cb53c626STakashi Iwai }
1739cb53c626STakashi Iwai #endif
1740cb53c626STakashi Iwai 
1741c577b8a1SJoseph Chan /*
1742c577b8a1SJoseph Chan  */
17435d41762aSTakashi Iwai 
17445d41762aSTakashi Iwai static int via_init(struct hda_codec *codec);
17455d41762aSTakashi Iwai 
174690dd48a1STakashi Iwai static const struct hda_codec_ops via_patch_ops = {
1747c577b8a1SJoseph Chan 	.build_controls = via_build_controls,
1748c577b8a1SJoseph Chan 	.build_pcms = via_build_pcms,
1749c577b8a1SJoseph Chan 	.init = via_init,
1750c577b8a1SJoseph Chan 	.free = via_free,
17514a918ffeSTakashi Iwai 	.unsol_event = via_unsol_event,
17522a43952aSTakashi Iwai #ifdef CONFIG_PM
17531f2e99feSLydia Wang 	.suspend = via_suspend,
17541f2e99feSLydia Wang #endif
1755cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
1756cb53c626STakashi Iwai 	.check_power_status = via_check_power_status,
1757cb53c626STakashi Iwai #endif
1758c577b8a1SJoseph Chan };
1759c577b8a1SJoseph Chan 
17604a79616dSTakashi Iwai static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
1761c577b8a1SJoseph Chan {
17624a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
17634a79616dSTakashi Iwai 	int i;
17644a79616dSTakashi Iwai 
17654a79616dSTakashi Iwai 	for (i = 0; i < spec->multiout.num_dacs; i++) {
17664a79616dSTakashi Iwai 		if (spec->multiout.dac_nids[i] == dac)
17674a79616dSTakashi Iwai 			return false;
17684a79616dSTakashi Iwai 	}
1769ece8d043STakashi Iwai 	if (spec->hp_dac_nid == dac)
17704a79616dSTakashi Iwai 		return false;
17714a79616dSTakashi Iwai 	return true;
17724a79616dSTakashi Iwai }
17734a79616dSTakashi Iwai 
17748e3679dcSTakashi Iwai static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
17753214b966STakashi Iwai 				hda_nid_t target_dac, int with_aa_mix,
17763214b966STakashi Iwai 				struct nid_path *path, int depth)
17774a79616dSTakashi Iwai {
17783214b966STakashi Iwai 	struct via_spec *spec = codec->spec;
17794a79616dSTakashi Iwai 	hda_nid_t conn[8];
17804a79616dSTakashi Iwai 	int i, nums;
17814a79616dSTakashi Iwai 
17823214b966STakashi Iwai 	if (nid == spec->aa_mix_nid) {
17833214b966STakashi Iwai 		if (!with_aa_mix)
17843214b966STakashi Iwai 			return false;
17853214b966STakashi Iwai 		with_aa_mix = 2; /* mark aa-mix is included */
17863214b966STakashi Iwai 	}
17873214b966STakashi Iwai 
17884a79616dSTakashi Iwai 	nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
17894a79616dSTakashi Iwai 	for (i = 0; i < nums; i++) {
17904a79616dSTakashi Iwai 		if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
17914a79616dSTakashi Iwai 			continue;
17923214b966STakashi Iwai 		if (conn[i] == target_dac || is_empty_dac(codec, conn[i])) {
17933214b966STakashi Iwai 			/* aa-mix is requested but not included? */
17943214b966STakashi Iwai 			if (!(spec->aa_mix_nid && with_aa_mix == 1))
179509a9ad69STakashi Iwai 				goto found;
17964a79616dSTakashi Iwai 		}
17973214b966STakashi Iwai 	}
17988e3679dcSTakashi Iwai 	if (depth >= MAX_NID_PATH_DEPTH)
17994a79616dSTakashi Iwai 		return false;
18004a79616dSTakashi Iwai 	for (i = 0; i < nums; i++) {
18014a79616dSTakashi Iwai 		unsigned int type;
18024a79616dSTakashi Iwai 		type = get_wcaps_type(get_wcaps(codec, conn[i]));
18033214b966STakashi Iwai 		if (type == AC_WID_AUD_OUT)
18044a79616dSTakashi Iwai 			continue;
18058e3679dcSTakashi Iwai 		if (__parse_output_path(codec, conn[i], target_dac,
18063214b966STakashi Iwai 					with_aa_mix, path, depth + 1))
180709a9ad69STakashi Iwai 			goto found;
18084a79616dSTakashi Iwai 	}
18094a79616dSTakashi Iwai 	return false;
181009a9ad69STakashi Iwai 
181109a9ad69STakashi Iwai  found:
181209a9ad69STakashi Iwai 	path->path[path->depth] = conn[i];
181309a9ad69STakashi Iwai 	path->idx[path->depth] = i;
181409a9ad69STakashi Iwai 	if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
181509a9ad69STakashi Iwai 		path->multi[path->depth] = 1;
181609a9ad69STakashi Iwai 	path->depth++;
181709a9ad69STakashi Iwai 	return true;
18184a79616dSTakashi Iwai }
18194a79616dSTakashi Iwai 
18208e3679dcSTakashi Iwai static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
18213214b966STakashi Iwai 			      hda_nid_t target_dac, int with_aa_mix,
18223214b966STakashi Iwai 			      struct nid_path *path)
18238e3679dcSTakashi Iwai {
18243214b966STakashi Iwai 	if (__parse_output_path(codec, nid, target_dac, with_aa_mix, path, 1)) {
18258e3679dcSTakashi Iwai 		path->path[path->depth] = nid;
18268e3679dcSTakashi Iwai 		path->depth++;
18273214b966STakashi Iwai 		snd_printdd("output-path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
18283214b966STakashi Iwai 			    path->depth, path->path[0], path->path[1],
18293214b966STakashi Iwai 			    path->path[2], path->path[3], path->path[4]);
18308e3679dcSTakashi Iwai 		return true;
18318e3679dcSTakashi Iwai 	}
18328e3679dcSTakashi Iwai 	return false;
18338e3679dcSTakashi Iwai }
18348e3679dcSTakashi Iwai 
18354a79616dSTakashi Iwai static int via_auto_fill_dac_nids(struct hda_codec *codec)
18364a79616dSTakashi Iwai {
18374a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
18384a79616dSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
18395c9a5615SLydia Wang 	int i, dac_num;
1840c577b8a1SJoseph Chan 	hda_nid_t nid;
1841c577b8a1SJoseph Chan 
1842c577b8a1SJoseph Chan 	spec->multiout.dac_nids = spec->private_dac_nids;
18435c9a5615SLydia Wang 	dac_num = 0;
18444a79616dSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
18453214b966STakashi Iwai 		hda_nid_t dac = 0;
1846c577b8a1SJoseph Chan 		nid = cfg->line_out_pins[i];
18474a79616dSTakashi Iwai 		if (!nid)
18484a79616dSTakashi Iwai 			continue;
18493214b966STakashi Iwai 		if (parse_output_path(codec, nid, 0, 0, &spec->out_path[i]))
18503214b966STakashi Iwai 			dac = spec->out_path[i].path[0];
18513214b966STakashi Iwai 		if (!i && parse_output_path(codec, nid, dac, 1,
18523214b966STakashi Iwai 					    &spec->out_mix_path))
18533214b966STakashi Iwai 			dac = spec->out_mix_path.path[0];
18543214b966STakashi Iwai 		if (dac) {
18553214b966STakashi Iwai 			spec->private_dac_nids[i] = dac;
18565c9a5615SLydia Wang 			dac_num++;
1857c577b8a1SJoseph Chan 		}
18585c9a5615SLydia Wang 	}
18593214b966STakashi Iwai 	if (!spec->out_path[0].depth && spec->out_mix_path.depth) {
18603214b966STakashi Iwai 		spec->out_path[0] = spec->out_mix_path;
18613214b966STakashi Iwai 		spec->out_mix_path.depth = 0;
18623214b966STakashi Iwai 	}
18635c9a5615SLydia Wang 	spec->multiout.num_dacs = dac_num;
1864c577b8a1SJoseph Chan 	return 0;
1865c577b8a1SJoseph Chan }
1866c577b8a1SJoseph Chan 
18674a79616dSTakashi Iwai static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
186809a9ad69STakashi Iwai 			  int chs, bool check_dac, struct nid_path *path)
1869c577b8a1SJoseph Chan {
18704a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
1871c577b8a1SJoseph Chan 	char name[32];
187209a9ad69STakashi Iwai 	hda_nid_t dac, pin, sel, nid;
187309a9ad69STakashi Iwai 	int err;
1874a934d5a9STakashi Iwai 
187509a9ad69STakashi Iwai 	dac = check_dac ? path->path[0] : 0;
187609a9ad69STakashi Iwai 	pin = path->path[path->depth - 1];
187709a9ad69STakashi Iwai 	sel = path->depth > 1 ? path->path[1] : 0;
1878c577b8a1SJoseph Chan 
18798df2a312STakashi Iwai 	if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
18804a79616dSTakashi Iwai 		nid = dac;
18818df2a312STakashi Iwai 	else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
18824a79616dSTakashi Iwai 		nid = pin;
1883a934d5a9STakashi Iwai 	else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1884a934d5a9STakashi Iwai 		nid = sel;
18854a79616dSTakashi Iwai 	else
18864a79616dSTakashi Iwai 		nid = 0;
18874a79616dSTakashi Iwai 	if (nid) {
18884a79616dSTakashi Iwai 		sprintf(name, "%s Playback Volume", pfx);
1889c577b8a1SJoseph Chan 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1890a00a5fadSLydia Wang 			      HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1891c577b8a1SJoseph Chan 		if (err < 0)
1892c577b8a1SJoseph Chan 			return err;
189309a9ad69STakashi Iwai 		path->vol_ctl = nid;
1894c577b8a1SJoseph Chan 	}
18954a79616dSTakashi Iwai 
18968df2a312STakashi Iwai 	if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
18974a79616dSTakashi Iwai 		nid = dac;
18988df2a312STakashi Iwai 	else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
18994a79616dSTakashi Iwai 		nid = pin;
1900a934d5a9STakashi Iwai 	else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
1901a934d5a9STakashi Iwai 		nid = sel;
19024a79616dSTakashi Iwai 	else
19034a79616dSTakashi Iwai 		nid = 0;
19044a79616dSTakashi Iwai 	if (nid) {
19054a79616dSTakashi Iwai 		sprintf(name, "%s Playback Switch", pfx);
19064a79616dSTakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
19074a79616dSTakashi Iwai 			      HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
19084a79616dSTakashi Iwai 		if (err < 0)
19094a79616dSTakashi Iwai 			return err;
191009a9ad69STakashi Iwai 		path->mute_ctl = nid;
19114a79616dSTakashi Iwai 	}
19124a79616dSTakashi Iwai 	return 0;
19134a79616dSTakashi Iwai }
19144a79616dSTakashi Iwai 
1915f4a7828bSTakashi Iwai static void mangle_smart51(struct hda_codec *codec)
1916f4a7828bSTakashi Iwai {
1917f4a7828bSTakashi Iwai 	struct via_spec *spec = codec->spec;
1918f4a7828bSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
19190f98c24bSTakashi Iwai 	struct auto_pin_cfg_item *ins = cfg->inputs;
19200f98c24bSTakashi Iwai 	int i, j, nums, attr;
19210f98c24bSTakashi Iwai 	int pins[AUTO_CFG_MAX_INS];
1922f4a7828bSTakashi Iwai 
19230f98c24bSTakashi Iwai 	for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
19240f98c24bSTakashi Iwai 		nums = 0;
1925f4a7828bSTakashi Iwai 		for (i = 0; i < cfg->num_inputs; i++) {
19260f98c24bSTakashi Iwai 			unsigned int def;
19270f98c24bSTakashi Iwai 			if (ins[i].type > AUTO_PIN_LINE_IN)
19280f98c24bSTakashi Iwai 				continue;
19290f98c24bSTakashi Iwai 			def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
19300f98c24bSTakashi Iwai 			if (snd_hda_get_input_pin_attr(def) != attr)
19310f98c24bSTakashi Iwai 				continue;
19320f98c24bSTakashi Iwai 			for (j = 0; j < nums; j++)
19330f98c24bSTakashi Iwai 				if (ins[pins[j]].type < ins[i].type) {
19340f98c24bSTakashi Iwai 					memmove(pins + j + 1, pins + j,
193521d45d2bSTakashi Iwai 						(nums - j) * sizeof(int));
19360f98c24bSTakashi Iwai 					break;
19370f98c24bSTakashi Iwai 				}
19380f98c24bSTakashi Iwai 			pins[j] = i;
1939e3d7a143STakashi Iwai 			nums++;
1940e3d7a143STakashi Iwai 		}
1941e3d7a143STakashi Iwai 		if (cfg->line_outs + nums < 3)
1942f4a7828bSTakashi Iwai 			continue;
19430f98c24bSTakashi Iwai 		for (i = 0; i < nums; i++) {
19440f98c24bSTakashi Iwai 			hda_nid_t pin = ins[pins[i]].pin;
19450f98c24bSTakashi Iwai 			spec->smart51_pins[spec->smart51_nums++] = pin;
19460f98c24bSTakashi Iwai 			cfg->line_out_pins[cfg->line_outs++] = pin;
1947f4a7828bSTakashi Iwai 			if (cfg->line_outs == 3)
1948f4a7828bSTakashi Iwai 				break;
1949f4a7828bSTakashi Iwai 		}
19500f98c24bSTakashi Iwai 		return;
19510f98c24bSTakashi Iwai 	}
1952f4a7828bSTakashi Iwai }
1953f4a7828bSTakashi Iwai 
1954020066d1STakashi Iwai static void copy_path_mixer_ctls(struct nid_path *dst, struct nid_path *src)
1955020066d1STakashi Iwai {
1956020066d1STakashi Iwai 	dst->vol_ctl = src->vol_ctl;
1957020066d1STakashi Iwai 	dst->mute_ctl = src->mute_ctl;
1958020066d1STakashi Iwai }
1959020066d1STakashi Iwai 
19604a79616dSTakashi Iwai /* add playback controls from the parsed DAC table */
19614a79616dSTakashi Iwai static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
19624a79616dSTakashi Iwai {
19634a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
1964f4a7828bSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
19653214b966STakashi Iwai 	struct nid_path *path;
19664a79616dSTakashi Iwai 	static const char * const chname[4] = {
19674a79616dSTakashi Iwai 		"Front", "Surround", "C/LFE", "Side"
19684a79616dSTakashi Iwai 	};
19694a79616dSTakashi Iwai 	int i, idx, err;
1970f4a7828bSTakashi Iwai 	int old_line_outs;
1971f4a7828bSTakashi Iwai 
1972f4a7828bSTakashi Iwai 	/* check smart51 */
1973f4a7828bSTakashi Iwai 	old_line_outs = cfg->line_outs;
1974f4a7828bSTakashi Iwai 	if (cfg->line_outs == 1)
1975f4a7828bSTakashi Iwai 		mangle_smart51(codec);
19764a79616dSTakashi Iwai 
1977e3d7a143STakashi Iwai 	err = via_auto_fill_dac_nids(codec);
1978e3d7a143STakashi Iwai 	if (err < 0)
1979e3d7a143STakashi Iwai 		return err;
1980e3d7a143STakashi Iwai 
19815c9a5615SLydia Wang 	if (spec->multiout.num_dacs < 3) {
19825c9a5615SLydia Wang 		spec->smart51_nums = 0;
19835c9a5615SLydia Wang 		cfg->line_outs = old_line_outs;
19845c9a5615SLydia Wang 	}
19854a79616dSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
19864a79616dSTakashi Iwai 		hda_nid_t pin, dac;
19874a79616dSTakashi Iwai 		pin = cfg->line_out_pins[i];
19884a79616dSTakashi Iwai 		dac = spec->multiout.dac_nids[i];
19894a79616dSTakashi Iwai 		if (!pin || !dac)
19904a79616dSTakashi Iwai 			continue;
19913214b966STakashi Iwai 		path = spec->out_path + i;
19920fe0adf8STakashi Iwai 		if (i == HDA_CLFE) {
19933214b966STakashi Iwai 			err = create_ch_ctls(codec, "Center", 1, true, path);
19944a79616dSTakashi Iwai 			if (err < 0)
19954a79616dSTakashi Iwai 				return err;
19963214b966STakashi Iwai 			err = create_ch_ctls(codec, "LFE", 2, true, path);
19974a79616dSTakashi Iwai 			if (err < 0)
19984a79616dSTakashi Iwai 				return err;
19994a79616dSTakashi Iwai 		} else {
20006aadf41dSTakashi Iwai 			const char *pfx = chname[i];
20016aadf41dSTakashi Iwai 			if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
20026aadf41dSTakashi Iwai 			    cfg->line_outs == 1)
20036aadf41dSTakashi Iwai 				pfx = "Speaker";
20043214b966STakashi Iwai 			err = create_ch_ctls(codec, pfx, 3, true, path);
20054a79616dSTakashi Iwai 			if (err < 0)
20064a79616dSTakashi Iwai 				return err;
20074a79616dSTakashi Iwai 		}
2008020066d1STakashi Iwai 		if (path != spec->out_path + i)
2009020066d1STakashi Iwai 			copy_path_mixer_ctls(&spec->out_path[i], path);
2010020066d1STakashi Iwai 		if (path == spec->out_path && spec->out_mix_path.depth)
2011020066d1STakashi Iwai 			copy_path_mixer_ctls(&spec->out_mix_path, path);
20124a79616dSTakashi Iwai 	}
20134a79616dSTakashi Iwai 
20144a79616dSTakashi Iwai 	idx = get_connection_index(codec, spec->aa_mix_nid,
20154a79616dSTakashi Iwai 				   spec->multiout.dac_nids[0]);
20164a79616dSTakashi Iwai 	if (idx >= 0) {
20174a79616dSTakashi Iwai 		/* add control to mixer */
20183214b966STakashi Iwai 		const char *name;
20193214b966STakashi Iwai 		name = spec->out_mix_path.depth ?
20203214b966STakashi Iwai 			"PCM Loopback Playback Volume" : "PCM Playback Volume";
20213214b966STakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
20224a79616dSTakashi Iwai 				      HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
20234a79616dSTakashi Iwai 							  idx, HDA_INPUT));
20244a79616dSTakashi Iwai 		if (err < 0)
20254a79616dSTakashi Iwai 			return err;
20263214b966STakashi Iwai 		name = spec->out_mix_path.depth ?
20273214b966STakashi Iwai 			"PCM Loopback Playback Switch" : "PCM Playback Switch";
20283214b966STakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
20294a79616dSTakashi Iwai 				      HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
20304a79616dSTakashi Iwai 							  idx, HDA_INPUT));
20314a79616dSTakashi Iwai 		if (err < 0)
20324a79616dSTakashi Iwai 			return err;
2033c577b8a1SJoseph Chan 	}
2034c577b8a1SJoseph Chan 
2035f4a7828bSTakashi Iwai 	cfg->line_outs = old_line_outs;
2036f4a7828bSTakashi Iwai 
2037c577b8a1SJoseph Chan 	return 0;
2038c577b8a1SJoseph Chan }
2039c577b8a1SJoseph Chan 
20404a79616dSTakashi Iwai static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
2041c577b8a1SJoseph Chan {
20424a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
204309a9ad69STakashi Iwai 	struct nid_path *path;
204418bd2c44STakashi Iwai 	bool check_dac;
20453214b966STakashi Iwai 	int i, err;
2046c577b8a1SJoseph Chan 
2047c577b8a1SJoseph Chan 	if (!pin)
2048c577b8a1SJoseph Chan 		return 0;
2049c577b8a1SJoseph Chan 
20503214b966STakashi Iwai 	if (!parse_output_path(codec, pin, 0, 0, &spec->hp_indep_path)) {
20513214b966STakashi Iwai 		for (i = HDA_SIDE; i >= HDA_CLFE; i--) {
20523214b966STakashi Iwai 			if (i < spec->multiout.num_dacs &&
205325250505STakashi Iwai 			    parse_output_path(codec, pin,
20543214b966STakashi Iwai 					      spec->multiout.dac_nids[i], 0,
20553214b966STakashi Iwai 					      &spec->hp_indep_path)) {
20563214b966STakashi Iwai 				spec->hp_indep_shared = i;
20573214b966STakashi Iwai 				break;
205825250505STakashi Iwai 			}
20593214b966STakashi Iwai 		}
20603214b966STakashi Iwai 	}
20613214b966STakashi Iwai 	if (spec->hp_indep_path.depth) {
20623214b966STakashi Iwai 		spec->hp_dac_nid = spec->hp_indep_path.path[0];
20633214b966STakashi Iwai 		if (!spec->hp_indep_shared)
20643214b966STakashi Iwai 			spec->hp_path = spec->hp_indep_path;
20653214b966STakashi Iwai 	}
20663214b966STakashi Iwai 	/* optionally check front-path w/o AA-mix */
20673214b966STakashi Iwai 	if (!spec->hp_path.depth)
20683214b966STakashi Iwai 		parse_output_path(codec, pin,
20693214b966STakashi Iwai 				  spec->multiout.dac_nids[HDA_FRONT], 0,
20703214b966STakashi Iwai 				  &spec->hp_path);
20714a79616dSTakashi Iwai 
2072ece8d043STakashi Iwai 	if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
20733214b966STakashi Iwai 			       1, &spec->hp_mix_path) && !spec->hp_path.depth)
2074ece8d043STakashi Iwai 		return 0;
2075ece8d043STakashi Iwai 
20763214b966STakashi Iwai 	if (spec->hp_path.depth) {
207709a9ad69STakashi Iwai 		path = &spec->hp_path;
207818bd2c44STakashi Iwai 		check_dac = true;
207918bd2c44STakashi Iwai 	} else {
20803214b966STakashi Iwai 		path = &spec->hp_mix_path;
208118bd2c44STakashi Iwai 		check_dac = false;
208218bd2c44STakashi Iwai 	}
208318bd2c44STakashi Iwai 	err = create_ch_ctls(codec, "Headphone", 3, check_dac, path);
20844a79616dSTakashi Iwai 	if (err < 0)
20854a79616dSTakashi Iwai 		return err;
2086020066d1STakashi Iwai 	if (check_dac)
2087020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->hp_mix_path, path);
2088020066d1STakashi Iwai 	else
2089020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->hp_path, path);
2090020066d1STakashi Iwai 	if (spec->hp_indep_path.depth)
2091020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->hp_indep_path, path);
2092c577b8a1SJoseph Chan 	return 0;
2093c577b8a1SJoseph Chan }
2094c577b8a1SJoseph Chan 
20954a918ffeSTakashi Iwai static int via_auto_create_speaker_ctls(struct hda_codec *codec)
20964a918ffeSTakashi Iwai {
20974a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
20983214b966STakashi Iwai 	struct nid_path *path;
20993214b966STakashi Iwai 	bool check_dac;
210081c0a78bSWang Shaoyan 	hda_nid_t pin, dac = 0;
21013214b966STakashi Iwai 	int err;
21024a918ffeSTakashi Iwai 
21034a918ffeSTakashi Iwai 	pin = spec->autocfg.speaker_pins[0];
21044a918ffeSTakashi Iwai 	if (!spec->autocfg.speaker_outs || !pin)
21054a918ffeSTakashi Iwai 		return 0;
21064a918ffeSTakashi Iwai 
21073214b966STakashi Iwai 	if (parse_output_path(codec, pin, 0, 0, &spec->speaker_path))
21088e3679dcSTakashi Iwai 		dac = spec->speaker_path.path[0];
21093214b966STakashi Iwai 	if (!dac)
21103214b966STakashi Iwai 		parse_output_path(codec, pin,
21113214b966STakashi Iwai 				  spec->multiout.dac_nids[HDA_FRONT], 0,
211209a9ad69STakashi Iwai 				  &spec->speaker_path);
21133214b966STakashi Iwai 	if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
21143214b966STakashi Iwai 			       1, &spec->speaker_mix_path) && !dac)
21153214b966STakashi Iwai 		return 0;
21164a918ffeSTakashi Iwai 
21173214b966STakashi Iwai 	/* no AA-path for front? */
21183214b966STakashi Iwai 	if (!spec->out_mix_path.depth && spec->speaker_mix_path.depth)
21193214b966STakashi Iwai 		dac = 0;
21203214b966STakashi Iwai 
21213214b966STakashi Iwai 	spec->speaker_dac_nid = dac;
21223214b966STakashi Iwai 	spec->multiout.extra_out_nid[0] = dac;
21233214b966STakashi Iwai 	if (dac) {
21243214b966STakashi Iwai 		path = &spec->speaker_path;
21253214b966STakashi Iwai 		check_dac = true;
21263214b966STakashi Iwai 	} else {
21273214b966STakashi Iwai 		path = &spec->speaker_mix_path;
21283214b966STakashi Iwai 		check_dac = false;
21293214b966STakashi Iwai 	}
21303214b966STakashi Iwai 	err = create_ch_ctls(codec, "Speaker", 3, check_dac, path);
21313214b966STakashi Iwai 	if (err < 0)
21323214b966STakashi Iwai 		return err;
2133020066d1STakashi Iwai 	if (check_dac)
2134020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->speaker_mix_path, path);
2135020066d1STakashi Iwai 	else
2136020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->speaker_path, path);
21373214b966STakashi Iwai 	return 0;
21383214b966STakashi Iwai }
21393214b966STakashi Iwai 
21403214b966STakashi Iwai #define via_aamix_ctl_info	via_pin_power_ctl_info
21413214b966STakashi Iwai 
21423214b966STakashi Iwai static int via_aamix_ctl_get(struct snd_kcontrol *kcontrol,
21433214b966STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
21443214b966STakashi Iwai {
21453214b966STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
21463214b966STakashi Iwai 	struct via_spec *spec = codec->spec;
21473214b966STakashi Iwai 	ucontrol->value.enumerated.item[0] = spec->aamix_mode;
21483214b966STakashi Iwai 	return 0;
21493214b966STakashi Iwai }
21503214b966STakashi Iwai 
21513214b966STakashi Iwai static void update_aamix_paths(struct hda_codec *codec, int do_mix,
21523214b966STakashi Iwai 			       struct nid_path *nomix, struct nid_path *mix)
21533214b966STakashi Iwai {
21543214b966STakashi Iwai 	if (do_mix) {
21553214b966STakashi Iwai 		activate_output_path(codec, nomix, false, false);
21563214b966STakashi Iwai 		activate_output_path(codec, mix, true, false);
21573214b966STakashi Iwai 	} else {
21583214b966STakashi Iwai 		activate_output_path(codec, mix, false, false);
21593214b966STakashi Iwai 		activate_output_path(codec, nomix, true, false);
21603214b966STakashi Iwai 	}
21613214b966STakashi Iwai }
21623214b966STakashi Iwai 
21633214b966STakashi Iwai static int via_aamix_ctl_put(struct snd_kcontrol *kcontrol,
21643214b966STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
21653214b966STakashi Iwai {
21663214b966STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
21673214b966STakashi Iwai 	struct via_spec *spec = codec->spec;
21683214b966STakashi Iwai 	unsigned int val = ucontrol->value.enumerated.item[0];
21693214b966STakashi Iwai 
21703214b966STakashi Iwai 	if (val == spec->aamix_mode)
21713214b966STakashi Iwai 		return 0;
21723214b966STakashi Iwai 	spec->aamix_mode = val;
21733214b966STakashi Iwai 	/* update front path */
21743214b966STakashi Iwai 	update_aamix_paths(codec, val, &spec->out_path[0], &spec->out_mix_path);
21753214b966STakashi Iwai 	/* update HP path */
21763214b966STakashi Iwai 	if (!spec->hp_independent_mode) {
21773214b966STakashi Iwai 		update_aamix_paths(codec, val, &spec->hp_path,
21783214b966STakashi Iwai 				   &spec->hp_mix_path);
21793214b966STakashi Iwai 	}
21803214b966STakashi Iwai 	/* update speaker path */
21813214b966STakashi Iwai 	update_aamix_paths(codec, val, &spec->speaker_path,
21823214b966STakashi Iwai 			   &spec->speaker_mix_path);
21833214b966STakashi Iwai 	return 1;
21843214b966STakashi Iwai }
21853214b966STakashi Iwai 
21863214b966STakashi Iwai static const struct snd_kcontrol_new via_aamix_ctl_enum = {
21873214b966STakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
21883214b966STakashi Iwai 	.name = "Loopback Mixing",
21893214b966STakashi Iwai 	.info = via_aamix_ctl_info,
21903214b966STakashi Iwai 	.get = via_aamix_ctl_get,
21913214b966STakashi Iwai 	.put = via_aamix_ctl_put,
21923214b966STakashi Iwai };
21933214b966STakashi Iwai 
21943214b966STakashi Iwai static int via_auto_create_loopback_switch(struct hda_codec *codec)
21953214b966STakashi Iwai {
21963214b966STakashi Iwai 	struct via_spec *spec = codec->spec;
21973214b966STakashi Iwai 
21983214b966STakashi Iwai 	if (!spec->aa_mix_nid || !spec->out_mix_path.depth)
21993214b966STakashi Iwai 		return 0; /* no loopback switching available */
22003214b966STakashi Iwai 	if (!via_clone_control(spec, &via_aamix_ctl_enum))
22013214b966STakashi Iwai 		return -ENOMEM;
22024a918ffeSTakashi Iwai 	return 0;
22034a918ffeSTakashi Iwai }
22044a918ffeSTakashi Iwai 
2205a766d0d7STakashi Iwai /* look for ADCs */
2206a766d0d7STakashi Iwai static int via_fill_adcs(struct hda_codec *codec)
2207a766d0d7STakashi Iwai {
2208a766d0d7STakashi Iwai 	struct via_spec *spec = codec->spec;
2209a766d0d7STakashi Iwai 	hda_nid_t nid = codec->start_nid;
2210a766d0d7STakashi Iwai 	int i;
2211a766d0d7STakashi Iwai 
2212a766d0d7STakashi Iwai 	for (i = 0; i < codec->num_nodes; i++, nid++) {
2213a766d0d7STakashi Iwai 		unsigned int wcaps = get_wcaps(codec, nid);
2214a766d0d7STakashi Iwai 		if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2215a766d0d7STakashi Iwai 			continue;
2216a766d0d7STakashi Iwai 		if (wcaps & AC_WCAP_DIGITAL)
2217a766d0d7STakashi Iwai 			continue;
2218a766d0d7STakashi Iwai 		if (!(wcaps & AC_WCAP_CONN_LIST))
2219a766d0d7STakashi Iwai 			continue;
2220a766d0d7STakashi Iwai 		if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
2221a766d0d7STakashi Iwai 			return -ENOMEM;
2222a766d0d7STakashi Iwai 		spec->adc_nids[spec->num_adc_nids++] = nid;
2223a766d0d7STakashi Iwai 	}
2224a766d0d7STakashi Iwai 	return 0;
2225a766d0d7STakashi Iwai }
2226a766d0d7STakashi Iwai 
2227a86a88eaSTakashi Iwai /* input-src control */
2228a86a88eaSTakashi Iwai static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
2229a86a88eaSTakashi Iwai 			     struct snd_ctl_elem_info *uinfo)
2230a86a88eaSTakashi Iwai {
2231a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2232a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2233a86a88eaSTakashi Iwai 
2234a86a88eaSTakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2235a86a88eaSTakashi Iwai 	uinfo->count = 1;
2236a86a88eaSTakashi Iwai 	uinfo->value.enumerated.items = spec->num_inputs;
2237a86a88eaSTakashi Iwai 	if (uinfo->value.enumerated.item >= spec->num_inputs)
2238a86a88eaSTakashi Iwai 		uinfo->value.enumerated.item = spec->num_inputs - 1;
2239a86a88eaSTakashi Iwai 	strcpy(uinfo->value.enumerated.name,
2240a86a88eaSTakashi Iwai 	       spec->inputs[uinfo->value.enumerated.item].label);
2241a86a88eaSTakashi Iwai 	return 0;
2242a86a88eaSTakashi Iwai }
2243a86a88eaSTakashi Iwai 
2244a86a88eaSTakashi Iwai static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
2245a86a88eaSTakashi Iwai 			    struct snd_ctl_elem_value *ucontrol)
2246a86a88eaSTakashi Iwai {
2247a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2248a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2249a86a88eaSTakashi Iwai 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2250a86a88eaSTakashi Iwai 
2251a86a88eaSTakashi Iwai 	ucontrol->value.enumerated.item[0] = spec->cur_mux[idx];
2252a86a88eaSTakashi Iwai 	return 0;
2253a86a88eaSTakashi Iwai }
2254a86a88eaSTakashi Iwai 
2255a86a88eaSTakashi Iwai static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
2256a86a88eaSTakashi Iwai 			    struct snd_ctl_elem_value *ucontrol)
2257a86a88eaSTakashi Iwai {
2258a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2259a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2260a86a88eaSTakashi Iwai 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2261a86a88eaSTakashi Iwai 	hda_nid_t mux;
2262a86a88eaSTakashi Iwai 	int cur;
2263a86a88eaSTakashi Iwai 
2264a86a88eaSTakashi Iwai 	cur = ucontrol->value.enumerated.item[0];
2265a86a88eaSTakashi Iwai 	if (cur < 0 || cur >= spec->num_inputs)
2266a86a88eaSTakashi Iwai 		return -EINVAL;
2267a86a88eaSTakashi Iwai 	if (spec->cur_mux[idx] == cur)
2268a86a88eaSTakashi Iwai 		return 0;
2269a86a88eaSTakashi Iwai 	spec->cur_mux[idx] = cur;
2270a86a88eaSTakashi Iwai 	if (spec->dyn_adc_switch) {
2271a86a88eaSTakashi Iwai 		int adc_idx = spec->inputs[cur].adc_idx;
2272a86a88eaSTakashi Iwai 		mux = spec->mux_nids[adc_idx];
2273a86a88eaSTakashi Iwai 		via_dyn_adc_pcm_resetup(codec, cur);
2274a86a88eaSTakashi Iwai 	} else {
2275a86a88eaSTakashi Iwai 		mux = spec->mux_nids[idx];
2276a86a88eaSTakashi Iwai 		if (snd_BUG_ON(!mux))
2277a86a88eaSTakashi Iwai 			return -EINVAL;
2278a86a88eaSTakashi Iwai 	}
2279a86a88eaSTakashi Iwai 
2280a86a88eaSTakashi Iwai 	if (mux) {
2281a86a88eaSTakashi Iwai 		/* switch to D0 beofre change index */
2282a86a88eaSTakashi Iwai 		if (snd_hda_codec_read(codec, mux, 0,
2283a86a88eaSTakashi Iwai 			       AC_VERB_GET_POWER_STATE, 0x00) != AC_PWRST_D0)
2284a86a88eaSTakashi Iwai 			snd_hda_codec_write(codec, mux, 0,
2285a86a88eaSTakashi Iwai 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
2286a86a88eaSTakashi Iwai 		snd_hda_codec_write(codec, mux, 0,
2287a86a88eaSTakashi Iwai 				    AC_VERB_SET_CONNECT_SEL,
2288a86a88eaSTakashi Iwai 				    spec->inputs[cur].mux_idx);
2289a86a88eaSTakashi Iwai 	}
2290a86a88eaSTakashi Iwai 
2291a86a88eaSTakashi Iwai 	/* update jack power state */
2292a86a88eaSTakashi Iwai 	set_widgets_power_state(codec);
2293a86a88eaSTakashi Iwai 	return 0;
2294a86a88eaSTakashi Iwai }
2295a766d0d7STakashi Iwai 
2296d7a99cceSTakashi Iwai static const struct snd_kcontrol_new via_input_src_ctl = {
2297d7a99cceSTakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2298d7a99cceSTakashi Iwai 	/* The multiple "Capture Source" controls confuse alsamixer
2299d7a99cceSTakashi Iwai 	 * So call somewhat different..
2300d7a99cceSTakashi Iwai 	 */
2301d7a99cceSTakashi Iwai 	/* .name = "Capture Source", */
2302d7a99cceSTakashi Iwai 	.name = "Input Source",
2303d7a99cceSTakashi Iwai 	.info = via_mux_enum_info,
2304d7a99cceSTakashi Iwai 	.get = via_mux_enum_get,
2305d7a99cceSTakashi Iwai 	.put = via_mux_enum_put,
2306d7a99cceSTakashi Iwai };
2307d7a99cceSTakashi Iwai 
2308a86a88eaSTakashi Iwai static int create_input_src_ctls(struct hda_codec *codec, int count)
2309a86a88eaSTakashi Iwai {
2310a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2311a86a88eaSTakashi Iwai 	struct snd_kcontrol_new *knew;
2312a86a88eaSTakashi Iwai 
2313a86a88eaSTakashi Iwai 	if (spec->num_inputs <= 1 || !count)
2314a86a88eaSTakashi Iwai 		return 0; /* no need for single src */
2315a86a88eaSTakashi Iwai 
2316a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_input_src_ctl);
2317a86a88eaSTakashi Iwai 	if (!knew)
2318a86a88eaSTakashi Iwai 		return -ENOMEM;
2319a86a88eaSTakashi Iwai 	knew->count = count;
2320a86a88eaSTakashi Iwai 	return 0;
2321a86a88eaSTakashi Iwai }
2322a86a88eaSTakashi Iwai 
2323a86a88eaSTakashi Iwai /* add the powersave loopback-list entry */
232413af8e77STakashi Iwai static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
232513af8e77STakashi Iwai {
232613af8e77STakashi Iwai 	struct hda_amp_list *list;
232713af8e77STakashi Iwai 
232813af8e77STakashi Iwai 	if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
232913af8e77STakashi Iwai 		return;
233013af8e77STakashi Iwai 	list = spec->loopback_list + spec->num_loopbacks;
233113af8e77STakashi Iwai 	list->nid = mix;
233213af8e77STakashi Iwai 	list->dir = HDA_INPUT;
233313af8e77STakashi Iwai 	list->idx = idx;
233413af8e77STakashi Iwai 	spec->num_loopbacks++;
233513af8e77STakashi Iwai 	spec->loopback.amplist = spec->loopback_list;
233613af8e77STakashi Iwai }
233713af8e77STakashi Iwai 
2338a86a88eaSTakashi Iwai static bool is_reachable_nid(struct hda_codec *codec, hda_nid_t src,
23398d087c76STakashi Iwai 			     hda_nid_t dst)
2340a86a88eaSTakashi Iwai {
23418d087c76STakashi Iwai 	return snd_hda_get_conn_index(codec, src, dst, 1) >= 0;
2342a86a88eaSTakashi Iwai }
2343a86a88eaSTakashi Iwai 
2344a86a88eaSTakashi Iwai /* add the input-route to the given pin */
2345a86a88eaSTakashi Iwai static bool add_input_route(struct hda_codec *codec, hda_nid_t pin)
2346c577b8a1SJoseph Chan {
234710a20af7STakashi Iwai 	struct via_spec *spec = codec->spec;
2348a86a88eaSTakashi Iwai 	int c, idx;
2349a86a88eaSTakashi Iwai 
2350a86a88eaSTakashi Iwai 	spec->inputs[spec->num_inputs].adc_idx = -1;
2351a86a88eaSTakashi Iwai 	spec->inputs[spec->num_inputs].pin = pin;
2352a86a88eaSTakashi Iwai 	for (c = 0; c < spec->num_adc_nids; c++) {
2353a86a88eaSTakashi Iwai 		if (spec->mux_nids[c]) {
2354a86a88eaSTakashi Iwai 			idx = get_connection_index(codec, spec->mux_nids[c],
2355a86a88eaSTakashi Iwai 						   pin);
2356a86a88eaSTakashi Iwai 			if (idx < 0)
2357a86a88eaSTakashi Iwai 				continue;
2358a86a88eaSTakashi Iwai 			spec->inputs[spec->num_inputs].mux_idx = idx;
2359a86a88eaSTakashi Iwai 		} else {
23608d087c76STakashi Iwai 			if (!is_reachable_nid(codec, spec->adc_nids[c], pin))
2361a86a88eaSTakashi Iwai 				continue;
2362a86a88eaSTakashi Iwai 		}
2363a86a88eaSTakashi Iwai 		spec->inputs[spec->num_inputs].adc_idx = c;
2364a86a88eaSTakashi Iwai 		/* Can primary ADC satisfy all inputs? */
2365a86a88eaSTakashi Iwai 		if (!spec->dyn_adc_switch &&
2366a86a88eaSTakashi Iwai 		    spec->num_inputs > 0 && spec->inputs[0].adc_idx != c) {
2367a86a88eaSTakashi Iwai 			snd_printd(KERN_INFO
2368a86a88eaSTakashi Iwai 				   "via: dynamic ADC switching enabled\n");
2369a86a88eaSTakashi Iwai 			spec->dyn_adc_switch = 1;
2370a86a88eaSTakashi Iwai 		}
2371a86a88eaSTakashi Iwai 		return true;
2372a86a88eaSTakashi Iwai 	}
2373a86a88eaSTakashi Iwai 	return false;
2374a86a88eaSTakashi Iwai }
2375a86a88eaSTakashi Iwai 
2376a86a88eaSTakashi Iwai static int get_mux_nids(struct hda_codec *codec);
2377a86a88eaSTakashi Iwai 
2378a86a88eaSTakashi Iwai /* parse input-routes; fill ADCs, MUXs and input-src entries */
2379a86a88eaSTakashi Iwai static int parse_analog_inputs(struct hda_codec *codec)
2380a86a88eaSTakashi Iwai {
2381a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2382a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2383a86a88eaSTakashi Iwai 	int i, err;
2384a766d0d7STakashi Iwai 
2385a766d0d7STakashi Iwai 	err = via_fill_adcs(codec);
2386a766d0d7STakashi Iwai 	if (err < 0)
2387a766d0d7STakashi Iwai 		return err;
2388a766d0d7STakashi Iwai 	err = get_mux_nids(codec);
2389a766d0d7STakashi Iwai 	if (err < 0)
2390a766d0d7STakashi Iwai 		return err;
2391a766d0d7STakashi Iwai 
2392a86a88eaSTakashi Iwai 	/* fill all input-routes */
2393a86a88eaSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2394a86a88eaSTakashi Iwai 		if (add_input_route(codec, cfg->inputs[i].pin))
2395a86a88eaSTakashi Iwai 			spec->inputs[spec->num_inputs++].label =
2396a86a88eaSTakashi Iwai 				hda_get_autocfg_input_label(codec, cfg, i);
2397a86a88eaSTakashi Iwai 	}
2398a86a88eaSTakashi Iwai 
2399a86a88eaSTakashi Iwai 	/* check for internal loopback recording */
2400a86a88eaSTakashi Iwai 	if (spec->aa_mix_nid &&
2401a86a88eaSTakashi Iwai 	    add_input_route(codec, spec->aa_mix_nid))
2402a86a88eaSTakashi Iwai 		spec->inputs[spec->num_inputs++].label = "Stereo Mixer";
2403a86a88eaSTakashi Iwai 
2404a86a88eaSTakashi Iwai 	return 0;
2405a86a88eaSTakashi Iwai }
2406a86a88eaSTakashi Iwai 
2407a86a88eaSTakashi Iwai /* create analog-loopback volume/switch controls */
2408a86a88eaSTakashi Iwai static int create_loopback_ctls(struct hda_codec *codec)
2409a86a88eaSTakashi Iwai {
2410a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2411a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2412a86a88eaSTakashi Iwai 	const char *prev_label = NULL;
2413a86a88eaSTakashi Iwai 	int type_idx = 0;
2414a86a88eaSTakashi Iwai 	int i, j, err, idx;
2415a86a88eaSTakashi Iwai 
2416a86a88eaSTakashi Iwai 	if (!spec->aa_mix_nid)
2417a766d0d7STakashi Iwai 		return 0;
2418c577b8a1SJoseph Chan 
24197b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2420a86a88eaSTakashi Iwai 		hda_nid_t pin = cfg->inputs[i].pin;
2421a86a88eaSTakashi Iwai 		const char *label = hda_get_autocfg_input_label(codec, cfg, i);
2422a86a88eaSTakashi Iwai 
24231e11cae1STakashi Iwai 		if (prev_label && !strcmp(label, prev_label))
24247b315bb4STakashi Iwai 			type_idx++;
24257b315bb4STakashi Iwai 		else
24267b315bb4STakashi Iwai 			type_idx = 0;
24271e11cae1STakashi Iwai 		prev_label = label;
2428a86a88eaSTakashi Iwai 		idx = get_connection_index(codec, spec->aa_mix_nid, pin);
2429a86a88eaSTakashi Iwai 		if (idx >= 0) {
243016922281SLydia Wang 			err = via_new_analog_input(spec, label, type_idx,
2431a86a88eaSTakashi Iwai 						   idx, spec->aa_mix_nid);
2432c577b8a1SJoseph Chan 			if (err < 0)
2433c577b8a1SJoseph Chan 				return err;
2434a86a88eaSTakashi Iwai 			add_loopback_list(spec, spec->aa_mix_nid, idx);
243513af8e77STakashi Iwai 		}
2436e3d7a143STakashi Iwai 
2437e3d7a143STakashi Iwai 		/* remember the label for smart51 control */
2438e3d7a143STakashi Iwai 		for (j = 0; j < spec->smart51_nums; j++) {
2439a86a88eaSTakashi Iwai 			if (spec->smart51_pins[j] == pin) {
2440e3d7a143STakashi Iwai 				spec->smart51_idxs[j] = idx;
2441e3d7a143STakashi Iwai 				spec->smart51_labels[j] = label;
2442e3d7a143STakashi Iwai 				break;
2443e3d7a143STakashi Iwai 			}
2444e3d7a143STakashi Iwai 		}
2445c577b8a1SJoseph Chan 	}
2446a86a88eaSTakashi Iwai 	return 0;
2447a86a88eaSTakashi Iwai }
2448a86a88eaSTakashi Iwai 
2449a86a88eaSTakashi Iwai /* create mic-boost controls (if present) */
2450a86a88eaSTakashi Iwai static int create_mic_boost_ctls(struct hda_codec *codec)
2451a86a88eaSTakashi Iwai {
2452a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2453a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2454a86a88eaSTakashi Iwai 	int i, err;
2455a86a88eaSTakashi Iwai 
2456a86a88eaSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2457a86a88eaSTakashi Iwai 		hda_nid_t pin = cfg->inputs[i].pin;
2458a86a88eaSTakashi Iwai 		unsigned int caps;
2459a86a88eaSTakashi Iwai 		const char *label;
2460a86a88eaSTakashi Iwai 		char name[32];
2461a86a88eaSTakashi Iwai 
2462a86a88eaSTakashi Iwai 		if (cfg->inputs[i].type != AUTO_PIN_MIC)
2463a86a88eaSTakashi Iwai 			continue;
2464a86a88eaSTakashi Iwai 		caps = query_amp_caps(codec, pin, HDA_INPUT);
2465a86a88eaSTakashi Iwai 		if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
2466a86a88eaSTakashi Iwai 			continue;
2467a86a88eaSTakashi Iwai 		label = hda_get_autocfg_input_label(codec, cfg, i);
2468a86a88eaSTakashi Iwai 		snprintf(name, sizeof(name), "%s Boost Volume", label);
2469a86a88eaSTakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2470a86a88eaSTakashi Iwai 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
2471a86a88eaSTakashi Iwai 		if (err < 0)
2472a86a88eaSTakashi Iwai 			return err;
2473a86a88eaSTakashi Iwai 	}
2474a86a88eaSTakashi Iwai 	return 0;
2475a86a88eaSTakashi Iwai }
2476a86a88eaSTakashi Iwai 
2477a86a88eaSTakashi Iwai /* create capture and input-src controls for multiple streams */
2478a86a88eaSTakashi Iwai static int create_multi_adc_ctls(struct hda_codec *codec)
2479a86a88eaSTakashi Iwai {
2480a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2481a86a88eaSTakashi Iwai 	int i, err;
2482d7a99cceSTakashi Iwai 
2483d7a99cceSTakashi Iwai 	/* create capture mixer elements */
2484d7a99cceSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
2485d7a99cceSTakashi Iwai 		hda_nid_t adc = spec->adc_nids[i];
2486d7a99cceSTakashi Iwai 		err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
2487d7a99cceSTakashi Iwai 					"Capture Volume", i,
2488d7a99cceSTakashi Iwai 					HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2489d7a99cceSTakashi Iwai 							    HDA_INPUT));
2490d7a99cceSTakashi Iwai 		if (err < 0)
2491d7a99cceSTakashi Iwai 			return err;
2492d7a99cceSTakashi Iwai 		err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2493d7a99cceSTakashi Iwai 					"Capture Switch", i,
2494d7a99cceSTakashi Iwai 					HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2495d7a99cceSTakashi Iwai 							    HDA_INPUT));
2496d7a99cceSTakashi Iwai 		if (err < 0)
2497d7a99cceSTakashi Iwai 			return err;
2498d7a99cceSTakashi Iwai 	}
2499d7a99cceSTakashi Iwai 
2500d7a99cceSTakashi Iwai 	/* input-source control */
2501d7a99cceSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++)
2502d7a99cceSTakashi Iwai 		if (!spec->mux_nids[i])
2503d7a99cceSTakashi Iwai 			break;
2504a86a88eaSTakashi Iwai 	err = create_input_src_ctls(codec, i);
2505d7a99cceSTakashi Iwai 	if (err < 0)
2506d7a99cceSTakashi Iwai 		return err;
2507a86a88eaSTakashi Iwai 	return 0;
2508d7a99cceSTakashi Iwai }
2509d7a99cceSTakashi Iwai 
2510a86a88eaSTakashi Iwai /* bind capture volume/switch */
2511a86a88eaSTakashi Iwai static struct snd_kcontrol_new via_bind_cap_vol_ctl =
2512a86a88eaSTakashi Iwai 	HDA_BIND_VOL("Capture Volume", 0);
2513a86a88eaSTakashi Iwai static struct snd_kcontrol_new via_bind_cap_sw_ctl =
2514a86a88eaSTakashi Iwai 	HDA_BIND_SW("Capture Switch", 0);
2515a86a88eaSTakashi Iwai 
2516a86a88eaSTakashi Iwai static int init_bind_ctl(struct via_spec *spec, struct hda_bind_ctls **ctl_ret,
2517a86a88eaSTakashi Iwai 			 struct hda_ctl_ops *ops)
2518a86a88eaSTakashi Iwai {
2519a86a88eaSTakashi Iwai 	struct hda_bind_ctls *ctl;
2520a86a88eaSTakashi Iwai 	int i;
2521a86a88eaSTakashi Iwai 
2522a86a88eaSTakashi Iwai 	ctl = kzalloc(sizeof(*ctl) + sizeof(long) * 4, GFP_KERNEL);
2523a86a88eaSTakashi Iwai 	if (!ctl)
2524a86a88eaSTakashi Iwai 		return -ENOMEM;
2525a86a88eaSTakashi Iwai 	ctl->ops = ops;
2526a86a88eaSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++)
2527a86a88eaSTakashi Iwai 		ctl->values[i] =
2528a86a88eaSTakashi Iwai 			HDA_COMPOSE_AMP_VAL(spec->adc_nids[i], 3, 0, HDA_INPUT);
2529a86a88eaSTakashi Iwai 	*ctl_ret = ctl;
2530a86a88eaSTakashi Iwai 	return 0;
2531a86a88eaSTakashi Iwai }
2532a86a88eaSTakashi Iwai 
2533a86a88eaSTakashi Iwai /* create capture and input-src controls for dynamic ADC-switch case */
2534a86a88eaSTakashi Iwai static int create_dyn_adc_ctls(struct hda_codec *codec)
2535a86a88eaSTakashi Iwai {
2536a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2537a86a88eaSTakashi Iwai 	struct snd_kcontrol_new *knew;
2538a86a88eaSTakashi Iwai 	int err;
2539a86a88eaSTakashi Iwai 
2540a86a88eaSTakashi Iwai 	/* set up the bind capture ctls */
2541a86a88eaSTakashi Iwai 	err = init_bind_ctl(spec, &spec->bind_cap_vol, &snd_hda_bind_vol);
2542a86a88eaSTakashi Iwai 	if (err < 0)
2543a86a88eaSTakashi Iwai 		return err;
2544a86a88eaSTakashi Iwai 	err = init_bind_ctl(spec, &spec->bind_cap_sw, &snd_hda_bind_sw);
2545a86a88eaSTakashi Iwai 	if (err < 0)
2546a86a88eaSTakashi Iwai 		return err;
2547a86a88eaSTakashi Iwai 
2548a86a88eaSTakashi Iwai 	/* create capture mixer elements */
2549a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_bind_cap_vol_ctl);
2550a86a88eaSTakashi Iwai 	if (!knew)
2551a86a88eaSTakashi Iwai 		return -ENOMEM;
2552a86a88eaSTakashi Iwai 	knew->private_value = (long)spec->bind_cap_vol;
2553a86a88eaSTakashi Iwai 
2554a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_bind_cap_sw_ctl);
2555a86a88eaSTakashi Iwai 	if (!knew)
2556a86a88eaSTakashi Iwai 		return -ENOMEM;
2557a86a88eaSTakashi Iwai 	knew->private_value = (long)spec->bind_cap_sw;
2558a86a88eaSTakashi Iwai 
2559a86a88eaSTakashi Iwai 	/* input-source control */
2560a86a88eaSTakashi Iwai 	err = create_input_src_ctls(codec, 1);
2561a86a88eaSTakashi Iwai 	if (err < 0)
2562a86a88eaSTakashi Iwai 		return err;
2563a86a88eaSTakashi Iwai 	return 0;
2564a86a88eaSTakashi Iwai }
2565a86a88eaSTakashi Iwai 
2566a86a88eaSTakashi Iwai /* parse and create capture-related stuff */
2567a86a88eaSTakashi Iwai static int via_auto_create_analog_input_ctls(struct hda_codec *codec)
2568a86a88eaSTakashi Iwai {
2569a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2570a86a88eaSTakashi Iwai 	int err;
2571a86a88eaSTakashi Iwai 
2572a86a88eaSTakashi Iwai 	err = parse_analog_inputs(codec);
2573a86a88eaSTakashi Iwai 	if (err < 0)
2574a86a88eaSTakashi Iwai 		return err;
2575a86a88eaSTakashi Iwai 	if (spec->dyn_adc_switch)
2576a86a88eaSTakashi Iwai 		err = create_dyn_adc_ctls(codec);
2577a86a88eaSTakashi Iwai 	else
2578a86a88eaSTakashi Iwai 		err = create_multi_adc_ctls(codec);
2579a86a88eaSTakashi Iwai 	if (err < 0)
2580a86a88eaSTakashi Iwai 		return err;
2581a86a88eaSTakashi Iwai 	err = create_loopback_ctls(codec);
2582a86a88eaSTakashi Iwai 	if (err < 0)
2583a86a88eaSTakashi Iwai 		return err;
2584a86a88eaSTakashi Iwai 	err = create_mic_boost_ctls(codec);
2585a86a88eaSTakashi Iwai 	if (err < 0)
2586a86a88eaSTakashi Iwai 		return err;
2587c577b8a1SJoseph Chan 	return 0;
2588c577b8a1SJoseph Chan }
2589c577b8a1SJoseph Chan 
259076d9b0ddSHarald Welte static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
259176d9b0ddSHarald Welte {
259276d9b0ddSHarald Welte 	unsigned int def_conf;
259376d9b0ddSHarald Welte 	unsigned char seqassoc;
259476d9b0ddSHarald Welte 
25952f334f92STakashi Iwai 	def_conf = snd_hda_codec_get_pincfg(codec, nid);
259676d9b0ddSHarald Welte 	seqassoc = (unsigned char) get_defcfg_association(def_conf);
259776d9b0ddSHarald Welte 	seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
259882ef9e45SLydia Wang 	if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
259982ef9e45SLydia Wang 	    && (seqassoc == 0xf0 || seqassoc == 0xff)) {
260076d9b0ddSHarald Welte 		def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
26012f334f92STakashi Iwai 		snd_hda_codec_set_pincfg(codec, nid, def_conf);
260276d9b0ddSHarald Welte 	}
260376d9b0ddSHarald Welte 
260476d9b0ddSHarald Welte 	return;
260576d9b0ddSHarald Welte }
260676d9b0ddSHarald Welte 
2607e06e5a29STakashi Iwai static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
26081f2e99feSLydia Wang 				     struct snd_ctl_elem_value *ucontrol)
26091f2e99feSLydia Wang {
26101f2e99feSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
26111f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
26121f2e99feSLydia Wang 
26131f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
26141f2e99feSLydia Wang 		return 0;
2615e06e5a29STakashi Iwai 	spec->vt1708_jack_detect =
26161f2e99feSLydia Wang 		!((snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8) & 0x1);
2617e06e5a29STakashi Iwai 	ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
26181f2e99feSLydia Wang 	return 0;
26191f2e99feSLydia Wang }
26201f2e99feSLydia Wang 
2621e06e5a29STakashi Iwai static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
26221f2e99feSLydia Wang 				     struct snd_ctl_elem_value *ucontrol)
26231f2e99feSLydia Wang {
26241f2e99feSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
26251f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
26261f2e99feSLydia Wang 	int change;
26271f2e99feSLydia Wang 
26281f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
26291f2e99feSLydia Wang 		return 0;
2630e06e5a29STakashi Iwai 	spec->vt1708_jack_detect = ucontrol->value.integer.value[0];
26311f2e99feSLydia Wang 	change = (0x1 & (snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8))
2632e06e5a29STakashi Iwai 		== !spec->vt1708_jack_detect;
2633e06e5a29STakashi Iwai 	if (spec->vt1708_jack_detect) {
26341f2e99feSLydia Wang 		mute_aa_path(codec, 1);
26351f2e99feSLydia Wang 		notify_aa_path_ctls(codec);
26361f2e99feSLydia Wang 	}
26371f2e99feSLydia Wang 	return change;
26381f2e99feSLydia Wang }
26391f2e99feSLydia Wang 
2640e06e5a29STakashi Iwai static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
26411f2e99feSLydia Wang 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
26421f2e99feSLydia Wang 	.name = "Jack Detect",
26431f2e99feSLydia Wang 	.count = 1,
26441f2e99feSLydia Wang 	.info = snd_ctl_boolean_mono_info,
2645e06e5a29STakashi Iwai 	.get = vt1708_jack_detect_get,
2646e06e5a29STakashi Iwai 	.put = vt1708_jack_detect_put,
26471f2e99feSLydia Wang };
26481f2e99feSLydia Wang 
264912daef65STakashi Iwai static void fill_dig_outs(struct hda_codec *codec);
265012daef65STakashi Iwai static void fill_dig_in(struct hda_codec *codec);
265112daef65STakashi Iwai 
265212daef65STakashi Iwai static int via_parse_auto_config(struct hda_codec *codec)
2653c577b8a1SJoseph Chan {
2654c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
2655c577b8a1SJoseph Chan 	int err;
2656c577b8a1SJoseph Chan 
2657c577b8a1SJoseph Chan 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2658c577b8a1SJoseph Chan 	if (err < 0)
2659c577b8a1SJoseph Chan 		return err;
2660c577b8a1SJoseph Chan 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
26617f0df88cSTakashi Iwai 		return -EINVAL;
2662c577b8a1SJoseph Chan 
26634a79616dSTakashi Iwai 	err = via_auto_create_multi_out_ctls(codec);
2664c577b8a1SJoseph Chan 	if (err < 0)
2665c577b8a1SJoseph Chan 		return err;
26664a79616dSTakashi Iwai 	err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
2667c577b8a1SJoseph Chan 	if (err < 0)
2668c577b8a1SJoseph Chan 		return err;
26694a918ffeSTakashi Iwai 	err = via_auto_create_speaker_ctls(codec);
26704a918ffeSTakashi Iwai 	if (err < 0)
26714a918ffeSTakashi Iwai 		return err;
26723214b966STakashi Iwai 	err = via_auto_create_loopback_switch(codec);
26733214b966STakashi Iwai 	if (err < 0)
26743214b966STakashi Iwai 		return err;
2675a86a88eaSTakashi Iwai 	err = via_auto_create_analog_input_ctls(codec);
2676c577b8a1SJoseph Chan 	if (err < 0)
2677c577b8a1SJoseph Chan 		return err;
2678c577b8a1SJoseph Chan 
2679c577b8a1SJoseph Chan 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2680c577b8a1SJoseph Chan 
268112daef65STakashi Iwai 	fill_dig_outs(codec);
268212daef65STakashi Iwai 	fill_dig_in(codec);
2683c577b8a1SJoseph Chan 
2684603c4019STakashi Iwai 	if (spec->kctls.list)
2685603c4019STakashi Iwai 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
2686c577b8a1SJoseph Chan 
2687c577b8a1SJoseph Chan 
26883214b966STakashi Iwai 	if (spec->hp_dac_nid && spec->hp_mix_path.depth) {
2689ece8d043STakashi Iwai 		err = via_hp_build(codec);
2690ece8d043STakashi Iwai 		if (err < 0)
2691ece8d043STakashi Iwai 			return err;
2692ece8d043STakashi Iwai 	}
2693c577b8a1SJoseph Chan 
2694f4a7828bSTakashi Iwai 	err = via_smart51_build(codec);
2695f4a7828bSTakashi Iwai 	if (err < 0)
2696f4a7828bSTakashi Iwai 		return err;
2697f4a7828bSTakashi Iwai 
26985d41762aSTakashi Iwai 	/* assign slave outs */
26995d41762aSTakashi Iwai 	if (spec->slave_dig_outs[0])
27005d41762aSTakashi Iwai 		codec->slave_dig_outs = spec->slave_dig_outs;
27015d41762aSTakashi Iwai 
2702c577b8a1SJoseph Chan 	return 1;
2703c577b8a1SJoseph Chan }
2704c577b8a1SJoseph Chan 
27055d41762aSTakashi Iwai static void via_auto_init_dig_outs(struct hda_codec *codec)
2706c577b8a1SJoseph Chan {
270725eaba2fSLydia Wang 	struct via_spec *spec = codec->spec;
27085d41762aSTakashi Iwai 	if (spec->multiout.dig_out_nid)
27095d41762aSTakashi Iwai 		init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
27105d41762aSTakashi Iwai 	if (spec->slave_dig_outs[0])
27115d41762aSTakashi Iwai 		init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
27125d41762aSTakashi Iwai }
271325eaba2fSLydia Wang 
27145d41762aSTakashi Iwai static void via_auto_init_dig_in(struct hda_codec *codec)
27155d41762aSTakashi Iwai {
27165d41762aSTakashi Iwai 	struct via_spec *spec = codec->spec;
27175d41762aSTakashi Iwai 	if (!spec->dig_in_nid)
27185d41762aSTakashi Iwai 		return;
27195d41762aSTakashi Iwai 	snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
27205d41762aSTakashi Iwai 			    AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
27215d41762aSTakashi Iwai }
27225d41762aSTakashi Iwai 
27234a918ffeSTakashi Iwai /* initialize the unsolicited events */
27244a918ffeSTakashi Iwai static void via_auto_init_unsol_event(struct hda_codec *codec)
27254a918ffeSTakashi Iwai {
27264a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
27274a918ffeSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
27284a918ffeSTakashi Iwai 	unsigned int ev;
27294a918ffeSTakashi Iwai 	int i;
27304a918ffeSTakashi Iwai 
27314a918ffeSTakashi Iwai 	if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
27324a918ffeSTakashi Iwai 		snd_hda_codec_write(codec, cfg->hp_pins[0], 0,
27334a918ffeSTakashi Iwai 				AC_VERB_SET_UNSOLICITED_ENABLE,
27344a918ffeSTakashi Iwai 				AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT);
27354a918ffeSTakashi Iwai 
27364a918ffeSTakashi Iwai 	if (cfg->speaker_pins[0])
27374a918ffeSTakashi Iwai 		ev = VIA_LINE_EVENT;
27384a918ffeSTakashi Iwai 	else
27394a918ffeSTakashi Iwai 		ev = 0;
27404a918ffeSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
27414a918ffeSTakashi Iwai 		if (cfg->line_out_pins[i] &&
27424a918ffeSTakashi Iwai 		    is_jack_detectable(codec, cfg->line_out_pins[i]))
274363f10d2cSLydia Wang 			snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
27444a918ffeSTakashi Iwai 				AC_VERB_SET_UNSOLICITED_ENABLE,
27454a918ffeSTakashi Iwai 				AC_USRSP_EN | ev | VIA_JACK_EVENT);
27464a918ffeSTakashi Iwai 	}
27474a918ffeSTakashi Iwai 
27484a918ffeSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
27494a918ffeSTakashi Iwai 		if (is_jack_detectable(codec, cfg->inputs[i].pin))
27504a918ffeSTakashi Iwai 			snd_hda_codec_write(codec, cfg->inputs[i].pin, 0,
27514a918ffeSTakashi Iwai 				AC_VERB_SET_UNSOLICITED_ENABLE,
27524a918ffeSTakashi Iwai 				AC_USRSP_EN | VIA_JACK_EVENT);
27534a918ffeSTakashi Iwai 	}
27544a918ffeSTakashi Iwai }
27554a918ffeSTakashi Iwai 
27565d41762aSTakashi Iwai static int via_init(struct hda_codec *codec)
27575d41762aSTakashi Iwai {
27585d41762aSTakashi Iwai 	struct via_spec *spec = codec->spec;
27595d41762aSTakashi Iwai 	int i;
27605d41762aSTakashi Iwai 
27615d41762aSTakashi Iwai 	for (i = 0; i < spec->num_iverbs; i++)
27625d41762aSTakashi Iwai 		snd_hda_sequence_write(codec, spec->init_verbs[i]);
27635d41762aSTakashi Iwai 
2764c577b8a1SJoseph Chan 	via_auto_init_multi_out(codec);
2765c577b8a1SJoseph Chan 	via_auto_init_hp_out(codec);
27664a918ffeSTakashi Iwai 	via_auto_init_speaker_out(codec);
2767c577b8a1SJoseph Chan 	via_auto_init_analog_input(codec);
27685d41762aSTakashi Iwai 	via_auto_init_dig_outs(codec);
27695d41762aSTakashi Iwai 	via_auto_init_dig_in(codec);
277011890956SLydia Wang 
27714a918ffeSTakashi Iwai 	via_auto_init_unsol_event(codec);
27724a918ffeSTakashi Iwai 
277325eaba2fSLydia Wang 	via_hp_automute(codec);
277425eaba2fSLydia Wang 
2775c577b8a1SJoseph Chan 	return 0;
2776c577b8a1SJoseph Chan }
2777c577b8a1SJoseph Chan 
27781f2e99feSLydia Wang static void vt1708_update_hp_jack_state(struct work_struct *work)
27791f2e99feSLydia Wang {
27801f2e99feSLydia Wang 	struct via_spec *spec = container_of(work, struct via_spec,
27811f2e99feSLydia Wang 					     vt1708_hp_work.work);
27821f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
27831f2e99feSLydia Wang 		return;
27841f2e99feSLydia Wang 	/* if jack state toggled */
27851f2e99feSLydia Wang 	if (spec->vt1708_hp_present
2786d56757abSTakashi Iwai 	    != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
27871f2e99feSLydia Wang 		spec->vt1708_hp_present ^= 1;
27881f2e99feSLydia Wang 		via_hp_automute(spec->codec);
27891f2e99feSLydia Wang 	}
27901f2e99feSLydia Wang 	vt1708_start_hp_work(spec);
27911f2e99feSLydia Wang }
27921f2e99feSLydia Wang 
2793337b9d02STakashi Iwai static int get_mux_nids(struct hda_codec *codec)
2794337b9d02STakashi Iwai {
2795337b9d02STakashi Iwai 	struct via_spec *spec = codec->spec;
2796337b9d02STakashi Iwai 	hda_nid_t nid, conn[8];
2797337b9d02STakashi Iwai 	unsigned int type;
2798337b9d02STakashi Iwai 	int i, n;
2799337b9d02STakashi Iwai 
2800337b9d02STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
2801337b9d02STakashi Iwai 		nid = spec->adc_nids[i];
2802337b9d02STakashi Iwai 		while (nid) {
2803a22d543aSTakashi Iwai 			type = get_wcaps_type(get_wcaps(codec, nid));
28041c55d521STakashi Iwai 			if (type == AC_WID_PIN)
28051c55d521STakashi Iwai 				break;
2806337b9d02STakashi Iwai 			n = snd_hda_get_connections(codec, nid, conn,
2807337b9d02STakashi Iwai 						    ARRAY_SIZE(conn));
2808337b9d02STakashi Iwai 			if (n <= 0)
2809337b9d02STakashi Iwai 				break;
2810337b9d02STakashi Iwai 			if (n > 1) {
2811337b9d02STakashi Iwai 				spec->mux_nids[i] = nid;
2812337b9d02STakashi Iwai 				break;
2813337b9d02STakashi Iwai 			}
2814337b9d02STakashi Iwai 			nid = conn[0];
2815337b9d02STakashi Iwai 		}
2816337b9d02STakashi Iwai 	}
28171c55d521STakashi Iwai 	return 0;
2818337b9d02STakashi Iwai }
2819337b9d02STakashi Iwai 
2820c577b8a1SJoseph Chan static int patch_vt1708(struct hda_codec *codec)
2821c577b8a1SJoseph Chan {
2822c577b8a1SJoseph Chan 	struct via_spec *spec;
2823c577b8a1SJoseph Chan 	int err;
2824c577b8a1SJoseph Chan 
2825c577b8a1SJoseph Chan 	/* create a codec specific record */
28265b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2827c577b8a1SJoseph Chan 	if (spec == NULL)
2828c577b8a1SJoseph Chan 		return -ENOMEM;
2829c577b8a1SJoseph Chan 
2830620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x17;
2831620e2b28STakashi Iwai 
283212daef65STakashi Iwai 	/* Add HP and CD pin config connect bit re-config action */
283312daef65STakashi Iwai 	vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
283412daef65STakashi Iwai 	vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
283512daef65STakashi Iwai 
2836c577b8a1SJoseph Chan 	/* automatic parse from the BIOS config */
283712daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2838c577b8a1SJoseph Chan 	if (err < 0) {
2839c577b8a1SJoseph Chan 		via_free(codec);
2840c577b8a1SJoseph Chan 		return err;
2841c577b8a1SJoseph Chan 	}
2842c577b8a1SJoseph Chan 
284312daef65STakashi Iwai 	/* add jack detect on/off control */
284412daef65STakashi Iwai 	if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
284512daef65STakashi Iwai 		return -ENOMEM;
284612daef65STakashi Iwai 
2847bc9b5623STakashi Iwai 	/* disable 32bit format on VT1708 */
2848bc9b5623STakashi Iwai 	if (codec->vendor_id == 0x11061708)
2849bc9b5623STakashi Iwai 		spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
2850c577b8a1SJoseph Chan 
2851e322a36dSLydia Wang 	spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
2852e322a36dSLydia Wang 
2853c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
2854c577b8a1SJoseph Chan 
28551f2e99feSLydia Wang 	INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
2856c577b8a1SJoseph Chan 	return 0;
2857c577b8a1SJoseph Chan }
2858c577b8a1SJoseph Chan 
2859ddd304d8STakashi Iwai static int patch_vt1709(struct hda_codec *codec)
2860c577b8a1SJoseph Chan {
2861c577b8a1SJoseph Chan 	struct via_spec *spec;
2862c577b8a1SJoseph Chan 	int err;
2863c577b8a1SJoseph Chan 
2864c577b8a1SJoseph Chan 	/* create a codec specific record */
28655b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2866c577b8a1SJoseph Chan 	if (spec == NULL)
2867c577b8a1SJoseph Chan 		return -ENOMEM;
2868c577b8a1SJoseph Chan 
2869620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x18;
2870620e2b28STakashi Iwai 
287112daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2872c577b8a1SJoseph Chan 	if (err < 0) {
2873c577b8a1SJoseph Chan 		via_free(codec);
2874c577b8a1SJoseph Chan 		return err;
2875c577b8a1SJoseph Chan 	}
2876c577b8a1SJoseph Chan 
2877c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
2878c577b8a1SJoseph Chan 
2879f7278fd0SJosepch Chan 	return 0;
2880f7278fd0SJosepch Chan }
2881f7278fd0SJosepch Chan 
28823e95b9abSLydia Wang static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
28833e95b9abSLydia Wang {
28843e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
28853e95b9abSLydia Wang 	int imux_is_smixer;
28863e95b9abSLydia Wang 	unsigned int parm;
28873e95b9abSLydia Wang 	int is_8ch = 0;
2888bc92df7fSLydia Wang 	if ((spec->codec_type != VT1708B_4CH) &&
2889bc92df7fSLydia Wang 	    (codec->vendor_id != 0x11064397))
28903e95b9abSLydia Wang 		is_8ch = 1;
28913e95b9abSLydia Wang 
28923e95b9abSLydia Wang 	/* SW0 (17h) = stereo mixer */
28933e95b9abSLydia Wang 	imux_is_smixer =
28943e95b9abSLydia Wang 	(snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
28953e95b9abSLydia Wang 	 == ((spec->codec_type == VT1708S) ? 5 : 0));
28963e95b9abSLydia Wang 	/* inputs */
28973e95b9abSLydia Wang 	/* PW 1/2/5 (1ah/1bh/1eh) */
28983e95b9abSLydia Wang 	parm = AC_PWRST_D3;
28993e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1a, &parm);
29003e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1b, &parm);
29013e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
29023e95b9abSLydia Wang 	if (imux_is_smixer)
29033e95b9abSLydia Wang 		parm = AC_PWRST_D0;
29043e95b9abSLydia Wang 	/* SW0 (17h), AIW 0/1 (13h/14h) */
29053e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
29063e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
29073e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
29083e95b9abSLydia Wang 
29093e95b9abSLydia Wang 	/* outputs */
29103e95b9abSLydia Wang 	/* PW0 (19h), SW1 (18h), AOW1 (11h) */
29113e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29123e95b9abSLydia Wang 	set_pin_power_state(codec, 0x19, &parm);
29133e95b9abSLydia Wang 	if (spec->smart51_enabled)
29143e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1b, &parm);
29153e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
29163e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
29173e95b9abSLydia Wang 
29183e95b9abSLydia Wang 	/* PW6 (22h), SW2 (26h), AOW2 (24h) */
29193e95b9abSLydia Wang 	if (is_8ch) {
29203e95b9abSLydia Wang 		parm = AC_PWRST_D3;
29213e95b9abSLydia Wang 		set_pin_power_state(codec, 0x22, &parm);
29223e95b9abSLydia Wang 		if (spec->smart51_enabled)
29233e95b9abSLydia Wang 			set_pin_power_state(codec, 0x1a, &parm);
29243e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x26, 0,
29253e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
29263e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x24, 0,
29273e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
2928bc92df7fSLydia Wang 	} else if (codec->vendor_id == 0x11064397) {
2929bc92df7fSLydia Wang 		/* PW7(23h), SW2(27h), AOW2(25h) */
2930bc92df7fSLydia Wang 		parm = AC_PWRST_D3;
2931bc92df7fSLydia Wang 		set_pin_power_state(codec, 0x23, &parm);
2932bc92df7fSLydia Wang 		if (spec->smart51_enabled)
2933bc92df7fSLydia Wang 			set_pin_power_state(codec, 0x1a, &parm);
2934bc92df7fSLydia Wang 		snd_hda_codec_write(codec, 0x27, 0,
2935bc92df7fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
2936bc92df7fSLydia Wang 		snd_hda_codec_write(codec, 0x25, 0,
2937bc92df7fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
29383e95b9abSLydia Wang 	}
29393e95b9abSLydia Wang 
29403e95b9abSLydia Wang 	/* PW 3/4/7 (1ch/1dh/23h) */
29413e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29423e95b9abSLydia Wang 	/* force to D0 for internal Speaker */
29433e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1c, &parm);
29443e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1d, &parm);
29453e95b9abSLydia Wang 	if (is_8ch)
29463e95b9abSLydia Wang 		set_pin_power_state(codec, 0x23, &parm);
29473e95b9abSLydia Wang 
29483e95b9abSLydia Wang 	/* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
29493e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
29503e95b9abSLydia Wang 			    imux_is_smixer ? AC_PWRST_D0 : parm);
29513e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
29523e95b9abSLydia Wang 	if (is_8ch) {
29533e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x25, 0,
29543e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
29553e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x27, 0,
29563e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
2957bc92df7fSLydia Wang 	} else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
2958bc92df7fSLydia Wang 		snd_hda_codec_write(codec, 0x25, 0,
2959bc92df7fSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
29603e95b9abSLydia Wang }
29613e95b9abSLydia Wang 
2962518bf3baSLydia Wang static int patch_vt1708S(struct hda_codec *codec);
2963ddd304d8STakashi Iwai static int patch_vt1708B(struct hda_codec *codec)
2964f7278fd0SJosepch Chan {
2965f7278fd0SJosepch Chan 	struct via_spec *spec;
2966f7278fd0SJosepch Chan 	int err;
2967f7278fd0SJosepch Chan 
2968518bf3baSLydia Wang 	if (get_codec_type(codec) == VT1708BCE)
2969518bf3baSLydia Wang 		return patch_vt1708S(codec);
2970ddd304d8STakashi Iwai 
2971f7278fd0SJosepch Chan 	/* create a codec specific record */
29725b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2973f7278fd0SJosepch Chan 	if (spec == NULL)
2974f7278fd0SJosepch Chan 		return -ENOMEM;
2975f7278fd0SJosepch Chan 
2976620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
2977620e2b28STakashi Iwai 
2978f7278fd0SJosepch Chan 	/* automatic parse from the BIOS config */
297912daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2980f7278fd0SJosepch Chan 	if (err < 0) {
2981f7278fd0SJosepch Chan 		via_free(codec);
2982f7278fd0SJosepch Chan 		return err;
2983f7278fd0SJosepch Chan 	}
2984f7278fd0SJosepch Chan 
2985f7278fd0SJosepch Chan 	codec->patch_ops = via_patch_ops;
2986f7278fd0SJosepch Chan 
29873e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
29883e95b9abSLydia Wang 
2989f7278fd0SJosepch Chan 	return 0;
2990f7278fd0SJosepch Chan }
2991f7278fd0SJosepch Chan 
2992d949cac1SHarald Welte /* Patch for VT1708S */
2993096a8854STakashi Iwai static const struct hda_verb vt1708S_init_verbs[] = {
2994d7426329SHarald Welte 	/* Enable Mic Boost Volume backdoor */
2995d7426329SHarald Welte 	{0x1, 0xf98, 0x1},
2996bc7e7e5cSLydia Wang 	/* don't bybass mixer */
2997bc7e7e5cSLydia Wang 	{0x1, 0xf88, 0xc0},
2998d949cac1SHarald Welte 	{ }
2999d949cac1SHarald Welte };
3000d949cac1SHarald Welte 
30019da29271STakashi Iwai /* fill out digital output widgets; one for master and one for slave outputs */
30029da29271STakashi Iwai static void fill_dig_outs(struct hda_codec *codec)
30039da29271STakashi Iwai {
30049da29271STakashi Iwai 	struct via_spec *spec = codec->spec;
30059da29271STakashi Iwai 	int i;
30069da29271STakashi Iwai 
30079da29271STakashi Iwai 	for (i = 0; i < spec->autocfg.dig_outs; i++) {
30089da29271STakashi Iwai 		hda_nid_t nid;
30099da29271STakashi Iwai 		int conn;
30109da29271STakashi Iwai 
30119da29271STakashi Iwai 		nid = spec->autocfg.dig_out_pins[i];
30129da29271STakashi Iwai 		if (!nid)
30139da29271STakashi Iwai 			continue;
30149da29271STakashi Iwai 		conn = snd_hda_get_connections(codec, nid, &nid, 1);
30159da29271STakashi Iwai 		if (conn < 1)
30169da29271STakashi Iwai 			continue;
30179da29271STakashi Iwai 		if (!spec->multiout.dig_out_nid)
30189da29271STakashi Iwai 			spec->multiout.dig_out_nid = nid;
30199da29271STakashi Iwai 		else {
30209da29271STakashi Iwai 			spec->slave_dig_outs[0] = nid;
30219da29271STakashi Iwai 			break; /* at most two dig outs */
30229da29271STakashi Iwai 		}
30239da29271STakashi Iwai 	}
30249da29271STakashi Iwai }
30259da29271STakashi Iwai 
302612daef65STakashi Iwai static void fill_dig_in(struct hda_codec *codec)
3027d949cac1SHarald Welte {
3028d949cac1SHarald Welte 	struct via_spec *spec = codec->spec;
302912daef65STakashi Iwai 	hda_nid_t dig_nid;
303012daef65STakashi Iwai 	int i, err;
3031d949cac1SHarald Welte 
303212daef65STakashi Iwai 	if (!spec->autocfg.dig_in_pin)
303312daef65STakashi Iwai 		return;
3034d949cac1SHarald Welte 
303512daef65STakashi Iwai 	dig_nid = codec->start_nid;
303612daef65STakashi Iwai 	for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
303712daef65STakashi Iwai 		unsigned int wcaps = get_wcaps(codec, dig_nid);
303812daef65STakashi Iwai 		if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
303912daef65STakashi Iwai 			continue;
304012daef65STakashi Iwai 		if (!(wcaps & AC_WCAP_DIGITAL))
304112daef65STakashi Iwai 			continue;
304212daef65STakashi Iwai 		if (!(wcaps & AC_WCAP_CONN_LIST))
304312daef65STakashi Iwai 			continue;
304412daef65STakashi Iwai 		err = get_connection_index(codec, dig_nid,
304512daef65STakashi Iwai 					   spec->autocfg.dig_in_pin);
304612daef65STakashi Iwai 		if (err >= 0) {
304712daef65STakashi Iwai 			spec->dig_in_nid = dig_nid;
304812daef65STakashi Iwai 			break;
304912daef65STakashi Iwai 		}
305012daef65STakashi Iwai 	}
3051d949cac1SHarald Welte }
3052d949cac1SHarald Welte 
30536369bcfcSLydia Wang static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
30546369bcfcSLydia Wang 			       int offset, int num_steps, int step_size)
30556369bcfcSLydia Wang {
30566369bcfcSLydia Wang 	snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
30576369bcfcSLydia Wang 				  (offset << AC_AMPCAP_OFFSET_SHIFT) |
30586369bcfcSLydia Wang 				  (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
30596369bcfcSLydia Wang 				  (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
30606369bcfcSLydia Wang 				  (0 << AC_AMPCAP_MUTE_SHIFT));
30616369bcfcSLydia Wang }
30626369bcfcSLydia Wang 
3063d949cac1SHarald Welte static int patch_vt1708S(struct hda_codec *codec)
3064d949cac1SHarald Welte {
3065d949cac1SHarald Welte 	struct via_spec *spec;
3066d949cac1SHarald Welte 	int err;
3067d949cac1SHarald Welte 
3068d949cac1SHarald Welte 	/* create a codec specific record */
30695b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3070d949cac1SHarald Welte 	if (spec == NULL)
3071d949cac1SHarald Welte 		return -ENOMEM;
3072d949cac1SHarald Welte 
3073620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
3074d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1a, 0, 3, 40);
3075d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1e, 0, 3, 40);
3076620e2b28STakashi Iwai 
3077d949cac1SHarald Welte 	/* automatic parse from the BIOS config */
307812daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3079d949cac1SHarald Welte 	if (err < 0) {
3080d949cac1SHarald Welte 		via_free(codec);
3081d949cac1SHarald Welte 		return err;
3082d949cac1SHarald Welte 	}
3083d949cac1SHarald Welte 
3084096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
3085d949cac1SHarald Welte 
3086d949cac1SHarald Welte 	codec->patch_ops = via_patch_ops;
3087d949cac1SHarald Welte 
3088518bf3baSLydia Wang 	/* correct names for VT1708BCE */
3089518bf3baSLydia Wang 	if (get_codec_type(codec) == VT1708BCE)	{
3090518bf3baSLydia Wang 		kfree(codec->chip_name);
3091518bf3baSLydia Wang 		codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
3092518bf3baSLydia Wang 		snprintf(codec->bus->card->mixername,
3093518bf3baSLydia Wang 			 sizeof(codec->bus->card->mixername),
3094518bf3baSLydia Wang 			 "%s %s", codec->vendor_name, codec->chip_name);
3095970f630fSLydia Wang 	}
3096bc92df7fSLydia Wang 	/* correct names for VT1705 */
3097bc92df7fSLydia Wang 	if (codec->vendor_id == 0x11064397)	{
3098bc92df7fSLydia Wang 		kfree(codec->chip_name);
3099bc92df7fSLydia Wang 		codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
3100bc92df7fSLydia Wang 		snprintf(codec->bus->card->mixername,
3101bc92df7fSLydia Wang 			 sizeof(codec->bus->card->mixername),
3102bc92df7fSLydia Wang 			 "%s %s", codec->vendor_name, codec->chip_name);
3103bc92df7fSLydia Wang 	}
31043e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
3105d949cac1SHarald Welte 	return 0;
3106d949cac1SHarald Welte }
3107d949cac1SHarald Welte 
3108d949cac1SHarald Welte /* Patch for VT1702 */
3109d949cac1SHarald Welte 
3110096a8854STakashi Iwai static const struct hda_verb vt1702_init_verbs[] = {
3111bc7e7e5cSLydia Wang 	/* mixer enable */
3112bc7e7e5cSLydia Wang 	{0x1, 0xF88, 0x3},
3113bc7e7e5cSLydia Wang 	/* GPIO 0~2 */
3114bc7e7e5cSLydia Wang 	{0x1, 0xF82, 0x3F},
3115d949cac1SHarald Welte 	{ }
3116d949cac1SHarald Welte };
3117d949cac1SHarald Welte 
31183e95b9abSLydia Wang static void set_widgets_power_state_vt1702(struct hda_codec *codec)
31193e95b9abSLydia Wang {
31203e95b9abSLydia Wang 	int imux_is_smixer =
31213e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
31223e95b9abSLydia Wang 	unsigned int parm;
31233e95b9abSLydia Wang 	/* inputs */
31243e95b9abSLydia Wang 	/* PW 1/2/5 (14h/15h/18h) */
31253e95b9abSLydia Wang 	parm = AC_PWRST_D3;
31263e95b9abSLydia Wang 	set_pin_power_state(codec, 0x14, &parm);
31273e95b9abSLydia Wang 	set_pin_power_state(codec, 0x15, &parm);
31283e95b9abSLydia Wang 	set_pin_power_state(codec, 0x18, &parm);
31293e95b9abSLydia Wang 	if (imux_is_smixer)
31303e95b9abSLydia Wang 		parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
31313e95b9abSLydia Wang 	/* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
31323e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
31333e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x12, 0, AC_VERB_SET_POWER_STATE, parm);
31343e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
31353e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_POWER_STATE, parm);
31363e95b9abSLydia Wang 
31373e95b9abSLydia Wang 	/* outputs */
31383e95b9abSLydia Wang 	/* PW 3/4 (16h/17h) */
31393e95b9abSLydia Wang 	parm = AC_PWRST_D3;
31403e95b9abSLydia Wang 	set_pin_power_state(codec, 0x17, &parm);
31413e95b9abSLydia Wang 	set_pin_power_state(codec, 0x16, &parm);
31423e95b9abSLydia Wang 	/* MW0 (1ah), AOW 0/1 (10h/1dh) */
31433e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
31443e95b9abSLydia Wang 			    imux_is_smixer ? AC_PWRST_D0 : parm);
31453e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
31463e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
31473e95b9abSLydia Wang }
31483e95b9abSLydia Wang 
3149d949cac1SHarald Welte static int patch_vt1702(struct hda_codec *codec)
3150d949cac1SHarald Welte {
3151d949cac1SHarald Welte 	struct via_spec *spec;
3152d949cac1SHarald Welte 	int err;
3153d949cac1SHarald Welte 
3154d949cac1SHarald Welte 	/* create a codec specific record */
31555b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3156d949cac1SHarald Welte 	if (spec == NULL)
3157d949cac1SHarald Welte 		return -ENOMEM;
3158d949cac1SHarald Welte 
3159620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x1a;
3160620e2b28STakashi Iwai 
316112daef65STakashi Iwai 	/* limit AA path volume to 0 dB */
316212daef65STakashi Iwai 	snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
316312daef65STakashi Iwai 				  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
316412daef65STakashi Iwai 				  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
316512daef65STakashi Iwai 				  (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
316612daef65STakashi Iwai 				  (1 << AC_AMPCAP_MUTE_SHIFT));
316712daef65STakashi Iwai 
3168d949cac1SHarald Welte 	/* automatic parse from the BIOS config */
316912daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3170d949cac1SHarald Welte 	if (err < 0) {
3171d949cac1SHarald Welte 		via_free(codec);
3172d949cac1SHarald Welte 		return err;
3173d949cac1SHarald Welte 	}
3174d949cac1SHarald Welte 
3175096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
3176d949cac1SHarald Welte 
3177d949cac1SHarald Welte 	codec->patch_ops = via_patch_ops;
3178d949cac1SHarald Welte 
31793e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1702;
3180d949cac1SHarald Welte 	return 0;
3181d949cac1SHarald Welte }
3182d949cac1SHarald Welte 
3183eb7188caSLydia Wang /* Patch for VT1718S */
3184eb7188caSLydia Wang 
3185096a8854STakashi Iwai static const struct hda_verb vt1718S_init_verbs[] = {
31864ab2d53aSLydia Wang 	/* Enable MW0 adjust Gain 5 */
31874ab2d53aSLydia Wang 	{0x1, 0xfb2, 0x10},
3188eb7188caSLydia Wang 	/* Enable Boost Volume backdoor */
3189eb7188caSLydia Wang 	{0x1, 0xf88, 0x8},
31905d41762aSTakashi Iwai 
3191eb7188caSLydia Wang 	{ }
3192eb7188caSLydia Wang };
3193eb7188caSLydia Wang 
31943e95b9abSLydia Wang static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
31953e95b9abSLydia Wang {
31963e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
31973e95b9abSLydia Wang 	int imux_is_smixer;
31983e95b9abSLydia Wang 	unsigned int parm;
31993e95b9abSLydia Wang 	/* MUX6 (1eh) = stereo mixer */
32003e95b9abSLydia Wang 	imux_is_smixer =
32013e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
32023e95b9abSLydia Wang 	/* inputs */
32033e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
32043e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32053e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
32063e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
32073e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
32083e95b9abSLydia Wang 	if (imux_is_smixer)
32093e95b9abSLydia Wang 		parm = AC_PWRST_D0;
32103e95b9abSLydia Wang 	/* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
32113e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
32123e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
32133e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
32143e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
32153e95b9abSLydia Wang 
32163e95b9abSLydia Wang 	/* outputs */
32173e95b9abSLydia Wang 	/* PW3 (27h), MW2 (1ah), AOW3 (bh) */
32183e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32193e95b9abSLydia Wang 	set_pin_power_state(codec, 0x27, &parm);
32203e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE, parm);
32213e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0xb, 0, AC_VERB_SET_POWER_STATE, parm);
32223e95b9abSLydia Wang 
32233e95b9abSLydia Wang 	/* PW2 (26h), AOW2 (ah) */
32243e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32253e95b9abSLydia Wang 	set_pin_power_state(codec, 0x26, &parm);
32263e95b9abSLydia Wang 	if (spec->smart51_enabled)
32273e95b9abSLydia Wang 		set_pin_power_state(codec, 0x2b, &parm);
32283e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0xa, 0, AC_VERB_SET_POWER_STATE, parm);
32293e95b9abSLydia Wang 
32303e95b9abSLydia Wang 	/* PW0 (24h), AOW0 (8h) */
32313e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32323e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
32333e95b9abSLydia Wang 	if (!spec->hp_independent_mode) /* check for redirected HP */
32343e95b9abSLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
32353e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
32363e95b9abSLydia Wang 	/* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
32373e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x21, 0, AC_VERB_SET_POWER_STATE,
32383e95b9abSLydia Wang 			    imux_is_smixer ? AC_PWRST_D0 : parm);
32393e95b9abSLydia Wang 
32403e95b9abSLydia Wang 	/* PW1 (25h), AOW1 (9h) */
32413e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32423e95b9abSLydia Wang 	set_pin_power_state(codec, 0x25, &parm);
32433e95b9abSLydia Wang 	if (spec->smart51_enabled)
32443e95b9abSLydia Wang 		set_pin_power_state(codec, 0x2a, &parm);
32453e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x9, 0, AC_VERB_SET_POWER_STATE, parm);
32463e95b9abSLydia Wang 
32473e95b9abSLydia Wang 	if (spec->hp_independent_mode) {
32483e95b9abSLydia Wang 		/* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
32493e95b9abSLydia Wang 		parm = AC_PWRST_D3;
32503e95b9abSLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
32513e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x1b, 0,
32523e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
32533e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x34, 0,
32543e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
32553e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0xc, 0,
32563e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
32573e95b9abSLydia Wang 	}
32583e95b9abSLydia Wang }
32593e95b9abSLydia Wang 
326030b45033STakashi Iwai /* Add a connection to the primary DAC from AA-mixer for some codecs
326130b45033STakashi Iwai  * This isn't listed from the raw info, but the chip has a secret connection.
326230b45033STakashi Iwai  */
326330b45033STakashi Iwai static int add_secret_dac_path(struct hda_codec *codec)
326430b45033STakashi Iwai {
326530b45033STakashi Iwai 	struct via_spec *spec = codec->spec;
326630b45033STakashi Iwai 	int i, nums;
326730b45033STakashi Iwai 	hda_nid_t conn[8];
326830b45033STakashi Iwai 	hda_nid_t nid;
326930b45033STakashi Iwai 
327030b45033STakashi Iwai 	if (!spec->aa_mix_nid)
327130b45033STakashi Iwai 		return 0;
327230b45033STakashi Iwai 	nums = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
327330b45033STakashi Iwai 				       ARRAY_SIZE(conn) - 1);
327430b45033STakashi Iwai 	for (i = 0; i < nums; i++) {
327530b45033STakashi Iwai 		if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT)
327630b45033STakashi Iwai 			return 0;
327730b45033STakashi Iwai 	}
327830b45033STakashi Iwai 
327930b45033STakashi Iwai 	/* find the primary DAC and add to the connection list */
328030b45033STakashi Iwai 	nid = codec->start_nid;
328130b45033STakashi Iwai 	for (i = 0; i < codec->num_nodes; i++, nid++) {
328230b45033STakashi Iwai 		unsigned int caps = get_wcaps(codec, nid);
328330b45033STakashi Iwai 		if (get_wcaps_type(caps) == AC_WID_AUD_OUT &&
328430b45033STakashi Iwai 		    !(caps & AC_WCAP_DIGITAL)) {
328530b45033STakashi Iwai 			conn[nums++] = nid;
328630b45033STakashi Iwai 			return snd_hda_override_conn_list(codec,
328730b45033STakashi Iwai 							  spec->aa_mix_nid,
328830b45033STakashi Iwai 							  nums, conn);
328930b45033STakashi Iwai 		}
329030b45033STakashi Iwai 	}
329130b45033STakashi Iwai 	return 0;
329230b45033STakashi Iwai }
329330b45033STakashi Iwai 
329430b45033STakashi Iwai 
3295eb7188caSLydia Wang static int patch_vt1718S(struct hda_codec *codec)
3296eb7188caSLydia Wang {
3297eb7188caSLydia Wang 	struct via_spec *spec;
3298eb7188caSLydia Wang 	int err;
3299eb7188caSLydia Wang 
3300eb7188caSLydia Wang 	/* create a codec specific record */
33015b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3302eb7188caSLydia Wang 	if (spec == NULL)
3303eb7188caSLydia Wang 		return -ENOMEM;
3304eb7188caSLydia Wang 
3305620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
3306d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3307d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
330830b45033STakashi Iwai 	add_secret_dac_path(codec);
3309620e2b28STakashi Iwai 
3310eb7188caSLydia Wang 	/* automatic parse from the BIOS config */
331112daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3312eb7188caSLydia Wang 	if (err < 0) {
3313eb7188caSLydia Wang 		via_free(codec);
3314eb7188caSLydia Wang 		return err;
3315eb7188caSLydia Wang 	}
3316eb7188caSLydia Wang 
3317096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
3318eb7188caSLydia Wang 
3319eb7188caSLydia Wang 	codec->patch_ops = via_patch_ops;
3320eb7188caSLydia Wang 
33213e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1718S;
33223e95b9abSLydia Wang 
3323eb7188caSLydia Wang 	return 0;
3324eb7188caSLydia Wang }
3325f3db423dSLydia Wang 
3326f3db423dSLydia Wang /* Patch for VT1716S */
3327f3db423dSLydia Wang 
3328f3db423dSLydia Wang static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
3329f3db423dSLydia Wang 			    struct snd_ctl_elem_info *uinfo)
3330f3db423dSLydia Wang {
3331f3db423dSLydia Wang 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3332f3db423dSLydia Wang 	uinfo->count = 1;
3333f3db423dSLydia Wang 	uinfo->value.integer.min = 0;
3334f3db423dSLydia Wang 	uinfo->value.integer.max = 1;
3335f3db423dSLydia Wang 	return 0;
3336f3db423dSLydia Wang }
3337f3db423dSLydia Wang 
3338f3db423dSLydia Wang static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
3339f3db423dSLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
3340f3db423dSLydia Wang {
3341f3db423dSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3342f3db423dSLydia Wang 	int index = 0;
3343f3db423dSLydia Wang 
3344f3db423dSLydia Wang 	index = snd_hda_codec_read(codec, 0x26, 0,
3345f3db423dSLydia Wang 					       AC_VERB_GET_CONNECT_SEL, 0);
3346f3db423dSLydia Wang 	if (index != -1)
3347f3db423dSLydia Wang 		*ucontrol->value.integer.value = index;
3348f3db423dSLydia Wang 
3349f3db423dSLydia Wang 	return 0;
3350f3db423dSLydia Wang }
3351f3db423dSLydia Wang 
3352f3db423dSLydia Wang static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
3353f3db423dSLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
3354f3db423dSLydia Wang {
3355f3db423dSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3356f3db423dSLydia Wang 	struct via_spec *spec = codec->spec;
3357f3db423dSLydia Wang 	int index = *ucontrol->value.integer.value;
3358f3db423dSLydia Wang 
3359f3db423dSLydia Wang 	snd_hda_codec_write(codec, 0x26, 0,
3360f3db423dSLydia Wang 					       AC_VERB_SET_CONNECT_SEL, index);
3361f3db423dSLydia Wang 	spec->dmic_enabled = index;
33623e95b9abSLydia Wang 	set_widgets_power_state(codec);
3363f3db423dSLydia Wang 	return 1;
3364f3db423dSLydia Wang }
3365f3db423dSLydia Wang 
336690dd48a1STakashi Iwai static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
3367f3db423dSLydia Wang 	HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
3368f3db423dSLydia Wang 	{
3369f3db423dSLydia Wang 	 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3370f3db423dSLydia Wang 	 .name = "Digital Mic Capture Switch",
33715b0cb1d8SJaroslav Kysela 	 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
3372f3db423dSLydia Wang 	 .count = 1,
3373f3db423dSLydia Wang 	 .info = vt1716s_dmic_info,
3374f3db423dSLydia Wang 	 .get = vt1716s_dmic_get,
3375f3db423dSLydia Wang 	 .put = vt1716s_dmic_put,
3376f3db423dSLydia Wang 	 },
3377f3db423dSLydia Wang 	{}			/* end */
3378f3db423dSLydia Wang };
3379f3db423dSLydia Wang 
3380f3db423dSLydia Wang 
3381f3db423dSLydia Wang /* mono-out mixer elements */
338290dd48a1STakashi Iwai static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
3383f3db423dSLydia Wang 	HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
3384f3db423dSLydia Wang 	{ } /* end */
3385f3db423dSLydia Wang };
3386f3db423dSLydia Wang 
3387096a8854STakashi Iwai static const struct hda_verb vt1716S_init_verbs[] = {
3388f3db423dSLydia Wang 	/* Enable Boost Volume backdoor */
3389f3db423dSLydia Wang 	{0x1, 0xf8a, 0x80},
3390f3db423dSLydia Wang 	/* don't bybass mixer */
3391f3db423dSLydia Wang 	{0x1, 0xf88, 0xc0},
3392f3db423dSLydia Wang 	/* Enable mono output */
3393f3db423dSLydia Wang 	{0x1, 0xf90, 0x08},
3394f3db423dSLydia Wang 	{ }
3395f3db423dSLydia Wang };
3396f3db423dSLydia Wang 
33973e95b9abSLydia Wang static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
33983e95b9abSLydia Wang {
33993e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
34003e95b9abSLydia Wang 	int imux_is_smixer;
34013e95b9abSLydia Wang 	unsigned int parm;
34023e95b9abSLydia Wang 	unsigned int mono_out, present;
34033e95b9abSLydia Wang 	/* SW0 (17h) = stereo mixer */
34043e95b9abSLydia Wang 	imux_is_smixer =
34053e95b9abSLydia Wang 	(snd_hda_codec_read(codec, 0x17, 0,
34063e95b9abSLydia Wang 			    AC_VERB_GET_CONNECT_SEL, 0x00) ==  5);
34073e95b9abSLydia Wang 	/* inputs */
34083e95b9abSLydia Wang 	/* PW 1/2/5 (1ah/1bh/1eh) */
34093e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34103e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1a, &parm);
34113e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1b, &parm);
34123e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
34133e95b9abSLydia Wang 	if (imux_is_smixer)
34143e95b9abSLydia Wang 		parm = AC_PWRST_D0;
34153e95b9abSLydia Wang 	/* SW0 (17h), AIW0(13h) */
34163e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
34173e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
34183e95b9abSLydia Wang 
34193e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34203e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
34213e95b9abSLydia Wang 	/* PW11 (22h) */
34223e95b9abSLydia Wang 	if (spec->dmic_enabled)
34233e95b9abSLydia Wang 		set_pin_power_state(codec, 0x22, &parm);
34243e95b9abSLydia Wang 	else
34253e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x22, 0,
34263e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
34273e95b9abSLydia Wang 
34283e95b9abSLydia Wang 	/* SW2(26h), AIW1(14h) */
34293e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x26, 0, AC_VERB_SET_POWER_STATE, parm);
34303e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
34313e95b9abSLydia Wang 
34323e95b9abSLydia Wang 	/* outputs */
34333e95b9abSLydia Wang 	/* PW0 (19h), SW1 (18h), AOW1 (11h) */
34343e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34353e95b9abSLydia Wang 	set_pin_power_state(codec, 0x19, &parm);
34363e95b9abSLydia Wang 	/* Smart 5.1 PW2(1bh) */
34373e95b9abSLydia Wang 	if (spec->smart51_enabled)
34383e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1b, &parm);
34393e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
34403e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
34413e95b9abSLydia Wang 
34423e95b9abSLydia Wang 	/* PW7 (23h), SW3 (27h), AOW3 (25h) */
34433e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34443e95b9abSLydia Wang 	set_pin_power_state(codec, 0x23, &parm);
34453e95b9abSLydia Wang 	/* Smart 5.1 PW1(1ah) */
34463e95b9abSLydia Wang 	if (spec->smart51_enabled)
34473e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1a, &parm);
34483e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x27, 0, AC_VERB_SET_POWER_STATE, parm);
34493e95b9abSLydia Wang 
34503e95b9abSLydia Wang 	/* Smart 5.1 PW5(1eh) */
34513e95b9abSLydia Wang 	if (spec->smart51_enabled)
34523e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1e, &parm);
34533e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x25, 0, AC_VERB_SET_POWER_STATE, parm);
34543e95b9abSLydia Wang 
34553e95b9abSLydia Wang 	/* Mono out */
34563e95b9abSLydia Wang 	/* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
34573e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x1c);
34583e95b9abSLydia Wang 
34593e95b9abSLydia Wang 	if (present)
34603e95b9abSLydia Wang 		mono_out = 0;
34613e95b9abSLydia Wang 	else {
34623e95b9abSLydia Wang 		present = snd_hda_jack_detect(codec, 0x1d);
34633e95b9abSLydia Wang 		if (!spec->hp_independent_mode && present)
34643e95b9abSLydia Wang 			mono_out = 0;
34653e95b9abSLydia Wang 		else
34663e95b9abSLydia Wang 			mono_out = 1;
34673e95b9abSLydia Wang 	}
34683e95b9abSLydia Wang 	parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
34693e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x28, 0, AC_VERB_SET_POWER_STATE, parm);
34703e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x29, 0, AC_VERB_SET_POWER_STATE, parm);
34713e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x2a, 0, AC_VERB_SET_POWER_STATE, parm);
34723e95b9abSLydia Wang 
34733e95b9abSLydia Wang 	/* PW 3/4 (1ch/1dh) */
34743e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34753e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1c, &parm);
34763e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1d, &parm);
34773e95b9abSLydia Wang 	/* HP Independent Mode, power on AOW3 */
34783e95b9abSLydia Wang 	if (spec->hp_independent_mode)
34793e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x25, 0,
34803e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
34813e95b9abSLydia Wang 
34823e95b9abSLydia Wang 	/* force to D0 for internal Speaker */
34833e95b9abSLydia Wang 	/* MW0 (16h), AOW0 (10h) */
34843e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
34853e95b9abSLydia Wang 			    imux_is_smixer ? AC_PWRST_D0 : parm);
34863e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
34873e95b9abSLydia Wang 			    mono_out ? AC_PWRST_D0 : parm);
34883e95b9abSLydia Wang }
34893e95b9abSLydia Wang 
3490f3db423dSLydia Wang static int patch_vt1716S(struct hda_codec *codec)
3491f3db423dSLydia Wang {
3492f3db423dSLydia Wang 	struct via_spec *spec;
3493f3db423dSLydia Wang 	int err;
3494f3db423dSLydia Wang 
3495f3db423dSLydia Wang 	/* create a codec specific record */
34965b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3497f3db423dSLydia Wang 	if (spec == NULL)
3498f3db423dSLydia Wang 		return -ENOMEM;
3499f3db423dSLydia Wang 
3500620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
3501d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1a, 0, 3, 40);
3502d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1e, 0, 3, 40);
3503620e2b28STakashi Iwai 
3504f3db423dSLydia Wang 	/* automatic parse from the BIOS config */
350512daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3506f3db423dSLydia Wang 	if (err < 0) {
3507f3db423dSLydia Wang 		via_free(codec);
3508f3db423dSLydia Wang 		return err;
3509f3db423dSLydia Wang 	}
3510f3db423dSLydia Wang 
3511096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++]  = vt1716S_init_verbs;
3512f3db423dSLydia Wang 
3513f3db423dSLydia Wang 	spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
3514f3db423dSLydia Wang 	spec->num_mixers++;
3515f3db423dSLydia Wang 
3516f3db423dSLydia Wang 	spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
3517f3db423dSLydia Wang 
3518f3db423dSLydia Wang 	codec->patch_ops = via_patch_ops;
3519f3db423dSLydia Wang 
35203e95b9abSLydia Wang 	spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
3521f3db423dSLydia Wang 	return 0;
3522f3db423dSLydia Wang }
352325eaba2fSLydia Wang 
352425eaba2fSLydia Wang /* for vt2002P */
352525eaba2fSLydia Wang 
3526096a8854STakashi Iwai static const struct hda_verb vt2002P_init_verbs[] = {
3527eadb9a80SLydia Wang 	/* Class-D speaker related verbs */
3528eadb9a80SLydia Wang 	{0x1, 0xfe0, 0x4},
3529eadb9a80SLydia Wang 	{0x1, 0xfe9, 0x80},
3530eadb9a80SLydia Wang 	{0x1, 0xfe2, 0x22},
353125eaba2fSLydia Wang 	/* Enable Boost Volume backdoor */
353225eaba2fSLydia Wang 	{0x1, 0xfb9, 0x24},
353325eaba2fSLydia Wang 	/* Enable AOW0 to MW9 */
353425eaba2fSLydia Wang 	{0x1, 0xfb8, 0x88},
353525eaba2fSLydia Wang 	{ }
353625eaba2fSLydia Wang };
35374a918ffeSTakashi Iwai 
3538096a8854STakashi Iwai static const struct hda_verb vt1802_init_verbs[] = {
353911890956SLydia Wang 	/* Enable Boost Volume backdoor */
354011890956SLydia Wang 	{0x1, 0xfb9, 0x24},
354111890956SLydia Wang 	/* Enable AOW0 to MW9 */
354211890956SLydia Wang 	{0x1, 0xfb8, 0x88},
354311890956SLydia Wang 	{ }
354411890956SLydia Wang };
354525eaba2fSLydia Wang 
35463e95b9abSLydia Wang static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
35473e95b9abSLydia Wang {
35483e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
35493e95b9abSLydia Wang 	int imux_is_smixer;
35503e95b9abSLydia Wang 	unsigned int parm;
35513e95b9abSLydia Wang 	unsigned int present;
35523e95b9abSLydia Wang 	/* MUX9 (1eh) = stereo mixer */
35533e95b9abSLydia Wang 	imux_is_smixer =
35543e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
35553e95b9abSLydia Wang 	/* inputs */
35563e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
35573e95b9abSLydia Wang 	parm = AC_PWRST_D3;
35583e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
35593e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
35603e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
35613e95b9abSLydia Wang 	parm = AC_PWRST_D0;
35623e95b9abSLydia Wang 	/* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
35633e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
35643e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
35653e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
35663e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
35673e95b9abSLydia Wang 
35683e95b9abSLydia Wang 	/* outputs */
35693e95b9abSLydia Wang 	/* AOW0 (8h)*/
35703e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
35713e95b9abSLydia Wang 
357211890956SLydia Wang 	if (spec->codec_type == VT1802) {
357311890956SLydia Wang 		/* PW4 (28h), MW4 (18h), MUX4(38h) */
357411890956SLydia Wang 		parm = AC_PWRST_D3;
357511890956SLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
357611890956SLydia Wang 		snd_hda_codec_write(codec, 0x18, 0,
357711890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
357811890956SLydia Wang 		snd_hda_codec_write(codec, 0x38, 0,
357911890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
358011890956SLydia Wang 	} else {
35813e95b9abSLydia Wang 		/* PW4 (26h), MW4 (1ch), MUX4(37h) */
35823e95b9abSLydia Wang 		parm = AC_PWRST_D3;
35833e95b9abSLydia Wang 		set_pin_power_state(codec, 0x26, &parm);
35843e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x1c, 0,
35853e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
35863e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x37, 0,
35873e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
358811890956SLydia Wang 	}
35893e95b9abSLydia Wang 
359011890956SLydia Wang 	if (spec->codec_type == VT1802) {
359111890956SLydia Wang 		/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
359211890956SLydia Wang 		parm = AC_PWRST_D3;
359311890956SLydia Wang 		set_pin_power_state(codec, 0x25, &parm);
359411890956SLydia Wang 		snd_hda_codec_write(codec, 0x15, 0,
359511890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
359611890956SLydia Wang 		snd_hda_codec_write(codec, 0x35, 0,
359711890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
359811890956SLydia Wang 	} else {
35993e95b9abSLydia Wang 		/* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
36003e95b9abSLydia Wang 		parm = AC_PWRST_D3;
36013e95b9abSLydia Wang 		set_pin_power_state(codec, 0x25, &parm);
36023e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x19, 0,
36033e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
36043e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x35, 0,
36053e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
360611890956SLydia Wang 	}
36073e95b9abSLydia Wang 
36083e95b9abSLydia Wang 	if (spec->hp_independent_mode)
36093e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x9, 0,
36103e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
36113e95b9abSLydia Wang 
36123e95b9abSLydia Wang 	/* Class-D */
36133e95b9abSLydia Wang 	/* PW0 (24h), MW0(18h/14h), MUX0(34h) */
36143e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x25);
36153e95b9abSLydia Wang 
36163e95b9abSLydia Wang 	parm = AC_PWRST_D3;
36173e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
36183e95b9abSLydia Wang 	parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
361911890956SLydia Wang 	if (spec->codec_type == VT1802)
362011890956SLydia Wang 		snd_hda_codec_write(codec, 0x14, 0,
362111890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
362211890956SLydia Wang 	else
36233e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x18, 0,
36243e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
36253e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x34, 0, AC_VERB_SET_POWER_STATE, parm);
36263e95b9abSLydia Wang 
36273e95b9abSLydia Wang 	/* Mono Out */
36283e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x26);
36293e95b9abSLydia Wang 
36303e95b9abSLydia Wang 	parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
363111890956SLydia Wang 	if (spec->codec_type == VT1802) {
363211890956SLydia Wang 		/* PW15 (33h), MW8(1ch), MUX8(3ch) */
363311890956SLydia Wang 		snd_hda_codec_write(codec, 0x33, 0,
363411890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
363511890956SLydia Wang 		snd_hda_codec_write(codec, 0x1c, 0,
363611890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
363711890956SLydia Wang 		snd_hda_codec_write(codec, 0x3c, 0,
363811890956SLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
363911890956SLydia Wang 	} else {
36403e95b9abSLydia Wang 		/* PW15 (31h), MW8(17h), MUX8(3bh) */
36413e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x31, 0,
36423e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
36433e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x17, 0,
36443e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
36453e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3b, 0,
36463e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, parm);
364711890956SLydia Wang 	}
36483e95b9abSLydia Wang 	/* MW9 (21h) */
36493e95b9abSLydia Wang 	if (imux_is_smixer || !is_aa_path_mute(codec))
36503e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x21, 0,
36513e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
36523e95b9abSLydia Wang 	else
36533e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x21, 0,
36543e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
36553e95b9abSLydia Wang }
365625eaba2fSLydia Wang 
365725eaba2fSLydia Wang /* patch for vt2002P */
365825eaba2fSLydia Wang static int patch_vt2002P(struct hda_codec *codec)
365925eaba2fSLydia Wang {
366025eaba2fSLydia Wang 	struct via_spec *spec;
366125eaba2fSLydia Wang 	int err;
366225eaba2fSLydia Wang 
366325eaba2fSLydia Wang 	/* create a codec specific record */
36645b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
366525eaba2fSLydia Wang 	if (spec == NULL)
366625eaba2fSLydia Wang 		return -ENOMEM;
366725eaba2fSLydia Wang 
3668620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
3669d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3670d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
367130b45033STakashi Iwai 	add_secret_dac_path(codec);
3672620e2b28STakashi Iwai 
367325eaba2fSLydia Wang 	/* automatic parse from the BIOS config */
367412daef65STakashi Iwai 	err = via_parse_auto_config(codec);
367525eaba2fSLydia Wang 	if (err < 0) {
367625eaba2fSLydia Wang 		via_free(codec);
367725eaba2fSLydia Wang 		return err;
367825eaba2fSLydia Wang 	}
367925eaba2fSLydia Wang 
368011890956SLydia Wang 	if (spec->codec_type == VT1802)
36814a918ffeSTakashi Iwai 		spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
368211890956SLydia Wang 	else
36834a918ffeSTakashi Iwai 		spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
368411890956SLydia Wang 
368525eaba2fSLydia Wang 	codec->patch_ops = via_patch_ops;
368625eaba2fSLydia Wang 
36873e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt2002P;
368825eaba2fSLydia Wang 	return 0;
368925eaba2fSLydia Wang }
3690ab6734e7SLydia Wang 
3691ab6734e7SLydia Wang /* for vt1812 */
3692ab6734e7SLydia Wang 
3693096a8854STakashi Iwai static const struct hda_verb vt1812_init_verbs[] = {
3694ab6734e7SLydia Wang 	/* Enable Boost Volume backdoor */
3695ab6734e7SLydia Wang 	{0x1, 0xfb9, 0x24},
3696ab6734e7SLydia Wang 	/* Enable AOW0 to MW9 */
3697ab6734e7SLydia Wang 	{0x1, 0xfb8, 0xa8},
3698ab6734e7SLydia Wang 	{ }
3699ab6734e7SLydia Wang };
3700ab6734e7SLydia Wang 
37013e95b9abSLydia Wang static void set_widgets_power_state_vt1812(struct hda_codec *codec)
37023e95b9abSLydia Wang {
37033e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
37043e95b9abSLydia Wang 	int imux_is_smixer =
37053e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
37063e95b9abSLydia Wang 	unsigned int parm;
37073e95b9abSLydia Wang 	unsigned int present;
37083e95b9abSLydia Wang 	/* MUX10 (1eh) = stereo mixer */
37093e95b9abSLydia Wang 	imux_is_smixer =
37103e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
37113e95b9abSLydia Wang 	/* inputs */
37123e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
37133e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37143e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
37153e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
37163e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
37173e95b9abSLydia Wang 	parm = AC_PWRST_D0;
37183e95b9abSLydia Wang 	/* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
37193e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
37203e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
37213e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
37223e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
37233e95b9abSLydia Wang 
37243e95b9abSLydia Wang 	/* outputs */
37253e95b9abSLydia Wang 	/* AOW0 (8h)*/
37263e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x8, 0,
37273e95b9abSLydia Wang 			    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
37283e95b9abSLydia Wang 
37293e95b9abSLydia Wang 	/* PW4 (28h), MW4 (18h), MUX4(38h) */
37303e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37313e95b9abSLydia Wang 	set_pin_power_state(codec, 0x28, &parm);
37323e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
37333e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x38, 0, AC_VERB_SET_POWER_STATE, parm);
37343e95b9abSLydia Wang 
37353e95b9abSLydia Wang 	/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
37363e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37373e95b9abSLydia Wang 	set_pin_power_state(codec, 0x25, &parm);
37383e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x15, 0, AC_VERB_SET_POWER_STATE, parm);
37393e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x35, 0, AC_VERB_SET_POWER_STATE, parm);
37403e95b9abSLydia Wang 	if (spec->hp_independent_mode)
37413e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x9, 0,
37423e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
37433e95b9abSLydia Wang 
37443e95b9abSLydia Wang 	/* Internal Speaker */
37453e95b9abSLydia Wang 	/* PW0 (24h), MW0(14h), MUX0(34h) */
37463e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x25);
37473e95b9abSLydia Wang 
37483e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37493e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
37503e95b9abSLydia Wang 	if (present) {
37513e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x14, 0,
37523e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
37533e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x34, 0,
37543e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
37553e95b9abSLydia Wang 	} else {
37563e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x14, 0,
37573e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
37583e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x34, 0,
37593e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
37603e95b9abSLydia Wang 	}
37613e95b9abSLydia Wang 
37623e95b9abSLydia Wang 
37633e95b9abSLydia Wang 	/* Mono Out */
37643e95b9abSLydia Wang 	/* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
37653e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x28);
37663e95b9abSLydia Wang 
37673e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37683e95b9abSLydia Wang 	set_pin_power_state(codec, 0x31, &parm);
37693e95b9abSLydia Wang 	if (present) {
37703e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x1c, 0,
37713e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
37723e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3c, 0,
37733e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
37743e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3e, 0,
37753e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
37763e95b9abSLydia Wang 	} else {
37773e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x1c, 0,
37783e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
37793e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3c, 0,
37803e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
37813e95b9abSLydia Wang 		snd_hda_codec_write(codec, 0x3e, 0,
37823e95b9abSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
37833e95b9abSLydia Wang 	}
37843e95b9abSLydia Wang 
37853e95b9abSLydia Wang 	/* PW15 (33h), MW15 (1dh), MUX15(3dh) */
37863e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37873e95b9abSLydia Wang 	set_pin_power_state(codec, 0x33, &parm);
37883e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
37893e95b9abSLydia Wang 	snd_hda_codec_write(codec, 0x3d, 0, AC_VERB_SET_POWER_STATE, parm);
37903e95b9abSLydia Wang 
37913e95b9abSLydia Wang }
3792ab6734e7SLydia Wang 
3793ab6734e7SLydia Wang /* patch for vt1812 */
3794ab6734e7SLydia Wang static int patch_vt1812(struct hda_codec *codec)
3795ab6734e7SLydia Wang {
3796ab6734e7SLydia Wang 	struct via_spec *spec;
3797ab6734e7SLydia Wang 	int err;
3798ab6734e7SLydia Wang 
3799ab6734e7SLydia Wang 	/* create a codec specific record */
38005b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3801ab6734e7SLydia Wang 	if (spec == NULL)
3802ab6734e7SLydia Wang 		return -ENOMEM;
3803ab6734e7SLydia Wang 
3804620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
3805d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3806d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
380730b45033STakashi Iwai 	add_secret_dac_path(codec);
3808620e2b28STakashi Iwai 
3809ab6734e7SLydia Wang 	/* automatic parse from the BIOS config */
381012daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3811ab6734e7SLydia Wang 	if (err < 0) {
3812ab6734e7SLydia Wang 		via_free(codec);
3813ab6734e7SLydia Wang 		return err;
3814ab6734e7SLydia Wang 	}
3815ab6734e7SLydia Wang 
3816096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++]  = vt1812_init_verbs;
3817ab6734e7SLydia Wang 
3818ab6734e7SLydia Wang 	codec->patch_ops = via_patch_ops;
3819ab6734e7SLydia Wang 
38203e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1812;
3821ab6734e7SLydia Wang 	return 0;
3822ab6734e7SLydia Wang }
3823ab6734e7SLydia Wang 
3824c577b8a1SJoseph Chan /*
3825c577b8a1SJoseph Chan  * patch entries
3826c577b8a1SJoseph Chan  */
382790dd48a1STakashi Iwai static const struct hda_codec_preset snd_hda_preset_via[] = {
38283218c178STakashi Iwai 	{ .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
38293218c178STakashi Iwai 	{ .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
38303218c178STakashi Iwai 	{ .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
38313218c178STakashi Iwai 	{ .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
38323218c178STakashi Iwai 	{ .id = 0x1106e710, .name = "VT1709 10-Ch",
3833ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38343218c178STakashi Iwai 	{ .id = 0x1106e711, .name = "VT1709 10-Ch",
3835ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38363218c178STakashi Iwai 	{ .id = 0x1106e712, .name = "VT1709 10-Ch",
3837ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38383218c178STakashi Iwai 	{ .id = 0x1106e713, .name = "VT1709 10-Ch",
3839ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38403218c178STakashi Iwai 	{ .id = 0x1106e714, .name = "VT1709 6-Ch",
3841ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38423218c178STakashi Iwai 	{ .id = 0x1106e715, .name = "VT1709 6-Ch",
3843ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38443218c178STakashi Iwai 	{ .id = 0x1106e716, .name = "VT1709 6-Ch",
3845ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38463218c178STakashi Iwai 	{ .id = 0x1106e717, .name = "VT1709 6-Ch",
3847ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38483218c178STakashi Iwai 	{ .id = 0x1106e720, .name = "VT1708B 8-Ch",
3849ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38503218c178STakashi Iwai 	{ .id = 0x1106e721, .name = "VT1708B 8-Ch",
3851ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38523218c178STakashi Iwai 	{ .id = 0x1106e722, .name = "VT1708B 8-Ch",
3853ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38543218c178STakashi Iwai 	{ .id = 0x1106e723, .name = "VT1708B 8-Ch",
3855ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38563218c178STakashi Iwai 	{ .id = 0x1106e724, .name = "VT1708B 4-Ch",
3857ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38583218c178STakashi Iwai 	{ .id = 0x1106e725, .name = "VT1708B 4-Ch",
3859ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38603218c178STakashi Iwai 	{ .id = 0x1106e726, .name = "VT1708B 4-Ch",
3861ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38623218c178STakashi Iwai 	{ .id = 0x1106e727, .name = "VT1708B 4-Ch",
3863ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38643218c178STakashi Iwai 	{ .id = 0x11060397, .name = "VT1708S",
3865d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38663218c178STakashi Iwai 	{ .id = 0x11061397, .name = "VT1708S",
3867d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38683218c178STakashi Iwai 	{ .id = 0x11062397, .name = "VT1708S",
3869d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38703218c178STakashi Iwai 	{ .id = 0x11063397, .name = "VT1708S",
3871d949cac1SHarald Welte 	  .patch = patch_vt1708S},
3872bc92df7fSLydia Wang 	{ .id = 0x11064397, .name = "VT1705",
3873d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38743218c178STakashi Iwai 	{ .id = 0x11065397, .name = "VT1708S",
3875d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38763218c178STakashi Iwai 	{ .id = 0x11066397, .name = "VT1708S",
3877d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38783218c178STakashi Iwai 	{ .id = 0x11067397, .name = "VT1708S",
3879d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38803218c178STakashi Iwai 	{ .id = 0x11060398, .name = "VT1702",
3881d949cac1SHarald Welte 	  .patch = patch_vt1702},
38823218c178STakashi Iwai 	{ .id = 0x11061398, .name = "VT1702",
3883d949cac1SHarald Welte 	  .patch = patch_vt1702},
38843218c178STakashi Iwai 	{ .id = 0x11062398, .name = "VT1702",
3885d949cac1SHarald Welte 	  .patch = patch_vt1702},
38863218c178STakashi Iwai 	{ .id = 0x11063398, .name = "VT1702",
3887d949cac1SHarald Welte 	  .patch = patch_vt1702},
38883218c178STakashi Iwai 	{ .id = 0x11064398, .name = "VT1702",
3889d949cac1SHarald Welte 	  .patch = patch_vt1702},
38903218c178STakashi Iwai 	{ .id = 0x11065398, .name = "VT1702",
3891d949cac1SHarald Welte 	  .patch = patch_vt1702},
38923218c178STakashi Iwai 	{ .id = 0x11066398, .name = "VT1702",
3893d949cac1SHarald Welte 	  .patch = patch_vt1702},
38943218c178STakashi Iwai 	{ .id = 0x11067398, .name = "VT1702",
3895d949cac1SHarald Welte 	  .patch = patch_vt1702},
3896eb7188caSLydia Wang 	{ .id = 0x11060428, .name = "VT1718S",
3897eb7188caSLydia Wang 	  .patch = patch_vt1718S},
3898eb7188caSLydia Wang 	{ .id = 0x11064428, .name = "VT1718S",
3899eb7188caSLydia Wang 	  .patch = patch_vt1718S},
3900bb3c6bfcSLydia Wang 	{ .id = 0x11060441, .name = "VT2020",
3901bb3c6bfcSLydia Wang 	  .patch = patch_vt1718S},
3902bb3c6bfcSLydia Wang 	{ .id = 0x11064441, .name = "VT1828S",
3903bb3c6bfcSLydia Wang 	  .patch = patch_vt1718S},
3904f3db423dSLydia Wang 	{ .id = 0x11060433, .name = "VT1716S",
3905f3db423dSLydia Wang 	  .patch = patch_vt1716S},
3906f3db423dSLydia Wang 	{ .id = 0x1106a721, .name = "VT1716S",
3907f3db423dSLydia Wang 	  .patch = patch_vt1716S},
390825eaba2fSLydia Wang 	{ .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
390925eaba2fSLydia Wang 	{ .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
3910ab6734e7SLydia Wang 	{ .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
391136dd5c4aSLydia Wang 	{ .id = 0x11060440, .name = "VT1818S",
391236dd5c4aSLydia Wang 	  .patch = patch_vt1708S},
391311890956SLydia Wang 	{ .id = 0x11060446, .name = "VT1802",
391411890956SLydia Wang 		.patch = patch_vt2002P},
391511890956SLydia Wang 	{ .id = 0x11068446, .name = "VT1802",
391611890956SLydia Wang 		.patch = patch_vt2002P},
3917c577b8a1SJoseph Chan 	{} /* terminator */
3918c577b8a1SJoseph Chan };
39191289e9e8STakashi Iwai 
39201289e9e8STakashi Iwai MODULE_ALIAS("snd-hda-codec-id:1106*");
39211289e9e8STakashi Iwai 
39221289e9e8STakashi Iwai static struct hda_codec_preset_list via_list = {
39231289e9e8STakashi Iwai 	.preset = snd_hda_preset_via,
39241289e9e8STakashi Iwai 	.owner = THIS_MODULE,
39251289e9e8STakashi Iwai };
39261289e9e8STakashi Iwai 
39271289e9e8STakashi Iwai MODULE_LICENSE("GPL");
39281289e9e8STakashi Iwai MODULE_DESCRIPTION("VIA HD-audio codec");
39291289e9e8STakashi Iwai 
39301289e9e8STakashi Iwai static int __init patch_via_init(void)
39311289e9e8STakashi Iwai {
39321289e9e8STakashi Iwai 	return snd_hda_add_codec_preset(&via_list);
39331289e9e8STakashi Iwai }
39341289e9e8STakashi Iwai 
39351289e9e8STakashi Iwai static void __exit patch_via_exit(void)
39361289e9e8STakashi Iwai {
39371289e9e8STakashi Iwai 	snd_hda_delete_codec_preset(&via_list);
39381289e9e8STakashi Iwai }
39391289e9e8STakashi Iwai 
39401289e9e8STakashi Iwai module_init(patch_via_init)
39411289e9e8STakashi Iwai module_exit(patch_via_exit)
3942