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