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