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