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