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