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