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