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