ALSA: hda - Remove superfluous NID_MAPPING use for smart51 mixer
[deliverable/linux.git] / sound / pci / hda / patch_via.c
CommitLineData
c577b8a1
JC
1/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
8e86597f 4 * HD audio interface patch for VIA VT17xx/VT18xx/VT20xx codec
c577b8a1 5 *
8e86597f
LW
6 * (C) 2006-2009 VIA Technology, Inc.
7 * (C) 2006-2008 Takashi Iwai <tiwai@suse.de>
c577b8a1
JC
8 *
9 * This driver is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This driver is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24/* * * * * * * * * * * * * * Release History * * * * * * * * * * * * * * * * */
377ff31a 25/* */
c577b8a1 26/* 2006-03-03 Lydia Wang Create the basic patch to support VT1708 codec */
377ff31a
LW
27/* 2006-03-14 Lydia Wang Modify hard code for some pin widget nid */
28/* 2006-08-02 Lydia Wang Add support to VT1709 codec */
c577b8a1 29/* 2006-09-08 Lydia Wang Fix internal loopback recording source select bug */
377ff31a
LW
30/* 2007-09-12 Lydia Wang Add EAPD enable during driver initialization */
31/* 2007-09-17 Lydia Wang Add VT1708B codec support */
76d9b0dd 32/* 2007-11-14 Lydia Wang Add VT1708A codec HP and CD pin connect config */
fb4cb772 33/* 2008-02-03 Lydia Wang Fix Rear channels and Back channels inverse issue */
377ff31a
LW
34/* 2008-03-06 Lydia Wang Add VT1702 codec and VT1708S codec support */
35/* 2008-04-09 Lydia Wang Add mute front speaker when HP plugin */
36/* 2008-04-09 Lydia Wang Add Independent HP feature */
98aa34c0 37/* 2008-05-28 Lydia Wang Add second S/PDIF Out support for VT1702 */
377ff31a 38/* 2008-09-15 Logan Li Add VT1708S Mic Boost workaround/backdoor */
8e86597f
LW
39/* 2009-02-16 Logan Li Add support for VT1718S */
40/* 2009-03-13 Logan Li Add support for VT1716S */
41/* 2009-04-14 Lydai Wang Add support for VT1828S and VT2020 */
42/* 2009-07-08 Lydia Wang Add support for VT2002P */
43/* 2009-07-21 Lydia Wang Add support for VT1812 */
36dd5c4a 44/* 2009-09-19 Lydia Wang Add support for VT1818S */
377ff31a 45/* */
c577b8a1
JC
46/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
47
48
c577b8a1
JC
49#include <linux/init.h>
50#include <linux/delay.h>
51#include <linux/slab.h>
c577b8a1 52#include <sound/core.h>
0aa62aef 53#include <sound/asoundef.h>
c577b8a1
JC
54#include "hda_codec.h"
55#include "hda_local.h"
c577b8a1 56
5b0cb1d8
JK
57#define NID_MAPPING (-1)
58
c577b8a1
JC
59/* amp values */
60#define AMP_VAL_IDX_SHIFT 19
61#define AMP_VAL_IDX_MASK (0x0f<<19)
62
c577b8a1
JC
63/* Pin Widget NID */
64#define VT1708_HP_NID 0x13
65#define VT1708_DIGOUT_NID 0x14
66#define VT1708_DIGIN_NID 0x16
f7278fd0 67#define VT1708_DIGIN_PIN 0x26
d949cac1
HW
68#define VT1708_HP_PIN_NID 0x20
69#define VT1708_CD_PIN_NID 0x24
c577b8a1
JC
70
71#define VT1709_HP_DAC_NID 0x28
72#define VT1709_DIGOUT_NID 0x13
73#define VT1709_DIGIN_NID 0x17
f7278fd0
JC
74#define VT1709_DIGIN_PIN 0x25
75
76#define VT1708B_HP_NID 0x25
77#define VT1708B_DIGOUT_NID 0x12
78#define VT1708B_DIGIN_NID 0x15
79#define VT1708B_DIGIN_PIN 0x21
c577b8a1 80
d949cac1
HW
81#define VT1708S_HP_NID 0x25
82#define VT1708S_DIGOUT_NID 0x12
83
84#define VT1702_HP_NID 0x17
85#define VT1702_DIGOUT_NID 0x11
86
d7426329
HW
87enum VIA_HDA_CODEC {
88 UNKNOWN = -1,
89 VT1708,
90 VT1709_10CH,
91 VT1709_6CH,
92 VT1708B_8CH,
93 VT1708B_4CH,
94 VT1708S,
518bf3ba 95 VT1708BCE,
d7426329 96 VT1702,
eb7188ca 97 VT1718S,
f3db423d 98 VT1716S,
25eaba2f 99 VT2002P,
ab6734e7 100 VT1812,
11890956 101 VT1802,
d7426329
HW
102 CODEC_TYPES,
103};
104
11890956
LW
105#define VT2002P_COMPATIBLE(spec) \
106 ((spec)->codec_type == VT2002P ||\
107 (spec)->codec_type == VT1812 ||\
108 (spec)->codec_type == VT1802)
109
1f2e99fe
LW
110struct via_spec {
111 /* codec parameterization */
90dd48a1 112 const struct snd_kcontrol_new *mixers[6];
1f2e99fe
LW
113 unsigned int num_mixers;
114
90dd48a1 115 const struct hda_verb *init_verbs[5];
1f2e99fe
LW
116 unsigned int num_iverbs;
117
118 char *stream_name_analog;
90dd48a1
TI
119 const struct hda_pcm_stream *stream_analog_playback;
120 const struct hda_pcm_stream *stream_analog_capture;
1f2e99fe
LW
121
122 char *stream_name_digital;
90dd48a1
TI
123 const struct hda_pcm_stream *stream_digital_playback;
124 const struct hda_pcm_stream *stream_digital_capture;
1f2e99fe
LW
125
126 /* playback */
127 struct hda_multi_out multiout;
128 hda_nid_t slave_dig_outs[2];
129
130 /* capture */
131 unsigned int num_adc_nids;
90dd48a1 132 const hda_nid_t *adc_nids;
1f2e99fe
LW
133 hda_nid_t mux_nids[3];
134 hda_nid_t dig_in_nid;
135 hda_nid_t dig_in_pin;
136
137 /* capture source */
138 const struct hda_input_mux *input_mux;
139 unsigned int cur_mux[3];
140
141 /* PCM information */
142 struct hda_pcm pcm_rec[3];
143
144 /* dynamic controls, init_verbs and input_mux */
145 struct auto_pin_cfg autocfg;
146 struct snd_array kctls;
147 struct hda_input_mux private_imux[2];
148 hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
149
150 /* HP mode source */
151 const struct hda_input_mux *hp_mux;
152 unsigned int hp_independent_mode;
153 unsigned int hp_independent_mode_index;
154 unsigned int smart51_enabled;
f3db423d 155 unsigned int dmic_enabled;
1f2e99fe
LW
156 enum VIA_HDA_CODEC codec_type;
157
158 /* work to check hp jack state */
159 struct hda_codec *codec;
160 struct delayed_work vt1708_hp_work;
161 int vt1708_jack_detectect;
162 int vt1708_hp_present;
3e95b9ab
LW
163
164 void (*set_widgets_power_state)(struct hda_codec *codec);
165
1f2e99fe
LW
166#ifdef CONFIG_SND_HDA_POWER_SAVE
167 struct hda_loopback_check loopback;
168#endif
169};
170
0341ccd7 171static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
5b0cb1d8
JK
172static struct via_spec * via_new_spec(struct hda_codec *codec)
173{
174 struct via_spec *spec;
175
176 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
177 if (spec == NULL)
178 return NULL;
179
180 codec->spec = spec;
181 spec->codec = codec;
0341ccd7
LW
182 spec->codec_type = get_codec_type(codec);
183 /* VT1708BCE & VT1708S are almost same */
184 if (spec->codec_type == VT1708BCE)
185 spec->codec_type = VT1708S;
5b0cb1d8
JK
186 return spec;
187}
188
744ff5f4 189static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
d7426329 190{
744ff5f4 191 u32 vendor_id = codec->vendor_id;
d7426329
HW
192 u16 ven_id = vendor_id >> 16;
193 u16 dev_id = vendor_id & 0xffff;
194 enum VIA_HDA_CODEC codec_type;
195
196 /* get codec type */
197 if (ven_id != 0x1106)
198 codec_type = UNKNOWN;
199 else if (dev_id >= 0x1708 && dev_id <= 0x170b)
200 codec_type = VT1708;
201 else if (dev_id >= 0xe710 && dev_id <= 0xe713)
202 codec_type = VT1709_10CH;
203 else if (dev_id >= 0xe714 && dev_id <= 0xe717)
204 codec_type = VT1709_6CH;
518bf3ba 205 else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
d7426329 206 codec_type = VT1708B_8CH;
518bf3ba
LW
207 if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
208 codec_type = VT1708BCE;
209 } else if (dev_id >= 0xe724 && dev_id <= 0xe727)
d7426329
HW
210 codec_type = VT1708B_4CH;
211 else if ((dev_id & 0xfff) == 0x397
212 && (dev_id >> 12) < 8)
213 codec_type = VT1708S;
214 else if ((dev_id & 0xfff) == 0x398
215 && (dev_id >> 12) < 8)
216 codec_type = VT1702;
eb7188ca
LW
217 else if ((dev_id & 0xfff) == 0x428
218 && (dev_id >> 12) < 8)
219 codec_type = VT1718S;
f3db423d
LW
220 else if (dev_id == 0x0433 || dev_id == 0xa721)
221 codec_type = VT1716S;
bb3c6bfc
LW
222 else if (dev_id == 0x0441 || dev_id == 0x4441)
223 codec_type = VT1718S;
25eaba2f
LW
224 else if (dev_id == 0x0438 || dev_id == 0x4438)
225 codec_type = VT2002P;
ab6734e7
LW
226 else if (dev_id == 0x0448)
227 codec_type = VT1812;
36dd5c4a
LW
228 else if (dev_id == 0x0440)
229 codec_type = VT1708S;
11890956
LW
230 else if ((dev_id & 0xfff) == 0x446)
231 codec_type = VT1802;
d7426329
HW
232 else
233 codec_type = UNKNOWN;
234 return codec_type;
235};
236
ec7e7e42 237#define VIA_JACK_EVENT 0x20
69e52a80
HW
238#define VIA_HP_EVENT 0x01
239#define VIA_GPIO_EVENT 0x02
ec7e7e42
LW
240#define VIA_MONO_EVENT 0x03
241#define VIA_SPEAKER_EVENT 0x04
242#define VIA_BIND_HP_EVENT 0x05
69e52a80 243
c577b8a1
JC
244enum {
245 VIA_CTL_WIDGET_VOL,
246 VIA_CTL_WIDGET_MUTE,
f5271101 247 VIA_CTL_WIDGET_ANALOG_MUTE,
25eaba2f 248 VIA_CTL_WIDGET_BIND_PIN_MUTE,
c577b8a1
JC
249};
250
251enum {
eb14a46c 252 AUTO_SEQ_FRONT = 0,
c577b8a1
JC
253 AUTO_SEQ_SURROUND,
254 AUTO_SEQ_CENLFE,
255 AUTO_SEQ_SIDE
256};
257
f5271101 258static void analog_low_current_mode(struct hda_codec *codec, int stream_idle);
1f2e99fe
LW
259static int is_aa_path_mute(struct hda_codec *codec);
260
261static void vt1708_start_hp_work(struct via_spec *spec)
262{
263 if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
264 return;
265 snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
266 !spec->vt1708_jack_detectect);
267 if (!delayed_work_pending(&spec->vt1708_hp_work))
268 schedule_delayed_work(&spec->vt1708_hp_work,
269 msecs_to_jiffies(100));
270}
271
272static void vt1708_stop_hp_work(struct via_spec *spec)
273{
274 if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
275 return;
276 if (snd_hda_get_bool_hint(spec->codec, "analog_loopback_hp_detect") == 1
277 && !is_aa_path_mute(spec->codec))
278 return;
279 snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
280 !spec->vt1708_jack_detectect);
5b84ba26 281 cancel_delayed_work_sync(&spec->vt1708_hp_work);
1f2e99fe 282}
f5271101 283
3e95b9ab
LW
284static void set_widgets_power_state(struct hda_codec *codec)
285{
286 struct via_spec *spec = codec->spec;
287 if (spec->set_widgets_power_state)
288 spec->set_widgets_power_state(codec);
289}
25eaba2f 290
f5271101
LW
291static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
292 struct snd_ctl_elem_value *ucontrol)
293{
294 int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
295 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
296
3e95b9ab 297 set_widgets_power_state(codec);
f5271101 298 analog_low_current_mode(snd_kcontrol_chip(kcontrol), -1);
1f2e99fe
LW
299 if (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1) {
300 if (is_aa_path_mute(codec))
301 vt1708_start_hp_work(codec->spec);
302 else
303 vt1708_stop_hp_work(codec->spec);
304 }
f5271101
LW
305 return change;
306}
307
308/* modify .put = snd_hda_mixer_amp_switch_put */
309#define ANALOG_INPUT_MUTE \
310 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
311 .name = NULL, \
312 .index = 0, \
313 .info = snd_hda_mixer_amp_switch_info, \
314 .get = snd_hda_mixer_amp_switch_get, \
315 .put = analog_input_switch_put, \
316 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
317
25eaba2f
LW
318static void via_hp_bind_automute(struct hda_codec *codec);
319
320static int bind_pin_switch_put(struct snd_kcontrol *kcontrol,
321 struct snd_ctl_elem_value *ucontrol)
322{
323 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
324 struct via_spec *spec = codec->spec;
325 int i;
326 int change = 0;
327
328 long *valp = ucontrol->value.integer.value;
329 int lmute, rmute;
330 if (strstr(kcontrol->id.name, "Switch") == NULL) {
331 snd_printd("Invalid control!\n");
332 return change;
333 }
334 change = snd_hda_mixer_amp_switch_put(kcontrol,
335 ucontrol);
336 /* Get mute value */
337 lmute = *valp ? 0 : HDA_AMP_MUTE;
338 valp++;
339 rmute = *valp ? 0 : HDA_AMP_MUTE;
340
341 /* Set hp pins */
342 if (!spec->hp_independent_mode) {
343 for (i = 0; i < spec->autocfg.hp_outs; i++) {
344 snd_hda_codec_amp_update(
345 codec, spec->autocfg.hp_pins[i],
346 0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
347 lmute);
348 snd_hda_codec_amp_update(
349 codec, spec->autocfg.hp_pins[i],
350 1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
351 rmute);
352 }
353 }
354
355 if (!lmute && !rmute) {
356 /* Line Outs */
357 for (i = 0; i < spec->autocfg.line_outs; i++)
358 snd_hda_codec_amp_stereo(
359 codec, spec->autocfg.line_out_pins[i],
360 HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
361 /* Speakers */
362 for (i = 0; i < spec->autocfg.speaker_outs; i++)
363 snd_hda_codec_amp_stereo(
364 codec, spec->autocfg.speaker_pins[i],
365 HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
366 /* unmute */
367 via_hp_bind_automute(codec);
368
369 } else {
370 if (lmute) {
371 /* Mute all left channels */
372 for (i = 1; i < spec->autocfg.line_outs; i++)
373 snd_hda_codec_amp_update(
374 codec,
375 spec->autocfg.line_out_pins[i],
376 0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
377 lmute);
378 for (i = 0; i < spec->autocfg.speaker_outs; i++)
379 snd_hda_codec_amp_update(
380 codec,
381 spec->autocfg.speaker_pins[i],
382 0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
383 lmute);
384 }
385 if (rmute) {
386 /* mute all right channels */
387 for (i = 1; i < spec->autocfg.line_outs; i++)
388 snd_hda_codec_amp_update(
389 codec,
390 spec->autocfg.line_out_pins[i],
391 1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
392 rmute);
393 for (i = 0; i < spec->autocfg.speaker_outs; i++)
394 snd_hda_codec_amp_update(
395 codec,
396 spec->autocfg.speaker_pins[i],
397 1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
398 rmute);
399 }
400 }
401 return change;
402}
403
404#define BIND_PIN_MUTE \
405 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
406 .name = NULL, \
407 .index = 0, \
408 .info = snd_hda_mixer_amp_switch_info, \
409 .get = snd_hda_mixer_amp_switch_get, \
410 .put = bind_pin_switch_put, \
411 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
412
90dd48a1 413static const struct snd_kcontrol_new via_control_templates[] = {
c577b8a1
JC
414 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
415 HDA_CODEC_MUTE(NULL, 0, 0, 0),
f5271101 416 ANALOG_INPUT_MUTE,
25eaba2f 417 BIND_PIN_MUTE,
c577b8a1
JC
418};
419
90dd48a1 420static const hda_nid_t vt1708_adc_nids[2] = {
c577b8a1
JC
421 /* ADC1-2 */
422 0x15, 0x27
423};
424
90dd48a1 425static const hda_nid_t vt1709_adc_nids[3] = {
c577b8a1
JC
426 /* ADC1-2 */
427 0x14, 0x15, 0x16
428};
429
90dd48a1 430static const hda_nid_t vt1708B_adc_nids[2] = {
f7278fd0
JC
431 /* ADC1-2 */
432 0x13, 0x14
433};
434
90dd48a1 435static const hda_nid_t vt1708S_adc_nids[2] = {
d949cac1
HW
436 /* ADC1-2 */
437 0x13, 0x14
438};
439
90dd48a1 440static const hda_nid_t vt1702_adc_nids[3] = {
d949cac1
HW
441 /* ADC1-2 */
442 0x12, 0x20, 0x1F
443};
444
90dd48a1 445static const hda_nid_t vt1718S_adc_nids[2] = {
eb7188ca
LW
446 /* ADC1-2 */
447 0x10, 0x11
448};
449
90dd48a1 450static const hda_nid_t vt1716S_adc_nids[2] = {
f3db423d
LW
451 /* ADC1-2 */
452 0x13, 0x14
453};
454
90dd48a1 455static const hda_nid_t vt2002P_adc_nids[2] = {
25eaba2f
LW
456 /* ADC1-2 */
457 0x10, 0x11
458};
459
90dd48a1 460static const hda_nid_t vt1812_adc_nids[2] = {
ab6734e7
LW
461 /* ADC1-2 */
462 0x10, 0x11
463};
464
465
c577b8a1 466/* add dynamic controls */
7b315bb4
TI
467static int __via_add_control(struct via_spec *spec, int type, const char *name,
468 int idx, unsigned long val)
c577b8a1
JC
469{
470 struct snd_kcontrol_new *knew;
471
603c4019
TI
472 snd_array_init(&spec->kctls, sizeof(*knew), 32);
473 knew = snd_array_new(&spec->kctls);
474 if (!knew)
475 return -ENOMEM;
71eb7dcc 476 *knew = via_control_templates[type];
c577b8a1 477 knew->name = kstrdup(name, GFP_KERNEL);
c577b8a1
JC
478 if (!knew->name)
479 return -ENOMEM;
4d02d1b6 480 if (get_amp_nid_(val))
5e26dfd0 481 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
c577b8a1 482 knew->private_value = val;
c577b8a1
JC
483 return 0;
484}
485
7b315bb4
TI
486#define via_add_control(spec, type, name, val) \
487 __via_add_control(spec, type, name, 0, val)
488
5b0cb1d8 489static struct snd_kcontrol_new *via_clone_control(struct via_spec *spec,
90dd48a1 490 const struct snd_kcontrol_new *tmpl)
5b0cb1d8
JK
491{
492 struct snd_kcontrol_new *knew;
493
494 snd_array_init(&spec->kctls, sizeof(*knew), 32);
495 knew = snd_array_new(&spec->kctls);
496 if (!knew)
497 return NULL;
498 *knew = *tmpl;
499 knew->name = kstrdup(tmpl->name, GFP_KERNEL);
500 if (!knew->name)
501 return NULL;
b331439d 502 return knew;
5b0cb1d8
JK
503}
504
603c4019
TI
505static void via_free_kctls(struct hda_codec *codec)
506{
507 struct via_spec *spec = codec->spec;
508
509 if (spec->kctls.list) {
510 struct snd_kcontrol_new *kctl = spec->kctls.list;
511 int i;
512 for (i = 0; i < spec->kctls.used; i++)
513 kfree(kctl[i].name);
514 }
515 snd_array_free(&spec->kctls);
516}
517
c577b8a1 518/* create input playback/capture controls for the given pin */
9510e8dd 519static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
7b315bb4 520 int type_idx, int idx, int mix_nid)
c577b8a1
JC
521{
522 char name[32];
523 int err;
524
525 sprintf(name, "%s Playback Volume", ctlname);
7b315bb4 526 err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
c577b8a1
JC
527 HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
528 if (err < 0)
529 return err;
530 sprintf(name, "%s Playback Switch", ctlname);
7b315bb4 531 err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
c577b8a1
JC
532 HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
533 if (err < 0)
534 return err;
535 return 0;
536}
537
538static void via_auto_set_output_and_unmute(struct hda_codec *codec,
539 hda_nid_t nid, int pin_type,
540 int dac_idx)
541{
542 /* set as output */
543 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
544 pin_type);
545 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
546 AMP_OUT_UNMUTE);
d3a11e60 547 if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
377ff31a 548 snd_hda_codec_write(codec, nid, 0,
d3a11e60 549 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
c577b8a1
JC
550}
551
552
553static void via_auto_init_multi_out(struct hda_codec *codec)
554{
555 struct via_spec *spec = codec->spec;
556 int i;
557
558 for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
559 hda_nid_t nid = spec->autocfg.line_out_pins[i];
560 if (nid)
561 via_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
562 }
563}
564
565static void via_auto_init_hp_out(struct hda_codec *codec)
566{
567 struct via_spec *spec = codec->spec;
568 hda_nid_t pin;
25eaba2f 569 int i;
c577b8a1 570
25eaba2f
LW
571 for (i = 0; i < spec->autocfg.hp_outs; i++) {
572 pin = spec->autocfg.hp_pins[i];
573 if (pin) /* connect to front */
574 via_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
575 }
c577b8a1
JC
576}
577
32e0191d
CL
578static int is_smart51_pins(struct via_spec *spec, hda_nid_t pin);
579
c577b8a1
JC
580static void via_auto_init_analog_input(struct hda_codec *codec)
581{
582 struct via_spec *spec = codec->spec;
7b315bb4 583 const struct auto_pin_cfg *cfg = &spec->autocfg;
32e0191d 584 unsigned int ctl;
c577b8a1
JC
585 int i;
586
7b315bb4
TI
587 for (i = 0; i < cfg->num_inputs; i++) {
588 hda_nid_t nid = cfg->inputs[i].pin;
32e0191d
CL
589 if (spec->smart51_enabled && is_smart51_pins(spec, nid))
590 ctl = PIN_OUT;
30649676 591 else if (cfg->inputs[i].type == AUTO_PIN_MIC)
32e0191d
CL
592 ctl = PIN_VREF50;
593 else
594 ctl = PIN_IN;
c577b8a1 595 snd_hda_codec_write(codec, nid, 0,
32e0191d 596 AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
c577b8a1
JC
597 }
598}
f5271101
LW
599
600static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
601 unsigned int *affected_parm)
602{
603 unsigned parm;
604 unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
605 unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
606 >> AC_DEFCFG_MISC_SHIFT
607 & AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
d56757ab 608 unsigned present = snd_hda_jack_detect(codec, nid);
1564b287
LW
609 struct via_spec *spec = codec->spec;
610 if ((spec->smart51_enabled && is_smart51_pins(spec, nid))
611 || ((no_presence || present)
612 && get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
f5271101
LW
613 *affected_parm = AC_PWRST_D0; /* if it's connected */
614 parm = AC_PWRST_D0;
615 } else
616 parm = AC_PWRST_D3;
617
618 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
619}
620
c577b8a1
JC
621/*
622 * input MUX handling
623 */
624static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
625 struct snd_ctl_elem_info *uinfo)
626{
627 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
628 struct via_spec *spec = codec->spec;
629 return snd_hda_input_mux_info(spec->input_mux, uinfo);
630}
631
632static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
633 struct snd_ctl_elem_value *ucontrol)
634{
635 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
636 struct via_spec *spec = codec->spec;
637 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
638
639 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
640 return 0;
641}
642
643static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
644 struct snd_ctl_elem_value *ucontrol)
645{
646 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
647 struct via_spec *spec = codec->spec;
648 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
bff5fbf5 649 int ret;
c577b8a1 650
337b9d02
TI
651 if (!spec->mux_nids[adc_idx])
652 return -EINVAL;
a80e6e3c
LW
653 /* switch to D0 beofre change index */
654 if (snd_hda_codec_read(codec, spec->mux_nids[adc_idx], 0,
655 AC_VERB_GET_POWER_STATE, 0x00) != AC_PWRST_D0)
656 snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
657 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
a80e6e3c 658
bff5fbf5 659 ret = snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
337b9d02
TI
660 spec->mux_nids[adc_idx],
661 &spec->cur_mux[adc_idx]);
bff5fbf5 662 /* update jack power state */
3e95b9ab 663 set_widgets_power_state(codec);
bff5fbf5
LW
664
665 return ret;
c577b8a1
JC
666}
667
0aa62aef
HW
668static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
669 struct snd_ctl_elem_info *uinfo)
670{
671 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
672 struct via_spec *spec = codec->spec;
673 return snd_hda_input_mux_info(spec->hp_mux, uinfo);
674}
675
676static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
677 struct snd_ctl_elem_value *ucontrol)
678{
679 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5b0cb1d8 680 hda_nid_t nid = kcontrol->private_value;
eb7188ca 681 unsigned int pinsel;
0aa62aef 682
eb7188ca
LW
683 /* use !! to translate conn sel 2 for VT1718S */
684 pinsel = !!snd_hda_codec_read(codec, nid, 0,
685 AC_VERB_GET_CONNECT_SEL,
686 0x00);
0aa62aef
HW
687 ucontrol->value.enumerated.item[0] = pinsel;
688
689 return 0;
690}
691
0713efeb
LW
692static void activate_ctl(struct hda_codec *codec, const char *name, int active)
693{
694 struct snd_kcontrol *ctl = snd_hda_find_mixer_ctl(codec, name);
695 if (ctl) {
696 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
697 ctl->vd[0].access |= active
698 ? 0 : SNDRV_CTL_ELEM_ACCESS_INACTIVE;
699 snd_ctl_notify(codec->bus->card,
700 SNDRV_CTL_EVENT_MASK_VALUE, &ctl->id);
701 }
702}
703
5b0cb1d8
JK
704static hda_nid_t side_mute_channel(struct via_spec *spec)
705{
706 switch (spec->codec_type) {
707 case VT1708: return 0x1b;
708 case VT1709_10CH: return 0x29;
709 case VT1708B_8CH: /* fall thru */
710 case VT1708S: return 0x27;
e87885fe
LW
711 case VT2002P: return 0x19;
712 case VT1802: return 0x15;
713 case VT1812: return 0x15;
5b0cb1d8
JK
714 default: return 0;
715 }
716}
717
cdc1784d
LW
718static int update_side_mute_status(struct hda_codec *codec)
719{
720 /* mute side channel */
721 struct via_spec *spec = codec->spec;
e87885fe 722 unsigned int parm;
5b0cb1d8 723 hda_nid_t sw3 = side_mute_channel(spec);
cdc1784d 724
e87885fe
LW
725 if (sw3) {
726 if (VT2002P_COMPATIBLE(spec))
727 parm = spec->hp_independent_mode ?
728 AMP_IN_MUTE(1) : AMP_IN_UNMUTE(1);
729 else
730 parm = spec->hp_independent_mode ?
731 AMP_OUT_MUTE : AMP_OUT_UNMUTE;
732 snd_hda_codec_write(codec, sw3, 0,
733 AC_VERB_SET_AMP_GAIN_MUTE, parm);
734 if (spec->codec_type == VT1812)
735 snd_hda_codec_write(codec, 0x1d, 0,
736 AC_VERB_SET_AMP_GAIN_MUTE, parm);
737 }
cdc1784d
LW
738 return 0;
739}
740
0aa62aef
HW
741static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
742 struct snd_ctl_elem_value *ucontrol)
743{
744 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
745 struct via_spec *spec = codec->spec;
5b0cb1d8 746 hda_nid_t nid = kcontrol->private_value;
0aa62aef 747 unsigned int pinsel = ucontrol->value.enumerated.item[0];
cdc1784d
LW
748 /* Get Independent Mode index of headphone pin widget */
749 spec->hp_independent_mode = spec->hp_independent_mode_index == pinsel
750 ? 1 : 0;
ce0e5a9e
LW
751 if (spec->codec_type == VT1718S)
752 snd_hda_codec_write(codec, nid, 0,
753 AC_VERB_SET_CONNECT_SEL, pinsel ? 2 : 0);
754 else
755 snd_hda_codec_write(codec, nid, 0,
756 AC_VERB_SET_CONNECT_SEL, pinsel);
cdc1784d 757
ce0e5a9e
LW
758 if (spec->codec_type == VT1812)
759 snd_hda_codec_write(codec, 0x35, 0,
760 AC_VERB_SET_CONNECT_SEL, pinsel);
cdc1784d
LW
761 if (spec->multiout.hp_nid && spec->multiout.hp_nid
762 != spec->multiout.dac_nids[HDA_FRONT])
763 snd_hda_codec_setup_stream(codec, spec->multiout.hp_nid,
764 0, 0, 0);
0aa62aef 765
cdc1784d 766 update_side_mute_status(codec);
0713efeb
LW
767 /* update HP volume/swtich active state */
768 if (spec->codec_type == VT1708S
eb7188ca 769 || spec->codec_type == VT1702
f3db423d 770 || spec->codec_type == VT1718S
25eaba2f 771 || spec->codec_type == VT1716S
11890956 772 || VT2002P_COMPATIBLE(spec)) {
0713efeb
LW
773 activate_ctl(codec, "Headphone Playback Volume",
774 spec->hp_independent_mode);
775 activate_ctl(codec, "Headphone Playback Switch",
776 spec->hp_independent_mode);
777 }
ce0e5a9e 778 /* update jack power state */
3e95b9ab 779 set_widgets_power_state(codec);
0aa62aef
HW
780 return 0;
781}
782
90dd48a1 783static const struct snd_kcontrol_new via_hp_mixer[2] = {
0aa62aef
HW
784 {
785 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
786 .name = "Independent HP",
0aa62aef
HW
787 .info = via_independent_hp_info,
788 .get = via_independent_hp_get,
789 .put = via_independent_hp_put,
790 },
5b0cb1d8
JK
791 {
792 .iface = NID_MAPPING,
793 .name = "Independent HP",
794 },
0aa62aef
HW
795};
796
3d83e577 797static int via_hp_build(struct hda_codec *codec)
5b0cb1d8 798{
3d83e577 799 struct via_spec *spec = codec->spec;
5b0cb1d8
JK
800 struct snd_kcontrol_new *knew;
801 hda_nid_t nid;
3d83e577
TI
802 int nums;
803 hda_nid_t conn[HDA_MAX_CONNECTIONS];
5b0cb1d8
JK
804
805 switch (spec->codec_type) {
806 case VT1718S:
807 nid = 0x34;
808 break;
809 case VT2002P:
11890956 810 case VT1802:
5b0cb1d8
JK
811 nid = 0x35;
812 break;
813 case VT1812:
814 nid = 0x3d;
815 break;
816 default:
817 nid = spec->autocfg.hp_pins[0];
818 break;
819 }
820
ee3c35c0
LW
821 if (spec->codec_type != VT1708) {
822 nums = snd_hda_get_connections(codec, nid,
823 conn, HDA_MAX_CONNECTIONS);
824 if (nums <= 1)
825 return 0;
826 }
3d83e577
TI
827
828 knew = via_clone_control(spec, &via_hp_mixer[0]);
829 if (knew == NULL)
830 return -ENOMEM;
831
5b0cb1d8
JK
832 knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
833 knew->private_value = nid;
834
835 knew = via_clone_control(spec, &via_hp_mixer[1]);
836 if (knew == NULL)
837 return -ENOMEM;
838 knew->subdevice = side_mute_channel(spec);
839
840 return 0;
841}
842
1564b287
LW
843static void notify_aa_path_ctls(struct hda_codec *codec)
844{
845 int i;
846 struct snd_ctl_elem_id id;
525566cb
LW
847 const char *labels[] = {"Mic", "Front Mic", "Line", "Rear Mic"};
848 struct snd_kcontrol *ctl;
1564b287
LW
849
850 memset(&id, 0, sizeof(id));
851 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
852 for (i = 0; i < ARRAY_SIZE(labels); i++) {
853 sprintf(id.name, "%s Playback Volume", labels[i]);
525566cb
LW
854 ctl = snd_hda_find_mixer_ctl(codec, id.name);
855 if (ctl)
856 snd_ctl_notify(codec->bus->card,
857 SNDRV_CTL_EVENT_MASK_VALUE,
858 &ctl->id);
1564b287
LW
859 }
860}
861
862static void mute_aa_path(struct hda_codec *codec, int mute)
863{
864 struct via_spec *spec = codec->spec;
865 hda_nid_t nid_mixer;
866 int start_idx;
867 int end_idx;
868 int i;
869 /* get nid of MW0 and start & end index */
870 switch (spec->codec_type) {
871 case VT1708:
872 nid_mixer = 0x17;
873 start_idx = 2;
874 end_idx = 4;
875 break;
876 case VT1709_10CH:
877 case VT1709_6CH:
878 nid_mixer = 0x18;
879 start_idx = 2;
880 end_idx = 4;
881 break;
882 case VT1708B_8CH:
883 case VT1708B_4CH:
884 case VT1708S:
f3db423d 885 case VT1716S:
1564b287
LW
886 nid_mixer = 0x16;
887 start_idx = 2;
888 end_idx = 4;
889 break;
ab657e0c
LW
890 case VT1718S:
891 nid_mixer = 0x21;
892 start_idx = 1;
893 end_idx = 3;
894 break;
1564b287
LW
895 default:
896 return;
897 }
898 /* check AA path's mute status */
899 for (i = start_idx; i <= end_idx; i++) {
900 int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
901 snd_hda_codec_amp_stereo(codec, nid_mixer, HDA_INPUT, i,
902 HDA_AMP_MUTE, val);
903 }
904}
905static int is_smart51_pins(struct via_spec *spec, hda_nid_t pin)
906{
7b315bb4
TI
907 const struct auto_pin_cfg *cfg = &spec->autocfg;
908 int i;
909
910 for (i = 0; i < cfg->num_inputs; i++) {
911 if (pin == cfg->inputs[i].pin)
86e2959a 912 return cfg->inputs[i].type <= AUTO_PIN_LINE_IN;
1564b287 913 }
7b315bb4 914 return 0;
1564b287
LW
915}
916
917static int via_smart51_info(struct snd_kcontrol *kcontrol,
918 struct snd_ctl_elem_info *uinfo)
919{
920 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
921 uinfo->count = 1;
922 uinfo->value.integer.min = 0;
923 uinfo->value.integer.max = 1;
924 return 0;
925}
926
927static int via_smart51_get(struct snd_kcontrol *kcontrol,
928 struct snd_ctl_elem_value *ucontrol)
929{
930 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
931 struct via_spec *spec = codec->spec;
7b315bb4 932 const struct auto_pin_cfg *cfg = &spec->autocfg;
1564b287
LW
933 int on = 1;
934 int i;
935
7b315bb4
TI
936 for (i = 0; i < cfg->num_inputs; i++) {
937 hda_nid_t nid = cfg->inputs[i].pin;
938 int ctl = snd_hda_codec_read(codec, nid, 0,
939 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
86e2959a 940 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
7b315bb4 941 continue;
86e2959a 942 if (cfg->inputs[i].type == AUTO_PIN_MIC &&
7b315bb4
TI
943 spec->hp_independent_mode && spec->codec_type != VT1718S)
944 continue; /* ignore FMic for independent HP */
945 if ((ctl & AC_PINCTL_IN_EN) && !(ctl & AC_PINCTL_OUT_EN))
946 on = 0;
1564b287
LW
947 }
948 *ucontrol->value.integer.value = on;
949 return 0;
950}
951
952static int via_smart51_put(struct snd_kcontrol *kcontrol,
953 struct snd_ctl_elem_value *ucontrol)
954{
955 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
956 struct via_spec *spec = codec->spec;
7b315bb4 957 const struct auto_pin_cfg *cfg = &spec->autocfg;
1564b287
LW
958 int out_in = *ucontrol->value.integer.value
959 ? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
1564b287
LW
960 int i;
961
7b315bb4
TI
962 for (i = 0; i < cfg->num_inputs; i++) {
963 hda_nid_t nid = cfg->inputs[i].pin;
964 unsigned int parm;
965
86e2959a 966 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
7b315bb4 967 continue;
86e2959a 968 if (cfg->inputs[i].type == AUTO_PIN_MIC &&
7b315bb4 969 spec->hp_independent_mode && spec->codec_type != VT1718S)
1564b287 970 continue; /* don't retask FMic for independent HP */
7b315bb4
TI
971
972 parm = snd_hda_codec_read(codec, nid, 0,
973 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
974 parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
975 parm |= out_in;
976 snd_hda_codec_write(codec, nid, 0,
977 AC_VERB_SET_PIN_WIDGET_CONTROL,
978 parm);
979 if (out_in == AC_PINCTL_OUT_EN) {
980 mute_aa_path(codec, 1);
981 notify_aa_path_ctls(codec);
982 }
983 if (spec->codec_type == VT1718S) {
984 snd_hda_codec_amp_stereo(
eb7188ca
LW
985 codec, nid, HDA_OUTPUT, 0, HDA_AMP_MUTE,
986 HDA_AMP_UNMUTE);
1564b287 987 }
86e2959a 988 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
f3db423d
LW
989 if (spec->codec_type == VT1708S
990 || spec->codec_type == VT1716S) {
1564b287
LW
991 /* input = index 1 (AOW3) */
992 snd_hda_codec_write(
993 codec, nid, 0,
994 AC_VERB_SET_CONNECT_SEL, 1);
995 snd_hda_codec_amp_stereo(
996 codec, nid, HDA_OUTPUT,
997 0, HDA_AMP_MUTE, HDA_AMP_UNMUTE);
998 }
999 }
1000 }
1001 spec->smart51_enabled = *ucontrol->value.integer.value;
3e95b9ab 1002 set_widgets_power_state(codec);
1564b287
LW
1003 return 1;
1004}
1005
5f4b36d6
TI
1006static const struct snd_kcontrol_new via_smart51_mixer = {
1007 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1008 .name = "Smart 5.1",
1009 .count = 1,
1010 .info = via_smart51_info,
1011 .get = via_smart51_get,
1012 .put = via_smart51_put,
1564b287
LW
1013};
1014
5b0cb1d8
JK
1015static int via_smart51_build(struct via_spec *spec)
1016{
1017 struct snd_kcontrol_new *knew;
7b315bb4 1018 const struct auto_pin_cfg *cfg = &spec->autocfg;
5b0cb1d8
JK
1019 hda_nid_t nid;
1020 int i;
1021
cb34c207
LW
1022 if (!cfg)
1023 return 0;
1024 if (cfg->line_outs > 2)
1025 return 0;
1026
5f4b36d6 1027 knew = via_clone_control(spec, &via_smart51_mixer);
5b0cb1d8
JK
1028 if (knew == NULL)
1029 return -ENOMEM;
1030
7b315bb4
TI
1031 for (i = 0; i < cfg->num_inputs; i++) {
1032 nid = cfg->inputs[i].pin;
86e2959a 1033 if (cfg->inputs[i].type <= AUTO_PIN_LINE_IN) {
5f4b36d6 1034 knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
7b315bb4 1035 break;
5b0cb1d8
JK
1036 }
1037 }
1038
1039 return 0;
1040}
1041
c577b8a1 1042/* capture mixer elements */
90dd48a1 1043static const struct snd_kcontrol_new vt1708_capture_mixer[] = {
c577b8a1
JC
1044 HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_INPUT),
1045 HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_INPUT),
1046 HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x27, 0x0, HDA_INPUT),
1047 HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x27, 0x0, HDA_INPUT),
1048 {
1049 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1050 /* The multiple "Capture Source" controls confuse alsamixer
1051 * So call somewhat different..
c577b8a1
JC
1052 */
1053 /* .name = "Capture Source", */
1054 .name = "Input Source",
1055 .count = 1,
1056 .info = via_mux_enum_info,
1057 .get = via_mux_enum_get,
1058 .put = via_mux_enum_put,
1059 },
1060 { } /* end */
1061};
f5271101
LW
1062
1063/* check AA path's mute statue */
1064static int is_aa_path_mute(struct hda_codec *codec)
1065{
1066 int mute = 1;
1067 hda_nid_t nid_mixer;
1068 int start_idx;
1069 int end_idx;
1070 int i;
1071 struct via_spec *spec = codec->spec;
1072 /* get nid of MW0 and start & end index */
1073 switch (spec->codec_type) {
1074 case VT1708B_8CH:
1075 case VT1708B_4CH:
1076 case VT1708S:
f3db423d 1077 case VT1716S:
f5271101
LW
1078 nid_mixer = 0x16;
1079 start_idx = 2;
1080 end_idx = 4;
1081 break;
1082 case VT1702:
1083 nid_mixer = 0x1a;
1084 start_idx = 1;
1085 end_idx = 3;
1086 break;
eb7188ca
LW
1087 case VT1718S:
1088 nid_mixer = 0x21;
1089 start_idx = 1;
1090 end_idx = 3;
1091 break;
25eaba2f 1092 case VT2002P:
ab6734e7 1093 case VT1812:
11890956 1094 case VT1802:
25eaba2f
LW
1095 nid_mixer = 0x21;
1096 start_idx = 0;
1097 end_idx = 2;
1098 break;
f5271101
LW
1099 default:
1100 return 0;
1101 }
1102 /* check AA path's mute status */
1103 for (i = start_idx; i <= end_idx; i++) {
1104 unsigned int con_list = snd_hda_codec_read(
1105 codec, nid_mixer, 0, AC_VERB_GET_CONNECT_LIST, i/4*4);
1106 int shift = 8 * (i % 4);
1107 hda_nid_t nid_pin = (con_list & (0xff << shift)) >> shift;
1108 unsigned int defconf = snd_hda_codec_get_pincfg(codec, nid_pin);
1109 if (get_defcfg_connect(defconf) == AC_JACK_PORT_COMPLEX) {
1110 /* check mute status while the pin is connected */
1111 int mute_l = snd_hda_codec_amp_read(codec, nid_mixer, 0,
1112 HDA_INPUT, i) >> 7;
1113 int mute_r = snd_hda_codec_amp_read(codec, nid_mixer, 1,
1114 HDA_INPUT, i) >> 7;
1115 if (!mute_l || !mute_r) {
1116 mute = 0;
1117 break;
1118 }
1119 }
1120 }
1121 return mute;
1122}
1123
1124/* enter/exit analog low-current mode */
1125static void analog_low_current_mode(struct hda_codec *codec, int stream_idle)
1126{
1127 struct via_spec *spec = codec->spec;
1128 static int saved_stream_idle = 1; /* saved stream idle status */
1129 int enable = is_aa_path_mute(codec);
1130 unsigned int verb = 0;
1131 unsigned int parm = 0;
1132
1133 if (stream_idle == -1) /* stream status did not change */
1134 enable = enable && saved_stream_idle;
1135 else {
1136 enable = enable && stream_idle;
1137 saved_stream_idle = stream_idle;
1138 }
1139
1140 /* decide low current mode's verb & parameter */
1141 switch (spec->codec_type) {
1142 case VT1708B_8CH:
1143 case VT1708B_4CH:
1144 verb = 0xf70;
1145 parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
1146 break;
1147 case VT1708S:
eb7188ca 1148 case VT1718S:
f3db423d 1149 case VT1716S:
f5271101
LW
1150 verb = 0xf73;
1151 parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
1152 break;
1153 case VT1702:
1154 verb = 0xf73;
1155 parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
1156 break;
25eaba2f 1157 case VT2002P:
ab6734e7 1158 case VT1812:
11890956 1159 case VT1802:
25eaba2f
LW
1160 verb = 0xf93;
1161 parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
1162 break;
f5271101
LW
1163 default:
1164 return; /* other codecs are not supported */
1165 }
1166 /* send verb */
1167 snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
1168}
1169
c577b8a1
JC
1170/*
1171 * generic initialization of ADC, input mixers and output mixers
1172 */
90dd48a1 1173static const struct hda_verb vt1708_volume_init_verbs[] = {
c577b8a1
JC
1174 /*
1175 * Unmute ADC0-1 and set the default input to mic-in
1176 */
1177 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1178 {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1179
1180
f7278fd0 1181 /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
c577b8a1
JC
1182 * mixer widget
1183 */
1184 /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
f7278fd0
JC
1185 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1186 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1187 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
1188 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
1189 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
c577b8a1
JC
1190
1191 /*
1192 * Set up output mixers (0x19 - 0x1b)
1193 */
1194 /* set vol=0 to output mixers */
1195 {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1196 {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1197 {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
377ff31a 1198
bfdc675a
LW
1199 /* Setup default input MW0 to PW4 */
1200 {0x20, AC_VERB_SET_CONNECT_SEL, 0},
c577b8a1
JC
1201 /* PW9 Output enable */
1202 {0x25, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
aa266fcc
LW
1203 /* power down jack detect function */
1204 {0x1, 0xf81, 0x1},
f7278fd0 1205 { }
c577b8a1
JC
1206};
1207
1208static int via_playback_pcm_open(struct hda_pcm_stream *hinfo,
1209 struct hda_codec *codec,
1210 struct snd_pcm_substream *substream)
1211{
1212 struct via_spec *spec = codec->spec;
17314379
LW
1213 int idle = substream->pstr->substream_opened == 1
1214 && substream->ref_count == 0;
17314379 1215 analog_low_current_mode(codec, idle);
9a08160b
TI
1216 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
1217 hinfo);
c577b8a1
JC
1218}
1219
0aa62aef
HW
1220static void playback_multi_pcm_prep_0(struct hda_codec *codec,
1221 unsigned int stream_tag,
1222 unsigned int format,
1223 struct snd_pcm_substream *substream)
1224{
1225 struct via_spec *spec = codec->spec;
1226 struct hda_multi_out *mout = &spec->multiout;
dda14410 1227 const hda_nid_t *nids = mout->dac_nids;
0aa62aef
HW
1228 int chs = substream->runtime->channels;
1229 int i;
7c935976
SW
1230 struct hda_spdif_out *spdif =
1231 snd_hda_spdif_out_of_nid(codec, spec->multiout.dig_out_nid);
0aa62aef
HW
1232
1233 mutex_lock(&codec->spdif_mutex);
1234 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1235 if (chs == 2 &&
1236 snd_hda_is_supported_format(codec, mout->dig_out_nid,
1237 format) &&
7c935976 1238 !(spdif->status & IEC958_AES0_NONAUDIO)) {
0aa62aef
HW
1239 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
1240 /* turn off SPDIF once; otherwise the IEC958 bits won't
1241 * be updated */
7c935976 1242 if (spdif->ctls & AC_DIG1_ENABLE)
0aa62aef
HW
1243 snd_hda_codec_write(codec, mout->dig_out_nid, 0,
1244 AC_VERB_SET_DIGI_CONVERT_1,
7c935976 1245 spdif->ctls &
0aa62aef
HW
1246 ~AC_DIG1_ENABLE & 0xff);
1247 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1248 stream_tag, 0, format);
1249 /* turn on again (if needed) */
7c935976 1250 if (spdif->ctls & AC_DIG1_ENABLE)
0aa62aef
HW
1251 snd_hda_codec_write(codec, mout->dig_out_nid, 0,
1252 AC_VERB_SET_DIGI_CONVERT_1,
7c935976 1253 spdif->ctls & 0xff);
0aa62aef
HW
1254 } else {
1255 mout->dig_out_used = 0;
1256 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1257 0, 0, 0);
1258 }
1259 }
1260 mutex_unlock(&codec->spdif_mutex);
1261
1262 /* front */
1263 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
1264 0, format);
1265
eb7188ca
LW
1266 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]
1267 && !spec->hp_independent_mode)
0aa62aef
HW
1268 /* headphone out will just decode front left/right (stereo) */
1269 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
1270 0, format);
1271
1272 /* extra outputs copied from front */
1273 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1274 if (mout->extra_out_nid[i])
1275 snd_hda_codec_setup_stream(codec,
1276 mout->extra_out_nid[i],
1277 stream_tag, 0, format);
1278
1279 /* surrounds */
1280 for (i = 1; i < mout->num_dacs; i++) {
1281 if (chs >= (i + 1) * 2) /* independent out */
1282 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
1283 i * 2, format);
1284 else /* copy front */
1285 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
1286 0, format);
1287 }
1288}
1289
1290static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
1291 struct hda_codec *codec,
1292 unsigned int stream_tag,
1293 unsigned int format,
1294 struct snd_pcm_substream *substream)
1295{
1296 struct via_spec *spec = codec->spec;
1297 struct hda_multi_out *mout = &spec->multiout;
dda14410 1298 const hda_nid_t *nids = mout->dac_nids;
0aa62aef
HW
1299
1300 if (substream->number == 0)
1301 playback_multi_pcm_prep_0(codec, stream_tag, format,
1302 substream);
1303 else {
1304 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
1305 spec->hp_independent_mode)
1306 snd_hda_codec_setup_stream(codec, mout->hp_nid,
1307 stream_tag, 0, format);
1308 }
1f2e99fe 1309 vt1708_start_hp_work(spec);
0aa62aef
HW
1310 return 0;
1311}
1312
1313static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
1314 struct hda_codec *codec,
1315 struct snd_pcm_substream *substream)
1316{
1317 struct via_spec *spec = codec->spec;
1318 struct hda_multi_out *mout = &spec->multiout;
dda14410 1319 const hda_nid_t *nids = mout->dac_nids;
0aa62aef
HW
1320 int i;
1321
1322 if (substream->number == 0) {
1323 for (i = 0; i < mout->num_dacs; i++)
1324 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
1325
1326 if (mout->hp_nid && !spec->hp_independent_mode)
1327 snd_hda_codec_setup_stream(codec, mout->hp_nid,
1328 0, 0, 0);
1329
1330 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1331 if (mout->extra_out_nid[i])
1332 snd_hda_codec_setup_stream(codec,
1333 mout->extra_out_nid[i],
1334 0, 0, 0);
1335 mutex_lock(&codec->spdif_mutex);
1336 if (mout->dig_out_nid &&
1337 mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
1338 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1339 0, 0, 0);
1340 mout->dig_out_used = 0;
1341 }
1342 mutex_unlock(&codec->spdif_mutex);
1343 } else {
1344 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
1345 spec->hp_independent_mode)
1346 snd_hda_codec_setup_stream(codec, mout->hp_nid,
1347 0, 0, 0);
1348 }
1f2e99fe 1349 vt1708_stop_hp_work(spec);
0aa62aef
HW
1350 return 0;
1351}
1352
c577b8a1
JC
1353/*
1354 * Digital out
1355 */
1356static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1357 struct hda_codec *codec,
1358 struct snd_pcm_substream *substream)
1359{
1360 struct via_spec *spec = codec->spec;
1361 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1362}
1363
1364static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1365 struct hda_codec *codec,
1366 struct snd_pcm_substream *substream)
1367{
1368 struct via_spec *spec = codec->spec;
1369 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1370}
1371
5691ec7f 1372static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
98aa34c0
HW
1373 struct hda_codec *codec,
1374 unsigned int stream_tag,
1375 unsigned int format,
1376 struct snd_pcm_substream *substream)
1377{
1378 struct via_spec *spec = codec->spec;
9da29271
TI
1379 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1380 stream_tag, format, substream);
1381}
98aa34c0 1382
9da29271
TI
1383static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1384 struct hda_codec *codec,
1385 struct snd_pcm_substream *substream)
1386{
1387 struct via_spec *spec = codec->spec;
1388 snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
98aa34c0
HW
1389 return 0;
1390}
1391
c577b8a1
JC
1392/*
1393 * Analog capture
1394 */
1395static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1396 struct hda_codec *codec,
1397 unsigned int stream_tag,
1398 unsigned int format,
1399 struct snd_pcm_substream *substream)
1400{
1401 struct via_spec *spec = codec->spec;
1402
1403 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1404 stream_tag, 0, format);
1405 return 0;
1406}
1407
1408static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1409 struct hda_codec *codec,
1410 struct snd_pcm_substream *substream)
1411{
1412 struct via_spec *spec = codec->spec;
888afa15 1413 snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
c577b8a1
JC
1414 return 0;
1415}
1416
90dd48a1 1417static const struct hda_pcm_stream vt1708_pcm_analog_playback = {
0aa62aef 1418 .substreams = 2,
c577b8a1
JC
1419 .channels_min = 2,
1420 .channels_max = 8,
1421 .nid = 0x10, /* NID to query formats and rates */
1422 .ops = {
1423 .open = via_playback_pcm_open,
0aa62aef
HW
1424 .prepare = via_playback_multi_pcm_prepare,
1425 .cleanup = via_playback_multi_pcm_cleanup
c577b8a1
JC
1426 },
1427};
1428
90dd48a1 1429static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
c873cc25 1430 .substreams = 2,
bc9b5623
TI
1431 .channels_min = 2,
1432 .channels_max = 8,
1433 .nid = 0x10, /* NID to query formats and rates */
1434 /* We got noisy outputs on the right channel on VT1708 when
1435 * 24bit samples are used. Until any workaround is found,
1436 * disable the 24bit format, so far.
1437 */
1438 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1439 .ops = {
1440 .open = via_playback_pcm_open,
c873cc25
LW
1441 .prepare = via_playback_multi_pcm_prepare,
1442 .cleanup = via_playback_multi_pcm_cleanup
bc9b5623
TI
1443 },
1444};
1445
90dd48a1 1446static const struct hda_pcm_stream vt1708_pcm_analog_capture = {
c577b8a1
JC
1447 .substreams = 2,
1448 .channels_min = 2,
1449 .channels_max = 2,
1450 .nid = 0x15, /* NID to query formats and rates */
1451 .ops = {
1452 .prepare = via_capture_pcm_prepare,
1453 .cleanup = via_capture_pcm_cleanup
1454 },
1455};
1456
90dd48a1 1457static const struct hda_pcm_stream vt1708_pcm_digital_playback = {
c577b8a1
JC
1458 .substreams = 1,
1459 .channels_min = 2,
1460 .channels_max = 2,
1461 /* NID is set in via_build_pcms */
1462 .ops = {
1463 .open = via_dig_playback_pcm_open,
6b97eb45 1464 .close = via_dig_playback_pcm_close,
9da29271
TI
1465 .prepare = via_dig_playback_pcm_prepare,
1466 .cleanup = via_dig_playback_pcm_cleanup
c577b8a1
JC
1467 },
1468};
1469
90dd48a1 1470static const struct hda_pcm_stream vt1708_pcm_digital_capture = {
c577b8a1
JC
1471 .substreams = 1,
1472 .channels_min = 2,
1473 .channels_max = 2,
1474};
1475
1476static int via_build_controls(struct hda_codec *codec)
1477{
1478 struct via_spec *spec = codec->spec;
5b0cb1d8 1479 struct snd_kcontrol *kctl;
90dd48a1 1480 const struct snd_kcontrol_new *knew;
5b0cb1d8 1481 int err, i;
c577b8a1
JC
1482
1483 for (i = 0; i < spec->num_mixers; i++) {
1484 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1485 if (err < 0)
1486 return err;
1487 }
1488
1489 if (spec->multiout.dig_out_nid) {
1490 err = snd_hda_create_spdif_out_ctls(codec,
74b654c9 1491 spec->multiout.dig_out_nid,
c577b8a1
JC
1492 spec->multiout.dig_out_nid);
1493 if (err < 0)
1494 return err;
9a08160b
TI
1495 err = snd_hda_create_spdif_share_sw(codec,
1496 &spec->multiout);
1497 if (err < 0)
1498 return err;
1499 spec->multiout.share_spdif = 1;
c577b8a1
JC
1500 }
1501 if (spec->dig_in_nid) {
1502 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1503 if (err < 0)
1504 return err;
1505 }
17314379 1506
5b0cb1d8
JK
1507 /* assign Capture Source enums to NID */
1508 kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1509 for (i = 0; kctl && i < kctl->count; i++) {
21949f00 1510 err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
5b0cb1d8
JK
1511 if (err < 0)
1512 return err;
1513 }
1514
1515 /* other nid->control mapping */
1516 for (i = 0; i < spec->num_mixers; i++) {
1517 for (knew = spec->mixers[i]; knew->name; knew++) {
1518 if (knew->iface != NID_MAPPING)
1519 continue;
1520 kctl = snd_hda_find_mixer_ctl(codec, knew->name);
1521 if (kctl == NULL)
1522 continue;
1523 err = snd_hda_add_nid(codec, kctl, 0,
1524 knew->subdevice);
1525 }
1526 }
1527
17314379 1528 /* init power states */
3e95b9ab 1529 set_widgets_power_state(codec);
17314379
LW
1530 analog_low_current_mode(codec, 1);
1531
603c4019 1532 via_free_kctls(codec); /* no longer needed */
c577b8a1
JC
1533 return 0;
1534}
1535
1536static int via_build_pcms(struct hda_codec *codec)
1537{
1538 struct via_spec *spec = codec->spec;
1539 struct hda_pcm *info = spec->pcm_rec;
1540
1541 codec->num_pcms = 1;
1542 codec->pcm_info = info;
1543
1544 info->name = spec->stream_name_analog;
377ff31a
LW
1545 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1546 *(spec->stream_analog_playback);
1547 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1548 spec->multiout.dac_nids[0];
c577b8a1
JC
1549 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_analog_capture);
1550 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
1551
1552 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1553 spec->multiout.max_channels;
1554
1555 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1556 codec->num_pcms++;
1557 info++;
1558 info->name = spec->stream_name_digital;
7ba72ba1 1559 info->pcm_type = HDA_PCM_TYPE_SPDIF;
c577b8a1
JC
1560 if (spec->multiout.dig_out_nid) {
1561 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1562 *(spec->stream_digital_playback);
1563 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1564 spec->multiout.dig_out_nid;
1565 }
1566 if (spec->dig_in_nid) {
1567 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1568 *(spec->stream_digital_capture);
1569 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1570 spec->dig_in_nid;
1571 }
1572 }
1573
1574 return 0;
1575}
1576
1577static void via_free(struct hda_codec *codec)
1578{
1579 struct via_spec *spec = codec->spec;
c577b8a1
JC
1580
1581 if (!spec)
1582 return;
1583
603c4019 1584 via_free_kctls(codec);
1f2e99fe 1585 vt1708_stop_hp_work(spec);
c577b8a1
JC
1586 kfree(codec->spec);
1587}
1588
69e52a80
HW
1589/* mute internal speaker if HP is plugged */
1590static void via_hp_automute(struct hda_codec *codec)
1591{
dcf34c8c 1592 unsigned int present = 0;
69e52a80
HW
1593 struct via_spec *spec = codec->spec;
1594
d56757ab 1595 present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
dcf34c8c
LW
1596
1597 if (!spec->hp_independent_mode) {
1598 struct snd_ctl_elem_id id;
1599 /* auto mute */
1600 snd_hda_codec_amp_stereo(
1601 codec, spec->autocfg.line_out_pins[0], HDA_OUTPUT, 0,
1602 HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
1603 /* notify change */
1604 memset(&id, 0, sizeof(id));
1605 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1606 strcpy(id.name, "Front Playback Switch");
1607 snd_ctl_notify(codec->bus->card, SNDRV_CTL_EVENT_MASK_VALUE,
1608 &id);
1609 }
69e52a80
HW
1610}
1611
f3db423d
LW
1612/* mute mono out if HP or Line out is plugged */
1613static void via_mono_automute(struct hda_codec *codec)
1614{
1615 unsigned int hp_present, lineout_present;
1616 struct via_spec *spec = codec->spec;
1617
1618 if (spec->codec_type != VT1716S)
1619 return;
1620
d56757ab
TI
1621 lineout_present = snd_hda_jack_detect(codec,
1622 spec->autocfg.line_out_pins[0]);
f3db423d
LW
1623
1624 /* Mute Mono Out if Line Out is plugged */
1625 if (lineout_present) {
1626 snd_hda_codec_amp_stereo(
1627 codec, 0x2A, HDA_OUTPUT, 0, HDA_AMP_MUTE, HDA_AMP_MUTE);
1628 return;
1629 }
1630
d56757ab 1631 hp_present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
f3db423d
LW
1632
1633 if (!spec->hp_independent_mode)
1634 snd_hda_codec_amp_stereo(
1635 codec, 0x2A, HDA_OUTPUT, 0, HDA_AMP_MUTE,
1636 hp_present ? HDA_AMP_MUTE : 0);
1637}
1638
69e52a80
HW
1639static void via_gpio_control(struct hda_codec *codec)
1640{
1641 unsigned int gpio_data;
1642 unsigned int vol_counter;
1643 unsigned int vol;
1644 unsigned int master_vol;
1645
1646 struct via_spec *spec = codec->spec;
1647
1648 gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
1649 AC_VERB_GET_GPIO_DATA, 0) & 0x03;
1650
1651 vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
1652 0xF84, 0) & 0x3F0000) >> 16;
1653
1654 vol = vol_counter & 0x1F;
1655 master_vol = snd_hda_codec_read(codec, 0x1A, 0,
1656 AC_VERB_GET_AMP_GAIN_MUTE,
1657 AC_AMP_GET_INPUT);
1658
1659 if (gpio_data == 0x02) {
1660 /* unmute line out */
1661 snd_hda_codec_amp_stereo(codec, spec->autocfg.line_out_pins[0],
1662 HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
1663
1664 if (vol_counter & 0x20) {
1665 /* decrease volume */
1666 if (vol > master_vol)
1667 vol = master_vol;
1668 snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
1669 0, HDA_AMP_VOLMASK,
1670 master_vol-vol);
1671 } else {
1672 /* increase volume */
1673 snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
1674 HDA_AMP_VOLMASK,
1675 ((master_vol+vol) > 0x2A) ? 0x2A :
1676 (master_vol+vol));
1677 }
1678 } else if (!(gpio_data & 0x02)) {
1679 /* mute line out */
1680 snd_hda_codec_amp_stereo(codec,
1681 spec->autocfg.line_out_pins[0],
1682 HDA_OUTPUT, 0, HDA_AMP_MUTE,
1683 HDA_AMP_MUTE);
1684 }
1685}
1686
25eaba2f
LW
1687/* mute Internal-Speaker if HP is plugged */
1688static void via_speaker_automute(struct hda_codec *codec)
1689{
1690 unsigned int hp_present;
1691 struct via_spec *spec = codec->spec;
1692
27439ce7 1693 if (!VT2002P_COMPATIBLE(spec))
25eaba2f
LW
1694 return;
1695
d56757ab 1696 hp_present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
25eaba2f
LW
1697
1698 if (!spec->hp_independent_mode) {
1699 struct snd_ctl_elem_id id;
1700 snd_hda_codec_amp_stereo(
1701 codec, spec->autocfg.speaker_pins[0], HDA_OUTPUT, 0,
1702 HDA_AMP_MUTE, hp_present ? HDA_AMP_MUTE : 0);
1703 /* notify change */
1704 memset(&id, 0, sizeof(id));
1705 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1706 strcpy(id.name, "Speaker Playback Switch");
1707 snd_ctl_notify(codec->bus->card, SNDRV_CTL_EVENT_MASK_VALUE,
1708 &id);
1709 }
1710}
1711
1712/* mute line-out and internal speaker if HP is plugged */
1713static void via_hp_bind_automute(struct hda_codec *codec)
1714{
01a1796b 1715 /* use long instead of int below just to avoid an internal compiler
1716 * error with gcc 4.0.x
1717 */
1718 unsigned long hp_present, present = 0;
25eaba2f
LW
1719 struct via_spec *spec = codec->spec;
1720 int i;
1721
1722 if (!spec->autocfg.hp_pins[0] || !spec->autocfg.line_out_pins[0])
1723 return;
1724
d56757ab 1725 hp_present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
25eaba2f 1726
d56757ab 1727 present = snd_hda_jack_detect(codec, spec->autocfg.line_out_pins[0]);
25eaba2f
LW
1728
1729 if (!spec->hp_independent_mode) {
1730 /* Mute Line-Outs */
1731 for (i = 0; i < spec->autocfg.line_outs; i++)
1732 snd_hda_codec_amp_stereo(
1733 codec, spec->autocfg.line_out_pins[i],
1734 HDA_OUTPUT, 0,
1735 HDA_AMP_MUTE, hp_present ? HDA_AMP_MUTE : 0);
1736 if (hp_present)
1737 present = hp_present;
1738 }
1739 /* Speakers */
1740 for (i = 0; i < spec->autocfg.speaker_outs; i++)
1741 snd_hda_codec_amp_stereo(
1742 codec, spec->autocfg.speaker_pins[i], HDA_OUTPUT, 0,
1743 HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
1744}
1745
1746
69e52a80
HW
1747/* unsolicited event for jack sensing */
1748static void via_unsol_event(struct hda_codec *codec,
1749 unsigned int res)
1750{
1751 res >>= 26;
ec7e7e42 1752
a34df19a 1753 if (res & VIA_JACK_EVENT)
3e95b9ab 1754 set_widgets_power_state(codec);
ec7e7e42
LW
1755
1756 res &= ~VIA_JACK_EVENT;
1757
1758 if (res == VIA_HP_EVENT)
1759 via_hp_automute(codec);
1760 else if (res == VIA_GPIO_EVENT)
1761 via_gpio_control(codec);
1762 else if (res == VIA_MONO_EVENT)
f3db423d 1763 via_mono_automute(codec);
ec7e7e42 1764 else if (res == VIA_SPEAKER_EVENT)
25eaba2f 1765 via_speaker_automute(codec);
ec7e7e42 1766 else if (res == VIA_BIND_HP_EVENT)
25eaba2f 1767 via_hp_bind_automute(codec);
69e52a80
HW
1768}
1769
c577b8a1
JC
1770static int via_init(struct hda_codec *codec)
1771{
1772 struct via_spec *spec = codec->spec;
69e52a80
HW
1773 int i;
1774 for (i = 0; i < spec->num_iverbs; i++)
1775 snd_hda_sequence_write(codec, spec->init_verbs[i]);
1776
f7278fd0
JC
1777 /* Lydia Add for EAPD enable */
1778 if (!spec->dig_in_nid) { /* No Digital In connection */
55d1d6c1
TI
1779 if (spec->dig_in_pin) {
1780 snd_hda_codec_write(codec, spec->dig_in_pin, 0,
f7278fd0 1781 AC_VERB_SET_PIN_WIDGET_CONTROL,
12b74c80 1782 PIN_OUT);
55d1d6c1 1783 snd_hda_codec_write(codec, spec->dig_in_pin, 0,
f7278fd0
JC
1784 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
1785 }
12b74c80
TI
1786 } else /* enable SPDIF-input pin */
1787 snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
1788 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
f7278fd0 1789
9da29271
TI
1790 /* assign slave outs */
1791 if (spec->slave_dig_outs[0])
1792 codec->slave_dig_outs = spec->slave_dig_outs;
5691ec7f 1793
377ff31a 1794 return 0;
c577b8a1
JC
1795}
1796
1f2e99fe
LW
1797#ifdef SND_HDA_NEEDS_RESUME
1798static int via_suspend(struct hda_codec *codec, pm_message_t state)
1799{
1800 struct via_spec *spec = codec->spec;
1801 vt1708_stop_hp_work(spec);
1802 return 0;
1803}
1804#endif
1805
cb53c626
TI
1806#ifdef CONFIG_SND_HDA_POWER_SAVE
1807static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1808{
1809 struct via_spec *spec = codec->spec;
1810 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1811}
1812#endif
1813
c577b8a1
JC
1814/*
1815 */
90dd48a1 1816static const struct hda_codec_ops via_patch_ops = {
c577b8a1
JC
1817 .build_controls = via_build_controls,
1818 .build_pcms = via_build_pcms,
1819 .init = via_init,
1820 .free = via_free,
1f2e99fe
LW
1821#ifdef SND_HDA_NEEDS_RESUME
1822 .suspend = via_suspend,
1823#endif
cb53c626
TI
1824#ifdef CONFIG_SND_HDA_POWER_SAVE
1825 .check_power_status = via_check_power_status,
1826#endif
c577b8a1
JC
1827};
1828
1829/* fill in the dac_nids table from the parsed pin configuration */
1830static int vt1708_auto_fill_dac_nids(struct via_spec *spec,
1831 const struct auto_pin_cfg *cfg)
1832{
1833 int i;
1834 hda_nid_t nid;
1835
1836 spec->multiout.num_dacs = cfg->line_outs;
1837
1838 spec->multiout.dac_nids = spec->private_dac_nids;
377ff31a
LW
1839
1840 for (i = 0; i < 4; i++) {
c577b8a1
JC
1841 nid = cfg->line_out_pins[i];
1842 if (nid) {
1843 /* config dac list */
1844 switch (i) {
1845 case AUTO_SEQ_FRONT:
dda14410 1846 spec->private_dac_nids[i] = 0x10;
c577b8a1
JC
1847 break;
1848 case AUTO_SEQ_CENLFE:
dda14410 1849 spec->private_dac_nids[i] = 0x12;
c577b8a1
JC
1850 break;
1851 case AUTO_SEQ_SURROUND:
dda14410 1852 spec->private_dac_nids[i] = 0x11;
c577b8a1
JC
1853 break;
1854 case AUTO_SEQ_SIDE:
dda14410 1855 spec->private_dac_nids[i] = 0x13;
c577b8a1
JC
1856 break;
1857 }
1858 }
1859 }
1860
1861 return 0;
1862}
1863
1864/* add playback controls from the parsed DAC table */
1865static int vt1708_auto_create_multi_out_ctls(struct via_spec *spec,
1866 const struct auto_pin_cfg *cfg)
1867{
1868 char name[32];
ea734963
TI
1869 static const char * const chname[4] = {
1870 "Front", "Surround", "C/LFE", "Side"
1871 };
9645c203 1872 hda_nid_t nid, nid_vol, nid_vols[] = {0x17, 0x19, 0x1a, 0x1b};
c577b8a1
JC
1873 int i, err;
1874
1875 for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
1876 nid = cfg->line_out_pins[i];
1877
1878 if (!nid)
1879 continue;
377ff31a 1880
9645c203 1881 nid_vol = nid_vols[i];
c577b8a1
JC
1882
1883 if (i == AUTO_SEQ_CENLFE) {
1884 /* Center/LFE */
1885 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
f7278fd0
JC
1886 "Center Playback Volume",
1887 HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
1888 HDA_OUTPUT));
c577b8a1
JC
1889 if (err < 0)
1890 return err;
1891 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1892 "LFE Playback Volume",
f7278fd0
JC
1893 HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
1894 HDA_OUTPUT));
c577b8a1
JC
1895 if (err < 0)
1896 return err;
1897 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1898 "Center Playback Switch",
f7278fd0
JC
1899 HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
1900 HDA_OUTPUT));
c577b8a1
JC
1901 if (err < 0)
1902 return err;
1903 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1904 "LFE Playback Switch",
f7278fd0
JC
1905 HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
1906 HDA_OUTPUT));
c577b8a1
JC
1907 if (err < 0)
1908 return err;
377ff31a 1909 } else if (i == AUTO_SEQ_FRONT) {
c577b8a1
JC
1910 /* add control to mixer index 0 */
1911 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1912 "Master Front Playback Volume",
9645c203 1913 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
f7278fd0 1914 HDA_INPUT));
c577b8a1
JC
1915 if (err < 0)
1916 return err;
1917 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1918 "Master Front Playback Switch",
9645c203 1919 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
f7278fd0 1920 HDA_INPUT));
c577b8a1
JC
1921 if (err < 0)
1922 return err;
377ff31a 1923
c577b8a1
JC
1924 /* add control to PW3 */
1925 sprintf(name, "%s Playback Volume", chname[i]);
1926 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
f7278fd0
JC
1927 HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1928 HDA_OUTPUT));
c577b8a1
JC
1929 if (err < 0)
1930 return err;
1931 sprintf(name, "%s Playback Switch", chname[i]);
1932 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
f7278fd0
JC
1933 HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1934 HDA_OUTPUT));
c577b8a1
JC
1935 if (err < 0)
1936 return err;
1937 } else {
1938 sprintf(name, "%s Playback Volume", chname[i]);
1939 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
f7278fd0
JC
1940 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1941 HDA_OUTPUT));
c577b8a1
JC
1942 if (err < 0)
1943 return err;
1944 sprintf(name, "%s Playback Switch", chname[i]);
1945 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
f7278fd0
JC
1946 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1947 HDA_OUTPUT));
c577b8a1
JC
1948 if (err < 0)
1949 return err;
1950 }
1951 }
1952
1953 return 0;
1954}
1955
0aa62aef
HW
1956static void create_hp_imux(struct via_spec *spec)
1957{
1958 int i;
1959 struct hda_input_mux *imux = &spec->private_imux[1];
ea734963 1960 static const char * const texts[] = { "OFF", "ON", NULL};
0aa62aef
HW
1961
1962 /* for hp mode select */
10a20af7
TI
1963 for (i = 0; texts[i]; i++)
1964 snd_hda_add_imux_item(imux, texts[i], i, NULL);
0aa62aef
HW
1965
1966 spec->hp_mux = &spec->private_imux[1];
1967}
1968
c577b8a1
JC
1969static int vt1708_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
1970{
1971 int err;
1972
1973 if (!pin)
1974 return 0;
1975
1976 spec->multiout.hp_nid = VT1708_HP_NID; /* AOW3 */
cdc1784d 1977 spec->hp_independent_mode_index = 1;
c577b8a1
JC
1978
1979 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1980 "Headphone Playback Volume",
1981 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1982 if (err < 0)
1983 return err;
1984 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1985 "Headphone Playback Switch",
1986 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1987 if (err < 0)
1988 return err;
1989
0aa62aef
HW
1990 create_hp_imux(spec);
1991
c577b8a1
JC
1992 return 0;
1993}
1994
1995/* create playback/capture controls for input pins */
10a20af7 1996static int vt_auto_create_analog_input_ctls(struct hda_codec *codec,
f3268512
TI
1997 const struct auto_pin_cfg *cfg,
1998 hda_nid_t cap_nid,
90dd48a1
TI
1999 const hda_nid_t pin_idxs[],
2000 int num_idxs)
c577b8a1 2001{
10a20af7 2002 struct via_spec *spec = codec->spec;
0aa62aef 2003 struct hda_input_mux *imux = &spec->private_imux[0];
7b315bb4 2004 int i, err, idx, type, type_idx = 0;
c577b8a1
JC
2005
2006 /* for internal loopback recording select */
f3268512
TI
2007 for (idx = 0; idx < num_idxs; idx++) {
2008 if (pin_idxs[idx] == 0xff) {
10a20af7 2009 snd_hda_add_imux_item(imux, "Stereo Mixer", idx, NULL);
f3268512
TI
2010 break;
2011 }
2012 }
c577b8a1 2013
7b315bb4 2014 for (i = 0; i < cfg->num_inputs; i++) {
10a20af7 2015 const char *label;
7b315bb4 2016 type = cfg->inputs[i].type;
f3268512 2017 for (idx = 0; idx < num_idxs; idx++)
7b315bb4 2018 if (pin_idxs[idx] == cfg->inputs[i].pin)
f3268512
TI
2019 break;
2020 if (idx >= num_idxs)
2021 continue;
7b315bb4
TI
2022 if (i > 0 && type == cfg->inputs[i - 1].type)
2023 type_idx++;
2024 else
2025 type_idx = 0;
10a20af7 2026 label = hda_get_autocfg_input_label(codec, cfg, i);
16922281
LW
2027 if (spec->codec_type == VT1708S ||
2028 spec->codec_type == VT1702 ||
2029 spec->codec_type == VT1716S)
2030 err = via_new_analog_input(spec, label, type_idx,
2031 idx+1, cap_nid);
2032 else
2033 err = via_new_analog_input(spec, label, type_idx,
2034 idx, cap_nid);
c577b8a1
JC
2035 if (err < 0)
2036 return err;
10a20af7 2037 snd_hda_add_imux_item(imux, label, idx, NULL);
c577b8a1
JC
2038 }
2039 return 0;
2040}
2041
f3268512 2042/* create playback/capture controls for input pins */
10a20af7 2043static int vt1708_auto_create_analog_input_ctls(struct hda_codec *codec,
f3268512
TI
2044 const struct auto_pin_cfg *cfg)
2045{
90dd48a1 2046 static const hda_nid_t pin_idxs[] = { 0xff, 0x24, 0x1d, 0x1e, 0x21 };
10a20af7 2047 return vt_auto_create_analog_input_ctls(codec, cfg, 0x17, pin_idxs,
f3268512
TI
2048 ARRAY_SIZE(pin_idxs));
2049}
2050
cb53c626 2051#ifdef CONFIG_SND_HDA_POWER_SAVE
90dd48a1 2052static const struct hda_amp_list vt1708_loopbacks[] = {
cb53c626
TI
2053 { 0x17, HDA_INPUT, 1 },
2054 { 0x17, HDA_INPUT, 2 },
2055 { 0x17, HDA_INPUT, 3 },
2056 { 0x17, HDA_INPUT, 4 },
2057 { } /* end */
2058};
2059#endif
2060
76d9b0dd
HW
2061static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
2062{
2063 unsigned int def_conf;
2064 unsigned char seqassoc;
2065
2f334f92 2066 def_conf = snd_hda_codec_get_pincfg(codec, nid);
76d9b0dd
HW
2067 seqassoc = (unsigned char) get_defcfg_association(def_conf);
2068 seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
82ef9e45
LW
2069 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
2070 && (seqassoc == 0xf0 || seqassoc == 0xff)) {
2071 def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
2072 snd_hda_codec_set_pincfg(codec, nid, def_conf);
76d9b0dd
HW
2073 }
2074
2075 return;
2076}
2077
1f2e99fe
LW
2078static int vt1708_jack_detectect_get(struct snd_kcontrol *kcontrol,
2079 struct snd_ctl_elem_value *ucontrol)
2080{
2081 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2082 struct via_spec *spec = codec->spec;
2083
2084 if (spec->codec_type != VT1708)
2085 return 0;
2086 spec->vt1708_jack_detectect =
2087 !((snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8) & 0x1);
2088 ucontrol->value.integer.value[0] = spec->vt1708_jack_detectect;
2089 return 0;
2090}
2091
2092static int vt1708_jack_detectect_put(struct snd_kcontrol *kcontrol,
2093 struct snd_ctl_elem_value *ucontrol)
2094{
2095 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2096 struct via_spec *spec = codec->spec;
2097 int change;
2098
2099 if (spec->codec_type != VT1708)
2100 return 0;
2101 spec->vt1708_jack_detectect = ucontrol->value.integer.value[0];
2102 change = (0x1 & (snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8))
2103 == !spec->vt1708_jack_detectect;
2104 if (spec->vt1708_jack_detectect) {
2105 mute_aa_path(codec, 1);
2106 notify_aa_path_ctls(codec);
2107 }
2108 return change;
2109}
2110
90dd48a1 2111static const struct snd_kcontrol_new vt1708_jack_detectect[] = {
1f2e99fe
LW
2112 {
2113 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2114 .name = "Jack Detect",
2115 .count = 1,
2116 .info = snd_ctl_boolean_mono_info,
2117 .get = vt1708_jack_detectect_get,
2118 .put = vt1708_jack_detectect_put,
2119 },
2120 {} /* end */
2121};
2122
c577b8a1
JC
2123static int vt1708_parse_auto_config(struct hda_codec *codec)
2124{
2125 struct via_spec *spec = codec->spec;
2126 int err;
2127
76d9b0dd
HW
2128 /* Add HP and CD pin config connect bit re-config action */
2129 vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
2130 vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
2131
c577b8a1
JC
2132 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2133 if (err < 0)
2134 return err;
2135 err = vt1708_auto_fill_dac_nids(spec, &spec->autocfg);
2136 if (err < 0)
2137 return err;
2138 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2139 return 0; /* can't find valid BIOS pin config */
2140
2141 err = vt1708_auto_create_multi_out_ctls(spec, &spec->autocfg);
2142 if (err < 0)
2143 return err;
2144 err = vt1708_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2145 if (err < 0)
2146 return err;
10a20af7 2147 err = vt1708_auto_create_analog_input_ctls(codec, &spec->autocfg);
1f2e99fe
LW
2148 if (err < 0)
2149 return err;
2150 /* add jack detect on/off control */
2151 err = snd_hda_add_new_ctls(codec, vt1708_jack_detectect);
c577b8a1
JC
2152 if (err < 0)
2153 return err;
2154
2155 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2156
0852d7a6 2157 if (spec->autocfg.dig_outs)
c577b8a1 2158 spec->multiout.dig_out_nid = VT1708_DIGOUT_NID;
55d1d6c1 2159 spec->dig_in_pin = VT1708_DIGIN_PIN;
c577b8a1
JC
2160 if (spec->autocfg.dig_in_pin)
2161 spec->dig_in_nid = VT1708_DIGIN_NID;
2162
603c4019
TI
2163 if (spec->kctls.list)
2164 spec->mixers[spec->num_mixers++] = spec->kctls.list;
c577b8a1 2165
69e52a80 2166 spec->init_verbs[spec->num_iverbs++] = vt1708_volume_init_verbs;
c577b8a1 2167
0aa62aef
HW
2168 spec->input_mux = &spec->private_imux[0];
2169
f8fdd495 2170 if (spec->hp_mux)
3d83e577 2171 via_hp_build(codec);
c577b8a1 2172
5b0cb1d8 2173 via_smart51_build(spec);
c577b8a1
JC
2174 return 1;
2175}
2176
2177/* init callback for auto-configuration model -- overriding the default init */
2178static int via_auto_init(struct hda_codec *codec)
2179{
25eaba2f
LW
2180 struct via_spec *spec = codec->spec;
2181
c577b8a1
JC
2182 via_init(codec);
2183 via_auto_init_multi_out(codec);
2184 via_auto_init_hp_out(codec);
2185 via_auto_init_analog_input(codec);
11890956
LW
2186
2187 if (VT2002P_COMPATIBLE(spec)) {
25eaba2f
LW
2188 via_hp_bind_automute(codec);
2189 } else {
2190 via_hp_automute(codec);
2191 via_speaker_automute(codec);
2192 }
2193
c577b8a1
JC
2194 return 0;
2195}
2196
1f2e99fe
LW
2197static void vt1708_update_hp_jack_state(struct work_struct *work)
2198{
2199 struct via_spec *spec = container_of(work, struct via_spec,
2200 vt1708_hp_work.work);
2201 if (spec->codec_type != VT1708)
2202 return;
2203 /* if jack state toggled */
2204 if (spec->vt1708_hp_present
d56757ab 2205 != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
1f2e99fe
LW
2206 spec->vt1708_hp_present ^= 1;
2207 via_hp_automute(spec->codec);
2208 }
2209 vt1708_start_hp_work(spec);
2210}
2211
337b9d02
TI
2212static int get_mux_nids(struct hda_codec *codec)
2213{
2214 struct via_spec *spec = codec->spec;
2215 hda_nid_t nid, conn[8];
2216 unsigned int type;
2217 int i, n;
2218
2219 for (i = 0; i < spec->num_adc_nids; i++) {
2220 nid = spec->adc_nids[i];
2221 while (nid) {
a22d543a 2222 type = get_wcaps_type(get_wcaps(codec, nid));
1c55d521
TI
2223 if (type == AC_WID_PIN)
2224 break;
337b9d02
TI
2225 n = snd_hda_get_connections(codec, nid, conn,
2226 ARRAY_SIZE(conn));
2227 if (n <= 0)
2228 break;
2229 if (n > 1) {
2230 spec->mux_nids[i] = nid;
2231 break;
2232 }
2233 nid = conn[0];
2234 }
2235 }
1c55d521 2236 return 0;
337b9d02
TI
2237}
2238
c577b8a1
JC
2239static int patch_vt1708(struct hda_codec *codec)
2240{
2241 struct via_spec *spec;
2242 int err;
2243
2244 /* create a codec specific record */
5b0cb1d8 2245 spec = via_new_spec(codec);
c577b8a1
JC
2246 if (spec == NULL)
2247 return -ENOMEM;
2248
c577b8a1
JC
2249 /* automatic parse from the BIOS config */
2250 err = vt1708_parse_auto_config(codec);
2251 if (err < 0) {
2252 via_free(codec);
2253 return err;
2254 } else if (!err) {
2255 printk(KERN_INFO "hda_codec: Cannot set up configuration "
2256 "from BIOS. Using genenic mode...\n");
2257 }
2258
377ff31a 2259
c577b8a1
JC
2260 spec->stream_name_analog = "VT1708 Analog";
2261 spec->stream_analog_playback = &vt1708_pcm_analog_playback;
bc9b5623
TI
2262 /* disable 32bit format on VT1708 */
2263 if (codec->vendor_id == 0x11061708)
2264 spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
c577b8a1
JC
2265 spec->stream_analog_capture = &vt1708_pcm_analog_capture;
2266
2267 spec->stream_name_digital = "VT1708 Digital";
2268 spec->stream_digital_playback = &vt1708_pcm_digital_playback;
2269 spec->stream_digital_capture = &vt1708_pcm_digital_capture;
2270
377ff31a 2271
c577b8a1
JC
2272 if (!spec->adc_nids && spec->input_mux) {
2273 spec->adc_nids = vt1708_adc_nids;
2274 spec->num_adc_nids = ARRAY_SIZE(vt1708_adc_nids);
0f67a611 2275 get_mux_nids(codec);
c577b8a1
JC
2276 spec->mixers[spec->num_mixers] = vt1708_capture_mixer;
2277 spec->num_mixers++;
2278 }
2279
2280 codec->patch_ops = via_patch_ops;
2281
2282 codec->patch_ops.init = via_auto_init;
cb53c626
TI
2283#ifdef CONFIG_SND_HDA_POWER_SAVE
2284 spec->loopback.amplist = vt1708_loopbacks;
2285#endif
1f2e99fe 2286 INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
c577b8a1
JC
2287 return 0;
2288}
2289
2290/* capture mixer elements */
90dd48a1 2291static const struct snd_kcontrol_new vt1709_capture_mixer[] = {
c577b8a1
JC
2292 HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x0, HDA_INPUT),
2293 HDA_CODEC_MUTE("Capture Switch", 0x14, 0x0, HDA_INPUT),
2294 HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x15, 0x0, HDA_INPUT),
2295 HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x15, 0x0, HDA_INPUT),
2296 HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x16, 0x0, HDA_INPUT),
2297 HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x16, 0x0, HDA_INPUT),
2298 {
2299 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2300 /* The multiple "Capture Source" controls confuse alsamixer
2301 * So call somewhat different..
c577b8a1
JC
2302 */
2303 /* .name = "Capture Source", */
2304 .name = "Input Source",
2305 .count = 1,
2306 .info = via_mux_enum_info,
2307 .get = via_mux_enum_get,
2308 .put = via_mux_enum_put,
2309 },
2310 { } /* end */
2311};
2312
90dd48a1 2313static const struct hda_verb vt1709_uniwill_init_verbs[] = {
a34df19a
LW
2314 {0x20, AC_VERB_SET_UNSOLICITED_ENABLE,
2315 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
69e52a80
HW
2316 { }
2317};
2318
c577b8a1
JC
2319/*
2320 * generic initialization of ADC, input mixers and output mixers
2321 */
90dd48a1 2322static const struct hda_verb vt1709_10ch_volume_init_verbs[] = {
c577b8a1
JC
2323 /*
2324 * Unmute ADC0-2 and set the default input to mic-in
2325 */
2326 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2327 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2328 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2329
2330
f7278fd0 2331 /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
c577b8a1
JC
2332 * mixer widget
2333 */
2334 /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
f7278fd0
JC
2335 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2336 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2337 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2338 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2339 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
c577b8a1
JC
2340
2341 /*
2342 * Set up output selector (0x1a, 0x1b, 0x29)
2343 */
2344 /* set vol=0 to output mixers */
2345 {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2346 {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2347 {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2348
2349 /*
2350 * Unmute PW3 and PW4
2351 */
2352 {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2353 {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2354
bfdc675a
LW
2355 /* Set input of PW4 as MW0 */
2356 {0x20, AC_VERB_SET_CONNECT_SEL, 0},
c577b8a1
JC
2357 /* PW9 Output enable */
2358 {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2359 { }
2360};
2361
90dd48a1 2362static const struct hda_pcm_stream vt1709_10ch_pcm_analog_playback = {
c577b8a1
JC
2363 .substreams = 1,
2364 .channels_min = 2,
2365 .channels_max = 10,
2366 .nid = 0x10, /* NID to query formats and rates */
2367 .ops = {
2368 .open = via_playback_pcm_open,
c873cc25
LW
2369 .prepare = via_playback_multi_pcm_prepare,
2370 .cleanup = via_playback_multi_pcm_cleanup,
c577b8a1
JC
2371 },
2372};
2373
90dd48a1 2374static const struct hda_pcm_stream vt1709_6ch_pcm_analog_playback = {
c577b8a1
JC
2375 .substreams = 1,
2376 .channels_min = 2,
2377 .channels_max = 6,
2378 .nid = 0x10, /* NID to query formats and rates */
2379 .ops = {
2380 .open = via_playback_pcm_open,
c873cc25
LW
2381 .prepare = via_playback_multi_pcm_prepare,
2382 .cleanup = via_playback_multi_pcm_cleanup,
c577b8a1
JC
2383 },
2384};
2385
90dd48a1 2386static const struct hda_pcm_stream vt1709_pcm_analog_capture = {
c577b8a1
JC
2387 .substreams = 2,
2388 .channels_min = 2,
2389 .channels_max = 2,
2390 .nid = 0x14, /* NID to query formats and rates */
2391 .ops = {
2392 .prepare = via_capture_pcm_prepare,
2393 .cleanup = via_capture_pcm_cleanup
2394 },
2395};
2396
90dd48a1 2397static const struct hda_pcm_stream vt1709_pcm_digital_playback = {
c577b8a1
JC
2398 .substreams = 1,
2399 .channels_min = 2,
2400 .channels_max = 2,
2401 /* NID is set in via_build_pcms */
2402 .ops = {
2403 .open = via_dig_playback_pcm_open,
2404 .close = via_dig_playback_pcm_close
2405 },
2406};
2407
90dd48a1 2408static const struct hda_pcm_stream vt1709_pcm_digital_capture = {
c577b8a1
JC
2409 .substreams = 1,
2410 .channels_min = 2,
2411 .channels_max = 2,
2412};
2413
2414static int vt1709_auto_fill_dac_nids(struct via_spec *spec,
2415 const struct auto_pin_cfg *cfg)
2416{
2417 int i;
2418 hda_nid_t nid;
2419
2420 if (cfg->line_outs == 4) /* 10 channels */
2421 spec->multiout.num_dacs = cfg->line_outs+1; /* AOW0~AOW4 */
2422 else if (cfg->line_outs == 3) /* 6 channels */
2423 spec->multiout.num_dacs = cfg->line_outs; /* AOW0~AOW2 */
2424
2425 spec->multiout.dac_nids = spec->private_dac_nids;
2426
2427 if (cfg->line_outs == 4) { /* 10 channels */
2428 for (i = 0; i < cfg->line_outs; i++) {
2429 nid = cfg->line_out_pins[i];
2430 if (nid) {
2431 /* config dac list */
2432 switch (i) {
2433 case AUTO_SEQ_FRONT:
2434 /* AOW0 */
dda14410 2435 spec->private_dac_nids[i] = 0x10;
c577b8a1
JC
2436 break;
2437 case AUTO_SEQ_CENLFE:
2438 /* AOW2 */
dda14410 2439 spec->private_dac_nids[i] = 0x12;
c577b8a1
JC
2440 break;
2441 case AUTO_SEQ_SURROUND:
2442 /* AOW3 */
dda14410 2443 spec->private_dac_nids[i] = 0x11;
c577b8a1
JC
2444 break;
2445 case AUTO_SEQ_SIDE:
2446 /* AOW1 */
dda14410 2447 spec->private_dac_nids[i] = 0x27;
c577b8a1
JC
2448 break;
2449 default:
2450 break;
2451 }
2452 }
2453 }
dda14410 2454 spec->private_dac_nids[cfg->line_outs] = 0x28; /* AOW4 */
c577b8a1
JC
2455
2456 } else if (cfg->line_outs == 3) { /* 6 channels */
377ff31a 2457 for (i = 0; i < cfg->line_outs; i++) {
c577b8a1
JC
2458 nid = cfg->line_out_pins[i];
2459 if (nid) {
2460 /* config dac list */
377ff31a 2461 switch (i) {
c577b8a1
JC
2462 case AUTO_SEQ_FRONT:
2463 /* AOW0 */
dda14410 2464 spec->private_dac_nids[i] = 0x10;
c577b8a1
JC
2465 break;
2466 case AUTO_SEQ_CENLFE:
2467 /* AOW2 */
dda14410 2468 spec->private_dac_nids[i] = 0x12;
c577b8a1
JC
2469 break;
2470 case AUTO_SEQ_SURROUND:
2471 /* AOW1 */
dda14410 2472 spec->private_dac_nids[i] = 0x11;
c577b8a1
JC
2473 break;
2474 default:
2475 break;
2476 }
2477 }
2478 }
2479 }
2480
2481 return 0;
2482}
2483
2484/* add playback controls from the parsed DAC table */
2485static int vt1709_auto_create_multi_out_ctls(struct via_spec *spec,
2486 const struct auto_pin_cfg *cfg)
2487{
2488 char name[32];
ea734963
TI
2489 static const char * const chname[4] = {
2490 "Front", "Surround", "C/LFE", "Side"
2491 };
4483a2f5 2492 hda_nid_t nid, nid_vol, nid_vols[] = {0x18, 0x1a, 0x1b, 0x29};
c577b8a1
JC
2493 int i, err;
2494
2495 for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
2496 nid = cfg->line_out_pins[i];
2497
377ff31a 2498 if (!nid)
c577b8a1
JC
2499 continue;
2500
4483a2f5
LW
2501 nid_vol = nid_vols[i];
2502
c577b8a1
JC
2503 if (i == AUTO_SEQ_CENLFE) {
2504 /* Center/LFE */
2505 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2506 "Center Playback Volume",
4483a2f5 2507 HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
f7278fd0 2508 HDA_OUTPUT));
c577b8a1
JC
2509 if (err < 0)
2510 return err;
2511 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2512 "LFE Playback Volume",
4483a2f5 2513 HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
f7278fd0 2514 HDA_OUTPUT));
c577b8a1
JC
2515 if (err < 0)
2516 return err;
2517 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2518 "Center Playback Switch",
4483a2f5 2519 HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
f7278fd0 2520 HDA_OUTPUT));
c577b8a1
JC
2521 if (err < 0)
2522 return err;
2523 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2524 "LFE Playback Switch",
4483a2f5 2525 HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
f7278fd0 2526 HDA_OUTPUT));
c577b8a1
JC
2527 if (err < 0)
2528 return err;
377ff31a 2529 } else if (i == AUTO_SEQ_FRONT) {
4483a2f5 2530 /* ADD control to mixer index 0 */
c577b8a1
JC
2531 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2532 "Master Front Playback Volume",
4483a2f5 2533 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
f7278fd0 2534 HDA_INPUT));
c577b8a1
JC
2535 if (err < 0)
2536 return err;
2537 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2538 "Master Front Playback Switch",
4483a2f5 2539 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
f7278fd0 2540 HDA_INPUT));
c577b8a1
JC
2541 if (err < 0)
2542 return err;
377ff31a 2543
c577b8a1
JC
2544 /* add control to PW3 */
2545 sprintf(name, "%s Playback Volume", chname[i]);
2546 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
f7278fd0
JC
2547 HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2548 HDA_OUTPUT));
c577b8a1
JC
2549 if (err < 0)
2550 return err;
2551 sprintf(name, "%s Playback Switch", chname[i]);
2552 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
f7278fd0
JC
2553 HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2554 HDA_OUTPUT));
c577b8a1
JC
2555 if (err < 0)
2556 return err;
2557 } else if (i == AUTO_SEQ_SURROUND) {
2558 sprintf(name, "%s Playback Volume", chname[i]);
2559 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
4483a2f5 2560 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
f7278fd0 2561 HDA_OUTPUT));
c577b8a1
JC
2562 if (err < 0)
2563 return err;
2564 sprintf(name, "%s Playback Switch", chname[i]);
2565 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
4483a2f5 2566 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
f7278fd0 2567 HDA_OUTPUT));
c577b8a1
JC
2568 if (err < 0)
2569 return err;
2570 } else if (i == AUTO_SEQ_SIDE) {
2571 sprintf(name, "%s Playback Volume", chname[i]);
2572 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
4483a2f5 2573 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
f7278fd0 2574 HDA_OUTPUT));
c577b8a1
JC
2575 if (err < 0)
2576 return err;
2577 sprintf(name, "%s Playback Switch", chname[i]);
2578 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
4483a2f5 2579 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
f7278fd0 2580 HDA_OUTPUT));
c577b8a1
JC
2581 if (err < 0)
2582 return err;
2583 }
2584 }
2585
2586 return 0;
2587}
2588
2589static int vt1709_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
2590{
2591 int err;
2592
2593 if (!pin)
2594 return 0;
2595
2596 if (spec->multiout.num_dacs == 5) /* 10 channels */
2597 spec->multiout.hp_nid = VT1709_HP_DAC_NID;
2598 else if (spec->multiout.num_dacs == 3) /* 6 channels */
2599 spec->multiout.hp_nid = 0;
cdc1784d 2600 spec->hp_independent_mode_index = 1;
c577b8a1
JC
2601
2602 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2603 "Headphone Playback Volume",
2604 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2605 if (err < 0)
2606 return err;
2607 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2608 "Headphone Playback Switch",
2609 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2610 if (err < 0)
2611 return err;
2612
2613 return 0;
2614}
2615
2616/* create playback/capture controls for input pins */
10a20af7 2617static int vt1709_auto_create_analog_input_ctls(struct hda_codec *codec,
c577b8a1
JC
2618 const struct auto_pin_cfg *cfg)
2619{
90dd48a1 2620 static const hda_nid_t pin_idxs[] = { 0xff, 0x23, 0x1d, 0x1e, 0x21 };
10a20af7 2621 return vt_auto_create_analog_input_ctls(codec, cfg, 0x18, pin_idxs,
f3268512 2622 ARRAY_SIZE(pin_idxs));
c577b8a1
JC
2623}
2624
2625static int vt1709_parse_auto_config(struct hda_codec *codec)
2626{
2627 struct via_spec *spec = codec->spec;
2628 int err;
2629
2630 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2631 if (err < 0)
2632 return err;
2633 err = vt1709_auto_fill_dac_nids(spec, &spec->autocfg);
2634 if (err < 0)
2635 return err;
2636 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2637 return 0; /* can't find valid BIOS pin config */
2638
2639 err = vt1709_auto_create_multi_out_ctls(spec, &spec->autocfg);
2640 if (err < 0)
2641 return err;
2642 err = vt1709_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2643 if (err < 0)
2644 return err;
10a20af7 2645 err = vt1709_auto_create_analog_input_ctls(codec, &spec->autocfg);
c577b8a1
JC
2646 if (err < 0)
2647 return err;
2648
2649 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2650
0852d7a6 2651 if (spec->autocfg.dig_outs)
c577b8a1 2652 spec->multiout.dig_out_nid = VT1709_DIGOUT_NID;
55d1d6c1 2653 spec->dig_in_pin = VT1709_DIGIN_PIN;
c577b8a1
JC
2654 if (spec->autocfg.dig_in_pin)
2655 spec->dig_in_nid = VT1709_DIGIN_NID;
2656
603c4019
TI
2657 if (spec->kctls.list)
2658 spec->mixers[spec->num_mixers++] = spec->kctls.list;
c577b8a1 2659
0aa62aef 2660 spec->input_mux = &spec->private_imux[0];
c577b8a1 2661
f8fdd495 2662 if (spec->hp_mux)
3d83e577 2663 via_hp_build(codec);
f8fdd495 2664
5b0cb1d8 2665 via_smart51_build(spec);
c577b8a1
JC
2666 return 1;
2667}
2668
cb53c626 2669#ifdef CONFIG_SND_HDA_POWER_SAVE
90dd48a1 2670static const struct hda_amp_list vt1709_loopbacks[] = {
cb53c626
TI
2671 { 0x18, HDA_INPUT, 1 },
2672 { 0x18, HDA_INPUT, 2 },
2673 { 0x18, HDA_INPUT, 3 },
2674 { 0x18, HDA_INPUT, 4 },
2675 { } /* end */
2676};
2677#endif
2678
c577b8a1
JC
2679static int patch_vt1709_10ch(struct hda_codec *codec)
2680{
2681 struct via_spec *spec;
2682 int err;
2683
2684 /* create a codec specific record */
5b0cb1d8 2685 spec = via_new_spec(codec);
c577b8a1
JC
2686 if (spec == NULL)
2687 return -ENOMEM;
2688
c577b8a1
JC
2689 err = vt1709_parse_auto_config(codec);
2690 if (err < 0) {
2691 via_free(codec);
2692 return err;
2693 } else if (!err) {
2694 printk(KERN_INFO "hda_codec: Cannot set up configuration. "
2695 "Using genenic mode...\n");
2696 }
2697
69e52a80
HW
2698 spec->init_verbs[spec->num_iverbs++] = vt1709_10ch_volume_init_verbs;
2699 spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
c577b8a1
JC
2700
2701 spec->stream_name_analog = "VT1709 Analog";
2702 spec->stream_analog_playback = &vt1709_10ch_pcm_analog_playback;
2703 spec->stream_analog_capture = &vt1709_pcm_analog_capture;
2704
2705 spec->stream_name_digital = "VT1709 Digital";
2706 spec->stream_digital_playback = &vt1709_pcm_digital_playback;
2707 spec->stream_digital_capture = &vt1709_pcm_digital_capture;
2708
377ff31a 2709
c577b8a1
JC
2710 if (!spec->adc_nids && spec->input_mux) {
2711 spec->adc_nids = vt1709_adc_nids;
2712 spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
337b9d02 2713 get_mux_nids(codec);
c577b8a1
JC
2714 spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
2715 spec->num_mixers++;
2716 }
2717
2718 codec->patch_ops = via_patch_ops;
2719
2720 codec->patch_ops.init = via_auto_init;
69e52a80 2721 codec->patch_ops.unsol_event = via_unsol_event;
cb53c626
TI
2722#ifdef CONFIG_SND_HDA_POWER_SAVE
2723 spec->loopback.amplist = vt1709_loopbacks;
2724#endif
c577b8a1
JC
2725
2726 return 0;
2727}
2728/*
2729 * generic initialization of ADC, input mixers and output mixers
2730 */
90dd48a1 2731static const struct hda_verb vt1709_6ch_volume_init_verbs[] = {
c577b8a1
JC
2732 /*
2733 * Unmute ADC0-2 and set the default input to mic-in
2734 */
2735 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2736 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2737 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2738
2739
2740 /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2741 * mixer widget
2742 */
2743 /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2744 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2745 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2746 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2747 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2748 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2749
2750 /*
2751 * Set up output selector (0x1a, 0x1b, 0x29)
2752 */
2753 /* set vol=0 to output mixers */
2754 {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2755 {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2756 {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2757
2758 /*
2759 * Unmute PW3 and PW4
2760 */
2761 {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2762 {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2763
2764 /* Set input of PW4 as MW0 */
2765 {0x20, AC_VERB_SET_CONNECT_SEL, 0},
c577b8a1
JC
2766 /* PW9 Output enable */
2767 {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2768 { }
2769};
2770
2771static int patch_vt1709_6ch(struct hda_codec *codec)
2772{
2773 struct via_spec *spec;
2774 int err;
2775
2776 /* create a codec specific record */
5b0cb1d8 2777 spec = via_new_spec(codec);
c577b8a1
JC
2778 if (spec == NULL)
2779 return -ENOMEM;
2780
c577b8a1
JC
2781 err = vt1709_parse_auto_config(codec);
2782 if (err < 0) {
2783 via_free(codec);
2784 return err;
2785 } else if (!err) {
2786 printk(KERN_INFO "hda_codec: Cannot set up configuration. "
2787 "Using genenic mode...\n");
2788 }
2789
69e52a80
HW
2790 spec->init_verbs[spec->num_iverbs++] = vt1709_6ch_volume_init_verbs;
2791 spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
c577b8a1
JC
2792
2793 spec->stream_name_analog = "VT1709 Analog";
2794 spec->stream_analog_playback = &vt1709_6ch_pcm_analog_playback;
2795 spec->stream_analog_capture = &vt1709_pcm_analog_capture;
2796
2797 spec->stream_name_digital = "VT1709 Digital";
2798 spec->stream_digital_playback = &vt1709_pcm_digital_playback;
2799 spec->stream_digital_capture = &vt1709_pcm_digital_capture;
2800
377ff31a 2801
c577b8a1
JC
2802 if (!spec->adc_nids && spec->input_mux) {
2803 spec->adc_nids = vt1709_adc_nids;
2804 spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
337b9d02 2805 get_mux_nids(codec);
c577b8a1
JC
2806 spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
2807 spec->num_mixers++;
2808 }
2809
2810 codec->patch_ops = via_patch_ops;
2811
2812 codec->patch_ops.init = via_auto_init;
69e52a80 2813 codec->patch_ops.unsol_event = via_unsol_event;
cb53c626
TI
2814#ifdef CONFIG_SND_HDA_POWER_SAVE
2815 spec->loopback.amplist = vt1709_loopbacks;
2816#endif
f7278fd0
JC
2817 return 0;
2818}
2819
2820/* capture mixer elements */
90dd48a1 2821static const struct snd_kcontrol_new vt1708B_capture_mixer[] = {
f7278fd0
JC
2822 HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
2823 HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
2824 HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
2825 HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
2826 {
2827 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2828 /* The multiple "Capture Source" controls confuse alsamixer
2829 * So call somewhat different..
f7278fd0
JC
2830 */
2831 /* .name = "Capture Source", */
2832 .name = "Input Source",
2833 .count = 1,
2834 .info = via_mux_enum_info,
2835 .get = via_mux_enum_get,
2836 .put = via_mux_enum_put,
2837 },
2838 { } /* end */
2839};
2840/*
2841 * generic initialization of ADC, input mixers and output mixers
2842 */
90dd48a1 2843static const struct hda_verb vt1708B_8ch_volume_init_verbs[] = {
f7278fd0
JC
2844 /*
2845 * Unmute ADC0-1 and set the default input to mic-in
2846 */
2847 {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2848 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2849
2850
2851 /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2852 * mixer widget
2853 */
2854 /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2855 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2856 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2857 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2858 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2859 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2860
2861 /*
2862 * Set up output mixers
2863 */
2864 /* set vol=0 to output mixers */
2865 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2866 {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2867 {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2868
2869 /* Setup default input to PW4 */
bfdc675a 2870 {0x1d, AC_VERB_SET_CONNECT_SEL, 0},
f7278fd0
JC
2871 /* PW9 Output enable */
2872 {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2873 /* PW10 Input enable */
2874 {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2875 { }
2876};
2877
90dd48a1 2878static const struct hda_verb vt1708B_4ch_volume_init_verbs[] = {
f7278fd0
JC
2879 /*
2880 * Unmute ADC0-1 and set the default input to mic-in
2881 */
2882 {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2883 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2884
2885
2886 /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2887 * mixer widget
2888 */
2889 /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2890 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2891 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2892 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2893 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2894 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2895
2896 /*
2897 * Set up output mixers
2898 */
2899 /* set vol=0 to output mixers */
2900 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2901 {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2902 {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2903
2904 /* Setup default input of PW4 to MW0 */
2905 {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
2906 /* PW9 Output enable */
2907 {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2908 /* PW10 Input enable */
2909 {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2910 { }
2911};
2912
90dd48a1 2913static const struct hda_verb vt1708B_uniwill_init_verbs[] = {
a34df19a
LW
2914 {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
2915 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
2916 {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2917 {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2918 {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2919 {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2920 {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2921 {0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
2922 {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
69e52a80
HW
2923 { }
2924};
2925
17314379
LW
2926static int via_pcm_open_close(struct hda_pcm_stream *hinfo,
2927 struct hda_codec *codec,
2928 struct snd_pcm_substream *substream)
2929{
2930 int idle = substream->pstr->substream_opened == 1
2931 && substream->ref_count == 0;
2932
2933 analog_low_current_mode(codec, idle);
2934 return 0;
2935}
2936
90dd48a1 2937static const struct hda_pcm_stream vt1708B_8ch_pcm_analog_playback = {
0aa62aef 2938 .substreams = 2,
f7278fd0
JC
2939 .channels_min = 2,
2940 .channels_max = 8,
2941 .nid = 0x10, /* NID to query formats and rates */
2942 .ops = {
2943 .open = via_playback_pcm_open,
0aa62aef 2944 .prepare = via_playback_multi_pcm_prepare,
17314379
LW
2945 .cleanup = via_playback_multi_pcm_cleanup,
2946 .close = via_pcm_open_close
f7278fd0
JC
2947 },
2948};
2949
90dd48a1 2950static const struct hda_pcm_stream vt1708B_4ch_pcm_analog_playback = {
0aa62aef 2951 .substreams = 2,
f7278fd0
JC
2952 .channels_min = 2,
2953 .channels_max = 4,
2954 .nid = 0x10, /* NID to query formats and rates */
2955 .ops = {
2956 .open = via_playback_pcm_open,
0aa62aef
HW
2957 .prepare = via_playback_multi_pcm_prepare,
2958 .cleanup = via_playback_multi_pcm_cleanup
f7278fd0
JC
2959 },
2960};
2961
90dd48a1 2962static const struct hda_pcm_stream vt1708B_pcm_analog_capture = {
f7278fd0
JC
2963 .substreams = 2,
2964 .channels_min = 2,
2965 .channels_max = 2,
2966 .nid = 0x13, /* NID to query formats and rates */
2967 .ops = {
17314379 2968 .open = via_pcm_open_close,
f7278fd0 2969 .prepare = via_capture_pcm_prepare,
17314379
LW
2970 .cleanup = via_capture_pcm_cleanup,
2971 .close = via_pcm_open_close
f7278fd0
JC
2972 },
2973};
2974
90dd48a1 2975static const struct hda_pcm_stream vt1708B_pcm_digital_playback = {
f7278fd0
JC
2976 .substreams = 1,
2977 .channels_min = 2,
2978 .channels_max = 2,
2979 /* NID is set in via_build_pcms */
2980 .ops = {
2981 .open = via_dig_playback_pcm_open,
2982 .close = via_dig_playback_pcm_close,
9da29271
TI
2983 .prepare = via_dig_playback_pcm_prepare,
2984 .cleanup = via_dig_playback_pcm_cleanup
f7278fd0
JC
2985 },
2986};
2987
90dd48a1 2988static const struct hda_pcm_stream vt1708B_pcm_digital_capture = {
f7278fd0
JC
2989 .substreams = 1,
2990 .channels_min = 2,
2991 .channels_max = 2,
2992};
2993
2994/* fill in the dac_nids table from the parsed pin configuration */
2995static int vt1708B_auto_fill_dac_nids(struct via_spec *spec,
2996 const struct auto_pin_cfg *cfg)
2997{
2998 int i;
2999 hda_nid_t nid;
3000
3001 spec->multiout.num_dacs = cfg->line_outs;
3002
3003 spec->multiout.dac_nids = spec->private_dac_nids;
3004
3005 for (i = 0; i < 4; i++) {
3006 nid = cfg->line_out_pins[i];
3007 if (nid) {
3008 /* config dac list */
3009 switch (i) {
3010 case AUTO_SEQ_FRONT:
dda14410 3011 spec->private_dac_nids[i] = 0x10;
f7278fd0
JC
3012 break;
3013 case AUTO_SEQ_CENLFE:
dda14410 3014 spec->private_dac_nids[i] = 0x24;
f7278fd0
JC
3015 break;
3016 case AUTO_SEQ_SURROUND:
dda14410 3017 spec->private_dac_nids[i] = 0x11;
f7278fd0
JC
3018 break;
3019 case AUTO_SEQ_SIDE:
dda14410 3020 spec->private_dac_nids[i] = 0x25;
f7278fd0
JC
3021 break;
3022 }
3023 }
3024 }
3025
3026 return 0;
3027}
3028
3029/* add playback controls from the parsed DAC table */
3030static int vt1708B_auto_create_multi_out_ctls(struct via_spec *spec,
3031 const struct auto_pin_cfg *cfg)
3032{
3033 char name[32];
ea734963
TI
3034 static const char * const chname[4] = {
3035 "Front", "Surround", "C/LFE", "Side"
3036 };
fb4cb772 3037 hda_nid_t nid_vols[] = {0x16, 0x18, 0x26, 0x27};
f7278fd0
JC
3038 hda_nid_t nid, nid_vol = 0;
3039 int i, err;
3040
3041 for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
3042 nid = cfg->line_out_pins[i];
3043
3044 if (!nid)
3045 continue;
3046
3047 nid_vol = nid_vols[i];
3048
3049 if (i == AUTO_SEQ_CENLFE) {
3050 /* Center/LFE */
3051 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3052 "Center Playback Volume",
3053 HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
3054 HDA_OUTPUT));
3055 if (err < 0)
3056 return err;
3057 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3058 "LFE Playback Volume",
3059 HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
3060 HDA_OUTPUT));
3061 if (err < 0)
3062 return err;
3063 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3064 "Center Playback Switch",
3065 HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
3066 HDA_OUTPUT));
3067 if (err < 0)
3068 return err;
3069 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3070 "LFE Playback Switch",
3071 HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
3072 HDA_OUTPUT));
3073 if (err < 0)
3074 return err;
3075 } else if (i == AUTO_SEQ_FRONT) {
3076 /* add control to mixer index 0 */
3077 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3078 "Master Front Playback Volume",
3079 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3080 HDA_INPUT));
3081 if (err < 0)
3082 return err;
3083 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3084 "Master Front Playback Switch",
3085 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3086 HDA_INPUT));
3087 if (err < 0)
3088 return err;
3089
3090 /* add control to PW3 */
3091 sprintf(name, "%s Playback Volume", chname[i]);
3092 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3093 HDA_COMPOSE_AMP_VAL(nid, 3, 0,
3094 HDA_OUTPUT));
3095 if (err < 0)
3096 return err;
3097 sprintf(name, "%s Playback Switch", chname[i]);
3098 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3099 HDA_COMPOSE_AMP_VAL(nid, 3, 0,
3100 HDA_OUTPUT));
3101 if (err < 0)
3102 return err;
3103 } else {
3104 sprintf(name, "%s Playback Volume", chname[i]);
3105 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3106 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3107 HDA_OUTPUT));
3108 if (err < 0)
3109 return err;
3110 sprintf(name, "%s Playback Switch", chname[i]);
3111 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3112 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3113 HDA_OUTPUT));
3114 if (err < 0)
3115 return err;
3116 }
3117 }
3118
3119 return 0;
3120}
3121
3122static int vt1708B_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
3123{
3124 int err;
3125
3126 if (!pin)
3127 return 0;
3128
3129 spec->multiout.hp_nid = VT1708B_HP_NID; /* AOW3 */
cdc1784d 3130 spec->hp_independent_mode_index = 1;
f7278fd0
JC
3131
3132 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3133 "Headphone Playback Volume",
3134 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3135 if (err < 0)
3136 return err;
3137 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3138 "Headphone Playback Switch",
3139 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3140 if (err < 0)
3141 return err;
3142
0aa62aef
HW
3143 create_hp_imux(spec);
3144
f7278fd0
JC
3145 return 0;
3146}
3147
3148/* create playback/capture controls for input pins */
10a20af7 3149static int vt1708B_auto_create_analog_input_ctls(struct hda_codec *codec,
f7278fd0
JC
3150 const struct auto_pin_cfg *cfg)
3151{
90dd48a1 3152 static const hda_nid_t pin_idxs[] = { 0xff, 0x1f, 0x1a, 0x1b, 0x1e };
10a20af7 3153 return vt_auto_create_analog_input_ctls(codec, cfg, 0x16, pin_idxs,
f3268512 3154 ARRAY_SIZE(pin_idxs));
f7278fd0
JC
3155}
3156
3157static int vt1708B_parse_auto_config(struct hda_codec *codec)
3158{
3159 struct via_spec *spec = codec->spec;
3160 int err;
3161
3162 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3163 if (err < 0)
3164 return err;
3165 err = vt1708B_auto_fill_dac_nids(spec, &spec->autocfg);
3166 if (err < 0)
3167 return err;
3168 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3169 return 0; /* can't find valid BIOS pin config */
3170
3171 err = vt1708B_auto_create_multi_out_ctls(spec, &spec->autocfg);
3172 if (err < 0)
3173 return err;
3174 err = vt1708B_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
3175 if (err < 0)
3176 return err;
10a20af7 3177 err = vt1708B_auto_create_analog_input_ctls(codec, &spec->autocfg);
f7278fd0
JC
3178 if (err < 0)
3179 return err;
3180
3181 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3182
0852d7a6 3183 if (spec->autocfg.dig_outs)
f7278fd0 3184 spec->multiout.dig_out_nid = VT1708B_DIGOUT_NID;
55d1d6c1 3185 spec->dig_in_pin = VT1708B_DIGIN_PIN;
f7278fd0
JC
3186 if (spec->autocfg.dig_in_pin)
3187 spec->dig_in_nid = VT1708B_DIGIN_NID;
3188
603c4019
TI
3189 if (spec->kctls.list)
3190 spec->mixers[spec->num_mixers++] = spec->kctls.list;
f7278fd0 3191
0aa62aef
HW
3192 spec->input_mux = &spec->private_imux[0];
3193
f8fdd495 3194 if (spec->hp_mux)
3d83e577 3195 via_hp_build(codec);
f7278fd0 3196
5b0cb1d8 3197 via_smart51_build(spec);
f7278fd0
JC
3198 return 1;
3199}
3200
3201#ifdef CONFIG_SND_HDA_POWER_SAVE
90dd48a1 3202static const struct hda_amp_list vt1708B_loopbacks[] = {
f7278fd0
JC
3203 { 0x16, HDA_INPUT, 1 },
3204 { 0x16, HDA_INPUT, 2 },
3205 { 0x16, HDA_INPUT, 3 },
3206 { 0x16, HDA_INPUT, 4 },
3207 { } /* end */
3208};
3209#endif
3e95b9ab
LW
3210
3211static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
3212{
3213 struct via_spec *spec = codec->spec;
3214 int imux_is_smixer;
3215 unsigned int parm;
3216 int is_8ch = 0;
bc92df7f
LW
3217 if ((spec->codec_type != VT1708B_4CH) &&
3218 (codec->vendor_id != 0x11064397))
3e95b9ab
LW
3219 is_8ch = 1;
3220
3221 /* SW0 (17h) = stereo mixer */
3222 imux_is_smixer =
3223 (snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
3224 == ((spec->codec_type == VT1708S) ? 5 : 0));
3225 /* inputs */
3226 /* PW 1/2/5 (1ah/1bh/1eh) */
3227 parm = AC_PWRST_D3;
3228 set_pin_power_state(codec, 0x1a, &parm);
3229 set_pin_power_state(codec, 0x1b, &parm);
3230 set_pin_power_state(codec, 0x1e, &parm);
3231 if (imux_is_smixer)
3232 parm = AC_PWRST_D0;
3233 /* SW0 (17h), AIW 0/1 (13h/14h) */
3234 snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
3235 snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
3236 snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
3237
3238 /* outputs */
3239 /* PW0 (19h), SW1 (18h), AOW1 (11h) */
3240 parm = AC_PWRST_D3;
3241 set_pin_power_state(codec, 0x19, &parm);
3242 if (spec->smart51_enabled)
3243 set_pin_power_state(codec, 0x1b, &parm);
3244 snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
3245 snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
3246
3247 /* PW6 (22h), SW2 (26h), AOW2 (24h) */
3248 if (is_8ch) {
3249 parm = AC_PWRST_D3;
3250 set_pin_power_state(codec, 0x22, &parm);
3251 if (spec->smart51_enabled)
3252 set_pin_power_state(codec, 0x1a, &parm);
3253 snd_hda_codec_write(codec, 0x26, 0,
3254 AC_VERB_SET_POWER_STATE, parm);
3255 snd_hda_codec_write(codec, 0x24, 0,
3256 AC_VERB_SET_POWER_STATE, parm);
bc92df7f
LW
3257 } else if (codec->vendor_id == 0x11064397) {
3258 /* PW7(23h), SW2(27h), AOW2(25h) */
3259 parm = AC_PWRST_D3;
3260 set_pin_power_state(codec, 0x23, &parm);
3261 if (spec->smart51_enabled)
3262 set_pin_power_state(codec, 0x1a, &parm);
3263 snd_hda_codec_write(codec, 0x27, 0,
3264 AC_VERB_SET_POWER_STATE, parm);
3265 snd_hda_codec_write(codec, 0x25, 0,
3266 AC_VERB_SET_POWER_STATE, parm);
3e95b9ab
LW
3267 }
3268
3269 /* PW 3/4/7 (1ch/1dh/23h) */
3270 parm = AC_PWRST_D3;
3271 /* force to D0 for internal Speaker */
3272 set_pin_power_state(codec, 0x1c, &parm);
3273 set_pin_power_state(codec, 0x1d, &parm);
3274 if (is_8ch)
3275 set_pin_power_state(codec, 0x23, &parm);
3276
3277 /* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
3278 snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
3279 imux_is_smixer ? AC_PWRST_D0 : parm);
3280 snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
3281 if (is_8ch) {
3282 snd_hda_codec_write(codec, 0x25, 0,
3283 AC_VERB_SET_POWER_STATE, parm);
3284 snd_hda_codec_write(codec, 0x27, 0,
3285 AC_VERB_SET_POWER_STATE, parm);
bc92df7f
LW
3286 } else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
3287 snd_hda_codec_write(codec, 0x25, 0,
3288 AC_VERB_SET_POWER_STATE, parm);
3e95b9ab
LW
3289}
3290
518bf3ba 3291static int patch_vt1708S(struct hda_codec *codec);
f7278fd0
JC
3292static int patch_vt1708B_8ch(struct hda_codec *codec)
3293{
3294 struct via_spec *spec;
3295 int err;
3296
518bf3ba
LW
3297 if (get_codec_type(codec) == VT1708BCE)
3298 return patch_vt1708S(codec);
f7278fd0 3299 /* create a codec specific record */
5b0cb1d8 3300 spec = via_new_spec(codec);
f7278fd0
JC
3301 if (spec == NULL)
3302 return -ENOMEM;
3303
f7278fd0
JC
3304 /* automatic parse from the BIOS config */
3305 err = vt1708B_parse_auto_config(codec);
3306 if (err < 0) {
3307 via_free(codec);
3308 return err;
3309 } else if (!err) {
3310 printk(KERN_INFO "hda_codec: Cannot set up configuration "
3311 "from BIOS. Using genenic mode...\n");
3312 }
3313
69e52a80
HW
3314 spec->init_verbs[spec->num_iverbs++] = vt1708B_8ch_volume_init_verbs;
3315 spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
f7278fd0
JC
3316
3317 spec->stream_name_analog = "VT1708B Analog";
3318 spec->stream_analog_playback = &vt1708B_8ch_pcm_analog_playback;
3319 spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
3320
3321 spec->stream_name_digital = "VT1708B Digital";
3322 spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
3323 spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
3324
3325 if (!spec->adc_nids && spec->input_mux) {
3326 spec->adc_nids = vt1708B_adc_nids;
3327 spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
337b9d02 3328 get_mux_nids(codec);
f7278fd0
JC
3329 spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
3330 spec->num_mixers++;
3331 }
3332
3333 codec->patch_ops = via_patch_ops;
3334
3335 codec->patch_ops.init = via_auto_init;
69e52a80 3336 codec->patch_ops.unsol_event = via_unsol_event;
f7278fd0
JC
3337#ifdef CONFIG_SND_HDA_POWER_SAVE
3338 spec->loopback.amplist = vt1708B_loopbacks;
3339#endif
3340
3e95b9ab
LW
3341 spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
3342
f7278fd0
JC
3343 return 0;
3344}
3345
3346static int patch_vt1708B_4ch(struct hda_codec *codec)
3347{
3348 struct via_spec *spec;
3349 int err;
3350
3351 /* create a codec specific record */
5b0cb1d8 3352 spec = via_new_spec(codec);
f7278fd0
JC
3353 if (spec == NULL)
3354 return -ENOMEM;
3355
f7278fd0
JC
3356 /* automatic parse from the BIOS config */
3357 err = vt1708B_parse_auto_config(codec);
3358 if (err < 0) {
3359 via_free(codec);
3360 return err;
3361 } else if (!err) {
3362 printk(KERN_INFO "hda_codec: Cannot set up configuration "
3363 "from BIOS. Using genenic mode...\n");
3364 }
3365
69e52a80
HW
3366 spec->init_verbs[spec->num_iverbs++] = vt1708B_4ch_volume_init_verbs;
3367 spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
f7278fd0
JC
3368
3369 spec->stream_name_analog = "VT1708B Analog";
3370 spec->stream_analog_playback = &vt1708B_4ch_pcm_analog_playback;
3371 spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
3372
3373 spec->stream_name_digital = "VT1708B Digital";
3374 spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
3375 spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
3376
3377 if (!spec->adc_nids && spec->input_mux) {
3378 spec->adc_nids = vt1708B_adc_nids;
3379 spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
337b9d02 3380 get_mux_nids(codec);
f7278fd0
JC
3381 spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
3382 spec->num_mixers++;
3383 }
3384
3385 codec->patch_ops = via_patch_ops;
3386
3387 codec->patch_ops.init = via_auto_init;
69e52a80 3388 codec->patch_ops.unsol_event = via_unsol_event;
f7278fd0
JC
3389#ifdef CONFIG_SND_HDA_POWER_SAVE
3390 spec->loopback.amplist = vt1708B_loopbacks;
3391#endif
c577b8a1 3392
3e95b9ab
LW
3393 spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
3394
c577b8a1
JC
3395 return 0;
3396}
3397
d949cac1
HW
3398/* Patch for VT1708S */
3399
3400/* capture mixer elements */
90dd48a1 3401static const struct snd_kcontrol_new vt1708S_capture_mixer[] = {
d949cac1
HW
3402 HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
3403 HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
3404 HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
3405 HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
6369bcfc
LW
3406 HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x1A, 0x0, HDA_INPUT),
3407 HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x1E, 0x0,
3408 HDA_INPUT),
d949cac1
HW
3409 {
3410 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3411 /* The multiple "Capture Source" controls confuse alsamixer
3412 * So call somewhat different..
3413 */
3414 /* .name = "Capture Source", */
3415 .name = "Input Source",
3416 .count = 1,
3417 .info = via_mux_enum_info,
3418 .get = via_mux_enum_get,
3419 .put = via_mux_enum_put,
3420 },
3421 { } /* end */
3422};
3423
90dd48a1 3424static const struct hda_verb vt1708S_volume_init_verbs[] = {
d949cac1
HW
3425 /* Unmute ADC0-1 and set the default input to mic-in */
3426 {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3427 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3428
3429 /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the
3430 * analog-loopback mixer widget */
3431 /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
3432 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3433 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3434 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3435 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3436 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
3437
3438 /* Setup default input of PW4 to MW0 */
3439 {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
5691ec7f 3440 /* PW9, PW10 Output enable */
d949cac1 3441 {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
5691ec7f 3442 {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
d7426329
HW
3443 /* Enable Mic Boost Volume backdoor */
3444 {0x1, 0xf98, 0x1},
bc7e7e5c
LW
3445 /* don't bybass mixer */
3446 {0x1, 0xf88, 0xc0},
d949cac1
HW
3447 { }
3448};
3449
90dd48a1 3450static const struct hda_verb vt1708S_uniwill_init_verbs[] = {
a34df19a
LW
3451 {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
3452 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3453 {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3454 {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3455 {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3456 {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3457 {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3458 {0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3459 {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
69e52a80
HW
3460 { }
3461};
3462
90dd48a1 3463static const struct hda_verb vt1705_uniwill_init_verbs[] = {
bc92df7f
LW
3464 {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
3465 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3466 {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3467 {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3468 {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3469 {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3470 {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3471 {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3472 { }
3473};
3474
90dd48a1 3475static const struct hda_pcm_stream vt1708S_pcm_analog_playback = {
d949cac1
HW
3476 .substreams = 2,
3477 .channels_min = 2,
3478 .channels_max = 8,
3479 .nid = 0x10, /* NID to query formats and rates */
3480 .ops = {
3481 .open = via_playback_pcm_open,
c873cc25
LW
3482 .prepare = via_playback_multi_pcm_prepare,
3483 .cleanup = via_playback_multi_pcm_cleanup,
17314379 3484 .close = via_pcm_open_close
d949cac1
HW
3485 },
3486};
3487
90dd48a1 3488static const struct hda_pcm_stream vt1705_pcm_analog_playback = {
bc92df7f
LW
3489 .substreams = 2,
3490 .channels_min = 2,
3491 .channels_max = 6,
3492 .nid = 0x10, /* NID to query formats and rates */
3493 .ops = {
3494 .open = via_playback_pcm_open,
3495 .prepare = via_playback_multi_pcm_prepare,
3496 .cleanup = via_playback_multi_pcm_cleanup,
3497 .close = via_pcm_open_close
3498 },
3499};
3500
90dd48a1 3501static const struct hda_pcm_stream vt1708S_pcm_analog_capture = {
d949cac1
HW
3502 .substreams = 2,
3503 .channels_min = 2,
3504 .channels_max = 2,
3505 .nid = 0x13, /* NID to query formats and rates */
3506 .ops = {
17314379 3507 .open = via_pcm_open_close,
d949cac1 3508 .prepare = via_capture_pcm_prepare,
17314379
LW
3509 .cleanup = via_capture_pcm_cleanup,
3510 .close = via_pcm_open_close
d949cac1
HW
3511 },
3512};
3513
90dd48a1 3514static const struct hda_pcm_stream vt1708S_pcm_digital_playback = {
9da29271 3515 .substreams = 1,
d949cac1
HW
3516 .channels_min = 2,
3517 .channels_max = 2,
3518 /* NID is set in via_build_pcms */
3519 .ops = {
3520 .open = via_dig_playback_pcm_open,
3521 .close = via_dig_playback_pcm_close,
9da29271
TI
3522 .prepare = via_dig_playback_pcm_prepare,
3523 .cleanup = via_dig_playback_pcm_cleanup
d949cac1
HW
3524 },
3525};
3526
3527/* fill in the dac_nids table from the parsed pin configuration */
3528static int vt1708S_auto_fill_dac_nids(struct via_spec *spec,
3529 const struct auto_pin_cfg *cfg)
3530{
3531 int i;
3532 hda_nid_t nid;
3533
3534 spec->multiout.num_dacs = cfg->line_outs;
3535
3536 spec->multiout.dac_nids = spec->private_dac_nids;
3537
3538 for (i = 0; i < 4; i++) {
3539 nid = cfg->line_out_pins[i];
3540 if (nid) {
3541 /* config dac list */
3542 switch (i) {
3543 case AUTO_SEQ_FRONT:
dda14410 3544 spec->private_dac_nids[i] = 0x10;
d949cac1
HW
3545 break;
3546 case AUTO_SEQ_CENLFE:
bc92df7f 3547 if (spec->codec->vendor_id == 0x11064397)
dda14410 3548 spec->private_dac_nids[i] = 0x25;
bc92df7f 3549 else
dda14410 3550 spec->private_dac_nids[i] = 0x24;
d949cac1
HW
3551 break;
3552 case AUTO_SEQ_SURROUND:
dda14410 3553 spec->private_dac_nids[i] = 0x11;
d949cac1
HW
3554 break;
3555 case AUTO_SEQ_SIDE:
dda14410 3556 spec->private_dac_nids[i] = 0x25;
d949cac1
HW
3557 break;
3558 }
3559 }
3560 }
3561
32e0191d
CL
3562 /* for Smart 5.1, line/mic inputs double as output pins */
3563 if (cfg->line_outs == 1) {
3564 spec->multiout.num_dacs = 3;
dda14410 3565 spec->private_dac_nids[AUTO_SEQ_SURROUND] = 0x11;
bc92df7f 3566 if (spec->codec->vendor_id == 0x11064397)
dda14410 3567 spec->private_dac_nids[AUTO_SEQ_CENLFE] = 0x25;
bc92df7f 3568 else
dda14410 3569 spec->private_dac_nids[AUTO_SEQ_CENLFE] = 0x24;
32e0191d
CL
3570 }
3571
d949cac1
HW
3572 return 0;
3573}
3574
3575/* add playback controls from the parsed DAC table */
bc92df7f 3576static int vt1708S_auto_create_multi_out_ctls(struct hda_codec *codec,
d949cac1
HW
3577 const struct auto_pin_cfg *cfg)
3578{
bc92df7f 3579 struct via_spec *spec = codec->spec;
d949cac1 3580 char name[32];
ea734963
TI
3581 static const char * const chname[4] = {
3582 "Front", "Surround", "C/LFE", "Side"
3583 };
bc92df7f
LW
3584 hda_nid_t nid_vols[2][4] = { {0x10, 0x11, 0x24, 0x25},
3585 {0x10, 0x11, 0x25, 0} };
3586 hda_nid_t nid_mutes[2][4] = { {0x1C, 0x18, 0x26, 0x27},
3587 {0x1C, 0x18, 0x27, 0} };
d949cac1
HW
3588 hda_nid_t nid, nid_vol, nid_mute;
3589 int i, err;
3590
3591 for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
3592 nid = cfg->line_out_pins[i];
3593
32e0191d
CL
3594 /* for Smart 5.1, there are always at least six channels */
3595 if (!nid && i > AUTO_SEQ_CENLFE)
d949cac1
HW
3596 continue;
3597
bc92df7f
LW
3598 if (codec->vendor_id == 0x11064397) {
3599 nid_vol = nid_vols[1][i];
3600 nid_mute = nid_mutes[1][i];
3601 } else {
3602 nid_vol = nid_vols[0][i];
3603 nid_mute = nid_mutes[0][i];
3604 }
3605 if (!nid_vol && !nid_mute)
3606 continue;
d949cac1
HW
3607
3608 if (i == AUTO_SEQ_CENLFE) {
3609 /* Center/LFE */
3610 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3611 "Center Playback Volume",
3612 HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
3613 HDA_OUTPUT));
3614 if (err < 0)
3615 return err;
3616 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3617 "LFE Playback Volume",
3618 HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
3619 HDA_OUTPUT));
3620 if (err < 0)
3621 return err;
3622 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3623 "Center Playback Switch",
3624 HDA_COMPOSE_AMP_VAL(nid_mute,
3625 1, 0,
3626 HDA_OUTPUT));
3627 if (err < 0)
3628 return err;
3629 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3630 "LFE Playback Switch",
3631 HDA_COMPOSE_AMP_VAL(nid_mute,
3632 2, 0,
3633 HDA_OUTPUT));
3634 if (err < 0)
3635 return err;
3636 } else if (i == AUTO_SEQ_FRONT) {
3637 /* add control to mixer index 0 */
3638 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3639 "Master Front Playback Volume",
3640 HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
3641 HDA_INPUT));
3642 if (err < 0)
3643 return err;
3644 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3645 "Master Front Playback Switch",
3646 HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
3647 HDA_INPUT));
3648 if (err < 0)
3649 return err;
3650
3651 /* Front */
3652 sprintf(name, "%s Playback Volume", chname[i]);
3653 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3654 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3655 HDA_OUTPUT));
3656 if (err < 0)
3657 return err;
3658 sprintf(name, "%s Playback Switch", chname[i]);
3659 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3660 HDA_COMPOSE_AMP_VAL(nid_mute,
3661 3, 0,
3662 HDA_OUTPUT));
3663 if (err < 0)
3664 return err;
3665 } else {
3666 sprintf(name, "%s Playback Volume", chname[i]);
3667 err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
3668 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
3669 HDA_OUTPUT));
3670 if (err < 0)
3671 return err;
3672 sprintf(name, "%s Playback Switch", chname[i]);
3673 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
3674 HDA_COMPOSE_AMP_VAL(nid_mute,
3675 3, 0,
3676 HDA_OUTPUT));
3677 if (err < 0)
3678 return err;
3679 }
3680 }
3681
3682 return 0;
3683}
3684
3685static int vt1708S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
3686{
3687 int err;
3688
3689 if (!pin)
3690 return 0;
3691
3692 spec->multiout.hp_nid = VT1708S_HP_NID; /* AOW3 */
cdc1784d 3693 spec->hp_independent_mode_index = 1;
d949cac1
HW
3694
3695 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3696 "Headphone Playback Volume",
3697 HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
3698 if (err < 0)
3699 return err;
3700
3701 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3702 "Headphone Playback Switch",
3703 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3704 if (err < 0)
3705 return err;
3706
0aa62aef
HW
3707 create_hp_imux(spec);
3708
d949cac1
HW
3709 return 0;
3710}
3711
3712/* create playback/capture controls for input pins */
10a20af7 3713static int vt1708S_auto_create_analog_input_ctls(struct hda_codec *codec,
d949cac1
HW
3714 const struct auto_pin_cfg *cfg)
3715{
90dd48a1 3716 static const hda_nid_t pin_idxs[] = { 0x1f, 0x1a, 0x1b, 0x1e, 0, 0xff };
10a20af7 3717 return vt_auto_create_analog_input_ctls(codec, cfg, 0x16, pin_idxs,
f3268512 3718 ARRAY_SIZE(pin_idxs));
d949cac1
HW
3719}
3720
9da29271
TI
3721/* fill out digital output widgets; one for master and one for slave outputs */
3722static void fill_dig_outs(struct hda_codec *codec)
3723{
3724 struct via_spec *spec = codec->spec;
3725 int i;
3726
3727 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3728 hda_nid_t nid;
3729 int conn;
3730
3731 nid = spec->autocfg.dig_out_pins[i];
3732 if (!nid)
3733 continue;
3734 conn = snd_hda_get_connections(codec, nid, &nid, 1);
3735 if (conn < 1)
3736 continue;
3737 if (!spec->multiout.dig_out_nid)
3738 spec->multiout.dig_out_nid = nid;
3739 else {
3740 spec->slave_dig_outs[0] = nid;
3741 break; /* at most two dig outs */
3742 }
3743 }
3744}
3745
d949cac1
HW
3746static int vt1708S_parse_auto_config(struct hda_codec *codec)
3747{
3748 struct via_spec *spec = codec->spec;
3749 int err;
d949cac1 3750
9da29271 3751 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
d949cac1
HW
3752 if (err < 0)
3753 return err;
3754 err = vt1708S_auto_fill_dac_nids(spec, &spec->autocfg);
3755 if (err < 0)
3756 return err;
3757 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3758 return 0; /* can't find valid BIOS pin config */
3759
bc92df7f 3760 err = vt1708S_auto_create_multi_out_ctls(codec, &spec->autocfg);
d949cac1
HW
3761 if (err < 0)
3762 return err;
3763 err = vt1708S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
3764 if (err < 0)
3765 return err;
10a20af7 3766 err = vt1708S_auto_create_analog_input_ctls(codec, &spec->autocfg);
d949cac1
HW
3767 if (err < 0)
3768 return err;
3769
3770 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3771
9da29271 3772 fill_dig_outs(codec);
98aa34c0 3773
603c4019
TI
3774 if (spec->kctls.list)
3775 spec->mixers[spec->num_mixers++] = spec->kctls.list;
d949cac1 3776
0aa62aef
HW
3777 spec->input_mux = &spec->private_imux[0];
3778
f8fdd495 3779 if (spec->hp_mux)
3d83e577 3780 via_hp_build(codec);
d949cac1 3781
5b0cb1d8 3782 via_smart51_build(spec);
d949cac1
HW
3783 return 1;
3784}
3785
3786#ifdef CONFIG_SND_HDA_POWER_SAVE
90dd48a1 3787static const struct hda_amp_list vt1708S_loopbacks[] = {
d949cac1
HW
3788 { 0x16, HDA_INPUT, 1 },
3789 { 0x16, HDA_INPUT, 2 },
3790 { 0x16, HDA_INPUT, 3 },
3791 { 0x16, HDA_INPUT, 4 },
3792 { } /* end */
3793};
3794#endif
3795
6369bcfc
LW
3796static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
3797 int offset, int num_steps, int step_size)
3798{
3799 snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
3800 (offset << AC_AMPCAP_OFFSET_SHIFT) |
3801 (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
3802 (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
3803 (0 << AC_AMPCAP_MUTE_SHIFT));
3804}
3805
d949cac1
HW
3806static int patch_vt1708S(struct hda_codec *codec)
3807{
3808 struct via_spec *spec;
3809 int err;
3810
3811 /* create a codec specific record */
5b0cb1d8 3812 spec = via_new_spec(codec);
d949cac1
HW
3813 if (spec == NULL)
3814 return -ENOMEM;
3815
d949cac1
HW
3816 /* automatic parse from the BIOS config */
3817 err = vt1708S_parse_auto_config(codec);
3818 if (err < 0) {
3819 via_free(codec);
3820 return err;
3821 } else if (!err) {
3822 printk(KERN_INFO "hda_codec: Cannot set up configuration "
3823 "from BIOS. Using genenic mode...\n");
3824 }
3825
69e52a80 3826 spec->init_verbs[spec->num_iverbs++] = vt1708S_volume_init_verbs;
bc92df7f
LW
3827 if (codec->vendor_id == 0x11064397)
3828 spec->init_verbs[spec->num_iverbs++] =
3829 vt1705_uniwill_init_verbs;
3830 else
3831 spec->init_verbs[spec->num_iverbs++] =
3832 vt1708S_uniwill_init_verbs;
d949cac1 3833
36dd5c4a
LW
3834 if (codec->vendor_id == 0x11060440)
3835 spec->stream_name_analog = "VT1818S Analog";
bc92df7f
LW
3836 else if (codec->vendor_id == 0x11064397)
3837 spec->stream_name_analog = "VT1705 Analog";
36dd5c4a
LW
3838 else
3839 spec->stream_name_analog = "VT1708S Analog";
bc92df7f
LW
3840 if (codec->vendor_id == 0x11064397)
3841 spec->stream_analog_playback = &vt1705_pcm_analog_playback;
3842 else
3843 spec->stream_analog_playback = &vt1708S_pcm_analog_playback;
d949cac1
HW
3844 spec->stream_analog_capture = &vt1708S_pcm_analog_capture;
3845
36dd5c4a
LW
3846 if (codec->vendor_id == 0x11060440)
3847 spec->stream_name_digital = "VT1818S Digital";
bc92df7f
LW
3848 else if (codec->vendor_id == 0x11064397)
3849 spec->stream_name_digital = "VT1705 Digital";
36dd5c4a
LW
3850 else
3851 spec->stream_name_digital = "VT1708S Digital";
d949cac1
HW
3852 spec->stream_digital_playback = &vt1708S_pcm_digital_playback;
3853
3854 if (!spec->adc_nids && spec->input_mux) {
3855 spec->adc_nids = vt1708S_adc_nids;
3856 spec->num_adc_nids = ARRAY_SIZE(vt1708S_adc_nids);
337b9d02 3857 get_mux_nids(codec);
6369bcfc
LW
3858 override_mic_boost(codec, 0x1a, 0, 3, 40);
3859 override_mic_boost(codec, 0x1e, 0, 3, 40);
d949cac1
HW
3860 spec->mixers[spec->num_mixers] = vt1708S_capture_mixer;
3861 spec->num_mixers++;
3862 }
3863
3864 codec->patch_ops = via_patch_ops;
3865
3866 codec->patch_ops.init = via_auto_init;
69e52a80 3867 codec->patch_ops.unsol_event = via_unsol_event;
d949cac1
HW
3868#ifdef CONFIG_SND_HDA_POWER_SAVE
3869 spec->loopback.amplist = vt1708S_loopbacks;
3870#endif
3871
518bf3ba
LW
3872 /* correct names for VT1708BCE */
3873 if (get_codec_type(codec) == VT1708BCE) {
3874 kfree(codec->chip_name);
3875 codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
3876 snprintf(codec->bus->card->mixername,
3877 sizeof(codec->bus->card->mixername),
3878 "%s %s", codec->vendor_name, codec->chip_name);
3879 spec->stream_name_analog = "VT1708BCE Analog";
3880 spec->stream_name_digital = "VT1708BCE Digital";
3881 }
970f630f
LW
3882 /* correct names for VT1818S */
3883 if (codec->vendor_id == 0x11060440) {
3884 spec->stream_name_analog = "VT1818S Analog";
3885 spec->stream_name_digital = "VT1818S Digital";
3886 }
bc92df7f
LW
3887 /* correct names for VT1705 */
3888 if (codec->vendor_id == 0x11064397) {
3889 kfree(codec->chip_name);
3890 codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
3891 snprintf(codec->bus->card->mixername,
3892 sizeof(codec->bus->card->mixername),
3893 "%s %s", codec->vendor_name, codec->chip_name);
3894 }
3e95b9ab 3895 spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
d949cac1
HW
3896 return 0;
3897}
3898
3899/* Patch for VT1702 */
3900
3901/* capture mixer elements */
90dd48a1 3902static const struct snd_kcontrol_new vt1702_capture_mixer[] = {
d949cac1
HW
3903 HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_INPUT),
3904 HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_INPUT),
3905 HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x20, 0x0, HDA_INPUT),
3906 HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x20, 0x0, HDA_INPUT),
3907 HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x1F, 0x0, HDA_INPUT),
3908 HDA_CODEC_MUTE("Digital Mic Capture Switch", 0x1F, 0x0, HDA_INPUT),
3909 HDA_CODEC_VOLUME("Digital Mic Boost Capture Volume", 0x1E, 0x0,
3910 HDA_INPUT),
3911 {
3912 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3913 /* The multiple "Capture Source" controls confuse alsamixer
3914 * So call somewhat different..
3915 */
3916 /* .name = "Capture Source", */
3917 .name = "Input Source",
3918 .count = 1,
3919 .info = via_mux_enum_info,
3920 .get = via_mux_enum_get,
3921 .put = via_mux_enum_put,
3922 },
3923 { } /* end */
3924};
3925
90dd48a1 3926static const struct hda_verb vt1702_volume_init_verbs[] = {
d949cac1
HW
3927 /*
3928 * Unmute ADC0-1 and set the default input to mic-in
3929 */
3930 {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3931 {0x1F, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3932 {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3933
3934
3935 /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3936 * mixer widget
3937 */
3938 /* Amp Indices: Mic1 = 1, Line = 1, Mic2 = 3 */
3939 {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3940 {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3941 {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3942 {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3943 {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3944
3945 /* Setup default input of PW4 to MW0 */
3946 {0x17, AC_VERB_SET_CONNECT_SEL, 0x1},
3947 /* PW6 PW7 Output enable */
3948 {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3949 {0x1C, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
bc7e7e5c
LW
3950 /* mixer enable */
3951 {0x1, 0xF88, 0x3},
3952 /* GPIO 0~2 */
3953 {0x1, 0xF82, 0x3F},
d949cac1
HW
3954 { }
3955};
3956
90dd48a1 3957static const struct hda_verb vt1702_uniwill_init_verbs[] = {
a34df19a
LW
3958 {0x17, AC_VERB_SET_UNSOLICITED_ENABLE,
3959 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
3960 {0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3961 {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3962 {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
3963 {0x18, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
69e52a80
HW
3964 { }
3965};
3966
90dd48a1 3967static const struct hda_pcm_stream vt1702_pcm_analog_playback = {
0aa62aef 3968 .substreams = 2,
d949cac1
HW
3969 .channels_min = 2,
3970 .channels_max = 2,
3971 .nid = 0x10, /* NID to query formats and rates */
3972 .ops = {
3973 .open = via_playback_pcm_open,
0aa62aef 3974 .prepare = via_playback_multi_pcm_prepare,
17314379
LW
3975 .cleanup = via_playback_multi_pcm_cleanup,
3976 .close = via_pcm_open_close
d949cac1
HW
3977 },
3978};
3979
90dd48a1 3980static const struct hda_pcm_stream vt1702_pcm_analog_capture = {
d949cac1
HW
3981 .substreams = 3,
3982 .channels_min = 2,
3983 .channels_max = 2,
3984 .nid = 0x12, /* NID to query formats and rates */
3985 .ops = {
17314379 3986 .open = via_pcm_open_close,
d949cac1 3987 .prepare = via_capture_pcm_prepare,
17314379
LW
3988 .cleanup = via_capture_pcm_cleanup,
3989 .close = via_pcm_open_close
d949cac1
HW
3990 },
3991};
3992
90dd48a1 3993static const struct hda_pcm_stream vt1702_pcm_digital_playback = {
5691ec7f 3994 .substreams = 2,
d949cac1
HW
3995 .channels_min = 2,
3996 .channels_max = 2,
3997 /* NID is set in via_build_pcms */
3998 .ops = {
3999 .open = via_dig_playback_pcm_open,
4000 .close = via_dig_playback_pcm_close,
9da29271
TI
4001 .prepare = via_dig_playback_pcm_prepare,
4002 .cleanup = via_dig_playback_pcm_cleanup
d949cac1
HW
4003 },
4004};
4005
4006/* fill in the dac_nids table from the parsed pin configuration */
4007static int vt1702_auto_fill_dac_nids(struct via_spec *spec,
4008 const struct auto_pin_cfg *cfg)
4009{
4010 spec->multiout.num_dacs = 1;
4011 spec->multiout.dac_nids = spec->private_dac_nids;
4012
4013 if (cfg->line_out_pins[0]) {
4014 /* config dac list */
dda14410 4015 spec->private_dac_nids[0] = 0x10;
d949cac1
HW
4016 }
4017
4018 return 0;
4019}
4020
4021/* add playback controls from the parsed DAC table */
4022static int vt1702_auto_create_line_out_ctls(struct via_spec *spec,
4023 const struct auto_pin_cfg *cfg)
4024{
4025 int err;
4026
4027 if (!cfg->line_out_pins[0])
4028 return -1;
4029
4030 /* add control to mixer index 0 */
4031 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4032 "Master Front Playback Volume",
4033 HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
4034 if (err < 0)
4035 return err;
4036 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4037 "Master Front Playback Switch",
4038 HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
4039 if (err < 0)
4040 return err;
4041
4042 /* Front */
4043 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4044 "Front Playback Volume",
4045 HDA_COMPOSE_AMP_VAL(0x10, 3, 0, HDA_OUTPUT));
4046 if (err < 0)
4047 return err;
4048 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4049 "Front Playback Switch",
4050 HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_OUTPUT));
4051 if (err < 0)
4052 return err;
4053
4054 return 0;
4055}
4056
4057static int vt1702_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
4058{
0713efeb
LW
4059 int err, i;
4060 struct hda_input_mux *imux;
ea734963 4061 static const char * const texts[] = { "ON", "OFF", NULL};
d949cac1
HW
4062 if (!pin)
4063 return 0;
d949cac1 4064 spec->multiout.hp_nid = 0x1D;
cdc1784d 4065 spec->hp_independent_mode_index = 0;
d949cac1
HW
4066
4067 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4068 "Headphone Playback Volume",
4069 HDA_COMPOSE_AMP_VAL(0x1D, 3, 0, HDA_OUTPUT));
4070 if (err < 0)
4071 return err;
4072
4073 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4074 "Headphone Playback Switch",
4075 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
4076 if (err < 0)
4077 return err;
4078
0713efeb 4079 imux = &spec->private_imux[1];
0aa62aef 4080
0713efeb 4081 /* for hp mode select */
10a20af7
TI
4082 for (i = 0; texts[i]; i++)
4083 snd_hda_add_imux_item(imux, texts[i], i, NULL);
0713efeb
LW
4084
4085 spec->hp_mux = &spec->private_imux[1];
d949cac1
HW
4086 return 0;
4087}
4088
4089/* create playback/capture controls for input pins */
10a20af7 4090static int vt1702_auto_create_analog_input_ctls(struct hda_codec *codec,
d949cac1
HW
4091 const struct auto_pin_cfg *cfg)
4092{
90dd48a1 4093 static const hda_nid_t pin_idxs[] = { 0x14, 0x15, 0x18, 0xff };
10a20af7 4094 return vt_auto_create_analog_input_ctls(codec, cfg, 0x1a, pin_idxs,
f3268512 4095 ARRAY_SIZE(pin_idxs));
d949cac1
HW
4096}
4097
4098static int vt1702_parse_auto_config(struct hda_codec *codec)
4099{
4100 struct via_spec *spec = codec->spec;
4101 int err;
d949cac1 4102
9da29271 4103 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
d949cac1
HW
4104 if (err < 0)
4105 return err;
4106 err = vt1702_auto_fill_dac_nids(spec, &spec->autocfg);
4107 if (err < 0)
4108 return err;
4109 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
4110 return 0; /* can't find valid BIOS pin config */
4111
4112 err = vt1702_auto_create_line_out_ctls(spec, &spec->autocfg);
4113 if (err < 0)
4114 return err;
4115 err = vt1702_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
4116 if (err < 0)
4117 return err;
c2c02ea3
LW
4118 /* limit AA path volume to 0 dB */
4119 snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
4120 (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
4121 (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
4122 (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
4123 (1 << AC_AMPCAP_MUTE_SHIFT));
10a20af7 4124 err = vt1702_auto_create_analog_input_ctls(codec, &spec->autocfg);
d949cac1
HW
4125 if (err < 0)
4126 return err;
4127
4128 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4129
9da29271 4130 fill_dig_outs(codec);
98aa34c0 4131
603c4019
TI
4132 if (spec->kctls.list)
4133 spec->mixers[spec->num_mixers++] = spec->kctls.list;
d949cac1 4134
0aa62aef
HW
4135 spec->input_mux = &spec->private_imux[0];
4136
f8fdd495 4137 if (spec->hp_mux)
3d83e577 4138 via_hp_build(codec);
d949cac1
HW
4139
4140 return 1;
4141}
4142
4143#ifdef CONFIG_SND_HDA_POWER_SAVE
90dd48a1 4144static const struct hda_amp_list vt1702_loopbacks[] = {
d949cac1
HW
4145 { 0x1A, HDA_INPUT, 1 },
4146 { 0x1A, HDA_INPUT, 2 },
4147 { 0x1A, HDA_INPUT, 3 },
4148 { 0x1A, HDA_INPUT, 4 },
4149 { } /* end */
4150};
4151#endif
4152
3e95b9ab
LW
4153static void set_widgets_power_state_vt1702(struct hda_codec *codec)
4154{
4155 int imux_is_smixer =
4156 snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
4157 unsigned int parm;
4158 /* inputs */
4159 /* PW 1/2/5 (14h/15h/18h) */
4160 parm = AC_PWRST_D3;
4161 set_pin_power_state(codec, 0x14, &parm);
4162 set_pin_power_state(codec, 0x15, &parm);
4163 set_pin_power_state(codec, 0x18, &parm);
4164 if (imux_is_smixer)
4165 parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
4166 /* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
4167 snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
4168 snd_hda_codec_write(codec, 0x12, 0, AC_VERB_SET_POWER_STATE, parm);
4169 snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
4170 snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_POWER_STATE, parm);
4171
4172 /* outputs */
4173 /* PW 3/4 (16h/17h) */
4174 parm = AC_PWRST_D3;
4175 set_pin_power_state(codec, 0x17, &parm);
4176 set_pin_power_state(codec, 0x16, &parm);
4177 /* MW0 (1ah), AOW 0/1 (10h/1dh) */
4178 snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
4179 imux_is_smixer ? AC_PWRST_D0 : parm);
4180 snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
4181 snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
4182}
4183
d949cac1
HW
4184static int patch_vt1702(struct hda_codec *codec)
4185{
4186 struct via_spec *spec;
4187 int err;
d949cac1
HW
4188
4189 /* create a codec specific record */
5b0cb1d8 4190 spec = via_new_spec(codec);
d949cac1
HW
4191 if (spec == NULL)
4192 return -ENOMEM;
4193
d949cac1
HW
4194 /* automatic parse from the BIOS config */
4195 err = vt1702_parse_auto_config(codec);
4196 if (err < 0) {
4197 via_free(codec);
4198 return err;
4199 } else if (!err) {
4200 printk(KERN_INFO "hda_codec: Cannot set up configuration "
4201 "from BIOS. Using genenic mode...\n");
4202 }
4203
69e52a80
HW
4204 spec->init_verbs[spec->num_iverbs++] = vt1702_volume_init_verbs;
4205 spec->init_verbs[spec->num_iverbs++] = vt1702_uniwill_init_verbs;
d949cac1
HW
4206
4207 spec->stream_name_analog = "VT1702 Analog";
4208 spec->stream_analog_playback = &vt1702_pcm_analog_playback;
4209 spec->stream_analog_capture = &vt1702_pcm_analog_capture;
4210
4211 spec->stream_name_digital = "VT1702 Digital";
4212 spec->stream_digital_playback = &vt1702_pcm_digital_playback;
4213
4214 if (!spec->adc_nids && spec->input_mux) {
4215 spec->adc_nids = vt1702_adc_nids;
4216 spec->num_adc_nids = ARRAY_SIZE(vt1702_adc_nids);
337b9d02 4217 get_mux_nids(codec);
d949cac1
HW
4218 spec->mixers[spec->num_mixers] = vt1702_capture_mixer;
4219 spec->num_mixers++;
4220 }
4221
4222 codec->patch_ops = via_patch_ops;
4223
4224 codec->patch_ops.init = via_auto_init;
69e52a80 4225 codec->patch_ops.unsol_event = via_unsol_event;
d949cac1
HW
4226#ifdef CONFIG_SND_HDA_POWER_SAVE
4227 spec->loopback.amplist = vt1702_loopbacks;
4228#endif
4229
3e95b9ab 4230 spec->set_widgets_power_state = set_widgets_power_state_vt1702;
d949cac1
HW
4231 return 0;
4232}
4233
eb7188ca
LW
4234/* Patch for VT1718S */
4235
4236/* capture mixer elements */
90dd48a1 4237static const struct snd_kcontrol_new vt1718S_capture_mixer[] = {
eb7188ca
LW
4238 HDA_CODEC_VOLUME("Capture Volume", 0x10, 0x0, HDA_INPUT),
4239 HDA_CODEC_MUTE("Capture Switch", 0x10, 0x0, HDA_INPUT),
4240 HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x11, 0x0, HDA_INPUT),
4241 HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x11, 0x0, HDA_INPUT),
4242 HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x2b, 0x0, HDA_INPUT),
4243 HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x29, 0x0,
4244 HDA_INPUT),
4245 {
4246 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4247 /* The multiple "Capture Source" controls confuse alsamixer
4248 * So call somewhat different..
4249 */
4250 .name = "Input Source",
4251 .count = 2,
4252 .info = via_mux_enum_info,
4253 .get = via_mux_enum_get,
4254 .put = via_mux_enum_put,
4255 },
4256 { } /* end */
4257};
4258
90dd48a1 4259static const struct hda_verb vt1718S_volume_init_verbs[] = {
eb7188ca
LW
4260 /*
4261 * Unmute ADC0-1 and set the default input to mic-in
4262 */
4263 {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4264 {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4265
4ab2d53a
LW
4266 /* Enable MW0 adjust Gain 5 */
4267 {0x1, 0xfb2, 0x10},
eb7188ca
LW
4268 /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
4269 * mixer widget
4270 */
4271 /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
4272 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4273 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4274 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
4275 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
4ab2d53a 4276 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(5)},
eb7188ca
LW
4277
4278 /* Setup default input of Front HP to MW9 */
4279 {0x28, AC_VERB_SET_CONNECT_SEL, 0x1},
4280 /* PW9 PW10 Output enable */
4281 {0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
4282 {0x2e, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
4283 /* PW11 Input enable */
4284 {0x2f, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_IN_EN},
4285 /* Enable Boost Volume backdoor */
4286 {0x1, 0xf88, 0x8},
4287 /* MW0/1/2/3/4: un-mute index 0 (AOWx), mute index 1 (MW9) */
4288 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4289 {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4290 {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4291 {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4292 {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4293 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4294 {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4295 {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4296 {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4297 {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4298 /* set MUX1 = 2 (AOW4), MUX2 = 1 (AOW3) */
4299 {0x34, AC_VERB_SET_CONNECT_SEL, 0x2},
4300 {0x35, AC_VERB_SET_CONNECT_SEL, 0x1},
4301 /* Unmute MW4's index 0 */
4302 {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4303 { }
4304};
4305
4306
90dd48a1 4307static const struct hda_verb vt1718S_uniwill_init_verbs[] = {
eb7188ca
LW
4308 {0x28, AC_VERB_SET_UNSOLICITED_ENABLE,
4309 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
4310 {0x24, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4311 {0x25, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4312 {0x26, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4313 {0x27, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4314 {0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4315 {0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4316 {0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4317 { }
4318};
4319
90dd48a1 4320static const struct hda_pcm_stream vt1718S_pcm_analog_playback = {
eb7188ca
LW
4321 .substreams = 2,
4322 .channels_min = 2,
4323 .channels_max = 10,
4324 .nid = 0x8, /* NID to query formats and rates */
4325 .ops = {
4326 .open = via_playback_pcm_open,
4327 .prepare = via_playback_multi_pcm_prepare,
4328 .cleanup = via_playback_multi_pcm_cleanup,
4329 .close = via_pcm_open_close,
4330 },
4331};
4332
90dd48a1 4333static const struct hda_pcm_stream vt1718S_pcm_analog_capture = {
eb7188ca
LW
4334 .substreams = 2,
4335 .channels_min = 2,
4336 .channels_max = 2,
4337 .nid = 0x10, /* NID to query formats and rates */
4338 .ops = {
4339 .open = via_pcm_open_close,
4340 .prepare = via_capture_pcm_prepare,
4341 .cleanup = via_capture_pcm_cleanup,
4342 .close = via_pcm_open_close,
4343 },
4344};
4345
90dd48a1 4346static const struct hda_pcm_stream vt1718S_pcm_digital_playback = {
eb7188ca
LW
4347 .substreams = 2,
4348 .channels_min = 2,
4349 .channels_max = 2,
eb7188ca
LW
4350 /* NID is set in via_build_pcms */
4351 .ops = {
4352 .open = via_dig_playback_pcm_open,
4353 .close = via_dig_playback_pcm_close,
4354 .prepare = via_dig_playback_pcm_prepare,
4355 .cleanup = via_dig_playback_pcm_cleanup
4356 },
4357};
4358
90dd48a1 4359static const struct hda_pcm_stream vt1718S_pcm_digital_capture = {
eb7188ca
LW
4360 .substreams = 1,
4361 .channels_min = 2,
4362 .channels_max = 2,
4363};
4364
4365/* fill in the dac_nids table from the parsed pin configuration */
4366static int vt1718S_auto_fill_dac_nids(struct via_spec *spec,
4367 const struct auto_pin_cfg *cfg)
4368{
4369 int i;
4370 hda_nid_t nid;
4371
4372 spec->multiout.num_dacs = cfg->line_outs;
4373
4374 spec->multiout.dac_nids = spec->private_dac_nids;
4375
4376 for (i = 0; i < 4; i++) {
4377 nid = cfg->line_out_pins[i];
4378 if (nid) {
4379 /* config dac list */
4380 switch (i) {
4381 case AUTO_SEQ_FRONT:
dda14410 4382 spec->private_dac_nids[i] = 0x8;
eb7188ca
LW
4383 break;
4384 case AUTO_SEQ_CENLFE:
dda14410 4385 spec->private_dac_nids[i] = 0xa;
eb7188ca
LW
4386 break;
4387 case AUTO_SEQ_SURROUND:
dda14410 4388 spec->private_dac_nids[i] = 0x9;
eb7188ca
LW
4389 break;
4390 case AUTO_SEQ_SIDE:
dda14410 4391 spec->private_dac_nids[i] = 0xb;
eb7188ca
LW
4392 break;
4393 }
4394 }
4395 }
4396
4397 return 0;
4398}
4399
4400/* add playback controls from the parsed DAC table */
4401static int vt1718S_auto_create_multi_out_ctls(struct via_spec *spec,
4402 const struct auto_pin_cfg *cfg)
4403{
4404 char name[32];
ea734963
TI
4405 static const char * const chname[4] = {
4406 "Front", "Surround", "C/LFE", "Side"
4407 };
eb7188ca
LW
4408 hda_nid_t nid_vols[] = {0x8, 0x9, 0xa, 0xb};
4409 hda_nid_t nid_mutes[] = {0x24, 0x25, 0x26, 0x27};
4410 hda_nid_t nid, nid_vol, nid_mute = 0;
4411 int i, err;
4412
4413 for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
4414 nid = cfg->line_out_pins[i];
4415
4416 if (!nid)
4417 continue;
4418 nid_vol = nid_vols[i];
4419 nid_mute = nid_mutes[i];
4420
4421 if (i == AUTO_SEQ_CENLFE) {
4422 /* Center/LFE */
4423 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4424 "Center Playback Volume",
4425 HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
4426 HDA_OUTPUT));
4427 if (err < 0)
4428 return err;
4429 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4430 "LFE Playback Volume",
4431 HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
4432 HDA_OUTPUT));
4433 if (err < 0)
4434 return err;
4435 err = via_add_control(
4436 spec, VIA_CTL_WIDGET_MUTE,
4437 "Center Playback Switch",
4438 HDA_COMPOSE_AMP_VAL(nid_mute, 1, 0,
4439 HDA_OUTPUT));
4440 if (err < 0)
4441 return err;
4442 err = via_add_control(
4443 spec, VIA_CTL_WIDGET_MUTE,
4444 "LFE Playback Switch",
4445 HDA_COMPOSE_AMP_VAL(nid_mute, 2, 0,
4446 HDA_OUTPUT));
4447 if (err < 0)
4448 return err;
4449 } else if (i == AUTO_SEQ_FRONT) {
4450 /* Front */
4451 sprintf(name, "%s Playback Volume", chname[i]);
4452 err = via_add_control(
4453 spec, VIA_CTL_WIDGET_VOL, name,
4454 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
4455 if (err < 0)
4456 return err;
4457 sprintf(name, "%s Playback Switch", chname[i]);
4458 err = via_add_control(
4459 spec, VIA_CTL_WIDGET_MUTE, name,
4460 HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
4461 HDA_OUTPUT));
4462 if (err < 0)
4463 return err;
4464 } else {
4465 sprintf(name, "%s Playback Volume", chname[i]);
4466 err = via_add_control(
4467 spec, VIA_CTL_WIDGET_VOL, name,
4468 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
4469 if (err < 0)
4470 return err;
4471 sprintf(name, "%s Playback Switch", chname[i]);
4472 err = via_add_control(
4473 spec, VIA_CTL_WIDGET_MUTE, name,
4474 HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
4475 HDA_OUTPUT));
4476 if (err < 0)
4477 return err;
4478 }
4479 }
4480 return 0;
4481}
4482
4483static int vt1718S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
4484{
4485 int err;
4486
4487 if (!pin)
4488 return 0;
4489
4490 spec->multiout.hp_nid = 0xc; /* AOW4 */
4491 spec->hp_independent_mode_index = 1;
4492
4493 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
4494 "Headphone Playback Volume",
4495 HDA_COMPOSE_AMP_VAL(0xc, 3, 0, HDA_OUTPUT));
4496 if (err < 0)
4497 return err;
4498
4499 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
4500 "Headphone Playback Switch",
4501 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
4502 if (err < 0)
4503 return err;
4504
4505 create_hp_imux(spec);
4506 return 0;
4507}
4508
4509/* create playback/capture controls for input pins */
10a20af7 4510static int vt1718S_auto_create_analog_input_ctls(struct hda_codec *codec,
eb7188ca
LW
4511 const struct auto_pin_cfg *cfg)
4512{
90dd48a1 4513 static const hda_nid_t pin_idxs[] = { 0x2c, 0x2b, 0x2a, 0x29, 0, 0xff };
10a20af7 4514 return vt_auto_create_analog_input_ctls(codec, cfg, 0x21, pin_idxs,
f3268512 4515 ARRAY_SIZE(pin_idxs));
eb7188ca
LW
4516}
4517
4518static int vt1718S_parse_auto_config(struct hda_codec *codec)
4519{
4520 struct via_spec *spec = codec->spec;
4521 int err;
4522
4523 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4524
4525 if (err < 0)
4526 return err;
4527 err = vt1718S_auto_fill_dac_nids(spec, &spec->autocfg);
4528 if (err < 0)
4529 return err;
4530 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
4531 return 0; /* can't find valid BIOS pin config */
4532
4533 err = vt1718S_auto_create_multi_out_ctls(spec, &spec->autocfg);
4534 if (err < 0)
4535 return err;
4536 err = vt1718S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
4537 if (err < 0)
4538 return err;
10a20af7 4539 err = vt1718S_auto_create_analog_input_ctls(codec, &spec->autocfg);
eb7188ca
LW
4540 if (err < 0)
4541 return err;
4542
4543 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4544
4545 fill_dig_outs(codec);
4546
4547 if (spec->autocfg.dig_in_pin && codec->vendor_id == 0x11060428)
4548 spec->dig_in_nid = 0x13;
4549
4550 if (spec->kctls.list)
4551 spec->mixers[spec->num_mixers++] = spec->kctls.list;
4552
4553 spec->input_mux = &spec->private_imux[0];
4554
4555 if (spec->hp_mux)
3d83e577 4556 via_hp_build(codec);
eb7188ca 4557
5b0cb1d8 4558 via_smart51_build(spec);
eb7188ca
LW
4559
4560 return 1;
4561}
4562
4563#ifdef CONFIG_SND_HDA_POWER_SAVE
90dd48a1 4564static const struct hda_amp_list vt1718S_loopbacks[] = {
eb7188ca
LW
4565 { 0x21, HDA_INPUT, 1 },
4566 { 0x21, HDA_INPUT, 2 },
4567 { 0x21, HDA_INPUT, 3 },
4568 { 0x21, HDA_INPUT, 4 },
4569 { } /* end */
4570};
4571#endif
4572
3e95b9ab
LW
4573static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
4574{
4575 struct via_spec *spec = codec->spec;
4576 int imux_is_smixer;
4577 unsigned int parm;
4578 /* MUX6 (1eh) = stereo mixer */
4579 imux_is_smixer =
4580 snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
4581 /* inputs */
4582 /* PW 5/6/7 (29h/2ah/2bh) */
4583 parm = AC_PWRST_D3;
4584 set_pin_power_state(codec, 0x29, &parm);
4585 set_pin_power_state(codec, 0x2a, &parm);
4586 set_pin_power_state(codec, 0x2b, &parm);
4587 if (imux_is_smixer)
4588 parm = AC_PWRST_D0;
4589 /* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
4590 snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
4591 snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
4592 snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
4593 snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
4594
4595 /* outputs */
4596 /* PW3 (27h), MW2 (1ah), AOW3 (bh) */
4597 parm = AC_PWRST_D3;
4598 set_pin_power_state(codec, 0x27, &parm);
4599 snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE, parm);
4600 snd_hda_codec_write(codec, 0xb, 0, AC_VERB_SET_POWER_STATE, parm);
4601
4602 /* PW2 (26h), AOW2 (ah) */
4603 parm = AC_PWRST_D3;
4604 set_pin_power_state(codec, 0x26, &parm);
4605 if (spec->smart51_enabled)
4606 set_pin_power_state(codec, 0x2b, &parm);
4607 snd_hda_codec_write(codec, 0xa, 0, AC_VERB_SET_POWER_STATE, parm);
4608
4609 /* PW0 (24h), AOW0 (8h) */
4610 parm = AC_PWRST_D3;
4611 set_pin_power_state(codec, 0x24, &parm);
4612 if (!spec->hp_independent_mode) /* check for redirected HP */
4613 set_pin_power_state(codec, 0x28, &parm);
4614 snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
4615 /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
4616 snd_hda_codec_write(codec, 0x21, 0, AC_VERB_SET_POWER_STATE,
4617 imux_is_smixer ? AC_PWRST_D0 : parm);
4618
4619 /* PW1 (25h), AOW1 (9h) */
4620 parm = AC_PWRST_D3;
4621 set_pin_power_state(codec, 0x25, &parm);
4622 if (spec->smart51_enabled)
4623 set_pin_power_state(codec, 0x2a, &parm);
4624 snd_hda_codec_write(codec, 0x9, 0, AC_VERB_SET_POWER_STATE, parm);
4625
4626 if (spec->hp_independent_mode) {
4627 /* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
4628 parm = AC_PWRST_D3;
4629 set_pin_power_state(codec, 0x28, &parm);
4630 snd_hda_codec_write(codec, 0x1b, 0,
4631 AC_VERB_SET_POWER_STATE, parm);
4632 snd_hda_codec_write(codec, 0x34, 0,
4633 AC_VERB_SET_POWER_STATE, parm);
4634 snd_hda_codec_write(codec, 0xc, 0,
4635 AC_VERB_SET_POWER_STATE, parm);
4636 }
4637}
4638
eb7188ca
LW
4639static int patch_vt1718S(struct hda_codec *codec)
4640{
4641 struct via_spec *spec;
4642 int err;
4643
4644 /* create a codec specific record */
5b0cb1d8 4645 spec = via_new_spec(codec);
eb7188ca
LW
4646 if (spec == NULL)
4647 return -ENOMEM;
4648
eb7188ca
LW
4649 /* automatic parse from the BIOS config */
4650 err = vt1718S_parse_auto_config(codec);
4651 if (err < 0) {
4652 via_free(codec);
4653 return err;
4654 } else if (!err) {
4655 printk(KERN_INFO "hda_codec: Cannot set up configuration "
4656 "from BIOS. Using genenic mode...\n");
4657 }
4658
4659 spec->init_verbs[spec->num_iverbs++] = vt1718S_volume_init_verbs;
4660 spec->init_verbs[spec->num_iverbs++] = vt1718S_uniwill_init_verbs;
4661
bb3c6bfc
LW
4662 if (codec->vendor_id == 0x11060441)
4663 spec->stream_name_analog = "VT2020 Analog";
4664 else if (codec->vendor_id == 0x11064441)
4665 spec->stream_name_analog = "VT1828S Analog";
4666 else
4667 spec->stream_name_analog = "VT1718S Analog";
eb7188ca
LW
4668 spec->stream_analog_playback = &vt1718S_pcm_analog_playback;
4669 spec->stream_analog_capture = &vt1718S_pcm_analog_capture;
4670
bb3c6bfc
LW
4671 if (codec->vendor_id == 0x11060441)
4672 spec->stream_name_digital = "VT2020 Digital";
4673 else if (codec->vendor_id == 0x11064441)
4674 spec->stream_name_digital = "VT1828S Digital";
4675 else
4676 spec->stream_name_digital = "VT1718S Digital";
eb7188ca 4677 spec->stream_digital_playback = &vt1718S_pcm_digital_playback;
bb3c6bfc 4678 if (codec->vendor_id == 0x11060428 || codec->vendor_id == 0x11060441)
eb7188ca
LW
4679 spec->stream_digital_capture = &vt1718S_pcm_digital_capture;
4680
4681 if (!spec->adc_nids && spec->input_mux) {
4682 spec->adc_nids = vt1718S_adc_nids;
4683 spec->num_adc_nids = ARRAY_SIZE(vt1718S_adc_nids);
4684 get_mux_nids(codec);
bb3c6bfc
LW
4685 override_mic_boost(codec, 0x2b, 0, 3, 40);
4686 override_mic_boost(codec, 0x29, 0, 3, 40);
eb7188ca
LW
4687 spec->mixers[spec->num_mixers] = vt1718S_capture_mixer;
4688 spec->num_mixers++;
4689 }
4690
4691 codec->patch_ops = via_patch_ops;
4692
4693 codec->patch_ops.init = via_auto_init;
0f48327e 4694 codec->patch_ops.unsol_event = via_unsol_event;
eb7188ca
LW
4695
4696#ifdef CONFIG_SND_HDA_POWER_SAVE
4697 spec->loopback.amplist = vt1718S_loopbacks;
4698#endif
4699
3e95b9ab
LW
4700 spec->set_widgets_power_state = set_widgets_power_state_vt1718S;
4701
eb7188ca
LW
4702 return 0;
4703}
f3db423d
LW
4704
4705/* Patch for VT1716S */
4706
4707static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
4708 struct snd_ctl_elem_info *uinfo)
4709{
4710 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
4711 uinfo->count = 1;
4712 uinfo->value.integer.min = 0;
4713 uinfo->value.integer.max = 1;
4714 return 0;
4715}
4716
4717static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
4718 struct snd_ctl_elem_value *ucontrol)
4719{
4720 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4721 int index = 0;
4722
4723 index = snd_hda_codec_read(codec, 0x26, 0,
4724 AC_VERB_GET_CONNECT_SEL, 0);
4725 if (index != -1)
4726 *ucontrol->value.integer.value = index;
4727
4728 return 0;
4729}
4730
4731static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
4732 struct snd_ctl_elem_value *ucontrol)
4733{
4734 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4735 struct via_spec *spec = codec->spec;
4736 int index = *ucontrol->value.integer.value;
4737
4738 snd_hda_codec_write(codec, 0x26, 0,
4739 AC_VERB_SET_CONNECT_SEL, index);
4740 spec->dmic_enabled = index;
3e95b9ab 4741 set_widgets_power_state(codec);
f3db423d
LW
4742 return 1;
4743}
4744
4745/* capture mixer elements */
90dd48a1 4746static const struct snd_kcontrol_new vt1716S_capture_mixer[] = {
f3db423d
LW
4747 HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
4748 HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
4749 HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
4750 HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
4751 HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x1A, 0x0, HDA_INPUT),
4752 HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x1E, 0x0,
4753 HDA_INPUT),
4754 {
4755 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4756 .name = "Input Source",
4757 .count = 1,
4758 .info = via_mux_enum_info,
4759 .get = via_mux_enum_get,
4760 .put = via_mux_enum_put,
4761 },
4762 { } /* end */
4763};
4764
90dd48a1 4765static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
f3db423d
LW
4766 HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
4767 {
4768 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4769 .name = "Digital Mic Capture Switch",
5b0cb1d8 4770 .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
f3db423d
LW
4771 .count = 1,
4772 .info = vt1716s_dmic_info,
4773 .get = vt1716s_dmic_get,
4774 .put = vt1716s_dmic_put,
4775 },
4776 {} /* end */
4777};
4778
4779
4780/* mono-out mixer elements */
90dd48a1 4781static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
f3db423d
LW
4782 HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
4783 { } /* end */
4784};
4785
90dd48a1 4786static const struct hda_verb vt1716S_volume_init_verbs[] = {
f3db423d
LW
4787 /*
4788 * Unmute ADC0-1 and set the default input to mic-in
4789 */
4790 {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4791 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4792
4793
4794 /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
4795 * mixer widget
4796 */
4797 /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
4798 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4799 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4800 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
4801 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
4802 {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
4803
4804 /* MUX Indices: Stereo Mixer = 5 */
4805 {0x17, AC_VERB_SET_CONNECT_SEL, 0x5},
4806
4807 /* Setup default input of PW4 to MW0 */
4808 {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
4809
4810 /* Setup default input of SW1 as MW0 */
4811 {0x18, AC_VERB_SET_CONNECT_SEL, 0x1},
4812
4813 /* Setup default input of SW4 as AOW0 */
4814 {0x28, AC_VERB_SET_CONNECT_SEL, 0x1},
4815
4816 /* PW9 PW10 Output enable */
4817 {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
4818 {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
4819
4820 /* Unmute SW1, PW12 */
4821 {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4822 {0x2a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4823 /* PW12 Output enable */
4824 {0x2a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
4825 /* Enable Boost Volume backdoor */
4826 {0x1, 0xf8a, 0x80},
4827 /* don't bybass mixer */
4828 {0x1, 0xf88, 0xc0},
4829 /* Enable mono output */
4830 {0x1, 0xf90, 0x08},
4831 { }
4832};
4833
4834
90dd48a1 4835static const struct hda_verb vt1716S_uniwill_init_verbs[] = {
f3db423d
LW
4836 {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
4837 AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
4838 {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4839 {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4840 {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4841 {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE,
4842 AC_USRSP_EN | VIA_MONO_EVENT | VIA_JACK_EVENT},
4843 {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4844 {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
4845 { }
4846};
4847
90dd48a1 4848static const struct hda_pcm_stream vt1716S_pcm_analog_playback = {
f3db423d
LW
4849 .substreams = 2,
4850 .channels_min = 2,
4851 .channels_max = 6,
4852 .nid = 0x10, /* NID to query formats and rates */
4853 .ops = {
4854 .open = via_playback_pcm_open,
4855 .prepare = via_playback_multi_pcm_prepare,
4856 .cleanup = via_playback_multi_pcm_cleanup,
4857 .close = via_pcm_open_close,
4858 },
4859};
4860
90dd48a1 4861static const struct hda_pcm_stream vt1716S_pcm_analog_capture = {
f3db423d
LW
4862 .substreams = 2,
4863 .channels_min = 2,
4864 .channels_max = 2,
4865 .nid = 0x13, /* NID to query formats and rates */
4866 .ops = {
4867 .open = via_pcm_open_close,
4868 .prepare = via_capture_pcm_prepare,
4869 .cleanup = via_capture_pcm_cleanup,
4870 .close = via_pcm_open_close,
4871 },
4872};
4873
90dd48a1 4874static const struct hda_pcm_stream vt1716S_pcm_digital_playback = {
f3db423d
LW
4875 .substreams = 2,
4876 .channels_min = 2,
4877 .channels_max = 2,
f3db423d
LW
4878 /* NID is set in via_build_pcms */
4879 .ops = {
4880 .open = via_dig_playback_pcm_open,
4881 .close = via_dig_playback_pcm_close,
4882 .prepare = via_dig_playback_pcm_prepare,
4883 .cleanup = via_dig_playback_pcm_cleanup
4884 },
4885};
4886
4887/* fill in the dac_nids table from the parsed pin configuration */
4888static int vt1716S_auto_fill_dac_nids(struct via_spec *spec,
4889 const struct auto_pin_cfg *cfg)
4890{ int i;
4891 hda_nid_t nid;
4892
4893 spec->multiout.num_dacs = cfg->line_outs;
4894
4895 spec->multiout.dac_nids = spec->private_dac_nids;
4896
4897 for (i = 0; i < 3; i++) {
4898 nid = cfg->line_out_pins[i];
4899 if (nid) {
4900 /* config dac list */
4901 switch (i) {
4902 case AUTO_SEQ_FRONT:
dda14410 4903 spec->private_dac_nids[i] = 0x10;
f3db423d
LW
4904 break;
4905 case AUTO_SEQ_CENLFE:
dda14410 4906 spec->private_dac_nids[i] = 0x25;
f3db423d
LW
4907 break;
4908 case AUTO_SEQ_SURROUND:
dda14410 4909 spec->private_dac_nids[i] = 0x11;
f3db423d
LW
4910 break;
4911 }
4912 }
4913 }
4914
4915 return 0;
4916}
4917
4918/* add playback controls from the parsed DAC table */
4919static int vt1716S_auto_create_multi_out_ctls(struct via_spec *spec,
4920 const struct auto_pin_cfg *cfg)
4921{
4922 char name[32];
ea734963
TI
4923 static const char * const chname[3] = {
4924 "Front", "Surround", "C/LFE"
4925 };
f3db423d
LW
4926 hda_nid_t nid_vols[] = {0x10, 0x11, 0x25};
4927 hda_nid_t nid_mutes[] = {0x1C, 0x18, 0x27};
4928 hda_nid_t nid, nid_vol, nid_mute;
4929 int i, err;
4930
4931 for (i = 0; i <= AUTO_SEQ_CENLFE; i++) {
4932 nid = cfg->line_out_pins[i];
4933
4934 if (!nid)
4935 continue;
4936
4937 nid_vol = nid_vols[i];
4938 nid_mute = nid_mutes[i];
4939
4940 if (i == AUTO_SEQ_CENLFE) {
4941 err = via_add_control(
4942 spec, VIA_CTL_WIDGET_VOL,
4943 "Center Playback Volume",
4944 HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0, HDA_OUTPUT));
4945 if (err < 0)
4946 return err;
4947 err = via_add_control(
4948 spec, VIA_CTL_WIDGET_VOL,
4949 "LFE Playback Volume",
4950 HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0, HDA_OUTPUT));
4951 if (err < 0)
4952 return err;
4953 err = via_add_control(
4954 spec, VIA_CTL_WIDGET_MUTE,
4955 "Center Playback Switch",
4956 HDA_COMPOSE_AMP_VAL(nid_mute, 1, 0,
4957 HDA_OUTPUT));
4958 if (err < 0)
4959 return err;
4960 err = via_add_control(
4961 spec, VIA_CTL_WIDGET_MUTE,
4962 "LFE Playback Switch",
4963 HDA_COMPOSE_AMP_VAL(nid_mute, 2, 0,
4964 HDA_OUTPUT));
4965 if (err < 0)
4966 return err;
4967 } else if (i == AUTO_SEQ_FRONT) {
4968
4969 err = via_add_control(
4970 spec, VIA_CTL_WIDGET_VOL,
4971 "Master Front Playback Volume",
4972 HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_INPUT));
4973 if (err < 0)
4974 return err;
4975 err = via_add_control(
4976 spec, VIA_CTL_WIDGET_MUTE,
4977 "Master Front Playback Switch",
4978 HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_INPUT));
4979 if (err < 0)
4980 return err;
4981
4982 sprintf(name, "%s Playback Volume", chname[i]);
4983 err = via_add_control(
4984 spec, VIA_CTL_WIDGET_VOL, name,
4985 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
4986 if (err < 0)
4987 return err;
4988 sprintf(name, "%s Playback Switch", chname[i]);
4989 err = via_add_control(
4990 spec, VIA_CTL_WIDGET_MUTE, name,
4991 HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
4992 HDA_OUTPUT));
4993 if (err < 0)
4994 return err;
4995 } else {
4996 sprintf(name, "%s Playback Volume", chname[i]);
4997 err = via_add_control(
4998 spec, VIA_CTL_WIDGET_VOL, name,
4999 HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
5000 if (err < 0)
5001 return err;
5002 sprintf(name, "%s Playback Switch", chname[i]);
5003 err = via_add_control(
5004 spec, VIA_CTL_WIDGET_MUTE, name,
5005 HDA_COMPOSE_AMP_VAL(nid_mute, 3, 0,
5006 HDA_OUTPUT));
5007 if (err < 0)
5008 return err;
5009 }
5010 }
5011 return 0;
5012}
5013
5014static int vt1716S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
5015{
5016 int err;
5017
5018 if (!pin)
5019 return 0;
5020
5021 spec->multiout.hp_nid = 0x25; /* AOW3 */
5022 spec->hp_independent_mode_index = 1;
5023
5024 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5025 "Headphone Playback Volume",
5026 HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
5027 if (err < 0)
5028 return err;
5029
5030 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
5031 "Headphone Playback Switch",
5032 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
5033 if (err < 0)
5034 return err;
5035
5036 create_hp_imux(spec);
5037 return 0;
5038}
5039
5040/* create playback/capture controls for input pins */
10a20af7 5041static int vt1716S_auto_create_analog_input_ctls(struct hda_codec *codec,
f3db423d
LW
5042 const struct auto_pin_cfg *cfg)
5043{
90dd48a1 5044 static const hda_nid_t pin_idxs[] = { 0x1f, 0x1a, 0x1b, 0x1e, 0, 0xff };
10a20af7 5045 return vt_auto_create_analog_input_ctls(codec, cfg, 0x16, pin_idxs,
f3268512 5046 ARRAY_SIZE(pin_idxs));
f3db423d
LW
5047}
5048
5049static int vt1716S_parse_auto_config(struct hda_codec *codec)
5050{
5051 struct via_spec *spec = codec->spec;
5052 int err;
5053
5054 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
5055 if (err < 0)
5056 return err;
5057 err = vt1716S_auto_fill_dac_nids(spec, &spec->autocfg);
5058 if (err < 0)
5059 return err;
5060 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
5061 return 0; /* can't find valid BIOS pin config */
5062
5063 err = vt1716S_auto_create_multi_out_ctls(spec, &spec->autocfg);
5064 if (err < 0)
5065 return err;
5066 err = vt1716S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
5067 if (err < 0)
5068 return err;
10a20af7 5069 err = vt1716S_auto_create_analog_input_ctls(codec, &spec->autocfg);
f3db423d
LW
5070 if (err < 0)
5071 return err;
5072
5073 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
5074
5075 fill_dig_outs(codec);
5076
5077 if (spec->kctls.list)
5078 spec->mixers[spec->num_mixers++] = spec->kctls.list;
5079
5080 spec->input_mux = &spec->private_imux[0];
5081
5082 if (spec->hp_mux)
3d83e577 5083 via_hp_build(codec);
f3db423d 5084
5b0cb1d8 5085 via_smart51_build(spec);
f3db423d
LW
5086
5087 return 1;
5088}
5089
5090#ifdef CONFIG_SND_HDA_POWER_SAVE
90dd48a1 5091static const struct hda_amp_list vt1716S_loopbacks[] = {
f3db423d
LW
5092 { 0x16, HDA_INPUT, 1 },
5093 { 0x16, HDA_INPUT, 2 },
5094 { 0x16, HDA_INPUT, 3 },
5095 { 0x16, HDA_INPUT, 4 },
5096 { } /* end */
5097};
5098#endif
5099
3e95b9ab
LW
5100static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
5101{
5102 struct via_spec *spec = codec->spec;
5103 int imux_is_smixer;
5104 unsigned int parm;
5105 unsigned int mono_out, present;
5106 /* SW0 (17h) = stereo mixer */
5107 imux_is_smixer =
5108 (snd_hda_codec_read(codec, 0x17, 0,
5109 AC_VERB_GET_CONNECT_SEL, 0x00) == 5);
5110 /* inputs */
5111 /* PW 1/2/5 (1ah/1bh/1eh) */
5112 parm = AC_PWRST_D3;
5113 set_pin_power_state(codec, 0x1a, &parm);
5114 set_pin_power_state(codec, 0x1b, &parm);
5115 set_pin_power_state(codec, 0x1e, &parm);
5116 if (imux_is_smixer)
5117 parm = AC_PWRST_D0;
5118 /* SW0 (17h), AIW0(13h) */
5119 snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
5120 snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
5121
5122 parm = AC_PWRST_D3;
5123 set_pin_power_state(codec, 0x1e, &parm);
5124 /* PW11 (22h) */
5125 if (spec->dmic_enabled)
5126 set_pin_power_state(codec, 0x22, &parm);
5127 else
5128 snd_hda_codec_write(codec, 0x22, 0,
5129 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
5130
5131 /* SW2(26h), AIW1(14h) */
5132 snd_hda_codec_write(codec, 0x26, 0, AC_VERB_SET_POWER_STATE, parm);
5133 snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
5134
5135 /* outputs */
5136 /* PW0 (19h), SW1 (18h), AOW1 (11h) */
5137 parm = AC_PWRST_D3;
5138 set_pin_power_state(codec, 0x19, &parm);
5139 /* Smart 5.1 PW2(1bh) */
5140 if (spec->smart51_enabled)
5141 set_pin_power_state(codec, 0x1b, &parm);
5142 snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
5143 snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
5144
5145 /* PW7 (23h), SW3 (27h), AOW3 (25h) */
5146 parm = AC_PWRST_D3;
5147 set_pin_power_state(codec, 0x23, &parm);
5148 /* Smart 5.1 PW1(1ah) */
5149 if (spec->smart51_enabled)
5150 set_pin_power_state(codec, 0x1a, &parm);
5151 snd_hda_codec_write(codec, 0x27, 0, AC_VERB_SET_POWER_STATE, parm);
5152
5153 /* Smart 5.1 PW5(1eh) */
5154 if (spec->smart51_enabled)
5155 set_pin_power_state(codec, 0x1e, &parm);
5156 snd_hda_codec_write(codec, 0x25, 0, AC_VERB_SET_POWER_STATE, parm);
5157
5158 /* Mono out */
5159 /* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
5160 present = snd_hda_jack_detect(codec, 0x1c);
5161
5162 if (present)
5163 mono_out = 0;
5164 else {
5165 present = snd_hda_jack_detect(codec, 0x1d);
5166 if (!spec->hp_independent_mode && present)
5167 mono_out = 0;
5168 else
5169 mono_out = 1;
5170 }
5171 parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
5172 snd_hda_codec_write(codec, 0x28, 0, AC_VERB_SET_POWER_STATE, parm);
5173 snd_hda_codec_write(codec, 0x29, 0, AC_VERB_SET_POWER_STATE, parm);
5174 snd_hda_codec_write(codec, 0x2a, 0, AC_VERB_SET_POWER_STATE, parm);
5175
5176 /* PW 3/4 (1ch/1dh) */
5177 parm = AC_PWRST_D3;
5178 set_pin_power_state(codec, 0x1c, &parm);
5179 set_pin_power_state(codec, 0x1d, &parm);
5180 /* HP Independent Mode, power on AOW3 */
5181 if (spec->hp_independent_mode)
5182 snd_hda_codec_write(codec, 0x25, 0,
5183 AC_VERB_SET_POWER_STATE, parm);
5184
5185 /* force to D0 for internal Speaker */
5186 /* MW0 (16h), AOW0 (10h) */
5187 snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
5188 imux_is_smixer ? AC_PWRST_D0 : parm);
5189 snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
5190 mono_out ? AC_PWRST_D0 : parm);
5191}
5192
f3db423d
LW
5193static int patch_vt1716S(struct hda_codec *codec)
5194{
5195 struct via_spec *spec;
5196 int err;
5197
5198 /* create a codec specific record */
5b0cb1d8 5199 spec = via_new_spec(codec);
f3db423d
LW
5200 if (spec == NULL)
5201 return -ENOMEM;
5202
f3db423d
LW
5203 /* automatic parse from the BIOS config */
5204 err = vt1716S_parse_auto_config(codec);
5205 if (err < 0) {
5206 via_free(codec);
5207 return err;
5208 } else if (!err) {
5209 printk(KERN_INFO "hda_codec: Cannot set up configuration "
5210 "from BIOS. Using genenic mode...\n");
5211 }
5212
5213 spec->init_verbs[spec->num_iverbs++] = vt1716S_volume_init_verbs;
5214 spec->init_verbs[spec->num_iverbs++] = vt1716S_uniwill_init_verbs;
5215
5216 spec->stream_name_analog = "VT1716S Analog";
5217 spec->stream_analog_playback = &vt1716S_pcm_analog_playback;
5218 spec->stream_analog_capture = &vt1716S_pcm_analog_capture;
5219
5220 spec->stream_name_digital = "VT1716S Digital";
5221 spec->stream_digital_playback = &vt1716S_pcm_digital_playback;
5222
5223 if (!spec->adc_nids && spec->input_mux) {
5224 spec->adc_nids = vt1716S_adc_nids;
5225 spec->num_adc_nids = ARRAY_SIZE(vt1716S_adc_nids);
5226 get_mux_nids(codec);
5227 override_mic_boost(codec, 0x1a, 0, 3, 40);
5228 override_mic_boost(codec, 0x1e, 0, 3, 40);
5229 spec->mixers[spec->num_mixers] = vt1716S_capture_mixer;
5230 spec->num_mixers++;
5231 }
5232
5233 spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
5234 spec->num_mixers++;
5235
5236 spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
5237
5238 codec->patch_ops = via_patch_ops;
5239
5240 codec->patch_ops.init = via_auto_init;
0f48327e 5241 codec->patch_ops.unsol_event = via_unsol_event;
f3db423d
LW
5242
5243#ifdef CONFIG_SND_HDA_POWER_SAVE
5244 spec->loopback.amplist = vt1716S_loopbacks;
5245#endif
5246
3e95b9ab 5247 spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
f3db423d
LW
5248 return 0;
5249}
25eaba2f
LW
5250
5251/* for vt2002P */
5252
5253/* capture mixer elements */
90dd48a1 5254static const struct snd_kcontrol_new vt2002P_capture_mixer[] = {
25eaba2f
LW
5255 HDA_CODEC_VOLUME("Capture Volume", 0x10, 0x0, HDA_INPUT),
5256 HDA_CODEC_MUTE("Capture Switch", 0x10, 0x0, HDA_INPUT),
5257 HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x11, 0x0, HDA_INPUT),
5258 HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x11, 0x0, HDA_INPUT),
5259 HDA_CODEC_VOLUME("Mic Boost Capture Volume", 0x2b, 0x0, HDA_INPUT),
5260 HDA_CODEC_VOLUME("Front Mic Boost Capture Volume", 0x29, 0x0,
5261 HDA_INPUT),
5262 {
5263 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5264 /* The multiple "Capture Source" controls confuse alsamixer
5265 * So call somewhat different..
5266 */
5267 /* .name = "Capture Source", */
5268 .name = "Input Source",
5269 .count = 2,
5270 .info = via_mux_enum_info,
5271 .get = via_mux_enum_get,
5272 .put = via_mux_enum_put,
5273 },
5274 { } /* end */
5275};
5276
90dd48a1 5277static const struct hda_verb vt2002P_volume_init_verbs[] = {
eadb9a80
LW
5278 /* Class-D speaker related verbs */
5279 {0x1, 0xfe0, 0x4},
5280 {0x1, 0xfe9, 0x80},
5281 {0x1, 0xfe2, 0x22},
25eaba2f
LW
5282 /*
5283 * Unmute ADC0-1 and set the default input to mic-in
5284 */
5285 {0x8, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5286 {0x9, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5287
5288
5289 /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
5290 * mixer widget
5291 */
5292 /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
5293 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
5294 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5295 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
5296 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
5297 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
5298
5299 /* MUX Indices: Mic = 0 */
5300 {0x1e, AC_VERB_SET_CONNECT_SEL, 0},
5301 {0x1f, AC_VERB_SET_CONNECT_SEL, 0},
5302
5303 /* PW9 Output enable */
5304 {0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
5305
5306 /* Enable Boost Volume backdoor */
5307 {0x1, 0xfb9, 0x24},
5308
5309 /* MW0/1/4/8: un-mute index 0 (MUXx), un-mute index 1 (MW9) */
5310 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5311 {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5312 {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5313 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5314 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5315 {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5316 {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5317 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5318
5319 /* set MUX0/1/4/8 = 0 (AOW0) */
5320 {0x34, AC_VERB_SET_CONNECT_SEL, 0},
5321 {0x35, AC_VERB_SET_CONNECT_SEL, 0},
5322 {0x37, AC_VERB_SET_CONNECT_SEL, 0},
5323 {0x3b, AC_VERB_SET_CONNECT_SEL, 0},
5324
5325 /* set PW0 index=0 (MW0) */
5326 {0x24, AC_VERB_SET_CONNECT_SEL, 0},
5327
5328 /* Enable AOW0 to MW9 */
5329 {0x1, 0xfb8, 0x88},
5330 { }
5331};
90dd48a1 5332static const struct hda_verb vt1802_volume_init_verbs[] = {
11890956
LW
5333 /*
5334 * Unmute ADC0-1 and set the default input to mic-in
5335 */
5336 {0x8, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5337 {0x9, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5338
5339
5340 /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
5341 * mixer widget
5342 */
5343 /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
5344 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
5345 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5346 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
5347 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
5348 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
5349
5350 /* MUX Indices: Mic = 0 */
5351 {0x1e, AC_VERB_SET_CONNECT_SEL, 0},
5352 {0x1f, AC_VERB_SET_CONNECT_SEL, 0},
5353
5354 /* PW9 Output enable */
5355 {0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
5356
5357 /* Enable Boost Volume backdoor */
5358 {0x1, 0xfb9, 0x24},
5359
5360 /* MW0/1/4/8: un-mute index 0 (MUXx), un-mute index 1 (MW9) */
5361 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5362 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5363 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5364 {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5365 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5366 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5367 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5368 {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5369
5370 /* set MUX0/1/4/8 = 0 (AOW0) */
5371 {0x34, AC_VERB_SET_CONNECT_SEL, 0},
5372 {0x35, AC_VERB_SET_CONNECT_SEL, 0},
5373 {0x38, AC_VERB_SET_CONNECT_SEL, 0},
5374 {0x3c, AC_VERB_SET_CONNECT_SEL, 0},
5375
5376 /* set PW0 index=0 (MW0) */
5377 {0x24, AC_VERB_SET_CONNECT_SEL, 0},
5378
5379 /* Enable AOW0 to MW9 */
5380 {0x1, 0xfb8, 0x88},
5381 { }
5382};
25eaba2f
LW
5383
5384
90dd48a1 5385static const struct hda_verb vt2002P_uniwill_init_verbs[] = {
25eaba2f
LW
5386 {0x25, AC_VERB_SET_UNSOLICITED_ENABLE,
5387 AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5388 {0x26, AC_VERB_SET_UNSOLICITED_ENABLE,
5389 AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5390 {0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5391 {0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5392 {0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5393 { }
5394};
90dd48a1 5395static const struct hda_verb vt1802_uniwill_init_verbs[] = {
11890956
LW
5396 {0x25, AC_VERB_SET_UNSOLICITED_ENABLE,
5397 AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5398 {0x28, AC_VERB_SET_UNSOLICITED_ENABLE,
5399 AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5400 {0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5401 {0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5402 {0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5403 { }
5404};
25eaba2f 5405
90dd48a1 5406static const struct hda_pcm_stream vt2002P_pcm_analog_playback = {
25eaba2f
LW
5407 .substreams = 2,
5408 .channels_min = 2,
5409 .channels_max = 2,
5410 .nid = 0x8, /* NID to query formats and rates */
5411 .ops = {
5412 .open = via_playback_pcm_open,
5413 .prepare = via_playback_multi_pcm_prepare,
5414 .cleanup = via_playback_multi_pcm_cleanup,
5415 .close = via_pcm_open_close,
5416 },
5417};
5418
90dd48a1 5419static const struct hda_pcm_stream vt2002P_pcm_analog_capture = {
25eaba2f
LW
5420 .substreams = 2,
5421 .channels_min = 2,
5422 .channels_max = 2,
5423 .nid = 0x10, /* NID to query formats and rates */
5424 .ops = {
5425 .open = via_pcm_open_close,
5426 .prepare = via_capture_pcm_prepare,
5427 .cleanup = via_capture_pcm_cleanup,
5428 .close = via_pcm_open_close,
5429 },
5430};
5431
90dd48a1 5432static const struct hda_pcm_stream vt2002P_pcm_digital_playback = {
25eaba2f
LW
5433 .substreams = 1,
5434 .channels_min = 2,
5435 .channels_max = 2,
25eaba2f
LW
5436 /* NID is set in via_build_pcms */
5437 .ops = {
5438 .open = via_dig_playback_pcm_open,
5439 .close = via_dig_playback_pcm_close,
5440 .prepare = via_dig_playback_pcm_prepare,
5441 .cleanup = via_dig_playback_pcm_cleanup
5442 },
5443};
5444
5445/* fill in the dac_nids table from the parsed pin configuration */
5446static int vt2002P_auto_fill_dac_nids(struct via_spec *spec,
5447 const struct auto_pin_cfg *cfg)
5448{
5449 spec->multiout.num_dacs = 1;
5450 spec->multiout.dac_nids = spec->private_dac_nids;
5451 if (cfg->line_out_pins[0])
dda14410 5452 spec->private_dac_nids[0] = 0x8;
25eaba2f
LW
5453 return 0;
5454}
5455
5456/* add playback controls from the parsed DAC table */
5457static int vt2002P_auto_create_multi_out_ctls(struct via_spec *spec,
5458 const struct auto_pin_cfg *cfg)
5459{
5460 int err;
11890956 5461 hda_nid_t sw_nid;
25eaba2f
LW
5462
5463 if (!cfg->line_out_pins[0])
5464 return -1;
5465
11890956
LW
5466 if (spec->codec_type == VT1802)
5467 sw_nid = 0x28;
5468 else
5469 sw_nid = 0x26;
25eaba2f
LW
5470
5471 /* Line-Out: PortE */
5472 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5473 "Master Front Playback Volume",
5474 HDA_COMPOSE_AMP_VAL(0x8, 3, 0, HDA_OUTPUT));
5475 if (err < 0)
5476 return err;
5477 err = via_add_control(spec, VIA_CTL_WIDGET_BIND_PIN_MUTE,
5478 "Master Front Playback Switch",
11890956 5479 HDA_COMPOSE_AMP_VAL(sw_nid, 3, 0, HDA_OUTPUT));
25eaba2f
LW
5480 if (err < 0)
5481 return err;
5482
5483 return 0;
5484}
5485
5486static int vt2002P_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
5487{
5488 int err;
5489
5490 if (!pin)
5491 return 0;
5492
5493 spec->multiout.hp_nid = 0x9;
5494 spec->hp_independent_mode_index = 1;
5495
5496 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5497 "Headphone Playback Volume",
5498 HDA_COMPOSE_AMP_VAL(
5499 spec->multiout.hp_nid, 3, 0, HDA_OUTPUT));
5500 if (err < 0)
5501 return err;
5502
5503 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
5504 "Headphone Playback Switch",
5505 HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
5506 if (err < 0)
5507 return err;
5508
5509 create_hp_imux(spec);
5510 return 0;
5511}
5512
5513/* create playback/capture controls for input pins */
10a20af7 5514static int vt2002P_auto_create_analog_input_ctls(struct hda_codec *codec,
25eaba2f
LW
5515 const struct auto_pin_cfg *cfg)
5516{
10a20af7 5517 struct via_spec *spec = codec->spec;
25eaba2f 5518 struct hda_input_mux *imux = &spec->private_imux[0];
90dd48a1 5519 static const hda_nid_t pin_idxs[] = { 0x2b, 0x2a, 0x29, 0xff };
f3268512 5520 int err;
25eaba2f 5521
10a20af7 5522 err = vt_auto_create_analog_input_ctls(codec, cfg, 0x21, pin_idxs,
f3268512
TI
5523 ARRAY_SIZE(pin_idxs));
5524 if (err < 0)
5525 return err;
25eaba2f 5526 /* build volume/mute control of loopback */
7b315bb4 5527 err = via_new_analog_input(spec, "Stereo Mixer", 0, 3, 0x21);
25eaba2f
LW
5528 if (err < 0)
5529 return err;
5530
25eaba2f 5531 /* for digital mic select */
10a20af7 5532 snd_hda_add_imux_item(imux, "Digital Mic", 4, NULL);
25eaba2f
LW
5533
5534 return 0;
5535}
5536
5537static int vt2002P_parse_auto_config(struct hda_codec *codec)
5538{
5539 struct via_spec *spec = codec->spec;
5540 int err;
5541
5542
5543 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
5544 if (err < 0)
5545 return err;
5546
5547 err = vt2002P_auto_fill_dac_nids(spec, &spec->autocfg);
5548 if (err < 0)
5549 return err;
5550
5551 if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
5552 return 0; /* can't find valid BIOS pin config */
5553
5554 err = vt2002P_auto_create_multi_out_ctls(spec, &spec->autocfg);
5555 if (err < 0)
5556 return err;
5557 err = vt2002P_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
5558 if (err < 0)
5559 return err;
10a20af7 5560 err = vt2002P_auto_create_analog_input_ctls(codec, &spec->autocfg);
25eaba2f
LW
5561 if (err < 0)
5562 return err;
5563
5564 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
5565
5566 fill_dig_outs(codec);
5567
5568 if (spec->kctls.list)
5569 spec->mixers[spec->num_mixers++] = spec->kctls.list;
5570
5571 spec->input_mux = &spec->private_imux[0];
5572
5573 if (spec->hp_mux)
3d83e577 5574 via_hp_build(codec);
25eaba2f
LW
5575
5576 return 1;
5577}
5578
5579#ifdef CONFIG_SND_HDA_POWER_SAVE
90dd48a1 5580static const struct hda_amp_list vt2002P_loopbacks[] = {
25eaba2f
LW
5581 { 0x21, HDA_INPUT, 0 },
5582 { 0x21, HDA_INPUT, 1 },
5583 { 0x21, HDA_INPUT, 2 },
5584 { } /* end */
5585};
5586#endif
5587
3e95b9ab
LW
5588static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
5589{
5590 struct via_spec *spec = codec->spec;
5591 int imux_is_smixer;
5592 unsigned int parm;
5593 unsigned int present;
5594 /* MUX9 (1eh) = stereo mixer */
5595 imux_is_smixer =
5596 snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
5597 /* inputs */
5598 /* PW 5/6/7 (29h/2ah/2bh) */
5599 parm = AC_PWRST_D3;
5600 set_pin_power_state(codec, 0x29, &parm);
5601 set_pin_power_state(codec, 0x2a, &parm);
5602 set_pin_power_state(codec, 0x2b, &parm);
5603 parm = AC_PWRST_D0;
5604 /* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
5605 snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
5606 snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
5607 snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
5608 snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
5609
5610 /* outputs */
5611 /* AOW0 (8h)*/
5612 snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
5613
11890956
LW
5614 if (spec->codec_type == VT1802) {
5615 /* PW4 (28h), MW4 (18h), MUX4(38h) */
5616 parm = AC_PWRST_D3;
5617 set_pin_power_state(codec, 0x28, &parm);
5618 snd_hda_codec_write(codec, 0x18, 0,
5619 AC_VERB_SET_POWER_STATE, parm);
5620 snd_hda_codec_write(codec, 0x38, 0,
5621 AC_VERB_SET_POWER_STATE, parm);
5622 } else {
5623 /* PW4 (26h), MW4 (1ch), MUX4(37h) */
5624 parm = AC_PWRST_D3;
5625 set_pin_power_state(codec, 0x26, &parm);
5626 snd_hda_codec_write(codec, 0x1c, 0,
5627 AC_VERB_SET_POWER_STATE, parm);
5628 snd_hda_codec_write(codec, 0x37, 0,
5629 AC_VERB_SET_POWER_STATE, parm);
5630 }
3e95b9ab 5631
11890956
LW
5632 if (spec->codec_type == VT1802) {
5633 /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
5634 parm = AC_PWRST_D3;
5635 set_pin_power_state(codec, 0x25, &parm);
5636 snd_hda_codec_write(codec, 0x15, 0,
5637 AC_VERB_SET_POWER_STATE, parm);
5638 snd_hda_codec_write(codec, 0x35, 0,
5639 AC_VERB_SET_POWER_STATE, parm);
5640 } else {
5641 /* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
5642 parm = AC_PWRST_D3;
5643 set_pin_power_state(codec, 0x25, &parm);
5644 snd_hda_codec_write(codec, 0x19, 0,
5645 AC_VERB_SET_POWER_STATE, parm);
5646 snd_hda_codec_write(codec, 0x35, 0,
5647 AC_VERB_SET_POWER_STATE, parm);
5648 }
3e95b9ab
LW
5649
5650 if (spec->hp_independent_mode)
5651 snd_hda_codec_write(codec, 0x9, 0,
5652 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
5653
5654 /* Class-D */
5655 /* PW0 (24h), MW0(18h/14h), MUX0(34h) */
5656 present = snd_hda_jack_detect(codec, 0x25);
5657
5658 parm = AC_PWRST_D3;
5659 set_pin_power_state(codec, 0x24, &parm);
5660 parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
11890956
LW
5661 if (spec->codec_type == VT1802)
5662 snd_hda_codec_write(codec, 0x14, 0,
5663 AC_VERB_SET_POWER_STATE, parm);
5664 else
5665 snd_hda_codec_write(codec, 0x18, 0,
5666 AC_VERB_SET_POWER_STATE, parm);
3e95b9ab
LW
5667 snd_hda_codec_write(codec, 0x34, 0, AC_VERB_SET_POWER_STATE, parm);
5668
5669 /* Mono Out */
5670 present = snd_hda_jack_detect(codec, 0x26);
5671
5672 parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
11890956
LW
5673 if (spec->codec_type == VT1802) {
5674 /* PW15 (33h), MW8(1ch), MUX8(3ch) */
5675 snd_hda_codec_write(codec, 0x33, 0,
5676 AC_VERB_SET_POWER_STATE, parm);
5677 snd_hda_codec_write(codec, 0x1c, 0,
5678 AC_VERB_SET_POWER_STATE, parm);
5679 snd_hda_codec_write(codec, 0x3c, 0,
5680 AC_VERB_SET_POWER_STATE, parm);
5681 } else {
5682 /* PW15 (31h), MW8(17h), MUX8(3bh) */
5683 snd_hda_codec_write(codec, 0x31, 0,
5684 AC_VERB_SET_POWER_STATE, parm);
5685 snd_hda_codec_write(codec, 0x17, 0,
5686 AC_VERB_SET_POWER_STATE, parm);
5687 snd_hda_codec_write(codec, 0x3b, 0,
5688 AC_VERB_SET_POWER_STATE, parm);
5689 }
3e95b9ab
LW
5690 /* MW9 (21h) */
5691 if (imux_is_smixer || !is_aa_path_mute(codec))
5692 snd_hda_codec_write(codec, 0x21, 0,
5693 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
5694 else
5695 snd_hda_codec_write(codec, 0x21, 0,
5696 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
5697}
25eaba2f
LW
5698
5699/* patch for vt2002P */
5700static int patch_vt2002P(struct hda_codec *codec)
5701{
5702 struct via_spec *spec;
5703 int err;
5704
5705 /* create a codec specific record */
5b0cb1d8 5706 spec = via_new_spec(codec);
25eaba2f
LW
5707 if (spec == NULL)
5708 return -ENOMEM;
5709
25eaba2f
LW
5710 /* automatic parse from the BIOS config */
5711 err = vt2002P_parse_auto_config(codec);
5712 if (err < 0) {
5713 via_free(codec);
5714 return err;
5715 } else if (!err) {
5716 printk(KERN_INFO "hda_codec: Cannot set up configuration "
5717 "from BIOS. Using genenic mode...\n");
5718 }
5719
11890956
LW
5720 if (spec->codec_type == VT1802)
5721 spec->init_verbs[spec->num_iverbs++] =
5722 vt1802_volume_init_verbs;
5723 else
5724 spec->init_verbs[spec->num_iverbs++] =
5725 vt2002P_volume_init_verbs;
25eaba2f 5726
11890956
LW
5727 if (spec->codec_type == VT1802)
5728 spec->init_verbs[spec->num_iverbs++] =
5729 vt1802_uniwill_init_verbs;
5730 else
5731 spec->init_verbs[spec->num_iverbs++] =
5732 vt2002P_uniwill_init_verbs;
5733
5734 if (spec->codec_type == VT1802)
5735 spec->stream_name_analog = "VT1802 Analog";
5736 else
5737 spec->stream_name_analog = "VT2002P Analog";
25eaba2f
LW
5738 spec->stream_analog_playback = &vt2002P_pcm_analog_playback;
5739 spec->stream_analog_capture = &vt2002P_pcm_analog_capture;
5740
11890956
LW
5741 if (spec->codec_type == VT1802)
5742 spec->stream_name_digital = "VT1802 Digital";
5743 else
5744 spec->stream_name_digital = "VT2002P Digital";
25eaba2f
LW
5745 spec->stream_digital_playback = &vt2002P_pcm_digital_playback;
5746
5747 if (!spec->adc_nids && spec->input_mux) {
5748 spec->adc_nids = vt2002P_adc_nids;
5749 spec->num_adc_nids = ARRAY_SIZE(vt2002P_adc_nids);
5750 get_mux_nids(codec);
5751 override_mic_boost(codec, 0x2b, 0, 3, 40);
5752 override_mic_boost(codec, 0x29, 0, 3, 40);
5753 spec->mixers[spec->num_mixers] = vt2002P_capture_mixer;
5754 spec->num_mixers++;
5755 }
5756
5757 codec->patch_ops = via_patch_ops;
5758
5759 codec->patch_ops.init = via_auto_init;
0f48327e 5760 codec->patch_ops.unsol_event = via_unsol_event;
25eaba2f
LW
5761
5762#ifdef CONFIG_SND_HDA_POWER_SAVE
5763 spec->loopback.amplist = vt2002P_loopbacks;
5764#endif
5765
3e95b9ab 5766 spec->set_widgets_power_state = set_widgets_power_state_vt2002P;
25eaba2f
LW
5767 return 0;
5768}
ab6734e7
LW
5769
5770/* for vt1812 */
5771
5772/* capture mixer elements */
90dd48a1 5773static const struct snd_kcontrol_new vt1812_capture_mixer[] = {
ab6734e7
LW
5774 HDA_CODEC_VOLUME("Capture Volume", 0x10, 0x0, HDA_INPUT),
5775 HDA_CODEC_MUTE("Capture Switch", 0x10, 0x0, HDA_INPUT),
5776 HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x11, 0x0, HDA_INPUT),
5777 HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x11, 0x0, HDA_INPUT),
5778 HDA_CODEC_MUTE("Mic Boost Capture Volume", 0x2b, 0x0, HDA_INPUT),
5779 HDA_CODEC_MUTE("Front Mic Boost Capture Volume", 0x29, 0x0,
5780 HDA_INPUT),
5781 {
5782 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5783 /* The multiple "Capture Source" controls confuse alsamixer
5784 * So call somewhat different..
5785 */
5786 .name = "Input Source",
5787 .count = 2,
5788 .info = via_mux_enum_info,
5789 .get = via_mux_enum_get,
5790 .put = via_mux_enum_put,
5791 },
5792 { } /* end */
5793};
5794
90dd48a1 5795static const struct hda_verb vt1812_volume_init_verbs[] = {
ab6734e7
LW
5796 /*
5797 * Unmute ADC0-1 and set the default input to mic-in
5798 */
5799 {0x8, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5800 {0x9, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5801
5802
5803 /* Mute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
5804 * mixer widget
5805 */
5806 /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
5807 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
5808 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5809 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
5810 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
5811 {0x21, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
5812
5813 /* MUX Indices: Mic = 0 */
5814 {0x1e, AC_VERB_SET_CONNECT_SEL, 0},
5815 {0x1f, AC_VERB_SET_CONNECT_SEL, 0},
5816
5817 /* PW9 Output enable */
5818 {0x2d, AC_VERB_SET_PIN_WIDGET_CONTROL, AC_PINCTL_OUT_EN},
5819
5820 /* Enable Boost Volume backdoor */
5821 {0x1, 0xfb9, 0x24},
5822
5823 /* MW0/1/4/13/15: un-mute index 0 (MUXx), un-mute index 1 (MW9) */
5824 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5825 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5826 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5827 {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5828 {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5829 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5830 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5831 {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5832 {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5833 {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
5834
5835 /* set MUX0/1/4/13/15 = 0 (AOW0) */
5836 {0x34, AC_VERB_SET_CONNECT_SEL, 0},
5837 {0x35, AC_VERB_SET_CONNECT_SEL, 0},
5838 {0x38, AC_VERB_SET_CONNECT_SEL, 0},
5839 {0x3c, AC_VERB_SET_CONNECT_SEL, 0},
5840 {0x3d, AC_VERB_SET_CONNECT_SEL, 0},
5841
5842 /* Enable AOW0 to MW9 */
5843 {0x1, 0xfb8, 0xa8},
5844 { }
5845};
5846
5847
90dd48a1 5848static const struct hda_verb vt1812_uniwill_init_verbs[] = {
ab6734e7
LW
5849 {0x33, AC_VERB_SET_UNSOLICITED_ENABLE,
5850 AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5851 {0x25, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT },
5852 {0x28, AC_VERB_SET_UNSOLICITED_ENABLE,
5853 AC_USRSP_EN | VIA_JACK_EVENT | VIA_BIND_HP_EVENT},
5854 {0x29, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5855 {0x2a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5856 {0x2b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
5857 { }
5858};
5859
90dd48a1 5860static const struct hda_pcm_stream vt1812_pcm_analog_playback = {
ab6734e7
LW
5861 .substreams = 2,
5862 .channels_min = 2,
5863 .channels_max = 2,
5864 .nid = 0x8, /* NID to query formats and rates */
5865 .ops = {
5866 .open = via_playback_pcm_open,
5867 .prepare = via_playback_multi_pcm_prepare,
5868 .cleanup = via_playback_multi_pcm_cleanup,
5869 .close = via_pcm_open_close,
5870 },
5871};
5872
90dd48a1 5873static const struct hda_pcm_stream vt1812_pcm_analog_capture = {
ab6734e7
LW
5874 .substreams = 2,
5875 .channels_min = 2,
5876 .channels_max = 2,
5877 .nid = 0x10, /* NID to query formats and rates */
5878 .ops = {
5879 .open = via_pcm_open_close,
5880 .prepare = via_capture_pcm_prepare,
5881 .cleanup = via_capture_pcm_cleanup,
5882 .close = via_pcm_open_close,
5883 },
5884};
5885
90dd48a1 5886static const struct hda_pcm_stream vt1812_pcm_digital_playback = {
ab6734e7
LW
5887 .substreams = 1,
5888 .channels_min = 2,
5889 .channels_max = 2,
ab6734e7
LW
5890 /* NID is set in via_build_pcms */
5891 .ops = {
5892 .open = via_dig_playback_pcm_open,
5893 .close = via_dig_playback_pcm_close,
5894 .prepare = via_dig_playback_pcm_prepare,
5895 .cleanup = via_dig_playback_pcm_cleanup
5896 },
5897};
5898/* fill in the dac_nids table from the parsed pin configuration */
5899static int vt1812_auto_fill_dac_nids(struct via_spec *spec,
5900 const struct auto_pin_cfg *cfg)
5901{
5902 spec->multiout.num_dacs = 1;
5903 spec->multiout.dac_nids = spec->private_dac_nids;
5904 if (cfg->line_out_pins[0])
dda14410 5905 spec->private_dac_nids[0] = 0x8;
ab6734e7
LW
5906 return 0;
5907}
5908
5909
5910/* add playback controls from the parsed DAC table */
5911static int vt1812_auto_create_multi_out_ctls(struct via_spec *spec,
5912 const struct auto_pin_cfg *cfg)
5913{
5914 int err;
5915
5916 if (!cfg->line_out_pins[0])
5917 return -1;
5918
5919 /* Line-Out: PortE */
5920 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3d83e577 5921 "Front Playback Volume",
ab6734e7
LW
5922 HDA_COMPOSE_AMP_VAL(0x8, 3, 0, HDA_OUTPUT));
5923 if (err < 0)
5924 return err;
5925 err = via_add_control(spec, VIA_CTL_WIDGET_BIND_PIN_MUTE,
3d83e577 5926 "Front Playback Switch",
ab6734e7
LW
5927 HDA_COMPOSE_AMP_VAL(0x28, 3, 0, HDA_OUTPUT));
5928 if (err < 0)
5929 return err;
5930
5931 return 0;
5932}
5933
5934static int vt1812_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
5935{
5936 int err;
5937
5938 if (!pin)
5939 return 0;
5940
5941 spec->multiout.hp_nid = 0x9;
5942 spec->hp_independent_mode_index = 1;
5943
5944
5945 err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
5946 "Headphone Playback Volume",
5947 HDA_COMPOSE_AMP_VAL(
5948 spec->multiout.hp_nid, 3, 0, HDA_OUTPUT));
5949 if (err < 0)
5950 return err;
5951
5952 err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
5953 "Headphone Playback Switch",
5954 HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
5955 if (err < 0)
5956 return err;
5957
5958 create_hp_imux(spec);
5959 return 0;
5960}
5961
5962/* create playback/capture controls for input pins */
10a20af7 5963static int vt1812_auto_create_analog_input_ctls(struct hda_codec *codec,
ab6734e7
LW
5964 const struct auto_pin_cfg *cfg)
5965{
10a20af7 5966 struct via_spec *spec = codec->spec;
ab6734e7 5967 struct hda_input_mux *imux = &spec->private_imux[0];
90dd48a1 5968 static const hda_nid_t pin_idxs[] = { 0x2b, 0x2a, 0x29, 0, 0, 0xff };
f3268512 5969 int err;
ab6734e7 5970
10a20af7 5971 err = vt_auto_create_analog_input_ctls(codec, cfg, 0x21, pin_idxs,
f3268512
TI
5972 ARRAY_SIZE(pin_idxs));
5973 if (err < 0)
5974 return err;
ab6734e7 5975
ab6734e7 5976 /* build volume/mute control of loopback */
7b315bb4 5977 err = via_new_analog_input(spec, "Stereo Mixer", 0, 5, 0x21);
ab6734e7
LW
5978 if (err < 0)
5979 return err;
5980
ab6734e7 5981 /* for digital mic select */
10a20af7 5982 snd_hda_add_imux_item(imux, "Digital Mic", 6, NULL);
ab6734e7
LW
5983
5984 return 0;
5985}
5986
5987static int vt1812_parse_auto_config(struct hda_codec *codec)
5988{
5989 struct via_spec *spec = codec->spec;
5990 int err;
5991
5992
5993 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
5994 if (err < 0)
5995 return err;
5996 fill_dig_outs(codec);
5997 err = vt1812_auto_fill_dac_nids(spec, &spec->autocfg);
5998 if (err < 0)
5999 return err;
6000
6001 if (!spec->autocfg.line_outs && !spec->autocfg.hp_outs)
6002 return 0; /* can't find valid BIOS pin config */
6003
6004 err = vt1812_auto_create_multi_out_ctls(spec, &spec->autocfg);
6005 if (err < 0)
6006 return err;
6007 err = vt1812_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
6008 if (err < 0)
6009 return err;
10a20af7 6010 err = vt1812_auto_create_analog_input_ctls(codec, &spec->autocfg);
ab6734e7
LW
6011 if (err < 0)
6012 return err;
6013
6014 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
6015
6016 fill_dig_outs(codec);
6017
6018 if (spec->kctls.list)
6019 spec->mixers[spec->num_mixers++] = spec->kctls.list;
6020
6021 spec->input_mux = &spec->private_imux[0];
6022
6023 if (spec->hp_mux)
3d83e577 6024 via_hp_build(codec);
ab6734e7
LW
6025
6026 return 1;
6027}
6028
6029#ifdef CONFIG_SND_HDA_POWER_SAVE
90dd48a1 6030static const struct hda_amp_list vt1812_loopbacks[] = {
ab6734e7
LW
6031 { 0x21, HDA_INPUT, 0 },
6032 { 0x21, HDA_INPUT, 1 },
6033 { 0x21, HDA_INPUT, 2 },
6034 { } /* end */
6035};
6036#endif
6037
3e95b9ab
LW
6038static void set_widgets_power_state_vt1812(struct hda_codec *codec)
6039{
6040 struct via_spec *spec = codec->spec;
6041 int imux_is_smixer =
6042 snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
6043 unsigned int parm;
6044 unsigned int present;
6045 /* MUX10 (1eh) = stereo mixer */
6046 imux_is_smixer =
6047 snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
6048 /* inputs */
6049 /* PW 5/6/7 (29h/2ah/2bh) */
6050 parm = AC_PWRST_D3;
6051 set_pin_power_state(codec, 0x29, &parm);
6052 set_pin_power_state(codec, 0x2a, &parm);
6053 set_pin_power_state(codec, 0x2b, &parm);
6054 parm = AC_PWRST_D0;
6055 /* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
6056 snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
6057 snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
6058 snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
6059 snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
6060
6061 /* outputs */
6062 /* AOW0 (8h)*/
6063 snd_hda_codec_write(codec, 0x8, 0,
6064 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6065
6066 /* PW4 (28h), MW4 (18h), MUX4(38h) */
6067 parm = AC_PWRST_D3;
6068 set_pin_power_state(codec, 0x28, &parm);
6069 snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
6070 snd_hda_codec_write(codec, 0x38, 0, AC_VERB_SET_POWER_STATE, parm);
6071
6072 /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
6073 parm = AC_PWRST_D3;
6074 set_pin_power_state(codec, 0x25, &parm);
6075 snd_hda_codec_write(codec, 0x15, 0, AC_VERB_SET_POWER_STATE, parm);
6076 snd_hda_codec_write(codec, 0x35, 0, AC_VERB_SET_POWER_STATE, parm);
6077 if (spec->hp_independent_mode)
6078 snd_hda_codec_write(codec, 0x9, 0,
6079 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6080
6081 /* Internal Speaker */
6082 /* PW0 (24h), MW0(14h), MUX0(34h) */
6083 present = snd_hda_jack_detect(codec, 0x25);
6084
6085 parm = AC_PWRST_D3;
6086 set_pin_power_state(codec, 0x24, &parm);
6087 if (present) {
6088 snd_hda_codec_write(codec, 0x14, 0,
6089 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6090 snd_hda_codec_write(codec, 0x34, 0,
6091 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6092 } else {
6093 snd_hda_codec_write(codec, 0x14, 0,
6094 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6095 snd_hda_codec_write(codec, 0x34, 0,
6096 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6097 }
6098
6099
6100 /* Mono Out */
6101 /* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
6102 present = snd_hda_jack_detect(codec, 0x28);
6103
6104 parm = AC_PWRST_D3;
6105 set_pin_power_state(codec, 0x31, &parm);
6106 if (present) {
6107 snd_hda_codec_write(codec, 0x1c, 0,
6108 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6109 snd_hda_codec_write(codec, 0x3c, 0,
6110 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6111 snd_hda_codec_write(codec, 0x3e, 0,
6112 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6113 } else {
6114 snd_hda_codec_write(codec, 0x1c, 0,
6115 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6116 snd_hda_codec_write(codec, 0x3c, 0,
6117 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6118 snd_hda_codec_write(codec, 0x3e, 0,
6119 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
6120 }
6121
6122 /* PW15 (33h), MW15 (1dh), MUX15(3dh) */
6123 parm = AC_PWRST_D3;
6124 set_pin_power_state(codec, 0x33, &parm);
6125 snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
6126 snd_hda_codec_write(codec, 0x3d, 0, AC_VERB_SET_POWER_STATE, parm);
6127
6128}
ab6734e7
LW
6129
6130/* patch for vt1812 */
6131static int patch_vt1812(struct hda_codec *codec)
6132{
6133 struct via_spec *spec;
6134 int err;
6135
6136 /* create a codec specific record */
5b0cb1d8 6137 spec = via_new_spec(codec);
ab6734e7
LW
6138 if (spec == NULL)
6139 return -ENOMEM;
6140
ab6734e7
LW
6141 /* automatic parse from the BIOS config */
6142 err = vt1812_parse_auto_config(codec);
6143 if (err < 0) {
6144 via_free(codec);
6145 return err;
6146 } else if (!err) {
6147 printk(KERN_INFO "hda_codec: Cannot set up configuration "
6148 "from BIOS. Using genenic mode...\n");
6149 }
6150
6151
6152 spec->init_verbs[spec->num_iverbs++] = vt1812_volume_init_verbs;
6153 spec->init_verbs[spec->num_iverbs++] = vt1812_uniwill_init_verbs;
6154
6155 spec->stream_name_analog = "VT1812 Analog";
6156 spec->stream_analog_playback = &vt1812_pcm_analog_playback;
6157 spec->stream_analog_capture = &vt1812_pcm_analog_capture;
6158
6159 spec->stream_name_digital = "VT1812 Digital";
6160 spec->stream_digital_playback = &vt1812_pcm_digital_playback;
6161
6162
6163 if (!spec->adc_nids && spec->input_mux) {
6164 spec->adc_nids = vt1812_adc_nids;
6165 spec->num_adc_nids = ARRAY_SIZE(vt1812_adc_nids);
6166 get_mux_nids(codec);
6167 override_mic_boost(codec, 0x2b, 0, 3, 40);
6168 override_mic_boost(codec, 0x29, 0, 3, 40);
6169 spec->mixers[spec->num_mixers] = vt1812_capture_mixer;
6170 spec->num_mixers++;
6171 }
6172
6173 codec->patch_ops = via_patch_ops;
6174
6175 codec->patch_ops.init = via_auto_init;
0f48327e 6176 codec->patch_ops.unsol_event = via_unsol_event;
ab6734e7
LW
6177
6178#ifdef CONFIG_SND_HDA_POWER_SAVE
6179 spec->loopback.amplist = vt1812_loopbacks;
6180#endif
6181
3e95b9ab 6182 spec->set_widgets_power_state = set_widgets_power_state_vt1812;
ab6734e7
LW
6183 return 0;
6184}
6185
c577b8a1
JC
6186/*
6187 * patch entries
6188 */
90dd48a1 6189static const struct hda_codec_preset snd_hda_preset_via[] = {
3218c178
TI
6190 { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
6191 { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
6192 { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
6193 { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
6194 { .id = 0x1106e710, .name = "VT1709 10-Ch",
f7278fd0 6195 .patch = patch_vt1709_10ch},
3218c178 6196 { .id = 0x1106e711, .name = "VT1709 10-Ch",
f7278fd0 6197 .patch = patch_vt1709_10ch},
3218c178 6198 { .id = 0x1106e712, .name = "VT1709 10-Ch",
f7278fd0 6199 .patch = patch_vt1709_10ch},
3218c178 6200 { .id = 0x1106e713, .name = "VT1709 10-Ch",
f7278fd0 6201 .patch = patch_vt1709_10ch},
3218c178 6202 { .id = 0x1106e714, .name = "VT1709 6-Ch",
f7278fd0 6203 .patch = patch_vt1709_6ch},
3218c178 6204 { .id = 0x1106e715, .name = "VT1709 6-Ch",
f7278fd0 6205 .patch = patch_vt1709_6ch},
3218c178 6206 { .id = 0x1106e716, .name = "VT1709 6-Ch",
f7278fd0 6207 .patch = patch_vt1709_6ch},
3218c178 6208 { .id = 0x1106e717, .name = "VT1709 6-Ch",
f7278fd0 6209 .patch = patch_vt1709_6ch},
3218c178 6210 { .id = 0x1106e720, .name = "VT1708B 8-Ch",
f7278fd0 6211 .patch = patch_vt1708B_8ch},
3218c178 6212 { .id = 0x1106e721, .name = "VT1708B 8-Ch",
f7278fd0 6213 .patch = patch_vt1708B_8ch},
3218c178 6214 { .id = 0x1106e722, .name = "VT1708B 8-Ch",
f7278fd0 6215 .patch = patch_vt1708B_8ch},
3218c178 6216 { .id = 0x1106e723, .name = "VT1708B 8-Ch",
f7278fd0 6217 .patch = patch_vt1708B_8ch},
3218c178 6218 { .id = 0x1106e724, .name = "VT1708B 4-Ch",
f7278fd0 6219 .patch = patch_vt1708B_4ch},
3218c178 6220 { .id = 0x1106e725, .name = "VT1708B 4-Ch",
f7278fd0 6221 .patch = patch_vt1708B_4ch},
3218c178 6222 { .id = 0x1106e726, .name = "VT1708B 4-Ch",
f7278fd0 6223 .patch = patch_vt1708B_4ch},
3218c178 6224 { .id = 0x1106e727, .name = "VT1708B 4-Ch",
f7278fd0 6225 .patch = patch_vt1708B_4ch},
3218c178 6226 { .id = 0x11060397, .name = "VT1708S",
d949cac1 6227 .patch = patch_vt1708S},
3218c178 6228 { .id = 0x11061397, .name = "VT1708S",
d949cac1 6229 .patch = patch_vt1708S},
3218c178 6230 { .id = 0x11062397, .name = "VT1708S",
d949cac1 6231 .patch = patch_vt1708S},
3218c178 6232 { .id = 0x11063397, .name = "VT1708S",
d949cac1 6233 .patch = patch_vt1708S},
bc92df7f 6234 { .id = 0x11064397, .name = "VT1705",
d949cac1 6235 .patch = patch_vt1708S},
3218c178 6236 { .id = 0x11065397, .name = "VT1708S",
d949cac1 6237 .patch = patch_vt1708S},
3218c178 6238 { .id = 0x11066397, .name = "VT1708S",
d949cac1 6239 .patch = patch_vt1708S},
3218c178 6240 { .id = 0x11067397, .name = "VT1708S",
d949cac1 6241 .patch = patch_vt1708S},
3218c178 6242 { .id = 0x11060398, .name = "VT1702",
d949cac1 6243 .patch = patch_vt1702},
3218c178 6244 { .id = 0x11061398, .name = "VT1702",
d949cac1 6245 .patch = patch_vt1702},
3218c178 6246 { .id = 0x11062398, .name = "VT1702",
d949cac1 6247 .patch = patch_vt1702},
3218c178 6248 { .id = 0x11063398, .name = "VT1702",
d949cac1 6249 .patch = patch_vt1702},
3218c178 6250 { .id = 0x11064398, .name = "VT1702",
d949cac1 6251 .patch = patch_vt1702},
3218c178 6252 { .id = 0x11065398, .name = "VT1702",
d949cac1 6253 .patch = patch_vt1702},
3218c178 6254 { .id = 0x11066398, .name = "VT1702",
d949cac1 6255 .patch = patch_vt1702},
3218c178 6256 { .id = 0x11067398, .name = "VT1702",
d949cac1 6257 .patch = patch_vt1702},
eb7188ca
LW
6258 { .id = 0x11060428, .name = "VT1718S",
6259 .patch = patch_vt1718S},
6260 { .id = 0x11064428, .name = "VT1718S",
6261 .patch = patch_vt1718S},
bb3c6bfc
LW
6262 { .id = 0x11060441, .name = "VT2020",
6263 .patch = patch_vt1718S},
6264 { .id = 0x11064441, .name = "VT1828S",
6265 .patch = patch_vt1718S},
f3db423d
LW
6266 { .id = 0x11060433, .name = "VT1716S",
6267 .patch = patch_vt1716S},
6268 { .id = 0x1106a721, .name = "VT1716S",
6269 .patch = patch_vt1716S},
25eaba2f
LW
6270 { .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
6271 { .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
ab6734e7 6272 { .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
36dd5c4a
LW
6273 { .id = 0x11060440, .name = "VT1818S",
6274 .patch = patch_vt1708S},
11890956
LW
6275 { .id = 0x11060446, .name = "VT1802",
6276 .patch = patch_vt2002P},
6277 { .id = 0x11068446, .name = "VT1802",
6278 .patch = patch_vt2002P},
c577b8a1
JC
6279 {} /* terminator */
6280};
1289e9e8
TI
6281
6282MODULE_ALIAS("snd-hda-codec-id:1106*");
6283
6284static struct hda_codec_preset_list via_list = {
6285 .preset = snd_hda_preset_via,
6286 .owner = THIS_MODULE,
6287};
6288
6289MODULE_LICENSE("GPL");
6290MODULE_DESCRIPTION("VIA HD-audio codec");
6291
6292static int __init patch_via_init(void)
6293{
6294 return snd_hda_add_codec_preset(&via_list);
6295}
6296
6297static void __exit patch_via_exit(void)
6298{
6299 snd_hda_delete_codec_preset(&via_list);
6300}
6301
6302module_init(patch_via_init)
6303module_exit(patch_via_exit)
This page took 0.624066 seconds and 5 git commands to generate.