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