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