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