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