Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Universal Interface for Intel High Definition Audio Codec
4 *
5 * HD audio interface patch for VIA VT17xx/VT18xx/VT20xx codec
6 *
7 * (C) 2006-2009 VIA Technology, Inc.
8 * (C) 2006-2008 Takashi Iwai <tiwai@suse.de>
9 */
10
11/* * * * * * * * * * * * * * Release History * * * * * * * * * * * * * * * * */
12/* */
13/* 2006-03-03 Lydia Wang Create the basic patch to support VT1708 codec */
14/* 2006-03-14 Lydia Wang Modify hard code for some pin widget nid */
15/* 2006-08-02 Lydia Wang Add support to VT1709 codec */
16/* 2006-09-08 Lydia Wang Fix internal loopback recording source select bug */
17/* 2007-09-12 Lydia Wang Add EAPD enable during driver initialization */
18/* 2007-09-17 Lydia Wang Add VT1708B codec support */
19/* 2007-11-14 Lydia Wang Add VT1708A codec HP and CD pin connect config */
20/* 2008-02-03 Lydia Wang Fix Rear channels and Back channels inverse issue */
21/* 2008-03-06 Lydia Wang Add VT1702 codec and VT1708S codec support */
22/* 2008-04-09 Lydia Wang Add mute front speaker when HP plugin */
23/* 2008-04-09 Lydia Wang Add Independent HP feature */
24/* 2008-05-28 Lydia Wang Add second S/PDIF Out support for VT1702 */
25/* 2008-09-15 Logan Li Add VT1708S Mic Boost workaround/backdoor */
26/* 2009-02-16 Logan Li Add support for VT1718S */
27/* 2009-03-13 Logan Li Add support for VT1716S */
28/* 2009-04-14 Lydai Wang Add support for VT1828S and VT2020 */
29/* 2009-07-08 Lydia Wang Add support for VT2002P */
30/* 2009-07-21 Lydia Wang Add support for VT1812 */
31/* 2009-09-19 Lydia Wang Add support for VT1818S */
32/* */
33/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
34
35
36#include <linux/init.h>
37#include <linux/delay.h>
38#include <linux/slab.h>
39#include <linux/module.h>
40#include <sound/core.h>
41#include <sound/asoundef.h>
42#include <sound/hda_codec.h>
43#include "hda_local.h"
44#include "hda_auto_parser.h"
45#include "hda_jack.h"
46#include "hda_generic.h"
47
48/* Pin Widget NID */
49#define VT1708_HP_PIN_NID 0x20
50#define VT1708_CD_PIN_NID 0x24
51
52enum VIA_HDA_CODEC {
53 UNKNOWN = -1,
54 VT1708,
55 VT1709_10CH,
56 VT1709_6CH,
57 VT1708B_8CH,
58 VT1708B_4CH,
59 VT1708S,
60 VT1708BCE,
61 VT1702,
62 VT1718S,
63 VT1716S,
64 VT2002P,
65 VT1812,
66 VT1802,
67 VT1705CF,
68 VT1808,
69 CODEC_TYPES,
70};
71
72#define VT2002P_COMPATIBLE(spec) \
73 ((spec)->codec_type == VT2002P ||\
74 (spec)->codec_type == VT1812 ||\
75 (spec)->codec_type == VT1802)
76
77struct via_spec {
78 struct hda_gen_spec gen;
79
80 /* HP mode source */
81 unsigned int dmic_enabled;
82 enum VIA_HDA_CODEC codec_type;
83
84 /* analog low-power control */
85 bool alc_mode;
86
87 /* work to check hp jack state */
88 int hp_work_active;
89 int vt1708_jack_detect;
90};
91
92static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
93static void via_playback_pcm_hook(struct hda_pcm_stream *hinfo,
94 struct hda_codec *codec,
95 struct snd_pcm_substream *substream,
96 int action);
97
98static const struct hda_codec_ops via_patch_ops; /* defined below */
99
100static struct via_spec *via_new_spec(struct hda_codec *codec)
101{
102 struct via_spec *spec;
103
104 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
105 if (spec == NULL)
106 return NULL;
107
108 codec->spec = spec;
109 snd_hda_gen_spec_init(&spec->gen);
110 spec->codec_type = get_codec_type(codec);
111 /* VT1708BCE & VT1708S are almost same */
112 if (spec->codec_type == VT1708BCE)
113 spec->codec_type = VT1708S;
114 spec->gen.indep_hp = 1;
115 spec->gen.keep_eapd_on = 1;
116 spec->gen.pcm_playback_hook = via_playback_pcm_hook;
117 spec->gen.add_stereo_mix_input = HDA_HINT_STEREO_MIX_AUTO;
118 codec->power_save_node = 1;
119 spec->gen.power_down_unused = 1;
120 codec->patch_ops = via_patch_ops;
121 return spec;
122}
123
124static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
125{
126 u32 vendor_id = codec->core.vendor_id;
127 u16 ven_id = vendor_id >> 16;
128 u16 dev_id = vendor_id & 0xffff;
129 enum VIA_HDA_CODEC codec_type;
130
131 /* get codec type */
132 if (ven_id != 0x1106)
133 codec_type = UNKNOWN;
134 else if (dev_id >= 0x1708 && dev_id <= 0x170b)
135 codec_type = VT1708;
136 else if (dev_id >= 0xe710 && dev_id <= 0xe713)
137 codec_type = VT1709_10CH;
138 else if (dev_id >= 0xe714 && dev_id <= 0xe717)
139 codec_type = VT1709_6CH;
140 else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
141 codec_type = VT1708B_8CH;
142 if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
143 codec_type = VT1708BCE;
144 } else if (dev_id >= 0xe724 && dev_id <= 0xe727)
145 codec_type = VT1708B_4CH;
146 else if ((dev_id & 0xfff) == 0x397
147 && (dev_id >> 12) < 8)
148 codec_type = VT1708S;
149 else if ((dev_id & 0xfff) == 0x398
150 && (dev_id >> 12) < 8)
151 codec_type = VT1702;
152 else if ((dev_id & 0xfff) == 0x428
153 && (dev_id >> 12) < 8)
154 codec_type = VT1718S;
155 else if (dev_id == 0x0433 || dev_id == 0xa721)
156 codec_type = VT1716S;
157 else if (dev_id == 0x0441 || dev_id == 0x4441)
158 codec_type = VT1718S;
159 else if (dev_id == 0x0438 || dev_id == 0x4438)
160 codec_type = VT2002P;
161 else if (dev_id == 0x0448)
162 codec_type = VT1812;
163 else if (dev_id == 0x0440)
164 codec_type = VT1708S;
165 else if ((dev_id & 0xfff) == 0x446)
166 codec_type = VT1802;
167 else if (dev_id == 0x4760)
168 codec_type = VT1705CF;
169 else if (dev_id == 0x4761 || dev_id == 0x4762)
170 codec_type = VT1808;
171 else
172 codec_type = UNKNOWN;
173 return codec_type;
174};
175
176static void analog_low_current_mode(struct hda_codec *codec);
177static bool is_aa_path_mute(struct hda_codec *codec);
178
179#define hp_detect_with_aa(codec) \
180 (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1 && \
181 !is_aa_path_mute(codec))
182
183static void vt1708_stop_hp_work(struct hda_codec *codec)
184{
185 struct via_spec *spec = codec->spec;
186 if (spec->codec_type != VT1708 || !spec->gen.autocfg.hp_outs)
187 return;
188 if (spec->hp_work_active) {
189 snd_hda_codec_write(codec, 0x1, 0, 0xf81, 1);
190 codec->jackpoll_interval = 0;
191 cancel_delayed_work_sync(&codec->jackpoll_work);
192 spec->hp_work_active = false;
193 }
194}
195
196static void vt1708_update_hp_work(struct hda_codec *codec)
197{
198 struct via_spec *spec = codec->spec;
199 if (spec->codec_type != VT1708 || !spec->gen.autocfg.hp_outs)
200 return;
201 if (spec->vt1708_jack_detect) {
202 if (!spec->hp_work_active) {
203 codec->jackpoll_interval = msecs_to_jiffies(100);
204 snd_hda_codec_write(codec, 0x1, 0, 0xf81, 0);
205 schedule_delayed_work(&codec->jackpoll_work, 0);
206 spec->hp_work_active = true;
207 }
208 } else if (!hp_detect_with_aa(codec))
209 vt1708_stop_hp_work(codec);
210}
211
212static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
213 struct snd_ctl_elem_info *uinfo)
214{
215 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
216}
217
218static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
219 struct snd_ctl_elem_value *ucontrol)
220{
221 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
222 struct via_spec *spec = codec->spec;
223
224 ucontrol->value.enumerated.item[0] = spec->gen.power_down_unused;
225 return 0;
226}
227
228static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
229 struct snd_ctl_elem_value *ucontrol)
230{
231 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
232 struct via_spec *spec = codec->spec;
233 bool val = !!ucontrol->value.enumerated.item[0];
234
235 if (val == spec->gen.power_down_unused)
236 return 0;
237 /* codec->power_save_node = val; */ /* widget PM seems yet broken */
238 spec->gen.power_down_unused = val;
239 analog_low_current_mode(codec);
240 return 1;
241}
242
243static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
244 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
245 .name = "Dynamic Power-Control",
246 .info = via_pin_power_ctl_info,
247 .get = via_pin_power_ctl_get,
248 .put = via_pin_power_ctl_put,
249};
250
251#ifdef CONFIG_SND_HDA_INPUT_BEEP
252/* additional beep mixers; the actual parameters are overwritten at build */
253static const struct snd_kcontrol_new via_beep_mixer[] = {
254 HDA_CODEC_VOLUME_MONO("Beep Playback Volume", 0, 1, 0, HDA_OUTPUT),
255 HDA_CODEC_MUTE_BEEP_MONO("Beep Playback Switch", 0, 1, 0, HDA_OUTPUT),
256};
257
258static int set_beep_amp(struct via_spec *spec, hda_nid_t nid,
259 int idx, int dir)
260{
261 struct snd_kcontrol_new *knew;
262 unsigned int beep_amp = HDA_COMPOSE_AMP_VAL(nid, 1, idx, dir);
263 int i;
264
265 spec->gen.beep_nid = nid;
266 for (i = 0; i < ARRAY_SIZE(via_beep_mixer); i++) {
267 knew = snd_hda_gen_add_kctl(&spec->gen, NULL,
268 &via_beep_mixer[i]);
269 if (!knew)
270 return -ENOMEM;
271 knew->private_value = beep_amp;
272 }
273 return 0;
274}
275
276static int auto_parse_beep(struct hda_codec *codec)
277{
278 struct via_spec *spec = codec->spec;
279 hda_nid_t nid;
280
281 for_each_hda_codec_node(nid, codec)
282 if (get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_BEEP)
283 return set_beep_amp(spec, nid, 0, HDA_OUTPUT);
284 return 0;
285}
286#else
287#define auto_parse_beep(codec) 0
288#endif
289
290/* check AA path's mute status */
291static bool is_aa_path_mute(struct hda_codec *codec)
292{
293 struct via_spec *spec = codec->spec;
294 const struct hda_amp_list *p;
295 int ch, v;
296
297 p = spec->gen.loopback.amplist;
298 if (!p)
299 return true;
300 for (; p->nid; p++) {
301 for (ch = 0; ch < 2; ch++) {
302 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
303 p->idx);
304 if (!(v & HDA_AMP_MUTE) && v > 0)
305 return false;
306 }
307 }
308 return true;
309}
310
311/* enter/exit analog low-current mode */
312static void __analog_low_current_mode(struct hda_codec *codec, bool force)
313{
314 struct via_spec *spec = codec->spec;
315 bool enable;
316 unsigned int verb, parm;
317
318 if (!codec->power_save_node)
319 enable = false;
320 else
321 enable = is_aa_path_mute(codec) && !spec->gen.active_streams;
322 if (enable == spec->alc_mode && !force)
323 return;
324 spec->alc_mode = enable;
325
326 /* decide low current mode's verb & parameter */
327 switch (spec->codec_type) {
328 case VT1708B_8CH:
329 case VT1708B_4CH:
330 verb = 0xf70;
331 parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
332 break;
333 case VT1708S:
334 case VT1718S:
335 case VT1716S:
336 verb = 0xf73;
337 parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
338 break;
339 case VT1702:
340 verb = 0xf73;
341 parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
342 break;
343 case VT2002P:
344 case VT1812:
345 case VT1802:
346 verb = 0xf93;
347 parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
348 break;
349 case VT1705CF:
350 case VT1808:
351 verb = 0xf82;
352 parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
353 break;
354 default:
355 return; /* other codecs are not supported */
356 }
357 /* send verb */
358 snd_hda_codec_write(codec, codec->core.afg, 0, verb, parm);
359}
360
361static void analog_low_current_mode(struct hda_codec *codec)
362{
363 return __analog_low_current_mode(codec, false);
364}
365
366static void via_playback_pcm_hook(struct hda_pcm_stream *hinfo,
367 struct hda_codec *codec,
368 struct snd_pcm_substream *substream,
369 int action)
370{
371 analog_low_current_mode(codec);
372 vt1708_update_hp_work(codec);
373}
374
375static void via_free(struct hda_codec *codec)
376{
377 vt1708_stop_hp_work(codec);
378 snd_hda_gen_free(codec);
379}
380
381#ifdef CONFIG_PM
382static int via_suspend(struct hda_codec *codec)
383{
384 struct via_spec *spec = codec->spec;
385 vt1708_stop_hp_work(codec);
386
387 /* Fix pop noise on headphones */
388 if (spec->codec_type == VT1802)
389 snd_hda_shutup_pins(codec);
390
391 return 0;
392}
393
394static int via_resume(struct hda_codec *codec)
395{
396 /* some delay here to make jack detection working (bko#98921) */
397 msleep(10);
398 codec->patch_ops.init(codec);
399 snd_hda_regmap_sync(codec);
400 return 0;
401}
402#endif
403
404#ifdef CONFIG_PM
405static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
406{
407 struct via_spec *spec = codec->spec;
408 analog_low_current_mode(codec);
409 vt1708_update_hp_work(codec);
410 return snd_hda_check_amp_list_power(codec, &spec->gen.loopback, nid);
411}
412#endif
413
414/*
415 */
416
417static int via_init(struct hda_codec *codec);
418
419static const struct hda_codec_ops via_patch_ops = {
420 .build_controls = snd_hda_gen_build_controls,
421 .build_pcms = snd_hda_gen_build_pcms,
422 .init = via_init,
423 .free = via_free,
424 .unsol_event = snd_hda_jack_unsol_event,
425#ifdef CONFIG_PM
426 .suspend = via_suspend,
427 .resume = via_resume,
428 .check_power_status = via_check_power_status,
429#endif
430};
431
432
433static const struct hda_verb vt1708_init_verbs[] = {
434 /* power down jack detect function */
435 {0x1, 0xf81, 0x1},
436 { }
437};
438static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
439{
440 unsigned int def_conf;
441 unsigned char seqassoc;
442
443 def_conf = snd_hda_codec_get_pincfg(codec, nid);
444 seqassoc = (unsigned char) get_defcfg_association(def_conf);
445 seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
446 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
447 && (seqassoc == 0xf0 || seqassoc == 0xff)) {
448 def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
449 snd_hda_codec_set_pincfg(codec, nid, def_conf);
450 }
451
452 return;
453}
454
455static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
456 struct snd_ctl_elem_value *ucontrol)
457{
458 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
459 struct via_spec *spec = codec->spec;
460
461 if (spec->codec_type != VT1708)
462 return 0;
463 ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
464 return 0;
465}
466
467static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
468 struct snd_ctl_elem_value *ucontrol)
469{
470 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
471 struct via_spec *spec = codec->spec;
472 int val;
473
474 if (spec->codec_type != VT1708)
475 return 0;
476 val = !!ucontrol->value.integer.value[0];
477 if (spec->vt1708_jack_detect == val)
478 return 0;
479 spec->vt1708_jack_detect = val;
480 vt1708_update_hp_work(codec);
481 return 1;
482}
483
484static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
485 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
486 .name = "Jack Detect",
487 .count = 1,
488 .info = snd_ctl_boolean_mono_info,
489 .get = vt1708_jack_detect_get,
490 .put = vt1708_jack_detect_put,
491};
492
493static const struct badness_table via_main_out_badness = {
494 .no_primary_dac = 0x10000,
495 .no_dac = 0x4000,
496 .shared_primary = 0x10000,
497 .shared_surr = 0x20,
498 .shared_clfe = 0x20,
499 .shared_surr_main = 0x20,
500};
501static const struct badness_table via_extra_out_badness = {
502 .no_primary_dac = 0x4000,
503 .no_dac = 0x4000,
504 .shared_primary = 0x12,
505 .shared_surr = 0x20,
506 .shared_clfe = 0x20,
507 .shared_surr_main = 0x10,
508};
509
510static int via_parse_auto_config(struct hda_codec *codec)
511{
512 struct via_spec *spec = codec->spec;
513 int err;
514
515 spec->gen.main_out_badness = &via_main_out_badness;
516 spec->gen.extra_out_badness = &via_extra_out_badness;
517
518 err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
519 if (err < 0)
520 return err;
521
522 err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
523 if (err < 0)
524 return err;
525
526 err = auto_parse_beep(codec);
527 if (err < 0)
528 return err;
529
530 if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &via_pin_power_ctl_enum))
531 return -ENOMEM;
532
533 /* disable widget PM at start for compatibility */
534 codec->power_save_node = 0;
535 spec->gen.power_down_unused = 0;
536 return 0;
537}
538
539static int via_init(struct hda_codec *codec)
540{
541 /* init power states */
542 __analog_low_current_mode(codec, true);
543
544 snd_hda_gen_init(codec);
545
546 vt1708_update_hp_work(codec);
547
548 return 0;
549}
550
551static int vt1708_build_controls(struct hda_codec *codec)
552{
553 /* In order not to create "Phantom Jack" controls,
554 temporary enable jackpoll */
555 int err;
556 int old_interval = codec->jackpoll_interval;
557 codec->jackpoll_interval = msecs_to_jiffies(100);
558 err = snd_hda_gen_build_controls(codec);
559 codec->jackpoll_interval = old_interval;
560 return err;
561}
562
563static int vt1708_build_pcms(struct hda_codec *codec)
564{
565 struct via_spec *spec = codec->spec;
566 int i, err;
567
568 err = snd_hda_gen_build_pcms(codec);
569 if (err < 0 || codec->core.vendor_id != 0x11061708)
570 return err;
571
572 /* We got noisy outputs on the right channel on VT1708 when
573 * 24bit samples are used. Until any workaround is found,
574 * disable the 24bit format, so far.
575 */
576 for (i = 0; i < ARRAY_SIZE(spec->gen.pcm_rec); i++) {
577 struct hda_pcm *info = spec->gen.pcm_rec[i];
578 if (!info)
579 continue;
580 if (!info->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams ||
581 info->pcm_type != HDA_PCM_TYPE_AUDIO)
582 continue;
583 info->stream[SNDRV_PCM_STREAM_PLAYBACK].formats =
584 SNDRV_PCM_FMTBIT_S16_LE;
585 }
586
587 return 0;
588}
589
590static int patch_vt1708(struct hda_codec *codec)
591{
592 struct via_spec *spec;
593 int err;
594
595 /* create a codec specific record */
596 spec = via_new_spec(codec);
597 if (spec == NULL)
598 return -ENOMEM;
599
600 /* override some patch_ops */
601 codec->patch_ops.build_controls = vt1708_build_controls;
602 codec->patch_ops.build_pcms = vt1708_build_pcms;
603 spec->gen.mixer_nid = 0x17;
604
605 /* set jackpoll_interval while parsing the codec */
606 codec->jackpoll_interval = msecs_to_jiffies(100);
607 spec->vt1708_jack_detect = 1;
608
609 /* don't support the input jack switching due to lack of unsol event */
610 /* (it may work with polling, though, but it needs testing) */
611 spec->gen.suppress_auto_mic = 1;
612 /* Some machines show the broken speaker mute */
613 spec->gen.auto_mute_via_amp = 1;
614
615 /* Add HP and CD pin config connect bit re-config action */
616 vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
617 vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
618
619 err = snd_hda_add_verbs(codec, vt1708_init_verbs);
620 if (err < 0)
621 goto error;
622
623 /* automatic parse from the BIOS config */
624 err = via_parse_auto_config(codec);
625 if (err < 0)
626 goto error;
627
628 /* add jack detect on/off control */
629 if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &vt1708_jack_detect_ctl)) {
630 err = -ENOMEM;
631 goto error;
632 }
633
634 /* clear jackpoll_interval again; it's set dynamically */
635 codec->jackpoll_interval = 0;
636
637 return 0;
638
639 error:
640 via_free(codec);
641 return err;
642}
643
644static int patch_vt1709(struct hda_codec *codec)
645{
646 struct via_spec *spec;
647 int err;
648
649 /* create a codec specific record */
650 spec = via_new_spec(codec);
651 if (spec == NULL)
652 return -ENOMEM;
653
654 spec->gen.mixer_nid = 0x18;
655
656 err = via_parse_auto_config(codec);
657 if (err < 0)
658 goto error;
659
660 return 0;
661
662 error:
663 via_free(codec);
664 return err;
665}
666
667static int patch_vt1708S(struct hda_codec *codec);
668static int patch_vt1708B(struct hda_codec *codec)
669{
670 struct via_spec *spec;
671 int err;
672
673 if (get_codec_type(codec) == VT1708BCE)
674 return patch_vt1708S(codec);
675
676 /* create a codec specific record */
677 spec = via_new_spec(codec);
678 if (spec == NULL)
679 return -ENOMEM;
680
681 spec->gen.mixer_nid = 0x16;
682
683 /* automatic parse from the BIOS config */
684 err = via_parse_auto_config(codec);
685 if (err < 0)
686 goto error;
687
688 return 0;
689
690 error:
691 via_free(codec);
692 return err;
693}
694
695/* Patch for VT1708S */
696static const struct hda_verb vt1708S_init_verbs[] = {
697 /* Enable Mic Boost Volume backdoor */
698 {0x1, 0xf98, 0x1},
699 /* don't bybass mixer */
700 {0x1, 0xf88, 0xc0},
701 { }
702};
703
704static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
705 int offset, int num_steps, int step_size)
706{
707 snd_hda_override_wcaps(codec, pin,
708 get_wcaps(codec, pin) | AC_WCAP_IN_AMP);
709 snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
710 (offset << AC_AMPCAP_OFFSET_SHIFT) |
711 (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
712 (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
713 (0 << AC_AMPCAP_MUTE_SHIFT));
714}
715
716static int patch_vt1708S(struct hda_codec *codec)
717{
718 struct via_spec *spec;
719 int err;
720
721 /* create a codec specific record */
722 spec = via_new_spec(codec);
723 if (spec == NULL)
724 return -ENOMEM;
725
726 spec->gen.mixer_nid = 0x16;
727 override_mic_boost(codec, 0x1a, 0, 3, 40);
728 override_mic_boost(codec, 0x1e, 0, 3, 40);
729
730 /* correct names for VT1708BCE */
731 if (get_codec_type(codec) == VT1708BCE)
732 snd_hda_codec_set_name(codec, "VT1708BCE");
733 /* correct names for VT1705 */
734 if (codec->core.vendor_id == 0x11064397)
735 snd_hda_codec_set_name(codec, "VT1705");
736
737 err = snd_hda_add_verbs(codec, vt1708S_init_verbs);
738 if (err < 0)
739 goto error;
740
741 /* automatic parse from the BIOS config */
742 err = via_parse_auto_config(codec);
743 if (err < 0)
744 goto error;
745
746 return 0;
747
748 error:
749 via_free(codec);
750 return err;
751}
752
753/* Patch for VT1702 */
754
755static const struct hda_verb vt1702_init_verbs[] = {
756 /* mixer enable */
757 {0x1, 0xF88, 0x3},
758 /* GPIO 0~2 */
759 {0x1, 0xF82, 0x3F},
760 { }
761};
762
763static int patch_vt1702(struct hda_codec *codec)
764{
765 struct via_spec *spec;
766 int err;
767
768 /* create a codec specific record */
769 spec = via_new_spec(codec);
770 if (spec == NULL)
771 return -ENOMEM;
772
773 spec->gen.mixer_nid = 0x1a;
774
775 /* limit AA path volume to 0 dB */
776 snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
777 (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
778 (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
779 (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
780 (1 << AC_AMPCAP_MUTE_SHIFT));
781
782 err = snd_hda_add_verbs(codec, vt1702_init_verbs);
783 if (err < 0)
784 goto error;
785
786 /* automatic parse from the BIOS config */
787 err = via_parse_auto_config(codec);
788 if (err < 0)
789 goto error;
790
791 return 0;
792
793 error:
794 via_free(codec);
795 return err;
796}
797
798/* Patch for VT1718S */
799
800static const struct hda_verb vt1718S_init_verbs[] = {
801 /* Enable MW0 adjust Gain 5 */
802 {0x1, 0xfb2, 0x10},
803 /* Enable Boost Volume backdoor */
804 {0x1, 0xf88, 0x8},
805
806 { }
807};
808
809/* Add a connection to the primary DAC from AA-mixer for some codecs
810 * This isn't listed from the raw info, but the chip has a secret connection.
811 */
812static int add_secret_dac_path(struct hda_codec *codec)
813{
814 struct via_spec *spec = codec->spec;
815 int i, nums;
816 hda_nid_t conn[8];
817 hda_nid_t nid;
818
819 if (!spec->gen.mixer_nid)
820 return 0;
821 nums = snd_hda_get_connections(codec, spec->gen.mixer_nid, conn,
822 ARRAY_SIZE(conn) - 1);
823 for (i = 0; i < nums; i++) {
824 if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT)
825 return 0;
826 }
827
828 /* find the primary DAC and add to the connection list */
829 for_each_hda_codec_node(nid, codec) {
830 unsigned int caps = get_wcaps(codec, nid);
831 if (get_wcaps_type(caps) == AC_WID_AUD_OUT &&
832 !(caps & AC_WCAP_DIGITAL)) {
833 conn[nums++] = nid;
834 return snd_hda_override_conn_list(codec,
835 spec->gen.mixer_nid,
836 nums, conn);
837 }
838 }
839 return 0;
840}
841
842
843static int patch_vt1718S(struct hda_codec *codec)
844{
845 struct via_spec *spec;
846 int err;
847
848 /* create a codec specific record */
849 spec = via_new_spec(codec);
850 if (spec == NULL)
851 return -ENOMEM;
852
853 spec->gen.mixer_nid = 0x21;
854 override_mic_boost(codec, 0x2b, 0, 3, 40);
855 override_mic_boost(codec, 0x29, 0, 3, 40);
856 add_secret_dac_path(codec);
857
858 err = snd_hda_add_verbs(codec, vt1718S_init_verbs);
859 if (err < 0)
860 goto error;
861
862 /* automatic parse from the BIOS config */
863 err = via_parse_auto_config(codec);
864 if (err < 0)
865 goto error;
866
867 return 0;
868
869 error:
870 via_free(codec);
871 return err;
872}
873
874/* Patch for VT1716S */
875
876static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
877 struct snd_ctl_elem_info *uinfo)
878{
879 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
880 uinfo->count = 1;
881 uinfo->value.integer.min = 0;
882 uinfo->value.integer.max = 1;
883 return 0;
884}
885
886static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
887 struct snd_ctl_elem_value *ucontrol)
888{
889 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
890 int index = 0;
891
892 index = snd_hda_codec_read(codec, 0x26, 0,
893 AC_VERB_GET_CONNECT_SEL, 0);
894 if (index != -1)
895 *ucontrol->value.integer.value = index;
896
897 return 0;
898}
899
900static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
901 struct snd_ctl_elem_value *ucontrol)
902{
903 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
904 struct via_spec *spec = codec->spec;
905 int index = *ucontrol->value.integer.value;
906
907 snd_hda_codec_write(codec, 0x26, 0,
908 AC_VERB_SET_CONNECT_SEL, index);
909 spec->dmic_enabled = index;
910 return 1;
911}
912
913static const struct snd_kcontrol_new vt1716s_dmic_mixer_vol =
914 HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT);
915static const struct snd_kcontrol_new vt1716s_dmic_mixer_sw = {
916 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
917 .name = "Digital Mic Capture Switch",
918 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
919 .count = 1,
920 .info = vt1716s_dmic_info,
921 .get = vt1716s_dmic_get,
922 .put = vt1716s_dmic_put,
923};
924
925
926/* mono-out mixer elements */
927static const struct snd_kcontrol_new vt1716S_mono_out_mixer =
928 HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT);
929
930static const struct hda_verb vt1716S_init_verbs[] = {
931 /* Enable Boost Volume backdoor */
932 {0x1, 0xf8a, 0x80},
933 /* don't bybass mixer */
934 {0x1, 0xf88, 0xc0},
935 /* Enable mono output */
936 {0x1, 0xf90, 0x08},
937 { }
938};
939
940static int patch_vt1716S(struct hda_codec *codec)
941{
942 struct via_spec *spec;
943 int err;
944
945 /* create a codec specific record */
946 spec = via_new_spec(codec);
947 if (spec == NULL)
948 return -ENOMEM;
949
950 spec->gen.mixer_nid = 0x16;
951 override_mic_boost(codec, 0x1a, 0, 3, 40);
952 override_mic_boost(codec, 0x1e, 0, 3, 40);
953
954 err = snd_hda_add_verbs(codec, vt1716S_init_verbs);
955 if (err < 0)
956 goto error;
957
958 /* automatic parse from the BIOS config */
959 err = via_parse_auto_config(codec);
960 if (err < 0)
961 goto error;
962
963 if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &vt1716s_dmic_mixer_vol) ||
964 !snd_hda_gen_add_kctl(&spec->gen, NULL, &vt1716s_dmic_mixer_sw) ||
965 !snd_hda_gen_add_kctl(&spec->gen, NULL, &vt1716S_mono_out_mixer)) {
966 err = -ENOMEM;
967 goto error;
968 }
969
970 return 0;
971
972 error:
973 via_free(codec);
974 return err;
975}
976
977/* for vt2002P */
978
979static const struct hda_verb vt2002P_init_verbs[] = {
980 /* Class-D speaker related verbs */
981 {0x1, 0xfe0, 0x4},
982 {0x1, 0xfe9, 0x80},
983 {0x1, 0xfe2, 0x22},
984 /* Enable Boost Volume backdoor */
985 {0x1, 0xfb9, 0x24},
986 /* Enable AOW0 to MW9 */
987 {0x1, 0xfb8, 0x88},
988 { }
989};
990
991static const struct hda_verb vt1802_init_verbs[] = {
992 /* Enable Boost Volume backdoor */
993 {0x1, 0xfb9, 0x24},
994 /* Enable AOW0 to MW9 */
995 {0x1, 0xfb8, 0x88},
996 { }
997};
998
999/*
1000 * pin fix-up
1001 */
1002enum {
1003 VIA_FIXUP_INTMIC_BOOST,
1004 VIA_FIXUP_ASUS_G75,
1005};
1006
1007static void via_fixup_intmic_boost(struct hda_codec *codec,
1008 const struct hda_fixup *fix, int action)
1009{
1010 if (action == HDA_FIXUP_ACT_PRE_PROBE)
1011 override_mic_boost(codec, 0x30, 0, 2, 40);
1012}
1013
1014static const struct hda_fixup via_fixups[] = {
1015 [VIA_FIXUP_INTMIC_BOOST] = {
1016 .type = HDA_FIXUP_FUNC,
1017 .v.func = via_fixup_intmic_boost,
1018 },
1019 [VIA_FIXUP_ASUS_G75] = {
1020 .type = HDA_FIXUP_PINS,
1021 .v.pins = (const struct hda_pintbl[]) {
1022 /* set 0x24 and 0x33 as speakers */
1023 { 0x24, 0x991301f0 },
1024 { 0x33, 0x991301f1 }, /* subwoofer */
1025 { }
1026 }
1027 },
1028};
1029
1030static const struct snd_pci_quirk vt2002p_fixups[] = {
1031 SND_PCI_QUIRK(0x1043, 0x1487, "Asus G75", VIA_FIXUP_ASUS_G75),
1032 SND_PCI_QUIRK(0x1043, 0x8532, "Asus X202E", VIA_FIXUP_INTMIC_BOOST),
1033 {}
1034};
1035
1036/* NIDs 0x24 and 0x33 on VT1802 have connections to non-existing NID 0x3e
1037 * Replace this with mixer NID 0x1c
1038 */
1039static void fix_vt1802_connections(struct hda_codec *codec)
1040{
1041 static const hda_nid_t conn_24[] = { 0x14, 0x1c };
1042 static const hda_nid_t conn_33[] = { 0x1c };
1043
1044 snd_hda_override_conn_list(codec, 0x24, ARRAY_SIZE(conn_24), conn_24);
1045 snd_hda_override_conn_list(codec, 0x33, ARRAY_SIZE(conn_33), conn_33);
1046}
1047
1048/* patch for vt2002P */
1049static int patch_vt2002P(struct hda_codec *codec)
1050{
1051 struct via_spec *spec;
1052 int err;
1053
1054 /* create a codec specific record */
1055 spec = via_new_spec(codec);
1056 if (spec == NULL)
1057 return -ENOMEM;
1058
1059 spec->gen.mixer_nid = 0x21;
1060 override_mic_boost(codec, 0x2b, 0, 3, 40);
1061 override_mic_boost(codec, 0x29, 0, 3, 40);
1062 if (spec->codec_type == VT1802)
1063 fix_vt1802_connections(codec);
1064 add_secret_dac_path(codec);
1065
1066 snd_hda_pick_fixup(codec, NULL, vt2002p_fixups, via_fixups);
1067 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1068
1069 if (spec->codec_type == VT1802)
1070 err = snd_hda_add_verbs(codec, vt1802_init_verbs);
1071 else
1072 err = snd_hda_add_verbs(codec, vt2002P_init_verbs);
1073 if (err < 0)
1074 goto error;
1075
1076 /* automatic parse from the BIOS config */
1077 err = via_parse_auto_config(codec);
1078 if (err < 0)
1079 goto error;
1080
1081 return 0;
1082
1083 error:
1084 via_free(codec);
1085 return err;
1086}
1087
1088/* for vt1812 */
1089
1090static const struct hda_verb vt1812_init_verbs[] = {
1091 /* Enable Boost Volume backdoor */
1092 {0x1, 0xfb9, 0x24},
1093 /* Enable AOW0 to MW9 */
1094 {0x1, 0xfb8, 0xa8},
1095 { }
1096};
1097
1098/* patch for vt1812 */
1099static int patch_vt1812(struct hda_codec *codec)
1100{
1101 struct via_spec *spec;
1102 int err;
1103
1104 /* create a codec specific record */
1105 spec = via_new_spec(codec);
1106 if (spec == NULL)
1107 return -ENOMEM;
1108
1109 spec->gen.mixer_nid = 0x21;
1110 override_mic_boost(codec, 0x2b, 0, 3, 40);
1111 override_mic_boost(codec, 0x29, 0, 3, 40);
1112 add_secret_dac_path(codec);
1113
1114 err = snd_hda_add_verbs(codec, vt1812_init_verbs);
1115 if (err < 0)
1116 goto error;
1117
1118 /* automatic parse from the BIOS config */
1119 err = via_parse_auto_config(codec);
1120 if (err < 0)
1121 goto error;
1122
1123 return 0;
1124
1125 error:
1126 via_free(codec);
1127 return err;
1128}
1129
1130/* patch for vt3476 */
1131
1132static const struct hda_verb vt3476_init_verbs[] = {
1133 /* Enable DMic 8/16/32K */
1134 {0x1, 0xF7B, 0x30},
1135 /* Enable Boost Volume backdoor */
1136 {0x1, 0xFB9, 0x20},
1137 /* Enable AOW-MW9 path */
1138 {0x1, 0xFB8, 0x10},
1139 { }
1140};
1141
1142static int patch_vt3476(struct hda_codec *codec)
1143{
1144 struct via_spec *spec;
1145 int err;
1146
1147 /* create a codec specific record */
1148 spec = via_new_spec(codec);
1149 if (spec == NULL)
1150 return -ENOMEM;
1151
1152 spec->gen.mixer_nid = 0x3f;
1153 add_secret_dac_path(codec);
1154
1155 err = snd_hda_add_verbs(codec, vt3476_init_verbs);
1156 if (err < 0)
1157 goto error;
1158
1159 /* automatic parse from the BIOS config */
1160 err = via_parse_auto_config(codec);
1161 if (err < 0)
1162 goto error;
1163
1164 return 0;
1165
1166 error:
1167 via_free(codec);
1168 return err;
1169}
1170
1171/*
1172 * patch entries
1173 */
1174static const struct hda_device_id snd_hda_id_via[] = {
1175 HDA_CODEC_ENTRY(0x11061708, "VT1708", patch_vt1708),
1176 HDA_CODEC_ENTRY(0x11061709, "VT1708", patch_vt1708),
1177 HDA_CODEC_ENTRY(0x1106170a, "VT1708", patch_vt1708),
1178 HDA_CODEC_ENTRY(0x1106170b, "VT1708", patch_vt1708),
1179 HDA_CODEC_ENTRY(0x1106e710, "VT1709 10-Ch", patch_vt1709),
1180 HDA_CODEC_ENTRY(0x1106e711, "VT1709 10-Ch", patch_vt1709),
1181 HDA_CODEC_ENTRY(0x1106e712, "VT1709 10-Ch", patch_vt1709),
1182 HDA_CODEC_ENTRY(0x1106e713, "VT1709 10-Ch", patch_vt1709),
1183 HDA_CODEC_ENTRY(0x1106e714, "VT1709 6-Ch", patch_vt1709),
1184 HDA_CODEC_ENTRY(0x1106e715, "VT1709 6-Ch", patch_vt1709),
1185 HDA_CODEC_ENTRY(0x1106e716, "VT1709 6-Ch", patch_vt1709),
1186 HDA_CODEC_ENTRY(0x1106e717, "VT1709 6-Ch", patch_vt1709),
1187 HDA_CODEC_ENTRY(0x1106e720, "VT1708B 8-Ch", patch_vt1708B),
1188 HDA_CODEC_ENTRY(0x1106e721, "VT1708B 8-Ch", patch_vt1708B),
1189 HDA_CODEC_ENTRY(0x1106e722, "VT1708B 8-Ch", patch_vt1708B),
1190 HDA_CODEC_ENTRY(0x1106e723, "VT1708B 8-Ch", patch_vt1708B),
1191 HDA_CODEC_ENTRY(0x1106e724, "VT1708B 4-Ch", patch_vt1708B),
1192 HDA_CODEC_ENTRY(0x1106e725, "VT1708B 4-Ch", patch_vt1708B),
1193 HDA_CODEC_ENTRY(0x1106e726, "VT1708B 4-Ch", patch_vt1708B),
1194 HDA_CODEC_ENTRY(0x1106e727, "VT1708B 4-Ch", patch_vt1708B),
1195 HDA_CODEC_ENTRY(0x11060397, "VT1708S", patch_vt1708S),
1196 HDA_CODEC_ENTRY(0x11061397, "VT1708S", patch_vt1708S),
1197 HDA_CODEC_ENTRY(0x11062397, "VT1708S", patch_vt1708S),
1198 HDA_CODEC_ENTRY(0x11063397, "VT1708S", patch_vt1708S),
1199 HDA_CODEC_ENTRY(0x11064397, "VT1705", patch_vt1708S),
1200 HDA_CODEC_ENTRY(0x11065397, "VT1708S", patch_vt1708S),
1201 HDA_CODEC_ENTRY(0x11066397, "VT1708S", patch_vt1708S),
1202 HDA_CODEC_ENTRY(0x11067397, "VT1708S", patch_vt1708S),
1203 HDA_CODEC_ENTRY(0x11060398, "VT1702", patch_vt1702),
1204 HDA_CODEC_ENTRY(0x11061398, "VT1702", patch_vt1702),
1205 HDA_CODEC_ENTRY(0x11062398, "VT1702", patch_vt1702),
1206 HDA_CODEC_ENTRY(0x11063398, "VT1702", patch_vt1702),
1207 HDA_CODEC_ENTRY(0x11064398, "VT1702", patch_vt1702),
1208 HDA_CODEC_ENTRY(0x11065398, "VT1702", patch_vt1702),
1209 HDA_CODEC_ENTRY(0x11066398, "VT1702", patch_vt1702),
1210 HDA_CODEC_ENTRY(0x11067398, "VT1702", patch_vt1702),
1211 HDA_CODEC_ENTRY(0x11060428, "VT1718S", patch_vt1718S),
1212 HDA_CODEC_ENTRY(0x11064428, "VT1718S", patch_vt1718S),
1213 HDA_CODEC_ENTRY(0x11060441, "VT2020", patch_vt1718S),
1214 HDA_CODEC_ENTRY(0x11064441, "VT1828S", patch_vt1718S),
1215 HDA_CODEC_ENTRY(0x11060433, "VT1716S", patch_vt1716S),
1216 HDA_CODEC_ENTRY(0x1106a721, "VT1716S", patch_vt1716S),
1217 HDA_CODEC_ENTRY(0x11060438, "VT2002P", patch_vt2002P),
1218 HDA_CODEC_ENTRY(0x11064438, "VT2002P", patch_vt2002P),
1219 HDA_CODEC_ENTRY(0x11060448, "VT1812", patch_vt1812),
1220 HDA_CODEC_ENTRY(0x11060440, "VT1818S", patch_vt1708S),
1221 HDA_CODEC_ENTRY(0x11060446, "VT1802", patch_vt2002P),
1222 HDA_CODEC_ENTRY(0x11068446, "VT1802", patch_vt2002P),
1223 HDA_CODEC_ENTRY(0x11064760, "VT1705CF", patch_vt3476),
1224 HDA_CODEC_ENTRY(0x11064761, "VT1708SCE", patch_vt3476),
1225 HDA_CODEC_ENTRY(0x11064762, "VT1808", patch_vt3476),
1226 {} /* terminator */
1227};
1228MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_via);
1229
1230static struct hda_codec_driver via_driver = {
1231 .id = snd_hda_id_via,
1232};
1233
1234MODULE_LICENSE("GPL");
1235MODULE_DESCRIPTION("VIA HD-audio codec");
1236
1237module_hda_codec_driver(via_driver);
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Universal Interface for Intel High Definition Audio Codec
4 *
5 * HD audio interface patch for VIA VT17xx/VT18xx/VT20xx codec
6 *
7 * (C) 2006-2009 VIA Technology, Inc.
8 * (C) 2006-2008 Takashi Iwai <tiwai@suse.de>
9 */
10
11/* * * * * * * * * * * * * * Release History * * * * * * * * * * * * * * * * */
12/* */
13/* 2006-03-03 Lydia Wang Create the basic patch to support VT1708 codec */
14/* 2006-03-14 Lydia Wang Modify hard code for some pin widget nid */
15/* 2006-08-02 Lydia Wang Add support to VT1709 codec */
16/* 2006-09-08 Lydia Wang Fix internal loopback recording source select bug */
17/* 2007-09-12 Lydia Wang Add EAPD enable during driver initialization */
18/* 2007-09-17 Lydia Wang Add VT1708B codec support */
19/* 2007-11-14 Lydia Wang Add VT1708A codec HP and CD pin connect config */
20/* 2008-02-03 Lydia Wang Fix Rear channels and Back channels inverse issue */
21/* 2008-03-06 Lydia Wang Add VT1702 codec and VT1708S codec support */
22/* 2008-04-09 Lydia Wang Add mute front speaker when HP plugin */
23/* 2008-04-09 Lydia Wang Add Independent HP feature */
24/* 2008-05-28 Lydia Wang Add second S/PDIF Out support for VT1702 */
25/* 2008-09-15 Logan Li Add VT1708S Mic Boost workaround/backdoor */
26/* 2009-02-16 Logan Li Add support for VT1718S */
27/* 2009-03-13 Logan Li Add support for VT1716S */
28/* 2009-04-14 Lydai Wang Add support for VT1828S and VT2020 */
29/* 2009-07-08 Lydia Wang Add support for VT2002P */
30/* 2009-07-21 Lydia Wang Add support for VT1812 */
31/* 2009-09-19 Lydia Wang Add support for VT1818S */
32/* */
33/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
34
35
36#include <linux/init.h>
37#include <linux/delay.h>
38#include <linux/slab.h>
39#include <linux/module.h>
40#include <sound/core.h>
41#include <sound/asoundef.h>
42#include <sound/hda_codec.h>
43#include "hda_local.h"
44#include "hda_auto_parser.h"
45#include "hda_jack.h"
46#include "hda_generic.h"
47
48/* Pin Widget NID */
49#define VT1708_HP_PIN_NID 0x20
50#define VT1708_CD_PIN_NID 0x24
51
52enum VIA_HDA_CODEC {
53 UNKNOWN = -1,
54 VT1708,
55 VT1709_10CH,
56 VT1709_6CH,
57 VT1708B_8CH,
58 VT1708B_4CH,
59 VT1708S,
60 VT1708BCE,
61 VT1702,
62 VT1718S,
63 VT1716S,
64 VT2002P,
65 VT1812,
66 VT1802,
67 VT1705CF,
68 VT1808,
69 CODEC_TYPES,
70};
71
72#define VT2002P_COMPATIBLE(spec) \
73 ((spec)->codec_type == VT2002P ||\
74 (spec)->codec_type == VT1812 ||\
75 (spec)->codec_type == VT1802)
76
77struct via_spec {
78 struct hda_gen_spec gen;
79
80 /* HP mode source */
81 unsigned int dmic_enabled;
82 enum VIA_HDA_CODEC codec_type;
83
84 /* analog low-power control */
85 bool alc_mode;
86
87 /* work to check hp jack state */
88 int hp_work_active;
89 int vt1708_jack_detect;
90};
91
92static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
93static void via_playback_pcm_hook(struct hda_pcm_stream *hinfo,
94 struct hda_codec *codec,
95 struct snd_pcm_substream *substream,
96 int action);
97
98static const struct hda_codec_ops via_patch_ops; /* defined below */
99
100static struct via_spec *via_new_spec(struct hda_codec *codec)
101{
102 struct via_spec *spec;
103
104 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
105 if (spec == NULL)
106 return NULL;
107
108 codec->spec = spec;
109 snd_hda_gen_spec_init(&spec->gen);
110 spec->codec_type = get_codec_type(codec);
111 /* VT1708BCE & VT1708S are almost same */
112 if (spec->codec_type == VT1708BCE)
113 spec->codec_type = VT1708S;
114 spec->gen.indep_hp = 1;
115 spec->gen.keep_eapd_on = 1;
116 spec->gen.dac_min_mute = 1;
117 spec->gen.pcm_playback_hook = via_playback_pcm_hook;
118 spec->gen.add_stereo_mix_input = HDA_HINT_STEREO_MIX_AUTO;
119 codec->power_save_node = 1;
120 spec->gen.power_down_unused = 1;
121 codec->patch_ops = via_patch_ops;
122 return spec;
123}
124
125static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
126{
127 u32 vendor_id = codec->core.vendor_id;
128 u16 ven_id = vendor_id >> 16;
129 u16 dev_id = vendor_id & 0xffff;
130 enum VIA_HDA_CODEC codec_type;
131
132 /* get codec type */
133 if (ven_id != 0x1106)
134 codec_type = UNKNOWN;
135 else if (dev_id >= 0x1708 && dev_id <= 0x170b)
136 codec_type = VT1708;
137 else if (dev_id >= 0xe710 && dev_id <= 0xe713)
138 codec_type = VT1709_10CH;
139 else if (dev_id >= 0xe714 && dev_id <= 0xe717)
140 codec_type = VT1709_6CH;
141 else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
142 codec_type = VT1708B_8CH;
143 if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
144 codec_type = VT1708BCE;
145 } else if (dev_id >= 0xe724 && dev_id <= 0xe727)
146 codec_type = VT1708B_4CH;
147 else if ((dev_id & 0xfff) == 0x397
148 && (dev_id >> 12) < 8)
149 codec_type = VT1708S;
150 else if ((dev_id & 0xfff) == 0x398
151 && (dev_id >> 12) < 8)
152 codec_type = VT1702;
153 else if ((dev_id & 0xfff) == 0x428
154 && (dev_id >> 12) < 8)
155 codec_type = VT1718S;
156 else if (dev_id == 0x0433 || dev_id == 0xa721)
157 codec_type = VT1716S;
158 else if (dev_id == 0x0441 || dev_id == 0x4441)
159 codec_type = VT1718S;
160 else if (dev_id == 0x0438 || dev_id == 0x4438)
161 codec_type = VT2002P;
162 else if (dev_id == 0x0448)
163 codec_type = VT1812;
164 else if (dev_id == 0x0440)
165 codec_type = VT1708S;
166 else if ((dev_id & 0xfff) == 0x446)
167 codec_type = VT1802;
168 else if (dev_id == 0x4760)
169 codec_type = VT1705CF;
170 else if (dev_id == 0x4761 || dev_id == 0x4762)
171 codec_type = VT1808;
172 else
173 codec_type = UNKNOWN;
174 return codec_type;
175};
176
177static void analog_low_current_mode(struct hda_codec *codec);
178static bool is_aa_path_mute(struct hda_codec *codec);
179
180#define hp_detect_with_aa(codec) \
181 (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1 && \
182 !is_aa_path_mute(codec))
183
184static void vt1708_stop_hp_work(struct hda_codec *codec)
185{
186 struct via_spec *spec = codec->spec;
187 if (spec->codec_type != VT1708 || !spec->gen.autocfg.hp_outs)
188 return;
189 if (spec->hp_work_active) {
190 snd_hda_codec_write(codec, 0x1, 0, 0xf81, 1);
191 codec->jackpoll_interval = 0;
192 cancel_delayed_work_sync(&codec->jackpoll_work);
193 spec->hp_work_active = false;
194 }
195}
196
197static void vt1708_update_hp_work(struct hda_codec *codec)
198{
199 struct via_spec *spec = codec->spec;
200 if (spec->codec_type != VT1708 || !spec->gen.autocfg.hp_outs)
201 return;
202 if (spec->vt1708_jack_detect) {
203 if (!spec->hp_work_active) {
204 codec->jackpoll_interval = msecs_to_jiffies(100);
205 snd_hda_codec_write(codec, 0x1, 0, 0xf81, 0);
206 schedule_delayed_work(&codec->jackpoll_work, 0);
207 spec->hp_work_active = true;
208 }
209 } else if (!hp_detect_with_aa(codec))
210 vt1708_stop_hp_work(codec);
211}
212
213static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
214 struct snd_ctl_elem_info *uinfo)
215{
216 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
217}
218
219static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
220 struct snd_ctl_elem_value *ucontrol)
221{
222 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
223 struct via_spec *spec = codec->spec;
224
225 ucontrol->value.enumerated.item[0] = spec->gen.power_down_unused;
226 return 0;
227}
228
229static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
230 struct snd_ctl_elem_value *ucontrol)
231{
232 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
233 struct via_spec *spec = codec->spec;
234 bool val = !!ucontrol->value.enumerated.item[0];
235
236 if (val == spec->gen.power_down_unused)
237 return 0;
238 /* codec->power_save_node = val; */ /* widget PM seems yet broken */
239 spec->gen.power_down_unused = val;
240 analog_low_current_mode(codec);
241 return 1;
242}
243
244static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
245 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
246 .name = "Dynamic Power-Control",
247 .info = via_pin_power_ctl_info,
248 .get = via_pin_power_ctl_get,
249 .put = via_pin_power_ctl_put,
250};
251
252#ifdef CONFIG_SND_HDA_INPUT_BEEP
253/* additional beep mixers; the actual parameters are overwritten at build */
254static const struct snd_kcontrol_new via_beep_mixer[] = {
255 HDA_CODEC_VOLUME_MONO("Beep Playback Volume", 0, 1, 0, HDA_OUTPUT),
256 HDA_CODEC_MUTE_BEEP_MONO("Beep Playback Switch", 0, 1, 0, HDA_OUTPUT),
257};
258
259static int set_beep_amp(struct via_spec *spec, hda_nid_t nid,
260 int idx, int dir)
261{
262 struct snd_kcontrol_new *knew;
263 unsigned int beep_amp = HDA_COMPOSE_AMP_VAL(nid, 1, idx, dir);
264 int i;
265
266 spec->gen.beep_nid = nid;
267 for (i = 0; i < ARRAY_SIZE(via_beep_mixer); i++) {
268 knew = snd_hda_gen_add_kctl(&spec->gen, NULL,
269 &via_beep_mixer[i]);
270 if (!knew)
271 return -ENOMEM;
272 knew->private_value = beep_amp;
273 }
274 return 0;
275}
276
277static int auto_parse_beep(struct hda_codec *codec)
278{
279 struct via_spec *spec = codec->spec;
280 hda_nid_t nid;
281
282 for_each_hda_codec_node(nid, codec)
283 if (get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_BEEP)
284 return set_beep_amp(spec, nid, 0, HDA_OUTPUT);
285 return 0;
286}
287#else
288#define auto_parse_beep(codec) 0
289#endif
290
291/* check AA path's mute status */
292static bool is_aa_path_mute(struct hda_codec *codec)
293{
294 struct via_spec *spec = codec->spec;
295 const struct hda_amp_list *p;
296 int ch, v;
297
298 p = spec->gen.loopback.amplist;
299 if (!p)
300 return true;
301 for (; p->nid; p++) {
302 for (ch = 0; ch < 2; ch++) {
303 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
304 p->idx);
305 if (!(v & HDA_AMP_MUTE) && v > 0)
306 return false;
307 }
308 }
309 return true;
310}
311
312/* enter/exit analog low-current mode */
313static void __analog_low_current_mode(struct hda_codec *codec, bool force)
314{
315 struct via_spec *spec = codec->spec;
316 bool enable;
317 unsigned int verb, parm;
318
319 if (!codec->power_save_node)
320 enable = false;
321 else
322 enable = is_aa_path_mute(codec) && !spec->gen.active_streams;
323 if (enable == spec->alc_mode && !force)
324 return;
325 spec->alc_mode = enable;
326
327 /* decide low current mode's verb & parameter */
328 switch (spec->codec_type) {
329 case VT1708B_8CH:
330 case VT1708B_4CH:
331 verb = 0xf70;
332 parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
333 break;
334 case VT1708S:
335 case VT1718S:
336 case VT1716S:
337 verb = 0xf73;
338 parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
339 break;
340 case VT1702:
341 verb = 0xf73;
342 parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
343 break;
344 case VT2002P:
345 case VT1812:
346 case VT1802:
347 verb = 0xf93;
348 parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
349 break;
350 case VT1705CF:
351 case VT1808:
352 verb = 0xf82;
353 parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
354 break;
355 default:
356 return; /* other codecs are not supported */
357 }
358 /* send verb */
359 snd_hda_codec_write(codec, codec->core.afg, 0, verb, parm);
360}
361
362static void analog_low_current_mode(struct hda_codec *codec)
363{
364 return __analog_low_current_mode(codec, false);
365}
366
367static void via_playback_pcm_hook(struct hda_pcm_stream *hinfo,
368 struct hda_codec *codec,
369 struct snd_pcm_substream *substream,
370 int action)
371{
372 analog_low_current_mode(codec);
373 vt1708_update_hp_work(codec);
374}
375
376static void via_free(struct hda_codec *codec)
377{
378 vt1708_stop_hp_work(codec);
379 snd_hda_gen_free(codec);
380}
381
382#ifdef CONFIG_PM
383static int via_suspend(struct hda_codec *codec)
384{
385 struct via_spec *spec = codec->spec;
386 vt1708_stop_hp_work(codec);
387
388 /* Fix pop noise on headphones */
389 if (spec->codec_type == VT1802)
390 snd_hda_shutup_pins(codec);
391
392 return 0;
393}
394
395static int via_resume(struct hda_codec *codec)
396{
397 /* some delay here to make jack detection working (bko#98921) */
398 msleep(10);
399 codec->patch_ops.init(codec);
400 snd_hda_regmap_sync(codec);
401 return 0;
402}
403#endif
404
405#ifdef CONFIG_PM
406static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
407{
408 struct via_spec *spec = codec->spec;
409 analog_low_current_mode(codec);
410 vt1708_update_hp_work(codec);
411 return snd_hda_check_amp_list_power(codec, &spec->gen.loopback, nid);
412}
413#endif
414
415/*
416 */
417
418static int via_init(struct hda_codec *codec);
419
420static const struct hda_codec_ops via_patch_ops = {
421 .build_controls = snd_hda_gen_build_controls,
422 .build_pcms = snd_hda_gen_build_pcms,
423 .init = via_init,
424 .free = via_free,
425 .unsol_event = snd_hda_jack_unsol_event,
426#ifdef CONFIG_PM
427 .suspend = via_suspend,
428 .resume = via_resume,
429 .check_power_status = via_check_power_status,
430#endif
431};
432
433
434static const struct hda_verb vt1708_init_verbs[] = {
435 /* power down jack detect function */
436 {0x1, 0xf81, 0x1},
437 { }
438};
439static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
440{
441 unsigned int def_conf;
442 unsigned char seqassoc;
443
444 def_conf = snd_hda_codec_get_pincfg(codec, nid);
445 seqassoc = (unsigned char) get_defcfg_association(def_conf);
446 seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
447 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
448 && (seqassoc == 0xf0 || seqassoc == 0xff)) {
449 def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
450 snd_hda_codec_set_pincfg(codec, nid, def_conf);
451 }
452}
453
454static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
455 struct snd_ctl_elem_value *ucontrol)
456{
457 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
458 struct via_spec *spec = codec->spec;
459
460 if (spec->codec_type != VT1708)
461 return 0;
462 ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
463 return 0;
464}
465
466static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
467 struct snd_ctl_elem_value *ucontrol)
468{
469 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
470 struct via_spec *spec = codec->spec;
471 int val;
472
473 if (spec->codec_type != VT1708)
474 return 0;
475 val = !!ucontrol->value.integer.value[0];
476 if (spec->vt1708_jack_detect == val)
477 return 0;
478 spec->vt1708_jack_detect = val;
479 vt1708_update_hp_work(codec);
480 return 1;
481}
482
483static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
484 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
485 .name = "Jack Detect",
486 .count = 1,
487 .info = snd_ctl_boolean_mono_info,
488 .get = vt1708_jack_detect_get,
489 .put = vt1708_jack_detect_put,
490};
491
492static const struct badness_table via_main_out_badness = {
493 .no_primary_dac = 0x10000,
494 .no_dac = 0x4000,
495 .shared_primary = 0x10000,
496 .shared_surr = 0x20,
497 .shared_clfe = 0x20,
498 .shared_surr_main = 0x20,
499};
500static const struct badness_table via_extra_out_badness = {
501 .no_primary_dac = 0x4000,
502 .no_dac = 0x4000,
503 .shared_primary = 0x12,
504 .shared_surr = 0x20,
505 .shared_clfe = 0x20,
506 .shared_surr_main = 0x10,
507};
508
509static int via_parse_auto_config(struct hda_codec *codec)
510{
511 struct via_spec *spec = codec->spec;
512 int err;
513
514 spec->gen.main_out_badness = &via_main_out_badness;
515 spec->gen.extra_out_badness = &via_extra_out_badness;
516
517 err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
518 if (err < 0)
519 return err;
520
521 err = auto_parse_beep(codec);
522 if (err < 0)
523 return err;
524
525 err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
526 if (err < 0)
527 return err;
528
529 if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &via_pin_power_ctl_enum))
530 return -ENOMEM;
531
532 /* disable widget PM at start for compatibility */
533 codec->power_save_node = 0;
534 spec->gen.power_down_unused = 0;
535 return 0;
536}
537
538static int via_init(struct hda_codec *codec)
539{
540 /* init power states */
541 __analog_low_current_mode(codec, true);
542
543 snd_hda_gen_init(codec);
544
545 vt1708_update_hp_work(codec);
546
547 return 0;
548}
549
550static int vt1708_build_controls(struct hda_codec *codec)
551{
552 /* In order not to create "Phantom Jack" controls,
553 temporary enable jackpoll */
554 int err;
555 int old_interval = codec->jackpoll_interval;
556 codec->jackpoll_interval = msecs_to_jiffies(100);
557 err = snd_hda_gen_build_controls(codec);
558 codec->jackpoll_interval = old_interval;
559 return err;
560}
561
562static int vt1708_build_pcms(struct hda_codec *codec)
563{
564 struct via_spec *spec = codec->spec;
565 int i, err;
566
567 err = snd_hda_gen_build_pcms(codec);
568 if (err < 0 || codec->core.vendor_id != 0x11061708)
569 return err;
570
571 /* We got noisy outputs on the right channel on VT1708 when
572 * 24bit samples are used. Until any workaround is found,
573 * disable the 24bit format, so far.
574 */
575 for (i = 0; i < ARRAY_SIZE(spec->gen.pcm_rec); i++) {
576 struct hda_pcm *info = spec->gen.pcm_rec[i];
577 if (!info)
578 continue;
579 if (!info->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams ||
580 info->pcm_type != HDA_PCM_TYPE_AUDIO)
581 continue;
582 info->stream[SNDRV_PCM_STREAM_PLAYBACK].formats =
583 SNDRV_PCM_FMTBIT_S16_LE;
584 }
585
586 return 0;
587}
588
589static int patch_vt1708(struct hda_codec *codec)
590{
591 struct via_spec *spec;
592 int err;
593
594 /* create a codec specific record */
595 spec = via_new_spec(codec);
596 if (spec == NULL)
597 return -ENOMEM;
598
599 /* override some patch_ops */
600 codec->patch_ops.build_controls = vt1708_build_controls;
601 codec->patch_ops.build_pcms = vt1708_build_pcms;
602 spec->gen.mixer_nid = 0x17;
603
604 /* set jackpoll_interval while parsing the codec */
605 codec->jackpoll_interval = msecs_to_jiffies(100);
606 spec->vt1708_jack_detect = 1;
607
608 /* don't support the input jack switching due to lack of unsol event */
609 /* (it may work with polling, though, but it needs testing) */
610 spec->gen.suppress_auto_mic = 1;
611 /* Some machines show the broken speaker mute */
612 spec->gen.auto_mute_via_amp = 1;
613
614 /* Add HP and CD pin config connect bit re-config action */
615 vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
616 vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
617
618 err = snd_hda_add_verbs(codec, vt1708_init_verbs);
619 if (err < 0)
620 goto error;
621
622 /* automatic parse from the BIOS config */
623 err = via_parse_auto_config(codec);
624 if (err < 0)
625 goto error;
626
627 /* add jack detect on/off control */
628 if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &vt1708_jack_detect_ctl)) {
629 err = -ENOMEM;
630 goto error;
631 }
632
633 /* clear jackpoll_interval again; it's set dynamically */
634 codec->jackpoll_interval = 0;
635
636 return 0;
637
638 error:
639 via_free(codec);
640 return err;
641}
642
643static int patch_vt1709(struct hda_codec *codec)
644{
645 struct via_spec *spec;
646 int err;
647
648 /* create a codec specific record */
649 spec = via_new_spec(codec);
650 if (spec == NULL)
651 return -ENOMEM;
652
653 spec->gen.mixer_nid = 0x18;
654
655 err = via_parse_auto_config(codec);
656 if (err < 0)
657 goto error;
658
659 return 0;
660
661 error:
662 via_free(codec);
663 return err;
664}
665
666static int patch_vt1708S(struct hda_codec *codec);
667static int patch_vt1708B(struct hda_codec *codec)
668{
669 struct via_spec *spec;
670 int err;
671
672 if (get_codec_type(codec) == VT1708BCE)
673 return patch_vt1708S(codec);
674
675 /* create a codec specific record */
676 spec = via_new_spec(codec);
677 if (spec == NULL)
678 return -ENOMEM;
679
680 spec->gen.mixer_nid = 0x16;
681
682 /* automatic parse from the BIOS config */
683 err = via_parse_auto_config(codec);
684 if (err < 0)
685 goto error;
686
687 return 0;
688
689 error:
690 via_free(codec);
691 return err;
692}
693
694/* Patch for VT1708S */
695static const struct hda_verb vt1708S_init_verbs[] = {
696 /* Enable Mic Boost Volume backdoor */
697 {0x1, 0xf98, 0x1},
698 /* don't bybass mixer */
699 {0x1, 0xf88, 0xc0},
700 { }
701};
702
703static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
704 int offset, int num_steps, int step_size)
705{
706 snd_hda_override_wcaps(codec, pin,
707 get_wcaps(codec, pin) | AC_WCAP_IN_AMP);
708 snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
709 (offset << AC_AMPCAP_OFFSET_SHIFT) |
710 (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
711 (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
712 (0 << AC_AMPCAP_MUTE_SHIFT));
713}
714
715static int patch_vt1708S(struct hda_codec *codec)
716{
717 struct via_spec *spec;
718 int err;
719
720 /* create a codec specific record */
721 spec = via_new_spec(codec);
722 if (spec == NULL)
723 return -ENOMEM;
724
725 spec->gen.mixer_nid = 0x16;
726 override_mic_boost(codec, 0x1a, 0, 3, 40);
727 override_mic_boost(codec, 0x1e, 0, 3, 40);
728
729 /* correct names for VT1708BCE */
730 if (get_codec_type(codec) == VT1708BCE)
731 snd_hda_codec_set_name(codec, "VT1708BCE");
732 /* correct names for VT1705 */
733 if (codec->core.vendor_id == 0x11064397)
734 snd_hda_codec_set_name(codec, "VT1705");
735
736 err = snd_hda_add_verbs(codec, vt1708S_init_verbs);
737 if (err < 0)
738 goto error;
739
740 /* automatic parse from the BIOS config */
741 err = via_parse_auto_config(codec);
742 if (err < 0)
743 goto error;
744
745 return 0;
746
747 error:
748 via_free(codec);
749 return err;
750}
751
752/* Patch for VT1702 */
753
754static const struct hda_verb vt1702_init_verbs[] = {
755 /* mixer enable */
756 {0x1, 0xF88, 0x3},
757 /* GPIO 0~2 */
758 {0x1, 0xF82, 0x3F},
759 { }
760};
761
762static int patch_vt1702(struct hda_codec *codec)
763{
764 struct via_spec *spec;
765 int err;
766
767 /* create a codec specific record */
768 spec = via_new_spec(codec);
769 if (spec == NULL)
770 return -ENOMEM;
771
772 spec->gen.mixer_nid = 0x1a;
773
774 /* limit AA path volume to 0 dB */
775 snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
776 (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
777 (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
778 (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
779 (1 << AC_AMPCAP_MUTE_SHIFT));
780
781 err = snd_hda_add_verbs(codec, vt1702_init_verbs);
782 if (err < 0)
783 goto error;
784
785 /* automatic parse from the BIOS config */
786 err = via_parse_auto_config(codec);
787 if (err < 0)
788 goto error;
789
790 return 0;
791
792 error:
793 via_free(codec);
794 return err;
795}
796
797/* Patch for VT1718S */
798
799static const struct hda_verb vt1718S_init_verbs[] = {
800 /* Enable MW0 adjust Gain 5 */
801 {0x1, 0xfb2, 0x10},
802 /* Enable Boost Volume backdoor */
803 {0x1, 0xf88, 0x8},
804
805 { }
806};
807
808/* Add a connection to the primary DAC from AA-mixer for some codecs
809 * This isn't listed from the raw info, but the chip has a secret connection.
810 */
811static int add_secret_dac_path(struct hda_codec *codec)
812{
813 struct via_spec *spec = codec->spec;
814 int i, nums;
815 hda_nid_t conn[8];
816 hda_nid_t nid;
817
818 if (!spec->gen.mixer_nid)
819 return 0;
820 nums = snd_hda_get_connections(codec, spec->gen.mixer_nid, conn,
821 ARRAY_SIZE(conn) - 1);
822 if (nums < 0)
823 return nums;
824
825 for (i = 0; i < nums; i++) {
826 if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT)
827 return 0;
828 }
829
830 /* find the primary DAC and add to the connection list */
831 for_each_hda_codec_node(nid, codec) {
832 unsigned int caps = get_wcaps(codec, nid);
833 if (get_wcaps_type(caps) == AC_WID_AUD_OUT &&
834 !(caps & AC_WCAP_DIGITAL)) {
835 conn[nums++] = nid;
836 return snd_hda_override_conn_list(codec,
837 spec->gen.mixer_nid,
838 nums, conn);
839 }
840 }
841 return 0;
842}
843
844
845static int patch_vt1718S(struct hda_codec *codec)
846{
847 struct via_spec *spec;
848 int err;
849
850 /* create a codec specific record */
851 spec = via_new_spec(codec);
852 if (spec == NULL)
853 return -ENOMEM;
854
855 spec->gen.mixer_nid = 0x21;
856 override_mic_boost(codec, 0x2b, 0, 3, 40);
857 override_mic_boost(codec, 0x29, 0, 3, 40);
858 add_secret_dac_path(codec);
859
860 err = snd_hda_add_verbs(codec, vt1718S_init_verbs);
861 if (err < 0)
862 goto error;
863
864 /* automatic parse from the BIOS config */
865 err = via_parse_auto_config(codec);
866 if (err < 0)
867 goto error;
868
869 return 0;
870
871 error:
872 via_free(codec);
873 return err;
874}
875
876/* Patch for VT1716S */
877
878static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
879 struct snd_ctl_elem_info *uinfo)
880{
881 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
882 uinfo->count = 1;
883 uinfo->value.integer.min = 0;
884 uinfo->value.integer.max = 1;
885 return 0;
886}
887
888static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
889 struct snd_ctl_elem_value *ucontrol)
890{
891 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
892 int index = 0;
893
894 index = snd_hda_codec_read(codec, 0x26, 0,
895 AC_VERB_GET_CONNECT_SEL, 0);
896 if (index != -1)
897 *ucontrol->value.integer.value = index;
898
899 return 0;
900}
901
902static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
903 struct snd_ctl_elem_value *ucontrol)
904{
905 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
906 struct via_spec *spec = codec->spec;
907 int index = *ucontrol->value.integer.value;
908
909 snd_hda_codec_write(codec, 0x26, 0,
910 AC_VERB_SET_CONNECT_SEL, index);
911 spec->dmic_enabled = index;
912 return 1;
913}
914
915static const struct snd_kcontrol_new vt1716s_dmic_mixer_vol =
916 HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT);
917static const struct snd_kcontrol_new vt1716s_dmic_mixer_sw = {
918 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
919 .name = "Digital Mic Capture Switch",
920 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
921 .count = 1,
922 .info = vt1716s_dmic_info,
923 .get = vt1716s_dmic_get,
924 .put = vt1716s_dmic_put,
925};
926
927
928/* mono-out mixer elements */
929static const struct snd_kcontrol_new vt1716S_mono_out_mixer =
930 HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT);
931
932static const struct hda_verb vt1716S_init_verbs[] = {
933 /* Enable Boost Volume backdoor */
934 {0x1, 0xf8a, 0x80},
935 /* don't bybass mixer */
936 {0x1, 0xf88, 0xc0},
937 /* Enable mono output */
938 {0x1, 0xf90, 0x08},
939 { }
940};
941
942static int patch_vt1716S(struct hda_codec *codec)
943{
944 struct via_spec *spec;
945 int err;
946
947 /* create a codec specific record */
948 spec = via_new_spec(codec);
949 if (spec == NULL)
950 return -ENOMEM;
951
952 spec->gen.mixer_nid = 0x16;
953 override_mic_boost(codec, 0x1a, 0, 3, 40);
954 override_mic_boost(codec, 0x1e, 0, 3, 40);
955
956 err = snd_hda_add_verbs(codec, vt1716S_init_verbs);
957 if (err < 0)
958 goto error;
959
960 /* automatic parse from the BIOS config */
961 err = via_parse_auto_config(codec);
962 if (err < 0)
963 goto error;
964
965 if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &vt1716s_dmic_mixer_vol) ||
966 !snd_hda_gen_add_kctl(&spec->gen, NULL, &vt1716s_dmic_mixer_sw) ||
967 !snd_hda_gen_add_kctl(&spec->gen, NULL, &vt1716S_mono_out_mixer)) {
968 err = -ENOMEM;
969 goto error;
970 }
971
972 return 0;
973
974 error:
975 via_free(codec);
976 return err;
977}
978
979/* for vt2002P */
980
981static const struct hda_verb vt2002P_init_verbs[] = {
982 /* Class-D speaker related verbs */
983 {0x1, 0xfe0, 0x4},
984 {0x1, 0xfe9, 0x80},
985 {0x1, 0xfe2, 0x22},
986 /* Enable Boost Volume backdoor */
987 {0x1, 0xfb9, 0x24},
988 /* Enable AOW0 to MW9 */
989 {0x1, 0xfb8, 0x88},
990 { }
991};
992
993static const struct hda_verb vt1802_init_verbs[] = {
994 /* Enable Boost Volume backdoor */
995 {0x1, 0xfb9, 0x24},
996 /* Enable AOW0 to MW9 */
997 {0x1, 0xfb8, 0x88},
998 { }
999};
1000
1001/*
1002 * pin fix-up
1003 */
1004enum {
1005 VIA_FIXUP_INTMIC_BOOST,
1006 VIA_FIXUP_ASUS_G75,
1007 VIA_FIXUP_POWER_SAVE,
1008};
1009
1010static void via_fixup_intmic_boost(struct hda_codec *codec,
1011 const struct hda_fixup *fix, int action)
1012{
1013 if (action == HDA_FIXUP_ACT_PRE_PROBE)
1014 override_mic_boost(codec, 0x30, 0, 2, 40);
1015}
1016
1017static void via_fixup_power_save(struct hda_codec *codec,
1018 const struct hda_fixup *fix, int action)
1019{
1020 if (action == HDA_FIXUP_ACT_PRE_PROBE)
1021 codec->power_save_node = 0;
1022}
1023
1024static const struct hda_fixup via_fixups[] = {
1025 [VIA_FIXUP_INTMIC_BOOST] = {
1026 .type = HDA_FIXUP_FUNC,
1027 .v.func = via_fixup_intmic_boost,
1028 },
1029 [VIA_FIXUP_ASUS_G75] = {
1030 .type = HDA_FIXUP_PINS,
1031 .v.pins = (const struct hda_pintbl[]) {
1032 /* set 0x24 and 0x33 as speakers */
1033 { 0x24, 0x991301f0 },
1034 { 0x33, 0x991301f1 }, /* subwoofer */
1035 { }
1036 }
1037 },
1038 [VIA_FIXUP_POWER_SAVE] = {
1039 .type = HDA_FIXUP_FUNC,
1040 .v.func = via_fixup_power_save,
1041 },
1042};
1043
1044static const struct snd_pci_quirk vt2002p_fixups[] = {
1045 SND_PCI_QUIRK(0x1043, 0x13f7, "Asus B23E", VIA_FIXUP_POWER_SAVE),
1046 SND_PCI_QUIRK(0x1043, 0x1487, "Asus G75", VIA_FIXUP_ASUS_G75),
1047 SND_PCI_QUIRK(0x1043, 0x8532, "Asus X202E", VIA_FIXUP_INTMIC_BOOST),
1048 SND_PCI_QUIRK_VENDOR(0x1558, "Clevo", VIA_FIXUP_POWER_SAVE),
1049 {}
1050};
1051
1052/* NIDs 0x24 and 0x33 on VT1802 have connections to non-existing NID 0x3e
1053 * Replace this with mixer NID 0x1c
1054 */
1055static void fix_vt1802_connections(struct hda_codec *codec)
1056{
1057 static const hda_nid_t conn_24[] = { 0x14, 0x1c };
1058 static const hda_nid_t conn_33[] = { 0x1c };
1059
1060 snd_hda_override_conn_list(codec, 0x24, ARRAY_SIZE(conn_24), conn_24);
1061 snd_hda_override_conn_list(codec, 0x33, ARRAY_SIZE(conn_33), conn_33);
1062}
1063
1064/* patch for vt2002P */
1065static int patch_vt2002P(struct hda_codec *codec)
1066{
1067 struct via_spec *spec;
1068 int err;
1069
1070 /* create a codec specific record */
1071 spec = via_new_spec(codec);
1072 if (spec == NULL)
1073 return -ENOMEM;
1074
1075 spec->gen.mixer_nid = 0x21;
1076 override_mic_boost(codec, 0x2b, 0, 3, 40);
1077 override_mic_boost(codec, 0x29, 0, 3, 40);
1078 if (spec->codec_type == VT1802)
1079 fix_vt1802_connections(codec);
1080 add_secret_dac_path(codec);
1081
1082 snd_hda_pick_fixup(codec, NULL, vt2002p_fixups, via_fixups);
1083 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1084
1085 if (spec->codec_type == VT1802)
1086 err = snd_hda_add_verbs(codec, vt1802_init_verbs);
1087 else
1088 err = snd_hda_add_verbs(codec, vt2002P_init_verbs);
1089 if (err < 0)
1090 goto error;
1091
1092 /* automatic parse from the BIOS config */
1093 err = via_parse_auto_config(codec);
1094 if (err < 0)
1095 goto error;
1096
1097 return 0;
1098
1099 error:
1100 via_free(codec);
1101 return err;
1102}
1103
1104/* for vt1812 */
1105
1106static const struct hda_verb vt1812_init_verbs[] = {
1107 /* Enable Boost Volume backdoor */
1108 {0x1, 0xfb9, 0x24},
1109 /* Enable AOW0 to MW9 */
1110 {0x1, 0xfb8, 0xa8},
1111 { }
1112};
1113
1114/* patch for vt1812 */
1115static int patch_vt1812(struct hda_codec *codec)
1116{
1117 struct via_spec *spec;
1118 int err;
1119
1120 /* create a codec specific record */
1121 spec = via_new_spec(codec);
1122 if (spec == NULL)
1123 return -ENOMEM;
1124
1125 spec->gen.mixer_nid = 0x21;
1126 override_mic_boost(codec, 0x2b, 0, 3, 40);
1127 override_mic_boost(codec, 0x29, 0, 3, 40);
1128 add_secret_dac_path(codec);
1129
1130 err = snd_hda_add_verbs(codec, vt1812_init_verbs);
1131 if (err < 0)
1132 goto error;
1133
1134 /* automatic parse from the BIOS config */
1135 err = via_parse_auto_config(codec);
1136 if (err < 0)
1137 goto error;
1138
1139 return 0;
1140
1141 error:
1142 via_free(codec);
1143 return err;
1144}
1145
1146/* patch for vt3476 */
1147
1148static const struct hda_verb vt3476_init_verbs[] = {
1149 /* Enable DMic 8/16/32K */
1150 {0x1, 0xF7B, 0x30},
1151 /* Enable Boost Volume backdoor */
1152 {0x1, 0xFB9, 0x20},
1153 /* Enable AOW-MW9 path */
1154 {0x1, 0xFB8, 0x10},
1155 { }
1156};
1157
1158static int patch_vt3476(struct hda_codec *codec)
1159{
1160 struct via_spec *spec;
1161 int err;
1162
1163 /* create a codec specific record */
1164 spec = via_new_spec(codec);
1165 if (spec == NULL)
1166 return -ENOMEM;
1167
1168 spec->gen.mixer_nid = 0x3f;
1169 add_secret_dac_path(codec);
1170
1171 err = snd_hda_add_verbs(codec, vt3476_init_verbs);
1172 if (err < 0)
1173 goto error;
1174
1175 /* automatic parse from the BIOS config */
1176 err = via_parse_auto_config(codec);
1177 if (err < 0)
1178 goto error;
1179
1180 return 0;
1181
1182 error:
1183 via_free(codec);
1184 return err;
1185}
1186
1187/*
1188 * patch entries
1189 */
1190static const struct hda_device_id snd_hda_id_via[] = {
1191 HDA_CODEC_ENTRY(0x11061708, "VT1708", patch_vt1708),
1192 HDA_CODEC_ENTRY(0x11061709, "VT1708", patch_vt1708),
1193 HDA_CODEC_ENTRY(0x1106170a, "VT1708", patch_vt1708),
1194 HDA_CODEC_ENTRY(0x1106170b, "VT1708", patch_vt1708),
1195 HDA_CODEC_ENTRY(0x1106e710, "VT1709 10-Ch", patch_vt1709),
1196 HDA_CODEC_ENTRY(0x1106e711, "VT1709 10-Ch", patch_vt1709),
1197 HDA_CODEC_ENTRY(0x1106e712, "VT1709 10-Ch", patch_vt1709),
1198 HDA_CODEC_ENTRY(0x1106e713, "VT1709 10-Ch", patch_vt1709),
1199 HDA_CODEC_ENTRY(0x1106e714, "VT1709 6-Ch", patch_vt1709),
1200 HDA_CODEC_ENTRY(0x1106e715, "VT1709 6-Ch", patch_vt1709),
1201 HDA_CODEC_ENTRY(0x1106e716, "VT1709 6-Ch", patch_vt1709),
1202 HDA_CODEC_ENTRY(0x1106e717, "VT1709 6-Ch", patch_vt1709),
1203 HDA_CODEC_ENTRY(0x1106e720, "VT1708B 8-Ch", patch_vt1708B),
1204 HDA_CODEC_ENTRY(0x1106e721, "VT1708B 8-Ch", patch_vt1708B),
1205 HDA_CODEC_ENTRY(0x1106e722, "VT1708B 8-Ch", patch_vt1708B),
1206 HDA_CODEC_ENTRY(0x1106e723, "VT1708B 8-Ch", patch_vt1708B),
1207 HDA_CODEC_ENTRY(0x1106e724, "VT1708B 4-Ch", patch_vt1708B),
1208 HDA_CODEC_ENTRY(0x1106e725, "VT1708B 4-Ch", patch_vt1708B),
1209 HDA_CODEC_ENTRY(0x1106e726, "VT1708B 4-Ch", patch_vt1708B),
1210 HDA_CODEC_ENTRY(0x1106e727, "VT1708B 4-Ch", patch_vt1708B),
1211 HDA_CODEC_ENTRY(0x11060397, "VT1708S", patch_vt1708S),
1212 HDA_CODEC_ENTRY(0x11061397, "VT1708S", patch_vt1708S),
1213 HDA_CODEC_ENTRY(0x11062397, "VT1708S", patch_vt1708S),
1214 HDA_CODEC_ENTRY(0x11063397, "VT1708S", patch_vt1708S),
1215 HDA_CODEC_ENTRY(0x11064397, "VT1705", patch_vt1708S),
1216 HDA_CODEC_ENTRY(0x11065397, "VT1708S", patch_vt1708S),
1217 HDA_CODEC_ENTRY(0x11066397, "VT1708S", patch_vt1708S),
1218 HDA_CODEC_ENTRY(0x11067397, "VT1708S", patch_vt1708S),
1219 HDA_CODEC_ENTRY(0x11060398, "VT1702", patch_vt1702),
1220 HDA_CODEC_ENTRY(0x11061398, "VT1702", patch_vt1702),
1221 HDA_CODEC_ENTRY(0x11062398, "VT1702", patch_vt1702),
1222 HDA_CODEC_ENTRY(0x11063398, "VT1702", patch_vt1702),
1223 HDA_CODEC_ENTRY(0x11064398, "VT1702", patch_vt1702),
1224 HDA_CODEC_ENTRY(0x11065398, "VT1702", patch_vt1702),
1225 HDA_CODEC_ENTRY(0x11066398, "VT1702", patch_vt1702),
1226 HDA_CODEC_ENTRY(0x11067398, "VT1702", patch_vt1702),
1227 HDA_CODEC_ENTRY(0x11060428, "VT1718S", patch_vt1718S),
1228 HDA_CODEC_ENTRY(0x11064428, "VT1718S", patch_vt1718S),
1229 HDA_CODEC_ENTRY(0x11060441, "VT2020", patch_vt1718S),
1230 HDA_CODEC_ENTRY(0x11064441, "VT1828S", patch_vt1718S),
1231 HDA_CODEC_ENTRY(0x11060433, "VT1716S", patch_vt1716S),
1232 HDA_CODEC_ENTRY(0x1106a721, "VT1716S", patch_vt1716S),
1233 HDA_CODEC_ENTRY(0x11060438, "VT2002P", patch_vt2002P),
1234 HDA_CODEC_ENTRY(0x11064438, "VT2002P", patch_vt2002P),
1235 HDA_CODEC_ENTRY(0x11060448, "VT1812", patch_vt1812),
1236 HDA_CODEC_ENTRY(0x11060440, "VT1818S", patch_vt1708S),
1237 HDA_CODEC_ENTRY(0x11060446, "VT1802", patch_vt2002P),
1238 HDA_CODEC_ENTRY(0x11068446, "VT1802", patch_vt2002P),
1239 HDA_CODEC_ENTRY(0x11064760, "VT1705CF", patch_vt3476),
1240 HDA_CODEC_ENTRY(0x11064761, "VT1708SCE", patch_vt3476),
1241 HDA_CODEC_ENTRY(0x11064762, "VT1808", patch_vt3476),
1242 {} /* terminator */
1243};
1244MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_via);
1245
1246static struct hda_codec_driver via_driver = {
1247 .id = snd_hda_id_via,
1248};
1249
1250MODULE_LICENSE("GPL");
1251MODULE_DESCRIPTION("VIA HD-audio codec");
1252
1253module_hda_codec_driver(via_driver);