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