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