xref: /openbmc/linux/sound/pci/hda/patch_via.c (revision eb7188cafcb7aa1419b8889494cdbd4e6a01da1c)
1c577b8a1SJoseph Chan /*
2c577b8a1SJoseph Chan  * Universal Interface for Intel High Definition Audio Codec
3c577b8a1SJoseph Chan  *
4d949cac1SHarald Welte  * HD audio interface patch for VIA VT1702/VT1708/VT1709 codec
5c577b8a1SJoseph Chan  *
676d9b0ddSHarald Welte  * Copyright (c) 2006-2008 Lydia Wang <lydiawang@viatech.com>
7c577b8a1SJoseph Chan  *			   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	     */
39c577b8a1SJoseph Chan /*                                                                           */
40c577b8a1SJoseph Chan /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
41c577b8a1SJoseph Chan 
42c577b8a1SJoseph Chan 
43c577b8a1SJoseph Chan #include <linux/init.h>
44c577b8a1SJoseph Chan #include <linux/delay.h>
45c577b8a1SJoseph Chan #include <linux/slab.h>
46c577b8a1SJoseph Chan #include <sound/core.h>
470aa62aefSHarald Welte #include <sound/asoundef.h>
48c577b8a1SJoseph Chan #include "hda_codec.h"
49c577b8a1SJoseph Chan #include "hda_local.h"
50c577b8a1SJoseph Chan 
51c577b8a1SJoseph Chan /* amp values */
52c577b8a1SJoseph Chan #define AMP_VAL_IDX_SHIFT	19
53c577b8a1SJoseph Chan #define AMP_VAL_IDX_MASK	(0x0f<<19)
54c577b8a1SJoseph Chan 
55c577b8a1SJoseph Chan /* Pin Widget NID */
56c577b8a1SJoseph Chan #define VT1708_HP_NID		0x13
57c577b8a1SJoseph Chan #define VT1708_DIGOUT_NID	0x14
58c577b8a1SJoseph Chan #define VT1708_DIGIN_NID	0x16
59f7278fd0SJosepch Chan #define VT1708_DIGIN_PIN	0x26
6076d9b0ddSHarald Welte #define VT1708_HP_PIN_NID	0x20
6176d9b0ddSHarald Welte #define VT1708_CD_PIN_NID	0x24
62c577b8a1SJoseph Chan 
63c577b8a1SJoseph Chan #define VT1709_HP_DAC_NID	0x28
64c577b8a1SJoseph Chan #define VT1709_DIGOUT_NID	0x13
65c577b8a1SJoseph Chan #define VT1709_DIGIN_NID	0x17
66f7278fd0SJosepch Chan #define VT1709_DIGIN_PIN	0x25
67f7278fd0SJosepch Chan 
68f7278fd0SJosepch Chan #define VT1708B_HP_NID		0x25
69f7278fd0SJosepch Chan #define VT1708B_DIGOUT_NID	0x12
70f7278fd0SJosepch Chan #define VT1708B_DIGIN_NID	0x15
71f7278fd0SJosepch Chan #define VT1708B_DIGIN_PIN	0x21
72c577b8a1SJoseph Chan 
73d949cac1SHarald Welte #define VT1708S_HP_NID		0x25
74d949cac1SHarald Welte #define VT1708S_DIGOUT_NID	0x12
75d949cac1SHarald Welte 
76d949cac1SHarald Welte #define VT1702_HP_NID		0x17
77d949cac1SHarald Welte #define VT1702_DIGOUT_NID	0x11
78d949cac1SHarald Welte 
79d7426329SHarald Welte enum VIA_HDA_CODEC {
80d7426329SHarald Welte 	UNKNOWN = -1,
81d7426329SHarald Welte 	VT1708,
82d7426329SHarald Welte 	VT1709_10CH,
83d7426329SHarald Welte 	VT1709_6CH,
84d7426329SHarald Welte 	VT1708B_8CH,
85d7426329SHarald Welte 	VT1708B_4CH,
86d7426329SHarald Welte 	VT1708S,
87518bf3baSLydia Wang 	VT1708BCE,
88d7426329SHarald Welte 	VT1702,
89*eb7188caSLydia Wang 	VT1718S,
90d7426329SHarald Welte 	CODEC_TYPES,
91d7426329SHarald Welte };
92d7426329SHarald Welte 
931f2e99feSLydia Wang struct via_spec {
941f2e99feSLydia Wang 	/* codec parameterization */
951f2e99feSLydia Wang 	struct snd_kcontrol_new *mixers[4];
961f2e99feSLydia Wang 	unsigned int num_mixers;
971f2e99feSLydia Wang 
981f2e99feSLydia Wang 	struct hda_verb *init_verbs[5];
991f2e99feSLydia Wang 	unsigned int num_iverbs;
1001f2e99feSLydia Wang 
1011f2e99feSLydia Wang 	char *stream_name_analog;
1021f2e99feSLydia Wang 	struct hda_pcm_stream *stream_analog_playback;
1031f2e99feSLydia Wang 	struct hda_pcm_stream *stream_analog_capture;
1041f2e99feSLydia Wang 
1051f2e99feSLydia Wang 	char *stream_name_digital;
1061f2e99feSLydia Wang 	struct hda_pcm_stream *stream_digital_playback;
1071f2e99feSLydia Wang 	struct hda_pcm_stream *stream_digital_capture;
1081f2e99feSLydia Wang 
1091f2e99feSLydia Wang 	/* playback */
1101f2e99feSLydia Wang 	struct hda_multi_out multiout;
1111f2e99feSLydia Wang 	hda_nid_t slave_dig_outs[2];
1121f2e99feSLydia Wang 
1131f2e99feSLydia Wang 	/* capture */
1141f2e99feSLydia Wang 	unsigned int num_adc_nids;
1151f2e99feSLydia Wang 	hda_nid_t *adc_nids;
1161f2e99feSLydia Wang 	hda_nid_t mux_nids[3];
1171f2e99feSLydia Wang 	hda_nid_t dig_in_nid;
1181f2e99feSLydia Wang 	hda_nid_t dig_in_pin;
1191f2e99feSLydia Wang 
1201f2e99feSLydia Wang 	/* capture source */
1211f2e99feSLydia Wang 	const struct hda_input_mux *input_mux;
1221f2e99feSLydia Wang 	unsigned int cur_mux[3];
1231f2e99feSLydia Wang 
1241f2e99feSLydia Wang 	/* PCM information */
1251f2e99feSLydia Wang 	struct hda_pcm pcm_rec[3];
1261f2e99feSLydia Wang 
1271f2e99feSLydia Wang 	/* dynamic controls, init_verbs and input_mux */
1281f2e99feSLydia Wang 	struct auto_pin_cfg autocfg;
1291f2e99feSLydia Wang 	struct snd_array kctls;
1301f2e99feSLydia Wang 	struct hda_input_mux private_imux[2];
1311f2e99feSLydia Wang 	hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
1321f2e99feSLydia Wang 
1331f2e99feSLydia Wang 	/* HP mode source */
1341f2e99feSLydia Wang 	const struct hda_input_mux *hp_mux;
1351f2e99feSLydia Wang 	unsigned int hp_independent_mode;
1361f2e99feSLydia Wang 	unsigned int hp_independent_mode_index;
1371f2e99feSLydia Wang 	unsigned int smart51_enabled;
1381f2e99feSLydia Wang 
1391f2e99feSLydia Wang 	enum VIA_HDA_CODEC codec_type;
1401f2e99feSLydia Wang 
1411f2e99feSLydia Wang 	/* work to check hp jack state */
1421f2e99feSLydia Wang 	struct hda_codec *codec;
1431f2e99feSLydia Wang 	struct delayed_work vt1708_hp_work;
1441f2e99feSLydia Wang 	int vt1708_jack_detectect;
1451f2e99feSLydia Wang 	int vt1708_hp_present;
1461f2e99feSLydia Wang #ifdef CONFIG_SND_HDA_POWER_SAVE
1471f2e99feSLydia Wang 	struct hda_loopback_check loopback;
1481f2e99feSLydia Wang #endif
1491f2e99feSLydia Wang };
1501f2e99feSLydia Wang 
151744ff5f4SLydia Wang static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
152d7426329SHarald Welte {
153744ff5f4SLydia Wang 	u32 vendor_id = codec->vendor_id;
154d7426329SHarald Welte 	u16 ven_id = vendor_id >> 16;
155d7426329SHarald Welte 	u16 dev_id = vendor_id & 0xffff;
156d7426329SHarald Welte 	enum VIA_HDA_CODEC codec_type;
157d7426329SHarald Welte 
158d7426329SHarald Welte 	/* get codec type */
159d7426329SHarald Welte 	if (ven_id != 0x1106)
160d7426329SHarald Welte 		codec_type = UNKNOWN;
161d7426329SHarald Welte 	else if (dev_id >= 0x1708 && dev_id <= 0x170b)
162d7426329SHarald Welte 		codec_type = VT1708;
163d7426329SHarald Welte 	else if (dev_id >= 0xe710 && dev_id <= 0xe713)
164d7426329SHarald Welte 		codec_type = VT1709_10CH;
165d7426329SHarald Welte 	else if (dev_id >= 0xe714 && dev_id <= 0xe717)
166d7426329SHarald Welte 		codec_type = VT1709_6CH;
167518bf3baSLydia Wang 	else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
168d7426329SHarald Welte 		codec_type = VT1708B_8CH;
169518bf3baSLydia Wang 		if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
170518bf3baSLydia Wang 			codec_type = VT1708BCE;
171518bf3baSLydia Wang 	} else if (dev_id >= 0xe724 && dev_id <= 0xe727)
172d7426329SHarald Welte 		codec_type = VT1708B_4CH;
173d7426329SHarald Welte 	else if ((dev_id & 0xfff) == 0x397
174d7426329SHarald Welte 		 && (dev_id >> 12) < 8)
175d7426329SHarald Welte 		codec_type = VT1708S;
176d7426329SHarald Welte 	else if ((dev_id & 0xfff) == 0x398
177d7426329SHarald Welte 		 && (dev_id >> 12) < 8)
178d7426329SHarald Welte 		codec_type = VT1702;
179*eb7188caSLydia Wang 	else if ((dev_id & 0xfff) == 0x428
180*eb7188caSLydia Wang 		 && (dev_id >> 12) < 8)
181*eb7188caSLydia Wang 		codec_type = VT1718S;
182d7426329SHarald Welte 	else
183d7426329SHarald Welte 		codec_type = UNKNOWN;
184d7426329SHarald Welte 	return codec_type;
185d7426329SHarald Welte };
186d7426329SHarald Welte 
18769e52a80SHarald Welte #define VIA_HP_EVENT		0x01
18869e52a80SHarald Welte #define VIA_GPIO_EVENT		0x02
189a34df19aSLydia Wang #define VIA_JACK_EVENT		0x04
19069e52a80SHarald Welte 
191c577b8a1SJoseph Chan enum {
192c577b8a1SJoseph Chan 	VIA_CTL_WIDGET_VOL,
193c577b8a1SJoseph Chan 	VIA_CTL_WIDGET_MUTE,
194f5271101SLydia Wang 	VIA_CTL_WIDGET_ANALOG_MUTE,
195c577b8a1SJoseph Chan };
196c577b8a1SJoseph Chan 
197c577b8a1SJoseph Chan enum {
198eb14a46cSHarald Welte 	AUTO_SEQ_FRONT = 0,
199c577b8a1SJoseph Chan 	AUTO_SEQ_SURROUND,
200c577b8a1SJoseph Chan 	AUTO_SEQ_CENLFE,
201c577b8a1SJoseph Chan 	AUTO_SEQ_SIDE
202c577b8a1SJoseph Chan };
203c577b8a1SJoseph Chan 
204f5271101SLydia Wang static void analog_low_current_mode(struct hda_codec *codec, int stream_idle);
205f5271101SLydia Wang static void set_jack_power_state(struct hda_codec *codec);
2061f2e99feSLydia Wang static int is_aa_path_mute(struct hda_codec *codec);
2071f2e99feSLydia Wang 
2081f2e99feSLydia Wang static void vt1708_start_hp_work(struct via_spec *spec)
2091f2e99feSLydia Wang {
2101f2e99feSLydia Wang 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
2111f2e99feSLydia Wang 		return;
2121f2e99feSLydia Wang 	snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
2131f2e99feSLydia Wang 			    !spec->vt1708_jack_detectect);
2141f2e99feSLydia Wang 	if (!delayed_work_pending(&spec->vt1708_hp_work))
2151f2e99feSLydia Wang 		schedule_delayed_work(&spec->vt1708_hp_work,
2161f2e99feSLydia Wang 				      msecs_to_jiffies(100));
2171f2e99feSLydia Wang }
2181f2e99feSLydia Wang 
2191f2e99feSLydia Wang static void vt1708_stop_hp_work(struct via_spec *spec)
2201f2e99feSLydia Wang {
2211f2e99feSLydia Wang 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
2221f2e99feSLydia Wang 		return;
2231f2e99feSLydia Wang 	if (snd_hda_get_bool_hint(spec->codec, "analog_loopback_hp_detect") == 1
2241f2e99feSLydia Wang 	    && !is_aa_path_mute(spec->codec))
2251f2e99feSLydia Wang 		return;
2261f2e99feSLydia Wang 	snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
2271f2e99feSLydia Wang 			    !spec->vt1708_jack_detectect);
2281f2e99feSLydia Wang 	cancel_delayed_work(&spec->vt1708_hp_work);
2291f2e99feSLydia Wang 	flush_scheduled_work();
2301f2e99feSLydia Wang }
231f5271101SLydia Wang 
232f5271101SLydia Wang static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
233f5271101SLydia Wang 				   struct snd_ctl_elem_value *ucontrol)
234f5271101SLydia Wang {
235f5271101SLydia Wang 	int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
236f5271101SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
237f5271101SLydia Wang 
238f5271101SLydia Wang 	set_jack_power_state(codec);
239f5271101SLydia Wang 	analog_low_current_mode(snd_kcontrol_chip(kcontrol), -1);
2401f2e99feSLydia Wang 	if (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1) {
2411f2e99feSLydia Wang 		if (is_aa_path_mute(codec))
2421f2e99feSLydia Wang 			vt1708_start_hp_work(codec->spec);
2431f2e99feSLydia Wang 		else
2441f2e99feSLydia Wang 			vt1708_stop_hp_work(codec->spec);
2451f2e99feSLydia Wang 	}
246f5271101SLydia Wang 	return change;
247f5271101SLydia Wang }
248f5271101SLydia Wang 
249f5271101SLydia Wang /* modify .put = snd_hda_mixer_amp_switch_put */
250f5271101SLydia Wang #define ANALOG_INPUT_MUTE						\
251f5271101SLydia Wang 	{		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,		\
252f5271101SLydia Wang 			.name = NULL,					\
253f5271101SLydia Wang 			.index = 0,					\
254f5271101SLydia Wang 			.info = snd_hda_mixer_amp_switch_info,		\
255f5271101SLydia Wang 			.get = snd_hda_mixer_amp_switch_get,		\
256f5271101SLydia Wang 			.put = analog_input_switch_put,			\
257f5271101SLydia Wang 			.private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
258f5271101SLydia Wang 
259c577b8a1SJoseph Chan static struct snd_kcontrol_new vt1708_control_templates[] = {
260c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME(NULL, 0, 0, 0),
261c577b8a1SJoseph Chan 	HDA_CODEC_MUTE(NULL, 0, 0, 0),
262f5271101SLydia Wang 	ANALOG_INPUT_MUTE,
263c577b8a1SJoseph Chan };
264c577b8a1SJoseph Chan 
265c577b8a1SJoseph Chan 
266c577b8a1SJoseph Chan static hda_nid_t vt1708_adc_nids[2] = {
267c577b8a1SJoseph Chan 	/* ADC1-2 */
268c577b8a1SJoseph Chan 	0x15, 0x27
269c577b8a1SJoseph Chan };
270c577b8a1SJoseph Chan 
271c577b8a1SJoseph Chan static hda_nid_t vt1709_adc_nids[3] = {
272c577b8a1SJoseph Chan 	/* ADC1-2 */
273c577b8a1SJoseph Chan 	0x14, 0x15, 0x16
274c577b8a1SJoseph Chan };
275c577b8a1SJoseph Chan 
276f7278fd0SJosepch Chan static hda_nid_t vt1708B_adc_nids[2] = {
277f7278fd0SJosepch Chan 	/* ADC1-2 */
278f7278fd0SJosepch Chan 	0x13, 0x14
279f7278fd0SJosepch Chan };
280f7278fd0SJosepch Chan 
281d949cac1SHarald Welte static hda_nid_t vt1708S_adc_nids[2] = {
282d949cac1SHarald Welte 	/* ADC1-2 */
283d949cac1SHarald Welte 	0x13, 0x14
284d949cac1SHarald Welte };
285d949cac1SHarald Welte 
286d949cac1SHarald Welte static hda_nid_t vt1702_adc_nids[3] = {
287d949cac1SHarald Welte 	/* ADC1-2 */
288d949cac1SHarald Welte 	0x12, 0x20, 0x1F
289d949cac1SHarald Welte };
290d949cac1SHarald Welte 
291*eb7188caSLydia Wang static hda_nid_t vt1718S_adc_nids[2] = {
292*eb7188caSLydia Wang 	/* ADC1-2 */
293*eb7188caSLydia Wang 	0x10, 0x11
294*eb7188caSLydia Wang };
295*eb7188caSLydia Wang 
296c577b8a1SJoseph Chan /* add dynamic controls */
297c577b8a1SJoseph Chan static int via_add_control(struct via_spec *spec, int type, const char *name,
298c577b8a1SJoseph Chan 			   unsigned long val)
299c577b8a1SJoseph Chan {
300c577b8a1SJoseph Chan 	struct snd_kcontrol_new *knew;
301c577b8a1SJoseph Chan 
302603c4019STakashi Iwai 	snd_array_init(&spec->kctls, sizeof(*knew), 32);
303603c4019STakashi Iwai 	knew = snd_array_new(&spec->kctls);
304c577b8a1SJoseph Chan 	if (!knew)
305c577b8a1SJoseph Chan 		return -ENOMEM;
306c577b8a1SJoseph Chan 	*knew = vt1708_control_templates[type];
307c577b8a1SJoseph Chan 	knew->name = kstrdup(name, GFP_KERNEL);
308c577b8a1SJoseph Chan 	if (!knew->name)
309c577b8a1SJoseph Chan 		return -ENOMEM;
310c577b8a1SJoseph Chan 	knew->private_value = val;
311c577b8a1SJoseph Chan 	return 0;
312c577b8a1SJoseph Chan }
313c577b8a1SJoseph Chan 
314603c4019STakashi Iwai static void via_free_kctls(struct hda_codec *codec)
315603c4019STakashi Iwai {
316603c4019STakashi Iwai 	struct via_spec *spec = codec->spec;
317603c4019STakashi Iwai 
318603c4019STakashi Iwai 	if (spec->kctls.list) {
319603c4019STakashi Iwai 		struct snd_kcontrol_new *kctl = spec->kctls.list;
320603c4019STakashi Iwai 		int i;
321603c4019STakashi Iwai 		for (i = 0; i < spec->kctls.used; i++)
322603c4019STakashi Iwai 			kfree(kctl[i].name);
323603c4019STakashi Iwai 	}
324603c4019STakashi Iwai 	snd_array_free(&spec->kctls);
325603c4019STakashi Iwai }
326603c4019STakashi Iwai 
327c577b8a1SJoseph Chan /* create input playback/capture controls for the given pin */
3289510e8ddSLydia Wang static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
3299510e8ddSLydia Wang 				int idx, int mix_nid)
330c577b8a1SJoseph Chan {
331c577b8a1SJoseph Chan 	char name[32];
332c577b8a1SJoseph Chan 	int err;
333c577b8a1SJoseph Chan 
334c577b8a1SJoseph Chan 	sprintf(name, "%s Playback Volume", ctlname);
335c577b8a1SJoseph Chan 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
336c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
337c577b8a1SJoseph Chan 	if (err < 0)
338c577b8a1SJoseph Chan 		return err;
339c577b8a1SJoseph Chan 	sprintf(name, "%s Playback Switch", ctlname);
340f5271101SLydia Wang 	err = via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name,
341c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
342c577b8a1SJoseph Chan 	if (err < 0)
343c577b8a1SJoseph Chan 		return err;
344c577b8a1SJoseph Chan 	return 0;
345c577b8a1SJoseph Chan }
346c577b8a1SJoseph Chan 
347c577b8a1SJoseph Chan static void via_auto_set_output_and_unmute(struct hda_codec *codec,
348c577b8a1SJoseph Chan 					   hda_nid_t nid, int pin_type,
349c577b8a1SJoseph Chan 					   int dac_idx)
350c577b8a1SJoseph Chan {
351c577b8a1SJoseph Chan 	/* set as output */
352c577b8a1SJoseph Chan 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
353c577b8a1SJoseph Chan 			    pin_type);
354c577b8a1SJoseph Chan 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
355c577b8a1SJoseph Chan 			    AMP_OUT_UNMUTE);
356d3a11e60STakashi Iwai 	if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
357d3a11e60STakashi Iwai 		snd_hda_codec_write(codec, nid, 0,
358d3a11e60STakashi Iwai 				    AC_VERB_SET_EAPD_BTLENABLE, 0x02);
359c577b8a1SJoseph Chan }
360c577b8a1SJoseph Chan 
361c577b8a1SJoseph Chan 
362c577b8a1SJoseph Chan static void via_auto_init_multi_out(struct hda_codec *codec)
363c577b8a1SJoseph Chan {
364c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
365c577b8a1SJoseph Chan 	int i;
366c577b8a1SJoseph Chan 
367c577b8a1SJoseph Chan 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
368c577b8a1SJoseph Chan 		hda_nid_t nid = spec->autocfg.line_out_pins[i];
369c577b8a1SJoseph Chan 		if (nid)
370c577b8a1SJoseph Chan 			via_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
371c577b8a1SJoseph Chan 	}
372c577b8a1SJoseph Chan }
373c577b8a1SJoseph Chan 
374c577b8a1SJoseph Chan static void via_auto_init_hp_out(struct hda_codec *codec)
375c577b8a1SJoseph Chan {
376c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
377c577b8a1SJoseph Chan 	hda_nid_t pin;
378c577b8a1SJoseph Chan 
379c577b8a1SJoseph Chan 	pin = spec->autocfg.hp_pins[0];
380c577b8a1SJoseph Chan 	if (pin) /* connect to front */
381c577b8a1SJoseph Chan 		via_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
382c577b8a1SJoseph Chan }
383c577b8a1SJoseph Chan 
384c577b8a1SJoseph Chan static void via_auto_init_analog_input(struct hda_codec *codec)
385c577b8a1SJoseph Chan {
386c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
387c577b8a1SJoseph Chan 	int i;
388c577b8a1SJoseph Chan 
389c577b8a1SJoseph Chan 	for (i = 0; i < AUTO_PIN_LAST; i++) {
390c577b8a1SJoseph Chan 		hda_nid_t nid = spec->autocfg.input_pins[i];
391c577b8a1SJoseph Chan 
392c577b8a1SJoseph Chan 		snd_hda_codec_write(codec, nid, 0,
393c577b8a1SJoseph Chan 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
394c577b8a1SJoseph Chan 				    (i <= AUTO_PIN_FRONT_MIC ?
395c577b8a1SJoseph Chan 				     PIN_VREF50 : PIN_IN));
396c577b8a1SJoseph Chan 
397c577b8a1SJoseph Chan 	}
398c577b8a1SJoseph Chan }
399f5271101SLydia Wang 
4001564b287SLydia Wang static int is_smart51_pins(struct via_spec *spec, hda_nid_t pin);
4011564b287SLydia Wang 
402f5271101SLydia Wang static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
403f5271101SLydia Wang 				unsigned int *affected_parm)
404f5271101SLydia Wang {
405f5271101SLydia Wang 	unsigned parm;
406f5271101SLydia Wang 	unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
407f5271101SLydia Wang 	unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
408f5271101SLydia Wang 		>> AC_DEFCFG_MISC_SHIFT
409f5271101SLydia Wang 		& AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
410f5271101SLydia Wang 	unsigned present = snd_hda_codec_read(codec, nid, 0,
411f5271101SLydia Wang 					      AC_VERB_GET_PIN_SENSE, 0) >> 31;
4121564b287SLydia Wang 	struct via_spec *spec = codec->spec;
4131564b287SLydia Wang 	if ((spec->smart51_enabled && is_smart51_pins(spec, nid))
4141564b287SLydia Wang 	    || ((no_presence || present)
4151564b287SLydia Wang 		&& get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
416f5271101SLydia Wang 		*affected_parm = AC_PWRST_D0; /* if it's connected */
417f5271101SLydia Wang 		parm = AC_PWRST_D0;
418f5271101SLydia Wang 	} else
419f5271101SLydia Wang 		parm = AC_PWRST_D3;
420f5271101SLydia Wang 
421f5271101SLydia Wang 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
422f5271101SLydia Wang }
423f5271101SLydia Wang 
424f5271101SLydia Wang static void set_jack_power_state(struct hda_codec *codec)
425f5271101SLydia Wang {
426f5271101SLydia Wang 	struct via_spec *spec = codec->spec;
427f5271101SLydia Wang 	int imux_is_smixer;
428f5271101SLydia Wang 	unsigned int parm;
429f5271101SLydia Wang 
430f5271101SLydia Wang 	if (spec->codec_type == VT1702) {
431f5271101SLydia Wang 		imux_is_smixer = snd_hda_codec_read(
432f5271101SLydia Wang 			codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
433f5271101SLydia Wang 		/* inputs */
434f5271101SLydia Wang 		/* PW 1/2/5 (14h/15h/18h) */
435f5271101SLydia Wang 		parm = AC_PWRST_D3;
436f5271101SLydia Wang 		set_pin_power_state(codec, 0x14, &parm);
437f5271101SLydia Wang 		set_pin_power_state(codec, 0x15, &parm);
438f5271101SLydia Wang 		set_pin_power_state(codec, 0x18, &parm);
439f5271101SLydia Wang 		if (imux_is_smixer)
440f5271101SLydia Wang 			parm = AC_PWRST_D0; /* SW0 = stereo mixer (idx 3) */
441f5271101SLydia Wang 		/* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
442f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE,
443f5271101SLydia Wang 				    parm);
444f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x12, 0, AC_VERB_SET_POWER_STATE,
445f5271101SLydia Wang 				    parm);
446f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE,
447f5271101SLydia Wang 				    parm);
448f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_POWER_STATE,
449f5271101SLydia Wang 				    parm);
450f5271101SLydia Wang 
451f5271101SLydia Wang 		/* outputs */
452f5271101SLydia Wang 		/* PW 3/4 (16h/17h) */
453f5271101SLydia Wang 		parm = AC_PWRST_D3;
454f5271101SLydia Wang 		set_pin_power_state(codec, 0x16, &parm);
455f5271101SLydia Wang 		set_pin_power_state(codec, 0x17, &parm);
456f5271101SLydia Wang 		/* MW0 (1ah), AOW 0/1 (10h/1dh) */
457f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
458f5271101SLydia Wang 				    imux_is_smixer ? AC_PWRST_D0 : parm);
459f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
460f5271101SLydia Wang 				    parm);
461f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE,
462f5271101SLydia Wang 				    parm);
463f5271101SLydia Wang 	} else if (spec->codec_type == VT1708B_8CH
464f5271101SLydia Wang 		   || spec->codec_type == VT1708B_4CH
465f5271101SLydia Wang 		   || spec->codec_type == VT1708S) {
466f5271101SLydia Wang 		/* SW0 (17h) = stereo mixer */
467f5271101SLydia Wang 		int is_8ch = spec->codec_type != VT1708B_4CH;
468f5271101SLydia Wang 		imux_is_smixer = snd_hda_codec_read(
469f5271101SLydia Wang 			codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
470f5271101SLydia Wang 			== ((spec->codec_type == VT1708S)  ? 5 : 0);
471f5271101SLydia Wang 		/* inputs */
472f5271101SLydia Wang 		/* PW 1/2/5 (1ah/1bh/1eh) */
473f5271101SLydia Wang 		parm = AC_PWRST_D3;
474f5271101SLydia Wang 		set_pin_power_state(codec, 0x1a, &parm);
475f5271101SLydia Wang 		set_pin_power_state(codec, 0x1b, &parm);
476f5271101SLydia Wang 		set_pin_power_state(codec, 0x1e, &parm);
477f5271101SLydia Wang 		if (imux_is_smixer)
478f5271101SLydia Wang 			parm = AC_PWRST_D0;
479f5271101SLydia Wang 		/* SW0 (17h), AIW 0/1 (13h/14h) */
480f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE,
481f5271101SLydia Wang 				    parm);
482f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE,
483f5271101SLydia Wang 				    parm);
484f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE,
485f5271101SLydia Wang 				    parm);
486f5271101SLydia Wang 
487f5271101SLydia Wang 		/* outputs */
488f5271101SLydia Wang 		/* PW0 (19h), SW1 (18h), AOW1 (11h) */
489f5271101SLydia Wang 		parm = AC_PWRST_D3;
490f5271101SLydia Wang 		set_pin_power_state(codec, 0x19, &parm);
491f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE,
492f5271101SLydia Wang 				    parm);
493f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE,
494f5271101SLydia Wang 				    parm);
495f5271101SLydia Wang 
496f5271101SLydia Wang 		/* PW6 (22h), SW2 (26h), AOW2 (24h) */
497f5271101SLydia Wang 		if (is_8ch) {
498f5271101SLydia Wang 			parm = AC_PWRST_D3;
499f5271101SLydia Wang 			set_pin_power_state(codec, 0x22, &parm);
500f5271101SLydia Wang 			snd_hda_codec_write(codec, 0x26, 0,
501f5271101SLydia Wang 					    AC_VERB_SET_POWER_STATE, parm);
502f5271101SLydia Wang 			snd_hda_codec_write(codec, 0x24, 0,
503f5271101SLydia Wang 					    AC_VERB_SET_POWER_STATE, parm);
504f5271101SLydia Wang 		}
505f5271101SLydia Wang 
506f5271101SLydia Wang 		/* PW 3/4/7 (1ch/1dh/23h) */
507f5271101SLydia Wang 		parm = AC_PWRST_D3;
508f5271101SLydia Wang 		/* force to D0 for internal Speaker */
509f5271101SLydia Wang 		set_pin_power_state(codec, 0x1c, &parm);
510f5271101SLydia Wang 		set_pin_power_state(codec, 0x1d, &parm);
511f5271101SLydia Wang 		if (is_8ch)
512f5271101SLydia Wang 			set_pin_power_state(codec, 0x23, &parm);
513f5271101SLydia Wang 		/* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
514f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
515f5271101SLydia Wang 				    imux_is_smixer ? AC_PWRST_D0 : parm);
516f5271101SLydia Wang 		snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
517f5271101SLydia Wang 				    parm);
518f5271101SLydia Wang 		if (is_8ch) {
519f5271101SLydia Wang 			snd_hda_codec_write(codec, 0x25, 0,
520f5271101SLydia Wang 					    AC_VERB_SET_POWER_STATE, parm);
521f5271101SLydia Wang 			snd_hda_codec_write(codec, 0x27, 0,
522f5271101SLydia Wang 					    AC_VERB_SET_POWER_STATE, parm);
523f5271101SLydia Wang 		}
524*eb7188caSLydia Wang 	}  else if (spec->codec_type == VT1718S) {
525*eb7188caSLydia Wang 		/* MUX6 (1eh) = stereo mixer */
526*eb7188caSLydia Wang 		imux_is_smixer = snd_hda_codec_read(
527*eb7188caSLydia Wang 			codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
528*eb7188caSLydia Wang 		/* inputs */
529*eb7188caSLydia Wang 		/* PW 5/6/7 (29h/2ah/2bh) */
530*eb7188caSLydia Wang 		parm = AC_PWRST_D3;
531*eb7188caSLydia Wang 		set_pin_power_state(codec, 0x29, &parm);
532*eb7188caSLydia Wang 		set_pin_power_state(codec, 0x2a, &parm);
533*eb7188caSLydia Wang 		set_pin_power_state(codec, 0x2b, &parm);
534*eb7188caSLydia Wang 		if (imux_is_smixer)
535*eb7188caSLydia Wang 			parm = AC_PWRST_D0;
536*eb7188caSLydia Wang 		/* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
537*eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE,
538*eb7188caSLydia Wang 				    parm);
539*eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE,
540*eb7188caSLydia Wang 				    parm);
541*eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
542*eb7188caSLydia Wang 				    parm);
543*eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE,
544*eb7188caSLydia Wang 				    parm);
545*eb7188caSLydia Wang 
546*eb7188caSLydia Wang 		/* outputs */
547*eb7188caSLydia Wang 		/* PW3 (27h), MW2 (1ah), AOW3 (bh) */
548*eb7188caSLydia Wang 		parm = AC_PWRST_D3;
549*eb7188caSLydia Wang 		set_pin_power_state(codec, 0x27, &parm);
550*eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
551*eb7188caSLydia Wang 				    parm);
552*eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0xb, 0, AC_VERB_SET_POWER_STATE,
553*eb7188caSLydia Wang 				    parm);
554*eb7188caSLydia Wang 
555*eb7188caSLydia Wang 		/* PW2 (26h), AOW2 (ah) */
556*eb7188caSLydia Wang 		parm = AC_PWRST_D3;
557*eb7188caSLydia Wang 		set_pin_power_state(codec, 0x26, &parm);
558*eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0xa, 0, AC_VERB_SET_POWER_STATE,
559*eb7188caSLydia Wang 				    parm);
560*eb7188caSLydia Wang 
561*eb7188caSLydia Wang 		/* PW0/1 (24h/25h) */
562*eb7188caSLydia Wang 		parm = AC_PWRST_D3;
563*eb7188caSLydia Wang 		set_pin_power_state(codec, 0x24, &parm);
564*eb7188caSLydia Wang 		set_pin_power_state(codec, 0x25, &parm);
565*eb7188caSLydia Wang 		if (!spec->hp_independent_mode) /* check for redirected HP */
566*eb7188caSLydia Wang 			set_pin_power_state(codec, 0x28, &parm);
567*eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE,
568*eb7188caSLydia Wang 				    parm);
569*eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0x9, 0, AC_VERB_SET_POWER_STATE,
570*eb7188caSLydia Wang 				    parm);
571*eb7188caSLydia Wang 		/* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
572*eb7188caSLydia Wang 		snd_hda_codec_write(codec, 0x21, 0, AC_VERB_SET_POWER_STATE,
573*eb7188caSLydia Wang 				    imux_is_smixer ? AC_PWRST_D0 : parm);
574*eb7188caSLydia Wang 		if (spec->hp_independent_mode) {
575*eb7188caSLydia Wang 			/* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
576*eb7188caSLydia Wang 			parm = AC_PWRST_D3;
577*eb7188caSLydia Wang 			set_pin_power_state(codec, 0x28, &parm);
578*eb7188caSLydia Wang 			snd_hda_codec_write(codec, 0x1b, 0,
579*eb7188caSLydia Wang 					    AC_VERB_SET_POWER_STATE, parm);
580*eb7188caSLydia Wang 			snd_hda_codec_write(codec, 0x34, 0,
581*eb7188caSLydia Wang 					    AC_VERB_SET_POWER_STATE, parm);
582*eb7188caSLydia Wang 			snd_hda_codec_write(codec, 0xc, 0,
583*eb7188caSLydia Wang 					    AC_VERB_SET_POWER_STATE, parm);
584*eb7188caSLydia Wang 		}
585f5271101SLydia Wang 	}
586f5271101SLydia Wang }
587f5271101SLydia Wang 
588c577b8a1SJoseph Chan /*
589c577b8a1SJoseph Chan  * input MUX handling
590c577b8a1SJoseph Chan  */
591c577b8a1SJoseph Chan static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
592c577b8a1SJoseph Chan 			     struct snd_ctl_elem_info *uinfo)
593c577b8a1SJoseph Chan {
594c577b8a1SJoseph Chan 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
595c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
596c577b8a1SJoseph Chan 	return snd_hda_input_mux_info(spec->input_mux, uinfo);
597c577b8a1SJoseph Chan }
598c577b8a1SJoseph Chan 
599c577b8a1SJoseph Chan static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
600c577b8a1SJoseph Chan 			    struct snd_ctl_elem_value *ucontrol)
601c577b8a1SJoseph Chan {
602c577b8a1SJoseph Chan 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
603c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
604c577b8a1SJoseph Chan 	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
605c577b8a1SJoseph Chan 
606c577b8a1SJoseph Chan 	ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
607c577b8a1SJoseph Chan 	return 0;
608c577b8a1SJoseph Chan }
609c577b8a1SJoseph Chan 
610c577b8a1SJoseph Chan static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
611c577b8a1SJoseph Chan 			    struct snd_ctl_elem_value *ucontrol)
612c577b8a1SJoseph Chan {
613c577b8a1SJoseph Chan 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
614c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
615c577b8a1SJoseph Chan 	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
616c577b8a1SJoseph Chan 
617337b9d02STakashi Iwai 	if (!spec->mux_nids[adc_idx])
618337b9d02STakashi Iwai 		return -EINVAL;
619a80e6e3cSLydia Wang 	/* switch to D0 beofre change index */
620a80e6e3cSLydia Wang 	if (snd_hda_codec_read(codec, spec->mux_nids[adc_idx], 0,
621a80e6e3cSLydia Wang 			       AC_VERB_GET_POWER_STATE, 0x00) != AC_PWRST_D0)
622a80e6e3cSLydia Wang 		snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
623a80e6e3cSLydia Wang 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
624a80e6e3cSLydia Wang 	/* update jack power state */
625a80e6e3cSLydia Wang 	set_jack_power_state(codec);
626a80e6e3cSLydia Wang 
627c577b8a1SJoseph Chan 	return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
628337b9d02STakashi Iwai 				     spec->mux_nids[adc_idx],
629c577b8a1SJoseph Chan 				     &spec->cur_mux[adc_idx]);
630c577b8a1SJoseph Chan }
631c577b8a1SJoseph Chan 
6320aa62aefSHarald Welte static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
6330aa62aefSHarald Welte 				   struct snd_ctl_elem_info *uinfo)
6340aa62aefSHarald Welte {
6350aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6360aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
6370aa62aefSHarald Welte 	return snd_hda_input_mux_info(spec->hp_mux, uinfo);
6380aa62aefSHarald Welte }
6390aa62aefSHarald Welte 
6400aa62aefSHarald Welte static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
6410aa62aefSHarald Welte 				  struct snd_ctl_elem_value *ucontrol)
6420aa62aefSHarald Welte {
6430aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6440aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
645*eb7188caSLydia Wang 	hda_nid_t nid;
646*eb7188caSLydia Wang 	unsigned int pinsel;
647*eb7188caSLydia Wang 
648*eb7188caSLydia Wang 	switch (spec->codec_type) {
649*eb7188caSLydia Wang 	case VT1718S:
650*eb7188caSLydia Wang 		nid = 0x34;
651*eb7188caSLydia Wang 		break;
652*eb7188caSLydia Wang 	default:
653*eb7188caSLydia Wang 		nid = spec->autocfg.hp_pins[0];
654*eb7188caSLydia Wang 		break;
655*eb7188caSLydia Wang 	}
656*eb7188caSLydia Wang 	/* use !! to translate conn sel 2 for VT1718S */
657*eb7188caSLydia Wang 	pinsel = !!snd_hda_codec_read(codec, nid, 0,
6580aa62aefSHarald Welte 				      AC_VERB_GET_CONNECT_SEL,
6590aa62aefSHarald Welte 				      0x00);
6600aa62aefSHarald Welte 	ucontrol->value.enumerated.item[0] = pinsel;
6610aa62aefSHarald Welte 
6620aa62aefSHarald Welte 	return 0;
6630aa62aefSHarald Welte }
6640aa62aefSHarald Welte 
6650713efebSLydia Wang static void activate_ctl(struct hda_codec *codec, const char *name, int active)
6660713efebSLydia Wang {
6670713efebSLydia Wang 	struct snd_kcontrol *ctl = snd_hda_find_mixer_ctl(codec, name);
6680713efebSLydia Wang 	if (ctl) {
6690713efebSLydia Wang 		ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
6700713efebSLydia Wang 		ctl->vd[0].access |= active
6710713efebSLydia Wang 			? 0 : SNDRV_CTL_ELEM_ACCESS_INACTIVE;
6720713efebSLydia Wang 		snd_ctl_notify(codec->bus->card,
6730713efebSLydia Wang 			       SNDRV_CTL_EVENT_MASK_VALUE, &ctl->id);
6740713efebSLydia Wang 	}
6750713efebSLydia Wang }
6760713efebSLydia Wang 
677cdc1784dSLydia Wang static int update_side_mute_status(struct hda_codec *codec)
678cdc1784dSLydia Wang {
679cdc1784dSLydia Wang 	/* mute side channel */
680cdc1784dSLydia Wang 	struct via_spec *spec = codec->spec;
681cdc1784dSLydia Wang 	unsigned int parm = spec->hp_independent_mode
682cdc1784dSLydia Wang 		? AMP_OUT_MUTE : AMP_OUT_UNMUTE;
683cdc1784dSLydia Wang 	hda_nid_t sw3;
684cdc1784dSLydia Wang 
685cdc1784dSLydia Wang 	switch (spec->codec_type) {
686cdc1784dSLydia Wang 	case VT1708:
687cdc1784dSLydia Wang 		sw3 = 0x1b;
688cdc1784dSLydia Wang 		break;
689cdc1784dSLydia Wang 	case VT1709_10CH:
690cdc1784dSLydia Wang 		sw3 = 0x29;
691cdc1784dSLydia Wang 		break;
692cdc1784dSLydia Wang 	case VT1708B_8CH:
693cdc1784dSLydia Wang 	case VT1708S:
694cdc1784dSLydia Wang 		sw3 = 0x27;
695cdc1784dSLydia Wang 		break;
696cdc1784dSLydia Wang 	default:
697cdc1784dSLydia Wang 		sw3 = 0;
698cdc1784dSLydia Wang 		break;
699cdc1784dSLydia Wang 	}
700cdc1784dSLydia Wang 
701cdc1784dSLydia Wang 	if (sw3)
702cdc1784dSLydia Wang 		snd_hda_codec_write(codec, sw3, 0, AC_VERB_SET_AMP_GAIN_MUTE,
703cdc1784dSLydia Wang 				    parm);
704cdc1784dSLydia Wang 	return 0;
705cdc1784dSLydia Wang }
706cdc1784dSLydia Wang 
7070aa62aefSHarald Welte static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
7080aa62aefSHarald Welte 				  struct snd_ctl_elem_value *ucontrol)
7090aa62aefSHarald Welte {
7100aa62aefSHarald Welte 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7110aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
7120aa62aefSHarald Welte 	hda_nid_t nid = spec->autocfg.hp_pins[0];
7130aa62aefSHarald Welte 	unsigned int pinsel = ucontrol->value.enumerated.item[0];
714cdc1784dSLydia Wang 	/* Get Independent Mode index of headphone pin widget */
715cdc1784dSLydia Wang 	spec->hp_independent_mode = spec->hp_independent_mode_index == pinsel
716cdc1784dSLydia Wang 		? 1 : 0;
7170aa62aefSHarald Welte 
718*eb7188caSLydia Wang 	switch (spec->codec_type) {
719*eb7188caSLydia Wang 	case VT1718S:
720*eb7188caSLydia Wang 		nid = 0x34;
721*eb7188caSLydia Wang 		pinsel = pinsel ? 2 : 0; /* indep HP use AOW4 (index 2) */
722*eb7188caSLydia Wang 		spec->multiout.num_dacs = 4;
723*eb7188caSLydia Wang 		break;
724*eb7188caSLydia Wang 	default:
725*eb7188caSLydia Wang 		nid = spec->autocfg.hp_pins[0];
726*eb7188caSLydia Wang 		break;
727*eb7188caSLydia Wang 	}
728cdc1784dSLydia Wang 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, pinsel);
7290aa62aefSHarald Welte 
730cdc1784dSLydia Wang 	if (spec->multiout.hp_nid && spec->multiout.hp_nid
731cdc1784dSLydia Wang 	    != spec->multiout.dac_nids[HDA_FRONT])
732cdc1784dSLydia Wang 		snd_hda_codec_setup_stream(codec, spec->multiout.hp_nid,
7330aa62aefSHarald Welte 					   0, 0, 0);
7340aa62aefSHarald Welte 
735cdc1784dSLydia Wang 	update_side_mute_status(codec);
7360713efebSLydia Wang 	/* update HP volume/swtich active state */
7370713efebSLydia Wang 	if (spec->codec_type == VT1708S
738*eb7188caSLydia Wang 	    || spec->codec_type == VT1702
739*eb7188caSLydia Wang 	    || spec->codec_type == VT1718S) {
7400713efebSLydia Wang 		activate_ctl(codec, "Headphone Playback Volume",
7410713efebSLydia Wang 			     spec->hp_independent_mode);
7420713efebSLydia Wang 		activate_ctl(codec, "Headphone Playback Switch",
7430713efebSLydia Wang 			     spec->hp_independent_mode);
7440713efebSLydia Wang 	}
7450aa62aefSHarald Welte 	return 0;
7460aa62aefSHarald Welte }
7470aa62aefSHarald Welte 
7480aa62aefSHarald Welte static struct snd_kcontrol_new via_hp_mixer[] = {
7490aa62aefSHarald Welte 	{
7500aa62aefSHarald Welte 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7510aa62aefSHarald Welte 		.name = "Independent HP",
7520aa62aefSHarald Welte 		.count = 1,
7530aa62aefSHarald Welte 		.info = via_independent_hp_info,
7540aa62aefSHarald Welte 		.get = via_independent_hp_get,
7550aa62aefSHarald Welte 		.put = via_independent_hp_put,
7560aa62aefSHarald Welte 	},
7570aa62aefSHarald Welte 	{ } /* end */
7580aa62aefSHarald Welte };
7590aa62aefSHarald Welte 
7601564b287SLydia Wang static void notify_aa_path_ctls(struct hda_codec *codec)
7611564b287SLydia Wang {
7621564b287SLydia Wang 	int i;
7631564b287SLydia Wang 	struct snd_ctl_elem_id id;
7641564b287SLydia Wang 	const char *labels[] = {"Mic", "Front Mic", "Line"};
7651564b287SLydia Wang 
7661564b287SLydia Wang 	memset(&id, 0, sizeof(id));
7671564b287SLydia Wang 	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
7681564b287SLydia Wang 	for (i = 0; i < ARRAY_SIZE(labels); i++) {
7691564b287SLydia Wang 		sprintf(id.name, "%s Playback Volume", labels[i]);
7701564b287SLydia Wang 		snd_ctl_notify(codec->bus->card, SNDRV_CTL_EVENT_MASK_VALUE,
7711564b287SLydia Wang 			       &id);
7721564b287SLydia Wang 	}
7731564b287SLydia Wang }
7741564b287SLydia Wang 
7751564b287SLydia Wang static void mute_aa_path(struct hda_codec *codec, int mute)
7761564b287SLydia Wang {
7771564b287SLydia Wang 	struct via_spec *spec = codec->spec;
7781564b287SLydia Wang 	hda_nid_t  nid_mixer;
7791564b287SLydia Wang 	int start_idx;
7801564b287SLydia Wang 	int end_idx;
7811564b287SLydia Wang 	int i;
7821564b287SLydia Wang 	/* get nid of MW0 and start & end index */
7831564b287SLydia Wang 	switch (spec->codec_type) {
7841564b287SLydia Wang 	case VT1708:
7851564b287SLydia Wang 		nid_mixer = 0x17;
7861564b287SLydia Wang 		start_idx = 2;
7871564b287SLydia Wang 		end_idx = 4;
7881564b287SLydia Wang 		break;
7891564b287SLydia Wang 	case VT1709_10CH:
7901564b287SLydia Wang 	case VT1709_6CH:
7911564b287SLydia Wang 		nid_mixer = 0x18;
7921564b287SLydia Wang 		start_idx = 2;
7931564b287SLydia Wang 		end_idx = 4;
7941564b287SLydia Wang 		break;
7951564b287SLydia Wang 	case VT1708B_8CH:
7961564b287SLydia Wang 	case VT1708B_4CH:
7971564b287SLydia Wang 	case VT1708S:
7981564b287SLydia Wang 		nid_mixer = 0x16;
7991564b287SLydia Wang 		start_idx = 2;
8001564b287SLydia Wang 		end_idx = 4;
8011564b287SLydia Wang 		break;
8021564b287SLydia Wang 	default:
8031564b287SLydia Wang 		return;
8041564b287SLydia Wang 	}
8051564b287SLydia Wang 	/* check AA path's mute status */
8061564b287SLydia Wang 	for (i = start_idx; i <= end_idx; i++) {
8071564b287SLydia Wang 		int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
8081564b287SLydia Wang 		snd_hda_codec_amp_stereo(codec, nid_mixer, HDA_INPUT, i,
8091564b287SLydia Wang 					 HDA_AMP_MUTE, val);
8101564b287SLydia Wang 	}
8111564b287SLydia Wang }
8121564b287SLydia Wang static int is_smart51_pins(struct via_spec *spec, hda_nid_t pin)
8131564b287SLydia Wang {
8141564b287SLydia Wang 	int res = 0;
8151564b287SLydia Wang 	int index;
8161564b287SLydia Wang 	for (index = AUTO_PIN_MIC; index < AUTO_PIN_FRONT_LINE; index++) {
8171564b287SLydia Wang 		if (pin == spec->autocfg.input_pins[index]) {
8181564b287SLydia Wang 			res = 1;
8191564b287SLydia Wang 			break;
8201564b287SLydia Wang 		}
8211564b287SLydia Wang 	}
8221564b287SLydia Wang 	return res;
8231564b287SLydia Wang }
8241564b287SLydia Wang 
8251564b287SLydia Wang static int via_smart51_info(struct snd_kcontrol *kcontrol,
8261564b287SLydia Wang 			    struct snd_ctl_elem_info *uinfo)
8271564b287SLydia Wang {
8281564b287SLydia Wang 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
8291564b287SLydia Wang 	uinfo->count = 1;
8301564b287SLydia Wang 	uinfo->value.integer.min = 0;
8311564b287SLydia Wang 	uinfo->value.integer.max = 1;
8321564b287SLydia Wang 	return 0;
8331564b287SLydia Wang }
8341564b287SLydia Wang 
8351564b287SLydia Wang static int via_smart51_get(struct snd_kcontrol *kcontrol,
8361564b287SLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
8371564b287SLydia Wang {
8381564b287SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
8391564b287SLydia Wang 	struct via_spec *spec = codec->spec;
8401564b287SLydia Wang 	int index[] = { AUTO_PIN_MIC, AUTO_PIN_FRONT_MIC, AUTO_PIN_LINE };
8411564b287SLydia Wang 	int on = 1;
8421564b287SLydia Wang 	int i;
8431564b287SLydia Wang 
8441564b287SLydia Wang 	for (i = 0; i < ARRAY_SIZE(index); i++) {
8451564b287SLydia Wang 		hda_nid_t nid = spec->autocfg.input_pins[index[i]];
8461564b287SLydia Wang 		if (nid) {
8471564b287SLydia Wang 			int ctl =
8481564b287SLydia Wang 			    snd_hda_codec_read(codec, nid, 0,
8491564b287SLydia Wang 					       AC_VERB_GET_PIN_WIDGET_CONTROL,
8501564b287SLydia Wang 					       0);
8511564b287SLydia Wang 			if (i == AUTO_PIN_FRONT_MIC
852*eb7188caSLydia Wang 			    && spec->hp_independent_mode
853*eb7188caSLydia Wang 			    && spec->codec_type != VT1718S)
8541564b287SLydia Wang 				continue; /* ignore FMic for independent HP */
8551564b287SLydia Wang 			if (ctl & AC_PINCTL_IN_EN
8561564b287SLydia Wang 			    && !(ctl & AC_PINCTL_OUT_EN))
8571564b287SLydia Wang 				on = 0;
8581564b287SLydia Wang 		}
8591564b287SLydia Wang 	}
8601564b287SLydia Wang 	*ucontrol->value.integer.value = on;
8611564b287SLydia Wang 	return 0;
8621564b287SLydia Wang }
8631564b287SLydia Wang 
8641564b287SLydia Wang static int via_smart51_put(struct snd_kcontrol *kcontrol,
8651564b287SLydia Wang 			   struct snd_ctl_elem_value *ucontrol)
8661564b287SLydia Wang {
8671564b287SLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
8681564b287SLydia Wang 	struct via_spec *spec = codec->spec;
8691564b287SLydia Wang 	int out_in = *ucontrol->value.integer.value
8701564b287SLydia Wang 		? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
8711564b287SLydia Wang 	int index[] = { AUTO_PIN_MIC, AUTO_PIN_FRONT_MIC, AUTO_PIN_LINE };
8721564b287SLydia Wang 	int i;
8731564b287SLydia Wang 
8741564b287SLydia Wang 	for (i = 0; i < ARRAY_SIZE(index); i++) {
8751564b287SLydia Wang 		hda_nid_t nid = spec->autocfg.input_pins[index[i]];
8761564b287SLydia Wang 		if (i == AUTO_PIN_FRONT_MIC
877*eb7188caSLydia Wang 		    && spec->hp_independent_mode
878*eb7188caSLydia Wang 		    && spec->codec_type != VT1718S)
8791564b287SLydia Wang 			continue; /* don't retask FMic for independent HP */
8801564b287SLydia Wang 		if (nid) {
8811564b287SLydia Wang 			unsigned int parm = snd_hda_codec_read(
8821564b287SLydia Wang 				codec, nid, 0,
8831564b287SLydia Wang 				AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
8841564b287SLydia Wang 			parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
8851564b287SLydia Wang 			parm |= out_in;
8861564b287SLydia Wang 			snd_hda_codec_write(codec, nid, 0,
8871564b287SLydia Wang 					    AC_VERB_SET_PIN_WIDGET_CONTROL,
8881564b287SLydia Wang 					    parm);
8891564b287SLydia Wang 			if (out_in == AC_PINCTL_OUT_EN) {
8901564b287SLydia Wang 				mute_aa_path(codec, 1);
8911564b287SLydia Wang 				notify_aa_path_ctls(codec);
8921564b287SLydia Wang 			}
893*eb7188caSLydia Wang 			if (spec->codec_type == VT1718S)
894*eb7188caSLydia Wang 				snd_hda_codec_amp_stereo(
895*eb7188caSLydia Wang 					codec, nid, HDA_OUTPUT, 0, HDA_AMP_MUTE,
896*eb7188caSLydia Wang 					HDA_AMP_UNMUTE);
8971564b287SLydia Wang 		}
8981564b287SLydia Wang 		if (i == AUTO_PIN_FRONT_MIC) {
8991564b287SLydia Wang 			if (spec->codec_type == VT1708S) {
9001564b287SLydia Wang 				/* input = index 1 (AOW3) */
9011564b287SLydia Wang 				snd_hda_codec_write(
9021564b287SLydia Wang 					codec, nid, 0,
9031564b287SLydia Wang 					AC_VERB_SET_CONNECT_SEL, 1);
9041564b287SLydia Wang 				snd_hda_codec_amp_stereo(
9051564b287SLydia Wang 					codec, nid, HDA_OUTPUT,
9061564b287SLydia Wang 					0, HDA_AMP_MUTE, HDA_AMP_UNMUTE);
9071564b287SLydia Wang 			}
9081564b287SLydia Wang 		}
9091564b287SLydia Wang 	}
9101564b287SLydia Wang 	spec->smart51_enabled = *ucontrol->value.integer.value;
9111564b287SLydia Wang 	set_jack_power_state(codec);
9121564b287SLydia Wang 	return 1;
9131564b287SLydia Wang }
9141564b287SLydia Wang 
9151564b287SLydia Wang static struct snd_kcontrol_new via_smart51_mixer[] = {
9161564b287SLydia Wang 	{
9171564b287SLydia Wang 	 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
9181564b287SLydia Wang 	 .name = "Smart 5.1",
9191564b287SLydia Wang 	 .count = 1,
9201564b287SLydia Wang 	 .info = via_smart51_info,
9211564b287SLydia Wang 	 .get = via_smart51_get,
9221564b287SLydia Wang 	 .put = via_smart51_put,
9231564b287SLydia Wang 	 },
9241564b287SLydia Wang 	{}			/* end */
9251564b287SLydia Wang };
9261564b287SLydia Wang 
927c577b8a1SJoseph Chan /* capture mixer elements */
928c577b8a1SJoseph Chan static struct snd_kcontrol_new vt1708_capture_mixer[] = {
929c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_INPUT),
930c577b8a1SJoseph Chan 	HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_INPUT),
931c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x27, 0x0, HDA_INPUT),
932c577b8a1SJoseph Chan 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x27, 0x0, HDA_INPUT),
933c577b8a1SJoseph Chan 	{
934c577b8a1SJoseph Chan 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
935c577b8a1SJoseph Chan 		/* The multiple "Capture Source" controls confuse alsamixer
936c577b8a1SJoseph Chan 		 * So call somewhat different..
937c577b8a1SJoseph Chan 		 */
938c577b8a1SJoseph Chan 		/* .name = "Capture Source", */
939c577b8a1SJoseph Chan 		.name = "Input Source",
940c577b8a1SJoseph Chan 		.count = 1,
941c577b8a1SJoseph Chan 		.info = via_mux_enum_info,
942c577b8a1SJoseph Chan 		.get = via_mux_enum_get,
943c577b8a1SJoseph Chan 		.put = via_mux_enum_put,
944c577b8a1SJoseph Chan 	},
945c577b8a1SJoseph Chan 	{ } /* end */
946c577b8a1SJoseph Chan };
947f5271101SLydia Wang 
948f5271101SLydia Wang /* check AA path's mute statue */
949f5271101SLydia Wang static int is_aa_path_mute(struct hda_codec *codec)
950f5271101SLydia Wang {
951f5271101SLydia Wang 	int mute = 1;
952f5271101SLydia Wang 	hda_nid_t  nid_mixer;
953f5271101SLydia Wang 	int start_idx;
954f5271101SLydia Wang 	int end_idx;
955f5271101SLydia Wang 	int i;
956f5271101SLydia Wang 	struct via_spec *spec = codec->spec;
957f5271101SLydia Wang 	/* get nid of MW0 and start & end index */
958f5271101SLydia Wang 	switch (spec->codec_type) {
959f5271101SLydia Wang 	case VT1708B_8CH:
960f5271101SLydia Wang 	case VT1708B_4CH:
961f5271101SLydia Wang 	case VT1708S:
962f5271101SLydia Wang 		nid_mixer = 0x16;
963f5271101SLydia Wang 		start_idx = 2;
964f5271101SLydia Wang 		end_idx = 4;
965f5271101SLydia Wang 		break;
966f5271101SLydia Wang 	case VT1702:
967f5271101SLydia Wang 		nid_mixer = 0x1a;
968f5271101SLydia Wang 		start_idx = 1;
969f5271101SLydia Wang 		end_idx = 3;
970f5271101SLydia Wang 		break;
971*eb7188caSLydia Wang 	case VT1718S:
972*eb7188caSLydia Wang 		nid_mixer = 0x21;
973*eb7188caSLydia Wang 		start_idx = 1;
974*eb7188caSLydia Wang 		end_idx = 3;
975*eb7188caSLydia Wang 		break;
976f5271101SLydia Wang 	default:
977f5271101SLydia Wang 		return 0;
978f5271101SLydia Wang 	}
979f5271101SLydia Wang 	/* check AA path's mute status */
980f5271101SLydia Wang 	for (i = start_idx; i <= end_idx; i++) {
981f5271101SLydia Wang 		unsigned int con_list = snd_hda_codec_read(
982f5271101SLydia Wang 			codec, nid_mixer, 0, AC_VERB_GET_CONNECT_LIST, i/4*4);
983f5271101SLydia Wang 		int shift = 8 * (i % 4);
984f5271101SLydia Wang 		hda_nid_t nid_pin = (con_list & (0xff << shift)) >> shift;
985f5271101SLydia Wang 		unsigned int defconf = snd_hda_codec_get_pincfg(codec, nid_pin);
986f5271101SLydia Wang 		if (get_defcfg_connect(defconf) == AC_JACK_PORT_COMPLEX) {
987f5271101SLydia Wang 			/* check mute status while the pin is connected */
988f5271101SLydia Wang 			int mute_l = snd_hda_codec_amp_read(codec, nid_mixer, 0,
989f5271101SLydia Wang 							    HDA_INPUT, i) >> 7;
990f5271101SLydia Wang 			int mute_r = snd_hda_codec_amp_read(codec, nid_mixer, 1,
991f5271101SLydia Wang 							    HDA_INPUT, i) >> 7;
992f5271101SLydia Wang 			if (!mute_l || !mute_r) {
993f5271101SLydia Wang 				mute = 0;
994f5271101SLydia Wang 				break;
995f5271101SLydia Wang 			}
996f5271101SLydia Wang 		}
997f5271101SLydia Wang 	}
998f5271101SLydia Wang 	return mute;
999f5271101SLydia Wang }
1000f5271101SLydia Wang 
1001f5271101SLydia Wang /* enter/exit analog low-current mode */
1002f5271101SLydia Wang static void analog_low_current_mode(struct hda_codec *codec, int stream_idle)
1003f5271101SLydia Wang {
1004f5271101SLydia Wang 	struct via_spec *spec = codec->spec;
1005f5271101SLydia Wang 	static int saved_stream_idle = 1; /* saved stream idle status */
1006f5271101SLydia Wang 	int enable = is_aa_path_mute(codec);
1007f5271101SLydia Wang 	unsigned int verb = 0;
1008f5271101SLydia Wang 	unsigned int parm = 0;
1009f5271101SLydia Wang 
1010f5271101SLydia Wang 	if (stream_idle == -1)	/* stream status did not change */
1011f5271101SLydia Wang 		enable = enable && saved_stream_idle;
1012f5271101SLydia Wang 	else {
1013f5271101SLydia Wang 		enable = enable && stream_idle;
1014f5271101SLydia Wang 		saved_stream_idle = stream_idle;
1015f5271101SLydia Wang 	}
1016f5271101SLydia Wang 
1017f5271101SLydia Wang 	/* decide low current mode's verb & parameter */
1018f5271101SLydia Wang 	switch (spec->codec_type) {
1019f5271101SLydia Wang 	case VT1708B_8CH:
1020f5271101SLydia Wang 	case VT1708B_4CH:
1021f5271101SLydia Wang 		verb = 0xf70;
1022f5271101SLydia Wang 		parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
1023f5271101SLydia Wang 		break;
1024f5271101SLydia Wang 	case VT1708S:
1025*eb7188caSLydia Wang 	case VT1718S:
1026f5271101SLydia Wang 		verb = 0xf73;
1027f5271101SLydia Wang 		parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
1028f5271101SLydia Wang 		break;
1029f5271101SLydia Wang 	case VT1702:
1030f5271101SLydia Wang 		verb = 0xf73;
1031f5271101SLydia Wang 		parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
1032f5271101SLydia Wang 		break;
1033f5271101SLydia Wang 	default:
1034f5271101SLydia Wang 		return;		/* other codecs are not supported */
1035f5271101SLydia Wang 	}
1036f5271101SLydia Wang 	/* send verb */
1037f5271101SLydia Wang 	snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
1038f5271101SLydia Wang }
1039f5271101SLydia Wang 
1040c577b8a1SJoseph Chan /*
1041c577b8a1SJoseph Chan  * generic initialization of ADC, input mixers and output mixers
1042c577b8a1SJoseph Chan  */
1043c577b8a1SJoseph Chan static struct hda_verb vt1708_volume_init_verbs[] = {
1044c577b8a1SJoseph Chan 	/*
1045c577b8a1SJoseph Chan 	 * Unmute ADC0-1 and set the default input to mic-in
1046c577b8a1SJoseph Chan 	 */
1047c577b8a1SJoseph Chan 	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1048c577b8a1SJoseph Chan 	{0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1049c577b8a1SJoseph Chan 
1050c577b8a1SJoseph Chan 
1051f7278fd0SJosepch Chan 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
1052c577b8a1SJoseph Chan 	 * mixer widget
1053c577b8a1SJoseph Chan 	 */
1054c577b8a1SJoseph Chan 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
1055f7278fd0SJosepch Chan 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1056f7278fd0SJosepch Chan 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1057f7278fd0SJosepch Chan 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
1058f7278fd0SJosepch Chan 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
1059f7278fd0SJosepch Chan 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
1060c577b8a1SJoseph Chan 
1061c577b8a1SJoseph Chan 	/*
1062c577b8a1SJoseph Chan 	 * Set up output mixers (0x19 - 0x1b)
1063c577b8a1SJoseph Chan 	 */
1064c577b8a1SJoseph Chan 	/* set vol=0 to output mixers */
1065c577b8a1SJoseph Chan 	{0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1066c577b8a1SJoseph Chan 	{0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1067c577b8a1SJoseph Chan 	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1068c577b8a1SJoseph Chan 
1069c577b8a1SJoseph Chan 	/* Setup default input to PW4 */
1070c577b8a1SJoseph Chan 	{0x20, AC_VERB_SET_CONNECT_SEL, 0x1},
1071c577b8a1SJoseph Chan 	/* PW9 Output enable */
1072c577b8a1SJoseph Chan 	{0x25, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
1073f7278fd0SJosepch Chan 	{ }
1074c577b8a1SJoseph Chan };
1075c577b8a1SJoseph Chan 
1076c577b8a1SJoseph Chan static int via_playback_pcm_open(struct hda_pcm_stream *hinfo,
1077c577b8a1SJoseph Chan 				 struct hda_codec *codec,
1078c577b8a1SJoseph Chan 				 struct snd_pcm_substream *substream)
1079c577b8a1SJoseph Chan {
1080c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
108117314379SLydia Wang 	int idle = substream->pstr->substream_opened == 1
108217314379SLydia Wang 		&& substream->ref_count == 0;
108317314379SLydia Wang 	analog_low_current_mode(codec, idle);
10849a08160bSTakashi Iwai 	return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
10859a08160bSTakashi Iwai 					     hinfo);
1086c577b8a1SJoseph Chan }
1087c577b8a1SJoseph Chan 
10880aa62aefSHarald Welte static void playback_multi_pcm_prep_0(struct hda_codec *codec,
10890aa62aefSHarald Welte 				      unsigned int stream_tag,
10900aa62aefSHarald Welte 				      unsigned int format,
10910aa62aefSHarald Welte 				      struct snd_pcm_substream *substream)
10920aa62aefSHarald Welte {
10930aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
10940aa62aefSHarald Welte 	struct hda_multi_out *mout = &spec->multiout;
10950aa62aefSHarald Welte 	hda_nid_t *nids = mout->dac_nids;
10960aa62aefSHarald Welte 	int chs = substream->runtime->channels;
10970aa62aefSHarald Welte 	int i;
10980aa62aefSHarald Welte 
10990aa62aefSHarald Welte 	mutex_lock(&codec->spdif_mutex);
11000aa62aefSHarald Welte 	if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
11010aa62aefSHarald Welte 		if (chs == 2 &&
11020aa62aefSHarald Welte 		    snd_hda_is_supported_format(codec, mout->dig_out_nid,
11030aa62aefSHarald Welte 						format) &&
11040aa62aefSHarald Welte 		    !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
11050aa62aefSHarald Welte 			mout->dig_out_used = HDA_DIG_ANALOG_DUP;
11060aa62aefSHarald Welte 			/* turn off SPDIF once; otherwise the IEC958 bits won't
11070aa62aefSHarald Welte 			 * be updated */
11080aa62aefSHarald Welte 			if (codec->spdif_ctls & AC_DIG1_ENABLE)
11090aa62aefSHarald Welte 				snd_hda_codec_write(codec, mout->dig_out_nid, 0,
11100aa62aefSHarald Welte 						    AC_VERB_SET_DIGI_CONVERT_1,
11110aa62aefSHarald Welte 						    codec->spdif_ctls &
11120aa62aefSHarald Welte 							~AC_DIG1_ENABLE & 0xff);
11130aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
11140aa62aefSHarald Welte 						   stream_tag, 0, format);
11150aa62aefSHarald Welte 			/* turn on again (if needed) */
11160aa62aefSHarald Welte 			if (codec->spdif_ctls & AC_DIG1_ENABLE)
11170aa62aefSHarald Welte 				snd_hda_codec_write(codec, mout->dig_out_nid, 0,
11180aa62aefSHarald Welte 						    AC_VERB_SET_DIGI_CONVERT_1,
11190aa62aefSHarald Welte 						    codec->spdif_ctls & 0xff);
11200aa62aefSHarald Welte 		} else {
11210aa62aefSHarald Welte 			mout->dig_out_used = 0;
11220aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
11230aa62aefSHarald Welte 						   0, 0, 0);
11240aa62aefSHarald Welte 		}
11250aa62aefSHarald Welte 	}
11260aa62aefSHarald Welte 	mutex_unlock(&codec->spdif_mutex);
11270aa62aefSHarald Welte 
11280aa62aefSHarald Welte 	/* front */
11290aa62aefSHarald Welte 	snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
11300aa62aefSHarald Welte 				   0, format);
11310aa62aefSHarald Welte 
1132*eb7188caSLydia Wang 	if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]
1133*eb7188caSLydia Wang 	    && !spec->hp_independent_mode)
11340aa62aefSHarald Welte 		/* headphone out will just decode front left/right (stereo) */
11350aa62aefSHarald Welte 		snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
11360aa62aefSHarald Welte 					   0, format);
11370aa62aefSHarald Welte 
11380aa62aefSHarald Welte 	/* extra outputs copied from front */
11390aa62aefSHarald Welte 	for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
11400aa62aefSHarald Welte 		if (mout->extra_out_nid[i])
11410aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec,
11420aa62aefSHarald Welte 						   mout->extra_out_nid[i],
11430aa62aefSHarald Welte 						   stream_tag, 0, format);
11440aa62aefSHarald Welte 
11450aa62aefSHarald Welte 	/* surrounds */
11460aa62aefSHarald Welte 	for (i = 1; i < mout->num_dacs; i++) {
11470aa62aefSHarald Welte 		if (chs >= (i + 1) * 2) /* independent out */
11480aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
11490aa62aefSHarald Welte 						   i * 2, format);
11500aa62aefSHarald Welte 		else /* copy front */
11510aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
11520aa62aefSHarald Welte 						   0, format);
11530aa62aefSHarald Welte 	}
11540aa62aefSHarald Welte }
11550aa62aefSHarald Welte 
11560aa62aefSHarald Welte static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
11570aa62aefSHarald Welte 					  struct hda_codec *codec,
11580aa62aefSHarald Welte 					  unsigned int stream_tag,
11590aa62aefSHarald Welte 					  unsigned int format,
11600aa62aefSHarald Welte 					  struct snd_pcm_substream *substream)
11610aa62aefSHarald Welte {
11620aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
11630aa62aefSHarald Welte 	struct hda_multi_out *mout = &spec->multiout;
11640aa62aefSHarald Welte 	hda_nid_t *nids = mout->dac_nids;
11650aa62aefSHarald Welte 
11660aa62aefSHarald Welte 	if (substream->number == 0)
11670aa62aefSHarald Welte 		playback_multi_pcm_prep_0(codec, stream_tag, format,
11680aa62aefSHarald Welte 					  substream);
11690aa62aefSHarald Welte 	else {
11700aa62aefSHarald Welte 		if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
11710aa62aefSHarald Welte 		    spec->hp_independent_mode)
11720aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, mout->hp_nid,
11730aa62aefSHarald Welte 						   stream_tag, 0, format);
11740aa62aefSHarald Welte 	}
11751f2e99feSLydia Wang 	vt1708_start_hp_work(spec);
11760aa62aefSHarald Welte 	return 0;
11770aa62aefSHarald Welte }
11780aa62aefSHarald Welte 
11790aa62aefSHarald Welte static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
11800aa62aefSHarald Welte 				    struct hda_codec *codec,
11810aa62aefSHarald Welte 				    struct snd_pcm_substream *substream)
11820aa62aefSHarald Welte {
11830aa62aefSHarald Welte 	struct via_spec *spec = codec->spec;
11840aa62aefSHarald Welte 	struct hda_multi_out *mout = &spec->multiout;
11850aa62aefSHarald Welte 	hda_nid_t *nids = mout->dac_nids;
11860aa62aefSHarald Welte 	int i;
11870aa62aefSHarald Welte 
11880aa62aefSHarald Welte 	if (substream->number == 0) {
11890aa62aefSHarald Welte 		for (i = 0; i < mout->num_dacs; i++)
11900aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
11910aa62aefSHarald Welte 
11920aa62aefSHarald Welte 		if (mout->hp_nid && !spec->hp_independent_mode)
11930aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, mout->hp_nid,
11940aa62aefSHarald Welte 						   0, 0, 0);
11950aa62aefSHarald Welte 
11960aa62aefSHarald Welte 		for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
11970aa62aefSHarald Welte 			if (mout->extra_out_nid[i])
11980aa62aefSHarald Welte 				snd_hda_codec_setup_stream(codec,
11990aa62aefSHarald Welte 							mout->extra_out_nid[i],
12000aa62aefSHarald Welte 							0, 0, 0);
12010aa62aefSHarald Welte 		mutex_lock(&codec->spdif_mutex);
12020aa62aefSHarald Welte 		if (mout->dig_out_nid &&
12030aa62aefSHarald Welte 		    mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
12040aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
12050aa62aefSHarald Welte 						   0, 0, 0);
12060aa62aefSHarald Welte 			mout->dig_out_used = 0;
12070aa62aefSHarald Welte 		}
12080aa62aefSHarald Welte 		mutex_unlock(&codec->spdif_mutex);
12090aa62aefSHarald Welte 	} else {
12100aa62aefSHarald Welte 		if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
12110aa62aefSHarald Welte 		    spec->hp_independent_mode)
12120aa62aefSHarald Welte 			snd_hda_codec_setup_stream(codec, mout->hp_nid,
12130aa62aefSHarald Welte 						   0, 0, 0);
12140aa62aefSHarald Welte 	}
12151f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
12160aa62aefSHarald Welte 	return 0;
12170aa62aefSHarald Welte }
12180aa62aefSHarald Welte 
1219c577b8a1SJoseph Chan /*
1220c577b8a1SJoseph Chan  * Digital out
1221c577b8a1SJoseph Chan  */
1222c577b8a1SJoseph Chan static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1223c577b8a1SJoseph Chan 				     struct hda_codec *codec,
1224c577b8a1SJoseph Chan 				     struct snd_pcm_substream *substream)
1225c577b8a1SJoseph Chan {
1226c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1227c577b8a1SJoseph Chan 	return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1228c577b8a1SJoseph Chan }
1229c577b8a1SJoseph Chan 
1230c577b8a1SJoseph Chan static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1231c577b8a1SJoseph Chan 				      struct hda_codec *codec,
1232c577b8a1SJoseph Chan 				      struct snd_pcm_substream *substream)
1233c577b8a1SJoseph Chan {
1234c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1235c577b8a1SJoseph Chan 	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1236c577b8a1SJoseph Chan }
1237c577b8a1SJoseph Chan 
12385691ec7fSHarald Welte static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
123998aa34c0SHarald Welte 					struct hda_codec *codec,
124098aa34c0SHarald Welte 					unsigned int stream_tag,
124198aa34c0SHarald Welte 					unsigned int format,
124298aa34c0SHarald Welte 					struct snd_pcm_substream *substream)
124398aa34c0SHarald Welte {
124498aa34c0SHarald Welte 	struct via_spec *spec = codec->spec;
12459da29271STakashi Iwai 	return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
12469da29271STakashi Iwai 					     stream_tag, format, substream);
12479da29271STakashi Iwai }
12485691ec7fSHarald Welte 
12499da29271STakashi Iwai static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
12509da29271STakashi Iwai 					struct hda_codec *codec,
12519da29271STakashi Iwai 					struct snd_pcm_substream *substream)
12529da29271STakashi Iwai {
12539da29271STakashi Iwai 	struct via_spec *spec = codec->spec;
12549da29271STakashi Iwai 	snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
125598aa34c0SHarald Welte 	return 0;
125698aa34c0SHarald Welte }
125798aa34c0SHarald Welte 
1258c577b8a1SJoseph Chan /*
1259c577b8a1SJoseph Chan  * Analog capture
1260c577b8a1SJoseph Chan  */
1261c577b8a1SJoseph Chan static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1262c577b8a1SJoseph Chan 				   struct hda_codec *codec,
1263c577b8a1SJoseph Chan 				   unsigned int stream_tag,
1264c577b8a1SJoseph Chan 				   unsigned int format,
1265c577b8a1SJoseph Chan 				   struct snd_pcm_substream *substream)
1266c577b8a1SJoseph Chan {
1267c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1268c577b8a1SJoseph Chan 
1269c577b8a1SJoseph Chan 	snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1270c577b8a1SJoseph Chan 				   stream_tag, 0, format);
1271c577b8a1SJoseph Chan 	return 0;
1272c577b8a1SJoseph Chan }
1273c577b8a1SJoseph Chan 
1274c577b8a1SJoseph Chan static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1275c577b8a1SJoseph Chan 				   struct hda_codec *codec,
1276c577b8a1SJoseph Chan 				   struct snd_pcm_substream *substream)
1277c577b8a1SJoseph Chan {
1278c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1279888afa15STakashi Iwai 	snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
1280c577b8a1SJoseph Chan 	return 0;
1281c577b8a1SJoseph Chan }
1282c577b8a1SJoseph Chan 
1283c577b8a1SJoseph Chan static struct hda_pcm_stream vt1708_pcm_analog_playback = {
12840aa62aefSHarald Welte 	.substreams = 2,
1285c577b8a1SJoseph Chan 	.channels_min = 2,
1286c577b8a1SJoseph Chan 	.channels_max = 8,
1287c577b8a1SJoseph Chan 	.nid = 0x10, /* NID to query formats and rates */
1288c577b8a1SJoseph Chan 	.ops = {
1289c577b8a1SJoseph Chan 		.open = via_playback_pcm_open,
12900aa62aefSHarald Welte 		.prepare = via_playback_multi_pcm_prepare,
12910aa62aefSHarald Welte 		.cleanup = via_playback_multi_pcm_cleanup
1292c577b8a1SJoseph Chan 	},
1293c577b8a1SJoseph Chan };
1294c577b8a1SJoseph Chan 
1295bc9b5623STakashi Iwai static struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
1296c873cc25SLydia Wang 	.substreams = 2,
1297bc9b5623STakashi Iwai 	.channels_min = 2,
1298bc9b5623STakashi Iwai 	.channels_max = 8,
1299bc9b5623STakashi Iwai 	.nid = 0x10, /* NID to query formats and rates */
1300bc9b5623STakashi Iwai 	/* We got noisy outputs on the right channel on VT1708 when
1301bc9b5623STakashi Iwai 	 * 24bit samples are used.  Until any workaround is found,
1302bc9b5623STakashi Iwai 	 * disable the 24bit format, so far.
1303bc9b5623STakashi Iwai 	 */
1304bc9b5623STakashi Iwai 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
1305bc9b5623STakashi Iwai 	.ops = {
1306bc9b5623STakashi Iwai 		.open = via_playback_pcm_open,
1307c873cc25SLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
1308c873cc25SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup
1309bc9b5623STakashi Iwai 	},
1310bc9b5623STakashi Iwai };
1311bc9b5623STakashi Iwai 
1312c577b8a1SJoseph Chan static struct hda_pcm_stream vt1708_pcm_analog_capture = {
1313c577b8a1SJoseph Chan 	.substreams = 2,
1314c577b8a1SJoseph Chan 	.channels_min = 2,
1315c577b8a1SJoseph Chan 	.channels_max = 2,
1316c577b8a1SJoseph Chan 	.nid = 0x15, /* NID to query formats and rates */
1317c577b8a1SJoseph Chan 	.ops = {
1318c577b8a1SJoseph Chan 		.prepare = via_capture_pcm_prepare,
1319c577b8a1SJoseph Chan 		.cleanup = via_capture_pcm_cleanup
1320c577b8a1SJoseph Chan 	},
1321c577b8a1SJoseph Chan };
1322c577b8a1SJoseph Chan 
1323c577b8a1SJoseph Chan static struct hda_pcm_stream vt1708_pcm_digital_playback = {
1324c577b8a1SJoseph Chan 	.substreams = 1,
1325c577b8a1SJoseph Chan 	.channels_min = 2,
1326c577b8a1SJoseph Chan 	.channels_max = 2,
1327c577b8a1SJoseph Chan 	/* NID is set in via_build_pcms */
1328c577b8a1SJoseph Chan 	.ops = {
1329c577b8a1SJoseph Chan 		.open = via_dig_playback_pcm_open,
13306b97eb45STakashi Iwai 		.close = via_dig_playback_pcm_close,
13319da29271STakashi Iwai 		.prepare = via_dig_playback_pcm_prepare,
13329da29271STakashi Iwai 		.cleanup = via_dig_playback_pcm_cleanup
1333c577b8a1SJoseph Chan 	},
1334c577b8a1SJoseph Chan };
1335c577b8a1SJoseph Chan 
1336c577b8a1SJoseph Chan static struct hda_pcm_stream vt1708_pcm_digital_capture = {
1337c577b8a1SJoseph Chan 	.substreams = 1,
1338c577b8a1SJoseph Chan 	.channels_min = 2,
1339c577b8a1SJoseph Chan 	.channels_max = 2,
1340c577b8a1SJoseph Chan };
1341c577b8a1SJoseph Chan 
1342c577b8a1SJoseph Chan static int via_build_controls(struct hda_codec *codec)
1343c577b8a1SJoseph Chan {
1344c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1345c577b8a1SJoseph Chan 	int err;
1346c577b8a1SJoseph Chan 	int i;
1347c577b8a1SJoseph Chan 
1348c577b8a1SJoseph Chan 	for (i = 0; i < spec->num_mixers; i++) {
1349c577b8a1SJoseph Chan 		err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1350c577b8a1SJoseph Chan 		if (err < 0)
1351c577b8a1SJoseph Chan 			return err;
1352c577b8a1SJoseph Chan 	}
1353c577b8a1SJoseph Chan 
1354c577b8a1SJoseph Chan 	if (spec->multiout.dig_out_nid) {
1355c577b8a1SJoseph Chan 		err = snd_hda_create_spdif_out_ctls(codec,
1356c577b8a1SJoseph Chan 						    spec->multiout.dig_out_nid);
1357c577b8a1SJoseph Chan 		if (err < 0)
1358c577b8a1SJoseph Chan 			return err;
13599a08160bSTakashi Iwai 		err = snd_hda_create_spdif_share_sw(codec,
13609a08160bSTakashi Iwai 						    &spec->multiout);
13619a08160bSTakashi Iwai 		if (err < 0)
13629a08160bSTakashi Iwai 			return err;
13639a08160bSTakashi Iwai 		spec->multiout.share_spdif = 1;
1364c577b8a1SJoseph Chan 	}
1365c577b8a1SJoseph Chan 	if (spec->dig_in_nid) {
1366c577b8a1SJoseph Chan 		err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1367c577b8a1SJoseph Chan 		if (err < 0)
1368c577b8a1SJoseph Chan 			return err;
1369c577b8a1SJoseph Chan 	}
137017314379SLydia Wang 
137117314379SLydia Wang 	/* init power states */
137217314379SLydia Wang 	set_jack_power_state(codec);
137317314379SLydia Wang 	analog_low_current_mode(codec, 1);
137417314379SLydia Wang 
1375603c4019STakashi Iwai 	via_free_kctls(codec); /* no longer needed */
1376c577b8a1SJoseph Chan 	return 0;
1377c577b8a1SJoseph Chan }
1378c577b8a1SJoseph Chan 
1379c577b8a1SJoseph Chan static int via_build_pcms(struct hda_codec *codec)
1380c577b8a1SJoseph Chan {
1381c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1382c577b8a1SJoseph Chan 	struct hda_pcm *info = spec->pcm_rec;
1383c577b8a1SJoseph Chan 
1384c577b8a1SJoseph Chan 	codec->num_pcms = 1;
1385c577b8a1SJoseph Chan 	codec->pcm_info = info;
1386c577b8a1SJoseph Chan 
1387c577b8a1SJoseph Chan 	info->name = spec->stream_name_analog;
1388c577b8a1SJoseph Chan 	info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *(spec->stream_analog_playback);
1389c577b8a1SJoseph Chan 	info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
1390c577b8a1SJoseph Chan 	info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_analog_capture);
1391c577b8a1SJoseph Chan 	info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
1392c577b8a1SJoseph Chan 
1393c577b8a1SJoseph Chan 	info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1394c577b8a1SJoseph Chan 		spec->multiout.max_channels;
1395c577b8a1SJoseph Chan 
1396c577b8a1SJoseph Chan 	if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1397c577b8a1SJoseph Chan 		codec->num_pcms++;
1398c577b8a1SJoseph Chan 		info++;
1399c577b8a1SJoseph Chan 		info->name = spec->stream_name_digital;
14007ba72ba1STakashi Iwai 		info->pcm_type = HDA_PCM_TYPE_SPDIF;
1401c577b8a1SJoseph Chan 		if (spec->multiout.dig_out_nid) {
1402c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1403c577b8a1SJoseph Chan 				*(spec->stream_digital_playback);
1404c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1405c577b8a1SJoseph Chan 				spec->multiout.dig_out_nid;
1406c577b8a1SJoseph Chan 		}
1407c577b8a1SJoseph Chan 		if (spec->dig_in_nid) {
1408c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1409c577b8a1SJoseph Chan 				*(spec->stream_digital_capture);
1410c577b8a1SJoseph Chan 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1411c577b8a1SJoseph Chan 				spec->dig_in_nid;
1412c577b8a1SJoseph Chan 		}
1413c577b8a1SJoseph Chan 	}
1414c577b8a1SJoseph Chan 
1415c577b8a1SJoseph Chan 	return 0;
1416c577b8a1SJoseph Chan }
1417c577b8a1SJoseph Chan 
1418c577b8a1SJoseph Chan static void via_free(struct hda_codec *codec)
1419c577b8a1SJoseph Chan {
1420c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1421c577b8a1SJoseph Chan 
1422c577b8a1SJoseph Chan 	if (!spec)
1423c577b8a1SJoseph Chan 		return;
1424c577b8a1SJoseph Chan 
1425603c4019STakashi Iwai 	via_free_kctls(codec);
14261f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
1427c577b8a1SJoseph Chan 	kfree(codec->spec);
1428c577b8a1SJoseph Chan }
1429c577b8a1SJoseph Chan 
143069e52a80SHarald Welte /* mute internal speaker if HP is plugged */
143169e52a80SHarald Welte static void via_hp_automute(struct hda_codec *codec)
143269e52a80SHarald Welte {
1433dcf34c8cSLydia Wang 	unsigned int present = 0;
143469e52a80SHarald Welte 	struct via_spec *spec = codec->spec;
143569e52a80SHarald Welte 
143669e52a80SHarald Welte 	present = snd_hda_codec_read(codec, spec->autocfg.hp_pins[0], 0,
143769e52a80SHarald Welte 				     AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1438dcf34c8cSLydia Wang 
1439dcf34c8cSLydia Wang 	if (!spec->hp_independent_mode) {
1440dcf34c8cSLydia Wang 		struct snd_ctl_elem_id id;
1441dcf34c8cSLydia Wang 		/* auto mute */
1442dcf34c8cSLydia Wang 		snd_hda_codec_amp_stereo(
1443dcf34c8cSLydia Wang 			codec, spec->autocfg.line_out_pins[0], HDA_OUTPUT, 0,
1444dcf34c8cSLydia Wang 			HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
1445dcf34c8cSLydia Wang 		/* notify change */
1446dcf34c8cSLydia Wang 		memset(&id, 0, sizeof(id));
1447dcf34c8cSLydia Wang 		id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1448dcf34c8cSLydia Wang 		strcpy(id.name, "Front Playback Switch");
1449dcf34c8cSLydia Wang 		snd_ctl_notify(codec->bus->card, SNDRV_CTL_EVENT_MASK_VALUE,
1450dcf34c8cSLydia Wang 			       &id);
1451dcf34c8cSLydia Wang 	}
145269e52a80SHarald Welte }
145369e52a80SHarald Welte 
145469e52a80SHarald Welte static void via_gpio_control(struct hda_codec *codec)
145569e52a80SHarald Welte {
145669e52a80SHarald Welte 	unsigned int gpio_data;
145769e52a80SHarald Welte 	unsigned int vol_counter;
145869e52a80SHarald Welte 	unsigned int vol;
145969e52a80SHarald Welte 	unsigned int master_vol;
146069e52a80SHarald Welte 
146169e52a80SHarald Welte 	struct via_spec *spec = codec->spec;
146269e52a80SHarald Welte 
146369e52a80SHarald Welte 	gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
146469e52a80SHarald Welte 				       AC_VERB_GET_GPIO_DATA, 0) & 0x03;
146569e52a80SHarald Welte 
146669e52a80SHarald Welte 	vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
146769e52a80SHarald Welte 					  0xF84, 0) & 0x3F0000) >> 16;
146869e52a80SHarald Welte 
146969e52a80SHarald Welte 	vol = vol_counter & 0x1F;
147069e52a80SHarald Welte 	master_vol = snd_hda_codec_read(codec, 0x1A, 0,
147169e52a80SHarald Welte 					AC_VERB_GET_AMP_GAIN_MUTE,
147269e52a80SHarald Welte 					AC_AMP_GET_INPUT);
147369e52a80SHarald Welte 
147469e52a80SHarald Welte 	if (gpio_data == 0x02) {
147569e52a80SHarald Welte 		/* unmute line out */
147669e52a80SHarald Welte 		snd_hda_codec_amp_stereo(codec, spec->autocfg.line_out_pins[0],
147769e52a80SHarald Welte 					 HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
147869e52a80SHarald Welte 
147969e52a80SHarald Welte 		if (vol_counter & 0x20) {
148069e52a80SHarald Welte 			/* decrease volume */
148169e52a80SHarald Welte 			if (vol > master_vol)
148269e52a80SHarald Welte 				vol = master_vol;
148369e52a80SHarald Welte 			snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
148469e52a80SHarald Welte 						 0, HDA_AMP_VOLMASK,
148569e52a80SHarald Welte 						 master_vol-vol);
148669e52a80SHarald Welte 		} else {
148769e52a80SHarald Welte 			/* increase volume */
148869e52a80SHarald Welte 			snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
148969e52a80SHarald Welte 					 HDA_AMP_VOLMASK,
149069e52a80SHarald Welte 					 ((master_vol+vol) > 0x2A) ? 0x2A :
149169e52a80SHarald Welte 					  (master_vol+vol));
149269e52a80SHarald Welte 		}
149369e52a80SHarald Welte 	} else if (!(gpio_data & 0x02)) {
149469e52a80SHarald Welte 		/* mute line out */
149569e52a80SHarald Welte 		snd_hda_codec_amp_stereo(codec,
149669e52a80SHarald Welte 					 spec->autocfg.line_out_pins[0],
149769e52a80SHarald Welte 					 HDA_OUTPUT, 0, HDA_AMP_MUTE,
149869e52a80SHarald Welte 					 HDA_AMP_MUTE);
149969e52a80SHarald Welte 	}
150069e52a80SHarald Welte }
150169e52a80SHarald Welte 
150269e52a80SHarald Welte /* unsolicited event for jack sensing */
150369e52a80SHarald Welte static void via_unsol_event(struct hda_codec *codec,
150469e52a80SHarald Welte 				  unsigned int res)
150569e52a80SHarald Welte {
150669e52a80SHarald Welte 	res >>= 26;
1507a34df19aSLydia Wang 	if (res & VIA_HP_EVENT)
150869e52a80SHarald Welte 		via_hp_automute(codec);
1509a34df19aSLydia Wang 	if (res & VIA_GPIO_EVENT)
151069e52a80SHarald Welte 		via_gpio_control(codec);
1511a34df19aSLydia Wang 	if (res & VIA_JACK_EVENT)
1512a34df19aSLydia Wang 		set_jack_power_state(codec);
151369e52a80SHarald Welte }
151469e52a80SHarald Welte 
1515c577b8a1SJoseph Chan static int via_init(struct hda_codec *codec)
1516c577b8a1SJoseph Chan {
1517c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
151869e52a80SHarald Welte 	int i;
151969e52a80SHarald Welte 	for (i = 0; i < spec->num_iverbs; i++)
152069e52a80SHarald Welte 		snd_hda_sequence_write(codec, spec->init_verbs[i]);
152169e52a80SHarald Welte 
1522518bf3baSLydia Wang 	spec->codec_type = get_codec_type(codec);
1523518bf3baSLydia Wang 	if (spec->codec_type == VT1708BCE)
1524518bf3baSLydia Wang 		spec->codec_type = VT1708S; /* VT1708BCE & VT1708S are almost
1525518bf3baSLydia Wang 					       same */
1526f7278fd0SJosepch Chan 	/* Lydia Add for EAPD enable */
1527f7278fd0SJosepch Chan 	if (!spec->dig_in_nid) { /* No Digital In connection */
152855d1d6c1STakashi Iwai 		if (spec->dig_in_pin) {
152955d1d6c1STakashi Iwai 			snd_hda_codec_write(codec, spec->dig_in_pin, 0,
1530f7278fd0SJosepch Chan 					    AC_VERB_SET_PIN_WIDGET_CONTROL,
153112b74c80STakashi Iwai 					    PIN_OUT);
153255d1d6c1STakashi Iwai 			snd_hda_codec_write(codec, spec->dig_in_pin, 0,
1533f7278fd0SJosepch Chan 					    AC_VERB_SET_EAPD_BTLENABLE, 0x02);
1534f7278fd0SJosepch Chan 		}
153512b74c80STakashi Iwai 	} else /* enable SPDIF-input pin */
153612b74c80STakashi Iwai 		snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
153712b74c80STakashi Iwai 				    AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
1538f7278fd0SJosepch Chan 
15399da29271STakashi Iwai 	/* assign slave outs */
15409da29271STakashi Iwai 	if (spec->slave_dig_outs[0])
15419da29271STakashi Iwai 		codec->slave_dig_outs = spec->slave_dig_outs;
15425691ec7fSHarald Welte 
1543c577b8a1SJoseph Chan  	return 0;
1544c577b8a1SJoseph Chan }
1545c577b8a1SJoseph Chan 
15461f2e99feSLydia Wang #ifdef SND_HDA_NEEDS_RESUME
15471f2e99feSLydia Wang static int via_suspend(struct hda_codec *codec, pm_message_t state)
15481f2e99feSLydia Wang {
15491f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
15501f2e99feSLydia Wang 	vt1708_stop_hp_work(spec);
15511f2e99feSLydia Wang 	return 0;
15521f2e99feSLydia Wang }
15531f2e99feSLydia Wang #endif
15541f2e99feSLydia Wang 
1555cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
1556cb53c626STakashi Iwai static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1557cb53c626STakashi Iwai {
1558cb53c626STakashi Iwai 	struct via_spec *spec = codec->spec;
1559cb53c626STakashi Iwai 	return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1560cb53c626STakashi Iwai }
1561cb53c626STakashi Iwai #endif
1562cb53c626STakashi Iwai 
1563c577b8a1SJoseph Chan /*
1564c577b8a1SJoseph Chan  */
1565c577b8a1SJoseph Chan static struct hda_codec_ops via_patch_ops = {
1566c577b8a1SJoseph Chan 	.build_controls = via_build_controls,
1567c577b8a1SJoseph Chan 	.build_pcms = via_build_pcms,
1568c577b8a1SJoseph Chan 	.init = via_init,
1569c577b8a1SJoseph Chan 	.free = via_free,
15701f2e99feSLydia Wang #ifdef SND_HDA_NEEDS_RESUME
15711f2e99feSLydia Wang 	.suspend = via_suspend,
15721f2e99feSLydia Wang #endif
1573cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
1574cb53c626STakashi Iwai 	.check_power_status = via_check_power_status,
1575cb53c626STakashi Iwai #endif
1576c577b8a1SJoseph Chan };
1577c577b8a1SJoseph Chan 
1578c577b8a1SJoseph Chan /* fill in the dac_nids table from the parsed pin configuration */
1579c577b8a1SJoseph Chan static int vt1708_auto_fill_dac_nids(struct via_spec *spec,
1580c577b8a1SJoseph Chan 				     const struct auto_pin_cfg *cfg)
1581c577b8a1SJoseph Chan {
1582c577b8a1SJoseph Chan 	int i;
1583c577b8a1SJoseph Chan 	hda_nid_t nid;
1584c577b8a1SJoseph Chan 
1585c577b8a1SJoseph Chan 	spec->multiout.num_dacs = cfg->line_outs;
1586c577b8a1SJoseph Chan 
1587c577b8a1SJoseph Chan 	spec->multiout.dac_nids = spec->private_dac_nids;
1588c577b8a1SJoseph Chan 
1589c577b8a1SJoseph Chan 	for(i = 0; i < 4; i++) {
1590c577b8a1SJoseph Chan 		nid = cfg->line_out_pins[i];
1591c577b8a1SJoseph Chan 		if (nid) {
1592c577b8a1SJoseph Chan 			/* config dac list */
1593c577b8a1SJoseph Chan 			switch (i) {
1594c577b8a1SJoseph Chan 			case AUTO_SEQ_FRONT:
1595c577b8a1SJoseph Chan 				spec->multiout.dac_nids[i] = 0x10;
1596c577b8a1SJoseph Chan 				break;
1597c577b8a1SJoseph Chan 			case AUTO_SEQ_CENLFE:
1598c577b8a1SJoseph Chan 				spec->multiout.dac_nids[i] = 0x12;
1599c577b8a1SJoseph Chan 				break;
1600c577b8a1SJoseph Chan 			case AUTO_SEQ_SURROUND:
1601fb4cb772SHarald Welte 				spec->multiout.dac_nids[i] = 0x11;
1602c577b8a1SJoseph Chan 				break;
1603c577b8a1SJoseph Chan 			case AUTO_SEQ_SIDE:
1604fb4cb772SHarald Welte 				spec->multiout.dac_nids[i] = 0x13;
1605c577b8a1SJoseph Chan 				break;
1606c577b8a1SJoseph Chan 			}
1607c577b8a1SJoseph Chan 		}
1608c577b8a1SJoseph Chan 	}
1609c577b8a1SJoseph Chan 
1610c577b8a1SJoseph Chan 	return 0;
1611c577b8a1SJoseph Chan }
1612c577b8a1SJoseph Chan 
1613c577b8a1SJoseph Chan /* add playback controls from the parsed DAC table */
1614c577b8a1SJoseph Chan static int vt1708_auto_create_multi_out_ctls(struct via_spec *spec,
1615c577b8a1SJoseph Chan 					     const struct auto_pin_cfg *cfg)
1616c577b8a1SJoseph Chan {
1617c577b8a1SJoseph Chan 	char name[32];
1618c577b8a1SJoseph Chan 	static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
16199645c203SLydia Wang 	hda_nid_t nid, nid_vol, nid_vols[] = {0x17, 0x19, 0x1a, 0x1b};
1620c577b8a1SJoseph Chan 	int i, err;
1621c577b8a1SJoseph Chan 
1622c577b8a1SJoseph Chan 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
1623c577b8a1SJoseph Chan 		nid = cfg->line_out_pins[i];
1624c577b8a1SJoseph Chan 
1625c577b8a1SJoseph Chan 		if (!nid)
1626c577b8a1SJoseph Chan 			continue;
1627c577b8a1SJoseph Chan 
16289645c203SLydia Wang 		nid_vol = nid_vols[i];
1629c577b8a1SJoseph Chan 
1630c577b8a1SJoseph Chan 		if (i == AUTO_SEQ_CENLFE) {
1631c577b8a1SJoseph Chan 			/* Center/LFE */
1632c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1633c577b8a1SJoseph Chan 					"Center Playback Volume",
1634f7278fd0SJosepch Chan 					HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
1635f7278fd0SJosepch Chan 							    HDA_OUTPUT));
1636c577b8a1SJoseph Chan 			if (err < 0)
1637c577b8a1SJoseph Chan 				return err;
1638c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1639c577b8a1SJoseph Chan 					      "LFE Playback Volume",
1640f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
1641f7278fd0SJosepch Chan 								  HDA_OUTPUT));
1642c577b8a1SJoseph Chan 			if (err < 0)
1643c577b8a1SJoseph Chan 				return err;
1644c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1645c577b8a1SJoseph Chan 					      "Center Playback Switch",
1646f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
1647f7278fd0SJosepch Chan 								  HDA_OUTPUT));
1648c577b8a1SJoseph Chan 			if (err < 0)
1649c577b8a1SJoseph Chan 				return err;
1650c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1651c577b8a1SJoseph Chan 					      "LFE Playback Switch",
1652f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
1653f7278fd0SJosepch Chan 								  HDA_OUTPUT));
1654c577b8a1SJoseph Chan 			if (err < 0)
1655c577b8a1SJoseph Chan 				return err;
1656c577b8a1SJoseph Chan 		} else if (i == AUTO_SEQ_FRONT){
1657c577b8a1SJoseph Chan 			/* add control to mixer index 0 */
1658c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1659c577b8a1SJoseph Chan 					      "Master Front Playback Volume",
16609645c203SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1661f7278fd0SJosepch Chan 								  HDA_INPUT));
1662c577b8a1SJoseph Chan 			if (err < 0)
1663c577b8a1SJoseph Chan 				return err;
1664c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1665c577b8a1SJoseph Chan 					      "Master Front Playback Switch",
16669645c203SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1667f7278fd0SJosepch Chan 								  HDA_INPUT));
1668c577b8a1SJoseph Chan 			if (err < 0)
1669c577b8a1SJoseph Chan 				return err;
1670c577b8a1SJoseph Chan 
1671c577b8a1SJoseph Chan 			/* add control to PW3 */
1672c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Volume", chname[i]);
1673c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1674f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1675f7278fd0SJosepch Chan 								  HDA_OUTPUT));
1676c577b8a1SJoseph Chan 			if (err < 0)
1677c577b8a1SJoseph Chan 				return err;
1678c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Switch", chname[i]);
1679c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1680f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1681f7278fd0SJosepch Chan 								  HDA_OUTPUT));
1682c577b8a1SJoseph Chan 			if (err < 0)
1683c577b8a1SJoseph Chan 				return err;
1684c577b8a1SJoseph Chan 		} else {
1685c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Volume", chname[i]);
1686c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1687f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1688f7278fd0SJosepch Chan 								  HDA_OUTPUT));
1689c577b8a1SJoseph Chan 			if (err < 0)
1690c577b8a1SJoseph Chan 				return err;
1691c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Switch", chname[i]);
1692c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1693f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1694f7278fd0SJosepch Chan 								  HDA_OUTPUT));
1695c577b8a1SJoseph Chan 			if (err < 0)
1696c577b8a1SJoseph Chan 				return err;
1697c577b8a1SJoseph Chan 		}
1698c577b8a1SJoseph Chan 	}
1699c577b8a1SJoseph Chan 
1700c577b8a1SJoseph Chan 	return 0;
1701c577b8a1SJoseph Chan }
1702c577b8a1SJoseph Chan 
17030aa62aefSHarald Welte static void create_hp_imux(struct via_spec *spec)
17040aa62aefSHarald Welte {
17050aa62aefSHarald Welte 	int i;
17060aa62aefSHarald Welte 	struct hda_input_mux *imux = &spec->private_imux[1];
17070aa62aefSHarald Welte 	static const char *texts[] = { "OFF", "ON", NULL};
17080aa62aefSHarald Welte 
17090aa62aefSHarald Welte 	/* for hp mode select */
17100aa62aefSHarald Welte 	i = 0;
17110aa62aefSHarald Welte 	while (texts[i] != NULL) {
17120aa62aefSHarald Welte 		imux->items[imux->num_items].label =  texts[i];
17130aa62aefSHarald Welte 		imux->items[imux->num_items].index = i;
17140aa62aefSHarald Welte 		imux->num_items++;
17150aa62aefSHarald Welte 		i++;
17160aa62aefSHarald Welte 	}
17170aa62aefSHarald Welte 
17180aa62aefSHarald Welte 	spec->hp_mux = &spec->private_imux[1];
17190aa62aefSHarald Welte }
17200aa62aefSHarald Welte 
1721c577b8a1SJoseph Chan static int vt1708_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
1722c577b8a1SJoseph Chan {
1723c577b8a1SJoseph Chan 	int err;
1724c577b8a1SJoseph Chan 
1725c577b8a1SJoseph Chan 	if (!pin)
1726c577b8a1SJoseph Chan 		return 0;
1727c577b8a1SJoseph Chan 
1728c577b8a1SJoseph Chan 	spec->multiout.hp_nid = VT1708_HP_NID; /* AOW3 */
1729cdc1784dSLydia Wang 	spec->hp_independent_mode_index = 1;
1730c577b8a1SJoseph Chan 
1731c577b8a1SJoseph Chan 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1732c577b8a1SJoseph Chan 			      "Headphone Playback Volume",
1733c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1734c577b8a1SJoseph Chan 	if (err < 0)
1735c577b8a1SJoseph Chan 		return err;
1736c577b8a1SJoseph Chan 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1737c577b8a1SJoseph Chan 			      "Headphone Playback Switch",
1738c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1739c577b8a1SJoseph Chan 	if (err < 0)
1740c577b8a1SJoseph Chan 		return err;
1741c577b8a1SJoseph Chan 
17420aa62aefSHarald Welte 	create_hp_imux(spec);
17430aa62aefSHarald Welte 
1744c577b8a1SJoseph Chan 	return 0;
1745c577b8a1SJoseph Chan }
1746c577b8a1SJoseph Chan 
1747c577b8a1SJoseph Chan /* create playback/capture controls for input pins */
1748c577b8a1SJoseph Chan static int vt1708_auto_create_analog_input_ctls(struct via_spec *spec,
1749c577b8a1SJoseph Chan 						const struct auto_pin_cfg *cfg)
1750c577b8a1SJoseph Chan {
1751c577b8a1SJoseph Chan 	static char *labels[] = {
1752c577b8a1SJoseph Chan 		"Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
1753c577b8a1SJoseph Chan 	};
17540aa62aefSHarald Welte 	struct hda_input_mux *imux = &spec->private_imux[0];
1755c577b8a1SJoseph Chan 	int i, err, idx = 0;
1756c577b8a1SJoseph Chan 
1757c577b8a1SJoseph Chan 	/* for internal loopback recording select */
1758c577b8a1SJoseph Chan 	imux->items[imux->num_items].label = "Stereo Mixer";
1759c577b8a1SJoseph Chan 	imux->items[imux->num_items].index = idx;
1760c577b8a1SJoseph Chan 	imux->num_items++;
1761c577b8a1SJoseph Chan 
1762c577b8a1SJoseph Chan 	for (i = 0; i < AUTO_PIN_LAST; i++) {
1763c577b8a1SJoseph Chan 		if (!cfg->input_pins[i])
1764c577b8a1SJoseph Chan 			continue;
1765c577b8a1SJoseph Chan 
1766c577b8a1SJoseph Chan 		switch (cfg->input_pins[i]) {
1767c577b8a1SJoseph Chan 		case 0x1d: /* Mic */
1768c577b8a1SJoseph Chan 			idx = 2;
1769c577b8a1SJoseph Chan 			break;
1770c577b8a1SJoseph Chan 
1771c577b8a1SJoseph Chan 		case 0x1e: /* Line In */
1772c577b8a1SJoseph Chan 			idx = 3;
1773c577b8a1SJoseph Chan 			break;
1774c577b8a1SJoseph Chan 
1775c577b8a1SJoseph Chan 		case 0x21: /* Front Mic */
1776c577b8a1SJoseph Chan 			idx = 4;
1777c577b8a1SJoseph Chan 			break;
1778c577b8a1SJoseph Chan 
1779c577b8a1SJoseph Chan 		case 0x24: /* CD */
1780c577b8a1SJoseph Chan 			idx = 1;
1781c577b8a1SJoseph Chan 			break;
1782c577b8a1SJoseph Chan 		}
17839510e8ddSLydia Wang 		err = via_new_analog_input(spec, labels[i], idx, 0x17);
1784c577b8a1SJoseph Chan 		if (err < 0)
1785c577b8a1SJoseph Chan 			return err;
1786c577b8a1SJoseph Chan 		imux->items[imux->num_items].label = labels[i];
1787c577b8a1SJoseph Chan 		imux->items[imux->num_items].index = idx;
1788c577b8a1SJoseph Chan 		imux->num_items++;
1789c577b8a1SJoseph Chan 	}
1790c577b8a1SJoseph Chan 	return 0;
1791c577b8a1SJoseph Chan }
1792c577b8a1SJoseph Chan 
1793cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
1794cb53c626STakashi Iwai static struct hda_amp_list vt1708_loopbacks[] = {
1795cb53c626STakashi Iwai 	{ 0x17, HDA_INPUT, 1 },
1796cb53c626STakashi Iwai 	{ 0x17, HDA_INPUT, 2 },
1797cb53c626STakashi Iwai 	{ 0x17, HDA_INPUT, 3 },
1798cb53c626STakashi Iwai 	{ 0x17, HDA_INPUT, 4 },
1799cb53c626STakashi Iwai 	{ } /* end */
1800cb53c626STakashi Iwai };
1801cb53c626STakashi Iwai #endif
1802cb53c626STakashi Iwai 
180376d9b0ddSHarald Welte static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
180476d9b0ddSHarald Welte {
180576d9b0ddSHarald Welte 	unsigned int def_conf;
180676d9b0ddSHarald Welte 	unsigned char seqassoc;
180776d9b0ddSHarald Welte 
18082f334f92STakashi Iwai 	def_conf = snd_hda_codec_get_pincfg(codec, nid);
180976d9b0ddSHarald Welte 	seqassoc = (unsigned char) get_defcfg_association(def_conf);
181076d9b0ddSHarald Welte 	seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
181182ef9e45SLydia Wang 	if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
181282ef9e45SLydia Wang 	    && (seqassoc == 0xf0 || seqassoc == 0xff)) {
181376d9b0ddSHarald Welte 		def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
18142f334f92STakashi Iwai 		snd_hda_codec_set_pincfg(codec, nid, def_conf);
181576d9b0ddSHarald Welte 	}
181676d9b0ddSHarald Welte 
181776d9b0ddSHarald Welte 	return;
181876d9b0ddSHarald Welte }
181976d9b0ddSHarald Welte 
18201f2e99feSLydia Wang static int vt1708_jack_detectect_get(struct snd_kcontrol *kcontrol,
18211f2e99feSLydia Wang 				     struct snd_ctl_elem_value *ucontrol)
18221f2e99feSLydia Wang {
18231f2e99feSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
18241f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
18251f2e99feSLydia Wang 
18261f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
18271f2e99feSLydia Wang 		return 0;
18281f2e99feSLydia Wang 	spec->vt1708_jack_detectect =
18291f2e99feSLydia Wang 		!((snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8) & 0x1);
18301f2e99feSLydia Wang 	ucontrol->value.integer.value[0] = spec->vt1708_jack_detectect;
18311f2e99feSLydia Wang 	return 0;
18321f2e99feSLydia Wang }
18331f2e99feSLydia Wang 
18341f2e99feSLydia Wang static int vt1708_jack_detectect_put(struct snd_kcontrol *kcontrol,
18351f2e99feSLydia Wang 				     struct snd_ctl_elem_value *ucontrol)
18361f2e99feSLydia Wang {
18371f2e99feSLydia Wang 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
18381f2e99feSLydia Wang 	struct via_spec *spec = codec->spec;
18391f2e99feSLydia Wang 	int change;
18401f2e99feSLydia Wang 
18411f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
18421f2e99feSLydia Wang 		return 0;
18431f2e99feSLydia Wang 	spec->vt1708_jack_detectect = ucontrol->value.integer.value[0];
18441f2e99feSLydia Wang 	change = (0x1 & (snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8))
18451f2e99feSLydia Wang 		== !spec->vt1708_jack_detectect;
18461f2e99feSLydia Wang 	if (spec->vt1708_jack_detectect) {
18471f2e99feSLydia Wang 		mute_aa_path(codec, 1);
18481f2e99feSLydia Wang 		notify_aa_path_ctls(codec);
18491f2e99feSLydia Wang 	}
18501f2e99feSLydia Wang 	return change;
18511f2e99feSLydia Wang }
18521f2e99feSLydia Wang 
18531f2e99feSLydia Wang static struct snd_kcontrol_new vt1708_jack_detectect[] = {
18541f2e99feSLydia Wang 	{
18551f2e99feSLydia Wang 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
18561f2e99feSLydia Wang 		.name = "Jack Detect",
18571f2e99feSLydia Wang 		.count = 1,
18581f2e99feSLydia Wang 		.info = snd_ctl_boolean_mono_info,
18591f2e99feSLydia Wang 		.get = vt1708_jack_detectect_get,
18601f2e99feSLydia Wang 		.put = vt1708_jack_detectect_put,
18611f2e99feSLydia Wang 	},
18621f2e99feSLydia Wang 	{} /* end */
18631f2e99feSLydia Wang };
18641f2e99feSLydia Wang 
1865c577b8a1SJoseph Chan static int vt1708_parse_auto_config(struct hda_codec *codec)
1866c577b8a1SJoseph Chan {
1867c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
1868c577b8a1SJoseph Chan 	int err;
1869c577b8a1SJoseph Chan 
187076d9b0ddSHarald Welte 	/* Add HP and CD pin config connect bit re-config action */
187176d9b0ddSHarald Welte 	vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
187276d9b0ddSHarald Welte 	vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
187376d9b0ddSHarald Welte 
1874c577b8a1SJoseph Chan 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1875c577b8a1SJoseph Chan 	if (err < 0)
1876c577b8a1SJoseph Chan 		return err;
1877c577b8a1SJoseph Chan 	err = vt1708_auto_fill_dac_nids(spec, &spec->autocfg);
1878c577b8a1SJoseph Chan 	if (err < 0)
1879c577b8a1SJoseph Chan 		return err;
1880c577b8a1SJoseph Chan 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
1881c577b8a1SJoseph Chan 		return 0; /* can't find valid BIOS pin config */
1882c577b8a1SJoseph Chan 
1883c577b8a1SJoseph Chan 	err = vt1708_auto_create_multi_out_ctls(spec, &spec->autocfg);
1884c577b8a1SJoseph Chan 	if (err < 0)
1885c577b8a1SJoseph Chan 		return err;
1886c577b8a1SJoseph Chan 	err = vt1708_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
1887c577b8a1SJoseph Chan 	if (err < 0)
1888c577b8a1SJoseph Chan 		return err;
1889c577b8a1SJoseph Chan 	err = vt1708_auto_create_analog_input_ctls(spec, &spec->autocfg);
1890c577b8a1SJoseph Chan 	if (err < 0)
1891c577b8a1SJoseph Chan 		return err;
18921f2e99feSLydia Wang 	/* add jack detect on/off control */
18931f2e99feSLydia Wang 	err = snd_hda_add_new_ctls(codec, vt1708_jack_detectect);
18941f2e99feSLydia Wang 	if (err < 0)
18951f2e99feSLydia Wang 		return err;
1896c577b8a1SJoseph Chan 
1897c577b8a1SJoseph Chan 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
1898c577b8a1SJoseph Chan 
18990852d7a6STakashi Iwai 	if (spec->autocfg.dig_outs)
1900c577b8a1SJoseph Chan 		spec->multiout.dig_out_nid = VT1708_DIGOUT_NID;
190155d1d6c1STakashi Iwai 	spec->dig_in_pin = VT1708_DIGIN_PIN;
1902c577b8a1SJoseph Chan 	if (spec->autocfg.dig_in_pin)
1903c577b8a1SJoseph Chan 		spec->dig_in_nid = VT1708_DIGIN_NID;
1904c577b8a1SJoseph Chan 
1905603c4019STakashi Iwai 	if (spec->kctls.list)
1906603c4019STakashi Iwai 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
1907c577b8a1SJoseph Chan 
190869e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1708_volume_init_verbs;
1909c577b8a1SJoseph Chan 
19100aa62aefSHarald Welte 	spec->input_mux = &spec->private_imux[0];
19110aa62aefSHarald Welte 
1912f8fdd495SHarald Welte 	if (spec->hp_mux)
19130aa62aefSHarald Welte 		spec->mixers[spec->num_mixers++] = via_hp_mixer;
1914c577b8a1SJoseph Chan 
19151564b287SLydia Wang 	spec->mixers[spec->num_mixers++] = via_smart51_mixer;
1916c577b8a1SJoseph Chan 	return 1;
1917c577b8a1SJoseph Chan }
1918c577b8a1SJoseph Chan 
1919c577b8a1SJoseph Chan /* init callback for auto-configuration model -- overriding the default init */
1920c577b8a1SJoseph Chan static int via_auto_init(struct hda_codec *codec)
1921c577b8a1SJoseph Chan {
1922c577b8a1SJoseph Chan 	via_init(codec);
1923c577b8a1SJoseph Chan 	via_auto_init_multi_out(codec);
1924c577b8a1SJoseph Chan 	via_auto_init_hp_out(codec);
1925c577b8a1SJoseph Chan 	via_auto_init_analog_input(codec);
1926c577b8a1SJoseph Chan 	return 0;
1927c577b8a1SJoseph Chan }
1928c577b8a1SJoseph Chan 
19291f2e99feSLydia Wang static void vt1708_update_hp_jack_state(struct work_struct *work)
19301f2e99feSLydia Wang {
19311f2e99feSLydia Wang 	struct via_spec *spec = container_of(work, struct via_spec,
19321f2e99feSLydia Wang 					     vt1708_hp_work.work);
19331f2e99feSLydia Wang 	if (spec->codec_type != VT1708)
19341f2e99feSLydia Wang 		return;
19351f2e99feSLydia Wang 	/* if jack state toggled */
19361f2e99feSLydia Wang 	if (spec->vt1708_hp_present
19371f2e99feSLydia Wang 	    != (snd_hda_codec_read(spec->codec, spec->autocfg.hp_pins[0], 0,
19381f2e99feSLydia Wang 				   AC_VERB_GET_PIN_SENSE, 0) >> 31)) {
19391f2e99feSLydia Wang 		spec->vt1708_hp_present ^= 1;
19401f2e99feSLydia Wang 		via_hp_automute(spec->codec);
19411f2e99feSLydia Wang 	}
19421f2e99feSLydia Wang 	vt1708_start_hp_work(spec);
19431f2e99feSLydia Wang }
19441f2e99feSLydia Wang 
1945337b9d02STakashi Iwai static int get_mux_nids(struct hda_codec *codec)
1946337b9d02STakashi Iwai {
1947337b9d02STakashi Iwai 	struct via_spec *spec = codec->spec;
1948337b9d02STakashi Iwai 	hda_nid_t nid, conn[8];
1949337b9d02STakashi Iwai 	unsigned int type;
1950337b9d02STakashi Iwai 	int i, n;
1951337b9d02STakashi Iwai 
1952337b9d02STakashi Iwai 	for (i = 0; i < spec->num_adc_nids; i++) {
1953337b9d02STakashi Iwai 		nid = spec->adc_nids[i];
1954337b9d02STakashi Iwai 		while (nid) {
1955a22d543aSTakashi Iwai 			type = get_wcaps_type(get_wcaps(codec, nid));
19561c55d521STakashi Iwai 			if (type == AC_WID_PIN)
19571c55d521STakashi Iwai 				break;
1958337b9d02STakashi Iwai 			n = snd_hda_get_connections(codec, nid, conn,
1959337b9d02STakashi Iwai 						    ARRAY_SIZE(conn));
1960337b9d02STakashi Iwai 			if (n <= 0)
1961337b9d02STakashi Iwai 				break;
1962337b9d02STakashi Iwai 			if (n > 1) {
1963337b9d02STakashi Iwai 				spec->mux_nids[i] = nid;
1964337b9d02STakashi Iwai 				break;
1965337b9d02STakashi Iwai 			}
1966337b9d02STakashi Iwai 			nid = conn[0];
1967337b9d02STakashi Iwai 		}
1968337b9d02STakashi Iwai 	}
19691c55d521STakashi Iwai 	return 0;
1970337b9d02STakashi Iwai }
1971337b9d02STakashi Iwai 
1972c577b8a1SJoseph Chan static int patch_vt1708(struct hda_codec *codec)
1973c577b8a1SJoseph Chan {
1974c577b8a1SJoseph Chan 	struct via_spec *spec;
1975c577b8a1SJoseph Chan 	int err;
1976c577b8a1SJoseph Chan 
1977c577b8a1SJoseph Chan 	/* create a codec specific record */
1978eb14a46cSHarald Welte 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1979c577b8a1SJoseph Chan 	if (spec == NULL)
1980c577b8a1SJoseph Chan 		return -ENOMEM;
1981c577b8a1SJoseph Chan 
1982c577b8a1SJoseph Chan 	codec->spec = spec;
1983c577b8a1SJoseph Chan 
1984c577b8a1SJoseph Chan 	/* automatic parse from the BIOS config */
1985c577b8a1SJoseph Chan 	err = vt1708_parse_auto_config(codec);
1986c577b8a1SJoseph Chan 	if (err < 0) {
1987c577b8a1SJoseph Chan 		via_free(codec);
1988c577b8a1SJoseph Chan 		return err;
1989c577b8a1SJoseph Chan 	} else if (!err) {
1990c577b8a1SJoseph Chan 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
1991c577b8a1SJoseph Chan 		       "from BIOS.  Using genenic mode...\n");
1992c577b8a1SJoseph Chan 	}
1993c577b8a1SJoseph Chan 
1994c577b8a1SJoseph Chan 
1995c577b8a1SJoseph Chan 	spec->stream_name_analog = "VT1708 Analog";
1996c577b8a1SJoseph Chan 	spec->stream_analog_playback = &vt1708_pcm_analog_playback;
1997bc9b5623STakashi Iwai 	/* disable 32bit format on VT1708 */
1998bc9b5623STakashi Iwai 	if (codec->vendor_id == 0x11061708)
1999bc9b5623STakashi Iwai 		spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
2000c577b8a1SJoseph Chan 	spec->stream_analog_capture = &vt1708_pcm_analog_capture;
2001c577b8a1SJoseph Chan 
2002c577b8a1SJoseph Chan 	spec->stream_name_digital = "VT1708 Digital";
2003c577b8a1SJoseph Chan 	spec->stream_digital_playback = &vt1708_pcm_digital_playback;
2004c577b8a1SJoseph Chan 	spec->stream_digital_capture = &vt1708_pcm_digital_capture;
2005c577b8a1SJoseph Chan 
2006c577b8a1SJoseph Chan 
2007c577b8a1SJoseph Chan 	if (!spec->adc_nids && spec->input_mux) {
2008c577b8a1SJoseph Chan 		spec->adc_nids = vt1708_adc_nids;
2009c577b8a1SJoseph Chan 		spec->num_adc_nids = ARRAY_SIZE(vt1708_adc_nids);
20100f67a611STakashi Iwai 		get_mux_nids(codec);
2011c577b8a1SJoseph Chan 		spec->mixers[spec->num_mixers] = vt1708_capture_mixer;
2012c577b8a1SJoseph Chan 		spec->num_mixers++;
2013c577b8a1SJoseph Chan 	}
2014c577b8a1SJoseph Chan 
2015c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
2016c577b8a1SJoseph Chan 
2017c577b8a1SJoseph Chan 	codec->patch_ops.init = via_auto_init;
2018cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
2019cb53c626STakashi Iwai 	spec->loopback.amplist = vt1708_loopbacks;
2020cb53c626STakashi Iwai #endif
20211f2e99feSLydia Wang 	spec->codec = codec;
20221f2e99feSLydia Wang 	INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
2023c577b8a1SJoseph Chan 	return 0;
2024c577b8a1SJoseph Chan }
2025c577b8a1SJoseph Chan 
2026c577b8a1SJoseph Chan /* capture mixer elements */
2027c577b8a1SJoseph Chan static struct snd_kcontrol_new vt1709_capture_mixer[] = {
2028c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x0, HDA_INPUT),
2029c577b8a1SJoseph Chan 	HDA_CODEC_MUTE("Capture Switch", 0x14, 0x0, HDA_INPUT),
2030c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x15, 0x0, HDA_INPUT),
2031c577b8a1SJoseph Chan 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x15, 0x0, HDA_INPUT),
2032c577b8a1SJoseph Chan 	HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x16, 0x0, HDA_INPUT),
2033c577b8a1SJoseph Chan 	HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x16, 0x0, HDA_INPUT),
2034c577b8a1SJoseph Chan 	{
2035c577b8a1SJoseph Chan 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2036c577b8a1SJoseph Chan 		/* The multiple "Capture Source" controls confuse alsamixer
2037c577b8a1SJoseph Chan 		 * So call somewhat different..
2038c577b8a1SJoseph Chan 		 */
2039c577b8a1SJoseph Chan 		/* .name = "Capture Source", */
2040c577b8a1SJoseph Chan 		.name = "Input Source",
2041c577b8a1SJoseph Chan 		.count = 1,
2042c577b8a1SJoseph Chan 		.info = via_mux_enum_info,
2043c577b8a1SJoseph Chan 		.get = via_mux_enum_get,
2044c577b8a1SJoseph Chan 		.put = via_mux_enum_put,
2045c577b8a1SJoseph Chan 	},
2046c577b8a1SJoseph Chan 	{ } /* end */
2047c577b8a1SJoseph Chan };
2048c577b8a1SJoseph Chan 
204969e52a80SHarald Welte static struct hda_verb vt1709_uniwill_init_verbs[] = {
2050a34df19aSLydia Wang 	{0x20, AC_VERB_SET_UNSOLICITED_ENABLE,
2051a34df19aSLydia Wang 	 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
205269e52a80SHarald Welte 	{ }
205369e52a80SHarald Welte };
205469e52a80SHarald Welte 
2055c577b8a1SJoseph Chan /*
2056c577b8a1SJoseph Chan  * generic initialization of ADC, input mixers and output mixers
2057c577b8a1SJoseph Chan  */
2058c577b8a1SJoseph Chan static struct hda_verb vt1709_10ch_volume_init_verbs[] = {
2059c577b8a1SJoseph Chan 	/*
2060c577b8a1SJoseph Chan 	 * Unmute ADC0-2 and set the default input to mic-in
2061c577b8a1SJoseph Chan 	 */
2062c577b8a1SJoseph Chan 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2063c577b8a1SJoseph Chan 	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2064c577b8a1SJoseph Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2065c577b8a1SJoseph Chan 
2066c577b8a1SJoseph Chan 
2067f7278fd0SJosepch Chan 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2068c577b8a1SJoseph Chan 	 * mixer widget
2069c577b8a1SJoseph Chan 	 */
2070c577b8a1SJoseph Chan 	/* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2071f7278fd0SJosepch Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2072f7278fd0SJosepch Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2073f7278fd0SJosepch Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2074f7278fd0SJosepch Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2075f7278fd0SJosepch Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2076c577b8a1SJoseph Chan 
2077c577b8a1SJoseph Chan 	/*
2078c577b8a1SJoseph Chan 	 * Set up output selector (0x1a, 0x1b, 0x29)
2079c577b8a1SJoseph Chan 	 */
2080c577b8a1SJoseph Chan 	/* set vol=0 to output mixers */
2081c577b8a1SJoseph Chan 	{0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2082c577b8a1SJoseph Chan 	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2083c577b8a1SJoseph Chan 	{0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2084c577b8a1SJoseph Chan 
2085c577b8a1SJoseph Chan 	/*
2086c577b8a1SJoseph Chan 	 *  Unmute PW3 and PW4
2087c577b8a1SJoseph Chan 	 */
2088c577b8a1SJoseph Chan 	{0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2089c577b8a1SJoseph Chan 	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2090c577b8a1SJoseph Chan 
2091c577b8a1SJoseph Chan 	/* Set input of PW4 as AOW4 */
2092c577b8a1SJoseph Chan 	{0x20, AC_VERB_SET_CONNECT_SEL, 0x1},
2093c577b8a1SJoseph Chan 	/* PW9 Output enable */
2094c577b8a1SJoseph Chan 	{0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2095c577b8a1SJoseph Chan 	{ }
2096c577b8a1SJoseph Chan };
2097c577b8a1SJoseph Chan 
2098c577b8a1SJoseph Chan static struct hda_pcm_stream vt1709_10ch_pcm_analog_playback = {
2099c577b8a1SJoseph Chan 	.substreams = 1,
2100c577b8a1SJoseph Chan 	.channels_min = 2,
2101c577b8a1SJoseph Chan 	.channels_max = 10,
2102c577b8a1SJoseph Chan 	.nid = 0x10, /* NID to query formats and rates */
2103c577b8a1SJoseph Chan 	.ops = {
2104c577b8a1SJoseph Chan 		.open = via_playback_pcm_open,
2105c873cc25SLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
2106c873cc25SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup,
2107c577b8a1SJoseph Chan 	},
2108c577b8a1SJoseph Chan };
2109c577b8a1SJoseph Chan 
2110c577b8a1SJoseph Chan static struct hda_pcm_stream vt1709_6ch_pcm_analog_playback = {
2111c577b8a1SJoseph Chan 	.substreams = 1,
2112c577b8a1SJoseph Chan 	.channels_min = 2,
2113c577b8a1SJoseph Chan 	.channels_max = 6,
2114c577b8a1SJoseph Chan 	.nid = 0x10, /* NID to query formats and rates */
2115c577b8a1SJoseph Chan 	.ops = {
2116c577b8a1SJoseph Chan 		.open = via_playback_pcm_open,
2117c873cc25SLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
2118c873cc25SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup,
2119c577b8a1SJoseph Chan 	},
2120c577b8a1SJoseph Chan };
2121c577b8a1SJoseph Chan 
2122c577b8a1SJoseph Chan static struct hda_pcm_stream vt1709_pcm_analog_capture = {
2123c577b8a1SJoseph Chan 	.substreams = 2,
2124c577b8a1SJoseph Chan 	.channels_min = 2,
2125c577b8a1SJoseph Chan 	.channels_max = 2,
2126c577b8a1SJoseph Chan 	.nid = 0x14, /* NID to query formats and rates */
2127c577b8a1SJoseph Chan 	.ops = {
2128c577b8a1SJoseph Chan 		.prepare = via_capture_pcm_prepare,
2129c577b8a1SJoseph Chan 		.cleanup = via_capture_pcm_cleanup
2130c577b8a1SJoseph Chan 	},
2131c577b8a1SJoseph Chan };
2132c577b8a1SJoseph Chan 
2133c577b8a1SJoseph Chan static struct hda_pcm_stream vt1709_pcm_digital_playback = {
2134c577b8a1SJoseph Chan 	.substreams = 1,
2135c577b8a1SJoseph Chan 	.channels_min = 2,
2136c577b8a1SJoseph Chan 	.channels_max = 2,
2137c577b8a1SJoseph Chan 	/* NID is set in via_build_pcms */
2138c577b8a1SJoseph Chan 	.ops = {
2139c577b8a1SJoseph Chan 		.open = via_dig_playback_pcm_open,
2140c577b8a1SJoseph Chan 		.close = via_dig_playback_pcm_close
2141c577b8a1SJoseph Chan 	},
2142c577b8a1SJoseph Chan };
2143c577b8a1SJoseph Chan 
2144c577b8a1SJoseph Chan static struct hda_pcm_stream vt1709_pcm_digital_capture = {
2145c577b8a1SJoseph Chan 	.substreams = 1,
2146c577b8a1SJoseph Chan 	.channels_min = 2,
2147c577b8a1SJoseph Chan 	.channels_max = 2,
2148c577b8a1SJoseph Chan };
2149c577b8a1SJoseph Chan 
2150c577b8a1SJoseph Chan static int vt1709_auto_fill_dac_nids(struct via_spec *spec,
2151c577b8a1SJoseph Chan 				     const struct auto_pin_cfg *cfg)
2152c577b8a1SJoseph Chan {
2153c577b8a1SJoseph Chan 	int i;
2154c577b8a1SJoseph Chan 	hda_nid_t nid;
2155c577b8a1SJoseph Chan 
2156c577b8a1SJoseph Chan 	if (cfg->line_outs == 4)  /* 10 channels */
2157c577b8a1SJoseph Chan 		spec->multiout.num_dacs = cfg->line_outs+1; /* AOW0~AOW4 */
2158c577b8a1SJoseph Chan 	else if (cfg->line_outs == 3) /* 6 channels */
2159c577b8a1SJoseph Chan 		spec->multiout.num_dacs = cfg->line_outs; /* AOW0~AOW2 */
2160c577b8a1SJoseph Chan 
2161c577b8a1SJoseph Chan 	spec->multiout.dac_nids = spec->private_dac_nids;
2162c577b8a1SJoseph Chan 
2163c577b8a1SJoseph Chan 	if (cfg->line_outs == 4) { /* 10 channels */
2164c577b8a1SJoseph Chan 		for (i = 0; i < cfg->line_outs; i++) {
2165c577b8a1SJoseph Chan 			nid = cfg->line_out_pins[i];
2166c577b8a1SJoseph Chan 			if (nid) {
2167c577b8a1SJoseph Chan 				/* config dac list */
2168c577b8a1SJoseph Chan 				switch (i) {
2169c577b8a1SJoseph Chan 				case AUTO_SEQ_FRONT:
2170c577b8a1SJoseph Chan 					/* AOW0 */
2171c577b8a1SJoseph Chan 					spec->multiout.dac_nids[i] = 0x10;
2172c577b8a1SJoseph Chan 					break;
2173c577b8a1SJoseph Chan 				case AUTO_SEQ_CENLFE:
2174c577b8a1SJoseph Chan 					/* AOW2 */
2175c577b8a1SJoseph Chan 					spec->multiout.dac_nids[i] = 0x12;
2176c577b8a1SJoseph Chan 					break;
2177c577b8a1SJoseph Chan 				case AUTO_SEQ_SURROUND:
2178c577b8a1SJoseph Chan 					/* AOW3 */
2179fb4cb772SHarald Welte 					spec->multiout.dac_nids[i] = 0x11;
2180c577b8a1SJoseph Chan 					break;
2181c577b8a1SJoseph Chan 				case AUTO_SEQ_SIDE:
2182c577b8a1SJoseph Chan 					/* AOW1 */
2183fb4cb772SHarald Welte 					spec->multiout.dac_nids[i] = 0x27;
2184c577b8a1SJoseph Chan 					break;
2185c577b8a1SJoseph Chan 				default:
2186c577b8a1SJoseph Chan 					break;
2187c577b8a1SJoseph Chan 				}
2188c577b8a1SJoseph Chan 			}
2189c577b8a1SJoseph Chan 		}
2190c577b8a1SJoseph Chan 		spec->multiout.dac_nids[cfg->line_outs] = 0x28; /* AOW4 */
2191c577b8a1SJoseph Chan 
2192c577b8a1SJoseph Chan 	} else if (cfg->line_outs == 3) { /* 6 channels */
2193c577b8a1SJoseph Chan 		for(i = 0; i < cfg->line_outs; i++) {
2194c577b8a1SJoseph Chan 			nid = cfg->line_out_pins[i];
2195c577b8a1SJoseph Chan 			if (nid) {
2196c577b8a1SJoseph Chan 				/* config dac list */
2197c577b8a1SJoseph Chan 				switch(i) {
2198c577b8a1SJoseph Chan 				case AUTO_SEQ_FRONT:
2199c577b8a1SJoseph Chan 					/* AOW0 */
2200c577b8a1SJoseph Chan 					spec->multiout.dac_nids[i] = 0x10;
2201c577b8a1SJoseph Chan 					break;
2202c577b8a1SJoseph Chan 				case AUTO_SEQ_CENLFE:
2203c577b8a1SJoseph Chan 					/* AOW2 */
2204c577b8a1SJoseph Chan 					spec->multiout.dac_nids[i] = 0x12;
2205c577b8a1SJoseph Chan 					break;
2206c577b8a1SJoseph Chan 				case AUTO_SEQ_SURROUND:
2207c577b8a1SJoseph Chan 					/* AOW1 */
2208c577b8a1SJoseph Chan 					spec->multiout.dac_nids[i] = 0x11;
2209c577b8a1SJoseph Chan 					break;
2210c577b8a1SJoseph Chan 				default:
2211c577b8a1SJoseph Chan 					break;
2212c577b8a1SJoseph Chan 				}
2213c577b8a1SJoseph Chan 			}
2214c577b8a1SJoseph Chan 		}
2215c577b8a1SJoseph Chan 	}
2216c577b8a1SJoseph Chan 
2217c577b8a1SJoseph Chan 	return 0;
2218c577b8a1SJoseph Chan }
2219c577b8a1SJoseph Chan 
2220c577b8a1SJoseph Chan /* add playback controls from the parsed DAC table */
2221c577b8a1SJoseph Chan static int vt1709_auto_create_multi_out_ctls(struct via_spec *spec,
2222c577b8a1SJoseph Chan 					     const struct auto_pin_cfg *cfg)
2223c577b8a1SJoseph Chan {
2224c577b8a1SJoseph Chan 	char name[32];
2225c577b8a1SJoseph Chan 	static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
22264483a2f5SLydia Wang 	hda_nid_t nid, nid_vol, nid_vols[] = {0x18, 0x1a, 0x1b, 0x29};
2227c577b8a1SJoseph Chan 	int i, err;
2228c577b8a1SJoseph Chan 
2229c577b8a1SJoseph Chan 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
2230c577b8a1SJoseph Chan 		nid = cfg->line_out_pins[i];
2231c577b8a1SJoseph Chan 
2232c577b8a1SJoseph Chan 		if (!nid)
2233c577b8a1SJoseph Chan 			continue;
2234c577b8a1SJoseph Chan 
22354483a2f5SLydia Wang 		nid_vol = nid_vols[i];
22364483a2f5SLydia Wang 
2237c577b8a1SJoseph Chan 		if (i == AUTO_SEQ_CENLFE) {
2238c577b8a1SJoseph Chan 			/* Center/LFE */
2239c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2240c577b8a1SJoseph Chan 					      "Center Playback Volume",
22414483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2242f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2243c577b8a1SJoseph Chan 			if (err < 0)
2244c577b8a1SJoseph Chan 				return err;
2245c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2246c577b8a1SJoseph Chan 					      "LFE Playback Volume",
22474483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2248f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2249c577b8a1SJoseph Chan 			if (err < 0)
2250c577b8a1SJoseph Chan 				return err;
2251c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2252c577b8a1SJoseph Chan 					      "Center Playback Switch",
22534483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2254f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2255c577b8a1SJoseph Chan 			if (err < 0)
2256c577b8a1SJoseph Chan 				return err;
2257c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2258c577b8a1SJoseph Chan 					      "LFE Playback Switch",
22594483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2260f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2261c577b8a1SJoseph Chan 			if (err < 0)
2262c577b8a1SJoseph Chan 				return err;
2263c577b8a1SJoseph Chan 		} else if (i == AUTO_SEQ_FRONT){
22644483a2f5SLydia Wang 			/* ADD control to mixer index 0 */
2265c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2266c577b8a1SJoseph Chan 					      "Master Front Playback Volume",
22674483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2268f7278fd0SJosepch Chan 								  HDA_INPUT));
2269c577b8a1SJoseph Chan 			if (err < 0)
2270c577b8a1SJoseph Chan 				return err;
2271c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2272c577b8a1SJoseph Chan 					      "Master Front Playback Switch",
22734483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2274f7278fd0SJosepch Chan 								  HDA_INPUT));
2275c577b8a1SJoseph Chan 			if (err < 0)
2276c577b8a1SJoseph Chan 				return err;
2277c577b8a1SJoseph Chan 
2278c577b8a1SJoseph Chan 			/* add control to PW3 */
2279c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Volume", chname[i]);
2280c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2281f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2282f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2283c577b8a1SJoseph Chan 			if (err < 0)
2284c577b8a1SJoseph Chan 				return err;
2285c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Switch", chname[i]);
2286c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2287f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2288f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2289c577b8a1SJoseph Chan 			if (err < 0)
2290c577b8a1SJoseph Chan 				return err;
2291c577b8a1SJoseph Chan 		} else if (i == AUTO_SEQ_SURROUND) {
2292c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Volume", chname[i]);
2293c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
22944483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2295f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2296c577b8a1SJoseph Chan 			if (err < 0)
2297c577b8a1SJoseph Chan 				return err;
2298c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Switch", chname[i]);
2299c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
23004483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2301f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2302c577b8a1SJoseph Chan 			if (err < 0)
2303c577b8a1SJoseph Chan 				return err;
2304c577b8a1SJoseph Chan 		} else if (i == AUTO_SEQ_SIDE) {
2305c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Volume", chname[i]);
2306c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
23074483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2308f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2309c577b8a1SJoseph Chan 			if (err < 0)
2310c577b8a1SJoseph Chan 				return err;
2311c577b8a1SJoseph Chan 			sprintf(name, "%s Playback Switch", chname[i]);
2312c577b8a1SJoseph Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
23134483a2f5SLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2314f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2315c577b8a1SJoseph Chan 			if (err < 0)
2316c577b8a1SJoseph Chan 				return err;
2317c577b8a1SJoseph Chan 		}
2318c577b8a1SJoseph Chan 	}
2319c577b8a1SJoseph Chan 
2320c577b8a1SJoseph Chan 	return 0;
2321c577b8a1SJoseph Chan }
2322c577b8a1SJoseph Chan 
2323c577b8a1SJoseph Chan static int vt1709_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
2324c577b8a1SJoseph Chan {
2325c577b8a1SJoseph Chan 	int err;
2326c577b8a1SJoseph Chan 
2327c577b8a1SJoseph Chan 	if (!pin)
2328c577b8a1SJoseph Chan 		return 0;
2329c577b8a1SJoseph Chan 
2330c577b8a1SJoseph Chan 	if (spec->multiout.num_dacs == 5) /* 10 channels */
2331c577b8a1SJoseph Chan 		spec->multiout.hp_nid = VT1709_HP_DAC_NID;
2332c577b8a1SJoseph Chan 	else if (spec->multiout.num_dacs == 3) /* 6 channels */
2333c577b8a1SJoseph Chan 		spec->multiout.hp_nid = 0;
2334cdc1784dSLydia Wang 	spec->hp_independent_mode_index = 1;
2335c577b8a1SJoseph Chan 
2336c577b8a1SJoseph Chan 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2337c577b8a1SJoseph Chan 			      "Headphone Playback Volume",
2338c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2339c577b8a1SJoseph Chan 	if (err < 0)
2340c577b8a1SJoseph Chan 		return err;
2341c577b8a1SJoseph Chan 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2342c577b8a1SJoseph Chan 			      "Headphone Playback Switch",
2343c577b8a1SJoseph Chan 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2344c577b8a1SJoseph Chan 	if (err < 0)
2345c577b8a1SJoseph Chan 		return err;
2346c577b8a1SJoseph Chan 
2347c577b8a1SJoseph Chan 	return 0;
2348c577b8a1SJoseph Chan }
2349c577b8a1SJoseph Chan 
2350c577b8a1SJoseph Chan /* create playback/capture controls for input pins */
2351c577b8a1SJoseph Chan static int vt1709_auto_create_analog_input_ctls(struct via_spec *spec,
2352c577b8a1SJoseph Chan 						const struct auto_pin_cfg *cfg)
2353c577b8a1SJoseph Chan {
2354c577b8a1SJoseph Chan 	static char *labels[] = {
2355c577b8a1SJoseph Chan 		"Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
2356c577b8a1SJoseph Chan 	};
23570aa62aefSHarald Welte 	struct hda_input_mux *imux = &spec->private_imux[0];
2358c577b8a1SJoseph Chan 	int i, err, idx = 0;
2359c577b8a1SJoseph Chan 
2360c577b8a1SJoseph Chan 	/* for internal loopback recording select */
2361c577b8a1SJoseph Chan 	imux->items[imux->num_items].label = "Stereo Mixer";
2362c577b8a1SJoseph Chan 	imux->items[imux->num_items].index = idx;
2363c577b8a1SJoseph Chan 	imux->num_items++;
2364c577b8a1SJoseph Chan 
2365c577b8a1SJoseph Chan 	for (i = 0; i < AUTO_PIN_LAST; i++) {
2366c577b8a1SJoseph Chan 		if (!cfg->input_pins[i])
2367c577b8a1SJoseph Chan 			continue;
2368c577b8a1SJoseph Chan 
2369c577b8a1SJoseph Chan 		switch (cfg->input_pins[i]) {
2370c577b8a1SJoseph Chan 		case 0x1d: /* Mic */
2371c577b8a1SJoseph Chan 			idx = 2;
2372c577b8a1SJoseph Chan 			break;
2373c577b8a1SJoseph Chan 
2374c577b8a1SJoseph Chan 		case 0x1e: /* Line In */
2375c577b8a1SJoseph Chan 			idx = 3;
2376c577b8a1SJoseph Chan 			break;
2377c577b8a1SJoseph Chan 
2378c577b8a1SJoseph Chan 		case 0x21: /* Front Mic */
2379c577b8a1SJoseph Chan 			idx = 4;
2380c577b8a1SJoseph Chan 			break;
2381c577b8a1SJoseph Chan 
2382c577b8a1SJoseph Chan 		case 0x23: /* CD */
2383c577b8a1SJoseph Chan 			idx = 1;
2384c577b8a1SJoseph Chan 			break;
2385c577b8a1SJoseph Chan 		}
23869510e8ddSLydia Wang 		err = via_new_analog_input(spec, labels[i], idx, 0x18);
2387c577b8a1SJoseph Chan 		if (err < 0)
2388c577b8a1SJoseph Chan 			return err;
2389c577b8a1SJoseph Chan 		imux->items[imux->num_items].label = labels[i];
2390c577b8a1SJoseph Chan 		imux->items[imux->num_items].index = idx;
2391c577b8a1SJoseph Chan 		imux->num_items++;
2392c577b8a1SJoseph Chan 	}
2393c577b8a1SJoseph Chan 	return 0;
2394c577b8a1SJoseph Chan }
2395c577b8a1SJoseph Chan 
2396c577b8a1SJoseph Chan static int vt1709_parse_auto_config(struct hda_codec *codec)
2397c577b8a1SJoseph Chan {
2398c577b8a1SJoseph Chan 	struct via_spec *spec = codec->spec;
2399c577b8a1SJoseph Chan 	int err;
2400c577b8a1SJoseph Chan 
2401c577b8a1SJoseph Chan 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2402c577b8a1SJoseph Chan 	if (err < 0)
2403c577b8a1SJoseph Chan 		return err;
2404c577b8a1SJoseph Chan 	err = vt1709_auto_fill_dac_nids(spec, &spec->autocfg);
2405c577b8a1SJoseph Chan 	if (err < 0)
2406c577b8a1SJoseph Chan 		return err;
2407c577b8a1SJoseph Chan 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2408c577b8a1SJoseph Chan 		return 0; /* can't find valid BIOS pin config */
2409c577b8a1SJoseph Chan 
2410c577b8a1SJoseph Chan 	err = vt1709_auto_create_multi_out_ctls(spec, &spec->autocfg);
2411c577b8a1SJoseph Chan 	if (err < 0)
2412c577b8a1SJoseph Chan 		return err;
2413c577b8a1SJoseph Chan 	err = vt1709_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2414c577b8a1SJoseph Chan 	if (err < 0)
2415c577b8a1SJoseph Chan 		return err;
2416c577b8a1SJoseph Chan 	err = vt1709_auto_create_analog_input_ctls(spec, &spec->autocfg);
2417c577b8a1SJoseph Chan 	if (err < 0)
2418c577b8a1SJoseph Chan 		return err;
2419c577b8a1SJoseph Chan 
2420c577b8a1SJoseph Chan 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2421c577b8a1SJoseph Chan 
24220852d7a6STakashi Iwai 	if (spec->autocfg.dig_outs)
2423c577b8a1SJoseph Chan 		spec->multiout.dig_out_nid = VT1709_DIGOUT_NID;
242455d1d6c1STakashi Iwai 	spec->dig_in_pin = VT1709_DIGIN_PIN;
2425c577b8a1SJoseph Chan 	if (spec->autocfg.dig_in_pin)
2426c577b8a1SJoseph Chan 		spec->dig_in_nid = VT1709_DIGIN_NID;
2427c577b8a1SJoseph Chan 
2428603c4019STakashi Iwai 	if (spec->kctls.list)
2429603c4019STakashi Iwai 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
2430c577b8a1SJoseph Chan 
24310aa62aefSHarald Welte 	spec->input_mux = &spec->private_imux[0];
2432c577b8a1SJoseph Chan 
2433f8fdd495SHarald Welte 	if (spec->hp_mux)
2434f8fdd495SHarald Welte 		spec->mixers[spec->num_mixers++] = via_hp_mixer;
2435f8fdd495SHarald Welte 
24361564b287SLydia Wang 	spec->mixers[spec->num_mixers++] = via_smart51_mixer;
2437c577b8a1SJoseph Chan 	return 1;
2438c577b8a1SJoseph Chan }
2439c577b8a1SJoseph Chan 
2440cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
2441cb53c626STakashi Iwai static struct hda_amp_list vt1709_loopbacks[] = {
2442cb53c626STakashi Iwai 	{ 0x18, HDA_INPUT, 1 },
2443cb53c626STakashi Iwai 	{ 0x18, HDA_INPUT, 2 },
2444cb53c626STakashi Iwai 	{ 0x18, HDA_INPUT, 3 },
2445cb53c626STakashi Iwai 	{ 0x18, HDA_INPUT, 4 },
2446cb53c626STakashi Iwai 	{ } /* end */
2447cb53c626STakashi Iwai };
2448cb53c626STakashi Iwai #endif
2449cb53c626STakashi Iwai 
2450c577b8a1SJoseph Chan static int patch_vt1709_10ch(struct hda_codec *codec)
2451c577b8a1SJoseph Chan {
2452c577b8a1SJoseph Chan 	struct via_spec *spec;
2453c577b8a1SJoseph Chan 	int err;
2454c577b8a1SJoseph Chan 
2455c577b8a1SJoseph Chan 	/* create a codec specific record */
2456eb14a46cSHarald Welte 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2457c577b8a1SJoseph Chan 	if (spec == NULL)
2458c577b8a1SJoseph Chan 		return -ENOMEM;
2459c577b8a1SJoseph Chan 
2460c577b8a1SJoseph Chan 	codec->spec = spec;
2461c577b8a1SJoseph Chan 
2462c577b8a1SJoseph Chan 	err = vt1709_parse_auto_config(codec);
2463c577b8a1SJoseph Chan 	if (err < 0) {
2464c577b8a1SJoseph Chan 		via_free(codec);
2465c577b8a1SJoseph Chan 		return err;
2466c577b8a1SJoseph Chan 	} else if (!err) {
2467c577b8a1SJoseph Chan 		printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
2468c577b8a1SJoseph Chan 		       "Using genenic mode...\n");
2469c577b8a1SJoseph Chan 	}
2470c577b8a1SJoseph Chan 
247169e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1709_10ch_volume_init_verbs;
247269e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
2473c577b8a1SJoseph Chan 
2474c577b8a1SJoseph Chan 	spec->stream_name_analog = "VT1709 Analog";
2475c577b8a1SJoseph Chan 	spec->stream_analog_playback = &vt1709_10ch_pcm_analog_playback;
2476c577b8a1SJoseph Chan 	spec->stream_analog_capture = &vt1709_pcm_analog_capture;
2477c577b8a1SJoseph Chan 
2478c577b8a1SJoseph Chan 	spec->stream_name_digital = "VT1709 Digital";
2479c577b8a1SJoseph Chan 	spec->stream_digital_playback = &vt1709_pcm_digital_playback;
2480c577b8a1SJoseph Chan 	spec->stream_digital_capture = &vt1709_pcm_digital_capture;
2481c577b8a1SJoseph Chan 
2482c577b8a1SJoseph Chan 
2483c577b8a1SJoseph Chan 	if (!spec->adc_nids && spec->input_mux) {
2484c577b8a1SJoseph Chan 		spec->adc_nids = vt1709_adc_nids;
2485c577b8a1SJoseph Chan 		spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
2486337b9d02STakashi Iwai 		get_mux_nids(codec);
2487c577b8a1SJoseph Chan 		spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
2488c577b8a1SJoseph Chan 		spec->num_mixers++;
2489c577b8a1SJoseph Chan 	}
2490c577b8a1SJoseph Chan 
2491c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
2492c577b8a1SJoseph Chan 
2493c577b8a1SJoseph Chan 	codec->patch_ops.init = via_auto_init;
249469e52a80SHarald Welte 	codec->patch_ops.unsol_event = via_unsol_event;
2495cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
2496cb53c626STakashi Iwai 	spec->loopback.amplist = vt1709_loopbacks;
2497cb53c626STakashi Iwai #endif
2498c577b8a1SJoseph Chan 
2499c577b8a1SJoseph Chan 	return 0;
2500c577b8a1SJoseph Chan }
2501c577b8a1SJoseph Chan /*
2502c577b8a1SJoseph Chan  * generic initialization of ADC, input mixers and output mixers
2503c577b8a1SJoseph Chan  */
2504c577b8a1SJoseph Chan static struct hda_verb vt1709_6ch_volume_init_verbs[] = {
2505c577b8a1SJoseph Chan 	/*
2506c577b8a1SJoseph Chan 	 * Unmute ADC0-2 and set the default input to mic-in
2507c577b8a1SJoseph Chan 	 */
2508c577b8a1SJoseph Chan 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2509c577b8a1SJoseph Chan 	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2510c577b8a1SJoseph Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2511c577b8a1SJoseph Chan 
2512c577b8a1SJoseph Chan 
2513c577b8a1SJoseph Chan 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2514c577b8a1SJoseph Chan 	 * mixer widget
2515c577b8a1SJoseph Chan 	 */
2516c577b8a1SJoseph Chan 	/* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2517c577b8a1SJoseph Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2518c577b8a1SJoseph Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2519c577b8a1SJoseph Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2520c577b8a1SJoseph Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2521c577b8a1SJoseph Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2522c577b8a1SJoseph Chan 
2523c577b8a1SJoseph Chan 	/*
2524c577b8a1SJoseph Chan 	 * Set up output selector (0x1a, 0x1b, 0x29)
2525c577b8a1SJoseph Chan 	 */
2526c577b8a1SJoseph Chan 	/* set vol=0 to output mixers */
2527c577b8a1SJoseph Chan 	{0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2528c577b8a1SJoseph Chan 	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2529c577b8a1SJoseph Chan 	{0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2530c577b8a1SJoseph Chan 
2531c577b8a1SJoseph Chan 	/*
2532c577b8a1SJoseph Chan 	 *  Unmute PW3 and PW4
2533c577b8a1SJoseph Chan 	 */
2534c577b8a1SJoseph Chan 	{0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2535c577b8a1SJoseph Chan 	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2536c577b8a1SJoseph Chan 
2537c577b8a1SJoseph Chan 	/* Set input of PW4 as MW0 */
2538c577b8a1SJoseph Chan 	{0x20, AC_VERB_SET_CONNECT_SEL, 0},
2539c577b8a1SJoseph Chan 	/* PW9 Output enable */
2540c577b8a1SJoseph Chan 	{0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2541c577b8a1SJoseph Chan 	{ }
2542c577b8a1SJoseph Chan };
2543c577b8a1SJoseph Chan 
2544c577b8a1SJoseph Chan static int patch_vt1709_6ch(struct hda_codec *codec)
2545c577b8a1SJoseph Chan {
2546c577b8a1SJoseph Chan 	struct via_spec *spec;
2547c577b8a1SJoseph Chan 	int err;
2548c577b8a1SJoseph Chan 
2549c577b8a1SJoseph Chan 	/* create a codec specific record */
2550eb14a46cSHarald Welte 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2551c577b8a1SJoseph Chan 	if (spec == NULL)
2552c577b8a1SJoseph Chan 		return -ENOMEM;
2553c577b8a1SJoseph Chan 
2554c577b8a1SJoseph Chan 	codec->spec = spec;
2555c577b8a1SJoseph Chan 
2556c577b8a1SJoseph Chan 	err = vt1709_parse_auto_config(codec);
2557c577b8a1SJoseph Chan 	if (err < 0) {
2558c577b8a1SJoseph Chan 		via_free(codec);
2559c577b8a1SJoseph Chan 		return err;
2560c577b8a1SJoseph Chan 	} else if (!err) {
2561c577b8a1SJoseph Chan 		printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
2562c577b8a1SJoseph Chan 		       "Using genenic mode...\n");
2563c577b8a1SJoseph Chan 	}
2564c577b8a1SJoseph Chan 
256569e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1709_6ch_volume_init_verbs;
256669e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
2567c577b8a1SJoseph Chan 
2568c577b8a1SJoseph Chan 	spec->stream_name_analog = "VT1709 Analog";
2569c577b8a1SJoseph Chan 	spec->stream_analog_playback = &vt1709_6ch_pcm_analog_playback;
2570c577b8a1SJoseph Chan 	spec->stream_analog_capture = &vt1709_pcm_analog_capture;
2571c577b8a1SJoseph Chan 
2572c577b8a1SJoseph Chan 	spec->stream_name_digital = "VT1709 Digital";
2573c577b8a1SJoseph Chan 	spec->stream_digital_playback = &vt1709_pcm_digital_playback;
2574c577b8a1SJoseph Chan 	spec->stream_digital_capture = &vt1709_pcm_digital_capture;
2575c577b8a1SJoseph Chan 
2576c577b8a1SJoseph Chan 
2577c577b8a1SJoseph Chan 	if (!spec->adc_nids && spec->input_mux) {
2578c577b8a1SJoseph Chan 		spec->adc_nids = vt1709_adc_nids;
2579c577b8a1SJoseph Chan 		spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
2580337b9d02STakashi Iwai 		get_mux_nids(codec);
2581c577b8a1SJoseph Chan 		spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
2582c577b8a1SJoseph Chan 		spec->num_mixers++;
2583c577b8a1SJoseph Chan 	}
2584c577b8a1SJoseph Chan 
2585c577b8a1SJoseph Chan 	codec->patch_ops = via_patch_ops;
2586c577b8a1SJoseph Chan 
2587c577b8a1SJoseph Chan 	codec->patch_ops.init = via_auto_init;
258869e52a80SHarald Welte 	codec->patch_ops.unsol_event = via_unsol_event;
2589cb53c626STakashi Iwai #ifdef CONFIG_SND_HDA_POWER_SAVE
2590cb53c626STakashi Iwai 	spec->loopback.amplist = vt1709_loopbacks;
2591cb53c626STakashi Iwai #endif
2592f7278fd0SJosepch Chan 	return 0;
2593f7278fd0SJosepch Chan }
2594f7278fd0SJosepch Chan 
2595f7278fd0SJosepch Chan /* capture mixer elements */
2596f7278fd0SJosepch Chan static struct snd_kcontrol_new vt1708B_capture_mixer[] = {
2597f7278fd0SJosepch Chan 	HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
2598f7278fd0SJosepch Chan 	HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
2599f7278fd0SJosepch Chan 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
2600f7278fd0SJosepch Chan 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
2601f7278fd0SJosepch Chan 	{
2602f7278fd0SJosepch Chan 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2603f7278fd0SJosepch Chan 		/* The multiple "Capture Source" controls confuse alsamixer
2604f7278fd0SJosepch Chan 		 * So call somewhat different..
2605f7278fd0SJosepch Chan 		 */
2606f7278fd0SJosepch Chan 		/* .name = "Capture Source", */
2607f7278fd0SJosepch Chan 		.name = "Input Source",
2608f7278fd0SJosepch Chan 		.count = 1,
2609f7278fd0SJosepch Chan 		.info = via_mux_enum_info,
2610f7278fd0SJosepch Chan 		.get = via_mux_enum_get,
2611f7278fd0SJosepch Chan 		.put = via_mux_enum_put,
2612f7278fd0SJosepch Chan 	},
2613f7278fd0SJosepch Chan 	{ } /* end */
2614f7278fd0SJosepch Chan };
2615f7278fd0SJosepch Chan /*
2616f7278fd0SJosepch Chan  * generic initialization of ADC, input mixers and output mixers
2617f7278fd0SJosepch Chan  */
2618f7278fd0SJosepch Chan static struct hda_verb vt1708B_8ch_volume_init_verbs[] = {
2619f7278fd0SJosepch Chan 	/*
2620f7278fd0SJosepch Chan 	 * Unmute ADC0-1 and set the default input to mic-in
2621f7278fd0SJosepch Chan 	 */
2622f7278fd0SJosepch Chan 	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2623f7278fd0SJosepch Chan 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2624f7278fd0SJosepch Chan 
2625f7278fd0SJosepch Chan 
2626f7278fd0SJosepch Chan 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2627f7278fd0SJosepch Chan 	 * mixer widget
2628f7278fd0SJosepch Chan 	 */
2629f7278fd0SJosepch Chan 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2630f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2631f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2632f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2633f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2634f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2635f7278fd0SJosepch Chan 
2636f7278fd0SJosepch Chan 	/*
2637f7278fd0SJosepch Chan 	 * Set up output mixers
2638f7278fd0SJosepch Chan 	 */
2639f7278fd0SJosepch Chan 	/* set vol=0 to output mixers */
2640f7278fd0SJosepch Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2641f7278fd0SJosepch Chan 	{0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2642f7278fd0SJosepch Chan 	{0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2643f7278fd0SJosepch Chan 
2644f7278fd0SJosepch Chan 	/* Setup default input to PW4 */
2645f7278fd0SJosepch Chan 	{0x1d, AC_VERB_SET_CONNECT_SEL, 0x1},
2646f7278fd0SJosepch Chan 	/* PW9 Output enable */
2647f7278fd0SJosepch Chan 	{0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2648f7278fd0SJosepch Chan 	/* PW10 Input enable */
2649f7278fd0SJosepch Chan 	{0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2650f7278fd0SJosepch Chan 	{ }
2651f7278fd0SJosepch Chan };
2652f7278fd0SJosepch Chan 
2653f7278fd0SJosepch Chan static struct hda_verb vt1708B_4ch_volume_init_verbs[] = {
2654f7278fd0SJosepch Chan 	/*
2655f7278fd0SJosepch Chan 	 * Unmute ADC0-1 and set the default input to mic-in
2656f7278fd0SJosepch Chan 	 */
2657f7278fd0SJosepch Chan 	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2658f7278fd0SJosepch Chan 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2659f7278fd0SJosepch Chan 
2660f7278fd0SJosepch Chan 
2661f7278fd0SJosepch Chan 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2662f7278fd0SJosepch Chan 	 * mixer widget
2663f7278fd0SJosepch Chan 	 */
2664f7278fd0SJosepch Chan 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2665f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2666f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2667f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2668f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2669f7278fd0SJosepch Chan 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2670f7278fd0SJosepch Chan 
2671f7278fd0SJosepch Chan 	/*
2672f7278fd0SJosepch Chan 	 * Set up output mixers
2673f7278fd0SJosepch Chan 	 */
2674f7278fd0SJosepch Chan 	/* set vol=0 to output mixers */
2675f7278fd0SJosepch Chan 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2676f7278fd0SJosepch Chan 	{0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2677f7278fd0SJosepch Chan 	{0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2678f7278fd0SJosepch Chan 
2679f7278fd0SJosepch Chan 	/* Setup default input of PW4 to MW0 */
2680f7278fd0SJosepch Chan 	{0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
2681f7278fd0SJosepch Chan 	/* PW9 Output enable */
2682f7278fd0SJosepch Chan 	{0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2683f7278fd0SJosepch Chan 	/* PW10 Input enable */
2684f7278fd0SJosepch Chan 	{0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2685f7278fd0SJosepch Chan 	{ }
2686f7278fd0SJosepch Chan };
2687f7278fd0SJosepch Chan 
268869e52a80SHarald Welte static struct hda_verb vt1708B_uniwill_init_verbs[] = {
2689a34df19aSLydia Wang 	{0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
2690a34df19aSLydia Wang 	 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
2691a34df19aSLydia Wang 	{0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2692a34df19aSLydia Wang 	{0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2693a34df19aSLydia Wang 	{0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2694a34df19aSLydia Wang 	{0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2695a34df19aSLydia Wang 	{0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2696a34df19aSLydia Wang 	{0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2697a34df19aSLydia Wang 	{0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
269869e52a80SHarald Welte 	{ }
269969e52a80SHarald Welte };
270069e52a80SHarald Welte 
270117314379SLydia Wang static int via_pcm_open_close(struct hda_pcm_stream *hinfo,
270217314379SLydia Wang 			      struct hda_codec *codec,
270317314379SLydia Wang 			      struct snd_pcm_substream *substream)
270417314379SLydia Wang {
270517314379SLydia Wang 	int idle = substream->pstr->substream_opened == 1
270617314379SLydia Wang 		&& substream->ref_count == 0;
270717314379SLydia Wang 
270817314379SLydia Wang 	analog_low_current_mode(codec, idle);
270917314379SLydia Wang 	return 0;
271017314379SLydia Wang }
271117314379SLydia Wang 
2712f7278fd0SJosepch Chan static struct hda_pcm_stream vt1708B_8ch_pcm_analog_playback = {
27130aa62aefSHarald Welte 	.substreams = 2,
2714f7278fd0SJosepch Chan 	.channels_min = 2,
2715f7278fd0SJosepch Chan 	.channels_max = 8,
2716f7278fd0SJosepch Chan 	.nid = 0x10, /* NID to query formats and rates */
2717f7278fd0SJosepch Chan 	.ops = {
2718f7278fd0SJosepch Chan 		.open = via_playback_pcm_open,
27190aa62aefSHarald Welte 		.prepare = via_playback_multi_pcm_prepare,
272017314379SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup,
272117314379SLydia Wang 		.close = via_pcm_open_close
2722f7278fd0SJosepch Chan 	},
2723f7278fd0SJosepch Chan };
2724f7278fd0SJosepch Chan 
2725f7278fd0SJosepch Chan static struct hda_pcm_stream vt1708B_4ch_pcm_analog_playback = {
27260aa62aefSHarald Welte 	.substreams = 2,
2727f7278fd0SJosepch Chan 	.channels_min = 2,
2728f7278fd0SJosepch Chan 	.channels_max = 4,
2729f7278fd0SJosepch Chan 	.nid = 0x10, /* NID to query formats and rates */
2730f7278fd0SJosepch Chan 	.ops = {
2731f7278fd0SJosepch Chan 		.open = via_playback_pcm_open,
27320aa62aefSHarald Welte 		.prepare = via_playback_multi_pcm_prepare,
27330aa62aefSHarald Welte 		.cleanup = via_playback_multi_pcm_cleanup
2734f7278fd0SJosepch Chan 	},
2735f7278fd0SJosepch Chan };
2736f7278fd0SJosepch Chan 
2737f7278fd0SJosepch Chan static struct hda_pcm_stream vt1708B_pcm_analog_capture = {
2738f7278fd0SJosepch Chan 	.substreams = 2,
2739f7278fd0SJosepch Chan 	.channels_min = 2,
2740f7278fd0SJosepch Chan 	.channels_max = 2,
2741f7278fd0SJosepch Chan 	.nid = 0x13, /* NID to query formats and rates */
2742f7278fd0SJosepch Chan 	.ops = {
274317314379SLydia Wang 		.open = via_pcm_open_close,
2744f7278fd0SJosepch Chan 		.prepare = via_capture_pcm_prepare,
274517314379SLydia Wang 		.cleanup = via_capture_pcm_cleanup,
274617314379SLydia Wang 		.close = via_pcm_open_close
2747f7278fd0SJosepch Chan 	},
2748f7278fd0SJosepch Chan };
2749f7278fd0SJosepch Chan 
2750f7278fd0SJosepch Chan static struct hda_pcm_stream vt1708B_pcm_digital_playback = {
2751f7278fd0SJosepch Chan 	.substreams = 1,
2752f7278fd0SJosepch Chan 	.channels_min = 2,
2753f7278fd0SJosepch Chan 	.channels_max = 2,
2754f7278fd0SJosepch Chan 	/* NID is set in via_build_pcms */
2755f7278fd0SJosepch Chan 	.ops = {
2756f7278fd0SJosepch Chan 		.open = via_dig_playback_pcm_open,
2757f7278fd0SJosepch Chan 		.close = via_dig_playback_pcm_close,
27589da29271STakashi Iwai 		.prepare = via_dig_playback_pcm_prepare,
27599da29271STakashi Iwai 		.cleanup = via_dig_playback_pcm_cleanup
2760f7278fd0SJosepch Chan 	},
2761f7278fd0SJosepch Chan };
2762f7278fd0SJosepch Chan 
2763f7278fd0SJosepch Chan static struct hda_pcm_stream vt1708B_pcm_digital_capture = {
2764f7278fd0SJosepch Chan 	.substreams = 1,
2765f7278fd0SJosepch Chan 	.channels_min = 2,
2766f7278fd0SJosepch Chan 	.channels_max = 2,
2767f7278fd0SJosepch Chan };
2768f7278fd0SJosepch Chan 
2769f7278fd0SJosepch Chan /* fill in the dac_nids table from the parsed pin configuration */
2770f7278fd0SJosepch Chan static int vt1708B_auto_fill_dac_nids(struct via_spec *spec,
2771f7278fd0SJosepch Chan 				     const struct auto_pin_cfg *cfg)
2772f7278fd0SJosepch Chan {
2773f7278fd0SJosepch Chan 	int i;
2774f7278fd0SJosepch Chan 	hda_nid_t nid;
2775f7278fd0SJosepch Chan 
2776f7278fd0SJosepch Chan 	spec->multiout.num_dacs = cfg->line_outs;
2777f7278fd0SJosepch Chan 
2778f7278fd0SJosepch Chan 	spec->multiout.dac_nids = spec->private_dac_nids;
2779f7278fd0SJosepch Chan 
2780f7278fd0SJosepch Chan 	for (i = 0; i < 4; i++) {
2781f7278fd0SJosepch Chan 		nid = cfg->line_out_pins[i];
2782f7278fd0SJosepch Chan 		if (nid) {
2783f7278fd0SJosepch Chan 			/* config dac list */
2784f7278fd0SJosepch Chan 			switch (i) {
2785f7278fd0SJosepch Chan 			case AUTO_SEQ_FRONT:
2786f7278fd0SJosepch Chan 				spec->multiout.dac_nids[i] = 0x10;
2787f7278fd0SJosepch Chan 				break;
2788f7278fd0SJosepch Chan 			case AUTO_SEQ_CENLFE:
2789f7278fd0SJosepch Chan 				spec->multiout.dac_nids[i] = 0x24;
2790f7278fd0SJosepch Chan 				break;
2791f7278fd0SJosepch Chan 			case AUTO_SEQ_SURROUND:
2792fb4cb772SHarald Welte 				spec->multiout.dac_nids[i] = 0x11;
2793f7278fd0SJosepch Chan 				break;
2794f7278fd0SJosepch Chan 			case AUTO_SEQ_SIDE:
2795fb4cb772SHarald Welte 				spec->multiout.dac_nids[i] = 0x25;
2796f7278fd0SJosepch Chan 				break;
2797f7278fd0SJosepch Chan 			}
2798f7278fd0SJosepch Chan 		}
2799f7278fd0SJosepch Chan 	}
2800f7278fd0SJosepch Chan 
2801f7278fd0SJosepch Chan 	return 0;
2802f7278fd0SJosepch Chan }
2803f7278fd0SJosepch Chan 
2804f7278fd0SJosepch Chan /* add playback controls from the parsed DAC table */
2805f7278fd0SJosepch Chan static int vt1708B_auto_create_multi_out_ctls(struct via_spec *spec,
2806f7278fd0SJosepch Chan 					     const struct auto_pin_cfg *cfg)
2807f7278fd0SJosepch Chan {
2808f7278fd0SJosepch Chan 	char name[32];
2809f7278fd0SJosepch Chan 	static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
2810fb4cb772SHarald Welte 	hda_nid_t nid_vols[] = {0x16, 0x18, 0x26, 0x27};
2811f7278fd0SJosepch Chan 	hda_nid_t nid, nid_vol = 0;
2812f7278fd0SJosepch Chan 	int i, err;
2813f7278fd0SJosepch Chan 
2814f7278fd0SJosepch Chan 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
2815f7278fd0SJosepch Chan 		nid = cfg->line_out_pins[i];
2816f7278fd0SJosepch Chan 
2817f7278fd0SJosepch Chan 		if (!nid)
2818f7278fd0SJosepch Chan 			continue;
2819f7278fd0SJosepch Chan 
2820f7278fd0SJosepch Chan 		nid_vol = nid_vols[i];
2821f7278fd0SJosepch Chan 
2822f7278fd0SJosepch Chan 		if (i == AUTO_SEQ_CENLFE) {
2823f7278fd0SJosepch Chan 			/* Center/LFE */
2824f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2825f7278fd0SJosepch Chan 					      "Center Playback Volume",
2826f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2827f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2828f7278fd0SJosepch Chan 			if (err < 0)
2829f7278fd0SJosepch Chan 				return err;
2830f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2831f7278fd0SJosepch Chan 					      "LFE Playback Volume",
2832f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2833f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2834f7278fd0SJosepch Chan 			if (err < 0)
2835f7278fd0SJosepch Chan 				return err;
2836f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2837f7278fd0SJosepch Chan 					      "Center Playback Switch",
2838f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2839f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2840f7278fd0SJosepch Chan 			if (err < 0)
2841f7278fd0SJosepch Chan 				return err;
2842f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2843f7278fd0SJosepch Chan 					      "LFE Playback Switch",
2844f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2845f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2846f7278fd0SJosepch Chan 			if (err < 0)
2847f7278fd0SJosepch Chan 				return err;
2848f7278fd0SJosepch Chan 		} else if (i == AUTO_SEQ_FRONT) {
2849f7278fd0SJosepch Chan 			/* add control to mixer index 0 */
2850f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2851f7278fd0SJosepch Chan 					      "Master Front Playback Volume",
2852f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2853f7278fd0SJosepch Chan 								  HDA_INPUT));
2854f7278fd0SJosepch Chan 			if (err < 0)
2855f7278fd0SJosepch Chan 				return err;
2856f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2857f7278fd0SJosepch Chan 					      "Master Front Playback Switch",
2858f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2859f7278fd0SJosepch Chan 								  HDA_INPUT));
2860f7278fd0SJosepch Chan 			if (err < 0)
2861f7278fd0SJosepch Chan 				return err;
2862f7278fd0SJosepch Chan 
2863f7278fd0SJosepch Chan 			/* add control to PW3 */
2864f7278fd0SJosepch Chan 			sprintf(name, "%s Playback Volume", chname[i]);
2865f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2866f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2867f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2868f7278fd0SJosepch Chan 			if (err < 0)
2869f7278fd0SJosepch Chan 				return err;
2870f7278fd0SJosepch Chan 			sprintf(name, "%s Playback Switch", chname[i]);
2871f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2872f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2873f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2874f7278fd0SJosepch Chan 			if (err < 0)
2875f7278fd0SJosepch Chan 				return err;
2876f7278fd0SJosepch Chan 		} else {
2877f7278fd0SJosepch Chan 			sprintf(name, "%s Playback Volume", chname[i]);
2878f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2879f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2880f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2881f7278fd0SJosepch Chan 			if (err < 0)
2882f7278fd0SJosepch Chan 				return err;
2883f7278fd0SJosepch Chan 			sprintf(name, "%s Playback Switch", chname[i]);
2884f7278fd0SJosepch Chan 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2885f7278fd0SJosepch Chan 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2886f7278fd0SJosepch Chan 								  HDA_OUTPUT));
2887f7278fd0SJosepch Chan 			if (err < 0)
2888f7278fd0SJosepch Chan 				return err;
2889f7278fd0SJosepch Chan 		}
2890f7278fd0SJosepch Chan 	}
2891f7278fd0SJosepch Chan 
2892f7278fd0SJosepch Chan 	return 0;
2893f7278fd0SJosepch Chan }
2894f7278fd0SJosepch Chan 
2895f7278fd0SJosepch Chan static int vt1708B_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
2896f7278fd0SJosepch Chan {
2897f7278fd0SJosepch Chan 	int err;
2898f7278fd0SJosepch Chan 
2899f7278fd0SJosepch Chan 	if (!pin)
2900f7278fd0SJosepch Chan 		return 0;
2901f7278fd0SJosepch Chan 
2902f7278fd0SJosepch Chan 	spec->multiout.hp_nid = VT1708B_HP_NID; /* AOW3 */
2903cdc1784dSLydia Wang 	spec->hp_independent_mode_index = 1;
2904f7278fd0SJosepch Chan 
2905f7278fd0SJosepch Chan 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2906f7278fd0SJosepch Chan 			      "Headphone Playback Volume",
2907f7278fd0SJosepch Chan 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2908f7278fd0SJosepch Chan 	if (err < 0)
2909f7278fd0SJosepch Chan 		return err;
2910f7278fd0SJosepch Chan 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2911f7278fd0SJosepch Chan 			      "Headphone Playback Switch",
2912f7278fd0SJosepch Chan 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2913f7278fd0SJosepch Chan 	if (err < 0)
2914f7278fd0SJosepch Chan 		return err;
2915f7278fd0SJosepch Chan 
29160aa62aefSHarald Welte 	create_hp_imux(spec);
29170aa62aefSHarald Welte 
2918f7278fd0SJosepch Chan 	return 0;
2919f7278fd0SJosepch Chan }
2920f7278fd0SJosepch Chan 
2921f7278fd0SJosepch Chan /* create playback/capture controls for input pins */
2922f7278fd0SJosepch Chan static int vt1708B_auto_create_analog_input_ctls(struct via_spec *spec,
2923f7278fd0SJosepch Chan 						const struct auto_pin_cfg *cfg)
2924f7278fd0SJosepch Chan {
2925f7278fd0SJosepch Chan 	static char *labels[] = {
2926f7278fd0SJosepch Chan 		"Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
2927f7278fd0SJosepch Chan 	};
29280aa62aefSHarald Welte 	struct hda_input_mux *imux = &spec->private_imux[0];
2929f7278fd0SJosepch Chan 	int i, err, idx = 0;
2930f7278fd0SJosepch Chan 
2931f7278fd0SJosepch Chan 	/* for internal loopback recording select */
2932f7278fd0SJosepch Chan 	imux->items[imux->num_items].label = "Stereo Mixer";
2933f7278fd0SJosepch Chan 	imux->items[imux->num_items].index = idx;
2934f7278fd0SJosepch Chan 	imux->num_items++;
2935f7278fd0SJosepch Chan 
2936f7278fd0SJosepch Chan 	for (i = 0; i < AUTO_PIN_LAST; i++) {
2937f7278fd0SJosepch Chan 		if (!cfg->input_pins[i])
2938f7278fd0SJosepch Chan 			continue;
2939f7278fd0SJosepch Chan 
2940f7278fd0SJosepch Chan 		switch (cfg->input_pins[i]) {
2941f7278fd0SJosepch Chan 		case 0x1a: /* Mic */
2942f7278fd0SJosepch Chan 			idx = 2;
2943f7278fd0SJosepch Chan 			break;
2944f7278fd0SJosepch Chan 
2945f7278fd0SJosepch Chan 		case 0x1b: /* Line In */
2946f7278fd0SJosepch Chan 			idx = 3;
2947f7278fd0SJosepch Chan 			break;
2948f7278fd0SJosepch Chan 
2949f7278fd0SJosepch Chan 		case 0x1e: /* Front Mic */
2950f7278fd0SJosepch Chan 			idx = 4;
2951f7278fd0SJosepch Chan 			break;
2952f7278fd0SJosepch Chan 
2953f7278fd0SJosepch Chan 		case 0x1f: /* CD */
2954f7278fd0SJosepch Chan 			idx = 1;
2955f7278fd0SJosepch Chan 			break;
2956f7278fd0SJosepch Chan 		}
29579510e8ddSLydia Wang 		err = via_new_analog_input(spec, labels[i], idx, 0x16);
2958f7278fd0SJosepch Chan 		if (err < 0)
2959f7278fd0SJosepch Chan 			return err;
2960f7278fd0SJosepch Chan 		imux->items[imux->num_items].label = labels[i];
2961f7278fd0SJosepch Chan 		imux->items[imux->num_items].index = idx;
2962f7278fd0SJosepch Chan 		imux->num_items++;
2963f7278fd0SJosepch Chan 	}
2964f7278fd0SJosepch Chan 	return 0;
2965f7278fd0SJosepch Chan }
2966f7278fd0SJosepch Chan 
2967f7278fd0SJosepch Chan static int vt1708B_parse_auto_config(struct hda_codec *codec)
2968f7278fd0SJosepch Chan {
2969f7278fd0SJosepch Chan 	struct via_spec *spec = codec->spec;
2970f7278fd0SJosepch Chan 	int err;
2971f7278fd0SJosepch Chan 
2972f7278fd0SJosepch Chan 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2973f7278fd0SJosepch Chan 	if (err < 0)
2974f7278fd0SJosepch Chan 		return err;
2975f7278fd0SJosepch Chan 	err = vt1708B_auto_fill_dac_nids(spec, &spec->autocfg);
2976f7278fd0SJosepch Chan 	if (err < 0)
2977f7278fd0SJosepch Chan 		return err;
2978f7278fd0SJosepch Chan 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2979f7278fd0SJosepch Chan 		return 0; /* can't find valid BIOS pin config */
2980f7278fd0SJosepch Chan 
2981f7278fd0SJosepch Chan 	err = vt1708B_auto_create_multi_out_ctls(spec, &spec->autocfg);
2982f7278fd0SJosepch Chan 	if (err < 0)
2983f7278fd0SJosepch Chan 		return err;
2984f7278fd0SJosepch Chan 	err = vt1708B_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2985f7278fd0SJosepch Chan 	if (err < 0)
2986f7278fd0SJosepch Chan 		return err;
2987f7278fd0SJosepch Chan 	err = vt1708B_auto_create_analog_input_ctls(spec, &spec->autocfg);
2988f7278fd0SJosepch Chan 	if (err < 0)
2989f7278fd0SJosepch Chan 		return err;
2990f7278fd0SJosepch Chan 
2991f7278fd0SJosepch Chan 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2992f7278fd0SJosepch Chan 
29930852d7a6STakashi Iwai 	if (spec->autocfg.dig_outs)
2994f7278fd0SJosepch Chan 		spec->multiout.dig_out_nid = VT1708B_DIGOUT_NID;
299555d1d6c1STakashi Iwai 	spec->dig_in_pin = VT1708B_DIGIN_PIN;
2996f7278fd0SJosepch Chan 	if (spec->autocfg.dig_in_pin)
2997f7278fd0SJosepch Chan 		spec->dig_in_nid = VT1708B_DIGIN_NID;
2998f7278fd0SJosepch Chan 
2999603c4019STakashi Iwai 	if (spec->kctls.list)
3000603c4019STakashi Iwai 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
3001f7278fd0SJosepch Chan 
30020aa62aefSHarald Welte 	spec->input_mux = &spec->private_imux[0];
30030aa62aefSHarald Welte 
3004f8fdd495SHarald Welte 	if (spec->hp_mux)
30050aa62aefSHarald Welte 		spec->mixers[spec->num_mixers++] = via_hp_mixer;
3006f7278fd0SJosepch Chan 
30071564b287SLydia Wang 	spec->mixers[spec->num_mixers++] = via_smart51_mixer;
3008f7278fd0SJosepch Chan 	return 1;
3009f7278fd0SJosepch Chan }
3010f7278fd0SJosepch Chan 
3011f7278fd0SJosepch Chan #ifdef CONFIG_SND_HDA_POWER_SAVE
3012f7278fd0SJosepch Chan static struct hda_amp_list vt1708B_loopbacks[] = {
3013f7278fd0SJosepch Chan 	{ 0x16, HDA_INPUT, 1 },
3014f7278fd0SJosepch Chan 	{ 0x16, HDA_INPUT, 2 },
3015f7278fd0SJosepch Chan 	{ 0x16, HDA_INPUT, 3 },
3016f7278fd0SJosepch Chan 	{ 0x16, HDA_INPUT, 4 },
3017f7278fd0SJosepch Chan 	{ } /* end */
3018f7278fd0SJosepch Chan };
3019f7278fd0SJosepch Chan #endif
3020518bf3baSLydia Wang static int patch_vt1708S(struct hda_codec *codec);
3021f7278fd0SJosepch Chan static int patch_vt1708B_8ch(struct hda_codec *codec)
3022f7278fd0SJosepch Chan {
3023f7278fd0SJosepch Chan 	struct via_spec *spec;
3024f7278fd0SJosepch Chan 	int err;
3025f7278fd0SJosepch Chan 
3026518bf3baSLydia Wang 	if (get_codec_type(codec) == VT1708BCE)
3027518bf3baSLydia Wang 		return patch_vt1708S(codec);
3028f7278fd0SJosepch Chan 	/* create a codec specific record */
3029eb14a46cSHarald Welte 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3030f7278fd0SJosepch Chan 	if (spec == NULL)
3031f7278fd0SJosepch Chan 		return -ENOMEM;
3032f7278fd0SJosepch Chan 
3033f7278fd0SJosepch Chan 	codec->spec = spec;
3034f7278fd0SJosepch Chan 
3035f7278fd0SJosepch Chan 	/* automatic parse from the BIOS config */
3036f7278fd0SJosepch Chan 	err = vt1708B_parse_auto_config(codec);
3037f7278fd0SJosepch Chan 	if (err < 0) {
3038f7278fd0SJosepch Chan 		via_free(codec);
3039f7278fd0SJosepch Chan 		return err;
3040f7278fd0SJosepch Chan 	} else if (!err) {
3041f7278fd0SJosepch Chan 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
3042f7278fd0SJosepch Chan 		       "from BIOS.  Using genenic mode...\n");
3043f7278fd0SJosepch Chan 	}
3044f7278fd0SJosepch Chan 
304569e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1708B_8ch_volume_init_verbs;
304669e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
3047f7278fd0SJosepch Chan 
3048f7278fd0SJosepch Chan 	spec->stream_name_analog = "VT1708B Analog";
3049f7278fd0SJosepch Chan 	spec->stream_analog_playback = &vt1708B_8ch_pcm_analog_playback;
3050f7278fd0SJosepch Chan 	spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
3051f7278fd0SJosepch Chan 
3052f7278fd0SJosepch Chan 	spec->stream_name_digital = "VT1708B Digital";
3053f7278fd0SJosepch Chan 	spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
3054f7278fd0SJosepch Chan 	spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
3055f7278fd0SJosepch Chan 
3056f7278fd0SJosepch Chan 	if (!spec->adc_nids && spec->input_mux) {
3057f7278fd0SJosepch Chan 		spec->adc_nids = vt1708B_adc_nids;
3058f7278fd0SJosepch Chan 		spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
3059337b9d02STakashi Iwai 		get_mux_nids(codec);
3060f7278fd0SJosepch Chan 		spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
3061f7278fd0SJosepch Chan 		spec->num_mixers++;
3062f7278fd0SJosepch Chan 	}
3063f7278fd0SJosepch Chan 
3064f7278fd0SJosepch Chan 	codec->patch_ops = via_patch_ops;
3065f7278fd0SJosepch Chan 
3066f7278fd0SJosepch Chan 	codec->patch_ops.init = via_auto_init;
306769e52a80SHarald Welte 	codec->patch_ops.unsol_event = via_unsol_event;
3068f7278fd0SJosepch Chan #ifdef CONFIG_SND_HDA_POWER_SAVE
3069f7278fd0SJosepch Chan 	spec->loopback.amplist = vt1708B_loopbacks;
3070f7278fd0SJosepch Chan #endif
3071f7278fd0SJosepch Chan 
3072f7278fd0SJosepch Chan 	return 0;
3073f7278fd0SJosepch Chan }
3074f7278fd0SJosepch Chan 
3075f7278fd0SJosepch Chan static int patch_vt1708B_4ch(struct hda_codec *codec)
3076f7278fd0SJosepch Chan {
3077f7278fd0SJosepch Chan 	struct via_spec *spec;
3078f7278fd0SJosepch Chan 	int err;
3079f7278fd0SJosepch Chan 
3080f7278fd0SJosepch Chan 	/* create a codec specific record */
3081eb14a46cSHarald Welte 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3082f7278fd0SJosepch Chan 	if (spec == NULL)
3083f7278fd0SJosepch Chan 		return -ENOMEM;
3084f7278fd0SJosepch Chan 
3085f7278fd0SJosepch Chan 	codec->spec = spec;
3086f7278fd0SJosepch Chan 
3087f7278fd0SJosepch Chan 	/* automatic parse from the BIOS config */
3088f7278fd0SJosepch Chan 	err = vt1708B_parse_auto_config(codec);
3089f7278fd0SJosepch Chan 	if (err < 0) {
3090f7278fd0SJosepch Chan 		via_free(codec);
3091f7278fd0SJosepch Chan 		return err;
3092f7278fd0SJosepch Chan 	} else if (!err) {
3093f7278fd0SJosepch Chan 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
3094f7278fd0SJosepch Chan 		       "from BIOS.  Using genenic mode...\n");
3095f7278fd0SJosepch Chan 	}
3096f7278fd0SJosepch Chan 
309769e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1708B_4ch_volume_init_verbs;
309869e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
3099f7278fd0SJosepch Chan 
3100f7278fd0SJosepch Chan 	spec->stream_name_analog = "VT1708B Analog";
3101f7278fd0SJosepch Chan 	spec->stream_analog_playback = &vt1708B_4ch_pcm_analog_playback;
3102f7278fd0SJosepch Chan 	spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
3103f7278fd0SJosepch Chan 
3104f7278fd0SJosepch Chan 	spec->stream_name_digital = "VT1708B Digital";
3105f7278fd0SJosepch Chan 	spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
3106f7278fd0SJosepch Chan 	spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
3107f7278fd0SJosepch Chan 
3108f7278fd0SJosepch Chan 	if (!spec->adc_nids && spec->input_mux) {
3109f7278fd0SJosepch Chan 		spec->adc_nids = vt1708B_adc_nids;
3110f7278fd0SJosepch Chan 		spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
3111337b9d02STakashi Iwai 		get_mux_nids(codec);
3112f7278fd0SJosepch Chan 		spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
3113f7278fd0SJosepch Chan 		spec->num_mixers++;
3114f7278fd0SJosepch Chan 	}
3115f7278fd0SJosepch Chan 
3116f7278fd0SJosepch Chan 	codec->patch_ops = via_patch_ops;
3117f7278fd0SJosepch Chan 
3118f7278fd0SJosepch Chan 	codec->patch_ops.init = via_auto_init;
311969e52a80SHarald Welte 	codec->patch_ops.unsol_event = via_unsol_event;
3120f7278fd0SJosepch Chan #ifdef CONFIG_SND_HDA_POWER_SAVE
3121f7278fd0SJosepch Chan 	spec->loopback.amplist = vt1708B_loopbacks;
3122f7278fd0SJosepch Chan #endif
3123c577b8a1SJoseph Chan 
3124c577b8a1SJoseph Chan 	return 0;
3125c577b8a1SJoseph Chan }
3126c577b8a1SJoseph Chan 
3127d949cac1SHarald Welte /* Patch for VT1708S */
3128d949cac1SHarald Welte 
3129d949cac1SHarald Welte /* capture mixer elements */
3130d949cac1SHarald Welte static struct snd_kcontrol_new vt1708S_capture_mixer[] = {
3131d949cac1SHarald Welte 	HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
3132d949cac1SHarald Welte 	HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
3133d949cac1SHarald Welte 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
3134d949cac1SHarald Welte 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
31356369bcfcSLydia Wang 	HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x1A, 0x0, HDA_INPUT),
31366369bcfcSLydia Wang 	HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x1E, 0x0,
31376369bcfcSLydia Wang 			 HDA_INPUT),
3138d949cac1SHarald Welte 	{
3139d949cac1SHarald Welte 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3140d949cac1SHarald Welte 		/* The multiple "Capture Source" controls confuse alsamixer
3141d949cac1SHarald Welte 		 * So call somewhat different..
3142d949cac1SHarald Welte 		 */
3143d949cac1SHarald Welte 		/* .name = "Capture Source", */
3144d949cac1SHarald Welte 		.name = "Input Source",
3145d949cac1SHarald Welte 		.count = 1,
3146d949cac1SHarald Welte 		.info = via_mux_enum_info,
3147d949cac1SHarald Welte 		.get = via_mux_enum_get,
3148d949cac1SHarald Welte 		.put = via_mux_enum_put,
3149d949cac1SHarald Welte 	},
3150d949cac1SHarald Welte 	{ } /* end */
3151d949cac1SHarald Welte };
3152d949cac1SHarald Welte 
3153d949cac1SHarald Welte static struct hda_verb vt1708S_volume_init_verbs[] = {
3154d949cac1SHarald Welte 	/* Unmute ADC0-1 and set the default input to mic-in */
3155d949cac1SHarald Welte 	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3156d949cac1SHarald Welte 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3157d949cac1SHarald Welte 
3158d949cac1SHarald Welte 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the
3159d949cac1SHarald Welte 	 * analog-loopback mixer widget */
3160d949cac1SHarald Welte 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3161d949cac1SHarald Welte 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3162d949cac1SHarald Welte 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3163d949cac1SHarald Welte 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3164d949cac1SHarald Welte 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3165d949cac1SHarald Welte 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
3166d949cac1SHarald Welte 
3167d949cac1SHarald Welte 	/* Setup default input of PW4 to MW0 */
3168d949cac1SHarald Welte 	{0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
31695691ec7fSHarald Welte 	/* PW9, PW10  Output enable */
3170d949cac1SHarald Welte 	{0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
31715691ec7fSHarald Welte 	{0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3172d7426329SHarald Welte 	/* Enable Mic Boost Volume backdoor */
3173d7426329SHarald Welte 	{0x1, 0xf98, 0x1},
3174bc7e7e5cSLydia Wang 	/* don't bybass mixer */
3175bc7e7e5cSLydia Wang 	{0x1, 0xf88, 0xc0},
3176d949cac1SHarald Welte 	{ }
3177d949cac1SHarald Welte };
3178d949cac1SHarald Welte 
317969e52a80SHarald Welte static struct hda_verb vt1708S_uniwill_init_verbs[] = {
3180a34df19aSLydia Wang 	{0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
3181a34df19aSLydia Wang 	 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3182a34df19aSLydia Wang 	{0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3183a34df19aSLydia Wang 	{0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3184a34df19aSLydia Wang 	{0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3185a34df19aSLydia Wang 	{0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3186a34df19aSLydia Wang 	{0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3187a34df19aSLydia Wang 	{0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3188a34df19aSLydia Wang 	{0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
318969e52a80SHarald Welte 	{ }
319069e52a80SHarald Welte };
319169e52a80SHarald Welte 
3192d949cac1SHarald Welte static struct hda_pcm_stream vt1708S_pcm_analog_playback = {
3193d949cac1SHarald Welte 	.substreams = 2,
3194d949cac1SHarald Welte 	.channels_min = 2,
3195d949cac1SHarald Welte 	.channels_max = 8,
3196d949cac1SHarald Welte 	.nid = 0x10, /* NID to query formats and rates */
3197d949cac1SHarald Welte 	.ops = {
3198d949cac1SHarald Welte 		.open = via_playback_pcm_open,
3199c873cc25SLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
3200c873cc25SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup,
320117314379SLydia Wang 		.close = via_pcm_open_close
3202d949cac1SHarald Welte 	},
3203d949cac1SHarald Welte };
3204d949cac1SHarald Welte 
3205d949cac1SHarald Welte static struct hda_pcm_stream vt1708S_pcm_analog_capture = {
3206d949cac1SHarald Welte 	.substreams = 2,
3207d949cac1SHarald Welte 	.channels_min = 2,
3208d949cac1SHarald Welte 	.channels_max = 2,
3209d949cac1SHarald Welte 	.nid = 0x13, /* NID to query formats and rates */
3210d949cac1SHarald Welte 	.ops = {
321117314379SLydia Wang 		.open = via_pcm_open_close,
3212d949cac1SHarald Welte 		.prepare = via_capture_pcm_prepare,
321317314379SLydia Wang 		.cleanup = via_capture_pcm_cleanup,
321417314379SLydia Wang 		.close = via_pcm_open_close
3215d949cac1SHarald Welte 	},
3216d949cac1SHarald Welte };
3217d949cac1SHarald Welte 
3218d949cac1SHarald Welte static struct hda_pcm_stream vt1708S_pcm_digital_playback = {
32199da29271STakashi Iwai 	.substreams = 1,
3220d949cac1SHarald Welte 	.channels_min = 2,
3221d949cac1SHarald Welte 	.channels_max = 2,
3222d949cac1SHarald Welte 	/* NID is set in via_build_pcms */
3223d949cac1SHarald Welte 	.ops = {
3224d949cac1SHarald Welte 		.open = via_dig_playback_pcm_open,
3225d949cac1SHarald Welte 		.close = via_dig_playback_pcm_close,
32269da29271STakashi Iwai 		.prepare = via_dig_playback_pcm_prepare,
32279da29271STakashi Iwai 		.cleanup = via_dig_playback_pcm_cleanup
3228d949cac1SHarald Welte 	},
3229d949cac1SHarald Welte };
3230d949cac1SHarald Welte 
3231d949cac1SHarald Welte /* fill in the dac_nids table from the parsed pin configuration */
3232d949cac1SHarald Welte static int vt1708S_auto_fill_dac_nids(struct via_spec *spec,
3233d949cac1SHarald Welte 				     const struct auto_pin_cfg *cfg)
3234d949cac1SHarald Welte {
3235d949cac1SHarald Welte 	int i;
3236d949cac1SHarald Welte 	hda_nid_t nid;
3237d949cac1SHarald Welte 
3238d949cac1SHarald Welte 	spec->multiout.num_dacs = cfg->line_outs;
3239d949cac1SHarald Welte 
3240d949cac1SHarald Welte 	spec->multiout.dac_nids = spec->private_dac_nids;
3241d949cac1SHarald Welte 
3242d949cac1SHarald Welte 	for (i = 0; i < 4; i++) {
3243d949cac1SHarald Welte 		nid = cfg->line_out_pins[i];
3244d949cac1SHarald Welte 		if (nid) {
3245d949cac1SHarald Welte 			/* config dac list */
3246d949cac1SHarald Welte 			switch (i) {
3247d949cac1SHarald Welte 			case AUTO_SEQ_FRONT:
3248d949cac1SHarald Welte 				spec->multiout.dac_nids[i] = 0x10;
3249d949cac1SHarald Welte 				break;
3250d949cac1SHarald Welte 			case AUTO_SEQ_CENLFE:
3251d949cac1SHarald Welte 				spec->multiout.dac_nids[i] = 0x24;
3252d949cac1SHarald Welte 				break;
3253d949cac1SHarald Welte 			case AUTO_SEQ_SURROUND:
3254d949cac1SHarald Welte 				spec->multiout.dac_nids[i] = 0x11;
3255d949cac1SHarald Welte 				break;
3256d949cac1SHarald Welte 			case AUTO_SEQ_SIDE:
3257d949cac1SHarald Welte 				spec->multiout.dac_nids[i] = 0x25;
3258d949cac1SHarald Welte 				break;
3259d949cac1SHarald Welte 			}
3260d949cac1SHarald Welte 		}
3261d949cac1SHarald Welte 	}
3262d949cac1SHarald Welte 
3263d949cac1SHarald Welte 	return 0;
3264d949cac1SHarald Welte }
3265d949cac1SHarald Welte 
3266d949cac1SHarald Welte /* add playback controls from the parsed DAC table */
3267d949cac1SHarald Welte static int vt1708S_auto_create_multi_out_ctls(struct via_spec *spec,
3268d949cac1SHarald Welte 					     const struct auto_pin_cfg *cfg)
3269d949cac1SHarald Welte {
3270d949cac1SHarald Welte 	char name[32];
3271d949cac1SHarald Welte 	static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
3272d949cac1SHarald Welte 	hda_nid_t nid_vols[] = {0x10, 0x11, 0x24, 0x25};
3273d949cac1SHarald Welte 	hda_nid_t nid_mutes[] = {0x1C, 0x18, 0x26, 0x27};
3274d949cac1SHarald Welte 	hda_nid_t nid, nid_vol, nid_mute;
3275d949cac1SHarald Welte 	int i, err;
3276d949cac1SHarald Welte 
3277d949cac1SHarald Welte 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
3278d949cac1SHarald Welte 		nid = cfg->line_out_pins[i];
3279d949cac1SHarald Welte 
3280d949cac1SHarald Welte 		if (!nid)
3281d949cac1SHarald Welte 			continue;
3282d949cac1SHarald Welte 
3283d949cac1SHarald Welte 		nid_vol = nid_vols[i];
3284d949cac1SHarald Welte 		nid_mute = nid_mutes[i];
3285d949cac1SHarald Welte 
3286d949cac1SHarald Welte 		if (i == AUTO_SEQ_CENLFE) {
3287d949cac1SHarald Welte 			/* Center/LFE */
3288d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3289d949cac1SHarald Welte 					      "Center Playback Volume",
3290d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
3291d949cac1SHarald Welte 								  HDA_OUTPUT));
3292d949cac1SHarald Welte 			if (err < 0)
3293d949cac1SHarald Welte 				return err;
3294d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3295d949cac1SHarald Welte 					      "LFE Playback Volume",
3296d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
3297d949cac1SHarald Welte 								  HDA_OUTPUT));
3298d949cac1SHarald Welte 			if (err < 0)
3299d949cac1SHarald Welte 				return err;
3300d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3301d949cac1SHarald Welte 					      "Center Playback Switch",
3302d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(nid_mute,
3303d949cac1SHarald Welte 								  1, 0,
3304d949cac1SHarald Welte 								  HDA_OUTPUT));
3305d949cac1SHarald Welte 			if (err < 0)
3306d949cac1SHarald Welte 				return err;
3307d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3308d949cac1SHarald Welte 					      "LFE Playback Switch",
3309d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(nid_mute,
3310d949cac1SHarald Welte 								  2, 0,
3311d949cac1SHarald Welte 								  HDA_OUTPUT));
3312d949cac1SHarald Welte 			if (err < 0)
3313d949cac1SHarald Welte 				return err;
3314d949cac1SHarald Welte 		} else if (i == AUTO_SEQ_FRONT) {
3315d949cac1SHarald Welte 			/* add control to mixer index 0 */
3316d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3317d949cac1SHarald Welte 					      "Master Front Playback Volume",
3318d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
3319d949cac1SHarald Welte 								  HDA_INPUT));
3320d949cac1SHarald Welte 			if (err < 0)
3321d949cac1SHarald Welte 				return err;
3322d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3323d949cac1SHarald Welte 					      "Master Front Playback Switch",
3324d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
3325d949cac1SHarald Welte 								  HDA_INPUT));
3326d949cac1SHarald Welte 			if (err < 0)
3327d949cac1SHarald Welte 				return err;
3328d949cac1SHarald Welte 
3329d949cac1SHarald Welte 			/* Front */
3330d949cac1SHarald Welte 			sprintf(name, "%s Playback Volume", chname[i]);
3331d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3332d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3333d949cac1SHarald Welte 								  HDA_OUTPUT));
3334d949cac1SHarald Welte 			if (err < 0)
3335d949cac1SHarald Welte 				return err;
3336d949cac1SHarald Welte 			sprintf(name, "%s Playback Switch", chname[i]);
3337d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3338d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(nid_mute,
3339d949cac1SHarald Welte 								  3, 0,
3340d949cac1SHarald Welte 								  HDA_OUTPUT));
3341d949cac1SHarald Welte 			if (err < 0)
3342d949cac1SHarald Welte 				return err;
3343d949cac1SHarald Welte 		} else {
3344d949cac1SHarald Welte 			sprintf(name, "%s Playback Volume", chname[i]);
3345d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3346d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3347d949cac1SHarald Welte 								  HDA_OUTPUT));
3348d949cac1SHarald Welte 			if (err < 0)
3349d949cac1SHarald Welte 				return err;
3350d949cac1SHarald Welte 			sprintf(name, "%s Playback Switch", chname[i]);
3351d949cac1SHarald Welte 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3352d949cac1SHarald Welte 					      HDA_COMPOSE_AMP_VAL(nid_mute,
3353d949cac1SHarald Welte 								  3, 0,
3354d949cac1SHarald Welte 								  HDA_OUTPUT));
3355d949cac1SHarald Welte 			if (err < 0)
3356d949cac1SHarald Welte 				return err;
3357d949cac1SHarald Welte 		}
3358d949cac1SHarald Welte 	}
3359d949cac1SHarald Welte 
3360d949cac1SHarald Welte 	return 0;
3361d949cac1SHarald Welte }
3362d949cac1SHarald Welte 
3363d949cac1SHarald Welte static int vt1708S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
3364d949cac1SHarald Welte {
3365d949cac1SHarald Welte 	int err;
3366d949cac1SHarald Welte 
3367d949cac1SHarald Welte 	if (!pin)
3368d949cac1SHarald Welte 		return 0;
3369d949cac1SHarald Welte 
3370d949cac1SHarald Welte 	spec->multiout.hp_nid = VT1708S_HP_NID; /* AOW3 */
3371cdc1784dSLydia Wang 	spec->hp_independent_mode_index = 1;
3372d949cac1SHarald Welte 
3373d949cac1SHarald Welte 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3374d949cac1SHarald Welte 			      "Headphone Playback Volume",
3375d949cac1SHarald Welte 			      HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
3376d949cac1SHarald Welte 	if (err < 0)
3377d949cac1SHarald Welte 		return err;
3378d949cac1SHarald Welte 
3379d949cac1SHarald Welte 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3380d949cac1SHarald Welte 			      "Headphone Playback Switch",
3381d949cac1SHarald Welte 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3382d949cac1SHarald Welte 	if (err < 0)
3383d949cac1SHarald Welte 		return err;
3384d949cac1SHarald Welte 
33850aa62aefSHarald Welte 	create_hp_imux(spec);
33860aa62aefSHarald Welte 
3387d949cac1SHarald Welte 	return 0;
3388d949cac1SHarald Welte }
3389d949cac1SHarald Welte 
3390d949cac1SHarald Welte /* create playback/capture controls for input pins */
3391d949cac1SHarald Welte static int vt1708S_auto_create_analog_input_ctls(struct via_spec *spec,
3392d949cac1SHarald Welte 						const struct auto_pin_cfg *cfg)
3393d949cac1SHarald Welte {
3394d949cac1SHarald Welte 	static char *labels[] = {
3395d949cac1SHarald Welte 		"Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
3396d949cac1SHarald Welte 	};
33970aa62aefSHarald Welte 	struct hda_input_mux *imux = &spec->private_imux[0];
3398d949cac1SHarald Welte 	int i, err, idx = 0;
3399d949cac1SHarald Welte 
3400d949cac1SHarald Welte 	/* for internal loopback recording select */
3401d949cac1SHarald Welte 	imux->items[imux->num_items].label = "Stereo Mixer";
3402d949cac1SHarald Welte 	imux->items[imux->num_items].index = 5;
3403d949cac1SHarald Welte 	imux->num_items++;
3404d949cac1SHarald Welte 
3405d949cac1SHarald Welte 	for (i = 0; i < AUTO_PIN_LAST; i++) {
3406d949cac1SHarald Welte 		if (!cfg->input_pins[i])
3407d949cac1SHarald Welte 			continue;
3408d949cac1SHarald Welte 
3409d949cac1SHarald Welte 		switch (cfg->input_pins[i]) {
3410d949cac1SHarald Welte 		case 0x1a: /* Mic */
3411d949cac1SHarald Welte 			idx = 2;
3412d949cac1SHarald Welte 			break;
3413d949cac1SHarald Welte 
3414d949cac1SHarald Welte 		case 0x1b: /* Line In */
3415d949cac1SHarald Welte 			idx = 3;
3416d949cac1SHarald Welte 			break;
3417d949cac1SHarald Welte 
3418d949cac1SHarald Welte 		case 0x1e: /* Front Mic */
3419d949cac1SHarald Welte 			idx = 4;
3420d949cac1SHarald Welte 			break;
3421d949cac1SHarald Welte 
3422d949cac1SHarald Welte 		case 0x1f: /* CD */
3423d949cac1SHarald Welte 			idx = 1;
3424d949cac1SHarald Welte 			break;
3425d949cac1SHarald Welte 		}
34269510e8ddSLydia Wang 		err = via_new_analog_input(spec, labels[i], idx, 0x16);
3427d949cac1SHarald Welte 		if (err < 0)
3428d949cac1SHarald Welte 			return err;
3429d949cac1SHarald Welte 		imux->items[imux->num_items].label = labels[i];
3430d949cac1SHarald Welte 		imux->items[imux->num_items].index = idx-1;
3431d949cac1SHarald Welte 		imux->num_items++;
3432d949cac1SHarald Welte 	}
3433d949cac1SHarald Welte 	return 0;
3434d949cac1SHarald Welte }
3435d949cac1SHarald Welte 
34369da29271STakashi Iwai /* fill out digital output widgets; one for master and one for slave outputs */
34379da29271STakashi Iwai static void fill_dig_outs(struct hda_codec *codec)
34389da29271STakashi Iwai {
34399da29271STakashi Iwai 	struct via_spec *spec = codec->spec;
34409da29271STakashi Iwai 	int i;
34419da29271STakashi Iwai 
34429da29271STakashi Iwai 	for (i = 0; i < spec->autocfg.dig_outs; i++) {
34439da29271STakashi Iwai 		hda_nid_t nid;
34449da29271STakashi Iwai 		int conn;
34459da29271STakashi Iwai 
34469da29271STakashi Iwai 		nid = spec->autocfg.dig_out_pins[i];
34479da29271STakashi Iwai 		if (!nid)
34489da29271STakashi Iwai 			continue;
34499da29271STakashi Iwai 		conn = snd_hda_get_connections(codec, nid, &nid, 1);
34509da29271STakashi Iwai 		if (conn < 1)
34519da29271STakashi Iwai 			continue;
34529da29271STakashi Iwai 		if (!spec->multiout.dig_out_nid)
34539da29271STakashi Iwai 			spec->multiout.dig_out_nid = nid;
34549da29271STakashi Iwai 		else {
34559da29271STakashi Iwai 			spec->slave_dig_outs[0] = nid;
34569da29271STakashi Iwai 			break; /* at most two dig outs */
34579da29271STakashi Iwai 		}
34589da29271STakashi Iwai 	}
34599da29271STakashi Iwai }
34609da29271STakashi Iwai 
3461d949cac1SHarald Welte static int vt1708S_parse_auto_config(struct hda_codec *codec)
3462d949cac1SHarald Welte {
3463d949cac1SHarald Welte 	struct via_spec *spec = codec->spec;
3464d949cac1SHarald Welte 	int err;
3465d949cac1SHarald Welte 
34669da29271STakashi Iwai 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3467d949cac1SHarald Welte 	if (err < 0)
3468d949cac1SHarald Welte 		return err;
3469d949cac1SHarald Welte 	err = vt1708S_auto_fill_dac_nids(spec, &spec->autocfg);
3470d949cac1SHarald Welte 	if (err < 0)
3471d949cac1SHarald Welte 		return err;
3472d949cac1SHarald Welte 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3473d949cac1SHarald Welte 		return 0; /* can't find valid BIOS pin config */
3474d949cac1SHarald Welte 
3475d949cac1SHarald Welte 	err = vt1708S_auto_create_multi_out_ctls(spec, &spec->autocfg);
3476d949cac1SHarald Welte 	if (err < 0)
3477d949cac1SHarald Welte 		return err;
3478d949cac1SHarald Welte 	err = vt1708S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
3479d949cac1SHarald Welte 	if (err < 0)
3480d949cac1SHarald Welte 		return err;
3481d949cac1SHarald Welte 	err = vt1708S_auto_create_analog_input_ctls(spec, &spec->autocfg);
3482d949cac1SHarald Welte 	if (err < 0)
3483d949cac1SHarald Welte 		return err;
3484d949cac1SHarald Welte 
3485d949cac1SHarald Welte 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3486d949cac1SHarald Welte 
34879da29271STakashi Iwai 	fill_dig_outs(codec);
348898aa34c0SHarald Welte 
3489603c4019STakashi Iwai 	if (spec->kctls.list)
3490603c4019STakashi Iwai 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
3491d949cac1SHarald Welte 
34920aa62aefSHarald Welte 	spec->input_mux = &spec->private_imux[0];
34930aa62aefSHarald Welte 
3494f8fdd495SHarald Welte 	if (spec->hp_mux)
34950aa62aefSHarald Welte 		spec->mixers[spec->num_mixers++] = via_hp_mixer;
3496d949cac1SHarald Welte 
34971564b287SLydia Wang 	spec->mixers[spec->num_mixers++] = via_smart51_mixer;
3498d949cac1SHarald Welte 	return 1;
3499d949cac1SHarald Welte }
3500d949cac1SHarald Welte 
3501d949cac1SHarald Welte #ifdef CONFIG_SND_HDA_POWER_SAVE
3502d949cac1SHarald Welte static struct hda_amp_list vt1708S_loopbacks[] = {
3503d949cac1SHarald Welte 	{ 0x16, HDA_INPUT, 1 },
3504d949cac1SHarald Welte 	{ 0x16, HDA_INPUT, 2 },
3505d949cac1SHarald Welte 	{ 0x16, HDA_INPUT, 3 },
3506d949cac1SHarald Welte 	{ 0x16, HDA_INPUT, 4 },
3507d949cac1SHarald Welte 	{ } /* end */
3508d949cac1SHarald Welte };
3509d949cac1SHarald Welte #endif
3510d949cac1SHarald Welte 
35116369bcfcSLydia Wang static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
35126369bcfcSLydia Wang 			       int offset, int num_steps, int step_size)
35136369bcfcSLydia Wang {
35146369bcfcSLydia Wang 	snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
35156369bcfcSLydia Wang 				  (offset << AC_AMPCAP_OFFSET_SHIFT) |
35166369bcfcSLydia Wang 				  (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
35176369bcfcSLydia Wang 				  (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
35186369bcfcSLydia Wang 				  (0 << AC_AMPCAP_MUTE_SHIFT));
35196369bcfcSLydia Wang }
35206369bcfcSLydia Wang 
3521d949cac1SHarald Welte static int patch_vt1708S(struct hda_codec *codec)
3522d949cac1SHarald Welte {
3523d949cac1SHarald Welte 	struct via_spec *spec;
3524d949cac1SHarald Welte 	int err;
3525d949cac1SHarald Welte 
3526d949cac1SHarald Welte 	/* create a codec specific record */
3527d949cac1SHarald Welte 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3528d949cac1SHarald Welte 	if (spec == NULL)
3529d949cac1SHarald Welte 		return -ENOMEM;
3530d949cac1SHarald Welte 
3531d949cac1SHarald Welte 	codec->spec = spec;
3532d949cac1SHarald Welte 
3533d949cac1SHarald Welte 	/* automatic parse from the BIOS config */
3534d949cac1SHarald Welte 	err = vt1708S_parse_auto_config(codec);
3535d949cac1SHarald Welte 	if (err < 0) {
3536d949cac1SHarald Welte 		via_free(codec);
3537d949cac1SHarald Welte 		return err;
3538d949cac1SHarald Welte 	} else if (!err) {
3539d949cac1SHarald Welte 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
3540d949cac1SHarald Welte 		       "from BIOS.  Using genenic mode...\n");
3541d949cac1SHarald Welte 	}
3542d949cac1SHarald Welte 
354369e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1708S_volume_init_verbs;
354469e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1708S_uniwill_init_verbs;
3545d949cac1SHarald Welte 
3546d949cac1SHarald Welte 	spec->stream_name_analog = "VT1708S Analog";
3547d949cac1SHarald Welte 	spec->stream_analog_playback = &vt1708S_pcm_analog_playback;
3548d949cac1SHarald Welte 	spec->stream_analog_capture = &vt1708S_pcm_analog_capture;
3549d949cac1SHarald Welte 
3550d949cac1SHarald Welte 	spec->stream_name_digital = "VT1708S Digital";
3551d949cac1SHarald Welte 	spec->stream_digital_playback = &vt1708S_pcm_digital_playback;
3552d949cac1SHarald Welte 
3553d949cac1SHarald Welte 	if (!spec->adc_nids && spec->input_mux) {
3554d949cac1SHarald Welte 		spec->adc_nids = vt1708S_adc_nids;
3555d949cac1SHarald Welte 		spec->num_adc_nids = ARRAY_SIZE(vt1708S_adc_nids);
3556337b9d02STakashi Iwai 		get_mux_nids(codec);
35576369bcfcSLydia Wang 		override_mic_boost(codec, 0x1a, 0, 3, 40);
35586369bcfcSLydia Wang 		override_mic_boost(codec, 0x1e, 0, 3, 40);
3559d949cac1SHarald Welte 		spec->mixers[spec->num_mixers] = vt1708S_capture_mixer;
3560d949cac1SHarald Welte 		spec->num_mixers++;
3561d949cac1SHarald Welte 	}
3562d949cac1SHarald Welte 
3563d949cac1SHarald Welte 	codec->patch_ops = via_patch_ops;
3564d949cac1SHarald Welte 
3565d949cac1SHarald Welte 	codec->patch_ops.init = via_auto_init;
356669e52a80SHarald Welte 	codec->patch_ops.unsol_event = via_unsol_event;
3567d949cac1SHarald Welte #ifdef CONFIG_SND_HDA_POWER_SAVE
3568d949cac1SHarald Welte 	spec->loopback.amplist = vt1708S_loopbacks;
3569d949cac1SHarald Welte #endif
3570d949cac1SHarald Welte 
3571518bf3baSLydia Wang 	/* correct names for VT1708BCE */
3572518bf3baSLydia Wang 	if (get_codec_type(codec) == VT1708BCE)	{
3573518bf3baSLydia Wang 		kfree(codec->chip_name);
3574518bf3baSLydia Wang 		codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
3575518bf3baSLydia Wang 		snprintf(codec->bus->card->mixername,
3576518bf3baSLydia Wang 			 sizeof(codec->bus->card->mixername),
3577518bf3baSLydia Wang 			 "%s %s", codec->vendor_name, codec->chip_name);
3578518bf3baSLydia Wang 		spec->stream_name_analog = "VT1708BCE Analog";
3579518bf3baSLydia Wang 		spec->stream_name_digital = "VT1708BCE Digital";
3580518bf3baSLydia Wang 	}
3581d949cac1SHarald Welte 	return 0;
3582d949cac1SHarald Welte }
3583d949cac1SHarald Welte 
3584d949cac1SHarald Welte /* Patch for VT1702 */
3585d949cac1SHarald Welte 
3586d949cac1SHarald Welte /* capture mixer elements */
3587d949cac1SHarald Welte static struct snd_kcontrol_new vt1702_capture_mixer[] = {
3588d949cac1SHarald Welte 	HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_INPUT),
3589d949cac1SHarald Welte 	HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_INPUT),
3590d949cac1SHarald Welte 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x20, 0x0, HDA_INPUT),
3591d949cac1SHarald Welte 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x20, 0x0, HDA_INPUT),
3592d949cac1SHarald Welte 	HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x1F, 0x0, HDA_INPUT),
3593d949cac1SHarald Welte 	HDA_CODEC_MUTE("Digital Mic Capture Switch", 0x1F, 0x0, HDA_INPUT),
3594d949cac1SHarald Welte 	HDA_CODEC_VOLUME("Digital Mic Boost Capture Volume", 0x1E, 0x0,
3595d949cac1SHarald Welte 			 HDA_INPUT),
3596d949cac1SHarald Welte 	{
3597d949cac1SHarald Welte 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3598d949cac1SHarald Welte 		/* The multiple "Capture Source" controls confuse alsamixer
3599d949cac1SHarald Welte 		 * So call somewhat different..
3600d949cac1SHarald Welte 		 */
3601d949cac1SHarald Welte 		/* .name = "Capture Source", */
3602d949cac1SHarald Welte 		.name = "Input Source",
3603d949cac1SHarald Welte 		.count = 1,
3604d949cac1SHarald Welte 		.info = via_mux_enum_info,
3605d949cac1SHarald Welte 		.get = via_mux_enum_get,
3606d949cac1SHarald Welte 		.put = via_mux_enum_put,
3607d949cac1SHarald Welte 	},
3608d949cac1SHarald Welte 	{ } /* end */
3609d949cac1SHarald Welte };
3610d949cac1SHarald Welte 
3611d949cac1SHarald Welte static struct hda_verb vt1702_volume_init_verbs[] = {
3612d949cac1SHarald Welte 	/*
3613d949cac1SHarald Welte 	 * Unmute ADC0-1 and set the default input to mic-in
3614d949cac1SHarald Welte 	 */
3615d949cac1SHarald Welte 	{0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3616d949cac1SHarald Welte 	{0x1F, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3617d949cac1SHarald Welte 	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3618d949cac1SHarald Welte 
3619d949cac1SHarald Welte 
3620d949cac1SHarald Welte 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3621d949cac1SHarald Welte 	 * mixer widget
3622d949cac1SHarald Welte 	 */
3623d949cac1SHarald Welte 	/* Amp Indices: Mic1 = 1, Line = 1, Mic2 = 3 */
3624d949cac1SHarald Welte 	{0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3625d949cac1SHarald Welte 	{0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3626d949cac1SHarald Welte 	{0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3627d949cac1SHarald Welte 	{0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3628d949cac1SHarald Welte 	{0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3629d949cac1SHarald Welte 
3630d949cac1SHarald Welte 	/* Setup default input of PW4 to MW0 */
3631d949cac1SHarald Welte 	{0x17, AC_VERB_SET_CONNECT_SEL, 0x1},
3632d949cac1SHarald Welte 	/* PW6 PW7 Output enable */
3633d949cac1SHarald Welte 	{0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3634d949cac1SHarald Welte 	{0x1C, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3635bc7e7e5cSLydia Wang 	/* mixer enable */
3636bc7e7e5cSLydia Wang 	{0x1, 0xF88, 0x3},
3637bc7e7e5cSLydia Wang 	/* GPIO 0~2 */
3638bc7e7e5cSLydia Wang 	{0x1, 0xF82, 0x3F},
3639d949cac1SHarald Welte 	{ }
3640d949cac1SHarald Welte };
3641d949cac1SHarald Welte 
364269e52a80SHarald Welte static struct hda_verb vt1702_uniwill_init_verbs[] = {
3643a34df19aSLydia Wang 	{0x17, AC_VERB_SET_UNSOLICITED_ENABLE,
3644a34df19aSLydia Wang 	 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3645a34df19aSLydia Wang 	{0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3646a34df19aSLydia Wang 	{0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3647a34df19aSLydia Wang 	{0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3648a34df19aSLydia Wang 	{0x18, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
364969e52a80SHarald Welte 	{ }
365069e52a80SHarald Welte };
365169e52a80SHarald Welte 
3652d949cac1SHarald Welte static struct hda_pcm_stream vt1702_pcm_analog_playback = {
36530aa62aefSHarald Welte 	.substreams = 2,
3654d949cac1SHarald Welte 	.channels_min = 2,
3655d949cac1SHarald Welte 	.channels_max = 2,
3656d949cac1SHarald Welte 	.nid = 0x10, /* NID to query formats and rates */
3657d949cac1SHarald Welte 	.ops = {
3658d949cac1SHarald Welte 		.open = via_playback_pcm_open,
36590aa62aefSHarald Welte 		.prepare = via_playback_multi_pcm_prepare,
366017314379SLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup,
366117314379SLydia Wang 		.close = via_pcm_open_close
3662d949cac1SHarald Welte 	},
3663d949cac1SHarald Welte };
3664d949cac1SHarald Welte 
3665d949cac1SHarald Welte static struct hda_pcm_stream vt1702_pcm_analog_capture = {
3666d949cac1SHarald Welte 	.substreams = 3,
3667d949cac1SHarald Welte 	.channels_min = 2,
3668d949cac1SHarald Welte 	.channels_max = 2,
3669d949cac1SHarald Welte 	.nid = 0x12, /* NID to query formats and rates */
3670d949cac1SHarald Welte 	.ops = {
367117314379SLydia Wang 		.open = via_pcm_open_close,
3672d949cac1SHarald Welte 		.prepare = via_capture_pcm_prepare,
367317314379SLydia Wang 		.cleanup = via_capture_pcm_cleanup,
367417314379SLydia Wang 		.close = via_pcm_open_close
3675d949cac1SHarald Welte 	},
3676d949cac1SHarald Welte };
3677d949cac1SHarald Welte 
3678d949cac1SHarald Welte static struct hda_pcm_stream vt1702_pcm_digital_playback = {
36795691ec7fSHarald Welte 	.substreams = 2,
3680d949cac1SHarald Welte 	.channels_min = 2,
3681d949cac1SHarald Welte 	.channels_max = 2,
3682d949cac1SHarald Welte 	/* NID is set in via_build_pcms */
3683d949cac1SHarald Welte 	.ops = {
3684d949cac1SHarald Welte 		.open = via_dig_playback_pcm_open,
3685d949cac1SHarald Welte 		.close = via_dig_playback_pcm_close,
36869da29271STakashi Iwai 		.prepare = via_dig_playback_pcm_prepare,
36879da29271STakashi Iwai 		.cleanup = via_dig_playback_pcm_cleanup
3688d949cac1SHarald Welte 	},
3689d949cac1SHarald Welte };
3690d949cac1SHarald Welte 
3691d949cac1SHarald Welte /* fill in the dac_nids table from the parsed pin configuration */
3692d949cac1SHarald Welte static int vt1702_auto_fill_dac_nids(struct via_spec *spec,
3693d949cac1SHarald Welte 				     const struct auto_pin_cfg *cfg)
3694d949cac1SHarald Welte {
3695d949cac1SHarald Welte 	spec->multiout.num_dacs = 1;
3696d949cac1SHarald Welte 	spec->multiout.dac_nids = spec->private_dac_nids;
3697d949cac1SHarald Welte 
3698d949cac1SHarald Welte 	if (cfg->line_out_pins[0]) {
3699d949cac1SHarald Welte 		/* config dac list */
3700d949cac1SHarald Welte 		spec->multiout.dac_nids[0] = 0x10;
3701d949cac1SHarald Welte 	}
3702d949cac1SHarald Welte 
3703d949cac1SHarald Welte 	return 0;
3704d949cac1SHarald Welte }
3705d949cac1SHarald Welte 
3706d949cac1SHarald Welte /* add playback controls from the parsed DAC table */
3707d949cac1SHarald Welte static int vt1702_auto_create_line_out_ctls(struct via_spec *spec,
3708d949cac1SHarald Welte 					     const struct auto_pin_cfg *cfg)
3709d949cac1SHarald Welte {
3710d949cac1SHarald Welte 	int err;
3711d949cac1SHarald Welte 
3712d949cac1SHarald Welte 	if (!cfg->line_out_pins[0])
3713d949cac1SHarald Welte 		return -1;
3714d949cac1SHarald Welte 
3715d949cac1SHarald Welte 	/* add control to mixer index 0 */
3716d949cac1SHarald Welte 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3717d949cac1SHarald Welte 			      "Master Front Playback Volume",
3718d949cac1SHarald Welte 			      HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
3719d949cac1SHarald Welte 	if (err < 0)
3720d949cac1SHarald Welte 		return err;
3721d949cac1SHarald Welte 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3722d949cac1SHarald Welte 			      "Master Front Playback Switch",
3723d949cac1SHarald Welte 			      HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
3724d949cac1SHarald Welte 	if (err < 0)
3725d949cac1SHarald Welte 		return err;
3726d949cac1SHarald Welte 
3727d949cac1SHarald Welte 	/* Front */
3728d949cac1SHarald Welte 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3729d949cac1SHarald Welte 			      "Front Playback Volume",
3730d949cac1SHarald Welte 			      HDA_COMPOSE_AMP_VAL(0x10, 3, 0, HDA_OUTPUT));
3731d949cac1SHarald Welte 	if (err < 0)
3732d949cac1SHarald Welte 		return err;
3733d949cac1SHarald Welte 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3734d949cac1SHarald Welte 			      "Front Playback Switch",
3735d949cac1SHarald Welte 			      HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_OUTPUT));
3736d949cac1SHarald Welte 	if (err < 0)
3737d949cac1SHarald Welte 		return err;
3738d949cac1SHarald Welte 
3739d949cac1SHarald Welte 	return 0;
3740d949cac1SHarald Welte }
3741d949cac1SHarald Welte 
3742d949cac1SHarald Welte static int vt1702_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
3743d949cac1SHarald Welte {
37440713efebSLydia Wang 	int err, i;
37450713efebSLydia Wang 	struct hda_input_mux *imux;
37460713efebSLydia Wang 	static const char *texts[] = { "ON", "OFF", NULL};
3747d949cac1SHarald Welte 	if (!pin)
3748d949cac1SHarald Welte 		return 0;
3749d949cac1SHarald Welte 	spec->multiout.hp_nid = 0x1D;
3750cdc1784dSLydia Wang 	spec->hp_independent_mode_index = 0;
3751d949cac1SHarald Welte 
3752d949cac1SHarald Welte 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3753d949cac1SHarald Welte 			      "Headphone Playback Volume",
3754d949cac1SHarald Welte 			      HDA_COMPOSE_AMP_VAL(0x1D, 3, 0, HDA_OUTPUT));
3755d949cac1SHarald Welte 	if (err < 0)
3756d949cac1SHarald Welte 		return err;
3757d949cac1SHarald Welte 
3758d949cac1SHarald Welte 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3759d949cac1SHarald Welte 			      "Headphone Playback Switch",
3760d949cac1SHarald Welte 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3761d949cac1SHarald Welte 	if (err < 0)
3762d949cac1SHarald Welte 		return err;
3763d949cac1SHarald Welte 
37640713efebSLydia Wang 	imux = &spec->private_imux[1];
37650aa62aefSHarald Welte 
37660713efebSLydia Wang 	/* for hp mode select */
37670713efebSLydia Wang 	i = 0;
37680713efebSLydia Wang 	while (texts[i] != NULL)	{
37690713efebSLydia Wang 		imux->items[imux->num_items].label =  texts[i];
37700713efebSLydia Wang 		imux->items[imux->num_items].index = i;
37710713efebSLydia Wang 		imux->num_items++;
37720713efebSLydia Wang 		i++;
37730713efebSLydia Wang 	}
37740713efebSLydia Wang 
37750713efebSLydia Wang 	spec->hp_mux = &spec->private_imux[1];
3776d949cac1SHarald Welte 	return 0;
3777d949cac1SHarald Welte }
3778d949cac1SHarald Welte 
3779d949cac1SHarald Welte /* create playback/capture controls for input pins */
3780d949cac1SHarald Welte static int vt1702_auto_create_analog_input_ctls(struct via_spec *spec,
3781d949cac1SHarald Welte 						const struct auto_pin_cfg *cfg)
3782d949cac1SHarald Welte {
3783d949cac1SHarald Welte 	static char *labels[] = {
3784d949cac1SHarald Welte 		"Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
3785d949cac1SHarald Welte 	};
37860aa62aefSHarald Welte 	struct hda_input_mux *imux = &spec->private_imux[0];
3787d949cac1SHarald Welte 	int i, err, idx = 0;
3788d949cac1SHarald Welte 
3789d949cac1SHarald Welte 	/* for internal loopback recording select */
3790d949cac1SHarald Welte 	imux->items[imux->num_items].label = "Stereo Mixer";
3791d949cac1SHarald Welte 	imux->items[imux->num_items].index = 3;
3792d949cac1SHarald Welte 	imux->num_items++;
3793d949cac1SHarald Welte 
3794d949cac1SHarald Welte 	for (i = 0; i < AUTO_PIN_LAST; i++) {
3795d949cac1SHarald Welte 		if (!cfg->input_pins[i])
3796d949cac1SHarald Welte 			continue;
3797d949cac1SHarald Welte 
3798d949cac1SHarald Welte 		switch (cfg->input_pins[i]) {
3799d949cac1SHarald Welte 		case 0x14: /* Mic */
3800d949cac1SHarald Welte 			idx = 1;
3801d949cac1SHarald Welte 			break;
3802d949cac1SHarald Welte 
3803d949cac1SHarald Welte 		case 0x15: /* Line In */
3804d949cac1SHarald Welte 			idx = 2;
3805d949cac1SHarald Welte 			break;
3806d949cac1SHarald Welte 
3807d949cac1SHarald Welte 		case 0x18: /* Front Mic */
3808d949cac1SHarald Welte 			idx = 3;
3809d949cac1SHarald Welte 			break;
3810d949cac1SHarald Welte 		}
38119510e8ddSLydia Wang 		err = via_new_analog_input(spec, labels[i], idx, 0x1A);
3812d949cac1SHarald Welte 		if (err < 0)
3813d949cac1SHarald Welte 			return err;
3814d949cac1SHarald Welte 		imux->items[imux->num_items].label = labels[i];
3815d949cac1SHarald Welte 		imux->items[imux->num_items].index = idx-1;
3816d949cac1SHarald Welte 		imux->num_items++;
3817d949cac1SHarald Welte 	}
3818d949cac1SHarald Welte 	return 0;
3819d949cac1SHarald Welte }
3820d949cac1SHarald Welte 
3821d949cac1SHarald Welte static int vt1702_parse_auto_config(struct hda_codec *codec)
3822d949cac1SHarald Welte {
3823d949cac1SHarald Welte 	struct via_spec *spec = codec->spec;
3824d949cac1SHarald Welte 	int err;
3825d949cac1SHarald Welte 
38269da29271STakashi Iwai 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3827d949cac1SHarald Welte 	if (err < 0)
3828d949cac1SHarald Welte 		return err;
3829d949cac1SHarald Welte 	err = vt1702_auto_fill_dac_nids(spec, &spec->autocfg);
3830d949cac1SHarald Welte 	if (err < 0)
3831d949cac1SHarald Welte 		return err;
3832d949cac1SHarald Welte 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3833d949cac1SHarald Welte 		return 0; /* can't find valid BIOS pin config */
3834d949cac1SHarald Welte 
3835d949cac1SHarald Welte 	err = vt1702_auto_create_line_out_ctls(spec, &spec->autocfg);
3836d949cac1SHarald Welte 	if (err < 0)
3837d949cac1SHarald Welte 		return err;
3838d949cac1SHarald Welte 	err = vt1702_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
3839d949cac1SHarald Welte 	if (err < 0)
3840d949cac1SHarald Welte 		return err;
3841c2c02ea3SLydia Wang 	/* limit AA path volume to 0 dB */
3842c2c02ea3SLydia Wang 	snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
3843c2c02ea3SLydia Wang 				  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
3844c2c02ea3SLydia Wang 				  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
3845c2c02ea3SLydia Wang 				  (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
3846c2c02ea3SLydia Wang 				  (1 << AC_AMPCAP_MUTE_SHIFT));
3847d949cac1SHarald Welte 	err = vt1702_auto_create_analog_input_ctls(spec, &spec->autocfg);
3848d949cac1SHarald Welte 	if (err < 0)
3849d949cac1SHarald Welte 		return err;
3850d949cac1SHarald Welte 
3851d949cac1SHarald Welte 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3852d949cac1SHarald Welte 
38539da29271STakashi Iwai 	fill_dig_outs(codec);
385498aa34c0SHarald Welte 
3855603c4019STakashi Iwai 	if (spec->kctls.list)
3856603c4019STakashi Iwai 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
3857d949cac1SHarald Welte 
38580aa62aefSHarald Welte 	spec->input_mux = &spec->private_imux[0];
38590aa62aefSHarald Welte 
3860f8fdd495SHarald Welte 	if (spec->hp_mux)
38610aa62aefSHarald Welte 		spec->mixers[spec->num_mixers++] = via_hp_mixer;
3862d949cac1SHarald Welte 
3863d949cac1SHarald Welte 	return 1;
3864d949cac1SHarald Welte }
3865d949cac1SHarald Welte 
3866d949cac1SHarald Welte #ifdef CONFIG_SND_HDA_POWER_SAVE
3867d949cac1SHarald Welte static struct hda_amp_list vt1702_loopbacks[] = {
3868d949cac1SHarald Welte 	{ 0x1A, HDA_INPUT, 1 },
3869d949cac1SHarald Welte 	{ 0x1A, HDA_INPUT, 2 },
3870d949cac1SHarald Welte 	{ 0x1A, HDA_INPUT, 3 },
3871d949cac1SHarald Welte 	{ 0x1A, HDA_INPUT, 4 },
3872d949cac1SHarald Welte 	{ } /* end */
3873d949cac1SHarald Welte };
3874d949cac1SHarald Welte #endif
3875d949cac1SHarald Welte 
3876d949cac1SHarald Welte static int patch_vt1702(struct hda_codec *codec)
3877d949cac1SHarald Welte {
3878d949cac1SHarald Welte 	struct via_spec *spec;
3879d949cac1SHarald Welte 	int err;
3880d949cac1SHarald Welte 
3881d949cac1SHarald Welte 	/* create a codec specific record */
3882d949cac1SHarald Welte 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3883d949cac1SHarald Welte 	if (spec == NULL)
3884d949cac1SHarald Welte 		return -ENOMEM;
3885d949cac1SHarald Welte 
3886d949cac1SHarald Welte 	codec->spec = spec;
3887d949cac1SHarald Welte 
3888d949cac1SHarald Welte 	/* automatic parse from the BIOS config */
3889d949cac1SHarald Welte 	err = vt1702_parse_auto_config(codec);
3890d949cac1SHarald Welte 	if (err < 0) {
3891d949cac1SHarald Welte 		via_free(codec);
3892d949cac1SHarald Welte 		return err;
3893d949cac1SHarald Welte 	} else if (!err) {
3894d949cac1SHarald Welte 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
3895d949cac1SHarald Welte 		       "from BIOS.  Using genenic mode...\n");
3896d949cac1SHarald Welte 	}
3897d949cac1SHarald Welte 
389869e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1702_volume_init_verbs;
389969e52a80SHarald Welte 	spec->init_verbs[spec->num_iverbs++] = vt1702_uniwill_init_verbs;
3900d949cac1SHarald Welte 
3901d949cac1SHarald Welte 	spec->stream_name_analog = "VT1702 Analog";
3902d949cac1SHarald Welte 	spec->stream_analog_playback = &vt1702_pcm_analog_playback;
3903d949cac1SHarald Welte 	spec->stream_analog_capture = &vt1702_pcm_analog_capture;
3904d949cac1SHarald Welte 
3905d949cac1SHarald Welte 	spec->stream_name_digital = "VT1702 Digital";
3906d949cac1SHarald Welte 	spec->stream_digital_playback = &vt1702_pcm_digital_playback;
3907d949cac1SHarald Welte 
3908d949cac1SHarald Welte 	if (!spec->adc_nids && spec->input_mux) {
3909d949cac1SHarald Welte 		spec->adc_nids = vt1702_adc_nids;
3910d949cac1SHarald Welte 		spec->num_adc_nids = ARRAY_SIZE(vt1702_adc_nids);
3911337b9d02STakashi Iwai 		get_mux_nids(codec);
3912d949cac1SHarald Welte 		spec->mixers[spec->num_mixers] = vt1702_capture_mixer;
3913d949cac1SHarald Welte 		spec->num_mixers++;
3914d949cac1SHarald Welte 	}
3915d949cac1SHarald Welte 
3916d949cac1SHarald Welte 	codec->patch_ops = via_patch_ops;
3917d949cac1SHarald Welte 
3918d949cac1SHarald Welte 	codec->patch_ops.init = via_auto_init;
391969e52a80SHarald Welte 	codec->patch_ops.unsol_event = via_unsol_event;
3920d949cac1SHarald Welte #ifdef CONFIG_SND_HDA_POWER_SAVE
3921d949cac1SHarald Welte 	spec->loopback.amplist = vt1702_loopbacks;
3922d949cac1SHarald Welte #endif
3923d949cac1SHarald Welte 
3924d949cac1SHarald Welte 	return 0;
3925d949cac1SHarald Welte }
3926d949cac1SHarald Welte 
3927*eb7188caSLydia Wang /* Patch for VT1718S */
3928*eb7188caSLydia Wang 
3929*eb7188caSLydia Wang /* capture mixer elements */
3930*eb7188caSLydia Wang static struct snd_kcontrol_new vt1718S_capture_mixer[] = {
3931*eb7188caSLydia Wang 	HDA_CODEC_VOLUME("Capture Volume", 0x10, 0x0, HDA_INPUT),
3932*eb7188caSLydia Wang 	HDA_CODEC_MUTE("Capture Switch", 0x10, 0x0, HDA_INPUT),
3933*eb7188caSLydia Wang 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x11, 0x0, HDA_INPUT),
3934*eb7188caSLydia Wang 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x11, 0x0, HDA_INPUT),
3935*eb7188caSLydia Wang 	HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x2b, 0x0, HDA_INPUT),
3936*eb7188caSLydia Wang 	HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x29, 0x0,
3937*eb7188caSLydia Wang 			 HDA_INPUT),
3938*eb7188caSLydia Wang 	{
3939*eb7188caSLydia Wang 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3940*eb7188caSLydia Wang 		/* The multiple "Capture Source" controls confuse alsamixer
3941*eb7188caSLydia Wang 		 * So call somewhat different..
3942*eb7188caSLydia Wang 		 */
3943*eb7188caSLydia Wang 		.name = "Input Source",
3944*eb7188caSLydia Wang 		.count = 2,
3945*eb7188caSLydia Wang 		.info = via_mux_enum_info,
3946*eb7188caSLydia Wang 		.get = via_mux_enum_get,
3947*eb7188caSLydia Wang 		.put = via_mux_enum_put,
3948*eb7188caSLydia Wang 	},
3949*eb7188caSLydia Wang 	{ } /* end */
3950*eb7188caSLydia Wang };
3951*eb7188caSLydia Wang 
3952*eb7188caSLydia Wang static struct hda_verb vt1718S_volume_init_verbs[] = {
3953*eb7188caSLydia Wang 	/*
3954*eb7188caSLydia Wang 	 * Unmute ADC0-1 and set the default input to mic-in
3955*eb7188caSLydia Wang 	 */
3956*eb7188caSLydia Wang 	{0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3957*eb7188caSLydia Wang 	{0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3958*eb7188caSLydia Wang 
3959*eb7188caSLydia Wang 
3960*eb7188caSLydia Wang 	/* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3961*eb7188caSLydia Wang 	 * mixer widget
3962*eb7188caSLydia Wang 	 */
3963*eb7188caSLydia Wang 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3964*eb7188caSLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3965*eb7188caSLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3966*eb7188caSLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
3967*eb7188caSLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
3968*eb7188caSLydia Wang 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
3969*eb7188caSLydia Wang 
3970*eb7188caSLydia Wang 	/* Setup default input of Front HP to MW9 */
3971*eb7188caSLydia Wang 	{0x28, AC_VERB_SET_CONNECT_SEL, 0x1},
3972*eb7188caSLydia Wang 	/* PW9 PW10 Output enable */
3973*eb7188caSLydia Wang 	{0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
3974*eb7188caSLydia Wang 	{0x2e, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
3975*eb7188caSLydia Wang 	/* PW11 Input enable */
3976*eb7188caSLydia Wang 	{0x2f, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_IN_EN},
3977*eb7188caSLydia Wang 	/* Enable Boost Volume backdoor */
3978*eb7188caSLydia Wang 	{0x1, 0xf88, 0x8},
3979*eb7188caSLydia Wang 	/* MW0/1/2/3/4: un-mute index 0 (AOWx), mute index 1 (MW9) */
3980*eb7188caSLydia Wang 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3981*eb7188caSLydia Wang 	{0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3982*eb7188caSLydia Wang 	{0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3983*eb7188caSLydia Wang 	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3984*eb7188caSLydia Wang 	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3985*eb7188caSLydia Wang 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3986*eb7188caSLydia Wang 	{0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3987*eb7188caSLydia Wang 	{0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3988*eb7188caSLydia Wang 	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3989*eb7188caSLydia Wang 	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3990*eb7188caSLydia Wang 	/* set MUX1 = 2 (AOW4), MUX2 = 1 (AOW3) */
3991*eb7188caSLydia Wang 	{0x34, AC_VERB_SET_CONNECT_SEL, 0x2},
3992*eb7188caSLydia Wang 	{0x35, AC_VERB_SET_CONNECT_SEL, 0x1},
3993*eb7188caSLydia Wang 	/* Unmute MW4's index 0 */
3994*eb7188caSLydia Wang 	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3995*eb7188caSLydia Wang 	{ }
3996*eb7188caSLydia Wang };
3997*eb7188caSLydia Wang 
3998*eb7188caSLydia Wang 
3999*eb7188caSLydia Wang static struct hda_verb vt1718S_uniwill_init_verbs[] = {
4000*eb7188caSLydia Wang 	{0x28, AC_VERB_SET_UNSOLICITED_ENABLE,
4001*eb7188caSLydia Wang 	 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
4002*eb7188caSLydia Wang 	{0x24, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4003*eb7188caSLydia Wang 	{0x25, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4004*eb7188caSLydia Wang 	{0x26, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4005*eb7188caSLydia Wang 	{0x27, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4006*eb7188caSLydia Wang 	{0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4007*eb7188caSLydia Wang 	{0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4008*eb7188caSLydia Wang 	{0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4009*eb7188caSLydia Wang 	{ }
4010*eb7188caSLydia Wang };
4011*eb7188caSLydia Wang 
4012*eb7188caSLydia Wang static struct hda_pcm_stream vt1718S_pcm_analog_playback = {
4013*eb7188caSLydia Wang 	.substreams = 2,
4014*eb7188caSLydia Wang 	.channels_min = 2,
4015*eb7188caSLydia Wang 	.channels_max = 10,
4016*eb7188caSLydia Wang 	.nid = 0x8, /* NID to query formats and rates */
4017*eb7188caSLydia Wang 	.ops = {
4018*eb7188caSLydia Wang 		.open = via_playback_pcm_open,
4019*eb7188caSLydia Wang 		.prepare = via_playback_multi_pcm_prepare,
4020*eb7188caSLydia Wang 		.cleanup = via_playback_multi_pcm_cleanup,
4021*eb7188caSLydia Wang 		.close = via_pcm_open_close,
4022*eb7188caSLydia Wang 	},
4023*eb7188caSLydia Wang };
4024*eb7188caSLydia Wang 
4025*eb7188caSLydia Wang static struct hda_pcm_stream vt1718S_pcm_analog_capture = {
4026*eb7188caSLydia Wang 	.substreams = 2,
4027*eb7188caSLydia Wang 	.channels_min = 2,
4028*eb7188caSLydia Wang 	.channels_max = 2,
4029*eb7188caSLydia Wang 	.nid = 0x10, /* NID to query formats and rates */
4030*eb7188caSLydia Wang 	.ops = {
4031*eb7188caSLydia Wang 		.open = via_pcm_open_close,
4032*eb7188caSLydia Wang 		.prepare = via_capture_pcm_prepare,
4033*eb7188caSLydia Wang 		.cleanup = via_capture_pcm_cleanup,
4034*eb7188caSLydia Wang 		.close = via_pcm_open_close,
4035*eb7188caSLydia Wang 	},
4036*eb7188caSLydia Wang };
4037*eb7188caSLydia Wang 
4038*eb7188caSLydia Wang static struct hda_pcm_stream vt1718S_pcm_digital_playback = {
4039*eb7188caSLydia Wang 	.substreams = 2,
4040*eb7188caSLydia Wang 	.channels_min = 2,
4041*eb7188caSLydia Wang 	.channels_max = 2,
4042*eb7188caSLydia Wang 	.rates = SNDRV_PCM_RATE_48000,
4043*eb7188caSLydia Wang 	/* NID is set in via_build_pcms */
4044*eb7188caSLydia Wang 	.ops = {
4045*eb7188caSLydia Wang 		.open = via_dig_playback_pcm_open,
4046*eb7188caSLydia Wang 		.close = via_dig_playback_pcm_close,
4047*eb7188caSLydia Wang 		.prepare = via_dig_playback_pcm_prepare,
4048*eb7188caSLydia Wang 		.cleanup = via_dig_playback_pcm_cleanup
4049*eb7188caSLydia Wang 	},
4050*eb7188caSLydia Wang };
4051*eb7188caSLydia Wang 
4052*eb7188caSLydia Wang static struct hda_pcm_stream vt1718S_pcm_digital_capture = {
4053*eb7188caSLydia Wang 	.substreams = 1,
4054*eb7188caSLydia Wang 	.channels_min = 2,
4055*eb7188caSLydia Wang 	.channels_max = 2,
4056*eb7188caSLydia Wang };
4057*eb7188caSLydia Wang 
4058*eb7188caSLydia Wang /* fill in the dac_nids table from the parsed pin configuration */
4059*eb7188caSLydia Wang static int vt1718S_auto_fill_dac_nids(struct via_spec *spec,
4060*eb7188caSLydia Wang 				     const struct auto_pin_cfg *cfg)
4061*eb7188caSLydia Wang {
4062*eb7188caSLydia Wang 	int i;
4063*eb7188caSLydia Wang 	hda_nid_t nid;
4064*eb7188caSLydia Wang 
4065*eb7188caSLydia Wang 	spec->multiout.num_dacs = cfg->line_outs;
4066*eb7188caSLydia Wang 
4067*eb7188caSLydia Wang 	spec->multiout.dac_nids = spec->private_dac_nids;
4068*eb7188caSLydia Wang 
4069*eb7188caSLydia Wang 	for (i = 0; i < 4; i++) {
4070*eb7188caSLydia Wang 		nid = cfg->line_out_pins[i];
4071*eb7188caSLydia Wang 		if (nid) {
4072*eb7188caSLydia Wang 			/* config dac list */
4073*eb7188caSLydia Wang 			switch (i) {
4074*eb7188caSLydia Wang 			case AUTO_SEQ_FRONT:
4075*eb7188caSLydia Wang 				spec->multiout.dac_nids[i] = 0x8;
4076*eb7188caSLydia Wang 				break;
4077*eb7188caSLydia Wang 			case AUTO_SEQ_CENLFE:
4078*eb7188caSLydia Wang 				spec->multiout.dac_nids[i] = 0xa;
4079*eb7188caSLydia Wang 				break;
4080*eb7188caSLydia Wang 			case AUTO_SEQ_SURROUND:
4081*eb7188caSLydia Wang 				spec->multiout.dac_nids[i] = 0x9;
4082*eb7188caSLydia Wang 				break;
4083*eb7188caSLydia Wang 			case AUTO_SEQ_SIDE:
4084*eb7188caSLydia Wang 				spec->multiout.dac_nids[i] = 0xb;
4085*eb7188caSLydia Wang 				break;
4086*eb7188caSLydia Wang 			}
4087*eb7188caSLydia Wang 		}
4088*eb7188caSLydia Wang 	}
4089*eb7188caSLydia Wang 
4090*eb7188caSLydia Wang 	return 0;
4091*eb7188caSLydia Wang }
4092*eb7188caSLydia Wang 
4093*eb7188caSLydia Wang /* add playback controls from the parsed DAC table */
4094*eb7188caSLydia Wang static int vt1718S_auto_create_multi_out_ctls(struct via_spec *spec,
4095*eb7188caSLydia Wang 					     const struct auto_pin_cfg *cfg)
4096*eb7188caSLydia Wang {
4097*eb7188caSLydia Wang 	char name[32];
4098*eb7188caSLydia Wang 	static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
4099*eb7188caSLydia Wang 	hda_nid_t nid_vols[] = {0x8, 0x9, 0xa, 0xb};
4100*eb7188caSLydia Wang 	hda_nid_t nid_mutes[] = {0x24, 0x25, 0x26, 0x27};
4101*eb7188caSLydia Wang 	hda_nid_t nid, nid_vol, nid_mute = 0;
4102*eb7188caSLydia Wang 	int i, err;
4103*eb7188caSLydia Wang 
4104*eb7188caSLydia Wang 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
4105*eb7188caSLydia Wang 		nid = cfg->line_out_pins[i];
4106*eb7188caSLydia Wang 
4107*eb7188caSLydia Wang 		if (!nid)
4108*eb7188caSLydia Wang 			continue;
4109*eb7188caSLydia Wang 		nid_vol = nid_vols[i];
4110*eb7188caSLydia Wang 		nid_mute = nid_mutes[i];
4111*eb7188caSLydia Wang 
4112*eb7188caSLydia Wang 		if (i == AUTO_SEQ_CENLFE) {
4113*eb7188caSLydia Wang 			/* Center/LFE */
4114*eb7188caSLydia Wang 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4115*eb7188caSLydia Wang 					      "Center Playback Volume",
4116*eb7188caSLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
4117*eb7188caSLydia Wang 								  HDA_OUTPUT));
4118*eb7188caSLydia Wang 			if (err < 0)
4119*eb7188caSLydia Wang 				return err;
4120*eb7188caSLydia Wang 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4121*eb7188caSLydia Wang 					      "LFE Playback Volume",
4122*eb7188caSLydia Wang 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
4123*eb7188caSLydia Wang 								  HDA_OUTPUT));
4124*eb7188caSLydia Wang 			if (err < 0)
4125*eb7188caSLydia Wang 				return err;
4126*eb7188caSLydia Wang 			err = via_add_control(
4127*eb7188caSLydia Wang 				spec, VIA_CTL_WIDGET_MUTE,
4128*eb7188caSLydia Wang 				"Center Playback Switch",
4129*eb7188caSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_mute, 1, 0,
4130*eb7188caSLydia Wang 						    HDA_OUTPUT));
4131*eb7188caSLydia Wang 			if (err < 0)
4132*eb7188caSLydia Wang 				return err;
4133*eb7188caSLydia Wang 			err = via_add_control(
4134*eb7188caSLydia Wang 				spec, VIA_CTL_WIDGET_MUTE,
4135*eb7188caSLydia Wang 				"LFE Playback Switch",
4136*eb7188caSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_mute, 2, 0,
4137*eb7188caSLydia Wang 						    HDA_OUTPUT));
4138*eb7188caSLydia Wang 			if (err < 0)
4139*eb7188caSLydia Wang 				return err;
4140*eb7188caSLydia Wang 		} else if (i == AUTO_SEQ_FRONT) {
4141*eb7188caSLydia Wang 			/* Front */
4142*eb7188caSLydia Wang 			sprintf(name, "%s Playback Volume", chname[i]);
4143*eb7188caSLydia Wang 			err = via_add_control(
4144*eb7188caSLydia Wang 				spec, VIA_CTL_WIDGET_VOL, name,
4145*eb7188caSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
4146*eb7188caSLydia Wang 			if (err < 0)
4147*eb7188caSLydia Wang 				return err;
4148*eb7188caSLydia Wang 			sprintf(name, "%s Playback Switch", chname[i]);
4149*eb7188caSLydia Wang 			err = via_add_control(
4150*eb7188caSLydia Wang 				spec, VIA_CTL_WIDGET_MUTE, name,
4151*eb7188caSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
4152*eb7188caSLydia Wang 						    HDA_OUTPUT));
4153*eb7188caSLydia Wang 			if (err < 0)
4154*eb7188caSLydia Wang 				return err;
4155*eb7188caSLydia Wang 		} else {
4156*eb7188caSLydia Wang 			sprintf(name, "%s Playback Volume", chname[i]);
4157*eb7188caSLydia Wang 			err = via_add_control(
4158*eb7188caSLydia Wang 				spec, VIA_CTL_WIDGET_VOL, name,
4159*eb7188caSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
4160*eb7188caSLydia Wang 			if (err < 0)
4161*eb7188caSLydia Wang 				return err;
4162*eb7188caSLydia Wang 			sprintf(name, "%s Playback Switch", chname[i]);
4163*eb7188caSLydia Wang 			err = via_add_control(
4164*eb7188caSLydia Wang 				spec, VIA_CTL_WIDGET_MUTE, name,
4165*eb7188caSLydia Wang 				HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
4166*eb7188caSLydia Wang 						    HDA_OUTPUT));
4167*eb7188caSLydia Wang 			if (err < 0)
4168*eb7188caSLydia Wang 				return err;
4169*eb7188caSLydia Wang 		}
4170*eb7188caSLydia Wang 	}
4171*eb7188caSLydia Wang 	return 0;
4172*eb7188caSLydia Wang }
4173*eb7188caSLydia Wang 
4174*eb7188caSLydia Wang static int vt1718S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
4175*eb7188caSLydia Wang {
4176*eb7188caSLydia Wang 	int err;
4177*eb7188caSLydia Wang 
4178*eb7188caSLydia Wang 	if (!pin)
4179*eb7188caSLydia Wang 		return 0;
4180*eb7188caSLydia Wang 
4181*eb7188caSLydia Wang 	spec->multiout.hp_nid = 0xc; /* AOW4 */
4182*eb7188caSLydia Wang 	spec->hp_independent_mode_index = 1;
4183*eb7188caSLydia Wang 
4184*eb7188caSLydia Wang 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4185*eb7188caSLydia Wang 			      "Headphone Playback Volume",
4186*eb7188caSLydia Wang 			      HDA_COMPOSE_AMP_VAL(0xc, 3, 0, HDA_OUTPUT));
4187*eb7188caSLydia Wang 	if (err < 0)
4188*eb7188caSLydia Wang 		return err;
4189*eb7188caSLydia Wang 
4190*eb7188caSLydia Wang 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4191*eb7188caSLydia Wang 			      "Headphone Playback Switch",
4192*eb7188caSLydia Wang 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
4193*eb7188caSLydia Wang 	if (err < 0)
4194*eb7188caSLydia Wang 		return err;
4195*eb7188caSLydia Wang 
4196*eb7188caSLydia Wang 	create_hp_imux(spec);
4197*eb7188caSLydia Wang 	return 0;
4198*eb7188caSLydia Wang }
4199*eb7188caSLydia Wang 
4200*eb7188caSLydia Wang /* create playback/capture controls for input pins */
4201*eb7188caSLydia Wang static int vt1718S_auto_create_analog_input_ctls(struct via_spec *spec,
4202*eb7188caSLydia Wang 						const struct auto_pin_cfg *cfg)
4203*eb7188caSLydia Wang {
4204*eb7188caSLydia Wang 	static char *labels[] = {
4205*eb7188caSLydia Wang 		"Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
4206*eb7188caSLydia Wang 	};
4207*eb7188caSLydia Wang 	struct hda_input_mux *imux = &spec->private_imux[0];
4208*eb7188caSLydia Wang 	int i, err, idx = 0;
4209*eb7188caSLydia Wang 
4210*eb7188caSLydia Wang 	/* for internal loopback recording select */
4211*eb7188caSLydia Wang 	imux->items[imux->num_items].label = "Stereo Mixer";
4212*eb7188caSLydia Wang 	imux->items[imux->num_items].index = 5;
4213*eb7188caSLydia Wang 	imux->num_items++;
4214*eb7188caSLydia Wang 
4215*eb7188caSLydia Wang 	for (i = 0; i < AUTO_PIN_LAST; i++) {
4216*eb7188caSLydia Wang 		if (!cfg->input_pins[i])
4217*eb7188caSLydia Wang 			continue;
4218*eb7188caSLydia Wang 
4219*eb7188caSLydia Wang 		switch (cfg->input_pins[i]) {
4220*eb7188caSLydia Wang 		case 0x2b: /* Mic */
4221*eb7188caSLydia Wang 			idx = 1;
4222*eb7188caSLydia Wang 			break;
4223*eb7188caSLydia Wang 
4224*eb7188caSLydia Wang 		case 0x2a: /* Line In */
4225*eb7188caSLydia Wang 			idx = 2;
4226*eb7188caSLydia Wang 			break;
4227*eb7188caSLydia Wang 
4228*eb7188caSLydia Wang 		case 0x29: /* Front Mic */
4229*eb7188caSLydia Wang 			idx = 3;
4230*eb7188caSLydia Wang 			break;
4231*eb7188caSLydia Wang 
4232*eb7188caSLydia Wang 		case 0x2c: /* CD */
4233*eb7188caSLydia Wang 			idx = 0;
4234*eb7188caSLydia Wang 			break;
4235*eb7188caSLydia Wang 		}
4236*eb7188caSLydia Wang 		err = via_new_analog_input(spec, labels[i], idx, 0x21);
4237*eb7188caSLydia Wang 		if (err < 0)
4238*eb7188caSLydia Wang 			return err;
4239*eb7188caSLydia Wang 		imux->items[imux->num_items].label = labels[i];
4240*eb7188caSLydia Wang 		imux->items[imux->num_items].index = idx;
4241*eb7188caSLydia Wang 		imux->num_items++;
4242*eb7188caSLydia Wang 	}
4243*eb7188caSLydia Wang 	return 0;
4244*eb7188caSLydia Wang }
4245*eb7188caSLydia Wang 
4246*eb7188caSLydia Wang static int vt1718S_parse_auto_config(struct hda_codec *codec)
4247*eb7188caSLydia Wang {
4248*eb7188caSLydia Wang 	struct via_spec *spec = codec->spec;
4249*eb7188caSLydia Wang 	int err;
4250*eb7188caSLydia Wang 
4251*eb7188caSLydia Wang 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4252*eb7188caSLydia Wang 
4253*eb7188caSLydia Wang 	if (err < 0)
4254*eb7188caSLydia Wang 		return err;
4255*eb7188caSLydia Wang 	err = vt1718S_auto_fill_dac_nids(spec, &spec->autocfg);
4256*eb7188caSLydia Wang 	if (err < 0)
4257*eb7188caSLydia Wang 		return err;
4258*eb7188caSLydia Wang 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
4259*eb7188caSLydia Wang 		return 0; /* can't find valid BIOS pin config */
4260*eb7188caSLydia Wang 
4261*eb7188caSLydia Wang 	err = vt1718S_auto_create_multi_out_ctls(spec, &spec->autocfg);
4262*eb7188caSLydia Wang 	if (err < 0)
4263*eb7188caSLydia Wang 		return err;
4264*eb7188caSLydia Wang 	err = vt1718S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
4265*eb7188caSLydia Wang 	if (err < 0)
4266*eb7188caSLydia Wang 		return err;
4267*eb7188caSLydia Wang 	err = vt1718S_auto_create_analog_input_ctls(spec, &spec->autocfg);
4268*eb7188caSLydia Wang 	if (err < 0)
4269*eb7188caSLydia Wang 		return err;
4270*eb7188caSLydia Wang 
4271*eb7188caSLydia Wang 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4272*eb7188caSLydia Wang 
4273*eb7188caSLydia Wang 	fill_dig_outs(codec);
4274*eb7188caSLydia Wang 
4275*eb7188caSLydia Wang 	if (spec->autocfg.dig_in_pin && codec->vendor_id == 0x11060428)
4276*eb7188caSLydia Wang 		spec->dig_in_nid = 0x13;
4277*eb7188caSLydia Wang 
4278*eb7188caSLydia Wang 	if (spec->kctls.list)
4279*eb7188caSLydia Wang 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
4280*eb7188caSLydia Wang 
4281*eb7188caSLydia Wang 	spec->input_mux = &spec->private_imux[0];
4282*eb7188caSLydia Wang 
4283*eb7188caSLydia Wang 	if (spec->hp_mux)
4284*eb7188caSLydia Wang 		spec->mixers[spec->num_mixers++] = via_hp_mixer;
4285*eb7188caSLydia Wang 
4286*eb7188caSLydia Wang 	spec->mixers[spec->num_mixers++] = via_smart51_mixer;
4287*eb7188caSLydia Wang 
4288*eb7188caSLydia Wang 	return 1;
4289*eb7188caSLydia Wang }
4290*eb7188caSLydia Wang 
4291*eb7188caSLydia Wang #ifdef CONFIG_SND_HDA_POWER_SAVE
4292*eb7188caSLydia Wang static struct hda_amp_list vt1718S_loopbacks[] = {
4293*eb7188caSLydia Wang 	{ 0x21, HDA_INPUT, 1 },
4294*eb7188caSLydia Wang 	{ 0x21, HDA_INPUT, 2 },
4295*eb7188caSLydia Wang 	{ 0x21, HDA_INPUT, 3 },
4296*eb7188caSLydia Wang 	{ 0x21, HDA_INPUT, 4 },
4297*eb7188caSLydia Wang 	{ } /* end */
4298*eb7188caSLydia Wang };
4299*eb7188caSLydia Wang #endif
4300*eb7188caSLydia Wang 
4301*eb7188caSLydia Wang static int patch_vt1718S(struct hda_codec *codec)
4302*eb7188caSLydia Wang {
4303*eb7188caSLydia Wang 	struct via_spec *spec;
4304*eb7188caSLydia Wang 	int err;
4305*eb7188caSLydia Wang 
4306*eb7188caSLydia Wang 	/* create a codec specific record */
4307*eb7188caSLydia Wang 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4308*eb7188caSLydia Wang 	if (spec == NULL)
4309*eb7188caSLydia Wang 		return -ENOMEM;
4310*eb7188caSLydia Wang 
4311*eb7188caSLydia Wang 	codec->spec = spec;
4312*eb7188caSLydia Wang 
4313*eb7188caSLydia Wang 	/* automatic parse from the BIOS config */
4314*eb7188caSLydia Wang 	err = vt1718S_parse_auto_config(codec);
4315*eb7188caSLydia Wang 	if (err < 0) {
4316*eb7188caSLydia Wang 		via_free(codec);
4317*eb7188caSLydia Wang 		return err;
4318*eb7188caSLydia Wang 	} else if (!err) {
4319*eb7188caSLydia Wang 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
4320*eb7188caSLydia Wang 		       "from BIOS.  Using genenic mode...\n");
4321*eb7188caSLydia Wang 	}
4322*eb7188caSLydia Wang 
4323*eb7188caSLydia Wang 	spec->init_verbs[spec->num_iverbs++] = vt1718S_volume_init_verbs;
4324*eb7188caSLydia Wang 	spec->init_verbs[spec->num_iverbs++] = vt1718S_uniwill_init_verbs;
4325*eb7188caSLydia Wang 
4326*eb7188caSLydia Wang 	spec->stream_name_analog = "VT1718S Analog";
4327*eb7188caSLydia Wang 	spec->stream_analog_playback = &vt1718S_pcm_analog_playback;
4328*eb7188caSLydia Wang 	spec->stream_analog_capture = &vt1718S_pcm_analog_capture;
4329*eb7188caSLydia Wang 
4330*eb7188caSLydia Wang 	spec->stream_name_digital = "VT1718S Digital";
4331*eb7188caSLydia Wang 	spec->stream_digital_playback = &vt1718S_pcm_digital_playback;
4332*eb7188caSLydia Wang 	if (codec->vendor_id == 0x11060428)
4333*eb7188caSLydia Wang 		spec->stream_digital_capture = &vt1718S_pcm_digital_capture;
4334*eb7188caSLydia Wang 
4335*eb7188caSLydia Wang 	if (!spec->adc_nids && spec->input_mux) {
4336*eb7188caSLydia Wang 		spec->adc_nids = vt1718S_adc_nids;
4337*eb7188caSLydia Wang 		spec->num_adc_nids = ARRAY_SIZE(vt1718S_adc_nids);
4338*eb7188caSLydia Wang 		get_mux_nids(codec);
4339*eb7188caSLydia Wang 		override_mic_boost(codec, 0x1a, 0, 3, 40);
4340*eb7188caSLydia Wang 		override_mic_boost(codec, 0x1e, 0, 3, 40);
4341*eb7188caSLydia Wang 		spec->mixers[spec->num_mixers] = vt1718S_capture_mixer;
4342*eb7188caSLydia Wang 		spec->num_mixers++;
4343*eb7188caSLydia Wang 	}
4344*eb7188caSLydia Wang 
4345*eb7188caSLydia Wang 	codec->patch_ops = via_patch_ops;
4346*eb7188caSLydia Wang 
4347*eb7188caSLydia Wang 	codec->patch_ops.init = via_auto_init;
4348*eb7188caSLydia Wang 	codec->patch_ops.unsol_event = via_unsol_event,
4349*eb7188caSLydia Wang 
4350*eb7188caSLydia Wang #ifdef CONFIG_SND_HDA_POWER_SAVE
4351*eb7188caSLydia Wang 	spec->loopback.amplist = vt1718S_loopbacks;
4352*eb7188caSLydia Wang #endif
4353*eb7188caSLydia Wang 
4354*eb7188caSLydia Wang 	return 0;
4355*eb7188caSLydia Wang }
4356c577b8a1SJoseph Chan /*
4357c577b8a1SJoseph Chan  * patch entries
4358c577b8a1SJoseph Chan  */
43591289e9e8STakashi Iwai static struct hda_codec_preset snd_hda_preset_via[] = {
43603218c178STakashi Iwai 	{ .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
43613218c178STakashi Iwai 	{ .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
43623218c178STakashi Iwai 	{ .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
43633218c178STakashi Iwai 	{ .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
43643218c178STakashi Iwai 	{ .id = 0x1106e710, .name = "VT1709 10-Ch",
4365f7278fd0SJosepch Chan 	  .patch = patch_vt1709_10ch},
43663218c178STakashi Iwai 	{ .id = 0x1106e711, .name = "VT1709 10-Ch",
4367f7278fd0SJosepch Chan 	  .patch = patch_vt1709_10ch},
43683218c178STakashi Iwai 	{ .id = 0x1106e712, .name = "VT1709 10-Ch",
4369f7278fd0SJosepch Chan 	  .patch = patch_vt1709_10ch},
43703218c178STakashi Iwai 	{ .id = 0x1106e713, .name = "VT1709 10-Ch",
4371f7278fd0SJosepch Chan 	  .patch = patch_vt1709_10ch},
43723218c178STakashi Iwai 	{ .id = 0x1106e714, .name = "VT1709 6-Ch",
4373f7278fd0SJosepch Chan 	  .patch = patch_vt1709_6ch},
43743218c178STakashi Iwai 	{ .id = 0x1106e715, .name = "VT1709 6-Ch",
4375f7278fd0SJosepch Chan 	  .patch = patch_vt1709_6ch},
43763218c178STakashi Iwai 	{ .id = 0x1106e716, .name = "VT1709 6-Ch",
4377f7278fd0SJosepch Chan 	  .patch = patch_vt1709_6ch},
43783218c178STakashi Iwai 	{ .id = 0x1106e717, .name = "VT1709 6-Ch",
4379f7278fd0SJosepch Chan 	  .patch = patch_vt1709_6ch},
43803218c178STakashi Iwai 	{ .id = 0x1106e720, .name = "VT1708B 8-Ch",
4381f7278fd0SJosepch Chan 	  .patch = patch_vt1708B_8ch},
43823218c178STakashi Iwai 	{ .id = 0x1106e721, .name = "VT1708B 8-Ch",
4383f7278fd0SJosepch Chan 	  .patch = patch_vt1708B_8ch},
43843218c178STakashi Iwai 	{ .id = 0x1106e722, .name = "VT1708B 8-Ch",
4385f7278fd0SJosepch Chan 	  .patch = patch_vt1708B_8ch},
43863218c178STakashi Iwai 	{ .id = 0x1106e723, .name = "VT1708B 8-Ch",
4387f7278fd0SJosepch Chan 	  .patch = patch_vt1708B_8ch},
43883218c178STakashi Iwai 	{ .id = 0x1106e724, .name = "VT1708B 4-Ch",
4389f7278fd0SJosepch Chan 	  .patch = patch_vt1708B_4ch},
43903218c178STakashi Iwai 	{ .id = 0x1106e725, .name = "VT1708B 4-Ch",
4391f7278fd0SJosepch Chan 	  .patch = patch_vt1708B_4ch},
43923218c178STakashi Iwai 	{ .id = 0x1106e726, .name = "VT1708B 4-Ch",
4393f7278fd0SJosepch Chan 	  .patch = patch_vt1708B_4ch},
43943218c178STakashi Iwai 	{ .id = 0x1106e727, .name = "VT1708B 4-Ch",
4395f7278fd0SJosepch Chan 	  .patch = patch_vt1708B_4ch},
43963218c178STakashi Iwai 	{ .id = 0x11060397, .name = "VT1708S",
4397d949cac1SHarald Welte 	  .patch = patch_vt1708S},
43983218c178STakashi Iwai 	{ .id = 0x11061397, .name = "VT1708S",
4399d949cac1SHarald Welte 	  .patch = patch_vt1708S},
44003218c178STakashi Iwai 	{ .id = 0x11062397, .name = "VT1708S",
4401d949cac1SHarald Welte 	  .patch = patch_vt1708S},
44023218c178STakashi Iwai 	{ .id = 0x11063397, .name = "VT1708S",
4403d949cac1SHarald Welte 	  .patch = patch_vt1708S},
44043218c178STakashi Iwai 	{ .id = 0x11064397, .name = "VT1708S",
4405d949cac1SHarald Welte 	  .patch = patch_vt1708S},
44063218c178STakashi Iwai 	{ .id = 0x11065397, .name = "VT1708S",
4407d949cac1SHarald Welte 	  .patch = patch_vt1708S},
44083218c178STakashi Iwai 	{ .id = 0x11066397, .name = "VT1708S",
4409d949cac1SHarald Welte 	  .patch = patch_vt1708S},
44103218c178STakashi Iwai 	{ .id = 0x11067397, .name = "VT1708S",
4411d949cac1SHarald Welte 	  .patch = patch_vt1708S},
44123218c178STakashi Iwai 	{ .id = 0x11060398, .name = "VT1702",
4413d949cac1SHarald Welte 	  .patch = patch_vt1702},
44143218c178STakashi Iwai 	{ .id = 0x11061398, .name = "VT1702",
4415d949cac1SHarald Welte 	  .patch = patch_vt1702},
44163218c178STakashi Iwai 	{ .id = 0x11062398, .name = "VT1702",
4417d949cac1SHarald Welte 	  .patch = patch_vt1702},
44183218c178STakashi Iwai 	{ .id = 0x11063398, .name = "VT1702",
4419d949cac1SHarald Welte 	  .patch = patch_vt1702},
44203218c178STakashi Iwai 	{ .id = 0x11064398, .name = "VT1702",
4421d949cac1SHarald Welte 	  .patch = patch_vt1702},
44223218c178STakashi Iwai 	{ .id = 0x11065398, .name = "VT1702",
4423d949cac1SHarald Welte 	  .patch = patch_vt1702},
44243218c178STakashi Iwai 	{ .id = 0x11066398, .name = "VT1702",
4425d949cac1SHarald Welte 	  .patch = patch_vt1702},
44263218c178STakashi Iwai 	{ .id = 0x11067398, .name = "VT1702",
4427d949cac1SHarald Welte 	  .patch = patch_vt1702},
4428*eb7188caSLydia Wang 	{ .id = 0x11060428, .name = "VT1718S",
4429*eb7188caSLydia Wang 	  .patch = patch_vt1718S},
4430*eb7188caSLydia Wang 	{ .id = 0x11064428, .name = "VT1718S",
4431*eb7188caSLydia Wang 	  .patch = patch_vt1718S},
4432c577b8a1SJoseph Chan 	{} /* terminator */
4433c577b8a1SJoseph Chan };
44341289e9e8STakashi Iwai 
44351289e9e8STakashi Iwai MODULE_ALIAS("snd-hda-codec-id:1106*");
44361289e9e8STakashi Iwai 
44371289e9e8STakashi Iwai static struct hda_codec_preset_list via_list = {
44381289e9e8STakashi Iwai 	.preset = snd_hda_preset_via,
44391289e9e8STakashi Iwai 	.owner = THIS_MODULE,
44401289e9e8STakashi Iwai };
44411289e9e8STakashi Iwai 
44421289e9e8STakashi Iwai MODULE_LICENSE("GPL");
44431289e9e8STakashi Iwai MODULE_DESCRIPTION("VIA HD-audio codec");
44441289e9e8STakashi Iwai 
44451289e9e8STakashi Iwai static int __init patch_via_init(void)
44461289e9e8STakashi Iwai {
44471289e9e8STakashi Iwai 	return snd_hda_add_codec_preset(&via_list);
44481289e9e8STakashi Iwai }
44491289e9e8STakashi Iwai 
44501289e9e8STakashi Iwai static void __exit patch_via_exit(void)
44511289e9e8STakashi Iwai {
44521289e9e8STakashi Iwai 	snd_hda_delete_codec_preset(&via_list);
44531289e9e8STakashi Iwai }
44541289e9e8STakashi Iwai 
44551289e9e8STakashi Iwai module_init(patch_via_init)
44561289e9e8STakashi Iwai module_exit(patch_via_exit)
4457