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