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