1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Universal Interface for Intel High Definition Audio Codec
4 *
5 * HD audio codec driver 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 "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,
56 VT1709_10CH,
57 VT1709_6CH,
58 VT1708B,
59 VT1708B_8CH,
60 VT1708B_4CH,
61 VT1708S,
62 VT1708BCE,
63 VT1702,
64 VT1718S,
65 VT1716S,
66 VT2002P,
67 VT1812,
68 VT1802,
69 VT1705CF,
70 VT1808,
71 VT3476,
72 CODEC_TYPES,
73};
74
75#define VT2002P_COMPATIBLE(spec) \
76 ((spec)->codec_type == VT2002P ||\
77 (spec)->codec_type == VT1812 ||\
78 (spec)->codec_type == VT1802)
79
80struct via_spec {
81 struct hda_gen_spec gen;
82
83 /* HP mode source */
84 unsigned int dmic_enabled;
85 enum VIA_HDA_CODEC codec_type;
86
87 /* analog low-power control */
88 bool alc_mode;
89
90 /* work to check hp jack state */
91 int hp_work_active;
92 int vt1708_jack_detect;
93};
94
95static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
96static void via_playback_pcm_hook(struct hda_pcm_stream *hinfo,
97 struct hda_codec *codec,
98 struct snd_pcm_substream *substream,
99 int action);
100
101static struct via_spec *via_new_spec(struct hda_codec *codec)
102{
103 struct via_spec *spec;
104
105 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
106 if (spec == NULL)
107 return NULL;
108
109 codec->spec = spec;
110 snd_hda_gen_spec_init(spec: &spec->gen);
111 spec->codec_type = get_codec_type(codec);
112 /* VT1708BCE & VT1708S are almost same */
113 if (spec->codec_type == VT1708BCE)
114 spec->codec_type = VT1708S;
115 spec->gen.indep_hp = 1;
116 spec->gen.keep_eapd_on = 1;
117 spec->gen.dac_min_mute = 1;
118 spec->gen.pcm_playback_hook = via_playback_pcm_hook;
119 spec->gen.add_stereo_mix_input = HDA_HINT_STEREO_MIX_AUTO;
120 codec->power_save_node = 1;
121 spec->gen.power_down_unused = 1;
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, nid: 0x1, flags: 0, verb: 0xf81, parm: 1);
191 codec->jackpoll_interval = 0;
192 cancel_delayed_work_sync(dwork: &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(m: 100);
205 snd_hda_codec_write(codec, nid: 0x1, flags: 0, verb: 0xf81, parm: 0);
206 schedule_delayed_work(dwork: &codec->jackpoll_work, delay: 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: &spec->gen, NULL,
269 temp: &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(wcaps: get_wcaps(codec, nid)) == AC_WID_BEEP)
284 return set_beep_amp(spec, nid, idx: 0, dir: 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, nid: codec->core.afg, flags: 0, verb, parm);
360}
361
362static void analog_low_current_mode(struct hda_codec *codec)
363{
364 return __analog_low_current_mode(codec, force: 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_remove(struct hda_codec *codec)
377{
378 vt1708_stop_hp_work(codec);
379 snd_hda_gen_remove(codec);
380}
381
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(msecs: 10);
398 snd_hda_codec_init(codec);
399 snd_hda_regmap_sync(codec);
400 return 0;
401}
402
403static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
404{
405 struct via_spec *spec = codec->spec;
406 analog_low_current_mode(codec);
407 vt1708_update_hp_work(codec);
408 return snd_hda_check_amp_list_power(codec, check: &spec->gen.loopback, nid);
409}
410
411/*
412 */
413
414static const struct hda_verb vt1708_init_verbs[] = {
415 /* power down jack detect function */
416 {0x1, 0xf81, 0x1},
417 { }
418};
419static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
420{
421 unsigned int def_conf;
422 unsigned char seqassoc;
423
424 def_conf = snd_hda_codec_get_pincfg(codec, nid);
425 seqassoc = (unsigned char) get_defcfg_association(def_conf);
426 seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
427 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
428 && (seqassoc == 0xf0 || seqassoc == 0xff)) {
429 def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
430 snd_hda_codec_set_pincfg(codec, nid, cfg: def_conf);
431 }
432}
433
434static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
435 struct snd_ctl_elem_value *ucontrol)
436{
437 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
438 struct via_spec *spec = codec->spec;
439
440 if (spec->codec_type != VT1708)
441 return 0;
442 ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
443 return 0;
444}
445
446static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
447 struct snd_ctl_elem_value *ucontrol)
448{
449 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
450 struct via_spec *spec = codec->spec;
451 int val;
452
453 if (spec->codec_type != VT1708)
454 return 0;
455 val = !!ucontrol->value.integer.value[0];
456 if (spec->vt1708_jack_detect == val)
457 return 0;
458 spec->vt1708_jack_detect = val;
459 vt1708_update_hp_work(codec);
460 return 1;
461}
462
463static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
464 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
465 .name = "Jack Detect",
466 .count = 1,
467 .info = snd_ctl_boolean_mono_info,
468 .get = vt1708_jack_detect_get,
469 .put = vt1708_jack_detect_put,
470};
471
472static const struct badness_table via_main_out_badness = {
473 .no_primary_dac = 0x10000,
474 .no_dac = 0x4000,
475 .shared_primary = 0x10000,
476 .shared_surr = 0x20,
477 .shared_clfe = 0x20,
478 .shared_surr_main = 0x20,
479};
480static const struct badness_table via_extra_out_badness = {
481 .no_primary_dac = 0x4000,
482 .no_dac = 0x4000,
483 .shared_primary = 0x12,
484 .shared_surr = 0x20,
485 .shared_clfe = 0x20,
486 .shared_surr_main = 0x10,
487};
488
489static int via_parse_auto_config(struct hda_codec *codec)
490{
491 struct via_spec *spec = codec->spec;
492 int err;
493
494 spec->gen.main_out_badness = &via_main_out_badness;
495 spec->gen.extra_out_badness = &via_extra_out_badness;
496
497 err = snd_hda_parse_pin_defcfg(codec, cfg: &spec->gen.autocfg, NULL, cond_flags: 0);
498 if (err < 0)
499 return err;
500
501 err = auto_parse_beep(codec);
502 if (err < 0)
503 return err;
504
505 err = snd_hda_gen_parse_auto_config(codec, cfg: &spec->gen.autocfg);
506 if (err < 0)
507 return err;
508
509 if (!snd_hda_gen_add_kctl(spec: &spec->gen, NULL, temp: &via_pin_power_ctl_enum))
510 return -ENOMEM;
511
512 /* disable widget PM at start for compatibility */
513 codec->power_save_node = 0;
514 spec->gen.power_down_unused = 0;
515 return 0;
516}
517
518static int via_init(struct hda_codec *codec)
519{
520 /* init power states */
521 __analog_low_current_mode(codec, force: true);
522
523 snd_hda_gen_init(codec);
524
525 vt1708_update_hp_work(codec);
526
527 return 0;
528}
529
530static int via_build_controls(struct hda_codec *codec)
531{
532 /* In order not to create "Phantom Jack" controls,
533 temporary enable jackpoll */
534 int err;
535 int old_interval = codec->jackpoll_interval;
536 if (old_interval)
537 codec->jackpoll_interval = msecs_to_jiffies(m: 100);
538 err = snd_hda_gen_build_controls(codec);
539 if (old_interval)
540 codec->jackpoll_interval = old_interval;
541 return err;
542}
543
544static int via_build_pcms(struct hda_codec *codec)
545{
546 struct via_spec *spec = codec->spec;
547 int i, err;
548
549 err = snd_hda_gen_build_pcms(codec);
550 if (err < 0 || codec->core.vendor_id != 0x11061708)
551 return err;
552
553 /* We got noisy outputs on the right channel on VT1708 when
554 * 24bit samples are used. Until any workaround is found,
555 * disable the 24bit format, so far.
556 */
557 for (i = 0; i < ARRAY_SIZE(spec->gen.pcm_rec); i++) {
558 struct hda_pcm *info = spec->gen.pcm_rec[i];
559 if (!info)
560 continue;
561 if (!info->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams ||
562 info->pcm_type != HDA_PCM_TYPE_AUDIO)
563 continue;
564 info->stream[SNDRV_PCM_STREAM_PLAYBACK].formats =
565 SNDRV_PCM_FMTBIT_S16_LE;
566 }
567
568 return 0;
569}
570
571static int probe_vt1708(struct hda_codec *codec)
572{
573 struct via_spec *spec = codec->spec;
574 int err;
575
576 spec->gen.mixer_nid = 0x17;
577
578 /* set jackpoll_interval while parsing the codec */
579 codec->jackpoll_interval = msecs_to_jiffies(m: 100);
580 spec->vt1708_jack_detect = 1;
581
582 /* don't support the input jack switching due to lack of unsol event */
583 /* (it may work with polling, though, but it needs testing) */
584 spec->gen.suppress_auto_mic = 1;
585 /* Some machines show the broken speaker mute */
586 spec->gen.auto_mute_via_amp = 1;
587
588 /* Add HP and CD pin config connect bit re-config action */
589 vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
590 vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
591
592 err = snd_hda_add_verbs(codec, list: vt1708_init_verbs);
593 if (err < 0)
594 return err;
595
596 /* automatic parse from the BIOS config */
597 err = via_parse_auto_config(codec);
598 if (err < 0)
599 return err;
600
601 /* add jack detect on/off control */
602 if (!snd_hda_gen_add_kctl(spec: &spec->gen, NULL, temp: &vt1708_jack_detect_ctl))
603 return -ENOMEM;
604
605 /* clear jackpoll_interval again; it's set dynamically */
606 codec->jackpoll_interval = 0;
607
608 return 0;
609}
610
611static int probe_vt1709(struct hda_codec *codec)
612{
613 struct via_spec *spec = codec->spec;
614
615 spec->gen.mixer_nid = 0x18;
616
617 return via_parse_auto_config(codec);
618}
619
620static int probe_vt1708S(struct hda_codec *codec);
621static int probe_vt1708B(struct hda_codec *codec)
622{
623 struct via_spec *spec = codec->spec;
624
625 if (get_codec_type(codec) == VT1708BCE)
626 return probe_vt1708S(codec);
627
628 spec->gen.mixer_nid = 0x16;
629
630 /* automatic parse from the BIOS config */
631 return via_parse_auto_config(codec);
632}
633
634/* Support for VT1708S */
635static const struct hda_verb vt1708S_init_verbs[] = {
636 /* Enable Mic Boost Volume backdoor */
637 {0x1, 0xf98, 0x1},
638 /* don't bybass mixer */
639 {0x1, 0xf88, 0xc0},
640 { }
641};
642
643static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
644 int offset, int num_steps, int step_size)
645{
646 snd_hda_override_wcaps(codec, nid: pin,
647 val: get_wcaps(codec, nid: pin) | AC_WCAP_IN_AMP);
648 snd_hda_override_amp_caps(codec, nid: pin, dir: HDA_INPUT,
649 caps: (offset << AC_AMPCAP_OFFSET_SHIFT) |
650 (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
651 (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
652 (0 << AC_AMPCAP_MUTE_SHIFT));
653}
654
655static int probe_vt1708S(struct hda_codec *codec)
656{
657 struct via_spec *spec = codec->spec;
658 int err;
659
660 spec->gen.mixer_nid = 0x16;
661 override_mic_boost(codec, pin: 0x1a, offset: 0, num_steps: 3, step_size: 40);
662 override_mic_boost(codec, pin: 0x1e, offset: 0, num_steps: 3, step_size: 40);
663
664 /* correct names for VT1708BCE */
665 if (get_codec_type(codec) == VT1708BCE)
666 snd_hda_codec_set_name(codec, name: "VT1708BCE");
667 /* correct names for VT1705 */
668 if (codec->core.vendor_id == 0x11064397)
669 snd_hda_codec_set_name(codec, name: "VT1705");
670
671 err = snd_hda_add_verbs(codec, list: vt1708S_init_verbs);
672 if (err < 0)
673 return err;
674
675 return via_parse_auto_config(codec);
676}
677
678/* Support for VT1702 */
679
680static const struct hda_verb vt1702_init_verbs[] = {
681 /* mixer enable */
682 {0x1, 0xF88, 0x3},
683 /* GPIO 0~2 */
684 {0x1, 0xF82, 0x3F},
685 { }
686};
687
688static int probe_vt1702(struct hda_codec *codec)
689{
690 struct via_spec *spec = codec->spec;
691 int err;
692
693 spec->gen.mixer_nid = 0x1a;
694
695 /* limit AA path volume to 0 dB */
696 snd_hda_override_amp_caps(codec, nid: 0x1A, dir: HDA_INPUT,
697 caps: (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
698 (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
699 (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
700 (1 << AC_AMPCAP_MUTE_SHIFT));
701
702 err = snd_hda_add_verbs(codec, list: vt1702_init_verbs);
703 if (err < 0)
704 return err;
705
706 /* automatic parse from the BIOS config */
707 return via_parse_auto_config(codec);
708}
709
710/* Support for VT1718S */
711
712static const struct hda_verb vt1718S_init_verbs[] = {
713 /* Enable MW0 adjust Gain 5 */
714 {0x1, 0xfb2, 0x10},
715 /* Enable Boost Volume backdoor */
716 {0x1, 0xf88, 0x8},
717
718 { }
719};
720
721/* Add a connection to the primary DAC from AA-mixer for some codecs
722 * This isn't listed from the raw info, but the chip has a secret connection.
723 */
724static int add_secret_dac_path(struct hda_codec *codec)
725{
726 struct via_spec *spec = codec->spec;
727 int i, nums;
728 hda_nid_t conn[8];
729 hda_nid_t nid;
730
731 if (!spec->gen.mixer_nid)
732 return 0;
733 nums = snd_hda_get_connections(codec, nid: spec->gen.mixer_nid, conn_list: conn,
734 ARRAY_SIZE(conn) - 1);
735 if (nums < 0)
736 return nums;
737
738 for (i = 0; i < nums; i++) {
739 if (get_wcaps_type(wcaps: get_wcaps(codec, nid: conn[i])) == AC_WID_AUD_OUT)
740 return 0;
741 }
742
743 /* find the primary DAC and add to the connection list */
744 for_each_hda_codec_node(nid, codec) {
745 unsigned int caps = get_wcaps(codec, nid);
746 if (get_wcaps_type(wcaps: caps) == AC_WID_AUD_OUT &&
747 !(caps & AC_WCAP_DIGITAL)) {
748 conn[nums++] = nid;
749 return snd_hda_override_conn_list(codec,
750 nid: spec->gen.mixer_nid,
751 nums, list: conn);
752 }
753 }
754 return 0;
755}
756
757
758static int probe_vt1718S(struct hda_codec *codec)
759{
760 struct via_spec *spec = codec->spec;
761 int err;
762
763 spec->gen.mixer_nid = 0x21;
764 override_mic_boost(codec, pin: 0x2b, offset: 0, num_steps: 3, step_size: 40);
765 override_mic_boost(codec, pin: 0x29, offset: 0, num_steps: 3, step_size: 40);
766 add_secret_dac_path(codec);
767
768 err = snd_hda_add_verbs(codec, list: vt1718S_init_verbs);
769 if (err < 0)
770 return err;
771
772 /* automatic parse from the BIOS config */
773 return via_parse_auto_config(codec);
774}
775
776/* Support for VT1716S */
777
778static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
779 struct snd_ctl_elem_info *uinfo)
780{
781 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
782 uinfo->count = 1;
783 uinfo->value.integer.min = 0;
784 uinfo->value.integer.max = 1;
785 return 0;
786}
787
788static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
789 struct snd_ctl_elem_value *ucontrol)
790{
791 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
792 int index = 0;
793
794 index = snd_hda_codec_read(codec, nid: 0x26, flags: 0,
795 AC_VERB_GET_CONNECT_SEL, parm: 0);
796 if (index != -1)
797 *ucontrol->value.integer.value = index;
798
799 return 0;
800}
801
802static int vt1716s_dmic_put(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 int index = *ucontrol->value.integer.value;
808
809 snd_hda_codec_write(codec, nid: 0x26, flags: 0,
810 AC_VERB_SET_CONNECT_SEL, parm: index);
811 spec->dmic_enabled = index;
812 return 1;
813}
814
815static const struct snd_kcontrol_new vt1716s_dmic_mixer_vol =
816 HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT);
817static const struct snd_kcontrol_new vt1716s_dmic_mixer_sw = {
818 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
819 .name = "Digital Mic Capture Switch",
820 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
821 .count = 1,
822 .info = vt1716s_dmic_info,
823 .get = vt1716s_dmic_get,
824 .put = vt1716s_dmic_put,
825};
826
827
828/* mono-out mixer elements */
829static const struct snd_kcontrol_new vt1716S_mono_out_mixer =
830 HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT);
831
832static const struct hda_verb vt1716S_init_verbs[] = {
833 /* Enable Boost Volume backdoor */
834 {0x1, 0xf8a, 0x80},
835 /* don't bybass mixer */
836 {0x1, 0xf88, 0xc0},
837 /* Enable mono output */
838 {0x1, 0xf90, 0x08},
839 { }
840};
841
842static int probe_vt1716S(struct hda_codec *codec)
843{
844 struct via_spec *spec = codec->spec;
845 int err;
846
847 spec->gen.mixer_nid = 0x16;
848 override_mic_boost(codec, pin: 0x1a, offset: 0, num_steps: 3, step_size: 40);
849 override_mic_boost(codec, pin: 0x1e, offset: 0, num_steps: 3, step_size: 40);
850
851 err = snd_hda_add_verbs(codec, list: vt1716S_init_verbs);
852 if (err < 0)
853 return err;
854
855 /* automatic parse from the BIOS config */
856 err = via_parse_auto_config(codec);
857 if (err < 0)
858 return err;
859
860 if (!snd_hda_gen_add_kctl(spec: &spec->gen, NULL, temp: &vt1716s_dmic_mixer_vol) ||
861 !snd_hda_gen_add_kctl(spec: &spec->gen, NULL, temp: &vt1716s_dmic_mixer_sw) ||
862 !snd_hda_gen_add_kctl(spec: &spec->gen, NULL, temp: &vt1716S_mono_out_mixer))
863 return -ENOMEM;
864
865 return 0;
866}
867
868/* for vt2002P */
869
870static const struct hda_verb vt2002P_init_verbs[] = {
871 /* Class-D speaker related verbs */
872 {0x1, 0xfe0, 0x4},
873 {0x1, 0xfe9, 0x80},
874 {0x1, 0xfe2, 0x22},
875 /* Enable Boost Volume backdoor */
876 {0x1, 0xfb9, 0x24},
877 /* Enable AOW0 to MW9 */
878 {0x1, 0xfb8, 0x88},
879 { }
880};
881
882static const struct hda_verb vt1802_init_verbs[] = {
883 /* Enable Boost Volume backdoor */
884 {0x1, 0xfb9, 0x24},
885 /* Enable AOW0 to MW9 */
886 {0x1, 0xfb8, 0x88},
887 { }
888};
889
890/*
891 * pin fix-up
892 */
893enum {
894 VIA_FIXUP_INTMIC_BOOST,
895 VIA_FIXUP_ASUS_G75,
896 VIA_FIXUP_POWER_SAVE,
897};
898
899static void via_fixup_intmic_boost(struct hda_codec *codec,
900 const struct hda_fixup *fix, int action)
901{
902 if (action == HDA_FIXUP_ACT_PRE_PROBE)
903 override_mic_boost(codec, pin: 0x30, offset: 0, num_steps: 2, step_size: 40);
904}
905
906static void via_fixup_power_save(struct hda_codec *codec,
907 const struct hda_fixup *fix, int action)
908{
909 if (action == HDA_FIXUP_ACT_PRE_PROBE)
910 codec->power_save_node = 0;
911}
912
913static const struct hda_fixup via_fixups[] = {
914 [VIA_FIXUP_INTMIC_BOOST] = {
915 .type = HDA_FIXUP_FUNC,
916 .v.func = via_fixup_intmic_boost,
917 },
918 [VIA_FIXUP_ASUS_G75] = {
919 .type = HDA_FIXUP_PINS,
920 .v.pins = (const struct hda_pintbl[]) {
921 /* set 0x24 and 0x33 as speakers */
922 { 0x24, 0x991301f0 },
923 { 0x33, 0x991301f1 }, /* subwoofer */
924 { }
925 }
926 },
927 [VIA_FIXUP_POWER_SAVE] = {
928 .type = HDA_FIXUP_FUNC,
929 .v.func = via_fixup_power_save,
930 },
931};
932
933static const struct hda_quirk vt2002p_fixups[] = {
934 SND_PCI_QUIRK(0x1043, 0x13f7, "Asus B23E", VIA_FIXUP_POWER_SAVE),
935 SND_PCI_QUIRK(0x1043, 0x1487, "Asus G75", VIA_FIXUP_ASUS_G75),
936 SND_PCI_QUIRK(0x1043, 0x8532, "Asus X202E", VIA_FIXUP_INTMIC_BOOST),
937 SND_PCI_QUIRK_VENDOR(0x1558, "Clevo", VIA_FIXUP_POWER_SAVE),
938 {}
939};
940
941/* NIDs 0x24 and 0x33 on VT1802 have connections to non-existing NID 0x3e
942 * Replace this with mixer NID 0x1c
943 */
944static void fix_vt1802_connections(struct hda_codec *codec)
945{
946 static const hda_nid_t conn_24[] = { 0x14, 0x1c };
947 static const hda_nid_t conn_33[] = { 0x1c };
948
949 snd_hda_override_conn_list(codec, nid: 0x24, ARRAY_SIZE(conn_24), list: conn_24);
950 snd_hda_override_conn_list(codec, nid: 0x33, ARRAY_SIZE(conn_33), list: conn_33);
951}
952
953/* Support for vt2002P */
954static int probe_vt2002P(struct hda_codec *codec)
955{
956 struct via_spec *spec = codec->spec;
957 int err;
958
959 spec->gen.mixer_nid = 0x21;
960 override_mic_boost(codec, pin: 0x2b, offset: 0, num_steps: 3, step_size: 40);
961 override_mic_boost(codec, pin: 0x29, offset: 0, num_steps: 3, step_size: 40);
962 if (spec->codec_type == VT1802)
963 fix_vt1802_connections(codec);
964 add_secret_dac_path(codec);
965
966 snd_hda_pick_fixup(codec, NULL, quirk: vt2002p_fixups, fixlist: via_fixups);
967 snd_hda_apply_fixup(codec, action: HDA_FIXUP_ACT_PRE_PROBE);
968
969 if (spec->codec_type == VT1802)
970 err = snd_hda_add_verbs(codec, list: vt1802_init_verbs);
971 else
972 err = snd_hda_add_verbs(codec, list: vt2002P_init_verbs);
973 if (err < 0)
974 return err;
975
976 /* automatic parse from the BIOS config */
977 return via_parse_auto_config(codec);
978}
979
980/* for vt1812 */
981
982static const struct hda_verb vt1812_init_verbs[] = {
983 /* Enable Boost Volume backdoor */
984 {0x1, 0xfb9, 0x24},
985 /* Enable AOW0 to MW9 */
986 {0x1, 0xfb8, 0xa8},
987 { }
988};
989
990static int probe_vt1812(struct hda_codec *codec)
991{
992 struct via_spec *spec = codec->spec;
993 int err;
994
995 spec->gen.mixer_nid = 0x21;
996 override_mic_boost(codec, pin: 0x2b, offset: 0, num_steps: 3, step_size: 40);
997 override_mic_boost(codec, pin: 0x29, offset: 0, num_steps: 3, step_size: 40);
998 add_secret_dac_path(codec);
999
1000 err = snd_hda_add_verbs(codec, list: vt1812_init_verbs);
1001 if (err < 0)
1002 return err;
1003
1004 /* automatic parse from the BIOS config */
1005 return via_parse_auto_config(codec);
1006}
1007
1008/* Support for vt3476 */
1009
1010static const struct hda_verb vt3476_init_verbs[] = {
1011 /* Enable DMic 8/16/32K */
1012 {0x1, 0xF7B, 0x30},
1013 /* Enable Boost Volume backdoor */
1014 {0x1, 0xFB9, 0x20},
1015 /* Enable AOW-MW9 path */
1016 {0x1, 0xFB8, 0x10},
1017 { }
1018};
1019
1020static int probe_vt3476(struct hda_codec *codec)
1021{
1022 struct via_spec *spec = codec->spec;
1023 int err;
1024
1025 spec->gen.mixer_nid = 0x3f;
1026 add_secret_dac_path(codec);
1027
1028 err = snd_hda_add_verbs(codec, list: vt3476_init_verbs);
1029 if (err < 0)
1030 return err;
1031
1032 /* automatic parse from the BIOS config */
1033 return via_parse_auto_config(codec);
1034
1035}
1036
1037/*
1038 * common driver probe
1039 */
1040static int via_probe(struct hda_codec *codec, const struct hda_device_id *id)
1041{
1042 struct via_spec *spec;
1043 int err;
1044
1045 /* create a codec specific record */
1046 spec = via_new_spec(codec);
1047 if (!spec)
1048 return -ENOMEM;
1049
1050 switch (id->driver_data) {
1051 case VT1708:
1052 err = probe_vt1708(codec);
1053 break;
1054 case VT1709:
1055 err = probe_vt1709(codec);
1056 break;
1057 case VT1708B:
1058 err = probe_vt1708B(codec);
1059 break;
1060 case VT1708S:
1061 err = probe_vt1708S(codec);
1062 break;
1063 case VT1702:
1064 err = probe_vt1702(codec);
1065 break;
1066 case VT1718S:
1067 err = probe_vt1718S(codec);
1068 break;
1069 case VT1716S:
1070 err = probe_vt1716S(codec);
1071 break;
1072 case VT2002P:
1073 err = probe_vt2002P(codec);
1074 break;
1075 case VT1812:
1076 err = probe_vt1812(codec);
1077 break;
1078 case VT3476:
1079 err = probe_vt3476(codec);
1080 break;
1081 default:
1082 err = -EINVAL;
1083 break;
1084 }
1085
1086 if (err < 0) {
1087 via_remove(codec);
1088 return err;
1089 }
1090
1091 return 0;
1092}
1093
1094static const struct hda_codec_ops via_codec_ops = {
1095 .probe = via_probe,
1096 .remove = via_remove,
1097 .build_controls = via_build_controls,
1098 .build_pcms = via_build_pcms,
1099 .init = via_init,
1100 .unsol_event = snd_hda_jack_unsol_event,
1101 .suspend = via_suspend,
1102 .resume = via_resume,
1103 .check_power_status = via_check_power_status,
1104 .stream_pm = snd_hda_gen_stream_pm,
1105};
1106
1107/*
1108 * driver entries
1109 */
1110static const struct hda_device_id snd_hda_id_via[] = {
1111 HDA_CODEC_ID_MODEL(0x11061708, "VT1708", VT1708),
1112 HDA_CODEC_ID_MODEL(0x11061709, "VT1708", VT1708),
1113 HDA_CODEC_ID_MODEL(0x1106170a, "VT1708", VT1708),
1114 HDA_CODEC_ID_MODEL(0x1106170b, "VT1708", VT1708),
1115 HDA_CODEC_ID_MODEL(0x1106e710, "VT1709 10-Ch", VT1709),
1116 HDA_CODEC_ID_MODEL(0x1106e711, "VT1709 10-Ch", VT1709),
1117 HDA_CODEC_ID_MODEL(0x1106e712, "VT1709 10-Ch", VT1709),
1118 HDA_CODEC_ID_MODEL(0x1106e713, "VT1709 10-Ch", VT1709),
1119 HDA_CODEC_ID_MODEL(0x1106e714, "VT1709 6-Ch", VT1709),
1120 HDA_CODEC_ID_MODEL(0x1106e715, "VT1709 6-Ch", VT1709),
1121 HDA_CODEC_ID_MODEL(0x1106e716, "VT1709 6-Ch", VT1709),
1122 HDA_CODEC_ID_MODEL(0x1106e717, "VT1709 6-Ch", VT1709),
1123 HDA_CODEC_ID_MODEL(0x1106e720, "VT1708B 8-Ch", VT1708B),
1124 HDA_CODEC_ID_MODEL(0x1106e721, "VT1708B 8-Ch", VT1708B),
1125 HDA_CODEC_ID_MODEL(0x1106e722, "VT1708B 8-Ch", VT1708B),
1126 HDA_CODEC_ID_MODEL(0x1106e723, "VT1708B 8-Ch", VT1708B),
1127 HDA_CODEC_ID_MODEL(0x1106e724, "VT1708B 4-Ch", VT1708B),
1128 HDA_CODEC_ID_MODEL(0x1106e725, "VT1708B 4-Ch", VT1708B),
1129 HDA_CODEC_ID_MODEL(0x1106e726, "VT1708B 4-Ch", VT1708B),
1130 HDA_CODEC_ID_MODEL(0x1106e727, "VT1708B 4-Ch", VT1708B),
1131 HDA_CODEC_ID_MODEL(0x11060397, "VT1708S", VT1708S),
1132 HDA_CODEC_ID_MODEL(0x11061397, "VT1708S", VT1708S),
1133 HDA_CODEC_ID_MODEL(0x11062397, "VT1708S", VT1708S),
1134 HDA_CODEC_ID_MODEL(0x11063397, "VT1708S", VT1708S),
1135 HDA_CODEC_ID_MODEL(0x11064397, "VT1705", VT1708S),
1136 HDA_CODEC_ID_MODEL(0x11065397, "VT1708S", VT1708S),
1137 HDA_CODEC_ID_MODEL(0x11066397, "VT1708S", VT1708S),
1138 HDA_CODEC_ID_MODEL(0x11067397, "VT1708S", VT1708S),
1139 HDA_CODEC_ID_MODEL(0x11060398, "VT1702", VT1702),
1140 HDA_CODEC_ID_MODEL(0x11061398, "VT1702", VT1702),
1141 HDA_CODEC_ID_MODEL(0x11062398, "VT1702", VT1702),
1142 HDA_CODEC_ID_MODEL(0x11063398, "VT1702", VT1702),
1143 HDA_CODEC_ID_MODEL(0x11064398, "VT1702", VT1702),
1144 HDA_CODEC_ID_MODEL(0x11065398, "VT1702", VT1702),
1145 HDA_CODEC_ID_MODEL(0x11066398, "VT1702", VT1702),
1146 HDA_CODEC_ID_MODEL(0x11067398, "VT1702", VT1702),
1147 HDA_CODEC_ID_MODEL(0x11060428, "VT1718S", VT1718S),
1148 HDA_CODEC_ID_MODEL(0x11064428, "VT1718S", VT1718S),
1149 HDA_CODEC_ID_MODEL(0x11060441, "VT2020", VT1718S),
1150 HDA_CODEC_ID_MODEL(0x11064441, "VT1828S", VT1718S),
1151 HDA_CODEC_ID_MODEL(0x11060433, "VT1716S", VT1716S),
1152 HDA_CODEC_ID_MODEL(0x1106a721, "VT1716S", VT1716S),
1153 HDA_CODEC_ID_MODEL(0x11060438, "VT2002P", VT2002P),
1154 HDA_CODEC_ID_MODEL(0x11064438, "VT2002P", VT2002P),
1155 HDA_CODEC_ID_MODEL(0x11060448, "VT1812", VT1812),
1156 HDA_CODEC_ID_MODEL(0x11060440, "VT1818S", VT1708S),
1157 HDA_CODEC_ID_MODEL(0x11060446, "VT1802", VT2002P),
1158 HDA_CODEC_ID_MODEL(0x11068446, "VT1802", VT2002P),
1159 HDA_CODEC_ID_MODEL(0x11064760, "VT1705CF", VT3476),
1160 HDA_CODEC_ID_MODEL(0x11064761, "VT1708SCE", VT3476),
1161 HDA_CODEC_ID_MODEL(0x11064762, "VT1808", VT3476),
1162 {} /* terminator */
1163};
1164MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_via);
1165
1166static struct hda_codec_driver via_driver = {
1167 .id = snd_hda_id_via,
1168 .ops = &via_codec_ops,
1169};
1170
1171MODULE_LICENSE("GPL");
1172MODULE_DESCRIPTION("VIA HD-audio codec");
1173
1174module_hda_codec_driver(via_driver);
1175

source code of linux/sound/hda/codecs/via.c