]> git.karo-electronics.de Git - mv-sheeva.git/blob - sound/pci/hda/patch_via.c
ALSA: hda - Remove redundant VT1709 and VT1708B codes
[mv-sheeva.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 /* Pin Widget NID */
58 #define VT1708_HP_PIN_NID       0x20
59 #define VT1708_CD_PIN_NID       0x24
60
61 enum VIA_HDA_CODEC {
62         UNKNOWN = -1,
63         VT1708,
64         VT1709_10CH,
65         VT1709_6CH,
66         VT1708B_8CH,
67         VT1708B_4CH,
68         VT1708S,
69         VT1708BCE,
70         VT1702,
71         VT1718S,
72         VT1716S,
73         VT2002P,
74         VT1812,
75         VT1802,
76         CODEC_TYPES,
77 };
78
79 #define VT2002P_COMPATIBLE(spec) \
80         ((spec)->codec_type == VT2002P ||\
81          (spec)->codec_type == VT1812 ||\
82          (spec)->codec_type == VT1802)
83
84 #define MAX_NID_PATH_DEPTH      5
85
86 /* output-path: DAC -> ... -> pin
87  * idx[] contains the source index number of the next widget;
88  * e.g. idx[0] is the index of the DAC selected by path[1] widget
89  * multi[] indicates whether it's a selector widget with multi-connectors
90  * (i.e. the connection selection is mandatory)
91  * vol_ctl and mute_ctl contains the NIDs for the assigned mixers
92  */
93 struct nid_path {
94         int depth;
95         hda_nid_t path[MAX_NID_PATH_DEPTH];
96         unsigned char idx[MAX_NID_PATH_DEPTH];
97         unsigned char multi[MAX_NID_PATH_DEPTH];
98         unsigned int vol_ctl;
99         unsigned int mute_ctl;
100 };
101
102 struct via_spec {
103         /* codec parameterization */
104         const struct snd_kcontrol_new *mixers[6];
105         unsigned int num_mixers;
106
107         const struct hda_verb *init_verbs[5];
108         unsigned int num_iverbs;
109
110         char stream_name_analog[32];
111         char stream_name_hp[32];
112         const struct hda_pcm_stream *stream_analog_playback;
113         const struct hda_pcm_stream *stream_analog_capture;
114
115         char stream_name_digital[32];
116         const struct hda_pcm_stream *stream_digital_playback;
117         const struct hda_pcm_stream *stream_digital_capture;
118
119         /* playback */
120         struct hda_multi_out multiout;
121         hda_nid_t slave_dig_outs[2];
122         hda_nid_t hp_dac_nid;
123         int num_active_streams;
124
125         struct nid_path out_path[4];
126         struct nid_path hp_path;
127         struct nid_path hp_dep_path;
128         struct nid_path speaker_path;
129
130         /* capture */
131         unsigned int num_adc_nids;
132         hda_nid_t adc_nids[3];
133         hda_nid_t mux_nids[3];
134         hda_nid_t aa_mix_nid;
135         hda_nid_t dig_in_nid;
136
137         /* capture source */
138         const struct hda_input_mux *input_mux;
139         unsigned int cur_mux[3];
140
141         /* PCM information */
142         struct hda_pcm pcm_rec[3];
143
144         /* dynamic controls, init_verbs and input_mux */
145         struct auto_pin_cfg autocfg;
146         struct snd_array kctls;
147         struct hda_input_mux private_imux[2];
148         hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
149
150         /* HP mode source */
151         unsigned int hp_independent_mode;
152         unsigned int dmic_enabled;
153         unsigned int no_pin_power_ctl;
154         enum VIA_HDA_CODEC codec_type;
155
156         /* smart51 setup */
157         unsigned int smart51_nums;
158         hda_nid_t smart51_pins[2];
159         int smart51_idxs[2];
160         const char *smart51_labels[2];
161         unsigned int smart51_enabled;
162
163         /* work to check hp jack state */
164         struct hda_codec *codec;
165         struct delayed_work vt1708_hp_work;
166         int vt1708_jack_detect;
167         int vt1708_hp_present;
168
169         void (*set_widgets_power_state)(struct hda_codec *codec);
170
171         struct hda_loopback_check loopback;
172         int num_loopbacks;
173         struct hda_amp_list loopback_list[8];
174 };
175
176 static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
177 static struct via_spec * via_new_spec(struct hda_codec *codec)
178 {
179         struct via_spec *spec;
180
181         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
182         if (spec == NULL)
183                 return NULL;
184
185         codec->spec = spec;
186         spec->codec = codec;
187         spec->codec_type = get_codec_type(codec);
188         /* VT1708BCE & VT1708S are almost same */
189         if (spec->codec_type == VT1708BCE)
190                 spec->codec_type = VT1708S;
191         return spec;
192 }
193
194 static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
195 {
196         u32 vendor_id = codec->vendor_id;
197         u16 ven_id = vendor_id >> 16;
198         u16 dev_id = vendor_id & 0xffff;
199         enum VIA_HDA_CODEC codec_type;
200
201         /* get codec type */
202         if (ven_id != 0x1106)
203                 codec_type = UNKNOWN;
204         else if (dev_id >= 0x1708 && dev_id <= 0x170b)
205                 codec_type = VT1708;
206         else if (dev_id >= 0xe710 && dev_id <= 0xe713)
207                 codec_type = VT1709_10CH;
208         else if (dev_id >= 0xe714 && dev_id <= 0xe717)
209                 codec_type = VT1709_6CH;
210         else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
211                 codec_type = VT1708B_8CH;
212                 if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
213                         codec_type = VT1708BCE;
214         } else if (dev_id >= 0xe724 && dev_id <= 0xe727)
215                 codec_type = VT1708B_4CH;
216         else if ((dev_id & 0xfff) == 0x397
217                  && (dev_id >> 12) < 8)
218                 codec_type = VT1708S;
219         else if ((dev_id & 0xfff) == 0x398
220                  && (dev_id >> 12) < 8)
221                 codec_type = VT1702;
222         else if ((dev_id & 0xfff) == 0x428
223                  && (dev_id >> 12) < 8)
224                 codec_type = VT1718S;
225         else if (dev_id == 0x0433 || dev_id == 0xa721)
226                 codec_type = VT1716S;
227         else if (dev_id == 0x0441 || dev_id == 0x4441)
228                 codec_type = VT1718S;
229         else if (dev_id == 0x0438 || dev_id == 0x4438)
230                 codec_type = VT2002P;
231         else if (dev_id == 0x0448)
232                 codec_type = VT1812;
233         else if (dev_id == 0x0440)
234                 codec_type = VT1708S;
235         else if ((dev_id & 0xfff) == 0x446)
236                 codec_type = VT1802;
237         else
238                 codec_type = UNKNOWN;
239         return codec_type;
240 };
241
242 #define VIA_JACK_EVENT          0x20
243 #define VIA_HP_EVENT            0x01
244 #define VIA_GPIO_EVENT          0x02
245 #define VIA_LINE_EVENT          0x03
246
247 enum {
248         VIA_CTL_WIDGET_VOL,
249         VIA_CTL_WIDGET_MUTE,
250         VIA_CTL_WIDGET_ANALOG_MUTE,
251 };
252
253 static void analog_low_current_mode(struct hda_codec *codec);
254 static bool is_aa_path_mute(struct hda_codec *codec);
255
256 static void vt1708_start_hp_work(struct via_spec *spec)
257 {
258         if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
259                 return;
260         snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
261                             !spec->vt1708_jack_detect);
262         if (!delayed_work_pending(&spec->vt1708_hp_work))
263                 schedule_delayed_work(&spec->vt1708_hp_work,
264                                       msecs_to_jiffies(100));
265 }
266
267 static void vt1708_stop_hp_work(struct via_spec *spec)
268 {
269         if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
270                 return;
271         if (snd_hda_get_bool_hint(spec->codec, "analog_loopback_hp_detect") == 1
272             && !is_aa_path_mute(spec->codec))
273                 return;
274         snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
275                             !spec->vt1708_jack_detect);
276         cancel_delayed_work_sync(&spec->vt1708_hp_work);
277 }
278
279 static void set_widgets_power_state(struct hda_codec *codec)
280 {
281         struct via_spec *spec = codec->spec;
282         if (spec->set_widgets_power_state)
283                 spec->set_widgets_power_state(codec);
284 }
285
286 static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
287                                    struct snd_ctl_elem_value *ucontrol)
288 {
289         int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
290         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
291
292         set_widgets_power_state(codec);
293         analog_low_current_mode(snd_kcontrol_chip(kcontrol));
294         if (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1) {
295                 if (is_aa_path_mute(codec))
296                         vt1708_start_hp_work(codec->spec);
297                 else
298                         vt1708_stop_hp_work(codec->spec);
299         }
300         return change;
301 }
302
303 /* modify .put = snd_hda_mixer_amp_switch_put */
304 #define ANALOG_INPUT_MUTE                                               \
305         {               .iface = SNDRV_CTL_ELEM_IFACE_MIXER,            \
306                         .name = NULL,                                   \
307                         .index = 0,                                     \
308                         .info = snd_hda_mixer_amp_switch_info,          \
309                         .get = snd_hda_mixer_amp_switch_get,            \
310                         .put = analog_input_switch_put,                 \
311                         .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
312
313 static const struct snd_kcontrol_new via_control_templates[] = {
314         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
315         HDA_CODEC_MUTE(NULL, 0, 0, 0),
316         ANALOG_INPUT_MUTE,
317 };
318
319
320 /* add dynamic controls */
321 static struct snd_kcontrol_new *__via_clone_ctl(struct via_spec *spec,
322                                 const struct snd_kcontrol_new *tmpl,
323                                 const char *name)
324 {
325         struct snd_kcontrol_new *knew;
326
327         snd_array_init(&spec->kctls, sizeof(*knew), 32);
328         knew = snd_array_new(&spec->kctls);
329         if (!knew)
330                 return NULL;
331         *knew = *tmpl;
332         if (!name)
333                 name = tmpl->name;
334         if (name) {
335                 knew->name = kstrdup(name, GFP_KERNEL);
336                 if (!knew->name)
337                         return NULL;
338         }
339         return knew;
340 }
341
342 static int __via_add_control(struct via_spec *spec, int type, const char *name,
343                              int idx, unsigned long val)
344 {
345         struct snd_kcontrol_new *knew;
346
347         knew = __via_clone_ctl(spec, &via_control_templates[type], name);
348         if (!knew)
349                 return -ENOMEM;
350         knew->index = idx;
351         if (get_amp_nid_(val))
352                 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
353         knew->private_value = val;
354         return 0;
355 }
356
357 #define via_add_control(spec, type, name, val) \
358         __via_add_control(spec, type, name, 0, val)
359
360 #define via_clone_control(spec, tmpl) __via_clone_ctl(spec, tmpl, NULL)
361
362 static void via_free_kctls(struct hda_codec *codec)
363 {
364         struct via_spec *spec = codec->spec;
365
366         if (spec->kctls.list) {
367                 struct snd_kcontrol_new *kctl = spec->kctls.list;
368                 int i;
369                 for (i = 0; i < spec->kctls.used; i++)
370                         kfree(kctl[i].name);
371         }
372         snd_array_free(&spec->kctls);
373 }
374
375 /* create input playback/capture controls for the given pin */
376 static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
377                                 int type_idx, int idx, int mix_nid)
378 {
379         char name[32];
380         int err;
381
382         sprintf(name, "%s Playback Volume", ctlname);
383         err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
384                               HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
385         if (err < 0)
386                 return err;
387         sprintf(name, "%s Playback Switch", ctlname);
388         err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
389                               HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
390         if (err < 0)
391                 return err;
392         return 0;
393 }
394
395 /* return the index of the given widget nid as the source of mux;
396  * return -1 if not found;
397  * if num_conns is non-NULL, set the total number of connections
398  */
399 static int __get_connection_index(struct hda_codec *codec, hda_nid_t mux,
400                                   hda_nid_t nid, int *num_conns)
401 {
402         hda_nid_t conn[HDA_MAX_NUM_INPUTS];
403         int i, nums;
404
405         nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
406         if (num_conns)
407                 *num_conns = nums;
408         for (i = 0; i < nums; i++)
409                 if (conn[i] == nid)
410                         return i;
411         return -1;
412 }
413
414 #define get_connection_index(codec, mux, nid) \
415         __get_connection_index(codec, mux, nid, NULL)
416
417 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
418                            unsigned int mask)
419 {
420         unsigned int caps;
421         if (!nid)
422                 return false;
423         caps = get_wcaps(codec, nid);
424         if (dir == HDA_INPUT)
425                 caps &= AC_WCAP_IN_AMP;
426         else
427                 caps &= AC_WCAP_OUT_AMP;
428         if (!caps)
429                 return false;
430         if (query_amp_caps(codec, nid, dir) & mask)
431                 return true;
432         return false;
433 }
434
435 #define have_mute(codec, nid, dir) \
436         check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
437
438 /* enable/disable the output-route */
439 static void activate_output_path(struct hda_codec *codec, struct nid_path *path,
440                                  bool enable, bool force)
441 {
442         int i;
443         for (i = 0; i < path->depth; i++) {
444                 hda_nid_t src, dst;
445                 int idx = path->idx[i];
446                 src = path->path[i];                    
447                 if (i < path->depth - 1)
448                         dst = path->path[i + 1];
449                 else
450                         dst = 0;
451                 if (enable && path->multi[i])
452                         snd_hda_codec_write(codec, dst, 0,
453                                             AC_VERB_SET_CONNECT_SEL, idx);
454                 if (have_mute(codec, dst, HDA_INPUT)) {
455                         int val = enable ? AMP_IN_UNMUTE(idx) :
456                                 AMP_IN_MUTE(idx);
457                         snd_hda_codec_write(codec, dst, 0,
458                                             AC_VERB_SET_AMP_GAIN_MUTE, val);
459                 }
460                 if (!force && (src == path->vol_ctl || src == path->mute_ctl))
461                         continue;
462                 if (have_mute(codec, src, HDA_OUTPUT)) {
463                         int val = enable ? AMP_OUT_UNMUTE : AMP_OUT_MUTE;
464                         snd_hda_codec_write(codec, src, 0,
465                                             AC_VERB_SET_AMP_GAIN_MUTE, val);
466                 }
467         }
468 }
469
470 /* set the given pin as output */
471 static void init_output_pin(struct hda_codec *codec, hda_nid_t pin,
472                             int pin_type)
473 {
474         if (!pin)
475                 return;
476         snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
477                             pin_type);
478         if (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD)
479                 snd_hda_codec_write(codec, pin, 0,
480                                     AC_VERB_SET_EAPD_BTLENABLE, 0x02);
481 }
482
483 static void via_auto_init_output(struct hda_codec *codec,
484                                  struct nid_path *path, int pin_type,
485                                  bool force)
486 {
487         struct via_spec *spec = codec->spec;
488         unsigned int caps;
489         hda_nid_t pin, nid;
490         int i, idx;
491
492         if (!path->depth)
493                 return;
494         pin = path->path[path->depth - 1];
495
496         init_output_pin(codec, pin, pin_type);
497         caps = query_amp_caps(codec, pin, HDA_OUTPUT);
498         if (caps & AC_AMPCAP_MUTE) {
499                 unsigned int val;
500                 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
501                 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
502                                     AMP_OUT_MUTE | val);
503         }
504
505         activate_output_path(codec, path, true, force);
506
507         /* initialize the AA-path */
508         if (!spec->aa_mix_nid)
509                 return;
510         for (i = path->depth - 1; i > 0; i--) {
511                 nid = path->path[i];
512                 idx = get_connection_index(codec, nid, spec->aa_mix_nid);
513                 if (idx >= 0) {
514                         if (have_mute(codec, nid, HDA_INPUT))
515                                 snd_hda_codec_write(codec, nid, 0,
516                                                     AC_VERB_SET_AMP_GAIN_MUTE,
517                                                     AMP_IN_UNMUTE(idx));
518                         break;
519                 }
520         }
521 }
522
523 static void via_auto_init_multi_out(struct hda_codec *codec)
524 {
525         struct via_spec *spec = codec->spec;
526         int i;
527
528         for (i = 0; i < spec->autocfg.line_outs + spec->smart51_nums; i++)
529                 via_auto_init_output(codec, &spec->out_path[i], PIN_OUT, true);
530 }
531
532 static void via_auto_init_hp_out(struct hda_codec *codec)
533 {
534         struct via_spec *spec = codec->spec;
535
536         if (!spec->hp_dac_nid) {
537                 via_auto_init_output(codec, &spec->hp_dep_path, PIN_HP, true);
538                 return;
539         }
540         if (spec->hp_independent_mode) {
541                 activate_output_path(codec, &spec->hp_dep_path, false, false);
542                 via_auto_init_output(codec, &spec->hp_path, PIN_HP, true);
543         } else {
544                 activate_output_path(codec, &spec->hp_path, false, false);
545                 via_auto_init_output(codec, &spec->hp_dep_path, PIN_HP, true);
546         }
547 }
548
549 static void via_auto_init_speaker_out(struct hda_codec *codec)
550 {
551         struct via_spec *spec = codec->spec;
552
553         if (spec->autocfg.speaker_outs)
554                 via_auto_init_output(codec, &spec->speaker_path, PIN_OUT, true);
555 }
556
557 static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin);
558
559 static void via_auto_init_analog_input(struct hda_codec *codec)
560 {
561         struct via_spec *spec = codec->spec;
562         const struct auto_pin_cfg *cfg = &spec->autocfg;
563         hda_nid_t conn[HDA_MAX_CONNECTIONS];
564         unsigned int ctl;
565         int i, num_conns;
566
567         /* init ADCs */
568         for (i = 0; i < spec->num_adc_nids; i++) {
569                 snd_hda_codec_write(codec, spec->adc_nids[i], 0,
570                                     AC_VERB_SET_AMP_GAIN_MUTE,
571                                     AMP_IN_UNMUTE(0));
572         }
573
574         /* init pins */
575         for (i = 0; i < cfg->num_inputs; i++) {
576                 hda_nid_t nid = cfg->inputs[i].pin;
577                 if (spec->smart51_enabled && is_smart51_pins(codec, nid))
578                         ctl = PIN_OUT;
579                 else if (cfg->inputs[i].type == AUTO_PIN_MIC)
580                         ctl = PIN_VREF50;
581                 else
582                         ctl = PIN_IN;
583                 snd_hda_codec_write(codec, nid, 0,
584                                     AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
585         }
586
587         /* init input-src */
588         for (i = 0; i < spec->num_adc_nids; i++) {
589                 const struct hda_input_mux *imux = spec->input_mux;
590                 if (!imux || !spec->mux_nids[i])
591                         continue;
592                 snd_hda_codec_write(codec, spec->mux_nids[i], 0,
593                                     AC_VERB_SET_CONNECT_SEL,
594                                     imux->items[spec->cur_mux[i]].index);
595         }
596
597         /* init aa-mixer */
598         if (!spec->aa_mix_nid)
599                 return;
600         num_conns = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
601                                             ARRAY_SIZE(conn));
602         for (i = 0; i < num_conns; i++) {
603                 unsigned int caps = get_wcaps(codec, conn[i]);
604                 if (get_wcaps_type(caps) == AC_WID_PIN)
605                         snd_hda_codec_write(codec, spec->aa_mix_nid, 0,
606                                             AC_VERB_SET_AMP_GAIN_MUTE,
607                                             AMP_IN_MUTE(i));
608         }
609 }
610
611 static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
612                                 unsigned int *affected_parm)
613 {
614         unsigned parm;
615         unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
616         unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
617                 >> AC_DEFCFG_MISC_SHIFT
618                 & AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
619         struct via_spec *spec = codec->spec;
620         unsigned present = 0;
621
622         no_presence |= spec->no_pin_power_ctl;
623         if (!no_presence)
624                 present = snd_hda_jack_detect(codec, nid);
625         if ((spec->smart51_enabled && is_smart51_pins(codec, nid))
626             || ((no_presence || present)
627                 && get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
628                 *affected_parm = AC_PWRST_D0; /* if it's connected */
629                 parm = AC_PWRST_D0;
630         } else
631                 parm = AC_PWRST_D3;
632
633         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
634 }
635
636 static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
637                                   struct snd_ctl_elem_info *uinfo)
638 {
639         static const char * const texts[] = {
640                 "Disabled", "Enabled"
641         };
642
643         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
644         uinfo->count = 1;
645         uinfo->value.enumerated.items = 2;
646         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
647                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
648         strcpy(uinfo->value.enumerated.name,
649                texts[uinfo->value.enumerated.item]);
650         return 0;
651 }
652
653 static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
654                                  struct snd_ctl_elem_value *ucontrol)
655 {
656         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
657         struct via_spec *spec = codec->spec;
658         ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
659         return 0;
660 }
661
662 static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
663                                  struct snd_ctl_elem_value *ucontrol)
664 {
665         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
666         struct via_spec *spec = codec->spec;
667         unsigned int val = !ucontrol->value.enumerated.item[0];
668
669         if (val == spec->no_pin_power_ctl)
670                 return 0;
671         spec->no_pin_power_ctl = val;
672         set_widgets_power_state(codec);
673         return 1;
674 }
675
676 static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
677         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
678         .name = "Dynamic Power-Control",
679         .info = via_pin_power_ctl_info,
680         .get = via_pin_power_ctl_get,
681         .put = via_pin_power_ctl_put,
682 };
683
684
685 /*
686  * input MUX handling
687  */
688 static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
689                              struct snd_ctl_elem_info *uinfo)
690 {
691         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
692         struct via_spec *spec = codec->spec;
693         return snd_hda_input_mux_info(spec->input_mux, uinfo);
694 }
695
696 static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
697                             struct snd_ctl_elem_value *ucontrol)
698 {
699         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
700         struct via_spec *spec = codec->spec;
701         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
702
703         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
704         return 0;
705 }
706
707 static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
708                             struct snd_ctl_elem_value *ucontrol)
709 {
710         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
711         struct via_spec *spec = codec->spec;
712         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
713         int ret;
714
715         if (!spec->mux_nids[adc_idx])
716                 return -EINVAL;
717         /* switch to D0 beofre change index */
718         if (snd_hda_codec_read(codec, spec->mux_nids[adc_idx], 0,
719                                AC_VERB_GET_POWER_STATE, 0x00) != AC_PWRST_D0)
720                 snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
721                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
722
723         ret = snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
724                                      spec->mux_nids[adc_idx],
725                                      &spec->cur_mux[adc_idx]);
726         /* update jack power state */
727         set_widgets_power_state(codec);
728
729         return ret;
730 }
731
732 static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
733                                    struct snd_ctl_elem_info *uinfo)
734 {
735         static const char * const texts[] = { "OFF", "ON" };
736
737         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
738         uinfo->count = 1;
739         uinfo->value.enumerated.items = 2;
740         if (uinfo->value.enumerated.item >= 2)
741                 uinfo->value.enumerated.item = 1;
742         strcpy(uinfo->value.enumerated.name,
743                texts[uinfo->value.enumerated.item]);
744         return 0;
745 }
746
747 static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
748                                   struct snd_ctl_elem_value *ucontrol)
749 {
750         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
751         struct via_spec *spec = codec->spec;
752
753         ucontrol->value.enumerated.item[0] = spec->hp_independent_mode;
754         return 0;
755 }
756
757 static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
758                                   struct snd_ctl_elem_value *ucontrol)
759 {
760         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
761         struct via_spec *spec = codec->spec;
762
763         spec->hp_independent_mode = !!ucontrol->value.enumerated.item[0];
764         if (spec->hp_independent_mode) {
765                 activate_output_path(codec, &spec->hp_dep_path, false, false);
766                 activate_output_path(codec, &spec->hp_path, true, false);
767         } else {
768                 activate_output_path(codec, &spec->hp_path, false, false);
769                 activate_output_path(codec, &spec->hp_dep_path, true, false);
770         }
771
772         /* update jack power state */
773         set_widgets_power_state(codec);
774         return 0;
775 }
776
777 static const struct snd_kcontrol_new via_hp_mixer = {
778         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
779         .name = "Independent HP",
780         .info = via_independent_hp_info,
781         .get = via_independent_hp_get,
782         .put = via_independent_hp_put,
783 };
784
785 static int via_hp_build(struct hda_codec *codec)
786 {
787         struct via_spec *spec = codec->spec;
788         struct snd_kcontrol_new *knew;
789         hda_nid_t nid;
790
791         nid = spec->autocfg.hp_pins[0];
792         knew = via_clone_control(spec, &via_hp_mixer);
793         if (knew == NULL)
794                 return -ENOMEM;
795
796         knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
797
798         return 0;
799 }
800
801 static void notify_aa_path_ctls(struct hda_codec *codec)
802 {
803         struct via_spec *spec = codec->spec;
804         int i;
805
806         for (i = 0; i < spec->smart51_nums; i++) {
807                 struct snd_kcontrol *ctl;
808                 struct snd_ctl_elem_id id;
809                 memset(&id, 0, sizeof(id));
810                 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
811                 sprintf(id.name, "%s Playback Volume", spec->smart51_labels[i]);
812                 ctl = snd_hda_find_mixer_ctl(codec, id.name);
813                 if (ctl)
814                         snd_ctl_notify(codec->bus->card,
815                                         SNDRV_CTL_EVENT_MASK_VALUE,
816                                         &ctl->id);
817         }
818 }
819
820 static void mute_aa_path(struct hda_codec *codec, int mute)
821 {
822         struct via_spec *spec = codec->spec;
823         int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
824         int i;
825
826         /* check AA path's mute status */
827         for (i = 0; i < spec->smart51_nums; i++) {
828                 if (spec->smart51_idxs[i] < 0)
829                         continue;
830                 snd_hda_codec_amp_stereo(codec, spec->aa_mix_nid,
831                                          HDA_INPUT, spec->smart51_idxs[i],
832                                          HDA_AMP_MUTE, val);
833         }
834 }
835
836 static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
837 {
838         struct via_spec *spec = codec->spec;
839         int i;
840
841         for (i = 0; i < spec->smart51_nums; i++)
842                 if (spec->smart51_pins[i] == pin)
843                         return true;
844         return false;
845 }
846
847 static int via_smart51_info(struct snd_kcontrol *kcontrol,
848                             struct snd_ctl_elem_info *uinfo)
849 {
850         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
851         uinfo->count = 1;
852         uinfo->value.integer.min = 0;
853         uinfo->value.integer.max = 1;
854         return 0;
855 }
856
857 static int via_smart51_get(struct snd_kcontrol *kcontrol,
858                            struct snd_ctl_elem_value *ucontrol)
859 {
860         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
861         struct via_spec *spec = codec->spec;
862         int on = 1;
863         int i;
864
865         for (i = 0; i < spec->smart51_nums; i++) {
866                 hda_nid_t nid = spec->smart51_pins[i];
867                 unsigned int ctl;
868                 ctl = snd_hda_codec_read(codec, nid, 0,
869                                          AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
870                 if ((ctl & AC_PINCTL_IN_EN) && !(ctl & AC_PINCTL_OUT_EN))
871                         on = 0;
872         }
873         *ucontrol->value.integer.value = on;
874         return 0;
875 }
876
877 static int via_smart51_put(struct snd_kcontrol *kcontrol,
878                            struct snd_ctl_elem_value *ucontrol)
879 {
880         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
881         struct via_spec *spec = codec->spec;
882         int out_in = *ucontrol->value.integer.value
883                 ? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
884         int i;
885
886         for (i = 0; i < spec->smart51_nums; i++) {
887                 hda_nid_t nid = spec->smart51_pins[i];
888                 unsigned int parm;
889
890                 parm = snd_hda_codec_read(codec, nid, 0,
891                                           AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
892                 parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
893                 parm |= out_in;
894                 snd_hda_codec_write(codec, nid, 0,
895                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
896                                     parm);
897                 if (out_in == AC_PINCTL_OUT_EN) {
898                         mute_aa_path(codec, 1);
899                         notify_aa_path_ctls(codec);
900                 }
901         }
902         spec->smart51_enabled = *ucontrol->value.integer.value;
903         set_widgets_power_state(codec);
904         return 1;
905 }
906
907 static const struct snd_kcontrol_new via_smart51_mixer = {
908         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
909         .name = "Smart 5.1",
910         .count = 1,
911         .info = via_smart51_info,
912         .get = via_smart51_get,
913         .put = via_smart51_put,
914 };
915
916 static int via_smart51_build(struct hda_codec *codec)
917 {
918         struct via_spec *spec = codec->spec;
919
920         if (!spec->smart51_nums)
921                 return 0;
922         if (!via_clone_control(spec, &via_smart51_mixer))
923                 return -ENOMEM;
924         return 0;
925 }
926
927 /* check AA path's mute status */
928 static bool is_aa_path_mute(struct hda_codec *codec)
929 {
930         struct via_spec *spec = codec->spec;
931         const struct hda_amp_list *p;
932         int i, ch, v;
933
934         for (i = 0; i < spec->num_loopbacks; i++) {
935                 p = &spec->loopback_list[i];
936                 for (ch = 0; ch < 2; ch++) {
937                         v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
938                                                    p->idx);
939                         if (!(v & HDA_AMP_MUTE) && v > 0)
940                                 return false;
941                 }
942         }
943         return true;
944 }
945
946 /* enter/exit analog low-current mode */
947 static void analog_low_current_mode(struct hda_codec *codec)
948 {
949         struct via_spec *spec = codec->spec;
950         bool enable;
951         unsigned int verb, parm;
952
953         enable = is_aa_path_mute(codec) && (spec->num_active_streams > 0);
954
955         /* decide low current mode's verb & parameter */
956         switch (spec->codec_type) {
957         case VT1708B_8CH:
958         case VT1708B_4CH:
959                 verb = 0xf70;
960                 parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
961                 break;
962         case VT1708S:
963         case VT1718S:
964         case VT1716S:
965                 verb = 0xf73;
966                 parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
967                 break;
968         case VT1702:
969                 verb = 0xf73;
970                 parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
971                 break;
972         case VT2002P:
973         case VT1812:
974         case VT1802:
975                 verb = 0xf93;
976                 parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
977                 break;
978         default:
979                 return;         /* other codecs are not supported */
980         }
981         /* send verb */
982         snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
983 }
984
985 /*
986  * generic initialization of ADC, input mixers and output mixers
987  */
988 static const struct hda_verb vt1708_init_verbs[] = {
989         /* power down jack detect function */
990         {0x1, 0xf81, 0x1},
991         { }
992 };
993
994 static void set_stream_active(struct hda_codec *codec, bool active)
995 {
996         struct via_spec *spec = codec->spec;
997
998         if (active)
999                 spec->num_active_streams++;
1000         else
1001                 spec->num_active_streams--;
1002         analog_low_current_mode(codec);
1003 }
1004
1005 static int via_playback_multi_pcm_open(struct hda_pcm_stream *hinfo,
1006                                  struct hda_codec *codec,
1007                                  struct snd_pcm_substream *substream)
1008 {
1009         struct via_spec *spec = codec->spec;
1010         int err;
1011
1012         if (!spec->hp_independent_mode)
1013                 spec->multiout.hp_nid = spec->hp_dac_nid;
1014         set_stream_active(codec, true);
1015         err = snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
1016                                             hinfo);
1017         if (err < 0) {
1018                 spec->multiout.hp_nid = 0;
1019                 set_stream_active(codec, false);
1020                 return err;
1021         }
1022         return 0;
1023 }
1024
1025 static int via_playback_multi_pcm_close(struct hda_pcm_stream *hinfo,
1026                                   struct hda_codec *codec,
1027                                   struct snd_pcm_substream *substream)
1028 {
1029         struct via_spec *spec = codec->spec;
1030
1031         spec->multiout.hp_nid = 0;
1032         set_stream_active(codec, false);
1033         return 0;
1034 }
1035
1036 static int via_playback_hp_pcm_open(struct hda_pcm_stream *hinfo,
1037                                     struct hda_codec *codec,
1038                                     struct snd_pcm_substream *substream)
1039 {
1040         struct via_spec *spec = codec->spec;
1041
1042         if (snd_BUG_ON(!spec->hp_dac_nid))
1043                 return -EINVAL;
1044         if (!spec->hp_independent_mode || spec->multiout.hp_nid)
1045                 return -EBUSY;
1046         set_stream_active(codec, true);
1047         return 0;
1048 }
1049
1050 static int via_playback_hp_pcm_close(struct hda_pcm_stream *hinfo,
1051                                      struct hda_codec *codec,
1052                                      struct snd_pcm_substream *substream)
1053 {
1054         set_stream_active(codec, false);
1055         return 0;
1056 }
1057
1058 static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
1059                                           struct hda_codec *codec,
1060                                           unsigned int stream_tag,
1061                                           unsigned int format,
1062                                           struct snd_pcm_substream *substream)
1063 {
1064         struct via_spec *spec = codec->spec;
1065
1066         snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
1067                                          format, substream);
1068         vt1708_start_hp_work(spec);
1069         return 0;
1070 }
1071
1072 static int via_playback_hp_pcm_prepare(struct hda_pcm_stream *hinfo,
1073                                        struct hda_codec *codec,
1074                                        unsigned int stream_tag,
1075                                        unsigned int format,
1076                                        struct snd_pcm_substream *substream)
1077 {
1078         struct via_spec *spec = codec->spec;
1079
1080         snd_hda_codec_setup_stream(codec, spec->hp_dac_nid,
1081                                    stream_tag, 0, format);
1082         vt1708_start_hp_work(spec);
1083         return 0;
1084 }
1085
1086 static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
1087                                     struct hda_codec *codec,
1088                                     struct snd_pcm_substream *substream)
1089 {
1090         struct via_spec *spec = codec->spec;
1091
1092         snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
1093         vt1708_stop_hp_work(spec);
1094         return 0;
1095 }
1096
1097 static int via_playback_hp_pcm_cleanup(struct hda_pcm_stream *hinfo,
1098                                        struct hda_codec *codec,
1099                                        struct snd_pcm_substream *substream)
1100 {
1101         struct via_spec *spec = codec->spec;
1102
1103         snd_hda_codec_setup_stream(codec, spec->hp_dac_nid, 0, 0, 0);
1104         vt1708_stop_hp_work(spec);
1105         return 0;
1106 }
1107
1108 /*
1109  * Digital out
1110  */
1111 static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1112                                      struct hda_codec *codec,
1113                                      struct snd_pcm_substream *substream)
1114 {
1115         struct via_spec *spec = codec->spec;
1116         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1117 }
1118
1119 static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1120                                       struct hda_codec *codec,
1121                                       struct snd_pcm_substream *substream)
1122 {
1123         struct via_spec *spec = codec->spec;
1124         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1125 }
1126
1127 static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1128                                         struct hda_codec *codec,
1129                                         unsigned int stream_tag,
1130                                         unsigned int format,
1131                                         struct snd_pcm_substream *substream)
1132 {
1133         struct via_spec *spec = codec->spec;
1134         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1135                                              stream_tag, format, substream);
1136 }
1137
1138 static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1139                                         struct hda_codec *codec,
1140                                         struct snd_pcm_substream *substream)
1141 {
1142         struct via_spec *spec = codec->spec;
1143         snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
1144         return 0;
1145 }
1146
1147 /*
1148  * Analog capture
1149  */
1150 static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1151                                    struct hda_codec *codec,
1152                                    unsigned int stream_tag,
1153                                    unsigned int format,
1154                                    struct snd_pcm_substream *substream)
1155 {
1156         struct via_spec *spec = codec->spec;
1157
1158         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1159                                    stream_tag, 0, format);
1160         return 0;
1161 }
1162
1163 static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1164                                    struct hda_codec *codec,
1165                                    struct snd_pcm_substream *substream)
1166 {
1167         struct via_spec *spec = codec->spec;
1168         snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
1169         return 0;
1170 }
1171
1172 static const struct hda_pcm_stream via_pcm_analog_playback = {
1173         .substreams = 1,
1174         .channels_min = 2,
1175         .channels_max = 8,
1176         /* NID is set in via_build_pcms */
1177         .ops = {
1178                 .open = via_playback_multi_pcm_open,
1179                 .close = via_playback_multi_pcm_close,
1180                 .prepare = via_playback_multi_pcm_prepare,
1181                 .cleanup = via_playback_multi_pcm_cleanup
1182         },
1183 };
1184
1185 static const struct hda_pcm_stream via_pcm_hp_playback = {
1186         .substreams = 1,
1187         .channels_min = 2,
1188         .channels_max = 2,
1189         /* NID is set in via_build_pcms */
1190         .ops = {
1191                 .open = via_playback_hp_pcm_open,
1192                 .close = via_playback_hp_pcm_close,
1193                 .prepare = via_playback_hp_pcm_prepare,
1194                 .cleanup = via_playback_hp_pcm_cleanup
1195         },
1196 };
1197
1198 static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
1199         .substreams = 1,
1200         .channels_min = 2,
1201         .channels_max = 8,
1202         /* NID is set in via_build_pcms */
1203         /* We got noisy outputs on the right channel on VT1708 when
1204          * 24bit samples are used.  Until any workaround is found,
1205          * disable the 24bit format, so far.
1206          */
1207         .formats = SNDRV_PCM_FMTBIT_S16_LE,
1208         .ops = {
1209                 .open = via_playback_multi_pcm_open,
1210                 .close = via_playback_multi_pcm_close,
1211                 .prepare = via_playback_multi_pcm_prepare,
1212                 .cleanup = via_playback_multi_pcm_cleanup
1213         },
1214 };
1215
1216 static const struct hda_pcm_stream via_pcm_analog_capture = {
1217         .substreams = 1, /* will be changed in via_build_pcms() */
1218         .channels_min = 2,
1219         .channels_max = 2,
1220         /* NID is set in via_build_pcms */
1221         .ops = {
1222                 .prepare = via_capture_pcm_prepare,
1223                 .cleanup = via_capture_pcm_cleanup
1224         },
1225 };
1226
1227 static const struct hda_pcm_stream via_pcm_digital_playback = {
1228         .substreams = 1,
1229         .channels_min = 2,
1230         .channels_max = 2,
1231         /* NID is set in via_build_pcms */
1232         .ops = {
1233                 .open = via_dig_playback_pcm_open,
1234                 .close = via_dig_playback_pcm_close,
1235                 .prepare = via_dig_playback_pcm_prepare,
1236                 .cleanup = via_dig_playback_pcm_cleanup
1237         },
1238 };
1239
1240 static const struct hda_pcm_stream via_pcm_digital_capture = {
1241         .substreams = 1,
1242         .channels_min = 2,
1243         .channels_max = 2,
1244 };
1245
1246 /*
1247  * slave controls for virtual master
1248  */
1249 static const char * const via_slave_vols[] = {
1250         "Front Playback Volume",
1251         "Surround Playback Volume",
1252         "Center Playback Volume",
1253         "LFE Playback Volume",
1254         "Side Playback Volume",
1255         "Headphone Playback Volume",
1256         "Speaker Playback Volume",
1257         NULL,
1258 };
1259
1260 static const char * const via_slave_sws[] = {
1261         "Front Playback Switch",
1262         "Surround Playback Switch",
1263         "Center Playback Switch",
1264         "LFE Playback Switch",
1265         "Side Playback Switch",
1266         "Headphone Playback Switch",
1267         "Speaker Playback Switch",
1268         NULL,
1269 };
1270
1271 static int via_build_controls(struct hda_codec *codec)
1272 {
1273         struct via_spec *spec = codec->spec;
1274         struct snd_kcontrol *kctl;
1275         int err, i;
1276
1277         if (spec->set_widgets_power_state)
1278                 if (!via_clone_control(spec, &via_pin_power_ctl_enum))
1279                         return -ENOMEM;
1280
1281         for (i = 0; i < spec->num_mixers; i++) {
1282                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1283                 if (err < 0)
1284                         return err;
1285         }
1286
1287         if (spec->multiout.dig_out_nid) {
1288                 err = snd_hda_create_spdif_out_ctls(codec,
1289                                                     spec->multiout.dig_out_nid,
1290                                                     spec->multiout.dig_out_nid);
1291                 if (err < 0)
1292                         return err;
1293                 err = snd_hda_create_spdif_share_sw(codec,
1294                                                     &spec->multiout);
1295                 if (err < 0)
1296                         return err;
1297                 spec->multiout.share_spdif = 1;
1298         }
1299         if (spec->dig_in_nid) {
1300                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1301                 if (err < 0)
1302                         return err;
1303         }
1304
1305         /* if we have no master control, let's create it */
1306         if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1307                 unsigned int vmaster_tlv[4];
1308                 snd_hda_set_vmaster_tlv(codec, spec->multiout.dac_nids[0],
1309                                         HDA_OUTPUT, vmaster_tlv);
1310                 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
1311                                           vmaster_tlv, via_slave_vols);
1312                 if (err < 0)
1313                         return err;
1314         }
1315         if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1316                 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
1317                                           NULL, via_slave_sws);
1318                 if (err < 0)
1319                         return err;
1320         }
1321
1322         /* assign Capture Source enums to NID */
1323         kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1324         for (i = 0; kctl && i < kctl->count; i++) {
1325                 err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
1326                 if (err < 0)
1327                         return err;
1328         }
1329
1330         /* init power states */
1331         set_widgets_power_state(codec);
1332         analog_low_current_mode(codec);
1333
1334         via_free_kctls(codec); /* no longer needed */
1335         return 0;
1336 }
1337
1338 static int via_build_pcms(struct hda_codec *codec)
1339 {
1340         struct via_spec *spec = codec->spec;
1341         struct hda_pcm *info = spec->pcm_rec;
1342
1343         codec->num_pcms = 1;
1344         codec->pcm_info = info;
1345
1346         snprintf(spec->stream_name_analog, sizeof(spec->stream_name_analog),
1347                  "%s Analog", codec->chip_name);
1348         info->name = spec->stream_name_analog;
1349
1350         if (!spec->stream_analog_playback)
1351                 spec->stream_analog_playback = &via_pcm_analog_playback;
1352         info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1353                 *spec->stream_analog_playback;
1354         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1355                 spec->multiout.dac_nids[0];
1356         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1357                 spec->multiout.max_channels;
1358
1359         if (!spec->stream_analog_capture)
1360                 spec->stream_analog_capture = &via_pcm_analog_capture;
1361         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1362                 *spec->stream_analog_capture;
1363         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
1364         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
1365                 spec->num_adc_nids;
1366
1367         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1368                 codec->num_pcms++;
1369                 info++;
1370                 snprintf(spec->stream_name_digital,
1371                          sizeof(spec->stream_name_digital),
1372                          "%s Digital", codec->chip_name);
1373                 info->name = spec->stream_name_digital;
1374                 info->pcm_type = HDA_PCM_TYPE_SPDIF;
1375                 if (spec->multiout.dig_out_nid) {
1376                         if (!spec->stream_digital_playback)
1377                                 spec->stream_digital_playback =
1378                                         &via_pcm_digital_playback;
1379                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1380                                 *spec->stream_digital_playback;
1381                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1382                                 spec->multiout.dig_out_nid;
1383                 }
1384                 if (spec->dig_in_nid) {
1385                         if (!spec->stream_digital_capture)
1386                                 spec->stream_digital_capture =
1387                                         &via_pcm_digital_capture;
1388                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1389                                 *spec->stream_digital_capture;
1390                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1391                                 spec->dig_in_nid;
1392                 }
1393         }
1394
1395         if (spec->hp_dac_nid) {
1396                 codec->num_pcms++;
1397                 info++;
1398                 snprintf(spec->stream_name_hp, sizeof(spec->stream_name_hp),
1399                          "%s HP", codec->chip_name);
1400                 info->name = spec->stream_name_hp;
1401                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = via_pcm_hp_playback;
1402                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1403                         spec->hp_dac_nid;
1404         }
1405         return 0;
1406 }
1407
1408 static void via_free(struct hda_codec *codec)
1409 {
1410         struct via_spec *spec = codec->spec;
1411
1412         if (!spec)
1413                 return;
1414
1415         via_free_kctls(codec);
1416         vt1708_stop_hp_work(spec);
1417         kfree(codec->spec);
1418 }
1419
1420 /* mute/unmute outputs */
1421 static void toggle_output_mutes(struct hda_codec *codec, int num_pins,
1422                                 hda_nid_t *pins, bool mute)
1423 {
1424         int i;
1425         for (i = 0; i < num_pins; i++)
1426                 snd_hda_codec_write(codec, pins[i], 0,
1427                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1428                                     mute ? 0 : PIN_OUT);
1429 }
1430
1431 /* mute internal speaker if line-out is plugged */
1432 static void via_line_automute(struct hda_codec *codec, int present)
1433 {
1434         struct via_spec *spec = codec->spec;
1435
1436         if (!spec->autocfg.speaker_outs)
1437                 return;
1438         if (!present)
1439                 present = snd_hda_jack_detect(codec,
1440                                               spec->autocfg.line_out_pins[0]);
1441         toggle_output_mutes(codec, spec->autocfg.speaker_outs,
1442                             spec->autocfg.speaker_pins,
1443                             present);
1444 }
1445
1446 /* mute internal speaker if HP is plugged */
1447 static void via_hp_automute(struct hda_codec *codec)
1448 {
1449         int present = 0;
1450         struct via_spec *spec = codec->spec;
1451
1452         if (!spec->hp_independent_mode && spec->autocfg.hp_pins[0]) {
1453                 present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
1454                 toggle_output_mutes(codec, spec->autocfg.line_outs,
1455                                     spec->autocfg.line_out_pins,
1456                                     present);
1457         }
1458         via_line_automute(codec, present);
1459 }
1460
1461 static void via_gpio_control(struct hda_codec *codec)
1462 {
1463         unsigned int gpio_data;
1464         unsigned int vol_counter;
1465         unsigned int vol;
1466         unsigned int master_vol;
1467
1468         struct via_spec *spec = codec->spec;
1469
1470         gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
1471                                        AC_VERB_GET_GPIO_DATA, 0) & 0x03;
1472
1473         vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
1474                                           0xF84, 0) & 0x3F0000) >> 16;
1475
1476         vol = vol_counter & 0x1F;
1477         master_vol = snd_hda_codec_read(codec, 0x1A, 0,
1478                                         AC_VERB_GET_AMP_GAIN_MUTE,
1479                                         AC_AMP_GET_INPUT);
1480
1481         if (gpio_data == 0x02) {
1482                 /* unmute line out */
1483                 snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
1484                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1485                                     PIN_OUT);
1486                 if (vol_counter & 0x20) {
1487                         /* decrease volume */
1488                         if (vol > master_vol)
1489                                 vol = master_vol;
1490                         snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
1491                                                  0, HDA_AMP_VOLMASK,
1492                                                  master_vol-vol);
1493                 } else {
1494                         /* increase volume */
1495                         snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
1496                                          HDA_AMP_VOLMASK,
1497                                          ((master_vol+vol) > 0x2A) ? 0x2A :
1498                                           (master_vol+vol));
1499                 }
1500         } else if (!(gpio_data & 0x02)) {
1501                 /* mute line out */
1502                 snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
1503                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1504                                     0);
1505         }
1506 }
1507
1508 /* unsolicited event for jack sensing */
1509 static void via_unsol_event(struct hda_codec *codec,
1510                                   unsigned int res)
1511 {
1512         res >>= 26;
1513
1514         if (res & VIA_JACK_EVENT)
1515                 set_widgets_power_state(codec);
1516
1517         res &= ~VIA_JACK_EVENT;
1518
1519         if (res == VIA_HP_EVENT)
1520                 via_hp_automute(codec);
1521         else if (res == VIA_GPIO_EVENT)
1522                 via_gpio_control(codec);
1523         else if (res == VIA_LINE_EVENT)
1524                 via_line_automute(codec, false);
1525 }
1526
1527 #ifdef SND_HDA_NEEDS_RESUME
1528 static int via_suspend(struct hda_codec *codec, pm_message_t state)
1529 {
1530         struct via_spec *spec = codec->spec;
1531         vt1708_stop_hp_work(spec);
1532         return 0;
1533 }
1534 #endif
1535
1536 #ifdef CONFIG_SND_HDA_POWER_SAVE
1537 static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1538 {
1539         struct via_spec *spec = codec->spec;
1540         return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1541 }
1542 #endif
1543
1544 /*
1545  */
1546
1547 static int via_init(struct hda_codec *codec);
1548
1549 static const struct hda_codec_ops via_patch_ops = {
1550         .build_controls = via_build_controls,
1551         .build_pcms = via_build_pcms,
1552         .init = via_init,
1553         .free = via_free,
1554         .unsol_event = via_unsol_event,
1555 #ifdef SND_HDA_NEEDS_RESUME
1556         .suspend = via_suspend,
1557 #endif
1558 #ifdef CONFIG_SND_HDA_POWER_SAVE
1559         .check_power_status = via_check_power_status,
1560 #endif
1561 };
1562
1563 static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
1564 {
1565         struct via_spec *spec = codec->spec;
1566         int i;
1567
1568         for (i = 0; i < spec->multiout.num_dacs; i++) {
1569                 if (spec->multiout.dac_nids[i] == dac)
1570                         return false;
1571         }
1572         if (spec->hp_dac_nid == dac)
1573                 return false;
1574         return true;
1575 }
1576
1577 static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
1578                               hda_nid_t target_dac, struct nid_path *path,
1579                               int depth, int wid_type)
1580 {
1581         hda_nid_t conn[8];
1582         int i, nums;
1583
1584         nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
1585         for (i = 0; i < nums; i++) {
1586                 if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
1587                         continue;
1588                 if (conn[i] == target_dac || is_empty_dac(codec, conn[i]))
1589                         goto found;
1590         }
1591         if (depth >= MAX_NID_PATH_DEPTH)
1592                 return false;
1593         for (i = 0; i < nums; i++) {
1594                 unsigned int type;
1595                 type = get_wcaps_type(get_wcaps(codec, conn[i]));
1596                 if (type == AC_WID_AUD_OUT ||
1597                     (wid_type != -1 && type != wid_type))
1598                         continue;
1599                 if (__parse_output_path(codec, conn[i], target_dac,
1600                                       path, depth + 1, AC_WID_AUD_SEL))
1601                         goto found;
1602         }
1603         return false;
1604
1605  found:
1606         path->path[path->depth] = conn[i];
1607         path->idx[path->depth] = i;
1608         if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
1609                 path->multi[path->depth] = 1;
1610         path->depth++;
1611         return true;
1612 }
1613
1614 static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
1615                               hda_nid_t target_dac, struct nid_path *path)
1616 {
1617         if (__parse_output_path(codec, nid, target_dac, path, 1, -1)) {
1618                 path->path[path->depth] = nid;
1619                 path->depth++;
1620                 return true;
1621         }
1622         return false;
1623 }
1624
1625 static int via_auto_fill_dac_nids(struct hda_codec *codec)
1626 {
1627         struct via_spec *spec = codec->spec;
1628         const struct auto_pin_cfg *cfg = &spec->autocfg;
1629         int i;
1630         hda_nid_t nid;
1631
1632         spec->multiout.dac_nids = spec->private_dac_nids;
1633         spec->multiout.num_dacs = cfg->line_outs;
1634         for (i = 0; i < cfg->line_outs; i++) {
1635                 nid = cfg->line_out_pins[i];
1636                 if (!nid)
1637                         continue;
1638                 if (parse_output_path(codec, nid, 0, &spec->out_path[i]))
1639                         spec->private_dac_nids[i] = spec->out_path[i].path[0];
1640         }
1641         return 0;
1642 }
1643
1644 static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
1645                           int chs, bool check_dac, struct nid_path *path)
1646 {
1647         struct via_spec *spec = codec->spec;
1648         char name[32];
1649         hda_nid_t dac, pin, sel, nid;
1650         int err;
1651
1652         dac = check_dac ? path->path[0] : 0;
1653         pin = path->path[path->depth - 1];
1654         sel = path->depth > 1 ? path->path[1] : 0;
1655
1656         if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1657                 nid = dac;
1658         else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1659                 nid = pin;
1660         else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1661                 nid = sel;
1662         else
1663                 nid = 0;
1664         if (nid) {
1665                 sprintf(name, "%s Playback Volume", pfx);
1666                 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1667                               HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1668                 if (err < 0)
1669                         return err;
1670                 path->vol_ctl = nid;
1671         }
1672
1673         if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
1674                 nid = dac;
1675         else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
1676                 nid = pin;
1677         else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
1678                 nid = sel;
1679         else
1680                 nid = 0;
1681         if (nid) {
1682                 sprintf(name, "%s Playback Switch", pfx);
1683                 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1684                               HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1685                 if (err < 0)
1686                         return err;
1687                 path->mute_ctl = nid;
1688         }
1689         return 0;
1690 }
1691
1692 static void mangle_smart51(struct hda_codec *codec)
1693 {
1694         struct via_spec *spec = codec->spec;
1695         struct auto_pin_cfg *cfg = &spec->autocfg;
1696         struct auto_pin_cfg_item *ins = cfg->inputs;
1697         int i, j, nums, attr;
1698         int pins[AUTO_CFG_MAX_INS];
1699
1700         for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
1701                 nums = 0;
1702                 for (i = 0; i < cfg->num_inputs; i++) {
1703                         unsigned int def;
1704                         if (ins[i].type > AUTO_PIN_LINE_IN)
1705                                 continue;
1706                         def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
1707                         if (snd_hda_get_input_pin_attr(def) != attr)
1708                                 continue;
1709                         for (j = 0; j < nums; j++)
1710                                 if (ins[pins[j]].type < ins[i].type) {
1711                                         memmove(pins + j + 1, pins + j,
1712                                                 (nums - j - 1) * sizeof(int));
1713                                         break;
1714                                 }
1715                         pins[j] = i;
1716                         nums++;
1717                 }
1718                 if (cfg->line_outs + nums < 3)
1719                         continue;
1720                 for (i = 0; i < nums; i++) {
1721                         hda_nid_t pin = ins[pins[i]].pin;
1722                         spec->smart51_pins[spec->smart51_nums++] = pin;
1723                         cfg->line_out_pins[cfg->line_outs++] = pin;
1724                         if (cfg->line_outs == 3)
1725                                 break;
1726                 }
1727                 return;
1728         }
1729 }
1730
1731 /* add playback controls from the parsed DAC table */
1732 static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
1733 {
1734         struct via_spec *spec = codec->spec;
1735         struct auto_pin_cfg *cfg = &spec->autocfg;
1736         static const char * const chname[4] = {
1737                 "Front", "Surround", "C/LFE", "Side"
1738         };
1739         int i, idx, err;
1740         int old_line_outs;
1741
1742         /* check smart51 */
1743         old_line_outs = cfg->line_outs;
1744         if (cfg->line_outs == 1)
1745                 mangle_smart51(codec);
1746
1747         err = via_auto_fill_dac_nids(codec);
1748         if (err < 0)
1749                 return err;
1750
1751         for (i = 0; i < cfg->line_outs; i++) {
1752                 hda_nid_t pin, dac;
1753                 pin = cfg->line_out_pins[i];
1754                 dac = spec->multiout.dac_nids[i];
1755                 if (!pin || !dac)
1756                         continue;
1757                 if (i == HDA_CLFE) {
1758                         err = create_ch_ctls(codec, "Center", 1, true,
1759                                              &spec->out_path[i]);
1760                         if (err < 0)
1761                                 return err;
1762                         err = create_ch_ctls(codec, "LFE", 2, true,
1763                                              &spec->out_path[i]);
1764                         if (err < 0)
1765                                 return err;
1766                 } else {
1767                         const char *pfx = chname[i];
1768                         if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
1769                             cfg->line_outs == 1)
1770                                 pfx = "Speaker";
1771                         err = create_ch_ctls(codec, pfx, 3, true,
1772                                              &spec->out_path[i]);
1773                         if (err < 0)
1774                                 return err;
1775                 }
1776         }
1777
1778         idx = get_connection_index(codec, spec->aa_mix_nid,
1779                                    spec->multiout.dac_nids[0]);
1780         if (idx >= 0) {
1781                 /* add control to mixer */
1782                 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1783                                       "PCM Playback Volume",
1784                                       HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
1785                                                           idx, HDA_INPUT));
1786                 if (err < 0)
1787                         return err;
1788                 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1789                                       "PCM Playback Switch",
1790                                       HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
1791                                                           idx, HDA_INPUT));
1792                 if (err < 0)
1793                         return err;
1794         }
1795
1796         cfg->line_outs = old_line_outs;
1797
1798         return 0;
1799 }
1800
1801 static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
1802 {
1803         struct via_spec *spec = codec->spec;
1804         struct nid_path *path;
1805         int err;
1806
1807         if (!pin)
1808                 return 0;
1809
1810         if (parse_output_path(codec, pin, 0, &spec->hp_path))
1811                 spec->hp_dac_nid = spec->hp_path.path[0];
1812
1813         if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
1814                                &spec->hp_dep_path) &&
1815             !spec->hp_dac_nid)
1816                 return 0;
1817
1818         if (spec->hp_dac_nid)
1819                 path = &spec->hp_path;
1820         else
1821                 path = &spec->hp_dep_path;
1822         err = create_ch_ctls(codec, "Headphone", 3, false, path);
1823         if (err < 0)
1824                 return err;
1825         if (spec->hp_dac_nid) {
1826                 spec->hp_dep_path.vol_ctl = spec->hp_path.vol_ctl;
1827                 spec->hp_dep_path.mute_ctl = spec->hp_path.mute_ctl;
1828         }
1829
1830         return 0;
1831 }
1832
1833 static int via_auto_create_speaker_ctls(struct hda_codec *codec)
1834 {
1835         struct via_spec *spec = codec->spec;
1836         hda_nid_t pin, dac;
1837
1838         pin = spec->autocfg.speaker_pins[0];
1839         if (!spec->autocfg.speaker_outs || !pin)
1840                 return 0;
1841
1842         if (parse_output_path(codec, pin, 0, &spec->speaker_path)) {
1843                 dac = spec->speaker_path.path[0];
1844                 spec->multiout.extra_out_nid[0] = dac;
1845                 return create_ch_ctls(codec, "Speaker", 3, true,
1846                                       &spec->speaker_path);
1847         }
1848         if (parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
1849                               &spec->speaker_path))
1850                 return create_ch_ctls(codec, "Speaker", 3, false,
1851                                       &spec->speaker_path);
1852
1853         return 0;
1854 }
1855
1856 /* look for ADCs */
1857 static int via_fill_adcs(struct hda_codec *codec)
1858 {
1859         struct via_spec *spec = codec->spec;
1860         hda_nid_t nid = codec->start_nid;
1861         int i;
1862
1863         for (i = 0; i < codec->num_nodes; i++, nid++) {
1864                 unsigned int wcaps = get_wcaps(codec, nid);
1865                 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
1866                         continue;
1867                 if (wcaps & AC_WCAP_DIGITAL)
1868                         continue;
1869                 if (!(wcaps & AC_WCAP_CONN_LIST))
1870                         continue;
1871                 if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
1872                         return -ENOMEM;
1873                 spec->adc_nids[spec->num_adc_nids++] = nid;
1874         }
1875         return 0;
1876 }
1877
1878 static int get_mux_nids(struct hda_codec *codec);
1879
1880 static const struct snd_kcontrol_new via_input_src_ctl = {
1881         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1882         /* The multiple "Capture Source" controls confuse alsamixer
1883          * So call somewhat different..
1884          */
1885         /* .name = "Capture Source", */
1886         .name = "Input Source",
1887         .info = via_mux_enum_info,
1888         .get = via_mux_enum_get,
1889         .put = via_mux_enum_put,
1890 };
1891
1892 static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
1893 {
1894         struct hda_amp_list *list;
1895
1896         if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1897                 return;
1898         list = spec->loopback_list + spec->num_loopbacks;
1899         list->nid = mix;
1900         list->dir = HDA_INPUT;
1901         list->idx = idx;
1902         spec->num_loopbacks++;
1903         spec->loopback.amplist = spec->loopback_list;
1904 }
1905
1906 /* create playback/capture controls for input pins */
1907 static int via_auto_create_analog_input_ctls(struct hda_codec *codec,
1908                                              const struct auto_pin_cfg *cfg)
1909 {
1910         struct via_spec *spec = codec->spec;
1911         struct hda_input_mux *imux = &spec->private_imux[0];
1912         int i, j, err, idx, idx2, type, type_idx = 0;
1913         const char *prev_label = NULL;
1914         hda_nid_t cap_nid;
1915         hda_nid_t pin_idxs[8];
1916         int num_idxs;
1917
1918         err = via_fill_adcs(codec);
1919         if (err < 0)
1920                 return err;
1921         err = get_mux_nids(codec);
1922         if (err < 0)
1923                 return err;
1924         cap_nid = spec->mux_nids[0];
1925
1926         num_idxs = snd_hda_get_connections(codec, cap_nid, pin_idxs,
1927                                            ARRAY_SIZE(pin_idxs));
1928         if (num_idxs <= 0)
1929                 return 0;
1930
1931         /* for internal loopback recording select */
1932         for (idx = 0; idx < num_idxs; idx++) {
1933                 if (pin_idxs[idx] == spec->aa_mix_nid) {
1934                         snd_hda_add_imux_item(imux, "Stereo Mixer", idx, NULL);
1935                         break;
1936                 }
1937         }
1938
1939         for (i = 0; i < cfg->num_inputs; i++) {
1940                 const char *label;
1941                 type = cfg->inputs[i].type;
1942                 for (idx = 0; idx < num_idxs; idx++)
1943                         if (pin_idxs[idx] == cfg->inputs[i].pin)
1944                                 break;
1945                 if (idx >= num_idxs)
1946                         continue;
1947                 label = hda_get_autocfg_input_label(codec, cfg, i);
1948                 if (prev_label && !strcmp(label, prev_label))
1949                         type_idx++;
1950                 else
1951                         type_idx = 0;
1952                 prev_label = label;
1953                 idx2 = get_connection_index(codec, spec->aa_mix_nid,
1954                                             pin_idxs[idx]);
1955                 if (idx2 >= 0) {
1956                         err = via_new_analog_input(spec, label, type_idx,
1957                                                    idx2, spec->aa_mix_nid);
1958                         if (err < 0)
1959                                 return err;
1960                         add_loopback_list(spec, spec->aa_mix_nid, idx2);
1961                 }
1962                 snd_hda_add_imux_item(imux, label, idx, NULL);
1963
1964                 /* remember the label for smart51 control */
1965                 for (j = 0; j < spec->smart51_nums; j++) {
1966                         if (spec->smart51_pins[j] == cfg->inputs[i].pin) {
1967                                 spec->smart51_idxs[j] = idx;
1968                                 spec->smart51_labels[j] = label;
1969                                 break;
1970                         }
1971                 }
1972         }
1973
1974         /* create capture mixer elements */
1975         for (i = 0; i < spec->num_adc_nids; i++) {
1976                 hda_nid_t adc = spec->adc_nids[i];
1977                 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
1978                                         "Capture Volume", i,
1979                                         HDA_COMPOSE_AMP_VAL(adc, 3, 0,
1980                                                             HDA_INPUT));
1981                 if (err < 0)
1982                         return err;
1983                 err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1984                                         "Capture Switch", i,
1985                                         HDA_COMPOSE_AMP_VAL(adc, 3, 0,
1986                                                             HDA_INPUT));
1987                 if (err < 0)
1988                         return err;
1989         }
1990
1991         /* input-source control */
1992         for (i = 0; i < spec->num_adc_nids; i++)
1993                 if (!spec->mux_nids[i])
1994                         break;
1995         if (i) {
1996                 struct snd_kcontrol_new *knew;
1997                 knew = via_clone_control(spec, &via_input_src_ctl);
1998                 if (!knew)
1999                         return -ENOMEM;
2000                 knew->count = i;
2001         }
2002
2003         /* mic-boosts */
2004         for (i = 0; i < cfg->num_inputs; i++) {
2005                 hda_nid_t pin = cfg->inputs[i].pin;
2006                 unsigned int caps;
2007                 const char *label;
2008                 char name[32];
2009
2010                 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2011                         continue;
2012                 caps = query_amp_caps(codec, pin, HDA_INPUT);
2013                 if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
2014                         continue;
2015                 label = hda_get_autocfg_input_label(codec, cfg, i);
2016                 snprintf(name, sizeof(name), "%s Boost Volume", label);
2017                 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2018                               HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
2019                 if (err < 0)
2020                         return err;
2021         }
2022
2023         return 0;
2024 }
2025
2026 static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
2027 {
2028         unsigned int def_conf;
2029         unsigned char seqassoc;
2030
2031         def_conf = snd_hda_codec_get_pincfg(codec, nid);
2032         seqassoc = (unsigned char) get_defcfg_association(def_conf);
2033         seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
2034         if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
2035             && (seqassoc == 0xf0 || seqassoc == 0xff)) {
2036                 def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
2037                 snd_hda_codec_set_pincfg(codec, nid, def_conf);
2038         }
2039
2040         return;
2041 }
2042
2043 static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
2044                                      struct snd_ctl_elem_value *ucontrol)
2045 {
2046         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2047         struct via_spec *spec = codec->spec;
2048
2049         if (spec->codec_type != VT1708)
2050                 return 0;
2051         spec->vt1708_jack_detect =
2052                 !((snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8) & 0x1);
2053         ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
2054         return 0;
2055 }
2056
2057 static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
2058                                      struct snd_ctl_elem_value *ucontrol)
2059 {
2060         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2061         struct via_spec *spec = codec->spec;
2062         int change;
2063
2064         if (spec->codec_type != VT1708)
2065                 return 0;
2066         spec->vt1708_jack_detect = ucontrol->value.integer.value[0];
2067         change = (0x1 & (snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8))
2068                 == !spec->vt1708_jack_detect;
2069         if (spec->vt1708_jack_detect) {
2070                 mute_aa_path(codec, 1);
2071                 notify_aa_path_ctls(codec);
2072         }
2073         return change;
2074 }
2075
2076 static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
2077         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2078         .name = "Jack Detect",
2079         .count = 1,
2080         .info = snd_ctl_boolean_mono_info,
2081         .get = vt1708_jack_detect_get,
2082         .put = vt1708_jack_detect_put,
2083 };
2084
2085 static void fill_dig_outs(struct hda_codec *codec);
2086 static void fill_dig_in(struct hda_codec *codec);
2087
2088 static int via_parse_auto_config(struct hda_codec *codec)
2089 {
2090         struct via_spec *spec = codec->spec;
2091         int err;
2092
2093         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2094         if (err < 0)
2095                 return err;
2096         if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2097                 return -EINVAL;
2098
2099         err = via_auto_create_multi_out_ctls(codec);
2100         if (err < 0)
2101                 return err;
2102         err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
2103         if (err < 0)
2104                 return err;
2105         err = via_auto_create_speaker_ctls(codec);
2106         if (err < 0)
2107                 return err;
2108         err = via_auto_create_analog_input_ctls(codec, &spec->autocfg);
2109         if (err < 0)
2110                 return err;
2111
2112         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2113
2114         fill_dig_outs(codec);
2115         fill_dig_in(codec);
2116
2117         if (spec->kctls.list)
2118                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
2119
2120         spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
2121
2122         spec->input_mux = &spec->private_imux[0];
2123
2124         if (spec->hp_dac_nid && spec->hp_dep_path.depth) {
2125                 err = via_hp_build(codec);
2126                 if (err < 0)
2127                         return err;
2128         }
2129
2130         err = via_smart51_build(codec);
2131         if (err < 0)
2132                 return err;
2133
2134         /* assign slave outs */
2135         if (spec->slave_dig_outs[0])
2136                 codec->slave_dig_outs = spec->slave_dig_outs;
2137
2138         return 1;
2139 }
2140
2141 static void via_auto_init_dig_outs(struct hda_codec *codec)
2142 {
2143         struct via_spec *spec = codec->spec;
2144         if (spec->multiout.dig_out_nid)
2145                 init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
2146         if (spec->slave_dig_outs[0])
2147                 init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
2148 }
2149
2150 static void via_auto_init_dig_in(struct hda_codec *codec)
2151 {
2152         struct via_spec *spec = codec->spec;
2153         if (!spec->dig_in_nid)
2154                 return;
2155         snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
2156                             AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
2157 }
2158
2159 /* initialize the unsolicited events */
2160 static void via_auto_init_unsol_event(struct hda_codec *codec)
2161 {
2162         struct via_spec *spec = codec->spec;
2163         struct auto_pin_cfg *cfg = &spec->autocfg;
2164         unsigned int ev;
2165         int i;
2166
2167         if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
2168                 snd_hda_codec_write(codec, cfg->hp_pins[0], 0,
2169                                 AC_VERB_SET_UNSOLICITED_ENABLE,
2170                                 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT);
2171
2172         if (cfg->speaker_pins[0])
2173                 ev = VIA_LINE_EVENT;
2174         else
2175                 ev = 0;
2176         for (i = 0; i < cfg->line_outs; i++) {
2177                 if (cfg->line_out_pins[i] &&
2178                     is_jack_detectable(codec, cfg->line_out_pins[i]))
2179                         snd_hda_codec_write(codec, cfg->line_out_pins[0], 0,
2180                                 AC_VERB_SET_UNSOLICITED_ENABLE,
2181                                 AC_USRSP_EN | ev | VIA_JACK_EVENT);
2182         }
2183
2184         for (i = 0; i < cfg->num_inputs; i++) {
2185                 if (is_jack_detectable(codec, cfg->inputs[i].pin))
2186                         snd_hda_codec_write(codec, cfg->inputs[i].pin, 0,
2187                                 AC_VERB_SET_UNSOLICITED_ENABLE,
2188                                 AC_USRSP_EN | VIA_JACK_EVENT);
2189         }
2190 }
2191
2192 static int via_init(struct hda_codec *codec)
2193 {
2194         struct via_spec *spec = codec->spec;
2195         int i;
2196
2197         for (i = 0; i < spec->num_iverbs; i++)
2198                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
2199
2200         via_auto_init_multi_out(codec);
2201         via_auto_init_hp_out(codec);
2202         via_auto_init_speaker_out(codec);
2203         via_auto_init_analog_input(codec);
2204         via_auto_init_dig_outs(codec);
2205         via_auto_init_dig_in(codec);
2206
2207         via_auto_init_unsol_event(codec);
2208
2209         via_hp_automute(codec);
2210         via_line_automute(codec, false);
2211
2212         return 0;
2213 }
2214
2215 static void vt1708_update_hp_jack_state(struct work_struct *work)
2216 {
2217         struct via_spec *spec = container_of(work, struct via_spec,
2218                                              vt1708_hp_work.work);
2219         if (spec->codec_type != VT1708)
2220                 return;
2221         /* if jack state toggled */
2222         if (spec->vt1708_hp_present
2223             != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
2224                 spec->vt1708_hp_present ^= 1;
2225                 via_hp_automute(spec->codec);
2226         }
2227         vt1708_start_hp_work(spec);
2228 }
2229
2230 static int get_mux_nids(struct hda_codec *codec)
2231 {
2232         struct via_spec *spec = codec->spec;
2233         hda_nid_t nid, conn[8];
2234         unsigned int type;
2235         int i, n;
2236
2237         for (i = 0; i < spec->num_adc_nids; i++) {
2238                 nid = spec->adc_nids[i];
2239                 while (nid) {
2240                         type = get_wcaps_type(get_wcaps(codec, nid));
2241                         if (type == AC_WID_PIN)
2242                                 break;
2243                         n = snd_hda_get_connections(codec, nid, conn,
2244                                                     ARRAY_SIZE(conn));
2245                         if (n <= 0)
2246                                 break;
2247                         if (n > 1) {
2248                                 spec->mux_nids[i] = nid;
2249                                 break;
2250                         }
2251                         nid = conn[0];
2252                 }
2253         }
2254         return 0;
2255 }
2256
2257 static int patch_vt1708(struct hda_codec *codec)
2258 {
2259         struct via_spec *spec;
2260         int err;
2261
2262         /* create a codec specific record */
2263         spec = via_new_spec(codec);
2264         if (spec == NULL)
2265                 return -ENOMEM;
2266
2267         spec->aa_mix_nid = 0x17;
2268
2269         /* Add HP and CD pin config connect bit re-config action */
2270         vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
2271         vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
2272
2273         /* automatic parse from the BIOS config */
2274         err = via_parse_auto_config(codec);
2275         if (err < 0) {
2276                 via_free(codec);
2277                 return err;
2278         }
2279
2280         /* add jack detect on/off control */
2281         if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
2282                 return -ENOMEM;
2283
2284         /* disable 32bit format on VT1708 */
2285         if (codec->vendor_id == 0x11061708)
2286                 spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
2287
2288         codec->patch_ops = via_patch_ops;
2289
2290         INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
2291         return 0;
2292 }
2293
2294 static int patch_vt1709(struct hda_codec *codec)
2295 {
2296         struct via_spec *spec;
2297         int err;
2298
2299         /* create a codec specific record */
2300         spec = via_new_spec(codec);
2301         if (spec == NULL)
2302                 return -ENOMEM;
2303
2304         spec->aa_mix_nid = 0x18;
2305
2306         err = via_parse_auto_config(codec);
2307         if (err < 0) {
2308                 via_free(codec);
2309                 return err;
2310         }
2311
2312         codec->patch_ops = via_patch_ops;
2313
2314         return 0;
2315 }
2316
2317 static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
2318 {
2319         struct via_spec *spec = codec->spec;
2320         int imux_is_smixer;
2321         unsigned int parm;
2322         int is_8ch = 0;
2323         if ((spec->codec_type != VT1708B_4CH) &&
2324             (codec->vendor_id != 0x11064397))
2325                 is_8ch = 1;
2326
2327         /* SW0 (17h) = stereo mixer */
2328         imux_is_smixer =
2329         (snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
2330          == ((spec->codec_type == VT1708S) ? 5 : 0));
2331         /* inputs */
2332         /* PW 1/2/5 (1ah/1bh/1eh) */
2333         parm = AC_PWRST_D3;
2334         set_pin_power_state(codec, 0x1a, &parm);
2335         set_pin_power_state(codec, 0x1b, &parm);
2336         set_pin_power_state(codec, 0x1e, &parm);
2337         if (imux_is_smixer)
2338                 parm = AC_PWRST_D0;
2339         /* SW0 (17h), AIW 0/1 (13h/14h) */
2340         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
2341         snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
2342         snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
2343
2344         /* outputs */
2345         /* PW0 (19h), SW1 (18h), AOW1 (11h) */
2346         parm = AC_PWRST_D3;
2347         set_pin_power_state(codec, 0x19, &parm);
2348         if (spec->smart51_enabled)
2349                 set_pin_power_state(codec, 0x1b, &parm);
2350         snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
2351         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
2352
2353         /* PW6 (22h), SW2 (26h), AOW2 (24h) */
2354         if (is_8ch) {
2355                 parm = AC_PWRST_D3;
2356                 set_pin_power_state(codec, 0x22, &parm);
2357                 if (spec->smart51_enabled)
2358                         set_pin_power_state(codec, 0x1a, &parm);
2359                 snd_hda_codec_write(codec, 0x26, 0,
2360                                     AC_VERB_SET_POWER_STATE, parm);
2361                 snd_hda_codec_write(codec, 0x24, 0,
2362                                     AC_VERB_SET_POWER_STATE, parm);
2363         } else if (codec->vendor_id == 0x11064397) {
2364                 /* PW7(23h), SW2(27h), AOW2(25h) */
2365                 parm = AC_PWRST_D3;
2366                 set_pin_power_state(codec, 0x23, &parm);
2367                 if (spec->smart51_enabled)
2368                         set_pin_power_state(codec, 0x1a, &parm);
2369                 snd_hda_codec_write(codec, 0x27, 0,
2370                                     AC_VERB_SET_POWER_STATE, parm);
2371                 snd_hda_codec_write(codec, 0x25, 0,
2372                                     AC_VERB_SET_POWER_STATE, parm);
2373         }
2374
2375         /* PW 3/4/7 (1ch/1dh/23h) */
2376         parm = AC_PWRST_D3;
2377         /* force to D0 for internal Speaker */
2378         set_pin_power_state(codec, 0x1c, &parm);
2379         set_pin_power_state(codec, 0x1d, &parm);
2380         if (is_8ch)
2381                 set_pin_power_state(codec, 0x23, &parm);
2382
2383         /* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
2384         snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
2385                             imux_is_smixer ? AC_PWRST_D0 : parm);
2386         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
2387         if (is_8ch) {
2388                 snd_hda_codec_write(codec, 0x25, 0,
2389                                     AC_VERB_SET_POWER_STATE, parm);
2390                 snd_hda_codec_write(codec, 0x27, 0,
2391                                     AC_VERB_SET_POWER_STATE, parm);
2392         } else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
2393                 snd_hda_codec_write(codec, 0x25, 0,
2394                                     AC_VERB_SET_POWER_STATE, parm);
2395 }
2396
2397 static int patch_vt1708S(struct hda_codec *codec);
2398 static int patch_vt1708B(struct hda_codec *codec)
2399 {
2400         struct via_spec *spec;
2401         int err;
2402
2403         if (get_codec_type(codec) == VT1708BCE)
2404                 return patch_vt1708S(codec);
2405
2406         /* create a codec specific record */
2407         spec = via_new_spec(codec);
2408         if (spec == NULL)
2409                 return -ENOMEM;
2410
2411         spec->aa_mix_nid = 0x16;
2412
2413         /* automatic parse from the BIOS config */
2414         err = via_parse_auto_config(codec);
2415         if (err < 0) {
2416                 via_free(codec);
2417                 return err;
2418         }
2419
2420         codec->patch_ops = via_patch_ops;
2421
2422         spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
2423
2424         return 0;
2425 }
2426
2427 /* Patch for VT1708S */
2428 static const struct hda_verb vt1708S_init_verbs[] = {
2429         /* Enable Mic Boost Volume backdoor */
2430         {0x1, 0xf98, 0x1},
2431         /* don't bybass mixer */
2432         {0x1, 0xf88, 0xc0},
2433         { }
2434 };
2435
2436 /* fill out digital output widgets; one for master and one for slave outputs */
2437 static void fill_dig_outs(struct hda_codec *codec)
2438 {
2439         struct via_spec *spec = codec->spec;
2440         int i;
2441
2442         for (i = 0; i < spec->autocfg.dig_outs; i++) {
2443                 hda_nid_t nid;
2444                 int conn;
2445
2446                 nid = spec->autocfg.dig_out_pins[i];
2447                 if (!nid)
2448                         continue;
2449                 conn = snd_hda_get_connections(codec, nid, &nid, 1);
2450                 if (conn < 1)
2451                         continue;
2452                 if (!spec->multiout.dig_out_nid)
2453                         spec->multiout.dig_out_nid = nid;
2454                 else {
2455                         spec->slave_dig_outs[0] = nid;
2456                         break; /* at most two dig outs */
2457                 }
2458         }
2459 }
2460
2461 static void fill_dig_in(struct hda_codec *codec)
2462 {
2463         struct via_spec *spec = codec->spec;
2464         hda_nid_t dig_nid;
2465         int i, err;
2466
2467         if (!spec->autocfg.dig_in_pin)
2468                 return;
2469
2470         dig_nid = codec->start_nid;
2471         for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2472                 unsigned int wcaps = get_wcaps(codec, dig_nid);
2473                 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2474                         continue;
2475                 if (!(wcaps & AC_WCAP_DIGITAL))
2476                         continue;
2477                 if (!(wcaps & AC_WCAP_CONN_LIST))
2478                         continue;
2479                 err = get_connection_index(codec, dig_nid,
2480                                            spec->autocfg.dig_in_pin);
2481                 if (err >= 0) {
2482                         spec->dig_in_nid = dig_nid;
2483                         break;
2484                 }
2485         }
2486 }
2487
2488 static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
2489                                int offset, int num_steps, int step_size)
2490 {
2491         snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
2492                                   (offset << AC_AMPCAP_OFFSET_SHIFT) |
2493                                   (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
2494                                   (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
2495                                   (0 << AC_AMPCAP_MUTE_SHIFT));
2496 }
2497
2498 static int patch_vt1708S(struct hda_codec *codec)
2499 {
2500         struct via_spec *spec;
2501         int err;
2502
2503         /* create a codec specific record */
2504         spec = via_new_spec(codec);
2505         if (spec == NULL)
2506                 return -ENOMEM;
2507
2508         spec->aa_mix_nid = 0x16;
2509         override_mic_boost(codec, 0x1a, 0, 3, 40);
2510         override_mic_boost(codec, 0x1e, 0, 3, 40);
2511
2512         /* automatic parse from the BIOS config */
2513         err = via_parse_auto_config(codec);
2514         if (err < 0) {
2515                 via_free(codec);
2516                 return err;
2517         }
2518
2519         spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
2520
2521         codec->patch_ops = via_patch_ops;
2522
2523         /* correct names for VT1708BCE */
2524         if (get_codec_type(codec) == VT1708BCE) {
2525                 kfree(codec->chip_name);
2526                 codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
2527                 snprintf(codec->bus->card->mixername,
2528                          sizeof(codec->bus->card->mixername),
2529                          "%s %s", codec->vendor_name, codec->chip_name);
2530         }
2531         /* correct names for VT1705 */
2532         if (codec->vendor_id == 0x11064397)     {
2533                 kfree(codec->chip_name);
2534                 codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
2535                 snprintf(codec->bus->card->mixername,
2536                          sizeof(codec->bus->card->mixername),
2537                          "%s %s", codec->vendor_name, codec->chip_name);
2538         }
2539         spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
2540         return 0;
2541 }
2542
2543 /* Patch for VT1702 */
2544
2545 static const struct hda_verb vt1702_init_verbs[] = {
2546         /* mixer enable */
2547         {0x1, 0xF88, 0x3},
2548         /* GPIO 0~2 */
2549         {0x1, 0xF82, 0x3F},
2550         { }
2551 };
2552
2553 static void set_widgets_power_state_vt1702(struct hda_codec *codec)
2554 {
2555         int imux_is_smixer =
2556         snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
2557         unsigned int parm;
2558         /* inputs */
2559         /* PW 1/2/5 (14h/15h/18h) */
2560         parm = AC_PWRST_D3;
2561         set_pin_power_state(codec, 0x14, &parm);
2562         set_pin_power_state(codec, 0x15, &parm);
2563         set_pin_power_state(codec, 0x18, &parm);
2564         if (imux_is_smixer)
2565                 parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
2566         /* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
2567         snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
2568         snd_hda_codec_write(codec, 0x12, 0, AC_VERB_SET_POWER_STATE, parm);
2569         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
2570         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_POWER_STATE, parm);
2571
2572         /* outputs */
2573         /* PW 3/4 (16h/17h) */
2574         parm = AC_PWRST_D3;
2575         set_pin_power_state(codec, 0x17, &parm);
2576         set_pin_power_state(codec, 0x16, &parm);
2577         /* MW0 (1ah), AOW 0/1 (10h/1dh) */
2578         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
2579                             imux_is_smixer ? AC_PWRST_D0 : parm);
2580         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
2581         snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
2582 }
2583
2584 static int patch_vt1702(struct hda_codec *codec)
2585 {
2586         struct via_spec *spec;
2587         int err;
2588
2589         /* create a codec specific record */
2590         spec = via_new_spec(codec);
2591         if (spec == NULL)
2592                 return -ENOMEM;
2593
2594         spec->aa_mix_nid = 0x1a;
2595
2596         /* limit AA path volume to 0 dB */
2597         snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
2598                                   (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
2599                                   (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
2600                                   (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
2601                                   (1 << AC_AMPCAP_MUTE_SHIFT));
2602
2603         /* automatic parse from the BIOS config */
2604         err = via_parse_auto_config(codec);
2605         if (err < 0) {
2606                 via_free(codec);
2607                 return err;
2608         }
2609
2610         spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
2611
2612         codec->patch_ops = via_patch_ops;
2613
2614         spec->set_widgets_power_state =  set_widgets_power_state_vt1702;
2615         return 0;
2616 }
2617
2618 /* Patch for VT1718S */
2619
2620 static const struct hda_verb vt1718S_init_verbs[] = {
2621         /* Enable MW0 adjust Gain 5 */
2622         {0x1, 0xfb2, 0x10},
2623         /* Enable Boost Volume backdoor */
2624         {0x1, 0xf88, 0x8},
2625
2626         { }
2627 };
2628
2629 static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
2630 {
2631         struct via_spec *spec = codec->spec;
2632         int imux_is_smixer;
2633         unsigned int parm;
2634         /* MUX6 (1eh) = stereo mixer */
2635         imux_is_smixer =
2636         snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
2637         /* inputs */
2638         /* PW 5/6/7 (29h/2ah/2bh) */
2639         parm = AC_PWRST_D3;
2640         set_pin_power_state(codec, 0x29, &parm);
2641         set_pin_power_state(codec, 0x2a, &parm);
2642         set_pin_power_state(codec, 0x2b, &parm);
2643         if (imux_is_smixer)
2644                 parm = AC_PWRST_D0;
2645         /* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
2646         snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
2647         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
2648         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
2649         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
2650
2651         /* outputs */
2652         /* PW3 (27h), MW2 (1ah), AOW3 (bh) */
2653         parm = AC_PWRST_D3;
2654         set_pin_power_state(codec, 0x27, &parm);
2655         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE, parm);
2656         snd_hda_codec_write(codec, 0xb, 0, AC_VERB_SET_POWER_STATE, parm);
2657
2658         /* PW2 (26h), AOW2 (ah) */
2659         parm = AC_PWRST_D3;
2660         set_pin_power_state(codec, 0x26, &parm);
2661         if (spec->smart51_enabled)
2662                 set_pin_power_state(codec, 0x2b, &parm);
2663         snd_hda_codec_write(codec, 0xa, 0, AC_VERB_SET_POWER_STATE, parm);
2664
2665         /* PW0 (24h), AOW0 (8h) */
2666         parm = AC_PWRST_D3;
2667         set_pin_power_state(codec, 0x24, &parm);
2668         if (!spec->hp_independent_mode) /* check for redirected HP */
2669                 set_pin_power_state(codec, 0x28, &parm);
2670         snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
2671         /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
2672         snd_hda_codec_write(codec, 0x21, 0, AC_VERB_SET_POWER_STATE,
2673                             imux_is_smixer ? AC_PWRST_D0 : parm);
2674
2675         /* PW1 (25h), AOW1 (9h) */
2676         parm = AC_PWRST_D3;
2677         set_pin_power_state(codec, 0x25, &parm);
2678         if (spec->smart51_enabled)
2679                 set_pin_power_state(codec, 0x2a, &parm);
2680         snd_hda_codec_write(codec, 0x9, 0, AC_VERB_SET_POWER_STATE, parm);
2681
2682         if (spec->hp_independent_mode) {
2683                 /* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
2684                 parm = AC_PWRST_D3;
2685                 set_pin_power_state(codec, 0x28, &parm);
2686                 snd_hda_codec_write(codec, 0x1b, 0,
2687                                     AC_VERB_SET_POWER_STATE, parm);
2688                 snd_hda_codec_write(codec, 0x34, 0,
2689                                     AC_VERB_SET_POWER_STATE, parm);
2690                 snd_hda_codec_write(codec, 0xc, 0,
2691                                     AC_VERB_SET_POWER_STATE, parm);
2692         }
2693 }
2694
2695 static int patch_vt1718S(struct hda_codec *codec)
2696 {
2697         struct via_spec *spec;
2698         int err;
2699
2700         /* create a codec specific record */
2701         spec = via_new_spec(codec);
2702         if (spec == NULL)
2703                 return -ENOMEM;
2704
2705         spec->aa_mix_nid = 0x21;
2706         override_mic_boost(codec, 0x2b, 0, 3, 40);
2707         override_mic_boost(codec, 0x29, 0, 3, 40);
2708
2709         /* automatic parse from the BIOS config */
2710         err = via_parse_auto_config(codec);
2711         if (err < 0) {
2712                 via_free(codec);
2713                 return err;
2714         }
2715
2716         spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
2717
2718         codec->patch_ops = via_patch_ops;
2719
2720         spec->set_widgets_power_state =  set_widgets_power_state_vt1718S;
2721
2722         return 0;
2723 }
2724
2725 /* Patch for VT1716S */
2726
2727 static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
2728                             struct snd_ctl_elem_info *uinfo)
2729 {
2730         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2731         uinfo->count = 1;
2732         uinfo->value.integer.min = 0;
2733         uinfo->value.integer.max = 1;
2734         return 0;
2735 }
2736
2737 static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
2738                            struct snd_ctl_elem_value *ucontrol)
2739 {
2740         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2741         int index = 0;
2742
2743         index = snd_hda_codec_read(codec, 0x26, 0,
2744                                                AC_VERB_GET_CONNECT_SEL, 0);
2745         if (index != -1)
2746                 *ucontrol->value.integer.value = index;
2747
2748         return 0;
2749 }
2750
2751 static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
2752                            struct snd_ctl_elem_value *ucontrol)
2753 {
2754         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2755         struct via_spec *spec = codec->spec;
2756         int index = *ucontrol->value.integer.value;
2757
2758         snd_hda_codec_write(codec, 0x26, 0,
2759                                                AC_VERB_SET_CONNECT_SEL, index);
2760         spec->dmic_enabled = index;
2761         set_widgets_power_state(codec);
2762         return 1;
2763 }
2764
2765 static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
2766         HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
2767         {
2768          .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2769          .name = "Digital Mic Capture Switch",
2770          .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
2771          .count = 1,
2772          .info = vt1716s_dmic_info,
2773          .get = vt1716s_dmic_get,
2774          .put = vt1716s_dmic_put,
2775          },
2776         {}                      /* end */
2777 };
2778
2779
2780 /* mono-out mixer elements */
2781 static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
2782         HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
2783         { } /* end */
2784 };
2785
2786 static const struct hda_verb vt1716S_init_verbs[] = {
2787         /* Enable Boost Volume backdoor */
2788         {0x1, 0xf8a, 0x80},
2789         /* don't bybass mixer */
2790         {0x1, 0xf88, 0xc0},
2791         /* Enable mono output */
2792         {0x1, 0xf90, 0x08},
2793         { }
2794 };
2795
2796 static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
2797 {
2798         struct via_spec *spec = codec->spec;
2799         int imux_is_smixer;
2800         unsigned int parm;
2801         unsigned int mono_out, present;
2802         /* SW0 (17h) = stereo mixer */
2803         imux_is_smixer =
2804         (snd_hda_codec_read(codec, 0x17, 0,
2805                             AC_VERB_GET_CONNECT_SEL, 0x00) ==  5);
2806         /* inputs */
2807         /* PW 1/2/5 (1ah/1bh/1eh) */
2808         parm = AC_PWRST_D3;
2809         set_pin_power_state(codec, 0x1a, &parm);
2810         set_pin_power_state(codec, 0x1b, &parm);
2811         set_pin_power_state(codec, 0x1e, &parm);
2812         if (imux_is_smixer)
2813                 parm = AC_PWRST_D0;
2814         /* SW0 (17h), AIW0(13h) */
2815         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
2816         snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
2817
2818         parm = AC_PWRST_D3;
2819         set_pin_power_state(codec, 0x1e, &parm);
2820         /* PW11 (22h) */
2821         if (spec->dmic_enabled)
2822                 set_pin_power_state(codec, 0x22, &parm);
2823         else
2824                 snd_hda_codec_write(codec, 0x22, 0,
2825                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
2826
2827         /* SW2(26h), AIW1(14h) */
2828         snd_hda_codec_write(codec, 0x26, 0, AC_VERB_SET_POWER_STATE, parm);
2829         snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
2830
2831         /* outputs */
2832         /* PW0 (19h), SW1 (18h), AOW1 (11h) */
2833         parm = AC_PWRST_D3;
2834         set_pin_power_state(codec, 0x19, &parm);
2835         /* Smart 5.1 PW2(1bh) */
2836         if (spec->smart51_enabled)
2837                 set_pin_power_state(codec, 0x1b, &parm);
2838         snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
2839         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
2840
2841         /* PW7 (23h), SW3 (27h), AOW3 (25h) */
2842         parm = AC_PWRST_D3;
2843         set_pin_power_state(codec, 0x23, &parm);
2844         /* Smart 5.1 PW1(1ah) */
2845         if (spec->smart51_enabled)
2846                 set_pin_power_state(codec, 0x1a, &parm);
2847         snd_hda_codec_write(codec, 0x27, 0, AC_VERB_SET_POWER_STATE, parm);
2848
2849         /* Smart 5.1 PW5(1eh) */
2850         if (spec->smart51_enabled)
2851                 set_pin_power_state(codec, 0x1e, &parm);
2852         snd_hda_codec_write(codec, 0x25, 0, AC_VERB_SET_POWER_STATE, parm);
2853
2854         /* Mono out */
2855         /* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
2856         present = snd_hda_jack_detect(codec, 0x1c);
2857
2858         if (present)
2859                 mono_out = 0;
2860         else {
2861                 present = snd_hda_jack_detect(codec, 0x1d);
2862                 if (!spec->hp_independent_mode && present)
2863                         mono_out = 0;
2864                 else
2865                         mono_out = 1;
2866         }
2867         parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
2868         snd_hda_codec_write(codec, 0x28, 0, AC_VERB_SET_POWER_STATE, parm);
2869         snd_hda_codec_write(codec, 0x29, 0, AC_VERB_SET_POWER_STATE, parm);
2870         snd_hda_codec_write(codec, 0x2a, 0, AC_VERB_SET_POWER_STATE, parm);
2871
2872         /* PW 3/4 (1ch/1dh) */
2873         parm = AC_PWRST_D3;
2874         set_pin_power_state(codec, 0x1c, &parm);
2875         set_pin_power_state(codec, 0x1d, &parm);
2876         /* HP Independent Mode, power on AOW3 */
2877         if (spec->hp_independent_mode)
2878                 snd_hda_codec_write(codec, 0x25, 0,
2879                                     AC_VERB_SET_POWER_STATE, parm);
2880
2881         /* force to D0 for internal Speaker */
2882         /* MW0 (16h), AOW0 (10h) */
2883         snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
2884                             imux_is_smixer ? AC_PWRST_D0 : parm);
2885         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
2886                             mono_out ? AC_PWRST_D0 : parm);
2887 }
2888
2889 static int patch_vt1716S(struct hda_codec *codec)
2890 {
2891         struct via_spec *spec;
2892         int err;
2893
2894         /* create a codec specific record */
2895         spec = via_new_spec(codec);
2896         if (spec == NULL)
2897                 return -ENOMEM;
2898
2899         spec->aa_mix_nid = 0x16;
2900         override_mic_boost(codec, 0x1a, 0, 3, 40);
2901         override_mic_boost(codec, 0x1e, 0, 3, 40);
2902
2903         /* automatic parse from the BIOS config */
2904         err = via_parse_auto_config(codec);
2905         if (err < 0) {
2906                 via_free(codec);
2907                 return err;
2908         }
2909
2910         spec->init_verbs[spec->num_iverbs++]  = vt1716S_init_verbs;
2911
2912         spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
2913         spec->num_mixers++;
2914
2915         spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
2916
2917         codec->patch_ops = via_patch_ops;
2918
2919         spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
2920         return 0;
2921 }
2922
2923 /* for vt2002P */
2924
2925 static const struct hda_verb vt2002P_init_verbs[] = {
2926         /* Class-D speaker related verbs */
2927         {0x1, 0xfe0, 0x4},
2928         {0x1, 0xfe9, 0x80},
2929         {0x1, 0xfe2, 0x22},
2930         /* Enable Boost Volume backdoor */
2931         {0x1, 0xfb9, 0x24},
2932         /* Enable AOW0 to MW9 */
2933         {0x1, 0xfb8, 0x88},
2934         { }
2935 };
2936
2937 static const struct hda_verb vt1802_init_verbs[] = {
2938         /* Enable Boost Volume backdoor */
2939         {0x1, 0xfb9, 0x24},
2940         /* Enable AOW0 to MW9 */
2941         {0x1, 0xfb8, 0x88},
2942         { }
2943 };
2944
2945 static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
2946 {
2947         struct via_spec *spec = codec->spec;
2948         int imux_is_smixer;
2949         unsigned int parm;
2950         unsigned int present;
2951         /* MUX9 (1eh) = stereo mixer */
2952         imux_is_smixer =
2953         snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
2954         /* inputs */
2955         /* PW 5/6/7 (29h/2ah/2bh) */
2956         parm = AC_PWRST_D3;
2957         set_pin_power_state(codec, 0x29, &parm);
2958         set_pin_power_state(codec, 0x2a, &parm);
2959         set_pin_power_state(codec, 0x2b, &parm);
2960         parm = AC_PWRST_D0;
2961         /* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
2962         snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
2963         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
2964         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
2965         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
2966
2967         /* outputs */
2968         /* AOW0 (8h)*/
2969         snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
2970
2971         if (spec->codec_type == VT1802) {
2972                 /* PW4 (28h), MW4 (18h), MUX4(38h) */
2973                 parm = AC_PWRST_D3;
2974                 set_pin_power_state(codec, 0x28, &parm);
2975                 snd_hda_codec_write(codec, 0x18, 0,
2976                                     AC_VERB_SET_POWER_STATE, parm);
2977                 snd_hda_codec_write(codec, 0x38, 0,
2978                                     AC_VERB_SET_POWER_STATE, parm);
2979         } else {
2980                 /* PW4 (26h), MW4 (1ch), MUX4(37h) */
2981                 parm = AC_PWRST_D3;
2982                 set_pin_power_state(codec, 0x26, &parm);
2983                 snd_hda_codec_write(codec, 0x1c, 0,
2984                                     AC_VERB_SET_POWER_STATE, parm);
2985                 snd_hda_codec_write(codec, 0x37, 0,
2986                                     AC_VERB_SET_POWER_STATE, parm);
2987         }
2988
2989         if (spec->codec_type == VT1802) {
2990                 /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
2991                 parm = AC_PWRST_D3;
2992                 set_pin_power_state(codec, 0x25, &parm);
2993                 snd_hda_codec_write(codec, 0x15, 0,
2994                                     AC_VERB_SET_POWER_STATE, parm);
2995                 snd_hda_codec_write(codec, 0x35, 0,
2996                                     AC_VERB_SET_POWER_STATE, parm);
2997         } else {
2998                 /* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
2999                 parm = AC_PWRST_D3;
3000                 set_pin_power_state(codec, 0x25, &parm);
3001                 snd_hda_codec_write(codec, 0x19, 0,
3002                                     AC_VERB_SET_POWER_STATE, parm);
3003                 snd_hda_codec_write(codec, 0x35, 0,
3004                                     AC_VERB_SET_POWER_STATE, parm);
3005         }
3006
3007         if (spec->hp_independent_mode)
3008                 snd_hda_codec_write(codec, 0x9, 0,
3009                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3010
3011         /* Class-D */
3012         /* PW0 (24h), MW0(18h/14h), MUX0(34h) */
3013         present = snd_hda_jack_detect(codec, 0x25);
3014
3015         parm = AC_PWRST_D3;
3016         set_pin_power_state(codec, 0x24, &parm);
3017         parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
3018         if (spec->codec_type == VT1802)
3019                 snd_hda_codec_write(codec, 0x14, 0,
3020                                     AC_VERB_SET_POWER_STATE, parm);
3021         else
3022                 snd_hda_codec_write(codec, 0x18, 0,
3023                                     AC_VERB_SET_POWER_STATE, parm);
3024         snd_hda_codec_write(codec, 0x34, 0, AC_VERB_SET_POWER_STATE, parm);
3025
3026         /* Mono Out */
3027         present = snd_hda_jack_detect(codec, 0x26);
3028
3029         parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
3030         if (spec->codec_type == VT1802) {
3031                 /* PW15 (33h), MW8(1ch), MUX8(3ch) */
3032                 snd_hda_codec_write(codec, 0x33, 0,
3033                                     AC_VERB_SET_POWER_STATE, parm);
3034                 snd_hda_codec_write(codec, 0x1c, 0,
3035                                     AC_VERB_SET_POWER_STATE, parm);
3036                 snd_hda_codec_write(codec, 0x3c, 0,
3037                                     AC_VERB_SET_POWER_STATE, parm);
3038         } else {
3039                 /* PW15 (31h), MW8(17h), MUX8(3bh) */
3040                 snd_hda_codec_write(codec, 0x31, 0,
3041                                     AC_VERB_SET_POWER_STATE, parm);
3042                 snd_hda_codec_write(codec, 0x17, 0,
3043                                     AC_VERB_SET_POWER_STATE, parm);
3044                 snd_hda_codec_write(codec, 0x3b, 0,
3045                                     AC_VERB_SET_POWER_STATE, parm);
3046         }
3047         /* MW9 (21h) */
3048         if (imux_is_smixer || !is_aa_path_mute(codec))
3049                 snd_hda_codec_write(codec, 0x21, 0,
3050                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3051         else
3052                 snd_hda_codec_write(codec, 0x21, 0,
3053                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
3054 }
3055
3056 /* patch for vt2002P */
3057 static int patch_vt2002P(struct hda_codec *codec)
3058 {
3059         struct via_spec *spec;
3060         int err;
3061
3062         /* create a codec specific record */
3063         spec = via_new_spec(codec);
3064         if (spec == NULL)
3065                 return -ENOMEM;
3066
3067         spec->aa_mix_nid = 0x21;
3068         override_mic_boost(codec, 0x2b, 0, 3, 40);
3069         override_mic_boost(codec, 0x29, 0, 3, 40);
3070
3071         /* automatic parse from the BIOS config */
3072         err = via_parse_auto_config(codec);
3073         if (err < 0) {
3074                 via_free(codec);
3075                 return err;
3076         }
3077
3078         if (spec->codec_type == VT1802)
3079                 spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
3080         else
3081                 spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
3082
3083         codec->patch_ops = via_patch_ops;
3084
3085         spec->set_widgets_power_state =  set_widgets_power_state_vt2002P;
3086         return 0;
3087 }
3088
3089 /* for vt1812 */
3090
3091 static const struct hda_verb vt1812_init_verbs[] = {
3092         /* Enable Boost Volume backdoor */
3093         {0x1, 0xfb9, 0x24},
3094         /* Enable AOW0 to MW9 */
3095         {0x1, 0xfb8, 0xa8},
3096         { }
3097 };
3098
3099 static void set_widgets_power_state_vt1812(struct hda_codec *codec)
3100 {
3101         struct via_spec *spec = codec->spec;
3102         int imux_is_smixer =
3103         snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
3104         unsigned int parm;
3105         unsigned int present;
3106         /* MUX10 (1eh) = stereo mixer */
3107         imux_is_smixer =
3108         snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
3109         /* inputs */
3110         /* PW 5/6/7 (29h/2ah/2bh) */
3111         parm = AC_PWRST_D3;
3112         set_pin_power_state(codec, 0x29, &parm);
3113         set_pin_power_state(codec, 0x2a, &parm);
3114         set_pin_power_state(codec, 0x2b, &parm);
3115         parm = AC_PWRST_D0;
3116         /* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
3117         snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
3118         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
3119         snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
3120         snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
3121
3122         /* outputs */
3123         /* AOW0 (8h)*/
3124         snd_hda_codec_write(codec, 0x8, 0,
3125                             AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3126
3127         /* PW4 (28h), MW4 (18h), MUX4(38h) */
3128         parm = AC_PWRST_D3;
3129         set_pin_power_state(codec, 0x28, &parm);
3130         snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
3131         snd_hda_codec_write(codec, 0x38, 0, AC_VERB_SET_POWER_STATE, parm);
3132
3133         /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
3134         parm = AC_PWRST_D3;
3135         set_pin_power_state(codec, 0x25, &parm);
3136         snd_hda_codec_write(codec, 0x15, 0, AC_VERB_SET_POWER_STATE, parm);
3137         snd_hda_codec_write(codec, 0x35, 0, AC_VERB_SET_POWER_STATE, parm);
3138         if (spec->hp_independent_mode)
3139                 snd_hda_codec_write(codec, 0x9, 0,
3140                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3141
3142         /* Internal Speaker */
3143         /* PW0 (24h), MW0(14h), MUX0(34h) */
3144         present = snd_hda_jack_detect(codec, 0x25);
3145
3146         parm = AC_PWRST_D3;
3147         set_pin_power_state(codec, 0x24, &parm);
3148         if (present) {
3149                 snd_hda_codec_write(codec, 0x14, 0,
3150                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
3151                 snd_hda_codec_write(codec, 0x34, 0,
3152                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
3153         } else {
3154                 snd_hda_codec_write(codec, 0x14, 0,
3155                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3156                 snd_hda_codec_write(codec, 0x34, 0,
3157                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3158         }
3159
3160
3161         /* Mono Out */
3162         /* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
3163         present = snd_hda_jack_detect(codec, 0x28);
3164
3165         parm = AC_PWRST_D3;
3166         set_pin_power_state(codec, 0x31, &parm);
3167         if (present) {
3168                 snd_hda_codec_write(codec, 0x1c, 0,
3169                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
3170                 snd_hda_codec_write(codec, 0x3c, 0,
3171                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
3172                 snd_hda_codec_write(codec, 0x3e, 0,
3173                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
3174         } else {
3175                 snd_hda_codec_write(codec, 0x1c, 0,
3176                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3177                 snd_hda_codec_write(codec, 0x3c, 0,
3178                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3179                 snd_hda_codec_write(codec, 0x3e, 0,
3180                                     AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3181         }
3182
3183         /* PW15 (33h), MW15 (1dh), MUX15(3dh) */
3184         parm = AC_PWRST_D3;
3185         set_pin_power_state(codec, 0x33, &parm);
3186         snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
3187         snd_hda_codec_write(codec, 0x3d, 0, AC_VERB_SET_POWER_STATE, parm);
3188
3189 }
3190
3191 /* patch for vt1812 */
3192 static int patch_vt1812(struct hda_codec *codec)
3193 {
3194         struct via_spec *spec;
3195         int err;
3196
3197         /* create a codec specific record */
3198         spec = via_new_spec(codec);
3199         if (spec == NULL)
3200                 return -ENOMEM;
3201
3202         spec->aa_mix_nid = 0x21;
3203         override_mic_boost(codec, 0x2b, 0, 3, 40);
3204         override_mic_boost(codec, 0x29, 0, 3, 40);
3205
3206         /* automatic parse from the BIOS config */
3207         err = via_parse_auto_config(codec);
3208         if (err < 0) {
3209                 via_free(codec);
3210                 return err;
3211         }
3212
3213         spec->init_verbs[spec->num_iverbs++]  = vt1812_init_verbs;
3214
3215         codec->patch_ops = via_patch_ops;
3216
3217         spec->set_widgets_power_state =  set_widgets_power_state_vt1812;
3218         return 0;
3219 }
3220
3221 /*
3222  * patch entries
3223  */
3224 static const struct hda_codec_preset snd_hda_preset_via[] = {
3225         { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
3226         { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
3227         { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
3228         { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
3229         { .id = 0x1106e710, .name = "VT1709 10-Ch",
3230           .patch = patch_vt1709},
3231         { .id = 0x1106e711, .name = "VT1709 10-Ch",
3232           .patch = patch_vt1709},
3233         { .id = 0x1106e712, .name = "VT1709 10-Ch",
3234           .patch = patch_vt1709},
3235         { .id = 0x1106e713, .name = "VT1709 10-Ch",
3236           .patch = patch_vt1709},
3237         { .id = 0x1106e714, .name = "VT1709 6-Ch",
3238           .patch = patch_vt1709},
3239         { .id = 0x1106e715, .name = "VT1709 6-Ch",
3240           .patch = patch_vt1709},
3241         { .id = 0x1106e716, .name = "VT1709 6-Ch",
3242           .patch = patch_vt1709},
3243         { .id = 0x1106e717, .name = "VT1709 6-Ch",
3244           .patch = patch_vt1709},
3245         { .id = 0x1106e720, .name = "VT1708B 8-Ch",
3246           .patch = patch_vt1708B},
3247         { .id = 0x1106e721, .name = "VT1708B 8-Ch",
3248           .patch = patch_vt1708B},
3249         { .id = 0x1106e722, .name = "VT1708B 8-Ch",
3250           .patch = patch_vt1708B},
3251         { .id = 0x1106e723, .name = "VT1708B 8-Ch",
3252           .patch = patch_vt1708B},
3253         { .id = 0x1106e724, .name = "VT1708B 4-Ch",
3254           .patch = patch_vt1708B},
3255         { .id = 0x1106e725, .name = "VT1708B 4-Ch",
3256           .patch = patch_vt1708B},
3257         { .id = 0x1106e726, .name = "VT1708B 4-Ch",
3258           .patch = patch_vt1708B},
3259         { .id = 0x1106e727, .name = "VT1708B 4-Ch",
3260           .patch = patch_vt1708B},
3261         { .id = 0x11060397, .name = "VT1708S",
3262           .patch = patch_vt1708S},
3263         { .id = 0x11061397, .name = "VT1708S",
3264           .patch = patch_vt1708S},
3265         { .id = 0x11062397, .name = "VT1708S",
3266           .patch = patch_vt1708S},
3267         { .id = 0x11063397, .name = "VT1708S",
3268           .patch = patch_vt1708S},
3269         { .id = 0x11064397, .name = "VT1705",
3270           .patch = patch_vt1708S},
3271         { .id = 0x11065397, .name = "VT1708S",
3272           .patch = patch_vt1708S},
3273         { .id = 0x11066397, .name = "VT1708S",
3274           .patch = patch_vt1708S},
3275         { .id = 0x11067397, .name = "VT1708S",
3276           .patch = patch_vt1708S},
3277         { .id = 0x11060398, .name = "VT1702",
3278           .patch = patch_vt1702},
3279         { .id = 0x11061398, .name = "VT1702",
3280           .patch = patch_vt1702},
3281         { .id = 0x11062398, .name = "VT1702",
3282           .patch = patch_vt1702},
3283         { .id = 0x11063398, .name = "VT1702",
3284           .patch = patch_vt1702},
3285         { .id = 0x11064398, .name = "VT1702",
3286           .patch = patch_vt1702},
3287         { .id = 0x11065398, .name = "VT1702",
3288           .patch = patch_vt1702},
3289         { .id = 0x11066398, .name = "VT1702",
3290           .patch = patch_vt1702},
3291         { .id = 0x11067398, .name = "VT1702",
3292           .patch = patch_vt1702},
3293         { .id = 0x11060428, .name = "VT1718S",
3294           .patch = patch_vt1718S},
3295         { .id = 0x11064428, .name = "VT1718S",
3296           .patch = patch_vt1718S},
3297         { .id = 0x11060441, .name = "VT2020",
3298           .patch = patch_vt1718S},
3299         { .id = 0x11064441, .name = "VT1828S",
3300           .patch = patch_vt1718S},
3301         { .id = 0x11060433, .name = "VT1716S",
3302           .patch = patch_vt1716S},
3303         { .id = 0x1106a721, .name = "VT1716S",
3304           .patch = patch_vt1716S},
3305         { .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
3306         { .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
3307         { .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
3308         { .id = 0x11060440, .name = "VT1818S",
3309           .patch = patch_vt1708S},
3310         { .id = 0x11060446, .name = "VT1802",
3311                 .patch = patch_vt2002P},
3312         { .id = 0x11068446, .name = "VT1802",
3313                 .patch = patch_vt2002P},
3314         {} /* terminator */
3315 };
3316
3317 MODULE_ALIAS("snd-hda-codec-id:1106*");
3318
3319 static struct hda_codec_preset_list via_list = {
3320         .preset = snd_hda_preset_via,
3321         .owner = THIS_MODULE,
3322 };
3323
3324 MODULE_LICENSE("GPL");
3325 MODULE_DESCRIPTION("VIA HD-audio codec");
3326
3327 static int __init patch_via_init(void)
3328 {
3329         return snd_hda_add_codec_preset(&via_list);
3330 }
3331
3332 static void __exit patch_via_exit(void)
3333 {
3334         snd_hda_delete_codec_preset(&via_list);
3335 }
3336
3337 module_init(patch_via_init)
3338 module_exit(patch_via_exit)