xref: /openbmc/linux/sound/pci/hda/patch_via.c (revision f7777dcc)
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * HD audio interface patch for VIA VT17xx/VT18xx/VT20xx codec
5  *
6  *  (C) 2006-2009 VIA Technology, Inc.
7  *  (C) 2006-2008 Takashi Iwai <tiwai@suse.de>
8  *
9  *  This driver is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This driver is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  */
23 
24 /* * * * * * * * * * * * * * Release History * * * * * * * * * * * * * * * * */
25 /*									     */
26 /* 2006-03-03  Lydia Wang  Create the basic patch to support VT1708 codec    */
27 /* 2006-03-14  Lydia Wang  Modify hard code for some pin widget nid	     */
28 /* 2006-08-02  Lydia Wang  Add support to VT1709 codec			     */
29 /* 2006-09-08  Lydia Wang  Fix internal loopback recording source select bug */
30 /* 2007-09-12  Lydia Wang  Add EAPD enable during driver initialization	     */
31 /* 2007-09-17  Lydia Wang  Add VT1708B codec support			    */
32 /* 2007-11-14  Lydia Wang  Add VT1708A codec HP and CD pin connect config    */
33 /* 2008-02-03  Lydia Wang  Fix Rear channels and Back channels inverse issue */
34 /* 2008-03-06  Lydia Wang  Add VT1702 codec and VT1708S codec support	     */
35 /* 2008-04-09  Lydia Wang  Add mute front speaker when HP plugin	     */
36 /* 2008-04-09  Lydia Wang  Add Independent HP feature			     */
37 /* 2008-05-28  Lydia Wang  Add second S/PDIF Out support for VT1702	     */
38 /* 2008-09-15  Logan Li	   Add VT1708S Mic Boost workaround/backdoor	     */
39 /* 2009-02-16  Logan Li	   Add support for VT1718S			     */
40 /* 2009-03-13  Logan Li	   Add support for VT1716S			     */
41 /* 2009-04-14  Lydai Wang  Add support for VT1828S and VT2020		     */
42 /* 2009-07-08  Lydia Wang  Add support for VT2002P			     */
43 /* 2009-07-21  Lydia Wang  Add support for VT1812			     */
44 /* 2009-09-19  Lydia Wang  Add support for VT1818S			     */
45 /*									     */
46 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
47 
48 
49 #include <linux/init.h>
50 #include <linux/delay.h>
51 #include <linux/slab.h>
52 #include <linux/module.h>
53 #include <sound/core.h>
54 #include <sound/asoundef.h>
55 #include "hda_codec.h"
56 #include "hda_local.h"
57 #include "hda_auto_parser.h"
58 #include "hda_jack.h"
59 #include "hda_generic.h"
60 
61 /* Pin Widget NID */
62 #define VT1708_HP_PIN_NID	0x20
63 #define VT1708_CD_PIN_NID	0x24
64 
65 enum VIA_HDA_CODEC {
66 	UNKNOWN = -1,
67 	VT1708,
68 	VT1709_10CH,
69 	VT1709_6CH,
70 	VT1708B_8CH,
71 	VT1708B_4CH,
72 	VT1708S,
73 	VT1708BCE,
74 	VT1702,
75 	VT1718S,
76 	VT1716S,
77 	VT2002P,
78 	VT1812,
79 	VT1802,
80 	VT1705CF,
81 	VT1808,
82 	CODEC_TYPES,
83 };
84 
85 #define VT2002P_COMPATIBLE(spec) \
86 	((spec)->codec_type == VT2002P ||\
87 	 (spec)->codec_type == VT1812 ||\
88 	 (spec)->codec_type == VT1802)
89 
90 struct via_spec {
91 	struct hda_gen_spec gen;
92 
93 	/* codec parameterization */
94 	const struct snd_kcontrol_new *mixers[6];
95 	unsigned int num_mixers;
96 
97 	const struct hda_verb *init_verbs[5];
98 	unsigned int num_iverbs;
99 
100 	/* HP mode source */
101 	unsigned int dmic_enabled;
102 	unsigned int no_pin_power_ctl;
103 	enum VIA_HDA_CODEC codec_type;
104 
105 	/* analog low-power control */
106 	bool alc_mode;
107 
108 	/* work to check hp jack state */
109 	int hp_work_active;
110 	int vt1708_jack_detect;
111 
112 	void (*set_widgets_power_state)(struct hda_codec *codec);
113 	unsigned int dac_stream_tag[4];
114 };
115 
116 static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
117 static void via_playback_pcm_hook(struct hda_pcm_stream *hinfo,
118 				  struct hda_codec *codec,
119 				  struct snd_pcm_substream *substream,
120 				  int action);
121 static void via_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *tbl);
122 
123 static struct via_spec *via_new_spec(struct hda_codec *codec)
124 {
125 	struct via_spec *spec;
126 
127 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
128 	if (spec == NULL)
129 		return NULL;
130 
131 	codec->spec = spec;
132 	snd_hda_gen_spec_init(&spec->gen);
133 	spec->codec_type = get_codec_type(codec);
134 	/* VT1708BCE & VT1708S are almost same */
135 	if (spec->codec_type == VT1708BCE)
136 		spec->codec_type = VT1708S;
137 	spec->no_pin_power_ctl = 1;
138 	spec->gen.indep_hp = 1;
139 	spec->gen.keep_eapd_on = 1;
140 	spec->gen.pcm_playback_hook = via_playback_pcm_hook;
141 	return spec;
142 }
143 
144 static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
145 {
146 	u32 vendor_id = codec->vendor_id;
147 	u16 ven_id = vendor_id >> 16;
148 	u16 dev_id = vendor_id & 0xffff;
149 	enum VIA_HDA_CODEC codec_type;
150 
151 	/* get codec type */
152 	if (ven_id != 0x1106)
153 		codec_type = UNKNOWN;
154 	else if (dev_id >= 0x1708 && dev_id <= 0x170b)
155 		codec_type = VT1708;
156 	else if (dev_id >= 0xe710 && dev_id <= 0xe713)
157 		codec_type = VT1709_10CH;
158 	else if (dev_id >= 0xe714 && dev_id <= 0xe717)
159 		codec_type = VT1709_6CH;
160 	else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
161 		codec_type = VT1708B_8CH;
162 		if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
163 			codec_type = VT1708BCE;
164 	} else if (dev_id >= 0xe724 && dev_id <= 0xe727)
165 		codec_type = VT1708B_4CH;
166 	else if ((dev_id & 0xfff) == 0x397
167 		 && (dev_id >> 12) < 8)
168 		codec_type = VT1708S;
169 	else if ((dev_id & 0xfff) == 0x398
170 		 && (dev_id >> 12) < 8)
171 		codec_type = VT1702;
172 	else if ((dev_id & 0xfff) == 0x428
173 		 && (dev_id >> 12) < 8)
174 		codec_type = VT1718S;
175 	else if (dev_id == 0x0433 || dev_id == 0xa721)
176 		codec_type = VT1716S;
177 	else if (dev_id == 0x0441 || dev_id == 0x4441)
178 		codec_type = VT1718S;
179 	else if (dev_id == 0x0438 || dev_id == 0x4438)
180 		codec_type = VT2002P;
181 	else if (dev_id == 0x0448)
182 		codec_type = VT1812;
183 	else if (dev_id == 0x0440)
184 		codec_type = VT1708S;
185 	else if ((dev_id & 0xfff) == 0x446)
186 		codec_type = VT1802;
187 	else if (dev_id == 0x4760)
188 		codec_type = VT1705CF;
189 	else if (dev_id == 0x4761 || dev_id == 0x4762)
190 		codec_type = VT1808;
191 	else
192 		codec_type = UNKNOWN;
193 	return codec_type;
194 };
195 
196 static void analog_low_current_mode(struct hda_codec *codec);
197 static bool is_aa_path_mute(struct hda_codec *codec);
198 
199 #define hp_detect_with_aa(codec) \
200 	(snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1 && \
201 	 !is_aa_path_mute(codec))
202 
203 static void vt1708_stop_hp_work(struct hda_codec *codec)
204 {
205 	struct via_spec *spec = codec->spec;
206 	if (spec->codec_type != VT1708 || !spec->gen.autocfg.hp_outs)
207 		return;
208 	if (spec->hp_work_active) {
209 		snd_hda_codec_write(codec, 0x1, 0, 0xf81, 1);
210 		codec->jackpoll_interval = 0;
211 		cancel_delayed_work_sync(&codec->jackpoll_work);
212 		spec->hp_work_active = false;
213 	}
214 }
215 
216 static void vt1708_update_hp_work(struct hda_codec *codec)
217 {
218 	struct via_spec *spec = codec->spec;
219 	if (spec->codec_type != VT1708 || !spec->gen.autocfg.hp_outs)
220 		return;
221 	if (spec->vt1708_jack_detect) {
222 		if (!spec->hp_work_active) {
223 			codec->jackpoll_interval = msecs_to_jiffies(100);
224 			snd_hda_codec_write(codec, 0x1, 0, 0xf81, 0);
225 			queue_delayed_work(codec->bus->workq,
226 					   &codec->jackpoll_work, 0);
227 			spec->hp_work_active = true;
228 		}
229 	} else if (!hp_detect_with_aa(codec))
230 		vt1708_stop_hp_work(codec);
231 }
232 
233 static void set_widgets_power_state(struct hda_codec *codec)
234 {
235 #if 0 /* FIXME: the assumed connections don't match always with the
236        * actual routes by the generic parser, so better to disable
237        * the control for safety.
238        */
239 	struct via_spec *spec = codec->spec;
240 	if (spec->set_widgets_power_state)
241 		spec->set_widgets_power_state(codec);
242 #endif
243 }
244 
245 static void update_power_state(struct hda_codec *codec, hda_nid_t nid,
246 			       unsigned int parm)
247 {
248 	if (snd_hda_check_power_state(codec, nid, parm))
249 		return;
250 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
251 }
252 
253 static void update_conv_power_state(struct hda_codec *codec, hda_nid_t nid,
254 			       unsigned int parm, unsigned int index)
255 {
256 	struct via_spec *spec = codec->spec;
257 	unsigned int format;
258 
259 	if (snd_hda_check_power_state(codec, nid, parm))
260 		return;
261 	format = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
262 	if (format && (spec->dac_stream_tag[index] != format))
263 		spec->dac_stream_tag[index] = format;
264 
265 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
266 	if (parm == AC_PWRST_D0) {
267 		format = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
268 		if (!format && (spec->dac_stream_tag[index] != format))
269 			snd_hda_codec_write(codec, nid, 0,
270 						  AC_VERB_SET_CHANNEL_STREAMID,
271 						  spec->dac_stream_tag[index]);
272 	}
273 }
274 
275 static bool smart51_enabled(struct hda_codec *codec)
276 {
277 	struct via_spec *spec = codec->spec;
278 	return spec->gen.ext_channel_count > 2;
279 }
280 
281 static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
282 {
283 	struct via_spec *spec = codec->spec;
284 	int i;
285 
286 	for (i = 0; i < spec->gen.multi_ios; i++)
287 		if (spec->gen.multi_io[i].pin == pin)
288 			return true;
289 	return false;
290 }
291 
292 static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
293 				unsigned int *affected_parm)
294 {
295 	unsigned parm;
296 	unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
297 	unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
298 		>> AC_DEFCFG_MISC_SHIFT
299 		& AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
300 	struct via_spec *spec = codec->spec;
301 	unsigned present = 0;
302 
303 	no_presence |= spec->no_pin_power_ctl;
304 	if (!no_presence)
305 		present = snd_hda_jack_detect(codec, nid);
306 	if ((smart51_enabled(codec) && is_smart51_pins(codec, nid))
307 	    || ((no_presence || present)
308 		&& get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
309 		*affected_parm = AC_PWRST_D0; /* if it's connected */
310 		parm = AC_PWRST_D0;
311 	} else
312 		parm = AC_PWRST_D3;
313 
314 	update_power_state(codec, nid, parm);
315 }
316 
317 static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
318 				  struct snd_ctl_elem_info *uinfo)
319 {
320 	return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
321 }
322 
323 static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
324 				 struct snd_ctl_elem_value *ucontrol)
325 {
326 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
327 	struct via_spec *spec = codec->spec;
328 	ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
329 	return 0;
330 }
331 
332 static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
333 				 struct snd_ctl_elem_value *ucontrol)
334 {
335 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
336 	struct via_spec *spec = codec->spec;
337 	unsigned int val = !ucontrol->value.enumerated.item[0];
338 
339 	if (val == spec->no_pin_power_ctl)
340 		return 0;
341 	spec->no_pin_power_ctl = val;
342 	set_widgets_power_state(codec);
343 	analog_low_current_mode(codec);
344 	return 1;
345 }
346 
347 static const struct snd_kcontrol_new via_pin_power_ctl_enum[] = {
348 	{
349 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
350 	.name = "Dynamic Power-Control",
351 	.info = via_pin_power_ctl_info,
352 	.get = via_pin_power_ctl_get,
353 	.put = via_pin_power_ctl_put,
354 	},
355 	{} /* terminator */
356 };
357 
358 
359 /* check AA path's mute status */
360 static bool is_aa_path_mute(struct hda_codec *codec)
361 {
362 	struct via_spec *spec = codec->spec;
363 	const struct hda_amp_list *p;
364 	int ch, v;
365 
366 	p = spec->gen.loopback.amplist;
367 	if (!p)
368 		return true;
369 	for (; p->nid; p++) {
370 		for (ch = 0; ch < 2; ch++) {
371 			v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
372 						   p->idx);
373 			if (!(v & HDA_AMP_MUTE) && v > 0)
374 				return false;
375 		}
376 	}
377 	return true;
378 }
379 
380 /* enter/exit analog low-current mode */
381 static void __analog_low_current_mode(struct hda_codec *codec, bool force)
382 {
383 	struct via_spec *spec = codec->spec;
384 	bool enable;
385 	unsigned int verb, parm;
386 
387 	if (spec->no_pin_power_ctl)
388 		enable = false;
389 	else
390 		enable = is_aa_path_mute(codec) && !spec->gen.active_streams;
391 	if (enable == spec->alc_mode && !force)
392 		return;
393 	spec->alc_mode = enable;
394 
395 	/* decide low current mode's verb & parameter */
396 	switch (spec->codec_type) {
397 	case VT1708B_8CH:
398 	case VT1708B_4CH:
399 		verb = 0xf70;
400 		parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
401 		break;
402 	case VT1708S:
403 	case VT1718S:
404 	case VT1716S:
405 		verb = 0xf73;
406 		parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
407 		break;
408 	case VT1702:
409 		verb = 0xf73;
410 		parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
411 		break;
412 	case VT2002P:
413 	case VT1812:
414 	case VT1802:
415 		verb = 0xf93;
416 		parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
417 		break;
418 	case VT1705CF:
419 	case VT1808:
420 		verb = 0xf82;
421 		parm = enable ? 0x00 : 0xe0;  /* 0x00: 4/40x, 0xe0: 1x */
422 		break;
423 	default:
424 		return;		/* other codecs are not supported */
425 	}
426 	/* send verb */
427 	snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
428 }
429 
430 static void analog_low_current_mode(struct hda_codec *codec)
431 {
432 	return __analog_low_current_mode(codec, false);
433 }
434 
435 static int via_build_controls(struct hda_codec *codec)
436 {
437 	struct via_spec *spec = codec->spec;
438 	int err, i;
439 
440 	err = snd_hda_gen_build_controls(codec);
441 	if (err < 0)
442 		return err;
443 
444 	if (spec->set_widgets_power_state)
445 		spec->mixers[spec->num_mixers++] = via_pin_power_ctl_enum;
446 
447 	for (i = 0; i < spec->num_mixers; i++) {
448 		err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
449 		if (err < 0)
450 			return err;
451 	}
452 
453 	return 0;
454 }
455 
456 static void via_playback_pcm_hook(struct hda_pcm_stream *hinfo,
457 				  struct hda_codec *codec,
458 				  struct snd_pcm_substream *substream,
459 				  int action)
460 {
461 	analog_low_current_mode(codec);
462 	vt1708_update_hp_work(codec);
463 }
464 
465 static void via_free(struct hda_codec *codec)
466 {
467 	struct via_spec *spec = codec->spec;
468 
469 	if (!spec)
470 		return;
471 
472 	vt1708_stop_hp_work(codec);
473 	snd_hda_gen_spec_free(&spec->gen);
474 	kfree(spec);
475 }
476 
477 #ifdef CONFIG_PM
478 static int via_suspend(struct hda_codec *codec)
479 {
480 	struct via_spec *spec = codec->spec;
481 	vt1708_stop_hp_work(codec);
482 
483 	/* Fix pop noise on headphones */
484 	if (spec->codec_type == VT1802)
485 		snd_hda_shutup_pins(codec);
486 
487 	return 0;
488 }
489 #endif
490 
491 #ifdef CONFIG_PM
492 static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
493 {
494 	struct via_spec *spec = codec->spec;
495 	set_widgets_power_state(codec);
496 	analog_low_current_mode(codec);
497 	vt1708_update_hp_work(codec);
498 	return snd_hda_check_amp_list_power(codec, &spec->gen.loopback, nid);
499 }
500 #endif
501 
502 /*
503  */
504 
505 static int via_init(struct hda_codec *codec);
506 
507 static const struct hda_codec_ops via_patch_ops = {
508 	.build_controls = via_build_controls,
509 	.build_pcms = snd_hda_gen_build_pcms,
510 	.init = via_init,
511 	.free = via_free,
512 	.unsol_event = snd_hda_jack_unsol_event,
513 #ifdef CONFIG_PM
514 	.suspend = via_suspend,
515 	.check_power_status = via_check_power_status,
516 #endif
517 };
518 
519 
520 static const struct hda_verb vt1708_init_verbs[] = {
521 	/* power down jack detect function */
522 	{0x1, 0xf81, 0x1},
523 	{ }
524 };
525 static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
526 {
527 	unsigned int def_conf;
528 	unsigned char seqassoc;
529 
530 	def_conf = snd_hda_codec_get_pincfg(codec, nid);
531 	seqassoc = (unsigned char) get_defcfg_association(def_conf);
532 	seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
533 	if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
534 	    && (seqassoc == 0xf0 || seqassoc == 0xff)) {
535 		def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
536 		snd_hda_codec_set_pincfg(codec, nid, def_conf);
537 	}
538 
539 	return;
540 }
541 
542 static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
543 				     struct snd_ctl_elem_value *ucontrol)
544 {
545 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
546 	struct via_spec *spec = codec->spec;
547 
548 	if (spec->codec_type != VT1708)
549 		return 0;
550 	ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
551 	return 0;
552 }
553 
554 static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
555 				     struct snd_ctl_elem_value *ucontrol)
556 {
557 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
558 	struct via_spec *spec = codec->spec;
559 	int val;
560 
561 	if (spec->codec_type != VT1708)
562 		return 0;
563 	val = !!ucontrol->value.integer.value[0];
564 	if (spec->vt1708_jack_detect == val)
565 		return 0;
566 	spec->vt1708_jack_detect = val;
567 	vt1708_update_hp_work(codec);
568 	return 1;
569 }
570 
571 static const struct snd_kcontrol_new vt1708_jack_detect_ctl[] = {
572 	{
573 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
574 	.name = "Jack Detect",
575 	.count = 1,
576 	.info = snd_ctl_boolean_mono_info,
577 	.get = vt1708_jack_detect_get,
578 	.put = vt1708_jack_detect_put,
579 	},
580 	{} /* terminator */
581 };
582 
583 static void via_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *tbl)
584 {
585 	set_widgets_power_state(codec);
586 	snd_hda_gen_hp_automute(codec, tbl);
587 }
588 
589 static void via_line_automute(struct hda_codec *codec, struct hda_jack_tbl *tbl)
590 {
591 	set_widgets_power_state(codec);
592 	snd_hda_gen_line_automute(codec, tbl);
593 }
594 
595 static void via_jack_powerstate_event(struct hda_codec *codec, struct hda_jack_tbl *tbl)
596 {
597 	set_widgets_power_state(codec);
598 }
599 
600 #define VIA_JACK_EVENT	(HDA_GEN_LAST_EVENT + 1)
601 
602 static void via_set_jack_unsol_events(struct hda_codec *codec)
603 {
604 	struct via_spec *spec = codec->spec;
605 	struct auto_pin_cfg *cfg = &spec->gen.autocfg;
606 	hda_nid_t pin;
607 	int i;
608 
609 	spec->gen.hp_automute_hook = via_hp_automute;
610 	if (cfg->speaker_pins[0])
611 		spec->gen.line_automute_hook = via_line_automute;
612 
613 	for (i = 0; i < cfg->line_outs; i++) {
614 		pin = cfg->line_out_pins[i];
615 		if (pin && !snd_hda_jack_tbl_get(codec, pin) &&
616 		    is_jack_detectable(codec, pin))
617 			snd_hda_jack_detect_enable_callback(codec, pin,
618 							    VIA_JACK_EVENT,
619 							    via_jack_powerstate_event);
620 	}
621 
622 	for (i = 0; i < cfg->num_inputs; i++) {
623 		pin = cfg->line_out_pins[i];
624 		if (pin && !snd_hda_jack_tbl_get(codec, pin) &&
625 		    is_jack_detectable(codec, pin))
626 			snd_hda_jack_detect_enable_callback(codec, pin,
627 							    VIA_JACK_EVENT,
628 							    via_jack_powerstate_event);
629 	}
630 }
631 
632 static const struct badness_table via_main_out_badness = {
633 	.no_primary_dac = 0x10000,
634 	.no_dac = 0x4000,
635 	.shared_primary = 0x10000,
636 	.shared_surr = 0x20,
637 	.shared_clfe = 0x20,
638 	.shared_surr_main = 0x20,
639 };
640 static const struct badness_table via_extra_out_badness = {
641 	.no_primary_dac = 0x4000,
642 	.no_dac = 0x4000,
643 	.shared_primary = 0x12,
644 	.shared_surr = 0x20,
645 	.shared_clfe = 0x20,
646 	.shared_surr_main = 0x10,
647 };
648 
649 static int via_parse_auto_config(struct hda_codec *codec)
650 {
651 	struct via_spec *spec = codec->spec;
652 	int err;
653 
654 	spec->gen.main_out_badness = &via_main_out_badness;
655 	spec->gen.extra_out_badness = &via_extra_out_badness;
656 
657 	err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
658 	if (err < 0)
659 		return err;
660 
661 	err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
662 	if (err < 0)
663 		return err;
664 
665 	via_set_jack_unsol_events(codec);
666 	return 0;
667 }
668 
669 static int via_init(struct hda_codec *codec)
670 {
671 	struct via_spec *spec = codec->spec;
672 	int i;
673 
674 	for (i = 0; i < spec->num_iverbs; i++)
675 		snd_hda_sequence_write(codec, spec->init_verbs[i]);
676 
677 	/* init power states */
678 	set_widgets_power_state(codec);
679 	__analog_low_current_mode(codec, true);
680 
681 	snd_hda_gen_init(codec);
682 
683 	vt1708_update_hp_work(codec);
684 
685 	return 0;
686 }
687 
688 static int vt1708_build_controls(struct hda_codec *codec)
689 {
690 	/* In order not to create "Phantom Jack" controls,
691 	   temporary enable jackpoll */
692 	int err;
693 	int old_interval = codec->jackpoll_interval;
694 	codec->jackpoll_interval = msecs_to_jiffies(100);
695 	err = via_build_controls(codec);
696 	codec->jackpoll_interval = old_interval;
697 	return err;
698 }
699 
700 static int vt1708_build_pcms(struct hda_codec *codec)
701 {
702 	struct via_spec *spec = codec->spec;
703 	int i, err;
704 
705 	err = snd_hda_gen_build_pcms(codec);
706 	if (err < 0 || codec->vendor_id != 0x11061708)
707 		return err;
708 
709 	/* We got noisy outputs on the right channel on VT1708 when
710 	 * 24bit samples are used.  Until any workaround is found,
711 	 * disable the 24bit format, so far.
712 	 */
713 	for (i = 0; i < codec->num_pcms; i++) {
714 		struct hda_pcm *info = &spec->gen.pcm_rec[i];
715 		if (!info->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams ||
716 		    info->pcm_type != HDA_PCM_TYPE_AUDIO)
717 			continue;
718 		info->stream[SNDRV_PCM_STREAM_PLAYBACK].formats =
719 			SNDRV_PCM_FMTBIT_S16_LE;
720 	}
721 
722 	return 0;
723 }
724 
725 static int patch_vt1708(struct hda_codec *codec)
726 {
727 	struct via_spec *spec;
728 	int err;
729 
730 	/* create a codec specific record */
731 	spec = via_new_spec(codec);
732 	if (spec == NULL)
733 		return -ENOMEM;
734 
735 	spec->gen.mixer_nid = 0x17;
736 
737 	/* set jackpoll_interval while parsing the codec */
738 	codec->jackpoll_interval = msecs_to_jiffies(100);
739 	spec->vt1708_jack_detect = 1;
740 
741 	/* don't support the input jack switching due to lack of unsol event */
742 	/* (it may work with polling, though, but it needs testing) */
743 	spec->gen.suppress_auto_mic = 1;
744 	/* Some machines show the broken speaker mute */
745 	spec->gen.auto_mute_via_amp = 1;
746 
747 	/* Add HP and CD pin config connect bit re-config action */
748 	vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
749 	vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
750 
751 	/* automatic parse from the BIOS config */
752 	err = via_parse_auto_config(codec);
753 	if (err < 0) {
754 		via_free(codec);
755 		return err;
756 	}
757 
758 	/* add jack detect on/off control */
759 	spec->mixers[spec->num_mixers++] = vt1708_jack_detect_ctl;
760 
761 	spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
762 
763 	codec->patch_ops = via_patch_ops;
764 	codec->patch_ops.build_controls = vt1708_build_controls;
765 	codec->patch_ops.build_pcms = vt1708_build_pcms;
766 
767 	/* clear jackpoll_interval again; it's set dynamically */
768 	codec->jackpoll_interval = 0;
769 
770 	return 0;
771 }
772 
773 static int patch_vt1709(struct hda_codec *codec)
774 {
775 	struct via_spec *spec;
776 	int err;
777 
778 	/* create a codec specific record */
779 	spec = via_new_spec(codec);
780 	if (spec == NULL)
781 		return -ENOMEM;
782 
783 	spec->gen.mixer_nid = 0x18;
784 
785 	err = via_parse_auto_config(codec);
786 	if (err < 0) {
787 		via_free(codec);
788 		return err;
789 	}
790 
791 	codec->patch_ops = via_patch_ops;
792 
793 	return 0;
794 }
795 
796 static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
797 {
798 	struct via_spec *spec = codec->spec;
799 	int imux_is_smixer;
800 	unsigned int parm;
801 	int is_8ch = 0;
802 	if ((spec->codec_type != VT1708B_4CH) &&
803 	    (codec->vendor_id != 0x11064397))
804 		is_8ch = 1;
805 
806 	/* SW0 (17h) = stereo mixer */
807 	imux_is_smixer =
808 	(snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
809 	 == ((spec->codec_type == VT1708S) ? 5 : 0));
810 	/* inputs */
811 	/* PW 1/2/5 (1ah/1bh/1eh) */
812 	parm = AC_PWRST_D3;
813 	set_pin_power_state(codec, 0x1a, &parm);
814 	set_pin_power_state(codec, 0x1b, &parm);
815 	set_pin_power_state(codec, 0x1e, &parm);
816 	if (imux_is_smixer)
817 		parm = AC_PWRST_D0;
818 	/* SW0 (17h), AIW 0/1 (13h/14h) */
819 	update_power_state(codec, 0x17, parm);
820 	update_power_state(codec, 0x13, parm);
821 	update_power_state(codec, 0x14, parm);
822 
823 	/* outputs */
824 	/* PW0 (19h), SW1 (18h), AOW1 (11h) */
825 	parm = AC_PWRST_D3;
826 	set_pin_power_state(codec, 0x19, &parm);
827 	if (smart51_enabled(codec))
828 		set_pin_power_state(codec, 0x1b, &parm);
829 	update_power_state(codec, 0x18, parm);
830 	update_power_state(codec, 0x11, parm);
831 
832 	/* PW6 (22h), SW2 (26h), AOW2 (24h) */
833 	if (is_8ch) {
834 		parm = AC_PWRST_D3;
835 		set_pin_power_state(codec, 0x22, &parm);
836 		if (smart51_enabled(codec))
837 			set_pin_power_state(codec, 0x1a, &parm);
838 		update_power_state(codec, 0x26, parm);
839 		update_power_state(codec, 0x24, parm);
840 	} else if (codec->vendor_id == 0x11064397) {
841 		/* PW7(23h), SW2(27h), AOW2(25h) */
842 		parm = AC_PWRST_D3;
843 		set_pin_power_state(codec, 0x23, &parm);
844 		if (smart51_enabled(codec))
845 			set_pin_power_state(codec, 0x1a, &parm);
846 		update_power_state(codec, 0x27, parm);
847 		update_power_state(codec, 0x25, parm);
848 	}
849 
850 	/* PW 3/4/7 (1ch/1dh/23h) */
851 	parm = AC_PWRST_D3;
852 	/* force to D0 for internal Speaker */
853 	set_pin_power_state(codec, 0x1c, &parm);
854 	set_pin_power_state(codec, 0x1d, &parm);
855 	if (is_8ch)
856 		set_pin_power_state(codec, 0x23, &parm);
857 
858 	/* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
859 	update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
860 	update_power_state(codec, 0x10, parm);
861 	if (is_8ch) {
862 		update_power_state(codec, 0x25, parm);
863 		update_power_state(codec, 0x27, parm);
864 	} else if (codec->vendor_id == 0x11064397 && spec->gen.indep_hp_enabled)
865 		update_power_state(codec, 0x25, parm);
866 }
867 
868 static int patch_vt1708S(struct hda_codec *codec);
869 static int patch_vt1708B(struct hda_codec *codec)
870 {
871 	struct via_spec *spec;
872 	int err;
873 
874 	if (get_codec_type(codec) == VT1708BCE)
875 		return patch_vt1708S(codec);
876 
877 	/* create a codec specific record */
878 	spec = via_new_spec(codec);
879 	if (spec == NULL)
880 		return -ENOMEM;
881 
882 	spec->gen.mixer_nid = 0x16;
883 
884 	/* automatic parse from the BIOS config */
885 	err = via_parse_auto_config(codec);
886 	if (err < 0) {
887 		via_free(codec);
888 		return err;
889 	}
890 
891 	codec->patch_ops = via_patch_ops;
892 
893 	spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
894 
895 	return 0;
896 }
897 
898 /* Patch for VT1708S */
899 static const struct hda_verb vt1708S_init_verbs[] = {
900 	/* Enable Mic Boost Volume backdoor */
901 	{0x1, 0xf98, 0x1},
902 	/* don't bybass mixer */
903 	{0x1, 0xf88, 0xc0},
904 	{ }
905 };
906 
907 static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
908 			       int offset, int num_steps, int step_size)
909 {
910 	snd_hda_override_wcaps(codec, pin,
911 			       get_wcaps(codec, pin) | AC_WCAP_IN_AMP);
912 	snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
913 				  (offset << AC_AMPCAP_OFFSET_SHIFT) |
914 				  (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
915 				  (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
916 				  (0 << AC_AMPCAP_MUTE_SHIFT));
917 }
918 
919 static int patch_vt1708S(struct hda_codec *codec)
920 {
921 	struct via_spec *spec;
922 	int err;
923 
924 	/* create a codec specific record */
925 	spec = via_new_spec(codec);
926 	if (spec == NULL)
927 		return -ENOMEM;
928 
929 	spec->gen.mixer_nid = 0x16;
930 	override_mic_boost(codec, 0x1a, 0, 3, 40);
931 	override_mic_boost(codec, 0x1e, 0, 3, 40);
932 
933 	/* correct names for VT1708BCE */
934 	if (get_codec_type(codec) == VT1708BCE)	{
935 		kfree(codec->chip_name);
936 		codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
937 		snprintf(codec->bus->card->mixername,
938 			 sizeof(codec->bus->card->mixername),
939 			 "%s %s", codec->vendor_name, codec->chip_name);
940 	}
941 	/* correct names for VT1705 */
942 	if (codec->vendor_id == 0x11064397)	{
943 		kfree(codec->chip_name);
944 		codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
945 		snprintf(codec->bus->card->mixername,
946 			 sizeof(codec->bus->card->mixername),
947 			 "%s %s", codec->vendor_name, codec->chip_name);
948 	}
949 
950 	/* automatic parse from the BIOS config */
951 	err = via_parse_auto_config(codec);
952 	if (err < 0) {
953 		via_free(codec);
954 		return err;
955 	}
956 
957 	spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
958 
959 	codec->patch_ops = via_patch_ops;
960 
961 	spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
962 	return 0;
963 }
964 
965 /* Patch for VT1702 */
966 
967 static const struct hda_verb vt1702_init_verbs[] = {
968 	/* mixer enable */
969 	{0x1, 0xF88, 0x3},
970 	/* GPIO 0~2 */
971 	{0x1, 0xF82, 0x3F},
972 	{ }
973 };
974 
975 static void set_widgets_power_state_vt1702(struct hda_codec *codec)
976 {
977 	int imux_is_smixer =
978 	snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
979 	unsigned int parm;
980 	/* inputs */
981 	/* PW 1/2/5 (14h/15h/18h) */
982 	parm = AC_PWRST_D3;
983 	set_pin_power_state(codec, 0x14, &parm);
984 	set_pin_power_state(codec, 0x15, &parm);
985 	set_pin_power_state(codec, 0x18, &parm);
986 	if (imux_is_smixer)
987 		parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
988 	/* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
989 	update_power_state(codec, 0x13, parm);
990 	update_power_state(codec, 0x12, parm);
991 	update_power_state(codec, 0x1f, parm);
992 	update_power_state(codec, 0x20, parm);
993 
994 	/* outputs */
995 	/* PW 3/4 (16h/17h) */
996 	parm = AC_PWRST_D3;
997 	set_pin_power_state(codec, 0x17, &parm);
998 	set_pin_power_state(codec, 0x16, &parm);
999 	/* MW0 (1ah), AOW 0/1 (10h/1dh) */
1000 	update_power_state(codec, 0x1a, imux_is_smixer ? AC_PWRST_D0 : parm);
1001 	update_power_state(codec, 0x10, parm);
1002 	update_power_state(codec, 0x1d, parm);
1003 }
1004 
1005 static int patch_vt1702(struct hda_codec *codec)
1006 {
1007 	struct via_spec *spec;
1008 	int err;
1009 
1010 	/* create a codec specific record */
1011 	spec = via_new_spec(codec);
1012 	if (spec == NULL)
1013 		return -ENOMEM;
1014 
1015 	spec->gen.mixer_nid = 0x1a;
1016 
1017 	/* limit AA path volume to 0 dB */
1018 	snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
1019 				  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
1020 				  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
1021 				  (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
1022 				  (1 << AC_AMPCAP_MUTE_SHIFT));
1023 
1024 	/* automatic parse from the BIOS config */
1025 	err = via_parse_auto_config(codec);
1026 	if (err < 0) {
1027 		via_free(codec);
1028 		return err;
1029 	}
1030 
1031 	spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
1032 
1033 	codec->patch_ops = via_patch_ops;
1034 
1035 	spec->set_widgets_power_state =  set_widgets_power_state_vt1702;
1036 	return 0;
1037 }
1038 
1039 /* Patch for VT1718S */
1040 
1041 static const struct hda_verb vt1718S_init_verbs[] = {
1042 	/* Enable MW0 adjust Gain 5 */
1043 	{0x1, 0xfb2, 0x10},
1044 	/* Enable Boost Volume backdoor */
1045 	{0x1, 0xf88, 0x8},
1046 
1047 	{ }
1048 };
1049 
1050 static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
1051 {
1052 	struct via_spec *spec = codec->spec;
1053 	int imux_is_smixer;
1054 	unsigned int parm, parm2;
1055 	/* MUX6 (1eh) = stereo mixer */
1056 	imux_is_smixer =
1057 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
1058 	/* inputs */
1059 	/* PW 5/6/7 (29h/2ah/2bh) */
1060 	parm = AC_PWRST_D3;
1061 	set_pin_power_state(codec, 0x29, &parm);
1062 	set_pin_power_state(codec, 0x2a, &parm);
1063 	set_pin_power_state(codec, 0x2b, &parm);
1064 	if (imux_is_smixer)
1065 		parm = AC_PWRST_D0;
1066 	/* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
1067 	update_power_state(codec, 0x1e, parm);
1068 	update_power_state(codec, 0x1f, parm);
1069 	update_power_state(codec, 0x10, parm);
1070 	update_power_state(codec, 0x11, parm);
1071 
1072 	/* outputs */
1073 	/* PW3 (27h), MW2 (1ah), AOW3 (bh) */
1074 	parm = AC_PWRST_D3;
1075 	set_pin_power_state(codec, 0x27, &parm);
1076 	update_power_state(codec, 0x1a, parm);
1077 	parm2 = parm; /* for pin 0x0b */
1078 
1079 	/* PW2 (26h), AOW2 (ah) */
1080 	parm = AC_PWRST_D3;
1081 	set_pin_power_state(codec, 0x26, &parm);
1082 	if (smart51_enabled(codec))
1083 		set_pin_power_state(codec, 0x2b, &parm);
1084 	update_power_state(codec, 0xa, parm);
1085 
1086 	/* PW0 (24h), AOW0 (8h) */
1087 	parm = AC_PWRST_D3;
1088 	set_pin_power_state(codec, 0x24, &parm);
1089 	if (!spec->gen.indep_hp_enabled) /* check for redirected HP */
1090 		set_pin_power_state(codec, 0x28, &parm);
1091 	update_power_state(codec, 0x8, parm);
1092 	if (!spec->gen.indep_hp_enabled && parm2 != AC_PWRST_D3)
1093 		parm = parm2;
1094 	update_power_state(codec, 0xb, parm);
1095 	/* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
1096 	update_power_state(codec, 0x21, imux_is_smixer ? AC_PWRST_D0 : parm);
1097 
1098 	/* PW1 (25h), AOW1 (9h) */
1099 	parm = AC_PWRST_D3;
1100 	set_pin_power_state(codec, 0x25, &parm);
1101 	if (smart51_enabled(codec))
1102 		set_pin_power_state(codec, 0x2a, &parm);
1103 	update_power_state(codec, 0x9, parm);
1104 
1105 	if (spec->gen.indep_hp_enabled) {
1106 		/* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
1107 		parm = AC_PWRST_D3;
1108 		set_pin_power_state(codec, 0x28, &parm);
1109 		update_power_state(codec, 0x1b, parm);
1110 		update_power_state(codec, 0x34, parm);
1111 		update_power_state(codec, 0xc, parm);
1112 	}
1113 }
1114 
1115 /* Add a connection to the primary DAC from AA-mixer for some codecs
1116  * This isn't listed from the raw info, but the chip has a secret connection.
1117  */
1118 static int add_secret_dac_path(struct hda_codec *codec)
1119 {
1120 	struct via_spec *spec = codec->spec;
1121 	int i, nums;
1122 	hda_nid_t conn[8];
1123 	hda_nid_t nid;
1124 
1125 	if (!spec->gen.mixer_nid)
1126 		return 0;
1127 	nums = snd_hda_get_connections(codec, spec->gen.mixer_nid, conn,
1128 				       ARRAY_SIZE(conn) - 1);
1129 	for (i = 0; i < nums; i++) {
1130 		if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT)
1131 			return 0;
1132 	}
1133 
1134 	/* find the primary DAC and add to the connection list */
1135 	nid = codec->start_nid;
1136 	for (i = 0; i < codec->num_nodes; i++, nid++) {
1137 		unsigned int caps = get_wcaps(codec, nid);
1138 		if (get_wcaps_type(caps) == AC_WID_AUD_OUT &&
1139 		    !(caps & AC_WCAP_DIGITAL)) {
1140 			conn[nums++] = nid;
1141 			return snd_hda_override_conn_list(codec,
1142 							  spec->gen.mixer_nid,
1143 							  nums, conn);
1144 		}
1145 	}
1146 	return 0;
1147 }
1148 
1149 
1150 static int patch_vt1718S(struct hda_codec *codec)
1151 {
1152 	struct via_spec *spec;
1153 	int err;
1154 
1155 	/* create a codec specific record */
1156 	spec = via_new_spec(codec);
1157 	if (spec == NULL)
1158 		return -ENOMEM;
1159 
1160 	spec->gen.mixer_nid = 0x21;
1161 	override_mic_boost(codec, 0x2b, 0, 3, 40);
1162 	override_mic_boost(codec, 0x29, 0, 3, 40);
1163 	add_secret_dac_path(codec);
1164 
1165 	/* automatic parse from the BIOS config */
1166 	err = via_parse_auto_config(codec);
1167 	if (err < 0) {
1168 		via_free(codec);
1169 		return err;
1170 	}
1171 
1172 	spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
1173 
1174 	codec->patch_ops = via_patch_ops;
1175 
1176 	spec->set_widgets_power_state =  set_widgets_power_state_vt1718S;
1177 
1178 	return 0;
1179 }
1180 
1181 /* Patch for VT1716S */
1182 
1183 static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
1184 			    struct snd_ctl_elem_info *uinfo)
1185 {
1186 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1187 	uinfo->count = 1;
1188 	uinfo->value.integer.min = 0;
1189 	uinfo->value.integer.max = 1;
1190 	return 0;
1191 }
1192 
1193 static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
1194 			   struct snd_ctl_elem_value *ucontrol)
1195 {
1196 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1197 	int index = 0;
1198 
1199 	index = snd_hda_codec_read(codec, 0x26, 0,
1200 					       AC_VERB_GET_CONNECT_SEL, 0);
1201 	if (index != -1)
1202 		*ucontrol->value.integer.value = index;
1203 
1204 	return 0;
1205 }
1206 
1207 static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
1208 			   struct snd_ctl_elem_value *ucontrol)
1209 {
1210 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1211 	struct via_spec *spec = codec->spec;
1212 	int index = *ucontrol->value.integer.value;
1213 
1214 	snd_hda_codec_write(codec, 0x26, 0,
1215 					       AC_VERB_SET_CONNECT_SEL, index);
1216 	spec->dmic_enabled = index;
1217 	set_widgets_power_state(codec);
1218 	return 1;
1219 }
1220 
1221 static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
1222 	HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
1223 	{
1224 	 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1225 	 .name = "Digital Mic Capture Switch",
1226 	 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
1227 	 .count = 1,
1228 	 .info = vt1716s_dmic_info,
1229 	 .get = vt1716s_dmic_get,
1230 	 .put = vt1716s_dmic_put,
1231 	 },
1232 	{}			/* end */
1233 };
1234 
1235 
1236 /* mono-out mixer elements */
1237 static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
1238 	HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
1239 	{ } /* end */
1240 };
1241 
1242 static const struct hda_verb vt1716S_init_verbs[] = {
1243 	/* Enable Boost Volume backdoor */
1244 	{0x1, 0xf8a, 0x80},
1245 	/* don't bybass mixer */
1246 	{0x1, 0xf88, 0xc0},
1247 	/* Enable mono output */
1248 	{0x1, 0xf90, 0x08},
1249 	{ }
1250 };
1251 
1252 static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
1253 {
1254 	struct via_spec *spec = codec->spec;
1255 	int imux_is_smixer;
1256 	unsigned int parm;
1257 	unsigned int mono_out, present;
1258 	/* SW0 (17h) = stereo mixer */
1259 	imux_is_smixer =
1260 	(snd_hda_codec_read(codec, 0x17, 0,
1261 			    AC_VERB_GET_CONNECT_SEL, 0x00) ==  5);
1262 	/* inputs */
1263 	/* PW 1/2/5 (1ah/1bh/1eh) */
1264 	parm = AC_PWRST_D3;
1265 	set_pin_power_state(codec, 0x1a, &parm);
1266 	set_pin_power_state(codec, 0x1b, &parm);
1267 	set_pin_power_state(codec, 0x1e, &parm);
1268 	if (imux_is_smixer)
1269 		parm = AC_PWRST_D0;
1270 	/* SW0 (17h), AIW0(13h) */
1271 	update_power_state(codec, 0x17, parm);
1272 	update_power_state(codec, 0x13, parm);
1273 
1274 	parm = AC_PWRST_D3;
1275 	set_pin_power_state(codec, 0x1e, &parm);
1276 	/* PW11 (22h) */
1277 	if (spec->dmic_enabled)
1278 		set_pin_power_state(codec, 0x22, &parm);
1279 	else
1280 		update_power_state(codec, 0x22, AC_PWRST_D3);
1281 
1282 	/* SW2(26h), AIW1(14h) */
1283 	update_power_state(codec, 0x26, parm);
1284 	update_power_state(codec, 0x14, parm);
1285 
1286 	/* outputs */
1287 	/* PW0 (19h), SW1 (18h), AOW1 (11h) */
1288 	parm = AC_PWRST_D3;
1289 	set_pin_power_state(codec, 0x19, &parm);
1290 	/* Smart 5.1 PW2(1bh) */
1291 	if (smart51_enabled(codec))
1292 		set_pin_power_state(codec, 0x1b, &parm);
1293 	update_power_state(codec, 0x18, parm);
1294 	update_power_state(codec, 0x11, parm);
1295 
1296 	/* PW7 (23h), SW3 (27h), AOW3 (25h) */
1297 	parm = AC_PWRST_D3;
1298 	set_pin_power_state(codec, 0x23, &parm);
1299 	/* Smart 5.1 PW1(1ah) */
1300 	if (smart51_enabled(codec))
1301 		set_pin_power_state(codec, 0x1a, &parm);
1302 	update_power_state(codec, 0x27, parm);
1303 
1304 	/* Smart 5.1 PW5(1eh) */
1305 	if (smart51_enabled(codec))
1306 		set_pin_power_state(codec, 0x1e, &parm);
1307 	update_power_state(codec, 0x25, parm);
1308 
1309 	/* Mono out */
1310 	/* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
1311 	present = snd_hda_jack_detect(codec, 0x1c);
1312 
1313 	if (present)
1314 		mono_out = 0;
1315 	else {
1316 		present = snd_hda_jack_detect(codec, 0x1d);
1317 		if (!spec->gen.indep_hp_enabled && present)
1318 			mono_out = 0;
1319 		else
1320 			mono_out = 1;
1321 	}
1322 	parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
1323 	update_power_state(codec, 0x28, parm);
1324 	update_power_state(codec, 0x29, parm);
1325 	update_power_state(codec, 0x2a, parm);
1326 
1327 	/* PW 3/4 (1ch/1dh) */
1328 	parm = AC_PWRST_D3;
1329 	set_pin_power_state(codec, 0x1c, &parm);
1330 	set_pin_power_state(codec, 0x1d, &parm);
1331 	/* HP Independent Mode, power on AOW3 */
1332 	if (spec->gen.indep_hp_enabled)
1333 		update_power_state(codec, 0x25, parm);
1334 
1335 	/* force to D0 for internal Speaker */
1336 	/* MW0 (16h), AOW0 (10h) */
1337 	update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
1338 	update_power_state(codec, 0x10, mono_out ? AC_PWRST_D0 : parm);
1339 }
1340 
1341 static int patch_vt1716S(struct hda_codec *codec)
1342 {
1343 	struct via_spec *spec;
1344 	int err;
1345 
1346 	/* create a codec specific record */
1347 	spec = via_new_spec(codec);
1348 	if (spec == NULL)
1349 		return -ENOMEM;
1350 
1351 	spec->gen.mixer_nid = 0x16;
1352 	override_mic_boost(codec, 0x1a, 0, 3, 40);
1353 	override_mic_boost(codec, 0x1e, 0, 3, 40);
1354 
1355 	/* automatic parse from the BIOS config */
1356 	err = via_parse_auto_config(codec);
1357 	if (err < 0) {
1358 		via_free(codec);
1359 		return err;
1360 	}
1361 
1362 	spec->init_verbs[spec->num_iverbs++]  = vt1716S_init_verbs;
1363 
1364 	spec->mixers[spec->num_mixers++] = vt1716s_dmic_mixer;
1365 	spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
1366 
1367 	codec->patch_ops = via_patch_ops;
1368 
1369 	spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
1370 	return 0;
1371 }
1372 
1373 /* for vt2002P */
1374 
1375 static const struct hda_verb vt2002P_init_verbs[] = {
1376 	/* Class-D speaker related verbs */
1377 	{0x1, 0xfe0, 0x4},
1378 	{0x1, 0xfe9, 0x80},
1379 	{0x1, 0xfe2, 0x22},
1380 	/* Enable Boost Volume backdoor */
1381 	{0x1, 0xfb9, 0x24},
1382 	/* Enable AOW0 to MW9 */
1383 	{0x1, 0xfb8, 0x88},
1384 	{ }
1385 };
1386 
1387 static const struct hda_verb vt1802_init_verbs[] = {
1388 	/* Enable Boost Volume backdoor */
1389 	{0x1, 0xfb9, 0x24},
1390 	/* Enable AOW0 to MW9 */
1391 	{0x1, 0xfb8, 0x88},
1392 	{ }
1393 };
1394 
1395 static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
1396 {
1397 	struct via_spec *spec = codec->spec;
1398 	int imux_is_smixer;
1399 	unsigned int parm;
1400 	unsigned int present;
1401 	/* MUX9 (1eh) = stereo mixer */
1402 	imux_is_smixer =
1403 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
1404 	/* inputs */
1405 	/* PW 5/6/7 (29h/2ah/2bh) */
1406 	parm = AC_PWRST_D3;
1407 	set_pin_power_state(codec, 0x29, &parm);
1408 	set_pin_power_state(codec, 0x2a, &parm);
1409 	set_pin_power_state(codec, 0x2b, &parm);
1410 	parm = AC_PWRST_D0;
1411 	/* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
1412 	update_power_state(codec, 0x1e, parm);
1413 	update_power_state(codec, 0x1f, parm);
1414 	update_power_state(codec, 0x10, parm);
1415 	update_power_state(codec, 0x11, parm);
1416 
1417 	/* outputs */
1418 	/* AOW0 (8h)*/
1419 	update_power_state(codec, 0x8, parm);
1420 
1421 	if (spec->codec_type == VT1802) {
1422 		/* PW4 (28h), MW4 (18h), MUX4(38h) */
1423 		parm = AC_PWRST_D3;
1424 		set_pin_power_state(codec, 0x28, &parm);
1425 		update_power_state(codec, 0x18, parm);
1426 		update_power_state(codec, 0x38, parm);
1427 	} else {
1428 		/* PW4 (26h), MW4 (1ch), MUX4(37h) */
1429 		parm = AC_PWRST_D3;
1430 		set_pin_power_state(codec, 0x26, &parm);
1431 		update_power_state(codec, 0x1c, parm);
1432 		update_power_state(codec, 0x37, parm);
1433 	}
1434 
1435 	if (spec->codec_type == VT1802) {
1436 		/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
1437 		parm = AC_PWRST_D3;
1438 		set_pin_power_state(codec, 0x25, &parm);
1439 		update_power_state(codec, 0x15, parm);
1440 		update_power_state(codec, 0x35, parm);
1441 	} else {
1442 		/* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
1443 		parm = AC_PWRST_D3;
1444 		set_pin_power_state(codec, 0x25, &parm);
1445 		update_power_state(codec, 0x19, parm);
1446 		update_power_state(codec, 0x35, parm);
1447 	}
1448 
1449 	if (spec->gen.indep_hp_enabled)
1450 		update_power_state(codec, 0x9, AC_PWRST_D0);
1451 
1452 	/* Class-D */
1453 	/* PW0 (24h), MW0(18h/14h), MUX0(34h) */
1454 	present = snd_hda_jack_detect(codec, 0x25);
1455 
1456 	parm = AC_PWRST_D3;
1457 	set_pin_power_state(codec, 0x24, &parm);
1458 	parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
1459 	if (spec->codec_type == VT1802)
1460 		update_power_state(codec, 0x14, parm);
1461 	else
1462 		update_power_state(codec, 0x18, parm);
1463 	update_power_state(codec, 0x34, parm);
1464 
1465 	/* Mono Out */
1466 	present = snd_hda_jack_detect(codec, 0x26);
1467 
1468 	parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
1469 	if (spec->codec_type == VT1802) {
1470 		/* PW15 (33h), MW8(1ch), MUX8(3ch) */
1471 		update_power_state(codec, 0x33, parm);
1472 		update_power_state(codec, 0x1c, parm);
1473 		update_power_state(codec, 0x3c, parm);
1474 	} else {
1475 		/* PW15 (31h), MW8(17h), MUX8(3bh) */
1476 		update_power_state(codec, 0x31, parm);
1477 		update_power_state(codec, 0x17, parm);
1478 		update_power_state(codec, 0x3b, parm);
1479 	}
1480 	/* MW9 (21h) */
1481 	if (imux_is_smixer || !is_aa_path_mute(codec))
1482 		update_power_state(codec, 0x21, AC_PWRST_D0);
1483 	else
1484 		update_power_state(codec, 0x21, AC_PWRST_D3);
1485 }
1486 
1487 /*
1488  * pin fix-up
1489  */
1490 enum {
1491 	VIA_FIXUP_INTMIC_BOOST,
1492 	VIA_FIXUP_ASUS_G75,
1493 };
1494 
1495 static void via_fixup_intmic_boost(struct hda_codec *codec,
1496 				  const struct hda_fixup *fix, int action)
1497 {
1498 	if (action == HDA_FIXUP_ACT_PRE_PROBE)
1499 		override_mic_boost(codec, 0x30, 0, 2, 40);
1500 }
1501 
1502 static const struct hda_fixup via_fixups[] = {
1503 	[VIA_FIXUP_INTMIC_BOOST] = {
1504 		.type = HDA_FIXUP_FUNC,
1505 		.v.func = via_fixup_intmic_boost,
1506 	},
1507 	[VIA_FIXUP_ASUS_G75] = {
1508 		.type = HDA_FIXUP_PINS,
1509 		.v.pins = (const struct hda_pintbl[]) {
1510 			/* set 0x24 and 0x33 as speakers */
1511 			{ 0x24, 0x991301f0 },
1512 			{ 0x33, 0x991301f1 }, /* subwoofer */
1513 			{ }
1514 		}
1515 	},
1516 };
1517 
1518 static const struct snd_pci_quirk vt2002p_fixups[] = {
1519 	SND_PCI_QUIRK(0x1043, 0x1487, "Asus G75", VIA_FIXUP_ASUS_G75),
1520 	SND_PCI_QUIRK(0x1043, 0x8532, "Asus X202E", VIA_FIXUP_INTMIC_BOOST),
1521 	{}
1522 };
1523 
1524 /* NIDs 0x24 and 0x33 on VT1802 have connections to non-existing NID 0x3e
1525  * Replace this with mixer NID 0x1c
1526  */
1527 static void fix_vt1802_connections(struct hda_codec *codec)
1528 {
1529 	static hda_nid_t conn_24[] = { 0x14, 0x1c };
1530 	static hda_nid_t conn_33[] = { 0x1c };
1531 
1532 	snd_hda_override_conn_list(codec, 0x24, ARRAY_SIZE(conn_24), conn_24);
1533 	snd_hda_override_conn_list(codec, 0x33, ARRAY_SIZE(conn_33), conn_33);
1534 }
1535 
1536 /* patch for vt2002P */
1537 static int patch_vt2002P(struct hda_codec *codec)
1538 {
1539 	struct via_spec *spec;
1540 	int err;
1541 
1542 	/* create a codec specific record */
1543 	spec = via_new_spec(codec);
1544 	if (spec == NULL)
1545 		return -ENOMEM;
1546 
1547 	spec->gen.mixer_nid = 0x21;
1548 	override_mic_boost(codec, 0x2b, 0, 3, 40);
1549 	override_mic_boost(codec, 0x29, 0, 3, 40);
1550 	if (spec->codec_type == VT1802)
1551 		fix_vt1802_connections(codec);
1552 	add_secret_dac_path(codec);
1553 
1554 	snd_hda_pick_fixup(codec, NULL, vt2002p_fixups, via_fixups);
1555 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1556 
1557 	/* automatic parse from the BIOS config */
1558 	err = via_parse_auto_config(codec);
1559 	if (err < 0) {
1560 		via_free(codec);
1561 		return err;
1562 	}
1563 
1564 	if (spec->codec_type == VT1802)
1565 		spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
1566 	else
1567 		spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
1568 
1569 	codec->patch_ops = via_patch_ops;
1570 
1571 	spec->set_widgets_power_state =  set_widgets_power_state_vt2002P;
1572 	return 0;
1573 }
1574 
1575 /* for vt1812 */
1576 
1577 static const struct hda_verb vt1812_init_verbs[] = {
1578 	/* Enable Boost Volume backdoor */
1579 	{0x1, 0xfb9, 0x24},
1580 	/* Enable AOW0 to MW9 */
1581 	{0x1, 0xfb8, 0xa8},
1582 	{ }
1583 };
1584 
1585 static void set_widgets_power_state_vt1812(struct hda_codec *codec)
1586 {
1587 	struct via_spec *spec = codec->spec;
1588 	unsigned int parm;
1589 	unsigned int present;
1590 	/* inputs */
1591 	/* PW 5/6/7 (29h/2ah/2bh) */
1592 	parm = AC_PWRST_D3;
1593 	set_pin_power_state(codec, 0x29, &parm);
1594 	set_pin_power_state(codec, 0x2a, &parm);
1595 	set_pin_power_state(codec, 0x2b, &parm);
1596 	parm = AC_PWRST_D0;
1597 	/* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
1598 	update_power_state(codec, 0x1e, parm);
1599 	update_power_state(codec, 0x1f, parm);
1600 	update_power_state(codec, 0x10, parm);
1601 	update_power_state(codec, 0x11, parm);
1602 
1603 	/* outputs */
1604 	/* AOW0 (8h)*/
1605 	update_power_state(codec, 0x8, AC_PWRST_D0);
1606 
1607 	/* PW4 (28h), MW4 (18h), MUX4(38h) */
1608 	parm = AC_PWRST_D3;
1609 	set_pin_power_state(codec, 0x28, &parm);
1610 	update_power_state(codec, 0x18, parm);
1611 	update_power_state(codec, 0x38, parm);
1612 
1613 	/* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
1614 	parm = AC_PWRST_D3;
1615 	set_pin_power_state(codec, 0x25, &parm);
1616 	update_power_state(codec, 0x15, parm);
1617 	update_power_state(codec, 0x35, parm);
1618 	if (spec->gen.indep_hp_enabled)
1619 		update_power_state(codec, 0x9, AC_PWRST_D0);
1620 
1621 	/* Internal Speaker */
1622 	/* PW0 (24h), MW0(14h), MUX0(34h) */
1623 	present = snd_hda_jack_detect(codec, 0x25);
1624 
1625 	parm = AC_PWRST_D3;
1626 	set_pin_power_state(codec, 0x24, &parm);
1627 	if (present) {
1628 		update_power_state(codec, 0x14, AC_PWRST_D3);
1629 		update_power_state(codec, 0x34, AC_PWRST_D3);
1630 	} else {
1631 		update_power_state(codec, 0x14, AC_PWRST_D0);
1632 		update_power_state(codec, 0x34, AC_PWRST_D0);
1633 	}
1634 
1635 
1636 	/* Mono Out */
1637 	/* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
1638 	present = snd_hda_jack_detect(codec, 0x28);
1639 
1640 	parm = AC_PWRST_D3;
1641 	set_pin_power_state(codec, 0x31, &parm);
1642 	if (present) {
1643 		update_power_state(codec, 0x1c, AC_PWRST_D3);
1644 		update_power_state(codec, 0x3c, AC_PWRST_D3);
1645 		update_power_state(codec, 0x3e, AC_PWRST_D3);
1646 	} else {
1647 		update_power_state(codec, 0x1c, AC_PWRST_D0);
1648 		update_power_state(codec, 0x3c, AC_PWRST_D0);
1649 		update_power_state(codec, 0x3e, AC_PWRST_D0);
1650 	}
1651 
1652 	/* PW15 (33h), MW15 (1dh), MUX15(3dh) */
1653 	parm = AC_PWRST_D3;
1654 	set_pin_power_state(codec, 0x33, &parm);
1655 	update_power_state(codec, 0x1d, parm);
1656 	update_power_state(codec, 0x3d, parm);
1657 
1658 }
1659 
1660 /* patch for vt1812 */
1661 static int patch_vt1812(struct hda_codec *codec)
1662 {
1663 	struct via_spec *spec;
1664 	int err;
1665 
1666 	/* create a codec specific record */
1667 	spec = via_new_spec(codec);
1668 	if (spec == NULL)
1669 		return -ENOMEM;
1670 
1671 	spec->gen.mixer_nid = 0x21;
1672 	override_mic_boost(codec, 0x2b, 0, 3, 40);
1673 	override_mic_boost(codec, 0x29, 0, 3, 40);
1674 	add_secret_dac_path(codec);
1675 
1676 	/* automatic parse from the BIOS config */
1677 	err = via_parse_auto_config(codec);
1678 	if (err < 0) {
1679 		via_free(codec);
1680 		return err;
1681 	}
1682 
1683 	spec->init_verbs[spec->num_iverbs++]  = vt1812_init_verbs;
1684 
1685 	codec->patch_ops = via_patch_ops;
1686 
1687 	spec->set_widgets_power_state =  set_widgets_power_state_vt1812;
1688 	return 0;
1689 }
1690 
1691 /* patch for vt3476 */
1692 
1693 static const struct hda_verb vt3476_init_verbs[] = {
1694 	/* Enable DMic 8/16/32K */
1695 	{0x1, 0xF7B, 0x30},
1696 	/* Enable Boost Volume backdoor */
1697 	{0x1, 0xFB9, 0x20},
1698 	/* Enable AOW-MW9 path */
1699 	{0x1, 0xFB8, 0x10},
1700 	{ }
1701 };
1702 
1703 static void set_widgets_power_state_vt3476(struct hda_codec *codec)
1704 {
1705 	struct via_spec *spec = codec->spec;
1706 	int imux_is_smixer;
1707 	unsigned int parm, parm2;
1708 	/* MUX10 (1eh) = stereo mixer */
1709 	imux_is_smixer =
1710 	snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 4;
1711 	/* inputs */
1712 	/* PW 5/6/7 (29h/2ah/2bh) */
1713 	parm = AC_PWRST_D3;
1714 	set_pin_power_state(codec, 0x29, &parm);
1715 	set_pin_power_state(codec, 0x2a, &parm);
1716 	set_pin_power_state(codec, 0x2b, &parm);
1717 	if (imux_is_smixer)
1718 		parm = AC_PWRST_D0;
1719 	/* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
1720 	update_power_state(codec, 0x1e, parm);
1721 	update_power_state(codec, 0x1f, parm);
1722 	update_power_state(codec, 0x10, parm);
1723 	update_power_state(codec, 0x11, parm);
1724 
1725 	/* outputs */
1726 	/* PW3 (27h), MW3(37h), AOW3 (bh) */
1727 	if (spec->codec_type == VT1705CF) {
1728 		parm = AC_PWRST_D3;
1729 		update_power_state(codec, 0x27, parm);
1730 		update_power_state(codec, 0x37, parm);
1731 	}	else {
1732 		parm = AC_PWRST_D3;
1733 		set_pin_power_state(codec, 0x27, &parm);
1734 		update_power_state(codec, 0x37, parm);
1735 	}
1736 
1737 	/* PW2 (26h), MW2(36h), AOW2 (ah) */
1738 	parm = AC_PWRST_D3;
1739 	set_pin_power_state(codec, 0x26, &parm);
1740 	update_power_state(codec, 0x36, parm);
1741 	if (smart51_enabled(codec)) {
1742 		/* PW7(2bh), MW7(3bh), MUX7(1Bh) */
1743 		set_pin_power_state(codec, 0x2b, &parm);
1744 		update_power_state(codec, 0x3b, parm);
1745 		update_power_state(codec, 0x1b, parm);
1746 	}
1747 	update_conv_power_state(codec, 0xa, parm, 2);
1748 
1749 	/* PW1 (25h), MW1(35h), AOW1 (9h) */
1750 	parm = AC_PWRST_D3;
1751 	set_pin_power_state(codec, 0x25, &parm);
1752 	update_power_state(codec, 0x35, parm);
1753 	if (smart51_enabled(codec)) {
1754 		/* PW6(2ah), MW6(3ah), MUX6(1ah) */
1755 		set_pin_power_state(codec, 0x2a, &parm);
1756 		update_power_state(codec, 0x3a, parm);
1757 		update_power_state(codec, 0x1a, parm);
1758 	}
1759 	update_conv_power_state(codec, 0x9, parm, 1);
1760 
1761 	/* PW4 (28h), MW4 (38h), MUX4(18h), AOW3(bh)/AOW0(8h) */
1762 	parm = AC_PWRST_D3;
1763 	set_pin_power_state(codec, 0x28, &parm);
1764 	update_power_state(codec, 0x38, parm);
1765 	update_power_state(codec, 0x18, parm);
1766 	if (spec->gen.indep_hp_enabled)
1767 		update_conv_power_state(codec, 0xb, parm, 3);
1768 	parm2 = parm; /* for pin 0x0b */
1769 
1770 	/* PW0 (24h), MW0(34h), MW9(3fh), AOW0 (8h) */
1771 	parm = AC_PWRST_D3;
1772 	set_pin_power_state(codec, 0x24, &parm);
1773 	update_power_state(codec, 0x34, parm);
1774 	if (!spec->gen.indep_hp_enabled && parm2 != AC_PWRST_D3)
1775 		parm = parm2;
1776 	update_conv_power_state(codec, 0x8, parm, 0);
1777 	/* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
1778 	update_power_state(codec, 0x3f, imux_is_smixer ? AC_PWRST_D0 : parm);
1779 }
1780 
1781 static int patch_vt3476(struct hda_codec *codec)
1782 {
1783 	struct via_spec *spec;
1784 	int err;
1785 
1786 	/* create a codec specific record */
1787 	spec = via_new_spec(codec);
1788 	if (spec == NULL)
1789 		return -ENOMEM;
1790 
1791 	spec->gen.mixer_nid = 0x3f;
1792 	add_secret_dac_path(codec);
1793 
1794 	/* automatic parse from the BIOS config */
1795 	err = via_parse_auto_config(codec);
1796 	if (err < 0) {
1797 		via_free(codec);
1798 		return err;
1799 	}
1800 
1801 	spec->init_verbs[spec->num_iverbs++] = vt3476_init_verbs;
1802 
1803 	codec->patch_ops = via_patch_ops;
1804 
1805 	spec->set_widgets_power_state = set_widgets_power_state_vt3476;
1806 
1807 	return 0;
1808 }
1809 
1810 /*
1811  * patch entries
1812  */
1813 static const struct hda_codec_preset snd_hda_preset_via[] = {
1814 	{ .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
1815 	{ .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
1816 	{ .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
1817 	{ .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
1818 	{ .id = 0x1106e710, .name = "VT1709 10-Ch",
1819 	  .patch = patch_vt1709},
1820 	{ .id = 0x1106e711, .name = "VT1709 10-Ch",
1821 	  .patch = patch_vt1709},
1822 	{ .id = 0x1106e712, .name = "VT1709 10-Ch",
1823 	  .patch = patch_vt1709},
1824 	{ .id = 0x1106e713, .name = "VT1709 10-Ch",
1825 	  .patch = patch_vt1709},
1826 	{ .id = 0x1106e714, .name = "VT1709 6-Ch",
1827 	  .patch = patch_vt1709},
1828 	{ .id = 0x1106e715, .name = "VT1709 6-Ch",
1829 	  .patch = patch_vt1709},
1830 	{ .id = 0x1106e716, .name = "VT1709 6-Ch",
1831 	  .patch = patch_vt1709},
1832 	{ .id = 0x1106e717, .name = "VT1709 6-Ch",
1833 	  .patch = patch_vt1709},
1834 	{ .id = 0x1106e720, .name = "VT1708B 8-Ch",
1835 	  .patch = patch_vt1708B},
1836 	{ .id = 0x1106e721, .name = "VT1708B 8-Ch",
1837 	  .patch = patch_vt1708B},
1838 	{ .id = 0x1106e722, .name = "VT1708B 8-Ch",
1839 	  .patch = patch_vt1708B},
1840 	{ .id = 0x1106e723, .name = "VT1708B 8-Ch",
1841 	  .patch = patch_vt1708B},
1842 	{ .id = 0x1106e724, .name = "VT1708B 4-Ch",
1843 	  .patch = patch_vt1708B},
1844 	{ .id = 0x1106e725, .name = "VT1708B 4-Ch",
1845 	  .patch = patch_vt1708B},
1846 	{ .id = 0x1106e726, .name = "VT1708B 4-Ch",
1847 	  .patch = patch_vt1708B},
1848 	{ .id = 0x1106e727, .name = "VT1708B 4-Ch",
1849 	  .patch = patch_vt1708B},
1850 	{ .id = 0x11060397, .name = "VT1708S",
1851 	  .patch = patch_vt1708S},
1852 	{ .id = 0x11061397, .name = "VT1708S",
1853 	  .patch = patch_vt1708S},
1854 	{ .id = 0x11062397, .name = "VT1708S",
1855 	  .patch = patch_vt1708S},
1856 	{ .id = 0x11063397, .name = "VT1708S",
1857 	  .patch = patch_vt1708S},
1858 	{ .id = 0x11064397, .name = "VT1705",
1859 	  .patch = patch_vt1708S},
1860 	{ .id = 0x11065397, .name = "VT1708S",
1861 	  .patch = patch_vt1708S},
1862 	{ .id = 0x11066397, .name = "VT1708S",
1863 	  .patch = patch_vt1708S},
1864 	{ .id = 0x11067397, .name = "VT1708S",
1865 	  .patch = patch_vt1708S},
1866 	{ .id = 0x11060398, .name = "VT1702",
1867 	  .patch = patch_vt1702},
1868 	{ .id = 0x11061398, .name = "VT1702",
1869 	  .patch = patch_vt1702},
1870 	{ .id = 0x11062398, .name = "VT1702",
1871 	  .patch = patch_vt1702},
1872 	{ .id = 0x11063398, .name = "VT1702",
1873 	  .patch = patch_vt1702},
1874 	{ .id = 0x11064398, .name = "VT1702",
1875 	  .patch = patch_vt1702},
1876 	{ .id = 0x11065398, .name = "VT1702",
1877 	  .patch = patch_vt1702},
1878 	{ .id = 0x11066398, .name = "VT1702",
1879 	  .patch = patch_vt1702},
1880 	{ .id = 0x11067398, .name = "VT1702",
1881 	  .patch = patch_vt1702},
1882 	{ .id = 0x11060428, .name = "VT1718S",
1883 	  .patch = patch_vt1718S},
1884 	{ .id = 0x11064428, .name = "VT1718S",
1885 	  .patch = patch_vt1718S},
1886 	{ .id = 0x11060441, .name = "VT2020",
1887 	  .patch = patch_vt1718S},
1888 	{ .id = 0x11064441, .name = "VT1828S",
1889 	  .patch = patch_vt1718S},
1890 	{ .id = 0x11060433, .name = "VT1716S",
1891 	  .patch = patch_vt1716S},
1892 	{ .id = 0x1106a721, .name = "VT1716S",
1893 	  .patch = patch_vt1716S},
1894 	{ .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
1895 	{ .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
1896 	{ .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
1897 	{ .id = 0x11060440, .name = "VT1818S",
1898 	  .patch = patch_vt1708S},
1899 	{ .id = 0x11060446, .name = "VT1802",
1900 		.patch = patch_vt2002P},
1901 	{ .id = 0x11068446, .name = "VT1802",
1902 		.patch = patch_vt2002P},
1903 	{ .id = 0x11064760, .name = "VT1705CF",
1904 		.patch = patch_vt3476},
1905 	{ .id = 0x11064761, .name = "VT1708SCE",
1906 		.patch = patch_vt3476},
1907 	{ .id = 0x11064762, .name = "VT1808",
1908 		.patch = patch_vt3476},
1909 	{} /* terminator */
1910 };
1911 
1912 MODULE_ALIAS("snd-hda-codec-id:1106*");
1913 
1914 static struct hda_codec_preset_list via_list = {
1915 	.preset = snd_hda_preset_via,
1916 	.owner = THIS_MODULE,
1917 };
1918 
1919 MODULE_LICENSE("GPL");
1920 MODULE_DESCRIPTION("VIA HD-audio codec");
1921 
1922 static int __init patch_via_init(void)
1923 {
1924 	return snd_hda_add_codec_preset(&via_list);
1925 }
1926 
1927 static void __exit patch_via_exit(void)
1928 {
1929 	snd_hda_delete_codec_preset(&via_list);
1930 }
1931 
1932 module_init(patch_via_init)
1933 module_exit(patch_via_exit)
1934