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