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