d374e8cfdcc8046edb9cbeb705091019e3567803
[pandora-kernel.git] / sound / pci / hda / patch_via.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * HD audio interface patch for VIA VT17xx/VT18xx/VT20xx codec
5  *
6  *  (C) 2006-2009 VIA Technology, Inc.
7  *  (C) 2006-2008 Takashi Iwai <tiwai@suse.de>
8  *
9  *  This driver is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This driver is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  */
23
24 /* * * * * * * * * * * * * * Release History * * * * * * * * * * * * * * * * */
25 /*                                                                           */
26 /* 2006-03-03  Lydia Wang  Create the basic patch to support VT1708 codec    */
27 /* 2006-03-14  Lydia Wang  Modify hard code for some pin widget nid          */
28 /* 2006-08-02  Lydia Wang  Add support to VT1709 codec                       */
29 /* 2006-09-08  Lydia Wang  Fix internal loopback recording source select bug */
30 /* 2007-09-12  Lydia Wang  Add EAPD enable during driver initialization      */
31 /* 2007-09-17  Lydia Wang  Add VT1708B codec support                        */
32 /* 2007-11-14  Lydia Wang  Add VT1708A codec HP and CD pin connect config    */
33 /* 2008-02-03  Lydia Wang  Fix Rear channels and Back channels inverse issue */
34 /* 2008-03-06  Lydia Wang  Add VT1702 codec and VT1708S codec support        */
35 /* 2008-04-09  Lydia Wang  Add mute front speaker when HP plugin             */
36 /* 2008-04-09  Lydia Wang  Add Independent HP feature                        */
37 /* 2008-05-28  Lydia Wang  Add second S/PDIF Out support for VT1702          */
38 /* 2008-09-15  Logan Li    Add VT1708S Mic Boost workaround/backdoor         */
39 /* 2009-02-16  Logan Li    Add support for VT1718S                           */
40 /* 2009-03-13  Logan Li    Add support for VT1716S                           */
41 /* 2009-04-14  Lydai Wang  Add support for VT1828S and VT2020                */
42 /* 2009-07-08  Lydia Wang  Add support for VT2002P                           */
43 /* 2009-07-21  Lydia Wang  Add support for VT1812                            */
44 /* 2009-09-19  Lydia Wang  Add support for VT1818S                           */
45 /*                                                                           */
46 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
47
48
49 #include <linux/init.h>
50 #include <linux/delay.h>
51 #include <linux/slab.h>
52 #include <sound/core.h>
53 #include <sound/asoundef.h>
54 #include "hda_codec.h"
55 #include "hda_local.h"
56
57 #define NID_MAPPING             (-1)
58
59 /* amp values */
60 #define AMP_VAL_IDX_SHIFT       19
61 #define AMP_VAL_IDX_MASK        (0x0f<<19)
62
63 /* Pin Widget NID */
64 #define VT1708_HP_NID           0x13
65 #define VT1708_DIGOUT_NID       0x14
66 #define VT1708_DIGIN_NID        0x16
67 #define VT1708_DIGIN_PIN        0x26
68 #define VT1708_HP_PIN_NID       0x20
69 #define VT1708_CD_PIN_NID       0x24
70
71 #define VT1709_HP_DAC_NID       0x28
72 #define VT1709_DIGOUT_NID       0x13
73 #define VT1709_DIGIN_NID        0x17
74 #define VT1709_DIGIN_PIN        0x25
75
76 #define VT1708B_HP_NID          0x25
77 #define VT1708B_DIGOUT_NID      0x12
78 #define VT1708B_DIGIN_NID       0x15
79 #define VT1708B_DIGIN_PIN       0x21
80
81 #define VT1708S_HP_NID          0x25
82 #define VT1708S_DIGOUT_NID      0x12
83
84 #define VT1702_HP_NID           0x17
85 #define VT1702_DIGOUT_NID       0x11
86
87 enum VIA_HDA_CODEC {
88         UNKNOWN = -1,
89         VT1708,
90         VT1709_10CH,
91         VT1709_6CH,
92         VT1708B_8CH,
93         VT1708B_4CH,
94         VT1708S,
95         VT1708BCE,
96         VT1702,
97         VT1718S,
98         VT1716S,
99         VT2002P,
100         VT1812,
101         VT1802,
102         CODEC_TYPES,
103 };
104
105 #define VT2002P_COMPATIBLE(spec) \
106         ((spec)->codec_type == VT2002P ||\
107          (spec)->codec_type == VT1812 ||\
108          (spec)->codec_type == VT1802)
109
110 struct via_spec {
111         /* codec parameterization */
112         const struct snd_kcontrol_new *mixers[6];
113         unsigned int num_mixers;
114
115         const struct hda_verb *init_verbs[5];
116         unsigned int num_iverbs;
117
118         char stream_name_analog[32];
119         const struct hda_pcm_stream *stream_analog_playback;
120         const struct hda_pcm_stream *stream_analog_capture;
121
122         char stream_name_digital[32];
123         const struct hda_pcm_stream *stream_digital_playback;
124         const struct hda_pcm_stream *stream_digital_capture;
125
126         /* playback */
127         struct hda_multi_out multiout;
128         hda_nid_t slave_dig_outs[2];
129
130         /* capture */
131         unsigned int num_adc_nids;
132         hda_nid_t adc_nids[3];
133         hda_nid_t mux_nids[3];
134         hda_nid_t dig_in_nid;
135         hda_nid_t dig_in_pin;
136
137         /* capture source */
138         const struct hda_input_mux *input_mux;
139         unsigned int cur_mux[3];
140
141         /* PCM information */
142         struct hda_pcm pcm_rec[3];
143
144         /* dynamic controls, init_verbs and input_mux */
145         struct auto_pin_cfg autocfg;
146         struct snd_array kctls;
147         struct hda_input_mux private_imux[2];
148         hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
149
150         /* HP mode source */
151         const struct hda_input_mux *hp_mux;
152         unsigned int hp_independent_mode;
153         unsigned int hp_independent_mode_index;
154         unsigned int smart51_enabled;
155         unsigned int dmic_enabled;
156         unsigned int no_pin_power_ctl;
157         enum VIA_HDA_CODEC codec_type;
158
159         /* work to check hp jack state */
160         struct hda_codec *codec;
161         struct delayed_work vt1708_hp_work;
162         int vt1708_jack_detect;
163         int vt1708_hp_present;
164
165         void (*set_widgets_power_state)(struct hda_codec *codec);
166
167 #ifdef CONFIG_SND_HDA_POWER_SAVE
168         struct hda_loopback_check loopback;
169 #endif
170 };
171
172 static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
173 static struct via_spec * via_new_spec(struct hda_codec *codec)
174 {
175         struct via_spec *spec;
176
177         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
178         if (spec == NULL)
179                 return NULL;
180
181         codec->spec = spec;
182         spec->codec = codec;
183         spec->codec_type = get_codec_type(codec);
184         /* VT1708BCE & VT1708S are almost same */
185         if (spec->codec_type == VT1708BCE)
186                 spec->codec_type = VT1708S;
187         return spec;
188 }
189
190 static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
191 {
192         u32 vendor_id = codec->vendor_id;
193         u16 ven_id = vendor_id >> 16;
194         u16 dev_id = vendor_id & 0xffff;
195         enum VIA_HDA_CODEC codec_type;
196
197         /* get codec type */
198         if (ven_id != 0x1106)
199                 codec_type = UNKNOWN;
200         else if (dev_id >= 0x1708 && dev_id <= 0x170b)
201                 codec_type = VT1708;
202         else if (dev_id >= 0xe710 && dev_id <= 0xe713)
203                 codec_type = VT1709_10CH;
204         else if (dev_id >= 0xe714 && dev_id <= 0xe717)
205                 codec_type = VT1709_6CH;
206         else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
207                 codec_type = VT1708B_8CH;
208                 if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
209                         codec_type = VT1708BCE;
210         } else if (dev_id >= 0xe724 && dev_id <= 0xe727)
211                 codec_type = VT1708B_4CH;
212         else if ((dev_id & 0xfff) == 0x397
213                  && (dev_id >> 12) < 8)
214                 codec_type = VT1708S;
215         else if ((dev_id & 0xfff) == 0x398
216                  && (dev_id >> 12) < 8)
217                 codec_type = VT1702;
218         else if ((dev_id & 0xfff) == 0x428
219                  && (dev_id >> 12) < 8)
220                 codec_type = VT1718S;
221         else if (dev_id == 0x0433 || dev_id == 0xa721)
222                 codec_type = VT1716S;
223         else if (dev_id == 0x0441 || dev_id == 0x4441)
224                 codec_type = VT1718S;
225         else if (dev_id == 0x0438 || dev_id == 0x4438)
226                 codec_type = VT2002P;
227         else if (dev_id == 0x0448)
228                 codec_type = VT1812;
229         else if (dev_id == 0x0440)
230                 codec_type = VT1708S;
231         else if ((dev_id & 0xfff) == 0x446)
232                 codec_type = VT1802;
233         else
234                 codec_type = UNKNOWN;
235         return codec_type;
236 };
237
238 #define VIA_JACK_EVENT          0x20
239 #define VIA_HP_EVENT            0x01
240 #define VIA_GPIO_EVENT          0x02
241 #define VIA_MONO_EVENT          0x03
242 #define VIA_SPEAKER_EVENT       0x04
243 #define VIA_BIND_HP_EVENT       0x05
244
245 enum {
246         VIA_CTL_WIDGET_VOL,
247         VIA_CTL_WIDGET_MUTE,
248         VIA_CTL_WIDGET_ANALOG_MUTE,
249         VIA_CTL_WIDGET_BIND_PIN_MUTE,
250 };
251
252 enum {
253         AUTO_SEQ_FRONT = 0,
254         AUTO_SEQ_SURROUND,
255         AUTO_SEQ_CENLFE,
256         AUTO_SEQ_SIDE
257 };
258
259 static void analog_low_current_mode(struct hda_codec *codec, int stream_idle);
260 static int is_aa_path_mute(struct hda_codec *codec);
261
262 static void vt1708_start_hp_work(struct via_spec *spec)
263 {
264         if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
265                 return;
266         snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
267                             !spec->vt1708_jack_detect);
268         if (!delayed_work_pending(&spec->vt1708_hp_work))
269                 schedule_delayed_work(&spec->vt1708_hp_work,
270                                       msecs_to_jiffies(100));
271 }
272
273 static void vt1708_stop_hp_work(struct via_spec *spec)
274 {
275         if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
276                 return;
277         if (snd_hda_get_bool_hint(spec->codec, "analog_loopback_hp_detect") == 1
278             && !is_aa_path_mute(spec->codec))
279                 return;
280         snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
281                             !spec->vt1708_jack_detect);
282         cancel_delayed_work_sync(&spec->vt1708_hp_work);
283 }
284
285 static void set_widgets_power_state(struct hda_codec *codec)
286 {
287         struct via_spec *spec = codec->spec;
288         if (spec->set_widgets_power_state)
289                 spec->set_widgets_power_state(codec);
290 }
291
292 static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
293                                    struct snd_ctl_elem_value *ucontrol)
294 {
295         int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
296         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
297
298         set_widgets_power_state(codec);
299         analog_low_current_mode(snd_kcontrol_chip(kcontrol), -1);
300         if (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1) {
301                 if (is_aa_path_mute(codec))
302                         vt1708_start_hp_work(codec->spec);
303                 else
304                         vt1708_stop_hp_work(codec->spec);
305         }
306         return change;
307 }
308
309 /* modify .put = snd_hda_mixer_amp_switch_put */
310 #define ANALOG_INPUT_MUTE                                               \
311         {               .iface = SNDRV_CTL_ELEM_IFACE_MIXER,            \
312                         .name = NULL,                                   \
313                         .index = 0,                                     \
314                         .info = snd_hda_mixer_amp_switch_info,          \
315                         .get = snd_hda_mixer_amp_switch_get,            \
316                         .put = analog_input_switch_put,                 \
317                         .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
318
319 static void via_hp_bind_automute(struct hda_codec *codec);
320
321 static int bind_pin_switch_put(struct snd_kcontrol *kcontrol,
322                                struct snd_ctl_elem_value *ucontrol)
323 {
324         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
325         struct via_spec *spec = codec->spec;
326         int i;
327         int change = 0;
328
329         long *valp = ucontrol->value.integer.value;
330         int lmute, rmute;
331         if (strstr(kcontrol->id.name, "Switch") == NULL) {
332                 snd_printd("Invalid control!\n");
333                 return change;
334         }
335         change = snd_hda_mixer_amp_switch_put(kcontrol,
336                                               ucontrol);
337         /* Get mute value */
338         lmute = *valp ? 0 : HDA_AMP_MUTE;
339         valp++;
340         rmute = *valp ? 0 : HDA_AMP_MUTE;
341
342         /* Set hp pins */
343         if (!spec->hp_independent_mode) {
344                 for (i = 0; i < spec->autocfg.hp_outs; i++) {
345                         snd_hda_codec_amp_update(
346                                 codec, spec->autocfg.hp_pins[i],
347                                 0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
348                                 lmute);
349                         snd_hda_codec_amp_update(
350                                 codec, spec->autocfg.hp_pins[i],
351                                 1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
352                                 rmute);
353                 }
354         }
355
356         if (!lmute && !rmute) {
357                 /* Line Outs */
358                 for (i = 0; i < spec->autocfg.line_outs; i++)
359                         snd_hda_codec_amp_stereo(
360                                 codec, spec->autocfg.line_out_pins[i],
361                                 HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
362                 /* Speakers */
363                 for (i = 0; i < spec->autocfg.speaker_outs; i++)
364                         snd_hda_codec_amp_stereo(
365                                 codec, spec->autocfg.speaker_pins[i],
366                                 HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
367                 /* unmute */
368                 via_hp_bind_automute(codec);
369
370         } else {
371                 if (lmute) {
372                         /* Mute all left channels */
373                         for (i = 1; i < spec->autocfg.line_outs; i++)
374                                 snd_hda_codec_amp_update(
375                                         codec,
376                                         spec->autocfg.line_out_pins[i],
377                                         0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
378                                         lmute);
379                         for (i = 0; i < spec->autocfg.speaker_outs; i++)
380                                 snd_hda_codec_amp_update(
381                                         codec,
382                                         spec->autocfg.speaker_pins[i],
383                                         0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
384                                         lmute);
385                 }
386                 if (rmute) {
387                         /* mute all right channels */
388                         for (i = 1; i < spec->autocfg.line_outs; i++)
389                                 snd_hda_codec_amp_update(
390                                         codec,
391                                         spec->autocfg.line_out_pins[i],
392                                         1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
393                                         rmute);
394                         for (i = 0; i < spec->autocfg.speaker_outs; i++)
395                                 snd_hda_codec_amp_update(
396                                         codec,
397                                         spec->autocfg.speaker_pins[i],
398                                         1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
399                                         rmute);
400                 }
401         }
402         return change;
403 }
404
405 #define BIND_PIN_MUTE                                                   \
406         {               .iface = SNDRV_CTL_ELEM_IFACE_MIXER,            \
407                         .name = NULL,                                   \
408                         .index = 0,                                     \
409                         .info = snd_hda_mixer_amp_switch_info,          \
410                         .get = snd_hda_mixer_amp_switch_get,            \
411                         .put = bind_pin_switch_put,                     \
412                         .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
413
414 static const struct snd_kcontrol_new via_control_templates[] = {
415         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
416         HDA_CODEC_MUTE(NULL, 0, 0, 0),
417         ANALOG_INPUT_MUTE,
418         BIND_PIN_MUTE,
419 };
420
421
422 /* add dynamic controls */
423 static struct snd_kcontrol_new *__via_clone_ctl(struct via_spec *spec,
424                                 const struct snd_kcontrol_new *tmpl,
425                                 const char *name)
426 {
427         struct snd_kcontrol_new *knew;
428
429         snd_array_init(&spec->kctls, sizeof(*knew), 32);
430         knew = snd_array_new(&spec->kctls);
431         if (!knew)
432                 return NULL;
433         *knew = *tmpl;
434         if (!name)
435                 name = tmpl->name;
436         if (name) {
437                 knew->name = kstrdup(name, GFP_KERNEL);
438                 if (!knew->name)
439                         return NULL;
440         }
441         return knew;
442 }
443
444 static int __via_add_control(struct via_spec *spec, int type, const char *name,
445                              int idx, unsigned long val)
446 {
447         struct snd_kcontrol_new *knew;
448
449         knew = __via_clone_ctl(spec, &via_control_templates[type], name);
450         if (!knew)
451                 return -ENOMEM;
452         if (get_amp_nid_(val))
453                 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
454         knew->private_value = val;
455         return 0;
456 }
457
458 #define via_add_control(spec, type, name, val) \
459         __via_add_control(spec, type, name, 0, val)
460
461 #define via_clone_control(spec, tmpl) __via_clone_ctl(spec, tmpl, NULL)
462
463 static void via_free_kctls(struct hda_codec *codec)
464 {
465         struct via_spec *spec = codec->spec;
466
467         if (spec->kctls.list) {
468                 struct snd_kcontrol_new *kctl = spec->kctls.list;
469                 int i;
470                 for (i = 0; i < spec->kctls.used; i++)
471                         kfree(kctl[i].name);
472         }
473         snd_array_free(&spec->kctls);
474 }
475
476 /* create input playback/capture controls for the given pin */
477 static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
478                                 int type_idx, int idx, int mix_nid)
479 {
480         char name[32];
481         int err;
482
483         sprintf(name, "%s Playback Volume", ctlname);
484         err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
485                               HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
486         if (err < 0)
487                 return err;
488         sprintf(name, "%s Playback Switch", ctlname);
489         err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
490                               HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
491         if (err < 0)
492                 return err;
493         return 0;
494 }
495
496 static void via_auto_set_output_and_unmute(struct hda_codec *codec,
497                                            hda_nid_t nid, int pin_type,
498                                            int dac_idx)
499 {
500         /* set as output */
501         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
502                             pin_type);
503         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
504                             AMP_OUT_UNMUTE);
505         if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
506                 snd_hda_codec_write(codec, nid, 0,
507                                     AC_VERB_SET_EAPD_BTLENABLE, 0x02);
508 }
509
510
511 static void via_auto_init_multi_out(struct hda_codec *codec)
512 {
513         struct via_spec *spec = codec->spec;
514         int i;
515
516         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
517                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
518                 if (nid)
519                         via_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
520         }
521 }
522
523 static void via_auto_init_hp_out(struct hda_codec *codec)
524 {
525         struct via_spec *spec = codec->spec;
526         hda_nid_t pin;
527         int i;
528
529         for (i = 0; i < spec->autocfg.hp_outs; i++) {
530                 pin = spec->autocfg.hp_pins[i];
531                 if (pin) /* connect to front */
532                         via_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
533         }
534 }
535
536 static int is_smart51_pins(struct via_spec *spec, hda_nid_t pin);
537
538 static void via_auto_init_analog_input(struct hda_codec *codec)
539 {
540         struct via_spec *spec = codec->spec;
541         const struct auto_pin_cfg *cfg = &spec->autocfg;
542         unsigned int ctl;
543         int i;
544
545         for (i = 0; i < cfg->num_inputs; i++) {
546                 hda_nid_t nid = cfg->inputs[i].pin;
547                 if (spec->smart51_enabled && is_smart51_pins(spec, nid))
548                         ctl = PIN_OUT;
549                 else if (cfg->inputs[i].type == AUTO_PIN_MIC)
550                         ctl = PIN_VREF50;
551                 else
552                         ctl = PIN_IN;
553                 snd_hda_codec_write(codec, nid, 0,
554                                     AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
555         }
556 }
557
558 static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
559                                 unsigned int *affected_parm)
560 {
561         unsigned parm;
562         unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
563         unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
564                 >> AC_DEFCFG_MISC_SHIFT
565                 & AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
566         struct via_spec *spec = codec->spec;
567         unsigned present = 0;
568
569         no_presence |= spec->no_pin_power_ctl;
570         if (!no_presence)
571                 present = snd_hda_jack_detect(codec, nid);
572         if ((spec->smart51_enabled && is_smart51_pins(spec, nid))
573             || ((no_presence || present)
574                 && get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
575                 *affected_parm = AC_PWRST_D0; /* if it's connected */
576                 parm = AC_PWRST_D0;
577         } else
578                 parm = AC_PWRST_D3;
579
580         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
581 }
582
583 static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
584                                   struct snd_ctl_elem_info *uinfo)
585 {
586         static const char * const texts[] = {
587                 "Disabled", "Enabled"
588         };
589
590         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
591         uinfo->count = 1;
592         uinfo->value.enumerated.items = 2;
593         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
594                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
595         strcpy(uinfo->value.enumerated.name,
596                texts[uinfo->value.enumerated.item]);
597         return 0;
598 }
599
600 static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
601                                  struct snd_ctl_elem_value *ucontrol)
602 {
603         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
604         struct via_spec *spec = codec->spec;
605         ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
606         return 0;
607 }
608
609 static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
610                                  struct snd_ctl_elem_value *ucontrol)
611 {
612         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
613         struct via_spec *spec = codec->spec;
614         unsigned int val = !ucontrol->value.enumerated.item[0];
615
616         if (val == spec->no_pin_power_ctl)
617                 return 0;
618         spec->no_pin_power_ctl = val;
619         set_widgets_power_state(codec);
620         return 1;
621 }
622
623 static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
624         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
625         .name = "Dynamic Power-Control",
626         .info = via_pin_power_ctl_info,
627         .get = via_pin_power_ctl_get,
628         .put = via_pin_power_ctl_put,
629 };
630
631
632 /*
633  * input MUX handling
634  */
635 static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
636                              struct snd_ctl_elem_info *uinfo)
637 {
638         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
639         struct via_spec *spec = codec->spec;
640         return snd_hda_input_mux_info(spec->input_mux, uinfo);
641 }
642
643 static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
644                             struct snd_ctl_elem_value *ucontrol)
645 {
646         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
647         struct via_spec *spec = codec->spec;
648         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
649
650         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
651         return 0;
652 }
653
654 static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
655                             struct snd_ctl_elem_value *ucontrol)
656 {
657         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
658         struct via_spec *spec = codec->spec;
659         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
660         int ret;
661
662         if (!spec->mux_nids[adc_idx])
663                 return -EINVAL;
664         /* switch to D0 beofre change index */
665         if (snd_hda_codec_read(codec, spec->mux_nids[adc_idx], 0,
666                                AC_VERB_GET_POWER_STATE, 0x00) != AC_PWRST_D0)
667                 snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
668                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
669
670         ret = snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
671                                      spec->mux_nids[adc_idx],
672                                      &spec->cur_mux[adc_idx]);
673         /* update jack power state */
674         set_widgets_power_state(codec);
675
676         return ret;
677 }
678
679 static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
680                                    struct snd_ctl_elem_info *uinfo)
681 {
682         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
683         struct via_spec *spec = codec->spec;
684         return snd_hda_input_mux_info(spec->hp_mux, uinfo);
685 }
686
687 static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
688                                   struct snd_ctl_elem_value *ucontrol)
689 {
690         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
691         hda_nid_t nid = kcontrol->private_value;
692         unsigned int pinsel;
693
694         /* use !! to translate conn sel 2 for VT1718S */
695         pinsel = !!snd_hda_codec_read(codec, nid, 0,
696                                       AC_VERB_GET_CONNECT_SEL,
697                                       0x00);
698         ucontrol->value.enumerated.item[0] = pinsel;
699
700         return 0;
701 }
702
703 static void activate_ctl(struct hda_codec *codec, const char *name, int active)
704 {
705         struct snd_kcontrol *ctl = snd_hda_find_mixer_ctl(codec, name);
706         if (ctl) {
707                 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
708                 ctl->vd[0].access |= active
709                         ? 0 : SNDRV_CTL_ELEM_ACCESS_INACTIVE;
710                 snd_ctl_notify(codec->bus->card,
711                                SNDRV_CTL_EVENT_MASK_VALUE, &ctl->id);
712         }
713 }
714
715 static hda_nid_t side_mute_channel(struct via_spec *spec)
716 {
717         switch (spec->codec_type) {
718         case VT1708:            return 0x1b;
719         case VT1709_10CH:       return 0x29;
720         case VT1708B_8CH:       /* fall thru */
721         case VT1708S:           return 0x27;
722         case VT2002P:           return 0x19;
723         case VT1802:            return 0x15;
724         case VT1812:            return 0x15;
725         default:                return 0;
726         }
727 }
728
729 static int update_side_mute_status(struct hda_codec *codec)
730 {
731         /* mute side channel */
732         struct via_spec *spec = codec->spec;
733         unsigned int parm;
734         hda_nid_t sw3 = side_mute_channel(spec);
735
736         if (sw3) {
737                 if (VT2002P_COMPATIBLE(spec))
738                         parm = spec->hp_independent_mode ?
739                                AMP_IN_MUTE(1) : AMP_IN_UNMUTE(1);
740                 else
741                         parm = spec->hp_independent_mode ?
742                                AMP_OUT_MUTE : AMP_OUT_UNMUTE;
743                 snd_hda_codec_write(codec, sw3, 0,
744                                     AC_VERB_SET_AMP_GAIN_MUTE, parm);
745                 if (spec->codec_type == VT1812)
746                         snd_hda_codec_write(codec, 0x1d, 0,
747                                             AC_VERB_SET_AMP_GAIN_MUTE, parm);
748         }
749         return 0;
750 }
751
752 static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
753                                   struct snd_ctl_elem_value *ucontrol)
754 {
755         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
756         struct via_spec *spec = codec->spec;
757         hda_nid_t nid = kcontrol->private_value;
758         unsigned int pinsel = ucontrol->value.enumerated.item[0];
759         /* Get Independent Mode index of headphone pin widget */
760         spec->hp_independent_mode = spec->hp_independent_mode_index == pinsel
761                 ? 1 : 0;
762         if (spec->codec_type == VT1718S)
763                 snd_hda_codec_write(codec, nid, 0,
764                                     AC_VERB_SET_CONNECT_SEL, pinsel ? 2 : 0);
765         else
766                 snd_hda_codec_write(codec, nid, 0,
767                                     AC_VERB_SET_CONNECT_SEL, pinsel);
768
769         if (spec->codec_type == VT1812)
770                 snd_hda_codec_write(codec, 0x35, 0,
771                                     AC_VERB_SET_CONNECT_SEL, pinsel);
772         if (spec->multiout.hp_nid && spec->multiout.hp_nid
773             != spec->multiout.dac_nids[HDA_FRONT])
774                 snd_hda_codec_setup_stream(codec, spec->multiout.hp_nid,
775                                            0, 0, 0);
776
777         update_side_mute_status(codec);
778         /* update HP volume/swtich active state */
779         if (spec->codec_type == VT1708S
780             || spec->codec_type == VT1702
781             || spec->codec_type == VT1718S
782             || spec->codec_type == VT1716S
783             || VT2002P_COMPATIBLE(spec)) {
784                 activate_ctl(codec, "Headphone Playback Volume",
785                              spec->hp_independent_mode);
786                 activate_ctl(codec, "Headphone Playback Switch",
787                              spec->hp_independent_mode);
788         }
789         /* update jack power state */
790         set_widgets_power_state(codec);
791         return 0;
792 }
793
794 static const struct snd_kcontrol_new via_hp_mixer[2] = {
795         {
796                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
797                 .name = "Independent HP",
798                 .info = via_independent_hp_info,
799                 .get = via_independent_hp_get,
800                 .put = via_independent_hp_put,
801         },
802         {
803                 .iface = NID_MAPPING,
804                 .name = "Independent HP",
805         },
806 };
807
808 static int via_hp_build(struct hda_codec *codec)
809 {
810         struct via_spec *spec = codec->spec;
811         struct snd_kcontrol_new *knew;
812         hda_nid_t nid;
813         int nums;
814         hda_nid_t conn[HDA_MAX_CONNECTIONS];
815
816         switch (spec->codec_type) {
817         case VT1718S:
818                 nid = 0x34;
819                 break;
820         case VT2002P:
821         case VT1802:
822                 nid = 0x35;
823                 break;
824         case VT1812:
825                 nid = 0x3d;
826                 break;
827         default:
828                 nid = spec->autocfg.hp_pins[0];
829                 break;
830         }
831
832         if (spec->codec_type != VT1708) {
833                 nums = snd_hda_get_connections(codec, nid,
834                                                conn, HDA_MAX_CONNECTIONS);
835                 if (nums <= 1)
836                         return 0;
837         }
838
839         knew = via_clone_control(spec, &via_hp_mixer[0]);
840         if (knew == NULL)
841                 return -ENOMEM;
842
843         knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
844         knew->private_value = nid;
845
846         knew = via_clone_control(spec, &via_hp_mixer[1]);
847         if (knew == NULL)
848                 return -ENOMEM;
849         knew->subdevice = side_mute_channel(spec);
850
851         return 0;
852 }
853
854 static void notify_aa_path_ctls(struct hda_codec *codec)
855 {
856         int i;
857         struct snd_ctl_elem_id id;
858         const char *labels[] = {"Mic", "Front Mic", "Line", "Rear Mic"};
859         struct snd_kcontrol *ctl;
860
861         memset(&id, 0, sizeof(id));
862         id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
863         for (i = 0; i < ARRAY_SIZE(labels); i++) {
864                 sprintf(id.name, "%s Playback Volume", labels[i]);
865                 ctl = snd_hda_find_mixer_ctl(codec, id.name);
866                 if (ctl)
867                         snd_ctl_notify(codec->bus->card,
868                                         SNDRV_CTL_EVENT_MASK_VALUE,
869                                         &ctl->id);
870         }
871 }
872
873 static void mute_aa_path(struct hda_codec *codec, int mute)
874 {
875         struct via_spec *spec = codec->spec;
876         hda_nid_t  nid_mixer;
877         int start_idx;
878         int end_idx;
879         int i;
880         /* get nid of MW0 and start & end index */
881         switch (spec->codec_type) {
882         case VT1708:
883                 nid_mixer = 0x17;
884                 start_idx = 2;
885                 end_idx = 4;
886                 break;
887         case VT1709_10CH:
888         case VT1709_6CH:
889                 nid_mixer = 0x18;
890                 start_idx = 2;
891                 end_idx = 4;
892                 break;
893         case VT1708B_8CH:
894         case VT1708B_4CH:
895         case VT1708S:
896         case VT1716S:
897                 nid_mixer = 0x16;
898                 start_idx = 2;
899                 end_idx = 4;
900                 break;
901         case VT1718S:
902                 nid_mixer = 0x21;
903                 start_idx = 1;
904                 end_idx = 3;
905                 break;
906         default:
907                 return;
908         }
909         /* check AA path's mute status */
910         for (i = start_idx; i <= end_idx; i++) {
911                 int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
912                 snd_hda_codec_amp_stereo(codec, nid_mixer, HDA_INPUT, i,
913                                          HDA_AMP_MUTE, val);
914         }
915 }
916 static int is_smart51_pins(struct via_spec *spec, hda_nid_t pin)
917 {
918         const struct auto_pin_cfg *cfg = &spec->autocfg;
919         int i;
920
921         for (i = 0; i < cfg->num_inputs; i++) {
922                 if (pin == cfg->inputs[i].pin)
923                         return cfg->inputs[i].type <= AUTO_PIN_LINE_IN;
924         }
925         return 0;
926 }
927
928 static int via_smart51_info(struct snd_kcontrol *kcontrol,
929                             struct snd_ctl_elem_info *uinfo)
930 {
931         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
932         uinfo->count = 1;
933         uinfo->value.integer.min = 0;
934         uinfo->value.integer.max = 1;
935         return 0;
936 }
937
938 static int via_smart51_get(struct snd_kcontrol *kcontrol,
939                            struct snd_ctl_elem_value *ucontrol)
940 {
941         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
942         struct via_spec *spec = codec->spec;
943         const struct auto_pin_cfg *cfg = &spec->autocfg;
944         int on = 1;
945         int i;
946
947         for (i = 0; i < cfg->num_inputs; i++) {
948                 hda_nid_t nid = cfg->inputs[i].pin;
949                 int ctl = snd_hda_codec_read(codec, nid, 0,
950                                              AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
951                 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
952                         continue;
953                 if (cfg->inputs[i].type == AUTO_PIN_MIC &&
954                     spec->hp_independent_mode && spec->codec_type != VT1718S)
955                         continue; /* ignore FMic for independent HP */
956                 if ((ctl & AC_PINCTL_IN_EN) && !(ctl & AC_PINCTL_OUT_EN))
957                         on = 0;
958         }
959         *ucontrol->value.integer.value = on;
960         return 0;
961 }
962
963 static int via_smart51_put(struct snd_kcontrol *kcontrol,
964                            struct snd_ctl_elem_value *ucontrol)
965 {
966         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
967         struct via_spec *spec = codec->spec;
968         const struct auto_pin_cfg *cfg = &spec->autocfg;
969         int out_in = *ucontrol->value.integer.value
970                 ? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
971         int i;
972
973         for (i = 0; i < cfg->num_inputs; i++) {
974                 hda_nid_t nid = cfg->inputs[i].pin;
975                 unsigned int parm;
976
977                 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
978                         continue;
979                 if (cfg->inputs[i].type == AUTO_PIN_MIC &&
980                     spec->hp_independent_mode && spec->codec_type != VT1718S)
981                         continue; /* don't retask FMic for independent HP */
982
983                 parm = snd_hda_codec_read(codec, nid, 0,
984                                           AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
985                 parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
986                 parm |= out_in;
987                 snd_hda_codec_write(codec, nid, 0,
988                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
989                                     parm);
990                 if (out_in == AC_PINCTL_OUT_EN) {
991                         mute_aa_path(codec, 1);
992                         notify_aa_path_ctls(codec);
993                 }
994                 if (spec->codec_type == VT1718S) {
995                         snd_hda_codec_amp_stereo(
996                                         codec, nid, HDA_OUTPUT, 0, HDA_AMP_MUTE,
997                                         HDA_AMP_UNMUTE);
998                 }
999                 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
1000                         if (spec->codec_type == VT1708S
1001                             || spec->codec_type == VT1716S) {
1002                                 /* input = index 1 (AOW3) */
1003                                 snd_hda_codec_write(
1004                                         codec, nid, 0,
1005                                         AC_VERB_SET_CONNECT_SEL, 1);
1006                                 snd_hda_codec_amp_stereo(
1007                                         codec, nid, HDA_OUTPUT,
1008                                         0, HDA_AMP_MUTE, HDA_AMP_UNMUTE);
1009                         }
1010                 }
1011         }
1012         spec->smart51_enabled = *ucontrol->value.integer.value;
1013         set_widgets_power_state(codec);
1014         return 1;
1015 }
1016
1017 static const struct snd_kcontrol_new via_smart51_mixer = {
1018         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1019         .name = "Smart 5.1",
1020         .count = 1,
1021         .info = via_smart51_info,
1022         .get = via_smart51_get,
1023         .put = via_smart51_put,
1024 };
1025
1026 static int via_smart51_build(struct via_spec *spec)
1027 {
1028         struct snd_kcontrol_new *knew;
1029         const struct auto_pin_cfg *cfg = &spec->autocfg;
1030         hda_nid_t nid;
1031         int i;
1032
1033         if (!cfg)
1034                 return 0;
1035         if (cfg->line_outs > 2)
1036                 return 0;
1037
1038         knew = via_clone_control(spec, &via_smart51_mixer);
1039         if (knew == NULL)
1040                 return -ENOMEM;
1041
1042         for (i = 0; i < cfg->num_inputs; i++) {
1043                 nid = cfg->inputs[i].pin;
1044                 if (cfg->inputs[i].type <= AUTO_PIN_LINE_IN) {
1045                         knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
1046                         break;
1047                 }
1048         }
1049
1050         return 0;
1051 }
1052
1053 /* capture mixer elements */
1054 static const struct snd_kcontrol_new vt1708_capture_mixer[] = {
1055         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_INPUT),
1056         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_INPUT),
1057         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x27, 0x0, HDA_INPUT),
1058         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x27, 0x0, HDA_INPUT),
1059         {
1060                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1061                 /* The multiple "Capture Source" controls confuse alsamixer
1062                  * So call somewhat different..
1063                  */
1064                 /* .name = "Capture Source", */
1065                 .name = "Input Source",
1066                 .count = 1,
1067                 .info = via_mux_enum_info,
1068                 .get = via_mux_enum_get,
1069                 .put = via_mux_enum_put,
1070         },
1071         { } /* end */
1072 };
1073
1074 /* check AA path's mute statue */
1075 static int is_aa_path_mute(struct hda_codec *codec)
1076 {
1077         int mute = 1;
1078         hda_nid_t  nid_mixer;
1079         int start_idx;
1080         int end_idx;
1081         int i;
1082         struct via_spec *spec = codec->spec;
1083         /* get nid of MW0 and start & end index */
1084         switch (spec->codec_type) {
1085         case VT1708B_8CH:
1086         case VT1708B_4CH:
1087         case VT1708S:
1088         case VT1716S:
1089                 nid_mixer = 0x16;
1090                 start_idx = 2;
1091                 end_idx = 4;
1092                 break;
1093         case VT1702:
1094                 nid_mixer = 0x1a;
1095                 start_idx = 1;
1096                 end_idx = 3;
1097                 break;
1098         case VT1718S:
1099                 nid_mixer = 0x21;
1100                 start_idx = 1;
1101                 end_idx = 3;
1102                 break;
1103         case VT2002P:
1104         case VT1812:
1105         case VT1802:
1106                 nid_mixer = 0x21;
1107                 start_idx = 0;
1108                 end_idx = 2;
1109                 break;
1110         default:
1111                 return 0;
1112         }
1113         /* check AA path's mute status */
1114         for (i = start_idx; i <= end_idx; i++) {
1115                 unsigned int con_list = snd_hda_codec_read(
1116                         codec, nid_mixer, 0, AC_VERB_GET_CONNECT_LIST, i/4*4);
1117                 int shift = 8 * (i % 4);
1118                 hda_nid_t nid_pin = (con_list & (0xff << shift)) >> shift;
1119                 unsigned int defconf = snd_hda_codec_get_pincfg(codec, nid_pin);
1120                 if (get_defcfg_connect(defconf) == AC_JACK_PORT_COMPLEX) {
1121                         /* check mute status while the pin is connected */
1122                         int mute_l = snd_hda_codec_amp_read(codec, nid_mixer, 0,
1123                                                             HDA_INPUT, i) >> 7;
1124                         int mute_r = snd_hda_codec_amp_read(codec, nid_mixer, 1,
1125                                                             HDA_INPUT, i) >> 7;
1126                         if (!mute_l || !mute_r) {
1127                                 mute = 0;
1128                                 break;
1129                         }
1130                 }
1131         }
1132         return mute;
1133 }
1134
1135 /* enter/exit analog low-current mode */
1136 static void analog_low_current_mode(struct hda_codec *codec, int stream_idle)
1137 {
1138         struct via_spec *spec = codec->spec;
1139         static int saved_stream_idle = 1; /* saved stream idle status */
1140         int enable = is_aa_path_mute(codec);
1141         unsigned int verb = 0;
1142         unsigned int parm = 0;
1143
1144         if (stream_idle == -1)  /* stream status did not change */
1145                 enable = enable && saved_stream_idle;
1146         else {
1147                 enable = enable && stream_idle;
1148                 saved_stream_idle = stream_idle;
1149         }
1150
1151         /* decide low current mode's verb & parameter */
1152         switch (spec->codec_type) {
1153         case VT1708B_8CH:
1154         case VT1708B_4CH:
1155                 verb = 0xf70;
1156                 parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
1157                 break;
1158         case VT1708S:
1159         case VT1718S:
1160         case VT1716S:
1161                 verb = 0xf73;
1162                 parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
1163                 break;
1164         case VT1702:
1165                 verb = 0xf73;
1166                 parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
1167                 break;
1168         case VT2002P:
1169         case VT1812:
1170         case VT1802:
1171                 verb = 0xf93;
1172                 parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
1173                 break;
1174         default:
1175                 return;         /* other codecs are not supported */
1176         }
1177         /* send verb */
1178         snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
1179 }
1180
1181 /*
1182  * generic initialization of ADC, input mixers and output mixers
1183  */
1184 static const struct hda_verb vt1708_volume_init_verbs[] = {
1185         /*
1186          * Unmute ADC0-1 and set the default input to mic-in
1187          */
1188         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1189         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1190
1191
1192         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
1193          * mixer widget
1194          */
1195         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
1196         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1197         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1198         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
1199         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
1200         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
1201
1202         /*
1203          * Set up output mixers (0x19 - 0x1b)
1204          */
1205         /* set vol=0 to output mixers */
1206         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1207         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1208         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1209
1210         /* Setup default input MW0 to PW4 */
1211         {0x20, AC_VERB_SET_CONNECT_SEL, 0},
1212         /* PW9 Output enable */
1213         {0x25, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
1214         /* power down jack detect function */
1215         {0x1, 0xf81, 0x1},
1216         { }
1217 };
1218
1219 static int via_playback_pcm_open(struct hda_pcm_stream *hinfo,
1220                                  struct hda_codec *codec,
1221                                  struct snd_pcm_substream *substream)
1222 {
1223         struct via_spec *spec = codec->spec;
1224         int idle = substream->pstr->substream_opened == 1
1225                 && substream->ref_count == 0;
1226         analog_low_current_mode(codec, idle);
1227         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
1228                                              hinfo);
1229 }
1230
1231 static void playback_multi_pcm_prep_0(struct hda_codec *codec,
1232                                       unsigned int stream_tag,
1233                                       unsigned int format,
1234                                       struct snd_pcm_substream *substream)
1235 {
1236         struct via_spec *spec = codec->spec;
1237         struct hda_multi_out *mout = &spec->multiout;
1238         const hda_nid_t *nids = mout->dac_nids;
1239         int chs = substream->runtime->channels;
1240         int i;
1241         struct hda_spdif_out *spdif =
1242                 snd_hda_spdif_out_of_nid(codec, spec->multiout.dig_out_nid);
1243
1244         mutex_lock(&codec->spdif_mutex);
1245         if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1246                 if (chs == 2 &&
1247                     snd_hda_is_supported_format(codec, mout->dig_out_nid,
1248                                                 format) &&
1249                     !(spdif->status & IEC958_AES0_NONAUDIO)) {
1250                         mout->dig_out_used = HDA_DIG_ANALOG_DUP;
1251                         /* turn off SPDIF once; otherwise the IEC958 bits won't
1252                          * be updated */
1253                         if (spdif->ctls & AC_DIG1_ENABLE)
1254                                 snd_hda_codec_write(codec, mout->dig_out_nid, 0,
1255                                                     AC_VERB_SET_DIGI_CONVERT_1,
1256                                                     spdif->ctls &
1257                                                         ~AC_DIG1_ENABLE & 0xff);
1258                         snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1259                                                    stream_tag, 0, format);
1260                         /* turn on again (if needed) */
1261                         if (spdif->ctls & AC_DIG1_ENABLE)
1262                                 snd_hda_codec_write(codec, mout->dig_out_nid, 0,
1263                                                     AC_VERB_SET_DIGI_CONVERT_1,
1264                                                     spdif->ctls & 0xff);
1265                 } else {
1266                         mout->dig_out_used = 0;
1267                         snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1268                                                    0, 0, 0);
1269                 }
1270         }
1271         mutex_unlock(&codec->spdif_mutex);
1272
1273         /* front */
1274         snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
1275                                    0, format);
1276
1277         if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]
1278             && !spec->hp_independent_mode)
1279                 /* headphone out will just decode front left/right (stereo) */
1280                 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
1281                                            0, format);
1282
1283         /* extra outputs copied from front */
1284         for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1285                 if (mout->extra_out_nid[i])
1286                         snd_hda_codec_setup_stream(codec,
1287                                                    mout->extra_out_nid[i],
1288                                                    stream_tag, 0, format);
1289
1290         /* surrounds */
1291         for (i = 1; i < mout->num_dacs; i++) {
1292                 if (chs >= (i + 1) * 2) /* independent out */
1293                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
1294                                                    i * 2, format);
1295                 else /* copy front */
1296                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
1297                                                    0, format);
1298         }
1299 }
1300
1301 static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
1302                                           struct hda_codec *codec,
1303                                           unsigned int stream_tag,
1304                                           unsigned int format,
1305                                           struct snd_pcm_substream *substream)
1306 {
1307         struct via_spec *spec = codec->spec;
1308         struct hda_multi_out *mout = &spec->multiout;
1309         const hda_nid_t *nids = mout->dac_nids;
1310
1311         if (substream->number == 0)
1312                 playback_multi_pcm_prep_0(codec, stream_tag, format,
1313                                           substream);
1314         else {
1315                 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
1316                     spec->hp_independent_mode)
1317                         snd_hda_codec_setup_stream(codec, mout->hp_nid,
1318                                                    stream_tag, 0, format);
1319         }
1320         vt1708_start_hp_work(spec);
1321         return 0;
1322 }
1323
1324 static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
1325                                     struct hda_codec *codec,
1326                                     struct snd_pcm_substream *substream)
1327 {
1328         struct via_spec *spec = codec->spec;
1329         struct hda_multi_out *mout = &spec->multiout;
1330         const hda_nid_t *nids = mout->dac_nids;
1331         int i;
1332
1333         if (substream->number == 0) {
1334                 for (i = 0; i < mout->num_dacs; i++)
1335                         snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
1336
1337                 if (mout->hp_nid && !spec->hp_independent_mode)
1338                         snd_hda_codec_setup_stream(codec, mout->hp_nid,
1339                                                    0, 0, 0);
1340
1341                 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1342                         if (mout->extra_out_nid[i])
1343                                 snd_hda_codec_setup_stream(codec,
1344                                                         mout->extra_out_nid[i],
1345                                                         0, 0, 0);
1346                 mutex_lock(&codec->spdif_mutex);
1347                 if (mout->dig_out_nid &&
1348                     mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
1349                         snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1350                                                    0, 0, 0);
1351                         mout->dig_out_used = 0;
1352                 }
1353                 mutex_unlock(&codec->spdif_mutex);
1354         } else {
1355                 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
1356                     spec->hp_independent_mode)
1357                         snd_hda_codec_setup_stream(codec, mout->hp_nid,
1358                                                    0, 0, 0);
1359         }
1360         vt1708_stop_hp_work(spec);
1361         return 0;
1362 }
1363
1364 /*
1365  * Digital out
1366  */
1367 static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1368                                      struct hda_codec *codec,
1369                                      struct snd_pcm_substream *substream)
1370 {
1371         struct via_spec *spec = codec->spec;
1372         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1373 }
1374
1375 static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1376                                       struct hda_codec *codec,
1377                                       struct snd_pcm_substream *substream)
1378 {
1379         struct via_spec *spec = codec->spec;
1380         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1381 }
1382
1383 static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1384                                         struct hda_codec *codec,
1385                                         unsigned int stream_tag,
1386                                         unsigned int format,
1387                                         struct snd_pcm_substream *substream)
1388 {
1389         struct via_spec *spec = codec->spec;
1390         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1391                                              stream_tag, format, substream);
1392 }
1393
1394 static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1395                                         struct hda_codec *codec,
1396                                         struct snd_pcm_substream *substream)
1397 {
1398         struct via_spec *spec = codec->spec;
1399         snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
1400         return 0;
1401 }
1402
1403 /*
1404  * Analog capture
1405  */
1406 static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1407                                    struct hda_codec *codec,
1408                                    unsigned int stream_tag,
1409                                    unsigned int format,
1410                                    struct snd_pcm_substream *substream)
1411 {
1412         struct via_spec *spec = codec->spec;
1413
1414         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1415                                    stream_tag, 0, format);
1416         return 0;
1417 }
1418
1419 static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1420                                    struct hda_codec *codec,
1421                                    struct snd_pcm_substream *substream)
1422 {
1423         struct via_spec *spec = codec->spec;
1424         snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
1425         return 0;
1426 }
1427
1428 static const struct hda_pcm_stream vt1708_pcm_analog_playback = {
1429         .substreams = 2,
1430         .channels_min = 2,
1431         .channels_max = 8,
1432         .nid = 0x10, /* NID to query formats and rates */
1433         .ops = {
1434                 .open = via_playback_pcm_open,
1435                 .prepare = via_playback_multi_pcm_prepare,
1436                 .cleanup = via_playback_multi_pcm_cleanup
1437         },
1438 };
1439
1440 static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
1441         .substreams = 2,
1442         .channels_min = 2,
1443         .channels_max = 8,
1444         .nid = 0x10, /* NID to query formats and rates */
1445         /* We got noisy outputs on the right channel on VT1708 when
1446          * 24bit samples are used.  Until any workaround is found,
1447          * disable the 24bit format, so far.
1448          */
1449         .formats = SNDRV_PCM_FMTBIT_S16_LE,
1450         .ops = {
1451                 .open = via_playback_pcm_open,
1452                 .prepare = via_playback_multi_pcm_prepare,
1453                 .cleanup = via_playback_multi_pcm_cleanup
1454         },
1455 };
1456
1457 static const struct hda_pcm_stream vt1708_pcm_analog_capture = {
1458         .substreams = 2,
1459         .channels_min = 2,
1460         .channels_max = 2,
1461         .nid = 0x15, /* NID to query formats and rates */
1462         .ops = {
1463                 .prepare = via_capture_pcm_prepare,
1464                 .cleanup = via_capture_pcm_cleanup
1465         },
1466 };
1467
1468 static const struct hda_pcm_stream vt1708_pcm_digital_playback = {
1469         .substreams = 1,
1470         .channels_min = 2,
1471         .channels_max = 2,
1472         /* NID is set in via_build_pcms */
1473         .ops = {
1474                 .open = via_dig_playback_pcm_open,
1475                 .close = via_dig_playback_pcm_close,
1476                 .prepare = via_dig_playback_pcm_prepare,
1477                 .cleanup = via_dig_playback_pcm_cleanup
1478         },
1479 };
1480
1481 static const struct hda_pcm_stream vt1708_pcm_digital_capture = {
1482         .substreams = 1,
1483         .channels_min = 2,
1484         .channels_max = 2,
1485 };
1486
1487 static int via_build_controls(struct hda_codec *codec)
1488 {
1489         struct via_spec *spec = codec->spec;
1490         struct snd_kcontrol *kctl;
1491         const struct snd_kcontrol_new *knew;
1492         int err, i;
1493
1494         if (spec->set_widgets_power_state)
1495                 if (!via_clone_control(spec, &via_pin_power_ctl_enum))
1496                         return -ENOMEM;
1497
1498         for (i = 0; i < spec->num_mixers; i++) {
1499                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1500                 if (err < 0)
1501                         return err;
1502         }
1503
1504         if (spec->multiout.dig_out_nid) {
1505                 err = snd_hda_create_spdif_out_ctls(codec,
1506                                                     spec->multiout.dig_out_nid,
1507                                                     spec->multiout.dig_out_nid);
1508                 if (err < 0)
1509                         return err;
1510                 err = snd_hda_create_spdif_share_sw(codec,
1511                                                     &spec->multiout);
1512                 if (err < 0)
1513                         return err;
1514                 spec->multiout.share_spdif = 1;
1515         }
1516         if (spec->dig_in_nid) {
1517                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1518                 if (err < 0)
1519                         return err;
1520         }
1521
1522         /* assign Capture Source enums to NID */
1523         kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1524         for (i = 0; kctl && i < kctl->count; i++) {
1525                 err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
1526                 if (err < 0)
1527                         return err;
1528         }
1529
1530         /* other nid->control mapping */
1531         for (i = 0; i < spec->num_mixers; i++) {
1532                 for (knew = spec->mixers[i]; knew->name; knew++) {
1533                         if (knew->iface != NID_MAPPING)
1534                                 continue;
1535                         kctl = snd_hda_find_mixer_ctl(codec, knew->name);
1536                         if (kctl == NULL)
1537                                 continue;
1538                         err = snd_hda_add_nid(codec, kctl, 0,
1539                                               knew->subdevice);
1540                 }
1541         }
1542
1543         /* init power states */
1544         set_widgets_power_state(codec);
1545         analog_low_current_mode(codec, 1);
1546
1547         via_free_kctls(codec); /* no longer needed */
1548         return 0;
1549 }
1550
1551 static int via_build_pcms(struct hda_codec *codec)
1552 {
1553         struct via_spec *spec = codec->spec;
1554         struct hda_pcm *info = spec->pcm_rec;
1555
1556         codec->num_pcms = 1;
1557         codec->pcm_info = info;
1558
1559         snprintf(spec->stream_name_analog, sizeof(spec->stream_name_analog),
1560                  "%s Analog", codec->chip_name);
1561         info->name = spec->stream_name_analog;
1562         info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1563                 *(spec->stream_analog_playback);
1564         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1565                 spec->multiout.dac_nids[0];
1566         info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_analog_capture);
1567         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
1568
1569         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1570                 spec->multiout.max_channels;
1571
1572         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1573                 codec->num_pcms++;
1574                 info++;
1575                 snprintf(spec->stream_name_digital,
1576                          sizeof(spec->stream_name_digital),
1577                          "%s Digital", codec->chip_name);
1578                 info->name = spec->stream_name_digital;
1579                 info->pcm_type = HDA_PCM_TYPE_SPDIF;
1580                 if (spec->multiout.dig_out_nid) {
1581                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1582                                 *(spec->stream_digital_playback);
1583                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1584                                 spec->multiout.dig_out_nid;
1585                 }
1586                 if (spec->dig_in_nid) {
1587                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1588                                 *(spec->stream_digital_capture);
1589                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1590                                 spec->dig_in_nid;
1591                 }
1592         }
1593
1594         return 0;
1595 }
1596
1597 static void via_free(struct hda_codec *codec)
1598 {
1599         struct via_spec *spec = codec->spec;
1600
1601         if (!spec)
1602                 return;
1603
1604         via_free_kctls(codec);
1605         vt1708_stop_hp_work(spec);
1606         kfree(codec->spec);
1607 }
1608
1609 /* mute internal speaker if HP is plugged */
1610 static void via_hp_automute(struct hda_codec *codec)
1611 {
1612         unsigned int present = 0;
1613         struct via_spec *spec = codec->spec;
1614
1615         present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
1616
1617         if (!spec->hp_independent_mode) {
1618                 /* auto mute */
1619                 snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
1620                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1621                                     present ? 0 : PIN_OUT);
1622         }
1623 }
1624
1625 /* mute mono out if HP or Line out is plugged */
1626 static void via_mono_automute(struct hda_codec *codec)
1627 {
1628         unsigned int hp_present, lineout_present;
1629         struct via_spec *spec = codec->spec;
1630
1631         if (spec->codec_type != VT1716S)
1632                 return;
1633
1634         lineout_present = snd_hda_jack_detect(codec,
1635                                               spec->autocfg.line_out_pins[0]);
1636
1637         /* Mute Mono Out if Line Out is plugged */
1638         if (lineout_present) {
1639                 snd_hda_codec_write(codec, 0x2A, 0,
1640                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1641                                     lineout_present ? 0 : PIN_OUT);
1642                 return;
1643         }
1644
1645         hp_present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
1646
1647         if (!spec->hp_independent_mode)
1648                 snd_hda_codec_write(codec, 0x2A, 0,
1649                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1650                                     hp_present ? 0 : PIN_OUT);
1651 }
1652
1653 static void via_gpio_control(struct hda_codec *codec)
1654 {
1655         unsigned int gpio_data;
1656         unsigned int vol_counter;
1657         unsigned int vol;
1658         unsigned int master_vol;
1659
1660         struct via_spec *spec = codec->spec;
1661
1662         gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
1663                                        AC_VERB_GET_GPIO_DATA, 0) & 0x03;
1664
1665         vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
1666                                           0xF84, 0) & 0x3F0000) >> 16;
1667
1668         vol = vol_counter & 0x1F;
1669         master_vol = snd_hda_codec_read(codec, 0x1A, 0,
1670                                         AC_VERB_GET_AMP_GAIN_MUTE,
1671                                         AC_AMP_GET_INPUT);
1672
1673         if (gpio_data == 0x02) {
1674                 /* unmute line out */
1675                 snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
1676                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1677                                     PIN_OUT);
1678                 if (vol_counter & 0x20) {
1679                         /* decrease volume */
1680                         if (vol > master_vol)
1681                                 vol = master_vol;
1682                         snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
1683                                                  0, HDA_AMP_VOLMASK,
1684                                                  master_vol-vol);
1685                 } else {
1686                         /* increase volume */
1687                         snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
1688                                          HDA_AMP_VOLMASK,
1689                                          ((master_vol+vol) > 0x2A) ? 0x2A :
1690                                           (master_vol+vol));
1691                 }
1692         } else if (!(gpio_data & 0x02)) {
1693                 /* mute line out */
1694                 snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
1695                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1696                                     0);
1697         }
1698 }
1699
1700 /* mute Internal-Speaker if HP is plugged */
1701 static void via_speaker_automute(struct hda_codec *codec)
1702 {
1703         unsigned int hp_present;
1704         struct via_spec *spec = codec->spec;
1705
1706         if (!VT2002P_COMPATIBLE(spec))
1707                 return;
1708
1709         hp_present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
1710
1711         if (!spec->hp_independent_mode) {
1712                 snd_hda_codec_write(codec, spec->autocfg.speaker_pins[0], 0,
1713                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1714                                     hp_present ? 0 : PIN_OUT);
1715         }
1716 }
1717
1718 /* mute line-out and internal speaker if HP is plugged */
1719 static void via_hp_bind_automute(struct hda_codec *codec)
1720 {
1721         /* use long instead of int below just to avoid an internal compiler
1722          * error with gcc 4.0.x
1723          */
1724         unsigned long hp_present, present = 0;
1725         struct via_spec *spec = codec->spec;
1726         int i;
1727
1728         if (!spec->autocfg.hp_pins[0] || !spec->autocfg.line_out_pins[0])
1729                 return;
1730
1731         hp_present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
1732
1733         present = snd_hda_jack_detect(codec, spec->autocfg.line_out_pins[0]);
1734
1735         if (!spec->hp_independent_mode) {
1736                 /* Mute Line-Outs */
1737                 for (i = 0; i < spec->autocfg.line_outs; i++)
1738                         snd_hda_codec_write(codec,
1739                                             spec->autocfg.line_out_pins[i], 0,
1740                                             AC_VERB_SET_PIN_WIDGET_CONTROL,
1741                                             hp_present ? 0 : PIN_OUT);
1742                 if (hp_present)
1743                         present = hp_present;
1744         }
1745         /* Speakers */
1746         for (i = 0; i < spec->autocfg.speaker_outs; i++)
1747                 snd_hda_codec_write(codec, spec->autocfg.speaker_pins[i], 0,
1748                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1749                                     present ? 0 : PIN_OUT);
1750 }
1751
1752
1753 /* unsolicited event for jack sensing */
1754 static void via_unsol_event(struct hda_codec *codec,
1755                                   unsigned int res)
1756 {
1757         res >>= 26;
1758
1759         if (res & VIA_JACK_EVENT)
1760                 set_widgets_power_state(codec);
1761
1762         res &= ~VIA_JACK_EVENT;
1763
1764         if (res == VIA_HP_EVENT)
1765                 via_hp_automute(codec);
1766         else if (res == VIA_GPIO_EVENT)
1767                 via_gpio_control(codec);
1768         else if (res == VIA_MONO_EVENT)
1769                 via_mono_automute(codec);
1770         else if (res == VIA_SPEAKER_EVENT)
1771                 via_speaker_automute(codec);
1772         else if (res == VIA_BIND_HP_EVENT)
1773                 via_hp_bind_automute(codec);
1774 }
1775
1776 static int via_init(struct hda_codec *codec)
1777 {
1778         struct via_spec *spec = codec->spec;
1779         int i;
1780         for (i = 0; i < spec->num_iverbs; i++)
1781                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
1782
1783         /* Lydia Add for EAPD enable */
1784         if (!spec->dig_in_nid) { /* No Digital In connection */
1785                 if (spec->dig_in_pin) {
1786                         snd_hda_codec_write(codec, spec->dig_in_pin, 0,
1787                                             AC_VERB_SET_PIN_WIDGET_CONTROL,
1788                                             PIN_OUT);
1789                         snd_hda_codec_write(codec, spec->dig_in_pin, 0,
1790                                             AC_VERB_SET_EAPD_BTLENABLE, 0x02);
1791                 }
1792         } else /* enable SPDIF-input pin */
1793                 snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
1794                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
1795
1796         /* assign slave outs */
1797         if (spec->slave_dig_outs[0])
1798                 codec->slave_dig_outs = spec->slave_dig_outs;
1799
1800         return 0;
1801 }
1802
1803 #ifdef SND_HDA_NEEDS_RESUME
1804 static int via_suspend(struct hda_codec *codec, pm_message_t state)
1805 {
1806         struct via_spec *spec = codec->spec;
1807         vt1708_stop_hp_work(spec);
1808         return 0;
1809 }
1810 #endif
1811
1812 #ifdef CONFIG_SND_HDA_POWER_SAVE
1813 static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1814 {
1815         struct via_spec *spec = codec->spec;
1816         return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1817 }
1818 #endif
1819
1820 /*
1821  */
1822 static const struct hda_codec_ops via_patch_ops = {
1823         .build_controls = via_build_controls,
1824         .build_pcms = via_build_pcms,
1825         .init = via_init,
1826         .free = via_free,
1827 #ifdef SND_HDA_NEEDS_RESUME
1828         .suspend = via_suspend,
1829 #endif
1830 #ifdef CONFIG_SND_HDA_POWER_SAVE
1831         .check_power_status = via_check_power_status,
1832 #endif
1833 };
1834
1835 /* fill in the dac_nids table from the parsed pin configuration */
1836 static int vt1708_auto_fill_dac_nids(struct via_spec *spec,
1837                                      const struct auto_pin_cfg *cfg)
1838 {
1839         int i;
1840         hda_nid_t nid;
1841
1842         spec->multiout.num_dacs = cfg->line_outs;
1843
1844         spec->multiout.dac_nids = spec->private_dac_nids;
1845
1846         for (i = 0; i < 4; i++) {
1847                 nid = cfg->line_out_pins[i];
1848                 if (nid) {
1849                         /* config dac list */
1850                         switch (i) {
1851                         case AUTO_SEQ_FRONT:
1852                                 spec->private_dac_nids[i] = 0x10;
1853                                 break;
1854                         case AUTO_SEQ_CENLFE:
1855                                 spec->private_dac_nids[i] = 0x12;
1856                                 break;
1857                         case AUTO_SEQ_SURROUND:
1858                                 spec->private_dac_nids[i] = 0x11;
1859                                 break;
1860                         case AUTO_SEQ_SIDE:
1861                                 spec->private_dac_nids[i] = 0x13;
1862                                 break;
1863                         }
1864                 }
1865         }
1866
1867         return 0;
1868 }
1869
1870 /* add playback controls from the parsed DAC table */
1871 static int vt1708_auto_create_multi_out_ctls(struct via_spec *spec,
1872                                              const struct auto_pin_cfg *cfg)
1873 {
1874         char name[32];
1875         static const char * const chname[4] = {
1876                 "Front", "Surround", "C/LFE", "Side"
1877         };
1878         hda_nid_t nid, nid_vol, nid_vols[] = {0x17, 0x19, 0x1a, 0x1b};
1879         int i, err;
1880
1881         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
1882                 nid = cfg->line_out_pins[i];
1883
1884                 if (!nid)
1885                         continue;
1886
1887                 nid_vol = nid_vols[i];
1888
1889                 if (i == AUTO_SEQ_CENLFE) {
1890                         /* Center/LFE */
1891                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1892                                         "Center Playback Volume",
1893                                         HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
1894                                                             HDA_OUTPUT));
1895                         if (err < 0)
1896                                 return err;
1897                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1898                                               "LFE Playback Volume",
1899                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
1900                                                                   HDA_OUTPUT));
1901                         if (err < 0)
1902                                 return err;
1903                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1904                                               "Center Playback Switch",
1905                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
1906                                                                   HDA_OUTPUT));
1907                         if (err < 0)
1908                                 return err;
1909                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1910                                               "LFE Playback Switch",
1911                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
1912                                                                   HDA_OUTPUT));
1913                         if (err < 0)
1914                                 return err;
1915                 } else if (i == AUTO_SEQ_FRONT) {
1916                         /* add control to mixer index 0 */
1917                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1918                                               "Master Front Playback Volume",
1919                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1920                                                                   HDA_INPUT));
1921                         if (err < 0)
1922                                 return err;
1923                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1924                                               "Master Front Playback Switch",
1925                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1926                                                                   HDA_INPUT));
1927                         if (err < 0)
1928                                 return err;
1929
1930                         /* add control to PW3 */
1931                         sprintf(name, "%s Playback Volume", chname[i]);
1932                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1933                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1934                                                                   HDA_OUTPUT));
1935                         if (err < 0)
1936                                 return err;
1937                         sprintf(name, "%s Playback Switch", chname[i]);
1938                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1939                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1940                                                                   HDA_OUTPUT));
1941                         if (err < 0)
1942                                 return err;
1943                 } else {
1944                         sprintf(name, "%s Playback Volume", chname[i]);
1945                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1946                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1947                                                                   HDA_OUTPUT));
1948                         if (err < 0)
1949                                 return err;
1950                         sprintf(name, "%s Playback Switch", chname[i]);
1951                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1952                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1953                                                                   HDA_OUTPUT));
1954                         if (err < 0)
1955                                 return err;
1956                 }
1957         }
1958
1959         return 0;
1960 }
1961
1962 static void create_hp_imux(struct via_spec *spec)
1963 {
1964         int i;
1965         struct hda_input_mux *imux = &spec->private_imux[1];
1966         static const char * const texts[] = { "OFF", "ON", NULL};
1967
1968         /* for hp mode select */
1969         for (i = 0; texts[i]; i++)
1970                 snd_hda_add_imux_item(imux, texts[i], i, NULL);
1971
1972         spec->hp_mux = &spec->private_imux[1];
1973 }
1974
1975 static int vt1708_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
1976 {
1977         int err;
1978
1979         if (!pin)
1980                 return 0;
1981
1982         spec->multiout.hp_nid = VT1708_HP_NID; /* AOW3 */
1983         spec->hp_independent_mode_index = 1;
1984
1985         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1986                               "Headphone Playback Volume",
1987                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1988         if (err < 0)
1989                 return err;
1990         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1991                               "Headphone Playback Switch",
1992                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1993         if (err < 0)
1994                 return err;
1995
1996         create_hp_imux(spec);
1997
1998         return 0;
1999 }
2000
2001 static int get_connection_index(struct hda_codec *codec, hda_nid_t mux,
2002                                 hda_nid_t nid)
2003 {
2004         hda_nid_t conn[HDA_MAX_NUM_INPUTS];
2005         int i, nums;
2006
2007         nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
2008         for (i = 0; i < nums; i++)
2009                 if (conn[i] == nid)
2010                         return i;
2011         return -1;
2012 }
2013
2014 /* look for ADCs */
2015 static int via_fill_adcs(struct hda_codec *codec)
2016 {
2017         struct via_spec *spec = codec->spec;
2018         hda_nid_t nid = codec->start_nid;
2019         int i;
2020
2021         for (i = 0; i < codec->num_nodes; i++, nid++) {
2022                 unsigned int wcaps = get_wcaps(codec, nid);
2023                 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2024                         continue;
2025                 if (wcaps & AC_WCAP_DIGITAL)
2026                         continue;
2027                 if (!(wcaps & AC_WCAP_CONN_LIST))
2028                         continue;
2029                 if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
2030                         return -ENOMEM;
2031                 spec->adc_nids[spec->num_adc_nids++] = nid;
2032         }
2033         return 0;
2034 }
2035
2036 static int get_mux_nids(struct hda_codec *codec);
2037
2038 /* create playback/capture controls for input pins */
2039 static int vt_auto_create_analog_input_ctls(struct hda_codec *codec,
2040                                             const struct auto_pin_cfg *cfg,
2041                                             hda_nid_t mix_nid)
2042 {
2043         struct via_spec *spec = codec->spec;
2044         struct hda_input_mux *imux = &spec->private_imux[0];
2045         int i, err, idx, idx2, type, type_idx = 0;
2046         hda_nid_t cap_nid;
2047         hda_nid_t pin_idxs[8];
2048         int num_idxs;
2049
2050         err = via_fill_adcs(codec);
2051         if (err < 0)
2052                 return err;
2053         err = get_mux_nids(codec);
2054         if (err < 0)
2055                 return err;
2056         cap_nid = spec->mux_nids[0];
2057
2058         num_idxs = snd_hda_get_connections(codec, cap_nid, pin_idxs,
2059                                            ARRAY_SIZE(pin_idxs));
2060         if (num_idxs <= 0)
2061                 return 0;
2062
2063         /* for internal loopback recording select */
2064         for (idx = 0; idx < num_idxs; idx++) {
2065                 if (pin_idxs[idx] == mix_nid) {
2066                         snd_hda_add_imux_item(imux, "Stereo Mixer", idx, NULL);
2067                         break;
2068                 }
2069         }
2070
2071         for (i = 0; i < cfg->num_inputs; i++) {
2072                 const char *label;
2073                 type = cfg->inputs[i].type;
2074                 for (idx = 0; idx < num_idxs; idx++)
2075                         if (pin_idxs[idx] == cfg->inputs[i].pin)
2076                                 break;
2077                 if (idx >= num_idxs)
2078                         continue;
2079                 if (i > 0 && type == cfg->inputs[i - 1].type)
2080                         type_idx++;
2081                 else
2082                         type_idx = 0;
2083                 label = hda_get_autocfg_input_label(codec, cfg, i);
2084                 idx2 = get_connection_index(codec, mix_nid, pin_idxs[idx]);
2085                 if (idx2 >= 0)
2086                         err = via_new_analog_input(spec, label, type_idx,
2087                                                    idx2, mix_nid);
2088                 if (err < 0)
2089                         return err;
2090                 snd_hda_add_imux_item(imux, label, idx, NULL);
2091         }
2092         return 0;
2093 }
2094
2095 /* create playback/capture controls for input pins */
2096 static int vt1708_auto_create_analog_input_ctls(struct hda_codec *codec,
2097                                                 const struct auto_pin_cfg *cfg)
2098 {
2099         return vt_auto_create_analog_input_ctls(codec, cfg, 0x17);
2100 }
2101
2102 #ifdef CONFIG_SND_HDA_POWER_SAVE
2103 static const struct hda_amp_list vt1708_loopbacks[] = {
2104         { 0x17, HDA_INPUT, 1 },
2105         { 0x17, HDA_INPUT, 2 },
2106         { 0x17, HDA_INPUT, 3 },
2107         { 0x17, HDA_INPUT, 4 },
2108         { } /* end */
2109 };
2110 #endif
2111
2112 static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
2113 {
2114         unsigned int def_conf;
2115         unsigned char seqassoc;
2116
2117         def_conf = snd_hda_codec_get_pincfg(codec, nid);
2118         seqassoc = (unsigned char) get_defcfg_association(def_conf);
2119         seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
2120         if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
2121             && (seqassoc == 0xf0 || seqassoc == 0xff)) {
2122                 def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
2123                 snd_hda_codec_set_pincfg(codec, nid, def_conf);
2124         }
2125
2126         return;
2127 }
2128
2129 static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
2130                                      struct snd_ctl_elem_value *ucontrol)
2131 {
2132         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2133         struct via_spec *spec = codec->spec;
2134
2135         if (spec->codec_type != VT1708)
2136                 return 0;
2137         spec->vt1708_jack_detect =
2138                 !((snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8) & 0x1);
2139         ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
2140         return 0;
2141 }
2142
2143 static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
2144                                      struct snd_ctl_elem_value *ucontrol)
2145 {
2146         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2147         struct via_spec *spec = codec->spec;
2148         int change;
2149
2150         if (spec->codec_type != VT1708)
2151                 return 0;
2152         spec->vt1708_jack_detect = ucontrol->value.integer.value[0];
2153         change = (0x1 & (snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8))
2154                 == !spec->vt1708_jack_detect;
2155         if (spec->vt1708_jack_detect) {
2156                 mute_aa_path(codec, 1);
2157                 notify_aa_path_ctls(codec);
2158         }
2159         return change;
2160 }
2161
2162 static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
2163         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2164         .name = "Jack Detect",
2165         .count = 1,
2166         .info = snd_ctl_boolean_mono_info,
2167         .get = vt1708_jack_detect_get,
2168         .put = vt1708_jack_detect_put,
2169 };
2170
2171 static int vt1708_parse_auto_config(struct hda_codec *codec)
2172 {
2173         struct via_spec *spec = codec->spec;
2174         int err;
2175
2176         /* Add HP and CD pin config connect bit re-config action */
2177         vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
2178         vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
2179
2180         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2181         if (err < 0)
2182                 return err;
2183         err = vt1708_auto_fill_dac_nids(spec, &spec->autocfg);
2184         if (err < 0)
2185                 return err;
2186         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2187                 return 0; /* can't find valid BIOS pin config */
2188
2189         err = vt1708_auto_create_multi_out_ctls(spec, &spec->autocfg);
2190         if (err < 0)
2191                 return err;
2192         err = vt1708_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2193         if (err < 0)
2194                 return err;
2195         err = vt1708_auto_create_analog_input_ctls(codec, &spec->autocfg);
2196         if (err < 0)
2197                 return err;
2198         /* add jack detect on/off control */
2199         if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
2200                 return -ENOMEM;
2201
2202         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2203
2204         if (spec->autocfg.dig_outs)
2205                 spec->multiout.dig_out_nid = VT1708_DIGOUT_NID;
2206         spec->dig_in_pin = VT1708_DIGIN_PIN;
2207         if (spec->autocfg.dig_in_pin)
2208                 spec->dig_in_nid = VT1708_DIGIN_NID;
2209
2210         if (spec->kctls.list)
2211                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
2212
2213         spec->init_verbs[spec->num_iverbs++] = vt1708_volume_init_verbs;
2214
2215         spec->input_mux = &spec->private_imux[0];
2216
2217         if (spec->hp_mux)
2218                 via_hp_build(codec);
2219
2220         via_smart51_build(spec);
2221         return 1;
2222 }
2223
2224 /* init callback for auto-configuration model -- overriding the default init */
2225 static int via_auto_init(struct hda_codec *codec)
2226 {
2227         struct via_spec *spec = codec->spec;
2228
2229         via_init(codec);
2230         via_auto_init_multi_out(codec);
2231         via_auto_init_hp_out(codec);
2232         via_auto_init_analog_input(codec);
2233
2234         if (VT2002P_COMPATIBLE(spec)) {
2235                 via_hp_bind_automute(codec);
2236         } else {
2237                 via_hp_automute(codec);
2238                 via_speaker_automute(codec);
2239         }
2240
2241         return 0;
2242 }
2243
2244 static void vt1708_update_hp_jack_state(struct work_struct *work)
2245 {
2246         struct via_spec *spec = container_of(work, struct via_spec,
2247                                              vt1708_hp_work.work);
2248         if (spec->codec_type != VT1708)
2249                 return;
2250         /* if jack state toggled */
2251         if (spec->vt1708_hp_present
2252             != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
2253                 spec->vt1708_hp_present ^= 1;
2254                 via_hp_automute(spec->codec);
2255         }
2256         vt1708_start_hp_work(spec);
2257 }
2258
2259 static int get_mux_nids(struct hda_codec *codec)
2260 {
2261         struct via_spec *spec = codec->spec;
2262         hda_nid_t nid, conn[8];
2263         unsigned int type;
2264         int i, n;
2265
2266         for (i = 0; i < spec->num_adc_nids; i++) {
2267                 nid = spec->adc_nids[i];
2268                 while (nid) {
2269                         type = get_wcaps_type(get_wcaps(codec, nid));
2270                         if (type == AC_WID_PIN)
2271                                 break;
2272                         n = snd_hda_get_connections(codec, nid, conn,
2273                                                     ARRAY_SIZE(conn));
2274                         if (n <= 0)
2275                                 break;
2276                         if (n > 1) {
2277                                 spec->mux_nids[i] = nid;
2278                                 break;
2279                         }
2280                         nid = conn[0];
2281                 }
2282         }
2283         return 0;
2284 }
2285
2286 static int patch_vt1708(struct hda_codec *codec)
2287 {
2288         struct via_spec *spec;
2289         int err;
2290
2291         /* create a codec specific record */
2292         spec = via_new_spec(codec);
2293         if (spec == NULL)
2294                 return -ENOMEM;
2295
2296         /* automatic parse from the BIOS config */
2297         err = vt1708_parse_auto_config(codec);
2298         if (err < 0) {
2299                 via_free(codec);
2300                 return err;
2301         } else if (!err) {
2302                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
2303                        "from BIOS.  Using genenic mode...\n");
2304         }
2305
2306
2307         spec->stream_analog_playback = &vt1708_pcm_analog_playback;
2308         /* disable 32bit format on VT1708 */
2309         if (codec->vendor_id == 0x11061708)
2310                 spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
2311         spec->stream_analog_capture = &vt1708_pcm_analog_capture;
2312
2313         spec->stream_digital_playback = &vt1708_pcm_digital_playback;
2314         spec->stream_digital_capture = &vt1708_pcm_digital_capture;
2315
2316         if (spec->adc_nids && spec->input_mux) {
2317                 spec->mixers[spec->num_mixers] = vt1708_capture_mixer;
2318                 spec->num_mixers++;
2319         }
2320
2321         codec->patch_ops = via_patch_ops;
2322
2323         codec->patch_ops.init = via_auto_init;
2324 #ifdef CONFIG_SND_HDA_POWER_SAVE
2325         spec->loopback.amplist = vt1708_loopbacks;
2326 #endif
2327         INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
2328         return 0;
2329 }
2330
2331 /* capture mixer elements */
2332 static const struct snd_kcontrol_new vt1709_capture_mixer[] = {
2333         HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x0, HDA_INPUT),
2334         HDA_CODEC_MUTE("Capture Switch", 0x14, 0x0, HDA_INPUT),
2335         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x15, 0x0, HDA_INPUT),
2336         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x15, 0x0, HDA_INPUT),
2337         HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x16, 0x0, HDA_INPUT),
2338         HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x16, 0x0, HDA_INPUT),
2339         {
2340                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2341                 /* The multiple "Capture Source" controls confuse alsamixer
2342                  * So call somewhat different..
2343                  */
2344                 /* .name = "Capture Source", */
2345                 .name = "Input Source",
2346                 .count = 1,
2347                 .info = via_mux_enum_info,
2348                 .get = via_mux_enum_get,
2349                 .put = via_mux_enum_put,
2350         },
2351         { } /* end */
2352 };
2353
2354 static const struct hda_verb vt1709_uniwill_init_verbs[] = {
2355         {0x20, AC_VERB_SET_UNSOLICITED_ENABLE,
2356          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
2357         { }
2358 };
2359
2360 /*
2361  * generic initialization of ADC, input mixers and output mixers
2362  */
2363 static const struct hda_verb vt1709_10ch_volume_init_verbs[] = {
2364         /*
2365          * Unmute ADC0-2 and set the default input to mic-in
2366          */
2367         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2368         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2369         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2370
2371
2372         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2373          * mixer widget
2374          */
2375         /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2376         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2377         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2378         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2379         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2380         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2381
2382         /*
2383          * Set up output selector (0x1a, 0x1b, 0x29)
2384          */
2385         /* set vol=0 to output mixers */
2386         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2387         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2388         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2389
2390         /*
2391          *  Unmute PW3 and PW4
2392          */
2393         {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2394         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2395
2396         /* Set input of PW4 as MW0 */
2397         {0x20, AC_VERB_SET_CONNECT_SEL, 0},
2398         /* PW9 Output enable */
2399         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2400         { }
2401 };
2402
2403 static const struct hda_pcm_stream vt1709_10ch_pcm_analog_playback = {
2404         .substreams = 1,
2405         .channels_min = 2,
2406         .channels_max = 10,
2407         .nid = 0x10, /* NID to query formats and rates */
2408         .ops = {
2409                 .open = via_playback_pcm_open,
2410                 .prepare = via_playback_multi_pcm_prepare,
2411                 .cleanup = via_playback_multi_pcm_cleanup,
2412         },
2413 };
2414
2415 static const struct hda_pcm_stream vt1709_6ch_pcm_analog_playback = {
2416         .substreams = 1,
2417         .channels_min = 2,
2418         .channels_max = 6,
2419         .nid = 0x10, /* NID to query formats and rates */
2420         .ops = {
2421                 .open = via_playback_pcm_open,
2422                 .prepare = via_playback_multi_pcm_prepare,
2423                 .cleanup = via_playback_multi_pcm_cleanup,
2424         },
2425 };
2426
2427 static const struct hda_pcm_stream vt1709_pcm_analog_capture = {
2428         .substreams = 2,
2429         .channels_min = 2,
2430         .channels_max = 2,
2431         .nid = 0x14, /* NID to query formats and rates */
2432         .ops = {
2433                 .prepare = via_capture_pcm_prepare,
2434                 .cleanup = via_capture_pcm_cleanup
2435         },
2436 };
2437
2438 static const struct hda_pcm_stream vt1709_pcm_digital_playback = {
2439         .substreams = 1,
2440         .channels_min = 2,
2441         .channels_max = 2,
2442         /* NID is set in via_build_pcms */
2443         .ops = {
2444                 .open = via_dig_playback_pcm_open,
2445                 .close = via_dig_playback_pcm_close
2446         },
2447 };
2448
2449 static const struct hda_pcm_stream vt1709_pcm_digital_capture = {
2450         .substreams = 1,
2451         .channels_min = 2,
2452         .channels_max = 2,
2453 };
2454
2455 static int vt1709_auto_fill_dac_nids(struct via_spec *spec,
2456                                      const struct auto_pin_cfg *cfg)
2457 {
2458         int i;
2459         hda_nid_t nid;
2460
2461         if (cfg->line_outs == 4)  /* 10 channels */
2462                 spec->multiout.num_dacs = cfg->line_outs+1; /* AOW0~AOW4 */
2463         else if (cfg->line_outs == 3) /* 6 channels */
2464                 spec->multiout.num_dacs = cfg->line_outs; /* AOW0~AOW2 */
2465
2466         spec->multiout.dac_nids = spec->private_dac_nids;
2467
2468         if (cfg->line_outs == 4) { /* 10 channels */
2469                 for (i = 0; i < cfg->line_outs; i++) {
2470                         nid = cfg->line_out_pins[i];
2471                         if (nid) {
2472                                 /* config dac list */
2473                                 switch (i) {
2474                                 case AUTO_SEQ_FRONT:
2475                                         /* AOW0 */
2476                                         spec->private_dac_nids[i] = 0x10;
2477                                         break;
2478                                 case AUTO_SEQ_CENLFE:
2479                                         /* AOW2 */
2480                                         spec->private_dac_nids[i] = 0x12;
2481                                         break;
2482                                 case AUTO_SEQ_SURROUND:
2483                                         /* AOW3 */
2484                                         spec->private_dac_nids[i] = 0x11;
2485                                         break;
2486                                 case AUTO_SEQ_SIDE:
2487                                         /* AOW1 */
2488                                         spec->private_dac_nids[i] = 0x27;
2489                                         break;
2490                                 default:
2491                                         break;
2492                                 }
2493                         }
2494                 }
2495                 spec->private_dac_nids[cfg->line_outs] = 0x28; /* AOW4 */
2496
2497         } else if (cfg->line_outs == 3) { /* 6 channels */
2498                 for (i = 0; i < cfg->line_outs; i++) {
2499                         nid = cfg->line_out_pins[i];
2500                         if (nid) {
2501                                 /* config dac list */
2502                                 switch (i) {
2503                                 case AUTO_SEQ_FRONT:
2504                                         /* AOW0 */
2505                                         spec->private_dac_nids[i] = 0x10;
2506                                         break;
2507                                 case AUTO_SEQ_CENLFE:
2508                                         /* AOW2 */
2509                                         spec->private_dac_nids[i] = 0x12;
2510                                         break;
2511                                 case AUTO_SEQ_SURROUND:
2512                                         /* AOW1 */
2513                                         spec->private_dac_nids[i] = 0x11;
2514                                         break;
2515                                 default:
2516                                         break;
2517                                 }
2518                         }
2519                 }
2520         }
2521
2522         return 0;
2523 }
2524
2525 /* add playback controls from the parsed DAC table */
2526 static int vt1709_auto_create_multi_out_ctls(struct via_spec *spec,
2527                                              const struct auto_pin_cfg *cfg)
2528 {
2529         char name[32];
2530         static const char * const chname[4] = {
2531                 "Front", "Surround", "C/LFE", "Side"
2532         };
2533         hda_nid_t nid, nid_vol, nid_vols[] = {0x18, 0x1a, 0x1b, 0x29};
2534         int i, err;
2535
2536         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
2537                 nid = cfg->line_out_pins[i];
2538
2539                 if (!nid)
2540                         continue;
2541
2542                 nid_vol = nid_vols[i];
2543
2544                 if (i == AUTO_SEQ_CENLFE) {
2545                         /* Center/LFE */
2546                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2547                                               "Center Playback Volume",
2548                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2549                                                                   HDA_OUTPUT));
2550                         if (err < 0)
2551                                 return err;
2552                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2553                                               "LFE Playback Volume",
2554                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2555                                                                   HDA_OUTPUT));
2556                         if (err < 0)
2557                                 return err;
2558                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2559                                               "Center Playback Switch",
2560                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2561                                                                   HDA_OUTPUT));
2562                         if (err < 0)
2563                                 return err;
2564                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2565                                               "LFE Playback Switch",
2566                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2567                                                                   HDA_OUTPUT));
2568                         if (err < 0)
2569                                 return err;
2570                 } else if (i == AUTO_SEQ_FRONT) {
2571                         /* ADD control to mixer index 0 */
2572                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2573                                               "Master Front Playback Volume",
2574                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2575                                                                   HDA_INPUT));
2576                         if (err < 0)
2577                                 return err;
2578                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2579                                               "Master Front Playback Switch",
2580                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2581                                                                   HDA_INPUT));
2582                         if (err < 0)
2583                                 return err;
2584
2585                         /* add control to PW3 */
2586                         sprintf(name, "%s Playback Volume", chname[i]);
2587                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2588                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2589                                                                   HDA_OUTPUT));
2590                         if (err < 0)
2591                                 return err;
2592                         sprintf(name, "%s Playback Switch", chname[i]);
2593                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2594                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2595                                                                   HDA_OUTPUT));
2596                         if (err < 0)
2597                                 return err;
2598                 } else if (i == AUTO_SEQ_SURROUND) {
2599                         sprintf(name, "%s Playback Volume", chname[i]);
2600                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2601                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2602                                                                   HDA_OUTPUT));
2603                         if (err < 0)
2604                                 return err;
2605                         sprintf(name, "%s Playback Switch", chname[i]);
2606                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2607                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2608                                                                   HDA_OUTPUT));
2609                         if (err < 0)
2610                                 return err;
2611                 } else if (i == AUTO_SEQ_SIDE) {
2612                         sprintf(name, "%s Playback Volume", chname[i]);
2613                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2614                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2615                                                                   HDA_OUTPUT));
2616                         if (err < 0)
2617                                 return err;
2618                         sprintf(name, "%s Playback Switch", chname[i]);
2619                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2620                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2621                                                                   HDA_OUTPUT));
2622                         if (err < 0)
2623                                 return err;
2624                 }
2625         }
2626
2627         return 0;
2628 }
2629
2630 static int vt1709_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
2631 {
2632         int err;
2633
2634         if (!pin)
2635                 return 0;
2636
2637         if (spec->multiout.num_dacs == 5) /* 10 channels */
2638                 spec->multiout.hp_nid = VT1709_HP_DAC_NID;
2639         else if (spec->multiout.num_dacs == 3) /* 6 channels */
2640                 spec->multiout.hp_nid = 0;
2641         spec->hp_independent_mode_index = 1;
2642
2643         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2644                               "Headphone Playback Volume",
2645                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2646         if (err < 0)
2647                 return err;
2648         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2649                               "Headphone Playback Switch",
2650                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2651         if (err < 0)
2652                 return err;
2653
2654         return 0;
2655 }
2656
2657 /* create playback/capture controls for input pins */
2658 static int vt1709_auto_create_analog_input_ctls(struct hda_codec *codec,
2659                                                 const struct auto_pin_cfg *cfg)
2660 {
2661         return vt_auto_create_analog_input_ctls(codec, cfg, 0x18);
2662 }
2663
2664 static int vt1709_parse_auto_config(struct hda_codec *codec)
2665 {
2666         struct via_spec *spec = codec->spec;
2667         int err;
2668
2669         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2670         if (err < 0)
2671                 return err;
2672         err = vt1709_auto_fill_dac_nids(spec, &spec->autocfg);
2673         if (err < 0)
2674                 return err;
2675         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2676                 return 0; /* can't find valid BIOS pin config */
2677
2678         err = vt1709_auto_create_multi_out_ctls(spec, &spec->autocfg);
2679         if (err < 0)
2680                 return err;
2681         err = vt1709_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2682         if (err < 0)
2683                 return err;
2684         err = vt1709_auto_create_analog_input_ctls(codec, &spec->autocfg);
2685         if (err < 0)
2686                 return err;
2687
2688         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2689
2690         if (spec->autocfg.dig_outs)
2691                 spec->multiout.dig_out_nid = VT1709_DIGOUT_NID;
2692         spec->dig_in_pin = VT1709_DIGIN_PIN;
2693         if (spec->autocfg.dig_in_pin)
2694                 spec->dig_in_nid = VT1709_DIGIN_NID;
2695
2696         if (spec->kctls.list)
2697                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
2698
2699         spec->input_mux = &spec->private_imux[0];
2700
2701         if (spec->hp_mux)
2702                 via_hp_build(codec);
2703
2704         via_smart51_build(spec);
2705         return 1;
2706 }
2707
2708 #ifdef CONFIG_SND_HDA_POWER_SAVE
2709 static const struct hda_amp_list vt1709_loopbacks[] = {
2710         { 0x18, HDA_INPUT, 1 },
2711         { 0x18, HDA_INPUT, 2 },
2712         { 0x18, HDA_INPUT, 3 },
2713         { 0x18, HDA_INPUT, 4 },
2714         { } /* end */
2715 };
2716 #endif
2717
2718 static int patch_vt1709_10ch(struct hda_codec *codec)
2719 {
2720         struct via_spec *spec;
2721         int err;
2722
2723         /* create a codec specific record */
2724         spec = via_new_spec(codec);
2725         if (spec == NULL)
2726                 return -ENOMEM;
2727
2728         err = vt1709_parse_auto_config(codec);
2729         if (err < 0) {
2730                 via_free(codec);
2731                 return err;
2732         } else if (!err) {
2733                 printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
2734                        "Using genenic mode...\n");
2735         }
2736
2737         spec->init_verbs[spec->num_iverbs++] = vt1709_10ch_volume_init_verbs;
2738         spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
2739
2740         spec->stream_analog_playback = &vt1709_10ch_pcm_analog_playback;
2741         spec->stream_analog_capture = &vt1709_pcm_analog_capture;
2742
2743         spec->stream_digital_playback = &vt1709_pcm_digital_playback;
2744         spec->stream_digital_capture = &vt1709_pcm_digital_capture;
2745
2746         if (spec->adc_nids && spec->input_mux) {
2747                 spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
2748                 spec->num_mixers++;
2749         }
2750
2751         codec->patch_ops = via_patch_ops;
2752
2753         codec->patch_ops.init = via_auto_init;
2754         codec->patch_ops.unsol_event = via_unsol_event;
2755 #ifdef CONFIG_SND_HDA_POWER_SAVE
2756         spec->loopback.amplist = vt1709_loopbacks;
2757 #endif
2758
2759         return 0;
2760 }
2761 /*
2762  * generic initialization of ADC, input mixers and output mixers
2763  */
2764 static const struct hda_verb vt1709_6ch_volume_init_verbs[] = {
2765         /*
2766          * Unmute ADC0-2 and set the default input to mic-in
2767          */
2768         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2769         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2770         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2771
2772
2773         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2774          * mixer widget
2775          */
2776         /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2777         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2778         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2779         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2780         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2781         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2782
2783         /*
2784          * Set up output selector (0x1a, 0x1b, 0x29)
2785          */
2786         /* set vol=0 to output mixers */
2787         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2788         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2789         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2790
2791         /*
2792          *  Unmute PW3 and PW4
2793          */
2794         {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2795         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2796
2797         /* Set input of PW4 as MW0 */
2798         {0x20, AC_VERB_SET_CONNECT_SEL, 0},
2799         /* PW9 Output enable */
2800         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2801         { }
2802 };
2803
2804 static int patch_vt1709_6ch(struct hda_codec *codec)
2805 {
2806         struct via_spec *spec;
2807         int err;
2808
2809         /* create a codec specific record */
2810         spec = via_new_spec(codec);
2811         if (spec == NULL)
2812                 return -ENOMEM;
2813
2814         err = vt1709_parse_auto_config(codec);
2815         if (err < 0) {
2816                 via_free(codec);
2817                 return err;
2818         } else if (!err) {
2819                 printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
2820                        "Using genenic mode...\n");
2821         }
2822
2823         spec->init_verbs[spec->num_iverbs++] = vt1709_6ch_volume_init_verbs;
2824         spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
2825
2826         spec->stream_analog_playback = &vt1709_6ch_pcm_analog_playback;
2827         spec->stream_analog_capture = &vt1709_pcm_analog_capture;
2828
2829         spec->stream_digital_playback = &vt1709_pcm_digital_playback;
2830         spec->stream_digital_capture = &vt1709_pcm_digital_capture;
2831
2832         if (spec->adc_nids && spec->input_mux) {
2833                 spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
2834                 spec->num_mixers++;
2835         }
2836
2837         codec->patch_ops = via_patch_ops;
2838
2839         codec->patch_ops.init = via_auto_init;
2840         codec->patch_ops.unsol_event = via_unsol_event;
2841 #ifdef CONFIG_SND_HDA_POWER_SAVE
2842         spec->loopback.amplist = vt1709_loopbacks;
2843 #endif
2844         return 0;
2845 }
2846
2847 /* capture mixer elements */
2848 static const struct snd_kcontrol_new vt1708B_capture_mixer[] = {
2849         HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
2850         HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
2851         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
2852         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
2853         {
2854                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2855                 /* The multiple "Capture Source" controls confuse alsamixer
2856                  * So call somewhat different..
2857                  */
2858                 /* .name = "Capture Source", */
2859                 .name = "Input Source",
2860                 .count = 1,
2861                 .info = via_mux_enum_info,
2862                 .get = via_mux_enum_get,
2863                 .put = via_mux_enum_put,
2864         },
2865         { } /* end */
2866 };
2867 /*
2868  * generic initialization of ADC, input mixers and output mixers
2869  */
2870 static const struct hda_verb vt1708B_8ch_volume_init_verbs[] = {
2871         /*
2872          * Unmute ADC0-1 and set the default input to mic-in
2873          */
2874         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2875         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2876
2877
2878         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2879          * mixer widget
2880          */
2881         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2882         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2883         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2884         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2885         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2886         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2887
2888         /*
2889          * Set up output mixers
2890          */
2891         /* set vol=0 to output mixers */
2892         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2893         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2894         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2895
2896         /* Setup default input to PW4 */
2897         {0x1d, AC_VERB_SET_CONNECT_SEL, 0},
2898         /* PW9 Output enable */
2899         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2900         /* PW10 Input enable */
2901         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2902         { }
2903 };
2904
2905 static const struct hda_verb vt1708B_4ch_volume_init_verbs[] = {
2906         /*
2907          * Unmute ADC0-1 and set the default input to mic-in
2908          */
2909         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2910         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2911
2912
2913         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2914          * mixer widget
2915          */
2916         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2917         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2918         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2919         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2920         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2921         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2922
2923         /*
2924          * Set up output mixers
2925          */
2926         /* set vol=0 to output mixers */
2927         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2928         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2929         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2930
2931         /* Setup default input of PW4 to MW0 */
2932         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
2933         /* PW9 Output enable */
2934         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2935         /* PW10 Input enable */
2936         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2937         { }
2938 };
2939
2940 static const struct hda_verb vt1708B_uniwill_init_verbs[] = {
2941         {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
2942          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
2943         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2944         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2945         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2946         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2947         {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2948         {0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2949         {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2950         { }
2951 };
2952
2953 static int via_pcm_open_close(struct hda_pcm_stream *hinfo,
2954                               struct hda_codec *codec,
2955                               struct snd_pcm_substream *substream)
2956 {
2957         int idle = substream->pstr->substream_opened == 1
2958                 && substream->ref_count == 0;
2959
2960         analog_low_current_mode(codec, idle);
2961         return 0;
2962 }
2963
2964 static const struct hda_pcm_stream vt1708B_8ch_pcm_analog_playback = {
2965         .substreams = 2,
2966         .channels_min = 2,
2967         .channels_max = 8,
2968         .nid = 0x10, /* NID to query formats and rates */
2969         .ops = {
2970                 .open = via_playback_pcm_open,
2971                 .prepare = via_playback_multi_pcm_prepare,
2972                 .cleanup = via_playback_multi_pcm_cleanup,
2973                 .close = via_pcm_open_close
2974         },
2975 };
2976
2977 static const struct hda_pcm_stream vt1708B_4ch_pcm_analog_playback = {
2978         .substreams = 2,
2979         .channels_min = 2,
2980         .channels_max = 4,
2981         .nid = 0x10, /* NID to query formats and rates */
2982         .ops = {
2983                 .open = via_playback_pcm_open,
2984                 .prepare = via_playback_multi_pcm_prepare,
2985                 .cleanup = via_playback_multi_pcm_cleanup
2986         },
2987 };
2988
2989 static const struct hda_pcm_stream vt1708B_pcm_analog_capture = {
2990         .substreams = 2,
2991         .channels_min = 2,
2992         .channels_max = 2,
2993         .nid = 0x13, /* NID to query formats and rates */
2994         .ops = {
2995                 .open = via_pcm_open_close,
2996                 .prepare = via_capture_pcm_prepare,
2997                 .cleanup = via_capture_pcm_cleanup,
2998                 .close = via_pcm_open_close
2999         },
3000 };
3001
3002 static const struct hda_pcm_stream vt1708B_pcm_digital_playback = {
3003         .substreams = 1,
3004         .channels_min = 2,
3005         .channels_max = 2,
3006         /* NID is set in via_build_pcms */
3007         .ops = {
3008                 .open = via_dig_playback_pcm_open,
3009                 .close = via_dig_playback_pcm_close,
3010                 .prepare = via_dig_playback_pcm_prepare,
3011                 .cleanup = via_dig_playback_pcm_cleanup
3012         },
3013 };
3014
3015 static const struct hda_pcm_stream vt1708B_pcm_digital_capture = {
3016         .substreams = 1,
3017         .channels_min = 2,
3018         .channels_max = 2,
3019 };
3020
3021 /* fill in the dac_nids table from the parsed pin configuration */
3022 static int vt1708B_auto_fill_dac_nids(struct via_spec *spec,
3023                                      const struct auto_pin_cfg *cfg)
3024 {
3025         int i;
3026         hda_nid_t nid;
3027
3028         spec->multiout.num_dacs = cfg->line_outs;
3029
3030         spec->multiout.dac_nids = spec->private_dac_nids;
3031
3032         for (i = 0; i < 4; i++) {
3033                 nid = cfg->line_out_pins[i];
3034                 if (nid) {
3035                         /* config dac list */
3036                         switch (i) {
3037                         case AUTO_SEQ_FRONT:
3038                                 spec->private_dac_nids[i] = 0x10;
3039                                 break;
3040                         case AUTO_SEQ_CENLFE:
3041                                 spec->private_dac_nids[i] = 0x24;
3042                                 break;
3043                         case AUTO_SEQ_SURROUND:
3044                                 spec->private_dac_nids[i] = 0x11;
3045                                 break;
3046                         case AUTO_SEQ_SIDE:
3047                                 spec->private_dac_nids[i] = 0x25;
3048                                 break;
3049                         }
3050                 }
3051         }
3052
3053         return 0;
3054 }
3055
3056 /* add playback controls from the parsed DAC table */
3057 static int vt1708B_auto_create_multi_out_ctls(struct via_spec *spec,
3058                                              const struct auto_pin_cfg *cfg)
3059 {
3060         char name[32];
3061         static const char * const chname[4] = {
3062                 "Front", "Surround", "C/LFE", "Side"
3063         };
3064         hda_nid_t nid_vols[] = {0x16, 0x18, 0x26, 0x27};
3065         hda_nid_t nid, nid_vol = 0;
3066         int i, err;
3067
3068         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
3069                 nid = cfg->line_out_pins[i];
3070
3071                 if (!nid)
3072                         continue;
3073
3074                 nid_vol = nid_vols[i];
3075
3076                 if (i == AUTO_SEQ_CENLFE) {
3077                         /* Center/LFE */
3078                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3079                                               "Center Playback Volume",
3080                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
3081                                                                   HDA_OUTPUT));
3082                         if (err < 0)
3083                                 return err;
3084                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3085                                               "LFE Playback Volume",
3086                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
3087                                                                   HDA_OUTPUT));
3088                         if (err < 0)
3089                                 return err;
3090                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3091                                               "Center Playback Switch",
3092                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
3093                                                                   HDA_OUTPUT));
3094                         if (err < 0)
3095                                 return err;
3096                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3097                                               "LFE Playback Switch",
3098                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
3099                                                                   HDA_OUTPUT));
3100                         if (err < 0)
3101                                 return err;
3102                 } else if (i == AUTO_SEQ_FRONT) {
3103                         /* add control to mixer index 0 */
3104                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3105                                               "Master Front Playback Volume",
3106                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3107                                                                   HDA_INPUT));
3108                         if (err < 0)
3109                                 return err;
3110                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3111                                               "Master Front Playback Switch",
3112                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3113                                                                   HDA_INPUT));
3114                         if (err < 0)
3115                                 return err;
3116
3117                         /* add control to PW3 */
3118                         sprintf(name, "%s Playback Volume", chname[i]);
3119                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3120                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
3121                                                                   HDA_OUTPUT));
3122                         if (err < 0)
3123                                 return err;
3124                         sprintf(name, "%s Playback Switch", chname[i]);
3125                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3126                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
3127                                                                   HDA_OUTPUT));
3128                         if (err < 0)
3129                                 return err;
3130                 } else {
3131                         sprintf(name, "%s Playback Volume", chname[i]);
3132                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3133                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3134                                                                   HDA_OUTPUT));
3135                         if (err < 0)
3136                                 return err;
3137                         sprintf(name, "%s Playback Switch", chname[i]);
3138                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3139                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3140                                                                   HDA_OUTPUT));
3141                         if (err < 0)
3142                                 return err;
3143                 }
3144         }
3145
3146         return 0;
3147 }
3148
3149 static int vt1708B_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
3150 {
3151         int err;
3152
3153         if (!pin)
3154                 return 0;
3155
3156         spec->multiout.hp_nid = VT1708B_HP_NID; /* AOW3 */
3157         spec->hp_independent_mode_index = 1;
3158
3159         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3160                               "Headphone Playback Volume",
3161                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3162         if (err < 0)
3163                 return err;
3164         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3165                               "Headphone Playback Switch",
3166                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3167         if (err < 0)
3168                 return err;
3169
3170         create_hp_imux(spec);
3171
3172         return 0;
3173 }
3174
3175 /* create playback/capture controls for input pins */
3176 static int vt1708B_auto_create_analog_input_ctls(struct hda_codec *codec,
3177                                                 const struct auto_pin_cfg *cfg)
3178 {
3179         return vt_auto_create_analog_input_ctls(codec, cfg, 0x16);
3180 }
3181
3182 static int vt1708B_parse_auto_config(struct hda_codec *codec)
3183 {
3184         struct via_spec *spec = codec->spec;
3185         int err;
3186
3187         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3188         if (err < 0)
3189                 return err;
3190         err = vt1708B_auto_fill_dac_nids(spec, &spec->autocfg);
3191         if (err < 0)
3192                 return err;
3193         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3194                 return 0; /* can't find valid BIOS pin config */
3195
3196         err = vt1708B_auto_create_multi_out_ctls(spec, &spec->autocfg);
3197         if (err < 0)
3198                 return err;
3199         err = vt1708B_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
3200         if (err < 0)
3201                 return err;
3202         err = vt1708B_auto_create_analog_input_ctls(codec, &spec->autocfg);
3203         if (err < 0)
3204                 return err;
3205
3206         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3207
3208         if (spec->autocfg.dig_outs)
3209                 spec->multiout.dig_out_nid = VT1708B_DIGOUT_NID;
3210         spec->dig_in_pin = VT1708B_DIGIN_PIN;
3211         if (spec->autocfg.dig_in_pin)
3212                 spec->dig_in_nid = VT1708B_DIGIN_NID;
3213
3214         if (spec->kctls.list)
3215                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
3216
3217         spec->input_mux = &spec->private_imux[0];
3218
3219         if (spec->hp_mux)
3220                 via_hp_build(codec);
3221
3222         via_smart51_build(spec);
3223         return 1;
3224 }
3225
3226 #ifdef CONFIG_SND_HDA_POWER_SAVE
3227 static const struct hda_amp_list vt1708B_loopbacks[] = {
3228         { 0x16, HDA_INPUT, 1 },
3229         { 0x16, HDA_INPUT, 2 },
3230         { 0x16, HDA_INPUT, 3 },
3231         { 0x16, HDA_INPUT, 4 },
3232         { } /* end */
3233 };
3234 #endif
3235
3236 static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
3237 {
3238         struct via_spec *spec = codec->spec;
3239         int imux_is_smixer;
3240         unsigned int parm;
3241         int is_8ch = 0;
3242         if ((spec->codec_type != VT1708B_4CH) &&
3243             (codec->vendor_id != 0x11064397))
3244                 is_8ch = 1;
3245
3246         /* SW0 (17h) = stereo mixer */
3247         imux_is_smixer =
3248         (snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
3249          == ((spec->codec_type == VT1708S) ? 5 : 0));
3250         /* inputs */
3251         /* PW 1/2/5 (1ah/1bh/1eh) */
3252         parm = AC_PWRST_D3;
3253         set_pin_power_state(codec, 0x1a, &parm);
3254         set_pin_power_state(codec, 0x1b, &parm);
3255         set_pin_power_state(codec, 0x1e, &parm);
3256         if (imux_is_smixer)
3257                 parm = AC_PWRST_D0;
3258         /* SW0 (17h), AIW 0/1 (13h/14h) */
3259         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
3260         snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
3261         snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
3262
3263         /* outputs */
3264         /* PW0 (19h), SW1 (18h), AOW1 (11h) */
3265         parm = AC_PWRST_D3;
3266         set_pin_power_state(codec, 0x19, &parm);
3267         if (spec->smart51_enabled)
3268                 set_pin_power_state(codec, 0x1b, &parm);
3269         snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
3270         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
3271
3272         /* PW6 (22h), SW2 (26h), AOW2 (24h) */
3273         if (is_8ch) {
3274                 parm = AC_PWRST_D3;
3275                 set_pin_power_state(codec, 0x22, &parm);
3276                 if (spec->smart51_enabled)
3277                         set_pin_power_state(codec, 0x1a, &parm);
3278                 snd_hda_codec_write(codec, 0x26, 0,
3279                                     AC_VERB_SET_POWER_STATE, parm);
3280                 snd_hda_codec_write(codec, 0x24, 0,
3281                                     AC_VERB_SET_POWER_STATE, parm);
3282         } else if (codec->vendor_id == 0x11064397) {
3283                 /* PW7(23h), SW2(27h), AOW2(25h) */
3284                 parm = AC_PWRST_D3;
3285                 set_pin_power_state(codec, 0x23, &parm);
3286                 if (spec->smart51_enabled)
3287                         set_pin_power_state(codec, 0x1a, &parm);
3288                 snd_hda_codec_write(codec, 0x27, 0,
3289                                     AC_VERB_SET_POWER_STATE, parm);
3290                 snd_hda_codec_write(codec, 0x25, 0,
3291                                     AC_VERB_SET_POWER_STATE, parm);
3292         }
3293
3294         /* PW 3/4/7 (1ch/1dh/23h) */
3295         parm = AC_PWRST_D3;
3296         /* force to D0 for internal Speaker */
3297         set_pin_power_state(codec, 0x1c, &parm);
3298         set_pin_power_state(codec, 0x1d, &parm);
3299         if (is_8ch)
3300                 set_pin_power_state(codec, 0x23, &parm);
3301
3302         /* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
3303         snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
3304                             imux_is_smixer ? AC_PWRST_D0 : parm);
3305         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
3306         if (is_8ch) {
3307                 snd_hda_codec_write(codec, 0x25, 0,
3308                                     AC_VERB_SET_POWER_STATE, parm);
3309                 snd_hda_codec_write(codec, 0x27, 0,
3310                                     AC_VERB_SET_POWER_STATE, parm);
3311         } else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
3312                 snd_hda_codec_write(codec, 0x25, 0,
3313                                     AC_VERB_SET_POWER_STATE, parm);
3314 }
3315
3316 static int patch_vt1708S(struct hda_codec *codec);
3317 static int patch_vt1708B_8ch(struct hda_codec *codec)
3318 {
3319         struct via_spec *spec;
3320         int err;
3321
3322         if (get_codec_type(codec) == VT1708BCE)
3323                 return patch_vt1708S(codec);
3324         /* create a codec specific record */
3325         spec = via_new_spec(codec);
3326         if (spec == NULL)
3327                 return -ENOMEM;
3328
3329         /* automatic parse from the BIOS config */
3330         err = vt1708B_parse_auto_config(codec);
3331         if (err < 0) {
3332                 via_free(codec);
3333                 return err;
3334         } else if (!err) {
3335                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
3336                        "from BIOS.  Using genenic mode...\n");
3337         }
3338
3339         spec->init_verbs[spec->num_iverbs++] = vt1708B_8ch_volume_init_verbs;
3340         spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
3341
3342         spec->stream_analog_playback = &vt1708B_8ch_pcm_analog_playback;
3343         spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
3344
3345         spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
3346         spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
3347
3348         if (spec->adc_nids && spec->input_mux) {
3349                 spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
3350                 spec->num_mixers++;
3351         }
3352
3353         codec->patch_ops = via_patch_ops;
3354
3355         codec->patch_ops.init = via_auto_init;
3356         codec->patch_ops.unsol_event = via_unsol_event;
3357 #ifdef CONFIG_SND_HDA_POWER_SAVE
3358         spec->loopback.amplist = vt1708B_loopbacks;
3359 #endif
3360
3361         spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
3362
3363         return 0;
3364 }
3365
3366 static int patch_vt1708B_4ch(struct hda_codec *codec)
3367 {
3368         struct via_spec *spec;
3369         int err;
3370
3371         /* create a codec specific record */
3372         spec = via_new_spec(codec);
3373         if (spec == NULL)
3374                 return -ENOMEM;
3375
3376         /* automatic parse from the BIOS config */
3377         err = vt1708B_parse_auto_config(codec);
3378         if (err < 0) {
3379                 via_free(codec);
3380                 return err;
3381         } else if (!err) {
3382                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
3383                        "from BIOS.  Using genenic mode...\n");
3384         }
3385
3386         spec->init_verbs[spec->num_iverbs++] = vt1708B_4ch_volume_init_verbs;
3387         spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
3388
3389         spec->stream_analog_playback = &vt1708B_4ch_pcm_analog_playback;
3390         spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
3391
3392         spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
3393         spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
3394
3395         if (spec->adc_nids && spec->input_mux) {
3396                 spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
3397                 spec->num_mixers++;
3398         }
3399
3400         codec->patch_ops = via_patch_ops;
3401
3402         codec->patch_ops.init = via_auto_init;
3403         codec->patch_ops.unsol_event = via_unsol_event;
3404 #ifdef CONFIG_SND_HDA_POWER_SAVE
3405         spec->loopback.amplist = vt1708B_loopbacks;
3406 #endif
3407
3408         spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
3409
3410         return 0;
3411 }
3412
3413 /* Patch for VT1708S */
3414
3415 /* capture mixer elements */
3416 static const struct snd_kcontrol_new vt1708S_capture_mixer[] = {
3417         HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
3418         HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
3419         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
3420         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
3421         HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x1A, 0x0, HDA_INPUT),
3422         HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x1E, 0x0,
3423                          HDA_INPUT),
3424         {
3425                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3426                 /* The multiple "Capture Source" controls confuse alsamixer
3427                  * So call somewhat different..
3428                  */
3429                 /* .name = "Capture Source", */
3430                 .name = "Input Source",
3431                 .count = 1,
3432                 .info = via_mux_enum_info,
3433                 .get = via_mux_enum_get,
3434                 .put = via_mux_enum_put,
3435         },
3436         { } /* end */
3437 };
3438
3439 static const struct hda_verb vt1708S_volume_init_verbs[] = {
3440         /* Unmute ADC0-1 and set the default input to mic-in */
3441         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3442         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3443
3444         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the
3445          * analog-loopback mixer widget */
3446         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3447         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3448         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3449         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3450         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3451         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
3452
3453         /* Setup default input of PW4 to MW0 */
3454         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
3455         /* PW9, PW10  Output enable */
3456         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3457         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3458         /* Enable Mic Boost Volume backdoor */
3459         {0x1, 0xf98, 0x1},
3460         /* don't bybass mixer */
3461         {0x1, 0xf88, 0xc0},
3462         { }
3463 };
3464
3465 static const struct hda_verb vt1708S_uniwill_init_verbs[] = {
3466         {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
3467          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3468         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3469         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3470         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3471         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3472         {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3473         {0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3474         {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3475         { }
3476 };
3477
3478 static const struct hda_verb vt1705_uniwill_init_verbs[] = {
3479         {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
3480          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3481         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3482         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3483         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3484         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3485         {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3486         {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3487         { }
3488 };
3489
3490 static const struct hda_pcm_stream vt1708S_pcm_analog_playback = {
3491         .substreams = 2,
3492         .channels_min = 2,
3493         .channels_max = 8,
3494         .nid = 0x10, /* NID to query formats and rates */
3495         .ops = {
3496                 .open = via_playback_pcm_open,
3497                 .prepare = via_playback_multi_pcm_prepare,
3498                 .cleanup = via_playback_multi_pcm_cleanup,
3499                 .close = via_pcm_open_close
3500         },
3501 };
3502
3503 static const struct hda_pcm_stream vt1705_pcm_analog_playback = {
3504         .substreams = 2,
3505         .channels_min = 2,
3506         .channels_max = 6,
3507         .nid = 0x10, /* NID to query formats and rates */
3508         .ops = {
3509                 .open = via_playback_pcm_open,
3510                 .prepare = via_playback_multi_pcm_prepare,
3511                 .cleanup = via_playback_multi_pcm_cleanup,
3512                 .close = via_pcm_open_close
3513         },
3514 };
3515
3516 static const struct hda_pcm_stream vt1708S_pcm_analog_capture = {
3517         .substreams = 2,
3518         .channels_min = 2,
3519         .channels_max = 2,
3520         .nid = 0x13, /* NID to query formats and rates */
3521         .ops = {
3522                 .open = via_pcm_open_close,
3523                 .prepare = via_capture_pcm_prepare,
3524                 .cleanup = via_capture_pcm_cleanup,
3525                 .close = via_pcm_open_close
3526         },
3527 };
3528
3529 static const struct hda_pcm_stream vt1708S_pcm_digital_playback = {
3530         .substreams = 1,
3531         .channels_min = 2,
3532         .channels_max = 2,
3533         /* NID is set in via_build_pcms */
3534         .ops = {
3535                 .open = via_dig_playback_pcm_open,
3536                 .close = via_dig_playback_pcm_close,
3537                 .prepare = via_dig_playback_pcm_prepare,
3538                 .cleanup = via_dig_playback_pcm_cleanup
3539         },
3540 };
3541
3542 /* fill in the dac_nids table from the parsed pin configuration */
3543 static int vt1708S_auto_fill_dac_nids(struct via_spec *spec,
3544                                      const struct auto_pin_cfg *cfg)
3545 {
3546         int i;
3547         hda_nid_t nid;
3548
3549         spec->multiout.num_dacs = cfg->line_outs;
3550
3551         spec->multiout.dac_nids = spec->private_dac_nids;
3552
3553         for (i = 0; i < 4; i++) {
3554                 nid = cfg->line_out_pins[i];
3555                 if (nid) {
3556                         /* config dac list */
3557                         switch (i) {
3558                         case AUTO_SEQ_FRONT:
3559                                 spec->private_dac_nids[i] = 0x10;
3560                                 break;
3561                         case AUTO_SEQ_CENLFE:
3562                                 if (spec->codec->vendor_id == 0x11064397)
3563                                         spec->private_dac_nids[i] = 0x25;
3564                                 else
3565                                         spec->private_dac_nids[i] = 0x24;
3566                                 break;
3567                         case AUTO_SEQ_SURROUND:
3568                                 spec->private_dac_nids[i] = 0x11;
3569                                 break;
3570                         case AUTO_SEQ_SIDE:
3571                                 spec->private_dac_nids[i] = 0x25;
3572                                 break;
3573                         }
3574                 }
3575         }
3576
3577         /* for Smart 5.1, line/mic inputs double as output pins */
3578         if (cfg->line_outs == 1) {
3579                 spec->multiout.num_dacs = 3;
3580                 spec->private_dac_nids[AUTO_SEQ_SURROUND] = 0x11;
3581                 if (spec->codec->vendor_id == 0x11064397)
3582                         spec->private_dac_nids[AUTO_SEQ_CENLFE] = 0x25;
3583                 else
3584                         spec->private_dac_nids[AUTO_SEQ_CENLFE] = 0x24;
3585         }
3586
3587         return 0;
3588 }
3589
3590 /* add playback controls from the parsed DAC table */
3591 static int vt1708S_auto_create_multi_out_ctls(struct hda_codec *codec,
3592                                              const struct auto_pin_cfg *cfg)
3593 {
3594         struct via_spec *spec = codec->spec;
3595         char name[32];
3596         static const char * const chname[4] = {
3597                 "Front", "Surround", "C/LFE", "Side"
3598         };
3599         hda_nid_t nid_vols[2][4] = { {0x10, 0x11, 0x24, 0x25},
3600                                      {0x10, 0x11, 0x25, 0} };
3601         hda_nid_t nid_mutes[2][4] = { {0x1C, 0x18, 0x26, 0x27},
3602                                       {0x1C, 0x18, 0x27, 0} };
3603         hda_nid_t nid, nid_vol, nid_mute;
3604         int i, err;
3605
3606         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
3607                 nid = cfg->line_out_pins[i];
3608
3609                 /* for Smart 5.1, there are always at least six channels */
3610                 if (!nid && i > AUTO_SEQ_CENLFE)
3611                         continue;
3612
3613                 if (codec->vendor_id == 0x11064397) {
3614                         nid_vol = nid_vols[1][i];
3615                         nid_mute = nid_mutes[1][i];
3616                 } else {
3617                         nid_vol = nid_vols[0][i];
3618                         nid_mute = nid_mutes[0][i];
3619                 }
3620                 if (!nid_vol && !nid_mute)
3621                         continue;
3622
3623                 if (i == AUTO_SEQ_CENLFE) {
3624                         /* Center/LFE */
3625                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3626                                               "Center Playback Volume",
3627                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
3628                                                                   HDA_OUTPUT));
3629                         if (err < 0)
3630                                 return err;
3631                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3632                                               "LFE Playback Volume",
3633                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
3634                                                                   HDA_OUTPUT));
3635                         if (err < 0)
3636                                 return err;
3637                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3638                                               "Center Playback Switch",
3639                                               HDA_COMPOSE_AMP_VAL(nid_mute,
3640                                                                   1, 0,
3641                                                                   HDA_OUTPUT));
3642                         if (err < 0)
3643                                 return err;
3644                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3645                                               "LFE Playback Switch",
3646                                               HDA_COMPOSE_AMP_VAL(nid_mute,
3647                                                                   2, 0,
3648                                                                   HDA_OUTPUT));
3649                         if (err < 0)
3650                                 return err;
3651                 } else if (i == AUTO_SEQ_FRONT) {
3652                         /* add control to mixer index 0 */
3653                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3654                                               "Master Front Playback Volume",
3655                                               HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
3656                                                                   HDA_INPUT));
3657                         if (err < 0)
3658                                 return err;
3659                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3660                                               "Master Front Playback Switch",
3661                                               HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
3662                                                                   HDA_INPUT));
3663                         if (err < 0)
3664                                 return err;
3665
3666                         /* Front */
3667                         sprintf(name, "%s Playback Volume", chname[i]);
3668                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3669                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3670                                                                   HDA_OUTPUT));
3671                         if (err < 0)
3672                                 return err;
3673                         sprintf(name, "%s Playback Switch", chname[i]);
3674                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3675                                               HDA_COMPOSE_AMP_VAL(nid_mute,
3676                                                                   3, 0,
3677                                                                   HDA_OUTPUT));
3678                         if (err < 0)
3679                                 return err;
3680                 } else {
3681                         sprintf(name, "%s Playback Volume", chname[i]);
3682                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3683                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3684                                                                   HDA_OUTPUT));
3685                         if (err < 0)
3686                                 return err;
3687                         sprintf(name, "%s Playback Switch", chname[i]);
3688                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3689                                               HDA_COMPOSE_AMP_VAL(nid_mute,
3690                                                                   3, 0,
3691                                                                   HDA_OUTPUT));
3692                         if (err < 0)
3693                                 return err;
3694                 }
3695         }
3696
3697         return 0;
3698 }
3699
3700 static int vt1708S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
3701 {
3702         int err;
3703
3704         if (!pin)
3705                 return 0;
3706
3707         spec->multiout.hp_nid = VT1708S_HP_NID; /* AOW3 */
3708         spec->hp_independent_mode_index = 1;
3709
3710         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3711                               "Headphone Playback Volume",
3712                               HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
3713         if (err < 0)
3714                 return err;
3715
3716         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3717                               "Headphone Playback Switch",
3718                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3719         if (err < 0)
3720                 return err;
3721
3722         create_hp_imux(spec);
3723
3724         return 0;
3725 }
3726
3727 /* create playback/capture controls for input pins */
3728 static int vt1708S_auto_create_analog_input_ctls(struct hda_codec *codec,
3729                                                 const struct auto_pin_cfg *cfg)
3730 {
3731         return vt_auto_create_analog_input_ctls(codec, cfg, 0x16);
3732 }
3733
3734 /* fill out digital output widgets; one for master and one for slave outputs */
3735 static void fill_dig_outs(struct hda_codec *codec)
3736 {
3737         struct via_spec *spec = codec->spec;
3738         int i;
3739
3740         for (i = 0; i < spec->autocfg.dig_outs; i++) {
3741                 hda_nid_t nid;
3742                 int conn;
3743
3744                 nid = spec->autocfg.dig_out_pins[i];
3745                 if (!nid)
3746                         continue;
3747                 conn = snd_hda_get_connections(codec, nid, &nid, 1);
3748                 if (conn < 1)
3749                         continue;
3750                 if (!spec->multiout.dig_out_nid)
3751                         spec->multiout.dig_out_nid = nid;
3752                 else {
3753                         spec->slave_dig_outs[0] = nid;
3754                         break; /* at most two dig outs */
3755                 }
3756         }
3757 }
3758
3759 static int vt1708S_parse_auto_config(struct hda_codec *codec)
3760 {
3761         struct via_spec *spec = codec->spec;
3762         int err;
3763
3764         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3765         if (err < 0)
3766                 return err;
3767         err = vt1708S_auto_fill_dac_nids(spec, &spec->autocfg);
3768         if (err < 0)
3769                 return err;
3770         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3771                 return 0; /* can't find valid BIOS pin config */
3772
3773         err = vt1708S_auto_create_multi_out_ctls(codec, &spec->autocfg);
3774         if (err < 0)
3775                 return err;
3776         err = vt1708S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
3777         if (err < 0)
3778                 return err;
3779         err = vt1708S_auto_create_analog_input_ctls(codec, &spec->autocfg);
3780         if (err < 0)
3781                 return err;
3782
3783         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3784
3785         fill_dig_outs(codec);
3786
3787         if (spec->kctls.list)
3788                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
3789
3790         spec->input_mux = &spec->private_imux[0];
3791
3792         if (spec->hp_mux)
3793                 via_hp_build(codec);
3794
3795         via_smart51_build(spec);
3796         return 1;
3797 }
3798
3799 #ifdef CONFIG_SND_HDA_POWER_SAVE
3800 static const struct hda_amp_list vt1708S_loopbacks[] = {
3801         { 0x16, HDA_INPUT, 1 },
3802         { 0x16, HDA_INPUT, 2 },
3803         { 0x16, HDA_INPUT, 3 },
3804         { 0x16, HDA_INPUT, 4 },
3805         { } /* end */
3806 };
3807 #endif
3808
3809 static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
3810                                int offset, int num_steps, int step_size)
3811 {
3812         snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
3813                                   (offset << AC_AMPCAP_OFFSET_SHIFT) |
3814                                   (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
3815                                   (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
3816                                   (0 << AC_AMPCAP_MUTE_SHIFT));
3817 }
3818
3819 static int patch_vt1708S(struct hda_codec *codec)
3820 {
3821         struct via_spec *spec;
3822         int err;
3823
3824         /* create a codec specific record */
3825         spec = via_new_spec(codec);
3826         if (spec == NULL)
3827                 return -ENOMEM;
3828
3829         /* automatic parse from the BIOS config */
3830         err = vt1708S_parse_auto_config(codec);
3831         if (err < 0) {
3832                 via_free(codec);
3833                 return err;
3834         } else if (!err) {
3835                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
3836                        "from BIOS.  Using genenic mode...\n");
3837         }
3838
3839         spec->init_verbs[spec->num_iverbs++] = vt1708S_volume_init_verbs;
3840         if (codec->vendor_id == 0x11064397)
3841                 spec->init_verbs[spec->num_iverbs++] =
3842                         vt1705_uniwill_init_verbs;
3843         else
3844                 spec->init_verbs[spec->num_iverbs++] =
3845                         vt1708S_uniwill_init_verbs;
3846
3847         if (codec->vendor_id == 0x11064397)
3848                 spec->stream_analog_playback = &vt1705_pcm_analog_playback;
3849         else
3850                 spec->stream_analog_playback = &vt1708S_pcm_analog_playback;
3851         spec->stream_analog_capture = &vt1708S_pcm_analog_capture;
3852
3853         spec->stream_digital_playback = &vt1708S_pcm_digital_playback;
3854
3855         if (spec->adc_nids && spec->input_mux) {
3856                 override_mic_boost(codec, 0x1a, 0, 3, 40);
3857                 override_mic_boost(codec, 0x1e, 0, 3, 40);
3858                 spec->mixers[spec->num_mixers] = vt1708S_capture_mixer;
3859                 spec->num_mixers++;
3860         }
3861
3862         codec->patch_ops = via_patch_ops;
3863
3864         codec->patch_ops.init = via_auto_init;
3865         codec->patch_ops.unsol_event = via_unsol_event;
3866 #ifdef CONFIG_SND_HDA_POWER_SAVE
3867         spec->loopback.amplist = vt1708S_loopbacks;
3868 #endif
3869
3870         /* correct names for VT1708BCE */
3871         if (get_codec_type(codec) == VT1708BCE) {
3872                 kfree(codec->chip_name);
3873                 codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
3874                 snprintf(codec->bus->card->mixername,
3875                          sizeof(codec->bus->card->mixername),
3876                          "%s %s", codec->vendor_name, codec->chip_name);
3877         }
3878         /* correct names for VT1705 */
3879         if (codec->vendor_id == 0x11064397)     {
3880                 kfree(codec->chip_name);
3881                 codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
3882                 snprintf(codec->bus->card->mixername,
3883                          sizeof(codec->bus->card->mixername),
3884                          "%s %s", codec->vendor_name, codec->chip_name);
3885         }
3886         spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
3887         return 0;
3888 }
3889
3890 /* Patch for VT1702 */
3891
3892 /* capture mixer elements */
3893 static const struct snd_kcontrol_new vt1702_capture_mixer[] = {
3894         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_INPUT),
3895         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_INPUT),
3896         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x20, 0x0, HDA_INPUT),
3897         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x20, 0x0, HDA_INPUT),
3898         HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x1F, 0x0, HDA_INPUT),
3899         HDA_CODEC_MUTE("Digital Mic Capture Switch", 0x1F, 0x0, HDA_INPUT),
3900         HDA_CODEC_VOLUME("Digital Mic Boost Capture Volume", 0x1E, 0x0,
3901                          HDA_INPUT),
3902         {
3903                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3904                 /* The multiple "Capture Source" controls confuse alsamixer
3905                  * So call somewhat different..
3906                  */
3907                 /* .name = "Capture Source", */
3908                 .name = "Input Source",
3909                 .count = 1,
3910                 .info = via_mux_enum_info,
3911                 .get = via_mux_enum_get,
3912                 .put = via_mux_enum_put,
3913         },
3914         { } /* end */
3915 };
3916
3917 static const struct hda_verb vt1702_volume_init_verbs[] = {
3918         /*
3919          * Unmute ADC0-1 and set the default input to mic-in
3920          */
3921         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3922         {0x1F, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3923         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3924
3925
3926         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3927          * mixer widget
3928          */
3929         /* Amp Indices: Mic1 = 1, Line = 1, Mic2 = 3 */
3930         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3931         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3932         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3933         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3934         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3935
3936         /* Setup default input of PW4 to MW0 */
3937         {0x17, AC_VERB_SET_CONNECT_SEL, 0x1},
3938         /* PW6 PW7 Output enable */
3939         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3940         {0x1C, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3941         /* mixer enable */
3942         {0x1, 0xF88, 0x3},
3943         /* GPIO 0~2 */
3944         {0x1, 0xF82, 0x3F},
3945         { }
3946 };
3947
3948 static const struct hda_verb vt1702_uniwill_init_verbs[] = {
3949         {0x17, AC_VERB_SET_UNSOLICITED_ENABLE,
3950          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3951         {0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3952         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3953         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3954         {0x18, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3955         { }
3956 };
3957
3958 static const struct hda_pcm_stream vt1702_pcm_analog_playback = {
3959         .substreams = 2,
3960         .channels_min = 2,
3961         .channels_max = 2,
3962         .nid = 0x10, /* NID to query formats and rates */
3963         .ops = {
3964                 .open = via_playback_pcm_open,
3965                 .prepare = via_playback_multi_pcm_prepare,
3966                 .cleanup = via_playback_multi_pcm_cleanup,
3967                 .close = via_pcm_open_close
3968         },
3969 };
3970
3971 static const struct hda_pcm_stream vt1702_pcm_analog_capture = {
3972         .substreams = 3,
3973         .channels_min = 2,
3974         .channels_max = 2,
3975         .nid = 0x12, /* NID to query formats and rates */
3976         .ops = {
3977                 .open = via_pcm_open_close,
3978                 .prepare = via_capture_pcm_prepare,
3979                 .cleanup = via_capture_pcm_cleanup,
3980                 .close = via_pcm_open_close
3981         },
3982 };
3983
3984 static const struct hda_pcm_stream vt1702_pcm_digital_playback = {
3985         .substreams = 2,
3986         .channels_min = 2,
3987         .channels_max = 2,
3988         /* NID is set in via_build_pcms */
3989         .ops = {
3990                 .open = via_dig_playback_pcm_open,
3991                 .close = via_dig_playback_pcm_close,
3992                 .prepare = via_dig_playback_pcm_prepare,
3993                 .cleanup = via_dig_playback_pcm_cleanup
3994         },
3995 };
3996
3997 /* fill in the dac_nids table from the parsed pin configuration */
3998 static int vt1702_auto_fill_dac_nids(struct via_spec *spec,
3999                                      const struct auto_pin_cfg *cfg)
4000 {
4001         spec->multiout.num_dacs = 1;
4002         spec->multiout.dac_nids = spec->private_dac_nids;
4003
4004         if (cfg->line_out_pins[0]) {
4005                 /* config dac list */
4006                 spec->private_dac_nids[0] = 0x10;
4007         }
4008
4009         return 0;
4010 }
4011
4012 /* add playback controls from the parsed DAC table */
4013 static int vt1702_auto_create_line_out_ctls(struct via_spec *spec,
4014                                              const struct auto_pin_cfg *cfg)
4015 {
4016         int err;
4017
4018         if (!cfg->line_out_pins[0])
4019                 return -1;
4020
4021         /* add control to mixer index 0 */
4022         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4023                               "Master Front Playback Volume",
4024                               HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
4025         if (err < 0)
4026                 return err;
4027         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4028                               "Master Front Playback Switch",
4029                               HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
4030         if (err < 0)
4031                 return err;
4032
4033         /* Front */
4034         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4035                               "Front Playback Volume",
4036                               HDA_COMPOSE_AMP_VAL(0x10, 3, 0, HDA_OUTPUT));
4037         if (err < 0)
4038                 return err;
4039         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4040                               "Front Playback Switch",
4041                               HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_OUTPUT));
4042         if (err < 0)
4043                 return err;
4044
4045         return 0;
4046 }
4047
4048 static int vt1702_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
4049 {
4050         int err, i;
4051         struct hda_input_mux *imux;
4052         static const char * const texts[] = { "ON", "OFF", NULL};
4053         if (!pin)
4054                 return 0;
4055         spec->multiout.hp_nid = 0x1D;
4056         spec->hp_independent_mode_index = 0;
4057
4058         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4059                               "Headphone Playback Volume",
4060                               HDA_COMPOSE_AMP_VAL(0x1D, 3, 0, HDA_OUTPUT));
4061         if (err < 0)
4062                 return err;
4063
4064         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4065                               "Headphone Playback Switch",
4066                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
4067         if (err < 0)
4068                 return err;
4069
4070         imux = &spec->private_imux[1];
4071
4072         /* for hp mode select */
4073         for (i = 0; texts[i]; i++)
4074                 snd_hda_add_imux_item(imux, texts[i], i, NULL);
4075
4076         spec->hp_mux = &spec->private_imux[1];
4077         return 0;
4078 }
4079
4080 /* create playback/capture controls for input pins */
4081 static int vt1702_auto_create_analog_input_ctls(struct hda_codec *codec,
4082                                                 const struct auto_pin_cfg *cfg)
4083 {
4084         return vt_auto_create_analog_input_ctls(codec, cfg, 0x1a);
4085 }
4086
4087 static int vt1702_parse_auto_config(struct hda_codec *codec)
4088 {
4089         struct via_spec *spec = codec->spec;
4090         int err;
4091
4092         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4093         if (err < 0)
4094                 return err;
4095         err = vt1702_auto_fill_dac_nids(spec, &spec->autocfg);
4096         if (err < 0)
4097                 return err;
4098         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
4099                 return 0; /* can't find valid BIOS pin config */
4100
4101         err = vt1702_auto_create_line_out_ctls(spec, &spec->autocfg);
4102         if (err < 0)
4103                 return err;
4104         err = vt1702_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
4105         if (err < 0)
4106                 return err;
4107         /* limit AA path volume to 0 dB */
4108         snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
4109                                   (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
4110                                   (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
4111                                   (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
4112                                   (1 << AC_AMPCAP_MUTE_SHIFT));
4113         err = vt1702_auto_create_analog_input_ctls(codec, &spec->autocfg);
4114         if (err < 0)
4115                 return err;
4116
4117         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4118
4119         fill_dig_outs(codec);
4120
4121         if (spec->kctls.list)
4122                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
4123
4124         spec->input_mux = &spec->private_imux[0];
4125
4126         if (spec->hp_mux)
4127                 via_hp_build(codec);
4128
4129         return 1;
4130 }
4131
4132 #ifdef CONFIG_SND_HDA_POWER_SAVE
4133 static const struct hda_amp_list vt1702_loopbacks[] = {
4134         { 0x1A, HDA_INPUT, 1 },
4135         { 0x1A, HDA_INPUT, 2 },
4136         { 0x1A, HDA_INPUT, 3 },
4137         { 0x1A, HDA_INPUT, 4 },
4138         { } /* end */
4139 };
4140 #endif
4141
4142 static void set_widgets_power_state_vt1702(struct hda_codec *codec)
4143 {
4144         int imux_is_smixer =
4145         snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
4146         unsigned int parm;
4147         /* inputs */
4148         /* PW 1/2/5 (14h/15h/18h) */
4149         parm = AC_PWRST_D3;
4150         set_pin_power_state(codec, 0x14, &parm);
4151         set_pin_power_state(codec, 0x15, &parm);
4152         set_pin_power_state(codec, 0x18, &parm);
4153         if (imux_is_smixer)
4154                 parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
4155         /* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
4156         snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
4157         snd_hda_codec_write(codec, 0x12, 0, AC_VERB_SET_POWER_STATE, parm);
4158         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
4159         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_POWER_STATE, parm);
4160
4161         /* outputs */
4162         /* PW 3/4 (16h/17h) */
4163         parm = AC_PWRST_D3;
4164         set_pin_power_state(codec, 0x17, &parm);
4165         set_pin_power_state(codec, 0x16, &parm);
4166         /* MW0 (1ah), AOW 0/1 (10h/1dh) */
4167         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
4168                             imux_is_smixer ? AC_PWRST_D0 : parm);
4169         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
4170         snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
4171 }
4172
4173 static int patch_vt1702(struct hda_codec *codec)
4174 {
4175         struct via_spec *spec;
4176         int err;
4177
4178         /* create a codec specific record */
4179         spec = via_new_spec(codec);
4180         if (spec == NULL)
4181                 return -ENOMEM;
4182
4183         /* automatic parse from the BIOS config */
4184         err = vt1702_parse_auto_config(codec);
4185         if (err < 0) {
4186                 via_free(codec);
4187                 return err;
4188         } else if (!err) {
4189                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
4190                        "from BIOS.  Using genenic mode...\n");
4191         }
4192
4193         spec->init_verbs[spec->num_iverbs++] = vt1702_volume_init_verbs;
4194         spec->init_verbs[spec->num_iverbs++] = vt1702_uniwill_init_verbs;
4195
4196         spec->stream_analog_playback = &vt1702_pcm_analog_playback;
4197         spec->stream_analog_capture = &vt1702_pcm_analog_capture;
4198
4199         spec->stream_digital_playback = &vt1702_pcm_digital_playback;
4200
4201         if (spec->adc_nids && spec->input_mux) {
4202                 spec->mixers[spec->num_mixers] = vt1702_capture_mixer;
4203                 spec->num_mixers++;
4204         }
4205
4206         codec->patch_ops = via_patch_ops;
4207
4208         codec->patch_ops.init = via_auto_init;
4209         codec->patch_ops.unsol_event = via_unsol_event;
4210 #ifdef CONFIG_SND_HDA_POWER_SAVE
4211         spec->loopback.amplist = vt1702_loopbacks;
4212 #endif
4213
4214         spec->set_widgets_power_state =  set_widgets_power_state_vt1702;
4215         return 0;
4216 }
4217
4218 /* Patch for VT1718S */
4219
4220 /* capture mixer elements */
4221 static const struct snd_kcontrol_new vt1718S_capture_mixer[] = {
4222         HDA_CODEC_VOLUME("Capture Volume", 0x10, 0x0, HDA_INPUT),
4223         HDA_CODEC_MUTE("Capture Switch", 0x10, 0x0, HDA_INPUT),
4224         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x11, 0x0, HDA_INPUT),
4225         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x11, 0x0, HDA_INPUT),
4226         HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x2b, 0x0, HDA_INPUT),
4227         HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x29, 0x0,
4228                          HDA_INPUT),
4229         {
4230                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4231                 /* The multiple "Capture Source" controls confuse alsamixer
4232                  * So call somewhat different..
4233                  */
4234                 .name = "Input Source",
4235                 .count = 2,
4236                 .info = via_mux_enum_info,
4237                 .get = via_mux_enum_get,
4238                 .put = via_mux_enum_put,
4239         },
4240         { } /* end */
4241 };
4242
4243 static const struct hda_verb vt1718S_volume_init_verbs[] = {
4244         /*
4245          * Unmute ADC0-1 and set the default input to mic-in
4246          */
4247         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4248         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4249
4250         /* Enable MW0 adjust Gain 5 */
4251         {0x1, 0xfb2, 0x10},
4252         /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
4253          * mixer widget
4254          */
4255         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
4256         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4257         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4258         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
4259         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
4260         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(5)},
4261
4262         /* Setup default input of Front HP to MW9 */
4263         {0x28, AC_VERB_SET_CONNECT_SEL, 0x1},
4264         /* PW9 PW10 Output enable */
4265         {0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
4266         {0x2e, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
4267         /* PW11 Input enable */
4268         {0x2f, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_IN_EN},
4269         /* Enable Boost Volume backdoor */
4270         {0x1, 0xf88, 0x8},
4271         /* MW0/1/2/3/4: un-mute index 0 (AOWx), mute index 1 (MW9) */
4272         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4273         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4274         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4275         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4276         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4277         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4278         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4279         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4280         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4281         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4282         /* set MUX1 = 2 (AOW4), MUX2 = 1 (AOW3) */
4283         {0x34, AC_VERB_SET_CONNECT_SEL, 0x2},
4284         {0x35, AC_VERB_SET_CONNECT_SEL, 0x1},
4285         /* Unmute MW4's index 0 */
4286         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4287         { }
4288 };
4289
4290
4291 static const struct hda_verb vt1718S_uniwill_init_verbs[] = {
4292         {0x28, AC_VERB_SET_UNSOLICITED_ENABLE,
4293          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
4294         {0x24, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4295         {0x25, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4296         {0x26, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4297         {0x27, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4298         {0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4299         {0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4300         {0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4301         { }
4302 };
4303
4304 static const struct hda_pcm_stream vt1718S_pcm_analog_playback = {
4305         .substreams = 2,
4306         .channels_min = 2,
4307         .channels_max = 10,
4308         .nid = 0x8, /* NID to query formats and rates */
4309         .ops = {
4310                 .open = via_playback_pcm_open,
4311                 .prepare = via_playback_multi_pcm_prepare,
4312                 .cleanup = via_playback_multi_pcm_cleanup,
4313                 .close = via_pcm_open_close,
4314         },
4315 };
4316
4317 static const struct hda_pcm_stream vt1718S_pcm_analog_capture = {
4318         .substreams = 2,
4319         .channels_min = 2,
4320         .channels_max = 2,
4321         .nid = 0x10, /* NID to query formats and rates */
4322         .ops = {
4323                 .open = via_pcm_open_close,
4324                 .prepare = via_capture_pcm_prepare,
4325                 .cleanup = via_capture_pcm_cleanup,
4326                 .close = via_pcm_open_close,
4327         },
4328 };
4329
4330 static const struct hda_pcm_stream vt1718S_pcm_digital_playback = {
4331         .substreams = 2,
4332         .channels_min = 2,
4333         .channels_max = 2,
4334         /* NID is set in via_build_pcms */
4335         .ops = {
4336                 .open = via_dig_playback_pcm_open,
4337                 .close = via_dig_playback_pcm_close,
4338                 .prepare = via_dig_playback_pcm_prepare,
4339                 .cleanup = via_dig_playback_pcm_cleanup
4340         },
4341 };
4342
4343 static const struct hda_pcm_stream vt1718S_pcm_digital_capture = {
4344         .substreams = 1,
4345         .channels_min = 2,
4346         .channels_max = 2,
4347 };
4348
4349 /* fill in the dac_nids table from the parsed pin configuration */
4350 static int vt1718S_auto_fill_dac_nids(struct via_spec *spec,
4351                                      const struct auto_pin_cfg *cfg)
4352 {
4353         int i;
4354         hda_nid_t nid;
4355
4356         spec->multiout.num_dacs = cfg->line_outs;
4357
4358         spec->multiout.dac_nids = spec->private_dac_nids;
4359
4360         for (i = 0; i < 4; i++) {
4361                 nid = cfg->line_out_pins[i];
4362                 if (nid) {
4363                         /* config dac list */
4364                         switch (i) {
4365                         case AUTO_SEQ_FRONT:
4366                                 spec->private_dac_nids[i] = 0x8;
4367                                 break;
4368                         case AUTO_SEQ_CENLFE:
4369                                 spec->private_dac_nids[i] = 0xa;
4370                                 break;
4371                         case AUTO_SEQ_SURROUND:
4372                                 spec->private_dac_nids[i] = 0x9;
4373                                 break;
4374                         case AUTO_SEQ_SIDE:
4375                                 spec->private_dac_nids[i] = 0xb;
4376                                 break;
4377                         }
4378                 }
4379         }
4380
4381         return 0;
4382 }
4383
4384 /* add playback controls from the parsed DAC table */
4385 static int vt1718S_auto_create_multi_out_ctls(struct via_spec *spec,
4386                                              const struct auto_pin_cfg *cfg)
4387 {
4388         char name[32];
4389         static const char * const chname[4] = {
4390                 "Front", "Surround", "C/LFE", "Side"
4391         };
4392         hda_nid_t nid_vols[] = {0x8, 0x9, 0xa, 0xb};
4393         hda_nid_t nid_mutes[] = {0x24, 0x25, 0x26, 0x27};
4394         hda_nid_t nid, nid_vol, nid_mute = 0;
4395         int i, err;
4396
4397         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
4398                 nid = cfg->line_out_pins[i];
4399
4400                 if (!nid)
4401                         continue;
4402                 nid_vol = nid_vols[i];
4403                 nid_mute = nid_mutes[i];
4404
4405                 if (i == AUTO_SEQ_CENLFE) {
4406                         /* Center/LFE */
4407                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4408                                               "Center Playback Volume",
4409                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
4410                                                                   HDA_OUTPUT));
4411                         if (err < 0)
4412                                 return err;
4413                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4414                                               "LFE Playback Volume",
4415                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
4416                                                                   HDA_OUTPUT));
4417                         if (err < 0)
4418                                 return err;
4419                         err = via_add_control(
4420                                 spec, VIA_CTL_WIDGET_MUTE,
4421                                 "Center Playback Switch",
4422                                 HDA_COMPOSE_AMP_VAL(nid_mute, 1, 0,
4423                                                     HDA_OUTPUT));
4424                         if (err < 0)
4425                                 return err;
4426                         err = via_add_control(
4427                                 spec, VIA_CTL_WIDGET_MUTE,
4428                                 "LFE Playback Switch",
4429                                 HDA_COMPOSE_AMP_VAL(nid_mute, 2, 0,
4430                                                     HDA_OUTPUT));
4431                         if (err < 0)
4432                                 return err;
4433                 } else if (i == AUTO_SEQ_FRONT) {
4434                         /* Front */
4435                         sprintf(name, "%s Playback Volume", chname[i]);
4436                         err = via_add_control(
4437                                 spec, VIA_CTL_WIDGET_VOL, name,
4438                                 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
4439                         if (err < 0)
4440                                 return err;
4441                         sprintf(name, "%s Playback Switch", chname[i]);
4442                         err = via_add_control(
4443                                 spec, VIA_CTL_WIDGET_MUTE, name,
4444                                 HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
4445                                                     HDA_OUTPUT));
4446                         if (err < 0)
4447                                 return err;
4448                 } else {
4449                         sprintf(name, "%s Playback Volume", chname[i]);
4450                         err = via_add_control(
4451                                 spec, VIA_CTL_WIDGET_VOL, name,
4452                                 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
4453                         if (err < 0)
4454                                 return err;
4455                         sprintf(name, "%s Playback Switch", chname[i]);
4456                         err = via_add_control(
4457                                 spec, VIA_CTL_WIDGET_MUTE, name,
4458                                 HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
4459                                                     HDA_OUTPUT));
4460                         if (err < 0)
4461                                 return err;
4462                 }
4463         }
4464         return 0;
4465 }
4466
4467 static int vt1718S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
4468 {
4469         int err;
4470
4471         if (!pin)
4472                 return 0;
4473
4474         spec->multiout.hp_nid = 0xc; /* AOW4 */
4475         spec->hp_independent_mode_index = 1;
4476
4477         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4478                               "Headphone Playback Volume",
4479                               HDA_COMPOSE_AMP_VAL(0xc, 3, 0, HDA_OUTPUT));
4480         if (err < 0)
4481                 return err;
4482
4483         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4484                               "Headphone Playback Switch",
4485                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
4486         if (err < 0)
4487                 return err;
4488
4489         create_hp_imux(spec);
4490         return 0;
4491 }
4492
4493 /* create playback/capture controls for input pins */
4494 static int vt1718S_auto_create_analog_input_ctls(struct hda_codec *codec,
4495                                                 const struct auto_pin_cfg *cfg)
4496 {
4497         return vt_auto_create_analog_input_ctls(codec, cfg, 0x21);
4498 }
4499
4500 static int vt1718S_parse_auto_config(struct hda_codec *codec)
4501 {
4502         struct via_spec *spec = codec->spec;
4503         int err;
4504
4505         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4506
4507         if (err < 0)
4508                 return err;
4509         err = vt1718S_auto_fill_dac_nids(spec, &spec->autocfg);
4510         if (err < 0)
4511                 return err;
4512         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
4513                 return 0; /* can't find valid BIOS pin config */
4514
4515         err = vt1718S_auto_create_multi_out_ctls(spec, &spec->autocfg);
4516         if (err < 0)
4517                 return err;
4518         err = vt1718S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
4519         if (err < 0)
4520                 return err;
4521         err = vt1718S_auto_create_analog_input_ctls(codec, &spec->autocfg);
4522         if (err < 0)
4523                 return err;
4524
4525         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4526
4527         fill_dig_outs(codec);
4528
4529         if (spec->autocfg.dig_in_pin && codec->vendor_id == 0x11060428)
4530                 spec->dig_in_nid = 0x13;
4531
4532         if (spec->kctls.list)
4533                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
4534
4535         spec->input_mux = &spec->private_imux[0];
4536
4537         if (spec->hp_mux)
4538                 via_hp_build(codec);
4539
4540         via_smart51_build(spec);
4541
4542         return 1;
4543 }
4544
4545 #ifdef CONFIG_SND_HDA_POWER_SAVE
4546 static const struct hda_amp_list vt1718S_loopbacks[] = {
4547         { 0x21, HDA_INPUT, 1 },
4548         { 0x21, HDA_INPUT, 2 },
4549         { 0x21, HDA_INPUT, 3 },
4550         { 0x21, HDA_INPUT, 4 },
4551         { } /* end */
4552 };
4553 #endif
4554
4555 static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
4556 {
4557         struct via_spec *spec = codec->spec;
4558         int imux_is_smixer;
4559         unsigned int parm;
4560         /* MUX6 (1eh) = stereo mixer */
4561         imux_is_smixer =
4562         snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
4563         /* inputs */
4564         /* PW 5/6/7 (29h/2ah/2bh) */
4565         parm = AC_PWRST_D3;
4566         set_pin_power_state(codec, 0x29, &parm);
4567         set_pin_power_state(codec, 0x2a, &parm);
4568         set_pin_power_state(codec, 0x2b, &parm);
4569         if (imux_is_smixer)
4570                 parm = AC_PWRST_D0;
4571         /* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
4572         snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
4573         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
4574         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
4575         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
4576
4577         /* outputs */
4578         /* PW3 (27h), MW2 (1ah), AOW3 (bh) */
4579         parm = AC_PWRST_D3;
4580         set_pin_power_state(codec, 0x27, &parm);
4581         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE, parm);
4582         snd_hda_codec_write(codec, 0xb, 0, AC_VERB_SET_POWER_STATE, parm);
4583
4584         /* PW2 (26h), AOW2 (ah) */
4585         parm = AC_PWRST_D3;
4586         set_pin_power_state(codec, 0x26, &parm);
4587         if (spec->smart51_enabled)
4588                 set_pin_power_state(codec, 0x2b, &parm);
4589         snd_hda_codec_write(codec, 0xa, 0, AC_VERB_SET_POWER_STATE, parm);
4590
4591         /* PW0 (24h), AOW0 (8h) */
4592         parm = AC_PWRST_D3;
4593         set_pin_power_state(codec, 0x24, &parm);
4594         if (!spec->hp_independent_mode) /* check for redirected HP */
4595                 set_pin_power_state(codec, 0x28, &parm);
4596         snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
4597         /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
4598         snd_hda_codec_write(codec, 0x21, 0, AC_VERB_SET_POWER_STATE,
4599                             imux_is_smixer ? AC_PWRST_D0 : parm);
4600
4601         /* PW1 (25h), AOW1 (9h) */
4602         parm = AC_PWRST_D3;
4603         set_pin_power_state(codec, 0x25, &parm);
4604         if (spec->smart51_enabled)
4605                 set_pin_power_state(codec, 0x2a, &parm);
4606         snd_hda_codec_write(codec, 0x9, 0, AC_VERB_SET_POWER_STATE, parm);
4607
4608         if (spec->hp_independent_mode) {
4609                 /* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
4610                 parm = AC_PWRST_D3;
4611                 set_pin_power_state(codec, 0x28, &parm);
4612                 snd_hda_codec_write(codec, 0x1b, 0,
4613                                     AC_VERB_SET_POWER_STATE, parm);
4614                 snd_hda_codec_write(codec, 0x34, 0,
4615                                     AC_VERB_SET_POWER_STATE, parm);
4616                 snd_hda_codec_write(codec, 0xc, 0,
4617                                     AC_VERB_SET_POWER_STATE, parm);
4618         }
4619 }
4620
4621 static int patch_vt1718S(struct hda_codec *codec)
4622 {
4623         struct via_spec *spec;
4624         int err;
4625
4626         /* create a codec specific record */
4627         spec = via_new_spec(codec);
4628         if (spec == NULL)
4629                 return -ENOMEM;
4630
4631         /* automatic parse from the BIOS config */
4632         err = vt1718S_parse_auto_config(codec);
4633         if (err < 0) {
4634                 via_free(codec);
4635                 return err;
4636         } else if (!err) {
4637                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
4638                        "from BIOS.  Using genenic mode...\n");
4639         }
4640
4641         spec->init_verbs[spec->num_iverbs++] = vt1718S_volume_init_verbs;
4642         spec->init_verbs[spec->num_iverbs++] = vt1718S_uniwill_init_verbs;
4643
4644         spec->stream_analog_playback = &vt1718S_pcm_analog_playback;
4645         spec->stream_analog_capture = &vt1718S_pcm_analog_capture;
4646
4647         spec->stream_digital_playback = &vt1718S_pcm_digital_playback;
4648         if (codec->vendor_id == 0x11060428 || codec->vendor_id == 0x11060441)
4649                 spec->stream_digital_capture = &vt1718S_pcm_digital_capture;
4650
4651         if (spec->adc_nids && spec->input_mux) {
4652                 override_mic_boost(codec, 0x2b, 0, 3, 40);
4653                 override_mic_boost(codec, 0x29, 0, 3, 40);
4654                 spec->mixers[spec->num_mixers] = vt1718S_capture_mixer;
4655                 spec->num_mixers++;
4656         }
4657
4658         codec->patch_ops = via_patch_ops;
4659
4660         codec->patch_ops.init = via_auto_init;
4661         codec->patch_ops.unsol_event = via_unsol_event;
4662
4663 #ifdef CONFIG_SND_HDA_POWER_SAVE
4664         spec->loopback.amplist = vt1718S_loopbacks;
4665 #endif
4666
4667         spec->set_widgets_power_state =  set_widgets_power_state_vt1718S;
4668
4669         return 0;
4670 }
4671
4672 /* Patch for VT1716S */
4673
4674 static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
4675                             struct snd_ctl_elem_info *uinfo)
4676 {
4677         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
4678         uinfo->count = 1;
4679         uinfo->value.integer.min = 0;
4680         uinfo->value.integer.max = 1;
4681         return 0;
4682 }
4683
4684 static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
4685                            struct snd_ctl_elem_value *ucontrol)
4686 {
4687         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4688         int index = 0;
4689
4690         index = snd_hda_codec_read(codec, 0x26, 0,
4691                                                AC_VERB_GET_CONNECT_SEL, 0);
4692         if (index != -1)
4693                 *ucontrol->value.integer.value = index;
4694
4695         return 0;
4696 }
4697
4698 static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
4699                            struct snd_ctl_elem_value *ucontrol)
4700 {
4701         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4702         struct via_spec *spec = codec->spec;
4703         int index = *ucontrol->value.integer.value;
4704
4705         snd_hda_codec_write(codec, 0x26, 0,
4706                                                AC_VERB_SET_CONNECT_SEL, index);
4707         spec->dmic_enabled = index;
4708         set_widgets_power_state(codec);
4709         return 1;
4710 }
4711
4712 /* capture mixer elements */
4713 static const struct snd_kcontrol_new vt1716S_capture_mixer[] = {
4714         HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
4715         HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
4716         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
4717         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
4718         HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x1A, 0x0, HDA_INPUT),
4719         HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x1E, 0x0,
4720                          HDA_INPUT),
4721         {
4722                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4723                 .name = "Input Source",
4724                 .count = 1,
4725                 .info = via_mux_enum_info,
4726                 .get = via_mux_enum_get,
4727                 .put = via_mux_enum_put,
4728         },
4729         { } /* end */
4730 };
4731
4732 static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
4733         HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
4734         {
4735          .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4736          .name = "Digital Mic Capture Switch",
4737          .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
4738          .count = 1,
4739          .info = vt1716s_dmic_info,
4740          .get = vt1716s_dmic_get,
4741          .put = vt1716s_dmic_put,
4742          },
4743         {}                      /* end */
4744 };
4745
4746
4747 /* mono-out mixer elements */
4748 static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
4749         HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
4750         { } /* end */
4751 };
4752
4753 static const struct hda_verb vt1716S_volume_init_verbs[] = {
4754         /*
4755          * Unmute ADC0-1 and set the default input to mic-in
4756          */
4757         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4758         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4759
4760
4761         /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
4762          * mixer widget
4763          */
4764         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
4765         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4766         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4767         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
4768         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
4769         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
4770
4771         /* MUX Indices: Stereo Mixer = 5 */
4772         {0x17, AC_VERB_SET_CONNECT_SEL, 0x5},
4773
4774         /* Setup default input of PW4 to MW0 */
4775         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
4776
4777         /* Setup default input of SW1 as MW0 */
4778         {0x18, AC_VERB_SET_CONNECT_SEL, 0x1},
4779
4780         /* Setup default input of SW4 as AOW0 */
4781         {0x28, AC_VERB_SET_CONNECT_SEL, 0x1},
4782
4783         /* PW9 PW10 Output enable */
4784         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
4785         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
4786
4787         /* Unmute SW1, PW12 */
4788         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4789         {0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4790         /* PW12 Output enable */
4791         {0x2a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
4792         /* Enable Boost Volume backdoor */
4793         {0x1, 0xf8a, 0x80},
4794         /* don't bybass mixer */
4795         {0x1, 0xf88, 0xc0},
4796         /* Enable mono output */
4797         {0x1, 0xf90, 0x08},
4798         { }
4799 };
4800
4801
4802 static const struct hda_verb vt1716S_uniwill_init_verbs[] = {
4803         {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
4804          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
4805         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4806         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4807         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4808         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE,
4809          AC_USRSP_EN | VIA_MONO_EVENT | VIA_JACK_EVENT},
4810         {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4811         {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4812         { }
4813 };
4814
4815 static const struct hda_pcm_stream vt1716S_pcm_analog_playback = {
4816         .substreams = 2,
4817         .channels_min = 2,
4818         .channels_max = 6,
4819         .nid = 0x10, /* NID to query formats and rates */
4820         .ops = {
4821                 .open = via_playback_pcm_open,
4822                 .prepare = via_playback_multi_pcm_prepare,
4823                 .cleanup = via_playback_multi_pcm_cleanup,
4824                 .close = via_pcm_open_close,
4825         },
4826 };
4827
4828 static const struct hda_pcm_stream vt1716S_pcm_analog_capture = {
4829         .substreams = 2,
4830         .channels_min = 2,
4831         .channels_max = 2,
4832         .nid = 0x13, /* NID to query formats and rates */
4833         .ops = {
4834                 .open = via_pcm_open_close,
4835                 .prepare = via_capture_pcm_prepare,
4836                 .cleanup = via_capture_pcm_cleanup,
4837                 .close = via_pcm_open_close,
4838         },
4839 };
4840
4841 static const struct hda_pcm_stream vt1716S_pcm_digital_playback = {
4842         .substreams = 2,
4843         .channels_min = 2,
4844         .channels_max = 2,
4845         /* NID is set in via_build_pcms */
4846         .ops = {
4847                 .open = via_dig_playback_pcm_open,
4848                 .close = via_dig_playback_pcm_close,
4849                 .prepare = via_dig_playback_pcm_prepare,
4850                 .cleanup = via_dig_playback_pcm_cleanup
4851         },
4852 };
4853
4854 /* fill in the dac_nids table from the parsed pin configuration */
4855 static int vt1716S_auto_fill_dac_nids(struct via_spec *spec,
4856                                       const struct auto_pin_cfg *cfg)
4857 {       int i;
4858         hda_nid_t nid;
4859
4860         spec->multiout.num_dacs = cfg->line_outs;
4861
4862         spec->multiout.dac_nids = spec->private_dac_nids;
4863
4864         for (i = 0; i < 3; i++) {
4865                 nid = cfg->line_out_pins[i];
4866                 if (nid) {
4867                         /* config dac list */
4868                         switch (i) {
4869                         case AUTO_SEQ_FRONT:
4870                                 spec->private_dac_nids[i] = 0x10;
4871                                 break;
4872                         case AUTO_SEQ_CENLFE:
4873                                 spec->private_dac_nids[i] = 0x25;
4874                                 break;
4875                         case AUTO_SEQ_SURROUND:
4876                                 spec->private_dac_nids[i] = 0x11;
4877                                 break;
4878                         }
4879                 }
4880         }
4881
4882         return 0;
4883 }
4884
4885 /* add playback controls from the parsed DAC table */
4886 static int vt1716S_auto_create_multi_out_ctls(struct via_spec *spec,
4887                                               const struct auto_pin_cfg *cfg)
4888 {
4889         char name[32];
4890         static const char * const chname[3] = {
4891                 "Front", "Surround", "C/LFE"
4892         };
4893         hda_nid_t nid_vols[] = {0x10, 0x11, 0x25};
4894         hda_nid_t nid_mutes[] = {0x1C, 0x18, 0x27};
4895         hda_nid_t nid, nid_vol, nid_mute;
4896         int i, err;
4897
4898         for (i = 0; i <= AUTO_SEQ_CENLFE; i++) {
4899                 nid = cfg->line_out_pins[i];
4900
4901                 if (!nid)
4902                         continue;
4903
4904                 nid_vol = nid_vols[i];
4905                 nid_mute = nid_mutes[i];
4906
4907                 if (i == AUTO_SEQ_CENLFE) {
4908                         err = via_add_control(
4909                                 spec, VIA_CTL_WIDGET_VOL,
4910                                 "Center Playback Volume",
4911                                 HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0, HDA_OUTPUT));
4912                         if (err < 0)
4913                                 return err;
4914                         err = via_add_control(
4915                                 spec, VIA_CTL_WIDGET_VOL,
4916                                 "LFE Playback Volume",
4917                                 HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0, HDA_OUTPUT));
4918                         if (err < 0)
4919                                 return err;
4920                         err = via_add_control(
4921                                 spec, VIA_CTL_WIDGET_MUTE,
4922                                 "Center Playback Switch",
4923                                 HDA_COMPOSE_AMP_VAL(nid_mute, 1, 0,
4924                                                     HDA_OUTPUT));
4925                         if (err < 0)
4926                                 return err;
4927                         err = via_add_control(
4928                                 spec, VIA_CTL_WIDGET_MUTE,
4929                                 "LFE Playback Switch",
4930                                 HDA_COMPOSE_AMP_VAL(nid_mute, 2, 0,
4931                                                     HDA_OUTPUT));
4932                         if (err < 0)
4933                                 return err;
4934                 } else if (i == AUTO_SEQ_FRONT) {
4935
4936                         err = via_add_control(
4937                                 spec, VIA_CTL_WIDGET_VOL,
4938                                 "Master Front Playback Volume",
4939                                 HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_INPUT));
4940                         if (err < 0)
4941                                 return err;
4942                         err = via_add_control(
4943                                 spec, VIA_CTL_WIDGET_MUTE,
4944                                 "Master Front Playback Switch",
4945                                 HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_INPUT));
4946                         if (err < 0)
4947                                 return err;
4948
4949                         sprintf(name, "%s Playback Volume", chname[i]);
4950                         err = via_add_control(
4951                                 spec, VIA_CTL_WIDGET_VOL, name,
4952                                 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
4953                         if (err < 0)
4954                                 return err;
4955                         sprintf(name, "%s Playback Switch", chname[i]);
4956                         err = via_add_control(
4957                                 spec, VIA_CTL_WIDGET_MUTE, name,
4958                                 HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
4959                                                     HDA_OUTPUT));
4960                         if (err < 0)
4961                                 return err;
4962                 } else {
4963                         sprintf(name, "%s Playback Volume", chname[i]);
4964                         err = via_add_control(
4965                                 spec, VIA_CTL_WIDGET_VOL, name,
4966                                 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
4967                         if (err < 0)
4968                                 return err;
4969                         sprintf(name, "%s Playback Switch", chname[i]);
4970                         err = via_add_control(
4971                                 spec, VIA_CTL_WIDGET_MUTE, name,
4972                                 HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
4973                                                     HDA_OUTPUT));
4974                         if (err < 0)
4975                                 return err;
4976                 }
4977         }
4978         return 0;
4979 }
4980
4981 static int vt1716S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
4982 {
4983         int err;
4984
4985         if (!pin)
4986                 return 0;
4987
4988         spec->multiout.hp_nid = 0x25; /* AOW3 */
4989         spec->hp_independent_mode_index = 1;
4990
4991         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4992                               "Headphone Playback Volume",
4993                               HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
4994         if (err < 0)
4995                 return err;
4996
4997         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4998                               "Headphone Playback Switch",
4999                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
5000         if (err < 0)
5001                 return err;
5002
5003         create_hp_imux(spec);
5004         return 0;
5005 }
5006
5007 /* create playback/capture controls for input pins */
5008 static int vt1716S_auto_create_analog_input_ctls(struct hda_codec *codec,
5009                                                 const struct auto_pin_cfg *cfg)
5010 {
5011         return vt_auto_create_analog_input_ctls(codec, cfg, 0x16);
5012 }
5013
5014 static int vt1716S_parse_auto_config(struct hda_codec *codec)
5015 {
5016         struct via_spec *spec = codec->spec;
5017         int err;
5018
5019         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
5020         if (err < 0)
5021                 return err;
5022         err = vt1716S_auto_fill_dac_nids(spec, &spec->autocfg);
5023         if (err < 0)
5024                 return err;
5025         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
5026                 return 0; /* can't find valid BIOS pin config */
5027
5028         err = vt1716S_auto_create_multi_out_ctls(spec, &spec->autocfg);
5029         if (err < 0)
5030                 return err;
5031         err = vt1716S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
5032         if (err < 0)
5033                 return err;
5034         err = vt1716S_auto_create_analog_input_ctls(codec, &spec->autocfg);
5035         if (err < 0)
5036                 return err;
5037
5038         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
5039
5040         fill_dig_outs(codec);
5041
5042         if (spec->kctls.list)
5043                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
5044
5045         spec->input_mux = &spec->private_imux[0];
5046
5047         if (spec->hp_mux)
5048                 via_hp_build(codec);
5049
5050         via_smart51_build(spec);
5051
5052         return 1;
5053 }
5054
5055 #ifdef CONFIG_SND_HDA_POWER_SAVE
5056 static const struct hda_amp_list vt1716S_loopbacks[] = {
5057         { 0x16, HDA_INPUT, 1 },
5058         { 0x16, HDA_INPUT, 2 },
5059         { 0x16, HDA_INPUT, 3 },
5060         { 0x16, HDA_INPUT, 4 },
5061         { } /* end */
5062 };
5063 #endif
5064
5065 static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
5066 {
5067         struct via_spec *spec = codec->spec;
5068         int imux_is_smixer;
5069         unsigned int parm;
5070         unsigned int mono_out, present;
5071         /* SW0 (17h) = stereo mixer */
5072         imux_is_smixer =
5073         (snd_hda_codec_read(codec, 0x17, 0,
5074                             AC_VERB_GET_CONNECT_SEL, 0x00) ==  5);
5075         /* inputs */
5076         /* PW 1/2/5 (1ah/1bh/1eh) */
5077         parm = AC_PWRST_D3;
5078         set_pin_power_state(codec, 0x1a, &parm);
5079         set_pin_power_state(codec, 0x1b, &parm);
5080         set_pin_power_state(codec, 0x1e, &parm);
5081         if (imux_is_smixer)
5082                 parm = AC_PWRST_D0;
5083         /* SW0 (17h), AIW0(13h) */
5084         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
5085         snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
5086
5087         parm = AC_PWRST_D3;
5088         set_pin_power_state(codec, 0x1e, &parm);
5089         /* PW11 (22h) */
5090         if (spec->dmic_enabled)
5091                 set_pin_power_state(codec, 0x22, &parm);
5092         else
5093                 snd_hda_codec_write(codec, 0x22, 0,
5094                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
5095
5096         /* SW2(26h), AIW1(14h) */
5097         snd_hda_codec_write(codec, 0x26, 0, AC_VERB_SET_POWER_STATE, parm);
5098         snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
5099
5100         /* outputs */
5101         /* PW0 (19h), SW1 (18h), AOW1 (11h) */
5102         parm = AC_PWRST_D3;
5103         set_pin_power_state(codec, 0x19, &parm);
5104         /* Smart 5.1 PW2(1bh) */
5105         if (spec->smart51_enabled)
5106                 set_pin_power_state(codec, 0x1b, &parm);
5107         snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
5108         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
5109
5110         /* PW7 (23h), SW3 (27h), AOW3 (25h) */
5111         parm = AC_PWRST_D3;
5112         set_pin_power_state(codec, 0x23, &parm);
5113         /* Smart 5.1 PW1(1ah) */
5114         if (spec->smart51_enabled)
5115                 set_pin_power_state(codec, 0x1a, &parm);
5116         snd_hda_codec_write(codec, 0x27, 0, AC_VERB_SET_POWER_STATE, parm);
5117
5118         /* Smart 5.1 PW5(1eh) */
5119         if (spec->smart51_enabled)
5120                 set_pin_power_state(codec, 0x1e, &parm);
5121         snd_hda_codec_write(codec, 0x25, 0, AC_VERB_SET_POWER_STATE, parm);
5122
5123         /* Mono out */
5124         /* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
5125         present = snd_hda_jack_detect(codec, 0x1c);
5126
5127         if (present)
5128                 mono_out = 0;
5129         else {
5130                 present = snd_hda_jack_detect(codec, 0x1d);
5131                 if (!spec->hp_independent_mode && present)
5132                         mono_out = 0;
5133                 else
5134                         mono_out = 1;
5135         }
5136         parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
5137         snd_hda_codec_write(codec, 0x28, 0, AC_VERB_SET_POWER_STATE, parm);
5138         snd_hda_codec_write(codec, 0x29, 0, AC_VERB_SET_POWER_STATE, parm);
5139         snd_hda_codec_write(codec, 0x2a, 0, AC_VERB_SET_POWER_STATE, parm);
5140
5141         /* PW 3/4 (1ch/1dh) */
5142         parm = AC_PWRST_D3;
5143         set_pin_power_state(codec, 0x1c, &parm);
5144         set_pin_power_state(codec, 0x1d, &parm);
5145         /* HP Independent Mode, power on AOW3 */
5146         if (spec->hp_independent_mode)
5147                 snd_hda_codec_write(codec, 0x25, 0,
5148                                     AC_VERB_SET_POWER_STATE, parm);
5149
5150         /* force to D0 for internal Speaker */
5151         /* MW0 (16h), AOW0 (10h) */
5152         snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
5153                             imux_is_smixer ? AC_PWRST_D0 : parm);
5154         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
5155                             mono_out ? AC_PWRST_D0 : parm);
5156 }
5157
5158 static int patch_vt1716S(struct hda_codec *codec)
5159 {
5160         struct via_spec *spec;
5161         int err;
5162
5163         /* create a codec specific record */
5164         spec = via_new_spec(codec);
5165         if (spec == NULL)
5166                 return -ENOMEM;
5167
5168         /* automatic parse from the BIOS config */
5169         err = vt1716S_parse_auto_config(codec);
5170         if (err < 0) {
5171                 via_free(codec);
5172                 return err;
5173         } else if (!err) {
5174                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
5175                        "from BIOS.  Using genenic mode...\n");
5176         }
5177
5178         spec->init_verbs[spec->num_iverbs++]  = vt1716S_volume_init_verbs;
5179         spec->init_verbs[spec->num_iverbs++] = vt1716S_uniwill_init_verbs;
5180
5181         spec->stream_analog_playback = &vt1716S_pcm_analog_playback;
5182         spec->stream_analog_capture = &vt1716S_pcm_analog_capture;
5183
5184         spec->stream_digital_playback = &vt1716S_pcm_digital_playback;
5185
5186         if (spec->adc_nids && spec->input_mux) {
5187                 override_mic_boost(codec, 0x1a, 0, 3, 40);
5188                 override_mic_boost(codec, 0x1e, 0, 3, 40);
5189                 spec->mixers[spec->num_mixers] = vt1716S_capture_mixer;
5190                 spec->num_mixers++;
5191         }
5192
5193         spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
5194         spec->num_mixers++;
5195
5196         spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
5197
5198         codec->patch_ops = via_patch_ops;
5199
5200         codec->patch_ops.init = via_auto_init;
5201         codec->patch_ops.unsol_event = via_unsol_event;
5202
5203 #ifdef CONFIG_SND_HDA_POWER_SAVE
5204         spec->loopback.amplist = vt1716S_loopbacks;
5205 #endif
5206
5207         spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
5208         return 0;
5209 }
5210
5211 /* for vt2002P */
5212
5213 /* capture mixer elements */
5214 static const struct snd_kcontrol_new vt2002P_capture_mixer[] = {
5215         HDA_CODEC_VOLUME("Capture Volume", 0x10, 0x0, HDA_INPUT),
5216         HDA_CODEC_MUTE("Capture Switch", 0x10, 0x0, HDA_INPUT),
5217         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x11, 0x0, HDA_INPUT),
5218         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x11, 0x0, HDA_INPUT),
5219         HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x2b, 0x0, HDA_INPUT),
5220         HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x29, 0x0,
5221                          HDA_INPUT),
5222         {
5223                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5224                 /* The multiple "Capture Source" controls confuse alsamixer
5225                  * So call somewhat different..
5226                  */
5227                 /* .name = "Capture Source", */
5228                 .name = "Input Source",
5229                 .count = 2,
5230                 .info = via_mux_enum_info,
5231                 .get = via_mux_enum_get,
5232                 .put = via_mux_enum_put,
5233         },
5234         { } /* end */
5235 };
5236
5237 static const struct hda_verb vt2002P_volume_init_verbs[] = {
5238         /* Class-D speaker related verbs */
5239         {0x1, 0xfe0, 0x4},
5240         {0x1, 0xfe9, 0x80},
5241         {0x1, 0xfe2, 0x22},
5242         /*
5243          * Unmute ADC0-1 and set the default input to mic-in
5244          */
5245         {0x8, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5246         {0x9, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5247
5248
5249         /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
5250          * mixer widget
5251          */
5252         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
5253         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
5254         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5255         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
5256         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
5257         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
5258
5259         /* MUX Indices: Mic = 0 */
5260         {0x1e, AC_VERB_SET_CONNECT_SEL, 0},
5261         {0x1f, AC_VERB_SET_CONNECT_SEL, 0},
5262
5263         /* PW9 Output enable */
5264         {0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
5265
5266         /* Enable Boost Volume backdoor */
5267         {0x1, 0xfb9, 0x24},
5268
5269         /* MW0/1/4/8: un-mute index 0 (MUXx), un-mute index 1 (MW9) */
5270         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5271         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5272         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5273         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5274         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5275         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5276         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5277         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5278
5279         /* set MUX0/1/4/8 = 0 (AOW0) */
5280         {0x34, AC_VERB_SET_CONNECT_SEL, 0},
5281         {0x35, AC_VERB_SET_CONNECT_SEL, 0},
5282         {0x37, AC_VERB_SET_CONNECT_SEL, 0},
5283         {0x3b, AC_VERB_SET_CONNECT_SEL, 0},
5284
5285         /* set PW0 index=0 (MW0) */
5286         {0x24, AC_VERB_SET_CONNECT_SEL, 0},
5287
5288         /* Enable AOW0 to MW9 */
5289         {0x1, 0xfb8, 0x88},
5290         { }
5291 };
5292 static const struct hda_verb vt1802_volume_init_verbs[] = {
5293         /*
5294          * Unmute ADC0-1 and set the default input to mic-in
5295          */
5296         {0x8, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5297         {0x9, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5298
5299
5300         /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
5301          * mixer widget
5302          */
5303         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
5304         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
5305         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5306         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
5307         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
5308         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
5309
5310         /* MUX Indices: Mic = 0 */
5311         {0x1e, AC_VERB_SET_CONNECT_SEL, 0},
5312         {0x1f, AC_VERB_SET_CONNECT_SEL, 0},
5313
5314         /* PW9 Output enable */
5315         {0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
5316
5317         /* Enable Boost Volume backdoor */
5318         {0x1, 0xfb9, 0x24},
5319
5320         /* MW0/1/4/8: un-mute index 0 (MUXx), un-mute index 1 (MW9) */
5321         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5322         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5323         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5324         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5325         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5326         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5327         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5328         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5329
5330         /* set MUX0/1/4/8 = 0 (AOW0) */
5331         {0x34, AC_VERB_SET_CONNECT_SEL, 0},
5332         {0x35, AC_VERB_SET_CONNECT_SEL, 0},
5333         {0x38, AC_VERB_SET_CONNECT_SEL, 0},
5334         {0x3c, AC_VERB_SET_CONNECT_SEL, 0},
5335
5336         /* set PW0 index=0 (MW0) */
5337         {0x24, AC_VERB_SET_CONNECT_SEL, 0},
5338
5339         /* Enable AOW0 to MW9 */
5340         {0x1, 0xfb8, 0x88},
5341         { }
5342 };
5343
5344
5345 static const struct hda_verb vt2002P_uniwill_init_verbs[] = {
5346         {0x25, AC_VERB_SET_UNSOLICITED_ENABLE,
5347          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5348         {0x26, AC_VERB_SET_UNSOLICITED_ENABLE,
5349          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5350         {0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5351         {0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5352         {0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5353         { }
5354 };
5355 static const struct hda_verb vt1802_uniwill_init_verbs[] = {
5356         {0x25, AC_VERB_SET_UNSOLICITED_ENABLE,
5357          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5358         {0x28, AC_VERB_SET_UNSOLICITED_ENABLE,
5359          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5360         {0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5361         {0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5362         {0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5363         { }
5364 };
5365
5366 static const struct hda_pcm_stream vt2002P_pcm_analog_playback = {
5367         .substreams = 2,
5368         .channels_min = 2,
5369         .channels_max = 2,
5370         .nid = 0x8, /* NID to query formats and rates */
5371         .ops = {
5372                 .open = via_playback_pcm_open,
5373                 .prepare = via_playback_multi_pcm_prepare,
5374                 .cleanup = via_playback_multi_pcm_cleanup,
5375                 .close = via_pcm_open_close,
5376         },
5377 };
5378
5379 static const struct hda_pcm_stream vt2002P_pcm_analog_capture = {
5380         .substreams = 2,
5381         .channels_min = 2,
5382         .channels_max = 2,
5383         .nid = 0x10, /* NID to query formats and rates */
5384         .ops = {
5385                 .open = via_pcm_open_close,
5386                 .prepare = via_capture_pcm_prepare,
5387                 .cleanup = via_capture_pcm_cleanup,
5388                 .close = via_pcm_open_close,
5389         },
5390 };
5391
5392 static const struct hda_pcm_stream vt2002P_pcm_digital_playback = {
5393         .substreams = 1,
5394         .channels_min = 2,
5395         .channels_max = 2,
5396         /* NID is set in via_build_pcms */
5397         .ops = {
5398                 .open = via_dig_playback_pcm_open,
5399                 .close = via_dig_playback_pcm_close,
5400                 .prepare = via_dig_playback_pcm_prepare,
5401                 .cleanup = via_dig_playback_pcm_cleanup
5402         },
5403 };
5404
5405 /* fill in the dac_nids table from the parsed pin configuration */
5406 static int vt2002P_auto_fill_dac_nids(struct via_spec *spec,
5407                                       const struct auto_pin_cfg *cfg)
5408 {
5409         spec->multiout.num_dacs = 1;
5410         spec->multiout.dac_nids = spec->private_dac_nids;
5411         if (cfg->line_out_pins[0])
5412                 spec->private_dac_nids[0] = 0x8;
5413         return 0;
5414 }
5415
5416 /* add playback controls from the parsed DAC table */
5417 static int vt2002P_auto_create_multi_out_ctls(struct via_spec *spec,
5418                                              const struct auto_pin_cfg *cfg)
5419 {
5420         int err;
5421         hda_nid_t sw_nid;
5422
5423         if (!cfg->line_out_pins[0])
5424                 return -1;
5425
5426         if (spec->codec_type == VT1802)
5427                 sw_nid = 0x28;
5428         else
5429                 sw_nid = 0x26;
5430
5431         /* Line-Out: PortE */
5432         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5433                               "Master Front Playback Volume",
5434                               HDA_COMPOSE_AMP_VAL(0x8, 3, 0, HDA_OUTPUT));
5435         if (err < 0)
5436                 return err;
5437         err = via_add_control(spec, VIA_CTL_WIDGET_BIND_PIN_MUTE,
5438                               "Master Front Playback Switch",
5439                               HDA_COMPOSE_AMP_VAL(sw_nid, 3, 0, HDA_OUTPUT));
5440         if (err < 0)
5441                 return err;
5442
5443         return 0;
5444 }
5445
5446 static int vt2002P_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
5447 {
5448         int err;
5449
5450         if (!pin)
5451                 return 0;
5452
5453         spec->multiout.hp_nid = 0x9;
5454         spec->hp_independent_mode_index = 1;
5455
5456         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5457                               "Headphone Playback Volume",
5458                               HDA_COMPOSE_AMP_VAL(
5459                                       spec->multiout.hp_nid, 3, 0, HDA_OUTPUT));
5460         if (err < 0)
5461                 return err;
5462
5463         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
5464                               "Headphone Playback Switch",
5465                               HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
5466         if (err < 0)
5467                 return err;
5468
5469         create_hp_imux(spec);
5470         return 0;
5471 }
5472
5473 /* create playback/capture controls for input pins */
5474 static int vt2002P_auto_create_analog_input_ctls(struct hda_codec *codec,
5475                                                 const struct auto_pin_cfg *cfg)
5476 {
5477         return vt_auto_create_analog_input_ctls(codec, cfg, 0x21);
5478 }
5479
5480 static int vt2002P_parse_auto_config(struct hda_codec *codec)
5481 {
5482         struct via_spec *spec = codec->spec;
5483         int err;
5484
5485
5486         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
5487         if (err < 0)
5488                 return err;
5489
5490         err = vt2002P_auto_fill_dac_nids(spec, &spec->autocfg);
5491         if (err < 0)
5492                 return err;
5493
5494         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
5495                 return 0; /* can't find valid BIOS pin config */
5496
5497         err = vt2002P_auto_create_multi_out_ctls(spec, &spec->autocfg);
5498         if (err < 0)
5499                 return err;
5500         err = vt2002P_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
5501         if (err < 0)
5502                 return err;
5503         err = vt2002P_auto_create_analog_input_ctls(codec, &spec->autocfg);
5504         if (err < 0)
5505                 return err;
5506
5507         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
5508
5509         fill_dig_outs(codec);
5510
5511         if (spec->kctls.list)
5512                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
5513
5514         spec->input_mux = &spec->private_imux[0];
5515
5516         if (spec->hp_mux)
5517                 via_hp_build(codec);
5518
5519         return 1;
5520 }
5521
5522 #ifdef CONFIG_SND_HDA_POWER_SAVE
5523 static const struct hda_amp_list vt2002P_loopbacks[] = {
5524         { 0x21, HDA_INPUT, 0 },
5525         { 0x21, HDA_INPUT, 1 },
5526         { 0x21, HDA_INPUT, 2 },
5527         { } /* end */
5528 };
5529 #endif
5530
5531 static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
5532 {
5533         struct via_spec *spec = codec->spec;
5534         int imux_is_smixer;
5535         unsigned int parm;
5536         unsigned int present;
5537         /* MUX9 (1eh) = stereo mixer */
5538         imux_is_smixer =
5539         snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
5540         /* inputs */
5541         /* PW 5/6/7 (29h/2ah/2bh) */
5542         parm = AC_PWRST_D3;
5543         set_pin_power_state(codec, 0x29, &parm);
5544         set_pin_power_state(codec, 0x2a, &parm);
5545         set_pin_power_state(codec, 0x2b, &parm);
5546         parm = AC_PWRST_D0;
5547         /* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
5548         snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
5549         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
5550         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
5551         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
5552
5553         /* outputs */
5554         /* AOW0 (8h)*/
5555         snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
5556
5557         if (spec->codec_type == VT1802) {
5558                 /* PW4 (28h), MW4 (18h), MUX4(38h) */
5559                 parm = AC_PWRST_D3;
5560                 set_pin_power_state(codec, 0x28, &parm);
5561                 snd_hda_codec_write(codec, 0x18, 0,
5562                                     AC_VERB_SET_POWER_STATE, parm);
5563                 snd_hda_codec_write(codec, 0x38, 0,
5564                                     AC_VERB_SET_POWER_STATE, parm);
5565         } else {
5566                 /* PW4 (26h), MW4 (1ch), MUX4(37h) */
5567                 parm = AC_PWRST_D3;
5568                 set_pin_power_state(codec, 0x26, &parm);
5569                 snd_hda_codec_write(codec, 0x1c, 0,
5570                                     AC_VERB_SET_POWER_STATE, parm);
5571                 snd_hda_codec_write(codec, 0x37, 0,
5572                                     AC_VERB_SET_POWER_STATE, parm);
5573         }
5574
5575         if (spec->codec_type == VT1802) {
5576                 /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
5577                 parm = AC_PWRST_D3;
5578                 set_pin_power_state(codec, 0x25, &parm);
5579                 snd_hda_codec_write(codec, 0x15, 0,
5580                                     AC_VERB_SET_POWER_STATE, parm);
5581                 snd_hda_codec_write(codec, 0x35, 0,
5582                                     AC_VERB_SET_POWER_STATE, parm);
5583         } else {
5584                 /* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
5585                 parm = AC_PWRST_D3;
5586                 set_pin_power_state(codec, 0x25, &parm);
5587                 snd_hda_codec_write(codec, 0x19, 0,
5588                                     AC_VERB_SET_POWER_STATE, parm);
5589                 snd_hda_codec_write(codec, 0x35, 0,
5590                                     AC_VERB_SET_POWER_STATE, parm);
5591         }
5592
5593         if (spec->hp_independent_mode)
5594                 snd_hda_codec_write(codec, 0x9, 0,
5595                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
5596
5597         /* Class-D */
5598         /* PW0 (24h), MW0(18h/14h), MUX0(34h) */
5599         present = snd_hda_jack_detect(codec, 0x25);
5600
5601         parm = AC_PWRST_D3;
5602         set_pin_power_state(codec, 0x24, &parm);
5603         parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
5604         if (spec->codec_type == VT1802)
5605                 snd_hda_codec_write(codec, 0x14, 0,
5606                                     AC_VERB_SET_POWER_STATE, parm);
5607         else
5608                 snd_hda_codec_write(codec, 0x18, 0,
5609                                     AC_VERB_SET_POWER_STATE, parm);
5610         snd_hda_codec_write(codec, 0x34, 0, AC_VERB_SET_POWER_STATE, parm);
5611
5612         /* Mono Out */
5613         present = snd_hda_jack_detect(codec, 0x26);
5614
5615         parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
5616         if (spec->codec_type == VT1802) {
5617                 /* PW15 (33h), MW8(1ch), MUX8(3ch) */
5618                 snd_hda_codec_write(codec, 0x33, 0,
5619                                     AC_VERB_SET_POWER_STATE, parm);
5620                 snd_hda_codec_write(codec, 0x1c, 0,
5621                                     AC_VERB_SET_POWER_STATE, parm);
5622                 snd_hda_codec_write(codec, 0x3c, 0,
5623                                     AC_VERB_SET_POWER_STATE, parm);
5624         } else {
5625                 /* PW15 (31h), MW8(17h), MUX8(3bh) */
5626                 snd_hda_codec_write(codec, 0x31, 0,
5627                                     AC_VERB_SET_POWER_STATE, parm);
5628                 snd_hda_codec_write(codec, 0x17, 0,
5629                                     AC_VERB_SET_POWER_STATE, parm);
5630                 snd_hda_codec_write(codec, 0x3b, 0,
5631                                     AC_VERB_SET_POWER_STATE, parm);
5632         }
5633         /* MW9 (21h) */
5634         if (imux_is_smixer || !is_aa_path_mute(codec))
5635                 snd_hda_codec_write(codec, 0x21, 0,
5636                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
5637         else
5638                 snd_hda_codec_write(codec, 0x21, 0,
5639                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
5640 }
5641
5642 /* patch for vt2002P */
5643 static int patch_vt2002P(struct hda_codec *codec)
5644 {
5645         struct via_spec *spec;
5646         int err;
5647
5648         /* create a codec specific record */
5649         spec = via_new_spec(codec);
5650         if (spec == NULL)
5651                 return -ENOMEM;
5652
5653         /* automatic parse from the BIOS config */
5654         err = vt2002P_parse_auto_config(codec);
5655         if (err < 0) {
5656                 via_free(codec);
5657                 return err;
5658         } else if (!err) {
5659                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
5660                        "from BIOS.  Using genenic mode...\n");
5661         }
5662
5663         if (spec->codec_type == VT1802)
5664                 spec->init_verbs[spec->num_iverbs++]  =
5665                         vt1802_volume_init_verbs;
5666         else
5667                 spec->init_verbs[spec->num_iverbs++]  =
5668                         vt2002P_volume_init_verbs;
5669
5670         if (spec->codec_type == VT1802)
5671                 spec->init_verbs[spec->num_iverbs++] =
5672                         vt1802_uniwill_init_verbs;
5673         else
5674                 spec->init_verbs[spec->num_iverbs++] =
5675                         vt2002P_uniwill_init_verbs;
5676
5677         spec->stream_analog_playback = &vt2002P_pcm_analog_playback;
5678         spec->stream_analog_capture = &vt2002P_pcm_analog_capture;
5679
5680         spec->stream_digital_playback = &vt2002P_pcm_digital_playback;
5681
5682         if (spec->adc_nids && spec->input_mux) {
5683                 override_mic_boost(codec, 0x2b, 0, 3, 40);
5684                 override_mic_boost(codec, 0x29, 0, 3, 40);
5685                 spec->mixers[spec->num_mixers] = vt2002P_capture_mixer;
5686                 spec->num_mixers++;
5687         }
5688
5689         codec->patch_ops = via_patch_ops;
5690
5691         codec->patch_ops.init = via_auto_init;
5692         codec->patch_ops.unsol_event = via_unsol_event;
5693
5694 #ifdef CONFIG_SND_HDA_POWER_SAVE
5695         spec->loopback.amplist = vt2002P_loopbacks;
5696 #endif
5697
5698         spec->set_widgets_power_state =  set_widgets_power_state_vt2002P;
5699         return 0;
5700 }
5701
5702 /* for vt1812 */
5703
5704 /* capture mixer elements */
5705 static const struct snd_kcontrol_new vt1812_capture_mixer[] = {
5706         HDA_CODEC_VOLUME("Capture Volume", 0x10, 0x0, HDA_INPUT),
5707         HDA_CODEC_MUTE("Capture Switch", 0x10, 0x0, HDA_INPUT),
5708         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x11, 0x0, HDA_INPUT),
5709         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x11, 0x0, HDA_INPUT),
5710         HDA_CODEC_MUTE("Mic Boost Capture Volume", 0x2b, 0x0, HDA_INPUT),
5711         HDA_CODEC_MUTE("Front Mic Boost Capture Volume", 0x29, 0x0,
5712                        HDA_INPUT),
5713         {
5714                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5715                 /* The multiple "Capture Source" controls confuse alsamixer
5716                  * So call somewhat different..
5717                  */
5718                 .name = "Input Source",
5719                 .count = 2,
5720                 .info = via_mux_enum_info,
5721                 .get = via_mux_enum_get,
5722                 .put = via_mux_enum_put,
5723         },
5724         { } /* end */
5725 };
5726
5727 static const struct hda_verb vt1812_volume_init_verbs[] = {
5728         /*
5729          * Unmute ADC0-1 and set the default input to mic-in
5730          */
5731         {0x8, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5732         {0x9, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5733
5734
5735         /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
5736          * mixer widget
5737          */
5738         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
5739         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
5740         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5741         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
5742         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
5743         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
5744
5745         /* MUX Indices: Mic = 0 */
5746         {0x1e, AC_VERB_SET_CONNECT_SEL, 0},
5747         {0x1f, AC_VERB_SET_CONNECT_SEL, 0},
5748
5749         /* PW9 Output enable */
5750         {0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
5751
5752         /* Enable Boost Volume backdoor */
5753         {0x1, 0xfb9, 0x24},
5754
5755         /* MW0/1/4/13/15: un-mute index 0 (MUXx), un-mute index 1 (MW9) */
5756         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5757         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5758         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5759         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5760         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5761         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5762         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5763         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5764         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5765         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5766
5767         /* set MUX0/1/4/13/15 = 0 (AOW0) */
5768         {0x34, AC_VERB_SET_CONNECT_SEL, 0},
5769         {0x35, AC_VERB_SET_CONNECT_SEL, 0},
5770         {0x38, AC_VERB_SET_CONNECT_SEL, 0},
5771         {0x3c, AC_VERB_SET_CONNECT_SEL, 0},
5772         {0x3d, AC_VERB_SET_CONNECT_SEL, 0},
5773
5774         /* Enable AOW0 to MW9 */
5775         {0x1, 0xfb8, 0xa8},
5776         { }
5777 };
5778
5779
5780 static const struct hda_verb vt1812_uniwill_init_verbs[] = {
5781         {0x33, AC_VERB_SET_UNSOLICITED_ENABLE,
5782          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5783         {0x25, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT },
5784         {0x28, AC_VERB_SET_UNSOLICITED_ENABLE,
5785          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5786         {0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5787         {0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5788         {0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5789         { }
5790 };
5791
5792 static const struct hda_pcm_stream vt1812_pcm_analog_playback = {
5793         .substreams = 2,
5794         .channels_min = 2,
5795         .channels_max = 2,
5796         .nid = 0x8, /* NID to query formats and rates */
5797         .ops = {
5798                 .open = via_playback_pcm_open,
5799                 .prepare = via_playback_multi_pcm_prepare,
5800                 .cleanup = via_playback_multi_pcm_cleanup,
5801                 .close = via_pcm_open_close,
5802         },
5803 };
5804
5805 static const struct hda_pcm_stream vt1812_pcm_analog_capture = {
5806         .substreams = 2,
5807         .channels_min = 2,
5808         .channels_max = 2,
5809         .nid = 0x10, /* NID to query formats and rates */
5810         .ops = {
5811                 .open = via_pcm_open_close,
5812                 .prepare = via_capture_pcm_prepare,
5813                 .cleanup = via_capture_pcm_cleanup,
5814                 .close = via_pcm_open_close,
5815         },
5816 };
5817
5818 static const struct hda_pcm_stream vt1812_pcm_digital_playback = {
5819         .substreams = 1,
5820         .channels_min = 2,
5821         .channels_max = 2,
5822         /* NID is set in via_build_pcms */
5823         .ops = {
5824                 .open = via_dig_playback_pcm_open,
5825                 .close = via_dig_playback_pcm_close,
5826                 .prepare = via_dig_playback_pcm_prepare,
5827                 .cleanup = via_dig_playback_pcm_cleanup
5828         },
5829 };
5830 /* fill in the dac_nids table from the parsed pin configuration */
5831 static int vt1812_auto_fill_dac_nids(struct via_spec *spec,
5832                                      const struct auto_pin_cfg *cfg)
5833 {
5834         spec->multiout.num_dacs = 1;
5835         spec->multiout.dac_nids = spec->private_dac_nids;
5836         if (cfg->line_out_pins[0])
5837                 spec->private_dac_nids[0] = 0x8;
5838         return 0;
5839 }
5840
5841
5842 /* add playback controls from the parsed DAC table */
5843 static int vt1812_auto_create_multi_out_ctls(struct via_spec *spec,
5844                                              const struct auto_pin_cfg *cfg)
5845 {
5846         int err;
5847
5848         if (!cfg->line_out_pins[0])
5849                 return -1;
5850
5851         /* Line-Out: PortE */
5852         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5853                               "Front Playback Volume",
5854                               HDA_COMPOSE_AMP_VAL(0x8, 3, 0, HDA_OUTPUT));
5855         if (err < 0)
5856                 return err;
5857         err = via_add_control(spec, VIA_CTL_WIDGET_BIND_PIN_MUTE,
5858                               "Front Playback Switch",
5859                               HDA_COMPOSE_AMP_VAL(0x28, 3, 0, HDA_OUTPUT));
5860         if (err < 0)
5861                 return err;
5862
5863         return 0;
5864 }
5865
5866 static int vt1812_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
5867 {
5868         int err;
5869
5870         if (!pin)
5871                 return 0;
5872
5873         spec->multiout.hp_nid = 0x9;
5874         spec->hp_independent_mode_index = 1;
5875
5876
5877         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5878                               "Headphone Playback Volume",
5879                               HDA_COMPOSE_AMP_VAL(
5880                                       spec->multiout.hp_nid, 3, 0, HDA_OUTPUT));
5881         if (err < 0)
5882                 return err;
5883
5884         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
5885                               "Headphone Playback Switch",
5886                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
5887         if (err < 0)
5888                 return err;
5889
5890         create_hp_imux(spec);
5891         return 0;
5892 }
5893
5894 /* create playback/capture controls for input pins */
5895 static int vt1812_auto_create_analog_input_ctls(struct hda_codec *codec,
5896                                                 const struct auto_pin_cfg *cfg)
5897 {
5898         return vt_auto_create_analog_input_ctls(codec, cfg, 0x21);
5899 }
5900
5901 static int vt1812_parse_auto_config(struct hda_codec *codec)
5902 {
5903         struct via_spec *spec = codec->spec;
5904         int err;
5905
5906
5907         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
5908         if (err < 0)
5909                 return err;
5910         fill_dig_outs(codec);
5911         err = vt1812_auto_fill_dac_nids(spec, &spec->autocfg);
5912         if (err < 0)
5913                 return err;
5914
5915         if (!spec->autocfg.line_outs && !spec->autocfg.hp_outs)
5916                 return 0; /* can't find valid BIOS pin config */
5917
5918         err = vt1812_auto_create_multi_out_ctls(spec, &spec->autocfg);
5919         if (err < 0)
5920                 return err;
5921         err = vt1812_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
5922         if (err < 0)
5923                 return err;
5924         err = vt1812_auto_create_analog_input_ctls(codec, &spec->autocfg);
5925         if (err < 0)
5926                 return err;
5927
5928         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
5929
5930         fill_dig_outs(codec);
5931
5932         if (spec->kctls.list)
5933                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
5934
5935         spec->input_mux = &spec->private_imux[0];
5936
5937         if (spec->hp_mux)
5938                 via_hp_build(codec);
5939
5940         return 1;
5941 }
5942
5943 #ifdef CONFIG_SND_HDA_POWER_SAVE
5944 static const struct hda_amp_list vt1812_loopbacks[] = {
5945         { 0x21, HDA_INPUT, 0 },
5946         { 0x21, HDA_INPUT, 1 },
5947         { 0x21, HDA_INPUT, 2 },
5948         { } /* end */
5949 };
5950 #endif
5951
5952 static void set_widgets_power_state_vt1812(struct hda_codec *codec)
5953 {
5954         struct via_spec *spec = codec->spec;
5955         int imux_is_smixer =
5956         snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
5957         unsigned int parm;
5958         unsigned int present;
5959         /* MUX10 (1eh) = stereo mixer */
5960         imux_is_smixer =
5961         snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
5962         /* inputs */
5963         /* PW 5/6/7 (29h/2ah/2bh) */
5964         parm = AC_PWRST_D3;
5965         set_pin_power_state(codec, 0x29, &parm);
5966         set_pin_power_state(codec, 0x2a, &parm);
5967         set_pin_power_state(codec, 0x2b, &parm);
5968         parm = AC_PWRST_D0;
5969         /* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
5970         snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
5971         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
5972         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
5973         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
5974
5975         /* outputs */
5976         /* AOW0 (8h)*/
5977         snd_hda_codec_write(codec, 0x8, 0,
5978                             AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
5979
5980         /* PW4 (28h), MW4 (18h), MUX4(38h) */
5981         parm = AC_PWRST_D3;
5982         set_pin_power_state(codec, 0x28, &parm);
5983         snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
5984         snd_hda_codec_write(codec, 0x38, 0, AC_VERB_SET_POWER_STATE, parm);
5985
5986         /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
5987         parm = AC_PWRST_D3;
5988         set_pin_power_state(codec, 0x25, &parm);
5989         snd_hda_codec_write(codec, 0x15, 0, AC_VERB_SET_POWER_STATE, parm);
5990         snd_hda_codec_write(codec, 0x35, 0, AC_VERB_SET_POWER_STATE, parm);
5991         if (spec->hp_independent_mode)
5992                 snd_hda_codec_write(codec, 0x9, 0,
5993                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
5994
5995         /* Internal Speaker */
5996         /* PW0 (24h), MW0(14h), MUX0(34h) */
5997         present = snd_hda_jack_detect(codec, 0x25);
5998
5999         parm = AC_PWRST_D3;
6000         set_pin_power_state(codec, 0x24, &parm);
6001         if (present) {
6002                 snd_hda_codec_write(codec, 0x14, 0,
6003                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6004                 snd_hda_codec_write(codec, 0x34, 0,
6005                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6006         } else {
6007                 snd_hda_codec_write(codec, 0x14, 0,
6008                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6009                 snd_hda_codec_write(codec, 0x34, 0,
6010                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6011         }
6012
6013
6014         /* Mono Out */
6015         /* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
6016         present = snd_hda_jack_detect(codec, 0x28);
6017
6018         parm = AC_PWRST_D3;
6019         set_pin_power_state(codec, 0x31, &parm);
6020         if (present) {
6021                 snd_hda_codec_write(codec, 0x1c, 0,
6022                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6023                 snd_hda_codec_write(codec, 0x3c, 0,
6024                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6025                 snd_hda_codec_write(codec, 0x3e, 0,
6026                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6027         } else {
6028                 snd_hda_codec_write(codec, 0x1c, 0,
6029                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6030                 snd_hda_codec_write(codec, 0x3c, 0,
6031                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6032                 snd_hda_codec_write(codec, 0x3e, 0,
6033                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6034         }
6035
6036         /* PW15 (33h), MW15 (1dh), MUX15(3dh) */
6037         parm = AC_PWRST_D3;
6038         set_pin_power_state(codec, 0x33, &parm);
6039         snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
6040         snd_hda_codec_write(codec, 0x3d, 0, AC_VERB_SET_POWER_STATE, parm);
6041
6042 }
6043
6044 /* patch for vt1812 */
6045 static int patch_vt1812(struct hda_codec *codec)
6046 {
6047         struct via_spec *spec;
6048         int err;
6049
6050         /* create a codec specific record */
6051         spec = via_new_spec(codec);
6052         if (spec == NULL)
6053                 return -ENOMEM;
6054
6055         /* automatic parse from the BIOS config */
6056         err = vt1812_parse_auto_config(codec);
6057         if (err < 0) {
6058                 via_free(codec);
6059                 return err;
6060         } else if (!err) {
6061                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
6062                        "from BIOS.  Using genenic mode...\n");
6063         }
6064
6065
6066         spec->init_verbs[spec->num_iverbs++]  = vt1812_volume_init_verbs;
6067         spec->init_verbs[spec->num_iverbs++] = vt1812_uniwill_init_verbs;
6068
6069         spec->stream_analog_playback = &vt1812_pcm_analog_playback;
6070         spec->stream_analog_capture = &vt1812_pcm_analog_capture;
6071
6072         spec->stream_digital_playback = &vt1812_pcm_digital_playback;
6073
6074         if (spec->adc_nids && spec->input_mux) {
6075                 override_mic_boost(codec, 0x2b, 0, 3, 40);
6076                 override_mic_boost(codec, 0x29, 0, 3, 40);
6077                 spec->mixers[spec->num_mixers] = vt1812_capture_mixer;
6078                 spec->num_mixers++;
6079         }
6080
6081         codec->patch_ops = via_patch_ops;
6082
6083         codec->patch_ops.init = via_auto_init;
6084         codec->patch_ops.unsol_event = via_unsol_event;
6085
6086 #ifdef CONFIG_SND_HDA_POWER_SAVE
6087         spec->loopback.amplist = vt1812_loopbacks;
6088 #endif
6089
6090         spec->set_widgets_power_state =  set_widgets_power_state_vt1812;
6091         return 0;
6092 }
6093
6094 /*
6095  * patch entries
6096  */
6097 static const struct hda_codec_preset snd_hda_preset_via[] = {
6098         { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
6099         { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
6100         { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
6101         { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
6102         { .id = 0x1106e710, .name = "VT1709 10-Ch",
6103           .patch = patch_vt1709_10ch},
6104         { .id = 0x1106e711, .name = "VT1709 10-Ch",
6105           .patch = patch_vt1709_10ch},
6106         { .id = 0x1106e712, .name = "VT1709 10-Ch",
6107           .patch = patch_vt1709_10ch},
6108         { .id = 0x1106e713, .name = "VT1709 10-Ch",
6109           .patch = patch_vt1709_10ch},
6110         { .id = 0x1106e714, .name = "VT1709 6-Ch",
6111           .patch = patch_vt1709_6ch},
6112         { .id = 0x1106e715, .name = "VT1709 6-Ch",
6113           .patch = patch_vt1709_6ch},
6114         { .id = 0x1106e716, .name = "VT1709 6-Ch",
6115           .patch = patch_vt1709_6ch},
6116         { .id = 0x1106e717, .name = "VT1709 6-Ch",
6117           .patch = patch_vt1709_6ch},
6118         { .id = 0x1106e720, .name = "VT1708B 8-Ch",
6119           .patch = patch_vt1708B_8ch},
6120         { .id = 0x1106e721, .name = "VT1708B 8-Ch",
6121           .patch = patch_vt1708B_8ch},
6122         { .id = 0x1106e722, .name = "VT1708B 8-Ch",
6123           .patch = patch_vt1708B_8ch},
6124         { .id = 0x1106e723, .name = "VT1708B 8-Ch",
6125           .patch = patch_vt1708B_8ch},
6126         { .id = 0x1106e724, .name = "VT1708B 4-Ch",
6127           .patch = patch_vt1708B_4ch},
6128         { .id = 0x1106e725, .name = "VT1708B 4-Ch",
6129           .patch = patch_vt1708B_4ch},
6130         { .id = 0x1106e726, .name = "VT1708B 4-Ch",
6131           .patch = patch_vt1708B_4ch},
6132         { .id = 0x1106e727, .name = "VT1708B 4-Ch",
6133           .patch = patch_vt1708B_4ch},
6134         { .id = 0x11060397, .name = "VT1708S",
6135           .patch = patch_vt1708S},
6136         { .id = 0x11061397, .name = "VT1708S",
6137           .patch = patch_vt1708S},
6138         { .id = 0x11062397, .name = "VT1708S",
6139           .patch = patch_vt1708S},
6140         { .id = 0x11063397, .name = "VT1708S",
6141           .patch = patch_vt1708S},
6142         { .id = 0x11064397, .name = "VT1705",
6143           .patch = patch_vt1708S},
6144         { .id = 0x11065397, .name = "VT1708S",
6145           .patch = patch_vt1708S},
6146         { .id = 0x11066397, .name = "VT1708S",
6147           .patch = patch_vt1708S},
6148         { .id = 0x11067397, .name = "VT1708S",
6149           .patch = patch_vt1708S},
6150         { .id = 0x11060398, .name = "VT1702",
6151           .patch = patch_vt1702},
6152         { .id = 0x11061398, .name = "VT1702",
6153           .patch = patch_vt1702},
6154         { .id = 0x11062398, .name = "VT1702",
6155           .patch = patch_vt1702},
6156         { .id = 0x11063398, .name = "VT1702",
6157           .patch = patch_vt1702},
6158         { .id = 0x11064398, .name = "VT1702",
6159           .patch = patch_vt1702},
6160         { .id = 0x11065398, .name = "VT1702",
6161           .patch = patch_vt1702},
6162         { .id = 0x11066398, .name = "VT1702",
6163           .patch = patch_vt1702},
6164         { .id = 0x11067398, .name = "VT1702",
6165           .patch = patch_vt1702},
6166         { .id = 0x11060428, .name = "VT1718S",
6167           .patch = patch_vt1718S},
6168         { .id = 0x11064428, .name = "VT1718S",
6169           .patch = patch_vt1718S},
6170         { .id = 0x11060441, .name = "VT2020",
6171           .patch = patch_vt1718S},
6172         { .id = 0x11064441, .name = "VT1828S",
6173           .patch = patch_vt1718S},
6174         { .id = 0x11060433, .name = "VT1716S",
6175           .patch = patch_vt1716S},
6176         { .id = 0x1106a721, .name = "VT1716S",
6177           .patch = patch_vt1716S},
6178         { .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
6179         { .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
6180         { .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
6181         { .id = 0x11060440, .name = "VT1818S",
6182           .patch = patch_vt1708S},
6183         { .id = 0x11060446, .name = "VT1802",
6184                 .patch = patch_vt2002P},
6185         { .id = 0x11068446, .name = "VT1802",
6186                 .patch = patch_vt2002P},
6187         {} /* terminator */
6188 };
6189
6190 MODULE_ALIAS("snd-hda-codec-id:1106*");
6191
6192 static struct hda_codec_preset_list via_list = {
6193         .preset = snd_hda_preset_via,
6194         .owner = THIS_MODULE,
6195 };
6196
6197 MODULE_LICENSE("GPL");
6198 MODULE_DESCRIPTION("VIA HD-audio codec");
6199
6200 static int __init patch_via_init(void)
6201 {
6202         return snd_hda_add_codec_preset(&via_list);
6203 }
6204
6205 static void __exit patch_via_exit(void)
6206 {
6207         snd_hda_delete_codec_preset(&via_list);
6208 }
6209
6210 module_init(patch_via_init)
6211 module_exit(patch_via_exit)