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