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