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