xref: /openbmc/linux/sound/pci/hda/patch_via.c (revision 05bcf503)
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * HD audio interface patch for VIA VT17xx/VT18xx/VT20xx codec
5  *
6  *  (C) 2006-2009 VIA Technology, Inc.
7  *  (C) 2006-2008 Takashi Iwai <tiwai@suse.de>
8  *
9  *  This driver is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This driver is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  */
23 
24 /* * * * * * * * * * * * * * Release History * * * * * * * * * * * * * * * * */
25 /*									     */
26 /* 2006-03-03  Lydia Wang  Create the basic patch to support VT1708 codec    */
27 /* 2006-03-14  Lydia Wang  Modify hard code for some pin widget nid	     */
28 /* 2006-08-02  Lydia Wang  Add support to VT1709 codec			     */
29 /* 2006-09-08  Lydia Wang  Fix internal loopback recording source select bug */
30 /* 2007-09-12  Lydia Wang  Add EAPD enable during driver initialization	     */
31 /* 2007-09-17  Lydia Wang  Add VT1708B codec support			    */
32 /* 2007-11-14  Lydia Wang  Add VT1708A codec HP and CD pin connect config    */
33 /* 2008-02-03  Lydia Wang  Fix Rear channels and Back channels inverse issue */
34 /* 2008-03-06  Lydia Wang  Add VT1702 codec and VT1708S codec support	     */
35 /* 2008-04-09  Lydia Wang  Add mute front speaker when HP plugin	     */
36 /* 2008-04-09  Lydia Wang  Add Independent HP feature			     */
37 /* 2008-05-28  Lydia Wang  Add second S/PDIF Out support for VT1702	     */
38 /* 2008-09-15  Logan Li	   Add VT1708S Mic Boost workaround/backdoor	     */
39 /* 2009-02-16  Logan Li	   Add support for VT1718S			     */
40 /* 2009-03-13  Logan Li	   Add support for VT1716S			     */
41 /* 2009-04-14  Lydai Wang  Add support for VT1828S and VT2020		     */
42 /* 2009-07-08  Lydia Wang  Add support for VT2002P			     */
43 /* 2009-07-21  Lydia Wang  Add support for VT1812			     */
44 /* 2009-09-19  Lydia Wang  Add support for VT1818S			     */
45 /*									     */
46 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
47 
48 
49 #include <linux/init.h>
50 #include <linux/delay.h>
51 #include <linux/slab.h>
52 #include <linux/module.h>
53 #include <sound/core.h>
54 #include <sound/asoundef.h>
55 #include "hda_codec.h"
56 #include "hda_local.h"
57 #include "hda_auto_parser.h"
58 #include "hda_jack.h"
59 
60 /* Pin Widget NID */
61 #define VT1708_HP_PIN_NID	0x20
62 #define VT1708_CD_PIN_NID	0x24
63 
64 enum VIA_HDA_CODEC {
65 	UNKNOWN = -1,
66 	VT1708,
67 	VT1709_10CH,
68 	VT1709_6CH,
69 	VT1708B_8CH,
70 	VT1708B_4CH,
71 	VT1708S,
72 	VT1708BCE,
73 	VT1702,
74 	VT1718S,
75 	VT1716S,
76 	VT2002P,
77 	VT1812,
78 	VT1802,
79 	CODEC_TYPES,
80 };
81 
82 #define VT2002P_COMPATIBLE(spec) \
83 	((spec)->codec_type == VT2002P ||\
84 	 (spec)->codec_type == VT1812 ||\
85 	 (spec)->codec_type == VT1802)
86 
87 #define MAX_NID_PATH_DEPTH	5
88 
89 /* output-path: DAC -> ... -> pin
90  * idx[] contains the source index number of the next widget;
91  * e.g. idx[0] is the index of the DAC selected by path[1] widget
92  * multi[] indicates whether it's a selector widget with multi-connectors
93  * (i.e. the connection selection is mandatory)
94  * vol_ctl and mute_ctl contains the NIDs for the assigned mixers
95  */
96 struct nid_path {
97 	int depth;
98 	hda_nid_t path[MAX_NID_PATH_DEPTH];
99 	unsigned char idx[MAX_NID_PATH_DEPTH];
100 	unsigned char multi[MAX_NID_PATH_DEPTH];
101 	unsigned int vol_ctl;
102 	unsigned int mute_ctl;
103 };
104 
105 /* input-path */
106 struct via_input {
107 	hda_nid_t pin;	/* input-pin or aa-mix */
108 	int adc_idx;	/* ADC index to be used */
109 	int mux_idx;	/* MUX index (if any) */
110 	const char *label;	/* input-source label */
111 };
112 
113 #define VIA_MAX_ADCS	3
114 
115 enum {
116 	STREAM_MULTI_OUT = (1 << 0),
117 	STREAM_INDEP_HP = (1 << 1),
118 };
119 
120 struct via_spec {
121 	struct hda_gen_spec gen;
122 
123 	/* codec parameterization */
124 	const struct snd_kcontrol_new *mixers[6];
125 	unsigned int num_mixers;
126 
127 	const struct hda_verb *init_verbs[5];
128 	unsigned int num_iverbs;
129 
130 	char stream_name_analog[32];
131 	char stream_name_hp[32];
132 	const struct hda_pcm_stream *stream_analog_playback;
133 	const struct hda_pcm_stream *stream_analog_capture;
134 
135 	char stream_name_digital[32];
136 	const struct hda_pcm_stream *stream_digital_playback;
137 	const struct hda_pcm_stream *stream_digital_capture;
138 
139 	/* playback */
140 	struct hda_multi_out multiout;
141 	hda_nid_t slave_dig_outs[2];
142 	hda_nid_t hp_dac_nid;
143 	hda_nid_t speaker_dac_nid;
144 	int hp_indep_shared;	/* indep HP-DAC is shared with side ch */
145 	int opened_streams;	/* STREAM_* bits */
146 	int active_streams;	/* STREAM_* bits */
147 	int aamix_mode;		/* loopback is enabled for output-path? */
148 
149 	/* Output-paths:
150 	 * There are different output-paths depending on the setup.
151 	 * out_path, hp_path and speaker_path are primary paths.  If both
152 	 * direct DAC and aa-loopback routes are available, these contain
153 	 * the former paths.  Meanwhile *_mix_path contain the paths with
154 	 * loopback mixer.  (Since the loopback is only for front channel,
155 	 * no out_mix_path for surround channels.)
156 	 * The HP output has another path, hp_indep_path, which is used in
157 	 * the independent-HP mode.
158 	 */
159 	struct nid_path out_path[HDA_SIDE + 1];
160 	struct nid_path out_mix_path;
161 	struct nid_path hp_path;
162 	struct nid_path hp_mix_path;
163 	struct nid_path hp_indep_path;
164 	struct nid_path speaker_path;
165 	struct nid_path speaker_mix_path;
166 
167 	/* capture */
168 	unsigned int num_adc_nids;
169 	hda_nid_t adc_nids[VIA_MAX_ADCS];
170 	hda_nid_t mux_nids[VIA_MAX_ADCS];
171 	hda_nid_t aa_mix_nid;
172 	hda_nid_t dig_in_nid;
173 
174 	/* capture source */
175 	bool dyn_adc_switch;
176 	int num_inputs;
177 	struct via_input inputs[AUTO_CFG_MAX_INS + 1];
178 	unsigned int cur_mux[VIA_MAX_ADCS];
179 
180 	/* dynamic DAC switching */
181 	unsigned int cur_dac_stream_tag;
182 	unsigned int cur_dac_format;
183 	unsigned int cur_hp_stream_tag;
184 	unsigned int cur_hp_format;
185 
186 	/* dynamic ADC switching */
187 	hda_nid_t cur_adc;
188 	unsigned int cur_adc_stream_tag;
189 	unsigned int cur_adc_format;
190 
191 	/* PCM information */
192 	struct hda_pcm pcm_rec[3];
193 
194 	/* dynamic controls, init_verbs and input_mux */
195 	struct auto_pin_cfg autocfg;
196 	struct snd_array kctls;
197 	hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
198 
199 	/* HP mode source */
200 	unsigned int hp_independent_mode;
201 	unsigned int dmic_enabled;
202 	unsigned int no_pin_power_ctl;
203 	enum VIA_HDA_CODEC codec_type;
204 
205 	/* analog low-power control */
206 	bool alc_mode;
207 
208 	/* smart51 setup */
209 	unsigned int smart51_nums;
210 	hda_nid_t smart51_pins[2];
211 	int smart51_idxs[2];
212 	const char *smart51_labels[2];
213 	unsigned int smart51_enabled;
214 
215 	/* work to check hp jack state */
216 	struct hda_codec *codec;
217 	struct delayed_work vt1708_hp_work;
218 	int hp_work_active;
219 	int vt1708_jack_detect;
220 	int vt1708_hp_present;
221 
222 	void (*set_widgets_power_state)(struct hda_codec *codec);
223 
224 	struct hda_loopback_check loopback;
225 	int num_loopbacks;
226 	struct hda_amp_list loopback_list[8];
227 
228 	/* bind capture-volume */
229 	struct hda_bind_ctls *bind_cap_vol;
230 	struct hda_bind_ctls *bind_cap_sw;
231 
232 	struct mutex config_mutex;
233 };
234 
235 static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
236 static struct via_spec * via_new_spec(struct hda_codec *codec)
237 {
238 	struct via_spec *spec;
239 
240 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
241 	if (spec == NULL)
242 		return NULL;
243 
244 	snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
245 	mutex_init(&spec->config_mutex);
246 	codec->spec = spec;
247 	spec->codec = codec;
248 	spec->codec_type = get_codec_type(codec);
249 	/* VT1708BCE & VT1708S are almost same */
250 	if (spec->codec_type == VT1708BCE)
251 		spec->codec_type = VT1708S;
252 	snd_hda_gen_init(&spec->gen);
253 	return spec;
254 }
255 
256 static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
257 {
258 	u32 vendor_id = codec->vendor_id;
259 	u16 ven_id = vendor_id >> 16;
260 	u16 dev_id = vendor_id & 0xffff;
261 	enum VIA_HDA_CODEC codec_type;
262 
263 	/* get codec type */
264 	if (ven_id != 0x1106)
265 		codec_type = UNKNOWN;
266 	else if (dev_id >= 0x1708 && dev_id <= 0x170b)
267 		codec_type = VT1708;
268 	else if (dev_id >= 0xe710 && dev_id <= 0xe713)
269 		codec_type = VT1709_10CH;
270 	else if (dev_id >= 0xe714 && dev_id <= 0xe717)
271 		codec_type = VT1709_6CH;
272 	else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
273 		codec_type = VT1708B_8CH;
274 		if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
275 			codec_type = VT1708BCE;
276 	} else if (dev_id >= 0xe724 && dev_id <= 0xe727)
277 		codec_type = VT1708B_4CH;
278 	else if ((dev_id & 0xfff) == 0x397
279 		 && (dev_id >> 12) < 8)
280 		codec_type = VT1708S;
281 	else if ((dev_id & 0xfff) == 0x398
282 		 && (dev_id >> 12) < 8)
283 		codec_type = VT1702;
284 	else if ((dev_id & 0xfff) == 0x428
285 		 && (dev_id >> 12) < 8)
286 		codec_type = VT1718S;
287 	else if (dev_id == 0x0433 || dev_id == 0xa721)
288 		codec_type = VT1716S;
289 	else if (dev_id == 0x0441 || dev_id == 0x4441)
290 		codec_type = VT1718S;
291 	else if (dev_id == 0x0438 || dev_id == 0x4438)
292 		codec_type = VT2002P;
293 	else if (dev_id == 0x0448)
294 		codec_type = VT1812;
295 	else if (dev_id == 0x0440)
296 		codec_type = VT1708S;
297 	else if ((dev_id & 0xfff) == 0x446)
298 		codec_type = VT1802;
299 	else
300 		codec_type = UNKNOWN;
301 	return codec_type;
302 };
303 
304 #define VIA_JACK_EVENT		0x20
305 #define VIA_HP_EVENT		0x01
306 #define VIA_LINE_EVENT		0x03
307 
308 enum {
309 	VIA_CTL_WIDGET_VOL,
310 	VIA_CTL_WIDGET_MUTE,
311 	VIA_CTL_WIDGET_ANALOG_MUTE,
312 };
313 
314 static void analog_low_current_mode(struct hda_codec *codec);
315 static bool is_aa_path_mute(struct hda_codec *codec);
316 
317 #define hp_detect_with_aa(codec) \
318 	(snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1 && \
319 	 !is_aa_path_mute(codec))
320 
321 static void vt1708_stop_hp_work(struct via_spec *spec)
322 {
323 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
324 		return;
325 	if (spec->hp_work_active) {
326 		snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 1);
327 		cancel_delayed_work_sync(&spec->vt1708_hp_work);
328 		spec->hp_work_active = 0;
329 	}
330 }
331 
332 static void vt1708_update_hp_work(struct via_spec *spec)
333 {
334 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
335 		return;
336 	if (spec->vt1708_jack_detect &&
337 	    (spec->active_streams || hp_detect_with_aa(spec->codec))) {
338 		if (!spec->hp_work_active) {
339 			snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 0);
340 			schedule_delayed_work(&spec->vt1708_hp_work,
341 					      msecs_to_jiffies(100));
342 			spec->hp_work_active = 1;
343 		}
344 	} else if (!hp_detect_with_aa(spec->codec))
345 		vt1708_stop_hp_work(spec);
346 }
347 
348 static void set_widgets_power_state(struct hda_codec *codec)
349 {
350 	struct via_spec *spec = codec->spec;
351 	if (spec->set_widgets_power_state)
352 		spec->set_widgets_power_state(codec);
353 }
354 
355 static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
356 				   struct snd_ctl_elem_value *ucontrol)
357 {
358 	int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
359 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
360 
361 	set_widgets_power_state(codec);
362 	analog_low_current_mode(snd_kcontrol_chip(kcontrol));
363 	vt1708_update_hp_work(codec->spec);
364 	return change;
365 }
366 
367 /* modify .put = snd_hda_mixer_amp_switch_put */
368 #define ANALOG_INPUT_MUTE						\
369 	{		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,		\
370 			.name = NULL,					\
371 			.index = 0,					\
372 			.info = snd_hda_mixer_amp_switch_info,		\
373 			.get = snd_hda_mixer_amp_switch_get,		\
374 			.put = analog_input_switch_put,			\
375 			.private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
376 
377 static const struct snd_kcontrol_new via_control_templates[] = {
378 	HDA_CODEC_VOLUME(NULL, 0, 0, 0),
379 	HDA_CODEC_MUTE(NULL, 0, 0, 0),
380 	ANALOG_INPUT_MUTE,
381 };
382 
383 
384 /* add dynamic controls */
385 static struct snd_kcontrol_new *__via_clone_ctl(struct via_spec *spec,
386 				const struct snd_kcontrol_new *tmpl,
387 				const char *name)
388 {
389 	struct snd_kcontrol_new *knew;
390 
391 	knew = snd_array_new(&spec->kctls);
392 	if (!knew)
393 		return NULL;
394 	*knew = *tmpl;
395 	if (!name)
396 		name = tmpl->name;
397 	if (name) {
398 		knew->name = kstrdup(name, GFP_KERNEL);
399 		if (!knew->name)
400 			return NULL;
401 	}
402 	return knew;
403 }
404 
405 static int __via_add_control(struct via_spec *spec, int type, const char *name,
406 			     int idx, unsigned long val)
407 {
408 	struct snd_kcontrol_new *knew;
409 
410 	knew = __via_clone_ctl(spec, &via_control_templates[type], name);
411 	if (!knew)
412 		return -ENOMEM;
413 	knew->index = idx;
414 	if (get_amp_nid_(val))
415 		knew->subdevice = HDA_SUBDEV_AMP_FLAG;
416 	knew->private_value = val;
417 	return 0;
418 }
419 
420 #define via_add_control(spec, type, name, val) \
421 	__via_add_control(spec, type, name, 0, val)
422 
423 #define via_clone_control(spec, tmpl) __via_clone_ctl(spec, tmpl, NULL)
424 
425 static void via_free_kctls(struct hda_codec *codec)
426 {
427 	struct via_spec *spec = codec->spec;
428 
429 	if (spec->kctls.list) {
430 		struct snd_kcontrol_new *kctl = spec->kctls.list;
431 		int i;
432 		for (i = 0; i < spec->kctls.used; i++)
433 			kfree(kctl[i].name);
434 	}
435 	snd_array_free(&spec->kctls);
436 }
437 
438 /* create input playback/capture controls for the given pin */
439 static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
440 				int type_idx, int idx, int mix_nid)
441 {
442 	char name[32];
443 	int err;
444 
445 	sprintf(name, "%s Playback Volume", ctlname);
446 	err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
447 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
448 	if (err < 0)
449 		return err;
450 	sprintf(name, "%s Playback Switch", ctlname);
451 	err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
452 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
453 	if (err < 0)
454 		return err;
455 	return 0;
456 }
457 
458 #define get_connection_index(codec, mux, nid) \
459 	snd_hda_get_conn_index(codec, mux, nid, 0)
460 
461 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
462 			   unsigned int mask)
463 {
464 	unsigned int caps;
465 	if (!nid)
466 		return false;
467 	caps = get_wcaps(codec, nid);
468 	if (dir == HDA_INPUT)
469 		caps &= AC_WCAP_IN_AMP;
470 	else
471 		caps &= AC_WCAP_OUT_AMP;
472 	if (!caps)
473 		return false;
474 	if (query_amp_caps(codec, nid, dir) & mask)
475 		return true;
476 	return false;
477 }
478 
479 #define have_mute(codec, nid, dir) \
480 	check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
481 
482 /* enable/disable the output-route mixers */
483 static void activate_output_mix(struct hda_codec *codec, struct nid_path *path,
484 				hda_nid_t mix_nid, int idx, bool enable)
485 {
486 	int i, num, val;
487 
488 	if (!path)
489 		return;
490 	num = snd_hda_get_num_conns(codec, mix_nid);
491 	for (i = 0; i < num; i++) {
492 		if (i == idx)
493 			val = AMP_IN_UNMUTE(i);
494 		else
495 			val = AMP_IN_MUTE(i);
496 		snd_hda_codec_write(codec, mix_nid, 0,
497 				    AC_VERB_SET_AMP_GAIN_MUTE, val);
498 	}
499 }
500 
501 /* enable/disable the output-route */
502 static void activate_output_path(struct hda_codec *codec, struct nid_path *path,
503 				 bool enable, bool force)
504 {
505 	struct via_spec *spec = codec->spec;
506 	int i;
507 	for (i = 0; i < path->depth; i++) {
508 		hda_nid_t src, dst;
509 		int idx = path->idx[i];
510 		src = path->path[i];
511 		if (i < path->depth - 1)
512 			dst = path->path[i + 1];
513 		else
514 			dst = 0;
515 		if (enable && path->multi[i])
516 			snd_hda_codec_write(codec, dst, 0,
517 					    AC_VERB_SET_CONNECT_SEL, idx);
518 		if (!force && (dst == spec->aa_mix_nid))
519 			continue;
520 		if (have_mute(codec, dst, HDA_INPUT))
521 			activate_output_mix(codec, path, dst, idx, enable);
522 		if (!force && (src == path->vol_ctl || src == path->mute_ctl))
523 			continue;
524 		if (have_mute(codec, src, HDA_OUTPUT)) {
525 			int val = enable ? AMP_OUT_UNMUTE : AMP_OUT_MUTE;
526 			snd_hda_codec_write(codec, src, 0,
527 					    AC_VERB_SET_AMP_GAIN_MUTE, val);
528 		}
529 	}
530 }
531 
532 /* set the given pin as output */
533 static void init_output_pin(struct hda_codec *codec, hda_nid_t pin,
534 			    int pin_type)
535 {
536 	if (!pin)
537 		return;
538 	snd_hda_set_pin_ctl(codec, pin, pin_type);
539 	if (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD)
540 		snd_hda_codec_write(codec, pin, 0,
541 				    AC_VERB_SET_EAPD_BTLENABLE, 0x02);
542 }
543 
544 static void via_auto_init_output(struct hda_codec *codec,
545 				 struct nid_path *path, int pin_type)
546 {
547 	unsigned int caps;
548 	hda_nid_t pin;
549 
550 	if (!path->depth)
551 		return;
552 	pin = path->path[path->depth - 1];
553 
554 	init_output_pin(codec, pin, pin_type);
555 	if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
556 		caps = query_amp_caps(codec, pin, HDA_OUTPUT);
557 	else
558 		caps = 0;
559 	if (caps & AC_AMPCAP_MUTE) {
560 		unsigned int val;
561 		val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
562 		snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
563 				    AMP_OUT_MUTE | val);
564 	}
565 	activate_output_path(codec, path, true, true); /* force on */
566 }
567 
568 static void via_auto_init_multi_out(struct hda_codec *codec)
569 {
570 	struct via_spec *spec = codec->spec;
571 	struct nid_path *path;
572 	int i;
573 
574 	for (i = 0; i < spec->autocfg.line_outs + spec->smart51_nums; i++) {
575 		path = &spec->out_path[i];
576 		if (!i && spec->aamix_mode && spec->out_mix_path.depth)
577 			path = &spec->out_mix_path;
578 		via_auto_init_output(codec, path, PIN_OUT);
579 	}
580 }
581 
582 /* deactivate the inactive headphone-paths */
583 static void deactivate_hp_paths(struct hda_codec *codec)
584 {
585 	struct via_spec *spec = codec->spec;
586 	int shared = spec->hp_indep_shared;
587 
588 	if (spec->hp_independent_mode) {
589 		activate_output_path(codec, &spec->hp_path, false, false);
590 		activate_output_path(codec, &spec->hp_mix_path, false, false);
591 		if (shared)
592 			activate_output_path(codec, &spec->out_path[shared],
593 					     false, false);
594 	} else if (spec->aamix_mode || !spec->hp_path.depth) {
595 		activate_output_path(codec, &spec->hp_indep_path, false, false);
596 		activate_output_path(codec, &spec->hp_path, false, false);
597 	} else {
598 		activate_output_path(codec, &spec->hp_indep_path, false, false);
599 		activate_output_path(codec, &spec->hp_mix_path, false, false);
600 	}
601 }
602 
603 static void via_auto_init_hp_out(struct hda_codec *codec)
604 {
605 	struct via_spec *spec = codec->spec;
606 
607 	if (!spec->hp_path.depth) {
608 		via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
609 		return;
610 	}
611 	deactivate_hp_paths(codec);
612 	if (spec->hp_independent_mode)
613 		via_auto_init_output(codec, &spec->hp_indep_path, PIN_HP);
614 	else if (spec->aamix_mode)
615 		via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
616 	else
617 		via_auto_init_output(codec, &spec->hp_path, PIN_HP);
618 }
619 
620 static void via_auto_init_speaker_out(struct hda_codec *codec)
621 {
622 	struct via_spec *spec = codec->spec;
623 
624 	if (!spec->autocfg.speaker_outs)
625 		return;
626 	if (!spec->speaker_path.depth) {
627 		via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
628 		return;
629 	}
630 	if (!spec->aamix_mode) {
631 		activate_output_path(codec, &spec->speaker_mix_path,
632 				     false, false);
633 		via_auto_init_output(codec, &spec->speaker_path, PIN_OUT);
634 	} else {
635 		activate_output_path(codec, &spec->speaker_path, false, false);
636 		via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
637 	}
638 }
639 
640 static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin);
641 static void via_hp_automute(struct hda_codec *codec);
642 
643 static void via_auto_init_analog_input(struct hda_codec *codec)
644 {
645 	struct via_spec *spec = codec->spec;
646 	const struct auto_pin_cfg *cfg = &spec->autocfg;
647 	hda_nid_t conn[HDA_MAX_CONNECTIONS];
648 	unsigned int ctl;
649 	int i, num_conns;
650 
651 	/* init ADCs */
652 	for (i = 0; i < spec->num_adc_nids; i++) {
653 		hda_nid_t nid = spec->adc_nids[i];
654 		if (!(get_wcaps(codec, nid) & AC_WCAP_IN_AMP) ||
655 		    !(query_amp_caps(codec, nid, HDA_INPUT) & AC_AMPCAP_MUTE))
656 			continue;
657 		snd_hda_codec_write(codec, spec->adc_nids[i], 0,
658 				    AC_VERB_SET_AMP_GAIN_MUTE,
659 				    AMP_IN_UNMUTE(0));
660 	}
661 
662 	/* init pins */
663 	for (i = 0; i < cfg->num_inputs; i++) {
664 		hda_nid_t nid = cfg->inputs[i].pin;
665 		if (spec->smart51_enabled && is_smart51_pins(codec, nid))
666 			ctl = PIN_OUT;
667 		else {
668 			ctl = PIN_IN;
669 			if (cfg->inputs[i].type == AUTO_PIN_MIC)
670 				ctl |= snd_hda_get_default_vref(codec, nid);
671 		}
672 		snd_hda_set_pin_ctl(codec, nid, ctl);
673 	}
674 
675 	/* init input-src */
676 	for (i = 0; i < spec->num_adc_nids; i++) {
677 		int adc_idx = spec->inputs[spec->cur_mux[i]].adc_idx;
678 		/* secondary ADCs must have the unique MUX */
679 		if (i > 0 && !spec->mux_nids[i])
680 			break;
681 		if (spec->mux_nids[adc_idx]) {
682 			int mux_idx = spec->inputs[spec->cur_mux[i]].mux_idx;
683 			snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
684 					    AC_VERB_SET_CONNECT_SEL,
685 					    mux_idx);
686 		}
687 		if (spec->dyn_adc_switch)
688 			break; /* only one input-src */
689 	}
690 
691 	/* init aa-mixer */
692 	if (!spec->aa_mix_nid)
693 		return;
694 	num_conns = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
695 					    ARRAY_SIZE(conn));
696 	for (i = 0; i < num_conns; i++) {
697 		unsigned int caps = get_wcaps(codec, conn[i]);
698 		if (get_wcaps_type(caps) == AC_WID_PIN)
699 			snd_hda_codec_write(codec, spec->aa_mix_nid, 0,
700 					    AC_VERB_SET_AMP_GAIN_MUTE,
701 					    AMP_IN_MUTE(i));
702 	}
703 }
704 
705 static void update_power_state(struct hda_codec *codec, hda_nid_t nid,
706 			       unsigned int parm)
707 {
708 	if (snd_hda_codec_read(codec, nid, 0,
709 			       AC_VERB_GET_POWER_STATE, 0) == parm)
710 		return;
711 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
712 }
713 
714 static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
715 				unsigned int *affected_parm)
716 {
717 	unsigned parm;
718 	unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
719 	unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
720 		>> AC_DEFCFG_MISC_SHIFT
721 		& AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
722 	struct via_spec *spec = codec->spec;
723 	unsigned present = 0;
724 
725 	no_presence |= spec->no_pin_power_ctl;
726 	if (!no_presence)
727 		present = snd_hda_jack_detect(codec, nid);
728 	if ((spec->smart51_enabled && is_smart51_pins(codec, nid))
729 	    || ((no_presence || present)
730 		&& get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
731 		*affected_parm = AC_PWRST_D0; /* if it's connected */
732 		parm = AC_PWRST_D0;
733 	} else
734 		parm = AC_PWRST_D3;
735 
736 	update_power_state(codec, nid, parm);
737 }
738 
739 static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
740 				  struct snd_ctl_elem_info *uinfo)
741 {
742 	return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
743 }
744 
745 static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
746 				 struct snd_ctl_elem_value *ucontrol)
747 {
748 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
749 	struct via_spec *spec = codec->spec;
750 	ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
751 	return 0;
752 }
753 
754 static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
755 				 struct snd_ctl_elem_value *ucontrol)
756 {
757 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
758 	struct via_spec *spec = codec->spec;
759 	unsigned int val = !ucontrol->value.enumerated.item[0];
760 
761 	if (val == spec->no_pin_power_ctl)
762 		return 0;
763 	spec->no_pin_power_ctl = val;
764 	set_widgets_power_state(codec);
765 	analog_low_current_mode(codec);
766 	return 1;
767 }
768 
769 static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
770 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
771 	.name = "Dynamic Power-Control",
772 	.info = via_pin_power_ctl_info,
773 	.get = via_pin_power_ctl_get,
774 	.put = via_pin_power_ctl_put,
775 };
776 
777 
778 static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
779 				   struct snd_ctl_elem_info *uinfo)
780 {
781 	static const char * const texts[] = { "OFF", "ON" };
782 
783 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
784 	uinfo->count = 1;
785 	uinfo->value.enumerated.items = 2;
786 	if (uinfo->value.enumerated.item >= 2)
787 		uinfo->value.enumerated.item = 1;
788 	strcpy(uinfo->value.enumerated.name,
789 	       texts[uinfo->value.enumerated.item]);
790 	return 0;
791 }
792 
793 static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
794 				  struct snd_ctl_elem_value *ucontrol)
795 {
796 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
797 	struct via_spec *spec = codec->spec;
798 
799 	ucontrol->value.enumerated.item[0] = spec->hp_independent_mode;
800 	return 0;
801 }
802 
803 /* adjust spec->multiout setup according to the current flags */
804 static void setup_playback_multi_pcm(struct via_spec *spec)
805 {
806 	const struct auto_pin_cfg *cfg = &spec->autocfg;
807 	spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
808 	spec->multiout.hp_nid = 0;
809 	if (!spec->hp_independent_mode) {
810 		if (!spec->hp_indep_shared)
811 			spec->multiout.hp_nid = spec->hp_dac_nid;
812 	} else {
813 		if (spec->hp_indep_shared)
814 			spec->multiout.num_dacs = cfg->line_outs - 1;
815 	}
816 }
817 
818 /* update DAC setups according to indep-HP switch;
819  * this function is called only when indep-HP is modified
820  */
821 static void switch_indep_hp_dacs(struct hda_codec *codec)
822 {
823 	struct via_spec *spec = codec->spec;
824 	int shared = spec->hp_indep_shared;
825 	hda_nid_t shared_dac, hp_dac;
826 
827 	if (!spec->opened_streams)
828 		return;
829 
830 	shared_dac = shared ? spec->multiout.dac_nids[shared] : 0;
831 	hp_dac = spec->hp_dac_nid;
832 	if (spec->hp_independent_mode) {
833 		/* switch to indep-HP mode */
834 		if (spec->active_streams & STREAM_MULTI_OUT) {
835 			__snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
836 			__snd_hda_codec_cleanup_stream(codec, shared_dac, 1);
837 		}
838 		if (spec->active_streams & STREAM_INDEP_HP)
839 			snd_hda_codec_setup_stream(codec, hp_dac,
840 						   spec->cur_hp_stream_tag, 0,
841 						   spec->cur_hp_format);
842 	} else {
843 		/* back to HP or shared-DAC */
844 		if (spec->active_streams & STREAM_INDEP_HP)
845 			__snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
846 		if (spec->active_streams & STREAM_MULTI_OUT) {
847 			hda_nid_t dac;
848 			int ch;
849 			if (shared_dac) { /* reset mutli-ch DAC */
850 				dac = shared_dac;
851 				ch = shared * 2;
852 			} else { /* reset HP DAC */
853 				dac = hp_dac;
854 				ch = 0;
855 			}
856 			snd_hda_codec_setup_stream(codec, dac,
857 						   spec->cur_dac_stream_tag, ch,
858 						   spec->cur_dac_format);
859 		}
860 	}
861 	setup_playback_multi_pcm(spec);
862 }
863 
864 static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
865 				  struct snd_ctl_elem_value *ucontrol)
866 {
867 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
868 	struct via_spec *spec = codec->spec;
869 	int cur, shared;
870 
871 	mutex_lock(&spec->config_mutex);
872 	cur = !!ucontrol->value.enumerated.item[0];
873 	if (spec->hp_independent_mode == cur) {
874 		mutex_unlock(&spec->config_mutex);
875 		return 0;
876 	}
877 	spec->hp_independent_mode = cur;
878 	shared = spec->hp_indep_shared;
879 	deactivate_hp_paths(codec);
880 	if (cur)
881 		activate_output_path(codec, &spec->hp_indep_path, true, false);
882 	else {
883 		if (shared)
884 			activate_output_path(codec, &spec->out_path[shared],
885 					     true, false);
886 		if (spec->aamix_mode || !spec->hp_path.depth)
887 			activate_output_path(codec, &spec->hp_mix_path,
888 					     true, false);
889 		else
890 			activate_output_path(codec, &spec->hp_path,
891 					     true, false);
892 	}
893 
894 	switch_indep_hp_dacs(codec);
895 	mutex_unlock(&spec->config_mutex);
896 
897 	/* update jack power state */
898 	set_widgets_power_state(codec);
899 	via_hp_automute(codec);
900 	return 1;
901 }
902 
903 static const struct snd_kcontrol_new via_hp_mixer = {
904 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
905 	.name = "Independent HP",
906 	.info = via_independent_hp_info,
907 	.get = via_independent_hp_get,
908 	.put = via_independent_hp_put,
909 };
910 
911 static int via_hp_build(struct hda_codec *codec)
912 {
913 	struct via_spec *spec = codec->spec;
914 	struct snd_kcontrol_new *knew;
915 	hda_nid_t nid;
916 
917 	nid = spec->autocfg.hp_pins[0];
918 	knew = via_clone_control(spec, &via_hp_mixer);
919 	if (knew == NULL)
920 		return -ENOMEM;
921 
922 	knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
923 
924 	return 0;
925 }
926 
927 static void notify_aa_path_ctls(struct hda_codec *codec)
928 {
929 	struct via_spec *spec = codec->spec;
930 	int i;
931 
932 	for (i = 0; i < spec->smart51_nums; i++) {
933 		struct snd_kcontrol *ctl;
934 		struct snd_ctl_elem_id id;
935 		memset(&id, 0, sizeof(id));
936 		id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
937 		sprintf(id.name, "%s Playback Volume", spec->smart51_labels[i]);
938 		ctl = snd_hda_find_mixer_ctl(codec, id.name);
939 		if (ctl)
940 			snd_ctl_notify(codec->bus->card,
941 					SNDRV_CTL_EVENT_MASK_VALUE,
942 					&ctl->id);
943 	}
944 }
945 
946 static void mute_aa_path(struct hda_codec *codec, int mute)
947 {
948 	struct via_spec *spec = codec->spec;
949 	int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
950 	int i;
951 
952 	/* check AA path's mute status */
953 	for (i = 0; i < spec->smart51_nums; i++) {
954 		if (spec->smart51_idxs[i] < 0)
955 			continue;
956 		snd_hda_codec_amp_stereo(codec, spec->aa_mix_nid,
957 					 HDA_INPUT, spec->smart51_idxs[i],
958 					 HDA_AMP_MUTE, val);
959 	}
960 }
961 
962 static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
963 {
964 	struct via_spec *spec = codec->spec;
965 	int i;
966 
967 	for (i = 0; i < spec->smart51_nums; i++)
968 		if (spec->smart51_pins[i] == pin)
969 			return true;
970 	return false;
971 }
972 
973 static int via_smart51_get(struct snd_kcontrol *kcontrol,
974 			   struct snd_ctl_elem_value *ucontrol)
975 {
976 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
977 	struct via_spec *spec = codec->spec;
978 
979 	*ucontrol->value.integer.value = spec->smart51_enabled;
980 	return 0;
981 }
982 
983 static int via_smart51_put(struct snd_kcontrol *kcontrol,
984 			   struct snd_ctl_elem_value *ucontrol)
985 {
986 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
987 	struct via_spec *spec = codec->spec;
988 	int out_in = *ucontrol->value.integer.value
989 		? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
990 	int i;
991 
992 	for (i = 0; i < spec->smart51_nums; i++) {
993 		hda_nid_t nid = spec->smart51_pins[i];
994 		unsigned int parm;
995 
996 		parm = snd_hda_codec_read(codec, nid, 0,
997 					  AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
998 		parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
999 		parm |= out_in;
1000 		snd_hda_set_pin_ctl(codec, nid, parm);
1001 		if (out_in == AC_PINCTL_OUT_EN) {
1002 			mute_aa_path(codec, 1);
1003 			notify_aa_path_ctls(codec);
1004 		}
1005 	}
1006 	spec->smart51_enabled = *ucontrol->value.integer.value;
1007 	set_widgets_power_state(codec);
1008 	return 1;
1009 }
1010 
1011 static const struct snd_kcontrol_new via_smart51_mixer = {
1012 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1013 	.name = "Smart 5.1",
1014 	.count = 1,
1015 	.info = snd_ctl_boolean_mono_info,
1016 	.get = via_smart51_get,
1017 	.put = via_smart51_put,
1018 };
1019 
1020 static int via_smart51_build(struct hda_codec *codec)
1021 {
1022 	struct via_spec *spec = codec->spec;
1023 
1024 	if (!spec->smart51_nums)
1025 		return 0;
1026 	if (!via_clone_control(spec, &via_smart51_mixer))
1027 		return -ENOMEM;
1028 	return 0;
1029 }
1030 
1031 /* check AA path's mute status */
1032 static bool is_aa_path_mute(struct hda_codec *codec)
1033 {
1034 	struct via_spec *spec = codec->spec;
1035 	const struct hda_amp_list *p;
1036 	int i, ch, v;
1037 
1038 	for (i = 0; i < spec->num_loopbacks; i++) {
1039 		p = &spec->loopback_list[i];
1040 		for (ch = 0; ch < 2; ch++) {
1041 			v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
1042 						   p->idx);
1043 			if (!(v & HDA_AMP_MUTE) && v > 0)
1044 				return false;
1045 		}
1046 	}
1047 	return true;
1048 }
1049 
1050 /* enter/exit analog low-current mode */
1051 static void __analog_low_current_mode(struct hda_codec *codec, bool force)
1052 {
1053 	struct via_spec *spec = codec->spec;
1054 	bool enable;
1055 	unsigned int verb, parm;
1056 
1057 	if (spec->no_pin_power_ctl)
1058 		enable = false;
1059 	else
1060 		enable = is_aa_path_mute(codec) && !spec->opened_streams;
1061 	if (enable == spec->alc_mode && !force)
1062 		return;
1063 	spec->alc_mode = enable;
1064 
1065 	/* decide low current mode's verb & parameter */
1066 	switch (spec->codec_type) {
1067 	case VT1708B_8CH:
1068 	case VT1708B_4CH:
1069 		verb = 0xf70;
1070 		parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
1071 		break;
1072 	case VT1708S:
1073 	case VT1718S:
1074 	case VT1716S:
1075 		verb = 0xf73;
1076 		parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
1077 		break;
1078 	case VT1702:
1079 		verb = 0xf73;
1080 		parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
1081 		break;
1082 	case VT2002P:
1083 	case VT1812:
1084 	case VT1802:
1085 		verb = 0xf93;
1086 		parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
1087 		break;
1088 	default:
1089 		return;		/* other codecs are not supported */
1090 	}
1091 	/* send verb */
1092 	snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
1093 }
1094 
1095 static void analog_low_current_mode(struct hda_codec *codec)
1096 {
1097 	return __analog_low_current_mode(codec, false);
1098 }
1099 
1100 /*
1101  * generic initialization of ADC, input mixers and output mixers
1102  */
1103 static const struct hda_verb vt1708_init_verbs[] = {
1104 	/* power down jack detect function */
1105 	{0x1, 0xf81, 0x1},
1106 	{ }
1107 };
1108 
1109 static void set_stream_open(struct hda_codec *codec, int bit, bool active)
1110 {
1111 	struct via_spec *spec = codec->spec;
1112 
1113 	if (active)
1114 		spec->opened_streams |= bit;
1115 	else
1116 		spec->opened_streams &= ~bit;
1117 	analog_low_current_mode(codec);
1118 }
1119 
1120 static int via_playback_multi_pcm_open(struct hda_pcm_stream *hinfo,
1121 				 struct hda_codec *codec,
1122 				 struct snd_pcm_substream *substream)
1123 {
1124 	struct via_spec *spec = codec->spec;
1125 	const struct auto_pin_cfg *cfg = &spec->autocfg;
1126 	int err;
1127 
1128 	spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
1129 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
1130 	set_stream_open(codec, STREAM_MULTI_OUT, true);
1131 	err = snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
1132 					    hinfo);
1133 	if (err < 0) {
1134 		set_stream_open(codec, STREAM_MULTI_OUT, false);
1135 		return err;
1136 	}
1137 	return 0;
1138 }
1139 
1140 static int via_playback_multi_pcm_close(struct hda_pcm_stream *hinfo,
1141 				  struct hda_codec *codec,
1142 				  struct snd_pcm_substream *substream)
1143 {
1144 	set_stream_open(codec, STREAM_MULTI_OUT, false);
1145 	return 0;
1146 }
1147 
1148 static int via_playback_hp_pcm_open(struct hda_pcm_stream *hinfo,
1149 				    struct hda_codec *codec,
1150 				    struct snd_pcm_substream *substream)
1151 {
1152 	struct via_spec *spec = codec->spec;
1153 
1154 	if (snd_BUG_ON(!spec->hp_dac_nid))
1155 		return -EINVAL;
1156 	set_stream_open(codec, STREAM_INDEP_HP, true);
1157 	return 0;
1158 }
1159 
1160 static int via_playback_hp_pcm_close(struct hda_pcm_stream *hinfo,
1161 				     struct hda_codec *codec,
1162 				     struct snd_pcm_substream *substream)
1163 {
1164 	set_stream_open(codec, STREAM_INDEP_HP, false);
1165 	return 0;
1166 }
1167 
1168 static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
1169 					  struct hda_codec *codec,
1170 					  unsigned int stream_tag,
1171 					  unsigned int format,
1172 					  struct snd_pcm_substream *substream)
1173 {
1174 	struct via_spec *spec = codec->spec;
1175 
1176 	mutex_lock(&spec->config_mutex);
1177 	setup_playback_multi_pcm(spec);
1178 	snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
1179 					 format, substream);
1180 	/* remember for dynamic DAC switch with indep-HP */
1181 	spec->active_streams |= STREAM_MULTI_OUT;
1182 	spec->cur_dac_stream_tag = stream_tag;
1183 	spec->cur_dac_format = format;
1184 	mutex_unlock(&spec->config_mutex);
1185 	vt1708_update_hp_work(spec);
1186 	return 0;
1187 }
1188 
1189 static int via_playback_hp_pcm_prepare(struct hda_pcm_stream *hinfo,
1190 				       struct hda_codec *codec,
1191 				       unsigned int stream_tag,
1192 				       unsigned int format,
1193 				       struct snd_pcm_substream *substream)
1194 {
1195 	struct via_spec *spec = codec->spec;
1196 
1197 	mutex_lock(&spec->config_mutex);
1198 	if (spec->hp_independent_mode)
1199 		snd_hda_codec_setup_stream(codec, spec->hp_dac_nid,
1200 					   stream_tag, 0, format);
1201 	spec->active_streams |= STREAM_INDEP_HP;
1202 	spec->cur_hp_stream_tag = stream_tag;
1203 	spec->cur_hp_format = format;
1204 	mutex_unlock(&spec->config_mutex);
1205 	vt1708_update_hp_work(spec);
1206 	return 0;
1207 }
1208 
1209 static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
1210 				    struct hda_codec *codec,
1211 				    struct snd_pcm_substream *substream)
1212 {
1213 	struct via_spec *spec = codec->spec;
1214 
1215 	mutex_lock(&spec->config_mutex);
1216 	snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
1217 	spec->active_streams &= ~STREAM_MULTI_OUT;
1218 	mutex_unlock(&spec->config_mutex);
1219 	vt1708_update_hp_work(spec);
1220 	return 0;
1221 }
1222 
1223 static int via_playback_hp_pcm_cleanup(struct hda_pcm_stream *hinfo,
1224 				       struct hda_codec *codec,
1225 				       struct snd_pcm_substream *substream)
1226 {
1227 	struct via_spec *spec = codec->spec;
1228 
1229 	mutex_lock(&spec->config_mutex);
1230 	if (spec->hp_independent_mode)
1231 		snd_hda_codec_setup_stream(codec, spec->hp_dac_nid, 0, 0, 0);
1232 	spec->active_streams &= ~STREAM_INDEP_HP;
1233 	mutex_unlock(&spec->config_mutex);
1234 	vt1708_update_hp_work(spec);
1235 	return 0;
1236 }
1237 
1238 /*
1239  * Digital out
1240  */
1241 static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1242 				     struct hda_codec *codec,
1243 				     struct snd_pcm_substream *substream)
1244 {
1245 	struct via_spec *spec = codec->spec;
1246 	return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1247 }
1248 
1249 static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1250 				      struct hda_codec *codec,
1251 				      struct snd_pcm_substream *substream)
1252 {
1253 	struct via_spec *spec = codec->spec;
1254 	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1255 }
1256 
1257 static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1258 					struct hda_codec *codec,
1259 					unsigned int stream_tag,
1260 					unsigned int format,
1261 					struct snd_pcm_substream *substream)
1262 {
1263 	struct via_spec *spec = codec->spec;
1264 	return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1265 					     stream_tag, format, substream);
1266 }
1267 
1268 static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1269 					struct hda_codec *codec,
1270 					struct snd_pcm_substream *substream)
1271 {
1272 	struct via_spec *spec = codec->spec;
1273 	snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
1274 	return 0;
1275 }
1276 
1277 /*
1278  * Analog capture
1279  */
1280 static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1281 				   struct hda_codec *codec,
1282 				   unsigned int stream_tag,
1283 				   unsigned int format,
1284 				   struct snd_pcm_substream *substream)
1285 {
1286 	struct via_spec *spec = codec->spec;
1287 
1288 	snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1289 				   stream_tag, 0, format);
1290 	return 0;
1291 }
1292 
1293 static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1294 				   struct hda_codec *codec,
1295 				   struct snd_pcm_substream *substream)
1296 {
1297 	struct via_spec *spec = codec->spec;
1298 	snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
1299 	return 0;
1300 }
1301 
1302 /* analog capture with dynamic ADC switching */
1303 static int via_dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1304 					   struct hda_codec *codec,
1305 					   unsigned int stream_tag,
1306 					   unsigned int format,
1307 					   struct snd_pcm_substream *substream)
1308 {
1309 	struct via_spec *spec = codec->spec;
1310 	int adc_idx = spec->inputs[spec->cur_mux[0]].adc_idx;
1311 
1312 	mutex_lock(&spec->config_mutex);
1313 	spec->cur_adc = spec->adc_nids[adc_idx];
1314 	spec->cur_adc_stream_tag = stream_tag;
1315 	spec->cur_adc_format = format;
1316 	snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
1317 	mutex_unlock(&spec->config_mutex);
1318 	return 0;
1319 }
1320 
1321 static int via_dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1322 					   struct hda_codec *codec,
1323 					   struct snd_pcm_substream *substream)
1324 {
1325 	struct via_spec *spec = codec->spec;
1326 
1327 	mutex_lock(&spec->config_mutex);
1328 	snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1329 	spec->cur_adc = 0;
1330 	mutex_unlock(&spec->config_mutex);
1331 	return 0;
1332 }
1333 
1334 /* re-setup the stream if running; called from input-src put */
1335 static bool via_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
1336 {
1337 	struct via_spec *spec = codec->spec;
1338 	int adc_idx = spec->inputs[cur].adc_idx;
1339 	hda_nid_t adc = spec->adc_nids[adc_idx];
1340 	bool ret = false;
1341 
1342 	mutex_lock(&spec->config_mutex);
1343 	if (spec->cur_adc && spec->cur_adc != adc) {
1344 		/* stream is running, let's swap the current ADC */
1345 		__snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1346 		spec->cur_adc = adc;
1347 		snd_hda_codec_setup_stream(codec, adc,
1348 					   spec->cur_adc_stream_tag, 0,
1349 					   spec->cur_adc_format);
1350 		ret = true;
1351 	}
1352 	mutex_unlock(&spec->config_mutex);
1353 	return ret;
1354 }
1355 
1356 static const struct hda_pcm_stream via_pcm_analog_playback = {
1357 	.substreams = 1,
1358 	.channels_min = 2,
1359 	.channels_max = 8,
1360 	/* NID is set in via_build_pcms */
1361 	.ops = {
1362 		.open = via_playback_multi_pcm_open,
1363 		.close = via_playback_multi_pcm_close,
1364 		.prepare = via_playback_multi_pcm_prepare,
1365 		.cleanup = via_playback_multi_pcm_cleanup
1366 	},
1367 };
1368 
1369 static const struct hda_pcm_stream via_pcm_hp_playback = {
1370 	.substreams = 1,
1371 	.channels_min = 2,
1372 	.channels_max = 2,
1373 	/* NID is set in via_build_pcms */
1374 	.ops = {
1375 		.open = via_playback_hp_pcm_open,
1376 		.close = via_playback_hp_pcm_close,
1377 		.prepare = via_playback_hp_pcm_prepare,
1378 		.cleanup = via_playback_hp_pcm_cleanup
1379 	},
1380 };
1381 
1382 static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
1383 	.substreams = 1,
1384 	.channels_min = 2,
1385 	.channels_max = 8,
1386 	/* NID is set in via_build_pcms */
1387 	/* We got noisy outputs on the right channel on VT1708 when
1388 	 * 24bit samples are used.  Until any workaround is found,
1389 	 * disable the 24bit format, so far.
1390 	 */
1391 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
1392 	.ops = {
1393 		.open = via_playback_multi_pcm_open,
1394 		.close = via_playback_multi_pcm_close,
1395 		.prepare = via_playback_multi_pcm_prepare,
1396 		.cleanup = via_playback_multi_pcm_cleanup
1397 	},
1398 };
1399 
1400 static const struct hda_pcm_stream via_pcm_analog_capture = {
1401 	.substreams = 1, /* will be changed in via_build_pcms() */
1402 	.channels_min = 2,
1403 	.channels_max = 2,
1404 	/* NID is set in via_build_pcms */
1405 	.ops = {
1406 		.prepare = via_capture_pcm_prepare,
1407 		.cleanup = via_capture_pcm_cleanup
1408 	},
1409 };
1410 
1411 static const struct hda_pcm_stream via_pcm_dyn_adc_analog_capture = {
1412 	.substreams = 1,
1413 	.channels_min = 2,
1414 	.channels_max = 2,
1415 	/* NID is set in via_build_pcms */
1416 	.ops = {
1417 		.prepare = via_dyn_adc_capture_pcm_prepare,
1418 		.cleanup = via_dyn_adc_capture_pcm_cleanup,
1419 	},
1420 };
1421 
1422 static const struct hda_pcm_stream via_pcm_digital_playback = {
1423 	.substreams = 1,
1424 	.channels_min = 2,
1425 	.channels_max = 2,
1426 	/* NID is set in via_build_pcms */
1427 	.ops = {
1428 		.open = via_dig_playback_pcm_open,
1429 		.close = via_dig_playback_pcm_close,
1430 		.prepare = via_dig_playback_pcm_prepare,
1431 		.cleanup = via_dig_playback_pcm_cleanup
1432 	},
1433 };
1434 
1435 static const struct hda_pcm_stream via_pcm_digital_capture = {
1436 	.substreams = 1,
1437 	.channels_min = 2,
1438 	.channels_max = 2,
1439 };
1440 
1441 /*
1442  * slave controls for virtual master
1443  */
1444 static const char * const via_slave_pfxs[] = {
1445 	"Front", "Surround", "Center", "LFE", "Side",
1446 	"Headphone", "Speaker", "Bass Speaker",
1447 	NULL,
1448 };
1449 
1450 static int via_build_controls(struct hda_codec *codec)
1451 {
1452 	struct via_spec *spec = codec->spec;
1453 	struct snd_kcontrol *kctl;
1454 	int err, i;
1455 
1456 	spec->no_pin_power_ctl = 1;
1457 	if (spec->set_widgets_power_state)
1458 		if (!via_clone_control(spec, &via_pin_power_ctl_enum))
1459 			return -ENOMEM;
1460 
1461 	for (i = 0; i < spec->num_mixers; i++) {
1462 		err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1463 		if (err < 0)
1464 			return err;
1465 	}
1466 
1467 	if (spec->multiout.dig_out_nid) {
1468 		err = snd_hda_create_spdif_out_ctls(codec,
1469 						    spec->multiout.dig_out_nid,
1470 						    spec->multiout.dig_out_nid);
1471 		if (err < 0)
1472 			return err;
1473 		err = snd_hda_create_spdif_share_sw(codec,
1474 						    &spec->multiout);
1475 		if (err < 0)
1476 			return err;
1477 		spec->multiout.share_spdif = 1;
1478 	}
1479 	if (spec->dig_in_nid) {
1480 		err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1481 		if (err < 0)
1482 			return err;
1483 	}
1484 
1485 	/* if we have no master control, let's create it */
1486 	if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1487 		unsigned int vmaster_tlv[4];
1488 		snd_hda_set_vmaster_tlv(codec, spec->multiout.dac_nids[0],
1489 					HDA_OUTPUT, vmaster_tlv);
1490 		err = snd_hda_add_vmaster(codec, "Master Playback Volume",
1491 					  vmaster_tlv, via_slave_pfxs,
1492 					  "Playback Volume");
1493 		if (err < 0)
1494 			return err;
1495 	}
1496 	if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1497 		err = snd_hda_add_vmaster(codec, "Master Playback Switch",
1498 					  NULL, via_slave_pfxs,
1499 					  "Playback Switch");
1500 		if (err < 0)
1501 			return err;
1502 	}
1503 
1504 	/* assign Capture Source enums to NID */
1505 	kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1506 	for (i = 0; kctl && i < kctl->count; i++) {
1507 		if (!spec->mux_nids[i])
1508 			continue;
1509 		err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
1510 		if (err < 0)
1511 			return err;
1512 	}
1513 
1514 	via_free_kctls(codec); /* no longer needed */
1515 
1516 	err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1517 	if (err < 0)
1518 		return err;
1519 
1520 	return 0;
1521 }
1522 
1523 static int via_build_pcms(struct hda_codec *codec)
1524 {
1525 	struct via_spec *spec = codec->spec;
1526 	struct hda_pcm *info = spec->pcm_rec;
1527 
1528 	codec->num_pcms = 0;
1529 	codec->pcm_info = info;
1530 
1531 	if (spec->multiout.num_dacs || spec->num_adc_nids) {
1532 		snprintf(spec->stream_name_analog,
1533 			 sizeof(spec->stream_name_analog),
1534 			 "%s Analog", codec->chip_name);
1535 		info->name = spec->stream_name_analog;
1536 
1537 		if (spec->multiout.num_dacs) {
1538 			if (!spec->stream_analog_playback)
1539 				spec->stream_analog_playback =
1540 					&via_pcm_analog_playback;
1541 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1542 				*spec->stream_analog_playback;
1543 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1544 				spec->multiout.dac_nids[0];
1545 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1546 				spec->multiout.max_channels;
1547 			if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT
1548 			    && spec->autocfg.line_outs == 2)
1549 				info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
1550 					snd_pcm_2_1_chmaps;
1551 		}
1552 
1553 		if (!spec->stream_analog_capture) {
1554 			if (spec->dyn_adc_switch)
1555 				spec->stream_analog_capture =
1556 					&via_pcm_dyn_adc_analog_capture;
1557 			else
1558 				spec->stream_analog_capture =
1559 					&via_pcm_analog_capture;
1560 		}
1561 		if (spec->num_adc_nids) {
1562 			info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1563 				*spec->stream_analog_capture;
1564 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1565 				spec->adc_nids[0];
1566 			if (!spec->dyn_adc_switch)
1567 				info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
1568 					spec->num_adc_nids;
1569 		}
1570 		codec->num_pcms++;
1571 		info++;
1572 	}
1573 
1574 	if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1575 		snprintf(spec->stream_name_digital,
1576 			 sizeof(spec->stream_name_digital),
1577 			 "%s Digital", codec->chip_name);
1578 		info->name = spec->stream_name_digital;
1579 		info->pcm_type = HDA_PCM_TYPE_SPDIF;
1580 		if (spec->multiout.dig_out_nid) {
1581 			if (!spec->stream_digital_playback)
1582 				spec->stream_digital_playback =
1583 					&via_pcm_digital_playback;
1584 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1585 				*spec->stream_digital_playback;
1586 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1587 				spec->multiout.dig_out_nid;
1588 		}
1589 		if (spec->dig_in_nid) {
1590 			if (!spec->stream_digital_capture)
1591 				spec->stream_digital_capture =
1592 					&via_pcm_digital_capture;
1593 			info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1594 				*spec->stream_digital_capture;
1595 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1596 				spec->dig_in_nid;
1597 		}
1598 		codec->num_pcms++;
1599 		info++;
1600 	}
1601 
1602 	if (spec->hp_dac_nid) {
1603 		snprintf(spec->stream_name_hp, sizeof(spec->stream_name_hp),
1604 			 "%s HP", codec->chip_name);
1605 		info->name = spec->stream_name_hp;
1606 		info->stream[SNDRV_PCM_STREAM_PLAYBACK] = via_pcm_hp_playback;
1607 		info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1608 			spec->hp_dac_nid;
1609 		codec->num_pcms++;
1610 		info++;
1611 	}
1612 	return 0;
1613 }
1614 
1615 static void via_free(struct hda_codec *codec)
1616 {
1617 	struct via_spec *spec = codec->spec;
1618 
1619 	if (!spec)
1620 		return;
1621 
1622 	via_free_kctls(codec);
1623 	vt1708_stop_hp_work(spec);
1624 	kfree(spec->bind_cap_vol);
1625 	kfree(spec->bind_cap_sw);
1626 	snd_hda_gen_free(&spec->gen);
1627 	kfree(spec);
1628 }
1629 
1630 /* mute/unmute outputs */
1631 static void toggle_output_mutes(struct hda_codec *codec, int num_pins,
1632 				hda_nid_t *pins, bool mute)
1633 {
1634 	int i;
1635 	for (i = 0; i < num_pins; i++) {
1636 		unsigned int parm = snd_hda_codec_read(codec, pins[i], 0,
1637 					  AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1638 		if (parm & AC_PINCTL_IN_EN)
1639 			continue;
1640 		if (mute)
1641 			parm &= ~AC_PINCTL_OUT_EN;
1642 		else
1643 			parm |= AC_PINCTL_OUT_EN;
1644 		snd_hda_set_pin_ctl(codec, pins[i], parm);
1645 	}
1646 }
1647 
1648 /* mute internal speaker if line-out is plugged */
1649 static void via_line_automute(struct hda_codec *codec, int present)
1650 {
1651 	struct via_spec *spec = codec->spec;
1652 
1653 	if (!spec->autocfg.speaker_outs)
1654 		return;
1655 	if (!present)
1656 		present = snd_hda_jack_detect(codec,
1657 					      spec->autocfg.line_out_pins[0]);
1658 	toggle_output_mutes(codec, spec->autocfg.speaker_outs,
1659 			    spec->autocfg.speaker_pins,
1660 			    present);
1661 }
1662 
1663 /* mute internal speaker if HP is plugged */
1664 static void via_hp_automute(struct hda_codec *codec)
1665 {
1666 	int present = 0;
1667 	int nums;
1668 	struct via_spec *spec = codec->spec;
1669 
1670 	if (!spec->hp_independent_mode && spec->autocfg.hp_pins[0] &&
1671 	    (spec->codec_type != VT1708 || spec->vt1708_jack_detect) &&
1672 	    is_jack_detectable(codec, spec->autocfg.hp_pins[0]))
1673 		present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
1674 
1675 	if (spec->smart51_enabled)
1676 		nums = spec->autocfg.line_outs + spec->smart51_nums;
1677 	else
1678 		nums = spec->autocfg.line_outs;
1679 	toggle_output_mutes(codec, nums, spec->autocfg.line_out_pins, present);
1680 
1681 	via_line_automute(codec, present);
1682 }
1683 
1684 #ifdef CONFIG_PM
1685 static int via_suspend(struct hda_codec *codec)
1686 {
1687 	struct via_spec *spec = codec->spec;
1688 	vt1708_stop_hp_work(spec);
1689 
1690 	if (spec->codec_type == VT1802) {
1691 		/* Fix pop noise on headphones */
1692 		int i;
1693 		for (i = 0; i < spec->autocfg.hp_outs; i++)
1694 			snd_hda_set_pin_ctl(codec, spec->autocfg.hp_pins[i], 0);
1695 	}
1696 
1697 	return 0;
1698 }
1699 #endif
1700 
1701 #ifdef CONFIG_PM
1702 static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1703 {
1704 	struct via_spec *spec = codec->spec;
1705 	return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1706 }
1707 #endif
1708 
1709 /*
1710  */
1711 
1712 static int via_init(struct hda_codec *codec);
1713 
1714 static const struct hda_codec_ops via_patch_ops = {
1715 	.build_controls = via_build_controls,
1716 	.build_pcms = via_build_pcms,
1717 	.init = via_init,
1718 	.free = via_free,
1719 	.unsol_event = snd_hda_jack_unsol_event,
1720 #ifdef CONFIG_PM
1721 	.suspend = via_suspend,
1722 	.check_power_status = via_check_power_status,
1723 #endif
1724 };
1725 
1726 static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
1727 {
1728 	struct via_spec *spec = codec->spec;
1729 	int i;
1730 
1731 	for (i = 0; i < spec->multiout.num_dacs; i++) {
1732 		if (spec->multiout.dac_nids[i] == dac)
1733 			return false;
1734 	}
1735 	if (spec->hp_dac_nid == dac)
1736 		return false;
1737 	return true;
1738 }
1739 
1740 static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
1741 				hda_nid_t target_dac, int with_aa_mix,
1742 				struct nid_path *path, int depth)
1743 {
1744 	struct via_spec *spec = codec->spec;
1745 	hda_nid_t conn[8];
1746 	int i, nums;
1747 
1748 	if (nid == spec->aa_mix_nid) {
1749 		if (!with_aa_mix)
1750 			return false;
1751 		with_aa_mix = 2; /* mark aa-mix is included */
1752 	}
1753 
1754 	nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
1755 	for (i = 0; i < nums; i++) {
1756 		if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
1757 			continue;
1758 		if (conn[i] == target_dac || is_empty_dac(codec, conn[i])) {
1759 			/* aa-mix is requested but not included? */
1760 			if (!(spec->aa_mix_nid && with_aa_mix == 1))
1761 				goto found;
1762 		}
1763 	}
1764 	if (depth >= MAX_NID_PATH_DEPTH)
1765 		return false;
1766 	for (i = 0; i < nums; i++) {
1767 		unsigned int type;
1768 		type = get_wcaps_type(get_wcaps(codec, conn[i]));
1769 		if (type == AC_WID_AUD_OUT)
1770 			continue;
1771 		if (__parse_output_path(codec, conn[i], target_dac,
1772 					with_aa_mix, path, depth + 1))
1773 			goto found;
1774 	}
1775 	return false;
1776 
1777  found:
1778 	path->path[path->depth] = conn[i];
1779 	path->idx[path->depth] = i;
1780 	if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
1781 		path->multi[path->depth] = 1;
1782 	path->depth++;
1783 	return true;
1784 }
1785 
1786 static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
1787 			      hda_nid_t target_dac, int with_aa_mix,
1788 			      struct nid_path *path)
1789 {
1790 	if (__parse_output_path(codec, nid, target_dac, with_aa_mix, path, 1)) {
1791 		path->path[path->depth] = nid;
1792 		path->depth++;
1793 		snd_printdd("output-path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
1794 			    path->depth, path->path[0], path->path[1],
1795 			    path->path[2], path->path[3], path->path[4]);
1796 		return true;
1797 	}
1798 	return false;
1799 }
1800 
1801 static int via_auto_fill_dac_nids(struct hda_codec *codec)
1802 {
1803 	struct via_spec *spec = codec->spec;
1804 	const struct auto_pin_cfg *cfg = &spec->autocfg;
1805 	int i;
1806 	hda_nid_t nid;
1807 
1808 	spec->multiout.num_dacs = 0;
1809 	spec->multiout.dac_nids = spec->private_dac_nids;
1810 	for (i = 0; i < cfg->line_outs; i++) {
1811 		hda_nid_t dac = 0;
1812 		nid = cfg->line_out_pins[i];
1813 		if (!nid)
1814 			continue;
1815 		if (parse_output_path(codec, nid, 0, 0, &spec->out_path[i]))
1816 			dac = spec->out_path[i].path[0];
1817 		if (!i && parse_output_path(codec, nid, dac, 1,
1818 					    &spec->out_mix_path))
1819 			dac = spec->out_mix_path.path[0];
1820 		if (dac)
1821 			spec->private_dac_nids[spec->multiout.num_dacs++] = dac;
1822 	}
1823 	if (!spec->out_path[0].depth && spec->out_mix_path.depth) {
1824 		spec->out_path[0] = spec->out_mix_path;
1825 		spec->out_mix_path.depth = 0;
1826 	}
1827 	return 0;
1828 }
1829 
1830 static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
1831 			  int chs, bool check_dac, struct nid_path *path)
1832 {
1833 	struct via_spec *spec = codec->spec;
1834 	char name[32];
1835 	hda_nid_t dac, pin, sel, nid;
1836 	int err;
1837 
1838 	dac = check_dac ? path->path[0] : 0;
1839 	pin = path->path[path->depth - 1];
1840 	sel = path->depth > 1 ? path->path[1] : 0;
1841 
1842 	if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1843 		nid = dac;
1844 	else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1845 		nid = pin;
1846 	else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1847 		nid = sel;
1848 	else
1849 		nid = 0;
1850 	if (nid) {
1851 		sprintf(name, "%s Playback Volume", pfx);
1852 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1853 			      HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1854 		if (err < 0)
1855 			return err;
1856 		path->vol_ctl = nid;
1857 	}
1858 
1859 	if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
1860 		nid = dac;
1861 	else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
1862 		nid = pin;
1863 	else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
1864 		nid = sel;
1865 	else
1866 		nid = 0;
1867 	if (nid) {
1868 		sprintf(name, "%s Playback Switch", pfx);
1869 		err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1870 			      HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1871 		if (err < 0)
1872 			return err;
1873 		path->mute_ctl = nid;
1874 	}
1875 	return 0;
1876 }
1877 
1878 static void mangle_smart51(struct hda_codec *codec)
1879 {
1880 	struct via_spec *spec = codec->spec;
1881 	struct auto_pin_cfg *cfg = &spec->autocfg;
1882 	struct auto_pin_cfg_item *ins = cfg->inputs;
1883 	int i, j, nums, attr;
1884 	int pins[AUTO_CFG_MAX_INS];
1885 
1886 	for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
1887 		nums = 0;
1888 		for (i = 0; i < cfg->num_inputs; i++) {
1889 			unsigned int def;
1890 			if (ins[i].type > AUTO_PIN_LINE_IN)
1891 				continue;
1892 			def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
1893 			if (snd_hda_get_input_pin_attr(def) != attr)
1894 				continue;
1895 			for (j = 0; j < nums; j++)
1896 				if (ins[pins[j]].type < ins[i].type) {
1897 					memmove(pins + j + 1, pins + j,
1898 						(nums - j) * sizeof(int));
1899 					break;
1900 				}
1901 			pins[j] = i;
1902 			nums++;
1903 		}
1904 		if (cfg->line_outs + nums < 3)
1905 			continue;
1906 		for (i = 0; i < nums; i++) {
1907 			hda_nid_t pin = ins[pins[i]].pin;
1908 			spec->smart51_pins[spec->smart51_nums++] = pin;
1909 			cfg->line_out_pins[cfg->line_outs++] = pin;
1910 			if (cfg->line_outs == 3)
1911 				break;
1912 		}
1913 		return;
1914 	}
1915 }
1916 
1917 static void copy_path_mixer_ctls(struct nid_path *dst, struct nid_path *src)
1918 {
1919 	dst->vol_ctl = src->vol_ctl;
1920 	dst->mute_ctl = src->mute_ctl;
1921 }
1922 
1923 /* add playback controls from the parsed DAC table */
1924 static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
1925 {
1926 	struct via_spec *spec = codec->spec;
1927 	struct auto_pin_cfg *cfg = &spec->autocfg;
1928 	struct nid_path *path;
1929 	static const char * const chname[4] = {
1930 		"Front", "Surround", NULL /* "CLFE" */, "Side"
1931 	};
1932 	int i, idx, err;
1933 	int old_line_outs;
1934 
1935 	/* check smart51 */
1936 	old_line_outs = cfg->line_outs;
1937 	if (cfg->line_outs == 1)
1938 		mangle_smart51(codec);
1939 
1940 	err = via_auto_fill_dac_nids(codec);
1941 	if (err < 0)
1942 		return err;
1943 
1944 	if (spec->multiout.num_dacs < 3) {
1945 		spec->smart51_nums = 0;
1946 		cfg->line_outs = old_line_outs;
1947 	}
1948 	for (i = 0; i < cfg->line_outs; i++) {
1949 		hda_nid_t pin, dac;
1950 		pin = cfg->line_out_pins[i];
1951 		dac = spec->multiout.dac_nids[i];
1952 		if (!pin || !dac)
1953 			continue;
1954 		path = spec->out_path + i;
1955 		if (i == HDA_CLFE) {
1956 			err = create_ch_ctls(codec, "Center", 1, true, path);
1957 			if (err < 0)
1958 				return err;
1959 			err = create_ch_ctls(codec, "LFE", 2, true, path);
1960 			if (err < 0)
1961 				return err;
1962 		} else {
1963 			const char *pfx = chname[i];
1964 			if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
1965 			    cfg->line_outs <= 2)
1966 				pfx = i ? "Bass Speaker" : "Speaker";
1967 			err = create_ch_ctls(codec, pfx, 3, true, path);
1968 			if (err < 0)
1969 				return err;
1970 		}
1971 		if (path != spec->out_path + i)
1972 			copy_path_mixer_ctls(&spec->out_path[i], path);
1973 		if (path == spec->out_path && spec->out_mix_path.depth)
1974 			copy_path_mixer_ctls(&spec->out_mix_path, path);
1975 	}
1976 
1977 	idx = get_connection_index(codec, spec->aa_mix_nid,
1978 				   spec->multiout.dac_nids[0]);
1979 	if (idx >= 0) {
1980 		/* add control to mixer */
1981 		const char *name;
1982 		name = spec->out_mix_path.depth ?
1983 			"PCM Loopback Playback Volume" : "PCM Playback Volume";
1984 		err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1985 				      HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
1986 							  idx, HDA_INPUT));
1987 		if (err < 0)
1988 			return err;
1989 		name = spec->out_mix_path.depth ?
1990 			"PCM Loopback Playback Switch" : "PCM Playback Switch";
1991 		err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1992 				      HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
1993 							  idx, HDA_INPUT));
1994 		if (err < 0)
1995 			return err;
1996 	}
1997 
1998 	cfg->line_outs = old_line_outs;
1999 
2000 	return 0;
2001 }
2002 
2003 static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
2004 {
2005 	struct via_spec *spec = codec->spec;
2006 	struct nid_path *path;
2007 	bool check_dac;
2008 	int i, err;
2009 
2010 	if (!pin)
2011 		return 0;
2012 
2013 	if (!parse_output_path(codec, pin, 0, 0, &spec->hp_indep_path)) {
2014 		for (i = HDA_SIDE; i >= HDA_CLFE; i--) {
2015 			if (i < spec->multiout.num_dacs &&
2016 			    parse_output_path(codec, pin,
2017 					      spec->multiout.dac_nids[i], 0,
2018 					      &spec->hp_indep_path)) {
2019 				spec->hp_indep_shared = i;
2020 				break;
2021 			}
2022 		}
2023 	}
2024 	if (spec->hp_indep_path.depth) {
2025 		spec->hp_dac_nid = spec->hp_indep_path.path[0];
2026 		if (!spec->hp_indep_shared)
2027 			spec->hp_path = spec->hp_indep_path;
2028 	}
2029 	/* optionally check front-path w/o AA-mix */
2030 	if (!spec->hp_path.depth)
2031 		parse_output_path(codec, pin,
2032 				  spec->multiout.dac_nids[HDA_FRONT], 0,
2033 				  &spec->hp_path);
2034 
2035 	if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
2036 			       1, &spec->hp_mix_path) && !spec->hp_path.depth)
2037 		return 0;
2038 
2039 	if (spec->hp_path.depth) {
2040 		path = &spec->hp_path;
2041 		check_dac = true;
2042 	} else {
2043 		path = &spec->hp_mix_path;
2044 		check_dac = false;
2045 	}
2046 	err = create_ch_ctls(codec, "Headphone", 3, check_dac, path);
2047 	if (err < 0)
2048 		return err;
2049 	if (check_dac)
2050 		copy_path_mixer_ctls(&spec->hp_mix_path, path);
2051 	else
2052 		copy_path_mixer_ctls(&spec->hp_path, path);
2053 	if (spec->hp_indep_path.depth)
2054 		copy_path_mixer_ctls(&spec->hp_indep_path, path);
2055 	return 0;
2056 }
2057 
2058 static int via_auto_create_speaker_ctls(struct hda_codec *codec)
2059 {
2060 	struct via_spec *spec = codec->spec;
2061 	struct nid_path *path;
2062 	bool check_dac;
2063 	hda_nid_t pin, dac = 0;
2064 	int err;
2065 
2066 	pin = spec->autocfg.speaker_pins[0];
2067 	if (!spec->autocfg.speaker_outs || !pin)
2068 		return 0;
2069 
2070 	if (parse_output_path(codec, pin, 0, 0, &spec->speaker_path))
2071 		dac = spec->speaker_path.path[0];
2072 	if (!dac)
2073 		parse_output_path(codec, pin,
2074 				  spec->multiout.dac_nids[HDA_FRONT], 0,
2075 				  &spec->speaker_path);
2076 	if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
2077 			       1, &spec->speaker_mix_path) && !dac)
2078 		return 0;
2079 
2080 	/* no AA-path for front? */
2081 	if (!spec->out_mix_path.depth && spec->speaker_mix_path.depth)
2082 		dac = 0;
2083 
2084 	spec->speaker_dac_nid = dac;
2085 	spec->multiout.extra_out_nid[0] = dac;
2086 	if (dac) {
2087 		path = &spec->speaker_path;
2088 		check_dac = true;
2089 	} else {
2090 		path = &spec->speaker_mix_path;
2091 		check_dac = false;
2092 	}
2093 	err = create_ch_ctls(codec, "Speaker", 3, check_dac, path);
2094 	if (err < 0)
2095 		return err;
2096 	if (check_dac)
2097 		copy_path_mixer_ctls(&spec->speaker_mix_path, path);
2098 	else
2099 		copy_path_mixer_ctls(&spec->speaker_path, path);
2100 	return 0;
2101 }
2102 
2103 #define via_aamix_ctl_info	via_pin_power_ctl_info
2104 
2105 static int via_aamix_ctl_get(struct snd_kcontrol *kcontrol,
2106 			     struct snd_ctl_elem_value *ucontrol)
2107 {
2108 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2109 	struct via_spec *spec = codec->spec;
2110 	ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2111 	return 0;
2112 }
2113 
2114 static void update_aamix_paths(struct hda_codec *codec, int do_mix,
2115 			       struct nid_path *nomix, struct nid_path *mix)
2116 {
2117 	if (do_mix) {
2118 		activate_output_path(codec, nomix, false, false);
2119 		activate_output_path(codec, mix, true, false);
2120 	} else {
2121 		activate_output_path(codec, mix, false, false);
2122 		activate_output_path(codec, nomix, true, false);
2123 	}
2124 }
2125 
2126 static int via_aamix_ctl_put(struct snd_kcontrol *kcontrol,
2127 			     struct snd_ctl_elem_value *ucontrol)
2128 {
2129 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2130 	struct via_spec *spec = codec->spec;
2131 	unsigned int val = ucontrol->value.enumerated.item[0];
2132 
2133 	if (val == spec->aamix_mode)
2134 		return 0;
2135 	spec->aamix_mode = val;
2136 	/* update front path */
2137 	update_aamix_paths(codec, val, &spec->out_path[0], &spec->out_mix_path);
2138 	/* update HP path */
2139 	if (!spec->hp_independent_mode) {
2140 		update_aamix_paths(codec, val, &spec->hp_path,
2141 				   &spec->hp_mix_path);
2142 	}
2143 	/* update speaker path */
2144 	update_aamix_paths(codec, val, &spec->speaker_path,
2145 			   &spec->speaker_mix_path);
2146 	return 1;
2147 }
2148 
2149 static const struct snd_kcontrol_new via_aamix_ctl_enum = {
2150 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2151 	.name = "Loopback Mixing",
2152 	.info = via_aamix_ctl_info,
2153 	.get = via_aamix_ctl_get,
2154 	.put = via_aamix_ctl_put,
2155 };
2156 
2157 static int via_auto_create_loopback_switch(struct hda_codec *codec)
2158 {
2159 	struct via_spec *spec = codec->spec;
2160 
2161 	if (!spec->aa_mix_nid)
2162 		return 0; /* no loopback switching available */
2163 	if (!(spec->out_mix_path.depth || spec->hp_mix_path.depth ||
2164 	      spec->speaker_path.depth))
2165 		return 0; /* no loopback switching available */
2166 	if (!via_clone_control(spec, &via_aamix_ctl_enum))
2167 		return -ENOMEM;
2168 	return 0;
2169 }
2170 
2171 /* look for ADCs */
2172 static int via_fill_adcs(struct hda_codec *codec)
2173 {
2174 	struct via_spec *spec = codec->spec;
2175 	hda_nid_t nid = codec->start_nid;
2176 	int i;
2177 
2178 	for (i = 0; i < codec->num_nodes; i++, nid++) {
2179 		unsigned int wcaps = get_wcaps(codec, nid);
2180 		if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2181 			continue;
2182 		if (wcaps & AC_WCAP_DIGITAL)
2183 			continue;
2184 		if (!(wcaps & AC_WCAP_CONN_LIST))
2185 			continue;
2186 		if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
2187 			return -ENOMEM;
2188 		spec->adc_nids[spec->num_adc_nids++] = nid;
2189 	}
2190 	return 0;
2191 }
2192 
2193 /* input-src control */
2194 static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
2195 			     struct snd_ctl_elem_info *uinfo)
2196 {
2197 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2198 	struct via_spec *spec = codec->spec;
2199 
2200 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2201 	uinfo->count = 1;
2202 	uinfo->value.enumerated.items = spec->num_inputs;
2203 	if (uinfo->value.enumerated.item >= spec->num_inputs)
2204 		uinfo->value.enumerated.item = spec->num_inputs - 1;
2205 	strcpy(uinfo->value.enumerated.name,
2206 	       spec->inputs[uinfo->value.enumerated.item].label);
2207 	return 0;
2208 }
2209 
2210 static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
2211 			    struct snd_ctl_elem_value *ucontrol)
2212 {
2213 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2214 	struct via_spec *spec = codec->spec;
2215 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2216 
2217 	ucontrol->value.enumerated.item[0] = spec->cur_mux[idx];
2218 	return 0;
2219 }
2220 
2221 static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
2222 			    struct snd_ctl_elem_value *ucontrol)
2223 {
2224 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2225 	struct via_spec *spec = codec->spec;
2226 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2227 	hda_nid_t mux;
2228 	int cur;
2229 
2230 	cur = ucontrol->value.enumerated.item[0];
2231 	if (cur < 0 || cur >= spec->num_inputs)
2232 		return -EINVAL;
2233 	if (spec->cur_mux[idx] == cur)
2234 		return 0;
2235 	spec->cur_mux[idx] = cur;
2236 	if (spec->dyn_adc_switch) {
2237 		int adc_idx = spec->inputs[cur].adc_idx;
2238 		mux = spec->mux_nids[adc_idx];
2239 		via_dyn_adc_pcm_resetup(codec, cur);
2240 	} else {
2241 		mux = spec->mux_nids[idx];
2242 		if (snd_BUG_ON(!mux))
2243 			return -EINVAL;
2244 	}
2245 
2246 	if (mux) {
2247 		/* switch to D0 beofre change index */
2248 		update_power_state(codec, mux, AC_PWRST_D0);
2249 		snd_hda_codec_write(codec, mux, 0,
2250 				    AC_VERB_SET_CONNECT_SEL,
2251 				    spec->inputs[cur].mux_idx);
2252 	}
2253 
2254 	/* update jack power state */
2255 	set_widgets_power_state(codec);
2256 	return 0;
2257 }
2258 
2259 static const struct snd_kcontrol_new via_input_src_ctl = {
2260 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2261 	/* The multiple "Capture Source" controls confuse alsamixer
2262 	 * So call somewhat different..
2263 	 */
2264 	/* .name = "Capture Source", */
2265 	.name = "Input Source",
2266 	.info = via_mux_enum_info,
2267 	.get = via_mux_enum_get,
2268 	.put = via_mux_enum_put,
2269 };
2270 
2271 static int create_input_src_ctls(struct hda_codec *codec, int count)
2272 {
2273 	struct via_spec *spec = codec->spec;
2274 	struct snd_kcontrol_new *knew;
2275 
2276 	if (spec->num_inputs <= 1 || !count)
2277 		return 0; /* no need for single src */
2278 
2279 	knew = via_clone_control(spec, &via_input_src_ctl);
2280 	if (!knew)
2281 		return -ENOMEM;
2282 	knew->count = count;
2283 	return 0;
2284 }
2285 
2286 /* add the powersave loopback-list entry */
2287 static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
2288 {
2289 	struct hda_amp_list *list;
2290 
2291 	if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2292 		return;
2293 	list = spec->loopback_list + spec->num_loopbacks;
2294 	list->nid = mix;
2295 	list->dir = HDA_INPUT;
2296 	list->idx = idx;
2297 	spec->num_loopbacks++;
2298 	spec->loopback.amplist = spec->loopback_list;
2299 }
2300 
2301 static bool is_reachable_nid(struct hda_codec *codec, hda_nid_t src,
2302 			     hda_nid_t dst)
2303 {
2304 	return snd_hda_get_conn_index(codec, src, dst, 1) >= 0;
2305 }
2306 
2307 /* add the input-route to the given pin */
2308 static bool add_input_route(struct hda_codec *codec, hda_nid_t pin)
2309 {
2310 	struct via_spec *spec = codec->spec;
2311 	int c, idx;
2312 
2313 	spec->inputs[spec->num_inputs].adc_idx = -1;
2314 	spec->inputs[spec->num_inputs].pin = pin;
2315 	for (c = 0; c < spec->num_adc_nids; c++) {
2316 		if (spec->mux_nids[c]) {
2317 			idx = get_connection_index(codec, spec->mux_nids[c],
2318 						   pin);
2319 			if (idx < 0)
2320 				continue;
2321 			spec->inputs[spec->num_inputs].mux_idx = idx;
2322 		} else {
2323 			if (!is_reachable_nid(codec, spec->adc_nids[c], pin))
2324 				continue;
2325 		}
2326 		spec->inputs[spec->num_inputs].adc_idx = c;
2327 		/* Can primary ADC satisfy all inputs? */
2328 		if (!spec->dyn_adc_switch &&
2329 		    spec->num_inputs > 0 && spec->inputs[0].adc_idx != c) {
2330 			snd_printd(KERN_INFO
2331 				   "via: dynamic ADC switching enabled\n");
2332 			spec->dyn_adc_switch = 1;
2333 		}
2334 		return true;
2335 	}
2336 	return false;
2337 }
2338 
2339 static int get_mux_nids(struct hda_codec *codec);
2340 
2341 /* parse input-routes; fill ADCs, MUXs and input-src entries */
2342 static int parse_analog_inputs(struct hda_codec *codec)
2343 {
2344 	struct via_spec *spec = codec->spec;
2345 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2346 	int i, err;
2347 
2348 	err = via_fill_adcs(codec);
2349 	if (err < 0)
2350 		return err;
2351 	err = get_mux_nids(codec);
2352 	if (err < 0)
2353 		return err;
2354 
2355 	/* fill all input-routes */
2356 	for (i = 0; i < cfg->num_inputs; i++) {
2357 		if (add_input_route(codec, cfg->inputs[i].pin))
2358 			spec->inputs[spec->num_inputs++].label =
2359 				hda_get_autocfg_input_label(codec, cfg, i);
2360 	}
2361 
2362 	/* check for internal loopback recording */
2363 	if (spec->aa_mix_nid &&
2364 	    add_input_route(codec, spec->aa_mix_nid))
2365 		spec->inputs[spec->num_inputs++].label = "Stereo Mixer";
2366 
2367 	return 0;
2368 }
2369 
2370 /* create analog-loopback volume/switch controls */
2371 static int create_loopback_ctls(struct hda_codec *codec)
2372 {
2373 	struct via_spec *spec = codec->spec;
2374 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2375 	const char *prev_label = NULL;
2376 	int type_idx = 0;
2377 	int i, j, err, idx;
2378 
2379 	if (!spec->aa_mix_nid)
2380 		return 0;
2381 
2382 	for (i = 0; i < cfg->num_inputs; i++) {
2383 		hda_nid_t pin = cfg->inputs[i].pin;
2384 		const char *label = hda_get_autocfg_input_label(codec, cfg, i);
2385 
2386 		if (prev_label && !strcmp(label, prev_label))
2387 			type_idx++;
2388 		else
2389 			type_idx = 0;
2390 		prev_label = label;
2391 		idx = get_connection_index(codec, spec->aa_mix_nid, pin);
2392 		if (idx >= 0) {
2393 			err = via_new_analog_input(spec, label, type_idx,
2394 						   idx, spec->aa_mix_nid);
2395 			if (err < 0)
2396 				return err;
2397 			add_loopback_list(spec, spec->aa_mix_nid, idx);
2398 		}
2399 
2400 		/* remember the label for smart51 control */
2401 		for (j = 0; j < spec->smart51_nums; j++) {
2402 			if (spec->smart51_pins[j] == pin) {
2403 				spec->smart51_idxs[j] = idx;
2404 				spec->smart51_labels[j] = label;
2405 				break;
2406 			}
2407 		}
2408 	}
2409 	return 0;
2410 }
2411 
2412 /* create mic-boost controls (if present) */
2413 static int create_mic_boost_ctls(struct hda_codec *codec)
2414 {
2415 	struct via_spec *spec = codec->spec;
2416 	const struct auto_pin_cfg *cfg = &spec->autocfg;
2417 	const char *prev_label = NULL;
2418 	int type_idx = 0;
2419 	int i, err;
2420 
2421 	for (i = 0; i < cfg->num_inputs; i++) {
2422 		hda_nid_t pin = cfg->inputs[i].pin;
2423 		unsigned int caps;
2424 		const char *label;
2425 		char name[32];
2426 
2427 		if (cfg->inputs[i].type != AUTO_PIN_MIC)
2428 			continue;
2429 		caps = query_amp_caps(codec, pin, HDA_INPUT);
2430 		if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
2431 			continue;
2432 		label = hda_get_autocfg_input_label(codec, cfg, i);
2433 		if (prev_label && !strcmp(label, prev_label))
2434 			type_idx++;
2435 		else
2436 			type_idx = 0;
2437 		prev_label = label;
2438 		snprintf(name, sizeof(name), "%s Boost Volume", label);
2439 		err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
2440 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
2441 		if (err < 0)
2442 			return err;
2443 	}
2444 	return 0;
2445 }
2446 
2447 /* create capture and input-src controls for multiple streams */
2448 static int create_multi_adc_ctls(struct hda_codec *codec)
2449 {
2450 	struct via_spec *spec = codec->spec;
2451 	int i, err;
2452 
2453 	/* create capture mixer elements */
2454 	for (i = 0; i < spec->num_adc_nids; i++) {
2455 		hda_nid_t adc = spec->adc_nids[i];
2456 		err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
2457 					"Capture Volume", i,
2458 					HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2459 							    HDA_INPUT));
2460 		if (err < 0)
2461 			return err;
2462 		err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2463 					"Capture Switch", i,
2464 					HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2465 							    HDA_INPUT));
2466 		if (err < 0)
2467 			return err;
2468 	}
2469 
2470 	/* input-source control */
2471 	for (i = 0; i < spec->num_adc_nids; i++)
2472 		if (!spec->mux_nids[i])
2473 			break;
2474 	err = create_input_src_ctls(codec, i);
2475 	if (err < 0)
2476 		return err;
2477 	return 0;
2478 }
2479 
2480 /* bind capture volume/switch */
2481 static struct snd_kcontrol_new via_bind_cap_vol_ctl =
2482 	HDA_BIND_VOL("Capture Volume", 0);
2483 static struct snd_kcontrol_new via_bind_cap_sw_ctl =
2484 	HDA_BIND_SW("Capture Switch", 0);
2485 
2486 static int init_bind_ctl(struct via_spec *spec, struct hda_bind_ctls **ctl_ret,
2487 			 struct hda_ctl_ops *ops)
2488 {
2489 	struct hda_bind_ctls *ctl;
2490 	int i;
2491 
2492 	ctl = kzalloc(sizeof(*ctl) + sizeof(long) * 4, GFP_KERNEL);
2493 	if (!ctl)
2494 		return -ENOMEM;
2495 	ctl->ops = ops;
2496 	for (i = 0; i < spec->num_adc_nids; i++)
2497 		ctl->values[i] =
2498 			HDA_COMPOSE_AMP_VAL(spec->adc_nids[i], 3, 0, HDA_INPUT);
2499 	*ctl_ret = ctl;
2500 	return 0;
2501 }
2502 
2503 /* create capture and input-src controls for dynamic ADC-switch case */
2504 static int create_dyn_adc_ctls(struct hda_codec *codec)
2505 {
2506 	struct via_spec *spec = codec->spec;
2507 	struct snd_kcontrol_new *knew;
2508 	int err;
2509 
2510 	/* set up the bind capture ctls */
2511 	err = init_bind_ctl(spec, &spec->bind_cap_vol, &snd_hda_bind_vol);
2512 	if (err < 0)
2513 		return err;
2514 	err = init_bind_ctl(spec, &spec->bind_cap_sw, &snd_hda_bind_sw);
2515 	if (err < 0)
2516 		return err;
2517 
2518 	/* create capture mixer elements */
2519 	knew = via_clone_control(spec, &via_bind_cap_vol_ctl);
2520 	if (!knew)
2521 		return -ENOMEM;
2522 	knew->private_value = (long)spec->bind_cap_vol;
2523 
2524 	knew = via_clone_control(spec, &via_bind_cap_sw_ctl);
2525 	if (!knew)
2526 		return -ENOMEM;
2527 	knew->private_value = (long)spec->bind_cap_sw;
2528 
2529 	/* input-source control */
2530 	err = create_input_src_ctls(codec, 1);
2531 	if (err < 0)
2532 		return err;
2533 	return 0;
2534 }
2535 
2536 /* parse and create capture-related stuff */
2537 static int via_auto_create_analog_input_ctls(struct hda_codec *codec)
2538 {
2539 	struct via_spec *spec = codec->spec;
2540 	int err;
2541 
2542 	err = parse_analog_inputs(codec);
2543 	if (err < 0)
2544 		return err;
2545 	if (spec->dyn_adc_switch)
2546 		err = create_dyn_adc_ctls(codec);
2547 	else
2548 		err = create_multi_adc_ctls(codec);
2549 	if (err < 0)
2550 		return err;
2551 	err = create_loopback_ctls(codec);
2552 	if (err < 0)
2553 		return err;
2554 	err = create_mic_boost_ctls(codec);
2555 	if (err < 0)
2556 		return err;
2557 	return 0;
2558 }
2559 
2560 static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
2561 {
2562 	unsigned int def_conf;
2563 	unsigned char seqassoc;
2564 
2565 	def_conf = snd_hda_codec_get_pincfg(codec, nid);
2566 	seqassoc = (unsigned char) get_defcfg_association(def_conf);
2567 	seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
2568 	if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
2569 	    && (seqassoc == 0xf0 || seqassoc == 0xff)) {
2570 		def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
2571 		snd_hda_codec_set_pincfg(codec, nid, def_conf);
2572 	}
2573 
2574 	return;
2575 }
2576 
2577 static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
2578 				     struct snd_ctl_elem_value *ucontrol)
2579 {
2580 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2581 	struct via_spec *spec = codec->spec;
2582 
2583 	if (spec->codec_type != VT1708)
2584 		return 0;
2585 	ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
2586 	return 0;
2587 }
2588 
2589 static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
2590 				     struct snd_ctl_elem_value *ucontrol)
2591 {
2592 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2593 	struct via_spec *spec = codec->spec;
2594 	int val;
2595 
2596 	if (spec->codec_type != VT1708)
2597 		return 0;
2598 	val = !!ucontrol->value.integer.value[0];
2599 	if (spec->vt1708_jack_detect == val)
2600 		return 0;
2601 	spec->vt1708_jack_detect = val;
2602 	if (spec->vt1708_jack_detect &&
2603 	    snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") != 1) {
2604 		mute_aa_path(codec, 1);
2605 		notify_aa_path_ctls(codec);
2606 	}
2607 	via_hp_automute(codec);
2608 	vt1708_update_hp_work(spec);
2609 	return 1;
2610 }
2611 
2612 static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
2613 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2614 	.name = "Jack Detect",
2615 	.count = 1,
2616 	.info = snd_ctl_boolean_mono_info,
2617 	.get = vt1708_jack_detect_get,
2618 	.put = vt1708_jack_detect_put,
2619 };
2620 
2621 static void fill_dig_outs(struct hda_codec *codec);
2622 static void fill_dig_in(struct hda_codec *codec);
2623 
2624 static int via_parse_auto_config(struct hda_codec *codec)
2625 {
2626 	struct via_spec *spec = codec->spec;
2627 	int err;
2628 
2629 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2630 	if (err < 0)
2631 		return err;
2632 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2633 		return -EINVAL;
2634 
2635 	err = via_auto_create_multi_out_ctls(codec);
2636 	if (err < 0)
2637 		return err;
2638 	err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
2639 	if (err < 0)
2640 		return err;
2641 	err = via_auto_create_speaker_ctls(codec);
2642 	if (err < 0)
2643 		return err;
2644 	err = via_auto_create_loopback_switch(codec);
2645 	if (err < 0)
2646 		return err;
2647 	err = via_auto_create_analog_input_ctls(codec);
2648 	if (err < 0)
2649 		return err;
2650 
2651 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2652 
2653 	fill_dig_outs(codec);
2654 	fill_dig_in(codec);
2655 
2656 	if (spec->kctls.list)
2657 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
2658 
2659 
2660 	if (spec->hp_dac_nid && spec->hp_mix_path.depth) {
2661 		err = via_hp_build(codec);
2662 		if (err < 0)
2663 			return err;
2664 	}
2665 
2666 	err = via_smart51_build(codec);
2667 	if (err < 0)
2668 		return err;
2669 
2670 	/* assign slave outs */
2671 	if (spec->slave_dig_outs[0])
2672 		codec->slave_dig_outs = spec->slave_dig_outs;
2673 
2674 	return 1;
2675 }
2676 
2677 static void via_auto_init_dig_outs(struct hda_codec *codec)
2678 {
2679 	struct via_spec *spec = codec->spec;
2680 	if (spec->multiout.dig_out_nid)
2681 		init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
2682 	if (spec->slave_dig_outs[0])
2683 		init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
2684 }
2685 
2686 static void via_auto_init_dig_in(struct hda_codec *codec)
2687 {
2688 	struct via_spec *spec = codec->spec;
2689 	if (!spec->dig_in_nid)
2690 		return;
2691 	snd_hda_set_pin_ctl(codec, spec->autocfg.dig_in_pin, PIN_IN);
2692 }
2693 
2694 static void via_jack_output_event(struct hda_codec *codec, struct hda_jack_tbl *tbl)
2695 {
2696 	set_widgets_power_state(codec);
2697 	via_hp_automute(codec);
2698 }
2699 
2700 static void via_jack_powerstate_event(struct hda_codec *codec, struct hda_jack_tbl *tbl)
2701 {
2702 	set_widgets_power_state(codec);
2703 }
2704 
2705 /* initialize the unsolicited events */
2706 static void via_auto_init_unsol_event(struct hda_codec *codec)
2707 {
2708 	struct via_spec *spec = codec->spec;
2709 	struct auto_pin_cfg *cfg = &spec->autocfg;
2710 	unsigned int ev;
2711 	int i;
2712 	hda_jack_callback cb;
2713 
2714 	if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
2715 		snd_hda_jack_detect_enable_callback(codec, cfg->hp_pins[0],
2716 						    VIA_HP_EVENT | VIA_JACK_EVENT,
2717 						    via_jack_output_event);
2718 
2719 	if (cfg->speaker_pins[0])
2720 		ev = VIA_LINE_EVENT;
2721 	else
2722 		ev = 0;
2723 	cb = ev ? via_jack_output_event : via_jack_powerstate_event;
2724 
2725 	for (i = 0; i < cfg->line_outs; i++) {
2726 		if (cfg->line_out_pins[i] &&
2727 		    is_jack_detectable(codec, cfg->line_out_pins[i]))
2728 			snd_hda_jack_detect_enable_callback(codec, cfg->line_out_pins[i],
2729 							    ev | VIA_JACK_EVENT, cb);
2730 	}
2731 
2732 	for (i = 0; i < cfg->num_inputs; i++) {
2733 		if (is_jack_detectable(codec, cfg->inputs[i].pin))
2734 			snd_hda_jack_detect_enable_callback(codec, cfg->inputs[i].pin,
2735 							    VIA_JACK_EVENT,
2736 							    via_jack_powerstate_event);
2737 	}
2738 }
2739 
2740 static int via_init(struct hda_codec *codec)
2741 {
2742 	struct via_spec *spec = codec->spec;
2743 	int i;
2744 
2745 	for (i = 0; i < spec->num_iverbs; i++)
2746 		snd_hda_sequence_write(codec, spec->init_verbs[i]);
2747 
2748 	/* init power states */
2749 	set_widgets_power_state(codec);
2750 	__analog_low_current_mode(codec, true);
2751 
2752 	via_auto_init_multi_out(codec);
2753 	via_auto_init_hp_out(codec);
2754 	via_auto_init_speaker_out(codec);
2755 	via_auto_init_analog_input(codec);
2756 	via_auto_init_dig_outs(codec);
2757 	via_auto_init_dig_in(codec);
2758 
2759 	via_auto_init_unsol_event(codec);
2760 
2761 	via_hp_automute(codec);
2762 	vt1708_update_hp_work(spec);
2763 
2764 	return 0;
2765 }
2766 
2767 static void vt1708_update_hp_jack_state(struct work_struct *work)
2768 {
2769 	struct via_spec *spec = container_of(work, struct via_spec,
2770 					     vt1708_hp_work.work);
2771 	if (spec->codec_type != VT1708)
2772 		return;
2773 	snd_hda_jack_set_dirty_all(spec->codec);
2774 	/* if jack state toggled */
2775 	if (spec->vt1708_hp_present
2776 	    != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
2777 		spec->vt1708_hp_present ^= 1;
2778 		via_hp_automute(spec->codec);
2779 	}
2780 	if (spec->vt1708_jack_detect)
2781 		schedule_delayed_work(&spec->vt1708_hp_work,
2782 				      msecs_to_jiffies(100));
2783 }
2784 
2785 static int get_mux_nids(struct hda_codec *codec)
2786 {
2787 	struct via_spec *spec = codec->spec;
2788 	hda_nid_t nid, conn[8];
2789 	unsigned int type;
2790 	int i, n;
2791 
2792 	for (i = 0; i < spec->num_adc_nids; i++) {
2793 		nid = spec->adc_nids[i];
2794 		while (nid) {
2795 			type = get_wcaps_type(get_wcaps(codec, nid));
2796 			if (type == AC_WID_PIN)
2797 				break;
2798 			n = snd_hda_get_connections(codec, nid, conn,
2799 						    ARRAY_SIZE(conn));
2800 			if (n <= 0)
2801 				break;
2802 			if (n > 1) {
2803 				spec->mux_nids[i] = nid;
2804 				break;
2805 			}
2806 			nid = conn[0];
2807 		}
2808 	}
2809 	return 0;
2810 }
2811 
2812 static int patch_vt1708(struct hda_codec *codec)
2813 {
2814 	struct via_spec *spec;
2815 	int err;
2816 
2817 	/* create a codec specific record */
2818 	spec = via_new_spec(codec);
2819 	if (spec == NULL)
2820 		return -ENOMEM;
2821 
2822 	spec->aa_mix_nid = 0x17;
2823 
2824 	/* Add HP and CD pin config connect bit re-config action */
2825 	vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
2826 	vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
2827 
2828 	/* automatic parse from the BIOS config */
2829 	err = via_parse_auto_config(codec);
2830 	if (err < 0) {
2831 		via_free(codec);
2832 		return err;
2833 	}
2834 
2835 	/* add jack detect on/off control */
2836 	if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
2837 		return -ENOMEM;
2838 
2839 	/* disable 32bit format on VT1708 */
2840 	if (codec->vendor_id == 0x11061708)
2841 		spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
2842 
2843 	spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
2844 
2845 	codec->patch_ops = via_patch_ops;
2846 
2847 	INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
2848 	return 0;
2849 }
2850 
2851 static int patch_vt1709(struct hda_codec *codec)
2852 {
2853 	struct via_spec *spec;
2854 	int err;
2855 
2856 	/* create a codec specific record */
2857 	spec = via_new_spec(codec);
2858 	if (spec == NULL)
2859 		return -ENOMEM;
2860 
2861 	spec->aa_mix_nid = 0x18;
2862 
2863 	err = via_parse_auto_config(codec);
2864 	if (err < 0) {
2865 		via_free(codec);
2866 		return err;
2867 	}
2868 
2869 	codec->patch_ops = via_patch_ops;
2870 
2871 	return 0;
2872 }
2873 
2874 static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
2875 {
2876 	struct via_spec *spec = codec->spec;
2877 	int imux_is_smixer;
2878 	unsigned int parm;
2879 	int is_8ch = 0;
2880 	if ((spec->codec_type != VT1708B_4CH) &&
2881 	    (codec->vendor_id != 0x11064397))
2882 		is_8ch = 1;
2883 
2884 	/* SW0 (17h) = stereo mixer */
2885 	imux_is_smixer =
2886 	(snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
2887 	 == ((spec->codec_type == VT1708S) ? 5 : 0));
2888 	/* inputs */
2889 	/* PW 1/2/5 (1ah/1bh/1eh) */
2890 	parm = AC_PWRST_D3;
2891 	set_pin_power_state(codec, 0x1a, &parm);
2892 	set_pin_power_state(codec, 0x1b, &parm);
2893 	set_pin_power_state(codec, 0x1e, &parm);
2894 	if (imux_is_smixer)
2895 		parm = AC_PWRST_D0;
2896 	/* SW0 (17h), AIW 0/1 (13h/14h) */
2897 	update_power_state(codec, 0x17, parm);
2898 	update_power_state(codec, 0x13, parm);
2899 	update_power_state(codec, 0x14, parm);
2900 
2901 	/* outputs */
2902 	/* PW0 (19h), SW1 (18h), AOW1 (11h) */
2903 	parm = AC_PWRST_D3;
2904 	set_pin_power_state(codec, 0x19, &parm);
2905 	if (spec->smart51_enabled)
2906 		set_pin_power_state(codec, 0x1b, &parm);
2907 	update_power_state(codec, 0x18, parm);
2908 	update_power_state(codec, 0x11, parm);
2909 
2910 	/* PW6 (22h), SW2 (26h), AOW2 (24h) */
2911 	if (is_8ch) {
2912 		parm = AC_PWRST_D3;
2913 		set_pin_power_state(codec, 0x22, &parm);
2914 		if (spec->smart51_enabled)
2915 			set_pin_power_state(codec, 0x1a, &parm);
2916 		update_power_state(codec, 0x26, parm);
2917 		update_power_state(codec, 0x24, parm);
2918 	} else if (codec->vendor_id == 0x11064397) {
2919 		/* PW7(23h), SW2(27h), AOW2(25h) */
2920 		parm = AC_PWRST_D3;
2921 		set_pin_power_state(codec, 0x23, &parm);
2922 		if (spec->smart51_enabled)
2923 			set_pin_power_state(codec, 0x1a, &parm);
2924 		update_power_state(codec, 0x27, parm);
2925 		update_power_state(codec, 0x25, parm);
2926 	}
2927 
2928 	/* PW 3/4/7 (1ch/1dh/23h) */
2929 	parm = AC_PWRST_D3;
2930 	/* force to D0 for internal Speaker */
2931 	set_pin_power_state(codec, 0x1c, &parm);
2932 	set_pin_power_state(codec, 0x1d, &parm);
2933 	if (is_8ch)
2934 		set_pin_power_state(codec, 0x23, &parm);
2935 
2936 	/* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
2937 	update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
2938 	update_power_state(codec, 0x10, parm);
2939 	if (is_8ch) {
2940 		update_power_state(codec, 0x25, parm);
2941 		update_power_state(codec, 0x27, parm);
2942 	} else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
2943 		update_power_state(codec, 0x25, parm);
2944 }
2945 
2946 static int patch_vt1708S(struct hda_codec *codec);
2947 static int patch_vt1708B(struct hda_codec *codec)
2948 {
2949 	struct via_spec *spec;
2950 	int err;
2951 
2952 	if (get_codec_type(codec) == VT1708BCE)
2953 		return patch_vt1708S(codec);
2954 
2955 	/* create a codec specific record */
2956 	spec = via_new_spec(codec);
2957 	if (spec == NULL)
2958 		return -ENOMEM;
2959 
2960 	spec->aa_mix_nid = 0x16;
2961 
2962 	/* automatic parse from the BIOS config */
2963 	err = via_parse_auto_config(codec);
2964 	if (err < 0) {
2965 		via_free(codec);
2966 		return err;
2967 	}
2968 
2969 	codec->patch_ops = via_patch_ops;
2970 
2971 	spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
2972 
2973 	return 0;
2974 }
2975 
2976 /* Patch for VT1708S */
2977 static const struct hda_verb vt1708S_init_verbs[] = {
2978 	/* Enable Mic Boost Volume backdoor */
2979 	{0x1, 0xf98, 0x1},
2980 	/* don't bybass mixer */
2981 	{0x1, 0xf88, 0xc0},
2982 	{ }
2983 };
2984 
2985 /* fill out digital output widgets; one for master and one for slave outputs */
2986 static void fill_dig_outs(struct hda_codec *codec)
2987 {
2988 	struct via_spec *spec = codec->spec;
2989 	int i;
2990 
2991 	for (i = 0; i < spec->autocfg.dig_outs; i++) {
2992 		hda_nid_t nid;
2993 		int conn;
2994 
2995 		nid = spec->autocfg.dig_out_pins[i];
2996 		if (!nid)
2997 			continue;
2998 		conn = snd_hda_get_connections(codec, nid, &nid, 1);
2999 		if (conn < 1)
3000 			continue;
3001 		if (!spec->multiout.dig_out_nid)
3002 			spec->multiout.dig_out_nid = nid;
3003 		else {
3004 			spec->slave_dig_outs[0] = nid;
3005 			break; /* at most two dig outs */
3006 		}
3007 	}
3008 }
3009 
3010 static void fill_dig_in(struct hda_codec *codec)
3011 {
3012 	struct via_spec *spec = codec->spec;
3013 	hda_nid_t dig_nid;
3014 	int i, err;
3015 
3016 	if (!spec->autocfg.dig_in_pin)
3017 		return;
3018 
3019 	dig_nid = codec->start_nid;
3020 	for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3021 		unsigned int wcaps = get_wcaps(codec, dig_nid);
3022 		if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3023 			continue;
3024 		if (!(wcaps & AC_WCAP_DIGITAL))
3025 			continue;
3026 		if (!(wcaps & AC_WCAP_CONN_LIST))
3027 			continue;
3028 		err = get_connection_index(codec, dig_nid,
3029 					   spec->autocfg.dig_in_pin);
3030 		if (err >= 0) {
3031 			spec->dig_in_nid = dig_nid;
3032 			break;
3033 		}
3034 	}
3035 }
3036 
3037 static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
3038 			       int offset, int num_steps, int step_size)
3039 {
3040 	snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
3041 				  (offset << AC_AMPCAP_OFFSET_SHIFT) |
3042 				  (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
3043 				  (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
3044 				  (0 << AC_AMPCAP_MUTE_SHIFT));
3045 }
3046 
3047 static int patch_vt1708S(struct hda_codec *codec)
3048 {
3049 	struct via_spec *spec;
3050 	int err;
3051 
3052 	/* create a codec specific record */
3053 	spec = via_new_spec(codec);
3054 	if (spec == NULL)
3055 		return -ENOMEM;
3056 
3057 	spec->aa_mix_nid = 0x16;
3058 	override_mic_boost(codec, 0x1a, 0, 3, 40);
3059 	override_mic_boost(codec, 0x1e, 0, 3, 40);
3060 
3061 	/* automatic parse from the BIOS config */
3062 	err = via_parse_auto_config(codec);
3063 	if (err < 0) {
3064 		via_free(codec);
3065 		return err;
3066 	}
3067 
3068 	spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
3069 
3070 	codec->patch_ops = via_patch_ops;
3071 
3072 	/* correct names for VT1708BCE */
3073 	if (get_codec_type(codec) == VT1708BCE)	{
3074 		kfree(codec->chip_name);
3075 		codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
3076 		snprintf(codec->bus->card->mixername,
3077 			 sizeof(codec->bus->card->mixername),
3078 			 "%s %s", codec->vendor_name, codec->chip_name);
3079 	}
3080 	/* correct names for VT1705 */
3081 	if (codec->vendor_id == 0x11064397)	{
3082 		kfree(codec->chip_name);
3083 		codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
3084 		snprintf(codec->bus->card->mixername,
3085 			 sizeof(codec->bus->card->mixername),
3086 			 "%s %s", codec->vendor_name, codec->chip_name);
3087 	}
3088 	spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
3089 	return 0;
3090 }
3091 
3092 /* Patch for VT1702 */
3093 
3094 static const struct hda_verb vt1702_init_verbs[] = {
3095 	/* mixer enable */
3096 	{0x1, 0xF88, 0x3},
3097 	/* GPIO 0~2 */
3098 	{0x1, 0xF82, 0x3F},
3099 	{ }
3100 };
3101 
3102 static void set_widgets_power_state_vt1702(struct hda_codec *codec)
3103 {
3104 	int imux_is_smixer =
3105 	snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
3106 	unsigned int parm;
3107 	/* inputs */
3108 	/* PW 1/2/5 (14h/15h/18h) */
3109 	parm = AC_PWRST_D3;
3110 	set_pin_power_state(codec, 0x14, &parm);
3111 	set_pin_power_state(codec, 0x15, &parm);
3112 	set_pin_power_state(codec, 0x18, &parm);
3113 	if (imux_is_smixer)
3114 		parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
3115 	/* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
3116 	update_power_state(codec, 0x13, parm);
3117 	update_power_state(codec, 0x12, parm);
3118 	update_power_state(codec, 0x1f, parm);
3119 	update_power_state(codec, 0x20, parm);
3120 
3121 	/* outputs */
3122 	/* PW 3/4 (16h/17h) */
3123 	parm = AC_PWRST_D3;
3124 	set_pin_power_state(codec, 0x17, &parm);
3125 	set_pin_power_state(codec, 0x16, &parm);
3126 	/* MW0 (1ah), AOW 0/1 (10h/1dh) */
3127 	update_power_state(codec, 0x1a, imux_is_smixer ? AC_PWRST_D0 : parm);
3128 	update_power_state(codec, 0x10, parm);
3129 	update_power_state(codec, 0x1d, parm);
3130 }
3131 
3132 static int patch_vt1702(struct hda_codec *codec)
3133 {
3134 	struct via_spec *spec;
3135 	int err;
3136 
3137 	/* create a codec specific record */
3138 	spec = via_new_spec(codec);
3139 	if (spec == NULL)
3140 		return -ENOMEM;
3141 
3142 	spec->aa_mix_nid = 0x1a;
3143 
3144 	/* limit AA path volume to 0 dB */
3145 	snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
3146 				  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
3147 				  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
3148 				  (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
3149 				  (1 << AC_AMPCAP_MUTE_SHIFT));
3150 
3151 	/* automatic parse from the BIOS config */
3152 	err = via_parse_auto_config(codec);
3153 	if (err < 0) {
3154 		via_free(codec);
3155 		return err;
3156 	}
3157 
3158 	spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
3159 
3160 	codec->patch_ops = via_patch_ops;
3161 
3162 	spec->set_widgets_power_state =  set_widgets_power_state_vt1702;
3163 	return 0;
3164 }
3165 
3166 /* Patch for VT1718S */
3167 
3168 static const struct hda_verb vt1718S_init_verbs[] = {
3169 	/* Enable MW0 adjust Gain 5 */
3170 	{0x1, 0xfb2, 0x10},
3171 	/* Enable Boost Volume backdoor */
3172 	{0x1, 0xf88, 0x8},
3173 
3174 	{ }
3175 };
3176 
3177 static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
3178 {
3179 	struct via_spec *spec = codec->spec;
3180 	int imux_is_smixer;
3181 	unsigned int parm, parm2;
3182 	/* MUX6 (1eh) = stereo mixer */
3183 	imux_is_smixer =
3184 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
3185 	/* inputs */
3186 	/* PW 5/6/7 (29h/2ah/2bh) */
3187 	parm = AC_PWRST_D3;
3188 	set_pin_power_state(codec, 0x29, &parm);
3189 	set_pin_power_state(codec, 0x2a, &parm);
3190 	set_pin_power_state(codec, 0x2b, &parm);
3191 	if (imux_is_smixer)
3192 		parm = AC_PWRST_D0;
3193 	/* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
3194 	update_power_state(codec, 0x1e, parm);
3195 	update_power_state(codec, 0x1f, parm);
3196 	update_power_state(codec, 0x10, parm);
3197 	update_power_state(codec, 0x11, parm);
3198 
3199 	/* outputs */
3200 	/* PW3 (27h), MW2 (1ah), AOW3 (bh) */
3201 	parm = AC_PWRST_D3;
3202 	set_pin_power_state(codec, 0x27, &parm);
3203 	update_power_state(codec, 0x1a, parm);
3204 	parm2 = parm; /* for pin 0x0b */
3205 
3206 	/* PW2 (26h), AOW2 (ah) */
3207 	parm = AC_PWRST_D3;
3208 	set_pin_power_state(codec, 0x26, &parm);
3209 	if (spec->smart51_enabled)
3210 		set_pin_power_state(codec, 0x2b, &parm);
3211 	update_power_state(codec, 0xa, parm);
3212 
3213 	/* PW0 (24h), AOW0 (8h) */
3214 	parm = AC_PWRST_D3;
3215 	set_pin_power_state(codec, 0x24, &parm);
3216 	if (!spec->hp_independent_mode) /* check for redirected HP */
3217 		set_pin_power_state(codec, 0x28, &parm);
3218 	update_power_state(codec, 0x8, parm);
3219 	if (!spec->hp_independent_mode && parm2 != AC_PWRST_D3)
3220 		parm = parm2;
3221 	update_power_state(codec, 0xb, parm);
3222 	/* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
3223 	update_power_state(codec, 0x21, imux_is_smixer ? AC_PWRST_D0 : parm);
3224 
3225 	/* PW1 (25h), AOW1 (9h) */
3226 	parm = AC_PWRST_D3;
3227 	set_pin_power_state(codec, 0x25, &parm);
3228 	if (spec->smart51_enabled)
3229 		set_pin_power_state(codec, 0x2a, &parm);
3230 	update_power_state(codec, 0x9, parm);
3231 
3232 	if (spec->hp_independent_mode) {
3233 		/* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
3234 		parm = AC_PWRST_D3;
3235 		set_pin_power_state(codec, 0x28, &parm);
3236 		update_power_state(codec, 0x1b, parm);
3237 		update_power_state(codec, 0x34, parm);
3238 		update_power_state(codec, 0xc, parm);
3239 	}
3240 }
3241 
3242 /* Add a connection to the primary DAC from AA-mixer for some codecs
3243  * This isn't listed from the raw info, but the chip has a secret connection.
3244  */
3245 static int add_secret_dac_path(struct hda_codec *codec)
3246 {
3247 	struct via_spec *spec = codec->spec;
3248 	int i, nums;
3249 	hda_nid_t conn[8];
3250 	hda_nid_t nid;
3251 
3252 	if (!spec->aa_mix_nid)
3253 		return 0;
3254 	nums = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
3255 				       ARRAY_SIZE(conn) - 1);
3256 	for (i = 0; i < nums; i++) {
3257 		if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT)
3258 			return 0;
3259 	}
3260 
3261 	/* find the primary DAC and add to the connection list */
3262 	nid = codec->start_nid;
3263 	for (i = 0; i < codec->num_nodes; i++, nid++) {
3264 		unsigned int caps = get_wcaps(codec, nid);
3265 		if (get_wcaps_type(caps) == AC_WID_AUD_OUT &&
3266 		    !(caps & AC_WCAP_DIGITAL)) {
3267 			conn[nums++] = nid;
3268 			return snd_hda_override_conn_list(codec,
3269 							  spec->aa_mix_nid,
3270 							  nums, conn);
3271 		}
3272 	}
3273 	return 0;
3274 }
3275 
3276 
3277 static int patch_vt1718S(struct hda_codec *codec)
3278 {
3279 	struct via_spec *spec;
3280 	int err;
3281 
3282 	/* create a codec specific record */
3283 	spec = via_new_spec(codec);
3284 	if (spec == NULL)
3285 		return -ENOMEM;
3286 
3287 	spec->aa_mix_nid = 0x21;
3288 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3289 	override_mic_boost(codec, 0x29, 0, 3, 40);
3290 	add_secret_dac_path(codec);
3291 
3292 	/* automatic parse from the BIOS config */
3293 	err = via_parse_auto_config(codec);
3294 	if (err < 0) {
3295 		via_free(codec);
3296 		return err;
3297 	}
3298 
3299 	spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
3300 
3301 	codec->patch_ops = via_patch_ops;
3302 
3303 	spec->set_widgets_power_state =  set_widgets_power_state_vt1718S;
3304 
3305 	return 0;
3306 }
3307 
3308 /* Patch for VT1716S */
3309 
3310 static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
3311 			    struct snd_ctl_elem_info *uinfo)
3312 {
3313 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3314 	uinfo->count = 1;
3315 	uinfo->value.integer.min = 0;
3316 	uinfo->value.integer.max = 1;
3317 	return 0;
3318 }
3319 
3320 static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
3321 			   struct snd_ctl_elem_value *ucontrol)
3322 {
3323 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3324 	int index = 0;
3325 
3326 	index = snd_hda_codec_read(codec, 0x26, 0,
3327 					       AC_VERB_GET_CONNECT_SEL, 0);
3328 	if (index != -1)
3329 		*ucontrol->value.integer.value = index;
3330 
3331 	return 0;
3332 }
3333 
3334 static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
3335 			   struct snd_ctl_elem_value *ucontrol)
3336 {
3337 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3338 	struct via_spec *spec = codec->spec;
3339 	int index = *ucontrol->value.integer.value;
3340 
3341 	snd_hda_codec_write(codec, 0x26, 0,
3342 					       AC_VERB_SET_CONNECT_SEL, index);
3343 	spec->dmic_enabled = index;
3344 	set_widgets_power_state(codec);
3345 	return 1;
3346 }
3347 
3348 static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
3349 	HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
3350 	{
3351 	 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3352 	 .name = "Digital Mic Capture Switch",
3353 	 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
3354 	 .count = 1,
3355 	 .info = vt1716s_dmic_info,
3356 	 .get = vt1716s_dmic_get,
3357 	 .put = vt1716s_dmic_put,
3358 	 },
3359 	{}			/* end */
3360 };
3361 
3362 
3363 /* mono-out mixer elements */
3364 static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
3365 	HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
3366 	{ } /* end */
3367 };
3368 
3369 static const struct hda_verb vt1716S_init_verbs[] = {
3370 	/* Enable Boost Volume backdoor */
3371 	{0x1, 0xf8a, 0x80},
3372 	/* don't bybass mixer */
3373 	{0x1, 0xf88, 0xc0},
3374 	/* Enable mono output */
3375 	{0x1, 0xf90, 0x08},
3376 	{ }
3377 };
3378 
3379 static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
3380 {
3381 	struct via_spec *spec = codec->spec;
3382 	int imux_is_smixer;
3383 	unsigned int parm;
3384 	unsigned int mono_out, present;
3385 	/* SW0 (17h) = stereo mixer */
3386 	imux_is_smixer =
3387 	(snd_hda_codec_read(codec, 0x17, 0,
3388 			    AC_VERB_GET_CONNECT_SEL, 0x00) ==  5);
3389 	/* inputs */
3390 	/* PW 1/2/5 (1ah/1bh/1eh) */
3391 	parm = AC_PWRST_D3;
3392 	set_pin_power_state(codec, 0x1a, &parm);
3393 	set_pin_power_state(codec, 0x1b, &parm);
3394 	set_pin_power_state(codec, 0x1e, &parm);
3395 	if (imux_is_smixer)
3396 		parm = AC_PWRST_D0;
3397 	/* SW0 (17h), AIW0(13h) */
3398 	update_power_state(codec, 0x17, parm);
3399 	update_power_state(codec, 0x13, parm);
3400 
3401 	parm = AC_PWRST_D3;
3402 	set_pin_power_state(codec, 0x1e, &parm);
3403 	/* PW11 (22h) */
3404 	if (spec->dmic_enabled)
3405 		set_pin_power_state(codec, 0x22, &parm);
3406 	else
3407 		update_power_state(codec, 0x22, AC_PWRST_D3);
3408 
3409 	/* SW2(26h), AIW1(14h) */
3410 	update_power_state(codec, 0x26, parm);
3411 	update_power_state(codec, 0x14, parm);
3412 
3413 	/* outputs */
3414 	/* PW0 (19h), SW1 (18h), AOW1 (11h) */
3415 	parm = AC_PWRST_D3;
3416 	set_pin_power_state(codec, 0x19, &parm);
3417 	/* Smart 5.1 PW2(1bh) */
3418 	if (spec->smart51_enabled)
3419 		set_pin_power_state(codec, 0x1b, &parm);
3420 	update_power_state(codec, 0x18, parm);
3421 	update_power_state(codec, 0x11, parm);
3422 
3423 	/* PW7 (23h), SW3 (27h), AOW3 (25h) */
3424 	parm = AC_PWRST_D3;
3425 	set_pin_power_state(codec, 0x23, &parm);
3426 	/* Smart 5.1 PW1(1ah) */
3427 	if (spec->smart51_enabled)
3428 		set_pin_power_state(codec, 0x1a, &parm);
3429 	update_power_state(codec, 0x27, parm);
3430 
3431 	/* Smart 5.1 PW5(1eh) */
3432 	if (spec->smart51_enabled)
3433 		set_pin_power_state(codec, 0x1e, &parm);
3434 	update_power_state(codec, 0x25, parm);
3435 
3436 	/* Mono out */
3437 	/* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
3438 	present = snd_hda_jack_detect(codec, 0x1c);
3439 
3440 	if (present)
3441 		mono_out = 0;
3442 	else {
3443 		present = snd_hda_jack_detect(codec, 0x1d);
3444 		if (!spec->hp_independent_mode && present)
3445 			mono_out = 0;
3446 		else
3447 			mono_out = 1;
3448 	}
3449 	parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
3450 	update_power_state(codec, 0x28, parm);
3451 	update_power_state(codec, 0x29, parm);
3452 	update_power_state(codec, 0x2a, parm);
3453 
3454 	/* PW 3/4 (1ch/1dh) */
3455 	parm = AC_PWRST_D3;
3456 	set_pin_power_state(codec, 0x1c, &parm);
3457 	set_pin_power_state(codec, 0x1d, &parm);
3458 	/* HP Independent Mode, power on AOW3 */
3459 	if (spec->hp_independent_mode)
3460 		update_power_state(codec, 0x25, parm);
3461 
3462 	/* force to D0 for internal Speaker */
3463 	/* MW0 (16h), AOW0 (10h) */
3464 	update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
3465 	update_power_state(codec, 0x10, mono_out ? AC_PWRST_D0 : parm);
3466 }
3467 
3468 static int patch_vt1716S(struct hda_codec *codec)
3469 {
3470 	struct via_spec *spec;
3471 	int err;
3472 
3473 	/* create a codec specific record */
3474 	spec = via_new_spec(codec);
3475 	if (spec == NULL)
3476 		return -ENOMEM;
3477 
3478 	spec->aa_mix_nid = 0x16;
3479 	override_mic_boost(codec, 0x1a, 0, 3, 40);
3480 	override_mic_boost(codec, 0x1e, 0, 3, 40);
3481 
3482 	/* automatic parse from the BIOS config */
3483 	err = via_parse_auto_config(codec);
3484 	if (err < 0) {
3485 		via_free(codec);
3486 		return err;
3487 	}
3488 
3489 	spec->init_verbs[spec->num_iverbs++]  = vt1716S_init_verbs;
3490 
3491 	spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
3492 	spec->num_mixers++;
3493 
3494 	spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
3495 
3496 	codec->patch_ops = via_patch_ops;
3497 
3498 	spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
3499 	return 0;
3500 }
3501 
3502 /* for vt2002P */
3503 
3504 static const struct hda_verb vt2002P_init_verbs[] = {
3505 	/* Class-D speaker related verbs */
3506 	{0x1, 0xfe0, 0x4},
3507 	{0x1, 0xfe9, 0x80},
3508 	{0x1, 0xfe2, 0x22},
3509 	/* Enable Boost Volume backdoor */
3510 	{0x1, 0xfb9, 0x24},
3511 	/* Enable AOW0 to MW9 */
3512 	{0x1, 0xfb8, 0x88},
3513 	{ }
3514 };
3515 
3516 static const struct hda_verb vt1802_init_verbs[] = {
3517 	/* Enable Boost Volume backdoor */
3518 	{0x1, 0xfb9, 0x24},
3519 	/* Enable AOW0 to MW9 */
3520 	{0x1, 0xfb8, 0x88},
3521 	{ }
3522 };
3523 
3524 static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
3525 {
3526 	struct via_spec *spec = codec->spec;
3527 	int imux_is_smixer;
3528 	unsigned int parm;
3529 	unsigned int present;
3530 	/* MUX9 (1eh) = stereo mixer */
3531 	imux_is_smixer =
3532 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
3533 	/* inputs */
3534 	/* PW 5/6/7 (29h/2ah/2bh) */
3535 	parm = AC_PWRST_D3;
3536 	set_pin_power_state(codec, 0x29, &parm);
3537 	set_pin_power_state(codec, 0x2a, &parm);
3538 	set_pin_power_state(codec, 0x2b, &parm);
3539 	parm = AC_PWRST_D0;
3540 	/* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
3541 	update_power_state(codec, 0x1e, parm);
3542 	update_power_state(codec, 0x1f, parm);
3543 	update_power_state(codec, 0x10, parm);
3544 	update_power_state(codec, 0x11, parm);
3545 
3546 	/* outputs */
3547 	/* AOW0 (8h)*/
3548 	update_power_state(codec, 0x8, parm);
3549 
3550 	if (spec->codec_type == VT1802) {
3551 		/* PW4 (28h), MW4 (18h), MUX4(38h) */
3552 		parm = AC_PWRST_D3;
3553 		set_pin_power_state(codec, 0x28, &parm);
3554 		update_power_state(codec, 0x18, parm);
3555 		update_power_state(codec, 0x38, parm);
3556 	} else {
3557 		/* PW4 (26h), MW4 (1ch), MUX4(37h) */
3558 		parm = AC_PWRST_D3;
3559 		set_pin_power_state(codec, 0x26, &parm);
3560 		update_power_state(codec, 0x1c, parm);
3561 		update_power_state(codec, 0x37, parm);
3562 	}
3563 
3564 	if (spec->codec_type == VT1802) {
3565 		/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
3566 		parm = AC_PWRST_D3;
3567 		set_pin_power_state(codec, 0x25, &parm);
3568 		update_power_state(codec, 0x15, parm);
3569 		update_power_state(codec, 0x35, parm);
3570 	} else {
3571 		/* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
3572 		parm = AC_PWRST_D3;
3573 		set_pin_power_state(codec, 0x25, &parm);
3574 		update_power_state(codec, 0x19, parm);
3575 		update_power_state(codec, 0x35, parm);
3576 	}
3577 
3578 	if (spec->hp_independent_mode)
3579 		update_power_state(codec, 0x9, AC_PWRST_D0);
3580 
3581 	/* Class-D */
3582 	/* PW0 (24h), MW0(18h/14h), MUX0(34h) */
3583 	present = snd_hda_jack_detect(codec, 0x25);
3584 
3585 	parm = AC_PWRST_D3;
3586 	set_pin_power_state(codec, 0x24, &parm);
3587 	parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
3588 	if (spec->codec_type == VT1802)
3589 		update_power_state(codec, 0x14, parm);
3590 	else
3591 		update_power_state(codec, 0x18, parm);
3592 	update_power_state(codec, 0x34, parm);
3593 
3594 	/* Mono Out */
3595 	present = snd_hda_jack_detect(codec, 0x26);
3596 
3597 	parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
3598 	if (spec->codec_type == VT1802) {
3599 		/* PW15 (33h), MW8(1ch), MUX8(3ch) */
3600 		update_power_state(codec, 0x33, parm);
3601 		update_power_state(codec, 0x1c, parm);
3602 		update_power_state(codec, 0x3c, parm);
3603 	} else {
3604 		/* PW15 (31h), MW8(17h), MUX8(3bh) */
3605 		update_power_state(codec, 0x31, parm);
3606 		update_power_state(codec, 0x17, parm);
3607 		update_power_state(codec, 0x3b, parm);
3608 	}
3609 	/* MW9 (21h) */
3610 	if (imux_is_smixer || !is_aa_path_mute(codec))
3611 		update_power_state(codec, 0x21, AC_PWRST_D0);
3612 	else
3613 		update_power_state(codec, 0x21, AC_PWRST_D3);
3614 }
3615 
3616 /*
3617  * pin fix-up
3618  */
3619 enum {
3620 	VIA_FIXUP_INTMIC_BOOST,
3621 	VIA_FIXUP_ASUS_G75,
3622 };
3623 
3624 static void via_fixup_intmic_boost(struct hda_codec *codec,
3625 				  const struct hda_fixup *fix, int action)
3626 {
3627 	if (action == HDA_FIXUP_ACT_PRE_PROBE)
3628 		override_mic_boost(codec, 0x30, 0, 2, 40);
3629 }
3630 
3631 static const struct hda_fixup via_fixups[] = {
3632 	[VIA_FIXUP_INTMIC_BOOST] = {
3633 		.type = HDA_FIXUP_FUNC,
3634 		.v.func = via_fixup_intmic_boost,
3635 	},
3636 	[VIA_FIXUP_ASUS_G75] = {
3637 		.type = HDA_FIXUP_PINS,
3638 		.v.pins = (const struct hda_pintbl[]) {
3639 			/* set 0x24 and 0x33 as speakers */
3640 			{ 0x24, 0x991301f0 },
3641 			{ 0x33, 0x991301f1 }, /* subwoofer */
3642 			{ }
3643 		}
3644 	},
3645 };
3646 
3647 static const struct snd_pci_quirk vt2002p_fixups[] = {
3648 	SND_PCI_QUIRK(0x1043, 0x1487, "Asus G75", VIA_FIXUP_ASUS_G75),
3649 	SND_PCI_QUIRK(0x1043, 0x8532, "Asus X202E", VIA_FIXUP_INTMIC_BOOST),
3650 	{}
3651 };
3652 
3653 /* NIDs 0x24 and 0x33 on VT1802 have connections to non-existing NID 0x3e
3654  * Replace this with mixer NID 0x1c
3655  */
3656 static void fix_vt1802_connections(struct hda_codec *codec)
3657 {
3658 	static hda_nid_t conn_24[] = { 0x14, 0x1c };
3659 	static hda_nid_t conn_33[] = { 0x1c };
3660 
3661 	snd_hda_override_conn_list(codec, 0x24, ARRAY_SIZE(conn_24), conn_24);
3662 	snd_hda_override_conn_list(codec, 0x33, ARRAY_SIZE(conn_33), conn_33);
3663 }
3664 
3665 /* patch for vt2002P */
3666 static int patch_vt2002P(struct hda_codec *codec)
3667 {
3668 	struct via_spec *spec;
3669 	int err;
3670 
3671 	/* create a codec specific record */
3672 	spec = via_new_spec(codec);
3673 	if (spec == NULL)
3674 		return -ENOMEM;
3675 
3676 	spec->aa_mix_nid = 0x21;
3677 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3678 	override_mic_boost(codec, 0x29, 0, 3, 40);
3679 	if (spec->codec_type == VT1802)
3680 		fix_vt1802_connections(codec);
3681 	add_secret_dac_path(codec);
3682 
3683 	snd_hda_pick_fixup(codec, NULL, vt2002p_fixups, via_fixups);
3684 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
3685 
3686 	/* automatic parse from the BIOS config */
3687 	err = via_parse_auto_config(codec);
3688 	if (err < 0) {
3689 		via_free(codec);
3690 		return err;
3691 	}
3692 
3693 	if (spec->codec_type == VT1802)
3694 		spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
3695 	else
3696 		spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
3697 
3698 	codec->patch_ops = via_patch_ops;
3699 
3700 	spec->set_widgets_power_state =  set_widgets_power_state_vt2002P;
3701 	return 0;
3702 }
3703 
3704 /* for vt1812 */
3705 
3706 static const struct hda_verb vt1812_init_verbs[] = {
3707 	/* Enable Boost Volume backdoor */
3708 	{0x1, 0xfb9, 0x24},
3709 	/* Enable AOW0 to MW9 */
3710 	{0x1, 0xfb8, 0xa8},
3711 	{ }
3712 };
3713 
3714 static void set_widgets_power_state_vt1812(struct hda_codec *codec)
3715 {
3716 	struct via_spec *spec = codec->spec;
3717 	unsigned int parm;
3718 	unsigned int present;
3719 	/* inputs */
3720 	/* PW 5/6/7 (29h/2ah/2bh) */
3721 	parm = AC_PWRST_D3;
3722 	set_pin_power_state(codec, 0x29, &parm);
3723 	set_pin_power_state(codec, 0x2a, &parm);
3724 	set_pin_power_state(codec, 0x2b, &parm);
3725 	parm = AC_PWRST_D0;
3726 	/* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
3727 	update_power_state(codec, 0x1e, parm);
3728 	update_power_state(codec, 0x1f, parm);
3729 	update_power_state(codec, 0x10, parm);
3730 	update_power_state(codec, 0x11, parm);
3731 
3732 	/* outputs */
3733 	/* AOW0 (8h)*/
3734 	update_power_state(codec, 0x8, AC_PWRST_D0);
3735 
3736 	/* PW4 (28h), MW4 (18h), MUX4(38h) */
3737 	parm = AC_PWRST_D3;
3738 	set_pin_power_state(codec, 0x28, &parm);
3739 	update_power_state(codec, 0x18, parm);
3740 	update_power_state(codec, 0x38, parm);
3741 
3742 	/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
3743 	parm = AC_PWRST_D3;
3744 	set_pin_power_state(codec, 0x25, &parm);
3745 	update_power_state(codec, 0x15, parm);
3746 	update_power_state(codec, 0x35, parm);
3747 	if (spec->hp_independent_mode)
3748 		update_power_state(codec, 0x9, AC_PWRST_D0);
3749 
3750 	/* Internal Speaker */
3751 	/* PW0 (24h), MW0(14h), MUX0(34h) */
3752 	present = snd_hda_jack_detect(codec, 0x25);
3753 
3754 	parm = AC_PWRST_D3;
3755 	set_pin_power_state(codec, 0x24, &parm);
3756 	if (present) {
3757 		update_power_state(codec, 0x14, AC_PWRST_D3);
3758 		update_power_state(codec, 0x34, AC_PWRST_D3);
3759 	} else {
3760 		update_power_state(codec, 0x14, AC_PWRST_D0);
3761 		update_power_state(codec, 0x34, AC_PWRST_D0);
3762 	}
3763 
3764 
3765 	/* Mono Out */
3766 	/* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
3767 	present = snd_hda_jack_detect(codec, 0x28);
3768 
3769 	parm = AC_PWRST_D3;
3770 	set_pin_power_state(codec, 0x31, &parm);
3771 	if (present) {
3772 		update_power_state(codec, 0x1c, AC_PWRST_D3);
3773 		update_power_state(codec, 0x3c, AC_PWRST_D3);
3774 		update_power_state(codec, 0x3e, AC_PWRST_D3);
3775 	} else {
3776 		update_power_state(codec, 0x1c, AC_PWRST_D0);
3777 		update_power_state(codec, 0x3c, AC_PWRST_D0);
3778 		update_power_state(codec, 0x3e, AC_PWRST_D0);
3779 	}
3780 
3781 	/* PW15 (33h), MW15 (1dh), MUX15(3dh) */
3782 	parm = AC_PWRST_D3;
3783 	set_pin_power_state(codec, 0x33, &parm);
3784 	update_power_state(codec, 0x1d, parm);
3785 	update_power_state(codec, 0x3d, parm);
3786 
3787 }
3788 
3789 /* patch for vt1812 */
3790 static int patch_vt1812(struct hda_codec *codec)
3791 {
3792 	struct via_spec *spec;
3793 	int err;
3794 
3795 	/* create a codec specific record */
3796 	spec = via_new_spec(codec);
3797 	if (spec == NULL)
3798 		return -ENOMEM;
3799 
3800 	spec->aa_mix_nid = 0x21;
3801 	override_mic_boost(codec, 0x2b, 0, 3, 40);
3802 	override_mic_boost(codec, 0x29, 0, 3, 40);
3803 	add_secret_dac_path(codec);
3804 
3805 	/* automatic parse from the BIOS config */
3806 	err = via_parse_auto_config(codec);
3807 	if (err < 0) {
3808 		via_free(codec);
3809 		return err;
3810 	}
3811 
3812 	spec->init_verbs[spec->num_iverbs++]  = vt1812_init_verbs;
3813 
3814 	codec->patch_ops = via_patch_ops;
3815 
3816 	spec->set_widgets_power_state =  set_widgets_power_state_vt1812;
3817 	return 0;
3818 }
3819 
3820 /*
3821  * patch entries
3822  */
3823 static const struct hda_codec_preset snd_hda_preset_via[] = {
3824 	{ .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
3825 	{ .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
3826 	{ .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
3827 	{ .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
3828 	{ .id = 0x1106e710, .name = "VT1709 10-Ch",
3829 	  .patch = patch_vt1709},
3830 	{ .id = 0x1106e711, .name = "VT1709 10-Ch",
3831 	  .patch = patch_vt1709},
3832 	{ .id = 0x1106e712, .name = "VT1709 10-Ch",
3833 	  .patch = patch_vt1709},
3834 	{ .id = 0x1106e713, .name = "VT1709 10-Ch",
3835 	  .patch = patch_vt1709},
3836 	{ .id = 0x1106e714, .name = "VT1709 6-Ch",
3837 	  .patch = patch_vt1709},
3838 	{ .id = 0x1106e715, .name = "VT1709 6-Ch",
3839 	  .patch = patch_vt1709},
3840 	{ .id = 0x1106e716, .name = "VT1709 6-Ch",
3841 	  .patch = patch_vt1709},
3842 	{ .id = 0x1106e717, .name = "VT1709 6-Ch",
3843 	  .patch = patch_vt1709},
3844 	{ .id = 0x1106e720, .name = "VT1708B 8-Ch",
3845 	  .patch = patch_vt1708B},
3846 	{ .id = 0x1106e721, .name = "VT1708B 8-Ch",
3847 	  .patch = patch_vt1708B},
3848 	{ .id = 0x1106e722, .name = "VT1708B 8-Ch",
3849 	  .patch = patch_vt1708B},
3850 	{ .id = 0x1106e723, .name = "VT1708B 8-Ch",
3851 	  .patch = patch_vt1708B},
3852 	{ .id = 0x1106e724, .name = "VT1708B 4-Ch",
3853 	  .patch = patch_vt1708B},
3854 	{ .id = 0x1106e725, .name = "VT1708B 4-Ch",
3855 	  .patch = patch_vt1708B},
3856 	{ .id = 0x1106e726, .name = "VT1708B 4-Ch",
3857 	  .patch = patch_vt1708B},
3858 	{ .id = 0x1106e727, .name = "VT1708B 4-Ch",
3859 	  .patch = patch_vt1708B},
3860 	{ .id = 0x11060397, .name = "VT1708S",
3861 	  .patch = patch_vt1708S},
3862 	{ .id = 0x11061397, .name = "VT1708S",
3863 	  .patch = patch_vt1708S},
3864 	{ .id = 0x11062397, .name = "VT1708S",
3865 	  .patch = patch_vt1708S},
3866 	{ .id = 0x11063397, .name = "VT1708S",
3867 	  .patch = patch_vt1708S},
3868 	{ .id = 0x11064397, .name = "VT1705",
3869 	  .patch = patch_vt1708S},
3870 	{ .id = 0x11065397, .name = "VT1708S",
3871 	  .patch = patch_vt1708S},
3872 	{ .id = 0x11066397, .name = "VT1708S",
3873 	  .patch = patch_vt1708S},
3874 	{ .id = 0x11067397, .name = "VT1708S",
3875 	  .patch = patch_vt1708S},
3876 	{ .id = 0x11060398, .name = "VT1702",
3877 	  .patch = patch_vt1702},
3878 	{ .id = 0x11061398, .name = "VT1702",
3879 	  .patch = patch_vt1702},
3880 	{ .id = 0x11062398, .name = "VT1702",
3881 	  .patch = patch_vt1702},
3882 	{ .id = 0x11063398, .name = "VT1702",
3883 	  .patch = patch_vt1702},
3884 	{ .id = 0x11064398, .name = "VT1702",
3885 	  .patch = patch_vt1702},
3886 	{ .id = 0x11065398, .name = "VT1702",
3887 	  .patch = patch_vt1702},
3888 	{ .id = 0x11066398, .name = "VT1702",
3889 	  .patch = patch_vt1702},
3890 	{ .id = 0x11067398, .name = "VT1702",
3891 	  .patch = patch_vt1702},
3892 	{ .id = 0x11060428, .name = "VT1718S",
3893 	  .patch = patch_vt1718S},
3894 	{ .id = 0x11064428, .name = "VT1718S",
3895 	  .patch = patch_vt1718S},
3896 	{ .id = 0x11060441, .name = "VT2020",
3897 	  .patch = patch_vt1718S},
3898 	{ .id = 0x11064441, .name = "VT1828S",
3899 	  .patch = patch_vt1718S},
3900 	{ .id = 0x11060433, .name = "VT1716S",
3901 	  .patch = patch_vt1716S},
3902 	{ .id = 0x1106a721, .name = "VT1716S",
3903 	  .patch = patch_vt1716S},
3904 	{ .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
3905 	{ .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
3906 	{ .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
3907 	{ .id = 0x11060440, .name = "VT1818S",
3908 	  .patch = patch_vt1708S},
3909 	{ .id = 0x11060446, .name = "VT1802",
3910 		.patch = patch_vt2002P},
3911 	{ .id = 0x11068446, .name = "VT1802",
3912 		.patch = patch_vt2002P},
3913 	{} /* terminator */
3914 };
3915 
3916 MODULE_ALIAS("snd-hda-codec-id:1106*");
3917 
3918 static struct hda_codec_preset_list via_list = {
3919 	.preset = snd_hda_preset_via,
3920 	.owner = THIS_MODULE,
3921 };
3922 
3923 MODULE_LICENSE("GPL");
3924 MODULE_DESCRIPTION("VIA HD-audio codec");
3925 
3926 static int __init patch_via_init(void)
3927 {
3928 	return snd_hda_add_codec_preset(&via_list);
3929 }
3930 
3931 static void __exit patch_via_exit(void)
3932 {
3933 	snd_hda_delete_codec_preset(&via_list);
3934 }
3935 
3936 module_init(patch_via_init)
3937 module_exit(patch_via_exit)
3938