xref: /openbmc/linux/sound/pci/hda/patch_via.c (revision 7819d1c70eb6a57e43554d86e10b39d1e106ed65)
1c577b8a1SJoseph Chan /*
2c577b8a1SJoseph Chan  * Universal Interface for Intel High Definition Audio Codec
3c577b8a1SJoseph Chan  *
48e86597fSLydia Wang  * HD audio interface patch for VIA VT17xx/VT18xx/VT20xx codec
5c577b8a1SJoseph Chan  *
68e86597fSLydia Wang  *  (C) 2006-2009 VIA Technology, Inc.
78e86597fSLydia Wang  *  (C) 2006-2008 Takashi Iwai <tiwai@suse.de>
8c577b8a1SJoseph Chan  *
9c577b8a1SJoseph Chan  *  This driver is free software; you can redistribute it and/or modify
10c577b8a1SJoseph Chan  *  it under the terms of the GNU General Public License as published by
11c577b8a1SJoseph Chan  *  the Free Software Foundation; either version 2 of the License, or
12c577b8a1SJoseph Chan  *  (at your option) any later version.
13c577b8a1SJoseph Chan  *
14c577b8a1SJoseph Chan  *  This driver is distributed in the hope that it will be useful,
15c577b8a1SJoseph Chan  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16c577b8a1SJoseph Chan  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17c577b8a1SJoseph Chan  *  GNU General Public License for more details.
18c577b8a1SJoseph Chan  *
19c577b8a1SJoseph Chan  *  You should have received a copy of the GNU General Public License
20c577b8a1SJoseph Chan  *  along with this program; if not, write to the Free Software
21c577b8a1SJoseph Chan  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22c577b8a1SJoseph Chan  */
23c577b8a1SJoseph Chan 
24c577b8a1SJoseph Chan /* * * * * * * * * * * * * * Release History * * * * * * * * * * * * * * * * */
25c577b8a1SJoseph Chan /*									     */
26c577b8a1SJoseph Chan /* 2006-03-03  Lydia Wang  Create the basic patch to support VT1708 codec    */
27c577b8a1SJoseph Chan /* 2006-03-14  Lydia Wang  Modify hard code for some pin widget nid	     */
28c577b8a1SJoseph Chan /* 2006-08-02  Lydia Wang  Add support to VT1709 codec			     */
29c577b8a1SJoseph Chan /* 2006-09-08  Lydia Wang  Fix internal loopback recording source select bug */
30f7278fd0SJosepch Chan /* 2007-09-12  Lydia Wang  Add EAPD enable during driver initialization	     */
31f7278fd0SJosepch Chan /* 2007-09-17  Lydia Wang  Add VT1708B codec support			    */
3276d9b0ddSHarald Welte /* 2007-11-14  Lydia Wang  Add VT1708A codec HP and CD pin connect config    */
33fb4cb772SHarald Welte /* 2008-02-03  Lydia Wang  Fix Rear channels and Back channels inverse issue */
34d949cac1SHarald Welte /* 2008-03-06  Lydia Wang  Add VT1702 codec and VT1708S codec support	     */
3569e52a80SHarald Welte /* 2008-04-09  Lydia Wang  Add mute front speaker when HP plugin	     */
360aa62aefSHarald Welte /* 2008-04-09  Lydia Wang  Add Independent HP feature			     */
3798aa34c0SHarald Welte /* 2008-05-28  Lydia Wang  Add second S/PDIF Out support for VT1702	     */
38d7426329SHarald Welte /* 2008-09-15  Logan Li	   Add VT1708S Mic Boost workaround/backdoor	     */
398e86597fSLydia Wang /* 2009-02-16  Logan Li	   Add support for VT1718S			     */
408e86597fSLydia Wang /* 2009-03-13  Logan Li	   Add support for VT1716S			     */
418e86597fSLydia Wang /* 2009-04-14  Lydai Wang  Add support for VT1828S and VT2020		     */
428e86597fSLydia Wang /* 2009-07-08  Lydia Wang  Add support for VT2002P			     */
438e86597fSLydia Wang /* 2009-07-21  Lydia Wang  Add support for VT1812			     */
4436dd5c4aSLydia Wang /* 2009-09-19  Lydia Wang  Add support for VT1818S			     */
45c577b8a1SJoseph Chan /*									     */
46c577b8a1SJoseph Chan /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
47c577b8a1SJoseph Chan 
48c577b8a1SJoseph Chan 
49c577b8a1SJoseph Chan #include <linux/init.h>
50c577b8a1SJoseph Chan #include <linux/delay.h>
51c577b8a1SJoseph Chan #include <linux/slab.h>
52da155d5bSPaul Gortmaker #include <linux/module.h>
53c577b8a1SJoseph Chan #include <sound/core.h>
540aa62aefSHarald Welte #include <sound/asoundef.h>
55c577b8a1SJoseph Chan #include "hda_codec.h"
56c577b8a1SJoseph Chan #include "hda_local.h"
57128bc4baSTakashi Iwai #include "hda_auto_parser.h"
581835a0f9STakashi Iwai #include "hda_jack.h"
59c577b8a1SJoseph Chan 
60c577b8a1SJoseph Chan /* Pin Widget NID */
6176d9b0ddSHarald Welte #define VT1708_HP_PIN_NID	0x20
6276d9b0ddSHarald Welte #define VT1708_CD_PIN_NID	0x24
63c577b8a1SJoseph Chan 
64d7426329SHarald Welte enum VIA_HDA_CODEC {
65d7426329SHarald Welte 	UNKNOWN = -1,
66d7426329SHarald Welte 	VT1708,
67d7426329SHarald Welte 	VT1709_10CH,
68d7426329SHarald Welte 	VT1709_6CH,
69d7426329SHarald Welte 	VT1708B_8CH,
70d7426329SHarald Welte 	VT1708B_4CH,
71d7426329SHarald Welte 	VT1708S,
72518bf3baSLydia Wang 	VT1708BCE,
73d7426329SHarald Welte 	VT1702,
74eb7188caSLydia Wang 	VT1718S,
75f3db423dSLydia Wang 	VT1716S,
7625eaba2fSLydia Wang 	VT2002P,
77ab6734e7SLydia Wang 	VT1812,
7811890956SLydia Wang 	VT1802,
79d7426329SHarald Welte 	CODEC_TYPES,
80d7426329SHarald Welte };
81d7426329SHarald Welte 
8211890956SLydia Wang #define VT2002P_COMPATIBLE(spec) \
8311890956SLydia Wang 	((spec)->codec_type == VT2002P ||\
8411890956SLydia Wang 	 (spec)->codec_type == VT1812 ||\
8511890956SLydia Wang 	 (spec)->codec_type == VT1802)
8611890956SLydia Wang 
878e3679dcSTakashi Iwai #define MAX_NID_PATH_DEPTH	5
888e3679dcSTakashi Iwai 
8909a9ad69STakashi Iwai /* output-path: DAC -> ... -> pin
9009a9ad69STakashi Iwai  * idx[] contains the source index number of the next widget;
9109a9ad69STakashi Iwai  * e.g. idx[0] is the index of the DAC selected by path[1] widget
9209a9ad69STakashi Iwai  * multi[] indicates whether it's a selector widget with multi-connectors
9309a9ad69STakashi Iwai  * (i.e. the connection selection is mandatory)
9409a9ad69STakashi Iwai  * vol_ctl and mute_ctl contains the NIDs for the assigned mixers
9509a9ad69STakashi Iwai  */
964a79616dSTakashi Iwai struct nid_path {
974a79616dSTakashi Iwai 	int depth;
988e3679dcSTakashi Iwai 	hda_nid_t path[MAX_NID_PATH_DEPTH];
9909a9ad69STakashi Iwai 	unsigned char idx[MAX_NID_PATH_DEPTH];
10009a9ad69STakashi Iwai 	unsigned char multi[MAX_NID_PATH_DEPTH];
10109a9ad69STakashi Iwai 	unsigned int vol_ctl;
10209a9ad69STakashi Iwai 	unsigned int mute_ctl;
1034a79616dSTakashi Iwai };
1044a79616dSTakashi Iwai 
105a86a88eaSTakashi Iwai /* input-path */
106a86a88eaSTakashi Iwai struct via_input {
107a86a88eaSTakashi Iwai 	hda_nid_t pin;	/* input-pin or aa-mix */
108a86a88eaSTakashi Iwai 	int adc_idx;	/* ADC index to be used */
109a86a88eaSTakashi Iwai 	int mux_idx;	/* MUX index (if any) */
110a86a88eaSTakashi Iwai 	const char *label;	/* input-source label */
111a86a88eaSTakashi Iwai };
112a86a88eaSTakashi Iwai 
113de6c74f3STakashi Iwai #define VIA_MAX_ADCS	3
114de6c74f3STakashi Iwai 
1153b607e3dSTakashi Iwai enum {
1163b607e3dSTakashi Iwai 	STREAM_MULTI_OUT = (1 << 0),
1173b607e3dSTakashi Iwai 	STREAM_INDEP_HP = (1 << 1),
1183b607e3dSTakashi Iwai };
1193b607e3dSTakashi Iwai 
1201f2e99feSLydia Wang struct via_spec {
121*7819d1c7STakashi Iwai 	struct hda_gen_spec gen;
122*7819d1c7STakashi Iwai 
1231f2e99feSLydia Wang 	/* codec parameterization */
12490dd48a1STakashi Iwai 	const struct snd_kcontrol_new *mixers[6];
1251f2e99feSLydia Wang 	unsigned int num_mixers;
1261f2e99feSLydia Wang 
12790dd48a1STakashi Iwai 	const struct hda_verb *init_verbs[5];
1281f2e99feSLydia Wang 	unsigned int num_iverbs;
1291f2e99feSLydia Wang 
13082673bc8STakashi Iwai 	char stream_name_analog[32];
1317eb56e84STakashi Iwai 	char stream_name_hp[32];
13290dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_analog_playback;
13390dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_analog_capture;
1341f2e99feSLydia Wang 
13582673bc8STakashi Iwai 	char stream_name_digital[32];
13690dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_digital_playback;
13790dd48a1STakashi Iwai 	const struct hda_pcm_stream *stream_digital_capture;
1381f2e99feSLydia Wang 
1391f2e99feSLydia Wang 	/* playback */
1401f2e99feSLydia Wang 	struct hda_multi_out multiout;
1411f2e99feSLydia Wang 	hda_nid_t slave_dig_outs[2];
142ece8d043STakashi Iwai 	hda_nid_t hp_dac_nid;
1433214b966STakashi Iwai 	hda_nid_t speaker_dac_nid;
1443214b966STakashi Iwai 	int hp_indep_shared;	/* indep HP-DAC is shared with side ch */
1453b607e3dSTakashi Iwai 	int opened_streams;	/* STREAM_* bits */
1463b607e3dSTakashi Iwai 	int active_streams;	/* STREAM_* bits */
1473214b966STakashi Iwai 	int aamix_mode;		/* loopback is enabled for output-path? */
1481f2e99feSLydia Wang 
1493214b966STakashi Iwai 	/* Output-paths:
1503214b966STakashi Iwai 	 * There are different output-paths depending on the setup.
1513214b966STakashi Iwai 	 * out_path, hp_path and speaker_path are primary paths.  If both
1523214b966STakashi Iwai 	 * direct DAC and aa-loopback routes are available, these contain
1533214b966STakashi Iwai 	 * the former paths.  Meanwhile *_mix_path contain the paths with
1543214b966STakashi Iwai 	 * loopback mixer.  (Since the loopback is only for front channel,
1553214b966STakashi Iwai 	 * no out_mix_path for surround channels.)
1563214b966STakashi Iwai 	 * The HP output has another path, hp_indep_path, which is used in
1573214b966STakashi Iwai 	 * the independent-HP mode.
1583214b966STakashi Iwai 	 */
159de6c74f3STakashi Iwai 	struct nid_path out_path[HDA_SIDE + 1];
1603214b966STakashi Iwai 	struct nid_path out_mix_path;
1614a79616dSTakashi Iwai 	struct nid_path hp_path;
1623214b966STakashi Iwai 	struct nid_path hp_mix_path;
1633214b966STakashi Iwai 	struct nid_path hp_indep_path;
1644a918ffeSTakashi Iwai 	struct nid_path speaker_path;
1653214b966STakashi Iwai 	struct nid_path speaker_mix_path;
1664a79616dSTakashi Iwai 
1671f2e99feSLydia Wang 	/* capture */
1681f2e99feSLydia Wang 	unsigned int num_adc_nids;
169de6c74f3STakashi Iwai 	hda_nid_t adc_nids[VIA_MAX_ADCS];
170de6c74f3STakashi Iwai 	hda_nid_t mux_nids[VIA_MAX_ADCS];
171620e2b28STakashi Iwai 	hda_nid_t aa_mix_nid;
1721f2e99feSLydia Wang 	hda_nid_t dig_in_nid;
1731f2e99feSLydia Wang 
1741f2e99feSLydia Wang 	/* capture source */
175a86a88eaSTakashi Iwai 	bool dyn_adc_switch;
176a86a88eaSTakashi Iwai 	int num_inputs;
177a86a88eaSTakashi Iwai 	struct via_input inputs[AUTO_CFG_MAX_INS + 1];
178de6c74f3STakashi Iwai 	unsigned int cur_mux[VIA_MAX_ADCS];
1791f2e99feSLydia Wang 
1803b607e3dSTakashi Iwai 	/* dynamic DAC switching */
1813b607e3dSTakashi Iwai 	unsigned int cur_dac_stream_tag;
1823b607e3dSTakashi Iwai 	unsigned int cur_dac_format;
1833b607e3dSTakashi Iwai 	unsigned int cur_hp_stream_tag;
1843b607e3dSTakashi Iwai 	unsigned int cur_hp_format;
1853b607e3dSTakashi Iwai 
186a86a88eaSTakashi Iwai 	/* dynamic ADC switching */
187a86a88eaSTakashi Iwai 	hda_nid_t cur_adc;
188a86a88eaSTakashi Iwai 	unsigned int cur_adc_stream_tag;
189a86a88eaSTakashi Iwai 	unsigned int cur_adc_format;
190a86a88eaSTakashi Iwai 
1911f2e99feSLydia Wang 	/* PCM information */
1921f2e99feSLydia Wang 	struct hda_pcm pcm_rec[3];
1931f2e99feSLydia Wang 
1941f2e99feSLydia Wang 	/* dynamic controls, init_verbs and input_mux */
1951f2e99feSLydia Wang 	struct auto_pin_cfg autocfg;
1961f2e99feSLydia Wang 	struct snd_array kctls;
1971f2e99feSLydia Wang 	hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
1981f2e99feSLydia Wang 
1991f2e99feSLydia Wang 	/* HP mode source */
2001f2e99feSLydia Wang 	unsigned int hp_independent_mode;
201f3db423dSLydia Wang 	unsigned int dmic_enabled;
20224088a58STakashi Iwai 	unsigned int no_pin_power_ctl;
2031f2e99feSLydia Wang 	enum VIA_HDA_CODEC codec_type;
2041f2e99feSLydia Wang 
205e9d010c2STakashi Iwai 	/* analog low-power control */
206e9d010c2STakashi Iwai 	bool alc_mode;
207e9d010c2STakashi Iwai 
208e3d7a143STakashi Iwai 	/* smart51 setup */
209e3d7a143STakashi Iwai 	unsigned int smart51_nums;
210e3d7a143STakashi Iwai 	hda_nid_t smart51_pins[2];
211e3d7a143STakashi Iwai 	int smart51_idxs[2];
212e3d7a143STakashi Iwai 	const char *smart51_labels[2];
213e3d7a143STakashi Iwai 	unsigned int smart51_enabled;
214e3d7a143STakashi Iwai 
2151f2e99feSLydia Wang 	/* work to check hp jack state */
2161f2e99feSLydia Wang 	struct hda_codec *codec;
2171f2e99feSLydia Wang 	struct delayed_work vt1708_hp_work;
218187d333eSTakashi Iwai 	int hp_work_active;
219e06e5a29STakashi Iwai 	int vt1708_jack_detect;
2201f2e99feSLydia Wang 	int vt1708_hp_present;
2213e95b9abSLydia Wang 
2223e95b9abSLydia Wang 	void (*set_widgets_power_state)(struct hda_codec *codec);
2233e95b9abSLydia Wang 
2241f2e99feSLydia Wang 	struct hda_loopback_check loopback;
22513af8e77STakashi Iwai 	int num_loopbacks;
22613af8e77STakashi Iwai 	struct hda_amp_list loopback_list[8];
227a86a88eaSTakashi Iwai 
228a86a88eaSTakashi Iwai 	/* bind capture-volume */
229a86a88eaSTakashi Iwai 	struct hda_bind_ctls *bind_cap_vol;
230a86a88eaSTakashi Iwai 	struct hda_bind_ctls *bind_cap_sw;
2313b607e3dSTakashi Iwai 
2323b607e3dSTakashi Iwai 	struct mutex config_mutex;
2331f2e99feSLydia Wang };
2341f2e99feSLydia Wang 
2350341ccd7SLydia Wang static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
2365b0cb1d8SJaroslav Kysela static struct via_spec * via_new_spec(struct hda_codec *codec)
2375b0cb1d8SJaroslav Kysela {
2385b0cb1d8SJaroslav Kysela 	struct via_spec *spec;
2395b0cb1d8SJaroslav Kysela 
2405b0cb1d8SJaroslav Kysela 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2415b0cb1d8SJaroslav Kysela 	if (spec == NULL)
2425b0cb1d8SJaroslav Kysela 		return NULL;
2435b0cb1d8SJaroslav Kysela 
2443b607e3dSTakashi Iwai 	mutex_init(&spec->config_mutex);
2455b0cb1d8SJaroslav Kysela 	codec->spec = spec;
2465b0cb1d8SJaroslav Kysela 	spec->codec = codec;
2470341ccd7SLydia Wang 	spec->codec_type = get_codec_type(codec);
2480341ccd7SLydia Wang 	/* VT1708BCE & VT1708S are almost same */
2490341ccd7SLydia Wang 	if (spec->codec_type == VT1708BCE)
2500341ccd7SLydia Wang 		spec->codec_type = VT1708S;
251*7819d1c7STakashi Iwai 	snd_hda_gen_init(&spec->gen);
2525b0cb1d8SJaroslav Kysela 	return spec;
2535b0cb1d8SJaroslav Kysela }
2545b0cb1d8SJaroslav Kysela 
255744ff5f4SLydia Wang static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
256d7426329SHarald Welte {
257744ff5f4SLydia Wang 	u32 vendor_id = codec->vendor_id;
258d7426329SHarald Welte 	u16 ven_id = vendor_id >> 16;
259d7426329SHarald Welte 	u16 dev_id = vendor_id & 0xffff;
260d7426329SHarald Welte 	enum VIA_HDA_CODEC codec_type;
261d7426329SHarald Welte 
262d7426329SHarald Welte 	/* get codec type */
263d7426329SHarald Welte 	if (ven_id != 0x1106)
264d7426329SHarald Welte 		codec_type = UNKNOWN;
265d7426329SHarald Welte 	else if (dev_id >= 0x1708 && dev_id <= 0x170b)
266d7426329SHarald Welte 		codec_type = VT1708;
267d7426329SHarald Welte 	else if (dev_id >= 0xe710 && dev_id <= 0xe713)
268d7426329SHarald Welte 		codec_type = VT1709_10CH;
269d7426329SHarald Welte 	else if (dev_id >= 0xe714 && dev_id <= 0xe717)
270d7426329SHarald Welte 		codec_type = VT1709_6CH;
271518bf3baSLydia Wang 	else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
272d7426329SHarald Welte 		codec_type = VT1708B_8CH;
273518bf3baSLydia Wang 		if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
274518bf3baSLydia Wang 			codec_type = VT1708BCE;
275518bf3baSLydia Wang 	} else if (dev_id >= 0xe724 && dev_id <= 0xe727)
276d7426329SHarald Welte 		codec_type = VT1708B_4CH;
277d7426329SHarald Welte 	else if ((dev_id & 0xfff) == 0x397
278d7426329SHarald Welte 		 && (dev_id >> 12) < 8)
279d7426329SHarald Welte 		codec_type = VT1708S;
280d7426329SHarald Welte 	else if ((dev_id & 0xfff) == 0x398
281d7426329SHarald Welte 		 && (dev_id >> 12) < 8)
282d7426329SHarald Welte 		codec_type = VT1702;
283eb7188caSLydia Wang 	else if ((dev_id & 0xfff) == 0x428
284eb7188caSLydia Wang 		 && (dev_id >> 12) < 8)
285eb7188caSLydia Wang 		codec_type = VT1718S;
286f3db423dSLydia Wang 	else if (dev_id == 0x0433 || dev_id == 0xa721)
287f3db423dSLydia Wang 		codec_type = VT1716S;
288bb3c6bfcSLydia Wang 	else if (dev_id == 0x0441 || dev_id == 0x4441)
289bb3c6bfcSLydia Wang 		codec_type = VT1718S;
29025eaba2fSLydia Wang 	else if (dev_id == 0x0438 || dev_id == 0x4438)
29125eaba2fSLydia Wang 		codec_type = VT2002P;
292ab6734e7SLydia Wang 	else if (dev_id == 0x0448)
293ab6734e7SLydia Wang 		codec_type = VT1812;
29436dd5c4aSLydia Wang 	else if (dev_id == 0x0440)
29536dd5c4aSLydia Wang 		codec_type = VT1708S;
29611890956SLydia Wang 	else if ((dev_id & 0xfff) == 0x446)
29711890956SLydia Wang 		codec_type = VT1802;
298d7426329SHarald Welte 	else
299d7426329SHarald Welte 		codec_type = UNKNOWN;
300d7426329SHarald Welte 	return codec_type;
301d7426329SHarald Welte };
302d7426329SHarald Welte 
303ec7e7e42SLydia Wang #define VIA_JACK_EVENT		0x20
30469e52a80SHarald Welte #define VIA_HP_EVENT		0x01
3054a918ffeSTakashi Iwai #define VIA_LINE_EVENT		0x03
30669e52a80SHarald Welte 
307c577b8a1SJoseph Chan enum {
308c577b8a1SJoseph Chan 	VIA_CTL_WIDGET_VOL,
309c577b8a1SJoseph Chan 	VIA_CTL_WIDGET_MUTE,
310f5271101SLydia Wang 	VIA_CTL_WIDGET_ANALOG_MUTE,
311c577b8a1SJoseph Chan };
312c577b8a1SJoseph Chan 
313ada509ecSTakashi Iwai static void analog_low_current_mode(struct hda_codec *codec);
314ada509ecSTakashi Iwai static bool is_aa_path_mute(struct hda_codec *codec);
3151f2e99feSLydia Wang 
316187d333eSTakashi Iwai #define hp_detect_with_aa(codec) \
317187d333eSTakashi Iwai 	(snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1 && \
318187d333eSTakashi Iwai 	 !is_aa_path_mute(codec))
3191f2e99feSLydia Wang 
3201f2e99feSLydia Wang static void vt1708_stop_hp_work(struct via_spec *spec)
3211f2e99feSLydia Wang {
3221f2e99feSLydia Wang 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
3231f2e99feSLydia Wang 		return;
324187d333eSTakashi Iwai 	if (spec->hp_work_active) {
325187d333eSTakashi Iwai 		snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 1);
3265b84ba26STejun Heo 		cancel_delayed_work_sync(&spec->vt1708_hp_work);
327187d333eSTakashi Iwai 		spec->hp_work_active = 0;
328187d333eSTakashi Iwai 	}
329187d333eSTakashi Iwai }
330187d333eSTakashi Iwai 
331187d333eSTakashi Iwai static void vt1708_update_hp_work(struct via_spec *spec)
332187d333eSTakashi Iwai {
333187d333eSTakashi Iwai 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
334187d333eSTakashi Iwai 		return;
335187d333eSTakashi Iwai 	if (spec->vt1708_jack_detect &&
336187d333eSTakashi Iwai 	    (spec->active_streams || hp_detect_with_aa(spec->codec))) {
337187d333eSTakashi Iwai 		if (!spec->hp_work_active) {
338187d333eSTakashi Iwai 			snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 0);
339187d333eSTakashi Iwai 			schedule_delayed_work(&spec->vt1708_hp_work,
340187d333eSTakashi Iwai 					      msecs_to_jiffies(100));
341187d333eSTakashi Iwai 			spec->hp_work_active = 1;
342187d333eSTakashi Iwai 		}
343187d333eSTakashi Iwai 	} else if (!hp_detect_with_aa(spec->codec))
344187d333eSTakashi Iwai 		vt1708_stop_hp_work(spec);
3451f2e99feSLydia Wang }
346f5271101SLydia Wang 
3473e95b9abSLydia Wang static void set_widgets_power_state(struct hda_codec *codec)
3483e95b9abSLydia Wang {
3493e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
3503e95b9abSLydia Wang 	if (spec->set_widgets_power_state)
3513e95b9abSLydia Wang 		spec->set_widgets_power_state(codec);
3523e95b9abSLydia Wang }
35325eaba2fSLydia Wang 
354f5271101SLydia Wang static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
355f5271101SLydia Wang 				   struct snd_ctl_elem_value *ucontrol)
356f5271101SLydia Wang {
357f5271101SLydia Wang 	int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
358f5271101SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
359f5271101SLydia Wang 
3603e95b9abSLydia Wang 	set_widgets_power_state(codec);
361ada509ecSTakashi Iwai 	analog_low_current_mode(snd_kcontrol_chip(kcontrol));
362187d333eSTakashi Iwai 	vt1708_update_hp_work(codec->spec);
363f5271101SLydia Wang 	return change;
364f5271101SLydia Wang }
365f5271101SLydia Wang 
366f5271101SLydia Wang /* modify .put = snd_hda_mixer_amp_switch_put */
367f5271101SLydia Wang #define ANALOG_INPUT_MUTE						\
368f5271101SLydia Wang 	{		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,		\
369f5271101SLydia Wang 			.name = NULL,					\
370f5271101SLydia Wang 			.index = 0,					\
371f5271101SLydia Wang 			.info = snd_hda_mixer_amp_switch_info,		\
372f5271101SLydia Wang 			.get = snd_hda_mixer_amp_switch_get,		\
373f5271101SLydia Wang 			.put = analog_input_switch_put,			\
374f5271101SLydia Wang 			.private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
375f5271101SLydia Wang 
37690dd48a1STakashi Iwai static const struct snd_kcontrol_new via_control_templates[] = {
377c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME(NULL, 0, 0, 0),
378c577b8a1SJoseph Chan 	HDA_CODEC_MUTE(NULL, 0, 0, 0),
379f5271101SLydia Wang 	ANALOG_INPUT_MUTE,
380c577b8a1SJoseph Chan };
381c577b8a1SJoseph Chan 
382ab6734e7SLydia Wang 
383c577b8a1SJoseph Chan /* add dynamic controls */
384291c9e33STakashi Iwai static struct snd_kcontrol_new *__via_clone_ctl(struct via_spec *spec,
385291c9e33STakashi Iwai 				const struct snd_kcontrol_new *tmpl,
386291c9e33STakashi Iwai 				const char *name)
387c577b8a1SJoseph Chan {
388c577b8a1SJoseph Chan 	struct snd_kcontrol_new *knew;
389c577b8a1SJoseph Chan 
390603c4019STakashi Iwai 	snd_array_init(&spec->kctls, sizeof(*knew), 32);
391603c4019STakashi Iwai 	knew = snd_array_new(&spec->kctls);
392c577b8a1SJoseph Chan 	if (!knew)
393291c9e33STakashi Iwai 		return NULL;
394291c9e33STakashi Iwai 	*knew = *tmpl;
395291c9e33STakashi Iwai 	if (!name)
396291c9e33STakashi Iwai 		name = tmpl->name;
397291c9e33STakashi Iwai 	if (name) {
398c577b8a1SJoseph Chan 		knew->name = kstrdup(name, GFP_KERNEL);
399c577b8a1SJoseph Chan 		if (!knew->name)
400291c9e33STakashi Iwai 			return NULL;
401291c9e33STakashi Iwai 	}
402291c9e33STakashi Iwai 	return knew;
403291c9e33STakashi Iwai }
404291c9e33STakashi Iwai 
405291c9e33STakashi Iwai static int __via_add_control(struct via_spec *spec, int type, const char *name,
406291c9e33STakashi Iwai 			     int idx, unsigned long val)
407291c9e33STakashi Iwai {
408291c9e33STakashi Iwai 	struct snd_kcontrol_new *knew;
409291c9e33STakashi Iwai 
410291c9e33STakashi Iwai 	knew = __via_clone_ctl(spec, &via_control_templates[type], name);
411291c9e33STakashi Iwai 	if (!knew)
412c577b8a1SJoseph Chan 		return -ENOMEM;
413d7a99cceSTakashi Iwai 	knew->index = idx;
4144d02d1b6SJaroslav Kysela 	if (get_amp_nid_(val))
4155e26dfd0SJaroslav Kysela 		knew->subdevice = HDA_SUBDEV_AMP_FLAG;
416c577b8a1SJoseph Chan 	knew->private_value = val;
417c577b8a1SJoseph Chan 	return 0;
418c577b8a1SJoseph Chan }
419c577b8a1SJoseph Chan 
4207b315bb4STakashi Iwai #define via_add_control(spec, type, name, val) \
4217b315bb4STakashi Iwai 	__via_add_control(spec, type, name, 0, val)
4227b315bb4STakashi Iwai 
423291c9e33STakashi Iwai #define via_clone_control(spec, tmpl) __via_clone_ctl(spec, tmpl, NULL)
4245b0cb1d8SJaroslav Kysela 
425603c4019STakashi Iwai static void via_free_kctls(struct hda_codec *codec)
426603c4019STakashi Iwai {
427603c4019STakashi Iwai 	struct via_spec *spec = codec->spec;
428603c4019STakashi Iwai 
429603c4019STakashi Iwai 	if (spec->kctls.list) {
430603c4019STakashi Iwai 		struct snd_kcontrol_new *kctl = spec->kctls.list;
431603c4019STakashi Iwai 		int i;
432603c4019STakashi Iwai 		for (i = 0; i < spec->kctls.used; i++)
433603c4019STakashi Iwai 			kfree(kctl[i].name);
434603c4019STakashi Iwai 	}
435603c4019STakashi Iwai 	snd_array_free(&spec->kctls);
436603c4019STakashi Iwai }
437603c4019STakashi Iwai 
438c577b8a1SJoseph Chan /* create input playback/capture controls for the given pin */
4399510e8ddSLydia Wang static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
4407b315bb4STakashi Iwai 				int type_idx, int idx, int mix_nid)
441c577b8a1SJoseph Chan {
442c577b8a1SJoseph Chan 	char name[32];
443c577b8a1SJoseph Chan 	int err;
444c577b8a1SJoseph Chan 
445c577b8a1SJoseph Chan 	sprintf(name, "%s Playback Volume", ctlname);
4467b315bb4STakashi Iwai 	err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
447c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
448c577b8a1SJoseph Chan 	if (err < 0)
449c577b8a1SJoseph Chan 		return err;
450c577b8a1SJoseph Chan 	sprintf(name, "%s Playback Switch", ctlname);
4517b315bb4STakashi Iwai 	err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
452c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
453c577b8a1SJoseph Chan 	if (err < 0)
454c577b8a1SJoseph Chan 		return err;
455c577b8a1SJoseph Chan 	return 0;
456c577b8a1SJoseph Chan }
457c577b8a1SJoseph Chan 
4585d41762aSTakashi Iwai #define get_connection_index(codec, mux, nid) \
4598d087c76STakashi Iwai 	snd_hda_get_conn_index(codec, mux, nid, 0)
4605d41762aSTakashi Iwai 
4618df2a312STakashi Iwai static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
4628df2a312STakashi Iwai 			   unsigned int mask)
4638df2a312STakashi Iwai {
464a934d5a9STakashi Iwai 	unsigned int caps;
465a934d5a9STakashi Iwai 	if (!nid)
466a934d5a9STakashi Iwai 		return false;
467a934d5a9STakashi Iwai 	caps = get_wcaps(codec, nid);
4688df2a312STakashi Iwai 	if (dir == HDA_INPUT)
4698df2a312STakashi Iwai 		caps &= AC_WCAP_IN_AMP;
4708df2a312STakashi Iwai 	else
4718df2a312STakashi Iwai 		caps &= AC_WCAP_OUT_AMP;
4728df2a312STakashi Iwai 	if (!caps)
4738df2a312STakashi Iwai 		return false;
4748df2a312STakashi Iwai 	if (query_amp_caps(codec, nid, dir) & mask)
4758df2a312STakashi Iwai 		return true;
4768df2a312STakashi Iwai 	return false;
4778df2a312STakashi Iwai }
4788df2a312STakashi Iwai 
47909a9ad69STakashi Iwai #define have_mute(codec, nid, dir) \
48009a9ad69STakashi Iwai 	check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
4818df2a312STakashi Iwai 
482d69607b3SLydia Wang /* enable/disable the output-route mixers */
483d69607b3SLydia Wang static void activate_output_mix(struct hda_codec *codec, struct nid_path *path,
4843214b966STakashi Iwai 				hda_nid_t mix_nid, int idx, bool enable)
485d69607b3SLydia Wang {
486d69607b3SLydia Wang 	int i, num, val;
487d69607b3SLydia Wang 
488d69607b3SLydia Wang 	if (!path)
489d69607b3SLydia Wang 		return;
49009cf03b8STakashi Iwai 	num = snd_hda_get_num_conns(codec, mix_nid);
491d69607b3SLydia Wang 	for (i = 0; i < num; i++) {
4923214b966STakashi Iwai 		if (i == idx)
493d69607b3SLydia Wang 			val = AMP_IN_UNMUTE(i);
494d69607b3SLydia Wang 		else
495d69607b3SLydia Wang 			val = AMP_IN_MUTE(i);
496d69607b3SLydia Wang 		snd_hda_codec_write(codec, mix_nid, 0,
497d69607b3SLydia Wang 				    AC_VERB_SET_AMP_GAIN_MUTE, val);
498d69607b3SLydia Wang 	}
499d69607b3SLydia Wang }
500d69607b3SLydia Wang 
50109a9ad69STakashi Iwai /* enable/disable the output-route */
50209a9ad69STakashi Iwai static void activate_output_path(struct hda_codec *codec, struct nid_path *path,
50309a9ad69STakashi Iwai 				 bool enable, bool force)
5045d41762aSTakashi Iwai {
505d69607b3SLydia Wang 	struct via_spec *spec = codec->spec;
5063214b966STakashi Iwai 	int i;
50709a9ad69STakashi Iwai 	for (i = 0; i < path->depth; i++) {
50809a9ad69STakashi Iwai 		hda_nid_t src, dst;
50909a9ad69STakashi Iwai 		int idx = path->idx[i];
51009a9ad69STakashi Iwai 		src = path->path[i];
51109a9ad69STakashi Iwai 		if (i < path->depth - 1)
51209a9ad69STakashi Iwai 			dst = path->path[i + 1];
51309a9ad69STakashi Iwai 		else
51409a9ad69STakashi Iwai 			dst = 0;
51509a9ad69STakashi Iwai 		if (enable && path->multi[i])
51609a9ad69STakashi Iwai 			snd_hda_codec_write(codec, dst, 0,
5175d41762aSTakashi Iwai 					    AC_VERB_SET_CONNECT_SEL, idx);
5183214b966STakashi Iwai 		if (!force && (dst == spec->aa_mix_nid))
519e5e14681SLydia Wang 			continue;
5203214b966STakashi Iwai 		if (have_mute(codec, dst, HDA_INPUT))
5213214b966STakashi Iwai 			activate_output_mix(codec, path, dst, idx, enable);
52209a9ad69STakashi Iwai 		if (!force && (src == path->vol_ctl || src == path->mute_ctl))
52309a9ad69STakashi Iwai 			continue;
52409a9ad69STakashi Iwai 		if (have_mute(codec, src, HDA_OUTPUT)) {
52509a9ad69STakashi Iwai 			int val = enable ? AMP_OUT_UNMUTE : AMP_OUT_MUTE;
52609a9ad69STakashi Iwai 			snd_hda_codec_write(codec, src, 0,
52709a9ad69STakashi Iwai 					    AC_VERB_SET_AMP_GAIN_MUTE, val);
52809a9ad69STakashi Iwai 		}
52909a9ad69STakashi Iwai 	}
5305d41762aSTakashi Iwai }
5315d41762aSTakashi Iwai 
5325d41762aSTakashi Iwai /* set the given pin as output */
5335d41762aSTakashi Iwai static void init_output_pin(struct hda_codec *codec, hda_nid_t pin,
5345d41762aSTakashi Iwai 			    int pin_type)
5355d41762aSTakashi Iwai {
5365d41762aSTakashi Iwai 	if (!pin)
5375d41762aSTakashi Iwai 		return;
538cdd03cedSTakashi Iwai 	snd_hda_set_pin_ctl(codec, pin, pin_type);
5395d41762aSTakashi Iwai 	if (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD)
5405d41762aSTakashi Iwai 		snd_hda_codec_write(codec, pin, 0,
541d3a11e60STakashi Iwai 				    AC_VERB_SET_EAPD_BTLENABLE, 0x02);
542c577b8a1SJoseph Chan }
543c577b8a1SJoseph Chan 
54409a9ad69STakashi Iwai static void via_auto_init_output(struct hda_codec *codec,
545a353fbb1STakashi Iwai 				 struct nid_path *path, int pin_type)
5465d41762aSTakashi Iwai {
5475d41762aSTakashi Iwai 	unsigned int caps;
548d69607b3SLydia Wang 	hda_nid_t pin;
5495d41762aSTakashi Iwai 
55009a9ad69STakashi Iwai 	if (!path->depth)
5515d41762aSTakashi Iwai 		return;
55209a9ad69STakashi Iwai 	pin = path->path[path->depth - 1];
5535d41762aSTakashi Iwai 
5545d41762aSTakashi Iwai 	init_output_pin(codec, pin, pin_type);
55577e314f7STakashi Iwai 	if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
5565d41762aSTakashi Iwai 		caps = query_amp_caps(codec, pin, HDA_OUTPUT);
55777e314f7STakashi Iwai 	else
55877e314f7STakashi Iwai 		caps = 0;
5595d41762aSTakashi Iwai 	if (caps & AC_AMPCAP_MUTE) {
5605d41762aSTakashi Iwai 		unsigned int val;
5615d41762aSTakashi Iwai 		val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
5625d41762aSTakashi Iwai 		snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
5635d41762aSTakashi Iwai 				    AMP_OUT_MUTE | val);
5645d41762aSTakashi Iwai 	}
565a353fbb1STakashi Iwai 	activate_output_path(codec, path, true, true); /* force on */
56609a9ad69STakashi Iwai }
567c577b8a1SJoseph Chan 
568c577b8a1SJoseph Chan static void via_auto_init_multi_out(struct hda_codec *codec)
569c577b8a1SJoseph Chan {
570c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
5713214b966STakashi Iwai 	struct nid_path *path;
572c577b8a1SJoseph Chan 	int i;
573c577b8a1SJoseph Chan 
5743214b966STakashi Iwai 	for (i = 0; i < spec->autocfg.line_outs + spec->smart51_nums; i++) {
5753214b966STakashi Iwai 		path = &spec->out_path[i];
5763214b966STakashi Iwai 		if (!i && spec->aamix_mode && spec->out_mix_path.depth)
5773214b966STakashi Iwai 			path = &spec->out_mix_path;
578a353fbb1STakashi Iwai 		via_auto_init_output(codec, path, PIN_OUT);
5793214b966STakashi Iwai 	}
580c577b8a1SJoseph Chan }
581c577b8a1SJoseph Chan 
582020066d1STakashi Iwai /* deactivate the inactive headphone-paths */
583020066d1STakashi Iwai static void deactivate_hp_paths(struct hda_codec *codec)
584c577b8a1SJoseph Chan {
585c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
5863214b966STakashi Iwai 	int shared = spec->hp_indep_shared;
587c577b8a1SJoseph Chan 
58809a9ad69STakashi Iwai 	if (spec->hp_independent_mode) {
58909a9ad69STakashi Iwai 		activate_output_path(codec, &spec->hp_path, false, false);
5903214b966STakashi Iwai 		activate_output_path(codec, &spec->hp_mix_path, false, false);
5913214b966STakashi Iwai 		if (shared)
5923214b966STakashi Iwai 			activate_output_path(codec, &spec->out_path[shared],
5933214b966STakashi Iwai 					     false, false);
594020066d1STakashi Iwai 	} else if (spec->aamix_mode || !spec->hp_path.depth) {
595020066d1STakashi Iwai 		activate_output_path(codec, &spec->hp_indep_path, false, false);
5963214b966STakashi Iwai 		activate_output_path(codec, &spec->hp_path, false, false);
5973214b966STakashi Iwai 	} else {
598020066d1STakashi Iwai 		activate_output_path(codec, &spec->hp_indep_path, false, false);
5993214b966STakashi Iwai 		activate_output_path(codec, &spec->hp_mix_path, false, false);
60009a9ad69STakashi Iwai 	}
60125eaba2fSLydia Wang }
602c577b8a1SJoseph Chan 
603020066d1STakashi Iwai static void via_auto_init_hp_out(struct hda_codec *codec)
604020066d1STakashi Iwai {
605020066d1STakashi Iwai 	struct via_spec *spec = codec->spec;
606020066d1STakashi Iwai 
607020066d1STakashi Iwai 	if (!spec->hp_path.depth) {
608a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
609020066d1STakashi Iwai 		return;
610020066d1STakashi Iwai 	}
611020066d1STakashi Iwai 	deactivate_hp_paths(codec);
612020066d1STakashi Iwai 	if (spec->hp_independent_mode)
613a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->hp_indep_path, PIN_HP);
614020066d1STakashi Iwai 	else if (spec->aamix_mode)
615a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
616020066d1STakashi Iwai 	else
617a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->hp_path, PIN_HP);
618020066d1STakashi Iwai }
619020066d1STakashi Iwai 
6204a918ffeSTakashi Iwai static void via_auto_init_speaker_out(struct hda_codec *codec)
6214a918ffeSTakashi Iwai {
6224a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
6234a918ffeSTakashi Iwai 
6243214b966STakashi Iwai 	if (!spec->autocfg.speaker_outs)
6253214b966STakashi Iwai 		return;
6263214b966STakashi Iwai 	if (!spec->speaker_path.depth) {
627a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
6283214b966STakashi Iwai 		return;
6293214b966STakashi Iwai 	}
6303214b966STakashi Iwai 	if (!spec->aamix_mode) {
6313214b966STakashi Iwai 		activate_output_path(codec, &spec->speaker_mix_path,
6323214b966STakashi Iwai 				     false, false);
633a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->speaker_path, PIN_OUT);
6343214b966STakashi Iwai 	} else {
6353214b966STakashi Iwai 		activate_output_path(codec, &spec->speaker_path, false, false);
636a353fbb1STakashi Iwai 		via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
6373214b966STakashi Iwai 	}
6384a918ffeSTakashi Iwai }
6394a918ffeSTakashi Iwai 
640f4a7828bSTakashi Iwai static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin);
6416e969d91STakashi Iwai static void via_hp_automute(struct hda_codec *codec);
64232e0191dSClemens Ladisch 
643c577b8a1SJoseph Chan static void via_auto_init_analog_input(struct hda_codec *codec)
644c577b8a1SJoseph Chan {
645c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
6467b315bb4STakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
647096a8854STakashi Iwai 	hda_nid_t conn[HDA_MAX_CONNECTIONS];
64832e0191dSClemens Ladisch 	unsigned int ctl;
649096a8854STakashi Iwai 	int i, num_conns;
650c577b8a1SJoseph Chan 
651096a8854STakashi Iwai 	/* init ADCs */
652096a8854STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
65377e314f7STakashi Iwai 		hda_nid_t nid = spec->adc_nids[i];
65477e314f7STakashi Iwai 		if (!(get_wcaps(codec, nid) & AC_WCAP_IN_AMP) ||
65577e314f7STakashi Iwai 		    !(query_amp_caps(codec, nid, HDA_INPUT) & AC_AMPCAP_MUTE))
65677e314f7STakashi Iwai 			continue;
657096a8854STakashi Iwai 		snd_hda_codec_write(codec, spec->adc_nids[i], 0,
658096a8854STakashi Iwai 				    AC_VERB_SET_AMP_GAIN_MUTE,
659096a8854STakashi Iwai 				    AMP_IN_UNMUTE(0));
660096a8854STakashi Iwai 	}
661096a8854STakashi Iwai 
662096a8854STakashi Iwai 	/* init pins */
6637b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
6647b315bb4STakashi Iwai 		hda_nid_t nid = cfg->inputs[i].pin;
665f4a7828bSTakashi Iwai 		if (spec->smart51_enabled && is_smart51_pins(codec, nid))
66632e0191dSClemens Ladisch 			ctl = PIN_OUT;
6674740860bSTakashi Iwai 		else {
66832e0191dSClemens Ladisch 			ctl = PIN_IN;
6694740860bSTakashi Iwai 			if (cfg->inputs[i].type == AUTO_PIN_MIC)
6704740860bSTakashi Iwai 				ctl |= snd_hda_get_default_vref(codec, nid);
6714740860bSTakashi Iwai 		}
672cdd03cedSTakashi Iwai 		snd_hda_set_pin_ctl(codec, nid, ctl);
673c577b8a1SJoseph Chan 	}
674096a8854STakashi Iwai 
675096a8854STakashi Iwai 	/* init input-src */
676096a8854STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
677a86a88eaSTakashi Iwai 		int adc_idx = spec->inputs[spec->cur_mux[i]].adc_idx;
678fc1156c0STakashi Iwai 		/* secondary ADCs must have the unique MUX */
679fc1156c0STakashi Iwai 		if (i > 0 && !spec->mux_nids[i])
680fc1156c0STakashi Iwai 			break;
681a86a88eaSTakashi Iwai 		if (spec->mux_nids[adc_idx]) {
682a86a88eaSTakashi Iwai 			int mux_idx = spec->inputs[spec->cur_mux[i]].mux_idx;
683a86a88eaSTakashi Iwai 			snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
684096a8854STakashi Iwai 					    AC_VERB_SET_CONNECT_SEL,
685a86a88eaSTakashi Iwai 					    mux_idx);
686a86a88eaSTakashi Iwai 		}
687a86a88eaSTakashi Iwai 		if (spec->dyn_adc_switch)
688a86a88eaSTakashi Iwai 			break; /* only one input-src */
689096a8854STakashi Iwai 	}
690096a8854STakashi Iwai 
691096a8854STakashi Iwai 	/* init aa-mixer */
692096a8854STakashi Iwai 	if (!spec->aa_mix_nid)
693096a8854STakashi Iwai 		return;
694096a8854STakashi Iwai 	num_conns = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
695096a8854STakashi Iwai 					    ARRAY_SIZE(conn));
696096a8854STakashi Iwai 	for (i = 0; i < num_conns; i++) {
697096a8854STakashi Iwai 		unsigned int caps = get_wcaps(codec, conn[i]);
698096a8854STakashi Iwai 		if (get_wcaps_type(caps) == AC_WID_PIN)
699096a8854STakashi Iwai 			snd_hda_codec_write(codec, spec->aa_mix_nid, 0,
700096a8854STakashi Iwai 					    AC_VERB_SET_AMP_GAIN_MUTE,
701096a8854STakashi Iwai 					    AMP_IN_MUTE(i));
702096a8854STakashi Iwai 	}
703c577b8a1SJoseph Chan }
704f5271101SLydia Wang 
705054d867eSTakashi Iwai static void update_power_state(struct hda_codec *codec, hda_nid_t nid,
706054d867eSTakashi Iwai 			       unsigned int parm)
707054d867eSTakashi Iwai {
708054d867eSTakashi Iwai 	if (snd_hda_codec_read(codec, nid, 0,
709054d867eSTakashi Iwai 			       AC_VERB_GET_POWER_STATE, 0) == parm)
710054d867eSTakashi Iwai 		return;
711054d867eSTakashi Iwai 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
712054d867eSTakashi Iwai }
713054d867eSTakashi Iwai 
714f5271101SLydia Wang static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
715f5271101SLydia Wang 				unsigned int *affected_parm)
716f5271101SLydia Wang {
717f5271101SLydia Wang 	unsigned parm;
718f5271101SLydia Wang 	unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
719f5271101SLydia Wang 	unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
720f5271101SLydia Wang 		>> AC_DEFCFG_MISC_SHIFT
721f5271101SLydia Wang 		& AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
7221564b287SLydia Wang 	struct via_spec *spec = codec->spec;
72324088a58STakashi Iwai 	unsigned present = 0;
72424088a58STakashi Iwai 
72524088a58STakashi Iwai 	no_presence |= spec->no_pin_power_ctl;
72624088a58STakashi Iwai 	if (!no_presence)
72724088a58STakashi Iwai 		present = snd_hda_jack_detect(codec, nid);
728f4a7828bSTakashi Iwai 	if ((spec->smart51_enabled && is_smart51_pins(codec, nid))
7291564b287SLydia Wang 	    || ((no_presence || present)
7301564b287SLydia Wang 		&& get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
731f5271101SLydia Wang 		*affected_parm = AC_PWRST_D0; /* if it's connected */
732f5271101SLydia Wang 		parm = AC_PWRST_D0;
733f5271101SLydia Wang 	} else
734f5271101SLydia Wang 		parm = AC_PWRST_D3;
735f5271101SLydia Wang 
736054d867eSTakashi Iwai 	update_power_state(codec, nid, parm);
737f5271101SLydia Wang }
738f5271101SLydia Wang 
73924088a58STakashi Iwai static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
74024088a58STakashi Iwai 				  struct snd_ctl_elem_info *uinfo)
74124088a58STakashi Iwai {
74224088a58STakashi Iwai 	static const char * const texts[] = {
74324088a58STakashi Iwai 		"Disabled", "Enabled"
74424088a58STakashi Iwai 	};
74524088a58STakashi Iwai 
74624088a58STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
74724088a58STakashi Iwai 	uinfo->count = 1;
74824088a58STakashi Iwai 	uinfo->value.enumerated.items = 2;
74924088a58STakashi Iwai 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
75024088a58STakashi Iwai 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
75124088a58STakashi Iwai 	strcpy(uinfo->value.enumerated.name,
75224088a58STakashi Iwai 	       texts[uinfo->value.enumerated.item]);
75324088a58STakashi Iwai 	return 0;
75424088a58STakashi Iwai }
75524088a58STakashi Iwai 
75624088a58STakashi Iwai static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
75724088a58STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
75824088a58STakashi Iwai {
75924088a58STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
76024088a58STakashi Iwai 	struct via_spec *spec = codec->spec;
76124088a58STakashi Iwai 	ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
76224088a58STakashi Iwai 	return 0;
76324088a58STakashi Iwai }
76424088a58STakashi Iwai 
76524088a58STakashi Iwai static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
76624088a58STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
76724088a58STakashi Iwai {
76824088a58STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
76924088a58STakashi Iwai 	struct via_spec *spec = codec->spec;
77024088a58STakashi Iwai 	unsigned int val = !ucontrol->value.enumerated.item[0];
77124088a58STakashi Iwai 
77224088a58STakashi Iwai 	if (val == spec->no_pin_power_ctl)
77324088a58STakashi Iwai 		return 0;
77424088a58STakashi Iwai 	spec->no_pin_power_ctl = val;
77524088a58STakashi Iwai 	set_widgets_power_state(codec);
776e9d010c2STakashi Iwai 	analog_low_current_mode(codec);
77724088a58STakashi Iwai 	return 1;
77824088a58STakashi Iwai }
77924088a58STakashi Iwai 
78024088a58STakashi Iwai static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
78124088a58STakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
78224088a58STakashi Iwai 	.name = "Dynamic Power-Control",
78324088a58STakashi Iwai 	.info = via_pin_power_ctl_info,
78424088a58STakashi Iwai 	.get = via_pin_power_ctl_get,
78524088a58STakashi Iwai 	.put = via_pin_power_ctl_put,
78624088a58STakashi Iwai };
78724088a58STakashi Iwai 
78824088a58STakashi Iwai 
7890aa62aefSHarald Welte static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
7900aa62aefSHarald Welte 				   struct snd_ctl_elem_info *uinfo)
7910aa62aefSHarald Welte {
7928df2a312STakashi Iwai 	static const char * const texts[] = { "OFF", "ON" };
7938df2a312STakashi Iwai 
7948df2a312STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
7958df2a312STakashi Iwai 	uinfo->count = 1;
7968df2a312STakashi Iwai 	uinfo->value.enumerated.items = 2;
7978df2a312STakashi Iwai 	if (uinfo->value.enumerated.item >= 2)
7988df2a312STakashi Iwai 		uinfo->value.enumerated.item = 1;
7998df2a312STakashi Iwai 	strcpy(uinfo->value.enumerated.name,
8008df2a312STakashi Iwai 	       texts[uinfo->value.enumerated.item]);
8018df2a312STakashi Iwai 	return 0;
8020aa62aefSHarald Welte }
8030aa62aefSHarald Welte 
8040aa62aefSHarald Welte static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
8050aa62aefSHarald Welte 				  struct snd_ctl_elem_value *ucontrol)
8060aa62aefSHarald Welte {
8070aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
808cdc1784dSLydia Wang 	struct via_spec *spec = codec->spec;
809cdc1784dSLydia Wang 
810ece8d043STakashi Iwai 	ucontrol->value.enumerated.item[0] = spec->hp_independent_mode;
811cdc1784dSLydia Wang 	return 0;
812cdc1784dSLydia Wang }
813cdc1784dSLydia Wang 
8143b607e3dSTakashi Iwai /* adjust spec->multiout setup according to the current flags */
8153b607e3dSTakashi Iwai static void setup_playback_multi_pcm(struct via_spec *spec)
8163b607e3dSTakashi Iwai {
8173b607e3dSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
8183b607e3dSTakashi Iwai 	spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
8193b607e3dSTakashi Iwai 	spec->multiout.hp_nid = 0;
8203b607e3dSTakashi Iwai 	if (!spec->hp_independent_mode) {
8213b607e3dSTakashi Iwai 		if (!spec->hp_indep_shared)
8223b607e3dSTakashi Iwai 			spec->multiout.hp_nid = spec->hp_dac_nid;
8233b607e3dSTakashi Iwai 	} else {
8243b607e3dSTakashi Iwai 		if (spec->hp_indep_shared)
8253b607e3dSTakashi Iwai 			spec->multiout.num_dacs = cfg->line_outs - 1;
8263b607e3dSTakashi Iwai 	}
8273b607e3dSTakashi Iwai }
8283b607e3dSTakashi Iwai 
8293b607e3dSTakashi Iwai /* update DAC setups according to indep-HP switch;
8303b607e3dSTakashi Iwai  * this function is called only when indep-HP is modified
8313b607e3dSTakashi Iwai  */
8323b607e3dSTakashi Iwai static void switch_indep_hp_dacs(struct hda_codec *codec)
8333b607e3dSTakashi Iwai {
8343b607e3dSTakashi Iwai 	struct via_spec *spec = codec->spec;
8353b607e3dSTakashi Iwai 	int shared = spec->hp_indep_shared;
8363b607e3dSTakashi Iwai 	hda_nid_t shared_dac, hp_dac;
8373b607e3dSTakashi Iwai 
8383b607e3dSTakashi Iwai 	if (!spec->opened_streams)
8393b607e3dSTakashi Iwai 		return;
8403b607e3dSTakashi Iwai 
8413b607e3dSTakashi Iwai 	shared_dac = shared ? spec->multiout.dac_nids[shared] : 0;
8423b607e3dSTakashi Iwai 	hp_dac = spec->hp_dac_nid;
8433b607e3dSTakashi Iwai 	if (spec->hp_independent_mode) {
8443b607e3dSTakashi Iwai 		/* switch to indep-HP mode */
8453b607e3dSTakashi Iwai 		if (spec->active_streams & STREAM_MULTI_OUT) {
8463b607e3dSTakashi Iwai 			__snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
8473b607e3dSTakashi Iwai 			__snd_hda_codec_cleanup_stream(codec, shared_dac, 1);
8483b607e3dSTakashi Iwai 		}
8493b607e3dSTakashi Iwai 		if (spec->active_streams & STREAM_INDEP_HP)
8503b607e3dSTakashi Iwai 			snd_hda_codec_setup_stream(codec, hp_dac,
8513b607e3dSTakashi Iwai 						   spec->cur_hp_stream_tag, 0,
8523b607e3dSTakashi Iwai 						   spec->cur_hp_format);
8533b607e3dSTakashi Iwai 	} else {
8543b607e3dSTakashi Iwai 		/* back to HP or shared-DAC */
8553b607e3dSTakashi Iwai 		if (spec->active_streams & STREAM_INDEP_HP)
8563b607e3dSTakashi Iwai 			__snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
8573b607e3dSTakashi Iwai 		if (spec->active_streams & STREAM_MULTI_OUT) {
8583b607e3dSTakashi Iwai 			hda_nid_t dac;
8593b607e3dSTakashi Iwai 			int ch;
8603b607e3dSTakashi Iwai 			if (shared_dac) { /* reset mutli-ch DAC */
8613b607e3dSTakashi Iwai 				dac = shared_dac;
8623b607e3dSTakashi Iwai 				ch = shared * 2;
8633b607e3dSTakashi Iwai 			} else { /* reset HP DAC */
8643b607e3dSTakashi Iwai 				dac = hp_dac;
8653b607e3dSTakashi Iwai 				ch = 0;
8663b607e3dSTakashi Iwai 			}
8673b607e3dSTakashi Iwai 			snd_hda_codec_setup_stream(codec, dac,
8683b607e3dSTakashi Iwai 						   spec->cur_dac_stream_tag, ch,
8693b607e3dSTakashi Iwai 						   spec->cur_dac_format);
8703b607e3dSTakashi Iwai 		}
8713b607e3dSTakashi Iwai 	}
8723b607e3dSTakashi Iwai 	setup_playback_multi_pcm(spec);
8733b607e3dSTakashi Iwai }
8743b607e3dSTakashi Iwai 
8750aa62aefSHarald Welte static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
8760aa62aefSHarald Welte 				  struct snd_ctl_elem_value *ucontrol)
8770aa62aefSHarald Welte {
8780aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
8790aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
8803214b966STakashi Iwai 	int cur, shared;
8818df2a312STakashi Iwai 
8823b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
88325250505STakashi Iwai 	cur = !!ucontrol->value.enumerated.item[0];
8843b607e3dSTakashi Iwai 	if (spec->hp_independent_mode == cur) {
8853b607e3dSTakashi Iwai 		mutex_unlock(&spec->config_mutex);
88625250505STakashi Iwai 		return 0;
8873b607e3dSTakashi Iwai 	}
88825250505STakashi Iwai 	spec->hp_independent_mode = cur;
8893214b966STakashi Iwai 	shared = spec->hp_indep_shared;
890020066d1STakashi Iwai 	deactivate_hp_paths(codec);
891020066d1STakashi Iwai 	if (cur)
892020066d1STakashi Iwai 		activate_output_path(codec, &spec->hp_indep_path, true, false);
893020066d1STakashi Iwai 	else {
8943214b966STakashi Iwai 		if (shared)
8953214b966STakashi Iwai 			activate_output_path(codec, &spec->out_path[shared],
89625250505STakashi Iwai 					     true, false);
897020066d1STakashi Iwai 		if (spec->aamix_mode || !spec->hp_path.depth)
898020066d1STakashi Iwai 			activate_output_path(codec, &spec->hp_mix_path,
899020066d1STakashi Iwai 					     true, false);
900020066d1STakashi Iwai 		else
901020066d1STakashi Iwai 			activate_output_path(codec, &spec->hp_path,
902020066d1STakashi Iwai 					     true, false);
9038df2a312STakashi Iwai 	}
9040aa62aefSHarald Welte 
9053b607e3dSTakashi Iwai 	switch_indep_hp_dacs(codec);
9063b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
9073b607e3dSTakashi Iwai 
908ce0e5a9eSLydia Wang 	/* update jack power state */
9093e95b9abSLydia Wang 	set_widgets_power_state(codec);
9106e969d91STakashi Iwai 	via_hp_automute(codec);
91125250505STakashi Iwai 	return 1;
9120aa62aefSHarald Welte }
9130aa62aefSHarald Welte 
914ece8d043STakashi Iwai static const struct snd_kcontrol_new via_hp_mixer = {
9150aa62aefSHarald Welte 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
9160aa62aefSHarald Welte 	.name = "Independent HP",
9170aa62aefSHarald Welte 	.info = via_independent_hp_info,
9180aa62aefSHarald Welte 	.get = via_independent_hp_get,
9190aa62aefSHarald Welte 	.put = via_independent_hp_put,
9200aa62aefSHarald Welte };
9210aa62aefSHarald Welte 
9223d83e577STakashi Iwai static int via_hp_build(struct hda_codec *codec)
9235b0cb1d8SJaroslav Kysela {
9243d83e577STakashi Iwai 	struct via_spec *spec = codec->spec;
9255b0cb1d8SJaroslav Kysela 	struct snd_kcontrol_new *knew;
9265b0cb1d8SJaroslav Kysela 	hda_nid_t nid;
9275b0cb1d8SJaroslav Kysela 
9285b0cb1d8SJaroslav Kysela 	nid = spec->autocfg.hp_pins[0];
929ece8d043STakashi Iwai 	knew = via_clone_control(spec, &via_hp_mixer);
9303d83e577STakashi Iwai 	if (knew == NULL)
9313d83e577STakashi Iwai 		return -ENOMEM;
9323d83e577STakashi Iwai 
9335b0cb1d8SJaroslav Kysela 	knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
9345b0cb1d8SJaroslav Kysela 
9355b0cb1d8SJaroslav Kysela 	return 0;
9365b0cb1d8SJaroslav Kysela }
9375b0cb1d8SJaroslav Kysela 
9381564b287SLydia Wang static void notify_aa_path_ctls(struct hda_codec *codec)
9391564b287SLydia Wang {
940e3d7a143STakashi Iwai 	struct via_spec *spec = codec->spec;
9411564b287SLydia Wang 	int i;
9421564b287SLydia Wang 
943e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
944e3d7a143STakashi Iwai 		struct snd_kcontrol *ctl;
945e3d7a143STakashi Iwai 		struct snd_ctl_elem_id id;
9461564b287SLydia Wang 		memset(&id, 0, sizeof(id));
9471564b287SLydia Wang 		id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
948e3d7a143STakashi Iwai 		sprintf(id.name, "%s Playback Volume", spec->smart51_labels[i]);
949525566cbSLydia Wang 		ctl = snd_hda_find_mixer_ctl(codec, id.name);
950525566cbSLydia Wang 		if (ctl)
951525566cbSLydia Wang 			snd_ctl_notify(codec->bus->card,
952525566cbSLydia Wang 					SNDRV_CTL_EVENT_MASK_VALUE,
953525566cbSLydia Wang 					&ctl->id);
9541564b287SLydia Wang 	}
9551564b287SLydia Wang }
9561564b287SLydia Wang 
9571564b287SLydia Wang static void mute_aa_path(struct hda_codec *codec, int mute)
9581564b287SLydia Wang {
9591564b287SLydia Wang 	struct via_spec *spec = codec->spec;
9601564b287SLydia Wang 	int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
961e3d7a143STakashi Iwai 	int i;
962e3d7a143STakashi Iwai 
963e3d7a143STakashi Iwai 	/* check AA path's mute status */
964e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
965e3d7a143STakashi Iwai 		if (spec->smart51_idxs[i] < 0)
966e3d7a143STakashi Iwai 			continue;
967e3d7a143STakashi Iwai 		snd_hda_codec_amp_stereo(codec, spec->aa_mix_nid,
968e3d7a143STakashi Iwai 					 HDA_INPUT, spec->smart51_idxs[i],
9691564b287SLydia Wang 					 HDA_AMP_MUTE, val);
9701564b287SLydia Wang 	}
9711564b287SLydia Wang }
972f4a7828bSTakashi Iwai 
973e3d7a143STakashi Iwai static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
974e3d7a143STakashi Iwai {
975e3d7a143STakashi Iwai 	struct via_spec *spec = codec->spec;
976e3d7a143STakashi Iwai 	int i;
977e3d7a143STakashi Iwai 
978e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++)
979e3d7a143STakashi Iwai 		if (spec->smart51_pins[i] == pin)
980e3d7a143STakashi Iwai 			return true;
981e3d7a143STakashi Iwai 	return false;
982e3d7a143STakashi Iwai }
983e3d7a143STakashi Iwai 
9841564b287SLydia Wang static int via_smart51_get(struct snd_kcontrol *kcontrol,
9851564b287SLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
9861564b287SLydia Wang {
9871564b287SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
9881564b287SLydia Wang 	struct via_spec *spec = codec->spec;
9891564b287SLydia Wang 
990f2b1c9f0STakashi Iwai 	*ucontrol->value.integer.value = spec->smart51_enabled;
9911564b287SLydia Wang 	return 0;
9921564b287SLydia Wang }
9931564b287SLydia Wang 
9941564b287SLydia Wang static int via_smart51_put(struct snd_kcontrol *kcontrol,
9951564b287SLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
9961564b287SLydia Wang {
9971564b287SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
9981564b287SLydia Wang 	struct via_spec *spec = codec->spec;
9991564b287SLydia Wang 	int out_in = *ucontrol->value.integer.value
10001564b287SLydia Wang 		? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
10011564b287SLydia Wang 	int i;
10021564b287SLydia Wang 
1003e3d7a143STakashi Iwai 	for (i = 0; i < spec->smart51_nums; i++) {
1004e3d7a143STakashi Iwai 		hda_nid_t nid = spec->smart51_pins[i];
10057b315bb4STakashi Iwai 		unsigned int parm;
10067b315bb4STakashi Iwai 
10077b315bb4STakashi Iwai 		parm = snd_hda_codec_read(codec, nid, 0,
10081564b287SLydia Wang 					  AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
10091564b287SLydia Wang 		parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
10101564b287SLydia Wang 		parm |= out_in;
1011cdd03cedSTakashi Iwai 		snd_hda_set_pin_ctl(codec, nid, parm);
10121564b287SLydia Wang 		if (out_in == AC_PINCTL_OUT_EN) {
10131564b287SLydia Wang 			mute_aa_path(codec, 1);
10141564b287SLydia Wang 			notify_aa_path_ctls(codec);
10151564b287SLydia Wang 		}
10161564b287SLydia Wang 	}
10171564b287SLydia Wang 	spec->smart51_enabled = *ucontrol->value.integer.value;
10183e95b9abSLydia Wang 	set_widgets_power_state(codec);
10191564b287SLydia Wang 	return 1;
10201564b287SLydia Wang }
10211564b287SLydia Wang 
10225f4b36d6STakashi Iwai static const struct snd_kcontrol_new via_smart51_mixer = {
10231564b287SLydia Wang 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
10241564b287SLydia Wang 	.name = "Smart 5.1",
10251564b287SLydia Wang 	.count = 1,
1026f2b1c9f0STakashi Iwai 	.info = snd_ctl_boolean_mono_info,
10271564b287SLydia Wang 	.get = via_smart51_get,
10281564b287SLydia Wang 	.put = via_smart51_put,
10291564b287SLydia Wang };
10301564b287SLydia Wang 
1031f4a7828bSTakashi Iwai static int via_smart51_build(struct hda_codec *codec)
10325b0cb1d8SJaroslav Kysela {
1033f4a7828bSTakashi Iwai 	struct via_spec *spec = codec->spec;
10345b0cb1d8SJaroslav Kysela 
1035e3d7a143STakashi Iwai 	if (!spec->smart51_nums)
1036cb34c207SLydia Wang 		return 0;
1037e3d7a143STakashi Iwai 	if (!via_clone_control(spec, &via_smart51_mixer))
10385b0cb1d8SJaroslav Kysela 		return -ENOMEM;
10395b0cb1d8SJaroslav Kysela 	return 0;
10405b0cb1d8SJaroslav Kysela }
10415b0cb1d8SJaroslav Kysela 
1042f5271101SLydia Wang /* check AA path's mute status */
1043ada509ecSTakashi Iwai static bool is_aa_path_mute(struct hda_codec *codec)
1044ada509ecSTakashi Iwai {
1045ada509ecSTakashi Iwai 	struct via_spec *spec = codec->spec;
1046ada509ecSTakashi Iwai 	const struct hda_amp_list *p;
1047ada509ecSTakashi Iwai 	int i, ch, v;
1048ada509ecSTakashi Iwai 
1049ada509ecSTakashi Iwai 	for (i = 0; i < spec->num_loopbacks; i++) {
1050ada509ecSTakashi Iwai 		p = &spec->loopback_list[i];
1051ada509ecSTakashi Iwai 		for (ch = 0; ch < 2; ch++) {
1052ada509ecSTakashi Iwai 			v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
1053ada509ecSTakashi Iwai 						   p->idx);
1054ada509ecSTakashi Iwai 			if (!(v & HDA_AMP_MUTE) && v > 0)
1055ada509ecSTakashi Iwai 				return false;
1056f5271101SLydia Wang 		}
1057f5271101SLydia Wang 	}
1058ada509ecSTakashi Iwai 	return true;
1059f5271101SLydia Wang }
1060f5271101SLydia Wang 
1061f5271101SLydia Wang /* enter/exit analog low-current mode */
1062e9d010c2STakashi Iwai static void __analog_low_current_mode(struct hda_codec *codec, bool force)
1063f5271101SLydia Wang {
1064f5271101SLydia Wang 	struct via_spec *spec = codec->spec;
1065ada509ecSTakashi Iwai 	bool enable;
1066ada509ecSTakashi Iwai 	unsigned int verb, parm;
1067f5271101SLydia Wang 
1068e9d010c2STakashi Iwai 	if (spec->no_pin_power_ctl)
1069e9d010c2STakashi Iwai 		enable = false;
1070e9d010c2STakashi Iwai 	else
107192433923STakashi Iwai 		enable = is_aa_path_mute(codec) && !spec->opened_streams;
1072e9d010c2STakashi Iwai 	if (enable == spec->alc_mode && !force)
1073e9d010c2STakashi Iwai 		return;
1074e9d010c2STakashi Iwai 	spec->alc_mode = enable;
1075f5271101SLydia Wang 
1076f5271101SLydia Wang 	/* decide low current mode's verb & parameter */
1077f5271101SLydia Wang 	switch (spec->codec_type) {
1078f5271101SLydia Wang 	case VT1708B_8CH:
1079f5271101SLydia Wang 	case VT1708B_4CH:
1080f5271101SLydia Wang 		verb = 0xf70;
1081f5271101SLydia Wang 		parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
1082f5271101SLydia Wang 		break;
1083f5271101SLydia Wang 	case VT1708S:
1084eb7188caSLydia Wang 	case VT1718S:
1085f3db423dSLydia Wang 	case VT1716S:
1086f5271101SLydia Wang 		verb = 0xf73;
1087f5271101SLydia Wang 		parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
1088f5271101SLydia Wang 		break;
1089f5271101SLydia Wang 	case VT1702:
1090f5271101SLydia Wang 		verb = 0xf73;
1091f5271101SLydia Wang 		parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
1092f5271101SLydia Wang 		break;
109325eaba2fSLydia Wang 	case VT2002P:
1094ab6734e7SLydia Wang 	case VT1812:
109511890956SLydia Wang 	case VT1802:
109625eaba2fSLydia Wang 		verb = 0xf93;
109725eaba2fSLydia Wang 		parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
109825eaba2fSLydia Wang 		break;
1099f5271101SLydia Wang 	default:
1100f5271101SLydia Wang 		return;		/* other codecs are not supported */
1101f5271101SLydia Wang 	}
1102f5271101SLydia Wang 	/* send verb */
1103f5271101SLydia Wang 	snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
1104f5271101SLydia Wang }
1105f5271101SLydia Wang 
1106e9d010c2STakashi Iwai static void analog_low_current_mode(struct hda_codec *codec)
1107e9d010c2STakashi Iwai {
1108e9d010c2STakashi Iwai 	return __analog_low_current_mode(codec, false);
1109e9d010c2STakashi Iwai }
1110e9d010c2STakashi Iwai 
1111c577b8a1SJoseph Chan /*
1112c577b8a1SJoseph Chan  * generic initialization of ADC, input mixers and output mixers
1113c577b8a1SJoseph Chan  */
1114096a8854STakashi Iwai static const struct hda_verb vt1708_init_verbs[] = {
1115aa266fccSLydia Wang 	/* power down jack detect function */
1116aa266fccSLydia Wang 	{0x1, 0xf81, 0x1},
1117f7278fd0SJosepch Chan 	{ }
1118c577b8a1SJoseph Chan };
1119c577b8a1SJoseph Chan 
11203b607e3dSTakashi Iwai static void set_stream_open(struct hda_codec *codec, int bit, bool active)
11217eb56e84STakashi Iwai {
1122ada509ecSTakashi Iwai 	struct via_spec *spec = codec->spec;
1123ada509ecSTakashi Iwai 
1124ada509ecSTakashi Iwai 	if (active)
11253b607e3dSTakashi Iwai 		spec->opened_streams |= bit;
1126ada509ecSTakashi Iwai 	else
11273b607e3dSTakashi Iwai 		spec->opened_streams &= ~bit;
1128ada509ecSTakashi Iwai 	analog_low_current_mode(codec);
11297eb56e84STakashi Iwai }
11307eb56e84STakashi Iwai 
1131ece8d043STakashi Iwai static int via_playback_multi_pcm_open(struct hda_pcm_stream *hinfo,
1132c577b8a1SJoseph Chan 				 struct hda_codec *codec,
1133c577b8a1SJoseph Chan 				 struct snd_pcm_substream *substream)
1134c577b8a1SJoseph Chan {
1135c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
113625250505STakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
1137ada509ecSTakashi Iwai 	int err;
1138ece8d043STakashi Iwai 
113925250505STakashi Iwai 	spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
114025250505STakashi Iwai 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
11413b607e3dSTakashi Iwai 	set_stream_open(codec, STREAM_MULTI_OUT, true);
1142ada509ecSTakashi Iwai 	err = snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
11439a08160bSTakashi Iwai 					    hinfo);
1144ada509ecSTakashi Iwai 	if (err < 0) {
11453b607e3dSTakashi Iwai 		set_stream_open(codec, STREAM_MULTI_OUT, false);
1146ada509ecSTakashi Iwai 		return err;
1147ada509ecSTakashi Iwai 	}
1148ada509ecSTakashi Iwai 	return 0;
1149c577b8a1SJoseph Chan }
1150c577b8a1SJoseph Chan 
1151ece8d043STakashi Iwai static int via_playback_multi_pcm_close(struct hda_pcm_stream *hinfo,
11529af74210STakashi Iwai 				  struct hda_codec *codec,
11539af74210STakashi Iwai 				  struct snd_pcm_substream *substream)
11549af74210STakashi Iwai {
11553b607e3dSTakashi Iwai 	set_stream_open(codec, STREAM_MULTI_OUT, false);
11569af74210STakashi Iwai 	return 0;
11579af74210STakashi Iwai }
11589af74210STakashi Iwai 
11597eb56e84STakashi Iwai static int via_playback_hp_pcm_open(struct hda_pcm_stream *hinfo,
11607eb56e84STakashi Iwai 				    struct hda_codec *codec,
11617eb56e84STakashi Iwai 				    struct snd_pcm_substream *substream)
11627eb56e84STakashi Iwai {
11637eb56e84STakashi Iwai 	struct via_spec *spec = codec->spec;
11647eb56e84STakashi Iwai 
1165ece8d043STakashi Iwai 	if (snd_BUG_ON(!spec->hp_dac_nid))
11667eb56e84STakashi Iwai 		return -EINVAL;
11673b607e3dSTakashi Iwai 	set_stream_open(codec, STREAM_INDEP_HP, true);
1168ece8d043STakashi Iwai 	return 0;
1169ece8d043STakashi Iwai }
1170ece8d043STakashi Iwai 
1171ece8d043STakashi Iwai static int via_playback_hp_pcm_close(struct hda_pcm_stream *hinfo,
1172ece8d043STakashi Iwai 				     struct hda_codec *codec,
1173ece8d043STakashi Iwai 				     struct snd_pcm_substream *substream)
1174ece8d043STakashi Iwai {
11753b607e3dSTakashi Iwai 	set_stream_open(codec, STREAM_INDEP_HP, false);
11767eb56e84STakashi Iwai 	return 0;
11777eb56e84STakashi Iwai }
11787eb56e84STakashi Iwai 
11797eb56e84STakashi Iwai static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
11807eb56e84STakashi Iwai 					  struct hda_codec *codec,
11810aa62aefSHarald Welte 					  unsigned int stream_tag,
11820aa62aefSHarald Welte 					  unsigned int format,
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);
11883b607e3dSTakashi Iwai 	setup_playback_multi_pcm(spec);
1189ece8d043STakashi Iwai 	snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
1190ece8d043STakashi Iwai 					 format, substream);
11913b607e3dSTakashi Iwai 	/* remember for dynamic DAC switch with indep-HP */
11923b607e3dSTakashi Iwai 	spec->active_streams |= STREAM_MULTI_OUT;
11933b607e3dSTakashi Iwai 	spec->cur_dac_stream_tag = stream_tag;
11943b607e3dSTakashi Iwai 	spec->cur_dac_format = format;
11953b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1196187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
11977eb56e84STakashi Iwai 	return 0;
11980aa62aefSHarald Welte }
11990aa62aefSHarald Welte 
12007eb56e84STakashi Iwai static int via_playback_hp_pcm_prepare(struct hda_pcm_stream *hinfo,
12010aa62aefSHarald Welte 				       struct hda_codec *codec,
12020aa62aefSHarald Welte 				       unsigned int stream_tag,
12030aa62aefSHarald Welte 				       unsigned int format,
12040aa62aefSHarald Welte 				       struct snd_pcm_substream *substream)
12050aa62aefSHarald Welte {
12060aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
12070aa62aefSHarald Welte 
12083b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
12093b607e3dSTakashi Iwai 	if (spec->hp_independent_mode)
1210ece8d043STakashi Iwai 		snd_hda_codec_setup_stream(codec, spec->hp_dac_nid,
1211ece8d043STakashi Iwai 					   stream_tag, 0, format);
12123b607e3dSTakashi Iwai 	spec->active_streams |= STREAM_INDEP_HP;
12133b607e3dSTakashi Iwai 	spec->cur_hp_stream_tag = stream_tag;
12143b607e3dSTakashi Iwai 	spec->cur_hp_format = format;
12153b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1216187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
12170aa62aefSHarald Welte 	return 0;
12180aa62aefSHarald Welte }
12190aa62aefSHarald Welte 
12200aa62aefSHarald Welte static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
12210aa62aefSHarald Welte 				    struct hda_codec *codec,
12220aa62aefSHarald Welte 				    struct snd_pcm_substream *substream)
12230aa62aefSHarald Welte {
12240aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
12250aa62aefSHarald Welte 
12263b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
1227ece8d043STakashi Iwai 	snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
12283b607e3dSTakashi Iwai 	spec->active_streams &= ~STREAM_MULTI_OUT;
12293b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1230187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
12317eb56e84STakashi Iwai 	return 0;
12320aa62aefSHarald Welte }
12337eb56e84STakashi Iwai 
12347eb56e84STakashi Iwai static int via_playback_hp_pcm_cleanup(struct hda_pcm_stream *hinfo,
12357eb56e84STakashi Iwai 				       struct hda_codec *codec,
12367eb56e84STakashi Iwai 				       struct snd_pcm_substream *substream)
12377eb56e84STakashi Iwai {
12387eb56e84STakashi Iwai 	struct via_spec *spec = codec->spec;
12397eb56e84STakashi Iwai 
12403b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
12413b607e3dSTakashi Iwai 	if (spec->hp_independent_mode)
1242ece8d043STakashi Iwai 		snd_hda_codec_setup_stream(codec, spec->hp_dac_nid, 0, 0, 0);
12433b607e3dSTakashi Iwai 	spec->active_streams &= ~STREAM_INDEP_HP;
12443b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1245187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
12460aa62aefSHarald Welte 	return 0;
12470aa62aefSHarald Welte }
12480aa62aefSHarald Welte 
1249c577b8a1SJoseph Chan /*
1250c577b8a1SJoseph Chan  * Digital out
1251c577b8a1SJoseph Chan  */
1252c577b8a1SJoseph Chan static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1253c577b8a1SJoseph Chan 				     struct hda_codec *codec,
1254c577b8a1SJoseph Chan 				     struct snd_pcm_substream *substream)
1255c577b8a1SJoseph Chan {
1256c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1257c577b8a1SJoseph Chan 	return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1258c577b8a1SJoseph Chan }
1259c577b8a1SJoseph Chan 
1260c577b8a1SJoseph Chan static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1261c577b8a1SJoseph Chan 				      struct hda_codec *codec,
1262c577b8a1SJoseph Chan 				      struct snd_pcm_substream *substream)
1263c577b8a1SJoseph Chan {
1264c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1265c577b8a1SJoseph Chan 	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1266c577b8a1SJoseph Chan }
1267c577b8a1SJoseph Chan 
12685691ec7fSHarald Welte static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
126998aa34c0SHarald Welte 					struct hda_codec *codec,
127098aa34c0SHarald Welte 					unsigned int stream_tag,
127198aa34c0SHarald Welte 					unsigned int format,
127298aa34c0SHarald Welte 					struct snd_pcm_substream *substream)
127398aa34c0SHarald Welte {
127498aa34c0SHarald Welte 	struct via_spec *spec = codec->spec;
12759da29271STakashi Iwai 	return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
12769da29271STakashi Iwai 					     stream_tag, format, substream);
12779da29271STakashi Iwai }
12785691ec7fSHarald Welte 
12799da29271STakashi Iwai static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
12809da29271STakashi Iwai 					struct hda_codec *codec,
12819da29271STakashi Iwai 					struct snd_pcm_substream *substream)
12829da29271STakashi Iwai {
12839da29271STakashi Iwai 	struct via_spec *spec = codec->spec;
12849da29271STakashi Iwai 	snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
128598aa34c0SHarald Welte 	return 0;
128698aa34c0SHarald Welte }
128798aa34c0SHarald Welte 
1288c577b8a1SJoseph Chan /*
1289c577b8a1SJoseph Chan  * Analog capture
1290c577b8a1SJoseph Chan  */
1291c577b8a1SJoseph Chan static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1292c577b8a1SJoseph Chan 				   struct hda_codec *codec,
1293c577b8a1SJoseph Chan 				   unsigned int stream_tag,
1294c577b8a1SJoseph Chan 				   unsigned int format,
1295c577b8a1SJoseph Chan 				   struct snd_pcm_substream *substream)
1296c577b8a1SJoseph Chan {
1297c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1298c577b8a1SJoseph Chan 
1299c577b8a1SJoseph Chan 	snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1300c577b8a1SJoseph Chan 				   stream_tag, 0, format);
1301c577b8a1SJoseph Chan 	return 0;
1302c577b8a1SJoseph Chan }
1303c577b8a1SJoseph Chan 
1304c577b8a1SJoseph Chan static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1305c577b8a1SJoseph Chan 				   struct hda_codec *codec,
1306c577b8a1SJoseph Chan 				   struct snd_pcm_substream *substream)
1307c577b8a1SJoseph Chan {
1308c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1309888afa15STakashi Iwai 	snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
1310c577b8a1SJoseph Chan 	return 0;
1311c577b8a1SJoseph Chan }
1312c577b8a1SJoseph Chan 
1313a86a88eaSTakashi Iwai /* analog capture with dynamic ADC switching */
1314a86a88eaSTakashi Iwai static int via_dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1315a86a88eaSTakashi Iwai 					   struct hda_codec *codec,
1316a86a88eaSTakashi Iwai 					   unsigned int stream_tag,
1317a86a88eaSTakashi Iwai 					   unsigned int format,
1318a86a88eaSTakashi Iwai 					   struct snd_pcm_substream *substream)
1319a86a88eaSTakashi Iwai {
1320a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1321a86a88eaSTakashi Iwai 	int adc_idx = spec->inputs[spec->cur_mux[0]].adc_idx;
1322a86a88eaSTakashi Iwai 
13233b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
1324a86a88eaSTakashi Iwai 	spec->cur_adc = spec->adc_nids[adc_idx];
1325a86a88eaSTakashi Iwai 	spec->cur_adc_stream_tag = stream_tag;
1326a86a88eaSTakashi Iwai 	spec->cur_adc_format = format;
1327a86a88eaSTakashi Iwai 	snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
13283b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1329a86a88eaSTakashi Iwai 	return 0;
1330a86a88eaSTakashi Iwai }
1331a86a88eaSTakashi Iwai 
1332a86a88eaSTakashi Iwai static int via_dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1333a86a88eaSTakashi Iwai 					   struct hda_codec *codec,
1334a86a88eaSTakashi Iwai 					   struct snd_pcm_substream *substream)
1335a86a88eaSTakashi Iwai {
1336a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1337a86a88eaSTakashi Iwai 
13383b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
1339a86a88eaSTakashi Iwai 	snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1340a86a88eaSTakashi Iwai 	spec->cur_adc = 0;
13413b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
1342a86a88eaSTakashi Iwai 	return 0;
1343a86a88eaSTakashi Iwai }
1344a86a88eaSTakashi Iwai 
1345a86a88eaSTakashi Iwai /* re-setup the stream if running; called from input-src put */
1346a86a88eaSTakashi Iwai static bool via_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
1347a86a88eaSTakashi Iwai {
1348a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
1349a86a88eaSTakashi Iwai 	int adc_idx = spec->inputs[cur].adc_idx;
1350a86a88eaSTakashi Iwai 	hda_nid_t adc = spec->adc_nids[adc_idx];
13513b607e3dSTakashi Iwai 	bool ret = false;
1352a86a88eaSTakashi Iwai 
13533b607e3dSTakashi Iwai 	mutex_lock(&spec->config_mutex);
1354a86a88eaSTakashi Iwai 	if (spec->cur_adc && spec->cur_adc != adc) {
1355a86a88eaSTakashi Iwai 		/* stream is running, let's swap the current ADC */
1356a86a88eaSTakashi Iwai 		__snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1357a86a88eaSTakashi Iwai 		spec->cur_adc = adc;
1358a86a88eaSTakashi Iwai 		snd_hda_codec_setup_stream(codec, adc,
1359a86a88eaSTakashi Iwai 					   spec->cur_adc_stream_tag, 0,
1360a86a88eaSTakashi Iwai 					   spec->cur_adc_format);
13613b607e3dSTakashi Iwai 		ret = true;
1362a86a88eaSTakashi Iwai 	}
13633b607e3dSTakashi Iwai 	mutex_unlock(&spec->config_mutex);
13643b607e3dSTakashi Iwai 	return ret;
1365a86a88eaSTakashi Iwai }
1366a86a88eaSTakashi Iwai 
13679af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_analog_playback = {
13687eb56e84STakashi Iwai 	.substreams = 1,
1369c577b8a1SJoseph Chan 	.channels_min = 2,
1370c577b8a1SJoseph Chan 	.channels_max = 8,
13719af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1372c577b8a1SJoseph Chan 	.ops = {
1373ece8d043STakashi Iwai 		.open = via_playback_multi_pcm_open,
1374ece8d043STakashi Iwai 		.close = via_playback_multi_pcm_close,
13750aa62aefSHarald Welte 		.prepare = via_playback_multi_pcm_prepare,
13760aa62aefSHarald Welte 		.cleanup = via_playback_multi_pcm_cleanup
1377c577b8a1SJoseph Chan 	},
1378c577b8a1SJoseph Chan };
1379c577b8a1SJoseph Chan 
13807eb56e84STakashi Iwai static const struct hda_pcm_stream via_pcm_hp_playback = {
13817eb56e84STakashi Iwai 	.substreams = 1,
13827eb56e84STakashi Iwai 	.channels_min = 2,
13837eb56e84STakashi Iwai 	.channels_max = 2,
13847eb56e84STakashi Iwai 	/* NID is set in via_build_pcms */
13857eb56e84STakashi Iwai 	.ops = {
13867eb56e84STakashi Iwai 		.open = via_playback_hp_pcm_open,
1387ece8d043STakashi Iwai 		.close = via_playback_hp_pcm_close,
13887eb56e84STakashi Iwai 		.prepare = via_playback_hp_pcm_prepare,
13897eb56e84STakashi Iwai 		.cleanup = via_playback_hp_pcm_cleanup
13907eb56e84STakashi Iwai 	},
13917eb56e84STakashi Iwai };
13927eb56e84STakashi Iwai 
139390dd48a1STakashi Iwai static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
13947eb56e84STakashi Iwai 	.substreams = 1,
1395bc9b5623STakashi Iwai 	.channels_min = 2,
1396bc9b5623STakashi Iwai 	.channels_max = 8,
13979af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1398bc9b5623STakashi Iwai 	/* We got noisy outputs on the right channel on VT1708 when
1399bc9b5623STakashi Iwai 	 * 24bit samples are used.  Until any workaround is found,
1400bc9b5623STakashi Iwai 	 * disable the 24bit format, so far.
1401bc9b5623STakashi Iwai 	 */
1402bc9b5623STakashi Iwai 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
1403bc9b5623STakashi Iwai 	.ops = {
1404ece8d043STakashi Iwai 		.open = via_playback_multi_pcm_open,
1405ece8d043STakashi Iwai 		.close = via_playback_multi_pcm_close,
1406c873cc25SLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
1407c873cc25SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup
1408bc9b5623STakashi Iwai 	},
1409bc9b5623STakashi Iwai };
1410bc9b5623STakashi Iwai 
14119af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_analog_capture = {
14127eb56e84STakashi Iwai 	.substreams = 1, /* will be changed in via_build_pcms() */
1413c577b8a1SJoseph Chan 	.channels_min = 2,
1414c577b8a1SJoseph Chan 	.channels_max = 2,
14159af74210STakashi Iwai 	/* NID is set in via_build_pcms */
1416c577b8a1SJoseph Chan 	.ops = {
1417c577b8a1SJoseph Chan 		.prepare = via_capture_pcm_prepare,
1418c577b8a1SJoseph Chan 		.cleanup = via_capture_pcm_cleanup
1419c577b8a1SJoseph Chan 	},
1420c577b8a1SJoseph Chan };
1421c577b8a1SJoseph Chan 
1422a86a88eaSTakashi Iwai static const struct hda_pcm_stream via_pcm_dyn_adc_analog_capture = {
1423a86a88eaSTakashi Iwai 	.substreams = 1,
1424a86a88eaSTakashi Iwai 	.channels_min = 2,
1425a86a88eaSTakashi Iwai 	.channels_max = 2,
1426a86a88eaSTakashi Iwai 	/* NID is set in via_build_pcms */
1427a86a88eaSTakashi Iwai 	.ops = {
1428a86a88eaSTakashi Iwai 		.prepare = via_dyn_adc_capture_pcm_prepare,
1429a86a88eaSTakashi Iwai 		.cleanup = via_dyn_adc_capture_pcm_cleanup,
1430a86a88eaSTakashi Iwai 	},
1431a86a88eaSTakashi Iwai };
1432a86a88eaSTakashi Iwai 
14339af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_digital_playback = {
1434c577b8a1SJoseph Chan 	.substreams = 1,
1435c577b8a1SJoseph Chan 	.channels_min = 2,
1436c577b8a1SJoseph Chan 	.channels_max = 2,
1437c577b8a1SJoseph Chan 	/* NID is set in via_build_pcms */
1438c577b8a1SJoseph Chan 	.ops = {
1439c577b8a1SJoseph Chan 		.open = via_dig_playback_pcm_open,
14406b97eb45STakashi Iwai 		.close = via_dig_playback_pcm_close,
14419da29271STakashi Iwai 		.prepare = via_dig_playback_pcm_prepare,
14429da29271STakashi Iwai 		.cleanup = via_dig_playback_pcm_cleanup
1443c577b8a1SJoseph Chan 	},
1444c577b8a1SJoseph Chan };
1445c577b8a1SJoseph Chan 
14469af74210STakashi Iwai static const struct hda_pcm_stream via_pcm_digital_capture = {
1447c577b8a1SJoseph Chan 	.substreams = 1,
1448c577b8a1SJoseph Chan 	.channels_min = 2,
1449c577b8a1SJoseph Chan 	.channels_max = 2,
1450c577b8a1SJoseph Chan };
1451c577b8a1SJoseph Chan 
1452370bafbdSTakashi Iwai /*
1453370bafbdSTakashi Iwai  * slave controls for virtual master
1454370bafbdSTakashi Iwai  */
14559322ca54STakashi Iwai static const char * const via_slave_pfxs[] = {
14569322ca54STakashi Iwai 	"Front", "Surround", "Center", "LFE", "Side",
14579322ca54STakashi Iwai 	"Headphone", "Speaker",
1458370bafbdSTakashi Iwai 	NULL,
1459370bafbdSTakashi Iwai };
1460370bafbdSTakashi Iwai 
1461c577b8a1SJoseph Chan static int via_build_controls(struct hda_codec *codec)
1462c577b8a1SJoseph Chan {
1463c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
14645b0cb1d8SJaroslav Kysela 	struct snd_kcontrol *kctl;
14655b0cb1d8SJaroslav Kysela 	int err, i;
1466c577b8a1SJoseph Chan 
1467b5bcc189STakashi Iwai 	spec->no_pin_power_ctl = 1;
146824088a58STakashi Iwai 	if (spec->set_widgets_power_state)
146924088a58STakashi Iwai 		if (!via_clone_control(spec, &via_pin_power_ctl_enum))
147024088a58STakashi Iwai 			return -ENOMEM;
147124088a58STakashi Iwai 
1472c577b8a1SJoseph Chan 	for (i = 0; i < spec->num_mixers; i++) {
1473c577b8a1SJoseph Chan 		err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1474c577b8a1SJoseph Chan 		if (err < 0)
1475c577b8a1SJoseph Chan 			return err;
1476c577b8a1SJoseph Chan 	}
1477c577b8a1SJoseph Chan 
1478c577b8a1SJoseph Chan 	if (spec->multiout.dig_out_nid) {
1479c577b8a1SJoseph Chan 		err = snd_hda_create_spdif_out_ctls(codec,
148074b654c9SStephen Warren 						    spec->multiout.dig_out_nid,
1481c577b8a1SJoseph Chan 						    spec->multiout.dig_out_nid);
1482c577b8a1SJoseph Chan 		if (err < 0)
1483c577b8a1SJoseph Chan 			return err;
14849a08160bSTakashi Iwai 		err = snd_hda_create_spdif_share_sw(codec,
14859a08160bSTakashi Iwai 						    &spec->multiout);
14869a08160bSTakashi Iwai 		if (err < 0)
14879a08160bSTakashi Iwai 			return err;
14889a08160bSTakashi Iwai 		spec->multiout.share_spdif = 1;
1489c577b8a1SJoseph Chan 	}
1490c577b8a1SJoseph Chan 	if (spec->dig_in_nid) {
1491c577b8a1SJoseph Chan 		err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1492c577b8a1SJoseph Chan 		if (err < 0)
1493c577b8a1SJoseph Chan 			return err;
1494c577b8a1SJoseph Chan 	}
149517314379SLydia Wang 
1496370bafbdSTakashi Iwai 	/* if we have no master control, let's create it */
1497370bafbdSTakashi Iwai 	if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1498370bafbdSTakashi Iwai 		unsigned int vmaster_tlv[4];
1499370bafbdSTakashi Iwai 		snd_hda_set_vmaster_tlv(codec, spec->multiout.dac_nids[0],
1500370bafbdSTakashi Iwai 					HDA_OUTPUT, vmaster_tlv);
1501370bafbdSTakashi Iwai 		err = snd_hda_add_vmaster(codec, "Master Playback Volume",
15029322ca54STakashi Iwai 					  vmaster_tlv, via_slave_pfxs,
15039322ca54STakashi Iwai 					  "Playback Volume");
1504370bafbdSTakashi Iwai 		if (err < 0)
1505370bafbdSTakashi Iwai 			return err;
1506370bafbdSTakashi Iwai 	}
1507370bafbdSTakashi Iwai 	if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1508370bafbdSTakashi Iwai 		err = snd_hda_add_vmaster(codec, "Master Playback Switch",
15099322ca54STakashi Iwai 					  NULL, via_slave_pfxs,
15109322ca54STakashi Iwai 					  "Playback Switch");
1511370bafbdSTakashi Iwai 		if (err < 0)
1512370bafbdSTakashi Iwai 			return err;
1513370bafbdSTakashi Iwai 	}
1514370bafbdSTakashi Iwai 
15155b0cb1d8SJaroslav Kysela 	/* assign Capture Source enums to NID */
15165b0cb1d8SJaroslav Kysela 	kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
15175b0cb1d8SJaroslav Kysela 	for (i = 0; kctl && i < kctl->count; i++) {
151877e314f7STakashi Iwai 		if (!spec->mux_nids[i])
151977e314f7STakashi Iwai 			continue;
152021949f00STakashi Iwai 		err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
15215b0cb1d8SJaroslav Kysela 		if (err < 0)
15225b0cb1d8SJaroslav Kysela 			return err;
15235b0cb1d8SJaroslav Kysela 	}
15245b0cb1d8SJaroslav Kysela 
1525603c4019STakashi Iwai 	via_free_kctls(codec); /* no longer needed */
152601a61e12STakashi Iwai 
152701a61e12STakashi Iwai 	err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
152801a61e12STakashi Iwai 	if (err < 0)
152901a61e12STakashi Iwai 		return err;
153001a61e12STakashi Iwai 
1531c577b8a1SJoseph Chan 	return 0;
1532c577b8a1SJoseph Chan }
1533c577b8a1SJoseph Chan 
1534c577b8a1SJoseph Chan static int via_build_pcms(struct hda_codec *codec)
1535c577b8a1SJoseph Chan {
1536c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1537c577b8a1SJoseph Chan 	struct hda_pcm *info = spec->pcm_rec;
1538c577b8a1SJoseph Chan 
1539a5973103STakashi Iwai 	codec->num_pcms = 0;
1540c577b8a1SJoseph Chan 	codec->pcm_info = info;
1541c577b8a1SJoseph Chan 
1542a5973103STakashi Iwai 	if (spec->multiout.num_dacs || spec->num_adc_nids) {
1543a5973103STakashi Iwai 		snprintf(spec->stream_name_analog,
1544a5973103STakashi Iwai 			 sizeof(spec->stream_name_analog),
154582673bc8STakashi Iwai 			 "%s Analog", codec->chip_name);
1546c577b8a1SJoseph Chan 		info->name = spec->stream_name_analog;
15479af74210STakashi Iwai 
1548a5973103STakashi Iwai 		if (spec->multiout.num_dacs) {
15499af74210STakashi Iwai 			if (!spec->stream_analog_playback)
1550a5973103STakashi Iwai 				spec->stream_analog_playback =
1551a5973103STakashi Iwai 					&via_pcm_analog_playback;
1552377ff31aSLydia Wang 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
15539af74210STakashi Iwai 				*spec->stream_analog_playback;
1554377ff31aSLydia Wang 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1555377ff31aSLydia Wang 				spec->multiout.dac_nids[0];
1556c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1557c577b8a1SJoseph Chan 				spec->multiout.max_channels;
1558a5973103STakashi Iwai 		}
15599af74210STakashi Iwai 
1560a86a88eaSTakashi Iwai 		if (!spec->stream_analog_capture) {
1561a86a88eaSTakashi Iwai 			if (spec->dyn_adc_switch)
1562a86a88eaSTakashi Iwai 				spec->stream_analog_capture =
1563a86a88eaSTakashi Iwai 					&via_pcm_dyn_adc_analog_capture;
1564a86a88eaSTakashi Iwai 			else
1565a5973103STakashi Iwai 				spec->stream_analog_capture =
1566a5973103STakashi Iwai 					&via_pcm_analog_capture;
1567a86a88eaSTakashi Iwai 		}
1568a5973103STakashi Iwai 		if (spec->num_adc_nids) {
15699af74210STakashi Iwai 			info->stream[SNDRV_PCM_STREAM_CAPTURE] =
15709af74210STakashi Iwai 				*spec->stream_analog_capture;
1571a5973103STakashi Iwai 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1572a5973103STakashi Iwai 				spec->adc_nids[0];
1573a86a88eaSTakashi Iwai 			if (!spec->dyn_adc_switch)
15749af74210STakashi Iwai 				info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
15759af74210STakashi Iwai 					spec->num_adc_nids;
1576a5973103STakashi Iwai 		}
1577c577b8a1SJoseph Chan 		codec->num_pcms++;
1578c577b8a1SJoseph Chan 		info++;
1579a5973103STakashi Iwai 	}
1580a5973103STakashi Iwai 
1581a5973103STakashi Iwai 	if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
158282673bc8STakashi Iwai 		snprintf(spec->stream_name_digital,
158382673bc8STakashi Iwai 			 sizeof(spec->stream_name_digital),
158482673bc8STakashi Iwai 			 "%s Digital", codec->chip_name);
1585c577b8a1SJoseph Chan 		info->name = spec->stream_name_digital;
15867ba72ba1STakashi Iwai 		info->pcm_type = HDA_PCM_TYPE_SPDIF;
1587c577b8a1SJoseph Chan 		if (spec->multiout.dig_out_nid) {
15889af74210STakashi Iwai 			if (!spec->stream_digital_playback)
15899af74210STakashi Iwai 				spec->stream_digital_playback =
15909af74210STakashi Iwai 					&via_pcm_digital_playback;
1591c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
15929af74210STakashi Iwai 				*spec->stream_digital_playback;
1593c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1594c577b8a1SJoseph Chan 				spec->multiout.dig_out_nid;
1595c577b8a1SJoseph Chan 		}
1596c577b8a1SJoseph Chan 		if (spec->dig_in_nid) {
15979af74210STakashi Iwai 			if (!spec->stream_digital_capture)
15989af74210STakashi Iwai 				spec->stream_digital_capture =
15999af74210STakashi Iwai 					&via_pcm_digital_capture;
1600c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_CAPTURE] =
16019af74210STakashi Iwai 				*spec->stream_digital_capture;
1602c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1603c577b8a1SJoseph Chan 				spec->dig_in_nid;
1604c577b8a1SJoseph Chan 		}
1605a5973103STakashi Iwai 		codec->num_pcms++;
1606a5973103STakashi Iwai 		info++;
1607c577b8a1SJoseph Chan 	}
1608c577b8a1SJoseph Chan 
1609ece8d043STakashi Iwai 	if (spec->hp_dac_nid) {
16107eb56e84STakashi Iwai 		snprintf(spec->stream_name_hp, sizeof(spec->stream_name_hp),
16117eb56e84STakashi Iwai 			 "%s HP", codec->chip_name);
16127eb56e84STakashi Iwai 		info->name = spec->stream_name_hp;
16137eb56e84STakashi Iwai 		info->stream[SNDRV_PCM_STREAM_PLAYBACK] = via_pcm_hp_playback;
16147eb56e84STakashi Iwai 		info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1615ece8d043STakashi Iwai 			spec->hp_dac_nid;
1616a5973103STakashi Iwai 		codec->num_pcms++;
1617a5973103STakashi Iwai 		info++;
16187eb56e84STakashi Iwai 	}
1619c577b8a1SJoseph Chan 	return 0;
1620c577b8a1SJoseph Chan }
1621c577b8a1SJoseph Chan 
1622c577b8a1SJoseph Chan static void via_free(struct hda_codec *codec)
1623c577b8a1SJoseph Chan {
1624c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1625c577b8a1SJoseph Chan 
1626c577b8a1SJoseph Chan 	if (!spec)
1627c577b8a1SJoseph Chan 		return;
1628c577b8a1SJoseph Chan 
1629603c4019STakashi Iwai 	via_free_kctls(codec);
16301f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
1631a86a88eaSTakashi Iwai 	kfree(spec->bind_cap_vol);
1632a86a88eaSTakashi Iwai 	kfree(spec->bind_cap_sw);
1633*7819d1c7STakashi Iwai 	snd_hda_gen_free(&spec->gen);
1634a86a88eaSTakashi Iwai 	kfree(spec);
1635c577b8a1SJoseph Chan }
1636c577b8a1SJoseph Chan 
163764be285bSTakashi Iwai /* mute/unmute outputs */
163864be285bSTakashi Iwai static void toggle_output_mutes(struct hda_codec *codec, int num_pins,
163964be285bSTakashi Iwai 				hda_nid_t *pins, bool mute)
164064be285bSTakashi Iwai {
164164be285bSTakashi Iwai 	int i;
164294994734STakashi Iwai 	for (i = 0; i < num_pins; i++) {
164394994734STakashi Iwai 		unsigned int parm = snd_hda_codec_read(codec, pins[i], 0,
164494994734STakashi Iwai 					  AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
164594994734STakashi Iwai 		if (parm & AC_PINCTL_IN_EN)
164694994734STakashi Iwai 			continue;
164794994734STakashi Iwai 		if (mute)
164894994734STakashi Iwai 			parm &= ~AC_PINCTL_OUT_EN;
164994994734STakashi Iwai 		else
165094994734STakashi Iwai 			parm |= AC_PINCTL_OUT_EN;
1651cdd03cedSTakashi Iwai 		snd_hda_set_pin_ctl(codec, pins[i], parm);
165294994734STakashi Iwai 	}
165364be285bSTakashi Iwai }
165464be285bSTakashi Iwai 
16554a918ffeSTakashi Iwai /* mute internal speaker if line-out is plugged */
16564a918ffeSTakashi Iwai static void via_line_automute(struct hda_codec *codec, int present)
16574a918ffeSTakashi Iwai {
16584a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
16594a918ffeSTakashi Iwai 
16604a918ffeSTakashi Iwai 	if (!spec->autocfg.speaker_outs)
16614a918ffeSTakashi Iwai 		return;
16624a918ffeSTakashi Iwai 	if (!present)
16634a918ffeSTakashi Iwai 		present = snd_hda_jack_detect(codec,
16644a918ffeSTakashi Iwai 					      spec->autocfg.line_out_pins[0]);
16654a918ffeSTakashi Iwai 	toggle_output_mutes(codec, spec->autocfg.speaker_outs,
16664a918ffeSTakashi Iwai 			    spec->autocfg.speaker_pins,
16674a918ffeSTakashi Iwai 			    present);
16684a918ffeSTakashi Iwai }
16694a918ffeSTakashi Iwai 
167069e52a80SHarald Welte /* mute internal speaker if HP is plugged */
167169e52a80SHarald Welte static void via_hp_automute(struct hda_codec *codec)
167269e52a80SHarald Welte {
16734a918ffeSTakashi Iwai 	int present = 0;
16746e969d91STakashi Iwai 	int nums;
167569e52a80SHarald Welte 	struct via_spec *spec = codec->spec;
167669e52a80SHarald Welte 
1677187d333eSTakashi Iwai 	if (!spec->hp_independent_mode && spec->autocfg.hp_pins[0] &&
1678cf55e904SHerton Ronaldo Krzesinski 	    (spec->codec_type != VT1708 || spec->vt1708_jack_detect) &&
1679cf55e904SHerton Ronaldo Krzesinski 	    is_jack_detectable(codec, spec->autocfg.hp_pins[0]))
1680d56757abSTakashi Iwai 		present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
16816e969d91STakashi Iwai 
1682f2b1c9f0STakashi Iwai 	if (spec->smart51_enabled)
1683f2b1c9f0STakashi Iwai 		nums = spec->autocfg.line_outs + spec->smart51_nums;
1684f2b1c9f0STakashi Iwai 	else
1685f2b1c9f0STakashi Iwai 		nums = spec->autocfg.line_outs;
16866e969d91STakashi Iwai 	toggle_output_mutes(codec, nums, spec->autocfg.line_out_pins, present);
16876e969d91STakashi Iwai 
16884a918ffeSTakashi Iwai 	via_line_automute(codec, present);
1689f3db423dSLydia Wang }
1690f3db423dSLydia Wang 
16912a43952aSTakashi Iwai #ifdef CONFIG_PM
169268cb2b55STakashi Iwai static int via_suspend(struct hda_codec *codec)
16931f2e99feSLydia Wang {
16941f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
16951f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
169694c142a1SDavid Henningsson 
169794c142a1SDavid Henningsson 	if (spec->codec_type == VT1802) {
169894c142a1SDavid Henningsson 		/* Fix pop noise on headphones */
169994c142a1SDavid Henningsson 		int i;
170094c142a1SDavid Henningsson 		for (i = 0; i < spec->autocfg.hp_outs; i++)
170194c142a1SDavid Henningsson 			snd_hda_set_pin_ctl(codec, spec->autocfg.hp_pins[i], 0);
170294c142a1SDavid Henningsson 	}
170394c142a1SDavid Henningsson 
17041f2e99feSLydia Wang 	return 0;
17051f2e99feSLydia Wang }
17061f2e99feSLydia Wang #endif
17071f2e99feSLydia Wang 
170883012a7cSTakashi Iwai #ifdef CONFIG_PM
1709cb53c626STakashi Iwai static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1710cb53c626STakashi Iwai {
1711cb53c626STakashi Iwai 	struct via_spec *spec = codec->spec;
1712cb53c626STakashi Iwai 	return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1713cb53c626STakashi Iwai }
1714cb53c626STakashi Iwai #endif
1715cb53c626STakashi Iwai 
1716c577b8a1SJoseph Chan /*
1717c577b8a1SJoseph Chan  */
17185d41762aSTakashi Iwai 
17195d41762aSTakashi Iwai static int via_init(struct hda_codec *codec);
17205d41762aSTakashi Iwai 
172190dd48a1STakashi Iwai static const struct hda_codec_ops via_patch_ops = {
1722c577b8a1SJoseph Chan 	.build_controls = via_build_controls,
1723c577b8a1SJoseph Chan 	.build_pcms = via_build_pcms,
1724c577b8a1SJoseph Chan 	.init = via_init,
1725c577b8a1SJoseph Chan 	.free = via_free,
17264e2d16d3SDavid Henningsson 	.unsol_event = snd_hda_jack_unsol_event,
17272a43952aSTakashi Iwai #ifdef CONFIG_PM
17281f2e99feSLydia Wang 	.suspend = via_suspend,
1729cb53c626STakashi Iwai 	.check_power_status = via_check_power_status,
1730cb53c626STakashi Iwai #endif
1731c577b8a1SJoseph Chan };
1732c577b8a1SJoseph Chan 
17334a79616dSTakashi Iwai static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
1734c577b8a1SJoseph Chan {
17354a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
17364a79616dSTakashi Iwai 	int i;
17374a79616dSTakashi Iwai 
17384a79616dSTakashi Iwai 	for (i = 0; i < spec->multiout.num_dacs; i++) {
17394a79616dSTakashi Iwai 		if (spec->multiout.dac_nids[i] == dac)
17404a79616dSTakashi Iwai 			return false;
17414a79616dSTakashi Iwai 	}
1742ece8d043STakashi Iwai 	if (spec->hp_dac_nid == dac)
17434a79616dSTakashi Iwai 		return false;
17444a79616dSTakashi Iwai 	return true;
17454a79616dSTakashi Iwai }
17464a79616dSTakashi Iwai 
17478e3679dcSTakashi Iwai static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
17483214b966STakashi Iwai 				hda_nid_t target_dac, int with_aa_mix,
17493214b966STakashi Iwai 				struct nid_path *path, int depth)
17504a79616dSTakashi Iwai {
17513214b966STakashi Iwai 	struct via_spec *spec = codec->spec;
17524a79616dSTakashi Iwai 	hda_nid_t conn[8];
17534a79616dSTakashi Iwai 	int i, nums;
17544a79616dSTakashi Iwai 
17553214b966STakashi Iwai 	if (nid == spec->aa_mix_nid) {
17563214b966STakashi Iwai 		if (!with_aa_mix)
17573214b966STakashi Iwai 			return false;
17583214b966STakashi Iwai 		with_aa_mix = 2; /* mark aa-mix is included */
17593214b966STakashi Iwai 	}
17603214b966STakashi Iwai 
17614a79616dSTakashi Iwai 	nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
17624a79616dSTakashi Iwai 	for (i = 0; i < nums; i++) {
17634a79616dSTakashi Iwai 		if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
17644a79616dSTakashi Iwai 			continue;
17653214b966STakashi Iwai 		if (conn[i] == target_dac || is_empty_dac(codec, conn[i])) {
17663214b966STakashi Iwai 			/* aa-mix is requested but not included? */
17673214b966STakashi Iwai 			if (!(spec->aa_mix_nid && with_aa_mix == 1))
176809a9ad69STakashi Iwai 				goto found;
17694a79616dSTakashi Iwai 		}
17703214b966STakashi Iwai 	}
17718e3679dcSTakashi Iwai 	if (depth >= MAX_NID_PATH_DEPTH)
17724a79616dSTakashi Iwai 		return false;
17734a79616dSTakashi Iwai 	for (i = 0; i < nums; i++) {
17744a79616dSTakashi Iwai 		unsigned int type;
17754a79616dSTakashi Iwai 		type = get_wcaps_type(get_wcaps(codec, conn[i]));
17763214b966STakashi Iwai 		if (type == AC_WID_AUD_OUT)
17774a79616dSTakashi Iwai 			continue;
17788e3679dcSTakashi Iwai 		if (__parse_output_path(codec, conn[i], target_dac,
17793214b966STakashi Iwai 					with_aa_mix, path, depth + 1))
178009a9ad69STakashi Iwai 			goto found;
17814a79616dSTakashi Iwai 	}
17824a79616dSTakashi Iwai 	return false;
178309a9ad69STakashi Iwai 
178409a9ad69STakashi Iwai  found:
178509a9ad69STakashi Iwai 	path->path[path->depth] = conn[i];
178609a9ad69STakashi Iwai 	path->idx[path->depth] = i;
178709a9ad69STakashi Iwai 	if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
178809a9ad69STakashi Iwai 		path->multi[path->depth] = 1;
178909a9ad69STakashi Iwai 	path->depth++;
179009a9ad69STakashi Iwai 	return true;
17914a79616dSTakashi Iwai }
17924a79616dSTakashi Iwai 
17938e3679dcSTakashi Iwai static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
17943214b966STakashi Iwai 			      hda_nid_t target_dac, int with_aa_mix,
17953214b966STakashi Iwai 			      struct nid_path *path)
17968e3679dcSTakashi Iwai {
17973214b966STakashi Iwai 	if (__parse_output_path(codec, nid, target_dac, with_aa_mix, path, 1)) {
17988e3679dcSTakashi Iwai 		path->path[path->depth] = nid;
17998e3679dcSTakashi Iwai 		path->depth++;
18003214b966STakashi Iwai 		snd_printdd("output-path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
18013214b966STakashi Iwai 			    path->depth, path->path[0], path->path[1],
18023214b966STakashi Iwai 			    path->path[2], path->path[3], path->path[4]);
18038e3679dcSTakashi Iwai 		return true;
18048e3679dcSTakashi Iwai 	}
18058e3679dcSTakashi Iwai 	return false;
18068e3679dcSTakashi Iwai }
18078e3679dcSTakashi Iwai 
18084a79616dSTakashi Iwai static int via_auto_fill_dac_nids(struct hda_codec *codec)
18094a79616dSTakashi Iwai {
18104a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
18114a79616dSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
18125c9a5615SLydia Wang 	int i, dac_num;
1813c577b8a1SJoseph Chan 	hda_nid_t nid;
1814c577b8a1SJoseph Chan 
1815c577b8a1SJoseph Chan 	spec->multiout.dac_nids = spec->private_dac_nids;
18165c9a5615SLydia Wang 	dac_num = 0;
18174a79616dSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
18183214b966STakashi Iwai 		hda_nid_t dac = 0;
1819c577b8a1SJoseph Chan 		nid = cfg->line_out_pins[i];
18204a79616dSTakashi Iwai 		if (!nid)
18214a79616dSTakashi Iwai 			continue;
18223214b966STakashi Iwai 		if (parse_output_path(codec, nid, 0, 0, &spec->out_path[i]))
18233214b966STakashi Iwai 			dac = spec->out_path[i].path[0];
18243214b966STakashi Iwai 		if (!i && parse_output_path(codec, nid, dac, 1,
18253214b966STakashi Iwai 					    &spec->out_mix_path))
18263214b966STakashi Iwai 			dac = spec->out_mix_path.path[0];
18273214b966STakashi Iwai 		if (dac) {
18283214b966STakashi Iwai 			spec->private_dac_nids[i] = dac;
18295c9a5615SLydia Wang 			dac_num++;
1830c577b8a1SJoseph Chan 		}
18315c9a5615SLydia Wang 	}
18323214b966STakashi Iwai 	if (!spec->out_path[0].depth && spec->out_mix_path.depth) {
18333214b966STakashi Iwai 		spec->out_path[0] = spec->out_mix_path;
18343214b966STakashi Iwai 		spec->out_mix_path.depth = 0;
18353214b966STakashi Iwai 	}
18365c9a5615SLydia Wang 	spec->multiout.num_dacs = dac_num;
1837c577b8a1SJoseph Chan 	return 0;
1838c577b8a1SJoseph Chan }
1839c577b8a1SJoseph Chan 
18404a79616dSTakashi Iwai static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
184109a9ad69STakashi Iwai 			  int chs, bool check_dac, struct nid_path *path)
1842c577b8a1SJoseph Chan {
18434a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
1844c577b8a1SJoseph Chan 	char name[32];
184509a9ad69STakashi Iwai 	hda_nid_t dac, pin, sel, nid;
184609a9ad69STakashi Iwai 	int err;
1847a934d5a9STakashi Iwai 
184809a9ad69STakashi Iwai 	dac = check_dac ? path->path[0] : 0;
184909a9ad69STakashi Iwai 	pin = path->path[path->depth - 1];
185009a9ad69STakashi Iwai 	sel = path->depth > 1 ? path->path[1] : 0;
1851c577b8a1SJoseph Chan 
18528df2a312STakashi Iwai 	if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
18534a79616dSTakashi Iwai 		nid = dac;
18548df2a312STakashi Iwai 	else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
18554a79616dSTakashi Iwai 		nid = pin;
1856a934d5a9STakashi Iwai 	else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1857a934d5a9STakashi Iwai 		nid = sel;
18584a79616dSTakashi Iwai 	else
18594a79616dSTakashi Iwai 		nid = 0;
18604a79616dSTakashi Iwai 	if (nid) {
18614a79616dSTakashi Iwai 		sprintf(name, "%s Playback Volume", pfx);
1862c577b8a1SJoseph Chan 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1863a00a5fadSLydia Wang 			      HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1864c577b8a1SJoseph Chan 		if (err < 0)
1865c577b8a1SJoseph Chan 			return err;
186609a9ad69STakashi Iwai 		path->vol_ctl = nid;
1867c577b8a1SJoseph Chan 	}
18684a79616dSTakashi Iwai 
18698df2a312STakashi Iwai 	if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
18704a79616dSTakashi Iwai 		nid = dac;
18718df2a312STakashi Iwai 	else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
18724a79616dSTakashi Iwai 		nid = pin;
1873a934d5a9STakashi Iwai 	else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
1874a934d5a9STakashi Iwai 		nid = sel;
18754a79616dSTakashi Iwai 	else
18764a79616dSTakashi Iwai 		nid = 0;
18774a79616dSTakashi Iwai 	if (nid) {
18784a79616dSTakashi Iwai 		sprintf(name, "%s Playback Switch", pfx);
18794a79616dSTakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
18804a79616dSTakashi Iwai 			      HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
18814a79616dSTakashi Iwai 		if (err < 0)
18824a79616dSTakashi Iwai 			return err;
188309a9ad69STakashi Iwai 		path->mute_ctl = nid;
18844a79616dSTakashi Iwai 	}
18854a79616dSTakashi Iwai 	return 0;
18864a79616dSTakashi Iwai }
18874a79616dSTakashi Iwai 
1888f4a7828bSTakashi Iwai static void mangle_smart51(struct hda_codec *codec)
1889f4a7828bSTakashi Iwai {
1890f4a7828bSTakashi Iwai 	struct via_spec *spec = codec->spec;
1891f4a7828bSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
18920f98c24bSTakashi Iwai 	struct auto_pin_cfg_item *ins = cfg->inputs;
18930f98c24bSTakashi Iwai 	int i, j, nums, attr;
18940f98c24bSTakashi Iwai 	int pins[AUTO_CFG_MAX_INS];
1895f4a7828bSTakashi Iwai 
18960f98c24bSTakashi Iwai 	for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
18970f98c24bSTakashi Iwai 		nums = 0;
1898f4a7828bSTakashi Iwai 		for (i = 0; i < cfg->num_inputs; i++) {
18990f98c24bSTakashi Iwai 			unsigned int def;
19000f98c24bSTakashi Iwai 			if (ins[i].type > AUTO_PIN_LINE_IN)
19010f98c24bSTakashi Iwai 				continue;
19020f98c24bSTakashi Iwai 			def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
19030f98c24bSTakashi Iwai 			if (snd_hda_get_input_pin_attr(def) != attr)
19040f98c24bSTakashi Iwai 				continue;
19050f98c24bSTakashi Iwai 			for (j = 0; j < nums; j++)
19060f98c24bSTakashi Iwai 				if (ins[pins[j]].type < ins[i].type) {
19070f98c24bSTakashi Iwai 					memmove(pins + j + 1, pins + j,
190821d45d2bSTakashi Iwai 						(nums - j) * sizeof(int));
19090f98c24bSTakashi Iwai 					break;
19100f98c24bSTakashi Iwai 				}
19110f98c24bSTakashi Iwai 			pins[j] = i;
1912e3d7a143STakashi Iwai 			nums++;
1913e3d7a143STakashi Iwai 		}
1914e3d7a143STakashi Iwai 		if (cfg->line_outs + nums < 3)
1915f4a7828bSTakashi Iwai 			continue;
19160f98c24bSTakashi Iwai 		for (i = 0; i < nums; i++) {
19170f98c24bSTakashi Iwai 			hda_nid_t pin = ins[pins[i]].pin;
19180f98c24bSTakashi Iwai 			spec->smart51_pins[spec->smart51_nums++] = pin;
19190f98c24bSTakashi Iwai 			cfg->line_out_pins[cfg->line_outs++] = pin;
1920f4a7828bSTakashi Iwai 			if (cfg->line_outs == 3)
1921f4a7828bSTakashi Iwai 				break;
1922f4a7828bSTakashi Iwai 		}
19230f98c24bSTakashi Iwai 		return;
19240f98c24bSTakashi Iwai 	}
1925f4a7828bSTakashi Iwai }
1926f4a7828bSTakashi Iwai 
1927020066d1STakashi Iwai static void copy_path_mixer_ctls(struct nid_path *dst, struct nid_path *src)
1928020066d1STakashi Iwai {
1929020066d1STakashi Iwai 	dst->vol_ctl = src->vol_ctl;
1930020066d1STakashi Iwai 	dst->mute_ctl = src->mute_ctl;
1931020066d1STakashi Iwai }
1932020066d1STakashi Iwai 
19334a79616dSTakashi Iwai /* add playback controls from the parsed DAC table */
19344a79616dSTakashi Iwai static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
19354a79616dSTakashi Iwai {
19364a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
1937f4a7828bSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
19383214b966STakashi Iwai 	struct nid_path *path;
19394a79616dSTakashi Iwai 	static const char * const chname[4] = {
19404a79616dSTakashi Iwai 		"Front", "Surround", "C/LFE", "Side"
19414a79616dSTakashi Iwai 	};
19424a79616dSTakashi Iwai 	int i, idx, err;
1943f4a7828bSTakashi Iwai 	int old_line_outs;
1944f4a7828bSTakashi Iwai 
1945f4a7828bSTakashi Iwai 	/* check smart51 */
1946f4a7828bSTakashi Iwai 	old_line_outs = cfg->line_outs;
1947f4a7828bSTakashi Iwai 	if (cfg->line_outs == 1)
1948f4a7828bSTakashi Iwai 		mangle_smart51(codec);
19494a79616dSTakashi Iwai 
1950e3d7a143STakashi Iwai 	err = via_auto_fill_dac_nids(codec);
1951e3d7a143STakashi Iwai 	if (err < 0)
1952e3d7a143STakashi Iwai 		return err;
1953e3d7a143STakashi Iwai 
19545c9a5615SLydia Wang 	if (spec->multiout.num_dacs < 3) {
19555c9a5615SLydia Wang 		spec->smart51_nums = 0;
19565c9a5615SLydia Wang 		cfg->line_outs = old_line_outs;
19575c9a5615SLydia Wang 	}
19584a79616dSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
19594a79616dSTakashi Iwai 		hda_nid_t pin, dac;
19604a79616dSTakashi Iwai 		pin = cfg->line_out_pins[i];
19614a79616dSTakashi Iwai 		dac = spec->multiout.dac_nids[i];
19624a79616dSTakashi Iwai 		if (!pin || !dac)
19634a79616dSTakashi Iwai 			continue;
19643214b966STakashi Iwai 		path = spec->out_path + i;
19650fe0adf8STakashi Iwai 		if (i == HDA_CLFE) {
19663214b966STakashi Iwai 			err = create_ch_ctls(codec, "Center", 1, true, path);
19674a79616dSTakashi Iwai 			if (err < 0)
19684a79616dSTakashi Iwai 				return err;
19693214b966STakashi Iwai 			err = create_ch_ctls(codec, "LFE", 2, true, path);
19704a79616dSTakashi Iwai 			if (err < 0)
19714a79616dSTakashi Iwai 				return err;
19724a79616dSTakashi Iwai 		} else {
19736aadf41dSTakashi Iwai 			const char *pfx = chname[i];
19746aadf41dSTakashi Iwai 			if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
19756aadf41dSTakashi Iwai 			    cfg->line_outs == 1)
19766aadf41dSTakashi Iwai 				pfx = "Speaker";
19773214b966STakashi Iwai 			err = create_ch_ctls(codec, pfx, 3, true, path);
19784a79616dSTakashi Iwai 			if (err < 0)
19794a79616dSTakashi Iwai 				return err;
19804a79616dSTakashi Iwai 		}
1981020066d1STakashi Iwai 		if (path != spec->out_path + i)
1982020066d1STakashi Iwai 			copy_path_mixer_ctls(&spec->out_path[i], path);
1983020066d1STakashi Iwai 		if (path == spec->out_path && spec->out_mix_path.depth)
1984020066d1STakashi Iwai 			copy_path_mixer_ctls(&spec->out_mix_path, path);
19854a79616dSTakashi Iwai 	}
19864a79616dSTakashi Iwai 
19874a79616dSTakashi Iwai 	idx = get_connection_index(codec, spec->aa_mix_nid,
19884a79616dSTakashi Iwai 				   spec->multiout.dac_nids[0]);
19894a79616dSTakashi Iwai 	if (idx >= 0) {
19904a79616dSTakashi Iwai 		/* add control to mixer */
19913214b966STakashi Iwai 		const char *name;
19923214b966STakashi Iwai 		name = spec->out_mix_path.depth ?
19933214b966STakashi Iwai 			"PCM Loopback Playback Volume" : "PCM Playback Volume";
19943214b966STakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
19954a79616dSTakashi Iwai 				      HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
19964a79616dSTakashi Iwai 							  idx, HDA_INPUT));
19974a79616dSTakashi Iwai 		if (err < 0)
19984a79616dSTakashi Iwai 			return err;
19993214b966STakashi Iwai 		name = spec->out_mix_path.depth ?
20003214b966STakashi Iwai 			"PCM Loopback Playback Switch" : "PCM Playback Switch";
20013214b966STakashi Iwai 		err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
20024a79616dSTakashi Iwai 				      HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
20034a79616dSTakashi Iwai 							  idx, HDA_INPUT));
20044a79616dSTakashi Iwai 		if (err < 0)
20054a79616dSTakashi Iwai 			return err;
2006c577b8a1SJoseph Chan 	}
2007c577b8a1SJoseph Chan 
2008f4a7828bSTakashi Iwai 	cfg->line_outs = old_line_outs;
2009f4a7828bSTakashi Iwai 
2010c577b8a1SJoseph Chan 	return 0;
2011c577b8a1SJoseph Chan }
2012c577b8a1SJoseph Chan 
20134a79616dSTakashi Iwai static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
2014c577b8a1SJoseph Chan {
20154a79616dSTakashi Iwai 	struct via_spec *spec = codec->spec;
201609a9ad69STakashi Iwai 	struct nid_path *path;
201718bd2c44STakashi Iwai 	bool check_dac;
20183214b966STakashi Iwai 	int i, err;
2019c577b8a1SJoseph Chan 
2020c577b8a1SJoseph Chan 	if (!pin)
2021c577b8a1SJoseph Chan 		return 0;
2022c577b8a1SJoseph Chan 
20233214b966STakashi Iwai 	if (!parse_output_path(codec, pin, 0, 0, &spec->hp_indep_path)) {
20243214b966STakashi Iwai 		for (i = HDA_SIDE; i >= HDA_CLFE; i--) {
20253214b966STakashi Iwai 			if (i < spec->multiout.num_dacs &&
202625250505STakashi Iwai 			    parse_output_path(codec, pin,
20273214b966STakashi Iwai 					      spec->multiout.dac_nids[i], 0,
20283214b966STakashi Iwai 					      &spec->hp_indep_path)) {
20293214b966STakashi Iwai 				spec->hp_indep_shared = i;
20303214b966STakashi Iwai 				break;
203125250505STakashi Iwai 			}
20323214b966STakashi Iwai 		}
20333214b966STakashi Iwai 	}
20343214b966STakashi Iwai 	if (spec->hp_indep_path.depth) {
20353214b966STakashi Iwai 		spec->hp_dac_nid = spec->hp_indep_path.path[0];
20363214b966STakashi Iwai 		if (!spec->hp_indep_shared)
20373214b966STakashi Iwai 			spec->hp_path = spec->hp_indep_path;
20383214b966STakashi Iwai 	}
20393214b966STakashi Iwai 	/* optionally check front-path w/o AA-mix */
20403214b966STakashi Iwai 	if (!spec->hp_path.depth)
20413214b966STakashi Iwai 		parse_output_path(codec, pin,
20423214b966STakashi Iwai 				  spec->multiout.dac_nids[HDA_FRONT], 0,
20433214b966STakashi Iwai 				  &spec->hp_path);
20444a79616dSTakashi Iwai 
2045ece8d043STakashi Iwai 	if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
20463214b966STakashi Iwai 			       1, &spec->hp_mix_path) && !spec->hp_path.depth)
2047ece8d043STakashi Iwai 		return 0;
2048ece8d043STakashi Iwai 
20493214b966STakashi Iwai 	if (spec->hp_path.depth) {
205009a9ad69STakashi Iwai 		path = &spec->hp_path;
205118bd2c44STakashi Iwai 		check_dac = true;
205218bd2c44STakashi Iwai 	} else {
20533214b966STakashi Iwai 		path = &spec->hp_mix_path;
205418bd2c44STakashi Iwai 		check_dac = false;
205518bd2c44STakashi Iwai 	}
205618bd2c44STakashi Iwai 	err = create_ch_ctls(codec, "Headphone", 3, check_dac, path);
20574a79616dSTakashi Iwai 	if (err < 0)
20584a79616dSTakashi Iwai 		return err;
2059020066d1STakashi Iwai 	if (check_dac)
2060020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->hp_mix_path, path);
2061020066d1STakashi Iwai 	else
2062020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->hp_path, path);
2063020066d1STakashi Iwai 	if (spec->hp_indep_path.depth)
2064020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->hp_indep_path, path);
2065c577b8a1SJoseph Chan 	return 0;
2066c577b8a1SJoseph Chan }
2067c577b8a1SJoseph Chan 
20684a918ffeSTakashi Iwai static int via_auto_create_speaker_ctls(struct hda_codec *codec)
20694a918ffeSTakashi Iwai {
20704a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
20713214b966STakashi Iwai 	struct nid_path *path;
20723214b966STakashi Iwai 	bool check_dac;
207381c0a78bSWang Shaoyan 	hda_nid_t pin, dac = 0;
20743214b966STakashi Iwai 	int err;
20754a918ffeSTakashi Iwai 
20764a918ffeSTakashi Iwai 	pin = spec->autocfg.speaker_pins[0];
20774a918ffeSTakashi Iwai 	if (!spec->autocfg.speaker_outs || !pin)
20784a918ffeSTakashi Iwai 		return 0;
20794a918ffeSTakashi Iwai 
20803214b966STakashi Iwai 	if (parse_output_path(codec, pin, 0, 0, &spec->speaker_path))
20818e3679dcSTakashi Iwai 		dac = spec->speaker_path.path[0];
20823214b966STakashi Iwai 	if (!dac)
20833214b966STakashi Iwai 		parse_output_path(codec, pin,
20843214b966STakashi Iwai 				  spec->multiout.dac_nids[HDA_FRONT], 0,
208509a9ad69STakashi Iwai 				  &spec->speaker_path);
20863214b966STakashi Iwai 	if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
20873214b966STakashi Iwai 			       1, &spec->speaker_mix_path) && !dac)
20883214b966STakashi Iwai 		return 0;
20894a918ffeSTakashi Iwai 
20903214b966STakashi Iwai 	/* no AA-path for front? */
20913214b966STakashi Iwai 	if (!spec->out_mix_path.depth && spec->speaker_mix_path.depth)
20923214b966STakashi Iwai 		dac = 0;
20933214b966STakashi Iwai 
20943214b966STakashi Iwai 	spec->speaker_dac_nid = dac;
20953214b966STakashi Iwai 	spec->multiout.extra_out_nid[0] = dac;
20963214b966STakashi Iwai 	if (dac) {
20973214b966STakashi Iwai 		path = &spec->speaker_path;
20983214b966STakashi Iwai 		check_dac = true;
20993214b966STakashi Iwai 	} else {
21003214b966STakashi Iwai 		path = &spec->speaker_mix_path;
21013214b966STakashi Iwai 		check_dac = false;
21023214b966STakashi Iwai 	}
21033214b966STakashi Iwai 	err = create_ch_ctls(codec, "Speaker", 3, check_dac, path);
21043214b966STakashi Iwai 	if (err < 0)
21053214b966STakashi Iwai 		return err;
2106020066d1STakashi Iwai 	if (check_dac)
2107020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->speaker_mix_path, path);
2108020066d1STakashi Iwai 	else
2109020066d1STakashi Iwai 		copy_path_mixer_ctls(&spec->speaker_path, path);
21103214b966STakashi Iwai 	return 0;
21113214b966STakashi Iwai }
21123214b966STakashi Iwai 
21133214b966STakashi Iwai #define via_aamix_ctl_info	via_pin_power_ctl_info
21143214b966STakashi Iwai 
21153214b966STakashi Iwai static int via_aamix_ctl_get(struct snd_kcontrol *kcontrol,
21163214b966STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
21173214b966STakashi Iwai {
21183214b966STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
21193214b966STakashi Iwai 	struct via_spec *spec = codec->spec;
21203214b966STakashi Iwai 	ucontrol->value.enumerated.item[0] = spec->aamix_mode;
21213214b966STakashi Iwai 	return 0;
21223214b966STakashi Iwai }
21233214b966STakashi Iwai 
21243214b966STakashi Iwai static void update_aamix_paths(struct hda_codec *codec, int do_mix,
21253214b966STakashi Iwai 			       struct nid_path *nomix, struct nid_path *mix)
21263214b966STakashi Iwai {
21273214b966STakashi Iwai 	if (do_mix) {
21283214b966STakashi Iwai 		activate_output_path(codec, nomix, false, false);
21293214b966STakashi Iwai 		activate_output_path(codec, mix, true, false);
21303214b966STakashi Iwai 	} else {
21313214b966STakashi Iwai 		activate_output_path(codec, mix, false, false);
21323214b966STakashi Iwai 		activate_output_path(codec, nomix, true, false);
21333214b966STakashi Iwai 	}
21343214b966STakashi Iwai }
21353214b966STakashi Iwai 
21363214b966STakashi Iwai static int via_aamix_ctl_put(struct snd_kcontrol *kcontrol,
21373214b966STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
21383214b966STakashi Iwai {
21393214b966STakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
21403214b966STakashi Iwai 	struct via_spec *spec = codec->spec;
21413214b966STakashi Iwai 	unsigned int val = ucontrol->value.enumerated.item[0];
21423214b966STakashi Iwai 
21433214b966STakashi Iwai 	if (val == spec->aamix_mode)
21443214b966STakashi Iwai 		return 0;
21453214b966STakashi Iwai 	spec->aamix_mode = val;
21463214b966STakashi Iwai 	/* update front path */
21473214b966STakashi Iwai 	update_aamix_paths(codec, val, &spec->out_path[0], &spec->out_mix_path);
21483214b966STakashi Iwai 	/* update HP path */
21493214b966STakashi Iwai 	if (!spec->hp_independent_mode) {
21503214b966STakashi Iwai 		update_aamix_paths(codec, val, &spec->hp_path,
21513214b966STakashi Iwai 				   &spec->hp_mix_path);
21523214b966STakashi Iwai 	}
21533214b966STakashi Iwai 	/* update speaker path */
21543214b966STakashi Iwai 	update_aamix_paths(codec, val, &spec->speaker_path,
21553214b966STakashi Iwai 			   &spec->speaker_mix_path);
21563214b966STakashi Iwai 	return 1;
21573214b966STakashi Iwai }
21583214b966STakashi Iwai 
21593214b966STakashi Iwai static const struct snd_kcontrol_new via_aamix_ctl_enum = {
21603214b966STakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
21613214b966STakashi Iwai 	.name = "Loopback Mixing",
21623214b966STakashi Iwai 	.info = via_aamix_ctl_info,
21633214b966STakashi Iwai 	.get = via_aamix_ctl_get,
21643214b966STakashi Iwai 	.put = via_aamix_ctl_put,
21653214b966STakashi Iwai };
21663214b966STakashi Iwai 
21673214b966STakashi Iwai static int via_auto_create_loopback_switch(struct hda_codec *codec)
21683214b966STakashi Iwai {
21693214b966STakashi Iwai 	struct via_spec *spec = codec->spec;
21703214b966STakashi Iwai 
21714808d12dSTakashi Iwai 	if (!spec->aa_mix_nid)
21724808d12dSTakashi Iwai 		return 0; /* no loopback switching available */
21734808d12dSTakashi Iwai 	if (!(spec->out_mix_path.depth || spec->hp_mix_path.depth ||
21744808d12dSTakashi Iwai 	      spec->speaker_path.depth))
21753214b966STakashi Iwai 		return 0; /* no loopback switching available */
21763214b966STakashi Iwai 	if (!via_clone_control(spec, &via_aamix_ctl_enum))
21773214b966STakashi Iwai 		return -ENOMEM;
21784a918ffeSTakashi Iwai 	return 0;
21794a918ffeSTakashi Iwai }
21804a918ffeSTakashi Iwai 
2181a766d0d7STakashi Iwai /* look for ADCs */
2182a766d0d7STakashi Iwai static int via_fill_adcs(struct hda_codec *codec)
2183a766d0d7STakashi Iwai {
2184a766d0d7STakashi Iwai 	struct via_spec *spec = codec->spec;
2185a766d0d7STakashi Iwai 	hda_nid_t nid = codec->start_nid;
2186a766d0d7STakashi Iwai 	int i;
2187a766d0d7STakashi Iwai 
2188a766d0d7STakashi Iwai 	for (i = 0; i < codec->num_nodes; i++, nid++) {
2189a766d0d7STakashi Iwai 		unsigned int wcaps = get_wcaps(codec, nid);
2190a766d0d7STakashi Iwai 		if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2191a766d0d7STakashi Iwai 			continue;
2192a766d0d7STakashi Iwai 		if (wcaps & AC_WCAP_DIGITAL)
2193a766d0d7STakashi Iwai 			continue;
2194a766d0d7STakashi Iwai 		if (!(wcaps & AC_WCAP_CONN_LIST))
2195a766d0d7STakashi Iwai 			continue;
2196a766d0d7STakashi Iwai 		if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
2197a766d0d7STakashi Iwai 			return -ENOMEM;
2198a766d0d7STakashi Iwai 		spec->adc_nids[spec->num_adc_nids++] = nid;
2199a766d0d7STakashi Iwai 	}
2200a766d0d7STakashi Iwai 	return 0;
2201a766d0d7STakashi Iwai }
2202a766d0d7STakashi Iwai 
2203a86a88eaSTakashi Iwai /* input-src control */
2204a86a88eaSTakashi Iwai static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
2205a86a88eaSTakashi Iwai 			     struct snd_ctl_elem_info *uinfo)
2206a86a88eaSTakashi Iwai {
2207a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2208a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2209a86a88eaSTakashi Iwai 
2210a86a88eaSTakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2211a86a88eaSTakashi Iwai 	uinfo->count = 1;
2212a86a88eaSTakashi Iwai 	uinfo->value.enumerated.items = spec->num_inputs;
2213a86a88eaSTakashi Iwai 	if (uinfo->value.enumerated.item >= spec->num_inputs)
2214a86a88eaSTakashi Iwai 		uinfo->value.enumerated.item = spec->num_inputs - 1;
2215a86a88eaSTakashi Iwai 	strcpy(uinfo->value.enumerated.name,
2216a86a88eaSTakashi Iwai 	       spec->inputs[uinfo->value.enumerated.item].label);
2217a86a88eaSTakashi Iwai 	return 0;
2218a86a88eaSTakashi Iwai }
2219a86a88eaSTakashi Iwai 
2220a86a88eaSTakashi Iwai static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
2221a86a88eaSTakashi Iwai 			    struct snd_ctl_elem_value *ucontrol)
2222a86a88eaSTakashi Iwai {
2223a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2224a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2225a86a88eaSTakashi Iwai 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2226a86a88eaSTakashi Iwai 
2227a86a88eaSTakashi Iwai 	ucontrol->value.enumerated.item[0] = spec->cur_mux[idx];
2228a86a88eaSTakashi Iwai 	return 0;
2229a86a88eaSTakashi Iwai }
2230a86a88eaSTakashi Iwai 
2231a86a88eaSTakashi Iwai static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
2232a86a88eaSTakashi Iwai 			    struct snd_ctl_elem_value *ucontrol)
2233a86a88eaSTakashi Iwai {
2234a86a88eaSTakashi Iwai 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2235a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2236a86a88eaSTakashi Iwai 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2237a86a88eaSTakashi Iwai 	hda_nid_t mux;
2238a86a88eaSTakashi Iwai 	int cur;
2239a86a88eaSTakashi Iwai 
2240a86a88eaSTakashi Iwai 	cur = ucontrol->value.enumerated.item[0];
2241a86a88eaSTakashi Iwai 	if (cur < 0 || cur >= spec->num_inputs)
2242a86a88eaSTakashi Iwai 		return -EINVAL;
2243a86a88eaSTakashi Iwai 	if (spec->cur_mux[idx] == cur)
2244a86a88eaSTakashi Iwai 		return 0;
2245a86a88eaSTakashi Iwai 	spec->cur_mux[idx] = cur;
2246a86a88eaSTakashi Iwai 	if (spec->dyn_adc_switch) {
2247a86a88eaSTakashi Iwai 		int adc_idx = spec->inputs[cur].adc_idx;
2248a86a88eaSTakashi Iwai 		mux = spec->mux_nids[adc_idx];
2249a86a88eaSTakashi Iwai 		via_dyn_adc_pcm_resetup(codec, cur);
2250a86a88eaSTakashi Iwai 	} else {
2251a86a88eaSTakashi Iwai 		mux = spec->mux_nids[idx];
2252a86a88eaSTakashi Iwai 		if (snd_BUG_ON(!mux))
2253a86a88eaSTakashi Iwai 			return -EINVAL;
2254a86a88eaSTakashi Iwai 	}
2255a86a88eaSTakashi Iwai 
2256a86a88eaSTakashi Iwai 	if (mux) {
2257a86a88eaSTakashi Iwai 		/* switch to D0 beofre change index */
2258054d867eSTakashi Iwai 		update_power_state(codec, mux, AC_PWRST_D0);
2259a86a88eaSTakashi Iwai 		snd_hda_codec_write(codec, mux, 0,
2260a86a88eaSTakashi Iwai 				    AC_VERB_SET_CONNECT_SEL,
2261a86a88eaSTakashi Iwai 				    spec->inputs[cur].mux_idx);
2262a86a88eaSTakashi Iwai 	}
2263a86a88eaSTakashi Iwai 
2264a86a88eaSTakashi Iwai 	/* update jack power state */
2265a86a88eaSTakashi Iwai 	set_widgets_power_state(codec);
2266a86a88eaSTakashi Iwai 	return 0;
2267a86a88eaSTakashi Iwai }
2268a766d0d7STakashi Iwai 
2269d7a99cceSTakashi Iwai static const struct snd_kcontrol_new via_input_src_ctl = {
2270d7a99cceSTakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2271d7a99cceSTakashi Iwai 	/* The multiple "Capture Source" controls confuse alsamixer
2272d7a99cceSTakashi Iwai 	 * So call somewhat different..
2273d7a99cceSTakashi Iwai 	 */
2274d7a99cceSTakashi Iwai 	/* .name = "Capture Source", */
2275d7a99cceSTakashi Iwai 	.name = "Input Source",
2276d7a99cceSTakashi Iwai 	.info = via_mux_enum_info,
2277d7a99cceSTakashi Iwai 	.get = via_mux_enum_get,
2278d7a99cceSTakashi Iwai 	.put = via_mux_enum_put,
2279d7a99cceSTakashi Iwai };
2280d7a99cceSTakashi Iwai 
2281a86a88eaSTakashi Iwai static int create_input_src_ctls(struct hda_codec *codec, int count)
2282a86a88eaSTakashi Iwai {
2283a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2284a86a88eaSTakashi Iwai 	struct snd_kcontrol_new *knew;
2285a86a88eaSTakashi Iwai 
2286a86a88eaSTakashi Iwai 	if (spec->num_inputs <= 1 || !count)
2287a86a88eaSTakashi Iwai 		return 0; /* no need for single src */
2288a86a88eaSTakashi Iwai 
2289a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_input_src_ctl);
2290a86a88eaSTakashi Iwai 	if (!knew)
2291a86a88eaSTakashi Iwai 		return -ENOMEM;
2292a86a88eaSTakashi Iwai 	knew->count = count;
2293a86a88eaSTakashi Iwai 	return 0;
2294a86a88eaSTakashi Iwai }
2295a86a88eaSTakashi Iwai 
2296a86a88eaSTakashi Iwai /* add the powersave loopback-list entry */
229713af8e77STakashi Iwai static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
229813af8e77STakashi Iwai {
229913af8e77STakashi Iwai 	struct hda_amp_list *list;
230013af8e77STakashi Iwai 
230113af8e77STakashi Iwai 	if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
230213af8e77STakashi Iwai 		return;
230313af8e77STakashi Iwai 	list = spec->loopback_list + spec->num_loopbacks;
230413af8e77STakashi Iwai 	list->nid = mix;
230513af8e77STakashi Iwai 	list->dir = HDA_INPUT;
230613af8e77STakashi Iwai 	list->idx = idx;
230713af8e77STakashi Iwai 	spec->num_loopbacks++;
230813af8e77STakashi Iwai 	spec->loopback.amplist = spec->loopback_list;
230913af8e77STakashi Iwai }
231013af8e77STakashi Iwai 
2311a86a88eaSTakashi Iwai static bool is_reachable_nid(struct hda_codec *codec, hda_nid_t src,
23128d087c76STakashi Iwai 			     hda_nid_t dst)
2313a86a88eaSTakashi Iwai {
23148d087c76STakashi Iwai 	return snd_hda_get_conn_index(codec, src, dst, 1) >= 0;
2315a86a88eaSTakashi Iwai }
2316a86a88eaSTakashi Iwai 
2317a86a88eaSTakashi Iwai /* add the input-route to the given pin */
2318a86a88eaSTakashi Iwai static bool add_input_route(struct hda_codec *codec, hda_nid_t pin)
2319c577b8a1SJoseph Chan {
232010a20af7STakashi Iwai 	struct via_spec *spec = codec->spec;
2321a86a88eaSTakashi Iwai 	int c, idx;
2322a86a88eaSTakashi Iwai 
2323a86a88eaSTakashi Iwai 	spec->inputs[spec->num_inputs].adc_idx = -1;
2324a86a88eaSTakashi Iwai 	spec->inputs[spec->num_inputs].pin = pin;
2325a86a88eaSTakashi Iwai 	for (c = 0; c < spec->num_adc_nids; c++) {
2326a86a88eaSTakashi Iwai 		if (spec->mux_nids[c]) {
2327a86a88eaSTakashi Iwai 			idx = get_connection_index(codec, spec->mux_nids[c],
2328a86a88eaSTakashi Iwai 						   pin);
2329a86a88eaSTakashi Iwai 			if (idx < 0)
2330a86a88eaSTakashi Iwai 				continue;
2331a86a88eaSTakashi Iwai 			spec->inputs[spec->num_inputs].mux_idx = idx;
2332a86a88eaSTakashi Iwai 		} else {
23338d087c76STakashi Iwai 			if (!is_reachable_nid(codec, spec->adc_nids[c], pin))
2334a86a88eaSTakashi Iwai 				continue;
2335a86a88eaSTakashi Iwai 		}
2336a86a88eaSTakashi Iwai 		spec->inputs[spec->num_inputs].adc_idx = c;
2337a86a88eaSTakashi Iwai 		/* Can primary ADC satisfy all inputs? */
2338a86a88eaSTakashi Iwai 		if (!spec->dyn_adc_switch &&
2339a86a88eaSTakashi Iwai 		    spec->num_inputs > 0 && spec->inputs[0].adc_idx != c) {
2340a86a88eaSTakashi Iwai 			snd_printd(KERN_INFO
2341a86a88eaSTakashi Iwai 				   "via: dynamic ADC switching enabled\n");
2342a86a88eaSTakashi Iwai 			spec->dyn_adc_switch = 1;
2343a86a88eaSTakashi Iwai 		}
2344a86a88eaSTakashi Iwai 		return true;
2345a86a88eaSTakashi Iwai 	}
2346a86a88eaSTakashi Iwai 	return false;
2347a86a88eaSTakashi Iwai }
2348a86a88eaSTakashi Iwai 
2349a86a88eaSTakashi Iwai static int get_mux_nids(struct hda_codec *codec);
2350a86a88eaSTakashi Iwai 
2351a86a88eaSTakashi Iwai /* parse input-routes; fill ADCs, MUXs and input-src entries */
2352a86a88eaSTakashi Iwai static int parse_analog_inputs(struct hda_codec *codec)
2353a86a88eaSTakashi Iwai {
2354a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2355a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2356a86a88eaSTakashi Iwai 	int i, err;
2357a766d0d7STakashi Iwai 
2358a766d0d7STakashi Iwai 	err = via_fill_adcs(codec);
2359a766d0d7STakashi Iwai 	if (err < 0)
2360a766d0d7STakashi Iwai 		return err;
2361a766d0d7STakashi Iwai 	err = get_mux_nids(codec);
2362a766d0d7STakashi Iwai 	if (err < 0)
2363a766d0d7STakashi Iwai 		return err;
2364a766d0d7STakashi Iwai 
2365a86a88eaSTakashi Iwai 	/* fill all input-routes */
2366a86a88eaSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2367a86a88eaSTakashi Iwai 		if (add_input_route(codec, cfg->inputs[i].pin))
2368a86a88eaSTakashi Iwai 			spec->inputs[spec->num_inputs++].label =
2369a86a88eaSTakashi Iwai 				hda_get_autocfg_input_label(codec, cfg, i);
2370a86a88eaSTakashi Iwai 	}
2371a86a88eaSTakashi Iwai 
2372a86a88eaSTakashi Iwai 	/* check for internal loopback recording */
2373a86a88eaSTakashi Iwai 	if (spec->aa_mix_nid &&
2374a86a88eaSTakashi Iwai 	    add_input_route(codec, spec->aa_mix_nid))
2375a86a88eaSTakashi Iwai 		spec->inputs[spec->num_inputs++].label = "Stereo Mixer";
2376a86a88eaSTakashi Iwai 
2377a86a88eaSTakashi Iwai 	return 0;
2378a86a88eaSTakashi Iwai }
2379a86a88eaSTakashi Iwai 
2380a86a88eaSTakashi Iwai /* create analog-loopback volume/switch controls */
2381a86a88eaSTakashi Iwai static int create_loopback_ctls(struct hda_codec *codec)
2382a86a88eaSTakashi Iwai {
2383a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2384a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2385a86a88eaSTakashi Iwai 	const char *prev_label = NULL;
2386a86a88eaSTakashi Iwai 	int type_idx = 0;
2387a86a88eaSTakashi Iwai 	int i, j, err, idx;
2388a86a88eaSTakashi Iwai 
2389a86a88eaSTakashi Iwai 	if (!spec->aa_mix_nid)
2390a766d0d7STakashi Iwai 		return 0;
2391c577b8a1SJoseph Chan 
23927b315bb4STakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2393a86a88eaSTakashi Iwai 		hda_nid_t pin = cfg->inputs[i].pin;
2394a86a88eaSTakashi Iwai 		const char *label = hda_get_autocfg_input_label(codec, cfg, i);
2395a86a88eaSTakashi Iwai 
23961e11cae1STakashi Iwai 		if (prev_label && !strcmp(label, prev_label))
23977b315bb4STakashi Iwai 			type_idx++;
23987b315bb4STakashi Iwai 		else
23997b315bb4STakashi Iwai 			type_idx = 0;
24001e11cae1STakashi Iwai 		prev_label = label;
2401a86a88eaSTakashi Iwai 		idx = get_connection_index(codec, spec->aa_mix_nid, pin);
2402a86a88eaSTakashi Iwai 		if (idx >= 0) {
240316922281SLydia Wang 			err = via_new_analog_input(spec, label, type_idx,
2404a86a88eaSTakashi Iwai 						   idx, spec->aa_mix_nid);
2405c577b8a1SJoseph Chan 			if (err < 0)
2406c577b8a1SJoseph Chan 				return err;
2407a86a88eaSTakashi Iwai 			add_loopback_list(spec, spec->aa_mix_nid, idx);
240813af8e77STakashi Iwai 		}
2409e3d7a143STakashi Iwai 
2410e3d7a143STakashi Iwai 		/* remember the label for smart51 control */
2411e3d7a143STakashi Iwai 		for (j = 0; j < spec->smart51_nums; j++) {
2412a86a88eaSTakashi Iwai 			if (spec->smart51_pins[j] == pin) {
2413e3d7a143STakashi Iwai 				spec->smart51_idxs[j] = idx;
2414e3d7a143STakashi Iwai 				spec->smart51_labels[j] = label;
2415e3d7a143STakashi Iwai 				break;
2416e3d7a143STakashi Iwai 			}
2417e3d7a143STakashi Iwai 		}
2418c577b8a1SJoseph Chan 	}
2419a86a88eaSTakashi Iwai 	return 0;
2420a86a88eaSTakashi Iwai }
2421a86a88eaSTakashi Iwai 
2422a86a88eaSTakashi Iwai /* create mic-boost controls (if present) */
2423a86a88eaSTakashi Iwai static int create_mic_boost_ctls(struct hda_codec *codec)
2424a86a88eaSTakashi Iwai {
2425a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2426a86a88eaSTakashi Iwai 	const struct auto_pin_cfg *cfg = &spec->autocfg;
24278d8bbc6fSTakashi Iwai 	const char *prev_label = NULL;
24288d8bbc6fSTakashi Iwai 	int type_idx = 0;
2429a86a88eaSTakashi Iwai 	int i, err;
2430a86a88eaSTakashi Iwai 
2431a86a88eaSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
2432a86a88eaSTakashi Iwai 		hda_nid_t pin = cfg->inputs[i].pin;
2433a86a88eaSTakashi Iwai 		unsigned int caps;
2434a86a88eaSTakashi Iwai 		const char *label;
2435a86a88eaSTakashi Iwai 		char name[32];
2436a86a88eaSTakashi Iwai 
2437a86a88eaSTakashi Iwai 		if (cfg->inputs[i].type != AUTO_PIN_MIC)
2438a86a88eaSTakashi Iwai 			continue;
2439a86a88eaSTakashi Iwai 		caps = query_amp_caps(codec, pin, HDA_INPUT);
2440a86a88eaSTakashi Iwai 		if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
2441a86a88eaSTakashi Iwai 			continue;
2442a86a88eaSTakashi Iwai 		label = hda_get_autocfg_input_label(codec, cfg, i);
24438d8bbc6fSTakashi Iwai 		if (prev_label && !strcmp(label, prev_label))
24448d8bbc6fSTakashi Iwai 			type_idx++;
24458d8bbc6fSTakashi Iwai 		else
24468d8bbc6fSTakashi Iwai 			type_idx = 0;
24478d8bbc6fSTakashi Iwai 		prev_label = label;
2448a86a88eaSTakashi Iwai 		snprintf(name, sizeof(name), "%s Boost Volume", label);
24498d8bbc6fSTakashi Iwai 		err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
2450a86a88eaSTakashi Iwai 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
2451a86a88eaSTakashi Iwai 		if (err < 0)
2452a86a88eaSTakashi Iwai 			return err;
2453a86a88eaSTakashi Iwai 	}
2454a86a88eaSTakashi Iwai 	return 0;
2455a86a88eaSTakashi Iwai }
2456a86a88eaSTakashi Iwai 
2457a86a88eaSTakashi Iwai /* create capture and input-src controls for multiple streams */
2458a86a88eaSTakashi Iwai static int create_multi_adc_ctls(struct hda_codec *codec)
2459a86a88eaSTakashi Iwai {
2460a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2461a86a88eaSTakashi Iwai 	int i, err;
2462d7a99cceSTakashi Iwai 
2463d7a99cceSTakashi Iwai 	/* create capture mixer elements */
2464d7a99cceSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
2465d7a99cceSTakashi Iwai 		hda_nid_t adc = spec->adc_nids[i];
2466d7a99cceSTakashi Iwai 		err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
2467d7a99cceSTakashi Iwai 					"Capture Volume", i,
2468d7a99cceSTakashi Iwai 					HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2469d7a99cceSTakashi Iwai 							    HDA_INPUT));
2470d7a99cceSTakashi Iwai 		if (err < 0)
2471d7a99cceSTakashi Iwai 			return err;
2472d7a99cceSTakashi Iwai 		err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2473d7a99cceSTakashi Iwai 					"Capture Switch", i,
2474d7a99cceSTakashi Iwai 					HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2475d7a99cceSTakashi Iwai 							    HDA_INPUT));
2476d7a99cceSTakashi Iwai 		if (err < 0)
2477d7a99cceSTakashi Iwai 			return err;
2478d7a99cceSTakashi Iwai 	}
2479d7a99cceSTakashi Iwai 
2480d7a99cceSTakashi Iwai 	/* input-source control */
2481d7a99cceSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++)
2482d7a99cceSTakashi Iwai 		if (!spec->mux_nids[i])
2483d7a99cceSTakashi Iwai 			break;
2484a86a88eaSTakashi Iwai 	err = create_input_src_ctls(codec, i);
2485d7a99cceSTakashi Iwai 	if (err < 0)
2486d7a99cceSTakashi Iwai 		return err;
2487a86a88eaSTakashi Iwai 	return 0;
2488d7a99cceSTakashi Iwai }
2489d7a99cceSTakashi Iwai 
2490a86a88eaSTakashi Iwai /* bind capture volume/switch */
2491a86a88eaSTakashi Iwai static struct snd_kcontrol_new via_bind_cap_vol_ctl =
2492a86a88eaSTakashi Iwai 	HDA_BIND_VOL("Capture Volume", 0);
2493a86a88eaSTakashi Iwai static struct snd_kcontrol_new via_bind_cap_sw_ctl =
2494a86a88eaSTakashi Iwai 	HDA_BIND_SW("Capture Switch", 0);
2495a86a88eaSTakashi Iwai 
2496a86a88eaSTakashi Iwai static int init_bind_ctl(struct via_spec *spec, struct hda_bind_ctls **ctl_ret,
2497a86a88eaSTakashi Iwai 			 struct hda_ctl_ops *ops)
2498a86a88eaSTakashi Iwai {
2499a86a88eaSTakashi Iwai 	struct hda_bind_ctls *ctl;
2500a86a88eaSTakashi Iwai 	int i;
2501a86a88eaSTakashi Iwai 
2502a86a88eaSTakashi Iwai 	ctl = kzalloc(sizeof(*ctl) + sizeof(long) * 4, GFP_KERNEL);
2503a86a88eaSTakashi Iwai 	if (!ctl)
2504a86a88eaSTakashi Iwai 		return -ENOMEM;
2505a86a88eaSTakashi Iwai 	ctl->ops = ops;
2506a86a88eaSTakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++)
2507a86a88eaSTakashi Iwai 		ctl->values[i] =
2508a86a88eaSTakashi Iwai 			HDA_COMPOSE_AMP_VAL(spec->adc_nids[i], 3, 0, HDA_INPUT);
2509a86a88eaSTakashi Iwai 	*ctl_ret = ctl;
2510a86a88eaSTakashi Iwai 	return 0;
2511a86a88eaSTakashi Iwai }
2512a86a88eaSTakashi Iwai 
2513a86a88eaSTakashi Iwai /* create capture and input-src controls for dynamic ADC-switch case */
2514a86a88eaSTakashi Iwai static int create_dyn_adc_ctls(struct hda_codec *codec)
2515a86a88eaSTakashi Iwai {
2516a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2517a86a88eaSTakashi Iwai 	struct snd_kcontrol_new *knew;
2518a86a88eaSTakashi Iwai 	int err;
2519a86a88eaSTakashi Iwai 
2520a86a88eaSTakashi Iwai 	/* set up the bind capture ctls */
2521a86a88eaSTakashi Iwai 	err = init_bind_ctl(spec, &spec->bind_cap_vol, &snd_hda_bind_vol);
2522a86a88eaSTakashi Iwai 	if (err < 0)
2523a86a88eaSTakashi Iwai 		return err;
2524a86a88eaSTakashi Iwai 	err = init_bind_ctl(spec, &spec->bind_cap_sw, &snd_hda_bind_sw);
2525a86a88eaSTakashi Iwai 	if (err < 0)
2526a86a88eaSTakashi Iwai 		return err;
2527a86a88eaSTakashi Iwai 
2528a86a88eaSTakashi Iwai 	/* create capture mixer elements */
2529a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_bind_cap_vol_ctl);
2530a86a88eaSTakashi Iwai 	if (!knew)
2531a86a88eaSTakashi Iwai 		return -ENOMEM;
2532a86a88eaSTakashi Iwai 	knew->private_value = (long)spec->bind_cap_vol;
2533a86a88eaSTakashi Iwai 
2534a86a88eaSTakashi Iwai 	knew = via_clone_control(spec, &via_bind_cap_sw_ctl);
2535a86a88eaSTakashi Iwai 	if (!knew)
2536a86a88eaSTakashi Iwai 		return -ENOMEM;
2537a86a88eaSTakashi Iwai 	knew->private_value = (long)spec->bind_cap_sw;
2538a86a88eaSTakashi Iwai 
2539a86a88eaSTakashi Iwai 	/* input-source control */
2540a86a88eaSTakashi Iwai 	err = create_input_src_ctls(codec, 1);
2541a86a88eaSTakashi Iwai 	if (err < 0)
2542a86a88eaSTakashi Iwai 		return err;
2543a86a88eaSTakashi Iwai 	return 0;
2544a86a88eaSTakashi Iwai }
2545a86a88eaSTakashi Iwai 
2546a86a88eaSTakashi Iwai /* parse and create capture-related stuff */
2547a86a88eaSTakashi Iwai static int via_auto_create_analog_input_ctls(struct hda_codec *codec)
2548a86a88eaSTakashi Iwai {
2549a86a88eaSTakashi Iwai 	struct via_spec *spec = codec->spec;
2550a86a88eaSTakashi Iwai 	int err;
2551a86a88eaSTakashi Iwai 
2552a86a88eaSTakashi Iwai 	err = parse_analog_inputs(codec);
2553a86a88eaSTakashi Iwai 	if (err < 0)
2554a86a88eaSTakashi Iwai 		return err;
2555a86a88eaSTakashi Iwai 	if (spec->dyn_adc_switch)
2556a86a88eaSTakashi Iwai 		err = create_dyn_adc_ctls(codec);
2557a86a88eaSTakashi Iwai 	else
2558a86a88eaSTakashi Iwai 		err = create_multi_adc_ctls(codec);
2559a86a88eaSTakashi Iwai 	if (err < 0)
2560a86a88eaSTakashi Iwai 		return err;
2561a86a88eaSTakashi Iwai 	err = create_loopback_ctls(codec);
2562a86a88eaSTakashi Iwai 	if (err < 0)
2563a86a88eaSTakashi Iwai 		return err;
2564a86a88eaSTakashi Iwai 	err = create_mic_boost_ctls(codec);
2565a86a88eaSTakashi Iwai 	if (err < 0)
2566a86a88eaSTakashi Iwai 		return err;
2567c577b8a1SJoseph Chan 	return 0;
2568c577b8a1SJoseph Chan }
2569c577b8a1SJoseph Chan 
257076d9b0ddSHarald Welte static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
257176d9b0ddSHarald Welte {
257276d9b0ddSHarald Welte 	unsigned int def_conf;
257376d9b0ddSHarald Welte 	unsigned char seqassoc;
257476d9b0ddSHarald Welte 
25752f334f92STakashi Iwai 	def_conf = snd_hda_codec_get_pincfg(codec, nid);
257676d9b0ddSHarald Welte 	seqassoc = (unsigned char) get_defcfg_association(def_conf);
257776d9b0ddSHarald Welte 	seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
257882ef9e45SLydia Wang 	if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
257982ef9e45SLydia Wang 	    && (seqassoc == 0xf0 || seqassoc == 0xff)) {
258076d9b0ddSHarald Welte 		def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
25812f334f92STakashi Iwai 		snd_hda_codec_set_pincfg(codec, nid, def_conf);
258276d9b0ddSHarald Welte 	}
258376d9b0ddSHarald Welte 
258476d9b0ddSHarald Welte 	return;
258576d9b0ddSHarald Welte }
258676d9b0ddSHarald Welte 
2587e06e5a29STakashi Iwai static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
25881f2e99feSLydia Wang 				     struct snd_ctl_elem_value *ucontrol)
25891f2e99feSLydia Wang {
25901f2e99feSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
25911f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
25921f2e99feSLydia Wang 
25931f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
25941f2e99feSLydia Wang 		return 0;
2595e06e5a29STakashi Iwai 	ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
25961f2e99feSLydia Wang 	return 0;
25971f2e99feSLydia Wang }
25981f2e99feSLydia Wang 
2599e06e5a29STakashi Iwai static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
26001f2e99feSLydia Wang 				     struct snd_ctl_elem_value *ucontrol)
26011f2e99feSLydia Wang {
26021f2e99feSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
26031f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
2604187d333eSTakashi Iwai 	int val;
26051f2e99feSLydia Wang 
26061f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
26071f2e99feSLydia Wang 		return 0;
2608187d333eSTakashi Iwai 	val = !!ucontrol->value.integer.value[0];
2609187d333eSTakashi Iwai 	if (spec->vt1708_jack_detect == val)
2610187d333eSTakashi Iwai 		return 0;
2611187d333eSTakashi Iwai 	spec->vt1708_jack_detect = val;
2612187d333eSTakashi Iwai 	if (spec->vt1708_jack_detect &&
2613187d333eSTakashi Iwai 	    snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") != 1) {
26141f2e99feSLydia Wang 		mute_aa_path(codec, 1);
26151f2e99feSLydia Wang 		notify_aa_path_ctls(codec);
26161f2e99feSLydia Wang 	}
2617187d333eSTakashi Iwai 	via_hp_automute(codec);
2618187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
2619187d333eSTakashi Iwai 	return 1;
26201f2e99feSLydia Wang }
26211f2e99feSLydia Wang 
2622e06e5a29STakashi Iwai static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
26231f2e99feSLydia Wang 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
26241f2e99feSLydia Wang 	.name = "Jack Detect",
26251f2e99feSLydia Wang 	.count = 1,
26261f2e99feSLydia Wang 	.info = snd_ctl_boolean_mono_info,
2627e06e5a29STakashi Iwai 	.get = vt1708_jack_detect_get,
2628e06e5a29STakashi Iwai 	.put = vt1708_jack_detect_put,
26291f2e99feSLydia Wang };
26301f2e99feSLydia Wang 
263112daef65STakashi Iwai static void fill_dig_outs(struct hda_codec *codec);
263212daef65STakashi Iwai static void fill_dig_in(struct hda_codec *codec);
263312daef65STakashi Iwai 
263412daef65STakashi Iwai static int via_parse_auto_config(struct hda_codec *codec)
2635c577b8a1SJoseph Chan {
2636c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
2637c577b8a1SJoseph Chan 	int err;
2638c577b8a1SJoseph Chan 
2639c577b8a1SJoseph Chan 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2640c577b8a1SJoseph Chan 	if (err < 0)
2641c577b8a1SJoseph Chan 		return err;
2642c577b8a1SJoseph Chan 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
26437f0df88cSTakashi Iwai 		return -EINVAL;
2644c577b8a1SJoseph Chan 
26454a79616dSTakashi Iwai 	err = via_auto_create_multi_out_ctls(codec);
2646c577b8a1SJoseph Chan 	if (err < 0)
2647c577b8a1SJoseph Chan 		return err;
26484a79616dSTakashi Iwai 	err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
2649c577b8a1SJoseph Chan 	if (err < 0)
2650c577b8a1SJoseph Chan 		return err;
26514a918ffeSTakashi Iwai 	err = via_auto_create_speaker_ctls(codec);
26524a918ffeSTakashi Iwai 	if (err < 0)
26534a918ffeSTakashi Iwai 		return err;
26543214b966STakashi Iwai 	err = via_auto_create_loopback_switch(codec);
26553214b966STakashi Iwai 	if (err < 0)
26563214b966STakashi Iwai 		return err;
2657a86a88eaSTakashi Iwai 	err = via_auto_create_analog_input_ctls(codec);
2658c577b8a1SJoseph Chan 	if (err < 0)
2659c577b8a1SJoseph Chan 		return err;
2660c577b8a1SJoseph Chan 
2661c577b8a1SJoseph Chan 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2662c577b8a1SJoseph Chan 
266312daef65STakashi Iwai 	fill_dig_outs(codec);
266412daef65STakashi Iwai 	fill_dig_in(codec);
2665c577b8a1SJoseph Chan 
2666603c4019STakashi Iwai 	if (spec->kctls.list)
2667603c4019STakashi Iwai 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
2668c577b8a1SJoseph Chan 
2669c577b8a1SJoseph Chan 
26703214b966STakashi Iwai 	if (spec->hp_dac_nid && spec->hp_mix_path.depth) {
2671ece8d043STakashi Iwai 		err = via_hp_build(codec);
2672ece8d043STakashi Iwai 		if (err < 0)
2673ece8d043STakashi Iwai 			return err;
2674ece8d043STakashi Iwai 	}
2675c577b8a1SJoseph Chan 
2676f4a7828bSTakashi Iwai 	err = via_smart51_build(codec);
2677f4a7828bSTakashi Iwai 	if (err < 0)
2678f4a7828bSTakashi Iwai 		return err;
2679f4a7828bSTakashi Iwai 
26805d41762aSTakashi Iwai 	/* assign slave outs */
26815d41762aSTakashi Iwai 	if (spec->slave_dig_outs[0])
26825d41762aSTakashi Iwai 		codec->slave_dig_outs = spec->slave_dig_outs;
26835d41762aSTakashi Iwai 
2684c577b8a1SJoseph Chan 	return 1;
2685c577b8a1SJoseph Chan }
2686c577b8a1SJoseph Chan 
26875d41762aSTakashi Iwai static void via_auto_init_dig_outs(struct hda_codec *codec)
2688c577b8a1SJoseph Chan {
268925eaba2fSLydia Wang 	struct via_spec *spec = codec->spec;
26905d41762aSTakashi Iwai 	if (spec->multiout.dig_out_nid)
26915d41762aSTakashi Iwai 		init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
26925d41762aSTakashi Iwai 	if (spec->slave_dig_outs[0])
26935d41762aSTakashi Iwai 		init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
26945d41762aSTakashi Iwai }
269525eaba2fSLydia Wang 
26965d41762aSTakashi Iwai static void via_auto_init_dig_in(struct hda_codec *codec)
26975d41762aSTakashi Iwai {
26985d41762aSTakashi Iwai 	struct via_spec *spec = codec->spec;
26995d41762aSTakashi Iwai 	if (!spec->dig_in_nid)
27005d41762aSTakashi Iwai 		return;
2701cdd03cedSTakashi Iwai 	snd_hda_set_pin_ctl(codec, spec->autocfg.dig_in_pin, PIN_IN);
27025d41762aSTakashi Iwai }
27035d41762aSTakashi Iwai 
27044e2d16d3SDavid Henningsson static void via_jack_output_event(struct hda_codec *codec, struct hda_jack_tbl *tbl)
27054e2d16d3SDavid Henningsson {
27064e2d16d3SDavid Henningsson 	set_widgets_power_state(codec);
27074e2d16d3SDavid Henningsson 	via_hp_automute(codec);
27084e2d16d3SDavid Henningsson }
27094e2d16d3SDavid Henningsson 
27104e2d16d3SDavid Henningsson static void via_jack_powerstate_event(struct hda_codec *codec, struct hda_jack_tbl *tbl)
27114e2d16d3SDavid Henningsson {
27124e2d16d3SDavid Henningsson 	set_widgets_power_state(codec);
27134e2d16d3SDavid Henningsson }
27144e2d16d3SDavid Henningsson 
27154a918ffeSTakashi Iwai /* initialize the unsolicited events */
27164a918ffeSTakashi Iwai static void via_auto_init_unsol_event(struct hda_codec *codec)
27174a918ffeSTakashi Iwai {
27184a918ffeSTakashi Iwai 	struct via_spec *spec = codec->spec;
27194a918ffeSTakashi Iwai 	struct auto_pin_cfg *cfg = &spec->autocfg;
27204a918ffeSTakashi Iwai 	unsigned int ev;
27214a918ffeSTakashi Iwai 	int i;
27224e2d16d3SDavid Henningsson 	hda_jack_callback cb;
27234a918ffeSTakashi Iwai 
27244a918ffeSTakashi Iwai 	if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
27254e2d16d3SDavid Henningsson 		snd_hda_jack_detect_enable_callback(codec, cfg->hp_pins[0],
27264e2d16d3SDavid Henningsson 						    VIA_HP_EVENT | VIA_JACK_EVENT,
27274e2d16d3SDavid Henningsson 						    via_jack_output_event);
27284a918ffeSTakashi Iwai 
27294a918ffeSTakashi Iwai 	if (cfg->speaker_pins[0])
27304a918ffeSTakashi Iwai 		ev = VIA_LINE_EVENT;
27314a918ffeSTakashi Iwai 	else
27324a918ffeSTakashi Iwai 		ev = 0;
27334e2d16d3SDavid Henningsson 	cb = ev ? via_jack_output_event : via_jack_powerstate_event;
27344e2d16d3SDavid Henningsson 
27354a918ffeSTakashi Iwai 	for (i = 0; i < cfg->line_outs; i++) {
27364a918ffeSTakashi Iwai 		if (cfg->line_out_pins[i] &&
27374a918ffeSTakashi Iwai 		    is_jack_detectable(codec, cfg->line_out_pins[i]))
27384e2d16d3SDavid Henningsson 			snd_hda_jack_detect_enable_callback(codec, cfg->line_out_pins[i],
27394e2d16d3SDavid Henningsson 							    ev | VIA_JACK_EVENT, cb);
27404a918ffeSTakashi Iwai 	}
27414a918ffeSTakashi Iwai 
27424a918ffeSTakashi Iwai 	for (i = 0; i < cfg->num_inputs; i++) {
27434a918ffeSTakashi Iwai 		if (is_jack_detectable(codec, cfg->inputs[i].pin))
27444e2d16d3SDavid Henningsson 			snd_hda_jack_detect_enable_callback(codec, cfg->inputs[i].pin,
27454e2d16d3SDavid Henningsson 							    VIA_JACK_EVENT,
27464e2d16d3SDavid Henningsson 							    via_jack_powerstate_event);
27474a918ffeSTakashi Iwai 	}
27484a918ffeSTakashi Iwai }
27494a918ffeSTakashi Iwai 
27505d41762aSTakashi Iwai static int via_init(struct hda_codec *codec)
27515d41762aSTakashi Iwai {
27525d41762aSTakashi Iwai 	struct via_spec *spec = codec->spec;
27535d41762aSTakashi Iwai 	int i;
27545d41762aSTakashi Iwai 
27555d41762aSTakashi Iwai 	for (i = 0; i < spec->num_iverbs; i++)
27565d41762aSTakashi Iwai 		snd_hda_sequence_write(codec, spec->init_verbs[i]);
27575d41762aSTakashi Iwai 
2758e9d010c2STakashi Iwai 	/* init power states */
2759e9d010c2STakashi Iwai 	set_widgets_power_state(codec);
2760e9d010c2STakashi Iwai 	__analog_low_current_mode(codec, true);
2761e9d010c2STakashi Iwai 
2762c577b8a1SJoseph Chan 	via_auto_init_multi_out(codec);
2763c577b8a1SJoseph Chan 	via_auto_init_hp_out(codec);
27644a918ffeSTakashi Iwai 	via_auto_init_speaker_out(codec);
2765c577b8a1SJoseph Chan 	via_auto_init_analog_input(codec);
27665d41762aSTakashi Iwai 	via_auto_init_dig_outs(codec);
27675d41762aSTakashi Iwai 	via_auto_init_dig_in(codec);
276811890956SLydia Wang 
27694a918ffeSTakashi Iwai 	via_auto_init_unsol_event(codec);
27704a918ffeSTakashi Iwai 
277125eaba2fSLydia Wang 	via_hp_automute(codec);
2772187d333eSTakashi Iwai 	vt1708_update_hp_work(spec);
277325eaba2fSLydia Wang 
2774c577b8a1SJoseph Chan 	return 0;
2775c577b8a1SJoseph Chan }
2776c577b8a1SJoseph Chan 
27771f2e99feSLydia Wang static void vt1708_update_hp_jack_state(struct work_struct *work)
27781f2e99feSLydia Wang {
27791f2e99feSLydia Wang 	struct via_spec *spec = container_of(work, struct via_spec,
27801f2e99feSLydia Wang 					     vt1708_hp_work.work);
27811f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
27821f2e99feSLydia Wang 		return;
27831835a0f9STakashi Iwai 	snd_hda_jack_set_dirty_all(spec->codec);
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 	}
2790187d333eSTakashi Iwai 	if (spec->vt1708_jack_detect)
2791187d333eSTakashi Iwai 		schedule_delayed_work(&spec->vt1708_hp_work,
2792187d333eSTakashi Iwai 				      msecs_to_jiffies(100));
27931f2e99feSLydia Wang }
27941f2e99feSLydia Wang 
2795337b9d02STakashi Iwai static int get_mux_nids(struct hda_codec *codec)
2796337b9d02STakashi Iwai {
2797337b9d02STakashi Iwai 	struct via_spec *spec = codec->spec;
2798337b9d02STakashi Iwai 	hda_nid_t nid, conn[8];
2799337b9d02STakashi Iwai 	unsigned int type;
2800337b9d02STakashi Iwai 	int i, n;
2801337b9d02STakashi Iwai 
2802337b9d02STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
2803337b9d02STakashi Iwai 		nid = spec->adc_nids[i];
2804337b9d02STakashi Iwai 		while (nid) {
2805a22d543aSTakashi Iwai 			type = get_wcaps_type(get_wcaps(codec, nid));
28061c55d521STakashi Iwai 			if (type == AC_WID_PIN)
28071c55d521STakashi Iwai 				break;
2808337b9d02STakashi Iwai 			n = snd_hda_get_connections(codec, nid, conn,
2809337b9d02STakashi Iwai 						    ARRAY_SIZE(conn));
2810337b9d02STakashi Iwai 			if (n <= 0)
2811337b9d02STakashi Iwai 				break;
2812337b9d02STakashi Iwai 			if (n > 1) {
2813337b9d02STakashi Iwai 				spec->mux_nids[i] = nid;
2814337b9d02STakashi Iwai 				break;
2815337b9d02STakashi Iwai 			}
2816337b9d02STakashi Iwai 			nid = conn[0];
2817337b9d02STakashi Iwai 		}
2818337b9d02STakashi Iwai 	}
28191c55d521STakashi Iwai 	return 0;
2820337b9d02STakashi Iwai }
2821337b9d02STakashi Iwai 
2822c577b8a1SJoseph Chan static int patch_vt1708(struct hda_codec *codec)
2823c577b8a1SJoseph Chan {
2824c577b8a1SJoseph Chan 	struct via_spec *spec;
2825c577b8a1SJoseph Chan 	int err;
2826c577b8a1SJoseph Chan 
2827c577b8a1SJoseph Chan 	/* create a codec specific record */
28285b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2829c577b8a1SJoseph Chan 	if (spec == NULL)
2830c577b8a1SJoseph Chan 		return -ENOMEM;
2831c577b8a1SJoseph Chan 
2832620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x17;
2833620e2b28STakashi Iwai 
283412daef65STakashi Iwai 	/* Add HP and CD pin config connect bit re-config action */
283512daef65STakashi Iwai 	vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
283612daef65STakashi Iwai 	vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
283712daef65STakashi Iwai 
2838c577b8a1SJoseph Chan 	/* automatic parse from the BIOS config */
283912daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2840c577b8a1SJoseph Chan 	if (err < 0) {
2841c577b8a1SJoseph Chan 		via_free(codec);
2842c577b8a1SJoseph Chan 		return err;
2843c577b8a1SJoseph Chan 	}
2844c577b8a1SJoseph Chan 
284512daef65STakashi Iwai 	/* add jack detect on/off control */
284612daef65STakashi Iwai 	if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
284712daef65STakashi Iwai 		return -ENOMEM;
284812daef65STakashi Iwai 
2849bc9b5623STakashi Iwai 	/* disable 32bit format on VT1708 */
2850bc9b5623STakashi Iwai 	if (codec->vendor_id == 0x11061708)
2851bc9b5623STakashi Iwai 		spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
2852c577b8a1SJoseph Chan 
2853e322a36dSLydia Wang 	spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
2854e322a36dSLydia Wang 
2855c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
2856c577b8a1SJoseph Chan 
28571f2e99feSLydia Wang 	INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
2858c577b8a1SJoseph Chan 	return 0;
2859c577b8a1SJoseph Chan }
2860c577b8a1SJoseph Chan 
2861ddd304d8STakashi Iwai static int patch_vt1709(struct hda_codec *codec)
2862c577b8a1SJoseph Chan {
2863c577b8a1SJoseph Chan 	struct via_spec *spec;
2864c577b8a1SJoseph Chan 	int err;
2865c577b8a1SJoseph Chan 
2866c577b8a1SJoseph Chan 	/* create a codec specific record */
28675b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2868c577b8a1SJoseph Chan 	if (spec == NULL)
2869c577b8a1SJoseph Chan 		return -ENOMEM;
2870c577b8a1SJoseph Chan 
2871620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x18;
2872620e2b28STakashi Iwai 
287312daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2874c577b8a1SJoseph Chan 	if (err < 0) {
2875c577b8a1SJoseph Chan 		via_free(codec);
2876c577b8a1SJoseph Chan 		return err;
2877c577b8a1SJoseph Chan 	}
2878c577b8a1SJoseph Chan 
2879c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
2880c577b8a1SJoseph Chan 
2881f7278fd0SJosepch Chan 	return 0;
2882f7278fd0SJosepch Chan }
2883f7278fd0SJosepch Chan 
28843e95b9abSLydia Wang static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
28853e95b9abSLydia Wang {
28863e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
28873e95b9abSLydia Wang 	int imux_is_smixer;
28883e95b9abSLydia Wang 	unsigned int parm;
28893e95b9abSLydia Wang 	int is_8ch = 0;
2890bc92df7fSLydia Wang 	if ((spec->codec_type != VT1708B_4CH) &&
2891bc92df7fSLydia Wang 	    (codec->vendor_id != 0x11064397))
28923e95b9abSLydia Wang 		is_8ch = 1;
28933e95b9abSLydia Wang 
28943e95b9abSLydia Wang 	/* SW0 (17h) = stereo mixer */
28953e95b9abSLydia Wang 	imux_is_smixer =
28963e95b9abSLydia Wang 	(snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
28973e95b9abSLydia Wang 	 == ((spec->codec_type == VT1708S) ? 5 : 0));
28983e95b9abSLydia Wang 	/* inputs */
28993e95b9abSLydia Wang 	/* PW 1/2/5 (1ah/1bh/1eh) */
29003e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29013e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1a, &parm);
29023e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1b, &parm);
29033e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
29043e95b9abSLydia Wang 	if (imux_is_smixer)
29053e95b9abSLydia Wang 		parm = AC_PWRST_D0;
29063e95b9abSLydia Wang 	/* SW0 (17h), AIW 0/1 (13h/14h) */
2907054d867eSTakashi Iwai 	update_power_state(codec, 0x17, parm);
2908054d867eSTakashi Iwai 	update_power_state(codec, 0x13, parm);
2909054d867eSTakashi Iwai 	update_power_state(codec, 0x14, parm);
29103e95b9abSLydia Wang 
29113e95b9abSLydia Wang 	/* outputs */
29123e95b9abSLydia Wang 	/* PW0 (19h), SW1 (18h), AOW1 (11h) */
29133e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29143e95b9abSLydia Wang 	set_pin_power_state(codec, 0x19, &parm);
29153e95b9abSLydia Wang 	if (spec->smart51_enabled)
29163e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1b, &parm);
2917054d867eSTakashi Iwai 	update_power_state(codec, 0x18, parm);
2918054d867eSTakashi Iwai 	update_power_state(codec, 0x11, parm);
29193e95b9abSLydia Wang 
29203e95b9abSLydia Wang 	/* PW6 (22h), SW2 (26h), AOW2 (24h) */
29213e95b9abSLydia Wang 	if (is_8ch) {
29223e95b9abSLydia Wang 		parm = AC_PWRST_D3;
29233e95b9abSLydia Wang 		set_pin_power_state(codec, 0x22, &parm);
29243e95b9abSLydia Wang 		if (spec->smart51_enabled)
29253e95b9abSLydia Wang 			set_pin_power_state(codec, 0x1a, &parm);
2926054d867eSTakashi Iwai 		update_power_state(codec, 0x26, parm);
2927054d867eSTakashi Iwai 		update_power_state(codec, 0x24, 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);
2934054d867eSTakashi Iwai 		update_power_state(codec, 0x27, parm);
2935054d867eSTakashi Iwai 		update_power_state(codec, 0x25, parm);
29363e95b9abSLydia Wang 	}
29373e95b9abSLydia Wang 
29383e95b9abSLydia Wang 	/* PW 3/4/7 (1ch/1dh/23h) */
29393e95b9abSLydia Wang 	parm = AC_PWRST_D3;
29403e95b9abSLydia Wang 	/* force to D0 for internal Speaker */
29413e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1c, &parm);
29423e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1d, &parm);
29433e95b9abSLydia Wang 	if (is_8ch)
29443e95b9abSLydia Wang 		set_pin_power_state(codec, 0x23, &parm);
29453e95b9abSLydia Wang 
29463e95b9abSLydia Wang 	/* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
2947054d867eSTakashi Iwai 	update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
2948054d867eSTakashi Iwai 	update_power_state(codec, 0x10, parm);
29493e95b9abSLydia Wang 	if (is_8ch) {
2950054d867eSTakashi Iwai 		update_power_state(codec, 0x25, parm);
2951054d867eSTakashi Iwai 		update_power_state(codec, 0x27, parm);
2952bc92df7fSLydia Wang 	} else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
2953054d867eSTakashi Iwai 		update_power_state(codec, 0x25, parm);
29543e95b9abSLydia Wang }
29553e95b9abSLydia Wang 
2956518bf3baSLydia Wang static int patch_vt1708S(struct hda_codec *codec);
2957ddd304d8STakashi Iwai static int patch_vt1708B(struct hda_codec *codec)
2958f7278fd0SJosepch Chan {
2959f7278fd0SJosepch Chan 	struct via_spec *spec;
2960f7278fd0SJosepch Chan 	int err;
2961f7278fd0SJosepch Chan 
2962518bf3baSLydia Wang 	if (get_codec_type(codec) == VT1708BCE)
2963518bf3baSLydia Wang 		return patch_vt1708S(codec);
2964ddd304d8STakashi Iwai 
2965f7278fd0SJosepch Chan 	/* create a codec specific record */
29665b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
2967f7278fd0SJosepch Chan 	if (spec == NULL)
2968f7278fd0SJosepch Chan 		return -ENOMEM;
2969f7278fd0SJosepch Chan 
2970620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
2971620e2b28STakashi Iwai 
2972f7278fd0SJosepch Chan 	/* automatic parse from the BIOS config */
297312daef65STakashi Iwai 	err = via_parse_auto_config(codec);
2974f7278fd0SJosepch Chan 	if (err < 0) {
2975f7278fd0SJosepch Chan 		via_free(codec);
2976f7278fd0SJosepch Chan 		return err;
2977f7278fd0SJosepch Chan 	}
2978f7278fd0SJosepch Chan 
2979f7278fd0SJosepch Chan 	codec->patch_ops = via_patch_ops;
2980f7278fd0SJosepch Chan 
29813e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
29823e95b9abSLydia Wang 
2983f7278fd0SJosepch Chan 	return 0;
2984f7278fd0SJosepch Chan }
2985f7278fd0SJosepch Chan 
2986d949cac1SHarald Welte /* Patch for VT1708S */
2987096a8854STakashi Iwai static const struct hda_verb vt1708S_init_verbs[] = {
2988d7426329SHarald Welte 	/* Enable Mic Boost Volume backdoor */
2989d7426329SHarald Welte 	{0x1, 0xf98, 0x1},
2990bc7e7e5cSLydia Wang 	/* don't bybass mixer */
2991bc7e7e5cSLydia Wang 	{0x1, 0xf88, 0xc0},
2992d949cac1SHarald Welte 	{ }
2993d949cac1SHarald Welte };
2994d949cac1SHarald Welte 
29959da29271STakashi Iwai /* fill out digital output widgets; one for master and one for slave outputs */
29969da29271STakashi Iwai static void fill_dig_outs(struct hda_codec *codec)
29979da29271STakashi Iwai {
29989da29271STakashi Iwai 	struct via_spec *spec = codec->spec;
29999da29271STakashi Iwai 	int i;
30009da29271STakashi Iwai 
30019da29271STakashi Iwai 	for (i = 0; i < spec->autocfg.dig_outs; i++) {
30029da29271STakashi Iwai 		hda_nid_t nid;
30039da29271STakashi Iwai 		int conn;
30049da29271STakashi Iwai 
30059da29271STakashi Iwai 		nid = spec->autocfg.dig_out_pins[i];
30069da29271STakashi Iwai 		if (!nid)
30079da29271STakashi Iwai 			continue;
30089da29271STakashi Iwai 		conn = snd_hda_get_connections(codec, nid, &nid, 1);
30099da29271STakashi Iwai 		if (conn < 1)
30109da29271STakashi Iwai 			continue;
30119da29271STakashi Iwai 		if (!spec->multiout.dig_out_nid)
30129da29271STakashi Iwai 			spec->multiout.dig_out_nid = nid;
30139da29271STakashi Iwai 		else {
30149da29271STakashi Iwai 			spec->slave_dig_outs[0] = nid;
30159da29271STakashi Iwai 			break; /* at most two dig outs */
30169da29271STakashi Iwai 		}
30179da29271STakashi Iwai 	}
30189da29271STakashi Iwai }
30199da29271STakashi Iwai 
302012daef65STakashi Iwai static void fill_dig_in(struct hda_codec *codec)
3021d949cac1SHarald Welte {
3022d949cac1SHarald Welte 	struct via_spec *spec = codec->spec;
302312daef65STakashi Iwai 	hda_nid_t dig_nid;
302412daef65STakashi Iwai 	int i, err;
3025d949cac1SHarald Welte 
302612daef65STakashi Iwai 	if (!spec->autocfg.dig_in_pin)
302712daef65STakashi Iwai 		return;
3028d949cac1SHarald Welte 
302912daef65STakashi Iwai 	dig_nid = codec->start_nid;
303012daef65STakashi Iwai 	for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
303112daef65STakashi Iwai 		unsigned int wcaps = get_wcaps(codec, dig_nid);
303212daef65STakashi Iwai 		if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
303312daef65STakashi Iwai 			continue;
303412daef65STakashi Iwai 		if (!(wcaps & AC_WCAP_DIGITAL))
303512daef65STakashi Iwai 			continue;
303612daef65STakashi Iwai 		if (!(wcaps & AC_WCAP_CONN_LIST))
303712daef65STakashi Iwai 			continue;
303812daef65STakashi Iwai 		err = get_connection_index(codec, dig_nid,
303912daef65STakashi Iwai 					   spec->autocfg.dig_in_pin);
304012daef65STakashi Iwai 		if (err >= 0) {
304112daef65STakashi Iwai 			spec->dig_in_nid = dig_nid;
304212daef65STakashi Iwai 			break;
304312daef65STakashi Iwai 		}
304412daef65STakashi Iwai 	}
3045d949cac1SHarald Welte }
3046d949cac1SHarald Welte 
30476369bcfcSLydia Wang static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
30486369bcfcSLydia Wang 			       int offset, int num_steps, int step_size)
30496369bcfcSLydia Wang {
30506369bcfcSLydia Wang 	snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
30516369bcfcSLydia Wang 				  (offset << AC_AMPCAP_OFFSET_SHIFT) |
30526369bcfcSLydia Wang 				  (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
30536369bcfcSLydia Wang 				  (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
30546369bcfcSLydia Wang 				  (0 << AC_AMPCAP_MUTE_SHIFT));
30556369bcfcSLydia Wang }
30566369bcfcSLydia Wang 
3057d949cac1SHarald Welte static int patch_vt1708S(struct hda_codec *codec)
3058d949cac1SHarald Welte {
3059d949cac1SHarald Welte 	struct via_spec *spec;
3060d949cac1SHarald Welte 	int err;
3061d949cac1SHarald Welte 
3062d949cac1SHarald Welte 	/* create a codec specific record */
30635b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3064d949cac1SHarald Welte 	if (spec == NULL)
3065d949cac1SHarald Welte 		return -ENOMEM;
3066d949cac1SHarald Welte 
3067620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
3068d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1a, 0, 3, 40);
3069d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1e, 0, 3, 40);
3070620e2b28STakashi Iwai 
3071d949cac1SHarald Welte 	/* automatic parse from the BIOS config */
307212daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3073d949cac1SHarald Welte 	if (err < 0) {
3074d949cac1SHarald Welte 		via_free(codec);
3075d949cac1SHarald Welte 		return err;
3076d949cac1SHarald Welte 	}
3077d949cac1SHarald Welte 
3078096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
3079d949cac1SHarald Welte 
3080d949cac1SHarald Welte 	codec->patch_ops = via_patch_ops;
3081d949cac1SHarald Welte 
3082518bf3baSLydia Wang 	/* correct names for VT1708BCE */
3083518bf3baSLydia Wang 	if (get_codec_type(codec) == VT1708BCE)	{
3084518bf3baSLydia Wang 		kfree(codec->chip_name);
3085518bf3baSLydia Wang 		codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
3086518bf3baSLydia Wang 		snprintf(codec->bus->card->mixername,
3087518bf3baSLydia Wang 			 sizeof(codec->bus->card->mixername),
3088518bf3baSLydia Wang 			 "%s %s", codec->vendor_name, codec->chip_name);
3089970f630fSLydia Wang 	}
3090bc92df7fSLydia Wang 	/* correct names for VT1705 */
3091bc92df7fSLydia Wang 	if (codec->vendor_id == 0x11064397)	{
3092bc92df7fSLydia Wang 		kfree(codec->chip_name);
3093bc92df7fSLydia Wang 		codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
3094bc92df7fSLydia Wang 		snprintf(codec->bus->card->mixername,
3095bc92df7fSLydia Wang 			 sizeof(codec->bus->card->mixername),
3096bc92df7fSLydia Wang 			 "%s %s", codec->vendor_name, codec->chip_name);
3097bc92df7fSLydia Wang 	}
30983e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
3099d949cac1SHarald Welte 	return 0;
3100d949cac1SHarald Welte }
3101d949cac1SHarald Welte 
3102d949cac1SHarald Welte /* Patch for VT1702 */
3103d949cac1SHarald Welte 
3104096a8854STakashi Iwai static const struct hda_verb vt1702_init_verbs[] = {
3105bc7e7e5cSLydia Wang 	/* mixer enable */
3106bc7e7e5cSLydia Wang 	{0x1, 0xF88, 0x3},
3107bc7e7e5cSLydia Wang 	/* GPIO 0~2 */
3108bc7e7e5cSLydia Wang 	{0x1, 0xF82, 0x3F},
3109d949cac1SHarald Welte 	{ }
3110d949cac1SHarald Welte };
3111d949cac1SHarald Welte 
31123e95b9abSLydia Wang static void set_widgets_power_state_vt1702(struct hda_codec *codec)
31133e95b9abSLydia Wang {
31143e95b9abSLydia Wang 	int imux_is_smixer =
31153e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
31163e95b9abSLydia Wang 	unsigned int parm;
31173e95b9abSLydia Wang 	/* inputs */
31183e95b9abSLydia Wang 	/* PW 1/2/5 (14h/15h/18h) */
31193e95b9abSLydia Wang 	parm = AC_PWRST_D3;
31203e95b9abSLydia Wang 	set_pin_power_state(codec, 0x14, &parm);
31213e95b9abSLydia Wang 	set_pin_power_state(codec, 0x15, &parm);
31223e95b9abSLydia Wang 	set_pin_power_state(codec, 0x18, &parm);
31233e95b9abSLydia Wang 	if (imux_is_smixer)
31243e95b9abSLydia Wang 		parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
31253e95b9abSLydia Wang 	/* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
3126054d867eSTakashi Iwai 	update_power_state(codec, 0x13, parm);
3127054d867eSTakashi Iwai 	update_power_state(codec, 0x12, parm);
3128054d867eSTakashi Iwai 	update_power_state(codec, 0x1f, parm);
3129054d867eSTakashi Iwai 	update_power_state(codec, 0x20, parm);
31303e95b9abSLydia Wang 
31313e95b9abSLydia Wang 	/* outputs */
31323e95b9abSLydia Wang 	/* PW 3/4 (16h/17h) */
31333e95b9abSLydia Wang 	parm = AC_PWRST_D3;
31343e95b9abSLydia Wang 	set_pin_power_state(codec, 0x17, &parm);
31353e95b9abSLydia Wang 	set_pin_power_state(codec, 0x16, &parm);
31363e95b9abSLydia Wang 	/* MW0 (1ah), AOW 0/1 (10h/1dh) */
3137054d867eSTakashi Iwai 	update_power_state(codec, 0x1a, imux_is_smixer ? AC_PWRST_D0 : parm);
3138054d867eSTakashi Iwai 	update_power_state(codec, 0x10, parm);
3139054d867eSTakashi Iwai 	update_power_state(codec, 0x1d, parm);
31403e95b9abSLydia Wang }
31413e95b9abSLydia Wang 
3142d949cac1SHarald Welte static int patch_vt1702(struct hda_codec *codec)
3143d949cac1SHarald Welte {
3144d949cac1SHarald Welte 	struct via_spec *spec;
3145d949cac1SHarald Welte 	int err;
3146d949cac1SHarald Welte 
3147d949cac1SHarald Welte 	/* create a codec specific record */
31485b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3149d949cac1SHarald Welte 	if (spec == NULL)
3150d949cac1SHarald Welte 		return -ENOMEM;
3151d949cac1SHarald Welte 
3152620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x1a;
3153620e2b28STakashi Iwai 
315412daef65STakashi Iwai 	/* limit AA path volume to 0 dB */
315512daef65STakashi Iwai 	snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
315612daef65STakashi Iwai 				  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
315712daef65STakashi Iwai 				  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
315812daef65STakashi Iwai 				  (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
315912daef65STakashi Iwai 				  (1 << AC_AMPCAP_MUTE_SHIFT));
316012daef65STakashi Iwai 
3161d949cac1SHarald Welte 	/* automatic parse from the BIOS config */
316212daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3163d949cac1SHarald Welte 	if (err < 0) {
3164d949cac1SHarald Welte 		via_free(codec);
3165d949cac1SHarald Welte 		return err;
3166d949cac1SHarald Welte 	}
3167d949cac1SHarald Welte 
3168096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
3169d949cac1SHarald Welte 
3170d949cac1SHarald Welte 	codec->patch_ops = via_patch_ops;
3171d949cac1SHarald Welte 
31723e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1702;
3173d949cac1SHarald Welte 	return 0;
3174d949cac1SHarald Welte }
3175d949cac1SHarald Welte 
3176eb7188caSLydia Wang /* Patch for VT1718S */
3177eb7188caSLydia Wang 
3178096a8854STakashi Iwai static const struct hda_verb vt1718S_init_verbs[] = {
31794ab2d53aSLydia Wang 	/* Enable MW0 adjust Gain 5 */
31804ab2d53aSLydia Wang 	{0x1, 0xfb2, 0x10},
3181eb7188caSLydia Wang 	/* Enable Boost Volume backdoor */
3182eb7188caSLydia Wang 	{0x1, 0xf88, 0x8},
31835d41762aSTakashi Iwai 
3184eb7188caSLydia Wang 	{ }
3185eb7188caSLydia Wang };
3186eb7188caSLydia Wang 
31873e95b9abSLydia Wang static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
31883e95b9abSLydia Wang {
31893e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
31903e95b9abSLydia Wang 	int imux_is_smixer;
31916162552bSTakashi Iwai 	unsigned int parm, parm2;
31923e95b9abSLydia Wang 	/* MUX6 (1eh) = stereo mixer */
31933e95b9abSLydia Wang 	imux_is_smixer =
31943e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
31953e95b9abSLydia Wang 	/* inputs */
31963e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
31973e95b9abSLydia Wang 	parm = AC_PWRST_D3;
31983e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
31993e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
32003e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
32013e95b9abSLydia Wang 	if (imux_is_smixer)
32023e95b9abSLydia Wang 		parm = AC_PWRST_D0;
32033e95b9abSLydia Wang 	/* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
3204054d867eSTakashi Iwai 	update_power_state(codec, 0x1e, parm);
3205054d867eSTakashi Iwai 	update_power_state(codec, 0x1f, parm);
3206054d867eSTakashi Iwai 	update_power_state(codec, 0x10, parm);
3207054d867eSTakashi Iwai 	update_power_state(codec, 0x11, parm);
32083e95b9abSLydia Wang 
32093e95b9abSLydia Wang 	/* outputs */
32103e95b9abSLydia Wang 	/* PW3 (27h), MW2 (1ah), AOW3 (bh) */
32113e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32123e95b9abSLydia Wang 	set_pin_power_state(codec, 0x27, &parm);
3213054d867eSTakashi Iwai 	update_power_state(codec, 0x1a, parm);
32146162552bSTakashi Iwai 	parm2 = parm; /* for pin 0x0b */
32153e95b9abSLydia Wang 
32163e95b9abSLydia Wang 	/* PW2 (26h), AOW2 (ah) */
32173e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32183e95b9abSLydia Wang 	set_pin_power_state(codec, 0x26, &parm);
32193e95b9abSLydia Wang 	if (spec->smart51_enabled)
32203e95b9abSLydia Wang 		set_pin_power_state(codec, 0x2b, &parm);
3221054d867eSTakashi Iwai 	update_power_state(codec, 0xa, parm);
32223e95b9abSLydia Wang 
32233e95b9abSLydia Wang 	/* PW0 (24h), AOW0 (8h) */
32243e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32253e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
32263e95b9abSLydia Wang 	if (!spec->hp_independent_mode) /* check for redirected HP */
32273e95b9abSLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
3228054d867eSTakashi Iwai 	update_power_state(codec, 0x8, parm);
32296162552bSTakashi Iwai 	if (!spec->hp_independent_mode && parm2 != AC_PWRST_D3)
32306162552bSTakashi Iwai 		parm = parm2;
32316162552bSTakashi Iwai 	update_power_state(codec, 0xb, parm);
32323e95b9abSLydia Wang 	/* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
3233054d867eSTakashi Iwai 	update_power_state(codec, 0x21, imux_is_smixer ? AC_PWRST_D0 : parm);
32343e95b9abSLydia Wang 
32353e95b9abSLydia Wang 	/* PW1 (25h), AOW1 (9h) */
32363e95b9abSLydia Wang 	parm = AC_PWRST_D3;
32373e95b9abSLydia Wang 	set_pin_power_state(codec, 0x25, &parm);
32383e95b9abSLydia Wang 	if (spec->smart51_enabled)
32393e95b9abSLydia Wang 		set_pin_power_state(codec, 0x2a, &parm);
3240054d867eSTakashi Iwai 	update_power_state(codec, 0x9, parm);
32413e95b9abSLydia Wang 
32423e95b9abSLydia Wang 	if (spec->hp_independent_mode) {
32433e95b9abSLydia Wang 		/* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
32443e95b9abSLydia Wang 		parm = AC_PWRST_D3;
32453e95b9abSLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
3246054d867eSTakashi Iwai 		update_power_state(codec, 0x1b, parm);
3247054d867eSTakashi Iwai 		update_power_state(codec, 0x34, parm);
3248054d867eSTakashi Iwai 		update_power_state(codec, 0xc, parm);
32493e95b9abSLydia Wang 	}
32503e95b9abSLydia Wang }
32513e95b9abSLydia Wang 
325230b45033STakashi Iwai /* Add a connection to the primary DAC from AA-mixer for some codecs
325330b45033STakashi Iwai  * This isn't listed from the raw info, but the chip has a secret connection.
325430b45033STakashi Iwai  */
325530b45033STakashi Iwai static int add_secret_dac_path(struct hda_codec *codec)
325630b45033STakashi Iwai {
325730b45033STakashi Iwai 	struct via_spec *spec = codec->spec;
325830b45033STakashi Iwai 	int i, nums;
325930b45033STakashi Iwai 	hda_nid_t conn[8];
326030b45033STakashi Iwai 	hda_nid_t nid;
326130b45033STakashi Iwai 
326230b45033STakashi Iwai 	if (!spec->aa_mix_nid)
326330b45033STakashi Iwai 		return 0;
326430b45033STakashi Iwai 	nums = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
326530b45033STakashi Iwai 				       ARRAY_SIZE(conn) - 1);
326630b45033STakashi Iwai 	for (i = 0; i < nums; i++) {
326730b45033STakashi Iwai 		if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT)
326830b45033STakashi Iwai 			return 0;
326930b45033STakashi Iwai 	}
327030b45033STakashi Iwai 
327130b45033STakashi Iwai 	/* find the primary DAC and add to the connection list */
327230b45033STakashi Iwai 	nid = codec->start_nid;
327330b45033STakashi Iwai 	for (i = 0; i < codec->num_nodes; i++, nid++) {
327430b45033STakashi Iwai 		unsigned int caps = get_wcaps(codec, nid);
327530b45033STakashi Iwai 		if (get_wcaps_type(caps) == AC_WID_AUD_OUT &&
327630b45033STakashi Iwai 		    !(caps & AC_WCAP_DIGITAL)) {
327730b45033STakashi Iwai 			conn[nums++] = nid;
327830b45033STakashi Iwai 			return snd_hda_override_conn_list(codec,
327930b45033STakashi Iwai 							  spec->aa_mix_nid,
328030b45033STakashi Iwai 							  nums, conn);
328130b45033STakashi Iwai 		}
328230b45033STakashi Iwai 	}
328330b45033STakashi Iwai 	return 0;
328430b45033STakashi Iwai }
328530b45033STakashi Iwai 
328630b45033STakashi Iwai 
3287eb7188caSLydia Wang static int patch_vt1718S(struct hda_codec *codec)
3288eb7188caSLydia Wang {
3289eb7188caSLydia Wang 	struct via_spec *spec;
3290eb7188caSLydia Wang 	int err;
3291eb7188caSLydia Wang 
3292eb7188caSLydia Wang 	/* create a codec specific record */
32935b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3294eb7188caSLydia Wang 	if (spec == NULL)
3295eb7188caSLydia Wang 		return -ENOMEM;
3296eb7188caSLydia Wang 
3297620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
3298d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3299d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
330030b45033STakashi Iwai 	add_secret_dac_path(codec);
3301620e2b28STakashi Iwai 
3302eb7188caSLydia Wang 	/* automatic parse from the BIOS config */
330312daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3304eb7188caSLydia Wang 	if (err < 0) {
3305eb7188caSLydia Wang 		via_free(codec);
3306eb7188caSLydia Wang 		return err;
3307eb7188caSLydia Wang 	}
3308eb7188caSLydia Wang 
3309096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
3310eb7188caSLydia Wang 
3311eb7188caSLydia Wang 	codec->patch_ops = via_patch_ops;
3312eb7188caSLydia Wang 
33133e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1718S;
33143e95b9abSLydia Wang 
3315eb7188caSLydia Wang 	return 0;
3316eb7188caSLydia Wang }
3317f3db423dSLydia Wang 
3318f3db423dSLydia Wang /* Patch for VT1716S */
3319f3db423dSLydia Wang 
3320f3db423dSLydia Wang static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
3321f3db423dSLydia Wang 			    struct snd_ctl_elem_info *uinfo)
3322f3db423dSLydia Wang {
3323f3db423dSLydia Wang 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3324f3db423dSLydia Wang 	uinfo->count = 1;
3325f3db423dSLydia Wang 	uinfo->value.integer.min = 0;
3326f3db423dSLydia Wang 	uinfo->value.integer.max = 1;
3327f3db423dSLydia Wang 	return 0;
3328f3db423dSLydia Wang }
3329f3db423dSLydia Wang 
3330f3db423dSLydia Wang static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
3331f3db423dSLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
3332f3db423dSLydia Wang {
3333f3db423dSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3334f3db423dSLydia Wang 	int index = 0;
3335f3db423dSLydia Wang 
3336f3db423dSLydia Wang 	index = snd_hda_codec_read(codec, 0x26, 0,
3337f3db423dSLydia Wang 					       AC_VERB_GET_CONNECT_SEL, 0);
3338f3db423dSLydia Wang 	if (index != -1)
3339f3db423dSLydia Wang 		*ucontrol->value.integer.value = index;
3340f3db423dSLydia Wang 
3341f3db423dSLydia Wang 	return 0;
3342f3db423dSLydia Wang }
3343f3db423dSLydia Wang 
3344f3db423dSLydia Wang static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
3345f3db423dSLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
3346f3db423dSLydia Wang {
3347f3db423dSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3348f3db423dSLydia Wang 	struct via_spec *spec = codec->spec;
3349f3db423dSLydia Wang 	int index = *ucontrol->value.integer.value;
3350f3db423dSLydia Wang 
3351f3db423dSLydia Wang 	snd_hda_codec_write(codec, 0x26, 0,
3352f3db423dSLydia Wang 					       AC_VERB_SET_CONNECT_SEL, index);
3353f3db423dSLydia Wang 	spec->dmic_enabled = index;
33543e95b9abSLydia Wang 	set_widgets_power_state(codec);
3355f3db423dSLydia Wang 	return 1;
3356f3db423dSLydia Wang }
3357f3db423dSLydia Wang 
335890dd48a1STakashi Iwai static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
3359f3db423dSLydia Wang 	HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
3360f3db423dSLydia Wang 	{
3361f3db423dSLydia Wang 	 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3362f3db423dSLydia Wang 	 .name = "Digital Mic Capture Switch",
33635b0cb1d8SJaroslav Kysela 	 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
3364f3db423dSLydia Wang 	 .count = 1,
3365f3db423dSLydia Wang 	 .info = vt1716s_dmic_info,
3366f3db423dSLydia Wang 	 .get = vt1716s_dmic_get,
3367f3db423dSLydia Wang 	 .put = vt1716s_dmic_put,
3368f3db423dSLydia Wang 	 },
3369f3db423dSLydia Wang 	{}			/* end */
3370f3db423dSLydia Wang };
3371f3db423dSLydia Wang 
3372f3db423dSLydia Wang 
3373f3db423dSLydia Wang /* mono-out mixer elements */
337490dd48a1STakashi Iwai static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
3375f3db423dSLydia Wang 	HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
3376f3db423dSLydia Wang 	{ } /* end */
3377f3db423dSLydia Wang };
3378f3db423dSLydia Wang 
3379096a8854STakashi Iwai static const struct hda_verb vt1716S_init_verbs[] = {
3380f3db423dSLydia Wang 	/* Enable Boost Volume backdoor */
3381f3db423dSLydia Wang 	{0x1, 0xf8a, 0x80},
3382f3db423dSLydia Wang 	/* don't bybass mixer */
3383f3db423dSLydia Wang 	{0x1, 0xf88, 0xc0},
3384f3db423dSLydia Wang 	/* Enable mono output */
3385f3db423dSLydia Wang 	{0x1, 0xf90, 0x08},
3386f3db423dSLydia Wang 	{ }
3387f3db423dSLydia Wang };
3388f3db423dSLydia Wang 
33893e95b9abSLydia Wang static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
33903e95b9abSLydia Wang {
33913e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
33923e95b9abSLydia Wang 	int imux_is_smixer;
33933e95b9abSLydia Wang 	unsigned int parm;
33943e95b9abSLydia Wang 	unsigned int mono_out, present;
33953e95b9abSLydia Wang 	/* SW0 (17h) = stereo mixer */
33963e95b9abSLydia Wang 	imux_is_smixer =
33973e95b9abSLydia Wang 	(snd_hda_codec_read(codec, 0x17, 0,
33983e95b9abSLydia Wang 			    AC_VERB_GET_CONNECT_SEL, 0x00) ==  5);
33993e95b9abSLydia Wang 	/* inputs */
34003e95b9abSLydia Wang 	/* PW 1/2/5 (1ah/1bh/1eh) */
34013e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34023e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1a, &parm);
34033e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1b, &parm);
34043e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
34053e95b9abSLydia Wang 	if (imux_is_smixer)
34063e95b9abSLydia Wang 		parm = AC_PWRST_D0;
34073e95b9abSLydia Wang 	/* SW0 (17h), AIW0(13h) */
3408054d867eSTakashi Iwai 	update_power_state(codec, 0x17, parm);
3409054d867eSTakashi Iwai 	update_power_state(codec, 0x13, parm);
34103e95b9abSLydia Wang 
34113e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34123e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1e, &parm);
34133e95b9abSLydia Wang 	/* PW11 (22h) */
34143e95b9abSLydia Wang 	if (spec->dmic_enabled)
34153e95b9abSLydia Wang 		set_pin_power_state(codec, 0x22, &parm);
34163e95b9abSLydia Wang 	else
3417054d867eSTakashi Iwai 		update_power_state(codec, 0x22, AC_PWRST_D3);
34183e95b9abSLydia Wang 
34193e95b9abSLydia Wang 	/* SW2(26h), AIW1(14h) */
3420054d867eSTakashi Iwai 	update_power_state(codec, 0x26, parm);
3421054d867eSTakashi Iwai 	update_power_state(codec, 0x14, parm);
34223e95b9abSLydia Wang 
34233e95b9abSLydia Wang 	/* outputs */
34243e95b9abSLydia Wang 	/* PW0 (19h), SW1 (18h), AOW1 (11h) */
34253e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34263e95b9abSLydia Wang 	set_pin_power_state(codec, 0x19, &parm);
34273e95b9abSLydia Wang 	/* Smart 5.1 PW2(1bh) */
34283e95b9abSLydia Wang 	if (spec->smart51_enabled)
34293e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1b, &parm);
3430054d867eSTakashi Iwai 	update_power_state(codec, 0x18, parm);
3431054d867eSTakashi Iwai 	update_power_state(codec, 0x11, parm);
34323e95b9abSLydia Wang 
34333e95b9abSLydia Wang 	/* PW7 (23h), SW3 (27h), AOW3 (25h) */
34343e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34353e95b9abSLydia Wang 	set_pin_power_state(codec, 0x23, &parm);
34363e95b9abSLydia Wang 	/* Smart 5.1 PW1(1ah) */
34373e95b9abSLydia Wang 	if (spec->smart51_enabled)
34383e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1a, &parm);
3439054d867eSTakashi Iwai 	update_power_state(codec, 0x27, parm);
34403e95b9abSLydia Wang 
34413e95b9abSLydia Wang 	/* Smart 5.1 PW5(1eh) */
34423e95b9abSLydia Wang 	if (spec->smart51_enabled)
34433e95b9abSLydia Wang 		set_pin_power_state(codec, 0x1e, &parm);
3444054d867eSTakashi Iwai 	update_power_state(codec, 0x25, parm);
34453e95b9abSLydia Wang 
34463e95b9abSLydia Wang 	/* Mono out */
34473e95b9abSLydia Wang 	/* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
34483e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x1c);
34493e95b9abSLydia Wang 
34503e95b9abSLydia Wang 	if (present)
34513e95b9abSLydia Wang 		mono_out = 0;
34523e95b9abSLydia Wang 	else {
34533e95b9abSLydia Wang 		present = snd_hda_jack_detect(codec, 0x1d);
34543e95b9abSLydia Wang 		if (!spec->hp_independent_mode && present)
34553e95b9abSLydia Wang 			mono_out = 0;
34563e95b9abSLydia Wang 		else
34573e95b9abSLydia Wang 			mono_out = 1;
34583e95b9abSLydia Wang 	}
34593e95b9abSLydia Wang 	parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
3460054d867eSTakashi Iwai 	update_power_state(codec, 0x28, parm);
3461054d867eSTakashi Iwai 	update_power_state(codec, 0x29, parm);
3462054d867eSTakashi Iwai 	update_power_state(codec, 0x2a, parm);
34633e95b9abSLydia Wang 
34643e95b9abSLydia Wang 	/* PW 3/4 (1ch/1dh) */
34653e95b9abSLydia Wang 	parm = AC_PWRST_D3;
34663e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1c, &parm);
34673e95b9abSLydia Wang 	set_pin_power_state(codec, 0x1d, &parm);
34683e95b9abSLydia Wang 	/* HP Independent Mode, power on AOW3 */
34693e95b9abSLydia Wang 	if (spec->hp_independent_mode)
3470054d867eSTakashi Iwai 		update_power_state(codec, 0x25, parm);
34713e95b9abSLydia Wang 
34723e95b9abSLydia Wang 	/* force to D0 for internal Speaker */
34733e95b9abSLydia Wang 	/* MW0 (16h), AOW0 (10h) */
3474054d867eSTakashi Iwai 	update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
3475054d867eSTakashi Iwai 	update_power_state(codec, 0x10, mono_out ? AC_PWRST_D0 : parm);
34763e95b9abSLydia Wang }
34773e95b9abSLydia Wang 
3478f3db423dSLydia Wang static int patch_vt1716S(struct hda_codec *codec)
3479f3db423dSLydia Wang {
3480f3db423dSLydia Wang 	struct via_spec *spec;
3481f3db423dSLydia Wang 	int err;
3482f3db423dSLydia Wang 
3483f3db423dSLydia Wang 	/* create a codec specific record */
34845b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3485f3db423dSLydia Wang 	if (spec == NULL)
3486f3db423dSLydia Wang 		return -ENOMEM;
3487f3db423dSLydia Wang 
3488620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x16;
3489d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1a, 0, 3, 40);
3490d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x1e, 0, 3, 40);
3491620e2b28STakashi Iwai 
3492f3db423dSLydia Wang 	/* automatic parse from the BIOS config */
349312daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3494f3db423dSLydia Wang 	if (err < 0) {
3495f3db423dSLydia Wang 		via_free(codec);
3496f3db423dSLydia Wang 		return err;
3497f3db423dSLydia Wang 	}
3498f3db423dSLydia Wang 
3499096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++]  = vt1716S_init_verbs;
3500f3db423dSLydia Wang 
3501f3db423dSLydia Wang 	spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
3502f3db423dSLydia Wang 	spec->num_mixers++;
3503f3db423dSLydia Wang 
3504f3db423dSLydia Wang 	spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
3505f3db423dSLydia Wang 
3506f3db423dSLydia Wang 	codec->patch_ops = via_patch_ops;
3507f3db423dSLydia Wang 
35083e95b9abSLydia Wang 	spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
3509f3db423dSLydia Wang 	return 0;
3510f3db423dSLydia Wang }
351125eaba2fSLydia Wang 
351225eaba2fSLydia Wang /* for vt2002P */
351325eaba2fSLydia Wang 
3514096a8854STakashi Iwai static const struct hda_verb vt2002P_init_verbs[] = {
3515eadb9a80SLydia Wang 	/* Class-D speaker related verbs */
3516eadb9a80SLydia Wang 	{0x1, 0xfe0, 0x4},
3517eadb9a80SLydia Wang 	{0x1, 0xfe9, 0x80},
3518eadb9a80SLydia Wang 	{0x1, 0xfe2, 0x22},
351925eaba2fSLydia Wang 	/* Enable Boost Volume backdoor */
352025eaba2fSLydia Wang 	{0x1, 0xfb9, 0x24},
352125eaba2fSLydia Wang 	/* Enable AOW0 to MW9 */
352225eaba2fSLydia Wang 	{0x1, 0xfb8, 0x88},
352325eaba2fSLydia Wang 	{ }
352425eaba2fSLydia Wang };
35254a918ffeSTakashi Iwai 
3526096a8854STakashi Iwai static const struct hda_verb vt1802_init_verbs[] = {
352711890956SLydia Wang 	/* Enable Boost Volume backdoor */
352811890956SLydia Wang 	{0x1, 0xfb9, 0x24},
352911890956SLydia Wang 	/* Enable AOW0 to MW9 */
353011890956SLydia Wang 	{0x1, 0xfb8, 0x88},
353111890956SLydia Wang 	{ }
353211890956SLydia Wang };
353325eaba2fSLydia Wang 
35343e95b9abSLydia Wang static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
35353e95b9abSLydia Wang {
35363e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
35373e95b9abSLydia Wang 	int imux_is_smixer;
35383e95b9abSLydia Wang 	unsigned int parm;
35393e95b9abSLydia Wang 	unsigned int present;
35403e95b9abSLydia Wang 	/* MUX9 (1eh) = stereo mixer */
35413e95b9abSLydia Wang 	imux_is_smixer =
35423e95b9abSLydia Wang 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
35433e95b9abSLydia Wang 	/* inputs */
35443e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
35453e95b9abSLydia Wang 	parm = AC_PWRST_D3;
35463e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
35473e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
35483e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
35493e95b9abSLydia Wang 	parm = AC_PWRST_D0;
35503e95b9abSLydia Wang 	/* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
3551054d867eSTakashi Iwai 	update_power_state(codec, 0x1e, parm);
3552054d867eSTakashi Iwai 	update_power_state(codec, 0x1f, parm);
3553054d867eSTakashi Iwai 	update_power_state(codec, 0x10, parm);
3554054d867eSTakashi Iwai 	update_power_state(codec, 0x11, parm);
35553e95b9abSLydia Wang 
35563e95b9abSLydia Wang 	/* outputs */
35573e95b9abSLydia Wang 	/* AOW0 (8h)*/
3558054d867eSTakashi Iwai 	update_power_state(codec, 0x8, parm);
35593e95b9abSLydia Wang 
356011890956SLydia Wang 	if (spec->codec_type == VT1802) {
356111890956SLydia Wang 		/* PW4 (28h), MW4 (18h), MUX4(38h) */
356211890956SLydia Wang 		parm = AC_PWRST_D3;
356311890956SLydia Wang 		set_pin_power_state(codec, 0x28, &parm);
3564054d867eSTakashi Iwai 		update_power_state(codec, 0x18, parm);
3565054d867eSTakashi Iwai 		update_power_state(codec, 0x38, parm);
356611890956SLydia Wang 	} else {
35673e95b9abSLydia Wang 		/* PW4 (26h), MW4 (1ch), MUX4(37h) */
35683e95b9abSLydia Wang 		parm = AC_PWRST_D3;
35693e95b9abSLydia Wang 		set_pin_power_state(codec, 0x26, &parm);
3570054d867eSTakashi Iwai 		update_power_state(codec, 0x1c, parm);
3571054d867eSTakashi Iwai 		update_power_state(codec, 0x37, parm);
357211890956SLydia Wang 	}
35733e95b9abSLydia Wang 
357411890956SLydia Wang 	if (spec->codec_type == VT1802) {
357511890956SLydia Wang 		/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
357611890956SLydia Wang 		parm = AC_PWRST_D3;
357711890956SLydia Wang 		set_pin_power_state(codec, 0x25, &parm);
3578054d867eSTakashi Iwai 		update_power_state(codec, 0x15, parm);
3579054d867eSTakashi Iwai 		update_power_state(codec, 0x35, parm);
358011890956SLydia Wang 	} else {
35813e95b9abSLydia Wang 		/* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
35823e95b9abSLydia Wang 		parm = AC_PWRST_D3;
35833e95b9abSLydia Wang 		set_pin_power_state(codec, 0x25, &parm);
3584054d867eSTakashi Iwai 		update_power_state(codec, 0x19, parm);
3585054d867eSTakashi Iwai 		update_power_state(codec, 0x35, parm);
358611890956SLydia Wang 	}
35873e95b9abSLydia Wang 
35883e95b9abSLydia Wang 	if (spec->hp_independent_mode)
3589054d867eSTakashi Iwai 		update_power_state(codec, 0x9, AC_PWRST_D0);
35903e95b9abSLydia Wang 
35913e95b9abSLydia Wang 	/* Class-D */
35923e95b9abSLydia Wang 	/* PW0 (24h), MW0(18h/14h), MUX0(34h) */
35933e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x25);
35943e95b9abSLydia Wang 
35953e95b9abSLydia Wang 	parm = AC_PWRST_D3;
35963e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
35973e95b9abSLydia Wang 	parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
359811890956SLydia Wang 	if (spec->codec_type == VT1802)
3599054d867eSTakashi Iwai 		update_power_state(codec, 0x14, parm);
360011890956SLydia Wang 	else
3601054d867eSTakashi Iwai 		update_power_state(codec, 0x18, parm);
3602054d867eSTakashi Iwai 	update_power_state(codec, 0x34, parm);
36033e95b9abSLydia Wang 
36043e95b9abSLydia Wang 	/* Mono Out */
36053e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x26);
36063e95b9abSLydia Wang 
36073e95b9abSLydia Wang 	parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
360811890956SLydia Wang 	if (spec->codec_type == VT1802) {
360911890956SLydia Wang 		/* PW15 (33h), MW8(1ch), MUX8(3ch) */
3610054d867eSTakashi Iwai 		update_power_state(codec, 0x33, parm);
3611054d867eSTakashi Iwai 		update_power_state(codec, 0x1c, parm);
3612054d867eSTakashi Iwai 		update_power_state(codec, 0x3c, parm);
361311890956SLydia Wang 	} else {
36143e95b9abSLydia Wang 		/* PW15 (31h), MW8(17h), MUX8(3bh) */
3615054d867eSTakashi Iwai 		update_power_state(codec, 0x31, parm);
3616054d867eSTakashi Iwai 		update_power_state(codec, 0x17, parm);
3617054d867eSTakashi Iwai 		update_power_state(codec, 0x3b, parm);
361811890956SLydia Wang 	}
36193e95b9abSLydia Wang 	/* MW9 (21h) */
36203e95b9abSLydia Wang 	if (imux_is_smixer || !is_aa_path_mute(codec))
3621054d867eSTakashi Iwai 		update_power_state(codec, 0x21, AC_PWRST_D0);
36223e95b9abSLydia Wang 	else
3623054d867eSTakashi Iwai 		update_power_state(codec, 0x21, AC_PWRST_D3);
36243e95b9abSLydia Wang }
362525eaba2fSLydia Wang 
36264b527b65SDavid Henningsson /*
36274b527b65SDavid Henningsson  * pin fix-up
36284b527b65SDavid Henningsson  */
36294b527b65SDavid Henningsson enum {
36304b527b65SDavid Henningsson 	VIA_FIXUP_INTMIC_BOOST,
36314b527b65SDavid Henningsson };
36324b527b65SDavid Henningsson 
36334b527b65SDavid Henningsson static void via_fixup_intmic_boost(struct hda_codec *codec,
36344b527b65SDavid Henningsson 				  const struct hda_fixup *fix, int action)
36354b527b65SDavid Henningsson {
36364b527b65SDavid Henningsson 	if (action == HDA_FIXUP_ACT_PRE_PROBE)
36374b527b65SDavid Henningsson 		override_mic_boost(codec, 0x30, 0, 2, 40);
36384b527b65SDavid Henningsson }
36394b527b65SDavid Henningsson 
36404b527b65SDavid Henningsson static const struct hda_fixup via_fixups[] = {
36414b527b65SDavid Henningsson 	[VIA_FIXUP_INTMIC_BOOST] = {
36424b527b65SDavid Henningsson 		.type = HDA_FIXUP_FUNC,
36434b527b65SDavid Henningsson 		.v.func = via_fixup_intmic_boost,
36444b527b65SDavid Henningsson 	},
36454b527b65SDavid Henningsson };
36464b527b65SDavid Henningsson 
36474b527b65SDavid Henningsson static const struct snd_pci_quirk vt2002p_fixups[] = {
36484b527b65SDavid Henningsson 	SND_PCI_QUIRK(0x1043, 0x8532, "Asus X202E", VIA_FIXUP_INTMIC_BOOST),
36494b527b65SDavid Henningsson 	{}
36504b527b65SDavid Henningsson };
36514b527b65SDavid Henningsson 
365225eaba2fSLydia Wang /* patch for vt2002P */
365325eaba2fSLydia Wang static int patch_vt2002P(struct hda_codec *codec)
365425eaba2fSLydia Wang {
365525eaba2fSLydia Wang 	struct via_spec *spec;
365625eaba2fSLydia Wang 	int err;
365725eaba2fSLydia Wang 
365825eaba2fSLydia Wang 	/* create a codec specific record */
36595b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
366025eaba2fSLydia Wang 	if (spec == NULL)
366125eaba2fSLydia Wang 		return -ENOMEM;
366225eaba2fSLydia Wang 
3663620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
3664d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3665d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
366630b45033STakashi Iwai 	add_secret_dac_path(codec);
3667620e2b28STakashi Iwai 
36684b527b65SDavid Henningsson 	snd_hda_pick_fixup(codec, NULL, vt2002p_fixups, via_fixups);
36694b527b65SDavid Henningsson 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
36704b527b65SDavid Henningsson 
367125eaba2fSLydia Wang 	/* automatic parse from the BIOS config */
367212daef65STakashi Iwai 	err = via_parse_auto_config(codec);
367325eaba2fSLydia Wang 	if (err < 0) {
367425eaba2fSLydia Wang 		via_free(codec);
367525eaba2fSLydia Wang 		return err;
367625eaba2fSLydia Wang 	}
367725eaba2fSLydia Wang 
367811890956SLydia Wang 	if (spec->codec_type == VT1802)
36794a918ffeSTakashi Iwai 		spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
368011890956SLydia Wang 	else
36814a918ffeSTakashi Iwai 		spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
368211890956SLydia Wang 
368325eaba2fSLydia Wang 	codec->patch_ops = via_patch_ops;
368425eaba2fSLydia Wang 
36853e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt2002P;
368625eaba2fSLydia Wang 	return 0;
368725eaba2fSLydia Wang }
3688ab6734e7SLydia Wang 
3689ab6734e7SLydia Wang /* for vt1812 */
3690ab6734e7SLydia Wang 
3691096a8854STakashi Iwai static const struct hda_verb vt1812_init_verbs[] = {
3692ab6734e7SLydia Wang 	/* Enable Boost Volume backdoor */
3693ab6734e7SLydia Wang 	{0x1, 0xfb9, 0x24},
3694ab6734e7SLydia Wang 	/* Enable AOW0 to MW9 */
3695ab6734e7SLydia Wang 	{0x1, 0xfb8, 0xa8},
3696ab6734e7SLydia Wang 	{ }
3697ab6734e7SLydia Wang };
3698ab6734e7SLydia Wang 
36993e95b9abSLydia Wang static void set_widgets_power_state_vt1812(struct hda_codec *codec)
37003e95b9abSLydia Wang {
37013e95b9abSLydia Wang 	struct via_spec *spec = codec->spec;
37023e95b9abSLydia Wang 	unsigned int parm;
37033e95b9abSLydia Wang 	unsigned int present;
37043e95b9abSLydia Wang 	/* inputs */
37053e95b9abSLydia Wang 	/* PW 5/6/7 (29h/2ah/2bh) */
37063e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37073e95b9abSLydia Wang 	set_pin_power_state(codec, 0x29, &parm);
37083e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2a, &parm);
37093e95b9abSLydia Wang 	set_pin_power_state(codec, 0x2b, &parm);
37103e95b9abSLydia Wang 	parm = AC_PWRST_D0;
37113e95b9abSLydia Wang 	/* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
3712054d867eSTakashi Iwai 	update_power_state(codec, 0x1e, parm);
3713054d867eSTakashi Iwai 	update_power_state(codec, 0x1f, parm);
3714054d867eSTakashi Iwai 	update_power_state(codec, 0x10, parm);
3715054d867eSTakashi Iwai 	update_power_state(codec, 0x11, parm);
37163e95b9abSLydia Wang 
37173e95b9abSLydia Wang 	/* outputs */
37183e95b9abSLydia Wang 	/* AOW0 (8h)*/
3719054d867eSTakashi Iwai 	update_power_state(codec, 0x8, AC_PWRST_D0);
37203e95b9abSLydia Wang 
37213e95b9abSLydia Wang 	/* PW4 (28h), MW4 (18h), MUX4(38h) */
37223e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37233e95b9abSLydia Wang 	set_pin_power_state(codec, 0x28, &parm);
3724054d867eSTakashi Iwai 	update_power_state(codec, 0x18, parm);
3725054d867eSTakashi Iwai 	update_power_state(codec, 0x38, parm);
37263e95b9abSLydia Wang 
37273e95b9abSLydia Wang 	/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
37283e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37293e95b9abSLydia Wang 	set_pin_power_state(codec, 0x25, &parm);
3730054d867eSTakashi Iwai 	update_power_state(codec, 0x15, parm);
3731054d867eSTakashi Iwai 	update_power_state(codec, 0x35, parm);
37323e95b9abSLydia Wang 	if (spec->hp_independent_mode)
3733054d867eSTakashi Iwai 		update_power_state(codec, 0x9, AC_PWRST_D0);
37343e95b9abSLydia Wang 
37353e95b9abSLydia Wang 	/* Internal Speaker */
37363e95b9abSLydia Wang 	/* PW0 (24h), MW0(14h), MUX0(34h) */
37373e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x25);
37383e95b9abSLydia Wang 
37393e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37403e95b9abSLydia Wang 	set_pin_power_state(codec, 0x24, &parm);
37413e95b9abSLydia Wang 	if (present) {
3742054d867eSTakashi Iwai 		update_power_state(codec, 0x14, AC_PWRST_D3);
3743054d867eSTakashi Iwai 		update_power_state(codec, 0x34, AC_PWRST_D3);
37443e95b9abSLydia Wang 	} else {
3745054d867eSTakashi Iwai 		update_power_state(codec, 0x14, AC_PWRST_D0);
3746054d867eSTakashi Iwai 		update_power_state(codec, 0x34, AC_PWRST_D0);
37473e95b9abSLydia Wang 	}
37483e95b9abSLydia Wang 
37493e95b9abSLydia Wang 
37503e95b9abSLydia Wang 	/* Mono Out */
37513e95b9abSLydia Wang 	/* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
37523e95b9abSLydia Wang 	present = snd_hda_jack_detect(codec, 0x28);
37533e95b9abSLydia Wang 
37543e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37553e95b9abSLydia Wang 	set_pin_power_state(codec, 0x31, &parm);
37563e95b9abSLydia Wang 	if (present) {
3757054d867eSTakashi Iwai 		update_power_state(codec, 0x1c, AC_PWRST_D3);
3758054d867eSTakashi Iwai 		update_power_state(codec, 0x3c, AC_PWRST_D3);
3759054d867eSTakashi Iwai 		update_power_state(codec, 0x3e, AC_PWRST_D3);
37603e95b9abSLydia Wang 	} else {
3761054d867eSTakashi Iwai 		update_power_state(codec, 0x1c, AC_PWRST_D0);
3762054d867eSTakashi Iwai 		update_power_state(codec, 0x3c, AC_PWRST_D0);
3763054d867eSTakashi Iwai 		update_power_state(codec, 0x3e, AC_PWRST_D0);
37643e95b9abSLydia Wang 	}
37653e95b9abSLydia Wang 
37663e95b9abSLydia Wang 	/* PW15 (33h), MW15 (1dh), MUX15(3dh) */
37673e95b9abSLydia Wang 	parm = AC_PWRST_D3;
37683e95b9abSLydia Wang 	set_pin_power_state(codec, 0x33, &parm);
3769054d867eSTakashi Iwai 	update_power_state(codec, 0x1d, parm);
3770054d867eSTakashi Iwai 	update_power_state(codec, 0x3d, parm);
37713e95b9abSLydia Wang 
37723e95b9abSLydia Wang }
3773ab6734e7SLydia Wang 
3774ab6734e7SLydia Wang /* patch for vt1812 */
3775ab6734e7SLydia Wang static int patch_vt1812(struct hda_codec *codec)
3776ab6734e7SLydia Wang {
3777ab6734e7SLydia Wang 	struct via_spec *spec;
3778ab6734e7SLydia Wang 	int err;
3779ab6734e7SLydia Wang 
3780ab6734e7SLydia Wang 	/* create a codec specific record */
37815b0cb1d8SJaroslav Kysela 	spec = via_new_spec(codec);
3782ab6734e7SLydia Wang 	if (spec == NULL)
3783ab6734e7SLydia Wang 		return -ENOMEM;
3784ab6734e7SLydia Wang 
3785620e2b28STakashi Iwai 	spec->aa_mix_nid = 0x21;
3786d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3787d7a99cceSTakashi Iwai 	override_mic_boost(codec, 0x29, 0, 3, 40);
378830b45033STakashi Iwai 	add_secret_dac_path(codec);
3789620e2b28STakashi Iwai 
3790ab6734e7SLydia Wang 	/* automatic parse from the BIOS config */
379112daef65STakashi Iwai 	err = via_parse_auto_config(codec);
3792ab6734e7SLydia Wang 	if (err < 0) {
3793ab6734e7SLydia Wang 		via_free(codec);
3794ab6734e7SLydia Wang 		return err;
3795ab6734e7SLydia Wang 	}
3796ab6734e7SLydia Wang 
3797096a8854STakashi Iwai 	spec->init_verbs[spec->num_iverbs++]  = vt1812_init_verbs;
3798ab6734e7SLydia Wang 
3799ab6734e7SLydia Wang 	codec->patch_ops = via_patch_ops;
3800ab6734e7SLydia Wang 
38013e95b9abSLydia Wang 	spec->set_widgets_power_state =  set_widgets_power_state_vt1812;
3802ab6734e7SLydia Wang 	return 0;
3803ab6734e7SLydia Wang }
3804ab6734e7SLydia Wang 
3805c577b8a1SJoseph Chan /*
3806c577b8a1SJoseph Chan  * patch entries
3807c577b8a1SJoseph Chan  */
380890dd48a1STakashi Iwai static const struct hda_codec_preset snd_hda_preset_via[] = {
38093218c178STakashi Iwai 	{ .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
38103218c178STakashi Iwai 	{ .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
38113218c178STakashi Iwai 	{ .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
38123218c178STakashi Iwai 	{ .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
38133218c178STakashi Iwai 	{ .id = 0x1106e710, .name = "VT1709 10-Ch",
3814ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38153218c178STakashi Iwai 	{ .id = 0x1106e711, .name = "VT1709 10-Ch",
3816ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38173218c178STakashi Iwai 	{ .id = 0x1106e712, .name = "VT1709 10-Ch",
3818ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38193218c178STakashi Iwai 	{ .id = 0x1106e713, .name = "VT1709 10-Ch",
3820ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38213218c178STakashi Iwai 	{ .id = 0x1106e714, .name = "VT1709 6-Ch",
3822ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38233218c178STakashi Iwai 	{ .id = 0x1106e715, .name = "VT1709 6-Ch",
3824ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38253218c178STakashi Iwai 	{ .id = 0x1106e716, .name = "VT1709 6-Ch",
3826ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38273218c178STakashi Iwai 	{ .id = 0x1106e717, .name = "VT1709 6-Ch",
3828ddd304d8STakashi Iwai 	  .patch = patch_vt1709},
38293218c178STakashi Iwai 	{ .id = 0x1106e720, .name = "VT1708B 8-Ch",
3830ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38313218c178STakashi Iwai 	{ .id = 0x1106e721, .name = "VT1708B 8-Ch",
3832ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38333218c178STakashi Iwai 	{ .id = 0x1106e722, .name = "VT1708B 8-Ch",
3834ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38353218c178STakashi Iwai 	{ .id = 0x1106e723, .name = "VT1708B 8-Ch",
3836ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38373218c178STakashi Iwai 	{ .id = 0x1106e724, .name = "VT1708B 4-Ch",
3838ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38393218c178STakashi Iwai 	{ .id = 0x1106e725, .name = "VT1708B 4-Ch",
3840ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38413218c178STakashi Iwai 	{ .id = 0x1106e726, .name = "VT1708B 4-Ch",
3842ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38433218c178STakashi Iwai 	{ .id = 0x1106e727, .name = "VT1708B 4-Ch",
3844ddd304d8STakashi Iwai 	  .patch = patch_vt1708B},
38453218c178STakashi Iwai 	{ .id = 0x11060397, .name = "VT1708S",
3846d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38473218c178STakashi Iwai 	{ .id = 0x11061397, .name = "VT1708S",
3848d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38493218c178STakashi Iwai 	{ .id = 0x11062397, .name = "VT1708S",
3850d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38513218c178STakashi Iwai 	{ .id = 0x11063397, .name = "VT1708S",
3852d949cac1SHarald Welte 	  .patch = patch_vt1708S},
3853bc92df7fSLydia Wang 	{ .id = 0x11064397, .name = "VT1705",
3854d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38553218c178STakashi Iwai 	{ .id = 0x11065397, .name = "VT1708S",
3856d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38573218c178STakashi Iwai 	{ .id = 0x11066397, .name = "VT1708S",
3858d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38593218c178STakashi Iwai 	{ .id = 0x11067397, .name = "VT1708S",
3860d949cac1SHarald Welte 	  .patch = patch_vt1708S},
38613218c178STakashi Iwai 	{ .id = 0x11060398, .name = "VT1702",
3862d949cac1SHarald Welte 	  .patch = patch_vt1702},
38633218c178STakashi Iwai 	{ .id = 0x11061398, .name = "VT1702",
3864d949cac1SHarald Welte 	  .patch = patch_vt1702},
38653218c178STakashi Iwai 	{ .id = 0x11062398, .name = "VT1702",
3866d949cac1SHarald Welte 	  .patch = patch_vt1702},
38673218c178STakashi Iwai 	{ .id = 0x11063398, .name = "VT1702",
3868d949cac1SHarald Welte 	  .patch = patch_vt1702},
38693218c178STakashi Iwai 	{ .id = 0x11064398, .name = "VT1702",
3870d949cac1SHarald Welte 	  .patch = patch_vt1702},
38713218c178STakashi Iwai 	{ .id = 0x11065398, .name = "VT1702",
3872d949cac1SHarald Welte 	  .patch = patch_vt1702},
38733218c178STakashi Iwai 	{ .id = 0x11066398, .name = "VT1702",
3874d949cac1SHarald Welte 	  .patch = patch_vt1702},
38753218c178STakashi Iwai 	{ .id = 0x11067398, .name = "VT1702",
3876d949cac1SHarald Welte 	  .patch = patch_vt1702},
3877eb7188caSLydia Wang 	{ .id = 0x11060428, .name = "VT1718S",
3878eb7188caSLydia Wang 	  .patch = patch_vt1718S},
3879eb7188caSLydia Wang 	{ .id = 0x11064428, .name = "VT1718S",
3880eb7188caSLydia Wang 	  .patch = patch_vt1718S},
3881bb3c6bfcSLydia Wang 	{ .id = 0x11060441, .name = "VT2020",
3882bb3c6bfcSLydia Wang 	  .patch = patch_vt1718S},
3883bb3c6bfcSLydia Wang 	{ .id = 0x11064441, .name = "VT1828S",
3884bb3c6bfcSLydia Wang 	  .patch = patch_vt1718S},
3885f3db423dSLydia Wang 	{ .id = 0x11060433, .name = "VT1716S",
3886f3db423dSLydia Wang 	  .patch = patch_vt1716S},
3887f3db423dSLydia Wang 	{ .id = 0x1106a721, .name = "VT1716S",
3888f3db423dSLydia Wang 	  .patch = patch_vt1716S},
388925eaba2fSLydia Wang 	{ .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
389025eaba2fSLydia Wang 	{ .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
3891ab6734e7SLydia Wang 	{ .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
389236dd5c4aSLydia Wang 	{ .id = 0x11060440, .name = "VT1818S",
389336dd5c4aSLydia Wang 	  .patch = patch_vt1708S},
389411890956SLydia Wang 	{ .id = 0x11060446, .name = "VT1802",
389511890956SLydia Wang 		.patch = patch_vt2002P},
389611890956SLydia Wang 	{ .id = 0x11068446, .name = "VT1802",
389711890956SLydia Wang 		.patch = patch_vt2002P},
3898c577b8a1SJoseph Chan 	{} /* terminator */
3899c577b8a1SJoseph Chan };
39001289e9e8STakashi Iwai 
39011289e9e8STakashi Iwai MODULE_ALIAS("snd-hda-codec-id:1106*");
39021289e9e8STakashi Iwai 
39031289e9e8STakashi Iwai static struct hda_codec_preset_list via_list = {
39041289e9e8STakashi Iwai 	.preset = snd_hda_preset_via,
39051289e9e8STakashi Iwai 	.owner = THIS_MODULE,
39061289e9e8STakashi Iwai };
39071289e9e8STakashi Iwai 
39081289e9e8STakashi Iwai MODULE_LICENSE("GPL");
39091289e9e8STakashi Iwai MODULE_DESCRIPTION("VIA HD-audio codec");
39101289e9e8STakashi Iwai 
39111289e9e8STakashi Iwai static int __init patch_via_init(void)
39121289e9e8STakashi Iwai {
39131289e9e8STakashi Iwai 	return snd_hda_add_codec_preset(&via_list);
39141289e9e8STakashi Iwai }
39151289e9e8STakashi Iwai 
39161289e9e8STakashi Iwai static void __exit patch_via_exit(void)
39171289e9e8STakashi Iwai {
39181289e9e8STakashi Iwai 	snd_hda_delete_codec_preset(&via_list);
39191289e9e8STakashi Iwai }
39201289e9e8STakashi Iwai 
39211289e9e8STakashi Iwai module_init(patch_via_init)
39221289e9e8STakashi Iwai module_exit(patch_via_exit)
3923