ALSA: hda - Generate PCM names dynamically in patch_via.c
[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                 struct snd_ctl_elem_id id;
1619                 /* auto mute */
1620                 snd_hda_codec_amp_stereo(
1621                         codec, spec->autocfg.line_out_pins[0], HDA_OUTPUT, 0,
1622                         HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
1623                 /* notify change */
1624                 memset(&id, 0, sizeof(id));
1625                 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1626                 strcpy(id.name, "Front Playback Switch");
1627                 snd_ctl_notify(codec->bus->card, SNDRV_CTL_EVENT_MASK_VALUE,
1628                                &id);
1629         }
1630 }
1631
1632 /* mute mono out if HP or Line out is plugged */
1633 static void via_mono_automute(struct hda_codec *codec)
1634 {
1635         unsigned int hp_present, lineout_present;
1636         struct via_spec *spec = codec->spec;
1637
1638         if (spec->codec_type != VT1716S)
1639                 return;
1640
1641         lineout_present = snd_hda_jack_detect(codec,
1642                                               spec->autocfg.line_out_pins[0]);
1643
1644         /* Mute Mono Out if Line Out is plugged */
1645         if (lineout_present) {
1646                 snd_hda_codec_amp_stereo(
1647                         codec, 0x2A, HDA_OUTPUT, 0, HDA_AMP_MUTE, HDA_AMP_MUTE);
1648                 return;
1649         }
1650
1651         hp_present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
1652
1653         if (!spec->hp_independent_mode)
1654                 snd_hda_codec_amp_stereo(
1655                         codec, 0x2A, HDA_OUTPUT, 0, HDA_AMP_MUTE,
1656                         hp_present ? HDA_AMP_MUTE : 0);
1657 }
1658
1659 static void via_gpio_control(struct hda_codec *codec)
1660 {
1661         unsigned int gpio_data;
1662         unsigned int vol_counter;
1663         unsigned int vol;
1664         unsigned int master_vol;
1665
1666         struct via_spec *spec = codec->spec;
1667
1668         gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
1669                                        AC_VERB_GET_GPIO_DATA, 0) & 0x03;
1670
1671         vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
1672                                           0xF84, 0) & 0x3F0000) >> 16;
1673
1674         vol = vol_counter & 0x1F;
1675         master_vol = snd_hda_codec_read(codec, 0x1A, 0,
1676                                         AC_VERB_GET_AMP_GAIN_MUTE,
1677                                         AC_AMP_GET_INPUT);
1678
1679         if (gpio_data == 0x02) {
1680                 /* unmute line out */
1681                 snd_hda_codec_amp_stereo(codec, spec->autocfg.line_out_pins[0],
1682                                          HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
1683
1684                 if (vol_counter & 0x20) {
1685                         /* decrease volume */
1686                         if (vol > master_vol)
1687                                 vol = master_vol;
1688                         snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
1689                                                  0, HDA_AMP_VOLMASK,
1690                                                  master_vol-vol);
1691                 } else {
1692                         /* increase volume */
1693                         snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
1694                                          HDA_AMP_VOLMASK,
1695                                          ((master_vol+vol) > 0x2A) ? 0x2A :
1696                                           (master_vol+vol));
1697                 }
1698         } else if (!(gpio_data & 0x02)) {
1699                 /* mute line out */
1700                 snd_hda_codec_amp_stereo(codec,
1701                                          spec->autocfg.line_out_pins[0],
1702                                          HDA_OUTPUT, 0, HDA_AMP_MUTE,
1703                                          HDA_AMP_MUTE);
1704         }
1705 }
1706
1707 /* mute Internal-Speaker if HP is plugged */
1708 static void via_speaker_automute(struct hda_codec *codec)
1709 {
1710         unsigned int hp_present;
1711         struct via_spec *spec = codec->spec;
1712
1713         if (!VT2002P_COMPATIBLE(spec))
1714                 return;
1715
1716         hp_present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
1717
1718         if (!spec->hp_independent_mode) {
1719                 struct snd_ctl_elem_id id;
1720                 snd_hda_codec_amp_stereo(
1721                         codec, spec->autocfg.speaker_pins[0], HDA_OUTPUT, 0,
1722                         HDA_AMP_MUTE, hp_present ? HDA_AMP_MUTE : 0);
1723                 /* notify change */
1724                 memset(&id, 0, sizeof(id));
1725                 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1726                 strcpy(id.name, "Speaker Playback Switch");
1727                 snd_ctl_notify(codec->bus->card, SNDRV_CTL_EVENT_MASK_VALUE,
1728                                &id);
1729         }
1730 }
1731
1732 /* mute line-out and internal speaker if HP is plugged */
1733 static void via_hp_bind_automute(struct hda_codec *codec)
1734 {
1735         /* use long instead of int below just to avoid an internal compiler
1736          * error with gcc 4.0.x
1737          */
1738         unsigned long hp_present, present = 0;
1739         struct via_spec *spec = codec->spec;
1740         int i;
1741
1742         if (!spec->autocfg.hp_pins[0] || !spec->autocfg.line_out_pins[0])
1743                 return;
1744
1745         hp_present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
1746
1747         present = snd_hda_jack_detect(codec, spec->autocfg.line_out_pins[0]);
1748
1749         if (!spec->hp_independent_mode) {
1750                 /* Mute Line-Outs */
1751                 for (i = 0; i < spec->autocfg.line_outs; i++)
1752                         snd_hda_codec_amp_stereo(
1753                                 codec, spec->autocfg.line_out_pins[i],
1754                                 HDA_OUTPUT, 0,
1755                                 HDA_AMP_MUTE, hp_present ? HDA_AMP_MUTE : 0);
1756                 if (hp_present)
1757                         present = hp_present;
1758         }
1759         /* Speakers */
1760         for (i = 0; i < spec->autocfg.speaker_outs; i++)
1761                 snd_hda_codec_amp_stereo(
1762                         codec, spec->autocfg.speaker_pins[i], HDA_OUTPUT, 0,
1763                         HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
1764 }
1765
1766
1767 /* unsolicited event for jack sensing */
1768 static void via_unsol_event(struct hda_codec *codec,
1769                                   unsigned int res)
1770 {
1771         res >>= 26;
1772
1773         if (res & VIA_JACK_EVENT)
1774                 set_widgets_power_state(codec);
1775
1776         res &= ~VIA_JACK_EVENT;
1777
1778         if (res == VIA_HP_EVENT)
1779                 via_hp_automute(codec);
1780         else if (res == VIA_GPIO_EVENT)
1781                 via_gpio_control(codec);
1782         else if (res == VIA_MONO_EVENT)
1783                 via_mono_automute(codec);
1784         else if (res == VIA_SPEAKER_EVENT)
1785                 via_speaker_automute(codec);
1786         else if (res == VIA_BIND_HP_EVENT)
1787                 via_hp_bind_automute(codec);
1788 }
1789
1790 static int via_init(struct hda_codec *codec)
1791 {
1792         struct via_spec *spec = codec->spec;
1793         int i;
1794         for (i = 0; i < spec->num_iverbs; i++)
1795                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
1796
1797         /* Lydia Add for EAPD enable */
1798         if (!spec->dig_in_nid) { /* No Digital In connection */
1799                 if (spec->dig_in_pin) {
1800                         snd_hda_codec_write(codec, spec->dig_in_pin, 0,
1801                                             AC_VERB_SET_PIN_WIDGET_CONTROL,
1802                                             PIN_OUT);
1803                         snd_hda_codec_write(codec, spec->dig_in_pin, 0,
1804                                             AC_VERB_SET_EAPD_BTLENABLE, 0x02);
1805                 }
1806         } else /* enable SPDIF-input pin */
1807                 snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
1808                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
1809
1810         /* assign slave outs */
1811         if (spec->slave_dig_outs[0])
1812                 codec->slave_dig_outs = spec->slave_dig_outs;
1813
1814         return 0;
1815 }
1816
1817 #ifdef SND_HDA_NEEDS_RESUME
1818 static int via_suspend(struct hda_codec *codec, pm_message_t state)
1819 {
1820         struct via_spec *spec = codec->spec;
1821         vt1708_stop_hp_work(spec);
1822         return 0;
1823 }
1824 #endif
1825
1826 #ifdef CONFIG_SND_HDA_POWER_SAVE
1827 static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1828 {
1829         struct via_spec *spec = codec->spec;
1830         return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1831 }
1832 #endif
1833
1834 /*
1835  */
1836 static const struct hda_codec_ops via_patch_ops = {
1837         .build_controls = via_build_controls,
1838         .build_pcms = via_build_pcms,
1839         .init = via_init,
1840         .free = via_free,
1841 #ifdef SND_HDA_NEEDS_RESUME
1842         .suspend = via_suspend,
1843 #endif
1844 #ifdef CONFIG_SND_HDA_POWER_SAVE
1845         .check_power_status = via_check_power_status,
1846 #endif
1847 };
1848
1849 /* fill in the dac_nids table from the parsed pin configuration */
1850 static int vt1708_auto_fill_dac_nids(struct via_spec *spec,
1851                                      const struct auto_pin_cfg *cfg)
1852 {
1853         int i;
1854         hda_nid_t nid;
1855
1856         spec->multiout.num_dacs = cfg->line_outs;
1857
1858         spec->multiout.dac_nids = spec->private_dac_nids;
1859
1860         for (i = 0; i < 4; i++) {
1861                 nid = cfg->line_out_pins[i];
1862                 if (nid) {
1863                         /* config dac list */
1864                         switch (i) {
1865                         case AUTO_SEQ_FRONT:
1866                                 spec->private_dac_nids[i] = 0x10;
1867                                 break;
1868                         case AUTO_SEQ_CENLFE:
1869                                 spec->private_dac_nids[i] = 0x12;
1870                                 break;
1871                         case AUTO_SEQ_SURROUND:
1872                                 spec->private_dac_nids[i] = 0x11;
1873                                 break;
1874                         case AUTO_SEQ_SIDE:
1875                                 spec->private_dac_nids[i] = 0x13;
1876                                 break;
1877                         }
1878                 }
1879         }
1880
1881         return 0;
1882 }
1883
1884 /* add playback controls from the parsed DAC table */
1885 static int vt1708_auto_create_multi_out_ctls(struct via_spec *spec,
1886                                              const struct auto_pin_cfg *cfg)
1887 {
1888         char name[32];
1889         static const char * const chname[4] = {
1890                 "Front", "Surround", "C/LFE", "Side"
1891         };
1892         hda_nid_t nid, nid_vol, nid_vols[] = {0x17, 0x19, 0x1a, 0x1b};
1893         int i, err;
1894
1895         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
1896                 nid = cfg->line_out_pins[i];
1897
1898                 if (!nid)
1899                         continue;
1900
1901                 nid_vol = nid_vols[i];
1902
1903                 if (i == AUTO_SEQ_CENLFE) {
1904                         /* Center/LFE */
1905                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1906                                         "Center Playback Volume",
1907                                         HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
1908                                                             HDA_OUTPUT));
1909                         if (err < 0)
1910                                 return err;
1911                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1912                                               "LFE Playback Volume",
1913                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
1914                                                                   HDA_OUTPUT));
1915                         if (err < 0)
1916                                 return err;
1917                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1918                                               "Center Playback Switch",
1919                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
1920                                                                   HDA_OUTPUT));
1921                         if (err < 0)
1922                                 return err;
1923                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1924                                               "LFE Playback Switch",
1925                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
1926                                                                   HDA_OUTPUT));
1927                         if (err < 0)
1928                                 return err;
1929                 } else if (i == AUTO_SEQ_FRONT) {
1930                         /* add control to mixer index 0 */
1931                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1932                                               "Master Front Playback Volume",
1933                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1934                                                                   HDA_INPUT));
1935                         if (err < 0)
1936                                 return err;
1937                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1938                                               "Master Front Playback Switch",
1939                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1940                                                                   HDA_INPUT));
1941                         if (err < 0)
1942                                 return err;
1943
1944                         /* add control to PW3 */
1945                         sprintf(name, "%s Playback Volume", chname[i]);
1946                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1947                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1948                                                                   HDA_OUTPUT));
1949                         if (err < 0)
1950                                 return err;
1951                         sprintf(name, "%s Playback Switch", chname[i]);
1952                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1953                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1954                                                                   HDA_OUTPUT));
1955                         if (err < 0)
1956                                 return err;
1957                 } else {
1958                         sprintf(name, "%s Playback Volume", chname[i]);
1959                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1960                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1961                                                                   HDA_OUTPUT));
1962                         if (err < 0)
1963                                 return err;
1964                         sprintf(name, "%s Playback Switch", chname[i]);
1965                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1966                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1967                                                                   HDA_OUTPUT));
1968                         if (err < 0)
1969                                 return err;
1970                 }
1971         }
1972
1973         return 0;
1974 }
1975
1976 static void create_hp_imux(struct via_spec *spec)
1977 {
1978         int i;
1979         struct hda_input_mux *imux = &spec->private_imux[1];
1980         static const char * const texts[] = { "OFF", "ON", NULL};
1981
1982         /* for hp mode select */
1983         for (i = 0; texts[i]; i++)
1984                 snd_hda_add_imux_item(imux, texts[i], i, NULL);
1985
1986         spec->hp_mux = &spec->private_imux[1];
1987 }
1988
1989 static int vt1708_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
1990 {
1991         int err;
1992
1993         if (!pin)
1994                 return 0;
1995
1996         spec->multiout.hp_nid = VT1708_HP_NID; /* AOW3 */
1997         spec->hp_independent_mode_index = 1;
1998
1999         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2000                               "Headphone Playback Volume",
2001                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2002         if (err < 0)
2003                 return err;
2004         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2005                               "Headphone Playback Switch",
2006                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2007         if (err < 0)
2008                 return err;
2009
2010         create_hp_imux(spec);
2011
2012         return 0;
2013 }
2014
2015 static int get_connection_index(struct hda_codec *codec, hda_nid_t mux,
2016                                 hda_nid_t nid)
2017 {
2018         hda_nid_t conn[HDA_MAX_NUM_INPUTS];
2019         int i, nums;
2020
2021         nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
2022         for (i = 0; i < nums; i++)
2023                 if (conn[i] == nid)
2024                         return i;
2025         return -1;
2026 }
2027
2028 /* look for ADCs */
2029 static int via_fill_adcs(struct hda_codec *codec)
2030 {
2031         struct via_spec *spec = codec->spec;
2032         hda_nid_t nid = codec->start_nid;
2033         int i;
2034
2035         for (i = 0; i < codec->num_nodes; i++, nid++) {
2036                 unsigned int wcaps = get_wcaps(codec, nid);
2037                 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2038                         continue;
2039                 if (wcaps & AC_WCAP_DIGITAL)
2040                         continue;
2041                 if (!(wcaps & AC_WCAP_CONN_LIST))
2042                         continue;
2043                 if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
2044                         return -ENOMEM;
2045                 spec->adc_nids[spec->num_adc_nids++] = nid;
2046         }
2047         return 0;
2048 }
2049
2050 static int get_mux_nids(struct hda_codec *codec);
2051
2052 /* create playback/capture controls for input pins */
2053 static int vt_auto_create_analog_input_ctls(struct hda_codec *codec,
2054                                             const struct auto_pin_cfg *cfg,
2055                                             hda_nid_t mix_nid)
2056 {
2057         struct via_spec *spec = codec->spec;
2058         struct hda_input_mux *imux = &spec->private_imux[0];
2059         int i, err, idx, idx2, type, type_idx = 0;
2060         hda_nid_t cap_nid;
2061         hda_nid_t pin_idxs[8];
2062         int num_idxs;
2063
2064         err = via_fill_adcs(codec);
2065         if (err < 0)
2066                 return err;
2067         err = get_mux_nids(codec);
2068         if (err < 0)
2069                 return err;
2070         cap_nid = spec->mux_nids[0];
2071
2072         num_idxs = snd_hda_get_connections(codec, cap_nid, pin_idxs,
2073                                            ARRAY_SIZE(pin_idxs));
2074         if (num_idxs <= 0)
2075                 return 0;
2076
2077         /* for internal loopback recording select */
2078         for (idx = 0; idx < num_idxs; idx++) {
2079                 if (pin_idxs[idx] == mix_nid) {
2080                         snd_hda_add_imux_item(imux, "Stereo Mixer", idx, NULL);
2081                         break;
2082                 }
2083         }
2084
2085         for (i = 0; i < cfg->num_inputs; i++) {
2086                 const char *label;
2087                 type = cfg->inputs[i].type;
2088                 for (idx = 0; idx < num_idxs; idx++)
2089                         if (pin_idxs[idx] == cfg->inputs[i].pin)
2090                                 break;
2091                 if (idx >= num_idxs)
2092                         continue;
2093                 if (i > 0 && type == cfg->inputs[i - 1].type)
2094                         type_idx++;
2095                 else
2096                         type_idx = 0;
2097                 label = hda_get_autocfg_input_label(codec, cfg, i);
2098                 idx2 = get_connection_index(codec, mix_nid, pin_idxs[idx]);
2099                 if (idx2 >= 0)
2100                         err = via_new_analog_input(spec, label, type_idx,
2101                                                    idx2, mix_nid);
2102                 if (err < 0)
2103                         return err;
2104                 snd_hda_add_imux_item(imux, label, idx, NULL);
2105         }
2106         return 0;
2107 }
2108
2109 /* create playback/capture controls for input pins */
2110 static int vt1708_auto_create_analog_input_ctls(struct hda_codec *codec,
2111                                                 const struct auto_pin_cfg *cfg)
2112 {
2113         return vt_auto_create_analog_input_ctls(codec, cfg, 0x17);
2114 }
2115
2116 #ifdef CONFIG_SND_HDA_POWER_SAVE
2117 static const struct hda_amp_list vt1708_loopbacks[] = {
2118         { 0x17, HDA_INPUT, 1 },
2119         { 0x17, HDA_INPUT, 2 },
2120         { 0x17, HDA_INPUT, 3 },
2121         { 0x17, HDA_INPUT, 4 },
2122         { } /* end */
2123 };
2124 #endif
2125
2126 static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
2127 {
2128         unsigned int def_conf;
2129         unsigned char seqassoc;
2130
2131         def_conf = snd_hda_codec_get_pincfg(codec, nid);
2132         seqassoc = (unsigned char) get_defcfg_association(def_conf);
2133         seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
2134         if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
2135             && (seqassoc == 0xf0 || seqassoc == 0xff)) {
2136                 def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
2137                 snd_hda_codec_set_pincfg(codec, nid, def_conf);
2138         }
2139
2140         return;
2141 }
2142
2143 static int vt1708_jack_detect_get(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
2149         if (spec->codec_type != VT1708)
2150                 return 0;
2151         spec->vt1708_jack_detect =
2152                 !((snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8) & 0x1);
2153         ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
2154         return 0;
2155 }
2156
2157 static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
2158                                      struct snd_ctl_elem_value *ucontrol)
2159 {
2160         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2161         struct via_spec *spec = codec->spec;
2162         int change;
2163
2164         if (spec->codec_type != VT1708)
2165                 return 0;
2166         spec->vt1708_jack_detect = ucontrol->value.integer.value[0];
2167         change = (0x1 & (snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8))
2168                 == !spec->vt1708_jack_detect;
2169         if (spec->vt1708_jack_detect) {
2170                 mute_aa_path(codec, 1);
2171                 notify_aa_path_ctls(codec);
2172         }
2173         return change;
2174 }
2175
2176 static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
2177         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2178         .name = "Jack Detect",
2179         .count = 1,
2180         .info = snd_ctl_boolean_mono_info,
2181         .get = vt1708_jack_detect_get,
2182         .put = vt1708_jack_detect_put,
2183 };
2184
2185 static int vt1708_parse_auto_config(struct hda_codec *codec)
2186 {
2187         struct via_spec *spec = codec->spec;
2188         int err;
2189
2190         /* Add HP and CD pin config connect bit re-config action */
2191         vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
2192         vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
2193
2194         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2195         if (err < 0)
2196                 return err;
2197         err = vt1708_auto_fill_dac_nids(spec, &spec->autocfg);
2198         if (err < 0)
2199                 return err;
2200         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2201                 return 0; /* can't find valid BIOS pin config */
2202
2203         err = vt1708_auto_create_multi_out_ctls(spec, &spec->autocfg);
2204         if (err < 0)
2205                 return err;
2206         err = vt1708_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2207         if (err < 0)
2208                 return err;
2209         err = vt1708_auto_create_analog_input_ctls(codec, &spec->autocfg);
2210         if (err < 0)
2211                 return err;
2212         /* add jack detect on/off control */
2213         if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
2214                 return -ENOMEM;
2215
2216         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2217
2218         if (spec->autocfg.dig_outs)
2219                 spec->multiout.dig_out_nid = VT1708_DIGOUT_NID;
2220         spec->dig_in_pin = VT1708_DIGIN_PIN;
2221         if (spec->autocfg.dig_in_pin)
2222                 spec->dig_in_nid = VT1708_DIGIN_NID;
2223
2224         if (spec->kctls.list)
2225                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
2226
2227         spec->init_verbs[spec->num_iverbs++] = vt1708_volume_init_verbs;
2228
2229         spec->input_mux = &spec->private_imux[0];
2230
2231         if (spec->hp_mux)
2232                 via_hp_build(codec);
2233
2234         via_smart51_build(spec);
2235         return 1;
2236 }
2237
2238 /* init callback for auto-configuration model -- overriding the default init */
2239 static int via_auto_init(struct hda_codec *codec)
2240 {
2241         struct via_spec *spec = codec->spec;
2242
2243         via_init(codec);
2244         via_auto_init_multi_out(codec);
2245         via_auto_init_hp_out(codec);
2246         via_auto_init_analog_input(codec);
2247
2248         if (VT2002P_COMPATIBLE(spec)) {
2249                 via_hp_bind_automute(codec);
2250         } else {
2251                 via_hp_automute(codec);
2252                 via_speaker_automute(codec);
2253         }
2254
2255         return 0;
2256 }
2257
2258 static void vt1708_update_hp_jack_state(struct work_struct *work)
2259 {
2260         struct via_spec *spec = container_of(work, struct via_spec,
2261                                              vt1708_hp_work.work);
2262         if (spec->codec_type != VT1708)
2263                 return;
2264         /* if jack state toggled */
2265         if (spec->vt1708_hp_present
2266             != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
2267                 spec->vt1708_hp_present ^= 1;
2268                 via_hp_automute(spec->codec);
2269         }
2270         vt1708_start_hp_work(spec);
2271 }
2272
2273 static int get_mux_nids(struct hda_codec *codec)
2274 {
2275         struct via_spec *spec = codec->spec;
2276         hda_nid_t nid, conn[8];
2277         unsigned int type;
2278         int i, n;
2279
2280         for (i = 0; i < spec->num_adc_nids; i++) {
2281                 nid = spec->adc_nids[i];
2282                 while (nid) {
2283                         type = get_wcaps_type(get_wcaps(codec, nid));
2284                         if (type == AC_WID_PIN)
2285                                 break;
2286                         n = snd_hda_get_connections(codec, nid, conn,
2287                                                     ARRAY_SIZE(conn));
2288                         if (n <= 0)
2289                                 break;
2290                         if (n > 1) {
2291                                 spec->mux_nids[i] = nid;
2292                                 break;
2293                         }
2294                         nid = conn[0];
2295                 }
2296         }
2297         return 0;
2298 }
2299
2300 static int patch_vt1708(struct hda_codec *codec)
2301 {
2302         struct via_spec *spec;
2303         int err;
2304
2305         /* create a codec specific record */
2306         spec = via_new_spec(codec);
2307         if (spec == NULL)
2308                 return -ENOMEM;
2309
2310         /* automatic parse from the BIOS config */
2311         err = vt1708_parse_auto_config(codec);
2312         if (err < 0) {
2313                 via_free(codec);
2314                 return err;
2315         } else if (!err) {
2316                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
2317                        "from BIOS.  Using genenic mode...\n");
2318         }
2319
2320
2321         spec->stream_analog_playback = &vt1708_pcm_analog_playback;
2322         /* disable 32bit format on VT1708 */
2323         if (codec->vendor_id == 0x11061708)
2324                 spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
2325         spec->stream_analog_capture = &vt1708_pcm_analog_capture;
2326
2327         spec->stream_digital_playback = &vt1708_pcm_digital_playback;
2328         spec->stream_digital_capture = &vt1708_pcm_digital_capture;
2329
2330         if (spec->adc_nids && spec->input_mux) {
2331                 spec->mixers[spec->num_mixers] = vt1708_capture_mixer;
2332                 spec->num_mixers++;
2333         }
2334
2335         codec->patch_ops = via_patch_ops;
2336
2337         codec->patch_ops.init = via_auto_init;
2338 #ifdef CONFIG_SND_HDA_POWER_SAVE
2339         spec->loopback.amplist = vt1708_loopbacks;
2340 #endif
2341         INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
2342         return 0;
2343 }
2344
2345 /* capture mixer elements */
2346 static const struct snd_kcontrol_new vt1709_capture_mixer[] = {
2347         HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x0, HDA_INPUT),
2348         HDA_CODEC_MUTE("Capture Switch", 0x14, 0x0, HDA_INPUT),
2349         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x15, 0x0, HDA_INPUT),
2350         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x15, 0x0, HDA_INPUT),
2351         HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x16, 0x0, HDA_INPUT),
2352         HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x16, 0x0, HDA_INPUT),
2353         {
2354                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2355                 /* The multiple "Capture Source" controls confuse alsamixer
2356                  * So call somewhat different..
2357                  */
2358                 /* .name = "Capture Source", */
2359                 .name = "Input Source",
2360                 .count = 1,
2361                 .info = via_mux_enum_info,
2362                 .get = via_mux_enum_get,
2363                 .put = via_mux_enum_put,
2364         },
2365         { } /* end */
2366 };
2367
2368 static const struct hda_verb vt1709_uniwill_init_verbs[] = {
2369         {0x20, AC_VERB_SET_UNSOLICITED_ENABLE,
2370          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
2371         { }
2372 };
2373
2374 /*
2375  * generic initialization of ADC, input mixers and output mixers
2376  */
2377 static const struct hda_verb vt1709_10ch_volume_init_verbs[] = {
2378         /*
2379          * Unmute ADC0-2 and set the default input to mic-in
2380          */
2381         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2382         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2383         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2384
2385
2386         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2387          * mixer widget
2388          */
2389         /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2390         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2391         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2392         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2393         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2394         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2395
2396         /*
2397          * Set up output selector (0x1a, 0x1b, 0x29)
2398          */
2399         /* set vol=0 to output mixers */
2400         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2401         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2402         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2403
2404         /*
2405          *  Unmute PW3 and PW4
2406          */
2407         {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2408         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2409
2410         /* Set input of PW4 as MW0 */
2411         {0x20, AC_VERB_SET_CONNECT_SEL, 0},
2412         /* PW9 Output enable */
2413         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2414         { }
2415 };
2416
2417 static const struct hda_pcm_stream vt1709_10ch_pcm_analog_playback = {
2418         .substreams = 1,
2419         .channels_min = 2,
2420         .channels_max = 10,
2421         .nid = 0x10, /* NID to query formats and rates */
2422         .ops = {
2423                 .open = via_playback_pcm_open,
2424                 .prepare = via_playback_multi_pcm_prepare,
2425                 .cleanup = via_playback_multi_pcm_cleanup,
2426         },
2427 };
2428
2429 static const struct hda_pcm_stream vt1709_6ch_pcm_analog_playback = {
2430         .substreams = 1,
2431         .channels_min = 2,
2432         .channels_max = 6,
2433         .nid = 0x10, /* NID to query formats and rates */
2434         .ops = {
2435                 .open = via_playback_pcm_open,
2436                 .prepare = via_playback_multi_pcm_prepare,
2437                 .cleanup = via_playback_multi_pcm_cleanup,
2438         },
2439 };
2440
2441 static const struct hda_pcm_stream vt1709_pcm_analog_capture = {
2442         .substreams = 2,
2443         .channels_min = 2,
2444         .channels_max = 2,
2445         .nid = 0x14, /* NID to query formats and rates */
2446         .ops = {
2447                 .prepare = via_capture_pcm_prepare,
2448                 .cleanup = via_capture_pcm_cleanup
2449         },
2450 };
2451
2452 static const struct hda_pcm_stream vt1709_pcm_digital_playback = {
2453         .substreams = 1,
2454         .channels_min = 2,
2455         .channels_max = 2,
2456         /* NID is set in via_build_pcms */
2457         .ops = {
2458                 .open = via_dig_playback_pcm_open,
2459                 .close = via_dig_playback_pcm_close
2460         },
2461 };
2462
2463 static const struct hda_pcm_stream vt1709_pcm_digital_capture = {
2464         .substreams = 1,
2465         .channels_min = 2,
2466         .channels_max = 2,
2467 };
2468
2469 static int vt1709_auto_fill_dac_nids(struct via_spec *spec,
2470                                      const struct auto_pin_cfg *cfg)
2471 {
2472         int i;
2473         hda_nid_t nid;
2474
2475         if (cfg->line_outs == 4)  /* 10 channels */
2476                 spec->multiout.num_dacs = cfg->line_outs+1; /* AOW0~AOW4 */
2477         else if (cfg->line_outs == 3) /* 6 channels */
2478                 spec->multiout.num_dacs = cfg->line_outs; /* AOW0~AOW2 */
2479
2480         spec->multiout.dac_nids = spec->private_dac_nids;
2481
2482         if (cfg->line_outs == 4) { /* 10 channels */
2483                 for (i = 0; i < cfg->line_outs; i++) {
2484                         nid = cfg->line_out_pins[i];
2485                         if (nid) {
2486                                 /* config dac list */
2487                                 switch (i) {
2488                                 case AUTO_SEQ_FRONT:
2489                                         /* AOW0 */
2490                                         spec->private_dac_nids[i] = 0x10;
2491                                         break;
2492                                 case AUTO_SEQ_CENLFE:
2493                                         /* AOW2 */
2494                                         spec->private_dac_nids[i] = 0x12;
2495                                         break;
2496                                 case AUTO_SEQ_SURROUND:
2497                                         /* AOW3 */
2498                                         spec->private_dac_nids[i] = 0x11;
2499                                         break;
2500                                 case AUTO_SEQ_SIDE:
2501                                         /* AOW1 */
2502                                         spec->private_dac_nids[i] = 0x27;
2503                                         break;
2504                                 default:
2505                                         break;
2506                                 }
2507                         }
2508                 }
2509                 spec->private_dac_nids[cfg->line_outs] = 0x28; /* AOW4 */
2510
2511         } else if (cfg->line_outs == 3) { /* 6 channels */
2512                 for (i = 0; i < cfg->line_outs; i++) {
2513                         nid = cfg->line_out_pins[i];
2514                         if (nid) {
2515                                 /* config dac list */
2516                                 switch (i) {
2517                                 case AUTO_SEQ_FRONT:
2518                                         /* AOW0 */
2519                                         spec->private_dac_nids[i] = 0x10;
2520                                         break;
2521                                 case AUTO_SEQ_CENLFE:
2522                                         /* AOW2 */
2523                                         spec->private_dac_nids[i] = 0x12;
2524                                         break;
2525                                 case AUTO_SEQ_SURROUND:
2526                                         /* AOW1 */
2527                                         spec->private_dac_nids[i] = 0x11;
2528                                         break;
2529                                 default:
2530                                         break;
2531                                 }
2532                         }
2533                 }
2534         }
2535
2536         return 0;
2537 }
2538
2539 /* add playback controls from the parsed DAC table */
2540 static int vt1709_auto_create_multi_out_ctls(struct via_spec *spec,
2541                                              const struct auto_pin_cfg *cfg)
2542 {
2543         char name[32];
2544         static const char * const chname[4] = {
2545                 "Front", "Surround", "C/LFE", "Side"
2546         };
2547         hda_nid_t nid, nid_vol, nid_vols[] = {0x18, 0x1a, 0x1b, 0x29};
2548         int i, err;
2549
2550         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
2551                 nid = cfg->line_out_pins[i];
2552
2553                 if (!nid)
2554                         continue;
2555
2556                 nid_vol = nid_vols[i];
2557
2558                 if (i == AUTO_SEQ_CENLFE) {
2559                         /* Center/LFE */
2560                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2561                                               "Center Playback Volume",
2562                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2563                                                                   HDA_OUTPUT));
2564                         if (err < 0)
2565                                 return err;
2566                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2567                                               "LFE Playback Volume",
2568                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2569                                                                   HDA_OUTPUT));
2570                         if (err < 0)
2571                                 return err;
2572                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2573                                               "Center Playback Switch",
2574                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2575                                                                   HDA_OUTPUT));
2576                         if (err < 0)
2577                                 return err;
2578                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2579                                               "LFE Playback Switch",
2580                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2581                                                                   HDA_OUTPUT));
2582                         if (err < 0)
2583                                 return err;
2584                 } else if (i == AUTO_SEQ_FRONT) {
2585                         /* ADD control to mixer index 0 */
2586                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2587                                               "Master Front Playback Volume",
2588                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2589                                                                   HDA_INPUT));
2590                         if (err < 0)
2591                                 return err;
2592                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2593                                               "Master Front Playback Switch",
2594                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2595                                                                   HDA_INPUT));
2596                         if (err < 0)
2597                                 return err;
2598
2599                         /* add control to PW3 */
2600                         sprintf(name, "%s Playback Volume", chname[i]);
2601                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2602                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2603                                                                   HDA_OUTPUT));
2604                         if (err < 0)
2605                                 return err;
2606                         sprintf(name, "%s Playback Switch", chname[i]);
2607                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2608                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2609                                                                   HDA_OUTPUT));
2610                         if (err < 0)
2611                                 return err;
2612                 } else if (i == AUTO_SEQ_SURROUND) {
2613                         sprintf(name, "%s Playback Volume", chname[i]);
2614                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2615                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2616                                                                   HDA_OUTPUT));
2617                         if (err < 0)
2618                                 return err;
2619                         sprintf(name, "%s Playback Switch", chname[i]);
2620                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2621                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2622                                                                   HDA_OUTPUT));
2623                         if (err < 0)
2624                                 return err;
2625                 } else if (i == AUTO_SEQ_SIDE) {
2626                         sprintf(name, "%s Playback Volume", chname[i]);
2627                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2628                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2629                                                                   HDA_OUTPUT));
2630                         if (err < 0)
2631                                 return err;
2632                         sprintf(name, "%s Playback Switch", chname[i]);
2633                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2634                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2635                                                                   HDA_OUTPUT));
2636                         if (err < 0)
2637                                 return err;
2638                 }
2639         }
2640
2641         return 0;
2642 }
2643
2644 static int vt1709_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
2645 {
2646         int err;
2647
2648         if (!pin)
2649                 return 0;
2650
2651         if (spec->multiout.num_dacs == 5) /* 10 channels */
2652                 spec->multiout.hp_nid = VT1709_HP_DAC_NID;
2653         else if (spec->multiout.num_dacs == 3) /* 6 channels */
2654                 spec->multiout.hp_nid = 0;
2655         spec->hp_independent_mode_index = 1;
2656
2657         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2658                               "Headphone Playback Volume",
2659                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2660         if (err < 0)
2661                 return err;
2662         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2663                               "Headphone Playback Switch",
2664                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2665         if (err < 0)
2666                 return err;
2667
2668         return 0;
2669 }
2670
2671 /* create playback/capture controls for input pins */
2672 static int vt1709_auto_create_analog_input_ctls(struct hda_codec *codec,
2673                                                 const struct auto_pin_cfg *cfg)
2674 {
2675         return vt_auto_create_analog_input_ctls(codec, cfg, 0x18);
2676 }
2677
2678 static int vt1709_parse_auto_config(struct hda_codec *codec)
2679 {
2680         struct via_spec *spec = codec->spec;
2681         int err;
2682
2683         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2684         if (err < 0)
2685                 return err;
2686         err = vt1709_auto_fill_dac_nids(spec, &spec->autocfg);
2687         if (err < 0)
2688                 return err;
2689         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2690                 return 0; /* can't find valid BIOS pin config */
2691
2692         err = vt1709_auto_create_multi_out_ctls(spec, &spec->autocfg);
2693         if (err < 0)
2694                 return err;
2695         err = vt1709_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2696         if (err < 0)
2697                 return err;
2698         err = vt1709_auto_create_analog_input_ctls(codec, &spec->autocfg);
2699         if (err < 0)
2700                 return err;
2701
2702         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2703
2704         if (spec->autocfg.dig_outs)
2705                 spec->multiout.dig_out_nid = VT1709_DIGOUT_NID;
2706         spec->dig_in_pin = VT1709_DIGIN_PIN;
2707         if (spec->autocfg.dig_in_pin)
2708                 spec->dig_in_nid = VT1709_DIGIN_NID;
2709
2710         if (spec->kctls.list)
2711                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
2712
2713         spec->input_mux = &spec->private_imux[0];
2714
2715         if (spec->hp_mux)
2716                 via_hp_build(codec);
2717
2718         via_smart51_build(spec);
2719         return 1;
2720 }
2721
2722 #ifdef CONFIG_SND_HDA_POWER_SAVE
2723 static const struct hda_amp_list vt1709_loopbacks[] = {
2724         { 0x18, HDA_INPUT, 1 },
2725         { 0x18, HDA_INPUT, 2 },
2726         { 0x18, HDA_INPUT, 3 },
2727         { 0x18, HDA_INPUT, 4 },
2728         { } /* end */
2729 };
2730 #endif
2731
2732 static int patch_vt1709_10ch(struct hda_codec *codec)
2733 {
2734         struct via_spec *spec;
2735         int err;
2736
2737         /* create a codec specific record */
2738         spec = via_new_spec(codec);
2739         if (spec == NULL)
2740                 return -ENOMEM;
2741
2742         err = vt1709_parse_auto_config(codec);
2743         if (err < 0) {
2744                 via_free(codec);
2745                 return err;
2746         } else if (!err) {
2747                 printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
2748                        "Using genenic mode...\n");
2749         }
2750
2751         spec->init_verbs[spec->num_iverbs++] = vt1709_10ch_volume_init_verbs;
2752         spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
2753
2754         spec->stream_analog_playback = &vt1709_10ch_pcm_analog_playback;
2755         spec->stream_analog_capture = &vt1709_pcm_analog_capture;
2756
2757         spec->stream_digital_playback = &vt1709_pcm_digital_playback;
2758         spec->stream_digital_capture = &vt1709_pcm_digital_capture;
2759
2760         if (spec->adc_nids && spec->input_mux) {
2761                 spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
2762                 spec->num_mixers++;
2763         }
2764
2765         codec->patch_ops = via_patch_ops;
2766
2767         codec->patch_ops.init = via_auto_init;
2768         codec->patch_ops.unsol_event = via_unsol_event;
2769 #ifdef CONFIG_SND_HDA_POWER_SAVE
2770         spec->loopback.amplist = vt1709_loopbacks;
2771 #endif
2772
2773         return 0;
2774 }
2775 /*
2776  * generic initialization of ADC, input mixers and output mixers
2777  */
2778 static const struct hda_verb vt1709_6ch_volume_init_verbs[] = {
2779         /*
2780          * Unmute ADC0-2 and set the default input to mic-in
2781          */
2782         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2783         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2784         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2785
2786
2787         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2788          * mixer widget
2789          */
2790         /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2791         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2792         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2793         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2794         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2795         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2796
2797         /*
2798          * Set up output selector (0x1a, 0x1b, 0x29)
2799          */
2800         /* set vol=0 to output mixers */
2801         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2802         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2803         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2804
2805         /*
2806          *  Unmute PW3 and PW4
2807          */
2808         {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2809         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2810
2811         /* Set input of PW4 as MW0 */
2812         {0x20, AC_VERB_SET_CONNECT_SEL, 0},
2813         /* PW9 Output enable */
2814         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2815         { }
2816 };
2817
2818 static int patch_vt1709_6ch(struct hda_codec *codec)
2819 {
2820         struct via_spec *spec;
2821         int err;
2822
2823         /* create a codec specific record */
2824         spec = via_new_spec(codec);
2825         if (spec == NULL)
2826                 return -ENOMEM;
2827
2828         err = vt1709_parse_auto_config(codec);
2829         if (err < 0) {
2830                 via_free(codec);
2831                 return err;
2832         } else if (!err) {
2833                 printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
2834                        "Using genenic mode...\n");
2835         }
2836
2837         spec->init_verbs[spec->num_iverbs++] = vt1709_6ch_volume_init_verbs;
2838         spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
2839
2840         spec->stream_analog_playback = &vt1709_6ch_pcm_analog_playback;
2841         spec->stream_analog_capture = &vt1709_pcm_analog_capture;
2842
2843         spec->stream_digital_playback = &vt1709_pcm_digital_playback;
2844         spec->stream_digital_capture = &vt1709_pcm_digital_capture;
2845
2846         if (spec->adc_nids && spec->input_mux) {
2847                 spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
2848                 spec->num_mixers++;
2849         }
2850
2851         codec->patch_ops = via_patch_ops;
2852
2853         codec->patch_ops.init = via_auto_init;
2854         codec->patch_ops.unsol_event = via_unsol_event;
2855 #ifdef CONFIG_SND_HDA_POWER_SAVE
2856         spec->loopback.amplist = vt1709_loopbacks;
2857 #endif
2858         return 0;
2859 }
2860
2861 /* capture mixer elements */
2862 static const struct snd_kcontrol_new vt1708B_capture_mixer[] = {
2863         HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
2864         HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
2865         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
2866         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
2867         {
2868                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2869                 /* The multiple "Capture Source" controls confuse alsamixer
2870                  * So call somewhat different..
2871                  */
2872                 /* .name = "Capture Source", */
2873                 .name = "Input Source",
2874                 .count = 1,
2875                 .info = via_mux_enum_info,
2876                 .get = via_mux_enum_get,
2877                 .put = via_mux_enum_put,
2878         },
2879         { } /* end */
2880 };
2881 /*
2882  * generic initialization of ADC, input mixers and output mixers
2883  */
2884 static const struct hda_verb vt1708B_8ch_volume_init_verbs[] = {
2885         /*
2886          * Unmute ADC0-1 and set the default input to mic-in
2887          */
2888         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2889         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2890
2891
2892         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2893          * mixer widget
2894          */
2895         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2896         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2897         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2898         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2899         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2900         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2901
2902         /*
2903          * Set up output mixers
2904          */
2905         /* set vol=0 to output mixers */
2906         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2907         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2908         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2909
2910         /* Setup default input to PW4 */
2911         {0x1d, AC_VERB_SET_CONNECT_SEL, 0},
2912         /* PW9 Output enable */
2913         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2914         /* PW10 Input enable */
2915         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2916         { }
2917 };
2918
2919 static const struct hda_verb vt1708B_4ch_volume_init_verbs[] = {
2920         /*
2921          * Unmute ADC0-1 and set the default input to mic-in
2922          */
2923         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2924         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2925
2926
2927         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2928          * mixer widget
2929          */
2930         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2931         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2932         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2933         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2934         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2935         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2936
2937         /*
2938          * Set up output mixers
2939          */
2940         /* set vol=0 to output mixers */
2941         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2942         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2943         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2944
2945         /* Setup default input of PW4 to MW0 */
2946         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
2947         /* PW9 Output enable */
2948         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2949         /* PW10 Input enable */
2950         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2951         { }
2952 };
2953
2954 static const struct hda_verb vt1708B_uniwill_init_verbs[] = {
2955         {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
2956          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
2957         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2958         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2959         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2960         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2961         {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2962         {0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2963         {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2964         { }
2965 };
2966
2967 static int via_pcm_open_close(struct hda_pcm_stream *hinfo,
2968                               struct hda_codec *codec,
2969                               struct snd_pcm_substream *substream)
2970 {
2971         int idle = substream->pstr->substream_opened == 1
2972                 && substream->ref_count == 0;
2973
2974         analog_low_current_mode(codec, idle);
2975         return 0;
2976 }
2977
2978 static const struct hda_pcm_stream vt1708B_8ch_pcm_analog_playback = {
2979         .substreams = 2,
2980         .channels_min = 2,
2981         .channels_max = 8,
2982         .nid = 0x10, /* NID to query formats and rates */
2983         .ops = {
2984                 .open = via_playback_pcm_open,
2985                 .prepare = via_playback_multi_pcm_prepare,
2986                 .cleanup = via_playback_multi_pcm_cleanup,
2987                 .close = via_pcm_open_close
2988         },
2989 };
2990
2991 static const struct hda_pcm_stream vt1708B_4ch_pcm_analog_playback = {
2992         .substreams = 2,
2993         .channels_min = 2,
2994         .channels_max = 4,
2995         .nid = 0x10, /* NID to query formats and rates */
2996         .ops = {
2997                 .open = via_playback_pcm_open,
2998                 .prepare = via_playback_multi_pcm_prepare,
2999                 .cleanup = via_playback_multi_pcm_cleanup
3000         },
3001 };
3002
3003 static const struct hda_pcm_stream vt1708B_pcm_analog_capture = {
3004         .substreams = 2,
3005         .channels_min = 2,
3006         .channels_max = 2,
3007         .nid = 0x13, /* NID to query formats and rates */
3008         .ops = {
3009                 .open = via_pcm_open_close,
3010                 .prepare = via_capture_pcm_prepare,
3011                 .cleanup = via_capture_pcm_cleanup,
3012                 .close = via_pcm_open_close
3013         },
3014 };
3015
3016 static const struct hda_pcm_stream vt1708B_pcm_digital_playback = {
3017         .substreams = 1,
3018         .channels_min = 2,
3019         .channels_max = 2,
3020         /* NID is set in via_build_pcms */
3021         .ops = {
3022                 .open = via_dig_playback_pcm_open,
3023                 .close = via_dig_playback_pcm_close,
3024                 .prepare = via_dig_playback_pcm_prepare,
3025                 .cleanup = via_dig_playback_pcm_cleanup
3026         },
3027 };
3028
3029 static const struct hda_pcm_stream vt1708B_pcm_digital_capture = {
3030         .substreams = 1,
3031         .channels_min = 2,
3032         .channels_max = 2,
3033 };
3034
3035 /* fill in the dac_nids table from the parsed pin configuration */
3036 static int vt1708B_auto_fill_dac_nids(struct via_spec *spec,
3037                                      const struct auto_pin_cfg *cfg)
3038 {
3039         int i;
3040         hda_nid_t nid;
3041
3042         spec->multiout.num_dacs = cfg->line_outs;
3043
3044         spec->multiout.dac_nids = spec->private_dac_nids;
3045
3046         for (i = 0; i < 4; i++) {
3047                 nid = cfg->line_out_pins[i];
3048                 if (nid) {
3049                         /* config dac list */
3050                         switch (i) {
3051                         case AUTO_SEQ_FRONT:
3052                                 spec->private_dac_nids[i] = 0x10;
3053                                 break;
3054                         case AUTO_SEQ_CENLFE:
3055                                 spec->private_dac_nids[i] = 0x24;
3056                                 break;
3057                         case AUTO_SEQ_SURROUND:
3058                                 spec->private_dac_nids[i] = 0x11;
3059                                 break;
3060                         case AUTO_SEQ_SIDE:
3061                                 spec->private_dac_nids[i] = 0x25;
3062                                 break;
3063                         }
3064                 }
3065         }
3066
3067         return 0;
3068 }
3069
3070 /* add playback controls from the parsed DAC table */
3071 static int vt1708B_auto_create_multi_out_ctls(struct via_spec *spec,
3072                                              const struct auto_pin_cfg *cfg)
3073 {
3074         char name[32];
3075         static const char * const chname[4] = {
3076                 "Front", "Surround", "C/LFE", "Side"
3077         };
3078         hda_nid_t nid_vols[] = {0x16, 0x18, 0x26, 0x27};
3079         hda_nid_t nid, nid_vol = 0;
3080         int i, err;
3081
3082         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
3083                 nid = cfg->line_out_pins[i];
3084
3085                 if (!nid)
3086                         continue;
3087
3088                 nid_vol = nid_vols[i];
3089
3090                 if (i == AUTO_SEQ_CENLFE) {
3091                         /* Center/LFE */
3092                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3093                                               "Center Playback Volume",
3094                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
3095                                                                   HDA_OUTPUT));
3096                         if (err < 0)
3097                                 return err;
3098                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3099                                               "LFE Playback Volume",
3100                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
3101                                                                   HDA_OUTPUT));
3102                         if (err < 0)
3103                                 return err;
3104                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3105                                               "Center Playback Switch",
3106                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
3107                                                                   HDA_OUTPUT));
3108                         if (err < 0)
3109                                 return err;
3110                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3111                                               "LFE Playback Switch",
3112                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
3113                                                                   HDA_OUTPUT));
3114                         if (err < 0)
3115                                 return err;
3116                 } else if (i == AUTO_SEQ_FRONT) {
3117                         /* add control to mixer index 0 */
3118                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3119                                               "Master Front Playback Volume",
3120                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3121                                                                   HDA_INPUT));
3122                         if (err < 0)
3123                                 return err;
3124                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3125                                               "Master Front Playback Switch",
3126                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3127                                                                   HDA_INPUT));
3128                         if (err < 0)
3129                                 return err;
3130
3131                         /* add control to PW3 */
3132                         sprintf(name, "%s Playback Volume", chname[i]);
3133                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3134                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
3135                                                                   HDA_OUTPUT));
3136                         if (err < 0)
3137                                 return err;
3138                         sprintf(name, "%s Playback Switch", chname[i]);
3139                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3140                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
3141                                                                   HDA_OUTPUT));
3142                         if (err < 0)
3143                                 return err;
3144                 } else {
3145                         sprintf(name, "%s Playback Volume", chname[i]);
3146                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3147                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3148                                                                   HDA_OUTPUT));
3149                         if (err < 0)
3150                                 return err;
3151                         sprintf(name, "%s Playback Switch", chname[i]);
3152                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3153                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3154                                                                   HDA_OUTPUT));
3155                         if (err < 0)
3156                                 return err;
3157                 }
3158         }
3159
3160         return 0;
3161 }
3162
3163 static int vt1708B_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
3164 {
3165         int err;
3166
3167         if (!pin)
3168                 return 0;
3169
3170         spec->multiout.hp_nid = VT1708B_HP_NID; /* AOW3 */
3171         spec->hp_independent_mode_index = 1;
3172
3173         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3174                               "Headphone Playback Volume",
3175                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3176         if (err < 0)
3177                 return err;
3178         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3179                               "Headphone Playback Switch",
3180                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3181         if (err < 0)
3182                 return err;
3183
3184         create_hp_imux(spec);
3185
3186         return 0;
3187 }
3188
3189 /* create playback/capture controls for input pins */
3190 static int vt1708B_auto_create_analog_input_ctls(struct hda_codec *codec,
3191                                                 const struct auto_pin_cfg *cfg)
3192 {
3193         return vt_auto_create_analog_input_ctls(codec, cfg, 0x16);
3194 }
3195
3196 static int vt1708B_parse_auto_config(struct hda_codec *codec)
3197 {
3198         struct via_spec *spec = codec->spec;
3199         int err;
3200
3201         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3202         if (err < 0)
3203                 return err;
3204         err = vt1708B_auto_fill_dac_nids(spec, &spec->autocfg);
3205         if (err < 0)
3206                 return err;
3207         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3208                 return 0; /* can't find valid BIOS pin config */
3209
3210         err = vt1708B_auto_create_multi_out_ctls(spec, &spec->autocfg);
3211         if (err < 0)
3212                 return err;
3213         err = vt1708B_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
3214         if (err < 0)
3215                 return err;
3216         err = vt1708B_auto_create_analog_input_ctls(codec, &spec->autocfg);
3217         if (err < 0)
3218                 return err;
3219
3220         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3221
3222         if (spec->autocfg.dig_outs)
3223                 spec->multiout.dig_out_nid = VT1708B_DIGOUT_NID;
3224         spec->dig_in_pin = VT1708B_DIGIN_PIN;
3225         if (spec->autocfg.dig_in_pin)
3226                 spec->dig_in_nid = VT1708B_DIGIN_NID;
3227
3228         if (spec->kctls.list)
3229                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
3230
3231         spec->input_mux = &spec->private_imux[0];
3232
3233         if (spec->hp_mux)
3234                 via_hp_build(codec);
3235
3236         via_smart51_build(spec);
3237         return 1;
3238 }
3239
3240 #ifdef CONFIG_SND_HDA_POWER_SAVE
3241 static const struct hda_amp_list vt1708B_loopbacks[] = {
3242         { 0x16, HDA_INPUT, 1 },
3243         { 0x16, HDA_INPUT, 2 },
3244         { 0x16, HDA_INPUT, 3 },
3245         { 0x16, HDA_INPUT, 4 },
3246         { } /* end */
3247 };
3248 #endif
3249
3250 static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
3251 {
3252         struct via_spec *spec = codec->spec;
3253         int imux_is_smixer;
3254         unsigned int parm;
3255         int is_8ch = 0;
3256         if ((spec->codec_type != VT1708B_4CH) &&
3257             (codec->vendor_id != 0x11064397))
3258                 is_8ch = 1;
3259
3260         /* SW0 (17h) = stereo mixer */
3261         imux_is_smixer =
3262         (snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
3263          == ((spec->codec_type == VT1708S) ? 5 : 0));
3264         /* inputs */
3265         /* PW 1/2/5 (1ah/1bh/1eh) */
3266         parm = AC_PWRST_D3;
3267         set_pin_power_state(codec, 0x1a, &parm);
3268         set_pin_power_state(codec, 0x1b, &parm);
3269         set_pin_power_state(codec, 0x1e, &parm);
3270         if (imux_is_smixer)
3271                 parm = AC_PWRST_D0;
3272         /* SW0 (17h), AIW 0/1 (13h/14h) */
3273         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
3274         snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
3275         snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
3276
3277         /* outputs */
3278         /* PW0 (19h), SW1 (18h), AOW1 (11h) */
3279         parm = AC_PWRST_D3;
3280         set_pin_power_state(codec, 0x19, &parm);
3281         if (spec->smart51_enabled)
3282                 set_pin_power_state(codec, 0x1b, &parm);
3283         snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
3284         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
3285
3286         /* PW6 (22h), SW2 (26h), AOW2 (24h) */
3287         if (is_8ch) {
3288                 parm = AC_PWRST_D3;
3289                 set_pin_power_state(codec, 0x22, &parm);
3290                 if (spec->smart51_enabled)
3291                         set_pin_power_state(codec, 0x1a, &parm);
3292                 snd_hda_codec_write(codec, 0x26, 0,
3293                                     AC_VERB_SET_POWER_STATE, parm);
3294                 snd_hda_codec_write(codec, 0x24, 0,
3295                                     AC_VERB_SET_POWER_STATE, parm);
3296         } else if (codec->vendor_id == 0x11064397) {
3297                 /* PW7(23h), SW2(27h), AOW2(25h) */
3298                 parm = AC_PWRST_D3;
3299                 set_pin_power_state(codec, 0x23, &parm);
3300                 if (spec->smart51_enabled)
3301                         set_pin_power_state(codec, 0x1a, &parm);
3302                 snd_hda_codec_write(codec, 0x27, 0,
3303                                     AC_VERB_SET_POWER_STATE, parm);
3304                 snd_hda_codec_write(codec, 0x25, 0,
3305                                     AC_VERB_SET_POWER_STATE, parm);
3306         }
3307
3308         /* PW 3/4/7 (1ch/1dh/23h) */
3309         parm = AC_PWRST_D3;
3310         /* force to D0 for internal Speaker */
3311         set_pin_power_state(codec, 0x1c, &parm);
3312         set_pin_power_state(codec, 0x1d, &parm);
3313         if (is_8ch)
3314                 set_pin_power_state(codec, 0x23, &parm);
3315
3316         /* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
3317         snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
3318                             imux_is_smixer ? AC_PWRST_D0 : parm);
3319         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
3320         if (is_8ch) {
3321                 snd_hda_codec_write(codec, 0x25, 0,
3322                                     AC_VERB_SET_POWER_STATE, parm);
3323                 snd_hda_codec_write(codec, 0x27, 0,
3324                                     AC_VERB_SET_POWER_STATE, parm);
3325         } else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
3326                 snd_hda_codec_write(codec, 0x25, 0,
3327                                     AC_VERB_SET_POWER_STATE, parm);
3328 }
3329
3330 static int patch_vt1708S(struct hda_codec *codec);
3331 static int patch_vt1708B_8ch(struct hda_codec *codec)
3332 {
3333         struct via_spec *spec;
3334         int err;
3335
3336         if (get_codec_type(codec) == VT1708BCE)
3337                 return patch_vt1708S(codec);
3338         /* create a codec specific record */
3339         spec = via_new_spec(codec);
3340         if (spec == NULL)
3341                 return -ENOMEM;
3342
3343         /* automatic parse from the BIOS config */
3344         err = vt1708B_parse_auto_config(codec);
3345         if (err < 0) {
3346                 via_free(codec);
3347                 return err;
3348         } else if (!err) {
3349                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
3350                        "from BIOS.  Using genenic mode...\n");
3351         }
3352
3353         spec->init_verbs[spec->num_iverbs++] = vt1708B_8ch_volume_init_verbs;
3354         spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
3355
3356         spec->stream_analog_playback = &vt1708B_8ch_pcm_analog_playback;
3357         spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
3358
3359         spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
3360         spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
3361
3362         if (spec->adc_nids && spec->input_mux) {
3363                 spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
3364                 spec->num_mixers++;
3365         }
3366
3367         codec->patch_ops = via_patch_ops;
3368
3369         codec->patch_ops.init = via_auto_init;
3370         codec->patch_ops.unsol_event = via_unsol_event;
3371 #ifdef CONFIG_SND_HDA_POWER_SAVE
3372         spec->loopback.amplist = vt1708B_loopbacks;
3373 #endif
3374
3375         spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
3376
3377         return 0;
3378 }
3379
3380 static int patch_vt1708B_4ch(struct hda_codec *codec)
3381 {
3382         struct via_spec *spec;
3383         int err;
3384
3385         /* create a codec specific record */
3386         spec = via_new_spec(codec);
3387         if (spec == NULL)
3388                 return -ENOMEM;
3389
3390         /* automatic parse from the BIOS config */
3391         err = vt1708B_parse_auto_config(codec);
3392         if (err < 0) {
3393                 via_free(codec);
3394                 return err;
3395         } else if (!err) {
3396                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
3397                        "from BIOS.  Using genenic mode...\n");
3398         }
3399
3400         spec->init_verbs[spec->num_iverbs++] = vt1708B_4ch_volume_init_verbs;
3401         spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
3402
3403         spec->stream_analog_playback = &vt1708B_4ch_pcm_analog_playback;
3404         spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
3405
3406         spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
3407         spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
3408
3409         if (spec->adc_nids && spec->input_mux) {
3410                 spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
3411                 spec->num_mixers++;
3412         }
3413
3414         codec->patch_ops = via_patch_ops;
3415
3416         codec->patch_ops.init = via_auto_init;
3417         codec->patch_ops.unsol_event = via_unsol_event;
3418 #ifdef CONFIG_SND_HDA_POWER_SAVE
3419         spec->loopback.amplist = vt1708B_loopbacks;
3420 #endif
3421
3422         spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
3423
3424         return 0;
3425 }
3426
3427 /* Patch for VT1708S */
3428
3429 /* capture mixer elements */
3430 static const struct snd_kcontrol_new vt1708S_capture_mixer[] = {
3431         HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
3432         HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
3433         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
3434         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
3435         HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x1A, 0x0, HDA_INPUT),
3436         HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x1E, 0x0,
3437                          HDA_INPUT),
3438         {
3439                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3440                 /* The multiple "Capture Source" controls confuse alsamixer
3441                  * So call somewhat different..
3442                  */
3443                 /* .name = "Capture Source", */
3444                 .name = "Input Source",
3445                 .count = 1,
3446                 .info = via_mux_enum_info,
3447                 .get = via_mux_enum_get,
3448                 .put = via_mux_enum_put,
3449         },
3450         { } /* end */
3451 };
3452
3453 static const struct hda_verb vt1708S_volume_init_verbs[] = {
3454         /* Unmute ADC0-1 and set the default input to mic-in */
3455         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3456         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3457
3458         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the
3459          * analog-loopback mixer widget */
3460         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3461         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3462         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3463         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3464         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3465         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
3466
3467         /* Setup default input of PW4 to MW0 */
3468         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
3469         /* PW9, PW10  Output enable */
3470         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3471         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3472         /* Enable Mic Boost Volume backdoor */
3473         {0x1, 0xf98, 0x1},
3474         /* don't bybass mixer */
3475         {0x1, 0xf88, 0xc0},
3476         { }
3477 };
3478
3479 static const struct hda_verb vt1708S_uniwill_init_verbs[] = {
3480         {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
3481          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3482         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3483         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3484         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3485         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3486         {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3487         {0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3488         {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3489         { }
3490 };
3491
3492 static const struct hda_verb vt1705_uniwill_init_verbs[] = {
3493         {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
3494          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3495         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3496         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3497         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3498         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3499         {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3500         {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3501         { }
3502 };
3503
3504 static const struct hda_pcm_stream vt1708S_pcm_analog_playback = {
3505         .substreams = 2,
3506         .channels_min = 2,
3507         .channels_max = 8,
3508         .nid = 0x10, /* NID to query formats and rates */
3509         .ops = {
3510                 .open = via_playback_pcm_open,
3511                 .prepare = via_playback_multi_pcm_prepare,
3512                 .cleanup = via_playback_multi_pcm_cleanup,
3513                 .close = via_pcm_open_close
3514         },
3515 };
3516
3517 static const struct hda_pcm_stream vt1705_pcm_analog_playback = {
3518         .substreams = 2,
3519         .channels_min = 2,
3520         .channels_max = 6,
3521         .nid = 0x10, /* NID to query formats and rates */
3522         .ops = {
3523                 .open = via_playback_pcm_open,
3524                 .prepare = via_playback_multi_pcm_prepare,
3525                 .cleanup = via_playback_multi_pcm_cleanup,
3526                 .close = via_pcm_open_close
3527         },
3528 };
3529
3530 static const struct hda_pcm_stream vt1708S_pcm_analog_capture = {
3531         .substreams = 2,
3532         .channels_min = 2,
3533         .channels_max = 2,
3534         .nid = 0x13, /* NID to query formats and rates */
3535         .ops = {
3536                 .open = via_pcm_open_close,
3537                 .prepare = via_capture_pcm_prepare,
3538                 .cleanup = via_capture_pcm_cleanup,
3539                 .close = via_pcm_open_close
3540         },
3541 };
3542
3543 static const struct hda_pcm_stream vt1708S_pcm_digital_playback = {
3544         .substreams = 1,
3545         .channels_min = 2,
3546         .channels_max = 2,
3547         /* NID is set in via_build_pcms */
3548         .ops = {
3549                 .open = via_dig_playback_pcm_open,
3550                 .close = via_dig_playback_pcm_close,
3551                 .prepare = via_dig_playback_pcm_prepare,
3552                 .cleanup = via_dig_playback_pcm_cleanup
3553         },
3554 };
3555
3556 /* fill in the dac_nids table from the parsed pin configuration */
3557 static int vt1708S_auto_fill_dac_nids(struct via_spec *spec,
3558                                      const struct auto_pin_cfg *cfg)
3559 {
3560         int i;
3561         hda_nid_t nid;
3562
3563         spec->multiout.num_dacs = cfg->line_outs;
3564
3565         spec->multiout.dac_nids = spec->private_dac_nids;
3566
3567         for (i = 0; i < 4; i++) {
3568                 nid = cfg->line_out_pins[i];
3569                 if (nid) {
3570                         /* config dac list */
3571                         switch (i) {
3572                         case AUTO_SEQ_FRONT:
3573                                 spec->private_dac_nids[i] = 0x10;
3574                                 break;
3575                         case AUTO_SEQ_CENLFE:
3576                                 if (spec->codec->vendor_id == 0x11064397)
3577                                         spec->private_dac_nids[i] = 0x25;
3578                                 else
3579                                         spec->private_dac_nids[i] = 0x24;
3580                                 break;
3581                         case AUTO_SEQ_SURROUND:
3582                                 spec->private_dac_nids[i] = 0x11;
3583                                 break;
3584                         case AUTO_SEQ_SIDE:
3585                                 spec->private_dac_nids[i] = 0x25;
3586                                 break;
3587                         }
3588                 }
3589         }
3590
3591         /* for Smart 5.1, line/mic inputs double as output pins */
3592         if (cfg->line_outs == 1) {
3593                 spec->multiout.num_dacs = 3;
3594                 spec->private_dac_nids[AUTO_SEQ_SURROUND] = 0x11;
3595                 if (spec->codec->vendor_id == 0x11064397)
3596                         spec->private_dac_nids[AUTO_SEQ_CENLFE] = 0x25;
3597                 else
3598                         spec->private_dac_nids[AUTO_SEQ_CENLFE] = 0x24;
3599         }
3600
3601         return 0;
3602 }
3603
3604 /* add playback controls from the parsed DAC table */
3605 static int vt1708S_auto_create_multi_out_ctls(struct hda_codec *codec,
3606                                              const struct auto_pin_cfg *cfg)
3607 {
3608         struct via_spec *spec = codec->spec;
3609         char name[32];
3610         static const char * const chname[4] = {
3611                 "Front", "Surround", "C/LFE", "Side"
3612         };
3613         hda_nid_t nid_vols[2][4] = { {0x10, 0x11, 0x24, 0x25},
3614                                      {0x10, 0x11, 0x25, 0} };
3615         hda_nid_t nid_mutes[2][4] = { {0x1C, 0x18, 0x26, 0x27},
3616                                       {0x1C, 0x18, 0x27, 0} };
3617         hda_nid_t nid, nid_vol, nid_mute;
3618         int i, err;
3619
3620         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
3621                 nid = cfg->line_out_pins[i];
3622
3623                 /* for Smart 5.1, there are always at least six channels */
3624                 if (!nid && i > AUTO_SEQ_CENLFE)
3625                         continue;
3626
3627                 if (codec->vendor_id == 0x11064397) {
3628                         nid_vol = nid_vols[1][i];
3629                         nid_mute = nid_mutes[1][i];
3630                 } else {
3631                         nid_vol = nid_vols[0][i];
3632                         nid_mute = nid_mutes[0][i];
3633                 }
3634                 if (!nid_vol && !nid_mute)
3635                         continue;
3636
3637                 if (i == AUTO_SEQ_CENLFE) {
3638                         /* Center/LFE */
3639                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3640                                               "Center Playback Volume",
3641                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
3642                                                                   HDA_OUTPUT));
3643                         if (err < 0)
3644                                 return err;
3645                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3646                                               "LFE Playback Volume",
3647                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
3648                                                                   HDA_OUTPUT));
3649                         if (err < 0)
3650                                 return err;
3651                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3652                                               "Center Playback Switch",
3653                                               HDA_COMPOSE_AMP_VAL(nid_mute,
3654                                                                   1, 0,
3655                                                                   HDA_OUTPUT));
3656                         if (err < 0)
3657                                 return err;
3658                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3659                                               "LFE Playback Switch",
3660                                               HDA_COMPOSE_AMP_VAL(nid_mute,
3661                                                                   2, 0,
3662                                                                   HDA_OUTPUT));
3663                         if (err < 0)
3664                                 return err;
3665                 } else if (i == AUTO_SEQ_FRONT) {
3666                         /* add control to mixer index 0 */
3667                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3668                                               "Master Front Playback Volume",
3669                                               HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
3670                                                                   HDA_INPUT));
3671                         if (err < 0)
3672                                 return err;
3673                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3674                                               "Master Front Playback Switch",
3675                                               HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
3676                                                                   HDA_INPUT));
3677                         if (err < 0)
3678                                 return err;
3679
3680                         /* Front */
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                 } else {
3695                         sprintf(name, "%s Playback Volume", chname[i]);
3696                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3697                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3698                                                                   HDA_OUTPUT));
3699                         if (err < 0)
3700                                 return err;
3701                         sprintf(name, "%s Playback Switch", chname[i]);
3702                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3703                                               HDA_COMPOSE_AMP_VAL(nid_mute,
3704                                                                   3, 0,
3705                                                                   HDA_OUTPUT));
3706                         if (err < 0)
3707                                 return err;
3708                 }
3709         }
3710
3711         return 0;
3712 }
3713
3714 static int vt1708S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
3715 {
3716         int err;
3717
3718         if (!pin)
3719                 return 0;
3720
3721         spec->multiout.hp_nid = VT1708S_HP_NID; /* AOW3 */
3722         spec->hp_independent_mode_index = 1;
3723
3724         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3725                               "Headphone Playback Volume",
3726                               HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
3727         if (err < 0)
3728                 return err;
3729
3730         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3731                               "Headphone Playback Switch",
3732                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3733         if (err < 0)
3734                 return err;
3735
3736         create_hp_imux(spec);
3737
3738         return 0;
3739 }
3740
3741 /* create playback/capture controls for input pins */
3742 static int vt1708S_auto_create_analog_input_ctls(struct hda_codec *codec,
3743                                                 const struct auto_pin_cfg *cfg)
3744 {
3745         return vt_auto_create_analog_input_ctls(codec, cfg, 0x16);
3746 }
3747
3748 /* fill out digital output widgets; one for master and one for slave outputs */
3749 static void fill_dig_outs(struct hda_codec *codec)
3750 {
3751         struct via_spec *spec = codec->spec;
3752         int i;
3753
3754         for (i = 0; i < spec->autocfg.dig_outs; i++) {
3755                 hda_nid_t nid;
3756                 int conn;
3757
3758                 nid = spec->autocfg.dig_out_pins[i];
3759                 if (!nid)
3760                         continue;
3761                 conn = snd_hda_get_connections(codec, nid, &nid, 1);
3762                 if (conn < 1)
3763                         continue;
3764                 if (!spec->multiout.dig_out_nid)
3765                         spec->multiout.dig_out_nid = nid;
3766                 else {
3767                         spec->slave_dig_outs[0] = nid;
3768                         break; /* at most two dig outs */
3769                 }
3770         }
3771 }
3772
3773 static int vt1708S_parse_auto_config(struct hda_codec *codec)
3774 {
3775         struct via_spec *spec = codec->spec;
3776         int err;
3777
3778         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3779         if (err < 0)
3780                 return err;
3781         err = vt1708S_auto_fill_dac_nids(spec, &spec->autocfg);
3782         if (err < 0)
3783                 return err;
3784         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3785                 return 0; /* can't find valid BIOS pin config */
3786
3787         err = vt1708S_auto_create_multi_out_ctls(codec, &spec->autocfg);
3788         if (err < 0)
3789                 return err;
3790         err = vt1708S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
3791         if (err < 0)
3792                 return err;
3793         err = vt1708S_auto_create_analog_input_ctls(codec, &spec->autocfg);
3794         if (err < 0)
3795                 return err;
3796
3797         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3798
3799         fill_dig_outs(codec);
3800
3801         if (spec->kctls.list)
3802                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
3803
3804         spec->input_mux = &spec->private_imux[0];
3805
3806         if (spec->hp_mux)
3807                 via_hp_build(codec);
3808
3809         via_smart51_build(spec);
3810         return 1;
3811 }
3812
3813 #ifdef CONFIG_SND_HDA_POWER_SAVE
3814 static const struct hda_amp_list vt1708S_loopbacks[] = {
3815         { 0x16, HDA_INPUT, 1 },
3816         { 0x16, HDA_INPUT, 2 },
3817         { 0x16, HDA_INPUT, 3 },
3818         { 0x16, HDA_INPUT, 4 },
3819         { } /* end */
3820 };
3821 #endif
3822
3823 static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
3824                                int offset, int num_steps, int step_size)
3825 {
3826         snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
3827                                   (offset << AC_AMPCAP_OFFSET_SHIFT) |
3828                                   (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
3829                                   (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
3830                                   (0 << AC_AMPCAP_MUTE_SHIFT));
3831 }
3832
3833 static int patch_vt1708S(struct hda_codec *codec)
3834 {
3835         struct via_spec *spec;
3836         int err;
3837
3838         /* create a codec specific record */
3839         spec = via_new_spec(codec);
3840         if (spec == NULL)
3841                 return -ENOMEM;
3842
3843         /* automatic parse from the BIOS config */
3844         err = vt1708S_parse_auto_config(codec);
3845         if (err < 0) {
3846                 via_free(codec);
3847                 return err;
3848         } else if (!err) {
3849                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
3850                        "from BIOS.  Using genenic mode...\n");
3851         }
3852
3853         spec->init_verbs[spec->num_iverbs++] = vt1708S_volume_init_verbs;
3854         if (codec->vendor_id == 0x11064397)
3855                 spec->init_verbs[spec->num_iverbs++] =
3856                         vt1705_uniwill_init_verbs;
3857         else
3858                 spec->init_verbs[spec->num_iverbs++] =
3859                         vt1708S_uniwill_init_verbs;
3860
3861         if (codec->vendor_id == 0x11064397)
3862                 spec->stream_analog_playback = &vt1705_pcm_analog_playback;
3863         else
3864                 spec->stream_analog_playback = &vt1708S_pcm_analog_playback;
3865         spec->stream_analog_capture = &vt1708S_pcm_analog_capture;
3866
3867         spec->stream_digital_playback = &vt1708S_pcm_digital_playback;
3868
3869         if (spec->adc_nids && spec->input_mux) {
3870                 override_mic_boost(codec, 0x1a, 0, 3, 40);
3871                 override_mic_boost(codec, 0x1e, 0, 3, 40);
3872                 spec->mixers[spec->num_mixers] = vt1708S_capture_mixer;
3873                 spec->num_mixers++;
3874         }
3875
3876         codec->patch_ops = via_patch_ops;
3877
3878         codec->patch_ops.init = via_auto_init;
3879         codec->patch_ops.unsol_event = via_unsol_event;
3880 #ifdef CONFIG_SND_HDA_POWER_SAVE
3881         spec->loopback.amplist = vt1708S_loopbacks;
3882 #endif
3883
3884         /* correct names for VT1708BCE */
3885         if (get_codec_type(codec) == VT1708BCE) {
3886                 kfree(codec->chip_name);
3887                 codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
3888                 snprintf(codec->bus->card->mixername,
3889                          sizeof(codec->bus->card->mixername),
3890                          "%s %s", codec->vendor_name, codec->chip_name);
3891         }
3892         /* correct names for VT1705 */
3893         if (codec->vendor_id == 0x11064397)     {
3894                 kfree(codec->chip_name);
3895                 codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
3896                 snprintf(codec->bus->card->mixername,
3897                          sizeof(codec->bus->card->mixername),
3898                          "%s %s", codec->vendor_name, codec->chip_name);
3899         }
3900         spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
3901         return 0;
3902 }
3903
3904 /* Patch for VT1702 */
3905
3906 /* capture mixer elements */
3907 static const struct snd_kcontrol_new vt1702_capture_mixer[] = {
3908         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_INPUT),
3909         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_INPUT),
3910         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x20, 0x0, HDA_INPUT),
3911         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x20, 0x0, HDA_INPUT),
3912         HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x1F, 0x0, HDA_INPUT),
3913         HDA_CODEC_MUTE("Digital Mic Capture Switch", 0x1F, 0x0, HDA_INPUT),
3914         HDA_CODEC_VOLUME("Digital Mic Boost Capture Volume", 0x1E, 0x0,
3915                          HDA_INPUT),
3916         {
3917                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3918                 /* The multiple "Capture Source" controls confuse alsamixer
3919                  * So call somewhat different..
3920                  */
3921                 /* .name = "Capture Source", */
3922                 .name = "Input Source",
3923                 .count = 1,
3924                 .info = via_mux_enum_info,
3925                 .get = via_mux_enum_get,
3926                 .put = via_mux_enum_put,
3927         },
3928         { } /* end */
3929 };
3930
3931 static const struct hda_verb vt1702_volume_init_verbs[] = {
3932         /*
3933          * Unmute ADC0-1 and set the default input to mic-in
3934          */
3935         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3936         {0x1F, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3937         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3938
3939
3940         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3941          * mixer widget
3942          */
3943         /* Amp Indices: Mic1 = 1, Line = 1, Mic2 = 3 */
3944         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3945         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3946         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3947         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3948         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3949
3950         /* Setup default input of PW4 to MW0 */
3951         {0x17, AC_VERB_SET_CONNECT_SEL, 0x1},
3952         /* PW6 PW7 Output enable */
3953         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3954         {0x1C, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3955         /* mixer enable */
3956         {0x1, 0xF88, 0x3},
3957         /* GPIO 0~2 */
3958         {0x1, 0xF82, 0x3F},
3959         { }
3960 };
3961
3962 static const struct hda_verb vt1702_uniwill_init_verbs[] = {
3963         {0x17, AC_VERB_SET_UNSOLICITED_ENABLE,
3964          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3965         {0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3966         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3967         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3968         {0x18, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3969         { }
3970 };
3971
3972 static const struct hda_pcm_stream vt1702_pcm_analog_playback = {
3973         .substreams = 2,
3974         .channels_min = 2,
3975         .channels_max = 2,
3976         .nid = 0x10, /* NID to query formats and rates */
3977         .ops = {
3978                 .open = via_playback_pcm_open,
3979                 .prepare = via_playback_multi_pcm_prepare,
3980                 .cleanup = via_playback_multi_pcm_cleanup,
3981                 .close = via_pcm_open_close
3982         },
3983 };
3984
3985 static const struct hda_pcm_stream vt1702_pcm_analog_capture = {
3986         .substreams = 3,
3987         .channels_min = 2,
3988         .channels_max = 2,
3989         .nid = 0x12, /* NID to query formats and rates */
3990         .ops = {
3991                 .open = via_pcm_open_close,
3992                 .prepare = via_capture_pcm_prepare,
3993                 .cleanup = via_capture_pcm_cleanup,
3994                 .close = via_pcm_open_close
3995         },
3996 };
3997
3998 static const struct hda_pcm_stream vt1702_pcm_digital_playback = {
3999         .substreams = 2,
4000         .channels_min = 2,
4001         .channels_max = 2,
4002         /* NID is set in via_build_pcms */
4003         .ops = {
4004                 .open = via_dig_playback_pcm_open,
4005                 .close = via_dig_playback_pcm_close,
4006                 .prepare = via_dig_playback_pcm_prepare,
4007                 .cleanup = via_dig_playback_pcm_cleanup
4008         },
4009 };
4010
4011 /* fill in the dac_nids table from the parsed pin configuration */
4012 static int vt1702_auto_fill_dac_nids(struct via_spec *spec,
4013                                      const struct auto_pin_cfg *cfg)
4014 {
4015         spec->multiout.num_dacs = 1;
4016         spec->multiout.dac_nids = spec->private_dac_nids;
4017
4018         if (cfg->line_out_pins[0]) {
4019                 /* config dac list */
4020                 spec->private_dac_nids[0] = 0x10;
4021         }
4022
4023         return 0;
4024 }
4025
4026 /* add playback controls from the parsed DAC table */
4027 static int vt1702_auto_create_line_out_ctls(struct via_spec *spec,
4028                                              const struct auto_pin_cfg *cfg)
4029 {
4030         int err;
4031
4032         if (!cfg->line_out_pins[0])
4033                 return -1;
4034
4035         /* add control to mixer index 0 */
4036         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4037                               "Master Front Playback Volume",
4038                               HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
4039         if (err < 0)
4040                 return err;
4041         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4042                               "Master Front Playback Switch",
4043                               HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
4044         if (err < 0)
4045                 return err;
4046
4047         /* Front */
4048         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4049                               "Front Playback Volume",
4050                               HDA_COMPOSE_AMP_VAL(0x10, 3, 0, HDA_OUTPUT));
4051         if (err < 0)
4052                 return err;
4053         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4054                               "Front Playback Switch",
4055                               HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_OUTPUT));
4056         if (err < 0)
4057                 return err;
4058
4059         return 0;
4060 }
4061
4062 static int vt1702_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
4063 {
4064         int err, i;
4065         struct hda_input_mux *imux;
4066         static const char * const texts[] = { "ON", "OFF", NULL};
4067         if (!pin)
4068                 return 0;
4069         spec->multiout.hp_nid = 0x1D;
4070         spec->hp_independent_mode_index = 0;
4071
4072         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4073                               "Headphone Playback Volume",
4074                               HDA_COMPOSE_AMP_VAL(0x1D, 3, 0, HDA_OUTPUT));
4075         if (err < 0)
4076                 return err;
4077
4078         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4079                               "Headphone Playback Switch",
4080                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
4081         if (err < 0)
4082                 return err;
4083
4084         imux = &spec->private_imux[1];
4085
4086         /* for hp mode select */
4087         for (i = 0; texts[i]; i++)
4088                 snd_hda_add_imux_item(imux, texts[i], i, NULL);
4089
4090         spec->hp_mux = &spec->private_imux[1];
4091         return 0;
4092 }
4093
4094 /* create playback/capture controls for input pins */
4095 static int vt1702_auto_create_analog_input_ctls(struct hda_codec *codec,
4096                                                 const struct auto_pin_cfg *cfg)
4097 {
4098         return vt_auto_create_analog_input_ctls(codec, cfg, 0x1a);
4099 }
4100
4101 static int vt1702_parse_auto_config(struct hda_codec *codec)
4102 {
4103         struct via_spec *spec = codec->spec;
4104         int err;
4105
4106         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4107         if (err < 0)
4108                 return err;
4109         err = vt1702_auto_fill_dac_nids(spec, &spec->autocfg);
4110         if (err < 0)
4111                 return err;
4112         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
4113                 return 0; /* can't find valid BIOS pin config */
4114
4115         err = vt1702_auto_create_line_out_ctls(spec, &spec->autocfg);
4116         if (err < 0)
4117                 return err;
4118         err = vt1702_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
4119         if (err < 0)
4120                 return err;
4121         /* limit AA path volume to 0 dB */
4122         snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
4123                                   (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
4124                                   (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
4125                                   (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
4126                                   (1 << AC_AMPCAP_MUTE_SHIFT));
4127         err = vt1702_auto_create_analog_input_ctls(codec, &spec->autocfg);
4128         if (err < 0)
4129                 return err;
4130
4131         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4132
4133         fill_dig_outs(codec);
4134
4135         if (spec->kctls.list)
4136                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
4137
4138         spec->input_mux = &spec->private_imux[0];
4139
4140         if (spec->hp_mux)
4141                 via_hp_build(codec);
4142
4143         return 1;
4144 }
4145
4146 #ifdef CONFIG_SND_HDA_POWER_SAVE
4147 static const struct hda_amp_list vt1702_loopbacks[] = {
4148         { 0x1A, HDA_INPUT, 1 },
4149         { 0x1A, HDA_INPUT, 2 },
4150         { 0x1A, HDA_INPUT, 3 },
4151         { 0x1A, HDA_INPUT, 4 },
4152         { } /* end */
4153 };
4154 #endif
4155
4156 static void set_widgets_power_state_vt1702(struct hda_codec *codec)
4157 {
4158         int imux_is_smixer =
4159         snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
4160         unsigned int parm;
4161         /* inputs */
4162         /* PW 1/2/5 (14h/15h/18h) */
4163         parm = AC_PWRST_D3;
4164         set_pin_power_state(codec, 0x14, &parm);
4165         set_pin_power_state(codec, 0x15, &parm);
4166         set_pin_power_state(codec, 0x18, &parm);
4167         if (imux_is_smixer)
4168                 parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
4169         /* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
4170         snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
4171         snd_hda_codec_write(codec, 0x12, 0, AC_VERB_SET_POWER_STATE, parm);
4172         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
4173         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_POWER_STATE, parm);
4174
4175         /* outputs */
4176         /* PW 3/4 (16h/17h) */
4177         parm = AC_PWRST_D3;
4178         set_pin_power_state(codec, 0x17, &parm);
4179         set_pin_power_state(codec, 0x16, &parm);
4180         /* MW0 (1ah), AOW 0/1 (10h/1dh) */
4181         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
4182                             imux_is_smixer ? AC_PWRST_D0 : parm);
4183         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
4184         snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
4185 }
4186
4187 static int patch_vt1702(struct hda_codec *codec)
4188 {
4189         struct via_spec *spec;
4190         int err;
4191
4192         /* create a codec specific record */
4193         spec = via_new_spec(codec);
4194         if (spec == NULL)
4195                 return -ENOMEM;
4196
4197         /* automatic parse from the BIOS config */
4198         err = vt1702_parse_auto_config(codec);
4199         if (err < 0) {
4200                 via_free(codec);
4201                 return err;
4202         } else if (!err) {
4203                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
4204                        "from BIOS.  Using genenic mode...\n");
4205         }
4206
4207         spec->init_verbs[spec->num_iverbs++] = vt1702_volume_init_verbs;
4208         spec->init_verbs[spec->num_iverbs++] = vt1702_uniwill_init_verbs;
4209
4210         spec->stream_analog_playback = &vt1702_pcm_analog_playback;
4211         spec->stream_analog_capture = &vt1702_pcm_analog_capture;
4212
4213         spec->stream_digital_playback = &vt1702_pcm_digital_playback;
4214
4215         if (spec->adc_nids && spec->input_mux) {
4216                 spec->mixers[spec->num_mixers] = vt1702_capture_mixer;
4217                 spec->num_mixers++;
4218         }
4219
4220         codec->patch_ops = via_patch_ops;
4221
4222         codec->patch_ops.init = via_auto_init;
4223         codec->patch_ops.unsol_event = via_unsol_event;
4224 #ifdef CONFIG_SND_HDA_POWER_SAVE
4225         spec->loopback.amplist = vt1702_loopbacks;
4226 #endif
4227
4228         spec->set_widgets_power_state =  set_widgets_power_state_vt1702;
4229         return 0;
4230 }
4231
4232 /* Patch for VT1718S */
4233
4234 /* capture mixer elements */
4235 static const struct snd_kcontrol_new vt1718S_capture_mixer[] = {
4236         HDA_CODEC_VOLUME("Capture Volume", 0x10, 0x0, HDA_INPUT),
4237         HDA_CODEC_MUTE("Capture Switch", 0x10, 0x0, HDA_INPUT),
4238         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x11, 0x0, HDA_INPUT),
4239         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x11, 0x0, HDA_INPUT),
4240         HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x2b, 0x0, HDA_INPUT),
4241         HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x29, 0x0,
4242                          HDA_INPUT),
4243         {
4244                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4245                 /* The multiple "Capture Source" controls confuse alsamixer
4246                  * So call somewhat different..
4247                  */
4248                 .name = "Input Source",
4249                 .count = 2,
4250                 .info = via_mux_enum_info,
4251                 .get = via_mux_enum_get,
4252                 .put = via_mux_enum_put,
4253         },
4254         { } /* end */
4255 };
4256
4257 static const struct hda_verb vt1718S_volume_init_verbs[] = {
4258         /*
4259          * Unmute ADC0-1 and set the default input to mic-in
4260          */
4261         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4262         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4263
4264         /* Enable MW0 adjust Gain 5 */
4265         {0x1, 0xfb2, 0x10},
4266         /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
4267          * mixer widget
4268          */
4269         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
4270         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4271         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4272         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
4273         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
4274         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(5)},
4275
4276         /* Setup default input of Front HP to MW9 */
4277         {0x28, AC_VERB_SET_CONNECT_SEL, 0x1},
4278         /* PW9 PW10 Output enable */
4279         {0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
4280         {0x2e, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
4281         /* PW11 Input enable */
4282         {0x2f, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_IN_EN},
4283         /* Enable Boost Volume backdoor */
4284         {0x1, 0xf88, 0x8},
4285         /* MW0/1/2/3/4: un-mute index 0 (AOWx), mute index 1 (MW9) */
4286         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4287         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4288         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4289         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4290         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4291         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4292         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4293         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4294         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4295         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4296         /* set MUX1 = 2 (AOW4), MUX2 = 1 (AOW3) */
4297         {0x34, AC_VERB_SET_CONNECT_SEL, 0x2},
4298         {0x35, AC_VERB_SET_CONNECT_SEL, 0x1},
4299         /* Unmute MW4's index 0 */
4300         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4301         { }
4302 };
4303
4304
4305 static const struct hda_verb vt1718S_uniwill_init_verbs[] = {
4306         {0x28, AC_VERB_SET_UNSOLICITED_ENABLE,
4307          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
4308         {0x24, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4309         {0x25, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4310         {0x26, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4311         {0x27, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4312         {0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4313         {0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4314         {0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4315         { }
4316 };
4317
4318 static const struct hda_pcm_stream vt1718S_pcm_analog_playback = {
4319         .substreams = 2,
4320         .channels_min = 2,
4321         .channels_max = 10,
4322         .nid = 0x8, /* NID to query formats and rates */
4323         .ops = {
4324                 .open = via_playback_pcm_open,
4325                 .prepare = via_playback_multi_pcm_prepare,
4326                 .cleanup = via_playback_multi_pcm_cleanup,
4327                 .close = via_pcm_open_close,
4328         },
4329 };
4330
4331 static const struct hda_pcm_stream vt1718S_pcm_analog_capture = {
4332         .substreams = 2,
4333         .channels_min = 2,
4334         .channels_max = 2,
4335         .nid = 0x10, /* NID to query formats and rates */
4336         .ops = {
4337                 .open = via_pcm_open_close,
4338                 .prepare = via_capture_pcm_prepare,
4339                 .cleanup = via_capture_pcm_cleanup,
4340                 .close = via_pcm_open_close,
4341         },
4342 };
4343
4344 static const struct hda_pcm_stream vt1718S_pcm_digital_playback = {
4345         .substreams = 2,
4346         .channels_min = 2,
4347         .channels_max = 2,
4348         /* NID is set in via_build_pcms */
4349         .ops = {
4350                 .open = via_dig_playback_pcm_open,
4351                 .close = via_dig_playback_pcm_close,
4352                 .prepare = via_dig_playback_pcm_prepare,
4353                 .cleanup = via_dig_playback_pcm_cleanup
4354         },
4355 };
4356
4357 static const struct hda_pcm_stream vt1718S_pcm_digital_capture = {
4358         .substreams = 1,
4359         .channels_min = 2,
4360         .channels_max = 2,
4361 };
4362
4363 /* fill in the dac_nids table from the parsed pin configuration */
4364 static int vt1718S_auto_fill_dac_nids(struct via_spec *spec,
4365                                      const struct auto_pin_cfg *cfg)
4366 {
4367         int i;
4368         hda_nid_t nid;
4369
4370         spec->multiout.num_dacs = cfg->line_outs;
4371
4372         spec->multiout.dac_nids = spec->private_dac_nids;
4373
4374         for (i = 0; i < 4; i++) {
4375                 nid = cfg->line_out_pins[i];
4376                 if (nid) {
4377                         /* config dac list */
4378                         switch (i) {
4379                         case AUTO_SEQ_FRONT:
4380                                 spec->private_dac_nids[i] = 0x8;
4381                                 break;
4382                         case AUTO_SEQ_CENLFE:
4383                                 spec->private_dac_nids[i] = 0xa;
4384                                 break;
4385                         case AUTO_SEQ_SURROUND:
4386                                 spec->private_dac_nids[i] = 0x9;
4387                                 break;
4388                         case AUTO_SEQ_SIDE:
4389                                 spec->private_dac_nids[i] = 0xb;
4390                                 break;
4391                         }
4392                 }
4393         }
4394
4395         return 0;
4396 }
4397
4398 /* add playback controls from the parsed DAC table */
4399 static int vt1718S_auto_create_multi_out_ctls(struct via_spec *spec,
4400                                              const struct auto_pin_cfg *cfg)
4401 {
4402         char name[32];
4403         static const char * const chname[4] = {
4404                 "Front", "Surround", "C/LFE", "Side"
4405         };
4406         hda_nid_t nid_vols[] = {0x8, 0x9, 0xa, 0xb};
4407         hda_nid_t nid_mutes[] = {0x24, 0x25, 0x26, 0x27};
4408         hda_nid_t nid, nid_vol, nid_mute = 0;
4409         int i, err;
4410
4411         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
4412                 nid = cfg->line_out_pins[i];
4413
4414                 if (!nid)
4415                         continue;
4416                 nid_vol = nid_vols[i];
4417                 nid_mute = nid_mutes[i];
4418
4419                 if (i == AUTO_SEQ_CENLFE) {
4420                         /* Center/LFE */
4421                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4422                                               "Center Playback Volume",
4423                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
4424                                                                   HDA_OUTPUT));
4425                         if (err < 0)
4426                                 return err;
4427                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4428                                               "LFE Playback Volume",
4429                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
4430                                                                   HDA_OUTPUT));
4431                         if (err < 0)
4432                                 return err;
4433                         err = via_add_control(
4434                                 spec, VIA_CTL_WIDGET_MUTE,
4435                                 "Center Playback Switch",
4436                                 HDA_COMPOSE_AMP_VAL(nid_mute, 1, 0,
4437                                                     HDA_OUTPUT));
4438                         if (err < 0)
4439                                 return err;
4440                         err = via_add_control(
4441                                 spec, VIA_CTL_WIDGET_MUTE,
4442                                 "LFE Playback Switch",
4443                                 HDA_COMPOSE_AMP_VAL(nid_mute, 2, 0,
4444                                                     HDA_OUTPUT));
4445                         if (err < 0)
4446                                 return err;
4447                 } else if (i == AUTO_SEQ_FRONT) {
4448                         /* Front */
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                 } else {
4463                         sprintf(name, "%s Playback Volume", chname[i]);
4464                         err = via_add_control(
4465                                 spec, VIA_CTL_WIDGET_VOL, name,
4466                                 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
4467                         if (err < 0)
4468                                 return err;
4469                         sprintf(name, "%s Playback Switch", chname[i]);
4470                         err = via_add_control(
4471                                 spec, VIA_CTL_WIDGET_MUTE, name,
4472                                 HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
4473                                                     HDA_OUTPUT));
4474                         if (err < 0)
4475                                 return err;
4476                 }
4477         }
4478         return 0;
4479 }
4480
4481 static int vt1718S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
4482 {
4483         int err;
4484
4485         if (!pin)
4486                 return 0;
4487
4488         spec->multiout.hp_nid = 0xc; /* AOW4 */
4489         spec->hp_independent_mode_index = 1;
4490
4491         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4492                               "Headphone Playback Volume",
4493                               HDA_COMPOSE_AMP_VAL(0xc, 3, 0, HDA_OUTPUT));
4494         if (err < 0)
4495                 return err;
4496
4497         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4498                               "Headphone Playback Switch",
4499                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
4500         if (err < 0)
4501                 return err;
4502
4503         create_hp_imux(spec);
4504         return 0;
4505 }
4506
4507 /* create playback/capture controls for input pins */
4508 static int vt1718S_auto_create_analog_input_ctls(struct hda_codec *codec,
4509                                                 const struct auto_pin_cfg *cfg)
4510 {
4511         return vt_auto_create_analog_input_ctls(codec, cfg, 0x21);
4512 }
4513
4514 static int vt1718S_parse_auto_config(struct hda_codec *codec)
4515 {
4516         struct via_spec *spec = codec->spec;
4517         int err;
4518
4519         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4520
4521         if (err < 0)
4522                 return err;
4523         err = vt1718S_auto_fill_dac_nids(spec, &spec->autocfg);
4524         if (err < 0)
4525                 return err;
4526         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
4527                 return 0; /* can't find valid BIOS pin config */
4528
4529         err = vt1718S_auto_create_multi_out_ctls(spec, &spec->autocfg);
4530         if (err < 0)
4531                 return err;
4532         err = vt1718S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
4533         if (err < 0)
4534                 return err;
4535         err = vt1718S_auto_create_analog_input_ctls(codec, &spec->autocfg);
4536         if (err < 0)
4537                 return err;
4538
4539         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4540
4541         fill_dig_outs(codec);
4542
4543         if (spec->autocfg.dig_in_pin && codec->vendor_id == 0x11060428)
4544                 spec->dig_in_nid = 0x13;
4545
4546         if (spec->kctls.list)
4547                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
4548
4549         spec->input_mux = &spec->private_imux[0];
4550
4551         if (spec->hp_mux)
4552                 via_hp_build(codec);
4553
4554         via_smart51_build(spec);
4555
4556         return 1;
4557 }
4558
4559 #ifdef CONFIG_SND_HDA_POWER_SAVE
4560 static const struct hda_amp_list vt1718S_loopbacks[] = {
4561         { 0x21, HDA_INPUT, 1 },
4562         { 0x21, HDA_INPUT, 2 },
4563         { 0x21, HDA_INPUT, 3 },
4564         { 0x21, HDA_INPUT, 4 },
4565         { } /* end */
4566 };
4567 #endif
4568
4569 static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
4570 {
4571         struct via_spec *spec = codec->spec;
4572         int imux_is_smixer;
4573         unsigned int parm;
4574         /* MUX6 (1eh) = stereo mixer */
4575         imux_is_smixer =
4576         snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
4577         /* inputs */
4578         /* PW 5/6/7 (29h/2ah/2bh) */
4579         parm = AC_PWRST_D3;
4580         set_pin_power_state(codec, 0x29, &parm);
4581         set_pin_power_state(codec, 0x2a, &parm);
4582         set_pin_power_state(codec, 0x2b, &parm);
4583         if (imux_is_smixer)
4584                 parm = AC_PWRST_D0;
4585         /* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
4586         snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
4587         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
4588         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
4589         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
4590
4591         /* outputs */
4592         /* PW3 (27h), MW2 (1ah), AOW3 (bh) */
4593         parm = AC_PWRST_D3;
4594         set_pin_power_state(codec, 0x27, &parm);
4595         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE, parm);
4596         snd_hda_codec_write(codec, 0xb, 0, AC_VERB_SET_POWER_STATE, parm);
4597
4598         /* PW2 (26h), AOW2 (ah) */
4599         parm = AC_PWRST_D3;
4600         set_pin_power_state(codec, 0x26, &parm);
4601         if (spec->smart51_enabled)
4602                 set_pin_power_state(codec, 0x2b, &parm);
4603         snd_hda_codec_write(codec, 0xa, 0, AC_VERB_SET_POWER_STATE, parm);
4604
4605         /* PW0 (24h), AOW0 (8h) */
4606         parm = AC_PWRST_D3;
4607         set_pin_power_state(codec, 0x24, &parm);
4608         if (!spec->hp_independent_mode) /* check for redirected HP */
4609                 set_pin_power_state(codec, 0x28, &parm);
4610         snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
4611         /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
4612         snd_hda_codec_write(codec, 0x21, 0, AC_VERB_SET_POWER_STATE,
4613                             imux_is_smixer ? AC_PWRST_D0 : parm);
4614
4615         /* PW1 (25h), AOW1 (9h) */
4616         parm = AC_PWRST_D3;
4617         set_pin_power_state(codec, 0x25, &parm);
4618         if (spec->smart51_enabled)
4619                 set_pin_power_state(codec, 0x2a, &parm);
4620         snd_hda_codec_write(codec, 0x9, 0, AC_VERB_SET_POWER_STATE, parm);
4621
4622         if (spec->hp_independent_mode) {
4623                 /* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
4624                 parm = AC_PWRST_D3;
4625                 set_pin_power_state(codec, 0x28, &parm);
4626                 snd_hda_codec_write(codec, 0x1b, 0,
4627                                     AC_VERB_SET_POWER_STATE, parm);
4628                 snd_hda_codec_write(codec, 0x34, 0,
4629                                     AC_VERB_SET_POWER_STATE, parm);
4630                 snd_hda_codec_write(codec, 0xc, 0,
4631                                     AC_VERB_SET_POWER_STATE, parm);
4632         }
4633 }
4634
4635 static int patch_vt1718S(struct hda_codec *codec)
4636 {
4637         struct via_spec *spec;
4638         int err;
4639
4640         /* create a codec specific record */
4641         spec = via_new_spec(codec);
4642         if (spec == NULL)
4643                 return -ENOMEM;
4644
4645         /* automatic parse from the BIOS config */
4646         err = vt1718S_parse_auto_config(codec);
4647         if (err < 0) {
4648                 via_free(codec);
4649                 return err;
4650         } else if (!err) {
4651                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
4652                        "from BIOS.  Using genenic mode...\n");
4653         }
4654
4655         spec->init_verbs[spec->num_iverbs++] = vt1718S_volume_init_verbs;
4656         spec->init_verbs[spec->num_iverbs++] = vt1718S_uniwill_init_verbs;
4657
4658         spec->stream_analog_playback = &vt1718S_pcm_analog_playback;
4659         spec->stream_analog_capture = &vt1718S_pcm_analog_capture;
4660
4661         spec->stream_digital_playback = &vt1718S_pcm_digital_playback;
4662         if (codec->vendor_id == 0x11060428 || codec->vendor_id == 0x11060441)
4663                 spec->stream_digital_capture = &vt1718S_pcm_digital_capture;
4664
4665         if (spec->adc_nids && spec->input_mux) {
4666                 override_mic_boost(codec, 0x2b, 0, 3, 40);
4667                 override_mic_boost(codec, 0x29, 0, 3, 40);
4668                 spec->mixers[spec->num_mixers] = vt1718S_capture_mixer;
4669                 spec->num_mixers++;
4670         }
4671
4672         codec->patch_ops = via_patch_ops;
4673
4674         codec->patch_ops.init = via_auto_init;
4675         codec->patch_ops.unsol_event = via_unsol_event;
4676
4677 #ifdef CONFIG_SND_HDA_POWER_SAVE
4678         spec->loopback.amplist = vt1718S_loopbacks;
4679 #endif
4680
4681         spec->set_widgets_power_state =  set_widgets_power_state_vt1718S;
4682
4683         return 0;
4684 }
4685
4686 /* Patch for VT1716S */
4687
4688 static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
4689                             struct snd_ctl_elem_info *uinfo)
4690 {
4691         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
4692         uinfo->count = 1;
4693         uinfo->value.integer.min = 0;
4694         uinfo->value.integer.max = 1;
4695         return 0;
4696 }
4697
4698 static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
4699                            struct snd_ctl_elem_value *ucontrol)
4700 {
4701         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4702         int index = 0;
4703
4704         index = snd_hda_codec_read(codec, 0x26, 0,
4705                                                AC_VERB_GET_CONNECT_SEL, 0);
4706         if (index != -1)
4707                 *ucontrol->value.integer.value = index;
4708
4709         return 0;
4710 }
4711
4712 static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
4713                            struct snd_ctl_elem_value *ucontrol)
4714 {
4715         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4716         struct via_spec *spec = codec->spec;
4717         int index = *ucontrol->value.integer.value;
4718
4719         snd_hda_codec_write(codec, 0x26, 0,
4720                                                AC_VERB_SET_CONNECT_SEL, index);
4721         spec->dmic_enabled = index;
4722         set_widgets_power_state(codec);
4723         return 1;
4724 }
4725
4726 /* capture mixer elements */
4727 static const struct snd_kcontrol_new vt1716S_capture_mixer[] = {
4728         HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
4729         HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
4730         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
4731         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
4732         HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x1A, 0x0, HDA_INPUT),
4733         HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x1E, 0x0,
4734                          HDA_INPUT),
4735         {
4736                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4737                 .name = "Input Source",
4738                 .count = 1,
4739                 .info = via_mux_enum_info,
4740                 .get = via_mux_enum_get,
4741                 .put = via_mux_enum_put,
4742         },
4743         { } /* end */
4744 };
4745
4746 static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
4747         HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
4748         {
4749          .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4750          .name = "Digital Mic Capture Switch",
4751          .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
4752          .count = 1,
4753          .info = vt1716s_dmic_info,
4754          .get = vt1716s_dmic_get,
4755          .put = vt1716s_dmic_put,
4756          },
4757         {}                      /* end */
4758 };
4759
4760
4761 /* mono-out mixer elements */
4762 static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
4763         HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
4764         { } /* end */
4765 };
4766
4767 static const struct hda_verb vt1716S_volume_init_verbs[] = {
4768         /*
4769          * Unmute ADC0-1 and set the default input to mic-in
4770          */
4771         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4772         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4773
4774
4775         /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
4776          * mixer widget
4777          */
4778         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
4779         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4780         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4781         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
4782         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
4783         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
4784
4785         /* MUX Indices: Stereo Mixer = 5 */
4786         {0x17, AC_VERB_SET_CONNECT_SEL, 0x5},
4787
4788         /* Setup default input of PW4 to MW0 */
4789         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
4790
4791         /* Setup default input of SW1 as MW0 */
4792         {0x18, AC_VERB_SET_CONNECT_SEL, 0x1},
4793
4794         /* Setup default input of SW4 as AOW0 */
4795         {0x28, AC_VERB_SET_CONNECT_SEL, 0x1},
4796
4797         /* PW9 PW10 Output enable */
4798         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
4799         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
4800
4801         /* Unmute SW1, PW12 */
4802         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4803         {0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4804         /* PW12 Output enable */
4805         {0x2a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
4806         /* Enable Boost Volume backdoor */
4807         {0x1, 0xf8a, 0x80},
4808         /* don't bybass mixer */
4809         {0x1, 0xf88, 0xc0},
4810         /* Enable mono output */
4811         {0x1, 0xf90, 0x08},
4812         { }
4813 };
4814
4815
4816 static const struct hda_verb vt1716S_uniwill_init_verbs[] = {
4817         {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
4818          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
4819         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4820         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4821         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4822         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE,
4823          AC_USRSP_EN | VIA_MONO_EVENT | VIA_JACK_EVENT},
4824         {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4825         {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4826         { }
4827 };
4828
4829 static const struct hda_pcm_stream vt1716S_pcm_analog_playback = {
4830         .substreams = 2,
4831         .channels_min = 2,
4832         .channels_max = 6,
4833         .nid = 0x10, /* NID to query formats and rates */
4834         .ops = {
4835                 .open = via_playback_pcm_open,
4836                 .prepare = via_playback_multi_pcm_prepare,
4837                 .cleanup = via_playback_multi_pcm_cleanup,
4838                 .close = via_pcm_open_close,
4839         },
4840 };
4841
4842 static const struct hda_pcm_stream vt1716S_pcm_analog_capture = {
4843         .substreams = 2,
4844         .channels_min = 2,
4845         .channels_max = 2,
4846         .nid = 0x13, /* NID to query formats and rates */
4847         .ops = {
4848                 .open = via_pcm_open_close,
4849                 .prepare = via_capture_pcm_prepare,
4850                 .cleanup = via_capture_pcm_cleanup,
4851                 .close = via_pcm_open_close,
4852         },
4853 };
4854
4855 static const struct hda_pcm_stream vt1716S_pcm_digital_playback = {
4856         .substreams = 2,
4857         .channels_min = 2,
4858         .channels_max = 2,
4859         /* NID is set in via_build_pcms */
4860         .ops = {
4861                 .open = via_dig_playback_pcm_open,
4862                 .close = via_dig_playback_pcm_close,
4863                 .prepare = via_dig_playback_pcm_prepare,
4864                 .cleanup = via_dig_playback_pcm_cleanup
4865         },
4866 };
4867
4868 /* fill in the dac_nids table from the parsed pin configuration */
4869 static int vt1716S_auto_fill_dac_nids(struct via_spec *spec,
4870                                       const struct auto_pin_cfg *cfg)
4871 {       int i;
4872         hda_nid_t nid;
4873
4874         spec->multiout.num_dacs = cfg->line_outs;
4875
4876         spec->multiout.dac_nids = spec->private_dac_nids;
4877
4878         for (i = 0; i < 3; i++) {
4879                 nid = cfg->line_out_pins[i];
4880                 if (nid) {
4881                         /* config dac list */
4882                         switch (i) {
4883                         case AUTO_SEQ_FRONT:
4884                                 spec->private_dac_nids[i] = 0x10;
4885                                 break;
4886                         case AUTO_SEQ_CENLFE:
4887                                 spec->private_dac_nids[i] = 0x25;
4888                                 break;
4889                         case AUTO_SEQ_SURROUND:
4890                                 spec->private_dac_nids[i] = 0x11;
4891                                 break;
4892                         }
4893                 }
4894         }
4895
4896         return 0;
4897 }
4898
4899 /* add playback controls from the parsed DAC table */
4900 static int vt1716S_auto_create_multi_out_ctls(struct via_spec *spec,
4901                                               const struct auto_pin_cfg *cfg)
4902 {
4903         char name[32];
4904         static const char * const chname[3] = {
4905                 "Front", "Surround", "C/LFE"
4906         };
4907         hda_nid_t nid_vols[] = {0x10, 0x11, 0x25};
4908         hda_nid_t nid_mutes[] = {0x1C, 0x18, 0x27};
4909         hda_nid_t nid, nid_vol, nid_mute;
4910         int i, err;
4911
4912         for (i = 0; i <= AUTO_SEQ_CENLFE; i++) {
4913                 nid = cfg->line_out_pins[i];
4914
4915                 if (!nid)
4916                         continue;
4917
4918                 nid_vol = nid_vols[i];
4919                 nid_mute = nid_mutes[i];
4920
4921                 if (i == AUTO_SEQ_CENLFE) {
4922                         err = via_add_control(
4923                                 spec, VIA_CTL_WIDGET_VOL,
4924                                 "Center Playback Volume",
4925                                 HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0, HDA_OUTPUT));
4926                         if (err < 0)
4927                                 return err;
4928                         err = via_add_control(
4929                                 spec, VIA_CTL_WIDGET_VOL,
4930                                 "LFE Playback Volume",
4931                                 HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0, HDA_OUTPUT));
4932                         if (err < 0)
4933                                 return err;
4934                         err = via_add_control(
4935                                 spec, VIA_CTL_WIDGET_MUTE,
4936                                 "Center Playback Switch",
4937                                 HDA_COMPOSE_AMP_VAL(nid_mute, 1, 0,
4938                                                     HDA_OUTPUT));
4939                         if (err < 0)
4940                                 return err;
4941                         err = via_add_control(
4942                                 spec, VIA_CTL_WIDGET_MUTE,
4943                                 "LFE Playback Switch",
4944                                 HDA_COMPOSE_AMP_VAL(nid_mute, 2, 0,
4945                                                     HDA_OUTPUT));
4946                         if (err < 0)
4947                                 return err;
4948                 } else if (i == AUTO_SEQ_FRONT) {
4949
4950                         err = via_add_control(
4951                                 spec, VIA_CTL_WIDGET_VOL,
4952                                 "Master Front Playback Volume",
4953                                 HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_INPUT));
4954                         if (err < 0)
4955                                 return err;
4956                         err = via_add_control(
4957                                 spec, VIA_CTL_WIDGET_MUTE,
4958                                 "Master Front Playback Switch",
4959                                 HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_INPUT));
4960                         if (err < 0)
4961                                 return err;
4962
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                 } else {
4977                         sprintf(name, "%s Playback Volume", chname[i]);
4978                         err = via_add_control(
4979                                 spec, VIA_CTL_WIDGET_VOL, name,
4980                                 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
4981                         if (err < 0)
4982                                 return err;
4983                         sprintf(name, "%s Playback Switch", chname[i]);
4984                         err = via_add_control(
4985                                 spec, VIA_CTL_WIDGET_MUTE, name,
4986                                 HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
4987                                                     HDA_OUTPUT));
4988                         if (err < 0)
4989                                 return err;
4990                 }
4991         }
4992         return 0;
4993 }
4994
4995 static int vt1716S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
4996 {
4997         int err;
4998
4999         if (!pin)
5000                 return 0;
5001
5002         spec->multiout.hp_nid = 0x25; /* AOW3 */
5003         spec->hp_independent_mode_index = 1;
5004
5005         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5006                               "Headphone Playback Volume",
5007                               HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
5008         if (err < 0)
5009                 return err;
5010
5011         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
5012                               "Headphone Playback Switch",
5013                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
5014         if (err < 0)
5015                 return err;
5016
5017         create_hp_imux(spec);
5018         return 0;
5019 }
5020
5021 /* create playback/capture controls for input pins */
5022 static int vt1716S_auto_create_analog_input_ctls(struct hda_codec *codec,
5023                                                 const struct auto_pin_cfg *cfg)
5024 {
5025         return vt_auto_create_analog_input_ctls(codec, cfg, 0x16);
5026 }
5027
5028 static int vt1716S_parse_auto_config(struct hda_codec *codec)
5029 {
5030         struct via_spec *spec = codec->spec;
5031         int err;
5032
5033         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
5034         if (err < 0)
5035                 return err;
5036         err = vt1716S_auto_fill_dac_nids(spec, &spec->autocfg);
5037         if (err < 0)
5038                 return err;
5039         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
5040                 return 0; /* can't find valid BIOS pin config */
5041
5042         err = vt1716S_auto_create_multi_out_ctls(spec, &spec->autocfg);
5043         if (err < 0)
5044                 return err;
5045         err = vt1716S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
5046         if (err < 0)
5047                 return err;
5048         err = vt1716S_auto_create_analog_input_ctls(codec, &spec->autocfg);
5049         if (err < 0)
5050                 return err;
5051
5052         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
5053
5054         fill_dig_outs(codec);
5055
5056         if (spec->kctls.list)
5057                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
5058
5059         spec->input_mux = &spec->private_imux[0];
5060
5061         if (spec->hp_mux)
5062                 via_hp_build(codec);
5063
5064         via_smart51_build(spec);
5065
5066         return 1;
5067 }
5068
5069 #ifdef CONFIG_SND_HDA_POWER_SAVE
5070 static const struct hda_amp_list vt1716S_loopbacks[] = {
5071         { 0x16, HDA_INPUT, 1 },
5072         { 0x16, HDA_INPUT, 2 },
5073         { 0x16, HDA_INPUT, 3 },
5074         { 0x16, HDA_INPUT, 4 },
5075         { } /* end */
5076 };
5077 #endif
5078
5079 static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
5080 {
5081         struct via_spec *spec = codec->spec;
5082         int imux_is_smixer;
5083         unsigned int parm;
5084         unsigned int mono_out, present;
5085         /* SW0 (17h) = stereo mixer */
5086         imux_is_smixer =
5087         (snd_hda_codec_read(codec, 0x17, 0,
5088                             AC_VERB_GET_CONNECT_SEL, 0x00) ==  5);
5089         /* inputs */
5090         /* PW 1/2/5 (1ah/1bh/1eh) */
5091         parm = AC_PWRST_D3;
5092         set_pin_power_state(codec, 0x1a, &parm);
5093         set_pin_power_state(codec, 0x1b, &parm);
5094         set_pin_power_state(codec, 0x1e, &parm);
5095         if (imux_is_smixer)
5096                 parm = AC_PWRST_D0;
5097         /* SW0 (17h), AIW0(13h) */
5098         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
5099         snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
5100
5101         parm = AC_PWRST_D3;
5102         set_pin_power_state(codec, 0x1e, &parm);
5103         /* PW11 (22h) */
5104         if (spec->dmic_enabled)
5105                 set_pin_power_state(codec, 0x22, &parm);
5106         else
5107                 snd_hda_codec_write(codec, 0x22, 0,
5108                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
5109
5110         /* SW2(26h), AIW1(14h) */
5111         snd_hda_codec_write(codec, 0x26, 0, AC_VERB_SET_POWER_STATE, parm);
5112         snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
5113
5114         /* outputs */
5115         /* PW0 (19h), SW1 (18h), AOW1 (11h) */
5116         parm = AC_PWRST_D3;
5117         set_pin_power_state(codec, 0x19, &parm);
5118         /* Smart 5.1 PW2(1bh) */
5119         if (spec->smart51_enabled)
5120                 set_pin_power_state(codec, 0x1b, &parm);
5121         snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
5122         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
5123
5124         /* PW7 (23h), SW3 (27h), AOW3 (25h) */
5125         parm = AC_PWRST_D3;
5126         set_pin_power_state(codec, 0x23, &parm);
5127         /* Smart 5.1 PW1(1ah) */
5128         if (spec->smart51_enabled)
5129                 set_pin_power_state(codec, 0x1a, &parm);
5130         snd_hda_codec_write(codec, 0x27, 0, AC_VERB_SET_POWER_STATE, parm);
5131
5132         /* Smart 5.1 PW5(1eh) */
5133         if (spec->smart51_enabled)
5134                 set_pin_power_state(codec, 0x1e, &parm);
5135         snd_hda_codec_write(codec, 0x25, 0, AC_VERB_SET_POWER_STATE, parm);
5136
5137         /* Mono out */
5138         /* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
5139         present = snd_hda_jack_detect(codec, 0x1c);
5140
5141         if (present)
5142                 mono_out = 0;
5143         else {
5144                 present = snd_hda_jack_detect(codec, 0x1d);
5145                 if (!spec->hp_independent_mode && present)
5146                         mono_out = 0;
5147                 else
5148                         mono_out = 1;
5149         }
5150         parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
5151         snd_hda_codec_write(codec, 0x28, 0, AC_VERB_SET_POWER_STATE, parm);
5152         snd_hda_codec_write(codec, 0x29, 0, AC_VERB_SET_POWER_STATE, parm);
5153         snd_hda_codec_write(codec, 0x2a, 0, AC_VERB_SET_POWER_STATE, parm);
5154
5155         /* PW 3/4 (1ch/1dh) */
5156         parm = AC_PWRST_D3;
5157         set_pin_power_state(codec, 0x1c, &parm);
5158         set_pin_power_state(codec, 0x1d, &parm);
5159         /* HP Independent Mode, power on AOW3 */
5160         if (spec->hp_independent_mode)
5161                 snd_hda_codec_write(codec, 0x25, 0,
5162                                     AC_VERB_SET_POWER_STATE, parm);
5163
5164         /* force to D0 for internal Speaker */
5165         /* MW0 (16h), AOW0 (10h) */
5166         snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
5167                             imux_is_smixer ? AC_PWRST_D0 : parm);
5168         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
5169                             mono_out ? AC_PWRST_D0 : parm);
5170 }
5171
5172 static int patch_vt1716S(struct hda_codec *codec)
5173 {
5174         struct via_spec *spec;
5175         int err;
5176
5177         /* create a codec specific record */
5178         spec = via_new_spec(codec);
5179         if (spec == NULL)
5180                 return -ENOMEM;
5181
5182         /* automatic parse from the BIOS config */
5183         err = vt1716S_parse_auto_config(codec);
5184         if (err < 0) {
5185                 via_free(codec);
5186                 return err;
5187         } else if (!err) {
5188                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
5189                        "from BIOS.  Using genenic mode...\n");
5190         }
5191
5192         spec->init_verbs[spec->num_iverbs++]  = vt1716S_volume_init_verbs;
5193         spec->init_verbs[spec->num_iverbs++] = vt1716S_uniwill_init_verbs;
5194
5195         spec->stream_analog_playback = &vt1716S_pcm_analog_playback;
5196         spec->stream_analog_capture = &vt1716S_pcm_analog_capture;
5197
5198         spec->stream_digital_playback = &vt1716S_pcm_digital_playback;
5199
5200         if (spec->adc_nids && spec->input_mux) {
5201                 override_mic_boost(codec, 0x1a, 0, 3, 40);
5202                 override_mic_boost(codec, 0x1e, 0, 3, 40);
5203                 spec->mixers[spec->num_mixers] = vt1716S_capture_mixer;
5204                 spec->num_mixers++;
5205         }
5206
5207         spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
5208         spec->num_mixers++;
5209
5210         spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
5211
5212         codec->patch_ops = via_patch_ops;
5213
5214         codec->patch_ops.init = via_auto_init;
5215         codec->patch_ops.unsol_event = via_unsol_event;
5216
5217 #ifdef CONFIG_SND_HDA_POWER_SAVE
5218         spec->loopback.amplist = vt1716S_loopbacks;
5219 #endif
5220
5221         spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
5222         return 0;
5223 }
5224
5225 /* for vt2002P */
5226
5227 /* capture mixer elements */
5228 static const struct snd_kcontrol_new vt2002P_capture_mixer[] = {
5229         HDA_CODEC_VOLUME("Capture Volume", 0x10, 0x0, HDA_INPUT),
5230         HDA_CODEC_MUTE("Capture Switch", 0x10, 0x0, HDA_INPUT),
5231         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x11, 0x0, HDA_INPUT),
5232         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x11, 0x0, HDA_INPUT),
5233         HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x2b, 0x0, HDA_INPUT),
5234         HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x29, 0x0,
5235                          HDA_INPUT),
5236         {
5237                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5238                 /* The multiple "Capture Source" controls confuse alsamixer
5239                  * So call somewhat different..
5240                  */
5241                 /* .name = "Capture Source", */
5242                 .name = "Input Source",
5243                 .count = 2,
5244                 .info = via_mux_enum_info,
5245                 .get = via_mux_enum_get,
5246                 .put = via_mux_enum_put,
5247         },
5248         { } /* end */
5249 };
5250
5251 static const struct hda_verb vt2002P_volume_init_verbs[] = {
5252         /* Class-D speaker related verbs */
5253         {0x1, 0xfe0, 0x4},
5254         {0x1, 0xfe9, 0x80},
5255         {0x1, 0xfe2, 0x22},
5256         /*
5257          * Unmute ADC0-1 and set the default input to mic-in
5258          */
5259         {0x8, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5260         {0x9, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5261
5262
5263         /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
5264          * mixer widget
5265          */
5266         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
5267         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
5268         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5269         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
5270         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
5271         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
5272
5273         /* MUX Indices: Mic = 0 */
5274         {0x1e, AC_VERB_SET_CONNECT_SEL, 0},
5275         {0x1f, AC_VERB_SET_CONNECT_SEL, 0},
5276
5277         /* PW9 Output enable */
5278         {0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
5279
5280         /* Enable Boost Volume backdoor */
5281         {0x1, 0xfb9, 0x24},
5282
5283         /* MW0/1/4/8: un-mute index 0 (MUXx), un-mute index 1 (MW9) */
5284         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5285         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5286         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5287         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5288         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5289         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5290         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5291         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5292
5293         /* set MUX0/1/4/8 = 0 (AOW0) */
5294         {0x34, AC_VERB_SET_CONNECT_SEL, 0},
5295         {0x35, AC_VERB_SET_CONNECT_SEL, 0},
5296         {0x37, AC_VERB_SET_CONNECT_SEL, 0},
5297         {0x3b, AC_VERB_SET_CONNECT_SEL, 0},
5298
5299         /* set PW0 index=0 (MW0) */
5300         {0x24, AC_VERB_SET_CONNECT_SEL, 0},
5301
5302         /* Enable AOW0 to MW9 */
5303         {0x1, 0xfb8, 0x88},
5304         { }
5305 };
5306 static const struct hda_verb vt1802_volume_init_verbs[] = {
5307         /*
5308          * Unmute ADC0-1 and set the default input to mic-in
5309          */
5310         {0x8, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5311         {0x9, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5312
5313
5314         /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
5315          * mixer widget
5316          */
5317         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
5318         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
5319         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5320         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
5321         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
5322         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
5323
5324         /* MUX Indices: Mic = 0 */
5325         {0x1e, AC_VERB_SET_CONNECT_SEL, 0},
5326         {0x1f, AC_VERB_SET_CONNECT_SEL, 0},
5327
5328         /* PW9 Output enable */
5329         {0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
5330
5331         /* Enable Boost Volume backdoor */
5332         {0x1, 0xfb9, 0x24},
5333
5334         /* MW0/1/4/8: un-mute index 0 (MUXx), un-mute index 1 (MW9) */
5335         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5336         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5337         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5338         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5339         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5340         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5341         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5342         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5343
5344         /* set MUX0/1/4/8 = 0 (AOW0) */
5345         {0x34, AC_VERB_SET_CONNECT_SEL, 0},
5346         {0x35, AC_VERB_SET_CONNECT_SEL, 0},
5347         {0x38, AC_VERB_SET_CONNECT_SEL, 0},
5348         {0x3c, AC_VERB_SET_CONNECT_SEL, 0},
5349
5350         /* set PW0 index=0 (MW0) */
5351         {0x24, AC_VERB_SET_CONNECT_SEL, 0},
5352
5353         /* Enable AOW0 to MW9 */
5354         {0x1, 0xfb8, 0x88},
5355         { }
5356 };
5357
5358
5359 static const struct hda_verb vt2002P_uniwill_init_verbs[] = {
5360         {0x25, AC_VERB_SET_UNSOLICITED_ENABLE,
5361          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5362         {0x26, AC_VERB_SET_UNSOLICITED_ENABLE,
5363          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5364         {0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5365         {0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5366         {0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5367         { }
5368 };
5369 static const struct hda_verb vt1802_uniwill_init_verbs[] = {
5370         {0x25, AC_VERB_SET_UNSOLICITED_ENABLE,
5371          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5372         {0x28, AC_VERB_SET_UNSOLICITED_ENABLE,
5373          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5374         {0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5375         {0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5376         {0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5377         { }
5378 };
5379
5380 static const struct hda_pcm_stream vt2002P_pcm_analog_playback = {
5381         .substreams = 2,
5382         .channels_min = 2,
5383         .channels_max = 2,
5384         .nid = 0x8, /* NID to query formats and rates */
5385         .ops = {
5386                 .open = via_playback_pcm_open,
5387                 .prepare = via_playback_multi_pcm_prepare,
5388                 .cleanup = via_playback_multi_pcm_cleanup,
5389                 .close = via_pcm_open_close,
5390         },
5391 };
5392
5393 static const struct hda_pcm_stream vt2002P_pcm_analog_capture = {
5394         .substreams = 2,
5395         .channels_min = 2,
5396         .channels_max = 2,
5397         .nid = 0x10, /* NID to query formats and rates */
5398         .ops = {
5399                 .open = via_pcm_open_close,
5400                 .prepare = via_capture_pcm_prepare,
5401                 .cleanup = via_capture_pcm_cleanup,
5402                 .close = via_pcm_open_close,
5403         },
5404 };
5405
5406 static const struct hda_pcm_stream vt2002P_pcm_digital_playback = {
5407         .substreams = 1,
5408         .channels_min = 2,
5409         .channels_max = 2,
5410         /* NID is set in via_build_pcms */
5411         .ops = {
5412                 .open = via_dig_playback_pcm_open,
5413                 .close = via_dig_playback_pcm_close,
5414                 .prepare = via_dig_playback_pcm_prepare,
5415                 .cleanup = via_dig_playback_pcm_cleanup
5416         },
5417 };
5418
5419 /* fill in the dac_nids table from the parsed pin configuration */
5420 static int vt2002P_auto_fill_dac_nids(struct via_spec *spec,
5421                                       const struct auto_pin_cfg *cfg)
5422 {
5423         spec->multiout.num_dacs = 1;
5424         spec->multiout.dac_nids = spec->private_dac_nids;
5425         if (cfg->line_out_pins[0])
5426                 spec->private_dac_nids[0] = 0x8;
5427         return 0;
5428 }
5429
5430 /* add playback controls from the parsed DAC table */
5431 static int vt2002P_auto_create_multi_out_ctls(struct via_spec *spec,
5432                                              const struct auto_pin_cfg *cfg)
5433 {
5434         int err;
5435         hda_nid_t sw_nid;
5436
5437         if (!cfg->line_out_pins[0])
5438                 return -1;
5439
5440         if (spec->codec_type == VT1802)
5441                 sw_nid = 0x28;
5442         else
5443                 sw_nid = 0x26;
5444
5445         /* Line-Out: PortE */
5446         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5447                               "Master Front Playback Volume",
5448                               HDA_COMPOSE_AMP_VAL(0x8, 3, 0, HDA_OUTPUT));
5449         if (err < 0)
5450                 return err;
5451         err = via_add_control(spec, VIA_CTL_WIDGET_BIND_PIN_MUTE,
5452                               "Master Front Playback Switch",
5453                               HDA_COMPOSE_AMP_VAL(sw_nid, 3, 0, HDA_OUTPUT));
5454         if (err < 0)
5455                 return err;
5456
5457         return 0;
5458 }
5459
5460 static int vt2002P_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
5461 {
5462         int err;
5463
5464         if (!pin)
5465                 return 0;
5466
5467         spec->multiout.hp_nid = 0x9;
5468         spec->hp_independent_mode_index = 1;
5469
5470         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5471                               "Headphone Playback Volume",
5472                               HDA_COMPOSE_AMP_VAL(
5473                                       spec->multiout.hp_nid, 3, 0, HDA_OUTPUT));
5474         if (err < 0)
5475                 return err;
5476
5477         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
5478                               "Headphone Playback Switch",
5479                               HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
5480         if (err < 0)
5481                 return err;
5482
5483         create_hp_imux(spec);
5484         return 0;
5485 }
5486
5487 /* create playback/capture controls for input pins */
5488 static int vt2002P_auto_create_analog_input_ctls(struct hda_codec *codec,
5489                                                 const struct auto_pin_cfg *cfg)
5490 {
5491         return vt_auto_create_analog_input_ctls(codec, cfg, 0x21);
5492 }
5493
5494 static int vt2002P_parse_auto_config(struct hda_codec *codec)
5495 {
5496         struct via_spec *spec = codec->spec;
5497         int err;
5498
5499
5500         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
5501         if (err < 0)
5502                 return err;
5503
5504         err = vt2002P_auto_fill_dac_nids(spec, &spec->autocfg);
5505         if (err < 0)
5506                 return err;
5507
5508         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
5509                 return 0; /* can't find valid BIOS pin config */
5510
5511         err = vt2002P_auto_create_multi_out_ctls(spec, &spec->autocfg);
5512         if (err < 0)
5513                 return err;
5514         err = vt2002P_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
5515         if (err < 0)
5516                 return err;
5517         err = vt2002P_auto_create_analog_input_ctls(codec, &spec->autocfg);
5518         if (err < 0)
5519                 return err;
5520
5521         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
5522
5523         fill_dig_outs(codec);
5524
5525         if (spec->kctls.list)
5526                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
5527
5528         spec->input_mux = &spec->private_imux[0];
5529
5530         if (spec->hp_mux)
5531                 via_hp_build(codec);
5532
5533         return 1;
5534 }
5535
5536 #ifdef CONFIG_SND_HDA_POWER_SAVE
5537 static const struct hda_amp_list vt2002P_loopbacks[] = {
5538         { 0x21, HDA_INPUT, 0 },
5539         { 0x21, HDA_INPUT, 1 },
5540         { 0x21, HDA_INPUT, 2 },
5541         { } /* end */
5542 };
5543 #endif
5544
5545 static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
5546 {
5547         struct via_spec *spec = codec->spec;
5548         int imux_is_smixer;
5549         unsigned int parm;
5550         unsigned int present;
5551         /* MUX9 (1eh) = stereo mixer */
5552         imux_is_smixer =
5553         snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
5554         /* inputs */
5555         /* PW 5/6/7 (29h/2ah/2bh) */
5556         parm = AC_PWRST_D3;
5557         set_pin_power_state(codec, 0x29, &parm);
5558         set_pin_power_state(codec, 0x2a, &parm);
5559         set_pin_power_state(codec, 0x2b, &parm);
5560         parm = AC_PWRST_D0;
5561         /* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
5562         snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
5563         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
5564         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
5565         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
5566
5567         /* outputs */
5568         /* AOW0 (8h)*/
5569         snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
5570
5571         if (spec->codec_type == VT1802) {
5572                 /* PW4 (28h), MW4 (18h), MUX4(38h) */
5573                 parm = AC_PWRST_D3;
5574                 set_pin_power_state(codec, 0x28, &parm);
5575                 snd_hda_codec_write(codec, 0x18, 0,
5576                                     AC_VERB_SET_POWER_STATE, parm);
5577                 snd_hda_codec_write(codec, 0x38, 0,
5578                                     AC_VERB_SET_POWER_STATE, parm);
5579         } else {
5580                 /* PW4 (26h), MW4 (1ch), MUX4(37h) */
5581                 parm = AC_PWRST_D3;
5582                 set_pin_power_state(codec, 0x26, &parm);
5583                 snd_hda_codec_write(codec, 0x1c, 0,
5584                                     AC_VERB_SET_POWER_STATE, parm);
5585                 snd_hda_codec_write(codec, 0x37, 0,
5586                                     AC_VERB_SET_POWER_STATE, parm);
5587         }
5588
5589         if (spec->codec_type == VT1802) {
5590                 /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
5591                 parm = AC_PWRST_D3;
5592                 set_pin_power_state(codec, 0x25, &parm);
5593                 snd_hda_codec_write(codec, 0x15, 0,
5594                                     AC_VERB_SET_POWER_STATE, parm);
5595                 snd_hda_codec_write(codec, 0x35, 0,
5596                                     AC_VERB_SET_POWER_STATE, parm);
5597         } else {
5598                 /* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
5599                 parm = AC_PWRST_D3;
5600                 set_pin_power_state(codec, 0x25, &parm);
5601                 snd_hda_codec_write(codec, 0x19, 0,
5602                                     AC_VERB_SET_POWER_STATE, parm);
5603                 snd_hda_codec_write(codec, 0x35, 0,
5604                                     AC_VERB_SET_POWER_STATE, parm);
5605         }
5606
5607         if (spec->hp_independent_mode)
5608                 snd_hda_codec_write(codec, 0x9, 0,
5609                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
5610
5611         /* Class-D */
5612         /* PW0 (24h), MW0(18h/14h), MUX0(34h) */
5613         present = snd_hda_jack_detect(codec, 0x25);
5614
5615         parm = AC_PWRST_D3;
5616         set_pin_power_state(codec, 0x24, &parm);
5617         parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
5618         if (spec->codec_type == VT1802)
5619                 snd_hda_codec_write(codec, 0x14, 0,
5620                                     AC_VERB_SET_POWER_STATE, parm);
5621         else
5622                 snd_hda_codec_write(codec, 0x18, 0,
5623                                     AC_VERB_SET_POWER_STATE, parm);
5624         snd_hda_codec_write(codec, 0x34, 0, AC_VERB_SET_POWER_STATE, parm);
5625
5626         /* Mono Out */
5627         present = snd_hda_jack_detect(codec, 0x26);
5628
5629         parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
5630         if (spec->codec_type == VT1802) {
5631                 /* PW15 (33h), MW8(1ch), MUX8(3ch) */
5632                 snd_hda_codec_write(codec, 0x33, 0,
5633                                     AC_VERB_SET_POWER_STATE, parm);
5634                 snd_hda_codec_write(codec, 0x1c, 0,
5635                                     AC_VERB_SET_POWER_STATE, parm);
5636                 snd_hda_codec_write(codec, 0x3c, 0,
5637                                     AC_VERB_SET_POWER_STATE, parm);
5638         } else {
5639                 /* PW15 (31h), MW8(17h), MUX8(3bh) */
5640                 snd_hda_codec_write(codec, 0x31, 0,
5641                                     AC_VERB_SET_POWER_STATE, parm);
5642                 snd_hda_codec_write(codec, 0x17, 0,
5643                                     AC_VERB_SET_POWER_STATE, parm);
5644                 snd_hda_codec_write(codec, 0x3b, 0,
5645                                     AC_VERB_SET_POWER_STATE, parm);
5646         }
5647         /* MW9 (21h) */
5648         if (imux_is_smixer || !is_aa_path_mute(codec))
5649                 snd_hda_codec_write(codec, 0x21, 0,
5650                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
5651         else
5652                 snd_hda_codec_write(codec, 0x21, 0,
5653                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
5654 }
5655
5656 /* patch for vt2002P */
5657 static int patch_vt2002P(struct hda_codec *codec)
5658 {
5659         struct via_spec *spec;
5660         int err;
5661
5662         /* create a codec specific record */
5663         spec = via_new_spec(codec);
5664         if (spec == NULL)
5665                 return -ENOMEM;
5666
5667         /* automatic parse from the BIOS config */
5668         err = vt2002P_parse_auto_config(codec);
5669         if (err < 0) {
5670                 via_free(codec);
5671                 return err;
5672         } else if (!err) {
5673                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
5674                        "from BIOS.  Using genenic mode...\n");
5675         }
5676
5677         if (spec->codec_type == VT1802)
5678                 spec->init_verbs[spec->num_iverbs++]  =
5679                         vt1802_volume_init_verbs;
5680         else
5681                 spec->init_verbs[spec->num_iverbs++]  =
5682                         vt2002P_volume_init_verbs;
5683
5684         if (spec->codec_type == VT1802)
5685                 spec->init_verbs[spec->num_iverbs++] =
5686                         vt1802_uniwill_init_verbs;
5687         else
5688                 spec->init_verbs[spec->num_iverbs++] =
5689                         vt2002P_uniwill_init_verbs;
5690
5691         spec->stream_analog_playback = &vt2002P_pcm_analog_playback;
5692         spec->stream_analog_capture = &vt2002P_pcm_analog_capture;
5693
5694         spec->stream_digital_playback = &vt2002P_pcm_digital_playback;
5695
5696         if (spec->adc_nids && spec->input_mux) {
5697                 override_mic_boost(codec, 0x2b, 0, 3, 40);
5698                 override_mic_boost(codec, 0x29, 0, 3, 40);
5699                 spec->mixers[spec->num_mixers] = vt2002P_capture_mixer;
5700                 spec->num_mixers++;
5701         }
5702
5703         codec->patch_ops = via_patch_ops;
5704
5705         codec->patch_ops.init = via_auto_init;
5706         codec->patch_ops.unsol_event = via_unsol_event;
5707
5708 #ifdef CONFIG_SND_HDA_POWER_SAVE
5709         spec->loopback.amplist = vt2002P_loopbacks;
5710 #endif
5711
5712         spec->set_widgets_power_state =  set_widgets_power_state_vt2002P;
5713         return 0;
5714 }
5715
5716 /* for vt1812 */
5717
5718 /* capture mixer elements */
5719 static const struct snd_kcontrol_new vt1812_capture_mixer[] = {
5720         HDA_CODEC_VOLUME("Capture Volume", 0x10, 0x0, HDA_INPUT),
5721         HDA_CODEC_MUTE("Capture Switch", 0x10, 0x0, HDA_INPUT),
5722         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x11, 0x0, HDA_INPUT),
5723         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x11, 0x0, HDA_INPUT),
5724         HDA_CODEC_MUTE("Mic Boost Capture Volume", 0x2b, 0x0, HDA_INPUT),
5725         HDA_CODEC_MUTE("Front Mic Boost Capture Volume", 0x29, 0x0,
5726                        HDA_INPUT),
5727         {
5728                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5729                 /* The multiple "Capture Source" controls confuse alsamixer
5730                  * So call somewhat different..
5731                  */
5732                 .name = "Input Source",
5733                 .count = 2,
5734                 .info = via_mux_enum_info,
5735                 .get = via_mux_enum_get,
5736                 .put = via_mux_enum_put,
5737         },
5738         { } /* end */
5739 };
5740
5741 static const struct hda_verb vt1812_volume_init_verbs[] = {
5742         /*
5743          * Unmute ADC0-1 and set the default input to mic-in
5744          */
5745         {0x8, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5746         {0x9, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5747
5748
5749         /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
5750          * mixer widget
5751          */
5752         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
5753         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
5754         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5755         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
5756         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
5757         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
5758
5759         /* MUX Indices: Mic = 0 */
5760         {0x1e, AC_VERB_SET_CONNECT_SEL, 0},
5761         {0x1f, AC_VERB_SET_CONNECT_SEL, 0},
5762
5763         /* PW9 Output enable */
5764         {0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
5765
5766         /* Enable Boost Volume backdoor */
5767         {0x1, 0xfb9, 0x24},
5768
5769         /* MW0/1/4/13/15: un-mute index 0 (MUXx), un-mute index 1 (MW9) */
5770         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5771         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5772         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5773         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5774         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5775         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5776         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5777         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5778         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5779         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5780
5781         /* set MUX0/1/4/13/15 = 0 (AOW0) */
5782         {0x34, AC_VERB_SET_CONNECT_SEL, 0},
5783         {0x35, AC_VERB_SET_CONNECT_SEL, 0},
5784         {0x38, AC_VERB_SET_CONNECT_SEL, 0},
5785         {0x3c, AC_VERB_SET_CONNECT_SEL, 0},
5786         {0x3d, AC_VERB_SET_CONNECT_SEL, 0},
5787
5788         /* Enable AOW0 to MW9 */
5789         {0x1, 0xfb8, 0xa8},
5790         { }
5791 };
5792
5793
5794 static const struct hda_verb vt1812_uniwill_init_verbs[] = {
5795         {0x33, AC_VERB_SET_UNSOLICITED_ENABLE,
5796          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5797         {0x25, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT },
5798         {0x28, AC_VERB_SET_UNSOLICITED_ENABLE,
5799          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5800         {0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5801         {0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5802         {0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5803         { }
5804 };
5805
5806 static const struct hda_pcm_stream vt1812_pcm_analog_playback = {
5807         .substreams = 2,
5808         .channels_min = 2,
5809         .channels_max = 2,
5810         .nid = 0x8, /* NID to query formats and rates */
5811         .ops = {
5812                 .open = via_playback_pcm_open,
5813                 .prepare = via_playback_multi_pcm_prepare,
5814                 .cleanup = via_playback_multi_pcm_cleanup,
5815                 .close = via_pcm_open_close,
5816         },
5817 };
5818
5819 static const struct hda_pcm_stream vt1812_pcm_analog_capture = {
5820         .substreams = 2,
5821         .channels_min = 2,
5822         .channels_max = 2,
5823         .nid = 0x10, /* NID to query formats and rates */
5824         .ops = {
5825                 .open = via_pcm_open_close,
5826                 .prepare = via_capture_pcm_prepare,
5827                 .cleanup = via_capture_pcm_cleanup,
5828                 .close = via_pcm_open_close,
5829         },
5830 };
5831
5832 static const struct hda_pcm_stream vt1812_pcm_digital_playback = {
5833         .substreams = 1,
5834         .channels_min = 2,
5835         .channels_max = 2,
5836         /* NID is set in via_build_pcms */
5837         .ops = {
5838                 .open = via_dig_playback_pcm_open,
5839                 .close = via_dig_playback_pcm_close,
5840                 .prepare = via_dig_playback_pcm_prepare,
5841                 .cleanup = via_dig_playback_pcm_cleanup
5842         },
5843 };
5844 /* fill in the dac_nids table from the parsed pin configuration */
5845 static int vt1812_auto_fill_dac_nids(struct via_spec *spec,
5846                                      const struct auto_pin_cfg *cfg)
5847 {
5848         spec->multiout.num_dacs = 1;
5849         spec->multiout.dac_nids = spec->private_dac_nids;
5850         if (cfg->line_out_pins[0])
5851                 spec->private_dac_nids[0] = 0x8;
5852         return 0;
5853 }
5854
5855
5856 /* add playback controls from the parsed DAC table */
5857 static int vt1812_auto_create_multi_out_ctls(struct via_spec *spec,
5858                                              const struct auto_pin_cfg *cfg)
5859 {
5860         int err;
5861
5862         if (!cfg->line_out_pins[0])
5863                 return -1;
5864
5865         /* Line-Out: PortE */
5866         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5867                               "Front Playback Volume",
5868                               HDA_COMPOSE_AMP_VAL(0x8, 3, 0, HDA_OUTPUT));
5869         if (err < 0)
5870                 return err;
5871         err = via_add_control(spec, VIA_CTL_WIDGET_BIND_PIN_MUTE,
5872                               "Front Playback Switch",
5873                               HDA_COMPOSE_AMP_VAL(0x28, 3, 0, HDA_OUTPUT));
5874         if (err < 0)
5875                 return err;
5876
5877         return 0;
5878 }
5879
5880 static int vt1812_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
5881 {
5882         int err;
5883
5884         if (!pin)
5885                 return 0;
5886
5887         spec->multiout.hp_nid = 0x9;
5888         spec->hp_independent_mode_index = 1;
5889
5890
5891         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5892                               "Headphone Playback Volume",
5893                               HDA_COMPOSE_AMP_VAL(
5894                                       spec->multiout.hp_nid, 3, 0, HDA_OUTPUT));
5895         if (err < 0)
5896                 return err;
5897
5898         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
5899                               "Headphone Playback Switch",
5900                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
5901         if (err < 0)
5902                 return err;
5903
5904         create_hp_imux(spec);
5905         return 0;
5906 }
5907
5908 /* create playback/capture controls for input pins */
5909 static int vt1812_auto_create_analog_input_ctls(struct hda_codec *codec,
5910                                                 const struct auto_pin_cfg *cfg)
5911 {
5912         return vt_auto_create_analog_input_ctls(codec, cfg, 0x21);
5913 }
5914
5915 static int vt1812_parse_auto_config(struct hda_codec *codec)
5916 {
5917         struct via_spec *spec = codec->spec;
5918         int err;
5919
5920
5921         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
5922         if (err < 0)
5923                 return err;
5924         fill_dig_outs(codec);
5925         err = vt1812_auto_fill_dac_nids(spec, &spec->autocfg);
5926         if (err < 0)
5927                 return err;
5928
5929         if (!spec->autocfg.line_outs && !spec->autocfg.hp_outs)
5930                 return 0; /* can't find valid BIOS pin config */
5931
5932         err = vt1812_auto_create_multi_out_ctls(spec, &spec->autocfg);
5933         if (err < 0)
5934                 return err;
5935         err = vt1812_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
5936         if (err < 0)
5937                 return err;
5938         err = vt1812_auto_create_analog_input_ctls(codec, &spec->autocfg);
5939         if (err < 0)
5940                 return err;
5941
5942         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
5943
5944         fill_dig_outs(codec);
5945
5946         if (spec->kctls.list)
5947                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
5948
5949         spec->input_mux = &spec->private_imux[0];
5950
5951         if (spec->hp_mux)
5952                 via_hp_build(codec);
5953
5954         return 1;
5955 }
5956
5957 #ifdef CONFIG_SND_HDA_POWER_SAVE
5958 static const struct hda_amp_list vt1812_loopbacks[] = {
5959         { 0x21, HDA_INPUT, 0 },
5960         { 0x21, HDA_INPUT, 1 },
5961         { 0x21, HDA_INPUT, 2 },
5962         { } /* end */
5963 };
5964 #endif
5965
5966 static void set_widgets_power_state_vt1812(struct hda_codec *codec)
5967 {
5968         struct via_spec *spec = codec->spec;
5969         int imux_is_smixer =
5970         snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
5971         unsigned int parm;
5972         unsigned int present;
5973         /* MUX10 (1eh) = stereo mixer */
5974         imux_is_smixer =
5975         snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
5976         /* inputs */
5977         /* PW 5/6/7 (29h/2ah/2bh) */
5978         parm = AC_PWRST_D3;
5979         set_pin_power_state(codec, 0x29, &parm);
5980         set_pin_power_state(codec, 0x2a, &parm);
5981         set_pin_power_state(codec, 0x2b, &parm);
5982         parm = AC_PWRST_D0;
5983         /* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
5984         snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
5985         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
5986         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
5987         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
5988
5989         /* outputs */
5990         /* AOW0 (8h)*/
5991         snd_hda_codec_write(codec, 0x8, 0,
5992                             AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
5993
5994         /* PW4 (28h), MW4 (18h), MUX4(38h) */
5995         parm = AC_PWRST_D3;
5996         set_pin_power_state(codec, 0x28, &parm);
5997         snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
5998         snd_hda_codec_write(codec, 0x38, 0, AC_VERB_SET_POWER_STATE, parm);
5999
6000         /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
6001         parm = AC_PWRST_D3;
6002         set_pin_power_state(codec, 0x25, &parm);
6003         snd_hda_codec_write(codec, 0x15, 0, AC_VERB_SET_POWER_STATE, parm);
6004         snd_hda_codec_write(codec, 0x35, 0, AC_VERB_SET_POWER_STATE, parm);
6005         if (spec->hp_independent_mode)
6006                 snd_hda_codec_write(codec, 0x9, 0,
6007                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6008
6009         /* Internal Speaker */
6010         /* PW0 (24h), MW0(14h), MUX0(34h) */
6011         present = snd_hda_jack_detect(codec, 0x25);
6012
6013         parm = AC_PWRST_D3;
6014         set_pin_power_state(codec, 0x24, &parm);
6015         if (present) {
6016                 snd_hda_codec_write(codec, 0x14, 0,
6017                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6018                 snd_hda_codec_write(codec, 0x34, 0,
6019                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6020         } else {
6021                 snd_hda_codec_write(codec, 0x14, 0,
6022                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6023                 snd_hda_codec_write(codec, 0x34, 0,
6024                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6025         }
6026
6027
6028         /* Mono Out */
6029         /* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
6030         present = snd_hda_jack_detect(codec, 0x28);
6031
6032         parm = AC_PWRST_D3;
6033         set_pin_power_state(codec, 0x31, &parm);
6034         if (present) {
6035                 snd_hda_codec_write(codec, 0x1c, 0,
6036                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6037                 snd_hda_codec_write(codec, 0x3c, 0,
6038                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6039                 snd_hda_codec_write(codec, 0x3e, 0,
6040                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6041         } else {
6042                 snd_hda_codec_write(codec, 0x1c, 0,
6043                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6044                 snd_hda_codec_write(codec, 0x3c, 0,
6045                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6046                 snd_hda_codec_write(codec, 0x3e, 0,
6047                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6048         }
6049
6050         /* PW15 (33h), MW15 (1dh), MUX15(3dh) */
6051         parm = AC_PWRST_D3;
6052         set_pin_power_state(codec, 0x33, &parm);
6053         snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
6054         snd_hda_codec_write(codec, 0x3d, 0, AC_VERB_SET_POWER_STATE, parm);
6055
6056 }
6057
6058 /* patch for vt1812 */
6059 static int patch_vt1812(struct hda_codec *codec)
6060 {
6061         struct via_spec *spec;
6062         int err;
6063
6064         /* create a codec specific record */
6065         spec = via_new_spec(codec);
6066         if (spec == NULL)
6067                 return -ENOMEM;
6068
6069         /* automatic parse from the BIOS config */
6070         err = vt1812_parse_auto_config(codec);
6071         if (err < 0) {
6072                 via_free(codec);
6073                 return err;
6074         } else if (!err) {
6075                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
6076                        "from BIOS.  Using genenic mode...\n");
6077         }
6078
6079
6080         spec->init_verbs[spec->num_iverbs++]  = vt1812_volume_init_verbs;
6081         spec->init_verbs[spec->num_iverbs++] = vt1812_uniwill_init_verbs;
6082
6083         spec->stream_analog_playback = &vt1812_pcm_analog_playback;
6084         spec->stream_analog_capture = &vt1812_pcm_analog_capture;
6085
6086         spec->stream_digital_playback = &vt1812_pcm_digital_playback;
6087
6088         if (spec->adc_nids && spec->input_mux) {
6089                 override_mic_boost(codec, 0x2b, 0, 3, 40);
6090                 override_mic_boost(codec, 0x29, 0, 3, 40);
6091                 spec->mixers[spec->num_mixers] = vt1812_capture_mixer;
6092                 spec->num_mixers++;
6093         }
6094
6095         codec->patch_ops = via_patch_ops;
6096
6097         codec->patch_ops.init = via_auto_init;
6098         codec->patch_ops.unsol_event = via_unsol_event;
6099
6100 #ifdef CONFIG_SND_HDA_POWER_SAVE
6101         spec->loopback.amplist = vt1812_loopbacks;
6102 #endif
6103
6104         spec->set_widgets_power_state =  set_widgets_power_state_vt1812;
6105         return 0;
6106 }
6107
6108 /*
6109  * patch entries
6110  */
6111 static const struct hda_codec_preset snd_hda_preset_via[] = {
6112         { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
6113         { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
6114         { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
6115         { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
6116         { .id = 0x1106e710, .name = "VT1709 10-Ch",
6117           .patch = patch_vt1709_10ch},
6118         { .id = 0x1106e711, .name = "VT1709 10-Ch",
6119           .patch = patch_vt1709_10ch},
6120         { .id = 0x1106e712, .name = "VT1709 10-Ch",
6121           .patch = patch_vt1709_10ch},
6122         { .id = 0x1106e713, .name = "VT1709 10-Ch",
6123           .patch = patch_vt1709_10ch},
6124         { .id = 0x1106e714, .name = "VT1709 6-Ch",
6125           .patch = patch_vt1709_6ch},
6126         { .id = 0x1106e715, .name = "VT1709 6-Ch",
6127           .patch = patch_vt1709_6ch},
6128         { .id = 0x1106e716, .name = "VT1709 6-Ch",
6129           .patch = patch_vt1709_6ch},
6130         { .id = 0x1106e717, .name = "VT1709 6-Ch",
6131           .patch = patch_vt1709_6ch},
6132         { .id = 0x1106e720, .name = "VT1708B 8-Ch",
6133           .patch = patch_vt1708B_8ch},
6134         { .id = 0x1106e721, .name = "VT1708B 8-Ch",
6135           .patch = patch_vt1708B_8ch},
6136         { .id = 0x1106e722, .name = "VT1708B 8-Ch",
6137           .patch = patch_vt1708B_8ch},
6138         { .id = 0x1106e723, .name = "VT1708B 8-Ch",
6139           .patch = patch_vt1708B_8ch},
6140         { .id = 0x1106e724, .name = "VT1708B 4-Ch",
6141           .patch = patch_vt1708B_4ch},
6142         { .id = 0x1106e725, .name = "VT1708B 4-Ch",
6143           .patch = patch_vt1708B_4ch},
6144         { .id = 0x1106e726, .name = "VT1708B 4-Ch",
6145           .patch = patch_vt1708B_4ch},
6146         { .id = 0x1106e727, .name = "VT1708B 4-Ch",
6147           .patch = patch_vt1708B_4ch},
6148         { .id = 0x11060397, .name = "VT1708S",
6149           .patch = patch_vt1708S},
6150         { .id = 0x11061397, .name = "VT1708S",
6151           .patch = patch_vt1708S},
6152         { .id = 0x11062397, .name = "VT1708S",
6153           .patch = patch_vt1708S},
6154         { .id = 0x11063397, .name = "VT1708S",
6155           .patch = patch_vt1708S},
6156         { .id = 0x11064397, .name = "VT1705",
6157           .patch = patch_vt1708S},
6158         { .id = 0x11065397, .name = "VT1708S",
6159           .patch = patch_vt1708S},
6160         { .id = 0x11066397, .name = "VT1708S",
6161           .patch = patch_vt1708S},
6162         { .id = 0x11067397, .name = "VT1708S",
6163           .patch = patch_vt1708S},
6164         { .id = 0x11060398, .name = "VT1702",
6165           .patch = patch_vt1702},
6166         { .id = 0x11061398, .name = "VT1702",
6167           .patch = patch_vt1702},
6168         { .id = 0x11062398, .name = "VT1702",
6169           .patch = patch_vt1702},
6170         { .id = 0x11063398, .name = "VT1702",
6171           .patch = patch_vt1702},
6172         { .id = 0x11064398, .name = "VT1702",
6173           .patch = patch_vt1702},
6174         { .id = 0x11065398, .name = "VT1702",
6175           .patch = patch_vt1702},
6176         { .id = 0x11066398, .name = "VT1702",
6177           .patch = patch_vt1702},
6178         { .id = 0x11067398, .name = "VT1702",
6179           .patch = patch_vt1702},
6180         { .id = 0x11060428, .name = "VT1718S",
6181           .patch = patch_vt1718S},
6182         { .id = 0x11064428, .name = "VT1718S",
6183           .patch = patch_vt1718S},
6184         { .id = 0x11060441, .name = "VT2020",
6185           .patch = patch_vt1718S},
6186         { .id = 0x11064441, .name = "VT1828S",
6187           .patch = patch_vt1718S},
6188         { .id = 0x11060433, .name = "VT1716S",
6189           .patch = patch_vt1716S},
6190         { .id = 0x1106a721, .name = "VT1716S",
6191           .patch = patch_vt1716S},
6192         { .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
6193         { .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
6194         { .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
6195         { .id = 0x11060440, .name = "VT1818S",
6196           .patch = patch_vt1708S},
6197         { .id = 0x11060446, .name = "VT1802",
6198                 .patch = patch_vt2002P},
6199         { .id = 0x11068446, .name = "VT1802",
6200                 .patch = patch_vt2002P},
6201         {} /* terminator */
6202 };
6203
6204 MODULE_ALIAS("snd-hda-codec-id:1106*");
6205
6206 static struct hda_codec_preset_list via_list = {
6207         .preset = snd_hda_preset_via,
6208         .owner = THIS_MODULE,
6209 };
6210
6211 MODULE_LICENSE("GPL");
6212 MODULE_DESCRIPTION("VIA HD-audio codec");
6213
6214 static int __init patch_via_init(void)
6215 {
6216         return snd_hda_add_codec_preset(&via_list);
6217 }
6218
6219 static void __exit patch_via_exit(void)
6220 {
6221         snd_hda_delete_codec_preset(&via_list);
6222 }
6223
6224 module_init(patch_via_init)
6225 module_exit(patch_via_exit)