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