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