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