ALSA: hda - Introduce get_wcaps_type() macro
[pandora-kernel.git] / sound / pci / hda / patch_via.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * HD audio interface patch for VIA VT1702/VT1708/VT1709 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 /* 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 /*                                                                           */
40 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
41
42
43 #include <linux/init.h>
44 #include <linux/delay.h>
45 #include <linux/slab.h>
46 #include <sound/core.h>
47 #include <sound/asoundef.h>
48 #include "hda_codec.h"
49 #include "hda_local.h"
50
51 /* amp values */
52 #define AMP_VAL_IDX_SHIFT       19
53 #define AMP_VAL_IDX_MASK        (0x0f<<19)
54
55 /* Pin Widget NID */
56 #define VT1708_HP_NID           0x13
57 #define VT1708_DIGOUT_NID       0x14
58 #define VT1708_DIGIN_NID        0x16
59 #define VT1708_DIGIN_PIN        0x26
60 #define VT1708_HP_PIN_NID       0x20
61 #define VT1708_CD_PIN_NID       0x24
62
63 #define VT1709_HP_DAC_NID       0x28
64 #define VT1709_DIGOUT_NID       0x13
65 #define VT1709_DIGIN_NID        0x17
66 #define VT1709_DIGIN_PIN        0x25
67
68 #define VT1708B_HP_NID          0x25
69 #define VT1708B_DIGOUT_NID      0x12
70 #define VT1708B_DIGIN_NID       0x15
71 #define VT1708B_DIGIN_PIN       0x21
72
73 #define VT1708S_HP_NID          0x25
74 #define VT1708S_DIGOUT_NID      0x12
75
76 #define VT1702_HP_NID           0x17
77 #define VT1702_DIGOUT_NID       0x11
78
79 #define IS_VT1708_VENDORID(x)           ((x) >= 0x11061708 && (x) <= 0x1106170b)
80 #define IS_VT1709_10CH_VENDORID(x)      ((x) >= 0x1106e710 && (x) <= 0x1106e713)
81 #define IS_VT1709_6CH_VENDORID(x)       ((x) >= 0x1106e714 && (x) <= 0x1106e717)
82 #define IS_VT1708B_8CH_VENDORID(x)      ((x) >= 0x1106e720 && (x) <= 0x1106e723)
83 #define IS_VT1708B_4CH_VENDORID(x)      ((x) >= 0x1106e724 && (x) <= 0x1106e727)
84 #define IS_VT1708S_VENDORID(x)          ((x) >= 0x11060397 && (x) <= 0x11067397)
85 #define IS_VT1702_VENDORID(x)           ((x) >= 0x11060398 && (x) <= 0x11067398)
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         VT1702,
96         CODEC_TYPES,
97 };
98
99 static enum VIA_HDA_CODEC get_codec_type(u32 vendor_id)
100 {
101         u16 ven_id = vendor_id >> 16;
102         u16 dev_id = vendor_id & 0xffff;
103         enum VIA_HDA_CODEC codec_type;
104
105         /* get codec type */
106         if (ven_id != 0x1106)
107                 codec_type = UNKNOWN;
108         else if (dev_id >= 0x1708 && dev_id <= 0x170b)
109                 codec_type = VT1708;
110         else if (dev_id >= 0xe710 && dev_id <= 0xe713)
111                 codec_type = VT1709_10CH;
112         else if (dev_id >= 0xe714 && dev_id <= 0xe717)
113                 codec_type = VT1709_6CH;
114         else if (dev_id >= 0xe720 && dev_id <= 0xe723)
115                 codec_type = VT1708B_8CH;
116         else if (dev_id >= 0xe724 && dev_id <= 0xe727)
117                 codec_type = VT1708B_4CH;
118         else if ((dev_id & 0xfff) == 0x397
119                  && (dev_id >> 12) < 8)
120                 codec_type = VT1708S;
121         else if ((dev_id & 0xfff) == 0x398
122                  && (dev_id >> 12) < 8)
123                 codec_type = VT1702;
124         else
125                 codec_type = UNKNOWN;
126         return codec_type;
127 };
128
129 #define VIA_HP_EVENT            0x01
130 #define VIA_GPIO_EVENT          0x02
131
132 enum {
133         VIA_CTL_WIDGET_VOL,
134         VIA_CTL_WIDGET_MUTE,
135 };
136
137 enum {
138         AUTO_SEQ_FRONT = 0,
139         AUTO_SEQ_SURROUND,
140         AUTO_SEQ_CENLFE,
141         AUTO_SEQ_SIDE
142 };
143
144 /* Some VT1708S based boards gets the micboost setting wrong, so we have
145  * to apply some brute-force and re-write the TLV's by software. */
146 static int mic_boost_tlv(struct snd_kcontrol *kcontrol, int op_flag,
147                          unsigned int size, unsigned int __user *_tlv)
148 {
149         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
150         hda_nid_t nid = get_amp_nid(kcontrol);
151
152         if (get_codec_type(codec->vendor_id) == VT1708S
153             && (nid == 0x1a || nid == 0x1e)) {
154                 if (size < 4 * sizeof(unsigned int))
155                         return -ENOMEM;
156                 if (put_user(1, _tlv))  /* SNDRV_CTL_TLVT_DB_SCALE */
157                         return -EFAULT;
158                 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
159                         return -EFAULT;
160                 if (put_user(0, _tlv + 2)) /* offset = 0 */
161                         return -EFAULT;
162                 if (put_user(1000, _tlv + 3)) /* step size = 10 dB */
163                         return -EFAULT;
164         }
165         return 0;
166 }
167
168 static int mic_boost_volume_info(struct snd_kcontrol *kcontrol,
169                                  struct snd_ctl_elem_info *uinfo)
170 {
171         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
172         hda_nid_t nid = get_amp_nid(kcontrol);
173
174         if (get_codec_type(codec->vendor_id) == VT1708S
175             && (nid == 0x1a || nid == 0x1e)) {
176                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
177                 uinfo->count = 2;
178                 uinfo->value.integer.min = 0;
179                 uinfo->value.integer.max = 3;
180         }
181         return 0;
182 }
183
184 static struct snd_kcontrol_new vt1708_control_templates[] = {
185         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
186         HDA_CODEC_MUTE(NULL, 0, 0, 0),
187 };
188
189
190 struct via_spec {
191         /* codec parameterization */
192         struct snd_kcontrol_new *mixers[3];
193         unsigned int num_mixers;
194
195         struct hda_verb *init_verbs[5];
196         unsigned int num_iverbs;
197
198         char *stream_name_analog;
199         struct hda_pcm_stream *stream_analog_playback;
200         struct hda_pcm_stream *stream_analog_capture;
201
202         char *stream_name_digital;
203         struct hda_pcm_stream *stream_digital_playback;
204         struct hda_pcm_stream *stream_digital_capture;
205
206         /* playback */
207         struct hda_multi_out multiout;
208         hda_nid_t slave_dig_outs[2];
209
210         /* capture */
211         unsigned int num_adc_nids;
212         hda_nid_t *adc_nids;
213         hda_nid_t mux_nids[3];
214         hda_nid_t dig_in_nid;
215         hda_nid_t dig_in_pin;
216
217         /* capture source */
218         const struct hda_input_mux *input_mux;
219         unsigned int cur_mux[3];
220
221         /* PCM information */
222         struct hda_pcm pcm_rec[3];
223
224         /* dynamic controls, init_verbs and input_mux */
225         struct auto_pin_cfg autocfg;
226         struct snd_array kctls;
227         struct hda_input_mux private_imux[2];
228         hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
229
230         /* HP mode source */
231         const struct hda_input_mux *hp_mux;
232         unsigned int hp_independent_mode;
233
234 #ifdef CONFIG_SND_HDA_POWER_SAVE
235         struct hda_loopback_check loopback;
236 #endif
237 };
238
239 static hda_nid_t vt1708_adc_nids[2] = {
240         /* ADC1-2 */
241         0x15, 0x27
242 };
243
244 static hda_nid_t vt1709_adc_nids[3] = {
245         /* ADC1-2 */
246         0x14, 0x15, 0x16
247 };
248
249 static hda_nid_t vt1708B_adc_nids[2] = {
250         /* ADC1-2 */
251         0x13, 0x14
252 };
253
254 static hda_nid_t vt1708S_adc_nids[2] = {
255         /* ADC1-2 */
256         0x13, 0x14
257 };
258
259 static hda_nid_t vt1702_adc_nids[3] = {
260         /* ADC1-2 */
261         0x12, 0x20, 0x1F
262 };
263
264 /* add dynamic controls */
265 static int via_add_control(struct via_spec *spec, int type, const char *name,
266                            unsigned long val)
267 {
268         struct snd_kcontrol_new *knew;
269
270         snd_array_init(&spec->kctls, sizeof(*knew), 32);
271         knew = snd_array_new(&spec->kctls);
272         if (!knew)
273                 return -ENOMEM;
274         *knew = vt1708_control_templates[type];
275         knew->name = kstrdup(name, GFP_KERNEL);
276         if (!knew->name)
277                 return -ENOMEM;
278         knew->private_value = val;
279         return 0;
280 }
281
282 static void via_free_kctls(struct hda_codec *codec)
283 {
284         struct via_spec *spec = codec->spec;
285
286         if (spec->kctls.list) {
287                 struct snd_kcontrol_new *kctl = spec->kctls.list;
288                 int i;
289                 for (i = 0; i < spec->kctls.used; i++)
290                         kfree(kctl[i].name);
291         }
292         snd_array_free(&spec->kctls);
293 }
294
295 /* create input playback/capture controls for the given pin */
296 static int via_new_analog_input(struct via_spec *spec, hda_nid_t pin,
297                                 const char *ctlname, int idx, int mix_nid)
298 {
299         char name[32];
300         int err;
301
302         sprintf(name, "%s Playback Volume", ctlname);
303         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
304                               HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
305         if (err < 0)
306                 return err;
307         sprintf(name, "%s Playback Switch", ctlname);
308         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
309                               HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
310         if (err < 0)
311                 return err;
312         return 0;
313 }
314
315 static void via_auto_set_output_and_unmute(struct hda_codec *codec,
316                                            hda_nid_t nid, int pin_type,
317                                            int dac_idx)
318 {
319         /* set as output */
320         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
321                             pin_type);
322         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
323                             AMP_OUT_UNMUTE);
324         if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
325                 snd_hda_codec_write(codec, nid, 0, 
326                                     AC_VERB_SET_EAPD_BTLENABLE, 0x02);
327 }
328
329
330 static void via_auto_init_multi_out(struct hda_codec *codec)
331 {
332         struct via_spec *spec = codec->spec;
333         int i;
334
335         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
336                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
337                 if (nid)
338                         via_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
339         }
340 }
341
342 static void via_auto_init_hp_out(struct hda_codec *codec)
343 {
344         struct via_spec *spec = codec->spec;
345         hda_nid_t pin;
346
347         pin = spec->autocfg.hp_pins[0];
348         if (pin) /* connect to front */
349                 via_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
350 }
351
352 static void via_auto_init_analog_input(struct hda_codec *codec)
353 {
354         struct via_spec *spec = codec->spec;
355         int i;
356
357         for (i = 0; i < AUTO_PIN_LAST; i++) {
358                 hda_nid_t nid = spec->autocfg.input_pins[i];
359
360                 snd_hda_codec_write(codec, nid, 0,
361                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
362                                     (i <= AUTO_PIN_FRONT_MIC ?
363                                      PIN_VREF50 : PIN_IN));
364
365         }
366 }
367 /*
368  * input MUX handling
369  */
370 static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
371                              struct snd_ctl_elem_info *uinfo)
372 {
373         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
374         struct via_spec *spec = codec->spec;
375         return snd_hda_input_mux_info(spec->input_mux, uinfo);
376 }
377
378 static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
379                             struct snd_ctl_elem_value *ucontrol)
380 {
381         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
382         struct via_spec *spec = codec->spec;
383         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
384
385         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
386         return 0;
387 }
388
389 static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
390                             struct snd_ctl_elem_value *ucontrol)
391 {
392         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
393         struct via_spec *spec = codec->spec;
394         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
395
396         if (!spec->mux_nids[adc_idx])
397                 return -EINVAL;
398         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
399                                      spec->mux_nids[adc_idx],
400                                      &spec->cur_mux[adc_idx]);
401 }
402
403 static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
404                                    struct snd_ctl_elem_info *uinfo)
405 {
406         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
407         struct via_spec *spec = codec->spec;
408         return snd_hda_input_mux_info(spec->hp_mux, uinfo);
409 }
410
411 static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
412                                   struct snd_ctl_elem_value *ucontrol)
413 {
414         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
415         struct via_spec *spec = codec->spec;
416         hda_nid_t nid = spec->autocfg.hp_pins[0];
417         unsigned int pinsel = snd_hda_codec_read(codec, nid, 0,
418                                                  AC_VERB_GET_CONNECT_SEL,
419                                                  0x00);
420
421         ucontrol->value.enumerated.item[0] = pinsel;
422
423         return 0;
424 }
425
426 static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
427                                   struct snd_ctl_elem_value *ucontrol)
428 {
429         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
430         struct via_spec *spec = codec->spec;
431         hda_nid_t nid = spec->autocfg.hp_pins[0];
432         unsigned int pinsel = ucontrol->value.enumerated.item[0];
433         unsigned int con_nid = snd_hda_codec_read(codec, nid, 0,
434                                          AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
435
436         if (con_nid == spec->multiout.hp_nid) {
437                 if (pinsel == 0) {
438                         if (!spec->hp_independent_mode) {
439                                 if (spec->multiout.num_dacs > 1)
440                                         spec->multiout.num_dacs -= 1;
441                                 spec->hp_independent_mode = 1;
442                         }
443                 } else if (pinsel == 1) {
444                        if (spec->hp_independent_mode) {
445                                 if (spec->multiout.num_dacs > 1)
446                                         spec->multiout.num_dacs += 1;
447                                 spec->hp_independent_mode = 0;
448                        }
449                 }
450         } else {
451                 if (pinsel == 0) {
452                         if (spec->hp_independent_mode) {
453                                 if (spec->multiout.num_dacs > 1)
454                                         spec->multiout.num_dacs += 1;
455                                 spec->hp_independent_mode = 0;
456                         }
457                 } else if (pinsel == 1) {
458                        if (!spec->hp_independent_mode) {
459                                 if (spec->multiout.num_dacs > 1)
460                                         spec->multiout.num_dacs -= 1;
461                                 spec->hp_independent_mode = 1;
462                        }
463                 }
464         }
465         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
466                             pinsel);
467
468         if (spec->multiout.hp_nid &&
469             spec->multiout.hp_nid != spec->multiout.dac_nids[HDA_FRONT])
470                         snd_hda_codec_setup_stream(codec,
471                                                    spec->multiout.hp_nid,
472                                                    0, 0, 0);
473
474         return 0;
475 }
476
477 static struct snd_kcontrol_new via_hp_mixer[] = {
478         {
479                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
480                 .name = "Independent HP",
481                 .count = 1,
482                 .info = via_independent_hp_info,
483                 .get = via_independent_hp_get,
484                 .put = via_independent_hp_put,
485         },
486         { } /* end */
487 };
488
489 /* capture mixer elements */
490 static struct snd_kcontrol_new vt1708_capture_mixer[] = {
491         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_INPUT),
492         HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_INPUT),
493         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x27, 0x0, HDA_INPUT),
494         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x27, 0x0, HDA_INPUT),
495         {
496                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
497                 /* The multiple "Capture Source" controls confuse alsamixer
498                  * So call somewhat different..
499                  */
500                 /* .name = "Capture Source", */
501                 .name = "Input Source",
502                 .count = 1,
503                 .info = via_mux_enum_info,
504                 .get = via_mux_enum_get,
505                 .put = via_mux_enum_put,
506         },
507         { } /* end */
508 };
509 /*
510  * generic initialization of ADC, input mixers and output mixers
511  */
512 static struct hda_verb vt1708_volume_init_verbs[] = {
513         /*
514          * Unmute ADC0-1 and set the default input to mic-in
515          */
516         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
517         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
518
519
520         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
521          * mixer widget
522          */
523         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
524         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
525         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
526         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
527         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
528         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
529
530         /*
531          * Set up output mixers (0x19 - 0x1b)
532          */
533         /* set vol=0 to output mixers */
534         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
535         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
536         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
537         
538         /* Setup default input to PW4 */
539         {0x20, AC_VERB_SET_CONNECT_SEL, 0x1},
540         /* PW9 Output enable */
541         {0x25, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
542         { }
543 };
544
545 static int via_playback_pcm_open(struct hda_pcm_stream *hinfo,
546                                  struct hda_codec *codec,
547                                  struct snd_pcm_substream *substream)
548 {
549         struct via_spec *spec = codec->spec;
550         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
551                                              hinfo);
552 }
553
554 static int via_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
555                                     struct hda_codec *codec,
556                                     unsigned int stream_tag,
557                                     unsigned int format,
558                                     struct snd_pcm_substream *substream)
559 {
560         struct via_spec *spec = codec->spec;
561         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
562                                                 stream_tag, format, substream);
563 }
564
565 static int via_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
566                                     struct hda_codec *codec,
567                                     struct snd_pcm_substream *substream)
568 {
569         struct via_spec *spec = codec->spec;
570         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
571 }
572
573
574 static void playback_multi_pcm_prep_0(struct hda_codec *codec,
575                                       unsigned int stream_tag,
576                                       unsigned int format,
577                                       struct snd_pcm_substream *substream)
578 {
579         struct via_spec *spec = codec->spec;
580         struct hda_multi_out *mout = &spec->multiout;
581         hda_nid_t *nids = mout->dac_nids;
582         int chs = substream->runtime->channels;
583         int i;
584
585         mutex_lock(&codec->spdif_mutex);
586         if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
587                 if (chs == 2 &&
588                     snd_hda_is_supported_format(codec, mout->dig_out_nid,
589                                                 format) &&
590                     !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
591                         mout->dig_out_used = HDA_DIG_ANALOG_DUP;
592                         /* turn off SPDIF once; otherwise the IEC958 bits won't
593                          * be updated */
594                         if (codec->spdif_ctls & AC_DIG1_ENABLE)
595                                 snd_hda_codec_write(codec, mout->dig_out_nid, 0,
596                                                     AC_VERB_SET_DIGI_CONVERT_1,
597                                                     codec->spdif_ctls &
598                                                         ~AC_DIG1_ENABLE & 0xff);
599                         snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
600                                                    stream_tag, 0, format);
601                         /* turn on again (if needed) */
602                         if (codec->spdif_ctls & AC_DIG1_ENABLE)
603                                 snd_hda_codec_write(codec, mout->dig_out_nid, 0,
604                                                     AC_VERB_SET_DIGI_CONVERT_1,
605                                                     codec->spdif_ctls & 0xff);
606                 } else {
607                         mout->dig_out_used = 0;
608                         snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
609                                                    0, 0, 0);
610                 }
611         }
612         mutex_unlock(&codec->spdif_mutex);
613
614         /* front */
615         snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
616                                    0, format);
617
618         if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
619             !spec->hp_independent_mode)
620                 /* headphone out will just decode front left/right (stereo) */
621                 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
622                                            0, format);
623
624         /* extra outputs copied from front */
625         for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
626                 if (mout->extra_out_nid[i])
627                         snd_hda_codec_setup_stream(codec,
628                                                    mout->extra_out_nid[i],
629                                                    stream_tag, 0, format);
630
631         /* surrounds */
632         for (i = 1; i < mout->num_dacs; i++) {
633                 if (chs >= (i + 1) * 2) /* independent out */
634                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
635                                                    i * 2, format);
636                 else /* copy front */
637                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
638                                                    0, format);
639         }
640 }
641
642 static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
643                                           struct hda_codec *codec,
644                                           unsigned int stream_tag,
645                                           unsigned int format,
646                                           struct snd_pcm_substream *substream)
647 {
648         struct via_spec *spec = codec->spec;
649         struct hda_multi_out *mout = &spec->multiout;
650         hda_nid_t *nids = mout->dac_nids;
651
652         if (substream->number == 0)
653                 playback_multi_pcm_prep_0(codec, stream_tag, format,
654                                           substream);
655         else {
656                 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
657                     spec->hp_independent_mode)
658                         snd_hda_codec_setup_stream(codec, mout->hp_nid,
659                                                    stream_tag, 0, format);
660         }
661
662         return 0;
663 }
664
665 static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
666                                     struct hda_codec *codec,
667                                     struct snd_pcm_substream *substream)
668 {
669         struct via_spec *spec = codec->spec;
670         struct hda_multi_out *mout = &spec->multiout;
671         hda_nid_t *nids = mout->dac_nids;
672         int i;
673
674         if (substream->number == 0) {
675                 for (i = 0; i < mout->num_dacs; i++)
676                         snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
677
678                 if (mout->hp_nid && !spec->hp_independent_mode)
679                         snd_hda_codec_setup_stream(codec, mout->hp_nid,
680                                                    0, 0, 0);
681
682                 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
683                         if (mout->extra_out_nid[i])
684                                 snd_hda_codec_setup_stream(codec,
685                                                         mout->extra_out_nid[i],
686                                                         0, 0, 0);
687                 mutex_lock(&codec->spdif_mutex);
688                 if (mout->dig_out_nid &&
689                     mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
690                         snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
691                                                    0, 0, 0);
692                         mout->dig_out_used = 0;
693                 }
694                 mutex_unlock(&codec->spdif_mutex);
695         } else {
696                 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
697                     spec->hp_independent_mode)
698                         snd_hda_codec_setup_stream(codec, mout->hp_nid,
699                                                    0, 0, 0);
700         }
701
702         return 0;
703 }
704
705 /*
706  * Digital out
707  */
708 static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
709                                      struct hda_codec *codec,
710                                      struct snd_pcm_substream *substream)
711 {
712         struct via_spec *spec = codec->spec;
713         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
714 }
715
716 static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
717                                       struct hda_codec *codec,
718                                       struct snd_pcm_substream *substream)
719 {
720         struct via_spec *spec = codec->spec;
721         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
722 }
723
724 static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
725                                         struct hda_codec *codec,
726                                         unsigned int stream_tag,
727                                         unsigned int format,
728                                         struct snd_pcm_substream *substream)
729 {
730         struct via_spec *spec = codec->spec;
731         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
732                                              stream_tag, format, substream);
733 }
734
735 static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
736                                         struct hda_codec *codec,
737                                         struct snd_pcm_substream *substream)
738 {
739         struct via_spec *spec = codec->spec;
740         snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
741         return 0;
742 }
743
744 /*
745  * Analog capture
746  */
747 static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
748                                    struct hda_codec *codec,
749                                    unsigned int stream_tag,
750                                    unsigned int format,
751                                    struct snd_pcm_substream *substream)
752 {
753         struct via_spec *spec = codec->spec;
754
755         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
756                                    stream_tag, 0, format);
757         return 0;
758 }
759
760 static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
761                                    struct hda_codec *codec,
762                                    struct snd_pcm_substream *substream)
763 {
764         struct via_spec *spec = codec->spec;
765         snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
766         return 0;
767 }
768
769 static struct hda_pcm_stream vt1708_pcm_analog_playback = {
770         .substreams = 2,
771         .channels_min = 2,
772         .channels_max = 8,
773         .nid = 0x10, /* NID to query formats and rates */
774         .ops = {
775                 .open = via_playback_pcm_open,
776                 .prepare = via_playback_multi_pcm_prepare,
777                 .cleanup = via_playback_multi_pcm_cleanup
778         },
779 };
780
781 static struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
782         .substreams = 1,
783         .channels_min = 2,
784         .channels_max = 8,
785         .nid = 0x10, /* NID to query formats and rates */
786         /* We got noisy outputs on the right channel on VT1708 when
787          * 24bit samples are used.  Until any workaround is found,
788          * disable the 24bit format, so far.
789          */
790         .formats = SNDRV_PCM_FMTBIT_S16_LE,
791         .ops = {
792                 .open = via_playback_pcm_open,
793                 .prepare = via_playback_pcm_prepare,
794                 .cleanup = via_playback_pcm_cleanup
795         },
796 };
797
798 static struct hda_pcm_stream vt1708_pcm_analog_capture = {
799         .substreams = 2,
800         .channels_min = 2,
801         .channels_max = 2,
802         .nid = 0x15, /* NID to query formats and rates */
803         .ops = {
804                 .prepare = via_capture_pcm_prepare,
805                 .cleanup = via_capture_pcm_cleanup
806         },
807 };
808
809 static struct hda_pcm_stream vt1708_pcm_digital_playback = {
810         .substreams = 1,
811         .channels_min = 2,
812         .channels_max = 2,
813         /* NID is set in via_build_pcms */
814         .ops = {
815                 .open = via_dig_playback_pcm_open,
816                 .close = via_dig_playback_pcm_close,
817                 .prepare = via_dig_playback_pcm_prepare,
818                 .cleanup = via_dig_playback_pcm_cleanup
819         },
820 };
821
822 static struct hda_pcm_stream vt1708_pcm_digital_capture = {
823         .substreams = 1,
824         .channels_min = 2,
825         .channels_max = 2,
826 };
827
828 static int via_build_controls(struct hda_codec *codec)
829 {
830         struct via_spec *spec = codec->spec;
831         int err;
832         int i;
833
834         for (i = 0; i < spec->num_mixers; i++) {
835                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
836                 if (err < 0)
837                         return err;
838         }
839
840         if (spec->multiout.dig_out_nid) {
841                 err = snd_hda_create_spdif_out_ctls(codec,
842                                                     spec->multiout.dig_out_nid);
843                 if (err < 0)
844                         return err;
845                 err = snd_hda_create_spdif_share_sw(codec,
846                                                     &spec->multiout);
847                 if (err < 0)
848                         return err;
849                 spec->multiout.share_spdif = 1;
850         }
851         if (spec->dig_in_nid) {
852                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
853                 if (err < 0)
854                         return err;
855         }
856         via_free_kctls(codec); /* no longer needed */
857         return 0;
858 }
859
860 static int via_build_pcms(struct hda_codec *codec)
861 {
862         struct via_spec *spec = codec->spec;
863         struct hda_pcm *info = spec->pcm_rec;
864
865         codec->num_pcms = 1;
866         codec->pcm_info = info;
867
868         info->name = spec->stream_name_analog;
869         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *(spec->stream_analog_playback);
870         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
871         info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_analog_capture);
872         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
873
874         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
875                 spec->multiout.max_channels;
876
877         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
878                 codec->num_pcms++;
879                 info++;
880                 info->name = spec->stream_name_digital;
881                 info->pcm_type = HDA_PCM_TYPE_SPDIF;
882                 if (spec->multiout.dig_out_nid) {
883                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
884                                 *(spec->stream_digital_playback);
885                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
886                                 spec->multiout.dig_out_nid;
887                 }
888                 if (spec->dig_in_nid) {
889                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
890                                 *(spec->stream_digital_capture);
891                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
892                                 spec->dig_in_nid;
893                 }
894         }
895
896         return 0;
897 }
898
899 static void via_free(struct hda_codec *codec)
900 {
901         struct via_spec *spec = codec->spec;
902
903         if (!spec)
904                 return;
905
906         via_free_kctls(codec);
907         kfree(codec->spec);
908 }
909
910 /* mute internal speaker if HP is plugged */
911 static void via_hp_automute(struct hda_codec *codec)
912 {
913         unsigned int present;
914         struct via_spec *spec = codec->spec;
915
916         present = snd_hda_codec_read(codec, spec->autocfg.hp_pins[0], 0,
917                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
918         snd_hda_codec_amp_stereo(codec, spec->autocfg.line_out_pins[0],
919                                  HDA_OUTPUT, 0, HDA_AMP_MUTE,
920                                  present ? HDA_AMP_MUTE : 0);
921 }
922
923 static void via_gpio_control(struct hda_codec *codec)
924 {
925         unsigned int gpio_data;
926         unsigned int vol_counter;
927         unsigned int vol;
928         unsigned int master_vol;
929
930         struct via_spec *spec = codec->spec;
931
932         gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
933                                        AC_VERB_GET_GPIO_DATA, 0) & 0x03;
934
935         vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
936                                           0xF84, 0) & 0x3F0000) >> 16;
937
938         vol = vol_counter & 0x1F;
939         master_vol = snd_hda_codec_read(codec, 0x1A, 0,
940                                         AC_VERB_GET_AMP_GAIN_MUTE,
941                                         AC_AMP_GET_INPUT);
942
943         if (gpio_data == 0x02) {
944                 /* unmute line out */
945                 snd_hda_codec_amp_stereo(codec, spec->autocfg.line_out_pins[0],
946                                          HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
947
948                 if (vol_counter & 0x20) {
949                         /* decrease volume */
950                         if (vol > master_vol)
951                                 vol = master_vol;
952                         snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
953                                                  0, HDA_AMP_VOLMASK,
954                                                  master_vol-vol);
955                 } else {
956                         /* increase volume */
957                         snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
958                                          HDA_AMP_VOLMASK,
959                                          ((master_vol+vol) > 0x2A) ? 0x2A :
960                                           (master_vol+vol));
961                 }
962         } else if (!(gpio_data & 0x02)) {
963                 /* mute line out */
964                 snd_hda_codec_amp_stereo(codec,
965                                          spec->autocfg.line_out_pins[0],
966                                          HDA_OUTPUT, 0, HDA_AMP_MUTE,
967                                          HDA_AMP_MUTE);
968         }
969 }
970
971 /* unsolicited event for jack sensing */
972 static void via_unsol_event(struct hda_codec *codec,
973                                   unsigned int res)
974 {
975         res >>= 26;
976         if (res == VIA_HP_EVENT)
977                 via_hp_automute(codec);
978         else if (res == VIA_GPIO_EVENT)
979                 via_gpio_control(codec);
980 }
981
982 static int via_init(struct hda_codec *codec)
983 {
984         struct via_spec *spec = codec->spec;
985         int i;
986         for (i = 0; i < spec->num_iverbs; i++)
987                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
988
989         /* Lydia Add for EAPD enable */
990         if (!spec->dig_in_nid) { /* No Digital In connection */
991                 if (spec->dig_in_pin) {
992                         snd_hda_codec_write(codec, spec->dig_in_pin, 0,
993                                             AC_VERB_SET_PIN_WIDGET_CONTROL,
994                                             PIN_OUT);
995                         snd_hda_codec_write(codec, spec->dig_in_pin, 0,
996                                             AC_VERB_SET_EAPD_BTLENABLE, 0x02);
997                 }
998         } else /* enable SPDIF-input pin */
999                 snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
1000                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
1001
1002         /* assign slave outs */
1003         if (spec->slave_dig_outs[0])
1004                 codec->slave_dig_outs = spec->slave_dig_outs;
1005
1006         return 0;
1007 }
1008
1009 #ifdef CONFIG_SND_HDA_POWER_SAVE
1010 static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1011 {
1012         struct via_spec *spec = codec->spec;
1013         return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1014 }
1015 #endif
1016
1017 /*
1018  */
1019 static struct hda_codec_ops via_patch_ops = {
1020         .build_controls = via_build_controls,
1021         .build_pcms = via_build_pcms,
1022         .init = via_init,
1023         .free = via_free,
1024 #ifdef CONFIG_SND_HDA_POWER_SAVE
1025         .check_power_status = via_check_power_status,
1026 #endif
1027 };
1028
1029 /* fill in the dac_nids table from the parsed pin configuration */
1030 static int vt1708_auto_fill_dac_nids(struct via_spec *spec,
1031                                      const struct auto_pin_cfg *cfg)
1032 {
1033         int i;
1034         hda_nid_t nid;
1035
1036         spec->multiout.num_dacs = cfg->line_outs;
1037
1038         spec->multiout.dac_nids = spec->private_dac_nids;
1039         
1040         for(i = 0; i < 4; i++) {
1041                 nid = cfg->line_out_pins[i];
1042                 if (nid) {
1043                         /* config dac list */
1044                         switch (i) {
1045                         case AUTO_SEQ_FRONT:
1046                                 spec->multiout.dac_nids[i] = 0x10;
1047                                 break;
1048                         case AUTO_SEQ_CENLFE:
1049                                 spec->multiout.dac_nids[i] = 0x12;
1050                                 break;
1051                         case AUTO_SEQ_SURROUND:
1052                                 spec->multiout.dac_nids[i] = 0x11;
1053                                 break;
1054                         case AUTO_SEQ_SIDE:
1055                                 spec->multiout.dac_nids[i] = 0x13;
1056                                 break;
1057                         }
1058                 }
1059         }
1060
1061         return 0;
1062 }
1063
1064 /* add playback controls from the parsed DAC table */
1065 static int vt1708_auto_create_multi_out_ctls(struct via_spec *spec,
1066                                              const struct auto_pin_cfg *cfg)
1067 {
1068         char name[32];
1069         static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
1070         hda_nid_t nid, nid_vol = 0;
1071         int i, err;
1072
1073         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
1074                 nid = cfg->line_out_pins[i];
1075
1076                 if (!nid)
1077                         continue;
1078                 
1079                 if (i != AUTO_SEQ_FRONT)
1080                         nid_vol = 0x18 + i;
1081
1082                 if (i == AUTO_SEQ_CENLFE) {
1083                         /* Center/LFE */
1084                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1085                                         "Center Playback Volume",
1086                                         HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
1087                                                             HDA_OUTPUT));
1088                         if (err < 0)
1089                                 return err;
1090                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1091                                               "LFE Playback Volume",
1092                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
1093                                                                   HDA_OUTPUT));
1094                         if (err < 0)
1095                                 return err;
1096                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1097                                               "Center Playback Switch",
1098                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
1099                                                                   HDA_OUTPUT));
1100                         if (err < 0)
1101                                 return err;
1102                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1103                                               "LFE Playback Switch",
1104                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
1105                                                                   HDA_OUTPUT));
1106                         if (err < 0)
1107                                 return err;
1108                 } else if (i == AUTO_SEQ_FRONT){
1109                         /* add control to mixer index 0 */
1110                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1111                                               "Master Front Playback Volume",
1112                                               HDA_COMPOSE_AMP_VAL(0x17, 3, 0,
1113                                                                   HDA_INPUT));
1114                         if (err < 0)
1115                                 return err;
1116                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1117                                               "Master Front Playback Switch",
1118                                               HDA_COMPOSE_AMP_VAL(0x17, 3, 0,
1119                                                                   HDA_INPUT));
1120                         if (err < 0)
1121                                 return err;
1122                         
1123                         /* add control to PW3 */
1124                         sprintf(name, "%s Playback Volume", chname[i]);
1125                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1126                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1127                                                                   HDA_OUTPUT));
1128                         if (err < 0)
1129                                 return err;
1130                         sprintf(name, "%s Playback Switch", chname[i]);
1131                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1132                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1133                                                                   HDA_OUTPUT));
1134                         if (err < 0)
1135                                 return err;
1136                 } else {
1137                         sprintf(name, "%s Playback Volume", chname[i]);
1138                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1139                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1140                                                                   HDA_OUTPUT));
1141                         if (err < 0)
1142                                 return err;
1143                         sprintf(name, "%s Playback Switch", chname[i]);
1144                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1145                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1146                                                                   HDA_OUTPUT));
1147                         if (err < 0)
1148                                 return err;
1149                 }
1150         }
1151
1152         return 0;
1153 }
1154
1155 static void create_hp_imux(struct via_spec *spec)
1156 {
1157         int i;
1158         struct hda_input_mux *imux = &spec->private_imux[1];
1159         static const char *texts[] = { "OFF", "ON", NULL};
1160
1161         /* for hp mode select */
1162         i = 0;
1163         while (texts[i] != NULL) {
1164                 imux->items[imux->num_items].label =  texts[i];
1165                 imux->items[imux->num_items].index = i;
1166                 imux->num_items++;
1167                 i++;
1168         }
1169
1170         spec->hp_mux = &spec->private_imux[1];
1171 }
1172
1173 static int vt1708_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
1174 {
1175         int err;
1176
1177         if (!pin)
1178                 return 0;
1179
1180         spec->multiout.hp_nid = VT1708_HP_NID; /* AOW3 */
1181
1182         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1183                               "Headphone Playback Volume",
1184                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1185         if (err < 0)
1186                 return err;
1187         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1188                               "Headphone Playback Switch",
1189                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1190         if (err < 0)
1191                 return err;
1192
1193         create_hp_imux(spec);
1194
1195         return 0;
1196 }
1197
1198 /* create playback/capture controls for input pins */
1199 static int vt1708_auto_create_analog_input_ctls(struct via_spec *spec,
1200                                                 const struct auto_pin_cfg *cfg)
1201 {
1202         static char *labels[] = {
1203                 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
1204         };
1205         struct hda_input_mux *imux = &spec->private_imux[0];
1206         int i, err, idx = 0;
1207
1208         /* for internal loopback recording select */
1209         imux->items[imux->num_items].label = "Stereo Mixer";
1210         imux->items[imux->num_items].index = idx;
1211         imux->num_items++;
1212
1213         for (i = 0; i < AUTO_PIN_LAST; i++) {
1214                 if (!cfg->input_pins[i])
1215                         continue;
1216
1217                 switch (cfg->input_pins[i]) {
1218                 case 0x1d: /* Mic */
1219                         idx = 2;
1220                         break;
1221                                 
1222                 case 0x1e: /* Line In */
1223                         idx = 3;
1224                         break;
1225
1226                 case 0x21: /* Front Mic */
1227                         idx = 4;
1228                         break;
1229
1230                 case 0x24: /* CD */
1231                         idx = 1;
1232                         break;
1233                 }
1234                 err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
1235                                            idx, 0x17);
1236                 if (err < 0)
1237                         return err;
1238                 imux->items[imux->num_items].label = labels[i];
1239                 imux->items[imux->num_items].index = idx;
1240                 imux->num_items++;
1241         }
1242         return 0;
1243 }
1244
1245 #ifdef CONFIG_SND_HDA_POWER_SAVE
1246 static struct hda_amp_list vt1708_loopbacks[] = {
1247         { 0x17, HDA_INPUT, 1 },
1248         { 0x17, HDA_INPUT, 2 },
1249         { 0x17, HDA_INPUT, 3 },
1250         { 0x17, HDA_INPUT, 4 },
1251         { } /* end */
1252 };
1253 #endif
1254
1255 static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
1256 {
1257         unsigned int def_conf;
1258         unsigned char seqassoc;
1259
1260         def_conf = snd_hda_codec_get_pincfg(codec, nid);
1261         seqassoc = (unsigned char) get_defcfg_association(def_conf);
1262         seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
1263         if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE) {
1264                 if (seqassoc == 0xff) {
1265                         def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
1266                         snd_hda_codec_set_pincfg(codec, nid, def_conf);
1267                 }
1268         }
1269
1270         return;
1271 }
1272
1273 static int vt1708_parse_auto_config(struct hda_codec *codec)
1274 {
1275         struct via_spec *spec = codec->spec;
1276         int err;
1277
1278         /* Add HP and CD pin config connect bit re-config action */
1279         vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
1280         vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
1281
1282         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1283         if (err < 0)
1284                 return err;
1285         err = vt1708_auto_fill_dac_nids(spec, &spec->autocfg);
1286         if (err < 0)
1287                 return err;
1288         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
1289                 return 0; /* can't find valid BIOS pin config */
1290
1291         err = vt1708_auto_create_multi_out_ctls(spec, &spec->autocfg);
1292         if (err < 0)
1293                 return err;
1294         err = vt1708_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
1295         if (err < 0)
1296                 return err;
1297         err = vt1708_auto_create_analog_input_ctls(spec, &spec->autocfg);
1298         if (err < 0)
1299                 return err;
1300
1301         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
1302
1303         if (spec->autocfg.dig_outs)
1304                 spec->multiout.dig_out_nid = VT1708_DIGOUT_NID;
1305         spec->dig_in_pin = VT1708_DIGIN_PIN;
1306         if (spec->autocfg.dig_in_pin)
1307                 spec->dig_in_nid = VT1708_DIGIN_NID;
1308
1309         if (spec->kctls.list)
1310                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
1311
1312         spec->init_verbs[spec->num_iverbs++] = vt1708_volume_init_verbs;
1313
1314         spec->input_mux = &spec->private_imux[0];
1315
1316         if (spec->hp_mux)
1317                 spec->mixers[spec->num_mixers++] = via_hp_mixer;
1318
1319         return 1;
1320 }
1321
1322 /* init callback for auto-configuration model -- overriding the default init */
1323 static int via_auto_init(struct hda_codec *codec)
1324 {
1325         via_init(codec);
1326         via_auto_init_multi_out(codec);
1327         via_auto_init_hp_out(codec);
1328         via_auto_init_analog_input(codec);
1329         return 0;
1330 }
1331
1332 static int get_mux_nids(struct hda_codec *codec)
1333 {
1334         struct via_spec *spec = codec->spec;
1335         hda_nid_t nid, conn[8];
1336         unsigned int type;
1337         int i, n;
1338
1339         for (i = 0; i < spec->num_adc_nids; i++) {
1340                 nid = spec->adc_nids[i];
1341                 while (nid) {
1342                         type = get_wcaps_type(get_wcaps(codec, nid));
1343                         if (type == AC_WID_PIN)
1344                                 break;
1345                         n = snd_hda_get_connections(codec, nid, conn,
1346                                                     ARRAY_SIZE(conn));
1347                         if (n <= 0)
1348                                 break;
1349                         if (n > 1) {
1350                                 spec->mux_nids[i] = nid;
1351                                 break;
1352                         }
1353                         nid = conn[0];
1354                 }
1355         }
1356         return 0;
1357 }
1358
1359 static int patch_vt1708(struct hda_codec *codec)
1360 {
1361         struct via_spec *spec;
1362         int err;
1363
1364         /* create a codec specific record */
1365         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1366         if (spec == NULL)
1367                 return -ENOMEM;
1368
1369         codec->spec = spec;
1370
1371         /* automatic parse from the BIOS config */
1372         err = vt1708_parse_auto_config(codec);
1373         if (err < 0) {
1374                 via_free(codec);
1375                 return err;
1376         } else if (!err) {
1377                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
1378                        "from BIOS.  Using genenic mode...\n");
1379         }
1380
1381         
1382         spec->stream_name_analog = "VT1708 Analog";
1383         spec->stream_analog_playback = &vt1708_pcm_analog_playback;
1384         /* disable 32bit format on VT1708 */
1385         if (codec->vendor_id == 0x11061708)
1386                 spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
1387         spec->stream_analog_capture = &vt1708_pcm_analog_capture;
1388
1389         spec->stream_name_digital = "VT1708 Digital";
1390         spec->stream_digital_playback = &vt1708_pcm_digital_playback;
1391         spec->stream_digital_capture = &vt1708_pcm_digital_capture;
1392
1393         
1394         if (!spec->adc_nids && spec->input_mux) {
1395                 spec->adc_nids = vt1708_adc_nids;
1396                 spec->num_adc_nids = ARRAY_SIZE(vt1708_adc_nids);
1397                 spec->mixers[spec->num_mixers] = vt1708_capture_mixer;
1398                 spec->num_mixers++;
1399         }
1400
1401         codec->patch_ops = via_patch_ops;
1402
1403         codec->patch_ops.init = via_auto_init;
1404 #ifdef CONFIG_SND_HDA_POWER_SAVE
1405         spec->loopback.amplist = vt1708_loopbacks;
1406 #endif
1407
1408         return 0;
1409 }
1410
1411 /* capture mixer elements */
1412 static struct snd_kcontrol_new vt1709_capture_mixer[] = {
1413         HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x0, HDA_INPUT),
1414         HDA_CODEC_MUTE("Capture Switch", 0x14, 0x0, HDA_INPUT),
1415         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x15, 0x0, HDA_INPUT),
1416         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x15, 0x0, HDA_INPUT),
1417         HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x16, 0x0, HDA_INPUT),
1418         HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x16, 0x0, HDA_INPUT),
1419         {
1420                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1421                 /* The multiple "Capture Source" controls confuse alsamixer
1422                  * So call somewhat different..
1423                  */
1424                 /* .name = "Capture Source", */
1425                 .name = "Input Source",
1426                 .count = 1,
1427                 .info = via_mux_enum_info,
1428                 .get = via_mux_enum_get,
1429                 .put = via_mux_enum_put,
1430         },
1431         { } /* end */
1432 };
1433
1434 static struct hda_verb vt1709_uniwill_init_verbs[] = {
1435         {0x20, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_HP_EVENT},
1436         { }
1437 };
1438
1439 /*
1440  * generic initialization of ADC, input mixers and output mixers
1441  */
1442 static struct hda_verb vt1709_10ch_volume_init_verbs[] = {
1443         /*
1444          * Unmute ADC0-2 and set the default input to mic-in
1445          */
1446         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1447         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1448         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1449
1450
1451         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
1452          * mixer widget
1453          */
1454         /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
1455         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1456         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1457         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
1458         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
1459         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
1460
1461         /*
1462          * Set up output selector (0x1a, 0x1b, 0x29)
1463          */
1464         /* set vol=0 to output mixers */
1465         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1466         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1467         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1468
1469         /*
1470          *  Unmute PW3 and PW4
1471          */
1472         {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1473         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1474
1475         /* Set input of PW4 as AOW4 */
1476         {0x20, AC_VERB_SET_CONNECT_SEL, 0x1},
1477         /* PW9 Output enable */
1478         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
1479         { }
1480 };
1481
1482 static struct hda_pcm_stream vt1709_10ch_pcm_analog_playback = {
1483         .substreams = 1,
1484         .channels_min = 2,
1485         .channels_max = 10,
1486         .nid = 0x10, /* NID to query formats and rates */
1487         .ops = {
1488                 .open = via_playback_pcm_open,
1489                 .prepare = via_playback_pcm_prepare,
1490                 .cleanup = via_playback_pcm_cleanup
1491         },
1492 };
1493
1494 static struct hda_pcm_stream vt1709_6ch_pcm_analog_playback = {
1495         .substreams = 1,
1496         .channels_min = 2,
1497         .channels_max = 6,
1498         .nid = 0x10, /* NID to query formats and rates */
1499         .ops = {
1500                 .open = via_playback_pcm_open,
1501                 .prepare = via_playback_pcm_prepare,
1502                 .cleanup = via_playback_pcm_cleanup
1503         },
1504 };
1505
1506 static struct hda_pcm_stream vt1709_pcm_analog_capture = {
1507         .substreams = 2,
1508         .channels_min = 2,
1509         .channels_max = 2,
1510         .nid = 0x14, /* NID to query formats and rates */
1511         .ops = {
1512                 .prepare = via_capture_pcm_prepare,
1513                 .cleanup = via_capture_pcm_cleanup
1514         },
1515 };
1516
1517 static struct hda_pcm_stream vt1709_pcm_digital_playback = {
1518         .substreams = 1,
1519         .channels_min = 2,
1520         .channels_max = 2,
1521         /* NID is set in via_build_pcms */
1522         .ops = {
1523                 .open = via_dig_playback_pcm_open,
1524                 .close = via_dig_playback_pcm_close
1525         },
1526 };
1527
1528 static struct hda_pcm_stream vt1709_pcm_digital_capture = {
1529         .substreams = 1,
1530         .channels_min = 2,
1531         .channels_max = 2,
1532 };
1533
1534 static int vt1709_auto_fill_dac_nids(struct via_spec *spec,
1535                                      const struct auto_pin_cfg *cfg)
1536 {
1537         int i;
1538         hda_nid_t nid;
1539
1540         if (cfg->line_outs == 4)  /* 10 channels */
1541                 spec->multiout.num_dacs = cfg->line_outs+1; /* AOW0~AOW4 */
1542         else if (cfg->line_outs == 3) /* 6 channels */
1543                 spec->multiout.num_dacs = cfg->line_outs; /* AOW0~AOW2 */
1544
1545         spec->multiout.dac_nids = spec->private_dac_nids;
1546
1547         if (cfg->line_outs == 4) { /* 10 channels */
1548                 for (i = 0; i < cfg->line_outs; i++) {
1549                         nid = cfg->line_out_pins[i];
1550                         if (nid) {
1551                                 /* config dac list */
1552                                 switch (i) {
1553                                 case AUTO_SEQ_FRONT:
1554                                         /* AOW0 */
1555                                         spec->multiout.dac_nids[i] = 0x10;
1556                                         break;
1557                                 case AUTO_SEQ_CENLFE:
1558                                         /* AOW2 */
1559                                         spec->multiout.dac_nids[i] = 0x12;
1560                                         break;
1561                                 case AUTO_SEQ_SURROUND:
1562                                         /* AOW3 */
1563                                         spec->multiout.dac_nids[i] = 0x11;
1564                                         break;
1565                                 case AUTO_SEQ_SIDE:
1566                                         /* AOW1 */
1567                                         spec->multiout.dac_nids[i] = 0x27;
1568                                         break;
1569                                 default:
1570                                         break;
1571                                 }
1572                         }
1573                 }
1574                 spec->multiout.dac_nids[cfg->line_outs] = 0x28; /* AOW4 */
1575
1576         } else if (cfg->line_outs == 3) { /* 6 channels */
1577                 for(i = 0; i < cfg->line_outs; i++) {
1578                         nid = cfg->line_out_pins[i];
1579                         if (nid) {
1580                                 /* config dac list */
1581                                 switch(i) {
1582                                 case AUTO_SEQ_FRONT:
1583                                         /* AOW0 */
1584                                         spec->multiout.dac_nids[i] = 0x10;
1585                                         break;
1586                                 case AUTO_SEQ_CENLFE:
1587                                         /* AOW2 */
1588                                         spec->multiout.dac_nids[i] = 0x12;
1589                                         break;
1590                                 case AUTO_SEQ_SURROUND:
1591                                         /* AOW1 */
1592                                         spec->multiout.dac_nids[i] = 0x11;
1593                                         break;
1594                                 default:
1595                                         break;
1596                                 }
1597                         }
1598                 }
1599         }
1600
1601         return 0;
1602 }
1603
1604 /* add playback controls from the parsed DAC table */
1605 static int vt1709_auto_create_multi_out_ctls(struct via_spec *spec,
1606                                              const struct auto_pin_cfg *cfg)
1607 {
1608         char name[32];
1609         static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
1610         hda_nid_t nid = 0;
1611         int i, err;
1612
1613         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
1614                 nid = cfg->line_out_pins[i];
1615
1616                 if (!nid)       
1617                         continue;
1618
1619                 if (i == AUTO_SEQ_CENLFE) {
1620                         /* Center/LFE */
1621                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1622                                               "Center Playback Volume",
1623                                               HDA_COMPOSE_AMP_VAL(0x1b, 1, 0,
1624                                                                   HDA_OUTPUT));
1625                         if (err < 0)
1626                                 return err;
1627                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1628                                               "LFE Playback Volume",
1629                                               HDA_COMPOSE_AMP_VAL(0x1b, 2, 0,
1630                                                                   HDA_OUTPUT));
1631                         if (err < 0)
1632                                 return err;
1633                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1634                                               "Center Playback Switch",
1635                                               HDA_COMPOSE_AMP_VAL(0x1b, 1, 0,
1636                                                                   HDA_OUTPUT));
1637                         if (err < 0)
1638                                 return err;
1639                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1640                                               "LFE Playback Switch",
1641                                               HDA_COMPOSE_AMP_VAL(0x1b, 2, 0,
1642                                                                   HDA_OUTPUT));
1643                         if (err < 0)
1644                                 return err;
1645                 } else if (i == AUTO_SEQ_FRONT){
1646                         /* add control to mixer index 0 */
1647                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1648                                               "Master Front Playback Volume",
1649                                               HDA_COMPOSE_AMP_VAL(0x18, 3, 0,
1650                                                                   HDA_INPUT));
1651                         if (err < 0)
1652                                 return err;
1653                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1654                                               "Master Front Playback Switch",
1655                                               HDA_COMPOSE_AMP_VAL(0x18, 3, 0,
1656                                                                   HDA_INPUT));
1657                         if (err < 0)
1658                                 return err;
1659                         
1660                         /* add control to PW3 */
1661                         sprintf(name, "%s Playback Volume", chname[i]);
1662                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1663                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1664                                                                   HDA_OUTPUT));
1665                         if (err < 0)
1666                                 return err;
1667                         sprintf(name, "%s Playback Switch", chname[i]);
1668                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1669                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1670                                                                   HDA_OUTPUT));
1671                         if (err < 0)
1672                                 return err;
1673                 } else if (i == AUTO_SEQ_SURROUND) {
1674                         sprintf(name, "%s Playback Volume", chname[i]);
1675                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1676                                               HDA_COMPOSE_AMP_VAL(0x1a, 3, 0,
1677                                                                   HDA_OUTPUT));
1678                         if (err < 0)
1679                                 return err;
1680                         sprintf(name, "%s Playback Switch", chname[i]);
1681                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1682                                               HDA_COMPOSE_AMP_VAL(0x1a, 3, 0,
1683                                                                   HDA_OUTPUT));
1684                         if (err < 0)
1685                                 return err;
1686                 } else if (i == AUTO_SEQ_SIDE) {
1687                         sprintf(name, "%s Playback Volume", chname[i]);
1688                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1689                                               HDA_COMPOSE_AMP_VAL(0x29, 3, 0,
1690                                                                   HDA_OUTPUT));
1691                         if (err < 0)
1692                                 return err;
1693                         sprintf(name, "%s Playback Switch", chname[i]);
1694                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1695                                               HDA_COMPOSE_AMP_VAL(0x29, 3, 0,
1696                                                                   HDA_OUTPUT));
1697                         if (err < 0)
1698                                 return err;
1699                 }
1700         }
1701
1702         return 0;
1703 }
1704
1705 static int vt1709_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
1706 {
1707         int err;
1708
1709         if (!pin)
1710                 return 0;
1711
1712         if (spec->multiout.num_dacs == 5) /* 10 channels */
1713                 spec->multiout.hp_nid = VT1709_HP_DAC_NID;
1714         else if (spec->multiout.num_dacs == 3) /* 6 channels */
1715                 spec->multiout.hp_nid = 0;
1716
1717         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1718                               "Headphone Playback Volume",
1719                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1720         if (err < 0)
1721                 return err;
1722         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1723                               "Headphone Playback Switch",
1724                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1725         if (err < 0)
1726                 return err;
1727
1728         return 0;
1729 }
1730
1731 /* create playback/capture controls for input pins */
1732 static int vt1709_auto_create_analog_input_ctls(struct via_spec *spec,
1733                                                 const struct auto_pin_cfg *cfg)
1734 {
1735         static char *labels[] = {
1736                 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
1737         };
1738         struct hda_input_mux *imux = &spec->private_imux[0];
1739         int i, err, idx = 0;
1740
1741         /* for internal loopback recording select */
1742         imux->items[imux->num_items].label = "Stereo Mixer";
1743         imux->items[imux->num_items].index = idx;
1744         imux->num_items++;
1745
1746         for (i = 0; i < AUTO_PIN_LAST; i++) {
1747                 if (!cfg->input_pins[i])
1748                         continue;
1749
1750                 switch (cfg->input_pins[i]) {
1751                 case 0x1d: /* Mic */
1752                         idx = 2;
1753                         break;
1754                                 
1755                 case 0x1e: /* Line In */
1756                         idx = 3;
1757                         break;
1758
1759                 case 0x21: /* Front Mic */
1760                         idx = 4;
1761                         break;
1762
1763                 case 0x23: /* CD */
1764                         idx = 1;
1765                         break;
1766                 }
1767                 err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
1768                                            idx, 0x18);
1769                 if (err < 0)
1770                         return err;
1771                 imux->items[imux->num_items].label = labels[i];
1772                 imux->items[imux->num_items].index = idx;
1773                 imux->num_items++;
1774         }
1775         return 0;
1776 }
1777
1778 static int vt1709_parse_auto_config(struct hda_codec *codec)
1779 {
1780         struct via_spec *spec = codec->spec;
1781         int err;
1782
1783         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1784         if (err < 0)
1785                 return err;
1786         err = vt1709_auto_fill_dac_nids(spec, &spec->autocfg);
1787         if (err < 0)
1788                 return err;
1789         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
1790                 return 0; /* can't find valid BIOS pin config */
1791
1792         err = vt1709_auto_create_multi_out_ctls(spec, &spec->autocfg);
1793         if (err < 0)
1794                 return err;
1795         err = vt1709_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
1796         if (err < 0)
1797                 return err;
1798         err = vt1709_auto_create_analog_input_ctls(spec, &spec->autocfg);
1799         if (err < 0)
1800                 return err;
1801
1802         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
1803
1804         if (spec->autocfg.dig_outs)
1805                 spec->multiout.dig_out_nid = VT1709_DIGOUT_NID;
1806         spec->dig_in_pin = VT1709_DIGIN_PIN;
1807         if (spec->autocfg.dig_in_pin)
1808                 spec->dig_in_nid = VT1709_DIGIN_NID;
1809
1810         if (spec->kctls.list)
1811                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
1812
1813         spec->input_mux = &spec->private_imux[0];
1814
1815         if (spec->hp_mux)
1816                 spec->mixers[spec->num_mixers++] = via_hp_mixer;
1817
1818         return 1;
1819 }
1820
1821 #ifdef CONFIG_SND_HDA_POWER_SAVE
1822 static struct hda_amp_list vt1709_loopbacks[] = {
1823         { 0x18, HDA_INPUT, 1 },
1824         { 0x18, HDA_INPUT, 2 },
1825         { 0x18, HDA_INPUT, 3 },
1826         { 0x18, HDA_INPUT, 4 },
1827         { } /* end */
1828 };
1829 #endif
1830
1831 static int patch_vt1709_10ch(struct hda_codec *codec)
1832 {
1833         struct via_spec *spec;
1834         int err;
1835
1836         /* create a codec specific record */
1837         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1838         if (spec == NULL)
1839                 return -ENOMEM;
1840
1841         codec->spec = spec;
1842
1843         err = vt1709_parse_auto_config(codec);
1844         if (err < 0) {
1845                 via_free(codec);
1846                 return err;
1847         } else if (!err) {
1848                 printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
1849                        "Using genenic mode...\n");
1850         }
1851
1852         spec->init_verbs[spec->num_iverbs++] = vt1709_10ch_volume_init_verbs;
1853         spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
1854
1855         spec->stream_name_analog = "VT1709 Analog";
1856         spec->stream_analog_playback = &vt1709_10ch_pcm_analog_playback;
1857         spec->stream_analog_capture = &vt1709_pcm_analog_capture;
1858
1859         spec->stream_name_digital = "VT1709 Digital";
1860         spec->stream_digital_playback = &vt1709_pcm_digital_playback;
1861         spec->stream_digital_capture = &vt1709_pcm_digital_capture;
1862
1863         
1864         if (!spec->adc_nids && spec->input_mux) {
1865                 spec->adc_nids = vt1709_adc_nids;
1866                 spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
1867                 get_mux_nids(codec);
1868                 spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
1869                 spec->num_mixers++;
1870         }
1871
1872         codec->patch_ops = via_patch_ops;
1873
1874         codec->patch_ops.init = via_auto_init;
1875         codec->patch_ops.unsol_event = via_unsol_event;
1876 #ifdef CONFIG_SND_HDA_POWER_SAVE
1877         spec->loopback.amplist = vt1709_loopbacks;
1878 #endif
1879
1880         return 0;
1881 }
1882 /*
1883  * generic initialization of ADC, input mixers and output mixers
1884  */
1885 static struct hda_verb vt1709_6ch_volume_init_verbs[] = {
1886         /*
1887          * Unmute ADC0-2 and set the default input to mic-in
1888          */
1889         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1890         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1891         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1892
1893
1894         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
1895          * mixer widget
1896          */
1897         /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
1898         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1899         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1900         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
1901         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
1902         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
1903
1904         /*
1905          * Set up output selector (0x1a, 0x1b, 0x29)
1906          */
1907         /* set vol=0 to output mixers */
1908         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1909         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1910         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1911
1912         /*
1913          *  Unmute PW3 and PW4
1914          */
1915         {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1916         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1917
1918         /* Set input of PW4 as MW0 */
1919         {0x20, AC_VERB_SET_CONNECT_SEL, 0},
1920         /* PW9 Output enable */
1921         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
1922         { }
1923 };
1924
1925 static int patch_vt1709_6ch(struct hda_codec *codec)
1926 {
1927         struct via_spec *spec;
1928         int err;
1929
1930         /* create a codec specific record */
1931         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1932         if (spec == NULL)
1933                 return -ENOMEM;
1934
1935         codec->spec = spec;
1936
1937         err = vt1709_parse_auto_config(codec);
1938         if (err < 0) {
1939                 via_free(codec);
1940                 return err;
1941         } else if (!err) {
1942                 printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
1943                        "Using genenic mode...\n");
1944         }
1945
1946         spec->init_verbs[spec->num_iverbs++] = vt1709_6ch_volume_init_verbs;
1947         spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
1948
1949         spec->stream_name_analog = "VT1709 Analog";
1950         spec->stream_analog_playback = &vt1709_6ch_pcm_analog_playback;
1951         spec->stream_analog_capture = &vt1709_pcm_analog_capture;
1952
1953         spec->stream_name_digital = "VT1709 Digital";
1954         spec->stream_digital_playback = &vt1709_pcm_digital_playback;
1955         spec->stream_digital_capture = &vt1709_pcm_digital_capture;
1956
1957         
1958         if (!spec->adc_nids && spec->input_mux) {
1959                 spec->adc_nids = vt1709_adc_nids;
1960                 spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
1961                 get_mux_nids(codec);
1962                 spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
1963                 spec->num_mixers++;
1964         }
1965
1966         codec->patch_ops = via_patch_ops;
1967
1968         codec->patch_ops.init = via_auto_init;
1969         codec->patch_ops.unsol_event = via_unsol_event;
1970 #ifdef CONFIG_SND_HDA_POWER_SAVE
1971         spec->loopback.amplist = vt1709_loopbacks;
1972 #endif
1973         return 0;
1974 }
1975
1976 /* capture mixer elements */
1977 static struct snd_kcontrol_new vt1708B_capture_mixer[] = {
1978         HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
1979         HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
1980         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
1981         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
1982         {
1983                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1984                 /* The multiple "Capture Source" controls confuse alsamixer
1985                  * So call somewhat different..
1986                  */
1987                 /* .name = "Capture Source", */
1988                 .name = "Input Source",
1989                 .count = 1,
1990                 .info = via_mux_enum_info,
1991                 .get = via_mux_enum_get,
1992                 .put = via_mux_enum_put,
1993         },
1994         { } /* end */
1995 };
1996 /*
1997  * generic initialization of ADC, input mixers and output mixers
1998  */
1999 static struct hda_verb vt1708B_8ch_volume_init_verbs[] = {
2000         /*
2001          * Unmute ADC0-1 and set the default input to mic-in
2002          */
2003         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2004         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2005
2006
2007         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2008          * mixer widget
2009          */
2010         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2011         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2012         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2013         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2014         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2015         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2016
2017         /*
2018          * Set up output mixers
2019          */
2020         /* set vol=0 to output mixers */
2021         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2022         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2023         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2024
2025         /* Setup default input to PW4 */
2026         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x1},
2027         /* PW9 Output enable */
2028         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2029         /* PW10 Input enable */
2030         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2031         { }
2032 };
2033
2034 static struct hda_verb vt1708B_4ch_volume_init_verbs[] = {
2035         /*
2036          * Unmute ADC0-1 and set the default input to mic-in
2037          */
2038         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2039         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2040
2041
2042         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2043          * mixer widget
2044          */
2045         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2046         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2047         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2048         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2049         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2050         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2051
2052         /*
2053          * Set up output mixers
2054          */
2055         /* set vol=0 to output mixers */
2056         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2057         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2058         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2059
2060         /* Setup default input of PW4 to MW0 */
2061         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
2062         /* PW9 Output enable */
2063         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2064         /* PW10 Input enable */
2065         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2066         { }
2067 };
2068
2069 static struct hda_verb vt1708B_uniwill_init_verbs[] = {
2070         {0x1D, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_HP_EVENT},
2071         { }
2072 };
2073
2074 static struct hda_pcm_stream vt1708B_8ch_pcm_analog_playback = {
2075         .substreams = 2,
2076         .channels_min = 2,
2077         .channels_max = 8,
2078         .nid = 0x10, /* NID to query formats and rates */
2079         .ops = {
2080                 .open = via_playback_pcm_open,
2081                 .prepare = via_playback_multi_pcm_prepare,
2082                 .cleanup = via_playback_multi_pcm_cleanup
2083         },
2084 };
2085
2086 static struct hda_pcm_stream vt1708B_4ch_pcm_analog_playback = {
2087         .substreams = 2,
2088         .channels_min = 2,
2089         .channels_max = 4,
2090         .nid = 0x10, /* NID to query formats and rates */
2091         .ops = {
2092                 .open = via_playback_pcm_open,
2093                 .prepare = via_playback_multi_pcm_prepare,
2094                 .cleanup = via_playback_multi_pcm_cleanup
2095         },
2096 };
2097
2098 static struct hda_pcm_stream vt1708B_pcm_analog_capture = {
2099         .substreams = 2,
2100         .channels_min = 2,
2101         .channels_max = 2,
2102         .nid = 0x13, /* NID to query formats and rates */
2103         .ops = {
2104                 .prepare = via_capture_pcm_prepare,
2105                 .cleanup = via_capture_pcm_cleanup
2106         },
2107 };
2108
2109 static struct hda_pcm_stream vt1708B_pcm_digital_playback = {
2110         .substreams = 1,
2111         .channels_min = 2,
2112         .channels_max = 2,
2113         /* NID is set in via_build_pcms */
2114         .ops = {
2115                 .open = via_dig_playback_pcm_open,
2116                 .close = via_dig_playback_pcm_close,
2117                 .prepare = via_dig_playback_pcm_prepare,
2118                 .cleanup = via_dig_playback_pcm_cleanup
2119         },
2120 };
2121
2122 static struct hda_pcm_stream vt1708B_pcm_digital_capture = {
2123         .substreams = 1,
2124         .channels_min = 2,
2125         .channels_max = 2,
2126 };
2127
2128 /* fill in the dac_nids table from the parsed pin configuration */
2129 static int vt1708B_auto_fill_dac_nids(struct via_spec *spec,
2130                                      const struct auto_pin_cfg *cfg)
2131 {
2132         int i;
2133         hda_nid_t nid;
2134
2135         spec->multiout.num_dacs = cfg->line_outs;
2136
2137         spec->multiout.dac_nids = spec->private_dac_nids;
2138
2139         for (i = 0; i < 4; i++) {
2140                 nid = cfg->line_out_pins[i];
2141                 if (nid) {
2142                         /* config dac list */
2143                         switch (i) {
2144                         case AUTO_SEQ_FRONT:
2145                                 spec->multiout.dac_nids[i] = 0x10;
2146                                 break;
2147                         case AUTO_SEQ_CENLFE:
2148                                 spec->multiout.dac_nids[i] = 0x24;
2149                                 break;
2150                         case AUTO_SEQ_SURROUND:
2151                                 spec->multiout.dac_nids[i] = 0x11;
2152                                 break;
2153                         case AUTO_SEQ_SIDE:
2154                                 spec->multiout.dac_nids[i] = 0x25;
2155                                 break;
2156                         }
2157                 }
2158         }
2159
2160         return 0;
2161 }
2162
2163 /* add playback controls from the parsed DAC table */
2164 static int vt1708B_auto_create_multi_out_ctls(struct via_spec *spec,
2165                                              const struct auto_pin_cfg *cfg)
2166 {
2167         char name[32];
2168         static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
2169         hda_nid_t nid_vols[] = {0x16, 0x18, 0x26, 0x27};
2170         hda_nid_t nid, nid_vol = 0;
2171         int i, err;
2172
2173         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
2174                 nid = cfg->line_out_pins[i];
2175
2176                 if (!nid)
2177                         continue;
2178
2179                 nid_vol = nid_vols[i];
2180
2181                 if (i == AUTO_SEQ_CENLFE) {
2182                         /* Center/LFE */
2183                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2184                                               "Center Playback Volume",
2185                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2186                                                                   HDA_OUTPUT));
2187                         if (err < 0)
2188                                 return err;
2189                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2190                                               "LFE Playback Volume",
2191                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2192                                                                   HDA_OUTPUT));
2193                         if (err < 0)
2194                                 return err;
2195                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2196                                               "Center Playback Switch",
2197                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2198                                                                   HDA_OUTPUT));
2199                         if (err < 0)
2200                                 return err;
2201                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2202                                               "LFE Playback Switch",
2203                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2204                                                                   HDA_OUTPUT));
2205                         if (err < 0)
2206                                 return err;
2207                 } else if (i == AUTO_SEQ_FRONT) {
2208                         /* add control to mixer index 0 */
2209                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2210                                               "Master Front Playback Volume",
2211                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2212                                                                   HDA_INPUT));
2213                         if (err < 0)
2214                                 return err;
2215                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2216                                               "Master Front Playback Switch",
2217                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2218                                                                   HDA_INPUT));
2219                         if (err < 0)
2220                                 return err;
2221
2222                         /* add control to PW3 */
2223                         sprintf(name, "%s Playback Volume", chname[i]);
2224                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2225                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2226                                                                   HDA_OUTPUT));
2227                         if (err < 0)
2228                                 return err;
2229                         sprintf(name, "%s Playback Switch", chname[i]);
2230                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2231                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2232                                                                   HDA_OUTPUT));
2233                         if (err < 0)
2234                                 return err;
2235                 } else {
2236                         sprintf(name, "%s Playback Volume", chname[i]);
2237                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2238                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2239                                                                   HDA_OUTPUT));
2240                         if (err < 0)
2241                                 return err;
2242                         sprintf(name, "%s Playback Switch", chname[i]);
2243                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2244                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2245                                                                   HDA_OUTPUT));
2246                         if (err < 0)
2247                                 return err;
2248                 }
2249         }
2250
2251         return 0;
2252 }
2253
2254 static int vt1708B_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
2255 {
2256         int err;
2257
2258         if (!pin)
2259                 return 0;
2260
2261         spec->multiout.hp_nid = VT1708B_HP_NID; /* AOW3 */
2262
2263         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2264                               "Headphone Playback Volume",
2265                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2266         if (err < 0)
2267                 return err;
2268         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2269                               "Headphone Playback Switch",
2270                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2271         if (err < 0)
2272                 return err;
2273
2274         create_hp_imux(spec);
2275
2276         return 0;
2277 }
2278
2279 /* create playback/capture controls for input pins */
2280 static int vt1708B_auto_create_analog_input_ctls(struct via_spec *spec,
2281                                                 const struct auto_pin_cfg *cfg)
2282 {
2283         static char *labels[] = {
2284                 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
2285         };
2286         struct hda_input_mux *imux = &spec->private_imux[0];
2287         int i, err, idx = 0;
2288
2289         /* for internal loopback recording select */
2290         imux->items[imux->num_items].label = "Stereo Mixer";
2291         imux->items[imux->num_items].index = idx;
2292         imux->num_items++;
2293
2294         for (i = 0; i < AUTO_PIN_LAST; i++) {
2295                 if (!cfg->input_pins[i])
2296                         continue;
2297
2298                 switch (cfg->input_pins[i]) {
2299                 case 0x1a: /* Mic */
2300                         idx = 2;
2301                         break;
2302
2303                 case 0x1b: /* Line In */
2304                         idx = 3;
2305                         break;
2306
2307                 case 0x1e: /* Front Mic */
2308                         idx = 4;
2309                         break;
2310
2311                 case 0x1f: /* CD */
2312                         idx = 1;
2313                         break;
2314                 }
2315                 err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
2316                                            idx, 0x16);
2317                 if (err < 0)
2318                         return err;
2319                 imux->items[imux->num_items].label = labels[i];
2320                 imux->items[imux->num_items].index = idx;
2321                 imux->num_items++;
2322         }
2323         return 0;
2324 }
2325
2326 static int vt1708B_parse_auto_config(struct hda_codec *codec)
2327 {
2328         struct via_spec *spec = codec->spec;
2329         int err;
2330
2331         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2332         if (err < 0)
2333                 return err;
2334         err = vt1708B_auto_fill_dac_nids(spec, &spec->autocfg);
2335         if (err < 0)
2336                 return err;
2337         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2338                 return 0; /* can't find valid BIOS pin config */
2339
2340         err = vt1708B_auto_create_multi_out_ctls(spec, &spec->autocfg);
2341         if (err < 0)
2342                 return err;
2343         err = vt1708B_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2344         if (err < 0)
2345                 return err;
2346         err = vt1708B_auto_create_analog_input_ctls(spec, &spec->autocfg);
2347         if (err < 0)
2348                 return err;
2349
2350         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2351
2352         if (spec->autocfg.dig_outs)
2353                 spec->multiout.dig_out_nid = VT1708B_DIGOUT_NID;
2354         spec->dig_in_pin = VT1708B_DIGIN_PIN;
2355         if (spec->autocfg.dig_in_pin)
2356                 spec->dig_in_nid = VT1708B_DIGIN_NID;
2357
2358         if (spec->kctls.list)
2359                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
2360
2361         spec->input_mux = &spec->private_imux[0];
2362
2363         if (spec->hp_mux)
2364                 spec->mixers[spec->num_mixers++] = via_hp_mixer;
2365
2366         return 1;
2367 }
2368
2369 #ifdef CONFIG_SND_HDA_POWER_SAVE
2370 static struct hda_amp_list vt1708B_loopbacks[] = {
2371         { 0x16, HDA_INPUT, 1 },
2372         { 0x16, HDA_INPUT, 2 },
2373         { 0x16, HDA_INPUT, 3 },
2374         { 0x16, HDA_INPUT, 4 },
2375         { } /* end */
2376 };
2377 #endif
2378
2379 static int patch_vt1708B_8ch(struct hda_codec *codec)
2380 {
2381         struct via_spec *spec;
2382         int err;
2383
2384         /* create a codec specific record */
2385         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2386         if (spec == NULL)
2387                 return -ENOMEM;
2388
2389         codec->spec = spec;
2390
2391         /* automatic parse from the BIOS config */
2392         err = vt1708B_parse_auto_config(codec);
2393         if (err < 0) {
2394                 via_free(codec);
2395                 return err;
2396         } else if (!err) {
2397                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
2398                        "from BIOS.  Using genenic mode...\n");
2399         }
2400
2401         spec->init_verbs[spec->num_iverbs++] = vt1708B_8ch_volume_init_verbs;
2402         spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
2403
2404         spec->stream_name_analog = "VT1708B Analog";
2405         spec->stream_analog_playback = &vt1708B_8ch_pcm_analog_playback;
2406         spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
2407
2408         spec->stream_name_digital = "VT1708B Digital";
2409         spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
2410         spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
2411
2412         if (!spec->adc_nids && spec->input_mux) {
2413                 spec->adc_nids = vt1708B_adc_nids;
2414                 spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
2415                 get_mux_nids(codec);
2416                 spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
2417                 spec->num_mixers++;
2418         }
2419
2420         codec->patch_ops = via_patch_ops;
2421
2422         codec->patch_ops.init = via_auto_init;
2423         codec->patch_ops.unsol_event = via_unsol_event;
2424 #ifdef CONFIG_SND_HDA_POWER_SAVE
2425         spec->loopback.amplist = vt1708B_loopbacks;
2426 #endif
2427
2428         return 0;
2429 }
2430
2431 static int patch_vt1708B_4ch(struct hda_codec *codec)
2432 {
2433         struct via_spec *spec;
2434         int err;
2435
2436         /* create a codec specific record */
2437         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2438         if (spec == NULL)
2439                 return -ENOMEM;
2440
2441         codec->spec = spec;
2442
2443         /* automatic parse from the BIOS config */
2444         err = vt1708B_parse_auto_config(codec);
2445         if (err < 0) {
2446                 via_free(codec);
2447                 return err;
2448         } else if (!err) {
2449                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
2450                        "from BIOS.  Using genenic mode...\n");
2451         }
2452
2453         spec->init_verbs[spec->num_iverbs++] = vt1708B_4ch_volume_init_verbs;
2454         spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
2455
2456         spec->stream_name_analog = "VT1708B Analog";
2457         spec->stream_analog_playback = &vt1708B_4ch_pcm_analog_playback;
2458         spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
2459
2460         spec->stream_name_digital = "VT1708B Digital";
2461         spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
2462         spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
2463
2464         if (!spec->adc_nids && spec->input_mux) {
2465                 spec->adc_nids = vt1708B_adc_nids;
2466                 spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
2467                 get_mux_nids(codec);
2468                 spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
2469                 spec->num_mixers++;
2470         }
2471
2472         codec->patch_ops = via_patch_ops;
2473
2474         codec->patch_ops.init = via_auto_init;
2475         codec->patch_ops.unsol_event = via_unsol_event;
2476 #ifdef CONFIG_SND_HDA_POWER_SAVE
2477         spec->loopback.amplist = vt1708B_loopbacks;
2478 #endif
2479
2480         return 0;
2481 }
2482
2483 /* Patch for VT1708S */
2484
2485 /* VT1708S software backdoor based override for buggy hardware micboost
2486  * setting */
2487 #define MIC_BOOST_VOLUME(xname, nid) {                          \
2488         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,            \
2489         .name = xname,                                  \
2490         .index = 0,                                     \
2491         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |     \
2492         SNDRV_CTL_ELEM_ACCESS_TLV_READ |                \
2493         SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,             \
2494         .info = mic_boost_volume_info,                  \
2495         .get = snd_hda_mixer_amp_volume_get,            \
2496         .put = snd_hda_mixer_amp_volume_put,            \
2497         .tlv = { .c = mic_boost_tlv },                  \
2498         .private_value = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT) }
2499
2500 /* capture mixer elements */
2501 static struct snd_kcontrol_new vt1708S_capture_mixer[] = {
2502         HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
2503         HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
2504         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
2505         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
2506         MIC_BOOST_VOLUME("Mic Boost Capture Volume", 0x1A),
2507         MIC_BOOST_VOLUME("Front Mic Boost Capture Volume", 0x1E),
2508         {
2509                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2510                 /* The multiple "Capture Source" controls confuse alsamixer
2511                  * So call somewhat different..
2512                  */
2513                 /* .name = "Capture Source", */
2514                 .name = "Input Source",
2515                 .count = 1,
2516                 .info = via_mux_enum_info,
2517                 .get = via_mux_enum_get,
2518                 .put = via_mux_enum_put,
2519         },
2520         { } /* end */
2521 };
2522
2523 static struct hda_verb vt1708S_volume_init_verbs[] = {
2524         /* Unmute ADC0-1 and set the default input to mic-in */
2525         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2526         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2527
2528         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the
2529          * analog-loopback mixer widget */
2530         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2531         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2532         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2533         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2534         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2535         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2536
2537         /* Setup default input of PW4 to MW0 */
2538         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
2539         /* PW9, PW10  Output enable */
2540         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2541         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2542         /* Enable Mic Boost Volume backdoor */
2543         {0x1, 0xf98, 0x1},
2544         { }
2545 };
2546
2547 static struct hda_verb vt1708S_uniwill_init_verbs[] = {
2548         {0x1D, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_HP_EVENT},
2549         { }
2550 };
2551
2552 static struct hda_pcm_stream vt1708S_pcm_analog_playback = {
2553         .substreams = 2,
2554         .channels_min = 2,
2555         .channels_max = 8,
2556         .nid = 0x10, /* NID to query formats and rates */
2557         .ops = {
2558                 .open = via_playback_pcm_open,
2559                 .prepare = via_playback_pcm_prepare,
2560                 .cleanup = via_playback_pcm_cleanup
2561         },
2562 };
2563
2564 static struct hda_pcm_stream vt1708S_pcm_analog_capture = {
2565         .substreams = 2,
2566         .channels_min = 2,
2567         .channels_max = 2,
2568         .nid = 0x13, /* NID to query formats and rates */
2569         .ops = {
2570                 .prepare = via_capture_pcm_prepare,
2571                 .cleanup = via_capture_pcm_cleanup
2572         },
2573 };
2574
2575 static struct hda_pcm_stream vt1708S_pcm_digital_playback = {
2576         .substreams = 1,
2577         .channels_min = 2,
2578         .channels_max = 2,
2579         /* NID is set in via_build_pcms */
2580         .ops = {
2581                 .open = via_dig_playback_pcm_open,
2582                 .close = via_dig_playback_pcm_close,
2583                 .prepare = via_dig_playback_pcm_prepare,
2584                 .cleanup = via_dig_playback_pcm_cleanup
2585         },
2586 };
2587
2588 /* fill in the dac_nids table from the parsed pin configuration */
2589 static int vt1708S_auto_fill_dac_nids(struct via_spec *spec,
2590                                      const struct auto_pin_cfg *cfg)
2591 {
2592         int i;
2593         hda_nid_t nid;
2594
2595         spec->multiout.num_dacs = cfg->line_outs;
2596
2597         spec->multiout.dac_nids = spec->private_dac_nids;
2598
2599         for (i = 0; i < 4; i++) {
2600                 nid = cfg->line_out_pins[i];
2601                 if (nid) {
2602                         /* config dac list */
2603                         switch (i) {
2604                         case AUTO_SEQ_FRONT:
2605                                 spec->multiout.dac_nids[i] = 0x10;
2606                                 break;
2607                         case AUTO_SEQ_CENLFE:
2608                                 spec->multiout.dac_nids[i] = 0x24;
2609                                 break;
2610                         case AUTO_SEQ_SURROUND:
2611                                 spec->multiout.dac_nids[i] = 0x11;
2612                                 break;
2613                         case AUTO_SEQ_SIDE:
2614                                 spec->multiout.dac_nids[i] = 0x25;
2615                                 break;
2616                         }
2617                 }
2618         }
2619
2620         return 0;
2621 }
2622
2623 /* add playback controls from the parsed DAC table */
2624 static int vt1708S_auto_create_multi_out_ctls(struct via_spec *spec,
2625                                              const struct auto_pin_cfg *cfg)
2626 {
2627         char name[32];
2628         static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
2629         hda_nid_t nid_vols[] = {0x10, 0x11, 0x24, 0x25};
2630         hda_nid_t nid_mutes[] = {0x1C, 0x18, 0x26, 0x27};
2631         hda_nid_t nid, nid_vol, nid_mute;
2632         int i, err;
2633
2634         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
2635                 nid = cfg->line_out_pins[i];
2636
2637                 if (!nid)
2638                         continue;
2639
2640                 nid_vol = nid_vols[i];
2641                 nid_mute = nid_mutes[i];
2642
2643                 if (i == AUTO_SEQ_CENLFE) {
2644                         /* Center/LFE */
2645                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2646                                               "Center Playback Volume",
2647                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2648                                                                   HDA_OUTPUT));
2649                         if (err < 0)
2650                                 return err;
2651                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2652                                               "LFE Playback Volume",
2653                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2654                                                                   HDA_OUTPUT));
2655                         if (err < 0)
2656                                 return err;
2657                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2658                                               "Center Playback Switch",
2659                                               HDA_COMPOSE_AMP_VAL(nid_mute,
2660                                                                   1, 0,
2661                                                                   HDA_OUTPUT));
2662                         if (err < 0)
2663                                 return err;
2664                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2665                                               "LFE Playback Switch",
2666                                               HDA_COMPOSE_AMP_VAL(nid_mute,
2667                                                                   2, 0,
2668                                                                   HDA_OUTPUT));
2669                         if (err < 0)
2670                                 return err;
2671                 } else if (i == AUTO_SEQ_FRONT) {
2672                         /* add control to mixer index 0 */
2673                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2674                                               "Master Front Playback Volume",
2675                                               HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
2676                                                                   HDA_INPUT));
2677                         if (err < 0)
2678                                 return err;
2679                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2680                                               "Master Front Playback Switch",
2681                                               HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
2682                                                                   HDA_INPUT));
2683                         if (err < 0)
2684                                 return err;
2685
2686                         /* Front */
2687                         sprintf(name, "%s Playback Volume", chname[i]);
2688                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2689                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2690                                                                   HDA_OUTPUT));
2691                         if (err < 0)
2692                                 return err;
2693                         sprintf(name, "%s Playback Switch", chname[i]);
2694                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2695                                               HDA_COMPOSE_AMP_VAL(nid_mute,
2696                                                                   3, 0,
2697                                                                   HDA_OUTPUT));
2698                         if (err < 0)
2699                                 return err;
2700                 } else {
2701                         sprintf(name, "%s Playback Volume", chname[i]);
2702                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2703                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2704                                                                   HDA_OUTPUT));
2705                         if (err < 0)
2706                                 return err;
2707                         sprintf(name, "%s Playback Switch", chname[i]);
2708                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2709                                               HDA_COMPOSE_AMP_VAL(nid_mute,
2710                                                                   3, 0,
2711                                                                   HDA_OUTPUT));
2712                         if (err < 0)
2713                                 return err;
2714                 }
2715         }
2716
2717         return 0;
2718 }
2719
2720 static int vt1708S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
2721 {
2722         int err;
2723
2724         if (!pin)
2725                 return 0;
2726
2727         spec->multiout.hp_nid = VT1708S_HP_NID; /* AOW3 */
2728
2729         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2730                               "Headphone Playback Volume",
2731                               HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
2732         if (err < 0)
2733                 return err;
2734
2735         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2736                               "Headphone Playback Switch",
2737                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2738         if (err < 0)
2739                 return err;
2740
2741         create_hp_imux(spec);
2742
2743         return 0;
2744 }
2745
2746 /* create playback/capture controls for input pins */
2747 static int vt1708S_auto_create_analog_input_ctls(struct via_spec *spec,
2748                                                 const struct auto_pin_cfg *cfg)
2749 {
2750         static char *labels[] = {
2751                 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
2752         };
2753         struct hda_input_mux *imux = &spec->private_imux[0];
2754         int i, err, idx = 0;
2755
2756         /* for internal loopback recording select */
2757         imux->items[imux->num_items].label = "Stereo Mixer";
2758         imux->items[imux->num_items].index = 5;
2759         imux->num_items++;
2760
2761         for (i = 0; i < AUTO_PIN_LAST; i++) {
2762                 if (!cfg->input_pins[i])
2763                         continue;
2764
2765                 switch (cfg->input_pins[i]) {
2766                 case 0x1a: /* Mic */
2767                         idx = 2;
2768                         break;
2769
2770                 case 0x1b: /* Line In */
2771                         idx = 3;
2772                         break;
2773
2774                 case 0x1e: /* Front Mic */
2775                         idx = 4;
2776                         break;
2777
2778                 case 0x1f: /* CD */
2779                         idx = 1;
2780                         break;
2781                 }
2782                 err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
2783                                            idx, 0x16);
2784                 if (err < 0)
2785                         return err;
2786                 imux->items[imux->num_items].label = labels[i];
2787                 imux->items[imux->num_items].index = idx-1;
2788                 imux->num_items++;
2789         }
2790         return 0;
2791 }
2792
2793 /* fill out digital output widgets; one for master and one for slave outputs */
2794 static void fill_dig_outs(struct hda_codec *codec)
2795 {
2796         struct via_spec *spec = codec->spec;
2797         int i;
2798
2799         for (i = 0; i < spec->autocfg.dig_outs; i++) {
2800                 hda_nid_t nid;
2801                 int conn;
2802
2803                 nid = spec->autocfg.dig_out_pins[i];
2804                 if (!nid)
2805                         continue;
2806                 conn = snd_hda_get_connections(codec, nid, &nid, 1);
2807                 if (conn < 1)
2808                         continue;
2809                 if (!spec->multiout.dig_out_nid)
2810                         spec->multiout.dig_out_nid = nid;
2811                 else {
2812                         spec->slave_dig_outs[0] = nid;
2813                         break; /* at most two dig outs */
2814                 }
2815         }
2816 }
2817
2818 static int vt1708S_parse_auto_config(struct hda_codec *codec)
2819 {
2820         struct via_spec *spec = codec->spec;
2821         int err;
2822
2823         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2824         if (err < 0)
2825                 return err;
2826         err = vt1708S_auto_fill_dac_nids(spec, &spec->autocfg);
2827         if (err < 0)
2828                 return err;
2829         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2830                 return 0; /* can't find valid BIOS pin config */
2831
2832         err = vt1708S_auto_create_multi_out_ctls(spec, &spec->autocfg);
2833         if (err < 0)
2834                 return err;
2835         err = vt1708S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2836         if (err < 0)
2837                 return err;
2838         err = vt1708S_auto_create_analog_input_ctls(spec, &spec->autocfg);
2839         if (err < 0)
2840                 return err;
2841
2842         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2843
2844         fill_dig_outs(codec);
2845
2846         if (spec->kctls.list)
2847                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
2848
2849         spec->input_mux = &spec->private_imux[0];
2850
2851         if (spec->hp_mux)
2852                 spec->mixers[spec->num_mixers++] = via_hp_mixer;
2853
2854         return 1;
2855 }
2856
2857 #ifdef CONFIG_SND_HDA_POWER_SAVE
2858 static struct hda_amp_list vt1708S_loopbacks[] = {
2859         { 0x16, HDA_INPUT, 1 },
2860         { 0x16, HDA_INPUT, 2 },
2861         { 0x16, HDA_INPUT, 3 },
2862         { 0x16, HDA_INPUT, 4 },
2863         { } /* end */
2864 };
2865 #endif
2866
2867 static int patch_vt1708S(struct hda_codec *codec)
2868 {
2869         struct via_spec *spec;
2870         int err;
2871
2872         /* create a codec specific record */
2873         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2874         if (spec == NULL)
2875                 return -ENOMEM;
2876
2877         codec->spec = spec;
2878
2879         /* automatic parse from the BIOS config */
2880         err = vt1708S_parse_auto_config(codec);
2881         if (err < 0) {
2882                 via_free(codec);
2883                 return err;
2884         } else if (!err) {
2885                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
2886                        "from BIOS.  Using genenic mode...\n");
2887         }
2888
2889         spec->init_verbs[spec->num_iverbs++] = vt1708S_volume_init_verbs;
2890         spec->init_verbs[spec->num_iverbs++] = vt1708S_uniwill_init_verbs;
2891
2892         spec->stream_name_analog = "VT1708S Analog";
2893         spec->stream_analog_playback = &vt1708S_pcm_analog_playback;
2894         spec->stream_analog_capture = &vt1708S_pcm_analog_capture;
2895
2896         spec->stream_name_digital = "VT1708S Digital";
2897         spec->stream_digital_playback = &vt1708S_pcm_digital_playback;
2898
2899         if (!spec->adc_nids && spec->input_mux) {
2900                 spec->adc_nids = vt1708S_adc_nids;
2901                 spec->num_adc_nids = ARRAY_SIZE(vt1708S_adc_nids);
2902                 get_mux_nids(codec);
2903                 spec->mixers[spec->num_mixers] = vt1708S_capture_mixer;
2904                 spec->num_mixers++;
2905         }
2906
2907         codec->patch_ops = via_patch_ops;
2908
2909         codec->patch_ops.init = via_auto_init;
2910         codec->patch_ops.unsol_event = via_unsol_event;
2911 #ifdef CONFIG_SND_HDA_POWER_SAVE
2912         spec->loopback.amplist = vt1708S_loopbacks;
2913 #endif
2914
2915         return 0;
2916 }
2917
2918 /* Patch for VT1702 */
2919
2920 /* capture mixer elements */
2921 static struct snd_kcontrol_new vt1702_capture_mixer[] = {
2922         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_INPUT),
2923         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_INPUT),
2924         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x20, 0x0, HDA_INPUT),
2925         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x20, 0x0, HDA_INPUT),
2926         HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x1F, 0x0, HDA_INPUT),
2927         HDA_CODEC_MUTE("Digital Mic Capture Switch", 0x1F, 0x0, HDA_INPUT),
2928         HDA_CODEC_VOLUME("Digital Mic Boost Capture Volume", 0x1E, 0x0,
2929                          HDA_INPUT),
2930         {
2931                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2932                 /* The multiple "Capture Source" controls confuse alsamixer
2933                  * So call somewhat different..
2934                  */
2935                 /* .name = "Capture Source", */
2936                 .name = "Input Source",
2937                 .count = 1,
2938                 .info = via_mux_enum_info,
2939                 .get = via_mux_enum_get,
2940                 .put = via_mux_enum_put,
2941         },
2942         { } /* end */
2943 };
2944
2945 static struct hda_verb vt1702_volume_init_verbs[] = {
2946         /*
2947          * Unmute ADC0-1 and set the default input to mic-in
2948          */
2949         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2950         {0x1F, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2951         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2952
2953
2954         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2955          * mixer widget
2956          */
2957         /* Amp Indices: Mic1 = 1, Line = 1, Mic2 = 3 */
2958         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2959         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2960         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2961         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2962         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2963
2964         /* Setup default input of PW4 to MW0 */
2965         {0x17, AC_VERB_SET_CONNECT_SEL, 0x1},
2966         /* PW6 PW7 Output enable */
2967         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2968         {0x1C, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2969         { }
2970 };
2971