xref: /linux/sound/pci/hda/patch_via.c (revision d39d0ed196aa1685bb24771e92f78633c66ac9cb)
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 <sound/core.h>
53 #include <sound/asoundef.h>
54 #include "hda_codec.h"
55 #include "hda_local.h"
56 
57 #define NID_MAPPING		(-1)
58 
59 /* amp values */
60 #define AMP_VAL_IDX_SHIFT	19
61 #define AMP_VAL_IDX_MASK	(0x0f<<19)
62 
63 /* Pin Widget NID */
64 #define VT1708_HP_NID		0x13
65 #define VT1708_DIGOUT_NID	0x14
66 #define VT1708_DIGIN_NID	0x16
67 #define VT1708_DIGIN_PIN	0x26
68 #define VT1708_HP_PIN_NID	0x20
69 #define VT1708_CD_PIN_NID	0x24
70 
71 #define VT1709_HP_DAC_NID	0x28
72 #define VT1709_DIGOUT_NID	0x13
73 #define VT1709_DIGIN_NID	0x17
74 #define VT1709_DIGIN_PIN	0x25
75 
76 #define VT1708B_HP_NID		0x25
77 #define VT1708B_DIGOUT_NID	0x12
78 #define VT1708B_DIGIN_NID	0x15
79 #define VT1708B_DIGIN_PIN	0x21
80 
81 #define VT1708S_HP_NID		0x25
82 #define VT1708S_DIGOUT_NID	0x12
83 
84 #define VT1702_HP_NID		0x17
85 #define VT1702_DIGOUT_NID	0x11
86 
87 enum VIA_HDA_CODEC {
88 	UNKNOWN = -1,
89 	VT1708,
90 	VT1709_10CH,
91 	VT1709_6CH,
92 	VT1708B_8CH,
93 	VT1708B_4CH,
94 	VT1708S,
95 	VT1708BCE,
96 	VT1702,
97 	VT1718S,
98 	VT1716S,
99 	VT2002P,
100 	VT1812,
101 	CODEC_TYPES,
102 };
103 
104 struct via_spec {
105 	/* codec parameterization */
106 	struct snd_kcontrol_new *mixers[6];
107 	unsigned int num_mixers;
108 
109 	struct hda_verb *init_verbs[5];
110 	unsigned int num_iverbs;
111 
112 	char *stream_name_analog;
113 	struct hda_pcm_stream *stream_analog_playback;
114 	struct hda_pcm_stream *stream_analog_capture;
115 
116 	char *stream_name_digital;
117 	struct hda_pcm_stream *stream_digital_playback;
118 	struct hda_pcm_stream *stream_digital_capture;
119 
120 	/* playback */
121 	struct hda_multi_out multiout;
122 	hda_nid_t slave_dig_outs[2];
123 
124 	/* capture */
125 	unsigned int num_adc_nids;
126 	hda_nid_t *adc_nids;
127 	hda_nid_t mux_nids[3];
128 	hda_nid_t dig_in_nid;
129 	hda_nid_t dig_in_pin;
130 
131 	/* capture source */
132 	const struct hda_input_mux *input_mux;
133 	unsigned int cur_mux[3];
134 
135 	/* PCM information */
136 	struct hda_pcm pcm_rec[3];
137 
138 	/* dynamic controls, init_verbs and input_mux */
139 	struct auto_pin_cfg autocfg;
140 	struct snd_array kctls;
141 	struct hda_input_mux private_imux[2];
142 	hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
143 
144 	/* HP mode source */
145 	const struct hda_input_mux *hp_mux;
146 	unsigned int hp_independent_mode;
147 	unsigned int hp_independent_mode_index;
148 	unsigned int smart51_enabled;
149 	unsigned int dmic_enabled;
150 	enum VIA_HDA_CODEC codec_type;
151 
152 	/* work to check hp jack state */
153 	struct hda_codec *codec;
154 	struct delayed_work vt1708_hp_work;
155 	int vt1708_jack_detectect;
156 	int vt1708_hp_present;
157 #ifdef CONFIG_SND_HDA_POWER_SAVE
158 	struct hda_loopback_check loopback;
159 #endif
160 };
161 
162 static struct via_spec * via_new_spec(struct hda_codec *codec)
163 {
164 	struct via_spec *spec;
165 
166 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
167 	if (spec == NULL)
168 		return NULL;
169 
170 	codec->spec = spec;
171 	spec->codec = codec;
172 	return spec;
173 }
174 
175 static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
176 {
177 	u32 vendor_id = codec->vendor_id;
178 	u16 ven_id = vendor_id >> 16;
179 	u16 dev_id = vendor_id & 0xffff;
180 	enum VIA_HDA_CODEC codec_type;
181 
182 	/* get codec type */
183 	if (ven_id != 0x1106)
184 		codec_type = UNKNOWN;
185 	else if (dev_id >= 0x1708 && dev_id <= 0x170b)
186 		codec_type = VT1708;
187 	else if (dev_id >= 0xe710 && dev_id <= 0xe713)
188 		codec_type = VT1709_10CH;
189 	else if (dev_id >= 0xe714 && dev_id <= 0xe717)
190 		codec_type = VT1709_6CH;
191 	else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
192 		codec_type = VT1708B_8CH;
193 		if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
194 			codec_type = VT1708BCE;
195 	} else if (dev_id >= 0xe724 && dev_id <= 0xe727)
196 		codec_type = VT1708B_4CH;
197 	else if ((dev_id & 0xfff) == 0x397
198 		 && (dev_id >> 12) < 8)
199 		codec_type = VT1708S;
200 	else if ((dev_id & 0xfff) == 0x398
201 		 && (dev_id >> 12) < 8)
202 		codec_type = VT1702;
203 	else if ((dev_id & 0xfff) == 0x428
204 		 && (dev_id >> 12) < 8)
205 		codec_type = VT1718S;
206 	else if (dev_id == 0x0433 || dev_id == 0xa721)
207 		codec_type = VT1716S;
208 	else if (dev_id == 0x0441 || dev_id == 0x4441)
209 		codec_type = VT1718S;
210 	else if (dev_id == 0x0438 || dev_id == 0x4438)
211 		codec_type = VT2002P;
212 	else if (dev_id == 0x0448)
213 		codec_type = VT1812;
214 	else if (dev_id == 0x0440)
215 		codec_type = VT1708S;
216 	else
217 		codec_type = UNKNOWN;
218 	return codec_type;
219 };
220 
221 #define VIA_HP_EVENT		0x01
222 #define VIA_GPIO_EVENT		0x02
223 #define VIA_JACK_EVENT		0x04
224 #define VIA_MONO_EVENT		0x08
225 #define VIA_SPEAKER_EVENT	0x10
226 #define VIA_BIND_HP_EVENT	0x20
227 
228 enum {
229 	VIA_CTL_WIDGET_VOL,
230 	VIA_CTL_WIDGET_MUTE,
231 	VIA_CTL_WIDGET_ANALOG_MUTE,
232 	VIA_CTL_WIDGET_BIND_PIN_MUTE,
233 };
234 
235 enum {
236 	AUTO_SEQ_FRONT = 0,
237 	AUTO_SEQ_SURROUND,
238 	AUTO_SEQ_CENLFE,
239 	AUTO_SEQ_SIDE
240 };
241 
242 static void analog_low_current_mode(struct hda_codec *codec, int stream_idle);
243 static void set_jack_power_state(struct hda_codec *codec);
244 static int is_aa_path_mute(struct hda_codec *codec);
245 
246 static void vt1708_start_hp_work(struct via_spec *spec)
247 {
248 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
249 		return;
250 	snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
251 			    !spec->vt1708_jack_detectect);
252 	if (!delayed_work_pending(&spec->vt1708_hp_work))
253 		schedule_delayed_work(&spec->vt1708_hp_work,
254 				      msecs_to_jiffies(100));
255 }
256 
257 static void vt1708_stop_hp_work(struct via_spec *spec)
258 {
259 	if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
260 		return;
261 	if (snd_hda_get_bool_hint(spec->codec, "analog_loopback_hp_detect") == 1
262 	    && !is_aa_path_mute(spec->codec))
263 		return;
264 	snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
265 			    !spec->vt1708_jack_detectect);
266 	cancel_delayed_work(&spec->vt1708_hp_work);
267 	flush_scheduled_work();
268 }
269 
270 
271 static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
272 				   struct snd_ctl_elem_value *ucontrol)
273 {
274 	int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
275 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
276 
277 	set_jack_power_state(codec);
278 	analog_low_current_mode(snd_kcontrol_chip(kcontrol), -1);
279 	if (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1) {
280 		if (is_aa_path_mute(codec))
281 			vt1708_start_hp_work(codec->spec);
282 		else
283 			vt1708_stop_hp_work(codec->spec);
284 	}
285 	return change;
286 }
287 
288 /* modify .put = snd_hda_mixer_amp_switch_put */
289 #define ANALOG_INPUT_MUTE						\
290 	{		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,		\
291 			.name = NULL,					\
292 			.index = 0,					\
293 			.info = snd_hda_mixer_amp_switch_info,		\
294 			.get = snd_hda_mixer_amp_switch_get,		\
295 			.put = analog_input_switch_put,			\
296 			.private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
297 
298 static void via_hp_bind_automute(struct hda_codec *codec);
299 
300 static int bind_pin_switch_put(struct snd_kcontrol *kcontrol,
301 			       struct snd_ctl_elem_value *ucontrol)
302 {
303 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
304 	struct via_spec *spec = codec->spec;
305 	int i;
306 	int change = 0;
307 
308 	long *valp = ucontrol->value.integer.value;
309 	int lmute, rmute;
310 	if (strstr(kcontrol->id.name, "Switch") == NULL) {
311 		snd_printd("Invalid control!\n");
312 		return change;
313 	}
314 	change = snd_hda_mixer_amp_switch_put(kcontrol,
315 					      ucontrol);
316 	/* Get mute value */
317 	lmute = *valp ? 0 : HDA_AMP_MUTE;
318 	valp++;
319 	rmute = *valp ? 0 : HDA_AMP_MUTE;
320 
321 	/* Set hp pins */
322 	if (!spec->hp_independent_mode) {
323 		for (i = 0; i < spec->autocfg.hp_outs; i++) {
324 			snd_hda_codec_amp_update(
325 				codec, spec->autocfg.hp_pins[i],
326 				0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
327 				lmute);
328 			snd_hda_codec_amp_update(
329 				codec, spec->autocfg.hp_pins[i],
330 				1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
331 				rmute);
332 		}
333 	}
334 
335 	if (!lmute && !rmute) {
336 		/* Line Outs */
337 		for (i = 0; i < spec->autocfg.line_outs; i++)
338 			snd_hda_codec_amp_stereo(
339 				codec, spec->autocfg.line_out_pins[i],
340 				HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
341 		/* Speakers */
342 		for (i = 0; i < spec->autocfg.speaker_outs; i++)
343 			snd_hda_codec_amp_stereo(
344 				codec, spec->autocfg.speaker_pins[i],
345 				HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
346 		/* unmute */
347 		via_hp_bind_automute(codec);
348 
349 	} else {
350 		if (lmute) {
351 			/* Mute all left channels */
352 			for (i = 1; i < spec->autocfg.line_outs; i++)
353 				snd_hda_codec_amp_update(
354 					codec,
355 					spec->autocfg.line_out_pins[i],
356 					0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
357 					lmute);
358 			for (i = 0; i < spec->autocfg.speaker_outs; i++)
359 				snd_hda_codec_amp_update(
360 					codec,
361 					spec->autocfg.speaker_pins[i],
362 					0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
363 					lmute);
364 		}
365 		if (rmute) {
366 			/* mute all right channels */
367 			for (i = 1; i < spec->autocfg.line_outs; i++)
368 				snd_hda_codec_amp_update(
369 					codec,
370 					spec->autocfg.line_out_pins[i],
371 					1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
372 					rmute);
373 			for (i = 0; i < spec->autocfg.speaker_outs; i++)
374 				snd_hda_codec_amp_update(
375 					codec,
376 					spec->autocfg.speaker_pins[i],
377 					1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
378 					rmute);
379 		}
380 	}
381 	return change;
382 }
383 
384 #define BIND_PIN_MUTE							\
385 	{		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,		\
386 			.name = NULL,					\
387 			.index = 0,					\
388 			.info = snd_hda_mixer_amp_switch_info,		\
389 			.get = snd_hda_mixer_amp_switch_get,		\
390 			.put = bind_pin_switch_put,			\
391 			.private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
392 
393 static struct snd_kcontrol_new via_control_templates[] = {
394 	HDA_CODEC_VOLUME(NULL, 0, 0, 0),
395 	HDA_CODEC_MUTE(NULL, 0, 0, 0),
396 	ANALOG_INPUT_MUTE,
397 	BIND_PIN_MUTE,
398 };
399 
400 static hda_nid_t vt1708_adc_nids[2] = {
401 	/* ADC1-2 */
402 	0x15, 0x27
403 };
404 
405 static hda_nid_t vt1709_adc_nids[3] = {
406 	/* ADC1-2 */
407 	0x14, 0x15, 0x16
408 };
409 
410 static hda_nid_t vt1708B_adc_nids[2] = {
411 	/* ADC1-2 */
412 	0x13, 0x14
413 };
414 
415 static hda_nid_t vt1708S_adc_nids[2] = {
416 	/* ADC1-2 */
417 	0x13, 0x14
418 };
419 
420 static hda_nid_t vt1702_adc_nids[3] = {
421 	/* ADC1-2 */
422 	0x12, 0x20, 0x1F
423 };
424 
425 static hda_nid_t vt1718S_adc_nids[2] = {
426 	/* ADC1-2 */
427 	0x10, 0x11
428 };
429 
430 static hda_nid_t vt1716S_adc_nids[2] = {
431 	/* ADC1-2 */
432 	0x13, 0x14
433 };
434 
435 static hda_nid_t vt2002P_adc_nids[2] = {
436 	/* ADC1-2 */
437 	0x10, 0x11
438 };
439 
440 static hda_nid_t vt1812_adc_nids[2] = {
441 	/* ADC1-2 */
442 	0x10, 0x11
443 };
444 
445 
446 /* add dynamic controls */
447 static int via_add_control(struct via_spec *spec, int type, const char *name,
448 			   unsigned long val)
449 {
450 	struct snd_kcontrol_new *knew;
451 
452 	snd_array_init(&spec->kctls, sizeof(*knew), 32);
453 	knew = snd_array_new(&spec->kctls);
454 	if (!knew)
455 		return -ENOMEM;
456 	*knew = via_control_templates[type];
457 	knew->name = kstrdup(name, GFP_KERNEL);
458 	if (!knew->name)
459 		return -ENOMEM;
460 	if (get_amp_nid_(val))
461 		knew->subdevice = HDA_SUBDEV_AMP_FLAG;
462 	knew->private_value = val;
463 	return 0;
464 }
465 
466 static struct snd_kcontrol_new *via_clone_control(struct via_spec *spec,
467 						struct snd_kcontrol_new *tmpl)
468 {
469 	struct snd_kcontrol_new *knew;
470 
471 	snd_array_init(&spec->kctls, sizeof(*knew), 32);
472 	knew = snd_array_new(&spec->kctls);
473 	if (!knew)
474 		return NULL;
475 	*knew = *tmpl;
476 	knew->name = kstrdup(tmpl->name, GFP_KERNEL);
477 	if (!knew->name)
478 		return NULL;
479 	return knew;
480 }
481 
482 static void via_free_kctls(struct hda_codec *codec)
483 {
484 	struct via_spec *spec = codec->spec;
485 
486 	if (spec->kctls.list) {
487 		struct snd_kcontrol_new *kctl = spec->kctls.list;
488 		int i;
489 		for (i = 0; i < spec->kctls.used; i++)
490 			kfree(kctl[i].name);
491 	}
492 	snd_array_free(&spec->kctls);
493 }
494 
495 /* create input playback/capture controls for the given pin */
496 static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
497 				int idx, int mix_nid)
498 {
499 	char name[32];
500 	int err;
501 
502 	sprintf(name, "%s Playback Volume", ctlname);
503 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
504 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
505 	if (err < 0)
506 		return err;
507 	sprintf(name, "%s Playback Switch", ctlname);
508 	err = via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name,
509 			      HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
510 	if (err < 0)
511 		return err;
512 	return 0;
513 }
514 
515 static void via_auto_set_output_and_unmute(struct hda_codec *codec,
516 					   hda_nid_t nid, int pin_type,
517 					   int dac_idx)
518 {
519 	/* set as output */
520 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
521 			    pin_type);
522 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
523 			    AMP_OUT_UNMUTE);
524 	if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
525 		snd_hda_codec_write(codec, nid, 0,
526 				    AC_VERB_SET_EAPD_BTLENABLE, 0x02);
527 }
528 
529 
530 static void via_auto_init_multi_out(struct hda_codec *codec)
531 {
532 	struct via_spec *spec = codec->spec;
533 	int i;
534 
535 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
536 		hda_nid_t nid = spec->autocfg.line_out_pins[i];
537 		if (nid)
538 			via_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
539 	}
540 }
541 
542 static void via_auto_init_hp_out(struct hda_codec *codec)
543 {
544 	struct via_spec *spec = codec->spec;
545 	hda_nid_t pin;
546 	int i;
547 
548 	for (i = 0; i < spec->autocfg.hp_outs; i++) {
549 		pin = spec->autocfg.hp_pins[i];
550 		if (pin) /* connect to front */
551 			via_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
552 	}
553 }
554 
555 static int is_smart51_pins(struct via_spec *spec, hda_nid_t pin);
556 
557 static void via_auto_init_analog_input(struct hda_codec *codec)
558 {
559 	struct via_spec *spec = codec->spec;
560 	unsigned int ctl;
561 	int i;
562 
563 	for (i = 0; i < AUTO_PIN_LAST; i++) {
564 		hda_nid_t nid = spec->autocfg.input_pins[i];
565 		if (!nid)
566 			continue;
567 
568 		if (spec->smart51_enabled && is_smart51_pins(spec, nid))
569 			ctl = PIN_OUT;
570 		else if (i <= AUTO_PIN_FRONT_MIC)
571 			ctl = PIN_VREF50;
572 		else
573 			ctl = PIN_IN;
574 		snd_hda_codec_write(codec, nid, 0,
575 				    AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
576 	}
577 }
578 
579 static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
580 				unsigned int *affected_parm)
581 {
582 	unsigned parm;
583 	unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
584 	unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
585 		>> AC_DEFCFG_MISC_SHIFT
586 		& AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
587 	unsigned present = snd_hda_jack_detect(codec, nid);
588 	struct via_spec *spec = codec->spec;
589 	if ((spec->smart51_enabled && is_smart51_pins(spec, nid))
590 	    || ((no_presence || present)
591 		&& get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
592 		*affected_parm = AC_PWRST_D0; /* if it's connected */
593 		parm = AC_PWRST_D0;
594 	} else
595 		parm = AC_PWRST_D3;
596 
597 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
598 }
599 
600 static void set_jack_power_state(struct hda_codec *codec)
601 {
602 	struct via_spec *spec = codec->spec;
603 	int imux_is_smixer;
604 	unsigned int parm;
605 
606 	if (spec->codec_type == VT1702) {
607 		imux_is_smixer = snd_hda_codec_read(
608 			codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
609 		/* inputs */
610 		/* PW 1/2/5 (14h/15h/18h) */
611 		parm = AC_PWRST_D3;
612 		set_pin_power_state(codec, 0x14, &parm);
613 		set_pin_power_state(codec, 0x15, &parm);
614 		set_pin_power_state(codec, 0x18, &parm);
615 		if (imux_is_smixer)
616 			parm = AC_PWRST_D0; /* SW0 = stereo mixer (idx 3) */
617 		/* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
618 		snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE,
619 				    parm);
620 		snd_hda_codec_write(codec, 0x12, 0, AC_VERB_SET_POWER_STATE,
621 				    parm);
622 		snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE,
623 				    parm);
624 		snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_POWER_STATE,
625 				    parm);
626 
627 		/* outputs */
628 		/* PW 3/4 (16h/17h) */
629 		parm = AC_PWRST_D3;
630 		set_pin_power_state(codec, 0x16, &parm);
631 		set_pin_power_state(codec, 0x17, &parm);
632 		/* MW0 (1ah), AOW 0/1 (10h/1dh) */
633 		snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
634 				    imux_is_smixer ? AC_PWRST_D0 : parm);
635 		snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
636 				    parm);
637 		snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE,
638 				    parm);
639 	} else if (spec->codec_type == VT1708B_8CH
640 		   || spec->codec_type == VT1708B_4CH
641 		   || spec->codec_type == VT1708S) {
642 		/* SW0 (17h) = stereo mixer */
643 		int is_8ch = spec->codec_type != VT1708B_4CH;
644 		imux_is_smixer = snd_hda_codec_read(
645 			codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
646 			== ((spec->codec_type == VT1708S)  ? 5 : 0);
647 		/* inputs */
648 		/* PW 1/2/5 (1ah/1bh/1eh) */
649 		parm = AC_PWRST_D3;
650 		set_pin_power_state(codec, 0x1a, &parm);
651 		set_pin_power_state(codec, 0x1b, &parm);
652 		set_pin_power_state(codec, 0x1e, &parm);
653 		if (imux_is_smixer)
654 			parm = AC_PWRST_D0;
655 		/* SW0 (17h), AIW 0/1 (13h/14h) */
656 		snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE,
657 				    parm);
658 		snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE,
659 				    parm);
660 		snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE,
661 				    parm);
662 
663 		/* outputs */
664 		/* PW0 (19h), SW1 (18h), AOW1 (11h) */
665 		parm = AC_PWRST_D3;
666 		set_pin_power_state(codec, 0x19, &parm);
667 		if (spec->smart51_enabled)
668 			parm = AC_PWRST_D0;
669 		snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE,
670 				    parm);
671 		snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE,
672 				    parm);
673 
674 		/* PW6 (22h), SW2 (26h), AOW2 (24h) */
675 		if (is_8ch) {
676 			parm = AC_PWRST_D3;
677 			set_pin_power_state(codec, 0x22, &parm);
678 			if (spec->smart51_enabled)
679 				parm = AC_PWRST_D0;
680 			snd_hda_codec_write(codec, 0x26, 0,
681 					    AC_VERB_SET_POWER_STATE, parm);
682 			snd_hda_codec_write(codec, 0x24, 0,
683 					    AC_VERB_SET_POWER_STATE, parm);
684 		}
685 
686 		/* PW 3/4/7 (1ch/1dh/23h) */
687 		parm = AC_PWRST_D3;
688 		/* force to D0 for internal Speaker */
689 		set_pin_power_state(codec, 0x1c, &parm);
690 		set_pin_power_state(codec, 0x1d, &parm);
691 		if (is_8ch)
692 			set_pin_power_state(codec, 0x23, &parm);
693 		/* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
694 		snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
695 				    imux_is_smixer ? AC_PWRST_D0 : parm);
696 		snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
697 				    parm);
698 		if (is_8ch) {
699 			snd_hda_codec_write(codec, 0x25, 0,
700 					    AC_VERB_SET_POWER_STATE, parm);
701 			snd_hda_codec_write(codec, 0x27, 0,
702 					    AC_VERB_SET_POWER_STATE, parm);
703 		}
704 	}  else if (spec->codec_type == VT1718S) {
705 		/* MUX6 (1eh) = stereo mixer */
706 		imux_is_smixer = snd_hda_codec_read(
707 			codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
708 		/* inputs */
709 		/* PW 5/6/7 (29h/2ah/2bh) */
710 		parm = AC_PWRST_D3;
711 		set_pin_power_state(codec, 0x29, &parm);
712 		set_pin_power_state(codec, 0x2a, &parm);
713 		set_pin_power_state(codec, 0x2b, &parm);
714 		if (imux_is_smixer)
715 			parm = AC_PWRST_D0;
716 		/* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
717 		snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE,
718 				    parm);
719 		snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE,
720 				    parm);
721 		snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
722 				    parm);
723 		snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE,
724 				    parm);
725 
726 		/* outputs */
727 		/* PW3 (27h), MW2 (1ah), AOW3 (bh) */
728 		parm = AC_PWRST_D3;
729 		set_pin_power_state(codec, 0x27, &parm);
730 		snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
731 				    parm);
732 		snd_hda_codec_write(codec, 0xb, 0, AC_VERB_SET_POWER_STATE,
733 				    parm);
734 
735 		/* PW2 (26h), AOW2 (ah) */
736 		parm = AC_PWRST_D3;
737 		set_pin_power_state(codec, 0x26, &parm);
738 		snd_hda_codec_write(codec, 0xa, 0, AC_VERB_SET_POWER_STATE,
739 				    parm);
740 
741 		/* PW0/1 (24h/25h) */
742 		parm = AC_PWRST_D3;
743 		set_pin_power_state(codec, 0x24, &parm);
744 		set_pin_power_state(codec, 0x25, &parm);
745 		if (!spec->hp_independent_mode) /* check for redirected HP */
746 			set_pin_power_state(codec, 0x28, &parm);
747 		snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE,
748 				    parm);
749 		snd_hda_codec_write(codec, 0x9, 0, AC_VERB_SET_POWER_STATE,
750 				    parm);
751 		/* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
752 		snd_hda_codec_write(codec, 0x21, 0, AC_VERB_SET_POWER_STATE,
753 				    imux_is_smixer ? AC_PWRST_D0 : parm);
754 		if (spec->hp_independent_mode) {
755 			/* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
756 			parm = AC_PWRST_D3;
757 			set_pin_power_state(codec, 0x28, &parm);
758 			snd_hda_codec_write(codec, 0x1b, 0,
759 					    AC_VERB_SET_POWER_STATE, parm);
760 			snd_hda_codec_write(codec, 0x34, 0,
761 					    AC_VERB_SET_POWER_STATE, parm);
762 			snd_hda_codec_write(codec, 0xc, 0,
763 					    AC_VERB_SET_POWER_STATE, parm);
764 		}
765 	} else if (spec->codec_type == VT1716S) {
766 		unsigned int mono_out, present;
767 		/* SW0 (17h) = stereo mixer */
768 		imux_is_smixer = snd_hda_codec_read(
769 			codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00) ==  5;
770 		/* inputs */
771 		/* PW 1/2/5 (1ah/1bh/1eh) */
772 		parm = AC_PWRST_D3;
773 		set_pin_power_state(codec, 0x1a, &parm);
774 		set_pin_power_state(codec, 0x1b, &parm);
775 		set_pin_power_state(codec, 0x1e, &parm);
776 		if (imux_is_smixer)
777 			parm = AC_PWRST_D0;
778 		/* SW0 (17h), AIW0(13h) */
779 		snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE,
780 				    parm);
781 		snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE,
782 				    parm);
783 
784 		parm = AC_PWRST_D3;
785 		set_pin_power_state(codec, 0x1e, &parm);
786 		/* PW11 (22h) */
787 		if (spec->dmic_enabled)
788 			set_pin_power_state(codec, 0x22, &parm);
789 		else
790 			snd_hda_codec_write(
791 				codec, 0x22, 0,
792 				AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
793 
794 		/* SW2(26h), AIW1(14h) */
795 		snd_hda_codec_write(codec, 0x26, 0, AC_VERB_SET_POWER_STATE,
796 				    parm);
797 		snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE,
798 				    parm);
799 
800 		/* outputs */
801 		/* PW0 (19h), SW1 (18h), AOW1 (11h) */
802 		parm = AC_PWRST_D3;
803 		set_pin_power_state(codec, 0x19, &parm);
804 		/* Smart 5.1 PW2(1bh) */
805 		if (spec->smart51_enabled)
806 			set_pin_power_state(codec, 0x1b, &parm);
807 		snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE,
808 				    parm);
809 		snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE,
810 				    parm);
811 
812 		/* PW7 (23h), SW3 (27h), AOW3 (25h) */
813 		parm = AC_PWRST_D3;
814 		set_pin_power_state(codec, 0x23, &parm);
815 		/* Smart 5.1 PW1(1ah) */
816 		if (spec->smart51_enabled)
817 			set_pin_power_state(codec, 0x1a, &parm);
818 		snd_hda_codec_write(codec, 0x27, 0, AC_VERB_SET_POWER_STATE,
819 				    parm);
820 
821 		/* Smart 5.1 PW5(1eh) */
822 		if (spec->smart51_enabled)
823 			set_pin_power_state(codec, 0x1e, &parm);
824 		snd_hda_codec_write(codec, 0x25, 0, AC_VERB_SET_POWER_STATE,
825 				    parm);
826 
827 		/* Mono out */
828 		/* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
829 		present = snd_hda_jack_detect(codec, 0x1c);
830 		if (present)
831 			mono_out = 0;
832 		else {
833 			present = snd_hda_jack_detect(codec, 0x1d);
834 			if (!spec->hp_independent_mode && present)
835 				mono_out = 0;
836 			else
837 				mono_out = 1;
838 		}
839 		parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
840 		snd_hda_codec_write(codec, 0x28, 0, AC_VERB_SET_POWER_STATE,
841 				    parm);
842 		snd_hda_codec_write(codec, 0x29, 0, AC_VERB_SET_POWER_STATE,
843 				    parm);
844 		snd_hda_codec_write(codec, 0x2a, 0, AC_VERB_SET_POWER_STATE,
845 				    parm);
846 
847 		/* PW 3/4 (1ch/1dh) */
848 		parm = AC_PWRST_D3;
849 		set_pin_power_state(codec, 0x1c, &parm);
850 		set_pin_power_state(codec, 0x1d, &parm);
851 		/* HP Independent Mode, power on AOW3 */
852 		if (spec->hp_independent_mode)
853 			snd_hda_codec_write(codec, 0x25, 0,
854 					    AC_VERB_SET_POWER_STATE, parm);
855 
856 		/* force to D0 for internal Speaker */
857 		/* MW0 (16h), AOW0 (10h) */
858 		snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
859 				    imux_is_smixer ? AC_PWRST_D0 : parm);
860 		snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
861 				    mono_out ? AC_PWRST_D0 : parm);
862 	} else if (spec->codec_type == VT2002P) {
863 		unsigned int present;
864 		/* MUX9 (1eh) = stereo mixer */
865 		imux_is_smixer = snd_hda_codec_read(
866 			codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
867 		/* inputs */
868 		/* PW 5/6/7 (29h/2ah/2bh) */
869 		parm = AC_PWRST_D3;
870 		set_pin_power_state(codec, 0x29, &parm);
871 		set_pin_power_state(codec, 0x2a, &parm);
872 		set_pin_power_state(codec, 0x2b, &parm);
873 		if (imux_is_smixer)
874 			parm = AC_PWRST_D0;
875 		/* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
876 		snd_hda_codec_write(codec, 0x1e, 0,
877 				    AC_VERB_SET_POWER_STATE, parm);
878 		snd_hda_codec_write(codec, 0x1f, 0,
879 				    AC_VERB_SET_POWER_STATE, parm);
880 		snd_hda_codec_write(codec, 0x10, 0,
881 				    AC_VERB_SET_POWER_STATE, parm);
882 		snd_hda_codec_write(codec, 0x11, 0,
883 				    AC_VERB_SET_POWER_STATE, parm);
884 
885 		/* outputs */
886 		/* AOW0 (8h)*/
887 		snd_hda_codec_write(codec, 0x8, 0,
888 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
889 
890 		/* PW4 (26h), MW4 (1ch), MUX4(37h) */
891 		parm = AC_PWRST_D3;
892 		set_pin_power_state(codec, 0x26, &parm);
893 		snd_hda_codec_write(codec, 0x1c, 0,
894 				    AC_VERB_SET_POWER_STATE, parm);
895 		snd_hda_codec_write(codec, 0x37,
896 				    0, AC_VERB_SET_POWER_STATE, parm);
897 
898 		/* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
899 		parm = AC_PWRST_D3;
900 		set_pin_power_state(codec, 0x25, &parm);
901 		snd_hda_codec_write(codec, 0x19, 0,
902 				    AC_VERB_SET_POWER_STATE, parm);
903 		snd_hda_codec_write(codec, 0x35, 0,
904 				    AC_VERB_SET_POWER_STATE, parm);
905 		if (spec->hp_independent_mode)	{
906 			snd_hda_codec_write(codec, 0x9, 0,
907 					    AC_VERB_SET_POWER_STATE, parm);
908 		}
909 
910 		/* Class-D */
911 		/* PW0 (24h), MW0(18h), MUX0(34h) */
912 		present = snd_hda_jack_detect(codec, 0x25);
913 		parm = AC_PWRST_D3;
914 		set_pin_power_state(codec, 0x24, &parm);
915 		if (present) {
916 			snd_hda_codec_write(
917 				codec, 0x18, 0,
918 				AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
919 			snd_hda_codec_write(
920 				codec, 0x34, 0,
921 				AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
922 		} else {
923 			snd_hda_codec_write(
924 				codec, 0x18, 0,
925 				AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
926 			snd_hda_codec_write(
927 				codec, 0x34, 0,
928 				AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
929 		}
930 
931 		/* Mono Out */
932 		/* PW15 (31h), MW8(17h), MUX8(3bh) */
933 		present = snd_hda_jack_detect(codec, 0x26);
934 		parm = AC_PWRST_D3;
935 		set_pin_power_state(codec, 0x31, &parm);
936 		if (present) {
937 			snd_hda_codec_write(
938 				codec, 0x17, 0,
939 				AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
940 			snd_hda_codec_write(
941 				codec, 0x3b, 0,
942 				AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
943 		} else {
944 			snd_hda_codec_write(
945 				codec, 0x17, 0,
946 				AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
947 			snd_hda_codec_write(
948 				codec, 0x3b, 0,
949 				AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
950 		}
951 
952 		/* MW9 (21h) */
953 		if (imux_is_smixer || !is_aa_path_mute(codec))
954 			snd_hda_codec_write(
955 				codec, 0x21, 0,
956 				AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
957 		else
958 			snd_hda_codec_write(
959 				codec, 0x21, 0,
960 				AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
961 	} else if (spec->codec_type == VT1812) {
962 		unsigned int present;
963 		/* MUX10 (1eh) = stereo mixer */
964 		imux_is_smixer = snd_hda_codec_read(
965 			codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
966 		/* inputs */
967 		/* PW 5/6/7 (29h/2ah/2bh) */
968 		parm = AC_PWRST_D3;
969 		set_pin_power_state(codec, 0x29, &parm);
970 		set_pin_power_state(codec, 0x2a, &parm);
971 		set_pin_power_state(codec, 0x2b, &parm);
972 		if (imux_is_smixer)
973 			parm = AC_PWRST_D0;
974 		/* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
975 		snd_hda_codec_write(codec, 0x1e, 0,
976 				    AC_VERB_SET_POWER_STATE, parm);
977 		snd_hda_codec_write(codec, 0x1f, 0,
978 				    AC_VERB_SET_POWER_STATE, parm);
979 		snd_hda_codec_write(codec, 0x10, 0,
980 				    AC_VERB_SET_POWER_STATE, parm);
981 		snd_hda_codec_write(codec, 0x11, 0,
982 				    AC_VERB_SET_POWER_STATE, parm);
983 
984 		/* outputs */
985 		/* AOW0 (8h)*/
986 		snd_hda_codec_write(codec, 0x8, 0,
987 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
988 
989 		/* PW4 (28h), MW4 (18h), MUX4(38h) */
990 		parm = AC_PWRST_D3;
991 		set_pin_power_state(codec, 0x28, &parm);
992 		snd_hda_codec_write(codec, 0x18, 0,
993 				    AC_VERB_SET_POWER_STATE, parm);
994 		snd_hda_codec_write(codec, 0x38, 0,
995 				    AC_VERB_SET_POWER_STATE, parm);
996 
997 		/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
998 		parm = AC_PWRST_D3;
999 		set_pin_power_state(codec, 0x25, &parm);
1000 		snd_hda_codec_write(codec, 0x15, 0,
1001 				    AC_VERB_SET_POWER_STATE, parm);
1002 		snd_hda_codec_write(codec, 0x35, 0,
1003 				    AC_VERB_SET_POWER_STATE, parm);
1004 		if (spec->hp_independent_mode)	{
1005 			snd_hda_codec_write(codec, 0x9, 0,
1006 					    AC_VERB_SET_POWER_STATE, parm);
1007 		}
1008 
1009 		/* Internal Speaker */
1010 		/* PW0 (24h), MW0(14h), MUX0(34h) */
1011 		present = snd_hda_jack_detect(codec, 0x25);
1012 		parm = AC_PWRST_D3;
1013 		set_pin_power_state(codec, 0x24, &parm);
1014 		if (present) {
1015 			snd_hda_codec_write(codec, 0x14, 0,
1016 					    AC_VERB_SET_POWER_STATE,
1017 					    AC_PWRST_D3);
1018 			snd_hda_codec_write(codec, 0x34, 0,
1019 					    AC_VERB_SET_POWER_STATE,
1020 					    AC_PWRST_D3);
1021 		} else {
1022 			snd_hda_codec_write(codec, 0x14, 0,
1023 					    AC_VERB_SET_POWER_STATE,
1024 					    AC_PWRST_D0);
1025 			snd_hda_codec_write(codec, 0x34, 0,
1026 					    AC_VERB_SET_POWER_STATE,
1027 					    AC_PWRST_D0);
1028 		}
1029 		/* Mono Out */
1030 		/* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
1031 		present = snd_hda_jack_detect(codec, 0x28);
1032 		parm = AC_PWRST_D3;
1033 		set_pin_power_state(codec, 0x31, &parm);
1034 		if (present) {
1035 			snd_hda_codec_write(codec, 0x1c, 0,
1036 					    AC_VERB_SET_POWER_STATE,
1037 					    AC_PWRST_D3);
1038 			snd_hda_codec_write(codec, 0x3c, 0,
1039 					    AC_VERB_SET_POWER_STATE,
1040 					    AC_PWRST_D3);
1041 			snd_hda_codec_write(codec, 0x3e, 0,
1042 					    AC_VERB_SET_POWER_STATE,
1043 					    AC_PWRST_D3);
1044 		} else {
1045 			snd_hda_codec_write(codec, 0x1c, 0,
1046 					    AC_VERB_SET_POWER_STATE,
1047 					    AC_PWRST_D0);
1048 			snd_hda_codec_write(codec, 0x3c, 0,
1049 					    AC_VERB_SET_POWER_STATE,
1050 					    AC_PWRST_D0);
1051 			snd_hda_codec_write(codec, 0x3e, 0,
1052 					    AC_VERB_SET_POWER_STATE,
1053 					    AC_PWRST_D0);
1054 		}
1055 
1056 		/* PW15 (33h), MW15 (1dh), MUX15(3dh) */
1057 		parm = AC_PWRST_D3;
1058 		set_pin_power_state(codec, 0x33, &parm);
1059 		snd_hda_codec_write(codec, 0x1d, 0,
1060 				    AC_VERB_SET_POWER_STATE, parm);
1061 		snd_hda_codec_write(codec, 0x3d, 0,
1062 				    AC_VERB_SET_POWER_STATE, parm);
1063 
1064 		/* MW9 (21h) */
1065 		if (imux_is_smixer || !is_aa_path_mute(codec))
1066 			snd_hda_codec_write(
1067 				codec, 0x21, 0,
1068 				AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1069 		else
1070 			snd_hda_codec_write(
1071 				codec, 0x21, 0,
1072 				AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1073 	}
1074 }
1075 
1076 /*
1077  * input MUX handling
1078  */
1079 static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
1080 			     struct snd_ctl_elem_info *uinfo)
1081 {
1082 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1083 	struct via_spec *spec = codec->spec;
1084 	return snd_hda_input_mux_info(spec->input_mux, uinfo);
1085 }
1086 
1087 static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
1088 			    struct snd_ctl_elem_value *ucontrol)
1089 {
1090 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1091 	struct via_spec *spec = codec->spec;
1092 	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1093 
1094 	ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1095 	return 0;
1096 }
1097 
1098 static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
1099 			    struct snd_ctl_elem_value *ucontrol)
1100 {
1101 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1102 	struct via_spec *spec = codec->spec;
1103 	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1104 
1105 	if (!spec->mux_nids[adc_idx])
1106 		return -EINVAL;
1107 	/* switch to D0 beofre change index */
1108 	if (snd_hda_codec_read(codec, spec->mux_nids[adc_idx], 0,
1109 			       AC_VERB_GET_POWER_STATE, 0x00) != AC_PWRST_D0)
1110 		snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
1111 				    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1112 	/* update jack power state */
1113 	set_jack_power_state(codec);
1114 
1115 	return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
1116 				     spec->mux_nids[adc_idx],
1117 				     &spec->cur_mux[adc_idx]);
1118 }
1119 
1120 static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
1121 				   struct snd_ctl_elem_info *uinfo)
1122 {
1123 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1124 	struct via_spec *spec = codec->spec;
1125 	return snd_hda_input_mux_info(spec->hp_mux, uinfo);
1126 }
1127 
1128 static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
1129 				  struct snd_ctl_elem_value *ucontrol)
1130 {
1131 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1132 	hda_nid_t nid = kcontrol->private_value;
1133 	unsigned int pinsel;
1134 
1135 	/* use !! to translate conn sel 2 for VT1718S */
1136 	pinsel = !!snd_hda_codec_read(codec, nid, 0,
1137 				      AC_VERB_GET_CONNECT_SEL,
1138 				      0x00);
1139 	ucontrol->value.enumerated.item[0] = pinsel;
1140 
1141 	return 0;
1142 }
1143 
1144 static void activate_ctl(struct hda_codec *codec, const char *name, int active)
1145 {
1146 	struct snd_kcontrol *ctl = snd_hda_find_mixer_ctl(codec, name);
1147 	if (ctl) {
1148 		ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1149 		ctl->vd[0].access |= active
1150 			? 0 : SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1151 		snd_ctl_notify(codec->bus->card,
1152 			       SNDRV_CTL_EVENT_MASK_VALUE, &ctl->id);
1153 	}
1154 }
1155 
1156 static hda_nid_t side_mute_channel(struct via_spec *spec)
1157 {
1158 	switch (spec->codec_type) {
1159 	case VT1708:		return 0x1b;
1160 	case VT1709_10CH:	return 0x29;
1161 	case VT1708B_8CH:	/* fall thru */
1162 	case VT1708S:		return 0x27;
1163 	default:		return 0;
1164 	}
1165 }
1166 
1167 static int update_side_mute_status(struct hda_codec *codec)
1168 {
1169 	/* mute side channel */
1170 	struct via_spec *spec = codec->spec;
1171 	unsigned int parm = spec->hp_independent_mode
1172 		? AMP_OUT_MUTE : AMP_OUT_UNMUTE;
1173 	hda_nid_t sw3 = side_mute_channel(spec);
1174 
1175 	if (sw3)
1176 		snd_hda_codec_write(codec, sw3, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1177 				    parm);
1178 	return 0;
1179 }
1180 
1181 static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
1182 				  struct snd_ctl_elem_value *ucontrol)
1183 {
1184 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1185 	struct via_spec *spec = codec->spec;
1186 	hda_nid_t nid = kcontrol->private_value;
1187 	unsigned int pinsel = ucontrol->value.enumerated.item[0];
1188 	/* Get Independent Mode index of headphone pin widget */
1189 	spec->hp_independent_mode = spec->hp_independent_mode_index == pinsel
1190 		? 1 : 0;
1191 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, pinsel);
1192 
1193 	if (spec->multiout.hp_nid && spec->multiout.hp_nid
1194 	    != spec->multiout.dac_nids[HDA_FRONT])
1195 		snd_hda_codec_setup_stream(codec, spec->multiout.hp_nid,
1196 					   0, 0, 0);
1197 
1198 	update_side_mute_status(codec);
1199 	/* update HP volume/swtich active state */
1200 	if (spec->codec_type == VT1708S
1201 	    || spec->codec_type == VT1702
1202 	    || spec->codec_type == VT1718S
1203 	    || spec->codec_type == VT1716S
1204 	    || spec->codec_type == VT2002P
1205 	    || spec->codec_type == VT1812) {
1206 		activate_ctl(codec, "Headphone Playback Volume",
1207 			     spec->hp_independent_mode);
1208 		activate_ctl(codec, "Headphone Playback Switch",
1209 			     spec->hp_independent_mode);
1210 	}
1211 	return 0;
1212 }
1213 
1214 static struct snd_kcontrol_new via_hp_mixer[2] = {
1215 	{
1216 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1217 		.name = "Independent HP",
1218 		.info = via_independent_hp_info,
1219 		.get = via_independent_hp_get,
1220 		.put = via_independent_hp_put,
1221 	},
1222 	{
1223 		.iface = NID_MAPPING,
1224 		.name = "Independent HP",
1225 	},
1226 };
1227 
1228 static int via_hp_build(struct hda_codec *codec)
1229 {
1230 	struct via_spec *spec = codec->spec;
1231 	struct snd_kcontrol_new *knew;
1232 	hda_nid_t nid;
1233 	int nums;
1234 	hda_nid_t conn[HDA_MAX_CONNECTIONS];
1235 
1236 	switch (spec->codec_type) {
1237 	case VT1718S:
1238 		nid = 0x34;
1239 		break;
1240 	case VT2002P:
1241 		nid = 0x35;
1242 		break;
1243 	case VT1812:
1244 		nid = 0x3d;
1245 		break;
1246 	default:
1247 		nid = spec->autocfg.hp_pins[0];
1248 		break;
1249 	}
1250 
1251 	nums = snd_hda_get_connections(codec, nid, conn, HDA_MAX_CONNECTIONS);
1252 	if (nums <= 1)
1253 		return 0;
1254 
1255 	knew = via_clone_control(spec, &via_hp_mixer[0]);
1256 	if (knew == NULL)
1257 		return -ENOMEM;
1258 
1259 	knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
1260 	knew->private_value = nid;
1261 
1262 	knew = via_clone_control(spec, &via_hp_mixer[1]);
1263 	if (knew == NULL)
1264 		return -ENOMEM;
1265 	knew->subdevice = side_mute_channel(spec);
1266 
1267 	return 0;
1268 }
1269 
1270 static void notify_aa_path_ctls(struct hda_codec *codec)
1271 {
1272 	int i;
1273 	struct snd_ctl_elem_id id;
1274 	const char *labels[] = {"Mic", "Front Mic", "Line"};
1275 
1276 	memset(&id, 0, sizeof(id));
1277 	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1278 	for (i = 0; i < ARRAY_SIZE(labels); i++) {
1279 		sprintf(id.name, "%s Playback Volume", labels[i]);
1280 		snd_ctl_notify(codec->bus->card, SNDRV_CTL_EVENT_MASK_VALUE,
1281 			       &id);
1282 	}
1283 }
1284 
1285 static void mute_aa_path(struct hda_codec *codec, int mute)
1286 {
1287 	struct via_spec *spec = codec->spec;
1288 	hda_nid_t  nid_mixer;
1289 	int start_idx;
1290 	int end_idx;
1291 	int i;
1292 	/* get nid of MW0 and start & end index */
1293 	switch (spec->codec_type) {
1294 	case VT1708:
1295 		nid_mixer = 0x17;
1296 		start_idx = 2;
1297 		end_idx = 4;
1298 		break;
1299 	case VT1709_10CH:
1300 	case VT1709_6CH:
1301 		nid_mixer = 0x18;
1302 		start_idx = 2;
1303 		end_idx = 4;
1304 		break;
1305 	case VT1708B_8CH:
1306 	case VT1708B_4CH:
1307 	case VT1708S:
1308 	case VT1716S:
1309 		nid_mixer = 0x16;
1310 		start_idx = 2;
1311 		end_idx = 4;
1312 		break;
1313 	default:
1314 		return;
1315 	}
1316 	/* check AA path's mute status */
1317 	for (i = start_idx; i <= end_idx; i++) {
1318 		int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
1319 		snd_hda_codec_amp_stereo(codec, nid_mixer, HDA_INPUT, i,
1320 					 HDA_AMP_MUTE, val);
1321 	}
1322 }
1323 static int is_smart51_pins(struct via_spec *spec, hda_nid_t pin)
1324 {
1325 	int res = 0;
1326 	int index;
1327 	for (index = AUTO_PIN_MIC; index < AUTO_PIN_FRONT_LINE; index++) {
1328 		if (pin == spec->autocfg.input_pins[index]) {
1329 			res = 1;
1330 			break;
1331 		}
1332 	}
1333 	return res;
1334 }
1335 
1336 static int via_smart51_info(struct snd_kcontrol *kcontrol,
1337 			    struct snd_ctl_elem_info *uinfo)
1338 {
1339 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1340 	uinfo->count = 1;
1341 	uinfo->value.integer.min = 0;
1342 	uinfo->value.integer.max = 1;
1343 	return 0;
1344 }
1345 
1346 static int via_smart51_get(struct snd_kcontrol *kcontrol,
1347 			   struct snd_ctl_elem_value *ucontrol)
1348 {
1349 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1350 	struct via_spec *spec = codec->spec;
1351 	int index[] = { AUTO_PIN_MIC, AUTO_PIN_FRONT_MIC, AUTO_PIN_LINE };
1352 	int on = 1;
1353 	int i;
1354 
1355 	for (i = 0; i < ARRAY_SIZE(index); i++) {
1356 		hda_nid_t nid = spec->autocfg.input_pins[index[i]];
1357 		if (nid) {
1358 			int ctl =
1359 			    snd_hda_codec_read(codec, nid, 0,
1360 					       AC_VERB_GET_PIN_WIDGET_CONTROL,
1361 					       0);
1362 			if (i == AUTO_PIN_FRONT_MIC
1363 			    && spec->hp_independent_mode
1364 			    && spec->codec_type != VT1718S)
1365 				continue; /* ignore FMic for independent HP */
1366 			if (ctl & AC_PINCTL_IN_EN
1367 			    && !(ctl & AC_PINCTL_OUT_EN))
1368 				on = 0;
1369 		}
1370 	}
1371 	*ucontrol->value.integer.value = on;
1372 	return 0;
1373 }
1374 
1375 static int via_smart51_put(struct snd_kcontrol *kcontrol,
1376 			   struct snd_ctl_elem_value *ucontrol)
1377 {
1378 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1379 	struct via_spec *spec = codec->spec;
1380 	int out_in = *ucontrol->value.integer.value
1381 		? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
1382 	int index[] = { AUTO_PIN_MIC, AUTO_PIN_FRONT_MIC, AUTO_PIN_LINE };
1383 	int i;
1384 
1385 	for (i = 0; i < ARRAY_SIZE(index); i++) {
1386 		hda_nid_t nid = spec->autocfg.input_pins[index[i]];
1387 		if (i == AUTO_PIN_FRONT_MIC
1388 		    && spec->hp_independent_mode
1389 		    && spec->codec_type != VT1718S)
1390 			continue; /* don't retask FMic for independent HP */
1391 		if (nid) {
1392 			unsigned int parm = snd_hda_codec_read(
1393 				codec, nid, 0,
1394 				AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1395 			parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
1396 			parm |= out_in;
1397 			snd_hda_codec_write(codec, nid, 0,
1398 					    AC_VERB_SET_PIN_WIDGET_CONTROL,
1399 					    parm);
1400 			if (out_in == AC_PINCTL_OUT_EN) {
1401 				mute_aa_path(codec, 1);
1402 				notify_aa_path_ctls(codec);
1403 			}
1404 			if (spec->codec_type == VT1718S)
1405 				snd_hda_codec_amp_stereo(
1406 					codec, nid, HDA_OUTPUT, 0, HDA_AMP_MUTE,
1407 					HDA_AMP_UNMUTE);
1408 		}
1409 		if (i == AUTO_PIN_FRONT_MIC) {
1410 			if (spec->codec_type == VT1708S
1411 			    || spec->codec_type == VT1716S) {
1412 				/* input = index 1 (AOW3) */
1413 				snd_hda_codec_write(
1414 					codec, nid, 0,
1415 					AC_VERB_SET_CONNECT_SEL, 1);
1416 				snd_hda_codec_amp_stereo(
1417 					codec, nid, HDA_OUTPUT,
1418 					0, HDA_AMP_MUTE, HDA_AMP_UNMUTE);
1419 			}
1420 		}
1421 	}
1422 	spec->smart51_enabled = *ucontrol->value.integer.value;
1423 	set_jack_power_state(codec);
1424 	return 1;
1425 }
1426 
1427 static struct snd_kcontrol_new via_smart51_mixer[2] = {
1428 	{
1429 	 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1430 	 .name = "Smart 5.1",
1431 	 .count = 1,
1432 	 .info = via_smart51_info,
1433 	 .get = via_smart51_get,
1434 	 .put = via_smart51_put,
1435 	 },
1436 	{
1437 	 .iface = NID_MAPPING,
1438 	 .name = "Smart 5.1",
1439 	}
1440 };
1441 
1442 static int via_smart51_build(struct via_spec *spec)
1443 {
1444 	struct snd_kcontrol_new *knew;
1445 	int index[] = { AUTO_PIN_MIC, AUTO_PIN_FRONT_MIC, AUTO_PIN_LINE };
1446 	hda_nid_t nid;
1447 	int i;
1448 
1449 	knew = via_clone_control(spec, &via_smart51_mixer[0]);
1450 	if (knew == NULL)
1451 		return -ENOMEM;
1452 
1453 	for (i = 0; i < ARRAY_SIZE(index); i++) {
1454 		nid = spec->autocfg.input_pins[index[i]];
1455 		if (nid) {
1456 			knew = via_clone_control(spec, &via_smart51_mixer[1]);
1457 			if (knew == NULL)
1458 				return -ENOMEM;
1459 			knew->subdevice = nid;
1460 		}
1461 	}
1462 
1463 	return 0;
1464 }
1465 
1466 /* capture mixer elements */
1467 static struct snd_kcontrol_new vt1708_capture_mixer[] = {
1468 	HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_INPUT),
1469 	HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_INPUT),
1470 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x27, 0x0, HDA_INPUT),
1471 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x27, 0x0, HDA_INPUT),
1472 	{
1473 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1474 		/* The multiple "Capture Source" controls confuse alsamixer
1475 		 * So call somewhat different..
1476 		 */
1477 		/* .name = "Capture Source", */
1478 		.name = "Input Source",
1479 		.count = 1,
1480 		.info = via_mux_enum_info,
1481 		.get = via_mux_enum_get,
1482 		.put = via_mux_enum_put,
1483 	},
1484 	{ } /* end */
1485 };
1486 
1487 /* check AA path's mute statue */
1488 static int is_aa_path_mute(struct hda_codec *codec)
1489 {
1490 	int mute = 1;
1491 	hda_nid_t  nid_mixer;
1492 	int start_idx;
1493 	int end_idx;
1494 	int i;
1495 	struct via_spec *spec = codec->spec;
1496 	/* get nid of MW0 and start & end index */
1497 	switch (spec->codec_type) {
1498 	case VT1708B_8CH:
1499 	case VT1708B_4CH:
1500 	case VT1708S:
1501 	case VT1716S:
1502 		nid_mixer = 0x16;
1503 		start_idx = 2;
1504 		end_idx = 4;
1505 		break;
1506 	case VT1702:
1507 		nid_mixer = 0x1a;
1508 		start_idx = 1;
1509 		end_idx = 3;
1510 		break;
1511 	case VT1718S:
1512 		nid_mixer = 0x21;
1513 		start_idx = 1;
1514 		end_idx = 3;
1515 		break;
1516 	case VT2002P:
1517 	case VT1812:
1518 		nid_mixer = 0x21;
1519 		start_idx = 0;
1520 		end_idx = 2;
1521 		break;
1522 	default:
1523 		return 0;
1524 	}
1525 	/* check AA path's mute status */
1526 	for (i = start_idx; i <= end_idx; i++) {
1527 		unsigned int con_list = snd_hda_codec_read(
1528 			codec, nid_mixer, 0, AC_VERB_GET_CONNECT_LIST, i/4*4);
1529 		int shift = 8 * (i % 4);
1530 		hda_nid_t nid_pin = (con_list & (0xff << shift)) >> shift;
1531 		unsigned int defconf = snd_hda_codec_get_pincfg(codec, nid_pin);
1532 		if (get_defcfg_connect(defconf) == AC_JACK_PORT_COMPLEX) {
1533 			/* check mute status while the pin is connected */
1534 			int mute_l = snd_hda_codec_amp_read(codec, nid_mixer, 0,
1535 							    HDA_INPUT, i) >> 7;
1536 			int mute_r = snd_hda_codec_amp_read(codec, nid_mixer, 1,
1537 							    HDA_INPUT, i) >> 7;
1538 			if (!mute_l || !mute_r) {
1539 				mute = 0;
1540 				break;
1541 			}
1542 		}
1543 	}
1544 	return mute;
1545 }
1546 
1547 /* enter/exit analog low-current mode */
1548 static void analog_low_current_mode(struct hda_codec *codec, int stream_idle)
1549 {
1550 	struct via_spec *spec = codec->spec;
1551 	static int saved_stream_idle = 1; /* saved stream idle status */
1552 	int enable = is_aa_path_mute(codec);
1553 	unsigned int verb = 0;
1554 	unsigned int parm = 0;
1555 
1556 	if (stream_idle == -1)	/* stream status did not change */
1557 		enable = enable && saved_stream_idle;
1558 	else {
1559 		enable = enable && stream_idle;
1560 		saved_stream_idle = stream_idle;
1561 	}
1562 
1563 	/* decide low current mode's verb & parameter */
1564 	switch (spec->codec_type) {
1565 	case VT1708B_8CH:
1566 	case VT1708B_4CH:
1567 		verb = 0xf70;
1568 		parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
1569 		break;
1570 	case VT1708S:
1571 	case VT1718S:
1572 	case VT1716S:
1573 		verb = 0xf73;
1574 		parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
1575 		break;
1576 	case VT1702:
1577 		verb = 0xf73;
1578 		parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
1579 		break;
1580 	case VT2002P:
1581 	case VT1812:
1582 		verb = 0xf93;
1583 		parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
1584 		break;
1585 	default:
1586 		return;		/* other codecs are not supported */
1587 	}
1588 	/* send verb */
1589 	snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
1590 }
1591 
1592 /*
1593  * generic initialization of ADC, input mixers and output mixers
1594  */
1595 static struct hda_verb vt1708_volume_init_verbs[] = {
1596 	/*
1597 	 * Unmute ADC0-1 and set the default input to mic-in
1598 	 */
1599 	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1600 	{0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1601 
1602 
1603 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
1604 	 * mixer widget
1605 	 */
1606 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
1607 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1608 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1609 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
1610 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
1611 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
1612 
1613 	/*
1614 	 * Set up output mixers (0x19 - 0x1b)
1615 	 */
1616 	/* set vol=0 to output mixers */
1617 	{0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1618 	{0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1619 	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1620 
1621 	/* Setup default input MW0 to PW4 */
1622 	{0x20, AC_VERB_SET_CONNECT_SEL, 0},
1623 	/* PW9 Output enable */
1624 	{0x25, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
1625 	{ }
1626 };
1627 
1628 static int via_playback_pcm_open(struct hda_pcm_stream *hinfo,
1629 				 struct hda_codec *codec,
1630 				 struct snd_pcm_substream *substream)
1631 {
1632 	struct via_spec *spec = codec->spec;
1633 	int idle = substream->pstr->substream_opened == 1
1634 		&& substream->ref_count == 0;
1635 	analog_low_current_mode(codec, idle);
1636 	return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
1637 					     hinfo);
1638 }
1639 
1640 static void playback_multi_pcm_prep_0(struct hda_codec *codec,
1641 				      unsigned int stream_tag,
1642 				      unsigned int format,
1643 				      struct snd_pcm_substream *substream)
1644 {
1645 	struct via_spec *spec = codec->spec;
1646 	struct hda_multi_out *mout = &spec->multiout;
1647 	hda_nid_t *nids = mout->dac_nids;
1648 	int chs = substream->runtime->channels;
1649 	int i;
1650 
1651 	mutex_lock(&codec->spdif_mutex);
1652 	if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1653 		if (chs == 2 &&
1654 		    snd_hda_is_supported_format(codec, mout->dig_out_nid,
1655 						format) &&
1656 		    !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
1657 			mout->dig_out_used = HDA_DIG_ANALOG_DUP;
1658 			/* turn off SPDIF once; otherwise the IEC958 bits won't
1659 			 * be updated */
1660 			if (codec->spdif_ctls & AC_DIG1_ENABLE)
1661 				snd_hda_codec_write(codec, mout->dig_out_nid, 0,
1662 						    AC_VERB_SET_DIGI_CONVERT_1,
1663 						    codec->spdif_ctls &
1664 							~AC_DIG1_ENABLE & 0xff);
1665 			snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1666 						   stream_tag, 0, format);
1667 			/* turn on again (if needed) */
1668 			if (codec->spdif_ctls & AC_DIG1_ENABLE)
1669 				snd_hda_codec_write(codec, mout->dig_out_nid, 0,
1670 						    AC_VERB_SET_DIGI_CONVERT_1,
1671 						    codec->spdif_ctls & 0xff);
1672 		} else {
1673 			mout->dig_out_used = 0;
1674 			snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1675 						   0, 0, 0);
1676 		}
1677 	}
1678 	mutex_unlock(&codec->spdif_mutex);
1679 
1680 	/* front */
1681 	snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
1682 				   0, format);
1683 
1684 	if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]
1685 	    && !spec->hp_independent_mode)
1686 		/* headphone out will just decode front left/right (stereo) */
1687 		snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
1688 					   0, format);
1689 
1690 	/* extra outputs copied from front */
1691 	for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1692 		if (mout->extra_out_nid[i])
1693 			snd_hda_codec_setup_stream(codec,
1694 						   mout->extra_out_nid[i],
1695 						   stream_tag, 0, format);
1696 
1697 	/* surrounds */
1698 	for (i = 1; i < mout->num_dacs; i++) {
1699 		if (chs >= (i + 1) * 2) /* independent out */
1700 			snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
1701 						   i * 2, format);
1702 		else /* copy front */
1703 			snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
1704 						   0, format);
1705 	}
1706 }
1707 
1708 static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
1709 					  struct hda_codec *codec,
1710 					  unsigned int stream_tag,
1711 					  unsigned int format,
1712 					  struct snd_pcm_substream *substream)
1713 {
1714 	struct via_spec *spec = codec->spec;
1715 	struct hda_multi_out *mout = &spec->multiout;
1716 	hda_nid_t *nids = mout->dac_nids;
1717 
1718 	if (substream->number == 0)
1719 		playback_multi_pcm_prep_0(codec, stream_tag, format,
1720 					  substream);
1721 	else {
1722 		if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
1723 		    spec->hp_independent_mode)
1724 			snd_hda_codec_setup_stream(codec, mout->hp_nid,
1725 						   stream_tag, 0, format);
1726 	}
1727 	vt1708_start_hp_work(spec);
1728 	return 0;
1729 }
1730 
1731 static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
1732 				    struct hda_codec *codec,
1733 				    struct snd_pcm_substream *substream)
1734 {
1735 	struct via_spec *spec = codec->spec;
1736 	struct hda_multi_out *mout = &spec->multiout;
1737 	hda_nid_t *nids = mout->dac_nids;
1738 	int i;
1739 
1740 	if (substream->number == 0) {
1741 		for (i = 0; i < mout->num_dacs; i++)
1742 			snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
1743 
1744 		if (mout->hp_nid && !spec->hp_independent_mode)
1745 			snd_hda_codec_setup_stream(codec, mout->hp_nid,
1746 						   0, 0, 0);
1747 
1748 		for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1749 			if (mout->extra_out_nid[i])
1750 				snd_hda_codec_setup_stream(codec,
1751 							mout->extra_out_nid[i],
1752 							0, 0, 0);
1753 		mutex_lock(&codec->spdif_mutex);
1754 		if (mout->dig_out_nid &&
1755 		    mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
1756 			snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1757 						   0, 0, 0);
1758 			mout->dig_out_used = 0;
1759 		}
1760 		mutex_unlock(&codec->spdif_mutex);
1761 	} else {
1762 		if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
1763 		    spec->hp_independent_mode)
1764 			snd_hda_codec_setup_stream(codec, mout->hp_nid,
1765 						   0, 0, 0);
1766 	}
1767 	vt1708_stop_hp_work(spec);
1768 	return 0;
1769 }
1770 
1771 /*
1772  * Digital out
1773  */
1774 static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1775 				     struct hda_codec *codec,
1776 				     struct snd_pcm_substream *substream)
1777 {
1778 	struct via_spec *spec = codec->spec;
1779 	return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1780 }
1781 
1782 static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1783 				      struct hda_codec *codec,
1784 				      struct snd_pcm_substream *substream)
1785 {
1786 	struct via_spec *spec = codec->spec;
1787 	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1788 }
1789 
1790 static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1791 					struct hda_codec *codec,
1792 					unsigned int stream_tag,
1793 					unsigned int format,
1794 					struct snd_pcm_substream *substream)
1795 {
1796 	struct via_spec *spec = codec->spec;
1797 	return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1798 					     stream_tag, format, substream);
1799 }
1800 
1801 static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1802 					struct hda_codec *codec,
1803 					struct snd_pcm_substream *substream)
1804 {
1805 	struct via_spec *spec = codec->spec;
1806 	snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
1807 	return 0;
1808 }
1809 
1810 /*
1811  * Analog capture
1812  */
1813 static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1814 				   struct hda_codec *codec,
1815 				   unsigned int stream_tag,
1816 				   unsigned int format,
1817 				   struct snd_pcm_substream *substream)
1818 {
1819 	struct via_spec *spec = codec->spec;
1820 
1821 	snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1822 				   stream_tag, 0, format);
1823 	return 0;
1824 }
1825 
1826 static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1827 				   struct hda_codec *codec,
1828 				   struct snd_pcm_substream *substream)
1829 {
1830 	struct via_spec *spec = codec->spec;
1831 	snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
1832 	return 0;
1833 }
1834 
1835 static struct hda_pcm_stream vt1708_pcm_analog_playback = {
1836 	.substreams = 2,
1837 	.channels_min = 2,
1838 	.channels_max = 8,
1839 	.nid = 0x10, /* NID to query formats and rates */
1840 	.ops = {
1841 		.open = via_playback_pcm_open,
1842 		.prepare = via_playback_multi_pcm_prepare,
1843 		.cleanup = via_playback_multi_pcm_cleanup
1844 	},
1845 };
1846 
1847 static struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
1848 	.substreams = 2,
1849 	.channels_min = 2,
1850 	.channels_max = 8,
1851 	.nid = 0x10, /* NID to query formats and rates */
1852 	/* We got noisy outputs on the right channel on VT1708 when
1853 	 * 24bit samples are used.  Until any workaround is found,
1854 	 * disable the 24bit format, so far.
1855 	 */
1856 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
1857 	.ops = {
1858 		.open = via_playback_pcm_open,
1859 		.prepare = via_playback_multi_pcm_prepare,
1860 		.cleanup = via_playback_multi_pcm_cleanup
1861 	},
1862 };
1863 
1864 static struct hda_pcm_stream vt1708_pcm_analog_capture = {
1865 	.substreams = 2,
1866 	.channels_min = 2,
1867 	.channels_max = 2,
1868 	.nid = 0x15, /* NID to query formats and rates */
1869 	.ops = {
1870 		.prepare = via_capture_pcm_prepare,
1871 		.cleanup = via_capture_pcm_cleanup
1872 	},
1873 };
1874 
1875 static struct hda_pcm_stream vt1708_pcm_digital_playback = {
1876 	.substreams = 1,
1877 	.channels_min = 2,
1878 	.channels_max = 2,
1879 	/* NID is set in via_build_pcms */
1880 	.ops = {
1881 		.open = via_dig_playback_pcm_open,
1882 		.close = via_dig_playback_pcm_close,
1883 		.prepare = via_dig_playback_pcm_prepare,
1884 		.cleanup = via_dig_playback_pcm_cleanup
1885 	},
1886 };
1887 
1888 static struct hda_pcm_stream vt1708_pcm_digital_capture = {
1889 	.substreams = 1,
1890 	.channels_min = 2,
1891 	.channels_max = 2,
1892 };
1893 
1894 static int via_build_controls(struct hda_codec *codec)
1895 {
1896 	struct via_spec *spec = codec->spec;
1897 	struct snd_kcontrol *kctl;
1898 	struct snd_kcontrol_new *knew;
1899 	int err, i;
1900 
1901 	for (i = 0; i < spec->num_mixers; i++) {
1902 		err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1903 		if (err < 0)
1904 			return err;
1905 	}
1906 
1907 	if (spec->multiout.dig_out_nid) {
1908 		err = snd_hda_create_spdif_out_ctls(codec,
1909 						    spec->multiout.dig_out_nid);
1910 		if (err < 0)
1911 			return err;
1912 		err = snd_hda_create_spdif_share_sw(codec,
1913 						    &spec->multiout);
1914 		if (err < 0)
1915 			return err;
1916 		spec->multiout.share_spdif = 1;
1917 	}
1918 	if (spec->dig_in_nid) {
1919 		err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1920 		if (err < 0)
1921 			return err;
1922 	}
1923 
1924 	/* assign Capture Source enums to NID */
1925 	kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1926 	for (i = 0; kctl && i < kctl->count; i++) {
1927 		err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
1928 		if (err < 0)
1929 			return err;
1930 	}
1931 
1932 	/* other nid->control mapping */
1933 	for (i = 0; i < spec->num_mixers; i++) {
1934 		for (knew = spec->mixers[i]; knew->name; knew++) {
1935 			if (knew->iface != NID_MAPPING)
1936 				continue;
1937 			kctl = snd_hda_find_mixer_ctl(codec, knew->name);
1938 			if (kctl == NULL)
1939 				continue;
1940 			err = snd_hda_add_nid(codec, kctl, 0,
1941 					      knew->subdevice);
1942 		}
1943 	}
1944 
1945 	/* init power states */
1946 	set_jack_power_state(codec);
1947 	analog_low_current_mode(codec, 1);
1948 
1949 	via_free_kctls(codec); /* no longer needed */
1950 	return 0;
1951 }
1952 
1953 static int via_build_pcms(struct hda_codec *codec)
1954 {
1955 	struct via_spec *spec = codec->spec;
1956 	struct hda_pcm *info = spec->pcm_rec;
1957 
1958 	codec->num_pcms = 1;
1959 	codec->pcm_info = info;
1960 
1961 	info->name = spec->stream_name_analog;
1962 	info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1963 		*(spec->stream_analog_playback);
1964 	info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1965 		spec->multiout.dac_nids[0];
1966 	info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_analog_capture);
1967 	info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
1968 
1969 	info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1970 		spec->multiout.max_channels;
1971 
1972 	if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1973 		codec->num_pcms++;
1974 		info++;
1975 		info->name = spec->stream_name_digital;
1976 		info->pcm_type = HDA_PCM_TYPE_SPDIF;
1977 		if (spec->multiout.dig_out_nid) {
1978 			info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1979 				*(spec->stream_digital_playback);
1980 			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1981 				spec->multiout.dig_out_nid;
1982 		}
1983 		if (spec->dig_in_nid) {
1984 			info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1985 				*(spec->stream_digital_capture);
1986 			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1987 				spec->dig_in_nid;
1988 		}
1989 	}
1990 
1991 	return 0;
1992 }
1993 
1994 static void via_free(struct hda_codec *codec)
1995 {
1996 	struct via_spec *spec = codec->spec;
1997 
1998 	if (!spec)
1999 		return;
2000 
2001 	via_free_kctls(codec);
2002 	vt1708_stop_hp_work(spec);
2003 	kfree(codec->spec);
2004 }
2005 
2006 /* mute internal speaker if HP is plugged */
2007 static void via_hp_automute(struct hda_codec *codec)
2008 {
2009 	unsigned int present = 0;
2010 	struct via_spec *spec = codec->spec;
2011 
2012 	present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
2013 
2014 	if (!spec->hp_independent_mode) {
2015 		struct snd_ctl_elem_id id;
2016 		/* auto mute */
2017 		snd_hda_codec_amp_stereo(
2018 			codec, spec->autocfg.line_out_pins[0], HDA_OUTPUT, 0,
2019 			HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
2020 		/* notify change */
2021 		memset(&id, 0, sizeof(id));
2022 		id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2023 		strcpy(id.name, "Front Playback Switch");
2024 		snd_ctl_notify(codec->bus->card, SNDRV_CTL_EVENT_MASK_VALUE,
2025 			       &id);
2026 	}
2027 }
2028 
2029 /* mute mono out if HP or Line out is plugged */
2030 static void via_mono_automute(struct hda_codec *codec)
2031 {
2032 	unsigned int hp_present, lineout_present;
2033 	struct via_spec *spec = codec->spec;
2034 
2035 	if (spec->codec_type != VT1716S)
2036 		return;
2037 
2038 	lineout_present = snd_hda_jack_detect(codec,
2039 					      spec->autocfg.line_out_pins[0]);
2040 
2041 	/* Mute Mono Out if Line Out is plugged */
2042 	if (lineout_present) {
2043 		snd_hda_codec_amp_stereo(
2044 			codec, 0x2A, HDA_OUTPUT, 0, HDA_AMP_MUTE, HDA_AMP_MUTE);
2045 		return;
2046 	}
2047 
2048 	hp_present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
2049 
2050 	if (!spec->hp_independent_mode)
2051 		snd_hda_codec_amp_stereo(
2052 			codec, 0x2A, HDA_OUTPUT, 0, HDA_AMP_MUTE,
2053 			hp_present ? HDA_AMP_MUTE : 0);
2054 }
2055 
2056 static void via_gpio_control(struct hda_codec *codec)
2057 {
2058 	unsigned int gpio_data;
2059 	unsigned int vol_counter;
2060 	unsigned int vol;
2061 	unsigned int master_vol;
2062 
2063 	struct via_spec *spec = codec->spec;
2064 
2065 	gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
2066 				       AC_VERB_GET_GPIO_DATA, 0) & 0x03;
2067 
2068 	vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
2069 					  0xF84, 0) & 0x3F0000) >> 16;
2070 
2071 	vol = vol_counter & 0x1F;
2072 	master_vol = snd_hda_codec_read(codec, 0x1A, 0,
2073 					AC_VERB_GET_AMP_GAIN_MUTE,
2074 					AC_AMP_GET_INPUT);
2075 
2076 	if (gpio_data == 0x02) {
2077 		/* unmute line out */
2078 		snd_hda_codec_amp_stereo(codec, spec->autocfg.line_out_pins[0],
2079 					 HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
2080 
2081 		if (vol_counter & 0x20) {
2082 			/* decrease volume */
2083 			if (vol > master_vol)
2084 				vol = master_vol;
2085 			snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
2086 						 0, HDA_AMP_VOLMASK,
2087 						 master_vol-vol);
2088 		} else {
2089 			/* increase volume */
2090 			snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
2091 					 HDA_AMP_VOLMASK,
2092 					 ((master_vol+vol) > 0x2A) ? 0x2A :
2093 					  (master_vol+vol));
2094 		}
2095 	} else if (!(gpio_data & 0x02)) {
2096 		/* mute line out */
2097 		snd_hda_codec_amp_stereo(codec,
2098 					 spec->autocfg.line_out_pins[0],
2099 					 HDA_OUTPUT, 0, HDA_AMP_MUTE,
2100 					 HDA_AMP_MUTE);
2101 	}
2102 }
2103 
2104 /* mute Internal-Speaker if HP is plugged */
2105 static void via_speaker_automute(struct hda_codec *codec)
2106 {
2107 	unsigned int hp_present;
2108 	struct via_spec *spec = codec->spec;
2109 
2110 	if (spec->codec_type != VT2002P && spec->codec_type != VT1812)
2111 		return;
2112 
2113 	hp_present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
2114 
2115 	if (!spec->hp_independent_mode) {
2116 		struct snd_ctl_elem_id id;
2117 		snd_hda_codec_amp_stereo(
2118 			codec, spec->autocfg.speaker_pins[0], HDA_OUTPUT, 0,
2119 			HDA_AMP_MUTE, hp_present ? HDA_AMP_MUTE : 0);
2120 		/* notify change */
2121 		memset(&id, 0, sizeof(id));
2122 		id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2123 		strcpy(id.name, "Speaker Playback Switch");
2124 		snd_ctl_notify(codec->bus->card, SNDRV_CTL_EVENT_MASK_VALUE,
2125 			       &id);
2126 	}
2127 }
2128 
2129 /* mute line-out and internal speaker if HP is plugged */
2130 static void via_hp_bind_automute(struct hda_codec *codec)
2131 {
2132 	/* use long instead of int below just to avoid an internal compiler
2133 	 * error with gcc 4.0.x
2134 	 */
2135 	unsigned long hp_present, present = 0;
2136 	struct via_spec *spec = codec->spec;
2137 	int i;
2138 
2139 	if (!spec->autocfg.hp_pins[0] || !spec->autocfg.line_out_pins[0])
2140 		return;
2141 
2142 	hp_present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
2143 
2144 	present = snd_hda_jack_detect(codec, spec->autocfg.line_out_pins[0]);
2145 
2146 	if (!spec->hp_independent_mode) {
2147 		/* Mute Line-Outs */
2148 		for (i = 0; i < spec->autocfg.line_outs; i++)
2149 			snd_hda_codec_amp_stereo(
2150 				codec, spec->autocfg.line_out_pins[i],
2151 				HDA_OUTPUT, 0,
2152 				HDA_AMP_MUTE, hp_present ? HDA_AMP_MUTE : 0);
2153 		if (hp_present)
2154 			present = hp_present;
2155 	}
2156 	/* Speakers */
2157 	for (i = 0; i < spec->autocfg.speaker_outs; i++)
2158 		snd_hda_codec_amp_stereo(
2159 			codec, spec->autocfg.speaker_pins[i], HDA_OUTPUT, 0,
2160 			HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
2161 }
2162 
2163 
2164 /* unsolicited event for jack sensing */
2165 static void via_unsol_event(struct hda_codec *codec,
2166 				  unsigned int res)
2167 {
2168 	res >>= 26;
2169 	if (res & VIA_HP_EVENT)
2170 		via_hp_automute(codec);
2171 	if (res & VIA_GPIO_EVENT)
2172 		via_gpio_control(codec);
2173 	if (res & VIA_JACK_EVENT)
2174 		set_jack_power_state(codec);
2175 	if (res & VIA_MONO_EVENT)
2176 		via_mono_automute(codec);
2177 	if (res & VIA_SPEAKER_EVENT)
2178 		via_speaker_automute(codec);
2179 	if (res & VIA_BIND_HP_EVENT)
2180 		via_hp_bind_automute(codec);
2181 }
2182 
2183 static int via_init(struct hda_codec *codec)
2184 {
2185 	struct via_spec *spec = codec->spec;
2186 	int i;
2187 	for (i = 0; i < spec->num_iverbs; i++)
2188 		snd_hda_sequence_write(codec, spec->init_verbs[i]);
2189 
2190 	spec->codec_type = get_codec_type(codec);
2191 	if (spec->codec_type == VT1708BCE)
2192 		spec->codec_type = VT1708S; /* VT1708BCE & VT1708S are almost
2193 					       same */
2194 	/* Lydia Add for EAPD enable */
2195 	if (!spec->dig_in_nid) { /* No Digital In connection */
2196 		if (spec->dig_in_pin) {
2197 			snd_hda_codec_write(codec, spec->dig_in_pin, 0,
2198 					    AC_VERB_SET_PIN_WIDGET_CONTROL,
2199 					    PIN_OUT);
2200 			snd_hda_codec_write(codec, spec->dig_in_pin, 0,
2201 					    AC_VERB_SET_EAPD_BTLENABLE, 0x02);
2202 		}
2203 	} else /* enable SPDIF-input pin */
2204 		snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
2205 				    AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
2206 
2207 	/* assign slave outs */
2208 	if (spec->slave_dig_outs[0])
2209 		codec->slave_dig_outs = spec->slave_dig_outs;
2210 
2211 	return 0;
2212 }
2213 
2214 #ifdef SND_HDA_NEEDS_RESUME
2215 static int via_suspend(struct hda_codec *codec, pm_message_t state)
2216 {
2217 	struct via_spec *spec = codec->spec;
2218 	vt1708_stop_hp_work(spec);
2219 	return 0;
2220 }
2221 #endif
2222 
2223 #ifdef CONFIG_SND_HDA_POWER_SAVE
2224 static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
2225 {
2226 	struct via_spec *spec = codec->spec;
2227 	return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
2228 }
2229 #endif
2230 
2231 /*
2232  */
2233 static struct hda_codec_ops via_patch_ops = {
2234 	.build_controls = via_build_controls,
2235 	.build_pcms = via_build_pcms,
2236 	.init = via_init,
2237 	.free = via_free,
2238 #ifdef SND_HDA_NEEDS_RESUME
2239 	.suspend = via_suspend,
2240 #endif
2241 #ifdef CONFIG_SND_HDA_POWER_SAVE
2242 	.check_power_status = via_check_power_status,
2243 #endif
2244 };
2245 
2246 /* fill in the dac_nids table from the parsed pin configuration */
2247 static int vt1708_auto_fill_dac_nids(struct via_spec *spec,
2248 				     const struct auto_pin_cfg *cfg)
2249 {
2250 	int i;
2251 	hda_nid_t nid;
2252 
2253 	spec->multiout.num_dacs = cfg->line_outs;
2254 
2255 	spec->multiout.dac_nids = spec->private_dac_nids;
2256 
2257 	for (i = 0; i < 4; i++) {
2258 		nid = cfg->line_out_pins[i];
2259 		if (nid) {
2260 			/* config dac list */
2261 			switch (i) {
2262 			case AUTO_SEQ_FRONT:
2263 				spec->multiout.dac_nids[i] = 0x10;
2264 				break;
2265 			case AUTO_SEQ_CENLFE:
2266 				spec->multiout.dac_nids[i] = 0x12;
2267 				break;
2268 			case AUTO_SEQ_SURROUND:
2269 				spec->multiout.dac_nids[i] = 0x11;
2270 				break;
2271 			case AUTO_SEQ_SIDE:
2272 				spec->multiout.dac_nids[i] = 0x13;
2273 				break;
2274 			}
2275 		}
2276 	}
2277 
2278 	return 0;
2279 }
2280 
2281 /* add playback controls from the parsed DAC table */
2282 static int vt1708_auto_create_multi_out_ctls(struct via_spec *spec,
2283 					     const struct auto_pin_cfg *cfg)
2284 {
2285 	char name[32];
2286 	static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
2287 	hda_nid_t nid, nid_vol, nid_vols[] = {0x17, 0x19, 0x1a, 0x1b};
2288 	int i, err;
2289 
2290 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
2291 		nid = cfg->line_out_pins[i];
2292 
2293 		if (!nid)
2294 			continue;
2295 
2296 		nid_vol = nid_vols[i];
2297 
2298 		if (i == AUTO_SEQ_CENLFE) {
2299 			/* Center/LFE */
2300 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2301 					"Center Playback Volume",
2302 					HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2303 							    HDA_OUTPUT));
2304 			if (err < 0)
2305 				return err;
2306 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2307 					      "LFE Playback Volume",
2308 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2309 								  HDA_OUTPUT));
2310 			if (err < 0)
2311 				return err;
2312 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2313 					      "Center Playback Switch",
2314 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2315 								  HDA_OUTPUT));
2316 			if (err < 0)
2317 				return err;
2318 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2319 					      "LFE Playback Switch",
2320 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2321 								  HDA_OUTPUT));
2322 			if (err < 0)
2323 				return err;
2324 		} else if (i == AUTO_SEQ_FRONT) {
2325 			/* add control to mixer index 0 */
2326 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2327 					      "Master Front Playback Volume",
2328 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2329 								  HDA_INPUT));
2330 			if (err < 0)
2331 				return err;
2332 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2333 					      "Master Front Playback Switch",
2334 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2335 								  HDA_INPUT));
2336 			if (err < 0)
2337 				return err;
2338 
2339 			/* add control to PW3 */
2340 			sprintf(name, "%s Playback Volume", chname[i]);
2341 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2342 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2343 								  HDA_OUTPUT));
2344 			if (err < 0)
2345 				return err;
2346 			sprintf(name, "%s Playback Switch", chname[i]);
2347 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2348 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2349 								  HDA_OUTPUT));
2350 			if (err < 0)
2351 				return err;
2352 		} else {
2353 			sprintf(name, "%s Playback Volume", chname[i]);
2354 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2355 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2356 								  HDA_OUTPUT));
2357 			if (err < 0)
2358 				return err;
2359 			sprintf(name, "%s Playback Switch", chname[i]);
2360 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2361 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2362 								  HDA_OUTPUT));
2363 			if (err < 0)
2364 				return err;
2365 		}
2366 	}
2367 
2368 	return 0;
2369 }
2370 
2371 static void create_hp_imux(struct via_spec *spec)
2372 {
2373 	int i;
2374 	struct hda_input_mux *imux = &spec->private_imux[1];
2375 	static const char *texts[] = { "OFF", "ON", NULL};
2376 
2377 	/* for hp mode select */
2378 	i = 0;
2379 	while (texts[i] != NULL) {
2380 		imux->items[imux->num_items].label =  texts[i];
2381 		imux->items[imux->num_items].index = i;
2382 		imux->num_items++;
2383 		i++;
2384 	}
2385 
2386 	spec->hp_mux = &spec->private_imux[1];
2387 }
2388 
2389 static int vt1708_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
2390 {
2391 	int err;
2392 
2393 	if (!pin)
2394 		return 0;
2395 
2396 	spec->multiout.hp_nid = VT1708_HP_NID; /* AOW3 */
2397 	spec->hp_independent_mode_index = 1;
2398 
2399 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2400 			      "Headphone Playback Volume",
2401 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2402 	if (err < 0)
2403 		return err;
2404 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2405 			      "Headphone Playback Switch",
2406 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2407 	if (err < 0)
2408 		return err;
2409 
2410 	create_hp_imux(spec);
2411 
2412 	return 0;
2413 }
2414 
2415 /* create playback/capture controls for input pins */
2416 static int vt1708_auto_create_analog_input_ctls(struct via_spec *spec,
2417 						const struct auto_pin_cfg *cfg)
2418 {
2419 	static char *labels[] = {
2420 		"Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
2421 	};
2422 	struct hda_input_mux *imux = &spec->private_imux[0];
2423 	int i, err, idx = 0;
2424 
2425 	/* for internal loopback recording select */
2426 	imux->items[imux->num_items].label = "Stereo Mixer";
2427 	imux->items[imux->num_items].index = idx;
2428 	imux->num_items++;
2429 
2430 	for (i = 0; i < AUTO_PIN_LAST; i++) {
2431 		if (!cfg->input_pins[i])
2432 			continue;
2433 
2434 		switch (cfg->input_pins[i]) {
2435 		case 0x1d: /* Mic */
2436 			idx = 2;
2437 			break;
2438 
2439 		case 0x1e: /* Line In */
2440 			idx = 3;
2441 			break;
2442 
2443 		case 0x21: /* Front Mic */
2444 			idx = 4;
2445 			break;
2446 
2447 		case 0x24: /* CD */
2448 			idx = 1;
2449 			break;
2450 		}
2451 		err = via_new_analog_input(spec, labels[i], idx, 0x17);
2452 		if (err < 0)
2453 			return err;
2454 		imux->items[imux->num_items].label = labels[i];
2455 		imux->items[imux->num_items].index = idx;
2456 		imux->num_items++;
2457 	}
2458 	return 0;
2459 }
2460 
2461 #ifdef CONFIG_SND_HDA_POWER_SAVE
2462 static struct hda_amp_list vt1708_loopbacks[] = {
2463 	{ 0x17, HDA_INPUT, 1 },
2464 	{ 0x17, HDA_INPUT, 2 },
2465 	{ 0x17, HDA_INPUT, 3 },
2466 	{ 0x17, HDA_INPUT, 4 },
2467 	{ } /* end */
2468 };
2469 #endif
2470 
2471 static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
2472 {
2473 	unsigned int def_conf;
2474 	unsigned char seqassoc;
2475 
2476 	def_conf = snd_hda_codec_get_pincfg(codec, nid);
2477 	seqassoc = (unsigned char) get_defcfg_association(def_conf);
2478 	seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
2479 	if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
2480 	    && (seqassoc == 0xf0 || seqassoc == 0xff)) {
2481 		def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
2482 		snd_hda_codec_set_pincfg(codec, nid, def_conf);
2483 	}
2484 
2485 	return;
2486 }
2487 
2488 static int vt1708_jack_detectect_get(struct snd_kcontrol *kcontrol,
2489 				     struct snd_ctl_elem_value *ucontrol)
2490 {
2491 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2492 	struct via_spec *spec = codec->spec;
2493 
2494 	if (spec->codec_type != VT1708)
2495 		return 0;
2496 	spec->vt1708_jack_detectect =
2497 		!((snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8) & 0x1);
2498 	ucontrol->value.integer.value[0] = spec->vt1708_jack_detectect;
2499 	return 0;
2500 }
2501 
2502 static int vt1708_jack_detectect_put(struct snd_kcontrol *kcontrol,
2503 				     struct snd_ctl_elem_value *ucontrol)
2504 {
2505 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2506 	struct via_spec *spec = codec->spec;
2507 	int change;
2508 
2509 	if (spec->codec_type != VT1708)
2510 		return 0;
2511 	spec->vt1708_jack_detectect = ucontrol->value.integer.value[0];
2512 	change = (0x1 & (snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8))
2513 		== !spec->vt1708_jack_detectect;
2514 	if (spec->vt1708_jack_detectect) {
2515 		mute_aa_path(codec, 1);
2516 		notify_aa_path_ctls(codec);
2517 	}
2518 	return change;
2519 }
2520 
2521 static struct snd_kcontrol_new vt1708_jack_detectect[] = {
2522 	{
2523 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2524 		.name = "Jack Detect",
2525 		.count = 1,
2526 		.info = snd_ctl_boolean_mono_info,
2527 		.get = vt1708_jack_detectect_get,
2528 		.put = vt1708_jack_detectect_put,
2529 	},
2530 	{} /* end */
2531 };
2532 
2533 static int vt1708_parse_auto_config(struct hda_codec *codec)
2534 {
2535 	struct via_spec *spec = codec->spec;
2536 	int err;
2537 
2538 	/* Add HP and CD pin config connect bit re-config action */
2539 	vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
2540 	vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
2541 
2542 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2543 	if (err < 0)
2544 		return err;
2545 	err = vt1708_auto_fill_dac_nids(spec, &spec->autocfg);
2546 	if (err < 0)
2547 		return err;
2548 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2549 		return 0; /* can't find valid BIOS pin config */
2550 
2551 	err = vt1708_auto_create_multi_out_ctls(spec, &spec->autocfg);
2552 	if (err < 0)
2553 		return err;
2554 	err = vt1708_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2555 	if (err < 0)
2556 		return err;
2557 	err = vt1708_auto_create_analog_input_ctls(spec, &spec->autocfg);
2558 	if (err < 0)
2559 		return err;
2560 	/* add jack detect on/off control */
2561 	err = snd_hda_add_new_ctls(codec, vt1708_jack_detectect);
2562 	if (err < 0)
2563 		return err;
2564 
2565 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2566 
2567 	if (spec->autocfg.dig_outs)
2568 		spec->multiout.dig_out_nid = VT1708_DIGOUT_NID;
2569 	spec->dig_in_pin = VT1708_DIGIN_PIN;
2570 	if (spec->autocfg.dig_in_pin)
2571 		spec->dig_in_nid = VT1708_DIGIN_NID;
2572 
2573 	if (spec->kctls.list)
2574 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
2575 
2576 	spec->init_verbs[spec->num_iverbs++] = vt1708_volume_init_verbs;
2577 
2578 	spec->input_mux = &spec->private_imux[0];
2579 
2580 	if (spec->hp_mux)
2581 		via_hp_build(codec);
2582 
2583 	via_smart51_build(spec);
2584 	return 1;
2585 }
2586 
2587 /* init callback for auto-configuration model -- overriding the default init */
2588 static int via_auto_init(struct hda_codec *codec)
2589 {
2590 	struct via_spec *spec = codec->spec;
2591 
2592 	via_init(codec);
2593 	via_auto_init_multi_out(codec);
2594 	via_auto_init_hp_out(codec);
2595 	via_auto_init_analog_input(codec);
2596 	if (spec->codec_type == VT2002P || spec->codec_type == VT1812) {
2597 		via_hp_bind_automute(codec);
2598 	} else {
2599 		via_hp_automute(codec);
2600 		via_speaker_automute(codec);
2601 	}
2602 
2603 	return 0;
2604 }
2605 
2606 static void vt1708_update_hp_jack_state(struct work_struct *work)
2607 {
2608 	struct via_spec *spec = container_of(work, struct via_spec,
2609 					     vt1708_hp_work.work);
2610 	if (spec->codec_type != VT1708)
2611 		return;
2612 	/* if jack state toggled */
2613 	if (spec->vt1708_hp_present
2614 	    != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
2615 		spec->vt1708_hp_present ^= 1;
2616 		via_hp_automute(spec->codec);
2617 	}
2618 	vt1708_start_hp_work(spec);
2619 }
2620 
2621 static int get_mux_nids(struct hda_codec *codec)
2622 {
2623 	struct via_spec *spec = codec->spec;
2624 	hda_nid_t nid, conn[8];
2625 	unsigned int type;
2626 	int i, n;
2627 
2628 	for (i = 0; i < spec->num_adc_nids; i++) {
2629 		nid = spec->adc_nids[i];
2630 		while (nid) {
2631 			type = get_wcaps_type(get_wcaps(codec, nid));
2632 			if (type == AC_WID_PIN)
2633 				break;
2634 			n = snd_hda_get_connections(codec, nid, conn,
2635 						    ARRAY_SIZE(conn));
2636 			if (n <= 0)
2637 				break;
2638 			if (n > 1) {
2639 				spec->mux_nids[i] = nid;
2640 				break;
2641 			}
2642 			nid = conn[0];
2643 		}
2644 	}
2645 	return 0;
2646 }
2647 
2648 static int patch_vt1708(struct hda_codec *codec)
2649 {
2650 	struct via_spec *spec;
2651 	int err;
2652 
2653 	/* create a codec specific record */
2654 	spec = via_new_spec(codec);
2655 	if (spec == NULL)
2656 		return -ENOMEM;
2657 
2658 	/* automatic parse from the BIOS config */
2659 	err = vt1708_parse_auto_config(codec);
2660 	if (err < 0) {
2661 		via_free(codec);
2662 		return err;
2663 	} else if (!err) {
2664 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
2665 		       "from BIOS.  Using genenic mode...\n");
2666 	}
2667 
2668 
2669 	spec->stream_name_analog = "VT1708 Analog";
2670 	spec->stream_analog_playback = &vt1708_pcm_analog_playback;
2671 	/* disable 32bit format on VT1708 */
2672 	if (codec->vendor_id == 0x11061708)
2673 		spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
2674 	spec->stream_analog_capture = &vt1708_pcm_analog_capture;
2675 
2676 	spec->stream_name_digital = "VT1708 Digital";
2677 	spec->stream_digital_playback = &vt1708_pcm_digital_playback;
2678 	spec->stream_digital_capture = &vt1708_pcm_digital_capture;
2679 
2680 
2681 	if (!spec->adc_nids && spec->input_mux) {
2682 		spec->adc_nids = vt1708_adc_nids;
2683 		spec->num_adc_nids = ARRAY_SIZE(vt1708_adc_nids);
2684 		get_mux_nids(codec);
2685 		spec->mixers[spec->num_mixers] = vt1708_capture_mixer;
2686 		spec->num_mixers++;
2687 	}
2688 
2689 	codec->patch_ops = via_patch_ops;
2690 
2691 	codec->patch_ops.init = via_auto_init;
2692 #ifdef CONFIG_SND_HDA_POWER_SAVE
2693 	spec->loopback.amplist = vt1708_loopbacks;
2694 #endif
2695 	INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
2696 	return 0;
2697 }
2698 
2699 /* capture mixer elements */
2700 static struct snd_kcontrol_new vt1709_capture_mixer[] = {
2701 	HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x0, HDA_INPUT),
2702 	HDA_CODEC_MUTE("Capture Switch", 0x14, 0x0, HDA_INPUT),
2703 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x15, 0x0, HDA_INPUT),
2704 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x15, 0x0, HDA_INPUT),
2705 	HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x16, 0x0, HDA_INPUT),
2706 	HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x16, 0x0, HDA_INPUT),
2707 	{
2708 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2709 		/* The multiple "Capture Source" controls confuse alsamixer
2710 		 * So call somewhat different..
2711 		 */
2712 		/* .name = "Capture Source", */
2713 		.name = "Input Source",
2714 		.count = 1,
2715 		.info = via_mux_enum_info,
2716 		.get = via_mux_enum_get,
2717 		.put = via_mux_enum_put,
2718 	},
2719 	{ } /* end */
2720 };
2721 
2722 static struct hda_verb vt1709_uniwill_init_verbs[] = {
2723 	{0x20, AC_VERB_SET_UNSOLICITED_ENABLE,
2724 	 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
2725 	{ }
2726 };
2727 
2728 /*
2729  * generic initialization of ADC, input mixers and output mixers
2730  */
2731 static struct hda_verb vt1709_10ch_volume_init_verbs[] = {
2732 	/*
2733 	 * Unmute ADC0-2 and set the default input to mic-in
2734 	 */
2735 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2736 	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2737 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2738 
2739 
2740 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2741 	 * mixer widget
2742 	 */
2743 	/* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2744 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2745 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2746 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2747 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2748 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2749 
2750 	/*
2751 	 * Set up output selector (0x1a, 0x1b, 0x29)
2752 	 */
2753 	/* set vol=0 to output mixers */
2754 	{0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2755 	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2756 	{0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2757 
2758 	/*
2759 	 *  Unmute PW3 and PW4
2760 	 */
2761 	{0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2762 	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2763 
2764 	/* Set input of PW4 as MW0 */
2765 	{0x20, AC_VERB_SET_CONNECT_SEL, 0},
2766 	/* PW9 Output enable */
2767 	{0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2768 	{ }
2769 };
2770 
2771 static struct hda_pcm_stream vt1709_10ch_pcm_analog_playback = {
2772 	.substreams = 1,
2773 	.channels_min = 2,
2774 	.channels_max = 10,
2775 	.nid = 0x10, /* NID to query formats and rates */
2776 	.ops = {
2777 		.open = via_playback_pcm_open,
2778 		.prepare = via_playback_multi_pcm_prepare,
2779 		.cleanup = via_playback_multi_pcm_cleanup,
2780 	},
2781 };
2782 
2783 static struct hda_pcm_stream vt1709_6ch_pcm_analog_playback = {
2784 	.substreams = 1,
2785 	.channels_min = 2,
2786 	.channels_max = 6,
2787 	.nid = 0x10, /* NID to query formats and rates */
2788 	.ops = {
2789 		.open = via_playback_pcm_open,
2790 		.prepare = via_playback_multi_pcm_prepare,
2791 		.cleanup = via_playback_multi_pcm_cleanup,
2792 	},
2793 };
2794 
2795 static struct hda_pcm_stream vt1709_pcm_analog_capture = {
2796 	.substreams = 2,
2797 	.channels_min = 2,
2798 	.channels_max = 2,
2799 	.nid = 0x14, /* NID to query formats and rates */
2800 	.ops = {
2801 		.prepare = via_capture_pcm_prepare,
2802 		.cleanup = via_capture_pcm_cleanup
2803 	},
2804 };
2805 
2806 static struct hda_pcm_stream vt1709_pcm_digital_playback = {
2807 	.substreams = 1,
2808 	.channels_min = 2,
2809 	.channels_max = 2,
2810 	/* NID is set in via_build_pcms */
2811 	.ops = {
2812 		.open = via_dig_playback_pcm_open,
2813 		.close = via_dig_playback_pcm_close
2814 	},
2815 };
2816 
2817 static struct hda_pcm_stream vt1709_pcm_digital_capture = {
2818 	.substreams = 1,
2819 	.channels_min = 2,
2820 	.channels_max = 2,
2821 };
2822 
2823 static int vt1709_auto_fill_dac_nids(struct via_spec *spec,
2824 				     const struct auto_pin_cfg *cfg)
2825 {
2826 	int i;
2827 	hda_nid_t nid;
2828 
2829 	if (cfg->line_outs == 4)  /* 10 channels */
2830 		spec->multiout.num_dacs = cfg->line_outs+1; /* AOW0~AOW4 */
2831 	else if (cfg->line_outs == 3) /* 6 channels */
2832 		spec->multiout.num_dacs = cfg->line_outs; /* AOW0~AOW2 */
2833 
2834 	spec->multiout.dac_nids = spec->private_dac_nids;
2835 
2836 	if (cfg->line_outs == 4) { /* 10 channels */
2837 		for (i = 0; i < cfg->line_outs; i++) {
2838 			nid = cfg->line_out_pins[i];
2839 			if (nid) {
2840 				/* config dac list */
2841 				switch (i) {
2842 				case AUTO_SEQ_FRONT:
2843 					/* AOW0 */
2844 					spec->multiout.dac_nids[i] = 0x10;
2845 					break;
2846 				case AUTO_SEQ_CENLFE:
2847 					/* AOW2 */
2848 					spec->multiout.dac_nids[i] = 0x12;
2849 					break;
2850 				case AUTO_SEQ_SURROUND:
2851 					/* AOW3 */
2852 					spec->multiout.dac_nids[i] = 0x11;
2853 					break;
2854 				case AUTO_SEQ_SIDE:
2855 					/* AOW1 */
2856 					spec->multiout.dac_nids[i] = 0x27;
2857 					break;
2858 				default:
2859 					break;
2860 				}
2861 			}
2862 		}
2863 		spec->multiout.dac_nids[cfg->line_outs] = 0x28; /* AOW4 */
2864 
2865 	} else if (cfg->line_outs == 3) { /* 6 channels */
2866 		for (i = 0; i < cfg->line_outs; i++) {
2867 			nid = cfg->line_out_pins[i];
2868 			if (nid) {
2869 				/* config dac list */
2870 				switch (i) {
2871 				case AUTO_SEQ_FRONT:
2872 					/* AOW0 */
2873 					spec->multiout.dac_nids[i] = 0x10;
2874 					break;
2875 				case AUTO_SEQ_CENLFE:
2876 					/* AOW2 */
2877 					spec->multiout.dac_nids[i] = 0x12;
2878 					break;
2879 				case AUTO_SEQ_SURROUND:
2880 					/* AOW1 */
2881 					spec->multiout.dac_nids[i] = 0x11;
2882 					break;
2883 				default:
2884 					break;
2885 				}
2886 			}
2887 		}
2888 	}
2889 
2890 	return 0;
2891 }
2892 
2893 /* add playback controls from the parsed DAC table */
2894 static int vt1709_auto_create_multi_out_ctls(struct via_spec *spec,
2895 					     const struct auto_pin_cfg *cfg)
2896 {
2897 	char name[32];
2898 	static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
2899 	hda_nid_t nid, nid_vol, nid_vols[] = {0x18, 0x1a, 0x1b, 0x29};
2900 	int i, err;
2901 
2902 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
2903 		nid = cfg->line_out_pins[i];
2904 
2905 		if (!nid)
2906 			continue;
2907 
2908 		nid_vol = nid_vols[i];
2909 
2910 		if (i == AUTO_SEQ_CENLFE) {
2911 			/* Center/LFE */
2912 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2913 					      "Center Playback Volume",
2914 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2915 								  HDA_OUTPUT));
2916 			if (err < 0)
2917 				return err;
2918 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2919 					      "LFE Playback Volume",
2920 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2921 								  HDA_OUTPUT));
2922 			if (err < 0)
2923 				return err;
2924 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2925 					      "Center Playback Switch",
2926 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2927 								  HDA_OUTPUT));
2928 			if (err < 0)
2929 				return err;
2930 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2931 					      "LFE Playback Switch",
2932 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2933 								  HDA_OUTPUT));
2934 			if (err < 0)
2935 				return err;
2936 		} else if (i == AUTO_SEQ_FRONT) {
2937 			/* ADD control to mixer index 0 */
2938 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2939 					      "Master Front Playback Volume",
2940 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2941 								  HDA_INPUT));
2942 			if (err < 0)
2943 				return err;
2944 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2945 					      "Master Front Playback Switch",
2946 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2947 								  HDA_INPUT));
2948 			if (err < 0)
2949 				return err;
2950 
2951 			/* add control to PW3 */
2952 			sprintf(name, "%s Playback Volume", chname[i]);
2953 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2954 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2955 								  HDA_OUTPUT));
2956 			if (err < 0)
2957 				return err;
2958 			sprintf(name, "%s Playback Switch", chname[i]);
2959 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2960 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2961 								  HDA_OUTPUT));
2962 			if (err < 0)
2963 				return err;
2964 		} else if (i == AUTO_SEQ_SURROUND) {
2965 			sprintf(name, "%s Playback Volume", chname[i]);
2966 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2967 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2968 								  HDA_OUTPUT));
2969 			if (err < 0)
2970 				return err;
2971 			sprintf(name, "%s Playback Switch", chname[i]);
2972 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2973 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2974 								  HDA_OUTPUT));
2975 			if (err < 0)
2976 				return err;
2977 		} else if (i == AUTO_SEQ_SIDE) {
2978 			sprintf(name, "%s Playback Volume", chname[i]);
2979 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2980 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2981 								  HDA_OUTPUT));
2982 			if (err < 0)
2983 				return err;
2984 			sprintf(name, "%s Playback Switch", chname[i]);
2985 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2986 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2987 								  HDA_OUTPUT));
2988 			if (err < 0)
2989 				return err;
2990 		}
2991 	}
2992 
2993 	return 0;
2994 }
2995 
2996 static int vt1709_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
2997 {
2998 	int err;
2999 
3000 	if (!pin)
3001 		return 0;
3002 
3003 	if (spec->multiout.num_dacs == 5) /* 10 channels */
3004 		spec->multiout.hp_nid = VT1709_HP_DAC_NID;
3005 	else if (spec->multiout.num_dacs == 3) /* 6 channels */
3006 		spec->multiout.hp_nid = 0;
3007 	spec->hp_independent_mode_index = 1;
3008 
3009 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3010 			      "Headphone Playback Volume",
3011 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3012 	if (err < 0)
3013 		return err;
3014 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3015 			      "Headphone Playback Switch",
3016 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3017 	if (err < 0)
3018 		return err;
3019 
3020 	return 0;
3021 }
3022 
3023 /* create playback/capture controls for input pins */
3024 static int vt1709_auto_create_analog_input_ctls(struct via_spec *spec,
3025 						const struct auto_pin_cfg *cfg)
3026 {
3027 	static char *labels[] = {
3028 		"Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
3029 	};
3030 	struct hda_input_mux *imux = &spec->private_imux[0];
3031 	int i, err, idx = 0;
3032 
3033 	/* for internal loopback recording select */
3034 	imux->items[imux->num_items].label = "Stereo Mixer";
3035 	imux->items[imux->num_items].index = idx;
3036 	imux->num_items++;
3037 
3038 	for (i = 0; i < AUTO_PIN_LAST; i++) {
3039 		if (!cfg->input_pins[i])
3040 			continue;
3041 
3042 		switch (cfg->input_pins[i]) {
3043 		case 0x1d: /* Mic */
3044 			idx = 2;
3045 			break;
3046 
3047 		case 0x1e: /* Line In */
3048 			idx = 3;
3049 			break;
3050 
3051 		case 0x21: /* Front Mic */
3052 			idx = 4;
3053 			break;
3054 
3055 		case 0x23: /* CD */
3056 			idx = 1;
3057 			break;
3058 		}
3059 		err = via_new_analog_input(spec, labels[i], idx, 0x18);
3060 		if (err < 0)
3061 			return err;
3062 		imux->items[imux->num_items].label = labels[i];
3063 		imux->items[imux->num_items].index = idx;
3064 		imux->num_items++;
3065 	}
3066 	return 0;
3067 }
3068 
3069 static int vt1709_parse_auto_config(struct hda_codec *codec)
3070 {
3071 	struct via_spec *spec = codec->spec;
3072 	int err;
3073 
3074 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3075 	if (err < 0)
3076 		return err;
3077 	err = vt1709_auto_fill_dac_nids(spec, &spec->autocfg);
3078 	if (err < 0)
3079 		return err;
3080 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3081 		return 0; /* can't find valid BIOS pin config */
3082 
3083 	err = vt1709_auto_create_multi_out_ctls(spec, &spec->autocfg);
3084 	if (err < 0)
3085 		return err;
3086 	err = vt1709_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
3087 	if (err < 0)
3088 		return err;
3089 	err = vt1709_auto_create_analog_input_ctls(spec, &spec->autocfg);
3090 	if (err < 0)
3091 		return err;
3092 
3093 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3094 
3095 	if (spec->autocfg.dig_outs)
3096 		spec->multiout.dig_out_nid = VT1709_DIGOUT_NID;
3097 	spec->dig_in_pin = VT1709_DIGIN_PIN;
3098 	if (spec->autocfg.dig_in_pin)
3099 		spec->dig_in_nid = VT1709_DIGIN_NID;
3100 
3101 	if (spec->kctls.list)
3102 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
3103 
3104 	spec->input_mux = &spec->private_imux[0];
3105 
3106 	if (spec->hp_mux)
3107 		via_hp_build(codec);
3108 
3109 	via_smart51_build(spec);
3110 	return 1;
3111 }
3112 
3113 #ifdef CONFIG_SND_HDA_POWER_SAVE
3114 static struct hda_amp_list vt1709_loopbacks[] = {
3115 	{ 0x18, HDA_INPUT, 1 },
3116 	{ 0x18, HDA_INPUT, 2 },
3117 	{ 0x18, HDA_INPUT, 3 },
3118 	{ 0x18, HDA_INPUT, 4 },
3119 	{ } /* end */
3120 };
3121 #endif
3122 
3123 static int patch_vt1709_10ch(struct hda_codec *codec)
3124 {
3125 	struct via_spec *spec;
3126 	int err;
3127 
3128 	/* create a codec specific record */
3129 	spec = via_new_spec(codec);
3130 	if (spec == NULL)
3131 		return -ENOMEM;
3132 
3133 	err = vt1709_parse_auto_config(codec);
3134 	if (err < 0) {
3135 		via_free(codec);
3136 		return err;
3137 	} else if (!err) {
3138 		printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
3139 		       "Using genenic mode...\n");
3140 	}
3141 
3142 	spec->init_verbs[spec->num_iverbs++] = vt1709_10ch_volume_init_verbs;
3143 	spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
3144 
3145 	spec->stream_name_analog = "VT1709 Analog";
3146 	spec->stream_analog_playback = &vt1709_10ch_pcm_analog_playback;
3147 	spec->stream_analog_capture = &vt1709_pcm_analog_capture;
3148 
3149 	spec->stream_name_digital = "VT1709 Digital";
3150 	spec->stream_digital_playback = &vt1709_pcm_digital_playback;
3151 	spec->stream_digital_capture = &vt1709_pcm_digital_capture;
3152 
3153 
3154 	if (!spec->adc_nids && spec->input_mux) {
3155 		spec->adc_nids = vt1709_adc_nids;
3156 		spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
3157 		get_mux_nids(codec);
3158 		spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
3159 		spec->num_mixers++;
3160 	}
3161 
3162 	codec->patch_ops = via_patch_ops;
3163 
3164 	codec->patch_ops.init = via_auto_init;
3165 	codec->patch_ops.unsol_event = via_unsol_event;
3166 #ifdef CONFIG_SND_HDA_POWER_SAVE
3167 	spec->loopback.amplist = vt1709_loopbacks;
3168 #endif
3169 
3170 	return 0;
3171 }
3172 /*
3173  * generic initialization of ADC, input mixers and output mixers
3174  */
3175 static struct hda_verb vt1709_6ch_volume_init_verbs[] = {
3176 	/*
3177 	 * Unmute ADC0-2 and set the default input to mic-in
3178 	 */
3179 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3180 	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3181 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3182 
3183 
3184 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3185 	 * mixer widget
3186 	 */
3187 	/* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3188 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3189 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3190 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3191 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3192 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
3193 
3194 	/*
3195 	 * Set up output selector (0x1a, 0x1b, 0x29)
3196 	 */
3197 	/* set vol=0 to output mixers */
3198 	{0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3199 	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3200 	{0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3201 
3202 	/*
3203 	 *  Unmute PW3 and PW4
3204 	 */
3205 	{0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3206 	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3207 
3208 	/* Set input of PW4 as MW0 */
3209 	{0x20, AC_VERB_SET_CONNECT_SEL, 0},
3210 	/* PW9 Output enable */
3211 	{0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3212 	{ }
3213 };
3214 
3215 static int patch_vt1709_6ch(struct hda_codec *codec)
3216 {
3217 	struct via_spec *spec;
3218 	int err;
3219 
3220 	/* create a codec specific record */
3221 	spec = via_new_spec(codec);
3222 	if (spec == NULL)
3223 		return -ENOMEM;
3224 
3225 	err = vt1709_parse_auto_config(codec);
3226 	if (err < 0) {
3227 		via_free(codec);
3228 		return err;
3229 	} else if (!err) {
3230 		printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
3231 		       "Using genenic mode...\n");
3232 	}
3233 
3234 	spec->init_verbs[spec->num_iverbs++] = vt1709_6ch_volume_init_verbs;
3235 	spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
3236 
3237 	spec->stream_name_analog = "VT1709 Analog";
3238 	spec->stream_analog_playback = &vt1709_6ch_pcm_analog_playback;
3239 	spec->stream_analog_capture = &vt1709_pcm_analog_capture;
3240 
3241 	spec->stream_name_digital = "VT1709 Digital";
3242 	spec->stream_digital_playback = &vt1709_pcm_digital_playback;
3243 	spec->stream_digital_capture = &vt1709_pcm_digital_capture;
3244 
3245 
3246 	if (!spec->adc_nids && spec->input_mux) {
3247 		spec->adc_nids = vt1709_adc_nids;
3248 		spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
3249 		get_mux_nids(codec);
3250 		spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
3251 		spec->num_mixers++;
3252 	}
3253 
3254 	codec->patch_ops = via_patch_ops;
3255 
3256 	codec->patch_ops.init = via_auto_init;
3257 	codec->patch_ops.unsol_event = via_unsol_event;
3258 #ifdef CONFIG_SND_HDA_POWER_SAVE
3259 	spec->loopback.amplist = vt1709_loopbacks;
3260 #endif
3261 	return 0;
3262 }
3263 
3264 /* capture mixer elements */
3265 static struct snd_kcontrol_new vt1708B_capture_mixer[] = {
3266 	HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
3267 	HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
3268 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
3269 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
3270 	{
3271 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3272 		/* The multiple "Capture Source" controls confuse alsamixer
3273 		 * So call somewhat different..
3274 		 */
3275 		/* .name = "Capture Source", */
3276 		.name = "Input Source",
3277 		.count = 1,
3278 		.info = via_mux_enum_info,
3279 		.get = via_mux_enum_get,
3280 		.put = via_mux_enum_put,
3281 	},
3282 	{ } /* end */
3283 };
3284 /*
3285  * generic initialization of ADC, input mixers and output mixers
3286  */
3287 static struct hda_verb vt1708B_8ch_volume_init_verbs[] = {
3288 	/*
3289 	 * Unmute ADC0-1 and set the default input to mic-in
3290 	 */
3291 	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3292 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3293 
3294 
3295 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3296 	 * mixer widget
3297 	 */
3298 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3299 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3300 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3301 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3302 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3303 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
3304 
3305 	/*
3306 	 * Set up output mixers
3307 	 */
3308 	/* set vol=0 to output mixers */
3309 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3310 	{0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3311 	{0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3312 
3313 	/* Setup default input to PW4 */
3314 	{0x1d, AC_VERB_SET_CONNECT_SEL, 0},
3315 	/* PW9 Output enable */
3316 	{0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3317 	/* PW10 Input enable */
3318 	{0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
3319 	{ }
3320 };
3321 
3322 static struct hda_verb vt1708B_4ch_volume_init_verbs[] = {
3323 	/*
3324 	 * Unmute ADC0-1 and set the default input to mic-in
3325 	 */
3326 	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3327 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3328 
3329 
3330 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3331 	 * mixer widget
3332 	 */
3333 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3334 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3335 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3336 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3337 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3338 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
3339 
3340 	/*
3341 	 * Set up output mixers
3342 	 */
3343 	/* set vol=0 to output mixers */
3344 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3345 	{0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3346 	{0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3347 
3348 	/* Setup default input of PW4 to MW0 */
3349 	{0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
3350 	/* PW9 Output enable */
3351 	{0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3352 	/* PW10 Input enable */
3353 	{0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
3354 	{ }
3355 };
3356 
3357 static struct hda_verb vt1708B_uniwill_init_verbs[] = {
3358 	{0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
3359 	 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3360 	{0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3361 	{0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3362 	{0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3363 	{0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3364 	{0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3365 	{0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3366 	{0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3367 	{ }
3368 };
3369 
3370 static int via_pcm_open_close(struct hda_pcm_stream *hinfo,
3371 			      struct hda_codec *codec,
3372 			      struct snd_pcm_substream *substream)
3373 {
3374 	int idle = substream->pstr->substream_opened == 1
3375 		&& substream->ref_count == 0;
3376 
3377 	analog_low_current_mode(codec, idle);
3378 	return 0;
3379 }
3380 
3381 static struct hda_pcm_stream vt1708B_8ch_pcm_analog_playback = {
3382 	.substreams = 2,
3383 	.channels_min = 2,
3384 	.channels_max = 8,
3385 	.nid = 0x10, /* NID to query formats and rates */
3386 	.ops = {
3387 		.open = via_playback_pcm_open,
3388 		.prepare = via_playback_multi_pcm_prepare,
3389 		.cleanup = via_playback_multi_pcm_cleanup,
3390 		.close = via_pcm_open_close
3391 	},
3392 };
3393 
3394 static struct hda_pcm_stream vt1708B_4ch_pcm_analog_playback = {
3395 	.substreams = 2,
3396 	.channels_min = 2,
3397 	.channels_max = 4,
3398 	.nid = 0x10, /* NID to query formats and rates */
3399 	.ops = {
3400 		.open = via_playback_pcm_open,
3401 		.prepare = via_playback_multi_pcm_prepare,
3402 		.cleanup = via_playback_multi_pcm_cleanup
3403 	},
3404 };
3405 
3406 static struct hda_pcm_stream vt1708B_pcm_analog_capture = {
3407 	.substreams = 2,
3408 	.channels_min = 2,
3409 	.channels_max = 2,
3410 	.nid = 0x13, /* NID to query formats and rates */
3411 	.ops = {
3412 		.open = via_pcm_open_close,
3413 		.prepare = via_capture_pcm_prepare,
3414 		.cleanup = via_capture_pcm_cleanup,
3415 		.close = via_pcm_open_close
3416 	},
3417 };
3418 
3419 static struct hda_pcm_stream vt1708B_pcm_digital_playback = {
3420 	.substreams = 1,
3421 	.channels_min = 2,
3422 	.channels_max = 2,
3423 	/* NID is set in via_build_pcms */
3424 	.ops = {
3425 		.open = via_dig_playback_pcm_open,
3426 		.close = via_dig_playback_pcm_close,
3427 		.prepare = via_dig_playback_pcm_prepare,
3428 		.cleanup = via_dig_playback_pcm_cleanup
3429 	},
3430 };
3431 
3432 static struct hda_pcm_stream vt1708B_pcm_digital_capture = {
3433 	.substreams = 1,
3434 	.channels_min = 2,
3435 	.channels_max = 2,
3436 };
3437 
3438 /* fill in the dac_nids table from the parsed pin configuration */
3439 static int vt1708B_auto_fill_dac_nids(struct via_spec *spec,
3440 				     const struct auto_pin_cfg *cfg)
3441 {
3442 	int i;
3443 	hda_nid_t nid;
3444 
3445 	spec->multiout.num_dacs = cfg->line_outs;
3446 
3447 	spec->multiout.dac_nids = spec->private_dac_nids;
3448 
3449 	for (i = 0; i < 4; i++) {
3450 		nid = cfg->line_out_pins[i];
3451 		if (nid) {
3452 			/* config dac list */
3453 			switch (i) {
3454 			case AUTO_SEQ_FRONT:
3455 				spec->multiout.dac_nids[i] = 0x10;
3456 				break;
3457 			case AUTO_SEQ_CENLFE:
3458 				spec->multiout.dac_nids[i] = 0x24;
3459 				break;
3460 			case AUTO_SEQ_SURROUND:
3461 				spec->multiout.dac_nids[i] = 0x11;
3462 				break;
3463 			case AUTO_SEQ_SIDE:
3464 				spec->multiout.dac_nids[i] = 0x25;
3465 				break;
3466 			}
3467 		}
3468 	}
3469 
3470 	return 0;
3471 }
3472 
3473 /* add playback controls from the parsed DAC table */
3474 static int vt1708B_auto_create_multi_out_ctls(struct via_spec *spec,
3475 					     const struct auto_pin_cfg *cfg)
3476 {
3477 	char name[32];
3478 	static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
3479 	hda_nid_t nid_vols[] = {0x16, 0x18, 0x26, 0x27};
3480 	hda_nid_t nid, nid_vol = 0;
3481 	int i, err;
3482 
3483 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
3484 		nid = cfg->line_out_pins[i];
3485 
3486 		if (!nid)
3487 			continue;
3488 
3489 		nid_vol = nid_vols[i];
3490 
3491 		if (i == AUTO_SEQ_CENLFE) {
3492 			/* Center/LFE */
3493 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3494 					      "Center Playback Volume",
3495 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
3496 								  HDA_OUTPUT));
3497 			if (err < 0)
3498 				return err;
3499 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3500 					      "LFE Playback Volume",
3501 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
3502 								  HDA_OUTPUT));
3503 			if (err < 0)
3504 				return err;
3505 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3506 					      "Center Playback Switch",
3507 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
3508 								  HDA_OUTPUT));
3509 			if (err < 0)
3510 				return err;
3511 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3512 					      "LFE Playback Switch",
3513 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
3514 								  HDA_OUTPUT));
3515 			if (err < 0)
3516 				return err;
3517 		} else if (i == AUTO_SEQ_FRONT) {
3518 			/* add control to mixer index 0 */
3519 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3520 					      "Master Front Playback Volume",
3521 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3522 								  HDA_INPUT));
3523 			if (err < 0)
3524 				return err;
3525 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3526 					      "Master Front Playback Switch",
3527 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3528 								  HDA_INPUT));
3529 			if (err < 0)
3530 				return err;
3531 
3532 			/* add control to PW3 */
3533 			sprintf(name, "%s Playback Volume", chname[i]);
3534 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3535 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
3536 								  HDA_OUTPUT));
3537 			if (err < 0)
3538 				return err;
3539 			sprintf(name, "%s Playback Switch", chname[i]);
3540 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3541 					      HDA_COMPOSE_AMP_VAL(nid, 3, 0,
3542 								  HDA_OUTPUT));
3543 			if (err < 0)
3544 				return err;
3545 		} else {
3546 			sprintf(name, "%s Playback Volume", chname[i]);
3547 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3548 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3549 								  HDA_OUTPUT));
3550 			if (err < 0)
3551 				return err;
3552 			sprintf(name, "%s Playback Switch", chname[i]);
3553 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3554 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3555 								  HDA_OUTPUT));
3556 			if (err < 0)
3557 				return err;
3558 		}
3559 	}
3560 
3561 	return 0;
3562 }
3563 
3564 static int vt1708B_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
3565 {
3566 	int err;
3567 
3568 	if (!pin)
3569 		return 0;
3570 
3571 	spec->multiout.hp_nid = VT1708B_HP_NID; /* AOW3 */
3572 	spec->hp_independent_mode_index = 1;
3573 
3574 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3575 			      "Headphone Playback Volume",
3576 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3577 	if (err < 0)
3578 		return err;
3579 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3580 			      "Headphone Playback Switch",
3581 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3582 	if (err < 0)
3583 		return err;
3584 
3585 	create_hp_imux(spec);
3586 
3587 	return 0;
3588 }
3589 
3590 /* create playback/capture controls for input pins */
3591 static int vt1708B_auto_create_analog_input_ctls(struct via_spec *spec,
3592 						const struct auto_pin_cfg *cfg)
3593 {
3594 	static char *labels[] = {
3595 		"Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
3596 	};
3597 	struct hda_input_mux *imux = &spec->private_imux[0];
3598 	int i, err, idx = 0;
3599 
3600 	/* for internal loopback recording select */
3601 	imux->items[imux->num_items].label = "Stereo Mixer";
3602 	imux->items[imux->num_items].index = idx;
3603 	imux->num_items++;
3604 
3605 	for (i = 0; i < AUTO_PIN_LAST; i++) {
3606 		if (!cfg->input_pins[i])
3607 			continue;
3608 
3609 		switch (cfg->input_pins[i]) {
3610 		case 0x1a: /* Mic */
3611 			idx = 2;
3612 			break;
3613 
3614 		case 0x1b: /* Line In */
3615 			idx = 3;
3616 			break;
3617 
3618 		case 0x1e: /* Front Mic */
3619 			idx = 4;
3620 			break;
3621 
3622 		case 0x1f: /* CD */
3623 			idx = 1;
3624 			break;
3625 		}
3626 		err = via_new_analog_input(spec, labels[i], idx, 0x16);
3627 		if (err < 0)
3628 			return err;
3629 		imux->items[imux->num_items].label = labels[i];
3630 		imux->items[imux->num_items].index = idx;
3631 		imux->num_items++;
3632 	}
3633 	return 0;
3634 }
3635 
3636 static int vt1708B_parse_auto_config(struct hda_codec *codec)
3637 {
3638 	struct via_spec *spec = codec->spec;
3639 	int err;
3640 
3641 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3642 	if (err < 0)
3643 		return err;
3644 	err = vt1708B_auto_fill_dac_nids(spec, &spec->autocfg);
3645 	if (err < 0)
3646 		return err;
3647 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3648 		return 0; /* can't find valid BIOS pin config */
3649 
3650 	err = vt1708B_auto_create_multi_out_ctls(spec, &spec->autocfg);
3651 	if (err < 0)
3652 		return err;
3653 	err = vt1708B_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
3654 	if (err < 0)
3655 		return err;
3656 	err = vt1708B_auto_create_analog_input_ctls(spec, &spec->autocfg);
3657 	if (err < 0)
3658 		return err;
3659 
3660 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3661 
3662 	if (spec->autocfg.dig_outs)
3663 		spec->multiout.dig_out_nid = VT1708B_DIGOUT_NID;
3664 	spec->dig_in_pin = VT1708B_DIGIN_PIN;
3665 	if (spec->autocfg.dig_in_pin)
3666 		spec->dig_in_nid = VT1708B_DIGIN_NID;
3667 
3668 	if (spec->kctls.list)
3669 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
3670 
3671 	spec->input_mux = &spec->private_imux[0];
3672 
3673 	if (spec->hp_mux)
3674 		via_hp_build(codec);
3675 
3676 	via_smart51_build(spec);
3677 	return 1;
3678 }
3679 
3680 #ifdef CONFIG_SND_HDA_POWER_SAVE
3681 static struct hda_amp_list vt1708B_loopbacks[] = {
3682 	{ 0x16, HDA_INPUT, 1 },
3683 	{ 0x16, HDA_INPUT, 2 },
3684 	{ 0x16, HDA_INPUT, 3 },
3685 	{ 0x16, HDA_INPUT, 4 },
3686 	{ } /* end */
3687 };
3688 #endif
3689 static int patch_vt1708S(struct hda_codec *codec);
3690 static int patch_vt1708B_8ch(struct hda_codec *codec)
3691 {
3692 	struct via_spec *spec;
3693 	int err;
3694 
3695 	if (get_codec_type(codec) == VT1708BCE)
3696 		return patch_vt1708S(codec);
3697 	/* create a codec specific record */
3698 	spec = via_new_spec(codec);
3699 	if (spec == NULL)
3700 		return -ENOMEM;
3701 
3702 	/* automatic parse from the BIOS config */
3703 	err = vt1708B_parse_auto_config(codec);
3704 	if (err < 0) {
3705 		via_free(codec);
3706 		return err;
3707 	} else if (!err) {
3708 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
3709 		       "from BIOS.  Using genenic mode...\n");
3710 	}
3711 
3712 	spec->init_verbs[spec->num_iverbs++] = vt1708B_8ch_volume_init_verbs;
3713 	spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
3714 
3715 	spec->stream_name_analog = "VT1708B Analog";
3716 	spec->stream_analog_playback = &vt1708B_8ch_pcm_analog_playback;
3717 	spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
3718 
3719 	spec->stream_name_digital = "VT1708B Digital";
3720 	spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
3721 	spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
3722 
3723 	if (!spec->adc_nids && spec->input_mux) {
3724 		spec->adc_nids = vt1708B_adc_nids;
3725 		spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
3726 		get_mux_nids(codec);
3727 		spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
3728 		spec->num_mixers++;
3729 	}
3730 
3731 	codec->patch_ops = via_patch_ops;
3732 
3733 	codec->patch_ops.init = via_auto_init;
3734 	codec->patch_ops.unsol_event = via_unsol_event;
3735 #ifdef CONFIG_SND_HDA_POWER_SAVE
3736 	spec->loopback.amplist = vt1708B_loopbacks;
3737 #endif
3738 
3739 	return 0;
3740 }
3741 
3742 static int patch_vt1708B_4ch(struct hda_codec *codec)
3743 {
3744 	struct via_spec *spec;
3745 	int err;
3746 
3747 	/* create a codec specific record */
3748 	spec = via_new_spec(codec);
3749 	if (spec == NULL)
3750 		return -ENOMEM;
3751 
3752 	/* automatic parse from the BIOS config */
3753 	err = vt1708B_parse_auto_config(codec);
3754 	if (err < 0) {
3755 		via_free(codec);
3756 		return err;
3757 	} else if (!err) {
3758 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
3759 		       "from BIOS.  Using genenic mode...\n");
3760 	}
3761 
3762 	spec->init_verbs[spec->num_iverbs++] = vt1708B_4ch_volume_init_verbs;
3763 	spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
3764 
3765 	spec->stream_name_analog = "VT1708B Analog";
3766 	spec->stream_analog_playback = &vt1708B_4ch_pcm_analog_playback;
3767 	spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
3768 
3769 	spec->stream_name_digital = "VT1708B Digital";
3770 	spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
3771 	spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
3772 
3773 	if (!spec->adc_nids && spec->input_mux) {
3774 		spec->adc_nids = vt1708B_adc_nids;
3775 		spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
3776 		get_mux_nids(codec);
3777 		spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
3778 		spec->num_mixers++;
3779 	}
3780 
3781 	codec->patch_ops = via_patch_ops;
3782 
3783 	codec->patch_ops.init = via_auto_init;
3784 	codec->patch_ops.unsol_event = via_unsol_event;
3785 #ifdef CONFIG_SND_HDA_POWER_SAVE
3786 	spec->loopback.amplist = vt1708B_loopbacks;
3787 #endif
3788 
3789 	return 0;
3790 }
3791 
3792 /* Patch for VT1708S */
3793 
3794 /* capture mixer elements */
3795 static struct snd_kcontrol_new vt1708S_capture_mixer[] = {
3796 	HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
3797 	HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
3798 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
3799 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
3800 	HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x1A, 0x0, HDA_INPUT),
3801 	HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x1E, 0x0,
3802 			 HDA_INPUT),
3803 	{
3804 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3805 		/* The multiple "Capture Source" controls confuse alsamixer
3806 		 * So call somewhat different..
3807 		 */
3808 		/* .name = "Capture Source", */
3809 		.name = "Input Source",
3810 		.count = 1,
3811 		.info = via_mux_enum_info,
3812 		.get = via_mux_enum_get,
3813 		.put = via_mux_enum_put,
3814 	},
3815 	{ } /* end */
3816 };
3817 
3818 static struct hda_verb vt1708S_volume_init_verbs[] = {
3819 	/* Unmute ADC0-1 and set the default input to mic-in */
3820 	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3821 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3822 
3823 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the
3824 	 * analog-loopback mixer widget */
3825 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3826 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3827 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3828 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3829 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3830 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
3831 
3832 	/* Setup default input of PW4 to MW0 */
3833 	{0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
3834 	/* PW9, PW10  Output enable */
3835 	{0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3836 	{0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3837 	/* Enable Mic Boost Volume backdoor */
3838 	{0x1, 0xf98, 0x1},
3839 	/* don't bybass mixer */
3840 	{0x1, 0xf88, 0xc0},
3841 	{ }
3842 };
3843 
3844 static struct hda_verb vt1708S_uniwill_init_verbs[] = {
3845 	{0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
3846 	 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3847 	{0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3848 	{0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3849 	{0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3850 	{0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3851 	{0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3852 	{0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3853 	{0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3854 	{ }
3855 };
3856 
3857 static struct hda_pcm_stream vt1708S_pcm_analog_playback = {
3858 	.substreams = 2,
3859 	.channels_min = 2,
3860 	.channels_max = 8,
3861 	.nid = 0x10, /* NID to query formats and rates */
3862 	.ops = {
3863 		.open = via_playback_pcm_open,
3864 		.prepare = via_playback_multi_pcm_prepare,
3865 		.cleanup = via_playback_multi_pcm_cleanup,
3866 		.close = via_pcm_open_close
3867 	},
3868 };
3869 
3870 static struct hda_pcm_stream vt1708S_pcm_analog_capture = {
3871 	.substreams = 2,
3872 	.channels_min = 2,
3873 	.channels_max = 2,
3874 	.nid = 0x13, /* NID to query formats and rates */
3875 	.ops = {
3876 		.open = via_pcm_open_close,
3877 		.prepare = via_capture_pcm_prepare,
3878 		.cleanup = via_capture_pcm_cleanup,
3879 		.close = via_pcm_open_close
3880 	},
3881 };
3882 
3883 static struct hda_pcm_stream vt1708S_pcm_digital_playback = {
3884 	.substreams = 1,
3885 	.channels_min = 2,
3886 	.channels_max = 2,
3887 	/* NID is set in via_build_pcms */
3888 	.ops = {
3889 		.open = via_dig_playback_pcm_open,
3890 		.close = via_dig_playback_pcm_close,
3891 		.prepare = via_dig_playback_pcm_prepare,
3892 		.cleanup = via_dig_playback_pcm_cleanup
3893 	},
3894 };
3895 
3896 /* fill in the dac_nids table from the parsed pin configuration */
3897 static int vt1708S_auto_fill_dac_nids(struct via_spec *spec,
3898 				     const struct auto_pin_cfg *cfg)
3899 {
3900 	int i;
3901 	hda_nid_t nid;
3902 
3903 	spec->multiout.num_dacs = cfg->line_outs;
3904 
3905 	spec->multiout.dac_nids = spec->private_dac_nids;
3906 
3907 	for (i = 0; i < 4; i++) {
3908 		nid = cfg->line_out_pins[i];
3909 		if (nid) {
3910 			/* config dac list */
3911 			switch (i) {
3912 			case AUTO_SEQ_FRONT:
3913 				spec->multiout.dac_nids[i] = 0x10;
3914 				break;
3915 			case AUTO_SEQ_CENLFE:
3916 				spec->multiout.dac_nids[i] = 0x24;
3917 				break;
3918 			case AUTO_SEQ_SURROUND:
3919 				spec->multiout.dac_nids[i] = 0x11;
3920 				break;
3921 			case AUTO_SEQ_SIDE:
3922 				spec->multiout.dac_nids[i] = 0x25;
3923 				break;
3924 			}
3925 		}
3926 	}
3927 
3928 	/* for Smart 5.1, line/mic inputs double as output pins */
3929 	if (cfg->line_outs == 1) {
3930 		spec->multiout.num_dacs = 3;
3931 		spec->multiout.dac_nids[AUTO_SEQ_SURROUND] = 0x11;
3932 		spec->multiout.dac_nids[AUTO_SEQ_CENLFE] = 0x24;
3933 	}
3934 
3935 	return 0;
3936 }
3937 
3938 /* add playback controls from the parsed DAC table */
3939 static int vt1708S_auto_create_multi_out_ctls(struct via_spec *spec,
3940 					     const struct auto_pin_cfg *cfg)
3941 {
3942 	char name[32];
3943 	static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
3944 	hda_nid_t nid_vols[] = {0x10, 0x11, 0x24, 0x25};
3945 	hda_nid_t nid_mutes[] = {0x1C, 0x18, 0x26, 0x27};
3946 	hda_nid_t nid, nid_vol, nid_mute;
3947 	int i, err;
3948 
3949 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
3950 		nid = cfg->line_out_pins[i];
3951 
3952 		/* for Smart 5.1, there are always at least six channels */
3953 		if (!nid && i > AUTO_SEQ_CENLFE)
3954 			continue;
3955 
3956 		nid_vol = nid_vols[i];
3957 		nid_mute = nid_mutes[i];
3958 
3959 		if (i == AUTO_SEQ_CENLFE) {
3960 			/* Center/LFE */
3961 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3962 					      "Center Playback Volume",
3963 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
3964 								  HDA_OUTPUT));
3965 			if (err < 0)
3966 				return err;
3967 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3968 					      "LFE Playback Volume",
3969 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
3970 								  HDA_OUTPUT));
3971 			if (err < 0)
3972 				return err;
3973 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3974 					      "Center Playback Switch",
3975 					      HDA_COMPOSE_AMP_VAL(nid_mute,
3976 								  1, 0,
3977 								  HDA_OUTPUT));
3978 			if (err < 0)
3979 				return err;
3980 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3981 					      "LFE Playback Switch",
3982 					      HDA_COMPOSE_AMP_VAL(nid_mute,
3983 								  2, 0,
3984 								  HDA_OUTPUT));
3985 			if (err < 0)
3986 				return err;
3987 		} else if (i == AUTO_SEQ_FRONT) {
3988 			/* add control to mixer index 0 */
3989 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3990 					      "Master Front Playback Volume",
3991 					      HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
3992 								  HDA_INPUT));
3993 			if (err < 0)
3994 				return err;
3995 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3996 					      "Master Front Playback Switch",
3997 					      HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
3998 								  HDA_INPUT));
3999 			if (err < 0)
4000 				return err;
4001 
4002 			/* Front */
4003 			sprintf(name, "%s Playback Volume", chname[i]);
4004 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
4005 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
4006 								  HDA_OUTPUT));
4007 			if (err < 0)
4008 				return err;
4009 			sprintf(name, "%s Playback Switch", chname[i]);
4010 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
4011 					      HDA_COMPOSE_AMP_VAL(nid_mute,
4012 								  3, 0,
4013 								  HDA_OUTPUT));
4014 			if (err < 0)
4015 				return err;
4016 		} else {
4017 			sprintf(name, "%s Playback Volume", chname[i]);
4018 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
4019 					      HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
4020 								  HDA_OUTPUT));
4021 			if (err < 0)
4022 				return err;
4023 			sprintf(name, "%s Playback Switch", chname[i]);
4024 			err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
4025 					      HDA_COMPOSE_AMP_VAL(nid_mute,
4026 								  3, 0,
4027 								  HDA_OUTPUT));
4028 			if (err < 0)
4029 				return err;
4030 		}
4031 	}
4032 
4033 	return 0;
4034 }
4035 
4036 static int vt1708S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
4037 {
4038 	int err;
4039 
4040 	if (!pin)
4041 		return 0;
4042 
4043 	spec->multiout.hp_nid = VT1708S_HP_NID; /* AOW3 */
4044 	spec->hp_independent_mode_index = 1;
4045 
4046 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4047 			      "Headphone Playback Volume",
4048 			      HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
4049 	if (err < 0)
4050 		return err;
4051 
4052 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4053 			      "Headphone Playback Switch",
4054 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
4055 	if (err < 0)
4056 		return err;
4057 
4058 	create_hp_imux(spec);
4059 
4060 	return 0;
4061 }
4062 
4063 /* create playback/capture controls for input pins */
4064 static int vt1708S_auto_create_analog_input_ctls(struct via_spec *spec,
4065 						const struct auto_pin_cfg *cfg)
4066 {
4067 	static char *labels[] = {
4068 		"Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
4069 	};
4070 	struct hda_input_mux *imux = &spec->private_imux[0];
4071 	int i, err, idx = 0;
4072 
4073 	/* for internal loopback recording select */
4074 	imux->items[imux->num_items].label = "Stereo Mixer";
4075 	imux->items[imux->num_items].index = 5;
4076 	imux->num_items++;
4077 
4078 	for (i = 0; i < AUTO_PIN_LAST; i++) {
4079 		if (!cfg->input_pins[i])
4080 			continue;
4081 
4082 		switch (cfg->input_pins[i]) {
4083 		case 0x1a: /* Mic */
4084 			idx = 2;
4085 			break;
4086 
4087 		case 0x1b: /* Line In */
4088 			idx = 3;
4089 			break;
4090 
4091 		case 0x1e: /* Front Mic */
4092 			idx = 4;
4093 			break;
4094 
4095 		case 0x1f: /* CD */
4096 			idx = 1;
4097 			break;
4098 		}
4099 		err = via_new_analog_input(spec, labels[i], idx, 0x16);
4100 		if (err < 0)
4101 			return err;
4102 		imux->items[imux->num_items].label = labels[i];
4103 		imux->items[imux->num_items].index = idx-1;
4104 		imux->num_items++;
4105 	}
4106 	return 0;
4107 }
4108 
4109 /* fill out digital output widgets; one for master and one for slave outputs */
4110 static void fill_dig_outs(struct hda_codec *codec)
4111 {
4112 	struct via_spec *spec = codec->spec;
4113 	int i;
4114 
4115 	for (i = 0; i < spec->autocfg.dig_outs; i++) {
4116 		hda_nid_t nid;
4117 		int conn;
4118 
4119 		nid = spec->autocfg.dig_out_pins[i];
4120 		if (!nid)
4121 			continue;
4122 		conn = snd_hda_get_connections(codec, nid, &nid, 1);
4123 		if (conn < 1)
4124 			continue;
4125 		if (!spec->multiout.dig_out_nid)
4126 			spec->multiout.dig_out_nid = nid;
4127 		else {
4128 			spec->slave_dig_outs[0] = nid;
4129 			break; /* at most two dig outs */
4130 		}
4131 	}
4132 }
4133 
4134 static int vt1708S_parse_auto_config(struct hda_codec *codec)
4135 {
4136 	struct via_spec *spec = codec->spec;
4137 	int err;
4138 
4139 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4140 	if (err < 0)
4141 		return err;
4142 	err = vt1708S_auto_fill_dac_nids(spec, &spec->autocfg);
4143 	if (err < 0)
4144 		return err;
4145 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
4146 		return 0; /* can't find valid BIOS pin config */
4147 
4148 	err = vt1708S_auto_create_multi_out_ctls(spec, &spec->autocfg);
4149 	if (err < 0)
4150 		return err;
4151 	err = vt1708S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
4152 	if (err < 0)
4153 		return err;
4154 	err = vt1708S_auto_create_analog_input_ctls(spec, &spec->autocfg);
4155 	if (err < 0)
4156 		return err;
4157 
4158 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4159 
4160 	fill_dig_outs(codec);
4161 
4162 	if (spec->kctls.list)
4163 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
4164 
4165 	spec->input_mux = &spec->private_imux[0];
4166 
4167 	if (spec->hp_mux)
4168 		via_hp_build(codec);
4169 
4170 	via_smart51_build(spec);
4171 	return 1;
4172 }
4173 
4174 #ifdef CONFIG_SND_HDA_POWER_SAVE
4175 static struct hda_amp_list vt1708S_loopbacks[] = {
4176 	{ 0x16, HDA_INPUT, 1 },
4177 	{ 0x16, HDA_INPUT, 2 },
4178 	{ 0x16, HDA_INPUT, 3 },
4179 	{ 0x16, HDA_INPUT, 4 },
4180 	{ } /* end */
4181 };
4182 #endif
4183 
4184 static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
4185 			       int offset, int num_steps, int step_size)
4186 {
4187 	snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
4188 				  (offset << AC_AMPCAP_OFFSET_SHIFT) |
4189 				  (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
4190 				  (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
4191 				  (0 << AC_AMPCAP_MUTE_SHIFT));
4192 }
4193 
4194 static int patch_vt1708S(struct hda_codec *codec)
4195 {
4196 	struct via_spec *spec;
4197 	int err;
4198 
4199 	/* create a codec specific record */
4200 	spec = via_new_spec(codec);
4201 	if (spec == NULL)
4202 		return -ENOMEM;
4203 
4204 	/* automatic parse from the BIOS config */
4205 	err = vt1708S_parse_auto_config(codec);
4206 	if (err < 0) {
4207 		via_free(codec);
4208 		return err;
4209 	} else if (!err) {
4210 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
4211 		       "from BIOS.  Using genenic mode...\n");
4212 	}
4213 
4214 	spec->init_verbs[spec->num_iverbs++] = vt1708S_volume_init_verbs;
4215 	spec->init_verbs[spec->num_iverbs++] = vt1708S_uniwill_init_verbs;
4216 
4217 	if (codec->vendor_id == 0x11060440)
4218 		spec->stream_name_analog = "VT1818S Analog";
4219 	else
4220 		spec->stream_name_analog = "VT1708S Analog";
4221 	spec->stream_analog_playback = &vt1708S_pcm_analog_playback;
4222 	spec->stream_analog_capture = &vt1708S_pcm_analog_capture;
4223 
4224 	if (codec->vendor_id == 0x11060440)
4225 		spec->stream_name_digital = "VT1818S Digital";
4226 	else
4227 		spec->stream_name_digital = "VT1708S Digital";
4228 	spec->stream_digital_playback = &vt1708S_pcm_digital_playback;
4229 
4230 	if (!spec->adc_nids && spec->input_mux) {
4231 		spec->adc_nids = vt1708S_adc_nids;
4232 		spec->num_adc_nids = ARRAY_SIZE(vt1708S_adc_nids);
4233 		get_mux_nids(codec);
4234 		override_mic_boost(codec, 0x1a, 0, 3, 40);
4235 		override_mic_boost(codec, 0x1e, 0, 3, 40);
4236 		spec->mixers[spec->num_mixers] = vt1708S_capture_mixer;
4237 		spec->num_mixers++;
4238 	}
4239 
4240 	codec->patch_ops = via_patch_ops;
4241 
4242 	codec->patch_ops.init = via_auto_init;
4243 	codec->patch_ops.unsol_event = via_unsol_event;
4244 #ifdef CONFIG_SND_HDA_POWER_SAVE
4245 	spec->loopback.amplist = vt1708S_loopbacks;
4246 #endif
4247 
4248 	/* correct names for VT1708BCE */
4249 	if (get_codec_type(codec) == VT1708BCE)	{
4250 		kfree(codec->chip_name);
4251 		codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
4252 		snprintf(codec->bus->card->mixername,
4253 			 sizeof(codec->bus->card->mixername),
4254 			 "%s %s", codec->vendor_name, codec->chip_name);
4255 		spec->stream_name_analog = "VT1708BCE Analog";
4256 		spec->stream_name_digital = "VT1708BCE Digital";
4257 	}
4258 	return 0;
4259 }
4260 
4261 /* Patch for VT1702 */
4262 
4263 /* capture mixer elements */
4264 static struct snd_kcontrol_new vt1702_capture_mixer[] = {
4265 	HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_INPUT),
4266 	HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_INPUT),
4267 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x20, 0x0, HDA_INPUT),
4268 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x20, 0x0, HDA_INPUT),
4269 	HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x1F, 0x0, HDA_INPUT),
4270 	HDA_CODEC_MUTE("Digital Mic Capture Switch", 0x1F, 0x0, HDA_INPUT),
4271 	HDA_CODEC_VOLUME("Digital Mic Boost Capture Volume", 0x1E, 0x0,
4272 			 HDA_INPUT),
4273 	{
4274 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4275 		/* The multiple "Capture Source" controls confuse alsamixer
4276 		 * So call somewhat different..
4277 		 */
4278 		/* .name = "Capture Source", */
4279 		.name = "Input Source",
4280 		.count = 1,
4281 		.info = via_mux_enum_info,
4282 		.get = via_mux_enum_get,
4283 		.put = via_mux_enum_put,
4284 	},
4285 	{ } /* end */
4286 };
4287 
4288 static struct hda_verb vt1702_volume_init_verbs[] = {
4289 	/*
4290 	 * Unmute ADC0-1 and set the default input to mic-in
4291 	 */
4292 	{0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4293 	{0x1F, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4294 	{0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4295 
4296 
4297 	/* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
4298 	 * mixer widget
4299 	 */
4300 	/* Amp Indices: Mic1 = 1, Line = 1, Mic2 = 3 */
4301 	{0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4302 	{0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4303 	{0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
4304 	{0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
4305 	{0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
4306 
4307 	/* Setup default input of PW4 to MW0 */
4308 	{0x17, AC_VERB_SET_CONNECT_SEL, 0x1},
4309 	/* PW6 PW7 Output enable */
4310 	{0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
4311 	{0x1C, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
4312 	/* mixer enable */
4313 	{0x1, 0xF88, 0x3},
4314 	/* GPIO 0~2 */
4315 	{0x1, 0xF82, 0x3F},
4316 	{ }
4317 };
4318 
4319 static struct hda_verb vt1702_uniwill_init_verbs[] = {
4320 	{0x17, AC_VERB_SET_UNSOLICITED_ENABLE,
4321 	 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
4322 	{0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4323 	{0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4324 	{0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4325 	{0x18, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4326 	{ }
4327 };
4328 
4329 static struct hda_pcm_stream vt1702_pcm_analog_playback = {
4330 	.substreams = 2,
4331 	.channels_min = 2,
4332 	.channels_max = 2,
4333 	.nid = 0x10, /* NID to query formats and rates */
4334 	.ops = {
4335 		.open = via_playback_pcm_open,
4336 		.prepare = via_playback_multi_pcm_prepare,
4337 		.cleanup = via_playback_multi_pcm_cleanup,
4338 		.close = via_pcm_open_close
4339 	},
4340 };
4341 
4342 static struct hda_pcm_stream vt1702_pcm_analog_capture = {
4343 	.substreams = 3,
4344 	.channels_min = 2,
4345 	.channels_max = 2,
4346 	.nid = 0x12, /* NID to query formats and rates */
4347 	.ops = {
4348 		.open = via_pcm_open_close,
4349 		.prepare = via_capture_pcm_prepare,
4350 		.cleanup = via_capture_pcm_cleanup,
4351 		.close = via_pcm_open_close
4352 	},
4353 };
4354 
4355 static struct hda_pcm_stream vt1702_pcm_digital_playback = {
4356 	.substreams = 2,
4357 	.channels_min = 2,
4358 	.channels_max = 2,
4359 	/* NID is set in via_build_pcms */
4360 	.ops = {
4361 		.open = via_dig_playback_pcm_open,
4362 		.close = via_dig_playback_pcm_close,
4363 		.prepare = via_dig_playback_pcm_prepare,
4364 		.cleanup = via_dig_playback_pcm_cleanup
4365 	},
4366 };
4367 
4368 /* fill in the dac_nids table from the parsed pin configuration */
4369 static int vt1702_auto_fill_dac_nids(struct via_spec *spec,
4370 				     const struct auto_pin_cfg *cfg)
4371 {
4372 	spec->multiout.num_dacs = 1;
4373 	spec->multiout.dac_nids = spec->private_dac_nids;
4374 
4375 	if (cfg->line_out_pins[0]) {
4376 		/* config dac list */
4377 		spec->multiout.dac_nids[0] = 0x10;
4378 	}
4379 
4380 	return 0;
4381 }
4382 
4383 /* add playback controls from the parsed DAC table */
4384 static int vt1702_auto_create_line_out_ctls(struct via_spec *spec,
4385 					     const struct auto_pin_cfg *cfg)
4386 {
4387 	int err;
4388 
4389 	if (!cfg->line_out_pins[0])
4390 		return -1;
4391 
4392 	/* add control to mixer index 0 */
4393 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4394 			      "Master Front Playback Volume",
4395 			      HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
4396 	if (err < 0)
4397 		return err;
4398 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4399 			      "Master Front Playback Switch",
4400 			      HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
4401 	if (err < 0)
4402 		return err;
4403 
4404 	/* Front */
4405 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4406 			      "Front Playback Volume",
4407 			      HDA_COMPOSE_AMP_VAL(0x10, 3, 0, HDA_OUTPUT));
4408 	if (err < 0)
4409 		return err;
4410 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4411 			      "Front Playback Switch",
4412 			      HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_OUTPUT));
4413 	if (err < 0)
4414 		return err;
4415 
4416 	return 0;
4417 }
4418 
4419 static int vt1702_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
4420 {
4421 	int err, i;
4422 	struct hda_input_mux *imux;
4423 	static const char *texts[] = { "ON", "OFF", NULL};
4424 	if (!pin)
4425 		return 0;
4426 	spec->multiout.hp_nid = 0x1D;
4427 	spec->hp_independent_mode_index = 0;
4428 
4429 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4430 			      "Headphone Playback Volume",
4431 			      HDA_COMPOSE_AMP_VAL(0x1D, 3, 0, HDA_OUTPUT));
4432 	if (err < 0)
4433 		return err;
4434 
4435 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4436 			      "Headphone Playback Switch",
4437 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
4438 	if (err < 0)
4439 		return err;
4440 
4441 	imux = &spec->private_imux[1];
4442 
4443 	/* for hp mode select */
4444 	i = 0;
4445 	while (texts[i] != NULL)	{
4446 		imux->items[imux->num_items].label =  texts[i];
4447 		imux->items[imux->num_items].index = i;
4448 		imux->num_items++;
4449 		i++;
4450 	}
4451 
4452 	spec->hp_mux = &spec->private_imux[1];
4453 	return 0;
4454 }
4455 
4456 /* create playback/capture controls for input pins */
4457 static int vt1702_auto_create_analog_input_ctls(struct via_spec *spec,
4458 						const struct auto_pin_cfg *cfg)
4459 {
4460 	static char *labels[] = {
4461 		"Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
4462 	};
4463 	struct hda_input_mux *imux = &spec->private_imux[0];
4464 	int i, err, idx = 0;
4465 
4466 	/* for internal loopback recording select */
4467 	imux->items[imux->num_items].label = "Stereo Mixer";
4468 	imux->items[imux->num_items].index = 3;
4469 	imux->num_items++;
4470 
4471 	for (i = 0; i < AUTO_PIN_LAST; i++) {
4472 		if (!cfg->input_pins[i])
4473 			continue;
4474 
4475 		switch (cfg->input_pins[i]) {
4476 		case 0x14: /* Mic */
4477 			idx = 1;
4478 			break;
4479 
4480 		case 0x15: /* Line In */
4481 			idx = 2;
4482 			break;
4483 
4484 		case 0x18: /* Front Mic */
4485 			idx = 3;
4486 			break;
4487 		}
4488 		err = via_new_analog_input(spec, labels[i], idx, 0x1A);
4489 		if (err < 0)
4490 			return err;
4491 		imux->items[imux->num_items].label = labels[i];
4492 		imux->items[imux->num_items].index = idx-1;
4493 		imux->num_items++;
4494 	}
4495 	return 0;
4496 }
4497 
4498 static int vt1702_parse_auto_config(struct hda_codec *codec)
4499 {
4500 	struct via_spec *spec = codec->spec;
4501 	int err;
4502 
4503 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4504 	if (err < 0)
4505 		return err;
4506 	err = vt1702_auto_fill_dac_nids(spec, &spec->autocfg);
4507 	if (err < 0)
4508 		return err;
4509 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
4510 		return 0; /* can't find valid BIOS pin config */
4511 
4512 	err = vt1702_auto_create_line_out_ctls(spec, &spec->autocfg);
4513 	if (err < 0)
4514 		return err;
4515 	err = vt1702_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
4516 	if (err < 0)
4517 		return err;
4518 	/* limit AA path volume to 0 dB */
4519 	snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
4520 				  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
4521 				  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
4522 				  (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
4523 				  (1 << AC_AMPCAP_MUTE_SHIFT));
4524 	err = vt1702_auto_create_analog_input_ctls(spec, &spec->autocfg);
4525 	if (err < 0)
4526 		return err;
4527 
4528 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4529 
4530 	fill_dig_outs(codec);
4531 
4532 	if (spec->kctls.list)
4533 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
4534 
4535 	spec->input_mux = &spec->private_imux[0];
4536 
4537 	if (spec->hp_mux)
4538 		via_hp_build(codec);
4539 
4540 	return 1;
4541 }
4542 
4543 #ifdef CONFIG_SND_HDA_POWER_SAVE
4544 static struct hda_amp_list vt1702_loopbacks[] = {
4545 	{ 0x1A, HDA_INPUT, 1 },
4546 	{ 0x1A, HDA_INPUT, 2 },
4547 	{ 0x1A, HDA_INPUT, 3 },
4548 	{ 0x1A, HDA_INPUT, 4 },
4549 	{ } /* end */
4550 };
4551 #endif
4552 
4553 static int patch_vt1702(struct hda_codec *codec)
4554 {
4555 	struct via_spec *spec;
4556 	int err;
4557 
4558 	/* create a codec specific record */
4559 	spec = via_new_spec(codec);
4560 	if (spec == NULL)
4561 		return -ENOMEM;
4562 
4563 	/* automatic parse from the BIOS config */
4564 	err = vt1702_parse_auto_config(codec);
4565 	if (err < 0) {
4566 		via_free(codec);
4567 		return err;
4568 	} else if (!err) {
4569 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
4570 		       "from BIOS.  Using genenic mode...\n");
4571 	}
4572 
4573 	spec->init_verbs[spec->num_iverbs++] = vt1702_volume_init_verbs;
4574 	spec->init_verbs[spec->num_iverbs++] = vt1702_uniwill_init_verbs;
4575 
4576 	spec->stream_name_analog = "VT1702 Analog";
4577 	spec->stream_analog_playback = &vt1702_pcm_analog_playback;
4578 	spec->stream_analog_capture = &vt1702_pcm_analog_capture;
4579 
4580 	spec->stream_name_digital = "VT1702 Digital";
4581 	spec->stream_digital_playback = &vt1702_pcm_digital_playback;
4582 
4583 	if (!spec->adc_nids && spec->input_mux) {
4584 		spec->adc_nids = vt1702_adc_nids;
4585 		spec->num_adc_nids = ARRAY_SIZE(vt1702_adc_nids);
4586 		get_mux_nids(codec);
4587 		spec->mixers[spec->num_mixers] = vt1702_capture_mixer;
4588 		spec->num_mixers++;
4589 	}
4590 
4591 	codec->patch_ops = via_patch_ops;
4592 
4593 	codec->patch_ops.init = via_auto_init;
4594 	codec->patch_ops.unsol_event = via_unsol_event;
4595 #ifdef CONFIG_SND_HDA_POWER_SAVE
4596 	spec->loopback.amplist = vt1702_loopbacks;
4597 #endif
4598 
4599 	return 0;
4600 }
4601 
4602 /* Patch for VT1718S */
4603 
4604 /* capture mixer elements */
4605 static struct snd_kcontrol_new vt1718S_capture_mixer[] = {
4606 	HDA_CODEC_VOLUME("Capture Volume", 0x10, 0x0, HDA_INPUT),
4607 	HDA_CODEC_MUTE("Capture Switch", 0x10, 0x0, HDA_INPUT),
4608 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x11, 0x0, HDA_INPUT),
4609 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x11, 0x0, HDA_INPUT),
4610 	HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x2b, 0x0, HDA_INPUT),
4611 	HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x29, 0x0,
4612 			 HDA_INPUT),
4613 	{
4614 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4615 		/* The multiple "Capture Source" controls confuse alsamixer
4616 		 * So call somewhat different..
4617 		 */
4618 		.name = "Input Source",
4619 		.count = 2,
4620 		.info = via_mux_enum_info,
4621 		.get = via_mux_enum_get,
4622 		.put = via_mux_enum_put,
4623 	},
4624 	{ } /* end */
4625 };
4626 
4627 static struct hda_verb vt1718S_volume_init_verbs[] = {
4628 	/*
4629 	 * Unmute ADC0-1 and set the default input to mic-in
4630 	 */
4631 	{0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4632 	{0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4633 
4634 
4635 	/* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
4636 	 * mixer widget
4637 	 */
4638 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
4639 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4640 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4641 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
4642 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
4643 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)},
4644 
4645 	/* Setup default input of Front HP to MW9 */
4646 	{0x28, AC_VERB_SET_CONNECT_SEL, 0x1},
4647 	/* PW9 PW10 Output enable */
4648 	{0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
4649 	{0x2e, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
4650 	/* PW11 Input enable */
4651 	{0x2f, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_IN_EN},
4652 	/* Enable Boost Volume backdoor */
4653 	{0x1, 0xf88, 0x8},
4654 	/* MW0/1/2/3/4: un-mute index 0 (AOWx), mute index 1 (MW9) */
4655 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4656 	{0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4657 	{0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4658 	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4659 	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4660 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4661 	{0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4662 	{0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4663 	{0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4664 	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4665 	/* set MUX1 = 2 (AOW4), MUX2 = 1 (AOW3) */
4666 	{0x34, AC_VERB_SET_CONNECT_SEL, 0x2},
4667 	{0x35, AC_VERB_SET_CONNECT_SEL, 0x1},
4668 	/* Unmute MW4's index 0 */
4669 	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4670 	{ }
4671 };
4672 
4673 
4674 static struct hda_verb vt1718S_uniwill_init_verbs[] = {
4675 	{0x28, AC_VERB_SET_UNSOLICITED_ENABLE,
4676 	 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
4677 	{0x24, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4678 	{0x25, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4679 	{0x26, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4680 	{0x27, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4681 	{0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4682 	{0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4683 	{0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4684 	{ }
4685 };
4686 
4687 static struct hda_pcm_stream vt1718S_pcm_analog_playback = {
4688 	.substreams = 2,
4689 	.channels_min = 2,
4690 	.channels_max = 10,
4691 	.nid = 0x8, /* NID to query formats and rates */
4692 	.ops = {
4693 		.open = via_playback_pcm_open,
4694 		.prepare = via_playback_multi_pcm_prepare,
4695 		.cleanup = via_playback_multi_pcm_cleanup,
4696 		.close = via_pcm_open_close,
4697 	},
4698 };
4699 
4700 static struct hda_pcm_stream vt1718S_pcm_analog_capture = {
4701 	.substreams = 2,
4702 	.channels_min = 2,
4703 	.channels_max = 2,
4704 	.nid = 0x10, /* NID to query formats and rates */
4705 	.ops = {
4706 		.open = via_pcm_open_close,
4707 		.prepare = via_capture_pcm_prepare,
4708 		.cleanup = via_capture_pcm_cleanup,
4709 		.close = via_pcm_open_close,
4710 	},
4711 };
4712 
4713 static struct hda_pcm_stream vt1718S_pcm_digital_playback = {
4714 	.substreams = 2,
4715 	.channels_min = 2,
4716 	.channels_max = 2,
4717 	/* NID is set in via_build_pcms */
4718 	.ops = {
4719 		.open = via_dig_playback_pcm_open,
4720 		.close = via_dig_playback_pcm_close,
4721 		.prepare = via_dig_playback_pcm_prepare,
4722 		.cleanup = via_dig_playback_pcm_cleanup
4723 	},
4724 };
4725 
4726 static struct hda_pcm_stream vt1718S_pcm_digital_capture = {
4727 	.substreams = 1,
4728 	.channels_min = 2,
4729 	.channels_max = 2,
4730 };
4731 
4732 /* fill in the dac_nids table from the parsed pin configuration */
4733 static int vt1718S_auto_fill_dac_nids(struct via_spec *spec,
4734 				     const struct auto_pin_cfg *cfg)
4735 {
4736 	int i;
4737 	hda_nid_t nid;
4738 
4739 	spec->multiout.num_dacs = cfg->line_outs;
4740 
4741 	spec->multiout.dac_nids = spec->private_dac_nids;
4742 
4743 	for (i = 0; i < 4; i++) {
4744 		nid = cfg->line_out_pins[i];
4745 		if (nid) {
4746 			/* config dac list */
4747 			switch (i) {
4748 			case AUTO_SEQ_FRONT:
4749 				spec->multiout.dac_nids[i] = 0x8;
4750 				break;
4751 			case AUTO_SEQ_CENLFE:
4752 				spec->multiout.dac_nids[i] = 0xa;
4753 				break;
4754 			case AUTO_SEQ_SURROUND:
4755 				spec->multiout.dac_nids[i] = 0x9;
4756 				break;
4757 			case AUTO_SEQ_SIDE:
4758 				spec->multiout.dac_nids[i] = 0xb;
4759 				break;
4760 			}
4761 		}
4762 	}
4763 
4764 	return 0;
4765 }
4766 
4767 /* add playback controls from the parsed DAC table */
4768 static int vt1718S_auto_create_multi_out_ctls(struct via_spec *spec,
4769 					     const struct auto_pin_cfg *cfg)
4770 {
4771 	char name[32];
4772 	static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
4773 	hda_nid_t nid_vols[] = {0x8, 0x9, 0xa, 0xb};
4774 	hda_nid_t nid_mutes[] = {0x24, 0x25, 0x26, 0x27};
4775 	hda_nid_t nid, nid_vol, nid_mute = 0;
4776 	int i, err;
4777 
4778 	for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
4779 		nid = cfg->line_out_pins[i];
4780 
4781 		if (!nid)
4782 			continue;
4783 		nid_vol = nid_vols[i];
4784 		nid_mute = nid_mutes[i];
4785 
4786 		if (i == AUTO_SEQ_CENLFE) {
4787 			/* Center/LFE */
4788 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4789 					      "Center Playback Volume",
4790 					      HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
4791 								  HDA_OUTPUT));
4792 			if (err < 0)
4793 				return err;
4794 			err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4795 					      "LFE Playback Volume",
4796 					      HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
4797 								  HDA_OUTPUT));
4798 			if (err < 0)
4799 				return err;
4800 			err = via_add_control(
4801 				spec, VIA_CTL_WIDGET_MUTE,
4802 				"Center Playback Switch",
4803 				HDA_COMPOSE_AMP_VAL(nid_mute, 1, 0,
4804 						    HDA_OUTPUT));
4805 			if (err < 0)
4806 				return err;
4807 			err = via_add_control(
4808 				spec, VIA_CTL_WIDGET_MUTE,
4809 				"LFE Playback Switch",
4810 				HDA_COMPOSE_AMP_VAL(nid_mute, 2, 0,
4811 						    HDA_OUTPUT));
4812 			if (err < 0)
4813 				return err;
4814 		} else if (i == AUTO_SEQ_FRONT) {
4815 			/* Front */
4816 			sprintf(name, "%s Playback Volume", chname[i]);
4817 			err = via_add_control(
4818 				spec, VIA_CTL_WIDGET_VOL, name,
4819 				HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
4820 			if (err < 0)
4821 				return err;
4822 			sprintf(name, "%s Playback Switch", chname[i]);
4823 			err = via_add_control(
4824 				spec, VIA_CTL_WIDGET_MUTE, name,
4825 				HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
4826 						    HDA_OUTPUT));
4827 			if (err < 0)
4828 				return err;
4829 		} else {
4830 			sprintf(name, "%s Playback Volume", chname[i]);
4831 			err = via_add_control(
4832 				spec, VIA_CTL_WIDGET_VOL, name,
4833 				HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
4834 			if (err < 0)
4835 				return err;
4836 			sprintf(name, "%s Playback Switch", chname[i]);
4837 			err = via_add_control(
4838 				spec, VIA_CTL_WIDGET_MUTE, name,
4839 				HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
4840 						    HDA_OUTPUT));
4841 			if (err < 0)
4842 				return err;
4843 		}
4844 	}
4845 	return 0;
4846 }
4847 
4848 static int vt1718S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
4849 {
4850 	int err;
4851 
4852 	if (!pin)
4853 		return 0;
4854 
4855 	spec->multiout.hp_nid = 0xc; /* AOW4 */
4856 	spec->hp_independent_mode_index = 1;
4857 
4858 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4859 			      "Headphone Playback Volume",
4860 			      HDA_COMPOSE_AMP_VAL(0xc, 3, 0, HDA_OUTPUT));
4861 	if (err < 0)
4862 		return err;
4863 
4864 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4865 			      "Headphone Playback Switch",
4866 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
4867 	if (err < 0)
4868 		return err;
4869 
4870 	create_hp_imux(spec);
4871 	return 0;
4872 }
4873 
4874 /* create playback/capture controls for input pins */
4875 static int vt1718S_auto_create_analog_input_ctls(struct via_spec *spec,
4876 						const struct auto_pin_cfg *cfg)
4877 {
4878 	static char *labels[] = {
4879 		"Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
4880 	};
4881 	struct hda_input_mux *imux = &spec->private_imux[0];
4882 	int i, err, idx = 0;
4883 
4884 	/* for internal loopback recording select */
4885 	imux->items[imux->num_items].label = "Stereo Mixer";
4886 	imux->items[imux->num_items].index = 5;
4887 	imux->num_items++;
4888 
4889 	for (i = 0; i < AUTO_PIN_LAST; i++) {
4890 		if (!cfg->input_pins[i])
4891 			continue;
4892 
4893 		switch (cfg->input_pins[i]) {
4894 		case 0x2b: /* Mic */
4895 			idx = 1;
4896 			break;
4897 
4898 		case 0x2a: /* Line In */
4899 			idx = 2;
4900 			break;
4901 
4902 		case 0x29: /* Front Mic */
4903 			idx = 3;
4904 			break;
4905 
4906 		case 0x2c: /* CD */
4907 			idx = 0;
4908 			break;
4909 		}
4910 		err = via_new_analog_input(spec, labels[i], idx, 0x21);
4911 		if (err < 0)
4912 			return err;
4913 		imux->items[imux->num_items].label = labels[i];
4914 		imux->items[imux->num_items].index = idx;
4915 		imux->num_items++;
4916 	}
4917 	return 0;
4918 }
4919 
4920 static int vt1718S_parse_auto_config(struct hda_codec *codec)
4921 {
4922 	struct via_spec *spec = codec->spec;
4923 	int err;
4924 
4925 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4926 
4927 	if (err < 0)
4928 		return err;
4929 	err = vt1718S_auto_fill_dac_nids(spec, &spec->autocfg);
4930 	if (err < 0)
4931 		return err;
4932 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
4933 		return 0; /* can't find valid BIOS pin config */
4934 
4935 	err = vt1718S_auto_create_multi_out_ctls(spec, &spec->autocfg);
4936 	if (err < 0)
4937 		return err;
4938 	err = vt1718S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
4939 	if (err < 0)
4940 		return err;
4941 	err = vt1718S_auto_create_analog_input_ctls(spec, &spec->autocfg);
4942 	if (err < 0)
4943 		return err;
4944 
4945 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4946 
4947 	fill_dig_outs(codec);
4948 
4949 	if (spec->autocfg.dig_in_pin && codec->vendor_id == 0x11060428)
4950 		spec->dig_in_nid = 0x13;
4951 
4952 	if (spec->kctls.list)
4953 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
4954 
4955 	spec->input_mux = &spec->private_imux[0];
4956 
4957 	if (spec->hp_mux)
4958 		via_hp_build(codec);
4959 
4960 	via_smart51_build(spec);
4961 
4962 	return 1;
4963 }
4964 
4965 #ifdef CONFIG_SND_HDA_POWER_SAVE
4966 static struct hda_amp_list vt1718S_loopbacks[] = {
4967 	{ 0x21, HDA_INPUT, 1 },
4968 	{ 0x21, HDA_INPUT, 2 },
4969 	{ 0x21, HDA_INPUT, 3 },
4970 	{ 0x21, HDA_INPUT, 4 },
4971 	{ } /* end */
4972 };
4973 #endif
4974 
4975 static int patch_vt1718S(struct hda_codec *codec)
4976 {
4977 	struct via_spec *spec;
4978 	int err;
4979 
4980 	/* create a codec specific record */
4981 	spec = via_new_spec(codec);
4982 	if (spec == NULL)
4983 		return -ENOMEM;
4984 
4985 	/* automatic parse from the BIOS config */
4986 	err = vt1718S_parse_auto_config(codec);
4987 	if (err < 0) {
4988 		via_free(codec);
4989 		return err;
4990 	} else if (!err) {
4991 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
4992 		       "from BIOS.  Using genenic mode...\n");
4993 	}
4994 
4995 	spec->init_verbs[spec->num_iverbs++] = vt1718S_volume_init_verbs;
4996 	spec->init_verbs[spec->num_iverbs++] = vt1718S_uniwill_init_verbs;
4997 
4998 	if (codec->vendor_id == 0x11060441)
4999 		spec->stream_name_analog = "VT2020 Analog";
5000 	else if (codec->vendor_id == 0x11064441)
5001 		spec->stream_name_analog = "VT1828S Analog";
5002 	else
5003 		spec->stream_name_analog = "VT1718S Analog";
5004 	spec->stream_analog_playback = &vt1718S_pcm_analog_playback;
5005 	spec->stream_analog_capture = &vt1718S_pcm_analog_capture;
5006 
5007 	if (codec->vendor_id == 0x11060441)
5008 		spec->stream_name_digital = "VT2020 Digital";
5009 	else if (codec->vendor_id == 0x11064441)
5010 		spec->stream_name_digital = "VT1828S Digital";
5011 	else
5012 		spec->stream_name_digital = "VT1718S Digital";
5013 	spec->stream_digital_playback = &vt1718S_pcm_digital_playback;
5014 	if (codec->vendor_id == 0x11060428 || codec->vendor_id == 0x11060441)
5015 		spec->stream_digital_capture = &vt1718S_pcm_digital_capture;
5016 
5017 	if (!spec->adc_nids && spec->input_mux) {
5018 		spec->adc_nids = vt1718S_adc_nids;
5019 		spec->num_adc_nids = ARRAY_SIZE(vt1718S_adc_nids);
5020 		get_mux_nids(codec);
5021 		override_mic_boost(codec, 0x2b, 0, 3, 40);
5022 		override_mic_boost(codec, 0x29, 0, 3, 40);
5023 		spec->mixers[spec->num_mixers] = vt1718S_capture_mixer;
5024 		spec->num_mixers++;
5025 	}
5026 
5027 	codec->patch_ops = via_patch_ops;
5028 
5029 	codec->patch_ops.init = via_auto_init;
5030 	codec->patch_ops.unsol_event = via_unsol_event;
5031 
5032 #ifdef CONFIG_SND_HDA_POWER_SAVE
5033 	spec->loopback.amplist = vt1718S_loopbacks;
5034 #endif
5035 
5036 	return 0;
5037 }
5038 
5039 /* Patch for VT1716S */
5040 
5041 static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
5042 			    struct snd_ctl_elem_info *uinfo)
5043 {
5044 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
5045 	uinfo->count = 1;
5046 	uinfo->value.integer.min = 0;
5047 	uinfo->value.integer.max = 1;
5048 	return 0;
5049 }
5050 
5051 static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
5052 			   struct snd_ctl_elem_value *ucontrol)
5053 {
5054 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5055 	int index = 0;
5056 
5057 	index = snd_hda_codec_read(codec, 0x26, 0,
5058 					       AC_VERB_GET_CONNECT_SEL, 0);
5059 	if (index != -1)
5060 		*ucontrol->value.integer.value = index;
5061 
5062 	return 0;
5063 }
5064 
5065 static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
5066 			   struct snd_ctl_elem_value *ucontrol)
5067 {
5068 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5069 	struct via_spec *spec = codec->spec;
5070 	int index = *ucontrol->value.integer.value;
5071 
5072 	snd_hda_codec_write(codec, 0x26, 0,
5073 					       AC_VERB_SET_CONNECT_SEL, index);
5074 	spec->dmic_enabled = index;
5075 	set_jack_power_state(codec);
5076 
5077 	return 1;
5078 }
5079 
5080 /* capture mixer elements */
5081 static struct snd_kcontrol_new vt1716S_capture_mixer[] = {
5082 	HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
5083 	HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
5084 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
5085 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
5086 	HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x1A, 0x0, HDA_INPUT),
5087 	HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x1E, 0x0,
5088 			 HDA_INPUT),
5089 	{
5090 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5091 		.name = "Input Source",
5092 		.count = 1,
5093 		.info = via_mux_enum_info,
5094 		.get = via_mux_enum_get,
5095 		.put = via_mux_enum_put,
5096 	},
5097 	{ } /* end */
5098 };
5099 
5100 static struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
5101 	HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
5102 	{
5103 	 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5104 	 .name = "Digital Mic Capture Switch",
5105 	 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
5106 	 .count = 1,
5107 	 .info = vt1716s_dmic_info,
5108 	 .get = vt1716s_dmic_get,
5109 	 .put = vt1716s_dmic_put,
5110 	 },
5111 	{}			/* end */
5112 };
5113 
5114 
5115 /* mono-out mixer elements */
5116 static struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
5117 	HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
5118 	{ } /* end */
5119 };
5120 
5121 static struct hda_verb vt1716S_volume_init_verbs[] = {
5122 	/*
5123 	 * Unmute ADC0-1 and set the default input to mic-in
5124 	 */
5125 	{0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5126 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5127 
5128 
5129 	/* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
5130 	 * mixer widget
5131 	 */
5132 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
5133 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
5134 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5135 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
5136 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
5137 	{0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
5138 
5139 	/* MUX Indices: Stereo Mixer = 5 */
5140 	{0x17, AC_VERB_SET_CONNECT_SEL, 0x5},
5141 
5142 	/* Setup default input of PW4 to MW0 */
5143 	{0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
5144 
5145 	/* Setup default input of SW1 as MW0 */
5146 	{0x18, AC_VERB_SET_CONNECT_SEL, 0x1},
5147 
5148 	/* Setup default input of SW4 as AOW0 */
5149 	{0x28, AC_VERB_SET_CONNECT_SEL, 0x1},
5150 
5151 	/* PW9 PW10 Output enable */
5152 	{0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
5153 	{0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
5154 
5155 	/* Unmute SW1, PW12 */
5156 	{0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5157 	{0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
5158 	/* PW12 Output enable */
5159 	{0x2a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
5160 	/* Enable Boost Volume backdoor */
5161 	{0x1, 0xf8a, 0x80},
5162 	/* don't bybass mixer */
5163 	{0x1, 0xf88, 0xc0},
5164 	/* Enable mono output */
5165 	{0x1, 0xf90, 0x08},
5166 	{ }
5167 };
5168 
5169 
5170 static struct hda_verb vt1716S_uniwill_init_verbs[] = {
5171 	{0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
5172 	 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
5173 	{0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5174 	{0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5175 	{0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5176 	{0x1c, AC_VERB_SET_UNSOLICITED_ENABLE,
5177 	 AC_USRSP_EN | VIA_MONO_EVENT | VIA_JACK_EVENT},
5178 	{0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5179 	{0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5180 	{ }
5181 };
5182 
5183 static struct hda_pcm_stream vt1716S_pcm_analog_playback = {
5184 	.substreams = 2,
5185 	.channels_min = 2,
5186 	.channels_max = 6,
5187 	.nid = 0x10, /* NID to query formats and rates */
5188 	.ops = {
5189 		.open = via_playback_pcm_open,
5190 		.prepare = via_playback_multi_pcm_prepare,
5191 		.cleanup = via_playback_multi_pcm_cleanup,
5192 		.close = via_pcm_open_close,
5193 	},
5194 };
5195 
5196 static struct hda_pcm_stream vt1716S_pcm_analog_capture = {
5197 	.substreams = 2,
5198 	.channels_min = 2,
5199 	.channels_max = 2,
5200 	.nid = 0x13, /* NID to query formats and rates */
5201 	.ops = {
5202 		.open = via_pcm_open_close,
5203 		.prepare = via_capture_pcm_prepare,
5204 		.cleanup = via_capture_pcm_cleanup,
5205 		.close = via_pcm_open_close,
5206 	},
5207 };
5208 
5209 static struct hda_pcm_stream vt1716S_pcm_digital_playback = {
5210 	.substreams = 2,
5211 	.channels_min = 2,
5212 	.channels_max = 2,
5213 	/* NID is set in via_build_pcms */
5214 	.ops = {
5215 		.open = via_dig_playback_pcm_open,
5216 		.close = via_dig_playback_pcm_close,
5217 		.prepare = via_dig_playback_pcm_prepare,
5218 		.cleanup = via_dig_playback_pcm_cleanup
5219 	},
5220 };
5221 
5222 /* fill in the dac_nids table from the parsed pin configuration */
5223 static int vt1716S_auto_fill_dac_nids(struct via_spec *spec,
5224 				      const struct auto_pin_cfg *cfg)
5225 {	int i;
5226 	hda_nid_t nid;
5227 
5228 	spec->multiout.num_dacs = cfg->line_outs;
5229 
5230 	spec->multiout.dac_nids = spec->private_dac_nids;
5231 
5232 	for (i = 0; i < 3; i++) {
5233 		nid = cfg->line_out_pins[i];
5234 		if (nid) {
5235 			/* config dac list */
5236 			switch (i) {
5237 			case AUTO_SEQ_FRONT:
5238 				spec->multiout.dac_nids[i] = 0x10;
5239 				break;
5240 			case AUTO_SEQ_CENLFE:
5241 				spec->multiout.dac_nids[i] = 0x25;
5242 				break;
5243 			case AUTO_SEQ_SURROUND:
5244 				spec->multiout.dac_nids[i] = 0x11;
5245 				break;
5246 			}
5247 		}
5248 	}
5249 
5250 	return 0;
5251 }
5252 
5253 /* add playback controls from the parsed DAC table */
5254 static int vt1716S_auto_create_multi_out_ctls(struct via_spec *spec,
5255 					      const struct auto_pin_cfg *cfg)
5256 {
5257 	char name[32];
5258 	static const char *chname[3] = { "Front", "Surround", "C/LFE" };
5259 	hda_nid_t nid_vols[] = {0x10, 0x11, 0x25};
5260 	hda_nid_t nid_mutes[] = {0x1C, 0x18, 0x27};
5261 	hda_nid_t nid, nid_vol, nid_mute;
5262 	int i, err;
5263 
5264 	for (i = 0; i <= AUTO_SEQ_CENLFE; i++) {
5265 		nid = cfg->line_out_pins[i];
5266 
5267 		if (!nid)
5268 			continue;
5269 
5270 		nid_vol = nid_vols[i];
5271 		nid_mute = nid_mutes[i];
5272 
5273 		if (i == AUTO_SEQ_CENLFE) {
5274 			err = via_add_control(
5275 				spec, VIA_CTL_WIDGET_VOL,
5276 				"Center Playback Volume",
5277 				HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0, HDA_OUTPUT));
5278 			if (err < 0)
5279 				return err;
5280 			err = via_add_control(
5281 				spec, VIA_CTL_WIDGET_VOL,
5282 				"LFE Playback Volume",
5283 				HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0, HDA_OUTPUT));
5284 			if (err < 0)
5285 				return err;
5286 			err = via_add_control(
5287 				spec, VIA_CTL_WIDGET_MUTE,
5288 				"Center Playback Switch",
5289 				HDA_COMPOSE_AMP_VAL(nid_mute, 1, 0,
5290 						    HDA_OUTPUT));
5291 			if (err < 0)
5292 				return err;
5293 			err = via_add_control(
5294 				spec, VIA_CTL_WIDGET_MUTE,
5295 				"LFE Playback Switch",
5296 				HDA_COMPOSE_AMP_VAL(nid_mute, 2, 0,
5297 						    HDA_OUTPUT));
5298 			if (err < 0)
5299 				return err;
5300 		} else if (i == AUTO_SEQ_FRONT) {
5301 
5302 			err = via_add_control(
5303 				spec, VIA_CTL_WIDGET_VOL,
5304 				"Master Front Playback Volume",
5305 				HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_INPUT));
5306 			if (err < 0)
5307 				return err;
5308 			err = via_add_control(
5309 				spec, VIA_CTL_WIDGET_MUTE,
5310 				"Master Front Playback Switch",
5311 				HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_INPUT));
5312 			if (err < 0)
5313 				return err;
5314 
5315 			sprintf(name, "%s Playback Volume", chname[i]);
5316 			err = via_add_control(
5317 				spec, VIA_CTL_WIDGET_VOL, name,
5318 				HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
5319 			if (err < 0)
5320 				return err;
5321 			sprintf(name, "%s Playback Switch", chname[i]);
5322 			err = via_add_control(
5323 				spec, VIA_CTL_WIDGET_MUTE, name,
5324 				HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
5325 						    HDA_OUTPUT));
5326 			if (err < 0)
5327 				return err;
5328 		} else {
5329 			sprintf(name, "%s Playback Volume", chname[i]);
5330 			err = via_add_control(
5331 				spec, VIA_CTL_WIDGET_VOL, name,
5332 				HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
5333 			if (err < 0)
5334 				return err;
5335 			sprintf(name, "%s Playback Switch", chname[i]);
5336 			err = via_add_control(
5337 				spec, VIA_CTL_WIDGET_MUTE, name,
5338 				HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
5339 						    HDA_OUTPUT));
5340 			if (err < 0)
5341 				return err;
5342 		}
5343 	}
5344 	return 0;
5345 }
5346 
5347 static int vt1716S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
5348 {
5349 	int err;
5350 
5351 	if (!pin)
5352 		return 0;
5353 
5354 	spec->multiout.hp_nid = 0x25; /* AOW3 */
5355 	spec->hp_independent_mode_index = 1;
5356 
5357 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5358 			      "Headphone Playback Volume",
5359 			      HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
5360 	if (err < 0)
5361 		return err;
5362 
5363 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
5364 			      "Headphone Playback Switch",
5365 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
5366 	if (err < 0)
5367 		return err;
5368 
5369 	create_hp_imux(spec);
5370 	return 0;
5371 }
5372 
5373 /* create playback/capture controls for input pins */
5374 static int vt1716S_auto_create_analog_input_ctls(struct via_spec *spec,
5375 						const struct auto_pin_cfg *cfg)
5376 {
5377 	static char *labels[] = {
5378 		"Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
5379 	};
5380 	struct hda_input_mux *imux = &spec->private_imux[0];
5381 	int i, err, idx = 0;
5382 
5383 	/* for internal loopback recording select */
5384 	imux->items[imux->num_items].label = "Stereo Mixer";
5385 	imux->items[imux->num_items].index = 5;
5386 	imux->num_items++;
5387 
5388 	for (i = 0; i < AUTO_PIN_LAST; i++) {
5389 		if (!cfg->input_pins[i])
5390 			continue;
5391 
5392 		switch (cfg->input_pins[i]) {
5393 		case 0x1a: /* Mic */
5394 			idx = 2;
5395 			break;
5396 
5397 		case 0x1b: /* Line In */
5398 			idx = 3;
5399 			break;
5400 
5401 		case 0x1e: /* Front Mic */
5402 			idx = 4;
5403 			break;
5404 
5405 		case 0x1f: /* CD */
5406 			idx = 1;
5407 			break;
5408 		}
5409 		err = via_new_analog_input(spec, labels[i], idx, 0x16);
5410 		if (err < 0)
5411 			return err;
5412 		imux->items[imux->num_items].label = labels[i];
5413 		imux->items[imux->num_items].index = idx-1;
5414 		imux->num_items++;
5415 	}
5416 	return 0;
5417 }
5418 
5419 static int vt1716S_parse_auto_config(struct hda_codec *codec)
5420 {
5421 	struct via_spec *spec = codec->spec;
5422 	int err;
5423 
5424 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
5425 	if (err < 0)
5426 		return err;
5427 	err = vt1716S_auto_fill_dac_nids(spec, &spec->autocfg);
5428 	if (err < 0)
5429 		return err;
5430 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
5431 		return 0; /* can't find valid BIOS pin config */
5432 
5433 	err = vt1716S_auto_create_multi_out_ctls(spec, &spec->autocfg);
5434 	if (err < 0)
5435 		return err;
5436 	err = vt1716S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
5437 	if (err < 0)
5438 		return err;
5439 	err = vt1716S_auto_create_analog_input_ctls(spec, &spec->autocfg);
5440 	if (err < 0)
5441 		return err;
5442 
5443 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
5444 
5445 	fill_dig_outs(codec);
5446 
5447 	if (spec->kctls.list)
5448 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
5449 
5450 	spec->input_mux = &spec->private_imux[0];
5451 
5452 	if (spec->hp_mux)
5453 		via_hp_build(codec);
5454 
5455 	via_smart51_build(spec);
5456 
5457 	return 1;
5458 }
5459 
5460 #ifdef CONFIG_SND_HDA_POWER_SAVE
5461 static struct hda_amp_list vt1716S_loopbacks[] = {
5462 	{ 0x16, HDA_INPUT, 1 },
5463 	{ 0x16, HDA_INPUT, 2 },
5464 	{ 0x16, HDA_INPUT, 3 },
5465 	{ 0x16, HDA_INPUT, 4 },
5466 	{ } /* end */
5467 };
5468 #endif
5469 
5470 static int patch_vt1716S(struct hda_codec *codec)
5471 {
5472 	struct via_spec *spec;
5473 	int err;
5474 
5475 	/* create a codec specific record */
5476 	spec = via_new_spec(codec);
5477 	if (spec == NULL)
5478 		return -ENOMEM;
5479 
5480 	/* automatic parse from the BIOS config */
5481 	err = vt1716S_parse_auto_config(codec);
5482 	if (err < 0) {
5483 		via_free(codec);
5484 		return err;
5485 	} else if (!err) {
5486 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
5487 		       "from BIOS.  Using genenic mode...\n");
5488 	}
5489 
5490 	spec->init_verbs[spec->num_iverbs++]  = vt1716S_volume_init_verbs;
5491 	spec->init_verbs[spec->num_iverbs++] = vt1716S_uniwill_init_verbs;
5492 
5493 	spec->stream_name_analog = "VT1716S Analog";
5494 	spec->stream_analog_playback = &vt1716S_pcm_analog_playback;
5495 	spec->stream_analog_capture = &vt1716S_pcm_analog_capture;
5496 
5497 	spec->stream_name_digital = "VT1716S Digital";
5498 	spec->stream_digital_playback = &vt1716S_pcm_digital_playback;
5499 
5500 	if (!spec->adc_nids && spec->input_mux) {
5501 		spec->adc_nids = vt1716S_adc_nids;
5502 		spec->num_adc_nids = ARRAY_SIZE(vt1716S_adc_nids);
5503 		get_mux_nids(codec);
5504 		override_mic_boost(codec, 0x1a, 0, 3, 40);
5505 		override_mic_boost(codec, 0x1e, 0, 3, 40);
5506 		spec->mixers[spec->num_mixers] = vt1716S_capture_mixer;
5507 		spec->num_mixers++;
5508 	}
5509 
5510 	spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
5511 	spec->num_mixers++;
5512 
5513 	spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
5514 
5515 	codec->patch_ops = via_patch_ops;
5516 
5517 	codec->patch_ops.init = via_auto_init;
5518 	codec->patch_ops.unsol_event = via_unsol_event;
5519 
5520 #ifdef CONFIG_SND_HDA_POWER_SAVE
5521 	spec->loopback.amplist = vt1716S_loopbacks;
5522 #endif
5523 
5524 	return 0;
5525 }
5526 
5527 /* for vt2002P */
5528 
5529 /* capture mixer elements */
5530 static struct snd_kcontrol_new vt2002P_capture_mixer[] = {
5531 	HDA_CODEC_VOLUME("Capture Volume", 0x10, 0x0, HDA_INPUT),
5532 	HDA_CODEC_MUTE("Capture Switch", 0x10, 0x0, HDA_INPUT),
5533 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x11, 0x0, HDA_INPUT),
5534 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x11, 0x0, HDA_INPUT),
5535 	HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x2b, 0x0, HDA_INPUT),
5536 	HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x29, 0x0,
5537 			 HDA_INPUT),
5538 	{
5539 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5540 		/* The multiple "Capture Source" controls confuse alsamixer
5541 		 * So call somewhat different..
5542 		 */
5543 		/* .name = "Capture Source", */
5544 		.name = "Input Source",
5545 		.count = 2,
5546 		.info = via_mux_enum_info,
5547 		.get = via_mux_enum_get,
5548 		.put = via_mux_enum_put,
5549 	},
5550 	{ } /* end */
5551 };
5552 
5553 static struct hda_verb vt2002P_volume_init_verbs[] = {
5554 	/*
5555 	 * Unmute ADC0-1 and set the default input to mic-in
5556 	 */
5557 	{0x8, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5558 	{0x9, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5559 
5560 
5561 	/* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
5562 	 * mixer widget
5563 	 */
5564 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
5565 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
5566 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5567 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
5568 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
5569 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
5570 
5571 	/* MUX Indices: Mic = 0 */
5572 	{0x1e, AC_VERB_SET_CONNECT_SEL, 0},
5573 	{0x1f, AC_VERB_SET_CONNECT_SEL, 0},
5574 
5575 	/* PW9 Output enable */
5576 	{0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
5577 
5578 	/* Enable Boost Volume backdoor */
5579 	{0x1, 0xfb9, 0x24},
5580 
5581 	/* MW0/1/4/8: un-mute index 0 (MUXx), un-mute index 1 (MW9) */
5582 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5583 	{0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5584 	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5585 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5586 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5587 	{0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5588 	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5589 	{0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5590 
5591 	/* set MUX0/1/4/8 = 0 (AOW0) */
5592 	{0x34, AC_VERB_SET_CONNECT_SEL, 0},
5593 	{0x35, AC_VERB_SET_CONNECT_SEL, 0},
5594 	{0x37, AC_VERB_SET_CONNECT_SEL, 0},
5595 	{0x3b, AC_VERB_SET_CONNECT_SEL, 0},
5596 
5597 	/* set PW0 index=0 (MW0) */
5598 	{0x24, AC_VERB_SET_CONNECT_SEL, 0},
5599 
5600 	/* Enable AOW0 to MW9 */
5601 	{0x1, 0xfb8, 0x88},
5602 	{ }
5603 };
5604 
5605 
5606 static struct hda_verb vt2002P_uniwill_init_verbs[] = {
5607 	{0x25, AC_VERB_SET_UNSOLICITED_ENABLE,
5608 	 AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5609 	{0x26, AC_VERB_SET_UNSOLICITED_ENABLE,
5610 	 AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5611 	{0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5612 	{0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5613 	{0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5614 	{ }
5615 };
5616 
5617 static struct hda_pcm_stream vt2002P_pcm_analog_playback = {
5618 	.substreams = 2,
5619 	.channels_min = 2,
5620 	.channels_max = 2,
5621 	.nid = 0x8, /* NID to query formats and rates */
5622 	.ops = {
5623 		.open = via_playback_pcm_open,
5624 		.prepare = via_playback_multi_pcm_prepare,
5625 		.cleanup = via_playback_multi_pcm_cleanup,
5626 		.close = via_pcm_open_close,
5627 	},
5628 };
5629 
5630 static struct hda_pcm_stream vt2002P_pcm_analog_capture = {
5631 	.substreams = 2,
5632 	.channels_min = 2,
5633 	.channels_max = 2,
5634 	.nid = 0x10, /* NID to query formats and rates */
5635 	.ops = {
5636 		.open = via_pcm_open_close,
5637 		.prepare = via_capture_pcm_prepare,
5638 		.cleanup = via_capture_pcm_cleanup,
5639 		.close = via_pcm_open_close,
5640 	},
5641 };
5642 
5643 static struct hda_pcm_stream vt2002P_pcm_digital_playback = {
5644 	.substreams = 1,
5645 	.channels_min = 2,
5646 	.channels_max = 2,
5647 	/* NID is set in via_build_pcms */
5648 	.ops = {
5649 		.open = via_dig_playback_pcm_open,
5650 		.close = via_dig_playback_pcm_close,
5651 		.prepare = via_dig_playback_pcm_prepare,
5652 		.cleanup = via_dig_playback_pcm_cleanup
5653 	},
5654 };
5655 
5656 /* fill in the dac_nids table from the parsed pin configuration */
5657 static int vt2002P_auto_fill_dac_nids(struct via_spec *spec,
5658 				      const struct auto_pin_cfg *cfg)
5659 {
5660 	spec->multiout.num_dacs = 1;
5661 	spec->multiout.dac_nids = spec->private_dac_nids;
5662 	if (cfg->line_out_pins[0])
5663 		spec->multiout.dac_nids[0] = 0x8;
5664 	return 0;
5665 }
5666 
5667 /* add playback controls from the parsed DAC table */
5668 static int vt2002P_auto_create_multi_out_ctls(struct via_spec *spec,
5669 					     const struct auto_pin_cfg *cfg)
5670 {
5671 	int err;
5672 
5673 	if (!cfg->line_out_pins[0])
5674 		return -1;
5675 
5676 
5677 	/* Line-Out: PortE */
5678 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5679 			      "Master Front Playback Volume",
5680 			      HDA_COMPOSE_AMP_VAL(0x8, 3, 0, HDA_OUTPUT));
5681 	if (err < 0)
5682 		return err;
5683 	err = via_add_control(spec, VIA_CTL_WIDGET_BIND_PIN_MUTE,
5684 			      "Master Front Playback Switch",
5685 			      HDA_COMPOSE_AMP_VAL(0x26, 3, 0, HDA_OUTPUT));
5686 	if (err < 0)
5687 		return err;
5688 
5689 	return 0;
5690 }
5691 
5692 static int vt2002P_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
5693 {
5694 	int err;
5695 
5696 	if (!pin)
5697 		return 0;
5698 
5699 	spec->multiout.hp_nid = 0x9;
5700 	spec->hp_independent_mode_index = 1;
5701 
5702 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5703 			      "Headphone Playback Volume",
5704 			      HDA_COMPOSE_AMP_VAL(
5705 				      spec->multiout.hp_nid, 3, 0, HDA_OUTPUT));
5706 	if (err < 0)
5707 		return err;
5708 
5709 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
5710 			      "Headphone Playback Switch",
5711 			      HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
5712 	if (err < 0)
5713 		return err;
5714 
5715 	create_hp_imux(spec);
5716 	return 0;
5717 }
5718 
5719 /* create playback/capture controls for input pins */
5720 static int vt2002P_auto_create_analog_input_ctls(struct via_spec *spec,
5721 						const struct auto_pin_cfg *cfg)
5722 {
5723 	static char *labels[] = {
5724 		"Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
5725 	};
5726 	struct hda_input_mux *imux = &spec->private_imux[0];
5727 	int i, err, idx = 0;
5728 
5729 	for (i = 0; i < AUTO_PIN_LAST; i++) {
5730 		if (!cfg->input_pins[i])
5731 			continue;
5732 
5733 		switch (cfg->input_pins[i]) {
5734 		case 0x2b: /* Mic */
5735 			idx = 0;
5736 			break;
5737 
5738 		case 0x2a: /* Line In */
5739 			idx = 1;
5740 			break;
5741 
5742 		case 0x29: /* Front Mic */
5743 			idx = 2;
5744 			break;
5745 		}
5746 		err = via_new_analog_input(spec, labels[i], idx, 0x21);
5747 		if (err < 0)
5748 			return err;
5749 		imux->items[imux->num_items].label = labels[i];
5750 		imux->items[imux->num_items].index = idx;
5751 		imux->num_items++;
5752 	}
5753 
5754 	/* build volume/mute control of loopback */
5755 	err = via_new_analog_input(spec, "Stereo Mixer", 3, 0x21);
5756 	if (err < 0)
5757 		return err;
5758 
5759 	/* for internal loopback recording select */
5760 	imux->items[imux->num_items].label = "Stereo Mixer";
5761 	imux->items[imux->num_items].index = 3;
5762 	imux->num_items++;
5763 
5764 	/* for digital mic select */
5765 	imux->items[imux->num_items].label = "Digital Mic";
5766 	imux->items[imux->num_items].index = 4;
5767 	imux->num_items++;
5768 
5769 	return 0;
5770 }
5771 
5772 static int vt2002P_parse_auto_config(struct hda_codec *codec)
5773 {
5774 	struct via_spec *spec = codec->spec;
5775 	int err;
5776 
5777 
5778 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
5779 	if (err < 0)
5780 		return err;
5781 
5782 	err = vt2002P_auto_fill_dac_nids(spec, &spec->autocfg);
5783 	if (err < 0)
5784 		return err;
5785 
5786 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
5787 		return 0; /* can't find valid BIOS pin config */
5788 
5789 	err = vt2002P_auto_create_multi_out_ctls(spec, &spec->autocfg);
5790 	if (err < 0)
5791 		return err;
5792 	err = vt2002P_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
5793 	if (err < 0)
5794 		return err;
5795 	err = vt2002P_auto_create_analog_input_ctls(spec, &spec->autocfg);
5796 	if (err < 0)
5797 		return err;
5798 
5799 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
5800 
5801 	fill_dig_outs(codec);
5802 
5803 	if (spec->kctls.list)
5804 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
5805 
5806 	spec->input_mux = &spec->private_imux[0];
5807 
5808 	if (spec->hp_mux)
5809 		via_hp_build(codec);
5810 
5811 	return 1;
5812 }
5813 
5814 #ifdef CONFIG_SND_HDA_POWER_SAVE
5815 static struct hda_amp_list vt2002P_loopbacks[] = {
5816 	{ 0x21, HDA_INPUT, 0 },
5817 	{ 0x21, HDA_INPUT, 1 },
5818 	{ 0x21, HDA_INPUT, 2 },
5819 	{ } /* end */
5820 };
5821 #endif
5822 
5823 
5824 /* patch for vt2002P */
5825 static int patch_vt2002P(struct hda_codec *codec)
5826 {
5827 	struct via_spec *spec;
5828 	int err;
5829 
5830 	/* create a codec specific record */
5831 	spec = via_new_spec(codec);
5832 	if (spec == NULL)
5833 		return -ENOMEM;
5834 
5835 	/* automatic parse from the BIOS config */
5836 	err = vt2002P_parse_auto_config(codec);
5837 	if (err < 0) {
5838 		via_free(codec);
5839 		return err;
5840 	} else if (!err) {
5841 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
5842 		       "from BIOS.  Using genenic mode...\n");
5843 	}
5844 
5845 	spec->init_verbs[spec->num_iverbs++]  = vt2002P_volume_init_verbs;
5846 	spec->init_verbs[spec->num_iverbs++] = vt2002P_uniwill_init_verbs;
5847 
5848 	spec->stream_name_analog = "VT2002P Analog";
5849 	spec->stream_analog_playback = &vt2002P_pcm_analog_playback;
5850 	spec->stream_analog_capture = &vt2002P_pcm_analog_capture;
5851 
5852 	spec->stream_name_digital = "VT2002P Digital";
5853 	spec->stream_digital_playback = &vt2002P_pcm_digital_playback;
5854 
5855 	if (!spec->adc_nids && spec->input_mux) {
5856 		spec->adc_nids = vt2002P_adc_nids;
5857 		spec->num_adc_nids = ARRAY_SIZE(vt2002P_adc_nids);
5858 		get_mux_nids(codec);
5859 		override_mic_boost(codec, 0x2b, 0, 3, 40);
5860 		override_mic_boost(codec, 0x29, 0, 3, 40);
5861 		spec->mixers[spec->num_mixers] = vt2002P_capture_mixer;
5862 		spec->num_mixers++;
5863 	}
5864 
5865 	codec->patch_ops = via_patch_ops;
5866 
5867 	codec->patch_ops.init = via_auto_init;
5868 	codec->patch_ops.unsol_event = via_unsol_event;
5869 
5870 #ifdef CONFIG_SND_HDA_POWER_SAVE
5871 	spec->loopback.amplist = vt2002P_loopbacks;
5872 #endif
5873 
5874 	return 0;
5875 }
5876 
5877 /* for vt1812 */
5878 
5879 /* capture mixer elements */
5880 static struct snd_kcontrol_new vt1812_capture_mixer[] = {
5881 	HDA_CODEC_VOLUME("Capture Volume", 0x10, 0x0, HDA_INPUT),
5882 	HDA_CODEC_MUTE("Capture Switch", 0x10, 0x0, HDA_INPUT),
5883 	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x11, 0x0, HDA_INPUT),
5884 	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x11, 0x0, HDA_INPUT),
5885 	HDA_CODEC_MUTE("Mic Boost Capture Volume", 0x2b, 0x0, HDA_INPUT),
5886 	HDA_CODEC_MUTE("Front Mic Boost Capture Volume", 0x29, 0x0,
5887 		       HDA_INPUT),
5888 	{
5889 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5890 		/* The multiple "Capture Source" controls confuse alsamixer
5891 		 * So call somewhat different..
5892 		 */
5893 		.name = "Input Source",
5894 		.count = 2,
5895 		.info = via_mux_enum_info,
5896 		.get = via_mux_enum_get,
5897 		.put = via_mux_enum_put,
5898 	},
5899 	{ } /* end */
5900 };
5901 
5902 static struct hda_verb vt1812_volume_init_verbs[] = {
5903 	/*
5904 	 * Unmute ADC0-1 and set the default input to mic-in
5905 	 */
5906 	{0x8, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5907 	{0x9, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5908 
5909 
5910 	/* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
5911 	 * mixer widget
5912 	 */
5913 	/* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
5914 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
5915 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5916 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
5917 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
5918 	{0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
5919 
5920 	/* MUX Indices: Mic = 0 */
5921 	{0x1e, AC_VERB_SET_CONNECT_SEL, 0},
5922 	{0x1f, AC_VERB_SET_CONNECT_SEL, 0},
5923 
5924 	/* PW9 Output enable */
5925 	{0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
5926 
5927 	/* Enable Boost Volume backdoor */
5928 	{0x1, 0xfb9, 0x24},
5929 
5930 	/* MW0/1/4/13/15: un-mute index 0 (MUXx), un-mute index 1 (MW9) */
5931 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5932 	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5933 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5934 	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5935 	{0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5936 	{0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5937 	{0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5938 	{0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5939 	{0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5940 	{0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5941 
5942 	/* set MUX0/1/4/13/15 = 0 (AOW0) */
5943 	{0x34, AC_VERB_SET_CONNECT_SEL, 0},
5944 	{0x35, AC_VERB_SET_CONNECT_SEL, 0},
5945 	{0x38, AC_VERB_SET_CONNECT_SEL, 0},
5946 	{0x3c, AC_VERB_SET_CONNECT_SEL, 0},
5947 	{0x3d, AC_VERB_SET_CONNECT_SEL, 0},
5948 
5949 	/* Enable AOW0 to MW9 */
5950 	{0x1, 0xfb8, 0xa8},
5951 	{ }
5952 };
5953 
5954 
5955 static struct hda_verb vt1812_uniwill_init_verbs[] = {
5956 	{0x33, AC_VERB_SET_UNSOLICITED_ENABLE,
5957 	 AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5958 	{0x25, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT },
5959 	{0x28, AC_VERB_SET_UNSOLICITED_ENABLE,
5960 	 AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5961 	{0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5962 	{0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5963 	{0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5964 	{ }
5965 };
5966 
5967 static struct hda_pcm_stream vt1812_pcm_analog_playback = {
5968 	.substreams = 2,
5969 	.channels_min = 2,
5970 	.channels_max = 2,
5971 	.nid = 0x8, /* NID to query formats and rates */
5972 	.ops = {
5973 		.open = via_playback_pcm_open,
5974 		.prepare = via_playback_multi_pcm_prepare,
5975 		.cleanup = via_playback_multi_pcm_cleanup,
5976 		.close = via_pcm_open_close,
5977 	},
5978 };
5979 
5980 static struct hda_pcm_stream vt1812_pcm_analog_capture = {
5981 	.substreams = 2,
5982 	.channels_min = 2,
5983 	.channels_max = 2,
5984 	.nid = 0x10, /* NID to query formats and rates */
5985 	.ops = {
5986 		.open = via_pcm_open_close,
5987 		.prepare = via_capture_pcm_prepare,
5988 		.cleanup = via_capture_pcm_cleanup,
5989 		.close = via_pcm_open_close,
5990 	},
5991 };
5992 
5993 static struct hda_pcm_stream vt1812_pcm_digital_playback = {
5994 	.substreams = 1,
5995 	.channels_min = 2,
5996 	.channels_max = 2,
5997 	/* NID is set in via_build_pcms */
5998 	.ops = {
5999 		.open = via_dig_playback_pcm_open,
6000 		.close = via_dig_playback_pcm_close,
6001 		.prepare = via_dig_playback_pcm_prepare,
6002 		.cleanup = via_dig_playback_pcm_cleanup
6003 	},
6004 };
6005 /* fill in the dac_nids table from the parsed pin configuration */
6006 static int vt1812_auto_fill_dac_nids(struct via_spec *spec,
6007 				     const struct auto_pin_cfg *cfg)
6008 {
6009 	spec->multiout.num_dacs = 1;
6010 	spec->multiout.dac_nids = spec->private_dac_nids;
6011 	if (cfg->line_out_pins[0])
6012 		spec->multiout.dac_nids[0] = 0x8;
6013 	return 0;
6014 }
6015 
6016 
6017 /* add playback controls from the parsed DAC table */
6018 static int vt1812_auto_create_multi_out_ctls(struct via_spec *spec,
6019 					     const struct auto_pin_cfg *cfg)
6020 {
6021 	int err;
6022 
6023 	if (!cfg->line_out_pins[0])
6024 		return -1;
6025 
6026 	/* Line-Out: PortE */
6027 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
6028 			      "Front Playback Volume",
6029 			      HDA_COMPOSE_AMP_VAL(0x8, 3, 0, HDA_OUTPUT));
6030 	if (err < 0)
6031 		return err;
6032 	err = via_add_control(spec, VIA_CTL_WIDGET_BIND_PIN_MUTE,
6033 			      "Front Playback Switch",
6034 			      HDA_COMPOSE_AMP_VAL(0x28, 3, 0, HDA_OUTPUT));
6035 	if (err < 0)
6036 		return err;
6037 
6038 	return 0;
6039 }
6040 
6041 static int vt1812_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
6042 {
6043 	int err;
6044 
6045 	if (!pin)
6046 		return 0;
6047 
6048 	spec->multiout.hp_nid = 0x9;
6049 	spec->hp_independent_mode_index = 1;
6050 
6051 
6052 	err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
6053 			      "Headphone Playback Volume",
6054 			      HDA_COMPOSE_AMP_VAL(
6055 				      spec->multiout.hp_nid, 3, 0, HDA_OUTPUT));
6056 	if (err < 0)
6057 		return err;
6058 
6059 	err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
6060 			      "Headphone Playback Switch",
6061 			      HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
6062 	if (err < 0)
6063 		return err;
6064 
6065 	create_hp_imux(spec);
6066 	return 0;
6067 }
6068 
6069 /* create playback/capture controls for input pins */
6070 static int vt1812_auto_create_analog_input_ctls(struct via_spec *spec,
6071 						const struct auto_pin_cfg *cfg)
6072 {
6073 	static char *labels[] = {
6074 		"Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
6075 	};
6076 	struct hda_input_mux *imux = &spec->private_imux[0];
6077 	int i, err, idx = 0;
6078 
6079 	for (i = 0; i < AUTO_PIN_LAST; i++) {
6080 		if (!cfg->input_pins[i])
6081 			continue;
6082 
6083 		switch (cfg->input_pins[i]) {
6084 		case 0x2b: /* Mic */
6085 			idx = 0;
6086 			break;
6087 
6088 		case 0x2a: /* Line In */
6089 			idx = 1;
6090 			break;
6091 
6092 		case 0x29: /* Front Mic */
6093 			idx = 2;
6094 			break;
6095 		}
6096 		err = via_new_analog_input(spec, labels[i], idx, 0x21);
6097 		if (err < 0)
6098 			return err;
6099 		imux->items[imux->num_items].label = labels[i];
6100 		imux->items[imux->num_items].index = idx;
6101 		imux->num_items++;
6102 	}
6103 	/* build volume/mute control of loopback */
6104 	err = via_new_analog_input(spec, "Stereo Mixer", 5, 0x21);
6105 	if (err < 0)
6106 		return err;
6107 
6108 	/* for internal loopback recording select */
6109 	imux->items[imux->num_items].label = "Stereo Mixer";
6110 	imux->items[imux->num_items].index = 5;
6111 	imux->num_items++;
6112 
6113 	/* for digital mic select */
6114 	imux->items[imux->num_items].label = "Digital Mic";
6115 	imux->items[imux->num_items].index = 6;
6116 	imux->num_items++;
6117 
6118 	return 0;
6119 }
6120 
6121 static int vt1812_parse_auto_config(struct hda_codec *codec)
6122 {
6123 	struct via_spec *spec = codec->spec;
6124 	int err;
6125 
6126 
6127 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
6128 	if (err < 0)
6129 		return err;
6130 	fill_dig_outs(codec);
6131 	err = vt1812_auto_fill_dac_nids(spec, &spec->autocfg);
6132 	if (err < 0)
6133 		return err;
6134 
6135 	if (!spec->autocfg.line_outs && !spec->autocfg.hp_outs)
6136 		return 0; /* can't find valid BIOS pin config */
6137 
6138 	err = vt1812_auto_create_multi_out_ctls(spec, &spec->autocfg);
6139 	if (err < 0)
6140 		return err;
6141 	err = vt1812_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
6142 	if (err < 0)
6143 		return err;
6144 	err = vt1812_auto_create_analog_input_ctls(spec, &spec->autocfg);
6145 	if (err < 0)
6146 		return err;
6147 
6148 	spec->multiout.max_channels = spec->multiout.num_dacs * 2;
6149 
6150 	fill_dig_outs(codec);
6151 
6152 	if (spec->kctls.list)
6153 		spec->mixers[spec->num_mixers++] = spec->kctls.list;
6154 
6155 	spec->input_mux = &spec->private_imux[0];
6156 
6157 	if (spec->hp_mux)
6158 		via_hp_build(codec);
6159 
6160 	return 1;
6161 }
6162 
6163 #ifdef CONFIG_SND_HDA_POWER_SAVE
6164 static struct hda_amp_list vt1812_loopbacks[] = {
6165 	{ 0x21, HDA_INPUT, 0 },
6166 	{ 0x21, HDA_INPUT, 1 },
6167 	{ 0x21, HDA_INPUT, 2 },
6168 	{ } /* end */
6169 };
6170 #endif
6171 
6172 
6173 /* patch for vt1812 */
6174 static int patch_vt1812(struct hda_codec *codec)
6175 {
6176 	struct via_spec *spec;
6177 	int err;
6178 
6179 	/* create a codec specific record */
6180 	spec = via_new_spec(codec);
6181 	if (spec == NULL)
6182 		return -ENOMEM;
6183 
6184 	/* automatic parse from the BIOS config */
6185 	err = vt1812_parse_auto_config(codec);
6186 	if (err < 0) {
6187 		via_free(codec);
6188 		return err;
6189 	} else if (!err) {
6190 		printk(KERN_INFO "hda_codec: Cannot set up configuration "
6191 		       "from BIOS.  Using genenic mode...\n");
6192 	}
6193 
6194 
6195 	spec->init_verbs[spec->num_iverbs++]  = vt1812_volume_init_verbs;
6196 	spec->init_verbs[spec->num_iverbs++] = vt1812_uniwill_init_verbs;
6197 
6198 	spec->stream_name_analog = "VT1812 Analog";
6199 	spec->stream_analog_playback = &vt1812_pcm_analog_playback;
6200 	spec->stream_analog_capture = &vt1812_pcm_analog_capture;
6201 
6202 	spec->stream_name_digital = "VT1812 Digital";
6203 	spec->stream_digital_playback = &vt1812_pcm_digital_playback;
6204 
6205 
6206 	if (!spec->adc_nids && spec->input_mux) {
6207 		spec->adc_nids = vt1812_adc_nids;
6208 		spec->num_adc_nids = ARRAY_SIZE(vt1812_adc_nids);
6209 		get_mux_nids(codec);
6210 		override_mic_boost(codec, 0x2b, 0, 3, 40);
6211 		override_mic_boost(codec, 0x29, 0, 3, 40);
6212 		spec->mixers[spec->num_mixers] = vt1812_capture_mixer;
6213 		spec->num_mixers++;
6214 	}
6215 
6216 	codec->patch_ops = via_patch_ops;
6217 
6218 	codec->patch_ops.init = via_auto_init;
6219 	codec->patch_ops.unsol_event = via_unsol_event;
6220 
6221 #ifdef CONFIG_SND_HDA_POWER_SAVE
6222 	spec->loopback.amplist = vt1812_loopbacks;
6223 #endif
6224 
6225 	return 0;
6226 }
6227 
6228 /*
6229  * patch entries
6230  */
6231 static struct hda_codec_preset snd_hda_preset_via[] = {
6232 	{ .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
6233 	{ .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
6234 	{ .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
6235 	{ .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
6236 	{ .id = 0x1106e710, .name = "VT1709 10-Ch",
6237 	  .patch = patch_vt1709_10ch},
6238 	{ .id = 0x1106e711, .name = "VT1709 10-Ch",
6239 	  .patch = patch_vt1709_10ch},
6240 	{ .id = 0x1106e712, .name = "VT1709 10-Ch",
6241 	  .patch = patch_vt1709_10ch},
6242 	{ .id = 0x1106e713, .name = "VT1709 10-Ch",
6243 	  .patch = patch_vt1709_10ch},
6244 	{ .id = 0x1106e714, .name = "VT1709 6-Ch",
6245 	  .patch = patch_vt1709_6ch},
6246 	{ .id = 0x1106e715, .name = "VT1709 6-Ch",
6247 	  .patch = patch_vt1709_6ch},
6248 	{ .id = 0x1106e716, .name = "VT1709 6-Ch",
6249 	  .patch = patch_vt1709_6ch},
6250 	{ .id = 0x1106e717, .name = "VT1709 6-Ch",
6251 	  .patch = patch_vt1709_6ch},
6252 	{ .id = 0x1106e720, .name = "VT1708B 8-Ch",
6253 	  .patch = patch_vt1708B_8ch},
6254 	{ .id = 0x1106e721, .name = "VT1708B 8-Ch",
6255 	  .patch = patch_vt1708B_8ch},
6256 	{ .id = 0x1106e722, .name = "VT1708B 8-Ch",
6257 	  .patch = patch_vt1708B_8ch},
6258 	{ .id = 0x1106e723, .name = "VT1708B 8-Ch",
6259 	  .patch = patch_vt1708B_8ch},
6260 	{ .id = 0x1106e724, .name = "VT1708B 4-Ch",
6261 	  .patch = patch_vt1708B_4ch},
6262 	{ .id = 0x1106e725, .name = "VT1708B 4-Ch",
6263 	  .patch = patch_vt1708B_4ch},
6264 	{ .id = 0x1106e726, .name = "VT1708B 4-Ch",
6265 	  .patch = patch_vt1708B_4ch},
6266 	{ .id = 0x1106e727, .name = "VT1708B 4-Ch",
6267 	  .patch = patch_vt1708B_4ch},
6268 	{ .id = 0x11060397, .name = "VT1708S",
6269 	  .patch = patch_vt1708S},
6270 	{ .id = 0x11061397, .name = "VT1708S",
6271 	  .patch = patch_vt1708S},
6272 	{ .id = 0x11062397, .name = "VT1708S",
6273 	  .patch = patch_vt1708S},
6274 	{ .id = 0x11063397, .name = "VT1708S",
6275 	  .patch = patch_vt1708S},
6276 	{ .id = 0x11064397, .name = "VT1708S",
6277 	  .patch = patch_vt1708S},
6278 	{ .id = 0x11065397, .name = "VT1708S",
6279 	  .patch = patch_vt1708S},
6280 	{ .id = 0x11066397, .name = "VT1708S",
6281 	  .patch = patch_vt1708S},
6282 	{ .id = 0x11067397, .name = "VT1708S",
6283 	  .patch = patch_vt1708S},
6284 	{ .id = 0x11060398, .name = "VT1702",
6285 	  .patch = patch_vt1702},
6286 	{ .id = 0x11061398, .name = "VT1702",
6287 	  .patch = patch_vt1702},
6288 	{ .id = 0x11062398, .name = "VT1702",
6289 	  .patch = patch_vt1702},
6290 	{ .id = 0x11063398, .name = "VT1702",
6291 	  .patch = patch_vt1702},
6292 	{ .id = 0x11064398, .name = "VT1702",
6293 	  .patch = patch_vt1702},
6294 	{ .id = 0x11065398, .name = "VT1702",
6295 	  .patch = patch_vt1702},
6296 	{ .id = 0x11066398, .name = "VT1702",
6297 	  .patch = patch_vt1702},
6298 	{ .id = 0x11067398, .name = "VT1702",
6299 	  .patch = patch_vt1702},
6300 	{ .id = 0x11060428, .name = "VT1718S",
6301 	  .patch = patch_vt1718S},
6302 	{ .id = 0x11064428, .name = "VT1718S",
6303 	  .patch = patch_vt1718S},
6304 	{ .id = 0x11060441, .name = "VT2020",
6305 	  .patch = patch_vt1718S},
6306 	{ .id = 0x11064441, .name = "VT1828S",
6307 	  .patch = patch_vt1718S},
6308 	{ .id = 0x11060433, .name = "VT1716S",
6309 	  .patch = patch_vt1716S},
6310 	{ .id = 0x1106a721, .name = "VT1716S",
6311 	  .patch = patch_vt1716S},
6312 	{ .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
6313 	{ .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
6314 	{ .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
6315 	{ .id = 0x11060440, .name = "VT1818S",
6316 	  .patch = patch_vt1708S},
6317 	{} /* terminator */
6318 };
6319 
6320 MODULE_ALIAS("snd-hda-codec-id:1106*");
6321 
6322 static struct hda_codec_preset_list via_list = {
6323 	.preset = snd_hda_preset_via,
6324 	.owner = THIS_MODULE,
6325 };
6326 
6327 MODULE_LICENSE("GPL");
6328 MODULE_DESCRIPTION("VIA HD-audio codec");
6329 
6330 static int __init patch_via_init(void)
6331 {
6332 	return snd_hda_add_codec_preset(&via_list);
6333 }
6334 
6335 static void __exit patch_via_exit(void)
6336 {
6337 	snd_hda_delete_codec_preset(&via_list);
6338 }
6339 
6340 module_init(patch_via_init)
6341 module_exit(patch_via_exit)
6342