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