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