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