aboutsummaryrefslogtreecommitdiffstats
path: root/sound/pci/hda/patch_via.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/pci/hda/patch_via.c')
-rw-r--r--sound/pci/hda/patch_via.c2630
1 files changed, 220 insertions, 2410 deletions
diff --git a/sound/pci/hda/patch_via.c b/sound/pci/hda/patch_via.c
index 09bb64996d72..c35338a8771d 100644
--- a/sound/pci/hda/patch_via.c
+++ b/sound/pci/hda/patch_via.c
@@ -56,6 +56,7 @@
56#include "hda_local.h" 56#include "hda_local.h"
57#include "hda_auto_parser.h" 57#include "hda_auto_parser.h"
58#include "hda_jack.h" 58#include "hda_jack.h"
59#include "hda_generic.h"
59 60
60/* Pin Widget NID */ 61/* Pin Widget NID */
61#define VT1708_HP_PIN_NID 0x20 62#define VT1708_HP_PIN_NID 0x20
@@ -86,39 +87,6 @@ enum VIA_HDA_CODEC {
86 (spec)->codec_type == VT1812 ||\ 87 (spec)->codec_type == VT1812 ||\
87 (spec)->codec_type == VT1802) 88 (spec)->codec_type == VT1802)
88 89
89#define MAX_NID_PATH_DEPTH 5
90
91/* output-path: DAC -> ... -> pin
92 * idx[] contains the source index number of the next widget;
93 * e.g. idx[0] is the index of the DAC selected by path[1] widget
94 * multi[] indicates whether it's a selector widget with multi-connectors
95 * (i.e. the connection selection is mandatory)
96 * vol_ctl and mute_ctl contains the NIDs for the assigned mixers
97 */
98struct nid_path {
99 int depth;
100 hda_nid_t path[MAX_NID_PATH_DEPTH];
101 unsigned char idx[MAX_NID_PATH_DEPTH];
102 unsigned char multi[MAX_NID_PATH_DEPTH];
103 unsigned int vol_ctl;
104 unsigned int mute_ctl;
105};
106
107/* input-path */
108struct via_input {
109 hda_nid_t pin; /* input-pin or aa-mix */
110 int adc_idx; /* ADC index to be used */
111 int mux_idx; /* MUX index (if any) */
112 const char *label; /* input-source label */
113};
114
115#define VIA_MAX_ADCS 3
116
117enum {
118 STREAM_MULTI_OUT = (1 << 0),
119 STREAM_INDEP_HP = (1 << 1),
120};
121
122struct via_spec { 90struct via_spec {
123 struct hda_gen_spec gen; 91 struct hda_gen_spec gen;
124 92
@@ -129,77 +97,7 @@ struct via_spec {
129 const struct hda_verb *init_verbs[5]; 97 const struct hda_verb *init_verbs[5];
130 unsigned int num_iverbs; 98 unsigned int num_iverbs;
131 99
132 char stream_name_analog[32];
133 char stream_name_hp[32];
134 const struct hda_pcm_stream *stream_analog_playback;
135 const struct hda_pcm_stream *stream_analog_capture;
136
137 char stream_name_digital[32];
138 const struct hda_pcm_stream *stream_digital_playback;
139 const struct hda_pcm_stream *stream_digital_capture;
140
141 /* playback */
142 struct hda_multi_out multiout;
143 hda_nid_t slave_dig_outs[2];
144 hda_nid_t hp_dac_nid;
145 hda_nid_t speaker_dac_nid;
146 int hp_indep_shared; /* indep HP-DAC is shared with side ch */
147 int opened_streams; /* STREAM_* bits */
148 int active_streams; /* STREAM_* bits */
149 int aamix_mode; /* loopback is enabled for output-path? */
150
151 /* Output-paths:
152 * There are different output-paths depending on the setup.
153 * out_path, hp_path and speaker_path are primary paths. If both
154 * direct DAC and aa-loopback routes are available, these contain
155 * the former paths. Meanwhile *_mix_path contain the paths with
156 * loopback mixer. (Since the loopback is only for front channel,
157 * no out_mix_path for surround channels.)
158 * The HP output has another path, hp_indep_path, which is used in
159 * the independent-HP mode.
160 */
161 struct nid_path out_path[HDA_SIDE + 1];
162 struct nid_path out_mix_path;
163 struct nid_path hp_path;
164 struct nid_path hp_mix_path;
165 struct nid_path hp_indep_path;
166 struct nid_path speaker_path;
167 struct nid_path speaker_mix_path;
168
169 /* capture */
170 unsigned int num_adc_nids;
171 hda_nid_t adc_nids[VIA_MAX_ADCS];
172 hda_nid_t mux_nids[VIA_MAX_ADCS];
173 hda_nid_t aa_mix_nid;
174 hda_nid_t dig_in_nid;
175
176 /* capture source */
177 bool dyn_adc_switch;
178 int num_inputs;
179 struct via_input inputs[AUTO_CFG_MAX_INS + 1];
180 unsigned int cur_mux[VIA_MAX_ADCS];
181
182 /* dynamic DAC switching */
183 unsigned int cur_dac_stream_tag;
184 unsigned int cur_dac_format;
185 unsigned int cur_hp_stream_tag;
186 unsigned int cur_hp_format;
187
188 /* dynamic ADC switching */
189 hda_nid_t cur_adc;
190 unsigned int cur_adc_stream_tag;
191 unsigned int cur_adc_format;
192
193 /* PCM information */
194 struct hda_pcm pcm_rec[3];
195
196 /* dynamic controls, init_verbs and input_mux */
197 struct auto_pin_cfg autocfg;
198 struct snd_array kctls;
199 hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
200
201 /* HP mode source */ 100 /* HP mode source */
202 unsigned int hp_independent_mode;
203 unsigned int dmic_enabled; 101 unsigned int dmic_enabled;
204 unsigned int no_pin_power_ctl; 102 unsigned int no_pin_power_ctl;
205 enum VIA_HDA_CODEC codec_type; 103 enum VIA_HDA_CODEC codec_type;
@@ -207,36 +105,22 @@ struct via_spec {
207 /* analog low-power control */ 105 /* analog low-power control */
208 bool alc_mode; 106 bool alc_mode;
209 107
210 /* smart51 setup */
211 unsigned int smart51_nums;
212 hda_nid_t smart51_pins[2];
213 int smart51_idxs[2];
214 const char *smart51_labels[2];
215 unsigned int smart51_enabled;
216
217 /* work to check hp jack state */ 108 /* work to check hp jack state */
218 struct hda_codec *codec;
219 struct delayed_work vt1708_hp_work;
220 int hp_work_active; 109 int hp_work_active;
221 int vt1708_jack_detect; 110 int vt1708_jack_detect;
222 int vt1708_hp_present;
223 111
224 void (*set_widgets_power_state)(struct hda_codec *codec); 112 void (*set_widgets_power_state)(struct hda_codec *codec);
225 unsigned int dac_stream_tag[4]; 113 unsigned int dac_stream_tag[4];
226
227 struct hda_loopback_check loopback;
228 int num_loopbacks;
229 struct hda_amp_list loopback_list[8];
230
231 /* bind capture-volume */
232 struct hda_bind_ctls *bind_cap_vol;
233 struct hda_bind_ctls *bind_cap_sw;
234
235 struct mutex config_mutex;
236}; 114};
237 115
238static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec); 116static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
239static struct via_spec * via_new_spec(struct hda_codec *codec) 117static void via_playback_pcm_hook(struct hda_pcm_stream *hinfo,
118 struct hda_codec *codec,
119 struct snd_pcm_substream *substream,
120 int action);
121static void via_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *tbl);
122
123static struct via_spec *via_new_spec(struct hda_codec *codec)
240{ 124{
241 struct via_spec *spec; 125 struct via_spec *spec;
242 126
@@ -244,15 +128,15 @@ static struct via_spec * via_new_spec(struct hda_codec *codec)
244 if (spec == NULL) 128 if (spec == NULL)
245 return NULL; 129 return NULL;
246 130
247 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
248 mutex_init(&spec->config_mutex);
249 codec->spec = spec; 131 codec->spec = spec;
250 spec->codec = codec; 132 snd_hda_gen_spec_init(&spec->gen);
251 spec->codec_type = get_codec_type(codec); 133 spec->codec_type = get_codec_type(codec);
252 /* VT1708BCE & VT1708S are almost same */ 134 /* VT1708BCE & VT1708S are almost same */
253 if (spec->codec_type == VT1708BCE) 135 if (spec->codec_type == VT1708BCE)
254 spec->codec_type = VT1708S; 136 spec->codec_type = VT1708S;
255 snd_hda_gen_init(&spec->gen); 137 spec->no_pin_power_ctl = 1;
138 spec->gen.indep_hp = 1;
139 spec->gen.pcm_playback_hook = via_playback_pcm_hook;
256 return spec; 140 return spec;
257} 141}
258 142
@@ -308,16 +192,6 @@ static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
308 return codec_type; 192 return codec_type;
309}; 193};
310 194
311#define VIA_JACK_EVENT 0x20
312#define VIA_HP_EVENT 0x01
313#define VIA_LINE_EVENT 0x03
314
315enum {
316 VIA_CTL_WIDGET_VOL,
317 VIA_CTL_WIDGET_MUTE,
318 VIA_CTL_WIDGET_ANALOG_MUTE,
319};
320
321static void analog_low_current_mode(struct hda_codec *codec); 195static void analog_low_current_mode(struct hda_codec *codec);
322static bool is_aa_path_mute(struct hda_codec *codec); 196static bool is_aa_path_mute(struct hda_codec *codec);
323 197
@@ -325,31 +199,34 @@ static bool is_aa_path_mute(struct hda_codec *codec);
325 (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1 && \ 199 (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1 && \
326 !is_aa_path_mute(codec)) 200 !is_aa_path_mute(codec))
327 201
328static void vt1708_stop_hp_work(struct via_spec *spec) 202static void vt1708_stop_hp_work(struct hda_codec *codec)
329{ 203{
330 if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0) 204 struct via_spec *spec = codec->spec;
205 if (spec->codec_type != VT1708 || !spec->gen.autocfg.hp_outs)
331 return; 206 return;
332 if (spec->hp_work_active) { 207 if (spec->hp_work_active) {
333 snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 1); 208 snd_hda_codec_write(codec, 0x1, 0, 0xf81, 1);
334 cancel_delayed_work_sync(&spec->vt1708_hp_work); 209 cancel_delayed_work_sync(&codec->jackpoll_work);
335 spec->hp_work_active = 0; 210 spec->hp_work_active = false;
211 codec->jackpoll_interval = 0;
336 } 212 }
337} 213}
338 214
339static void vt1708_update_hp_work(struct via_spec *spec) 215static void vt1708_update_hp_work(struct hda_codec *codec)
340{ 216{
341 if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0) 217 struct via_spec *spec = codec->spec;
218 if (spec->codec_type != VT1708 || !spec->gen.autocfg.hp_outs)
342 return; 219 return;
343 if (spec->vt1708_jack_detect && 220 if (spec->vt1708_jack_detect) {
344 (spec->active_streams || hp_detect_with_aa(spec->codec))) {
345 if (!spec->hp_work_active) { 221 if (!spec->hp_work_active) {
346 snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 0); 222 codec->jackpoll_interval = msecs_to_jiffies(100);
347 schedule_delayed_work(&spec->vt1708_hp_work, 223 snd_hda_codec_write(codec, 0x1, 0, 0xf81, 0);
348 msecs_to_jiffies(100)); 224 queue_delayed_work(codec->bus->workq,
349 spec->hp_work_active = 1; 225 &codec->jackpoll_work, 0);
226 spec->hp_work_active = true;
350 } 227 }
351 } else if (!hp_detect_with_aa(spec->codec)) 228 } else if (!hp_detect_with_aa(codec))
352 vt1708_stop_hp_work(spec); 229 vt1708_stop_hp_work(codec);
353} 230}
354 231
355static void set_widgets_power_state(struct hda_codec *codec) 232static void set_widgets_power_state(struct hda_codec *codec)
@@ -359,361 +236,10 @@ static void set_widgets_power_state(struct hda_codec *codec)
359 spec->set_widgets_power_state(codec); 236 spec->set_widgets_power_state(codec);
360} 237}
361 238
362static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
363 struct snd_ctl_elem_value *ucontrol)
364{
365 int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
366 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
367
368 set_widgets_power_state(codec);
369 analog_low_current_mode(snd_kcontrol_chip(kcontrol));
370 vt1708_update_hp_work(codec->spec);
371 return change;
372}
373
374/* modify .put = snd_hda_mixer_amp_switch_put */
375#define ANALOG_INPUT_MUTE \
376 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
377 .name = NULL, \
378 .index = 0, \
379 .info = snd_hda_mixer_amp_switch_info, \
380 .get = snd_hda_mixer_amp_switch_get, \
381 .put = analog_input_switch_put, \
382 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
383
384static const struct snd_kcontrol_new via_control_templates[] = {
385 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
386 HDA_CODEC_MUTE(NULL, 0, 0, 0),
387 ANALOG_INPUT_MUTE,
388};
389
390
391/* add dynamic controls */
392static struct snd_kcontrol_new *__via_clone_ctl(struct via_spec *spec,
393 const struct snd_kcontrol_new *tmpl,
394 const char *name)
395{
396 struct snd_kcontrol_new *knew;
397
398 knew = snd_array_new(&spec->kctls);
399 if (!knew)
400 return NULL;
401 *knew = *tmpl;
402 if (!name)
403 name = tmpl->name;
404 if (name) {
405 knew->name = kstrdup(name, GFP_KERNEL);
406 if (!knew->name)
407 return NULL;
408 }
409 return knew;
410}
411
412static int __via_add_control(struct via_spec *spec, int type, const char *name,
413 int idx, unsigned long val)
414{
415 struct snd_kcontrol_new *knew;
416
417 knew = __via_clone_ctl(spec, &via_control_templates[type], name);
418 if (!knew)
419 return -ENOMEM;
420 knew->index = idx;
421 if (get_amp_nid_(val))
422 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
423 knew->private_value = val;
424 return 0;
425}
426
427#define via_add_control(spec, type, name, val) \
428 __via_add_control(spec, type, name, 0, val)
429
430#define via_clone_control(spec, tmpl) __via_clone_ctl(spec, tmpl, NULL)
431
432static void via_free_kctls(struct hda_codec *codec)
433{
434 struct via_spec *spec = codec->spec;
435
436 if (spec->kctls.list) {
437 struct snd_kcontrol_new *kctl = spec->kctls.list;
438 int i;
439 for (i = 0; i < spec->kctls.used; i++)
440 kfree(kctl[i].name);
441 }
442 snd_array_free(&spec->kctls);
443}
444
445/* create input playback/capture controls for the given pin */
446static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
447 int type_idx, int idx, int mix_nid)
448{
449 char name[32];
450 int err;
451
452 sprintf(name, "%s Playback Volume", ctlname);
453 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
454 HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
455 if (err < 0)
456 return err;
457 sprintf(name, "%s Playback Switch", ctlname);
458 err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
459 HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
460 if (err < 0)
461 return err;
462 return 0;
463}
464
465#define get_connection_index(codec, mux, nid) \
466 snd_hda_get_conn_index(codec, mux, nid, 0)
467
468static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
469 unsigned int mask)
470{
471 unsigned int caps;
472 if (!nid)
473 return false;
474 caps = get_wcaps(codec, nid);
475 if (dir == HDA_INPUT)
476 caps &= AC_WCAP_IN_AMP;
477 else
478 caps &= AC_WCAP_OUT_AMP;
479 if (!caps)
480 return false;
481 if (query_amp_caps(codec, nid, dir) & mask)
482 return true;
483 return false;
484}
485
486#define have_mute(codec, nid, dir) \
487 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
488
489/* enable/disable the output-route mixers */
490static void activate_output_mix(struct hda_codec *codec, struct nid_path *path,
491 hda_nid_t mix_nid, int idx, bool enable)
492{
493 int i, num, val;
494
495 if (!path)
496 return;
497 num = snd_hda_get_num_conns(codec, mix_nid);
498 for (i = 0; i < num; i++) {
499 if (i == idx)
500 val = AMP_IN_UNMUTE(i);
501 else
502 val = AMP_IN_MUTE(i);
503 snd_hda_codec_write(codec, mix_nid, 0,
504 AC_VERB_SET_AMP_GAIN_MUTE, val);
505 }
506}
507
508/* enable/disable the output-route */
509static void activate_output_path(struct hda_codec *codec, struct nid_path *path,
510 bool enable, bool force)
511{
512 struct via_spec *spec = codec->spec;
513 int i;
514 for (i = 0; i < path->depth; i++) {
515 hda_nid_t src, dst;
516 int idx = path->idx[i];
517 src = path->path[i];
518 if (i < path->depth - 1)
519 dst = path->path[i + 1];
520 else
521 dst = 0;
522 if (enable && path->multi[i])
523 snd_hda_codec_write(codec, dst, 0,
524 AC_VERB_SET_CONNECT_SEL, idx);
525 if (!force && (dst == spec->aa_mix_nid))
526 continue;
527 if (have_mute(codec, dst, HDA_INPUT))
528 activate_output_mix(codec, path, dst, idx, enable);
529 if (!force && (src == path->vol_ctl || src == path->mute_ctl))
530 continue;
531 if (have_mute(codec, src, HDA_OUTPUT)) {
532 int val = enable ? AMP_OUT_UNMUTE : AMP_OUT_MUTE;
533 snd_hda_codec_write(codec, src, 0,
534 AC_VERB_SET_AMP_GAIN_MUTE, val);
535 }
536 }
537}
538
539/* set the given pin as output */
540static void init_output_pin(struct hda_codec *codec, hda_nid_t pin,
541 int pin_type)
542{
543 if (!pin)
544 return;
545 snd_hda_set_pin_ctl(codec, pin, pin_type);
546 if (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD)
547 snd_hda_codec_write(codec, pin, 0,
548 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
549}
550
551static void via_auto_init_output(struct hda_codec *codec,
552 struct nid_path *path, int pin_type)
553{
554 unsigned int caps;
555 hda_nid_t pin;
556
557 if (!path->depth)
558 return;
559 pin = path->path[path->depth - 1];
560
561 init_output_pin(codec, pin, pin_type);
562 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
563 caps = query_amp_caps(codec, pin, HDA_OUTPUT);
564 else
565 caps = 0;
566 if (caps & AC_AMPCAP_MUTE) {
567 unsigned int val;
568 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
569 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
570 AMP_OUT_MUTE | val);
571 }
572 activate_output_path(codec, path, true, true); /* force on */
573}
574
575static void via_auto_init_multi_out(struct hda_codec *codec)
576{
577 struct via_spec *spec = codec->spec;
578 struct nid_path *path;
579 int i;
580
581 for (i = 0; i < spec->autocfg.line_outs + spec->smart51_nums; i++) {
582 path = &spec->out_path[i];
583 if (!i && spec->aamix_mode && spec->out_mix_path.depth)
584 path = &spec->out_mix_path;
585 via_auto_init_output(codec, path, PIN_OUT);
586 }
587}
588
589/* deactivate the inactive headphone-paths */
590static void deactivate_hp_paths(struct hda_codec *codec)
591{
592 struct via_spec *spec = codec->spec;
593 int shared = spec->hp_indep_shared;
594
595 if (spec->hp_independent_mode) {
596 activate_output_path(codec, &spec->hp_path, false, false);
597 activate_output_path(codec, &spec->hp_mix_path, false, false);
598 if (shared)
599 activate_output_path(codec, &spec->out_path[shared],
600 false, false);
601 } else if (spec->aamix_mode || !spec->hp_path.depth) {
602 activate_output_path(codec, &spec->hp_indep_path, false, false);
603 activate_output_path(codec, &spec->hp_path, false, false);
604 } else {
605 activate_output_path(codec, &spec->hp_indep_path, false, false);
606 activate_output_path(codec, &spec->hp_mix_path, false, false);
607 }
608}
609
610static void via_auto_init_hp_out(struct hda_codec *codec)
611{
612 struct via_spec *spec = codec->spec;
613
614 if (!spec->hp_path.depth) {
615 via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
616 return;
617 }
618 deactivate_hp_paths(codec);
619 if (spec->hp_independent_mode)
620 via_auto_init_output(codec, &spec->hp_indep_path, PIN_HP);
621 else if (spec->aamix_mode)
622 via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
623 else
624 via_auto_init_output(codec, &spec->hp_path, PIN_HP);
625}
626
627static void via_auto_init_speaker_out(struct hda_codec *codec)
628{
629 struct via_spec *spec = codec->spec;
630
631 if (!spec->autocfg.speaker_outs)
632 return;
633 if (!spec->speaker_path.depth) {
634 via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
635 return;
636 }
637 if (!spec->aamix_mode) {
638 activate_output_path(codec, &spec->speaker_mix_path,
639 false, false);
640 via_auto_init_output(codec, &spec->speaker_path, PIN_OUT);
641 } else {
642 activate_output_path(codec, &spec->speaker_path, false, false);
643 via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
644 }
645}
646
647static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin);
648static void via_hp_automute(struct hda_codec *codec);
649
650static void via_auto_init_analog_input(struct hda_codec *codec)
651{
652 struct via_spec *spec = codec->spec;
653 const struct auto_pin_cfg *cfg = &spec->autocfg;
654 hda_nid_t conn[HDA_MAX_CONNECTIONS];
655 unsigned int ctl;
656 int i, num_conns;
657
658 /* init ADCs */
659 for (i = 0; i < spec->num_adc_nids; i++) {
660 hda_nid_t nid = spec->adc_nids[i];
661 if (!(get_wcaps(codec, nid) & AC_WCAP_IN_AMP) ||
662 !(query_amp_caps(codec, nid, HDA_INPUT) & AC_AMPCAP_MUTE))
663 continue;
664 snd_hda_codec_write(codec, spec->adc_nids[i], 0,
665 AC_VERB_SET_AMP_GAIN_MUTE,
666 AMP_IN_UNMUTE(0));
667 }
668
669 /* init pins */
670 for (i = 0; i < cfg->num_inputs; i++) {
671 hda_nid_t nid = cfg->inputs[i].pin;
672 if (spec->smart51_enabled && is_smart51_pins(codec, nid))
673 ctl = PIN_OUT;
674 else {
675 ctl = PIN_IN;
676 if (cfg->inputs[i].type == AUTO_PIN_MIC)
677 ctl |= snd_hda_get_default_vref(codec, nid);
678 }
679 snd_hda_set_pin_ctl(codec, nid, ctl);
680 }
681
682 /* init input-src */
683 for (i = 0; i < spec->num_adc_nids; i++) {
684 int adc_idx = spec->inputs[spec->cur_mux[i]].adc_idx;
685 /* secondary ADCs must have the unique MUX */
686 if (i > 0 && !spec->mux_nids[i])
687 break;
688 if (spec->mux_nids[adc_idx]) {
689 int mux_idx = spec->inputs[spec->cur_mux[i]].mux_idx;
690 snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
691 AC_VERB_SET_CONNECT_SEL,
692 mux_idx);
693 }
694 if (spec->dyn_adc_switch)
695 break; /* only one input-src */
696 }
697
698 /* init aa-mixer */
699 if (!spec->aa_mix_nid)
700 return;
701 num_conns = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
702 ARRAY_SIZE(conn));
703 for (i = 0; i < num_conns; i++) {
704 unsigned int caps = get_wcaps(codec, conn[i]);
705 if (get_wcaps_type(caps) == AC_WID_PIN)
706 snd_hda_codec_write(codec, spec->aa_mix_nid, 0,
707 AC_VERB_SET_AMP_GAIN_MUTE,
708 AMP_IN_MUTE(i));
709 }
710}
711
712static void update_power_state(struct hda_codec *codec, hda_nid_t nid, 239static void update_power_state(struct hda_codec *codec, hda_nid_t nid,
713 unsigned int parm) 240 unsigned int parm)
714{ 241{
715 if (snd_hda_codec_read(codec, nid, 0, 242 if (snd_hda_check_power_state(codec, nid, parm))
716 AC_VERB_GET_POWER_STATE, 0) == parm)
717 return; 243 return;
718 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm); 244 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
719} 245}
@@ -723,8 +249,8 @@ static void update_conv_power_state(struct hda_codec *codec, hda_nid_t nid,
723{ 249{
724 struct via_spec *spec = codec->spec; 250 struct via_spec *spec = codec->spec;
725 unsigned int format; 251 unsigned int format;
726 if (snd_hda_codec_read(codec, nid, 0, 252
727 AC_VERB_GET_POWER_STATE, 0) == parm) 253 if (snd_hda_check_power_state(codec, nid, parm))
728 return; 254 return;
729 format = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); 255 format = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
730 if (format && (spec->dac_stream_tag[index] != format)) 256 if (format && (spec->dac_stream_tag[index] != format))
@@ -740,6 +266,23 @@ static void update_conv_power_state(struct hda_codec *codec, hda_nid_t nid,
740 } 266 }
741} 267}
742 268
269static bool smart51_enabled(struct hda_codec *codec)
270{
271 struct via_spec *spec = codec->spec;
272 return spec->gen.ext_channel_count > 2;
273}
274
275static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
276{
277 struct via_spec *spec = codec->spec;
278 int i;
279
280 for (i = 0; i < spec->gen.multi_ios; i++)
281 if (spec->gen.multi_io[i].pin == pin)
282 return true;
283 return false;
284}
285
743static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid, 286static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
744 unsigned int *affected_parm) 287 unsigned int *affected_parm)
745{ 288{
@@ -754,7 +297,7 @@ static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
754 no_presence |= spec->no_pin_power_ctl; 297 no_presence |= spec->no_pin_power_ctl;
755 if (!no_presence) 298 if (!no_presence)
756 present = snd_hda_jack_detect(codec, nid); 299 present = snd_hda_jack_detect(codec, nid);
757 if ((spec->smart51_enabled && is_smart51_pins(codec, nid)) 300 if ((smart51_enabled(codec) && is_smart51_pins(codec, nid))
758 || ((no_presence || present) 301 || ((no_presence || present)
759 && get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) { 302 && get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
760 *affected_parm = AC_PWRST_D0; /* if it's connected */ 303 *affected_parm = AC_PWRST_D0; /* if it's connected */
@@ -795,277 +338,29 @@ static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
795 return 1; 338 return 1;
796} 339}
797 340
798static const struct snd_kcontrol_new via_pin_power_ctl_enum = { 341static const struct snd_kcontrol_new via_pin_power_ctl_enum[] = {
342 {
799 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 343 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
800 .name = "Dynamic Power-Control", 344 .name = "Dynamic Power-Control",
801 .info = via_pin_power_ctl_info, 345 .info = via_pin_power_ctl_info,
802 .get = via_pin_power_ctl_get, 346 .get = via_pin_power_ctl_get,
803 .put = via_pin_power_ctl_put, 347 .put = via_pin_power_ctl_put,
348 },
349 {} /* terminator */
804}; 350};
805 351
806 352
807static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
808 struct snd_ctl_elem_info *uinfo)
809{
810 static const char * const texts[] = { "OFF", "ON" };
811
812 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
813 uinfo->count = 1;
814 uinfo->value.enumerated.items = 2;
815 if (uinfo->value.enumerated.item >= 2)
816 uinfo->value.enumerated.item = 1;
817 strcpy(uinfo->value.enumerated.name,
818 texts[uinfo->value.enumerated.item]);
819 return 0;
820}
821
822static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
823 struct snd_ctl_elem_value *ucontrol)
824{
825 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
826 struct via_spec *spec = codec->spec;
827
828 ucontrol->value.enumerated.item[0] = spec->hp_independent_mode;
829 return 0;
830}
831
832/* adjust spec->multiout setup according to the current flags */
833static void setup_playback_multi_pcm(struct via_spec *spec)
834{
835 const struct auto_pin_cfg *cfg = &spec->autocfg;
836 spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
837 spec->multiout.hp_nid = 0;
838 if (!spec->hp_independent_mode) {
839 if (!spec->hp_indep_shared)
840 spec->multiout.hp_nid = spec->hp_dac_nid;
841 } else {
842 if (spec->hp_indep_shared)
843 spec->multiout.num_dacs = cfg->line_outs - 1;
844 }
845}
846
847/* update DAC setups according to indep-HP switch;
848 * this function is called only when indep-HP is modified
849 */
850static void switch_indep_hp_dacs(struct hda_codec *codec)
851{
852 struct via_spec *spec = codec->spec;
853 int shared = spec->hp_indep_shared;
854 hda_nid_t shared_dac, hp_dac;
855
856 if (!spec->opened_streams)
857 return;
858
859 shared_dac = shared ? spec->multiout.dac_nids[shared] : 0;
860 hp_dac = spec->hp_dac_nid;
861 if (spec->hp_independent_mode) {
862 /* switch to indep-HP mode */
863 if (spec->active_streams & STREAM_MULTI_OUT) {
864 __snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
865 __snd_hda_codec_cleanup_stream(codec, shared_dac, 1);
866 }
867 if (spec->active_streams & STREAM_INDEP_HP)
868 snd_hda_codec_setup_stream(codec, hp_dac,
869 spec->cur_hp_stream_tag, 0,
870 spec->cur_hp_format);
871 } else {
872 /* back to HP or shared-DAC */
873 if (spec->active_streams & STREAM_INDEP_HP)
874 __snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
875 if (spec->active_streams & STREAM_MULTI_OUT) {
876 hda_nid_t dac;
877 int ch;
878 if (shared_dac) { /* reset mutli-ch DAC */
879 dac = shared_dac;
880 ch = shared * 2;
881 } else { /* reset HP DAC */
882 dac = hp_dac;
883 ch = 0;
884 }
885 snd_hda_codec_setup_stream(codec, dac,
886 spec->cur_dac_stream_tag, ch,
887 spec->cur_dac_format);
888 }
889 }
890 setup_playback_multi_pcm(spec);
891}
892
893static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
894 struct snd_ctl_elem_value *ucontrol)
895{
896 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
897 struct via_spec *spec = codec->spec;
898 int cur, shared;
899
900 mutex_lock(&spec->config_mutex);
901 cur = !!ucontrol->value.enumerated.item[0];
902 if (spec->hp_independent_mode == cur) {
903 mutex_unlock(&spec->config_mutex);
904 return 0;
905 }
906 spec->hp_independent_mode = cur;
907 shared = spec->hp_indep_shared;
908 deactivate_hp_paths(codec);
909 if (cur)
910 activate_output_path(codec, &spec->hp_indep_path, true, false);
911 else {
912 if (shared)
913 activate_output_path(codec, &spec->out_path[shared],
914 true, false);
915 if (spec->aamix_mode || !spec->hp_path.depth)
916 activate_output_path(codec, &spec->hp_mix_path,
917 true, false);
918 else
919 activate_output_path(codec, &spec->hp_path,
920 true, false);
921 }
922
923 switch_indep_hp_dacs(codec);
924 mutex_unlock(&spec->config_mutex);
925
926 /* update jack power state */
927 set_widgets_power_state(codec);
928 via_hp_automute(codec);
929 return 1;
930}
931
932static const struct snd_kcontrol_new via_hp_mixer = {
933 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
934 .name = "Independent HP",
935 .info = via_independent_hp_info,
936 .get = via_independent_hp_get,
937 .put = via_independent_hp_put,
938};
939
940static int via_hp_build(struct hda_codec *codec)
941{
942 struct via_spec *spec = codec->spec;
943 struct snd_kcontrol_new *knew;
944 hda_nid_t nid;
945
946 nid = spec->autocfg.hp_pins[0];
947 knew = via_clone_control(spec, &via_hp_mixer);
948 if (knew == NULL)
949 return -ENOMEM;
950
951 knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
952
953 return 0;
954}
955
956static void notify_aa_path_ctls(struct hda_codec *codec)
957{
958 struct via_spec *spec = codec->spec;
959 int i;
960
961 for (i = 0; i < spec->smart51_nums; i++) {
962 struct snd_kcontrol *ctl;
963 struct snd_ctl_elem_id id;
964 memset(&id, 0, sizeof(id));
965 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
966 sprintf(id.name, "%s Playback Volume", spec->smart51_labels[i]);
967 ctl = snd_hda_find_mixer_ctl(codec, id.name);
968 if (ctl)
969 snd_ctl_notify(codec->bus->card,
970 SNDRV_CTL_EVENT_MASK_VALUE,
971 &ctl->id);
972 }
973}
974
975static void mute_aa_path(struct hda_codec *codec, int mute)
976{
977 struct via_spec *spec = codec->spec;
978 int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
979 int i;
980
981 /* check AA path's mute status */
982 for (i = 0; i < spec->smart51_nums; i++) {
983 if (spec->smart51_idxs[i] < 0)
984 continue;
985 snd_hda_codec_amp_stereo(codec, spec->aa_mix_nid,
986 HDA_INPUT, spec->smart51_idxs[i],
987 HDA_AMP_MUTE, val);
988 }
989}
990
991static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
992{
993 struct via_spec *spec = codec->spec;
994 int i;
995
996 for (i = 0; i < spec->smart51_nums; i++)
997 if (spec->smart51_pins[i] == pin)
998 return true;
999 return false;
1000}
1001
1002static int via_smart51_get(struct snd_kcontrol *kcontrol,
1003 struct snd_ctl_elem_value *ucontrol)
1004{
1005 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1006 struct via_spec *spec = codec->spec;
1007
1008 *ucontrol->value.integer.value = spec->smart51_enabled;
1009 return 0;
1010}
1011
1012static int via_smart51_put(struct snd_kcontrol *kcontrol,
1013 struct snd_ctl_elem_value *ucontrol)
1014{
1015 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1016 struct via_spec *spec = codec->spec;
1017 int out_in = *ucontrol->value.integer.value
1018 ? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
1019 int i;
1020
1021 for (i = 0; i < spec->smart51_nums; i++) {
1022 hda_nid_t nid = spec->smart51_pins[i];
1023 unsigned int parm;
1024
1025 parm = snd_hda_codec_read(codec, nid, 0,
1026 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1027 parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
1028 parm |= out_in;
1029 snd_hda_set_pin_ctl(codec, nid, parm);
1030 if (out_in == AC_PINCTL_OUT_EN) {
1031 mute_aa_path(codec, 1);
1032 notify_aa_path_ctls(codec);
1033 }
1034 }
1035 spec->smart51_enabled = *ucontrol->value.integer.value;
1036 set_widgets_power_state(codec);
1037 return 1;
1038}
1039
1040static const struct snd_kcontrol_new via_smart51_mixer = {
1041 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1042 .name = "Smart 5.1",
1043 .count = 1,
1044 .info = snd_ctl_boolean_mono_info,
1045 .get = via_smart51_get,
1046 .put = via_smart51_put,
1047};
1048
1049static int via_smart51_build(struct hda_codec *codec)
1050{
1051 struct via_spec *spec = codec->spec;
1052
1053 if (!spec->smart51_nums)
1054 return 0;
1055 if (!via_clone_control(spec, &via_smart51_mixer))
1056 return -ENOMEM;
1057 return 0;
1058}
1059
1060/* check AA path's mute status */ 353/* check AA path's mute status */
1061static bool is_aa_path_mute(struct hda_codec *codec) 354static bool is_aa_path_mute(struct hda_codec *codec)
1062{ 355{
1063 struct via_spec *spec = codec->spec; 356 struct via_spec *spec = codec->spec;
1064 const struct hda_amp_list *p; 357 const struct hda_amp_list *p;
1065 int i, ch, v; 358 int ch, v;
1066 359
1067 for (i = 0; i < spec->num_loopbacks; i++) { 360 p = spec->gen.loopback.amplist;
1068 p = &spec->loopback_list[i]; 361 if (!p)
362 return true;
363 for (; p->nid; p++) {
1069 for (ch = 0; ch < 2; ch++) { 364 for (ch = 0; ch < 2; ch++) {
1070 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, 365 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
1071 p->idx); 366 p->idx);
@@ -1086,7 +381,7 @@ static void __analog_low_current_mode(struct hda_codec *codec, bool force)
1086 if (spec->no_pin_power_ctl) 381 if (spec->no_pin_power_ctl)
1087 enable = false; 382 enable = false;
1088 else 383 else
1089 enable = is_aa_path_mute(codec) && !spec->opened_streams; 384 enable = is_aa_path_mute(codec) && !spec->gen.active_streams;
1090 if (enable == spec->alc_mode && !force) 385 if (enable == spec->alc_mode && !force)
1091 return; 386 return;
1092 spec->alc_mode = enable; 387 spec->alc_mode = enable;
@@ -1131,366 +426,17 @@ static void analog_low_current_mode(struct hda_codec *codec)
1131 return __analog_low_current_mode(codec, false); 426 return __analog_low_current_mode(codec, false);
1132} 427}
1133 428
1134/*
1135 * generic initialization of ADC, input mixers and output mixers
1136 */
1137static const struct hda_verb vt1708_init_verbs[] = {
1138 /* power down jack detect function */
1139 {0x1, 0xf81, 0x1},
1140 { }
1141};
1142
1143static void set_stream_open(struct hda_codec *codec, int bit, bool active)
1144{
1145 struct via_spec *spec = codec->spec;
1146
1147 if (active)
1148 spec->opened_streams |= bit;
1149 else
1150 spec->opened_streams &= ~bit;
1151 analog_low_current_mode(codec);
1152}
1153
1154static int via_playback_multi_pcm_open(struct hda_pcm_stream *hinfo,
1155 struct hda_codec *codec,
1156 struct snd_pcm_substream *substream)
1157{
1158 struct via_spec *spec = codec->spec;
1159 const struct auto_pin_cfg *cfg = &spec->autocfg;
1160 int err;
1161
1162 spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
1163 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
1164 set_stream_open(codec, STREAM_MULTI_OUT, true);
1165 err = snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
1166 hinfo);
1167 if (err < 0) {
1168 set_stream_open(codec, STREAM_MULTI_OUT, false);
1169 return err;
1170 }
1171 return 0;
1172}
1173
1174static int via_playback_multi_pcm_close(struct hda_pcm_stream *hinfo,
1175 struct hda_codec *codec,
1176 struct snd_pcm_substream *substream)
1177{
1178 set_stream_open(codec, STREAM_MULTI_OUT, false);
1179 return 0;
1180}
1181
1182static int via_playback_hp_pcm_open(struct hda_pcm_stream *hinfo,
1183 struct hda_codec *codec,
1184 struct snd_pcm_substream *substream)
1185{
1186 struct via_spec *spec = codec->spec;
1187
1188 if (snd_BUG_ON(!spec->hp_dac_nid))
1189 return -EINVAL;
1190 set_stream_open(codec, STREAM_INDEP_HP, true);
1191 return 0;
1192}
1193
1194static int via_playback_hp_pcm_close(struct hda_pcm_stream *hinfo,
1195 struct hda_codec *codec,
1196 struct snd_pcm_substream *substream)
1197{
1198 set_stream_open(codec, STREAM_INDEP_HP, false);
1199 return 0;
1200}
1201
1202static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
1203 struct hda_codec *codec,
1204 unsigned int stream_tag,
1205 unsigned int format,
1206 struct snd_pcm_substream *substream)
1207{
1208 struct via_spec *spec = codec->spec;
1209
1210 mutex_lock(&spec->config_mutex);
1211 setup_playback_multi_pcm(spec);
1212 snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
1213 format, substream);
1214 /* remember for dynamic DAC switch with indep-HP */
1215 spec->active_streams |= STREAM_MULTI_OUT;
1216 spec->cur_dac_stream_tag = stream_tag;
1217 spec->cur_dac_format = format;
1218 mutex_unlock(&spec->config_mutex);
1219 vt1708_update_hp_work(spec);
1220 return 0;
1221}
1222
1223static int via_playback_hp_pcm_prepare(struct hda_pcm_stream *hinfo,
1224 struct hda_codec *codec,
1225 unsigned int stream_tag,
1226 unsigned int format,
1227 struct snd_pcm_substream *substream)
1228{
1229 struct via_spec *spec = codec->spec;
1230
1231 mutex_lock(&spec->config_mutex);
1232 if (spec->hp_independent_mode)
1233 snd_hda_codec_setup_stream(codec, spec->hp_dac_nid,
1234 stream_tag, 0, format);
1235 spec->active_streams |= STREAM_INDEP_HP;
1236 spec->cur_hp_stream_tag = stream_tag;
1237 spec->cur_hp_format = format;
1238 mutex_unlock(&spec->config_mutex);
1239 vt1708_update_hp_work(spec);
1240 return 0;
1241}
1242
1243static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
1244 struct hda_codec *codec,
1245 struct snd_pcm_substream *substream)
1246{
1247 struct via_spec *spec = codec->spec;
1248
1249 mutex_lock(&spec->config_mutex);
1250 snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
1251 spec->active_streams &= ~STREAM_MULTI_OUT;
1252 mutex_unlock(&spec->config_mutex);
1253 vt1708_update_hp_work(spec);
1254 return 0;
1255}
1256
1257static int via_playback_hp_pcm_cleanup(struct hda_pcm_stream *hinfo,
1258 struct hda_codec *codec,
1259 struct snd_pcm_substream *substream)
1260{
1261 struct via_spec *spec = codec->spec;
1262
1263 mutex_lock(&spec->config_mutex);
1264 if (spec->hp_independent_mode)
1265 snd_hda_codec_setup_stream(codec, spec->hp_dac_nid, 0, 0, 0);
1266 spec->active_streams &= ~STREAM_INDEP_HP;
1267 mutex_unlock(&spec->config_mutex);
1268 vt1708_update_hp_work(spec);
1269 return 0;
1270}
1271
1272/*
1273 * Digital out
1274 */
1275static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1276 struct hda_codec *codec,
1277 struct snd_pcm_substream *substream)
1278{
1279 struct via_spec *spec = codec->spec;
1280 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1281}
1282
1283static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1284 struct hda_codec *codec,
1285 struct snd_pcm_substream *substream)
1286{
1287 struct via_spec *spec = codec->spec;
1288 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1289}
1290
1291static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1292 struct hda_codec *codec,
1293 unsigned int stream_tag,
1294 unsigned int format,
1295 struct snd_pcm_substream *substream)
1296{
1297 struct via_spec *spec = codec->spec;
1298 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1299 stream_tag, format, substream);
1300}
1301
1302static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1303 struct hda_codec *codec,
1304 struct snd_pcm_substream *substream)
1305{
1306 struct via_spec *spec = codec->spec;
1307 snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
1308 return 0;
1309}
1310
1311/*
1312 * Analog capture
1313 */
1314static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1315 struct hda_codec *codec,
1316 unsigned int stream_tag,
1317 unsigned int format,
1318 struct snd_pcm_substream *substream)
1319{
1320 struct via_spec *spec = codec->spec;
1321
1322 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1323 stream_tag, 0, format);
1324 return 0;
1325}
1326
1327static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1328 struct hda_codec *codec,
1329 struct snd_pcm_substream *substream)
1330{
1331 struct via_spec *spec = codec->spec;
1332 snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
1333 return 0;
1334}
1335
1336/* analog capture with dynamic ADC switching */
1337static int via_dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1338 struct hda_codec *codec,
1339 unsigned int stream_tag,
1340 unsigned int format,
1341 struct snd_pcm_substream *substream)
1342{
1343 struct via_spec *spec = codec->spec;
1344 int adc_idx = spec->inputs[spec->cur_mux[0]].adc_idx;
1345
1346 mutex_lock(&spec->config_mutex);
1347 spec->cur_adc = spec->adc_nids[adc_idx];
1348 spec->cur_adc_stream_tag = stream_tag;
1349 spec->cur_adc_format = format;
1350 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
1351 mutex_unlock(&spec->config_mutex);
1352 return 0;
1353}
1354
1355static int via_dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1356 struct hda_codec *codec,
1357 struct snd_pcm_substream *substream)
1358{
1359 struct via_spec *spec = codec->spec;
1360
1361 mutex_lock(&spec->config_mutex);
1362 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1363 spec->cur_adc = 0;
1364 mutex_unlock(&spec->config_mutex);
1365 return 0;
1366}
1367
1368/* re-setup the stream if running; called from input-src put */
1369static bool via_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
1370{
1371 struct via_spec *spec = codec->spec;
1372 int adc_idx = spec->inputs[cur].adc_idx;
1373 hda_nid_t adc = spec->adc_nids[adc_idx];
1374 bool ret = false;
1375
1376 mutex_lock(&spec->config_mutex);
1377 if (spec->cur_adc && spec->cur_adc != adc) {
1378 /* stream is running, let's swap the current ADC */
1379 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1380 spec->cur_adc = adc;
1381 snd_hda_codec_setup_stream(codec, adc,
1382 spec->cur_adc_stream_tag, 0,
1383 spec->cur_adc_format);
1384 ret = true;
1385 }
1386 mutex_unlock(&spec->config_mutex);
1387 return ret;
1388}
1389
1390static const struct hda_pcm_stream via_pcm_analog_playback = {
1391 .substreams = 1,
1392 .channels_min = 2,
1393 .channels_max = 8,
1394 /* NID is set in via_build_pcms */
1395 .ops = {
1396 .open = via_playback_multi_pcm_open,
1397 .close = via_playback_multi_pcm_close,
1398 .prepare = via_playback_multi_pcm_prepare,
1399 .cleanup = via_playback_multi_pcm_cleanup
1400 },
1401};
1402
1403static const struct hda_pcm_stream via_pcm_hp_playback = {
1404 .substreams = 1,
1405 .channels_min = 2,
1406 .channels_max = 2,
1407 /* NID is set in via_build_pcms */
1408 .ops = {
1409 .open = via_playback_hp_pcm_open,
1410 .close = via_playback_hp_pcm_close,
1411 .prepare = via_playback_hp_pcm_prepare,
1412 .cleanup = via_playback_hp_pcm_cleanup
1413 },
1414};
1415
1416static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
1417 .substreams = 1,
1418 .channels_min = 2,
1419 .channels_max = 8,
1420 /* NID is set in via_build_pcms */
1421 /* We got noisy outputs on the right channel on VT1708 when
1422 * 24bit samples are used. Until any workaround is found,
1423 * disable the 24bit format, so far.
1424 */
1425 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1426 .ops = {
1427 .open = via_playback_multi_pcm_open,
1428 .close = via_playback_multi_pcm_close,
1429 .prepare = via_playback_multi_pcm_prepare,
1430 .cleanup = via_playback_multi_pcm_cleanup
1431 },
1432};
1433
1434static const struct hda_pcm_stream via_pcm_analog_capture = {
1435 .substreams = 1, /* will be changed in via_build_pcms() */
1436 .channels_min = 2,
1437 .channels_max = 2,
1438 /* NID is set in via_build_pcms */
1439 .ops = {
1440 .prepare = via_capture_pcm_prepare,
1441 .cleanup = via_capture_pcm_cleanup
1442 },
1443};
1444
1445static const struct hda_pcm_stream via_pcm_dyn_adc_analog_capture = {
1446 .substreams = 1,
1447 .channels_min = 2,
1448 .channels_max = 2,
1449 /* NID is set in via_build_pcms */
1450 .ops = {
1451 .prepare = via_dyn_adc_capture_pcm_prepare,
1452 .cleanup = via_dyn_adc_capture_pcm_cleanup,
1453 },
1454};
1455
1456static const struct hda_pcm_stream via_pcm_digital_playback = {
1457 .substreams = 1,
1458 .channels_min = 2,
1459 .channels_max = 2,
1460 /* NID is set in via_build_pcms */
1461 .ops = {
1462 .open = via_dig_playback_pcm_open,
1463 .close = via_dig_playback_pcm_close,
1464 .prepare = via_dig_playback_pcm_prepare,
1465 .cleanup = via_dig_playback_pcm_cleanup
1466 },
1467};
1468
1469static const struct hda_pcm_stream via_pcm_digital_capture = {
1470 .substreams = 1,
1471 .channels_min = 2,
1472 .channels_max = 2,
1473};
1474
1475/*
1476 * slave controls for virtual master
1477 */
1478static const char * const via_slave_pfxs[] = {
1479 "Front", "Surround", "Center", "LFE", "Side",
1480 "Headphone", "Speaker", "Bass Speaker",
1481 NULL,
1482};
1483
1484static int via_build_controls(struct hda_codec *codec) 429static int via_build_controls(struct hda_codec *codec)
1485{ 430{
1486 struct via_spec *spec = codec->spec; 431 struct via_spec *spec = codec->spec;
1487 struct snd_kcontrol *kctl;
1488 int err, i; 432 int err, i;
1489 433
1490 spec->no_pin_power_ctl = 1; 434 err = snd_hda_gen_build_controls(codec);
435 if (err < 0)
436 return err;
437
1491 if (spec->set_widgets_power_state) 438 if (spec->set_widgets_power_state)
1492 if (!via_clone_control(spec, &via_pin_power_ctl_enum)) 439 spec->mixers[spec->num_mixers++] = via_pin_power_ctl_enum;
1493 return -ENOMEM;
1494 440
1495 for (i = 0; i < spec->num_mixers; i++) { 441 for (i = 0; i < spec->num_mixers; i++) {
1496 err = snd_hda_add_new_ctls(codec, spec->mixers[i]); 442 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
@@ -1498,152 +444,16 @@ static int via_build_controls(struct hda_codec *codec)
1498 return err; 444 return err;
1499 } 445 }
1500 446
1501 if (spec->multiout.dig_out_nid) {
1502 err = snd_hda_create_spdif_out_ctls(codec,
1503 spec->multiout.dig_out_nid,
1504 spec->multiout.dig_out_nid);
1505 if (err < 0)
1506 return err;
1507 err = snd_hda_create_spdif_share_sw(codec,
1508 &spec->multiout);
1509 if (err < 0)
1510 return err;
1511 spec->multiout.share_spdif = 1;
1512 }
1513 if (spec->dig_in_nid) {
1514 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1515 if (err < 0)
1516 return err;
1517 }
1518
1519 /* if we have no master control, let's create it */
1520 if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1521 unsigned int vmaster_tlv[4];
1522 snd_hda_set_vmaster_tlv(codec, spec->multiout.dac_nids[0],
1523 HDA_OUTPUT, vmaster_tlv);
1524 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
1525 vmaster_tlv, via_slave_pfxs,
1526 "Playback Volume");
1527 if (err < 0)
1528 return err;
1529 }
1530 if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1531 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
1532 NULL, via_slave_pfxs,
1533 "Playback Switch");
1534 if (err < 0)
1535 return err;
1536 }
1537
1538 /* assign Capture Source enums to NID */
1539 kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1540 for (i = 0; kctl && i < kctl->count; i++) {
1541 if (!spec->mux_nids[i])
1542 continue;
1543 err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
1544 if (err < 0)
1545 return err;
1546 }
1547
1548 via_free_kctls(codec); /* no longer needed */
1549
1550 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
1551 if (err < 0)
1552 return err;
1553
1554 return 0; 447 return 0;
1555} 448}
1556 449
1557static int via_build_pcms(struct hda_codec *codec) 450static void via_playback_pcm_hook(struct hda_pcm_stream *hinfo,
451 struct hda_codec *codec,
452 struct snd_pcm_substream *substream,
453 int action)
1558{ 454{
1559 struct via_spec *spec = codec->spec; 455 analog_low_current_mode(codec);
1560 struct hda_pcm *info = spec->pcm_rec; 456 vt1708_update_hp_work(codec);
1561
1562 codec->num_pcms = 0;
1563 codec->pcm_info = info;
1564
1565 if (spec->multiout.num_dacs || spec->num_adc_nids) {
1566 snprintf(spec->stream_name_analog,
1567 sizeof(spec->stream_name_analog),
1568 "%s Analog", codec->chip_name);
1569 info->name = spec->stream_name_analog;
1570
1571 if (spec->multiout.num_dacs) {
1572 if (!spec->stream_analog_playback)
1573 spec->stream_analog_playback =
1574 &via_pcm_analog_playback;
1575 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1576 *spec->stream_analog_playback;
1577 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1578 spec->multiout.dac_nids[0];
1579 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1580 spec->multiout.max_channels;
1581 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT
1582 && spec->autocfg.line_outs == 2)
1583 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
1584 snd_pcm_2_1_chmaps;
1585 }
1586
1587 if (!spec->stream_analog_capture) {
1588 if (spec->dyn_adc_switch)
1589 spec->stream_analog_capture =
1590 &via_pcm_dyn_adc_analog_capture;
1591 else
1592 spec->stream_analog_capture =
1593 &via_pcm_analog_capture;
1594 }
1595 if (spec->num_adc_nids) {
1596 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1597 *spec->stream_analog_capture;
1598 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1599 spec->adc_nids[0];
1600 if (!spec->dyn_adc_switch)
1601 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
1602 spec->num_adc_nids;
1603 }
1604 codec->num_pcms++;
1605 info++;
1606 }
1607
1608 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1609 snprintf(spec->stream_name_digital,
1610 sizeof(spec->stream_name_digital),
1611 "%s Digital", codec->chip_name);
1612 info->name = spec->stream_name_digital;
1613 info->pcm_type = HDA_PCM_TYPE_SPDIF;
1614 if (spec->multiout.dig_out_nid) {
1615 if (!spec->stream_digital_playback)
1616 spec->stream_digital_playback =
1617 &via_pcm_digital_playback;
1618 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1619 *spec->stream_digital_playback;
1620 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1621 spec->multiout.dig_out_nid;
1622 }
1623 if (spec->dig_in_nid) {
1624 if (!spec->stream_digital_capture)
1625 spec->stream_digital_capture =
1626 &via_pcm_digital_capture;
1627 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1628 *spec->stream_digital_capture;
1629 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1630 spec->dig_in_nid;
1631 }
1632 codec->num_pcms++;
1633 info++;
1634 }
1635
1636 if (spec->hp_dac_nid) {
1637 snprintf(spec->stream_name_hp, sizeof(spec->stream_name_hp),
1638 "%s HP", codec->chip_name);
1639 info->name = spec->stream_name_hp;
1640 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = via_pcm_hp_playback;
1641 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1642 spec->hp_dac_nid;
1643 codec->num_pcms++;
1644 info++;
1645 }
1646 return 0;
1647} 457}
1648 458
1649static void via_free(struct hda_codec *codec) 459static void via_free(struct hda_codec *codec)
@@ -1653,79 +463,22 @@ static void via_free(struct hda_codec *codec)
1653 if (!spec) 463 if (!spec)
1654 return; 464 return;
1655 465
1656 via_free_kctls(codec); 466 vt1708_stop_hp_work(codec);
1657 vt1708_stop_hp_work(spec); 467 snd_hda_gen_spec_free(&spec->gen);
1658 kfree(spec->bind_cap_vol);
1659 kfree(spec->bind_cap_sw);
1660 snd_hda_gen_free(&spec->gen);
1661 kfree(spec); 468 kfree(spec);
1662} 469}
1663 470
1664/* mute/unmute outputs */
1665static void toggle_output_mutes(struct hda_codec *codec, int num_pins,
1666 hda_nid_t *pins, bool mute)
1667{
1668 int i;
1669 for (i = 0; i < num_pins; i++) {
1670 unsigned int parm = snd_hda_codec_read(codec, pins[i], 0,
1671 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1672 if (parm & AC_PINCTL_IN_EN)
1673 continue;
1674 if (mute)
1675 parm &= ~AC_PINCTL_OUT_EN;
1676 else
1677 parm |= AC_PINCTL_OUT_EN;
1678 snd_hda_set_pin_ctl(codec, pins[i], parm);
1679 }
1680}
1681
1682/* mute internal speaker if line-out is plugged */
1683static void via_line_automute(struct hda_codec *codec, int present)
1684{
1685 struct via_spec *spec = codec->spec;
1686
1687 if (!spec->autocfg.speaker_outs)
1688 return;
1689 if (!present)
1690 present = snd_hda_jack_detect(codec,
1691 spec->autocfg.line_out_pins[0]);
1692 toggle_output_mutes(codec, spec->autocfg.speaker_outs,
1693 spec->autocfg.speaker_pins,
1694 present);
1695}
1696
1697/* mute internal speaker if HP is plugged */
1698static void via_hp_automute(struct hda_codec *codec)
1699{
1700 int present = 0;
1701 int nums;
1702 struct via_spec *spec = codec->spec;
1703
1704 if (!spec->hp_independent_mode && spec->autocfg.hp_pins[0] &&
1705 (spec->codec_type != VT1708 || spec->vt1708_jack_detect) &&
1706 is_jack_detectable(codec, spec->autocfg.hp_pins[0]))
1707 present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
1708
1709 if (spec->smart51_enabled)
1710 nums = spec->autocfg.line_outs + spec->smart51_nums;
1711 else
1712 nums = spec->autocfg.line_outs;
1713 toggle_output_mutes(codec, nums, spec->autocfg.line_out_pins, present);
1714
1715 via_line_automute(codec, present);
1716}
1717
1718#ifdef CONFIG_PM 471#ifdef CONFIG_PM
1719static int via_suspend(struct hda_codec *codec) 472static int via_suspend(struct hda_codec *codec)
1720{ 473{
1721 struct via_spec *spec = codec->spec; 474 struct via_spec *spec = codec->spec;
1722 vt1708_stop_hp_work(spec); 475 vt1708_stop_hp_work(codec);
1723 476
1724 if (spec->codec_type == VT1802) { 477 if (spec->codec_type == VT1802) {
1725 /* Fix pop noise on headphones */ 478 /* Fix pop noise on headphones */
1726 int i; 479 int i;
1727 for (i = 0; i < spec->autocfg.hp_outs; i++) 480 for (i = 0; i < spec->gen.autocfg.hp_outs; i++)
1728 snd_hda_set_pin_ctl(codec, spec->autocfg.hp_pins[i], 0); 481 snd_hda_set_pin_ctl(codec, spec->gen.autocfg.hp_pins[i], 0);
1729 } 482 }
1730 483
1731 return 0; 484 return 0;
@@ -1736,7 +489,10 @@ static int via_suspend(struct hda_codec *codec)
1736static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid) 489static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1737{ 490{
1738 struct via_spec *spec = codec->spec; 491 struct via_spec *spec = codec->spec;
1739 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid); 492 set_widgets_power_state(codec);
493 analog_low_current_mode(codec);
494 vt1708_update_hp_work(codec);
495 return snd_hda_check_amp_list_power(codec, &spec->gen.loopback, nid);
1740} 496}
1741#endif 497#endif
1742 498
@@ -1747,7 +503,7 @@ static int via_init(struct hda_codec *codec);
1747 503
1748static const struct hda_codec_ops via_patch_ops = { 504static const struct hda_codec_ops via_patch_ops = {
1749 .build_controls = via_build_controls, 505 .build_controls = via_build_controls,
1750 .build_pcms = via_build_pcms, 506 .build_pcms = snd_hda_gen_build_pcms,
1751 .init = via_init, 507 .init = via_init,
1752 .free = via_free, 508 .free = via_free,
1753 .unsol_event = snd_hda_jack_unsol_event, 509 .unsol_event = snd_hda_jack_unsol_event,
@@ -1757,840 +513,12 @@ static const struct hda_codec_ops via_patch_ops = {
1757#endif 513#endif
1758}; 514};
1759 515
1760static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
1761{
1762 struct via_spec *spec = codec->spec;
1763 int i;
1764
1765 for (i = 0; i < spec->multiout.num_dacs; i++) {
1766 if (spec->multiout.dac_nids[i] == dac)
1767 return false;
1768 }
1769 if (spec->hp_dac_nid == dac)
1770 return false;
1771 return true;
1772}
1773
1774static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
1775 hda_nid_t target_dac, int with_aa_mix,
1776 struct nid_path *path, int depth)
1777{
1778 struct via_spec *spec = codec->spec;
1779 hda_nid_t conn[8];
1780 int i, nums;
1781
1782 if (nid == spec->aa_mix_nid) {
1783 if (!with_aa_mix)
1784 return false;
1785 with_aa_mix = 2; /* mark aa-mix is included */
1786 }
1787
1788 nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
1789 for (i = 0; i < nums; i++) {
1790 if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
1791 continue;
1792 if (conn[i] == target_dac || is_empty_dac(codec, conn[i])) {
1793 /* aa-mix is requested but not included? */
1794 if (!(spec->aa_mix_nid && with_aa_mix == 1))
1795 goto found;
1796 }
1797 }
1798 if (depth >= MAX_NID_PATH_DEPTH)
1799 return false;
1800 for (i = 0; i < nums; i++) {
1801 unsigned int type;
1802 type = get_wcaps_type(get_wcaps(codec, conn[i]));
1803 if (type == AC_WID_AUD_OUT)
1804 continue;
1805 if (__parse_output_path(codec, conn[i], target_dac,
1806 with_aa_mix, path, depth + 1))
1807 goto found;
1808 }
1809 return false;
1810
1811 found:
1812 path->path[path->depth] = conn[i];
1813 path->idx[path->depth] = i;
1814 if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
1815 path->multi[path->depth] = 1;
1816 path->depth++;
1817 return true;
1818}
1819
1820static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
1821 hda_nid_t target_dac, int with_aa_mix,
1822 struct nid_path *path)
1823{
1824 if (__parse_output_path(codec, nid, target_dac, with_aa_mix, path, 1)) {
1825 path->path[path->depth] = nid;
1826 path->depth++;
1827 snd_printdd("output-path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
1828 path->depth, path->path[0], path->path[1],
1829 path->path[2], path->path[3], path->path[4]);
1830 return true;
1831 }
1832 return false;
1833}
1834
1835static int via_auto_fill_dac_nids(struct hda_codec *codec)
1836{
1837 struct via_spec *spec = codec->spec;
1838 const struct auto_pin_cfg *cfg = &spec->autocfg;
1839 int i;
1840 hda_nid_t nid;
1841
1842 spec->multiout.num_dacs = 0;
1843 spec->multiout.dac_nids = spec->private_dac_nids;
1844 for (i = 0; i < cfg->line_outs; i++) {
1845 hda_nid_t dac = 0;
1846 nid = cfg->line_out_pins[i];
1847 if (!nid)
1848 continue;
1849 if (parse_output_path(codec, nid, 0, 0, &spec->out_path[i]))
1850 dac = spec->out_path[i].path[0];
1851 if (!i && parse_output_path(codec, nid, dac, 1,
1852 &spec->out_mix_path))
1853 dac = spec->out_mix_path.path[0];
1854 if (dac)
1855 spec->private_dac_nids[spec->multiout.num_dacs++] = dac;
1856 }
1857 if (!spec->out_path[0].depth && spec->out_mix_path.depth) {
1858 spec->out_path[0] = spec->out_mix_path;
1859 spec->out_mix_path.depth = 0;
1860 }
1861 return 0;
1862}
1863
1864static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
1865 int chs, bool check_dac, struct nid_path *path)
1866{
1867 struct via_spec *spec = codec->spec;
1868 char name[32];
1869 hda_nid_t dac, pin, sel, nid;
1870 int err;
1871
1872 dac = check_dac ? path->path[0] : 0;
1873 pin = path->path[path->depth - 1];
1874 sel = path->depth > 1 ? path->path[1] : 0;
1875
1876 if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1877 nid = dac;
1878 else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1879 nid = pin;
1880 else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1881 nid = sel;
1882 else
1883 nid = 0;
1884 if (nid) {
1885 sprintf(name, "%s Playback Volume", pfx);
1886 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1887 HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1888 if (err < 0)
1889 return err;
1890 path->vol_ctl = nid;
1891 }
1892
1893 if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
1894 nid = dac;
1895 else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
1896 nid = pin;
1897 else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
1898 nid = sel;
1899 else
1900 nid = 0;
1901 if (nid) {
1902 sprintf(name, "%s Playback Switch", pfx);
1903 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1904 HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1905 if (err < 0)
1906 return err;
1907 path->mute_ctl = nid;
1908 }
1909 return 0;
1910}
1911
1912static void mangle_smart51(struct hda_codec *codec)
1913{
1914 struct via_spec *spec = codec->spec;
1915 struct auto_pin_cfg *cfg = &spec->autocfg;
1916 struct auto_pin_cfg_item *ins = cfg->inputs;
1917 int i, j, nums, attr;
1918 int pins[AUTO_CFG_MAX_INS];
1919
1920 for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
1921 nums = 0;
1922 for (i = 0; i < cfg->num_inputs; i++) {
1923 unsigned int def;
1924 if (ins[i].type > AUTO_PIN_LINE_IN)
1925 continue;
1926 def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
1927 if (snd_hda_get_input_pin_attr(def) != attr)
1928 continue;
1929 for (j = 0; j < nums; j++)
1930 if (ins[pins[j]].type < ins[i].type) {
1931 memmove(pins + j + 1, pins + j,
1932 (nums - j) * sizeof(int));
1933 break;
1934 }
1935 pins[j] = i;
1936 nums++;
1937 }
1938 if (cfg->line_outs + nums < 3)
1939 continue;
1940 for (i = 0; i < nums; i++) {
1941 hda_nid_t pin = ins[pins[i]].pin;
1942 spec->smart51_pins[spec->smart51_nums++] = pin;
1943 cfg->line_out_pins[cfg->line_outs++] = pin;
1944 if (cfg->line_outs == 3)
1945 break;
1946 }
1947 return;
1948 }
1949}
1950
1951static void copy_path_mixer_ctls(struct nid_path *dst, struct nid_path *src)
1952{
1953 dst->vol_ctl = src->vol_ctl;
1954 dst->mute_ctl = src->mute_ctl;
1955}
1956
1957/* add playback controls from the parsed DAC table */
1958static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
1959{
1960 struct via_spec *spec = codec->spec;
1961 struct auto_pin_cfg *cfg = &spec->autocfg;
1962 struct nid_path *path;
1963 static const char * const chname[4] = {
1964 "Front", "Surround", NULL /* "CLFE" */, "Side"
1965 };
1966 int i, idx, err;
1967 int old_line_outs;
1968
1969 /* check smart51 */
1970 old_line_outs = cfg->line_outs;
1971 if (cfg->line_outs == 1)
1972 mangle_smart51(codec);
1973
1974 err = via_auto_fill_dac_nids(codec);
1975 if (err < 0)
1976 return err;
1977
1978 if (spec->multiout.num_dacs < 3) {
1979 spec->smart51_nums = 0;
1980 cfg->line_outs = old_line_outs;
1981 }
1982 for (i = 0; i < cfg->line_outs; i++) {
1983 hda_nid_t pin, dac;
1984 pin = cfg->line_out_pins[i];
1985 dac = spec->multiout.dac_nids[i];
1986 if (!pin || !dac)
1987 continue;
1988 path = spec->out_path + i;
1989 if (i == HDA_CLFE) {
1990 err = create_ch_ctls(codec, "Center", 1, true, path);
1991 if (err < 0)
1992 return err;
1993 err = create_ch_ctls(codec, "LFE", 2, true, path);
1994 if (err < 0)
1995 return err;
1996 } else {
1997 const char *pfx = chname[i];
1998 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
1999 cfg->line_outs <= 2)
2000 pfx = i ? "Bass Speaker" : "Speaker";
2001 err = create_ch_ctls(codec, pfx, 3, true, path);
2002 if (err < 0)
2003 return err;
2004 }
2005 if (path != spec->out_path + i)
2006 copy_path_mixer_ctls(&spec->out_path[i], path);
2007 if (path == spec->out_path && spec->out_mix_path.depth)
2008 copy_path_mixer_ctls(&spec->out_mix_path, path);
2009 }
2010
2011 idx = get_connection_index(codec, spec->aa_mix_nid,
2012 spec->multiout.dac_nids[0]);
2013 if (idx >= 0) {
2014 /* add control to mixer */
2015 const char *name;
2016 name = spec->out_mix_path.depth ?
2017 "PCM Loopback Playback Volume" : "PCM Playback Volume";
2018 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2019 HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
2020 idx, HDA_INPUT));
2021 if (err < 0)
2022 return err;
2023 name = spec->out_mix_path.depth ?
2024 "PCM Loopback Playback Switch" : "PCM Playback Switch";
2025 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2026 HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
2027 idx, HDA_INPUT));
2028 if (err < 0)
2029 return err;
2030 }
2031
2032 cfg->line_outs = old_line_outs;
2033
2034 return 0;
2035}
2036
2037static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
2038{
2039 struct via_spec *spec = codec->spec;
2040 struct nid_path *path;
2041 bool check_dac;
2042 int i, err;
2043
2044 if (!pin)
2045 return 0;
2046
2047 if (!parse_output_path(codec, pin, 0, 0, &spec->hp_indep_path)) {
2048 for (i = HDA_SIDE; i >= HDA_CLFE; i--) {
2049 if (i < spec->multiout.num_dacs &&
2050 parse_output_path(codec, pin,
2051 spec->multiout.dac_nids[i], 0,
2052 &spec->hp_indep_path)) {
2053 spec->hp_indep_shared = i;
2054 break;
2055 }
2056 }
2057 }
2058 if (spec->hp_indep_path.depth) {
2059 spec->hp_dac_nid = spec->hp_indep_path.path[0];
2060 if (!spec->hp_indep_shared)
2061 spec->hp_path = spec->hp_indep_path;
2062 }
2063 /* optionally check front-path w/o AA-mix */
2064 if (!spec->hp_path.depth)
2065 parse_output_path(codec, pin,
2066 spec->multiout.dac_nids[HDA_FRONT], 0,
2067 &spec->hp_path);
2068
2069 if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
2070 1, &spec->hp_mix_path) && !spec->hp_path.depth)
2071 return 0;
2072
2073 if (spec->hp_path.depth) {
2074 path = &spec->hp_path;
2075 check_dac = true;
2076 } else {
2077 path = &spec->hp_mix_path;
2078 check_dac = false;
2079 }
2080 err = create_ch_ctls(codec, "Headphone", 3, check_dac, path);
2081 if (err < 0)
2082 return err;
2083 if (check_dac)
2084 copy_path_mixer_ctls(&spec->hp_mix_path, path);
2085 else
2086 copy_path_mixer_ctls(&spec->hp_path, path);
2087 if (spec->hp_indep_path.depth)
2088 copy_path_mixer_ctls(&spec->hp_indep_path, path);
2089 return 0;
2090}
2091
2092static int via_auto_create_speaker_ctls(struct hda_codec *codec)
2093{
2094 struct via_spec *spec = codec->spec;
2095 struct nid_path *path;
2096 bool check_dac;
2097 hda_nid_t pin, dac = 0;
2098 int err;
2099
2100 pin = spec->autocfg.speaker_pins[0];
2101 if (!spec->autocfg.speaker_outs || !pin)
2102 return 0;
2103
2104 if (parse_output_path(codec, pin, 0, 0, &spec->speaker_path))
2105 dac = spec->speaker_path.path[0];
2106 if (!dac)
2107 parse_output_path(codec, pin,
2108 spec->multiout.dac_nids[HDA_FRONT], 0,
2109 &spec->speaker_path);
2110 if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
2111 1, &spec->speaker_mix_path) && !dac)
2112 return 0;
2113
2114 /* no AA-path for front? */
2115 if (!spec->out_mix_path.depth && spec->speaker_mix_path.depth)
2116 dac = 0;
2117
2118 spec->speaker_dac_nid = dac;
2119 spec->multiout.extra_out_nid[0] = dac;
2120 if (dac) {
2121 path = &spec->speaker_path;
2122 check_dac = true;
2123 } else {
2124 path = &spec->speaker_mix_path;
2125 check_dac = false;
2126 }
2127 err = create_ch_ctls(codec, "Speaker", 3, check_dac, path);
2128 if (err < 0)
2129 return err;
2130 if (check_dac)
2131 copy_path_mixer_ctls(&spec->speaker_mix_path, path);
2132 else
2133 copy_path_mixer_ctls(&spec->speaker_path, path);
2134 return 0;
2135}
2136
2137#define via_aamix_ctl_info via_pin_power_ctl_info
2138
2139static int via_aamix_ctl_get(struct snd_kcontrol *kcontrol,
2140 struct snd_ctl_elem_value *ucontrol)
2141{
2142 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2143 struct via_spec *spec = codec->spec;
2144 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2145 return 0;
2146}
2147
2148static void update_aamix_paths(struct hda_codec *codec, int do_mix,
2149 struct nid_path *nomix, struct nid_path *mix)
2150{
2151 if (do_mix) {
2152 activate_output_path(codec, nomix, false, false);
2153 activate_output_path(codec, mix, true, false);
2154 } else {
2155 activate_output_path(codec, mix, false, false);
2156 activate_output_path(codec, nomix, true, false);
2157 }
2158}
2159
2160static int via_aamix_ctl_put(struct snd_kcontrol *kcontrol,
2161 struct snd_ctl_elem_value *ucontrol)
2162{
2163 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2164 struct via_spec *spec = codec->spec;
2165 unsigned int val = ucontrol->value.enumerated.item[0];
2166
2167 if (val == spec->aamix_mode)
2168 return 0;
2169 spec->aamix_mode = val;
2170 /* update front path */
2171 update_aamix_paths(codec, val, &spec->out_path[0], &spec->out_mix_path);
2172 /* update HP path */
2173 if (!spec->hp_independent_mode) {
2174 update_aamix_paths(codec, val, &spec->hp_path,
2175 &spec->hp_mix_path);
2176 }
2177 /* update speaker path */
2178 update_aamix_paths(codec, val, &spec->speaker_path,
2179 &spec->speaker_mix_path);
2180 return 1;
2181}
2182
2183static const struct snd_kcontrol_new via_aamix_ctl_enum = {
2184 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2185 .name = "Loopback Mixing",
2186 .info = via_aamix_ctl_info,
2187 .get = via_aamix_ctl_get,
2188 .put = via_aamix_ctl_put,
2189};
2190
2191static int via_auto_create_loopback_switch(struct hda_codec *codec)
2192{
2193 struct via_spec *spec = codec->spec;
2194
2195 if (!spec->aa_mix_nid)
2196 return 0; /* no loopback switching available */
2197 if (!(spec->out_mix_path.depth || spec->hp_mix_path.depth ||
2198 spec->speaker_path.depth))
2199 return 0; /* no loopback switching available */
2200 if (!via_clone_control(spec, &via_aamix_ctl_enum))
2201 return -ENOMEM;
2202 return 0;
2203}
2204
2205/* look for ADCs */
2206static int via_fill_adcs(struct hda_codec *codec)
2207{
2208 struct via_spec *spec = codec->spec;
2209 hda_nid_t nid = codec->start_nid;
2210 int i;
2211
2212 for (i = 0; i < codec->num_nodes; i++, nid++) {
2213 unsigned int wcaps = get_wcaps(codec, nid);
2214 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2215 continue;
2216 if (wcaps & AC_WCAP_DIGITAL)
2217 continue;
2218 if (!(wcaps & AC_WCAP_CONN_LIST))
2219 continue;
2220 if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
2221 return -ENOMEM;
2222 spec->adc_nids[spec->num_adc_nids++] = nid;
2223 }
2224 return 0;
2225}
2226 516
2227/* input-src control */ 517static const struct hda_verb vt1708_init_verbs[] = {
2228static int via_mux_enum_info(struct snd_kcontrol *kcontrol, 518 /* power down jack detect function */
2229 struct snd_ctl_elem_info *uinfo) 519 {0x1, 0xf81, 0x1},
2230{ 520 { }
2231 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2232 struct via_spec *spec = codec->spec;
2233
2234 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2235 uinfo->count = 1;
2236 uinfo->value.enumerated.items = spec->num_inputs;
2237 if (uinfo->value.enumerated.item >= spec->num_inputs)
2238 uinfo->value.enumerated.item = spec->num_inputs - 1;
2239 strcpy(uinfo->value.enumerated.name,
2240 spec->inputs[uinfo->value.enumerated.item].label);
2241 return 0;
2242}
2243
2244static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
2245 struct snd_ctl_elem_value *ucontrol)
2246{
2247 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2248 struct via_spec *spec = codec->spec;
2249 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2250
2251 ucontrol->value.enumerated.item[0] = spec->cur_mux[idx];
2252 return 0;
2253}
2254
2255static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
2256 struct snd_ctl_elem_value *ucontrol)
2257{
2258 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2259 struct via_spec *spec = codec->spec;
2260 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2261 hda_nid_t mux;
2262 int cur;
2263
2264 cur = ucontrol->value.enumerated.item[0];
2265 if (cur < 0 || cur >= spec->num_inputs)
2266 return -EINVAL;
2267 if (spec->cur_mux[idx] == cur)
2268 return 0;
2269 spec->cur_mux[idx] = cur;
2270 if (spec->dyn_adc_switch) {
2271 int adc_idx = spec->inputs[cur].adc_idx;
2272 mux = spec->mux_nids[adc_idx];
2273 via_dyn_adc_pcm_resetup(codec, cur);
2274 } else {
2275 mux = spec->mux_nids[idx];
2276 if (snd_BUG_ON(!mux))
2277 return -EINVAL;
2278 }
2279
2280 if (mux) {
2281 /* switch to D0 beofre change index */
2282 update_power_state(codec, mux, AC_PWRST_D0);
2283 snd_hda_codec_write(codec, mux, 0,
2284 AC_VERB_SET_CONNECT_SEL,
2285 spec->inputs[cur].mux_idx);
2286 }
2287
2288 /* update jack power state */
2289 set_widgets_power_state(codec);
2290 return 0;
2291}
2292
2293static const struct snd_kcontrol_new via_input_src_ctl = {
2294 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2295 /* The multiple "Capture Source" controls confuse alsamixer
2296 * So call somewhat different..
2297 */
2298 /* .name = "Capture Source", */
2299 .name = "Input Source",
2300 .info = via_mux_enum_info,
2301 .get = via_mux_enum_get,
2302 .put = via_mux_enum_put,
2303}; 521};
2304
2305static int create_input_src_ctls(struct hda_codec *codec, int count)
2306{
2307 struct via_spec *spec = codec->spec;
2308 struct snd_kcontrol_new *knew;
2309
2310 if (spec->num_inputs <= 1 || !count)
2311 return 0; /* no need for single src */
2312
2313 knew = via_clone_control(spec, &via_input_src_ctl);
2314 if (!knew)
2315 return -ENOMEM;
2316 knew->count = count;
2317 return 0;
2318}
2319
2320/* add the powersave loopback-list entry */
2321static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
2322{
2323 struct hda_amp_list *list;
2324
2325 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2326 return;
2327 list = spec->loopback_list + spec->num_loopbacks;
2328 list->nid = mix;
2329 list->dir = HDA_INPUT;
2330 list->idx = idx;
2331 spec->num_loopbacks++;
2332 spec->loopback.amplist = spec->loopback_list;
2333}
2334
2335static bool is_reachable_nid(struct hda_codec *codec, hda_nid_t src,
2336 hda_nid_t dst)
2337{
2338 return snd_hda_get_conn_index(codec, src, dst, 1) >= 0;
2339}
2340
2341/* add the input-route to the given pin */
2342static bool add_input_route(struct hda_codec *codec, hda_nid_t pin)
2343{
2344 struct via_spec *spec = codec->spec;
2345 int c, idx;
2346
2347 spec->inputs[spec->num_inputs].adc_idx = -1;
2348 spec->inputs[spec->num_inputs].pin = pin;
2349 for (c = 0; c < spec->num_adc_nids; c++) {
2350 if (spec->mux_nids[c]) {
2351 idx = get_connection_index(codec, spec->mux_nids[c],
2352 pin);
2353 if (idx < 0)
2354 continue;
2355 spec->inputs[spec->num_inputs].mux_idx = idx;
2356 } else {
2357 if (!is_reachable_nid(codec, spec->adc_nids[c], pin))
2358 continue;
2359 }
2360 spec->inputs[spec->num_inputs].adc_idx = c;
2361 /* Can primary ADC satisfy all inputs? */
2362 if (!spec->dyn_adc_switch &&
2363 spec->num_inputs > 0 && spec->inputs[0].adc_idx != c) {
2364 snd_printd(KERN_INFO
2365 "via: dynamic ADC switching enabled\n");
2366 spec->dyn_adc_switch = 1;
2367 }
2368 return true;
2369 }
2370 return false;
2371}
2372
2373static int get_mux_nids(struct hda_codec *codec);
2374
2375/* parse input-routes; fill ADCs, MUXs and input-src entries */
2376static int parse_analog_inputs(struct hda_codec *codec)
2377{
2378 struct via_spec *spec = codec->spec;
2379 const struct auto_pin_cfg *cfg = &spec->autocfg;
2380 int i, err;
2381
2382 err = via_fill_adcs(codec);
2383 if (err < 0)
2384 return err;
2385 err = get_mux_nids(codec);
2386 if (err < 0)
2387 return err;
2388
2389 /* fill all input-routes */
2390 for (i = 0; i < cfg->num_inputs; i++) {
2391 if (add_input_route(codec, cfg->inputs[i].pin))
2392 spec->inputs[spec->num_inputs++].label =
2393 hda_get_autocfg_input_label(codec, cfg, i);
2394 }
2395
2396 /* check for internal loopback recording */
2397 if (spec->aa_mix_nid &&
2398 add_input_route(codec, spec->aa_mix_nid))
2399 spec->inputs[spec->num_inputs++].label = "Stereo Mixer";
2400
2401 return 0;
2402}
2403
2404/* create analog-loopback volume/switch controls */
2405static int create_loopback_ctls(struct hda_codec *codec)
2406{
2407 struct via_spec *spec = codec->spec;
2408 const struct auto_pin_cfg *cfg = &spec->autocfg;
2409 const char *prev_label = NULL;
2410 int type_idx = 0;
2411 int i, j, err, idx;
2412
2413 if (!spec->aa_mix_nid)
2414 return 0;
2415
2416 for (i = 0; i < cfg->num_inputs; i++) {
2417 hda_nid_t pin = cfg->inputs[i].pin;
2418 const char *label = hda_get_autocfg_input_label(codec, cfg, i);
2419
2420 if (prev_label && !strcmp(label, prev_label))
2421 type_idx++;
2422 else
2423 type_idx = 0;
2424 prev_label = label;
2425 idx = get_connection_index(codec, spec->aa_mix_nid, pin);
2426 if (idx >= 0) {
2427 err = via_new_analog_input(spec, label, type_idx,
2428 idx, spec->aa_mix_nid);
2429 if (err < 0)
2430 return err;
2431 add_loopback_list(spec, spec->aa_mix_nid, idx);
2432 }
2433
2434 /* remember the label for smart51 control */
2435 for (j = 0; j < spec->smart51_nums; j++) {
2436 if (spec->smart51_pins[j] == pin) {
2437 spec->smart51_idxs[j] = idx;
2438 spec->smart51_labels[j] = label;
2439 break;
2440 }
2441 }
2442 }
2443 return 0;
2444}
2445
2446/* create mic-boost controls (if present) */
2447static int create_mic_boost_ctls(struct hda_codec *codec)
2448{
2449 struct via_spec *spec = codec->spec;
2450 const struct auto_pin_cfg *cfg = &spec->autocfg;
2451 const char *prev_label = NULL;
2452 int type_idx = 0;
2453 int i, err;
2454
2455 for (i = 0; i < cfg->num_inputs; i++) {
2456 hda_nid_t pin = cfg->inputs[i].pin;
2457 unsigned int caps;
2458 const char *label;
2459 char name[32];
2460
2461 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2462 continue;
2463 caps = query_amp_caps(codec, pin, HDA_INPUT);
2464 if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
2465 continue;
2466 label = hda_get_autocfg_input_label(codec, cfg, i);
2467 if (prev_label && !strcmp(label, prev_label))
2468 type_idx++;
2469 else
2470 type_idx = 0;
2471 prev_label = label;
2472 snprintf(name, sizeof(name), "%s Boost Volume", label);
2473 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
2474 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
2475 if (err < 0)
2476 return err;
2477 }
2478 return 0;
2479}
2480
2481/* create capture and input-src controls for multiple streams */
2482static int create_multi_adc_ctls(struct hda_codec *codec)
2483{
2484 struct via_spec *spec = codec->spec;
2485 int i, err;
2486
2487 /* create capture mixer elements */
2488 for (i = 0; i < spec->num_adc_nids; i++) {
2489 hda_nid_t adc = spec->adc_nids[i];
2490 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
2491 "Capture Volume", i,
2492 HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2493 HDA_INPUT));
2494 if (err < 0)
2495 return err;
2496 err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2497 "Capture Switch", i,
2498 HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2499 HDA_INPUT));
2500 if (err < 0)
2501 return err;
2502 }
2503
2504 /* input-source control */
2505 for (i = 0; i < spec->num_adc_nids; i++)
2506 if (!spec->mux_nids[i])
2507 break;
2508 err = create_input_src_ctls(codec, i);
2509 if (err < 0)
2510 return err;
2511 return 0;
2512}
2513
2514/* bind capture volume/switch */
2515static struct snd_kcontrol_new via_bind_cap_vol_ctl =
2516 HDA_BIND_VOL("Capture Volume", 0);
2517static struct snd_kcontrol_new via_bind_cap_sw_ctl =
2518 HDA_BIND_SW("Capture Switch", 0);
2519
2520static int init_bind_ctl(struct via_spec *spec, struct hda_bind_ctls **ctl_ret,
2521 struct hda_ctl_ops *ops)
2522{
2523 struct hda_bind_ctls *ctl;
2524 int i;
2525
2526 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * 4, GFP_KERNEL);
2527 if (!ctl)
2528 return -ENOMEM;
2529 ctl->ops = ops;
2530 for (i = 0; i < spec->num_adc_nids; i++)
2531 ctl->values[i] =
2532 HDA_COMPOSE_AMP_VAL(spec->adc_nids[i], 3, 0, HDA_INPUT);
2533 *ctl_ret = ctl;
2534 return 0;
2535}
2536
2537/* create capture and input-src controls for dynamic ADC-switch case */
2538static int create_dyn_adc_ctls(struct hda_codec *codec)
2539{
2540 struct via_spec *spec = codec->spec;
2541 struct snd_kcontrol_new *knew;
2542 int err;
2543
2544 /* set up the bind capture ctls */
2545 err = init_bind_ctl(spec, &spec->bind_cap_vol, &snd_hda_bind_vol);
2546 if (err < 0)
2547 return err;
2548 err = init_bind_ctl(spec, &spec->bind_cap_sw, &snd_hda_bind_sw);
2549 if (err < 0)
2550 return err;
2551
2552 /* create capture mixer elements */
2553 knew = via_clone_control(spec, &via_bind_cap_vol_ctl);
2554 if (!knew)
2555 return -ENOMEM;
2556 knew->private_value = (long)spec->bind_cap_vol;
2557
2558 knew = via_clone_control(spec, &via_bind_cap_sw_ctl);
2559 if (!knew)
2560 return -ENOMEM;
2561 knew->private_value = (long)spec->bind_cap_sw;
2562
2563 /* input-source control */
2564 err = create_input_src_ctls(codec, 1);
2565 if (err < 0)
2566 return err;
2567 return 0;
2568}
2569
2570/* parse and create capture-related stuff */
2571static int via_auto_create_analog_input_ctls(struct hda_codec *codec)
2572{
2573 struct via_spec *spec = codec->spec;
2574 int err;
2575
2576 err = parse_analog_inputs(codec);
2577 if (err < 0)
2578 return err;
2579 if (spec->dyn_adc_switch)
2580 err = create_dyn_adc_ctls(codec);
2581 else
2582 err = create_multi_adc_ctls(codec);
2583 if (err < 0)
2584 return err;
2585 err = create_loopback_ctls(codec);
2586 if (err < 0)
2587 return err;
2588 err = create_mic_boost_ctls(codec);
2589 if (err < 0)
2590 return err;
2591 return 0;
2592}
2593
2594static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid) 522static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
2595{ 523{
2596 unsigned int def_conf; 524 unsigned int def_conf;
@@ -2633,102 +561,32 @@ static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
2633 if (spec->vt1708_jack_detect == val) 561 if (spec->vt1708_jack_detect == val)
2634 return 0; 562 return 0;
2635 spec->vt1708_jack_detect = val; 563 spec->vt1708_jack_detect = val;
2636 if (spec->vt1708_jack_detect && 564 vt1708_update_hp_work(codec);
2637 snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") != 1) {
2638 mute_aa_path(codec, 1);
2639 notify_aa_path_ctls(codec);
2640 }
2641 via_hp_automute(codec);
2642 vt1708_update_hp_work(spec);
2643 return 1; 565 return 1;
2644} 566}
2645 567
2646static const struct snd_kcontrol_new vt1708_jack_detect_ctl = { 568static const struct snd_kcontrol_new vt1708_jack_detect_ctl[] = {
569 {
2647 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 570 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2648 .name = "Jack Detect", 571 .name = "Jack Detect",
2649 .count = 1, 572 .count = 1,
2650 .info = snd_ctl_boolean_mono_info, 573 .info = snd_ctl_boolean_mono_info,
2651 .get = vt1708_jack_detect_get, 574 .get = vt1708_jack_detect_get,
2652 .put = vt1708_jack_detect_put, 575 .put = vt1708_jack_detect_put,
576 },
577 {} /* terminator */
2653}; 578};
2654 579
2655static void fill_dig_outs(struct hda_codec *codec); 580static void via_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *tbl)
2656static void fill_dig_in(struct hda_codec *codec);
2657
2658static int via_parse_auto_config(struct hda_codec *codec)
2659{ 581{
2660 struct via_spec *spec = codec->spec; 582 set_widgets_power_state(codec);
2661 int err; 583 snd_hda_gen_hp_automute(codec, tbl);
2662
2663 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2664 if (err < 0)
2665 return err;
2666 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2667 return -EINVAL;
2668
2669 err = via_auto_create_multi_out_ctls(codec);
2670 if (err < 0)
2671 return err;
2672 err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
2673 if (err < 0)
2674 return err;
2675 err = via_auto_create_speaker_ctls(codec);
2676 if (err < 0)
2677 return err;
2678 err = via_auto_create_loopback_switch(codec);
2679 if (err < 0)
2680 return err;
2681 err = via_auto_create_analog_input_ctls(codec);
2682 if (err < 0)
2683 return err;
2684
2685 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2686
2687 fill_dig_outs(codec);
2688 fill_dig_in(codec);
2689
2690 if (spec->kctls.list)
2691 spec->mixers[spec->num_mixers++] = spec->kctls.list;
2692
2693
2694 if (spec->hp_dac_nid && spec->hp_mix_path.depth) {
2695 err = via_hp_build(codec);
2696 if (err < 0)
2697 return err;
2698 }
2699
2700 err = via_smart51_build(codec);
2701 if (err < 0)
2702 return err;
2703
2704 /* assign slave outs */
2705 if (spec->slave_dig_outs[0])
2706 codec->slave_dig_outs = spec->slave_dig_outs;
2707
2708 return 1;
2709}
2710
2711static void via_auto_init_dig_outs(struct hda_codec *codec)
2712{
2713 struct via_spec *spec = codec->spec;
2714 if (spec->multiout.dig_out_nid)
2715 init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
2716 if (spec->slave_dig_outs[0])
2717 init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
2718}
2719
2720static void via_auto_init_dig_in(struct hda_codec *codec)
2721{
2722 struct via_spec *spec = codec->spec;
2723 if (!spec->dig_in_nid)
2724 return;
2725 snd_hda_set_pin_ctl(codec, spec->autocfg.dig_in_pin, PIN_IN);
2726} 584}
2727 585
2728static void via_jack_output_event(struct hda_codec *codec, struct hda_jack_tbl *tbl) 586static void via_line_automute(struct hda_codec *codec, struct hda_jack_tbl *tbl)
2729{ 587{
2730 set_widgets_power_state(codec); 588 set_widgets_power_state(codec);
2731 via_hp_automute(codec); 589 snd_hda_gen_line_automute(codec, tbl);
2732} 590}
2733 591
2734static void via_jack_powerstate_event(struct hda_codec *codec, struct hda_jack_tbl *tbl) 592static void via_jack_powerstate_event(struct hda_codec *codec, struct hda_jack_tbl *tbl)
@@ -2736,41 +594,55 @@ static void via_jack_powerstate_event(struct hda_codec *codec, struct hda_jack_t
2736 set_widgets_power_state(codec); 594 set_widgets_power_state(codec);
2737} 595}
2738 596
2739/* initialize the unsolicited events */ 597#define VIA_JACK_EVENT (HDA_GEN_LAST_EVENT + 1)
2740static void via_auto_init_unsol_event(struct hda_codec *codec) 598
599static void via_set_jack_unsol_events(struct hda_codec *codec)
2741{ 600{
2742 struct via_spec *spec = codec->spec; 601 struct via_spec *spec = codec->spec;
2743 struct auto_pin_cfg *cfg = &spec->autocfg; 602 struct auto_pin_cfg *cfg = &spec->gen.autocfg;
2744 unsigned int ev; 603 hda_nid_t pin;
2745 int i; 604 int i;
2746 hda_jack_callback cb;
2747
2748 if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
2749 snd_hda_jack_detect_enable_callback(codec, cfg->hp_pins[0],
2750 VIA_HP_EVENT | VIA_JACK_EVENT,
2751 via_jack_output_event);
2752 605
606 spec->gen.hp_automute_hook = via_hp_automute;
2753 if (cfg->speaker_pins[0]) 607 if (cfg->speaker_pins[0])
2754 ev = VIA_LINE_EVENT; 608 spec->gen.line_automute_hook = via_line_automute;
2755 else
2756 ev = 0;
2757 cb = ev ? via_jack_output_event : via_jack_powerstate_event;
2758 609
2759 for (i = 0; i < cfg->line_outs; i++) { 610 for (i = 0; i < cfg->line_outs; i++) {
2760 if (cfg->line_out_pins[i] && 611 pin = cfg->line_out_pins[i];
2761 is_jack_detectable(codec, cfg->line_out_pins[i])) 612 if (pin && !snd_hda_jack_tbl_get(codec, pin) &&
2762 snd_hda_jack_detect_enable_callback(codec, cfg->line_out_pins[i], 613 is_jack_detectable(codec, pin))
2763 ev | VIA_JACK_EVENT, cb); 614 snd_hda_jack_detect_enable_callback(codec, pin,
615 VIA_JACK_EVENT,
616 via_jack_powerstate_event);
2764 } 617 }
2765 618
2766 for (i = 0; i < cfg->num_inputs; i++) { 619 for (i = 0; i < cfg->num_inputs; i++) {
2767 if (is_jack_detectable(codec, cfg->inputs[i].pin)) 620 pin = cfg->line_out_pins[i];
2768 snd_hda_jack_detect_enable_callback(codec, cfg->inputs[i].pin, 621 if (pin && !snd_hda_jack_tbl_get(codec, pin) &&
622 is_jack_detectable(codec, pin))
623 snd_hda_jack_detect_enable_callback(codec, pin,
2769 VIA_JACK_EVENT, 624 VIA_JACK_EVENT,
2770 via_jack_powerstate_event); 625 via_jack_powerstate_event);
2771 } 626 }
2772} 627}
2773 628
629static int via_parse_auto_config(struct hda_codec *codec)
630{
631 struct via_spec *spec = codec->spec;
632 int err;
633
634 err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
635 if (err < 0)
636 return err;
637
638 err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
639 if (err < 0)
640 return err;
641
642 via_set_jack_unsol_events(codec);
643 return 0;
644}
645
2774static int via_init(struct hda_codec *codec) 646static int via_init(struct hda_codec *codec)
2775{ 647{
2776 struct via_spec *spec = codec->spec; 648 struct via_spec *spec = codec->spec;
@@ -2783,63 +655,47 @@ static int via_init(struct hda_codec *codec)
2783 set_widgets_power_state(codec); 655 set_widgets_power_state(codec);
2784 __analog_low_current_mode(codec, true); 656 __analog_low_current_mode(codec, true);
2785 657
2786 via_auto_init_multi_out(codec); 658 snd_hda_gen_init(codec);
2787 via_auto_init_hp_out(codec);
2788 via_auto_init_speaker_out(codec);
2789 via_auto_init_analog_input(codec);
2790 via_auto_init_dig_outs(codec);
2791 via_auto_init_dig_in(codec);
2792 659
2793 via_auto_init_unsol_event(codec); 660 vt1708_update_hp_work(codec);
2794
2795 via_hp_automute(codec);
2796 vt1708_update_hp_work(spec);
2797 661
2798 return 0; 662 return 0;
2799} 663}
2800 664
2801static void vt1708_update_hp_jack_state(struct work_struct *work) 665static int vt1708_build_controls(struct hda_codec *codec)
2802{ 666{
2803 struct via_spec *spec = container_of(work, struct via_spec, 667 /* In order not to create "Phantom Jack" controls,
2804 vt1708_hp_work.work); 668 temporary enable jackpoll */
2805 if (spec->codec_type != VT1708) 669 int err;
2806 return; 670 int old_interval = codec->jackpoll_interval;
2807 snd_hda_jack_set_dirty_all(spec->codec); 671 codec->jackpoll_interval = msecs_to_jiffies(100);
2808 /* if jack state toggled */ 672 err = via_build_controls(codec);
2809 if (spec->vt1708_hp_present 673 codec->jackpoll_interval = old_interval;
2810 != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) { 674 return err;
2811 spec->vt1708_hp_present ^= 1;
2812 via_hp_automute(spec->codec);
2813 }
2814 if (spec->vt1708_jack_detect)
2815 schedule_delayed_work(&spec->vt1708_hp_work,
2816 msecs_to_jiffies(100));
2817} 675}
2818 676
2819static int get_mux_nids(struct hda_codec *codec) 677static int vt1708_build_pcms(struct hda_codec *codec)
2820{ 678{
2821 struct via_spec *spec = codec->spec; 679 struct via_spec *spec = codec->spec;
2822 hda_nid_t nid, conn[8]; 680 int i, err;
2823 unsigned int type; 681
2824 int i, n; 682 err = snd_hda_gen_build_pcms(codec);
2825 683 if (err < 0 || codec->vendor_id != 0x11061708)
2826 for (i = 0; i < spec->num_adc_nids; i++) { 684 return err;
2827 nid = spec->adc_nids[i]; 685
2828 while (nid) { 686 /* We got noisy outputs on the right channel on VT1708 when
2829 type = get_wcaps_type(get_wcaps(codec, nid)); 687 * 24bit samples are used. Until any workaround is found,
2830 if (type == AC_WID_PIN) 688 * disable the 24bit format, so far.
2831 break; 689 */
2832 n = snd_hda_get_connections(codec, nid, conn, 690 for (i = 0; i < codec->num_pcms; i++) {
2833 ARRAY_SIZE(conn)); 691 struct hda_pcm *info = &spec->gen.pcm_rec[i];
2834 if (n <= 0) 692 if (!info->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams ||
2835 break; 693 info->pcm_type != HDA_PCM_TYPE_AUDIO)
2836 if (n > 1) { 694 continue;
2837 spec->mux_nids[i] = nid; 695 info->stream[SNDRV_PCM_STREAM_PLAYBACK].formats =
2838 break; 696 SNDRV_PCM_FMTBIT_S16_LE;
2839 }
2840 nid = conn[0];
2841 }
2842 } 697 }
698
2843 return 0; 699 return 0;
2844} 700}
2845 701
@@ -2853,7 +709,15 @@ static int patch_vt1708(struct hda_codec *codec)
2853 if (spec == NULL) 709 if (spec == NULL)
2854 return -ENOMEM; 710 return -ENOMEM;
2855 711
2856 spec->aa_mix_nid = 0x17; 712 spec->gen.mixer_nid = 0x17;
713
714 /* set jackpoll_interval while parsing the codec */
715 codec->jackpoll_interval = msecs_to_jiffies(100);
716 spec->vt1708_jack_detect = 1;
717
718 /* don't support the input jack switching due to lack of unsol event */
719 /* (it may work with polling, though, but it needs testing) */
720 spec->gen.suppress_auto_mic = 1;
2857 721
2858 /* Add HP and CD pin config connect bit re-config action */ 722 /* Add HP and CD pin config connect bit re-config action */
2859 vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID); 723 vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
@@ -2867,18 +731,17 @@ static int patch_vt1708(struct hda_codec *codec)
2867 } 731 }
2868 732
2869 /* add jack detect on/off control */ 733 /* add jack detect on/off control */
2870 if (!via_clone_control(spec, &vt1708_jack_detect_ctl)) 734 spec->mixers[spec->num_mixers++] = vt1708_jack_detect_ctl;
2871 return -ENOMEM;
2872
2873 /* disable 32bit format on VT1708 */
2874 if (codec->vendor_id == 0x11061708)
2875 spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
2876 735
2877 spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs; 736 spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
2878 737
2879 codec->patch_ops = via_patch_ops; 738 codec->patch_ops = via_patch_ops;
739 codec->patch_ops.build_controls = vt1708_build_controls;
740 codec->patch_ops.build_pcms = vt1708_build_pcms;
741
742 /* clear jackpoll_interval again; it's set dynamically */
743 codec->jackpoll_interval = 0;
2880 744
2881 INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
2882 return 0; 745 return 0;
2883} 746}
2884 747
@@ -2892,7 +755,7 @@ static int patch_vt1709(struct hda_codec *codec)
2892 if (spec == NULL) 755 if (spec == NULL)
2893 return -ENOMEM; 756 return -ENOMEM;
2894 757
2895 spec->aa_mix_nid = 0x18; 758 spec->gen.mixer_nid = 0x18;
2896 759
2897 err = via_parse_auto_config(codec); 760 err = via_parse_auto_config(codec);
2898 if (err < 0) { 761 if (err < 0) {
@@ -2936,7 +799,7 @@ static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
2936 /* PW0 (19h), SW1 (18h), AOW1 (11h) */ 799 /* PW0 (19h), SW1 (18h), AOW1 (11h) */
2937 parm = AC_PWRST_D3; 800 parm = AC_PWRST_D3;
2938 set_pin_power_state(codec, 0x19, &parm); 801 set_pin_power_state(codec, 0x19, &parm);
2939 if (spec->smart51_enabled) 802 if (smart51_enabled(codec))
2940 set_pin_power_state(codec, 0x1b, &parm); 803 set_pin_power_state(codec, 0x1b, &parm);
2941 update_power_state(codec, 0x18, parm); 804 update_power_state(codec, 0x18, parm);
2942 update_power_state(codec, 0x11, parm); 805 update_power_state(codec, 0x11, parm);
@@ -2945,7 +808,7 @@ static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
2945 if (is_8ch) { 808 if (is_8ch) {
2946 parm = AC_PWRST_D3; 809 parm = AC_PWRST_D3;
2947 set_pin_power_state(codec, 0x22, &parm); 810 set_pin_power_state(codec, 0x22, &parm);
2948 if (spec->smart51_enabled) 811 if (smart51_enabled(codec))
2949 set_pin_power_state(codec, 0x1a, &parm); 812 set_pin_power_state(codec, 0x1a, &parm);
2950 update_power_state(codec, 0x26, parm); 813 update_power_state(codec, 0x26, parm);
2951 update_power_state(codec, 0x24, parm); 814 update_power_state(codec, 0x24, parm);
@@ -2953,7 +816,7 @@ static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
2953 /* PW7(23h), SW2(27h), AOW2(25h) */ 816 /* PW7(23h), SW2(27h), AOW2(25h) */
2954 parm = AC_PWRST_D3; 817 parm = AC_PWRST_D3;
2955 set_pin_power_state(codec, 0x23, &parm); 818 set_pin_power_state(codec, 0x23, &parm);
2956 if (spec->smart51_enabled) 819 if (smart51_enabled(codec))
2957 set_pin_power_state(codec, 0x1a, &parm); 820 set_pin_power_state(codec, 0x1a, &parm);
2958 update_power_state(codec, 0x27, parm); 821 update_power_state(codec, 0x27, parm);
2959 update_power_state(codec, 0x25, parm); 822 update_power_state(codec, 0x25, parm);
@@ -2973,7 +836,7 @@ static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
2973 if (is_8ch) { 836 if (is_8ch) {
2974 update_power_state(codec, 0x25, parm); 837 update_power_state(codec, 0x25, parm);
2975 update_power_state(codec, 0x27, parm); 838 update_power_state(codec, 0x27, parm);
2976 } else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode) 839 } else if (codec->vendor_id == 0x11064397 && spec->gen.indep_hp_enabled)
2977 update_power_state(codec, 0x25, parm); 840 update_power_state(codec, 0x25, parm);
2978} 841}
2979 842
@@ -2991,7 +854,7 @@ static int patch_vt1708B(struct hda_codec *codec)
2991 if (spec == NULL) 854 if (spec == NULL)
2992 return -ENOMEM; 855 return -ENOMEM;
2993 856
2994 spec->aa_mix_nid = 0x16; 857 spec->gen.mixer_nid = 0x16;
2995 858
2996 /* automatic parse from the BIOS config */ 859 /* automatic parse from the BIOS config */
2997 err = via_parse_auto_config(codec); 860 err = via_parse_auto_config(codec);
@@ -3016,58 +879,6 @@ static const struct hda_verb vt1708S_init_verbs[] = {
3016 { } 879 { }
3017}; 880};
3018 881
3019/* fill out digital output widgets; one for master and one for slave outputs */
3020static void fill_dig_outs(struct hda_codec *codec)
3021{
3022 struct via_spec *spec = codec->spec;
3023 int i;
3024
3025 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3026 hda_nid_t nid;
3027 int conn;
3028
3029 nid = spec->autocfg.dig_out_pins[i];
3030 if (!nid)
3031 continue;
3032 conn = snd_hda_get_connections(codec, nid, &nid, 1);
3033 if (conn < 1)
3034 continue;
3035 if (!spec->multiout.dig_out_nid)
3036 spec->multiout.dig_out_nid = nid;
3037 else {
3038 spec->slave_dig_outs[0] = nid;
3039 break; /* at most two dig outs */
3040 }
3041 }
3042}
3043
3044static void fill_dig_in(struct hda_codec *codec)
3045{
3046 struct via_spec *spec = codec->spec;
3047 hda_nid_t dig_nid;
3048 int i, err;
3049
3050 if (!spec->autocfg.dig_in_pin)
3051 return;
3052
3053 dig_nid = codec->start_nid;
3054 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3055 unsigned int wcaps = get_wcaps(codec, dig_nid);
3056 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3057 continue;
3058 if (!(wcaps & AC_WCAP_DIGITAL))
3059 continue;
3060 if (!(wcaps & AC_WCAP_CONN_LIST))
3061 continue;
3062 err = get_connection_index(codec, dig_nid,
3063 spec->autocfg.dig_in_pin);
3064 if (err >= 0) {
3065 spec->dig_in_nid = dig_nid;
3066 break;
3067 }
3068 }
3069}
3070
3071static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin, 882static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
3072 int offset, int num_steps, int step_size) 883 int offset, int num_steps, int step_size)
3073{ 884{
@@ -3088,21 +899,10 @@ static int patch_vt1708S(struct hda_codec *codec)
3088 if (spec == NULL) 899 if (spec == NULL)
3089 return -ENOMEM; 900 return -ENOMEM;
3090 901
3091 spec->aa_mix_nid = 0x16; 902 spec->gen.mixer_nid = 0x16;
3092 override_mic_boost(codec, 0x1a, 0, 3, 40); 903 override_mic_boost(codec, 0x1a, 0, 3, 40);
3093 override_mic_boost(codec, 0x1e, 0, 3, 40); 904 override_mic_boost(codec, 0x1e, 0, 3, 40);
3094 905
3095 /* automatic parse from the BIOS config */
3096 err = via_parse_auto_config(codec);
3097 if (err < 0) {
3098 via_free(codec);
3099 return err;
3100 }
3101
3102 spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
3103
3104 codec->patch_ops = via_patch_ops;
3105
3106 /* correct names for VT1708BCE */ 906 /* correct names for VT1708BCE */
3107 if (get_codec_type(codec) == VT1708BCE) { 907 if (get_codec_type(codec) == VT1708BCE) {
3108 kfree(codec->chip_name); 908 kfree(codec->chip_name);
@@ -3119,6 +919,18 @@ static int patch_vt1708S(struct hda_codec *codec)
3119 sizeof(codec->bus->card->mixername), 919 sizeof(codec->bus->card->mixername),
3120 "%s %s", codec->vendor_name, codec->chip_name); 920 "%s %s", codec->vendor_name, codec->chip_name);
3121 } 921 }
922
923 /* automatic parse from the BIOS config */
924 err = via_parse_auto_config(codec);
925 if (err < 0) {
926 via_free(codec);
927 return err;
928 }
929
930 spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
931
932 codec->patch_ops = via_patch_ops;
933
3122 spec->set_widgets_power_state = set_widgets_power_state_vt1708B; 934 spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
3123 return 0; 935 return 0;
3124} 936}
@@ -3173,7 +985,7 @@ static int patch_vt1702(struct hda_codec *codec)
3173 if (spec == NULL) 985 if (spec == NULL)
3174 return -ENOMEM; 986 return -ENOMEM;
3175 987
3176 spec->aa_mix_nid = 0x1a; 988 spec->gen.mixer_nid = 0x1a;
3177 989
3178 /* limit AA path volume to 0 dB */ 990 /* limit AA path volume to 0 dB */
3179 snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT, 991 snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
@@ -3240,17 +1052,17 @@ static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
3240 /* PW2 (26h), AOW2 (ah) */ 1052 /* PW2 (26h), AOW2 (ah) */
3241 parm = AC_PWRST_D3; 1053 parm = AC_PWRST_D3;
3242 set_pin_power_state(codec, 0x26, &parm); 1054 set_pin_power_state(codec, 0x26, &parm);
3243 if (spec->smart51_enabled) 1055 if (smart51_enabled(codec))
3244 set_pin_power_state(codec, 0x2b, &parm); 1056 set_pin_power_state(codec, 0x2b, &parm);
3245 update_power_state(codec, 0xa, parm); 1057 update_power_state(codec, 0xa, parm);
3246 1058
3247 /* PW0 (24h), AOW0 (8h) */ 1059 /* PW0 (24h), AOW0 (8h) */
3248 parm = AC_PWRST_D3; 1060 parm = AC_PWRST_D3;
3249 set_pin_power_state(codec, 0x24, &parm); 1061 set_pin_power_state(codec, 0x24, &parm);
3250 if (!spec->hp_independent_mode) /* check for redirected HP */ 1062 if (!spec->gen.indep_hp_enabled) /* check for redirected HP */
3251 set_pin_power_state(codec, 0x28, &parm); 1063 set_pin_power_state(codec, 0x28, &parm);
3252 update_power_state(codec, 0x8, parm); 1064 update_power_state(codec, 0x8, parm);
3253 if (!spec->hp_independent_mode && parm2 != AC_PWRST_D3) 1065 if (!spec->gen.indep_hp_enabled && parm2 != AC_PWRST_D3)
3254 parm = parm2; 1066 parm = parm2;
3255 update_power_state(codec, 0xb, parm); 1067 update_power_state(codec, 0xb, parm);
3256 /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */ 1068 /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
@@ -3259,11 +1071,11 @@ static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
3259 /* PW1 (25h), AOW1 (9h) */ 1071 /* PW1 (25h), AOW1 (9h) */
3260 parm = AC_PWRST_D3; 1072 parm = AC_PWRST_D3;
3261 set_pin_power_state(codec, 0x25, &parm); 1073 set_pin_power_state(codec, 0x25, &parm);
3262 if (spec->smart51_enabled) 1074 if (smart51_enabled(codec))
3263 set_pin_power_state(codec, 0x2a, &parm); 1075 set_pin_power_state(codec, 0x2a, &parm);
3264 update_power_state(codec, 0x9, parm); 1076 update_power_state(codec, 0x9, parm);
3265 1077
3266 if (spec->hp_independent_mode) { 1078 if (spec->gen.indep_hp_enabled) {
3267 /* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */ 1079 /* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
3268 parm = AC_PWRST_D3; 1080 parm = AC_PWRST_D3;
3269 set_pin_power_state(codec, 0x28, &parm); 1081 set_pin_power_state(codec, 0x28, &parm);
@@ -3283,9 +1095,9 @@ static int add_secret_dac_path(struct hda_codec *codec)
3283 hda_nid_t conn[8]; 1095 hda_nid_t conn[8];
3284 hda_nid_t nid; 1096 hda_nid_t nid;
3285 1097
3286 if (!spec->aa_mix_nid) 1098 if (!spec->gen.mixer_nid)
3287 return 0; 1099 return 0;
3288 nums = snd_hda_get_connections(codec, spec->aa_mix_nid, conn, 1100 nums = snd_hda_get_connections(codec, spec->gen.mixer_nid, conn,
3289 ARRAY_SIZE(conn) - 1); 1101 ARRAY_SIZE(conn) - 1);
3290 for (i = 0; i < nums; i++) { 1102 for (i = 0; i < nums; i++) {
3291 if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT) 1103 if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT)
@@ -3300,7 +1112,7 @@ static int add_secret_dac_path(struct hda_codec *codec)
3300 !(caps & AC_WCAP_DIGITAL)) { 1112 !(caps & AC_WCAP_DIGITAL)) {
3301 conn[nums++] = nid; 1113 conn[nums++] = nid;
3302 return snd_hda_override_conn_list(codec, 1114 return snd_hda_override_conn_list(codec,
3303 spec->aa_mix_nid, 1115 spec->gen.mixer_nid,
3304 nums, conn); 1116 nums, conn);
3305 } 1117 }
3306 } 1118 }
@@ -3318,7 +1130,7 @@ static int patch_vt1718S(struct hda_codec *codec)
3318 if (spec == NULL) 1130 if (spec == NULL)
3319 return -ENOMEM; 1131 return -ENOMEM;
3320 1132
3321 spec->aa_mix_nid = 0x21; 1133 spec->gen.mixer_nid = 0x21;
3322 override_mic_boost(codec, 0x2b, 0, 3, 40); 1134 override_mic_boost(codec, 0x2b, 0, 3, 40);
3323 override_mic_boost(codec, 0x29, 0, 3, 40); 1135 override_mic_boost(codec, 0x29, 0, 3, 40);
3324 add_secret_dac_path(codec); 1136 add_secret_dac_path(codec);
@@ -3449,7 +1261,7 @@ static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
3449 parm = AC_PWRST_D3; 1261 parm = AC_PWRST_D3;
3450 set_pin_power_state(codec, 0x19, &parm); 1262 set_pin_power_state(codec, 0x19, &parm);
3451 /* Smart 5.1 PW2(1bh) */ 1263 /* Smart 5.1 PW2(1bh) */
3452 if (spec->smart51_enabled) 1264 if (smart51_enabled(codec))
3453 set_pin_power_state(codec, 0x1b, &parm); 1265 set_pin_power_state(codec, 0x1b, &parm);
3454 update_power_state(codec, 0x18, parm); 1266 update_power_state(codec, 0x18, parm);
3455 update_power_state(codec, 0x11, parm); 1267 update_power_state(codec, 0x11, parm);
@@ -3458,12 +1270,12 @@ static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
3458 parm = AC_PWRST_D3; 1270 parm = AC_PWRST_D3;
3459 set_pin_power_state(codec, 0x23, &parm); 1271 set_pin_power_state(codec, 0x23, &parm);
3460 /* Smart 5.1 PW1(1ah) */ 1272 /* Smart 5.1 PW1(1ah) */
3461 if (spec->smart51_enabled) 1273 if (smart51_enabled(codec))
3462 set_pin_power_state(codec, 0x1a, &parm); 1274 set_pin_power_state(codec, 0x1a, &parm);
3463 update_power_state(codec, 0x27, parm); 1275 update_power_state(codec, 0x27, parm);
3464 1276
3465 /* Smart 5.1 PW5(1eh) */ 1277 /* Smart 5.1 PW5(1eh) */
3466 if (spec->smart51_enabled) 1278 if (smart51_enabled(codec))
3467 set_pin_power_state(codec, 0x1e, &parm); 1279 set_pin_power_state(codec, 0x1e, &parm);
3468 update_power_state(codec, 0x25, parm); 1280 update_power_state(codec, 0x25, parm);
3469 1281
@@ -3475,7 +1287,7 @@ static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
3475 mono_out = 0; 1287 mono_out = 0;
3476 else { 1288 else {
3477 present = snd_hda_jack_detect(codec, 0x1d); 1289 present = snd_hda_jack_detect(codec, 0x1d);
3478 if (!spec->hp_independent_mode && present) 1290 if (!spec->gen.indep_hp_enabled && present)
3479 mono_out = 0; 1291 mono_out = 0;
3480 else 1292 else
3481 mono_out = 1; 1293 mono_out = 1;
@@ -3490,7 +1302,7 @@ static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
3490 set_pin_power_state(codec, 0x1c, &parm); 1302 set_pin_power_state(codec, 0x1c, &parm);
3491 set_pin_power_state(codec, 0x1d, &parm); 1303 set_pin_power_state(codec, 0x1d, &parm);
3492 /* HP Independent Mode, power on AOW3 */ 1304 /* HP Independent Mode, power on AOW3 */
3493 if (spec->hp_independent_mode) 1305 if (spec->gen.indep_hp_enabled)
3494 update_power_state(codec, 0x25, parm); 1306 update_power_state(codec, 0x25, parm);
3495 1307
3496 /* force to D0 for internal Speaker */ 1308 /* force to D0 for internal Speaker */
@@ -3509,7 +1321,7 @@ static int patch_vt1716S(struct hda_codec *codec)
3509 if (spec == NULL) 1321 if (spec == NULL)
3510 return -ENOMEM; 1322 return -ENOMEM;
3511 1323
3512 spec->aa_mix_nid = 0x16; 1324 spec->gen.mixer_nid = 0x16;
3513 override_mic_boost(codec, 0x1a, 0, 3, 40); 1325 override_mic_boost(codec, 0x1a, 0, 3, 40);
3514 override_mic_boost(codec, 0x1e, 0, 3, 40); 1326 override_mic_boost(codec, 0x1e, 0, 3, 40);
3515 1327
@@ -3522,9 +1334,7 @@ static int patch_vt1716S(struct hda_codec *codec)
3522 1334
3523 spec->init_verbs[spec->num_iverbs++] = vt1716S_init_verbs; 1335 spec->init_verbs[spec->num_iverbs++] = vt1716S_init_verbs;
3524 1336
3525 spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer; 1337 spec->mixers[spec->num_mixers++] = vt1716s_dmic_mixer;
3526 spec->num_mixers++;
3527
3528 spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer; 1338 spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
3529 1339
3530 codec->patch_ops = via_patch_ops; 1340 codec->patch_ops = via_patch_ops;
@@ -3609,7 +1419,7 @@ static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
3609 update_power_state(codec, 0x35, parm); 1419 update_power_state(codec, 0x35, parm);
3610 } 1420 }
3611 1421
3612 if (spec->hp_independent_mode) 1422 if (spec->gen.indep_hp_enabled)
3613 update_power_state(codec, 0x9, AC_PWRST_D0); 1423 update_power_state(codec, 0x9, AC_PWRST_D0);
3614 1424
3615 /* Class-D */ 1425 /* Class-D */
@@ -3707,7 +1517,7 @@ static int patch_vt2002P(struct hda_codec *codec)
3707 if (spec == NULL) 1517 if (spec == NULL)
3708 return -ENOMEM; 1518 return -ENOMEM;
3709 1519
3710 spec->aa_mix_nid = 0x21; 1520 spec->gen.mixer_nid = 0x21;
3711 override_mic_boost(codec, 0x2b, 0, 3, 40); 1521 override_mic_boost(codec, 0x2b, 0, 3, 40);
3712 override_mic_boost(codec, 0x29, 0, 3, 40); 1522 override_mic_boost(codec, 0x29, 0, 3, 40);
3713 if (spec->codec_type == VT1802) 1523 if (spec->codec_type == VT1802)
@@ -3778,7 +1588,7 @@ static void set_widgets_power_state_vt1812(struct hda_codec *codec)
3778 set_pin_power_state(codec, 0x25, &parm); 1588 set_pin_power_state(codec, 0x25, &parm);
3779 update_power_state(codec, 0x15, parm); 1589 update_power_state(codec, 0x15, parm);
3780 update_power_state(codec, 0x35, parm); 1590 update_power_state(codec, 0x35, parm);
3781 if (spec->hp_independent_mode) 1591 if (spec->gen.indep_hp_enabled)
3782 update_power_state(codec, 0x9, AC_PWRST_D0); 1592 update_power_state(codec, 0x9, AC_PWRST_D0);
3783 1593
3784 /* Internal Speaker */ 1594 /* Internal Speaker */
@@ -3831,7 +1641,7 @@ static int patch_vt1812(struct hda_codec *codec)
3831 if (spec == NULL) 1641 if (spec == NULL)
3832 return -ENOMEM; 1642 return -ENOMEM;
3833 1643
3834 spec->aa_mix_nid = 0x21; 1644 spec->gen.mixer_nid = 0x21;
3835 override_mic_boost(codec, 0x2b, 0, 3, 40); 1645 override_mic_boost(codec, 0x2b, 0, 3, 40);
3836 override_mic_boost(codec, 0x29, 0, 3, 40); 1646 override_mic_boost(codec, 0x29, 0, 3, 40);
3837 add_secret_dac_path(codec); 1647 add_secret_dac_path(codec);
@@ -3901,7 +1711,7 @@ static void set_widgets_power_state_vt3476(struct hda_codec *codec)
3901 parm = AC_PWRST_D3; 1711 parm = AC_PWRST_D3;
3902 set_pin_power_state(codec, 0x26, &parm); 1712 set_pin_power_state(codec, 0x26, &parm);
3903 update_power_state(codec, 0x36, parm); 1713 update_power_state(codec, 0x36, parm);
3904 if (spec->smart51_enabled) { 1714 if (smart51_enabled(codec)) {
3905 /* PW7(2bh), MW7(3bh), MUX7(1Bh) */ 1715 /* PW7(2bh), MW7(3bh), MUX7(1Bh) */
3906 set_pin_power_state(codec, 0x2b, &parm); 1716 set_pin_power_state(codec, 0x2b, &parm);
3907 update_power_state(codec, 0x3b, parm); 1717 update_power_state(codec, 0x3b, parm);
@@ -3913,7 +1723,7 @@ static void set_widgets_power_state_vt3476(struct hda_codec *codec)
3913 parm = AC_PWRST_D3; 1723 parm = AC_PWRST_D3;
3914 set_pin_power_state(codec, 0x25, &parm); 1724 set_pin_power_state(codec, 0x25, &parm);
3915 update_power_state(codec, 0x35, parm); 1725 update_power_state(codec, 0x35, parm);
3916 if (spec->smart51_enabled) { 1726 if (smart51_enabled(codec)) {
3917 /* PW6(2ah), MW6(3ah), MUX6(1ah) */ 1727 /* PW6(2ah), MW6(3ah), MUX6(1ah) */
3918 set_pin_power_state(codec, 0x2a, &parm); 1728 set_pin_power_state(codec, 0x2a, &parm);
3919 update_power_state(codec, 0x3a, parm); 1729 update_power_state(codec, 0x3a, parm);
@@ -3926,7 +1736,7 @@ static void set_widgets_power_state_vt3476(struct hda_codec *codec)
3926 set_pin_power_state(codec, 0x28, &parm); 1736 set_pin_power_state(codec, 0x28, &parm);
3927 update_power_state(codec, 0x38, parm); 1737 update_power_state(codec, 0x38, parm);
3928 update_power_state(codec, 0x18, parm); 1738 update_power_state(codec, 0x18, parm);
3929 if (spec->hp_independent_mode) 1739 if (spec->gen.indep_hp_enabled)
3930 update_conv_power_state(codec, 0xb, parm, 3); 1740 update_conv_power_state(codec, 0xb, parm, 3);
3931 parm2 = parm; /* for pin 0x0b */ 1741 parm2 = parm; /* for pin 0x0b */
3932 1742
@@ -3934,7 +1744,7 @@ static void set_widgets_power_state_vt3476(struct hda_codec *codec)
3934 parm = AC_PWRST_D3; 1744 parm = AC_PWRST_D3;
3935 set_pin_power_state(codec, 0x24, &parm); 1745 set_pin_power_state(codec, 0x24, &parm);
3936 update_power_state(codec, 0x34, parm); 1746 update_power_state(codec, 0x34, parm);
3937 if (!spec->hp_independent_mode && parm2 != AC_PWRST_D3) 1747 if (!spec->gen.indep_hp_enabled && parm2 != AC_PWRST_D3)
3938 parm = parm2; 1748 parm = parm2;
3939 update_conv_power_state(codec, 0x8, parm, 0); 1749 update_conv_power_state(codec, 0x8, parm, 0);
3940 /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */ 1750 /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
@@ -3951,7 +1761,7 @@ static int patch_vt3476(struct hda_codec *codec)
3951 if (spec == NULL) 1761 if (spec == NULL)
3952 return -ENOMEM; 1762 return -ENOMEM;
3953 1763
3954 spec->aa_mix_nid = 0x3f; 1764 spec->gen.mixer_nid = 0x3f;
3955 add_secret_dac_path(codec); 1765 add_secret_dac_path(codec);
3956 1766
3957 /* automatic parse from the BIOS config */ 1767 /* automatic parse from the BIOS config */