f91c4db038e45d58ff59925a5d265455f2fac702
[pandora-kernel.git] / sound / pci / hda / patch_via.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * HD audio interface patch for VIA VT17xx/VT18xx/VT20xx codec
5  *
6  *  (C) 2006-2009 VIA Technology, Inc.
7  *  (C) 2006-2008 Takashi Iwai <tiwai@suse.de>
8  *
9  *  This driver is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This driver is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  */
23
24 /* * * * * * * * * * * * * * Release History * * * * * * * * * * * * * * * * */
25 /*                                                                           */
26 /* 2006-03-03  Lydia Wang  Create the basic patch to support VT1708 codec    */
27 /* 2006-03-14  Lydia Wang  Modify hard code for some pin widget nid          */
28 /* 2006-08-02  Lydia Wang  Add support to VT1709 codec                       */
29 /* 2006-09-08  Lydia Wang  Fix internal loopback recording source select bug */
30 /* 2007-09-12  Lydia Wang  Add EAPD enable during driver initialization      */
31 /* 2007-09-17  Lydia Wang  Add VT1708B codec support                        */
32 /* 2007-11-14  Lydia Wang  Add VT1708A codec HP and CD pin connect config    */
33 /* 2008-02-03  Lydia Wang  Fix Rear channels and Back channels inverse issue */
34 /* 2008-03-06  Lydia Wang  Add VT1702 codec and VT1708S codec support        */
35 /* 2008-04-09  Lydia Wang  Add mute front speaker when HP plugin             */
36 /* 2008-04-09  Lydia Wang  Add Independent HP feature                        */
37 /* 2008-05-28  Lydia Wang  Add second S/PDIF Out support for VT1702          */
38 /* 2008-09-15  Logan Li    Add VT1708S Mic Boost workaround/backdoor         */
39 /* 2009-02-16  Logan Li    Add support for VT1718S                           */
40 /* 2009-03-13  Logan Li    Add support for VT1716S                           */
41 /* 2009-04-14  Lydai Wang  Add support for VT1828S and VT2020                */
42 /* 2009-07-08  Lydia Wang  Add support for VT2002P                           */
43 /* 2009-07-21  Lydia Wang  Add support for VT1812                            */
44 /* 2009-09-19  Lydia Wang  Add support for VT1818S                           */
45 /*                                                                           */
46 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
47
48
49 #include <linux/init.h>
50 #include <linux/delay.h>
51 #include <linux/slab.h>
52 #include <sound/core.h>
53 #include <sound/asoundef.h>
54 #include "hda_codec.h"
55 #include "hda_local.h"
56
57 #define NID_MAPPING             (-1)
58
59 /* amp values */
60 #define AMP_VAL_IDX_SHIFT       19
61 #define AMP_VAL_IDX_MASK        (0x0f<<19)
62
63 /* Pin Widget NID */
64 #define VT1708_HP_NID           0x13
65 #define VT1708_DIGOUT_NID       0x14
66 #define VT1708_DIGIN_NID        0x16
67 #define VT1708_DIGIN_PIN        0x26
68 #define VT1708_HP_PIN_NID       0x20
69 #define VT1708_CD_PIN_NID       0x24
70
71 #define VT1709_HP_DAC_NID       0x28
72 #define VT1709_DIGOUT_NID       0x13
73 #define VT1709_DIGIN_NID        0x17
74 #define VT1709_DIGIN_PIN        0x25
75
76 #define VT1708B_HP_NID          0x25
77 #define VT1708B_DIGOUT_NID      0x12
78 #define VT1708B_DIGIN_NID       0x15
79 #define VT1708B_DIGIN_PIN       0x21
80
81 #define VT1708S_HP_NID          0x25
82 #define VT1708S_DIGOUT_NID      0x12
83
84 #define VT1702_HP_NID           0x17
85 #define VT1702_DIGOUT_NID       0x11
86
87 enum VIA_HDA_CODEC {
88         UNKNOWN = -1,
89         VT1708,
90         VT1709_10CH,
91         VT1709_6CH,
92         VT1708B_8CH,
93         VT1708B_4CH,
94         VT1708S,
95         VT1708BCE,
96         VT1702,
97         VT1718S,
98         VT1716S,
99         VT2002P,
100         VT1812,
101         VT1802,
102         CODEC_TYPES,
103 };
104
105 #define VT2002P_COMPATIBLE(spec) \
106         ((spec)->codec_type == VT2002P ||\
107          (spec)->codec_type == VT1812 ||\
108          (spec)->codec_type == VT1802)
109
110 struct nid_path {
111         int depth;
112         hda_nid_t path[5];
113         short idx[5];
114 };
115
116 struct via_spec {
117         /* codec parameterization */
118         const struct snd_kcontrol_new *mixers[6];
119         unsigned int num_mixers;
120
121         const struct hda_verb *init_verbs[5];
122         unsigned int num_iverbs;
123
124         char stream_name_analog[32];
125         char stream_name_hp[32];
126         const struct hda_pcm_stream *stream_analog_playback;
127         const struct hda_pcm_stream *stream_analog_capture;
128
129         char stream_name_digital[32];
130         const struct hda_pcm_stream *stream_digital_playback;
131         const struct hda_pcm_stream *stream_digital_capture;
132
133         /* playback */
134         struct hda_multi_out multiout;
135         hda_nid_t slave_dig_outs[2];
136
137         struct nid_path out_path[4];
138         struct nid_path hp_path;
139         struct nid_path hp_dep_path;
140
141         /* capture */
142         unsigned int num_adc_nids;
143         hda_nid_t adc_nids[3];
144         hda_nid_t mux_nids[3];
145         hda_nid_t aa_mix_nid;
146         hda_nid_t dig_in_nid;
147         hda_nid_t dig_in_pin;
148
149         /* capture source */
150         const struct hda_input_mux *input_mux;
151         unsigned int cur_mux[3];
152
153         /* PCM information */
154         struct hda_pcm pcm_rec[3];
155
156         /* dynamic controls, init_verbs and input_mux */
157         struct auto_pin_cfg autocfg;
158         struct snd_array kctls;
159         struct hda_input_mux private_imux[2];
160         hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
161
162         /* HP mode source */
163         const struct hda_input_mux *hp_mux;
164         unsigned int hp_independent_mode;
165         unsigned int hp_independent_mode_index;
166         unsigned int can_smart51;
167         unsigned int smart51_enabled;
168         unsigned int dmic_enabled;
169         unsigned int no_pin_power_ctl;
170         enum VIA_HDA_CODEC codec_type;
171
172         /* work to check hp jack state */
173         struct hda_codec *codec;
174         struct delayed_work vt1708_hp_work;
175         int vt1708_jack_detect;
176         int vt1708_hp_present;
177
178         void (*set_widgets_power_state)(struct hda_codec *codec);
179
180 #ifdef CONFIG_SND_HDA_POWER_SAVE
181         struct hda_loopback_check loopback;
182 #endif
183 };
184
185 static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
186 static struct via_spec * via_new_spec(struct hda_codec *codec)
187 {
188         struct via_spec *spec;
189
190         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
191         if (spec == NULL)
192                 return NULL;
193
194         codec->spec = spec;
195         spec->codec = codec;
196         spec->codec_type = get_codec_type(codec);
197         /* VT1708BCE & VT1708S are almost same */
198         if (spec->codec_type == VT1708BCE)
199                 spec->codec_type = VT1708S;
200         return spec;
201 }
202
203 static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
204 {
205         u32 vendor_id = codec->vendor_id;
206         u16 ven_id = vendor_id >> 16;
207         u16 dev_id = vendor_id & 0xffff;
208         enum VIA_HDA_CODEC codec_type;
209
210         /* get codec type */
211         if (ven_id != 0x1106)
212                 codec_type = UNKNOWN;
213         else if (dev_id >= 0x1708 && dev_id <= 0x170b)
214                 codec_type = VT1708;
215         else if (dev_id >= 0xe710 && dev_id <= 0xe713)
216                 codec_type = VT1709_10CH;
217         else if (dev_id >= 0xe714 && dev_id <= 0xe717)
218                 codec_type = VT1709_6CH;
219         else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
220                 codec_type = VT1708B_8CH;
221                 if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
222                         codec_type = VT1708BCE;
223         } else if (dev_id >= 0xe724 && dev_id <= 0xe727)
224                 codec_type = VT1708B_4CH;
225         else if ((dev_id & 0xfff) == 0x397
226                  && (dev_id >> 12) < 8)
227                 codec_type = VT1708S;
228         else if ((dev_id & 0xfff) == 0x398
229                  && (dev_id >> 12) < 8)
230                 codec_type = VT1702;
231         else if ((dev_id & 0xfff) == 0x428
232                  && (dev_id >> 12) < 8)
233                 codec_type = VT1718S;
234         else if (dev_id == 0x0433 || dev_id == 0xa721)
235                 codec_type = VT1716S;
236         else if (dev_id == 0x0441 || dev_id == 0x4441)
237                 codec_type = VT1718S;
238         else if (dev_id == 0x0438 || dev_id == 0x4438)
239                 codec_type = VT2002P;
240         else if (dev_id == 0x0448)
241                 codec_type = VT1812;
242         else if (dev_id == 0x0440)
243                 codec_type = VT1708S;
244         else if ((dev_id & 0xfff) == 0x446)
245                 codec_type = VT1802;
246         else
247                 codec_type = UNKNOWN;
248         return codec_type;
249 };
250
251 #define VIA_JACK_EVENT          0x20
252 #define VIA_HP_EVENT            0x01
253 #define VIA_GPIO_EVENT          0x02
254 #define VIA_MONO_EVENT          0x03
255 #define VIA_SPEAKER_EVENT       0x04
256 #define VIA_BIND_HP_EVENT       0x05
257
258 enum {
259         VIA_CTL_WIDGET_VOL,
260         VIA_CTL_WIDGET_MUTE,
261         VIA_CTL_WIDGET_ANALOG_MUTE,
262         VIA_CTL_WIDGET_BIND_PIN_MUTE,
263 };
264
265 enum {
266         AUTO_SEQ_FRONT = 0,
267         AUTO_SEQ_SURROUND,
268         AUTO_SEQ_CENLFE,
269         AUTO_SEQ_SIDE
270 };
271
272 static void analog_low_current_mode(struct hda_codec *codec, int stream_idle);
273 static int is_aa_path_mute(struct hda_codec *codec);
274
275 static void vt1708_start_hp_work(struct via_spec *spec)
276 {
277         if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
278                 return;
279         snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
280                             !spec->vt1708_jack_detect);
281         if (!delayed_work_pending(&spec->vt1708_hp_work))
282                 schedule_delayed_work(&spec->vt1708_hp_work,
283                                       msecs_to_jiffies(100));
284 }
285
286 static void vt1708_stop_hp_work(struct via_spec *spec)
287 {
288         if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
289                 return;
290         if (snd_hda_get_bool_hint(spec->codec, "analog_loopback_hp_detect") == 1
291             && !is_aa_path_mute(spec->codec))
292                 return;
293         snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
294                             !spec->vt1708_jack_detect);
295         cancel_delayed_work_sync(&spec->vt1708_hp_work);
296 }
297
298 static void set_widgets_power_state(struct hda_codec *codec)
299 {
300         struct via_spec *spec = codec->spec;
301         if (spec->set_widgets_power_state)
302                 spec->set_widgets_power_state(codec);
303 }
304
305 static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
306                                    struct snd_ctl_elem_value *ucontrol)
307 {
308         int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
309         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
310
311         set_widgets_power_state(codec);
312         analog_low_current_mode(snd_kcontrol_chip(kcontrol), -1);
313         if (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1) {
314                 if (is_aa_path_mute(codec))
315                         vt1708_start_hp_work(codec->spec);
316                 else
317                         vt1708_stop_hp_work(codec->spec);
318         }
319         return change;
320 }
321
322 /* modify .put = snd_hda_mixer_amp_switch_put */
323 #define ANALOG_INPUT_MUTE                                               \
324         {               .iface = SNDRV_CTL_ELEM_IFACE_MIXER,            \
325                         .name = NULL,                                   \
326                         .index = 0,                                     \
327                         .info = snd_hda_mixer_amp_switch_info,          \
328                         .get = snd_hda_mixer_amp_switch_get,            \
329                         .put = analog_input_switch_put,                 \
330                         .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
331
332 static void via_hp_bind_automute(struct hda_codec *codec);
333
334 static int bind_pin_switch_put(struct snd_kcontrol *kcontrol,
335                                struct snd_ctl_elem_value *ucontrol)
336 {
337         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
338         struct via_spec *spec = codec->spec;
339         int i;
340         int change = 0;
341
342         long *valp = ucontrol->value.integer.value;
343         int lmute, rmute;
344         if (strstr(kcontrol->id.name, "Switch") == NULL) {
345                 snd_printd("Invalid control!\n");
346                 return change;
347         }
348         change = snd_hda_mixer_amp_switch_put(kcontrol,
349                                               ucontrol);
350         /* Get mute value */
351         lmute = *valp ? 0 : HDA_AMP_MUTE;
352         valp++;
353         rmute = *valp ? 0 : HDA_AMP_MUTE;
354
355         /* Set hp pins */
356         if (!spec->hp_independent_mode) {
357                 for (i = 0; i < spec->autocfg.hp_outs; i++) {
358                         snd_hda_codec_amp_update(
359                                 codec, spec->autocfg.hp_pins[i],
360                                 0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
361                                 lmute);
362                         snd_hda_codec_amp_update(
363                                 codec, spec->autocfg.hp_pins[i],
364                                 1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
365                                 rmute);
366                 }
367         }
368
369         if (!lmute && !rmute) {
370                 /* Line Outs */
371                 for (i = 0; i < spec->autocfg.line_outs; i++)
372                         snd_hda_codec_amp_stereo(
373                                 codec, spec->autocfg.line_out_pins[i],
374                                 HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
375                 /* Speakers */
376                 for (i = 0; i < spec->autocfg.speaker_outs; i++)
377                         snd_hda_codec_amp_stereo(
378                                 codec, spec->autocfg.speaker_pins[i],
379                                 HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
380                 /* unmute */
381                 via_hp_bind_automute(codec);
382
383         } else {
384                 if (lmute) {
385                         /* Mute all left channels */
386                         for (i = 1; i < spec->autocfg.line_outs; i++)
387                                 snd_hda_codec_amp_update(
388                                         codec,
389                                         spec->autocfg.line_out_pins[i],
390                                         0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
391                                         lmute);
392                         for (i = 0; i < spec->autocfg.speaker_outs; i++)
393                                 snd_hda_codec_amp_update(
394                                         codec,
395                                         spec->autocfg.speaker_pins[i],
396                                         0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
397                                         lmute);
398                 }
399                 if (rmute) {
400                         /* mute all right channels */
401                         for (i = 1; i < spec->autocfg.line_outs; i++)
402                                 snd_hda_codec_amp_update(
403                                         codec,
404                                         spec->autocfg.line_out_pins[i],
405                                         1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
406                                         rmute);
407                         for (i = 0; i < spec->autocfg.speaker_outs; i++)
408                                 snd_hda_codec_amp_update(
409                                         codec,
410                                         spec->autocfg.speaker_pins[i],
411                                         1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
412                                         rmute);
413                 }
414         }
415         return change;
416 }
417
418 #define BIND_PIN_MUTE                                                   \
419         {               .iface = SNDRV_CTL_ELEM_IFACE_MIXER,            \
420                         .name = NULL,                                   \
421                         .index = 0,                                     \
422                         .info = snd_hda_mixer_amp_switch_info,          \
423                         .get = snd_hda_mixer_amp_switch_get,            \
424                         .put = bind_pin_switch_put,                     \
425                         .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
426
427 static const struct snd_kcontrol_new via_control_templates[] = {
428         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
429         HDA_CODEC_MUTE(NULL, 0, 0, 0),
430         ANALOG_INPUT_MUTE,
431         BIND_PIN_MUTE,
432 };
433
434
435 /* add dynamic controls */
436 static struct snd_kcontrol_new *__via_clone_ctl(struct via_spec *spec,
437                                 const struct snd_kcontrol_new *tmpl,
438                                 const char *name)
439 {
440         struct snd_kcontrol_new *knew;
441
442         snd_array_init(&spec->kctls, sizeof(*knew), 32);
443         knew = snd_array_new(&spec->kctls);
444         if (!knew)
445                 return NULL;
446         *knew = *tmpl;
447         if (!name)
448                 name = tmpl->name;
449         if (name) {
450                 knew->name = kstrdup(name, GFP_KERNEL);
451                 if (!knew->name)
452                         return NULL;
453         }
454         return knew;
455 }
456
457 static int __via_add_control(struct via_spec *spec, int type, const char *name,
458                              int idx, unsigned long val)
459 {
460         struct snd_kcontrol_new *knew;
461
462         knew = __via_clone_ctl(spec, &via_control_templates[type], name);
463         if (!knew)
464                 return -ENOMEM;
465         knew->index = idx;
466         if (get_amp_nid_(val))
467                 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
468         knew->private_value = val;
469         return 0;
470 }
471
472 #define via_add_control(spec, type, name, val) \
473         __via_add_control(spec, type, name, 0, val)
474
475 #define via_clone_control(spec, tmpl) __via_clone_ctl(spec, tmpl, NULL)
476
477 static void via_free_kctls(struct hda_codec *codec)
478 {
479         struct via_spec *spec = codec->spec;
480
481         if (spec->kctls.list) {
482                 struct snd_kcontrol_new *kctl = spec->kctls.list;
483                 int i;
484                 for (i = 0; i < spec->kctls.used; i++)
485                         kfree(kctl[i].name);
486         }
487         snd_array_free(&spec->kctls);
488 }
489
490 /* create input playback/capture controls for the given pin */
491 static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
492                                 int type_idx, int idx, int mix_nid)
493 {
494         char name[32];
495         int err;
496
497         sprintf(name, "%s Playback Volume", ctlname);
498         err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
499                               HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
500         if (err < 0)
501                 return err;
502         sprintf(name, "%s Playback Switch", ctlname);
503         err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
504                               HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
505         if (err < 0)
506                 return err;
507         return 0;
508 }
509
510 static void via_auto_set_output_and_unmute(struct hda_codec *codec,
511                                            hda_nid_t nid, int pin_type,
512                                            int dac_idx)
513 {
514         /* set as output */
515         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
516                             pin_type);
517         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
518                             AMP_OUT_UNMUTE);
519         if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
520                 snd_hda_codec_write(codec, nid, 0,
521                                     AC_VERB_SET_EAPD_BTLENABLE, 0x02);
522 }
523
524
525 static void via_auto_init_multi_out(struct hda_codec *codec)
526 {
527         struct via_spec *spec = codec->spec;
528         int i;
529
530         for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
531                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
532                 if (nid)
533                         via_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
534         }
535 }
536
537 static void via_auto_init_hp_out(struct hda_codec *codec)
538 {
539         struct via_spec *spec = codec->spec;
540         hda_nid_t pin;
541         int i;
542
543         for (i = 0; i < spec->autocfg.hp_outs; i++) {
544                 pin = spec->autocfg.hp_pins[i];
545                 if (pin) /* connect to front */
546                         via_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
547         }
548 }
549
550 static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin);
551
552 static void via_auto_init_analog_input(struct hda_codec *codec)
553 {
554         struct via_spec *spec = codec->spec;
555         const struct auto_pin_cfg *cfg = &spec->autocfg;
556         unsigned int ctl;
557         int i;
558
559         for (i = 0; i < cfg->num_inputs; i++) {
560                 hda_nid_t nid = cfg->inputs[i].pin;
561                 if (spec->smart51_enabled && is_smart51_pins(codec, nid))
562                         ctl = PIN_OUT;
563                 else if (cfg->inputs[i].type == AUTO_PIN_MIC)
564                         ctl = PIN_VREF50;
565                 else
566                         ctl = PIN_IN;
567                 snd_hda_codec_write(codec, nid, 0,
568                                     AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
569         }
570 }
571
572 static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
573                                 unsigned int *affected_parm)
574 {
575         unsigned parm;
576         unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
577         unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
578                 >> AC_DEFCFG_MISC_SHIFT
579                 & AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
580         struct via_spec *spec = codec->spec;
581         unsigned present = 0;
582
583         no_presence |= spec->no_pin_power_ctl;
584         if (!no_presence)
585                 present = snd_hda_jack_detect(codec, nid);
586         if ((spec->smart51_enabled && is_smart51_pins(codec, nid))
587             || ((no_presence || present)
588                 && get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
589                 *affected_parm = AC_PWRST_D0; /* if it's connected */
590                 parm = AC_PWRST_D0;
591         } else
592                 parm = AC_PWRST_D3;
593
594         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
595 }
596
597 static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
598                                   struct snd_ctl_elem_info *uinfo)
599 {
600         static const char * const texts[] = {
601                 "Disabled", "Enabled"
602         };
603
604         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
605         uinfo->count = 1;
606         uinfo->value.enumerated.items = 2;
607         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
608                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
609         strcpy(uinfo->value.enumerated.name,
610                texts[uinfo->value.enumerated.item]);
611         return 0;
612 }
613
614 static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
615                                  struct snd_ctl_elem_value *ucontrol)
616 {
617         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
618         struct via_spec *spec = codec->spec;
619         ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
620         return 0;
621 }
622
623 static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
624                                  struct snd_ctl_elem_value *ucontrol)
625 {
626         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
627         struct via_spec *spec = codec->spec;
628         unsigned int val = !ucontrol->value.enumerated.item[0];
629
630         if (val == spec->no_pin_power_ctl)
631                 return 0;
632         spec->no_pin_power_ctl = val;
633         set_widgets_power_state(codec);
634         return 1;
635 }
636
637 static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
638         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
639         .name = "Dynamic Power-Control",
640         .info = via_pin_power_ctl_info,
641         .get = via_pin_power_ctl_get,
642         .put = via_pin_power_ctl_put,
643 };
644
645
646 /*
647  * input MUX handling
648  */
649 static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
650                              struct snd_ctl_elem_info *uinfo)
651 {
652         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
653         struct via_spec *spec = codec->spec;
654         return snd_hda_input_mux_info(spec->input_mux, uinfo);
655 }
656
657 static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
658                             struct snd_ctl_elem_value *ucontrol)
659 {
660         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
661         struct via_spec *spec = codec->spec;
662         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
663
664         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
665         return 0;
666 }
667
668 static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
669                             struct snd_ctl_elem_value *ucontrol)
670 {
671         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
672         struct via_spec *spec = codec->spec;
673         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
674         int ret;
675
676         if (!spec->mux_nids[adc_idx])
677                 return -EINVAL;
678         /* switch to D0 beofre change index */
679         if (snd_hda_codec_read(codec, spec->mux_nids[adc_idx], 0,
680                                AC_VERB_GET_POWER_STATE, 0x00) != AC_PWRST_D0)
681                 snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
682                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
683
684         ret = snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
685                                      spec->mux_nids[adc_idx],
686                                      &spec->cur_mux[adc_idx]);
687         /* update jack power state */
688         set_widgets_power_state(codec);
689
690         return ret;
691 }
692
693 static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
694                                    struct snd_ctl_elem_info *uinfo)
695 {
696         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
697         struct via_spec *spec = codec->spec;
698         return snd_hda_input_mux_info(spec->hp_mux, uinfo);
699 }
700
701 static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
702                                   struct snd_ctl_elem_value *ucontrol)
703 {
704         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
705         hda_nid_t nid = kcontrol->private_value;
706         unsigned int pinsel;
707
708         /* use !! to translate conn sel 2 for VT1718S */
709         pinsel = !!snd_hda_codec_read(codec, nid, 0,
710                                       AC_VERB_GET_CONNECT_SEL,
711                                       0x00);
712         ucontrol->value.enumerated.item[0] = pinsel;
713
714         return 0;
715 }
716
717 static void activate_ctl(struct hda_codec *codec, const char *name, int active)
718 {
719         struct snd_kcontrol *ctl = snd_hda_find_mixer_ctl(codec, name);
720         if (ctl) {
721                 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
722                 ctl->vd[0].access |= active
723                         ? 0 : SNDRV_CTL_ELEM_ACCESS_INACTIVE;
724                 snd_ctl_notify(codec->bus->card,
725                                SNDRV_CTL_EVENT_MASK_VALUE, &ctl->id);
726         }
727 }
728
729 static hda_nid_t side_mute_channel(struct via_spec *spec)
730 {
731         switch (spec->codec_type) {
732         case VT1708:            return 0x1b;
733         case VT1709_10CH:       return 0x29;
734         case VT1708B_8CH:       /* fall thru */
735         case VT1708S:           return 0x27;
736         case VT2002P:           return 0x19;
737         case VT1802:            return 0x15;
738         case VT1812:            return 0x15;
739         default:                return 0;
740         }
741 }
742
743 static int update_side_mute_status(struct hda_codec *codec)
744 {
745         /* mute side channel */
746         struct via_spec *spec = codec->spec;
747         unsigned int parm;
748         hda_nid_t sw3 = side_mute_channel(spec);
749
750         if (sw3) {
751                 if (VT2002P_COMPATIBLE(spec))
752                         parm = spec->hp_independent_mode ?
753                                AMP_IN_MUTE(1) : AMP_IN_UNMUTE(1);
754                 else
755                         parm = spec->hp_independent_mode ?
756                                AMP_OUT_MUTE : AMP_OUT_UNMUTE;
757                 snd_hda_codec_write(codec, sw3, 0,
758                                     AC_VERB_SET_AMP_GAIN_MUTE, parm);
759                 if (spec->codec_type == VT1812)
760                         snd_hda_codec_write(codec, 0x1d, 0,
761                                             AC_VERB_SET_AMP_GAIN_MUTE, parm);
762         }
763         return 0;
764 }
765
766 static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
767                                   struct snd_ctl_elem_value *ucontrol)
768 {
769         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
770         struct via_spec *spec = codec->spec;
771         hda_nid_t nid = kcontrol->private_value;
772         unsigned int pinsel = ucontrol->value.enumerated.item[0];
773         /* Get Independent Mode index of headphone pin widget */
774         spec->hp_independent_mode = spec->hp_independent_mode_index == pinsel
775                 ? 1 : 0;
776         if (spec->codec_type == VT1718S)
777                 snd_hda_codec_write(codec, nid, 0,
778                                     AC_VERB_SET_CONNECT_SEL, pinsel ? 2 : 0);
779         else
780                 snd_hda_codec_write(codec, nid, 0,
781                                     AC_VERB_SET_CONNECT_SEL, pinsel);
782
783         if (spec->codec_type == VT1812)
784                 snd_hda_codec_write(codec, 0x35, 0,
785                                     AC_VERB_SET_CONNECT_SEL, pinsel);
786         if (spec->multiout.hp_nid && spec->multiout.hp_nid
787             != spec->multiout.dac_nids[HDA_FRONT])
788                 snd_hda_codec_setup_stream(codec, spec->multiout.hp_nid,
789                                            0, 0, 0);
790
791         update_side_mute_status(codec);
792         /* update HP volume/swtich active state */
793         if (spec->codec_type == VT1708S
794             || spec->codec_type == VT1702
795             || spec->codec_type == VT1718S
796             || spec->codec_type == VT1716S
797             || VT2002P_COMPATIBLE(spec)) {
798                 activate_ctl(codec, "Headphone Playback Volume",
799                              spec->hp_independent_mode);
800                 activate_ctl(codec, "Headphone Playback Switch",
801                              spec->hp_independent_mode);
802         }
803         /* update jack power state */
804         set_widgets_power_state(codec);
805         return 0;
806 }
807
808 static const struct snd_kcontrol_new via_hp_mixer[2] = {
809         {
810                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
811                 .name = "Independent HP",
812                 .info = via_independent_hp_info,
813                 .get = via_independent_hp_get,
814                 .put = via_independent_hp_put,
815         },
816         {
817                 .iface = NID_MAPPING,
818                 .name = "Independent HP",
819         },
820 };
821
822 static int via_hp_build(struct hda_codec *codec)
823 {
824         struct via_spec *spec = codec->spec;
825         struct snd_kcontrol_new *knew;
826         hda_nid_t nid;
827         int nums;
828         hda_nid_t conn[HDA_MAX_CONNECTIONS];
829
830         switch (spec->codec_type) {
831         case VT1718S:
832                 nid = 0x34;
833                 break;
834         case VT2002P:
835         case VT1802:
836                 nid = 0x35;
837                 break;
838         case VT1812:
839                 nid = 0x3d;
840                 break;
841         default:
842                 nid = spec->autocfg.hp_pins[0];
843                 break;
844         }
845
846         if (spec->codec_type != VT1708) {
847                 nums = snd_hda_get_connections(codec, nid,
848                                                conn, HDA_MAX_CONNECTIONS);
849                 if (nums <= 1)
850                         return 0;
851         }
852
853         knew = via_clone_control(spec, &via_hp_mixer[0]);
854         if (knew == NULL)
855                 return -ENOMEM;
856
857         knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
858         knew->private_value = nid;
859
860         knew = via_clone_control(spec, &via_hp_mixer[1]);
861         if (knew == NULL)
862                 return -ENOMEM;
863         knew->subdevice = side_mute_channel(spec);
864
865         return 0;
866 }
867
868 static void notify_aa_path_ctls(struct hda_codec *codec)
869 {
870         int i;
871         struct snd_ctl_elem_id id;
872         const char *labels[] = {"Mic", "Front Mic", "Line", "Rear Mic"};
873         struct snd_kcontrol *ctl;
874
875         memset(&id, 0, sizeof(id));
876         id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
877         for (i = 0; i < ARRAY_SIZE(labels); i++) {
878                 sprintf(id.name, "%s Playback Volume", labels[i]);
879                 ctl = snd_hda_find_mixer_ctl(codec, id.name);
880                 if (ctl)
881                         snd_ctl_notify(codec->bus->card,
882                                         SNDRV_CTL_EVENT_MASK_VALUE,
883                                         &ctl->id);
884         }
885 }
886
887 static void mute_aa_path(struct hda_codec *codec, int mute)
888 {
889         struct via_spec *spec = codec->spec;
890         int start_idx;
891         int end_idx;
892         int i;
893         /* get nid of MW0 and start & end index */
894         switch (spec->codec_type) {
895         case VT1708:
896                 start_idx = 2;
897                 end_idx = 4;
898                 break;
899         case VT1709_10CH:
900         case VT1709_6CH:
901                 start_idx = 2;
902                 end_idx = 4;
903                 break;
904         case VT1708B_8CH:
905         case VT1708B_4CH:
906         case VT1708S:
907         case VT1716S:
908                 start_idx = 2;
909                 end_idx = 4;
910                 break;
911         case VT1718S:
912                 start_idx = 1;
913                 end_idx = 3;
914                 break;
915         default:
916                 return;
917         }
918         /* check AA path's mute status */
919         for (i = start_idx; i <= end_idx; i++) {
920                 int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
921                 snd_hda_codec_amp_stereo(codec, spec->aa_mix_nid, HDA_INPUT, i,
922                                          HDA_AMP_MUTE, val);
923         }
924 }
925
926 static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
927 {
928         struct via_spec *spec = codec->spec;
929         const struct auto_pin_cfg *cfg = &spec->autocfg;
930         int i;
931
932         for (i = 0; i < cfg->num_inputs; i++) {
933                 unsigned int defcfg;
934                 if (pin != cfg->inputs[i].pin)
935                         continue;
936                 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
937                         return false;
938                 defcfg = snd_hda_codec_get_pincfg(codec, pin);
939                 if (snd_hda_get_input_pin_attr(defcfg) < INPUT_PIN_ATTR_NORMAL)
940                         return false;
941                 return true;
942         }
943         return false;
944 }
945
946 static int via_smart51_info(struct snd_kcontrol *kcontrol,
947                             struct snd_ctl_elem_info *uinfo)
948 {
949         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
950         uinfo->count = 1;
951         uinfo->value.integer.min = 0;
952         uinfo->value.integer.max = 1;
953         return 0;
954 }
955
956 static int via_smart51_get(struct snd_kcontrol *kcontrol,
957                            struct snd_ctl_elem_value *ucontrol)
958 {
959         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
960         struct via_spec *spec = codec->spec;
961         const struct auto_pin_cfg *cfg = &spec->autocfg;
962         int on = 1;
963         int i;
964
965         for (i = 0; i < cfg->num_inputs; i++) {
966                 hda_nid_t nid = cfg->inputs[i].pin;
967                 unsigned int ctl;
968                 if (cfg->inputs[i].type == AUTO_PIN_MIC &&
969                     spec->hp_independent_mode && spec->codec_type != VT1718S)
970                         continue; /* ignore FMic for independent HP */
971                 if (!is_smart51_pins(codec, nid))
972                         continue;
973                 ctl = snd_hda_codec_read(codec, nid, 0,
974                                          AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
975                 if ((ctl & AC_PINCTL_IN_EN) && !(ctl & AC_PINCTL_OUT_EN))
976                         on = 0;
977         }
978         *ucontrol->value.integer.value = on;
979         return 0;
980 }
981
982 static int via_smart51_put(struct snd_kcontrol *kcontrol,
983                            struct snd_ctl_elem_value *ucontrol)
984 {
985         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
986         struct via_spec *spec = codec->spec;
987         const struct auto_pin_cfg *cfg = &spec->autocfg;
988         int out_in = *ucontrol->value.integer.value
989                 ? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
990         int i;
991
992         for (i = 0; i < cfg->num_inputs; i++) {
993                 hda_nid_t nid = cfg->inputs[i].pin;
994                 unsigned int parm;
995
996                 if (cfg->inputs[i].type == AUTO_PIN_MIC &&
997                     spec->hp_independent_mode && spec->codec_type != VT1718S)
998                         continue; /* don't retask FMic for independent HP */
999                 if (!is_smart51_pins(codec, nid))
1000                         continue;
1001
1002                 parm = snd_hda_codec_read(codec, nid, 0,
1003                                           AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1004                 parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
1005                 parm |= out_in;
1006                 snd_hda_codec_write(codec, nid, 0,
1007                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1008                                     parm);
1009                 if (out_in == AC_PINCTL_OUT_EN) {
1010                         mute_aa_path(codec, 1);
1011                         notify_aa_path_ctls(codec);
1012                 }
1013         }
1014         spec->smart51_enabled = *ucontrol->value.integer.value;
1015         set_widgets_power_state(codec);
1016         return 1;
1017 }
1018
1019 static const struct snd_kcontrol_new via_smart51_mixer = {
1020         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1021         .name = "Smart 5.1",
1022         .count = 1,
1023         .info = via_smart51_info,
1024         .get = via_smart51_get,
1025         .put = via_smart51_put,
1026 };
1027
1028 static int via_smart51_build(struct hda_codec *codec)
1029 {
1030         struct via_spec *spec = codec->spec;
1031         struct snd_kcontrol_new *knew;
1032         const struct auto_pin_cfg *cfg = &spec->autocfg;
1033         hda_nid_t nid;
1034         int i;
1035
1036         if (!spec->can_smart51)
1037                 return 0;
1038
1039         knew = via_clone_control(spec, &via_smart51_mixer);
1040         if (knew == NULL)
1041                 return -ENOMEM;
1042
1043         for (i = 0; i < cfg->num_inputs; i++) {
1044                 nid = cfg->inputs[i].pin;
1045                 if (is_smart51_pins(codec, nid)) {
1046                         knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
1047                         break;
1048                 }
1049         }
1050
1051         return 0;
1052 }
1053
1054 /* check AA path's mute statue */
1055 static int is_aa_path_mute(struct hda_codec *codec)
1056 {
1057         int mute = 1;
1058         int start_idx;
1059         int end_idx;
1060         int i;
1061         struct via_spec *spec = codec->spec;
1062         /* get nid of MW0 and start & end index */
1063         switch (spec->codec_type) {
1064         case VT1708B_8CH:
1065         case VT1708B_4CH:
1066         case VT1708S:
1067         case VT1716S:
1068                 start_idx = 2;
1069                 end_idx = 4;
1070                 break;
1071         case VT1702:
1072                 start_idx = 1;
1073                 end_idx = 3;
1074                 break;
1075         case VT1718S:
1076                 start_idx = 1;
1077                 end_idx = 3;
1078                 break;
1079         case VT2002P:
1080         case VT1812:
1081         case VT1802:
1082                 start_idx = 0;
1083                 end_idx = 2;
1084                 break;
1085         default:
1086                 return 0;
1087         }
1088         /* check AA path's mute status */
1089         for (i = start_idx; i <= end_idx; i++) {
1090                 unsigned int con_list = snd_hda_codec_read(
1091                         codec, spec->aa_mix_nid, 0, AC_VERB_GET_CONNECT_LIST, i/4*4);
1092                 int shift = 8 * (i % 4);
1093                 hda_nid_t nid_pin = (con_list & (0xff << shift)) >> shift;
1094                 unsigned int defconf = snd_hda_codec_get_pincfg(codec, nid_pin);
1095                 if (get_defcfg_connect(defconf) == AC_JACK_PORT_COMPLEX) {
1096                         /* check mute status while the pin is connected */
1097                         int mute_l = snd_hda_codec_amp_read(codec, spec->aa_mix_nid, 0,
1098                                                             HDA_INPUT, i) >> 7;
1099                         int mute_r = snd_hda_codec_amp_read(codec, spec->aa_mix_nid, 1,
1100                                                             HDA_INPUT, i) >> 7;
1101                         if (!mute_l || !mute_r) {
1102                                 mute = 0;
1103                                 break;
1104                         }
1105                 }
1106         }
1107         return mute;
1108 }
1109
1110 /* enter/exit analog low-current mode */
1111 static void analog_low_current_mode(struct hda_codec *codec, int stream_idle)
1112 {
1113         struct via_spec *spec = codec->spec;
1114         static int saved_stream_idle = 1; /* saved stream idle status */
1115         int enable = is_aa_path_mute(codec);
1116         unsigned int verb = 0;
1117         unsigned int parm = 0;
1118
1119         if (stream_idle == -1)  /* stream status did not change */
1120                 enable = enable && saved_stream_idle;
1121         else {
1122                 enable = enable && stream_idle;
1123                 saved_stream_idle = stream_idle;
1124         }
1125
1126         /* decide low current mode's verb & parameter */
1127         switch (spec->codec_type) {
1128         case VT1708B_8CH:
1129         case VT1708B_4CH:
1130                 verb = 0xf70;
1131                 parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
1132                 break;
1133         case VT1708S:
1134         case VT1718S:
1135         case VT1716S:
1136                 verb = 0xf73;
1137                 parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
1138                 break;
1139         case VT1702:
1140                 verb = 0xf73;
1141                 parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
1142                 break;
1143         case VT2002P:
1144         case VT1812:
1145         case VT1802:
1146                 verb = 0xf93;
1147                 parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
1148                 break;
1149         default:
1150                 return;         /* other codecs are not supported */
1151         }
1152         /* send verb */
1153         snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
1154 }
1155
1156 /*
1157  * generic initialization of ADC, input mixers and output mixers
1158  */
1159 static const struct hda_verb vt1708_volume_init_verbs[] = {
1160         /*
1161          * Unmute ADC0-1 and set the default input to mic-in
1162          */
1163         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1164         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1165
1166
1167         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
1168          * mixer widget
1169          */
1170         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
1171         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1172         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1173         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
1174         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
1175         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
1176
1177         /*
1178          * Set up output mixers (0x19 - 0x1b)
1179          */
1180         /* set vol=0 to output mixers */
1181         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1182         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1183         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1184
1185         /* Setup default input MW0 to PW4 */
1186         {0x20, AC_VERB_SET_CONNECT_SEL, 0},
1187         /* PW9 Output enable */
1188         {0x25, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
1189         /* power down jack detect function */
1190         {0x1, 0xf81, 0x1},
1191         { }
1192 };
1193
1194 static void substream_set_idle(struct hda_codec *codec,
1195                                struct snd_pcm_substream *substream)
1196 {
1197         int idle = substream->pstr->substream_opened == 1
1198                 && substream->ref_count == 0;
1199         analog_low_current_mode(codec, idle);
1200 }
1201
1202 static int via_playback_pcm_open(struct hda_pcm_stream *hinfo,
1203                                  struct hda_codec *codec,
1204                                  struct snd_pcm_substream *substream)
1205 {
1206         struct via_spec *spec = codec->spec;
1207         substream_set_idle(codec, substream);
1208         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
1209                                              hinfo);
1210 }
1211
1212 static int via_playback_pcm_close(struct hda_pcm_stream *hinfo,
1213                                   struct hda_codec *codec,
1214                                   struct snd_pcm_substream *substream)
1215 {
1216         substream_set_idle(codec, substream);
1217         return 0;
1218 }
1219
1220 static int via_playback_hp_pcm_open(struct hda_pcm_stream *hinfo,
1221                                     struct hda_codec *codec,
1222                                     struct snd_pcm_substream *substream)
1223 {
1224         struct via_spec *spec = codec->spec;
1225         struct hda_multi_out *mout = &spec->multiout;
1226
1227         if (!mout->hp_nid || mout->hp_nid == mout->dac_nids[HDA_FRONT] ||
1228             !spec->hp_independent_mode)
1229                 return -EINVAL;
1230         substream_set_idle(codec, substream);
1231         return 0;
1232 }
1233
1234 static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
1235                                           struct hda_codec *codec,
1236                                           unsigned int stream_tag,
1237                                           unsigned int format,
1238                                           struct snd_pcm_substream *substream)
1239 {
1240         struct via_spec *spec = codec->spec;
1241         struct hda_multi_out *mout = &spec->multiout;
1242         const hda_nid_t *nids = mout->dac_nids;
1243         int chs = substream->runtime->channels;
1244         int i;
1245         struct hda_spdif_out *spdif =
1246                 snd_hda_spdif_out_of_nid(codec, spec->multiout.dig_out_nid);
1247
1248         mutex_lock(&codec->spdif_mutex);
1249         if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1250                 if (chs == 2 &&
1251                     snd_hda_is_supported_format(codec, mout->dig_out_nid,
1252                                                 format) &&
1253                     !(spdif->status & IEC958_AES0_NONAUDIO)) {
1254                         mout->dig_out_used = HDA_DIG_ANALOG_DUP;
1255                         /* turn off SPDIF once; otherwise the IEC958 bits won't
1256                          * be updated */
1257                         if (spdif->ctls & AC_DIG1_ENABLE)
1258                                 snd_hda_codec_write(codec, mout->dig_out_nid, 0,
1259                                                     AC_VERB_SET_DIGI_CONVERT_1,
1260                                                     spdif->ctls &
1261                                                         ~AC_DIG1_ENABLE & 0xff);
1262                         snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1263                                                    stream_tag, 0, format);
1264                         /* turn on again (if needed) */
1265                         if (spdif->ctls & AC_DIG1_ENABLE)
1266                                 snd_hda_codec_write(codec, mout->dig_out_nid, 0,
1267                                                     AC_VERB_SET_DIGI_CONVERT_1,
1268                                                     spdif->ctls & 0xff);
1269                 } else {
1270                         mout->dig_out_used = 0;
1271                         snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1272                                                    0, 0, 0);
1273                 }
1274         }
1275         mutex_unlock(&codec->spdif_mutex);
1276
1277         /* front */
1278         snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
1279                                    0, format);
1280
1281         if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]
1282             && !spec->hp_independent_mode)
1283                 /* headphone out will just decode front left/right (stereo) */
1284                 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
1285                                            0, format);
1286
1287         /* extra outputs copied from front */
1288         for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1289                 if (mout->extra_out_nid[i])
1290                         snd_hda_codec_setup_stream(codec,
1291                                                    mout->extra_out_nid[i],
1292                                                    stream_tag, 0, format);
1293
1294         /* surrounds */
1295         for (i = 1; i < mout->num_dacs; i++) {
1296                 if (chs >= (i + 1) * 2) /* independent out */
1297                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
1298                                                    i * 2, format);
1299                 else /* copy front */
1300                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
1301                                                    0, format);
1302         }
1303         vt1708_start_hp_work(spec);
1304         return 0;
1305 }
1306
1307 static int via_playback_hp_pcm_prepare(struct hda_pcm_stream *hinfo,
1308                                        struct hda_codec *codec,
1309                                        unsigned int stream_tag,
1310                                        unsigned int format,
1311                                        struct snd_pcm_substream *substream)
1312 {
1313         struct via_spec *spec = codec->spec;
1314         struct hda_multi_out *mout = &spec->multiout;
1315
1316         snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
1317         vt1708_start_hp_work(spec);
1318         return 0;
1319 }
1320
1321 static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
1322                                     struct hda_codec *codec,
1323                                     struct snd_pcm_substream *substream)
1324 {
1325         struct via_spec *spec = codec->spec;
1326         struct hda_multi_out *mout = &spec->multiout;
1327         const hda_nid_t *nids = mout->dac_nids;
1328         int i;
1329
1330         for (i = 0; i < mout->num_dacs; i++)
1331                 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
1332
1333         if (mout->hp_nid && !spec->hp_independent_mode)
1334                 snd_hda_codec_setup_stream(codec, mout->hp_nid,
1335                                            0, 0, 0);
1336
1337         for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1338                 if (mout->extra_out_nid[i])
1339                         snd_hda_codec_setup_stream(codec,
1340                                                    mout->extra_out_nid[i],
1341                                                    0, 0, 0);
1342         mutex_lock(&codec->spdif_mutex);
1343         if (mout->dig_out_nid &&
1344             mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
1345                 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1346                                            0, 0, 0);
1347                 mout->dig_out_used = 0;
1348         }
1349         mutex_unlock(&codec->spdif_mutex);
1350         vt1708_stop_hp_work(spec);
1351         return 0;
1352 }
1353
1354 static int via_playback_hp_pcm_cleanup(struct hda_pcm_stream *hinfo,
1355                                        struct hda_codec *codec,
1356                                        struct snd_pcm_substream *substream)
1357 {
1358         struct via_spec *spec = codec->spec;
1359         struct hda_multi_out *mout = &spec->multiout;
1360
1361         snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
1362         vt1708_stop_hp_work(spec);
1363         return 0;
1364 }
1365
1366 /*
1367  * Digital out
1368  */
1369 static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1370                                      struct hda_codec *codec,
1371                                      struct snd_pcm_substream *substream)
1372 {
1373         struct via_spec *spec = codec->spec;
1374         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1375 }
1376
1377 static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1378                                       struct hda_codec *codec,
1379                                       struct snd_pcm_substream *substream)
1380 {
1381         struct via_spec *spec = codec->spec;
1382         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1383 }
1384
1385 static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1386                                         struct hda_codec *codec,
1387                                         unsigned int stream_tag,
1388                                         unsigned int format,
1389                                         struct snd_pcm_substream *substream)
1390 {
1391         struct via_spec *spec = codec->spec;
1392         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1393                                              stream_tag, format, substream);
1394 }
1395
1396 static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1397                                         struct hda_codec *codec,
1398                                         struct snd_pcm_substream *substream)
1399 {
1400         struct via_spec *spec = codec->spec;
1401         snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
1402         return 0;
1403 }
1404
1405 /*
1406  * Analog capture
1407  */
1408 static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1409                                    struct hda_codec *codec,
1410                                    unsigned int stream_tag,
1411                                    unsigned int format,
1412                                    struct snd_pcm_substream *substream)
1413 {
1414         struct via_spec *spec = codec->spec;
1415
1416         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1417                                    stream_tag, 0, format);
1418         return 0;
1419 }
1420
1421 static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1422                                    struct hda_codec *codec,
1423                                    struct snd_pcm_substream *substream)
1424 {
1425         struct via_spec *spec = codec->spec;
1426         snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
1427         return 0;
1428 }
1429
1430 static const struct hda_pcm_stream via_pcm_analog_playback = {
1431         .substreams = 1,
1432         .channels_min = 2,
1433         .channels_max = 8,
1434         /* NID is set in via_build_pcms */
1435         .ops = {
1436                 .open = via_playback_pcm_open,
1437                 .close = via_playback_pcm_close,
1438                 .prepare = via_playback_multi_pcm_prepare,
1439                 .cleanup = via_playback_multi_pcm_cleanup
1440         },
1441 };
1442
1443 static const struct hda_pcm_stream via_pcm_hp_playback = {
1444         .substreams = 1,
1445         .channels_min = 2,
1446         .channels_max = 2,
1447         /* NID is set in via_build_pcms */
1448         .ops = {
1449                 .open = via_playback_hp_pcm_open,
1450                 .close = via_playback_pcm_close,
1451                 .prepare = via_playback_hp_pcm_prepare,
1452                 .cleanup = via_playback_hp_pcm_cleanup
1453         },
1454 };
1455
1456 static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
1457         .substreams = 1,
1458         .channels_min = 2,
1459         .channels_max = 8,
1460         /* NID is set in via_build_pcms */
1461         /* We got noisy outputs on the right channel on VT1708 when
1462          * 24bit samples are used.  Until any workaround is found,
1463          * disable the 24bit format, so far.
1464          */
1465         .formats = SNDRV_PCM_FMTBIT_S16_LE,
1466         .ops = {
1467                 .open = via_playback_pcm_open,
1468                 .close = via_playback_pcm_close,
1469                 .prepare = via_playback_multi_pcm_prepare,
1470                 .cleanup = via_playback_multi_pcm_cleanup
1471         },
1472 };
1473
1474 static const struct hda_pcm_stream via_pcm_analog_capture = {
1475         .substreams = 1, /* will be changed in via_build_pcms() */
1476         .channels_min = 2,
1477         .channels_max = 2,
1478         /* NID is set in via_build_pcms */
1479         .ops = {
1480                 .open = via_playback_pcm_open,
1481                 .close = via_playback_pcm_close,
1482                 .prepare = via_capture_pcm_prepare,
1483                 .cleanup = via_capture_pcm_cleanup
1484         },
1485 };
1486
1487 static const struct hda_pcm_stream via_pcm_digital_playback = {
1488         .substreams = 1,
1489         .channels_min = 2,
1490         .channels_max = 2,
1491         /* NID is set in via_build_pcms */
1492         .ops = {
1493                 .open = via_dig_playback_pcm_open,
1494                 .close = via_dig_playback_pcm_close,
1495                 .prepare = via_dig_playback_pcm_prepare,
1496                 .cleanup = via_dig_playback_pcm_cleanup
1497         },
1498 };
1499
1500 static const struct hda_pcm_stream via_pcm_digital_capture = {
1501         .substreams = 1,
1502         .channels_min = 2,
1503         .channels_max = 2,
1504 };
1505
1506 static int via_build_controls(struct hda_codec *codec)
1507 {
1508         struct via_spec *spec = codec->spec;
1509         struct snd_kcontrol *kctl;
1510         const struct snd_kcontrol_new *knew;
1511         int err, i;
1512
1513         if (spec->set_widgets_power_state)
1514                 if (!via_clone_control(spec, &via_pin_power_ctl_enum))
1515                         return -ENOMEM;
1516
1517         for (i = 0; i < spec->num_mixers; i++) {
1518                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1519                 if (err < 0)
1520                         return err;
1521         }
1522
1523         if (spec->multiout.dig_out_nid) {
1524                 err = snd_hda_create_spdif_out_ctls(codec,
1525                                                     spec->multiout.dig_out_nid,
1526                                                     spec->multiout.dig_out_nid);
1527                 if (err < 0)
1528                         return err;
1529                 err = snd_hda_create_spdif_share_sw(codec,
1530                                                     &spec->multiout);
1531                 if (err < 0)
1532                         return err;
1533                 spec->multiout.share_spdif = 1;
1534         }
1535         if (spec->dig_in_nid) {
1536                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1537                 if (err < 0)
1538                         return err;
1539         }
1540
1541         /* assign Capture Source enums to NID */
1542         kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1543         for (i = 0; kctl && i < kctl->count; i++) {
1544                 err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
1545                 if (err < 0)
1546                         return err;
1547         }
1548
1549         /* other nid->control mapping */
1550         for (i = 0; i < spec->num_mixers; i++) {
1551                 for (knew = spec->mixers[i]; knew->name; knew++) {
1552                         if (knew->iface != NID_MAPPING)
1553                                 continue;
1554                         kctl = snd_hda_find_mixer_ctl(codec, knew->name);
1555                         if (kctl == NULL)
1556                                 continue;
1557                         err = snd_hda_add_nid(codec, kctl, 0,
1558                                               knew->subdevice);
1559                 }
1560         }
1561
1562         /* init power states */
1563         set_widgets_power_state(codec);
1564         analog_low_current_mode(codec, 1);
1565
1566         via_free_kctls(codec); /* no longer needed */
1567         return 0;
1568 }
1569
1570 static int via_build_pcms(struct hda_codec *codec)
1571 {
1572         struct via_spec *spec = codec->spec;
1573         struct hda_pcm *info = spec->pcm_rec;
1574
1575         codec->num_pcms = 1;
1576         codec->pcm_info = info;
1577
1578         snprintf(spec->stream_name_analog, sizeof(spec->stream_name_analog),
1579                  "%s Analog", codec->chip_name);
1580         info->name = spec->stream_name_analog;
1581
1582         if (!spec->stream_analog_playback)
1583                 spec->stream_analog_playback = &via_pcm_analog_playback;
1584         info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1585                 *spec->stream_analog_playback;
1586         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1587                 spec->multiout.dac_nids[0];
1588         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1589                 spec->multiout.max_channels;
1590
1591         if (!spec->stream_analog_capture)
1592                 spec->stream_analog_capture = &via_pcm_analog_capture;
1593         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1594                 *spec->stream_analog_capture;
1595         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
1596         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
1597                 spec->num_adc_nids;
1598
1599         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1600                 codec->num_pcms++;
1601                 info++;
1602                 snprintf(spec->stream_name_digital,
1603                          sizeof(spec->stream_name_digital),
1604                          "%s Digital", codec->chip_name);
1605                 info->name = spec->stream_name_digital;
1606                 info->pcm_type = HDA_PCM_TYPE_SPDIF;
1607                 if (spec->multiout.dig_out_nid) {
1608                         if (!spec->stream_digital_playback)
1609                                 spec->stream_digital_playback =
1610                                         &via_pcm_digital_playback;
1611                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1612                                 *spec->stream_digital_playback;
1613                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1614                                 spec->multiout.dig_out_nid;
1615                 }
1616                 if (spec->dig_in_nid) {
1617                         if (!spec->stream_digital_capture)
1618                                 spec->stream_digital_capture =
1619                                         &via_pcm_digital_capture;
1620                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1621                                 *spec->stream_digital_capture;
1622                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1623                                 spec->dig_in_nid;
1624                 }
1625         }
1626
1627         if (spec->multiout.hp_nid) {
1628                 codec->num_pcms++;
1629                 info++;
1630                 snprintf(spec->stream_name_hp, sizeof(spec->stream_name_hp),
1631                          "%s HP", codec->chip_name);
1632                 info->name = spec->stream_name_hp;
1633                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = via_pcm_hp_playback;
1634                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1635                         spec->multiout.hp_nid;
1636         }
1637         return 0;
1638 }
1639
1640 static void via_free(struct hda_codec *codec)
1641 {
1642         struct via_spec *spec = codec->spec;
1643
1644         if (!spec)
1645                 return;
1646
1647         via_free_kctls(codec);
1648         vt1708_stop_hp_work(spec);
1649         kfree(codec->spec);
1650 }
1651
1652 /* mute/unmute outputs */
1653 static void toggle_output_mutes(struct hda_codec *codec, int num_pins,
1654                                 hda_nid_t *pins, bool mute)
1655 {
1656         int i;
1657         for (i = 0; i < num_pins; i++)
1658                 snd_hda_codec_write(codec, pins[i], 0,
1659                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1660                                     mute ? 0 : PIN_OUT);
1661 }
1662
1663 /* mute internal speaker if HP is plugged */
1664 static void via_hp_automute(struct hda_codec *codec)
1665 {
1666         unsigned int present = 0;
1667         struct via_spec *spec = codec->spec;
1668
1669         present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
1670
1671         if (!spec->hp_independent_mode)
1672                 toggle_output_mutes(codec, spec->autocfg.line_outs,
1673                                     spec->autocfg.line_out_pins,
1674                                     present);
1675 }
1676
1677 /* mute mono out if HP or Line out is plugged */
1678 static void via_mono_automute(struct hda_codec *codec)
1679 {
1680         unsigned int hp_present, lineout_present;
1681         struct via_spec *spec = codec->spec;
1682
1683         if (spec->codec_type != VT1716S)
1684                 return;
1685
1686         lineout_present = snd_hda_jack_detect(codec,
1687                                               spec->autocfg.line_out_pins[0]);
1688
1689         /* Mute Mono Out if Line Out is plugged */
1690         if (lineout_present) {
1691                 snd_hda_codec_write(codec, 0x2A, 0,
1692                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1693                                     lineout_present ? 0 : PIN_OUT);
1694                 return;
1695         }
1696
1697         hp_present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
1698
1699         if (!spec->hp_independent_mode)
1700                 snd_hda_codec_write(codec, 0x2A, 0,
1701                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1702                                     hp_present ? 0 : PIN_OUT);
1703 }
1704
1705 static void via_gpio_control(struct hda_codec *codec)
1706 {
1707         unsigned int gpio_data;
1708         unsigned int vol_counter;
1709         unsigned int vol;
1710         unsigned int master_vol;
1711
1712         struct via_spec *spec = codec->spec;
1713
1714         gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
1715                                        AC_VERB_GET_GPIO_DATA, 0) & 0x03;
1716
1717         vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
1718                                           0xF84, 0) & 0x3F0000) >> 16;
1719
1720         vol = vol_counter & 0x1F;
1721         master_vol = snd_hda_codec_read(codec, 0x1A, 0,
1722                                         AC_VERB_GET_AMP_GAIN_MUTE,
1723                                         AC_AMP_GET_INPUT);
1724
1725         if (gpio_data == 0x02) {
1726                 /* unmute line out */
1727                 snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
1728                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1729                                     PIN_OUT);
1730                 if (vol_counter & 0x20) {
1731                         /* decrease volume */
1732                         if (vol > master_vol)
1733                                 vol = master_vol;
1734                         snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
1735                                                  0, HDA_AMP_VOLMASK,
1736                                                  master_vol-vol);
1737                 } else {
1738                         /* increase volume */
1739                         snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
1740                                          HDA_AMP_VOLMASK,
1741                                          ((master_vol+vol) > 0x2A) ? 0x2A :
1742                                           (master_vol+vol));
1743                 }
1744         } else if (!(gpio_data & 0x02)) {
1745                 /* mute line out */
1746                 snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
1747                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1748                                     0);
1749         }
1750 }
1751
1752 /* mute Internal-Speaker if HP is plugged */
1753 static void via_speaker_automute(struct hda_codec *codec)
1754 {
1755         unsigned int hp_present;
1756         struct via_spec *spec = codec->spec;
1757
1758         if (!VT2002P_COMPATIBLE(spec))
1759                 return;
1760
1761         hp_present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
1762
1763         if (!spec->hp_independent_mode)
1764                 toggle_output_mutes(codec, spec->autocfg.speaker_outs,
1765                                     spec->autocfg.speaker_pins,
1766                                     hp_present);
1767 }
1768
1769 /* mute line-out and internal speaker if HP is plugged */
1770 static void via_hp_bind_automute(struct hda_codec *codec)
1771 {
1772         int present;
1773         struct via_spec *spec = codec->spec;
1774
1775         if (!spec->autocfg.hp_pins[0] || !spec->autocfg.line_out_pins[0])
1776                 return;
1777
1778         present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
1779         if (!spec->hp_independent_mode)
1780                 toggle_output_mutes(codec, spec->autocfg.line_outs,
1781                                     spec->autocfg.line_out_pins,
1782                                     present);
1783
1784         if (!present)
1785                 present = snd_hda_jack_detect(codec,
1786                                               spec->autocfg.line_out_pins[0]);
1787
1788         /* Speakers */
1789         toggle_output_mutes(codec, spec->autocfg.speaker_outs,
1790                             spec->autocfg.speaker_pins,
1791                             present);
1792 }
1793
1794
1795 /* unsolicited event for jack sensing */
1796 static void via_unsol_event(struct hda_codec *codec,
1797                                   unsigned int res)
1798 {
1799         res >>= 26;
1800
1801         if (res & VIA_JACK_EVENT)
1802                 set_widgets_power_state(codec);
1803
1804         res &= ~VIA_JACK_EVENT;
1805
1806         if (res == VIA_HP_EVENT)
1807                 via_hp_automute(codec);
1808         else if (res == VIA_GPIO_EVENT)
1809                 via_gpio_control(codec);
1810         else if (res == VIA_MONO_EVENT)
1811                 via_mono_automute(codec);
1812         else if (res == VIA_SPEAKER_EVENT)
1813                 via_speaker_automute(codec);
1814         else if (res == VIA_BIND_HP_EVENT)
1815                 via_hp_bind_automute(codec);
1816 }
1817
1818 static int via_init(struct hda_codec *codec)
1819 {
1820         struct via_spec *spec = codec->spec;
1821         int i;
1822         for (i = 0; i < spec->num_iverbs; i++)
1823                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
1824
1825         /* Lydia Add for EAPD enable */
1826         if (!spec->dig_in_nid) { /* No Digital In connection */
1827                 if (spec->dig_in_pin) {
1828                         snd_hda_codec_write(codec, spec->dig_in_pin, 0,
1829                                             AC_VERB_SET_PIN_WIDGET_CONTROL,
1830                                             PIN_OUT);
1831                         snd_hda_codec_write(codec, spec->dig_in_pin, 0,
1832                                             AC_VERB_SET_EAPD_BTLENABLE, 0x02);
1833                 }
1834         } else /* enable SPDIF-input pin */
1835                 snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
1836                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
1837
1838         /* assign slave outs */
1839         if (spec->slave_dig_outs[0])
1840                 codec->slave_dig_outs = spec->slave_dig_outs;
1841
1842         return 0;
1843 }
1844
1845 #ifdef SND_HDA_NEEDS_RESUME
1846 static int via_suspend(struct hda_codec *codec, pm_message_t state)
1847 {
1848         struct via_spec *spec = codec->spec;
1849         vt1708_stop_hp_work(spec);
1850         return 0;
1851 }
1852 #endif
1853
1854 #ifdef CONFIG_SND_HDA_POWER_SAVE
1855 static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1856 {
1857         struct via_spec *spec = codec->spec;
1858         return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1859 }
1860 #endif
1861
1862 /*
1863  */
1864 static const struct hda_codec_ops via_patch_ops = {
1865         .build_controls = via_build_controls,
1866         .build_pcms = via_build_pcms,
1867         .init = via_init,
1868         .free = via_free,
1869 #ifdef SND_HDA_NEEDS_RESUME
1870         .suspend = via_suspend,
1871 #endif
1872 #ifdef CONFIG_SND_HDA_POWER_SAVE
1873         .check_power_status = via_check_power_status,
1874 #endif
1875 };
1876
1877 static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
1878 {
1879         struct via_spec *spec = codec->spec;
1880         int i;
1881
1882         for (i = 0; i < spec->multiout.num_dacs; i++) {
1883                 if (spec->multiout.dac_nids[i] == dac)
1884                         return false;
1885         }
1886         if (spec->multiout.hp_nid == dac)
1887                 return false;
1888         return true;
1889 }
1890
1891 static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
1892                               hda_nid_t target_dac, struct nid_path *path,
1893                               int depth, int wid_type)
1894 {
1895         hda_nid_t conn[8];
1896         int i, nums;
1897
1898         nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
1899         for (i = 0; i < nums; i++) {
1900                 if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
1901                         continue;
1902                 if (conn[i] == target_dac || is_empty_dac(codec, conn[i])) {
1903                         path->path[depth] = conn[i];
1904                         path->idx[depth] = i;
1905                         path->depth = ++depth;
1906                         return true;
1907                 }
1908         }
1909         if (depth > 4)
1910                 return false;
1911         for (i = 0; i < nums; i++) {
1912                 unsigned int type;
1913                 type = get_wcaps_type(get_wcaps(codec, conn[i]));
1914                 if (type == AC_WID_AUD_OUT ||
1915                     (wid_type != -1 && type != wid_type))
1916                         continue;
1917                 if (parse_output_path(codec, conn[i], target_dac,
1918                                       path, depth + 1, AC_WID_AUD_SEL)) {
1919                         path->path[depth] = conn[i];
1920                         path->idx[depth] = i;
1921                         return true;
1922                 }
1923         }
1924         return false;
1925 }
1926
1927 static int via_auto_fill_dac_nids(struct hda_codec *codec)
1928 {
1929         struct via_spec *spec = codec->spec;
1930         const struct auto_pin_cfg *cfg = &spec->autocfg;
1931         int i;
1932         hda_nid_t nid;
1933
1934         spec->multiout.dac_nids = spec->private_dac_nids;
1935         spec->multiout.num_dacs = cfg->line_outs;
1936         for (i = 0; i < cfg->line_outs; i++) {
1937                 nid = cfg->line_out_pins[i];
1938                 if (!nid)
1939                         continue;
1940                 if (parse_output_path(codec, nid, 0, &spec->out_path[i], 0, -1))
1941                         spec->private_dac_nids[i] =
1942                                 spec->out_path[i].path[spec->out_path[i].depth - 1];
1943         }
1944         return 0;
1945 }
1946
1947 static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
1948                           hda_nid_t pin, hda_nid_t dac, int chs)
1949 {
1950         struct via_spec *spec = codec->spec;
1951         char name[32];
1952         hda_nid_t nid;
1953         int err;
1954
1955         if (dac && query_amp_caps(codec, dac, HDA_OUTPUT) & AC_AMPCAP_NUM_STEPS)
1956                 nid = dac;
1957         else if (query_amp_caps(codec, pin, HDA_OUTPUT) & AC_AMPCAP_NUM_STEPS)
1958                 nid = pin;
1959         else
1960                 nid = 0;
1961         if (nid) {
1962                 sprintf(name, "%s Playback Volume", pfx);
1963                 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1964                               HDA_COMPOSE_AMP_VAL(dac, chs, 0, HDA_OUTPUT));
1965                 if (err < 0)
1966                         return err;
1967         }
1968
1969         if (dac && query_amp_caps(codec, dac, HDA_OUTPUT) & AC_AMPCAP_MUTE)
1970                 nid = dac;
1971         else if (query_amp_caps(codec, pin, HDA_OUTPUT) & AC_AMPCAP_MUTE)
1972                 nid = pin;
1973         else
1974                 nid = 0;
1975         if (nid) {
1976                 sprintf(name, "%s Playback Switch", pfx);
1977                 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1978                               HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1979                 if (err < 0)
1980                         return err;
1981         }
1982         return 0;
1983 }
1984
1985 static int get_connection_index(struct hda_codec *codec, hda_nid_t mux,
1986                                 hda_nid_t nid);
1987
1988 static void mangle_smart51(struct hda_codec *codec)
1989 {
1990         struct via_spec *spec = codec->spec;
1991         struct auto_pin_cfg *cfg = &spec->autocfg;
1992         int i;
1993
1994         for (i = 0; i < cfg->num_inputs; i++) {
1995                 if (!is_smart51_pins(codec, cfg->inputs[i].pin))
1996                         continue;
1997                 spec->can_smart51 = 1;
1998                 cfg->line_out_pins[cfg->line_outs++] = cfg->inputs[i].pin;
1999                 if (cfg->line_outs == 3)
2000                         break;
2001         }
2002 }
2003
2004 /* add playback controls from the parsed DAC table */
2005 static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
2006 {
2007         struct via_spec *spec = codec->spec;
2008         struct auto_pin_cfg *cfg = &spec->autocfg;
2009         static const char * const chname[4] = {
2010                 "Front", "Surround", "C/LFE", "Side"
2011         };
2012         int i, idx, err;
2013         int old_line_outs;
2014
2015         /* check smart51 */
2016         old_line_outs = cfg->line_outs;
2017         if (cfg->line_outs == 1)
2018                 mangle_smart51(codec);
2019
2020         for (i = 0; i < cfg->line_outs; i++) {
2021                 hda_nid_t pin, dac;
2022                 pin = cfg->line_out_pins[i];
2023                 dac = spec->multiout.dac_nids[i];
2024                 if (!pin || !dac)
2025                         continue;
2026                 if (i == AUTO_SEQ_CENLFE) {
2027                         err = create_ch_ctls(codec, "Center", pin, dac, 1);
2028                         if (err < 0)
2029                                 return err;
2030                         err = create_ch_ctls(codec, "LFE", pin, dac, 2);
2031                         if (err < 0)
2032                                 return err;
2033                 } else {
2034                         err = create_ch_ctls(codec, chname[i], pin, dac, 3);
2035                         if (err < 0)
2036                                 return err;
2037                 }
2038         }
2039
2040         idx = get_connection_index(codec, spec->aa_mix_nid,
2041                                    spec->multiout.dac_nids[0]);
2042         if (idx >= 0) {
2043                 /* add control to mixer */
2044                 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2045                                       "PCM Playback Volume",
2046                                       HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
2047                                                           idx, HDA_INPUT));
2048                 if (err < 0)
2049                         return err;
2050                 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2051                                       "PCM Playback Switch",
2052                                       HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
2053                                                           idx, HDA_INPUT));
2054                 if (err < 0)
2055                         return err;
2056         }
2057
2058         cfg->line_outs = old_line_outs;
2059
2060         return 0;
2061 }
2062
2063 static void create_hp_imux(struct via_spec *spec)
2064 {
2065         int i;
2066         struct hda_input_mux *imux = &spec->private_imux[1];
2067         static const char * const texts[] = { "OFF", "ON", NULL};
2068
2069         /* for hp mode select */
2070         for (i = 0; texts[i]; i++)
2071                 snd_hda_add_imux_item(imux, texts[i], i, NULL);
2072
2073         spec->hp_mux = &spec->private_imux[1];
2074 }
2075
2076 static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
2077 {
2078         struct via_spec *spec = codec->spec;
2079         hda_nid_t dac = 0;
2080         int err;
2081
2082         if (!pin)
2083                 return 0;
2084
2085         if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
2086                                &spec->hp_dep_path, 0, -1))
2087                 return 0;
2088         if (parse_output_path(codec, pin, 0, &spec->hp_path, 0, -1)) {
2089                 dac = spec->hp_path.path[spec->hp_path.depth - 1];
2090                 spec->multiout.hp_nid = dac;
2091                 spec->hp_independent_mode_index =
2092                         spec->hp_path.idx[spec->hp_path.depth - 1];
2093                 create_hp_imux(spec);
2094         }
2095
2096         err = create_ch_ctls(codec, "Headphone", pin, dac, 3);
2097         if (err < 0)
2098                 return err;
2099
2100         return 0;
2101 }
2102
2103 static int get_connection_index(struct hda_codec *codec, hda_nid_t mux,
2104                                 hda_nid_t nid)
2105 {
2106         hda_nid_t conn[HDA_MAX_NUM_INPUTS];
2107         int i, nums;
2108
2109         nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
2110         for (i = 0; i < nums; i++)
2111                 if (conn[i] == nid)
2112                         return i;
2113         return -1;
2114 }
2115
2116 /* look for ADCs */
2117 static int via_fill_adcs(struct hda_codec *codec)
2118 {
2119         struct via_spec *spec = codec->spec;
2120         hda_nid_t nid = codec->start_nid;
2121         int i;
2122
2123         for (i = 0; i < codec->num_nodes; i++, nid++) {
2124                 unsigned int wcaps = get_wcaps(codec, nid);
2125                 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2126                         continue;
2127                 if (wcaps & AC_WCAP_DIGITAL)
2128                         continue;
2129                 if (!(wcaps & AC_WCAP_CONN_LIST))
2130                         continue;
2131                 if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
2132                         return -ENOMEM;
2133                 spec->adc_nids[spec->num_adc_nids++] = nid;
2134         }
2135         return 0;
2136 }
2137
2138 static int get_mux_nids(struct hda_codec *codec);
2139
2140 static const struct snd_kcontrol_new via_input_src_ctl = {
2141         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2142         /* The multiple "Capture Source" controls confuse alsamixer
2143          * So call somewhat different..
2144          */
2145         /* .name = "Capture Source", */
2146         .name = "Input Source",
2147         .info = via_mux_enum_info,
2148         .get = via_mux_enum_get,
2149         .put = via_mux_enum_put,
2150 };
2151
2152 /* create playback/capture controls for input pins */
2153 static int via_auto_create_analog_input_ctls(struct hda_codec *codec,
2154                                              const struct auto_pin_cfg *cfg)
2155 {
2156         struct via_spec *spec = codec->spec;
2157         struct hda_input_mux *imux = &spec->private_imux[0];
2158         int i, err, idx, idx2, type, type_idx = 0;
2159         hda_nid_t cap_nid;
2160         hda_nid_t pin_idxs[8];
2161         int num_idxs;
2162
2163         err = via_fill_adcs(codec);
2164         if (err < 0)
2165                 return err;
2166         err = get_mux_nids(codec);
2167         if (err < 0)
2168                 return err;
2169         cap_nid = spec->mux_nids[0];
2170
2171         num_idxs = snd_hda_get_connections(codec, cap_nid, pin_idxs,
2172                                            ARRAY_SIZE(pin_idxs));
2173         if (num_idxs <= 0)
2174                 return 0;
2175
2176         /* for internal loopback recording select */
2177         for (idx = 0; idx < num_idxs; idx++) {
2178                 if (pin_idxs[idx] == spec->aa_mix_nid) {
2179                         snd_hda_add_imux_item(imux, "Stereo Mixer", idx, NULL);
2180                         break;
2181                 }
2182         }
2183
2184         for (i = 0; i < cfg->num_inputs; i++) {
2185                 const char *label;
2186                 type = cfg->inputs[i].type;
2187                 for (idx = 0; idx < num_idxs; idx++)
2188                         if (pin_idxs[idx] == cfg->inputs[i].pin)
2189                                 break;
2190                 if (idx >= num_idxs)
2191                         continue;
2192                 if (i > 0 && type == cfg->inputs[i - 1].type)
2193                         type_idx++;
2194                 else
2195                         type_idx = 0;
2196                 label = hda_get_autocfg_input_label(codec, cfg, i);
2197                 idx2 = get_connection_index(codec, spec->aa_mix_nid,
2198                                             pin_idxs[idx]);
2199                 if (idx2 >= 0)
2200                         err = via_new_analog_input(spec, label, type_idx,
2201                                                    idx2, spec->aa_mix_nid);
2202                 if (err < 0)
2203                         return err;
2204                 snd_hda_add_imux_item(imux, label, idx, NULL);
2205         }
2206
2207         /* create capture mixer elements */
2208         for (i = 0; i < spec->num_adc_nids; i++) {
2209                 hda_nid_t adc = spec->adc_nids[i];
2210                 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
2211                                         "Capture Volume", i,
2212                                         HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2213                                                             HDA_INPUT));
2214                 if (err < 0)
2215                         return err;
2216                 err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2217                                         "Capture Switch", i,
2218                                         HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2219                                                             HDA_INPUT));
2220                 if (err < 0)
2221                         return err;
2222         }
2223
2224         /* input-source control */
2225         for (i = 0; i < spec->num_adc_nids; i++)
2226                 if (!spec->mux_nids[i])
2227                         break;
2228         if (i) {
2229                 struct snd_kcontrol_new *knew;
2230                 knew = via_clone_control(spec, &via_input_src_ctl);
2231                 if (!knew)
2232                         return -ENOMEM;
2233                 knew->count = i;
2234         }
2235
2236         /* mic-boosts */
2237         for (i = 0; i < cfg->num_inputs; i++) {
2238                 hda_nid_t pin = cfg->inputs[i].pin;
2239                 unsigned int caps;
2240                 const char *label;
2241                 char name[32];
2242
2243                 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2244                         continue;
2245                 caps = query_amp_caps(codec, pin, HDA_INPUT);
2246                 if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
2247                         continue;
2248                 label = hda_get_autocfg_input_label(codec, cfg, i);
2249                 snprintf(name, sizeof(name), "%s Boost Capture Volume", label);
2250                 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2251                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
2252                 if (err < 0)
2253                         return err;
2254         }
2255
2256         return 0;
2257 }
2258
2259 #ifdef CONFIG_SND_HDA_POWER_SAVE
2260 static const struct hda_amp_list vt1708_loopbacks[] = {
2261         { 0x17, HDA_INPUT, 1 },
2262         { 0x17, HDA_INPUT, 2 },
2263         { 0x17, HDA_INPUT, 3 },
2264         { 0x17, HDA_INPUT, 4 },
2265         { } /* end */
2266 };
2267 #endif
2268
2269 static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
2270 {
2271         unsigned int def_conf;
2272         unsigned char seqassoc;
2273
2274         def_conf = snd_hda_codec_get_pincfg(codec, nid);
2275         seqassoc = (unsigned char) get_defcfg_association(def_conf);
2276         seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
2277         if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
2278             && (seqassoc == 0xf0 || seqassoc == 0xff)) {
2279                 def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
2280                 snd_hda_codec_set_pincfg(codec, nid, def_conf);
2281         }
2282
2283         return;
2284 }
2285
2286 static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
2287                                      struct snd_ctl_elem_value *ucontrol)
2288 {
2289         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2290         struct via_spec *spec = codec->spec;
2291
2292         if (spec->codec_type != VT1708)
2293                 return 0;
2294         spec->vt1708_jack_detect =
2295                 !((snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8) & 0x1);
2296         ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
2297         return 0;
2298 }
2299
2300 static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
2301                                      struct snd_ctl_elem_value *ucontrol)
2302 {
2303         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2304         struct via_spec *spec = codec->spec;
2305         int change;
2306
2307         if (spec->codec_type != VT1708)
2308                 return 0;
2309         spec->vt1708_jack_detect = ucontrol->value.integer.value[0];
2310         change = (0x1 & (snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8))
2311                 == !spec->vt1708_jack_detect;
2312         if (spec->vt1708_jack_detect) {
2313                 mute_aa_path(codec, 1);
2314                 notify_aa_path_ctls(codec);
2315         }
2316         return change;
2317 }
2318
2319 static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
2320         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2321         .name = "Jack Detect",
2322         .count = 1,
2323         .info = snd_ctl_boolean_mono_info,
2324         .get = vt1708_jack_detect_get,
2325         .put = vt1708_jack_detect_put,
2326 };
2327
2328 static int vt1708_parse_auto_config(struct hda_codec *codec)
2329 {
2330         struct via_spec *spec = codec->spec;
2331         int err;
2332
2333         /* Add HP and CD pin config connect bit re-config action */
2334         vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
2335         vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
2336
2337         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2338         if (err < 0)
2339                 return err;
2340         err = via_auto_fill_dac_nids(codec);
2341         if (err < 0)
2342                 return err;
2343         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2344                 return -EINVAL;
2345
2346         err = via_auto_create_multi_out_ctls(codec);
2347         if (err < 0)
2348                 return err;
2349         err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
2350         if (err < 0)
2351                 return err;
2352         err = via_auto_create_analog_input_ctls(codec, &spec->autocfg);
2353         if (err < 0)
2354                 return err;
2355         /* add jack detect on/off control */
2356         if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
2357                 return -ENOMEM;
2358
2359         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2360
2361         if (spec->autocfg.dig_outs)
2362                 spec->multiout.dig_out_nid = VT1708_DIGOUT_NID;
2363         spec->dig_in_pin = VT1708_DIGIN_PIN;
2364         if (spec->autocfg.dig_in_pin)
2365                 spec->dig_in_nid = VT1708_DIGIN_NID;
2366
2367         if (spec->kctls.list)
2368                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
2369
2370         spec->init_verbs[spec->num_iverbs++] = vt1708_volume_init_verbs;
2371
2372         spec->input_mux = &spec->private_imux[0];
2373
2374         if (spec->hp_mux)
2375                 via_hp_build(codec);
2376
2377         err = via_smart51_build(codec);
2378         if (err < 0)
2379                 return err;
2380
2381         return 1;
2382 }
2383
2384 /* init callback for auto-configuration model -- overriding the default init */
2385 static int via_auto_init(struct hda_codec *codec)
2386 {
2387         struct via_spec *spec = codec->spec;
2388
2389         via_init(codec);
2390         via_auto_init_multi_out(codec);
2391         via_auto_init_hp_out(codec);
2392         via_auto_init_analog_input(codec);
2393
2394         if (VT2002P_COMPATIBLE(spec)) {
2395                 via_hp_bind_automute(codec);
2396         } else {
2397                 via_hp_automute(codec);
2398                 via_speaker_automute(codec);
2399         }
2400
2401         return 0;
2402 }
2403
2404 static void vt1708_update_hp_jack_state(struct work_struct *work)
2405 {
2406         struct via_spec *spec = container_of(work, struct via_spec,
2407                                              vt1708_hp_work.work);
2408         if (spec->codec_type != VT1708)
2409                 return;
2410         /* if jack state toggled */
2411         if (spec->vt1708_hp_present
2412             != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
2413                 spec->vt1708_hp_present ^= 1;
2414                 via_hp_automute(spec->codec);
2415         }
2416         vt1708_start_hp_work(spec);
2417 }
2418
2419 static int get_mux_nids(struct hda_codec *codec)
2420 {
2421         struct via_spec *spec = codec->spec;
2422         hda_nid_t nid, conn[8];
2423         unsigned int type;
2424         int i, n;
2425
2426         for (i = 0; i < spec->num_adc_nids; i++) {
2427                 nid = spec->adc_nids[i];
2428                 while (nid) {
2429                         type = get_wcaps_type(get_wcaps(codec, nid));
2430                         if (type == AC_WID_PIN)
2431                                 break;
2432                         n = snd_hda_get_connections(codec, nid, conn,
2433                                                     ARRAY_SIZE(conn));
2434                         if (n <= 0)
2435                                 break;
2436                         if (n > 1) {
2437                                 spec->mux_nids[i] = nid;
2438                                 break;
2439                         }
2440                         nid = conn[0];
2441                 }
2442         }
2443         return 0;
2444 }
2445
2446 static int patch_vt1708(struct hda_codec *codec)
2447 {
2448         struct via_spec *spec;
2449         int err;
2450
2451         /* create a codec specific record */
2452         spec = via_new_spec(codec);
2453         if (spec == NULL)
2454                 return -ENOMEM;
2455
2456         spec->aa_mix_nid = 0x17;
2457
2458         /* automatic parse from the BIOS config */
2459         err = vt1708_parse_auto_config(codec);
2460         if (err < 0) {
2461                 via_free(codec);
2462                 return err;
2463         }
2464
2465         /* disable 32bit format on VT1708 */
2466         if (codec->vendor_id == 0x11061708)
2467                 spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
2468
2469         codec->patch_ops = via_patch_ops;
2470
2471         codec->patch_ops.init = via_auto_init;
2472 #ifdef CONFIG_SND_HDA_POWER_SAVE
2473         spec->loopback.amplist = vt1708_loopbacks;
2474 #endif
2475         INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
2476         return 0;
2477 }
2478
2479 static const struct hda_verb vt1709_uniwill_init_verbs[] = {
2480         {0x20, AC_VERB_SET_UNSOLICITED_ENABLE,
2481          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
2482         { }
2483 };
2484
2485 /*
2486  * generic initialization of ADC, input mixers and output mixers
2487  */
2488 static const struct hda_verb vt1709_10ch_volume_init_verbs[] = {
2489         /*
2490          * Unmute ADC0-2 and set the default input to mic-in
2491          */
2492         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2493         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2494         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2495
2496
2497         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2498          * mixer widget
2499          */
2500         /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2501         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2502         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2503         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2504         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2505         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2506
2507         /*
2508          * Set up output selector (0x1a, 0x1b, 0x29)
2509          */
2510         /* set vol=0 to output mixers */
2511         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2512         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2513         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2514
2515         /*
2516          *  Unmute PW3 and PW4
2517          */
2518         {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2519         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2520
2521         /* Set input of PW4 as MW0 */
2522         {0x20, AC_VERB_SET_CONNECT_SEL, 0},
2523         /* PW9 Output enable */
2524         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2525         { }
2526 };
2527
2528 static int vt1709_parse_auto_config(struct hda_codec *codec)
2529 {
2530         struct via_spec *spec = codec->spec;
2531         int err;
2532
2533         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2534         if (err < 0)
2535                 return err;
2536         err = via_auto_fill_dac_nids(codec);
2537         if (err < 0)
2538                 return err;
2539         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2540                 return -EINVAL;
2541
2542         err = via_auto_create_multi_out_ctls(codec);
2543         if (err < 0)
2544                 return err;
2545         err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
2546         if (err < 0)
2547                 return err;
2548         err = via_auto_create_analog_input_ctls(codec, &spec->autocfg);
2549         if (err < 0)
2550                 return err;
2551
2552         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2553
2554         if (spec->autocfg.dig_outs)
2555                 spec->multiout.dig_out_nid = VT1709_DIGOUT_NID;
2556         spec->dig_in_pin = VT1709_DIGIN_PIN;
2557         if (spec->autocfg.dig_in_pin)
2558                 spec->dig_in_nid = VT1709_DIGIN_NID;
2559
2560         if (spec->kctls.list)
2561                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
2562
2563         spec->input_mux = &spec->private_imux[0];
2564
2565         if (spec->hp_mux)
2566                 via_hp_build(codec);
2567
2568         err = via_smart51_build(codec);
2569         if (err < 0)
2570                 return err;
2571
2572         return 1;
2573 }
2574
2575 #ifdef CONFIG_SND_HDA_POWER_SAVE
2576 static const struct hda_amp_list vt1709_loopbacks[] = {
2577         { 0x18, HDA_INPUT, 1 },
2578         { 0x18, HDA_INPUT, 2 },
2579         { 0x18, HDA_INPUT, 3 },
2580         { 0x18, HDA_INPUT, 4 },
2581         { } /* end */
2582 };
2583 #endif
2584
2585 static int patch_vt1709_10ch(struct hda_codec *codec)
2586 {
2587         struct via_spec *spec;
2588         int err;
2589
2590         /* create a codec specific record */
2591         spec = via_new_spec(codec);
2592         if (spec == NULL)
2593                 return -ENOMEM;
2594
2595         spec->aa_mix_nid = 0x18;
2596
2597         err = vt1709_parse_auto_config(codec);
2598         if (err < 0) {
2599                 via_free(codec);
2600                 return err;
2601         }
2602
2603         spec->init_verbs[spec->num_iverbs++] = vt1709_10ch_volume_init_verbs;
2604         spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
2605
2606         codec->patch_ops = via_patch_ops;
2607
2608         codec->patch_ops.init = via_auto_init;
2609         codec->patch_ops.unsol_event = via_unsol_event;
2610 #ifdef CONFIG_SND_HDA_POWER_SAVE
2611         spec->loopback.amplist = vt1709_loopbacks;
2612 #endif
2613
2614         return 0;
2615 }
2616 /*
2617  * generic initialization of ADC, input mixers and output mixers
2618  */
2619 static const struct hda_verb vt1709_6ch_volume_init_verbs[] = {
2620         /*
2621          * Unmute ADC0-2 and set the default input to mic-in
2622          */
2623         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2624         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2625         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2626
2627
2628         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2629          * mixer widget
2630          */
2631         /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2632         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2633         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2634         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2635         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2636         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2637
2638         /*
2639          * Set up output selector (0x1a, 0x1b, 0x29)
2640          */
2641         /* set vol=0 to output mixers */
2642         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2643         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2644         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2645
2646         /*
2647          *  Unmute PW3 and PW4
2648          */
2649         {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2650         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2651
2652         /* Set input of PW4 as MW0 */
2653         {0x20, AC_VERB_SET_CONNECT_SEL, 0},
2654         /* PW9 Output enable */
2655         {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2656         { }
2657 };
2658
2659 static int patch_vt1709_6ch(struct hda_codec *codec)
2660 {
2661         struct via_spec *spec;
2662         int err;
2663
2664         /* create a codec specific record */
2665         spec = via_new_spec(codec);
2666         if (spec == NULL)
2667                 return -ENOMEM;
2668
2669         spec->aa_mix_nid = 0x18;
2670
2671         err = vt1709_parse_auto_config(codec);
2672         if (err < 0) {
2673                 via_free(codec);
2674                 return err;
2675         }
2676
2677         spec->init_verbs[spec->num_iverbs++] = vt1709_6ch_volume_init_verbs;
2678         spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
2679
2680         codec->patch_ops = via_patch_ops;
2681
2682         codec->patch_ops.init = via_auto_init;
2683         codec->patch_ops.unsol_event = via_unsol_event;
2684 #ifdef CONFIG_SND_HDA_POWER_SAVE
2685         spec->loopback.amplist = vt1709_loopbacks;
2686 #endif
2687         return 0;
2688 }
2689
2690 /*
2691  * generic initialization of ADC, input mixers and output mixers
2692  */
2693 static const struct hda_verb vt1708B_8ch_volume_init_verbs[] = {
2694         /*
2695          * Unmute ADC0-1 and set the default input to mic-in
2696          */
2697         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2698         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2699
2700
2701         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2702          * mixer widget
2703          */
2704         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2705         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2706         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2707         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2708         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2709         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2710
2711         /*
2712          * Set up output mixers
2713          */
2714         /* set vol=0 to output mixers */
2715         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2716         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2717         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2718
2719         /* Setup default input to PW4 */
2720         {0x1d, AC_VERB_SET_CONNECT_SEL, 0},
2721         /* PW9 Output enable */
2722         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2723         /* PW10 Input enable */
2724         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2725         { }
2726 };
2727
2728 static const struct hda_verb vt1708B_4ch_volume_init_verbs[] = {
2729         /*
2730          * Unmute ADC0-1 and set the default input to mic-in
2731          */
2732         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2733         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2734
2735
2736         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2737          * mixer widget
2738          */
2739         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2740         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2741         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2742         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2743         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2744         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2745
2746         /*
2747          * Set up output mixers
2748          */
2749         /* set vol=0 to output mixers */
2750         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2751         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2752         {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2753
2754         /* Setup default input of PW4 to MW0 */
2755         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
2756         /* PW9 Output enable */
2757         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2758         /* PW10 Input enable */
2759         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2760         { }
2761 };
2762
2763 static const struct hda_verb vt1708B_uniwill_init_verbs[] = {
2764         {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
2765          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
2766         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2767         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2768         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2769         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2770         {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2771         {0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2772         {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2773         { }
2774 };
2775
2776 static int vt1708B_parse_auto_config(struct hda_codec *codec)
2777 {
2778         struct via_spec *spec = codec->spec;
2779         int err;
2780
2781         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2782         if (err < 0)
2783                 return err;
2784         err = via_auto_fill_dac_nids(codec);
2785         if (err < 0)
2786                 return err;
2787         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2788                 return -EINVAL;
2789
2790         err = via_auto_create_multi_out_ctls(codec);
2791         if (err < 0)
2792                 return err;
2793         err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
2794         if (err < 0)
2795                 return err;
2796         err = via_auto_create_analog_input_ctls(codec, &spec->autocfg);
2797         if (err < 0)
2798                 return err;
2799
2800         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2801
2802         if (spec->autocfg.dig_outs)
2803                 spec->multiout.dig_out_nid = VT1708B_DIGOUT_NID;
2804         spec->dig_in_pin = VT1708B_DIGIN_PIN;
2805         if (spec->autocfg.dig_in_pin)
2806                 spec->dig_in_nid = VT1708B_DIGIN_NID;
2807
2808         if (spec->kctls.list)
2809                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
2810
2811         spec->input_mux = &spec->private_imux[0];
2812
2813         if (spec->hp_mux)
2814                 via_hp_build(codec);
2815
2816         err = via_smart51_build(codec);
2817         if (err < 0)
2818                 return err;
2819
2820         return 1;
2821 }
2822
2823 #ifdef CONFIG_SND_HDA_POWER_SAVE
2824 static const struct hda_amp_list vt1708B_loopbacks[] = {
2825         { 0x16, HDA_INPUT, 1 },
2826         { 0x16, HDA_INPUT, 2 },
2827         { 0x16, HDA_INPUT, 3 },
2828         { 0x16, HDA_INPUT, 4 },
2829         { } /* end */
2830 };
2831 #endif
2832
2833 static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
2834 {
2835         struct via_spec *spec = codec->spec;
2836         int imux_is_smixer;
2837         unsigned int parm;
2838         int is_8ch = 0;
2839         if ((spec->codec_type != VT1708B_4CH) &&
2840             (codec->vendor_id != 0x11064397))
2841                 is_8ch = 1;
2842
2843         /* SW0 (17h) = stereo mixer */
2844         imux_is_smixer =
2845         (snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
2846          == ((spec->codec_type == VT1708S) ? 5 : 0));
2847         /* inputs */
2848         /* PW 1/2/5 (1ah/1bh/1eh) */
2849         parm = AC_PWRST_D3;
2850         set_pin_power_state(codec, 0x1a, &parm);
2851         set_pin_power_state(codec, 0x1b, &parm);
2852         set_pin_power_state(codec, 0x1e, &parm);
2853         if (imux_is_smixer)
2854                 parm = AC_PWRST_D0;
2855         /* SW0 (17h), AIW 0/1 (13h/14h) */
2856         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
2857         snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
2858         snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
2859
2860         /* outputs */
2861         /* PW0 (19h), SW1 (18h), AOW1 (11h) */
2862         parm = AC_PWRST_D3;
2863         set_pin_power_state(codec, 0x19, &parm);
2864         if (spec->smart51_enabled)
2865                 set_pin_power_state(codec, 0x1b, &parm);
2866         snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
2867         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
2868
2869         /* PW6 (22h), SW2 (26h), AOW2 (24h) */
2870         if (is_8ch) {
2871                 parm = AC_PWRST_D3;
2872                 set_pin_power_state(codec, 0x22, &parm);
2873                 if (spec->smart51_enabled)
2874                         set_pin_power_state(codec, 0x1a, &parm);
2875                 snd_hda_codec_write(codec, 0x26, 0,
2876                                     AC_VERB_SET_POWER_STATE, parm);
2877                 snd_hda_codec_write(codec, 0x24, 0,
2878                                     AC_VERB_SET_POWER_STATE, parm);
2879         } else if (codec->vendor_id == 0x11064397) {
2880                 /* PW7(23h), SW2(27h), AOW2(25h) */
2881                 parm = AC_PWRST_D3;
2882                 set_pin_power_state(codec, 0x23, &parm);
2883                 if (spec->smart51_enabled)
2884                         set_pin_power_state(codec, 0x1a, &parm);
2885                 snd_hda_codec_write(codec, 0x27, 0,
2886                                     AC_VERB_SET_POWER_STATE, parm);
2887                 snd_hda_codec_write(codec, 0x25, 0,
2888                                     AC_VERB_SET_POWER_STATE, parm);
2889         }
2890
2891         /* PW 3/4/7 (1ch/1dh/23h) */
2892         parm = AC_PWRST_D3;
2893         /* force to D0 for internal Speaker */
2894         set_pin_power_state(codec, 0x1c, &parm);
2895         set_pin_power_state(codec, 0x1d, &parm);
2896         if (is_8ch)
2897                 set_pin_power_state(codec, 0x23, &parm);
2898
2899         /* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
2900         snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
2901                             imux_is_smixer ? AC_PWRST_D0 : parm);
2902         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
2903         if (is_8ch) {
2904                 snd_hda_codec_write(codec, 0x25, 0,
2905                                     AC_VERB_SET_POWER_STATE, parm);
2906                 snd_hda_codec_write(codec, 0x27, 0,
2907                                     AC_VERB_SET_POWER_STATE, parm);
2908         } else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
2909                 snd_hda_codec_write(codec, 0x25, 0,
2910                                     AC_VERB_SET_POWER_STATE, parm);
2911 }
2912
2913 static int patch_vt1708S(struct hda_codec *codec);
2914 static int patch_vt1708B_8ch(struct hda_codec *codec)
2915 {
2916         struct via_spec *spec;
2917         int err;
2918
2919         if (get_codec_type(codec) == VT1708BCE)
2920                 return patch_vt1708S(codec);
2921         /* create a codec specific record */
2922         spec = via_new_spec(codec);
2923         if (spec == NULL)
2924                 return -ENOMEM;
2925
2926         spec->aa_mix_nid = 0x16;
2927
2928         /* automatic parse from the BIOS config */
2929         err = vt1708B_parse_auto_config(codec);
2930         if (err < 0) {
2931                 via_free(codec);
2932                 return err;
2933         }
2934
2935         spec->init_verbs[spec->num_iverbs++] = vt1708B_8ch_volume_init_verbs;
2936         spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
2937
2938         codec->patch_ops = via_patch_ops;
2939
2940         codec->patch_ops.init = via_auto_init;
2941         codec->patch_ops.unsol_event = via_unsol_event;
2942 #ifdef CONFIG_SND_HDA_POWER_SAVE
2943         spec->loopback.amplist = vt1708B_loopbacks;
2944 #endif
2945
2946         spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
2947
2948         return 0;
2949 }
2950
2951 static int patch_vt1708B_4ch(struct hda_codec *codec)
2952 {
2953         struct via_spec *spec;
2954         int err;
2955
2956         /* create a codec specific record */
2957         spec = via_new_spec(codec);
2958         if (spec == NULL)
2959                 return -ENOMEM;
2960
2961         /* automatic parse from the BIOS config */
2962         err = vt1708B_parse_auto_config(codec);
2963         if (err < 0) {
2964                 via_free(codec);
2965                 return err;
2966         }
2967
2968         spec->init_verbs[spec->num_iverbs++] = vt1708B_4ch_volume_init_verbs;
2969         spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
2970
2971         codec->patch_ops = via_patch_ops;
2972
2973         codec->patch_ops.init = via_auto_init;
2974         codec->patch_ops.unsol_event = via_unsol_event;
2975 #ifdef CONFIG_SND_HDA_POWER_SAVE
2976         spec->loopback.amplist = vt1708B_loopbacks;
2977 #endif
2978
2979         spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
2980
2981         return 0;
2982 }
2983
2984 /* Patch for VT1708S */
2985
2986 static const struct hda_verb vt1708S_volume_init_verbs[] = {
2987         /* Unmute ADC0-1 and set the default input to mic-in */
2988         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2989         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2990
2991         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the
2992          * analog-loopback mixer widget */
2993         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2994         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2995         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2996         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2997         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2998         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2999
3000         /* Setup default input of PW4 to MW0 */
3001         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
3002         /* PW9, PW10  Output enable */
3003         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3004         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3005         /* Enable Mic Boost Volume backdoor */
3006         {0x1, 0xf98, 0x1},
3007         /* don't bybass mixer */
3008         {0x1, 0xf88, 0xc0},
3009         { }
3010 };
3011
3012 static const struct hda_verb vt1708S_uniwill_init_verbs[] = {
3013         {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
3014          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3015         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3016         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3017         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3018         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3019         {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3020         {0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3021         {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3022         { }
3023 };
3024
3025 static const struct hda_verb vt1705_uniwill_init_verbs[] = {
3026         {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
3027          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3028         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3029         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3030         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3031         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3032         {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3033         {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3034         { }
3035 };
3036
3037 /* fill out digital output widgets; one for master and one for slave outputs */
3038 static void fill_dig_outs(struct hda_codec *codec)
3039 {
3040         struct via_spec *spec = codec->spec;
3041         int i;
3042
3043         for (i = 0; i < spec->autocfg.dig_outs; i++) {
3044                 hda_nid_t nid;
3045                 int conn;
3046
3047                 nid = spec->autocfg.dig_out_pins[i];
3048                 if (!nid)
3049                         continue;
3050                 conn = snd_hda_get_connections(codec, nid, &nid, 1);
3051                 if (conn < 1)
3052                         continue;
3053                 if (!spec->multiout.dig_out_nid)
3054                         spec->multiout.dig_out_nid = nid;
3055                 else {
3056                         spec->slave_dig_outs[0] = nid;
3057                         break; /* at most two dig outs */
3058                 }
3059         }
3060 }
3061
3062 static int vt1708S_parse_auto_config(struct hda_codec *codec)
3063 {
3064         struct via_spec *spec = codec->spec;
3065         int err;
3066
3067         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3068         if (err < 0)
3069                 return err;
3070         err = via_auto_fill_dac_nids(codec);
3071         if (err < 0)
3072                 return err;
3073         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3074                 return -EINVAL;
3075
3076         err = via_auto_create_multi_out_ctls(codec);
3077         if (err < 0)
3078                 return err;
3079         err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
3080         if (err < 0)
3081                 return err;
3082         err = via_auto_create_analog_input_ctls(codec, &spec->autocfg);
3083         if (err < 0)
3084                 return err;
3085
3086         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3087
3088         fill_dig_outs(codec);
3089
3090         if (spec->kctls.list)
3091                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
3092
3093         spec->input_mux = &spec->private_imux[0];
3094
3095         if (spec->hp_mux)
3096                 via_hp_build(codec);
3097
3098         err = via_smart51_build(codec);
3099         if (err < 0)
3100                 return err;
3101
3102         return 1;
3103 }
3104
3105 #ifdef CONFIG_SND_HDA_POWER_SAVE
3106 static const struct hda_amp_list vt1708S_loopbacks[] = {
3107         { 0x16, HDA_INPUT, 1 },
3108         { 0x16, HDA_INPUT, 2 },
3109         { 0x16, HDA_INPUT, 3 },
3110         { 0x16, HDA_INPUT, 4 },
3111         { } /* end */
3112 };
3113 #endif
3114
3115 static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
3116                                int offset, int num_steps, int step_size)
3117 {
3118         snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
3119                                   (offset << AC_AMPCAP_OFFSET_SHIFT) |
3120                                   (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
3121                                   (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
3122                                   (0 << AC_AMPCAP_MUTE_SHIFT));
3123 }
3124
3125 static int patch_vt1708S(struct hda_codec *codec)
3126 {
3127         struct via_spec *spec;
3128         int err;
3129
3130         /* create a codec specific record */
3131         spec = via_new_spec(codec);
3132         if (spec == NULL)
3133                 return -ENOMEM;
3134
3135         spec->aa_mix_nid = 0x16;
3136         override_mic_boost(codec, 0x1a, 0, 3, 40);
3137         override_mic_boost(codec, 0x1e, 0, 3, 40);
3138
3139         /* automatic parse from the BIOS config */
3140         err = vt1708S_parse_auto_config(codec);
3141         if (err < 0) {
3142                 via_free(codec);
3143                 return err;
3144         }
3145
3146         spec->init_verbs[spec->num_iverbs++] = vt1708S_volume_init_verbs;
3147         if (codec->vendor_id == 0x11064397)
3148                 spec->init_verbs[spec->num_iverbs++] =
3149                         vt1705_uniwill_init_verbs;
3150         else
3151                 spec->init_verbs[spec->num_iverbs++] =
3152                         vt1708S_uniwill_init_verbs;
3153
3154         codec->patch_ops = via_patch_ops;
3155
3156         codec->patch_ops.init = via_auto_init;
3157         codec->patch_ops.unsol_event = via_unsol_event;
3158 #ifdef CONFIG_SND_HDA_POWER_SAVE
3159         spec->loopback.amplist = vt1708S_loopbacks;
3160 #endif
3161
3162         /* correct names for VT1708BCE */
3163         if (get_codec_type(codec) == VT1708BCE) {
3164                 kfree(codec->chip_name);
3165                 codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
3166                 snprintf(codec->bus->card->mixername,
3167                          sizeof(codec->bus->card->mixername),
3168                          "%s %s", codec->vendor_name, codec->chip_name);
3169         }
3170         /* correct names for VT1705 */
3171         if (codec->vendor_id == 0x11064397)     {
3172                 kfree(codec->chip_name);
3173                 codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
3174                 snprintf(codec->bus->card->mixername,
3175                          sizeof(codec->bus->card->mixername),
3176                          "%s %s", codec->vendor_name, codec->chip_name);
3177         }
3178         spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
3179         return 0;
3180 }
3181
3182 /* Patch for VT1702 */
3183
3184 static const struct hda_verb vt1702_volume_init_verbs[] = {
3185         /*
3186          * Unmute ADC0-1 and set the default input to mic-in
3187          */
3188         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3189         {0x1F, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3190         {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3191
3192
3193         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3194          * mixer widget
3195          */
3196         /* Amp Indices: Mic1 = 1, Line = 1, Mic2 = 3 */
3197         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3198         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3199         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3200         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3201         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3202
3203         /* Setup default input of PW4 to MW0 */
3204         {0x17, AC_VERB_SET_CONNECT_SEL, 0x1},
3205         /* PW6 PW7 Output enable */
3206         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3207         {0x1C, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3208         /* mixer enable */
3209         {0x1, 0xF88, 0x3},
3210         /* GPIO 0~2 */
3211         {0x1, 0xF82, 0x3F},
3212         { }
3213 };
3214
3215 static const struct hda_verb vt1702_uniwill_init_verbs[] = {
3216         {0x17, AC_VERB_SET_UNSOLICITED_ENABLE,
3217          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3218         {0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3219         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3220         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3221         {0x18, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3222         { }
3223 };
3224
3225 static int vt1702_parse_auto_config(struct hda_codec *codec)
3226 {
3227         struct via_spec *spec = codec->spec;
3228         int err;
3229
3230         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3231         if (err < 0)
3232                 return err;
3233         err = via_auto_fill_dac_nids(codec);
3234         if (err < 0)
3235                 return err;
3236         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3237                 return -EINVAL;
3238
3239         err = via_auto_create_multi_out_ctls(codec);
3240         if (err < 0)
3241                 return err;
3242         err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
3243         if (err < 0)
3244                 return err;
3245         /* limit AA path volume to 0 dB */
3246         snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
3247                                   (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
3248                                   (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
3249                                   (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
3250                                   (1 << AC_AMPCAP_MUTE_SHIFT));
3251         err = via_auto_create_analog_input_ctls(codec, &spec->autocfg);
3252         if (err < 0)
3253                 return err;
3254
3255         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3256
3257         fill_dig_outs(codec);
3258
3259         if (spec->kctls.list)
3260                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
3261
3262         spec->input_mux = &spec->private_imux[0];
3263
3264         if (spec->hp_mux)
3265                 via_hp_build(codec);
3266
3267         return 1;
3268 }
3269
3270 #ifdef CONFIG_SND_HDA_POWER_SAVE
3271 static const struct hda_amp_list vt1702_loopbacks[] = {
3272         { 0x1A, HDA_INPUT, 1 },
3273         { 0x1A, HDA_INPUT, 2 },
3274         { 0x1A, HDA_INPUT, 3 },
3275         { 0x1A, HDA_INPUT, 4 },
3276         { } /* end */
3277 };
3278 #endif
3279
3280 static void set_widgets_power_state_vt1702(struct hda_codec *codec)
3281 {
3282         int imux_is_smixer =
3283         snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
3284         unsigned int parm;
3285         /* inputs */
3286         /* PW 1/2/5 (14h/15h/18h) */
3287         parm = AC_PWRST_D3;
3288         set_pin_power_state(codec, 0x14, &parm);
3289         set_pin_power_state(codec, 0x15, &parm);
3290         set_pin_power_state(codec, 0x18, &parm);
3291         if (imux_is_smixer)
3292                 parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
3293         /* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
3294         snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
3295         snd_hda_codec_write(codec, 0x12, 0, AC_VERB_SET_POWER_STATE, parm);
3296         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
3297         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_POWER_STATE, parm);
3298
3299         /* outputs */
3300         /* PW 3/4 (16h/17h) */
3301         parm = AC_PWRST_D3;
3302         set_pin_power_state(codec, 0x17, &parm);
3303         set_pin_power_state(codec, 0x16, &parm);
3304         /* MW0 (1ah), AOW 0/1 (10h/1dh) */
3305         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
3306                             imux_is_smixer ? AC_PWRST_D0 : parm);
3307         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
3308         snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
3309 }
3310
3311 static int patch_vt1702(struct hda_codec *codec)
3312 {
3313         struct via_spec *spec;
3314         int err;
3315
3316         /* create a codec specific record */
3317         spec = via_new_spec(codec);
3318         if (spec == NULL)
3319                 return -ENOMEM;
3320
3321         spec->aa_mix_nid = 0x1a;
3322
3323         /* automatic parse from the BIOS config */
3324         err = vt1702_parse_auto_config(codec);
3325         if (err < 0) {
3326                 via_free(codec);
3327                 return err;
3328         }
3329
3330         spec->init_verbs[spec->num_iverbs++] = vt1702_volume_init_verbs;
3331         spec->init_verbs[spec->num_iverbs++] = vt1702_uniwill_init_verbs;
3332
3333         codec->patch_ops = via_patch_ops;
3334
3335         codec->patch_ops.init = via_auto_init;
3336         codec->patch_ops.unsol_event = via_unsol_event;
3337 #ifdef CONFIG_SND_HDA_POWER_SAVE
3338         spec->loopback.amplist = vt1702_loopbacks;
3339 #endif
3340
3341         spec->set_widgets_power_state =  set_widgets_power_state_vt1702;
3342         return 0;
3343 }
3344
3345 /* Patch for VT1718S */
3346
3347 static const struct hda_verb vt1718S_volume_init_verbs[] = {
3348         /*
3349          * Unmute ADC0-1 and set the default input to mic-in
3350          */
3351         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3352         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3353
3354         /* Enable MW0 adjust Gain 5 */
3355         {0x1, 0xfb2, 0x10},
3356         /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3357          * mixer widget
3358          */
3359         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3360         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3361         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3362         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
3363         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
3364         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(5)},
3365
3366         /* Setup default input of Front HP to MW9 */
3367         {0x28, AC_VERB_SET_CONNECT_SEL, 0x1},
3368         /* PW9 PW10 Output enable */
3369         {0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
3370         {0x2e, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
3371         /* PW11 Input enable */
3372         {0x2f, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_IN_EN},
3373         /* Enable Boost Volume backdoor */
3374         {0x1, 0xf88, 0x8},
3375         /* MW0/1/2/3/4: un-mute index 0 (AOWx), mute index 1 (MW9) */
3376         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3377         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3378         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3379         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3380         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3381         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3382         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3383         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3384         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3385         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3386         /* set MUX1 = 2 (AOW4), MUX2 = 1 (AOW3) */
3387         {0x34, AC_VERB_SET_CONNECT_SEL, 0x2},
3388         {0x35, AC_VERB_SET_CONNECT_SEL, 0x1},
3389         /* Unmute MW4's index 0 */
3390         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3391         { }
3392 };
3393
3394
3395 static const struct hda_verb vt1718S_uniwill_init_verbs[] = {
3396         {0x28, AC_VERB_SET_UNSOLICITED_ENABLE,
3397          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3398         {0x24, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3399         {0x25, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3400         {0x26, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3401         {0x27, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3402         {0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3403         {0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3404         {0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3405         { }
3406 };
3407
3408 static int vt1718S_parse_auto_config(struct hda_codec *codec)
3409 {
3410         struct via_spec *spec = codec->spec;
3411         int err;
3412
3413         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3414
3415         if (err < 0)
3416                 return err;
3417         err = via_auto_fill_dac_nids(codec);
3418         if (err < 0)
3419                 return err;
3420         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3421                 return -EINVAL;
3422
3423         err = via_auto_create_multi_out_ctls(codec);
3424         if (err < 0)
3425                 return err;
3426         err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
3427         if (err < 0)
3428                 return err;
3429         err = via_auto_create_analog_input_ctls(codec, &spec->autocfg);
3430         if (err < 0)
3431                 return err;
3432
3433         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3434
3435         fill_dig_outs(codec);
3436
3437         if (spec->autocfg.dig_in_pin && codec->vendor_id == 0x11060428)
3438                 spec->dig_in_nid = 0x13;
3439
3440         if (spec->kctls.list)
3441                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
3442
3443         spec->input_mux = &spec->private_imux[0];
3444
3445         if (spec->hp_mux)
3446                 via_hp_build(codec);
3447
3448         err = via_smart51_build(codec);
3449         if (err < 0)
3450                 return err;
3451
3452         return 1;
3453 }
3454
3455 #ifdef CONFIG_SND_HDA_POWER_SAVE
3456 static const struct hda_amp_list vt1718S_loopbacks[] = {
3457         { 0x21, HDA_INPUT, 1 },
3458         { 0x21, HDA_INPUT, 2 },
3459         { 0x21, HDA_INPUT, 3 },
3460         { 0x21, HDA_INPUT, 4 },
3461         { } /* end */
3462 };
3463 #endif
3464
3465 static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
3466 {
3467         struct via_spec *spec = codec->spec;
3468         int imux_is_smixer;
3469         unsigned int parm;
3470         /* MUX6 (1eh) = stereo mixer */
3471         imux_is_smixer =
3472         snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
3473         /* inputs */
3474         /* PW 5/6/7 (29h/2ah/2bh) */
3475         parm = AC_PWRST_D3;
3476         set_pin_power_state(codec, 0x29, &parm);
3477         set_pin_power_state(codec, 0x2a, &parm);
3478         set_pin_power_state(codec, 0x2b, &parm);
3479         if (imux_is_smixer)
3480                 parm = AC_PWRST_D0;
3481         /* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
3482         snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
3483         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
3484         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
3485         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
3486
3487         /* outputs */
3488         /* PW3 (27h), MW2 (1ah), AOW3 (bh) */
3489         parm = AC_PWRST_D3;
3490         set_pin_power_state(codec, 0x27, &parm);
3491         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE, parm);
3492         snd_hda_codec_write(codec, 0xb, 0, AC_VERB_SET_POWER_STATE, parm);
3493
3494         /* PW2 (26h), AOW2 (ah) */
3495         parm = AC_PWRST_D3;
3496         set_pin_power_state(codec, 0x26, &parm);
3497         if (spec->smart51_enabled)
3498                 set_pin_power_state(codec, 0x2b, &parm);
3499         snd_hda_codec_write(codec, 0xa, 0, AC_VERB_SET_POWER_STATE, parm);
3500
3501         /* PW0 (24h), AOW0 (8h) */
3502         parm = AC_PWRST_D3;
3503         set_pin_power_state(codec, 0x24, &parm);
3504         if (!spec->hp_independent_mode) /* check for redirected HP */
3505                 set_pin_power_state(codec, 0x28, &parm);
3506         snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
3507         /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
3508         snd_hda_codec_write(codec, 0x21, 0, AC_VERB_SET_POWER_STATE,
3509                             imux_is_smixer ? AC_PWRST_D0 : parm);
3510
3511         /* PW1 (25h), AOW1 (9h) */
3512         parm = AC_PWRST_D3;
3513         set_pin_power_state(codec, 0x25, &parm);
3514         if (spec->smart51_enabled)
3515                 set_pin_power_state(codec, 0x2a, &parm);
3516         snd_hda_codec_write(codec, 0x9, 0, AC_VERB_SET_POWER_STATE, parm);
3517
3518         if (spec->hp_independent_mode) {
3519                 /* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
3520                 parm = AC_PWRST_D3;
3521                 set_pin_power_state(codec, 0x28, &parm);
3522                 snd_hda_codec_write(codec, 0x1b, 0,
3523                                     AC_VERB_SET_POWER_STATE, parm);
3524                 snd_hda_codec_write(codec, 0x34, 0,
3525                                     AC_VERB_SET_POWER_STATE, parm);
3526                 snd_hda_codec_write(codec, 0xc, 0,
3527                                     AC_VERB_SET_POWER_STATE, parm);
3528         }
3529 }
3530
3531 static int patch_vt1718S(struct hda_codec *codec)
3532 {
3533         struct via_spec *spec;
3534         int err;
3535
3536         /* create a codec specific record */
3537         spec = via_new_spec(codec);
3538         if (spec == NULL)
3539                 return -ENOMEM;
3540
3541         spec->aa_mix_nid = 0x21;
3542         override_mic_boost(codec, 0x2b, 0, 3, 40);
3543         override_mic_boost(codec, 0x29, 0, 3, 40);
3544
3545         /* automatic parse from the BIOS config */
3546         err = vt1718S_parse_auto_config(codec);
3547         if (err < 0) {
3548                 via_free(codec);
3549                 return err;
3550         }
3551
3552         spec->init_verbs[spec->num_iverbs++] = vt1718S_volume_init_verbs;
3553         spec->init_verbs[spec->num_iverbs++] = vt1718S_uniwill_init_verbs;
3554
3555         codec->patch_ops = via_patch_ops;
3556
3557         codec->patch_ops.init = via_auto_init;
3558         codec->patch_ops.unsol_event = via_unsol_event;
3559
3560 #ifdef CONFIG_SND_HDA_POWER_SAVE
3561         spec->loopback.amplist = vt1718S_loopbacks;
3562 #endif
3563
3564         spec->set_widgets_power_state =  set_widgets_power_state_vt1718S;
3565
3566         return 0;
3567 }
3568
3569 /* Patch for VT1716S */
3570
3571 static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
3572                             struct snd_ctl_elem_info *uinfo)
3573 {
3574         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3575         uinfo->count = 1;
3576         uinfo->value.integer.min = 0;
3577         uinfo->value.integer.max = 1;
3578         return 0;
3579 }
3580
3581 static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
3582                            struct snd_ctl_elem_value *ucontrol)
3583 {
3584         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3585         int index = 0;
3586
3587         index = snd_hda_codec_read(codec, 0x26, 0,
3588                                                AC_VERB_GET_CONNECT_SEL, 0);
3589         if (index != -1)
3590                 *ucontrol->value.integer.value = index;
3591
3592         return 0;
3593 }
3594
3595 static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
3596                            struct snd_ctl_elem_value *ucontrol)
3597 {
3598         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3599         struct via_spec *spec = codec->spec;
3600         int index = *ucontrol->value.integer.value;
3601
3602         snd_hda_codec_write(codec, 0x26, 0,
3603                                                AC_VERB_SET_CONNECT_SEL, index);
3604         spec->dmic_enabled = index;
3605         set_widgets_power_state(codec);
3606         return 1;
3607 }
3608
3609 static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
3610         HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
3611         {
3612          .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3613          .name = "Digital Mic Capture Switch",
3614          .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
3615          .count = 1,
3616          .info = vt1716s_dmic_info,
3617          .get = vt1716s_dmic_get,
3618          .put = vt1716s_dmic_put,
3619          },
3620         {}                      /* end */
3621 };
3622
3623
3624 /* mono-out mixer elements */
3625 static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
3626         HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
3627         { } /* end */
3628 };
3629
3630 static const struct hda_verb vt1716S_volume_init_verbs[] = {
3631         /*
3632          * Unmute ADC0-1 and set the default input to mic-in
3633          */
3634         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3635         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3636
3637
3638         /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3639          * mixer widget
3640          */
3641         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3642         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3643         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3644         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
3645         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
3646         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3647
3648         /* MUX Indices: Stereo Mixer = 5 */
3649         {0x17, AC_VERB_SET_CONNECT_SEL, 0x5},
3650
3651         /* Setup default input of PW4 to MW0 */
3652         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
3653
3654         /* Setup default input of SW1 as MW0 */
3655         {0x18, AC_VERB_SET_CONNECT_SEL, 0x1},
3656
3657         /* Setup default input of SW4 as AOW0 */
3658         {0x28, AC_VERB_SET_CONNECT_SEL, 0x1},
3659
3660         /* PW9 PW10 Output enable */
3661         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3662         {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3663
3664         /* Unmute SW1, PW12 */
3665         {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3666         {0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3667         /* PW12 Output enable */
3668         {0x2a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3669         /* Enable Boost Volume backdoor */
3670         {0x1, 0xf8a, 0x80},
3671         /* don't bybass mixer */
3672         {0x1, 0xf88, 0xc0},
3673         /* Enable mono output */
3674         {0x1, 0xf90, 0x08},
3675         { }
3676 };
3677
3678
3679 static const struct hda_verb vt1716S_uniwill_init_verbs[] = {
3680         {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
3681          AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3682         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3683         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3684         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3685         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE,
3686          AC_USRSP_EN | VIA_MONO_EVENT | VIA_JACK_EVENT},
3687         {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3688         {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3689         { }
3690 };
3691
3692 static int vt1716S_parse_auto_config(struct hda_codec *codec)
3693 {
3694         struct via_spec *spec = codec->spec;
3695         int err;
3696
3697         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3698         if (err < 0)
3699                 return err;
3700         err = via_auto_fill_dac_nids(codec);
3701         if (err < 0)
3702                 return err;
3703         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3704                 return -EINVAL;
3705
3706         err = via_auto_create_multi_out_ctls(codec);
3707         if (err < 0)
3708                 return err;
3709         err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
3710         if (err < 0)
3711                 return err;
3712         err = via_auto_create_analog_input_ctls(codec, &spec->autocfg);
3713         if (err < 0)
3714                 return err;
3715
3716         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3717
3718         fill_dig_outs(codec);
3719
3720         if (spec->kctls.list)
3721                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
3722
3723         spec->input_mux = &spec->private_imux[0];
3724
3725         if (spec->hp_mux)
3726                 via_hp_build(codec);
3727
3728         err = via_smart51_build(codec);
3729         if (err < 0)
3730                 return err;
3731
3732         return 1;
3733 }
3734
3735 #ifdef CONFIG_SND_HDA_POWER_SAVE
3736 static const struct hda_amp_list vt1716S_loopbacks[] = {
3737         { 0x16, HDA_INPUT, 1 },
3738         { 0x16, HDA_INPUT, 2 },
3739         { 0x16, HDA_INPUT, 3 },
3740         { 0x16, HDA_INPUT, 4 },
3741         { } /* end */
3742 };
3743 #endif
3744
3745 static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
3746 {
3747         struct via_spec *spec = codec->spec;
3748         int imux_is_smixer;
3749         unsigned int parm;
3750         unsigned int mono_out, present;
3751         /* SW0 (17h) = stereo mixer */
3752         imux_is_smixer =
3753         (snd_hda_codec_read(codec, 0x17, 0,
3754                             AC_VERB_GET_CONNECT_SEL, 0x00) ==  5);
3755         /* inputs */
3756         /* PW 1/2/5 (1ah/1bh/1eh) */
3757         parm = AC_PWRST_D3;
3758         set_pin_power_state(codec, 0x1a, &parm);
3759         set_pin_power_state(codec, 0x1b, &parm);
3760         set_pin_power_state(codec, 0x1e, &parm);
3761         if (imux_is_smixer)
3762                 parm = AC_PWRST_D0;
3763         /* SW0 (17h), AIW0(13h) */
3764         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
3765         snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
3766
3767         parm = AC_PWRST_D3;
3768         set_pin_power_state(codec, 0x1e, &parm);
3769         /* PW11 (22h) */
3770         if (spec->dmic_enabled)
3771                 set_pin_power_state(codec, 0x22, &parm);
3772         else
3773                 snd_hda_codec_write(codec, 0x22, 0,
3774                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
3775
3776         /* SW2(26h), AIW1(14h) */
3777         snd_hda_codec_write(codec, 0x26, 0, AC_VERB_SET_POWER_STATE, parm);
3778         snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
3779
3780         /* outputs */
3781         /* PW0 (19h), SW1 (18h), AOW1 (11h) */
3782         parm = AC_PWRST_D3;
3783         set_pin_power_state(codec, 0x19, &parm);
3784         /* Smart 5.1 PW2(1bh) */
3785         if (spec->smart51_enabled)
3786                 set_pin_power_state(codec, 0x1b, &parm);
3787         snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
3788         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
3789
3790         /* PW7 (23h), SW3 (27h), AOW3 (25h) */
3791         parm = AC_PWRST_D3;
3792         set_pin_power_state(codec, 0x23, &parm);
3793         /* Smart 5.1 PW1(1ah) */
3794         if (spec->smart51_enabled)
3795                 set_pin_power_state(codec, 0x1a, &parm);
3796         snd_hda_codec_write(codec, 0x27, 0, AC_VERB_SET_POWER_STATE, parm);
3797
3798         /* Smart 5.1 PW5(1eh) */
3799         if (spec->smart51_enabled)
3800                 set_pin_power_state(codec, 0x1e, &parm);
3801         snd_hda_codec_write(codec, 0x25, 0, AC_VERB_SET_POWER_STATE, parm);
3802
3803         /* Mono out */
3804         /* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
3805         present = snd_hda_jack_detect(codec, 0x1c);
3806
3807         if (present)
3808                 mono_out = 0;
3809         else {
3810                 present = snd_hda_jack_detect(codec, 0x1d);
3811                 if (!spec->hp_independent_mode && present)
3812                         mono_out = 0;
3813                 else
3814                         mono_out = 1;
3815         }
3816         parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
3817         snd_hda_codec_write(codec, 0x28, 0, AC_VERB_SET_POWER_STATE, parm);
3818         snd_hda_codec_write(codec, 0x29, 0, AC_VERB_SET_POWER_STATE, parm);
3819         snd_hda_codec_write(codec, 0x2a, 0, AC_VERB_SET_POWER_STATE, parm);
3820
3821         /* PW 3/4 (1ch/1dh) */
3822         parm = AC_PWRST_D3;
3823         set_pin_power_state(codec, 0x1c, &parm);
3824         set_pin_power_state(codec, 0x1d, &parm);
3825         /* HP Independent Mode, power on AOW3 */
3826         if (spec->hp_independent_mode)
3827                 snd_hda_codec_write(codec, 0x25, 0,
3828                                     AC_VERB_SET_POWER_STATE, parm);
3829
3830         /* force to D0 for internal Speaker */
3831         /* MW0 (16h), AOW0 (10h) */
3832         snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
3833                             imux_is_smixer ? AC_PWRST_D0 : parm);
3834         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
3835                             mono_out ? AC_PWRST_D0 : parm);
3836 }
3837
3838 static int patch_vt1716S(struct hda_codec *codec)
3839 {
3840         struct via_spec *spec;
3841         int err;
3842
3843         /* create a codec specific record */
3844         spec = via_new_spec(codec);
3845         if (spec == NULL)
3846                 return -ENOMEM;
3847
3848         spec->aa_mix_nid = 0x16;
3849         override_mic_boost(codec, 0x1a, 0, 3, 40);
3850         override_mic_boost(codec, 0x1e, 0, 3, 40);
3851
3852         /* automatic parse from the BIOS config */
3853         err = vt1716S_parse_auto_config(codec);
3854         if (err < 0) {
3855                 via_free(codec);
3856                 return err;
3857         }
3858
3859         spec->init_verbs[spec->num_iverbs++]  = vt1716S_volume_init_verbs;
3860         spec->init_verbs[spec->num_iverbs++] = vt1716S_uniwill_init_verbs;
3861
3862         spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
3863         spec->num_mixers++;
3864
3865         spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
3866
3867         codec->patch_ops = via_patch_ops;
3868
3869         codec->patch_ops.init = via_auto_init;
3870         codec->patch_ops.unsol_event = via_unsol_event;
3871
3872 #ifdef CONFIG_SND_HDA_POWER_SAVE
3873         spec->loopback.amplist = vt1716S_loopbacks;
3874 #endif
3875
3876         spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
3877         return 0;
3878 }
3879
3880 /* for vt2002P */
3881
3882 static const struct hda_verb vt2002P_volume_init_verbs[] = {
3883         /* Class-D speaker related verbs */
3884         {0x1, 0xfe0, 0x4},
3885         {0x1, 0xfe9, 0x80},
3886         {0x1, 0xfe2, 0x22},
3887         /*
3888          * Unmute ADC0-1 and set the default input to mic-in
3889          */
3890         {0x8, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3891         {0x9, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3892
3893
3894         /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3895          * mixer widget
3896          */
3897         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3898         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3899         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3900         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
3901         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
3902         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3903
3904         /* MUX Indices: Mic = 0 */
3905         {0x1e, AC_VERB_SET_CONNECT_SEL, 0},
3906         {0x1f, AC_VERB_SET_CONNECT_SEL, 0},
3907
3908         /* PW9 Output enable */
3909         {0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
3910
3911         /* Enable Boost Volume backdoor */
3912         {0x1, 0xfb9, 0x24},
3913
3914         /* MW0/1/4/8: un-mute index 0 (MUXx), un-mute index 1 (MW9) */
3915         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3916         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3917         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3918         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3919         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3920         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3921         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3922         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3923
3924         /* set MUX0/1/4/8 = 0 (AOW0) */
3925         {0x34, AC_VERB_SET_CONNECT_SEL, 0},
3926         {0x35, AC_VERB_SET_CONNECT_SEL, 0},
3927         {0x37, AC_VERB_SET_CONNECT_SEL, 0},
3928         {0x3b, AC_VERB_SET_CONNECT_SEL, 0},
3929
3930         /* set PW0 index=0 (MW0) */
3931         {0x24, AC_VERB_SET_CONNECT_SEL, 0},
3932
3933         /* Enable AOW0 to MW9 */
3934         {0x1, 0xfb8, 0x88},
3935         { }
3936 };
3937 static const struct hda_verb vt1802_volume_init_verbs[] = {
3938         /*
3939          * Unmute ADC0-1 and set the default input to mic-in
3940          */
3941         {0x8, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3942         {0x9, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3943
3944
3945         /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3946          * mixer widget
3947          */
3948         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3949         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3950         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3951         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
3952         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
3953         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3954
3955         /* MUX Indices: Mic = 0 */
3956         {0x1e, AC_VERB_SET_CONNECT_SEL, 0},
3957         {0x1f, AC_VERB_SET_CONNECT_SEL, 0},
3958
3959         /* PW9 Output enable */
3960         {0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
3961
3962         /* Enable Boost Volume backdoor */
3963         {0x1, 0xfb9, 0x24},
3964
3965         /* MW0/1/4/8: un-mute index 0 (MUXx), un-mute index 1 (MW9) */
3966         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3967         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3968         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3969         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3970         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3971         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3972         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3973         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3974
3975         /* set MUX0/1/4/8 = 0 (AOW0) */
3976         {0x34, AC_VERB_SET_CONNECT_SEL, 0},
3977         {0x35, AC_VERB_SET_CONNECT_SEL, 0},
3978         {0x38, AC_VERB_SET_CONNECT_SEL, 0},
3979         {0x3c, AC_VERB_SET_CONNECT_SEL, 0},
3980
3981         /* set PW0 index=0 (MW0) */
3982         {0x24, AC_VERB_SET_CONNECT_SEL, 0},
3983
3984         /* Enable AOW0 to MW9 */
3985         {0x1, 0xfb8, 0x88},
3986         { }
3987 };
3988
3989
3990 static const struct hda_verb vt2002P_uniwill_init_verbs[] = {
3991         {0x25, AC_VERB_SET_UNSOLICITED_ENABLE,
3992          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
3993         {0x26, AC_VERB_SET_UNSOLICITED_ENABLE,
3994          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
3995         {0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3996         {0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3997         {0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3998         { }
3999 };
4000 static const struct hda_verb vt1802_uniwill_init_verbs[] = {
4001         {0x25, AC_VERB_SET_UNSOLICITED_ENABLE,
4002          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
4003         {0x28, AC_VERB_SET_UNSOLICITED_ENABLE,
4004          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
4005         {0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4006         {0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4007         {0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4008         { }
4009 };
4010
4011 static int vt2002P_parse_auto_config(struct hda_codec *codec)
4012 {
4013         struct via_spec *spec = codec->spec;
4014         int err;
4015
4016
4017         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4018         if (err < 0)
4019                 return err;
4020
4021         err = via_auto_fill_dac_nids(codec);
4022         if (err < 0)
4023                 return err;
4024
4025         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
4026                 return -EINVAL;
4027
4028         err = via_auto_create_multi_out_ctls(codec);
4029         if (err < 0)
4030                 return err;
4031         err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
4032         if (err < 0)
4033                 return err;
4034         err = via_auto_create_analog_input_ctls(codec, &spec->autocfg);
4035         if (err < 0)
4036                 return err;
4037
4038         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4039
4040         fill_dig_outs(codec);
4041
4042         if (spec->kctls.list)
4043                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
4044
4045         spec->input_mux = &spec->private_imux[0];
4046
4047         if (spec->hp_mux)
4048                 via_hp_build(codec);
4049
4050         return 1;
4051 }
4052
4053 #ifdef CONFIG_SND_HDA_POWER_SAVE
4054 static const struct hda_amp_list vt2002P_loopbacks[] = {
4055         { 0x21, HDA_INPUT, 0 },
4056         { 0x21, HDA_INPUT, 1 },
4057         { 0x21, HDA_INPUT, 2 },
4058         { } /* end */
4059 };
4060 #endif
4061
4062 static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
4063 {
4064         struct via_spec *spec = codec->spec;
4065         int imux_is_smixer;
4066         unsigned int parm;
4067         unsigned int present;
4068         /* MUX9 (1eh) = stereo mixer */
4069         imux_is_smixer =
4070         snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
4071         /* inputs */
4072         /* PW 5/6/7 (29h/2ah/2bh) */
4073         parm = AC_PWRST_D3;
4074         set_pin_power_state(codec, 0x29, &parm);
4075         set_pin_power_state(codec, 0x2a, &parm);
4076         set_pin_power_state(codec, 0x2b, &parm);
4077         parm = AC_PWRST_D0;
4078         /* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
4079         snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
4080         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
4081         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
4082         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
4083
4084         /* outputs */
4085         /* AOW0 (8h)*/
4086         snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
4087
4088         if (spec->codec_type == VT1802) {
4089                 /* PW4 (28h), MW4 (18h), MUX4(38h) */
4090                 parm = AC_PWRST_D3;
4091                 set_pin_power_state(codec, 0x28, &parm);
4092                 snd_hda_codec_write(codec, 0x18, 0,
4093                                     AC_VERB_SET_POWER_STATE, parm);
4094                 snd_hda_codec_write(codec, 0x38, 0,
4095                                     AC_VERB_SET_POWER_STATE, parm);
4096         } else {
4097                 /* PW4 (26h), MW4 (1ch), MUX4(37h) */
4098                 parm = AC_PWRST_D3;
4099                 set_pin_power_state(codec, 0x26, &parm);
4100                 snd_hda_codec_write(codec, 0x1c, 0,
4101                                     AC_VERB_SET_POWER_STATE, parm);
4102                 snd_hda_codec_write(codec, 0x37, 0,
4103                                     AC_VERB_SET_POWER_STATE, parm);
4104         }
4105
4106         if (spec->codec_type == VT1802) {
4107                 /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
4108                 parm = AC_PWRST_D3;
4109                 set_pin_power_state(codec, 0x25, &parm);
4110                 snd_hda_codec_write(codec, 0x15, 0,
4111                                     AC_VERB_SET_POWER_STATE, parm);
4112                 snd_hda_codec_write(codec, 0x35, 0,
4113                                     AC_VERB_SET_POWER_STATE, parm);
4114         } else {
4115                 /* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
4116                 parm = AC_PWRST_D3;
4117                 set_pin_power_state(codec, 0x25, &parm);
4118                 snd_hda_codec_write(codec, 0x19, 0,
4119                                     AC_VERB_SET_POWER_STATE, parm);
4120                 snd_hda_codec_write(codec, 0x35, 0,
4121                                     AC_VERB_SET_POWER_STATE, parm);
4122         }
4123
4124         if (spec->hp_independent_mode)
4125                 snd_hda_codec_write(codec, 0x9, 0,
4126                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
4127
4128         /* Class-D */
4129         /* PW0 (24h), MW0(18h/14h), MUX0(34h) */
4130         present = snd_hda_jack_detect(codec, 0x25);
4131
4132         parm = AC_PWRST_D3;
4133         set_pin_power_state(codec, 0x24, &parm);
4134         parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
4135         if (spec->codec_type == VT1802)
4136                 snd_hda_codec_write(codec, 0x14, 0,
4137                                     AC_VERB_SET_POWER_STATE, parm);
4138         else
4139                 snd_hda_codec_write(codec, 0x18, 0,
4140                                     AC_VERB_SET_POWER_STATE, parm);
4141         snd_hda_codec_write(codec, 0x34, 0, AC_VERB_SET_POWER_STATE, parm);
4142
4143         /* Mono Out */
4144         present = snd_hda_jack_detect(codec, 0x26);
4145
4146         parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
4147         if (spec->codec_type == VT1802) {
4148                 /* PW15 (33h), MW8(1ch), MUX8(3ch) */
4149                 snd_hda_codec_write(codec, 0x33, 0,
4150                                     AC_VERB_SET_POWER_STATE, parm);
4151                 snd_hda_codec_write(codec, 0x1c, 0,
4152                                     AC_VERB_SET_POWER_STATE, parm);
4153                 snd_hda_codec_write(codec, 0x3c, 0,
4154                                     AC_VERB_SET_POWER_STATE, parm);
4155         } else {
4156                 /* PW15 (31h), MW8(17h), MUX8(3bh) */
4157                 snd_hda_codec_write(codec, 0x31, 0,
4158                                     AC_VERB_SET_POWER_STATE, parm);
4159                 snd_hda_codec_write(codec, 0x17, 0,
4160                                     AC_VERB_SET_POWER_STATE, parm);
4161                 snd_hda_codec_write(codec, 0x3b, 0,
4162                                     AC_VERB_SET_POWER_STATE, parm);
4163         }
4164         /* MW9 (21h) */
4165         if (imux_is_smixer || !is_aa_path_mute(codec))
4166                 snd_hda_codec_write(codec, 0x21, 0,
4167                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
4168         else
4169                 snd_hda_codec_write(codec, 0x21, 0,
4170                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
4171 }
4172
4173 /* patch for vt2002P */
4174 static int patch_vt2002P(struct hda_codec *codec)
4175 {
4176         struct via_spec *spec;
4177         int err;
4178
4179         /* create a codec specific record */
4180         spec = via_new_spec(codec);
4181         if (spec == NULL)
4182                 return -ENOMEM;
4183
4184         spec->aa_mix_nid = 0x21;
4185         override_mic_boost(codec, 0x2b, 0, 3, 40);
4186         override_mic_boost(codec, 0x29, 0, 3, 40);
4187
4188         /* automatic parse from the BIOS config */
4189         err = vt2002P_parse_auto_config(codec);
4190         if (err < 0) {
4191                 via_free(codec);
4192                 return err;
4193         }
4194
4195         if (spec->codec_type == VT1802)
4196                 spec->init_verbs[spec->num_iverbs++]  =
4197                         vt1802_volume_init_verbs;
4198         else
4199                 spec->init_verbs[spec->num_iverbs++]  =
4200                         vt2002P_volume_init_verbs;
4201
4202         if (spec->codec_type == VT1802)
4203                 spec->init_verbs[spec->num_iverbs++] =
4204                         vt1802_uniwill_init_verbs;
4205         else
4206                 spec->init_verbs[spec->num_iverbs++] =
4207                         vt2002P_uniwill_init_verbs;
4208
4209         codec->patch_ops = via_patch_ops;
4210
4211         codec->patch_ops.init = via_auto_init;
4212         codec->patch_ops.unsol_event = via_unsol_event;
4213
4214 #ifdef CONFIG_SND_HDA_POWER_SAVE
4215         spec->loopback.amplist = vt2002P_loopbacks;
4216 #endif
4217
4218         spec->set_widgets_power_state =  set_widgets_power_state_vt2002P;
4219         return 0;
4220 }
4221
4222 /* for vt1812 */
4223
4224 static const struct hda_verb vt1812_volume_init_verbs[] = {
4225         /*
4226          * Unmute ADC0-1 and set the default input to mic-in
4227          */
4228         {0x8, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4229         {0x9, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4230
4231
4232         /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
4233          * mixer widget
4234          */
4235         /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
4236         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4237         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4238         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
4239         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
4240         {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
4241
4242         /* MUX Indices: Mic = 0 */
4243         {0x1e, AC_VERB_SET_CONNECT_SEL, 0},
4244         {0x1f, AC_VERB_SET_CONNECT_SEL, 0},
4245
4246         /* PW9 Output enable */
4247         {0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
4248
4249         /* Enable Boost Volume backdoor */
4250         {0x1, 0xfb9, 0x24},
4251
4252         /* MW0/1/4/13/15: un-mute index 0 (MUXx), un-mute index 1 (MW9) */
4253         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4254         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4255         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4256         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4257         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4258         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4259         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4260         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4261         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4262         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4263
4264         /* set MUX0/1/4/13/15 = 0 (AOW0) */
4265         {0x34, AC_VERB_SET_CONNECT_SEL, 0},
4266         {0x35, AC_VERB_SET_CONNECT_SEL, 0},
4267         {0x38, AC_VERB_SET_CONNECT_SEL, 0},
4268         {0x3c, AC_VERB_SET_CONNECT_SEL, 0},
4269         {0x3d, AC_VERB_SET_CONNECT_SEL, 0},
4270
4271         /* Enable AOW0 to MW9 */
4272         {0x1, 0xfb8, 0xa8},
4273         { }
4274 };
4275
4276
4277 static const struct hda_verb vt1812_uniwill_init_verbs[] = {
4278         {0x33, AC_VERB_SET_UNSOLICITED_ENABLE,
4279          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
4280         {0x25, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT },
4281         {0x28, AC_VERB_SET_UNSOLICITED_ENABLE,
4282          AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
4283         {0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4284         {0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4285         {0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4286         { }
4287 };
4288
4289 static int vt1812_parse_auto_config(struct hda_codec *codec)
4290 {
4291         struct via_spec *spec = codec->spec;
4292         int err;
4293
4294
4295         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4296         if (err < 0)
4297                 return err;
4298         fill_dig_outs(codec);
4299         err = via_auto_fill_dac_nids(codec);
4300         if (err < 0)
4301                 return err;
4302
4303         if (!spec->autocfg.line_outs && !spec->autocfg.hp_outs)
4304                 return -EINVAL;
4305
4306         err = via_auto_create_multi_out_ctls(codec);
4307         if (err < 0)
4308                 return err;
4309         err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
4310         if (err < 0)
4311                 return err;
4312         err = via_auto_create_analog_input_ctls(codec, &spec->autocfg);
4313         if (err < 0)
4314                 return err;
4315
4316         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4317
4318         fill_dig_outs(codec);
4319
4320         if (spec->kctls.list)
4321                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
4322
4323         spec->input_mux = &spec->private_imux[0];
4324
4325         if (spec->hp_mux)
4326                 via_hp_build(codec);
4327
4328         return 1;
4329 }
4330
4331 #ifdef CONFIG_SND_HDA_POWER_SAVE
4332 static const struct hda_amp_list vt1812_loopbacks[] = {
4333         { 0x21, HDA_INPUT, 0 },
4334         { 0x21, HDA_INPUT, 1 },
4335         { 0x21, HDA_INPUT, 2 },
4336         { } /* end */
4337 };
4338 #endif
4339
4340 static void set_widgets_power_state_vt1812(struct hda_codec *codec)
4341 {
4342         struct via_spec *spec = codec->spec;
4343         int imux_is_smixer =
4344         snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
4345         unsigned int parm;
4346         unsigned int present;
4347         /* MUX10 (1eh) = stereo mixer */
4348         imux_is_smixer =
4349         snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
4350         /* inputs */
4351         /* PW 5/6/7 (29h/2ah/2bh) */
4352         parm = AC_PWRST_D3;
4353         set_pin_power_state(codec, 0x29, &parm);
4354         set_pin_power_state(codec, 0x2a, &parm);
4355         set_pin_power_state(codec, 0x2b, &parm);
4356         parm = AC_PWRST_D0;
4357         /* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
4358         snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
4359         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
4360         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
4361         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
4362
4363         /* outputs */
4364         /* AOW0 (8h)*/
4365         snd_hda_codec_write(codec, 0x8, 0,
4366                             AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
4367
4368         /* PW4 (28h), MW4 (18h), MUX4(38h) */
4369         parm = AC_PWRST_D3;
4370         set_pin_power_state(codec, 0x28, &parm);
4371         snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
4372         snd_hda_codec_write(codec, 0x38, 0, AC_VERB_SET_POWER_STATE, parm);
4373
4374         /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
4375         parm = AC_PWRST_D3;
4376         set_pin_power_state(codec, 0x25, &parm);
4377         snd_hda_codec_write(codec, 0x15, 0, AC_VERB_SET_POWER_STATE, parm);
4378         snd_hda_codec_write(codec, 0x35, 0, AC_VERB_SET_POWER_STATE, parm);
4379         if (spec->hp_independent_mode)
4380                 snd_hda_codec_write(codec, 0x9, 0,
4381                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
4382
4383         /* Internal Speaker */
4384         /* PW0 (24h), MW0(14h), MUX0(34h) */
4385         present = snd_hda_jack_detect(codec, 0x25);
4386
4387         parm = AC_PWRST_D3;
4388         set_pin_power_state(codec, 0x24, &parm);
4389         if (present) {
4390                 snd_hda_codec_write(codec, 0x14, 0,
4391                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
4392                 snd_hda_codec_write(codec, 0x34, 0,
4393                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
4394         } else {
4395                 snd_hda_codec_write(codec, 0x14, 0,
4396                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
4397                 snd_hda_codec_write(codec, 0x34, 0,
4398                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
4399         }
4400
4401
4402         /* Mono Out */
4403         /* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
4404         present = snd_hda_jack_detect(codec, 0x28);
4405
4406         parm = AC_PWRST_D3;
4407         set_pin_power_state(codec, 0x31, &parm);
4408         if (present) {
4409                 snd_hda_codec_write(codec, 0x1c, 0,
4410                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
4411                 snd_hda_codec_write(codec, 0x3c, 0,
4412                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
4413                 snd_hda_codec_write(codec, 0x3e, 0,
4414                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
4415         } else {
4416                 snd_hda_codec_write(codec, 0x1c, 0,
4417                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
4418                 snd_hda_codec_write(codec, 0x3c, 0,
4419                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
4420                 snd_hda_codec_write(codec, 0x3e, 0,
4421                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
4422         }
4423
4424         /* PW15 (33h), MW15 (1dh), MUX15(3dh) */
4425         parm = AC_PWRST_D3;
4426         set_pin_power_state(codec, 0x33, &parm);
4427         snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
4428         snd_hda_codec_write(codec, 0x3d, 0, AC_VERB_SET_POWER_STATE, parm);
4429
4430 }
4431
4432 /* patch for vt1812 */
4433 static int patch_vt1812(struct hda_codec *codec)
4434 {
4435         struct via_spec *spec;
4436         int err;
4437
4438         /* create a codec specific record */
4439         spec = via_new_spec(codec);
4440         if (spec == NULL)
4441                 return -ENOMEM;
4442
4443         spec->aa_mix_nid = 0x21;
4444         override_mic_boost(codec, 0x2b, 0, 3, 40);
4445         override_mic_boost(codec, 0x29, 0, 3, 40);
4446
4447         /* automatic parse from the BIOS config */
4448         err = vt1812_parse_auto_config(codec);
4449         if (err < 0) {
4450                 via_free(codec);
4451                 return err;
4452         }
4453
4454         spec->init_verbs[spec->num_iverbs++]  = vt1812_volume_init_verbs;
4455         spec->init_verbs[spec->num_iverbs++] = vt1812_uniwill_init_verbs;
4456
4457         codec->patch_ops = via_patch_ops;
4458
4459         codec->patch_ops.init = via_auto_init;
4460         codec->patch_ops.unsol_event = via_unsol_event;
4461
4462 #ifdef CONFIG_SND_HDA_POWER_SAVE
4463         spec->loopback.amplist = vt1812_loopbacks;
4464 #endif
4465
4466         spec->set_widgets_power_state =  set_widgets_power_state_vt1812;
4467         return 0;
4468 }
4469
4470 /*
4471  * patch entries
4472  */
4473 static const struct hda_codec_preset snd_hda_preset_via[] = {
4474         { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
4475         { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
4476         { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
4477         { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
4478         { .id = 0x1106e710, .name = "VT1709 10-Ch",
4479           .patch = patch_vt1709_10ch},
4480         { .id = 0x1106e711, .name = "VT1709 10-Ch",
4481           .patch = patch_vt1709_10ch},
4482         { .id = 0x1106e712, .name = "VT1709 10-Ch",
4483           .patch = patch_vt1709_10ch},
4484         { .id = 0x1106e713, .name = "VT1709 10-Ch",
4485           .patch = patch_vt1709_10ch},
4486         { .id = 0x1106e714, .name = "VT1709 6-Ch",
4487           .patch = patch_vt1709_6ch},
4488         { .id = 0x1106e715, .name = "VT1709 6-Ch",
4489           .patch = patch_vt1709_6ch},
4490         { .id = 0x1106e716, .name = "VT1709 6-Ch",
4491           .patch = patch_vt1709_6ch},
4492         { .id = 0x1106e717, .name = "VT1709 6-Ch",
4493           .patch = patch_vt1709_6ch},
4494         { .id = 0x1106e720, .name = "VT1708B 8-Ch",
4495           .patch = patch_vt1708B_8ch},
4496         { .id = 0x1106e721, .name = "VT1708B 8-Ch",
4497           .patch = patch_vt1708B_8ch},
4498         { .id = 0x1106e722, .name = "VT1708B 8-Ch",
4499           .patch = patch_vt1708B_8ch},
4500         { .id = 0x1106e723, .name = "VT1708B 8-Ch",
4501           .patch = patch_vt1708B_8ch},
4502         { .id = 0x1106e724, .name = "VT1708B 4-Ch",
4503           .patch = patch_vt1708B_4ch},
4504         { .id = 0x1106e725, .name = "VT1708B 4-Ch",
4505           .patch = patch_vt1708B_4ch},
4506         { .id = 0x1106e726, .name = "VT1708B 4-Ch",
4507           .patch = patch_vt1708B_4ch},
4508         { .id = 0x1106e727, .name = "VT1708B 4-Ch",
4509           .patch = patch_vt1708B_4ch},
4510         { .id = 0x11060397, .name = "VT1708S",
4511           .patch = patch_vt1708S},
4512         { .id = 0x11061397, .name = "VT1708S",
4513           .patch = patch_vt1708S},
4514         { .id = 0x11062397, .name = "VT1708S",
4515           .patch = patch_vt1708S},
4516         { .id = 0x11063397, .name = "VT1708S",
4517           .patch = patch_vt1708S},
4518         { .id = 0x11064397, .name = "VT1705",
4519           .patch = patch_vt1708S},
4520         { .id = 0x11065397, .name = "VT1708S",
4521           .patch = patch_vt1708S},
4522         { .id = 0x11066397, .name = "VT1708S",
4523           .patch = patch_vt1708S},
4524         { .id = 0x11067397, .name = "VT1708S",
4525           .patch = patch_vt1708S},
4526         { .id = 0x11060398, .name = "VT1702",
4527           .patch = patch_vt1702},
4528         { .id = 0x11061398, .name = "VT1702",
4529           .patch = patch_vt1702},
4530         { .id = 0x11062398, .name = "VT1702",
4531           .patch = patch_vt1702},
4532         { .id = 0x11063398, .name = "VT1702",
4533           .patch = patch_vt1702},
4534         { .id = 0x11064398, .name = "VT1702",
4535           .patch = patch_vt1702},
4536         { .id = 0x11065398, .name = "VT1702",
4537           .patch = patch_vt1702},
4538         { .id = 0x11066398, .name = "VT1702",
4539           .patch = patch_vt1702},
4540         { .id = 0x11067398, .name = "VT1702",
4541           .patch = patch_vt1702},
4542         { .id = 0x11060428, .name = "VT1718S",
4543           .patch = patch_vt1718S},
4544         { .id = 0x11064428, .name = "VT1718S",
4545           .patch = patch_vt1718S},
4546         { .id = 0x11060441, .name = "VT2020",
4547           .patch = patch_vt1718S},
4548         { .id = 0x11064441, .name = "VT1828S",
4549           .patch = patch_vt1718S},
4550         { .id = 0x11060433, .name = "VT1716S",
4551           .patch = patch_vt1716S},
4552         { .id = 0x1106a721, .name = "VT1716S",
4553           .patch = patch_vt1716S},
4554         { .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
4555         { .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
4556         { .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
4557         { .id = 0x11060440, .name = "VT1818S",
4558           .patch = patch_vt1708S},
4559         { .id = 0x11060446, .name = "VT1802",
4560                 .patch = patch_vt2002P},
4561         { .id = 0x11068446, .name = "VT1802",
4562                 .patch = patch_vt2002P},
4563         {} /* terminator */
4564 };
4565
4566 MODULE_ALIAS("snd-hda-codec-id:1106*");
4567
4568 static struct hda_codec_preset_list via_list = {
4569         .preset = snd_hda_preset_via,
4570         .owner = THIS_MODULE,
4571 };
4572
4573 MODULE_LICENSE("GPL");
4574 MODULE_DESCRIPTION("VIA HD-audio codec");
4575
4576 static int __init patch_via_init(void)
4577 {
4578         return snd_hda_add_codec_preset(&via_list);
4579 }
4580
4581 static void __exit patch_via_exit(void)
4582 {
4583         snd_hda_delete_codec_preset(&via_list);
4584 }
4585
4586 module_init(patch_via_init)
4587 module_exit(patch_via_exit)