e8f10b10cceb046f360d8320b8ccd5e0001c2988
[linux-2.6.git] / sound / pci / hda / patch_via.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * HD audio interface patch for VIA 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(codec, nid) & AC_WCAP_TYPE)
1343                                 >> AC_WCAP_TYPE_SHIFT;
1344                         if (type == AC_WID_PIN)
1345                                 break;
1346                         n = snd_hda_get_connections(codec, nid, conn,
1347                                                     ARRAY_SIZE(conn));
1348                         if (n <= 0)
1349                                 break;
1350                         if (n > 1) {
1351                                 spec->mux_nids[i] = nid;
1352                                 break;
1353                         }
1354                         nid = conn[0];
1355                 }
1356         }
1357         return 0;
1358 }
1359
1360 static int patch_vt1708(struct hda_codec *codec)
1361 {
1362         struct via_spec *spec;
1363         int err;
1364
1365         /* create a codec specific record */
1366         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1367         if (spec == NULL)
1368                 return -ENOMEM;
1369
1370         codec->spec = spec;
1371
1372         /* automatic parse from the BIOS config */
1373         err = vt1708_parse_auto_config(codec);
1374         if (err < 0) {
1375                 via_free(codec);
1376                 return err;
1377         } else if (!err) {
1378                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
1379                        "from BIOS.  Using genenic mode...\n");
1380         }
1381
1382         
1383         spec->stream_name_analog = "VT1708 Analog";
1384         spec->stream_analog_playback = &vt1708_pcm_analog_playback;
1385         /* disable 32bit format on VT1708 */
1386         if (codec->vendor_id == 0x11061708)
1387                 spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
1388         spec->stream_analog_capture = &vt1708_pcm_analog_capture;
1389
1390         spec->stream_name_digital = "VT1708 Digital";
1391         spec->stream_digital_playback = &vt1708_pcm_digital_playback;
1392         spec->stream_digital_capture = &vt1708_pcm_digital_capture;
1393
1394         
1395         if (!spec->adc_nids && spec->input_mux) {
1396                 spec->adc_nids = vt1708_adc_nids;
1397                 spec->num_adc_nids = ARRAY_SIZE(vt1708_adc_nids);
1398                 get_mux_nids(codec);
1399                 spec->mixers[spec->num_mixers] = vt1708_capture_mixer;
1400                 spec->num_mixers++;
1401         }
1402
1403         codec->patch_ops = via_patch_ops;
1404
1405         codec->patch_ops.init = via_auto_init;
1406 #ifdef CONFIG_SND_HDA_POWER_SAVE
1407         spec->loopback.amplist = vt1708_loopbacks;
1408 #endif
1409
1410         return 0;
1411 }
1412
1413 /* capture mixer elements */
1414 static struct snd_kcontrol_new vt1709_capture_mixer[] = {
1415         HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x0, HDA_INPUT),
1416         HDA_CODEC_MUTE("Capture Switch", 0x14, 0x0, HDA_INPUT),
1417         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x15, 0x0, HDA_INPUT),
1418         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x15, 0x0, HDA_INPUT),
1419         HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x16, 0x0, HDA_INPUT),
1420         HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x16, 0x0, HDA_INPUT),
1421         {
1422                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1423                 /* The multiple "Capture Source" controls confuse alsamixer
1424                  * So call somewhat different..
1425                  */
1426                 /* .name = "Capture Source", */
1427                 .name = "Input Source",
1428                 .count = 1,
1429                 .info = via_mux_enum_info,
1430                 .get = via_mux_enum_get,
1431                 .put = via_mux_enum_put,
1432         },
1433         { } /* end */
1434 };
1435
1436 static struct hda_verb vt1709_uniwill_init_verbs[] = {
1437         {0x20, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_HP_EVENT},
1438         { }
1439 };
1440
1441 /*
1442  * generic initialization of ADC, input mixers and output mixers
1443  */
1444 static struct hda_verb vt1709_10ch_volume_init_verbs[] = {
1445         /*
1446          * Unmute ADC0-2 and set the default input to mic-in
1447          */
1448         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1449         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1450         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1451
1452
1453         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
1454          * mixer widget
1455          */
1456         /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
1457         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1458         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1459         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
1460         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
1461         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
1462
1463         /*
1464          * Set up output selector (0x1a, 0x1b, 0x29)
1465          */
1466         /* set vol=0 to output mixers */
1467         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1468         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1469         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1470
1471         /*
1472          *  Unmute PW3 and PW4
1473          */
1474         {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1475         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1476
1477         /* Set input of PW4 as AOW4 */
1478         {0x20, AC_VERB_SET_CONNECT_SEL, 0x1},
1479         /* PW9 Output enable */
1480         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
1481         { }
1482 };
1483
1484 static struct hda_pcm_stream vt1709_10ch_pcm_analog_playback = {
1485         .substreams = 1,
1486         .channels_min = 2,
1487         .channels_max = 10,
1488         .nid = 0x10, /* NID to query formats and rates */
1489         .ops = {
1490                 .open = via_playback_pcm_open,
1491                 .prepare = via_playback_pcm_prepare,
1492                 .cleanup = via_playback_pcm_cleanup
1493         },
1494 };
1495
1496 static struct hda_pcm_stream vt1709_6ch_pcm_analog_playback = {
1497         .substreams = 1,
1498         .channels_min = 2,
1499         .channels_max = 6,
1500         .nid = 0x10, /* NID to query formats and rates */
1501         .ops = {
1502                 .open = via_playback_pcm_open,
1503                 .prepare = via_playback_pcm_prepare,
1504                 .cleanup = via_playback_pcm_cleanup
1505         },
1506 };
1507
1508 static struct hda_pcm_stream vt1709_pcm_analog_capture = {
1509         .substreams = 2,
1510         .channels_min = 2,
1511         .channels_max = 2,
1512         .nid = 0x14, /* NID to query formats and rates */
1513         .ops = {
1514                 .prepare = via_capture_pcm_prepare,
1515                 .cleanup = via_capture_pcm_cleanup
1516         },
1517 };
1518
1519 static struct hda_pcm_stream vt1709_pcm_digital_playback = {
1520         .substreams = 1,
1521         .channels_min = 2,
1522         .channels_max = 2,
1523         /* NID is set in via_build_pcms */
1524         .ops = {
1525                 .open = via_dig_playback_pcm_open,
1526                 .close = via_dig_playback_pcm_close
1527         },
1528 };
1529
1530 static struct hda_pcm_stream vt1709_pcm_digital_capture = {
1531         .substreams = 1,
1532         .channels_min = 2,
1533         .channels_max = 2,
1534 };
1535
1536 static int vt1709_auto_fill_dac_nids(struct via_spec *spec,
1537                                      const struct auto_pin_cfg *cfg)
1538 {
1539         int i;
1540         hda_nid_t nid;
1541
1542         if (cfg->line_outs == 4)  /* 10 channels */
1543                 spec->multiout.num_dacs = cfg->line_outs+1; /* AOW0~AOW4 */
1544         else if (cfg->line_outs == 3) /* 6 channels */
1545                 spec->multiout.num_dacs = cfg->line_outs; /* AOW0~AOW2 */
1546
1547         spec->multiout.dac_nids = spec->private_dac_nids;
1548
1549         if (cfg->line_outs == 4) { /* 10 channels */
1550                 for (i = 0; i < cfg->line_outs; i++) {
1551                         nid = cfg->line_out_pins[i];
1552                         if (nid) {
1553                                 /* config dac list */
1554                                 switch (i) {
1555                                 case AUTO_SEQ_FRONT:
1556                                         /* AOW0 */
1557                                         spec->multiout.dac_nids[i] = 0x10;
1558                                         break;
1559                                 case AUTO_SEQ_CENLFE:
1560                                         /* AOW2 */
1561                                         spec->multiout.dac_nids[i] = 0x12;
1562                                         break;
1563                                 case AUTO_SEQ_SURROUND:
1564                                         /* AOW3 */
1565                                         spec->multiout.dac_nids[i] = 0x11;
1566                                         break;
1567                                 case AUTO_SEQ_SIDE:
1568                                         /* AOW1 */
1569                                         spec->multiout.dac_nids[i] = 0x27;
1570                                         break;
1571                                 default:
1572                                         break;
1573                                 }
1574                         }
1575                 }
1576                 spec->multiout.dac_nids[cfg->line_outs] = 0x28; /* AOW4 */
1577
1578         } else if (cfg->line_outs == 3) { /* 6 channels */
1579                 for(i = 0; i < cfg->line_outs; i++) {
1580                         nid = cfg->line_out_pins[i];
1581                         if (nid) {
1582                                 /* config dac list */
1583                                 switch(i) {
1584                                 case AUTO_SEQ_FRONT:
1585                                         /* AOW0 */
1586                                         spec->multiout.dac_nids[i] = 0x10;
1587                                         break;
1588                                 case AUTO_SEQ_CENLFE:
1589                                         /* AOW2 */
1590                                         spec->multiout.dac_nids[i] = 0x12;
1591                                         break;
1592                                 case AUTO_SEQ_SURROUND:
1593                                         /* AOW1 */
1594                                         spec->multiout.dac_nids[i] = 0x11;
1595                                         break;
1596                                 default:
1597                                         break;
1598                                 }
1599                         }
1600                 }
1601         }
1602
1603         return 0;
1604 }
1605
1606 /* add playback controls from the parsed DAC table */
1607 static int vt1709_auto_create_multi_out_ctls(struct via_spec *spec,
1608                                              const struct auto_pin_cfg *cfg)
1609 {
1610         char name[32];
1611         static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
1612         hda_nid_t nid = 0;
1613         int i, err;
1614
1615         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
1616                 nid = cfg->line_out_pins[i];
1617
1618                 if (!nid)       
1619                         continue;
1620
1621                 if (i == AUTO_SEQ_CENLFE) {
1622                         /* Center/LFE */
1623                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1624                                               "Center Playback Volume",
1625                                               HDA_COMPOSE_AMP_VAL(0x1b, 1, 0,
1626                                                                   HDA_OUTPUT));
1627                         if (err < 0)
1628                                 return err;
1629                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1630                                               "LFE Playback Volume",
1631                                               HDA_COMPOSE_AMP_VAL(0x1b, 2, 0,
1632                                                                   HDA_OUTPUT));
1633                         if (err < 0)
1634                                 return err;
1635                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1636                                               "Center Playback Switch",
1637                                               HDA_COMPOSE_AMP_VAL(0x1b, 1, 0,
1638                                                                   HDA_OUTPUT));
1639                         if (err < 0)
1640                                 return err;
1641                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1642                                               "LFE Playback Switch",
1643                                               HDA_COMPOSE_AMP_VAL(0x1b, 2, 0,
1644                                                                   HDA_OUTPUT));
1645                         if (err < 0)
1646                                 return err;
1647                 } else if (i == AUTO_SEQ_FRONT){
1648                         /* add control to mixer index 0 */
1649                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1650                                               "Master Front Playback Volume",
1651                                               HDA_COMPOSE_AMP_VAL(0x18, 3, 0,
1652                                                                   HDA_INPUT));
1653                         if (err < 0)
1654                                 return err;
1655                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1656                                               "Master Front Playback Switch",
1657                                               HDA_COMPOSE_AMP_VAL(0x18, 3, 0,
1658                                                                   HDA_INPUT));
1659                         if (err < 0)
1660                                 return err;
1661                         
1662                         /* add control to PW3 */
1663                         sprintf(name, "%s Playback Volume", chname[i]);
1664                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1665                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1666                                                                   HDA_OUTPUT));
1667                         if (err < 0)
1668                                 return err;
1669                         sprintf(name, "%s Playback Switch", chname[i]);
1670                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1671                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1672                                                                   HDA_OUTPUT));
1673                         if (err < 0)
1674                                 return err;
1675                 } else if (i == AUTO_SEQ_SURROUND) {
1676                         sprintf(name, "%s Playback Volume", chname[i]);
1677                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1678                                               HDA_COMPOSE_AMP_VAL(0x1a, 3, 0,
1679                                                                   HDA_OUTPUT));
1680                         if (err < 0)
1681                                 return err;
1682                         sprintf(name, "%s Playback Switch", chname[i]);
1683                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1684                                               HDA_COMPOSE_AMP_VAL(0x1a, 3, 0,
1685                                                                   HDA_OUTPUT));
1686                         if (err < 0)
1687                                 return err;
1688                 } else if (i == AUTO_SEQ_SIDE) {
1689                         sprintf(name, "%s Playback Volume", chname[i]);
1690                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1691                                               HDA_COMPOSE_AMP_VAL(0x29, 3, 0,
1692                                                                   HDA_OUTPUT));
1693                         if (err < 0)
1694                                 return err;
1695                         sprintf(name, "%s Playback Switch", chname[i]);
1696                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1697                                               HDA_COMPOSE_AMP_VAL(0x29, 3, 0,
1698                                                                   HDA_OUTPUT));
1699                         if (err < 0)
1700                                 return err;
1701                 }
1702         }
1703
1704         return 0;
1705 }
1706
1707 static int vt1709_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
1708 {
1709         int err;
1710
1711         if (!pin)
1712                 return 0;
1713
1714         if (spec->multiout.num_dacs == 5) /* 10 channels */
1715                 spec->multiout.hp_nid = VT1709_HP_DAC_NID;
1716         else if (spec->multiout.num_dacs == 3) /* 6 channels */
1717                 spec->multiout.hp_nid = 0;
1718
1719         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1720                               "Headphone Playback Volume",
1721                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1722         if (err < 0)
1723                 return err;
1724         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1725                               "Headphone Playback Switch",
1726                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1727         if (err < 0)
1728                 return err;
1729
1730         return 0;
1731 }
1732
1733 /* create playback/capture controls for input pins */
1734 static int vt1709_auto_create_analog_input_ctls(struct via_spec *spec,
1735                                                 const struct auto_pin_cfg *cfg)
1736 {
1737         static char *labels[] = {
1738                 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
1739         };
1740         struct hda_input_mux *imux = &spec->private_imux[0];
1741         int i, err, idx = 0;
1742
1743         /* for internal loopback recording select */
1744         imux->items[imux->num_items].label = "Stereo Mixer";
1745         imux->items[imux->num_items].index = idx;
1746         imux->num_items++;
1747
1748         for (i = 0; i < AUTO_PIN_LAST; i++) {
1749                 if (!cfg->input_pins[i])
1750                         continue;
1751
1752                 switch (cfg->input_pins[i]) {
1753                 case 0x1d: /* Mic */
1754                         idx = 2;
1755                         break;
1756                                 
1757                 case 0x1e: /* Line In */
1758                         idx = 3;
1759                         break;
1760
1761                 case 0x21: /* Front Mic */
1762                         idx = 4;
1763                         break;
1764
1765                 case 0x23: /* CD */
1766                         idx = 1;
1767                         break;
1768                 }
1769                 err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
1770                                            idx, 0x18);
1771                 if (err < 0)
1772                         return err;
1773                 imux->items[imux->num_items].label = labels[i];
1774                 imux->items[imux->num_items].index = idx;
1775                 imux->num_items++;
1776         }
1777         return 0;
1778 }
1779
1780 static int vt1709_parse_auto_config(struct hda_codec *codec)
1781 {
1782         struct via_spec *spec = codec->spec;
1783         int err;
1784
1785         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1786         if (err < 0)
1787                 return err;
1788         err = vt1709_auto_fill_dac_nids(spec, &spec->autocfg);
1789         if (err < 0)
1790                 return err;
1791         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
1792                 return 0; /* can't find valid BIOS pin config */
1793
1794         err = vt1709_auto_create_multi_out_ctls(spec, &spec->autocfg);
1795         if (err < 0)
1796                 return err;
1797         err = vt1709_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
1798         if (err < 0)
1799                 return err;
1800         err = vt1709_auto_create_analog_input_ctls(spec, &spec->autocfg);
1801         if (err < 0)
1802                 return err;
1803
1804         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
1805
1806         if (spec->autocfg.dig_outs)
1807                 spec->multiout.dig_out_nid = VT1709_DIGOUT_NID;
1808         spec->dig_in_pin = VT1709_DIGIN_PIN;
1809         if (spec->autocfg.dig_in_pin)
1810                 spec->dig_in_nid = VT1709_DIGIN_NID;
1811
1812         if (spec->kctls.list)
1813                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
1814
1815         spec->input_mux = &spec->private_imux[0];
1816
1817         if (spec->hp_mux)
1818                 spec->mixers[spec->num_mixers++] = via_hp_mixer;
1819
1820         return 1;
1821 }
1822
1823 #ifdef CONFIG_SND_HDA_POWER_SAVE
1824 static struct hda_amp_list vt1709_loopbacks[] = {
1825         { 0x18, HDA_INPUT, 1 },
1826         { 0x18, HDA_INPUT, 2 },
1827         { 0x18, HDA_INPUT, 3 },
1828         { 0x18, HDA_INPUT, 4 },
1829         { } /* end */
1830 };
1831 #endif
1832
1833 static int patch_vt1709_10ch(struct hda_codec *codec)
1834 {
1835         struct via_spec *spec;
1836         int err;
1837
1838         /* create a codec specific record */
1839         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1840         if (spec == NULL)
1841                 return -ENOMEM;
1842
1843         codec->spec = spec;
1844
1845         err = vt1709_parse_auto_config(codec);
1846         if (err < 0) {
1847                 via_free(codec);
1848                 return err;
1849         } else if (!err) {
1850                 printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
1851                        "Using genenic mode...\n");
1852         }
1853
1854         spec->init_verbs[spec->num_iverbs++] = vt1709_10ch_volume_init_verbs;
1855         spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
1856
1857         spec->stream_name_analog = "VT1709 Analog";
1858         spec->stream_analog_playback = &vt1709_10ch_pcm_analog_playback;
1859         spec->stream_analog_capture = &vt1709_pcm_analog_capture;
1860
1861         spec->stream_name_digital = "VT1709 Digital";
1862         spec->stream_digital_playback = &vt1709_pcm_digital_playback;
1863         spec->stream_digital_capture = &vt1709_pcm_digital_capture;
1864
1865         
1866         if (!spec->adc_nids && spec->input_mux) {
1867                 spec->adc_nids = vt1709_adc_nids;
1868                 spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
1869                 get_mux_nids(codec);
1870                 spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
1871                 spec->num_mixers++;
1872         }
1873
1874         codec->patch_ops = via_patch_ops;
1875
1876         codec->patch_ops.init = via_auto_init;
1877         codec->patch_ops.unsol_event = via_unsol_event;
1878 #ifdef CONFIG_SND_HDA_POWER_SAVE
1879         spec->loopback.amplist = vt1709_loopbacks;
1880 #endif
1881
1882         return 0;
1883 }
1884 /*
1885  * generic initialization of ADC, input mixers and output mixers
1886  */
1887 static struct hda_verb vt1709_6ch_volume_init_verbs[] = {
1888         /*
1889          * Unmute ADC0-2 and set the default input to mic-in
1890          */
1891         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1892         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1893         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1894
1895
1896         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
1897          * mixer widget
1898          */
1899         /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
1900         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1901         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1902         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
1903         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
1904         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
1905
1906         /*
1907          * Set up output selector (0x1a, 0x1b, 0x29)
1908          */
1909         /* set vol=0 to output mixers */
1910         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1911         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1912         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1913
1914         /*
1915          *  Unmute PW3 and PW4
1916          */
1917         {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1918         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1919
1920         /* Set input of PW4 as MW0 */
1921         {0x20, AC_VERB_SET_CONNECT_SEL, 0},
1922         /* PW9 Output enable */
1923         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
1924         { }
1925 };
1926
1927 static int patch_vt1709_6ch(struct hda_codec *codec)
1928 {
1929         struct via_spec *spec;
1930         int err;
1931
1932         /* create a codec specific record */
1933         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1934         if (spec == NULL)
1935                 return -ENOMEM;
1936
1937         codec->spec = spec;
1938
1939         err = vt1709_parse_auto_config(codec);
1940         if (err < 0) {
1941                 via_free(codec);
1942                 return err;
1943         } else if (!err) {
1944                 printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
1945                        "Using genenic mode...\n");
1946         }
1947
1948         spec->init_verbs[spec->num_iverbs++] = vt1709_6ch_volume_init_verbs;
1949         spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
1950
1951         spec->stream_name_analog = "VT1709 Analog";
1952         spec->stream_analog_playback = &vt1709_6ch_pcm_analog_playback;
1953         spec->stream_analog_capture = &vt1709_pcm_analog_capture;
1954
1955         spec->stream_name_digital = "VT1709 Digital";
1956         spec->stream_digital_playback = &vt1709_pcm_digital_playback;
1957         spec->stream_digital_capture = &vt1709_pcm_digital_capture;
1958
1959         
1960         if (!spec->adc_nids && spec->input_mux) {
1961                 spec->adc_nids = vt1709_adc_nids;
1962                 spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
1963                 get_mux_nids(codec);
1964                 spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
1965                 spec->num_mixers++;
1966         }
1967
1968         codec->patch_ops = via_patch_ops;
1969
1970         codec->patch_ops.init = via_auto_init;
1971         codec->patch_ops.unsol_event = via_unsol_event;
1972 #ifdef CONFIG_SND_HDA_POWER_SAVE
1973         spec->loopback.amplist = vt1709_loopbacks;
1974 #endif
1975         return 0;
1976 }
1977
1978 /* capture mixer elements */
1979 static struct snd_kcontrol_new vt1708B_capture_mixer[] = {
1980         HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
1981         HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
1982         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
1983         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
1984         {
1985                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1986                 /* The multiple "Capture Source" controls confuse alsamixer
1987                  * So call somewhat different..
1988                  */
1989                 /* .name = "Capture Source", */
1990                 .name = "Input Source",
1991                 .count = 1,
1992                 .info = via_mux_enum_info,
1993                 .get = via_mux_enum_get,
1994                 .put = via_mux_enum_put,
1995         },
1996         { } /* end */
1997 };
1998 /*
1999  * generic initialization of ADC, input mixers and output mixers
2000  */
2001 static struct hda_verb vt1708B_8ch_volume_init_verbs[] = {
2002         /*
2003          * Unmute ADC0-1 and set the default input to mic-in
2004          */
2005         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2006         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2007
2008
2009         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2010          * mixer widget
2011          */
2012         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2013         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2014         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2015         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2016         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2017         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2018
2019         /*
2020          * Set up output mixers
2021          */
2022         /* set vol=0 to output mixers */
2023         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2024         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2025         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2026
2027         /* Setup default input to PW4 */
2028         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x1},
2029         /* PW9 Output enable */
2030         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2031         /* PW10 Input enable */
2032         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2033         { }
2034 };
2035
2036 static struct hda_verb vt1708B_4ch_volume_init_verbs[] = {
2037         /*
2038          * Unmute ADC0-1 and set the default input to mic-in
2039          */
2040         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2041         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2042
2043
2044         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2045          * mixer widget
2046          */
2047         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2048         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2049         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2050         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2051         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2052         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2053
2054         /*
2055          * Set up output mixers
2056          */
2057         /* set vol=0 to output mixers */
2058         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2059         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2060         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2061
2062         /* Setup default input of PW4 to MW0 */
2063         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
2064         /* PW9 Output enable */
2065         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2066         /* PW10 Input enable */
2067         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2068         { }
2069 };
2070
2071 static struct hda_verb vt1708B_uniwill_init_verbs[] = {
2072         {0x1D, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_HP_EVENT},
2073         { }
2074 };
2075
2076 static struct hda_pcm_stream vt1708B_8ch_pcm_analog_playback = {
2077         .substreams = 2,
2078         .channels_min = 2,
2079         .channels_max = 8,
2080         .nid = 0x10, /* NID to query formats and rates */
2081         .ops = {
2082                 .open = via_playback_pcm_open,
2083                 .prepare = via_playback_multi_pcm_prepare,
2084                 .cleanup = via_playback_multi_pcm_cleanup
2085         },
2086 };
2087
2088 static struct hda_pcm_stream vt1708B_4ch_pcm_analog_playback = {
2089         .substreams = 2,
2090         .channels_min = 2,
2091         .channels_max = 4,
2092         .nid = 0x10, /* NID to query formats and rates */
2093         .ops = {
2094                 .open = via_playback_pcm_open,
2095                 .prepare = via_playback_multi_pcm_prepare,
2096                 .cleanup = via_playback_multi_pcm_cleanup
2097         },
2098 };
2099
2100 static struct hda_pcm_stream vt1708B_pcm_analog_capture = {
2101         .substreams = 2,
2102         .channels_min = 2,
2103         .channels_max = 2,
2104         .nid = 0x13, /* NID to query formats and rates */
2105         .ops = {
2106                 .prepare = via_capture_pcm_prepare,
2107                 .cleanup = via_capture_pcm_cleanup
2108         },
2109 };
2110
2111 static struct hda_pcm_stream vt1708B_pcm_digital_playback = {
2112         .substreams = 1,
2113         .channels_min = 2,
2114         .channels_max = 2,
2115         /* NID is set in via_build_pcms */
2116         .ops = {
2117                 .open = via_dig_playback_pcm_open,
2118                 .close = via_dig_playback_pcm_close,
2119                 .prepare = via_dig_playback_pcm_prepare,
2120                 .cleanup = via_dig_playback_pcm_cleanup
2121         },
2122 };
2123
2124 static struct hda_pcm_stream vt1708B_pcm_digital_capture = {
2125         .substreams = 1,
2126         .channels_min = 2,
2127         .channels_max = 2,
2128 };
2129
2130 /* fill in the dac_nids table from the parsed pin configuration */
2131 static int vt1708B_auto_fill_dac_nids(struct via_spec *spec,
2132                                      const struct auto_pin_cfg *cfg)
2133 {
2134         int i;
2135         hda_nid_t nid;
2136
2137         spec->multiout.num_dacs = cfg->line_outs;
2138
2139         spec->multiout.dac_nids = spec->private_dac_nids;
2140
2141         for (i = 0; i < 4; i++) {
2142                 nid = cfg->line_out_pins[i];
2143                 if (nid) {
2144                         /* config dac list */
2145                         switch (i) {
2146                         case AUTO_SEQ_FRONT:
2147                                 spec->multiout.dac_nids[i] = 0x10;
2148                                 break;
2149                         case AUTO_SEQ_CENLFE:
2150                                 spec->multiout.dac_nids[i] = 0x24;
2151                                 break;
2152                         case AUTO_SEQ_SURROUND:
2153                                 spec->multiout.dac_nids[i] = 0x11;
2154                                 break;
2155                         case AUTO_SEQ_SIDE:
2156                                 spec->multiout.dac_nids[i] = 0x25;
2157                                 break;
2158                         }
2159                 }
2160         }
2161
2162         return 0;
2163 }
2164
2165 /* add playback controls from the parsed DAC table */
2166 static int vt1708B_auto_create_multi_out_ctls(struct via_spec *spec,
2167                                              const struct auto_pin_cfg *cfg)
2168 {
2169         char name[32];
2170         static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
2171         hda_nid_t nid_vols[] = {0x16, 0x18, 0x26, 0x27};
2172         hda_nid_t nid, nid_vol = 0;
2173         int i, err;
2174
2175         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
2176                 nid = cfg->line_out_pins[i];
2177
2178                 if (!nid)
2179                         continue;
2180
2181                 nid_vol = nid_vols[i];
2182
2183                 if (i == AUTO_SEQ_CENLFE) {
2184                         /* Center/LFE */
2185                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2186                                               "Center Playback Volume",
2187                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2188                                                                   HDA_OUTPUT));
2189                         if (err < 0)
2190                                 return err;
2191                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2192                                               "LFE Playback Volume",
2193                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2194                                                                   HDA_OUTPUT));
2195                         if (err < 0)
2196                                 return err;
2197                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2198                                               "Center Playback Switch",
2199                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2200                                                                   HDA_OUTPUT));
2201                         if (err < 0)
2202                                 return err;
2203                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2204                                               "LFE Playback Switch",
2205                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2206                                                                   HDA_OUTPUT));
2207                         if (err < 0)
2208                                 return err;
2209                 } else if (i == AUTO_SEQ_FRONT) {
2210                         /* add control to mixer index 0 */
2211                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2212                                               "Master Front Playback Volume",
2213                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2214                                                                   HDA_INPUT));
2215                         if (err < 0)
2216                                 return err;
2217                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2218                                               "Master Front Playback Switch",
2219                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2220                                                                   HDA_INPUT));
2221                         if (err < 0)
2222                                 return err;
2223
2224                         /* add control to PW3 */
2225                         sprintf(name, "%s Playback Volume", chname[i]);
2226                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2227                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2228                                                                   HDA_OUTPUT));
2229                         if (err < 0)
2230                                 return err;
2231                         sprintf(name, "%s Playback Switch", chname[i]);
2232                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2233                                               HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2234                                                                   HDA_OUTPUT));
2235                         if (err < 0)
2236                                 return err;
2237                 } else {
2238                         sprintf(name, "%s Playback Volume", chname[i]);
2239                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2240                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2241                                                                   HDA_OUTPUT));
2242                         if (err < 0)
2243                                 return err;
2244                         sprintf(name, "%s Playback Switch", chname[i]);
2245                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2246                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2247                                                                   HDA_OUTPUT));
2248                         if (err < 0)
2249                                 return err;
2250                 }
2251         }
2252
2253         return 0;
2254 }
2255
2256 static int vt1708B_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
2257 {
2258         int err;
2259
2260         if (!pin)
2261                 return 0;
2262
2263         spec->multiout.hp_nid = VT1708B_HP_NID; /* AOW3 */
2264
2265         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2266                               "Headphone Playback Volume",
2267                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2268         if (err < 0)
2269                 return err;
2270         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2271                               "Headphone Playback Switch",
2272                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2273         if (err < 0)
2274                 return err;
2275
2276         create_hp_imux(spec);
2277
2278         return 0;
2279 }
2280
2281 /* create playback/capture controls for input pins */
2282 static int vt1708B_auto_create_analog_input_ctls(struct via_spec *spec,
2283                                                 const struct auto_pin_cfg *cfg)
2284 {
2285         static char *labels[] = {
2286                 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
2287         };
2288         struct hda_input_mux *imux = &spec->private_imux[0];
2289         int i, err, idx = 0;
2290
2291         /* for internal loopback recording select */
2292         imux->items[imux->num_items].label = "Stereo Mixer";
2293         imux->items[imux->num_items].index = idx;
2294         imux->num_items++;
2295
2296         for (i = 0; i < AUTO_PIN_LAST; i++) {
2297                 if (!cfg->input_pins[i])
2298                         continue;
2299
2300                 switch (cfg->input_pins[i]) {
2301                 case 0x1a: /* Mic */
2302                         idx = 2;
2303                         break;
2304
2305                 case 0x1b: /* Line In */
2306                         idx = 3;
2307                         break;
2308
2309                 case 0x1e: /* Front Mic */
2310                         idx = 4;
2311                         break;
2312
2313                 case 0x1f: /* CD */
2314                         idx = 1;
2315                         break;
2316                 }
2317                 err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
2318                                            idx, 0x16);
2319                 if (err < 0)
2320                         return err;
2321                 imux->items[imux->num_items].label = labels[i];
2322                 imux->items[imux->num_items].index = idx;
2323                 imux->num_items++;
2324         }
2325         return 0;
2326 }
2327
2328 static int vt1708B_parse_auto_config(struct hda_codec *codec)
2329 {
2330         struct via_spec *spec = codec->spec;
2331         int err;
2332
2333         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2334         if (err < 0)
2335                 return err;
2336         err = vt1708B_auto_fill_dac_nids(spec, &spec->autocfg);
2337         if (err < 0)
2338                 return err;
2339         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2340                 return 0; /* can't find valid BIOS pin config */
2341
2342         err = vt1708B_auto_create_multi_out_ctls(spec, &spec->autocfg);
2343         if (err < 0)
2344                 return err;
2345         err = vt1708B_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2346         if (err < 0)
2347                 return err;
2348         err = vt1708B_auto_create_analog_input_ctls(spec, &spec->autocfg);
2349         if (err < 0)
2350                 return err;
2351
2352         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2353
2354         if (spec->autocfg.dig_outs)
2355                 spec->multiout.dig_out_nid = VT1708B_DIGOUT_NID;
2356         spec->dig_in_pin = VT1708B_DIGIN_PIN;
2357         if (spec->autocfg.dig_in_pin)
2358                 spec->dig_in_nid = VT1708B_DIGIN_NID;
2359
2360         if (spec->kctls.list)
2361                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
2362
2363         spec->input_mux = &spec->private_imux[0];
2364
2365         if (spec->hp_mux)
2366                 spec->mixers[spec->num_mixers++] = via_hp_mixer;
2367
2368         return 1;
2369 }
2370
2371 #ifdef CONFIG_SND_HDA_POWER_SAVE
2372 static struct hda_amp_list vt1708B_loopbacks[] = {
2373         { 0x16, HDA_INPUT, 1 },
2374         { 0x16, HDA_INPUT, 2 },
2375         { 0x16, HDA_INPUT, 3 },
2376         { 0x16, HDA_INPUT, 4 },
2377         { } /* end */
2378 };
2379 #endif
2380
2381 static int patch_vt1708B_8ch(struct hda_codec *codec)
2382 {
2383         struct via_spec *spec;
2384         int err;
2385
2386         /* create a codec specific record */
2387         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2388         if (spec == NULL)
2389                 return -ENOMEM;
2390
2391         codec->spec = spec;
2392
2393         /* automatic parse from the BIOS config */
2394         err = vt1708B_parse_auto_config(codec);
2395         if (err < 0) {
2396                 via_free(codec);
2397                 return err;
2398         } else if (!err) {
2399                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
2400                        "from BIOS.  Using genenic mode...\n");
2401         }
2402
2403         spec->init_verbs[spec->num_iverbs++] = vt1708B_8ch_volume_init_verbs;
2404         spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
2405
2406         spec->stream_name_analog = "VT1708B Analog";
2407         spec->stream_analog_playback = &vt1708B_8ch_pcm_analog_playback;
2408         spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
2409
2410         spec->stream_name_digital = "VT1708B Digital";
2411         spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
2412         spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
2413
2414         if (!spec->adc_nids && spec->input_mux) {
2415                 spec->adc_nids = vt1708B_adc_nids;
2416                 spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
2417                 get_mux_nids(codec);
2418                 spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
2419                 spec->num_mixers++;
2420         }
2421
2422         codec->patch_ops = via_patch_ops;
2423
2424         codec->patch_ops.init = via_auto_init;
2425         codec->patch_ops.unsol_event = via_unsol_event;
2426 #ifdef CONFIG_SND_HDA_POWER_SAVE
2427         spec->loopback.amplist = vt1708B_loopbacks;
2428 #endif
2429
2430         return 0;
2431 }
2432
2433 static int patch_vt1708B_4ch(struct hda_codec *codec)
2434 {
2435         struct via_spec *spec;
2436         int err;
2437
2438         /* create a codec specific record */
2439         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2440         if (spec == NULL)
2441                 return -ENOMEM;
2442
2443         codec->spec = spec;
2444
2445         /* automatic parse from the BIOS config */
2446         err = vt1708B_parse_auto_config(codec);
2447         if (err < 0) {
2448                 via_free(codec);
2449                 return err;
2450         } else if (!err) {
2451                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
2452                        "from BIOS.  Using genenic mode...\n");
2453         }
2454
2455         spec->init_verbs[spec->num_iverbs++] = vt1708B_4ch_volume_init_verbs;
2456         spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
2457
2458         spec->stream_name_analog = "VT1708B Analog";
2459         spec->stream_analog_playback = &vt1708B_4ch_pcm_analog_playback;
2460         spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
2461
2462         spec->stream_name_digital = "VT1708B Digital";
2463         spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
2464         spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
2465
2466         if (!spec->adc_nids && spec->input_mux) {
2467                 spec->adc_nids = vt1708B_adc_nids;
2468                 spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
2469                 get_mux_nids(codec);
2470                 spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
2471                 spec->num_mixers++;
2472         }
2473
2474         codec->patch_ops = via_patch_ops;
2475
2476         codec->patch_ops.init = via_auto_init;
2477         codec->patch_ops.unsol_event = via_unsol_event;
2478 #ifdef CONFIG_SND_HDA_POWER_SAVE
2479         spec->loopback.amplist = vt1708B_loopbacks;
2480 #endif
2481
2482         return 0;
2483 }
2484
2485 /* Patch for VT1708S */
2486
2487 /* VT1708S software backdoor based override for buggy hardware micboost
2488  * setting */
2489 #define MIC_BOOST_VOLUME(xname, nid) {                          \
2490         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,            \
2491         .name = xname,                                  \
2492         .index = 0,                                     \
2493         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |     \
2494         SNDRV_CTL_ELEM_ACCESS_TLV_READ |                \
2495         SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,             \
2496         .info = mic_boost_volume_info,                  \
2497         .get = snd_hda_mixer_amp_volume_get,            \
2498         .put = snd_hda_mixer_amp_volume_put,            \
2499         .tlv = { .c = mic_boost_tlv },                  \
2500         .private_value = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT) }
2501
2502 /* capture mixer elements */
2503 static struct snd_kcontrol_new vt1708S_capture_mixer[] = {
2504         HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
2505         HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
2506         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
2507         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
2508         MIC_BOOST_VOLUME("Mic Boost Capture Volume", 0x1A),
2509         MIC_BOOST_VOLUME("Front Mic Boost Capture Volume", 0x1E),
2510         {
2511                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2512                 /* The multiple "Capture Source" controls confuse alsamixer
2513                  * So call somewhat different..
2514                  */
2515                 /* .name = "Capture Source", */
2516                 .name = "Input Source",
2517                 .count = 1,
2518                 .info = via_mux_enum_info,
2519                 .get = via_mux_enum_get,
2520                 .put = via_mux_enum_put,
2521         },
2522         { } /* end */
2523 };
2524
2525 static struct hda_verb vt1708S_volume_init_verbs[] = {
2526         /* Unmute ADC0-1 and set the default input to mic-in */
2527         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2528         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2529
2530         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the
2531          * analog-loopback mixer widget */
2532         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2533         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2534         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2535         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2536         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2537         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2538
2539         /* Setup default input of PW4 to MW0 */
2540         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
2541         /* PW9, PW10  Output enable */
2542         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2543         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2544         /* Enable Mic Boost Volume backdoor */
2545         {0x1, 0xf98, 0x1},
2546         { }
2547 };
2548
2549 static struct hda_verb vt1708S_uniwill_init_verbs[] = {
2550         {0x1D, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_HP_EVENT},
2551         { }
2552 };
2553
2554 static struct hda_pcm_stream vt1708S_pcm_analog_playback = {
2555         .substreams = 2,
2556         .channels_min = 2,
2557         .channels_max = 8,
2558         .nid = 0x10, /* NID to query formats and rates */
2559         .ops = {
2560                 .open = via_playback_pcm_open,
2561                 .prepare = via_playback_pcm_prepare,
2562                 .cleanup = via_playback_pcm_cleanup
2563         },
2564 };
2565
2566 static struct hda_pcm_stream vt1708S_pcm_analog_capture = {
2567         .substreams = 2,
2568         .channels_min = 2,
2569         .channels_max = 2,
2570         .nid = 0x13, /* NID to query formats and rates */
2571         .ops = {
2572                 .prepare = via_capture_pcm_prepare,
2573                 .cleanup = via_capture_pcm_cleanup
2574         },
2575 };
2576
2577 static struct hda_pcm_stream vt1708S_pcm_digital_playback = {
2578         .substreams = 1,
2579         .channels_min = 2,
2580         .channels_max = 2,
2581         /* NID is set in via_build_pcms */
2582         .ops = {
2583                 .open = via_dig_playback_pcm_open,
2584                 .close = via_dig_playback_pcm_close,
2585                 .prepare = via_dig_playback_pcm_prepare,
2586                 .cleanup = via_dig_playback_pcm_cleanup
2587         },
2588 };
2589
2590 /* fill in the dac_nids table from the parsed pin configuration */
2591 static int vt1708S_auto_fill_dac_nids(struct via_spec *spec,
2592                                      const struct auto_pin_cfg *cfg)
2593 {
2594         int i;
2595         hda_nid_t nid;
2596
2597         spec->multiout.num_dacs = cfg->line_outs;
2598
2599         spec->multiout.dac_nids = spec->private_dac_nids;
2600
2601         for (i = 0; i < 4; i++) {
2602                 nid = cfg->line_out_pins[i];
2603                 if (nid) {
2604                         /* config dac list */
2605                         switch (i) {
2606                         case AUTO_SEQ_FRONT:
2607                                 spec->multiout.dac_nids[i] = 0x10;
2608                                 break;
2609                         case AUTO_SEQ_CENLFE:
2610                                 spec->multiout.dac_nids[i] = 0x24;
2611                                 break;
2612                         case AUTO_SEQ_SURROUND:
2613                                 spec->multiout.dac_nids[i] = 0x11;
2614                                 break;
2615                         case AUTO_SEQ_SIDE:
2616                                 spec->multiout.dac_nids[i] = 0x25;
2617                                 break;
2618                         }
2619                 }
2620         }
2621
2622         return 0;
2623 }
2624
2625 /* add playback controls from the parsed DAC table */
2626 static int vt1708S_auto_create_multi_out_ctls(struct via_spec *spec,
2627                                              const struct auto_pin_cfg *cfg)
2628 {
2629         char name[32];
2630         static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
2631         hda_nid_t nid_vols[] = {0x10, 0x11, 0x24, 0x25};
2632         hda_nid_t nid_mutes[] = {0x1C, 0x18, 0x26, 0x27};
2633         hda_nid_t nid, nid_vol, nid_mute;
2634         int i, err;
2635
2636         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
2637                 nid = cfg->line_out_pins[i];
2638
2639                 if (!nid)
2640                         continue;
2641
2642                 nid_vol = nid_vols[i];
2643                 nid_mute = nid_mutes[i];
2644
2645                 if (i == AUTO_SEQ_CENLFE) {
2646                         /* Center/LFE */
2647                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2648                                               "Center Playback Volume",
2649                                               HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2650                                                                   HDA_OUTPUT));
2651                         if (err < 0)
2652                                 return err;
2653                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2654                                               "LFE Playback Volume",
2655                                               HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2656                                                                   HDA_OUTPUT));
2657                         if (err < 0)
2658                                 return err;
2659                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2660                                               "Center Playback Switch",
2661                                               HDA_COMPOSE_AMP_VAL(nid_mute,
2662                                                                   1, 0,
2663                                                                   HDA_OUTPUT));
2664                         if (err < 0)
2665                                 return err;
2666                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2667                                               "LFE Playback Switch",
2668                                               HDA_COMPOSE_AMP_VAL(nid_mute,
2669                                                                   2, 0,
2670                                                                   HDA_OUTPUT));
2671                         if (err < 0)
2672                                 return err;
2673                 } else if (i == AUTO_SEQ_FRONT) {
2674                         /* add control to mixer index 0 */
2675                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2676                                               "Master Front Playback Volume",
2677                                               HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
2678                                                                   HDA_INPUT));
2679                         if (err < 0)
2680                                 return err;
2681                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2682                                               "Master Front Playback Switch",
2683                                               HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
2684                                                                   HDA_INPUT));
2685                         if (err < 0)
2686                                 return err;
2687
2688                         /* Front */
2689                         sprintf(name, "%s Playback Volume", chname[i]);
2690                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2691                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2692                                                                   HDA_OUTPUT));
2693                         if (err < 0)
2694                                 return err;
2695                         sprintf(name, "%s Playback Switch", chname[i]);
2696                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2697                                               HDA_COMPOSE_AMP_VAL(nid_mute,
2698                                                                   3, 0,
2699                                                                   HDA_OUTPUT));
2700                         if (err < 0)
2701                                 return err;
2702                 } else {
2703                         sprintf(name, "%s Playback Volume", chname[i]);
2704                         err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2705                                               HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2706                                                                   HDA_OUTPUT));
2707                         if (err < 0)
2708                                 return err;
2709                         sprintf(name, "%s Playback Switch", chname[i]);
2710                         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2711                                               HDA_COMPOSE_AMP_VAL(nid_mute,
2712                                                                   3, 0,
2713                                                                   HDA_OUTPUT));
2714                         if (err < 0)
2715                                 return err;
2716                 }
2717         }
2718
2719         return 0;
2720 }
2721
2722 static int vt1708S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
2723 {
2724         int err;
2725
2726         if (!pin)
2727                 return 0;
2728
2729         spec->multiout.hp_nid = VT1708S_HP_NID; /* AOW3 */
2730
2731         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2732                               "Headphone Playback Volume",
2733                               HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
2734         if (err < 0)
2735                 return err;
2736
2737         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2738                               "Headphone Playback Switch",
2739                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2740         if (err < 0)
2741                 return err;
2742
2743         create_hp_imux(spec);
2744
2745         return 0;
2746 }
2747
2748 /* create playback/capture controls for input pins */
2749 static int vt1708S_auto_create_analog_input_ctls(struct via_spec *spec,
2750                                                 const struct auto_pin_cfg *cfg)
2751 {
2752         static char *labels[] = {
2753                 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
2754         };
2755         struct hda_input_mux *imux = &spec->private_imux[0];
2756         int i, err, idx = 0;
2757
2758         /* for internal loopback recording select */
2759         imux->items[imux->num_items].label = "Stereo Mixer";
2760         imux->items[imux->num_items].index = 5;
2761         imux->num_items++;
2762
2763         for (i = 0; i < AUTO_PIN_LAST; i++) {
2764                 if (!cfg->input_pins[i])
2765                         continue;
2766
2767                 switch (cfg->input_pins[i]) {
2768                 case 0x1a: /* Mic */
2769                         idx = 2;
2770                         break;
2771
2772                 case 0x1b: /* Line In */
2773                         idx = 3;
2774                         break;
2775
2776                 case 0x1e: /* Front Mic */
2777                         idx = 4;
2778                         break;
2779
2780                 case 0x1f: /* CD */
2781                         idx = 1;
2782                         break;
2783                 }
2784                 err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
2785                                            idx, 0x16);
2786                 if (err < 0)
2787                         return err;
2788                 imux->items[imux->num_items].label = labels[i];
2789                 imux->items[imux->num_items].index = idx-1;
2790                 imux->num_items++;
2791         }
2792         return 0;
2793 }
2794
2795 /* fill out digital output widgets; one for master and one for slave outputs */
2796 static void fill_dig_outs(struct hda_codec *codec)
2797 {
2798         struct via_spec *spec = codec->spec;
2799         int i;
2800
2801         for (i = 0; i < spec->autocfg.dig_outs; i++) {
2802                 hda_nid_t nid;
2803                 int conn;
2804
2805                 nid = spec->autocfg.dig_out_pins[i];
2806                 if (!nid)
2807                         continue;
2808                 conn = snd_hda_get_connections(codec, nid, &nid, 1);
2809                 if (conn < 1)
2810                         continue;
2811                 if (!spec->multiout.dig_out_nid)
2812                         spec->multiout.dig_out_nid = nid;
2813                 else {
2814                         spec->slave_dig_outs[0] = nid;
2815                         break; /* at most two dig outs */
2816                 }
2817         }
2818 }
2819
2820 static int vt1708S_parse_auto_config(struct hda_codec *codec)
2821 {
2822         struct via_spec *spec = codec->spec;
2823         int err;
2824
2825         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2826         if (err < 0)
2827                 return err;
2828         err = vt1708S_auto_fill_dac_nids(spec, &spec->autocfg);
2829         if (err < 0)
2830                 return err;
2831         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2832                 return 0; /* can't find valid BIOS pin config */
2833
2834         err = vt1708S_auto_create_multi_out_ctls(spec, &spec->autocfg);
2835         if (err < 0)
2836                 return err;
2837         err = vt1708S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2838         if (err < 0)
2839                 return err;
2840         err = vt1708S_auto_create_analog_input_ctls(spec, &spec->autocfg);
2841         if (err < 0)
2842                 return err;
2843
2844         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2845
2846         fill_dig_outs(codec);
2847
2848         if (spec->kctls.list)
2849                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
2850
2851         spec->input_mux = &spec->private_imux[0];
2852
2853         if (spec->hp_mux)
2854                 spec->mixers[spec->num_mixers++] = via_hp_mixer;
2855
2856         return 1;
2857 }
2858
2859 #ifdef CONFIG_SND_HDA_POWER_SAVE
2860 static struct hda_amp_list vt1708S_loopbacks[] = {
2861         { 0x16, HDA_INPUT, 1 },
2862         { 0x16, HDA_INPUT, 2 },
2863         { 0x16, HDA_INPUT, 3 },
2864         { 0x16, HDA_INPUT, 4 },
2865         { } /* end */
2866 };
2867 #endif
2868
2869 static int patch_vt1708S(struct hda_codec *codec)
2870 {
2871         struct via_spec *spec;
2872         int err;
2873
2874         /* create a codec specific record */
2875         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2876         if (spec == NULL)
2877                 return -ENOMEM;
2878
2879         codec->spec = spec;
2880
2881         /* automatic parse from the BIOS config */
2882         err = vt1708S_parse_auto_config(codec);
2883         if (err < 0) {
2884                 via_free(codec);
2885                 return err;
2886         } else if (!err) {
2887                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
2888                        "from BIOS.  Using genenic mode...\n");
2889         }
2890
2891         spec->init_verbs[spec->num_iverbs++] = vt1708S_volume_init_verbs;
2892         spec->init_verbs[spec->num_iverbs++] = vt1708S_uniwill_init_verbs;
2893
2894         spec->stream_name_analog = "VT1708S Analog";
2895         spec->stream_analog_playback = &vt1708S_pcm_analog_playback;
2896         spec->stream_analog_capture = &vt1708S_pcm_analog_capture;
2897
2898         spec->stream_name_digital = "VT1708S Digital";
2899         spec->stream_digital_playback = &vt1708S_pcm_digital_playback;
2900
2901         if (!spec->adc_nids && spec->input_mux) {
2902                 spec->adc_nids = vt1708S_adc_nids;
2903                 spec->num_adc_nids = ARRAY_SIZE(vt1708S_adc_nids);
2904                 get_mux_nids(codec);
2905                 spec->mixers[spec->num_mixers] = vt1708S_capture_mixer;
2906                 spec->num_mixers++;
2907         }
2908
2909         codec->patch_ops = via_patch_ops;
2910
2911         codec->patch_ops.init = via_auto_init;
2912         codec->patch_ops.unsol_event = via_unsol_event;
2913 #ifdef CONFIG_SND_HDA_POWER_SAVE
2914         spec->loopback.amplist = vt1708S_loopbacks;
2915 #endif
2916
2917         return 0;
2918 }
2919
2920 /* Patch for VT1702 */
2921
2922 /* capture mixer elements */
2923 static struct snd_kcontrol_new vt1702_capture_mixer[] = {
2924         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_INPUT),
2925         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_INPUT),
2926         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x20, 0x0, HDA_INPUT),
2927         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x20, 0x0, HDA_INPUT),
2928         HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x1F, 0x0, HDA_INPUT),
2929         HDA_CODEC_MUTE("Digital Mic Capture Switch", 0x1F, 0x0, HDA_INPUT),
2930         HDA_CODEC_VOLUME("Digital Mic Boost Capture Volume", 0x1E, 0x0,
2931                          HDA_INPUT),
2932         {
2933                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2934                 /* The multiple "Capture Source" controls confuse alsamixer
2935                  * So call somewhat different..
2936                  */
2937                 /* .name = "Capture Source", */
2938                 .name = "Input Source",
2939                 .count = 1,
2940                 .info = via_mux_enum_info,
2941                 .get = via_mux_enum_get,
2942                 .put = via_mux_enum_put,
2943         },
2944         { } /* end */
2945 };
2946
2947 static struct hda_verb vt1702_volume_init_verbs[] = {
2948         /*
2949          * Unmute ADC0-1 and set the default input to mic-in
2950          */
2951         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2952         {0x1F, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2953         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2954
2955
2956         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2957          * mixer widget
2958          */
2959         /* Amp Indices: Mic1 = 1, Line = 1, Mic2 = 3 */
2960         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2961         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2962         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2963         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2964         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2965
2966         /* Setup default input of PW4 to MW0 */
2967         {0x17, AC_VERB_SET_CONNECT_SEL, 0x1},
2968         /* PW6 PW7 Output enable */
2969         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2970         {0x1C, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2971         { }
2972 };
2973
2974 static struct hda_verb vt1702_uniwill_init_verbs[] = {
2975         {0x01, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_GPIO_EVENT},
2976         {0x17, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_HP_EVENT},
2977         { }
2978 };
2979
2980 static struct hda_pcm_stream vt1702_pcm_analog_playback = {
2981         .substreams = 2,
2982         .channels_min = 2,
2983         .channels_max = 2,
2984         .nid = 0x10, /* NID to query formats and rates */
2985         .ops = {
2986                 .open = via_playback_pcm_open,
2987                 .prepare = via_playback_multi_pcm_prepare,
2988                 .cleanup = via_playback_multi_pcm_cleanup
2989         },
2990 };
2991
2992 static struct hda_pcm_stream vt1702_pcm_analog_capture = {
2993         .substreams = 3,
2994         .channels_min = 2,
2995         .channels_max = 2,
2996         .nid = 0x12, /* NID to query formats and rates */
2997         .ops = {
2998                 .prepare = via_capture_pcm_prepare,
2999                 .cleanup = via_capture_pcm_cleanup
3000         },
3001 };
3002
3003 static struct hda_pcm_stream vt1702_pcm_digital_playback = {
3004         .substreams = 2,
3005         .channels_min = 2,
3006         .channels_max = 2,
3007         /* NID is set in via_build_pcms */
3008         .ops = {
3009                 .open = via_dig_playback_pcm_open,
3010                 .close = via_dig_playback_pcm_close,
3011                 .prepare = via_dig_playback_pcm_prepare,
3012                 .cleanup = via_dig_playback_pcm_cleanup
3013         },
3014 };
3015
3016 /* fill in the dac_nids table from the parsed pin configuration */
3017 static int vt1702_auto_fill_dac_nids(struct via_spec *spec,
3018                                      const struct auto_pin_cfg *cfg)
3019 {
3020         spec->multiout.num_dacs = 1;
3021         spec->multiout.dac_nids = spec->private_dac_nids;
3022
3023         if (cfg->line_out_pins[0]) {
3024                 /* config dac list */
3025                 spec->multiout.dac_nids[0] = 0x10;
3026         }
3027
3028         return 0;
3029 }
3030
3031 /* add playback controls from the parsed DAC table */
3032 static int vt1702_auto_create_line_out_ctls(struct via_spec *spec,
3033                                              const struct auto_pin_cfg *cfg)
3034 {
3035         int err;
3036
3037         if (!cfg->line_out_pins[0])
3038                 return -1;
3039
3040         /* add control to mixer index 0 */
3041         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3042                               "Master Front Playback Volume",
3043                               HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
3044         if (err < 0)
3045                 return err;
3046         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3047                               "Master Front Playback Switch",
3048                               HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
3049         if (err < 0)
3050                 return err;
3051
3052         /* Front */
3053         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3054                               "Front Playback Volume",
3055                               HDA_COMPOSE_AMP_VAL(0x10, 3, 0, HDA_OUTPUT));
3056         if (err < 0)
3057                 return err;
3058         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3059                               "Front Playback Switch",
3060                               HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_OUTPUT));
3061         if (err < 0)
3062                 return err;
3063
3064         return 0;
3065 }
3066
3067 static int vt1702_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
3068 {
3069         int err;
3070
3071         if (!pin)
3072                 return 0;
3073
3074         spec->multiout.hp_nid = 0x1D;
3075
3076         err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3077                               "Headphone Playback Volume",
3078                               HDA_COMPOSE_AMP_VAL(0x1D, 3, 0, HDA_OUTPUT));
3079         if (err < 0)
3080                 return err;
3081
3082         err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3083                               "Headphone Playback Switch",
3084                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3085         if (err < 0)
3086                 return err;
3087
3088         create_hp_imux(spec);
3089
3090         return 0;
3091 }
3092
3093 /* create playback/capture controls for input pins */
3094 static int vt1702_auto_create_analog_input_ctls(struct via_spec *spec,
3095                                                 const struct auto_pin_cfg *cfg)
3096 {
3097         static char *labels[] = {
3098                 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
3099         };
3100         struct hda_input_mux *imux = &spec->private_imux[0];
3101         int i, err, idx = 0;
3102
3103         /* for internal loopback recording select */
3104         imux->items[imux->num_items].label = "Stereo Mixer";
3105         imux->items[imux->num_items].index = 3;
3106         imux->num_items++;
3107
3108         for (i = 0; i < AUTO_PIN_LAST; i++) {
3109                 if (!cfg->input_pins[i])
3110                         continue;
3111
3112                 switch (cfg->input_pins[i]) {
3113                 case 0x14: /* Mic */
3114                         idx = 1;
3115                         break;
3116
3117                 case 0x15: /* Line In */
3118                         idx = 2;
3119                         break;
3120
3121                 case 0x18: /* Front Mic */
3122                         idx = 3;
3123                         break;
3124                 }
3125                 err = via_new_analog_input(spec, cfg->input_pins[i],
3126                                            labels[i], idx, 0x1A);
3127                 if (err < 0)
3128                         return err;
3129                 imux->items[imux->num_items].label = labels[i];
3130                 imux->items[imux->num_items].index = idx-1;
3131                 imux->num_items++;
3132         }
3133         return 0;
3134 }
3135
3136 static int vt1702_parse_auto_config(struct hda_codec *codec)
3137 {
3138         struct via_spec *spec = codec->spec;
3139         int err;
3140
3141         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3142         if (err < 0)
3143                 return err;
3144         err = vt1702_auto_fill_dac_nids(spec, &spec->autocfg);
3145         if (err < 0)
3146                 return err;
3147         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3148                 return 0; /* can't find valid BIOS pin config */
3149
3150         err = vt1702_auto_create_line_out_ctls(spec, &spec->autocfg);
3151         if (err < 0)
3152                 return err;
3153         err = vt1702_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
3154         if (err < 0)
3155                 return err;
3156         err = vt1702_auto_create_analog_input_ctls(spec, &spec->autocfg);
3157         if (err < 0)
3158                 return err;
3159
3160         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3161
3162         fill_dig_outs(codec);
3163
3164         if (spec->kctls.list)
3165                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
3166
3167         spec->input_mux = &spec->private_imux[0];
3168
3169         if (spec->hp_mux)
3170                 spec->mixers[spec->num_mixers++] = via_hp_mixer;
3171
3172         return 1;
3173 }
3174
3175 #ifdef CONFIG_SND_HDA_POWER_SAVE
3176 static struct hda_amp_list vt1702_loopbacks[] = {
3177         { 0x1A, HDA_INPUT, 1 },
3178         { 0x1A, HDA_INPUT, 2 },
3179         { 0x1A, HDA_INPUT, 3 },
3180         { 0x1A, HDA_INPUT, 4 },
3181         { } /* end */
3182 };
3183 #endif
3184
3185 static int patch_vt1702(struct hda_codec *codec)
3186 {
3187         struct via_spec *spec;
3188         int err;
3189         unsigned int response;
3190         unsigned char control;
3191
3192         /* create a codec specific record */
3193         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3194         if (spec == NULL)
3195                 return -ENOMEM;
3196
3197         codec->spec = spec;
3198
3199         /* automatic parse from the BIOS config */
3200         err = vt1702_parse_auto_config(codec);
3201         if (err < 0) {
3202                 via_free(codec);
3203                 return err;
3204         } else if (!err) {
3205                 printk(KERN_INFO "hda_codec: Cannot set up configuration "
3206                        "from BIOS.  Using genenic mode...\n");
3207         }
3208
3209         spec->init_verbs[spec->num_iverbs++] = vt1702_volume_init_verbs;
3210         spec->init_verbs[spec->num_iverbs++] = vt1702_uniwill_init_verbs;
3211
3212         spec->stream_name_analog = "VT1702 Analog";
3213         spec->stream_analog_playback = &vt1702_pcm_analog_playback;
3214         spec->stream_analog_capture = &vt1702_pcm_analog_capture;
3215
3216         spec->stream_name_digital = "VT1702 Digital";
3217         spec->stream_digital_playback = &vt1702_pcm_digital_playback;
3218
3219         if (!spec->adc_nids && spec->input_mux) {
3220                 spec->adc_nids = vt1702_adc_nids;
3221                 spec->num_adc_nids = ARRAY_SIZE(vt1702_adc_nids);
3222                 get_mux_nids(codec);
3223                 spec->mixers[spec->num_mixers] = vt1702_capture_mixer;
3224                 spec->num_mixers++;
3225         }
3226
3227         codec->patch_ops = via_patch_ops;
3228
3229         codec->patch_ops.init = via_auto_init;
3230         codec->patch_ops.unsol_event = via_unsol_event;
3231 #ifdef CONFIG_SND_HDA_POWER_SAVE
3232         spec->loopback.amplist = vt1702_loopbacks;
3233 #endif
3234
3235         /* Open backdoor */
3236         response = snd_hda_codec_read(codec, codec->afg, 0, 0xF8C, 0);
3237         control = (unsigned char)(response & 0xff);
3238         control |= 0x3;
3239         snd_hda_codec_write(codec,  codec->afg, 0, 0xF88, control);
3240
3241         /* Enable GPIO 0&1 for volume&mute control */
3242         /* Enable GPIO 2 for DMIC-DATA */
3243         response = snd_hda_codec_read(codec, codec->afg, 0, 0xF84, 0);
3244         control = (unsigned char)((response >> 16) & 0x3f);
3245         snd_hda_codec_write(codec,  codec->afg, 0, 0xF82, control);
3246
3247         return 0;
3248 }
3249
3250 /*
3251  * patch entries
3252  */
3253 static struct hda_codec_preset snd_hda_preset_via[] = {
3254         { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
3255         { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
3256         { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
3257         { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
3258         { .id = 0x1106e710, .name = "VT1709 10-Ch",
3259           .patch = patch_vt1709_10ch},
3260         { .id = 0x1106e711, .name = "VT1709 10-Ch",
3261           .patch = patch_vt1709_10ch},
3262         { .id = 0x1106e712, .name = "VT1709 10-Ch",
3263           .patch = patch_vt1709_10ch},
3264         { .id = 0x1106e713, .name = "VT1709 10-Ch",
3265           .patch = patch_vt1709_10ch},
3266         { .id = 0x1106e714, .name = "VT1709 6-Ch",
3267           .patch = patch_vt1709_6ch},
3268         { .id = 0x1106e715, .name = "VT1709 6-Ch",
3269           .patch = patch_vt1709_6ch},
3270         { .id = 0x1106e716, .name = "VT1709 6-Ch",
3271           .patch = patch_vt1709_6ch},
3272         { .id = 0x1106e717, .name = "VT1709 6-Ch",
3273           .patch = patch_vt1709_6ch},
3274         { .id = 0x1106e720, .name = "VT1708B 8-Ch",
3275           .patch = patch_vt1708B_8ch},
3276         { .id = 0x1106e721, .name = "VT1708B 8-Ch",
3277           .patch = patch_vt1708B_8ch},
3278         { .id = 0x1106e722, .name = "VT1708B 8-Ch",
3279           .patch = patch_vt1708B_8ch},
3280         { .id = 0x1106e723, .name = "VT1708B 8-Ch",
3281           .patch = patch_vt1708B_8ch},
3282         { .id = 0x1106e724, .name = "VT1708B 4-Ch",
3283           .patch = patch_vt1708B_4ch},
3284         { .id = 0x1106e725, .name = "VT1708B 4-Ch",
3285           .patch = patch_vt1708B_4ch},
3286         { .id = 0x1106e726, .name = "VT1708B 4-Ch",
3287           .patch = patch_vt1708B_4ch},
3288         { .id = 0x1106e727, .name = "VT1708B 4-Ch",
3289           .patch = patch_vt1708B_4ch},
3290         { .id = 0x11060397, .name = "VT1708S",
3291           .patch = patch_vt1708S},
3292         { .id = 0x11061397, .name = "VT1708S",
3293           .patch = patch_vt1708S},
3294         { .id = 0x11062397, .name = "VT1708S",
3295           .patch = patch_vt1708S},
3296         { .id = 0x11063397, .name = "VT1708S",
3297           .patch = patch_vt1708S},
3298         { .id = 0x11064397, .name = "VT1708S",
3299           .patch = patch_vt1708S},
3300         { .id = 0x11065397, .name = "VT1708S",
3301           .patch = patch_vt1708S},
3302         { .id = 0x11066397, .name = "VT1708S",
3303           .patch = patch_vt1708S},
3304         { .id = 0x11067397, .name = "VT1708S",
3305           .patch = patch_vt1708S},
3306         { .id = 0x11060398, .name = "VT1702",
3307           .patch = patch_vt1702},
3308         { .id = 0x11061398, .name = "VT1702",
3309           .patch = patch_vt1702},
3310         { .id = 0x11062398, .name = "VT1702",
3311           .patch = patch_vt1702},
3312         { .id = 0x11063398, .name = "VT1702",
3313           .patch = patch_vt1702},
3314         { .id = 0x11064398, .name = "VT1702",
3315           .patch = patch_vt1702},
3316         { .id = 0x11065398, .name = "VT1702",
3317           .patch = patch_vt1702},
3318         { .id = 0x11066398, .name = "VT1702",
3319           .patch = patch_vt1702},
3320         { .id = 0x11067398, .name = "VT1702",
3321           .patch = patch_vt1702},
3322         {} /* terminator */
3323 };
3324
3325 MODULE_ALIAS("snd-hda-codec-id:1106*");
3326
3327 static struct hda_codec_preset_list via_list = {
3328         .preset = snd_hda_preset_via,
3329         .owner = THIS_MODULE,
3330 };
3331
3332 MODULE_LICENSE("GPL");
3333 MODULE_DESCRIPTION("VIA HD-audio codec");
3334
3335 static int __init patch_via_init(void)
3336 {
3337         return snd_hda_add_codec_preset(&via_list);
3338 }
3339
3340 static void __exit patch_via_exit(void)
3341 {
3342         snd_hda_delete_codec_preset(&via_list);
3343 }
3344
3345 module_init(patch_via_init)
3346 module_exit(patch_via_exit)