]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - sound/pci/hda/patch_via.c
ALSA: HDA patch_via.c: HP and CD pin connect config
[linux-2.6-omap-h63xx.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 VT1708 codec
5  *
6  * Copyright (c) 2006-2008 Lydia Wang <lydiawang@viatech.com>
7  *                         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 /*                                                                           */
34 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
35
36
37 #include <linux/init.h>
38 #include <linux/delay.h>
39 #include <linux/slab.h>
40 #include <sound/core.h>
41 #include "hda_codec.h"
42 #include "hda_local.h"
43 #include "hda_patch.h"
44
45 /* amp values */
46 #define AMP_VAL_IDX_SHIFT       19
47 #define AMP_VAL_IDX_MASK        (0x0f<<19)
48
49 #define NUM_CONTROL_ALLOC       32
50 #define NUM_VERB_ALLOC          32
51
52 /* Pin Widget NID */
53 #define VT1708_HP_NID           0x13
54 #define VT1708_DIGOUT_NID       0x14
55 #define VT1708_DIGIN_NID        0x16
56 #define VT1708_DIGIN_PIN        0x26
57 #define VT1708_HP_PIN_NID      0x20
58 #define VT1708_CD_PIN_NID      0x24
59
60 #define VT1709_HP_DAC_NID       0x28
61 #define VT1709_DIGOUT_NID       0x13
62 #define VT1709_DIGIN_NID        0x17
63 #define VT1709_DIGIN_PIN        0x25
64
65 #define VT1708B_HP_NID          0x25
66 #define VT1708B_DIGOUT_NID      0x12
67 #define VT1708B_DIGIN_NID       0x15
68 #define VT1708B_DIGIN_PIN       0x21
69
70 #define IS_VT1708_VENDORID(x)           ((x) >= 0x11061708 && (x) <= 0x1106170b)
71 #define IS_VT1709_10CH_VENDORID(x)      ((x) >= 0x1106e710 && (x) <= 0x1106e713)
72 #define IS_VT1709_6CH_VENDORID(x)       ((x) >= 0x1106e714 && (x) <= 0x1106e717)
73 #define IS_VT1708B_8CH_VENDORID(x)      ((x) >= 0x1106e720 && (x) <= 0x1106e723)
74 #define IS_VT1708B_4CH_VENDORID(x)      ((x) >= 0x1106e724 && (x) <= 0x1106e727)
75
76
77 enum {
78         VIA_CTL_WIDGET_VOL,
79         VIA_CTL_WIDGET_MUTE,
80 };
81
82 enum {
83         AUTO_SEQ_FRONT = 0,
84         AUTO_SEQ_SURROUND,
85         AUTO_SEQ_CENLFE,
86         AUTO_SEQ_SIDE
87 };
88
89 static struct snd_kcontrol_new vt1708_control_templates[] = {
90         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
91         HDA_CODEC_MUTE(NULL, 0, 0, 0),
92 };
93
94
95 struct via_spec {
96         /* codec parameterization */
97         struct snd_kcontrol_new *mixers[3];
98         unsigned int num_mixers;
99
100         struct hda_verb *init_verbs;
101
102         char *stream_name_analog;
103         struct hda_pcm_stream *stream_analog_playback;
104         struct hda_pcm_stream *stream_analog_capture;
105
106         char *stream_name_digital;
107         struct hda_pcm_stream *stream_digital_playback;
108         struct hda_pcm_stream *stream_digital_capture;
109
110         /* playback */
111         struct hda_multi_out multiout;
112
113         /* capture */
114         unsigned int num_adc_nids;
115         hda_nid_t *adc_nids;
116         hda_nid_t dig_in_nid;
117
118         /* capture source */
119         const struct hda_input_mux *input_mux;
120         unsigned int cur_mux[3];
121
122         /* PCM information */
123         struct hda_pcm pcm_rec[2];
124
125         /* dynamic controls, init_verbs and input_mux */
126         struct auto_pin_cfg autocfg;
127         unsigned int num_kctl_alloc, num_kctl_used;
128         struct snd_kcontrol_new *kctl_alloc;
129         struct hda_input_mux private_imux;
130         hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
131
132 #ifdef CONFIG_SND_HDA_POWER_SAVE
133         struct hda_loopback_check loopback;
134 #endif
135 };
136
137 static hda_nid_t vt1708_adc_nids[2] = {
138         /* ADC1-2 */
139         0x15, 0x27
140 };
141
142 static hda_nid_t vt1709_adc_nids[3] = {
143         /* ADC1-2 */
144         0x14, 0x15, 0x16
145 };
146
147 static hda_nid_t vt1708B_adc_nids[2] = {
148         /* ADC1-2 */
149         0x13, 0x14
150 };
151
152 /* add dynamic controls */
153 static int via_add_control(struct via_spec *spec, int type, const char *name,
154                            unsigned long val)
155 {
156         struct snd_kcontrol_new *knew;
157
158         if (spec->num_kctl_used >= spec->num_kctl_alloc) {
159                 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
160
161                 /* array + terminator */
162                 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL);
163                 if (!knew)
164                         return -ENOMEM;
165                 if (spec->kctl_alloc) {
166                         memcpy(knew, spec->kctl_alloc,
167                                sizeof(*knew) * spec->num_kctl_alloc);
168                         kfree(spec->kctl_alloc);
169                 }
170                 spec->kctl_alloc = knew;
171                 spec->num_kctl_alloc = num;
172         }
173
174         knew = &spec->kctl_alloc[spec->num_kctl_used];
175         *knew = vt1708_control_templates[type];
176         knew->name = kstrdup(name, GFP_KERNEL);
177
178         if (!knew->name)
179                 return -ENOMEM;
180         knew->private_value = val;
181         spec->num_kctl_used++;
182         return 0;
183 }
184
185 /* create input playback/capture controls for the given pin */
186 static int via_new_analog_input(struct via_spec *spec, hda_nid_t pin,
187                                 const char *ctlname, int idx, int mix_nid)
188 {
189         char name[32];
190         int err;
191
192         sprintf(name, "%s Playback Volume", ctlname);
193         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
194                               HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
195         if (err < 0)
196                 return err;
197         sprintf(name, "%s Playback Switch", ctlname);
198         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
199                               HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
200         if (err < 0)
201                 return err;
202         return 0;
203 }
204
205 static void via_auto_set_output_and_unmute(struct hda_codec *codec,
206                                            hda_nid_t nid, int pin_type,
207                                            int dac_idx)
208 {
209         /* set as output */
210         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
211                             pin_type);
212         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
213                             AMP_OUT_UNMUTE);
214 }
215
216
217 static void via_auto_init_multi_out(struct hda_codec *codec)
218 {
219         struct via_spec *spec = codec->spec;
220         int i;
221
222         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
223                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
224                 if (nid)
225                         via_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
226         }
227 }
228
229 static void via_auto_init_hp_out(struct hda_codec *codec)
230 {
231         struct via_spec *spec = codec->spec;
232         hda_nid_t pin;
233
234         pin = spec->autocfg.hp_pins[0];
235         if (pin) /* connect to front */
236                 via_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
237 }
238
239 static void via_auto_init_analog_input(struct hda_codec *codec)
240 {
241         struct via_spec *spec = codec->spec;
242         int i;
243
244         for (i = 0; i < AUTO_PIN_LAST; i++) {
245                 hda_nid_t nid = spec->autocfg.input_pins[i];
246
247                 snd_hda_codec_write(codec, nid, 0,
248                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
249                                     (i <= AUTO_PIN_FRONT_MIC ?
250                                      PIN_VREF50 : PIN_IN));
251
252         }
253 }
254 /*
255  * input MUX handling
256  */
257 static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
258                              struct snd_ctl_elem_info *uinfo)
259 {
260         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
261         struct via_spec *spec = codec->spec;
262         return snd_hda_input_mux_info(spec->input_mux, uinfo);
263 }
264
265 static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
266                             struct snd_ctl_elem_value *ucontrol)
267 {
268         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
269         struct via_spec *spec = codec->spec;
270         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
271
272         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
273         return 0;
274 }
275
276 static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
277                             struct snd_ctl_elem_value *ucontrol)
278 {
279         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
280         struct via_spec *spec = codec->spec;
281         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
282         unsigned int vendor_id = codec->vendor_id;
283
284         /* AIW0  lydia 060801 add for correct sw0 input select */
285         if (IS_VT1708_VENDORID(vendor_id) && (adc_idx == 0))
286                 return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
287                                              0x18, &spec->cur_mux[adc_idx]);
288         else if ((IS_VT1709_10CH_VENDORID(vendor_id) ||
289                   IS_VT1709_6CH_VENDORID(vendor_id)) && (adc_idx == 0))
290                 return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
291                                              0x19, &spec->cur_mux[adc_idx]);
292         else if ((IS_VT1708B_8CH_VENDORID(vendor_id) ||
293                   IS_VT1708B_4CH_VENDORID(vendor_id)) && (adc_idx == 0))
294                 return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
295                                              0x17, &spec->cur_mux[adc_idx]);
296         else
297                 return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
298                                              spec->adc_nids[adc_idx],
299                                              &spec->cur_mux[adc_idx]);
300 }
301
302 /* capture mixer elements */
303 static struct snd_kcontrol_new vt1708_capture_mixer[] = {
304         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_INPUT),
305         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_INPUT),
306         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x27, 0x0, HDA_INPUT),
307         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x27, 0x0, HDA_INPUT),
308         {
309                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
310                 /* The multiple "Capture Source" controls confuse alsamixer
311                  * So call somewhat different..
312                  */
313                 /* .name = "Capture Source", */
314                 .name = "Input Source",
315                 .count = 1,
316                 .info = via_mux_enum_info,
317                 .get = via_mux_enum_get,
318                 .put = via_mux_enum_put,
319         },
320         { } /* end */
321 };
322 /*
323  * generic initialization of ADC, input mixers and output mixers
324  */
325 static struct hda_verb vt1708_volume_init_verbs[] = {
326         /*
327          * Unmute ADC0-1 and set the default input to mic-in
328          */
329         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
330         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
331
332
333         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
334          * mixer widget
335          */
336         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
337         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
338         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
339         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
340         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
341         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
342
343         /*
344          * Set up output mixers (0x19 - 0x1b)
345          */
346         /* set vol=0 to output mixers */
347         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
348         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
349         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
350         
351         /* Setup default input to PW4 */
352         {0x20, AC_VERB_SET_CONNECT_SEL, 0x1},
353         /* PW9 Output enable */
354         {0x25, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
355         { }
356 };
357
358 static int via_playback_pcm_open(struct hda_pcm_stream *hinfo,
359                                  struct hda_codec *codec,
360                                  struct snd_pcm_substream *substream)
361 {
362         struct via_spec *spec = codec->spec;
363         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
364                                              hinfo);
365 }
366
367 static int via_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
368                                     struct hda_codec *codec,
369                                     unsigned int stream_tag,
370                                     unsigned int format,
371                                     struct snd_pcm_substream *substream)
372 {
373         struct via_spec *spec = codec->spec;
374         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
375                                                 stream_tag, format, substream);
376 }
377
378 static int via_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
379                                     struct hda_codec *codec,
380                                     struct snd_pcm_substream *substream)
381 {
382         struct via_spec *spec = codec->spec;
383         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
384 }
385
386 /*
387  * Digital out
388  */
389 static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
390                                      struct hda_codec *codec,
391                                      struct snd_pcm_substream *substream)
392 {
393         struct via_spec *spec = codec->spec;
394         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
395 }
396
397 static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
398                                       struct hda_codec *codec,
399                                       struct snd_pcm_substream *substream)
400 {
401         struct via_spec *spec = codec->spec;
402         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
403 }
404
405 static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
406                                         struct hda_codec *codec,
407                                         unsigned int stream_tag,
408                                         unsigned int format,
409                                         struct snd_pcm_substream *substream)
410 {
411         struct via_spec *spec = codec->spec;
412         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
413                                              stream_tag, format, substream);
414 }
415
416 /*
417  * Analog capture
418  */
419 static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
420                                    struct hda_codec *codec,
421                                    unsigned int stream_tag,
422                                    unsigned int format,
423                                    struct snd_pcm_substream *substream)
424 {
425         struct via_spec *spec = codec->spec;
426
427         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
428                                    stream_tag, 0, format);
429         return 0;
430 }
431
432 static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
433                                    struct hda_codec *codec,
434                                    struct snd_pcm_substream *substream)
435 {
436         struct via_spec *spec = codec->spec;
437         snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
438         return 0;
439 }
440
441 static struct hda_pcm_stream vt1708_pcm_analog_playback = {
442         .substreams = 1,
443         .channels_min = 2,
444         .channels_max = 8,
445         .nid = 0x10, /* NID to query formats and rates */
446         .ops = {
447                 .open = via_playback_pcm_open,
448                 .prepare = via_playback_pcm_prepare,
449                 .cleanup = via_playback_pcm_cleanup
450         },
451 };
452
453 static struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
454         .substreams = 1,
455         .channels_min = 2,
456         .channels_max = 8,
457         .nid = 0x10, /* NID to query formats and rates */
458         /* We got noisy outputs on the right channel on VT1708 when
459          * 24bit samples are used.  Until any workaround is found,
460          * disable the 24bit format, so far.
461          */
462         .formats = SNDRV_PCM_FMTBIT_S16_LE,
463         .ops = {
464                 .open = via_playback_pcm_open,
465                 .prepare = via_playback_pcm_prepare,
466                 .cleanup = via_playback_pcm_cleanup
467         },
468 };
469
470 static struct hda_pcm_stream vt1708_pcm_analog_capture = {
471         .substreams = 2,
472         .channels_min = 2,
473         .channels_max = 2,
474         .nid = 0x15, /* NID to query formats and rates */
475         .ops = {
476                 .prepare = via_capture_pcm_prepare,
477                 .cleanup = via_capture_pcm_cleanup
478         },
479 };
480
481 static struct hda_pcm_stream vt1708_pcm_digital_playback = {
482         .substreams = 1,
483         .channels_min = 2,
484         .channels_max = 2,
485         /* NID is set in via_build_pcms */
486         .ops = {
487                 .open = via_dig_playback_pcm_open,
488                 .close = via_dig_playback_pcm_close,
489                 .prepare = via_dig_playback_pcm_prepare
490         },
491 };
492
493 static struct hda_pcm_stream vt1708_pcm_digital_capture = {
494         .substreams = 1,
495         .channels_min = 2,
496         .channels_max = 2,
497 };
498
499 static int via_build_controls(struct hda_codec *codec)
500 {
501         struct via_spec *spec = codec->spec;
502         int err;
503         int i;
504
505         for (i = 0; i < spec->num_mixers; i++) {
506                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
507                 if (err < 0)
508                         return err;
509         }
510
511         if (spec->multiout.dig_out_nid) {
512                 err = snd_hda_create_spdif_out_ctls(codec,
513                                                     spec->multiout.dig_out_nid);
514                 if (err < 0)
515                         return err;
516                 err = snd_hda_create_spdif_share_sw(codec,
517                                                     &spec->multiout);
518                 if (err < 0)
519                         return err;
520                 spec->multiout.share_spdif = 1;
521         }
522         if (spec->dig_in_nid) {
523                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
524                 if (err < 0)
525                         return err;
526         }
527         return 0;
528 }
529
530 static int via_build_pcms(struct hda_codec *codec)
531 {
532         struct via_spec *spec = codec->spec;
533         struct hda_pcm *info = spec->pcm_rec;
534
535         codec->num_pcms = 1;
536         codec->pcm_info = info;
537
538         info->name = spec->stream_name_analog;
539         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *(spec->stream_analog_playback);
540         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
541         info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_analog_capture);
542         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
543
544         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
545                 spec->multiout.max_channels;
546
547         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
548                 codec->num_pcms++;
549                 info++;
550                 info->name = spec->stream_name_digital;
551                 info->pcm_type = HDA_PCM_TYPE_SPDIF;
552                 if (spec->multiout.dig_out_nid) {
553                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
554                                 *(spec->stream_digital_playback);
555                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
556                                 spec->multiout.dig_out_nid;
557                 }
558                 if (spec->dig_in_nid) {
559                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
560                                 *(spec->stream_digital_capture);
561                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
562                                 spec->dig_in_nid;
563                 }
564         }
565
566         return 0;
567 }
568
569 static void via_free(struct hda_codec *codec)
570 {
571         struct via_spec *spec = codec->spec;
572         unsigned int i;
573
574         if (!spec)
575                 return;
576
577         if (spec->kctl_alloc) {
578                 for (i = 0; i < spec->num_kctl_used; i++)
579                         kfree(spec->kctl_alloc[i].name);
580                 kfree(spec->kctl_alloc);
581         }
582
583         kfree(codec->spec);
584 }
585
586 static int via_init(struct hda_codec *codec)
587 {
588         struct via_spec *spec = codec->spec;
589         snd_hda_sequence_write(codec, spec->init_verbs);
590         /* Lydia Add for EAPD enable */
591         if (!spec->dig_in_nid) { /* No Digital In connection */
592                 if (IS_VT1708_VENDORID(codec->vendor_id)) {
593                         snd_hda_codec_write(codec, VT1708_DIGIN_PIN, 0,
594                                             AC_VERB_SET_PIN_WIDGET_CONTROL,
595                                             PIN_OUT);
596                         snd_hda_codec_write(codec, VT1708_DIGIN_PIN, 0,
597                                             AC_VERB_SET_EAPD_BTLENABLE, 0x02);
598                 } else if (IS_VT1709_10CH_VENDORID(codec->vendor_id) ||
599                            IS_VT1709_6CH_VENDORID(codec->vendor_id)) {
600                         snd_hda_codec_write(codec, VT1709_DIGIN_PIN, 0,
601                                             AC_VERB_SET_PIN_WIDGET_CONTROL,
602                                             PIN_OUT);
603                         snd_hda_codec_write(codec, VT1709_DIGIN_PIN, 0,
604                                             AC_VERB_SET_EAPD_BTLENABLE, 0x02);
605                 } else if (IS_VT1708B_8CH_VENDORID(codec->vendor_id) ||
606                            IS_VT1708B_4CH_VENDORID(codec->vendor_id)) {
607                         snd_hda_codec_write(codec, VT1708B_DIGIN_PIN, 0,
608                                             AC_VERB_SET_PIN_WIDGET_CONTROL,
609                                             PIN_OUT);
610                         snd_hda_codec_write(codec, VT1708B_DIGIN_PIN, 0,
611                                             AC_VERB_SET_EAPD_BTLENABLE, 0x02);
612                 }
613         } else /* enable SPDIF-input pin */
614                 snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
615                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
616
617         return 0;
618 }
619
620 #ifdef CONFIG_SND_HDA_POWER_SAVE
621 static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
622 {
623         struct via_spec *spec = codec->spec;
624         return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
625 }
626 #endif
627
628 /*
629  */
630 static struct hda_codec_ops via_patch_ops = {
631         .build_controls = via_build_controls,
632         .build_pcms = via_build_pcms,
633         .init = via_init,
634         .free = via_free,
635 #ifdef CONFIG_SND_HDA_POWER_SAVE
636         .check_power_status = via_check_power_status,
637 #endif
638 };
639
640 /* fill in the dac_nids table from the parsed pin configuration */
641 static int vt1708_auto_fill_dac_nids(struct via_spec *spec,
642                                      const struct auto_pin_cfg *cfg)
643 {
644         int i;
645         hda_nid_t nid;
646
647         spec->multiout.num_dacs = cfg->line_outs;
648
649         spec->multiout.dac_nids = spec->private_dac_nids;
650         
651         for(i = 0; i < 4; i++) {
652                 nid = cfg->line_out_pins[i];
653                 if (nid) {
654                         /* config dac list */
655                         switch (i) {
656                         case AUTO_SEQ_FRONT:
657                                 spec->multiout.dac_nids[i] = 0x10;
658                                 break;
659                         case AUTO_SEQ_CENLFE:
660                                 spec->multiout.dac_nids[i] = 0x12;
661                                 break;
662                         case AUTO_SEQ_SURROUND:
663                                 spec->multiout.dac_nids[i] = 0x13;
664                                 break;
665                         case AUTO_SEQ_SIDE:
666                                 spec->multiout.dac_nids[i] = 0x11;
667                                 break;
668                         }
669                 }
670         }
671
672         return 0;
673 }
674
675 /* add playback controls from the parsed DAC table */
676 static int vt1708_auto_create_multi_out_ctls(struct via_spec *spec,
677                                              const struct auto_pin_cfg *cfg)
678 {
679         char name[32];
680         static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
681         hda_nid_t nid, nid_vol = 0;
682         int i, err;
683
684         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
685                 nid = cfg->line_out_pins[i];
686
687                 if (!nid)
688                         continue;
689                 
690                 if (i != AUTO_SEQ_FRONT)
691                         nid_vol = 0x1b - i + 1;
692
693                 if (i == AUTO_SEQ_CENLFE) {
694                         /* Center/LFE */
695                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
696                                         "Center Playback Volume",
697                                         HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
698                                                             HDA_OUTPUT));
699                         if (err < 0)
700                                 return err;
701                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
702                                               "LFE Playback Volume",
703                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
704                                                                   HDA_OUTPUT));
705                         if (err < 0)
706                                 return err;
707                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
708                                               "Center Playback Switch",
709                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
710                                                                   HDA_OUTPUT));
711                         if (err < 0)
712                                 return err;
713                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
714                                               "LFE Playback Switch",
715                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
716                                                                   HDA_OUTPUT));
717                         if (err < 0)
718                                 return err;
719                 } else if (i == AUTO_SEQ_FRONT){
720                         /* add control to mixer index 0 */
721                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
722                                               "Master Front Playback Volume",
723                                               HDA_COMPOSE_AMP_VAL(0x17, 3, 0,
724                                                                   HDA_INPUT));
725                         if (err < 0)
726                                 return err;
727                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
728                                               "Master Front Playback Switch",
729                                               HDA_COMPOSE_AMP_VAL(0x17, 3, 0,
730                                                                   HDA_INPUT));
731                         if (err < 0)
732                                 return err;
733                         
734                         /* add control to PW3 */
735                         sprintf(name, "%s Playback Volume", chname[i]);
736                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
737                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
738                                                                   HDA_OUTPUT));
739                         if (err < 0)
740                                 return err;
741                         sprintf(name, "%s Playback Switch", chname[i]);
742                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
743                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
744                                                                   HDA_OUTPUT));
745                         if (err < 0)
746                                 return err;
747                 } else {
748                         sprintf(name, "%s Playback Volume", chname[i]);
749                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
750                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
751                                                                   HDA_OUTPUT));
752                         if (err < 0)
753                                 return err;
754                         sprintf(name, "%s Playback Switch", chname[i]);
755                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
756                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
757                                                                   HDA_OUTPUT));
758                         if (err < 0)
759                                 return err;
760                 }
761         }
762
763         return 0;
764 }
765
766 static int vt1708_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
767 {
768         int err;
769
770         if (!pin)
771                 return 0;
772
773         spec->multiout.hp_nid = VT1708_HP_NID; /* AOW3 */
774
775         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
776                               "Headphone Playback Volume",
777                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
778         if (err < 0)
779                 return err;
780         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
781                               "Headphone Playback Switch",
782                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
783         if (err < 0)
784                 return err;
785
786         return 0;
787 }
788
789 /* create playback/capture controls for input pins */
790 static int vt1708_auto_create_analog_input_ctls(struct via_spec *spec,
791                                                 const struct auto_pin_cfg *cfg)
792 {
793         static char *labels[] = {
794                 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
795         };
796         struct hda_input_mux *imux = &spec->private_imux;
797         int i, err, idx = 0;
798
799         /* for internal loopback recording select */
800         imux->items[imux->num_items].label = "Stereo Mixer";
801         imux->items[imux->num_items].index = idx;
802         imux->num_items++;
803
804         for (i = 0; i < AUTO_PIN_LAST; i++) {
805                 if (!cfg->input_pins[i])
806                         continue;
807
808                 switch (cfg->input_pins[i]) {
809                 case 0x1d: /* Mic */
810                         idx = 2;
811                         break;
812                                 
813                 case 0x1e: /* Line In */
814                         idx = 3;
815                         break;
816
817                 case 0x21: /* Front Mic */
818                         idx = 4;
819                         break;
820
821                 case 0x24: /* CD */
822                         idx = 1;
823                         break;
824                 }
825                 err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
826                                            idx, 0x17);
827                 if (err < 0)
828                         return err;
829                 imux->items[imux->num_items].label = labels[i];
830                 imux->items[imux->num_items].index = idx;
831                 imux->num_items++;
832         }
833         return 0;
834 }
835
836 #ifdef CONFIG_SND_HDA_POWER_SAVE
837 static struct hda_amp_list vt1708_loopbacks[] = {
838         { 0x17, HDA_INPUT, 1 },
839         { 0x17, HDA_INPUT, 2 },
840         { 0x17, HDA_INPUT, 3 },
841         { 0x17, HDA_INPUT, 4 },
842         { } /* end */
843 };
844 #endif
845
846 static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
847 {
848         unsigned int def_conf;
849         unsigned char seqassoc;
850
851         def_conf = snd_hda_codec_read(codec, nid, 0,
852                                       AC_VERB_GET_CONFIG_DEFAULT, 0);
853         seqassoc = (unsigned char) get_defcfg_association(def_conf);
854         seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
855         if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE) {
856                 if (seqassoc == 0xff) {
857                         def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
858                         snd_hda_codec_write(codec, nid, 0,
859                                             AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
860                                             def_conf >> 24);
861                 }
862         }
863
864         return;
865 }
866
867 static int vt1708_parse_auto_config(struct hda_codec *codec)
868 {
869         struct via_spec *spec = codec->spec;
870         int err;
871
872         /* Add HP and CD pin config connect bit re-config action */
873         vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
874         vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
875
876         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
877         if (err < 0)
878                 return err;
879         err = vt1708_auto_fill_dac_nids(spec, &spec->autocfg);
880         if (err < 0)
881                 return err;
882         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
883                 return 0; /* can't find valid BIOS pin config */
884
885         err = vt1708_auto_create_multi_out_ctls(spec, &spec->autocfg);
886         if (err < 0)
887                 return err;
888         err = vt1708_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
889         if (err < 0)
890                 return err;
891         err = vt1708_auto_create_analog_input_ctls(spec, &spec->autocfg);
892         if (err < 0)
893                 return err;
894
895         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
896
897         if (spec->autocfg.dig_out_pin)
898                 spec->multiout.dig_out_nid = VT1708_DIGOUT_NID;
899         if (spec->autocfg.dig_in_pin)
900                 spec->dig_in_nid = VT1708_DIGIN_NID;
901
902         if (spec->kctl_alloc)
903                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
904
905         spec->init_verbs = vt1708_volume_init_verbs;    
906
907         spec->input_mux = &spec->private_imux;
908
909         return 1;
910 }
911
912 /* init callback for auto-configuration model -- overriding the default init */
913 static int via_auto_init(struct hda_codec *codec)
914 {
915         via_init(codec);
916         via_auto_init_multi_out(codec);
917         via_auto_init_hp_out(codec);
918         via_auto_init_analog_input(codec);
919         return 0;
920 }
921
922 static int patch_vt1708(struct hda_codec *codec)
923 {
924         struct via_spec *spec;
925         int err;
926
927         /* create a codec specific record */
928         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
929         if (spec == NULL)
930                 return -ENOMEM;
931
932         codec->spec = spec;
933
934         /* automatic parse from the BIOS config */
935         err = vt1708_parse_auto_config(codec);
936         if (err < 0) {
937                 via_free(codec);
938                 return err;
939         } else if (!err) {
940                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
941                        "from BIOS.  Using genenic mode...\n");
942         }
943
944         
945         spec->stream_name_analog = "VT1708 Analog";
946         spec->stream_analog_playback = &vt1708_pcm_analog_playback;
947         /* disable 32bit format on VT1708 */
948         if (codec->vendor_id == 0x11061708)
949                 spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
950         spec->stream_analog_capture = &vt1708_pcm_analog_capture;
951
952         spec->stream_name_digital = "VT1708 Digital";
953         spec->stream_digital_playback = &vt1708_pcm_digital_playback;
954         spec->stream_digital_capture = &vt1708_pcm_digital_capture;
955
956         
957         if (!spec->adc_nids && spec->input_mux) {
958                 spec->adc_nids = vt1708_adc_nids;
959                 spec->num_adc_nids = ARRAY_SIZE(vt1708_adc_nids);
960                 spec->mixers[spec->num_mixers] = vt1708_capture_mixer;
961                 spec->num_mixers++;
962         }
963
964         codec->patch_ops = via_patch_ops;
965
966         codec->patch_ops.init = via_auto_init;
967 #ifdef CONFIG_SND_HDA_POWER_SAVE
968         spec->loopback.amplist = vt1708_loopbacks;
969 #endif
970
971         return 0;
972 }
973
974 /* capture mixer elements */
975 static struct snd_kcontrol_new vt1709_capture_mixer[] = {
976         HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x0, HDA_INPUT),
977         HDA_CODEC_MUTE("Capture Switch", 0x14, 0x0, HDA_INPUT),
978         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x15, 0x0, HDA_INPUT),
979         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x15, 0x0, HDA_INPUT),
980         HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x16, 0x0, HDA_INPUT),
981         HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x16, 0x0, HDA_INPUT),
982         {
983                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
984                 /* The multiple "Capture Source" controls confuse alsamixer
985                  * So call somewhat different..
986                  */
987                 /* .name = "Capture Source", */
988                 .name = "Input Source",
989                 .count = 1,
990                 .info = via_mux_enum_info,
991                 .get = via_mux_enum_get,
992                 .put = via_mux_enum_put,
993         },
994         { } /* end */
995 };
996
997 /*
998  * generic initialization of ADC, input mixers and output mixers
999  */
1000 static struct hda_verb vt1709_10ch_volume_init_verbs[] = {
1001         /*
1002          * Unmute ADC0-2 and set the default input to mic-in
1003          */
1004         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1005         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1006         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1007
1008
1009         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
1010          * mixer widget
1011          */
1012         /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
1013         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1014         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1015         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
1016         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
1017         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
1018
1019         /*
1020          * Set up output selector (0x1a, 0x1b, 0x29)
1021          */
1022         /* set vol=0 to output mixers */
1023         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1024         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1025         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1026
1027         /*
1028          *  Unmute PW3 and PW4
1029          */
1030         {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1031         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1032
1033         /* Set input of PW4 as AOW4 */
1034         {0x20, AC_VERB_SET_CONNECT_SEL, 0x1},
1035         /* PW9 Output enable */
1036         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
1037         { }
1038 };
1039
1040 static struct hda_pcm_stream vt1709_10ch_pcm_analog_playback = {
1041         .substreams = 1,
1042         .channels_min = 2,
1043         .channels_max = 10,
1044         .nid = 0x10, /* NID to query formats and rates */
1045         .ops = {
1046                 .open = via_playback_pcm_open,
1047                 .prepare = via_playback_pcm_prepare,
1048                 .cleanup = via_playback_pcm_cleanup
1049         },
1050 };
1051
1052 static struct hda_pcm_stream vt1709_6ch_pcm_analog_playback = {
1053         .substreams = 1,
1054         .channels_min = 2,
1055         .channels_max = 6,
1056         .nid = 0x10, /* NID to query formats and rates */
1057         .ops = {
1058                 .open = via_playback_pcm_open,
1059                 .prepare = via_playback_pcm_prepare,
1060                 .cleanup = via_playback_pcm_cleanup
1061         },
1062 };
1063
1064 static struct hda_pcm_stream vt1709_pcm_analog_capture = {
1065         .substreams = 2,
1066         .channels_min = 2,
1067         .channels_max = 2,
1068         .nid = 0x14, /* NID to query formats and rates */
1069         .ops = {
1070                 .prepare = via_capture_pcm_prepare,
1071                 .cleanup = via_capture_pcm_cleanup
1072         },
1073 };
1074
1075 static struct hda_pcm_stream vt1709_pcm_digital_playback = {
1076         .substreams = 1,
1077         .channels_min = 2,
1078         .channels_max = 2,
1079         /* NID is set in via_build_pcms */
1080         .ops = {
1081                 .open = via_dig_playback_pcm_open,
1082                 .close = via_dig_playback_pcm_close
1083         },
1084 };
1085
1086 static struct hda_pcm_stream vt1709_pcm_digital_capture = {
1087         .substreams = 1,
1088         .channels_min = 2,
1089         .channels_max = 2,
1090 };
1091
1092 static int vt1709_auto_fill_dac_nids(struct via_spec *spec,
1093                                      const struct auto_pin_cfg *cfg)
1094 {
1095         int i;
1096         hda_nid_t nid;
1097
1098         if (cfg->line_outs == 4)  /* 10 channels */
1099                 spec->multiout.num_dacs = cfg->line_outs+1; /* AOW0~AOW4 */
1100         else if (cfg->line_outs == 3) /* 6 channels */
1101                 spec->multiout.num_dacs = cfg->line_outs; /* AOW0~AOW2 */
1102
1103         spec->multiout.dac_nids = spec->private_dac_nids;
1104
1105         if (cfg->line_outs == 4) { /* 10 channels */
1106                 for (i = 0; i < cfg->line_outs; i++) {
1107                         nid = cfg->line_out_pins[i];
1108                         if (nid) {
1109                                 /* config dac list */
1110                                 switch (i) {
1111                                 case AUTO_SEQ_FRONT:
1112                                         /* AOW0 */
1113                                         spec->multiout.dac_nids[i] = 0x10;
1114                                         break;
1115                                 case AUTO_SEQ_CENLFE:
1116                                         /* AOW2 */
1117                                         spec->multiout.dac_nids[i] = 0x12;
1118                                         break;
1119                                 case AUTO_SEQ_SURROUND:
1120                                         /* AOW3 */
1121                                         spec->multiout.dac_nids[i] = 0x27;
1122                                         break;
1123                                 case AUTO_SEQ_SIDE:
1124                                         /* AOW1 */
1125                                         spec->multiout.dac_nids[i] = 0x11;
1126                                         break;
1127                                 default:
1128                                         break;
1129                                 }
1130                         }
1131                 }
1132                 spec->multiout.dac_nids[cfg->line_outs] = 0x28; /* AOW4 */
1133
1134         } else if (cfg->line_outs == 3) { /* 6 channels */
1135                 for(i = 0; i < cfg->line_outs; i++) {
1136                         nid = cfg->line_out_pins[i];
1137                         if (nid) {
1138                                 /* config dac list */
1139                                 switch(i) {
1140                                 case AUTO_SEQ_FRONT:
1141                                         /* AOW0 */
1142                                         spec->multiout.dac_nids[i] = 0x10;
1143                                         break;
1144                                 case AUTO_SEQ_CENLFE:
1145                                         /* AOW2 */
1146                                         spec->multiout.dac_nids[i] = 0x12;
1147                                         break;
1148                                 case AUTO_SEQ_SURROUND:
1149                                         /* AOW1 */
1150                                         spec->multiout.dac_nids[i] = 0x11;
1151                                         break;
1152                                 default:
1153                                         break;
1154                                 }
1155                         }
1156                 }
1157         }
1158
1159         return 0;
1160 }
1161
1162 /* add playback controls from the parsed DAC table */
1163 static int vt1709_auto_create_multi_out_ctls(struct via_spec *spec,
1164                                              const struct auto_pin_cfg *cfg)
1165 {
1166         char name[32];
1167         static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
1168         hda_nid_t nid = 0;
1169         int i, err;
1170
1171         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
1172                 nid = cfg->line_out_pins[i];
1173
1174                 if (!nid)       
1175                         continue;
1176
1177                 if (i == AUTO_SEQ_CENLFE) {
1178                         /* Center/LFE */
1179                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1180                                               "Center Playback Volume",
1181                                               HDA_COMPOSE_AMP_VAL(0x1b, 1, 0,
1182                                                                   HDA_OUTPUT));
1183                         if (err < 0)
1184                                 return err;
1185                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1186                                               "LFE Playback Volume",
1187                                               HDA_COMPOSE_AMP_VAL(0x1b, 2, 0,
1188                                                                   HDA_OUTPUT));
1189                         if (err < 0)
1190                                 return err;
1191                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1192                                               "Center Playback Switch",
1193                                               HDA_COMPOSE_AMP_VAL(0x1b, 1, 0,
1194                                                                   HDA_OUTPUT));
1195                         if (err < 0)
1196                                 return err;
1197                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1198                                               "LFE Playback Switch",
1199                                               HDA_COMPOSE_AMP_VAL(0x1b, 2, 0,
1200                                                                   HDA_OUTPUT));
1201                         if (err < 0)
1202                                 return err;
1203                 } else if (i == AUTO_SEQ_FRONT){
1204                         /* add control to mixer index 0 */
1205                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1206                                               "Master Front Playback Volume",
1207                                               HDA_COMPOSE_AMP_VAL(0x18, 3, 0,
1208                                                                   HDA_INPUT));
1209                         if (err < 0)
1210                                 return err;
1211                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1212                                               "Master Front Playback Switch",
1213                                               HDA_COMPOSE_AMP_VAL(0x18, 3, 0,
1214                                                                   HDA_INPUT));
1215                         if (err < 0)
1216                                 return err;
1217                         
1218                         /* add control to PW3 */
1219                         sprintf(name, "%s Playback Volume", chname[i]);
1220                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1221                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1222                                                                   HDA_OUTPUT));
1223                         if (err < 0)
1224                                 return err;
1225                         sprintf(name, "%s Playback Switch", chname[i]);
1226                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1227                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1228                                                                   HDA_OUTPUT));
1229                         if (err < 0)
1230                                 return err;
1231                 } else if (i == AUTO_SEQ_SURROUND) {
1232                         sprintf(name, "%s Playback Volume", chname[i]);
1233                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1234                                               HDA_COMPOSE_AMP_VAL(0x29, 3, 0,
1235                                                                   HDA_OUTPUT));
1236                         if (err < 0)
1237                                 return err;
1238                         sprintf(name, "%s Playback Switch", chname[i]);
1239                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1240                                               HDA_COMPOSE_AMP_VAL(0x29, 3, 0,
1241                                                                   HDA_OUTPUT));
1242                         if (err < 0)
1243                                 return err;
1244                 } else if (i == AUTO_SEQ_SIDE) {
1245                         sprintf(name, "%s Playback Volume", chname[i]);
1246                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1247                                               HDA_COMPOSE_AMP_VAL(0x1a, 3, 0,
1248                                                                   HDA_OUTPUT));
1249                         if (err < 0)
1250                                 return err;
1251                         sprintf(name, "%s Playback Switch", chname[i]);
1252                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1253                                               HDA_COMPOSE_AMP_VAL(0x1a, 3, 0,
1254                                                                   HDA_OUTPUT));
1255                         if (err < 0)
1256                                 return err;
1257                 }
1258         }
1259
1260         return 0;
1261 }
1262
1263 static int vt1709_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
1264 {
1265         int err;
1266
1267         if (!pin)
1268                 return 0;
1269
1270         if (spec->multiout.num_dacs == 5) /* 10 channels */
1271                 spec->multiout.hp_nid = VT1709_HP_DAC_NID;
1272         else if (spec->multiout.num_dacs == 3) /* 6 channels */
1273                 spec->multiout.hp_nid = 0;
1274
1275         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1276                               "Headphone Playback Volume",
1277                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1278         if (err < 0)
1279                 return err;
1280         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1281                               "Headphone Playback Switch",
1282                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1283         if (err < 0)
1284                 return err;
1285
1286         return 0;
1287 }
1288
1289 /* create playback/capture controls for input pins */
1290 static int vt1709_auto_create_analog_input_ctls(struct via_spec *spec,
1291                                                 const struct auto_pin_cfg *cfg)
1292 {
1293         static char *labels[] = {
1294                 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
1295         };
1296         struct hda_input_mux *imux = &spec->private_imux;
1297         int i, err, idx = 0;
1298
1299         /* for internal loopback recording select */
1300         imux->items[imux->num_items].label = "Stereo Mixer";
1301         imux->items[imux->num_items].index = idx;
1302         imux->num_items++;
1303
1304         for (i = 0; i < AUTO_PIN_LAST; i++) {
1305                 if (!cfg->input_pins[i])
1306                         continue;
1307
1308                 switch (cfg->input_pins[i]) {
1309                 case 0x1d: /* Mic */
1310                         idx = 2;
1311                         break;
1312                                 
1313                 case 0x1e: /* Line In */
1314                         idx = 3;
1315                         break;
1316
1317                 case 0x21: /* Front Mic */
1318                         idx = 4;
1319                         break;
1320
1321                 case 0x23: /* CD */
1322                         idx = 1;
1323                         break;
1324                 }
1325                 err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
1326                                            idx, 0x18);
1327                 if (err < 0)
1328                         return err;
1329                 imux->items[imux->num_items].label = labels[i];
1330                 imux->items[imux->num_items].index = idx;
1331                 imux->num_items++;
1332         }
1333         return 0;
1334 }
1335
1336 static int vt1709_parse_auto_config(struct hda_codec *codec)
1337 {
1338         struct via_spec *spec = codec->spec;
1339         int err;
1340
1341         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1342         if (err < 0)
1343                 return err;
1344         err = vt1709_auto_fill_dac_nids(spec, &spec->autocfg);
1345         if (err < 0)
1346                 return err;
1347         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
1348                 return 0; /* can't find valid BIOS pin config */
1349
1350         err = vt1709_auto_create_multi_out_ctls(spec, &spec->autocfg);
1351         if (err < 0)
1352                 return err;
1353         err = vt1709_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
1354         if (err < 0)
1355                 return err;
1356         err = vt1709_auto_create_analog_input_ctls(spec, &spec->autocfg);
1357         if (err < 0)
1358                 return err;
1359
1360         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
1361
1362         if (spec->autocfg.dig_out_pin)
1363                 spec->multiout.dig_out_nid = VT1709_DIGOUT_NID;
1364         if (spec->autocfg.dig_in_pin)
1365                 spec->dig_in_nid = VT1709_DIGIN_NID;
1366
1367         if (spec->kctl_alloc)
1368                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
1369
1370         spec->input_mux = &spec->private_imux;
1371
1372         return 1;
1373 }
1374
1375 #ifdef CONFIG_SND_HDA_POWER_SAVE
1376 static struct hda_amp_list vt1709_loopbacks[] = {
1377         { 0x18, HDA_INPUT, 1 },
1378         { 0x18, HDA_INPUT, 2 },
1379         { 0x18, HDA_INPUT, 3 },
1380         { 0x18, HDA_INPUT, 4 },
1381         { } /* end */
1382 };
1383 #endif
1384
1385 static int patch_vt1709_10ch(struct hda_codec *codec)
1386 {
1387         struct via_spec *spec;
1388         int err;
1389
1390         /* create a codec specific record */
1391         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1392         if (spec == NULL)
1393                 return -ENOMEM;
1394
1395         codec->spec = spec;
1396
1397         err = vt1709_parse_auto_config(codec);
1398         if (err < 0) {
1399                 via_free(codec);
1400                 return err;
1401         } else if (!err) {
1402                 printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
1403                        "Using genenic mode...\n");
1404         }
1405
1406         spec->init_verbs = vt1709_10ch_volume_init_verbs;       
1407
1408         spec->stream_name_analog = "VT1709 Analog";
1409         spec->stream_analog_playback = &vt1709_10ch_pcm_analog_playback;
1410         spec->stream_analog_capture = &vt1709_pcm_analog_capture;
1411
1412         spec->stream_name_digital = "VT1709 Digital";
1413         spec->stream_digital_playback = &vt1709_pcm_digital_playback;
1414         spec->stream_digital_capture = &vt1709_pcm_digital_capture;
1415
1416         
1417         if (!spec->adc_nids && spec->input_mux) {
1418                 spec->adc_nids = vt1709_adc_nids;
1419                 spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
1420                 spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
1421                 spec->num_mixers++;
1422         }
1423
1424         codec->patch_ops = via_patch_ops;
1425
1426         codec->patch_ops.init = via_auto_init;
1427 #ifdef CONFIG_SND_HDA_POWER_SAVE
1428         spec->loopback.amplist = vt1709_loopbacks;
1429 #endif
1430
1431         return 0;
1432 }
1433 /*
1434  * generic initialization of ADC, input mixers and output mixers
1435  */
1436 static struct hda_verb vt1709_6ch_volume_init_verbs[] = {
1437         /*
1438          * Unmute ADC0-2 and set the default input to mic-in
1439          */
1440         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1441         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1442         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1443
1444
1445         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
1446          * mixer widget
1447          */
1448         /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
1449         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1450         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1451         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
1452         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
1453         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
1454
1455         /*
1456          * Set up output selector (0x1a, 0x1b, 0x29)
1457          */
1458         /* set vol=0 to output mixers */
1459         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1460         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1461         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1462
1463         /*
1464          *  Unmute PW3 and PW4
1465          */
1466         {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1467         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1468
1469         /* Set input of PW4 as MW0 */
1470         {0x20, AC_VERB_SET_CONNECT_SEL, 0},
1471         /* PW9 Output enable */
1472         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
1473         { }
1474 };
1475
1476 static int patch_vt1709_6ch(struct hda_codec *codec)
1477 {
1478         struct via_spec *spec;
1479         int err;
1480
1481         /* create a codec specific record */
1482         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1483         if (spec == NULL)
1484                 return -ENOMEM;
1485
1486         codec->spec = spec;
1487
1488         err = vt1709_parse_auto_config(codec);
1489         if (err < 0) {
1490                 via_free(codec);
1491                 return err;
1492         } else if (!err) {
1493                 printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
1494                        "Using genenic mode...\n");
1495         }
1496
1497         spec->init_verbs = vt1709_6ch_volume_init_verbs;        
1498
1499         spec->stream_name_analog = "VT1709 Analog";
1500         spec->stream_analog_playback = &vt1709_6ch_pcm_analog_playback;
1501         spec->stream_analog_capture = &vt1709_pcm_analog_capture;
1502
1503         spec->stream_name_digital = "VT1709 Digital";
1504         spec->stream_digital_playback = &vt1709_pcm_digital_playback;
1505         spec->stream_digital_capture = &vt1709_pcm_digital_capture;
1506
1507         
1508         if (!spec->adc_nids && spec->input_mux) {
1509                 spec->adc_nids = vt1709_adc_nids;
1510                 spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
1511                 spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
1512                 spec->num_mixers++;
1513         }
1514
1515         codec->patch_ops = via_patch_ops;
1516
1517         codec->patch_ops.init = via_auto_init;
1518 #ifdef CONFIG_SND_HDA_POWER_SAVE
1519         spec->loopback.amplist = vt1709_loopbacks;
1520 #endif
1521         return 0;
1522 }
1523
1524 /* capture mixer elements */
1525 static struct snd_kcontrol_new vt1708B_capture_mixer[] = {
1526         HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
1527         HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
1528         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
1529         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
1530         {
1531                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1532                 /* The multiple "Capture Source" controls confuse alsamixer
1533                  * So call somewhat different..
1534                  */
1535                 /* .name = "Capture Source", */
1536                 .name = "Input Source",
1537                 .count = 1,
1538                 .info = via_mux_enum_info,
1539                 .get = via_mux_enum_get,
1540                 .put = via_mux_enum_put,
1541         },
1542         { } /* end */
1543 };
1544 /*
1545  * generic initialization of ADC, input mixers and output mixers
1546  */
1547 static struct hda_verb vt1708B_8ch_volume_init_verbs[] = {
1548         /*
1549          * Unmute ADC0-1 and set the default input to mic-in
1550          */
1551         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1552         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1553
1554
1555         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
1556          * mixer widget
1557          */
1558         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
1559         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1560         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1561         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
1562         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
1563         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
1564
1565         /*
1566          * Set up output mixers
1567          */
1568         /* set vol=0 to output mixers */
1569         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1570         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1571         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1572
1573         /* Setup default input to PW4 */
1574         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x1},
1575         /* PW9 Output enable */
1576         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
1577         /* PW10 Input enable */
1578         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
1579         { }
1580 };
1581
1582 static struct hda_verb vt1708B_4ch_volume_init_verbs[] = {
1583         /*
1584          * Unmute ADC0-1 and set the default input to mic-in
1585          */
1586         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1587         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1588
1589
1590         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
1591          * mixer widget
1592          */
1593         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
1594         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1595         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1596         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
1597         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
1598         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
1599
1600         /*
1601          * Set up output mixers
1602          */
1603         /* set vol=0 to output mixers */
1604         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1605         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1606         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1607
1608         /* Setup default input of PW4 to MW0 */
1609         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
1610         /* PW9 Output enable */
1611         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
1612         /* PW10 Input enable */
1613         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
1614         { }
1615 };
1616
1617 static struct hda_pcm_stream vt1708B_8ch_pcm_analog_playback = {
1618         .substreams = 1,
1619         .channels_min = 2,
1620         .channels_max = 8,
1621         .nid = 0x10, /* NID to query formats and rates */
1622         .ops = {
1623                 .open = via_playback_pcm_open,
1624                 .prepare = via_playback_pcm_prepare,
1625                 .cleanup = via_playback_pcm_cleanup
1626         },
1627 };
1628
1629 static struct hda_pcm_stream vt1708B_4ch_pcm_analog_playback = {
1630         .substreams = 1,
1631         .channels_min = 2,
1632         .channels_max = 4,
1633         .nid = 0x10, /* NID to query formats and rates */
1634         .ops = {
1635                 .open = via_playback_pcm_open,
1636                 .prepare = via_playback_pcm_prepare,
1637                 .cleanup = via_playback_pcm_cleanup
1638         },
1639 };
1640
1641 static struct hda_pcm_stream vt1708B_pcm_analog_capture = {
1642         .substreams = 2,
1643         .channels_min = 2,
1644         .channels_max = 2,
1645         .nid = 0x13, /* NID to query formats and rates */
1646         .ops = {
1647                 .prepare = via_capture_pcm_prepare,
1648                 .cleanup = via_capture_pcm_cleanup
1649         },
1650 };
1651
1652 static struct hda_pcm_stream vt1708B_pcm_digital_playback = {
1653         .substreams = 1,
1654         .channels_min = 2,
1655         .channels_max = 2,
1656         /* NID is set in via_build_pcms */
1657         .ops = {
1658                 .open = via_dig_playback_pcm_open,
1659                 .close = via_dig_playback_pcm_close,
1660                 .prepare = via_dig_playback_pcm_prepare
1661         },
1662 };
1663
1664 static struct hda_pcm_stream vt1708B_pcm_digital_capture = {
1665         .substreams = 1,
1666         .channels_min = 2,
1667         .channels_max = 2,
1668 };
1669
1670 /* fill in the dac_nids table from the parsed pin configuration */
1671 static int vt1708B_auto_fill_dac_nids(struct via_spec *spec,
1672                                      const struct auto_pin_cfg *cfg)
1673 {
1674         int i;
1675         hda_nid_t nid;
1676
1677         spec->multiout.num_dacs = cfg->line_outs;
1678
1679         spec->multiout.dac_nids = spec->private_dac_nids;
1680
1681         for (i = 0; i < 4; i++) {
1682                 nid = cfg->line_out_pins[i];
1683                 if (nid) {
1684                         /* config dac list */
1685                         switch (i) {
1686                         case AUTO_SEQ_FRONT:
1687                                 spec->multiout.dac_nids[i] = 0x10;
1688                                 break;
1689                         case AUTO_SEQ_CENLFE:
1690                                 spec->multiout.dac_nids[i] = 0x24;
1691                                 break;
1692                         case AUTO_SEQ_SURROUND:
1693                                 spec->multiout.dac_nids[i] = 0x25;
1694                                 break;
1695                         case AUTO_SEQ_SIDE:
1696                                 spec->multiout.dac_nids[i] = 0x11;
1697                                 break;
1698                         }
1699                 }
1700         }
1701
1702         return 0;
1703 }
1704
1705 /* add playback controls from the parsed DAC table */
1706 static int vt1708B_auto_create_multi_out_ctls(struct via_spec *spec,
1707                                              const struct auto_pin_cfg *cfg)
1708 {
1709         char name[32];
1710         static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
1711         hda_nid_t nid_vols[] = {0x16, 0x27, 0x26, 0x18};
1712         hda_nid_t nid, nid_vol = 0;
1713         int i, err;
1714
1715         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
1716                 nid = cfg->line_out_pins[i];
1717
1718                 if (!nid)
1719                         continue;
1720
1721                 nid_vol = nid_vols[i];
1722
1723                 if (i == AUTO_SEQ_CENLFE) {
1724                         /* Center/LFE */
1725                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1726                                               "Center Playback Volume",
1727                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
1728                                                                   HDA_OUTPUT));
1729                         if (err < 0)
1730                                 return err;
1731                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1732                                               "LFE Playback Volume",
1733                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
1734                                                                   HDA_OUTPUT));
1735                         if (err < 0)
1736                                 return err;
1737                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1738                                               "Center Playback Switch",
1739                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
1740                                                                   HDA_OUTPUT));
1741                         if (err < 0)
1742                                 return err;
1743                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1744                                               "LFE Playback Switch",
1745                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
1746                                                                   HDA_OUTPUT));
1747                         if (err < 0)
1748                                 return err;
1749                 } else if (i == AUTO_SEQ_FRONT) {
1750                         /* add control to mixer index 0 */
1751                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1752                                               "Master Front Playback Volume",
1753                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1754                                                                   HDA_INPUT));
1755                         if (err < 0)
1756                                 return err;
1757                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1758                                               "Master Front Playback Switch",
1759                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1760                                                                   HDA_INPUT));
1761                         if (err < 0)
1762                                 return err;
1763
1764                         /* add control to PW3 */
1765                         sprintf(name, "%s Playback Volume", chname[i]);
1766                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1767                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1768                                                                   HDA_OUTPUT));
1769                         if (err < 0)
1770                                 return err;
1771                         sprintf(name, "%s Playback Switch", chname[i]);
1772                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1773                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1774                                                                   HDA_OUTPUT));
1775                         if (err < 0)
1776                                 return err;
1777                 } else {
1778                         sprintf(name, "%s Playback Volume", chname[i]);
1779                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1780                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1781                                                                   HDA_OUTPUT));
1782                         if (err < 0)
1783                                 return err;
1784                         sprintf(name, "%s Playback Switch", chname[i]);
1785                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1786                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1787                                                                   HDA_OUTPUT));
1788                         if (err < 0)
1789                                 return err;
1790                 }
1791         }
1792
1793         return 0;
1794 }
1795
1796 static int vt1708B_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
1797 {
1798         int err;
1799
1800         if (!pin)
1801                 return 0;
1802
1803         spec->multiout.hp_nid = VT1708B_HP_NID; /* AOW3 */
1804
1805         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1806                               "Headphone Playback Volume",
1807                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1808         if (err < 0)
1809                 return err;
1810         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1811                               "Headphone Playback Switch",
1812                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1813         if (err < 0)
1814                 return err;
1815
1816         return 0;
1817 }
1818
1819 /* create playback/capture controls for input pins */
1820 static int vt1708B_auto_create_analog_input_ctls(struct via_spec *spec,
1821                                                 const struct auto_pin_cfg *cfg)
1822 {
1823         static char *labels[] = {
1824                 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
1825         };
1826         struct hda_input_mux *imux = &spec->private_imux;
1827         int i, err, idx = 0;
1828
1829         /* for internal loopback recording select */
1830         imux->items[imux->num_items].label = "Stereo Mixer";
1831         imux->items[imux->num_items].index = idx;
1832         imux->num_items++;
1833
1834         for (i = 0; i < AUTO_PIN_LAST; i++) {
1835                 if (!cfg->input_pins[i])
1836                         continue;
1837
1838                 switch (cfg->input_pins[i]) {
1839                 case 0x1a: /* Mic */
1840                         idx = 2;
1841                         break;
1842
1843                 case 0x1b: /* Line In */
1844                         idx = 3;
1845                         break;
1846
1847                 case 0x1e: /* Front Mic */
1848                         idx = 4;
1849                         break;
1850
1851                 case 0x1f: /* CD */
1852                         idx = 1;
1853                         break;
1854                 }
1855                 err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
1856                                            idx, 0x16);
1857                 if (err < 0)
1858                         return err;
1859                 imux->items[imux->num_items].label = labels[i];
1860                 imux->items[imux->num_items].index = idx;
1861                 imux->num_items++;
1862         }
1863         return 0;
1864 }
1865
1866 static int vt1708B_parse_auto_config(struct hda_codec *codec)
1867 {
1868         struct via_spec *spec = codec->spec;
1869         int err;
1870
1871         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1872         if (err < 0)
1873                 return err;
1874         err = vt1708B_auto_fill_dac_nids(spec, &spec->autocfg);
1875         if (err < 0)
1876                 return err;
1877         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
1878                 return 0; /* can't find valid BIOS pin config */
1879
1880         err = vt1708B_auto_create_multi_out_ctls(spec, &spec->autocfg);
1881         if (err < 0)
1882                 return err;
1883         err = vt1708B_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
1884         if (err < 0)
1885                 return err;
1886         err = vt1708B_auto_create_analog_input_ctls(spec, &spec->autocfg);
1887         if (err < 0)
1888                 return err;
1889
1890         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
1891
1892         if (spec->autocfg.dig_out_pin)
1893                 spec->multiout.dig_out_nid = VT1708B_DIGOUT_NID;
1894         if (spec->autocfg.dig_in_pin)
1895                 spec->dig_in_nid = VT1708B_DIGIN_NID;
1896
1897         if (spec->kctl_alloc)
1898                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
1899
1900         spec->input_mux = &spec->private_imux;
1901
1902         return 1;
1903 }
1904
1905 #ifdef CONFIG_SND_HDA_POWER_SAVE
1906 static struct hda_amp_list vt1708B_loopbacks[] = {
1907         { 0x16, HDA_INPUT, 1 },
1908         { 0x16, HDA_INPUT, 2 },
1909         { 0x16, HDA_INPUT, 3 },
1910         { 0x16, HDA_INPUT, 4 },
1911         { } /* end */
1912 };
1913 #endif
1914
1915 static int patch_vt1708B_8ch(struct hda_codec *codec)
1916 {
1917         struct via_spec *spec;
1918         int err;
1919
1920         /* create a codec specific record */
1921         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1922         if (spec == NULL)
1923                 return -ENOMEM;
1924
1925         codec->spec = spec;
1926
1927         /* automatic parse from the BIOS config */
1928         err = vt1708B_parse_auto_config(codec);
1929         if (err < 0) {
1930                 via_free(codec);
1931                 return err;
1932         } else if (!err) {
1933                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
1934                        "from BIOS.  Using genenic mode...\n");
1935         }
1936
1937         spec->init_verbs = vt1708B_8ch_volume_init_verbs;
1938
1939         spec->stream_name_analog = "VT1708B Analog";
1940         spec->stream_analog_playback = &vt1708B_8ch_pcm_analog_playback;
1941         spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
1942
1943         spec->stream_name_digital = "VT1708B Digital";
1944         spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
1945         spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
1946
1947         if (!spec->adc_nids && spec->input_mux) {
1948                 spec->adc_nids = vt1708B_adc_nids;
1949                 spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
1950                 spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
1951                 spec->num_mixers++;
1952         }
1953
1954         codec->patch_ops = via_patch_ops;
1955
1956         codec->patch_ops.init = via_auto_init;
1957 #ifdef CONFIG_SND_HDA_POWER_SAVE
1958         spec->loopback.amplist = vt1708B_loopbacks;
1959 #endif
1960
1961         return 0;
1962 }
1963
1964 static int patch_vt1708B_4ch(struct hda_codec *codec)
1965 {
1966         struct via_spec *spec;
1967         int err;
1968
1969         /* create a codec specific record */
1970         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1971         if (spec == NULL)
1972                 return -ENOMEM;
1973
1974         codec->spec = spec;
1975
1976         /* automatic parse from the BIOS config */
1977         err = vt1708B_parse_auto_config(codec);
1978         if (err < 0) {
1979                 via_free(codec);
1980                 return err;
1981         } else if (!err) {
1982                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
1983                        "from BIOS.  Using genenic mode...\n");
1984         }
1985
1986         spec->init_verbs = vt1708B_4ch_volume_init_verbs;
1987
1988         spec->stream_name_analog = "VT1708B Analog";
1989         spec->stream_analog_playback = &vt1708B_4ch_pcm_analog_playback;
1990         spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
1991
1992         spec->stream_name_digital = "VT1708B Digital";
1993         spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
1994         spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
1995
1996         if (!spec->adc_nids && spec->input_mux) {
1997                 spec->adc_nids = vt1708B_adc_nids;
1998                 spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
1999                 spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
2000                 spec->num_mixers++;
2001         }
2002
2003         codec->patch_ops = via_patch_ops;
2004
2005         codec->patch_ops.init = via_auto_init;
2006 #ifdef CONFIG_SND_HDA_POWER_SAVE
2007         spec->loopback.amplist = vt1708B_loopbacks;
2008 #endif
2009
2010         return 0;
2011 }
2012
2013 /*
2014  * patch entries
2015  */
2016 struct hda_codec_preset snd_hda_preset_via[] = {
2017         { .id = 0x11061708, .name = "VIA VT1708", .patch = patch_vt1708},
2018         { .id = 0x11061709, .name = "VIA VT1708", .patch = patch_vt1708},
2019         { .id = 0x1106170A, .name = "VIA VT1708", .patch = patch_vt1708},
2020         { .id = 0x1106170B, .name = "VIA VT1708", .patch = patch_vt1708},
2021         { .id = 0x1106E710, .name = "VIA VT1709 10-Ch",
2022           .patch = patch_vt1709_10ch},
2023         { .id = 0x1106E711, .name = "VIA VT1709 10-Ch",
2024           .patch = patch_vt1709_10ch},
2025         { .id = 0x1106E712, .name = "VIA VT1709 10-Ch",
2026           .patch = patch_vt1709_10ch},
2027         { .id = 0x1106E713, .name = "VIA VT1709 10-Ch",
2028           .patch = patch_vt1709_10ch},
2029         { .id = 0x1106E714, .name = "VIA VT1709 6-Ch",
2030           .patch = patch_vt1709_6ch},
2031         { .id = 0x1106E715, .name = "VIA VT1709 6-Ch",
2032           .patch = patch_vt1709_6ch},
2033         { .id = 0x1106E716, .name = "VIA VT1709 6-Ch",
2034           .patch = patch_vt1709_6ch},
2035         { .id = 0x1106E717, .name = "VIA VT1709 6-Ch",
2036           .patch = patch_vt1709_6ch},
2037         { .id = 0x1106E720, .name = "VIA VT1708B 8-Ch",
2038           .patch = patch_vt1708B_8ch},
2039         { .id = 0x1106E721, .name = "VIA VT1708B 8-Ch",
2040           .patch = patch_vt1708B_8ch},
2041         { .id = 0x1106E722, .name = "VIA VT1708B 8-Ch",
2042           .patch = patch_vt1708B_8ch},
2043         { .id = 0x1106E723, .name = "VIA VT1708B 8-Ch",
2044           .patch = patch_vt1708B_8ch},
2045         { .id = 0x1106E724, .name = "VIA VT1708B 4-Ch",
2046           .patch = patch_vt1708B_4ch},
2047         { .id = 0x1106E725, .name = "VIA VT1708B 4-Ch",
2048           .patch = patch_vt1708B_4ch},
2049         { .id = 0x1106E726, .name = "VIA VT1708B 4-Ch",
2050           .patch = patch_vt1708B_4ch},
2051         { .id = 0x1106E727, .name = "VIA VT1708B 4-Ch",
2052           .patch = patch_vt1708B_4ch},
2053         {} /* terminator */
2054 };