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