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